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