xref: /csrg-svn/usr.bin/ftp/ftp.c (revision 40193)
121739Sdist /*
236942Skarels  * Copyright (c) 1985, 1989 Regents of the University of California.
333737Sbostic  * All rights reserved.
433737Sbostic  *
533737Sbostic  * Redistribution and use in source and binary forms are permitted
634901Sbostic  * provided that the above copyright notice and this paragraph are
734901Sbostic  * duplicated in all such forms and that any documentation,
834901Sbostic  * advertising materials, and other materials related to such
934901Sbostic  * distribution and use acknowledge that the software was developed
1034901Sbostic  * by the University of California, Berkeley.  The name of the
1134901Sbostic  * University may not be used to endorse or promote products derived
1234901Sbostic  * from this software without specific prior written permission.
1334901Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1434901Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1536935Skarels  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1621739Sdist  */
1721739Sdist 
1810296Ssam #ifndef lint
19*40193Sbostic static char sccsid[] = "@(#)ftp.c	5.32 (Berkeley) 02/21/90";
2033737Sbostic #endif /* not lint */
2110296Ssam 
2236940Skarels #include <sys/param.h>
2310296Ssam #include <sys/stat.h>
2410296Ssam #include <sys/ioctl.h>
2510296Ssam #include <sys/socket.h>
2613614Ssam #include <sys/time.h>
2736935Skarels #include <sys/file.h>
2810296Ssam 
2910296Ssam #include <netinet/in.h>
3012397Ssam #include <arpa/ftp.h>
3126048Sminshall #include <arpa/telnet.h>
3210296Ssam 
3310296Ssam #include <stdio.h>
3410296Ssam #include <signal.h>
3510296Ssam #include <errno.h>
3610296Ssam #include <netdb.h>
3726048Sminshall #include <fcntl.h>
3826048Sminshall #include <pwd.h>
3938133Srick #include <varargs.h>
4010296Ssam 
4136940Skarels #include "ftp_var.h"
4236940Skarels 
4310296Ssam struct	sockaddr_in hisctladdr;
4410296Ssam struct	sockaddr_in data_addr;
4510296Ssam int	data = -1;
4626048Sminshall int	abrtflag = 0;
4726048Sminshall int	ptflag = 0;
4810296Ssam struct	sockaddr_in myctladdr;
4926496Sminshall uid_t	getuid();
5038133Srick sig_t	lostpeer();
5137225Skarels off_t	restart_point = 0;
5210296Ssam 
5338202Srick extern char *strerror();
54*40193Sbostic extern int connected, errno;
5538202Srick 
5610296Ssam FILE	*cin, *cout;
5710296Ssam FILE	*dataconn();
5810296Ssam 
5925904Skarels char *
6010296Ssam hookup(host, port)
6110296Ssam 	char *host;
6210296Ssam 	int port;
6310296Ssam {
6425904Skarels 	register struct hostent *hp = 0;
6527687Sminshall 	int s,len;
6625904Skarels 	static char hostnamebuf[80];
6710296Ssam 
6810296Ssam 	bzero((char *)&hisctladdr, sizeof (hisctladdr));
6925904Skarels 	hisctladdr.sin_addr.s_addr = inet_addr(host);
7025904Skarels 	if (hisctladdr.sin_addr.s_addr != -1) {
7125904Skarels 		hisctladdr.sin_family = AF_INET;
7236940Skarels 		(void) strncpy(hostnamebuf, host, sizeof(hostnamebuf));
7336940Skarels 	} else {
7425100Sbloom 		hp = gethostbyname(host);
7525904Skarels 		if (hp == NULL) {
7635792Sbostic 			fprintf(stderr, "ftp: %s: ", host);
7735792Sbostic 			herror((char *)NULL);
7826048Sminshall 			code = -1;
7926048Sminshall 			return((char *) 0);
8025904Skarels 		}
8125904Skarels 		hisctladdr.sin_family = hp->h_addrtype;
8225904Skarels 		bcopy(hp->h_addr_list[0],
8325904Skarels 		    (caddr_t)&hisctladdr.sin_addr, hp->h_length);
8436940Skarels 		(void) strncpy(hostnamebuf, hp->h_name, sizeof(hostnamebuf));
8510296Ssam 	}
8625904Skarels 	hostname = hostnamebuf;
8725904Skarels 	s = socket(hisctladdr.sin_family, SOCK_STREAM, 0);
8810296Ssam 	if (s < 0) {
8910296Ssam 		perror("ftp: socket");
9026048Sminshall 		code = -1;
9110296Ssam 		return (0);
9210296Ssam 	}
9310296Ssam 	hisctladdr.sin_port = port;
9438133Srick 	while (connect(s, (struct sockaddr *)&hisctladdr, sizeof (hisctladdr)) < 0) {
9525904Skarels 		if (hp && hp->h_addr_list[1]) {
9625904Skarels 			int oerrno = errno;
9738133Srick 			extern char *inet_ntoa();
9825904Skarels 
9925904Skarels 			fprintf(stderr, "ftp: connect to address %s: ",
10025904Skarels 				inet_ntoa(hisctladdr.sin_addr));
10125904Skarels 			errno = oerrno;
10226496Sminshall 			perror((char *) 0);
10325904Skarels 			hp->h_addr_list++;
10425904Skarels 			bcopy(hp->h_addr_list[0],
10526048Sminshall 			     (caddr_t)&hisctladdr.sin_addr, hp->h_length);
10626496Sminshall 			fprintf(stdout, "Trying %s...\n",
10725904Skarels 				inet_ntoa(hisctladdr.sin_addr));
10826813Skarels 			(void) close(s);
10926813Skarels 			s = socket(hisctladdr.sin_family, SOCK_STREAM, 0);
11026813Skarels 			if (s < 0) {
11126813Skarels 				perror("ftp: socket");
11226813Skarels 				code = -1;
11326813Skarels 				return (0);
11426813Skarels 			}
11525904Skarels 			continue;
11625904Skarels 		}
11710296Ssam 		perror("ftp: connect");
11826048Sminshall 		code = -1;
11910296Ssam 		goto bad;
12010296Ssam 	}
12111627Ssam 	len = sizeof (myctladdr);
12238133Srick 	if (getsockname(s, (struct sockaddr *)&myctladdr, &len) < 0) {
12311627Ssam 		perror("ftp: getsockname");
12426048Sminshall 		code = -1;
12510296Ssam 		goto bad;
12610296Ssam 	}
12710296Ssam 	cin = fdopen(s, "r");
12810296Ssam 	cout = fdopen(s, "w");
12911219Ssam 	if (cin == NULL || cout == NULL) {
13010296Ssam 		fprintf(stderr, "ftp: fdopen failed.\n");
13110296Ssam 		if (cin)
13226496Sminshall 			(void) fclose(cin);
13310296Ssam 		if (cout)
13426496Sminshall 			(void) fclose(cout);
13526048Sminshall 		code = -1;
13610296Ssam 		goto bad;
13710296Ssam 	}
13810296Ssam 	if (verbose)
13926067Sminshall 		printf("Connected to %s.\n", hostname);
14027687Sminshall 	if (getreply(0) > 2) { 	/* read startup message from server */
14126048Sminshall 		if (cin)
14226496Sminshall 			(void) fclose(cin);
14326048Sminshall 		if (cout)
14426496Sminshall 			(void) fclose(cout);
14526048Sminshall 		code = -1;
14626048Sminshall 		goto bad;
14726048Sminshall 	}
14827687Sminshall #ifdef SO_OOBINLINE
14927687Sminshall 	{
15027687Sminshall 	int on = 1;
15126048Sminshall 
152*40193Sbostic 	if (setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (char *)&on, sizeof(on))
15327687Sminshall 		< 0 && debug) {
15427687Sminshall 			perror("ftp: setsockopt");
15527687Sminshall 		}
15627687Sminshall 	}
15738133Srick #endif /* SO_OOBINLINE */
15826048Sminshall 
15925904Skarels 	return (hostname);
16010296Ssam bad:
16126496Sminshall 	(void) close(s);
16225904Skarels 	return ((char *)0);
16310296Ssam }
16410296Ssam 
16525904Skarels login(host)
16625904Skarels 	char *host;
16710296Ssam {
16826048Sminshall 	char tmp[80];
16935659Sbostic 	char *user, *pass, *acct, *getlogin(), *getpass();
17026048Sminshall 	int n, aflag = 0;
17110296Ssam 
17226048Sminshall 	user = pass = acct = 0;
17326048Sminshall 	if (ruserpass(host, &user, &pass, &acct) < 0) {
17426048Sminshall 		code = -1;
17526048Sminshall 		return(0);
17626048Sminshall 	}
17737458Skarels 	while (user == NULL) {
17826048Sminshall 		char *myname = getlogin();
17926048Sminshall 
18026048Sminshall 		if (myname == NULL) {
18126048Sminshall 			struct passwd *pp = getpwuid(getuid());
18226048Sminshall 
18326448Slepreau 			if (pp != NULL)
18426048Sminshall 				myname = pp->pw_name;
18526048Sminshall 		}
18637458Skarels 		if (myname)
18737458Skarels 			printf("Name (%s:%s): ", host, myname);
18837458Skarels 		else
18937458Skarels 			printf("Name (%s): ", host);
19026048Sminshall 		(void) fgets(tmp, sizeof(tmp) - 1, stdin);
19126048Sminshall 		tmp[strlen(tmp) - 1] = '\0';
19226448Slepreau 		if (*tmp == '\0')
19326048Sminshall 			user = myname;
19426448Slepreau 		else
19526048Sminshall 			user = tmp;
19626048Sminshall 	}
19710296Ssam 	n = command("USER %s", user);
19826048Sminshall 	if (n == CONTINUE) {
19926448Slepreau 		if (pass == NULL)
20035659Sbostic 			pass = getpass("Password:");
20110296Ssam 		n = command("PASS %s", pass);
20226048Sminshall 	}
20310296Ssam 	if (n == CONTINUE) {
20426048Sminshall 		aflag++;
20535659Sbostic 		acct = getpass("Account:");
20610296Ssam 		n = command("ACCT %s", acct);
20710296Ssam 	}
20810296Ssam 	if (n != COMPLETE) {
20910296Ssam 		fprintf(stderr, "Login failed.\n");
21010296Ssam 		return (0);
21110296Ssam 	}
21226448Slepreau 	if (!aflag && acct != NULL)
21326048Sminshall 		(void) command("ACCT %s", acct);
21426448Slepreau 	if (proxy)
21526048Sminshall 		return(1);
21626048Sminshall 	for (n = 0; n < macnum; ++n) {
21726048Sminshall 		if (!strcmp("init", macros[n].mac_name)) {
21826496Sminshall 			(void) strcpy(line, "$init");
21926048Sminshall 			makeargv();
22026048Sminshall 			domacro(margc, margv);
22126048Sminshall 			break;
22226048Sminshall 		}
22326048Sminshall 	}
22410296Ssam 	return (1);
22510296Ssam }
22610296Ssam 
227*40193Sbostic void
22826048Sminshall cmdabort()
22926048Sminshall {
23026048Sminshall 	extern jmp_buf ptabort;
23126048Sminshall 
23226048Sminshall 	printf("\n");
23326048Sminshall 	(void) fflush(stdout);
23426048Sminshall 	abrtflag++;
23526448Slepreau 	if (ptflag)
23626048Sminshall 		longjmp(ptabort,1);
23726048Sminshall }
23826048Sminshall 
23938133Srick /*VARARGS*/
24038133Srick command(va_alist)
24138133Srick va_dcl
24238133Srick {
24338133Srick 	va_list ap;
24410296Ssam 	char *fmt;
24538133Srick 	int r;
246*40193Sbostic 	sig_t oldintr;
247*40193Sbostic 	void cmdabort();
24810296Ssam 
24926048Sminshall 	abrtflag = 0;
25010296Ssam 	if (debug) {
25110296Ssam 		printf("---> ");
25238133Srick 		va_start(ap);
25338133Srick 		fmt = va_arg(ap, char *);
25438133Srick 		if (strncmp("PASS ", fmt, 5) == 0)
25538133Srick 			printf("PASS XXXX");
25638133Srick 		else
25738133Srick 			vfprintf(stdout, fmt, ap);
25838133Srick 		va_end(ap);
25910296Ssam 		printf("\n");
26010296Ssam 		(void) fflush(stdout);
26110296Ssam 	}
26211219Ssam 	if (cout == NULL) {
26311219Ssam 		perror ("No control connection for command");
26426048Sminshall 		code = -1;
26511219Ssam 		return (0);
26611219Ssam 	}
26738133Srick 	oldintr = signal(SIGINT, cmdabort);
26838133Srick 	va_start(ap);
26938133Srick 	fmt = va_arg(ap, char *);
27038133Srick 	vfprintf(cout, fmt, ap);
27138133Srick 	va_end(ap);
27210296Ssam 	fprintf(cout, "\r\n");
27310296Ssam 	(void) fflush(cout);
27426048Sminshall 	cpend = 1;
27526048Sminshall 	r = getreply(!strcmp(fmt, "QUIT"));
27626448Slepreau 	if (abrtflag && oldintr != SIG_IGN)
27726048Sminshall 		(*oldintr)();
27826048Sminshall 	(void) signal(SIGINT, oldintr);
27926048Sminshall 	return(r);
28010296Ssam }
28110296Ssam 
28237229Skarels char reply_string[BUFSIZ];		/* last line of previous reply */
28336935Skarels 
28410296Ssam #include <ctype.h>
28510296Ssam 
28610296Ssam getreply(expecteof)
28710296Ssam 	int expecteof;
28810296Ssam {
28911219Ssam 	register int c, n;
29026048Sminshall 	register int dig;
29136935Skarels 	register char *cp;
29238133Srick 	int originalcode = 0, continuation = 0;
293*40193Sbostic 	sig_t oldintr;
29426048Sminshall 	int pflag = 0;
29526048Sminshall 	char *pt = pasv;
296*40193Sbostic 	void cmdabort();
29710296Ssam 
29838133Srick 	oldintr = signal(SIGINT, cmdabort);
29910296Ssam 	for (;;) {
30010296Ssam 		dig = n = code = 0;
30137229Skarels 		cp = reply_string;
30210296Ssam 		while ((c = getc(cin)) != '\n') {
30327687Sminshall 			if (c == IAC) {     /* handle telnet commands */
30427687Sminshall 				switch (c = getc(cin)) {
30527687Sminshall 				case WILL:
30627687Sminshall 				case WONT:
30727687Sminshall 					c = getc(cin);
30838133Srick 					fprintf(cout, "%c%c%c", IAC, DONT, c);
30927687Sminshall 					(void) fflush(cout);
31027687Sminshall 					break;
31127687Sminshall 				case DO:
31227687Sminshall 				case DONT:
31327687Sminshall 					c = getc(cin);
31438133Srick 					fprintf(cout, "%c%c%c", IAC, WONT, c);
31527687Sminshall 					(void) fflush(cout);
31627687Sminshall 					break;
31727687Sminshall 				default:
31827687Sminshall 					break;
31927687Sminshall 				}
32027687Sminshall 				continue;
32127687Sminshall 			}
32210296Ssam 			dig++;
32310296Ssam 			if (c == EOF) {
32426048Sminshall 				if (expecteof) {
32526048Sminshall 					(void) signal(SIGINT,oldintr);
32626048Sminshall 					code = 221;
32710296Ssam 					return (0);
32826048Sminshall 				}
32910296Ssam 				lostpeer();
33026048Sminshall 				if (verbose) {
33126048Sminshall 					printf("421 Service not available, remote server has closed connection\n");
33226048Sminshall 					(void) fflush(stdout);
33326048Sminshall 				}
33433772Scsvsj 				code = 421;
33533772Scsvsj 				return(4);
33610296Ssam 			}
33726048Sminshall 			if (c != '\r' && (verbose > 0 ||
33826048Sminshall 			    (verbose > -1 && n == '5' && dig > 4))) {
33926448Slepreau 				if (proxflag &&
34026448Slepreau 				   (dig == 1 || dig == 5 && verbose == 0))
34126048Sminshall 					printf("%s:",hostname);
34226496Sminshall 				(void) putchar(c);
34326048Sminshall 			}
34410296Ssam 			if (dig < 4 && isdigit(c))
34510296Ssam 				code = code * 10 + (c - '0');
34626448Slepreau 			if (!pflag && code == 227)
34726048Sminshall 				pflag = 1;
34826448Slepreau 			if (dig > 4 && pflag == 1 && isdigit(c))
34926048Sminshall 				pflag = 2;
35026048Sminshall 			if (pflag == 2) {
35126448Slepreau 				if (c != '\r' && c != ')')
35226048Sminshall 					*pt++ = c;
35326048Sminshall 				else {
35426048Sminshall 					*pt = '\0';
35526048Sminshall 					pflag = 3;
35626048Sminshall 				}
35726048Sminshall 			}
35826048Sminshall 			if (dig == 4 && c == '-') {
35926448Slepreau 				if (continuation)
36026048Sminshall 					code = 0;
36110296Ssam 				continuation++;
36226048Sminshall 			}
36310296Ssam 			if (n == 0)
36410296Ssam 				n = c;
36537229Skarels 			if (cp < &reply_string[sizeof(reply_string) - 1])
36637229Skarels 				*cp++ = c;
36710296Ssam 		}
36826048Sminshall 		if (verbose > 0 || verbose > -1 && n == '5') {
36926496Sminshall 			(void) putchar(c);
37011346Ssam 			(void) fflush (stdout);
37111346Ssam 		}
37210296Ssam 		if (continuation && code != originalcode) {
37310296Ssam 			if (originalcode == 0)
37410296Ssam 				originalcode = code;
37510296Ssam 			continue;
37610296Ssam 		}
37736935Skarels 		*cp = '\0';
37826448Slepreau 		if (n != '1')
37926048Sminshall 			cpend = 0;
38026048Sminshall 		(void) signal(SIGINT,oldintr);
38126448Slepreau 		if (code == 421 || originalcode == 421)
38226048Sminshall 			lostpeer();
38326448Slepreau 		if (abrtflag && oldintr != cmdabort && oldintr != SIG_IGN)
38426048Sminshall 			(*oldintr)();
38525907Smckusick 		return (n - '0');
38610296Ssam 	}
38710296Ssam }
38810296Ssam 
38926048Sminshall empty(mask, sec)
39027687Sminshall 	struct fd_set *mask;
39126048Sminshall 	int sec;
39226048Sminshall {
39326048Sminshall 	struct timeval t;
39426048Sminshall 
39526048Sminshall 	t.tv_sec = (long) sec;
39626048Sminshall 	t.tv_usec = 0;
39727687Sminshall 	return(select(32, mask, (struct fd_set *) 0, (struct fd_set *) 0, &t));
39826048Sminshall }
39926048Sminshall 
40010296Ssam jmp_buf	sendabort;
40110296Ssam 
402*40193Sbostic void
40310296Ssam abortsend()
40410296Ssam {
40510296Ssam 
40626048Sminshall 	mflag = 0;
40726048Sminshall 	abrtflag = 0;
40838133Srick 	printf("\nsend aborted\nwaiting for remote to finish abort\n");
40926048Sminshall 	(void) fflush(stdout);
41010296Ssam 	longjmp(sendabort, 1);
41110296Ssam }
41210296Ssam 
41336940Skarels #define HASHBYTES 1024
41436940Skarels 
41537225Skarels sendrequest(cmd, local, remote, printnames)
41610296Ssam 	char *cmd, *local, *remote;
41737225Skarels 	int printnames;
41810296Ssam {
419*40193Sbostic 	struct stat st;
420*40193Sbostic 	struct timeval start, stop;
421*40193Sbostic 	register int c, d;
42235659Sbostic 	FILE *fin, *dout = 0, *popen();
42338133Srick 	int (*closefunc)(), pclose(), fclose();
424*40193Sbostic 	sig_t oldintr, oldintp;
42536940Skarels 	long bytes = 0, hashbytes = HASHBYTES;
426*40193Sbostic 	char *lmode, buf[BUFSIZ], *bufp;
427*40193Sbostic 	void abortsend();
42810296Ssam 
42937225Skarels 	if (verbose && printnames) {
43037225Skarels 		if (local && *local != '-')
43137225Skarels 			printf("local: %s ", local);
43237225Skarels 		if (remote)
43337225Skarels 			printf("remote: %s\n", remote);
43437225Skarels 	}
43526048Sminshall 	if (proxy) {
43626048Sminshall 		proxtrans(cmd, local, remote);
43726048Sminshall 		return;
43826048Sminshall 	}
43938033Skarels 	if (curtype != type)
44038033Skarels 		changetype(type, 0);
44110296Ssam 	closefunc = NULL;
44226048Sminshall 	oldintr = NULL;
44326048Sminshall 	oldintp = NULL;
444*40193Sbostic 	lmode = "w";
44526048Sminshall 	if (setjmp(sendabort)) {
44626048Sminshall 		while (cpend) {
44726048Sminshall 			(void) getreply(0);
44826048Sminshall 		}
44926048Sminshall 		if (data >= 0) {
45026048Sminshall 			(void) close(data);
45126048Sminshall 			data = -1;
45226048Sminshall 		}
45326448Slepreau 		if (oldintr)
45426048Sminshall 			(void) signal(SIGINT,oldintr);
45526448Slepreau 		if (oldintp)
45626048Sminshall 			(void) signal(SIGPIPE,oldintp);
45726048Sminshall 		code = -1;
45826048Sminshall 		return;
45926048Sminshall 	}
46010296Ssam 	oldintr = signal(SIGINT, abortsend);
46110296Ssam 	if (strcmp(local, "-") == 0)
46210296Ssam 		fin = stdin;
46310296Ssam 	else if (*local == '|') {
46426048Sminshall 		oldintp = signal(SIGPIPE,SIG_IGN);
46535659Sbostic 		fin = popen(local + 1, "r");
46610296Ssam 		if (fin == NULL) {
46726048Sminshall 			perror(local + 1);
46826048Sminshall 			(void) signal(SIGINT, oldintr);
46926048Sminshall 			(void) signal(SIGPIPE, oldintp);
47026048Sminshall 			code = -1;
47126048Sminshall 			return;
47210296Ssam 		}
47335659Sbostic 		closefunc = pclose;
47410296Ssam 	} else {
47510296Ssam 		fin = fopen(local, "r");
47610296Ssam 		if (fin == NULL) {
47738202Srick 			fprintf(stderr, "local: %s: %s\n", local,
47838202Srick 				strerror(errno));
47926048Sminshall 			(void) signal(SIGINT, oldintr);
48026048Sminshall 			code = -1;
48126048Sminshall 			return;
48210296Ssam 		}
48310296Ssam 		closefunc = fclose;
48410296Ssam 		if (fstat(fileno(fin), &st) < 0 ||
48510296Ssam 		    (st.st_mode&S_IFMT) != S_IFREG) {
48626496Sminshall 			fprintf(stdout, "%s: not a plain file.\n", local);
48726048Sminshall 			(void) signal(SIGINT, oldintr);
48836935Skarels 			fclose(fin);
48926048Sminshall 			code = -1;
49026048Sminshall 			return;
49110296Ssam 		}
49210296Ssam 	}
49326048Sminshall 	if (initconn()) {
49426048Sminshall 		(void) signal(SIGINT, oldintr);
49526448Slepreau 		if (oldintp)
49626048Sminshall 			(void) signal(SIGPIPE, oldintp);
49726048Sminshall 		code = -1;
49836935Skarels 		if (closefunc != NULL)
49936935Skarels 			(*closefunc)(fin);
50026048Sminshall 		return;
50126048Sminshall 	}
50226448Slepreau 	if (setjmp(sendabort))
50326048Sminshall 		goto abort;
50436935Skarels 
50537225Skarels 	if (restart_point &&
50637225Skarels 	    (strcmp(cmd, "STOR") == 0 || strcmp(cmd, "APPE") == 0)) {
50737225Skarels 		if (fseek(fin, (long) restart_point, 0) < 0) {
50838202Srick 			fprintf(stderr, "local: %s: %s\n", local,
50938202Srick 				strerror(errno));
51037225Skarels 			restart_point = 0;
51137225Skarels 			if (closefunc != NULL)
51237225Skarels 				(*closefunc)(fin);
51337225Skarels 			return;
51437225Skarels 		}
51537225Skarels 		if (command("REST %ld", (long) restart_point)
51637225Skarels 			!= CONTINUE) {
51737225Skarels 			restart_point = 0;
51837225Skarels 			if (closefunc != NULL)
51937225Skarels 				(*closefunc)(fin);
52037225Skarels 			return;
52137225Skarels 		}
52237225Skarels 		restart_point = 0;
523*40193Sbostic 		lmode = "r+w";
52437225Skarels 	}
52510296Ssam 	if (remote) {
52626048Sminshall 		if (command("%s %s", cmd, remote) != PRELIM) {
52726048Sminshall 			(void) signal(SIGINT, oldintr);
52826448Slepreau 			if (oldintp)
52926048Sminshall 				(void) signal(SIGPIPE, oldintp);
53036935Skarels 			if (closefunc != NULL)
53136935Skarels 				(*closefunc)(fin);
53226048Sminshall 			return;
53326048Sminshall 		}
53410296Ssam 	} else
53526048Sminshall 		if (command("%s", cmd) != PRELIM) {
53626048Sminshall 			(void) signal(SIGINT, oldintr);
53726448Slepreau 			if (oldintp)
53826048Sminshall 				(void) signal(SIGPIPE, oldintp);
53936935Skarels 			if (closefunc != NULL)
54036935Skarels 				(*closefunc)(fin);
54126048Sminshall 			return;
54226048Sminshall 		}
543*40193Sbostic 	dout = dataconn(lmode);
54426448Slepreau 	if (dout == NULL)
54526048Sminshall 		goto abort;
54626496Sminshall 	(void) gettimeofday(&start, (struct timezone *)0);
54736935Skarels 	oldintp = signal(SIGPIPE, SIG_IGN);
54811219Ssam 	switch (type) {
54911219Ssam 
55011219Ssam 	case TYPE_I:
55111219Ssam 	case TYPE_L:
55211346Ssam 		errno = d = 0;
55336942Skarels 		while ((c = read(fileno(fin), buf, sizeof (buf))) > 0) {
55411219Ssam 			bytes += c;
55536942Skarels 			for (bufp = buf; c > 0; c -= d, bufp += d)
55636942Skarels 				if ((d = write(fileno(dout), bufp, c)) <= 0)
55736942Skarels 					break;
55811651Ssam 			if (hash) {
55936940Skarels 				while (bytes >= hashbytes) {
56036940Skarels 					(void) putchar('#');
56136940Skarels 					hashbytes += HASHBYTES;
56236940Skarels 				}
56326496Sminshall 				(void) fflush(stdout);
56411651Ssam 			}
56511219Ssam 		}
56613213Ssam 		if (hash && bytes > 0) {
56736940Skarels 			if (bytes < HASHBYTES)
56836940Skarels 				(void) putchar('#');
56926496Sminshall 			(void) putchar('\n');
57026496Sminshall 			(void) fflush(stdout);
57111651Ssam 		}
57211219Ssam 		if (c < 0)
57338202Srick 			fprintf(stderr, "local: %s: %s\n", local,
57438202Srick 				strerror(errno));
57536942Skarels 		if (d <= 0) {
57636942Skarels 			if (d == 0)
57736942Skarels 				fprintf(stderr, "netout: write returned 0?\n");
57836942Skarels 			else if (errno != EPIPE)
57936935Skarels 				perror("netout");
58036935Skarels 			bytes = -1;
58136935Skarels 		}
58211219Ssam 		break;
58311219Ssam 
58411219Ssam 	case TYPE_A:
58511219Ssam 		while ((c = getc(fin)) != EOF) {
58611219Ssam 			if (c == '\n') {
58711651Ssam 				while (hash && (bytes >= hashbytes)) {
58826496Sminshall 					(void) putchar('#');
58926496Sminshall 					(void) fflush(stdout);
59036940Skarels 					hashbytes += HASHBYTES;
59111651Ssam 				}
59211219Ssam 				if (ferror(dout))
59311219Ssam 					break;
59426496Sminshall 				(void) putc('\r', dout);
59511219Ssam 				bytes++;
59611219Ssam 			}
59726496Sminshall 			(void) putc(c, dout);
59811219Ssam 			bytes++;
59926048Sminshall 	/*		if (c == '\r') {			  	*/
60026496Sminshall 	/*		(void)	putc('\0', dout);  /* this violates rfc */
60126048Sminshall 	/*			bytes++;				*/
60226048Sminshall 	/*		}                          			*/
60311219Ssam 		}
60411651Ssam 		if (hash) {
60513213Ssam 			if (bytes < hashbytes)
60626496Sminshall 				(void) putchar('#');
60726496Sminshall 			(void) putchar('\n');
60826496Sminshall 			(void) fflush(stdout);
60911651Ssam 		}
61011219Ssam 		if (ferror(fin))
61138202Srick 			fprintf(stderr, "local: %s: %s\n", local,
61238202Srick 				strerror(errno));
61336935Skarels 		if (ferror(dout)) {
61436935Skarels 			if (errno != EPIPE)
61536935Skarels 				perror("netout");
61636935Skarels 			bytes = -1;
61736935Skarels 		}
61811219Ssam 		break;
61910296Ssam 	}
62026496Sminshall 	(void) gettimeofday(&stop, (struct timezone *)0);
62110296Ssam 	if (closefunc != NULL)
62226048Sminshall 		(*closefunc)(fin);
62310296Ssam 	(void) fclose(dout);
62426048Sminshall 	(void) getreply(0);
62526048Sminshall 	(void) signal(SIGINT, oldintr);
62636935Skarels 	if (oldintp)
62736935Skarels 		(void) signal(SIGPIPE, oldintp);
62835699Sbostic 	if (bytes > 0)
62937225Skarels 		ptransfer("sent", bytes, &start, &stop);
63010296Ssam 	return;
63126048Sminshall abort:
63226496Sminshall 	(void) gettimeofday(&stop, (struct timezone *)0);
63326048Sminshall 	(void) signal(SIGINT, oldintr);
63426448Slepreau 	if (oldintp)
63526048Sminshall 		(void) signal(SIGPIPE, oldintp);
63626048Sminshall 	if (!cpend) {
63726048Sminshall 		code = -1;
63826048Sminshall 		return;
63926048Sminshall 	}
64026048Sminshall 	if (data >= 0) {
64126048Sminshall 		(void) close(data);
64226048Sminshall 		data = -1;
64326048Sminshall 	}
64426448Slepreau 	if (dout)
64526048Sminshall 		(void) fclose(dout);
64626048Sminshall 	(void) getreply(0);
64726048Sminshall 	code = -1;
64810296Ssam 	if (closefunc != NULL && fin != NULL)
64926048Sminshall 		(*closefunc)(fin);
65035699Sbostic 	if (bytes > 0)
65137225Skarels 		ptransfer("sent", bytes, &start, &stop);
65210296Ssam }
65310296Ssam 
65410296Ssam jmp_buf	recvabort;
65510296Ssam 
656*40193Sbostic void
65710296Ssam abortrecv()
65810296Ssam {
65910296Ssam 
66026048Sminshall 	mflag = 0;
66126048Sminshall 	abrtflag = 0;
66238133Srick 	printf("\nreceive aborted\nwaiting for remote to finish abort\n");
66326048Sminshall 	(void) fflush(stdout);
66410296Ssam 	longjmp(recvabort, 1);
66510296Ssam }
66610296Ssam 
667*40193Sbostic recvrequest(cmd, local, remote, lmode, printnames)
668*40193Sbostic 	char *cmd, *local, *remote, *lmode;
66910296Ssam {
67035659Sbostic 	FILE *fout, *din = 0, *popen();
67138133Srick 	int (*closefunc)(), pclose(), fclose();
672*40193Sbostic 	sig_t oldintr, oldintp;
67338133Srick 	int is_retr, tcrflag, bare_lfs = 0;
67438133Srick 	char *gunique();
67538133Srick 	static int bufsize;
67636944Skarels 	static char *buf;
67736940Skarels 	long bytes = 0, hashbytes = HASHBYTES;
67811346Ssam 	register int c, d;
67910296Ssam 	struct timeval start, stop;
68036940Skarels 	struct stat st;
681*40193Sbostic 	off_t lseek();
682*40193Sbostic 	void abortrecv();
683*40193Sbostic 	char *malloc();
68410296Ssam 
68536935Skarels 	is_retr = strcmp(cmd, "RETR") == 0;
68637225Skarels 	if (is_retr && verbose && printnames) {
68737225Skarels 		if (local && *local != '-')
68837225Skarels 			printf("local: %s ", local);
68937225Skarels 		if (remote)
69037225Skarels 			printf("remote: %s\n", remote);
69137225Skarels 	}
69236935Skarels 	if (proxy && is_retr) {
69326048Sminshall 		proxtrans(cmd, local, remote);
69426048Sminshall 		return;
69526048Sminshall 	}
69610296Ssam 	closefunc = NULL;
69726048Sminshall 	oldintr = NULL;
69826048Sminshall 	oldintp = NULL;
69936935Skarels 	tcrflag = !crflag && is_retr;
70026048Sminshall 	if (setjmp(recvabort)) {
70126048Sminshall 		while (cpend) {
70226048Sminshall 			(void) getreply(0);
70326048Sminshall 		}
70426048Sminshall 		if (data >= 0) {
70526048Sminshall 			(void) close(data);
70626048Sminshall 			data = -1;
70726048Sminshall 		}
70826448Slepreau 		if (oldintr)
70926048Sminshall 			(void) signal(SIGINT, oldintr);
71026048Sminshall 		code = -1;
71126048Sminshall 		return;
71226048Sminshall 	}
71310296Ssam 	oldintr = signal(SIGINT, abortrecv);
71426048Sminshall 	if (strcmp(local, "-") && *local != '|') {
71510296Ssam 		if (access(local, 2) < 0) {
71626048Sminshall 			char *dir = rindex(local, '/');
71710296Ssam 
71826048Sminshall 			if (errno != ENOENT && errno != EACCES) {
71938202Srick 				fprintf(stderr, "local: %s: %s\n", local,
72038202Srick 					strerror(errno));
72126048Sminshall 				(void) signal(SIGINT, oldintr);
72226048Sminshall 				code = -1;
72326048Sminshall 				return;
72410296Ssam 			}
72526048Sminshall 			if (dir != NULL)
72626048Sminshall 				*dir = 0;
72726048Sminshall 			d = access(dir ? local : ".", 2);
72826048Sminshall 			if (dir != NULL)
72926048Sminshall 				*dir = '/';
73026048Sminshall 			if (d < 0) {
73138202Srick 				fprintf(stderr, "local: %s: %s\n", local,
73238202Srick 					strerror(errno));
73326048Sminshall 				(void) signal(SIGINT, oldintr);
73426048Sminshall 				code = -1;
73526048Sminshall 				return;
73626048Sminshall 			}
73726048Sminshall 			if (!runique && errno == EACCES &&
73836935Skarels 			    chmod(local, 0600) < 0) {
73938202Srick 				fprintf(stderr, "local: %s: %s\n", local,
74038202Srick 					strerror(errno));
74126048Sminshall 				(void) signal(SIGINT, oldintr);
74238202Srick 				(void) signal(SIGINT, oldintr);
74326048Sminshall 				code = -1;
74426048Sminshall 				return;
74526048Sminshall 			}
74626048Sminshall 			if (runique && errno == EACCES &&
74726048Sminshall 			   (local = gunique(local)) == NULL) {
74826048Sminshall 				(void) signal(SIGINT, oldintr);
74926048Sminshall 				code = -1;
75026048Sminshall 				return;
75126048Sminshall 			}
75210296Ssam 		}
75326048Sminshall 		else if (runique && (local = gunique(local)) == NULL) {
75426048Sminshall 			(void) signal(SIGINT, oldintr);
75526048Sminshall 			code = -1;
75626048Sminshall 			return;
75726048Sminshall 		}
75826048Sminshall 	}
75938033Skarels 	if (!is_retr) {
76038033Skarels 		if (curtype != TYPE_A)
76138033Skarels 			changetype(TYPE_A, 0);
76238033Skarels 	} else if (curtype != type)
76338033Skarels 		changetype(type, 0);
76426048Sminshall 	if (initconn()) {
76526048Sminshall 		(void) signal(SIGINT, oldintr);
76626048Sminshall 		code = -1;
76726048Sminshall 		return;
76826048Sminshall 	}
76926448Slepreau 	if (setjmp(recvabort))
77026048Sminshall 		goto abort;
77138033Skarels 	if (is_retr && restart_point &&
77238033Skarels 	    command("REST %ld", (long) restart_point) != CONTINUE)
77338033Skarels 		return;
77410296Ssam 	if (remote) {
77526048Sminshall 		if (command("%s %s", cmd, remote) != PRELIM) {
77626048Sminshall 			(void) signal(SIGINT, oldintr);
77726048Sminshall 			return;
77826048Sminshall 		}
77926048Sminshall 	} else {
78026048Sminshall 		if (command("%s", cmd) != PRELIM) {
78126048Sminshall 			(void) signal(SIGINT, oldintr);
78226048Sminshall 			return;
78326048Sminshall 		}
78426048Sminshall 	}
78526048Sminshall 	din = dataconn("r");
78626048Sminshall 	if (din == NULL)
78726048Sminshall 		goto abort;
78826448Slepreau 	if (strcmp(local, "-") == 0)
78910296Ssam 		fout = stdout;
79010296Ssam 	else if (*local == '|') {
79126048Sminshall 		oldintp = signal(SIGPIPE, SIG_IGN);
79235659Sbostic 		fout = popen(local + 1, "w");
79326048Sminshall 		if (fout == NULL) {
79426048Sminshall 			perror(local+1);
79526048Sminshall 			goto abort;
79626048Sminshall 		}
79735659Sbostic 		closefunc = pclose;
79836940Skarels 	} else {
799*40193Sbostic 		fout = fopen(local, lmode);
80026048Sminshall 		if (fout == NULL) {
80138202Srick 			fprintf(stderr, "local: %s: %s\n", local,
80238202Srick 				strerror(errno));
80326048Sminshall 			goto abort;
80426048Sminshall 		}
80510296Ssam 		closefunc = fclose;
80610296Ssam 	}
80736940Skarels 	if (fstat(fileno(fout), &st) < 0 || st.st_blksize == 0)
80836940Skarels 		st.st_blksize = BUFSIZ;
80936940Skarels 	if (st.st_blksize > bufsize) {
81036940Skarels 		if (buf)
81136940Skarels 			(void) free(buf);
81238133Srick 		buf = malloc((unsigned)st.st_blksize);
81336940Skarels 		if (buf == NULL) {
81436940Skarels 			perror("malloc");
81536944Skarels 			bufsize = 0;
81636940Skarels 			goto abort;
81736940Skarels 		}
81836940Skarels 		bufsize = st.st_blksize;
81936940Skarels 	}
82026496Sminshall 	(void) gettimeofday(&start, (struct timezone *)0);
82138033Skarels 	switch (curtype) {
82211219Ssam 
82311219Ssam 	case TYPE_I:
82411219Ssam 	case TYPE_L:
82537225Skarels 		if (restart_point &&
82637225Skarels 		    lseek(fileno(fout), (long) restart_point, L_SET) < 0) {
82738202Srick 			fprintf(stderr, "local: %s: %s\n", local,
82838202Srick 				strerror(errno));
82937225Skarels 			if (closefunc != NULL)
83037225Skarels 				(*closefunc)(fout);
83137225Skarels 			return;
83237225Skarels 		}
83311346Ssam 		errno = d = 0;
83436940Skarels 		while ((c = read(fileno(din), buf, bufsize)) > 0) {
83536944Skarels 			if ((d = write(fileno(fout), buf, c)) != c)
83611219Ssam 				break;
83711219Ssam 			bytes += c;
83811651Ssam 			if (hash) {
83936940Skarels 				while (bytes >= hashbytes) {
84036940Skarels 					(void) putchar('#');
84136940Skarels 					hashbytes += HASHBYTES;
84236940Skarels 				}
84326496Sminshall 				(void) fflush(stdout);
84411651Ssam 			}
84511219Ssam 		}
84613213Ssam 		if (hash && bytes > 0) {
84736940Skarels 			if (bytes < HASHBYTES)
84836940Skarels 				(void) putchar('#');
84926496Sminshall 			(void) putchar('\n');
85026496Sminshall 			(void) fflush(stdout);
85111651Ssam 		}
85236935Skarels 		if (c < 0) {
85336935Skarels 			if (errno != EPIPE)
85436935Skarels 				perror("netin");
85536935Skarels 			bytes = -1;
85636935Skarels 		}
85736942Skarels 		if (d < c) {
85836942Skarels 			if (d < 0)
85938202Srick 				fprintf(stderr, "local: %s: %s\n", local,
86038202Srick 					strerror(errno));
86136942Skarels 			else
86236942Skarels 				fprintf(stderr, "%s: short write\n", local);
86336942Skarels 		}
86411219Ssam 		break;
86511219Ssam 
86611219Ssam 	case TYPE_A:
86737225Skarels 		if (restart_point) {
868*40193Sbostic 			register int i, n, ch;
86937225Skarels 
87037225Skarels 			if (fseek(fout, 0L, L_SET) < 0)
87137225Skarels 				goto done;
87237225Skarels 			n = restart_point;
873*40193Sbostic 			for (i = 0; i++ < n;) {
874*40193Sbostic 				if ((ch = getc(fout)) == EOF)
87537225Skarels 					goto done;
876*40193Sbostic 				if (ch == '\n')
87737225Skarels 					i++;
87837225Skarels 			}
87937225Skarels 			if (fseek(fout, 0L, L_INCR) < 0) {
88037225Skarels done:
88138202Srick 				fprintf(stderr, "local: %s: %s\n", local,
88238202Srick 					strerror(errno));
88337225Skarels 				if (closefunc != NULL)
88437225Skarels 					(*closefunc)(fout);
88537225Skarels 				return;
88637225Skarels 			}
88737225Skarels 		}
88811219Ssam 		while ((c = getc(din)) != EOF) {
88938133Srick 			if (c == '\n')
89038133Srick 				bare_lfs++;
89127749Sminshall 			while (c == '\r') {
89211651Ssam 				while (hash && (bytes >= hashbytes)) {
89326496Sminshall 					(void) putchar('#');
89426496Sminshall 					(void) fflush(stdout);
89536940Skarels 					hashbytes += HASHBYTES;
89611651Ssam 				}
89710296Ssam 				bytes++;
89826048Sminshall 				if ((c = getc(din)) != '\n' || tcrflag) {
89936940Skarels 					if (ferror(fout))
90036940Skarels 						goto break2;
90136940Skarels 					(void) putc('\r', fout);
90236942Skarels 					if (c == '\0') {
90336942Skarels 						bytes++;
90436940Skarels 						goto contin2;
90536942Skarels 					}
90636942Skarels 					if (c == EOF)
90736942Skarels 						goto contin2;
90811219Ssam 				}
90911219Ssam 			}
91036940Skarels 			(void) putc(c, fout);
91111219Ssam 			bytes++;
91236940Skarels 	contin2:	;
91310296Ssam 		}
91436940Skarels break2:
91538133Srick 		if (bare_lfs) {
91638133Srick 			printf("WARNING! %d bare linefeeds received in ASCII mode\n", bare_lfs);
91738133Srick 			printf("File may not have transferred correctly.\n");
91838133Srick 		}
91911651Ssam 		if (hash) {
92013213Ssam 			if (bytes < hashbytes)
92126496Sminshall 				(void) putchar('#');
92226496Sminshall 			(void) putchar('\n');
92326496Sminshall 			(void) fflush(stdout);
92411651Ssam 		}
92536944Skarels 		if (ferror(din)) {
92636935Skarels 			if (errno != EPIPE)
92736944Skarels 				perror("netin");
92836935Skarels 			bytes = -1;
92936935Skarels 		}
93036940Skarels 		if (ferror(fout))
93138202Srick 			fprintf(stderr, "local: %s: %s\n", local,
93238202Srick 				strerror(errno));
93311219Ssam 		break;
93410296Ssam 	}
93526448Slepreau 	if (closefunc != NULL)
93626048Sminshall 		(*closefunc)(fout);
93726496Sminshall 	(void) signal(SIGINT, oldintr);
93826448Slepreau 	if (oldintp)
93926048Sminshall 		(void) signal(SIGPIPE, oldintp);
94026496Sminshall 	(void) gettimeofday(&stop, (struct timezone *)0);
94110296Ssam 	(void) fclose(din);
94226048Sminshall 	(void) getreply(0);
94336935Skarels 	if (bytes > 0 && is_retr)
94437225Skarels 		ptransfer("received", bytes, &start, &stop);
94526048Sminshall 	return;
94626048Sminshall abort:
94726048Sminshall 
94827687Sminshall /* abort using RFC959 recommended IP,SYNC sequence  */
94926048Sminshall 
95026496Sminshall 	(void) gettimeofday(&stop, (struct timezone *)0);
95126448Slepreau 	if (oldintp)
95226048Sminshall 		(void) signal(SIGPIPE, oldintr);
95338133Srick 	(void) signal(SIGINT, SIG_IGN);
95426048Sminshall 	if (!cpend) {
95526048Sminshall 		code = -1;
95638133Srick 		(void) signal(SIGINT, oldintr);
95726048Sminshall 		return;
95826048Sminshall 	}
95926048Sminshall 
96038133Srick 	abort_remote(din);
96126048Sminshall 	code = -1;
96226048Sminshall 	if (data >= 0) {
96326048Sminshall 		(void) close(data);
96426048Sminshall 		data = -1;
96526048Sminshall 	}
96626448Slepreau 	if (closefunc != NULL && fout != NULL)
96726048Sminshall 		(*closefunc)(fout);
96826448Slepreau 	if (din)
96926048Sminshall 		(void) fclose(din);
97035699Sbostic 	if (bytes > 0)
97137225Skarels 		ptransfer("received", bytes, &start, &stop);
97238133Srick 	(void) signal(SIGINT, oldintr);
97310296Ssam }
97410296Ssam 
97510296Ssam /*
976*40193Sbostic  * Need to start a listen on the data channel before we send the command,
977*40193Sbostic  * otherwise the server's connect may fail.
97810296Ssam  */
97910296Ssam initconn()
98010296Ssam {
98110296Ssam 	register char *p, *a;
98226048Sminshall 	int result, len, tmpno = 0;
98326993Skarels 	int on = 1;
98410296Ssam 
98511651Ssam noport:
98610296Ssam 	data_addr = myctladdr;
98711651Ssam 	if (sendport)
98811651Ssam 		data_addr.sin_port = 0;	/* let system pick one */
98911651Ssam 	if (data != -1)
99038133Srick 		(void) close(data);
99118287Sralph 	data = socket(AF_INET, SOCK_STREAM, 0);
99210296Ssam 	if (data < 0) {
99310296Ssam 		perror("ftp: socket");
99426448Slepreau 		if (tmpno)
99526048Sminshall 			sendport = 1;
99610296Ssam 		return (1);
99710296Ssam 	}
99812397Ssam 	if (!sendport)
99927687Sminshall 		if (setsockopt(data, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof (on)) < 0) {
100033224Sbostic 			perror("ftp: setsockopt (reuse address)");
100112397Ssam 			goto bad;
100212397Ssam 		}
100326496Sminshall 	if (bind(data, (struct sockaddr *)&data_addr, sizeof (data_addr)) < 0) {
100410296Ssam 		perror("ftp: bind");
100510296Ssam 		goto bad;
100610296Ssam 	}
100710296Ssam 	if (options & SO_DEBUG &&
100827687Sminshall 	    setsockopt(data, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof (on)) < 0)
100910296Ssam 		perror("ftp: setsockopt (ignored)");
101011627Ssam 	len = sizeof (data_addr);
101138133Srick 	if (getsockname(data, (struct sockaddr *)&data_addr, &len) < 0) {
101211627Ssam 		perror("ftp: getsockname");
101310296Ssam 		goto bad;
101410296Ssam 	}
101526448Slepreau 	if (listen(data, 1) < 0)
101610296Ssam 		perror("ftp: listen");
101711651Ssam 	if (sendport) {
101811651Ssam 		a = (char *)&data_addr.sin_addr;
101911651Ssam 		p = (char *)&data_addr.sin_port;
102010296Ssam #define	UC(b)	(((int)b)&0xff)
102111651Ssam 		result =
102211651Ssam 		    command("PORT %d,%d,%d,%d,%d,%d",
102311651Ssam 		      UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
102411651Ssam 		      UC(p[0]), UC(p[1]));
102511651Ssam 		if (result == ERROR && sendport == -1) {
102611651Ssam 			sendport = 0;
102726048Sminshall 			tmpno = 1;
102811651Ssam 			goto noport;
102911651Ssam 		}
103011651Ssam 		return (result != COMPLETE);
103111651Ssam 	}
103226448Slepreau 	if (tmpno)
103326048Sminshall 		sendport = 1;
103411651Ssam 	return (0);
103510296Ssam bad:
103610296Ssam 	(void) close(data), data = -1;
103726448Slepreau 	if (tmpno)
103826048Sminshall 		sendport = 1;
103910296Ssam 	return (1);
104010296Ssam }
104110296Ssam 
104210296Ssam FILE *
1043*40193Sbostic dataconn(lmode)
1044*40193Sbostic 	char *lmode;
104510296Ssam {
104610296Ssam 	struct sockaddr_in from;
104710296Ssam 	int s, fromlen = sizeof (from);
104810296Ssam 
104926496Sminshall 	s = accept(data, (struct sockaddr *) &from, &fromlen);
105010296Ssam 	if (s < 0) {
105110296Ssam 		perror("ftp: accept");
105210296Ssam 		(void) close(data), data = -1;
105310296Ssam 		return (NULL);
105410296Ssam 	}
105510296Ssam 	(void) close(data);
105610296Ssam 	data = s;
1057*40193Sbostic 	return (fdopen(data, lmode));
105810296Ssam }
105910296Ssam 
106037225Skarels ptransfer(direction, bytes, t0, t1)
106137225Skarels 	char *direction;
106211651Ssam 	long bytes;
106310296Ssam 	struct timeval *t0, *t1;
106410296Ssam {
106510296Ssam 	struct timeval td;
106616437Sleres 	float s, bs;
106710296Ssam 
106835699Sbostic 	if (verbose) {
106935699Sbostic 		tvsub(&td, t1, t0);
107035699Sbostic 		s = td.tv_sec + (td.tv_usec / 1000000.);
107110296Ssam #define	nz(x)	((x) == 0 ? 1 : (x))
107235699Sbostic 		bs = bytes / nz(s);
107335699Sbostic 		printf("%ld bytes %s in %.2g seconds (%.2g Kbytes/s)\n",
107435699Sbostic 		    bytes, direction, s, bs / 1024.);
107535699Sbostic 	}
107610296Ssam }
107710296Ssam 
107826496Sminshall /*tvadd(tsum, t0)
107910296Ssam 	struct timeval *tsum, *t0;
108010296Ssam {
108110296Ssam 
108210296Ssam 	tsum->tv_sec += t0->tv_sec;
108310296Ssam 	tsum->tv_usec += t0->tv_usec;
108410296Ssam 	if (tsum->tv_usec > 1000000)
108510296Ssam 		tsum->tv_sec++, tsum->tv_usec -= 1000000;
108626496Sminshall } */
108710296Ssam 
108810296Ssam tvsub(tdiff, t1, t0)
108910296Ssam 	struct timeval *tdiff, *t1, *t0;
109010296Ssam {
109110296Ssam 
109210296Ssam 	tdiff->tv_sec = t1->tv_sec - t0->tv_sec;
109310296Ssam 	tdiff->tv_usec = t1->tv_usec - t0->tv_usec;
109410296Ssam 	if (tdiff->tv_usec < 0)
109510296Ssam 		tdiff->tv_sec--, tdiff->tv_usec += 1000000;
109610296Ssam }
109726048Sminshall 
1098*40193Sbostic void
109926048Sminshall psabort()
110026048Sminshall {
110126048Sminshall 	extern int abrtflag;
110226048Sminshall 
110326048Sminshall 	abrtflag++;
110426048Sminshall }
110526048Sminshall 
110626048Sminshall pswitch(flag)
110726048Sminshall 	int flag;
110826048Sminshall {
110926048Sminshall 	extern int proxy, abrtflag;
1110*40193Sbostic 	sig_t oldintr;
111126048Sminshall 	static struct comvars {
111226048Sminshall 		int connect;
111328469Skarels 		char name[MAXHOSTNAMELEN];
111426048Sminshall 		struct sockaddr_in mctl;
111526048Sminshall 		struct sockaddr_in hctl;
111626048Sminshall 		FILE *in;
111726048Sminshall 		FILE *out;
111826048Sminshall 		int tpe;
111938033Skarels 		int curtpe;
112026048Sminshall 		int cpnd;
112126048Sminshall 		int sunqe;
112226048Sminshall 		int runqe;
112326048Sminshall 		int mcse;
112426048Sminshall 		int ntflg;
112526048Sminshall 		char nti[17];
112626048Sminshall 		char nto[17];
112726048Sminshall 		int mapflg;
112826048Sminshall 		char mi[MAXPATHLEN];
112926048Sminshall 		char mo[MAXPATHLEN];
113038033Skarels 	} proxstruct, tmpstruct;
113126048Sminshall 	struct comvars *ip, *op;
113226048Sminshall 
113326048Sminshall 	abrtflag = 0;
113426048Sminshall 	oldintr = signal(SIGINT, psabort);
113526048Sminshall 	if (flag) {
113626448Slepreau 		if (proxy)
113726048Sminshall 			return;
113826048Sminshall 		ip = &tmpstruct;
113926048Sminshall 		op = &proxstruct;
114026048Sminshall 		proxy++;
114138033Skarels 	} else {
114226448Slepreau 		if (!proxy)
114326048Sminshall 			return;
114426048Sminshall 		ip = &proxstruct;
114526048Sminshall 		op = &tmpstruct;
114626048Sminshall 		proxy = 0;
114726048Sminshall 	}
114826048Sminshall 	ip->connect = connected;
114926048Sminshall 	connected = op->connect;
115028469Skarels 	if (hostname) {
115128469Skarels 		(void) strncpy(ip->name, hostname, sizeof(ip->name) - 1);
115228469Skarels 		ip->name[strlen(ip->name)] = '\0';
115328469Skarels 	} else
115428469Skarels 		ip->name[0] = 0;
115526048Sminshall 	hostname = op->name;
115626048Sminshall 	ip->hctl = hisctladdr;
115726048Sminshall 	hisctladdr = op->hctl;
115826048Sminshall 	ip->mctl = myctladdr;
115926048Sminshall 	myctladdr = op->mctl;
116026048Sminshall 	ip->in = cin;
116126048Sminshall 	cin = op->in;
116226048Sminshall 	ip->out = cout;
116326048Sminshall 	cout = op->out;
116426048Sminshall 	ip->tpe = type;
116526048Sminshall 	type = op->tpe;
116638033Skarels 	ip->curtpe = curtype;
116738033Skarels 	curtype = op->curtpe;
116826048Sminshall 	ip->cpnd = cpend;
116926048Sminshall 	cpend = op->cpnd;
117026048Sminshall 	ip->sunqe = sunique;
117126048Sminshall 	sunique = op->sunqe;
117226048Sminshall 	ip->runqe = runique;
117326048Sminshall 	runique = op->runqe;
117426048Sminshall 	ip->mcse = mcase;
117526048Sminshall 	mcase = op->mcse;
117626048Sminshall 	ip->ntflg = ntflag;
117726048Sminshall 	ntflag = op->ntflg;
117826496Sminshall 	(void) strncpy(ip->nti, ntin, 16);
117926048Sminshall 	(ip->nti)[strlen(ip->nti)] = '\0';
118026496Sminshall 	(void) strcpy(ntin, op->nti);
118126496Sminshall 	(void) strncpy(ip->nto, ntout, 16);
118226048Sminshall 	(ip->nto)[strlen(ip->nto)] = '\0';
118326496Sminshall 	(void) strcpy(ntout, op->nto);
118426048Sminshall 	ip->mapflg = mapflag;
118526048Sminshall 	mapflag = op->mapflg;
118626496Sminshall 	(void) strncpy(ip->mi, mapin, MAXPATHLEN - 1);
118726048Sminshall 	(ip->mi)[strlen(ip->mi)] = '\0';
118826496Sminshall 	(void) strcpy(mapin, op->mi);
118926496Sminshall 	(void) strncpy(ip->mo, mapout, MAXPATHLEN - 1);
119026048Sminshall 	(ip->mo)[strlen(ip->mo)] = '\0';
119126496Sminshall 	(void) strcpy(mapout, op->mo);
119226048Sminshall 	(void) signal(SIGINT, oldintr);
119326048Sminshall 	if (abrtflag) {
119426048Sminshall 		abrtflag = 0;
119526048Sminshall 		(*oldintr)();
119626448Slepreau 	}
119726048Sminshall }
119826048Sminshall 
119926048Sminshall jmp_buf ptabort;
120026048Sminshall int ptabflg;
120126048Sminshall 
1202*40193Sbostic void
120326048Sminshall abortpt()
120426048Sminshall {
120526048Sminshall 	printf("\n");
120626496Sminshall 	(void) fflush(stdout);
120726048Sminshall 	ptabflg++;
120826048Sminshall 	mflag = 0;
120926048Sminshall 	abrtflag = 0;
121026048Sminshall 	longjmp(ptabort, 1);
121126048Sminshall }
121226048Sminshall 
121326048Sminshall proxtrans(cmd, local, remote)
121426048Sminshall 	char *cmd, *local, *remote;
121526048Sminshall {
1216*40193Sbostic 	sig_t oldintr;
121738133Srick 	int secndflag = 0, prox_type, nfnd;
121826048Sminshall 	extern jmp_buf ptabort;
121926048Sminshall 	char *cmd2;
122026496Sminshall 	struct fd_set mask;
1221*40193Sbostic 	void abortpt();
122226048Sminshall 
122326448Slepreau 	if (strcmp(cmd, "RETR"))
122426048Sminshall 		cmd2 = "RETR";
122526448Slepreau 	else
122626048Sminshall 		cmd2 = runique ? "STOU" : "STOR";
122738033Skarels 	if ((prox_type = type) == 0) {
122838033Skarels 		if (unix_server && unix_proxy)
122938033Skarels 			prox_type = TYPE_I;
123038033Skarels 		else
123138033Skarels 			prox_type = TYPE_A;
123238033Skarels 	}
123338033Skarels 	if (curtype != prox_type)
123438033Skarels 		changetype(prox_type, 1);
123526048Sminshall 	if (command("PASV") != COMPLETE) {
123638033Skarels 		printf("proxy server does not support third party transfers.\n");
123726048Sminshall 		return;
123826048Sminshall 	}
123926048Sminshall 	pswitch(0);
124026048Sminshall 	if (!connected) {
124126048Sminshall 		printf("No primary connection\n");
124226048Sminshall 		pswitch(1);
124326048Sminshall 		code = -1;
124426048Sminshall 		return;
124526048Sminshall 	}
124638033Skarels 	if (curtype != prox_type)
124738033Skarels 		changetype(prox_type, 1);
124826048Sminshall 	if (command("PORT %s", pasv) != COMPLETE) {
124926048Sminshall 		pswitch(1);
125026048Sminshall 		return;
125126048Sminshall 	}
125226448Slepreau 	if (setjmp(ptabort))
125326048Sminshall 		goto abort;
125426048Sminshall 	oldintr = signal(SIGINT, abortpt);
125526048Sminshall 	if (command("%s %s", cmd, remote) != PRELIM) {
125626048Sminshall 		(void) signal(SIGINT, oldintr);
125726048Sminshall 		pswitch(1);
125826048Sminshall 		return;
125926048Sminshall 	}
126026048Sminshall 	sleep(2);
126126048Sminshall 	pswitch(1);
126226048Sminshall 	secndflag++;
126326448Slepreau 	if (command("%s %s", cmd2, local) != PRELIM)
126426048Sminshall 		goto abort;
126526048Sminshall 	ptflag++;
126626048Sminshall 	(void) getreply(0);
126726048Sminshall 	pswitch(0);
126826048Sminshall 	(void) getreply(0);
126926048Sminshall 	(void) signal(SIGINT, oldintr);
127026048Sminshall 	pswitch(1);
127126048Sminshall 	ptflag = 0;
127226048Sminshall 	printf("local: %s remote: %s\n", local, remote);
127326048Sminshall 	return;
127426048Sminshall abort:
127526048Sminshall 	(void) signal(SIGINT, SIG_IGN);
127626048Sminshall 	ptflag = 0;
127726448Slepreau 	if (strcmp(cmd, "RETR") && !proxy)
127826048Sminshall 		pswitch(1);
127926448Slepreau 	else if (!strcmp(cmd, "RETR") && proxy)
128026048Sminshall 		pswitch(0);
128126048Sminshall 	if (!cpend && !secndflag) {  /* only here if cmd = "STOR" (proxy=1) */
128226048Sminshall 		if (command("%s %s", cmd2, local) != PRELIM) {
128326048Sminshall 			pswitch(0);
128438133Srick 			if (cpend)
128538133Srick 				abort_remote((FILE *) NULL);
128626048Sminshall 		}
128726048Sminshall 		pswitch(1);
128826448Slepreau 		if (ptabflg)
128926048Sminshall 			code = -1;
129026048Sminshall 		(void) signal(SIGINT, oldintr);
129126048Sminshall 		return;
129226048Sminshall 	}
129338133Srick 	if (cpend)
129438133Srick 		abort_remote((FILE *) NULL);
129526048Sminshall 	pswitch(!proxy);
129626048Sminshall 	if (!cpend && !secndflag) {  /* only if cmd = "RETR" (proxy=1) */
129726048Sminshall 		if (command("%s %s", cmd2, local) != PRELIM) {
129826048Sminshall 			pswitch(0);
129938133Srick 			if (cpend)
130038133Srick 				abort_remote((FILE *) NULL);
130126048Sminshall 			pswitch(1);
130226448Slepreau 			if (ptabflg)
130326048Sminshall 				code = -1;
130426048Sminshall 			(void) signal(SIGINT, oldintr);
130526048Sminshall 			return;
130626048Sminshall 		}
130726048Sminshall 	}
130838133Srick 	if (cpend)
130938133Srick 		abort_remote((FILE *) NULL);
131026048Sminshall 	pswitch(!proxy);
131126048Sminshall 	if (cpend) {
131227687Sminshall 		FD_ZERO(&mask);
131326496Sminshall 		FD_SET(fileno(cin), &mask);
131438133Srick 		if ((nfnd = empty(&mask, 10)) <= 0) {
131527687Sminshall 			if (nfnd < 0) {
131627687Sminshall 				perror("abort");
131727687Sminshall 			}
131826448Slepreau 			if (ptabflg)
131926048Sminshall 				code = -1;
132026048Sminshall 			lostpeer();
132126048Sminshall 		}
132226048Sminshall 		(void) getreply(0);
132326048Sminshall 		(void) getreply(0);
132426048Sminshall 	}
132526448Slepreau 	if (proxy)
132626048Sminshall 		pswitch(0);
132726048Sminshall 	pswitch(1);
132826448Slepreau 	if (ptabflg)
132926048Sminshall 		code = -1;
133026048Sminshall 	(void) signal(SIGINT, oldintr);
133126048Sminshall }
133226048Sminshall 
133326048Sminshall reset()
133426048Sminshall {
133526496Sminshall 	struct fd_set mask;
133626496Sminshall 	int nfnd = 1;
133726048Sminshall 
133827687Sminshall 	FD_ZERO(&mask);
133930946Scsvsj 	while (nfnd > 0) {
134026496Sminshall 		FD_SET(fileno(cin), &mask);
134127687Sminshall 		if ((nfnd = empty(&mask,0)) < 0) {
134226048Sminshall 			perror("reset");
134326048Sminshall 			code = -1;
134426048Sminshall 			lostpeer();
134526048Sminshall 		}
134627687Sminshall 		else if (nfnd) {
134726048Sminshall 			(void) getreply(0);
134826496Sminshall 		}
134926048Sminshall 	}
135026048Sminshall }
135126048Sminshall 
135226048Sminshall char *
135326048Sminshall gunique(local)
135426048Sminshall 	char *local;
135526048Sminshall {
135626048Sminshall 	static char new[MAXPATHLEN];
135726048Sminshall 	char *cp = rindex(local, '/');
135826048Sminshall 	int d, count=0;
135926048Sminshall 	char ext = '1';
136026048Sminshall 
136126448Slepreau 	if (cp)
136226048Sminshall 		*cp = '\0';
136326048Sminshall 	d = access(cp ? local : ".", 2);
136426448Slepreau 	if (cp)
136526048Sminshall 		*cp = '/';
136626048Sminshall 	if (d < 0) {
136738202Srick 		fprintf(stderr, "local: %s: %s\n", local, strerror(errno));
136826048Sminshall 		return((char *) 0);
136926048Sminshall 	}
137026048Sminshall 	(void) strcpy(new, local);
137126048Sminshall 	cp = new + strlen(new);
137226048Sminshall 	*cp++ = '.';
137326048Sminshall 	while (!d) {
137426048Sminshall 		if (++count == 100) {
137526048Sminshall 			printf("runique: can't find unique file name.\n");
137626048Sminshall 			return((char *) 0);
137726048Sminshall 		}
137826048Sminshall 		*cp++ = ext;
137926048Sminshall 		*cp = '\0';
138026448Slepreau 		if (ext == '9')
138126048Sminshall 			ext = '0';
138226448Slepreau 		else
138326048Sminshall 			ext++;
138426448Slepreau 		if ((d = access(new, 0)) < 0)
138526048Sminshall 			break;
138626448Slepreau 		if (ext != '0')
138726048Sminshall 			cp--;
138826448Slepreau 		else if (*(cp - 2) == '.')
138926048Sminshall 			*(cp - 1) = '1';
139026048Sminshall 		else {
139126048Sminshall 			*(cp - 2) = *(cp - 2) + 1;
139226048Sminshall 			cp--;
139326048Sminshall 		}
139426048Sminshall 	}
139526048Sminshall 	return(new);
139626048Sminshall }
139738133Srick 
139838133Srick abort_remote(din)
139938133Srick FILE *din;
140038133Srick {
140138133Srick 	char buf[BUFSIZ];
140238133Srick 	int nfnd;
140338133Srick 	struct fd_set mask;
140438133Srick 
140538133Srick 	/*
140638133Srick 	 * send IAC in urgent mode instead of DM because 4.3BSD places oob mark
140738133Srick 	 * after urgent byte rather than before as is protocol now
140838133Srick 	 */
140938133Srick 	sprintf(buf, "%c%c%c", IAC, IP, IAC);
141038133Srick 	if (send(fileno(cout), buf, 3, MSG_OOB) != 3)
141138133Srick 		perror("abort");
141238133Srick 	fprintf(cout,"%cABOR\r\n", DM);
141338133Srick 	(void) fflush(cout);
141438133Srick 	FD_ZERO(&mask);
141538133Srick 	FD_SET(fileno(cin), &mask);
141638133Srick 	if (din) {
141738133Srick 		FD_SET(fileno(din), &mask);
141838133Srick 	}
141938133Srick 	if ((nfnd = empty(&mask, 10)) <= 0) {
142038133Srick 		if (nfnd < 0) {
142138133Srick 			perror("abort");
142238133Srick 		}
142338133Srick 		if (ptabflg)
142438133Srick 			code = -1;
142538133Srick 		lostpeer();
142638133Srick 	}
142738133Srick 	if (din && FD_ISSET(fileno(din), &mask)) {
142838133Srick 		while (read(fileno(din), buf, BUFSIZ) > 0)
142938133Srick 			/* LOOP */;
143038133Srick 	}
143138133Srick 	if (getreply(0) == ERROR && code == 552) {
143238133Srick 		/* 552 needed for nic style abort */
143338133Srick 		(void) getreply(0);
143438133Srick 	}
143538133Srick 	(void) getreply(0);
143638133Srick }
1437