910506827cf62f6079e4535068ca7aa0d9ef7f69
[reactos.git] / base / applications / network / ftp / ftp.c
1 /*
2 * Copyright (c) 1985, 1989 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16 */
17
18 #include "precomp.h"
19
20 #include <signal.h>
21
22 #define L_SET SEEK_SET
23 #define L_INCR SEEK_CUR
24 #define caddr_t void *
25
26 #ifndef lint
27 static char sccsid[] = "@(#)ftp.c 5.28 (Berkeley) 4/20/89";
28 #endif /* not lint */
29
30 #ifndef MAXHOSTNAMELEN
31 #define MAXHOSTNAMELEN 64
32 #endif
33
34 #ifdef NOVFPRINTF
35 #define vfprintf(a,b,c) _doprnt(b,c,a)
36 #endif
37
38 #ifdef sun
39 /* FD_SET wasn't defined until 4.0. its a cheap test for uid_t presence */
40 #ifndef FD_SET
41 #define NBBY 8 /* number of bits in a byte */
42 /*
43 * Select uses bit masks of file descriptors in longs.
44 * These macros manipulate such bit fields (the filesystem macros use chars).
45 * FD_SETSIZE may be defined by the user, but the default here
46 * should be >= NOFILE (param.h).
47 */
48 #ifndef FD_SETSIZE
49 #define FD_SETSIZE 256
50 #endif
51
52 typedef long fd_mask;
53 #define NFDBITS (sizeof(fd_mask) * NBBY) /* bits per mask */
54 #ifndef howmany
55 #define howmany(x, y) (((x)+((y)-1))/(y))
56 #endif
57
58 #define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
59 #define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
60 #define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
61 #define FD_ZERO(p) bzero((char *)(p), sizeof(*(p)))
62
63 typedef int uid_t;
64 typedef int gid_t;
65 #endif
66 #endif
67
68 struct sockaddr_in hisctladdr;
69 struct sockaddr_in data_addr;
70 int data = -1;
71 int abrtflag = 0;
72 int ptflag = 0;
73 int allbinary;
74 struct sockaddr_in myctladdr;
75 uid_t getuid();
76 sig_t lostpeer();
77 off_t restart_point = 0;
78
79 SOCKET cin, cout;
80 int dataconn(const char *mode);
81
82 int command(const char *fmt, ...);
83
84 char *hostname;
85
86 typedef void (*Sig_t)(int);
87
88 // Signal Handlers
89
90 void psabort(int sig);
91
92 char *hookup(const char *host, int port)
93 {
94 register struct hostent *hp = 0;
95 int len;
96 SOCKET s;
97 static char hostnamebuf[80];
98
99 bzero((char *)&hisctladdr, sizeof (hisctladdr));
100 hisctladdr.sin_addr.s_addr = inet_addr(host);
101 if (hisctladdr.sin_addr.s_addr != (unsigned long)-1) {
102 hisctladdr.sin_family = AF_INET;
103 (void) strncpy(hostnamebuf, host, sizeof(hostnamebuf));
104 } else {
105 hp = gethostbyname(host);
106 if (hp == NULL) {
107 fprintf(stderr, "ftp: %s: ", host);
108 herror((char *)NULL);
109 code = -1;
110 return((char *) 0);
111 }
112 hisctladdr.sin_family = hp->h_addrtype;
113 bcopy(hp->h_addr_list[0],
114 (caddr_t)&hisctladdr.sin_addr, hp->h_length);
115 (void) strncpy(hostnamebuf, hp->h_name, sizeof(hostnamebuf));
116 }
117 hostname = hostnamebuf;
118 s = socket(hisctladdr.sin_family, SOCK_STREAM, 0);
119 if (s == INVALID_SOCKET) {
120 perror("ftp: socket");
121 code = -1;
122 return (0);
123 }
124 hisctladdr.sin_port = port;
125 while (connect(s, (struct sockaddr *)&hisctladdr, sizeof (hisctladdr)) < 0) {
126 if (hp && hp->h_addr_list[1]) {
127 int oerrno = errno;
128
129 fprintf(stderr, "ftp: connect to address %s: ",
130 inet_ntoa(hisctladdr.sin_addr));
131 errno = oerrno;
132 perror((char *) 0);
133 hp->h_addr_list++;
134 bcopy(hp->h_addr_list[0],
135 (caddr_t)&hisctladdr.sin_addr, hp->h_length);
136 fprintf(stdout, "Trying %s...\n",
137 inet_ntoa(hisctladdr.sin_addr));
138 (void) fflush(stdout);
139 (void) close(s);
140 s = socket(hisctladdr.sin_family, SOCK_STREAM, 0);
141 if (s == INVALID_SOCKET) {
142 perror("ftp: socket");
143 code = -1;
144 return (0);
145 }
146 continue;
147 }
148 perror("ftp: connect");
149 code = -1;
150 goto bad;
151 }
152 len = sizeof (myctladdr);
153 if (getsockname(s, (struct sockaddr *)&myctladdr, &len) < 0) {
154 perror("ftp: getsockname");
155 code = -1;
156 goto bad;
157 }
158 cin = cout = s;
159 if (verbose) {
160 printf("Connected to %s.\n", hostname);
161 (void) fflush(stdout);
162 }
163 if (getreply(0) > 2) { /* read startup message from server */
164 closesocket(cin);
165 code = -1;
166 goto bad;
167 }
168 #ifdef SO_OOBINLINE
169 {
170 int on = 1;
171
172 if (setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (const char *) &on, sizeof(on))
173 < 0 && debug) {
174 perror("ftp: setsockopt");
175 }
176 }
177 #endif //SO_OOBINLINE
178
179 return (hostname);
180 bad:
181 (void) close(s);
182 return ((char *)0);
183 }
184
185 int login(const char *host)
186 {
187 char tmp[80];
188 char *puser, *ppass, *pacct;
189 const char *user, *pass, *acct;
190 int n, aflag = 0;
191
192 user = pass = acct = 0;
193 n = ruserpass(host, &puser, &ppass, &pacct);
194 if (n < 0) {
195 code = -1;
196 return(0);
197 }
198 if (0 != n) {
199 user = puser;
200 pass = ppass;
201 acct = pacct;
202 }
203 while (user == NULL) {
204 const char *myname = "none"; // This needs to become the username env
205
206 if (myname)
207 printf("Name (%s:%s): ", host, myname);
208 else
209 printf("Name (%s): ", host);
210 (void) fflush(stdout);
211 (void) fgets(tmp, sizeof(tmp) - 1, stdin);
212 tmp[strlen(tmp) - 1] = '\0';
213 if (*tmp == '\0')
214 user = myname;
215 else
216 user = tmp;
217 }
218 n = command("USER %s", user);
219 if (n == CONTINUE) {
220 if (pass == NULL)
221 pass = getpass("Password:");
222 n = command("PASS %s", pass);
223 fflush(stdin);
224 }
225 if (n == CONTINUE) {
226 aflag++;
227 acct = getpass("Account:");
228 n = command("ACCT %s", acct);
229 }
230 if (n != COMPLETE) {
231 fprintf(stderr, "Login failed.\n");
232 return (0);
233 }
234 if (!aflag && acct != NULL)
235 (void) command("ACCT %s", acct);
236 if (proxy)
237 return(1);
238 for (n = 0; n < macnum; ++n) {
239 if (!strcmp("init", macros[n].mac_name)) {
240 (void) strcpy(line, "$init");
241 makeargv();
242 domacro(margc, margv);
243 break;
244 }
245 }
246 return (1);
247 }
248
249 static void
250 cmdabort(int sig)
251 {
252 extern jmp_buf ptabort;
253
254 printf("\n");
255 (void) fflush(stdout);
256 abrtflag++;
257 if (ptflag)
258 longjmp(ptabort,1);
259 }
260
261 /*VARARGS1*/
262 int command(const char *fmt, ...)
263 {
264 va_list ap;
265 int r;
266 void (*oldintr)(int);
267
268 abrtflag = 0;
269 if (debug) {
270 printf("---> ");
271 va_start(ap, fmt);
272 vfprintf(stdout, fmt, ap);
273 va_end(ap);
274 printf("\n");
275 (void) fflush(stdout);
276 }
277 if (cout == 0) {
278 perror ("No control connection for command");
279 code = -1;
280 return (0);
281 }
282 oldintr = signal(SIGINT,cmdabort);
283 {
284 char buffer[1024];
285
286 va_start(ap, fmt);
287 vsprintf(buffer, fmt, ap);
288 va_end(ap);
289 //DLJ: to work through firewalls - send the command as a single message
290 strcat(buffer,"\r\n");
291 fprintfSocket(cout, buffer);
292 }
293 //DLJ: the following two lines are replaced by the strcat above - seems to
294 // make it work through firewalls.
295 // fprintfSocket(cout, "\r\n");
296 // (void) fflush(cout);
297 cpend = 1;
298 r = getreply(!strcmp(fmt, "QUIT"));
299 if (abrtflag && oldintr != SIG_IGN)
300 (*oldintr)(SIGINT);
301 // (void) signal(SIGINT, oldintr);
302 return(r);
303 }
304
305 char reply_string[BUFSIZ]; /* last line of previous reply */
306
307 #include <ctype.h>
308
309 int
310 getreply(expecteof)
311 int expecteof;
312 {
313 register int c, n;
314 register int dig;
315 register char *cp;
316 int originalcode = 0, continuation = 0;
317 void (*oldintr)(int);
318 int pflag = 0;
319 char *pt = pasv;
320
321 oldintr = signal(SIGINT,cmdabort);
322 for (;;) {
323 dig = n = code = 0;
324 cp = reply_string;
325 while ((c = fgetcSocket(cin)) != '\n') {
326 if (c == IAC) { /* handle telnet commands */
327 switch (fgetcSocket(cin)) {
328 case WILL:
329 case WONT:
330 c = fgetcSocket(cin);
331 fprintfSocket(cout, "%c%c%c",IAC,DONT,c);
332 break;
333 case DO:
334 case DONT:
335 c = fgetcSocket(cin);
336 fprintfSocket(cout, "%c%c%c",IAC,WONT,c);
337 break;
338 default:
339 break;
340 }
341 continue;
342 }
343 dig++;
344 if (c == EOF) {
345 if (expecteof) {
346 // (void) signal(SIGINT,oldintr);
347 code = 221;
348 return (0);
349 }
350 lostpeer();
351 if (verbose) {
352 printf("421 Service not available, remote server has closed connection\n");
353 (void) fflush(stdout);
354 }
355 code = 421;
356 return(4);
357 }
358 if (c != '\r' && (verbose > 0 ||
359 (verbose > -1 && n == '5' && dig > 4))) {
360 if (proxflag &&
361 ((dig == 1 || dig == 5) && verbose == 0))
362 printf("%s:",hostname);
363 (void) putchar(c);
364 (void) fflush(stdout);
365 }
366 if (dig < 4 && isdigit(c))
367 code = code * 10 + (c - '0');
368 if (!pflag && code == 227)
369 pflag = 1;
370 if (dig > 4 && pflag == 1 && isdigit(c))
371 pflag = 2;
372 if (pflag == 2) {
373 if (c != '\r' && c != ')')
374 *pt++ = c;
375 else {
376 *pt = '\0';
377 pflag = 3;
378 }
379 }
380 if (dig == 4 && c == '-') {
381 if (continuation)
382 code = 0;
383 continuation++;
384 }
385 if (n == 0)
386 n = c;
387 if (cp < &reply_string[sizeof(reply_string) - 1])
388 *cp++ = c;
389 }
390 if (verbose > 0 || (verbose > -1 && n == '5')) {
391 (void) putchar(c);
392 (void) fflush (stdout);
393 }
394 if (continuation && code != originalcode) {
395 if (originalcode == 0)
396 originalcode = code;
397 continue;
398 }
399 *cp = '\0';
400 if (n != '1')
401 cpend = 0;
402 (void) signal(SIGINT,oldintr);
403 if (code == 421 || originalcode == 421)
404 lostpeer();
405 if (abrtflag && oldintr != cmdabort && oldintr != SIG_IGN)
406 (*oldintr)(SIGINT);
407 return (n - '0');
408 }
409 }
410
411 static int
412 empty(mask, sec)
413 struct fd_set *mask;
414 int sec;
415 {
416 struct timeval t;
417
418 t.tv_sec = (long) sec;
419 t.tv_usec = 0;
420 return(select(32, mask, (struct fd_set *) 0, (struct fd_set *) 0, &t));
421 }
422
423 jmp_buf sendabort;
424
425 #if 0
426 void abortsend()
427 {
428
429 mflag = 0;
430 abrtflag = 0;
431 printf("\nsend aborted\n");
432 (void) fflush(stdout);
433 longjmp(sendabort, 1);
434 }
435 #endif
436
437 #define HASHBYTES 1024
438
439 void sendrequest(const char *cmd, const char *local, const char *remote, int printnames)
440 {
441 FILE *fin;
442 int dout = 0;
443 int (*closefunc)();
444 sig_t (*oldintr)(), (*oldintp)();
445 char buf[BUFSIZ], *bufp;
446 long bytes = 0, hashbytes = HASHBYTES;
447 register int c, d;
448 struct stat st;
449 struct timeval start, stop;
450 const char *mode;
451
452 if (verbose && printnames) {
453 if (local && *local != '-')
454 printf("local: %s ", local);
455 if (remote)
456 printf("remote: %s\n", remote);
457 (void) fflush(stdout);
458 }
459 if (proxy) {
460 proxtrans(cmd, local, remote);
461 return;
462 }
463 closefunc = NULL;
464 oldintr = NULL;
465 oldintp = NULL;
466 mode = "w";
467 if (setjmp(sendabort)) {
468 while (cpend) {
469 (void) getreply(0);
470 }
471 if (data >= 0) {
472 (void) close(data);
473 data = -1;
474 }
475 if (oldintr)
476 null();// (void) signal(SIGINT,oldintr);
477 if (oldintp)
478 null();// (void) signal(SIGPIPE,oldintp);
479 code = -1;
480 return;
481 }
482 null();// oldintr = signal(SIGINT, abortsend);
483 if (strcmp(local, "-") == 0)
484 fin = stdin;
485 else if (*local == '|') {
486 null();// oldintp = signal(SIGPIPE,SIG_IGN);
487 fin = _popen(local + 1, "r");
488 if (fin == NULL) {
489 perror(local + 1);
490 null();// (void) signal(SIGINT, oldintr);
491 null();// (void) signal(SIGPIPE, oldintp);
492 code = -1;
493 return;
494 }
495 closefunc = _pclose;
496 } else {
497 fin = fopen(local, "r");
498 if (fin == NULL) {
499 perror(local);
500 null();// (void) signal(SIGINT, oldintr);
501 code = -1;
502 return;
503 }
504 closefunc = fclose;
505 if (fstat(fileno(fin), &st) < 0 ||
506 (st.st_mode&S_IFMT) != S_IFREG) {
507 fprintf(stdout, "%s: not a plain file.\n", local);
508 (void) fflush(stdout);
509 null();// (void) signal(SIGINT, oldintr);
510 fclose(fin);
511 code = -1;
512 return;
513 }
514 }
515 if (initconn()) {
516 null();// (void) signal(SIGINT, oldintr);
517 if (oldintp)
518 null();// (void) signal(SIGPIPE, oldintp);
519 code = -1;
520 if (closefunc != NULL)
521 (*closefunc)(fin);
522 return;
523 }
524 if (setjmp(sendabort))
525 goto abort;
526
527 if (restart_point &&
528 (strcmp(cmd, "STOR") == 0 || strcmp(cmd, "APPE") == 0)) {
529 if (fseek(fin, (long) restart_point, 0) < 0) {
530 perror(local);
531 restart_point = 0;
532 if (closefunc != NULL)
533 (*closefunc)(fin);
534 return;
535 }
536 if (command("REST %ld", (long) restart_point)
537 != CONTINUE) {
538 restart_point = 0;
539 if (closefunc != NULL)
540 (*closefunc)(fin);
541 return;
542 }
543 restart_point = 0;
544 mode = "r+w";
545 }
546 if (remote) {
547 if (command("%s %s", cmd, remote) != PRELIM) {
548 null();// (void) signal(SIGINT, oldintr);
549 if (oldintp)
550 null();// (void) signal(SIGPIPE, oldintp);
551 if (closefunc != NULL)
552 (*closefunc)(fin);
553 return;
554 }
555 } else
556 if (command("%s", cmd) != PRELIM) {
557 null();// (void) signal(SIGINT, oldintr);
558 if (oldintp)
559 null();// (void) signal(SIGPIPE, oldintp);
560 if (closefunc != NULL)
561 (*closefunc)(fin);
562 return;
563 }
564 dout = dataconn(mode);
565 if (!dout)
566 goto abort;
567 (void) gettimeofday(&start, (struct timezone *)0);
568 null();// oldintp = signal(SIGPIPE, SIG_IGN);
569 switch (type) {
570
571 case TYPE_I:
572 case TYPE_L:
573 errno = d = 0;
574 while ((c = read(fileno(fin), buf, sizeof (buf))) > 0) {
575 bytes += c;
576 for (bufp = buf; c > 0; c -= d, bufp += d)
577 if ((d = send(dout, bufp, c, 0)) <= 0)
578 break;
579 if (hash) {
580 while (bytes >= hashbytes) {
581 (void) putchar('#');
582 hashbytes += HASHBYTES;
583 }
584 (void) fflush(stdout);
585 }
586 }
587 if (hash && bytes > 0) {
588 if (bytes < HASHBYTES)
589 (void) putchar('#');
590 (void) putchar('\n');
591 (void) fflush(stdout);
592 }
593 if (c < 0)
594 perror(local);
595 if (d <= 0) {
596 if (d == 0)
597 fprintf(stderr, "netout: write returned 0?\n");
598 else if (errno != EPIPE)
599 perror("netout");
600 bytes = -1;
601 }
602 break;
603
604 case TYPE_A:
605 {
606 char buf[1024];
607 static int bufsize = 1024;
608 int ipos=0;
609
610 while ((c = getc(fin)) != EOF) {
611 if (c == '\n') {
612 while (hash && (bytes >= hashbytes)) {
613 (void) putchar('#');
614 (void) fflush(stdout);
615 hashbytes += HASHBYTES;
616 }
617 // Szurgot: The following code is unnecessary on Win32.
618 // (void) fputcSocket(dout, '\r');
619 // bytes++;
620 }
621
622 if (ipos >= bufsize) {
623 fputSocket(dout,buf,ipos);
624 if(!hash) (void) putchar('.');
625 ipos=0;
626 }
627 buf[ipos]=c; ++ipos;
628 bytes++;
629 }
630 if (ipos) {
631 fputSocket(dout,buf,ipos);
632 ipos=0;
633 }
634 if (hash) {
635 if (bytes < hashbytes)
636 (void) putchar('#');
637 (void) putchar('\n');
638 (void) fflush(stdout);
639 }
640 else {
641 (void) putchar('.');
642 (void) putchar('\n');
643 (void) fflush(stdout);
644 }
645 if (ferror(fin))
646 perror(local);
647 // if (ferror(dout)) {
648 // if (errno != EPIPE)
649 // perror("netout");
650 // bytes = -1;
651 // }
652 break;
653 }
654 }
655 (void) gettimeofday(&stop, (struct timezone *)0);
656 if (closefunc != NULL)
657 (*closefunc)(fin);
658 if(closesocket(dout)) {
659 int iret=WSAGetLastError ();
660 fprintf(stdout,"Error closing socket(%d)\n",iret);
661 (void) fflush(stdout);
662 }
663 (void) getreply(0);
664 null();// (void) signal(SIGINT, oldintr);
665 if (oldintp)
666 null();// (void) signal(SIGPIPE, oldintp);
667 if (bytes > 0)
668 ptransfer("sent", bytes, &start, &stop);
669 return;
670 abort:
671 (void) gettimeofday(&stop, (struct timezone *)0);
672 null();// (void) signal(SIGINT, oldintr);
673 if (oldintp)
674 null();// (void) signal(SIGPIPE, oldintp);
675 if (!cpend) {
676 code = -1;
677 (*closefunc)(fin);
678 return;
679 }
680 if (data >= 0) {
681 (void) close(data);
682 data = -1;
683 }
684 if (dout)
685 if(closesocket(dout)) {
686 int iret=WSAGetLastError ();
687 fprintf(stdout,"Error closing socket(%d)\n",iret);
688 (void) fflush(stdout);
689 }
690
691 (void) getreply(0);
692 code = -1;
693 if (closefunc != NULL && fin != NULL)
694 (*closefunc)(fin);
695 if (bytes > 0)
696 ptransfer("sent", bytes, &start, &stop);
697 }
698
699 jmp_buf recvabort;
700
701 #if 0
702 void abortrecv()
703 {
704
705 mflag = 0;
706 abrtflag = 0;
707 printf("\n");
708 (void) fflush(stdout);
709 longjmp(recvabort, 1);
710 }
711 #endif
712
713 void recvrequest(const char *cmd, const char *local, const char *remote, const char *mode,
714 int printnames)
715 {
716 FILE *fout = stdout;
717 int din = 0;
718 int (*closefunc)();
719 void (*oldintr)(int), (*oldintp)(int);
720 int oldverbose = 0, oldtype = 0, is_retr, tcrflag, nfnd, bare_lfs = 0;
721 char msg;
722 // static char *buf; // Szurgot: Shouldn't this go SOMEWHERE?
723 char buf[1024];
724 static int bufsize = 1024;
725 long bytes = 0, hashbytes = HASHBYTES;
726 // struct
727 fd_set mask;
728 register int c;
729 struct timeval start, stop;
730 // struct stat st;
731
732 is_retr = strcmp(cmd, "RETR") == 0;
733 if (is_retr && verbose && printnames) {
734 if (local && *local != '-')
735 printf("local: %s ", local);
736 if (remote)
737 printf("remote: %s\n", remote);
738 (void) fflush(stdout);
739 }
740 if (proxy && is_retr) {
741 proxtrans(cmd, local, remote);
742 return;
743 }
744 closefunc = NULL;
745 oldintr = NULL;
746 oldintp = NULL;
747 tcrflag = !crflag && is_retr;
748 if (setjmp(recvabort)) {
749 while (cpend) {
750 (void) getreply(0);
751 }
752 if (data >= 0) {
753 (void) close(data);
754 data = -1;
755 }
756 if (oldintr)
757 null();// (void) signal(SIGINT, oldintr);
758 code = -1;
759 return;
760 }
761 null();// oldintr = signal(SIGINT, abortrecv);
762 if (strcmp(local, "-") && *local != '|') {
763 #ifndef _WIN32
764 register int d;
765 // This whole thing is a problem... access Won't work on non-existent files
766 if (access(local, 2) < 0) {
767 char *dir = rindex(local, '/');
768
769 if (errno != ENOENT && errno != EACCES) {
770 perror(local);
771 (void) signal(SIGINT, oldintr);
772 code = -1;
773 return;
774 }
775 if (dir != NULL)
776 *dir = 0;
777 d = access(dir ? local : ".", 2);
778 if (dir != NULL)
779 *dir = '/';
780 if (d < 0) {
781 perror(local);
782 (void) signal(SIGINT, oldintr);
783 code = -1;
784 return;
785 }
786 if (!runique && errno == EACCES &&
787 chmod(local, 0600) < 0) {
788 perror(local);
789 (void) signal(SIGINT, oldintr);
790 code = -1;
791 return;
792 }
793 if (runique && errno == EACCES &&
794 (local = gunique(local)) == NULL) {
795 (void) signal(SIGINT, oldintr);
796 code = -1;
797 return;
798 }
799 }
800 else if (runique && (local = gunique(local)) == NULL) {
801 (void) signal(SIGINT, oldintr);
802 code = -1;
803 return;
804 }
805 #endif
806 }
807 if (initconn()) {
808 null();// (void) signal(SIGINT, oldintr);
809 code = -1;
810 return;
811 }
812 if (setjmp(recvabort))
813 goto abort;
814 if (!is_retr) {
815 if (type != TYPE_A && (allbinary == 0 || type != TYPE_I)) {
816 oldtype = type;
817 oldverbose = verbose;
818 if (!debug)
819 verbose = 0;
820 setascii(0, NULL);
821 verbose = oldverbose;
822 }
823 } else if (restart_point) {
824 if (command("REST %ld", (long) restart_point) != CONTINUE)
825 return;
826 }
827 if (remote) {
828 if (command("%s %s", cmd, remote) != PRELIM) {
829 null();// (void) signal(SIGINT, oldintr);
830 if (oldtype) {
831 if (!debug)
832 verbose = 0;
833 switch (oldtype) {
834 case TYPE_I:
835 setbinary(0, NULL);
836 break;
837 case TYPE_E:
838 setebcdic();
839 break;
840 case TYPE_L:
841 settenex(0, NULL);
842 break;
843 }
844 verbose = oldverbose;
845 }
846 return;
847 }
848 } else {
849 if (command("%s", cmd) != PRELIM) {
850 null();// (void) signal(SIGINT, oldintr);
851 if (oldtype) {
852 if (!debug)
853 verbose = 0;
854 switch (oldtype) {
855 case TYPE_I:
856 setbinary(0, NULL);
857 break;
858 case TYPE_E:
859 setebcdic();
860 break;
861 case TYPE_L:
862 settenex(0, NULL);
863 break;
864 }
865 verbose = oldverbose;
866 }
867 return;
868 }
869 }
870 din = dataconn("r");
871 if (!din)
872 goto abort;
873 if (strcmp(local, "-") == 0)
874 fout = stdout;
875 else if (*local == '|') {
876 null();// oldintp = signal(SIGPIPE, SIG_IGN);
877 fout = _popen(local + 1, "w");
878 if (fout == NULL) {
879 perror(local+1);
880 goto abort;
881 }
882 closefunc = _pclose;
883 } else {
884 fout = fopen(local, mode);
885 if (fout == NULL) {
886 perror(local);
887 goto abort;
888 }
889 closefunc = fclose;
890 }
891 (void) gettimeofday(&start, (struct timezone *)0);
892 switch (type) {
893
894 case TYPE_I:
895 case TYPE_L:
896 if (restart_point &&
897 lseek(fileno(fout), (long) restart_point, L_SET) < 0) {
898 perror(local);
899 if (closefunc != NULL)
900 (*closefunc)(fout);
901 return;
902 }
903 errno = 0;
904 // while ((c = recv(din, buf, bufsize, 1)) > 0) {
905 // if ((d = write(fileno(fout), buf, c)) != c)
906 // if ((d = write(fileno(fout), buf, c)) != c)
907 // break;
908 while ((c = recv(din, buf, bufsize, 0)) > 0) {
909 write(fileno(fout), buf, c);
910 bytes += c;
911 if (hash) {
912 while (bytes >= hashbytes) {
913 (void) putchar('#');
914 hashbytes += HASHBYTES;
915 }
916 (void) fflush(stdout);
917 }
918 }
919 if (hash && bytes > 0) {
920 if (bytes < HASHBYTES)
921 (void) putchar('#');
922 (void) putchar('\n');
923 (void) fflush(stdout);
924 }
925 // if (c < 0) {
926 // if (errno != EPIPE)
927 // perror("netin");
928 // bytes = -1;
929 // }
930 // if (d < c) {
931 // if (d < 0)
932 // perror(local);
933 // else
934 // fprintf(stderr, "%s: short write\n", local);
935 // }
936 break;
937
938 case TYPE_A:
939 if (restart_point) {
940 register int i, n, c;
941
942 if (fseek(fout, 0L, L_SET) < 0)
943 goto done;
944 n = restart_point;
945 i = 0;
946 while (i++ < n) {
947 if ((c=getc(fout)) == EOF)
948 goto done;
949 if (c == '\n')
950 i++;
951 }
952 if (fseek(fout, 0L, L_INCR) < 0) {
953 done:
954 perror(local);
955 if (closefunc != NULL)
956 (*closefunc)(fout);
957 return;
958 }
959 }
960 while ((c = fgetcSocket(din)) != EOF) {
961 if (c == '\n')
962 bare_lfs++;
963 while (c == '\r') {
964 while (hash && (bytes >= hashbytes)) {
965 (void) putchar('#');
966 (void) fflush(stdout);
967 hashbytes += HASHBYTES;
968 }
969 bytes++;
970 if ((c = fgetcSocket(din)) != '\n' || tcrflag) {
971 if (ferror(fout))
972 goto break2;
973 (void) putc('\r', fout);
974 if (c == '\0') {
975 bytes++;
976 goto contin2;
977 }
978 if (c == EOF)
979 goto contin2;
980 }
981 }
982 (void) putc(c, fout);
983 bytes++;
984 contin2: ;
985 }
986 break2:
987 if (bare_lfs) {
988 printf("WARNING! %d bare linefeeds received in ASCII mode\n", bare_lfs);
989 printf("File may not have transferred correctly.\n");
990 (void) fflush(stdout);
991 }
992 if (hash) {
993 if (bytes < hashbytes)
994 (void) putchar('#');
995 (void) putchar('\n');
996 (void) fflush(stdout);
997 }
998 // if (ferror(din)) {
999 // if (errno != EPIPE)
1000 // perror("netin");
1001 // bytes = -1;
1002 // }
1003 if (ferror(fout))
1004 perror(local);
1005 break;
1006 }
1007 if (closefunc != NULL)
1008 (*closefunc)(fout);
1009 null();// (void) signal(SIGINT, oldintr);
1010 if (oldintp)
1011 null();// (void) signal(SIGPIPE, oldintp);
1012 (void) gettimeofday(&stop, (struct timezone *)0);
1013 if(closesocket(din)) {
1014 int iret=WSAGetLastError ();
1015 fprintf(stdout,"Error closing socket(%d)\n",iret);
1016 (void) fflush(stdout);
1017 }
1018
1019 (void) getreply(0);
1020 if (bytes > 0 && is_retr)
1021 ptransfer("received", bytes, &start, &stop);
1022 if (oldtype) {
1023 if (!debug)
1024 verbose = 0;
1025 switch (oldtype) {
1026 case TYPE_I:
1027 setbinary(0, NULL);
1028 break;
1029 case TYPE_E:
1030 setebcdic();
1031 break;
1032 case TYPE_L:
1033 settenex(0, NULL);
1034 break;
1035 }
1036 verbose = oldverbose;
1037 }
1038 return;
1039 abort:
1040
1041 /* abort using RFC959 recommended IP,SYNC sequence */
1042
1043 (void) gettimeofday(&stop, (struct timezone *)0);
1044 if (oldintp)
1045 null();// (void) signal(SIGPIPE, oldintr);
1046 null();// (void) signal(SIGINT,SIG_IGN);
1047 if (oldtype) {
1048 if (!debug)
1049 verbose = 0;
1050 switch (oldtype) {
1051 case TYPE_I:
1052 setbinary(0, NULL);
1053 break;
1054 case TYPE_E:
1055 setebcdic();
1056 break;
1057 case TYPE_L:
1058 settenex(0, NULL);
1059 break;
1060 }
1061 verbose = oldverbose;
1062 }
1063 if (!cpend) {
1064 code = -1;
1065 null();// (void) signal(SIGINT,oldintr);
1066 return;
1067 }
1068
1069 fprintfSocket(cout,"%c%c",IAC,IP);
1070 msg = (char)IAC;
1071 /* send IAC in urgent mode instead of DM because UNIX places oob mark */
1072 /* after urgent byte rather than before as now is protocol */
1073 if (send(cout,&msg,1,MSG_OOB) != 1) {
1074 perror("abort");
1075 }
1076 fprintfSocket(cout,"%cABOR\r\n",DM);
1077 FD_ZERO(&mask);
1078 FD_SET(cin, &mask); // Need to correct this
1079 if (din) {
1080 FD_SET(din, &mask); // Need to correct this
1081 }
1082 if ((nfnd = empty(&mask,10)) <= 0) {
1083 if (nfnd < 0) {
1084 perror("abort");
1085 }
1086 code = -1;
1087 lostpeer();
1088 }
1089 if (din && FD_ISSET(din, &mask)) {
1090 while (recv(din, buf, bufsize, 0) > 0)
1091 ;
1092 }
1093 if (getreply(0) == ERROR && code == 552) { /* needed for nic style abort */
1094 if (data >= 0) {
1095 (void) close(data);
1096 data = -1;
1097 }
1098 (void) getreply(0);
1099 }
1100 (void) getreply(0);
1101 code = -1;
1102 if (data >= 0) {
1103 (void) close(data);
1104 data = -1;
1105 }
1106 if (closefunc != NULL && fout != NULL)
1107 (*closefunc)(fout);
1108 if (din)
1109 if(closesocket(din)) {
1110 int iret=WSAGetLastError ();
1111 fprintf(stdout,"Error closing socket(%d)\n",iret);
1112 (void) fflush(stdout);
1113 }
1114
1115 if (bytes > 0)
1116 ptransfer("received", bytes, &start, &stop);
1117 null();// (void) signal(SIGINT,oldintr);
1118 }
1119
1120 int
1121 initconn()
1122 {
1123 register char *p, *a;
1124 int result, len, tmpno = 0;
1125 int on = 1;
1126 int a0, a1, a2, a3, p0, p1;
1127
1128
1129 if (passivemode) {
1130 data = socket(AF_INET, SOCK_STREAM, 0);
1131 if (data < 0) {
1132 perror("ftp: socket");
1133 return(1);
1134 }
1135 if ((options & SO_DEBUG) &&
1136 setsockopt(data, SOL_SOCKET, SO_DEBUG, (char *)&on,
1137 sizeof (on)) < 0)
1138 perror("ftp: setsockopt (ignored)");
1139 if (command("PASV") != COMPLETE) {
1140 printf("Passive mode refused.\n");
1141 goto bad;
1142 }
1143
1144 /*
1145 * What we've got at this point is a string of comma
1146 * separated one-byte unsigned integer values.
1147 * The first four are the an IP address. The fifth is
1148 * the MSB of the port number, the sixth is the LSB.
1149 * From that we'll prepare a sockaddr_in.
1150 */
1151
1152 if (sscanf(pasv,"%d,%d,%d,%d,%d,%d",
1153 &a0, &a1, &a2, &a3, &p0, &p1) != 6) {
1154 printf("Passive mode address scan failure. Shouldn't happen!\n");
1155 goto bad;
1156 }
1157
1158 bzero(&data_addr, sizeof(data_addr));
1159 data_addr.sin_family = AF_INET;
1160 a = (char *)&data_addr.sin_addr.s_addr;
1161 a[0] = a0 & 0xff;
1162 a[1] = a1 & 0xff;
1163 a[2] = a2 & 0xff;
1164 a[3] = a3 & 0xff;
1165 p = (char *)&data_addr.sin_port;
1166 p[0] = p0 & 0xff;
1167 p[1] = p1 & 0xff;
1168
1169 if (connect(data, (struct sockaddr *)&data_addr,
1170 sizeof(data_addr)) < 0) {
1171 perror("ftp: connect");
1172 goto bad;
1173 }
1174 return(0);
1175 }
1176
1177
1178 noport:
1179 data_addr = myctladdr;
1180 if (sendport)
1181 data_addr.sin_port = 0; /* let system pick one */
1182 if (data != -1)
1183 (void) close (data);
1184 data = socket(AF_INET, SOCK_STREAM, 0);
1185 if (data < 0) {
1186 perror("ftp: socket");
1187 if (tmpno)
1188 sendport = 1;
1189 return (1);
1190 }
1191 if (!sendport)
1192 if (setsockopt(data, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof (on)) < 0) {
1193 perror("ftp: setsockopt (reuse address)");
1194 goto bad;
1195 }
1196 if (bind(data, (struct sockaddr *)&data_addr, sizeof (data_addr)) < 0) {
1197 perror("ftp: bind");
1198 goto bad;
1199 }
1200 if (options & SO_DEBUG &&
1201 setsockopt(data, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof (on)) < 0)
1202 perror("ftp: setsockopt (ignored)");
1203 len = sizeof (data_addr);
1204 if (getsockname(data, (struct sockaddr *)&data_addr, &len) < 0) {
1205 perror("ftp: getsockname");
1206 goto bad;
1207 }
1208 if (listen(data, 1) < 0)
1209 perror("ftp: listen");
1210 if (sendport) {
1211 a = (char *)&data_addr.sin_addr;
1212 p = (char *)&data_addr.sin_port;
1213 #define UC(b) (((int)b)&0xff)
1214 result =
1215 command("PORT %d,%d,%d,%d,%d,%d",
1216 UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
1217 UC(p[0]), UC(p[1]));
1218 if (result == ERROR && sendport == -1) {
1219 sendport = 0;
1220 tmpno = 1;
1221 goto noport;
1222 }
1223 return (result != COMPLETE);
1224 }
1225 if (tmpno)
1226 sendport = 1;
1227 return (0);
1228 bad:
1229 (void) fflush(stdout);
1230 (void) close(data), data = -1;
1231 if (tmpno)
1232 sendport = 1;
1233 return (1);
1234 }
1235
1236 int dataconn(const char *mode)
1237 {
1238 struct sockaddr_in from;
1239 int s, fromlen = sizeof (from);
1240
1241 if (passivemode)
1242 return (data);
1243
1244 s = accept(data, (struct sockaddr *) &from, &fromlen);
1245 if (s < 0) {
1246 perror("ftp: accept");
1247 (void) closesocket(data), data = -1;
1248 return 0;
1249 }
1250 if(closesocket(data)) {
1251 int iret=WSAGetLastError ();
1252 fprintf(stdout,"Error closing socket(%d)\n",iret);
1253 (void) fflush(stdout);
1254 }
1255
1256 data = s;
1257 return (data);
1258 }
1259
1260 void ptransfer(direction, bytes, t0, t1)
1261 const char *direction;
1262 long bytes;
1263 struct timeval *t0, *t1;
1264 {
1265 struct timeval td;
1266 double s, bs;
1267
1268 if (verbose) {
1269 tvsub(&td, t1, t0);
1270 s = td.tv_sec + (td.tv_usec / 1000000.);
1271 #define nz(x) ((x) == 0 ? 1 : (x))
1272 bs = bytes / nz(s);
1273 printf("%ld bytes %s in %.1f seconds (%.0f Kbytes/s)\n",
1274 bytes, direction, s, bs / 1024.);
1275 (void) fflush(stdout);
1276 }
1277 }
1278
1279 /*tvadd(tsum, t0)
1280 struct timeval *tsum, *t0;
1281 {
1282
1283 tsum->tv_sec += t0->tv_sec;
1284 tsum->tv_usec += t0->tv_usec;
1285 if (tsum->tv_usec > 1000000)
1286 tsum->tv_sec++, tsum->tv_usec -= 1000000;
1287 } */
1288
1289 void tvsub(tdiff, t1, t0)
1290 struct timeval *tdiff, *t1, *t0;
1291 {
1292
1293 tdiff->tv_sec = t1->tv_sec - t0->tv_sec;
1294 tdiff->tv_usec = t1->tv_usec - t0->tv_usec;
1295 if (tdiff->tv_usec < 0)
1296 tdiff->tv_sec--, tdiff->tv_usec += 1000000;
1297 }
1298
1299 void psabort(int flag)
1300 {
1301 extern int abrtflag;
1302
1303 abrtflag++;
1304 }
1305
1306 void pswitch(int flag)
1307 {
1308 extern int proxy, abrtflag;
1309 Sig_t oldintr;
1310 static struct comvars {
1311 int connect;
1312 char name[MAXHOSTNAMELEN];
1313 struct sockaddr_in mctl;
1314 struct sockaddr_in hctl;
1315 SOCKET in;
1316 SOCKET out;
1317 int tpe;
1318 int cpnd;
1319 int sunqe;
1320 int runqe;
1321 int mcse;
1322 int ntflg;
1323 char nti[17];
1324 char nto[17];
1325 int mapflg;
1326 char mi[MAXPATHLEN];
1327 char mo[MAXPATHLEN];
1328 } proxstruct, tmpstruct;
1329 struct comvars *ip, *op;
1330
1331 abrtflag = 0;
1332 oldintr = signal(SIGINT, psabort);
1333 if (flag) {
1334 if (proxy)
1335 return;
1336 ip = &tmpstruct;
1337 op = &proxstruct;
1338 proxy++;
1339 }
1340 else {
1341 if (!proxy)
1342 return;
1343 ip = &proxstruct;
1344 op = &tmpstruct;
1345 proxy = 0;
1346 }
1347 ip->connect = connected;
1348 connected = op->connect;
1349 if (hostname) {
1350 (void) strncpy(ip->name, hostname, sizeof(ip->name) - 1);
1351 ip->name[strlen(ip->name)] = '\0';
1352 } else
1353 ip->name[0] = 0;
1354 hostname = op->name;
1355 ip->hctl = hisctladdr;
1356 hisctladdr = op->hctl;
1357 ip->mctl = myctladdr;
1358 myctladdr = op->mctl;
1359 ip->in = cin;
1360 cin = op->in;
1361 ip->out = cout;
1362 cout = op->out;
1363 ip->tpe = type;
1364 type = op->tpe;
1365 if (!type)
1366 type = 1;
1367 ip->cpnd = cpend;
1368 cpend = op->cpnd;
1369 ip->sunqe = sunique;
1370 sunique = op->sunqe;
1371 ip->runqe = runique;
1372 runique = op->runqe;
1373 ip->mcse = mcase;
1374 mcase = op->mcse;
1375 ip->ntflg = ntflag;
1376 ntflag = op->ntflg;
1377 (void) strncpy(ip->nti, ntin, 16);
1378 (ip->nti)[strlen(ip->nti)] = '\0';
1379 (void) strcpy(ntin, op->nti);
1380 (void) strncpy(ip->nto, ntout, 16);
1381 (ip->nto)[strlen(ip->nto)] = '\0';
1382 (void) strcpy(ntout, op->nto);
1383 ip->mapflg = mapflag;
1384 mapflag = op->mapflg;
1385 (void) strncpy(ip->mi, mapin, MAXPATHLEN - 1);
1386 (ip->mi)[strlen(ip->mi)] = '\0';
1387 (void) strcpy(mapin, op->mi);
1388 (void) strncpy(ip->mo, mapout, MAXPATHLEN - 1);
1389 (ip->mo)[strlen(ip->mo)] = '\0';
1390 (void) strcpy(mapout, op->mo);
1391 // (void) signal(SIGINT, oldintr);
1392 if (abrtflag) {
1393 abrtflag = 0;
1394 (*oldintr)(1);
1395 }
1396 }
1397
1398 jmp_buf ptabort;
1399 int ptabflg;
1400
1401 #if 0
1402 void
1403 abortpt()
1404 {
1405 printf("\n");
1406 (void) fflush(stdout);
1407 ptabflg++;
1408 mflag = 0;
1409 abrtflag = 0;
1410 longjmp(ptabort, 1);
1411 }
1412 #endif
1413
1414 void proxtrans(cmd, local, remote)
1415 const char *cmd, *local, *remote;
1416 {
1417 // void (*oldintr)(int);
1418 int tmptype, oldtype = 0, secndflag = 0, nfnd;
1419 extern jmp_buf ptabort;
1420 const char *cmd2;
1421 // struct
1422 fd_set mask;
1423
1424 if (strcmp(cmd, "RETR"))
1425 cmd2 = "RETR";
1426 else
1427 cmd2 = runique ? "STOU" : "STOR";
1428 if (command("PASV") != COMPLETE) {
1429 printf("proxy server does not support third part transfers.\n");
1430 (void) fflush(stdout);
1431 return;
1432 }
1433 tmptype = type;
1434 pswitch(0);
1435 if (!connected) {
1436 printf("No primary connection\n");
1437 (void) fflush(stdout);
1438 pswitch(1);
1439 code = -1;
1440 return;
1441 }
1442 if (type != tmptype) {
1443 oldtype = type;
1444 switch (tmptype) {
1445 case TYPE_A:
1446 setascii(0, NULL);
1447 break;
1448 case TYPE_I:
1449 setbinary(0, NULL);
1450 break;
1451 case TYPE_E:
1452 setebcdic();
1453 break;
1454 case TYPE_L:
1455 settenex(0, NULL);
1456 break;
1457 }
1458 }
1459 if (command("PORT %s", pasv) != COMPLETE) {
1460 switch (oldtype) {
1461 case 0:
1462 break;
1463 case TYPE_A:
1464 setascii(0, NULL);
1465 break;
1466 case TYPE_I:
1467 setbinary(0, NULL);
1468 break;
1469 case TYPE_E:
1470 setebcdic();
1471 break;
1472 case TYPE_L:
1473 settenex(0, NULL);
1474 break;
1475 }
1476 pswitch(1);
1477 return;
1478 }
1479 if (setjmp(ptabort))
1480 goto abort;
1481 null();// oldintr = signal(SIGINT, abortpt);
1482 if (command("%s %s", cmd, remote) != PRELIM) {
1483 null();// (void) signal(SIGINT, oldintr);
1484 switch (oldtype) {
1485 case 0:
1486 break;
1487 case TYPE_A:
1488 setascii(0, NULL);
1489 break;
1490 case TYPE_I:
1491 setbinary(0, NULL);
1492 break;
1493 case TYPE_E:
1494 setebcdic();
1495 break;
1496 case TYPE_L:
1497 settenex(0, NULL);
1498 break;
1499 }
1500 pswitch(1);
1501 return;
1502 }
1503 sleep(2);
1504 pswitch(1);
1505 secndflag++;
1506 if (command("%s %s", cmd2, local) != PRELIM)
1507 goto abort;
1508 ptflag++;
1509 (void) getreply(0);
1510 pswitch(0);
1511 (void) getreply(0);
1512 null();// (void) signal(SIGINT, oldintr);
1513 switch (oldtype) {
1514 case 0:
1515 break;
1516 case TYPE_A:
1517 setascii(0, NULL);
1518 break;
1519 case TYPE_I:
1520 setbinary(0, NULL);
1521 break;
1522 case TYPE_E:
1523 setebcdic();
1524 break;
1525 case TYPE_L:
1526 settenex(0, NULL);
1527 break;
1528 }
1529 pswitch(1);
1530 ptflag = 0;
1531 printf("local: %s remote: %s\n", local, remote);
1532 (void) fflush(stdout);
1533 return;
1534 abort:
1535 null();// (void) signal(SIGINT, SIG_IGN);
1536 ptflag = 0;
1537 if (strcmp(cmd, "RETR") && !proxy)
1538 pswitch(1);
1539 else if (!strcmp(cmd, "RETR") && proxy)
1540 pswitch(0);
1541 if (!cpend && !secndflag) { /* only here if cmd = "STOR" (proxy=1) */
1542 if (command("%s %s", cmd2, local) != PRELIM) {
1543 pswitch(0);
1544 switch (oldtype) {
1545 case 0:
1546 break;
1547 case TYPE_A:
1548 setascii(0, NULL);
1549 break;
1550 case TYPE_I:
1551 setbinary(0, NULL);
1552 break;
1553 case TYPE_E:
1554 setebcdic();
1555 break;
1556 case TYPE_L:
1557 settenex(0, NULL);
1558 break;
1559 }
1560 if (cpend) {
1561 char msg[2];
1562
1563 fprintfSocket(cout,"%c%c",IAC,IP);
1564 *msg = (char) IAC;
1565 *(msg+1) = (char) DM;
1566 if (send(cout,msg,2,MSG_OOB) != 2)
1567 perror("abort");
1568 fprintfSocket(cout,"ABOR\r\n");
1569 FD_ZERO(&mask);
1570 // FD_SET(fileno(cin), &mask); // Chris: Need to correct this
1571 if ((nfnd = empty(&mask,10)) <= 0) {
1572 if (nfnd < 0) {
1573 perror("abort");
1574 }
1575 if (ptabflg)
1576 code = -1;
1577 lostpeer();
1578 }
1579 (void) getreply(0);
1580 (void) getreply(0);
1581 }
1582 }
1583 pswitch(1);
1584 if (ptabflg)
1585 code = -1;
1586 null();// (void) signal(SIGINT, oldintr);
1587 return;
1588 }
1589 if (cpend) {
1590 char msg[2];
1591
1592 fprintfSocket(cout,"%c%c",IAC,IP);
1593 *msg = (char)IAC;
1594 *(msg+1) = (char)DM;
1595 if (send(cout,msg,2,MSG_OOB) != 2)
1596 perror("abort");
1597 fprintfSocket(cout,"ABOR\r\n");
1598 FD_ZERO(&mask);
1599 // FD_SET(fileno(cin), &mask); // Chris: Need to correct this...
1600 if ((nfnd = empty(&mask,10)) <= 0) {
1601 if (nfnd < 0) {
1602 perror("abort");
1603 }
1604 if (ptabflg)
1605 code = -1;
1606 lostpeer();
1607 }
1608 (void) getreply(0);
1609 (void) getreply(0);
1610 }
1611 pswitch(!proxy);
1612 if (!cpend && !secndflag) { /* only if cmd = "RETR" (proxy=1) */
1613 if (command("%s %s", cmd2, local) != PRELIM) {
1614 pswitch(0);
1615 switch (oldtype) {
1616 case 0:
1617 break;
1618 case TYPE_A:
1619 setascii(0, NULL);
1620 break;
1621 case TYPE_I:
1622 setbinary(0, NULL);
1623 break;
1624 case TYPE_E:
1625 setebcdic();
1626 break;
1627 case TYPE_L:
1628 settenex(0, NULL);
1629 break;
1630 }
1631 if (cpend) {
1632 char msg[2];
1633
1634 fprintfSocket(cout,"%c%c",IAC,IP);
1635 *msg = (char)IAC;
1636 *(msg+1) = (char)DM;
1637 if (send(cout,msg,2,MSG_OOB) != 2)
1638 perror("abort");
1639 fprintfSocket(cout,"ABOR\r\n");
1640 FD_ZERO(&mask);
1641 // FD_SET(fileno(cin), &mask); // Chris:
1642 if ((nfnd = empty(&mask,10)) <= 0) {
1643 if (nfnd < 0) {
1644 perror("abort");
1645 }
1646 if (ptabflg)
1647 code = -1;
1648 lostpeer();
1649 }
1650 (void) getreply(0);
1651 (void) getreply(0);
1652 }
1653 pswitch(1);
1654 if (ptabflg)
1655 code = -1;
1656 null();// (void) signal(SIGINT, oldintr);
1657 return;
1658 }
1659 }
1660 if (cpend) {
1661 char msg[2];
1662
1663 fprintfSocket(cout,"%c%c",IAC,IP);
1664 *msg = (char)IAC;
1665 *(msg+1) = (char)DM;
1666 if (send(cout,msg,2,MSG_OOB) != 2)
1667 perror("abort");
1668 fprintfSocket(cout,"ABOR\r\n");
1669 FD_ZERO(&mask);
1670 // FD_SET(fileno(cin), &mask); // Chris:
1671 if ((nfnd = empty(&mask,10)) <= 0) {
1672 if (nfnd < 0) {
1673 perror("abort");
1674 }
1675 if (ptabflg)
1676 code = -1;
1677 lostpeer();
1678 }
1679 (void) getreply(0);
1680 (void) getreply(0);
1681 }
1682 pswitch(!proxy);
1683 if (cpend) {
1684 FD_ZERO(&mask);
1685 // FD_SET(fileno(cin), &mask); // Chris:
1686 if ((nfnd = empty(&mask,10)) <= 0) {
1687 if (nfnd < 0) {
1688 perror("abort");
1689 }
1690 if (ptabflg)
1691 code = -1;
1692 lostpeer();
1693 }
1694 (void) getreply(0);
1695 (void) getreply(0);
1696 }
1697 if (proxy)
1698 pswitch(0);
1699 switch (oldtype) {
1700 case 0:
1701 break;
1702 case TYPE_A:
1703 setascii(0, NULL);
1704 break;
1705 case TYPE_I:
1706 setbinary(0, NULL);
1707 break;
1708 case TYPE_E:
1709 setebcdic();
1710 break;
1711 case TYPE_L:
1712 settenex(0, NULL);
1713 break;
1714 }
1715 pswitch(1);
1716 if (ptabflg)
1717 code = -1;
1718 null();// (void) signal(SIGINT, oldintr);
1719 }
1720
1721 void reset(int argc, const char *argv[])
1722 {
1723 // struct
1724 fd_set mask;
1725 int nfnd = 1;
1726
1727 FD_ZERO(&mask);
1728 while (nfnd > 0) {
1729 // FD_SET(fileno(cin), &mask); // Chris
1730 if ((nfnd = empty(&mask,0)) < 0) {
1731 perror("reset");
1732 code = -1;
1733 lostpeer();
1734 }
1735 else if (nfnd) {
1736 (void) getreply(0);
1737 }
1738 }
1739 }
1740
1741 #if 0
1742 char *
1743 gunique(local)
1744 char *local;
1745 {
1746 static char new[MAXPATHLEN];
1747 char *cp = rindex(local, '/');
1748 int d, count=0;
1749 char ext = '1';
1750
1751 if (cp)
1752 *cp = '\0';
1753 d = access(cp ? local : ".", 2);
1754 if (cp)
1755 *cp = '/';
1756 if (d < 0) {
1757 perror(local);
1758 return((char *) 0);
1759 }
1760 (void) strcpy(new, local);
1761 cp = new + strlen(new);
1762 *cp++ = '.';
1763 while (!d) {
1764 if (++count == 100) {
1765 printf("runique: can't find unique file name.\n");
1766 (void) fflush(stdout);
1767 return((char *) 0);
1768 }
1769 *cp++ = ext;
1770 *cp = '\0';
1771 if (ext == '9')
1772 ext = '0';
1773 else
1774 ext++;
1775 if ((d = access(new, 0)) < 0)
1776 break;
1777 if (ext != '0')
1778 cp--;
1779 else if (*(cp - 2) == '.')
1780 *(cp - 1) = '1';
1781 else {
1782 *(cp - 2) = *(cp - 2) + 1;
1783 cp--;
1784 }
1785 }
1786 return(new);
1787 }
1788 #endif
1789
1790 int null(void)
1791 {
1792 return 0;
1793 }