Home | History | Annotate | Line # | Download | only in ftp
ftp.c revision 1.44
      1 /*	$NetBSD: ftp.c,v 1.44 1999/06/02 02:03:58 lukem Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1985, 1989, 1993, 1994
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 #if 0
     39 static char sccsid[] = "@(#)ftp.c	8.6 (Berkeley) 10/27/94";
     40 #else
     41 __RCSID("$NetBSD: ftp.c,v 1.44 1999/06/02 02:03:58 lukem Exp $");
     42 #endif
     43 #endif /* not lint */
     44 
     45 #include <sys/types.h>
     46 #include <sys/stat.h>
     47 #include <sys/socket.h>
     48 #include <sys/time.h>
     49 
     50 #include <netinet/in.h>
     51 #include <netinet/in_systm.h>
     52 #include <netinet/ip.h>
     53 #include <arpa/inet.h>
     54 #include <arpa/ftp.h>
     55 #include <arpa/telnet.h>
     56 
     57 #include <ctype.h>
     58 #include <err.h>
     59 #include <errno.h>
     60 #include <netdb.h>
     61 #include <signal.h>
     62 #include <stdio.h>
     63 #include <stdlib.h>
     64 #include <string.h>
     65 #include <time.h>
     66 #include <unistd.h>
     67 #ifdef __STDC__
     68 #include <stdarg.h>
     69 #else
     70 #include <varargs.h>
     71 #endif
     72 
     73 #include "ftp_var.h"
     74 
     75 struct	sockaddr_in hisctladdr;
     76 struct	sockaddr_in data_addr;
     77 int	data = -1;
     78 int	abrtflag = 0;
     79 jmp_buf	ptabort;
     80 int	ptabflg;
     81 int	ptflag = 0;
     82 struct	sockaddr_in myctladdr;
     83 
     84 
     85 FILE	*cin, *cout;
     86 
     87 char *
     88 hookup(host, port)
     89 	const char *host;
     90 	in_port_t port;
     91 {
     92 	struct hostent *hp = NULL;
     93 	int s, len, tos;
     94 	static char hostnamebuf[MAXHOSTNAMELEN];
     95 
     96 	memset((void *)&hisctladdr, 0, sizeof(hisctladdr));
     97 	if (inet_aton(host, &hisctladdr.sin_addr) != 0) {
     98 		hisctladdr.sin_family = AF_INET;
     99 		(void)strncpy(hostnamebuf, host, sizeof(hostnamebuf) - 1);
    100 		hostnamebuf[sizeof(hostnamebuf) - 1] = '\0';
    101 	} else {
    102 		hp = gethostbyname(host);
    103 		if (hp == NULL) {
    104 			warnx("%s: %s", host, hstrerror(h_errno));
    105 			code = -1;
    106 			return (NULL);
    107 		}
    108 		hisctladdr.sin_family = hp->h_addrtype;
    109 		memcpy(&hisctladdr.sin_addr, hp->h_addr, hp->h_length);
    110 		(void)strncpy(hostnamebuf, hp->h_name, sizeof(hostnamebuf) - 1);
    111 		hostnamebuf[sizeof(hostnamebuf) - 1] = '\0';
    112 	}
    113 	hostname = hostnamebuf;
    114 	s = socket(hisctladdr.sin_family, SOCK_STREAM, 0);
    115 	if (s < 0) {
    116 		warn("socket");
    117 		code = -1;
    118 		return (0);
    119 	}
    120 	hisctladdr.sin_port = port;
    121 	while (xconnect(s, (struct sockaddr *)&hisctladdr,
    122 			sizeof(hisctladdr)) < 0) {
    123 		if (errno == EINTR)
    124 			continue;
    125 		if (hp && hp->h_addr_list[1]) {
    126 			int oerrno = errno;
    127 			char *ia;
    128 
    129 			ia = inet_ntoa(hisctladdr.sin_addr);
    130 			errno = oerrno;
    131 			warn("connect to address %s", ia);
    132 			hp->h_addr_list++;
    133 			memcpy(&hisctladdr.sin_addr, hp->h_addr, hp->h_length);
    134 			fprintf(ttyout, "Trying %s...\n",
    135 			    inet_ntoa(hisctladdr.sin_addr));
    136 			(void)close(s);
    137 			s = socket(hisctladdr.sin_family, SOCK_STREAM, 0);
    138 			if (s < 0) {
    139 				warn("socket");
    140 				code = -1;
    141 				return (0);
    142 			}
    143 			continue;
    144 		}
    145 		warn("connect");
    146 		code = -1;
    147 		goto bad;
    148 	}
    149 	len = sizeof(myctladdr);
    150 	if (getsockname(s, (struct sockaddr *)&myctladdr, &len) < 0) {
    151 		warn("getsockname");
    152 		code = -1;
    153 		goto bad;
    154 	}
    155 #ifdef IP_TOS
    156 	tos = IPTOS_LOWDELAY;
    157 	if (setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0)
    158 		warn("setsockopt TOS (ignored)");
    159 #endif
    160 	cin = fdopen(s, "r");
    161 	cout = fdopen(s, "w");
    162 	if (cin == NULL || cout == NULL) {
    163 		warnx("fdopen failed.");
    164 		if (cin)
    165 			(void)fclose(cin);
    166 		if (cout)
    167 			(void)fclose(cout);
    168 		code = -1;
    169 		goto bad;
    170 	}
    171 	if (verbose)
    172 		fprintf(ttyout, "Connected to %s.\n", hostname);
    173 	if (getreply(0) > 2) { 	/* read startup message from server */
    174 		if (cin)
    175 			(void)fclose(cin);
    176 		if (cout)
    177 			(void)fclose(cout);
    178 		code = -1;
    179 		goto bad;
    180 	}
    181 #ifdef SO_OOBINLINE
    182 	{
    183 	int on = 1;
    184 
    185 	if (setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (char *)&on, sizeof(on))
    186 		< 0 && debug) {
    187 			warn("setsockopt");
    188 		}
    189 	}
    190 #endif /* SO_OOBINLINE */
    191 
    192 	return (hostname);
    193 bad:
    194 	(void)close(s);
    195 	return (NULL);
    196 }
    197 
    198 void
    199 cmdabort(notused)
    200 	int notused;
    201 {
    202 
    203 	alarmtimer(0);
    204 	putc('\n', ttyout);
    205 	abrtflag++;
    206 	if (ptflag)
    207 		longjmp(ptabort, 1);
    208 }
    209 
    210 /*VARARGS*/
    211 int
    212 #ifdef __STDC__
    213 command(const char *fmt, ...)
    214 #else
    215 command(va_alist)
    216 	va_dcl
    217 #endif
    218 {
    219 	va_list ap;
    220 	int r;
    221 	sig_t oldintr;
    222 #ifndef __STDC__
    223 	const char *fmt;
    224 #endif
    225 
    226 	abrtflag = 0;
    227 	if (debug) {
    228 		fputs("---> ", ttyout);
    229 #ifdef __STDC__
    230 		va_start(ap, fmt);
    231 #else
    232 		va_start(ap);
    233 		fmt = va_arg(ap, const char *);
    234 #endif
    235 		if (strncmp("PASS ", fmt, 5) == 0)
    236 			fputs("PASS XXXX", ttyout);
    237 		else if (strncmp("ACCT ", fmt, 5) == 0)
    238 			fputs("ACCT XXXX", ttyout);
    239 		else
    240 			vfprintf(ttyout, fmt, ap);
    241 		va_end(ap);
    242 		putc('\n', ttyout);
    243 	}
    244 	if (cout == NULL) {
    245 		warnx("No control connection for command.");
    246 		code = -1;
    247 		return (0);
    248 	}
    249 	oldintr = signal(SIGINT, cmdabort);
    250 #ifdef __STDC__
    251 	va_start(ap, fmt);
    252 #else
    253 	va_start(ap);
    254 	fmt = va_arg(ap, char *);
    255 #endif
    256 	vfprintf(cout, fmt, ap);
    257 	va_end(ap);
    258 	fputs("\r\n", cout);
    259 	(void)fflush(cout);
    260 	cpend = 1;
    261 	r = getreply(!strcmp(fmt, "QUIT"));
    262 	if (abrtflag && oldintr != SIG_IGN)
    263 		(*oldintr)(SIGINT);
    264 	(void)signal(SIGINT, oldintr);
    265 	return (r);
    266 }
    267 
    268 char reply_string[BUFSIZ];		/* first line of previous reply */
    269 
    270 int
    271 getreply(expecteof)
    272 	int expecteof;
    273 {
    274 	char current_line[BUFSIZ];	/* last line of previous reply */
    275 	int c, n, line;
    276 	int dig;
    277 	int originalcode = 0, continuation = 0;
    278 	sig_t oldintr;
    279 	int pflag = 0;
    280 	char *cp, *pt = pasv;
    281 
    282 	oldintr = signal(SIGINT, cmdabort);
    283 	for (line = 0 ;; line++) {
    284 		dig = n = code = 0;
    285 		cp = current_line;
    286 		while ((c = getc(cin)) != '\n') {
    287 			if (c == IAC) {     /* handle telnet commands */
    288 				switch (c = getc(cin)) {
    289 				case WILL:
    290 				case WONT:
    291 					c = getc(cin);
    292 					fprintf(cout, "%c%c%c", IAC, DONT, c);
    293 					(void)fflush(cout);
    294 					break;
    295 				case DO:
    296 				case DONT:
    297 					c = getc(cin);
    298 					fprintf(cout, "%c%c%c", IAC, WONT, c);
    299 					(void)fflush(cout);
    300 					break;
    301 				default:
    302 					break;
    303 				}
    304 				continue;
    305 			}
    306 			dig++;
    307 			if (c == EOF) {
    308 				if (expecteof) {
    309 					(void)signal(SIGINT, oldintr);
    310 					code = 221;
    311 					return (0);
    312 				}
    313 				lostpeer();
    314 				if (verbose) {
    315 					fputs(
    316 	    "421 Service not available, remote server has closed connection.\n",
    317 					    ttyout);
    318 				}
    319 				code = 421;
    320 				return (4);
    321 			}
    322 			if (c != '\r' && (verbose > 0 ||
    323 			    ((verbose > -1 && n == '5' && dig > 4) &&
    324 			    (((!n && c < '5') || (n && n < '5'))
    325 			     || !retry_connect)))) {
    326 				if (proxflag &&
    327 				   (dig == 1 || (dig == 5 && verbose == 0)))
    328 					fprintf(ttyout, "%s:", hostname);
    329 				(void)putc(c, ttyout);
    330 			}
    331 			if (dig < 4 && isdigit(c))
    332 				code = code * 10 + (c - '0');
    333 			if (!pflag && code == 227)
    334 				pflag = 1;
    335 			if (dig > 4 && pflag == 1 && isdigit(c))
    336 				pflag = 2;
    337 			if (pflag == 2) {
    338 				if (c != '\r' && c != ')')
    339 					*pt++ = c;
    340 				else {
    341 					*pt = '\0';
    342 					pflag = 3;
    343 				}
    344 			}
    345 			if (dig == 4 && c == '-') {
    346 				if (continuation)
    347 					code = 0;
    348 				continuation++;
    349 			}
    350 			if (n == 0)
    351 				n = c;
    352 			if (cp < &current_line[sizeof(current_line) - 1])
    353 				*cp++ = c;
    354 		}
    355 		if (verbose > 0 || ((verbose > -1 && n == '5') &&
    356 		    (n < '5' || !retry_connect))) {
    357 			(void)putc(c, ttyout);
    358 			(void)fflush (ttyout);
    359 		}
    360 		if (line == 0) {
    361 			size_t len = cp - current_line;
    362 
    363 			if (len > sizeof(reply_string))
    364 				len = sizeof(reply_string);
    365 
    366 			(void)strncpy(reply_string, current_line, len);
    367 			reply_string[len] = '\0';
    368 		}
    369 		if (continuation && code != originalcode) {
    370 			if (originalcode == 0)
    371 				originalcode = code;
    372 			continue;
    373 		}
    374 		*cp = '\0';
    375 		if (n != '1')
    376 			cpend = 0;
    377 		(void)signal(SIGINT, oldintr);
    378 		if (code == 421 || originalcode == 421)
    379 			lostpeer();
    380 		if (abrtflag && oldintr != cmdabort && oldintr != SIG_IGN)
    381 			(*oldintr)(SIGINT);
    382 		return (n - '0');
    383 	}
    384 }
    385 
    386 int
    387 empty(mask, sec)
    388 	struct fd_set *mask;
    389 	int sec;
    390 {
    391 	struct timeval t;
    392 
    393 	t.tv_sec = (long) sec;
    394 	t.tv_usec = 0;
    395 	return (select(32, mask, NULL, NULL, &t));
    396 }
    397 
    398 jmp_buf	sendabort;
    399 
    400 void
    401 abortsend(notused)
    402 	int notused;
    403 {
    404 
    405 	alarmtimer(0);
    406 	mflag = 0;
    407 	abrtflag = 0;
    408 	fputs("\nsend aborted\nwaiting for remote to finish abort.\n", ttyout);
    409 	longjmp(sendabort, 1);
    410 }
    411 
    412 void
    413 sendrequest(cmd, local, remote, printnames)
    414 	const char *cmd, *local, *remote;
    415 	int printnames;
    416 {
    417 	struct stat st;
    418 	int c, d;
    419 	FILE *fin, *dout;
    420 	int (*closefunc) __P((FILE *));
    421 	sig_t oldinti, oldintr, oldintp;
    422 	volatile off_t hashbytes;
    423 	char *lmode, buf[BUFSIZ], *bufp;
    424 	int oprogress;
    425 
    426 #ifdef __GNUC__			/* to shut up gcc warnings */
    427 	(void)&fin;
    428 	(void)&dout;
    429 	(void)&closefunc;
    430 	(void)&oldinti;
    431 	(void)&oldintr;
    432 	(void)&oldintp;
    433 	(void)&lmode;
    434 #endif
    435 
    436 	hashbytes = mark;
    437 	direction = "sent";
    438 	dout = NULL;
    439 	bytes = 0;
    440 	filesize = -1;
    441 	oprogress = progress;
    442 	if (verbose && printnames) {
    443 		if (local && *local != '-')
    444 			fprintf(ttyout, "local: %s ", local);
    445 		if (remote)
    446 			fprintf(ttyout, "remote: %s\n", remote);
    447 	}
    448 	if (proxy) {
    449 		proxtrans(cmd, local, remote);
    450 		return;
    451 	}
    452 	if (curtype != type)
    453 		changetype(type, 0);
    454 	closefunc = NULL;
    455 	oldintr = NULL;
    456 	oldintp = NULL;
    457 	oldinti = NULL;
    458 	lmode = "w";
    459 	if (setjmp(sendabort)) {
    460 		while (cpend) {
    461 			(void)getreply(0);
    462 		}
    463 		if (data >= 0) {
    464 			(void)close(data);
    465 			data = -1;
    466 		}
    467 		if (oldintr)
    468 			(void)signal(SIGINT, oldintr);
    469 		if (oldintp)
    470 			(void)signal(SIGPIPE, oldintp);
    471 		if (oldinti)
    472 			(void)xsignal(SIGINFO, oldinti);
    473 		code = -1;
    474 		goto cleanupsend;
    475 	}
    476 	oldintr = signal(SIGINT, abortsend);
    477 	oldinti = xsignal(SIGINFO, psummary);
    478 	if (strcmp(local, "-") == 0) {
    479 		fin = stdin;
    480 		progress = 0;
    481 	} else if (*local == '|') {
    482 		oldintp = signal(SIGPIPE, SIG_IGN);
    483 		fin = popen(local + 1, "r");
    484 		if (fin == NULL) {
    485 			warn("%s", local + 1);
    486 			(void)signal(SIGINT, oldintr);
    487 			(void)signal(SIGPIPE, oldintp);
    488 			(void)xsignal(SIGINFO, oldinti);
    489 			code = -1;
    490 			goto cleanupsend;
    491 		}
    492 		progress = 0;
    493 		closefunc = pclose;
    494 	} else {
    495 		fin = fopen(local, "r");
    496 		if (fin == NULL) {
    497 			warn("local: %s", local);
    498 			(void)signal(SIGINT, oldintr);
    499 			(void)xsignal(SIGINFO, oldinti);
    500 			code = -1;
    501 			goto cleanupsend;
    502 		}
    503 		closefunc = fclose;
    504 		if (fstat(fileno(fin), &st) < 0 || !S_ISREG(st.st_mode)) {
    505 			fprintf(ttyout, "%s: not a plain file.\n", local);
    506 			(void)signal(SIGINT, oldintr);
    507 			(void)xsignal(SIGINFO, oldinti);
    508 			fclose(fin);
    509 			code = -1;
    510 			goto cleanupsend;
    511 		}
    512 		filesize = st.st_size;
    513 	}
    514 	if (initconn()) {
    515 		(void)signal(SIGINT, oldintr);
    516 		(void)xsignal(SIGINFO, oldinti);
    517 		if (oldintp)
    518 			(void)signal(SIGPIPE, oldintp);
    519 		code = -1;
    520 		if (closefunc != NULL)
    521 			(*closefunc)(fin);
    522 		goto cleanupsend;
    523 	}
    524 	if (setjmp(sendabort))
    525 		goto abort;
    526 
    527 	if (restart_point &&
    528 	    (strcmp(cmd, "STOR") == 0 || strcmp(cmd, "APPE") == 0)) {
    529 		int rc;
    530 
    531 		rc = -1;
    532 		switch (curtype) {
    533 		case TYPE_A:
    534 			rc = fseek(fin, (long) restart_point, SEEK_SET);
    535 			break;
    536 		case TYPE_I:
    537 		case TYPE_L:
    538 			rc = lseek(fileno(fin), restart_point, SEEK_SET);
    539 			break;
    540 		}
    541 		if (rc < 0) {
    542 			warn("local: %s", local);
    543 			if (closefunc != NULL)
    544 				(*closefunc)(fin);
    545 			goto cleanupsend;
    546 		}
    547 #ifndef NO_QUAD
    548 		if (command("REST %qd", (long long) restart_point) !=
    549 #else
    550 		if (command("REST %ld", (long) restart_point) !=
    551 #endif
    552 		    CONTINUE) {
    553 			if (closefunc != NULL)
    554 				(*closefunc)(fin);
    555 			goto cleanupsend;
    556 		}
    557 		lmode = "r+w";
    558 	}
    559 	if (remote) {
    560 		if (command("%s %s", cmd, remote) != PRELIM) {
    561 			(void)signal(SIGINT, oldintr);
    562 			(void)xsignal(SIGINFO, oldinti);
    563 			if (oldintp)
    564 				(void)signal(SIGPIPE, oldintp);
    565 			if (closefunc != NULL)
    566 				(*closefunc)(fin);
    567 			goto cleanupsend;
    568 		}
    569 	} else
    570 		if (command("%s", cmd) != PRELIM) {
    571 			(void)signal(SIGINT, oldintr);
    572 			(void)xsignal(SIGINFO, oldinti);
    573 			if (oldintp)
    574 				(void)signal(SIGPIPE, oldintp);
    575 			if (closefunc != NULL)
    576 				(*closefunc)(fin);
    577 			goto cleanupsend;
    578 		}
    579 	dout = dataconn(lmode);
    580 	if (dout == NULL)
    581 		goto abort;
    582 	progressmeter(-1);
    583 	oldintp = signal(SIGPIPE, SIG_IGN);
    584 	switch (curtype) {
    585 
    586 	case TYPE_I:
    587 	case TYPE_L:
    588 		errno = d = 0;
    589 		while ((c = read(fileno(fin), buf, sizeof(buf))) > 0) {
    590 			bytes += c;
    591 			for (bufp = buf; c > 0; c -= d, bufp += d)
    592 				if ((d = write(fileno(dout), bufp, c)) <= 0)
    593 					break;
    594 			if (hash && (!progress || filesize < 0) ) {
    595 				while (bytes >= hashbytes) {
    596 					(void)putc('#', ttyout);
    597 					hashbytes += mark;
    598 				}
    599 				(void)fflush(ttyout);
    600 			}
    601 		}
    602 		if (hash && (!progress || filesize < 0) && bytes > 0) {
    603 			if (bytes < mark)
    604 				(void)putc('#', ttyout);
    605 			(void)putc('\n', ttyout);
    606 		}
    607 		if (c < 0)
    608 			warn("local: %s", local);
    609 		if (d < 0) {
    610 			if (errno != EPIPE)
    611 				warn("netout");
    612 			bytes = -1;
    613 		}
    614 		break;
    615 
    616 	case TYPE_A:
    617 		while ((c = getc(fin)) != EOF) {
    618 			if (c == '\n') {
    619 				while (hash && (!progress || filesize < 0) &&
    620 				    (bytes >= hashbytes)) {
    621 					(void)putc('#', ttyout);
    622 					(void)fflush(ttyout);
    623 					hashbytes += mark;
    624 				}
    625 				if (ferror(dout))
    626 					break;
    627 				(void)putc('\r', dout);
    628 				bytes++;
    629 			}
    630 			(void)putc(c, dout);
    631 			bytes++;
    632 #if 0	/* this violates RFC */
    633 			if (c == '\r') {
    634 				(void)putc('\0', dout);
    635 				bytes++;
    636 			}
    637 #endif
    638 		}
    639 		if (hash && (!progress || filesize < 0)) {
    640 			if (bytes < hashbytes)
    641 				(void)putc('#', ttyout);
    642 			(void)putc('\n', ttyout);
    643 		}
    644 		if (ferror(fin))
    645 			warn("local: %s", local);
    646 		if (ferror(dout)) {
    647 			if (errno != EPIPE)
    648 				warn("netout");
    649 			bytes = -1;
    650 		}
    651 		break;
    652 	}
    653 	progressmeter(1);
    654 	if (closefunc != NULL)
    655 		(*closefunc)(fin);
    656 	(void)fclose(dout);
    657 	(void)getreply(0);
    658 	(void)signal(SIGINT, oldintr);
    659 	(void)xsignal(SIGINFO, oldinti);
    660 	if (oldintp)
    661 		(void)signal(SIGPIPE, oldintp);
    662 	if (bytes > 0)
    663 		ptransfer(0);
    664 	goto cleanupsend;
    665 abort:
    666 	(void)signal(SIGINT, oldintr);
    667 	(void)xsignal(SIGINFO, oldinti);
    668 	if (oldintp)
    669 		(void)signal(SIGPIPE, oldintp);
    670 	if (!cpend) {
    671 		code = -1;
    672 		return;
    673 	}
    674 	if (data >= 0) {
    675 		(void)close(data);
    676 		data = -1;
    677 	}
    678 	if (dout)
    679 		(void)fclose(dout);
    680 	(void)getreply(0);
    681 	code = -1;
    682 	if (closefunc != NULL && fin != NULL)
    683 		(*closefunc)(fin);
    684 	if (bytes > 0)
    685 		ptransfer(0);
    686 cleanupsend:
    687 	progress = oprogress;
    688 	restart_point = 0;
    689 }
    690 
    691 jmp_buf	recvabort;
    692 
    693 void
    694 abortrecv(notused)
    695 	int notused;
    696 {
    697 
    698 	alarmtimer(0);
    699 	mflag = 0;
    700 	abrtflag = 0;
    701 	fputs("\nreceive aborted\nwaiting for remote to finish abort.\n",
    702 	    ttyout);
    703 	longjmp(recvabort, 1);
    704 }
    705 
    706 void
    707 recvrequest(cmd, local, remote, lmode, printnames, ignorespecial)
    708 	const char *cmd, *local, *remote, *lmode;
    709 	int printnames, ignorespecial;
    710 {
    711 	FILE *fout, *din;
    712 	int (*closefunc) __P((FILE *));
    713 	sig_t oldinti, oldintr, oldintp;
    714 	int c, d;
    715 	volatile int is_retr, tcrflag, bare_lfs;
    716 	static size_t bufsize;
    717 	static char *buf;
    718 	volatile off_t hashbytes;
    719 	struct stat st;
    720 	time_t mtime;
    721 	struct timeval tval[2];
    722 	int oprogress;
    723 	int opreserve;
    724 
    725 #ifdef __GNUC__			/* to shut up gcc warnings */
    726 	(void)&local;
    727 	(void)&fout;
    728 	(void)&din;
    729 	(void)&closefunc;
    730 	(void)&oldinti;
    731 	(void)&oldintr;
    732 	(void)&oldintp;
    733 #endif
    734 
    735 	fout = NULL;
    736 	din = NULL;
    737 	oldinti = NULL;
    738 	hashbytes = mark;
    739 	direction = "received";
    740 	bytes = 0;
    741 	bare_lfs = 0;
    742 	filesize = -1;
    743 	oprogress = progress;
    744 	opreserve = preserve;
    745 	is_retr = (strcmp(cmd, "RETR") == 0);
    746 	if (is_retr && verbose && printnames) {
    747 		if (local && (ignorespecial || *local != '-'))
    748 			fprintf(ttyout, "local: %s ", local);
    749 		if (remote)
    750 			fprintf(ttyout, "remote: %s\n", remote);
    751 	}
    752 	if (proxy && is_retr) {
    753 		proxtrans(cmd, local, remote);
    754 		return;
    755 	}
    756 	closefunc = NULL;
    757 	oldintr = NULL;
    758 	oldintp = NULL;
    759 	tcrflag = !crflag && is_retr;
    760 	if (setjmp(recvabort)) {
    761 		while (cpend) {
    762 			(void)getreply(0);
    763 		}
    764 		if (data >= 0) {
    765 			(void)close(data);
    766 			data = -1;
    767 		}
    768 		if (oldintr)
    769 			(void)signal(SIGINT, oldintr);
    770 		if (oldinti)
    771 			(void)xsignal(SIGINFO, oldinti);
    772 		progress = oprogress;
    773 		preserve = opreserve;
    774 		code = -1;
    775 		return;
    776 	}
    777 	oldintr = signal(SIGINT, abortrecv);
    778 	oldinti = xsignal(SIGINFO, psummary);
    779 	if (ignorespecial || (strcmp(local, "-") && *local != '|')) {
    780 		if (access(local, W_OK) < 0) {
    781 			char *dir = strrchr(local, '/');
    782 
    783 			if (errno != ENOENT && errno != EACCES) {
    784 				warn("local: %s", local);
    785 				(void)signal(SIGINT, oldintr);
    786 				(void)xsignal(SIGINFO, oldinti);
    787 				code = -1;
    788 				return;
    789 			}
    790 			if (dir != NULL)
    791 				*dir = 0;
    792 			d = access(dir == local ? "/" : dir ? local : ".", W_OK);
    793 			if (dir != NULL)
    794 				*dir = '/';
    795 			if (d < 0) {
    796 				warn("local: %s", local);
    797 				(void)signal(SIGINT, oldintr);
    798 				(void)xsignal(SIGINFO, oldinti);
    799 				code = -1;
    800 				return;
    801 			}
    802 			if (!runique && errno == EACCES &&
    803 			    chmod(local, (S_IRUSR|S_IWUSR)) < 0) {
    804 				warn("local: %s", local);
    805 				(void)signal(SIGINT, oldintr);
    806 				(void)xsignal(SIGINFO, oldinti);
    807 				code = -1;
    808 				return;
    809 			}
    810 			if (runique && errno == EACCES &&
    811 			   (local = gunique(local)) == NULL) {
    812 				(void)signal(SIGINT, oldintr);
    813 				(void)xsignal(SIGINFO, oldinti);
    814 				code = -1;
    815 				return;
    816 			}
    817 		}
    818 		else if (runique && (local = gunique(local)) == NULL) {
    819 			(void)signal(SIGINT, oldintr);
    820 			(void)xsignal(SIGINFO, oldinti);
    821 			code = -1;
    822 			return;
    823 		}
    824 	}
    825 	if (!is_retr) {
    826 		if (curtype != TYPE_A)
    827 			changetype(TYPE_A, 0);
    828 	} else {
    829 		if (curtype != type)
    830 			changetype(type, 0);
    831 		filesize = remotesize(remote, 0);
    832 	}
    833 	if (initconn()) {
    834 		(void)signal(SIGINT, oldintr);
    835 		(void)xsignal(SIGINFO, oldinti);
    836 		code = -1;
    837 		return;
    838 	}
    839 	if (setjmp(recvabort))
    840 		goto abort;
    841 	if (is_retr && restart_point &&
    842 #ifndef NO_QUAD
    843 	    command("REST %qd", (long long) restart_point) != CONTINUE)
    844 #else
    845 	    command("REST %ld", (long) restart_point) != CONTINUE)
    846 #endif
    847 		return;
    848 	if (remote) {
    849 		if (command("%s %s", cmd, remote) != PRELIM) {
    850 			(void)signal(SIGINT, oldintr);
    851 			(void)xsignal(SIGINFO, oldinti);
    852 			return;
    853 		}
    854 	} else {
    855 		if (command("%s", cmd) != PRELIM) {
    856 			(void)signal(SIGINT, oldintr);
    857 			(void)xsignal(SIGINFO, oldinti);
    858 			return;
    859 		}
    860 	}
    861 	din = dataconn("r");
    862 	if (din == NULL)
    863 		goto abort;
    864 	if (!ignorespecial && strcmp(local, "-") == 0) {
    865 		fout = stdout;
    866 		progress = 0;
    867 		preserve = 0;
    868 	} else if (!ignorespecial && *local == '|') {
    869 		oldintp = signal(SIGPIPE, SIG_IGN);
    870 		fout = popen(local + 1, "w");
    871 		if (fout == NULL) {
    872 			warn("%s", local+1);
    873 			goto abort;
    874 		}
    875 		progress = 0;
    876 		preserve = 0;
    877 		closefunc = pclose;
    878 	} else {
    879 		fout = fopen(local, lmode);
    880 		if (fout == NULL) {
    881 			warn("local: %s", local);
    882 			goto abort;
    883 		}
    884 		closefunc = fclose;
    885 	}
    886 	if (fstat(fileno(fout), &st) < 0 || st.st_blksize == 0)
    887 		st.st_blksize = BUFSIZ;
    888 	if (st.st_blksize > bufsize) {
    889 		if (buf)
    890 			(void)free(buf);
    891 		bufsize = st.st_blksize;
    892 		buf = xmalloc(bufsize);
    893 	}
    894 	if (!S_ISREG(st.st_mode)) {
    895 		progress = 0;
    896 		preserve = 0;
    897 	}
    898 	progressmeter(-1);
    899 	switch (curtype) {
    900 
    901 	case TYPE_I:
    902 	case TYPE_L:
    903 		if (is_retr && restart_point &&
    904 		    lseek(fileno(fout), restart_point, SEEK_SET) < 0) {
    905 			warn("local: %s", local);
    906 			progress = oprogress;
    907 			preserve = opreserve;
    908 			if (closefunc != NULL)
    909 				(*closefunc)(fout);
    910 			return;
    911 		}
    912 		errno = d = 0;
    913 		while ((c = read(fileno(din), buf, bufsize)) > 0) {
    914 			if ((d = write(fileno(fout), buf, c)) != c)
    915 				break;
    916 			bytes += c;
    917 			if (hash && (!progress || filesize < 0)) {
    918 				while (bytes >= hashbytes) {
    919 					(void)putc('#', ttyout);
    920 					hashbytes += mark;
    921 				}
    922 				(void)fflush(ttyout);
    923 			}
    924 		}
    925 		if (hash && (!progress || filesize < 0) && bytes > 0) {
    926 			if (bytes < mark)
    927 				(void)putc('#', ttyout);
    928 			(void)putc('\n', ttyout);
    929 		}
    930 		if (c < 0) {
    931 			if (errno != EPIPE)
    932 				warn("netin");
    933 			bytes = -1;
    934 		}
    935 		if (d < c) {
    936 			if (d < 0)
    937 				warn("local: %s", local);
    938 			else
    939 				warnx("%s: short write", local);
    940 		}
    941 		break;
    942 
    943 	case TYPE_A:
    944 		if (is_retr && restart_point) {
    945 			int ch;
    946 			long i, n;
    947 
    948 			if (fseek(fout, 0L, SEEK_SET) < 0)
    949 				goto done;
    950 			n = (long)restart_point;
    951 			for (i = 0; i++ < n;) {
    952 				if ((ch = getc(fout)) == EOF)
    953 					goto done;
    954 				if (ch == '\n')
    955 					i++;
    956 			}
    957 			if (fseek(fout, 0L, SEEK_CUR) < 0) {
    958 done:
    959 				warn("local: %s", local);
    960 				progress = oprogress;
    961 				preserve = opreserve;
    962 				if (closefunc != NULL)
    963 					(*closefunc)(fout);
    964 				return;
    965 			}
    966 		}
    967 		while ((c = getc(din)) != EOF) {
    968 			if (c == '\n')
    969 				bare_lfs++;
    970 			while (c == '\r') {
    971 				while (hash && (!progress || filesize < 0) &&
    972 				    (bytes >= hashbytes)) {
    973 					(void)putc('#', ttyout);
    974 					(void)fflush(ttyout);
    975 					hashbytes += mark;
    976 				}
    977 				bytes++;
    978 				if ((c = getc(din)) != '\n' || tcrflag) {
    979 					if (ferror(fout))
    980 						goto break2;
    981 					(void)putc('\r', fout);
    982 					if (c == '\0') {
    983 						bytes++;
    984 						goto contin2;
    985 					}
    986 					if (c == EOF)
    987 						goto contin2;
    988 				}
    989 			}
    990 			(void)putc(c, fout);
    991 			bytes++;
    992 	contin2:	;
    993 		}
    994 break2:
    995 		if (hash && (!progress || filesize < 0)) {
    996 			if (bytes < hashbytes)
    997 				(void)putc('#', ttyout);
    998 			(void)putc('\n', ttyout);
    999 		}
   1000 		if (ferror(din)) {
   1001 			if (errno != EPIPE)
   1002 				warn("netin");
   1003 			bytes = -1;
   1004 		}
   1005 		if (ferror(fout))
   1006 			warn("local: %s", local);
   1007 		break;
   1008 	}
   1009 	progressmeter(1);
   1010 	if (closefunc != NULL)
   1011 		(*closefunc)(fout);
   1012 	(void)signal(SIGINT, oldintr);
   1013 	(void)xsignal(SIGINFO, oldinti);
   1014 	if (oldintp)
   1015 		(void)signal(SIGPIPE, oldintp);
   1016 	(void)fclose(din);
   1017 	(void)getreply(0);
   1018 	if (bare_lfs) {
   1019 		fprintf(ttyout,
   1020 		    "WARNING! %d bare linefeeds received in ASCII mode.\n",
   1021 		    bare_lfs);
   1022 		fputs("File may not have transferred correctly.\n", ttyout);
   1023 	}
   1024 	if (bytes >= 0 && is_retr) {
   1025 		if (bytes > 0)
   1026 			ptransfer(0);
   1027 		if (preserve && (closefunc == fclose)) {
   1028 			mtime = remotemodtime(remote, 0);
   1029 			if (mtime != -1) {
   1030 				(void)gettimeofday(&tval[0], NULL);
   1031 				tval[1].tv_sec = mtime;
   1032 				tval[1].tv_usec = 0;
   1033 				if (utimes(local, tval) == -1) {
   1034 					fprintf(ttyout,
   1035 				"Can't change modification time on %s to %s",
   1036 					    local, asctime(localtime(&mtime)));
   1037 				}
   1038 			}
   1039 		}
   1040 	}
   1041 	progress = oprogress;
   1042 	preserve = opreserve;
   1043 	return;
   1044 
   1045 abort:
   1046 
   1047 /* abort using RFC 959 recommended IP,SYNC sequence */
   1048 
   1049 	progress = oprogress;
   1050 	preserve = opreserve;
   1051 	if (oldintp)
   1052 		(void)signal(SIGPIPE, oldintp);
   1053 	(void)signal(SIGINT, SIG_IGN);
   1054 	if (!cpend) {
   1055 		code = -1;
   1056 		(void)signal(SIGINT, oldintr);
   1057 		(void)xsignal(SIGINFO, oldinti);
   1058 		return;
   1059 	}
   1060 
   1061 	abort_remote(din);
   1062 	code = -1;
   1063 	if (data >= 0) {
   1064 		(void)close(data);
   1065 		data = -1;
   1066 	}
   1067 	if (closefunc != NULL && fout != NULL)
   1068 		(*closefunc)(fout);
   1069 	if (din)
   1070 		(void)fclose(din);
   1071 	if (bytes > 0)
   1072 		ptransfer(0);
   1073 	(void)signal(SIGINT, oldintr);
   1074 	(void)xsignal(SIGINFO, oldinti);
   1075 }
   1076 
   1077 /*
   1078  * Need to start a listen on the data channel before we send the command,
   1079  * otherwise the server's connect may fail.
   1080  */
   1081 int
   1082 initconn()
   1083 {
   1084 	char *p, *a;
   1085 	int result, len, tmpno = 0;
   1086 	int on = 1;
   1087 	int a0, a1, a2, a3, p0, p1;
   1088 
   1089 reinit:
   1090 	if (passivemode) {
   1091 		data = socket(AF_INET, SOCK_STREAM, 0);
   1092 		if (data < 0) {
   1093 			warn("socket");
   1094 			return (1);
   1095 		}
   1096 		if ((options & SO_DEBUG) &&
   1097 		    setsockopt(data, SOL_SOCKET, SO_DEBUG, (char *)&on,
   1098 			       sizeof(on)) < 0)
   1099 			warn("setsockopt (ignored)");
   1100 		if (command("PASV") != COMPLETE) {
   1101 			if (activefallback) {
   1102 				(void)close(data);
   1103 				data = -1;
   1104 				passivemode = 0;
   1105 				activefallback = 0;
   1106 				goto reinit;
   1107 			}
   1108 			fputs("Passive mode refused.\n", ttyout);
   1109 			goto bad;
   1110 		}
   1111 
   1112 		/*
   1113 		 * What we've got at this point is a string of comma
   1114 		 * separated one-byte unsigned integer values.
   1115 		 * The first four are the an IP address. The fifth is
   1116 		 * the MSB of the port number, the sixth is the LSB.
   1117 		 * From that we'll prepare a sockaddr_in.
   1118 		 */
   1119 
   1120 		if (sscanf(pasv, "%d,%d,%d,%d,%d,%d",
   1121 			   &a0, &a1, &a2, &a3, &p0, &p1) != 6) {
   1122 			fputs(
   1123 		    "Passive mode address scan failure. Shouldn't happen!\n",
   1124 			    ttyout);
   1125 			goto bad;
   1126 		}
   1127 
   1128 		memset(&data_addr, 0, sizeof(data_addr));
   1129 		data_addr.sin_family = AF_INET;
   1130 		a = (char *)&data_addr.sin_addr.s_addr;
   1131 		a[0] = a0 & 0xff;
   1132 		a[1] = a1 & 0xff;
   1133 		a[2] = a2 & 0xff;
   1134 		a[3] = a3 & 0xff;
   1135 		p = (char *)&data_addr.sin_port;
   1136 		p[0] = p0 & 0xff;
   1137 		p[1] = p1 & 0xff;
   1138 
   1139 		while (xconnect(data, (struct sockaddr *)&data_addr,
   1140 			    sizeof(data_addr)) < 0) {
   1141 			if (errno == EINTR)
   1142 				continue;
   1143 			if (activefallback) {
   1144 				(void)close(data);
   1145 				data = -1;
   1146 				passivemode = 0;
   1147 				activefallback = 0;
   1148 				goto reinit;
   1149 			}
   1150 			warn("connect");
   1151 			goto bad;
   1152 		}
   1153 #ifdef IP_TOS
   1154 		on = IPTOS_THROUGHPUT;
   1155 		if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&on,
   1156 			       sizeof(int)) < 0)
   1157 			warn("setsockopt TOS (ignored)");
   1158 #endif
   1159 		return (0);
   1160 	}
   1161 
   1162 noport:
   1163 	data_addr = myctladdr;
   1164 	if (sendport)
   1165 		data_addr.sin_port = 0;	/* let system pick one */
   1166 	if (data != -1)
   1167 		(void)close(data);
   1168 	data = socket(AF_INET, SOCK_STREAM, 0);
   1169 	if (data < 0) {
   1170 		warn("socket");
   1171 		if (tmpno)
   1172 			sendport = 1;
   1173 		return (1);
   1174 	}
   1175 	if (!sendport)
   1176 		if (setsockopt(data, SOL_SOCKET, SO_REUSEADDR, (char *)&on,
   1177 				sizeof(on)) < 0) {
   1178 			warn("setsockopt (reuse address)");
   1179 			goto bad;
   1180 		}
   1181 	if (bind(data, (struct sockaddr *)&data_addr, sizeof(data_addr)) < 0) {
   1182 		warn("bind");
   1183 		goto bad;
   1184 	}
   1185 	if (options & SO_DEBUG &&
   1186 	    setsockopt(data, SOL_SOCKET, SO_DEBUG, (char *)&on,
   1187 			sizeof(on)) < 0)
   1188 		warn("setsockopt (ignored)");
   1189 	len = sizeof(data_addr);
   1190 	if (getsockname(data, (struct sockaddr *)&data_addr, &len) < 0) {
   1191 		warn("getsockname");
   1192 		goto bad;
   1193 	}
   1194 	if (xlisten(data, 1) < 0)
   1195 		warn("listen");
   1196 	if (sendport) {
   1197 		a = (char *)&data_addr.sin_addr;
   1198 		p = (char *)&data_addr.sin_port;
   1199 #define	UC(b)	(((int)b)&0xff)
   1200 		result =
   1201 		    command("PORT %d,%d,%d,%d,%d,%d",
   1202 		      UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
   1203 		      UC(p[0]), UC(p[1]));
   1204 		if (result == ERROR && sendport == -1) {
   1205 			sendport = 0;
   1206 			tmpno = 1;
   1207 			goto noport;
   1208 		}
   1209 		return (result != COMPLETE);
   1210 	}
   1211 	if (tmpno)
   1212 		sendport = 1;
   1213 #ifdef IP_TOS
   1214 	on = IPTOS_THROUGHPUT;
   1215 	if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0)
   1216 		warn("setsockopt TOS (ignored)");
   1217 #endif
   1218 	return (0);
   1219 bad:
   1220 	(void)close(data), data = -1;
   1221 	if (tmpno)
   1222 		sendport = 1;
   1223 	return (1);
   1224 }
   1225 
   1226 FILE *
   1227 dataconn(lmode)
   1228 	const char *lmode;
   1229 {
   1230 	struct sockaddr_in from;
   1231 	int s, fromlen, tos;
   1232 
   1233 	fromlen = sizeof(from);
   1234 
   1235 	if (passivemode)
   1236 		return (fdopen(data, lmode));
   1237 
   1238 	s = accept(data, (struct sockaddr *) &from, &fromlen);
   1239 	if (s < 0) {
   1240 		warn("accept");
   1241 		(void)close(data), data = -1;
   1242 		return (NULL);
   1243 	}
   1244 	(void)close(data);
   1245 	data = s;
   1246 #ifdef IP_TOS
   1247 	tos = IPTOS_THROUGHPUT;
   1248 	if (setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0)
   1249 		warn("setsockopt TOS (ignored)");
   1250 #endif
   1251 	return (fdopen(data, lmode));
   1252 }
   1253 
   1254 void
   1255 psummary(notused)
   1256 	int notused;
   1257 {
   1258 
   1259 	if (bytes > 0)
   1260 		ptransfer(1);
   1261 }
   1262 
   1263 void
   1264 psabort(notused)
   1265 	int notused;
   1266 {
   1267 
   1268 	alarmtimer(0);
   1269 	abrtflag++;
   1270 }
   1271 
   1272 void
   1273 pswitch(flag)
   1274 	int flag;
   1275 {
   1276 	sig_t oldintr;
   1277 	static struct comvars {
   1278 		int connect;
   1279 		char name[MAXHOSTNAMELEN];
   1280 		struct sockaddr_in mctl;
   1281 		struct sockaddr_in hctl;
   1282 		FILE *in;
   1283 		FILE *out;
   1284 		int tpe;
   1285 		int curtpe;
   1286 		int cpnd;
   1287 		int sunqe;
   1288 		int runqe;
   1289 		int mcse;
   1290 		int ntflg;
   1291 		char nti[17];
   1292 		char nto[17];
   1293 		int mapflg;
   1294 		char mi[MAXPATHLEN];
   1295 		char mo[MAXPATHLEN];
   1296 	} proxstruct, tmpstruct;
   1297 	struct comvars *ip, *op;
   1298 
   1299 	abrtflag = 0;
   1300 	oldintr = signal(SIGINT, psabort);
   1301 	if (flag) {
   1302 		if (proxy)
   1303 			return;
   1304 		ip = &tmpstruct;
   1305 		op = &proxstruct;
   1306 		proxy++;
   1307 	} else {
   1308 		if (!proxy)
   1309 			return;
   1310 		ip = &proxstruct;
   1311 		op = &tmpstruct;
   1312 		proxy = 0;
   1313 	}
   1314 	ip->connect = connected;
   1315 	connected = op->connect;
   1316 	if (hostname) {
   1317 		(void)strncpy(ip->name, hostname, sizeof(ip->name) - 1);
   1318 		ip->name[sizeof(ip->name) - 1] = '\0';
   1319 	} else
   1320 		ip->name[0] = '\0';
   1321 	hostname = op->name;
   1322 	ip->hctl = hisctladdr;
   1323 	hisctladdr = op->hctl;
   1324 	ip->mctl = myctladdr;
   1325 	myctladdr = op->mctl;
   1326 	ip->in = cin;
   1327 	cin = op->in;
   1328 	ip->out = cout;
   1329 	cout = op->out;
   1330 	ip->tpe = type;
   1331 	type = op->tpe;
   1332 	ip->curtpe = curtype;
   1333 	curtype = op->curtpe;
   1334 	ip->cpnd = cpend;
   1335 	cpend = op->cpnd;
   1336 	ip->sunqe = sunique;
   1337 	sunique = op->sunqe;
   1338 	ip->runqe = runique;
   1339 	runique = op->runqe;
   1340 	ip->mcse = mcase;
   1341 	mcase = op->mcse;
   1342 	ip->ntflg = ntflag;
   1343 	ntflag = op->ntflg;
   1344 	(void)strncpy(ip->nti, ntin, sizeof(ip->nti) - 1);
   1345 	(ip->nti)[sizeof(ip->nti) - 1] = '\0';
   1346 	(void)strcpy(ntin, op->nti);
   1347 	(void)strncpy(ip->nto, ntout, sizeof(ip->nto) - 1);
   1348 	(ip->nto)[sizeof(ip->nto) - 1] = '\0';
   1349 	(void)strcpy(ntout, op->nto);
   1350 	ip->mapflg = mapflag;
   1351 	mapflag = op->mapflg;
   1352 	(void)strncpy(ip->mi, mapin, sizeof(ip->mi) - 1);
   1353 	(ip->mi)[sizeof(ip->mi) - 1] = '\0';
   1354 	(void)strcpy(mapin, op->mi);
   1355 	(void)strncpy(ip->mo, mapout, sizeof(ip->mo) - 1);
   1356 	(ip->mo)[sizeof(ip->mo) - 1] = '\0';
   1357 	(void)strcpy(mapout, op->mo);
   1358 	(void)signal(SIGINT, oldintr);
   1359 	if (abrtflag) {
   1360 		abrtflag = 0;
   1361 		(*oldintr)(SIGINT);
   1362 	}
   1363 }
   1364 
   1365 void
   1366 abortpt(notused)
   1367 	int notused;
   1368 {
   1369 
   1370 	alarmtimer(0);
   1371 	putc('\n', ttyout);
   1372 	ptabflg++;
   1373 	mflag = 0;
   1374 	abrtflag = 0;
   1375 	longjmp(ptabort, 1);
   1376 }
   1377 
   1378 void
   1379 proxtrans(cmd, local, remote)
   1380 	const char *cmd, *local, *remote;
   1381 {
   1382 	sig_t oldintr;
   1383 	int prox_type, nfnd;
   1384 	volatile int secndflag;
   1385 	char *cmd2;
   1386 	struct fd_set mask;
   1387 
   1388 #ifdef __GNUC__			/* to shut up gcc warnings */
   1389 	(void)&oldintr;
   1390 	(void)&cmd2;
   1391 #endif
   1392 
   1393 	oldintr = NULL;
   1394 	secndflag = 0;
   1395 	if (strcmp(cmd, "RETR"))
   1396 		cmd2 = "RETR";
   1397 	else
   1398 		cmd2 = runique ? "STOU" : "STOR";
   1399 	if ((prox_type = type) == 0) {
   1400 		if (unix_server && unix_proxy)
   1401 			prox_type = TYPE_I;
   1402 		else
   1403 			prox_type = TYPE_A;
   1404 	}
   1405 	if (curtype != prox_type)
   1406 		changetype(prox_type, 1);
   1407 	if (command("PASV") != COMPLETE) {
   1408 		fputs("proxy server does not support third party transfers.\n",
   1409 		    ttyout);
   1410 		return;
   1411 	}
   1412 	pswitch(0);
   1413 	if (!connected) {
   1414 		fputs("No primary connection.\n", ttyout);
   1415 		pswitch(1);
   1416 		code = -1;
   1417 		return;
   1418 	}
   1419 	if (curtype != prox_type)
   1420 		changetype(prox_type, 1);
   1421 	if (command("PORT %s", pasv) != COMPLETE) {
   1422 		pswitch(1);
   1423 		return;
   1424 	}
   1425 	if (setjmp(ptabort))
   1426 		goto abort;
   1427 	oldintr = signal(SIGINT, abortpt);
   1428 	if ((restart_point &&
   1429 #ifndef NO_QUAD
   1430 	    (command("REST %qd", (long long) restart_point) != CONTINUE)
   1431 #else
   1432 	    (command("REST %ld", (long) restart_point) != CONTINUE)
   1433 #endif
   1434 	    ) || (command("%s %s", cmd, remote) != PRELIM)) {
   1435 		(void)signal(SIGINT, oldintr);
   1436 		pswitch(1);
   1437 		return;
   1438 	}
   1439 	sleep(2);
   1440 	pswitch(1);
   1441 	secndflag++;
   1442 	if ((restart_point &&
   1443 #ifndef NO_QUAD
   1444 	    (command("REST %qd", (long long) restart_point) != CONTINUE)
   1445 #else
   1446 	    (command("REST %ld", (long) restart_point) != CONTINUE)
   1447 #endif
   1448 	    ) || (command("%s %s", cmd2, local) != PRELIM))
   1449 		goto abort;
   1450 	ptflag++;
   1451 	(void)getreply(0);
   1452 	pswitch(0);
   1453 	(void)getreply(0);
   1454 	(void)signal(SIGINT, oldintr);
   1455 	pswitch(1);
   1456 	ptflag = 0;
   1457 	fprintf(ttyout, "local: %s remote: %s\n", local, remote);
   1458 	return;
   1459 abort:
   1460 	(void)signal(SIGINT, SIG_IGN);
   1461 	ptflag = 0;
   1462 	if (strcmp(cmd, "RETR") && !proxy)
   1463 		pswitch(1);
   1464 	else if (!strcmp(cmd, "RETR") && proxy)
   1465 		pswitch(0);
   1466 	if (!cpend && !secndflag) {  /* only here if cmd = "STOR" (proxy=1) */
   1467 		if (command("%s %s", cmd2, local) != PRELIM) {
   1468 			pswitch(0);
   1469 			if (cpend)
   1470 				abort_remote(NULL);
   1471 		}
   1472 		pswitch(1);
   1473 		if (ptabflg)
   1474 			code = -1;
   1475 		(void)signal(SIGINT, oldintr);
   1476 		return;
   1477 	}
   1478 	if (cpend)
   1479 		abort_remote(NULL);
   1480 	pswitch(!proxy);
   1481 	if (!cpend && !secndflag) {  /* only if cmd = "RETR" (proxy=1) */
   1482 		if (command("%s %s", cmd2, local) != PRELIM) {
   1483 			pswitch(0);
   1484 			if (cpend)
   1485 				abort_remote(NULL);
   1486 			pswitch(1);
   1487 			if (ptabflg)
   1488 				code = -1;
   1489 			(void)signal(SIGINT, oldintr);
   1490 			return;
   1491 		}
   1492 	}
   1493 	if (cpend)
   1494 		abort_remote(NULL);
   1495 	pswitch(!proxy);
   1496 	if (cpend) {
   1497 		FD_ZERO(&mask);
   1498 		FD_SET(fileno(cin), &mask);
   1499 		if ((nfnd = empty(&mask, 10)) <= 0) {
   1500 			if (nfnd < 0) {
   1501 				warn("abort");
   1502 			}
   1503 			if (ptabflg)
   1504 				code = -1;
   1505 			lostpeer();
   1506 		}
   1507 		(void)getreply(0);
   1508 		(void)getreply(0);
   1509 	}
   1510 	if (proxy)
   1511 		pswitch(0);
   1512 	pswitch(1);
   1513 	if (ptabflg)
   1514 		code = -1;
   1515 	(void)signal(SIGINT, oldintr);
   1516 }
   1517 
   1518 void
   1519 reset(argc, argv)
   1520 	int argc;
   1521 	char *argv[];
   1522 {
   1523 	struct fd_set mask;
   1524 	int nfnd = 1;
   1525 
   1526 	FD_ZERO(&mask);
   1527 	while (nfnd > 0) {
   1528 		FD_SET(fileno(cin), &mask);
   1529 		if ((nfnd = empty(&mask, 0)) < 0) {
   1530 			warn("reset");
   1531 			code = -1;
   1532 			lostpeer();
   1533 		}
   1534 		else if (nfnd) {
   1535 			(void)getreply(0);
   1536 		}
   1537 	}
   1538 }
   1539 
   1540 char *
   1541 gunique(local)
   1542 	const char *local;
   1543 {
   1544 	static char new[MAXPATHLEN];
   1545 	char *cp = strrchr(local, '/');
   1546 	int d, count=0;
   1547 	char ext = '1';
   1548 
   1549 	if (cp)
   1550 		*cp = '\0';
   1551 	d = access(cp == local ? "/" : cp ? local : ".", W_OK);
   1552 	if (cp)
   1553 		*cp = '/';
   1554 	if (d < 0) {
   1555 		warn("local: %s", local);
   1556 		return (NULL);
   1557 	}
   1558 	(void)strcpy(new, local);
   1559 	cp = new + strlen(new);
   1560 	*cp++ = '.';
   1561 	while (!d) {
   1562 		if (++count == 100) {
   1563 			fputs("runique: can't find unique file name.\n",
   1564 			    ttyout);
   1565 			return (NULL);
   1566 		}
   1567 		*cp++ = ext;
   1568 		*cp = '\0';
   1569 		if (ext == '9')
   1570 			ext = '0';
   1571 		else
   1572 			ext++;
   1573 		if ((d = access(new, F_OK)) < 0)
   1574 			break;
   1575 		if (ext != '0')
   1576 			cp--;
   1577 		else if (*(cp - 2) == '.')
   1578 			*(cp - 1) = '1';
   1579 		else {
   1580 			*(cp - 2) = *(cp - 2) + 1;
   1581 			cp--;
   1582 		}
   1583 	}
   1584 	return (new);
   1585 }
   1586 
   1587 void
   1588 abort_remote(din)
   1589 	FILE *din;
   1590 {
   1591 	char buf[BUFSIZ];
   1592 	int nfnd;
   1593 	struct fd_set mask;
   1594 
   1595 	if (cout == NULL) {
   1596 		warnx("Lost control connection for abort.");
   1597 		if (ptabflg)
   1598 			code = -1;
   1599 		lostpeer();
   1600 		return;
   1601 	}
   1602 	/*
   1603 	 * send IAC in urgent mode instead of DM because 4.3BSD places oob mark
   1604 	 * after urgent byte rather than before as is protocol now
   1605 	 */
   1606 	snprintf(buf, sizeof(buf), "%c%c%c", IAC, IP, IAC);
   1607 	if (send(fileno(cout), buf, 3, MSG_OOB) != 3)
   1608 		warn("abort");
   1609 	fprintf(cout, "%cABOR\r\n", DM);
   1610 	(void)fflush(cout);
   1611 	FD_ZERO(&mask);
   1612 	FD_SET(fileno(cin), &mask);
   1613 	if (din) {
   1614 		FD_SET(fileno(din), &mask);
   1615 	}
   1616 	if ((nfnd = empty(&mask, 10)) <= 0) {
   1617 		if (nfnd < 0) {
   1618 			warn("abort");
   1619 		}
   1620 		if (ptabflg)
   1621 			code = -1;
   1622 		lostpeer();
   1623 	}
   1624 	if (din && FD_ISSET(fileno(din), &mask)) {
   1625 		while (read(fileno(din), buf, BUFSIZ) > 0)
   1626 			/* LOOP */;
   1627 	}
   1628 	if (getreply(0) == ERROR && code == 552) {
   1629 		/* 552 needed for nic style abort */
   1630 		(void)getreply(0);
   1631 	}
   1632 	(void)getreply(0);
   1633 }
   1634