104203a83SThomas Cort /* $NetBSD: ftp.c,v 1.164 2012/07/04 06:09:37 is Exp $ */
204203a83SThomas Cort
304203a83SThomas Cort /*-
404203a83SThomas Cort * Copyright (c) 1996-2009 The NetBSD Foundation, Inc.
504203a83SThomas Cort * All rights reserved.
604203a83SThomas Cort *
704203a83SThomas Cort * This code is derived from software contributed to The NetBSD Foundation
804203a83SThomas Cort * by Luke Mewburn.
904203a83SThomas Cort *
1004203a83SThomas Cort * Redistribution and use in source and binary forms, with or without
1104203a83SThomas Cort * modification, are permitted provided that the following conditions
1204203a83SThomas Cort * are met:
1304203a83SThomas Cort * 1. Redistributions of source code must retain the above copyright
1404203a83SThomas Cort * notice, this list of conditions and the following disclaimer.
1504203a83SThomas Cort * 2. Redistributions in binary form must reproduce the above copyright
1604203a83SThomas Cort * notice, this list of conditions and the following disclaimer in the
1704203a83SThomas Cort * documentation and/or other materials provided with the distribution.
1804203a83SThomas Cort *
1904203a83SThomas Cort * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2004203a83SThomas Cort * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2104203a83SThomas Cort * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2204203a83SThomas Cort * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2304203a83SThomas Cort * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2404203a83SThomas Cort * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2504203a83SThomas Cort * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2604203a83SThomas Cort * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2704203a83SThomas Cort * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2804203a83SThomas Cort * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2904203a83SThomas Cort * POSSIBILITY OF SUCH DAMAGE.
3004203a83SThomas Cort */
3104203a83SThomas Cort
3204203a83SThomas Cort /*
3304203a83SThomas Cort * Copyright (c) 1985, 1989, 1993, 1994
3404203a83SThomas Cort * The Regents of the University of California. All rights reserved.
3504203a83SThomas Cort *
3604203a83SThomas Cort * Redistribution and use in source and binary forms, with or without
3704203a83SThomas Cort * modification, are permitted provided that the following conditions
3804203a83SThomas Cort * are met:
3904203a83SThomas Cort * 1. Redistributions of source code must retain the above copyright
4004203a83SThomas Cort * notice, this list of conditions and the following disclaimer.
4104203a83SThomas Cort * 2. Redistributions in binary form must reproduce the above copyright
4204203a83SThomas Cort * notice, this list of conditions and the following disclaimer in the
4304203a83SThomas Cort * documentation and/or other materials provided with the distribution.
4404203a83SThomas Cort * 3. Neither the name of the University nor the names of its contributors
4504203a83SThomas Cort * may be used to endorse or promote products derived from this software
4604203a83SThomas Cort * without specific prior written permission.
4704203a83SThomas Cort *
4804203a83SThomas Cort * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
4904203a83SThomas Cort * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5004203a83SThomas Cort * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5104203a83SThomas Cort * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
5204203a83SThomas Cort * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5304203a83SThomas Cort * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5404203a83SThomas Cort * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
5504203a83SThomas Cort * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
5604203a83SThomas Cort * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5704203a83SThomas Cort * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5804203a83SThomas Cort * SUCH DAMAGE.
5904203a83SThomas Cort */
6004203a83SThomas Cort
6104203a83SThomas Cort /*
6204203a83SThomas Cort * Copyright (C) 1997 and 1998 WIDE Project.
6304203a83SThomas Cort * All rights reserved.
6404203a83SThomas Cort *
6504203a83SThomas Cort * Redistribution and use in source and binary forms, with or without
6604203a83SThomas Cort * modification, are permitted provided that the following conditions
6704203a83SThomas Cort * are met:
6804203a83SThomas Cort * 1. Redistributions of source code must retain the above copyright
6904203a83SThomas Cort * notice, this list of conditions and the following disclaimer.
7004203a83SThomas Cort * 2. Redistributions in binary form must reproduce the above copyright
7104203a83SThomas Cort * notice, this list of conditions and the following disclaimer in the
7204203a83SThomas Cort * documentation and/or other materials provided with the distribution.
7304203a83SThomas Cort * 3. Neither the name of the project nor the names of its contributors
7404203a83SThomas Cort * may be used to endorse or promote products derived from this software
7504203a83SThomas Cort * without specific prior written permission.
7604203a83SThomas Cort *
7704203a83SThomas Cort * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
7804203a83SThomas Cort * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
7904203a83SThomas Cort * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
8004203a83SThomas Cort * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
8104203a83SThomas Cort * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
8204203a83SThomas Cort * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
8304203a83SThomas Cort * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
8404203a83SThomas Cort * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
8504203a83SThomas Cort * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
8604203a83SThomas Cort * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
8704203a83SThomas Cort * SUCH DAMAGE.
8804203a83SThomas Cort */
8904203a83SThomas Cort
9004203a83SThomas Cort #include <sys/cdefs.h>
9104203a83SThomas Cort #ifndef lint
9204203a83SThomas Cort #if 0
9304203a83SThomas Cort static char sccsid[] = "@(#)ftp.c 8.6 (Berkeley) 10/27/94";
9404203a83SThomas Cort #else
9504203a83SThomas Cort __RCSID("$NetBSD: ftp.c,v 1.164 2012/07/04 06:09:37 is Exp $");
9604203a83SThomas Cort #endif
9704203a83SThomas Cort #endif /* not lint */
9804203a83SThomas Cort
9904203a83SThomas Cort #include <sys/types.h>
10004203a83SThomas Cort #include <sys/stat.h>
10104203a83SThomas Cort #include <sys/socket.h>
10204203a83SThomas Cort #include <sys/time.h>
10304203a83SThomas Cort
10404203a83SThomas Cort #include <netinet/in.h>
105*84d9c625SLionel Sambuc #if !defined(__minix)
10604203a83SThomas Cort #include <netinet/in_systm.h>
10704203a83SThomas Cort #include <netinet/ip.h>
108*84d9c625SLionel Sambuc #endif /* !defined(__minix) */
10904203a83SThomas Cort #include <arpa/inet.h>
11004203a83SThomas Cort #include <arpa/ftp.h>
11104203a83SThomas Cort #include <arpa/telnet.h>
11204203a83SThomas Cort
11304203a83SThomas Cort #include <assert.h>
11404203a83SThomas Cort #include <ctype.h>
11504203a83SThomas Cort #include <err.h>
11604203a83SThomas Cort #include <errno.h>
11704203a83SThomas Cort #include <fcntl.h>
11804203a83SThomas Cort #include <netdb.h>
11904203a83SThomas Cort #include <stdio.h>
12004203a83SThomas Cort #include <stdlib.h>
12104203a83SThomas Cort #include <string.h>
12204203a83SThomas Cort #include <time.h>
12304203a83SThomas Cort #include <unistd.h>
12404203a83SThomas Cort #include <stdarg.h>
12504203a83SThomas Cort
12604203a83SThomas Cort #include "ftp_var.h"
12704203a83SThomas Cort
12804203a83SThomas Cort volatile sig_atomic_t abrtflag;
12904203a83SThomas Cort volatile sig_atomic_t timeoutflag;
13004203a83SThomas Cort
13104203a83SThomas Cort sigjmp_buf ptabort;
13204203a83SThomas Cort int ptabflg;
13304203a83SThomas Cort int ptflag = 0;
13404203a83SThomas Cort char pasv[BUFSIZ]; /* passive port for proxy data connection */
13504203a83SThomas Cort
13604203a83SThomas Cort static int empty(FILE *, FILE *, int);
13704203a83SThomas Cort __dead static void abort_squared(int);
13804203a83SThomas Cort
13904203a83SThomas Cort struct sockinet {
14004203a83SThomas Cort union sockunion {
14104203a83SThomas Cort struct sockaddr_in su_sin;
14204203a83SThomas Cort #ifdef INET6
14304203a83SThomas Cort struct sockaddr_in6 su_sin6;
14404203a83SThomas Cort #endif
14504203a83SThomas Cort } si_su;
14604203a83SThomas Cort #if !defined(HAVE_STRUCT_SOCKADDR_IN_SIN_LEN)
14704203a83SThomas Cort int si_len;
14804203a83SThomas Cort #endif
14904203a83SThomas Cort };
15004203a83SThomas Cort
15104203a83SThomas Cort #if !defined(HAVE_STRUCT_SOCKADDR_IN_SIN_LEN)
15204203a83SThomas Cort # define su_len si_len
15304203a83SThomas Cort #else
15404203a83SThomas Cort # define su_len si_su.su_sin.sin_len
15504203a83SThomas Cort #endif
15604203a83SThomas Cort #define su_family si_su.su_sin.sin_family
15704203a83SThomas Cort #define su_port si_su.su_sin.sin_port
15804203a83SThomas Cort
15904203a83SThomas Cort struct sockinet myctladdr, hisctladdr, data_addr;
16004203a83SThomas Cort
16104203a83SThomas Cort char *
hookup(const char * host,const char * port)16204203a83SThomas Cort hookup(const char *host, const char *port)
16304203a83SThomas Cort {
16404203a83SThomas Cort int s = -1, error;
16504203a83SThomas Cort struct addrinfo hints, *res, *res0;
16604203a83SThomas Cort static char hostnamebuf[MAXHOSTNAMELEN];
16704203a83SThomas Cort socklen_t len;
168*84d9c625SLionel Sambuc #if !defined(__minix)
16904203a83SThomas Cort int on = 1;
170*84d9c625SLionel Sambuc #endif /* !defined(__minix) */
17104203a83SThomas Cort
17204203a83SThomas Cort memset((char *)&hisctladdr, 0, sizeof (hisctladdr));
17304203a83SThomas Cort memset((char *)&myctladdr, 0, sizeof (myctladdr));
17404203a83SThomas Cort memset(&hints, 0, sizeof(hints));
17504203a83SThomas Cort hints.ai_flags = AI_CANONNAME;
17604203a83SThomas Cort hints.ai_family = family;
17704203a83SThomas Cort hints.ai_socktype = SOCK_STREAM;
17804203a83SThomas Cort hints.ai_protocol = 0;
17904203a83SThomas Cort error = getaddrinfo(host, port, &hints, &res0);
18004203a83SThomas Cort if (error) {
18104203a83SThomas Cort warnx("Can't lookup `%s:%s': %s", host, port,
18204203a83SThomas Cort (error == EAI_SYSTEM) ? strerror(errno)
18304203a83SThomas Cort : gai_strerror(error));
18404203a83SThomas Cort code = -1;
18504203a83SThomas Cort return (0);
18604203a83SThomas Cort }
18704203a83SThomas Cort
18804203a83SThomas Cort if (res0->ai_canonname)
18904203a83SThomas Cort (void)strlcpy(hostnamebuf, res0->ai_canonname,
19004203a83SThomas Cort sizeof(hostnamebuf));
19104203a83SThomas Cort else
19204203a83SThomas Cort (void)strlcpy(hostnamebuf, host, sizeof(hostnamebuf));
19304203a83SThomas Cort hostname = hostnamebuf;
19404203a83SThomas Cort
19504203a83SThomas Cort for (res = res0; res; res = res->ai_next) {
19604203a83SThomas Cort char hname[NI_MAXHOST], sname[NI_MAXSERV];
19704203a83SThomas Cort
19804203a83SThomas Cort ai_unmapped(res);
19904203a83SThomas Cort if (getnameinfo(res->ai_addr, res->ai_addrlen,
20004203a83SThomas Cort hname, sizeof(hname), sname, sizeof(sname),
20104203a83SThomas Cort NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
20204203a83SThomas Cort strlcpy(hname, "?", sizeof(hname));
20304203a83SThomas Cort strlcpy(sname, "?", sizeof(sname));
20404203a83SThomas Cort }
20504203a83SThomas Cort if (verbose && res0->ai_next) {
20604203a83SThomas Cort /* if we have multiple possibilities */
20704203a83SThomas Cort fprintf(ttyout, "Trying %s:%s ...\n", hname, sname);
20804203a83SThomas Cort }
20904203a83SThomas Cort s = socket(res->ai_family, SOCK_STREAM, res->ai_protocol);
21004203a83SThomas Cort if (s < 0) {
21104203a83SThomas Cort warn("Can't create socket for connection to `%s:%s'",
21204203a83SThomas Cort hname, sname);
21304203a83SThomas Cort continue;
21404203a83SThomas Cort }
21504203a83SThomas Cort if (ftp_connect(s, res->ai_addr, res->ai_addrlen,
21604203a83SThomas Cort verbose || !res->ai_next) < 0) {
21704203a83SThomas Cort close(s);
21804203a83SThomas Cort s = -1;
21904203a83SThomas Cort continue;
22004203a83SThomas Cort }
22104203a83SThomas Cort
22204203a83SThomas Cort /* finally we got one */
22304203a83SThomas Cort break;
22404203a83SThomas Cort }
22504203a83SThomas Cort if (s < 0) {
22604203a83SThomas Cort warnx("Can't connect to `%s:%s'", host, port);
22704203a83SThomas Cort code = -1;
22804203a83SThomas Cort freeaddrinfo(res0);
22904203a83SThomas Cort return 0;
23004203a83SThomas Cort }
23104203a83SThomas Cort memcpy(&hisctladdr.si_su, res->ai_addr, res->ai_addrlen);
23204203a83SThomas Cort hisctladdr.su_len = res->ai_addrlen;
23304203a83SThomas Cort freeaddrinfo(res0);
23404203a83SThomas Cort res0 = res = NULL;
23504203a83SThomas Cort
23604203a83SThomas Cort len = hisctladdr.su_len;
23704203a83SThomas Cort if (getsockname(s, (struct sockaddr *)&myctladdr.si_su, &len) == -1) {
23804203a83SThomas Cort warn("Can't determine my address of connection to `%s:%s'",
23904203a83SThomas Cort host, port);
24004203a83SThomas Cort code = -1;
24104203a83SThomas Cort goto bad;
24204203a83SThomas Cort }
24304203a83SThomas Cort myctladdr.su_len = len;
24404203a83SThomas Cort
24504203a83SThomas Cort #ifdef IPTOS_LOWDELAY
24604203a83SThomas Cort if (hisctladdr.su_family == AF_INET) {
24704203a83SThomas Cort int tos = IPTOS_LOWDELAY;
24804203a83SThomas Cort if (setsockopt(s, IPPROTO_IP, IP_TOS,
24904203a83SThomas Cort (void *)&tos, sizeof(tos)) == -1) {
25004203a83SThomas Cort DWARN("setsockopt %s (ignored)",
25104203a83SThomas Cort "IPTOS_LOWDELAY");
25204203a83SThomas Cort }
25304203a83SThomas Cort }
25404203a83SThomas Cort #endif
25504203a83SThomas Cort cin = fdopen(s, "r");
25604203a83SThomas Cort cout = fdopen(s, "w");
25704203a83SThomas Cort if (cin == NULL || cout == NULL) {
25804203a83SThomas Cort warnx("Can't fdopen socket");
25904203a83SThomas Cort if (cin)
26004203a83SThomas Cort (void)fclose(cin);
26104203a83SThomas Cort if (cout)
26204203a83SThomas Cort (void)fclose(cout);
26304203a83SThomas Cort code = -1;
26404203a83SThomas Cort goto bad;
26504203a83SThomas Cort }
26604203a83SThomas Cort if (verbose)
26704203a83SThomas Cort fprintf(ttyout, "Connected to %s.\n", hostname);
26804203a83SThomas Cort if (getreply(0) > 2) { /* read startup message from server */
26904203a83SThomas Cort if (cin)
27004203a83SThomas Cort (void)fclose(cin);
27104203a83SThomas Cort if (cout)
27204203a83SThomas Cort (void)fclose(cout);
27304203a83SThomas Cort code = -1;
27404203a83SThomas Cort goto bad;
27504203a83SThomas Cort }
27604203a83SThomas Cort
277*84d9c625SLionel Sambuc #if !defined(__minix)
27804203a83SThomas Cort if (setsockopt(s, SOL_SOCKET, SO_OOBINLINE,
27904203a83SThomas Cort (void *)&on, sizeof(on)) == -1) {
28004203a83SThomas Cort DWARN("setsockopt %s (ignored)", "SO_OOBINLINE");
28104203a83SThomas Cort }
282*84d9c625SLionel Sambuc #endif /* !defined(__minix) */
28304203a83SThomas Cort
28404203a83SThomas Cort return (hostname);
28504203a83SThomas Cort bad:
28604203a83SThomas Cort (void)close(s);
28704203a83SThomas Cort return (NULL);
28804203a83SThomas Cort }
28904203a83SThomas Cort
29004203a83SThomas Cort void
cmdabort(int notused)29104203a83SThomas Cort cmdabort(int notused)
29204203a83SThomas Cort {
29304203a83SThomas Cort int oerrno = errno;
29404203a83SThomas Cort
29504203a83SThomas Cort sigint_raised = 1;
29604203a83SThomas Cort alarmtimer(0);
29704203a83SThomas Cort if (fromatty)
29804203a83SThomas Cort write(fileno(ttyout), "\n", 1);
29904203a83SThomas Cort abrtflag++;
30004203a83SThomas Cort if (ptflag)
30104203a83SThomas Cort siglongjmp(ptabort, 1);
30204203a83SThomas Cort errno = oerrno;
30304203a83SThomas Cort }
30404203a83SThomas Cort
30504203a83SThomas Cort void
cmdtimeout(int notused)30604203a83SThomas Cort cmdtimeout(int notused)
30704203a83SThomas Cort {
30804203a83SThomas Cort int oerrno = errno;
30904203a83SThomas Cort
31004203a83SThomas Cort alarmtimer(0);
31104203a83SThomas Cort if (fromatty)
31204203a83SThomas Cort write(fileno(ttyout), "\n", 1);
31304203a83SThomas Cort timeoutflag++;
31404203a83SThomas Cort if (ptflag)
31504203a83SThomas Cort siglongjmp(ptabort, 1);
31604203a83SThomas Cort errno = oerrno;
31704203a83SThomas Cort }
31804203a83SThomas Cort
31904203a83SThomas Cort /*VARARGS*/
32004203a83SThomas Cort int
command(const char * fmt,...)32104203a83SThomas Cort command(const char *fmt, ...)
32204203a83SThomas Cort {
32304203a83SThomas Cort va_list ap;
32404203a83SThomas Cort int r;
32504203a83SThomas Cort sigfunc oldsigint;
32604203a83SThomas Cort
32704203a83SThomas Cort #ifndef NO_DEBUG
32804203a83SThomas Cort if (ftp_debug) {
32904203a83SThomas Cort fputs("---> ", ttyout);
33004203a83SThomas Cort va_start(ap, fmt);
33104203a83SThomas Cort if (strncmp("PASS ", fmt, 5) == 0)
33204203a83SThomas Cort fputs("PASS XXXX", ttyout);
33304203a83SThomas Cort else if (strncmp("ACCT ", fmt, 5) == 0)
33404203a83SThomas Cort fputs("ACCT XXXX", ttyout);
33504203a83SThomas Cort else
33604203a83SThomas Cort vfprintf(ttyout, fmt, ap);
33704203a83SThomas Cort va_end(ap);
33804203a83SThomas Cort putc('\n', ttyout);
33904203a83SThomas Cort }
34004203a83SThomas Cort #endif
34104203a83SThomas Cort if (cout == NULL) {
34204203a83SThomas Cort warnx("No control connection for command");
34304203a83SThomas Cort code = -1;
34404203a83SThomas Cort return (0);
34504203a83SThomas Cort }
34604203a83SThomas Cort
34704203a83SThomas Cort abrtflag = 0;
34804203a83SThomas Cort
34904203a83SThomas Cort oldsigint = xsignal(SIGINT, cmdabort);
35004203a83SThomas Cort
35104203a83SThomas Cort va_start(ap, fmt);
35204203a83SThomas Cort vfprintf(cout, fmt, ap);
35304203a83SThomas Cort va_end(ap);
35404203a83SThomas Cort fputs("\r\n", cout);
35504203a83SThomas Cort (void)fflush(cout);
35604203a83SThomas Cort cpend = 1;
35704203a83SThomas Cort r = getreply(!strcmp(fmt, "QUIT"));
35804203a83SThomas Cort if (abrtflag && oldsigint != SIG_IGN)
35904203a83SThomas Cort (*oldsigint)(SIGINT);
36004203a83SThomas Cort (void)xsignal(SIGINT, oldsigint);
36104203a83SThomas Cort return (r);
36204203a83SThomas Cort }
36304203a83SThomas Cort
36404203a83SThomas Cort static const char *m421[] = {
36504203a83SThomas Cort "remote server timed out. Connection closed",
36604203a83SThomas Cort "user interrupt. Connection closed",
36704203a83SThomas Cort "remote server has closed connection",
36804203a83SThomas Cort };
36904203a83SThomas Cort
37004203a83SThomas Cort int
getreply(int expecteof)37104203a83SThomas Cort getreply(int expecteof)
37204203a83SThomas Cort {
37304203a83SThomas Cort char current_line[BUFSIZ]; /* last line of previous reply */
37404203a83SThomas Cort int c, n, lineno;
37504203a83SThomas Cort int dig;
37604203a83SThomas Cort int originalcode = 0, continuation = 0;
37704203a83SThomas Cort sigfunc oldsigint, oldsigalrm;
37804203a83SThomas Cort int pflag = 0;
37904203a83SThomas Cort char *cp, *pt = pasv;
38004203a83SThomas Cort
38104203a83SThomas Cort abrtflag = 0;
38204203a83SThomas Cort timeoutflag = 0;
38304203a83SThomas Cort
38404203a83SThomas Cort oldsigint = xsignal(SIGINT, cmdabort);
38504203a83SThomas Cort oldsigalrm = xsignal(SIGALRM, cmdtimeout);
38604203a83SThomas Cort
38704203a83SThomas Cort for (lineno = 0 ;; lineno++) {
38804203a83SThomas Cort dig = n = code = 0;
38904203a83SThomas Cort cp = current_line;
39004203a83SThomas Cort while (alarmtimer(quit_time ? quit_time : 60),
39104203a83SThomas Cort ((c = getc(cin)) != '\n')) {
39204203a83SThomas Cort if (c == IAC) { /* handle telnet commands */
39304203a83SThomas Cort switch (c = getc(cin)) {
39404203a83SThomas Cort case WILL:
39504203a83SThomas Cort case WONT:
39604203a83SThomas Cort c = getc(cin);
39704203a83SThomas Cort fprintf(cout, "%c%c%c", IAC, DONT, c);
39804203a83SThomas Cort (void)fflush(cout);
39904203a83SThomas Cort break;
40004203a83SThomas Cort case DO:
40104203a83SThomas Cort case DONT:
40204203a83SThomas Cort c = getc(cin);
40304203a83SThomas Cort fprintf(cout, "%c%c%c", IAC, WONT, c);
40404203a83SThomas Cort (void)fflush(cout);
40504203a83SThomas Cort break;
40604203a83SThomas Cort default:
40704203a83SThomas Cort break;
40804203a83SThomas Cort }
40904203a83SThomas Cort continue;
41004203a83SThomas Cort }
41104203a83SThomas Cort dig++;
41204203a83SThomas Cort if (c == EOF) {
41304203a83SThomas Cort /*
41404203a83SThomas Cort * these will get trashed by pswitch()
41504203a83SThomas Cort * in lostpeer()
41604203a83SThomas Cort */
41704203a83SThomas Cort int reply_timeoutflag = timeoutflag;
41804203a83SThomas Cort int reply_abrtflag = abrtflag;
41904203a83SThomas Cort
42004203a83SThomas Cort alarmtimer(0);
42104203a83SThomas Cort if (expecteof && feof(cin)) {
42204203a83SThomas Cort (void)xsignal(SIGINT, oldsigint);
42304203a83SThomas Cort (void)xsignal(SIGALRM, oldsigalrm);
42404203a83SThomas Cort code = 221;
42504203a83SThomas Cort return (0);
42604203a83SThomas Cort }
42704203a83SThomas Cort cpend = 0;
42804203a83SThomas Cort lostpeer(0);
42904203a83SThomas Cort if (verbose) {
43004203a83SThomas Cort size_t midx;
43104203a83SThomas Cort if (reply_timeoutflag)
43204203a83SThomas Cort midx = 0;
43304203a83SThomas Cort else if (reply_abrtflag)
43404203a83SThomas Cort midx = 1;
43504203a83SThomas Cort else
43604203a83SThomas Cort midx = 2;
43704203a83SThomas Cort (void)fprintf(ttyout,
43804203a83SThomas Cort "421 Service not available, %s.\n", m421[midx]);
43904203a83SThomas Cort (void)fflush(ttyout);
44004203a83SThomas Cort }
44104203a83SThomas Cort code = 421;
44204203a83SThomas Cort (void)xsignal(SIGINT, oldsigint);
44304203a83SThomas Cort (void)xsignal(SIGALRM, oldsigalrm);
44404203a83SThomas Cort return (4);
44504203a83SThomas Cort }
44604203a83SThomas Cort if (c != '\r' && (verbose > 0 ||
44704203a83SThomas Cort ((verbose > -1 && n == '5' && dig > 4) &&
44804203a83SThomas Cort (((!n && c < '5') || (n && n < '5'))
44904203a83SThomas Cort || !retry_connect)))) {
45004203a83SThomas Cort if (proxflag &&
45104203a83SThomas Cort (dig == 1 || (dig == 5 && verbose == 0)))
45204203a83SThomas Cort fprintf(ttyout, "%s:", hostname);
45304203a83SThomas Cort (void)putc(c, ttyout);
45404203a83SThomas Cort }
45504203a83SThomas Cort if (dig < 4 && isdigit(c))
45604203a83SThomas Cort code = code * 10 + (c - '0');
45704203a83SThomas Cort if (!pflag && (code == 227 || code == 228))
45804203a83SThomas Cort pflag = 1;
45904203a83SThomas Cort else if (!pflag && code == 229)
46004203a83SThomas Cort pflag = 100;
46104203a83SThomas Cort if (dig > 4 && pflag == 1 && isdigit(c))
46204203a83SThomas Cort pflag = 2;
46304203a83SThomas Cort if (pflag == 2) {
46404203a83SThomas Cort if (c != '\r' && c != ')') {
46504203a83SThomas Cort if (pt < &pasv[sizeof(pasv) - 1])
46604203a83SThomas Cort *pt++ = c;
46704203a83SThomas Cort } else {
46804203a83SThomas Cort *pt = '\0';
46904203a83SThomas Cort pflag = 3;
47004203a83SThomas Cort }
47104203a83SThomas Cort }
47204203a83SThomas Cort if (pflag == 100 && c == '(')
47304203a83SThomas Cort pflag = 2;
47404203a83SThomas Cort if (dig == 4 && c == '-') {
47504203a83SThomas Cort if (continuation)
47604203a83SThomas Cort code = 0;
47704203a83SThomas Cort continuation++;
47804203a83SThomas Cort }
47904203a83SThomas Cort if (n == 0)
48004203a83SThomas Cort n = c;
48104203a83SThomas Cort if (cp < ¤t_line[sizeof(current_line) - 1])
48204203a83SThomas Cort *cp++ = c;
48304203a83SThomas Cort }
48404203a83SThomas Cort if (verbose > 0 || ((verbose > -1 && n == '5') &&
48504203a83SThomas Cort (n < '5' || !retry_connect))) {
48604203a83SThomas Cort (void)putc(c, ttyout);
48704203a83SThomas Cort (void)fflush(ttyout);
48804203a83SThomas Cort }
48904203a83SThomas Cort if (cp[-1] == '\r')
49004203a83SThomas Cort cp[-1] = '\0';
49104203a83SThomas Cort *cp = '\0';
49204203a83SThomas Cort if (lineno == 0)
49304203a83SThomas Cort (void)strlcpy(reply_string, current_line,
49404203a83SThomas Cort sizeof(reply_string));
49504203a83SThomas Cort if (lineno > 0 && code == 0 && reply_callback != NULL)
49604203a83SThomas Cort (*reply_callback)(current_line);
49704203a83SThomas Cort if (continuation && code != originalcode) {
49804203a83SThomas Cort if (originalcode == 0)
49904203a83SThomas Cort originalcode = code;
50004203a83SThomas Cort continue;
50104203a83SThomas Cort }
50204203a83SThomas Cort if (n != '1')
50304203a83SThomas Cort cpend = 0;
50404203a83SThomas Cort alarmtimer(0);
50504203a83SThomas Cort (void)xsignal(SIGINT, oldsigint);
50604203a83SThomas Cort (void)xsignal(SIGALRM, oldsigalrm);
50704203a83SThomas Cort if (code == 421 || originalcode == 421)
50804203a83SThomas Cort lostpeer(0);
50904203a83SThomas Cort if (abrtflag && oldsigint != cmdabort && oldsigint != SIG_IGN)
51004203a83SThomas Cort (*oldsigint)(SIGINT);
51104203a83SThomas Cort if (timeoutflag && oldsigalrm != cmdtimeout &&
51204203a83SThomas Cort oldsigalrm != SIG_IGN)
51304203a83SThomas Cort (*oldsigalrm)(SIGINT);
51404203a83SThomas Cort return (n - '0');
51504203a83SThomas Cort }
51604203a83SThomas Cort }
51704203a83SThomas Cort
51804203a83SThomas Cort static int
empty(FILE * ecin,FILE * din,int sec)51904203a83SThomas Cort empty(FILE *ecin, FILE *din, int sec)
52004203a83SThomas Cort {
52104203a83SThomas Cort int nr, nfd;
52204203a83SThomas Cort struct pollfd pfd[2];
52304203a83SThomas Cort
52404203a83SThomas Cort nfd = 0;
52504203a83SThomas Cort if (ecin) {
52604203a83SThomas Cort pfd[nfd].fd = fileno(ecin);
52704203a83SThomas Cort pfd[nfd++].events = POLLIN;
52804203a83SThomas Cort }
52904203a83SThomas Cort
53004203a83SThomas Cort if (din) {
53104203a83SThomas Cort pfd[nfd].fd = fileno(din);
53204203a83SThomas Cort pfd[nfd++].events = POLLIN;
53304203a83SThomas Cort }
53404203a83SThomas Cort
53504203a83SThomas Cort if ((nr = ftp_poll(pfd, nfd, sec * 1000)) <= 0)
53604203a83SThomas Cort return nr;
53704203a83SThomas Cort
53804203a83SThomas Cort nr = 0;
53904203a83SThomas Cort nfd = 0;
54004203a83SThomas Cort if (ecin)
54104203a83SThomas Cort nr |= (pfd[nfd++].revents & POLLIN) ? 1 : 0;
54204203a83SThomas Cort if (din)
54304203a83SThomas Cort nr |= (pfd[nfd++].revents & POLLIN) ? 2 : 0;
54404203a83SThomas Cort return nr;
54504203a83SThomas Cort }
54604203a83SThomas Cort
54704203a83SThomas Cort sigjmp_buf xferabort;
54804203a83SThomas Cort
54904203a83SThomas Cort __dead static void
abortxfer(int notused)55004203a83SThomas Cort abortxfer(int notused)
55104203a83SThomas Cort {
55204203a83SThomas Cort char msgbuf[100];
55304203a83SThomas Cort size_t len;
55404203a83SThomas Cort
55504203a83SThomas Cort sigint_raised = 1;
55604203a83SThomas Cort alarmtimer(0);
55704203a83SThomas Cort mflag = 0;
55804203a83SThomas Cort abrtflag = 0;
55904203a83SThomas Cort switch (direction[0]) {
56004203a83SThomas Cort case 'r':
56104203a83SThomas Cort strlcpy(msgbuf, "\nreceive", sizeof(msgbuf));
56204203a83SThomas Cort break;
56304203a83SThomas Cort case 's':
56404203a83SThomas Cort strlcpy(msgbuf, "\nsend", sizeof(msgbuf));
56504203a83SThomas Cort break;
56604203a83SThomas Cort default:
56704203a83SThomas Cort errx(1, "abortxfer: unknown direction `%s'", direction);
56804203a83SThomas Cort }
56904203a83SThomas Cort len = strlcat(msgbuf, " aborted. Waiting for remote to finish abort.\n",
57004203a83SThomas Cort sizeof(msgbuf));
57104203a83SThomas Cort write(fileno(ttyout), msgbuf, len);
57204203a83SThomas Cort siglongjmp(xferabort, 1);
57304203a83SThomas Cort }
57404203a83SThomas Cort
57504203a83SThomas Cort /*
57604203a83SThomas Cort * Read data from infd & write to outfd, using buf/bufsize as the temporary
57704203a83SThomas Cort * buffer, dealing with short writes.
57804203a83SThomas Cort * If rate_limit != 0, rate-limit the transfer.
57904203a83SThomas Cort * If hash_interval != 0, fputc('c', ttyout) every hash_interval bytes.
58004203a83SThomas Cort * Updates global variables: bytes.
58104203a83SThomas Cort * Returns 0 if ok, 1 if there was a read error, 2 if there was a write error.
58204203a83SThomas Cort * In the case of error, errno contains the appropriate error code.
58304203a83SThomas Cort */
58404203a83SThomas Cort static int
copy_bytes(int infd,int outfd,char * buf,size_t bufsize,int rate_limit,int hash_interval)58504203a83SThomas Cort copy_bytes(int infd, int outfd, char *buf, size_t bufsize,
58604203a83SThomas Cort int rate_limit, int hash_interval)
58704203a83SThomas Cort {
58804203a83SThomas Cort volatile off_t hashc;
58904203a83SThomas Cort ssize_t inc, outc;
59004203a83SThomas Cort char *bufp;
59104203a83SThomas Cort struct timeval tvthen, tvnow, tvdiff;
59204203a83SThomas Cort off_t bufrem, bufchunk;
59304203a83SThomas Cort int serr;
59404203a83SThomas Cort
59504203a83SThomas Cort hashc = hash_interval;
59604203a83SThomas Cort if (rate_limit)
59704203a83SThomas Cort bufchunk = rate_limit;
59804203a83SThomas Cort else
59904203a83SThomas Cort bufchunk = bufsize;
60004203a83SThomas Cort
60104203a83SThomas Cort while (1) {
60204203a83SThomas Cort if (rate_limit) {
60304203a83SThomas Cort (void)gettimeofday(&tvthen, NULL);
60404203a83SThomas Cort }
60504203a83SThomas Cort errno = 0;
60604203a83SThomas Cort inc = outc = 0;
60704203a83SThomas Cort /* copy bufchunk at a time */
60804203a83SThomas Cort bufrem = bufchunk;
60904203a83SThomas Cort while (bufrem > 0) {
61004203a83SThomas Cort inc = read(infd, buf, MIN((off_t)bufsize, bufrem));
61104203a83SThomas Cort if (inc <= 0)
61204203a83SThomas Cort goto copy_done;
61304203a83SThomas Cort bytes += inc;
61404203a83SThomas Cort bufrem -= inc;
61504203a83SThomas Cort bufp = buf;
61604203a83SThomas Cort while (inc > 0) {
61704203a83SThomas Cort outc = write(outfd, bufp, inc);
61804203a83SThomas Cort if (outc < 0)
61904203a83SThomas Cort goto copy_done;
62004203a83SThomas Cort inc -= outc;
62104203a83SThomas Cort bufp += outc;
62204203a83SThomas Cort }
62304203a83SThomas Cort if (hash_interval) {
62404203a83SThomas Cort while (bytes >= hashc) {
62504203a83SThomas Cort (void)putc('#', ttyout);
62604203a83SThomas Cort hashc += hash_interval;
62704203a83SThomas Cort }
62804203a83SThomas Cort (void)fflush(ttyout);
62904203a83SThomas Cort }
63004203a83SThomas Cort }
63104203a83SThomas Cort if (rate_limit) { /* rate limited; wait if necessary */
63204203a83SThomas Cort while (1) {
63304203a83SThomas Cort (void)gettimeofday(&tvnow, NULL);
63404203a83SThomas Cort timersub(&tvnow, &tvthen, &tvdiff);
63504203a83SThomas Cort if (tvdiff.tv_sec > 0)
63604203a83SThomas Cort break;
63704203a83SThomas Cort usleep(1000000 - tvdiff.tv_usec);
63804203a83SThomas Cort }
63904203a83SThomas Cort }
64004203a83SThomas Cort }
64104203a83SThomas Cort
64204203a83SThomas Cort copy_done:
64304203a83SThomas Cort serr = errno;
64404203a83SThomas Cort if (hash_interval && bytes > 0) {
64504203a83SThomas Cort if (bytes < hash_interval)
64604203a83SThomas Cort (void)putc('#', ttyout);
64704203a83SThomas Cort (void)putc('\n', ttyout);
64804203a83SThomas Cort (void)fflush(ttyout);
64904203a83SThomas Cort }
65004203a83SThomas Cort errno = serr;
65104203a83SThomas Cort if (inc == -1)
65204203a83SThomas Cort return 1;
65304203a83SThomas Cort if (outc == -1)
65404203a83SThomas Cort return 2;
65504203a83SThomas Cort
65604203a83SThomas Cort return 0;
65704203a83SThomas Cort }
65804203a83SThomas Cort
65904203a83SThomas Cort void
sendrequest(const char * cmd,const char * local,const char * remote,int printnames)66004203a83SThomas Cort sendrequest(const char *cmd, const char *local, const char *remote,
66104203a83SThomas Cort int printnames)
66204203a83SThomas Cort {
66304203a83SThomas Cort struct stat st;
66404203a83SThomas Cort int c;
66504203a83SThomas Cort FILE *volatile fin;
66604203a83SThomas Cort FILE *volatile dout;
66704203a83SThomas Cort int (*volatile closefunc)(FILE *);
66804203a83SThomas Cort sigfunc volatile oldintr;
66904203a83SThomas Cort sigfunc volatile oldintp;
67004203a83SThomas Cort off_t volatile hashbytes;
67104203a83SThomas Cort int hash_interval;
67204203a83SThomas Cort const char *lmode;
67304203a83SThomas Cort static size_t bufsize;
67404203a83SThomas Cort static char *buf;
67504203a83SThomas Cort int oprogress;
67604203a83SThomas Cort
67704203a83SThomas Cort hashbytes = mark;
67804203a83SThomas Cort direction = "sent";
67904203a83SThomas Cort dout = NULL;
68004203a83SThomas Cort bytes = 0;
68104203a83SThomas Cort filesize = -1;
68204203a83SThomas Cort oprogress = progress;
68304203a83SThomas Cort if (verbose && printnames) {
68404203a83SThomas Cort if (*local != '-')
68504203a83SThomas Cort fprintf(ttyout, "local: %s ", local);
68604203a83SThomas Cort if (remote)
68704203a83SThomas Cort fprintf(ttyout, "remote: %s\n", remote);
68804203a83SThomas Cort }
68904203a83SThomas Cort if (proxy) {
69004203a83SThomas Cort proxtrans(cmd, local, remote);
69104203a83SThomas Cort return;
69204203a83SThomas Cort }
69304203a83SThomas Cort if (curtype != type)
69404203a83SThomas Cort changetype(type, 0);
69504203a83SThomas Cort closefunc = NULL;
69604203a83SThomas Cort oldintr = NULL;
69704203a83SThomas Cort oldintp = NULL;
69804203a83SThomas Cort lmode = "w";
69904203a83SThomas Cort if (sigsetjmp(xferabort, 1)) {
70004203a83SThomas Cort while (cpend)
70104203a83SThomas Cort (void)getreply(0);
70204203a83SThomas Cort code = -1;
70304203a83SThomas Cort goto cleanupsend;
70404203a83SThomas Cort }
70504203a83SThomas Cort (void)xsignal(SIGQUIT, psummary);
70604203a83SThomas Cort oldintr = xsignal(SIGINT, abortxfer);
70704203a83SThomas Cort if (strcmp(local, "-") == 0) {
70804203a83SThomas Cort fin = stdin;
70904203a83SThomas Cort progress = 0;
71004203a83SThomas Cort } else if (*local == '|') {
71104203a83SThomas Cort oldintp = xsignal(SIGPIPE, SIG_IGN);
71204203a83SThomas Cort fin = popen(local + 1, "r");
71304203a83SThomas Cort if (fin == NULL) {
71404203a83SThomas Cort warn("Can't execute `%s'", local + 1);
71504203a83SThomas Cort code = -1;
71604203a83SThomas Cort goto cleanupsend;
71704203a83SThomas Cort }
71804203a83SThomas Cort progress = 0;
71904203a83SThomas Cort closefunc = pclose;
72004203a83SThomas Cort } else {
72104203a83SThomas Cort fin = fopen(local, "r");
72204203a83SThomas Cort if (fin == NULL) {
72304203a83SThomas Cort warn("Can't open `%s'", local);
72404203a83SThomas Cort code = -1;
72504203a83SThomas Cort goto cleanupsend;
72604203a83SThomas Cort }
72704203a83SThomas Cort closefunc = fclose;
72804203a83SThomas Cort if (fstat(fileno(fin), &st) < 0 || !S_ISREG(st.st_mode)) {
72904203a83SThomas Cort fprintf(ttyout, "%s: not a plain file.\n", local);
73004203a83SThomas Cort code = -1;
73104203a83SThomas Cort goto cleanupsend;
73204203a83SThomas Cort }
73304203a83SThomas Cort filesize = st.st_size;
73404203a83SThomas Cort }
73504203a83SThomas Cort if (initconn()) {
73604203a83SThomas Cort code = -1;
73704203a83SThomas Cort goto cleanupsend;
73804203a83SThomas Cort }
73904203a83SThomas Cort if (sigsetjmp(xferabort, 1))
74004203a83SThomas Cort goto abort;
74104203a83SThomas Cort
74204203a83SThomas Cort if (restart_point &&
74304203a83SThomas Cort (strcmp(cmd, "STOR") == 0 || strcmp(cmd, "APPE") == 0)) {
74404203a83SThomas Cort int rc;
74504203a83SThomas Cort
74604203a83SThomas Cort rc = -1;
74704203a83SThomas Cort switch (curtype) {
74804203a83SThomas Cort case TYPE_A:
74904203a83SThomas Cort rc = fseeko(fin, restart_point, SEEK_SET);
75004203a83SThomas Cort break;
75104203a83SThomas Cort case TYPE_I:
75204203a83SThomas Cort case TYPE_L:
75304203a83SThomas Cort rc = lseek(fileno(fin), restart_point, SEEK_SET);
75404203a83SThomas Cort break;
75504203a83SThomas Cort }
75604203a83SThomas Cort if (rc < 0) {
75704203a83SThomas Cort warn("Can't seek to restart `%s'", local);
75804203a83SThomas Cort goto cleanupsend;
75904203a83SThomas Cort }
76004203a83SThomas Cort if (command("REST " LLF, (LLT)restart_point) != CONTINUE)
76104203a83SThomas Cort goto cleanupsend;
76204203a83SThomas Cort lmode = "r+";
76304203a83SThomas Cort }
76404203a83SThomas Cort if (remote) {
76504203a83SThomas Cort if (command("%s %s", cmd, remote) != PRELIM)
76604203a83SThomas Cort goto cleanupsend;
76704203a83SThomas Cort } else {
76804203a83SThomas Cort if (command("%s", cmd) != PRELIM)
76904203a83SThomas Cort goto cleanupsend;
77004203a83SThomas Cort }
77104203a83SThomas Cort dirchange = 1;
77204203a83SThomas Cort dout = dataconn(lmode);
77304203a83SThomas Cort if (dout == NULL)
77404203a83SThomas Cort goto abort;
77504203a83SThomas Cort
77604203a83SThomas Cort assert(sndbuf_size > 0);
77704203a83SThomas Cort if ((size_t)sndbuf_size > bufsize) {
77804203a83SThomas Cort if (buf)
77904203a83SThomas Cort (void)free(buf);
78004203a83SThomas Cort bufsize = sndbuf_size;
78104203a83SThomas Cort buf = ftp_malloc(bufsize);
78204203a83SThomas Cort }
78304203a83SThomas Cort
78404203a83SThomas Cort progressmeter(-1);
78504203a83SThomas Cort oldintp = xsignal(SIGPIPE, SIG_IGN);
78604203a83SThomas Cort hash_interval = (hash && (!progress || filesize < 0)) ? mark : 0;
78704203a83SThomas Cort
78804203a83SThomas Cort switch (curtype) {
78904203a83SThomas Cort
79004203a83SThomas Cort case TYPE_I:
79104203a83SThomas Cort case TYPE_L:
79204203a83SThomas Cort c = copy_bytes(fileno(fin), fileno(dout), buf, bufsize,
79304203a83SThomas Cort rate_put, hash_interval);
79404203a83SThomas Cort if (c == 1) {
79504203a83SThomas Cort warn("Reading `%s'", local);
79604203a83SThomas Cort } else if (c == 2) {
79704203a83SThomas Cort if (errno != EPIPE)
79804203a83SThomas Cort warn("Writing to network");
79904203a83SThomas Cort bytes = -1;
80004203a83SThomas Cort }
80104203a83SThomas Cort break;
80204203a83SThomas Cort
80304203a83SThomas Cort case TYPE_A:
80404203a83SThomas Cort while ((c = getc(fin)) != EOF) {
80504203a83SThomas Cort if (c == '\n') {
80604203a83SThomas Cort while (hash_interval && bytes >= hashbytes) {
80704203a83SThomas Cort (void)putc('#', ttyout);
80804203a83SThomas Cort (void)fflush(ttyout);
80904203a83SThomas Cort hashbytes += mark;
81004203a83SThomas Cort }
81104203a83SThomas Cort if (ferror(dout))
81204203a83SThomas Cort break;
81304203a83SThomas Cort (void)putc('\r', dout);
81404203a83SThomas Cort bytes++;
81504203a83SThomas Cort }
81604203a83SThomas Cort (void)putc(c, dout);
81704203a83SThomas Cort bytes++;
81804203a83SThomas Cort #if 0 /* this violates RFC 959 */
81904203a83SThomas Cort if (c == '\r') {
82004203a83SThomas Cort (void)putc('\0', dout);
82104203a83SThomas Cort bytes++;
82204203a83SThomas Cort }
82304203a83SThomas Cort #endif
82404203a83SThomas Cort }
82504203a83SThomas Cort if (hash_interval) {
82604203a83SThomas Cort if (bytes < hashbytes)
82704203a83SThomas Cort (void)putc('#', ttyout);
82804203a83SThomas Cort (void)putc('\n', ttyout);
82904203a83SThomas Cort }
83004203a83SThomas Cort if (ferror(fin))
83104203a83SThomas Cort warn("Reading `%s'", local);
83204203a83SThomas Cort if (ferror(dout)) {
83304203a83SThomas Cort if (errno != EPIPE)
83404203a83SThomas Cort warn("Writing to network");
83504203a83SThomas Cort bytes = -1;
83604203a83SThomas Cort }
83704203a83SThomas Cort break;
83804203a83SThomas Cort }
83904203a83SThomas Cort
84004203a83SThomas Cort progressmeter(1);
84104203a83SThomas Cort if (closefunc != NULL) {
84204203a83SThomas Cort (*closefunc)(fin);
84304203a83SThomas Cort fin = NULL;
84404203a83SThomas Cort }
84504203a83SThomas Cort (void)fclose(dout);
84604203a83SThomas Cort dout = NULL;
84704203a83SThomas Cort (void)getreply(0);
84804203a83SThomas Cort if (bytes > 0)
84904203a83SThomas Cort ptransfer(0);
85004203a83SThomas Cort goto cleanupsend;
85104203a83SThomas Cort
85204203a83SThomas Cort abort:
85304203a83SThomas Cort (void)xsignal(SIGINT, oldintr);
85404203a83SThomas Cort oldintr = NULL;
85504203a83SThomas Cort if (!cpend) {
85604203a83SThomas Cort code = -1;
85704203a83SThomas Cort goto cleanupsend;
85804203a83SThomas Cort }
85904203a83SThomas Cort if (data >= 0) {
86004203a83SThomas Cort (void)close(data);
86104203a83SThomas Cort data = -1;
86204203a83SThomas Cort }
86304203a83SThomas Cort if (dout) {
86404203a83SThomas Cort (void)fclose(dout);
86504203a83SThomas Cort dout = NULL;
86604203a83SThomas Cort }
86704203a83SThomas Cort (void)getreply(0);
86804203a83SThomas Cort code = -1;
86904203a83SThomas Cort if (bytes > 0)
87004203a83SThomas Cort ptransfer(0);
87104203a83SThomas Cort
87204203a83SThomas Cort cleanupsend:
87304203a83SThomas Cort if (oldintr)
87404203a83SThomas Cort (void)xsignal(SIGINT, oldintr);
87504203a83SThomas Cort if (oldintp)
87604203a83SThomas Cort (void)xsignal(SIGPIPE, oldintp);
87704203a83SThomas Cort if (data >= 0) {
87804203a83SThomas Cort (void)close(data);
87904203a83SThomas Cort data = -1;
88004203a83SThomas Cort }
88104203a83SThomas Cort if (closefunc != NULL && fin != NULL)
88204203a83SThomas Cort (*closefunc)(fin);
88304203a83SThomas Cort if (dout)
88404203a83SThomas Cort (void)fclose(dout);
88504203a83SThomas Cort progress = oprogress;
88604203a83SThomas Cort restart_point = 0;
88704203a83SThomas Cort bytes = 0;
88804203a83SThomas Cort }
88904203a83SThomas Cort
89004203a83SThomas Cort void
recvrequest(const char * cmd,const char * volatile local,const char * remote,const char * lmode,int printnames,int ignorespecial)89104203a83SThomas Cort recvrequest(const char *cmd, const char *volatile local, const char *remote,
89204203a83SThomas Cort const char *lmode, int printnames, int ignorespecial)
89304203a83SThomas Cort {
89404203a83SThomas Cort FILE *volatile fout;
89504203a83SThomas Cort FILE *volatile din;
89604203a83SThomas Cort int (*volatile closefunc)(FILE *);
89704203a83SThomas Cort sigfunc volatile oldintr;
89804203a83SThomas Cort sigfunc volatile oldintp;
89904203a83SThomas Cort int c, d;
90004203a83SThomas Cort int volatile is_retr;
90104203a83SThomas Cort int volatile tcrflag;
90204203a83SThomas Cort int volatile bare_lfs;
90304203a83SThomas Cort static size_t bufsize;
90404203a83SThomas Cort static char *buf;
90504203a83SThomas Cort off_t volatile hashbytes;
90604203a83SThomas Cort int hash_interval;
90704203a83SThomas Cort struct stat st;
90804203a83SThomas Cort time_t mtime;
90904203a83SThomas Cort struct timeval tval[2];
91004203a83SThomas Cort int oprogress;
91104203a83SThomas Cort int opreserve;
91204203a83SThomas Cort
91304203a83SThomas Cort fout = NULL;
91404203a83SThomas Cort din = NULL;
91504203a83SThomas Cort hashbytes = mark;
91604203a83SThomas Cort direction = "received";
91704203a83SThomas Cort bytes = 0;
91804203a83SThomas Cort bare_lfs = 0;
91904203a83SThomas Cort filesize = -1;
92004203a83SThomas Cort oprogress = progress;
92104203a83SThomas Cort opreserve = preserve;
92204203a83SThomas Cort is_retr = (strcmp(cmd, "RETR") == 0);
92304203a83SThomas Cort if (is_retr && verbose && printnames) {
92404203a83SThomas Cort if (ignorespecial || *local != '-')
92504203a83SThomas Cort fprintf(ttyout, "local: %s ", local);
92604203a83SThomas Cort if (remote)
92704203a83SThomas Cort fprintf(ttyout, "remote: %s\n", remote);
92804203a83SThomas Cort }
92904203a83SThomas Cort if (proxy && is_retr) {
93004203a83SThomas Cort proxtrans(cmd, local, remote);
93104203a83SThomas Cort return;
93204203a83SThomas Cort }
93304203a83SThomas Cort closefunc = NULL;
93404203a83SThomas Cort oldintr = NULL;
93504203a83SThomas Cort oldintp = NULL;
93604203a83SThomas Cort tcrflag = !crflag && is_retr;
93704203a83SThomas Cort if (sigsetjmp(xferabort, 1)) {
93804203a83SThomas Cort while (cpend)
93904203a83SThomas Cort (void)getreply(0);
94004203a83SThomas Cort code = -1;
94104203a83SThomas Cort goto cleanuprecv;
94204203a83SThomas Cort }
94304203a83SThomas Cort (void)xsignal(SIGQUIT, psummary);
94404203a83SThomas Cort oldintr = xsignal(SIGINT, abortxfer);
94504203a83SThomas Cort if (ignorespecial || (strcmp(local, "-") && *local != '|')) {
94604203a83SThomas Cort if (access(local, W_OK) < 0) {
94704203a83SThomas Cort char *dir = strrchr(local, '/');
94804203a83SThomas Cort
94904203a83SThomas Cort if (errno != ENOENT && errno != EACCES) {
95004203a83SThomas Cort warn("Can't access `%s'", local);
95104203a83SThomas Cort code = -1;
95204203a83SThomas Cort goto cleanuprecv;
95304203a83SThomas Cort }
95404203a83SThomas Cort if (dir != NULL)
95504203a83SThomas Cort *dir = 0;
95604203a83SThomas Cort d = access(dir == local ? "/" :
95704203a83SThomas Cort dir ? local : ".", W_OK);
95804203a83SThomas Cort if (dir != NULL)
95904203a83SThomas Cort *dir = '/';
96004203a83SThomas Cort if (d < 0) {
96104203a83SThomas Cort warn("Can't access `%s'", local);
96204203a83SThomas Cort code = -1;
96304203a83SThomas Cort goto cleanuprecv;
96404203a83SThomas Cort }
96504203a83SThomas Cort if (!runique && errno == EACCES &&
96604203a83SThomas Cort chmod(local, (S_IRUSR|S_IWUSR)) < 0) {
96704203a83SThomas Cort warn("Can't chmod `%s'", local);
96804203a83SThomas Cort code = -1;
96904203a83SThomas Cort goto cleanuprecv;
97004203a83SThomas Cort }
97104203a83SThomas Cort if (runique && errno == EACCES &&
97204203a83SThomas Cort (local = gunique(local)) == NULL) {
97304203a83SThomas Cort code = -1;
97404203a83SThomas Cort goto cleanuprecv;
97504203a83SThomas Cort }
97604203a83SThomas Cort }
97704203a83SThomas Cort else if (runique && (local = gunique(local)) == NULL) {
97804203a83SThomas Cort code = -1;
97904203a83SThomas Cort goto cleanuprecv;
98004203a83SThomas Cort }
98104203a83SThomas Cort }
98204203a83SThomas Cort if (!is_retr) {
98304203a83SThomas Cort if (curtype != TYPE_A)
98404203a83SThomas Cort changetype(TYPE_A, 0);
98504203a83SThomas Cort } else {
98604203a83SThomas Cort if (curtype != type)
98704203a83SThomas Cort changetype(type, 0);
98804203a83SThomas Cort filesize = remotesize(remote, 0);
98904203a83SThomas Cort if (code == 421 || code == -1)
99004203a83SThomas Cort goto cleanuprecv;
99104203a83SThomas Cort }
99204203a83SThomas Cort if (initconn()) {
99304203a83SThomas Cort code = -1;
99404203a83SThomas Cort goto cleanuprecv;
99504203a83SThomas Cort }
99604203a83SThomas Cort if (sigsetjmp(xferabort, 1))
99704203a83SThomas Cort goto abort;
99804203a83SThomas Cort if (is_retr && restart_point &&
99904203a83SThomas Cort command("REST " LLF, (LLT) restart_point) != CONTINUE)
100004203a83SThomas Cort goto cleanuprecv;
100104203a83SThomas Cort if (! EMPTYSTRING(remote)) {
100204203a83SThomas Cort if (command("%s %s", cmd, remote) != PRELIM)
100304203a83SThomas Cort goto cleanuprecv;
100404203a83SThomas Cort } else {
100504203a83SThomas Cort if (command("%s", cmd) != PRELIM)
100604203a83SThomas Cort goto cleanuprecv;
100704203a83SThomas Cort }
100804203a83SThomas Cort din = dataconn("r");
100904203a83SThomas Cort if (din == NULL)
101004203a83SThomas Cort goto abort;
101104203a83SThomas Cort if (!ignorespecial && strcmp(local, "-") == 0) {
101204203a83SThomas Cort fout = stdout;
101304203a83SThomas Cort progress = 0;
101404203a83SThomas Cort preserve = 0;
101504203a83SThomas Cort } else if (!ignorespecial && *local == '|') {
101604203a83SThomas Cort oldintp = xsignal(SIGPIPE, SIG_IGN);
101704203a83SThomas Cort fout = popen(local + 1, "w");
101804203a83SThomas Cort if (fout == NULL) {
101904203a83SThomas Cort warn("Can't execute `%s'", local+1);
102004203a83SThomas Cort goto abort;
102104203a83SThomas Cort }
102204203a83SThomas Cort progress = 0;
102304203a83SThomas Cort preserve = 0;
102404203a83SThomas Cort closefunc = pclose;
102504203a83SThomas Cort } else {
102604203a83SThomas Cort fout = fopen(local, lmode);
102704203a83SThomas Cort if (fout == NULL) {
102804203a83SThomas Cort warn("Can't open `%s'", local);
102904203a83SThomas Cort goto abort;
103004203a83SThomas Cort }
103104203a83SThomas Cort closefunc = fclose;
103204203a83SThomas Cort }
103304203a83SThomas Cort
103404203a83SThomas Cort if (fstat(fileno(fout), &st) != -1 && !S_ISREG(st.st_mode)) {
103504203a83SThomas Cort progress = 0;
103604203a83SThomas Cort preserve = 0;
103704203a83SThomas Cort }
103804203a83SThomas Cort assert(rcvbuf_size > 0);
103904203a83SThomas Cort if ((size_t)rcvbuf_size > bufsize) {
104004203a83SThomas Cort if (buf)
104104203a83SThomas Cort (void)free(buf);
104204203a83SThomas Cort bufsize = rcvbuf_size;
104304203a83SThomas Cort buf = ftp_malloc(bufsize);
104404203a83SThomas Cort }
104504203a83SThomas Cort
104604203a83SThomas Cort progressmeter(-1);
104704203a83SThomas Cort hash_interval = (hash && (!progress || filesize < 0)) ? mark : 0;
104804203a83SThomas Cort
104904203a83SThomas Cort switch (curtype) {
105004203a83SThomas Cort
105104203a83SThomas Cort case TYPE_I:
105204203a83SThomas Cort case TYPE_L:
105304203a83SThomas Cort if (is_retr && restart_point &&
105404203a83SThomas Cort lseek(fileno(fout), restart_point, SEEK_SET) < 0) {
105504203a83SThomas Cort warn("Can't seek to restart `%s'", local);
105604203a83SThomas Cort goto cleanuprecv;
105704203a83SThomas Cort }
105804203a83SThomas Cort c = copy_bytes(fileno(din), fileno(fout), buf, bufsize,
105904203a83SThomas Cort rate_get, hash_interval);
106004203a83SThomas Cort if (c == 1) {
106104203a83SThomas Cort if (errno != EPIPE)
106204203a83SThomas Cort warn("Reading from network");
106304203a83SThomas Cort bytes = -1;
106404203a83SThomas Cort } else if (c == 2) {
106504203a83SThomas Cort warn("Writing `%s'", local);
106604203a83SThomas Cort }
106704203a83SThomas Cort break;
106804203a83SThomas Cort
106904203a83SThomas Cort case TYPE_A:
107004203a83SThomas Cort if (is_retr && restart_point) {
107104203a83SThomas Cort int ch;
107204203a83SThomas Cort off_t i;
107304203a83SThomas Cort
107404203a83SThomas Cort if (fseeko(fout, (off_t)0, SEEK_SET) < 0)
107504203a83SThomas Cort goto done;
107604203a83SThomas Cort for (i = 0; i++ < restart_point;) {
107704203a83SThomas Cort if ((ch = getc(fout)) == EOF)
107804203a83SThomas Cort goto done;
107904203a83SThomas Cort if (ch == '\n')
108004203a83SThomas Cort i++;
108104203a83SThomas Cort }
108204203a83SThomas Cort if (fseeko(fout, (off_t)0, SEEK_CUR) < 0) {
108304203a83SThomas Cort done:
108404203a83SThomas Cort warn("Can't seek to restart `%s'", local);
108504203a83SThomas Cort goto cleanuprecv;
108604203a83SThomas Cort }
108704203a83SThomas Cort }
108804203a83SThomas Cort while ((c = getc(din)) != EOF) {
108904203a83SThomas Cort if (c == '\n')
109004203a83SThomas Cort bare_lfs++;
109104203a83SThomas Cort while (c == '\r') {
109204203a83SThomas Cort while (hash_interval && bytes >= hashbytes) {
109304203a83SThomas Cort (void)putc('#', ttyout);
109404203a83SThomas Cort (void)fflush(ttyout);
109504203a83SThomas Cort hashbytes += mark;
109604203a83SThomas Cort }
109704203a83SThomas Cort bytes++;
109804203a83SThomas Cort if ((c = getc(din)) != '\n' || tcrflag) {
109904203a83SThomas Cort if (ferror(fout))
110004203a83SThomas Cort goto break2;
110104203a83SThomas Cort (void)putc('\r', fout);
110204203a83SThomas Cort if (c == '\0') {
110304203a83SThomas Cort bytes++;
110404203a83SThomas Cort goto contin2;
110504203a83SThomas Cort }
110604203a83SThomas Cort if (c == EOF)
110704203a83SThomas Cort goto contin2;
110804203a83SThomas Cort }
110904203a83SThomas Cort }
111004203a83SThomas Cort (void)putc(c, fout);
111104203a83SThomas Cort bytes++;
111204203a83SThomas Cort contin2: ;
111304203a83SThomas Cort }
111404203a83SThomas Cort break2:
111504203a83SThomas Cort if (hash_interval) {
111604203a83SThomas Cort if (bytes < hashbytes)
111704203a83SThomas Cort (void)putc('#', ttyout);
111804203a83SThomas Cort (void)putc('\n', ttyout);
111904203a83SThomas Cort }
112004203a83SThomas Cort if (ferror(din)) {
112104203a83SThomas Cort if (errno != EPIPE)
112204203a83SThomas Cort warn("Reading from network");
112304203a83SThomas Cort bytes = -1;
112404203a83SThomas Cort }
112504203a83SThomas Cort if (ferror(fout))
112604203a83SThomas Cort warn("Writing `%s'", local);
112704203a83SThomas Cort break;
112804203a83SThomas Cort }
112904203a83SThomas Cort
113004203a83SThomas Cort progressmeter(1);
113104203a83SThomas Cort if (closefunc != NULL) {
113204203a83SThomas Cort (*closefunc)(fout);
113304203a83SThomas Cort fout = NULL;
113404203a83SThomas Cort }
113504203a83SThomas Cort (void)fclose(din);
113604203a83SThomas Cort din = NULL;
113704203a83SThomas Cort (void)getreply(0);
113804203a83SThomas Cort if (bare_lfs) {
113904203a83SThomas Cort fprintf(ttyout,
114004203a83SThomas Cort "WARNING! %d bare linefeeds received in ASCII mode.\n",
114104203a83SThomas Cort bare_lfs);
114204203a83SThomas Cort fputs("File may not have transferred correctly.\n", ttyout);
114304203a83SThomas Cort }
114404203a83SThomas Cort if (bytes >= 0 && is_retr) {
114504203a83SThomas Cort if (bytes > 0)
114604203a83SThomas Cort ptransfer(0);
114704203a83SThomas Cort if (preserve && (closefunc == fclose)) {
114804203a83SThomas Cort mtime = remotemodtime(remote, 0);
114904203a83SThomas Cort if (mtime != -1) {
115004203a83SThomas Cort (void)gettimeofday(&tval[0], NULL);
115104203a83SThomas Cort tval[1].tv_sec = mtime;
115204203a83SThomas Cort tval[1].tv_usec = 0;
115304203a83SThomas Cort if (utimes(local, tval) == -1) {
115404203a83SThomas Cort fprintf(ttyout,
115504203a83SThomas Cort "Can't change modification time on %s to %s",
115604203a83SThomas Cort local,
115704203a83SThomas Cort rfc2822time(localtime(&mtime)));
115804203a83SThomas Cort }
115904203a83SThomas Cort }
116004203a83SThomas Cort }
116104203a83SThomas Cort }
116204203a83SThomas Cort goto cleanuprecv;
116304203a83SThomas Cort
116404203a83SThomas Cort abort:
116504203a83SThomas Cort /*
116604203a83SThomas Cort * abort using RFC 959 recommended IP,SYNC sequence
116704203a83SThomas Cort */
116804203a83SThomas Cort if (! sigsetjmp(xferabort, 1)) {
116904203a83SThomas Cort /* this is the first call */
117004203a83SThomas Cort (void)xsignal(SIGINT, abort_squared);
117104203a83SThomas Cort if (!cpend) {
117204203a83SThomas Cort code = -1;
117304203a83SThomas Cort goto cleanuprecv;
117404203a83SThomas Cort }
117504203a83SThomas Cort abort_remote(din);
117604203a83SThomas Cort }
117704203a83SThomas Cort code = -1;
117804203a83SThomas Cort if (bytes > 0)
117904203a83SThomas Cort ptransfer(0);
118004203a83SThomas Cort
118104203a83SThomas Cort cleanuprecv:
118204203a83SThomas Cort if (oldintr)
118304203a83SThomas Cort (void)xsignal(SIGINT, oldintr);
118404203a83SThomas Cort if (oldintp)
118504203a83SThomas Cort (void)xsignal(SIGPIPE, oldintp);
118604203a83SThomas Cort if (data >= 0) {
118704203a83SThomas Cort (void)close(data);
118804203a83SThomas Cort data = -1;
118904203a83SThomas Cort }
119004203a83SThomas Cort if (closefunc != NULL && fout != NULL)
119104203a83SThomas Cort (*closefunc)(fout);
119204203a83SThomas Cort if (din)
119304203a83SThomas Cort (void)fclose(din);
119404203a83SThomas Cort progress = oprogress;
119504203a83SThomas Cort preserve = opreserve;
119604203a83SThomas Cort bytes = 0;
119704203a83SThomas Cort }
119804203a83SThomas Cort
119904203a83SThomas Cort /*
120004203a83SThomas Cort * Need to start a listen on the data channel before we send the command,
120104203a83SThomas Cort * otherwise the server's connect may fail.
120204203a83SThomas Cort */
120304203a83SThomas Cort int
initconn(void)120404203a83SThomas Cort initconn(void)
120504203a83SThomas Cort {
120604203a83SThomas Cort char *p, *a;
120704203a83SThomas Cort int result, tmpno = 0;
120804203a83SThomas Cort int on = 1;
120904203a83SThomas Cort int error;
121004203a83SThomas Cort unsigned int addr[16], port[2];
121104203a83SThomas Cort unsigned int af, hal, pal;
121204203a83SThomas Cort socklen_t len;
121304203a83SThomas Cort const char *pasvcmd = NULL;
121404203a83SThomas Cort int overbose;
121504203a83SThomas Cort
121604203a83SThomas Cort #ifdef INET6
121704203a83SThomas Cort #ifndef NO_DEBUG
121804203a83SThomas Cort if (myctladdr.su_family == AF_INET6 && ftp_debug &&
121904203a83SThomas Cort (IN6_IS_ADDR_LINKLOCAL(&myctladdr.si_su.su_sin6.sin6_addr) ||
122004203a83SThomas Cort IN6_IS_ADDR_SITELOCAL(&myctladdr.si_su.su_sin6.sin6_addr))) {
122104203a83SThomas Cort warnx("Use of scoped addresses can be troublesome");
122204203a83SThomas Cort }
122304203a83SThomas Cort #endif
122404203a83SThomas Cort #endif
122504203a83SThomas Cort
122604203a83SThomas Cort reinit:
122704203a83SThomas Cort if (passivemode) {
122804203a83SThomas Cort data_addr = myctladdr;
122904203a83SThomas Cort data = socket(data_addr.su_family, SOCK_STREAM, 0);
123004203a83SThomas Cort if (data < 0) {
123104203a83SThomas Cort warn("Can't create socket for data connection");
123204203a83SThomas Cort return (1);
123304203a83SThomas Cort }
123404203a83SThomas Cort if ((options & SO_DEBUG) &&
123504203a83SThomas Cort setsockopt(data, SOL_SOCKET, SO_DEBUG,
123604203a83SThomas Cort (void *)&on, sizeof(on)) == -1) {
123704203a83SThomas Cort DWARN("setsockopt %s (ignored)", "SO_DEBUG");
123804203a83SThomas Cort }
123904203a83SThomas Cort result = COMPLETE + 1;
124004203a83SThomas Cort switch (data_addr.su_family) {
124104203a83SThomas Cort case AF_INET:
124204203a83SThomas Cort if (epsv4 && !epsv4bad) {
124304203a83SThomas Cort pasvcmd = "EPSV";
124404203a83SThomas Cort overbose = verbose;
124504203a83SThomas Cort if (ftp_debug == 0)
124604203a83SThomas Cort verbose = -1;
124704203a83SThomas Cort result = command("EPSV");
124804203a83SThomas Cort verbose = overbose;
124904203a83SThomas Cort if (verbose > 0 &&
125004203a83SThomas Cort (result == COMPLETE || !connected))
125104203a83SThomas Cort fprintf(ttyout, "%s\n", reply_string);
125204203a83SThomas Cort if (!connected)
125304203a83SThomas Cort return (1);
125404203a83SThomas Cort /*
125504203a83SThomas Cort * this code is to be friendly with broken
125604203a83SThomas Cort * BSDI ftpd
125704203a83SThomas Cort */
125804203a83SThomas Cort if (code / 10 == 22 && code != 229) {
125904203a83SThomas Cort fputs(
126004203a83SThomas Cort "wrong server: return code must be 229\n",
126104203a83SThomas Cort ttyout);
126204203a83SThomas Cort result = COMPLETE + 1;
126304203a83SThomas Cort }
126404203a83SThomas Cort if (result != COMPLETE) {
126504203a83SThomas Cort epsv4bad = 1;
126604203a83SThomas Cort DPRINTF("disabling epsv4 for this "
126704203a83SThomas Cort "connection\n");
126804203a83SThomas Cort }
126904203a83SThomas Cort }
127004203a83SThomas Cort if (result != COMPLETE) {
127104203a83SThomas Cort pasvcmd = "PASV";
127204203a83SThomas Cort result = command("PASV");
127304203a83SThomas Cort if (!connected)
127404203a83SThomas Cort return (1);
127504203a83SThomas Cort }
127604203a83SThomas Cort break;
127704203a83SThomas Cort #ifdef INET6
127804203a83SThomas Cort case AF_INET6:
127904203a83SThomas Cort if (epsv6 && !epsv6bad) {
128004203a83SThomas Cort pasvcmd = "EPSV";
128104203a83SThomas Cort overbose = verbose;
128204203a83SThomas Cort if (ftp_debug == 0)
128304203a83SThomas Cort verbose = -1;
128404203a83SThomas Cort result = command("EPSV");
128504203a83SThomas Cort verbose = overbose;
128604203a83SThomas Cort if (verbose > 0 &&
128704203a83SThomas Cort (result == COMPLETE || !connected))
128804203a83SThomas Cort fprintf(ttyout, "%s\n", reply_string);
128904203a83SThomas Cort if (!connected)
129004203a83SThomas Cort return (1);
129104203a83SThomas Cort /*
129204203a83SThomas Cort * this code is to be friendly with
129304203a83SThomas Cort * broken BSDI ftpd
129404203a83SThomas Cort */
129504203a83SThomas Cort if (code / 10 == 22 && code != 229) {
129604203a83SThomas Cort fputs(
129704203a83SThomas Cort "wrong server: return code must be 229\n",
129804203a83SThomas Cort ttyout);
129904203a83SThomas Cort result = COMPLETE + 1;
130004203a83SThomas Cort }
130104203a83SThomas Cort if (result != COMPLETE) {
130204203a83SThomas Cort epsv6bad = 1;
130304203a83SThomas Cort DPRINTF("disabling epsv6 for this "
130404203a83SThomas Cort "connection\n");
130504203a83SThomas Cort }
130604203a83SThomas Cort }
130704203a83SThomas Cort if (result != COMPLETE) {
130804203a83SThomas Cort pasvcmd = "LPSV";
130904203a83SThomas Cort result = command("LPSV");
131004203a83SThomas Cort }
131104203a83SThomas Cort if (!connected)
131204203a83SThomas Cort return (1);
131304203a83SThomas Cort break;
131404203a83SThomas Cort #endif
131504203a83SThomas Cort default:
131604203a83SThomas Cort result = COMPLETE + 1;
131704203a83SThomas Cort break;
131804203a83SThomas Cort }
131904203a83SThomas Cort if (result != COMPLETE) {
132004203a83SThomas Cort if (activefallback) {
132104203a83SThomas Cort (void)close(data);
132204203a83SThomas Cort data = -1;
132304203a83SThomas Cort passivemode = 0;
132404203a83SThomas Cort #if 0
132504203a83SThomas Cort activefallback = 0;
132604203a83SThomas Cort #endif
132704203a83SThomas Cort goto reinit;
132804203a83SThomas Cort }
132904203a83SThomas Cort fputs("Passive mode refused.\n", ttyout);
133004203a83SThomas Cort goto bad;
133104203a83SThomas Cort }
133204203a83SThomas Cort
133304203a83SThomas Cort #define pack2(var, off) \
133404203a83SThomas Cort (((var[(off) + 0] & 0xff) << 8) | ((var[(off) + 1] & 0xff) << 0))
133504203a83SThomas Cort #define pack4(var, off) \
133604203a83SThomas Cort (((var[(off) + 0] & 0xff) << 24) | ((var[(off) + 1] & 0xff) << 16) | \
133704203a83SThomas Cort ((var[(off) + 2] & 0xff) << 8) | ((var[(off) + 3] & 0xff) << 0))
133804203a83SThomas Cort #define UC(b) (((int)b)&0xff)
133904203a83SThomas Cort
134004203a83SThomas Cort /*
134104203a83SThomas Cort * What we've got at this point is a string of comma separated
134204203a83SThomas Cort * one-byte unsigned integer values, separated by commas.
134304203a83SThomas Cort */
134404203a83SThomas Cort if (strcmp(pasvcmd, "PASV") == 0) {
134504203a83SThomas Cort if (data_addr.su_family != AF_INET) {
134604203a83SThomas Cort fputs(
134704203a83SThomas Cort "Passive mode AF mismatch. Shouldn't happen!\n", ttyout);
134804203a83SThomas Cort error = 1;
134904203a83SThomas Cort goto bad;
135004203a83SThomas Cort }
135104203a83SThomas Cort if (code / 10 == 22 && code != 227) {
135204203a83SThomas Cort fputs("wrong server: return code must be 227\n",
135304203a83SThomas Cort ttyout);
135404203a83SThomas Cort error = 1;
135504203a83SThomas Cort goto bad;
135604203a83SThomas Cort }
135704203a83SThomas Cort error = sscanf(pasv, "%u,%u,%u,%u,%u,%u",
135804203a83SThomas Cort &addr[0], &addr[1], &addr[2], &addr[3],
135904203a83SThomas Cort &port[0], &port[1]);
136004203a83SThomas Cort if (error != 6) {
136104203a83SThomas Cort fputs(
136204203a83SThomas Cort "Passive mode address scan failure. Shouldn't happen!\n", ttyout);
136304203a83SThomas Cort error = 1;
136404203a83SThomas Cort goto bad;
136504203a83SThomas Cort }
136604203a83SThomas Cort error = 0;
136704203a83SThomas Cort memset(&data_addr, 0, sizeof(data_addr));
136804203a83SThomas Cort data_addr.su_family = AF_INET;
136904203a83SThomas Cort data_addr.su_len = sizeof(struct sockaddr_in);
137004203a83SThomas Cort data_addr.si_su.su_sin.sin_addr.s_addr =
137104203a83SThomas Cort htonl(pack4(addr, 0));
137204203a83SThomas Cort data_addr.su_port = htons(pack2(port, 0));
137304203a83SThomas Cort } else if (strcmp(pasvcmd, "LPSV") == 0) {
137404203a83SThomas Cort if (code / 10 == 22 && code != 228) {
137504203a83SThomas Cort fputs("wrong server: return code must be 228\n",
137604203a83SThomas Cort ttyout);
137704203a83SThomas Cort error = 1;
137804203a83SThomas Cort goto bad;
137904203a83SThomas Cort }
138004203a83SThomas Cort switch (data_addr.su_family) {
138104203a83SThomas Cort case AF_INET:
138204203a83SThomas Cort error = sscanf(pasv,
138304203a83SThomas Cort "%u,%u,%u,%u,%u,%u,%u,%u,%u",
138404203a83SThomas Cort &af, &hal,
138504203a83SThomas Cort &addr[0], &addr[1], &addr[2], &addr[3],
138604203a83SThomas Cort &pal, &port[0], &port[1]);
138704203a83SThomas Cort if (error != 9) {
138804203a83SThomas Cort fputs(
138904203a83SThomas Cort "Passive mode address scan failure. Shouldn't happen!\n", ttyout);
139004203a83SThomas Cort error = 1;
139104203a83SThomas Cort goto bad;
139204203a83SThomas Cort }
139304203a83SThomas Cort if (af != 4 || hal != 4 || pal != 2) {
139404203a83SThomas Cort fputs(
139504203a83SThomas Cort "Passive mode AF mismatch. Shouldn't happen!\n", ttyout);
139604203a83SThomas Cort error = 1;
139704203a83SThomas Cort goto bad;
139804203a83SThomas Cort }
139904203a83SThomas Cort
140004203a83SThomas Cort error = 0;
140104203a83SThomas Cort memset(&data_addr, 0, sizeof(data_addr));
140204203a83SThomas Cort data_addr.su_family = AF_INET;
140304203a83SThomas Cort data_addr.su_len = sizeof(struct sockaddr_in);
140404203a83SThomas Cort data_addr.si_su.su_sin.sin_addr.s_addr =
140504203a83SThomas Cort htonl(pack4(addr, 0));
140604203a83SThomas Cort data_addr.su_port = htons(pack2(port, 0));
140704203a83SThomas Cort break;
140804203a83SThomas Cort #ifdef INET6
140904203a83SThomas Cort case AF_INET6:
141004203a83SThomas Cort error = sscanf(pasv,
141104203a83SThomas Cort "%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u",
141204203a83SThomas Cort &af, &hal,
141304203a83SThomas Cort &addr[0], &addr[1], &addr[2], &addr[3],
141404203a83SThomas Cort &addr[4], &addr[5], &addr[6], &addr[7],
141504203a83SThomas Cort &addr[8], &addr[9], &addr[10],
141604203a83SThomas Cort &addr[11], &addr[12], &addr[13],
141704203a83SThomas Cort &addr[14], &addr[15],
141804203a83SThomas Cort &pal, &port[0], &port[1]);
141904203a83SThomas Cort if (error != 21) {
142004203a83SThomas Cort fputs(
142104203a83SThomas Cort "Passive mode address scan failure. Shouldn't happen!\n", ttyout);
142204203a83SThomas Cort error = 1;
142304203a83SThomas Cort goto bad;
142404203a83SThomas Cort }
142504203a83SThomas Cort if (af != 6 || hal != 16 || pal != 2) {
142604203a83SThomas Cort fputs(
142704203a83SThomas Cort "Passive mode AF mismatch. Shouldn't happen!\n", ttyout);
142804203a83SThomas Cort error = 1;
142904203a83SThomas Cort goto bad;
143004203a83SThomas Cort }
143104203a83SThomas Cort
143204203a83SThomas Cort error = 0;
143304203a83SThomas Cort memset(&data_addr, 0, sizeof(data_addr));
143404203a83SThomas Cort data_addr.su_family = AF_INET6;
143504203a83SThomas Cort data_addr.su_len = sizeof(struct sockaddr_in6);
143604203a83SThomas Cort {
143704203a83SThomas Cort size_t i;
143804203a83SThomas Cort for (i = 0; i < sizeof(struct in6_addr); i++) {
143904203a83SThomas Cort data_addr.si_su.su_sin6.sin6_addr.s6_addr[i] =
144004203a83SThomas Cort UC(addr[i]);
144104203a83SThomas Cort }
144204203a83SThomas Cort }
144304203a83SThomas Cort data_addr.su_port = htons(pack2(port, 0));
144404203a83SThomas Cort break;
144504203a83SThomas Cort #endif
144604203a83SThomas Cort default:
144704203a83SThomas Cort error = 1;
144804203a83SThomas Cort }
144904203a83SThomas Cort } else if (strcmp(pasvcmd, "EPSV") == 0) {
145004203a83SThomas Cort char delim[4];
145104203a83SThomas Cort
145204203a83SThomas Cort port[0] = 0;
145304203a83SThomas Cort if (code / 10 == 22 && code != 229) {
145404203a83SThomas Cort fputs("wrong server: return code must be 229\n",
145504203a83SThomas Cort ttyout);
145604203a83SThomas Cort error = 1;
145704203a83SThomas Cort goto bad;
145804203a83SThomas Cort }
145904203a83SThomas Cort if (sscanf(pasv, "%c%c%c%d%c", &delim[0],
146004203a83SThomas Cort &delim[1], &delim[2], &port[1],
146104203a83SThomas Cort &delim[3]) != 5) {
146204203a83SThomas Cort fputs("parse error!\n", ttyout);
146304203a83SThomas Cort error = 1;
146404203a83SThomas Cort goto bad;
146504203a83SThomas Cort }
146604203a83SThomas Cort if (delim[0] != delim[1] || delim[0] != delim[2]
146704203a83SThomas Cort || delim[0] != delim[3]) {
146804203a83SThomas Cort fputs("parse error!\n", ttyout);
146904203a83SThomas Cort error = 1;
147004203a83SThomas Cort goto bad;
147104203a83SThomas Cort }
147204203a83SThomas Cort data_addr = hisctladdr;
147304203a83SThomas Cort data_addr.su_port = htons(port[1]);
147404203a83SThomas Cort } else
147504203a83SThomas Cort goto bad;
147604203a83SThomas Cort
147704203a83SThomas Cort if (ftp_connect(data, (struct sockaddr *)&data_addr.si_su,
147804203a83SThomas Cort data_addr.su_len, 1) < 0) {
147904203a83SThomas Cort if (activefallback) {
148004203a83SThomas Cort (void)close(data);
148104203a83SThomas Cort data = -1;
148204203a83SThomas Cort passivemode = 0;
148304203a83SThomas Cort #if 0
148404203a83SThomas Cort activefallback = 0;
148504203a83SThomas Cort #endif
148604203a83SThomas Cort goto reinit;
148704203a83SThomas Cort }
148804203a83SThomas Cort goto bad;
148904203a83SThomas Cort }
149004203a83SThomas Cort #ifdef IPTOS_THROUGHPUT
149104203a83SThomas Cort if (data_addr.su_family == AF_INET) {
149204203a83SThomas Cort on = IPTOS_THROUGHPUT;
149304203a83SThomas Cort if (setsockopt(data, IPPROTO_IP, IP_TOS,
149404203a83SThomas Cort (void *)&on, sizeof(on)) == -1) {
149504203a83SThomas Cort DWARN("setsockopt %s (ignored)",
149604203a83SThomas Cort "IPTOS_THROUGHPUT");
149704203a83SThomas Cort }
149804203a83SThomas Cort }
149904203a83SThomas Cort #endif
150004203a83SThomas Cort return (0);
150104203a83SThomas Cort }
150204203a83SThomas Cort
150304203a83SThomas Cort noport:
150404203a83SThomas Cort data_addr = myctladdr;
150504203a83SThomas Cort if (sendport)
150604203a83SThomas Cort data_addr.su_port = 0; /* let system pick one */
150704203a83SThomas Cort if (data != -1)
150804203a83SThomas Cort (void)close(data);
150904203a83SThomas Cort data = socket(data_addr.su_family, SOCK_STREAM, 0);
151004203a83SThomas Cort if (data < 0) {
151104203a83SThomas Cort warn("Can't create socket for data connection");
151204203a83SThomas Cort if (tmpno)
151304203a83SThomas Cort sendport = 1;
151404203a83SThomas Cort return (1);
151504203a83SThomas Cort }
151604203a83SThomas Cort if (!sendport)
151704203a83SThomas Cort if (setsockopt(data, SOL_SOCKET, SO_REUSEADDR,
151804203a83SThomas Cort (void *)&on, sizeof(on)) == -1) {
151904203a83SThomas Cort warn("Can't set SO_REUSEADDR on data connection");
152004203a83SThomas Cort goto bad;
152104203a83SThomas Cort }
152204203a83SThomas Cort if (bind(data, (struct sockaddr *)&data_addr.si_su,
152304203a83SThomas Cort data_addr.su_len) < 0) {
152404203a83SThomas Cort warn("Can't bind for data connection");
152504203a83SThomas Cort goto bad;
152604203a83SThomas Cort }
152704203a83SThomas Cort if ((options & SO_DEBUG) &&
152804203a83SThomas Cort setsockopt(data, SOL_SOCKET, SO_DEBUG,
152904203a83SThomas Cort (void *)&on, sizeof(on)) == -1) {
153004203a83SThomas Cort DWARN("setsockopt %s (ignored)", "SO_DEBUG");
153104203a83SThomas Cort }
153204203a83SThomas Cort len = sizeof(data_addr.si_su);
153304203a83SThomas Cort memset((char *)&data_addr, 0, sizeof (data_addr));
153404203a83SThomas Cort if (getsockname(data, (struct sockaddr *)&data_addr.si_su, &len) == -1) {
153504203a83SThomas Cort warn("Can't determine my address of data connection");
153604203a83SThomas Cort goto bad;
153704203a83SThomas Cort }
153804203a83SThomas Cort data_addr.su_len = len;
153904203a83SThomas Cort if (ftp_listen(data, 1) < 0)
154004203a83SThomas Cort warn("Can't listen to data connection");
154104203a83SThomas Cort
154204203a83SThomas Cort if (sendport) {
154304203a83SThomas Cort char hname[NI_MAXHOST], sname[NI_MAXSERV];
154404203a83SThomas Cort struct sockinet tmp;
154504203a83SThomas Cort
154604203a83SThomas Cort switch (data_addr.su_family) {
154704203a83SThomas Cort case AF_INET:
154804203a83SThomas Cort if (!epsv4 || epsv4bad) {
154904203a83SThomas Cort result = COMPLETE + 1;
155004203a83SThomas Cort break;
155104203a83SThomas Cort }
155204203a83SThomas Cort /* FALLTHROUGH */
155304203a83SThomas Cort #ifdef INET6
155404203a83SThomas Cort case AF_INET6:
155504203a83SThomas Cort if (!epsv6 || epsv6bad) {
155604203a83SThomas Cort result = COMPLETE + 1;
155704203a83SThomas Cort break;
155804203a83SThomas Cort }
155904203a83SThomas Cort #endif
156004203a83SThomas Cort af = (data_addr.su_family == AF_INET) ? 1 : 2;
156104203a83SThomas Cort tmp = data_addr;
156204203a83SThomas Cort #ifdef INET6
156304203a83SThomas Cort if (tmp.su_family == AF_INET6)
156404203a83SThomas Cort tmp.si_su.su_sin6.sin6_scope_id = 0;
156504203a83SThomas Cort #endif
156604203a83SThomas Cort if (getnameinfo((struct sockaddr *)&tmp.si_su,
156704203a83SThomas Cort tmp.su_len, hname, sizeof(hname), sname,
156804203a83SThomas Cort sizeof(sname), NI_NUMERICHOST | NI_NUMERICSERV)) {
156904203a83SThomas Cort result = ERROR;
157004203a83SThomas Cort } else {
157104203a83SThomas Cort overbose = verbose;
157204203a83SThomas Cort if (ftp_debug == 0)
157304203a83SThomas Cort verbose = -1;
157404203a83SThomas Cort result = command("EPRT |%u|%s|%s|", af, hname,
157504203a83SThomas Cort sname);
157604203a83SThomas Cort verbose = overbose;
157704203a83SThomas Cort if (verbose > 0 &&
157804203a83SThomas Cort (result == COMPLETE || !connected))
157904203a83SThomas Cort fprintf(ttyout, "%s\n", reply_string);
158004203a83SThomas Cort if (!connected)
158104203a83SThomas Cort return (1);
158204203a83SThomas Cort if (result != COMPLETE) {
158304203a83SThomas Cort epsv4bad = 1;
158404203a83SThomas Cort DPRINTF("disabling epsv4 for this "
158504203a83SThomas Cort "connection\n");
158604203a83SThomas Cort }
158704203a83SThomas Cort }
158804203a83SThomas Cort break;
158904203a83SThomas Cort default:
159004203a83SThomas Cort result = COMPLETE + 1;
159104203a83SThomas Cort break;
159204203a83SThomas Cort }
159304203a83SThomas Cort if (result == COMPLETE)
159404203a83SThomas Cort goto skip_port;
159504203a83SThomas Cort
159604203a83SThomas Cort switch (data_addr.su_family) {
159704203a83SThomas Cort case AF_INET:
159804203a83SThomas Cort a = (char *)&data_addr.si_su.su_sin.sin_addr;
159904203a83SThomas Cort p = (char *)&data_addr.su_port;
160004203a83SThomas Cort result = command("PORT %d,%d,%d,%d,%d,%d",
160104203a83SThomas Cort UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
160204203a83SThomas Cort UC(p[0]), UC(p[1]));
160304203a83SThomas Cort break;
160404203a83SThomas Cort #ifdef INET6
160504203a83SThomas Cort case AF_INET6: {
160604203a83SThomas Cort uint8_t ua[sizeof(data_addr.si_su.su_sin6.sin6_addr)];
160704203a83SThomas Cort uint8_t up[sizeof(data_addr.su_port)];
160804203a83SThomas Cort
160904203a83SThomas Cort memcpy(ua, &data_addr.si_su.su_sin6.sin6_addr,
161004203a83SThomas Cort sizeof(ua));
161104203a83SThomas Cort memcpy(up, &data_addr.su_port, sizeof(up));
161204203a83SThomas Cort
161304203a83SThomas Cort result = command(
161404203a83SThomas Cort "LPRT %d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
161504203a83SThomas Cort 6, 16,
161604203a83SThomas Cort ua[0], ua[1], ua[2], ua[3],
161704203a83SThomas Cort ua[4], ua[5], ua[6], ua[7],
161804203a83SThomas Cort ua[8], ua[9], ua[10], ua[11],
161904203a83SThomas Cort ua[12], ua[13], ua[14], ua[15],
162004203a83SThomas Cort 2,
162104203a83SThomas Cort up[0], up[1]);
162204203a83SThomas Cort break;
162304203a83SThomas Cort }
162404203a83SThomas Cort #endif
162504203a83SThomas Cort default:
162604203a83SThomas Cort result = COMPLETE + 1; /* xxx */
162704203a83SThomas Cort }
162804203a83SThomas Cort if (!connected)
162904203a83SThomas Cort return (1);
163004203a83SThomas Cort skip_port:
163104203a83SThomas Cort
163204203a83SThomas Cort if (result == ERROR && sendport == -1) {
163304203a83SThomas Cort sendport = 0;
163404203a83SThomas Cort tmpno = 1;
163504203a83SThomas Cort goto noport;
163604203a83SThomas Cort }
163704203a83SThomas Cort return (result != COMPLETE);
163804203a83SThomas Cort }
163904203a83SThomas Cort if (tmpno)
164004203a83SThomas Cort sendport = 1;
164104203a83SThomas Cort #ifdef IPTOS_THROUGHPUT
164204203a83SThomas Cort if (data_addr.su_family == AF_INET) {
164304203a83SThomas Cort on = IPTOS_THROUGHPUT;
164404203a83SThomas Cort if (setsockopt(data, IPPROTO_IP, IP_TOS,
164504203a83SThomas Cort (void *)&on, sizeof(on)) == -1) {
164604203a83SThomas Cort DWARN("setsockopt %s (ignored)", "IPTOS_THROUGHPUT");
164704203a83SThomas Cort }
164804203a83SThomas Cort }
164904203a83SThomas Cort #endif
165004203a83SThomas Cort return (0);
165104203a83SThomas Cort bad:
165204203a83SThomas Cort (void)close(data);
165304203a83SThomas Cort data = -1;
165404203a83SThomas Cort if (tmpno)
165504203a83SThomas Cort sendport = 1;
165604203a83SThomas Cort return (1);
165704203a83SThomas Cort }
165804203a83SThomas Cort
165904203a83SThomas Cort FILE *
dataconn(const char * lmode)166004203a83SThomas Cort dataconn(const char *lmode)
166104203a83SThomas Cort {
166204203a83SThomas Cort struct sockinet from;
166304203a83SThomas Cort int s, flags, rv, timeout;
166404203a83SThomas Cort struct timeval endtime, now, td;
166504203a83SThomas Cort struct pollfd pfd[1];
166604203a83SThomas Cort socklen_t fromlen;
166704203a83SThomas Cort
166804203a83SThomas Cort if (passivemode) /* passive data connection */
166904203a83SThomas Cort return (fdopen(data, lmode));
167004203a83SThomas Cort
167104203a83SThomas Cort /* active mode data connection */
167204203a83SThomas Cort
167304203a83SThomas Cort if ((flags = fcntl(data, F_GETFL, 0)) == -1)
167404203a83SThomas Cort goto dataconn_failed; /* get current socket flags */
167504203a83SThomas Cort if (fcntl(data, F_SETFL, flags | O_NONBLOCK) == -1)
167604203a83SThomas Cort goto dataconn_failed; /* set non-blocking connect */
167704203a83SThomas Cort
167804203a83SThomas Cort /* NOTE: we now must restore socket flags on successful exit */
167904203a83SThomas Cort
168004203a83SThomas Cort /* limit time waiting on listening socket */
168104203a83SThomas Cort pfd[0].fd = data;
168204203a83SThomas Cort pfd[0].events = POLLIN;
168304203a83SThomas Cort (void)gettimeofday(&endtime, NULL); /* determine end time */
168404203a83SThomas Cort endtime.tv_sec += (quit_time > 0) ? quit_time: 60;
168504203a83SThomas Cort /* without -q, default to 60s */
168604203a83SThomas Cort do {
168704203a83SThomas Cort (void)gettimeofday(&now, NULL);
168804203a83SThomas Cort timersub(&endtime, &now, &td);
168904203a83SThomas Cort timeout = td.tv_sec * 1000 + td.tv_usec/1000;
169004203a83SThomas Cort if (timeout < 0)
169104203a83SThomas Cort timeout = 0;
169204203a83SThomas Cort rv = ftp_poll(pfd, 1, timeout);
169304203a83SThomas Cort } while (rv == -1 && errno == EINTR); /* loop until poll ! EINTR */
169404203a83SThomas Cort if (rv == -1) {
169504203a83SThomas Cort warn("Can't poll waiting before accept");
169604203a83SThomas Cort goto dataconn_failed;
169704203a83SThomas Cort }
169804203a83SThomas Cort if (rv == 0) {
169904203a83SThomas Cort warnx("Poll timeout waiting before accept");
170004203a83SThomas Cort goto dataconn_failed;
170104203a83SThomas Cort }
170204203a83SThomas Cort
170304203a83SThomas Cort /* (non-blocking) accept the connection */
170404203a83SThomas Cort fromlen = myctladdr.su_len;
170504203a83SThomas Cort do {
170604203a83SThomas Cort s = accept(data, (struct sockaddr *) &from.si_su, &fromlen);
170704203a83SThomas Cort } while (s == -1 && errno == EINTR); /* loop until accept ! EINTR */
170804203a83SThomas Cort if (s == -1) {
170904203a83SThomas Cort warn("Can't accept data connection");
171004203a83SThomas Cort goto dataconn_failed;
171104203a83SThomas Cort }
171204203a83SThomas Cort
171304203a83SThomas Cort (void)close(data);
171404203a83SThomas Cort data = s;
171504203a83SThomas Cort if (fcntl(data, F_SETFL, flags) == -1) /* restore socket flags */
171604203a83SThomas Cort goto dataconn_failed;
171704203a83SThomas Cort
171804203a83SThomas Cort #ifdef IPTOS_THROUGHPUT
171904203a83SThomas Cort if (from.su_family == AF_INET) {
172004203a83SThomas Cort int tos = IPTOS_THROUGHPUT;
172104203a83SThomas Cort if (setsockopt(s, IPPROTO_IP, IP_TOS,
172204203a83SThomas Cort (void *)&tos, sizeof(tos)) == -1) {
172304203a83SThomas Cort DWARN("setsockopt %s (ignored)", "IPTOS_THROUGHPUT");
172404203a83SThomas Cort }
172504203a83SThomas Cort }
172604203a83SThomas Cort #endif
172704203a83SThomas Cort return (fdopen(data, lmode));
172804203a83SThomas Cort
172904203a83SThomas Cort dataconn_failed:
173004203a83SThomas Cort (void)close(data);
173104203a83SThomas Cort data = -1;
173204203a83SThomas Cort return (NULL);
173304203a83SThomas Cort }
173404203a83SThomas Cort
173504203a83SThomas Cort void
psabort(int notused)173604203a83SThomas Cort psabort(int notused)
173704203a83SThomas Cort {
173804203a83SThomas Cort int oerrno = errno;
173904203a83SThomas Cort
174004203a83SThomas Cort sigint_raised = 1;
174104203a83SThomas Cort alarmtimer(0);
174204203a83SThomas Cort abrtflag++;
174304203a83SThomas Cort errno = oerrno;
174404203a83SThomas Cort }
174504203a83SThomas Cort
174604203a83SThomas Cort void
pswitch(int flag)174704203a83SThomas Cort pswitch(int flag)
174804203a83SThomas Cort {
174904203a83SThomas Cort sigfunc oldintr;
175004203a83SThomas Cort static struct comvars {
175104203a83SThomas Cort int connect;
175204203a83SThomas Cort char name[MAXHOSTNAMELEN];
175304203a83SThomas Cort struct sockinet mctl;
175404203a83SThomas Cort struct sockinet hctl;
175504203a83SThomas Cort FILE *in;
175604203a83SThomas Cort FILE *out;
175704203a83SThomas Cort int tpe;
175804203a83SThomas Cort int curtpe;
175904203a83SThomas Cort int cpnd;
176004203a83SThomas Cort int sunqe;
176104203a83SThomas Cort int runqe;
176204203a83SThomas Cort int mcse;
176304203a83SThomas Cort int ntflg;
176404203a83SThomas Cort char nti[17];
176504203a83SThomas Cort char nto[17];
176604203a83SThomas Cort int mapflg;
176704203a83SThomas Cort char mi[MAXPATHLEN];
176804203a83SThomas Cort char mo[MAXPATHLEN];
176904203a83SThomas Cort } proxstruct, tmpstruct;
177004203a83SThomas Cort struct comvars *ip, *op;
177104203a83SThomas Cort
177204203a83SThomas Cort abrtflag = 0;
177304203a83SThomas Cort oldintr = xsignal(SIGINT, psabort);
177404203a83SThomas Cort if (flag) {
177504203a83SThomas Cort if (proxy)
177604203a83SThomas Cort return;
177704203a83SThomas Cort ip = &tmpstruct;
177804203a83SThomas Cort op = &proxstruct;
177904203a83SThomas Cort proxy++;
178004203a83SThomas Cort } else {
178104203a83SThomas Cort if (!proxy)
178204203a83SThomas Cort return;
178304203a83SThomas Cort ip = &proxstruct;
178404203a83SThomas Cort op = &tmpstruct;
178504203a83SThomas Cort proxy = 0;
178604203a83SThomas Cort }
178704203a83SThomas Cort ip->connect = connected;
178804203a83SThomas Cort connected = op->connect;
178904203a83SThomas Cort if (hostname)
179004203a83SThomas Cort (void)strlcpy(ip->name, hostname, sizeof(ip->name));
179104203a83SThomas Cort else
179204203a83SThomas Cort ip->name[0] = '\0';
179304203a83SThomas Cort hostname = op->name;
179404203a83SThomas Cort ip->hctl = hisctladdr;
179504203a83SThomas Cort hisctladdr = op->hctl;
179604203a83SThomas Cort ip->mctl = myctladdr;
179704203a83SThomas Cort myctladdr = op->mctl;
179804203a83SThomas Cort ip->in = cin;
179904203a83SThomas Cort cin = op->in;
180004203a83SThomas Cort ip->out = cout;
180104203a83SThomas Cort cout = op->out;
180204203a83SThomas Cort ip->tpe = type;
180304203a83SThomas Cort type = op->tpe;
180404203a83SThomas Cort ip->curtpe = curtype;
180504203a83SThomas Cort curtype = op->curtpe;
180604203a83SThomas Cort ip->cpnd = cpend;
180704203a83SThomas Cort cpend = op->cpnd;
180804203a83SThomas Cort ip->sunqe = sunique;
180904203a83SThomas Cort sunique = op->sunqe;
181004203a83SThomas Cort ip->runqe = runique;
181104203a83SThomas Cort runique = op->runqe;
181204203a83SThomas Cort ip->mcse = mcase;
181304203a83SThomas Cort mcase = op->mcse;
181404203a83SThomas Cort ip->ntflg = ntflag;
181504203a83SThomas Cort ntflag = op->ntflg;
181604203a83SThomas Cort (void)strlcpy(ip->nti, ntin, sizeof(ip->nti));
181704203a83SThomas Cort (void)strlcpy(ntin, op->nti, sizeof(ntin));
181804203a83SThomas Cort (void)strlcpy(ip->nto, ntout, sizeof(ip->nto));
181904203a83SThomas Cort (void)strlcpy(ntout, op->nto, sizeof(ntout));
182004203a83SThomas Cort ip->mapflg = mapflag;
182104203a83SThomas Cort mapflag = op->mapflg;
182204203a83SThomas Cort (void)strlcpy(ip->mi, mapin, sizeof(ip->mi));
182304203a83SThomas Cort (void)strlcpy(mapin, op->mi, sizeof(mapin));
182404203a83SThomas Cort (void)strlcpy(ip->mo, mapout, sizeof(ip->mo));
182504203a83SThomas Cort (void)strlcpy(mapout, op->mo, sizeof(mapout));
182604203a83SThomas Cort (void)xsignal(SIGINT, oldintr);
182704203a83SThomas Cort if (abrtflag) {
182804203a83SThomas Cort abrtflag = 0;
182904203a83SThomas Cort (*oldintr)(SIGINT);
183004203a83SThomas Cort }
183104203a83SThomas Cort }
183204203a83SThomas Cort
183304203a83SThomas Cort __dead static void
abortpt(int notused)183404203a83SThomas Cort abortpt(int notused)
183504203a83SThomas Cort {
183604203a83SThomas Cort
183704203a83SThomas Cort sigint_raised = 1;
183804203a83SThomas Cort alarmtimer(0);
183904203a83SThomas Cort if (fromatty)
184004203a83SThomas Cort write(fileno(ttyout), "\n", 1);
184104203a83SThomas Cort ptabflg++;
184204203a83SThomas Cort mflag = 0;
184304203a83SThomas Cort abrtflag = 0;
184404203a83SThomas Cort siglongjmp(ptabort, 1);
184504203a83SThomas Cort }
184604203a83SThomas Cort
184704203a83SThomas Cort void
proxtrans(const char * cmd,const char * local,const char * remote)184804203a83SThomas Cort proxtrans(const char *cmd, const char *local, const char *remote)
184904203a83SThomas Cort {
185004203a83SThomas Cort sigfunc volatile oldintr;
185104203a83SThomas Cort int prox_type, nfnd;
185204203a83SThomas Cort int volatile secndflag;
185304203a83SThomas Cort const char *volatile cmd2;
185404203a83SThomas Cort
185504203a83SThomas Cort oldintr = NULL;
185604203a83SThomas Cort secndflag = 0;
185704203a83SThomas Cort if (strcmp(cmd, "RETR"))
185804203a83SThomas Cort cmd2 = "RETR";
185904203a83SThomas Cort else
186004203a83SThomas Cort cmd2 = runique ? "STOU" : "STOR";
186104203a83SThomas Cort if ((prox_type = type) == 0) {
186204203a83SThomas Cort if (unix_server && unix_proxy)
186304203a83SThomas Cort prox_type = TYPE_I;
186404203a83SThomas Cort else
186504203a83SThomas Cort prox_type = TYPE_A;
186604203a83SThomas Cort }
186704203a83SThomas Cort if (curtype != prox_type)
186804203a83SThomas Cort changetype(prox_type, 1);
186904203a83SThomas Cort if (command("PASV") != COMPLETE) {
187004203a83SThomas Cort fputs("proxy server does not support third party transfers.\n",
187104203a83SThomas Cort ttyout);
187204203a83SThomas Cort return;
187304203a83SThomas Cort }
187404203a83SThomas Cort pswitch(0);
187504203a83SThomas Cort if (!connected) {
187604203a83SThomas Cort fputs("No primary connection.\n", ttyout);
187704203a83SThomas Cort pswitch(1);
187804203a83SThomas Cort code = -1;
187904203a83SThomas Cort return;
188004203a83SThomas Cort }
188104203a83SThomas Cort if (curtype != prox_type)
188204203a83SThomas Cort changetype(prox_type, 1);
188304203a83SThomas Cort if (command("PORT %s", pasv) != COMPLETE) {
188404203a83SThomas Cort pswitch(1);
188504203a83SThomas Cort return;
188604203a83SThomas Cort }
188704203a83SThomas Cort if (sigsetjmp(ptabort, 1))
188804203a83SThomas Cort goto abort;
188904203a83SThomas Cort oldintr = xsignal(SIGINT, abortpt);
189004203a83SThomas Cort if ((restart_point &&
189104203a83SThomas Cort (command("REST " LLF, (LLT) restart_point) != CONTINUE))
189204203a83SThomas Cort || (command("%s %s", cmd, remote) != PRELIM)) {
189304203a83SThomas Cort (void)xsignal(SIGINT, oldintr);
189404203a83SThomas Cort pswitch(1);
189504203a83SThomas Cort return;
189604203a83SThomas Cort }
189704203a83SThomas Cort sleep(2);
189804203a83SThomas Cort pswitch(1);
189904203a83SThomas Cort secndflag++;
190004203a83SThomas Cort if ((restart_point &&
190104203a83SThomas Cort (command("REST " LLF, (LLT) restart_point) != CONTINUE))
190204203a83SThomas Cort || (command("%s %s", cmd2, local) != PRELIM))
190304203a83SThomas Cort goto abort;
190404203a83SThomas Cort ptflag++;
190504203a83SThomas Cort (void)getreply(0);
190604203a83SThomas Cort pswitch(0);
190704203a83SThomas Cort (void)getreply(0);
190804203a83SThomas Cort (void)xsignal(SIGINT, oldintr);
190904203a83SThomas Cort pswitch(1);
191004203a83SThomas Cort ptflag = 0;
191104203a83SThomas Cort fprintf(ttyout, "local: %s remote: %s\n", local, remote);
191204203a83SThomas Cort return;
191304203a83SThomas Cort abort:
191404203a83SThomas Cort if (sigsetjmp(xferabort, 1)) {
191504203a83SThomas Cort (void)xsignal(SIGINT, oldintr);
191604203a83SThomas Cort return;
191704203a83SThomas Cort }
191804203a83SThomas Cort (void)xsignal(SIGINT, abort_squared);
191904203a83SThomas Cort ptflag = 0;
192004203a83SThomas Cort if (strcmp(cmd, "RETR") && !proxy)
192104203a83SThomas Cort pswitch(1);
192204203a83SThomas Cort else if (!strcmp(cmd, "RETR") && proxy)
192304203a83SThomas Cort pswitch(0);
192404203a83SThomas Cort if (!cpend && !secndflag) { /* only here if cmd = "STOR" (proxy=1) */
192504203a83SThomas Cort if (command("%s %s", cmd2, local) != PRELIM) {
192604203a83SThomas Cort pswitch(0);
192704203a83SThomas Cort if (cpend)
192804203a83SThomas Cort abort_remote(NULL);
192904203a83SThomas Cort }
193004203a83SThomas Cort pswitch(1);
193104203a83SThomas Cort if (ptabflg)
193204203a83SThomas Cort code = -1;
193304203a83SThomas Cort (void)xsignal(SIGINT, oldintr);
193404203a83SThomas Cort return;
193504203a83SThomas Cort }
193604203a83SThomas Cort if (cpend)
193704203a83SThomas Cort abort_remote(NULL);
193804203a83SThomas Cort pswitch(!proxy);
193904203a83SThomas Cort if (!cpend && !secndflag) { /* only if cmd = "RETR" (proxy=1) */
194004203a83SThomas Cort if (command("%s %s", cmd2, local) != PRELIM) {
194104203a83SThomas Cort pswitch(0);
194204203a83SThomas Cort if (cpend)
194304203a83SThomas Cort abort_remote(NULL);
194404203a83SThomas Cort pswitch(1);
194504203a83SThomas Cort if (ptabflg)
194604203a83SThomas Cort code = -1;
194704203a83SThomas Cort (void)xsignal(SIGINT, oldintr);
194804203a83SThomas Cort return;
194904203a83SThomas Cort }
195004203a83SThomas Cort }
195104203a83SThomas Cort if (cpend)
195204203a83SThomas Cort abort_remote(NULL);
195304203a83SThomas Cort pswitch(!proxy);
195404203a83SThomas Cort if (cpend) {
195504203a83SThomas Cort if ((nfnd = empty(cin, NULL, 10)) <= 0) {
195604203a83SThomas Cort if (nfnd < 0)
195704203a83SThomas Cort warn("Error aborting proxy command");
195804203a83SThomas Cort if (ptabflg)
195904203a83SThomas Cort code = -1;
196004203a83SThomas Cort lostpeer(0);
196104203a83SThomas Cort }
196204203a83SThomas Cort (void)getreply(0);
196304203a83SThomas Cort (void)getreply(0);
196404203a83SThomas Cort }
196504203a83SThomas Cort if (proxy)
196604203a83SThomas Cort pswitch(0);
196704203a83SThomas Cort pswitch(1);
196804203a83SThomas Cort if (ptabflg)
196904203a83SThomas Cort code = -1;
197004203a83SThomas Cort (void)xsignal(SIGINT, oldintr);
197104203a83SThomas Cort }
197204203a83SThomas Cort
197304203a83SThomas Cort void
reset(int argc,char * argv[])197404203a83SThomas Cort reset(int argc, char *argv[])
197504203a83SThomas Cort {
197604203a83SThomas Cort int nfnd = 1;
197704203a83SThomas Cort
197804203a83SThomas Cort if (argc == 0 && argv != NULL) {
197904203a83SThomas Cort UPRINTF("usage: %s\n", argv[0]);
198004203a83SThomas Cort code = -1;
198104203a83SThomas Cort return;
198204203a83SThomas Cort }
198304203a83SThomas Cort while (nfnd > 0) {
198404203a83SThomas Cort if ((nfnd = empty(cin, NULL, 0)) < 0) {
198504203a83SThomas Cort warn("Error resetting connection");
198604203a83SThomas Cort code = -1;
198704203a83SThomas Cort lostpeer(0);
198804203a83SThomas Cort } else if (nfnd)
198904203a83SThomas Cort (void)getreply(0);
199004203a83SThomas Cort }
199104203a83SThomas Cort }
199204203a83SThomas Cort
199304203a83SThomas Cort char *
gunique(const char * local)199404203a83SThomas Cort gunique(const char *local)
199504203a83SThomas Cort {
199604203a83SThomas Cort static char new[MAXPATHLEN];
199704203a83SThomas Cort char *cp = strrchr(local, '/');
199804203a83SThomas Cort int d, count=0, len;
199904203a83SThomas Cort char ext = '1';
200004203a83SThomas Cort
200104203a83SThomas Cort if (cp)
200204203a83SThomas Cort *cp = '\0';
200304203a83SThomas Cort d = access(cp == local ? "/" : cp ? local : ".", W_OK);
200404203a83SThomas Cort if (cp)
200504203a83SThomas Cort *cp = '/';
200604203a83SThomas Cort if (d < 0) {
200704203a83SThomas Cort warn("Can't access `%s'", local);
200804203a83SThomas Cort return (NULL);
200904203a83SThomas Cort }
201004203a83SThomas Cort len = strlcpy(new, local, sizeof(new));
201104203a83SThomas Cort cp = &new[len];
201204203a83SThomas Cort *cp++ = '.';
201304203a83SThomas Cort while (!d) {
201404203a83SThomas Cort if (++count == 100) {
201504203a83SThomas Cort fputs("runique: can't find unique file name.\n",
201604203a83SThomas Cort ttyout);
201704203a83SThomas Cort return (NULL);
201804203a83SThomas Cort }
201904203a83SThomas Cort *cp++ = ext;
202004203a83SThomas Cort *cp = '\0';
202104203a83SThomas Cort if (ext == '9')
202204203a83SThomas Cort ext = '0';
202304203a83SThomas Cort else
202404203a83SThomas Cort ext++;
202504203a83SThomas Cort if ((d = access(new, F_OK)) < 0)
202604203a83SThomas Cort break;
202704203a83SThomas Cort if (ext != '0')
202804203a83SThomas Cort cp--;
202904203a83SThomas Cort else if (*(cp - 2) == '.')
203004203a83SThomas Cort *(cp - 1) = '1';
203104203a83SThomas Cort else {
203204203a83SThomas Cort *(cp - 2) = *(cp - 2) + 1;
203304203a83SThomas Cort cp--;
203404203a83SThomas Cort }
203504203a83SThomas Cort }
203604203a83SThomas Cort return (new);
203704203a83SThomas Cort }
203804203a83SThomas Cort
203904203a83SThomas Cort /*
204004203a83SThomas Cort * abort_squared --
204104203a83SThomas Cort * aborts abort_remote(). lostpeer() is called because if the user is
204204203a83SThomas Cort * too impatient to wait or there's another problem then ftp really
204304203a83SThomas Cort * needs to get back to a known state.
204404203a83SThomas Cort */
204504203a83SThomas Cort static void
abort_squared(int dummy)204604203a83SThomas Cort abort_squared(int dummy)
204704203a83SThomas Cort {
204804203a83SThomas Cort char msgbuf[100];
204904203a83SThomas Cort size_t len;
205004203a83SThomas Cort
205104203a83SThomas Cort sigint_raised = 1;
205204203a83SThomas Cort alarmtimer(0);
205304203a83SThomas Cort len = strlcpy(msgbuf, "\nremote abort aborted; closing connection.\n",
205404203a83SThomas Cort sizeof(msgbuf));
205504203a83SThomas Cort write(fileno(ttyout), msgbuf, len);
205604203a83SThomas Cort lostpeer(0);
205704203a83SThomas Cort siglongjmp(xferabort, 1);
205804203a83SThomas Cort }
205904203a83SThomas Cort
206004203a83SThomas Cort void
abort_remote(FILE * din)206104203a83SThomas Cort abort_remote(FILE *din)
206204203a83SThomas Cort {
206304203a83SThomas Cort char buf[BUFSIZ];
206404203a83SThomas Cort int nfnd;
206504203a83SThomas Cort
206604203a83SThomas Cort if (cout == NULL) {
206704203a83SThomas Cort warnx("Lost control connection for abort");
206804203a83SThomas Cort if (ptabflg)
206904203a83SThomas Cort code = -1;
207004203a83SThomas Cort lostpeer(0);
207104203a83SThomas Cort return;
207204203a83SThomas Cort }
207304203a83SThomas Cort /*
207404203a83SThomas Cort * send IAC in urgent mode instead of DM because 4.3BSD places oob mark
207504203a83SThomas Cort * after urgent byte rather than before as is protocol now
207604203a83SThomas Cort */
207704203a83SThomas Cort buf[0] = IAC;
207804203a83SThomas Cort buf[1] = IP;
207904203a83SThomas Cort buf[2] = IAC;
208004203a83SThomas Cort if (send(fileno(cout), buf, 3, MSG_OOB) != 3)
208104203a83SThomas Cort warn("Can't send abort message");
208204203a83SThomas Cort fprintf(cout, "%cABOR\r\n", DM);
208304203a83SThomas Cort (void)fflush(cout);
208404203a83SThomas Cort if ((nfnd = empty(cin, din, 10)) <= 0) {
208504203a83SThomas Cort if (nfnd < 0)
208604203a83SThomas Cort warn("Can't send abort message");
208704203a83SThomas Cort if (ptabflg)
208804203a83SThomas Cort code = -1;
208904203a83SThomas Cort lostpeer(0);
209004203a83SThomas Cort }
209104203a83SThomas Cort if (din && (nfnd & 2)) {
209204203a83SThomas Cort while (read(fileno(din), buf, BUFSIZ) > 0)
209304203a83SThomas Cort continue;
209404203a83SThomas Cort }
209504203a83SThomas Cort if (getreply(0) == ERROR && code == 552) {
209604203a83SThomas Cort /* 552 needed for nic style abort */
209704203a83SThomas Cort (void)getreply(0);
209804203a83SThomas Cort }
209904203a83SThomas Cort (void)getreply(0);
210004203a83SThomas Cort }
210104203a83SThomas Cort
210204203a83SThomas Cort /*
210304203a83SThomas Cort * Ensure that ai->ai_addr is NOT an IPv4 mapped address.
210404203a83SThomas Cort * IPv4 mapped address complicates too many things in FTP
210504203a83SThomas Cort * protocol handling, as FTP protocol is defined differently
210604203a83SThomas Cort * between IPv4 and IPv6.
210704203a83SThomas Cort *
210804203a83SThomas Cort * This may not be the best way to handle this situation,
210904203a83SThomas Cort * since the semantics of IPv4 mapped address is defined in
211004203a83SThomas Cort * the kernel. There are configurations where we should use
211104203a83SThomas Cort * IPv4 mapped address as native IPv6 address, not as
211204203a83SThomas Cort * "an IPv6 address that embeds IPv4 address" (namely, SIIT).
211304203a83SThomas Cort *
211404203a83SThomas Cort * More complete solution would be to have an additional
211504203a83SThomas Cort * getsockopt to grab "real" peername/sockname. "real"
211604203a83SThomas Cort * peername/sockname will be AF_INET if IPv4 mapped address
211704203a83SThomas Cort * is used to embed IPv4 address, and will be AF_INET6 if
211804203a83SThomas Cort * we use it as native. What a mess!
211904203a83SThomas Cort */
212004203a83SThomas Cort void
ai_unmapped(struct addrinfo * ai)212104203a83SThomas Cort ai_unmapped(struct addrinfo *ai)
212204203a83SThomas Cort {
212304203a83SThomas Cort #ifdef INET6
212404203a83SThomas Cort struct sockaddr_in6 *sin6;
212504203a83SThomas Cort struct sockaddr_in sin;
212604203a83SThomas Cort socklen_t len;
212704203a83SThomas Cort
212804203a83SThomas Cort if (ai->ai_family != AF_INET6)
212904203a83SThomas Cort return;
213004203a83SThomas Cort if (ai->ai_addrlen != sizeof(struct sockaddr_in6) ||
213104203a83SThomas Cort sizeof(sin) > ai->ai_addrlen)
213204203a83SThomas Cort return;
213304203a83SThomas Cort sin6 = (struct sockaddr_in6 *)ai->ai_addr;
213404203a83SThomas Cort if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
213504203a83SThomas Cort return;
213604203a83SThomas Cort
213704203a83SThomas Cort memset(&sin, 0, sizeof(sin));
213804203a83SThomas Cort sin.sin_family = AF_INET;
213904203a83SThomas Cort len = sizeof(struct sockaddr_in);
214004203a83SThomas Cort memcpy(&sin.sin_addr, &sin6->sin6_addr.s6_addr[12],
214104203a83SThomas Cort sizeof(sin.sin_addr));
214204203a83SThomas Cort sin.sin_port = sin6->sin6_port;
214304203a83SThomas Cort
214404203a83SThomas Cort ai->ai_family = AF_INET;
214504203a83SThomas Cort #if defined(HAVE_STRUCT_SOCKADDR_IN_SIN_LEN)
214604203a83SThomas Cort sin.sin_len = len;
214704203a83SThomas Cort #endif
214804203a83SThomas Cort memcpy(ai->ai_addr, &sin, len);
214904203a83SThomas Cort ai->ai_addrlen = len;
215004203a83SThomas Cort #endif
215104203a83SThomas Cort }
215204203a83SThomas Cort
215304203a83SThomas Cort #ifdef NO_USAGE
215404203a83SThomas Cort void
xusage(void)215504203a83SThomas Cort xusage(void)
215604203a83SThomas Cort {
215704203a83SThomas Cort fputs("Usage error\n", ttyout);
215804203a83SThomas Cort }
215904203a83SThomas Cort #endif
2160