Annotation of src/usr.bin/ftp/ftp.c, Revision 1.30
1.30 ! millert 1: /* $OpenBSD: ftp.c,v 1.29 1998/07/07 17:26:41 art 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.30 ! millert 41: static char rcsid[] = "$OpenBSD: ftp.c,v 1.29 1998/07/07 17:26:41 art 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)
1.29 art 392: fd_set *mask;
1.1 deraadt 393: int sec;
394: {
395: struct timeval t;
396:
397: t.tv_sec = (long) sec;
398: t.tv_usec = 0;
1.29 art 399: return (select(32, mask, (fd_set *) 0, (fd_set *) 0, &t));
1.1 deraadt 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.16 millert 874: preserve = 0;
1.22 millert 875: } else if (!ignorespecial && *local == '|') {
1.1 deraadt 876: oldintp = signal(SIGPIPE, SIG_IGN);
877: fout = popen(local + 1, "w");
878: if (fout == NULL) {
879: warn("%s", local+1);
880: goto abort;
881: }
1.13 millert 882: progress = 0;
1.16 millert 883: preserve = 0;
1.1 deraadt 884: closefunc = pclose;
885: } else {
886: fout = fopen(local, lmode);
887: if (fout == NULL) {
888: warn("local: %s", local);
889: goto abort;
890: }
891: closefunc = fclose;
892: }
893: if (fstat(fileno(fout), &st) < 0 || st.st_blksize == 0)
894: st.st_blksize = BUFSIZ;
895: if (st.st_blksize > bufsize) {
896: if (buf)
1.11 millert 897: (void)free(buf);
1.1 deraadt 898: buf = malloc((unsigned)st.st_blksize);
899: if (buf == NULL) {
900: warn("malloc");
901: bufsize = 0;
902: goto abort;
903: }
904: bufsize = st.st_blksize;
905: }
1.14 millert 906: if ((st.st_mode & S_IFMT) != S_IFREG) {
1.13 millert 907: progress = 0;
908: preserve = 0;
909: }
1.10 millert 910: progressmeter(-1);
1.1 deraadt 911: switch (curtype) {
912:
913: case TYPE_I:
914: case TYPE_L:
915: if (restart_point &&
916: lseek(fileno(fout), restart_point, SEEK_SET) < 0) {
917: warn("local: %s", local);
1.13 millert 918: progress = oprogress;
1.16 millert 919: preserve = opreserve;
1.1 deraadt 920: if (closefunc != NULL)
921: (*closefunc)(fout);
922: return;
923: }
924: errno = d = 0;
925: while ((c = read(fileno(din), buf, bufsize)) > 0) {
1.20 millert 926: if ((d = write(fileno(fout), buf, (size_t)c)) != c)
1.1 deraadt 927: break;
928: bytes += c;
1.10 millert 929: if (hash && (!progress || filesize < 0)) {
1.1 deraadt 930: while (bytes >= hashbytes) {
1.18 deraadt 931: (void)putc('#', ttyout);
1.7 kstailey 932: hashbytes += mark;
1.1 deraadt 933: }
1.18 deraadt 934: (void)fflush(ttyout);
1.1 deraadt 935: }
936: }
1.10 millert 937: if (hash && (!progress || filesize < 0) && bytes > 0) {
1.7 kstailey 938: if (bytes < mark)
1.18 deraadt 939: (void)putc('#', ttyout);
940: (void)putc('\n', ttyout);
941: (void)fflush(ttyout);
1.1 deraadt 942: }
943: if (c < 0) {
944: if (errno != EPIPE)
945: warn("netin");
946: bytes = -1;
947: }
948: if (d < c) {
949: if (d < 0)
950: warn("local: %s", local);
951: else
952: warnx("%s: short write", local);
953: }
954: break;
955:
956: case TYPE_A:
957: if (restart_point) {
958: int i, n, ch;
959:
960: if (fseek(fout, 0L, SEEK_SET) < 0)
961: goto done;
962: n = restart_point;
963: for (i = 0; i++ < n;) {
1.23 millert 964: if ((ch = fgetc(fout)) == EOF)
1.1 deraadt 965: goto done;
966: if (ch == '\n')
967: i++;
968: }
969: if (fseek(fout, 0L, SEEK_CUR) < 0) {
970: done:
971: warn("local: %s", local);
1.13 millert 972: progress = oprogress;
1.16 millert 973: preserve = opreserve;
1.1 deraadt 974: if (closefunc != NULL)
975: (*closefunc)(fout);
976: return;
977: }
978: }
1.23 millert 979: while ((c = fgetc(din)) != EOF) {
1.1 deraadt 980: if (c == '\n')
981: bare_lfs++;
982: while (c == '\r') {
1.10 millert 983: while (hash && (!progress || filesize < 0) &&
984: (bytes >= hashbytes)) {
1.18 deraadt 985: (void)putc('#', ttyout);
986: (void)fflush(ttyout);
1.7 kstailey 987: hashbytes += mark;
1.1 deraadt 988: }
989: bytes++;
1.23 millert 990: if ((c = fgetc(din)) != '\n' || tcrflag) {
1.1 deraadt 991: if (ferror(fout))
992: goto break2;
1.11 millert 993: (void)putc('\r', fout);
1.1 deraadt 994: if (c == '\0') {
995: bytes++;
996: goto contin2;
997: }
998: if (c == EOF)
999: goto contin2;
1000: }
1001: }
1.11 millert 1002: (void)putc(c, fout);
1.1 deraadt 1003: bytes++;
1004: contin2: ;
1005: }
1006: break2:
1007: if (bare_lfs) {
1.22 millert 1008: fprintf(ttyout,
1.13 millert 1009: "WARNING! %d bare linefeeds received in ASCII mode.\n", bare_lfs);
1.22 millert 1010: fputs("File may not have transferred correctly.\n",
1011: ttyout);
1.1 deraadt 1012: }
1.10 millert 1013: if (hash && (!progress || filesize < 0)) {
1.1 deraadt 1014: if (bytes < hashbytes)
1.18 deraadt 1015: (void)putc('#', ttyout);
1016: (void)putc('\n', ttyout);
1017: (void)fflush(ttyout);
1.1 deraadt 1018: }
1019: if (ferror(din)) {
1020: if (errno != EPIPE)
1021: warn("netin");
1022: bytes = -1;
1023: }
1024: if (ferror(fout))
1025: warn("local: %s", local);
1026: break;
1027: }
1.10 millert 1028: progressmeter(1);
1.13 millert 1029: progress = oprogress;
1.16 millert 1030: preserve = opreserve;
1.1 deraadt 1031: if (closefunc != NULL)
1032: (*closefunc)(fout);
1.11 millert 1033: (void)signal(SIGINT, oldintr);
1034: (void)signal(SIGINFO, oldinti);
1.1 deraadt 1035: if (oldintp)
1.11 millert 1036: (void)signal(SIGPIPE, oldintp);
1037: (void)fclose(din);
1038: (void)getreply(0);
1.10 millert 1039: if (bytes >= 0 && is_retr) {
1040: if (bytes > 0)
1041: ptransfer(0);
1042: if (preserve && (closefunc == fclose)) {
1043: mtime = remotemodtime(remote, 0);
1044: if (mtime != -1) {
1.12 millert 1045: struct utimbuf ut;
1046:
1047: ut.actime = time(NULL);
1048: ut.modtime = mtime;
1049: if (utime(local, &ut) == -1)
1.22 millert 1050: fprintf(ttyout,
1.13 millert 1051: "Can't change modification time on %s to %s",
1.11 millert 1052: local, asctime(localtime(&mtime)));
1.10 millert 1053: }
1054: }
1055: }
1.1 deraadt 1056: return;
1.13 millert 1057:
1.1 deraadt 1058: abort:
1059:
1.10 millert 1060: /* abort using RFC959 recommended IP,SYNC sequence */
1.1 deraadt 1061:
1.13 millert 1062: progress = oprogress;
1.16 millert 1063: preserve = opreserve;
1.1 deraadt 1064: if (oldintp)
1.11 millert 1065: (void)signal(SIGPIPE, oldintp);
1066: (void)signal(SIGINT, SIG_IGN);
1.1 deraadt 1067: if (!cpend) {
1068: code = -1;
1.11 millert 1069: (void)signal(SIGINT, oldintr);
1070: (void)signal(SIGINFO, oldinti);
1.1 deraadt 1071: return;
1072: }
1073:
1074: abort_remote(din);
1075: code = -1;
1076: if (data >= 0) {
1.11 millert 1077: (void)close(data);
1.1 deraadt 1078: data = -1;
1079: }
1080: if (closefunc != NULL && fout != NULL)
1081: (*closefunc)(fout);
1082: if (din)
1.11 millert 1083: (void)fclose(din);
1.1 deraadt 1084: if (bytes > 0)
1.10 millert 1085: ptransfer(0);
1.11 millert 1086: (void)signal(SIGINT, oldintr);
1087: (void)signal(SIGINFO, oldinti);
1.1 deraadt 1088: }
1089:
1090: /*
1091: * Need to start a listen on the data channel before we send the command,
1092: * otherwise the server's connect may fail.
1093: */
1094: int
1095: initconn()
1096: {
1097: char *p, *a;
1098: int result, len, tmpno = 0;
1099: int on = 1;
1100: int a0, a1, a2, a3, p0, p1;
1101:
1.24 millert 1102: reinit:
1.1 deraadt 1103: if (passivemode) {
1104: data = socket(AF_INET, SOCK_STREAM, 0);
1105: if (data < 0) {
1.10 millert 1106: warn("socket");
1107: return (1);
1.1 deraadt 1108: }
1109: if ((options & SO_DEBUG) &&
1110: setsockopt(data, SOL_SOCKET, SO_DEBUG, (char *)&on,
1.11 millert 1111: sizeof(on)) < 0)
1.10 millert 1112: warn("setsockopt (ignored)");
1.1 deraadt 1113: if (command("PASV") != COMPLETE) {
1.24 millert 1114: if (activefallback) {
1115: (void)close(data);
1116: data = -1;
1117: passivemode = 0;
1118: activefallback = 0;
1119: goto reinit;
1120: }
1.18 deraadt 1121: fputs("Passive mode refused.\n", ttyout);
1.1 deraadt 1122: goto bad;
1123: }
1124:
1125: /*
1126: * What we've got at this point is a string of comma
1127: * separated one-byte unsigned integer values.
1128: * The first four are the an IP address. The fifth is
1129: * the MSB of the port number, the sixth is the LSB.
1130: * From that we'll prepare a sockaddr_in.
1131: */
1132:
1.10 millert 1133: if (sscanf(pasv, "%d,%d,%d,%d,%d,%d",
1.1 deraadt 1134: &a0, &a1, &a2, &a3, &p0, &p1) != 6) {
1.18 deraadt 1135: fputs(
1136: "Passive mode address scan failure. Shouldn't happen!\n", ttyout);
1.1 deraadt 1137: goto bad;
1138: }
1139:
1.10 millert 1140: memset(&data_addr, 0, sizeof(data_addr));
1.1 deraadt 1141: data_addr.sin_family = AF_INET;
1142: a = (char *)&data_addr.sin_addr.s_addr;
1143: a[0] = a0 & 0xff;
1144: a[1] = a1 & 0xff;
1145: a[2] = a2 & 0xff;
1146: a[3] = a3 & 0xff;
1147: p = (char *)&data_addr.sin_port;
1148: p[0] = p0 & 0xff;
1149: p[1] = p1 & 0xff;
1150:
1.27 deraadt 1151: while (connect(data, (struct sockaddr *)&data_addr,
1.1 deraadt 1152: sizeof(data_addr)) < 0) {
1.27 deraadt 1153: if (errno == EINTR)
1154: continue;
1.10 millert 1155: warn("connect");
1.1 deraadt 1156: goto bad;
1157: }
1158: #ifdef IP_TOS
1159: on = IPTOS_THROUGHPUT;
1160: if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&on,
1161: sizeof(int)) < 0)
1.10 millert 1162: warn("setsockopt TOS (ignored)");
1.1 deraadt 1163: #endif
1.10 millert 1164: return (0);
1.1 deraadt 1165: }
1166:
1167: noport:
1168: data_addr = myctladdr;
1169: if (sendport)
1.10 millert 1170: data_addr.sin_port = 0; /* let system pick one */
1.1 deraadt 1171: if (data != -1)
1.11 millert 1172: (void)close(data);
1.1 deraadt 1173: data = socket(AF_INET, SOCK_STREAM, 0);
1174: if (data < 0) {
1175: warn("socket");
1176: if (tmpno)
1177: sendport = 1;
1178: return (1);
1179: }
1180: if (!sendport)
1.10 millert 1181: if (setsockopt(data, SOL_SOCKET, SO_REUSEADDR, (char *)&on,
1.11 millert 1182: sizeof(on)) < 0) {
1.1 deraadt 1183: warn("setsockopt (reuse address)");
1184: goto bad;
1185: }
1.11 millert 1186: if (bind(data, (struct sockaddr *)&data_addr, sizeof(data_addr)) < 0) {
1.1 deraadt 1187: warn("bind");
1188: goto bad;
1189: }
1190: if (options & SO_DEBUG &&
1.10 millert 1191: setsockopt(data, SOL_SOCKET, SO_DEBUG, (char *)&on,
1.11 millert 1192: sizeof(on)) < 0)
1.1 deraadt 1193: warn("setsockopt (ignored)");
1.11 millert 1194: len = sizeof(data_addr);
1.1 deraadt 1195: if (getsockname(data, (struct sockaddr *)&data_addr, &len) < 0) {
1196: warn("getsockname");
1197: goto bad;
1198: }
1199: if (listen(data, 1) < 0)
1200: warn("listen");
1201: if (sendport) {
1202: a = (char *)&data_addr.sin_addr;
1203: p = (char *)&data_addr.sin_port;
1204: #define UC(b) (((int)b)&0xff)
1205: result =
1206: command("PORT %d,%d,%d,%d,%d,%d",
1207: UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
1208: UC(p[0]), UC(p[1]));
1209: if (result == ERROR && sendport == -1) {
1210: sendport = 0;
1211: tmpno = 1;
1212: goto noport;
1213: }
1214: return (result != COMPLETE);
1215: }
1216: if (tmpno)
1217: sendport = 1;
1218: #ifdef IP_TOS
1219: on = IPTOS_THROUGHPUT;
1220: if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&on, sizeof(int)) < 0)
1221: warn("setsockopt TOS (ignored)");
1222: #endif
1223: return (0);
1224: bad:
1.11 millert 1225: (void)close(data), data = -1;
1.1 deraadt 1226: if (tmpno)
1227: sendport = 1;
1228: return (1);
1229: }
1230:
1231: FILE *
1232: dataconn(lmode)
1.10 millert 1233: const char *lmode;
1.1 deraadt 1234: {
1235: struct sockaddr_in from;
1.13 millert 1236: int s, fromlen, tos;
1237:
1238: fromlen = sizeof(from);
1.1 deraadt 1239:
1240: if (passivemode)
1241: return (fdopen(data, lmode));
1242:
1243: s = accept(data, (struct sockaddr *) &from, &fromlen);
1244: if (s < 0) {
1245: warn("accept");
1.11 millert 1246: (void)close(data), data = -1;
1.1 deraadt 1247: return (NULL);
1248: }
1.11 millert 1249: (void)close(data);
1.1 deraadt 1250: data = s;
1251: #ifdef IP_TOS
1252: tos = IPTOS_THROUGHPUT;
1253: if (setsockopt(s, IPPROTO_IP, IP_TOS, (char *)&tos, sizeof(int)) < 0)
1254: warn("setsockopt TOS (ignored)");
1255: #endif
1256: return (fdopen(data, lmode));
1257: }
1258:
1259: void
1.10 millert 1260: psummary(notused)
1261: int notused;
1.1 deraadt 1262: {
1.26 deraadt 1263: int save_errno = errno;
1.10 millert 1264:
1265: if (bytes > 0)
1266: ptransfer(1);
1.26 deraadt 1267: errno = save_errno;
1.1 deraadt 1268: }
1269:
1270: void
1.12 millert 1271: psabort(notused)
1272: int notused;
1.1 deraadt 1273: {
1274:
1.10 millert 1275: alarmtimer(0);
1.1 deraadt 1276: abrtflag++;
1277: }
1278:
1279: void
1280: pswitch(flag)
1281: int flag;
1282: {
1283: sig_t oldintr;
1284: static struct comvars {
1285: int connect;
1286: char name[MAXHOSTNAMELEN];
1287: struct sockaddr_in mctl;
1288: struct sockaddr_in hctl;
1289: FILE *in;
1290: FILE *out;
1291: int tpe;
1292: int curtpe;
1293: int cpnd;
1294: int sunqe;
1295: int runqe;
1296: int mcse;
1297: int ntflg;
1298: char nti[17];
1299: char nto[17];
1300: int mapflg;
1301: char mi[MAXPATHLEN];
1302: char mo[MAXPATHLEN];
1303: } proxstruct, tmpstruct;
1304: struct comvars *ip, *op;
1305:
1306: abrtflag = 0;
1307: oldintr = signal(SIGINT, psabort);
1308: if (flag) {
1309: if (proxy)
1310: return;
1311: ip = &tmpstruct;
1312: op = &proxstruct;
1313: proxy++;
1314: } else {
1315: if (!proxy)
1316: return;
1317: ip = &proxstruct;
1318: op = &tmpstruct;
1319: proxy = 0;
1320: }
1321: ip->connect = connected;
1322: connected = op->connect;
1323: if (hostname) {
1.11 millert 1324: (void)strncpy(ip->name, hostname, sizeof(ip->name) - 1);
1325: ip->name[sizeof(ip->name) - 1] = '\0';
1.1 deraadt 1326: } else
1.11 millert 1327: ip->name[0] = '\0';
1.1 deraadt 1328: hostname = op->name;
1329: ip->hctl = hisctladdr;
1330: hisctladdr = op->hctl;
1331: ip->mctl = myctladdr;
1332: myctladdr = op->mctl;
1333: ip->in = cin;
1334: cin = op->in;
1335: ip->out = cout;
1336: cout = op->out;
1337: ip->tpe = type;
1338: type = op->tpe;
1339: ip->curtpe = curtype;
1340: curtype = op->curtpe;
1341: ip->cpnd = cpend;
1342: cpend = op->cpnd;
1343: ip->sunqe = sunique;
1344: sunique = op->sunqe;
1345: ip->runqe = runique;
1346: runique = op->runqe;
1347: ip->mcse = mcase;
1348: mcase = op->mcse;
1349: ip->ntflg = ntflag;
1350: ntflag = op->ntflg;
1.11 millert 1351: (void)strncpy(ip->nti, ntin, sizeof(ip->nti) - 1);
1352: (ip->nti)[sizeof(ip->nti) - 1] = '\0';
1353: (void)strcpy(ntin, op->nti);
1354: (void)strncpy(ip->nto, ntout, sizeof(ip->nto) - 1);
1355: (ip->nto)[sizeof(ip->nto) - 1] = '\0';
1356: (void)strcpy(ntout, op->nto);
1.1 deraadt 1357: ip->mapflg = mapflag;
1358: mapflag = op->mapflg;
1.11 millert 1359: (void)strncpy(ip->mi, mapin, sizeof(ip->mi) - 1);
1360: (ip->mi)[sizeof(ip->mi) - 1] = '\0';
1361: (void)strcpy(mapin, op->mi);
1362: (void)strncpy(ip->mo, mapout, sizeof(ip->mo) - 1);
1363: (ip->mo)[sizeof(ip->mo) - 1] = '\0';
1364: (void)strcpy(mapout, op->mo);
1365: (void)signal(SIGINT, oldintr);
1.1 deraadt 1366: if (abrtflag) {
1367: abrtflag = 0;
1368: (*oldintr)(SIGINT);
1369: }
1370: }
1371:
1372: void
1.12 millert 1373: abortpt(notused)
1374: int notused;
1.1 deraadt 1375: {
1376:
1.10 millert 1377: alarmtimer(0);
1.18 deraadt 1378: putc('\n', ttyout);
1379: (void)fflush(ttyout);
1.1 deraadt 1380: ptabflg++;
1381: mflag = 0;
1382: abrtflag = 0;
1383: longjmp(ptabort, 1);
1384: }
1385:
1386: void
1387: proxtrans(cmd, local, remote)
1.10 millert 1388: const char *cmd, *local, *remote;
1.1 deraadt 1389: {
1390: sig_t oldintr;
1.20 millert 1391: int prox_type, nfnd;
1392: volatile int secndflag;
1.1 deraadt 1393: char *cmd2;
1.29 art 1394: fd_set mask;
1.1 deraadt 1395:
1.20 millert 1396: #ifdef __GNUC__ /* XXX: to shut up gcc warnings */
1397: (void)&oldintr;
1398: (void)&cmd2;
1399: #endif
1400:
1401: oldintr = NULL;
1402: secndflag = 0;
1.1 deraadt 1403: if (strcmp(cmd, "RETR"))
1404: cmd2 = "RETR";
1405: else
1406: cmd2 = runique ? "STOU" : "STOR";
1407: if ((prox_type = type) == 0) {
1408: if (unix_server && unix_proxy)
1409: prox_type = TYPE_I;
1410: else
1411: prox_type = TYPE_A;
1412: }
1413: if (curtype != prox_type)
1414: changetype(prox_type, 1);
1415: if (command("PASV") != COMPLETE) {
1.18 deraadt 1416: fputs("proxy server does not support third party transfers.\n",
1417: ttyout);
1.1 deraadt 1418: return;
1419: }
1420: pswitch(0);
1421: if (!connected) {
1.18 deraadt 1422: fputs("No primary connection.\n", ttyout);
1.1 deraadt 1423: pswitch(1);
1424: code = -1;
1425: return;
1426: }
1427: if (curtype != prox_type)
1428: changetype(prox_type, 1);
1429: if (command("PORT %s", pasv) != COMPLETE) {
1430: pswitch(1);
1431: return;
1432: }
1433: if (setjmp(ptabort))
1434: goto abort;
1435: oldintr = signal(SIGINT, abortpt);
1436: if (command("%s %s", cmd, remote) != PRELIM) {
1.11 millert 1437: (void)signal(SIGINT, oldintr);
1.1 deraadt 1438: pswitch(1);
1439: return;
1440: }
1441: sleep(2);
1442: pswitch(1);
1443: secndflag++;
1444: if (command("%s %s", cmd2, local) != PRELIM)
1445: goto abort;
1446: ptflag++;
1.11 millert 1447: (void)getreply(0);
1.1 deraadt 1448: pswitch(0);
1.11 millert 1449: (void)getreply(0);
1450: (void)signal(SIGINT, oldintr);
1.1 deraadt 1451: pswitch(1);
1452: ptflag = 0;
1.18 deraadt 1453: fprintf(ttyout, "local: %s remote: %s\n", local, remote);
1.1 deraadt 1454: return;
1455: abort:
1.11 millert 1456: (void)signal(SIGINT, SIG_IGN);
1.1 deraadt 1457: ptflag = 0;
1458: if (strcmp(cmd, "RETR") && !proxy)
1459: pswitch(1);
1460: else if (!strcmp(cmd, "RETR") && proxy)
1461: pswitch(0);
1462: if (!cpend && !secndflag) { /* only here if cmd = "STOR" (proxy=1) */
1463: if (command("%s %s", cmd2, local) != PRELIM) {
1464: pswitch(0);
1465: if (cpend)
1.19 kstailey 1466: abort_remote(NULL);
1.1 deraadt 1467: }
1468: pswitch(1);
1469: if (ptabflg)
1470: code = -1;
1.11 millert 1471: (void)signal(SIGINT, oldintr);
1.1 deraadt 1472: return;
1473: }
1474: if (cpend)
1.19 kstailey 1475: abort_remote(NULL);
1.1 deraadt 1476: pswitch(!proxy);
1477: if (!cpend && !secndflag) { /* only if cmd = "RETR" (proxy=1) */
1478: if (command("%s %s", cmd2, local) != PRELIM) {
1479: pswitch(0);
1480: if (cpend)
1.19 kstailey 1481: abort_remote(NULL);
1.1 deraadt 1482: pswitch(1);
1483: if (ptabflg)
1484: code = -1;
1.11 millert 1485: (void)signal(SIGINT, oldintr);
1.1 deraadt 1486: return;
1487: }
1488: }
1489: if (cpend)
1.19 kstailey 1490: abort_remote(NULL);
1.1 deraadt 1491: pswitch(!proxy);
1492: if (cpend) {
1493: FD_ZERO(&mask);
1494: FD_SET(fileno(cin), &mask);
1495: if ((nfnd = empty(&mask, 10)) <= 0) {
1496: if (nfnd < 0) {
1497: warn("abort");
1498: }
1499: if (ptabflg)
1500: code = -1;
1501: lostpeer();
1502: }
1.11 millert 1503: (void)getreply(0);
1504: (void)getreply(0);
1.1 deraadt 1505: }
1506: if (proxy)
1507: pswitch(0);
1508: pswitch(1);
1509: if (ptabflg)
1510: code = -1;
1.11 millert 1511: (void)signal(SIGINT, oldintr);
1.1 deraadt 1512: }
1513:
1514: void
1515: reset(argc, argv)
1516: int argc;
1517: char *argv[];
1518: {
1.29 art 1519: fd_set mask;
1.1 deraadt 1520: int nfnd = 1;
1521:
1522: FD_ZERO(&mask);
1523: while (nfnd > 0) {
1524: FD_SET(fileno(cin), &mask);
1.10 millert 1525: if ((nfnd = empty(&mask, 0)) < 0) {
1.1 deraadt 1526: warn("reset");
1527: code = -1;
1528: lostpeer();
1529: }
1530: else if (nfnd) {
1.11 millert 1531: (void)getreply(0);
1.1 deraadt 1532: }
1533: }
1534: }
1535:
1536: char *
1537: gunique(local)
1.10 millert 1538: const char *local;
1.1 deraadt 1539: {
1540: static char new[MAXPATHLEN];
1541: char *cp = strrchr(local, '/');
1542: int d, count=0;
1543: char ext = '1';
1544:
1545: if (cp)
1546: *cp = '\0';
1.22 millert 1547: d = access(cp == local ? "/" : cp ? local : ".", W_OK);
1.1 deraadt 1548: if (cp)
1549: *cp = '/';
1550: if (d < 0) {
1551: warn("local: %s", local);
1552: return ((char *) 0);
1553: }
1.11 millert 1554: (void)strcpy(new, local);
1.1 deraadt 1555: cp = new + strlen(new);
1556: *cp++ = '.';
1557: while (!d) {
1558: if (++count == 100) {
1.18 deraadt 1559: fputs("runique: can't find unique file name.\n", ttyout);
1.1 deraadt 1560: return ((char *) 0);
1561: }
1562: *cp++ = ext;
1563: *cp = '\0';
1564: if (ext == '9')
1565: ext = '0';
1566: else
1567: ext++;
1.22 millert 1568: if ((d = access(new, F_OK)) < 0)
1.1 deraadt 1569: break;
1570: if (ext != '0')
1571: cp--;
1572: else if (*(cp - 2) == '.')
1573: *(cp - 1) = '1';
1574: else {
1575: *(cp - 2) = *(cp - 2) + 1;
1576: cp--;
1577: }
1578: }
1579: return (new);
1580: }
1581:
1582: void
1583: abort_remote(din)
1584: FILE *din;
1585: {
1586: char buf[BUFSIZ];
1587: int nfnd;
1.29 art 1588: fd_set mask;
1.13 millert 1589:
1590: if (cout == NULL) {
1591: warnx("Lost control connection for abort.");
1592: if (ptabflg)
1593: code = -1;
1594: lostpeer();
1595: return;
1596: }
1.1 deraadt 1597:
1598: /*
1599: * send IAC in urgent mode instead of DM because 4.3BSD places oob mark
1600: * after urgent byte rather than before as is protocol now
1601: */
1602: sprintf(buf, "%c%c%c", IAC, IP, IAC);
1603: if (send(fileno(cout), buf, 3, MSG_OOB) != 3)
1604: warn("abort");
1.10 millert 1605: fprintf(cout, "%cABOR\r\n", DM);
1.11 millert 1606: (void)fflush(cout);
1.1 deraadt 1607: FD_ZERO(&mask);
1608: FD_SET(fileno(cin), &mask);
1.10 millert 1609: if (din) {
1.1 deraadt 1610: FD_SET(fileno(din), &mask);
1611: }
1612: if ((nfnd = empty(&mask, 10)) <= 0) {
1613: if (nfnd < 0) {
1614: warn("abort");
1615: }
1616: if (ptabflg)
1617: code = -1;
1618: lostpeer();
1619: }
1620: if (din && FD_ISSET(fileno(din), &mask)) {
1621: while (read(fileno(din), buf, BUFSIZ) > 0)
1622: /* LOOP */;
1623: }
1624: if (getreply(0) == ERROR && code == 552) {
1625: /* 552 needed for nic style abort */
1.11 millert 1626: (void)getreply(0);
1.1 deraadt 1627: }
1.11 millert 1628: (void)getreply(0);
1.1 deraadt 1629: }