xref: /csrg-svn/usr.bin/ftp/ftp.c (revision 11346)
110296Ssam #ifndef lint
2*11346Ssam static char sccsid[] = "@(#)ftp.c	4.5 (Berkeley) 02/28/83";
310296Ssam #endif
410296Ssam 
510296Ssam #include <sys/param.h>
610296Ssam #include <sys/stat.h>
710296Ssam #include <sys/ioctl.h>
810296Ssam #include <sys/socket.h>
910296Ssam 
1010296Ssam #include <netinet/in.h>
1110296Ssam 
1210296Ssam #include <stdio.h>
1310296Ssam #include <signal.h>
1410296Ssam #include <time.h>
1510296Ssam #include <errno.h>
1610296Ssam #include <netdb.h>
1710296Ssam 
1810296Ssam #include "ftp.h"
1910296Ssam #include "ftp_var.h"
2010296Ssam 
2110296Ssam struct	sockaddr_in hisctladdr;
2210296Ssam struct	sockaddr_in data_addr;
2310296Ssam int	data = -1;
2410296Ssam int	connected;
2510296Ssam struct	sockaddr_in myctladdr;
2610296Ssam 
2710296Ssam FILE	*cin, *cout;
2810296Ssam FILE	*dataconn();
2910296Ssam 
3010296Ssam struct hostent *
3110296Ssam hookup(host, port)
3210296Ssam 	char *host;
3310296Ssam 	int port;
3410296Ssam {
3510296Ssam 	register struct hostent *hp;
3610296Ssam 	int s;
3710296Ssam 
3810296Ssam 	bzero((char *)&hisctladdr, sizeof (hisctladdr));
3910296Ssam 	hp = gethostbyname(host);
4011283Ssam 	if (hp == NULL) {
4110296Ssam 		static struct hostent def;
4210296Ssam 		static struct in_addr defaddr;
4310296Ssam 		static char namebuf[128];
4410296Ssam 		int inet_addr();
4510296Ssam 
4610296Ssam 		defaddr.s_addr = inet_addr(host);
4710296Ssam 		if (defaddr.s_addr == -1) {
4810296Ssam 			fprintf(stderr, "%s: Unknown host.\n", host);
4910296Ssam 			return (0);
5010296Ssam 		}
5110296Ssam 		strcpy(namebuf, host);
5210296Ssam 		def.h_name = namebuf;
5310296Ssam 		hostname = namebuf;
5410296Ssam 		def.h_addr = (char *)&defaddr;
5510296Ssam 		def.h_length = sizeof (struct in_addr);
5610296Ssam 		def.h_addrtype = AF_INET;
5710296Ssam 		def.h_aliases = 0;
5810296Ssam 		hp = &def;
5910296Ssam 	}
6011283Ssam 	hostname = hp->h_name;
6111283Ssam 	hisctladdr.sin_family = hp->h_addrtype;
6210296Ssam 	s = socket(hp->h_addrtype, SOCK_STREAM, 0, 0);
6310296Ssam 	if (s < 0) {
6410296Ssam 		perror("ftp: socket");
6510296Ssam 		return (0);
6610296Ssam 	}
6710296Ssam 	if (bind(s, (char *)&hisctladdr, sizeof (hisctladdr), 0) < 0) {
6810296Ssam 		perror("ftp: bind");
6910296Ssam 		goto bad;
7010296Ssam 	}
7110296Ssam 	bcopy(hp->h_addr, (char *)&hisctladdr.sin_addr, hp->h_length);
7210296Ssam 	hisctladdr.sin_port = port;
7310296Ssam 	if (connect(s, (char *)&hisctladdr, sizeof (hisctladdr), 0) < 0) {
7410296Ssam 		perror("ftp: connect");
7510296Ssam 		goto bad;
7610296Ssam 	}
7710296Ssam 	if (socketaddr(s, &myctladdr) < 0) {
7810296Ssam 		perror("ftp: socketaddr");
7910296Ssam 		goto bad;
8010296Ssam 	}
8110296Ssam 	cin = fdopen(s, "r");
8210296Ssam 	cout = fdopen(s, "w");
8311219Ssam 	if (cin == NULL || cout == NULL) {
8410296Ssam 		fprintf(stderr, "ftp: fdopen failed.\n");
8510296Ssam 		if (cin)
8610296Ssam 			fclose(cin);
8710296Ssam 		if (cout)
8810296Ssam 			fclose(cout);
8910296Ssam 		goto bad;
9010296Ssam 	}
9110296Ssam 	if (verbose)
9210296Ssam 		printf("Connected to %s.\n", hp->h_name);
9310296Ssam 	(void) getreply(0); 		/* read startup message from server */
9410296Ssam 	return (hp);
9510296Ssam bad:
9610296Ssam 	close(s);
9710296Ssam 	return ((struct hostent *)0);
9810296Ssam }
9910296Ssam 
10010296Ssam login(hp)
10110296Ssam 	struct hostent *hp;
10210296Ssam {
10310296Ssam 	char acct[80];
10410296Ssam 	char *user, *pass;
10510296Ssam 	int n;
10610296Ssam 
10710296Ssam 	user = pass = 0;
10810296Ssam 	ruserpass(hp->h_name, &user, &pass);
10910296Ssam 	n = command("USER %s", user);
11010296Ssam 	if (n == CONTINUE)
11110296Ssam 		n = command("PASS %s", pass);
11210296Ssam 	if (n == CONTINUE) {
11310296Ssam 		printf("Account: "); (void) fflush(stdout);
11410296Ssam 		(void) fgets(acct, sizeof(acct) - 1, stdin);
11510296Ssam 		acct[strlen(acct) - 1] = '\0';
11610296Ssam 		n = command("ACCT %s", acct);
11710296Ssam 	}
11810296Ssam 	if (n != COMPLETE) {
11910296Ssam 		fprintf(stderr, "Login failed.\n");
12010296Ssam 		return (0);
12110296Ssam 	}
12210296Ssam 	return (1);
12310296Ssam }
12410296Ssam 
12510296Ssam /*VARARGS 1*/
12610296Ssam command(fmt, args)
12710296Ssam 	char *fmt;
12810296Ssam {
12910296Ssam 
13010296Ssam 	if (debug) {
13110296Ssam 		printf("---> ");
13210296Ssam 		_doprnt(fmt, &args, stdout);
13310296Ssam 		printf("\n");
13410296Ssam 		(void) fflush(stdout);
13510296Ssam 	}
13611219Ssam 	if (cout == NULL) {
13711219Ssam 		perror ("No control connection for command");
13811219Ssam 		return (0);
13911219Ssam 	}
14010296Ssam 	_doprnt(fmt, &args, cout);
14110296Ssam 	fprintf(cout, "\r\n");
14210296Ssam 	(void) fflush(cout);
14310296Ssam 	return (getreply(!strcmp(fmt, "QUIT")));
14410296Ssam }
14510296Ssam 
14610296Ssam #include <ctype.h>
14710296Ssam 
14810296Ssam getreply(expecteof)
14910296Ssam 	int expecteof;
15010296Ssam {
15111219Ssam 	register int c, n;
15210296Ssam 	register int code, dig;
15310296Ssam 	int originalcode = 0, continuation = 0;
15410296Ssam 
15510296Ssam 	for (;;) {
15610296Ssam 		dig = n = code = 0;
15710296Ssam 		while ((c = getc(cin)) != '\n') {
15810296Ssam 			dig++;
15910296Ssam 			if (c == EOF) {
16010296Ssam 				if (expecteof)
16110296Ssam 					return (0);
16210296Ssam 				lostpeer();
16310296Ssam 				exit(1);
16410296Ssam 			}
16510296Ssam 			if (verbose && c != '\r' ||
16610296Ssam 			    (n == '5' && dig > 4))
16710296Ssam 				putchar(c);
16810296Ssam 			if (dig < 4 && isdigit(c))
16910296Ssam 				code = code * 10 + (c - '0');
17010296Ssam 			if (dig == 4 && c == '-')
17110296Ssam 				continuation++;
17210296Ssam 			if (n == 0)
17310296Ssam 				n = c;
17410296Ssam 		}
175*11346Ssam 		if (verbose || n == '5') {
17610296Ssam 			putchar(c);
177*11346Ssam 			(void) fflush (stdout);
178*11346Ssam 		}
17910296Ssam 		if (continuation && code != originalcode) {
18010296Ssam 			if (originalcode == 0)
18110296Ssam 				originalcode = code;
18210296Ssam 			continue;
18310296Ssam 		}
18411219Ssam 		if (expecteof || empty(cin))
18510296Ssam 			return (n - '0');
18610296Ssam 	}
18710296Ssam }
18810296Ssam 
18910296Ssam empty(f)
19010296Ssam 	FILE *f;
19110296Ssam {
19210296Ssam 	int mask;
19310296Ssam 	struct timeval t;
19410296Ssam 
19510296Ssam 	if (f->_cnt > 0)
19610296Ssam 		return (0);
19710296Ssam 	mask = (1 << fileno(f));
19810296Ssam 	t.tv_sec = t.tv_usec = 0;
19910296Ssam 	(void) select(20, &mask, 0, 0, &t);
20010296Ssam 	return (mask == 0);
20110296Ssam }
20210296Ssam 
20310296Ssam jmp_buf	sendabort;
20410296Ssam 
20510296Ssam abortsend()
20610296Ssam {
20710296Ssam 
20810296Ssam 	longjmp(sendabort, 1);
20910296Ssam }
21010296Ssam 
21110296Ssam sendrequest(cmd, local, remote)
21210296Ssam 	char *cmd, *local, *remote;
21310296Ssam {
21410296Ssam 	FILE *fin, *dout, *popen();
21510296Ssam 	int (*closefunc)(), pclose(), fclose(), (*oldintr)();
21611219Ssam 	char buf[BUFSIZ];
217*11346Ssam 	register int bytes = 0;
218*11346Ssam 	register int c, d;
21910296Ssam 	struct stat st;
22010296Ssam 	struct timeval start, stop;
22110296Ssam 
22210296Ssam 	closefunc = NULL;
22310296Ssam 	if (setjmp(sendabort))
22410296Ssam 		goto bad;
22510296Ssam 	oldintr = signal(SIGINT, abortsend);
22610296Ssam 	if (strcmp(local, "-") == 0)
22710296Ssam 		fin = stdin;
22810296Ssam 	else if (*local == '|') {
22910296Ssam 		fin = popen(local + 1, "r");
23010296Ssam 		if (fin == NULL) {
23110296Ssam 			perror(local + 1);
23210296Ssam 			goto bad;
23310296Ssam 		}
23410296Ssam 		closefunc = pclose;
23510296Ssam 	} else {
23610296Ssam 		fin = fopen(local, "r");
23710296Ssam 		if (fin == NULL) {
23810296Ssam 			perror(local);
23910296Ssam 			goto bad;
24010296Ssam 		}
24110296Ssam 		closefunc = fclose;
24210296Ssam 		if (fstat(fileno(fin), &st) < 0 ||
24310296Ssam 		    (st.st_mode&S_IFMT) != S_IFREG) {
24410296Ssam 			fprintf(stderr, "%s: not a plain file.", local);
24510296Ssam 			goto bad;
24610296Ssam 		}
24710296Ssam 	}
24810296Ssam 	if (initconn())
24910296Ssam 		goto bad;
25010296Ssam 	if (remote) {
25110296Ssam 		if (command("%s %s", cmd, remote) != PRELIM)
25210296Ssam 			goto bad;
25310296Ssam 	} else
25410296Ssam 		if (command("%s", cmd) != PRELIM)
25510296Ssam 			goto bad;
25610296Ssam 	dout = dataconn("w");
25710296Ssam 	if (dout == NULL)
25810296Ssam 		goto bad;
25910296Ssam 	gettimeofday(&start, (struct timezone *)0);
26011219Ssam 	switch (type) {
26111219Ssam 
26211219Ssam 	case TYPE_I:
26311219Ssam 	case TYPE_L:
264*11346Ssam 		errno = d = 0;
26511219Ssam 		while ((c = read(fileno (fin), buf, sizeof (buf))) > 0) {
266*11346Ssam 			if ((d = write(fileno (dout), buf, c)) < 0)
26711219Ssam 				break;
26811219Ssam 			bytes += c;
26911219Ssam 		}
27011219Ssam 		if (c < 0)
27111219Ssam 			perror(local);
272*11346Ssam 		if (d < 0)
27311219Ssam 			perror("netout");
27411219Ssam 		break;
27511219Ssam 
27611219Ssam 	case TYPE_A:
27711219Ssam 		while ((c = getc(fin)) != EOF) {
27811219Ssam 			if (c == '\n') {
27911219Ssam 				if (ferror(dout))
28011219Ssam 					break;
28111219Ssam 				putc('\r', dout);
28211219Ssam 				bytes++;
28311219Ssam 			}
28411219Ssam 			putc(c, dout);
28511219Ssam 			bytes++;
28611219Ssam 			if (c == '\r') {
28711219Ssam 				putc('\0', dout);
28811219Ssam 				bytes++;
28911219Ssam 			}
29011219Ssam 		}
29111219Ssam 		if (ferror(fin))
29211219Ssam 			perror(local);
293*11346Ssam 		if (ferror(dout))
29411219Ssam 			perror("netout");
29511219Ssam 		break;
29610296Ssam 	}
29710296Ssam 	gettimeofday(&stop, (struct timezone *)0);
29810296Ssam 	if (closefunc != NULL)
29910296Ssam 		(*closefunc)(fin);
30010296Ssam 	(void) fclose(dout);
30110296Ssam 	(void) getreply(0);
30210296Ssam done:
30310296Ssam 	signal(SIGINT, oldintr);
30410296Ssam 	if (bytes > 0 && verbose)
30510296Ssam 		ptransfer("sent", bytes, &start, &stop);
30610296Ssam 	return;
30710296Ssam bad:
30810296Ssam 	if (data >= 0)
30910296Ssam 		(void) close(data), data = -1;
31010296Ssam 	if (closefunc != NULL && fin != NULL)
31110296Ssam 		(*closefunc)(fin);
31210296Ssam 	goto done;
31310296Ssam }
31410296Ssam 
31510296Ssam jmp_buf	recvabort;
31610296Ssam 
31710296Ssam abortrecv()
31810296Ssam {
31910296Ssam 
32010296Ssam 	longjmp(recvabort, 1);
32110296Ssam }
32210296Ssam 
32310296Ssam recvrequest(cmd, local, remote)
32410296Ssam 	char *cmd, *local, *remote;
32510296Ssam {
32610296Ssam 	FILE *fout, *din, *popen();
32711219Ssam 	char buf[BUFSIZ];
328*11346Ssam 	int (*closefunc)(), pclose(), fclose(), (*oldintr)();
32910296Ssam 	register int bytes = 0;
330*11346Ssam 	register int c, d;
33110296Ssam 	struct timeval start, stop;
33210296Ssam 
33310296Ssam 	closefunc = NULL;
33410296Ssam 	if (setjmp(recvabort))
33510296Ssam 		goto bad;
33610296Ssam 	oldintr = signal(SIGINT, abortrecv);
33710296Ssam 	if (strcmp(local, "-") && *local != '|')
33810296Ssam 		if (access(local, 2) < 0) {
33910296Ssam 			char *dir = rindex(local, '/');
34010296Ssam 
34110296Ssam 			if (dir != NULL)
34210296Ssam 				*dir = 0;
34310296Ssam 			if (access(dir ? dir : ".", 2) < 0) {
34410296Ssam 				perror(local);
34510296Ssam 				goto bad;
34610296Ssam 			}
34710296Ssam 			if (dir != NULL)
34810296Ssam 				*dir = '/';
34910296Ssam 		}
35010296Ssam 	if (initconn())
35110296Ssam 		goto bad;
35210296Ssam 	if (remote) {
35310296Ssam 		if (command("%s %s", cmd, remote) != PRELIM)
35410296Ssam 			goto bad;
35510296Ssam 	} else
35610296Ssam 		if (command("%s", cmd) != PRELIM)
35710296Ssam 			goto bad;
35810296Ssam 	if (strcmp(local, "-") == 0)
35910296Ssam 		fout = stdout;
36010296Ssam 	else if (*local == '|') {
36110296Ssam 		fout = popen(local + 1, "w");
36210296Ssam 		closefunc = pclose;
36310296Ssam 	} else {
36410296Ssam 		fout = fopen(local, "w");
36510296Ssam 		closefunc = fclose;
36610296Ssam 	}
36710296Ssam 	if (fout == NULL) {
36810296Ssam 		perror(local + 1);
36910296Ssam 		goto bad;
37010296Ssam 	}
37110296Ssam 	din = dataconn("r");
37210296Ssam 	if (din == NULL)
37310296Ssam 		goto bad;
37410296Ssam 	gettimeofday(&start, (struct timezone *)0);
37511219Ssam 	switch (type) {
37611219Ssam 
37711219Ssam 	case TYPE_I:
37811219Ssam 	case TYPE_L:
379*11346Ssam 		errno = d = 0;
38011219Ssam 		while ((c = read(fileno(din), buf, sizeof (buf))) > 0) {
381*11346Ssam 			if ((d = write(fileno(fout), buf, c)) < 0)
38211219Ssam 				break;
38311219Ssam 			bytes += c;
38411219Ssam 		}
38511219Ssam 		if (c < 0)
38611219Ssam 			perror("netin");
387*11346Ssam 		if (d < 0)
38810296Ssam 			perror(local);
38911219Ssam 		break;
39011219Ssam 
39111219Ssam 	case TYPE_A:
39211219Ssam 		while ((c = getc(din)) != EOF) {
39311219Ssam 			if (c == '\r') {
39410296Ssam 				bytes++;
39511219Ssam 				if ((c = getc(din)) != '\n') {
39611219Ssam 					if (ferror (fout))
39711219Ssam 						break;
39811219Ssam 					putc ('\r', fout);
39911219Ssam 				}
40011219Ssam 				if (c == '\0') {
40111219Ssam 					bytes++;
40211219Ssam 					continue;
40311219Ssam 				}
40411219Ssam 			}
40511219Ssam 			putc (c, fout);
40611219Ssam 			bytes++;
40710296Ssam 		}
40811219Ssam 		if (ferror (din))
40911219Ssam 			perror ("netin");
41011219Ssam 		if (ferror (fout))
41111219Ssam 			perror (local);
41211219Ssam 		break;
41310296Ssam 	}
41410296Ssam 	gettimeofday(&stop, (struct timezone *)0);
41510296Ssam 	(void) fclose(din);
41610296Ssam 	if (closefunc != NULL)
41710296Ssam 		(*closefunc)(fout);
41810296Ssam 	(void) getreply(0);
41910296Ssam done:
42010296Ssam 	signal(SIGINT, oldintr);
42110296Ssam 	if (bytes > 0 && verbose)
42210296Ssam 		ptransfer("received", bytes, &start, &stop);
42310296Ssam 	return;
42410296Ssam bad:
42510296Ssam 	if (data >= 0)
42610296Ssam 		(void) close(data), data = -1;
42710296Ssam 	if (closefunc != NULL && fout != NULL)
42810296Ssam 		(*closefunc)(fout);
42910296Ssam 	goto done;
43010296Ssam }
43110296Ssam 
43210296Ssam /*
43310296Ssam  * Need to start a listen on the data channel
43410296Ssam  * before we send the command, otherwise the
43510296Ssam  * server's connect may fail.
43610296Ssam  */
43710296Ssam initconn()
43810296Ssam {
43910296Ssam 	register char *p, *a;
44010296Ssam 	int result;
44110296Ssam 
44210296Ssam 	data_addr = myctladdr;
44310296Ssam 	data_addr.sin_port = 0;		/* let system pick one */
44410296Ssam 	data = socket(AF_INET, SOCK_STREAM, 0, 0);
44510296Ssam 	if (data < 0) {
44610296Ssam 		perror("ftp: socket");
44710296Ssam 		return (1);
44810296Ssam 	}
44910296Ssam 	if (bind(data, (char *)&data_addr, sizeof (data_addr), 0) < 0) {
45010296Ssam 		perror("ftp: bind");
45110296Ssam 		goto bad;
45210296Ssam 	}
45310296Ssam 	if (options & SO_DEBUG &&
45410296Ssam 	    setsockopt(data, SOL_SOCKET, SO_DEBUG, 0, 0) < 0)
45510296Ssam 		perror("ftp: setsockopt (ignored)");
45610296Ssam 	if (socketaddr(data, &data_addr) < 0) {
45710296Ssam 		perror("ftp: socketaddr");
45810296Ssam 		goto bad;
45910296Ssam 	}
46010296Ssam 	if (listen(data, 1) < 0) {
46110296Ssam 		perror("ftp: listen");
46210296Ssam 		goto bad;
46310296Ssam 	}
46410296Ssam 	a = (char *)&data_addr.sin_addr;
46510296Ssam 	p = (char *)&data_addr.sin_port;
46610296Ssam #define	UC(b)	(((int)b)&0xff)
46710296Ssam 	result =
46810296Ssam 	    command("PORT %d,%d,%d,%d,%d,%d",
46910296Ssam 	      UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
47010296Ssam 	      UC(p[0]), UC(p[1]));
47110296Ssam 	return (result != COMPLETE);
47210296Ssam bad:
47310296Ssam 	(void) close(data), data = -1;
47410296Ssam 	return (1);
47510296Ssam }
47610296Ssam 
47710296Ssam FILE *
47810296Ssam dataconn(mode)
47910296Ssam 	char *mode;
48010296Ssam {
48110296Ssam 	struct sockaddr_in from;
48210296Ssam 	int s, fromlen = sizeof (from);
48310296Ssam 
48410296Ssam 	s = accept(data, &from, &fromlen, 0);
48510296Ssam 	if (s < 0) {
48610296Ssam 		perror("ftp: accept");
48710296Ssam 		(void) close(data), data = -1;
48810296Ssam 		return (NULL);
48910296Ssam 	}
49010296Ssam 	(void) close(data);
49110296Ssam 	data = s;
49210296Ssam 	return (fdopen(data, mode));
49310296Ssam }
49410296Ssam 
49510296Ssam ptransfer(direction, bytes, t0, t1)
49610296Ssam 	char *direction;
49710296Ssam 	int bytes;
49810296Ssam 	struct timeval *t0, *t1;
49910296Ssam {
50010296Ssam 	struct timeval td;
50110296Ssam 	int ms, bs;
50210296Ssam 
50310296Ssam 	tvsub(&td, t1, t0);
50410296Ssam 	ms = (td.tv_sec * 1000) + (td.tv_usec / 1000);
50510296Ssam #define	nz(x)	((x) == 0 ? 1 : (x))
50610296Ssam 	bs = ((bytes * NBBY * 1000) / nz(ms)) / NBBY;
50710296Ssam 	printf("%d bytes %s in %d.%02d seconds (%d.%01d Kbytes/s)\n",
50810296Ssam 		bytes, direction, td.tv_sec, td.tv_usec / 10000,
50910296Ssam 		bs / 1024, (((bs % 1024) * 10) + 1023) / 1024);
51010296Ssam }
51110296Ssam 
51210296Ssam tvadd(tsum, t0)
51310296Ssam 	struct timeval *tsum, *t0;
51410296Ssam {
51510296Ssam 
51610296Ssam 	tsum->tv_sec += t0->tv_sec;
51710296Ssam 	tsum->tv_usec += t0->tv_usec;
51810296Ssam 	if (tsum->tv_usec > 1000000)
51910296Ssam 		tsum->tv_sec++, tsum->tv_usec -= 1000000;
52010296Ssam }
52110296Ssam 
52210296Ssam tvsub(tdiff, t1, t0)
52310296Ssam 	struct timeval *tdiff, *t1, *t0;
52410296Ssam {
52510296Ssam 
52610296Ssam 	tdiff->tv_sec = t1->tv_sec - t0->tv_sec;
52710296Ssam 	tdiff->tv_usec = t1->tv_usec - t0->tv_usec;
52810296Ssam 	if (tdiff->tv_usec < 0)
52910296Ssam 		tdiff->tv_sec--, tdiff->tv_usec += 1000000;
53010296Ssam }
531