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