Index: head/usr.sbin/ppp/auth.c =================================================================== --- head/usr.sbin/ppp/auth.c (revision 6763) +++ head/usr.sbin/ppp/auth.c (revision 6764) @@ -1,202 +1,203 @@ /* * PPP Secret Key Module * * Written by Toshiharu OHNO (tony-o@iij.ad.jp) * * Copyright (C) 1994, Internet Initiative Japan, Inc. All rights reserverd. * * Redistribution and use in source and binary forms are permitted * provided that the above copyright notice and this paragraph are * duplicated in all such forms and that any documentation, * advertising materials, and other materials related to such * distribution and use acknowledge that the software was developed * by the Internet Initiative Japan, Inc. The name of the * IIJ may not be used to endorse or promote products derived * from this software without specific prior written permission. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. * - * $Id:$ + * $Id: auth.c,v 1.2 1995/02/26 12:17:12 amurai Exp $ * * TODO: * o Imprement check against with registerd IP addresses. */ #include "fsm.h" #include "lcpproto.h" #include "ipcp.h" #include "vars.h" #include "auth.h" extern FILE *OpenSecret(); extern void CloseSecret(); LOCAL_AUTH_VALID LocalAuthInit(void){ char *p; - + if ( gethostname( VarShortHost, sizeof(VarShortHost))) { - return(1); + return(NOT_FOUND); } if ( p = strchr( VarShortHost, '.' ) ) *p = '\0'; + VarLocalAuth = LOCAL_NO_AUTH; return LocalAuthValidate( SECRETFILE, VarShortHost, "" ); } LOCAL_AUTH_VALID LocalAuthValidate( char *fname, char *system, char *key) { FILE *fp; int n; char *vector[20]; /* XXX */ char buff[200]; /* XXX */ LOCAL_AUTH_VALID rc; rc = NOT_FOUND; /* No system entry */ fp = OpenSecret(fname); if (fp == NULL) return( rc ); while (fgets(buff, sizeof(buff), fp)) { if (buff[0] == '#') continue; buff[strlen(buff)-1] = 0; bzero(vector, sizeof(vector)); n = MakeArgs(buff, &vector); if (n < 1) continue; if (strcmp(vector[0], system) == 0) { if ( vector[1] != (char *) NULL && strcmp(vector[1], key) == 0) { rc = VALID; /* Valid */ } else { rc = INVALID; /* Invalid */ } break; } } CloseSecret(fp); return( rc ); } int AuthValidate(fname, system, key) char *fname, *system, *key; { FILE *fp; int n; char *vector[20]; char buff[200]; char passwd[100]; fp = OpenSecret(fname); if (fp == NULL) return(0); while (fgets(buff, sizeof(buff), fp)) { if (buff[0] == '#') continue; buff[strlen(buff)-1] = 0; bzero(vector, sizeof(vector)); n = MakeArgs(buff, &vector); if (n < 2) continue; if (strcmp(vector[0], system) == 0) { ExpandString(vector[1], passwd, 0); if (strcmp(passwd, key) == 0) { CloseSecret(fp); bzero(&DefHisAddress, sizeof(DefHisAddress)); n -= 2; if (n > 0) { ParseAddr(n--, &vector[2], &DefHisAddress.ipaddr, &DefHisAddress.mask, &DefHisAddress.width); } IpcpInit(); return(1); /* Valid */ } } } CloseSecret(fp); return(0); /* Invalid */ } char * AuthGetSecret(fname, system, len, setaddr) char *fname, *system; int len, setaddr; { FILE *fp; int n; char *vector[20]; char buff[200]; static char passwd[100]; fp = OpenSecret(fname); if (fp == NULL) return(NULL); while (fgets(buff, sizeof(buff), fp)) { if (buff[0] == '#') continue; buff[strlen(buff)-1] = 0; bzero(vector, sizeof(vector)); n = MakeArgs(buff, &vector); if (n < 2) continue; if (strlen(vector[0]) == len && strncmp(vector[0], system, len) == 0) { ExpandString(vector[1], passwd, 0); if (setaddr) { bzero(&DefHisAddress, sizeof(DefHisAddress)); } n -= 2; if (n > 0 && setaddr) { #ifdef DEBUG LogPrintf(LOG_LCP, "*** n = %d, %s\n", n, vector[2]); #endif ParseAddr(n--, &vector[2], &DefHisAddress.ipaddr, &DefHisAddress.mask, &DefHisAddress.width); IpcpInit(); } return(passwd); } } CloseSecret(fp); return(NULL); /* Invalid */ } static void AuthTimeout(authp) struct authinfo *authp; { struct pppTimer *tp; tp = &authp->authtimer; StopTimer(tp); if (--authp->retry > 0) { StartTimer(tp); (authp->ChallengeFunc)(++authp->id); } } void StartAuthChallenge(authp) struct authinfo *authp; { struct pppTimer *tp; tp = &authp->authtimer; StopTimer(tp); tp->func = AuthTimeout; tp->load = VarRetryTimeout * SECTICKS; tp->state = TIMER_STOPPED; tp->arg = (void *)authp; StartTimer(tp); authp->retry = 3; authp->id = 1; (authp->ChallengeFunc)(authp->id); } void StopAuthTimer(authp) struct authinfo *authp; { StopTimer(&authp->authtimer); } Index: head/usr.sbin/ppp/command.c =================================================================== --- head/usr.sbin/ppp/command.c (revision 6763) +++ head/usr.sbin/ppp/command.c (revision 6764) @@ -1,832 +1,832 @@ /* * PPP User command processing module * * Written by Toshiharu OHNO (tony-o@iij.ad.jp) * * Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd. * * Redistribution and use in source and binary forms are permitted * provided that the above copyright notice and this paragraph are * duplicated in all such forms and that any documentation, * advertising materials, and other materials related to such * distribution and use acknowledge that the software was developed * by the Internet Initiative Japan, Inc. The name of the * IIJ may not be used to endorse or promote products derived * from this software without specific prior written permission. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. * - * $Id:$ + * $Id: command.c,v 1.2 1995/02/26 12:17:22 amurai Exp $ * */ #include #include #include "fsm.h" #include "phase.h" #include "lcp.h" #include "ipcp.h" #include "modem.h" #include "command.h" #include "hdlc.h" #include "vars.h" #include "auth.h" #include #include #include #include #include "os.h" extern int MakeArgs(); extern void Cleanup(), TtyTermMode(), PacketMode(); extern int EnableCommand(), DisableCommand(), DisplayCommand(); extern int AcceptCommand(), DenyCommand(); extern int LocalAuthCommand(); extern int LoadCommand(), SaveCommand(); extern int ChangeParity(char *); extern int SelectSystem(); extern int ShowRoute(); extern struct pppvars pppVars; struct in_addr ifnetmask; static int ShowCommand(), TerminalCommand(), QuitCommand(); static int CloseCommand(), DialCommand(), DownCommand(); static int SetCommand(), AddCommand(), DeleteCommand(); static int HelpCommand(list, argc, argv, plist) struct cmdtab *list; int argc; char **argv; struct cmdtab *plist; { struct cmdtab *cmd; int n; char c; if (argc > 0) { for (cmd = plist; cmd->name; cmd++) { - if (strcmp(cmd->name, *argv) == 0) { + if (strcmp(cmd->name, *argv) == 0 && (cmd->lauth & VarLocalAuth)) { printf("%s %s\n", cmd->name, cmd->syntax); return(1); } } return(1); } n = 0; for (cmd = plist; cmd->func; cmd++) { - c = (n & 1)? '\n' : '\t'; - if (cmd->name) { + if (cmd->name && (cmd->lauth & VarLocalAuth)) { + c = (n & 1)? '\n' : '\t'; printf(" %-8s: %-20s%c", cmd->name, cmd->helpmes, c); n++; } } if (n & 1) printf("\n"); return(1); } int IsInteractive() { char *mes = NULL; if (mode & MODE_AUTO) mes = "Working as auto mode."; else if (mode & MODE_DIRECT) mes = "Working as direct mode."; else if (mode & MODE_DEDICATED) mes = "Workring as dedicated mode."; if (mes) { printf("%s\n", mes); return(0); } return(1); } static int DialCommand(cmdlist, argc, argv) struct cmdtab *cmdlist; int argc; char **argv; { if (LcpFsm.state > ST_CLOSED) { printf("LCP state is [%s]\n", StateNames[LcpFsm.state]); return(1); } if (!IsInteractive()) return(1); if (argc > 0) { if (SelectSystem(*argv, CONFFILE) < 0) { printf("%s: not found.\n", *argv); return(1); } } modem = OpenModem(mode); if (modem < 0) { printf("failed to open modem.\n"); modem = 0; return(1); } if (DialModem()) { sleep(1); ModemTimeout(); PacketMode(); } return(1); } static char StrOption[] = "option .."; static char StrRemote[] = "[remote]"; char StrNull[] = ""; struct cmdtab Commands[] = { { "accept", NULL, AcceptCommand, LOCAL_AUTH, "accept option request", StrOption}, { "add", NULL, AddCommand, LOCAL_AUTH, "add route", "dest mask gateway"}, { "close", NULL, CloseCommand, LOCAL_AUTH, "Close connection", StrNull}, { "delete", NULL, DeleteCommand, LOCAL_AUTH, "delete route", "dest gateway"}, { "deny", NULL, DenyCommand, LOCAL_AUTH, "Deny option request", StrOption}, { "dial", "call", DialCommand, LOCAL_AUTH, "Dial and login", StrRemote}, { "disable", NULL, DisableCommand, LOCAL_AUTH, "Disable option", StrOption}, { "display", NULL, DisplayCommand, LOCAL_AUTH, "Display option configs", StrNull}, { "enable", NULL, EnableCommand, LOCAL_AUTH, "Enable option", StrOption}, - { "passwd", NULL, LocalAuthCommand,LOCAL_NO_AUTH | LOCAL_NO_AUTH, + { "passwd", NULL, LocalAuthCommand,LOCAL_NO_AUTH, "Password for manupilation", StrOption}, { "load", NULL, LoadCommand, LOCAL_AUTH, "Load settings", StrRemote}, { "save", NULL, SaveCommand, LOCAL_AUTH, "Save settings", StrNull}, { "set", "setup", SetCommand, LOCAL_AUTH, "Set parameters", "var value"}, { "show", NULL, ShowCommand, LOCAL_AUTH, "Show status and statictics", "var"}, { "term", NULL, TerminalCommand,LOCAL_AUTH, "Enter to terminal mode", StrNull}, - { "quit", "bye", QuitCommand, LOCAL_AUTH, + { "quit", "bye", QuitCommand, LOCAL_AUTH | LOCAL_NO_AUTH, "Quit PPP program", StrNull}, { "help", "?", HelpCommand, LOCAL_AUTH | LOCAL_NO_AUTH, "Display this message", "[command]", (void *)Commands }, { NULL, "down", DownCommand, LOCAL_AUTH, "Generate down event", StrNull}, { NULL, NULL, NULL }, }; extern int ReportCcpStatus(); extern int ReportLcpStatus(); extern int ReportIpcpStatus(); extern int ReportProtStatus(); extern int ReportCompress(); extern int ShowModemStatus(); extern int ReportHdlcStatus(); extern int ShowMemMap(); static char *LogLevelName[] = { LM_PHASE, LM_CHAT, LM_LQM, LM_LCP, LM_TCPIP, LM_HDLC, LM_ASYNC, }; static int ShowDebugLevel() { int i; printf("%02x: ", loglevel); for (i = LOG_PHASE; i < MAXLOGLEVEL; i++) { if (loglevel & (1 << i)) printf("%s ", LogLevelName[i]); } printf("\n"); return(1); } static int ShowEscape() { int code, bit; if (EscMap[32]) { for (code = 0; code < 32; code++) { if (EscMap[code]) { for (bit = 0; bit < 8; bit++) { if (EscMap[code] & (1<func) { if (cmds->name && strncmp(str, cmds->name, len) == 0) { nmatch++; found = cmds; } else if (cmds->alias && strncmp(str, cmds->alias, len) == 0) { nmatch++; found = cmds; } cmds++; } *pmatch = nmatch; return(found); } int FindExec(cmdlist, argc, argv) struct cmdtab *cmdlist; int argc; char **argv; { struct cmdtab *cmd; int val = 1; int nmatch; cmd = FindCommand(cmdlist, *argv, &nmatch); if (nmatch > 1) printf("Ambiguous.\n"); else if (cmd && ( cmd->lauth & VarLocalAuth ) ) val = (cmd->func)(cmd, --argc, ++argv, cmd->args); else printf("what?\n"); return(val); } void Prompt(flag) int flag; { char *pconnect, *pauth; if (!(mode & MODE_INTER)) return; if (flag) printf("\n"); if ( VarLocalAuth == LOCAL_AUTH ) pauth = " ON "; else pauth = " on "; if (IpcpFsm.state == ST_OPENED && phase == PHASE_NETWORK) pconnect = "PPP"; else pconnect = "ppp"; printf("%s%s%s> ", pconnect, pauth, VarShortHost); fflush(stdout); } void DecodeCommand(buff, nb, prompt) char *buff; int nb; int prompt; { char *vector[20]; char **argv; int argc, val; char *cp; val = 1; if (nb > 0) { cp = buff + strcspn(buff, "\r\n"); if (cp) *cp = '\0'; { argc = MakeArgs(buff, &vector); argv = vector; if (argc > 0) val = FindExec(Commands, argc, argv); } } if (val && prompt) Prompt(0); } static int ShowCommand(list, argc, argv) struct cmdtab *list; int argc; char **argv; { int val = 1; if (argc > 0) val = FindExec(ShowCommands, argc, argv); else printf("Use ``show ?'' to get a list.\n"); return(val); } static int TerminalCommand() { if (LcpFsm.state > ST_CLOSED) { printf("LCP state is [%s]\n", StateNames[LcpFsm.state]); return(1); } if (!IsInteractive()) return(1); modem = OpenModem(mode); if (modem < 0) { printf("failed to open modem.\n"); modem = 0; return(1); } printf("Enter to terminal mode.\n"); printf("Type `~?' for help.\n"); TtyTermMode(); return(0); } static int QuitCommand(list, argc, argv) struct cmdtab *list; int argc; char **argv; { if (mode & (MODE_DIRECT|MODE_DEDICATED|MODE_AUTO)) { - if (argc > 0) { + if (argc > 0 && (VarLocalAuth & LOCAL_AUTH)) { Cleanup(EX_NORMAL); } else { VarLocalAuth = LOCAL_NO_AUTH; close(netfd); close(1); netfd = -1; mode &= ~MODE_INTER; } } else Cleanup(EX_NORMAL); return(1); } static int CloseCommand() { LcpClose(); return(1); } static int DownCommand() { LcpDown(); return(1); } static int SetModemSpeed(list, argc, argv) struct cmdtab *list; int argc; char **argv; { int speed; if (argc > 0) { if (strcmp(*argv, "sync") == 0) { VarSpeed = 0; return(1); } speed = atoi(*argv); if (IntToSpeed(speed) != B0) { VarSpeed = speed; return(1); } printf("invalid speed.\n"); } return(1); } static int SetModemParity(list, argc, argv) struct cmdtab *list; int argc; char **argv; { int parity; if (argc > 0) { parity = ChangeParity(*argv); if (parity < 0) printf("Invalid parity.\n"); else VarParity = parity; } return(1); } static int SetDebugLevel(list, argc, argv) struct cmdtab *list; int argc; char **argv; { int level, w; for (level = 0; argc-- > 0; argv++) { if (isdigit(**argv)) { w = atoi(*argv); if (w < 0 || w >= MAXLOGLEVEL) { printf("invalid log level.\n"); break; } else level |= (1 << w); } else { for (w = 0; w < MAXLOGLEVEL; w++) { if (strcasecmp(*argv, LogLevelName[w]) == 0) { level |= (1 << w); continue; } } } } loglevel = level; return(1); } static int SetEscape(list, argc, argv) struct cmdtab *list; int argc; char **argv; { int code; for (code = 0; code < 33; code++) EscMap[code] = 0; while (argc-- > 0) { sscanf(*argv++, "%x", &code); code &= 0xff; EscMap[code >> 3] |= (1 << (code&7)); EscMap[32] = 1; } return(1); } static int SetInitialMRU(list, argc, argv) struct cmdtab *list; int argc; char **argv; { int mru; if (argc > 0) { mru = atoi(*argv); if (mru < 100) printf("given value is too small.\n"); else if (mru > MAX_MRU) printf("given value is too big.\n"); else VarMRU = mru; } return(1); } static int SetIdleTimeout(list, argc, argv) struct cmdtab *list; int argc; char **argv; { if (argc-- > 0) { VarIdleTimeout = atoi(*argv++); if (argc-- > 0) { VarLqrTimeout = atoi(*argv++); if (VarLqrTimeout < 1) VarLqrTimeout = 30; if (argc > 0) { VarRetryTimeout = atoi(*argv); if (VarRetryTimeout < 1 || VarRetryTimeout > 10) VarRetryTimeout = 3; } } } return(1); } struct in_addr GetIpAddr(cp) char *cp; { struct hostent *hp; struct in_addr ipaddr; hp = gethostbyname(cp); if (hp && hp->h_addrtype == AF_INET) bcopy(hp->h_addr, &ipaddr, hp->h_length); else if (inet_aton(cp, &ipaddr) == 0) ipaddr.s_addr = 0; return(ipaddr); } static int SetInterfaceAddr(list, argc, argv) struct cmdtab *list; int argc; char **argv; { int width; DefMyAddress.ipaddr.s_addr = DefHisAddress.ipaddr.s_addr = 0L; if (argc > 0) { ParseAddr(argc, argv++, &DefMyAddress.ipaddr, &DefMyAddress.mask, &DefMyAddress.width); if (--argc > 0) { ParseAddr(argc, argv++, &DefHisAddress.ipaddr, &DefHisAddress.mask, &DefHisAddress.width); if (--argc > 0) { ifnetmask = GetIpAddr(*argv); } } } /* * For backwards compatibility, 0.0.0.0 means any address. */ if (DefMyAddress.ipaddr.s_addr == 0) { DefMyAddress.mask.s_addr = 0; DefMyAddress.width = 0; } if (DefHisAddress.ipaddr.s_addr == 0) { DefHisAddress.mask.s_addr = 0; DefHisAddress.width = 0; } if ((mode & MODE_AUTO) || ((mode & MODE_DEDICATED) && dstsystem)) { OsSetIpaddress(DefMyAddress.ipaddr, DefHisAddress.ipaddr, ifnetmask); } return(1); } #define VAR_AUTHKEY 0 #define VAR_DIAL 1 #define VAR_LOGIN 2 #define VAR_AUTHNAME 3 #define VAR_DEVICE 4 #define VAR_ACCMAP 5 #define VAR_PHONE 6 static int SetVariable(list, argc, argv, param) struct cmdtab *list; int argc; char **argv; int param; { u_long map; if (argc > 0) { switch (param) { case VAR_AUTHKEY: strncpy(VarAuthKey, *argv, sizeof(VarAuthKey)-1); break; case VAR_AUTHNAME: strncpy(VarAuthName, *argv, sizeof(VarAuthName)-1); break; case VAR_DIAL: strncpy(VarDialScript, *argv, sizeof(VarDialScript)-1); break; case VAR_LOGIN: strncpy(VarLoginScript, *argv, sizeof(VarDialScript)-1); break; case VAR_DEVICE: strncpy(VarDevice, *argv, sizeof(VarDevice)-1); break; case VAR_ACCMAP: sscanf(*argv, "%x", &map); VarAccmap = map; break; case VAR_PHONE: strncpy(VarPhone, *argv, sizeof(VarPhone)-1); break; } } return(1); } static int SetOpenMode(list, argc, argv) struct cmdtab *list; int argc; char **argv; { if (argc > 0) { if (strcmp(*argv, "active") == 0) VarOpenMode = OPEN_ACTIVE; else if (strcmp(*argv, "passive") == 0) VarOpenMode = OPEN_PASSIVE; else printf("Invalid mode.\n"); } return(1); } static char StrChatStr[] = "chat-script"; static char StrValue[] = "value"; extern int SetIfilter(), SetOfilter(), SetDfilter(), SetAfilter(); struct cmdtab SetCommands[] = { { "accmap", NULL, SetVariable, LOCAL_AUTH, "Set accmap value", "hex-value", (void *)VAR_ACCMAP}, { "afilter", NULL, SetAfilter, LOCAL_AUTH, "Set keep Alive filter", "..."}, { "authkey", "key", SetVariable, LOCAL_AUTH, "Set authentication key", "key", (void *)VAR_AUTHKEY}, { "authname", NULL, SetVariable, LOCAL_AUTH, "Set authentication name", "name", (void *)VAR_AUTHNAME}, { "debug", NULL, SetDebugLevel, LOCAL_AUTH, "Set debug level", StrValue}, { "device", "line", SetVariable, LOCAL_AUTH, "Set modem device name", "device-name", (void *)VAR_DEVICE}, { "dfilter", NULL, SetDfilter, LOCAL_AUTH, "Set demand filter", "..."}, { "dial", NULL, SetVariable, LOCAL_AUTH, "Set dialing script", StrChatStr, (void *)VAR_DIAL}, { "escape", NULL, SetEscape, LOCAL_AUTH, "Set escape characters", "hex-digit ..."}, { "ifaddr", NULL, SetInterfaceAddr, LOCAL_AUTH, "Set destination address", "src-addr dst-addr netmask"}, { "ifilter", NULL, SetIfilter, LOCAL_AUTH, "Set input filter", "..."}, { "login", NULL, SetVariable, LOCAL_AUTH, "Set login script", StrChatStr, (void *)VAR_LOGIN }, { "mru", "mtu", SetInitialMRU, LOCAL_AUTH, "Set Initial MRU value", StrValue }, { "ofilter", NULL, SetOfilter, LOCAL_AUTH, "Set output filter", "..." }, { "openmode", NULL, SetOpenMode, LOCAL_AUTH, "Set open mode", "[active|passive]"}, { "parity", NULL, SetModemParity, LOCAL_AUTH, "Set modem parity", "[odd|even|none]"}, { "phone", NULL, SetVariable, LOCAL_AUTH, "Set telephone number", "phone-number", (void *)VAR_PHONE }, { "speed", NULL, SetModemSpeed, LOCAL_AUTH, "Set modem speed", "speed"}, { "timeout", NULL, SetIdleTimeout, LOCAL_AUTH, "Set Idle timeout", StrValue}, { "help", "?", HelpCommand, LOCAL_AUTH | LOCAL_NO_AUTH, "Display this message", StrNull, (void *)SetCommands}, { NULL, NULL, NULL }, }; static int SetCommand(list, argc, argv) struct cmdtab *list; int argc; char **argv; { int val = 1; if (argc > 0) val = FindExec(SetCommands, argc, argv); else printf("Use ``set ?'' to get a list.\n"); return(val); } static int AddCommand(list, argc, argv) struct cmdtab *list; int argc; char **argv; { struct in_addr dest, gateway, netmask; if (argc == 3) { dest = GetIpAddr(argv[0]); netmask = GetIpAddr(argv[1]); if (strcmp(argv[2], "HISADDR") == 0) gateway = IpcpInfo.his_ipaddr; else gateway = GetIpAddr(argv[2]); OsSetRoute(RTM_ADD, dest, gateway, netmask); } else { printf("Usage: %s %s\n", list->name, list->syntax); } return(1); } static int DeleteCommand(list, argc, argv) struct cmdtab *list; int argc; char **argv; { struct in_addr dest, gateway, netmask; if (argc >= 2) { dest = GetIpAddr(argv[0]); if (strcmp(argv[1], "HISADDR") == 0) gateway = IpcpInfo.his_ipaddr; else gateway = GetIpAddr(argv[1]); netmask.s_addr = 0; if (argc == 3) { if (inet_aton(argv[1], &netmask) == 0) { printf("bad netmask value.\n"); return(1); } } OsSetRoute(RTM_DELETE, dest, gateway, netmask); } else if (argc == 1 && strcmp(argv[0], "ALL") == 0) { DeleteIfRoutes(0); } else { printf("Usage: %s %s\n", list->name, list->syntax); } return(1); } Index: head/usr.sbin/ppp/main.c =================================================================== --- head/usr.sbin/ppp/main.c (revision 6763) +++ head/usr.sbin/ppp/main.c (revision 6764) @@ -1,753 +1,770 @@ /* * User Process PPP * * Written by Toshiharu OHNO (tony-o@iij.ad.jp) * * Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd. * * Redistribution and use in source and binary forms are permitted * provided that the above copyright notice and this paragraph are * duplicated in all such forms and that any documentation, * advertising materials, and other materials related to such * distribution and use acknowledge that the software was developed * by the Internet Initiative Japan, Inc. The name of the * IIJ may not be used to endorse or promote products derived * from this software without specific prior written permission. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. * - * $Id:$ + * $Id: main.c,v 1.2 1995/02/26 12:17:41 amurai Exp $ * * TODO: * o Add commands for traffic summary, version display, etc. * o Add signal handler for misc controls. */ #include "fsm.h" #include #include #include #include #include #include #include #include #include #include "modem.h" #include "os.h" #include "hdlc.h" #include "lcp.h" #include "ipcp.h" #include "vars.h" #include "auth.h" +#define LAUTH_M1 "Warning: No password entry for this host in ppp.secret\n" +#define LAUTH_M2 "Warning: All manipulation is allowed by anyone in a world\n" + #ifndef O_NONBLOCK #ifdef O_NDELAY #define O_NONBLOCK O_NDELAY #endif #endif extern void VjInit(), AsyncInit(); extern void AsyncInput(), IpOutput(); extern int SelectSystem(); extern void DecodeCommand(), Prompt(); extern int IsInteractive(); extern struct in_addr ifnetmask; static void DoLoop(void); static struct termios oldtio; /* Original tty mode */ static struct termios comtio; /* Command level tty mode */ static int TermMode; static int server, update; struct sockaddr_in ifsin; static void TtyInit() { struct termios newtio; int stat; stat = fcntl(0, F_GETFL, 0); stat |= O_NONBLOCK; fcntl(0, F_SETFL, stat); newtio = oldtio; newtio.c_lflag &= ~(ECHO|ISIG|ICANON); newtio.c_iflag = 0; newtio.c_oflag &= ~OPOST; newtio.c_cc[VEOF] = _POSIX_VDISABLE; newtio.c_cc[VINTR] = _POSIX_VDISABLE; newtio.c_cc[VMIN] = 1; newtio.c_cc[VTIME] = 0; newtio.c_cflag |= CS8; tcsetattr(0, TCSADRAIN, &newtio); comtio = newtio; } /* * Set tty into command mode. We allow canonical input and echo processing. */ static void TtyCommandMode() { struct termios newtio; int stat; if (!(mode & MODE_INTER)) return; tcgetattr(0, &newtio); newtio.c_lflag |= (ECHO|ICANON); newtio.c_iflag = oldtio.c_iflag; newtio.c_oflag |= OPOST; tcsetattr(0, TCSADRAIN, &newtio); stat = fcntl(0, F_GETFL, 0); stat |= O_NONBLOCK; fcntl(0, F_SETFL, stat); TermMode = 0; Prompt(0); } /* * Set tty into terminal mode which is used while we invoke term command. */ void TtyTermMode() { int stat; tcsetattr(0, TCSADRAIN, &comtio); stat = fcntl(0, F_GETFL, 0); stat &= ~O_NONBLOCK; fcntl(0, F_SETFL, stat); TermMode = 1; } void Cleanup(excode) int excode; { int stat; OsLinkdown(); #ifdef notdef stat = fcntl(0, F_GETFL, 0); stat &= ~O_NONBLOCK; fcntl(0, F_SETFL, stat); tcsetattr(0, TCSANOW, &oldtio); #endif OsCloseLink(1); sleep(1); if (mode & MODE_AUTO) DeleteIfRoutes(1); OsInterfaceDown(1); LogPrintf(LOG_PHASE, "PPP Terminated.\n"); LogClose(); if (server > 0) close(server); #ifndef notdef stat = fcntl(0, F_GETFL, 0); stat &= ~O_NONBLOCK; fcntl(0, F_SETFL, stat); tcsetattr(0, TCSANOW, &oldtio); #endif exit(excode); } static void Hangup() { LogPrintf(LOG_PHASE, "SIGHUP\n"); signal(SIGHUP, Hangup); Cleanup(EX_HANGUP); } static void CloseSession() { LogPrintf(LOG_PHASE, "SIGTERM\n"); LcpClose(); Cleanup(EX_TERM); } void Usage() { fprintf(stderr, "Usage: ppp [-auto | -direct -dedicated] [system]\n"); exit(EX_START); } void ProcessArgs(int argc, char **argv) { int optc; char *cp; optc = 0; while (argc > 0 && **argv == '-') { cp = *argv + 1; if (strcmp(cp, "auto") == 0) mode |= MODE_AUTO; else if (strcmp(cp, "direct") == 0) mode |= MODE_DIRECT; else if (strcmp(cp, "dedicated") == 0) mode |= MODE_DEDICATED; else Usage(); optc++; argv++; argc--; } if (argc > 1) { fprintf(stderr, "specify only one system label.\n"); exit(EX_START); } if (argc == 1) dstsystem = *argv; if (optc > 1) { fprintf(stderr, "specify only one mode.\n"); exit(EX_START); } } static void Greetings() { printf("User Process PPP. Written by Toshiharu OHNO.\r\n"); fflush(stdout); } void main(argc, argv) int argc; char **argv; { int tunno; int on = 1; argc--; argv++; mode = MODE_INTER; /* default operation is interactive mode */ netfd = -1; ProcessArgs(argc, argv); Greetings(); GetUid(); IpcpDefAddress(); if (SelectSystem("default", CONFFILE) < 0) { fprintf(stderr, "Warning: No default entry is given in config file.\n"); } if (LogOpen()) exit(EX_START); switch ( LocalAuthInit() ) { case NOT_FOUND: - fprintf(stderr, "Warning: No password entry in secret file\n"); - fprintf(stderr, "Warning: Anyone is allowd manipulating!!!\n"); + fprintf(stderr,LAUTH_M1); + fprintf(stderr,LAUTH_M2); + fflush (stderr); + /* Fall down */ + case VALID: VarLocalAuth = LOCAL_AUTH; break; default: break; } if (OpenTunnel(&tunno) < 0) { perror("open_tun"); exit(EX_START); } if (mode & (MODE_AUTO|MODE_DIRECT|MODE_DEDICATED)) mode &= ~MODE_INTER; if (mode & MODE_INTER) { printf("Interactive mode\n"); netfd = 0; } else if (mode & MODE_AUTO) { printf("Automatic mode\n"); if (dstsystem == NULL) { fprintf(stderr, "Destination system must be specified in auto mode.\n"); exit(EX_START); } } tcgetattr(0, &oldtio); /* Save original tty mode */ signal(SIGHUP, Hangup); signal(SIGTERM, CloseSession); signal(SIGINT, CloseSession); #ifdef SIGSEGV signal(SIGSEGV, Hangup); #endif #ifdef SIGPIPE signal(SIGPIPE, Hangup); #endif #ifdef SIGALRM signal(SIGALRM, SIG_IGN); #endif if (dstsystem) { if (SelectSystem(dstsystem, CONFFILE) < 0) { fprintf(stderr, "Destination system not found in conf file.\n"); Cleanup(EX_START); } if ((mode & MODE_AUTO) && DefHisAddress.ipaddr.s_addr == INADDR_ANY) { fprintf(stderr, "Must specify dstaddr with auto mode.\n"); Cleanup(EX_START); } } if (mode & MODE_DIRECT) printf("Packet mode enabled.\n"); #ifdef notdef if (mode & MODE_AUTO) { OsSetIpaddress(IpcpInfo.want_ipaddr, IpcpInfo.his_ipaddr, ifnetmask); } #endif if (!(mode & MODE_INTER)) { int port = SERVER_PORT + tunno; /* * Create server socket and listen at there. */ server = socket(PF_INET, SOCK_STREAM, 0); if (server < 0) { perror("socket"); Cleanup(EX_SOCK); } ifsin.sin_family = AF_INET; ifsin.sin_addr.s_addr = INADDR_ANY; ifsin.sin_port = htons(port); if (bind(server, (struct sockaddr *) &ifsin, sizeof(ifsin)) < 0) { perror("bind"); if (errno == EADDRINUSE) fprintf(stderr, "Wait for a while, then try again.\n"); Cleanup(EX_SOCK); } listen(server, 5); DupLog(); if (!(mode & MODE_DIRECT)) { if (fork()) exit(0); } LogPrintf(LOG_PHASE, "Listening at %d.\n", port); #ifdef DOTTYINIT if (mode & (MODE_DIRECT|MODE_DEDICATED)) { /* } */ #else if (mode & MODE_DIRECT) { #endif TtyInit(); } else { setsid(); /* detach control tty */ } } else { server = -1; TtyInit(); TtyCommandMode(); } LogPrintf(LOG_PHASE, "PPP Started.\n"); do DoLoop(); while (mode & MODE_DEDICATED); Cleanup(EX_DONE); } /* * Turn into packet mode, where we speek PPP. */ void PacketMode() { if (RawModem(modem) < 0) { fprintf(stderr, "Not connected.\r\n"); return; } AsyncInit(); VjInit(); LcpInit(); IpcpInit(); CcpInit(); LcpUp(); if (mode & (MODE_DIRECT|MODE_DEDICATED)) LcpOpen(OPEN_ACTIVE); else LcpOpen(VarOpenMode); if ((mode & (MODE_INTER|MODE_AUTO)) == MODE_INTER) { TtyCommandMode(); fprintf(stderr, "Packet mode.\r\n"); } } static void ShowHelp() { fprintf(stderr, "Following commands are available\r\n"); fprintf(stderr, " ~p\tEnter to Packet mode\r\n"); fprintf(stderr, " ~.\tTerminate program\r\n"); } static void ReadTty() { int n; char ch; static int ttystate; #define MAXLINESIZE 200 char linebuff[MAXLINESIZE]; #ifdef DEBUG logprintf("termode = %d, netfd = %d, mode = %d\n", TermMode, netfd, mode); #endif if (!TermMode) { n = read(netfd, linebuff, sizeof(linebuff)-1); if (n > 0) { DecodeCommand(linebuff, n, 1); } else { #ifdef DEBUG logprintf("connection closed.\n"); #endif - VarLocalAuth = LOCAL_NO_AUTH; close(netfd); netfd = -1; mode &= ~MODE_INTER; } return; } /* * We are in terminal mode, decode special sequences */ n = read(0, &ch, 1); #ifdef DEBUG logprintf("got %d bytes\n", n); #endif if (n > 0) { switch (ttystate) { case 0: if (ch == '~') ttystate++; else write(modem, &ch, n); break; case 1: switch (ch) { case '?': ShowHelp(); break; case '-': if (loglevel > 0) { loglevel--; fprintf(stderr, "New loglevel is %d\r\n", loglevel); } break; case '+': loglevel++; fprintf(stderr, "New loglevel is %d\r\n", loglevel); break; #ifdef DEBUG case 'm': ShowMemMap(); break; #endif case 'p': /* * XXX: Should check carrier. */ if (LcpFsm.state <= ST_CLOSED) { VarOpenMode = OPEN_ACTIVE; PacketMode(); } break; #ifdef DEBUG case 't': ShowTimers(); break; #endif case '.': TermMode = 1; TtyCommandMode(); break; default: if (write(modem, &ch, n) < 0) fprintf(stderr, "err in write.\r\n"); break; } ttystate = 0; break; } } } /* * Here, we'll try to detect HDLC frame */ static char *FrameHeaders[] = { "\176\377\003\300\041", "\176\377\175\043\300\041", "\176\177\175\043\100\041", "\176\175\337\175\043\300\041", "\176\175\137\175\043\100\041", NULL, }; u_char * HdlcDetect(cp, n) u_char *cp; int n; { char *ptr, *fp, **hp; cp[n] = '\0'; /* be sure to null terminated */ ptr = NULL; for (hp = FrameHeaders; *hp; hp++) { fp = *hp; if (DEV_IS_SYNC) fp++; if (ptr = strstr((char *)cp, fp)) break; } return((u_char *)ptr); } static struct pppTimer RedialTimer; static void RedialTimeout() { StopTimer(&RedialTimer); LogPrintf(LOG_PHASE, "Redialing timer expired.\n"); } static void StartRedialTimer() { LogPrintf(LOG_PHASE, "Enter pause for redialing.\n"); StopTimer(&RedialTimer); RedialTimer.state = TIMER_STOPPED; RedialTimer.load = REDIAL_PERIOD * SECTICKS; RedialTimer.func = RedialTimeout; StartTimer(&RedialTimer); } static void DoLoop() { fd_set rfds, wfds, efds; int pri, i, n, wfd; struct sockaddr_in hisaddr; struct timeval timeout, *tp; int ssize = sizeof(hisaddr); u_char *cp; u_char rbuff[MAX_MRU]; struct itimerval itimer; if (mode & MODE_DIRECT) { modem = OpenModem(mode); fprintf(stderr, "Packet mode enabled\n"); PacketMode(); } else if (mode & MODE_DEDICATED) { if (!modem) modem = OpenModem(mode); } fflush(stdout); timeout.tv_sec = 0;; #ifdef SIGALRM signal(SIGALRM, (void (*)(int))TimerService); itimer.it_interval.tv_sec = itimer.it_value.tv_sec = 0; itimer.it_interval.tv_usec = itimer.it_value.tv_usec = TICKUNIT; setitimer(ITIMER_REAL, &itimer, NULL); #else timeout.tv_usec = 0; #endif for (;;) { IpStartOutput(); FD_ZERO(&rfds); FD_ZERO(&wfds); FD_ZERO(&efds); FD_SET(tun_in, &rfds); if (server > 0) FD_SET(server, &rfds); /* *** IMPORTANT *** * * CPU is serviced every TICKUNIT micro seconds. * This value must be chosen with great care. If this values is * too big, it results loss of characters from modem and poor responce. * If this values is too small, ppp process eats many CPU time. */ #ifndef SIGALRM usleep(TICKUNIT); TimerService(); #endif if (modem) { FD_SET(modem, &rfds); FD_SET(modem, &efds); if (ModemQlen() > 0) { FD_SET(modem, &wfds); } } if (netfd > -1) { FD_SET(netfd, &rfds); FD_SET(netfd, &efds); } #ifndef SIGALRM /* * Normally, slect() will not block because modem is writable. * In AUTO mode, select will block until we find packet from tun. * However, we have to run ourselves while we are in redial wait state. */ tp = (RedialTimer.state == TIMER_RUNNING)? &timeout : NULL; i = select(tun_in+10, &rfds, &wfds, &efds, tp); #else i = select(tun_in+10, &rfds, &wfds, &efds, NULL); #endif if (i == 0) { continue; } if (i < 0) { if (errno == EINTR) continue; perror("select"); break; } if ((netfd > 0 && FD_ISSET(netfd, &efds)) || FD_ISSET(modem, &efds)) { logprintf("Exception detected.\n"); break; } if (server > 0 && FD_ISSET(server, &rfds)) { #ifdef DEBUG logprintf("connected to client.\n"); #endif wfd = accept(server, (struct sockaddr *)&hisaddr, &ssize); if (netfd > 0) { write(wfd, "already in use.\n", 16); close(wfd); continue; } else netfd = wfd; if (dup2(netfd, 1) < 0) perror("dup2"); mode |= MODE_INTER; Greetings(); + switch ( LocalAuthInit() ) { + case NOT_FOUND: + fprintf(stdout,LAUTH_M1); + fprintf(stdout,LAUTH_M2); + fflush(stdout); + /* Fall down */ + case VALID: + VarLocalAuth = LOCAL_AUTH; + break; + default: + break; + } (void) IsInteractive(); Prompt(0); } if ((mode & MODE_INTER) && FD_ISSET(netfd, &rfds)) { /* something to read from tty */ ReadTty(); } if (modem) { if (FD_ISSET(modem, &wfds)) { /* ready to write into modem */ ModemStartOutput(modem); } if (FD_ISSET(modem, &rfds)) { /* something to read from modem */ if (LcpFsm.state <= ST_CLOSED) usleep(10000); n = read(modem, rbuff, sizeof(rbuff)); if ((mode & MODE_DIRECT) && n <= 0) { DownConnection(); } else LogDumpBuff(LOG_ASYNC, "ReadFromModem", rbuff, n); if (LcpFsm.state <= ST_CLOSED) { /* * In dedicated mode, we just discard input until LCP is started. */ if (!(mode & MODE_DEDICATED)) { cp = HdlcDetect(rbuff, n); if (cp) { /* * LCP packet is detected. Turn ourselves into packet mode. */ if (cp != rbuff) { write(1, rbuff, cp - rbuff); write(1, "\r\n", 2); } PacketMode(); #ifdef notdef AsyncInput(cp, n - (cp - rbuff)); #endif } else write(1, rbuff, n); } } else { if (n > 0) AsyncInput(rbuff, n); #ifdef notdef continue; /* THIS LINE RESULT AS POOR PERFORMANCE */ #endif } } } if (FD_ISSET(tun_in, &rfds)) { /* something to read from tun */ /* * If there are many packets queued, wait until they are drained. */ if (ModemQlen() > 5) continue; n = read(tun_in, rbuff, sizeof(rbuff)); if (n < 0) { perror("read from tun"); continue; } /* * Process on-demand dialup. Output packets are queued within tunnel * device until IPCP is opened. */ if (LcpFsm.state <= ST_CLOSED && (mode & MODE_AUTO)) { pri = PacketCheck(rbuff, n, 2); if (pri >= 0) { if (RedialTimer.state == TIMER_RUNNING) { /* * We are in redial wait state. Ignore packet. */ continue; } modem = OpenModem(mode); #ifdef DEBUG logprintf("going to dial: modem = %d\n", modem); #endif if (modem < 0) { printf("failed to open modem.\n"); Cleanup(EX_MODEM); } if (DialModem()) { sleep(1); /* little pause to allow peer starts */ ModemTimeout(); PacketMode(); } else { CloseModem(); /* Dial failed. Keep quite during redial wait period. */ /* XXX: We shoud implement re-dial */ StartRedialTimer(); continue; } IpEnqueue(pri, rbuff, n); } continue; } pri = PacketCheck(rbuff, n, 1); if (pri >= 0) IpEnqueue(pri, rbuff, n); } } #ifdef SIGALRM itimer.it_value.tv_usec = itimer.it_value.tv_sec = 0; setitimer(ITIMER_REAL, &itimer, NULL); signal(SIGALRM, SIG_DFL); #endif logprintf("job done.\n"); } Index: head/usr.sbin/ppp/modem.c =================================================================== --- head/usr.sbin/ppp/modem.c (revision 6763) +++ head/usr.sbin/ppp/modem.c (revision 6764) @@ -1,746 +1,749 @@ /* * PPP Modem handling module * * Written by Toshiharu OHNO (tony-o@iij.ad.jp) * * Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd. * * Redistribution and use in source and binary forms are permitted * provided that the above copyright notice and this paragraph are * duplicated in all such forms and that any documentation, * advertising materials, and other materials related to such * distribution and use acknowledge that the software was developed * by the Internet Initiative Japan, Inc. The name of the * IIJ may not be used to endorse or promote products derived * from this software without specific prior written permission. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. * - * $Id:$ + * $Id: modem.c,v 1.2 1995/02/26 12:17:45 amurai Exp $ * * TODO: */ #include "fsm.h" #include #include #include #include +#include #include "hdlc.h" #include "lcp.h" #include "modem.h" #include "vars.h" #ifndef O_NONBLOCK #ifdef O_NDELAY #define O_NONBLOCK O_NDELAY #endif #endif extern int DoChat(); static int mbits; /* Current DCD status */ static int connect_time; /* connection time */ static int connect_count; static struct pppTimer ModemTimer; -static char *uucplock; +static char uucplock[10]; extern int uu_lock(), uu_unlock(); extern void PacketMode(); #define Online (mbits & TIOCM_CD) static struct mbuf *modemout; static struct mqueue OutputQueues[PRI_URGENT+1]; static int dev_is_modem; void Enqueue(queue, bp) struct mqueue *queue; struct mbuf *bp; { if (queue->last) { queue->last->pnext = bp; queue->last = bp; } else queue->last = queue->top = bp; queue->qlen++; #ifdef QDEBUG logprintf("Enqueue: len = %d\n", queue->qlen); #endif } struct mbuf * Dequeue(queue) struct mqueue *queue; { struct mbuf *bp; #ifdef QDEBUG logprintf("Dequeue: len = %d\n", queue->qlen); #endif if (bp = queue->top) { queue->top = queue->top->pnext; queue->qlen--; if (queue->top == NULL) { queue->last = queue->top; #ifdef QDEBUG if (queue->qlen) logprintf("!!! not zero (%d)!!!\n", queue->qlen); #endif } } return(bp); } static struct speeds{ int nspeed; speed_t speed; } speeds[] = { #ifdef B50 { 50, B50, }, #endif #ifdef B75 { 75, B75, }, #endif #ifdef B110 { 110, B110, }, #endif #ifdef B134 { 134, B134, }, #endif #ifdef B150 { 150, B150, }, #endif #ifdef B200 { 200, B200, }, #endif #ifdef B300 { 300, B300, }, #endif #ifdef B600 { 600, B600, }, #endif #ifdef B1200 { 1200, B1200, }, #endif #ifdef B1800 { 1800, B1800, }, #endif #ifdef B2400 { 2400, B2400, }, #endif #ifdef B4800 { 4800, B4800, }, #endif #ifdef B9600 { 9600, B9600, }, #endif #ifdef B19200 { 19200, B19200, }, #endif #ifdef B38400 { 38400, B38400, }, #endif -#ifndef POSIX_SOURCE +#ifndef _POSIX_SOURCE #ifdef B7200 { 7200, B7200, }, #endif #ifdef B14400 { 14400, B14400, }, #endif #ifdef B28800 { 28800, B28800, }, #endif #ifdef B57600 { 57600, B57600, }, #endif #ifdef B76800 { 76800, B76800, }, #endif #ifdef B115200 { 115200, B115200, }, #endif #ifdef B230400 { 230400, B230400, }, #endif #ifdef EXTA { 19200, EXTA, }, #endif #ifdef EXTB { 38400, EXTB, }, #endif #endif /*_POSIX_SOURCE */ { 0, 0 } }; int SpeedToInt(speed) speed_t speed; { struct speeds *sp; for (sp = speeds; sp->nspeed; sp++) { if (sp->speed == speed) { return(sp->nspeed); } } return 0; } speed_t IntToSpeed(nspeed) int nspeed; { struct speeds *sp; for (sp = speeds; sp->nspeed; sp++) { if (sp->nspeed == nspeed) { return(sp->speed); } } return B0; } static time_t uptime; void DownConnection() { LogPrintf(LOG_PHASE, "Disconnected!\n"); LogPrintf(LOG_PHASE, "Connect time: %d secs\n", time(NULL) - uptime); LcpDown(); connect_time = 0; } /* * ModemTimeout() watches DCD signal and notifies if it's status is changed. * */ void ModemTimeout() { int ombits = mbits; int change; StopTimer(&ModemTimer); if (Online) connect_time++; StartTimer(&ModemTimer); if (dev_is_modem) { ioctl(modem, TIOCMGET, &mbits); change = ombits ^ mbits; if (change & TIOCM_CD) { if (Online) { time(&uptime); LogPrintf(LOG_PHASE, "*Connected!\n"); connect_count++; /* * In dedicated mode, start packet mode immediate * after we detected carrier. */ if (mode & MODE_DEDICATED) PacketMode(); } else { DownConnection(); } } } else { if (!Online) { time(&uptime); LogPrintf(LOG_PHASE, "Connected!\n"); mbits = TIOCM_CD; connect_count++; connect_time = 0; } else if (uptime == 0) { time(&uptime); } } } void StartModemTimer() { connect_time = 0; StopTimer(&ModemTimer); ModemTimer.state = TIMER_STOPPED; ModemTimer.load = SECTICKS; ModemTimer.func = ModemTimeout; StartTimer(&ModemTimer); } struct parity { char *name; char *name1; int set; } validparity[] = { { "even", "P_EVEN", CS7 | PARENB }, { "odd", "P_ODD", CS7 | PARENB | PARODD }, { "none", "P_ZERO", CS8 }, { NULL, 0 }, }; int GetParityValue(str) char *str; { struct parity *pp; for (pp = validparity; pp->name; pp++) { if (strcasecmp(pp->name, str) == 0 || strcasecmp(pp->name1, str) == 0) { VarParity = pp->set; return(pp->set); } } return(-1); } int ChangeParity(str) char *str; { struct termios rstio; int val; val = GetParityValue(str); if (val > 0) { VarParity = val; tcgetattr(modem, &rstio); rstio.c_cflag &= ~(CSIZE|PARODD|PARENB); rstio.c_cflag |= val; tcsetattr(modem, TCSADRAIN, &rstio); } return(val); } #include #include #include int OpenConnection(host, port) char *host, *port; { struct sockaddr_in dest; int sock; struct hostent *hp; struct servent *sp; dest.sin_family = AF_INET; dest.sin_addr.s_addr = inet_addr(host); if (dest.sin_addr.s_addr == INADDR_NONE) { hp = gethostbyname(host); if (hp) { bcopy(hp->h_addr_list[0], &dest.sin_addr.s_addr, 4); } else { printf("unknown host: %s\n", host); return(-1); } } dest.sin_port = htons(atoi(port)); if (dest.sin_port == 0) { sp = getservbyname(port, "tcp"); if (sp) { dest.sin_port = sp->s_port; } else { printf("unknown service: %s\n", port); return(-1); } } LogPrintf(LOG_PHASE, "Connected to %s:%s\n", host, port); sock = socket(PF_INET, SOCK_STREAM, 0); if (sock < 0) { return(sock); } if (connect(sock, (struct sockaddr *)&dest, sizeof(dest)) < 0) { printf("connection failed.\n"); return(-1); } return(sock); } int OpenModem(mode) int mode; { struct termios rstio; int oldflag; char *host, *cp, *port; mbits = 0; if (mode & MODE_DIRECT) { if (isatty(0)) modem = open(ctermid(NULL), O_RDWR|O_NONBLOCK); } else if (modem == 0) { if (strncmp(VarDevice, "/dev", 4) == 0) { - uucplock = rindex(VarDevice, '/')+1; + strcpy(uucplock, rindex(VarDevice, '/')+1); if (uu_lock(uucplock) < 0) { fprintf(stderr, "modem is in use.\n"); return(-1); } modem = open(VarDevice, O_RDWR|O_NONBLOCK); if (modem < 0) { perror("open modem"); return(modem); } } else { /* XXX: PPP over TCP */ cp = index(VarDevice, ':'); if (cp) { *cp = 0; host = VarDevice; port = cp + 1; if (*host && *port) { modem = OpenConnection(host, port); *cp = ':'; /* Don't destroy VarDevice */ if (modem < 0) return(-1); } else { *cp = ':'; /* Don't destroy VarDevice */ return(-1); } } else return(-1); } } while (modem < 3) modem = dup(modem); /* * If we are working on tty device, change it's mode into * the one desired for further operation. In this implementation, * we assume that modem is configuted to use CTS/RTS flow control. */ dev_is_modem = isatty(modem) || DEV_IS_SYNC; if (DEV_IS_SYNC) sleep(1); if (dev_is_modem && !DEV_IS_SYNC) { tcgetattr(modem, &rstio); #ifdef DEBUG logprintf("## modem = %d\n", modem); logprintf("modem (get): iflag = %x, oflag = %x, cflag = %x\n", rstio.c_iflag, rstio.c_oflag, rstio.c_cflag); #endif #define USE_CTSRTS #ifdef USE_CTSRTS rstio.c_cflag = (CS8 | CREAD | CLOCAL | CCTS_OFLOW|CRTS_IFLOW); #else rstio.c_cflag = (CS8 | CREAD | CLOCAL); #endif if ((mode & MODE_DIRECT) == 0) { /* * If we are working as direct mode, don't change tty speed. */ rstio.c_cflag &= ~(CSIZE|PARENB|PARODD); rstio.c_cflag |= VarParity; cfsetspeed(&rstio, IntToSpeed(VarSpeed)); } rstio.c_iflag |= (IGNBRK | ISTRIP | IGNPAR | IXON | IXOFF); rstio.c_iflag &= ~(BRKINT|ICRNL|IXANY|IMAXBEL); rstio.c_lflag = 0; rstio.c_oflag &= ~OPOST; #ifdef notdef rstio.c_cc[VMIN] = 10; rstio.c_cc[VTIME] = 1; #else rstio.c_cc[VMIN] = 1; rstio.c_cc[VTIME] = 0; #endif tcsetattr(modem, TCSADRAIN, &rstio); #ifdef DEBUG logprintf("modem (put): iflag = %x, oflag = %x, cflag = %x\n", rstio.c_iflag, rstio.c_oflag, rstio.c_cflag); #endif if (!(mode & MODE_DIRECT)) ioctl(modem, TIOCMGET, &mbits); #ifdef DEBUG fprintf(stderr, "modem control = %o\n", mbits); #endif oldflag = fcntl(modem, F_GETFL, 0); fcntl(modem, F_SETFL, oldflag & ~O_NONBLOCK); } StartModemTimer(); return(modem); } int ModemSpeed() { struct termios rstio; tcgetattr(modem, &rstio); return(SpeedToInt(cfgetispeed(&rstio))); } static struct termios modemios; /* * Put modem tty line into raw mode which is necessary in packet mode operation */ int RawModem(modem) int modem; { struct termios rstio; int oldflag; if (!isatty(modem) || DEV_IS_SYNC) return(0); if (!(mode & MODE_DIRECT) && modem && !Online) { #ifdef DEBUG logprintf("mode = %d, modem = %d, mbits = %x\n", mode, modem, mbits); #endif #ifdef notdef return(-1); #endif } tcgetattr(modem, &rstio); modemios = rstio; rstio.c_cflag &= ~(CSIZE|PARENB|PARODD); rstio.c_cflag |= CS8; rstio.c_iflag &= ~(ISTRIP|IXON|IXOFF|BRKINT|ICRNL|INLCR); tcsetattr(modem, TCSADRAIN, &rstio); oldflag = fcntl(modem, F_GETFL, 0); fcntl(modem, F_SETFL, oldflag | O_NONBLOCK); #ifdef DEBUG oldflag = fcntl(modem, F_GETFL, 0); logprintf("modem (put2): iflag = %x, oflag = %x, cflag = %x\n", rstio.c_iflag, rstio.c_oflag, rstio.c_cflag); logprintf("flag = %x\n", oldflag); #endif return(0); } void UnrawModem(modem) int modem; { int oldflag; if (isatty(modem)) { tcsetattr(modem, TCSADRAIN, &modemios); oldflag = fcntl(modem, F_GETFL, 0); fcntl(modem, F_SETFL, oldflag & ~O_NONBLOCK); } } void HangupModem(flag) int flag; { struct termios tio; if (!isatty(modem)) { mbits &= ~TIOCM_DTR; close(modem); modem = 0; /* Mark as modem has closed */ return; } if (modem && Online) { mbits &= ~TIOCM_DTR; #ifdef __bsdi__ /* not a POSIX way */ ioctl(modem, TIOCMSET, &mbits); #else tcgetattr(modem, &tio); cfsetspeed(&tio, B0); tcsetattr(modem, TCSANOW, &tio); #endif sleep(1); #ifdef notdef mbits &= ~TIOCM_CD; #endif } /* * If we are working as dedicated mode, never close it * until we are directed to quit program. */ if (modem && (flag || !(mode & MODE_DEDICATED))) { ModemTimeout(); /* XXX */ StopTimer(&ModemTimer); /* XXX */ tcflush(modem, TIOCFLUSH); UnrawModem(modem); close(modem); (void) uu_unlock(uucplock); modem = 0; /* Mark as modem has closed */ } else if (modem) { mbits |= TIOCM_DTR; #ifndef notyet ioctl(modem, TIOCMSET, &mbits); #else tcgetattr(modem, &ts); cfsetspeed(&ts, IntToSpeed(VarSpeed)); tcsetattr(modem, TCSADRAIN, &ts); #endif } } CloseModem() { close(modem); modem = 0; (void) uu_unlock(uucplock); } /* * Write to modem. Actualy, requested packets are queued, and goes out * to the line when ModemStartOutput() is called. */ void WriteModem(pri, ptr, count) int pri; char *ptr; int count; { struct mbuf *bp; bp = mballoc(count, MB_MODEM); bcopy(ptr, MBUF_CTOP(bp), count); Enqueue(&OutputQueues[pri], bp); } void ModemOutput(pri, bp) int pri; struct mbuf *bp; { struct mbuf *wp; int len; len = plength(bp); wp = mballoc(len, MB_MODEM); mbread(bp, MBUF_CTOP(wp), len); Enqueue(&OutputQueues[pri], wp); ModemStartOutput(modem); } int ModemQlen() { struct mbuf *bp; int len = 0; int i; for (i = PRI_NORMAL; i <= PRI_URGENT; i++) { for (bp = OutputQueues[i].top; bp; bp = bp->pnext) len++; } return(len); } void ModemStartOutput(fd) int fd; { struct mqueue *queue; int nb, nw, i; if (modemout == NULL) { i = 0; for (queue = &OutputQueues[PRI_URGENT]; queue >= OutputQueues; queue--) { if (queue->top) { modemout = Dequeue(queue); #ifdef QDEBUG if (i < 2) { struct mqueue *q; q = &OutputQueues[0]; logprintf("output from queue %d, normal has %d\n", i, q->qlen); } logprintf("Dequeue(%d): ", i); #endif break; } i++; } } if (modemout) { nb = modemout->cnt; if (nb > 1600) nb = 1600; if (fd == 0) fd = 1; nw = write(fd, MBUF_CTOP(modemout), nb); #ifdef QDEBUG logprintf("wrote: %d(%d)\n", nw, nb); LogDumpBuff(LOG_HDLC, "modem write", MBUF_CTOP(modemout), nb); #endif if (nw > 0) { modemout->cnt -= nw; modemout->offset += nw; if (modemout->cnt == 0) { modemout = mbfree(modemout); #ifdef QDEBUG logprintf(" mbfree\n"); #endif } - } else if (nw < 0) - perror("modem write"); + } else if (nw < 0) { + if (errno != EAGAIN) + perror("modem write"); + } } } int DialModem() { char ScriptBuffer[200]; strcpy(ScriptBuffer, VarDialScript); if (DoChat(ScriptBuffer) > 0) { fprintf(stderr, "dial OK!\n"); strcpy(ScriptBuffer, VarLoginScript); if (DoChat(ScriptBuffer) > 0) { fprintf(stderr, "login OK!\n"); return(1); } else { fprintf(stderr, "login failed.\n"); } ModemTimeout(); /* Dummy call to check modem status */ } else fprintf(stderr, "dial failed.\n"); return(0); } int ShowModemStatus() { #ifdef TIOCOUTQ int nb; #endif printf("device: %s speed: ", VarDevice); if (DEV_IS_SYNC) printf("sync\n"); else printf("%d\n", VarSpeed); switch (VarParity & CSIZE) { case CS7: printf("cs7, "); break; case CS8: printf("cs8, "); break; } if (VarParity & PARENB) { if (VarParity & PARODD) printf("odd parity\n"); else printf("even parity\n"); } else printf("none parity\n"); #ifdef DEBUG printf("fd = %d, modem control = %o\n", modem, mbits); #endif printf("connect count: %d\n", connect_count); #ifdef TIOCOUTQ ioctl(modem, TIOCOUTQ, &nb); printf("outq: %d\n", nb); #endif printf("DialScript = %s\n", VarDialScript); printf("LoginScript = %s\n", VarLoginScript); printf("PhoneNumber = %s\n", VarPhone); return(1); }