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