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