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