ftp.c revision 1.147 1 /* $NetBSD: ftp.c,v 1.147 2007/04/17 05:52:03 lukem Exp $ */
2
3 /*-
4 * Copyright (c) 1996-2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Luke Mewburn.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Copyright (c) 1985, 1989, 1993, 1994
41 * The Regents of the University of California. All rights reserved.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. Neither the name of the University nor the names of its contributors
52 * may be used to endorse or promote products derived from this software
53 * without specific prior written permission.
54 *
55 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * SUCH DAMAGE.
66 */
67
68 /*
69 * Copyright (C) 1997 and 1998 WIDE Project.
70 * All rights reserved.
71 *
72 * Redistribution and use in source and binary forms, with or without
73 * modification, are permitted provided that the following conditions
74 * are met:
75 * 1. Redistributions of source code must retain the above copyright
76 * notice, this list of conditions and the following disclaimer.
77 * 2. Redistributions in binary form must reproduce the above copyright
78 * notice, this list of conditions and the following disclaimer in the
79 * documentation and/or other materials provided with the distribution.
80 * 3. Neither the name of the project nor the names of its contributors
81 * may be used to endorse or promote products derived from this software
82 * without specific prior written permission.
83 *
84 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
85 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
86 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
87 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
88 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
89 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
90 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
91 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
92 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
93 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
94 * SUCH DAMAGE.
95 */
96
97 #include <sys/cdefs.h>
98 #ifndef lint
99 #if 0
100 static char sccsid[] = "@(#)ftp.c 8.6 (Berkeley) 10/27/94";
101 #else
102 __RCSID("$NetBSD: ftp.c,v 1.147 2007/04/17 05:52:03 lukem Exp $");
103 #endif
104 #endif /* not lint */
105
106 #include <sys/types.h>
107 #include <sys/stat.h>
108 #include <sys/socket.h>
109 #include <sys/time.h>
110
111 #include <netinet/in.h>
112 #include <netinet/in_systm.h>
113 #include <netinet/ip.h>
114 #include <arpa/inet.h>
115 #include <arpa/ftp.h>
116 #include <arpa/telnet.h>
117
118 #include <ctype.h>
119 #include <err.h>
120 #include <errno.h>
121 #include <fcntl.h>
122 #include <netdb.h>
123 #include <stdio.h>
124 #include <stdlib.h>
125 #include <string.h>
126 #include <time.h>
127 #include <unistd.h>
128 #include <stdarg.h>
129
130 #include "ftp_var.h"
131
132 volatile sig_atomic_t abrtflag;
133 volatile sig_atomic_t timeoutflag;
134
135 sigjmp_buf ptabort;
136 int ptabflg;
137 int ptflag = 0;
138 char pasv[BUFSIZ]; /* passive port for proxy data connection */
139
140 static int empty(FILE *, FILE *, int);
141
142 struct sockinet {
143 union sockunion {
144 struct sockaddr_in su_sin;
145 #ifdef INET6
146 struct sockaddr_in6 su_sin6;
147 #endif
148 } si_su;
149 #if !HAVE_SOCKADDR_SA_LEN
150 int si_len;
151 #endif
152 };
153
154 #if !HAVE_SOCKADDR_SA_LEN
155 # define su_len si_len
156 #else
157 # define su_len si_su.su_sin.sin_len
158 #endif
159 #define su_family si_su.su_sin.sin_family
160 #define su_port si_su.su_sin.sin_port
161
162 struct sockinet myctladdr, hisctladdr, data_addr;
163
164 char *
165 hookup(char *host, char *port)
166 {
167 int s = -1, error, portnum;
168 struct addrinfo hints, *res, *res0;
169 char hbuf[MAXHOSTNAMELEN];
170 static char hostnamebuf[MAXHOSTNAMELEN];
171 socklen_t len;
172 int on = 1;
173
174 memset((char *)&hisctladdr, 0, sizeof (hisctladdr));
175 memset((char *)&myctladdr, 0, sizeof (myctladdr));
176 memset(&hints, 0, sizeof(hints));
177 portnum = parseport(port, FTP_PORT);
178 hints.ai_flags = AI_CANONNAME;
179 hints.ai_family = family;
180 hints.ai_socktype = SOCK_STREAM;
181 hints.ai_protocol = 0;
182 error = getaddrinfo(host, NULL, &hints, &res0);
183 if (error) {
184 warnx("Can't lookup `%s': %s", host,
185 (error == EAI_SYSTEM) ? strerror(errno)
186 : gai_strerror(error));
187 code = -1;
188 return (0);
189 }
190
191 if (res0->ai_canonname)
192 (void)strlcpy(hostnamebuf, res0->ai_canonname,
193 sizeof(hostnamebuf));
194 else
195 (void)strlcpy(hostnamebuf, host, sizeof(hostnamebuf));
196 hostname = hostnamebuf;
197
198 for (res = res0; res; res = res->ai_next) {
199 ai_unmapped(res);
200 if (getnameinfo(res->ai_addr, res->ai_addrlen,
201 hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST))
202 strlcpy(hbuf, "?", sizeof(hbuf));
203 if (verbose && res0->ai_next) {
204 /* if we have multiple possibilities */
205 fprintf(ttyout, "Trying %s...\n", hbuf);
206 }
207 ((struct sockaddr_in *)res->ai_addr)->sin_port = htons(portnum);
208 s = socket(res->ai_family, SOCK_STREAM, res->ai_protocol);
209 if (s < 0) {
210 warn("Can't create socket for connection to `%s'",
211 hbuf);
212 continue;
213 }
214 if (ftp_connect(s, res->ai_addr, res->ai_addrlen) < 0) {
215 close(s);
216 s = -1;
217 continue;
218 }
219
220 /* finally we got one */
221 break;
222 }
223 if (s < 0) {
224 warnx("Can't connect to `%s'", host);
225 code = -1;
226 freeaddrinfo(res0);
227 return 0;
228 }
229 memcpy(&hisctladdr.si_su, res->ai_addr, res->ai_addrlen);
230 hisctladdr.su_len = res->ai_addrlen;
231 freeaddrinfo(res0);
232 res0 = res = NULL;
233
234 len = hisctladdr.su_len;
235 if (getsockname(s, (struct sockaddr *)&myctladdr.si_su, &len) == -1) {
236 warn("Can't determine my address of connection to `%s'", host);
237 code = -1;
238 goto bad;
239 }
240 myctladdr.su_len = len;
241
242 #ifdef IPTOS_LOWDELAY
243 if (hisctladdr.su_family == AF_INET) {
244 int tos = IPTOS_LOWDELAY;
245 if (setsockopt(s, IPPROTO_IP, IP_TOS,
246 (void *)&tos, sizeof(tos)) == -1) {
247 DWARN("setsockopt %s (ignored)",
248 "IPTOS_LOWDELAY");
249 }
250 }
251 #endif
252 cin = fdopen(s, "r");
253 cout = fdopen(s, "w");
254 if (cin == NULL || cout == NULL) {
255 warnx("Can't fdopen socket");
256 if (cin)
257 (void)fclose(cin);
258 if (cout)
259 (void)fclose(cout);
260 code = -1;
261 goto bad;
262 }
263 if (verbose)
264 fprintf(ttyout, "Connected to %s.\n", hostname);
265 if (getreply(0) > 2) { /* read startup message from server */
266 if (cin)
267 (void)fclose(cin);
268 if (cout)
269 (void)fclose(cout);
270 code = -1;
271 goto bad;
272 }
273
274 if (setsockopt(s, SOL_SOCKET, SO_OOBINLINE,
275 (void *)&on, sizeof(on)) == -1) {
276 DWARN("setsockopt %s (ignored)", "SO_OOBINLINE");
277 }
278
279 return (hostname);
280 bad:
281 (void)close(s);
282 return (NULL);
283 }
284
285 void
286 cmdabort(int notused)
287 {
288 int oerrno = errno;
289
290 sigint_raised = 1;
291 alarmtimer(0);
292 if (fromatty)
293 write(fileno(ttyout), "\n", 1);
294 abrtflag++;
295 if (ptflag)
296 siglongjmp(ptabort, 1);
297 errno = oerrno;
298 }
299
300 void
301 cmdtimeout(int notused)
302 {
303 int oerrno = errno;
304
305 alarmtimer(0);
306 if (fromatty)
307 write(fileno(ttyout), "\n", 1);
308 timeoutflag++;
309 if (ptflag)
310 siglongjmp(ptabort, 1);
311 errno = oerrno;
312 }
313
314 /*VARARGS*/
315 int
316 command(const char *fmt, ...)
317 {
318 va_list ap;
319 int r;
320 sigfunc oldsigint;
321
322 #ifndef NO_DEBUG
323 if (ftp_debug) {
324 fputs("---> ", ttyout);
325 va_start(ap, fmt);
326 if (strncmp("PASS ", fmt, 5) == 0)
327 fputs("PASS XXXX", ttyout);
328 else if (strncmp("ACCT ", fmt, 5) == 0)
329 fputs("ACCT XXXX", ttyout);
330 else
331 vfprintf(ttyout, fmt, ap);
332 va_end(ap);
333 putc('\n', ttyout);
334 }
335 #endif
336 if (cout == NULL) {
337 warnx("No control connection for command");
338 code = -1;
339 return (0);
340 }
341
342 abrtflag = 0;
343
344 oldsigint = xsignal(SIGINT, cmdabort);
345
346 va_start(ap, fmt);
347 vfprintf(cout, fmt, ap);
348 va_end(ap);
349 fputs("\r\n", cout);
350 (void)fflush(cout);
351 cpend = 1;
352 r = getreply(!strcmp(fmt, "QUIT"));
353 if (abrtflag && oldsigint != SIG_IGN)
354 (*oldsigint)(SIGINT);
355 (void)xsignal(SIGINT, oldsigint);
356 return (r);
357 }
358
359 static const char *m421[] = {
360 "remote server timed out. Connection closed",
361 "user interrupt. Connection closed",
362 "remote server has closed connection",
363 };
364
365 int
366 getreply(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 sigfunc oldsigint, oldsigalrm;
373 int pflag = 0;
374 char *cp, *pt = pasv;
375
376 abrtflag = 0;
377 timeoutflag = 0;
378
379 oldsigint = xsignal(SIGINT, cmdabort);
380 oldsigalrm = xsignal(SIGALRM, cmdtimeout);
381
382 for (line = 0 ;; line++) {
383 dig = n = code = 0;
384 cp = current_line;
385 while (alarmtimer(quit_time ? quit_time : 60),
386 ((c = getc(cin)) != '\n')) {
387 if (c == IAC) { /* handle telnet commands */
388 switch (c = getc(cin)) {
389 case WILL:
390 case WONT:
391 c = getc(cin);
392 fprintf(cout, "%c%c%c", IAC, DONT, c);
393 (void)fflush(cout);
394 break;
395 case DO:
396 case DONT:
397 c = getc(cin);
398 fprintf(cout, "%c%c%c", IAC, WONT, c);
399 (void)fflush(cout);
400 break;
401 default:
402 break;
403 }
404 continue;
405 }
406 dig++;
407 if (c == EOF) {
408 /*
409 * these will get trashed by pswitch()
410 * in lostpeer()
411 */
412 int reply_timeoutflag = timeoutflag;
413 int reply_abrtflag = abrtflag;
414
415 alarmtimer(0);
416 if (expecteof && feof(cin)) {
417 (void)xsignal(SIGINT, oldsigint);
418 (void)xsignal(SIGALRM, oldsigalrm);
419 code = 221;
420 return (0);
421 }
422 cpend = 0;
423 lostpeer(0);
424 if (verbose) {
425 size_t midx;
426 if (reply_timeoutflag)
427 midx = 0;
428 else if (reply_abrtflag)
429 midx = 1;
430 else
431 midx = 2;
432 (void)fprintf(ttyout,
433 "421 Service not available, %s.\n", m421[midx]);
434 (void)fflush(ttyout);
435 }
436 code = 421;
437 (void)xsignal(SIGINT, oldsigint);
438 (void)xsignal(SIGALRM, oldsigalrm);
439 return (4);
440 }
441 if (c != '\r' && (verbose > 0 ||
442 ((verbose > -1 && n == '5' && dig > 4) &&
443 (((!n && c < '5') || (n && n < '5'))
444 || !retry_connect)))) {
445 if (proxflag &&
446 (dig == 1 || (dig == 5 && verbose == 0)))
447 fprintf(ttyout, "%s:", hostname);
448 (void)putc(c, ttyout);
449 }
450 if (dig < 4 && isdigit(c))
451 code = code * 10 + (c - '0');
452 if (!pflag && (code == 227 || code == 228))
453 pflag = 1;
454 else if (!pflag && code == 229)
455 pflag = 100;
456 if (dig > 4 && pflag == 1 && isdigit(c))
457 pflag = 2;
458 if (pflag == 2) {
459 if (c != '\r' && c != ')') {
460 if (pt < &pasv[sizeof(pasv) - 1])
461 *pt++ = c;
462 } else {
463 *pt = '\0';
464 pflag = 3;
465 }
466 }
467 if (pflag == 100 && c == '(')
468 pflag = 2;
469 if (dig == 4 && c == '-') {
470 if (continuation)
471 code = 0;
472 continuation++;
473 }
474 if (n == 0)
475 n = c;
476 if (cp < ¤t_line[sizeof(current_line) - 1])
477 *cp++ = c;
478 }
479 if (verbose > 0 || ((verbose > -1 && n == '5') &&
480 (n < '5' || !retry_connect))) {
481 (void)putc(c, ttyout);
482 (void)fflush(ttyout);
483 }
484 if (cp[-1] == '\r')
485 cp[-1] = '\0';
486 *cp = '\0';
487 if (line == 0)
488 (void)strlcpy(reply_string, current_line,
489 sizeof(reply_string));
490 if (line > 0 && code == 0 && reply_callback != NULL)
491 (*reply_callback)(current_line);
492 if (continuation && code != originalcode) {
493 if (originalcode == 0)
494 originalcode = code;
495 continue;
496 }
497 if (n != '1')
498 cpend = 0;
499 alarmtimer(0);
500 (void)xsignal(SIGINT, oldsigint);
501 (void)xsignal(SIGALRM, oldsigalrm);
502 if (code == 421 || originalcode == 421)
503 lostpeer(0);
504 if (abrtflag && oldsigint != cmdabort && oldsigint != SIG_IGN)
505 (*oldsigint)(SIGINT);
506 if (timeoutflag && oldsigalrm != cmdtimeout &&
507 oldsigalrm != SIG_IGN)
508 (*oldsigalrm)(SIGINT);
509 return (n - '0');
510 }
511 }
512
513 static int
514 empty(FILE *cin, FILE *din, int sec)
515 {
516 int nr, nfd;
517 struct pollfd pfd[2];
518
519 nfd = 0;
520 if (cin) {
521 pfd[nfd].fd = fileno(cin);
522 pfd[nfd++].events = POLLIN;
523 }
524
525 if (din) {
526 pfd[nfd].fd = fileno(din);
527 pfd[nfd++].events = POLLIN;
528 }
529
530 if ((nr = ftp_poll(pfd, nfd, sec * 1000)) <= 0)
531 return nr;
532
533 nr = 0;
534 nfd = 0;
535 if (cin)
536 nr |= (pfd[nfd++].revents & POLLIN) ? 1 : 0;
537 if (din)
538 nr |= (pfd[nfd++].revents & POLLIN) ? 2 : 0;
539 return nr;
540 }
541
542 sigjmp_buf xferabort;
543
544 void
545 abortxfer(int notused)
546 {
547 char msgbuf[100];
548 size_t len;
549
550 sigint_raised = 1;
551 alarmtimer(0);
552 mflag = 0;
553 abrtflag = 0;
554 switch (direction[0]) {
555 case 'r':
556 strlcpy(msgbuf, "\nreceive", sizeof(msgbuf));
557 break;
558 case 's':
559 strlcpy(msgbuf, "\nsend", sizeof(msgbuf));
560 break;
561 default:
562 errx(1, "abortxfer: unknown direction `%s'", direction);
563 }
564 len = strlcat(msgbuf, " aborted. Waiting for remote to finish abort.\n",
565 sizeof(msgbuf));
566 write(fileno(ttyout), msgbuf, len);
567 siglongjmp(xferabort, 1);
568 }
569
570 void
571 sendrequest(const char *cmd, const char *local, const char *remote,
572 int printnames)
573 {
574 struct stat st;
575 int c, d;
576 FILE *volatile fin;
577 FILE *volatile dout;
578 int (*volatile closefunc)(FILE *);
579 sigfunc volatile oldintr;
580 sigfunc volatile oldintp;
581 off_t volatile hashbytes;
582 char *volatile lmode;
583 char *bufp;
584 static size_t bufsize;
585 static char *buf;
586 int oprogress;
587
588 hashbytes = mark;
589 direction = "sent";
590 dout = NULL;
591 bytes = 0;
592 filesize = -1;
593 oprogress = progress;
594 if (verbose && printnames) {
595 if (*local != '-')
596 fprintf(ttyout, "local: %s ", local);
597 if (remote)
598 fprintf(ttyout, "remote: %s\n", remote);
599 }
600 if (proxy) {
601 proxtrans(cmd, local, remote);
602 return;
603 }
604 if (curtype != type)
605 changetype(type, 0);
606 closefunc = NULL;
607 oldintr = NULL;
608 oldintp = NULL;
609 lmode = "w";
610 if (sigsetjmp(xferabort, 1)) {
611 while (cpend)
612 (void)getreply(0);
613 code = -1;
614 goto cleanupsend;
615 }
616 (void)xsignal(SIGQUIT, psummary);
617 oldintr = xsignal(SIGINT, abortxfer);
618 if (strcmp(local, "-") == 0) {
619 fin = stdin;
620 progress = 0;
621 } else if (*local == '|') {
622 oldintp = xsignal(SIGPIPE, SIG_IGN);
623 fin = popen(local + 1, "r");
624 if (fin == NULL) {
625 warn("Can't execute `%s'", local + 1);
626 code = -1;
627 goto cleanupsend;
628 }
629 progress = 0;
630 closefunc = pclose;
631 } else {
632 fin = fopen(local, "r");
633 if (fin == NULL) {
634 warn("Can't open `%s'", local);
635 code = -1;
636 goto cleanupsend;
637 }
638 closefunc = fclose;
639 if (fstat(fileno(fin), &st) < 0 || !S_ISREG(st.st_mode)) {
640 fprintf(ttyout, "%s: not a plain file.\n", local);
641 code = -1;
642 goto cleanupsend;
643 }
644 filesize = st.st_size;
645 }
646 if (initconn()) {
647 code = -1;
648 goto cleanupsend;
649 }
650 if (sigsetjmp(xferabort, 1))
651 goto abort;
652
653 if (restart_point &&
654 (strcmp(cmd, "STOR") == 0 || strcmp(cmd, "APPE") == 0)) {
655 int rc;
656
657 rc = -1;
658 switch (curtype) {
659 case TYPE_A:
660 rc = fseeko(fin, restart_point, SEEK_SET);
661 break;
662 case TYPE_I:
663 case TYPE_L:
664 rc = lseek(fileno(fin), restart_point, SEEK_SET);
665 break;
666 }
667 if (rc < 0) {
668 warn("Can't seek to restart `%s'", local);
669 goto cleanupsend;
670 }
671 if (command("REST " LLF, (LLT)restart_point) != CONTINUE)
672 goto cleanupsend;
673 lmode = "r+";
674 }
675 if (remote) {
676 if (command("%s %s", cmd, remote) != PRELIM)
677 goto cleanupsend;
678 } else {
679 if (command("%s", cmd) != PRELIM)
680 goto cleanupsend;
681 }
682 dirchange = 1;
683 dout = dataconn(lmode);
684 if (dout == NULL)
685 goto abort;
686
687 if (sndbuf_size > bufsize) {
688 if (buf)
689 (void)free(buf);
690 bufsize = sndbuf_size;
691 buf = ftp_malloc(bufsize);
692 }
693
694 progressmeter(-1);
695 oldintp = xsignal(SIGPIPE, SIG_IGN);
696
697 switch (curtype) {
698
699 case TYPE_I:
700 case TYPE_L:
701 if (rate_put) { /* rate limited */
702 while (1) {
703 struct timeval then, now, td;
704 off_t bufrem;
705
706 (void)gettimeofday(&then, NULL);
707 errno = c = d = 0;
708 bufrem = rate_put;
709 while (bufrem > 0) {
710 if ((c = read(fileno(fin), buf,
711 MIN(bufsize, bufrem))) <= 0)
712 goto senddone;
713 bytes += c;
714 bufrem -= c;
715 for (bufp = buf; c > 0;
716 c -= d, bufp += d)
717 if ((d = write(fileno(dout),
718 bufp, c)) <= 0)
719 break;
720 if (d < 0)
721 goto senddone;
722 if (hash &&
723 (!progress || filesize < 0) ) {
724 while (bytes >= hashbytes) {
725 (void)putc('#', ttyout);
726 hashbytes += mark;
727 }
728 (void)fflush(ttyout);
729 }
730 }
731 while (1) {
732 (void)gettimeofday(&now, NULL);
733 timersub(&now, &then, &td);
734 if (td.tv_sec > 0)
735 break;
736 usleep(1000000 - td.tv_usec);
737 }
738 }
739 } else { /* simpler/faster; no rate limit */
740 while (1) {
741 errno = c = d = 0;
742 if ((c = read(fileno(fin), buf, bufsize)) <= 0)
743 goto senddone;
744 bytes += c;
745 for (bufp = buf; c > 0; c -= d, bufp += d)
746 if ((d = write(fileno(dout), bufp, c))
747 <= 0)
748 break;
749 if (d < 0)
750 goto senddone;
751 if (hash && (!progress || filesize < 0) ) {
752 while (bytes >= hashbytes) {
753 (void)putc('#', ttyout);
754 hashbytes += mark;
755 }
756 (void)fflush(ttyout);
757 }
758 }
759 }
760 senddone:
761 if (hash && (!progress || filesize < 0) && bytes > 0) {
762 if (bytes < mark)
763 (void)putc('#', ttyout);
764 (void)putc('\n', ttyout);
765 }
766 if (c < 0)
767 warn("Reading `%s'", local);
768 if (d < 0) {
769 if (errno != EPIPE)
770 warn("Writing to network");
771 bytes = -1;
772 }
773 break;
774
775 case TYPE_A:
776 while ((c = getc(fin)) != EOF) {
777 if (c == '\n') {
778 while (hash && (!progress || filesize < 0) &&
779 (bytes >= hashbytes)) {
780 (void)putc('#', ttyout);
781 (void)fflush(ttyout);
782 hashbytes += mark;
783 }
784 if (ferror(dout))
785 break;
786 (void)putc('\r', dout);
787 bytes++;
788 }
789 (void)putc(c, dout);
790 bytes++;
791 #if 0 /* this violates RFC */
792 if (c == '\r') {
793 (void)putc('\0', dout);
794 bytes++;
795 }
796 #endif
797 }
798 if (hash && (!progress || filesize < 0)) {
799 if (bytes < hashbytes)
800 (void)putc('#', ttyout);
801 (void)putc('\n', ttyout);
802 }
803 if (ferror(fin))
804 warn("Reading `%s'", local);
805 if (ferror(dout)) {
806 if (errno != EPIPE)
807 warn("Writing to network");
808 bytes = -1;
809 }
810 break;
811 }
812
813 progressmeter(1);
814 if (closefunc != NULL) {
815 (*closefunc)(fin);
816 fin = NULL;
817 }
818 (void)fclose(dout);
819 dout = NULL;
820 (void)getreply(0);
821 if (bytes > 0)
822 ptransfer(0);
823 goto cleanupsend;
824
825 abort:
826 (void)xsignal(SIGINT, oldintr);
827 oldintr = NULL;
828 if (!cpend) {
829 code = -1;
830 goto cleanupsend;
831 }
832 if (data >= 0) {
833 (void)close(data);
834 data = -1;
835 }
836 if (dout) {
837 (void)fclose(dout);
838 dout = NULL;
839 }
840 (void)getreply(0);
841 code = -1;
842 if (bytes > 0)
843 ptransfer(0);
844
845 cleanupsend:
846 if (oldintr)
847 (void)xsignal(SIGINT, oldintr);
848 if (oldintp)
849 (void)xsignal(SIGPIPE, oldintp);
850 if (data >= 0) {
851 (void)close(data);
852 data = -1;
853 }
854 if (closefunc != NULL && fin != NULL)
855 (*closefunc)(fin);
856 if (dout)
857 (void)fclose(dout);
858 progress = oprogress;
859 restart_point = 0;
860 bytes = 0;
861 }
862
863 void
864 recvrequest(const char *cmd, const char *volatile local, const char *remote,
865 const char *lmode, int printnames, int ignorespecial)
866 {
867 FILE *volatile fout;
868 FILE *volatile din;
869 int (*volatile closefunc)(FILE *);
870 sigfunc volatile oldintr;
871 sigfunc volatile oldintp;
872 int c, d;
873 int volatile is_retr;
874 int volatile tcrflag;
875 int volatile bare_lfs;
876 static size_t bufsize;
877 static char *buf;
878 off_t volatile hashbytes;
879 struct stat st;
880 time_t mtime;
881 struct timeval tval[2];
882 int oprogress;
883 int opreserve;
884
885 fout = NULL;
886 din = NULL;
887 hashbytes = mark;
888 direction = "received";
889 bytes = 0;
890 bare_lfs = 0;
891 filesize = -1;
892 oprogress = progress;
893 opreserve = preserve;
894 is_retr = (strcmp(cmd, "RETR") == 0);
895 if (is_retr && verbose && printnames) {
896 if (ignorespecial || *local != '-')
897 fprintf(ttyout, "local: %s ", local);
898 if (remote)
899 fprintf(ttyout, "remote: %s\n", remote);
900 }
901 if (proxy && is_retr) {
902 proxtrans(cmd, local, remote);
903 return;
904 }
905 closefunc = NULL;
906 oldintr = NULL;
907 oldintp = NULL;
908 tcrflag = !crflag && is_retr;
909 if (sigsetjmp(xferabort, 1)) {
910 while (cpend)
911 (void)getreply(0);
912 code = -1;
913 goto cleanuprecv;
914 }
915 (void)xsignal(SIGQUIT, psummary);
916 oldintr = xsignal(SIGINT, abortxfer);
917 if (ignorespecial || (strcmp(local, "-") && *local != '|')) {
918 if (access(local, W_OK) < 0) {
919 char *dir = strrchr(local, '/');
920
921 if (errno != ENOENT && errno != EACCES) {
922 warn("Can't access `%s'", local);
923 code = -1;
924 goto cleanuprecv;
925 }
926 if (dir != NULL)
927 *dir = 0;
928 d = access(dir == local ? "/" :
929 dir ? local : ".", W_OK);
930 if (dir != NULL)
931 *dir = '/';
932 if (d < 0) {
933 warn("Can't access `%s'", local);
934 code = -1;
935 goto cleanuprecv;
936 }
937 if (!runique && errno == EACCES &&
938 chmod(local, (S_IRUSR|S_IWUSR)) < 0) {
939 warn("Can't chmod `%s'", local);
940 code = -1;
941 goto cleanuprecv;
942 }
943 if (runique && errno == EACCES &&
944 (local = gunique(local)) == NULL) {
945 code = -1;
946 goto cleanuprecv;
947 }
948 }
949 else if (runique && (local = gunique(local)) == NULL) {
950 code = -1;
951 goto cleanuprecv;
952 }
953 }
954 if (!is_retr) {
955 if (curtype != TYPE_A)
956 changetype(TYPE_A, 0);
957 } else {
958 if (curtype != type)
959 changetype(type, 0);
960 filesize = remotesize(remote, 0);
961 if (code == 421 || code == -1)
962 goto cleanuprecv;
963 }
964 if (initconn()) {
965 code = -1;
966 goto cleanuprecv;
967 }
968 if (sigsetjmp(xferabort, 1))
969 goto abort;
970 if (is_retr && restart_point &&
971 command("REST " LLF, (LLT) restart_point) != CONTINUE)
972 goto cleanuprecv;
973 if (! EMPTYSTRING(remote)) {
974 if (command("%s %s", cmd, remote) != PRELIM)
975 goto cleanuprecv;
976 } else {
977 if (command("%s", cmd) != PRELIM)
978 goto cleanuprecv;
979 }
980 din = dataconn("r");
981 if (din == NULL)
982 goto abort;
983 if (!ignorespecial && strcmp(local, "-") == 0) {
984 fout = stdout;
985 progress = 0;
986 preserve = 0;
987 } else if (!ignorespecial && *local == '|') {
988 oldintp = xsignal(SIGPIPE, SIG_IGN);
989 fout = popen(local + 1, "w");
990 if (fout == NULL) {
991 warn("Can't execute `%s'", local+1);
992 goto abort;
993 }
994 progress = 0;
995 preserve = 0;
996 closefunc = pclose;
997 } else {
998 fout = fopen(local, lmode);
999 if (fout == NULL) {
1000 warn("Can't open `%s'", local);
1001 goto abort;
1002 }
1003 closefunc = fclose;
1004 }
1005
1006 if (fstat(fileno(fout), &st) != -1 && !S_ISREG(st.st_mode)) {
1007 progress = 0;
1008 preserve = 0;
1009 }
1010 if (rcvbuf_size > bufsize) {
1011 if (buf)
1012 (void)free(buf);
1013 bufsize = rcvbuf_size;
1014 buf = ftp_malloc(bufsize);
1015 }
1016
1017 progressmeter(-1);
1018
1019 switch (curtype) {
1020
1021 case TYPE_I:
1022 case TYPE_L:
1023 if (is_retr && restart_point &&
1024 lseek(fileno(fout), restart_point, SEEK_SET) < 0) {
1025 warn("Can't seek to restart `%s'", local);
1026 goto cleanuprecv;
1027 }
1028 if (rate_get) { /* rate limiting */
1029 while (1) {
1030 struct timeval then, now, td;
1031 off_t bufrem;
1032
1033 (void)gettimeofday(&then, NULL);
1034 errno = c = d = 0;
1035 for (bufrem = rate_get; bufrem > 0; ) {
1036 if ((c = read(fileno(din), buf,
1037 MIN(bufsize, bufrem))) <= 0)
1038 goto recvdone;
1039 bytes += c;
1040 bufrem -=c;
1041 if ((d = write(fileno(fout), buf, c))
1042 != c)
1043 goto recvdone;
1044 if (hash &&
1045 (!progress || filesize < 0)) {
1046 while (bytes >= hashbytes) {
1047 (void)putc('#', ttyout);
1048 hashbytes += mark;
1049 }
1050 (void)fflush(ttyout);
1051 }
1052 }
1053 /* sleep until time is up */
1054 while (1) {
1055 (void)gettimeofday(&now, NULL);
1056 timersub(&now, &then, &td);
1057 if (td.tv_sec > 0)
1058 break;
1059 usleep(1000000 - td.tv_usec);
1060 }
1061 }
1062 } else { /* faster code (no limiting) */
1063 while (1) {
1064 errno = c = d = 0;
1065 if ((c = read(fileno(din), buf, bufsize)) <= 0)
1066 goto recvdone;
1067 bytes += c;
1068 if ((d = write(fileno(fout), buf, c)) != c)
1069 goto recvdone;
1070 if (hash && (!progress || filesize < 0)) {
1071 while (bytes >= hashbytes) {
1072 (void)putc('#', ttyout);
1073 hashbytes += mark;
1074 }
1075 (void)fflush(ttyout);
1076 }
1077 }
1078 }
1079 recvdone:
1080 if (hash && (!progress || filesize < 0) && bytes > 0) {
1081 if (bytes < mark)
1082 (void)putc('#', ttyout);
1083 (void)putc('\n', ttyout);
1084 }
1085 if (c < 0) {
1086 if (errno != EPIPE)
1087 warn("Reading from network");
1088 bytes = -1;
1089 }
1090 if (d < c) {
1091 if (d < 0)
1092 warn("Writing `%s'", local);
1093 else
1094 warnx("Writing `%s': short write", local);
1095 }
1096 break;
1097
1098 case TYPE_A:
1099 if (is_retr && restart_point) {
1100 int ch;
1101 off_t i;
1102
1103 if (fseeko(fout, (off_t)0, SEEK_SET) < 0)
1104 goto done;
1105 for (i = 0; i++ < restart_point;) {
1106 if ((ch = getc(fout)) == EOF)
1107 goto done;
1108 if (ch == '\n')
1109 i++;
1110 }
1111 if (fseeko(fout, (off_t)0, SEEK_CUR) < 0) {
1112 done:
1113 warn("Can't seek to restart `%s'", local);
1114 goto cleanuprecv;
1115 }
1116 }
1117 while ((c = getc(din)) != EOF) {
1118 if (c == '\n')
1119 bare_lfs++;
1120 while (c == '\r') {
1121 while (hash && (!progress || filesize < 0) &&
1122 (bytes >= hashbytes)) {
1123 (void)putc('#', ttyout);
1124 (void)fflush(ttyout);
1125 hashbytes += mark;
1126 }
1127 bytes++;
1128 if ((c = getc(din)) != '\n' || tcrflag) {
1129 if (ferror(fout))
1130 goto break2;
1131 (void)putc('\r', fout);
1132 if (c == '\0') {
1133 bytes++;
1134 goto contin2;
1135 }
1136 if (c == EOF)
1137 goto contin2;
1138 }
1139 }
1140 (void)putc(c, fout);
1141 bytes++;
1142 contin2: ;
1143 }
1144 break2:
1145 if (hash && (!progress || filesize < 0)) {
1146 if (bytes < hashbytes)
1147 (void)putc('#', ttyout);
1148 (void)putc('\n', ttyout);
1149 }
1150 if (ferror(din)) {
1151 if (errno != EPIPE)
1152 warn("Reading from network");
1153 bytes = -1;
1154 }
1155 if (ferror(fout))
1156 warn("Writing `%s'", local);
1157 break;
1158 }
1159
1160 progressmeter(1);
1161 if (closefunc != NULL) {
1162 (*closefunc)(fout);
1163 fout = NULL;
1164 }
1165 (void)fclose(din);
1166 din = NULL;
1167 (void)getreply(0);
1168 if (bare_lfs) {
1169 fprintf(ttyout,
1170 "WARNING! %d bare linefeeds received in ASCII mode.\n",
1171 bare_lfs);
1172 fputs("File may not have transferred correctly.\n", ttyout);
1173 }
1174 if (bytes >= 0 && is_retr) {
1175 if (bytes > 0)
1176 ptransfer(0);
1177 if (preserve && (closefunc == fclose)) {
1178 mtime = remotemodtime(remote, 0);
1179 if (mtime != -1) {
1180 (void)gettimeofday(&tval[0], NULL);
1181 tval[1].tv_sec = mtime;
1182 tval[1].tv_usec = 0;
1183 if (utimes(local, tval) == -1) {
1184 fprintf(ttyout,
1185 "Can't change modification time on %s to %s",
1186 local, asctime(localtime(&mtime)));
1187 }
1188 }
1189 }
1190 }
1191 goto cleanuprecv;
1192
1193 abort:
1194 /*
1195 * abort using RFC 959 recommended IP,SYNC sequence
1196 */
1197 if (! sigsetjmp(xferabort, 1)) {
1198 /* this is the first call */
1199 (void)xsignal(SIGINT, abort_squared);
1200 if (!cpend) {
1201 code = -1;
1202 goto cleanuprecv;
1203 }
1204 abort_remote(din);
1205 }
1206 code = -1;
1207 if (bytes > 0)
1208 ptransfer(0);
1209
1210 cleanuprecv:
1211 if (oldintr)
1212 (void)xsignal(SIGINT, oldintr);
1213 if (oldintp)
1214 (void)xsignal(SIGPIPE, oldintp);
1215 if (data >= 0) {
1216 (void)close(data);
1217 data = -1;
1218 }
1219 if (closefunc != NULL && fout != NULL)
1220 (*closefunc)(fout);
1221 if (din)
1222 (void)fclose(din);
1223 progress = oprogress;
1224 preserve = opreserve;
1225 bytes = 0;
1226 }
1227
1228 /*
1229 * Need to start a listen on the data channel before we send the command,
1230 * otherwise the server's connect may fail.
1231 */
1232 int
1233 initconn(void)
1234 {
1235 char *p, *a;
1236 int result, tmpno = 0;
1237 int on = 1;
1238 int error;
1239 unsigned int addr[16], port[2];
1240 unsigned int af, hal, pal;
1241 socklen_t len;
1242 char *pasvcmd = NULL;
1243 int overbose;
1244
1245 #ifdef INET6
1246 #ifndef NO_DEBUG
1247 if (myctladdr.su_family == AF_INET6 && ftp_debug &&
1248 (IN6_IS_ADDR_LINKLOCAL(&myctladdr.si_su.su_sin6.sin6_addr) ||
1249 IN6_IS_ADDR_SITELOCAL(&myctladdr.si_su.su_sin6.sin6_addr))) {
1250 warnx("Use of scoped addresses can be troublesome");
1251 }
1252 #endif
1253 #endif
1254
1255 reinit:
1256 if (passivemode) {
1257 data_addr = myctladdr;
1258 data = socket(data_addr.su_family, SOCK_STREAM, 0);
1259 if (data < 0) {
1260 warn("Can't create socket for data connection");
1261 return (1);
1262 }
1263 if ((options & SO_DEBUG) &&
1264 setsockopt(data, SOL_SOCKET, SO_DEBUG,
1265 (void *)&on, sizeof(on)) == -1) {
1266 DWARN("setsockopt %s (ignored)", "SO_DEBUG");
1267 }
1268 result = COMPLETE + 1;
1269 switch (data_addr.su_family) {
1270 case AF_INET:
1271 if (epsv4 && !epsv4bad) {
1272 pasvcmd = "EPSV";
1273 overbose = verbose;
1274 if (ftp_debug == 0)
1275 verbose = -1;
1276 result = command("EPSV");
1277 verbose = overbose;
1278 if (verbose &&
1279 (result == COMPLETE || !connected))
1280 fprintf(ttyout, "%s\n", reply_string);
1281 if (!connected)
1282 return (1);
1283 /*
1284 * this code is to be friendly with broken
1285 * BSDI ftpd
1286 */
1287 if (code / 10 == 22 && code != 229) {
1288 fputs(
1289 "wrong server: return code must be 229\n",
1290 ttyout);
1291 result = COMPLETE + 1;
1292 }
1293 if (result != COMPLETE) {
1294 epsv4bad = 1;
1295 DPRINTF("disabling epsv4 for this "
1296 "connection\n");
1297 }
1298 }
1299 if (result != COMPLETE) {
1300 pasvcmd = "PASV";
1301 result = command("PASV");
1302 if (!connected)
1303 return (1);
1304 }
1305 break;
1306 #ifdef INET6
1307 case AF_INET6:
1308 pasvcmd = "EPSV";
1309 overbose = verbose;
1310 if (ftp_debug == 0)
1311 verbose = -1;
1312 result = command("EPSV");
1313 verbose = overbose;
1314 if (verbose && (result == COMPLETE || !connected))
1315 fprintf(ttyout, "%s\n", reply_string);
1316 if (!connected)
1317 return (1);
1318 /* this code is to be friendly with broken BSDI ftpd */
1319 if (code / 10 == 22 && code != 229) {
1320 fputs(
1321 "wrong server: return code must be 229\n",
1322 ttyout);
1323 result = COMPLETE + 1;
1324 }
1325 if (result != COMPLETE) {
1326 pasvcmd = "LPSV";
1327 result = command("LPSV");
1328 }
1329 if (!connected)
1330 return (1);
1331 break;
1332 #endif
1333 default:
1334 result = COMPLETE + 1;
1335 break;
1336 }
1337 if (result != COMPLETE) {
1338 if (activefallback) {
1339 (void)close(data);
1340 data = -1;
1341 passivemode = 0;
1342 #if 0
1343 activefallback = 0;
1344 #endif
1345 goto reinit;
1346 }
1347 fputs("Passive mode refused.\n", ttyout);
1348 goto bad;
1349 }
1350
1351 #define pack2(var, off) \
1352 (((var[(off) + 0] & 0xff) << 8) | ((var[(off) + 1] & 0xff) << 0))
1353 #define pack4(var, off) \
1354 (((var[(off) + 0] & 0xff) << 24) | ((var[(off) + 1] & 0xff) << 16) | \
1355 ((var[(off) + 2] & 0xff) << 8) | ((var[(off) + 3] & 0xff) << 0))
1356 #define UC(b) (((int)b)&0xff)
1357
1358 /*
1359 * What we've got at this point is a string of comma separated
1360 * one-byte unsigned integer values, separated by commas.
1361 */
1362 if (strcmp(pasvcmd, "PASV") == 0) {
1363 if (data_addr.su_family != AF_INET) {
1364 fputs(
1365 "Passive mode AF mismatch. Shouldn't happen!\n", ttyout);
1366 error = 1;
1367 goto bad;
1368 }
1369 if (code / 10 == 22 && code != 227) {
1370 fputs("wrong server: return code must be 227\n",
1371 ttyout);
1372 error = 1;
1373 goto bad;
1374 }
1375 error = sscanf(pasv, "%u,%u,%u,%u,%u,%u",
1376 &addr[0], &addr[1], &addr[2], &addr[3],
1377 &port[0], &port[1]);
1378 if (error != 6) {
1379 fputs(
1380 "Passive mode address scan failure. Shouldn't happen!\n", ttyout);
1381 error = 1;
1382 goto bad;
1383 }
1384 error = 0;
1385 memset(&data_addr, 0, sizeof(data_addr));
1386 data_addr.su_family = AF_INET;
1387 data_addr.su_len = sizeof(struct sockaddr_in);
1388 data_addr.si_su.su_sin.sin_addr.s_addr =
1389 htonl(pack4(addr, 0));
1390 data_addr.su_port = htons(pack2(port, 0));
1391 } else if (strcmp(pasvcmd, "LPSV") == 0) {
1392 if (code / 10 == 22 && code != 228) {
1393 fputs("wrong server: return code must be 228\n",
1394 ttyout);
1395 error = 1;
1396 goto bad;
1397 }
1398 switch (data_addr.su_family) {
1399 case AF_INET:
1400 error = sscanf(pasv,
1401 "%u,%u,%u,%u,%u,%u,%u,%u,%u",
1402 &af, &hal,
1403 &addr[0], &addr[1], &addr[2], &addr[3],
1404 &pal, &port[0], &port[1]);
1405 if (error != 9) {
1406 fputs(
1407 "Passive mode address scan failure. Shouldn't happen!\n", ttyout);
1408 error = 1;
1409 goto bad;
1410 }
1411 if (af != 4 || hal != 4 || pal != 2) {
1412 fputs(
1413 "Passive mode AF mismatch. Shouldn't happen!\n", ttyout);
1414 error = 1;
1415 goto bad;
1416 }
1417
1418 error = 0;
1419 memset(&data_addr, 0, sizeof(data_addr));
1420 data_addr.su_family = AF_INET;
1421 data_addr.su_len = sizeof(struct sockaddr_in);
1422 data_addr.si_su.su_sin.sin_addr.s_addr =
1423 htonl(pack4(addr, 0));
1424 data_addr.su_port = htons(pack2(port, 0));
1425 break;
1426 #ifdef INET6
1427 case AF_INET6:
1428 error = sscanf(pasv,
1429 "%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u",
1430 &af, &hal,
1431 &addr[0], &addr[1], &addr[2], &addr[3],
1432 &addr[4], &addr[5], &addr[6], &addr[7],
1433 &addr[8], &addr[9], &addr[10],
1434 &addr[11], &addr[12], &addr[13],
1435 &addr[14], &addr[15],
1436 &pal, &port[0], &port[1]);
1437 if (error != 21) {
1438 fputs(
1439 "Passive mode address scan failure. Shouldn't happen!\n", ttyout);
1440 error = 1;
1441 goto bad;
1442 }
1443 if (af != 6 || hal != 16 || pal != 2) {
1444 fputs(
1445 "Passive mode AF mismatch. Shouldn't happen!\n", ttyout);
1446 error = 1;
1447 goto bad;
1448 }
1449
1450 error = 0;
1451 memset(&data_addr, 0, sizeof(data_addr));
1452 data_addr.su_family = AF_INET6;
1453 data_addr.su_len = sizeof(struct sockaddr_in6);
1454 {
1455 int i;
1456 for (i = 0; i < sizeof(struct in6_addr); i++) {
1457 data_addr.si_su.su_sin6.sin6_addr.s6_addr[i] =
1458 UC(addr[i]);
1459 }
1460 }
1461 data_addr.su_port = htons(pack2(port, 0));
1462 break;
1463 #endif
1464 default:
1465 error = 1;
1466 }
1467 } else if (strcmp(pasvcmd, "EPSV") == 0) {
1468 char delim[4];
1469
1470 port[0] = 0;
1471 if (code / 10 == 22 && code != 229) {
1472 fputs("wrong server: return code must be 229\n",
1473 ttyout);
1474 error = 1;
1475 goto bad;
1476 }
1477 if (sscanf(pasv, "%c%c%c%d%c", &delim[0],
1478 &delim[1], &delim[2], &port[1],
1479 &delim[3]) != 5) {
1480 fputs("parse error!\n", ttyout);
1481 error = 1;
1482 goto bad;
1483 }
1484 if (delim[0] != delim[1] || delim[0] != delim[2]
1485 || delim[0] != delim[3]) {
1486 fputs("parse error!\n", ttyout);
1487 error = 1;
1488 goto bad;
1489 }
1490 data_addr = hisctladdr;
1491 data_addr.su_port = htons(port[1]);
1492 } else
1493 goto bad;
1494
1495 if (ftp_connect(data, (struct sockaddr *)&data_addr.si_su,
1496 data_addr.su_len) < 0) {
1497 if (activefallback) {
1498 (void)close(data);
1499 data = -1;
1500 passivemode = 0;
1501 #if 0
1502 activefallback = 0;
1503 #endif
1504 goto reinit;
1505 }
1506 goto bad;
1507 }
1508 #ifdef IPTOS_THROUGHPUT
1509 if (data_addr.su_family == AF_INET) {
1510 on = IPTOS_THROUGHPUT;
1511 if (setsockopt(data, IPPROTO_IP, IP_TOS,
1512 (void *)&on, sizeof(on)) == -1) {
1513 DWARN("setsockopt %s (ignored)",
1514 "IPTOS_THROUGHPUT");
1515 }
1516 }
1517 #endif
1518 return (0);
1519 }
1520
1521 noport:
1522 data_addr = myctladdr;
1523 if (sendport)
1524 data_addr.su_port = 0; /* let system pick one */
1525 if (data != -1)
1526 (void)close(data);
1527 data = socket(data_addr.su_family, SOCK_STREAM, 0);
1528 if (data < 0) {
1529 warn("Can't create socket for data connection");
1530 if (tmpno)
1531 sendport = 1;
1532 return (1);
1533 }
1534 if (!sendport)
1535 if (setsockopt(data, SOL_SOCKET, SO_REUSEADDR,
1536 (void *)&on, sizeof(on)) == -1) {
1537 warn("Can't set SO_REUSEADDR on data connection");
1538 goto bad;
1539 }
1540 if (bind(data, (struct sockaddr *)&data_addr.si_su,
1541 data_addr.su_len) < 0) {
1542 warn("Can't bind for data connection");
1543 goto bad;
1544 }
1545 if ((options & SO_DEBUG) &&
1546 setsockopt(data, SOL_SOCKET, SO_DEBUG,
1547 (void *)&on, sizeof(on)) == -1) {
1548 DWARN("setsockopt %s (ignored)", "SO_DEBUG");
1549 }
1550 len = sizeof(data_addr.si_su);
1551 memset((char *)&data_addr, 0, sizeof (data_addr));
1552 if (getsockname(data, (struct sockaddr *)&data_addr.si_su, &len) == -1) {
1553 warn("Can't determine my address of data connection");
1554 goto bad;
1555 }
1556 data_addr.su_len = len;
1557 if (ftp_listen(data, 1) < 0)
1558 warn("Can't listen to data connection");
1559
1560 if (sendport) {
1561 char hname[NI_MAXHOST], sname[NI_MAXSERV];
1562 int af;
1563 struct sockinet tmp;
1564
1565 switch (data_addr.su_family) {
1566 case AF_INET:
1567 if (!epsv4 || epsv4bad) {
1568 result = COMPLETE + 1;
1569 break;
1570 }
1571 /* FALLTHROUGH */
1572 #ifdef INET6
1573 case AF_INET6:
1574 #endif
1575 af = (data_addr.su_family == AF_INET) ? 1 : 2;
1576 tmp = data_addr;
1577 #ifdef INET6
1578 if (tmp.su_family == AF_INET6)
1579 tmp.si_su.su_sin6.sin6_scope_id = 0;
1580 #endif
1581 if (getnameinfo((struct sockaddr *)&tmp.si_su,
1582 tmp.su_len, hname, sizeof(hname), sname,
1583 sizeof(sname), NI_NUMERICHOST | NI_NUMERICSERV)) {
1584 result = ERROR;
1585 } else {
1586 overbose = verbose;
1587 if (ftp_debug == 0)
1588 verbose = -1;
1589 result = command("EPRT |%d|%s|%s|", af, hname,
1590 sname);
1591 verbose = overbose;
1592 if (verbose &&
1593 (result == COMPLETE || !connected))
1594 fprintf(ttyout, "%s\n", reply_string);
1595 if (!connected)
1596 return (1);
1597 if (result != COMPLETE) {
1598 epsv4bad = 1;
1599 DPRINTF("disabling epsv4 for this "
1600 "connection\n");
1601 }
1602 }
1603 break;
1604 default:
1605 result = COMPLETE + 1;
1606 break;
1607 }
1608 if (result == COMPLETE)
1609 goto skip_port;
1610
1611 switch (data_addr.su_family) {
1612 case AF_INET:
1613 a = (char *)&data_addr.si_su.su_sin.sin_addr;
1614 p = (char *)&data_addr.su_port;
1615 result = command("PORT %d,%d,%d,%d,%d,%d",
1616 UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
1617 UC(p[0]), UC(p[1]));
1618 break;
1619 #ifdef INET6
1620 case AF_INET6:
1621 a = (char *)&data_addr.si_su.su_sin6.sin6_addr;
1622 p = (char *)&data_addr.su_port;
1623 result = command(
1624 "LPRT %d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
1625 6, 16,
1626 UC(a[0]),UC(a[1]),UC(a[2]),UC(a[3]),
1627 UC(a[4]),UC(a[5]),UC(a[6]),UC(a[7]),
1628 UC(a[8]),UC(a[9]),UC(a[10]),UC(a[11]),
1629 UC(a[12]),UC(a[13]),UC(a[14]),UC(a[15]),
1630 2, UC(p[0]), UC(p[1]));
1631 break;
1632 #endif
1633 default:
1634 result = COMPLETE + 1; /* xxx */
1635 }
1636 if (!connected)
1637 return (1);
1638 skip_port:
1639
1640 if (result == ERROR && sendport == -1) {
1641 sendport = 0;
1642 tmpno = 1;
1643 goto noport;
1644 }
1645 return (result != COMPLETE);
1646 }
1647 if (tmpno)
1648 sendport = 1;
1649 #ifdef IPTOS_THROUGHPUT
1650 if (data_addr.su_family == AF_INET) {
1651 on = IPTOS_THROUGHPUT;
1652 if (setsockopt(data, IPPROTO_IP, IP_TOS,
1653 (void *)&on, sizeof(on)) == -1)
1654 DWARN("setsockopt %s (ignored)", "IPTOS_THROUGHPUT");
1655 }
1656 #endif
1657 return (0);
1658 bad:
1659 (void)close(data);
1660 data = -1;
1661 if (tmpno)
1662 sendport = 1;
1663 return (1);
1664 }
1665
1666 FILE *
1667 dataconn(const char *lmode)
1668 {
1669 struct sockinet from;
1670 int s, flags, rv, timeout;
1671 struct timeval endtime, now, td;
1672 struct pollfd pfd[1];
1673 socklen_t fromlen;
1674
1675 if (passivemode) /* passive data connection */
1676 return (fdopen(data, lmode));
1677
1678 /* active mode data connection */
1679
1680 if ((flags = fcntl(data, F_GETFL, 0)) == -1)
1681 goto dataconn_failed; /* get current socket flags */
1682 if (fcntl(data, F_SETFL, flags | O_NONBLOCK) == -1)
1683 goto dataconn_failed; /* set non-blocking connect */
1684
1685 /* NOTE: we now must restore socket flags on successful exit */
1686
1687 /* limit time waiting on listening socket */
1688 pfd[0].fd = data;
1689 pfd[0].events = POLLIN;
1690 (void)gettimeofday(&endtime, NULL); /* determine end time */
1691 endtime.tv_sec += (quit_time > 0) ? quit_time: 60;
1692 /* without -q, default to 60s */
1693 do {
1694 (void)gettimeofday(&now, NULL);
1695 timersub(&endtime, &now, &td);
1696 timeout = td.tv_sec * 1000 + td.tv_usec/1000;
1697 if (timeout < 0)
1698 timeout = 0;
1699 rv = ftp_poll(pfd, 1, timeout);
1700 } while (rv == -1 && errno == EINTR); /* loop until poll ! EINTR */
1701 if (rv == -1) {
1702 warn("Can't poll waiting before accept");
1703 goto dataconn_failed;
1704 }
1705 if (rv == 0) {
1706 warnx("Poll timeout waiting before accept");
1707 goto dataconn_failed;
1708 }
1709
1710 /* (non-blocking) accept the connection */
1711 fromlen = myctladdr.su_len;
1712 do {
1713 s = accept(data, (struct sockaddr *) &from.si_su, &fromlen);
1714 } while (s == -1 && errno == EINTR); /* loop until accept ! EINTR */
1715 if (s == -1) {
1716 warn("Can't accept data connection");
1717 goto dataconn_failed;
1718 }
1719
1720 (void)close(data);
1721 data = s;
1722 if (fcntl(data, F_SETFL, flags) == -1) /* restore socket flags */
1723 goto dataconn_failed;
1724
1725 #ifdef IPTOS_THROUGHPUT
1726 if (from.su_family == AF_INET) {
1727 int tos = IPTOS_THROUGHPUT;
1728 if (setsockopt(s, IPPROTO_IP, IP_TOS,
1729 (void *)&tos, sizeof(tos)) == -1) {
1730 DWARN("setsockopt %s (ignored)", "IPTOS_THROUGHPUT");
1731 }
1732 }
1733 #endif
1734 return (fdopen(data, lmode));
1735
1736 dataconn_failed:
1737 (void)close(data);
1738 data = -1;
1739 return (NULL);
1740 }
1741
1742 void
1743 psabort(int notused)
1744 {
1745 int oerrno = errno;
1746
1747 sigint_raised = 1;
1748 alarmtimer(0);
1749 abrtflag++;
1750 errno = oerrno;
1751 }
1752
1753 void
1754 pswitch(int flag)
1755 {
1756 sigfunc oldintr;
1757 static struct comvars {
1758 int connect;
1759 char name[MAXHOSTNAMELEN];
1760 struct sockinet mctl;
1761 struct sockinet hctl;
1762 FILE *in;
1763 FILE *out;
1764 int tpe;
1765 int curtpe;
1766 int cpnd;
1767 int sunqe;
1768 int runqe;
1769 int mcse;
1770 int ntflg;
1771 char nti[17];
1772 char nto[17];
1773 int mapflg;
1774 char mi[MAXPATHLEN];
1775 char mo[MAXPATHLEN];
1776 } proxstruct, tmpstruct;
1777 struct comvars *ip, *op;
1778
1779 abrtflag = 0;
1780 oldintr = xsignal(SIGINT, psabort);
1781 if (flag) {
1782 if (proxy)
1783 return;
1784 ip = &tmpstruct;
1785 op = &proxstruct;
1786 proxy++;
1787 } else {
1788 if (!proxy)
1789 return;
1790 ip = &proxstruct;
1791 op = &tmpstruct;
1792 proxy = 0;
1793 }
1794 ip->connect = connected;
1795 connected = op->connect;
1796 if (hostname)
1797 (void)strlcpy(ip->name, hostname, sizeof(ip->name));
1798 else
1799 ip->name[0] = '\0';
1800 hostname = op->name;
1801 ip->hctl = hisctladdr;
1802 hisctladdr = op->hctl;
1803 ip->mctl = myctladdr;
1804 myctladdr = op->mctl;
1805 ip->in = cin;
1806 cin = op->in;
1807 ip->out = cout;
1808 cout = op->out;
1809 ip->tpe = type;
1810 type = op->tpe;
1811 ip->curtpe = curtype;
1812 curtype = op->curtpe;
1813 ip->cpnd = cpend;
1814 cpend = op->cpnd;
1815 ip->sunqe = sunique;
1816 sunique = op->sunqe;
1817 ip->runqe = runique;
1818 runique = op->runqe;
1819 ip->mcse = mcase;
1820 mcase = op->mcse;
1821 ip->ntflg = ntflag;
1822 ntflag = op->ntflg;
1823 (void)strlcpy(ip->nti, ntin, sizeof(ip->nti));
1824 (void)strlcpy(ntin, op->nti, sizeof(ntin));
1825 (void)strlcpy(ip->nto, ntout, sizeof(ip->nto));
1826 (void)strlcpy(ntout, op->nto, sizeof(ntout));
1827 ip->mapflg = mapflag;
1828 mapflag = op->mapflg;
1829 (void)strlcpy(ip->mi, mapin, sizeof(ip->mi));
1830 (void)strlcpy(mapin, op->mi, sizeof(mapin));
1831 (void)strlcpy(ip->mo, mapout, sizeof(ip->mo));
1832 (void)strlcpy(mapout, op->mo, sizeof(mapout));
1833 (void)xsignal(SIGINT, oldintr);
1834 if (abrtflag) {
1835 abrtflag = 0;
1836 (*oldintr)(SIGINT);
1837 }
1838 }
1839
1840 void
1841 abortpt(int notused)
1842 {
1843
1844 sigint_raised = 1;
1845 alarmtimer(0);
1846 if (fromatty)
1847 write(fileno(ttyout), "\n", 1);
1848 ptabflg++;
1849 mflag = 0;
1850 abrtflag = 0;
1851 siglongjmp(ptabort, 1);
1852 }
1853
1854 void
1855 proxtrans(const char *cmd, const char *local, const char *remote)
1856 {
1857 sigfunc volatile oldintr;
1858 int prox_type, nfnd;
1859 int volatile secndflag;
1860 char *volatile cmd2;
1861
1862 oldintr = NULL;
1863 secndflag = 0;
1864 if (strcmp(cmd, "RETR"))
1865 cmd2 = "RETR";
1866 else
1867 cmd2 = runique ? "STOU" : "STOR";
1868 if ((prox_type = type) == 0) {
1869 if (unix_server && unix_proxy)
1870 prox_type = TYPE_I;
1871 else
1872 prox_type = TYPE_A;
1873 }
1874 if (curtype != prox_type)
1875 changetype(prox_type, 1);
1876 if (command("PASV") != COMPLETE) {
1877 fputs("proxy server does not support third party transfers.\n",
1878 ttyout);
1879 return;
1880 }
1881 pswitch(0);
1882 if (!connected) {
1883 fputs("No primary connection.\n", ttyout);
1884 pswitch(1);
1885 code = -1;
1886 return;
1887 }
1888 if (curtype != prox_type)
1889 changetype(prox_type, 1);
1890 if (command("PORT %s", pasv) != COMPLETE) {
1891 pswitch(1);
1892 return;
1893 }
1894 if (sigsetjmp(ptabort, 1))
1895 goto abort;
1896 oldintr = xsignal(SIGINT, abortpt);
1897 if ((restart_point &&
1898 (command("REST " LLF, (LLT) restart_point) != CONTINUE))
1899 || (command("%s %s", cmd, remote) != PRELIM)) {
1900 (void)xsignal(SIGINT, oldintr);
1901 pswitch(1);
1902 return;
1903 }
1904 sleep(2);
1905 pswitch(1);
1906 secndflag++;
1907 if ((restart_point &&
1908 (command("REST " LLF, (LLT) restart_point) != CONTINUE))
1909 || (command("%s %s", cmd2, local) != PRELIM))
1910 goto abort;
1911 ptflag++;
1912 (void)getreply(0);
1913 pswitch(0);
1914 (void)getreply(0);
1915 (void)xsignal(SIGINT, oldintr);
1916 pswitch(1);
1917 ptflag = 0;
1918 fprintf(ttyout, "local: %s remote: %s\n", local, remote);
1919 return;
1920 abort:
1921 if (sigsetjmp(xferabort, 1)) {
1922 (void)xsignal(SIGINT, oldintr);
1923 return;
1924 }
1925 (void)xsignal(SIGINT, abort_squared);
1926 ptflag = 0;
1927 if (strcmp(cmd, "RETR") && !proxy)
1928 pswitch(1);
1929 else if (!strcmp(cmd, "RETR") && proxy)
1930 pswitch(0);
1931 if (!cpend && !secndflag) { /* only here if cmd = "STOR" (proxy=1) */
1932 if (command("%s %s", cmd2, local) != PRELIM) {
1933 pswitch(0);
1934 if (cpend)
1935 abort_remote(NULL);
1936 }
1937 pswitch(1);
1938 if (ptabflg)
1939 code = -1;
1940 (void)xsignal(SIGINT, oldintr);
1941 return;
1942 }
1943 if (cpend)
1944 abort_remote(NULL);
1945 pswitch(!proxy);
1946 if (!cpend && !secndflag) { /* only if cmd = "RETR" (proxy=1) */
1947 if (command("%s %s", cmd2, local) != PRELIM) {
1948 pswitch(0);
1949 if (cpend)
1950 abort_remote(NULL);
1951 pswitch(1);
1952 if (ptabflg)
1953 code = -1;
1954 (void)xsignal(SIGINT, oldintr);
1955 return;
1956 }
1957 }
1958 if (cpend)
1959 abort_remote(NULL);
1960 pswitch(!proxy);
1961 if (cpend) {
1962 if ((nfnd = empty(cin, NULL, 10)) <= 0) {
1963 if (nfnd < 0)
1964 warn("Error aborting proxy command");
1965 if (ptabflg)
1966 code = -1;
1967 lostpeer(0);
1968 }
1969 (void)getreply(0);
1970 (void)getreply(0);
1971 }
1972 if (proxy)
1973 pswitch(0);
1974 pswitch(1);
1975 if (ptabflg)
1976 code = -1;
1977 (void)xsignal(SIGINT, oldintr);
1978 }
1979
1980 void
1981 reset(int argc, char *argv[])
1982 {
1983 int nfnd = 1;
1984
1985 if (argc == 0 && argv != NULL) {
1986 UPRINTF("usage: %s\n", argv[0]);
1987 code = -1;
1988 return;
1989 }
1990 while (nfnd > 0) {
1991 if ((nfnd = empty(cin, NULL, 0)) < 0) {
1992 warn("Error resetting connection");
1993 code = -1;
1994 lostpeer(0);
1995 } else if (nfnd)
1996 (void)getreply(0);
1997 }
1998 }
1999
2000 char *
2001 gunique(const char *local)
2002 {
2003 static char new[MAXPATHLEN];
2004 char *cp = strrchr(local, '/');
2005 int d, count=0, len;
2006 char ext = '1';
2007
2008 if (cp)
2009 *cp = '\0';
2010 d = access(cp == local ? "/" : cp ? local : ".", W_OK);
2011 if (cp)
2012 *cp = '/';
2013 if (d < 0) {
2014 warn("Can't access `%s'", local);
2015 return (NULL);
2016 }
2017 len = strlcpy(new, local, sizeof(new));
2018 cp = &new[len];
2019 *cp++ = '.';
2020 while (!d) {
2021 if (++count == 100) {
2022 fputs("runique: can't find unique file name.\n",
2023 ttyout);
2024 return (NULL);
2025 }
2026 *cp++ = ext;
2027 *cp = '\0';
2028 if (ext == '9')
2029 ext = '0';
2030 else
2031 ext++;
2032 if ((d = access(new, F_OK)) < 0)
2033 break;
2034 if (ext != '0')
2035 cp--;
2036 else if (*(cp - 2) == '.')
2037 *(cp - 1) = '1';
2038 else {
2039 *(cp - 2) = *(cp - 2) + 1;
2040 cp--;
2041 }
2042 }
2043 return (new);
2044 }
2045
2046 /*
2047 * abort_squared --
2048 * aborts abort_remote(). lostpeer() is called because if the user is
2049 * too impatient to wait or there's another problem then ftp really
2050 * needs to get back to a known state.
2051 */
2052 void
2053 abort_squared(int dummy)
2054 {
2055 char msgbuf[100];
2056 size_t len;
2057
2058 sigint_raised = 1;
2059 alarmtimer(0);
2060 len = strlcpy(msgbuf, "\nremote abort aborted; closing connection.\n",
2061 sizeof(msgbuf));
2062 write(fileno(ttyout), msgbuf, len);
2063 lostpeer(0);
2064 siglongjmp(xferabort, 1);
2065 }
2066
2067 void
2068 abort_remote(FILE *din)
2069 {
2070 char buf[BUFSIZ];
2071 int nfnd;
2072
2073 if (cout == NULL) {
2074 warnx("Lost control connection for abort");
2075 if (ptabflg)
2076 code = -1;
2077 lostpeer(0);
2078 return;
2079 }
2080 /*
2081 * send IAC in urgent mode instead of DM because 4.3BSD places oob mark
2082 * after urgent byte rather than before as is protocol now
2083 */
2084 buf[0] = IAC;
2085 buf[1] = IP;
2086 buf[2] = IAC;
2087 if (send(fileno(cout), buf, 3, MSG_OOB) != 3)
2088 warn("Can't send abort message");
2089 fprintf(cout, "%cABOR\r\n", DM);
2090 (void)fflush(cout);
2091 if ((nfnd = empty(cin, din, 10)) <= 0) {
2092 if (nfnd < 0)
2093 warn("Can't send abort message");
2094 if (ptabflg)
2095 code = -1;
2096 lostpeer(0);
2097 }
2098 if (din && (nfnd & 2)) {
2099 while (read(fileno(din), buf, BUFSIZ) > 0)
2100 continue;
2101 }
2102 if (getreply(0) == ERROR && code == 552) {
2103 /* 552 needed for nic style abort */
2104 (void)getreply(0);
2105 }
2106 (void)getreply(0);
2107 }
2108
2109 /*
2110 * Ensure that ai->ai_addr is NOT an IPv4 mapped address.
2111 * IPv4 mapped address complicates too many things in FTP
2112 * protocol handling, as FTP protocol is defined differently
2113 * between IPv4 and IPv6.
2114 *
2115 * This may not be the best way to handle this situation,
2116 * since the semantics of IPv4 mapped address is defined in
2117 * the kernel. There are configurations where we should use
2118 * IPv4 mapped address as native IPv6 address, not as
2119 * "an IPv6 address that embeds IPv4 address" (namely, SIIT).
2120 *
2121 * More complete solution would be to have an additional
2122 * getsockopt to grab "real" peername/sockname. "real"
2123 * peername/sockname will be AF_INET if IPv4 mapped address
2124 * is used to embed IPv4 address, and will be AF_INET6 if
2125 * we use it as native. What a mess!
2126 */
2127 void
2128 ai_unmapped(struct addrinfo *ai)
2129 {
2130 #ifdef INET6
2131 struct sockaddr_in6 *sin6;
2132 struct sockaddr_in sin;
2133 socklen_t len;
2134
2135 if (ai->ai_family != AF_INET6)
2136 return;
2137 if (ai->ai_addrlen != sizeof(struct sockaddr_in6) ||
2138 sizeof(sin) > ai->ai_addrlen)
2139 return;
2140 sin6 = (struct sockaddr_in6 *)ai->ai_addr;
2141 if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
2142 return;
2143
2144 memset(&sin, 0, sizeof(sin));
2145 sin.sin_family = AF_INET;
2146 len = sizeof(struct sockaddr_in);
2147 memcpy(&sin.sin_addr, &sin6->sin6_addr.s6_addr[12],
2148 sizeof(sin.sin_addr));
2149 sin.sin_port = sin6->sin6_port;
2150
2151 ai->ai_family = AF_INET;
2152 #if HAVE_SOCKADDR_SA_LEN
2153 sin.sin_len = len;
2154 #endif
2155 memcpy(ai->ai_addr, &sin, len);
2156 ai->ai_addrlen = len;
2157 #endif
2158 }
2159
2160 #ifdef NO_USAGE
2161 void
2162 xusage(void)
2163 {
2164 fputs("Usage error\n", ttyout);
2165 }
2166 #endif
2167