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