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