Annotation of src/usr.bin/ftp/ftp.c, Revision 1.28
1.28 ! millert 1: /* $OpenBSD: ftp.c,v 1.27 1998/05/13 10:42:35 deraadt Exp $ */
1.22 millert 2: /* $NetBSD: ftp.c,v 1.27 1997/08/18 10:20:23 lukem Exp $ */
1.1 deraadt 3:
4: /*
5: * Copyright (c) 1985, 1989, 1993, 1994
6: * The Regents of the University of California. 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. All advertising materials mentioning features or use of this software
17: * must display the following acknowledgement:
18: * This product includes software developed by the University of
19: * California, Berkeley and its contributors.
20: * 4. Neither the name of the University nor the names of its contributors
21: * may be used to endorse or promote products derived from this software
22: * without specific prior written permission.
23: *
24: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34: * SUCH DAMAGE.
35: */
36:
37: #ifndef lint
38: #if 0
39: static char sccsid[] = "@(#)ftp.c 8.6 (Berkeley) 10/27/94";
40: #else
1.28 ! millert 41: static char rcsid[] = "$OpenBSD: ftp.c,v 1.27 1998/05/13 10:42:35 deraadt Exp $";
1.1 deraadt 42: #endif
43: #endif /* not lint */
44:
1.10 millert 45: #include <sys/types.h>
1.1 deraadt 46: #include <sys/stat.h>
47: #include <sys/socket.h>
48:
49: #include <netinet/in.h>
50: #include <netinet/in_systm.h>
51: #include <netinet/ip.h>
52: #include <arpa/inet.h>
53: #include <arpa/ftp.h>
54: #include <arpa/telnet.h>
55:
56: #include <ctype.h>
57: #include <err.h>
58: #include <errno.h>
59: #include <netdb.h>
60: #include <stdio.h>
61: #include <stdlib.h>
62: #include <string.h>
63: #include <unistd.h>
1.12 millert 64: #include <utime.h>
1.13 millert 65: #ifdef __STDC__
66: #include <stdarg.h>
67: #else
1.1 deraadt 68: #include <varargs.h>
1.13 millert 69: #endif
1.1 deraadt 70:
71: #include "ftp_var.h"
72:
73: struct sockaddr_in hisctladdr;
74: struct sockaddr_in data_addr;
75: int data = -1;
76: int abrtflag = 0;
77: jmp_buf ptabort;
78: int ptabflg;
79: int ptflag = 0;
80: struct sockaddr_in myctladdr;
81: off_t restart_point = 0;
82:
83:
84: FILE *cin, *cout;
85:
86: char *
87: hookup(host, port)
1.10 millert 88: const char *host;
1.20 millert 89: in_port_t port;
1.1 deraadt 90: {
1.20 millert 91: struct hostent *hp = NULL;
1.1 deraadt 92: int s, len, tos;
1.10 millert 93: static char hostnamebuf[MAXHOSTNAMELEN];
1.1 deraadt 94:
1.12 millert 95: memset((void *)&hisctladdr, 0, sizeof(hisctladdr));
1.1 deraadt 96: if (inet_aton(host, &hisctladdr.sin_addr) != 0) {
97: hisctladdr.sin_family = AF_INET;
1.11 millert 98: (void)strncpy(hostnamebuf, host, sizeof(hostnamebuf) - 1);
99: hostnamebuf[sizeof(hostnamebuf) - 1] = '\0';
1.1 deraadt 100: } else {
101: hp = gethostbyname(host);
102: if (hp == NULL) {
103: warnx("%s: %s", host, hstrerror(h_errno));
104: code = -1;
105: return ((char *) 0);
106: }
107: hisctladdr.sin_family = hp->h_addrtype;
1.20 millert 108: memcpy(&hisctladdr.sin_addr, hp->h_addr_list[0],
109: (size_t)hp->h_length);
1.11 millert 110: (void)strncpy(hostnamebuf, hp->h_name, sizeof(hostnamebuf) - 1);
111: hostnamebuf[sizeof(hostnamebuf) - 1] = '\0';
1.1 deraadt 112: }
113: hostname = hostnamebuf;
114: s = socket(hisctladdr.sin_family, SOCK_STREAM, 0);
115: if (s < 0) {
116: warn("socket");
117: code = -1;
118: return (0);
119: }
120: hisctladdr.sin_port = port;
1.10 millert 121: while (connect(s, (struct sockaddr *)&hisctladdr,
1.11 millert 122: sizeof(hisctladdr)) < 0) {
1.27 deraadt 123: if (errno == EINTR)
124: continue;
1.1 deraadt 125: if (hp && hp->h_addr_list[1]) {
126: int oerrno = errno;
127: char *ia;
128:
129: ia = inet_ntoa(hisctladdr.sin_addr);
130: errno = oerrno;
131: warn("connect to address %s", ia);
132: hp->h_addr_list++;
1.11 millert 133: memcpy(&hisctladdr.sin_addr, hp->h_addr_list[0],
1.20 millert 134: (size_t)hp->h_length);
1.27 deraadt 135: fprintf(ttyout, "Trying %s...\n",
136: inet_ntoa(hisctladdr.sin_addr));
1.11 millert 137: (void)close(s);
1.1 deraadt 138: s = socket(hisctladdr.sin_family, SOCK_STREAM, 0);
139: if (s < 0) {
140: warn("socket");
141: code = -1;
142: return (0);
143: }
144: continue;
145: }
146: warn("connect");
147: code = -1;
148: goto bad;
149: }
1.11 millert 150: len = sizeof(myctladdr);
1.1 deraadt 151: if (getsockname(s, (struct sockaddr *)&myctladdr, &len) < 0) {
152: warn("getsockname");
153: code = -1;
154: goto bad;
155: }
156: #ifdef IP_TOS
157: tos = IPTOS_LOWDELAY;
158: if (setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0)
159: warn("setsockopt TOS (ignored)");
160: #endif
161: cin = fdopen(s, "r");
162: cout = fdopen(s, "w");
163: if (cin == NULL || cout == NULL) {
164: warnx("fdopen failed.");
165: if (cin)
1.11 millert 166: (void)fclose(cin);
1.1 deraadt 167: if (cout)
1.11 millert 168: (void)fclose(cout);
1.1 deraadt 169: code = -1;
170: goto bad;
171: }
172: if (verbose)
1.18 deraadt 173: fprintf(ttyout, "Connected to %s.\n", hostname);
1.1 deraadt 174: if (getreply(0) > 2) { /* read startup message from server */
175: if (cin)
1.11 millert 176: (void)fclose(cin);
1.1 deraadt 177: if (cout)
1.11 millert 178: (void)fclose(cout);
1.1 deraadt 179: code = -1;
180: goto bad;
181: }
182: #ifdef SO_OOBINLINE
183: {
184: int on = 1;
185:
186: if (setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (char *)&on, sizeof(on))
187: < 0 && debug) {
188: warn("setsockopt");
189: }
190: }
191: #endif /* SO_OOBINLINE */
192:
193: return (hostname);
194: bad:
1.11 millert 195: (void)close(s);
1.1 deraadt 196: return ((char *)0);
197: }
198:
199: void
1.12 millert 200: cmdabort(notused)
201: int notused;
1.1 deraadt 202: {
203:
1.10 millert 204: alarmtimer(0);
1.18 deraadt 205: putc('\n', ttyout);
206: (void)fflush(ttyout);
1.1 deraadt 207: abrtflag++;
208: if (ptflag)
1.10 millert 209: longjmp(ptabort, 1);
1.1 deraadt 210: }
211:
212: /*VARARGS*/
213: int
1.13 millert 214: #ifdef __STDC__
215: command(const char *fmt, ...)
216: #else
1.1 deraadt 217: command(va_alist)
1.13 millert 218: va_dcl
219: #endif
1.1 deraadt 220: {
221: va_list ap;
222: int r;
223: sig_t oldintr;
1.13 millert 224: #ifndef __STDC__
225: const char *fmt;
226: #endif
1.1 deraadt 227:
228: abrtflag = 0;
229: if (debug) {
1.18 deraadt 230: fputs("---> ", ttyout);
1.13 millert 231: #ifdef __STDC__
232: va_start(ap, fmt);
233: #else
1.1 deraadt 234: va_start(ap);
1.13 millert 235: fmt = va_arg(ap, const char *);
236: #endif
1.1 deraadt 237: if (strncmp("PASS ", fmt, 5) == 0)
1.18 deraadt 238: fputs("PASS XXXX", ttyout);
1.10 millert 239: else if (strncmp("ACCT ", fmt, 5) == 0)
1.18 deraadt 240: fputs("ACCT XXXX", ttyout);
1.10 millert 241: else
1.28 ! millert 242: vfprintf(ttyout, fmt, ap);
1.1 deraadt 243: va_end(ap);
1.18 deraadt 244: putc('\n', ttyout);
245: (void)fflush(ttyout);
1.1 deraadt 246: }
247: if (cout == NULL) {
1.13 millert 248: warnx("No control connection for command.");
1.1 deraadt 249: code = -1;
250: return (0);
251: }
252: oldintr = signal(SIGINT, cmdabort);
1.13 millert 253: #ifdef __STDC__
254: va_start(ap, fmt);
255: #else
1.1 deraadt 256: va_start(ap);
257: fmt = va_arg(ap, char *);
1.13 millert 258: #endif
1.1 deraadt 259: vfprintf(cout, fmt, ap);
260: va_end(ap);
1.11 millert 261: fputs("\r\n", cout);
262: (void)fflush(cout);
1.1 deraadt 263: cpend = 1;
264: r = getreply(!strcmp(fmt, "QUIT"));
265: if (abrtflag && oldintr != SIG_IGN)
266: (*oldintr)(SIGINT);
1.11 millert 267: (void)signal(SIGINT, oldintr);
1.1 deraadt 268: return (r);
269: }
270:
1.10 millert 271: char reply_string[BUFSIZ]; /* first line of previous reply */
1.1 deraadt 272:
273: int
274: getreply(expecteof)
275: int expecteof;
276: {
1.10 millert 277: char current_line[BUFSIZ]; /* last line of previous reply */
278: int c, n, line;
1.1 deraadt 279: int dig;
280: int originalcode = 0, continuation = 0;
281: sig_t oldintr;
282: int pflag = 0;
283: char *cp, *pt = pasv;
284:
1.25 weingart 285: memset(current_line, 0, sizeof(current_line));
1.1 deraadt 286: oldintr = signal(SIGINT, cmdabort);
1.10 millert 287: for (line = 0 ;; line++) {
1.1 deraadt 288: dig = n = code = 0;
1.10 millert 289: cp = current_line;
1.23 millert 290: while ((c = fgetc(cin)) != '\n') {
1.1 deraadt 291: if (c == IAC) { /* handle telnet commands */
1.23 millert 292: switch (c = fgetc(cin)) {
1.1 deraadt 293: case WILL:
294: case WONT:
1.23 millert 295: c = fgetc(cin);
1.1 deraadt 296: fprintf(cout, "%c%c%c", IAC, DONT, c);
1.11 millert 297: (void)fflush(cout);
1.1 deraadt 298: break;
299: case DO:
300: case DONT:
1.23 millert 301: c = fgetc(cin);
1.1 deraadt 302: fprintf(cout, "%c%c%c", IAC, WONT, c);
1.11 millert 303: (void)fflush(cout);
1.1 deraadt 304: break;
305: default:
306: break;
307: }
308: continue;
309: }
310: dig++;
311: if (c == EOF) {
312: if (expecteof) {
1.11 millert 313: (void)signal(SIGINT, oldintr);
1.1 deraadt 314: code = 221;
315: return (0);
316: }
317: lostpeer();
318: if (verbose) {
1.18 deraadt 319: fputs(
320: "421 Service not available, remote server has closed connection.\n", ttyout);
321: (void)fflush(ttyout);
1.1 deraadt 322: }
323: code = 421;
324: return (4);
325: }
1.10 millert 326: if (c != '\r' && (verbose > 0 ||
1.21 mickey 327: ((verbose > -1 && n == '5' && dig > 4)) &&
328: (((!n && c < '5') || (n && n < '5'))
329: || !retry_connect))) {
1.1 deraadt 330: if (proxflag &&
1.10 millert 331: (dig == 1 || (dig == 5 && verbose == 0)))
1.18 deraadt 332: fprintf(ttyout, "%s:", hostname);
333: (void)putc(c, ttyout);
1.1 deraadt 334: }
335: if (dig < 4 && isdigit(c))
336: code = code * 10 + (c - '0');
337: if (!pflag && code == 227)
338: pflag = 1;
339: if (dig > 4 && pflag == 1 && isdigit(c))
340: pflag = 2;
341: if (pflag == 2) {
342: if (c != '\r' && c != ')')
343: *pt++ = c;
344: else {
345: *pt = '\0';
346: pflag = 3;
347: }
348: }
349: if (dig == 4 && c == '-') {
350: if (continuation)
351: code = 0;
352: continuation++;
353: }
1.10 millert 354: if (n == 0)
355: n = c;
356: if (cp < ¤t_line[sizeof(current_line) - 1])
1.1 deraadt 357: *cp++ = c;
358: }
1.11 millert 359: if (verbose > 0 || ((verbose > -1 && n == '5') &&
360: (n < '5' || !retry_connect))) {
1.18 deraadt 361: (void)putc(c, ttyout);
362: (void)fflush (ttyout);
1.1 deraadt 363: }
1.10 millert 364: if (line == 0) {
365: size_t len = cp - current_line;
366:
367: if (len > sizeof(reply_string))
368: len = sizeof(reply_string);
369:
1.11 millert 370: (void)strncpy(reply_string, current_line, len);
1.10 millert 371: reply_string[len] = '\0';
372: }
1.1 deraadt 373: if (continuation && code != originalcode) {
374: if (originalcode == 0)
375: originalcode = code;
376: continue;
377: }
378: *cp = '\0';
379: if (n != '1')
380: cpend = 0;
1.11 millert 381: (void)signal(SIGINT, oldintr);
1.1 deraadt 382: if (code == 421 || originalcode == 421)
383: lostpeer();
384: if (abrtflag && oldintr != cmdabort && oldintr != SIG_IGN)
385: (*oldintr)(SIGINT);
386: return (n - '0');
387: }
388: }
389:
390: int
391: empty(mask, sec)
392: struct fd_set *mask;
393: int sec;
394: {
395: struct timeval t;
396:
397: t.tv_sec = (long) sec;
398: t.tv_usec = 0;
399: return (select(32, mask, (struct fd_set *) 0, (struct fd_set *) 0, &t));
400: }
401:
402: jmp_buf sendabort;
403:
404: void
1.12 millert 405: abortsend(notused)
406: int notused;
1.1 deraadt 407: {
408:
1.10 millert 409: alarmtimer(0);
1.1 deraadt 410: mflag = 0;
411: abrtflag = 0;
1.18 deraadt 412: fputs("\nsend aborted\nwaiting for remote to finish abort.\n", ttyout);
413: (void)fflush(ttyout);
1.1 deraadt 414: longjmp(sendabort, 1);
415: }
416:
417: void
418: sendrequest(cmd, local, remote, printnames)
1.10 millert 419: const char *cmd, *local, *remote;
1.1 deraadt 420: int printnames;
421: {
422: struct stat st;
423: int c, d;
1.20 millert 424: FILE *fin, *dout;
1.1 deraadt 425: int (*closefunc) __P((FILE *));
1.10 millert 426: sig_t oldinti, oldintr, oldintp;
1.20 millert 427: volatile off_t hashbytes;
1.1 deraadt 428: char *lmode, buf[BUFSIZ], *bufp;
1.13 millert 429: int oprogress;
1.1 deraadt 430:
1.20 millert 431: #ifdef __GNUC__ /* XXX: to shut up gcc warnings */
432: (void)&fin;
433: (void)&dout;
434: (void)&closefunc;
435: (void)&oldinti;
436: (void)&oldintr;
437: (void)&oldintp;
438: (void)&lmode;
439: #endif
440:
1.10 millert 441: hashbytes = mark;
442: direction = "sent";
1.20 millert 443: dout = NULL;
1.10 millert 444: bytes = 0;
445: filesize = -1;
1.13 millert 446: oprogress = progress;
1.1 deraadt 447: if (verbose && printnames) {
448: if (local && *local != '-')
1.18 deraadt 449: fprintf(ttyout, "local: %s ", local);
1.1 deraadt 450: if (remote)
1.18 deraadt 451: fprintf(ttyout, "remote: %s\n", remote);
1.1 deraadt 452: }
453: if (proxy) {
454: proxtrans(cmd, local, remote);
455: return;
456: }
457: if (curtype != type)
458: changetype(type, 0);
459: closefunc = NULL;
460: oldintr = NULL;
461: oldintp = NULL;
1.10 millert 462: oldinti = NULL;
1.1 deraadt 463: lmode = "w";
464: if (setjmp(sendabort)) {
465: while (cpend) {
1.11 millert 466: (void)getreply(0);
1.1 deraadt 467: }
468: if (data >= 0) {
1.11 millert 469: (void)close(data);
1.1 deraadt 470: data = -1;
471: }
472: if (oldintr)
1.11 millert 473: (void)signal(SIGINT, oldintr);
1.1 deraadt 474: if (oldintp)
1.11 millert 475: (void)signal(SIGPIPE, oldintp);
1.10 millert 476: if (oldinti)
1.11 millert 477: (void)signal(SIGINFO, oldinti);
1.13 millert 478: progress = oprogress;
1.1 deraadt 479: code = -1;
480: return;
481: }
482: oldintr = signal(SIGINT, abortsend);
1.10 millert 483: oldinti = signal(SIGINFO, psummary);
1.13 millert 484: if (strcmp(local, "-") == 0) {
1.1 deraadt 485: fin = stdin;
1.13 millert 486: progress = 0;
487: } else if (*local == '|') {
1.10 millert 488: oldintp = signal(SIGPIPE, SIG_IGN);
1.1 deraadt 489: fin = popen(local + 1, "r");
490: if (fin == NULL) {
491: warn("%s", local + 1);
1.11 millert 492: (void)signal(SIGINT, oldintr);
493: (void)signal(SIGPIPE, oldintp);
494: (void)signal(SIGINFO, oldinti);
1.1 deraadt 495: code = -1;
496: return;
497: }
1.13 millert 498: progress = 0;
1.1 deraadt 499: closefunc = pclose;
500: } else {
501: fin = fopen(local, "r");
502: if (fin == NULL) {
503: warn("local: %s", local);
1.11 millert 504: (void)signal(SIGINT, oldintr);
505: (void)signal(SIGINFO, oldinti);
1.1 deraadt 506: code = -1;
507: return;
508: }
509: closefunc = fclose;
510: if (fstat(fileno(fin), &st) < 0 ||
1.14 millert 511: (st.st_mode & S_IFMT) != S_IFREG) {
1.18 deraadt 512: fprintf(ttyout, "%s: not a plain file.\n", local);
1.11 millert 513: (void)signal(SIGINT, oldintr);
514: (void)signal(SIGINFO, oldinti);
1.1 deraadt 515: fclose(fin);
516: code = -1;
517: return;
518: }
1.10 millert 519: filesize = st.st_size;
1.1 deraadt 520: }
521: if (initconn()) {
1.11 millert 522: (void)signal(SIGINT, oldintr);
523: (void)signal(SIGINFO, oldinti);
1.1 deraadt 524: if (oldintp)
1.11 millert 525: (void)signal(SIGPIPE, oldintp);
1.1 deraadt 526: code = -1;
1.13 millert 527: progress = oprogress;
1.1 deraadt 528: if (closefunc != NULL)
529: (*closefunc)(fin);
530: return;
531: }
532: if (setjmp(sendabort))
533: goto abort;
534:
535: if (restart_point &&
536: (strcmp(cmd, "STOR") == 0 || strcmp(cmd, "APPE") == 0)) {
537: int rc;
538:
1.10 millert 539: rc = -1;
1.1 deraadt 540: switch (curtype) {
541: case TYPE_A:
542: rc = fseek(fin, (long) restart_point, SEEK_SET);
543: break;
544: case TYPE_I:
545: case TYPE_L:
546: rc = lseek(fileno(fin), restart_point, SEEK_SET);
547: break;
548: }
549: if (rc < 0) {
550: warn("local: %s", local);
551: restart_point = 0;
1.13 millert 552: progress = oprogress;
1.1 deraadt 553: if (closefunc != NULL)
554: (*closefunc)(fin);
555: return;
556: }
557: if (command("REST %ld", (long) restart_point)
558: != CONTINUE) {
559: restart_point = 0;
1.13 millert 560: progress = oprogress;
1.1 deraadt 561: if (closefunc != NULL)
562: (*closefunc)(fin);
563: return;
564: }
565: restart_point = 0;
566: lmode = "r+w";
567: }
568: if (remote) {
569: if (command("%s %s", cmd, remote) != PRELIM) {
1.11 millert 570: (void)signal(SIGINT, oldintr);
571: (void)signal(SIGINFO, oldinti);
1.13 millert 572: progress = oprogress;
1.1 deraadt 573: if (oldintp)
1.11 millert 574: (void)signal(SIGPIPE, oldintp);
1.1 deraadt 575: if (closefunc != NULL)
576: (*closefunc)(fin);
577: return;
578: }
579: } else
580: if (command("%s", cmd) != PRELIM) {
1.11 millert 581: (void)signal(SIGINT, oldintr);
582: (void)signal(SIGINFO, oldinti);
1.13 millert 583: progress = oprogress;
1.1 deraadt 584: if (oldintp)
1.11 millert 585: (void)signal(SIGPIPE, oldintp);
1.1 deraadt 586: if (closefunc != NULL)
587: (*closefunc)(fin);
588: return;
589: }
590: dout = dataconn(lmode);
591: if (dout == NULL)
592: goto abort;
1.10 millert 593: progressmeter(-1);
1.1 deraadt 594: oldintp = signal(SIGPIPE, SIG_IGN);
595: switch (curtype) {
596:
597: case TYPE_I:
598: case TYPE_L:
599: errno = d = 0;
1.11 millert 600: while ((c = read(fileno(fin), buf, sizeof(buf))) > 0) {
1.1 deraadt 601: bytes += c;
602: for (bufp = buf; c > 0; c -= d, bufp += d)
1.20 millert 603: if ((d = write(fileno(dout), bufp, (size_t)c))
604: <= 0)
1.1 deraadt 605: break;
1.10 millert 606: if (hash && (!progress || filesize < 0) ) {
1.1 deraadt 607: while (bytes >= hashbytes) {
1.18 deraadt 608: (void)putc('#', ttyout);
1.7 kstailey 609: hashbytes += mark;
1.1 deraadt 610: }
1.18 deraadt 611: (void)fflush(ttyout);
1.1 deraadt 612: }
613: }
1.10 millert 614: if (hash && (!progress || filesize < 0) && bytes > 0) {
1.7 kstailey 615: if (bytes < mark)
1.18 deraadt 616: (void)putc('#', ttyout);
617: (void)putc('\n', ttyout);
618: (void)fflush(ttyout);
1.1 deraadt 619: }
620: if (c < 0)
621: warn("local: %s", local);
622: if (d < 0) {
1.10 millert 623: if (errno != EPIPE)
1.1 deraadt 624: warn("netout");
625: bytes = -1;
626: }
627: break;
628:
629: case TYPE_A:
1.23 millert 630: while ((c = fgetc(fin)) != EOF) {
1.1 deraadt 631: if (c == '\n') {
1.10 millert 632: while (hash && (!progress || filesize < 0) &&
633: (bytes >= hashbytes)) {
1.18 deraadt 634: (void)putc('#', ttyout);
635: (void)fflush(ttyout);
1.7 kstailey 636: hashbytes += mark;
1.1 deraadt 637: }
638: if (ferror(dout))
639: break;
1.11 millert 640: (void)putc('\r', dout);
1.1 deraadt 641: bytes++;
642: }
1.11 millert 643: (void)putc(c, dout);
1.1 deraadt 644: bytes++;
1.10 millert 645: #if 0 /* this violates RFC */
646: if (c == '\r') {
647: (void)putc('\0', dout);
648: bytes++;
649: }
650: #endif
1.1 deraadt 651: }
1.10 millert 652: if (hash && (!progress || filesize < 0)) {
1.1 deraadt 653: if (bytes < hashbytes)
1.18 deraadt 654: (void)putc('#', ttyout);
655: (void)putc('\n', ttyout);
656: (void)fflush(ttyout);
1.1 deraadt 657: }
658: if (ferror(fin))
659: warn("local: %s", local);
660: if (ferror(dout)) {
661: if (errno != EPIPE)
662: warn("netout");
663: bytes = -1;
664: }
665: break;
666: }
1.10 millert 667: progressmeter(1);
1.13 millert 668: progress = oprogress;
1.1 deraadt 669: if (closefunc != NULL)
670: (*closefunc)(fin);
1.11 millert 671: (void)fclose(dout);
672: (void)getreply(0);
673: (void)signal(SIGINT, oldintr);
674: (void)signal(SIGINFO, oldinti);
1.1 deraadt 675: if (oldintp)
1.11 millert 676: (void)signal(SIGPIPE, oldintp);
1.1 deraadt 677: if (bytes > 0)
1.10 millert 678: ptransfer(0);
1.1 deraadt 679: return;
680: abort:
1.11 millert 681: (void)signal(SIGINT, oldintr);
682: (void)signal(SIGINFO, oldinti);
1.13 millert 683: progress = oprogress;
1.1 deraadt 684: if (oldintp)
1.11 millert 685: (void)signal(SIGPIPE, oldintp);
1.1 deraadt 686: if (!cpend) {
687: code = -1;
688: return;
689: }
690: if (data >= 0) {
1.11 millert 691: (void)close(data);
1.1 deraadt 692: data = -1;
693: }
694: if (dout)
1.11 millert 695: (void)fclose(dout);
696: (void)getreply(0);
1.1 deraadt 697: code = -1;
698: if (closefunc != NULL && fin != NULL)
699: (*closefunc)(fin);
700: if (bytes > 0)
1.10 millert 701: ptransfer(0);
1.1 deraadt 702: }
703:
704: jmp_buf recvabort;
705:
706: void
1.12 millert 707: abortrecv(notused)
708: int notused;
1.1 deraadt 709: {
710:
1.10 millert 711: alarmtimer(0);
1.1 deraadt 712: mflag = 0;
713: abrtflag = 0;
1.18 deraadt 714: fputs("\nreceive aborted\nwaiting for remote to finish abort.\n", ttyout);
715: (void)fflush(ttyout);
1.1 deraadt 716: longjmp(recvabort, 1);
717: }
718:
719: void
1.22 millert 720: recvrequest(cmd, local, remote, lmode, printnames, ignorespecial)
1.10 millert 721: const char *cmd, *local, *remote, *lmode;
1.22 millert 722: int printnames, ignorespecial;
1.1 deraadt 723: {
1.20 millert 724: FILE *fout, *din;
1.1 deraadt 725: int (*closefunc) __P((FILE *));
1.10 millert 726: sig_t oldinti, oldintr, oldintp;
1.20 millert 727: int c, d;
728: volatile int is_retr, tcrflag, bare_lfs;
729: static size_t bufsize;
1.1 deraadt 730: static char *buf;
1.20 millert 731: volatile off_t hashbytes;
1.1 deraadt 732: struct stat st;
1.10 millert 733: time_t mtime;
1.13 millert 734: int oprogress;
1.16 millert 735: int opreserve;
1.1 deraadt 736:
1.20 millert 737: #ifdef __GNUC__ /* XXX: to shut up gcc warnings */
738: (void)&local;
739: (void)&fout;
740: (void)&din;
741: (void)&closefunc;
742: (void)&oldinti;
743: (void)&oldintr;
744: (void)&oldintp;
745: #endif
746:
747: fout = NULL;
748: din = NULL;
749: oldinti = NULL;
1.10 millert 750: hashbytes = mark;
751: direction = "received";
752: bytes = 0;
1.20 millert 753: bare_lfs = 0;
1.10 millert 754: filesize = -1;
1.13 millert 755: oprogress = progress;
1.16 millert 756: opreserve = preserve;
1.1 deraadt 757: is_retr = strcmp(cmd, "RETR") == 0;
758: if (is_retr && verbose && printnames) {
1.22 millert 759: if (local && (ignorespecial || *local != '-'))
1.18 deraadt 760: fprintf(ttyout, "local: %s ", local);
1.1 deraadt 761: if (remote)
1.18 deraadt 762: fprintf(ttyout, "remote: %s\n", remote);
1.1 deraadt 763: }
764: if (proxy && is_retr) {
765: proxtrans(cmd, local, remote);
766: return;
767: }
768: closefunc = NULL;
769: oldintr = NULL;
770: oldintp = NULL;
771: tcrflag = !crflag && is_retr;
772: if (setjmp(recvabort)) {
773: while (cpend) {
1.11 millert 774: (void)getreply(0);
1.1 deraadt 775: }
776: if (data >= 0) {
1.11 millert 777: (void)close(data);
1.1 deraadt 778: data = -1;
779: }
780: if (oldintr)
1.11 millert 781: (void)signal(SIGINT, oldintr);
1.10 millert 782: if (oldinti)
1.11 millert 783: (void)signal(SIGINFO, oldinti);
1.15 millert 784: progress = oprogress;
1.16 millert 785: preserve = opreserve;
1.1 deraadt 786: code = -1;
787: return;
788: }
789: oldintr = signal(SIGINT, abortrecv);
1.10 millert 790: oldinti = signal(SIGINFO, psummary);
1.22 millert 791: if (ignorespecial || (strcmp(local, "-") && *local != '|')) {
792: if (access(local, W_OK) < 0) {
1.1 deraadt 793: char *dir = strrchr(local, '/');
794:
795: if (errno != ENOENT && errno != EACCES) {
796: warn("local: %s", local);
1.11 millert 797: (void)signal(SIGINT, oldintr);
798: (void)signal(SIGINFO, oldinti);
1.1 deraadt 799: code = -1;
800: return;
801: }
802: if (dir != NULL)
803: *dir = 0;
1.22 millert 804: d = access(dir == local ? "/" : dir ? local : ".", W_OK);
1.1 deraadt 805: if (dir != NULL)
806: *dir = '/';
807: if (d < 0) {
808: warn("local: %s", local);
1.11 millert 809: (void)signal(SIGINT, oldintr);
810: (void)signal(SIGINFO, oldinti);
1.1 deraadt 811: code = -1;
812: return;
813: }
814: if (!runique && errno == EACCES &&
1.20 millert 815: chmod(local, (S_IRUSR|S_IWUSR)) < 0) {
1.1 deraadt 816: warn("local: %s", local);
1.11 millert 817: (void)signal(SIGINT, oldintr);
818: (void)signal(SIGINFO, oldinti);
1.1 deraadt 819: code = -1;
820: return;
821: }
822: if (runique && errno == EACCES &&
823: (local = gunique(local)) == NULL) {
1.11 millert 824: (void)signal(SIGINT, oldintr);
825: (void)signal(SIGINFO, oldinti);
1.1 deraadt 826: code = -1;
827: return;
828: }
829: }
830: else if (runique && (local = gunique(local)) == NULL) {
1.11 millert 831: (void)signal(SIGINT, oldintr);
832: (void)signal(SIGINFO, oldinti);
1.1 deraadt 833: code = -1;
834: return;
835: }
836: }
837: if (!is_retr) {
838: if (curtype != TYPE_A)
839: changetype(TYPE_A, 0);
1.10 millert 840: } else {
841: if (curtype != type)
842: changetype(type, 0);
843: filesize = remotesize(remote, 0);
844: }
1.1 deraadt 845: if (initconn()) {
1.11 millert 846: (void)signal(SIGINT, oldintr);
847: (void)signal(SIGINFO, oldinti);
1.1 deraadt 848: code = -1;
849: return;
850: }
851: if (setjmp(recvabort))
852: goto abort;
853: if (is_retr && restart_point &&
854: command("REST %ld", (long) restart_point) != CONTINUE)
855: return;
856: if (remote) {
857: if (command("%s %s", cmd, remote) != PRELIM) {
1.11 millert 858: (void)signal(SIGINT, oldintr);
859: (void)signal(SIGINFO, oldinti);
1.1 deraadt 860: return;
861: }
862: } else {
863: if (command("%s", cmd) != PRELIM) {
1.11 millert 864: (void)signal(SIGINT, oldintr);
865: (void)signal(SIGINFO, oldinti);
1.1 deraadt 866: return;
867: }
868: }
869: din = dataconn("r");
870: if (din == NULL)
871: goto abort;
1.22 millert 872: if (!ignorespecial && strcmp(local, "-") == 0) {
1.1 deraadt 873: fout = stdout;
1.13 millert 874: progress = 0;
1.16 millert 875: preserve = 0;
1.22 millert 876: } else if (!ignorespecial && *local == '|') {
1.1 deraadt 877: oldintp = signal(SIGPIPE, SIG_IGN);
878: fout = popen(local + 1, "w");
879: if (fout == NULL) {
880: warn("%s", local+1);
881: goto abort;
882: }
1.13 millert 883: progress = 0;
1.16 millert 884: preserve = 0;
1.1 deraadt 885: closefunc = pclose;
886: } else {
887: fout = fopen(local, lmode);
888: if (fout == NULL) {
889: warn("local: %s", local);
890: goto abort;
891: }
892: closefunc = fclose;
893: }
894: if (fstat(fileno(fout), &st) < 0 || st.st_blksize == 0)
895: st.st_blksize = BUFSIZ;
896: if (st.st_blksize > bufsize) {
897: if (buf)
1.11 millert 898: (void)free(buf);
1.1 deraadt 899: buf = malloc((unsigned)st.st_blksize);
900: if (buf == NULL) {
901: warn("malloc");
902: bufsize = 0;
903: goto abort;
904: }
905: bufsize = st.st_blksize;
906: }
1.14 millert 907: if ((st.st_mode & S_IFMT) != S_IFREG) {
1.13 millert 908: progress = 0;
909: preserve = 0;
910: }
1.10 millert 911: progressmeter(-1);
1.1 deraadt 912: switch (curtype) {
913:
914: case TYPE_I:
915: case TYPE_L:
916: if (restart_point &&
917: lseek(fileno(fout), restart_point, SEEK_SET) < 0) {
918: warn("local: %s", local);
1.13 millert 919: progress = oprogress;
1.16 millert 920: preserve = opreserve;
1.1 deraadt 921: if (closefunc != NULL)
922: (*closefunc)(fout);
923: return;
924: }
925: errno = d = 0;
926: while ((c = read(fileno(din), buf, bufsize)) > 0) {
1.20 millert 927: if ((d = write(fileno(fout), buf, (size_t)c)) != c)
1.1 deraadt 928: break;
929: bytes += c;
1.10 millert 930: if (hash && (!progress || filesize < 0)) {
1.1 deraadt 931: while (bytes >= hashbytes) {
1.18 deraadt 932: (void)putc('#', ttyout);
1.7 kstailey 933: hashbytes += mark;
1.1 deraadt 934: }
1.18 deraadt 935: (void)fflush(ttyout);
1.1 deraadt 936: }
937: }
1.10 millert 938: if (hash && (!progress || filesize < 0) && bytes > 0) {
1.7 kstailey 939: if (bytes < mark)
1.18 deraadt 940: (void)putc('#', ttyout);
941: (void)putc('\n', ttyout);
942: (void)fflush(ttyout);
1.1 deraadt 943: }
944: if (c < 0) {
945: if (errno != EPIPE)
946: warn("netin");
947: bytes = -1;
948: }
949: if (d < c) {
950: if (d < 0)
951: warn("local: %s", local);
952: else
953: warnx("%s: short write", local);
954: }
955: break;
956:
957: case TYPE_A:
958: if (restart_point) {
959: int i, n, ch;
960:
961: if (fseek(fout, 0L, SEEK_SET) < 0)
962: goto done;
963: n = restart_point;
964: for (i = 0; i++ < n;) {
1.23 millert 965: if ((ch = fgetc(fout)) == EOF)
1.1 deraadt 966: goto done;
967: if (ch == '\n')
968: i++;
969: }
970: if (fseek(fout, 0L, SEEK_CUR) < 0) {
971: done:
972: warn("local: %s", local);
1.13 millert 973: progress = oprogress;
1.16 millert 974: preserve = opreserve;
1.1 deraadt 975: if (closefunc != NULL)
976: (*closefunc)(fout);
977: return;
978: }
979: }
1.23 millert 980: while ((c = fgetc(din)) != EOF) {
1.1 deraadt 981: if (c == '\n')
982: bare_lfs++;
983: while (c == '\r') {
1.10 millert 984: while (hash && (!progress || filesize < 0) &&
985: (bytes >= hashbytes)) {
1.18 deraadt 986: (void)putc('#', ttyout);
987: (void)fflush(ttyout);
1.7 kstailey 988: hashbytes += mark;
1.1 deraadt 989: }
990: bytes++;
1.23 millert 991: if ((c = fgetc(din)) != '\n' || tcrflag) {
1.1 deraadt 992: if (ferror(fout))
993: goto break2;
1.11 millert 994: (void)putc('\r', fout);
1.1 deraadt 995: if (c == '\0') {
996: bytes++;
997: goto contin2;
998: }
999: if (c == EOF)
1000: goto contin2;
1001: }
1002: }
1.11 millert 1003: (void)putc(c, fout);
1.1 deraadt 1004: bytes++;
1005: contin2: ;
1006: }
1007: break2:
1008: if (bare_lfs) {
1.22 millert 1009: fprintf(ttyout,
1.13 millert 1010: "WARNING! %d bare linefeeds received in ASCII mode.\n", bare_lfs);
1.22 millert 1011: fputs("File may not have transferred correctly.\n",
1012: ttyout);
1.1 deraadt 1013: }
1.10 millert 1014: if (hash && (!progress || filesize < 0)) {
1.1 deraadt 1015: if (bytes < hashbytes)
1.18 deraadt 1016: (void)putc('#', ttyout);
1017: (void)putc('\n', ttyout);
1018: (void)fflush(ttyout);
1.1 deraadt 1019: }
1020: if (ferror(din)) {
1021: if (errno != EPIPE)
1022: warn("netin");
1023: bytes = -1;
1024: }
1025: if (ferror(fout))
1026: warn("local: %s", local);
1027: break;
1028: }
1.10 millert 1029: progressmeter(1);
1.13 millert 1030: progress = oprogress;
1.16 millert 1031: preserve = opreserve;
1.1 deraadt 1032: if (closefunc != NULL)
1033: (*closefunc)(fout);
1.11 millert 1034: (void)signal(SIGINT, oldintr);
1035: (void)signal(SIGINFO, oldinti);
1.1 deraadt 1036: if (oldintp)
1.11 millert 1037: (void)signal(SIGPIPE, oldintp);
1038: (void)fclose(din);
1039: (void)getreply(0);
1.10 millert 1040: if (bytes >= 0 && is_retr) {
1041: if (bytes > 0)
1042: ptransfer(0);
1043: if (preserve && (closefunc == fclose)) {
1044: mtime = remotemodtime(remote, 0);
1045: if (mtime != -1) {
1.12 millert 1046: struct utimbuf ut;
1047:
1048: ut.actime = time(NULL);
1049: ut.modtime = mtime;
1050: if (utime(local, &ut) == -1)
1.22 millert 1051: fprintf(ttyout,
1.13 millert 1052: "Can't change modification time on %s to %s",
1.11 millert 1053: local, asctime(localtime(&mtime)));
1.10 millert 1054: }
1055: }
1056: }
1.1 deraadt 1057: return;
1.13 millert 1058:
1.1 deraadt 1059: abort:
1060:
1.10 millert 1061: /* abort using RFC959 recommended IP,SYNC sequence */
1.1 deraadt 1062:
1.13 millert 1063: progress = oprogress;
1.16 millert 1064: preserve = opreserve;
1.1 deraadt 1065: if (oldintp)
1.11 millert 1066: (void)signal(SIGPIPE, oldintp);
1067: (void)signal(SIGINT, SIG_IGN);
1.1 deraadt 1068: if (!cpend) {
1069: code = -1;
1.11 millert 1070: (void)signal(SIGINT, oldintr);
1071: (void)signal(SIGINFO, oldinti);
1.1 deraadt 1072: return;
1073: }
1074:
1075: abort_remote(din);
1076: code = -1;
1077: if (data >= 0) {
1.11 millert 1078: (void)close(data);
1.1 deraadt 1079: data = -1;
1080: }
1081: if (closefunc != NULL && fout != NULL)
1082: (*closefunc)(fout);
1083: if (din)
1.11 millert 1084: (void)fclose(din);
1.1 deraadt 1085: if (bytes > 0)
1.10 millert 1086: ptransfer(0);
1.11 millert 1087: (void)signal(SIGINT, oldintr);
1088: (void)signal(SIGINFO, oldinti);
1.1 deraadt 1089: }
1090:
1091: /*
1092: * Need to start a listen on the data channel before we send the command,
1093: * otherwise the server's connect may fail.
1094: */
1095: int
1096: initconn()
1097: {
1098: char *p, *a;
1099: int result, len, tmpno = 0;
1100: int on = 1;
1101: int a0, a1, a2, a3, p0, p1;
1102:
1.24 millert 1103: reinit:
1.1 deraadt 1104: if (passivemode) {
1105: data = socket(AF_INET, SOCK_STREAM, 0);
1106: if (data < 0) {
1.10 millert 1107: warn("socket");
1108: return (1);
1.1 deraadt 1109: }
1110: if ((options & SO_DEBUG) &&
1111: setsockopt(data, SOL_SOCKET, SO_DEBUG, (char *)&on,
1.11 millert 1112: sizeof(on)) < 0)
1.10 millert 1113: warn("setsockopt (ignored)");
1.1 deraadt 1114: if (command("PASV") != COMPLETE) {
1.24 millert 1115: if (activefallback) {
1116: (void)close(data);
1117: data = -1;
1118: passivemode = 0;
1119: activefallback = 0;
1120: goto reinit;
1121: }
1.18 deraadt 1122: fputs("Passive mode refused.\n", ttyout);
1.1 deraadt 1123: goto bad;
1124: }
1125:
1126: /*
1127: * What we've got at this point is a string of comma
1128: * separated one-byte unsigned integer values.
1129: * The first four are the an IP address. The fifth is
1130: * the MSB of the port number, the sixth is the LSB.
1131: * From that we'll prepare a sockaddr_in.
1132: */
1133:
1.10 millert 1134: if (sscanf(pasv, "%d,%d,%d,%d,%d,%d",
1.1 deraadt 1135: &a0, &a1, &a2, &a3, &p0, &p1) != 6) {
1.18 deraadt 1136: fputs(
1137: "Passive mode address scan failure. Shouldn't happen!\n", ttyout);
1.1 deraadt 1138: goto bad;
1139: }
1140:
1.10 millert 1141: memset(&data_addr, 0, sizeof(data_addr));
1.1 deraadt 1142: data_addr.sin_family = AF_INET;
1143: a = (char *)&data_addr.sin_addr.s_addr;
1144: a[0] = a0 & 0xff;
1145: a[1] = a1 & 0xff;
1146: a[2] = a2 & 0xff;
1147: a[3] = a3 & 0xff;
1148: p = (char *)&data_addr.sin_port;
1149: p[0] = p0 & 0xff;
1150: p[1] = p1 & 0xff;
1151:
1.27 deraadt 1152: while (connect(data, (struct sockaddr *)&data_addr,
1.1 deraadt 1153: sizeof(data_addr)) < 0) {
1.27 deraadt 1154: if (errno == EINTR)
1155: continue;
1.10 millert 1156: warn("connect");
1.1 deraadt 1157: goto bad;
1158: }
1159: #ifdef IP_TOS
1160: on = IPTOS_THROUGHPUT;
1161: if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&on,
1162: sizeof(int)) < 0)
1.10 millert 1163: warn("setsockopt TOS (ignored)");
1.1 deraadt 1164: #endif
1.10 millert 1165: return (0);
1.1 deraadt 1166: }
1167:
1168: noport:
1169: data_addr = myctladdr;
1170: if (sendport)
1.10 millert 1171: data_addr.sin_port = 0; /* let system pick one */
1.1 deraadt 1172: if (data != -1)
1.11 millert 1173: (void)close(data);
1.1 deraadt 1174: data = socket(AF_INET, SOCK_STREAM, 0);
1175: if (data < 0) {
1176: warn("socket");
1177: if (tmpno)
1178: sendport = 1;
1179: return (1);
1180: }
1181: if (!sendport)
1.10 millert 1182: if (setsockopt(data, SOL_SOCKET, SO_REUSEADDR, (char *)&on,
1.11 millert 1183: sizeof(on)) < 0) {
1.1 deraadt 1184: warn("setsockopt (reuse address)");
1185: goto bad;
1186: }
1.11 millert 1187: if (bind(data, (struct sockaddr *)&data_addr, sizeof(data_addr)) < 0) {
1.1 deraadt 1188: warn("bind");
1189: goto bad;
1190: }
1191: if (options & SO_DEBUG &&
1.10 millert 1192: setsockopt(data, SOL_SOCKET, SO_DEBUG, (char *)&on,
1.11 millert 1193: sizeof(on)) < 0)
1.1 deraadt 1194: warn("setsockopt (ignored)");
1.11 millert 1195: len = sizeof(data_addr);
1.1 deraadt 1196: if (getsockname(data, (struct sockaddr *)&data_addr, &len) < 0) {
1197: warn("getsockname");
1198: goto bad;
1199: }
1200: if (listen(data, 1) < 0)
1201: warn("listen");
1202: if (sendport) {
1203: a = (char *)&data_addr.sin_addr;
1204: p = (char *)&data_addr.sin_port;
1205: #define UC(b) (((int)b)&0xff)
1206: result =
1207: command("PORT %d,%d,%d,%d,%d,%d",
1208: UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
1209: UC(p[0]), UC(p[1]));
1210: if (result == ERROR && sendport == -1) {
1211: sendport = 0;
1212: tmpno = 1;
1213: goto noport;
1214: }
1215: return (result != COMPLETE);
1216: }
1217: if (tmpno)
1218: sendport = 1;
1219: #ifdef IP_TOS
1220: on = IPTOS_THROUGHPUT;
1221: if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0)
1222: warn("setsockopt TOS (ignored)");
1223: #endif
1224: return (0);
1225: bad:
1.11 millert 1226: (void)close(data), data = -1;
1.1 deraadt 1227: if (tmpno)
1228: sendport = 1;
1229: return (1);
1230: }
1231:
1232: FILE *
1233: dataconn(lmode)
1.10 millert 1234: const char *lmode;
1.1 deraadt 1235: {
1236: struct sockaddr_in from;
1.13 millert 1237: int s, fromlen, tos;
1238:
1239: fromlen = sizeof(from);
1.1 deraadt 1240:
1241: if (passivemode)
1242: return (fdopen(data, lmode));
1243:
1244: s = accept(data, (struct sockaddr *) &from, &fromlen);
1245: if (s < 0) {
1246: warn("accept");
1.11 millert 1247: (void)close(data), data = -1;
1.1 deraadt 1248: return (NULL);
1249: }
1.11 millert 1250: (void)close(data);
1.1 deraadt 1251: data = s;
1252: #ifdef IP_TOS
1253: tos = IPTOS_THROUGHPUT;
1254: if (setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0)
1255: warn("setsockopt TOS (ignored)");
1256: #endif
1257: return (fdopen(data, lmode));
1258: }
1259:
1260: void
1.10 millert 1261: psummary(notused)
1262: int notused;
1.1 deraadt 1263: {
1.26 deraadt 1264: int save_errno = errno;
1.10 millert 1265:
1266: if (bytes > 0)
1267: ptransfer(1);
1.26 deraadt 1268: errno = save_errno;
1.1 deraadt 1269: }
1270:
1271: void
1.12 millert 1272: psabort(notused)
1273: int notused;
1.1 deraadt 1274: {
1275:
1.10 millert 1276: alarmtimer(0);
1.1 deraadt 1277: abrtflag++;
1278: }
1279:
1280: void
1281: pswitch(flag)
1282: int flag;
1283: {
1284: sig_t oldintr;
1285: static struct comvars {
1286: int connect;
1287: char name[MAXHOSTNAMELEN];
1288: struct sockaddr_in mctl;
1289: struct sockaddr_in hctl;
1290: FILE *in;
1291: FILE *out;
1292: int tpe;
1293: int curtpe;
1294: int cpnd;
1295: int sunqe;
1296: int runqe;
1297: int mcse;
1298: int ntflg;
1299: char nti[17];
1300: char nto[17];
1301: int mapflg;
1302: char mi[MAXPATHLEN];
1303: char mo[MAXPATHLEN];
1304: } proxstruct, tmpstruct;
1305: struct comvars *ip, *op;
1306:
1307: abrtflag = 0;
1308: oldintr = signal(SIGINT, psabort);
1309: if (flag) {
1310: if (proxy)
1311: return;
1312: ip = &tmpstruct;
1313: op = &proxstruct;
1314: proxy++;
1315: } else {
1316: if (!proxy)
1317: return;
1318: ip = &proxstruct;
1319: op = &tmpstruct;
1320: proxy = 0;
1321: }
1322: ip->connect = connected;
1323: connected = op->connect;
1324: if (hostname) {
1.11 millert 1325: (void)strncpy(ip->name, hostname, sizeof(ip->name) - 1);
1326: ip->name[sizeof(ip->name) - 1] = '\0';
1.1 deraadt 1327: } else
1.11 millert 1328: ip->name[0] = '\0';
1.1 deraadt 1329: hostname = op->name;
1330: ip->hctl = hisctladdr;
1331: hisctladdr = op->hctl;
1332: ip->mctl = myctladdr;
1333: myctladdr = op->mctl;
1334: ip->in = cin;
1335: cin = op->in;
1336: ip->out = cout;
1337: cout = op->out;
1338: ip->tpe = type;
1339: type = op->tpe;
1340: ip->curtpe = curtype;
1341: curtype = op->curtpe;
1342: ip->cpnd = cpend;
1343: cpend = op->cpnd;
1344: ip->sunqe = sunique;
1345: sunique = op->sunqe;
1346: ip->runqe = runique;
1347: runique = op->runqe;
1348: ip->mcse = mcase;
1349: mcase = op->mcse;
1350: ip->ntflg = ntflag;
1351: ntflag = op->ntflg;
1.11 millert 1352: (void)strncpy(ip->nti, ntin, sizeof(ip->nti) - 1);
1353: (ip->nti)[sizeof(ip->nti) - 1] = '\0';
1354: (void)strcpy(ntin, op->nti);
1355: (void)strncpy(ip->nto, ntout, sizeof(ip->nto) - 1);
1356: (ip->nto)[sizeof(ip->nto) - 1] = '\0';
1357: (void)strcpy(ntout, op->nto);
1.1 deraadt 1358: ip->mapflg = mapflag;
1359: mapflag = op->mapflg;
1.11 millert 1360: (void)strncpy(ip->mi, mapin, sizeof(ip->mi) - 1);
1361: (ip->mi)[sizeof(ip->mi) - 1] = '\0';
1362: (void)strcpy(mapin, op->mi);
1363: (void)strncpy(ip->mo, mapout, sizeof(ip->mo) - 1);
1364: (ip->mo)[sizeof(ip->mo) - 1] = '\0';
1365: (void)strcpy(mapout, op->mo);
1366: (void)signal(SIGINT, oldintr);
1.1 deraadt 1367: if (abrtflag) {
1368: abrtflag = 0;
1369: (*oldintr)(SIGINT);
1370: }
1371: }
1372:
1373: void
1.12 millert 1374: abortpt(notused)
1375: int notused;
1.1 deraadt 1376: {
1377:
1.10 millert 1378: alarmtimer(0);
1.18 deraadt 1379: putc('\n', ttyout);
1380: (void)fflush(ttyout);
1.1 deraadt 1381: ptabflg++;
1382: mflag = 0;
1383: abrtflag = 0;
1384: longjmp(ptabort, 1);
1385: }
1386:
1387: void
1388: proxtrans(cmd, local, remote)
1.10 millert 1389: const char *cmd, *local, *remote;
1.1 deraadt 1390: {
1391: sig_t oldintr;
1.20 millert 1392: int prox_type, nfnd;
1393: volatile int secndflag;
1.1 deraadt 1394: char *cmd2;
1395: struct fd_set mask;
1396:
1.20 millert 1397: #ifdef __GNUC__ /* XXX: to shut up gcc warnings */
1398: (void)&oldintr;
1399: (void)&cmd2;
1400: #endif
1401:
1402: oldintr = NULL;
1403: secndflag = 0;
1.1 deraadt 1404: if (strcmp(cmd, "RETR"))
1405: cmd2 = "RETR";
1406: else
1407: cmd2 = runique ? "STOU" : "STOR";
1408: if ((prox_type = type) == 0) {
1409: if (unix_server && unix_proxy)
1410: prox_type = TYPE_I;
1411: else
1412: prox_type = TYPE_A;
1413: }
1414: if (curtype != prox_type)
1415: changetype(prox_type, 1);
1416: if (command("PASV") != COMPLETE) {
1.18 deraadt 1417: fputs("proxy server does not support third party transfers.\n",
1418: ttyout);
1.1 deraadt 1419: return;
1420: }
1421: pswitch(0);
1422: if (!connected) {
1.18 deraadt 1423: fputs("No primary connection.\n", ttyout);
1.1 deraadt 1424: pswitch(1);
1425: code = -1;
1426: return;
1427: }
1428: if (curtype != prox_type)
1429: changetype(prox_type, 1);
1430: if (command("PORT %s", pasv) != COMPLETE) {
1431: pswitch(1);
1432: return;
1433: }
1434: if (setjmp(ptabort))
1435: goto abort;
1436: oldintr = signal(SIGINT, abortpt);
1437: if (command("%s %s", cmd, remote) != PRELIM) {
1.11 millert 1438: (void)signal(SIGINT, oldintr);
1.1 deraadt 1439: pswitch(1);
1440: return;
1441: }
1442: sleep(2);
1443: pswitch(1);
1444: secndflag++;
1445: if (command("%s %s", cmd2, local) != PRELIM)
1446: goto abort;
1447: ptflag++;
1.11 millert 1448: (void)getreply(0);
1.1 deraadt 1449: pswitch(0);
1.11 millert 1450: (void)getreply(0);
1451: (void)signal(SIGINT, oldintr);
1.1 deraadt 1452: pswitch(1);
1453: ptflag = 0;
1.18 deraadt 1454: fprintf(ttyout, "local: %s remote: %s\n", local, remote);
1.1 deraadt 1455: return;
1456: abort:
1.11 millert 1457: (void)signal(SIGINT, SIG_IGN);
1.1 deraadt 1458: ptflag = 0;
1459: if (strcmp(cmd, "RETR") && !proxy)
1460: pswitch(1);
1461: else if (!strcmp(cmd, "RETR") && proxy)
1462: pswitch(0);
1463: if (!cpend && !secndflag) { /* only here if cmd = "STOR" (proxy=1) */
1464: if (command("%s %s", cmd2, local) != PRELIM) {
1465: pswitch(0);
1466: if (cpend)
1.19 kstailey 1467: abort_remote(NULL);
1.1 deraadt 1468: }
1469: pswitch(1);
1470: if (ptabflg)
1471: code = -1;
1.11 millert 1472: (void)signal(SIGINT, oldintr);
1.1 deraadt 1473: return;
1474: }
1475: if (cpend)
1.19 kstailey 1476: abort_remote(NULL);
1.1 deraadt 1477: pswitch(!proxy);
1478: if (!cpend && !secndflag) { /* only if cmd = "RETR" (proxy=1) */
1479: if (command("%s %s", cmd2, local) != PRELIM) {
1480: pswitch(0);
1481: if (cpend)
1.19 kstailey 1482: abort_remote(NULL);
1.1 deraadt 1483: pswitch(1);
1484: if (ptabflg)
1485: code = -1;
1.11 millert 1486: (void)signal(SIGINT, oldintr);
1.1 deraadt 1487: return;
1488: }
1489: }
1490: if (cpend)
1.19 kstailey 1491: abort_remote(NULL);
1.1 deraadt 1492: pswitch(!proxy);
1493: if (cpend) {
1494: FD_ZERO(&mask);
1495: FD_SET(fileno(cin), &mask);
1496: if ((nfnd = empty(&mask, 10)) <= 0) {
1497: if (nfnd < 0) {
1498: warn("abort");
1499: }
1500: if (ptabflg)
1501: code = -1;
1502: lostpeer();
1503: }
1.11 millert 1504: (void)getreply(0);
1505: (void)getreply(0);
1.1 deraadt 1506: }
1507: if (proxy)
1508: pswitch(0);
1509: pswitch(1);
1510: if (ptabflg)
1511: code = -1;
1.11 millert 1512: (void)signal(SIGINT, oldintr);
1.1 deraadt 1513: }
1514:
1515: void
1516: reset(argc, argv)
1517: int argc;
1518: char *argv[];
1519: {
1520: struct fd_set mask;
1521: int nfnd = 1;
1522:
1523: FD_ZERO(&mask);
1524: while (nfnd > 0) {
1525: FD_SET(fileno(cin), &mask);
1.10 millert 1526: if ((nfnd = empty(&mask, 0)) < 0) {
1.1 deraadt 1527: warn("reset");
1528: code = -1;
1529: lostpeer();
1530: }
1531: else if (nfnd) {
1.11 millert 1532: (void)getreply(0);
1.1 deraadt 1533: }
1534: }
1535: }
1536:
1537: char *
1538: gunique(local)
1.10 millert 1539: const char *local;
1.1 deraadt 1540: {
1541: static char new[MAXPATHLEN];
1542: char *cp = strrchr(local, '/');
1543: int d, count=0;
1544: char ext = '1';
1545:
1546: if (cp)
1547: *cp = '\0';
1.22 millert 1548: d = access(cp == local ? "/" : cp ? local : ".", W_OK);
1.1 deraadt 1549: if (cp)
1550: *cp = '/';
1551: if (d < 0) {
1552: warn("local: %s", local);
1553: return ((char *) 0);
1554: }
1.11 millert 1555: (void)strcpy(new, local);
1.1 deraadt 1556: cp = new + strlen(new);
1557: *cp++ = '.';
1558: while (!d) {
1559: if (++count == 100) {
1.18 deraadt 1560: fputs("runique: can't find unique file name.\n", ttyout);
1.1 deraadt 1561: return ((char *) 0);
1562: }
1563: *cp++ = ext;
1564: *cp = '\0';
1565: if (ext == '9')
1566: ext = '0';
1567: else
1568: ext++;
1.22 millert 1569: if ((d = access(new, F_OK)) < 0)
1.1 deraadt 1570: break;
1571: if (ext != '0')
1572: cp--;
1573: else if (*(cp - 2) == '.')
1574: *(cp - 1) = '1';
1575: else {
1576: *(cp - 2) = *(cp - 2) + 1;
1577: cp--;
1578: }
1579: }
1580: return (new);
1581: }
1582:
1583: void
1584: abort_remote(din)
1585: FILE *din;
1586: {
1587: char buf[BUFSIZ];
1588: int nfnd;
1589: struct fd_set mask;
1.13 millert 1590:
1591: if (cout == NULL) {
1592: warnx("Lost control connection for abort.");
1593: if (ptabflg)
1594: code = -1;
1595: lostpeer();
1596: return;
1597: }
1.1 deraadt 1598:
1599: /*
1600: * send IAC in urgent mode instead of DM because 4.3BSD places oob mark
1601: * after urgent byte rather than before as is protocol now
1602: */
1603: sprintf(buf, "%c%c%c", IAC, IP, IAC);
1604: if (send(fileno(cout), buf, 3, MSG_OOB) != 3)
1605: warn("abort");
1.10 millert 1606: fprintf(cout, "%cABOR\r\n", DM);
1.11 millert 1607: (void)fflush(cout);
1.1 deraadt 1608: FD_ZERO(&mask);
1609: FD_SET(fileno(cin), &mask);
1.10 millert 1610: if (din) {
1.1 deraadt 1611: FD_SET(fileno(din), &mask);
1612: }
1613: if ((nfnd = empty(&mask, 10)) <= 0) {
1614: if (nfnd < 0) {
1615: warn("abort");
1616: }
1617: if (ptabflg)
1618: code = -1;
1619: lostpeer();
1620: }
1621: if (din && FD_ISSET(fileno(din), &mask)) {
1622: while (read(fileno(din), buf, BUFSIZ) > 0)
1623: /* LOOP */;
1624: }
1625: if (getreply(0) == ERROR && code == 552) {
1626: /* 552 needed for nic style abort */
1.11 millert 1627: (void)getreply(0);
1.1 deraadt 1628: }
1.11 millert 1629: (void)getreply(0);
1.1 deraadt 1630: }