fetch.c revision 1.51 1 /* $NetBSD: fetch.c,v 1.51 1999/03/15 08:52:17 christos Exp $ */
2
3 /*-
4 * Copyright (c) 1997, 1998, 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason Thorpe and 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.51 1999/03/15 08:52:17 christos 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 #include <sys/utsname.h>
54
55 #include <netinet/in.h>
56
57 #include <arpa/ftp.h>
58 #include <arpa/inet.h>
59
60 #include <ctype.h>
61 #include <err.h>
62 #include <errno.h>
63 #include <netdb.h>
64 #include <fcntl.h>
65 #include <signal.h>
66 #include <stdio.h>
67 #include <stdlib.h>
68 #include <string.h>
69 #include <unistd.h>
70 #include <util.h>
71
72 #include "ftp_var.h"
73
74 typedef enum {
75 UNKNOWN_URL_T=-1,
76 HTTP_URL_T,
77 FTP_URL_T,
78 FILE_URL_T
79 } url_t;
80
81 void aborthttp __P((int));
82 static int auth_url __P((const char *, char **));
83 static void base64_encode __P((const char *, size_t, char *));
84 static int go_fetch __P((const char *, const char *));
85 static int fetch_ftp __P((const char *, const char *));
86 static int fetch_url __P((const char *, const char *, const char *,
87 char *, char *));
88 static int parse_url __P((const char *, const char *, url_t *, char **,
89 char **, char **, in_port_t *, char **));
90 static void url_decode __P((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 #define EMPTYSTRING(x) ((x) == NULL || (*(x) == '\0'))
102 #define FREEPTR(x) if ((x) != NULL) { free(x); (x) = NULL; }
103
104 /*
105 * Generate authorization response based on given authentication challenge.
106 * Returns -1 if an error occurred, otherwise 0.
107 * Sets response to a malloc(3)ed string; caller should free.
108 */
109 static int
110 auth_url(challenge, response)
111 const char *challenge;
112 char **response;
113 {
114 char *cp, *ep, *clear, *line, *realm, *scheme;
115 char user[BUFSIZ], *pass;
116 int rval;
117 size_t len;
118
119 *response = NULL;
120 clear = realm = scheme = NULL;
121 rval = -1;
122 line = xstrdup(challenge);
123 cp = line;
124
125 if (debug)
126 fprintf(ttyout, "auth_url: challenge `%s'\n", challenge);
127
128 scheme = strsep(&cp, " ");
129 #define SCHEME_BASIC "Basic"
130 if (strncasecmp(scheme, SCHEME_BASIC, sizeof(SCHEME_BASIC) - 1) != 0) {
131 warnx("Unsupported WWW Authentication challenge - `%s'",
132 challenge);
133 goto cleanup_auth_url;
134 }
135 cp += strspn(cp, " ");
136
137 #define REALM "realm=\""
138 if (strncasecmp(cp, REALM, sizeof(REALM) - 1) == 0)
139 cp += sizeof(REALM) - 1;
140 else {
141 warnx("Unsupported WWW Authentication challenge - `%s'",
142 challenge);
143 goto cleanup_auth_url;
144 }
145 if ((ep = strchr(cp, '\"')) != NULL) {
146 size_t len = ep - cp;
147
148 realm = (char *)xmalloc(len + 1);
149 strncpy(realm, cp, len);
150 realm[len] = '\0';
151 } else {
152 warnx("Unsupported WWW Authentication challenge - `%s'",
153 challenge);
154 goto cleanup_auth_url;
155 }
156
157 fprintf(ttyout, "Username for `%s': ", realm);
158 (void)fflush(ttyout);
159 if (fgets(user, sizeof(user) - 1, stdin) == NULL)
160 goto cleanup_auth_url;
161 user[strlen(user) - 1] = '\0';
162 pass = getpass("Password: ");
163
164 len = strlen(user) + strlen(pass) + 1; /* user + ":" + pass */
165 clear = (char *)xmalloc(len + 1);
166 sprintf(clear, "%s:%s", user, pass);
167 memset(pass, '\0', strlen(pass));
168
169 /* scheme + " " + enc */
170 len = strlen(scheme) + 1 + (len + 2) * 4 / 3;
171 *response = (char *)xmalloc(len + 1);
172 len = sprintf(*response, "%s ", scheme);
173 base64_encode(clear, strlen(clear), *response + len);
174 rval = 0;
175
176 cleanup_auth_url:
177 FREEPTR(clear);
178 FREEPTR(line);
179 FREEPTR(realm);
180 return (rval);
181 }
182
183 /*
184 * Encode len bytes starting at clear using base64 encoding into encoded,
185 * which should be at least ((len + 2) * 4 / 3 + 1) in size.
186 */
187 void
188 base64_encode(clear, len, encoded)
189 const char *clear;
190 size_t len;
191 char *encoded;
192 {
193 static const char enc[] =
194 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
195 char *cp;
196 int i;
197
198 cp = encoded;
199 for (i = 0; i < len; i += 3) {
200 *(cp++) = enc[((clear[i + 0] >> 2))];
201 *(cp++) = enc[((clear[i + 0] << 4) & 0x30)
202 | ((clear[i + 1] >> 4) & 0x0f)];
203 *(cp++) = enc[((clear[i + 1] << 2) & 0x3c)
204 | ((clear[i + 2] >> 6) & 0x03)];
205 *(cp++) = enc[((clear[i + 2] ) & 0x3f)];
206 }
207 *cp = '\0';
208 while (i-- > len)
209 *(--cp) = '=';
210 }
211
212 /*
213 * Decode %xx escapes in given string, `in-place'.
214 */
215 static void
216 url_decode(url)
217 char *url;
218 {
219 unsigned char *p, *q;
220
221 if (EMPTYSTRING(url))
222 return;
223 p = q = url;
224
225 #define HEXTOINT(x) (x - (isdigit(x) ? '0' : (islower(x) ? 'a' : 'A') - 10))
226 while (*p) {
227 if (p[0] == '%'
228 && p[1] && isxdigit((unsigned char)p[1])
229 && p[2] && isxdigit((unsigned char)p[2])) {
230 *q++ = HEXTOINT(p[1]) * 16 + HEXTOINT(p[2]);
231 p+=3;
232 } else
233 *q++ = *p++;
234 }
235 *q = '\0';
236 }
237
238
239 /*
240 * Parse URL of form:
241 * <type>://[<user>[:<password>@]]<host>[:<port>]/<url-path>
242 * Returns -1 if a parse error occurred, otherwise 0.
243 * Only permit [<user>[:<password>@]] for ftp:// URLs
244 * It's the caller's responsibility to url_decode() the returned
245 * user, pass and path.
246 * Sets type to url_t, each of the given char ** pointers to a
247 * malloc(3)ed strings of the relevant section, and port to
248 * the number given, or ftpport if ftp://, or httpport if http://.
249 */
250 static int
251 parse_url(url, desc, type, user, pass, host, port, path)
252 const char *url;
253 const char *desc;
254 url_t *type;
255 char **user;
256 char **pass;
257 char **host;
258 in_port_t *port;
259 char **path;
260 {
261 char *cp, *ep, *thost;
262 size_t len;
263
264 if (url == NULL || desc == NULL || type == NULL || user == NULL
265 || pass == NULL || host == NULL || port == NULL || path == NULL)
266 errx(1, "parse_url: invoked with NULL argument!");
267
268 *type = UNKNOWN_URL_T;
269 *user = *pass = *host = *path = NULL;
270 *port = 0;
271
272 if (strncasecmp(url, HTTP_URL, sizeof(HTTP_URL) - 1) == 0) {
273 url += sizeof(HTTP_URL) - 1;
274 *type = HTTP_URL_T;
275 *port = httpport;
276 } else if (strncasecmp(url, FTP_URL, sizeof(FTP_URL) - 1) == 0) {
277 url += sizeof(FTP_URL) - 1;
278 *type = FTP_URL_T;
279 *port = ftpport;
280 } else if (strncasecmp(url, FILE_URL, sizeof(FILE_URL) - 1) == 0) {
281 url += sizeof(FILE_URL) - 1;
282 *type = FILE_URL_T;
283 } else {
284 warnx("Invalid %s `%s'", desc, url);
285 cleanup_parse_url:
286 FREEPTR(*user);
287 FREEPTR(*pass);
288 FREEPTR(*host);
289 FREEPTR(*path);
290 return (-1);
291 }
292
293 if (*url == '\0')
294 return (0);
295
296 /* find [user[:pass]@]host[:port] */
297 ep = strchr(url, '/');
298 if (ep == NULL)
299 thost = xstrdup(url);
300 else {
301 len = ep - url;
302 thost = (char *)xmalloc(len + 1);
303 strncpy(thost, url, len);
304 thost[len] = '\0';
305 ep++; /* skip first / for all URLs */
306 if (*type == FTP_URL_T) /* skip all leading /'s for ftp URLs */
307 while (*ep && *ep == '/')
308 ep++;
309 *path = xstrdup(ep);
310 }
311
312 cp = strchr(thost, '@');
313 /* look for user[:pass]@ in ftp URLs */
314 if (*type == FTP_URL_T && cp != NULL) {
315 anonftp = 0; /* disable anonftp */
316 *user = thost;
317 *cp = '\0';
318 *host = xstrdup(cp + 1);
319 cp = strchr(*user, ':');
320 if (cp != NULL) {
321 *cp = '\0';
322 *pass = xstrdup(cp + 1);
323 }
324 } else
325 *host = thost;
326
327 /* look for [:port] */
328 cp = strrchr(*host, ':');
329 if (cp != NULL) {
330 long nport;
331
332 *cp = '\0';
333 nport = strtol(cp + 1, &ep, 10);
334 if (nport < 1 || nport > MAX_IN_PORT_T || *ep != '\0') {
335 warnx("Invalid port `%s' in %s `%s'", cp, desc, url);
336 goto cleanup_parse_url;
337 }
338 *port = htons((in_port_t)nport);
339 }
340
341 if (debug)
342 fprintf(ttyout,
343 "parse_url: user `%s' pass `%s' host %s:%d path `%s'\n",
344 *user ? *user : "", *pass ? *pass : "", *host ? *host : "",
345 ntohs(*port), *path ? *path : "");
346
347 return (0);
348 }
349
350
351 jmp_buf httpabort;
352
353 /*
354 * Retrieve URL, via a proxy if necessary, using HTTP.
355 * If proxyenv is set, use that for the proxy, otherwise try ftp_proxy or
356 * http_proxy as appropriate.
357 * Supports HTTP redirects.
358 * Returns -1 on failure, 0 on completed xfer, 1 if ftp connection
359 * is still open (e.g, ftp xfer with trailing /)
360 */
361 static int
362 fetch_url(url, outfile, proxyenv, proxyauth, wwwauth)
363 const char *url;
364 const char *outfile;
365 const char *proxyenv;
366 char *proxyauth;
367 char *wwwauth;
368 {
369 struct sockaddr_in sin;
370 struct hostent *hp;
371 volatile sig_t oldintr, oldintp;
372 volatile int s;
373 int ischunked, isproxy, rval, hcode;
374 size_t len;
375 char *cp, *ep, *buf, *savefile;
376 char *auth, *location, *message;
377 char *user, *pass, *host, *path, *decodedpath;
378 off_t hashbytes;
379 int (*closefunc) __P((FILE *));
380 FILE *fin, *fout;
381 time_t mtime;
382 url_t urltype;
383 in_port_t port;
384
385 closefunc = NULL;
386 fin = fout = NULL;
387 s = -1;
388 buf = savefile = NULL;
389 auth = location = message = NULL;
390 ischunked = isproxy = 0;
391 rval = 1;
392 hp = NULL;
393 user = pass = host = path = decodedpath = NULL;
394
395 #ifdef __GNUC__ /* shut up gcc warnings */
396 (void)&closefunc;
397 (void)&fin;
398 (void)&fout;
399 (void)&buf;
400 (void)&savefile;
401 (void)&rval;
402 (void)&isproxy;
403 (void)&hcode;
404 (void)&ischunked;
405 (void)&message;
406 (void)&location;
407 (void)&auth;
408 (void)&decodedpath;
409 #endif
410
411 if (parse_url(url, "URL", &urltype, &user, &pass, &host, &port, &path)
412 == -1)
413 goto cleanup_fetch_url;
414
415 if (urltype == FILE_URL_T && ! EMPTYSTRING(host)
416 && strcasecmp(host, "localhost") != 0) {
417 warnx("No support for non local file URL `%s'", url);
418 goto cleanup_fetch_url;
419 }
420
421 if (EMPTYSTRING(path)) {
422 if (urltype == FTP_URL_T) {
423 rval = fetch_ftp(url, outfile);
424 goto cleanup_fetch_url;
425 }
426 if (urltype != HTTP_URL_T || outfile == NULL) {
427 warnx("Invalid URL (no file after host) `%s'", url);
428 goto cleanup_fetch_url;
429 }
430 }
431
432 decodedpath = xstrdup(path);
433 url_decode(decodedpath);
434
435 if (outfile)
436 savefile = xstrdup(outfile);
437 else {
438 cp = strrchr(decodedpath, '/'); /* find savefile */
439 if (cp != NULL)
440 savefile = xstrdup(cp + 1);
441 else
442 savefile = xstrdup(decodedpath);
443 }
444 if (EMPTYSTRING(savefile)) {
445 if (urltype == FTP_URL_T) {
446 rval = fetch_ftp(url, outfile);
447 goto cleanup_fetch_url;
448 }
449 warnx("Invalid URL (no file after directory) `%s'", url);
450 goto cleanup_fetch_url;
451 }
452
453 filesize = -1;
454 mtime = -1;
455 if (urltype == FILE_URL_T) { /* file:// URLs */
456 struct stat sb;
457
458 direction = "copied";
459 fin = fopen(decodedpath, "r");
460 if (fin == NULL) {
461 warn("Cannot open file `%s'", decodedpath);
462 goto cleanup_fetch_url;
463 }
464 if (fstat(fileno(fin), &sb) == 0) {
465 mtime = sb.st_mtime;
466 filesize = sb.st_size;
467 }
468 if (verbose)
469 fprintf(ttyout, "Copying %s\n", decodedpath);
470 } else { /* ftp:// or http:// URLs */
471 if (proxyenv == NULL) {
472 if (urltype == HTTP_URL_T)
473 proxyenv = httpproxy;
474 else if (urltype == FTP_URL_T)
475 proxyenv = ftpproxy;
476 }
477 direction = "retrieved";
478 if (proxyenv != NULL) { /* use proxy */
479 url_t purltype;
480 char *puser, *ppass, *phost;
481 char *ppath;
482
483 isproxy = 1;
484
485 /* check URL against list of no_proxied sites */
486 if (no_proxy != NULL) {
487 char *np, *np_copy;
488 long np_port;
489 size_t hlen, plen;
490
491 np_copy = xstrdup(no_proxy);
492 hlen = strlen(host);
493 while ((cp = strsep(&np_copy, " ,")) != NULL) {
494 if (*cp == '\0')
495 continue;
496 if ((np = strchr(cp, ':')) != NULL) {
497 *np = '\0';
498 np_port =
499 strtol(np + 1, &ep, 10);
500 if (*ep != '\0')
501 continue;
502 if (port !=
503 htons((in_port_t)np_port))
504 continue;
505 }
506 plen = strlen(cp);
507 if (strncasecmp(host + hlen - plen,
508 cp, plen) == 0) {
509 isproxy = 0;
510 break;
511 }
512 }
513 FREEPTR(np_copy);
514 }
515
516 if (isproxy) {
517 if (parse_url(proxyenv, "proxy URL", &purltype,
518 &puser, &ppass, &phost, &port, &ppath)
519 == -1)
520 goto cleanup_fetch_url;
521
522 if ((purltype != HTTP_URL_T
523 && purltype != FTP_URL_T) ||
524 EMPTYSTRING(phost) ||
525 (! EMPTYSTRING(ppath)
526 && strcmp(ppath, "/") != 0)) {
527 warnx("Malformed proxy URL `%s'",
528 proxyenv);
529 FREEPTR(puser);
530 FREEPTR(ppass);
531 FREEPTR(phost);
532 FREEPTR(ppath);
533 goto cleanup_fetch_url;
534 }
535
536 FREEPTR(user);
537 user = puser;
538 FREEPTR(pass);
539 pass = ppass;
540 FREEPTR(host);
541 host = phost;
542 FREEPTR(path);
543 FREEPTR(ppath);
544 path = xstrdup(url);
545 }
546 } /* proxyenv != NULL */
547
548 memset(&sin, 0, sizeof(sin));
549 sin.sin_family = AF_INET;
550
551 if (isdigit((unsigned char)host[0])) {
552 if (inet_aton(host, &sin.sin_addr) == 0) {
553 warnx("Invalid IP address `%s'", host);
554 goto cleanup_fetch_url;
555 }
556 } else {
557 hp = gethostbyname(host);
558 if (hp == NULL) {
559 warnx("%s: %s", host, hstrerror(h_errno));
560 goto cleanup_fetch_url;
561 }
562 if (hp->h_addrtype != AF_INET) {
563 warnx("`%s': not an Internet address?", host);
564 goto cleanup_fetch_url;
565 }
566 memcpy(&sin.sin_addr, hp->h_addr, hp->h_length);
567 }
568
569 if (port == 0) {
570 warnx("Unknown port for URL `%s'", url);
571 goto cleanup_fetch_url;
572 }
573 sin.sin_port = port;
574
575 s = socket(AF_INET, SOCK_STREAM, 0);
576 if (s == -1) {
577 warn("Can't create socket");
578 goto cleanup_fetch_url;
579 }
580
581 while (xconnect(s, (struct sockaddr *)&sin,
582 sizeof(sin)) == -1) {
583 if (errno == EINTR)
584 continue;
585 if (hp && hp->h_addr_list[1]) {
586 int oerrno = errno;
587 char *ia;
588
589 ia = inet_ntoa(sin.sin_addr);
590 errno = oerrno;
591 warn("Connect to address `%s'", ia);
592 hp->h_addr_list++;
593 memcpy(&sin.sin_addr, hp->h_addr_list[0],
594 (size_t)hp->h_length);
595 if (verbose)
596 fprintf(ttyout, "Trying %s...\n",
597 inet_ntoa(sin.sin_addr));
598 (void)close(s);
599 s = socket(AF_INET, SOCK_STREAM, 0);
600 if (s < 0) {
601 warn("Can't create socket");
602 goto cleanup_fetch_url;
603 }
604 continue;
605 }
606 warn("Can't connect to `%s'", host);
607 goto cleanup_fetch_url;
608 }
609
610 fin = fdopen(s, "r+");
611 /*
612 * Construct and send the request.
613 * Proxy requests don't want leading /.
614 */
615 if (isproxy) {
616 if (verbose)
617 fprintf(ttyout, "Requesting %s\n (via %s)\n",
618 url, proxyenv);
619 fprintf(fin, "GET %s HTTP/1.0\r\n", path);
620 if (flushcache)
621 fprintf(fin, "Pragma: no-cache\r\n");
622 } else {
623 struct utsname unam;
624
625 if (verbose)
626 fprintf(ttyout, "Requesting %s\n", url);
627 fprintf(fin, "GET %s HTTP/1.1\r\n", path);
628 fprintf(fin, "Host: %s:%d\r\n", host, ntohs(port));
629 fprintf(fin, "Accept: */*\r\n");
630 if (uname(&unam) != -1) {
631 fprintf(fin, "User-Agent: %s-%s/ftp\r\n",
632 unam.sysname, unam.release);
633 }
634 fprintf(fin, "Connection: close\r\n");
635 if (flushcache)
636 fprintf(fin, "Cache-Control: no-cache\r\n");
637 }
638 if (wwwauth) {
639 if (verbose)
640 fprintf(ttyout, " (with authorization)\n");
641 fprintf(fin, "Authorization: %s\r\n", wwwauth);
642 }
643 if (proxyauth) {
644 if (verbose)
645 fprintf(ttyout,
646 " (with proxy authorization)\n");
647 fprintf(fin, "Proxy-Authorization: %s\r\n", proxyauth);
648 }
649 fprintf(fin, "\r\n");
650 if (fflush(fin) == EOF) {
651 warn("Writing HTTP request");
652 goto cleanup_fetch_url;
653 }
654
655 /* Read the response */
656 if ((buf = fparseln(fin, &len, NULL, "\0\0\0", 0)) == NULL) {
657 warn("Receiving HTTP reply");
658 goto cleanup_fetch_url;
659 }
660 while (len > 0 && (buf[len-1] == '\r' || buf[len-1] == '\n'))
661 buf[--len] = '\0';
662 if (debug)
663 fprintf(ttyout, "received `%s'\n", buf);
664
665 /* Determine HTTP response code */
666 cp = strchr(buf, ' ');
667 if (cp == NULL)
668 goto improper;
669 else
670 cp++;
671 hcode = strtol(cp, &ep, 10);
672 if (*ep != '\0' && !isspace((unsigned char)*ep))
673 goto improper;
674 message = xstrdup(cp);
675
676 /* Read the rest of the header. */
677 FREEPTR(buf);
678 while (1) {
679 if ((buf = fparseln(fin, &len, NULL, "\0\0\0", 0))
680 == NULL) {
681 warn("Receiving HTTP reply");
682 goto cleanup_fetch_url;
683 }
684 while (len > 0 &&
685 (buf[len-1] == '\r' || buf[len-1] == '\n'))
686 buf[--len] = '\0';
687 if (len == 0)
688 break;
689 if (debug)
690 fprintf(ttyout, "received `%s'\n", buf);
691
692 /* Look for some headers */
693 cp = buf;
694
695 #define CONTENTLEN "Content-Length: "
696 if (strncasecmp(cp, CONTENTLEN,
697 sizeof(CONTENTLEN) - 1) == 0) {
698 cp += sizeof(CONTENTLEN) - 1;
699 filesize = strtol(cp, &ep, 10);
700 if (filesize < 1 || *ep != '\0')
701 goto improper;
702 if (debug)
703 fprintf(ttyout,
704 #ifndef NO_QUAD
705 "parsed length as: %qd\n",
706 (long long)filesize);
707 #else
708 "parsed length as: %ld\n",
709 (long)filesize);
710 #endif
711
712 #define LASTMOD "Last-Modified: "
713 } else if (strncasecmp(cp, LASTMOD,
714 sizeof(LASTMOD) - 1) == 0) {
715 struct tm parsed;
716 char *t;
717
718 cp += sizeof(LASTMOD) - 1;
719 /* RFC 1123 */
720 if ((t = strptime(cp,
721 "%a, %d %b %Y %H:%M:%S GMT",
722 &parsed))
723 /* RFC 850 */
724 || (t = strptime(cp,
725 "%a, %d-%b-%y %H:%M:%S GMT",
726 &parsed))
727 /* asctime */
728 || (t = strptime(cp,
729 "%a, %b %d %H:%M:%S %Y",
730 &parsed))) {
731 parsed.tm_isdst = -1;
732 if (*t == '\0')
733 mtime = mkgmtime(&parsed);
734 if (debug && mtime != -1) {
735 fprintf(ttyout,
736 "parsed date as: %s",
737 ctime(&mtime));
738 }
739 }
740
741 #define LOCATION "Location: "
742 } else if (strncasecmp(cp, LOCATION,
743 sizeof(LOCATION) - 1) == 0) {
744 cp += sizeof(LOCATION) - 1;
745 location = xstrdup(cp);
746 if (debug)
747 fprintf(ttyout,
748 "parsed location as: %s\n", cp);
749
750 #define TRANSENC "Transfer-Encoding: "
751 } else if (strncasecmp(cp, TRANSENC,
752 sizeof(TRANSENC) - 1) == 0) {
753 cp += sizeof(TRANSENC) - 1;
754 if (strcasecmp(cp, "chunked") != 0) {
755 warnx(
756 "Unsupported transfer encoding - `%s'",
757 cp);
758 goto cleanup_fetch_url;
759 }
760 ischunked++;
761 if (debug)
762 fprintf(ttyout,
763 "using chunked encoding\n");
764
765 #define PROXYAUTH "Proxy-Authenticate: "
766 } else if (strncasecmp(cp, PROXYAUTH,
767 sizeof(PROXYAUTH) - 1) == 0) {
768 cp += sizeof(PROXYAUTH) - 1;
769 FREEPTR(auth);
770 auth = xstrdup(cp);
771 if (debug)
772 fprintf(ttyout,
773 "parsed proxy-auth as: %s\n", cp);
774
775 #define WWWAUTH "WWW-Authenticate: "
776 } else if (strncasecmp(cp, WWWAUTH,
777 sizeof(WWWAUTH) - 1) == 0) {
778 cp += sizeof(WWWAUTH) - 1;
779 FREEPTR(auth);
780 auth = xstrdup(cp);
781 if (debug)
782 fprintf(ttyout,
783 "parsed www-auth as: %s\n", cp);
784
785 }
786
787 }
788 FREEPTR(buf);
789 }
790
791 switch (hcode) {
792 case 200:
793 break;
794 case 300:
795 case 301:
796 case 302:
797 case 303:
798 case 305:
799 if (EMPTYSTRING(location)) {
800 warnx("No redirection Location provided by server");
801 goto cleanup_fetch_url;
802 }
803 if (redirect_loop++ > 5) {
804 warnx("Too many redirections requested");
805 goto cleanup_fetch_url;
806 }
807 if (hcode == 305) {
808 if (verbose)
809 fprintf(ttyout, "Redirected via %s\n",
810 location);
811 rval = fetch_url(url, outfile, location, proxyauth,
812 wwwauth);
813 } else {
814 if (verbose)
815 fprintf(ttyout, "Redirected to %s\n", location);
816 rval = go_fetch(location, outfile);
817 }
818 goto cleanup_fetch_url;
819 case 401:
820 case 407:
821 {
822 char **authp;
823
824 fprintf(ttyout, "%s\n", message);
825 if (EMPTYSTRING(auth)) {
826 warnx("No authentication challenge provided by server");
827 goto cleanup_fetch_url;
828 }
829 authp = (hcode == 401) ? &wwwauth : &proxyauth;
830 if (*authp != NULL) {
831 char reply[10];
832
833 fprintf(ttyout, "Authorization failed. Retry (y/n)? ");
834 if (fgets(reply, sizeof(reply), stdin) != NULL &&
835 tolower(reply[0]) != 'y')
836 goto cleanup_fetch_url;
837 }
838 if (auth_url(auth, authp) == 0) {
839 rval = fetch_url(url, outfile, proxyenv, proxyauth,
840 wwwauth);
841 memset(*authp, '\0', strlen(*authp));
842 FREEPTR(*authp);
843 }
844 goto cleanup_fetch_url;
845 }
846 default:
847 warnx("Error retrieving file - `%s'", message);
848 goto cleanup_fetch_url;
849 }
850
851 oldintr = oldintp = NULL;
852
853 /* Open the output file. */
854 if (strcmp(savefile, "-") == 0) {
855 fout = stdout;
856 } else if (*savefile == '|') {
857 oldintp = signal(SIGPIPE, SIG_IGN);
858 fout = popen(savefile + 1, "w");
859 if (fout == NULL) {
860 warn("Can't run `%s'", savefile + 1);
861 goto cleanup_fetch_url;
862 }
863 closefunc = pclose;
864 } else {
865 fout = fopen(savefile, "w");
866 if (fout == NULL) {
867 warn("Can't open `%s'", savefile);
868 goto cleanup_fetch_url;
869 }
870 closefunc = fclose;
871 }
872
873 /* Trap signals */
874 if (setjmp(httpabort)) {
875 if (oldintr)
876 (void)signal(SIGINT, oldintr);
877 if (oldintp)
878 (void)signal(SIGPIPE, oldintp);
879 goto cleanup_fetch_url;
880 }
881 oldintr = signal(SIGINT, aborthttp);
882
883 bytes = 0;
884 hashbytes = mark;
885 progressmeter(-1);
886
887 /* Finally, suck down the file. */
888 buf = xmalloc(BUFSIZ + 1);
889 do {
890 ssize_t chunksize;
891
892 chunksize = 0;
893 /* read chunksize */
894 if (ischunked) {
895 if (fgets(buf, BUFSIZ, fin) == NULL) {
896 warnx("Unexpected EOF reading chunksize");
897 goto cleanup_fetch_url;
898 }
899 chunksize = strtol(buf, &ep, 16);
900 if (strcmp(ep, "\r\n") != 0) {
901 warnx("Unexpected data following chunksize");
902 goto cleanup_fetch_url;
903 }
904 if (debug)
905 fprintf(ttyout, "got chunksize of %qd\n",
906 (long long)chunksize);
907 if (chunksize == 0)
908 break;
909 }
910 while ((len = fread(buf, sizeof(char),
911 ischunked ? MIN(chunksize, BUFSIZ) : BUFSIZ, fin)) > 0) {
912 bytes += len;
913 if (fwrite(buf, sizeof(char), len, fout) != len) {
914 warn("Writing `%s'", savefile);
915 goto cleanup_fetch_url;
916 }
917 if (hash && !progress) {
918 while (bytes >= hashbytes) {
919 (void)putc('#', ttyout);
920 hashbytes += mark;
921 }
922 (void)fflush(ttyout);
923 }
924 if (ischunked)
925 chunksize -= len;
926 }
927 /* read CRLF after chunk*/
928 if (ischunked) {
929 if (fgets(buf, BUFSIZ, fin) == NULL)
930 break;
931 if (strcmp(buf, "\r\n") != 0) {
932 warnx("Unexpected data following chunk");
933 goto cleanup_fetch_url;
934 }
935 }
936 } while (ischunked);
937 if (hash && !progress && bytes > 0) {
938 if (bytes < mark)
939 (void)putc('#', ttyout);
940 (void)putc('\n', ttyout);
941 }
942 if (ferror(fin)) {
943 warn("Reading file");
944 goto cleanup_fetch_url;
945 }
946 progressmeter(1);
947 (void)fflush(fout);
948 (void)signal(SIGINT, oldintr);
949 if (oldintp)
950 (void)signal(SIGPIPE, oldintp);
951 if (closefunc == fclose && mtime != -1) {
952 struct timeval tval[2];
953
954 (void)gettimeofday(&tval[0], NULL);
955 tval[1].tv_sec = mtime;
956 tval[1].tv_usec = 0;
957 (*closefunc)(fout);
958 fout = NULL;
959
960 if (utimes(savefile, tval) == -1) {
961 fprintf(ttyout,
962 "Can't change modification time to %s",
963 asctime(localtime(&mtime)));
964 }
965 }
966 if (bytes > 0)
967 ptransfer(0);
968
969 rval = 0;
970 goto cleanup_fetch_url;
971
972 improper:
973 warnx("Improper response from `%s'", host);
974
975 cleanup_fetch_url:
976 resetsockbufsize();
977 if (fin != NULL)
978 fclose(fin);
979 else if (s != -1)
980 close(s);
981 if (closefunc != NULL && fout != NULL)
982 (*closefunc)(fout);
983 FREEPTR(savefile);
984 FREEPTR(user);
985 FREEPTR(pass);
986 FREEPTR(host);
987 FREEPTR(path);
988 FREEPTR(decodedpath);
989 FREEPTR(buf);
990 FREEPTR(auth);
991 FREEPTR(location);
992 FREEPTR(message);
993 return (rval);
994 }
995
996 /*
997 * Abort a HTTP retrieval
998 */
999 void
1000 aborthttp(notused)
1001 int notused;
1002 {
1003
1004 alarmtimer(0);
1005 fputs("\nHTTP fetch aborted.\n", ttyout);
1006 longjmp(httpabort, 1);
1007 }
1008
1009 /*
1010 * Retrieve ftp URL or classic ftp argument using FTP.
1011 * Returns -1 on failure, 0 on completed xfer, 1 if ftp connection
1012 * is still open (e.g, ftp xfer with trailing /)
1013 */
1014 static int
1015 fetch_ftp(url, outfile)
1016 const char *url;
1017 const char *outfile;
1018 {
1019 char *cp, *xargv[5], rempath[MAXPATHLEN];
1020 char portnum[6]; /* large enough for "65535\0" */
1021 char *host, *path, *dir, *file, *user, *pass;
1022 in_port_t port;
1023 int dirhasglob, filehasglob, oautologin, rval, xargc;
1024
1025 host = path = dir = file = user = pass = NULL;
1026 port = 0;
1027 rval = 1;
1028
1029 if (strncasecmp(url, FTP_URL, sizeof(FTP_URL) - 1) == 0) {
1030 url_t urltype;
1031
1032 if ((parse_url(url, "URL", &urltype, &user, &pass,
1033 &host, &port, &path) == -1) ||
1034 (user != NULL && *user == '\0') ||
1035 (pass != NULL && *pass == '\0') ||
1036 EMPTYSTRING(host)) {
1037 warnx("Invalid URL `%s'", url);
1038 goto cleanup_fetch_ftp;
1039 }
1040 url_decode(user);
1041 url_decode(pass);
1042 url_decode(path);
1043 } else { /* classic style `host:file' */
1044 host = xstrdup(url);
1045 cp = strchr(host, ':');
1046 if (cp != NULL) {
1047 *cp = '\0';
1048 path = xstrdup(cp + 1);
1049 }
1050 }
1051 if (EMPTYSTRING(host))
1052 goto cleanup_fetch_ftp;
1053
1054 /* Extract the file and (if present) directory name. */
1055 dir = path;
1056 if (! EMPTYSTRING(dir)) {
1057 cp = strrchr(dir, '/');
1058 if (cp == dir) {
1059 file = cp + 1;
1060 dir = "/";
1061 } else if (cp != NULL) {
1062 *cp++ = '\0';
1063 file = cp;
1064 } else {
1065 file = dir;
1066 dir = NULL;
1067 }
1068 }
1069 if (debug)
1070 fprintf(ttyout,
1071 "fetch_ftp: user `%s' pass `%s' host %s:%d path `%s' dir `%s' file `%s'\n",
1072 user ? user : "", pass ? pass : "",
1073 host ? host : "", ntohs(port), path ? path : "",
1074 dir ? dir : "", file ? file : "");
1075
1076 dirhasglob = filehasglob = 0;
1077 if (doglob) {
1078 if (! EMPTYSTRING(dir) && strpbrk(dir, "*?[]{}") != NULL)
1079 dirhasglob = 1;
1080 if (! EMPTYSTRING(file) && strpbrk(file, "*?[]{}") != NULL)
1081 filehasglob = 1;
1082 }
1083
1084 /* Set up the connection */
1085 if (connected)
1086 disconnect(0, NULL);
1087 xargv[0] = __progname;
1088 xargv[1] = host;
1089 xargv[2] = NULL;
1090 xargc = 2;
1091 if (port) {
1092 snprintf(portnum, sizeof(portnum), "%d", ntohs(port));
1093 xargv[2] = portnum;
1094 xargv[3] = NULL;
1095 xargc = 3;
1096 }
1097 oautologin = autologin;
1098 if (user != NULL)
1099 autologin = 0;
1100 setpeer(xargc, xargv);
1101 autologin = oautologin;
1102 if ((connected == 0) || ((connected == 1)
1103 && !ftp_login(host, user, pass))) {
1104 warnx("Can't connect or login to host `%s'", host);
1105 goto cleanup_fetch_ftp;
1106 }
1107
1108 /* Always use binary transfers. */
1109 setbinary(0, NULL);
1110
1111 /* Change directories, if necessary. */
1112 if (! EMPTYSTRING(dir) && !dirhasglob) {
1113 xargv[0] = "cd";
1114 xargv[1] = dir;
1115 xargv[2] = NULL;
1116 dirchange = 0;
1117 cd(2, xargv);
1118 if (! dirchange)
1119 goto cleanup_fetch_ftp;
1120 }
1121
1122 if (EMPTYSTRING(file)) {
1123 rval = -1;
1124 goto cleanup_fetch_ftp;
1125 }
1126
1127 if (dirhasglob) {
1128 snprintf(rempath, sizeof(rempath), "%s/%s", dir, file);
1129 file = rempath;
1130 }
1131
1132 /* Fetch the file(s). */
1133 xargc = 2;
1134 xargv[0] = "get";
1135 xargv[1] = file;
1136 xargv[2] = NULL;
1137 if (dirhasglob || filehasglob) {
1138 int ointeractive;
1139
1140 ointeractive = interactive;
1141 interactive = 0;
1142 xargv[0] = "mget";
1143 mget(xargc, xargv);
1144 interactive = ointeractive;
1145 } else {
1146 if (outfile != NULL) {
1147 xargv[2] = (char *)outfile;
1148 xargv[3] = NULL;
1149 xargc++;
1150 }
1151 get(xargc, xargv);
1152 if (outfile != NULL && strcmp(outfile, "-") != 0
1153 && outfile[0] != '|')
1154 outfile = NULL;
1155 }
1156
1157 if ((code / 100) == COMPLETE)
1158 rval = 0;
1159
1160 cleanup_fetch_ftp:
1161 FREEPTR(host);
1162 FREEPTR(path);
1163 FREEPTR(user);
1164 FREEPTR(pass);
1165 return (rval);
1166 }
1167
1168 /*
1169 * Retrieve the given file to outfile.
1170 * Supports arguments of the form:
1171 * "host:path", "ftp://host/path" if $ftpproxy, call fetch_url() else
1172 * call fetch_ftp()
1173 * "http://host/path" call fetch_url() to use HTTP
1174 * "file:///path" call fetch_url() to copy
1175 * "about:..." print a message
1176 *
1177 * Returns 1 on failure, 0 on completed xfer, -1 if ftp connection
1178 * is still open (e.g, ftp xfer with trailing /)
1179 */
1180 static int
1181 go_fetch(url, outfile)
1182 const char *url;
1183 const char *outfile;
1184 {
1185
1186 #ifndef SMALL
1187 /*
1188 * Check for about:*
1189 */
1190 if (strncasecmp(url, ABOUT_URL, sizeof(ABOUT_URL) - 1) == 0) {
1191 url += sizeof(ABOUT_URL) -1;
1192 if (strcasecmp(url, "ftp") == 0) {
1193 fprintf(ttyout, "%s\n%s\n",
1194 "This version of ftp has been enhanced by Luke Mewburn <lukem (at) netbsd.org>.",
1195 "Execute `man ftp' for more details");
1196 } else if (strcasecmp(url, "netbsd") == 0) {
1197 fprintf(ttyout, "%s\n%s\n",
1198 "NetBSD is a freely available and redistributable UNIX-like operating system.",
1199 "For more information, see http://www.netbsd.org/index.html");
1200 } else {
1201 fprintf(ttyout, "`%s' is an interesting topic.\n", url);
1202 }
1203 return (0);
1204 }
1205 #endif /* SMALL */
1206
1207 /*
1208 * Check for file:// and http:// URLs.
1209 */
1210 if (strncasecmp(url, HTTP_URL, sizeof(HTTP_URL) - 1) == 0 ||
1211 strncasecmp(url, FILE_URL, sizeof(FILE_URL) - 1) == 0)
1212 return (fetch_url(url, outfile, NULL, NULL, NULL));
1213
1214 /*
1215 * Try FTP URL-style and host:file arguments next.
1216 * If ftpproxy is set with an FTP URL, use fetch_url()
1217 * Othewise, use fetch_ftp().
1218 */
1219 if (ftpproxy && strncasecmp(url, FTP_URL, sizeof(FTP_URL) - 1) == 0)
1220 return (fetch_url(url, outfile, NULL, NULL, NULL));
1221
1222 return (fetch_ftp(url, outfile));
1223 }
1224
1225 /*
1226 * Retrieve multiple files from the command line,
1227 * calling go_fetch() for each file.
1228 *
1229 * If an ftp path has a trailing "/", the path will be cd-ed into and
1230 * the connection remains open, and the function will return -1
1231 * (to indicate the connection is alive).
1232 * If an error occurs the return value will be the offset+1 in
1233 * argv[] of the file that caused a problem (i.e, argv[x]
1234 * returns x+1)
1235 * Otherwise, 0 is returned if all files retrieved successfully.
1236 */
1237 int
1238 auto_fetch(argc, argv, outfile)
1239 int argc;
1240 char *argv[];
1241 char *outfile;
1242 {
1243 volatile int argpos;
1244 int rval;
1245
1246 argpos = 0;
1247
1248 if (setjmp(toplevel)) {
1249 if (connected)
1250 disconnect(0, NULL);
1251 return (argpos + 1);
1252 }
1253 (void)signal(SIGINT, (sig_t)intr);
1254 (void)signal(SIGPIPE, (sig_t)lostpeer);
1255
1256 /*
1257 * Loop through as long as there's files to fetch.
1258 */
1259 for (rval = 0; (rval == 0) && (argpos < argc); argpos++) {
1260 if (strchr(argv[argpos], ':') == NULL)
1261 break;
1262 redirect_loop = 0;
1263 anonftp = 1; /* Handle "automatic" transfers. */
1264 rval = go_fetch(argv[argpos], outfile);
1265 if (rval > 0)
1266 rval = argpos + 1;
1267 }
1268
1269 if (connected && rval != -1)
1270 disconnect(0, NULL);
1271 return (rval);
1272 }
1273