1*21739Sdist /* 2*21739Sdist * Copyright (c) 1980 Regents of the University of California. 3*21739Sdist * All rights reserved. The Berkeley software License Agreement 4*21739Sdist * specifies the terms and conditions for redistribution. 5*21739Sdist */ 6*21739Sdist 710296Ssam #ifndef lint 8*21739Sdist static char sccsid[] = "@(#)ftp.c 5.1 (Berkeley) 05/31/85"; 9*21739Sdist #endif not lint 1010296Ssam 1110296Ssam #include <sys/param.h> 1210296Ssam #include <sys/stat.h> 1310296Ssam #include <sys/ioctl.h> 1410296Ssam #include <sys/socket.h> 1513614Ssam #include <sys/time.h> 1610296Ssam 1710296Ssam #include <netinet/in.h> 1812397Ssam #include <arpa/ftp.h> 1910296Ssam 2010296Ssam #include <stdio.h> 2110296Ssam #include <signal.h> 2210296Ssam #include <errno.h> 2310296Ssam #include <netdb.h> 2410296Ssam 2510296Ssam #include "ftp_var.h" 2610296Ssam 2710296Ssam struct sockaddr_in hisctladdr; 2810296Ssam struct sockaddr_in data_addr; 2910296Ssam int data = -1; 3010296Ssam int connected; 3110296Ssam struct sockaddr_in myctladdr; 3210296Ssam 3310296Ssam FILE *cin, *cout; 3410296Ssam FILE *dataconn(); 3510296Ssam 3610296Ssam struct hostent * 3710296Ssam hookup(host, port) 3810296Ssam char *host; 3910296Ssam int port; 4010296Ssam { 4110296Ssam register struct hostent *hp; 4211627Ssam int s, len; 4310296Ssam 4410296Ssam bzero((char *)&hisctladdr, sizeof (hisctladdr)); 4510296Ssam hp = gethostbyname(host); 4611283Ssam if (hp == NULL) { 4710296Ssam static struct hostent def; 4810296Ssam static struct in_addr defaddr; 4910296Ssam static char namebuf[128]; 5010296Ssam int inet_addr(); 5110296Ssam 5210296Ssam defaddr.s_addr = inet_addr(host); 5310296Ssam if (defaddr.s_addr == -1) { 5410296Ssam fprintf(stderr, "%s: Unknown host.\n", host); 5510296Ssam return (0); 5610296Ssam } 5710296Ssam strcpy(namebuf, host); 5810296Ssam def.h_name = namebuf; 5910296Ssam hostname = namebuf; 6010296Ssam def.h_addr = (char *)&defaddr; 6110296Ssam def.h_length = sizeof (struct in_addr); 6210296Ssam def.h_addrtype = AF_INET; 6310296Ssam def.h_aliases = 0; 6410296Ssam hp = &def; 6510296Ssam } 6611283Ssam hostname = hp->h_name; 6711283Ssam hisctladdr.sin_family = hp->h_addrtype; 6818287Sralph s = socket(hp->h_addrtype, SOCK_STREAM, 0); 6910296Ssam if (s < 0) { 7010296Ssam perror("ftp: socket"); 7110296Ssam return (0); 7210296Ssam } 7310296Ssam if (bind(s, (char *)&hisctladdr, sizeof (hisctladdr), 0) < 0) { 7410296Ssam perror("ftp: bind"); 7510296Ssam goto bad; 7610296Ssam } 7710296Ssam bcopy(hp->h_addr, (char *)&hisctladdr.sin_addr, hp->h_length); 7810296Ssam hisctladdr.sin_port = port; 7910296Ssam if (connect(s, (char *)&hisctladdr, sizeof (hisctladdr), 0) < 0) { 8010296Ssam perror("ftp: connect"); 8110296Ssam goto bad; 8210296Ssam } 8311627Ssam len = sizeof (myctladdr); 8411627Ssam if (getsockname(s, (char *)&myctladdr, &len) < 0) { 8511627Ssam perror("ftp: getsockname"); 8610296Ssam goto bad; 8710296Ssam } 8810296Ssam cin = fdopen(s, "r"); 8910296Ssam cout = fdopen(s, "w"); 9011219Ssam if (cin == NULL || cout == NULL) { 9110296Ssam fprintf(stderr, "ftp: fdopen failed.\n"); 9210296Ssam if (cin) 9310296Ssam fclose(cin); 9410296Ssam if (cout) 9510296Ssam fclose(cout); 9610296Ssam goto bad; 9710296Ssam } 9810296Ssam if (verbose) 9910296Ssam printf("Connected to %s.\n", hp->h_name); 10010296Ssam (void) getreply(0); /* read startup message from server */ 10110296Ssam return (hp); 10210296Ssam bad: 10310296Ssam close(s); 10410296Ssam return ((struct hostent *)0); 10510296Ssam } 10610296Ssam 10710296Ssam login(hp) 10810296Ssam struct hostent *hp; 10910296Ssam { 11010296Ssam char acct[80]; 11110296Ssam char *user, *pass; 11210296Ssam int n; 11310296Ssam 11410296Ssam user = pass = 0; 11510296Ssam ruserpass(hp->h_name, &user, &pass); 11610296Ssam n = command("USER %s", user); 11710296Ssam if (n == CONTINUE) 11810296Ssam n = command("PASS %s", pass); 11910296Ssam if (n == CONTINUE) { 12010296Ssam printf("Account: "); (void) fflush(stdout); 12110296Ssam (void) fgets(acct, sizeof(acct) - 1, stdin); 12210296Ssam acct[strlen(acct) - 1] = '\0'; 12310296Ssam n = command("ACCT %s", acct); 12410296Ssam } 12510296Ssam if (n != COMPLETE) { 12610296Ssam fprintf(stderr, "Login failed.\n"); 12710296Ssam return (0); 12810296Ssam } 12910296Ssam return (1); 13010296Ssam } 13110296Ssam 13210296Ssam /*VARARGS 1*/ 13310296Ssam command(fmt, args) 13410296Ssam char *fmt; 13510296Ssam { 13610296Ssam 13710296Ssam if (debug) { 13810296Ssam printf("---> "); 13910296Ssam _doprnt(fmt, &args, stdout); 14010296Ssam printf("\n"); 14110296Ssam (void) fflush(stdout); 14210296Ssam } 14311219Ssam if (cout == NULL) { 14411219Ssam perror ("No control connection for command"); 14511219Ssam return (0); 14611219Ssam } 14710296Ssam _doprnt(fmt, &args, cout); 14810296Ssam fprintf(cout, "\r\n"); 14910296Ssam (void) fflush(cout); 15010296Ssam return (getreply(!strcmp(fmt, "QUIT"))); 15110296Ssam } 15210296Ssam 15310296Ssam #include <ctype.h> 15410296Ssam 15510296Ssam getreply(expecteof) 15610296Ssam int expecteof; 15710296Ssam { 15811219Ssam register int c, n; 15910296Ssam register int code, dig; 16010296Ssam int originalcode = 0, continuation = 0; 16110296Ssam 16210296Ssam for (;;) { 16310296Ssam dig = n = code = 0; 16410296Ssam while ((c = getc(cin)) != '\n') { 16510296Ssam dig++; 16610296Ssam if (c == EOF) { 16710296Ssam if (expecteof) 16810296Ssam return (0); 16910296Ssam lostpeer(); 17010296Ssam exit(1); 17110296Ssam } 17210296Ssam if (verbose && c != '\r' || 17310296Ssam (n == '5' && dig > 4)) 17410296Ssam putchar(c); 17510296Ssam if (dig < 4 && isdigit(c)) 17610296Ssam code = code * 10 + (c - '0'); 17710296Ssam if (dig == 4 && c == '-') 17810296Ssam continuation++; 17910296Ssam if (n == 0) 18010296Ssam n = c; 18110296Ssam } 18211346Ssam if (verbose || n == '5') { 18310296Ssam putchar(c); 18411346Ssam (void) fflush (stdout); 18511346Ssam } 18610296Ssam if (continuation && code != originalcode) { 18710296Ssam if (originalcode == 0) 18810296Ssam originalcode = code; 18910296Ssam continue; 19010296Ssam } 19111219Ssam if (expecteof || empty(cin)) 19210296Ssam return (n - '0'); 19310296Ssam } 19410296Ssam } 19510296Ssam 19610296Ssam empty(f) 19710296Ssam FILE *f; 19810296Ssam { 19911651Ssam long mask; 20010296Ssam struct timeval t; 20110296Ssam 20210296Ssam if (f->_cnt > 0) 20310296Ssam return (0); 20410296Ssam mask = (1 << fileno(f)); 20510296Ssam t.tv_sec = t.tv_usec = 0; 20610296Ssam (void) select(20, &mask, 0, 0, &t); 20710296Ssam return (mask == 0); 20810296Ssam } 20910296Ssam 21010296Ssam jmp_buf sendabort; 21110296Ssam 21210296Ssam abortsend() 21310296Ssam { 21410296Ssam 21510296Ssam longjmp(sendabort, 1); 21610296Ssam } 21710296Ssam 21810296Ssam sendrequest(cmd, local, remote) 21910296Ssam char *cmd, *local, *remote; 22010296Ssam { 22110296Ssam FILE *fin, *dout, *popen(); 22210296Ssam int (*closefunc)(), pclose(), fclose(), (*oldintr)(); 22311219Ssam char buf[BUFSIZ]; 22411651Ssam long bytes = 0, hashbytes = sizeof (buf); 22511346Ssam register int c, d; 22610296Ssam struct stat st; 22710296Ssam struct timeval start, stop; 22810296Ssam 22910296Ssam closefunc = NULL; 23010296Ssam if (setjmp(sendabort)) 23110296Ssam goto bad; 23210296Ssam oldintr = signal(SIGINT, abortsend); 23310296Ssam if (strcmp(local, "-") == 0) 23410296Ssam fin = stdin; 23510296Ssam else if (*local == '|') { 23610296Ssam fin = popen(local + 1, "r"); 23710296Ssam if (fin == NULL) { 23810296Ssam perror(local + 1); 23910296Ssam goto bad; 24010296Ssam } 24110296Ssam closefunc = pclose; 24210296Ssam } else { 24310296Ssam fin = fopen(local, "r"); 24410296Ssam if (fin == NULL) { 24510296Ssam perror(local); 24610296Ssam goto bad; 24710296Ssam } 24810296Ssam closefunc = fclose; 24910296Ssam if (fstat(fileno(fin), &st) < 0 || 25010296Ssam (st.st_mode&S_IFMT) != S_IFREG) { 25117949Sralph fprintf(stderr, "%s: not a plain file.\n", local); 25210296Ssam goto bad; 25310296Ssam } 25410296Ssam } 25510296Ssam if (initconn()) 25610296Ssam goto bad; 25710296Ssam if (remote) { 25810296Ssam if (command("%s %s", cmd, remote) != PRELIM) 25910296Ssam goto bad; 26010296Ssam } else 26110296Ssam if (command("%s", cmd) != PRELIM) 26210296Ssam goto bad; 26310296Ssam dout = dataconn("w"); 26410296Ssam if (dout == NULL) 26510296Ssam goto bad; 26610296Ssam gettimeofday(&start, (struct timezone *)0); 26711219Ssam switch (type) { 26811219Ssam 26911219Ssam case TYPE_I: 27011219Ssam case TYPE_L: 27111346Ssam errno = d = 0; 27211219Ssam while ((c = read(fileno (fin), buf, sizeof (buf))) > 0) { 27311346Ssam if ((d = write(fileno (dout), buf, c)) < 0) 27411219Ssam break; 27511219Ssam bytes += c; 27611651Ssam if (hash) { 27711651Ssam putchar('#'); 27811651Ssam fflush(stdout); 27911651Ssam } 28011219Ssam } 28113213Ssam if (hash && bytes > 0) { 28211651Ssam putchar('\n'); 28311651Ssam fflush(stdout); 28411651Ssam } 28511219Ssam if (c < 0) 28611219Ssam perror(local); 28711346Ssam if (d < 0) 28811219Ssam perror("netout"); 28911219Ssam break; 29011219Ssam 29111219Ssam case TYPE_A: 29211219Ssam while ((c = getc(fin)) != EOF) { 29311219Ssam if (c == '\n') { 29411651Ssam while (hash && (bytes >= hashbytes)) { 29511651Ssam putchar('#'); 29611651Ssam fflush(stdout); 29711651Ssam hashbytes += sizeof (buf); 29811651Ssam } 29911219Ssam if (ferror(dout)) 30011219Ssam break; 30111219Ssam putc('\r', dout); 30211219Ssam bytes++; 30311219Ssam } 30411219Ssam putc(c, dout); 30511219Ssam bytes++; 30611219Ssam if (c == '\r') { 30711219Ssam putc('\0', dout); 30811219Ssam bytes++; 30911219Ssam } 31011219Ssam } 31111651Ssam if (hash) { 31213213Ssam if (bytes < hashbytes) 31313213Ssam putchar('#'); 31411651Ssam putchar('\n'); 31511651Ssam fflush(stdout); 31611651Ssam } 31711219Ssam if (ferror(fin)) 31811219Ssam perror(local); 31911346Ssam if (ferror(dout)) 32011219Ssam perror("netout"); 32111219Ssam break; 32210296Ssam } 32310296Ssam gettimeofday(&stop, (struct timezone *)0); 32410296Ssam if (closefunc != NULL) 32510296Ssam (*closefunc)(fin); 32610296Ssam (void) fclose(dout); 32710296Ssam (void) getreply(0); 32810296Ssam done: 32910296Ssam signal(SIGINT, oldintr); 33010296Ssam if (bytes > 0 && verbose) 33110296Ssam ptransfer("sent", bytes, &start, &stop); 33210296Ssam return; 33310296Ssam bad: 33410296Ssam if (data >= 0) 33510296Ssam (void) close(data), data = -1; 33610296Ssam if (closefunc != NULL && fin != NULL) 33710296Ssam (*closefunc)(fin); 33810296Ssam goto done; 33910296Ssam } 34010296Ssam 34110296Ssam jmp_buf recvabort; 34210296Ssam 34310296Ssam abortrecv() 34410296Ssam { 34510296Ssam 34610296Ssam longjmp(recvabort, 1); 34710296Ssam } 34810296Ssam 34911651Ssam recvrequest(cmd, local, remote, mode) 35011651Ssam char *cmd, *local, *remote, *mode; 35110296Ssam { 35210296Ssam FILE *fout, *din, *popen(); 35311651Ssam int (*closefunc)(), pclose(), fclose(), (*oldintr)(); 35411219Ssam char buf[BUFSIZ]; 35511651Ssam long bytes = 0, hashbytes = sizeof (buf); 35611346Ssam register int c, d; 35710296Ssam struct timeval start, stop; 35810296Ssam 35910296Ssam closefunc = NULL; 36010296Ssam if (setjmp(recvabort)) 36110296Ssam goto bad; 36210296Ssam oldintr = signal(SIGINT, abortrecv); 36310296Ssam if (strcmp(local, "-") && *local != '|') 36410296Ssam if (access(local, 2) < 0) { 36510296Ssam char *dir = rindex(local, '/'); 36610296Ssam 36710296Ssam if (dir != NULL) 36810296Ssam *dir = 0; 36917305Sralph d = access(dir ? local : ".", 2); 37017305Sralph if (dir != NULL) 37117305Sralph *dir = '/'; 37217305Sralph if (d < 0) { 37310296Ssam perror(local); 37410296Ssam goto bad; 37510296Ssam } 37610296Ssam } 37710296Ssam if (initconn()) 37810296Ssam goto bad; 37910296Ssam if (remote) { 38010296Ssam if (command("%s %s", cmd, remote) != PRELIM) 38110296Ssam goto bad; 38210296Ssam } else 38310296Ssam if (command("%s", cmd) != PRELIM) 38410296Ssam goto bad; 38510296Ssam if (strcmp(local, "-") == 0) 38610296Ssam fout = stdout; 38710296Ssam else if (*local == '|') { 38810296Ssam fout = popen(local + 1, "w"); 38910296Ssam closefunc = pclose; 39010296Ssam } else { 39111651Ssam fout = fopen(local, mode); 39210296Ssam closefunc = fclose; 39310296Ssam } 39410296Ssam if (fout == NULL) { 39510296Ssam perror(local + 1); 39610296Ssam goto bad; 39710296Ssam } 39810296Ssam din = dataconn("r"); 39910296Ssam if (din == NULL) 40010296Ssam goto bad; 40110296Ssam gettimeofday(&start, (struct timezone *)0); 40211219Ssam switch (type) { 40311219Ssam 40411219Ssam case TYPE_I: 40511219Ssam case TYPE_L: 40611346Ssam errno = d = 0; 40711219Ssam while ((c = read(fileno(din), buf, sizeof (buf))) > 0) { 40811346Ssam if ((d = write(fileno(fout), buf, c)) < 0) 40911219Ssam break; 41011219Ssam bytes += c; 41111651Ssam if (hash) { 41211651Ssam putchar('#'); 41311651Ssam fflush(stdout); 41411651Ssam } 41511219Ssam } 41613213Ssam if (hash && bytes > 0) { 41711651Ssam putchar('\n'); 41811651Ssam fflush(stdout); 41911651Ssam } 42011219Ssam if (c < 0) 42111219Ssam perror("netin"); 42211346Ssam if (d < 0) 42310296Ssam perror(local); 42411219Ssam break; 42511219Ssam 42611219Ssam case TYPE_A: 42711219Ssam while ((c = getc(din)) != EOF) { 42811219Ssam if (c == '\r') { 42911651Ssam while (hash && (bytes >= hashbytes)) { 43011651Ssam putchar('#'); 43111651Ssam fflush(stdout); 43211651Ssam hashbytes += sizeof (buf); 43311651Ssam } 43410296Ssam bytes++; 43511219Ssam if ((c = getc(din)) != '\n') { 43611219Ssam if (ferror (fout)) 43711219Ssam break; 43811219Ssam putc ('\r', fout); 43911219Ssam } 44011219Ssam if (c == '\0') { 44111219Ssam bytes++; 44211219Ssam continue; 44311219Ssam } 44411219Ssam } 44511219Ssam putc (c, fout); 44611219Ssam bytes++; 44710296Ssam } 44811651Ssam if (hash) { 44913213Ssam if (bytes < hashbytes) 45013213Ssam putchar('#'); 45111651Ssam putchar('\n'); 45211651Ssam fflush(stdout); 45311651Ssam } 45411219Ssam if (ferror (din)) 45511219Ssam perror ("netin"); 45611219Ssam if (ferror (fout)) 45711219Ssam perror (local); 45811219Ssam break; 45910296Ssam } 46010296Ssam gettimeofday(&stop, (struct timezone *)0); 46110296Ssam (void) fclose(din); 46210296Ssam if (closefunc != NULL) 46310296Ssam (*closefunc)(fout); 46410296Ssam (void) getreply(0); 46510296Ssam done: 46610296Ssam signal(SIGINT, oldintr); 46710296Ssam if (bytes > 0 && verbose) 46810296Ssam ptransfer("received", bytes, &start, &stop); 46910296Ssam return; 47010296Ssam bad: 47110296Ssam if (data >= 0) 47210296Ssam (void) close(data), data = -1; 47310296Ssam if (closefunc != NULL && fout != NULL) 47410296Ssam (*closefunc)(fout); 47510296Ssam goto done; 47610296Ssam } 47710296Ssam 47810296Ssam /* 47910296Ssam * Need to start a listen on the data channel 48010296Ssam * before we send the command, otherwise the 48110296Ssam * server's connect may fail. 48210296Ssam */ 48311651Ssam static int sendport = -1; 48411651Ssam 48510296Ssam initconn() 48610296Ssam { 48710296Ssam register char *p, *a; 48811627Ssam int result, len; 48917450Slepreau int on = 1; 49010296Ssam 49111651Ssam noport: 49210296Ssam data_addr = myctladdr; 49311651Ssam if (sendport) 49411651Ssam data_addr.sin_port = 0; /* let system pick one */ 49511651Ssam if (data != -1) 49611651Ssam (void) close (data); 49718287Sralph data = socket(AF_INET, SOCK_STREAM, 0); 49810296Ssam if (data < 0) { 49910296Ssam perror("ftp: socket"); 50010296Ssam return (1); 50110296Ssam } 50212397Ssam if (!sendport) 50317450Slepreau if (setsockopt(data, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) < 0) { 50412397Ssam perror("ftp: setsockopt (resuse address)"); 50512397Ssam goto bad; 50612397Ssam } 50710296Ssam if (bind(data, (char *)&data_addr, sizeof (data_addr), 0) < 0) { 50810296Ssam perror("ftp: bind"); 50910296Ssam goto bad; 51010296Ssam } 51110296Ssam if (options & SO_DEBUG && 51217450Slepreau setsockopt(data, SOL_SOCKET, SO_DEBUG, &on, sizeof (on)) < 0) 51310296Ssam perror("ftp: setsockopt (ignored)"); 51411627Ssam len = sizeof (data_addr); 51511627Ssam if (getsockname(data, (char *)&data_addr, &len) < 0) { 51611627Ssam perror("ftp: getsockname"); 51710296Ssam goto bad; 51810296Ssam } 51910296Ssam if (listen(data, 1) < 0) { 52010296Ssam perror("ftp: listen"); 52110296Ssam goto bad; 52210296Ssam } 52311651Ssam if (sendport) { 52411651Ssam a = (char *)&data_addr.sin_addr; 52511651Ssam p = (char *)&data_addr.sin_port; 52610296Ssam #define UC(b) (((int)b)&0xff) 52711651Ssam result = 52811651Ssam command("PORT %d,%d,%d,%d,%d,%d", 52911651Ssam UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]), 53011651Ssam UC(p[0]), UC(p[1])); 53111651Ssam if (result == ERROR && sendport == -1) { 53211651Ssam sendport = 0; 53311651Ssam goto noport; 53411651Ssam } 53511651Ssam return (result != COMPLETE); 53611651Ssam } 53711651Ssam return (0); 53810296Ssam bad: 53910296Ssam (void) close(data), data = -1; 54010296Ssam return (1); 54110296Ssam } 54210296Ssam 54310296Ssam FILE * 54410296Ssam dataconn(mode) 54510296Ssam char *mode; 54610296Ssam { 54710296Ssam struct sockaddr_in from; 54810296Ssam int s, fromlen = sizeof (from); 54910296Ssam 55010296Ssam s = accept(data, &from, &fromlen, 0); 55110296Ssam if (s < 0) { 55210296Ssam perror("ftp: accept"); 55310296Ssam (void) close(data), data = -1; 55410296Ssam return (NULL); 55510296Ssam } 55610296Ssam (void) close(data); 55710296Ssam data = s; 55810296Ssam return (fdopen(data, mode)); 55910296Ssam } 56010296Ssam 56110296Ssam ptransfer(direction, bytes, t0, t1) 56210296Ssam char *direction; 56311651Ssam long bytes; 56410296Ssam struct timeval *t0, *t1; 56510296Ssam { 56610296Ssam struct timeval td; 56716437Sleres float s, bs; 56810296Ssam 56910296Ssam tvsub(&td, t1, t0); 57016437Sleres s = td.tv_sec + (td.tv_usec / 1000000.); 57110296Ssam #define nz(x) ((x) == 0 ? 1 : (x)) 57216437Sleres bs = bytes / nz(s); 57316437Sleres printf("%ld bytes %s in %.2g seconds (%.2g Kbytes/s)\n", 57416437Sleres bytes, direction, s, bs / 1024.); 57510296Ssam } 57610296Ssam 57710296Ssam tvadd(tsum, t0) 57810296Ssam struct timeval *tsum, *t0; 57910296Ssam { 58010296Ssam 58110296Ssam tsum->tv_sec += t0->tv_sec; 58210296Ssam tsum->tv_usec += t0->tv_usec; 58310296Ssam if (tsum->tv_usec > 1000000) 58410296Ssam tsum->tv_sec++, tsum->tv_usec -= 1000000; 58510296Ssam } 58610296Ssam 58710296Ssam tvsub(tdiff, t1, t0) 58810296Ssam struct timeval *tdiff, *t1, *t0; 58910296Ssam { 59010296Ssam 59110296Ssam tdiff->tv_sec = t1->tv_sec - t0->tv_sec; 59210296Ssam tdiff->tv_usec = t1->tv_usec - t0->tv_usec; 59310296Ssam if (tdiff->tv_usec < 0) 59410296Ssam tdiff->tv_sec--, tdiff->tv_usec += 1000000; 59510296Ssam } 596