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