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