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