1 /* $NetBSD: fetch.c,v 1.146 2003/12/10 12:34:28 lukem Exp $ */ 2 3 /*- 4 * Copyright (c) 1997-2003 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Luke Mewburn. 9 * 10 * This code is derived from software contributed to The NetBSD Foundation 11 * by Scott Aaron Bamford. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. All advertising materials mentioning features or use of this software 22 * must display the following acknowledgement: 23 * This product includes software developed by the NetBSD 24 * Foundation, Inc. and its contributors. 25 * 4. Neither the name of The NetBSD Foundation nor the names of its 26 * contributors may be used to endorse or promote products derived 27 * from this software without specific prior written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 30 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 31 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 32 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 33 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 35 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 36 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 37 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 39 * POSSIBILITY OF SUCH DAMAGE. 40 */ 41 42 #include <sys/cdefs.h> 43 #ifndef lint 44 __RCSID("$NetBSD: fetch.c,v 1.146 2003/12/10 12:34:28 lukem Exp $"); 45 #endif /* not lint */ 46 47 /* 48 * FTP User Program -- Command line file retrieval 49 */ 50 51 #include <sys/types.h> 52 #include <sys/param.h> 53 #include <sys/socket.h> 54 #include <sys/stat.h> 55 #include <sys/time.h> 56 57 #include <netinet/in.h> 58 59 #include <arpa/ftp.h> 60 #include <arpa/inet.h> 61 62 #include <ctype.h> 63 #include <err.h> 64 #include <errno.h> 65 #include <netdb.h> 66 #include <fcntl.h> 67 #include <stdio.h> 68 #include <stdlib.h> 69 #include <string.h> 70 #include <unistd.h> 71 #include <time.h> 72 73 #include "ftp_var.h" 74 #include "version.h" 75 76 typedef enum { 77 UNKNOWN_URL_T=-1, 78 HTTP_URL_T, 79 FTP_URL_T, 80 FILE_URL_T, 81 CLASSIC_URL_T 82 } url_t; 83 84 void aborthttp(int); 85 static int auth_url(const char *, char **, const char *, const char *); 86 static void base64_encode(const u_char *, size_t, u_char *); 87 static int go_fetch(const char *); 88 static int fetch_ftp(const char *); 89 static int fetch_url(const char *, const char *, char *, char *); 90 static int parse_url(const char *, const char *, url_t *, char **, 91 char **, char **, char **, in_port_t *, char **); 92 static void url_decode(char *); 93 94 static int redirect_loop; 95 96 97 #define ABOUT_URL "about:" /* propaganda */ 98 #define FILE_URL "file://" /* file URL prefix */ 99 #define FTP_URL "ftp://" /* ftp URL prefix */ 100 #define HTTP_URL "http://" /* http URL prefix */ 101 102 103 /* 104 * Generate authorization response based on given authentication challenge. 105 * Returns -1 if an error occurred, otherwise 0. 106 * Sets response to a malloc(3)ed string; caller should free. 107 */ 108 static int 109 auth_url(const char *challenge, char **response, const char *guser, 110 const char *gpass) 111 { 112 char *cp, *ep, *clear, *line, *realm, *scheme; 113 char user[BUFSIZ], *pass; 114 int rval; 115 size_t len, clen, rlen; 116 117 *response = NULL; 118 clear = realm = scheme = NULL; 119 rval = -1; 120 line = xstrdup(challenge); 121 cp = line; 122 123 if (debug) 124 fprintf(ttyout, "auth_url: challenge `%s'\n", challenge); 125 126 scheme = strsep(&cp, " "); 127 #define SCHEME_BASIC "Basic" 128 if (strncasecmp(scheme, SCHEME_BASIC, sizeof(SCHEME_BASIC) - 1) != 0) { 129 warnx("Unsupported WWW Authentication challenge - `%s'", 130 challenge); 131 goto cleanup_auth_url; 132 } 133 cp += strspn(cp, " "); 134 135 #define REALM "realm=\"" 136 if (strncasecmp(cp, REALM, sizeof(REALM) - 1) == 0) 137 cp += sizeof(REALM) - 1; 138 else { 139 warnx("Unsupported WWW Authentication challenge - `%s'", 140 challenge); 141 goto cleanup_auth_url; 142 } 143 if ((ep = strchr(cp, '\"')) != NULL) { 144 size_t len = ep - cp; 145 146 realm = (char *)xmalloc(len + 1); 147 (void)strlcpy(realm, cp, len + 1); 148 } else { 149 warnx("Unsupported WWW Authentication challenge - `%s'", 150 challenge); 151 goto cleanup_auth_url; 152 } 153 154 if (guser != NULL) 155 (void)strlcpy(user, guser, sizeof(user)); 156 else { 157 fprintf(ttyout, "Username for `%s': ", realm); 158 (void)fflush(ttyout); 159 if (fgets(user, sizeof(user) - 1, stdin) == NULL) { 160 clearerr(stdin); 161 goto cleanup_auth_url; 162 } 163 user[strlen(user) - 1] = '\0'; 164 } 165 if (gpass != NULL) 166 pass = (char *)gpass; 167 else 168 pass = getpass("Password: "); 169 170 clen = strlen(user) + strlen(pass) + 2; /* user + ":" + pass + "\0" */ 171 clear = (char *)xmalloc(clen); 172 (void)strlcpy(clear, user, clen); 173 (void)strlcat(clear, ":", clen); 174 (void)strlcat(clear, pass, clen); 175 if (gpass == NULL) 176 memset(pass, 0, strlen(pass)); 177 178 /* scheme + " " + enc + "\0" */ 179 rlen = strlen(scheme) + 1 + (clen + 2) * 4 / 3 + 1; 180 *response = (char *)xmalloc(rlen); 181 (void)strlcpy(*response, scheme, rlen); 182 len = strlcat(*response, " ", rlen); 183 base64_encode(clear, clen, (u_char *)*response + len); 184 memset(clear, 0, clen); 185 rval = 0; 186 187 cleanup_auth_url: 188 FREEPTR(clear); 189 FREEPTR(line); 190 FREEPTR(realm); 191 return (rval); 192 } 193 194 /* 195 * Encode len bytes starting at clear using base64 encoding into encoded, 196 * which should be at least ((len + 2) * 4 / 3 + 1) in size. 197 */ 198 static void 199 base64_encode(const u_char *clear, size_t len, u_char *encoded) 200 { 201 static const u_char enc[] = 202 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 203 u_char *cp; 204 int i; 205 206 cp = encoded; 207 for (i = 0; i < len; i += 3) { 208 *(cp++) = enc[((clear[i + 0] >> 2))]; 209 *(cp++) = enc[((clear[i + 0] << 4) & 0x30) 210 | ((clear[i + 1] >> 4) & 0x0f)]; 211 *(cp++) = enc[((clear[i + 1] << 2) & 0x3c) 212 | ((clear[i + 2] >> 6) & 0x03)]; 213 *(cp++) = enc[((clear[i + 2] ) & 0x3f)]; 214 } 215 *cp = '\0'; 216 while (i-- > len) 217 *(--cp) = '='; 218 } 219 220 /* 221 * Decode %xx escapes in given string, `in-place'. 222 */ 223 static void 224 url_decode(char *url) 225 { 226 unsigned char *p, *q; 227 228 if (EMPTYSTRING(url)) 229 return; 230 p = q = (unsigned char *)url; 231 232 #define HEXTOINT(x) (x - (isdigit(x) ? '0' : (islower(x) ? 'a' : 'A') - 10)) 233 while (*p) { 234 if (p[0] == '%' 235 && p[1] && isxdigit((unsigned char)p[1]) 236 && p[2] && isxdigit((unsigned char)p[2])) { 237 *q++ = HEXTOINT(p[1]) * 16 + HEXTOINT(p[2]); 238 p+=3; 239 } else 240 *q++ = *p++; 241 } 242 *q = '\0'; 243 } 244 245 246 /* 247 * Parse URL of form: 248 * <type>://[<user>[:<password>]@]<host>[:<port>][/<path>] 249 * Returns -1 if a parse error occurred, otherwise 0. 250 * It's the caller's responsibility to url_decode() the returned 251 * user, pass and path. 252 * 253 * Sets type to url_t, each of the given char ** pointers to a 254 * malloc(3)ed strings of the relevant section, and port to 255 * the number given, or ftpport if ftp://, or httpport if http://. 256 * 257 * If <host> is surrounded by `[' and ']', it's parsed as an 258 * IPv6 address (as per RFC 2732). 259 * 260 * XXX: this is not totally RFC 1738 compliant; <path> will have the 261 * leading `/' unless it's an ftp:// URL, as this makes things easier 262 * for file:// and http:// URLs. ftp:// URLs have the `/' between the 263 * host and the URL-path removed, but any additional leading slashes 264 * in the URL-path are retained (because they imply that we should 265 * later do "CWD" with a null argument). 266 * 267 * Examples: 268 * input URL output path 269 * --------- ----------- 270 * "ftp://host" NULL 271 * "http://host/" NULL 272 * "file://host/dir/file" "dir/file" 273 * "ftp://host/" "" 274 * "ftp://host//" NULL 275 * "ftp://host//dir/file" "/dir/file" 276 */ 277 static int 278 parse_url(const char *url, const char *desc, url_t *type, 279 char **user, char **pass, char **host, char **port, 280 in_port_t *portnum, char **path) 281 { 282 const char *origurl; 283 char *cp, *ep, *thost, *tport; 284 size_t len; 285 286 if (url == NULL || desc == NULL || type == NULL || user == NULL 287 || pass == NULL || host == NULL || port == NULL || portnum == NULL 288 || path == NULL) 289 errx(1, "parse_url: invoked with NULL argument!"); 290 291 origurl = url; 292 *type = UNKNOWN_URL_T; 293 *user = *pass = *host = *port = *path = NULL; 294 *portnum = 0; 295 tport = NULL; 296 297 if (strncasecmp(url, HTTP_URL, sizeof(HTTP_URL) - 1) == 0) { 298 url += sizeof(HTTP_URL) - 1; 299 *type = HTTP_URL_T; 300 *portnum = HTTP_PORT; 301 tport = httpport; 302 } else if (strncasecmp(url, FTP_URL, sizeof(FTP_URL) - 1) == 0) { 303 url += sizeof(FTP_URL) - 1; 304 *type = FTP_URL_T; 305 *portnum = FTP_PORT; 306 tport = ftpport; 307 } else if (strncasecmp(url, FILE_URL, sizeof(FILE_URL) - 1) == 0) { 308 url += sizeof(FILE_URL) - 1; 309 *type = FILE_URL_T; 310 } else { 311 warnx("Invalid %s `%s'", desc, url); 312 cleanup_parse_url: 313 FREEPTR(*user); 314 FREEPTR(*pass); 315 FREEPTR(*host); 316 FREEPTR(*port); 317 FREEPTR(*path); 318 return (-1); 319 } 320 321 if (*url == '\0') 322 return (0); 323 324 /* find [user[:pass]@]host[:port] */ 325 ep = strchr(url, '/'); 326 if (ep == NULL) 327 thost = xstrdup(url); 328 else { 329 len = ep - url; 330 thost = (char *)xmalloc(len + 1); 331 (void)strlcpy(thost, url, len + 1); 332 if (*type == FTP_URL_T) /* skip first / for ftp URLs */ 333 ep++; 334 *path = xstrdup(ep); 335 } 336 337 cp = strchr(thost, '@'); /* look for user[:pass]@ in URLs */ 338 if (cp != NULL) { 339 if (*type == FTP_URL_T) 340 anonftp = 0; /* disable anonftp */ 341 *user = thost; 342 *cp = '\0'; 343 thost = xstrdup(cp + 1); 344 cp = strchr(*user, ':'); 345 if (cp != NULL) { 346 *cp = '\0'; 347 *pass = xstrdup(cp + 1); 348 } 349 } 350 351 #ifdef INET6 352 /* 353 * Check if thost is an encoded IPv6 address, as per 354 * RFC 2732: 355 * `[' ipv6-address ']' 356 */ 357 if (*thost == '[') { 358 cp = thost + 1; 359 if ((ep = strchr(cp, ']')) == NULL || 360 (ep[1] != '\0' && ep[1] != ':')) { 361 warnx("Invalid address `%s' in %s `%s'", 362 thost, desc, origurl); 363 goto cleanup_parse_url; 364 } 365 len = ep - cp; /* change `[xyz]' -> `xyz' */ 366 memmove(thost, thost + 1, len); 367 thost[len] = '\0'; 368 if (! isipv6addr(thost)) { 369 warnx("Invalid IPv6 address `%s' in %s `%s'", 370 thost, desc, origurl); 371 goto cleanup_parse_url; 372 } 373 cp = ep + 1; 374 if (*cp == ':') 375 cp++; 376 else 377 cp = NULL; 378 } else 379 #endif /* INET6 */ 380 if ((cp = strchr(thost, ':')) != NULL) 381 *cp++ = '\0'; 382 *host = thost; 383 384 /* look for [:port] */ 385 if (cp != NULL) { 386 long nport; 387 388 nport = parseport(cp, -1); 389 if (nport == -1) { 390 warnx("Unknown port `%s' in %s `%s'", 391 cp, desc, origurl); 392 goto cleanup_parse_url; 393 } 394 *portnum = nport; 395 tport = cp; 396 } 397 398 if (tport != NULL) 399 *port = xstrdup(tport); 400 if (*path == NULL) 401 *path = xstrdup("/"); 402 403 if (debug) 404 fprintf(ttyout, 405 "parse_url: user `%s' pass `%s' host %s port %s(%d) " 406 "path `%s'\n", 407 *user ? *user : "<null>", *pass ? *pass : "<null>", 408 *host ? *host : "<null>", *port ? *port : "<null>", 409 *portnum ? *portnum : -1, *path ? *path : "<null>"); 410 411 return (0); 412 } 413 414 sigjmp_buf httpabort; 415 416 /* 417 * Retrieve URL, via a proxy if necessary, using HTTP. 418 * If proxyenv is set, use that for the proxy, otherwise try ftp_proxy or 419 * http_proxy as appropriate. 420 * Supports HTTP redirects. 421 * Returns 1 on failure, 0 on completed xfer, -1 if ftp connection 422 * is still open (e.g, ftp xfer with trailing /) 423 */ 424 static int 425 fetch_url(const char *url, const char *proxyenv, char *proxyauth, char *wwwauth) 426 { 427 struct addrinfo hints, *res, *res0 = NULL; 428 int error; 429 char hbuf[NI_MAXHOST]; 430 volatile sigfunc oldintr, oldintp; 431 volatile int s; 432 struct stat sb; 433 int ischunked, isproxy, rval, hcode; 434 size_t len; 435 static size_t bufsize; 436 static char *xferbuf; 437 char *cp, *ep, *buf, *savefile; 438 char *auth, *location, *message; 439 char *user, *pass, *host, *port, *path, *decodedpath; 440 char *puser, *ppass, *useragent; 441 off_t hashbytes, rangestart, rangeend, entitylen; 442 int (*closefunc)(FILE *); 443 FILE *fin, *fout; 444 time_t mtime; 445 url_t urltype; 446 in_port_t portnum; 447 448 oldintr = oldintp = NULL; 449 closefunc = NULL; 450 fin = fout = NULL; 451 s = -1; 452 buf = savefile = NULL; 453 auth = location = message = NULL; 454 ischunked = isproxy = hcode = 0; 455 rval = 1; 456 user = pass = host = path = decodedpath = puser = ppass = NULL; 457 458 #ifdef __GNUC__ /* shut up gcc warnings */ 459 (void)&closefunc; 460 (void)&fin; 461 (void)&fout; 462 (void)&buf; 463 (void)&savefile; 464 (void)&rval; 465 (void)&isproxy; 466 (void)&hcode; 467 (void)&ischunked; 468 (void)&message; 469 (void)&location; 470 (void)&auth; 471 (void)&decodedpath; 472 #endif 473 474 if (parse_url(url, "URL", &urltype, &user, &pass, &host, &port, 475 &portnum, &path) == -1) 476 goto cleanup_fetch_url; 477 478 if (urltype == FILE_URL_T && ! EMPTYSTRING(host) 479 && strcasecmp(host, "localhost") != 0) { 480 warnx("No support for non local file URL `%s'", url); 481 goto cleanup_fetch_url; 482 } 483 484 if (EMPTYSTRING(path)) { 485 if (urltype == FTP_URL_T) { 486 rval = fetch_ftp(url); 487 goto cleanup_fetch_url; 488 } 489 if (urltype != HTTP_URL_T || outfile == NULL) { 490 warnx("Invalid URL (no file after host) `%s'", url); 491 goto cleanup_fetch_url; 492 } 493 } 494 495 decodedpath = xstrdup(path); 496 url_decode(decodedpath); 497 498 if (outfile) 499 savefile = xstrdup(outfile); 500 else { 501 cp = strrchr(decodedpath, '/'); /* find savefile */ 502 if (cp != NULL) 503 savefile = xstrdup(cp + 1); 504 else 505 savefile = xstrdup(decodedpath); 506 } 507 if (EMPTYSTRING(savefile)) { 508 if (urltype == FTP_URL_T) { 509 rval = fetch_ftp(url); 510 goto cleanup_fetch_url; 511 } 512 warnx("no file after directory (you must specify an " 513 "output file) `%s'", url); 514 goto cleanup_fetch_url; 515 } else { 516 if (debug) 517 fprintf(ttyout, "got savefile as `%s'\n", savefile); 518 } 519 520 restart_point = 0; 521 filesize = -1; 522 rangestart = rangeend = entitylen = -1; 523 mtime = -1; 524 if (restartautofetch) { 525 if (strcmp(savefile, "-") != 0 && *savefile != '|' && 526 stat(savefile, &sb) == 0) 527 restart_point = sb.st_size; 528 } 529 if (urltype == FILE_URL_T) { /* file:// URLs */ 530 direction = "copied"; 531 fin = fopen(decodedpath, "r"); 532 if (fin == NULL) { 533 warn("Cannot open file `%s'", decodedpath); 534 goto cleanup_fetch_url; 535 } 536 if (fstat(fileno(fin), &sb) == 0) { 537 mtime = sb.st_mtime; 538 filesize = sb.st_size; 539 } 540 if (restart_point) { 541 if (lseek(fileno(fin), restart_point, SEEK_SET) < 0) { 542 warn("Can't lseek to restart `%s'", 543 decodedpath); 544 goto cleanup_fetch_url; 545 } 546 } 547 if (verbose) { 548 fprintf(ttyout, "Copying %s", decodedpath); 549 if (restart_point) 550 fprintf(ttyout, " (restarting at " LLF ")", 551 (LLT)restart_point); 552 fputs("\n", ttyout); 553 } 554 } else { /* ftp:// or http:// URLs */ 555 char *leading; 556 int hasleading; 557 558 if (proxyenv == NULL) { 559 if (urltype == HTTP_URL_T) 560 proxyenv = getoptionvalue("http_proxy"); 561 else if (urltype == FTP_URL_T) 562 proxyenv = getoptionvalue("ftp_proxy"); 563 } 564 direction = "retrieved"; 565 if (! EMPTYSTRING(proxyenv)) { /* use proxy */ 566 url_t purltype; 567 char *phost, *ppath; 568 char *pport, *no_proxy; 569 570 isproxy = 1; 571 572 /* check URL against list of no_proxied sites */ 573 no_proxy = getoptionvalue("no_proxy"); 574 if (! EMPTYSTRING(no_proxy)) { 575 char *np, *np_copy; 576 long np_port; 577 size_t hlen, plen; 578 579 np_copy = xstrdup(no_proxy); 580 hlen = strlen(host); 581 while ((cp = strsep(&np_copy, " ,")) != NULL) { 582 if (*cp == '\0') 583 continue; 584 if ((np = strrchr(cp, ':')) != NULL) { 585 *np = '\0'; 586 np_port = 587 strtol(np + 1, &ep, 10); 588 if (*ep != '\0') 589 continue; 590 if (np_port != portnum) 591 continue; 592 } 593 plen = strlen(cp); 594 if (hlen < plen) 595 continue; 596 if (strncasecmp(host + hlen - plen, 597 cp, plen) == 0) { 598 isproxy = 0; 599 break; 600 } 601 } 602 FREEPTR(np_copy); 603 if (isproxy == 0 && urltype == FTP_URL_T) { 604 rval = fetch_ftp(url); 605 goto cleanup_fetch_url; 606 } 607 } 608 609 if (isproxy) { 610 if (parse_url(proxyenv, "proxy URL", &purltype, 611 &puser, &ppass, &phost, &pport, &portnum, 612 &ppath) == -1) 613 goto cleanup_fetch_url; 614 615 if ((purltype != HTTP_URL_T 616 && purltype != FTP_URL_T) || 617 EMPTYSTRING(phost) || 618 (! EMPTYSTRING(ppath) 619 && strcmp(ppath, "/") != 0)) { 620 warnx("Malformed proxy URL `%s'", 621 proxyenv); 622 FREEPTR(phost); 623 FREEPTR(pport); 624 FREEPTR(ppath); 625 goto cleanup_fetch_url; 626 } 627 if (isipv6addr(host) && 628 strchr(host, '%') != NULL) { 629 warnx( 630 "Scoped address notation `%s' disallowed via web proxy", 631 host); 632 FREEPTR(phost); 633 FREEPTR(pport); 634 FREEPTR(ppath); 635 goto cleanup_fetch_url; 636 } 637 638 FREEPTR(host); 639 host = phost; 640 FREEPTR(port); 641 port = pport; 642 FREEPTR(path); 643 path = xstrdup(url); 644 FREEPTR(ppath); 645 } 646 } /* ! EMPTYSTRING(proxyenv) */ 647 648 memset(&hints, 0, sizeof(hints)); 649 hints.ai_flags = 0; 650 hints.ai_family = family; 651 hints.ai_socktype = SOCK_STREAM; 652 hints.ai_protocol = 0; 653 error = getaddrinfo(host, NULL, &hints, &res0); 654 if (error) { 655 warnx("%s", gai_strerror(error)); 656 goto cleanup_fetch_url; 657 } 658 if (res0->ai_canonname) 659 host = res0->ai_canonname; 660 661 s = -1; 662 for (res = res0; res; res = res->ai_next) { 663 /* 664 * see comment in hookup() 665 */ 666 ai_unmapped(res); 667 if (getnameinfo(res->ai_addr, res->ai_addrlen, 668 hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0) 669 strlcpy(hbuf, "invalid", sizeof(hbuf)); 670 671 if (verbose && res != res0) 672 fprintf(ttyout, "Trying %s...\n", hbuf); 673 674 ((struct sockaddr_in *)res->ai_addr)->sin_port = 675 htons(portnum); 676 s = socket(res->ai_family, SOCK_STREAM, 677 res->ai_protocol); 678 if (s < 0) { 679 warn("Can't create socket"); 680 continue; 681 } 682 683 if (xconnect(s, res->ai_addr, res->ai_addrlen) < 0) { 684 warn("Connect to address `%s'", hbuf); 685 close(s); 686 s = -1; 687 continue; 688 } 689 690 /* success */ 691 break; 692 } 693 freeaddrinfo(res0); 694 695 if (s < 0) { 696 warn("Can't connect to %s", host); 697 goto cleanup_fetch_url; 698 } 699 700 fin = fdopen(s, "r+"); 701 /* 702 * Construct and send the request. 703 */ 704 if (verbose) 705 fprintf(ttyout, "Requesting %s\n", url); 706 leading = " ("; 707 hasleading = 0; 708 if (isproxy) { 709 if (verbose) { 710 fprintf(ttyout, "%svia %s:%s", leading, 711 host, port); 712 leading = ", "; 713 hasleading++; 714 } 715 fprintf(fin, "GET %s HTTP/1.0\r\n", path); 716 if (flushcache) 717 fprintf(fin, "Pragma: no-cache\r\n"); 718 } else { 719 fprintf(fin, "GET %s HTTP/1.1\r\n", path); 720 if (strchr(host, ':')) { 721 char *h, *p; 722 723 /* 724 * strip off IPv6 scope identifier, since it is 725 * local to the node 726 */ 727 h = xstrdup(host); 728 if (isipv6addr(h) && 729 (p = strchr(h, '%')) != NULL) { 730 *p = '\0'; 731 } 732 fprintf(fin, "Host: [%s]", h); 733 free(h); 734 } else 735 fprintf(fin, "Host: %s", host); 736 if (portnum != HTTP_PORT) 737 fprintf(fin, ":%u", portnum); 738 fprintf(fin, "\r\n"); 739 fprintf(fin, "Accept: */*\r\n"); 740 fprintf(fin, "Connection: close\r\n"); 741 if (restart_point) { 742 fputs(leading, ttyout); 743 fprintf(fin, "Range: bytes=" LLF "-\r\n", 744 (LLT)restart_point); 745 fprintf(ttyout, "restarting at " LLF, 746 (LLT)restart_point); 747 leading = ", "; 748 hasleading++; 749 } 750 if (flushcache) 751 fprintf(fin, "Cache-Control: no-cache\r\n"); 752 } 753 if ((useragent=getenv("FTPUSERAGENT")) != NULL) { 754 fprintf(fin, "User-Agent: %s\r\n", useragent); 755 } else { 756 fprintf(fin, "User-Agent: %s/%s\r\n", 757 FTP_PRODUCT, FTP_VERSION); 758 } 759 if (wwwauth) { 760 if (verbose) { 761 fprintf(ttyout, "%swith authorization", 762 leading); 763 leading = ", "; 764 hasleading++; 765 } 766 fprintf(fin, "Authorization: %s\r\n", wwwauth); 767 } 768 if (proxyauth) { 769 if (verbose) { 770 fprintf(ttyout, 771 "%swith proxy authorization", leading); 772 leading = ", "; 773 hasleading++; 774 } 775 fprintf(fin, "Proxy-Authorization: %s\r\n", proxyauth); 776 } 777 if (verbose && hasleading) 778 fputs(")\n", ttyout); 779 fprintf(fin, "\r\n"); 780 if (fflush(fin) == EOF) { 781 warn("Writing HTTP request"); 782 goto cleanup_fetch_url; 783 } 784 785 /* Read the response */ 786 if ((buf = fparseln(fin, &len, NULL, "\0\0\0", 0)) == NULL) { 787 warn("Receiving HTTP reply"); 788 goto cleanup_fetch_url; 789 } 790 while (len > 0 && (buf[len-1] == '\r' || buf[len-1] == '\n')) 791 buf[--len] = '\0'; 792 if (debug) 793 fprintf(ttyout, "received `%s'\n", buf); 794 795 /* Determine HTTP response code */ 796 cp = strchr(buf, ' '); 797 if (cp == NULL) 798 goto improper; 799 else 800 cp++; 801 hcode = strtol(cp, &ep, 10); 802 if (*ep != '\0' && !isspace((unsigned char)*ep)) 803 goto improper; 804 message = xstrdup(cp); 805 806 /* Read the rest of the header. */ 807 FREEPTR(buf); 808 while (1) { 809 if ((buf = fparseln(fin, &len, NULL, "\0\0\0", 0)) 810 == NULL) { 811 warn("Receiving HTTP reply"); 812 goto cleanup_fetch_url; 813 } 814 while (len > 0 && 815 (buf[len-1] == '\r' || buf[len-1] == '\n')) 816 buf[--len] = '\0'; 817 if (len == 0) 818 break; 819 if (debug) 820 fprintf(ttyout, "received `%s'\n", buf); 821 822 /* Look for some headers */ 823 cp = buf; 824 825 #define CONTENTLEN "Content-Length: " 826 if (strncasecmp(cp, CONTENTLEN, 827 sizeof(CONTENTLEN) - 1) == 0) { 828 cp += sizeof(CONTENTLEN) - 1; 829 filesize = STRTOLL(cp, &ep, 10); 830 if (filesize < 0 || *ep != '\0') 831 goto improper; 832 if (debug) 833 fprintf(ttyout, 834 "parsed len as: " LLF "\n", 835 (LLT)filesize); 836 837 #define CONTENTRANGE "Content-Range: bytes " 838 } else if (strncasecmp(cp, CONTENTRANGE, 839 sizeof(CONTENTRANGE) - 1) == 0) { 840 cp += sizeof(CONTENTRANGE) - 1; 841 if (*cp == '*') { 842 ep = cp + 1; 843 } 844 else { 845 rangestart = STRTOLL(cp, &ep, 10); 846 if (rangestart < 0 || *ep != '-') 847 goto improper; 848 cp = ep + 1; 849 rangeend = STRTOLL(cp, &ep, 10); 850 if (rangeend < 0 || rangeend < rangestart) 851 goto improper; 852 } 853 if (*ep != '/') 854 goto improper; 855 cp = ep + 1; 856 if (*cp == '*') { 857 ep = cp + 1; 858 } 859 else { 860 entitylen = STRTOLL(cp, &ep, 10); 861 if (entitylen < 0) 862 goto improper; 863 } 864 if (*ep != '\0') 865 goto improper; 866 867 if (debug) { 868 fprintf(ttyout, "parsed range as: "); 869 if (rangestart == -1) 870 fprintf(ttyout, "*"); 871 else 872 fprintf(ttyout, LLF "-" LLF, 873 (LLT)rangestart, 874 (LLT)rangeend); 875 fprintf(ttyout, "/" LLF "\n", (LLT)entitylen); 876 } 877 if (! restart_point) { 878 warnx( 879 "Received unexpected Content-Range header"); 880 goto cleanup_fetch_url; 881 } 882 883 #define LASTMOD "Last-Modified: " 884 } else if (strncasecmp(cp, LASTMOD, 885 sizeof(LASTMOD) - 1) == 0) { 886 struct tm parsed; 887 char *t; 888 889 cp += sizeof(LASTMOD) - 1; 890 /* RFC 1123 */ 891 if ((t = strptime(cp, 892 "%a, %d %b %Y %H:%M:%S GMT", 893 &parsed)) 894 /* RFC 850 */ 895 || (t = strptime(cp, 896 "%a, %d-%b-%y %H:%M:%S GMT", 897 &parsed)) 898 /* asctime */ 899 || (t = strptime(cp, 900 "%a, %b %d %H:%M:%S %Y", 901 &parsed))) { 902 parsed.tm_isdst = -1; 903 if (*t == '\0') 904 mtime = timegm(&parsed); 905 if (debug && mtime != -1) { 906 fprintf(ttyout, 907 "parsed date as: %s", 908 ctime(&mtime)); 909 } 910 } 911 912 #define LOCATION "Location: " 913 } else if (strncasecmp(cp, LOCATION, 914 sizeof(LOCATION) - 1) == 0) { 915 cp += sizeof(LOCATION) - 1; 916 location = xstrdup(cp); 917 if (debug) 918 fprintf(ttyout, 919 "parsed location as: %s\n", cp); 920 921 #define TRANSENC "Transfer-Encoding: " 922 } else if (strncasecmp(cp, TRANSENC, 923 sizeof(TRANSENC) - 1) == 0) { 924 cp += sizeof(TRANSENC) - 1; 925 if (strcasecmp(cp, "binary") == 0) { 926 warnx( 927 "Bogus transfer encoding - `%s' (fetching anyway)", 928 cp); 929 continue; 930 } 931 if (strcasecmp(cp, "chunked") != 0) { 932 warnx( 933 "Unsupported transfer encoding - `%s'", 934 cp); 935 goto cleanup_fetch_url; 936 } 937 ischunked++; 938 if (debug) 939 fprintf(ttyout, 940 "using chunked encoding\n"); 941 942 #define PROXYAUTH "Proxy-Authenticate: " 943 } else if (strncasecmp(cp, PROXYAUTH, 944 sizeof(PROXYAUTH) - 1) == 0) { 945 cp += sizeof(PROXYAUTH) - 1; 946 FREEPTR(auth); 947 auth = xstrdup(cp); 948 if (debug) 949 fprintf(ttyout, 950 "parsed proxy-auth as: %s\n", cp); 951 952 #define WWWAUTH "WWW-Authenticate: " 953 } else if (strncasecmp(cp, WWWAUTH, 954 sizeof(WWWAUTH) - 1) == 0) { 955 cp += sizeof(WWWAUTH) - 1; 956 FREEPTR(auth); 957 auth = xstrdup(cp); 958 if (debug) 959 fprintf(ttyout, 960 "parsed www-auth as: %s\n", cp); 961 962 } 963 964 } 965 /* finished parsing header */ 966 FREEPTR(buf); 967 968 switch (hcode) { 969 case 200: 970 break; 971 case 206: 972 if (! restart_point) { 973 warnx("Not expecting partial content header"); 974 goto cleanup_fetch_url; 975 } 976 break; 977 case 300: 978 case 301: 979 case 302: 980 case 303: 981 case 305: 982 if (EMPTYSTRING(location)) { 983 warnx( 984 "No redirection Location provided by server"); 985 goto cleanup_fetch_url; 986 } 987 if (redirect_loop++ > 5) { 988 warnx("Too many redirections requested"); 989 goto cleanup_fetch_url; 990 } 991 if (hcode == 305) { 992 if (verbose) 993 fprintf(ttyout, "Redirected via %s\n", 994 location); 995 rval = fetch_url(url, location, 996 proxyauth, wwwauth); 997 } else { 998 if (verbose) 999 fprintf(ttyout, "Redirected to %s\n", 1000 location); 1001 rval = go_fetch(location); 1002 } 1003 goto cleanup_fetch_url; 1004 case 401: 1005 case 407: 1006 { 1007 char **authp; 1008 char *auser, *apass; 1009 1010 fprintf(ttyout, "%s\n", message); 1011 if (EMPTYSTRING(auth)) { 1012 warnx( 1013 "No authentication challenge provided by server"); 1014 goto cleanup_fetch_url; 1015 } 1016 if (hcode == 401) { 1017 authp = &wwwauth; 1018 auser = user; 1019 apass = pass; 1020 } else { 1021 authp = &proxyauth; 1022 auser = puser; 1023 apass = ppass; 1024 } 1025 if (*authp != NULL) { 1026 char reply[10]; 1027 1028 fprintf(ttyout, 1029 "Authorization failed. Retry (y/n)? "); 1030 if (fgets(reply, sizeof(reply), stdin) 1031 == NULL) { 1032 clearerr(stdin); 1033 goto cleanup_fetch_url; 1034 } else { 1035 if (tolower(reply[0]) != 'y') 1036 goto cleanup_fetch_url; 1037 } 1038 auser = NULL; 1039 apass = NULL; 1040 } 1041 if (auth_url(auth, authp, auser, apass) == 0) { 1042 rval = fetch_url(url, proxyenv, 1043 proxyauth, wwwauth); 1044 memset(*authp, 0, strlen(*authp)); 1045 FREEPTR(*authp); 1046 } 1047 goto cleanup_fetch_url; 1048 } 1049 default: 1050 if (message) 1051 warnx("Error retrieving file - `%s'", message); 1052 else 1053 warnx("Unknown error retrieving file"); 1054 goto cleanup_fetch_url; 1055 } 1056 } /* end of ftp:// or http:// specific setup */ 1057 1058 /* Open the output file. */ 1059 if (strcmp(savefile, "-") == 0) { 1060 fout = stdout; 1061 } else if (*savefile == '|') { 1062 oldintp = xsignal(SIGPIPE, SIG_IGN); 1063 fout = popen(savefile + 1, "w"); 1064 if (fout == NULL) { 1065 warn("Can't run `%s'", savefile + 1); 1066 goto cleanup_fetch_url; 1067 } 1068 closefunc = pclose; 1069 } else { 1070 if ((rangeend != -1 && rangeend <= restart_point) || 1071 (rangestart == -1 && filesize != -1 && filesize <= restart_point)) { 1072 /* already done */ 1073 if (verbose) 1074 fprintf(ttyout, "already done\n"); 1075 rval = 0; 1076 goto cleanup_fetch_url; 1077 } 1078 if (restart_point && rangestart != -1) { 1079 if (entitylen != -1) 1080 filesize = entitylen; 1081 if (rangestart != restart_point) { 1082 warnx( 1083 "Size of `%s' differs from save file `%s'", 1084 url, savefile); 1085 goto cleanup_fetch_url; 1086 } 1087 fout = fopen(savefile, "a"); 1088 } else 1089 fout = fopen(savefile, "w"); 1090 if (fout == NULL) { 1091 warn("Can't open `%s'", savefile); 1092 goto cleanup_fetch_url; 1093 } 1094 closefunc = fclose; 1095 } 1096 1097 /* Trap signals */ 1098 if (sigsetjmp(httpabort, 1)) 1099 goto cleanup_fetch_url; 1100 (void)xsignal(SIGQUIT, psummary); 1101 oldintr = xsignal(SIGINT, aborthttp); 1102 1103 if (rcvbuf_size > bufsize) { 1104 if (xferbuf) 1105 (void)free(xferbuf); 1106 bufsize = rcvbuf_size; 1107 xferbuf = xmalloc(bufsize); 1108 } 1109 1110 bytes = 0; 1111 hashbytes = mark; 1112 progressmeter(-1); 1113 1114 /* Finally, suck down the file. */ 1115 do { 1116 long chunksize; 1117 1118 chunksize = 0; 1119 /* read chunksize */ 1120 if (ischunked) { 1121 if (fgets(xferbuf, bufsize, fin) == NULL) { 1122 warnx("Unexpected EOF reading chunksize"); 1123 goto cleanup_fetch_url; 1124 } 1125 chunksize = strtol(xferbuf, &ep, 16); 1126 1127 /* 1128 * XXX: Work around bug in Apache 1.3.9 and 1129 * 1.3.11, which incorrectly put trailing 1130 * space after the chunksize. 1131 */ 1132 while (*ep == ' ') 1133 ep++; 1134 1135 if (strcmp(ep, "\r\n") != 0) { 1136 warnx("Unexpected data following chunksize"); 1137 goto cleanup_fetch_url; 1138 } 1139 if (debug) 1140 fprintf(ttyout, "got chunksize of " LLF "\n", 1141 (LLT)chunksize); 1142 if (chunksize == 0) 1143 break; 1144 } 1145 /* transfer file or chunk */ 1146 while (1) { 1147 struct timeval then, now, td; 1148 off_t bufrem; 1149 1150 if (rate_get) 1151 (void)gettimeofday(&then, NULL); 1152 bufrem = rate_get ? rate_get : bufsize; 1153 if (ischunked) 1154 bufrem = MIN(chunksize, bufrem); 1155 while (bufrem > 0) { 1156 len = fread(xferbuf, sizeof(char), 1157 MIN(bufsize, bufrem), fin); 1158 if (len <= 0) 1159 goto chunkdone; 1160 bytes += len; 1161 bufrem -= len; 1162 if (fwrite(xferbuf, sizeof(char), len, fout) 1163 != len) { 1164 warn("Writing `%s'", savefile); 1165 goto cleanup_fetch_url; 1166 } 1167 if (hash && !progress) { 1168 while (bytes >= hashbytes) { 1169 (void)putc('#', ttyout); 1170 hashbytes += mark; 1171 } 1172 (void)fflush(ttyout); 1173 } 1174 if (ischunked) { 1175 chunksize -= len; 1176 if (chunksize <= 0) 1177 break; 1178 } 1179 } 1180 if (rate_get) { 1181 while (1) { 1182 (void)gettimeofday(&now, NULL); 1183 timersub(&now, &then, &td); 1184 if (td.tv_sec > 0) 1185 break; 1186 usleep(1000000 - td.tv_usec); 1187 } 1188 } 1189 if (ischunked && chunksize <= 0) 1190 break; 1191 } 1192 /* read CRLF after chunk*/ 1193 chunkdone: 1194 if (ischunked) { 1195 if (fgets(xferbuf, bufsize, fin) == NULL) 1196 break; 1197 if (strcmp(xferbuf, "\r\n") != 0) { 1198 warnx("Unexpected data following chunk"); 1199 goto cleanup_fetch_url; 1200 } 1201 } 1202 } while (ischunked); 1203 if (hash && !progress && bytes > 0) { 1204 if (bytes < mark) 1205 (void)putc('#', ttyout); 1206 (void)putc('\n', ttyout); 1207 } 1208 if (ferror(fin)) { 1209 warn("Reading file"); 1210 goto cleanup_fetch_url; 1211 } 1212 progressmeter(1); 1213 (void)fflush(fout); 1214 if (closefunc == fclose && mtime != -1) { 1215 struct timeval tval[2]; 1216 1217 (void)gettimeofday(&tval[0], NULL); 1218 tval[1].tv_sec = mtime; 1219 tval[1].tv_usec = 0; 1220 (*closefunc)(fout); 1221 fout = NULL; 1222 1223 if (utimes(savefile, tval) == -1) { 1224 fprintf(ttyout, 1225 "Can't change modification time to %s", 1226 asctime(localtime(&mtime))); 1227 } 1228 } 1229 if (bytes > 0) 1230 ptransfer(0); 1231 bytes = 0; 1232 1233 rval = 0; 1234 goto cleanup_fetch_url; 1235 1236 improper: 1237 warnx("Improper response from `%s'", host); 1238 1239 cleanup_fetch_url: 1240 if (oldintr) 1241 (void)xsignal(SIGINT, oldintr); 1242 if (oldintp) 1243 (void)xsignal(SIGPIPE, oldintp); 1244 if (fin != NULL) 1245 fclose(fin); 1246 else if (s != -1) 1247 close(s); 1248 if (closefunc != NULL && fout != NULL) 1249 (*closefunc)(fout); 1250 FREEPTR(savefile); 1251 FREEPTR(user); 1252 FREEPTR(pass); 1253 FREEPTR(host); 1254 FREEPTR(port); 1255 FREEPTR(path); 1256 FREEPTR(decodedpath); 1257 FREEPTR(puser); 1258 FREEPTR(ppass); 1259 FREEPTR(buf); 1260 FREEPTR(auth); 1261 FREEPTR(location); 1262 FREEPTR(message); 1263 return (rval); 1264 } 1265 1266 /* 1267 * Abort a HTTP retrieval 1268 */ 1269 void 1270 aborthttp(int notused) 1271 { 1272 char msgbuf[100]; 1273 int len; 1274 1275 alarmtimer(0); 1276 len = strlcpy(msgbuf, "\nHTTP fetch aborted.\n", sizeof(msgbuf)); 1277 write(fileno(ttyout), msgbuf, len); 1278 siglongjmp(httpabort, 1); 1279 } 1280 1281 /* 1282 * Retrieve ftp URL or classic ftp argument using FTP. 1283 * Returns 1 on failure, 0 on completed xfer, -1 if ftp connection 1284 * is still open (e.g, ftp xfer with trailing /) 1285 */ 1286 static int 1287 fetch_ftp(const char *url) 1288 { 1289 char *cp, *xargv[5], rempath[MAXPATHLEN]; 1290 char *host, *path, *dir, *file, *user, *pass; 1291 char *port; 1292 int dirhasglob, filehasglob, oautologin, rval, type, xargc; 1293 in_port_t portnum; 1294 url_t urltype; 1295 1296 host = path = dir = file = user = pass = NULL; 1297 port = NULL; 1298 rval = 1; 1299 type = TYPE_I; 1300 1301 if (strncasecmp(url, FTP_URL, sizeof(FTP_URL) - 1) == 0) { 1302 if ((parse_url(url, "URL", &urltype, &user, &pass, 1303 &host, &port, &portnum, &path) == -1) || 1304 (user != NULL && *user == '\0') || 1305 EMPTYSTRING(host)) { 1306 warnx("Invalid URL `%s'", url); 1307 goto cleanup_fetch_ftp; 1308 } 1309 url_decode(user); 1310 url_decode(pass); 1311 /* 1312 * Note: Don't url_decode(path) here. We need to keep the 1313 * distinction between "/" and "%2F" until later. 1314 */ 1315 1316 /* check for trailing ';type=[aid]' */ 1317 if (! EMPTYSTRING(path) && (cp = strrchr(path, ';')) != NULL) { 1318 if (strcasecmp(cp, ";type=a") == 0) 1319 type = TYPE_A; 1320 else if (strcasecmp(cp, ";type=i") == 0) 1321 type = TYPE_I; 1322 else if (strcasecmp(cp, ";type=d") == 0) { 1323 warnx( 1324 "Directory listing via a URL is not supported"); 1325 goto cleanup_fetch_ftp; 1326 } else { 1327 warnx("Invalid suffix `%s' in URL `%s'", cp, 1328 url); 1329 goto cleanup_fetch_ftp; 1330 } 1331 *cp = 0; 1332 } 1333 } else { /* classic style `[user@]host:[file]' */ 1334 urltype = CLASSIC_URL_T; 1335 host = xstrdup(url); 1336 cp = strchr(host, '@'); 1337 if (cp != NULL) { 1338 *cp = '\0'; 1339 user = host; 1340 anonftp = 0; /* disable anonftp */ 1341 host = xstrdup(cp + 1); 1342 } 1343 cp = strchr(host, ':'); 1344 if (cp != NULL) { 1345 *cp = '\0'; 1346 path = xstrdup(cp + 1); 1347 } 1348 } 1349 if (EMPTYSTRING(host)) 1350 goto cleanup_fetch_ftp; 1351 1352 /* Extract the file and (if present) directory name. */ 1353 dir = path; 1354 if (! EMPTYSTRING(dir)) { 1355 /* 1356 * If we are dealing with classic `[user@]host:[path]' syntax, 1357 * then a path of the form `/file' (resulting from input of the 1358 * form `host:/file') means that we should do "CWD /" before 1359 * retrieving the file. So we set dir="/" and file="file". 1360 * 1361 * But if we are dealing with URLs like `ftp://host/path' then 1362 * a path of the form `/file' (resulting from a URL of the form 1363 * `ftp://host//file') means that we should do `CWD ' (with an 1364 * empty argument) before retrieving the file. So we set 1365 * dir="" and file="file". 1366 * 1367 * If the path does not contain / at all, we set dir=NULL. 1368 * (We get a path without any slashes if we are dealing with 1369 * classic `[user@]host:[file]' or URL `ftp://host/file'.) 1370 * 1371 * In all other cases, we set dir to a string that does not 1372 * include the final '/' that separates the dir part from the 1373 * file part of the path. (This will be the empty string if 1374 * and only if we are dealing with a path of the form `/file' 1375 * resulting from an URL of the form `ftp://host//file'.) 1376 */ 1377 cp = strrchr(dir, '/'); 1378 if (cp == dir && urltype == CLASSIC_URL_T) { 1379 file = cp + 1; 1380 dir = "/"; 1381 } else if (cp != NULL) { 1382 *cp++ = '\0'; 1383 file = cp; 1384 } else { 1385 file = dir; 1386 dir = NULL; 1387 } 1388 } else 1389 dir = NULL; 1390 if (urltype == FTP_URL_T && file != NULL) { 1391 url_decode(file); 1392 /* but still don't url_decode(dir) */ 1393 } 1394 if (debug) 1395 fprintf(ttyout, 1396 "fetch_ftp: user `%s' pass `%s' host %s port %s " 1397 "path `%s' dir `%s' file `%s'\n", 1398 user ? user : "<null>", pass ? pass : "<null>", 1399 host ? host : "<null>", port ? port : "<null>", 1400 path ? path : "<null>", 1401 dir ? dir : "<null>", file ? file : "<null>"); 1402 1403 dirhasglob = filehasglob = 0; 1404 if (doglob && urltype == CLASSIC_URL_T) { 1405 if (! EMPTYSTRING(dir) && strpbrk(dir, "*?[]{}") != NULL) 1406 dirhasglob = 1; 1407 if (! EMPTYSTRING(file) && strpbrk(file, "*?[]{}") != NULL) 1408 filehasglob = 1; 1409 } 1410 1411 /* Set up the connection */ 1412 if (connected) 1413 disconnect(0, NULL); 1414 xargv[0] = (char *)getprogname(); /* XXX discards const */ 1415 xargv[1] = host; 1416 xargv[2] = NULL; 1417 xargc = 2; 1418 if (port) { 1419 xargv[2] = port; 1420 xargv[3] = NULL; 1421 xargc = 3; 1422 } 1423 oautologin = autologin; 1424 /* don't autologin in setpeer(), use ftp_login() below */ 1425 autologin = 0; 1426 setpeer(xargc, xargv); 1427 autologin = oautologin; 1428 if ((connected == 0) || 1429 (connected == 1 && !ftp_login(host, user, pass))) { 1430 warnx("Can't connect or login to host `%s'", host); 1431 goto cleanup_fetch_ftp; 1432 } 1433 1434 switch (type) { 1435 case TYPE_A: 1436 setascii(1, xargv); 1437 break; 1438 case TYPE_I: 1439 setbinary(1, xargv); 1440 break; 1441 default: 1442 errx(1, "fetch_ftp: unknown transfer type %d", type); 1443 } 1444 1445 /* 1446 * Change directories, if necessary. 1447 * 1448 * Note: don't use EMPTYSTRING(dir) below, because 1449 * dir=="" means something different from dir==NULL. 1450 */ 1451 if (dir != NULL && !dirhasglob) { 1452 char *nextpart; 1453 1454 /* 1455 * If we are dealing with a classic `[user@]host:[path]' 1456 * (urltype is CLASSIC_URL_T) then we have a raw directory 1457 * name (not encoded in any way) and we can change 1458 * directories in one step. 1459 * 1460 * If we are dealing with an `ftp://host/path' URL 1461 * (urltype is FTP_URL_T), then RFC 1738 says we need to 1462 * send a separate CWD command for each unescaped "/" 1463 * in the path, and we have to interpret %hex escaping 1464 * *after* we find the slashes. It's possible to get 1465 * empty components here, (from multiple adjacent 1466 * slashes in the path) and RFC 1738 says that we should 1467 * still do `CWD ' (with a null argument) in such cases. 1468 * 1469 * Many ftp servers don't support `CWD ', so if there's an 1470 * error performing that command, bail out with a descriptive 1471 * message. 1472 * 1473 * Examples: 1474 * 1475 * host: dir="", urltype=CLASSIC_URL_T 1476 * logged in (to default directory) 1477 * host:file dir=NULL, urltype=CLASSIC_URL_T 1478 * "RETR file" 1479 * host:dir/ dir="dir", urltype=CLASSIC_URL_T 1480 * "CWD dir", logged in 1481 * ftp://host/ dir="", urltype=FTP_URL_T 1482 * logged in (to default directory) 1483 * ftp://host/dir/ dir="dir", urltype=FTP_URL_T 1484 * "CWD dir", logged in 1485 * ftp://host/file dir=NULL, urltype=FTP_URL_T 1486 * "RETR file" 1487 * ftp://host//file dir="", urltype=FTP_URL_T 1488 * "CWD ", "RETR file" 1489 * host:/file dir="/", urltype=CLASSIC_URL_T 1490 * "CWD /", "RETR file" 1491 * ftp://host///file dir="/", urltype=FTP_URL_T 1492 * "CWD ", "CWD ", "RETR file" 1493 * ftp://host/%2F/file dir="%2F", urltype=FTP_URL_T 1494 * "CWD /", "RETR file" 1495 * ftp://host/foo/file dir="foo", urltype=FTP_URL_T 1496 * "CWD foo", "RETR file" 1497 * ftp://host/foo/bar/file dir="foo/bar" 1498 * "CWD foo", "CWD bar", "RETR file" 1499 * ftp://host//foo/bar/file dir="/foo/bar" 1500 * "CWD ", "CWD foo", "CWD bar", "RETR file" 1501 * ftp://host/foo//bar/file dir="foo//bar" 1502 * "CWD foo", "CWD ", "CWD bar", "RETR file" 1503 * ftp://host/%2F/foo/bar/file dir="%2F/foo/bar" 1504 * "CWD /", "CWD foo", "CWD bar", "RETR file" 1505 * ftp://host/%2Ffoo/bar/file dir="%2Ffoo/bar" 1506 * "CWD /foo", "CWD bar", "RETR file" 1507 * ftp://host/%2Ffoo%2Fbar/file dir="%2Ffoo%2Fbar" 1508 * "CWD /foo/bar", "RETR file" 1509 * ftp://host/%2Ffoo%2Fbar%2Ffile dir=NULL 1510 * "RETR /foo/bar/file" 1511 * 1512 * Note that we don't need `dir' after this point. 1513 */ 1514 do { 1515 if (urltype == FTP_URL_T) { 1516 nextpart = strchr(dir, '/'); 1517 if (nextpart) { 1518 *nextpart = '\0'; 1519 nextpart++; 1520 } 1521 url_decode(dir); 1522 } else 1523 nextpart = NULL; 1524 if (debug) 1525 fprintf(ttyout, "dir `%s', nextpart `%s'\n", 1526 dir ? dir : "<null>", 1527 nextpart ? nextpart : "<null>"); 1528 if (urltype == FTP_URL_T || *dir != '\0') { 1529 xargv[0] = "cd"; 1530 xargv[1] = dir; 1531 xargv[2] = NULL; 1532 dirchange = 0; 1533 cd(2, xargv); 1534 if (! dirchange) { 1535 if (*dir == '\0' && code == 500) 1536 fprintf(stderr, 1537 "\n" 1538 "ftp: The `CWD ' command (without a directory), which is required by\n" 1539 " RFC 1738 to support the empty directory in the URL pathname (`//'),\n" 1540 " conflicts with the server's conformance to RFC 959.\n" 1541 " Try the same URL without the `//' in the URL pathname.\n" 1542 "\n"); 1543 goto cleanup_fetch_ftp; 1544 } 1545 } 1546 dir = nextpart; 1547 } while (dir != NULL); 1548 } 1549 1550 if (EMPTYSTRING(file)) { 1551 rval = -1; 1552 goto cleanup_fetch_ftp; 1553 } 1554 1555 if (dirhasglob) { 1556 (void)strlcpy(rempath, dir, sizeof(rempath)); 1557 (void)strlcat(rempath, "/", sizeof(rempath)); 1558 (void)strlcat(rempath, file, sizeof(rempath)); 1559 file = rempath; 1560 } 1561 1562 /* Fetch the file(s). */ 1563 xargc = 2; 1564 xargv[0] = "get"; 1565 xargv[1] = file; 1566 xargv[2] = NULL; 1567 if (dirhasglob || filehasglob) { 1568 int ointeractive; 1569 1570 ointeractive = interactive; 1571 interactive = 0; 1572 xargv[0] = "mget"; 1573 mget(xargc, xargv); 1574 interactive = ointeractive; 1575 } else { 1576 if (outfile == NULL) { 1577 cp = strrchr(file, '/'); /* find savefile */ 1578 if (cp != NULL) 1579 outfile = cp + 1; 1580 else 1581 outfile = file; 1582 } 1583 xargv[2] = (char *)outfile; 1584 xargv[3] = NULL; 1585 xargc++; 1586 if (restartautofetch) 1587 reget(xargc, xargv); 1588 else 1589 get(xargc, xargv); 1590 } 1591 1592 if ((code / 100) == COMPLETE) 1593 rval = 0; 1594 1595 cleanup_fetch_ftp: 1596 FREEPTR(host); 1597 FREEPTR(path); 1598 FREEPTR(user); 1599 FREEPTR(pass); 1600 return (rval); 1601 } 1602 1603 /* 1604 * Retrieve the given file to outfile. 1605 * Supports arguments of the form: 1606 * "host:path", "ftp://host/path" if $ftpproxy, call fetch_url() else 1607 * call fetch_ftp() 1608 * "http://host/path" call fetch_url() to use HTTP 1609 * "file:///path" call fetch_url() to copy 1610 * "about:..." print a message 1611 * 1612 * Returns 1 on failure, 0 on completed xfer, -1 if ftp connection 1613 * is still open (e.g, ftp xfer with trailing /) 1614 */ 1615 static int 1616 go_fetch(const char *url) 1617 { 1618 char *proxy; 1619 1620 /* 1621 * Check for about:* 1622 */ 1623 if (strncasecmp(url, ABOUT_URL, sizeof(ABOUT_URL) - 1) == 0) { 1624 url += sizeof(ABOUT_URL) -1; 1625 if (strcasecmp(url, "ftp") == 0 || 1626 strcasecmp(url, "tnftp") == 0) { 1627 fputs( 1628 "This version of ftp has been enhanced by Luke Mewburn <lukem@NetBSD.org>\n" 1629 "for the NetBSD project. Execute `man ftp' for more details.\n", ttyout); 1630 } else if (strcasecmp(url, "lukem") == 0) { 1631 fputs( 1632 "Luke Mewburn is the author of most of the enhancements in this ftp client.\n" 1633 "Please email feedback to <lukem@NetBSD.org>.\n", ttyout); 1634 } else if (strcasecmp(url, "netbsd") == 0) { 1635 fputs( 1636 "NetBSD is a freely available and redistributable UNIX-like operating system.\n" 1637 "For more information, see http://www.NetBSD.org/\n", ttyout); 1638 } else if (strcasecmp(url, "version") == 0) { 1639 fprintf(ttyout, "Version: %s %s%s\n", 1640 FTP_PRODUCT, FTP_VERSION, 1641 #ifdef INET6 1642 "" 1643 #else 1644 " (-IPv6)" 1645 #endif 1646 ); 1647 } else { 1648 fprintf(ttyout, "`%s' is an interesting topic.\n", url); 1649 } 1650 fputs("\n", ttyout); 1651 return (0); 1652 } 1653 1654 /* 1655 * Check for file:// and http:// URLs. 1656 */ 1657 if (strncasecmp(url, HTTP_URL, sizeof(HTTP_URL) - 1) == 0 || 1658 strncasecmp(url, FILE_URL, sizeof(FILE_URL) - 1) == 0) 1659 return (fetch_url(url, NULL, NULL, NULL)); 1660 1661 /* 1662 * Try FTP URL-style and host:file arguments next. 1663 * If ftpproxy is set with an FTP URL, use fetch_url() 1664 * Othewise, use fetch_ftp(). 1665 */ 1666 proxy = getoptionvalue("ftp_proxy"); 1667 if (!EMPTYSTRING(proxy) && 1668 strncasecmp(url, FTP_URL, sizeof(FTP_URL) - 1) == 0) 1669 return (fetch_url(url, NULL, NULL, NULL)); 1670 1671 return (fetch_ftp(url)); 1672 } 1673 1674 /* 1675 * Retrieve multiple files from the command line, 1676 * calling go_fetch() for each file. 1677 * 1678 * If an ftp path has a trailing "/", the path will be cd-ed into and 1679 * the connection remains open, and the function will return -1 1680 * (to indicate the connection is alive). 1681 * If an error occurs the return value will be the offset+1 in 1682 * argv[] of the file that caused a problem (i.e, argv[x] 1683 * returns x+1) 1684 * Otherwise, 0 is returned if all files retrieved successfully. 1685 */ 1686 int 1687 auto_fetch(int argc, char *argv[]) 1688 { 1689 volatile int argpos; 1690 int rval; 1691 1692 argpos = 0; 1693 1694 if (sigsetjmp(toplevel, 1)) { 1695 if (connected) 1696 disconnect(0, NULL); 1697 return (argpos + 1); 1698 } 1699 (void)xsignal(SIGINT, intr); 1700 (void)xsignal(SIGPIPE, lostpeer); 1701 1702 /* 1703 * Loop through as long as there's files to fetch. 1704 */ 1705 for (rval = 0; (rval == 0) && (argpos < argc); argpos++) { 1706 if (strchr(argv[argpos], ':') == NULL) 1707 break; 1708 redirect_loop = 0; 1709 if (!anonftp) 1710 anonftp = 2; /* Handle "automatic" transfers. */ 1711 rval = go_fetch(argv[argpos]); 1712 if (outfile != NULL && strcmp(outfile, "-") != 0 1713 && outfile[0] != '|') 1714 outfile = NULL; 1715 if (rval > 0) 1716 rval = argpos + 1; 1717 } 1718 1719 if (connected && rval != -1) 1720 disconnect(0, NULL); 1721 return (rval); 1722 } 1723 1724 1725 int 1726 auto_put(int argc, char **argv, const char *uploadserver) 1727 { 1728 char *uargv[4], *path, *pathsep; 1729 int uargc, rval, len; 1730 1731 uargc = 0; 1732 uargv[uargc++] = "mput"; 1733 uargv[uargc++] = argv[0]; 1734 uargv[2] = uargv[3] = NULL; 1735 pathsep = NULL; 1736 rval = 1; 1737 1738 if (debug) 1739 fprintf(ttyout, "auto_put: target `%s'\n", uploadserver); 1740 1741 path = xstrdup(uploadserver); 1742 len = strlen(path); 1743 if (path[len - 1] != '/' && path[len - 1] != ':') { 1744 /* 1745 * make sure we always pass a directory to auto_fetch 1746 */ 1747 if (argc > 1) { /* more than one file to upload */ 1748 int len; 1749 1750 len = strlen(uploadserver) + 2; /* path + "/" + "\0" */ 1751 free(path); 1752 path = (char *)xmalloc(len); 1753 (void)strlcpy(path, uploadserver, len); 1754 (void)strlcat(path, "/", len); 1755 } else { /* single file to upload */ 1756 uargv[0] = "put"; 1757 pathsep = strrchr(path, '/'); 1758 if (pathsep == NULL) { 1759 pathsep = strrchr(path, ':'); 1760 if (pathsep == NULL) { 1761 warnx("Invalid URL `%s'", path); 1762 goto cleanup_auto_put; 1763 } 1764 pathsep++; 1765 uargv[2] = xstrdup(pathsep); 1766 pathsep[0] = '/'; 1767 } else 1768 uargv[2] = xstrdup(pathsep + 1); 1769 pathsep[1] = '\0'; 1770 uargc++; 1771 } 1772 } 1773 if (debug) 1774 fprintf(ttyout, "auto_put: URL `%s' argv[2] `%s'\n", 1775 path, uargv[2] ? uargv[2] : "<null>"); 1776 1777 /* connect and cwd */ 1778 rval = auto_fetch(1, &path); 1779 free(path); 1780 if(rval >= 0) 1781 goto cleanup_auto_put; 1782 1783 /* XXX : is this the best way? */ 1784 if (uargc == 3) { 1785 uargv[1] = argv[0]; 1786 put(uargc, uargv); 1787 goto cleanup_auto_put; 1788 } 1789 1790 for(; argv[0] != NULL; argv++) { 1791 uargv[1] = argv[0]; 1792 mput(uargc, uargv); 1793 } 1794 rval = 0; 1795 1796 cleanup_auto_put: 1797 FREEPTR(uargv[2]); 1798 return (rval); 1799 } 1800