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