Home | History | Annotate | Line # | Download | only in ftp
ftp.c revision 1.81
      1 /*	$NetBSD: ftp.c,v 1.81 1999/10/09 03:00:56 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.81 1999/10/09 03:00:56 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 jmp_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 jmp_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 	(void)xsignal(SIGINT, SIG_IGN);
   1333 	if (!cpend) {
   1334 		code = -1;
   1335 		goto cleanuprecv;
   1336 	}
   1337 	abort_remote(din);
   1338 	code = -1;
   1339 	if (bytes > 0)
   1340 		ptransfer(0);
   1341 
   1342 cleanuprecv:
   1343 	if (oldintr)
   1344 		(void)xsignal(SIGINT, oldintr);
   1345 	if (oldintp)
   1346 		(void)xsignal(SIGPIPE, oldintp);
   1347 	if (data >= 0) {
   1348 		(void)close(data);
   1349 		data = -1;
   1350 	}
   1351 	if (closefunc != NULL && fout != NULL)
   1352 		(*closefunc)(fout);
   1353 	if (din)
   1354 		(void)fclose(din);
   1355 	progress = oprogress;
   1356 	preserve = opreserve;
   1357 	bytes = 0;
   1358 }
   1359 
   1360 /*
   1361  * Need to start a listen on the data channel before we send the command,
   1362  * otherwise the server's connect may fail.
   1363  */
   1364 int
   1365 initconn()
   1366 {
   1367 	char *p, *a;
   1368 	int result, len, tmpno = 0;
   1369 	int on = 1;
   1370 	int error;
   1371 	u_int addr[16], port[2];
   1372 	u_int af, hal, pal;
   1373 	char *pasvcmd = NULL;
   1374 
   1375 #ifdef INET6
   1376 	if (myctladdr.su_family == AF_INET6
   1377 	 && (IN6_IS_ADDR_LINKLOCAL(&myctladdr.su_sin6.sin6_addr)
   1378 	  || IN6_IS_ADDR_SITELOCAL(&myctladdr.su_sin6.sin6_addr))) {
   1379 		warnx("use of scoped address can be troublesome");
   1380 	}
   1381 #endif
   1382 reinit:
   1383 	if (passivemode) {
   1384 		data_addr = myctladdr;
   1385 		data = socket(data_addr.su_family, SOCK_STREAM, 0);
   1386 		if (data < 0) {
   1387 			warn("socket");
   1388 			return (1);
   1389 		}
   1390 		if ((options & SO_DEBUG) &&
   1391 		    setsockopt(data, SOL_SOCKET, SO_DEBUG, (char *)&on,
   1392 			       sizeof(on)) < 0)
   1393 			warn("setsockopt (ignored)");
   1394 		result = COMPLETE + 1;
   1395 		switch (data_addr.su_family) {
   1396 		case AF_INET:
   1397 			if (epsv4 && !epsv4bad) {
   1398 				result = command(pasvcmd = "EPSV");
   1399 				/*
   1400 				 * this code is to be friendly with broken
   1401 				 * BSDI ftpd
   1402 				 */
   1403 				if (code / 10 == 22 && code != 229) {
   1404 					fputs(
   1405 "wrong server: return code must be 229\n",
   1406 						ttyout);
   1407 					result = COMPLETE + 1;
   1408 				}
   1409 				if (result != COMPLETE) {
   1410 					epsv4bad = 1;
   1411 					if (debug)
   1412 						fputs(
   1413 					"disabling epsv4 for this connection\n",
   1414 						    ttyout);
   1415 				}
   1416 			}
   1417 			if (result != COMPLETE)
   1418 				result = command(pasvcmd = "PASV");
   1419 			break;
   1420 #ifdef INET6
   1421 		case AF_INET6:
   1422 			result = command(pasvcmd = "EPSV");
   1423 			/* this code is to be friendly with broken BSDI ftpd */
   1424 			if (code / 10 == 22 && code != 229) {
   1425 				fputs(
   1426 "wrong server: return code must be 229\n",
   1427 					ttyout);
   1428 				result = COMPLETE + 1;
   1429 			}
   1430 			if (result != COMPLETE)
   1431 				result = command(pasvcmd = "LPSV");
   1432 			break;
   1433 #endif
   1434 		default:
   1435 			result = COMPLETE + 1;
   1436 			break;
   1437 		}
   1438 		if (result != COMPLETE) {
   1439 			if (activefallback) {
   1440 				(void)close(data);
   1441 				data = -1;
   1442 				passivemode = 0;
   1443 				activefallback = 0;
   1444 				goto reinit;
   1445 			}
   1446 			fputs("Passive mode refused.\n", ttyout);
   1447 			goto bad;
   1448 		}
   1449 
   1450 #define pack2(var, off) \
   1451 	(((var[(off) + 0] & 0xff) << 8) | ((var[(off) + 1] & 0xff) << 0))
   1452 #define pack4(var, off) \
   1453 	(((var[(off) + 0] & 0xff) << 24) | ((var[(off) + 1] & 0xff) << 16) | \
   1454 	 ((var[(off) + 2] & 0xff) << 8) | ((var[(off) + 3] & 0xff) << 0))
   1455 
   1456 		/*
   1457 		 * What we've got at this point is a string of comma separated
   1458 		 * one-byte unsigned integer values, separated by commas.
   1459 		 */
   1460 		if (strcmp(pasvcmd, "PASV") == 0) {
   1461 			if (data_addr.su_family != AF_INET) {
   1462 				fputs(
   1463 "Passive mode AF mismatch. Shouldn't happen!\n", ttyout);
   1464 				error = 1;
   1465 				goto bad;
   1466 			}
   1467 			if (code / 10 == 22 && code != 227) {
   1468 				fputs("wrong server: return code must be 227\n",
   1469 					ttyout);
   1470 				error = 1;
   1471 				goto bad;
   1472 			}
   1473 			error = sscanf(pasv, "%u,%u,%u,%u,%u,%u",
   1474 					&addr[0], &addr[1], &addr[2], &addr[3],
   1475 					&port[0], &port[1]);
   1476 			if (error != 6) {
   1477 				fputs(
   1478 "Passive mode address scan failure. Shouldn't happen!\n", ttyout);
   1479 				error = 1;
   1480 				goto bad;
   1481 			}
   1482 			error = 0;
   1483 			memset(&data_addr, 0, sizeof(data_addr));
   1484 			data_addr.su_family = AF_INET;
   1485 			data_addr.su_len = sizeof(struct sockaddr_in);
   1486 			data_addr.su_sin.sin_addr.s_addr =
   1487 				htonl(pack4(addr, 0));
   1488 			data_addr.su_port = htons(pack2(port, 0));
   1489 		} else if (strcmp(pasvcmd, "LPSV") == 0) {
   1490 			if (code / 10 == 22 && code != 228) {
   1491 				fputs("wrong server: return code must be 228\n",
   1492 					ttyout);
   1493 				error = 1;
   1494 				goto bad;
   1495 			}
   1496 			switch (data_addr.su_family) {
   1497 			case AF_INET:
   1498 				error = sscanf(pasv,
   1499 "%u,%u,%u,%u,%u,%u,%u,%u,%u",
   1500 					&af, &hal,
   1501 					&addr[0], &addr[1], &addr[2], &addr[3],
   1502 					&pal, &port[0], &port[1]);
   1503 				if (error != 9) {
   1504 					fputs(
   1505 "Passive mode address scan failure. Shouldn't happen!\n", ttyout);
   1506 					error = 1;
   1507 					goto bad;
   1508 				}
   1509 				if (af != 4 || hal != 4 || pal != 2) {
   1510 					fputs(
   1511 "Passive mode AF mismatch. Shouldn't happen!\n", ttyout);
   1512 					error = 1;
   1513 					goto bad;
   1514 				}
   1515 
   1516 				error = 0;
   1517 				memset(&data_addr, 0, sizeof(data_addr));
   1518 				data_addr.su_family = AF_INET;
   1519 				data_addr.su_len = sizeof(struct sockaddr_in);
   1520 				data_addr.su_sin.sin_addr.s_addr =
   1521 					htonl(pack4(addr, 0));
   1522 				data_addr.su_port = htons(pack2(port, 0));
   1523 				break;
   1524 #ifdef INET6
   1525 			case AF_INET6:
   1526 				error = sscanf(pasv,
   1527 "%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u",
   1528 					&af, &hal,
   1529 					&addr[0], &addr[1], &addr[2], &addr[3],
   1530 					&addr[4], &addr[5], &addr[6], &addr[7],
   1531 					&addr[8], &addr[9], &addr[10],
   1532 					&addr[11], &addr[12], &addr[13],
   1533 					&addr[14], &addr[15],
   1534 					&pal, &port[0], &port[1]);
   1535 				if (error != 21) {
   1536 					fputs(
   1537 "Passive mode address scan failure. Shouldn't happen!\n", ttyout);
   1538 					error = 1;
   1539 					goto bad;
   1540 				}
   1541 				if (af != 6 || hal != 16 || pal != 2) {
   1542 					fputs(
   1543 "Passive mode AF mismatch. Shouldn't happen!\n", ttyout);
   1544 					error = 1;
   1545 					goto bad;
   1546 				}
   1547 
   1548 				error = 0;
   1549 				memset(&data_addr, 0, sizeof(data_addr));
   1550 				data_addr.su_family = AF_INET6;
   1551 				data_addr.su_len = sizeof(struct sockaddr_in6);
   1552 			    {
   1553 				u_int32_t *p32;
   1554 				p32 = (u_int32_t *)&data_addr.su_sin6.sin6_addr;
   1555 				p32[0] = htonl(pack4(addr, 0));
   1556 				p32[1] = htonl(pack4(addr, 4));
   1557 				p32[2] = htonl(pack4(addr, 8));
   1558 				p32[3] = htonl(pack4(addr, 12));
   1559 			    }
   1560 				data_addr.su_port = htons(pack2(port, 0));
   1561 				break;
   1562 #endif
   1563 			default:
   1564 				error = 1;
   1565 			}
   1566 		} else if (strcmp(pasvcmd, "EPSV") == 0) {
   1567 			char delim[4];
   1568 
   1569 			port[0] = 0;
   1570 			if (code / 10 == 22 && code != 229) {
   1571 				fputs("wrong server: return code must be 229\n",
   1572 					ttyout);
   1573 				error = 1;
   1574 				goto bad;
   1575 			}
   1576 			if (sscanf(pasv, "%c%c%c%d%c", &delim[0],
   1577 					&delim[1], &delim[2], &port[1],
   1578 					&delim[3]) != 5) {
   1579 				fputs("parse error!\n", ttyout);
   1580 				error = 1;
   1581 				goto bad;
   1582 			}
   1583 			if (delim[0] != delim[1] || delim[0] != delim[2]
   1584 			 || delim[0] != delim[3]) {
   1585 				fputs("parse error!\n", ttyout);
   1586 				error = 1;
   1587 				goto bad;
   1588 			}
   1589 			data_addr = hisctladdr;
   1590 			data_addr.su_port = htons(port[1]);
   1591 		} else
   1592 			goto bad;
   1593 
   1594 		while (xconnect(data, (struct sockaddr *)&data_addr,
   1595 			    data_addr.su_len) < 0) {
   1596 			if (errno == EINTR)
   1597 				continue;
   1598 			if (activefallback) {
   1599 				(void)close(data);
   1600 				data = -1;
   1601 				passivemode = 0;
   1602 				activefallback = 0;
   1603 				goto reinit;
   1604 			}
   1605 			warn("connect");
   1606 			goto bad;
   1607 		}
   1608 #if defined(IPPROTO_IP) && defined(IP_TOS)
   1609 		if (data_addr.su_family == AF_INET) {
   1610 			on = IPTOS_THROUGHPUT;
   1611 			if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&on,
   1612 				       sizeof(int)) < 0)
   1613 				warn("setsockopt TOS (ignored)");
   1614 		}
   1615 #endif
   1616 		return (0);
   1617 	}
   1618 
   1619 noport:
   1620 	data_addr = myctladdr;
   1621 	if (sendport)
   1622 		data_addr.su_port = 0;	/* let system pick one */
   1623 	if (data != -1)
   1624 		(void)close(data);
   1625 	data = socket(data_addr.su_family, SOCK_STREAM, 0);
   1626 	if (data < 0) {
   1627 		warn("socket");
   1628 		if (tmpno)
   1629 			sendport = 1;
   1630 		return (1);
   1631 	}
   1632 	if (!sendport)
   1633 		if (setsockopt(data, SOL_SOCKET, SO_REUSEADDR, (char *)&on,
   1634 				sizeof(on)) < 0) {
   1635 			warn("setsockopt (reuse address)");
   1636 			goto bad;
   1637 		}
   1638 	if (bind(data, (struct sockaddr *)&data_addr, data_addr.su_len) < 0) {
   1639 		warn("bind");
   1640 		goto bad;
   1641 	}
   1642 	if (options & SO_DEBUG &&
   1643 	    setsockopt(data, SOL_SOCKET, SO_DEBUG, (char *)&on,
   1644 			sizeof(on)) < 0)
   1645 		warn("setsockopt (ignored)");
   1646 	len = sizeof(data_addr);
   1647 	if (getsockname(data, (struct sockaddr *)&data_addr, &len) < 0) {
   1648 		warn("getsockname");
   1649 		goto bad;
   1650 	}
   1651 	if (xlisten(data, 1) < 0)
   1652 		warn("listen");
   1653 
   1654 #define	UC(b)	(((int)b)&0xff)
   1655 
   1656 	if (sendport) {
   1657 #ifdef INET6
   1658 		char hname[INET6_ADDRSTRLEN];
   1659 		int af;
   1660 #endif
   1661 
   1662 		switch (data_addr.su_family) {
   1663 		case AF_INET:
   1664 			if (!epsv4 || epsv4bad) {
   1665 				result = COMPLETE + 1;
   1666 				break;
   1667 			}
   1668 			/* FALLTHROUGH */
   1669 #ifdef INET6
   1670 		case AF_INET6:
   1671 			af = (data_addr.su_family == AF_INET) ? 1 : 2;
   1672 			if (getnameinfo((struct sockaddr *)&data_addr,
   1673 					data_addr.su_len, hname, sizeof(hname),
   1674 					NULL, 0, NI_NUMERICHOST)) {
   1675 				result = ERROR;
   1676 			} else {
   1677 				result = command("EPRT |%d|%s|%d|", af, hname,
   1678 						ntohs(data_addr.su_port));
   1679 				if (result != COMPLETE) {
   1680 					epsv4bad = 1;
   1681 					if (debug)
   1682 						fputs(
   1683 					"disabling epsv4 for this connection\n",
   1684 						    ttyout);
   1685 				}
   1686 			}
   1687 			break;
   1688 #endif
   1689 		default:
   1690 			result = COMPLETE + 1;
   1691 			break;
   1692 		}
   1693 		if (result == COMPLETE)
   1694 			goto skip_port;
   1695 
   1696 		switch (data_addr.su_family) {
   1697 		case AF_INET:
   1698 			a = (char *)&data_addr.su_sin.sin_addr;
   1699 			p = (char *)&data_addr.su_port;
   1700 			result = command("PORT %d,%d,%d,%d,%d,%d",
   1701 				 UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
   1702 				 UC(p[0]), UC(p[1]));
   1703 			break;
   1704 #ifdef INET6
   1705 		case AF_INET6:
   1706 			a = (char *)&data_addr.su_sin6.sin6_addr;
   1707 			p = (char *)&data_addr.su_port;
   1708 			result = command(
   1709 "LPRT %d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
   1710 				 6, 16,
   1711 				 UC(a[0]),UC(a[1]),UC(a[2]),UC(a[3]),
   1712 				 UC(a[4]),UC(a[5]),UC(a[6]),UC(a[7]),
   1713 				 UC(a[8]),UC(a[9]),UC(a[10]),UC(a[11]),
   1714 				 UC(a[12]),UC(a[13]),UC(a[14]),UC(a[15]),
   1715 				 2, UC(p[0]), UC(p[1]));
   1716 			break;
   1717 #endif
   1718 		default:
   1719 			result = COMPLETE + 1; /* xxx */
   1720 		}
   1721 	skip_port:
   1722 
   1723 		if (result == ERROR && sendport == -1) {
   1724 			sendport = 0;
   1725 			tmpno = 1;
   1726 			goto noport;
   1727 		}
   1728 		return (result != COMPLETE);
   1729 	}
   1730 	if (tmpno)
   1731 		sendport = 1;
   1732 #if defined(IPPROTO_IP) && defined(IP_TOS)
   1733 	if (data_addr.su_family == AF_INET) {
   1734 		on = IPTOS_THROUGHPUT;
   1735 		if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&on,
   1736 			       sizeof(int)) < 0)
   1737 			warn("setsockopt TOS (ignored)");
   1738 	}
   1739 #endif
   1740 	return (0);
   1741 bad:
   1742 	(void)close(data), data = -1;
   1743 	if (tmpno)
   1744 		sendport = 1;
   1745 	return (1);
   1746 }
   1747 
   1748 FILE *
   1749 dataconn(lmode)
   1750 	const char *lmode;
   1751 {
   1752 	union sockunion from;
   1753 	int s, fromlen = myctladdr.su_len;
   1754 
   1755 	if (passivemode)
   1756 		return (fdopen(data, lmode));
   1757 
   1758 	s = accept(data, (struct sockaddr *) &from, &fromlen);
   1759 	if (s < 0) {
   1760 		warn("accept");
   1761 		(void)close(data), data = -1;
   1762 		return (NULL);
   1763 	}
   1764 	(void)close(data);
   1765 	data = s;
   1766 #if defined(IPPROTO_IP) && defined(IP_TOS)
   1767 	if (from.su_family == AF_INET) {
   1768 		int tos = IPTOS_THROUGHPUT;
   1769 		if (setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&tos,
   1770 				sizeof(int)) < 0) {
   1771 			warn("setsockopt TOS (ignored)");
   1772 		}
   1773 	}
   1774 #endif
   1775 	return (fdopen(data, lmode));
   1776 }
   1777 
   1778 void
   1779 psabort(notused)
   1780 	int notused;
   1781 {
   1782 	int oerrno = errno;
   1783 
   1784 	alarmtimer(0);
   1785 	abrtflag++;
   1786 	errno = oerrno;
   1787 }
   1788 
   1789 void
   1790 pswitch(flag)
   1791 	int flag;
   1792 {
   1793 	sig_t oldintr;
   1794 	static struct comvars {
   1795 		int connect;
   1796 		char name[MAXHOSTNAMELEN];
   1797 		union sockunion mctl;
   1798 		union sockunion hctl;
   1799 		FILE *in;
   1800 		FILE *out;
   1801 		int tpe;
   1802 		int curtpe;
   1803 		int cpnd;
   1804 		int sunqe;
   1805 		int runqe;
   1806 		int mcse;
   1807 		int ntflg;
   1808 		char nti[17];
   1809 		char nto[17];
   1810 		int mapflg;
   1811 		char mi[MAXPATHLEN];
   1812 		char mo[MAXPATHLEN];
   1813 	} proxstruct, tmpstruct;
   1814 	struct comvars *ip, *op;
   1815 
   1816 	abrtflag = 0;
   1817 	oldintr = xsignal(SIGINT, psabort);
   1818 	if (flag) {
   1819 		if (proxy)
   1820 			return;
   1821 		ip = &tmpstruct;
   1822 		op = &proxstruct;
   1823 		proxy++;
   1824 	} else {
   1825 		if (!proxy)
   1826 			return;
   1827 		ip = &proxstruct;
   1828 		op = &tmpstruct;
   1829 		proxy = 0;
   1830 	}
   1831 	ip->connect = connected;
   1832 	connected = op->connect;
   1833 	if (hostname)
   1834 		(void)strlcpy(ip->name, hostname, sizeof(ip->name));
   1835 	else
   1836 		ip->name[0] = '\0';
   1837 	hostname = op->name;
   1838 	ip->hctl = hisctladdr;
   1839 	hisctladdr = op->hctl;
   1840 	ip->mctl = myctladdr;
   1841 	myctladdr = op->mctl;
   1842 	ip->in = cin;
   1843 	cin = op->in;
   1844 	ip->out = cout;
   1845 	cout = op->out;
   1846 	ip->tpe = type;
   1847 	type = op->tpe;
   1848 	ip->curtpe = curtype;
   1849 	curtype = op->curtpe;
   1850 	ip->cpnd = cpend;
   1851 	cpend = op->cpnd;
   1852 	ip->sunqe = sunique;
   1853 	sunique = op->sunqe;
   1854 	ip->runqe = runique;
   1855 	runique = op->runqe;
   1856 	ip->mcse = mcase;
   1857 	mcase = op->mcse;
   1858 	ip->ntflg = ntflag;
   1859 	ntflag = op->ntflg;
   1860 	(void)strlcpy(ip->nti, ntin, sizeof(ip->nti));
   1861 	(void)strlcpy(ntin, op->nti, sizeof(ntin));
   1862 	(void)strlcpy(ip->nto, ntout, sizeof(ip->nto));
   1863 	(void)strlcpy(ntout, op->nto, sizeof(ntout));
   1864 	ip->mapflg = mapflag;
   1865 	mapflag = op->mapflg;
   1866 	(void)strlcpy(ip->mi, mapin, sizeof(ip->mi));
   1867 	(void)strlcpy(mapin, op->mi, sizeof(mapin));
   1868 	(void)strlcpy(ip->mo, mapout, sizeof(ip->mo));
   1869 	(void)strlcpy(mapout, op->mo, sizeof(mapout));
   1870 	(void)xsignal(SIGINT, oldintr);
   1871 	if (abrtflag) {
   1872 		abrtflag = 0;
   1873 		(*oldintr)(SIGINT);
   1874 	}
   1875 }
   1876 
   1877 void
   1878 abortpt(notused)
   1879 	int notused;
   1880 {
   1881 
   1882 	alarmtimer(0);
   1883 	write(fileno(ttyout), "\n", 1);
   1884 	ptabflg++;
   1885 	mflag = 0;
   1886 	abrtflag = 0;
   1887 	siglongjmp(ptabort, 1);
   1888 }
   1889 
   1890 void
   1891 proxtrans(cmd, local, remote)
   1892 	const char *cmd, *local, *remote;
   1893 {
   1894 	sig_t oldintr;
   1895 	int prox_type, nfnd;
   1896 	volatile int secndflag;
   1897 	char *cmd2;
   1898 
   1899 #ifdef __GNUC__			/* to shut up gcc warnings */
   1900 	(void)&oldintr;
   1901 	(void)&cmd2;
   1902 #endif
   1903 
   1904 	oldintr = NULL;
   1905 	secndflag = 0;
   1906 	if (strcmp(cmd, "RETR"))
   1907 		cmd2 = "RETR";
   1908 	else
   1909 		cmd2 = runique ? "STOU" : "STOR";
   1910 	if ((prox_type = type) == 0) {
   1911 		if (unix_server && unix_proxy)
   1912 			prox_type = TYPE_I;
   1913 		else
   1914 			prox_type = TYPE_A;
   1915 	}
   1916 	if (curtype != prox_type)
   1917 		changetype(prox_type, 1);
   1918 	if (command("PASV") != COMPLETE) {
   1919 		fputs("proxy server does not support third party transfers.\n",
   1920 		    ttyout);
   1921 		return;
   1922 	}
   1923 	pswitch(0);
   1924 	if (!connected) {
   1925 		fputs("No primary connection.\n", ttyout);
   1926 		pswitch(1);
   1927 		code = -1;
   1928 		return;
   1929 	}
   1930 	if (curtype != prox_type)
   1931 		changetype(prox_type, 1);
   1932 	if (command("PORT %s", pasv) != COMPLETE) {
   1933 		pswitch(1);
   1934 		return;
   1935 	}
   1936 	if (sigsetjmp(ptabort, 1))
   1937 		goto abort;
   1938 	oldintr = xsignal(SIGINT, abortpt);
   1939 	if ((restart_point &&
   1940 #ifndef NO_QUAD
   1941 	    (command("REST %lld", (long long) restart_point) != CONTINUE)
   1942 #else
   1943 	    (command("REST %ld", (long) restart_point) != CONTINUE)
   1944 #endif
   1945 	    ) || (command("%s %s", cmd, remote) != PRELIM)) {
   1946 		(void)xsignal(SIGINT, oldintr);
   1947 		pswitch(1);
   1948 		return;
   1949 	}
   1950 	sleep(2);
   1951 	pswitch(1);
   1952 	secndflag++;
   1953 	if ((restart_point &&
   1954 #ifndef NO_QUAD
   1955 	    (command("REST %lld", (long long) restart_point) != CONTINUE)
   1956 #else
   1957 	    (command("REST %ld", (long) restart_point) != CONTINUE)
   1958 #endif
   1959 	    ) || (command("%s %s", cmd2, local) != PRELIM))
   1960 		goto abort;
   1961 	ptflag++;
   1962 	(void)getreply(0);
   1963 	pswitch(0);
   1964 	(void)getreply(0);
   1965 	(void)xsignal(SIGINT, oldintr);
   1966 	pswitch(1);
   1967 	ptflag = 0;
   1968 	fprintf(ttyout, "local: %s remote: %s\n", local, remote);
   1969 	return;
   1970 abort:
   1971 	(void)xsignal(SIGINT, SIG_IGN);
   1972 	ptflag = 0;
   1973 	if (strcmp(cmd, "RETR") && !proxy)
   1974 		pswitch(1);
   1975 	else if (!strcmp(cmd, "RETR") && proxy)
   1976 		pswitch(0);
   1977 	if (!cpend && !secndflag) {  /* only here if cmd = "STOR" (proxy=1) */
   1978 		if (command("%s %s", cmd2, local) != PRELIM) {
   1979 			pswitch(0);
   1980 			if (cpend)
   1981 				abort_remote(NULL);
   1982 		}
   1983 		pswitch(1);
   1984 		if (ptabflg)
   1985 			code = -1;
   1986 		(void)xsignal(SIGINT, oldintr);
   1987 		return;
   1988 	}
   1989 	if (cpend)
   1990 		abort_remote(NULL);
   1991 	pswitch(!proxy);
   1992 	if (!cpend && !secndflag) {  /* only if cmd = "RETR" (proxy=1) */
   1993 		if (command("%s %s", cmd2, local) != PRELIM) {
   1994 			pswitch(0);
   1995 			if (cpend)
   1996 				abort_remote(NULL);
   1997 			pswitch(1);
   1998 			if (ptabflg)
   1999 				code = -1;
   2000 			(void)xsignal(SIGINT, oldintr);
   2001 			return;
   2002 		}
   2003 	}
   2004 	if (cpend)
   2005 		abort_remote(NULL);
   2006 	pswitch(!proxy);
   2007 	if (cpend) {
   2008 		if ((nfnd = empty(cin, NULL, 10)) <= 0) {
   2009 			if (nfnd < 0) {
   2010 				warn("abort");
   2011 			}
   2012 			if (ptabflg)
   2013 				code = -1;
   2014 			lostpeer();
   2015 		}
   2016 		(void)getreply(0);
   2017 		(void)getreply(0);
   2018 	}
   2019 	if (proxy)
   2020 		pswitch(0);
   2021 	pswitch(1);
   2022 	if (ptabflg)
   2023 		code = -1;
   2024 	(void)xsignal(SIGINT, oldintr);
   2025 }
   2026 
   2027 void
   2028 reset(argc, argv)
   2029 	int argc;
   2030 	char *argv[];
   2031 {
   2032 	int nfnd = 1;
   2033 
   2034 	while (nfnd > 0) {
   2035 		if ((nfnd = empty(cin, NULL, 0)) < 0) {
   2036 			warn("reset");
   2037 			code = -1;
   2038 			lostpeer();
   2039 		}
   2040 		else if (nfnd) {
   2041 			(void)getreply(0);
   2042 		}
   2043 	}
   2044 }
   2045 
   2046 char *
   2047 gunique(local)
   2048 	const char *local;
   2049 {
   2050 	static char new[MAXPATHLEN];
   2051 	char *cp = strrchr(local, '/');
   2052 	int d, count=0, len;
   2053 	char ext = '1';
   2054 
   2055 	if (cp)
   2056 		*cp = '\0';
   2057 	d = access(cp == local ? "/" : cp ? local : ".", W_OK);
   2058 	if (cp)
   2059 		*cp = '/';
   2060 	if (d < 0) {
   2061 		warn("local: %s", local);
   2062 		return (NULL);
   2063 	}
   2064 	len = strlcpy(new, local, sizeof(new));
   2065 	cp = &new[len];
   2066 	*cp++ = '.';
   2067 	while (!d) {
   2068 		if (++count == 100) {
   2069 			fputs("runique: can't find unique file name.\n",
   2070 			    ttyout);
   2071 			return (NULL);
   2072 		}
   2073 		*cp++ = ext;
   2074 		*cp = '\0';
   2075 		if (ext == '9')
   2076 			ext = '0';
   2077 		else
   2078 			ext++;
   2079 		if ((d = access(new, F_OK)) < 0)
   2080 			break;
   2081 		if (ext != '0')
   2082 			cp--;
   2083 		else if (*(cp - 2) == '.')
   2084 			*(cp - 1) = '1';
   2085 		else {
   2086 			*(cp - 2) = *(cp - 2) + 1;
   2087 			cp--;
   2088 		}
   2089 	}
   2090 	return (new);
   2091 }
   2092 
   2093 void
   2094 abort_remote(din)
   2095 	FILE *din;
   2096 {
   2097 	char buf[BUFSIZ];
   2098 	int nfnd;
   2099 
   2100 	if (cout == NULL) {
   2101 		warnx("Lost control connection for abort.");
   2102 		if (ptabflg)
   2103 			code = -1;
   2104 		lostpeer();
   2105 		return;
   2106 	}
   2107 	/*
   2108 	 * send IAC in urgent mode instead of DM because 4.3BSD places oob mark
   2109 	 * after urgent byte rather than before as is protocol now
   2110 	 */
   2111 	buf[0] = IAC;
   2112 	buf[1] = IP;
   2113 	buf[2] = IAC;
   2114 	if (send(fileno(cout), buf, 3, MSG_OOB) != 3)
   2115 		warn("abort");
   2116 	fprintf(cout, "%cABOR\r\n", DM);
   2117 	(void)fflush(cout);
   2118 	if ((nfnd = empty(cin, din, 10)) <= 0) {
   2119 		if (nfnd < 0) {
   2120 			warn("abort");
   2121 		}
   2122 		if (ptabflg)
   2123 			code = -1;
   2124 		lostpeer();
   2125 	}
   2126 	if (din && (nfnd & 2)) {
   2127 		while (read(fileno(din), buf, BUFSIZ) > 0)
   2128 			continue;
   2129 	}
   2130 	if (getreply(0) == ERROR && code == 552) {
   2131 		/* 552 needed for nic style abort */
   2132 		(void)getreply(0);
   2133 	}
   2134 	(void)getreply(0);
   2135 }
   2136