fetch.c revision 1.57 1 /* $NetBSD: fetch.c,v 1.57 1999/06/24 14:48:35 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 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.57 1999/06/24 14:48:35 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 <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, "got chunksize of %qd\n",
951 (long long)chunksize);
952 if (chunksize == 0)
953 break;
954 }
955 while ((len = fread(buf, sizeof(char),
956 ischunked ? MIN(chunksize, BUFSIZ) : BUFSIZ, fin)) > 0) {
957 bytes += len;
958 if (fwrite(buf, sizeof(char), len, fout) != len) {
959 warn("Writing `%s'", savefile);
960 goto cleanup_fetch_url;
961 }
962 if (hash && !progress) {
963 while (bytes >= hashbytes) {
964 (void)putc('#', ttyout);
965 hashbytes += mark;
966 }
967 (void)fflush(ttyout);
968 }
969 if (ischunked)
970 chunksize -= len;
971 }
972 /* read CRLF after chunk*/
973 if (ischunked) {
974 if (fgets(buf, BUFSIZ, fin) == NULL)
975 break;
976 if (strcmp(buf, "\r\n") != 0) {
977 warnx("Unexpected data following chunk");
978 goto cleanup_fetch_url;
979 }
980 }
981 } while (ischunked);
982 if (hash && !progress && bytes > 0) {
983 if (bytes < mark)
984 (void)putc('#', ttyout);
985 (void)putc('\n', ttyout);
986 }
987 if (ferror(fin)) {
988 warn("Reading file");
989 goto cleanup_fetch_url;
990 }
991 progressmeter(1);
992 (void)fflush(fout);
993 (void)signal(SIGINT, oldintr);
994 if (oldintp)
995 (void)signal(SIGPIPE, oldintp);
996 if (closefunc == fclose && mtime != -1) {
997 struct timeval tval[2];
998
999 (void)gettimeofday(&tval[0], NULL);
1000 tval[1].tv_sec = mtime;
1001 tval[1].tv_usec = 0;
1002 (*closefunc)(fout);
1003 fout = NULL;
1004
1005 if (utimes(savefile, tval) == -1) {
1006 fprintf(ttyout,
1007 "Can't change modification time to %s",
1008 asctime(localtime(&mtime)));
1009 }
1010 }
1011 if (bytes > 0)
1012 ptransfer(0);
1013
1014 rval = 0;
1015 goto cleanup_fetch_url;
1016
1017 improper:
1018 warnx("Improper response from `%s'", host);
1019
1020 cleanup_fetch_url:
1021 resetsockbufsize();
1022 if (fin != NULL)
1023 fclose(fin);
1024 else if (s != -1)
1025 close(s);
1026 if (closefunc != NULL && fout != NULL)
1027 (*closefunc)(fout);
1028 FREEPTR(savefile);
1029 FREEPTR(user);
1030 FREEPTR(pass);
1031 FREEPTR(host);
1032 FREEPTR(path);
1033 FREEPTR(decodedpath);
1034 FREEPTR(puser);
1035 FREEPTR(ppass);
1036 FREEPTR(buf);
1037 FREEPTR(auth);
1038 FREEPTR(location);
1039 FREEPTR(message);
1040 return (rval);
1041 }
1042
1043 /*
1044 * Abort a HTTP retrieval
1045 */
1046 void
1047 aborthttp(notused)
1048 int notused;
1049 {
1050
1051 alarmtimer(0);
1052 fputs("\nHTTP fetch aborted.\n", ttyout);
1053 longjmp(httpabort, 1);
1054 }
1055
1056 /*
1057 * Retrieve ftp URL or classic ftp argument using FTP.
1058 * Returns -1 on failure, 0 on completed xfer, 1 if ftp connection
1059 * is still open (e.g, ftp xfer with trailing /)
1060 */
1061 static int
1062 fetch_ftp(url)
1063 const char *url;
1064 {
1065 char *cp, *xargv[5], rempath[MAXPATHLEN];
1066 char portnum[6]; /* large enough for "65535\0" */
1067 char *host, *path, *dir, *file, *user, *pass;
1068 in_port_t port;
1069 int dirhasglob, filehasglob, oautologin, rval, type, xargc;
1070 url_t urltype;
1071
1072 host = path = dir = file = user = pass = NULL;
1073 port = 0;
1074 rval = 1;
1075 type = TYPE_I;
1076
1077 if (strncasecmp(url, FTP_URL, sizeof(FTP_URL) - 1) == 0) {
1078 if ((parse_url(url, "URL", &urltype, &user, &pass,
1079 &host, &port, &path) == -1) ||
1080 (user != NULL && *user == '\0') ||
1081 (pass != NULL && *pass == '\0') ||
1082 EMPTYSTRING(host)) {
1083 warnx("Invalid URL `%s'", url);
1084 goto cleanup_fetch_ftp;
1085 }
1086 url_decode(user);
1087 url_decode(pass);
1088 /*
1089 * Note: Don't url_decode(path) here. We need to keep the
1090 * distinction between "/" and "%2F" until later.
1091 */
1092
1093 /* check for trailing ';type=[aid]' */
1094 cp = strrchr(path, ';');
1095 if (cp != NULL) {
1096 if (strcasecmp(cp, ";type=a") == 0)
1097 type = TYPE_A;
1098 else if (strcasecmp(cp, ";type=i") == 0)
1099 type = TYPE_I;
1100 else if (strcasecmp(cp, ";type=d") == 0) {
1101 warnx(
1102 "Directory listing via a URL is not supported");
1103 goto cleanup_fetch_ftp;
1104 } else {
1105 warnx("Invalid suffix `%s' in URL `%s'", cp,
1106 url);
1107 goto cleanup_fetch_ftp;
1108 }
1109 *cp = 0;
1110 }
1111 } else { /* classic style `host:file' */
1112 urltype = CLASSIC_URL_T;
1113 host = xstrdup(url);
1114 cp = strchr(host, ':');
1115 if (cp != NULL) {
1116 *cp = '\0';
1117 path = xstrdup(cp + 1);
1118 }
1119 }
1120 if (EMPTYSTRING(host))
1121 goto cleanup_fetch_ftp;
1122
1123 /* Extract the file and (if present) directory name. */
1124 dir = path;
1125 if (! EMPTYSTRING(dir)) {
1126 /*
1127 * If we are dealing with classic `host:path' syntax,
1128 * then a path of the form `/file' (resulting from
1129 * input of the form `host:/file') means that we should
1130 * do "CWD /" before retreiving the file. So we set
1131 * dir="/" and file="file".
1132 *
1133 * But if we are dealing with URLs like
1134 * `ftp://host/path' then a path of the form `/file'
1135 * (resulting from a URL of the form `ftp://host//file')
1136 * means that we should do `CWD ' (with an empty
1137 * argument) before retrieving the file. So we set
1138 * dir="" and file="file".
1139 *
1140 * If the path does not contain / at all, we set
1141 * dir=NULL. (We get a path without any slashes if
1142 * we are dealing with classic `host:file' or URL
1143 * `ftp://host/file'.)
1144 *
1145 * In all other cases, we set dir to a string that does
1146 * not include the final '/' that separates the dir part
1147 * from the file part of the path. (This will be the
1148 * empty string if and only if we are dealing with a
1149 * path of the form `/file' resulting from an URL of the
1150 * form `ftp://host//file'.)
1151 */
1152 cp = strrchr(dir, '/');
1153 if (cp == dir && urltype == CLASSIC_URL_T) {
1154 file = cp + 1;
1155 dir = "/";
1156 } else if (cp != NULL) {
1157 *cp++ = '\0';
1158 file = cp;
1159 } else {
1160 file = dir;
1161 dir = NULL;
1162 }
1163 }
1164 if (urltype == FTP_URL_T && file != NULL) {
1165 url_decode(file);
1166 /* but still don't url_decode(dir) */
1167 }
1168 if (debug)
1169 fprintf(ttyout,
1170 "fetch_ftp: user `%s' pass `%s' host %s:%d path `%s' dir `%s' file `%s'\n",
1171 user ? user : "<null>", pass ? pass : "<null>",
1172 host ? host : "<null>", ntohs(port), path ? path : "<null>",
1173 dir ? dir : "<null>", file ? file : "<null>");
1174
1175 dirhasglob = filehasglob = 0;
1176 if (doglob && urltype == CLASSIC_URL_T) {
1177 if (! EMPTYSTRING(dir) && strpbrk(dir, "*?[]{}") != NULL)
1178 dirhasglob = 1;
1179 if (! EMPTYSTRING(file) && strpbrk(file, "*?[]{}") != NULL)
1180 filehasglob = 1;
1181 }
1182
1183 /* Set up the connection */
1184 if (connected)
1185 disconnect(0, NULL);
1186 xargv[0] = __progname;
1187 xargv[1] = host;
1188 xargv[2] = NULL;
1189 xargc = 2;
1190 if (port) {
1191 snprintf(portnum, sizeof(portnum), "%d", ntohs(port));
1192 xargv[2] = portnum;
1193 xargv[3] = NULL;
1194 xargc = 3;
1195 }
1196 oautologin = autologin;
1197 if (user != NULL)
1198 autologin = 0;
1199 setpeer(xargc, xargv);
1200 autologin = oautologin;
1201 if ((connected == 0) || ((connected == 1)
1202 && !ftp_login(host, user, pass))) {
1203 warnx("Can't connect or login to host `%s'", host);
1204 goto cleanup_fetch_ftp;
1205 }
1206
1207 switch (type) {
1208 case TYPE_A:
1209 setascii(0, NULL);
1210 break;
1211 case TYPE_I:
1212 setbinary(0, NULL);
1213 break;
1214 default:
1215 errx(1, "fetch_ftp: unknown transfer type %d\n", type);
1216 }
1217
1218 /*
1219 * Change directories, if necessary.
1220 *
1221 * Note: don't use EMPTYSTRING(dir) below, because
1222 * dir="" means something different from dir=NULL.
1223 */
1224 if (dir != NULL && !dirhasglob) {
1225 char *nextpart;
1226
1227 /*
1228 * If we are dealing with a classic `host:path' (urltype
1229 * is CLASSIC_URL_T) then we have a raw directory
1230 * name (not encoded in any way) and we can change
1231 * directories in one step.
1232 *
1233 * If we are dealing with an `ftp://host/path' URL
1234 * (urltype is FTP_URL_T), then RFC 1738 says we need to
1235 * send a separate CWD command for each unescaped "/"
1236 * in the path, and we have to interpret %hex escaping
1237 * *after* we find the slashes. It's possible to get
1238 * empty components here, (from multiple adjacent
1239 * slashes in the path) and RFC 1738 says that we should
1240 * still do `CWD ' (with a null argument) in such cases.
1241 *
1242 * XXX: many ftp servers don't support `CWD ', so we
1243 * just skip the empty components.
1244 *
1245 * Examples:
1246 *
1247 * host:file dir=NULL, urltype=CLASSIC_URL_T
1248 * "RETR file"
1249 * ftp://host/file dir=NULL, urltype=FTP_URL_T
1250 * "RETR file"
1251 * ftp://host//file dir="", urltype=FTP_URL_T
1252 * (no-op), "RETR file"
1253 * host:/file dir="/", urltype=CLASSIC_URL_T
1254 * "CWD /", "RETR file"
1255 * ftp://host///file dir="/", urltype=FTP_URL_T
1256 * (no-op), (no-op), "RETR file"
1257 * ftp://host/%2F/file dir="%2F", urltype=FTP_URL_T
1258 * "CWD /", "RETR file"
1259 * ftp://host/foo/file dir="foo", urltype=FTP_URL_T
1260 * "CWD foo", "RETR file"
1261 * ftp://host/foo/bar/file dir="foo/bar"
1262 * "CWD foo", "CWD bar", "RETR file"
1263 * ftp://host//foo/bar/file dir="/foo/bar"
1264 * (no-op), "CWD foo", "CWD bar", "RETR file"
1265 * ftp://host/foo//bar/file dir="foo//bar"
1266 * "CWD foo", (no-op), "CWD bar", "RETR file"
1267 * ftp://host/%2F/foo/bar/file dir="%2F/foo/bar"
1268 * "CWD /", "CWD foo", "CWD bar", "RETR file"
1269 * ftp://host/%2Ffoo/bar/file dir="%2Ffoo/bar"
1270 * "CWD /foo", "CWD bar", "RETR file"
1271 * ftp://host/%2Ffoo%2Fbar/file dir="%2Ffoo%2Fbar"
1272 * "CWD /foo/bar", "RETR file"
1273 * ftp://host/%2Ffoo%2Fbar%2Ffile dir=NULL
1274 * "RETR /foo/bar/file"
1275 *
1276 * Note that we don't need `dir' after this point.
1277 */
1278 do {
1279 if (urltype == FTP_URL_T) {
1280 nextpart = strchr(dir, '/');
1281 if (nextpart) {
1282 *nextpart = '\0';
1283 nextpart++;
1284 }
1285 url_decode(dir);
1286 } else
1287 nextpart = NULL;
1288 if (debug)
1289 fprintf(ttyout, "dir `%s', nextpart `%s'\n",
1290 dir ? dir : "<null>",
1291 nextpart ? nextpart : "<null>");
1292 if (*dir != '\0') {
1293 xargv[0] = "cd";
1294 xargv[1] = dir;
1295 xargv[2] = NULL;
1296 dirchange = 0;
1297 cd(2, xargv);
1298 if (! dirchange)
1299 goto cleanup_fetch_ftp;
1300 }
1301 dir = nextpart;
1302 } while (dir != NULL);
1303 }
1304
1305 if (EMPTYSTRING(file)) {
1306 rval = -1;
1307 goto cleanup_fetch_ftp;
1308 }
1309
1310 if (dirhasglob) {
1311 snprintf(rempath, sizeof(rempath), "%s/%s", dir, file);
1312 file = rempath;
1313 }
1314
1315 /* Fetch the file(s). */
1316 xargc = 2;
1317 xargv[0] = "get";
1318 xargv[1] = file;
1319 xargv[2] = NULL;
1320 if (dirhasglob || filehasglob) {
1321 int ointeractive;
1322
1323 ointeractive = interactive;
1324 interactive = 0;
1325 xargv[0] = "mget";
1326 mget(xargc, xargv);
1327 interactive = ointeractive;
1328 } else {
1329 if (outfile == NULL) {
1330 cp = strrchr(file, '/'); /* find savefile */
1331 if (cp != NULL)
1332 outfile = cp + 1;
1333 else
1334 outfile = file;
1335 }
1336 xargv[2] = (char *)outfile;
1337 xargv[3] = NULL;
1338 xargc++;
1339 if (restartautofetch)
1340 reget(xargc, xargv);
1341 else
1342 get(xargc, xargv);
1343 }
1344
1345 if ((code / 100) == COMPLETE)
1346 rval = 0;
1347
1348 cleanup_fetch_ftp:
1349 FREEPTR(host);
1350 FREEPTR(path);
1351 FREEPTR(user);
1352 FREEPTR(pass);
1353 return (rval);
1354 }
1355
1356 /*
1357 * Retrieve the given file to outfile.
1358 * Supports arguments of the form:
1359 * "host:path", "ftp://host/path" if $ftpproxy, call fetch_url() else
1360 * call fetch_ftp()
1361 * "http://host/path" call fetch_url() to use HTTP
1362 * "file:///path" call fetch_url() to copy
1363 * "about:..." print a message
1364 *
1365 * Returns 1 on failure, 0 on completed xfer, -1 if ftp connection
1366 * is still open (e.g, ftp xfer with trailing /)
1367 */
1368 static int
1369 go_fetch(url)
1370 const char *url;
1371 {
1372
1373 #ifndef NO_ABOUT
1374 /*
1375 * Check for about:*
1376 */
1377 if (strncasecmp(url, ABOUT_URL, sizeof(ABOUT_URL) - 1) == 0) {
1378 url += sizeof(ABOUT_URL) -1;
1379 if (strcasecmp(url, "ftp") == 0) {
1380 fprintf(ttyout, "%s\n%s\n",
1381 "This version of ftp has been enhanced by Luke Mewburn <lukem (at) netbsd.org>.",
1382 "Execute `man ftp' for more details");
1383 } else if (strcasecmp(url, "netbsd") == 0) {
1384 fprintf(ttyout, "%s\n%s\n",
1385 "NetBSD is a freely available and redistributable UNIX-like operating system.",
1386 "For more information, see http://www.netbsd.org/index.html");
1387 } else {
1388 fprintf(ttyout, "`%s' is an interesting topic.\n", url);
1389 }
1390 return (0);
1391 }
1392 #endif /* NO_ABOUT */
1393
1394 /*
1395 * Check for file:// and http:// URLs.
1396 */
1397 if (strncasecmp(url, HTTP_URL, sizeof(HTTP_URL) - 1) == 0 ||
1398 strncasecmp(url, FILE_URL, sizeof(FILE_URL) - 1) == 0)
1399 return (fetch_url(url, NULL, NULL, NULL));
1400
1401 /*
1402 * Try FTP URL-style and host:file arguments next.
1403 * If ftpproxy is set with an FTP URL, use fetch_url()
1404 * Othewise, use fetch_ftp().
1405 */
1406 if (ftpproxy && strncasecmp(url, FTP_URL, sizeof(FTP_URL) - 1) == 0)
1407 return (fetch_url(url, NULL, NULL, NULL));
1408
1409 return (fetch_ftp(url));
1410 }
1411
1412 /*
1413 * Retrieve multiple files from the command line,
1414 * calling go_fetch() for each file.
1415 *
1416 * If an ftp path has a trailing "/", the path will be cd-ed into and
1417 * the connection remains open, and the function will return -1
1418 * (to indicate the connection is alive).
1419 * If an error occurs the return value will be the offset+1 in
1420 * argv[] of the file that caused a problem (i.e, argv[x]
1421 * returns x+1)
1422 * Otherwise, 0 is returned if all files retrieved successfully.
1423 */
1424 int
1425 auto_fetch(argc, argv)
1426 int argc;
1427 char *argv[];
1428 {
1429 volatile int argpos;
1430 int rval;
1431
1432 argpos = 0;
1433
1434 if (setjmp(toplevel)) {
1435 if (connected)
1436 disconnect(0, NULL);
1437 return (argpos + 1);
1438 }
1439 (void)signal(SIGINT, (sig_t)intr);
1440 (void)signal(SIGPIPE, (sig_t)lostpeer);
1441
1442 /*
1443 * Loop through as long as there's files to fetch.
1444 */
1445 for (rval = 0; (rval == 0) && (argpos < argc); argpos++) {
1446 if (strchr(argv[argpos], ':') == NULL)
1447 break;
1448 redirect_loop = 0;
1449 anonftp = 1; /* Handle "automatic" transfers. */
1450 rval = go_fetch(argv[argpos]);
1451 if (outfile != NULL && strcmp(outfile, "-") != 0
1452 && outfile[0] != '|')
1453 outfile = NULL;
1454 if (rval > 0)
1455 rval = argpos + 1;
1456 }
1457
1458 if (connected && rval != -1)
1459 disconnect(0, NULL);
1460 return (rval);
1461 }
1462