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