Home | History | Annotate | Line # | Download | only in ftp
ftp.c revision 1.69
      1 /*	$NetBSD: ftp.c,v 1.69 1999/09/28 07:51:06 lukem Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1997 and 1998 WIDE Project.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the project nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Copyright (c) 1985, 1989, 1993, 1994
     34  *	The Regents of the University of California.  All rights reserved.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions
     38  * are met:
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  * 3. All advertising materials mentioning features or use of this software
     45  *    must display the following acknowledgement:
     46  *	This product includes software developed by the University of
     47  *	California, Berkeley and its contributors.
     48  * 4. Neither the name of the University nor the names of its contributors
     49  *    may be used to endorse or promote products derived from this software
     50  *    without specific prior written permission.
     51  *
     52  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     54  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     55  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     56  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     57  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     58  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     59  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     60  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     61  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     62  * SUCH DAMAGE.
     63  */
     64 
     65 #include <sys/cdefs.h>
     66 #ifndef lint
     67 #if 0
     68 static char sccsid[] = "@(#)ftp.c	8.6 (Berkeley) 10/27/94";
     69 #else
     70 __RCSID("$NetBSD: ftp.c,v 1.69 1999/09/28 07:51:06 lukem Exp $");
     71 #endif
     72 #endif /* not lint */
     73 
     74 #include <sys/types.h>
     75 #include <sys/stat.h>
     76 #include <sys/socket.h>
     77 #include <sys/time.h>
     78 
     79 #include <netinet/in.h>
     80 #include <netinet/in_systm.h>
     81 #include <netinet/ip.h>
     82 #include <arpa/inet.h>
     83 #include <arpa/ftp.h>
     84 #include <arpa/telnet.h>
     85 
     86 #include <ctype.h>
     87 #include <err.h>
     88 #include <errno.h>
     89 #include <netdb.h>
     90 #include <stdio.h>
     91 #include <stdlib.h>
     92 #include <string.h>
     93 #include <time.h>
     94 #include <unistd.h>
     95 #ifdef __STDC__
     96 #include <stdarg.h>
     97 #else
     98 #include <varargs.h>
     99 #endif
    100 #ifndef __USE_SELECT
    101 #include <poll.h>
    102 #endif
    103 
    104 #include "ftp_var.h"
    105 
    106 extern int h_errno;
    107 
    108 int	data = -1;
    109 int	abrtflag = 0;
    110 jmp_buf	ptabort;
    111 int	ptabflg;
    112 int	ptflag = 0;
    113 off_t	restart_point = 0;
    114 
    115 static int empty __P((FILE *, FILE *, int));
    116 
    117 union sockunion {
    118 	struct sockinet {
    119 #ifdef BSD4_4
    120 		u_char si_len;
    121 		u_char si_family;
    122 #else
    123 		u_short si_family;
    124 #endif
    125 		u_short si_port;
    126 #ifndef BSD4_4
    127 		u_char  si_pad[
    128 #ifdef INET6
    129 				sizeof(struct sockaddr_in6)
    130 #else
    131 				sizeof(struct sockaddr_in)
    132 #endif
    133 				- sizeof(u_int)];
    134 		u_char	si_len;
    135 #endif
    136 	} su_si;
    137 	struct sockaddr_in  su_sin;
    138 #ifdef INET6
    139 	struct sockaddr_in6 su_sin6;
    140 #endif
    141 };
    142 
    143 #define su_len		su_si.si_len
    144 #define su_family	su_si.si_family
    145 #define su_port		su_si.si_port
    146 
    147 union sockunion myctladdr, hisctladdr, data_addr;
    148 
    149 FILE	*cin, *cout;
    150 
    151 char *
    152 hookup(host, port)
    153 	char *host;
    154 	char *port;
    155 {
    156 	int s = -1, len, error;
    157 #ifdef NI_NUMERICHOST
    158 	struct addrinfo hints, *res, *res0;
    159 	char hbuf[MAXHOSTNAMELEN];
    160 #else
    161 	struct hostent *hp = NULL;
    162 	struct servent *sp = NULL;
    163 	char **ptr;
    164 	struct sockaddr_in sin;
    165 #endif
    166 	static char hostnamebuf[MAXHOSTNAMELEN];
    167 	char *cause = "unknown";
    168 	int family;
    169 
    170 #ifdef NI_NUMERICHOST
    171 	memset((char *)&hisctladdr, 0, sizeof (hisctladdr));
    172 	memset(&hints, 0, sizeof(hints));
    173 	hints.ai_flags = AI_CANONNAME;
    174 	hints.ai_family = AF_UNSPEC;
    175 	hints.ai_socktype = SOCK_STREAM;
    176 	hints.ai_protocol = 0;
    177 	error = getaddrinfo(host, port, &hints, &res0);
    178 	if (error) {
    179 		warnx(gai_strerror(error));
    180 		code = -1;
    181 		return (0);
    182 	}
    183 
    184 	if (res0->ai_canonname)
    185 		(void)strlcpy(hostnamebuf, res0->ai_canonname,
    186 		    sizeof(hostnamebuf));
    187 	else
    188 		(void)strlcpy(hostnamebuf, host, sizeof(hostnamebuf));
    189 	hostname = hostnamebuf;
    190 
    191 	for (res = res0; res; res = res->ai_next) {
    192 #if 0	/*old behavior*/
    193 		if (res != res0)	/* not on the first address */
    194 #else
    195 		if (res0->ai_next)	/* if we have multiple possibilities */
    196 #endif
    197 		{
    198 			getnameinfo(res->ai_addr, res->ai_addrlen,
    199 				hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST);
    200 			fprintf(ttyout, "Trying %s...\n", hbuf);
    201 		}
    202 		s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
    203 		if (s < 0) {
    204 			cause = "socket";
    205 			continue;
    206 		}
    207 		while ((error = xconnect(s, res->ai_addr, res->ai_addrlen)) < 0
    208 				&& errno == EINTR) {
    209 			;
    210 		}
    211 		if (error) {
    212 			/* this "if" clause is to prevent print warning twice */
    213 			if (res->ai_next) {
    214 				getnameinfo(res->ai_addr, res->ai_addrlen,
    215 					hbuf, sizeof(hbuf), NULL, 0,
    216 					NI_NUMERICHOST);
    217 				warn("connect to address %s", hbuf);
    218 			}
    219 			cause = "connect";
    220 			close(s);
    221 			s = -1;
    222 			continue;
    223 		}
    224 
    225 		/* finally we got one */
    226 		break;
    227 	}
    228 	if (s < 0) {
    229 		warn(cause);
    230 		code = -1;
    231 		freeaddrinfo(res0);
    232 		return 0;
    233 	}
    234 	memcpy(&hisctladdr, res->ai_addr, res->ai_addrlen);
    235 	len = res->ai_addrlen;
    236 	freeaddrinfo(res0);
    237 	res0 = res = NULL;
    238 	family = hisctladdr.su_family;
    239 #else
    240 	memset(&sin, 0, sizeof(sin));
    241 	sin.sin_family = AF_INET;
    242 	if ((hp = gethostbyname(host)) == NULL) {
    243 		warnx("%s: %s", host, hstrerror(h_errno));
    244 		code = -1;
    245 		return 0;
    246 	}
    247 
    248 	if ((sp = getservbyname(port, "tcp")) == NULL) {
    249 		sin.sin_port = htons(21);
    250 	}
    251 	else
    252 		sin.sin_port = sp->s_port;
    253 
    254 	(void)strlcpy(hostnamebuf, hp->h_name, sizeof(hostnamebuf));
    255 	hostname = hostnamebuf;
    256 
    257 	if (hp->h_length > sizeof(sin.sin_addr))
    258 		hp->h_length = sizeof(sin.sin_addr);
    259 
    260 	for (ptr = hp->h_addr_list; *ptr; ptr++) {
    261 		memcpy(&sin.sin_addr, *ptr, (size_t)hp->h_length);
    262 		if (hp->h_addr_list[1])
    263 			fprintf(ttyout, "Trying %s...\n",
    264 			    inet_ntoa(sin.sin_addr));
    265 		s = socket(AF_INET, SOCK_STREAM, 0);
    266 		if (s < 0) {
    267 			cause = "socket";
    268 			continue;
    269 		}
    270 		while ((error = xconnect(s, (struct sockaddr *)&sin,
    271 		    sizeof(sin))) < 0 && errno == EINTR) {
    272 			;
    273 		}
    274 		if (error) {
    275 			/* this "if" clause is to prevent print warning twice */
    276 			if (hp->h_addr_list[1]) {
    277 				warn("connect to address %s",
    278 				    inet_ntoa(sin.sin_addr));
    279 			}
    280 			cause = "connect";
    281 			close(s);
    282 			s = -1;
    283 			continue;
    284 		}
    285 
    286 		/* finally we got one */
    287 		break;
    288 	}
    289 	if (s < 0) {
    290 		warn(cause);
    291 		code = -1;
    292 		return 0;
    293 	}
    294 	memcpy(&hisctladdr, &sin, sizeof(sin));
    295 	len = sizeof(sin);
    296 	if (hisctladdr.su_len == 0)
    297 		hisctladdr.su_len = len;
    298 	family = AF_INET;
    299 #endif
    300 
    301 	if (getsockname(s, (struct sockaddr *)&myctladdr, &len) < 0) {
    302 		warn("getsockname");
    303 		code = -1;
    304 		goto bad;
    305 	}
    306 	if (myctladdr.su_len == 0)
    307 		myctladdr.su_len = len;
    308 
    309 #if defined(IPPROTO_IP) && defined(IP_TOS)
    310 	if (family == AF_INET) {
    311 		int tos = IPTOS_LOWDELAY;
    312 		if (setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&tos,
    313 			       sizeof(int)) < 0)
    314 			warn("setsockopt TOS (ignored)");
    315 	}
    316 #endif
    317 	cin = fdopen(s, "r");
    318 	cout = fdopen(s, "w");
    319 	if (cin == NULL || cout == NULL) {
    320 		warnx("fdopen failed.");
    321 		if (cin)
    322 			(void)fclose(cin);
    323 		if (cout)
    324 			(void)fclose(cout);
    325 		code = -1;
    326 		goto bad;
    327 	}
    328 	if (verbose)
    329 		fprintf(ttyout, "Connected to %s.\n", hostname);
    330 	if (getreply(0) > 2) { 	/* read startup message from server */
    331 		if (cin)
    332 			(void)fclose(cin);
    333 		if (cout)
    334 			(void)fclose(cout);
    335 		code = -1;
    336 		goto bad;
    337 	}
    338 #ifdef SO_OOBINLINE
    339 	{
    340 	int on = 1;
    341 
    342 	if (setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (char *)&on, sizeof(on))
    343 		< 0 && debug) {
    344 			warn("setsockopt");
    345 		}
    346 	}
    347 #endif /* SO_OOBINLINE */
    348 
    349 	return (hostname);
    350 bad:
    351 	(void)close(s);
    352 	return (NULL);
    353 }
    354 
    355 void
    356 cmdabort(notused)
    357 	int notused;
    358 {
    359 
    360 	alarmtimer(0);
    361 	putc('\n', ttyout);
    362 	abrtflag++;
    363 	if (ptflag)
    364 		longjmp(ptabort, 1);
    365 }
    366 
    367 /*VARARGS*/
    368 int
    369 #ifdef __STDC__
    370 command(const char *fmt, ...)
    371 #else
    372 command(va_alist)
    373 	va_dcl
    374 #endif
    375 {
    376 	va_list ap;
    377 	int r;
    378 	sig_t oldintr;
    379 #ifndef __STDC__
    380 	const char *fmt;
    381 #endif
    382 
    383 	abrtflag = 0;
    384 	if (debug) {
    385 		fputs("---> ", ttyout);
    386 #ifdef __STDC__
    387 		va_start(ap, fmt);
    388 #else
    389 		va_start(ap);
    390 		fmt = va_arg(ap, const char *);
    391 #endif
    392 		if (strncmp("PASS ", fmt, 5) == 0)
    393 			fputs("PASS XXXX", ttyout);
    394 		else if (strncmp("ACCT ", fmt, 5) == 0)
    395 			fputs("ACCT XXXX", ttyout);
    396 		else
    397 			vfprintf(ttyout, fmt, ap);
    398 		va_end(ap);
    399 		putc('\n', ttyout);
    400 	}
    401 	if (cout == NULL) {
    402 		warnx("No control connection for command.");
    403 		code = -1;
    404 		return (0);
    405 	}
    406 	oldintr = xsignal(SIGINT, cmdabort);
    407 #ifdef __STDC__
    408 	va_start(ap, fmt);
    409 #else
    410 	va_start(ap);
    411 	fmt = va_arg(ap, char *);
    412 #endif
    413 	vfprintf(cout, fmt, ap);
    414 	va_end(ap);
    415 	fputs("\r\n", cout);
    416 	(void)fflush(cout);
    417 	cpend = 1;
    418 	r = getreply(!strcmp(fmt, "QUIT"));
    419 	if (abrtflag && oldintr != SIG_IGN)
    420 		(*oldintr)(SIGINT);
    421 	(void)xsignal(SIGINT, oldintr);
    422 	return (r);
    423 }
    424 
    425 char reply_string[BUFSIZ];		/* first line of previous reply */
    426 
    427 int
    428 getreply(expecteof)
    429 	int expecteof;
    430 {
    431 	char current_line[BUFSIZ];	/* last line of previous reply */
    432 	int c, n, line;
    433 	int dig;
    434 	int originalcode = 0, continuation = 0;
    435 	sig_t oldintr;
    436 	int pflag = 0;
    437 	char *cp, *pt = pasv;
    438 
    439 	oldintr = xsignal(SIGINT, cmdabort);
    440 	for (line = 0 ;; line++) {
    441 		dig = n = code = 0;
    442 		cp = current_line;
    443 		while ((c = getc(cin)) != '\n') {
    444 			if (c == IAC) {     /* handle telnet commands */
    445 				switch (c = getc(cin)) {
    446 				case WILL:
    447 				case WONT:
    448 					c = getc(cin);
    449 					fprintf(cout, "%c%c%c", IAC, DONT, c);
    450 					(void)fflush(cout);
    451 					break;
    452 				case DO:
    453 				case DONT:
    454 					c = getc(cin);
    455 					fprintf(cout, "%c%c%c", IAC, WONT, c);
    456 					(void)fflush(cout);
    457 					break;
    458 				default:
    459 					break;
    460 				}
    461 				continue;
    462 			}
    463 			dig++;
    464 			if (c == EOF) {
    465 				if (expecteof) {
    466 					(void)xsignal(SIGINT, oldintr);
    467 					code = 221;
    468 					return (0);
    469 				}
    470 				lostpeer();
    471 				if (verbose) {
    472 					fputs(
    473 	    "421 Service not available, remote server has closed connection.\n",
    474 					    ttyout);
    475 				}
    476 				code = 421;
    477 				return (4);
    478 			}
    479 			if (c != '\r' && (verbose > 0 ||
    480 			    ((verbose > -1 && n == '5' && dig > 4) &&
    481 			    (((!n && c < '5') || (n && n < '5'))
    482 			     || !retry_connect)))) {
    483 				if (proxflag &&
    484 				   (dig == 1 || (dig == 5 && verbose == 0)))
    485 					fprintf(ttyout, "%s:", hostname);
    486 				(void)putc(c, ttyout);
    487 			}
    488 			if (dig < 4 && isdigit(c))
    489 				code = code * 10 + (c - '0');
    490 			if (!pflag && (code == 227 || code == 228))
    491 				pflag = 1;
    492 			else if (!pflag && code == 229)
    493 				pflag = 100;
    494 			if (dig > 4 && pflag == 1 && isdigit(c))
    495 				pflag = 2;
    496 			if (pflag == 2) {
    497 				if (c != '\r' && c != ')')
    498 					*pt++ = c;
    499 				else {
    500 					*pt = '\0';
    501 					pflag = 3;
    502 				}
    503 			}
    504 			if (pflag == 100 && c == '(')
    505 				pflag = 2;
    506 			if (dig == 4 && c == '-') {
    507 				if (continuation)
    508 					code = 0;
    509 				continuation++;
    510 			}
    511 			if (n == 0)
    512 				n = c;
    513 			if (cp < &current_line[sizeof(current_line) - 1])
    514 				*cp++ = c;
    515 		}
    516 		if (verbose > 0 || ((verbose > -1 && n == '5') &&
    517 		    (n < '5' || !retry_connect))) {
    518 			(void)putc(c, ttyout);
    519 			(void)fflush (ttyout);
    520 		}
    521 		if (line == 0) {
    522 			size_t len = cp - current_line;
    523 
    524 			if (len > sizeof(reply_string))
    525 				len = sizeof(reply_string);
    526 
    527 			(void)strlcpy(reply_string, current_line, len);
    528 		}
    529 		if (continuation && code != originalcode) {
    530 			if (originalcode == 0)
    531 				originalcode = code;
    532 			continue;
    533 		}
    534 		*cp = '\0';
    535 		if (n != '1')
    536 			cpend = 0;
    537 		(void)xsignal(SIGINT, oldintr);
    538 		if (code == 421 || originalcode == 421)
    539 			lostpeer();
    540 		if (abrtflag && oldintr != cmdabort && oldintr != SIG_IGN)
    541 			(*oldintr)(SIGINT);
    542 		return (n - '0');
    543 	}
    544 }
    545 
    546 static int
    547 empty(cin, din, sec)
    548 	FILE *cin;
    549 	FILE *din;
    550 	int sec;
    551 {
    552 	int nr;
    553 	int nfd = 0;
    554 
    555 #ifdef __USE_SELECT
    556 	struct timeval t;
    557 	fd_set rmask;
    558 
    559 	FD_ZERO(&cin);
    560 	if (cin) {
    561 		if (nfd < fileno(cin))
    562 			nfd = fileno(cin);
    563 		FD_SET(fileno(cin), &rmask);
    564 	}
    565 	if (din) {
    566 		if (nfd < fileno(din))
    567 			nfd = fileno(din);
    568 		FD_SET(fileno(din), &rmask);
    569 	}
    570 
    571 	t.tv_sec = (long) sec;
    572 	t.tv_usec = 0;
    573 	if ((nr = select(nfd, &rmask, NULL, NULL, &t)) <= 0)
    574 		return nr;
    575 
    576 	nr = 0;
    577 	if (cin)
    578 		nr |= FD_ISSET(fileno(cin), &rmask) ? 1 : 0;
    579 	if (din)
    580 		nr |= FD_ISSET(fileno(din), &rmask) ? 2 : 0;
    581 
    582 #else
    583 	struct pollfd pfd[2];
    584 
    585 	if (cin) {
    586 	    pfd[nfd].fd = fileno(cin);
    587 	    pfd[nfd++].events = POLLIN;
    588 	}
    589 
    590 	if (din) {
    591 	    pfd[nfd].fd = fileno(din);
    592 	    pfd[nfd++].events = POLLIN;
    593 	}
    594 
    595 	if ((nr = poll(pfd, nfd, sec * 1000)) <= 0)
    596 		return nr;
    597 
    598 	nr = 0;
    599 	nfd = 0;
    600 	if (cin)
    601 		nr |= (pfd[nfd++].revents & POLLIN) ? 1 : 0;
    602 	if (din)
    603 		nr |= (pfd[nfd++].revents & POLLIN) ? 2 : 0;
    604 #endif
    605 	return nr;
    606 }
    607 
    608 jmp_buf	sendabort;
    609 
    610 void
    611 abortsend(notused)
    612 	int notused;
    613 {
    614 
    615 	alarmtimer(0);
    616 	mflag = 0;
    617 	abrtflag = 0;
    618 	fputs("\nsend aborted\nwaiting for remote to finish abort.\n", ttyout);
    619 	longjmp(sendabort, 1);
    620 }
    621 
    622 void
    623 sendrequest(cmd, local, remote, printnames)
    624 	const char *cmd, *local, *remote;
    625 	int printnames;
    626 {
    627 	struct stat st;
    628 	int c, d;
    629 	FILE *fin, *dout;
    630 	int (*closefunc) __P((FILE *));
    631 	sig_t oldinti, oldintr, oldintp;
    632 	volatile off_t hashbytes;
    633 	char *lmode, *bufp;
    634 	static size_t bufsize;
    635 	static char *buf;
    636 	int oprogress;
    637 
    638 #ifdef __GNUC__			/* to shut up gcc warnings */
    639 	(void)&fin;
    640 	(void)&dout;
    641 	(void)&closefunc;
    642 	(void)&oldinti;
    643 	(void)&oldintr;
    644 	(void)&oldintp;
    645 	(void)&lmode;
    646 #endif
    647 
    648 	hashbytes = mark;
    649 	direction = "sent";
    650 	dout = NULL;
    651 	bytes = 0;
    652 	filesize = -1;
    653 	oprogress = progress;
    654 	if (verbose && printnames) {
    655 		if (local && *local != '-')
    656 			fprintf(ttyout, "local: %s ", local);
    657 		if (remote)
    658 			fprintf(ttyout, "remote: %s\n", remote);
    659 	}
    660 	if (proxy) {
    661 		proxtrans(cmd, local, remote);
    662 		return;
    663 	}
    664 	if (curtype != type)
    665 		changetype(type, 0);
    666 	closefunc = NULL;
    667 	oldintr = NULL;
    668 	oldintp = NULL;
    669 	oldinti = NULL;
    670 	lmode = "w";
    671 	if (setjmp(sendabort)) {
    672 		while (cpend) {
    673 			(void)getreply(0);
    674 		}
    675 		if (data >= 0) {
    676 			(void)close(data);
    677 			data = -1;
    678 		}
    679 		if (oldintr)
    680 			(void)xsignal(SIGINT, oldintr);
    681 		if (oldintp)
    682 			(void)xsignal(SIGPIPE, oldintp);
    683 		if (oldinti)
    684 			(void)xsignal(SIGINFO, oldinti);
    685 		code = -1;
    686 		goto cleanupsend;
    687 	}
    688 	oldintr = xsignal(SIGINT, abortsend);
    689 	oldinti = xsignal(SIGINFO, psummary);
    690 	if (strcmp(local, "-") == 0) {
    691 		fin = stdin;
    692 		progress = 0;
    693 	} else if (*local == '|') {
    694 		oldintp = xsignal(SIGPIPE, SIG_IGN);
    695 		fin = popen(local + 1, "r");
    696 		if (fin == NULL) {
    697 			warn("%s", local + 1);
    698 			(void)xsignal(SIGINT, oldintr);
    699 			(void)xsignal(SIGPIPE, oldintp);
    700 			(void)xsignal(SIGINFO, oldinti);
    701 			code = -1;
    702 			goto cleanupsend;
    703 		}
    704 		progress = 0;
    705 		closefunc = pclose;
    706 	} else {
    707 		fin = fopen(local, "r");
    708 		if (fin == NULL) {
    709 			warn("local: %s", local);
    710 			(void)xsignal(SIGINT, oldintr);
    711 			(void)xsignal(SIGINFO, oldinti);
    712 			code = -1;
    713 			goto cleanupsend;
    714 		}
    715 		closefunc = fclose;
    716 		if (fstat(fileno(fin), &st) < 0 || !S_ISREG(st.st_mode)) {
    717 			fprintf(ttyout, "%s: not a plain file.\n", local);
    718 			(void)xsignal(SIGINT, oldintr);
    719 			(void)xsignal(SIGINFO, oldinti);
    720 			fclose(fin);
    721 			code = -1;
    722 			goto cleanupsend;
    723 		}
    724 		filesize = st.st_size;
    725 	}
    726 	if (initconn()) {
    727 		(void)xsignal(SIGINT, oldintr);
    728 		(void)xsignal(SIGINFO, oldinti);
    729 		if (oldintp)
    730 			(void)xsignal(SIGPIPE, oldintp);
    731 		code = -1;
    732 		if (closefunc != NULL)
    733 			(*closefunc)(fin);
    734 		goto cleanupsend;
    735 	}
    736 	if (setjmp(sendabort))
    737 		goto abort;
    738 
    739 	if (restart_point &&
    740 	    (strcmp(cmd, "STOR") == 0 || strcmp(cmd, "APPE") == 0)) {
    741 		int rc;
    742 
    743 		rc = -1;
    744 		switch (curtype) {
    745 		case TYPE_A:
    746 			rc = fseek(fin, (long) restart_point, SEEK_SET);
    747 			break;
    748 		case TYPE_I:
    749 		case TYPE_L:
    750 			rc = lseek(fileno(fin), restart_point, SEEK_SET);
    751 			break;
    752 		}
    753 		if (rc < 0) {
    754 			warn("local: %s", local);
    755 			if (closefunc != NULL)
    756 				(*closefunc)(fin);
    757 			goto cleanupsend;
    758 		}
    759 #ifndef NO_QUAD
    760 		if (command("REST %lld", (long long) restart_point) !=
    761 #else
    762 		if (command("REST %ld", (long) restart_point) !=
    763 #endif
    764 		    CONTINUE) {
    765 			if (closefunc != NULL)
    766 				(*closefunc)(fin);
    767 			goto cleanupsend;
    768 		}
    769 		lmode = "r+w";
    770 	}
    771 	if (remote) {
    772 		if (command("%s %s", cmd, remote) != PRELIM) {
    773 			(void)xsignal(SIGINT, oldintr);
    774 			(void)xsignal(SIGINFO, oldinti);
    775 			if (oldintp)
    776 				(void)xsignal(SIGPIPE, oldintp);
    777 			if (closefunc != NULL)
    778 				(*closefunc)(fin);
    779 			goto cleanupsend;
    780 		}
    781 	} else
    782 		if (command("%s", cmd) != PRELIM) {
    783 			(void)xsignal(SIGINT, oldintr);
    784 			(void)xsignal(SIGINFO, oldinti);
    785 			if (oldintp)
    786 				(void)xsignal(SIGPIPE, oldintp);
    787 			if (closefunc != NULL)
    788 				(*closefunc)(fin);
    789 			goto cleanupsend;
    790 		}
    791 	dout = dataconn(lmode);
    792 	if (dout == NULL)
    793 		goto abort;
    794 
    795 	if (sndbuf_size > bufsize) {
    796 		if (buf)
    797 			(void)free(buf);
    798 		bufsize = sndbuf_size;
    799 		buf = xmalloc(bufsize);
    800 	}
    801 
    802 	progressmeter(-1);
    803 	oldintp = xsignal(SIGPIPE, SIG_IGN);
    804 
    805 	switch (curtype) {
    806 
    807 	case TYPE_I:
    808 	case TYPE_L:
    809 		while (1) {
    810 			struct timeval then, now, td;
    811 			off_t bufrem;
    812 
    813 			if (rate_put)
    814 				(void)gettimeofday(&then, NULL);
    815 			errno = c = d = 0;
    816 			bufrem = rate_put ? rate_put : bufsize;
    817 			while (bufrem > 0) {
    818 				if ((c = read(fileno(fin), buf,
    819 				    MIN(bufsize, bufrem))) <= 0)
    820 					goto senddone;
    821 				bytes += c;
    822 				bufrem -= c;
    823 				for (bufp = buf; c > 0; c -= d, bufp += d)
    824 					if ((d = write(fileno(dout), bufp, c))
    825 					    <= 0)
    826 						break;
    827 				if (d < 0)
    828 					goto senddone;
    829 				if (hash && (!progress || filesize < 0) ) {
    830 					while (bytes >= hashbytes) {
    831 						(void)putc('#', ttyout);
    832 						hashbytes += mark;
    833 					}
    834 					(void)fflush(ttyout);
    835 				}
    836 			}
    837 			if (rate_put) {
    838 				while (1) {
    839 					(void)gettimeofday(&now, NULL);
    840 					timersub(&now, &then, &td);
    841 					if (td.tv_sec > 0)
    842 						break;
    843 					usleep(1000000 - td.tv_usec);
    844 				}
    845 			}
    846 		}
    847  senddone:
    848 		if (hash && (!progress || filesize < 0) && bytes > 0) {
    849 			if (bytes < mark)
    850 				(void)putc('#', ttyout);
    851 			(void)putc('\n', ttyout);
    852 		}
    853 		if (c < 0)
    854 			warn("local: %s", local);
    855 		if (d < 0) {
    856 			if (errno != EPIPE)
    857 				warn("netout");
    858 			bytes = -1;
    859 		}
    860 		break;
    861 
    862 	case TYPE_A:
    863 		while ((c = getc(fin)) != EOF) {
    864 			if (c == '\n') {
    865 				while (hash && (!progress || filesize < 0) &&
    866 				    (bytes >= hashbytes)) {
    867 					(void)putc('#', ttyout);
    868 					(void)fflush(ttyout);
    869 					hashbytes += mark;
    870 				}
    871 				if (ferror(dout))
    872 					break;
    873 				(void)putc('\r', dout);
    874 				bytes++;
    875 			}
    876 			(void)putc(c, dout);
    877 			bytes++;
    878 #if 0	/* this violates RFC */
    879 			if (c == '\r') {
    880 				(void)putc('\0', dout);
    881 				bytes++;
    882 			}
    883 #endif
    884 		}
    885 		if (hash && (!progress || filesize < 0)) {
    886 			if (bytes < hashbytes)
    887 				(void)putc('#', ttyout);
    888 			(void)putc('\n', ttyout);
    889 		}
    890 		if (ferror(fin))
    891 			warn("local: %s", local);
    892 		if (ferror(dout)) {
    893 			if (errno != EPIPE)
    894 				warn("netout");
    895 			bytes = -1;
    896 		}
    897 		break;
    898 	}
    899 	progressmeter(1);
    900 	if (closefunc != NULL)
    901 		(*closefunc)(fin);
    902 	(void)fclose(dout);
    903 	(void)getreply(0);
    904 	(void)xsignal(SIGINT, oldintr);
    905 	(void)xsignal(SIGINFO, oldinti);
    906 	if (oldintp)
    907 		(void)xsignal(SIGPIPE, oldintp);
    908 	if (bytes > 0)
    909 		ptransfer(0);
    910 	goto cleanupsend;
    911 abort:
    912 	(void)xsignal(SIGINT, oldintr);
    913 	(void)xsignal(SIGINFO, oldinti);
    914 	if (oldintp)
    915 		(void)xsignal(SIGPIPE, oldintp);
    916 	if (!cpend) {
    917 		code = -1;
    918 		return;
    919 	}
    920 	if (data >= 0) {
    921 		(void)close(data);
    922 		data = -1;
    923 	}
    924 	if (dout)
    925 		(void)fclose(dout);
    926 	(void)getreply(0);
    927 	code = -1;
    928 	if (closefunc != NULL && fin != NULL)
    929 		(*closefunc)(fin);
    930 	if (bytes > 0)
    931 		ptransfer(0);
    932 cleanupsend:
    933 	progress = oprogress;
    934 	restart_point = 0;
    935 }
    936 
    937 jmp_buf	recvabort;
    938 
    939 void
    940 abortrecv(notused)
    941 	int notused;
    942 {
    943 
    944 	alarmtimer(0);
    945 	mflag = 0;
    946 	abrtflag = 0;
    947 	fputs("\nreceive aborted\nwaiting for remote to finish abort.\n",
    948 	    ttyout);
    949 	longjmp(recvabort, 1);
    950 }
    951 
    952 void
    953 recvrequest(cmd, local, remote, lmode, printnames, ignorespecial)
    954 	const char *cmd, *local, *remote, *lmode;
    955 	int printnames, ignorespecial;
    956 {
    957 	FILE *fout, *din;
    958 	int (*closefunc) __P((FILE *));
    959 	sig_t oldinti, oldintr, oldintp;
    960 	int c, d;
    961 	volatile int is_retr, tcrflag, bare_lfs;
    962 	static size_t bufsize;
    963 	static char *buf;
    964 	volatile off_t hashbytes;
    965 	struct stat st;
    966 	time_t mtime;
    967 	struct timeval tval[2];
    968 	int oprogress;
    969 	int opreserve;
    970 
    971 #ifdef __GNUC__			/* to shut up gcc warnings */
    972 	(void)&local;
    973 	(void)&fout;
    974 	(void)&din;
    975 	(void)&closefunc;
    976 	(void)&oldinti;
    977 	(void)&oldintr;
    978 	(void)&oldintp;
    979 #endif
    980 
    981 	fout = NULL;
    982 	din = NULL;
    983 	oldinti = NULL;
    984 	hashbytes = mark;
    985 	direction = "received";
    986 	bytes = 0;
    987 	bare_lfs = 0;
    988 	filesize = -1;
    989 	oprogress = progress;
    990 	opreserve = preserve;
    991 	is_retr = (strcmp(cmd, "RETR") == 0);
    992 	if (is_retr && verbose && printnames) {
    993 		if (local && (ignorespecial || *local != '-'))
    994 			fprintf(ttyout, "local: %s ", local);
    995 		if (remote)
    996 			fprintf(ttyout, "remote: %s\n", remote);
    997 	}
    998 	if (proxy && is_retr) {
    999 		proxtrans(cmd, local, remote);
   1000 		return;
   1001 	}
   1002 	closefunc = NULL;
   1003 	oldintr = NULL;
   1004 	oldintp = NULL;
   1005 	tcrflag = !crflag && is_retr;
   1006 	if (setjmp(recvabort)) {
   1007 		while (cpend) {
   1008 			(void)getreply(0);
   1009 		}
   1010 		if (data >= 0) {
   1011 			(void)close(data);
   1012 			data = -1;
   1013 		}
   1014 		if (oldintr)
   1015 			(void)xsignal(SIGINT, oldintr);
   1016 		if (oldinti)
   1017 			(void)xsignal(SIGINFO, oldinti);
   1018 		progress = oprogress;
   1019 		preserve = opreserve;
   1020 		code = -1;
   1021 		return;
   1022 	}
   1023 	oldintr = xsignal(SIGINT, abortrecv);
   1024 	oldinti = xsignal(SIGINFO, psummary);
   1025 	if (ignorespecial || (strcmp(local, "-") && *local != '|')) {
   1026 		if (access(local, W_OK) < 0) {
   1027 			char *dir = strrchr(local, '/');
   1028 
   1029 			if (errno != ENOENT && errno != EACCES) {
   1030 				warn("local: %s", local);
   1031 				(void)xsignal(SIGINT, oldintr);
   1032 				(void)xsignal(SIGINFO, oldinti);
   1033 				code = -1;
   1034 				return;
   1035 			}
   1036 			if (dir != NULL)
   1037 				*dir = 0;
   1038 			d = access(dir == local ? "/" :
   1039 			    dir ? local : ".", W_OK);
   1040 			if (dir != NULL)
   1041 				*dir = '/';
   1042 			if (d < 0) {
   1043 				warn("local: %s", local);
   1044 				(void)xsignal(SIGINT, oldintr);
   1045 				(void)xsignal(SIGINFO, oldinti);
   1046 				code = -1;
   1047 				return;
   1048 			}
   1049 			if (!runique && errno == EACCES &&
   1050 			    chmod(local, (S_IRUSR|S_IWUSR)) < 0) {
   1051 				warn("local: %s", local);
   1052 				(void)xsignal(SIGINT, oldintr);
   1053 				(void)xsignal(SIGINFO, oldinti);
   1054 				code = -1;
   1055 				return;
   1056 			}
   1057 			if (runique && errno == EACCES &&
   1058 			   (local = gunique(local)) == NULL) {
   1059 				(void)xsignal(SIGINT, oldintr);
   1060 				(void)xsignal(SIGINFO, oldinti);
   1061 				code = -1;
   1062 				return;
   1063 			}
   1064 		}
   1065 		else if (runique && (local = gunique(local)) == NULL) {
   1066 			(void)xsignal(SIGINT, oldintr);
   1067 			(void)xsignal(SIGINFO, oldinti);
   1068 			code = -1;
   1069 			return;
   1070 		}
   1071 	}
   1072 	if (!is_retr) {
   1073 		if (curtype != TYPE_A)
   1074 			changetype(TYPE_A, 0);
   1075 	} else {
   1076 		if (curtype != type)
   1077 			changetype(type, 0);
   1078 		filesize = remotesize(remote, 0);
   1079 	}
   1080 	if (initconn()) {
   1081 		(void)xsignal(SIGINT, oldintr);
   1082 		(void)xsignal(SIGINFO, oldinti);
   1083 		code = -1;
   1084 		return;
   1085 	}
   1086 	if (setjmp(recvabort))
   1087 		goto abort;
   1088 	if (is_retr && restart_point &&
   1089 #ifndef NO_QUAD
   1090 	    command("REST %lld", (long long) restart_point) != CONTINUE)
   1091 #else
   1092 	    command("REST %ld", (long) restart_point) != CONTINUE)
   1093 #endif
   1094 		return;
   1095 	if (remote) {
   1096 		if (command("%s %s", cmd, remote) != PRELIM) {
   1097 			(void)xsignal(SIGINT, oldintr);
   1098 			(void)xsignal(SIGINFO, oldinti);
   1099 			return;
   1100 		}
   1101 	} else {
   1102 		if (command("%s", cmd) != PRELIM) {
   1103 			(void)xsignal(SIGINT, oldintr);
   1104 			(void)xsignal(SIGINFO, oldinti);
   1105 			return;
   1106 		}
   1107 	}
   1108 	din = dataconn("r");
   1109 	if (din == NULL)
   1110 		goto abort;
   1111 	if (!ignorespecial && strcmp(local, "-") == 0) {
   1112 		fout = stdout;
   1113 		progress = 0;
   1114 		preserve = 0;
   1115 	} else if (!ignorespecial && *local == '|') {
   1116 		oldintp = xsignal(SIGPIPE, SIG_IGN);
   1117 		fout = popen(local + 1, "w");
   1118 		if (fout == NULL) {
   1119 			warn("%s", local+1);
   1120 			goto abort;
   1121 		}
   1122 		progress = 0;
   1123 		preserve = 0;
   1124 		closefunc = pclose;
   1125 	} else {
   1126 		fout = fopen(local, lmode);
   1127 		if (fout == NULL) {
   1128 			warn("local: %s", local);
   1129 			goto abort;
   1130 		}
   1131 		closefunc = fclose;
   1132 	}
   1133 
   1134 	if (fstat(fileno(fout), &st) != -1 && !S_ISREG(st.st_mode)) {
   1135 		progress = 0;
   1136 		preserve = 0;
   1137 	}
   1138 	if (rcvbuf_size > bufsize) {
   1139 		if (buf)
   1140 			(void)free(buf);
   1141 		bufsize = rcvbuf_size;
   1142 		buf = xmalloc(bufsize);
   1143 	}
   1144 
   1145 	progressmeter(-1);
   1146 
   1147 	switch (curtype) {
   1148 
   1149 	case TYPE_I:
   1150 	case TYPE_L:
   1151 		if (is_retr && restart_point &&
   1152 		    lseek(fileno(fout), restart_point, SEEK_SET) < 0) {
   1153 			warn("local: %s", local);
   1154 			progress = oprogress;
   1155 			preserve = opreserve;
   1156 			if (closefunc != NULL)
   1157 				(*closefunc)(fout);
   1158 			return;
   1159 		}
   1160 		while (1) {
   1161 			struct timeval then, now, td;
   1162 			off_t bufrem;
   1163 
   1164 			if (rate_get)
   1165 				(void)gettimeofday(&then, NULL);
   1166 			errno = c = d = 0;
   1167 			bufrem = rate_get ? rate_get : bufsize;
   1168 			while (bufrem > 0) {
   1169 				if ((c = read(fileno(din), buf,
   1170 				    MIN(bufsize, bufrem))) <= 0)
   1171 					goto recvdone;
   1172 				bytes += c;
   1173 				bufrem -=c;
   1174 				if ((d = write(fileno(fout), buf, c)) != c)
   1175 					goto recvdone;
   1176 				if (hash && (!progress || filesize < 0)) {
   1177 					while (bytes >= hashbytes) {
   1178 						(void)putc('#', ttyout);
   1179 						hashbytes += mark;
   1180 					}
   1181 					(void)fflush(ttyout);
   1182 				}
   1183 			}
   1184 			if (rate_get) {
   1185 				while (1) {
   1186 					(void)gettimeofday(&now, NULL);
   1187 					timersub(&now, &then, &td);
   1188 					if (td.tv_sec > 0)
   1189 						break;
   1190 					usleep(1000000 - td.tv_usec);
   1191 				}
   1192 			}
   1193 		}
   1194  recvdone:
   1195 		if (hash && (!progress || filesize < 0) && bytes > 0) {
   1196 			if (bytes < mark)
   1197 				(void)putc('#', ttyout);
   1198 			(void)putc('\n', ttyout);
   1199 		}
   1200 		if (c < 0) {
   1201 			if (errno != EPIPE)
   1202 				warn("netin");
   1203 			bytes = -1;
   1204 		}
   1205 		if (d < c) {
   1206 			if (d < 0)
   1207 				warn("local: %s", local);
   1208 			else
   1209 				warnx("%s: short write", local);
   1210 		}
   1211 		break;
   1212 
   1213 	case TYPE_A:
   1214 		if (is_retr && restart_point) {
   1215 			int ch;
   1216 			long i, n;
   1217 
   1218 			if (fseek(fout, 0L, SEEK_SET) < 0)
   1219 				goto done;
   1220 			n = (long)restart_point;
   1221 			for (i = 0; i++ < n;) {
   1222 				if ((ch = getc(fout)) == EOF)
   1223 					goto done;
   1224 				if (ch == '\n')
   1225 					i++;
   1226 			}
   1227 			if (fseek(fout, 0L, SEEK_CUR) < 0) {
   1228 done:
   1229 				warn("local: %s", local);
   1230 				progress = oprogress;
   1231 				preserve = opreserve;
   1232 				if (closefunc != NULL)
   1233 					(*closefunc)(fout);
   1234 				return;
   1235 			}
   1236 		}
   1237 		while ((c = getc(din)) != EOF) {
   1238 			if (c == '\n')
   1239 				bare_lfs++;
   1240 			while (c == '\r') {
   1241 				while (hash && (!progress || filesize < 0) &&
   1242 				    (bytes >= hashbytes)) {
   1243 					(void)putc('#', ttyout);
   1244 					(void)fflush(ttyout);
   1245 					hashbytes += mark;
   1246 				}
   1247 				bytes++;
   1248 				if ((c = getc(din)) != '\n' || tcrflag) {
   1249 					if (ferror(fout))
   1250 						goto break2;
   1251 					(void)putc('\r', fout);
   1252 					if (c == '\0') {
   1253 						bytes++;
   1254 						goto contin2;
   1255 					}
   1256 					if (c == EOF)
   1257 						goto contin2;
   1258 				}
   1259 			}
   1260 			(void)putc(c, fout);
   1261 			bytes++;
   1262 	contin2:	;
   1263 		}
   1264 break2:
   1265 		if (hash && (!progress || filesize < 0)) {
   1266 			if (bytes < hashbytes)
   1267 				(void)putc('#', ttyout);
   1268 			(void)putc('\n', ttyout);
   1269 		}
   1270 		if (ferror(din)) {
   1271 			if (errno != EPIPE)
   1272 				warn("netin");
   1273 			bytes = -1;
   1274 		}
   1275 		if (ferror(fout))
   1276 			warn("local: %s", local);
   1277 		break;
   1278 	}
   1279 	progressmeter(1);
   1280 	if (closefunc != NULL)
   1281 		(*closefunc)(fout);
   1282 	(void)xsignal(SIGINT, oldintr);
   1283 	(void)xsignal(SIGINFO, oldinti);
   1284 	if (oldintp)
   1285 		(void)xsignal(SIGPIPE, oldintp);
   1286 	(void)fclose(din);
   1287 	(void)getreply(0);
   1288 	if (bare_lfs) {
   1289 		fprintf(ttyout,
   1290 		    "WARNING! %d bare linefeeds received in ASCII mode.\n",
   1291 		    bare_lfs);
   1292 		fputs("File may not have transferred correctly.\n", ttyout);
   1293 	}
   1294 	if (bytes >= 0 && is_retr) {
   1295 		if (bytes > 0)
   1296 			ptransfer(0);
   1297 		if (preserve && (closefunc == fclose)) {
   1298 			mtime = remotemodtime(remote, 0);
   1299 			if (mtime != -1) {
   1300 				(void)gettimeofday(&tval[0], NULL);
   1301 				tval[1].tv_sec = mtime;
   1302 				tval[1].tv_usec = 0;
   1303 				if (utimes(local, tval) == -1) {
   1304 					fprintf(ttyout,
   1305 				"Can't change modification time on %s to %s",
   1306 					    local, asctime(localtime(&mtime)));
   1307 				}
   1308 			}
   1309 		}
   1310 	}
   1311 	progress = oprogress;
   1312 	preserve = opreserve;
   1313 	return;
   1314 
   1315 abort:
   1316 
   1317 /* abort using RFC 959 recommended IP,SYNC sequence */
   1318 
   1319 	progress = oprogress;
   1320 	preserve = opreserve;
   1321 	if (oldintp)
   1322 		(void)xsignal(SIGPIPE, oldintp);
   1323 	(void)xsignal(SIGINT, SIG_IGN);
   1324 	if (!cpend) {
   1325 		code = -1;
   1326 		(void)xsignal(SIGINT, oldintr);
   1327 		(void)xsignal(SIGINFO, oldinti);
   1328 		return;
   1329 	}
   1330 
   1331 	abort_remote(din);
   1332 	code = -1;
   1333 	if (data >= 0) {
   1334 		(void)close(data);
   1335 		data = -1;
   1336 	}
   1337 	if (closefunc != NULL && fout != NULL)
   1338 		(*closefunc)(fout);
   1339 	if (din)
   1340 		(void)fclose(din);
   1341 	if (bytes > 0)
   1342 		ptransfer(0);
   1343 	(void)xsignal(SIGINT, oldintr);
   1344 	(void)xsignal(SIGINFO, oldinti);
   1345 }
   1346 
   1347 /*
   1348  * Need to start a listen on the data channel before we send the command,
   1349  * otherwise the server's connect may fail.
   1350  */
   1351 int
   1352 initconn()
   1353 {
   1354 	char *p, *a;
   1355 	int result, len, tmpno = 0;
   1356 	int on = 1;
   1357 	int error;
   1358 	u_int addr[16], port[2];
   1359 	u_int af, hal, pal;
   1360 	char *pasvcmd = NULL;
   1361 
   1362 #ifdef INET6
   1363 	if (myctladdr.su_family == AF_INET6
   1364 	 && (IN6_IS_ADDR_LINKLOCAL(&myctladdr.su_sin6.sin6_addr)
   1365 	  || IN6_IS_ADDR_SITELOCAL(&myctladdr.su_sin6.sin6_addr))) {
   1366 		warnx("use of scoped address can be troublesome");
   1367 	}
   1368 #endif
   1369 reinit:
   1370 	if (passivemode) {
   1371 		data_addr = myctladdr;
   1372 		data = socket(data_addr.su_family, SOCK_STREAM, 0);
   1373 		if (data < 0) {
   1374 			warn("socket");
   1375 			return (1);
   1376 		}
   1377 		if ((options & SO_DEBUG) &&
   1378 		    setsockopt(data, SOL_SOCKET, SO_DEBUG, (char *)&on,
   1379 			       sizeof(on)) < 0)
   1380 			warn("setsockopt (ignored)");
   1381 		result = COMPLETE + 1;
   1382 		switch (data_addr.su_family) {
   1383 		case AF_INET:
   1384 			if (epsv4) {
   1385 				result = command(pasvcmd = "EPSV");
   1386 				/*
   1387 				 * this code is to be friendly with broken
   1388 				 * BSDI ftpd
   1389 				 */
   1390 				if (code / 10 == 22 && code != 229) {
   1391 					fputs(
   1392 "wrong server: return code must be 229\n",
   1393 						ttyout);
   1394 					result = COMPLETE + 1;
   1395 				}
   1396 			}
   1397 			if (result != COMPLETE)
   1398 				result = command(pasvcmd = "PASV");
   1399 			break;
   1400 #ifdef INET6
   1401 		case AF_INET6:
   1402 			result = command(pasvcmd = "EPSV");
   1403 			/* this code is to be friendly with broken BSDI ftpd */
   1404 			if (code / 10 == 22 && code != 229) {
   1405 				fputs(
   1406 "wrong server: return code must be 229\n",
   1407 					ttyout);
   1408 				result = COMPLETE + 1;
   1409 			}
   1410 			if (result != COMPLETE)
   1411 				result = command(pasvcmd = "LPSV");
   1412 			break;
   1413 #endif
   1414 		default:
   1415 			result = COMPLETE + 1;
   1416 			break;
   1417 		}
   1418 		if (result != COMPLETE) {
   1419 			if (activefallback) {
   1420 				(void)close(data);
   1421 				data = -1;
   1422 				passivemode = 0;
   1423 				activefallback = 0;
   1424 				goto reinit;
   1425 			}
   1426 			fputs("Passive mode refused.\n", ttyout);
   1427 			goto bad;
   1428 		}
   1429 
   1430 #define pack2(var, off) \
   1431 	(((var[(off) + 0] & 0xff) << 8) | ((var[(off) + 1] & 0xff) << 0))
   1432 #define pack4(var, off) \
   1433 	(((var[(off) + 0] & 0xff) << 24) | ((var[(off) + 1] & 0xff) << 16) | \
   1434 	 ((var[(off) + 2] & 0xff) << 8) | ((var[(off) + 3] & 0xff) << 0))
   1435 
   1436 		/*
   1437 		 * What we've got at this point is a string of comma separated
   1438 		 * one-byte unsigned integer values, separated by commas.
   1439 		 */
   1440 		if (strcmp(pasvcmd, "PASV") == 0) {
   1441 			if (data_addr.su_family != AF_INET) {
   1442 				fputs(
   1443 "Passive mode AF mismatch. Shouldn't happen!\n", ttyout);
   1444 				error = 1;
   1445 				goto bad;
   1446 			}
   1447 			if (code / 10 == 22 && code != 227) {
   1448 				fputs("wrong server: return code must be 227\n",
   1449 					ttyout);
   1450 				error = 1;
   1451 				goto bad;
   1452 			}
   1453 			error = sscanf(pasv, "%u,%u,%u,%u,%u,%u",
   1454 					&addr[0], &addr[1], &addr[2], &addr[3],
   1455 					&port[0], &port[1]);
   1456 			if (error != 6) {
   1457 				fputs(
   1458 "Passive mode address scan failure. Shouldn't happen!\n", ttyout);
   1459 				error = 1;
   1460 				goto bad;
   1461 			}
   1462 			error = 0;
   1463 			memset(&data_addr, 0, sizeof(data_addr));
   1464 			data_addr.su_family = AF_INET;
   1465 			data_addr.su_len = sizeof(struct sockaddr_in);
   1466 			data_addr.su_sin.sin_addr.s_addr =
   1467 				htonl(pack4(addr, 0));
   1468 			data_addr.su_port = htons(pack2(port, 0));
   1469 		} else if (strcmp(pasvcmd, "LPSV") == 0) {
   1470 			if (code / 10 == 22 && code != 228) {
   1471 				fputs("wrong server: return code must be 228\n",
   1472 					ttyout);
   1473 				error = 1;
   1474 				goto bad;
   1475 			}
   1476 			switch (data_addr.su_family) {
   1477 			case AF_INET:
   1478 				error = sscanf(pasv,
   1479 "%u,%u,%u,%u,%u,%u,%u,%u,%u",
   1480 					&af, &hal,
   1481 					&addr[0], &addr[1], &addr[2], &addr[3],
   1482 					&pal, &port[0], &port[1]);
   1483 				if (error != 9) {
   1484 					fputs(
   1485 "Passive mode address scan failure. Shouldn't happen!\n", ttyout);
   1486 					error = 1;
   1487 					goto bad;
   1488 				}
   1489 				if (af != 4 || hal != 4 || pal != 2) {
   1490 					fputs(
   1491 "Passive mode AF mismatch. Shouldn't happen!\n", ttyout);
   1492 					error = 1;
   1493 					goto bad;
   1494 				}
   1495 
   1496 				error = 0;
   1497 				memset(&data_addr, 0, sizeof(data_addr));
   1498 				data_addr.su_family = AF_INET;
   1499 				data_addr.su_len = sizeof(struct sockaddr_in);
   1500 				data_addr.su_sin.sin_addr.s_addr =
   1501 					htonl(pack4(addr, 0));
   1502 				data_addr.su_port = htons(pack2(port, 0));
   1503 				break;
   1504 #ifdef INET6
   1505 			case AF_INET6:
   1506 				error = sscanf(pasv,
   1507 "%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u",
   1508 					&af, &hal,
   1509 					&addr[0], &addr[1], &addr[2], &addr[3],
   1510 					&addr[4], &addr[5], &addr[6], &addr[7],
   1511 					&addr[8], &addr[9], &addr[10],
   1512 					&addr[11], &addr[12], &addr[13],
   1513 					&addr[14], &addr[15],
   1514 					&pal, &port[0], &port[1]);
   1515 				if (error != 21) {
   1516 					fputs(
   1517 "Passive mode address scan failure. Shouldn't happen!\n", ttyout);
   1518 					error = 1;
   1519 					goto bad;
   1520 				}
   1521 				if (af != 6 || hal != 16 || pal != 2) {
   1522 					fputs(
   1523 "Passive mode AF mismatch. Shouldn't happen!\n", ttyout);
   1524 					error = 1;
   1525 					goto bad;
   1526 				}
   1527 
   1528 				error = 0;
   1529 				memset(&data_addr, 0, sizeof(data_addr));
   1530 				data_addr.su_family = AF_INET6;
   1531 				data_addr.su_len = sizeof(struct sockaddr_in6);
   1532 			    {
   1533 				u_int32_t *p32;
   1534 				p32 = (u_int32_t *)&data_addr.su_sin6.sin6_addr;
   1535 				p32[0] = htonl(pack4(addr, 0));
   1536 				p32[1] = htonl(pack4(addr, 4));
   1537 				p32[2] = htonl(pack4(addr, 8));
   1538 				p32[3] = htonl(pack4(addr, 12));
   1539 			    }
   1540 				data_addr.su_port = htons(pack2(port, 0));
   1541 				break;
   1542 #endif
   1543 			default:
   1544 				error = 1;
   1545 			}
   1546 		} else if (strcmp(pasvcmd, "EPSV") == 0) {
   1547 			char delim[4];
   1548 
   1549 			port[0] = 0;
   1550 			if (code / 10 == 22 && code != 229) {
   1551 				fputs("wrong server: return code must be 229\n",
   1552 					ttyout);
   1553 				error = 1;
   1554 				goto bad;
   1555 			}
   1556 			if (sscanf(pasv, "%c%c%c%d%c", &delim[0],
   1557 					&delim[1], &delim[2], &port[1],
   1558 					&delim[3]) != 5) {
   1559 				fputs("parse error!\n", ttyout);
   1560 				error = 1;
   1561 				goto bad;
   1562 			}
   1563 			if (delim[0] != delim[1] || delim[0] != delim[2]
   1564 			 || delim[0] != delim[3]) {
   1565 				fputs("parse error!\n", ttyout);
   1566 				error = 1;
   1567 				goto bad;
   1568 			}
   1569 			data_addr = hisctladdr;
   1570 			data_addr.su_port = htons(port[1]);
   1571 		} else
   1572 			goto bad;
   1573 
   1574 		while (xconnect(data, (struct sockaddr *)&data_addr,
   1575 			    data_addr.su_len) < 0) {
   1576 			if (errno == EINTR)
   1577 				continue;
   1578 			if (activefallback) {
   1579 				(void)close(data);
   1580 				data = -1;
   1581 				passivemode = 0;
   1582 				activefallback = 0;
   1583 				goto reinit;
   1584 			}
   1585 			warn("connect");
   1586 			goto bad;
   1587 		}
   1588 #if defined(IPPROTO_IP) && defined(IP_TOS)
   1589 		if (data_addr.su_family == AF_INET) {
   1590 			on = IPTOS_THROUGHPUT;
   1591 			if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&on,
   1592 				       sizeof(int)) < 0)
   1593 				warn("setsockopt TOS (ignored)");
   1594 		}
   1595 #endif
   1596 		return (0);
   1597 	}
   1598 
   1599 noport:
   1600 	data_addr = myctladdr;
   1601 	if (sendport)
   1602 		data_addr.su_port = 0;	/* let system pick one */
   1603 	if (data != -1)
   1604 		(void)close(data);
   1605 	data = socket(data_addr.su_family, SOCK_STREAM, 0);
   1606 	if (data < 0) {
   1607 		warn("socket");
   1608 		if (tmpno)
   1609 			sendport = 1;
   1610 		return (1);
   1611 	}
   1612 	if (!sendport)
   1613 		if (setsockopt(data, SOL_SOCKET, SO_REUSEADDR, (char *)&on,
   1614 				sizeof(on)) < 0) {
   1615 			warn("setsockopt (reuse address)");
   1616 			goto bad;
   1617 		}
   1618 	if (bind(data, (struct sockaddr *)&data_addr, data_addr.su_len) < 0) {
   1619 		warn("bind");
   1620 		goto bad;
   1621 	}
   1622 	if (options & SO_DEBUG &&
   1623 	    setsockopt(data, SOL_SOCKET, SO_DEBUG, (char *)&on,
   1624 			sizeof(on)) < 0)
   1625 		warn("setsockopt (ignored)");
   1626 	len = sizeof(data_addr);
   1627 	if (getsockname(data, (struct sockaddr *)&data_addr, &len) < 0) {
   1628 		warn("getsockname");
   1629 		goto bad;
   1630 	}
   1631 	if (xlisten(data, 1) < 0)
   1632 		warn("listen");
   1633 
   1634 #define	UC(b)	(((int)b)&0xff)
   1635 
   1636 	if (sendport) {
   1637 #ifdef INET6
   1638 		char hname[INET6_ADDRSTRLEN];
   1639 		int af;
   1640 #endif
   1641 
   1642 		switch (data_addr.su_family) {
   1643 		case AF_INET:
   1644 			if (!epsv4) {
   1645 				result = COMPLETE + 1;
   1646 				break;
   1647 			}
   1648 			/* FALLTHROUGH */
   1649 #ifdef INET6
   1650 		case AF_INET6:
   1651 			af = (data_addr.su_family == AF_INET) ? 1 : 2;
   1652 			if (getnameinfo((struct sockaddr *)&data_addr,
   1653 					data_addr.su_len, hname, sizeof(hname),
   1654 					NULL, 0, NI_NUMERICHOST)) {
   1655 				result = ERROR;
   1656 			} else {
   1657 				result = command("EPRT |%d|%s|%d|",
   1658 					af, hname, ntohs(data_addr.su_port));
   1659 			}
   1660 			break;
   1661 #endif
   1662 		default:
   1663 			result = COMPLETE + 1;
   1664 			break;
   1665 		}
   1666 		if (result == COMPLETE)
   1667 			goto skip_port;
   1668 
   1669 		switch (data_addr.su_family) {
   1670 		case AF_INET:
   1671 			a = (char *)&data_addr.su_sin.sin_addr;
   1672 			p = (char *)&data_addr.su_port;
   1673 			result = command("PORT %d,%d,%d,%d,%d,%d",
   1674 				 UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
   1675 				 UC(p[0]), UC(p[1]));
   1676 			break;
   1677 #ifdef INET6
   1678 		case AF_INET6:
   1679 			a = (char *)&data_addr.su_sin6.sin6_addr;
   1680 			p = (char *)&data_addr.su_port;
   1681 			result = command(
   1682 "LPRT %d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
   1683 				 6, 16,
   1684 				 UC(a[0]),UC(a[1]),UC(a[2]),UC(a[3]),
   1685 				 UC(a[4]),UC(a[5]),UC(a[6]),UC(a[7]),
   1686 				 UC(a[8]),UC(a[9]),UC(a[10]),UC(a[11]),
   1687 				 UC(a[12]),UC(a[13]),UC(a[14]),UC(a[15]),
   1688 				 2, UC(p[0]), UC(p[1]));
   1689 			break;
   1690 #endif
   1691 		default:
   1692 			result = COMPLETE + 1; /* xxx */
   1693 		}
   1694 	skip_port:
   1695 
   1696 		if (result == ERROR && sendport == -1) {
   1697 			sendport = 0;
   1698 			tmpno = 1;
   1699 			goto noport;
   1700 		}
   1701 		return (result != COMPLETE);
   1702 	}
   1703 	if (tmpno)
   1704 		sendport = 1;
   1705 #if defined(IPPROTO_IP) && defined(IP_TOS)
   1706 	if (data_addr.su_family == AF_INET) {
   1707 		on = IPTOS_THROUGHPUT;
   1708 		if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&on,
   1709 			       sizeof(int)) < 0)
   1710 			warn("setsockopt TOS (ignored)");
   1711 	}
   1712 #endif
   1713 	return (0);
   1714 bad:
   1715 	(void)close(data), data = -1;
   1716 	if (tmpno)
   1717 		sendport = 1;
   1718 	return (1);
   1719 }
   1720 
   1721 FILE *
   1722 dataconn(lmode)
   1723 	const char *lmode;
   1724 {
   1725 	union sockunion from;
   1726 	int s, fromlen = myctladdr.su_len;
   1727 
   1728 	if (passivemode)
   1729 		return (fdopen(data, lmode));
   1730 
   1731 	s = accept(data, (struct sockaddr *) &from, &fromlen);
   1732 	if (s < 0) {
   1733 		warn("accept");
   1734 		(void)close(data), data = -1;
   1735 		return (NULL);
   1736 	}
   1737 	(void)close(data);
   1738 	data = s;
   1739 #if defined(IPPROTO_IP) && defined(IP_TOS)
   1740 	if (from.su_family == AF_INET) {
   1741 		int tos = IPTOS_THROUGHPUT;
   1742 		if (setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&tos,
   1743 				sizeof(int)) < 0) {
   1744 			warn("setsockopt TOS (ignored)");
   1745 		}
   1746 	}
   1747 #endif
   1748 	return (fdopen(data, lmode));
   1749 }
   1750 
   1751 void
   1752 psummary(notused)
   1753 	int notused;
   1754 {
   1755 	int oerrno;
   1756 
   1757 	oerrno = errno;
   1758 	if (bytes > 0)
   1759 		ptransfer(1);
   1760 	errno = oerrno;
   1761 }
   1762 
   1763 void
   1764 psabort(notused)
   1765 	int notused;
   1766 {
   1767 
   1768 	alarmtimer(0);
   1769 	abrtflag++;
   1770 }
   1771 
   1772 void
   1773 pswitch(flag)
   1774 	int flag;
   1775 {
   1776 	sig_t oldintr;
   1777 	static struct comvars {
   1778 		int connect;
   1779 		char name[MAXHOSTNAMELEN];
   1780 		union sockunion mctl;
   1781 		union sockunion hctl;
   1782 		FILE *in;
   1783 		FILE *out;
   1784 		int tpe;
   1785 		int curtpe;
   1786 		int cpnd;
   1787 		int sunqe;
   1788 		int runqe;
   1789 		int mcse;
   1790 		int ntflg;
   1791 		char nti[17];
   1792 		char nto[17];
   1793 		int mapflg;
   1794 		char mi[MAXPATHLEN];
   1795 		char mo[MAXPATHLEN];
   1796 	} proxstruct, tmpstruct;
   1797 	struct comvars *ip, *op;
   1798 
   1799 	abrtflag = 0;
   1800 	oldintr = xsignal(SIGINT, psabort);
   1801 	if (flag) {
   1802 		if (proxy)
   1803 			return;
   1804 		ip = &tmpstruct;
   1805 		op = &proxstruct;
   1806 		proxy++;
   1807 	} else {
   1808 		if (!proxy)
   1809 			return;
   1810 		ip = &proxstruct;
   1811 		op = &tmpstruct;
   1812 		proxy = 0;
   1813 	}
   1814 	ip->connect = connected;
   1815 	connected = op->connect;
   1816 	if (hostname)
   1817 		(void)strlcpy(ip->name, hostname, sizeof(ip->name));
   1818 	else
   1819 		ip->name[0] = '\0';
   1820 	hostname = op->name;
   1821 	ip->hctl = hisctladdr;
   1822 	hisctladdr = op->hctl;
   1823 	ip->mctl = myctladdr;
   1824 	myctladdr = op->mctl;
   1825 	ip->in = cin;
   1826 	cin = op->in;
   1827 	ip->out = cout;
   1828 	cout = op->out;
   1829 	ip->tpe = type;
   1830 	type = op->tpe;
   1831 	ip->curtpe = curtype;
   1832 	curtype = op->curtpe;
   1833 	ip->cpnd = cpend;
   1834 	cpend = op->cpnd;
   1835 	ip->sunqe = sunique;
   1836 	sunique = op->sunqe;
   1837 	ip->runqe = runique;
   1838 	runique = op->runqe;
   1839 	ip->mcse = mcase;
   1840 	mcase = op->mcse;
   1841 	ip->ntflg = ntflag;
   1842 	ntflag = op->ntflg;
   1843 	(void)strlcpy(ip->nti, ntin, sizeof(ip->nti));
   1844 	(void)strlcpy(ntin, op->nti, sizeof(ntin));
   1845 	(void)strlcpy(ip->nto, ntout, sizeof(ip->nto));
   1846 	(void)strlcpy(ntout, op->nto, sizeof(ntout));
   1847 	ip->mapflg = mapflag;
   1848 	mapflag = op->mapflg;
   1849 	(void)strlcpy(ip->mi, mapin, sizeof(ip->mi));
   1850 	(void)strlcpy(mapin, op->mi, sizeof(mapin));
   1851 	(void)strlcpy(ip->mo, mapout, sizeof(ip->mo));
   1852 	(void)strlcpy(mapout, op->mo, sizeof(mapout));
   1853 	(void)xsignal(SIGINT, oldintr);
   1854 	if (abrtflag) {
   1855 		abrtflag = 0;
   1856 		(*oldintr)(SIGINT);
   1857 	}
   1858 }
   1859 
   1860 void
   1861 abortpt(notused)
   1862 	int notused;
   1863 {
   1864 
   1865 	alarmtimer(0);
   1866 	putc('\n', ttyout);
   1867 	ptabflg++;
   1868 	mflag = 0;
   1869 	abrtflag = 0;
   1870 	longjmp(ptabort, 1);
   1871 }
   1872 
   1873 void
   1874 proxtrans(cmd, local, remote)
   1875 	const char *cmd, *local, *remote;
   1876 {
   1877 	sig_t oldintr;
   1878 	int prox_type, nfnd;
   1879 	volatile int secndflag;
   1880 	char *cmd2;
   1881 
   1882 #ifdef __GNUC__			/* to shut up gcc warnings */
   1883 	(void)&oldintr;
   1884 	(void)&cmd2;
   1885 #endif
   1886 
   1887 	oldintr = NULL;
   1888 	secndflag = 0;
   1889 	if (strcmp(cmd, "RETR"))
   1890 		cmd2 = "RETR";
   1891 	else
   1892 		cmd2 = runique ? "STOU" : "STOR";
   1893 	if ((prox_type = type) == 0) {
   1894 		if (unix_server && unix_proxy)
   1895 			prox_type = TYPE_I;
   1896 		else
   1897 			prox_type = TYPE_A;
   1898 	}
   1899 	if (curtype != prox_type)
   1900 		changetype(prox_type, 1);
   1901 	if (command("PASV") != COMPLETE) {
   1902 		fputs("proxy server does not support third party transfers.\n",
   1903 		    ttyout);
   1904 		return;
   1905 	}
   1906 	pswitch(0);
   1907 	if (!connected) {
   1908 		fputs("No primary connection.\n", ttyout);
   1909 		pswitch(1);
   1910 		code = -1;
   1911 		return;
   1912 	}
   1913 	if (curtype != prox_type)
   1914 		changetype(prox_type, 1);
   1915 	if (command("PORT %s", pasv) != COMPLETE) {
   1916 		pswitch(1);
   1917 		return;
   1918 	}
   1919 	if (setjmp(ptabort))
   1920 		goto abort;
   1921 	oldintr = xsignal(SIGINT, abortpt);
   1922 	if ((restart_point &&
   1923 #ifndef NO_QUAD
   1924 	    (command("REST %lld", (long long) restart_point) != CONTINUE)
   1925 #else
   1926 	    (command("REST %ld", (long) restart_point) != CONTINUE)
   1927 #endif
   1928 	    ) || (command("%s %s", cmd, remote) != PRELIM)) {
   1929 		(void)xsignal(SIGINT, oldintr);
   1930 		pswitch(1);
   1931 		return;
   1932 	}
   1933 	sleep(2);
   1934 	pswitch(1);
   1935 	secndflag++;
   1936 	if ((restart_point &&
   1937 #ifndef NO_QUAD
   1938 	    (command("REST %lld", (long long) restart_point) != CONTINUE)
   1939 #else
   1940 	    (command("REST %ld", (long) restart_point) != CONTINUE)
   1941 #endif
   1942 	    ) || (command("%s %s", cmd2, local) != PRELIM))
   1943 		goto abort;
   1944 	ptflag++;
   1945 	(void)getreply(0);
   1946 	pswitch(0);
   1947 	(void)getreply(0);
   1948 	(void)xsignal(SIGINT, oldintr);
   1949 	pswitch(1);
   1950 	ptflag = 0;
   1951 	fprintf(ttyout, "local: %s remote: %s\n", local, remote);
   1952 	return;
   1953 abort:
   1954 	(void)xsignal(SIGINT, SIG_IGN);
   1955 	ptflag = 0;
   1956 	if (strcmp(cmd, "RETR") && !proxy)
   1957 		pswitch(1);
   1958 	else if (!strcmp(cmd, "RETR") && proxy)
   1959 		pswitch(0);
   1960 	if (!cpend && !secndflag) {  /* only here if cmd = "STOR" (proxy=1) */
   1961 		if (command("%s %s", cmd2, local) != PRELIM) {
   1962 			pswitch(0);
   1963 			if (cpend)
   1964 				abort_remote(NULL);
   1965 		}
   1966 		pswitch(1);
   1967 		if (ptabflg)
   1968 			code = -1;
   1969 		(void)xsignal(SIGINT, oldintr);
   1970 		return;
   1971 	}
   1972 	if (cpend)
   1973 		abort_remote(NULL);
   1974 	pswitch(!proxy);
   1975 	if (!cpend && !secndflag) {  /* only if cmd = "RETR" (proxy=1) */
   1976 		if (command("%s %s", cmd2, local) != PRELIM) {
   1977 			pswitch(0);
   1978 			if (cpend)
   1979 				abort_remote(NULL);
   1980 			pswitch(1);
   1981 			if (ptabflg)
   1982 				code = -1;
   1983 			(void)xsignal(SIGINT, oldintr);
   1984 			return;
   1985 		}
   1986 	}
   1987 	if (cpend)
   1988 		abort_remote(NULL);
   1989 	pswitch(!proxy);
   1990 	if (cpend) {
   1991 		if ((nfnd = empty(cin, NULL, 10)) <= 0) {
   1992 			if (nfnd < 0) {
   1993 				warn("abort");
   1994 			}
   1995 			if (ptabflg)
   1996 				code = -1;
   1997 			lostpeer();
   1998 		}
   1999 		(void)getreply(0);
   2000 		(void)getreply(0);
   2001 	}
   2002 	if (proxy)
   2003 		pswitch(0);
   2004 	pswitch(1);
   2005 	if (ptabflg)
   2006 		code = -1;
   2007 	(void)xsignal(SIGINT, oldintr);
   2008 }
   2009 
   2010 void
   2011 reset(argc, argv)
   2012 	int argc;
   2013 	char *argv[];
   2014 {
   2015 	int nfnd = 1;
   2016 
   2017 	while (nfnd > 0) {
   2018 		if ((nfnd = empty(cin, NULL, 0)) < 0) {
   2019 			warn("reset");
   2020 			code = -1;
   2021 			lostpeer();
   2022 		}
   2023 		else if (nfnd) {
   2024 			(void)getreply(0);
   2025 		}
   2026 	}
   2027 }
   2028 
   2029 char *
   2030 gunique(local)
   2031 	const char *local;
   2032 {
   2033 	static char new[MAXPATHLEN];
   2034 	char *cp = strrchr(local, '/');
   2035 	int d, count=0, len;
   2036 	char ext = '1';
   2037 
   2038 	if (cp)
   2039 		*cp = '\0';
   2040 	d = access(cp == local ? "/" : cp ? local : ".", W_OK);
   2041 	if (cp)
   2042 		*cp = '/';
   2043 	if (d < 0) {
   2044 		warn("local: %s", local);
   2045 		return (NULL);
   2046 	}
   2047 	len = strlcpy(new, local, sizeof(new));
   2048 	cp = &new[len];
   2049 	*cp++ = '.';
   2050 	while (!d) {
   2051 		if (++count == 100) {
   2052 			fputs("runique: can't find unique file name.\n",
   2053 			    ttyout);
   2054 			return (NULL);
   2055 		}
   2056 		*cp++ = ext;
   2057 		*cp = '\0';
   2058 		if (ext == '9')
   2059 			ext = '0';
   2060 		else
   2061 			ext++;
   2062 		if ((d = access(new, F_OK)) < 0)
   2063 			break;
   2064 		if (ext != '0')
   2065 			cp--;
   2066 		else if (*(cp - 2) == '.')
   2067 			*(cp - 1) = '1';
   2068 		else {
   2069 			*(cp - 2) = *(cp - 2) + 1;
   2070 			cp--;
   2071 		}
   2072 	}
   2073 	return (new);
   2074 }
   2075 
   2076 void
   2077 abort_remote(din)
   2078 	FILE *din;
   2079 {
   2080 	char buf[BUFSIZ];
   2081 	int nfnd;
   2082 
   2083 	if (cout == NULL) {
   2084 		warnx("Lost control connection for abort.");
   2085 		if (ptabflg)
   2086 			code = -1;
   2087 		lostpeer();
   2088 		return;
   2089 	}
   2090 	/*
   2091 	 * send IAC in urgent mode instead of DM because 4.3BSD places oob mark
   2092 	 * after urgent byte rather than before as is protocol now
   2093 	 */
   2094 	buf[0] = IAC;
   2095 	buf[1] = IP;
   2096 	buf[2] = IAC;
   2097 	if (send(fileno(cout), buf, 3, MSG_OOB) != 3)
   2098 		warn("abort");
   2099 	fprintf(cout, "%cABOR\r\n", DM);
   2100 	(void)fflush(cout);
   2101 	if ((nfnd = empty(cin, din, 10)) <= 0) {
   2102 		if (nfnd < 0) {
   2103 			warn("abort");
   2104 		}
   2105 		if (ptabflg)
   2106 			code = -1;
   2107 		lostpeer();
   2108 	}
   2109 	if (din && (nfnd & 2)) {
   2110 		while (read(fileno(din), buf, BUFSIZ) > 0)
   2111 			continue;
   2112 	}
   2113 	if (getreply(0) == ERROR && code == 552) {
   2114 		/* 552 needed for nic style abort */
   2115 		(void)getreply(0);
   2116 	}
   2117 	(void)getreply(0);
   2118 }
   2119