ftp.c revision 1.58 1 /* $NetBSD: ftp.c,v 1.58 1999/08/29 22:21:57 christos 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.58 1999/08/29 22:21:57 christos 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 warn(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 reinit:
1351 if (passivemode) {
1352 data_addr = myctladdr;
1353 data = socket(data_addr.su_family, SOCK_STREAM, 0);
1354 if (data < 0) {
1355 warn("socket");
1356 return (1);
1357 }
1358 if ((options & SO_DEBUG) &&
1359 setsockopt(data, SOL_SOCKET, SO_DEBUG, (char *)&on,
1360 sizeof(on)) < 0)
1361 warn("setsockopt (ignored)");
1362 result = COMPLETE + 1;
1363 switch (data_addr.su_family) {
1364 case AF_INET:
1365 if (epsv4) {
1366 result = command(pasvcmd = "EPSV");
1367 /*
1368 * this code is to be friendly with broken
1369 * BSDI ftpd
1370 */
1371 if (code / 10 == 22 && code != 229) {
1372 fputs(
1373 "wrong server: return code must be 229\n",
1374 ttyout);
1375 result = COMPLETE + 1;
1376 }
1377 }
1378 if (result != COMPLETE)
1379 result = command(pasvcmd = "PASV");
1380 break;
1381 case AF_INET6:
1382 result = command(pasvcmd = "EPSV");
1383 /* this code is to be friendly with broken BSDI ftpd */
1384 if (code / 10 == 22 && code != 229) {
1385 fputs(
1386 "wrong server: return code must be 229\n",
1387 ttyout);
1388 result = COMPLETE + 1;
1389 }
1390 if (result != COMPLETE)
1391 result = command(pasvcmd = "LPSV");
1392 break;
1393 default:
1394 result = COMPLETE + 1;
1395 break;
1396 }
1397 if (result != COMPLETE) {
1398 if (activefallback) {
1399 (void)close(data);
1400 data = -1;
1401 passivemode = 0;
1402 activefallback = 0;
1403 goto reinit;
1404 }
1405 fputs("Passive mode refused.\n", ttyout);
1406 goto bad;
1407 }
1408
1409 #define pack2(var, off) \
1410 (((var[(off) + 0] & 0xff) << 8) | ((var[(off) + 1] & 0xff) << 0))
1411 #define pack4(var, off) \
1412 (((var[(off) + 0] & 0xff) << 24) | ((var[(off) + 1] & 0xff) << 16) | \
1413 ((var[(off) + 2] & 0xff) << 8) | ((var[(off) + 3] & 0xff) << 0))
1414
1415 /*
1416 * What we've got at this point is a string of comma separated
1417 * one-byte unsigned integer values, separated by commas.
1418 */
1419 if (strcmp(pasvcmd, "PASV") == 0) {
1420 if (data_addr.su_family != AF_INET) {
1421 fputs(
1422 "Passive mode AF mismatch. Shouldn't happen!\n", ttyout);
1423 error = 1;
1424 goto bad;
1425 }
1426 if (code / 10 == 22 && code != 227) {
1427 fputs("wrong server: return code must be 227\n",
1428 ttyout);
1429 error = 1;
1430 goto bad;
1431 }
1432 error = sscanf(pasv, "%u,%u,%u,%u,%u,%u",
1433 &addr[0], &addr[1], &addr[2], &addr[3],
1434 &port[0], &port[1]);
1435 if (error != 6) {
1436 fputs(
1437 "Passive mode address scan failure. Shouldn't happen!\n", ttyout);
1438 error = 1;
1439 goto bad;
1440 }
1441 error = 0;
1442 memset(&data_addr, 0, sizeof(data_addr));
1443 data_addr.su_family = AF_INET;
1444 data_addr.su_len = sizeof(struct sockaddr_in);
1445 data_addr.su_sin.sin_addr.s_addr =
1446 htonl(pack4(addr, 0));
1447 data_addr.su_port = htons(pack2(port, 0));
1448 } else if (strcmp(pasvcmd, "LPSV") == 0) {
1449 if (code / 10 == 22 && code != 228) {
1450 fputs("wrong server: return code must be 228\n",
1451 ttyout);
1452 error = 1;
1453 goto bad;
1454 }
1455 switch (data_addr.su_family) {
1456 case AF_INET:
1457 error = sscanf(pasv,
1458 "%u,%u,%u,%u,%u,%u,%u,%u,%u",
1459 &af, &hal,
1460 &addr[0], &addr[1], &addr[2], &addr[3],
1461 &pal, &port[0], &port[1]);
1462 if (error != 9) {
1463 fputs(
1464 "Passive mode address scan failure. Shouldn't happen!\n", ttyout);
1465 error = 1;
1466 goto bad;
1467 }
1468 if (af != 4 || hal != 4 || pal != 2) {
1469 fputs(
1470 "Passive mode AF mismatch. Shouldn't happen!\n", ttyout);
1471 error = 1;
1472 goto bad;
1473 }
1474
1475 error = 0;
1476 memset(&data_addr, 0, sizeof(data_addr));
1477 data_addr.su_family = AF_INET;
1478 data_addr.su_len = sizeof(struct sockaddr_in);
1479 data_addr.su_sin.sin_addr.s_addr =
1480 htonl(pack4(addr, 0));
1481 data_addr.su_port = htons(pack2(port, 0));
1482 break;
1483 case AF_INET6:
1484 error = sscanf(pasv,
1485 "%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u",
1486 &af, &hal,
1487 &addr[0], &addr[1], &addr[2], &addr[3],
1488 &addr[4], &addr[5], &addr[6], &addr[7],
1489 &addr[8], &addr[9], &addr[10],
1490 &addr[11], &addr[12], &addr[13],
1491 &addr[14], &addr[15],
1492 &pal, &port[0], &port[1]);
1493 if (error != 21) {
1494 fputs(
1495 "Passive mode address scan failure. Shouldn't happen!\n", ttyout);
1496 error = 1;
1497 goto bad;
1498 }
1499 if (af != 6 || hal != 16 || pal != 2) {
1500 fputs(
1501 "Passive mode AF mismatch. Shouldn't happen!\n", ttyout);
1502 error = 1;
1503 goto bad;
1504 }
1505
1506 error = 0;
1507 memset(&data_addr, 0, sizeof(data_addr));
1508 data_addr.su_family = AF_INET6;
1509 data_addr.su_len = sizeof(struct sockaddr_in6);
1510 data_addr.su_sin6.sin6_addr.s6_addr32[0] =
1511 htonl(pack4(addr, 0));
1512 data_addr.su_sin6.sin6_addr.s6_addr32[1] =
1513 htonl(pack4(addr, 4));
1514 data_addr.su_sin6.sin6_addr.s6_addr32[2] =
1515 htonl(pack4(addr, 8));
1516 data_addr.su_sin6.sin6_addr.s6_addr32[3] =
1517 htonl(pack4(addr, 12));
1518 data_addr.su_port = htons(pack2(port, 0));
1519 break;
1520 default:
1521 error = 1;
1522 }
1523 } else if (strcmp(pasvcmd, "EPSV") == 0) {
1524 char delim[4];
1525
1526 port[0] = 0;
1527 if (code / 10 == 22 && code != 229) {
1528 fputs("wrong server: return code must be 229\n",
1529 ttyout);
1530 error = 1;
1531 goto bad;
1532 }
1533 if (sscanf(pasv, "%c%c%c%d%c", &delim[0],
1534 &delim[1], &delim[2], &port[1],
1535 &delim[3]) != 5) {
1536 fputs("parse error!\n", ttyout);
1537 error = 1;
1538 goto bad;
1539 }
1540 if (delim[0] != delim[1] || delim[0] != delim[2]
1541 || delim[0] != delim[3]) {
1542 fputs("parse error!\n", ttyout);
1543 error = 1;
1544 goto bad;
1545 }
1546 data_addr = hisctladdr;
1547 data_addr.su_port = htons(port[1]);
1548 } else
1549 goto bad;
1550
1551 while (xconnect(data, (struct sockaddr *)&data_addr,
1552 data_addr.su_len) < 0) {
1553 if (errno == EINTR)
1554 continue;
1555 if (activefallback) {
1556 (void)close(data);
1557 data = -1;
1558 passivemode = 0;
1559 activefallback = 0;
1560 goto reinit;
1561 }
1562 warn("connect");
1563 goto bad;
1564 }
1565 #if defined(IPPROTO_IP) && defined(IP_TOS)
1566 if (data_addr.su_family == AF_INET) {
1567 on = IPTOS_THROUGHPUT;
1568 if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&on,
1569 sizeof(int)) < 0)
1570 warn("setsockopt TOS (ignored)");
1571 }
1572 #endif
1573 return (0);
1574 }
1575
1576 noport:
1577 data_addr = myctladdr;
1578 if (sendport)
1579 data_addr.su_port = 0; /* let system pick one */
1580 if (data != -1)
1581 (void)close(data);
1582 data = socket(data_addr.su_family, SOCK_STREAM, 0);
1583 if (data < 0) {
1584 warn("socket");
1585 if (tmpno)
1586 sendport = 1;
1587 return (1);
1588 }
1589 if (!sendport)
1590 if (setsockopt(data, SOL_SOCKET, SO_REUSEADDR, (char *)&on,
1591 sizeof(on)) < 0) {
1592 warn("setsockopt (reuse address)");
1593 goto bad;
1594 }
1595 if (bind(data, (struct sockaddr *)&data_addr, data_addr.su_len) < 0) {
1596 warn("bind");
1597 goto bad;
1598 }
1599 if (options & SO_DEBUG &&
1600 setsockopt(data, SOL_SOCKET, SO_DEBUG, (char *)&on,
1601 sizeof(on)) < 0)
1602 warn("setsockopt (ignored)");
1603 len = sizeof(data_addr);
1604 if (getsockname(data, (struct sockaddr *)&data_addr, &len) < 0) {
1605 warn("getsockname");
1606 goto bad;
1607 }
1608 if (xlisten(data, 1) < 0)
1609 warn("listen");
1610
1611 #define UC(b) (((int)b)&0xff)
1612
1613 if (sendport) {
1614 #ifdef INET6
1615 char hname[INET6_ADDRSTRLEN];
1616 int af;
1617 #endif
1618
1619 switch (data_addr.su_family) {
1620 case AF_INET:
1621 if (!epsv4) {
1622 result = COMPLETE + 1;
1623 break;
1624 }
1625 /* FALLTHROUGH */
1626 #ifdef INET6
1627 case AF_INET6:
1628 af = (data_addr.su_family == AF_INET) ? 1 : 2;
1629 if (getnameinfo((struct sockaddr *)&data_addr,
1630 data_addr.su_len, hname, sizeof(hname),
1631 NULL, 0, NI_NUMERICHOST)) {
1632 result = ERROR;
1633 } else {
1634 result = command("EPRT |%d|%s|%d|",
1635 af, hname, ntohs(data_addr.su_port));
1636 }
1637 break;
1638 #endif
1639 default:
1640 result = COMPLETE + 1;
1641 break;
1642 }
1643 if (result == COMPLETE)
1644 goto skip_port;
1645
1646 switch (data_addr.su_family) {
1647 case AF_INET:
1648 a = (char *)&data_addr.su_sin.sin_addr;
1649 p = (char *)&data_addr.su_port;
1650 result = command("PORT %d,%d,%d,%d,%d,%d",
1651 UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
1652 UC(p[0]), UC(p[1]));
1653 break;
1654 #ifdef INET6
1655 case AF_INET6:
1656 a = (char *)&data_addr.su_sin6.sin6_addr;
1657 p = (char *)&data_addr.su_port;
1658 result = command(
1659 "LPRT %d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
1660 6, 16,
1661 UC(a[0]),UC(a[1]),UC(a[2]),UC(a[3]),
1662 UC(a[4]),UC(a[5]),UC(a[6]),UC(a[7]),
1663 UC(a[8]),UC(a[9]),UC(a[10]),UC(a[11]),
1664 UC(a[12]),UC(a[13]),UC(a[14]),UC(a[15]),
1665 2, UC(p[0]), UC(p[1]));
1666 break;
1667 #endif
1668 default:
1669 result = COMPLETE + 1; /* xxx */
1670 }
1671 skip_port:
1672
1673 if (result == ERROR && sendport == -1) {
1674 sendport = 0;
1675 tmpno = 1;
1676 goto noport;
1677 }
1678 return (result != COMPLETE);
1679 }
1680 if (tmpno)
1681 sendport = 1;
1682 #if defined(IPPROTO_IP) && defined(IP_TOS)
1683 if (data_addr.su_family == AF_INET) {
1684 on = IPTOS_THROUGHPUT;
1685 if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&on,
1686 sizeof(int)) < 0)
1687 warn("setsockopt TOS (ignored)");
1688 }
1689 #endif
1690 return (0);
1691 bad:
1692 (void)close(data), data = -1;
1693 if (tmpno)
1694 sendport = 1;
1695 return (1);
1696 }
1697
1698 FILE *
1699 dataconn(lmode)
1700 const char *lmode;
1701 {
1702 union sockunion from;
1703 int s, fromlen = myctladdr.su_len;
1704
1705 if (passivemode)
1706 return (fdopen(data, lmode));
1707
1708 s = accept(data, (struct sockaddr *) &from, &fromlen);
1709 if (s < 0) {
1710 warn("accept");
1711 (void)close(data), data = -1;
1712 return (NULL);
1713 }
1714 (void)close(data);
1715 data = s;
1716 #if defined(IPPROTO_IP) && defined(IP_TOS)
1717 if (from.su_family == AF_INET) {
1718 int tos = IPTOS_THROUGHPUT;
1719 if (setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&tos,
1720 sizeof(int)) < 0) {
1721 warn("setsockopt TOS (ignored)");
1722 }
1723 }
1724 #endif
1725 return (fdopen(data, lmode));
1726 }
1727
1728 void
1729 psummary(notused)
1730 int notused;
1731 {
1732 int oerrno;
1733
1734 oerrno = errno;
1735 if (bytes > 0)
1736 ptransfer(1);
1737 errno = oerrno;
1738 }
1739
1740 void
1741 psabort(notused)
1742 int notused;
1743 {
1744
1745 alarmtimer(0);
1746 abrtflag++;
1747 }
1748
1749 void
1750 pswitch(flag)
1751 int flag;
1752 {
1753 sig_t oldintr;
1754 static struct comvars {
1755 int connect;
1756 char name[MAXHOSTNAMELEN];
1757 union sockunion mctl;
1758 union sockunion hctl;
1759 FILE *in;
1760 FILE *out;
1761 int tpe;
1762 int curtpe;
1763 int cpnd;
1764 int sunqe;
1765 int runqe;
1766 int mcse;
1767 int ntflg;
1768 char nti[17];
1769 char nto[17];
1770 int mapflg;
1771 char mi[MAXPATHLEN];
1772 char mo[MAXPATHLEN];
1773 } proxstruct, tmpstruct;
1774 struct comvars *ip, *op;
1775
1776 abrtflag = 0;
1777 oldintr = signal(SIGINT, psabort);
1778 if (flag) {
1779 if (proxy)
1780 return;
1781 ip = &tmpstruct;
1782 op = &proxstruct;
1783 proxy++;
1784 } else {
1785 if (!proxy)
1786 return;
1787 ip = &proxstruct;
1788 op = &tmpstruct;
1789 proxy = 0;
1790 }
1791 ip->connect = connected;
1792 connected = op->connect;
1793 if (hostname) {
1794 (void)strncpy(ip->name, hostname, sizeof(ip->name) - 1);
1795 ip->name[sizeof(ip->name) - 1] = '\0';
1796 } else
1797 ip->name[0] = '\0';
1798 hostname = op->name;
1799 ip->hctl = hisctladdr;
1800 hisctladdr = op->hctl;
1801 ip->mctl = myctladdr;
1802 myctladdr = op->mctl;
1803 ip->in = cin;
1804 cin = op->in;
1805 ip->out = cout;
1806 cout = op->out;
1807 ip->tpe = type;
1808 type = op->tpe;
1809 ip->curtpe = curtype;
1810 curtype = op->curtpe;
1811 ip->cpnd = cpend;
1812 cpend = op->cpnd;
1813 ip->sunqe = sunique;
1814 sunique = op->sunqe;
1815 ip->runqe = runique;
1816 runique = op->runqe;
1817 ip->mcse = mcase;
1818 mcase = op->mcse;
1819 ip->ntflg = ntflag;
1820 ntflag = op->ntflg;
1821 (void)strncpy(ip->nti, ntin, sizeof(ip->nti) - 1);
1822 (ip->nti)[sizeof(ip->nti) - 1] = '\0';
1823 (void)strcpy(ntin, op->nti);
1824 (void)strncpy(ip->nto, ntout, sizeof(ip->nto) - 1);
1825 (ip->nto)[sizeof(ip->nto) - 1] = '\0';
1826 (void)strcpy(ntout, op->nto);
1827 ip->mapflg = mapflag;
1828 mapflag = op->mapflg;
1829 (void)strncpy(ip->mi, mapin, sizeof(ip->mi) - 1);
1830 (ip->mi)[sizeof(ip->mi) - 1] = '\0';
1831 (void)strcpy(mapin, op->mi);
1832 (void)strncpy(ip->mo, mapout, sizeof(ip->mo) - 1);
1833 (ip->mo)[sizeof(ip->mo) - 1] = '\0';
1834 (void)strcpy(mapout, op->mo);
1835 (void)signal(SIGINT, oldintr);
1836 if (abrtflag) {
1837 abrtflag = 0;
1838 (*oldintr)(SIGINT);
1839 }
1840 }
1841
1842 void
1843 abortpt(notused)
1844 int notused;
1845 {
1846
1847 alarmtimer(0);
1848 putc('\n', ttyout);
1849 ptabflg++;
1850 mflag = 0;
1851 abrtflag = 0;
1852 longjmp(ptabort, 1);
1853 }
1854
1855 void
1856 proxtrans(cmd, local, remote)
1857 const char *cmd, *local, *remote;
1858 {
1859 sig_t oldintr;
1860 int prox_type, nfnd;
1861 volatile int secndflag;
1862 char *cmd2;
1863
1864 #ifdef __GNUC__ /* to shut up gcc warnings */
1865 (void)&oldintr;
1866 (void)&cmd2;
1867 #endif
1868
1869 oldintr = NULL;
1870 secndflag = 0;
1871 if (strcmp(cmd, "RETR"))
1872 cmd2 = "RETR";
1873 else
1874 cmd2 = runique ? "STOU" : "STOR";
1875 if ((prox_type = type) == 0) {
1876 if (unix_server && unix_proxy)
1877 prox_type = TYPE_I;
1878 else
1879 prox_type = TYPE_A;
1880 }
1881 if (curtype != prox_type)
1882 changetype(prox_type, 1);
1883 if (command("PASV") != COMPLETE) {
1884 fputs("proxy server does not support third party transfers.\n",
1885 ttyout);
1886 return;
1887 }
1888 pswitch(0);
1889 if (!connected) {
1890 fputs("No primary connection.\n", ttyout);
1891 pswitch(1);
1892 code = -1;
1893 return;
1894 }
1895 if (curtype != prox_type)
1896 changetype(prox_type, 1);
1897 if (command("PORT %s", pasv) != COMPLETE) {
1898 pswitch(1);
1899 return;
1900 }
1901 if (setjmp(ptabort))
1902 goto abort;
1903 oldintr = signal(SIGINT, abortpt);
1904 if ((restart_point &&
1905 #ifndef NO_QUAD
1906 (command("REST %qd", (long long) restart_point) != CONTINUE)
1907 #else
1908 (command("REST %ld", (long) restart_point) != CONTINUE)
1909 #endif
1910 ) || (command("%s %s", cmd, remote) != PRELIM)) {
1911 (void)signal(SIGINT, oldintr);
1912 pswitch(1);
1913 return;
1914 }
1915 sleep(2);
1916 pswitch(1);
1917 secndflag++;
1918 if ((restart_point &&
1919 #ifndef NO_QUAD
1920 (command("REST %qd", (long long) restart_point) != CONTINUE)
1921 #else
1922 (command("REST %ld", (long) restart_point) != CONTINUE)
1923 #endif
1924 ) || (command("%s %s", cmd2, local) != PRELIM))
1925 goto abort;
1926 ptflag++;
1927 (void)getreply(0);
1928 pswitch(0);
1929 (void)getreply(0);
1930 (void)signal(SIGINT, oldintr);
1931 pswitch(1);
1932 ptflag = 0;
1933 fprintf(ttyout, "local: %s remote: %s\n", local, remote);
1934 return;
1935 abort:
1936 (void)signal(SIGINT, SIG_IGN);
1937 ptflag = 0;
1938 if (strcmp(cmd, "RETR") && !proxy)
1939 pswitch(1);
1940 else if (!strcmp(cmd, "RETR") && proxy)
1941 pswitch(0);
1942 if (!cpend && !secndflag) { /* only here if cmd = "STOR" (proxy=1) */
1943 if (command("%s %s", cmd2, local) != PRELIM) {
1944 pswitch(0);
1945 if (cpend)
1946 abort_remote(NULL);
1947 }
1948 pswitch(1);
1949 if (ptabflg)
1950 code = -1;
1951 (void)signal(SIGINT, oldintr);
1952 return;
1953 }
1954 if (cpend)
1955 abort_remote(NULL);
1956 pswitch(!proxy);
1957 if (!cpend && !secndflag) { /* only if cmd = "RETR" (proxy=1) */
1958 if (command("%s %s", cmd2, local) != PRELIM) {
1959 pswitch(0);
1960 if (cpend)
1961 abort_remote(NULL);
1962 pswitch(1);
1963 if (ptabflg)
1964 code = -1;
1965 (void)signal(SIGINT, oldintr);
1966 return;
1967 }
1968 }
1969 if (cpend)
1970 abort_remote(NULL);
1971 pswitch(!proxy);
1972 if (cpend) {
1973 if ((nfnd = empty(cin, NULL, 10)) <= 0) {
1974 if (nfnd < 0) {
1975 warn("abort");
1976 }
1977 if (ptabflg)
1978 code = -1;
1979 lostpeer();
1980 }
1981 (void)getreply(0);
1982 (void)getreply(0);
1983 }
1984 if (proxy)
1985 pswitch(0);
1986 pswitch(1);
1987 if (ptabflg)
1988 code = -1;
1989 (void)signal(SIGINT, oldintr);
1990 }
1991
1992 void
1993 reset(argc, argv)
1994 int argc;
1995 char *argv[];
1996 {
1997 int nfnd = 1;
1998
1999 while (nfnd > 0) {
2000 if ((nfnd = empty(cin, NULL, 0)) < 0) {
2001 warn("reset");
2002 code = -1;
2003 lostpeer();
2004 }
2005 else if (nfnd) {
2006 (void)getreply(0);
2007 }
2008 }
2009 }
2010
2011 char *
2012 gunique(local)
2013 const char *local;
2014 {
2015 static char new[MAXPATHLEN];
2016 char *cp = strrchr(local, '/');
2017 int d, count=0;
2018 char ext = '1';
2019
2020 if (cp)
2021 *cp = '\0';
2022 d = access(cp == local ? "/" : cp ? local : ".", W_OK);
2023 if (cp)
2024 *cp = '/';
2025 if (d < 0) {
2026 warn("local: %s", local);
2027 return (NULL);
2028 }
2029 (void)strcpy(new, local);
2030 cp = new + strlen(new);
2031 *cp++ = '.';
2032 while (!d) {
2033 if (++count == 100) {
2034 fputs("runique: can't find unique file name.\n",
2035 ttyout);
2036 return (NULL);
2037 }
2038 *cp++ = ext;
2039 *cp = '\0';
2040 if (ext == '9')
2041 ext = '0';
2042 else
2043 ext++;
2044 if ((d = access(new, F_OK)) < 0)
2045 break;
2046 if (ext != '0')
2047 cp--;
2048 else if (*(cp - 2) == '.')
2049 *(cp - 1) = '1';
2050 else {
2051 *(cp - 2) = *(cp - 2) + 1;
2052 cp--;
2053 }
2054 }
2055 return (new);
2056 }
2057
2058 void
2059 abort_remote(din)
2060 FILE *din;
2061 {
2062 char buf[BUFSIZ];
2063 int nfnd;
2064
2065 if (cout == NULL) {
2066 warnx("Lost control connection for abort.");
2067 if (ptabflg)
2068 code = -1;
2069 lostpeer();
2070 return;
2071 }
2072 /*
2073 * send IAC in urgent mode instead of DM because 4.3BSD places oob mark
2074 * after urgent byte rather than before as is protocol now
2075 */
2076 snprintf(buf, sizeof(buf), "%c%c%c", IAC, IP, IAC);
2077 if (send(fileno(cout), buf, 3, MSG_OOB) != 3)
2078 warn("abort");
2079 fprintf(cout, "%cABOR\r\n", DM);
2080 (void)fflush(cout);
2081 if ((nfnd = empty(cin, din, 10)) <= 0) {
2082 if (nfnd < 0) {
2083 warn("abort");
2084 }
2085 if (ptabflg)
2086 code = -1;
2087 lostpeer();
2088 }
2089 if (din && (nfnd & 2)) {
2090 while (read(fileno(din), buf, BUFSIZ) > 0)
2091 continue;
2092 }
2093 if (getreply(0) == ERROR && code == 552) {
2094 /* 552 needed for nic style abort */
2095 (void)getreply(0);
2096 }
2097 (void)getreply(0);
2098 }
2099