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