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