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