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