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