Annotation of src/usr.bin/ftp/main.c, Revision 1.99
1.99 ! sthen 1: /* $OpenBSD: main.c,v 1.98 2015/02/12 04:23:17 jsing Exp $ */
1.36 millert 2: /* $NetBSD: main.c,v 1.24 1997/08/18 10:20:26 lukem Exp $ */
1.5 deraadt 3:
1.1 deraadt 4: /*
1.44 itojun 5: * Copyright (C) 1997 and 1998 WIDE Project.
6: * All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
16: * 3. Neither the name of the project nor the names of its contributors
17: * may be used to endorse or promote products derived from this software
18: * without specific prior written permission.
19: *
20: * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23: * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30: * SUCH DAMAGE.
31: */
32:
33: /*
1.1 deraadt 34: * Copyright (c) 1985, 1989, 1993, 1994
35: * The Regents of the University of California. All rights reserved.
36: *
37: * Redistribution and use in source and binary forms, with or without
38: * modification, are permitted provided that the following conditions
39: * are met:
40: * 1. Redistributions of source code must retain the above copyright
41: * notice, this list of conditions and the following disclaimer.
42: * 2. Redistributions in binary form must reproduce the above copyright
43: * notice, this list of conditions and the following disclaimer in the
44: * documentation and/or other materials provided with the distribution.
1.53 millert 45: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 46: * may be used to endorse or promote products derived from this software
47: * without specific prior written permission.
48: *
49: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59: * SUCH DAMAGE.
60: */
61:
62: /*
63: * FTP User Program -- Command Interface.
64: */
65: #include <sys/types.h>
66: #include <sys/socket.h>
67:
1.18 millert 68: #include <ctype.h>
1.1 deraadt 69: #include <err.h>
70: #include <netdb.h>
71: #include <pwd.h>
72: #include <stdio.h>
1.39 deraadt 73: #include <errno.h>
1.18 millert 74: #include <stdlib.h>
1.1 deraadt 75: #include <string.h>
76: #include <unistd.h>
77:
1.95 jsing 78: #include <tls.h>
1.91 jsing 79:
80: #include "cmds.h"
1.1 deraadt 81: #include "ftp_var.h"
82:
1.86 jca 83: #ifndef SMALL
84: char * const ssl_verify_opts[] = {
85: #define SSL_CAFILE 0
86: "cafile",
87: #define SSL_CAPATH 1
88: "capath",
89: #define SSL_CIPHERS 2
90: "ciphers",
91: #define SSL_DONTVERIFY 3
92: "dont",
93: #define SSL_DOVERIFY 4
94: "do",
95: #define SSL_VERIFYDEPTH 5
96: "depth",
97: NULL
98: };
1.91 jsing 99:
1.95 jsing 100: struct tls_config *tls_config;
1.86 jca 101: #endif /* !SMALL */
102:
1.49 deraadt 103: int family = PF_UNSPEC;
1.81 halex 104: int pipeout;
1.49 deraadt 105:
1.1 deraadt 106: int
1.54 deraadt 107: main(volatile int argc, char *argv[])
1.1 deraadt 108: {
1.34 millert 109: int ch, top, rval;
1.1 deraadt 110: struct passwd *pw = NULL;
1.96 deraadt 111: char *cp, homedir[PATH_MAX];
1.38 millert 112: char *outfile = NULL;
1.62 tedu 113: const char *errstr;
1.28 millert 114: int dumb_terminal = 0;
1.91 jsing 115: #ifndef SMALL
116: long long depth;
117: #endif
1.1 deraadt 118:
1.44 itojun 119: ftpport = "ftp";
120: httpport = "http";
1.61 deraadt 121: #ifndef SMALL
122: httpsport = "https";
1.69 martynas 123: #endif /* !SMALL */
1.44 itojun 124: gateport = getenv("FTPSERVERPORT");
1.51 millert 125: if (gateport == NULL || *gateport == '\0')
1.44 itojun 126: gateport = "ftpgate";
1.1 deraadt 127: doglob = 1;
128: interactive = 1;
129: autologin = 1;
1.37 millert 130: passivemode = 1;
131: activefallback = 1;
1.17 millert 132: preserve = 1;
133: verbose = 0;
134: progress = 0;
1.36 millert 135: gatemode = 0;
1.25 millert 136: #ifndef SMALL
1.22 millert 137: editing = 0;
1.28 millert 138: el = NULL;
139: hist = NULL;
1.63 pyr 140: cookiefile = NULL;
1.67 martynas 141: resume = 0;
1.82 haesbaer 142: srcaddr = NULL;
1.77 martynas 143: marg_sl = sl_init();
1.69 martynas 144: #endif /* !SMALL */
1.8 kstailey 145: mark = HASHBYTES;
1.47 itojun 146: #ifdef INET6
147: epsv4 = 1;
148: #else
149: epsv4 = 0;
150: #endif
151: epsv4bad = 0;
1.1 deraadt 152:
1.37 millert 153: /* Set default operation mode based on FTPMODE environment variable */
1.51 millert 154: if ((cp = getenv("FTPMODE")) != NULL && *cp != '\0') {
1.37 millert 155: if (strcmp(cp, "passive") == 0) {
156: passivemode = 1;
157: activefallback = 0;
158: } else if (strcmp(cp, "active") == 0) {
159: passivemode = 0;
160: activefallback = 0;
161: } else if (strcmp(cp, "gate") == 0) {
162: gatemode = 1;
163: } else if (strcmp(cp, "auto") == 0) {
164: passivemode = 1;
165: activefallback = 1;
166: } else
167: warnx("unknown FTPMODE: %s. Using defaults", cp);
168: }
169:
170: if (strcmp(__progname, "gate-ftp") == 0)
1.36 millert 171: gatemode = 1;
172: gateserver = getenv("FTPSERVER");
1.97 tedu 173: if (gateserver == NULL)
174: gateserver = "";
1.36 millert 175: if (gatemode) {
176: if (*gateserver == '\0') {
177: warnx(
1.37 millert 178: "Neither $FTPSERVER nor $GATE_SERVER is defined; disabling gate-ftp");
1.36 millert 179: gatemode = 0;
180: }
181: }
1.17 millert 182:
1.31 millert 183: cp = getenv("TERM");
1.51 millert 184: dumb_terminal = (cp == NULL || *cp == '\0' || !strcmp(cp, "dumb") ||
1.31 millert 185: !strcmp(cp, "emacs") || !strcmp(cp, "su"));
1.17 millert 186: fromatty = isatty(fileno(stdin));
1.22 millert 187: if (fromatty) {
1.17 millert 188: verbose = 1; /* verbose if from a tty */
1.25 millert 189: #ifndef SMALL
1.28 millert 190: if (!dumb_terminal)
1.34 millert 191: editing = 1; /* editing mode on if tty is usable */
1.69 martynas 192: #endif /* !SMALL */
1.22 millert 193: }
1.30 deraadt 194:
195: ttyout = stdout;
1.33 millert 196: if (isatty(fileno(ttyout)) && !dumb_terminal && foregroundproc())
1.34 millert 197: progress = 1; /* progress bar on if tty is usable */
1.32 deraadt 198:
1.63 pyr 199: #ifndef SMALL
200: cookiefile = getenv("http_cookies");
1.99 ! sthen 201: if (tls_config == NULL) {
! 202: tls_config = tls_config_new();
! 203: if (tls_config == NULL)
! 204: errx(1, "tls config failed");
! 205: tls_config_set_protocols(tls_config,
! 206: TLS_PROTOCOLS_ALL);
! 207: }
! 208:
1.69 martynas 209: #endif /* !SMALL */
1.88 lteo 210: httpuseragent = NULL;
1.63 pyr 211:
1.86 jca 212: while ((ch = getopt(argc, argv,
1.88 lteo 213: "46AaCc:dD:Eegik:mno:pP:r:S:s:tU:vV")) != -1) {
1.1 deraadt 214: switch (ch) {
1.49 deraadt 215: case '4':
216: family = PF_INET;
217: break;
218: case '6':
219: family = PF_INET6;
220: break;
1.37 millert 221: case 'A':
222: activefallback = 0;
223: passivemode = 0;
224: break;
225:
1.17 millert 226: case 'a':
227: anonftp = 1;
228: break;
229:
1.67 martynas 230: case 'C':
231: #ifndef SMALL
232: resume = 1;
1.69 martynas 233: #endif /* !SMALL */
1.67 martynas 234: break;
235:
1.63 pyr 236: case 'c':
237: #ifndef SMALL
238: cookiefile = optarg;
1.69 martynas 239: #endif /* !SMALL */
1.63 pyr 240: break;
241:
1.87 deraadt 242: case 'D':
243: action = optarg;
244: break;
1.1 deraadt 245: case 'd':
1.70 martynas 246: #ifndef SMALL
1.1 deraadt 247: options |= SO_DEBUG;
248: debug++;
1.70 martynas 249: #endif /* !SMALL */
1.1 deraadt 250: break;
1.17 millert 251:
1.59 fgsch 252: case 'E':
253: epsv4 = 0;
254: break;
255:
1.28 millert 256: case 'e':
257: #ifndef SMALL
1.22 millert 258: editing = 0;
1.69 martynas 259: #endif /* !SMALL */
1.21 kstailey 260: break;
261:
1.1 deraadt 262: case 'g':
263: doglob = 0;
264: break;
265:
266: case 'i':
267: interactive = 0;
1.41 millert 268: break;
269:
1.65 espie 270: case 'k':
271: keep_alive_timeout = strtonum(optarg, 0, INT_MAX,
272: &errstr);
273: if (errstr != NULL) {
274: warnx("keep alive amount is %s: %s", errstr,
275: optarg);
276: usage();
277: }
278: break;
1.41 millert 279: case 'm':
1.42 millert 280: progress = -1;
1.1 deraadt 281: break;
282:
283: case 'n':
284: autologin = 0;
285: break;
286:
1.38 millert 287: case 'o':
288: outfile = optarg;
1.81 halex 289: if (*outfile == '\0') {
290: pipeout = 0;
291: outfile = NULL;
292: ttyout = stdout;
293: } else {
294: pipeout = strcmp(outfile, "-") == 0;
295: ttyout = pipeout ? stderr : stdout;
296: }
1.38 millert 297: break;
298:
1.3 deraadt 299: case 'p':
1.17 millert 300: passivemode = 1;
1.37 millert 301: activefallback = 0;
1.3 deraadt 302: break;
303:
1.17 millert 304: case 'P':
1.44 itojun 305: ftpport = optarg;
1.7 mickey 306: break;
307:
1.18 millert 308: case 'r':
1.62 tedu 309: retry_connect = strtonum(optarg, 0, INT_MAX, &errstr);
1.65 espie 310: if (errstr != NULL) {
311: warnx("retry amount is %s: %s", errstr,
1.62 tedu 312: optarg);
1.65 espie 313: usage();
314: }
1.18 millert 315: break;
316:
1.86 jca 317: case 'S':
318: #ifndef SMALL
319: cp = optarg;
320: while (*cp) {
321: char *str;
322: switch (getsubopt(&cp, ssl_verify_opts, &str)) {
323: case SSL_CAFILE:
324: if (str == NULL)
325: errx(1, "missing CA file");
1.95 jsing 326: if (tls_config_set_ca_file(
327: tls_config, str) != 0)
328: errx(1, "tls ca file failed");
1.86 jca 329: break;
330: case SSL_CAPATH:
331: if (str == NULL)
332: errx(1, "missing CA directory"
333: " path");
1.95 jsing 334: if (tls_config_set_ca_path(
335: tls_config, str) != 0)
336: errx(1, "tls ca path failed");
1.86 jca 337: break;
338: case SSL_CIPHERS:
339: if (str == NULL)
340: errx(1, "missing cipher list");
1.95 jsing 341: if (tls_config_set_ciphers(
342: tls_config, str) != 0)
343: errx(1, "tls ciphers failed");
1.86 jca 344: break;
345: case SSL_DONTVERIFY:
1.95 jsing 346: tls_config_insecure_noverifyhost(
347: tls_config);
348: tls_config_insecure_noverifycert(
349: tls_config);
1.86 jca 350: break;
351: case SSL_DOVERIFY:
1.95 jsing 352: tls_config_verify(tls_config);
1.86 jca 353: break;
354: case SSL_VERIFYDEPTH:
355: if (str == NULL)
356: errx(1, "missing depth");
1.91 jsing 357: depth = strtonum(str, 0, INT_MAX,
358: &errstr);
1.86 jca 359: if (errstr)
360: errx(1, "certificate "
361: "validation depth is %s",
362: errstr);
1.95 jsing 363: tls_config_set_verify_depth(
364: tls_config, (int)depth);
1.86 jca 365: break;
366: default:
367: errx(1, "unknown -S suboption `%s'",
368: suboptarg ? suboptarg : "");
369: /* NOTREACHED */
370: }
371: }
372: #endif
373: break;
374:
1.82 haesbaer 375: case 's':
376: #ifndef SMALL
377: srcaddr = optarg;
378: #endif /* !SMALL */
379: break;
380:
1.1 deraadt 381: case 't':
1.17 millert 382: trace = 1;
1.1 deraadt 383: break;
384:
1.89 halex 385: #ifndef SMALL
1.88 lteo 386: case 'U':
1.89 halex 387: free (httpuseragent);
1.88 lteo 388: if (strcspn(optarg, "\r\n") != strlen(optarg))
389: errx(1, "Invalid User-Agent: %s.", optarg);
390: if (asprintf(&httpuseragent, "User-Agent: %s",
391: optarg) == -1)
392: errx(1, "Can't allocate memory for HTTP(S) "
393: "User-Agent");
1.89 halex 394: break;
1.88 lteo 395: #endif /* !SMALL */
396:
1.1 deraadt 397: case 'v':
1.17 millert 398: verbose = 1;
399: break;
400:
401: case 'V':
402: verbose = 0;
1.1 deraadt 403: break;
404:
405: default:
1.17 millert 406: usage();
1.1 deraadt 407: }
408: }
409: argc -= optind;
410: argv += optind;
1.63 pyr 411:
412: #ifndef SMALL
413: cookie_load();
1.69 martynas 414: #endif /* !SMALL */
1.89 halex 415: if (httpuseragent == NULL)
416: httpuseragent = HTTP_USER_AGENT;
1.1 deraadt 417:
418: cpend = 0; /* no pending replies */
419: proxy = 0; /* proxy not active */
420: crflag = 1; /* strip c.r. on ascii gets */
421: sendport = -1; /* not using ports */
422: /*
423: * Set up the home directory in case we're globbing.
424: */
425: cp = getlogin();
426: if (cp != NULL) {
427: pw = getpwnam(cp);
428: }
429: if (pw == NULL)
430: pw = getpwuid(getuid());
431: if (pw != NULL) {
1.52 deraadt 432: (void)strlcpy(homedir, pw->pw_dir, sizeof homedir);
1.1 deraadt 433: home = homedir;
1.3 deraadt 434: }
1.9 michaels 435:
1.17 millert 436: setttywidth(0);
1.18 millert 437: (void)signal(SIGWINCH, setttywidth);
1.34 millert 438:
1.17 millert 439: if (argc > 0) {
1.44 itojun 440: if (isurl(argv[0])) {
1.38 millert 441: rval = auto_fetch(argc, argv, outfile);
1.17 millert 442: if (rval >= 0) /* -1 == connected and cd-ed */
443: exit(rval);
444: } else {
1.77 martynas 445: #ifndef SMALL
1.3 deraadt 446: char *xargv[5];
447:
448: if (setjmp(toplevel))
449: exit(0);
1.20 millert 450: (void)signal(SIGINT, (sig_t)intr);
451: (void)signal(SIGPIPE, (sig_t)lostpeer);
1.3 deraadt 452: xargv[0] = __progname;
1.17 millert 453: xargv[1] = argv[0];
454: xargv[2] = argv[1];
455: xargv[3] = argv[2];
456: xargv[4] = NULL;
1.18 millert 457: do {
458: setpeer(argc+1, xargv);
459: if (!retry_connect)
460: break;
461: if (!connected) {
462: macnum = 0;
1.30 deraadt 463: fputs("Retrying...\n", ttyout);
1.18 millert 464: sleep(retry_connect);
465: }
466: } while (!connected);
1.35 mickey 467: retry_connect = 0; /* connected, stop hiding msgs */
1.77 martynas 468: #endif /* !SMALL */
1.3 deraadt 469: }
1.1 deraadt 470: }
1.28 millert 471: #ifndef SMALL
472: controlediting();
1.1 deraadt 473: top = setjmp(toplevel) == 0;
474: if (top) {
1.20 millert 475: (void)signal(SIGINT, (sig_t)intr);
476: (void)signal(SIGPIPE, (sig_t)lostpeer);
1.1 deraadt 477: }
478: for (;;) {
479: cmdscanner(top);
480: top = 1;
481: }
1.77 martynas 482: #else /* !SMALL */
483: usage();
484: #endif /* !SMALL */
1.1 deraadt 485: }
486:
487: void
1.58 deraadt 488: intr(void)
1.1 deraadt 489: {
1.90 deraadt 490: int save_errno = errno;
1.1 deraadt 491:
1.90 deraadt 492: write(fileno(ttyout), "\n\r", 2);
1.17 millert 493: alarmtimer(0);
1.90 deraadt 494:
495: errno = save_errno;
1.1 deraadt 496: longjmp(toplevel, 1);
497: }
498:
499: void
1.58 deraadt 500: lostpeer(void)
1.1 deraadt 501: {
1.39 deraadt 502: int save_errno = errno;
1.1 deraadt 503:
1.17 millert 504: alarmtimer(0);
1.1 deraadt 505: if (connected) {
506: if (cout != NULL) {
1.66 moritz 507: (void)shutdown(fileno(cout), SHUT_RDWR);
1.18 millert 508: (void)fclose(cout);
1.1 deraadt 509: cout = NULL;
510: }
511: if (data >= 0) {
1.66 moritz 512: (void)shutdown(data, SHUT_RDWR);
1.18 millert 513: (void)close(data);
1.1 deraadt 514: data = -1;
515: }
516: connected = 0;
517: }
518: pswitch(1);
519: if (connected) {
520: if (cout != NULL) {
1.66 moritz 521: (void)shutdown(fileno(cout), SHUT_RDWR);
1.18 millert 522: (void)fclose(cout);
1.1 deraadt 523: cout = NULL;
524: }
525: connected = 0;
526: }
527: proxflag = 0;
528: pswitch(0);
1.39 deraadt 529: errno = save_errno;
1.1 deraadt 530: }
531:
1.77 martynas 532: #ifndef SMALL
1.1 deraadt 533: /*
1.17 millert 534: * Generate a prompt
535: */
1.1 deraadt 536: char *
1.58 deraadt 537: prompt(void)
1.1 deraadt 538: {
1.17 millert 539: return ("ftp> ");
1.1 deraadt 540: }
541:
542: /*
543: * Command parser.
544: */
545: void
1.58 deraadt 546: cmdscanner(int top)
1.1 deraadt 547: {
548: struct cmd *c;
1.17 millert 549: int num;
1.55 otto 550: HistEvent hev;
1.1 deraadt 551:
1.77 martynas 552: if (!top && !editing)
1.30 deraadt 553: (void)putc('\n', ttyout);
1.1 deraadt 554: for (;;) {
1.17 millert 555: if (!editing) {
556: if (fromatty) {
1.30 deraadt 557: fputs(prompt(), ttyout);
558: (void)fflush(ttyout);
1.17 millert 559: }
560: if (fgets(line, sizeof(line), stdin) == NULL)
561: quit(0, 0);
562: num = strlen(line);
563: if (num == 0)
564: break;
565: if (line[--num] == '\n') {
566: if (num == 0)
567: break;
568: line[num] = '\0';
569: } else if (num == sizeof(line) - 2) {
1.30 deraadt 570: fputs("sorry, input line too long.\n", ttyout);
1.17 millert 571: while ((num = getchar()) != '\n' && num != EOF)
572: /* void */;
573: break;
574: } /* else it was a line without a newline */
575: } else {
576: const char *buf;
577: cursor_pos = NULL;
578:
1.90 deraadt 579: if ((buf = el_gets(el, &num)) == NULL || num == 0) {
580: putc('\n', ttyout);
581: fflush(ttyout);
1.17 millert 582: quit(0, 0);
1.90 deraadt 583: }
1.46 fgsch 584: if (buf[--num] == '\n') {
1.17 millert 585: if (num == 0)
586: break;
1.46 fgsch 587: }
588: if (num >= sizeof(line)) {
1.30 deraadt 589: fputs("sorry, input line too long.\n", ttyout);
1.1 deraadt 590: break;
1.17 millert 591: }
1.34 millert 592: memcpy(line, buf, (size_t)num);
1.17 millert 593: line[num] = '\0';
1.55 otto 594: history(hist, &hev, H_ENTER, buf);
1.17 millert 595: }
596:
1.1 deraadt 597: makeargv();
1.17 millert 598: if (margc == 0)
1.1 deraadt 599: continue;
600: c = getcmd(margv[0]);
601: if (c == (struct cmd *)-1) {
1.30 deraadt 602: fputs("?Ambiguous command.\n", ttyout);
1.1 deraadt 603: continue;
604: }
605: if (c == 0) {
1.24 millert 606: /*
607: * Give editline(3) a shot at unknown commands.
608: * XXX - bogus commands with a colon in
609: * them will not elicit an error.
610: */
1.45 markus 611: if (editing &&
1.55 otto 612: el_parse(el, margc, (const char **)margv) != 0)
1.30 deraadt 613: fputs("?Invalid command.\n", ttyout);
1.1 deraadt 614: continue;
615: }
616: if (c->c_conn && !connected) {
1.30 deraadt 617: fputs("Not connected.\n", ttyout);
1.1 deraadt 618: continue;
619: }
1.17 millert 620: confirmrest = 0;
1.1 deraadt 621: (*c->c_handler)(margc, margv);
622: if (bell && c->c_bell)
1.30 deraadt 623: (void)putc('\007', ttyout);
1.1 deraadt 624: if (c->c_handler != help)
625: break;
626: }
1.20 millert 627: (void)signal(SIGINT, (sig_t)intr);
628: (void)signal(SIGPIPE, (sig_t)lostpeer);
1.1 deraadt 629: }
630:
631: struct cmd *
1.58 deraadt 632: getcmd(const char *name)
1.1 deraadt 633: {
1.17 millert 634: const char *p, *q;
1.1 deraadt 635: struct cmd *c, *found;
636: int nmatches, longest;
1.2 deraadt 637:
638: if (name == NULL)
639: return (0);
1.1 deraadt 640:
641: longest = 0;
642: nmatches = 0;
643: found = 0;
1.17 millert 644: for (c = cmdtab; (p = c->c_name) != NULL; c++) {
1.1 deraadt 645: for (q = name; *q == *p++; q++)
646: if (*q == 0) /* exact match? */
647: return (c);
648: if (!*q) { /* the name was a prefix */
649: if (q - name > longest) {
650: longest = q - name;
651: nmatches = 1;
652: found = c;
653: } else if (q - name == longest)
654: nmatches++;
655: }
656: }
657: if (nmatches > 1)
658: return ((struct cmd *)-1);
659: return (found);
660: }
661:
662: /*
663: * Slice a string up into argc/argv.
664: */
665:
666: int slrflag;
667:
668: void
1.58 deraadt 669: makeargv(void)
1.1 deraadt 670: {
1.17 millert 671: char *argp;
1.1 deraadt 672:
673: stringbase = line; /* scan from first of buffer */
674: argbase = argbuf; /* store from first of buffer */
675: slrflag = 0;
1.17 millert 676: marg_sl->sl_cur = 0; /* reset to start of marg_sl */
1.1 deraadt 677: for (margc = 0; ; margc++) {
1.17 millert 678: argp = slurpstring();
679: sl_add(marg_sl, argp);
680: if (argp == NULL)
1.1 deraadt 681: break;
682: }
1.17 millert 683: if (cursor_pos == line) {
684: cursor_argc = 0;
685: cursor_argo = 0;
686: } else if (cursor_pos != NULL) {
687: cursor_argc = margc;
688: cursor_argo = strlen(margv[margc-1]);
689: }
690: }
1.1 deraadt 691:
1.17 millert 692: #define INC_CHKCURSOR(x) { (x)++ ; \
693: if (x == cursor_pos) { \
694: cursor_argc = margc; \
695: cursor_argo = ap-argbase; \
696: cursor_pos = NULL; \
697: } }
1.1 deraadt 698:
699: /*
700: * Parse string into argbuf;
701: * implemented with FSM to
702: * handle quoting and strings
703: */
704: char *
1.58 deraadt 705: slurpstring(void)
1.1 deraadt 706: {
707: int got_one = 0;
708: char *sb = stringbase;
709: char *ap = argbase;
710: char *tmp = argbase; /* will return this if token found */
711:
712: if (*sb == '!' || *sb == '$') { /* recognize ! as a token for shell */
713: switch (slrflag) { /* and $ as token for macro invoke */
714: case 0:
715: slrflag++;
1.17 millert 716: INC_CHKCURSOR(stringbase);
1.1 deraadt 717: return ((*sb == '!') ? "!" : "$");
718: /* NOTREACHED */
719: case 1:
720: slrflag++;
721: altarg = stringbase;
722: break;
723: default:
724: break;
725: }
726: }
727:
728: S0:
729: switch (*sb) {
730:
731: case '\0':
732: goto OUT;
733:
734: case ' ':
735: case '\t':
1.17 millert 736: INC_CHKCURSOR(sb);
737: goto S0;
1.1 deraadt 738:
739: default:
740: switch (slrflag) {
741: case 0:
742: slrflag++;
743: break;
744: case 1:
745: slrflag++;
746: altarg = sb;
747: break;
748: default:
749: break;
750: }
751: goto S1;
752: }
753:
754: S1:
755: switch (*sb) {
756:
757: case ' ':
758: case '\t':
759: case '\0':
760: goto OUT; /* end of token */
761:
762: case '\\':
1.17 millert 763: INC_CHKCURSOR(sb);
764: goto S2; /* slurp next character */
1.1 deraadt 765:
766: case '"':
1.17 millert 767: INC_CHKCURSOR(sb);
768: goto S3; /* slurp quoted string */
1.1 deraadt 769:
770: default:
1.17 millert 771: *ap = *sb; /* add character to token */
772: ap++;
773: INC_CHKCURSOR(sb);
1.1 deraadt 774: got_one = 1;
775: goto S1;
776: }
777:
778: S2:
779: switch (*sb) {
780:
781: case '\0':
782: goto OUT;
783:
784: default:
1.17 millert 785: *ap = *sb;
786: ap++;
787: INC_CHKCURSOR(sb);
1.1 deraadt 788: got_one = 1;
789: goto S1;
790: }
791:
792: S3:
793: switch (*sb) {
794:
795: case '\0':
796: goto OUT;
797:
798: case '"':
1.17 millert 799: INC_CHKCURSOR(sb);
800: goto S1;
1.1 deraadt 801:
802: default:
1.17 millert 803: *ap = *sb;
804: ap++;
805: INC_CHKCURSOR(sb);
1.1 deraadt 806: got_one = 1;
807: goto S3;
808: }
809:
810: OUT:
811: if (got_one)
812: *ap++ = '\0';
813: argbase = ap; /* update storage pointer */
814: stringbase = sb; /* update scan pointer */
815: if (got_one) {
816: return (tmp);
817: }
818: switch (slrflag) {
819: case 0:
820: slrflag++;
821: break;
822: case 1:
823: slrflag++;
824: altarg = (char *) 0;
825: break;
826: default:
827: break;
828: }
829: return ((char *)0);
830: }
831:
832: /*
833: * Help command.
834: * Call each command handler with argc == 0 and argv[0] == name.
835: */
836: void
1.58 deraadt 837: help(int argc, char *argv[])
1.1 deraadt 838: {
839: struct cmd *c;
840:
841: if (argc == 1) {
1.17 millert 842: StringList *buf;
1.1 deraadt 843:
1.17 millert 844: buf = sl_init();
1.30 deraadt 845: fprintf(ttyout, "%sommands may be abbreviated. Commands are:\n\n",
1.17 millert 846: proxy ? "Proxy c" : "C");
847: for (c = cmdtab; c < &cmdtab[NCMDS]; c++)
848: if (c->c_name && (!proxy || c->c_proxy))
849: sl_add(buf, c->c_name);
850: list_vertical(buf);
851: sl_free(buf, 0);
1.1 deraadt 852: return;
853: }
1.17 millert 854:
1.18 millert 855: #define HELPINDENT ((int) sizeof("disconnect"))
1.17 millert 856:
1.1 deraadt 857: while (--argc > 0) {
858: char *arg;
1.17 millert 859:
1.1 deraadt 860: arg = *++argv;
861: c = getcmd(arg);
862: if (c == (struct cmd *)-1)
1.30 deraadt 863: fprintf(ttyout, "?Ambiguous help command %s\n", arg);
1.1 deraadt 864: else if (c == (struct cmd *)0)
1.30 deraadt 865: fprintf(ttyout, "?Invalid help command %s\n", arg);
1.1 deraadt 866: else
1.30 deraadt 867: fprintf(ttyout, "%-*s\t%s\n", HELPINDENT,
1.1 deraadt 868: c->c_name, c->c_help);
869: }
1.17 millert 870: }
1.77 martynas 871: #endif /* !SMALL */
1.17 millert 872:
873: void
1.58 deraadt 874: usage(void)
1.17 millert 875: {
1.92 lteo 876: fprintf(stderr, "usage: "
1.70 martynas 877: #ifndef SMALL
1.92 lteo 878: "%1$s [-46AadEegimnptVv] [-D title] [-k seconds] [-P port] "
1.87 deraadt 879: "[-r seconds]\n"
880: " [-s srcaddr] [host [port]]\n"
1.92 lteo 881: " %1$s [-C] [-o output] [-s srcaddr]\n"
882: " ftp://[user:password@]host[:port]/file[/] ...\n"
883: " %1$s [-C] [-c cookie] [-o output] [-S ssl_options] "
1.84 haesbaer 884: "[-s srcaddr]\n"
1.88 lteo 885: " [-U useragent] "
1.92 lteo 886: "http[s]://[user:password@]host[:port]/file ...\n"
887: " %1$s [-C] [-o output] [-s srcaddr] file:file ...\n"
888: " %1$s [-C] [-o output] [-s srcaddr] host:/file[/] ...\n",
1.69 martynas 889: #else /* !SMALL */
1.92 lteo 890: "%1$s [-o output] ftp://[user:password@]host[:port]/file[/] ...\n"
891: " %1$s [-o output] http://host[:port]/file ...\n"
892: " %1$s [-o output] file:file ...\n"
893: " %1$s [-o output] host:/file[/] ...\n",
1.69 martynas 894: #endif /* !SMALL */
1.92 lteo 895: __progname);
1.17 millert 896: exit(1);
1.1 deraadt 897: }