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