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