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