main.c revision 1.41 1 /* $NetBSD: main.c,v 1.41 1999/05/17 04:26:45 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 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) Copyright (c) 1985, 1989, 1993, 1994\n\
39 The Regents of the University of California. All rights reserved.\n");
40 #endif /* not lint */
41
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 10/9/94";
45 #else
46 __RCSID("$NetBSD: main.c,v 1.41 1999/05/17 04:26:45 lukem Exp $");
47 #endif
48 #endif /* not lint */
49
50 /*
51 * FTP User Program -- Command Interface.
52 */
53 #include <sys/types.h>
54 #include <sys/socket.h>
55
56 #include <err.h>
57 #include <netdb.h>
58 #include <pwd.h>
59 #include <signal.h>
60 #include <stdio.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <unistd.h>
64
65 #include "ftp_var.h"
66 #include "pathnames.h"
67
68 #define FTP_PROXY "ftp_proxy" /* env var with FTP proxy location */
69 #define HTTP_PROXY "http_proxy" /* env var with HTTP proxy location */
70 #define NO_PROXY "no_proxy" /* env var with list of non-proxied
71 * hosts, comma or space separated */
72
73 int main __P((int, char **));
74
75 int
76 main(argc, argv)
77 int argc;
78 char *argv[];
79 {
80 struct servent *sp;
81 int ch, top, rval;
82 long port;
83 struct passwd *pw = NULL;
84 char *cp, *ep, homedir[MAXPATHLEN];
85 int dumbterm;
86
87 sp = getservbyname("ftp", "tcp");
88 if (sp == 0)
89 ftpport = htons(FTP_PORT); /* good fallback */
90 else
91 ftpport = sp->s_port;
92 sp = getservbyname("http", "tcp");
93 if (sp == 0)
94 httpport = htons(HTTP_PORT); /* good fallback */
95 else
96 httpport = sp->s_port;
97 ftpproxy = getenv(FTP_PROXY);
98 httpproxy = getenv(HTTP_PROXY);
99 no_proxy = getenv(NO_PROXY);
100 gateport = 0;
101 cp = getenv("FTPSERVERPORT");
102 if (cp != NULL) {
103 port = strtol(cp, &ep, 10);
104 if (port < 1 || port > MAX_IN_PORT_T || *ep != '\0')
105 warnx("bad $FTPSERVERPORT port number: %s (ignored)",
106 cp);
107 else
108 gateport = htons(port);
109 }
110 if (gateport == 0) {
111 sp = getservbyname("ftpgate", "tcp");
112 if (sp == 0)
113 gateport = htons(GATE_PORT);
114 else
115 gateport = sp->s_port;
116 }
117 doglob = 1;
118 interactive = 1;
119 autologin = 1;
120 passivemode = 1;
121 activefallback = 1;
122 preserve = 1;
123 verbose = 0;
124 progress = 0;
125 gatemode = 0;
126 outfile = NULL;
127 restartautofetch = 0;
128 #ifndef SMALL
129 editing = 0;
130 el = NULL;
131 hist = NULL;
132 #endif
133 mark = HASHBYTES;
134 marg_sl = sl_init();
135 if ((tmpdir = getenv("TMPDIR")) == NULL)
136 tmpdir = _PATH_TMP;
137
138 /* Set default operation mode based on FTPMODE environment variable */
139 if ((cp = getenv("FTPMODE")) != NULL) {
140 if (strcmp(cp, "passive") == 0) {
141 passivemode = 1;
142 activefallback = 0;
143 } else if (strcmp(cp, "active") == 0) {
144 passivemode = 0;
145 activefallback = 0;
146 } else if (strcmp(cp, "gate") == 0) {
147 gatemode = 1;
148 } else if (strcmp(cp, "auto") == 0) {
149 passivemode = 1;
150 activefallback = 1;
151 } else
152 warnx("unknown $FTPMODE '%s'; using defaults", cp);
153 }
154
155 if (strcmp(__progname, "pftp") == 0) {
156 passivemode = 1;
157 activefallback = 0;
158 } else if (strcmp(__progname, "gate-ftp") == 0)
159 gatemode = 1;
160
161 gateserver = getenv("FTPSERVER");
162 if (gateserver == NULL || *gateserver == '\0')
163 gateserver = GATE_SERVER;
164 if (gatemode) {
165 if (*gateserver == '\0') {
166 warnx(
167 "Neither $FTPSERVER nor $GATE_SERVER is defined; disabling gate-ftp");
168 gatemode = 0;
169 }
170 }
171
172 cp = getenv("TERM");
173 if (cp == NULL || strcmp(cp, "dumb") == 0)
174 dumbterm = 1;
175 else
176 dumbterm = 0;
177 fromatty = isatty(fileno(stdin));
178 ttyout = stdout;
179 if (isatty(fileno(ttyout))) {
180 verbose = 1; /* verbose if to a tty */
181 #ifndef SMALL
182 if (! dumbterm) {
183 if (fromatty) /* editing mode on if tty is usable */
184 editing = 1;
185 if (foregroundproc())
186 progress = 1; /* progress bar on if fg */
187 }
188 #endif
189 }
190
191 while ((ch = getopt(argc, argv, "Aadefgino:pP:r:RtvV")) != -1) {
192 switch (ch) {
193 case 'A':
194 activefallback = 0;
195 passivemode = 0;
196 break;
197
198 case 'a':
199 anonftp = 1;
200 break;
201
202 case 'd':
203 options |= SO_DEBUG;
204 debug++;
205 break;
206
207 case 'e':
208 #ifndef SMALL
209 editing = 0;
210 #endif
211 break;
212
213 case 'f':
214 flushcache = 1;
215 break;
216
217 case 'g':
218 doglob = 0;
219 break;
220
221 case 'i':
222 interactive = 0;
223 break;
224
225 case 'n':
226 autologin = 0;
227 break;
228
229 case 'o':
230 outfile = optarg;
231 if (strcmp(outfile, "-") == 0)
232 ttyout = stderr;
233 break;
234
235 case 'p':
236 passivemode = 1;
237 activefallback = 0;
238 break;
239
240 case 'P':
241 port = strtol(optarg, &ep, 10);
242 if (port < 1 || port > MAX_IN_PORT_T || *ep != '\0')
243 warnx("bad port number: %s (ignored)", optarg);
244 else
245 ftpport = htons((in_port_t)port);
246 break;
247
248 case 'r':
249 retry_connect = strtol(optarg, &ep, 10);
250 if (retry_connect < 1 || *ep != '\0')
251 errx(1, "bad retry value: %s", optarg);
252 break;
253
254 case 'R':
255 restartautofetch = 1;
256 break;
257
258 case 't':
259 trace = 1;
260 break;
261
262 case 'v':
263 progress = verbose = 1;
264 break;
265
266 case 'V':
267 progress = verbose = 0;
268 break;
269
270 default:
271 usage();
272 }
273 }
274 /* set line buffering on ttyout */
275 setvbuf(ttyout, NULL, _IOLBF, 0);
276 argc -= optind;
277 argv += optind;
278
279 cpend = 0; /* no pending replies */
280 proxy = 0; /* proxy not active */
281 crflag = 1; /* strip c.r. on ascii gets */
282 sendport = -1; /* not using ports */
283 /*
284 * Set up the home directory in case we're globbing.
285 */
286 cp = getlogin();
287 if (cp != NULL) {
288 pw = getpwnam(cp);
289 }
290 if (pw == NULL)
291 pw = getpwuid(getuid());
292 if (pw != NULL) {
293 home = homedir;
294 (void)strcpy(home, pw->pw_dir);
295 }
296
297 setttywidth(0);
298 (void)xsignal(SIGWINCH, setttywidth);
299
300 #ifdef __GNUC__ /* to shut up gcc warnings */
301 (void)&argc;
302 (void)&argv;
303 #endif
304
305 if (argc > 0) {
306 if (strchr(argv[0], ':') != NULL) {
307 rval = auto_fetch(argc, argv);
308 if (rval >= 0) /* -1 == connected and cd-ed */
309 exit(rval);
310 } else {
311 char *xargv[5];
312
313 if (setjmp(toplevel))
314 exit(0);
315 (void)signal(SIGINT, (sig_t)intr);
316 (void)signal(SIGPIPE, (sig_t)lostpeer);
317 xargv[0] = __progname;
318 xargv[1] = argv[0];
319 xargv[2] = argv[1];
320 xargv[3] = argv[2];
321 xargv[4] = NULL;
322 do {
323 setpeer(argc+1, xargv);
324 if (!retry_connect)
325 break;
326 if (!connected) {
327 macnum = 0;
328 fprintf(ttyout,
329 "Retrying in %d seconds...\n",
330 retry_connect);
331 sleep(retry_connect);
332 }
333 } while (!connected);
334 retry_connect = 0; /* connected, stop hiding msgs */
335 }
336 }
337 #ifndef SMALL
338 controlediting();
339 #endif /* !SMALL */
340 top = setjmp(toplevel) == 0;
341 if (top) {
342 (void)signal(SIGINT, (sig_t)intr);
343 (void)signal(SIGPIPE, (sig_t)lostpeer);
344 }
345 for (;;) {
346 cmdscanner(top);
347 top = 1;
348 }
349 }
350
351 void
352 intr()
353 {
354
355 alarmtimer(0);
356 longjmp(toplevel, 1);
357 }
358
359 void
360 lostpeer()
361 {
362
363 alarmtimer(0);
364 if (connected) {
365 if (cout != NULL) {
366 (void)shutdown(fileno(cout), 1+1);
367 (void)fclose(cout);
368 cout = NULL;
369 }
370 if (data >= 0) {
371 (void)shutdown(data, 1+1);
372 (void)close(data);
373 data = -1;
374 }
375 connected = 0;
376 }
377 pswitch(1);
378 if (connected) {
379 if (cout != NULL) {
380 (void)shutdown(fileno(cout), 1+1);
381 (void)fclose(cout);
382 cout = NULL;
383 }
384 connected = 0;
385 }
386 proxflag = 0;
387 pswitch(0);
388 }
389
390 /*
391 * Generate a prompt
392 */
393 char *
394 prompt()
395 {
396 return ("ftp> ");
397 }
398
399 /*
400 * Command parser.
401 */
402 void
403 cmdscanner(top)
404 int top;
405 {
406 struct cmd *c;
407 int num;
408
409 if (!top
410 #ifndef SMALL
411 && !editing
412 #endif /* !SMALL */
413 )
414 (void)putc('\n', ttyout);
415 for (;;) {
416 #ifndef SMALL
417 if (!editing) {
418 #endif /* !SMALL */
419 if (fromatty) {
420 fputs(prompt(), ttyout);
421 (void)fflush(ttyout);
422 }
423 if (fgets(line, sizeof(line), stdin) == NULL)
424 quit(0, 0);
425 num = strlen(line);
426 if (num == 0)
427 break;
428 if (line[--num] == '\n') {
429 if (num == 0)
430 break;
431 line[num] = '\0';
432 } else if (num == sizeof(line) - 2) {
433 fputs("sorry, input line too long.\n", ttyout);
434 while ((num = getchar()) != '\n' && num != EOF)
435 /* void */;
436 break;
437 } /* else it was a line without a newline */
438 #ifndef SMALL
439 } else {
440 const char *buf;
441 HistEvent ev;
442 cursor_pos = NULL;
443
444 if ((buf = el_gets(el, &num)) == NULL || num == 0)
445 quit(0, 0);
446 if (buf[--num] == '\n') {
447 if (num == 0)
448 break;
449 } else if (num >= sizeof(line)) {
450 fputs("sorry, input line too long.\n", ttyout);
451 break;
452 }
453 memcpy(line, buf, num);
454 line[num] = '\0';
455 history(hist, &ev, H_ENTER, buf);
456 }
457 #endif /* !SMALL */
458
459 makeargv();
460 if (margc == 0)
461 continue;
462 c = getcmd(margv[0]);
463 if (c == (struct cmd *)-1) {
464 fputs("?Ambiguous command.\n", ttyout);
465 continue;
466 }
467 if (c == NULL) {
468 #if !defined(SMALL)
469 /*
470 * attempt to el_parse() unknown commands.
471 * any command containing a ':' would be parsed
472 * as "[prog:]cmd ...", and will result in a
473 * false positive if prog != "ftp", so treat
474 * such commands as invalid.
475 */
476 if (strchr(margv[0], ':') != NULL ||
477 el_parse(el, margc, margv) != 0)
478 #endif /* !SMALL */
479 fputs("?Invalid command.\n", ttyout);
480 continue;
481 }
482 if (c->c_conn && !connected) {
483 fputs("Not connected.\n", ttyout);
484 continue;
485 }
486 confirmrest = 0;
487 (*c->c_handler)(margc, margv);
488 if (bell && c->c_bell)
489 (void)putc('\007', ttyout);
490 if (c->c_handler != help)
491 break;
492 }
493 (void)signal(SIGINT, (sig_t)intr);
494 (void)signal(SIGPIPE, (sig_t)lostpeer);
495 }
496
497 struct cmd *
498 getcmd(name)
499 const char *name;
500 {
501 const char *p, *q;
502 struct cmd *c, *found;
503 int nmatches, longest;
504
505 if (name == NULL)
506 return (0);
507
508 longest = 0;
509 nmatches = 0;
510 found = 0;
511 for (c = cmdtab; (p = c->c_name) != NULL; c++) {
512 for (q = name; *q == *p++; q++)
513 if (*q == 0) /* exact match? */
514 return (c);
515 if (!*q) { /* the name was a prefix */
516 if (q - name > longest) {
517 longest = q - name;
518 nmatches = 1;
519 found = c;
520 } else if (q - name == longest)
521 nmatches++;
522 }
523 }
524 if (nmatches > 1)
525 return ((struct cmd *)-1);
526 return (found);
527 }
528
529 /*
530 * Slice a string up into argc/argv.
531 */
532
533 int slrflag;
534
535 void
536 makeargv()
537 {
538 char *argp;
539
540 stringbase = line; /* scan from first of buffer */
541 argbase = argbuf; /* store from first of buffer */
542 slrflag = 0;
543 marg_sl->sl_cur = 0; /* reset to start of marg_sl */
544 for (margc = 0; ; margc++) {
545 argp = slurpstring();
546 sl_add(marg_sl, argp);
547 if (argp == NULL)
548 break;
549 }
550 #ifndef SMALL
551 if (cursor_pos == line) {
552 cursor_argc = 0;
553 cursor_argo = 0;
554 } else if (cursor_pos != NULL) {
555 cursor_argc = margc;
556 cursor_argo = strlen(margv[margc-1]);
557 }
558 #endif /* !SMALL */
559 }
560
561 #ifdef SMALL
562 #define INC_CHKCURSOR(x) (x)++
563 #else /* !SMALL */
564 #define INC_CHKCURSOR(x) { (x)++ ; \
565 if (x == cursor_pos) { \
566 cursor_argc = margc; \
567 cursor_argo = ap-argbase; \
568 cursor_pos = NULL; \
569 } }
570
571 #endif /* !SMALL */
572
573 /*
574 * Parse string into argbuf;
575 * implemented with FSM to
576 * handle quoting and strings
577 */
578 char *
579 slurpstring()
580 {
581 int got_one = 0;
582 char *sb = stringbase;
583 char *ap = argbase;
584 char *tmp = argbase; /* will return this if token found */
585
586 if (*sb == '!' || *sb == '$') { /* recognize ! as a token for shell */
587 switch (slrflag) { /* and $ as token for macro invoke */
588 case 0:
589 slrflag++;
590 INC_CHKCURSOR(stringbase);
591 return ((*sb == '!') ? "!" : "$");
592 /* NOTREACHED */
593 case 1:
594 slrflag++;
595 altarg = stringbase;
596 break;
597 default:
598 break;
599 }
600 }
601
602 S0:
603 switch (*sb) {
604
605 case '\0':
606 goto OUT;
607
608 case ' ':
609 case '\t':
610 INC_CHKCURSOR(sb);
611 goto S0;
612
613 default:
614 switch (slrflag) {
615 case 0:
616 slrflag++;
617 break;
618 case 1:
619 slrflag++;
620 altarg = sb;
621 break;
622 default:
623 break;
624 }
625 goto S1;
626 }
627
628 S1:
629 switch (*sb) {
630
631 case ' ':
632 case '\t':
633 case '\0':
634 goto OUT; /* end of token */
635
636 case '\\':
637 INC_CHKCURSOR(sb);
638 goto S2; /* slurp next character */
639
640 case '"':
641 INC_CHKCURSOR(sb);
642 goto S3; /* slurp quoted string */
643
644 default:
645 *ap = *sb; /* add character to token */
646 ap++;
647 INC_CHKCURSOR(sb);
648 got_one = 1;
649 goto S1;
650 }
651
652 S2:
653 switch (*sb) {
654
655 case '\0':
656 goto OUT;
657
658 default:
659 *ap = *sb;
660 ap++;
661 INC_CHKCURSOR(sb);
662 got_one = 1;
663 goto S1;
664 }
665
666 S3:
667 switch (*sb) {
668
669 case '\0':
670 goto OUT;
671
672 case '"':
673 INC_CHKCURSOR(sb);
674 goto S1;
675
676 default:
677 *ap = *sb;
678 ap++;
679 INC_CHKCURSOR(sb);
680 got_one = 1;
681 goto S3;
682 }
683
684 OUT:
685 if (got_one)
686 *ap++ = '\0';
687 argbase = ap; /* update storage pointer */
688 stringbase = sb; /* update scan pointer */
689 if (got_one) {
690 return (tmp);
691 }
692 switch (slrflag) {
693 case 0:
694 slrflag++;
695 break;
696 case 1:
697 slrflag++;
698 altarg = NULL;
699 break;
700 default:
701 break;
702 }
703 return (NULL);
704 }
705
706 /*
707 * Help command.
708 * Call each command handler with argc == 0 and argv[0] == name.
709 */
710 void
711 help(argc, argv)
712 int argc;
713 char *argv[];
714 {
715 struct cmd *c;
716
717 if (argc == 1) {
718 StringList *buf;
719
720 buf = sl_init();
721 fprintf(ttyout,
722 "%sommands may be abbreviated. Commands are:\n\n",
723 proxy ? "Proxy c" : "C");
724 for (c = cmdtab; c < &cmdtab[NCMDS]; c++)
725 if (c->c_name && (!proxy || c->c_proxy))
726 sl_add(buf, c->c_name);
727 list_vertical(buf);
728 sl_free(buf, 0);
729 return;
730 }
731
732 #define HELPINDENT ((int) sizeof("disconnect"))
733
734 while (--argc > 0) {
735 char *arg;
736
737 arg = *++argv;
738 c = getcmd(arg);
739 if (c == (struct cmd *)-1)
740 fprintf(ttyout, "?Ambiguous help command %s\n", arg);
741 else if (c == NULL)
742 fprintf(ttyout, "?Invalid help command %s\n", arg);
743 else
744 fprintf(ttyout, "%-*s\t%s\n", HELPINDENT,
745 c->c_name, c->c_help);
746 }
747 }
748
749 void
750 usage()
751 {
752 (void)fprintf(stderr,
753 "usage: %s [-AadeginptvV] [-r retry] [-P port] [host [port]]\n"
754 " %s [-f] [-o outfile] file:///file\n"
755 " %s [-fR] [-o outfile] ftp://[user[:pass]@]host[:port]/path[/]\n"
756 " %s [-f] [-o outfile] http://host[:port]/path\n"
757 " %s [-fR] [-o outfile] host:path[/]\n",
758 __progname, __progname, __progname, __progname, __progname);
759 exit(1);
760 }
761