main.c revision 1.43 1 /* $NetBSD: main.c,v 1.43 1999/06/20 22:07:29 cgd 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.43 1999/06/20 22:07:29 cgd 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 NO_EDITCOMPLETE
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 if (! dumbterm) {
182 #ifndef NO_EDITCOMPLETE
183 if (fromatty) /* editing mode on if tty is usable */
184 editing = 1;
185 #endif
186 #ifndef NO_PROGRESS
187 if (foregroundproc())
188 progress = 1; /* progress bar on if fg */
189 #endif
190 }
191 }
192
193 while ((ch = getopt(argc, argv, "Aadefgino:pP:r:RtvV")) != -1) {
194 switch (ch) {
195 case 'A':
196 activefallback = 0;
197 passivemode = 0;
198 break;
199
200 case 'a':
201 anonftp = 1;
202 break;
203
204 case 'd':
205 options |= SO_DEBUG;
206 debug++;
207 break;
208
209 case 'e':
210 #ifndef NO_EDITCOMPLETE
211 editing = 0;
212 #endif
213 break;
214
215 case 'f':
216 flushcache = 1;
217 break;
218
219 case 'g':
220 doglob = 0;
221 break;
222
223 case 'i':
224 interactive = 0;
225 break;
226
227 case 'n':
228 autologin = 0;
229 break;
230
231 case 'o':
232 outfile = optarg;
233 if (strcmp(outfile, "-") == 0)
234 ttyout = stderr;
235 break;
236
237 case 'p':
238 passivemode = 1;
239 activefallback = 0;
240 break;
241
242 case 'P':
243 port = strtol(optarg, &ep, 10);
244 if (port < 1 || port > MAX_IN_PORT_T || *ep != '\0')
245 warnx("bad port number: %s (ignored)", optarg);
246 else
247 ftpport = htons((in_port_t)port);
248 break;
249
250 case 'r':
251 retry_connect = strtol(optarg, &ep, 10);
252 if (retry_connect < 1 || *ep != '\0')
253 errx(1, "bad retry value: %s", optarg);
254 break;
255
256 case 'R':
257 restartautofetch = 1;
258 break;
259
260 case 't':
261 trace = 1;
262 break;
263
264 case 'v':
265 progress = verbose = 1;
266 break;
267
268 case 'V':
269 progress = verbose = 0;
270 break;
271
272 default:
273 usage();
274 }
275 }
276 /* set line buffering on ttyout */
277 setvbuf(ttyout, NULL, _IOLBF, 0);
278 argc -= optind;
279 argv += optind;
280
281 cpend = 0; /* no pending replies */
282 proxy = 0; /* proxy not active */
283 crflag = 1; /* strip c.r. on ascii gets */
284 sendport = -1; /* not using ports */
285 /*
286 * Set up the home directory in case we're globbing.
287 */
288 cp = getlogin();
289 if (cp != NULL) {
290 pw = getpwnam(cp);
291 }
292 if (pw == NULL)
293 pw = getpwuid(getuid());
294 if (pw != NULL) {
295 home = homedir;
296 (void)strcpy(home, pw->pw_dir);
297 }
298
299 setttywidth(0);
300 (void)xsignal(SIGWINCH, setttywidth);
301
302 #ifdef __GNUC__ /* to shut up gcc warnings */
303 (void)&argc;
304 (void)&argv;
305 #endif
306
307 if (argc > 0) {
308 if (strchr(argv[0], ':') != NULL) {
309 rval = auto_fetch(argc, argv);
310 if (rval >= 0) /* -1 == connected and cd-ed */
311 exit(rval);
312 } else {
313 char *xargv[5];
314
315 if (setjmp(toplevel))
316 exit(0);
317 (void)signal(SIGINT, (sig_t)intr);
318 (void)signal(SIGPIPE, (sig_t)lostpeer);
319 xargv[0] = __progname;
320 xargv[1] = argv[0];
321 xargv[2] = argv[1];
322 xargv[3] = argv[2];
323 xargv[4] = NULL;
324 do {
325 setpeer(argc+1, xargv);
326 if (!retry_connect)
327 break;
328 if (!connected) {
329 macnum = 0;
330 fprintf(ttyout,
331 "Retrying in %d seconds...\n",
332 retry_connect);
333 sleep(retry_connect);
334 }
335 } while (!connected);
336 retry_connect = 0; /* connected, stop hiding msgs */
337 }
338 }
339 #ifndef NO_EDITCOMPLETE
340 controlediting();
341 #endif /* !NO_EDITCOMPLETE */
342 top = setjmp(toplevel) == 0;
343 if (top) {
344 (void)signal(SIGINT, (sig_t)intr);
345 (void)signal(SIGPIPE, (sig_t)lostpeer);
346 }
347 for (;;) {
348 cmdscanner(top);
349 top = 1;
350 }
351 }
352
353 void
354 intr()
355 {
356
357 alarmtimer(0);
358 longjmp(toplevel, 1);
359 }
360
361 void
362 lostpeer()
363 {
364
365 alarmtimer(0);
366 if (connected) {
367 if (cout != NULL) {
368 (void)shutdown(fileno(cout), 1+1);
369 (void)fclose(cout);
370 cout = NULL;
371 }
372 if (data >= 0) {
373 (void)shutdown(data, 1+1);
374 (void)close(data);
375 data = -1;
376 }
377 connected = 0;
378 }
379 pswitch(1);
380 if (connected) {
381 if (cout != NULL) {
382 (void)shutdown(fileno(cout), 1+1);
383 (void)fclose(cout);
384 cout = NULL;
385 }
386 connected = 0;
387 }
388 proxflag = 0;
389 pswitch(0);
390 }
391
392 /*
393 * Generate a prompt
394 */
395 char *
396 prompt()
397 {
398 return ("ftp> ");
399 }
400
401 /*
402 * Command parser.
403 */
404 void
405 cmdscanner(top)
406 int top;
407 {
408 struct cmd *c;
409 int num;
410
411 if (!top
412 #ifndef NO_EDITCOMPLETE
413 && !editing
414 #endif /* !NO_EDITCOMPLETE */
415 )
416 (void)putc('\n', ttyout);
417 for (;;) {
418 #ifndef NO_EDITCOMPLETE
419 if (!editing) {
420 #endif /* !NO_EDITCOMPLETE */
421 if (fromatty) {
422 fputs(prompt(), ttyout);
423 (void)fflush(ttyout);
424 }
425 if (fgets(line, sizeof(line), stdin) == NULL)
426 quit(0, 0);
427 num = strlen(line);
428 if (num == 0)
429 break;
430 if (line[--num] == '\n') {
431 if (num == 0)
432 break;
433 line[num] = '\0';
434 } else if (num == sizeof(line) - 2) {
435 fputs("sorry, input line too long.\n", ttyout);
436 while ((num = getchar()) != '\n' && num != EOF)
437 /* void */;
438 break;
439 } /* else it was a line without a newline */
440 #ifndef NO_EDITCOMPLETE
441 } else {
442 const char *buf;
443 HistEvent ev;
444 cursor_pos = NULL;
445
446 if ((buf = el_gets(el, &num)) == NULL || num == 0)
447 quit(0, 0);
448 if (buf[--num] == '\n') {
449 if (num == 0)
450 break;
451 } else if (num >= sizeof(line)) {
452 fputs("sorry, input line too long.\n", ttyout);
453 break;
454 }
455 memcpy(line, buf, num);
456 line[num] = '\0';
457 history(hist, &ev, H_ENTER, buf);
458 }
459 #endif /* !NO_EDITCOMPLETE */
460
461 makeargv();
462 if (margc == 0)
463 continue;
464 c = getcmd(margv[0]);
465 if (c == (struct cmd *)-1) {
466 fputs("?Ambiguous command.\n", ttyout);
467 continue;
468 }
469 if (c == NULL) {
470 #if !defined(NO_EDITCOMPLETE)
471 /*
472 * attempt to el_parse() unknown commands.
473 * any command containing a ':' would be parsed
474 * as "[prog:]cmd ...", and will result in a
475 * false positive if prog != "ftp", so treat
476 * such commands as invalid.
477 */
478 if (strchr(margv[0], ':') != NULL ||
479 el_parse(el, margc, margv) != 0)
480 #endif /* !NO_EDITCOMPLETE */
481 fputs("?Invalid command.\n", ttyout);
482 continue;
483 }
484 if (c->c_conn && !connected) {
485 fputs("Not connected.\n", ttyout);
486 continue;
487 }
488 confirmrest = 0;
489 (*c->c_handler)(margc, margv);
490 if (bell && c->c_bell)
491 (void)putc('\007', ttyout);
492 if (c->c_handler != help)
493 break;
494 }
495 (void)signal(SIGINT, (sig_t)intr);
496 (void)signal(SIGPIPE, (sig_t)lostpeer);
497 }
498
499 struct cmd *
500 getcmd(name)
501 const char *name;
502 {
503 const char *p, *q;
504 struct cmd *c, *found;
505 int nmatches, longest;
506
507 if (name == NULL)
508 return (0);
509
510 longest = 0;
511 nmatches = 0;
512 found = 0;
513 for (c = cmdtab; (p = c->c_name) != NULL; c++) {
514 for (q = name; *q == *p++; q++)
515 if (*q == 0) /* exact match? */
516 return (c);
517 if (!*q) { /* the name was a prefix */
518 if (q - name > longest) {
519 longest = q - name;
520 nmatches = 1;
521 found = c;
522 } else if (q - name == longest)
523 nmatches++;
524 }
525 }
526 if (nmatches > 1)
527 return ((struct cmd *)-1);
528 return (found);
529 }
530
531 /*
532 * Slice a string up into argc/argv.
533 */
534
535 int slrflag;
536
537 void
538 makeargv()
539 {
540 char *argp;
541
542 stringbase = line; /* scan from first of buffer */
543 argbase = argbuf; /* store from first of buffer */
544 slrflag = 0;
545 marg_sl->sl_cur = 0; /* reset to start of marg_sl */
546 for (margc = 0; ; margc++) {
547 argp = slurpstring();
548 sl_add(marg_sl, argp);
549 if (argp == NULL)
550 break;
551 }
552 #ifndef NO_EDITCOMPLETE
553 if (cursor_pos == line) {
554 cursor_argc = 0;
555 cursor_argo = 0;
556 } else if (cursor_pos != NULL) {
557 cursor_argc = margc;
558 cursor_argo = strlen(margv[margc-1]);
559 }
560 #endif /* !NO_EDITCOMPLETE */
561 }
562
563 #ifdef NO_EDITCOMPLETE
564 #define INC_CHKCURSOR(x) (x)++
565 #else /* !NO_EDITCOMPLETE */
566 #define INC_CHKCURSOR(x) { (x)++ ; \
567 if (x == cursor_pos) { \
568 cursor_argc = margc; \
569 cursor_argo = ap-argbase; \
570 cursor_pos = NULL; \
571 } }
572
573 #endif /* !NO_EDITCOMPLETE */
574
575 /*
576 * Parse string into argbuf;
577 * implemented with FSM to
578 * handle quoting and strings
579 */
580 char *
581 slurpstring()
582 {
583 int got_one = 0;
584 char *sb = stringbase;
585 char *ap = argbase;
586 char *tmp = argbase; /* will return this if token found */
587
588 if (*sb == '!' || *sb == '$') { /* recognize ! as a token for shell */
589 switch (slrflag) { /* and $ as token for macro invoke */
590 case 0:
591 slrflag++;
592 INC_CHKCURSOR(stringbase);
593 return ((*sb == '!') ? "!" : "$");
594 /* NOTREACHED */
595 case 1:
596 slrflag++;
597 altarg = stringbase;
598 break;
599 default:
600 break;
601 }
602 }
603
604 S0:
605 switch (*sb) {
606
607 case '\0':
608 goto OUT;
609
610 case ' ':
611 case '\t':
612 INC_CHKCURSOR(sb);
613 goto S0;
614
615 default:
616 switch (slrflag) {
617 case 0:
618 slrflag++;
619 break;
620 case 1:
621 slrflag++;
622 altarg = sb;
623 break;
624 default:
625 break;
626 }
627 goto S1;
628 }
629
630 S1:
631 switch (*sb) {
632
633 case ' ':
634 case '\t':
635 case '\0':
636 goto OUT; /* end of token */
637
638 case '\\':
639 INC_CHKCURSOR(sb);
640 goto S2; /* slurp next character */
641
642 case '"':
643 INC_CHKCURSOR(sb);
644 goto S3; /* slurp quoted string */
645
646 default:
647 *ap = *sb; /* add character to token */
648 ap++;
649 INC_CHKCURSOR(sb);
650 got_one = 1;
651 goto S1;
652 }
653
654 S2:
655 switch (*sb) {
656
657 case '\0':
658 goto OUT;
659
660 default:
661 *ap = *sb;
662 ap++;
663 INC_CHKCURSOR(sb);
664 got_one = 1;
665 goto S1;
666 }
667
668 S3:
669 switch (*sb) {
670
671 case '\0':
672 goto OUT;
673
674 case '"':
675 INC_CHKCURSOR(sb);
676 goto S1;
677
678 default:
679 *ap = *sb;
680 ap++;
681 INC_CHKCURSOR(sb);
682 got_one = 1;
683 goto S3;
684 }
685
686 OUT:
687 if (got_one)
688 *ap++ = '\0';
689 argbase = ap; /* update storage pointer */
690 stringbase = sb; /* update scan pointer */
691 if (got_one) {
692 return (tmp);
693 }
694 switch (slrflag) {
695 case 0:
696 slrflag++;
697 break;
698 case 1:
699 slrflag++;
700 altarg = NULL;
701 break;
702 default:
703 break;
704 }
705 return (NULL);
706 }
707
708 /*
709 * Help command.
710 * Call each command handler with argc == 0 and argv[0] == name.
711 */
712 void
713 help(argc, argv)
714 int argc;
715 char *argv[];
716 {
717 struct cmd *c;
718
719 if (argc == 1) {
720 StringList *buf;
721
722 buf = sl_init();
723 fprintf(ttyout,
724 "%sommands may be abbreviated. Commands are:\n\n",
725 proxy ? "Proxy c" : "C");
726 for (c = cmdtab; c < &cmdtab[NCMDS]; c++)
727 if (c->c_name && (!proxy || c->c_proxy))
728 sl_add(buf, c->c_name);
729 list_vertical(buf);
730 sl_free(buf, 0);
731 return;
732 }
733
734 #define HELPINDENT ((int) sizeof("disconnect"))
735
736 while (--argc > 0) {
737 char *arg;
738
739 arg = *++argv;
740 c = getcmd(arg);
741 if (c == (struct cmd *)-1)
742 fprintf(ttyout, "?Ambiguous help command %s\n", arg);
743 else if (c == NULL)
744 fprintf(ttyout, "?Invalid help command %s\n", arg);
745 else
746 fprintf(ttyout, "%-*s\t%s\n", HELPINDENT,
747 c->c_name, c->c_help);
748 }
749 }
750
751 void
752 usage()
753 {
754 (void)fprintf(stderr,
755 "usage: %s [-AadeginptvV] [-r retry] [-P port] [host [port]]\n"
756 " %s [-f] [-o outfile] file:///file\n"
757 " %s [-fR] [-o outfile] ftp://[user[:pass]@]host[:port]/path[/]\n"
758 " %s [-f] [-o outfile] http://host[:port]/path\n"
759 " %s [-fR] [-o outfile] host:path[/]\n",
760 __progname, __progname, __progname, __progname, __progname);
761 exit(1);
762 }
763