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