1 /* $NetBSD: fetch.c,v 1.158 2005/05/14 15:26:43 lukem Exp $ */
4 * Copyright (c) 1997-2004 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * This code is derived from software contributed to The NetBSD Foundation
11 * by Scott Aaron Bamford.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
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.
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.
42 #include <sys/cdefs.h>
45 * FTP User Program -- Command line file retrieval
48 #include <sys/types.h>
49 #include <sys/param.h>
50 #include <sys/socket.h>
54 #include <netinet/in.h>
57 #include <arpa/inet.h>
84 static int auth_url(const char *, char **, const char *, const char *);
85 static void base64_encode(const unsigned char *, size_t, unsigned char *);
87 static int go_fetch(const char *);
88 static int fetch_ftp(const char *);
89 static int fetch_url(const char *, const char *, char *, char *);
90 static const char *match_token(const char **, const char *);
91 static int parse_url(const char *, const char *, url_t
*, char **,
92 char **, char **, char **, in_port_t
*, char **);
93 static void url_decode(char *);
95 static int redirect_loop
;
98 #define STRNEQUAL(a,b) (strncasecmp((a), (b), sizeof((b))-1) == 0)
99 #define ISLWS(x) ((x)=='\r' || (x)=='\n' || (x)==' ' || (x)=='\t')
100 #define SKIPLWS(x) do { while (ISLWS((*x))) x++; } while (0)
103 #define ABOUT_URL "about:" /* propaganda */
104 #define FILE_URL "file://" /* file URL prefix */
105 #define FTP_URL "ftp://" /* ftp URL prefix */
106 #define HTTP_URL "http://" /* http URL prefix */
110 * Determine if token is the next word in buf (case insensitive).
111 * If so, advance buf past the token and any trailing LWS, and
112 * return a pointer to the token (in buf). Otherwise, return NULL.
113 * token may be preceeded by LWS.
114 * token must be followed by LWS or NUL. (I.e, don't partial match).
117 match_token(const char **buf
, const char *token
)
119 const char *p
, *orig
;
122 tlen
= strlen(token
);
126 if (strncasecmp(p
, token
, tlen
) != 0)
129 if (*p
!= '\0' && !ISLWS(*p
))
139 * Generate authorization response based on given authentication challenge.
140 * Returns -1 if an error occurred, otherwise 0.
141 * Sets response to a malloc(3)ed string; caller should free.
144 auth_url(const char *challenge
, char **response
, const char *guser
,
147 const char *cp
, *scheme
;
148 char *ep
, *clear
, *realm
;
149 char user
[BUFSIZ
], *pass
;
151 size_t len
, clen
, rlen
;
154 clear
= realm
= NULL
;
157 scheme
= "Basic"; /* only support Basic authentication */
160 fprintf(ttyout
, "auth_url: challenge `%s'\n", challenge
);
162 if (! match_token(&cp
, scheme
)) {
163 warnx("Unsupported authentication challenge - `%s'",
165 goto cleanup_auth_url
;
168 #define REALM "realm=\""
169 if (STRNEQUAL(cp
, REALM
))
170 cp
+= sizeof(REALM
) - 1;
172 warnx("Unsupported authentication challenge - `%s'",
174 goto cleanup_auth_url
;
176 /* XXX: need to improve quoted-string parsing to support \ quoting, etc. */
177 if ((ep
= strchr(cp
, '\"')) != NULL
) {
178 size_t len
= ep
- cp
;
180 realm
= (char *)xmalloc(len
+ 1);
181 (void)strlcpy(realm
, cp
, len
+ 1);
183 warnx("Unsupported authentication challenge - `%s'",
185 goto cleanup_auth_url
;
188 fprintf(ttyout
, "Username for `%s': ", realm
);
190 (void)strlcpy(user
, guser
, sizeof(user
));
191 fprintf(ttyout
, "%s\n", user
);
193 (void)fflush(ttyout
);
194 if (fgets(user
, sizeof(user
) - 1, stdin
) == NULL
) {
196 goto cleanup_auth_url
;
198 user
[strlen(user
) - 1] = '\0';
201 pass
= (char *)gpass
;
203 pass
= getpass("Password: ");
205 clen
= strlen(user
) + strlen(pass
) + 2; /* user + ":" + pass + "\0" */
206 clear
= (char *)xmalloc(clen
);
207 (void)strlcpy(clear
, user
, clen
);
208 (void)strlcat(clear
, ":", clen
);
209 (void)strlcat(clear
, pass
, clen
);
211 memset(pass
, 0, strlen(pass
));
213 /* scheme + " " + enc + "\0" */
214 rlen
= strlen(scheme
) + 1 + (clen
+ 2) * 4 / 3 + 1;
215 *response
= (char *)xmalloc(rlen
);
216 (void)strlcpy(*response
, scheme
, rlen
);
217 len
= strlcat(*response
, " ", rlen
);
218 /* use `clen - 1' to not encode the trailing NUL */
219 base64_encode((unsigned char *)clear
, clen
- 1,
220 (unsigned char *)*response
+ len
);
221 memset(clear
, 0, clen
);
231 * Encode len bytes starting at clear using base64 encoding into encoded,
232 * which should be at least ((len + 2) * 4 / 3 + 1) in size.
235 base64_encode(const unsigned char *clear
, size_t len
, unsigned char *encoded
)
237 static const unsigned char enc
[] =
238 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
243 for (i
= 0; i
< len
; i
+= 3) {
244 *(cp
++) = enc
[((clear
[i
+ 0] >> 2))];
245 *(cp
++) = enc
[((clear
[i
+ 0] << 4) & 0x30)
246 | ((clear
[i
+ 1] >> 4) & 0x0f)];
247 *(cp
++) = enc
[((clear
[i
+ 1] << 2) & 0x3c)
248 | ((clear
[i
+ 2] >> 6) & 0x03)];
249 *(cp
++) = enc
[((clear
[i
+ 2] ) & 0x3f)];
258 * Decode %xx escapes in given string, `in-place'.
261 url_decode(char *url
)
263 unsigned char *p
, *q
;
265 if (EMPTYSTRING(url
))
267 p
= q
= (unsigned char *)url
;
269 #define HEXTOINT(x) (x - (isdigit(x) ? '0' : (islower(x) ? 'a' : 'A') - 10))
272 && p
[1] && isxdigit((unsigned char)p
[1])
273 && p
[2] && isxdigit((unsigned char)p
[2])) {
274 *q
++ = HEXTOINT(p
[1]) * 16 + HEXTOINT(p
[2]);
285 * <type>://[<user>[:<password>]@]<host>[:<port>][/<path>]
286 * Returns -1 if a parse error occurred, otherwise 0.
287 * It's the caller's responsibility to url_decode() the returned
288 * user, pass and path.
290 * Sets type to url_t, each of the given char ** pointers to a
291 * malloc(3)ed strings of the relevant section, and port to
292 * the number given, or ftpport if ftp://, or httpport if http://.
294 * If <host> is surrounded by `[' and ']', it's parsed as an
295 * IPv6 address (as per RFC 2732).
297 * XXX: this is not totally RFC 1738 compliant; <path> will have the
298 * leading `/' unless it's an ftp:// URL, as this makes things easier
299 * for file:// and http:// URLs. ftp:// URLs have the `/' between the
300 * host and the URL-path removed, but any additional leading slashes
301 * in the URL-path are retained (because they imply that we should
302 * later do "CWD" with a null argument).
305 * input URL output path
306 * --------- -----------
308 * "http://host/" NULL
309 * "file://host/dir/file" "dir/file"
311 * "ftp://host//" NULL
312 * "ftp://host//dir/file" "/dir/file"
315 parse_url(const char *url
, const char *desc
, url_t
*type
,
316 char **user
, char **pass
, char **host
, char **port
,
317 in_port_t
*portnum
, char **path
)
320 char *cp
, *ep
, *thost
, *tport
;
323 if (url
== NULL
|| desc
== NULL
|| type
== NULL
|| user
== NULL
324 || pass
== NULL
|| host
== NULL
|| port
== NULL
|| portnum
== NULL
326 errx(1, "parse_url: invoked with NULL argument!");
329 *type
= UNKNOWN_URL_T
;
330 *user
= *pass
= *host
= *port
= *path
= NULL
;
334 if (STRNEQUAL(url
, HTTP_URL
)) {
335 url
+= sizeof(HTTP_URL
) - 1;
337 *portnum
= HTTP_PORT
;
339 } else if (STRNEQUAL(url
, FTP_URL
)) {
340 url
+= sizeof(FTP_URL
) - 1;
344 } else if (STRNEQUAL(url
, FILE_URL
)) {
345 url
+= sizeof(FILE_URL
) - 1;
348 warnx("Invalid %s `%s'", desc
, url
);
361 /* find [user[:pass]@]host[:port] */
362 ep
= strchr(url
, '/');
364 thost
= xstrdup(url
);
367 thost
= (char *)xmalloc(len
+ 1);
368 (void)strlcpy(thost
, url
, len
+ 1);
369 if (*type
== FTP_URL_T
) /* skip first / for ftp URLs */
374 cp
= strchr(thost
, '@'); /* look for user[:pass]@ in URLs */
376 if (*type
== FTP_URL_T
)
377 anonftp
= 0; /* disable anonftp */
380 thost
= xstrdup(cp
+ 1);
381 cp
= strchr(*user
, ':');
384 *pass
= xstrdup(cp
+ 1);
393 * Check if thost is an encoded IPv6 address, as per
395 * `[' ipv6-address ']'
399 if ((ep
= strchr(cp
, ']')) == NULL
||
400 (ep
[1] != '\0' && ep
[1] != ':')) {
401 warnx("Invalid address `%s' in %s `%s'",
402 thost
, desc
, origurl
);
403 goto cleanup_parse_url
;
405 len
= ep
- cp
; /* change `[xyz]' -> `xyz' */
406 memmove(thost
, thost
+ 1, len
);
408 if (! isipv6addr(thost
)) {
409 warnx("Invalid IPv6 address `%s' in %s `%s'",
410 thost
, desc
, origurl
);
411 goto cleanup_parse_url
;
420 if ((cp
= strchr(thost
, ':')) != NULL
)
424 /* look for [:port] */
428 nport
= parseport(cp
, -1);
430 warnx("Unknown port `%s' in %s `%s'",
432 goto cleanup_parse_url
;
439 *port
= xstrdup(tport
);
441 *path
= xstrdup("/");
445 "parse_url: user `%s' pass `%s' host %s port %s(%d) "
447 *user
? *user
: "<null>", *pass
? *pass
: "<null>",
448 *host
? *host
: "<null>", *port
? *port
: "<null>",
449 *portnum
? *portnum
: -1, *path
? *path
: "<null>");
454 sigjmp_buf httpabort
;
457 * Retrieve URL, via a proxy if necessary, using HTTP.
458 * If proxyenv is set, use that for the proxy, otherwise try ftp_proxy or
459 * http_proxy as appropriate.
460 * Supports HTTP redirects.
461 * Returns 1 on failure, 0 on completed xfer, -1 if ftp connection
462 * is still open (e.g, ftp xfer with trailing /)
465 fetch_url(const char *url
, const char *proxyenv
, char *proxyauth
, char *wwwauth
)
467 struct addrinfo hints
, *res
, *res0
= NULL
;
469 char hbuf
[NI_MAXHOST
];
470 volatile sigfunc oldintr
, oldintp
;
473 int ischunked
, isproxy
, rval
, hcode
;
475 static size_t bufsize
;
476 static char *xferbuf
;
477 const char *cp
, *token
;
478 char *ep
, *buf
, *savefile
;
479 char *auth
, *location
, *message
;
480 char *user
, *pass
, *host
, *port
, *path
, *decodedpath
;
481 char *puser
, *ppass
, *useragent
;
482 off_t hashbytes
, rangestart
, rangeend
, entitylen
;
483 int (*closefunc
)(FILE *);
489 oldintr
= oldintp
= NULL
;
493 buf
= savefile
= NULL
;
494 auth
= location
= message
= NULL
;
495 ischunked
= isproxy
= hcode
= 0;
497 user
= pass
= host
= path
= decodedpath
= puser
= ppass
= NULL
;
499 #ifdef __GNUC__ /* shut up gcc warnings */
515 if (parse_url(url
, "URL", &urltype
, &user
, &pass
, &host
, &port
,
516 &portnum
, &path
) == -1)
517 goto cleanup_fetch_url
;
519 if (urltype
== FILE_URL_T
&& ! EMPTYSTRING(host
)
520 && strcasecmp(host
, "localhost") != 0) {
521 warnx("No support for non local file URL `%s'", url
);
522 goto cleanup_fetch_url
;
525 if (EMPTYSTRING(path
)) {
526 if (urltype
== FTP_URL_T
) {
527 rval
= fetch_ftp(url
);
528 goto cleanup_fetch_url
;
530 if (urltype
!= HTTP_URL_T
|| outfile
== NULL
) {
531 warnx("Invalid URL (no file after host) `%s'", url
);
532 goto cleanup_fetch_url
;
536 decodedpath
= xstrdup(path
);
537 url_decode(decodedpath
);
540 savefile
= xstrdup(outfile
);
542 cp
= strrchr(decodedpath
, '/'); /* find savefile */
544 savefile
= xstrdup(cp
+ 1);
546 savefile
= xstrdup(decodedpath
);
548 if (EMPTYSTRING(savefile
)) {
549 if (urltype
== FTP_URL_T
) {
550 rval
= fetch_ftp(url
);
551 goto cleanup_fetch_url
;
553 warnx("no file after directory (you must specify an "
554 "output file) `%s'", url
);
555 goto cleanup_fetch_url
;
558 fprintf(ttyout
, "savefile `%s'\n", savefile
);
563 rangestart
= rangeend
= entitylen
= -1;
565 if (restartautofetch
) {
566 if (strcmp(savefile
, "-") != 0 && *savefile
!= '|' &&
567 stat(savefile
, &sb
) == 0)
568 restart_point
= sb
.st_size
;
570 if (urltype
== FILE_URL_T
) { /* file:// URLs */
571 direction
= "copied";
572 fin
= fopen(decodedpath
, "r");
574 warn("Cannot open file `%s'", decodedpath
);
575 goto cleanup_fetch_url
;
577 if (fstat(fileno(fin
), &sb
) == 0) {
579 filesize
= sb
.st_size
;
582 if (lseek(fileno(fin
), restart_point
, SEEK_SET
) < 0) {
583 warn("Can't lseek to restart `%s'",
585 goto cleanup_fetch_url
;
589 fprintf(ttyout
, "Copying %s", decodedpath
);
591 fprintf(ttyout
, " (restarting at " LLF
")",
595 } else { /* ftp:// or http:// URLs */
599 if (proxyenv
== NULL
) {
600 if (urltype
== HTTP_URL_T
)
601 proxyenv
= getoptionvalue("http_proxy");
602 else if (urltype
== FTP_URL_T
)
603 proxyenv
= getoptionvalue("ftp_proxy");
605 direction
= "retrieved";
606 if (! EMPTYSTRING(proxyenv
)) { /* use proxy */
609 char *pport
, *no_proxy
;
613 /* check URL against list of no_proxied sites */
614 no_proxy
= getoptionvalue("no_proxy");
615 if (! EMPTYSTRING(no_proxy
)) {
620 np_copy
= xstrdup(no_proxy
);
622 while ((cp
= strsep(&np_copy
, " ,")) != NULL
) {
625 if ((np
= strrchr(cp
, ':')) != NULL
) {
628 strtol(np
+ 1, &ep
, 10);
631 if (np_port
!= portnum
)
637 if (strncasecmp(host
+ hlen
- plen
,
644 if (isproxy
== 0 && urltype
== FTP_URL_T
) {
645 rval
= fetch_ftp(url
);
646 goto cleanup_fetch_url
;
651 if (parse_url(proxyenv
, "proxy URL", &purltype
,
652 &puser
, &ppass
, &phost
, &pport
, &portnum
,
654 goto cleanup_fetch_url
;
656 if ((purltype
!= HTTP_URL_T
657 && purltype
!= FTP_URL_T
) ||
658 EMPTYSTRING(phost
) ||
659 (! EMPTYSTRING(ppath
)
660 && strcmp(ppath
, "/") != 0)) {
661 warnx("Malformed proxy URL `%s'",
666 goto cleanup_fetch_url
;
668 if (isipv6addr(host
) &&
669 strchr(host
, '%') != NULL
) {
671 "Scoped address notation `%s' disallowed via web proxy",
676 goto cleanup_fetch_url
;
687 } /* ! EMPTYSTRING(proxyenv) */
689 memset(&hints
, 0, sizeof(hints
));
691 hints
.ai_family
= family
;
692 hints
.ai_socktype
= SOCK_STREAM
;
693 hints
.ai_protocol
= 0;
694 error
= getaddrinfo(host
, NULL
, &hints
, &res0
);
696 warnx("%s", gai_strerror(error
));
697 goto cleanup_fetch_url
;
699 if (res0
->ai_canonname
)
700 host
= res0
->ai_canonname
;
703 for (res
= res0
; res
; res
= res
->ai_next
) {
705 * see comment in hookup()
708 if (getnameinfo(res
->ai_addr
, res
->ai_addrlen
,
709 hbuf
, sizeof(hbuf
), NULL
, 0, NI_NUMERICHOST
) != 0)
710 strlcpy(hbuf
, "invalid", sizeof(hbuf
));
712 if (verbose
&& res
!= res0
)
713 fprintf(ttyout
, "Trying %s...\n", hbuf
);
715 ((struct sockaddr_in
*)res
->ai_addr
)->sin_port
=
717 s
= socket(res
->ai_family
, SOCK_STREAM
,
720 warn("Can't create socket");
724 if (xconnect(s
, res
->ai_addr
, res
->ai_addrlen
) < 0) {
725 warn("Connect to address `%s'", hbuf
);
736 warn("Can't connect to %s", host
);
737 goto cleanup_fetch_url
;
740 fin
= fdopen(s
, "r+");
742 * Construct and send the request.
745 fprintf(ttyout
, "Requesting %s\n", url
);
750 fprintf(ttyout
, "%svia %s:%s", leading
,
755 fprintf(fin
, "GET %s HTTP/1.0\r\n", path
);
757 fprintf(fin
, "Pragma: no-cache\r\n");
759 fprintf(fin
, "GET %s HTTP/1.1\r\n", path
);
760 if (strchr(host
, ':')) {
764 * strip off IPv6 scope identifier, since it is
769 (p
= strchr(h
, '%')) != NULL
) {
772 fprintf(fin
, "Host: [%s]", h
);
775 fprintf(fin
, "Host: %s", host
);
776 if (portnum
!= HTTP_PORT
)
777 fprintf(fin
, ":%u", portnum
);
778 fprintf(fin
, "\r\n");
779 fprintf(fin
, "Accept: */*\r\n");
780 fprintf(fin
, "Connection: close\r\n");
782 fputs(leading
, ttyout
);
783 fprintf(fin
, "Range: bytes=" LLF
"-\r\n",
785 fprintf(ttyout
, "restarting at " LLF
,
791 fprintf(fin
, "Cache-Control: no-cache\r\n");
793 if ((useragent
=getenv("FTPUSERAGENT")) != NULL
) {
794 fprintf(fin
, "User-Agent: %s\r\n", useragent
);
796 fprintf(fin
, "User-Agent: %s/%s\r\n",
797 FTP_PRODUCT
, FTP_VERSION
);
801 fprintf(ttyout
, "%swith authorization",
806 fprintf(fin
, "Authorization: %s\r\n", wwwauth
);
811 "%swith proxy authorization", leading
);
815 fprintf(fin
, "Proxy-Authorization: %s\r\n", proxyauth
);
817 if (verbose
&& hasleading
)
818 fputs(")\n", ttyout
);
819 fprintf(fin
, "\r\n");
820 if (fflush(fin
) == EOF
) {
821 warn("Writing HTTP request");
822 goto cleanup_fetch_url
;
825 /* Read the response */
826 if ((buf
= fparseln(fin
, &len
, NULL
, "\0\0\0", 0)) == NULL
) {
827 warn("Receiving HTTP reply");
828 goto cleanup_fetch_url
;
830 while (len
> 0 && (ISLWS(buf
[len
-1])))
833 fprintf(ttyout
, "received `%s'\n", buf
);
835 /* Determine HTTP response code */
836 cp
= strchr(buf
, ' ');
841 hcode
= strtol(cp
, &ep
, 10);
842 if (*ep
!= '\0' && !isspace((unsigned char)*ep
))
844 message
= xstrdup(cp
);
846 /* Read the rest of the header. */
849 if ((buf
= fparseln(fin
, &len
, NULL
, "\0\0\0", 0))
851 warn("Receiving HTTP reply");
852 goto cleanup_fetch_url
;
854 while (len
> 0 && (ISLWS(buf
[len
-1])))
859 fprintf(ttyout
, "received `%s'\n", buf
);
862 * Look for some headers
867 if (match_token(&cp
, "Content-Length:")) {
868 filesize
= STRTOLL(cp
, &ep
, 10);
869 if (filesize
< 0 || *ep
!= '\0')
873 "parsed len as: " LLF
"\n",
876 } else if (match_token(&cp
, "Content-Range:")) {
877 if (! match_token(&cp
, "bytes"))
883 rangestart
= STRTOLL(cp
, &ep
, 10);
884 if (rangestart
< 0 || *ep
!= '-')
887 rangeend
= STRTOLL(cp
, &ep
, 10);
888 if (rangeend
< 0 || rangeend
< rangestart
)
898 entitylen
= STRTOLL(cp
, &ep
, 10);
907 fprintf(ttyout
, "parsed range as: ");
908 if (rangestart
== -1)
909 fprintf(ttyout
, "*");
911 fprintf(ttyout
, LLF
"-" LLF
,
914 fprintf(ttyout
, "/" LLF
"\n", (LLT
)entitylen
);
916 if (! restart_point
) {
918 "Received unexpected Content-Range header");
919 goto cleanup_fetch_url
;
922 } else if (match_token(&cp
, "Last-Modified:")) {
927 if ((t
= strptime(cp
,
928 "%a, %d %b %Y %H:%M:%S GMT",
932 "%a, %d-%b-%y %H:%M:%S GMT",
936 "%a, %b %d %H:%M:%S %Y",
938 parsed
.tm_isdst
= -1;
940 mtime
= timegm(&parsed
);
941 if (debug
&& mtime
!= -1) {
943 "parsed date as: %s",
948 } else if (match_token(&cp
, "Location:")) {
949 location
= xstrdup(cp
);
952 "parsed location as `%s'\n", cp
);
954 } else if (match_token(&cp
, "Transfer-Encoding:")) {
955 if (match_token(&cp
, "binary")) {
957 "Bogus transfer encoding - `binary' (fetching anyway)");
960 if (! (token
= match_token(&cp
, "chunked"))) {
962 "Unsupported transfer encoding - `%s'",
964 goto cleanup_fetch_url
;
969 "using chunked encoding\n");
971 } else if (match_token(&cp
, "Proxy-Authenticate:")
972 || match_token(&cp
, "WWW-Authenticate:")) {
973 if (! (token
= match_token(&cp
, "Basic"))) {
976 "skipping unknown auth scheme `%s'\n",
981 auth
= xstrdup(token
);
984 "parsed auth as `%s'\n", cp
);
988 /* finished parsing header */
995 if (! restart_point
) {
996 warnx("Not expecting partial content header");
997 goto cleanup_fetch_url
;
1005 if (EMPTYSTRING(location
)) {
1007 "No redirection Location provided by server");
1008 goto cleanup_fetch_url
;
1010 if (redirect_loop
++ > 5) {
1011 warnx("Too many redirections requested");
1012 goto cleanup_fetch_url
;
1016 fprintf(ttyout
, "Redirected via %s\n",
1018 rval
= fetch_url(url
, location
,
1019 proxyauth
, wwwauth
);
1022 fprintf(ttyout
, "Redirected to %s\n",
1024 rval
= go_fetch(location
);
1026 goto cleanup_fetch_url
;
1032 char *auser
, *apass
;
1043 if (verbose
|| *authp
== NULL
||
1044 auser
== NULL
|| apass
== NULL
)
1045 fprintf(ttyout
, "%s\n", message
);
1046 if (EMPTYSTRING(auth
)) {
1048 "No authentication challenge provided by server");
1049 goto cleanup_fetch_url
;
1051 if (*authp
!= NULL
) {
1055 "Authorization failed. Retry (y/n)? ");
1056 if (fgets(reply
, sizeof(reply
), stdin
)
1059 goto cleanup_fetch_url
;
1061 if (tolower((unsigned char)reply
[0]) != 'y')
1062 goto cleanup_fetch_url
;
1066 if (auth_url(auth
, authp
, auser
, apass
) == 0) {
1067 rval
= fetch_url(url
, proxyenv
,
1068 proxyauth
, wwwauth
);
1069 memset(*authp
, 0, strlen(*authp
));
1072 goto cleanup_fetch_url
;
1077 warnx("Error retrieving file - `%s'", message
);
1079 warnx("Unknown error retrieving file");
1080 goto cleanup_fetch_url
;
1082 } /* end of ftp:// or http:// specific setup */
1084 /* Open the output file. */
1085 if (strcmp(savefile
, "-") == 0) {
1087 } else if (*savefile
== '|') {
1088 oldintp
= xsignal(SIGPIPE
, SIG_IGN
);
1089 fout
= popen(savefile
+ 1, "w");
1091 warn("Can't run `%s'", savefile
+ 1);
1092 goto cleanup_fetch_url
;
1096 if ((rangeend
!= -1 && rangeend
<= restart_point
) ||
1097 (rangestart
== -1 && filesize
!= -1 && filesize
<= restart_point
)) {
1100 fprintf(ttyout
, "already done\n");
1102 goto cleanup_fetch_url
;
1104 if (restart_point
&& rangestart
!= -1) {
1105 if (entitylen
!= -1)
1106 filesize
= entitylen
;
1107 if (rangestart
!= restart_point
) {
1109 "Size of `%s' differs from save file `%s'",
1111 goto cleanup_fetch_url
;
1113 fout
= fopen(savefile
, "a");
1115 fout
= fopen(savefile
, "w");
1117 warn("Can't open `%s'", savefile
);
1118 goto cleanup_fetch_url
;
1124 if (sigsetjmp(httpabort
, 1))
1125 goto cleanup_fetch_url
;
1126 (void)xsignal(SIGQUIT
, psummary
);
1127 oldintr
= xsignal(SIGINT
, aborthttp
);
1129 if (rcvbuf_size
> bufsize
) {
1131 (void)free(xferbuf
);
1132 bufsize
= rcvbuf_size
;
1133 xferbuf
= xmalloc(bufsize
);
1140 /* Finally, suck down the file. */
1145 /* read chunksize */
1147 if (fgets(xferbuf
, bufsize
, fin
) == NULL
) {
1148 warnx("Unexpected EOF reading chunksize");
1149 goto cleanup_fetch_url
;
1151 chunksize
= strtol(xferbuf
, &ep
, 16);
1154 * XXX: Work around bug in Apache 1.3.9 and
1155 * 1.3.11, which incorrectly put trailing
1156 * space after the chunksize.
1161 if (strcmp(ep
, "\r\n") != 0) {
1162 warnx("Unexpected data following chunksize");
1163 goto cleanup_fetch_url
;
1166 fprintf(ttyout
, "got chunksize of " LLF
"\n",
1171 /* transfer file or chunk */
1173 struct timeval then
, now
, td
;
1177 (void)gettimeofday(&then
, NULL
);
1178 bufrem
= rate_get
? rate_get
: bufsize
;
1180 bufrem
= MIN(chunksize
, bufrem
);
1181 while (bufrem
> 0) {
1182 len
= fread(xferbuf
, sizeof(char),
1183 MIN(bufsize
, bufrem
), fin
);
1188 if (fwrite(xferbuf
, sizeof(char), len
, fout
)
1190 warn("Writing `%s'", savefile
);
1191 goto cleanup_fetch_url
;
1193 if (hash
&& !progress
) {
1194 while (bytes
>= hashbytes
) {
1195 (void)putc('#', ttyout
);
1198 (void)fflush(ttyout
);
1208 (void)gettimeofday(&now
, NULL
);
1209 timersub(&now
, &then
, &td
);
1212 usleep(1000000 - td
.tv_usec
);
1215 if (ischunked
&& chunksize
<= 0)
1218 /* read CRLF after chunk*/
1221 if (fgets(xferbuf
, bufsize
, fin
) == NULL
)
1223 if (strcmp(xferbuf
, "\r\n") != 0) {
1224 warnx("Unexpected data following chunk");
1225 goto cleanup_fetch_url
;
1228 } while (ischunked
);
1229 if (hash
&& !progress
&& bytes
> 0) {
1231 (void)putc('#', ttyout
);
1232 (void)putc('\n', ttyout
);
1235 warn("Reading file");
1236 goto cleanup_fetch_url
;
1240 if (closefunc
== fclose
&& mtime
!= -1) {
1241 struct timeval tval
[2];
1243 (void)gettimeofday(&tval
[0], NULL
);
1244 tval
[1].tv_sec
= mtime
;
1245 tval
[1].tv_usec
= 0;
1249 if (utimes(savefile
, tval
) == -1) {
1251 "Can't change modification time to %s",
1252 asctime(localtime(&mtime
)));
1260 goto cleanup_fetch_url
;
1263 warnx("Improper response from `%s'", host
);
1267 (void)xsignal(SIGINT
, oldintr
);
1269 (void)xsignal(SIGPIPE
, oldintp
);
1274 if (closefunc
!= NULL
&& fout
!= NULL
)
1284 FREEPTR(decodedpath
);
1295 * Abort a HTTP retrieval
1298 aborthttp(int notused
)
1305 len
= strlcpy(msgbuf
, "\nHTTP fetch aborted.\n", sizeof(msgbuf
));
1306 write(fileno(ttyout
), msgbuf
, len
);
1307 siglongjmp(httpabort
, 1);
1311 * Retrieve ftp URL or classic ftp argument using FTP.
1312 * Returns 1 on failure, 0 on completed xfer, -1 if ftp connection
1313 * is still open (e.g, ftp xfer with trailing /)
1316 fetch_ftp(const char *url
)
1318 char *cp
, *xargv
[5], rempath
[MAXPATHLEN
];
1319 char *host
, *path
, *dir
, *file
, *user
, *pass
;
1321 int dirhasglob
, filehasglob
, oautologin
, rval
, type
, xargc
;
1325 host
= path
= dir
= file
= user
= pass
= NULL
;
1330 if (STRNEQUAL(url
, FTP_URL
)) {
1331 if ((parse_url(url
, "URL", &urltype
, &user
, &pass
,
1332 &host
, &port
, &portnum
, &path
) == -1) ||
1333 (user
!= NULL
&& *user
== '\0') ||
1334 EMPTYSTRING(host
)) {
1335 warnx("Invalid URL `%s'", url
);
1336 goto cleanup_fetch_ftp
;
1339 * Note: Don't url_decode(path) here. We need to keep the
1340 * distinction between "/" and "%2F" until later.
1343 /* check for trailing ';type=[aid]' */
1344 if (! EMPTYSTRING(path
) && (cp
= strrchr(path
, ';')) != NULL
) {
1345 if (strcasecmp(cp
, ";type=a") == 0)
1347 else if (strcasecmp(cp
, ";type=i") == 0)
1349 else if (strcasecmp(cp
, ";type=d") == 0) {
1351 "Directory listing via a URL is not supported");
1352 goto cleanup_fetch_ftp
;
1354 warnx("Invalid suffix `%s' in URL `%s'", cp
,
1356 goto cleanup_fetch_ftp
;
1360 } else { /* classic style `[user@]host:[file]' */
1361 urltype
= CLASSIC_URL_T
;
1362 host
= xstrdup(url
);
1363 cp
= strchr(host
, '@');
1367 anonftp
= 0; /* disable anonftp */
1368 host
= xstrdup(cp
+ 1);
1370 cp
= strchr(host
, ':');
1373 path
= xstrdup(cp
+ 1);
1376 if (EMPTYSTRING(host
))
1377 goto cleanup_fetch_ftp
;
1379 /* Extract the file and (if present) directory name. */
1381 if (! EMPTYSTRING(dir
)) {
1383 * If we are dealing with classic `[user@]host:[path]' syntax,
1384 * then a path of the form `/file' (resulting from input of the
1385 * form `host:/file') means that we should do "CWD /" before
1386 * retrieving the file. So we set dir="/" and file="file".
1388 * But if we are dealing with URLs like `ftp://host/path' then
1389 * a path of the form `/file' (resulting from a URL of the form
1390 * `ftp://host//file') means that we should do `CWD ' (with an
1391 * empty argument) before retrieving the file. So we set
1392 * dir="" and file="file".
1394 * If the path does not contain / at all, we set dir=NULL.
1395 * (We get a path without any slashes if we are dealing with
1396 * classic `[user@]host:[file]' or URL `ftp://host/file'.)
1398 * In all other cases, we set dir to a string that does not
1399 * include the final '/' that separates the dir part from the
1400 * file part of the path. (This will be the empty string if
1401 * and only if we are dealing with a path of the form `/file'
1402 * resulting from an URL of the form `ftp://host//file'.)
1404 cp
= strrchr(dir
, '/');
1405 if (cp
== dir
&& urltype
== CLASSIC_URL_T
) {
1408 } else if (cp
!= NULL
) {
1417 if (urltype
== FTP_URL_T
&& file
!= NULL
) {
1419 /* but still don't url_decode(dir) */
1423 "fetch_ftp: user `%s' pass `%s' host %s port %s "
1424 "path `%s' dir `%s' file `%s'\n",
1425 user
? user
: "<null>", pass
? pass
: "<null>",
1426 host
? host
: "<null>", port
? port
: "<null>",
1427 path
? path
: "<null>",
1428 dir
? dir
: "<null>", file
? file
: "<null>");
1430 dirhasglob
= filehasglob
= 0;
1431 if (doglob
&& urltype
== CLASSIC_URL_T
) {
1432 if (! EMPTYSTRING(dir
) && strpbrk(dir
, "*?[]{}") != NULL
)
1434 if (! EMPTYSTRING(file
) && strpbrk(file
, "*?[]{}") != NULL
)
1438 /* Set up the connection */
1440 disconnect(0, NULL
);
1441 xargv
[0] = (char *)getprogname(); /* XXX discards const */
1450 oautologin
= autologin
;
1451 /* don't autologin in setpeer(), use ftp_login() below */
1453 setpeer(xargc
, xargv
);
1454 autologin
= oautologin
;
1455 if ((connected
== 0) ||
1456 (connected
== 1 && !ftp_login(host
, user
, pass
))) {
1457 warnx("Can't connect or login to host `%s'", host
);
1458 goto cleanup_fetch_ftp
;
1466 setbinary(1, xargv
);
1469 errx(1, "fetch_ftp: unknown transfer type %d", type
);
1473 * Change directories, if necessary.
1475 * Note: don't use EMPTYSTRING(dir) below, because
1476 * dir=="" means something different from dir==NULL.
1478 if (dir
!= NULL
&& !dirhasglob
) {
1482 * If we are dealing with a classic `[user@]host:[path]'
1483 * (urltype is CLASSIC_URL_T) then we have a raw directory
1484 * name (not encoded in any way) and we can change
1485 * directories in one step.
1487 * If we are dealing with an `ftp://host/path' URL
1488 * (urltype is FTP_URL_T), then RFC 1738 says we need to
1489 * send a separate CWD command for each unescaped "/"
1490 * in the path, and we have to interpret %hex escaping
1491 * *after* we find the slashes. It's possible to get
1492 * empty components here, (from multiple adjacent
1493 * slashes in the path) and RFC 1738 says that we should
1494 * still do `CWD ' (with a null argument) in such cases.
1496 * Many ftp servers don't support `CWD ', so if there's an
1497 * error performing that command, bail out with a descriptive
1502 * host: dir="", urltype=CLASSIC_URL_T
1503 * logged in (to default directory)
1504 * host:file dir=NULL, urltype=CLASSIC_URL_T
1506 * host:dir/ dir="dir", urltype=CLASSIC_URL_T
1507 * "CWD dir", logged in
1508 * ftp://host/ dir="", urltype=FTP_URL_T
1509 * logged in (to default directory)
1510 * ftp://host/dir/ dir="dir", urltype=FTP_URL_T
1511 * "CWD dir", logged in
1512 * ftp://host/file dir=NULL, urltype=FTP_URL_T
1514 * ftp://host//file dir="", urltype=FTP_URL_T
1515 * "CWD ", "RETR file"
1516 * host:/file dir="/", urltype=CLASSIC_URL_T
1517 * "CWD /", "RETR file"
1518 * ftp://host///file dir="/", urltype=FTP_URL_T
1519 * "CWD ", "CWD ", "RETR file"
1520 * ftp://host/%2F/file dir="%2F", urltype=FTP_URL_T
1521 * "CWD /", "RETR file"
1522 * ftp://host/foo/file dir="foo", urltype=FTP_URL_T
1523 * "CWD foo", "RETR file"
1524 * ftp://host/foo/bar/file dir="foo/bar"
1525 * "CWD foo", "CWD bar", "RETR file"
1526 * ftp://host//foo/bar/file dir="/foo/bar"
1527 * "CWD ", "CWD foo", "CWD bar", "RETR file"
1528 * ftp://host/foo//bar/file dir="foo//bar"
1529 * "CWD foo", "CWD ", "CWD bar", "RETR file"
1530 * ftp://host/%2F/foo/bar/file dir="%2F/foo/bar"
1531 * "CWD /", "CWD foo", "CWD bar", "RETR file"
1532 * ftp://host/%2Ffoo/bar/file dir="%2Ffoo/bar"
1533 * "CWD /foo", "CWD bar", "RETR file"
1534 * ftp://host/%2Ffoo%2Fbar/file dir="%2Ffoo%2Fbar"
1535 * "CWD /foo/bar", "RETR file"
1536 * ftp://host/%2Ffoo%2Fbar%2Ffile dir=NULL
1537 * "RETR /foo/bar/file"
1539 * Note that we don't need `dir' after this point.
1542 if (urltype
== FTP_URL_T
) {
1543 nextpart
= strchr(dir
, '/');
1552 fprintf(ttyout
, "dir `%s', nextpart `%s'\n",
1553 dir
? dir
: "<null>",
1554 nextpart
? nextpart
: "<null>");
1555 if (urltype
== FTP_URL_T
|| *dir
!= '\0') {
1562 if (*dir
== '\0' && code
== 500)
1565 "ftp: The `CWD ' command (without a directory), which is required by\n"
1566 " RFC 1738 to support the empty directory in the URL pathname (`//'),\n"
1567 " conflicts with the server's conformance to RFC 959.\n"
1568 " Try the same URL without the `//' in the URL pathname.\n"
1570 goto cleanup_fetch_ftp
;
1574 } while (dir
!= NULL
);
1577 if (EMPTYSTRING(file
)) {
1579 goto cleanup_fetch_ftp
;
1583 (void)strlcpy(rempath
, dir
, sizeof(rempath
));
1584 (void)strlcat(rempath
, "/", sizeof(rempath
));
1585 (void)strlcat(rempath
, file
, sizeof(rempath
));
1589 /* Fetch the file(s). */
1594 if (dirhasglob
|| filehasglob
) {
1597 ointeractive
= interactive
;
1599 if (restartautofetch
)
1600 xargv
[0] = "mreget";
1604 interactive
= ointeractive
;
1606 if (outfile
== NULL
) {
1607 cp
= strrchr(file
, '/'); /* find savefile */
1613 xargv
[2] = (char *)outfile
;
1616 if (restartautofetch
)
1617 reget(xargc
, xargv
);
1622 if ((code
/ 100) == COMPLETE
)
1634 * Retrieve the given file to outfile.
1635 * Supports arguments of the form:
1636 * "host:path", "ftp://host/path" if $ftpproxy, call fetch_url() else
1638 * "http://host/path" call fetch_url() to use HTTP
1639 * "file:///path" call fetch_url() to copy
1640 * "about:..." print a message
1642 * Returns 1 on failure, 0 on completed xfer, -1 if ftp connection
1643 * is still open (e.g, ftp xfer with trailing /)
1646 go_fetch(const char *url
)
1654 if (STRNEQUAL(url
, ABOUT_URL
)) {
1655 url
+= sizeof(ABOUT_URL
) -1;
1656 if (strcasecmp(url
, "ftp") == 0 ||
1657 strcasecmp(url
, "tnftp") == 0) {
1659 "This version of ftp has been enhanced by Luke Mewburn <lukem@NetBSD.org>\n"
1660 "for the NetBSD project. Execute `man ftp' for more details.\n", ttyout
);
1661 } else if (strcasecmp(url
, "lukem") == 0) {
1663 "Luke Mewburn is the author of most of the enhancements in this ftp client.\n"
1664 "Please email feedback to <lukem@NetBSD.org>.\n", ttyout
);
1665 } else if (strcasecmp(url
, "netbsd") == 0) {
1667 "NetBSD is a freely available and redistributable UNIX-like operating system.\n"
1668 "For more information, see http://www.NetBSD.org/\n", ttyout
);
1669 } else if (strcasecmp(url
, "version") == 0) {
1670 fprintf(ttyout
, "Version: %s %s%s\n",
1671 FTP_PRODUCT
, FTP_VERSION
,
1679 fprintf(ttyout
, "`%s' is an interesting topic.\n", url
);
1681 fputs("\n", ttyout
);
1687 * Check for file:// and http:// URLs.
1689 if (STRNEQUAL(url
, HTTP_URL
) || STRNEQUAL(url
, FILE_URL
))
1690 return (fetch_url(url
, NULL
, NULL
, NULL
));
1693 * Try FTP URL-style and host:file arguments next.
1694 * If ftpproxy is set with an FTP URL, use fetch_url()
1695 * Othewise, use fetch_ftp().
1697 proxy
= getoptionvalue("ftp_proxy");
1698 if (!EMPTYSTRING(proxy
) && STRNEQUAL(url
, FTP_URL
))
1699 return (fetch_url(url
, NULL
, NULL
, NULL
));
1701 return (fetch_ftp(url
));
1705 * Retrieve multiple files from the command line,
1706 * calling go_fetch() for each file.
1708 * If an ftp path has a trailing "/", the path will be cd-ed into and
1709 * the connection remains open, and the function will return -1
1710 * (to indicate the connection is alive).
1711 * If an error occurs the return value will be the offset+1 in
1712 * argv[] of the file that caused a problem (i.e, argv[x]
1714 * Otherwise, 0 is returned if all files retrieved successfully.
1717 auto_fetch(int argc
, char *argv
[])
1719 volatile int argpos
;
1724 if (sigsetjmp(toplevel
, 1)) {
1726 disconnect(0, NULL
);
1731 (void)xsignal(SIGINT
, intr
);
1732 (void)xsignal(SIGPIPE
, lostpeer
);
1735 * Loop through as long as there's files to fetch.
1737 for (rval
= 0; (rval
== 0) && (argpos
< argc
); argpos
++) {
1738 if (strchr(argv
[argpos
], ':') == NULL
)
1742 anonftp
= 2; /* Handle "automatic" transfers. */
1743 rval
= go_fetch(argv
[argpos
]);
1744 if (outfile
!= NULL
&& strcmp(outfile
, "-") != 0
1745 && outfile
[0] != '|')
1751 if (connected
&& rval
!= -1)
1752 disconnect(0, NULL
);
1758 auto_put(int argc
, char **argv
, const char *uploadserver
)
1760 char *uargv
[4], *path
, *pathsep
;
1761 int uargc
, rval
, len
;
1764 uargv
[uargc
++] = "mput";
1765 uargv
[uargc
++] = argv
[0];
1766 uargv
[2] = uargv
[3] = NULL
;
1771 fprintf(ttyout
, "auto_put: target `%s'\n", uploadserver
);
1773 path
= xstrdup(uploadserver
);
1775 if (path
[len
- 1] != '/' && path
[len
- 1] != ':') {
1777 * make sure we always pass a directory to auto_fetch
1779 if (argc
> 1) { /* more than one file to upload */
1782 len
= strlen(uploadserver
) + 2; /* path + "/" + "\0" */
1784 path
= (char *)xmalloc(len
);
1785 (void)strlcpy(path
, uploadserver
, len
);
1786 (void)strlcat(path
, "/", len
);
1787 } else { /* single file to upload */
1789 pathsep
= strrchr(path
, '/');
1790 if (pathsep
== NULL
) {
1791 pathsep
= strrchr(path
, ':');
1792 if (pathsep
== NULL
) {
1793 warnx("Invalid URL `%s'", path
);
1794 goto cleanup_auto_put
;
1797 uargv
[2] = xstrdup(pathsep
);
1800 uargv
[2] = xstrdup(pathsep
+ 1);
1806 fprintf(ttyout
, "auto_put: URL `%s' argv[2] `%s'\n",
1807 path
, uargv
[2] ? uargv
[2] : "<null>");
1809 /* connect and cwd */
1810 rval
= auto_fetch(1, &path
);
1813 goto cleanup_auto_put
;
1815 /* XXX : is this the best way? */
1819 goto cleanup_auto_put
;
1822 for(; argv
[0] != NULL
; argv
++) {