1 /* $NetBSD: fetch.c,v 1.152 2004/08/08 13:52:04 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.152 2004/08/08 13:52:04 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 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 } else { 1050 if (tolower(reply[0]) != 'y') 1051 goto cleanup_fetch_url; 1052 } 1053 auser = NULL; 1054 apass = NULL; 1055 } 1056 if (auth_url(auth, authp, auser, apass) == 0) { 1057 rval = fetch_url(url, proxyenv, 1058 proxyauth, wwwauth); 1059 memset(*authp, 0, strlen(*authp)); 1060 FREEPTR(*authp); 1061 } 1062 goto cleanup_fetch_url; 1063 } 1064 #endif 1065 default: 1066 if (message) 1067 warnx("Error retrieving file - `%s'", message); 1068 else 1069 warnx("Unknown error retrieving file"); 1070 goto cleanup_fetch_url; 1071 } 1072 } /* end of ftp:// or http:// specific setup */ 1073 1074 /* Open the output file. */ 1075 if (strcmp(savefile, "-") == 0) { 1076 fout = stdout; 1077 } else if (*savefile == '|') { 1078 oldintp = xsignal(SIGPIPE, SIG_IGN); 1079 fout = popen(savefile + 1, "w"); 1080 if (fout == NULL) { 1081 warn("Can't run `%s'", savefile + 1); 1082 goto cleanup_fetch_url; 1083 } 1084 closefunc = pclose; 1085 } else { 1086 if ((rangeend != -1 && rangeend <= restart_point) || 1087 (rangestart == -1 && filesize != -1 && filesize <= restart_point)) { 1088 /* already done */ 1089 if (verbose) 1090 fprintf(ttyout, "already done\n"); 1091 rval = 0; 1092 goto cleanup_fetch_url; 1093 } 1094 if (restart_point && rangestart != -1) { 1095 if (entitylen != -1) 1096 filesize = entitylen; 1097 if (rangestart != restart_point) { 1098 warnx( 1099 "Size of `%s' differs from save file `%s'", 1100 url, savefile); 1101 goto cleanup_fetch_url; 1102 } 1103 fout = fopen(savefile, "a"); 1104 } else 1105 fout = fopen(savefile, "w"); 1106 if (fout == NULL) { 1107 warn("Can't open `%s'", savefile); 1108 goto cleanup_fetch_url; 1109 } 1110 closefunc = fclose; 1111 } 1112 1113 /* Trap signals */ 1114 if (sigsetjmp(httpabort, 1)) 1115 goto cleanup_fetch_url; 1116 (void)xsignal(SIGQUIT, psummary); 1117 oldintr = xsignal(SIGINT, aborthttp); 1118 1119 if (rcvbuf_size > bufsize) { 1120 if (xferbuf) 1121 (void)free(xferbuf); 1122 bufsize = rcvbuf_size; 1123 xferbuf = xmalloc(bufsize); 1124 } 1125 1126 bytes = 0; 1127 hashbytes = mark; 1128 progressmeter(-1); 1129 1130 /* Finally, suck down the file. */ 1131 do { 1132 long chunksize; 1133 1134 chunksize = 0; 1135 /* read chunksize */ 1136 if (ischunked) { 1137 if (fgets(xferbuf, bufsize, fin) == NULL) { 1138 warnx("Unexpected EOF reading chunksize"); 1139 goto cleanup_fetch_url; 1140 } 1141 chunksize = strtol(xferbuf, &ep, 16); 1142 1143 /* 1144 * XXX: Work around bug in Apache 1.3.9 and 1145 * 1.3.11, which incorrectly put trailing 1146 * space after the chunksize. 1147 */ 1148 while (*ep == ' ') 1149 ep++; 1150 1151 if (strcmp(ep, "\r\n") != 0) { 1152 warnx("Unexpected data following chunksize"); 1153 goto cleanup_fetch_url; 1154 } 1155 if (debug) 1156 fprintf(ttyout, "got chunksize of " LLF "\n", 1157 (LLT)chunksize); 1158 if (chunksize == 0) 1159 break; 1160 } 1161 /* transfer file or chunk */ 1162 while (1) { 1163 struct timeval then, now, td; 1164 off_t bufrem; 1165 1166 if (rate_get) 1167 (void)gettimeofday(&then, NULL); 1168 bufrem = rate_get ? rate_get : bufsize; 1169 if (ischunked) 1170 bufrem = MIN(chunksize, bufrem); 1171 while (bufrem > 0) { 1172 len = fread(xferbuf, sizeof(char), 1173 MIN(bufsize, bufrem), fin); 1174 if (len <= 0) 1175 goto chunkdone; 1176 bytes += len; 1177 bufrem -= len; 1178 if (fwrite(xferbuf, sizeof(char), len, fout) 1179 != len) { 1180 warn("Writing `%s'", savefile); 1181 goto cleanup_fetch_url; 1182 } 1183 if (hash && !progress) { 1184 while (bytes >= hashbytes) { 1185 (void)putc('#', ttyout); 1186 hashbytes += mark; 1187 } 1188 (void)fflush(ttyout); 1189 } 1190 if (ischunked) { 1191 chunksize -= len; 1192 if (chunksize <= 0) 1193 break; 1194 } 1195 } 1196 if (rate_get) { 1197 while (1) { 1198 (void)gettimeofday(&now, NULL); 1199 timersub(&now, &then, &td); 1200 if (td.tv_sec > 0) 1201 break; 1202 usleep(1000000 - td.tv_usec); 1203 } 1204 } 1205 if (ischunked && chunksize <= 0) 1206 break; 1207 } 1208 /* read CRLF after chunk*/ 1209 chunkdone: 1210 if (ischunked) { 1211 if (fgets(xferbuf, bufsize, fin) == NULL) 1212 break; 1213 if (strcmp(xferbuf, "\r\n") != 0) { 1214 warnx("Unexpected data following chunk"); 1215 goto cleanup_fetch_url; 1216 } 1217 } 1218 } while (ischunked); 1219 if (hash && !progress && bytes > 0) { 1220 if (bytes < mark) 1221 (void)putc('#', ttyout); 1222 (void)putc('\n', ttyout); 1223 } 1224 if (ferror(fin)) { 1225 warn("Reading file"); 1226 goto cleanup_fetch_url; 1227 } 1228 progressmeter(1); 1229 (void)fflush(fout); 1230 if (closefunc == fclose && mtime != -1) { 1231 struct timeval tval[2]; 1232 1233 (void)gettimeofday(&tval[0], NULL); 1234 tval[1].tv_sec = mtime; 1235 tval[1].tv_usec = 0; 1236 (*closefunc)(fout); 1237 fout = NULL; 1238 1239 if (utimes(savefile, tval) == -1) { 1240 fprintf(ttyout, 1241 "Can't change modification time to %s", 1242 asctime(localtime(&mtime))); 1243 } 1244 } 1245 if (bytes > 0) 1246 ptransfer(0); 1247 bytes = 0; 1248 1249 rval = 0; 1250 goto cleanup_fetch_url; 1251 1252 improper: 1253 warnx("Improper response from `%s'", host); 1254 1255 cleanup_fetch_url: 1256 if (oldintr) 1257 (void)xsignal(SIGINT, oldintr); 1258 if (oldintp) 1259 (void)xsignal(SIGPIPE, oldintp); 1260 if (fin != NULL) 1261 fclose(fin); 1262 else if (s != -1) 1263 close(s); 1264 if (closefunc != NULL && fout != NULL) 1265 (*closefunc)(fout); 1266 FREEPTR(savefile); 1267 FREEPTR(user); 1268 FREEPTR(pass); 1269 FREEPTR(host); 1270 FREEPTR(port); 1271 FREEPTR(path); 1272 FREEPTR(decodedpath); 1273 FREEPTR(puser); 1274 FREEPTR(ppass); 1275 FREEPTR(buf); 1276 FREEPTR(auth); 1277 FREEPTR(location); 1278 FREEPTR(message); 1279 return (rval); 1280 } 1281 1282 /* 1283 * Abort a HTTP retrieval 1284 */ 1285 void 1286 aborthttp(int notused) 1287 { 1288 char msgbuf[100]; 1289 int len; 1290 1291 sigint_raised = 1; 1292 alarmtimer(0); 1293 len = strlcpy(msgbuf, "\nHTTP fetch aborted.\n", sizeof(msgbuf)); 1294 write(fileno(ttyout), msgbuf, len); 1295 siglongjmp(httpabort, 1); 1296 } 1297 1298 /* 1299 * Retrieve ftp URL or classic ftp argument using FTP. 1300 * Returns 1 on failure, 0 on completed xfer, -1 if ftp connection 1301 * is still open (e.g, ftp xfer with trailing /) 1302 */ 1303 static int 1304 fetch_ftp(const char *url) 1305 { 1306 char *cp, *xargv[5], rempath[MAXPATHLEN]; 1307 char *host, *path, *dir, *file, *user, *pass; 1308 char *port; 1309 int dirhasglob, filehasglob, oautologin, rval, type, xargc; 1310 in_port_t portnum; 1311 url_t urltype; 1312 1313 host = path = dir = file = user = pass = NULL; 1314 port = NULL; 1315 rval = 1; 1316 type = TYPE_I; 1317 1318 if (STRNEQUAL(url, FTP_URL)) { 1319 if ((parse_url(url, "URL", &urltype, &user, &pass, 1320 &host, &port, &portnum, &path) == -1) || 1321 (user != NULL && *user == '\0') || 1322 EMPTYSTRING(host)) { 1323 warnx("Invalid URL `%s'", url); 1324 goto cleanup_fetch_ftp; 1325 } 1326 url_decode(user); 1327 url_decode(pass); 1328 /* 1329 * Note: Don't url_decode(path) here. We need to keep the 1330 * distinction between "/" and "%2F" until later. 1331 */ 1332 1333 /* check for trailing ';type=[aid]' */ 1334 if (! EMPTYSTRING(path) && (cp = strrchr(path, ';')) != NULL) { 1335 if (strcasecmp(cp, ";type=a") == 0) 1336 type = TYPE_A; 1337 else if (strcasecmp(cp, ";type=i") == 0) 1338 type = TYPE_I; 1339 else if (strcasecmp(cp, ";type=d") == 0) { 1340 warnx( 1341 "Directory listing via a URL is not supported"); 1342 goto cleanup_fetch_ftp; 1343 } else { 1344 warnx("Invalid suffix `%s' in URL `%s'", cp, 1345 url); 1346 goto cleanup_fetch_ftp; 1347 } 1348 *cp = 0; 1349 } 1350 } else { /* classic style `[user@]host:[file]' */ 1351 urltype = CLASSIC_URL_T; 1352 host = xstrdup(url); 1353 cp = strchr(host, '@'); 1354 if (cp != NULL) { 1355 *cp = '\0'; 1356 user = host; 1357 anonftp = 0; /* disable anonftp */ 1358 host = xstrdup(cp + 1); 1359 } 1360 cp = strchr(host, ':'); 1361 if (cp != NULL) { 1362 *cp = '\0'; 1363 path = xstrdup(cp + 1); 1364 } 1365 } 1366 if (EMPTYSTRING(host)) 1367 goto cleanup_fetch_ftp; 1368 1369 /* Extract the file and (if present) directory name. */ 1370 dir = path; 1371 if (! EMPTYSTRING(dir)) { 1372 /* 1373 * If we are dealing with classic `[user@]host:[path]' syntax, 1374 * then a path of the form `/file' (resulting from input of the 1375 * form `host:/file') means that we should do "CWD /" before 1376 * retrieving the file. So we set dir="/" and file="file". 1377 * 1378 * But if we are dealing with URLs like `ftp://host/path' then 1379 * a path of the form `/file' (resulting from a URL of the form 1380 * `ftp://host//file') means that we should do `CWD ' (with an 1381 * empty argument) before retrieving the file. So we set 1382 * dir="" and file="file". 1383 * 1384 * If the path does not contain / at all, we set dir=NULL. 1385 * (We get a path without any slashes if we are dealing with 1386 * classic `[user@]host:[file]' or URL `ftp://host/file'.) 1387 * 1388 * In all other cases, we set dir to a string that does not 1389 * include the final '/' that separates the dir part from the 1390 * file part of the path. (This will be the empty string if 1391 * and only if we are dealing with a path of the form `/file' 1392 * resulting from an URL of the form `ftp://host//file'.) 1393 */ 1394 cp = strrchr(dir, '/'); 1395 if (cp == dir && urltype == CLASSIC_URL_T) { 1396 file = cp + 1; 1397 dir = "/"; 1398 } else if (cp != NULL) { 1399 *cp++ = '\0'; 1400 file = cp; 1401 } else { 1402 file = dir; 1403 dir = NULL; 1404 } 1405 } else 1406 dir = NULL; 1407 if (urltype == FTP_URL_T && file != NULL) { 1408 url_decode(file); 1409 /* but still don't url_decode(dir) */ 1410 } 1411 if (debug) 1412 fprintf(ttyout, 1413 "fetch_ftp: user `%s' pass `%s' host %s port %s " 1414 "path `%s' dir `%s' file `%s'\n", 1415 user ? user : "<null>", pass ? pass : "<null>", 1416 host ? host : "<null>", port ? port : "<null>", 1417 path ? path : "<null>", 1418 dir ? dir : "<null>", file ? file : "<null>"); 1419 1420 dirhasglob = filehasglob = 0; 1421 if (doglob && urltype == CLASSIC_URL_T) { 1422 if (! EMPTYSTRING(dir) && strpbrk(dir, "*?[]{}") != NULL) 1423 dirhasglob = 1; 1424 if (! EMPTYSTRING(file) && strpbrk(file, "*?[]{}") != NULL) 1425 filehasglob = 1; 1426 } 1427 1428 /* Set up the connection */ 1429 if (connected) 1430 disconnect(0, NULL); 1431 xargv[0] = (char *)getprogname(); /* XXX discards const */ 1432 xargv[1] = host; 1433 xargv[2] = NULL; 1434 xargc = 2; 1435 if (port) { 1436 xargv[2] = port; 1437 xargv[3] = NULL; 1438 xargc = 3; 1439 } 1440 oautologin = autologin; 1441 /* don't autologin in setpeer(), use ftp_login() below */ 1442 autologin = 0; 1443 setpeer(xargc, xargv); 1444 autologin = oautologin; 1445 if ((connected == 0) || 1446 (connected == 1 && !ftp_login(host, user, pass))) { 1447 warnx("Can't connect or login to host `%s'", host); 1448 goto cleanup_fetch_ftp; 1449 } 1450 1451 switch (type) { 1452 case TYPE_A: 1453 setascii(1, xargv); 1454 break; 1455 case TYPE_I: 1456 setbinary(1, xargv); 1457 break; 1458 default: 1459 errx(1, "fetch_ftp: unknown transfer type %d", type); 1460 } 1461 1462 /* 1463 * Change directories, if necessary. 1464 * 1465 * Note: don't use EMPTYSTRING(dir) below, because 1466 * dir=="" means something different from dir==NULL. 1467 */ 1468 if (dir != NULL && !dirhasglob) { 1469 char *nextpart; 1470 1471 /* 1472 * If we are dealing with a classic `[user@]host:[path]' 1473 * (urltype is CLASSIC_URL_T) then we have a raw directory 1474 * name (not encoded in any way) and we can change 1475 * directories in one step. 1476 * 1477 * If we are dealing with an `ftp://host/path' URL 1478 * (urltype is FTP_URL_T), then RFC 1738 says we need to 1479 * send a separate CWD command for each unescaped "/" 1480 * in the path, and we have to interpret %hex escaping 1481 * *after* we find the slashes. It's possible to get 1482 * empty components here, (from multiple adjacent 1483 * slashes in the path) and RFC 1738 says that we should 1484 * still do `CWD ' (with a null argument) in such cases. 1485 * 1486 * Many ftp servers don't support `CWD ', so if there's an 1487 * error performing that command, bail out with a descriptive 1488 * message. 1489 * 1490 * Examples: 1491 * 1492 * host: dir="", urltype=CLASSIC_URL_T 1493 * logged in (to default directory) 1494 * host:file dir=NULL, urltype=CLASSIC_URL_T 1495 * "RETR file" 1496 * host:dir/ dir="dir", urltype=CLASSIC_URL_T 1497 * "CWD dir", logged in 1498 * ftp://host/ dir="", urltype=FTP_URL_T 1499 * logged in (to default directory) 1500 * ftp://host/dir/ dir="dir", urltype=FTP_URL_T 1501 * "CWD dir", logged in 1502 * ftp://host/file dir=NULL, urltype=FTP_URL_T 1503 * "RETR file" 1504 * ftp://host//file dir="", urltype=FTP_URL_T 1505 * "CWD ", "RETR file" 1506 * host:/file dir="/", urltype=CLASSIC_URL_T 1507 * "CWD /", "RETR file" 1508 * ftp://host///file dir="/", urltype=FTP_URL_T 1509 * "CWD ", "CWD ", "RETR file" 1510 * ftp://host/%2F/file dir="%2F", urltype=FTP_URL_T 1511 * "CWD /", "RETR file" 1512 * ftp://host/foo/file dir="foo", urltype=FTP_URL_T 1513 * "CWD foo", "RETR file" 1514 * ftp://host/foo/bar/file dir="foo/bar" 1515 * "CWD foo", "CWD bar", "RETR file" 1516 * ftp://host//foo/bar/file dir="/foo/bar" 1517 * "CWD ", "CWD foo", "CWD bar", "RETR file" 1518 * ftp://host/foo//bar/file dir="foo//bar" 1519 * "CWD foo", "CWD ", "CWD bar", "RETR file" 1520 * ftp://host/%2F/foo/bar/file dir="%2F/foo/bar" 1521 * "CWD /", "CWD foo", "CWD bar", "RETR file" 1522 * ftp://host/%2Ffoo/bar/file dir="%2Ffoo/bar" 1523 * "CWD /foo", "CWD bar", "RETR file" 1524 * ftp://host/%2Ffoo%2Fbar/file dir="%2Ffoo%2Fbar" 1525 * "CWD /foo/bar", "RETR file" 1526 * ftp://host/%2Ffoo%2Fbar%2Ffile dir=NULL 1527 * "RETR /foo/bar/file" 1528 * 1529 * Note that we don't need `dir' after this point. 1530 */ 1531 do { 1532 if (urltype == FTP_URL_T) { 1533 nextpart = strchr(dir, '/'); 1534 if (nextpart) { 1535 *nextpart = '\0'; 1536 nextpart++; 1537 } 1538 url_decode(dir); 1539 } else 1540 nextpart = NULL; 1541 if (debug) 1542 fprintf(ttyout, "dir `%s', nextpart `%s'\n", 1543 dir ? dir : "<null>", 1544 nextpart ? nextpart : "<null>"); 1545 if (urltype == FTP_URL_T || *dir != '\0') { 1546 xargv[0] = "cd"; 1547 xargv[1] = dir; 1548 xargv[2] = NULL; 1549 dirchange = 0; 1550 cd(2, xargv); 1551 if (! dirchange) { 1552 if (*dir == '\0' && code == 500) 1553 fprintf(stderr, 1554 "\n" 1555 "ftp: The `CWD ' command (without a directory), which is required by\n" 1556 " RFC 1738 to support the empty directory in the URL pathname (`//'),\n" 1557 " conflicts with the server's conformance to RFC 959.\n" 1558 " Try the same URL without the `//' in the URL pathname.\n" 1559 "\n"); 1560 goto cleanup_fetch_ftp; 1561 } 1562 } 1563 dir = nextpart; 1564 } while (dir != NULL); 1565 } 1566 1567 if (EMPTYSTRING(file)) { 1568 rval = -1; 1569 goto cleanup_fetch_ftp; 1570 } 1571 1572 if (dirhasglob) { 1573 (void)strlcpy(rempath, dir, sizeof(rempath)); 1574 (void)strlcat(rempath, "/", sizeof(rempath)); 1575 (void)strlcat(rempath, file, sizeof(rempath)); 1576 file = rempath; 1577 } 1578 1579 /* Fetch the file(s). */ 1580 xargc = 2; 1581 xargv[0] = "get"; 1582 xargv[1] = file; 1583 xargv[2] = NULL; 1584 if (dirhasglob || filehasglob) { 1585 int ointeractive; 1586 1587 ointeractive = interactive; 1588 interactive = 0; 1589 xargv[0] = "mget"; 1590 mget(xargc, xargv); 1591 interactive = ointeractive; 1592 } else { 1593 if (outfile == NULL) { 1594 cp = strrchr(file, '/'); /* find savefile */ 1595 if (cp != NULL) 1596 outfile = cp + 1; 1597 else 1598 outfile = file; 1599 } 1600 xargv[2] = (char *)outfile; 1601 xargv[3] = NULL; 1602 xargc++; 1603 if (restartautofetch) 1604 reget(xargc, xargv); 1605 else 1606 get(xargc, xargv); 1607 } 1608 1609 if ((code / 100) == COMPLETE) 1610 rval = 0; 1611 1612 cleanup_fetch_ftp: 1613 FREEPTR(host); 1614 FREEPTR(path); 1615 FREEPTR(user); 1616 FREEPTR(pass); 1617 return (rval); 1618 } 1619 1620 /* 1621 * Retrieve the given file to outfile. 1622 * Supports arguments of the form: 1623 * "host:path", "ftp://host/path" if $ftpproxy, call fetch_url() else 1624 * call fetch_ftp() 1625 * "http://host/path" call fetch_url() to use HTTP 1626 * "file:///path" call fetch_url() to copy 1627 * "about:..." print a message 1628 * 1629 * Returns 1 on failure, 0 on completed xfer, -1 if ftp connection 1630 * is still open (e.g, ftp xfer with trailing /) 1631 */ 1632 static int 1633 go_fetch(const char *url) 1634 { 1635 char *proxy; 1636 1637 #ifndef NO_ABOUT 1638 /* 1639 * Check for about:* 1640 */ 1641 if (STRNEQUAL(url, ABOUT_URL)) { 1642 url += sizeof(ABOUT_URL) -1; 1643 if (strcasecmp(url, "ftp") == 0 || 1644 strcasecmp(url, "tnftp") == 0) { 1645 fputs( 1646 "This version of ftp has been enhanced by Luke Mewburn <lukem@NetBSD.org>\n" 1647 "for the NetBSD project. Execute `man ftp' for more details.\n", ttyout); 1648 } else if (strcasecmp(url, "lukem") == 0) { 1649 fputs( 1650 "Luke Mewburn is the author of most of the enhancements in this ftp client.\n" 1651 "Please email feedback to <lukem@NetBSD.org>.\n", ttyout); 1652 } else if (strcasecmp(url, "netbsd") == 0) { 1653 fputs( 1654 "NetBSD is a freely available and redistributable UNIX-like operating system.\n" 1655 "For more information, see http://www.NetBSD.org/\n", ttyout); 1656 } else if (strcasecmp(url, "version") == 0) { 1657 fprintf(ttyout, "Version: %s %s%s\n", 1658 FTP_PRODUCT, FTP_VERSION, 1659 #ifdef INET6 1660 "" 1661 #else 1662 " (-IPv6)" 1663 #endif 1664 ); 1665 } else { 1666 fprintf(ttyout, "`%s' is an interesting topic.\n", url); 1667 } 1668 fputs("\n", ttyout); 1669 return (0); 1670 } 1671 #endif 1672 1673 /* 1674 * Check for file:// and http:// URLs. 1675 */ 1676 if (STRNEQUAL(url, HTTP_URL) || STRNEQUAL(url, FILE_URL)) 1677 return (fetch_url(url, NULL, NULL, NULL)); 1678 1679 /* 1680 * Try FTP URL-style and host:file arguments next. 1681 * If ftpproxy is set with an FTP URL, use fetch_url() 1682 * Othewise, use fetch_ftp(). 1683 */ 1684 proxy = getoptionvalue("ftp_proxy"); 1685 if (!EMPTYSTRING(proxy) && STRNEQUAL(url, FTP_URL)) 1686 return (fetch_url(url, NULL, NULL, NULL)); 1687 1688 return (fetch_ftp(url)); 1689 } 1690 1691 /* 1692 * Retrieve multiple files from the command line, 1693 * calling go_fetch() for each file. 1694 * 1695 * If an ftp path has a trailing "/", the path will be cd-ed into and 1696 * the connection remains open, and the function will return -1 1697 * (to indicate the connection is alive). 1698 * If an error occurs the return value will be the offset+1 in 1699 * argv[] of the file that caused a problem (i.e, argv[x] 1700 * returns x+1) 1701 * Otherwise, 0 is returned if all files retrieved successfully. 1702 */ 1703 int 1704 auto_fetch(int argc, char *argv[]) 1705 { 1706 volatile int argpos; 1707 int rval; 1708 1709 argpos = 0; 1710 1711 if (sigsetjmp(toplevel, 1)) { 1712 if (connected) 1713 disconnect(0, NULL); 1714 if (rval > 0) 1715 rval = argpos + 1; 1716 return (rval); 1717 } 1718 (void)xsignal(SIGINT, intr); 1719 (void)xsignal(SIGPIPE, lostpeer); 1720 1721 /* 1722 * Loop through as long as there's files to fetch. 1723 */ 1724 for (rval = 0; (rval == 0) && (argpos < argc); argpos++) { 1725 if (strchr(argv[argpos], ':') == NULL) 1726 break; 1727 redirect_loop = 0; 1728 if (!anonftp) 1729 anonftp = 2; /* Handle "automatic" transfers. */ 1730 rval = go_fetch(argv[argpos]); 1731 if (outfile != NULL && strcmp(outfile, "-") != 0 1732 && outfile[0] != '|') 1733 outfile = NULL; 1734 if (rval > 0) 1735 rval = argpos + 1; 1736 } 1737 1738 if (connected && rval != -1) 1739 disconnect(0, NULL); 1740 return (rval); 1741 } 1742 1743 1744 int 1745 auto_put(int argc, char **argv, const char *uploadserver) 1746 { 1747 char *uargv[4], *path, *pathsep; 1748 int uargc, rval, len; 1749 1750 uargc = 0; 1751 uargv[uargc++] = "mput"; 1752 uargv[uargc++] = argv[0]; 1753 uargv[2] = uargv[3] = NULL; 1754 pathsep = NULL; 1755 rval = 1; 1756 1757 if (debug) 1758 fprintf(ttyout, "auto_put: target `%s'\n", uploadserver); 1759 1760 path = xstrdup(uploadserver); 1761 len = strlen(path); 1762 if (path[len - 1] != '/' && path[len - 1] != ':') { 1763 /* 1764 * make sure we always pass a directory to auto_fetch 1765 */ 1766 if (argc > 1) { /* more than one file to upload */ 1767 int len; 1768 1769 len = strlen(uploadserver) + 2; /* path + "/" + "\0" */ 1770 free(path); 1771 path = (char *)xmalloc(len); 1772 (void)strlcpy(path, uploadserver, len); 1773 (void)strlcat(path, "/", len); 1774 } else { /* single file to upload */ 1775 uargv[0] = "put"; 1776 pathsep = strrchr(path, '/'); 1777 if (pathsep == NULL) { 1778 pathsep = strrchr(path, ':'); 1779 if (pathsep == NULL) { 1780 warnx("Invalid URL `%s'", path); 1781 goto cleanup_auto_put; 1782 } 1783 pathsep++; 1784 uargv[2] = xstrdup(pathsep); 1785 pathsep[0] = '/'; 1786 } else 1787 uargv[2] = xstrdup(pathsep + 1); 1788 pathsep[1] = '\0'; 1789 uargc++; 1790 } 1791 } 1792 if (debug) 1793 fprintf(ttyout, "auto_put: URL `%s' argv[2] `%s'\n", 1794 path, uargv[2] ? uargv[2] : "<null>"); 1795 1796 /* connect and cwd */ 1797 rval = auto_fetch(1, &path); 1798 free(path); 1799 if(rval >= 0) 1800 goto cleanup_auto_put; 1801 1802 /* XXX : is this the best way? */ 1803 if (uargc == 3) { 1804 uargv[1] = argv[0]; 1805 put(uargc, uargv); 1806 goto cleanup_auto_put; 1807 } 1808 1809 for(; argv[0] != NULL; argv++) { 1810 uargv[1] = argv[0]; 1811 mput(uargc, uargv); 1812 } 1813 rval = 0; 1814 1815 cleanup_auto_put: 1816 FREEPTR(uargv[2]); 1817 return (rval); 1818 } 1819