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