1 /* $NetBSD: ftp.c,v 1.19 2013/05/05 11:17:31 lukem Exp $ */
2 /* from NetBSD: ftp.c,v 1.164 2012/07/04 06:09:37 is Exp */
5 * Copyright (c) 1996-2009 The NetBSD Foundation, Inc.
8 * This code is derived from software contributed to The NetBSD Foundation
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
34 * Copyright (c) 1985, 1989, 1993, 1994
35 * The Regents of the University of California. All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * Copyright (C) 1997 and 1998 WIDE Project.
64 * All rights reserved.
66 * Redistribution and use in source and binary forms, with or without
67 * modification, are permitted provided that the following conditions
69 * 1. Redistributions of source code must retain the above copyright
70 * notice, this list of conditions and the following disclaimer.
71 * 2. Redistributions in binary form must reproduce the above copyright
72 * notice, this list of conditions and the following disclaimer in the
73 * documentation and/or other materials provided with the distribution.
74 * 3. Neither the name of the project nor the names of its contributors
75 * may be used to endorse or promote products derived from this software
76 * without specific prior written permission.
78 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
79 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
80 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
81 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
82 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
83 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
84 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
85 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
86 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
87 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
92 #include <arpa/telnet.h>
96 #include <sys/cdefs.h>
99 static char sccsid
[] = "@(#)ftp.c 8.6 (Berkeley) 10/27/94";
101 __RCSID(" NetBSD: ftp.c,v 1.164 2012/07/04 06:09:37 is Exp ");
103 #endif /* not lint */
105 #include <sys/types.h>
106 #include <sys/stat.h>
107 #include <sys/socket.h>
108 #include <sys/time.h>
110 #include <netinet/in.h>
111 #include <netinet/in_systm.h>
112 #include <netinet/ip.h>
113 #include <arpa/inet.h>
114 #include <arpa/ftp.h>
115 #include <arpa/telnet.h>
134 volatile sig_atomic_t abrtflag
;
135 volatile sig_atomic_t timeoutflag
;
140 char pasv
[BUFSIZ
]; /* passive port for proxy data connection */
142 static int empty(FILE *, FILE *, int);
143 __dead
static void abort_squared(int);
147 struct sockaddr_in su_sin
;
149 struct sockaddr_in6 su_sin6
;
152 #if !defined(HAVE_STRUCT_SOCKADDR_IN_SIN_LEN)
157 #if !defined(HAVE_STRUCT_SOCKADDR_IN_SIN_LEN)
158 # define su_len si_len
160 # define su_len si_su.su_sin.sin_len
162 #define su_family si_su.su_sin.sin_family
163 #define su_port si_su.su_sin.sin_port
165 struct sockinet myctladdr
, hisctladdr
, data_addr
;
168 hookup(const char *host
, const char *port
)
171 struct addrinfo hints
, *res
, *res0
;
172 static char hostnamebuf
[MAXHOSTNAMELEN
];
176 memset((char *)&hisctladdr
, 0, sizeof (hisctladdr
));
177 memset((char *)&myctladdr
, 0, sizeof (myctladdr
));
178 memset(&hints
, 0, sizeof(hints
));
179 hints
.ai_flags
= AI_CANONNAME
;
180 hints
.ai_family
= family
;
181 hints
.ai_socktype
= SOCK_STREAM
;
182 hints
.ai_protocol
= 0;
183 error
= getaddrinfo(host
, port
, &hints
, &res0
);
185 warnx("Can't lookup `%s:%s': %s", host
, port
,
186 (error
== EAI_SYSTEM
) ? strerror(errno
)
187 : gai_strerror(error
));
192 if (res0
->ai_canonname
)
193 (void)strlcpy(hostnamebuf
, res0
->ai_canonname
,
194 sizeof(hostnamebuf
));
196 (void)strlcpy(hostnamebuf
, host
, sizeof(hostnamebuf
));
197 hostname
= hostnamebuf
;
199 for (res
= res0
; res
; res
= res
->ai_next
) {
200 char hname
[NI_MAXHOST
], sname
[NI_MAXSERV
];
203 if (getnameinfo(res
->ai_addr
, res
->ai_addrlen
,
204 hname
, sizeof(hname
), sname
, sizeof(sname
),
205 NI_NUMERICHOST
| NI_NUMERICSERV
) != 0) {
206 strlcpy(hname
, "?", sizeof(hname
));
207 strlcpy(sname
, "?", sizeof(sname
));
209 if (verbose
&& res0
->ai_next
) {
210 /* if we have multiple possibilities */
211 fprintf(ttyout
, "Trying %s:%s ...\n", hname
, sname
);
213 s
= socket(res
->ai_family
, SOCK_STREAM
, res
->ai_protocol
);
215 warn("Can't create socket for connection to `%s:%s'",
219 if (ftp_connect(s
, res
->ai_addr
, res
->ai_addrlen
,
220 verbose
|| !res
->ai_next
) < 0) {
226 /* finally we got one */
230 warnx("Can't connect to `%s:%s'", host
, port
);
235 memcpy(&hisctladdr
.si_su
, res
->ai_addr
, res
->ai_addrlen
);
236 hisctladdr
.su_len
= res
->ai_addrlen
;
240 len
= hisctladdr
.su_len
;
241 if (getsockname(s
, (struct sockaddr
*)&myctladdr
.si_su
, &len
) == -1) {
242 warn("Can't determine my address of connection to `%s:%s'",
247 myctladdr
.su_len
= len
;
249 #ifdef IPTOS_LOWDELAY
250 if (hisctladdr
.su_family
== AF_INET
) {
251 int tos
= IPTOS_LOWDELAY
;
252 if (setsockopt(s
, IPPROTO_IP
, IP_TOS
,
253 (void *)&tos
, sizeof(tos
)) == -1) {
254 DWARN("setsockopt %s (ignored)",
259 cin
= fdopen(s
, "r");
260 cout
= fdopen(s
, "w");
261 if (cin
== NULL
|| cout
== NULL
) {
262 warnx("Can't fdopen socket");
271 fprintf(ttyout
, "Connected to %s.\n", hostname
);
272 if (getreply(0) > 2) { /* read startup message from server */
281 if (setsockopt(s
, SOL_SOCKET
, SO_OOBINLINE
,
282 (void *)&on
, sizeof(on
)) == -1) {
283 DWARN("setsockopt %s (ignored)", "SO_OOBINLINE");
293 cmdabort(int notused
)
300 write(fileno(ttyout
), "\n", 1);
303 siglongjmp(ptabort
, 1);
308 cmdtimeout(int notused
)
314 write(fileno(ttyout
), "\n", 1);
317 siglongjmp(ptabort
, 1);
323 command(const char *fmt
, ...)
331 fputs("---> ", ttyout
);
333 if (strncmp("PASS ", fmt
, 5) == 0)
334 fputs("PASS XXXX", ttyout
);
335 else if (strncmp("ACCT ", fmt
, 5) == 0)
336 fputs("ACCT XXXX", ttyout
);
338 vfprintf(ttyout
, fmt
, ap
);
344 warnx("No control connection for command");
351 oldsigint
= xsignal(SIGINT
, cmdabort
);
354 vfprintf(cout
, fmt
, ap
);
359 r
= getreply(!strcmp(fmt
, "QUIT"));
360 if (abrtflag
&& oldsigint
!= SIG_IGN
)
361 (*oldsigint
)(SIGINT
);
362 (void)xsignal(SIGINT
, oldsigint
);
366 static const char *m421
[] = {
367 "remote server timed out. Connection closed",
368 "user interrupt. Connection closed",
369 "remote server has closed connection",
373 getreply(int expecteof
)
375 char current_line
[BUFSIZ
]; /* last line of previous reply */
378 int originalcode
= 0, continuation
= 0;
379 sigfunc oldsigint
, oldsigalrm
;
381 char *cp
, *pt
= pasv
;
386 oldsigint
= xsignal(SIGINT
, cmdabort
);
387 oldsigalrm
= xsignal(SIGALRM
, cmdtimeout
);
389 for (lineno
= 0 ;; lineno
++) {
392 while (alarmtimer(quit_time
? quit_time
: 60),
393 ((c
= getc(cin
)) != '\n')) {
394 if (c
== IAC
) { /* handle telnet commands */
395 switch (c
= getc(cin
)) {
399 fprintf(cout
, "%c%c%c", IAC
, DONT
, c
);
405 fprintf(cout
, "%c%c%c", IAC
, WONT
, c
);
416 * these will get trashed by pswitch()
419 int reply_timeoutflag
= timeoutflag
;
420 int reply_abrtflag
= abrtflag
;
423 if (expecteof
&& feof(cin
)) {
424 (void)xsignal(SIGINT
, oldsigint
);
425 (void)xsignal(SIGALRM
, oldsigalrm
);
433 if (reply_timeoutflag
)
435 else if (reply_abrtflag
)
439 (void)fprintf(ttyout
,
440 "421 Service not available, %s.\n", m421
[midx
]);
441 (void)fflush(ttyout
);
444 (void)xsignal(SIGINT
, oldsigint
);
445 (void)xsignal(SIGALRM
, oldsigalrm
);
448 if (c
!= '\r' && (verbose
> 0 ||
449 ((verbose
> -1 && n
== '5' && dig
> 4) &&
450 (((!n
&& c
< '5') || (n
&& n
< '5'))
451 || !retry_connect
)))) {
453 (dig
== 1 || (dig
== 5 && verbose
== 0)))
454 fprintf(ttyout
, "%s:", hostname
);
455 (void)putc(c
, ttyout
);
457 if (dig
< 4 && isdigit(c
))
458 code
= code
* 10 + (c
- '0');
459 if (!pflag
&& (code
== 227 || code
== 228))
461 else if (!pflag
&& code
== 229)
463 if (dig
> 4 && pflag
== 1 && isdigit(c
))
466 if (c
!= '\r' && c
!= ')') {
467 if (pt
< &pasv
[sizeof(pasv
) - 1])
474 if (pflag
== 100 && c
== '(')
476 if (dig
== 4 && c
== '-') {
483 if (cp
< ¤t_line
[sizeof(current_line
) - 1])
486 if (verbose
> 0 || ((verbose
> -1 && n
== '5') &&
487 (n
< '5' || !retry_connect
))) {
488 (void)putc(c
, ttyout
);
489 (void)fflush(ttyout
);
495 (void)strlcpy(reply_string
, current_line
,
496 sizeof(reply_string
));
497 if (lineno
> 0 && code
== 0 && reply_callback
!= NULL
)
498 (*reply_callback
)(current_line
);
499 if (continuation
&& code
!= originalcode
) {
500 if (originalcode
== 0)
507 (void)xsignal(SIGINT
, oldsigint
);
508 (void)xsignal(SIGALRM
, oldsigalrm
);
509 if (code
== 421 || originalcode
== 421)
511 if (abrtflag
&& oldsigint
!= cmdabort
&& oldsigint
!= SIG_IGN
)
512 (*oldsigint
)(SIGINT
);
513 if (timeoutflag
&& oldsigalrm
!= cmdtimeout
&&
514 oldsigalrm
!= SIG_IGN
)
515 (*oldsigalrm
)(SIGINT
);
521 empty(FILE *ecin
, FILE *din
, int sec
)
524 struct pollfd pfd
[2];
528 pfd
[nfd
].fd
= fileno(ecin
);
529 pfd
[nfd
++].events
= POLLIN
;
533 pfd
[nfd
].fd
= fileno(din
);
534 pfd
[nfd
++].events
= POLLIN
;
537 if ((nr
= ftp_poll(pfd
, nfd
, sec
* 1000)) <= 0)
543 nr
|= (pfd
[nfd
++].revents
& POLLIN
) ? 1 : 0;
545 nr
|= (pfd
[nfd
++].revents
& POLLIN
) ? 2 : 0;
549 sigjmp_buf xferabort
;
552 abortxfer(int notused
)
561 switch (direction
[0]) {
563 strlcpy(msgbuf
, "\nreceive", sizeof(msgbuf
));
566 strlcpy(msgbuf
, "\nsend", sizeof(msgbuf
));
569 errx(1, "abortxfer: unknown direction `%s'", direction
);
571 len
= strlcat(msgbuf
, " aborted. Waiting for remote to finish abort.\n",
573 write(fileno(ttyout
), msgbuf
, len
);
574 siglongjmp(xferabort
, 1);
578 * Read data from infd & write to outfd, using buf/bufsize as the temporary
579 * buffer, dealing with short writes.
580 * If rate_limit != 0, rate-limit the transfer.
581 * If hash_interval != 0, fputc('c', ttyout) every hash_interval bytes.
582 * Updates global variables: bytes.
583 * Returns 0 if ok, 1 if there was a read error, 2 if there was a write error.
584 * In the case of error, errno contains the appropriate error code.
587 copy_bytes(int infd
, int outfd
, char *buf
, size_t bufsize
,
588 int rate_limit
, int hash_interval
)
590 volatile off_t hashc
;
593 struct timeval tvthen
, tvnow
, tvdiff
;
594 off_t bufrem
, bufchunk
;
597 hashc
= hash_interval
;
599 bufchunk
= rate_limit
;
605 (void)gettimeofday(&tvthen
, NULL
);
609 /* copy bufchunk at a time */
612 inc
= read(infd
, buf
, MIN((off_t
)bufsize
, bufrem
));
619 outc
= write(outfd
, bufp
, inc
);
626 while (bytes
>= hashc
) {
627 (void)putc('#', ttyout
);
628 hashc
+= hash_interval
;
630 (void)fflush(ttyout
);
633 if (rate_limit
) { /* rate limited; wait if necessary */
635 (void)gettimeofday(&tvnow
, NULL
);
636 timersub(&tvnow
, &tvthen
, &tvdiff
);
637 if (tvdiff
.tv_sec
> 0)
639 usleep(1000000 - tvdiff
.tv_usec
);
646 if (hash_interval
&& bytes
> 0) {
647 if (bytes
< hash_interval
)
648 (void)putc('#', ttyout
);
649 (void)putc('\n', ttyout
);
650 (void)fflush(ttyout
);
662 sendrequest(const char *cmd
, const char *local
, const char *remote
,
669 int (*volatile closefunc
)(FILE *);
670 sigfunc
volatile oldintr
;
671 sigfunc
volatile oldintp
;
672 off_t
volatile hashbytes
;
675 static size_t bufsize
;
684 oprogress
= progress
;
685 if (verbose
&& printnames
) {
687 fprintf(ttyout
, "local: %s ", local
);
689 fprintf(ttyout
, "remote: %s\n", remote
);
692 proxtrans(cmd
, local
, remote
);
701 if (sigsetjmp(xferabort
, 1)) {
707 (void)xsignal(SIGQUIT
, psummary
);
708 oldintr
= xsignal(SIGINT
, abortxfer
);
709 if (strcmp(local
, "-") == 0) {
712 } else if (*local
== '|') {
713 oldintp
= xsignal(SIGPIPE
, SIG_IGN
);
714 fin
= popen(local
+ 1, "r");
716 warn("Can't execute `%s'", local
+ 1);
723 fin
= fopen(local
, "r");
725 warn("Can't open `%s'", local
);
730 if (fstat(fileno(fin
), &st
) < 0 || !S_ISREG(st
.st_mode
)) {
731 fprintf(ttyout
, "%s: not a plain file.\n", local
);
735 filesize
= st
.st_size
;
741 if (sigsetjmp(xferabort
, 1))
745 (strcmp(cmd
, "STOR") == 0 || strcmp(cmd
, "APPE") == 0)) {
751 rc
= fseeko(fin
, restart_point
, SEEK_SET
);
755 rc
= lseek(fileno(fin
), restart_point
, SEEK_SET
);
759 warn("Can't seek to restart `%s'", local
);
762 if (command("REST " LLF
, (LLT
)restart_point
) != CONTINUE
)
767 if (command("%s %s", cmd
, remote
) != PRELIM
)
770 if (command("%s", cmd
) != PRELIM
)
774 dout
= dataconn(lmode
);
778 assert(sndbuf_size
> 0);
779 if ((size_t)sndbuf_size
> bufsize
) {
782 bufsize
= sndbuf_size
;
783 buf
= ftp_malloc(bufsize
);
787 oldintp
= xsignal(SIGPIPE
, SIG_IGN
);
788 hash_interval
= (hash
&& (!progress
|| filesize
< 0)) ? mark
: 0;
794 c
= copy_bytes(fileno(fin
), fileno(dout
), buf
, bufsize
,
795 rate_put
, hash_interval
);
797 warn("Reading `%s'", local
);
800 warn("Writing to network");
806 while ((c
= getc(fin
)) != EOF
) {
808 while (hash_interval
&& bytes
>= hashbytes
) {
809 (void)putc('#', ttyout
);
810 (void)fflush(ttyout
);
815 (void)putc('\r', dout
);
820 #if 0 /* this violates RFC 959 */
822 (void)putc('\0', dout
);
828 if (bytes
< hashbytes
)
829 (void)putc('#', ttyout
);
830 (void)putc('\n', ttyout
);
833 warn("Reading `%s'", local
);
836 warn("Writing to network");
843 if (closefunc
!= NULL
) {
855 (void)xsignal(SIGINT
, oldintr
);
876 (void)xsignal(SIGINT
, oldintr
);
878 (void)xsignal(SIGPIPE
, oldintp
);
883 if (closefunc
!= NULL
&& fin
!= NULL
)
887 progress
= oprogress
;
893 recvrequest(const char *cmd
, const char *volatile local
, const char *remote
,
894 const char *lmode
, int printnames
, int ignorespecial
)
898 int (*volatile closefunc
)(FILE *);
899 sigfunc
volatile oldintr
;
900 sigfunc
volatile oldintp
;
902 int volatile is_retr
;
903 int volatile tcrflag
;
904 int volatile bare_lfs
;
905 static size_t bufsize
;
907 off_t
volatile hashbytes
;
911 struct timeval tval
[2];
918 direction
= "received";
922 oprogress
= progress
;
923 opreserve
= preserve
;
924 is_retr
= (strcmp(cmd
, "RETR") == 0);
925 if (is_retr
&& verbose
&& printnames
) {
926 if (ignorespecial
|| *local
!= '-')
927 fprintf(ttyout
, "local: %s ", local
);
929 fprintf(ttyout
, "remote: %s\n", remote
);
931 if (proxy
&& is_retr
) {
932 proxtrans(cmd
, local
, remote
);
938 tcrflag
= !crflag
&& is_retr
;
939 if (sigsetjmp(xferabort
, 1)) {
945 (void)xsignal(SIGQUIT
, psummary
);
946 oldintr
= xsignal(SIGINT
, abortxfer
);
947 if (ignorespecial
|| (strcmp(local
, "-") && *local
!= '|')) {
948 if (access(local
, W_OK
) < 0) {
949 char *dir
= strrchr(local
, '/');
951 if (errno
!= ENOENT
&& errno
!= EACCES
) {
952 warn("Can't access `%s'", local
);
958 d
= access(dir
== local
? "/" :
959 dir
? local
: ".", W_OK
);
963 warn("Can't access `%s'", local
);
967 if (!runique
&& errno
== EACCES
&&
968 chmod(local
, (S_IRUSR
|S_IWUSR
)) < 0) {
969 warn("Can't chmod `%s'", local
);
973 if (runique
&& errno
== EACCES
&&
974 (local
= gunique(local
)) == NULL
) {
979 else if (runique
&& (local
= gunique(local
)) == NULL
) {
985 if (curtype
!= TYPE_A
)
986 changetype(TYPE_A
, 0);
990 filesize
= remotesize(remote
, 0);
991 if (code
== 421 || code
== -1)
998 if (sigsetjmp(xferabort
, 1))
1000 if (is_retr
&& restart_point
&&
1001 command("REST " LLF
, (LLT
) restart_point
) != CONTINUE
)
1003 if (! EMPTYSTRING(remote
)) {
1004 if (command("%s %s", cmd
, remote
) != PRELIM
)
1007 if (command("%s", cmd
) != PRELIM
)
1010 din
= dataconn("r");
1013 if (!ignorespecial
&& strcmp(local
, "-") == 0) {
1017 } else if (!ignorespecial
&& *local
== '|') {
1018 oldintp
= xsignal(SIGPIPE
, SIG_IGN
);
1019 fout
= popen(local
+ 1, "w");
1021 warn("Can't execute `%s'", local
+1);
1028 fout
= fopen(local
, lmode
);
1030 warn("Can't open `%s'", local
);
1036 if (fstat(fileno(fout
), &st
) != -1 && !S_ISREG(st
.st_mode
)) {
1040 assert(rcvbuf_size
> 0);
1041 if ((size_t)rcvbuf_size
> bufsize
) {
1044 bufsize
= rcvbuf_size
;
1045 buf
= ftp_malloc(bufsize
);
1049 hash_interval
= (hash
&& (!progress
|| filesize
< 0)) ? mark
: 0;
1055 if (is_retr
&& restart_point
&&
1056 lseek(fileno(fout
), restart_point
, SEEK_SET
) < 0) {
1057 warn("Can't seek to restart `%s'", local
);
1060 c
= copy_bytes(fileno(din
), fileno(fout
), buf
, bufsize
,
1061 rate_get
, hash_interval
);
1064 warn("Reading from network");
1066 } else if (c
== 2) {
1067 warn("Writing `%s'", local
);
1072 if (is_retr
&& restart_point
) {
1076 if (fseeko(fout
, (off_t
)0, SEEK_SET
) < 0)
1078 for (i
= 0; i
++ < restart_point
;) {
1079 if ((ch
= getc(fout
)) == EOF
)
1084 if (fseeko(fout
, (off_t
)0, SEEK_CUR
) < 0) {
1086 warn("Can't seek to restart `%s'", local
);
1090 while ((c
= getc(din
)) != EOF
) {
1094 while (hash_interval
&& bytes
>= hashbytes
) {
1095 (void)putc('#', ttyout
);
1096 (void)fflush(ttyout
);
1100 if ((c
= getc(din
)) != '\n' || tcrflag
) {
1103 (void)putc('\r', fout
);
1112 (void)putc(c
, fout
);
1117 if (hash_interval
) {
1118 if (bytes
< hashbytes
)
1119 (void)putc('#', ttyout
);
1120 (void)putc('\n', ttyout
);
1124 warn("Reading from network");
1128 warn("Writing `%s'", local
);
1133 if (closefunc
!= NULL
) {
1142 "WARNING! %d bare linefeeds received in ASCII mode.\n",
1144 fputs("File may not have transferred correctly.\n", ttyout
);
1146 if (bytes
>= 0 && is_retr
) {
1149 if (preserve
&& (closefunc
== fclose
)) {
1150 mtime
= remotemodtime(remote
, 0);
1152 (void)gettimeofday(&tval
[0], NULL
);
1153 tval
[1].tv_sec
= mtime
;
1154 tval
[1].tv_usec
= 0;
1155 if (utimes(local
, tval
) == -1) {
1157 "Can't change modification time on %s to %s",
1159 rfc2822time(localtime(&mtime
)));
1168 * abort using RFC 959 recommended IP,SYNC sequence
1170 if (! sigsetjmp(xferabort
, 1)) {
1171 /* this is the first call */
1172 (void)xsignal(SIGINT
, abort_squared
);
1185 (void)xsignal(SIGINT
, oldintr
);
1187 (void)xsignal(SIGPIPE
, oldintp
);
1192 if (closefunc
!= NULL
&& fout
!= NULL
)
1196 progress
= oprogress
;
1197 preserve
= opreserve
;
1202 * Need to start a listen on the data channel before we send the command,
1203 * otherwise the server's connect may fail.
1209 int result
, tmpno
= 0;
1212 unsigned int addr
[16], port
[2];
1213 unsigned int af
, hal
, pal
;
1215 const char *pasvcmd
= NULL
;
1220 if (myctladdr
.su_family
== AF_INET6
&& ftp_debug
&&
1221 (IN6_IS_ADDR_LINKLOCAL(&myctladdr
.si_su
.su_sin6
.sin6_addr
) ||
1222 IN6_IS_ADDR_SITELOCAL(&myctladdr
.si_su
.su_sin6
.sin6_addr
))) {
1223 warnx("Use of scoped addresses can be troublesome");
1230 data_addr
= myctladdr
;
1231 data
= socket(data_addr
.su_family
, SOCK_STREAM
, 0);
1233 warn("Can't create socket for data connection");
1236 if ((options
& SO_DEBUG
) &&
1237 setsockopt(data
, SOL_SOCKET
, SO_DEBUG
,
1238 (void *)&on
, sizeof(on
)) == -1) {
1239 DWARN("setsockopt %s (ignored)", "SO_DEBUG");
1241 result
= COMPLETE
+ 1;
1242 switch (data_addr
.su_family
) {
1244 if (epsv4
&& !epsv4bad
) {
1249 result
= command("EPSV");
1252 (result
== COMPLETE
|| !connected
))
1253 fprintf(ttyout
, "%s\n", reply_string
);
1257 * this code is to be friendly with broken
1260 if (code
/ 10 == 22 && code
!= 229) {
1262 "wrong server: return code must be 229\n",
1264 result
= COMPLETE
+ 1;
1266 if (result
!= COMPLETE
) {
1268 DPRINTF("disabling epsv4 for this "
1272 if (result
!= COMPLETE
) {
1274 result
= command("PASV");
1281 if (epsv6
&& !epsv6bad
) {
1286 result
= command("EPSV");
1289 (result
== COMPLETE
|| !connected
))
1290 fprintf(ttyout
, "%s\n", reply_string
);
1294 * this code is to be friendly with
1297 if (code
/ 10 == 22 && code
!= 229) {
1299 "wrong server: return code must be 229\n",
1301 result
= COMPLETE
+ 1;
1303 if (result
!= COMPLETE
) {
1305 DPRINTF("disabling epsv6 for this "
1309 if (result
!= COMPLETE
) {
1311 result
= command("LPSV");
1318 result
= COMPLETE
+ 1;
1321 if (result
!= COMPLETE
) {
1322 if (activefallback
) {
1331 fputs("Passive mode refused.\n", ttyout
);
1335 #define pack2(var, off) \
1336 (((var[(off) + 0] & 0xff) << 8) | ((var[(off) + 1] & 0xff) << 0))
1337 #define pack4(var, off) \
1338 (((var[(off) + 0] & 0xff) << 24) | ((var[(off) + 1] & 0xff) << 16) | \
1339 ((var[(off) + 2] & 0xff) << 8) | ((var[(off) + 3] & 0xff) << 0))
1340 #define UC(b) (((int)b)&0xff)
1343 * What we've got at this point is a string of comma separated
1344 * one-byte unsigned integer values, separated by commas.
1346 if (strcmp(pasvcmd
, "PASV") == 0) {
1347 if (data_addr
.su_family
!= AF_INET
) {
1349 "Passive mode AF mismatch. Shouldn't happen!\n", ttyout
);
1353 if (code
/ 10 == 22 && code
!= 227) {
1354 fputs("wrong server: return code must be 227\n",
1359 error
= sscanf(pasv
, "%u,%u,%u,%u,%u,%u",
1360 &addr
[0], &addr
[1], &addr
[2], &addr
[3],
1361 &port
[0], &port
[1]);
1364 "Passive mode address scan failure. Shouldn't happen!\n", ttyout
);
1369 memset(&data_addr
, 0, sizeof(data_addr
));
1370 data_addr
.su_family
= AF_INET
;
1371 data_addr
.su_len
= sizeof(struct sockaddr_in
);
1372 data_addr
.si_su
.su_sin
.sin_addr
.s_addr
=
1373 htonl(pack4(addr
, 0));
1374 data_addr
.su_port
= htons(pack2(port
, 0));
1375 } else if (strcmp(pasvcmd
, "LPSV") == 0) {
1376 if (code
/ 10 == 22 && code
!= 228) {
1377 fputs("wrong server: return code must be 228\n",
1382 switch (data_addr
.su_family
) {
1384 error
= sscanf(pasv
,
1385 "%u,%u,%u,%u,%u,%u,%u,%u,%u",
1387 &addr
[0], &addr
[1], &addr
[2], &addr
[3],
1388 &pal
, &port
[0], &port
[1]);
1391 "Passive mode address scan failure. Shouldn't happen!\n", ttyout
);
1395 if (af
!= 4 || hal
!= 4 || pal
!= 2) {
1397 "Passive mode AF mismatch. Shouldn't happen!\n", ttyout
);
1403 memset(&data_addr
, 0, sizeof(data_addr
));
1404 data_addr
.su_family
= AF_INET
;
1405 data_addr
.su_len
= sizeof(struct sockaddr_in
);
1406 data_addr
.si_su
.su_sin
.sin_addr
.s_addr
=
1407 htonl(pack4(addr
, 0));
1408 data_addr
.su_port
= htons(pack2(port
, 0));
1412 error
= sscanf(pasv
,
1413 "%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u",
1415 &addr
[0], &addr
[1], &addr
[2], &addr
[3],
1416 &addr
[4], &addr
[5], &addr
[6], &addr
[7],
1417 &addr
[8], &addr
[9], &addr
[10],
1418 &addr
[11], &addr
[12], &addr
[13],
1419 &addr
[14], &addr
[15],
1420 &pal
, &port
[0], &port
[1]);
1423 "Passive mode address scan failure. Shouldn't happen!\n", ttyout
);
1427 if (af
!= 6 || hal
!= 16 || pal
!= 2) {
1429 "Passive mode AF mismatch. Shouldn't happen!\n", ttyout
);
1435 memset(&data_addr
, 0, sizeof(data_addr
));
1436 data_addr
.su_family
= AF_INET6
;
1437 data_addr
.su_len
= sizeof(struct sockaddr_in6
);
1440 for (i
= 0; i
< sizeof(struct in6_addr
); i
++) {
1441 data_addr
.si_su
.su_sin6
.sin6_addr
.s6_addr
[i
] =
1445 data_addr
.su_port
= htons(pack2(port
, 0));
1451 } else if (strcmp(pasvcmd
, "EPSV") == 0) {
1455 if (code
/ 10 == 22 && code
!= 229) {
1456 fputs("wrong server: return code must be 229\n",
1461 if (sscanf(pasv
, "%c%c%c%d%c", &delim
[0],
1462 &delim
[1], &delim
[2], &port
[1],
1464 fputs("parse error!\n", ttyout
);
1468 if (delim
[0] != delim
[1] || delim
[0] != delim
[2]
1469 || delim
[0] != delim
[3]) {
1470 fputs("parse error!\n", ttyout
);
1474 data_addr
= hisctladdr
;
1475 data_addr
.su_port
= htons(port
[1]);
1479 if (ftp_connect(data
, (struct sockaddr
*)&data_addr
.si_su
,
1480 data_addr
.su_len
, 1) < 0) {
1481 if (activefallback
) {
1492 #ifdef IPTOS_THROUGHPUT
1493 if (data_addr
.su_family
== AF_INET
) {
1494 on
= IPTOS_THROUGHPUT
;
1495 if (setsockopt(data
, IPPROTO_IP
, IP_TOS
,
1496 (void *)&on
, sizeof(on
)) == -1) {
1497 DWARN("setsockopt %s (ignored)",
1498 "IPTOS_THROUGHPUT");
1506 data_addr
= myctladdr
;
1508 data_addr
.su_port
= 0; /* let system pick one */
1511 data
= socket(data_addr
.su_family
, SOCK_STREAM
, 0);
1513 warn("Can't create socket for data connection");
1519 if (setsockopt(data
, SOL_SOCKET
, SO_REUSEADDR
,
1520 (void *)&on
, sizeof(on
)) == -1) {
1521 warn("Can't set SO_REUSEADDR on data connection");
1524 if (bind(data
, (struct sockaddr
*)&data_addr
.si_su
,
1525 data_addr
.su_len
) < 0) {
1526 warn("Can't bind for data connection");
1529 if ((options
& SO_DEBUG
) &&
1530 setsockopt(data
, SOL_SOCKET
, SO_DEBUG
,
1531 (void *)&on
, sizeof(on
)) == -1) {
1532 DWARN("setsockopt %s (ignored)", "SO_DEBUG");
1534 len
= sizeof(data_addr
.si_su
);
1535 memset((char *)&data_addr
, 0, sizeof (data_addr
));
1536 if (getsockname(data
, (struct sockaddr
*)&data_addr
.si_su
, &len
) == -1) {
1537 warn("Can't determine my address of data connection");
1540 data_addr
.su_len
= len
;
1541 if (ftp_listen(data
, 1) < 0)
1542 warn("Can't listen to data connection");
1545 char hname
[NI_MAXHOST
], sname
[NI_MAXSERV
];
1546 struct sockinet tmp
;
1548 switch (data_addr
.su_family
) {
1550 if (!epsv4
|| epsv4bad
) {
1551 result
= COMPLETE
+ 1;
1557 if (!epsv6
|| epsv6bad
) {
1558 result
= COMPLETE
+ 1;
1562 af
= (data_addr
.su_family
== AF_INET
) ? 1 : 2;
1565 if (tmp
.su_family
== AF_INET6
)
1566 tmp
.si_su
.su_sin6
.sin6_scope_id
= 0;
1568 if (getnameinfo((struct sockaddr
*)&tmp
.si_su
,
1569 tmp
.su_len
, hname
, sizeof(hname
), sname
,
1570 sizeof(sname
), NI_NUMERICHOST
| NI_NUMERICSERV
)) {
1576 result
= command("EPRT |%u|%s|%s|", af
, hname
,
1580 (result
== COMPLETE
|| !connected
))
1581 fprintf(ttyout
, "%s\n", reply_string
);
1584 if (result
!= COMPLETE
) {
1586 DPRINTF("disabling epsv4 for this "
1592 result
= COMPLETE
+ 1;
1595 if (result
== COMPLETE
)
1598 switch (data_addr
.su_family
) {
1600 a
= (char *)&data_addr
.si_su
.su_sin
.sin_addr
;
1601 p
= (char *)&data_addr
.su_port
;
1602 result
= command("PORT %d,%d,%d,%d,%d,%d",
1603 UC(a
[0]), UC(a
[1]), UC(a
[2]), UC(a
[3]),
1604 UC(p
[0]), UC(p
[1]));
1608 uint8_t ua
[sizeof(data_addr
.si_su
.su_sin6
.sin6_addr
)];
1609 uint8_t up
[sizeof(data_addr
.su_port
)];
1611 memcpy(ua
, &data_addr
.si_su
.su_sin6
.sin6_addr
,
1613 memcpy(up
, &data_addr
.su_port
, sizeof(up
));
1616 "LPRT %d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
1618 ua
[0], ua
[1], ua
[2], ua
[3],
1619 ua
[4], ua
[5], ua
[6], ua
[7],
1620 ua
[8], ua
[9], ua
[10], ua
[11],
1621 ua
[12], ua
[13], ua
[14], ua
[15],
1628 result
= COMPLETE
+ 1; /* xxx */
1634 if (result
== ERROR
&& sendport
== -1) {
1639 return (result
!= COMPLETE
);
1643 #ifdef IPTOS_THROUGHPUT
1644 if (data_addr
.su_family
== AF_INET
) {
1645 on
= IPTOS_THROUGHPUT
;
1646 if (setsockopt(data
, IPPROTO_IP
, IP_TOS
,
1647 (void *)&on
, sizeof(on
)) == -1) {
1648 DWARN("setsockopt %s (ignored)", "IPTOS_THROUGHPUT");
1662 dataconn(const char *lmode
)
1664 struct sockinet from
;
1665 int s
, flags
, rv
, timeout
;
1666 struct timeval endtime
, now
, td
;
1667 struct pollfd pfd
[1];
1670 if (passivemode
) /* passive data connection */
1671 return (fdopen(data
, lmode
));
1673 /* active mode data connection */
1675 if ((flags
= fcntl(data
, F_GETFL
, 0)) == -1)
1676 goto dataconn_failed
; /* get current socket flags */
1677 if (fcntl(data
, F_SETFL
, flags
| O_NONBLOCK
) == -1)
1678 goto dataconn_failed
; /* set non-blocking connect */
1680 /* NOTE: we now must restore socket flags on successful exit */
1682 /* limit time waiting on listening socket */
1684 pfd
[0].events
= POLLIN
;
1685 (void)gettimeofday(&endtime
, NULL
); /* determine end time */
1686 endtime
.tv_sec
+= (quit_time
> 0) ? quit_time
: 60;
1687 /* without -q, default to 60s */
1689 (void)gettimeofday(&now
, NULL
);
1690 timersub(&endtime
, &now
, &td
);
1691 timeout
= td
.tv_sec
* 1000 + td
.tv_usec
/1000;
1694 rv
= ftp_poll(pfd
, 1, timeout
);
1695 } while (rv
== -1 && errno
== EINTR
); /* loop until poll ! EINTR */
1697 warn("Can't poll waiting before accept");
1698 goto dataconn_failed
;
1701 warnx("Poll timeout waiting before accept");
1702 goto dataconn_failed
;
1705 /* (non-blocking) accept the connection */
1706 fromlen
= myctladdr
.su_len
;
1708 s
= accept(data
, (struct sockaddr
*) &from
.si_su
, &fromlen
);
1709 } while (s
== -1 && errno
== EINTR
); /* loop until accept ! EINTR */
1711 warn("Can't accept data connection");
1712 goto dataconn_failed
;
1717 if (fcntl(data
, F_SETFL
, flags
) == -1) /* restore socket flags */
1718 goto dataconn_failed
;
1720 #ifdef IPTOS_THROUGHPUT
1721 if (from
.su_family
== AF_INET
) {
1722 int tos
= IPTOS_THROUGHPUT
;
1723 if (setsockopt(s
, IPPROTO_IP
, IP_TOS
,
1724 (void *)&tos
, sizeof(tos
)) == -1) {
1725 DWARN("setsockopt %s (ignored)", "IPTOS_THROUGHPUT");
1729 return (fdopen(data
, lmode
));
1738 psabort(int notused
)
1752 static struct comvars
{
1754 char name
[MAXHOSTNAMELEN
];
1755 struct sockinet mctl
;
1756 struct sockinet hctl
;
1769 char mi
[MAXPATHLEN
];
1770 char mo
[MAXPATHLEN
];
1771 } proxstruct
, tmpstruct
;
1772 struct comvars
*ip
, *op
;
1775 oldintr
= xsignal(SIGINT
, psabort
);
1789 ip
->connect
= connected
;
1790 connected
= op
->connect
;
1792 (void)strlcpy(ip
->name
, hostname
, sizeof(ip
->name
));
1795 hostname
= op
->name
;
1796 ip
->hctl
= hisctladdr
;
1797 hisctladdr
= op
->hctl
;
1798 ip
->mctl
= myctladdr
;
1799 myctladdr
= op
->mctl
;
1806 ip
->curtpe
= curtype
;
1807 curtype
= op
->curtpe
;
1810 ip
->sunqe
= sunique
;
1811 sunique
= op
->sunqe
;
1812 ip
->runqe
= runique
;
1813 runique
= op
->runqe
;
1818 (void)strlcpy(ip
->nti
, ntin
, sizeof(ip
->nti
));
1819 (void)strlcpy(ntin
, op
->nti
, sizeof(ntin
));
1820 (void)strlcpy(ip
->nto
, ntout
, sizeof(ip
->nto
));
1821 (void)strlcpy(ntout
, op
->nto
, sizeof(ntout
));
1822 ip
->mapflg
= mapflag
;
1823 mapflag
= op
->mapflg
;
1824 (void)strlcpy(ip
->mi
, mapin
, sizeof(ip
->mi
));
1825 (void)strlcpy(mapin
, op
->mi
, sizeof(mapin
));
1826 (void)strlcpy(ip
->mo
, mapout
, sizeof(ip
->mo
));
1827 (void)strlcpy(mapout
, op
->mo
, sizeof(mapout
));
1828 (void)xsignal(SIGINT
, oldintr
);
1836 abortpt(int notused
)
1842 write(fileno(ttyout
), "\n", 1);
1846 siglongjmp(ptabort
, 1);
1850 proxtrans(const char *cmd
, const char *local
, const char *remote
)
1852 sigfunc
volatile oldintr
;
1853 int prox_type
, nfnd
;
1854 int volatile secndflag
;
1855 const char *volatile cmd2
;
1859 if (strcmp(cmd
, "RETR"))
1862 cmd2
= runique
? "STOU" : "STOR";
1863 if ((prox_type
= type
) == 0) {
1864 if (unix_server
&& unix_proxy
)
1869 if (curtype
!= prox_type
)
1870 changetype(prox_type
, 1);
1871 if (command("PASV") != COMPLETE
) {
1872 fputs("proxy server does not support third party transfers.\n",
1878 fputs("No primary connection.\n", ttyout
);
1883 if (curtype
!= prox_type
)
1884 changetype(prox_type
, 1);
1885 if (command("PORT %s", pasv
) != COMPLETE
) {
1889 if (sigsetjmp(ptabort
, 1))
1891 oldintr
= xsignal(SIGINT
, abortpt
);
1892 if ((restart_point
&&
1893 (command("REST " LLF
, (LLT
) restart_point
) != CONTINUE
))
1894 || (command("%s %s", cmd
, remote
) != PRELIM
)) {
1895 (void)xsignal(SIGINT
, oldintr
);
1902 if ((restart_point
&&
1903 (command("REST " LLF
, (LLT
) restart_point
) != CONTINUE
))
1904 || (command("%s %s", cmd2
, local
) != PRELIM
))
1910 (void)xsignal(SIGINT
, oldintr
);
1913 fprintf(ttyout
, "local: %s remote: %s\n", local
, remote
);
1916 if (sigsetjmp(xferabort
, 1)) {
1917 (void)xsignal(SIGINT
, oldintr
);
1920 (void)xsignal(SIGINT
, abort_squared
);
1922 if (strcmp(cmd
, "RETR") && !proxy
)
1924 else if (!strcmp(cmd
, "RETR") && proxy
)
1926 if (!cpend
&& !secndflag
) { /* only here if cmd = "STOR" (proxy=1) */
1927 if (command("%s %s", cmd2
, local
) != PRELIM
) {
1935 (void)xsignal(SIGINT
, oldintr
);
1941 if (!cpend
&& !secndflag
) { /* only if cmd = "RETR" (proxy=1) */
1942 if (command("%s %s", cmd2
, local
) != PRELIM
) {
1949 (void)xsignal(SIGINT
, oldintr
);
1957 if ((nfnd
= empty(cin
, NULL
, 10)) <= 0) {
1959 warn("Error aborting proxy command");
1972 (void)xsignal(SIGINT
, oldintr
);
1976 reset(int argc
, char *argv
[])
1980 if (argc
== 0 && argv
!= NULL
) {
1981 UPRINTF("usage: %s\n", argv
[0]);
1986 if ((nfnd
= empty(cin
, NULL
, 0)) < 0) {
1987 warn("Error resetting connection");
1996 gunique(const char *local
)
1998 static char new[MAXPATHLEN
];
1999 char *cp
= strrchr(local
, '/');
2000 int d
, count
=0, len
;
2005 d
= access(cp
== local
? "/" : cp
? local
: ".", W_OK
);
2009 warn("Can't access `%s'", local
);
2012 len
= strlcpy(new, local
, sizeof(new));
2016 if (++count
== 100) {
2017 fputs("runique: can't find unique file name.\n",
2027 if ((d
= access(new, F_OK
)) < 0)
2031 else if (*(cp
- 2) == '.')
2034 *(cp
- 2) = *(cp
- 2) + 1;
2043 * aborts abort_remote(). lostpeer() is called because if the user is
2044 * too impatient to wait or there's another problem then ftp really
2045 * needs to get back to a known state.
2048 abort_squared(int dummy
)
2055 len
= strlcpy(msgbuf
, "\nremote abort aborted; closing connection.\n",
2057 write(fileno(ttyout
), msgbuf
, len
);
2059 siglongjmp(xferabort
, 1);
2063 abort_remote(FILE *din
)
2069 warnx("Lost control connection for abort");
2076 * send IAC in urgent mode instead of DM because 4.3BSD places oob mark
2077 * after urgent byte rather than before as is protocol now
2082 if (send(fileno(cout
), buf
, 3, MSG_OOB
) != 3)
2083 warn("Can't send abort message");
2084 fprintf(cout
, "%cABOR\r\n", DM
);
2086 if ((nfnd
= empty(cin
, din
, 10)) <= 0) {
2088 warn("Can't send abort message");
2093 if (din
&& (nfnd
& 2)) {
2094 while (read(fileno(din
), buf
, BUFSIZ
) > 0)
2097 if (getreply(0) == ERROR
&& code
== 552) {
2098 /* 552 needed for nic style abort */
2105 * Ensure that ai->ai_addr is NOT an IPv4 mapped address.
2106 * IPv4 mapped address complicates too many things in FTP
2107 * protocol handling, as FTP protocol is defined differently
2108 * between IPv4 and IPv6.
2110 * This may not be the best way to handle this situation,
2111 * since the semantics of IPv4 mapped address is defined in
2112 * the kernel. There are configurations where we should use
2113 * IPv4 mapped address as native IPv6 address, not as
2114 * "an IPv6 address that embeds IPv4 address" (namely, SIIT).
2116 * More complete solution would be to have an additional
2117 * getsockopt to grab "real" peername/sockname. "real"
2118 * peername/sockname will be AF_INET if IPv4 mapped address
2119 * is used to embed IPv4 address, and will be AF_INET6 if
2120 * we use it as native. What a mess!
2123 ai_unmapped(struct addrinfo
*ai
)
2126 struct sockaddr_in6
*sin6
;
2127 struct sockaddr_in sin
;
2130 if (ai
->ai_family
!= AF_INET6
)
2132 if (ai
->ai_addrlen
!= sizeof(struct sockaddr_in6
) ||
2133 sizeof(sin
) > ai
->ai_addrlen
)
2135 sin6
= (struct sockaddr_in6
*)ai
->ai_addr
;
2136 if (!IN6_IS_ADDR_V4MAPPED(&sin6
->sin6_addr
))
2139 memset(&sin
, 0, sizeof(sin
));
2140 sin
.sin_family
= AF_INET
;
2141 len
= sizeof(struct sockaddr_in
);
2142 memcpy(&sin
.sin_addr
, &sin6
->sin6_addr
.s6_addr
[12],
2143 sizeof(sin
.sin_addr
));
2144 sin
.sin_port
= sin6
->sin6_port
;
2146 ai
->ai_family
= AF_INET
;
2147 #if defined(HAVE_STRUCT_SOCKADDR_IN_SIN_LEN)
2150 memcpy(ai
->ai_addr
, &sin
, len
);
2151 ai
->ai_addrlen
= len
;
2159 fputs("Usage error\n", ttyout
);