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