From mboxrd@z Thu Jan 1 00:00:00 1970 Message-ID: <20030720235129.11224.qmail@g.bio.cse.psu.edu> To: 9fans@cse.psu.edu MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <11219.1058744919.0@bio.cse.psu.edu> From: Scott Schwartz Subject: [9fans] ppp patches Date: Sun, 20 Jul 2003 19:51:29 -0400 Topicbox-Message-UUID: fdae58c2-eacb-11e9-9e20-41e7f4b1d025 ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <11219.1058744919.1@bio.cse.psu.edu> Here's a suggested change to ppp, to provide better support for chat scripts. The main idea is to allow tokens like USER, PASSWD, SERVER and to let factotum hand over the real values, so you can dial completely non-interactively. The server flag does double duty: it lets you store multiple ppp credentials inside factotum and also uses the name of the server as the phone number to dial. Absent the -s flag, it tries to deduce from the name of the chat file or from the telco string. Examples, ppp -b 115200 -f -P -u -M telco.chat -p telco!5552222 ppp -b 115200 -f -P -u -M modem.chat -p /dev/eia1 -s 5552222 ppp -b 115200 -f -P -u -M 5552222.chat -p /dev/eia1 This is lightly tested, so let me know if anything goes wrong. ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="iso-8859-1" Content-ID: <11219.1058744919.2@bio.cse.psu.edu> Content-Transfer-Encoding: quoted-printable #include #include #include #include "chat.h" // XXX - make these parameters? extern int debug; extern char servername[128]; #define RLEN 1024 #define TIMEOUT (60*1000) int chat(int ifd, int ofd, char *chatfile) { int chatfd, lineno, nb; char *script, *p, *s, response[RLEN]; Dir *dir; UserPasswd *up; if ((chatfd =3D open(chatfile, OREAD)) < 0) sysfatal("cannot open %s: %r", chatfile); if ((dir =3D dirfstat(chatfd)) =3D=3D nil) sysfatal("cannot fstat %s: %r",chatfile); script =3D (char *)malloc(dir->length + 1); assert(script); if ((nb =3D read(chatfd, script, dir->length)) < 0) sysfatal("cannot read chatfile %s: %r", chatfile); assert(nb =3D=3D dir->length); script[dir->length] =3D '\0'; free(dir); close(chatfd); p =3D script; lineno =3D 0; while (1) { char *_args[3]; if ((s =3D strchr(p, '\n')) =3D=3D nil) break; *s++ =3D '\0'; = lineno++; if (*p =3D=3D '#') { p =3D s; = continue; } if (tokenize(p, _args, 3) !=3D 2) sysfatal("invalid line %d (line expected: 'send' 'expect')", = lineno); if (strcmp(_args[0], "USER") =3D=3D 0) { up =3D getuserpass(); _args[0] =3D up->user; } else if (strcmp(_args[0], "PASSWD") =3D=3D 0) { up =3D getuserpass(); _args[0] =3D up->passwd; } else if (strcmp(_args[0], "SERVER") =3D=3D 0) { _args[0] =3D servername; } else if (strcmp(_args[0], "CRNL") =3D=3D 0) { _args[0] =3D "\r\n"; } else if (strcmp(_args[0], "CR") =3D=3D 0) { _args[0] =3D "\r"; } else if (strcmp(_args[0], "NL") =3D=3D 0) { _args[0] =3D "\n"; } else if (strcmp(_args[0], "PAUSE") =3D=3D 0) { sleep(5000); _args[0] =3D ""; } if (debug) fprint(2, "sending =E2=80=9C%s=E2=80=9D, expecting =E2=80=9C%s=E2=80=9D= \n", _args[0], _args[1]); if ((nb =3D write(ofd, _args[0], strlen(_args[0]))) < 0) sysfatal("cannot write %ss: %r", _args[0]); assert(nb =3D=3D strlen(_args[0])); if (strlen(_args[1]) > 0) { char *rp =3D response; char *ep =3D response+sizeof(response)-1; int found =3D 0; alarm(TIMEOUT); while ((nb =3D read(ifd, rp, 1)) >=3D 0 && rp < ep) { if (debug) fprint(2, "response %02x %c\n", *rp, *rp); rp +=3D nb; *rp =3D 0; found =3D cistrstr(response, _args[1]) !=3D nil; if (found) { if (debug) fprint(2, "found =E2=80=9C%s=E2=80=9D\n", response); break; } } alarm(0); if (nb < 0) sysfatal("cannot read response from: %r"); if (nb =3D=3D 0) sysfatal("eof on input?\n"); if (!found) sysfatal("expected %s, got %s\n", _args[1], response); } p =3D s; } free(script); return 1; } ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <11219.1058744919.3@bio.cse.psu.edu> extern UserPasswd *getuserpass(void); extern int chat(int,int,char*); ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <11219.1058744919.4@bio.cse.psu.edu> # send expect ATQ0V1E0M1S0=0 '' atdt '' SERVER '' CR 'CONNECT' # PAUSE '' CRNL ':' # USER '' CRNL : # PASSWD '' CRNL : # ppp '' CRNL ~ ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <11219.1058744919.5@bio.cse.psu.edu> term% diff /sys/src/cmd/ip/ppp . Only in .: 8632222.chat Only in .: chat.c Only in .: chat.h diff /sys/src/cmd/ip/ppp/mkfile ./mkfile 11a12 > chat.$O\ Only in .: modem.chat diff /sys/src/cmd/ip/ppp/ppp.c ./ppp.c 8a9 > #include "chat.h" 24a26 > char servername[PATH]; 2512,2572c2514,2519 < int chatfd, lineno, nb; < char *buf, *p, *s, response[128]; < Dir *dir; < < if ((chatfd = open(chatfile, OREAD)) < 0) < sysfatal("cannot open %s: %r", chatfile); < < if ((dir = dirfstat(chatfd)) == nil) < sysfatal("cannot fstat %s: %r",chatfile); < < buf = (char *)malloc(dir->length + 1); < assert(buf); < < if ((nb = read(chatfd, buf, dir->length)) < 0) < sysfatal("cannot read chatfile %s: %r", chatfile); < assert(nb == dir->length); < buf[dir->length] = '\0'; < free(dir); < close(chatfd); < < p = buf; < lineno = 0; < while (1) { < char *_args[3]; < < if ((s = strchr(p, '\n')) == nil) < break; < *s++ = '\0'; < < lineno++; < < if (*p == '#') { < p = s; < continue; < } < < if (tokenize(p, _args, 3) != 2) < sysfatal("invalid line %d (line expected: 'send' 'expect')", < lineno); < < if (debug) < print("sending %s, expecting %s\n", _args[0], _args[1]); < < if ((nb = write(fd, _args[0], strlen(_args[0]))) < 0) < sysfatal("cannot write %ss: %r", _args[0]); < assert(nb == strlen(_args[0])); < < if (strlen(_args[1]) > 0) { < if ((nb = read(fd, response, sizeof response)) < 0) < sysfatal("cannot read response from: %r"); < < if (debug) < print("response %s\n", response); < < if (nb == 0) < sysfatal("eof on input?\n"); < < if (cistrstr(response, _args[1]) == nil) < sysfatal("expected %s, got %s\n", _args[1], response); < } < p = s; --- > // Try to deduce the server name from the chatfile name. > if (!*servername) { > char *p; > strecpy(servername, servername+PATH-1, chatfile); > if ((p=strstr(servername, ".chat"))) > *p = 0; 2574c2521 < free(buf); --- > chat(fd, fd, chatfile); 2576c2523 < } --- > }; 2632c2579 < fprint(2, "usage: ppp [-cCdfPSu] [-b baud] [-k keyspec] [-m mtu] [-p dev] [-s username] [-x netmntpt] [-t modemcmd] [local-addr [remote-addr]]\n"); --- > fprint(2, "usage: ppp [-cCdfPSu] [-b baud] [-k keyspec] [-m mtu] [-p dev] [-s servername] [-x netmntpt] [-t modemcmd] [local-addr [remote-addr]]\n"); 2706a2654,2656 > case 's': > snprint(servername, PATH, "%s", EARGF(usage())); > break; 2743a2694,2701 > // If we don't have a user specified server name, > // try to deduce one from the dial string. > if(!*servername) { > char *p = strchr(dev,'!'); > snprint(servername, PATH, "%s", p+1); > p = strchr(servername, '!'); > if (p) *p = 0; > } 2751a2710 > 2885a2845,2855 > UserPasswd * > getuserpass() > { > if (servername[0]==0) > return auth_getuserpasswd(auth_getkey, > "proto=pass service=ppp %s", keyspec); > else > return auth_getuserpasswd(auth_getkey, > "proto=pass service=ppp server=%s %s", servername, keyspec); > } > 2893,2895c2863,2864 < < up = auth_getuserpasswd(auth_getkey,"proto=pass service=ppp %s", keyspec); < if(up != nil){ --- > > if((up = getuserpass()) != nil){ 2899a2869 > Only in .: telco.chat Only in .: try ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <11219.1058744919.6@bio.cse.psu.edu> # send expect PAUSE '' CRNL ':' # USER '' CRNL : # PASSWD '' CRNL : # ppp '' CRNL ~ ------- =_aaaaaaaaaa0--