This patch adds the option -t to ftpfs, which causes it to use AUTH TLS over the control and data connection. Notes: Sun Apr 3 17:22:43 EDT 2005 rsc thanks for figuring this out. a couple minor comments. - the declaration of usetls said "int usetls;" but all the declarations around it said "int\tfoo;" so i changed your space to a tab. be aware of such things. it's harder in acme because in the default font there's not much difference between "int " and "int\t" at the beginning of a line. - usetls needn't be initialized to zero since it's global. - not sure why you inserted a space in if(cpassword == 0) but i took it back out. - the work in io() to print only on error is a good thing. it silences an annoying print when you unmount /n/ftp cleanly. so i left it in. - i changed the fatal messages to say "starting tls: %r" so that they can echo any error string set by tlsClient. thanks again. russ Reference: /n/sources/patch/applied/ftpfs-auth-tls Date: Sun Apr 3 22:55:15 CES 2005 Reviewed-by: rsc --- /sys/src/cmd/ip/ftpfs/proto.c Sun Apr 3 22:54:08 2005 +++ /sys/src/cmd/ip/ftpfs/proto.c Sun Apr 3 22:54:00 2005 @@ -2,6 +2,8 @@ #include #include #include +#include +#include #include #include #include @@ -36,6 +38,7 @@ char *user; int nopassive; long lastsend; +extern int usetls; static void sendrequest(char*, char*); static int getreply(Biobuf*, char*, int, int); @@ -60,6 +63,8 @@ { char *p; char dir[Maxpath]; + int ts; + TLSconn conn; Binit(&stdin, 0, OREAD); /* init for later use */ @@ -68,6 +73,7 @@ fprint(2, "can't dial %s: %r\n", dest); exits("dialing"); } + Binit(&ctlin, ctlfd, OREAD); /* remember network for the data connections */ @@ -82,6 +88,29 @@ fatal("bad hello"); if(strstr(msg, "Plan 9")) os = Plan9; + + if(usetls){ + sendrequest("AUTH", "TLS"); + if(getreply(&ctlin, msg, sizeof(msg), 1) != Success) + fatal("bad auth tls"); + + ts = tlsClient(ctlfd, &conn); + if(ts < 0) + fatal("tlsClient failed"); + ctlfd = ts; + + if(conn.cert != nil) + free(conn.cert); + + Binit(&ctlin, ctlfd, OREAD); + + sendrequest("PBSZ", "0"); + if(getreply(&ctlin, msg, sizeof(msg), 1) != Success) + fatal("bad pbsz 0"); + sendrequest("PROT", "P"); + if(getreply(&ctlin, msg, sizeof(msg), 1) != Success) + fatal("bad prot p"); + } } /* @@ -1193,9 +1222,10 @@ static int active(int mode, Biobuf **bpp, char *cmda, char *cmdb) { - int cfd, dfd, rv; + int cfd, dfd, rv, ts; char newdir[Maxpath]; char datafile[Maxpath + 6]; + TLSconn conn; if(port() < 0) return TempFail; @@ -1220,6 +1250,18 @@ close(cfd); if(dfd < 0) fatal("opening data connection"); + + if(usetls){ + memset(&conn, 0, sizeof(conn)); + ts = tlsClient(dfd, &conn); + if(ts < 0) + fatal("tlsClient failed"); + dfd = ts; + + if(conn.cert != nil) + free(conn.cert); + } + Binit(&dbuf, dfd, mode); *bpp = &dbuf; return Extra; @@ -1235,7 +1277,8 @@ char ds[1024]; char *f[6]; char *p; - int x, fd; + int x, fd, ts; + TLSconn conn; if(nopassive) return Impossible; @@ -1285,7 +1328,18 @@ return x; } + if(usetls){ + memset(&conn, 0, sizeof(conn)); + ts = tlsClient(fd, &conn); + if(ts < 0) + fatal("tlsClient failed"); + fd = ts; + + if(conn.cert != nil) + free(conn.cert); + } Binit(&dbuf, fd, mode); + *bpp = &dbuf; return Extra; } --- /sys/src/cmd/ip/ftpfs/ftpfs.c Sun Apr 3 22:54:30 2005 +++ /sys/src/cmd/ip/ftpfs/ftpfs.c Sun Apr 3 22:54:24 2005 @@ -26,6 +26,7 @@ Fcall thdr; int debug; int usenlst; +int usetls; char *ext; int quiet; int kapid = -1; @@ -87,7 +88,7 @@ void usage(void) { - fprint(2, "ftpfs [-/dqn] [-a passwd] [-m mountpoint] [-e ext] [-o os] [-r root] [net!]address\n"); + fprint(2, "ftpfs [-/dqnt] [-a passwd] [-m mountpoint] [-e ext] [-o os] [-r root] [net!]address\n"); exits("usage"); } @@ -103,6 +104,7 @@ defos = Unix; user = strdup(getuser()); + usetls = 0; ARGBEGIN { case '/': @@ -129,6 +131,9 @@ case 'e': ext = ARGF(); break; + case 't': + usetls = 1; + break; case 'o': cp = ARGF(); for(o = oslist; o->os != Unknown; o++) @@ -154,7 +159,7 @@ /* initial handshakes with remote side */ hello(*argv); - if(cpassword == 0) + if (cpassword == 0) rlogin(*argv, keyspec); else clogin("anonymous", cpassword); @@ -247,19 +252,15 @@ void io(void) { - char *err, buf[ERRMAX]; + char *err; int n; kapid = kaproc(); while(!dying){ n = read9pmsg(mfd, mdata, messagesize); - if(n <= 0){ - errstr(buf, sizeof buf); - if(buf[0]=='\0' || strstr(buf, "hungup")) - exits(""); - fatal("mount read: %s\n", buf); - } + if(n <= 0) + fatal("mount read"); if(convM2S(mdata, n, &thdr) == 0) continue; --- /sys/man/4/ftpfs Sun Apr 3 22:54:54 2005 +++ /sys/man/4/ftpfs Sun Apr 3 22:54:50 2005 @@ -4,7 +4,7 @@ .SH SYNOPSIS .B ftpfs [ -.B -/dqn +.B -/dqnt ] [ .B -m @@ -93,6 +93,10 @@ option causes ftp to send a NOP command every 15 seconds to attempt to keep the connection open. This command can cause some servers to hangup, so you'll have to feel your way. +.PP +If you want to use TLS AUTH on the connection to the server, use the +option +.BR -t. .PP To terminate the connection, .B unmount