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