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