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