ftp.c revision 1.84 1 /* $NetBSD: ftp.c,v 1.84 1999/10/12 06:05:01 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.84 1999/10/12 06:05:01 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 sigjmp_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 int oerrno = errno;
392
393 alarmtimer(0);
394 if (fromatty)
395 write(fileno(ttyout), "\n", 1);
396 abrtflag++;
397 if (ptflag)
398 siglongjmp(ptabort, 1);
399 errno = oerrno;
400 }
401
402 void
403 cmdtimeout(notused)
404 int notused;
405 {
406 int oerrno = errno;
407
408 alarmtimer(0);
409 if (fromatty)
410 write(fileno(ttyout), "\n", 1);
411 timeoutflag++;
412 if (ptflag)
413 siglongjmp(ptabort, 1);
414 errno = oerrno;
415 }
416
417
418 /*VARARGS*/
419 int
420 #ifdef __STDC__
421 command(const char *fmt, ...)
422 #else
423 command(va_alist)
424 va_dcl
425 #endif
426 {
427 va_list ap;
428 int r;
429 sig_t oldsigint;
430 #ifndef __STDC__
431 const char *fmt;
432 #endif
433
434 if (debug) {
435 fputs("---> ", ttyout);
436 #ifdef __STDC__
437 va_start(ap, fmt);
438 #else
439 va_start(ap);
440 fmt = va_arg(ap, const char *);
441 #endif
442 if (strncmp("PASS ", fmt, 5) == 0)
443 fputs("PASS XXXX", ttyout);
444 else if (strncmp("ACCT ", fmt, 5) == 0)
445 fputs("ACCT XXXX", ttyout);
446 else
447 vfprintf(ttyout, fmt, ap);
448 va_end(ap);
449 putc('\n', ttyout);
450 }
451 if (cout == NULL) {
452 warnx("No control connection for command.");
453 code = -1;
454 return (0);
455 }
456
457 abrtflag = 0;
458
459 oldsigint = xsignal(SIGINT, cmdabort);
460
461 #ifdef __STDC__
462 va_start(ap, fmt);
463 #else
464 va_start(ap);
465 fmt = va_arg(ap, char *);
466 #endif
467 vfprintf(cout, fmt, ap);
468 va_end(ap);
469 fputs("\r\n", cout);
470 (void)fflush(cout);
471 cpend = 1;
472 r = getreply(!strcmp(fmt, "QUIT"));
473 if (abrtflag && oldsigint != SIG_IGN)
474 (*oldsigint)(SIGINT);
475 (void)xsignal(SIGINT, oldsigint);
476 return (r);
477 }
478
479 int
480 getreply(expecteof)
481 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 sig_t 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 if (expecteof) {
530 alarmtimer(0);
531 (void)xsignal(SIGINT, oldsigint);
532 (void)xsignal(SIGALRM, oldsigalrm);
533 code = 221;
534 return (0);
535 }
536 lostpeer();
537 if (verbose) {
538 if (reply_timeoutflag)
539 fputs(
540 "421 Service not available, remote server timed out.\n", ttyout);
541 else if (reply_abrtflag)
542 fputs(
543 "421 Service not available, user interrupt.\n", ttyout);
544 else
545 fputs(
546 "421 Service not available, remote server has closed connection.\n",
547 ttyout);
548 (void)fflush(ttyout);
549 }
550 code = 421;
551 /* the lostpeer() above calls alarmtimer(0); */
552 (void)xsignal(SIGINT, oldsigint);
553 (void)xsignal(SIGALRM, oldsigalrm);
554 return (4);
555 }
556 if (c != '\r' && (verbose > 0 ||
557 ((verbose > -1 && n == '5' && dig > 4) &&
558 (((!n && c < '5') || (n && n < '5'))
559 || !retry_connect)))) {
560 if (proxflag &&
561 (dig == 1 || (dig == 5 && verbose == 0)))
562 fprintf(ttyout, "%s:", hostname);
563 (void)putc(c, ttyout);
564 }
565 if (dig < 4 && isdigit(c))
566 code = code * 10 + (c - '0');
567 if (!pflag && (code == 227 || code == 228))
568 pflag = 1;
569 else if (!pflag && code == 229)
570 pflag = 100;
571 if (dig > 4 && pflag == 1 && isdigit(c))
572 pflag = 2;
573 if (pflag == 2) {
574 if (c != '\r' && c != ')')
575 *pt++ = c;
576 else {
577 *pt = '\0';
578 pflag = 3;
579 }
580 }
581 if (pflag == 100 && c == '(')
582 pflag = 2;
583 if (dig == 4 && c == '-') {
584 if (continuation)
585 code = 0;
586 continuation++;
587 }
588 if (n == 0)
589 n = c;
590 if (cp < ¤t_line[sizeof(current_line) - 1])
591 *cp++ = c;
592 }
593 if (verbose > 0 || ((verbose > -1 && n == '5') &&
594 (n < '5' || !retry_connect))) {
595 (void)putc(c, ttyout);
596 (void)fflush (ttyout);
597 }
598 if (line == 0) {
599 size_t len = cp - current_line;
600
601 if (len > sizeof(reply_string))
602 len = sizeof(reply_string);
603
604 (void)strlcpy(reply_string, current_line, len);
605 }
606 if (continuation && code != originalcode) {
607 if (originalcode == 0)
608 originalcode = code;
609 continue;
610 }
611 *cp = '\0';
612 if (n != '1')
613 cpend = 0;
614 alarm(0);
615 (void)xsignal(SIGINT, oldsigint);
616 (void)xsignal(SIGALRM, oldsigalrm);
617 if (code == 421 || originalcode == 421)
618 lostpeer();
619 if (abrtflag && oldsigint != cmdabort && oldsigint != SIG_IGN)
620 (*oldsigint)(SIGINT);
621 if (timeoutflag && oldsigalrm != cmdtimeout &&
622 oldsigalrm != SIG_IGN)
623 (*oldsigalrm)(SIGINT);
624 return (n - '0');
625 }
626 }
627
628 static int
629 empty(cin, din, sec)
630 FILE *cin;
631 FILE *din;
632 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(notused)
694 int notused;
695 {
696 char msgbuf[100];
697 int len;
698
699 alarmtimer(0);
700 mflag = 0;
701 abrtflag = 0;
702 switch (direction[0]) {
703 case 'r':
704 strlcpy(msgbuf, "\nreceive", sizeof(msgbuf));
705 break;
706 case 's':
707 strlcpy(msgbuf, "\nsend", sizeof(msgbuf));
708 break;
709 default:
710 errx(1, "abortxfer called with unknown direction %s\n",
711 direction);
712 }
713 len = strlcat(msgbuf, " aborted\nwaiting for remote to finish abort.\n",
714 sizeof(msgbuf));
715 write(fileno(ttyout), msgbuf, len);
716 siglongjmp(xferabort, 1);
717 }
718
719 void
720 sendrequest(cmd, local, remote, printnames)
721 const char *cmd, *local, *remote;
722 int printnames;
723 {
724 struct stat st;
725 int c, d;
726 FILE *fin, *dout;
727 int (*closefunc) __P((FILE *));
728 sig_t oldintr, oldintp;
729 volatile off_t hashbytes;
730 char *lmode, *bufp;
731 static size_t bufsize;
732 static char *buf;
733 int oprogress;
734
735 #ifdef __GNUC__ /* to shut up gcc warnings */
736 (void)&fin;
737 (void)&dout;
738 (void)&closefunc;
739 (void)&oldintr;
740 (void)&oldintp;
741 (void)&lmode;
742 #endif
743
744 hashbytes = mark;
745 direction = "sent";
746 dout = NULL;
747 bytes = 0;
748 filesize = -1;
749 oprogress = progress;
750 if (verbose && printnames) {
751 if (local && *local != '-')
752 fprintf(ttyout, "local: %s ", local);
753 if (remote)
754 fprintf(ttyout, "remote: %s\n", remote);
755 }
756 if (proxy) {
757 proxtrans(cmd, local, remote);
758 return;
759 }
760 if (curtype != type)
761 changetype(type, 0);
762 closefunc = NULL;
763 oldintr = NULL;
764 oldintp = NULL;
765 lmode = "w";
766 if (sigsetjmp(xferabort, 1)) {
767 while (cpend) {
768 (void)getreply(0);
769 }
770 code = -1;
771 goto cleanupsend;
772 }
773 (void)xsignal(SIGQUIT, psummary);
774 oldintr = xsignal(SIGINT, abortxfer);
775 if (strcmp(local, "-") == 0) {
776 fin = stdin;
777 progress = 0;
778 } else if (*local == '|') {
779 oldintp = xsignal(SIGPIPE, SIG_IGN);
780 fin = popen(local + 1, "r");
781 if (fin == NULL) {
782 warn("%s", local + 1);
783 code = -1;
784 goto cleanupsend;
785 }
786 progress = 0;
787 closefunc = pclose;
788 } else {
789 fin = fopen(local, "r");
790 if (fin == NULL) {
791 warn("local: %s", local);
792 code = -1;
793 goto cleanupsend;
794 }
795 closefunc = fclose;
796 if (fstat(fileno(fin), &st) < 0 || !S_ISREG(st.st_mode)) {
797 fprintf(ttyout, "%s: not a plain file.\n", local);
798 code = -1;
799 goto cleanupsend;
800 }
801 filesize = st.st_size;
802 }
803 if (initconn()) {
804 code = -1;
805 goto cleanupsend;
806 }
807 if (sigsetjmp(xferabort, 1))
808 goto abort;
809
810 if (restart_point &&
811 (strcmp(cmd, "STOR") == 0 || strcmp(cmd, "APPE") == 0)) {
812 int rc;
813
814 rc = -1;
815 switch (curtype) {
816 case TYPE_A:
817 rc = fseek(fin, (long) restart_point, SEEK_SET);
818 break;
819 case TYPE_I:
820 case TYPE_L:
821 rc = lseek(fileno(fin), restart_point, SEEK_SET);
822 break;
823 }
824 if (rc < 0) {
825 warn("local: %s", local);
826 goto cleanupsend;
827 }
828 #ifndef NO_QUAD
829 if (command("REST %lld", (long long) restart_point) !=
830 #else
831 if (command("REST %ld", (long) restart_point) !=
832 #endif
833 CONTINUE) {
834 goto cleanupsend;
835 }
836 lmode = "r+w";
837 }
838 if (remote) {
839 if (command("%s %s", cmd, remote) != PRELIM) {
840 goto cleanupsend;
841 }
842 } else
843 if (command("%s", cmd) != PRELIM) {
844 goto cleanupsend;
845 }
846 dout = dataconn(lmode);
847 if (dout == NULL)
848 goto abort;
849
850 if (sndbuf_size > bufsize) {
851 if (buf)
852 (void)free(buf);
853 bufsize = sndbuf_size;
854 buf = xmalloc(bufsize);
855 }
856
857 progressmeter(-1);
858 oldintp = xsignal(SIGPIPE, SIG_IGN);
859
860 switch (curtype) {
861
862 case TYPE_I:
863 case TYPE_L:
864 while (1) {
865 struct timeval then, now, td;
866 off_t bufrem;
867
868 if (rate_put)
869 (void)gettimeofday(&then, NULL);
870 errno = c = d = 0;
871 bufrem = rate_put ? rate_put : bufsize;
872 while (bufrem > 0) {
873 if ((c = read(fileno(fin), buf,
874 MIN(bufsize, bufrem))) <= 0)
875 goto senddone;
876 bytes += c;
877 bufrem -= c;
878 for (bufp = buf; c > 0; c -= d, bufp += d)
879 if ((d = write(fileno(dout), bufp, c))
880 <= 0)
881 break;
882 if (d < 0)
883 goto senddone;
884 if (hash && (!progress || filesize < 0) ) {
885 while (bytes >= hashbytes) {
886 (void)putc('#', ttyout);
887 hashbytes += mark;
888 }
889 (void)fflush(ttyout);
890 }
891 }
892 if (rate_put) {
893 while (1) {
894 (void)gettimeofday(&now, NULL);
895 timersub(&now, &then, &td);
896 if (td.tv_sec > 0)
897 break;
898 usleep(1000000 - td.tv_usec);
899 }
900 }
901 }
902 senddone:
903 if (hash && (!progress || filesize < 0) && bytes > 0) {
904 if (bytes < mark)
905 (void)putc('#', ttyout);
906 (void)putc('\n', ttyout);
907 }
908 if (c < 0)
909 warn("local: %s", local);
910 if (d < 0) {
911 if (errno != EPIPE)
912 warn("netout");
913 bytes = -1;
914 }
915 break;
916
917 case TYPE_A:
918 while ((c = getc(fin)) != EOF) {
919 if (c == '\n') {
920 while (hash && (!progress || filesize < 0) &&
921 (bytes >= hashbytes)) {
922 (void)putc('#', ttyout);
923 (void)fflush(ttyout);
924 hashbytes += mark;
925 }
926 if (ferror(dout))
927 break;
928 (void)putc('\r', dout);
929 bytes++;
930 }
931 (void)putc(c, dout);
932 bytes++;
933 #if 0 /* this violates RFC */
934 if (c == '\r') {
935 (void)putc('\0', dout);
936 bytes++;
937 }
938 #endif
939 }
940 if (hash && (!progress || filesize < 0)) {
941 if (bytes < hashbytes)
942 (void)putc('#', ttyout);
943 (void)putc('\n', ttyout);
944 }
945 if (ferror(fin))
946 warn("local: %s", local);
947 if (ferror(dout)) {
948 if (errno != EPIPE)
949 warn("netout");
950 bytes = -1;
951 }
952 break;
953 }
954
955 progressmeter(1);
956 if (closefunc != NULL) {
957 (*closefunc)(fin);
958 fin = NULL;
959 }
960 (void)fclose(dout);
961 dout = NULL;
962 (void)getreply(0);
963 if (bytes > 0)
964 ptransfer(0);
965 goto cleanupsend;
966
967 abort:
968 (void)xsignal(SIGINT, oldintr);
969 oldintr = NULL;
970 if (!cpend) {
971 code = -1;
972 goto cleanupsend;
973 }
974 if (data >= 0) {
975 (void)close(data);
976 data = -1;
977 }
978 if (dout) {
979 (void)fclose(dout);
980 dout = NULL;
981 }
982 (void)getreply(0);
983 code = -1;
984 if (bytes > 0)
985 ptransfer(0);
986
987 cleanupsend:
988 if (oldintr)
989 (void)xsignal(SIGINT, oldintr);
990 if (oldintp)
991 (void)xsignal(SIGPIPE, oldintp);
992 if (data >= 0) {
993 (void)close(data);
994 data = -1;
995 }
996 if (closefunc != NULL && fin != NULL)
997 (*closefunc)(fin);
998 if (dout)
999 (void)fclose(dout);
1000 progress = oprogress;
1001 restart_point = 0;
1002 bytes = 0;
1003 }
1004
1005 void
1006 recvrequest(cmd, local, remote, lmode, printnames, ignorespecial)
1007 const char *cmd, *local, *remote, *lmode;
1008 int printnames, ignorespecial;
1009 {
1010 FILE *fout, *din;
1011 int (*closefunc) __P((FILE *));
1012 sig_t oldintr, oldintp;
1013 int c, d;
1014 volatile int is_retr, tcrflag, bare_lfs;
1015 static size_t bufsize;
1016 static char *buf;
1017 volatile off_t hashbytes;
1018 struct stat st;
1019 time_t mtime;
1020 struct timeval tval[2];
1021 int oprogress;
1022 int opreserve;
1023
1024 #ifdef __GNUC__ /* to shut up gcc warnings */
1025 (void)&local;
1026 (void)&fout;
1027 (void)&din;
1028 (void)&closefunc;
1029 (void)&oldintr;
1030 (void)&oldintp;
1031 #endif
1032
1033 fout = NULL;
1034 din = NULL;
1035 hashbytes = mark;
1036 direction = "received";
1037 bytes = 0;
1038 bare_lfs = 0;
1039 filesize = -1;
1040 oprogress = progress;
1041 opreserve = preserve;
1042 is_retr = (strcmp(cmd, "RETR") == 0);
1043 if (is_retr && verbose && printnames) {
1044 if (local && (ignorespecial || *local != '-'))
1045 fprintf(ttyout, "local: %s ", local);
1046 if (remote)
1047 fprintf(ttyout, "remote: %s\n", remote);
1048 }
1049 if (proxy && is_retr) {
1050 proxtrans(cmd, local, remote);
1051 return;
1052 }
1053 closefunc = NULL;
1054 oldintr = NULL;
1055 oldintp = NULL;
1056 tcrflag = !crflag && is_retr;
1057 if (sigsetjmp(xferabort, 1)) {
1058 while (cpend) {
1059 (void)getreply(0);
1060 }
1061 code = -1;
1062 goto cleanuprecv;
1063 }
1064 (void)xsignal(SIGQUIT, psummary);
1065 oldintr = xsignal(SIGINT, abortxfer);
1066 if (ignorespecial || (strcmp(local, "-") && *local != '|')) {
1067 if (access(local, W_OK) < 0) {
1068 char *dir = strrchr(local, '/');
1069
1070 if (errno != ENOENT && errno != EACCES) {
1071 warn("local: %s", local);
1072 code = -1;
1073 goto cleanuprecv;
1074 }
1075 if (dir != NULL)
1076 *dir = 0;
1077 d = access(dir == local ? "/" :
1078 dir ? local : ".", W_OK);
1079 if (dir != NULL)
1080 *dir = '/';
1081 if (d < 0) {
1082 warn("local: %s", local);
1083 code = -1;
1084 goto cleanuprecv;
1085 }
1086 if (!runique && errno == EACCES &&
1087 chmod(local, (S_IRUSR|S_IWUSR)) < 0) {
1088 warn("local: %s", local);
1089 code = -1;
1090 goto cleanuprecv;
1091 }
1092 if (runique && errno == EACCES &&
1093 (local = gunique(local)) == NULL) {
1094 code = -1;
1095 goto cleanuprecv;
1096 }
1097 }
1098 else if (runique && (local = gunique(local)) == NULL) {
1099 code = -1;
1100 goto cleanuprecv;
1101 }
1102 }
1103 if (!is_retr) {
1104 if (curtype != TYPE_A)
1105 changetype(TYPE_A, 0);
1106 } else {
1107 if (curtype != type)
1108 changetype(type, 0);
1109 filesize = remotesize(remote, 0);
1110 }
1111 if (initconn()) {
1112 code = -1;
1113 goto cleanuprecv;
1114 }
1115 if (sigsetjmp(xferabort, 1))
1116 goto abort;
1117 if (is_retr && restart_point &&
1118 #ifndef NO_QUAD
1119 command("REST %lld", (long long) restart_point) != CONTINUE)
1120 #else
1121 command("REST %ld", (long) restart_point) != CONTINUE)
1122 #endif
1123 goto cleanuprecv;
1124 if (remote) {
1125 if (command("%s %s", cmd, remote) != PRELIM) {
1126 goto cleanuprecv;
1127 }
1128 } else {
1129 if (command("%s", cmd) != PRELIM) {
1130 goto cleanuprecv;
1131 }
1132 }
1133 din = dataconn("r");
1134 if (din == NULL)
1135 goto abort;
1136 if (!ignorespecial && strcmp(local, "-") == 0) {
1137 fout = stdout;
1138 progress = 0;
1139 preserve = 0;
1140 } else if (!ignorespecial && *local == '|') {
1141 oldintp = xsignal(SIGPIPE, SIG_IGN);
1142 fout = popen(local + 1, "w");
1143 if (fout == NULL) {
1144 warn("%s", local+1);
1145 goto abort;
1146 }
1147 progress = 0;
1148 preserve = 0;
1149 closefunc = pclose;
1150 } else {
1151 fout = fopen(local, lmode);
1152 if (fout == NULL) {
1153 warn("local: %s", local);
1154 goto abort;
1155 }
1156 closefunc = fclose;
1157 }
1158
1159 if (fstat(fileno(fout), &st) != -1 && !S_ISREG(st.st_mode)) {
1160 progress = 0;
1161 preserve = 0;
1162 }
1163 if (rcvbuf_size > bufsize) {
1164 if (buf)
1165 (void)free(buf);
1166 bufsize = rcvbuf_size;
1167 buf = xmalloc(bufsize);
1168 }
1169
1170 progressmeter(-1);
1171
1172 switch (curtype) {
1173
1174 case TYPE_I:
1175 case TYPE_L:
1176 if (is_retr && restart_point &&
1177 lseek(fileno(fout), restart_point, SEEK_SET) < 0) {
1178 warn("local: %s", local);
1179 goto cleanuprecv;
1180 }
1181 while (1) {
1182 struct timeval then, now, td;
1183 off_t bufrem;
1184
1185 if (rate_get)
1186 (void)gettimeofday(&then, NULL);
1187 errno = c = d = 0;
1188 bufrem = rate_get ? rate_get : bufsize;
1189 while (bufrem > 0) {
1190 if ((c = read(fileno(din), buf,
1191 MIN(bufsize, bufrem))) <= 0)
1192 goto recvdone;
1193 bytes += c;
1194 bufrem -=c;
1195 if ((d = write(fileno(fout), buf, c)) != c)
1196 goto recvdone;
1197 if (hash && (!progress || filesize < 0)) {
1198 while (bytes >= hashbytes) {
1199 (void)putc('#', ttyout);
1200 hashbytes += mark;
1201 }
1202 (void)fflush(ttyout);
1203 }
1204 }
1205 if (rate_get) {
1206 while (1) {
1207 (void)gettimeofday(&now, NULL);
1208 timersub(&now, &then, &td);
1209 if (td.tv_sec > 0)
1210 break;
1211 usleep(1000000 - td.tv_usec);
1212 }
1213 }
1214 }
1215 recvdone:
1216 if (hash && (!progress || filesize < 0) && bytes > 0) {
1217 if (bytes < mark)
1218 (void)putc('#', ttyout);
1219 (void)putc('\n', ttyout);
1220 }
1221 if (c < 0) {
1222 if (errno != EPIPE)
1223 warn("netin");
1224 bytes = -1;
1225 }
1226 if (d < c) {
1227 if (d < 0)
1228 warn("local: %s", local);
1229 else
1230 warnx("%s: short write", local);
1231 }
1232 break;
1233
1234 case TYPE_A:
1235 if (is_retr && restart_point) {
1236 int ch;
1237 long i, n;
1238
1239 if (fseek(fout, 0L, SEEK_SET) < 0)
1240 goto done;
1241 n = (long)restart_point;
1242 for (i = 0; i++ < n;) {
1243 if ((ch = getc(fout)) == EOF)
1244 goto done;
1245 if (ch == '\n')
1246 i++;
1247 }
1248 if (fseek(fout, 0L, SEEK_CUR) < 0) {
1249 done:
1250 warn("local: %s", local);
1251 goto cleanuprecv;
1252 }
1253 }
1254 while ((c = getc(din)) != EOF) {
1255 if (c == '\n')
1256 bare_lfs++;
1257 while (c == '\r') {
1258 while (hash && (!progress || filesize < 0) &&
1259 (bytes >= hashbytes)) {
1260 (void)putc('#', ttyout);
1261 (void)fflush(ttyout);
1262 hashbytes += mark;
1263 }
1264 bytes++;
1265 if ((c = getc(din)) != '\n' || tcrflag) {
1266 if (ferror(fout))
1267 goto break2;
1268 (void)putc('\r', fout);
1269 if (c == '\0') {
1270 bytes++;
1271 goto contin2;
1272 }
1273 if (c == EOF)
1274 goto contin2;
1275 }
1276 }
1277 (void)putc(c, fout);
1278 bytes++;
1279 contin2: ;
1280 }
1281 break2:
1282 if (hash && (!progress || filesize < 0)) {
1283 if (bytes < hashbytes)
1284 (void)putc('#', ttyout);
1285 (void)putc('\n', ttyout);
1286 }
1287 if (ferror(din)) {
1288 if (errno != EPIPE)
1289 warn("netin");
1290 bytes = -1;
1291 }
1292 if (ferror(fout))
1293 warn("local: %s", local);
1294 break;
1295 }
1296
1297 progressmeter(1);
1298 if (closefunc != NULL) {
1299 (*closefunc)(fout);
1300 fout = NULL;
1301 }
1302 (void)fclose(din);
1303 din = NULL;
1304 (void)getreply(0);
1305 if (bare_lfs) {
1306 fprintf(ttyout,
1307 "WARNING! %d bare linefeeds received in ASCII mode.\n",
1308 bare_lfs);
1309 fputs("File may not have transferred correctly.\n", ttyout);
1310 }
1311 if (bytes >= 0 && is_retr) {
1312 if (bytes > 0)
1313 ptransfer(0);
1314 if (preserve && (closefunc == fclose)) {
1315 mtime = remotemodtime(remote, 0);
1316 if (mtime != -1) {
1317 (void)gettimeofday(&tval[0], NULL);
1318 tval[1].tv_sec = mtime;
1319 tval[1].tv_usec = 0;
1320 if (utimes(local, tval) == -1) {
1321 fprintf(ttyout,
1322 "Can't change modification time on %s to %s",
1323 local, asctime(localtime(&mtime)));
1324 }
1325 }
1326 }
1327 }
1328 goto cleanuprecv;
1329
1330 abort:
1331 /*
1332 * abort using RFC 959 recommended IP,SYNC sequence
1333 */
1334 if (! sigsetjmp(xferabort, 1)) {
1335 /* this is the first call */
1336 (void)xsignal(SIGINT, abort_squared);
1337 if (!cpend) {
1338 code = -1;
1339 goto cleanuprecv;
1340 }
1341 abort_remote(din);
1342 }
1343 code = -1;
1344 if (bytes > 0)
1345 ptransfer(0);
1346
1347 cleanuprecv:
1348 if (oldintr)
1349 (void)xsignal(SIGINT, oldintr);
1350 if (oldintp)
1351 (void)xsignal(SIGPIPE, oldintp);
1352 if (data >= 0) {
1353 (void)close(data);
1354 data = -1;
1355 }
1356 if (closefunc != NULL && fout != NULL)
1357 (*closefunc)(fout);
1358 if (din)
1359 (void)fclose(din);
1360 progress = oprogress;
1361 preserve = opreserve;
1362 bytes = 0;
1363 }
1364
1365 /*
1366 * Need to start a listen on the data channel before we send the command,
1367 * otherwise the server's connect may fail.
1368 */
1369 int
1370 initconn()
1371 {
1372 char *p, *a;
1373 int result, len, tmpno = 0;
1374 int on = 1;
1375 int error;
1376 u_int addr[16], port[2];
1377 u_int af, hal, pal;
1378 char *pasvcmd = NULL;
1379
1380 #ifdef INET6
1381 if (myctladdr.su_family == AF_INET6
1382 && (IN6_IS_ADDR_LINKLOCAL(&myctladdr.su_sin6.sin6_addr)
1383 || IN6_IS_ADDR_SITELOCAL(&myctladdr.su_sin6.sin6_addr))) {
1384 warnx("use of scoped address can be troublesome");
1385 }
1386 #endif
1387 reinit:
1388 if (passivemode) {
1389 data_addr = myctladdr;
1390 data = socket(data_addr.su_family, SOCK_STREAM, 0);
1391 if (data < 0) {
1392 warn("socket");
1393 return (1);
1394 }
1395 if ((options & SO_DEBUG) &&
1396 setsockopt(data, SOL_SOCKET, SO_DEBUG, (char *)&on,
1397 sizeof(on)) < 0)
1398 warn("setsockopt (ignored)");
1399 result = COMPLETE + 1;
1400 switch (data_addr.su_family) {
1401 case AF_INET:
1402 if (epsv4 && !epsv4bad) {
1403 result = command(pasvcmd = "EPSV");
1404 /*
1405 * this code is to be friendly with broken
1406 * BSDI ftpd
1407 */
1408 if (code / 10 == 22 && code != 229) {
1409 fputs(
1410 "wrong server: return code must be 229\n",
1411 ttyout);
1412 result = COMPLETE + 1;
1413 }
1414 if (result != COMPLETE) {
1415 epsv4bad = 1;
1416 if (debug)
1417 fputs(
1418 "disabling epsv4 for this connection\n",
1419 ttyout);
1420 }
1421 }
1422 if (result != COMPLETE)
1423 result = command(pasvcmd = "PASV");
1424 break;
1425 #ifdef INET6
1426 case AF_INET6:
1427 result = command(pasvcmd = "EPSV");
1428 /* this code is to be friendly with broken BSDI ftpd */
1429 if (code / 10 == 22 && code != 229) {
1430 fputs(
1431 "wrong server: return code must be 229\n",
1432 ttyout);
1433 result = COMPLETE + 1;
1434 }
1435 if (result != COMPLETE)
1436 result = command(pasvcmd = "LPSV");
1437 break;
1438 #endif
1439 default:
1440 result = COMPLETE + 1;
1441 break;
1442 }
1443 if (result != COMPLETE) {
1444 if (activefallback) {
1445 (void)close(data);
1446 data = -1;
1447 passivemode = 0;
1448 activefallback = 0;
1449 goto reinit;
1450 }
1451 fputs("Passive mode refused.\n", ttyout);
1452 goto bad;
1453 }
1454
1455 #define pack2(var, off) \
1456 (((var[(off) + 0] & 0xff) << 8) | ((var[(off) + 1] & 0xff) << 0))
1457 #define pack4(var, off) \
1458 (((var[(off) + 0] & 0xff) << 24) | ((var[(off) + 1] & 0xff) << 16) | \
1459 ((var[(off) + 2] & 0xff) << 8) | ((var[(off) + 3] & 0xff) << 0))
1460
1461 /*
1462 * What we've got at this point is a string of comma separated
1463 * one-byte unsigned integer values, separated by commas.
1464 */
1465 if (strcmp(pasvcmd, "PASV") == 0) {
1466 if (data_addr.su_family != AF_INET) {
1467 fputs(
1468 "Passive mode AF mismatch. Shouldn't happen!\n", ttyout);
1469 error = 1;
1470 goto bad;
1471 }
1472 if (code / 10 == 22 && code != 227) {
1473 fputs("wrong server: return code must be 227\n",
1474 ttyout);
1475 error = 1;
1476 goto bad;
1477 }
1478 error = sscanf(pasv, "%u,%u,%u,%u,%u,%u",
1479 &addr[0], &addr[1], &addr[2], &addr[3],
1480 &port[0], &port[1]);
1481 if (error != 6) {
1482 fputs(
1483 "Passive mode address scan failure. Shouldn't happen!\n", ttyout);
1484 error = 1;
1485 goto bad;
1486 }
1487 error = 0;
1488 memset(&data_addr, 0, sizeof(data_addr));
1489 data_addr.su_family = AF_INET;
1490 data_addr.su_len = sizeof(struct sockaddr_in);
1491 data_addr.su_sin.sin_addr.s_addr =
1492 htonl(pack4(addr, 0));
1493 data_addr.su_port = htons(pack2(port, 0));
1494 } else if (strcmp(pasvcmd, "LPSV") == 0) {
1495 if (code / 10 == 22 && code != 228) {
1496 fputs("wrong server: return code must be 228\n",
1497 ttyout);
1498 error = 1;
1499 goto bad;
1500 }
1501 switch (data_addr.su_family) {
1502 case AF_INET:
1503 error = sscanf(pasv,
1504 "%u,%u,%u,%u,%u,%u,%u,%u,%u",
1505 &af, &hal,
1506 &addr[0], &addr[1], &addr[2], &addr[3],
1507 &pal, &port[0], &port[1]);
1508 if (error != 9) {
1509 fputs(
1510 "Passive mode address scan failure. Shouldn't happen!\n", ttyout);
1511 error = 1;
1512 goto bad;
1513 }
1514 if (af != 4 || hal != 4 || pal != 2) {
1515 fputs(
1516 "Passive mode AF mismatch. Shouldn't happen!\n", ttyout);
1517 error = 1;
1518 goto bad;
1519 }
1520
1521 error = 0;
1522 memset(&data_addr, 0, sizeof(data_addr));
1523 data_addr.su_family = AF_INET;
1524 data_addr.su_len = sizeof(struct sockaddr_in);
1525 data_addr.su_sin.sin_addr.s_addr =
1526 htonl(pack4(addr, 0));
1527 data_addr.su_port = htons(pack2(port, 0));
1528 break;
1529 #ifdef INET6
1530 case AF_INET6:
1531 error = sscanf(pasv,
1532 "%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u",
1533 &af, &hal,
1534 &addr[0], &addr[1], &addr[2], &addr[3],
1535 &addr[4], &addr[5], &addr[6], &addr[7],
1536 &addr[8], &addr[9], &addr[10],
1537 &addr[11], &addr[12], &addr[13],
1538 &addr[14], &addr[15],
1539 &pal, &port[0], &port[1]);
1540 if (error != 21) {
1541 fputs(
1542 "Passive mode address scan failure. Shouldn't happen!\n", ttyout);
1543 error = 1;
1544 goto bad;
1545 }
1546 if (af != 6 || hal != 16 || pal != 2) {
1547 fputs(
1548 "Passive mode AF mismatch. Shouldn't happen!\n", ttyout);
1549 error = 1;
1550 goto bad;
1551 }
1552
1553 error = 0;
1554 memset(&data_addr, 0, sizeof(data_addr));
1555 data_addr.su_family = AF_INET6;
1556 data_addr.su_len = sizeof(struct sockaddr_in6);
1557 {
1558 u_int32_t *p32;
1559 p32 = (u_int32_t *)&data_addr.su_sin6.sin6_addr;
1560 p32[0] = htonl(pack4(addr, 0));
1561 p32[1] = htonl(pack4(addr, 4));
1562 p32[2] = htonl(pack4(addr, 8));
1563 p32[3] = htonl(pack4(addr, 12));
1564 }
1565 data_addr.su_port = htons(pack2(port, 0));
1566 break;
1567 #endif
1568 default:
1569 error = 1;
1570 }
1571 } else if (strcmp(pasvcmd, "EPSV") == 0) {
1572 char delim[4];
1573
1574 port[0] = 0;
1575 if (code / 10 == 22 && code != 229) {
1576 fputs("wrong server: return code must be 229\n",
1577 ttyout);
1578 error = 1;
1579 goto bad;
1580 }
1581 if (sscanf(pasv, "%c%c%c%d%c", &delim[0],
1582 &delim[1], &delim[2], &port[1],
1583 &delim[3]) != 5) {
1584 fputs("parse error!\n", ttyout);
1585 error = 1;
1586 goto bad;
1587 }
1588 if (delim[0] != delim[1] || delim[0] != delim[2]
1589 || delim[0] != delim[3]) {
1590 fputs("parse error!\n", ttyout);
1591 error = 1;
1592 goto bad;
1593 }
1594 data_addr = hisctladdr;
1595 data_addr.su_port = htons(port[1]);
1596 } else
1597 goto bad;
1598
1599 while (xconnect(data, (struct sockaddr *)&data_addr,
1600 data_addr.su_len) < 0) {
1601 if (errno == EINTR)
1602 continue;
1603 if (activefallback) {
1604 (void)close(data);
1605 data = -1;
1606 passivemode = 0;
1607 activefallback = 0;
1608 goto reinit;
1609 }
1610 warn("connect");
1611 goto bad;
1612 }
1613 #if defined(IPPROTO_IP) && defined(IP_TOS)
1614 if (data_addr.su_family == AF_INET) {
1615 on = IPTOS_THROUGHPUT;
1616 if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&on,
1617 sizeof(int)) < 0)
1618 warn("setsockopt TOS (ignored)");
1619 }
1620 #endif
1621 return (0);
1622 }
1623
1624 noport:
1625 data_addr = myctladdr;
1626 if (sendport)
1627 data_addr.su_port = 0; /* let system pick one */
1628 if (data != -1)
1629 (void)close(data);
1630 data = socket(data_addr.su_family, SOCK_STREAM, 0);
1631 if (data < 0) {
1632 warn("socket");
1633 if (tmpno)
1634 sendport = 1;
1635 return (1);
1636 }
1637 if (!sendport)
1638 if (setsockopt(data, SOL_SOCKET, SO_REUSEADDR, (char *)&on,
1639 sizeof(on)) < 0) {
1640 warn("setsockopt (reuse address)");
1641 goto bad;
1642 }
1643 if (bind(data, (struct sockaddr *)&data_addr, data_addr.su_len) < 0) {
1644 warn("bind");
1645 goto bad;
1646 }
1647 if (options & SO_DEBUG &&
1648 setsockopt(data, SOL_SOCKET, SO_DEBUG, (char *)&on,
1649 sizeof(on)) < 0)
1650 warn("setsockopt (ignored)");
1651 len = sizeof(data_addr);
1652 if (getsockname(data, (struct sockaddr *)&data_addr, &len) < 0) {
1653 warn("getsockname");
1654 goto bad;
1655 }
1656 if (xlisten(data, 1) < 0)
1657 warn("listen");
1658
1659 #define UC(b) (((int)b)&0xff)
1660
1661 if (sendport) {
1662 #ifdef INET6
1663 char hname[INET6_ADDRSTRLEN];
1664 int af;
1665 #endif
1666
1667 switch (data_addr.su_family) {
1668 case AF_INET:
1669 if (!epsv4 || epsv4bad) {
1670 result = COMPLETE + 1;
1671 break;
1672 }
1673 /* FALLTHROUGH */
1674 #ifdef INET6
1675 case AF_INET6:
1676 af = (data_addr.su_family == AF_INET) ? 1 : 2;
1677 if (getnameinfo((struct sockaddr *)&data_addr,
1678 data_addr.su_len, hname, sizeof(hname),
1679 NULL, 0, NI_NUMERICHOST)) {
1680 result = ERROR;
1681 } else {
1682 result = command("EPRT |%d|%s|%d|", af, hname,
1683 ntohs(data_addr.su_port));
1684 if (result != COMPLETE) {
1685 epsv4bad = 1;
1686 if (debug)
1687 fputs(
1688 "disabling epsv4 for this connection\n",
1689 ttyout);
1690 }
1691 }
1692 break;
1693 #endif
1694 default:
1695 result = COMPLETE + 1;
1696 break;
1697 }
1698 if (result == COMPLETE)
1699 goto skip_port;
1700
1701 switch (data_addr.su_family) {
1702 case AF_INET:
1703 a = (char *)&data_addr.su_sin.sin_addr;
1704 p = (char *)&data_addr.su_port;
1705 result = command("PORT %d,%d,%d,%d,%d,%d",
1706 UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
1707 UC(p[0]), UC(p[1]));
1708 break;
1709 #ifdef INET6
1710 case AF_INET6:
1711 a = (char *)&data_addr.su_sin6.sin6_addr;
1712 p = (char *)&data_addr.su_port;
1713 result = command(
1714 "LPRT %d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
1715 6, 16,
1716 UC(a[0]),UC(a[1]),UC(a[2]),UC(a[3]),
1717 UC(a[4]),UC(a[5]),UC(a[6]),UC(a[7]),
1718 UC(a[8]),UC(a[9]),UC(a[10]),UC(a[11]),
1719 UC(a[12]),UC(a[13]),UC(a[14]),UC(a[15]),
1720 2, UC(p[0]), UC(p[1]));
1721 break;
1722 #endif
1723 default:
1724 result = COMPLETE + 1; /* xxx */
1725 }
1726 skip_port:
1727
1728 if (result == ERROR && sendport == -1) {
1729 sendport = 0;
1730 tmpno = 1;
1731 goto noport;
1732 }
1733 return (result != COMPLETE);
1734 }
1735 if (tmpno)
1736 sendport = 1;
1737 #if defined(IPPROTO_IP) && defined(IP_TOS)
1738 if (data_addr.su_family == AF_INET) {
1739 on = IPTOS_THROUGHPUT;
1740 if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&on,
1741 sizeof(int)) < 0)
1742 warn("setsockopt TOS (ignored)");
1743 }
1744 #endif
1745 return (0);
1746 bad:
1747 (void)close(data), data = -1;
1748 if (tmpno)
1749 sendport = 1;
1750 return (1);
1751 }
1752
1753 FILE *
1754 dataconn(lmode)
1755 const char *lmode;
1756 {
1757 union sockunion from;
1758 int s, fromlen = myctladdr.su_len;
1759
1760 if (passivemode)
1761 return (fdopen(data, lmode));
1762
1763 s = accept(data, (struct sockaddr *) &from, &fromlen);
1764 if (s < 0) {
1765 warn("accept");
1766 (void)close(data), data = -1;
1767 return (NULL);
1768 }
1769 (void)close(data);
1770 data = s;
1771 #if defined(IPPROTO_IP) && defined(IP_TOS)
1772 if (from.su_family == AF_INET) {
1773 int tos = IPTOS_THROUGHPUT;
1774 if (setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&tos,
1775 sizeof(int)) < 0) {
1776 warn("setsockopt TOS (ignored)");
1777 }
1778 }
1779 #endif
1780 return (fdopen(data, lmode));
1781 }
1782
1783 void
1784 psabort(notused)
1785 int notused;
1786 {
1787 int oerrno = errno;
1788
1789 alarmtimer(0);
1790 abrtflag++;
1791 errno = oerrno;
1792 }
1793
1794 void
1795 pswitch(flag)
1796 int flag;
1797 {
1798 sig_t oldintr;
1799 static struct comvars {
1800 int connect;
1801 char name[MAXHOSTNAMELEN];
1802 union sockunion mctl;
1803 union sockunion hctl;
1804 FILE *in;
1805 FILE *out;
1806 int tpe;
1807 int curtpe;
1808 int cpnd;
1809 int sunqe;
1810 int runqe;
1811 int mcse;
1812 int ntflg;
1813 char nti[17];
1814 char nto[17];
1815 int mapflg;
1816 char mi[MAXPATHLEN];
1817 char mo[MAXPATHLEN];
1818 } proxstruct, tmpstruct;
1819 struct comvars *ip, *op;
1820
1821 abrtflag = 0;
1822 oldintr = xsignal(SIGINT, psabort);
1823 if (flag) {
1824 if (proxy)
1825 return;
1826 ip = &tmpstruct;
1827 op = &proxstruct;
1828 proxy++;
1829 } else {
1830 if (!proxy)
1831 return;
1832 ip = &proxstruct;
1833 op = &tmpstruct;
1834 proxy = 0;
1835 }
1836 ip->connect = connected;
1837 connected = op->connect;
1838 if (hostname)
1839 (void)strlcpy(ip->name, hostname, sizeof(ip->name));
1840 else
1841 ip->name[0] = '\0';
1842 hostname = op->name;
1843 ip->hctl = hisctladdr;
1844 hisctladdr = op->hctl;
1845 ip->mctl = myctladdr;
1846 myctladdr = op->mctl;
1847 ip->in = cin;
1848 cin = op->in;
1849 ip->out = cout;
1850 cout = op->out;
1851 ip->tpe = type;
1852 type = op->tpe;
1853 ip->curtpe = curtype;
1854 curtype = op->curtpe;
1855 ip->cpnd = cpend;
1856 cpend = op->cpnd;
1857 ip->sunqe = sunique;
1858 sunique = op->sunqe;
1859 ip->runqe = runique;
1860 runique = op->runqe;
1861 ip->mcse = mcase;
1862 mcase = op->mcse;
1863 ip->ntflg = ntflag;
1864 ntflag = op->ntflg;
1865 (void)strlcpy(ip->nti, ntin, sizeof(ip->nti));
1866 (void)strlcpy(ntin, op->nti, sizeof(ntin));
1867 (void)strlcpy(ip->nto, ntout, sizeof(ip->nto));
1868 (void)strlcpy(ntout, op->nto, sizeof(ntout));
1869 ip->mapflg = mapflag;
1870 mapflag = op->mapflg;
1871 (void)strlcpy(ip->mi, mapin, sizeof(ip->mi));
1872 (void)strlcpy(mapin, op->mi, sizeof(mapin));
1873 (void)strlcpy(ip->mo, mapout, sizeof(ip->mo));
1874 (void)strlcpy(mapout, op->mo, sizeof(mapout));
1875 (void)xsignal(SIGINT, oldintr);
1876 if (abrtflag) {
1877 abrtflag = 0;
1878 (*oldintr)(SIGINT);
1879 }
1880 }
1881
1882 void
1883 abortpt(notused)
1884 int notused;
1885 {
1886
1887 alarmtimer(0);
1888 if (fromatty)
1889 write(fileno(ttyout), "\n", 1);
1890 ptabflg++;
1891 mflag = 0;
1892 abrtflag = 0;
1893 siglongjmp(ptabort, 1);
1894 }
1895
1896 void
1897 proxtrans(cmd, local, remote)
1898 const char *cmd, *local, *remote;
1899 {
1900 sig_t oldintr;
1901 int prox_type, nfnd;
1902 volatile int secndflag;
1903 char *cmd2;
1904
1905 #ifdef __GNUC__ /* to shut up gcc warnings */
1906 (void)&oldintr;
1907 (void)&cmd2;
1908 #endif
1909
1910 oldintr = NULL;
1911 secndflag = 0;
1912 if (strcmp(cmd, "RETR"))
1913 cmd2 = "RETR";
1914 else
1915 cmd2 = runique ? "STOU" : "STOR";
1916 if ((prox_type = type) == 0) {
1917 if (unix_server && unix_proxy)
1918 prox_type = TYPE_I;
1919 else
1920 prox_type = TYPE_A;
1921 }
1922 if (curtype != prox_type)
1923 changetype(prox_type, 1);
1924 if (command("PASV") != COMPLETE) {
1925 fputs("proxy server does not support third party transfers.\n",
1926 ttyout);
1927 return;
1928 }
1929 pswitch(0);
1930 if (!connected) {
1931 fputs("No primary connection.\n", ttyout);
1932 pswitch(1);
1933 code = -1;
1934 return;
1935 }
1936 if (curtype != prox_type)
1937 changetype(prox_type, 1);
1938 if (command("PORT %s", pasv) != COMPLETE) {
1939 pswitch(1);
1940 return;
1941 }
1942 if (sigsetjmp(ptabort, 1))
1943 goto abort;
1944 oldintr = xsignal(SIGINT, abortpt);
1945 if ((restart_point &&
1946 #ifndef NO_QUAD
1947 (command("REST %lld", (long long) restart_point) != CONTINUE)
1948 #else
1949 (command("REST %ld", (long) restart_point) != CONTINUE)
1950 #endif
1951 ) || (command("%s %s", cmd, remote) != PRELIM)) {
1952 (void)xsignal(SIGINT, oldintr);
1953 pswitch(1);
1954 return;
1955 }
1956 sleep(2);
1957 pswitch(1);
1958 secndflag++;
1959 if ((restart_point &&
1960 #ifndef NO_QUAD
1961 (command("REST %lld", (long long) restart_point) != CONTINUE)
1962 #else
1963 (command("REST %ld", (long) restart_point) != CONTINUE)
1964 #endif
1965 ) || (command("%s %s", cmd2, local) != PRELIM))
1966 goto abort;
1967 ptflag++;
1968 (void)getreply(0);
1969 pswitch(0);
1970 (void)getreply(0);
1971 (void)xsignal(SIGINT, oldintr);
1972 pswitch(1);
1973 ptflag = 0;
1974 fprintf(ttyout, "local: %s remote: %s\n", local, remote);
1975 return;
1976 abort:
1977 if (sigsetjmp(xferabort, 1)) {
1978 (void)xsignal(SIGINT, oldintr);
1979 return;
1980 }
1981 (void)xsignal(SIGINT, abort_squared);
1982 ptflag = 0;
1983 if (strcmp(cmd, "RETR") && !proxy)
1984 pswitch(1);
1985 else if (!strcmp(cmd, "RETR") && proxy)
1986 pswitch(0);
1987 if (!cpend && !secndflag) { /* only here if cmd = "STOR" (proxy=1) */
1988 if (command("%s %s", cmd2, local) != PRELIM) {
1989 pswitch(0);
1990 if (cpend)
1991 abort_remote(NULL);
1992 }
1993 pswitch(1);
1994 if (ptabflg)
1995 code = -1;
1996 (void)xsignal(SIGINT, oldintr);
1997 return;
1998 }
1999 if (cpend)
2000 abort_remote(NULL);
2001 pswitch(!proxy);
2002 if (!cpend && !secndflag) { /* only if cmd = "RETR" (proxy=1) */
2003 if (command("%s %s", cmd2, local) != PRELIM) {
2004 pswitch(0);
2005 if (cpend)
2006 abort_remote(NULL);
2007 pswitch(1);
2008 if (ptabflg)
2009 code = -1;
2010 (void)xsignal(SIGINT, oldintr);
2011 return;
2012 }
2013 }
2014 if (cpend)
2015 abort_remote(NULL);
2016 pswitch(!proxy);
2017 if (cpend) {
2018 if ((nfnd = empty(cin, NULL, 10)) <= 0) {
2019 if (nfnd < 0) {
2020 warn("abort");
2021 }
2022 if (ptabflg)
2023 code = -1;
2024 lostpeer();
2025 }
2026 (void)getreply(0);
2027 (void)getreply(0);
2028 }
2029 if (proxy)
2030 pswitch(0);
2031 pswitch(1);
2032 if (ptabflg)
2033 code = -1;
2034 (void)xsignal(SIGINT, oldintr);
2035 }
2036
2037 void
2038 reset(argc, argv)
2039 int argc;
2040 char *argv[];
2041 {
2042 int nfnd = 1;
2043
2044 while (nfnd > 0) {
2045 if ((nfnd = empty(cin, NULL, 0)) < 0) {
2046 warn("reset");
2047 code = -1;
2048 lostpeer();
2049 }
2050 else if (nfnd) {
2051 (void)getreply(0);
2052 }
2053 }
2054 }
2055
2056 char *
2057 gunique(local)
2058 const char *local;
2059 {
2060 static char new[MAXPATHLEN];
2061 char *cp = strrchr(local, '/');
2062 int d, count=0, len;
2063 char ext = '1';
2064
2065 if (cp)
2066 *cp = '\0';
2067 d = access(cp == local ? "/" : cp ? local : ".", W_OK);
2068 if (cp)
2069 *cp = '/';
2070 if (d < 0) {
2071 warn("local: %s", local);
2072 return (NULL);
2073 }
2074 len = strlcpy(new, local, sizeof(new));
2075 cp = &new[len];
2076 *cp++ = '.';
2077 while (!d) {
2078 if (++count == 100) {
2079 fputs("runique: can't find unique file name.\n",
2080 ttyout);
2081 return (NULL);
2082 }
2083 *cp++ = ext;
2084 *cp = '\0';
2085 if (ext == '9')
2086 ext = '0';
2087 else
2088 ext++;
2089 if ((d = access(new, F_OK)) < 0)
2090 break;
2091 if (ext != '0')
2092 cp--;
2093 else if (*(cp - 2) == '.')
2094 *(cp - 1) = '1';
2095 else {
2096 *(cp - 2) = *(cp - 2) + 1;
2097 cp--;
2098 }
2099 }
2100 return (new);
2101 }
2102
2103 /*
2104 * abort_squared --
2105 * aborts abort_remote(). lostpeer() is called because if the user is
2106 * too impatient to wait or there's another problem then ftp really
2107 * needs to get back to a known state.
2108 */
2109 void
2110 abort_squared(dummy)
2111 int dummy;
2112 {
2113 char msgbuf[100];
2114 int len;
2115
2116 alarmtimer(0);
2117 len = strlcpy(msgbuf, "\nremote abort aborted; closing connection.\n",
2118 sizeof(msgbuf));
2119 write(fileno(ttyout), msgbuf, len);
2120 lostpeer();
2121 siglongjmp(xferabort, 1);
2122 }
2123
2124 void
2125 abort_remote(din)
2126 FILE *din;
2127 {
2128 char buf[BUFSIZ];
2129 int nfnd;
2130
2131 if (cout == NULL) {
2132 warnx("Lost control connection for abort.");
2133 if (ptabflg)
2134 code = -1;
2135 lostpeer();
2136 return;
2137 }
2138 /*
2139 * send IAC in urgent mode instead of DM because 4.3BSD places oob mark
2140 * after urgent byte rather than before as is protocol now
2141 */
2142 buf[0] = IAC;
2143 buf[1] = IP;
2144 buf[2] = IAC;
2145 if (send(fileno(cout), buf, 3, MSG_OOB) != 3)
2146 warn("abort");
2147 fprintf(cout, "%cABOR\r\n", DM);
2148 (void)fflush(cout);
2149 if ((nfnd = empty(cin, din, 10)) <= 0) {
2150 if (nfnd < 0) {
2151 warn("abort");
2152 }
2153 if (ptabflg)
2154 code = -1;
2155 lostpeer();
2156 }
2157 if (din && (nfnd & 2)) {
2158 while (read(fileno(din), buf, BUFSIZ) > 0)
2159 continue;
2160 }
2161 if (getreply(0) == ERROR && code == 552) {
2162 /* 552 needed for nic style abort */
2163 (void)getreply(0);
2164 }
2165 (void)getreply(0);
2166 }
2167