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