lobase

Linux port of OpenBSDs userland.

ftp.c (47221B)


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