1 /* $NetBSD: fetch.c,v 1.21 2015/10/04 04:53:26 lukem Exp $ */
2 /* from NetBSD: fetch.c,v 1.207 2015/09/12 19:38:42 wiz Exp */
5 * Copyright (c) 1997-2015 The NetBSD Foundation, Inc.
8 * This code is derived from software contributed to The NetBSD Foundation
11 * This code is derived from software contributed to The NetBSD Foundation
12 * by Scott Aaron Bamford.
14 * This code is derived from software contributed to The NetBSD Foundation
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
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.
43 #include <sys/cdefs.h>
45 __RCSID(" NetBSD: fetch.c,v 1.207 2015/09/12 19:38:42 wiz Exp ");
49 * FTP User Program -- Command line file retrieval
52 #include <sys/types.h>
53 #include <sys/param.h>
54 #include <sys/socket.h>
58 #include <netinet/in.h>
61 #include <arpa/inet.h>
92 __dead
static void aborthttp(int);
93 __dead
static void timeouthttp(int);
95 static int auth_url(const char *, char **, const char *, const char *);
96 static void base64_encode(const unsigned char *, size_t, unsigned char *);
98 static int go_fetch(const char *);
99 static int fetch_ftp(const char *);
100 static int fetch_url(const char *, const char *, char *, char *);
101 static const char *match_token(const char **, const char *);
102 static int parse_url(const char *, const char *, url_t
*, char **,
103 char **, char **, char **, in_port_t
*, char **);
104 static void url_decode(char *);
106 static int redirect_loop
;
109 #define STRNEQUAL(a,b) (strncasecmp((a), (b), sizeof((b))-1) == 0)
110 #define ISLWS(x) ((x)=='\r' || (x)=='\n' || (x)==' ' || (x)=='\t')
111 #define SKIPLWS(x) do { while (ISLWS((*x))) x++; } while (0)
114 #define ABOUT_URL "about:" /* propaganda */
115 #define FILE_URL "file://" /* file URL prefix */
116 #define FTP_URL "ftp://" /* ftp URL prefix */
117 #define HTTP_URL "http://" /* http URL prefix */
119 #define HTTPS_URL "https://" /* https URL prefix */
121 #define IS_HTTP_TYPE(urltype) \
122 (((urltype) == HTTP_URL_T) || ((urltype) == HTTPS_URL_T))
124 #define IS_HTTP_TYPE(urltype) \
125 ((urltype) == HTTP_URL_T)
129 * Determine if token is the next word in buf (case insensitive).
130 * If so, advance buf past the token and any trailing LWS, and
131 * return a pointer to the token (in buf). Otherwise, return NULL.
132 * token may be preceded by LWS.
133 * token must be followed by LWS or NUL. (I.e, don't partial match).
136 match_token(const char **buf
, const char *token
)
138 const char *p
, *orig
;
141 tlen
= strlen(token
);
145 if (strncasecmp(p
, token
, tlen
) != 0)
148 if (*p
!= '\0' && !ISLWS(*p
))
158 * Generate authorization response based on given authentication challenge.
159 * Returns -1 if an error occurred, otherwise 0.
160 * Sets response to a malloc(3)ed string; caller should free.
163 auth_url(const char *challenge
, char **response
, const char *guser
,
166 const char *cp
, *scheme
, *errormsg
;
167 char *ep
, *clear
, *realm
;
168 char uuser
[BUFSIZ
], *gotpass
;
171 size_t len
, clen
, rlen
;
174 clear
= realm
= NULL
;
177 scheme
= "Basic"; /* only support Basic authentication */
180 DPRINTF("auth_url: challenge `%s'\n", challenge
);
182 if (! match_token(&cp
, scheme
)) {
183 warnx("Unsupported authentication challenge `%s'",
185 goto cleanup_auth_url
;
188 #define REALM "realm=\""
189 if (STRNEQUAL(cp
, REALM
))
190 cp
+= sizeof(REALM
) - 1;
192 warnx("Unsupported authentication challenge `%s'",
194 goto cleanup_auth_url
;
196 /* XXX: need to improve quoted-string parsing to support \ quoting, etc. */
197 if ((ep
= strchr(cp
, '\"')) != NULL
) {
199 realm
= (char *)ftp_malloc(len
+ 1);
200 (void)strlcpy(realm
, cp
, len
+ 1);
202 warnx("Unsupported authentication challenge `%s'",
204 goto cleanup_auth_url
;
207 fprintf(ttyout
, "Username for `%s': ", realm
);
209 (void)strlcpy(uuser
, guser
, sizeof(uuser
));
210 fprintf(ttyout
, "%s\n", uuser
);
212 (void)fflush(ttyout
);
213 if (get_line(stdin
, uuser
, sizeof(uuser
), &errormsg
) < 0) {
214 warnx("%s; can't authenticate", errormsg
);
215 goto cleanup_auth_url
;
221 gotpass
= getpass("Password: ");
222 if (gotpass
== NULL
) {
223 warnx("Can't read password");
224 goto cleanup_auth_url
;
229 clen
= strlen(uuser
) + strlen(upass
) + 2; /* user + ":" + pass + "\0" */
230 clear
= (char *)ftp_malloc(clen
);
231 (void)strlcpy(clear
, uuser
, clen
);
232 (void)strlcat(clear
, ":", clen
);
233 (void)strlcat(clear
, upass
, clen
);
235 memset(gotpass
, 0, strlen(gotpass
));
237 /* scheme + " " + enc + "\0" */
238 rlen
= strlen(scheme
) + 1 + (clen
+ 2) * 4 / 3 + 1;
239 *response
= (char *)ftp_malloc(rlen
);
240 (void)strlcpy(*response
, scheme
, rlen
);
241 len
= strlcat(*response
, " ", rlen
);
242 /* use `clen - 1' to not encode the trailing NUL */
243 base64_encode((unsigned char *)clear
, clen
- 1,
244 (unsigned char *)*response
+ len
);
245 memset(clear
, 0, clen
);
255 * Encode len bytes starting at clear using base64 encoding into encoded,
256 * which should be at least ((len + 2) * 4 / 3 + 1) in size.
259 base64_encode(const unsigned char *clear
, size_t len
, unsigned char *encoded
)
261 static const unsigned char enc
[] =
262 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
267 for (i
= 0; i
< len
; i
+= 3) {
268 *(cp
++) = enc
[((clear
[i
+ 0] >> 2))];
269 *(cp
++) = enc
[((clear
[i
+ 0] << 4) & 0x30)
270 | ((clear
[i
+ 1] >> 4) & 0x0f)];
271 *(cp
++) = enc
[((clear
[i
+ 1] << 2) & 0x3c)
272 | ((clear
[i
+ 2] >> 6) & 0x03)];
273 *(cp
++) = enc
[((clear
[i
+ 2] ) & 0x3f)];
282 * Decode %xx escapes in given string, `in-place'.
285 url_decode(char *url
)
287 unsigned char *p
, *q
;
289 if (EMPTYSTRING(url
))
291 p
= q
= (unsigned char *)url
;
293 #define HEXTOINT(x) (x - (isdigit(x) ? '0' : (islower(x) ? 'a' : 'A') - 10))
296 && p
[1] && isxdigit((unsigned char)p
[1])
297 && p
[2] && isxdigit((unsigned char)p
[2])) {
298 *q
++ = HEXTOINT(p
[1]) * 16 + HEXTOINT(p
[2]);
308 * Parse URL of form (per RFC 3986):
309 * <type>://[<user>[:<password>]@]<host>[:<port>][/<path>]
310 * Returns -1 if a parse error occurred, otherwise 0.
311 * It's the caller's responsibility to url_decode() the returned
312 * user, pass and path.
314 * Sets type to url_t, each of the given char ** pointers to a
315 * malloc(3)ed strings of the relevant section, and port to
316 * the number given, or ftpport if ftp://, or httpport if http://.
318 * XXX: this is not totally RFC 3986 compliant; <path> will have the
319 * leading `/' unless it's an ftp:// URL, as this makes things easier
320 * for file:// and http:// URLs. ftp:// URLs have the `/' between the
321 * host and the URL-path removed, but any additional leading slashes
322 * in the URL-path are retained (because they imply that we should
323 * later do "CWD" with a null argument).
326 * input URL output path
327 * --------- -----------
330 * "http://host/path" "/path"
331 * "file://host/dir/file" "dir/file"
335 * "ftp://host/dir/file" "dir/file"
336 * "ftp://host//dir/file" "/dir/file"
339 parse_url(const char *url
, const char *desc
, url_t
*utype
,
340 char **uuser
, char **pass
, char **host
, char **port
,
341 in_port_t
*portnum
, char **path
)
343 const char *origurl
, *tport
;
344 char *cp
, *ep
, *thost
;
347 if (url
== NULL
|| desc
== NULL
|| utype
== NULL
|| uuser
== NULL
348 || pass
== NULL
|| host
== NULL
|| port
== NULL
|| portnum
== NULL
350 errx(1, "parse_url: invoked with NULL argument!");
351 DPRINTF("parse_url: %s `%s'\n", desc
, url
);
354 *utype
= UNKNOWN_URL_T
;
355 *uuser
= *pass
= *host
= *port
= *path
= NULL
;
359 if (STRNEQUAL(url
, HTTP_URL
)) {
360 url
+= sizeof(HTTP_URL
) - 1;
362 *portnum
= HTTP_PORT
;
364 } else if (STRNEQUAL(url
, FTP_URL
)) {
365 url
+= sizeof(FTP_URL
) - 1;
369 } else if (STRNEQUAL(url
, FILE_URL
)) {
370 url
+= sizeof(FILE_URL
) - 1;
373 } else if (STRNEQUAL(url
, HTTPS_URL
)) {
374 url
+= sizeof(HTTPS_URL
) - 1;
375 *utype
= HTTPS_URL_T
;
376 *portnum
= HTTPS_PORT
;
380 warnx("Invalid %s `%s'", desc
, url
);
384 memset(*pass
, 0, strlen(*pass
));
395 /* find [user[:pass]@]host[:port] */
396 ep
= strchr(url
, '/');
398 thost
= ftp_strdup(url
);
401 thost
= (char *)ftp_malloc(len
+ 1);
402 (void)strlcpy(thost
, url
, len
+ 1);
403 if (*utype
== FTP_URL_T
) /* skip first / for ftp URLs */
405 *path
= ftp_strdup(ep
);
408 cp
= strchr(thost
, '@'); /* look for user[:pass]@ in URLs */
410 if (*utype
== FTP_URL_T
)
411 anonftp
= 0; /* disable anonftp */
414 thost
= ftp_strdup(cp
+ 1);
415 cp
= strchr(*uuser
, ':');
418 *pass
= ftp_strdup(cp
+ 1);
427 * Check if thost is an encoded IPv6 address, as per
429 * `[' ipv6-address ']'
433 if ((ep
= strchr(cp
, ']')) == NULL
||
434 (ep
[1] != '\0' && ep
[1] != ':')) {
435 warnx("Invalid address `%s' in %s `%s'",
436 thost
, desc
, origurl
);
437 goto cleanup_parse_url
;
439 len
= ep
- cp
; /* change `[xyz]' -> `xyz' */
440 memmove(thost
, thost
+ 1, len
);
442 if (! isipv6addr(thost
)) {
443 warnx("Invalid IPv6 address `%s' in %s `%s'",
444 thost
, desc
, origurl
);
445 goto cleanup_parse_url
;
454 if ((cp
= strchr(thost
, ':')) != NULL
)
458 /* look for [:port] */
462 nport
= strtoul(cp
, &ep
, 10);
463 if (*cp
== '\0' || *ep
!= '\0' ||
464 nport
< 1 || nport
> MAX_IN_PORT_T
) {
465 warnx("Unknown port `%s' in %s `%s'",
467 goto cleanup_parse_url
;
474 *port
= ftp_strdup(tport
);
476 const char *emptypath
= "/";
477 if (*utype
== FTP_URL_T
) /* skip first / for ftp URLs */
479 *path
= ftp_strdup(emptypath
);
482 DPRINTF("parse_url: user `%s' pass `%s' host %s port %s(%d) "
484 STRorNULL(*uuser
), STRorNULL(*pass
),
485 STRorNULL(*host
), STRorNULL(*port
),
486 *portnum
? *portnum
: -1, STRorNULL(*path
));
491 sigjmp_buf httpabort
;
494 * Retrieve URL, via a proxy if necessary, using HTTP.
495 * If proxyenv is set, use that for the proxy, otherwise try ftp_proxy or
496 * http_proxy/https_proxy as appropriate.
497 * Supports HTTP redirects.
498 * Returns 1 on failure, 0 on completed xfer, -1 if ftp connection
499 * is still open (e.g, ftp xfer with trailing /)
502 fetch_url(const char *url
, const char *proxyenv
, char *proxyauth
, char *wwwauth
)
504 struct addrinfo hints
, *res
, *res0
= NULL
;
506 sigfunc
volatile oldint
;
507 sigfunc
volatile oldpipe
;
508 sigfunc
volatile oldalrm
;
509 sigfunc
volatile oldquit
;
512 int volatile ischunked
;
513 int volatile isproxy
;
518 static size_t bufsize
;
519 static char *xferbuf
;
520 const char *cp
, *token
;
523 const char *errormsg
;
524 char *volatile savefile
;
526 char *volatile location
;
527 char *volatile message
;
528 char *uuser
, *pass
, *host
, *port
, *path
;
529 char *volatile decodedpath
;
530 char *puser
, *ppass
, *useragent
;
531 off_t hashbytes
, rangestart
, rangeend
, entitylen
;
532 int (*volatile closefunc
)(FILE *);
535 const char *volatile penv
= proxyenv
;
543 DPRINTF("%s: `%s' proxyenv `%s'\n", __func__
, url
, STRorNULL(penv
));
545 oldquit
= oldalrm
= oldint
= oldpipe
= NULL
;
551 auth
= location
= message
= NULL
;
552 ischunked
= isproxy
= hcode
= 0;
554 uuser
= pass
= host
= path
= decodedpath
= puser
= ppass
= NULL
;
556 if (sigsetjmp(httpabort
, 1))
557 goto cleanup_fetch_url
;
559 if (parse_url(url
, "URL", &urltype
, &uuser
, &pass
, &host
, &port
,
560 &portnum
, &path
) == -1)
561 goto cleanup_fetch_url
;
563 if (urltype
== FILE_URL_T
&& ! EMPTYSTRING(host
)
564 && strcasecmp(host
, "localhost") != 0) {
565 warnx("No support for non local file URL `%s'", url
);
566 goto cleanup_fetch_url
;
569 if (EMPTYSTRING(path
)) {
570 if (urltype
== FTP_URL_T
) {
571 rval
= fetch_ftp(url
);
572 goto cleanup_fetch_url
;
574 if (!IS_HTTP_TYPE(urltype
) || outfile
== NULL
) {
575 warnx("Invalid URL (no file after host) `%s'", url
);
576 goto cleanup_fetch_url
;
580 decodedpath
= ftp_strdup(path
);
581 url_decode(decodedpath
);
586 cp
= strrchr(decodedpath
, '/'); /* find savefile */
588 savefile
= ftp_strdup(cp
+ 1);
590 savefile
= ftp_strdup(decodedpath
);
592 DPRINTF("%s: savefile `%s'\n", __func__
, savefile
);
593 if (EMPTYSTRING(savefile
)) {
594 if (urltype
== FTP_URL_T
) {
595 rval
= fetch_ftp(url
);
596 goto cleanup_fetch_url
;
598 warnx("No file after directory (you must specify an "
599 "output file) `%s'", url
);
600 goto cleanup_fetch_url
;
605 rangestart
= rangeend
= entitylen
= -1;
607 if (restartautofetch
) {
608 if (stat(savefile
, &sb
) == 0)
609 restart_point
= sb
.st_size
;
611 if (urltype
== FILE_URL_T
) { /* file:// URLs */
612 direction
= "copied";
613 fin
= fetch_open(decodedpath
, "r");
615 warn("Can't open `%s'", decodedpath
);
616 goto cleanup_fetch_url
;
618 if (fstat(fetch_fileno(fin
), &sb
) == 0) {
620 filesize
= sb
.st_size
;
623 if (lseek(fetch_fileno(fin
), restart_point
, SEEK_SET
) < 0) {
624 warn("Can't seek to restart `%s'",
626 goto cleanup_fetch_url
;
630 fprintf(ttyout
, "Copying %s", decodedpath
);
632 fprintf(ttyout
, " (restarting at " LLF
")",
636 if (0 == rcvbuf_size
) {
637 rcvbuf_size
= 8 * 1024; /* XXX */
639 } else { /* ftp:// or http:// URLs */
645 if (urltype
== HTTPS_URL_T
)
646 penv
= getoptionvalue("https_proxy");
648 if (penv
== NULL
&& IS_HTTP_TYPE(urltype
))
649 penv
= getoptionvalue("http_proxy");
650 else if (urltype
== FTP_URL_T
)
651 penv
= getoptionvalue("ftp_proxy");
653 direction
= "retrieved";
654 if (! EMPTYSTRING(penv
)) { /* use proxy */
657 char *pport
, *no_proxy
;
662 /* check URL against list of no_proxied sites */
663 no_proxy
= getoptionvalue("no_proxy");
664 if (! EMPTYSTRING(no_proxy
)) {
665 char *np
, *np_copy
, *np_iter
;
666 unsigned long np_port
;
669 np_iter
= np_copy
= ftp_strdup(no_proxy
);
671 while ((cp
= strsep(&np_iter
, " ,")) != NULL
) {
674 if ((np
= strrchr(cp
, ':')) != NULL
) {
676 np_port
= strtoul(np
, &ep
, 10);
677 if (*np
== '\0' || *ep
!= '\0')
679 if (np_port
!= portnum
)
685 if (strncasecmp(host
+ hlen
- plen
,
692 if (isproxy
== 0 && urltype
== FTP_URL_T
) {
693 rval
= fetch_ftp(url
);
694 goto cleanup_fetch_url
;
700 warnx("Can't restart via proxy URL `%s'",
702 goto cleanup_fetch_url
;
704 if (parse_url(penv
, "proxy URL", &purltype
,
705 &puser
, &ppass
, &phost
, &pport
, &pportnum
,
707 goto cleanup_fetch_url
;
709 if ((!IS_HTTP_TYPE(purltype
)
710 && purltype
!= FTP_URL_T
) ||
711 EMPTYSTRING(phost
) ||
712 (! EMPTYSTRING(ppath
)
713 && strcmp(ppath
, "/") != 0)) {
714 warnx("Malformed proxy URL `%s'", penv
);
718 goto cleanup_fetch_url
;
720 if (isipv6addr(host
) &&
721 strchr(host
, '%') != NULL
) {
723 "Scoped address notation `%s' disallowed via web proxy",
728 goto cleanup_fetch_url
;
736 path
= ftp_strdup(url
);
740 } /* ! EMPTYSTRING(penv) */
742 memset(&hints
, 0, sizeof(hints
));
744 hints
.ai_family
= family
;
745 hints
.ai_socktype
= SOCK_STREAM
;
746 hints
.ai_protocol
= 0;
747 error
= getaddrinfo(host
, port
, &hints
, &res0
);
749 warnx("Can't LOOKUP `%s:%s': %s", host
, port
,
750 (error
== EAI_SYSTEM
) ? strerror(errno
)
751 : gai_strerror(error
));
752 goto cleanup_fetch_url
;
754 if (res0
->ai_canonname
)
755 host
= res0
->ai_canonname
;
761 for (res
= res0
; res
; res
= res
->ai_next
) {
762 char hname
[NI_MAXHOST
], sname
[NI_MAXSERV
];
765 if (getnameinfo(res
->ai_addr
, res
->ai_addrlen
,
766 hname
, sizeof(hname
), sname
, sizeof(sname
),
767 NI_NUMERICHOST
| NI_NUMERICSERV
) != 0) {
768 strlcpy(hname
, "?", sizeof(hname
));
769 strlcpy(sname
, "?", sizeof(sname
));
772 if (verbose
&& res0
->ai_next
) {
773 fprintf(ttyout
, "Trying %s:%s ...\n",
777 s
= socket(res
->ai_family
, SOCK_STREAM
,
781 "Can't create socket for connection to "
782 "`%s:%s'", hname
, sname
);
786 if (ftp_connect(s
, res
->ai_addr
, res
->ai_addrlen
,
787 verbose
|| !res
->ai_next
) < 0) {
794 if (urltype
== HTTPS_URL_T
) {
795 if ((ssl
= fetch_start_ssl(s
, host
)) == NULL
) {
808 warnx("Can't connect to `%s:%s'", host
, port
);
809 goto cleanup_fetch_url
;
812 oldalrm
= xsignal(SIGALRM
, timeouthttp
);
813 alarmtimer(quit_time
? quit_time
: 60);
814 fin
= fetch_fdopen(s
, "r+");
815 fetch_set_ssl(fin
, ssl
);
818 alarmtimer(quit_time
? quit_time
: 60);
820 * Construct and send the request.
823 fprintf(ttyout
, "Requesting %s\n", url
);
828 fprintf(ttyout
, "%svia %s:%s", leading
,
833 fetch_printf(fin
, "GET %s HTTP/1.0\r\n", path
);
835 fetch_printf(fin
, "Pragma: no-cache\r\n");
837 fetch_printf(fin
, "GET %s HTTP/1.1\r\n", path
);
838 if (strchr(host
, ':')) {
842 * strip off IPv6 scope identifier, since it is
845 h
= ftp_strdup(host
);
847 (p
= strchr(h
, '%')) != NULL
) {
850 fetch_printf(fin
, "Host: [%s]", h
);
853 fetch_printf(fin
, "Host: %s", host
);
855 if ((urltype
== HTTP_URL_T
&& portnum
!= HTTP_PORT
) ||
856 (urltype
== HTTPS_URL_T
&& portnum
!= HTTPS_PORT
))
858 if (portnum
!= HTTP_PORT
)
860 fetch_printf(fin
, ":%u", portnum
);
861 fetch_printf(fin
, "\r\n");
862 fetch_printf(fin
, "Accept: */*\r\n");
863 fetch_printf(fin
, "Connection: close\r\n");
865 fputs(leading
, ttyout
);
866 fetch_printf(fin
, "Range: bytes=" LLF
"-\r\n",
868 fprintf(ttyout
, "restarting at " LLF
,
874 fetch_printf(fin
, "Cache-Control: no-cache\r\n");
876 if ((useragent
=getenv("FTPUSERAGENT")) != NULL
) {
877 fetch_printf(fin
, "User-Agent: %s\r\n", useragent
);
879 fetch_printf(fin
, "User-Agent: %s/%s\r\n",
880 FTP_PRODUCT
, FTP_VERSION
);
884 fprintf(ttyout
, "%swith authorization",
889 fetch_printf(fin
, "Authorization: %s\r\n", wwwauth
);
894 "%swith proxy authorization", leading
);
898 fetch_printf(fin
, "Proxy-Authorization: %s\r\n", proxyauth
);
900 if (verbose
&& hasleading
)
901 fputs(")\n", ttyout
);
902 fetch_printf(fin
, "\r\n");
903 if (fetch_flush(fin
) == EOF
) {
904 warn("Writing HTTP request");
906 goto cleanup_fetch_url
;
910 /* Read the response */
911 alarmtimer(quit_time
? quit_time
: 60);
912 len
= fetch_getline(fin
, buf
, sizeof(buf
), &errormsg
);
915 if (*errormsg
== '\n')
917 warnx("Receiving HTTP reply: %s", errormsg
);
918 goto cleanup_fetch_url
;
920 while (len
> 0 && (ISLWS(buf
[len
-1])))
922 DPRINTF("%s: received `%s'\n", __func__
, buf
);
924 /* Determine HTTP response code */
925 cp
= strchr(buf
, ' ');
930 hcode
= strtol(cp
, &ep
, 10);
931 if (*ep
!= '\0' && !isspace((unsigned char)*ep
))
933 message
= ftp_strdup(cp
);
935 /* Read the rest of the header. */
937 alarmtimer(quit_time
? quit_time
: 60);
938 len
= fetch_getline(fin
, buf
, sizeof(buf
), &errormsg
);
941 if (*errormsg
== '\n')
943 warnx("Receiving HTTP reply: %s", errormsg
);
944 goto cleanup_fetch_url
;
946 while (len
> 0 && (ISLWS(buf
[len
-1])))
950 DPRINTF("%s: received `%s'\n", __func__
, buf
);
953 * Look for some headers
958 if (match_token(&cp
, "Content-Length:")) {
959 filesize
= STRTOLL(cp
, &ep
, 10);
960 if (filesize
< 0 || *ep
!= '\0')
962 DPRINTF("%s: parsed len as: " LLF
"\n",
963 __func__
, (LLT
)filesize
);
965 } else if (match_token(&cp
, "Content-Range:")) {
966 if (! match_token(&cp
, "bytes"))
972 rangestart
= STRTOLL(cp
, &ep
, 10);
973 if (rangestart
< 0 || *ep
!= '-')
976 rangeend
= STRTOLL(cp
, &ep
, 10);
977 if (rangeend
< 0 || rangeend
< rangestart
)
987 entitylen
= STRTOLL(cp
, &ep
, 10);
997 fprintf(ttyout
, "parsed range as: ");
998 if (rangestart
== -1)
999 fprintf(ttyout
, "*");
1001 fprintf(ttyout
, LLF
"-" LLF
,
1004 fprintf(ttyout
, "/" LLF
"\n", (LLT
)entitylen
);
1007 if (! restart_point
) {
1009 "Received unexpected Content-Range header");
1010 goto cleanup_fetch_url
;
1013 } else if (match_token(&cp
, "Last-Modified:")) {
1017 memset(&parsed
, 0, sizeof(parsed
));
1018 t
= parse_rfc2616time(&parsed
, cp
);
1020 parsed
.tm_isdst
= -1;
1022 mtime
= timegm(&parsed
);
1024 if (ftp_debug
&& mtime
!= -1) {
1026 "parsed time as: %s",
1027 rfc2822time(localtime(&mtime
)));
1032 } else if (match_token(&cp
, "Location:")) {
1033 location
= ftp_strdup(cp
);
1034 DPRINTF("%s: parsed location as `%s'\n",
1037 } else if (match_token(&cp
, "Transfer-Encoding:")) {
1038 if (match_token(&cp
, "binary")) {
1040 "Bogus transfer encoding `binary' (fetching anyway)");
1043 if (! (token
= match_token(&cp
, "chunked"))) {
1045 "Unsupported transfer encoding `%s'",
1047 goto cleanup_fetch_url
;
1050 DPRINTF("%s: using chunked encoding\n",
1053 } else if (match_token(&cp
, "Proxy-Authenticate:")
1054 || match_token(&cp
, "WWW-Authenticate:")) {
1055 if (! (token
= match_token(&cp
, "Basic"))) {
1056 DPRINTF("%s: skipping unknown auth "
1057 "scheme `%s'\n", __func__
, token
);
1061 auth
= ftp_strdup(token
);
1062 DPRINTF("%s: parsed auth as `%s'\n",
1067 /* finished parsing header */
1073 if (! restart_point
) {
1074 warnx("Not expecting partial content header");
1075 goto cleanup_fetch_url
;
1084 if (EMPTYSTRING(location
)) {
1086 "No redirection Location provided by server");
1087 goto cleanup_fetch_url
;
1089 if (redirect_loop
++ > 5) {
1090 warnx("Too many redirections requested");
1091 goto cleanup_fetch_url
;
1095 fprintf(ttyout
, "Redirected via %s\n",
1097 rval
= fetch_url(url
, location
,
1098 proxyauth
, wwwauth
);
1101 fprintf(ttyout
, "Redirected to %s\n",
1103 rval
= go_fetch(location
);
1105 goto cleanup_fetch_url
;
1111 char *auser
, *apass
;
1122 if (verbose
|| *authp
== NULL
||
1123 auser
== NULL
|| apass
== NULL
)
1124 fprintf(ttyout
, "%s\n", message
);
1125 if (EMPTYSTRING(auth
)) {
1127 "No authentication challenge provided by server");
1128 goto cleanup_fetch_url
;
1130 if (*authp
!= NULL
) {
1134 "Authorization failed. Retry (y/n)? ");
1135 if (get_line(stdin
, reply
, sizeof(reply
), NULL
)
1137 goto cleanup_fetch_url
;
1139 if (tolower((unsigned char)reply
[0]) != 'y')
1140 goto cleanup_fetch_url
;
1144 if (auth_url(auth
, authp
, auser
, apass
) == 0) {
1145 rval
= fetch_url(url
, penv
,
1146 proxyauth
, wwwauth
);
1147 memset(*authp
, 0, strlen(*authp
));
1150 goto cleanup_fetch_url
;
1155 warnx("Error retrieving file `%s'", message
);
1157 warnx("Unknown error retrieving file");
1158 goto cleanup_fetch_url
;
1160 } /* end of ftp:// or http:// specific setup */
1162 /* Open the output file. */
1165 * Only trust filenames with special meaning if they came from
1168 if (outfile
== savefile
) {
1169 if (strcmp(savefile
, "-") == 0) {
1171 } else if (*savefile
== '|') {
1173 oldpipe
= xsignal(SIGPIPE
, SIG_IGN
);
1174 fout
= popen(savefile
+ 1, "w");
1176 warn("Can't execute `%s'", savefile
+ 1);
1177 goto cleanup_fetch_url
;
1184 if ((rangeend
!= -1 && rangeend
<= restart_point
) ||
1185 (rangestart
== -1 && filesize
!= -1 && filesize
<= restart_point
)) {
1188 fprintf(ttyout
, "already done\n");
1190 goto cleanup_fetch_url
;
1192 if (restart_point
&& rangestart
!= -1) {
1193 if (entitylen
!= -1)
1194 filesize
= entitylen
;
1195 if (rangestart
!= restart_point
) {
1197 "Size of `%s' differs from save file `%s'",
1199 goto cleanup_fetch_url
;
1201 fout
= fopen(savefile
, "a");
1203 fout
= fopen(savefile
, "w");
1205 warn("Can't open `%s'", savefile
);
1206 goto cleanup_fetch_url
;
1212 oldquit
= xsignal(SIGQUIT
, psummary
);
1213 oldint
= xsignal(SIGINT
, aborthttp
);
1215 assert(rcvbuf_size
> 0);
1216 if ((size_t)rcvbuf_size
> bufsize
) {
1218 (void)free(xferbuf
);
1219 bufsize
= rcvbuf_size
;
1220 xferbuf
= ftp_malloc(bufsize
);
1226 (void)xsignal(SIGALRM
, oldalrm
);
1231 /* Finally, suck down the file. */
1238 /* read chunk-size */
1240 if (fetch_getln(xferbuf
, bufsize
, fin
) == NULL
) {
1241 warnx("Unexpected EOF reading chunk-size");
1242 goto cleanup_fetch_url
;
1245 chunksize
= strtol(xferbuf
, &ep
, 16);
1246 if (ep
== xferbuf
) {
1247 warnx("Invalid chunk-size");
1248 goto cleanup_fetch_url
;
1250 if (errno
== ERANGE
|| chunksize
< 0) {
1252 warn("Chunk-size `%.*s'",
1253 (int)(ep
-xferbuf
), xferbuf
);
1254 goto cleanup_fetch_url
;
1258 * XXX: Work around bug in Apache 1.3.9 and
1259 * 1.3.11, which incorrectly put trailing
1260 * space after the chunk-size.
1265 /* skip [ chunk-ext ] */
1267 while (*ep
&& *ep
!= '\r')
1271 if (strcmp(ep
, "\r\n") != 0) {
1272 warnx("Unexpected data following chunk-size");
1273 goto cleanup_fetch_url
;
1275 DPRINTF("%s: got chunk-size of " LLF
"\n", __func__
,
1277 if (chunksize
== 0) {
1282 /* transfer file or chunk */
1284 struct timeval then
, now
, td
;
1285 volatile off_t bufrem
;
1288 (void)gettimeofday(&then
, NULL
);
1289 bufrem
= rate_get
? rate_get
: (off_t
)bufsize
;
1291 bufrem
= MIN(chunksize
, bufrem
);
1292 while (bufrem
> 0) {
1293 flen
= fetch_read(xferbuf
, sizeof(char),
1294 MIN((off_t
)bufsize
, bufrem
), fin
);
1299 if (fwrite(xferbuf
, sizeof(char), flen
, fout
)
1301 warn("Writing `%s'", savefile
);
1302 goto cleanup_fetch_url
;
1304 if (hash
&& !progress
) {
1305 while (bytes
>= hashbytes
) {
1306 (void)putc('#', ttyout
);
1309 (void)fflush(ttyout
);
1319 (void)gettimeofday(&now
, NULL
);
1320 timersub(&now
, &then
, &td
);
1323 usleep(1000000 - td
.tv_usec
);
1326 if (ischunked
&& chunksize
<= 0)
1329 /* read CRLF after chunk*/
1332 if (fetch_getln(xferbuf
, bufsize
, fin
) == NULL
) {
1334 warnx("Unexpected EOF reading chunk CRLF");
1335 goto cleanup_fetch_url
;
1337 if (strcmp(xferbuf
, "\r\n") != 0) {
1338 warnx("Unexpected data following chunk");
1339 goto cleanup_fetch_url
;
1344 } while (ischunked
);
1346 /* XXX: deal with optional trailer & CRLF here? */
1348 if (hash
&& !progress
&& bytes
> 0) {
1350 (void)putc('#', ttyout
);
1351 (void)putc('\n', ttyout
);
1353 if (fetch_error(fin
)) {
1354 warn("Reading file");
1355 goto cleanup_fetch_url
;
1359 if (closefunc
== fclose
&& mtime
!= -1) {
1360 struct timeval tval
[2];
1362 (void)gettimeofday(&tval
[0], NULL
);
1363 tval
[1].tv_sec
= mtime
;
1364 tval
[1].tv_usec
= 0;
1368 if (utimes(savefile
, tval
) == -1) {
1370 "Can't change modification time to %s",
1371 rfc2822time(localtime(&mtime
)));
1379 goto cleanup_fetch_url
;
1382 warnx("Improper response from `%s:%s'", host
, port
);
1386 (void)xsignal(SIGINT
, oldint
);
1388 (void)xsignal(SIGPIPE
, oldpipe
);
1390 (void)xsignal(SIGALRM
, oldalrm
);
1392 (void)xsignal(SIGQUIT
, oldpipe
);
1397 if (closefunc
!= NULL
&& fout
!= NULL
)
1401 if (savefile
!= outfile
)
1405 memset(pass
, 0, strlen(pass
));
1410 FREEPTR(decodedpath
);
1413 memset(ppass
, 0, strlen(ppass
));
1422 * Abort a HTTP retrieval
1425 aborthttp(int notused
)
1433 len
= snprintf(msgbuf
, sizeof(msgbuf
),
1434 "\n%s: HTTP fetch aborted.\n", getprogname());
1436 write(fileno(ttyout
), msgbuf
, len
);
1438 siglongjmp(httpabort
, 1);
1442 timeouthttp(int notused
)
1449 len
= snprintf(msgbuf
, sizeof(msgbuf
),
1450 "\n%s: HTTP fetch timeout.\n", getprogname());
1452 write(fileno(ttyout
), msgbuf
, len
);
1454 siglongjmp(httpabort
, 1);
1458 * Retrieve ftp URL or classic ftp argument using FTP.
1459 * Returns 1 on failure, 0 on completed xfer, -1 if ftp connection
1460 * is still open (e.g, ftp xfer with trailing /)
1463 fetch_ftp(const char *url
)
1465 char *cp
, *xargv
[5], rempath
[MAXPATHLEN
];
1466 char *host
, *path
, *dir
, *file
, *uuser
, *pass
;
1468 char cmdbuf
[MAXPATHLEN
];
1470 int dirhasglob
, filehasglob
, rval
, transtype
, xargc
;
1471 int oanonftp
, oautologin
;
1475 DPRINTF("fetch_ftp: `%s'\n", url
);
1476 host
= path
= dir
= file
= uuser
= pass
= NULL
;
1481 if (STRNEQUAL(url
, FTP_URL
)) {
1482 if ((parse_url(url
, "URL", &urltype
, &uuser
, &pass
,
1483 &host
, &port
, &portnum
, &path
) == -1) ||
1484 (uuser
!= NULL
&& *uuser
== '\0') ||
1485 EMPTYSTRING(host
)) {
1486 warnx("Invalid URL `%s'", url
);
1487 goto cleanup_fetch_ftp
;
1490 * Note: Don't url_decode(path) here. We need to keep the
1491 * distinction between "/" and "%2F" until later.
1494 /* check for trailing ';type=[aid]' */
1495 if (! EMPTYSTRING(path
) && (cp
= strrchr(path
, ';')) != NULL
) {
1496 if (strcasecmp(cp
, ";type=a") == 0)
1498 else if (strcasecmp(cp
, ";type=i") == 0)
1500 else if (strcasecmp(cp
, ";type=d") == 0) {
1502 "Directory listing via a URL is not supported");
1503 goto cleanup_fetch_ftp
;
1505 warnx("Invalid suffix `%s' in URL `%s'", cp
,
1507 goto cleanup_fetch_ftp
;
1511 } else { /* classic style `[user@]host:[file]' */
1512 urltype
= CLASSIC_URL_T
;
1513 host
= ftp_strdup(url
);
1514 cp
= strchr(host
, '@');
1518 anonftp
= 0; /* disable anonftp */
1519 host
= ftp_strdup(cp
+ 1);
1521 cp
= strchr(host
, ':');
1524 path
= ftp_strdup(cp
+ 1);
1527 if (EMPTYSTRING(host
))
1528 goto cleanup_fetch_ftp
;
1530 /* Extract the file and (if present) directory name. */
1532 if (! EMPTYSTRING(dir
)) {
1534 * If we are dealing with classic `[user@]host:[path]' syntax,
1535 * then a path of the form `/file' (resulting from input of the
1536 * form `host:/file') means that we should do "CWD /" before
1537 * retrieving the file. So we set dir="/" and file="file".
1539 * But if we are dealing with URLs like `ftp://host/path' then
1540 * a path of the form `/file' (resulting from a URL of the form
1541 * `ftp://host//file') means that we should do `CWD ' (with an
1542 * empty argument) before retrieving the file. So we set
1543 * dir="" and file="file".
1545 * If the path does not contain / at all, we set dir=NULL.
1546 * (We get a path without any slashes if we are dealing with
1547 * classic `[user@]host:[file]' or URL `ftp://host/file'.)
1549 * In all other cases, we set dir to a string that does not
1550 * include the final '/' that separates the dir part from the
1551 * file part of the path. (This will be the empty string if
1552 * and only if we are dealing with a path of the form `/file'
1553 * resulting from an URL of the form `ftp://host//file'.)
1555 cp
= strrchr(dir
, '/');
1556 if (cp
== dir
&& urltype
== CLASSIC_URL_T
) {
1558 (void)strlcpy(dirbuf
, "/", sizeof(dirbuf
));
1560 } else if (cp
!= NULL
) {
1569 if (urltype
== FTP_URL_T
&& file
!= NULL
) {
1571 /* but still don't url_decode(dir) */
1573 DPRINTF("fetch_ftp: user `%s' pass `%s' host %s port %s "
1574 "path `%s' dir `%s' file `%s'\n",
1575 STRorNULL(uuser
), STRorNULL(pass
),
1576 STRorNULL(host
), STRorNULL(port
),
1577 STRorNULL(path
), STRorNULL(dir
), STRorNULL(file
));
1579 dirhasglob
= filehasglob
= 0;
1580 if (doglob
&& urltype
== CLASSIC_URL_T
) {
1581 if (! EMPTYSTRING(dir
) && strpbrk(dir
, "*?[]{}") != NULL
)
1583 if (! EMPTYSTRING(file
) && strpbrk(file
, "*?[]{}") != NULL
)
1587 /* Set up the connection */
1590 disconnect(0, NULL
);
1592 (void)strlcpy(cmdbuf
, getprogname(), sizeof(cmdbuf
));
1602 oautologin
= autologin
;
1603 /* don't autologin in setpeer(), use ftp_login() below */
1605 setpeer(xargc
, xargv
);
1606 autologin
= oautologin
;
1607 if ((connected
== 0) ||
1608 (connected
== 1 && !ftp_login(host
, uuser
, pass
))) {
1609 warnx("Can't connect or login to host `%s:%s'",
1610 host
, port
? port
: "?");
1611 goto cleanup_fetch_ftp
;
1614 switch (transtype
) {
1619 setbinary(1, xargv
);
1622 errx(1, "fetch_ftp: unknown transfer type %d", transtype
);
1626 * Change directories, if necessary.
1628 * Note: don't use EMPTYSTRING(dir) below, because
1629 * dir=="" means something different from dir==NULL.
1631 if (dir
!= NULL
&& !dirhasglob
) {
1635 * If we are dealing with a classic `[user@]host:[path]'
1636 * (urltype is CLASSIC_URL_T) then we have a raw directory
1637 * name (not encoded in any way) and we can change
1638 * directories in one step.
1640 * If we are dealing with an `ftp://host/path' URL
1641 * (urltype is FTP_URL_T), then RFC 3986 says we need to
1642 * send a separate CWD command for each unescaped "/"
1643 * in the path, and we have to interpret %hex escaping
1644 * *after* we find the slashes. It's possible to get
1645 * empty components here, (from multiple adjacent
1646 * slashes in the path) and RFC 3986 says that we should
1647 * still do `CWD ' (with a null argument) in such cases.
1649 * Many ftp servers don't support `CWD ', so if there's an
1650 * error performing that command, bail out with a descriptive
1655 * host: dir="", urltype=CLASSIC_URL_T
1656 * logged in (to default directory)
1657 * host:file dir=NULL, urltype=CLASSIC_URL_T
1659 * host:dir/ dir="dir", urltype=CLASSIC_URL_T
1660 * "CWD dir", logged in
1661 * ftp://host/ dir="", urltype=FTP_URL_T
1662 * logged in (to default directory)
1663 * ftp://host/dir/ dir="dir", urltype=FTP_URL_T
1664 * "CWD dir", logged in
1665 * ftp://host/file dir=NULL, urltype=FTP_URL_T
1667 * ftp://host//file dir="", urltype=FTP_URL_T
1668 * "CWD ", "RETR file"
1669 * host:/file dir="/", urltype=CLASSIC_URL_T
1670 * "CWD /", "RETR file"
1671 * ftp://host///file dir="/", urltype=FTP_URL_T
1672 * "CWD ", "CWD ", "RETR file"
1673 * ftp://host/%2F/file dir="%2F", urltype=FTP_URL_T
1674 * "CWD /", "RETR file"
1675 * ftp://host/foo/file dir="foo", urltype=FTP_URL_T
1676 * "CWD foo", "RETR file"
1677 * ftp://host/foo/bar/file dir="foo/bar"
1678 * "CWD foo", "CWD bar", "RETR file"
1679 * ftp://host//foo/bar/file dir="/foo/bar"
1680 * "CWD ", "CWD foo", "CWD bar", "RETR file"
1681 * ftp://host/foo//bar/file dir="foo//bar"
1682 * "CWD foo", "CWD ", "CWD bar", "RETR file"
1683 * ftp://host/%2F/foo/bar/file dir="%2F/foo/bar"
1684 * "CWD /", "CWD foo", "CWD bar", "RETR file"
1685 * ftp://host/%2Ffoo/bar/file dir="%2Ffoo/bar"
1686 * "CWD /foo", "CWD bar", "RETR file"
1687 * ftp://host/%2Ffoo%2Fbar/file dir="%2Ffoo%2Fbar"
1688 * "CWD /foo/bar", "RETR file"
1689 * ftp://host/%2Ffoo%2Fbar%2Ffile dir=NULL
1690 * "RETR /foo/bar/file"
1692 * Note that we don't need `dir' after this point.
1695 if (urltype
== FTP_URL_T
) {
1696 nextpart
= strchr(dir
, '/');
1704 DPRINTF("fetch_ftp: dir `%s', nextpart `%s'\n",
1705 STRorNULL(dir
), STRorNULL(nextpart
));
1706 if (urltype
== FTP_URL_T
|| *dir
!= '\0') {
1707 (void)strlcpy(cmdbuf
, "cd", sizeof(cmdbuf
));
1714 if (*dir
== '\0' && code
== 500)
1717 "ftp: The `CWD ' command (without a directory), which is required by\n"
1718 " RFC 3986 to support the empty directory in the URL pathname (`//'),\n"
1719 " conflicts with the server's conformance to RFC 959.\n"
1720 " Try the same URL without the `//' in the URL pathname.\n"
1722 goto cleanup_fetch_ftp
;
1726 } while (dir
!= NULL
);
1729 if (EMPTYSTRING(file
)) {
1731 goto cleanup_fetch_ftp
;
1735 (void)strlcpy(rempath
, dir
, sizeof(rempath
));
1736 (void)strlcat(rempath
, "/", sizeof(rempath
));
1737 (void)strlcat(rempath
, file
, sizeof(rempath
));
1741 /* Fetch the file(s). */
1743 (void)strlcpy(cmdbuf
, "get", sizeof(cmdbuf
));
1747 if (dirhasglob
|| filehasglob
) {
1750 ointeractive
= interactive
;
1752 if (restartautofetch
)
1753 (void)strlcpy(cmdbuf
, "mreget", sizeof(cmdbuf
));
1755 (void)strlcpy(cmdbuf
, "mget", sizeof(cmdbuf
));
1758 interactive
= ointeractive
;
1760 if (outfile
== NULL
) {
1761 cp
= strrchr(file
, '/'); /* find savefile */
1767 xargv
[2] = (char *)outfile
;
1770 if (restartautofetch
)
1771 reget(xargc
, xargv
);
1776 if ((code
/ 100) == COMPLETE
)
1785 memset(pass
, 0, strlen(pass
));
1791 * Retrieve the given file to outfile.
1792 * Supports arguments of the form:
1793 * "host:path", "ftp://host/path" if $ftpproxy, call fetch_url() else
1795 * "http://host/path" call fetch_url() to use HTTP
1796 * "file:///path" call fetch_url() to copy
1797 * "about:..." print a message
1799 * Returns 1 on failure, 0 on completed xfer, -1 if ftp connection
1800 * is still open (e.g, ftp xfer with trailing /)
1803 go_fetch(const char *url
)
1812 if (STRNEQUAL(url
, ABOUT_URL
)) {
1813 url
+= sizeof(ABOUT_URL
) -1;
1814 if (strcasecmp(url
, "ftp") == 0 ||
1815 strcasecmp(url
, "tnftp") == 0) {
1817 "This version of ftp has been enhanced by Luke Mewburn <lukem@NetBSD.org>\n"
1818 "for the NetBSD project. Execute `man ftp' for more details.\n", ttyout
);
1819 } else if (strcasecmp(url
, "lukem") == 0) {
1821 "Luke Mewburn is the author of most of the enhancements in this ftp client.\n"
1822 "Please email feedback to <lukem@NetBSD.org>.\n", ttyout
);
1823 } else if (strcasecmp(url
, "netbsd") == 0) {
1825 "NetBSD is a freely available and redistributable UNIX-like operating system.\n"
1826 "For more information, see http://www.NetBSD.org/\n", ttyout
);
1827 } else if (strcasecmp(url
, "version") == 0) {
1828 fprintf(ttyout
, "Version: %s %s%s\n",
1829 FTP_PRODUCT
, FTP_VERSION
,
1837 fprintf(ttyout
, "`%s' is an interesting topic.\n", url
);
1839 fputs("\n", ttyout
);
1845 * Check for file:// and http:// URLs.
1847 if (STRNEQUAL(url
, HTTP_URL
)
1849 || STRNEQUAL(url
, HTTPS_URL
)
1851 || STRNEQUAL(url
, FILE_URL
))
1852 return (fetch_url(url
, NULL
, NULL
, NULL
));
1855 * If it contains "://" but does not begin with ftp://
1856 * or something that was already handled, then it's
1859 * If it contains ":" but not "://" then we assume the
1860 * part before the colon is a host name, not an URL scheme,
1861 * so we don't try to match that here.
1863 if ((p
= strstr(url
, "://")) != NULL
&& ! STRNEQUAL(url
, FTP_URL
))
1864 errx(1, "Unsupported URL scheme `%.*s'", (int)(p
- url
), url
);
1867 * Try FTP URL-style and host:file arguments next.
1868 * If ftpproxy is set with an FTP URL, use fetch_url()
1869 * Othewise, use fetch_ftp().
1871 proxyenv
= getoptionvalue("ftp_proxy");
1872 if (!EMPTYSTRING(proxyenv
) && STRNEQUAL(url
, FTP_URL
))
1873 return (fetch_url(url
, NULL
, NULL
, NULL
));
1875 return (fetch_ftp(url
));
1879 * Retrieve multiple files from the command line,
1880 * calling go_fetch() for each file.
1882 * If an ftp path has a trailing "/", the path will be cd-ed into and
1883 * the connection remains open, and the function will return -1
1884 * (to indicate the connection is alive).
1885 * If an error occurs the return value will be the offset+1 in
1886 * argv[] of the file that caused a problem (i.e, argv[x]
1888 * Otherwise, 0 is returned if all files retrieved successfully.
1891 auto_fetch(int argc
, char *argv
[])
1893 volatile int argpos
, rval
;
1897 if (sigsetjmp(toplevel
, 1)) {
1899 disconnect(0, NULL
);
1904 (void)xsignal(SIGINT
, intr
);
1905 (void)xsignal(SIGPIPE
, lostpeer
);
1908 * Loop through as long as there's files to fetch.
1910 for (; (rval
== 0) && (argpos
< argc
); argpos
++) {
1911 if (strchr(argv
[argpos
], ':') == NULL
)
1915 anonftp
= 2; /* Handle "automatic" transfers. */
1916 rval
= go_fetch(argv
[argpos
]);
1917 if (outfile
!= NULL
&& strcmp(outfile
, "-") != 0
1918 && outfile
[0] != '|')
1924 if (connected
&& rval
!= -1)
1925 disconnect(0, NULL
);
1931 * Upload multiple files from the command line.
1933 * If an error occurs the return value will be the offset+1 in
1934 * argv[] of the file that caused a problem (i.e, argv[x]
1936 * Otherwise, 0 is returned if all files uploaded successfully.
1939 auto_put(int argc
, char **argv
, const char *uploadserver
)
1941 char *uargv
[4], *path
, *pathsep
;
1942 int uargc
, rval
, argpos
;
1944 char cmdbuf
[MAX_C_NAME
];
1946 (void)strlcpy(cmdbuf
, "mput", sizeof(cmdbuf
));
1950 uargv
[2] = uargv
[3] = NULL
;
1954 DPRINTF("auto_put: target `%s'\n", uploadserver
);
1956 path
= ftp_strdup(uploadserver
);
1958 if (path
[len
- 1] != '/' && path
[len
- 1] != ':') {
1960 * make sure we always pass a directory to auto_fetch
1962 if (argc
> 1) { /* more than one file to upload */
1963 len
= strlen(uploadserver
) + 2; /* path + "/" + "\0" */
1965 path
= (char *)ftp_malloc(len
);
1966 (void)strlcpy(path
, uploadserver
, len
);
1967 (void)strlcat(path
, "/", len
);
1968 } else { /* single file to upload */
1969 (void)strlcpy(cmdbuf
, "put", sizeof(cmdbuf
));
1971 pathsep
= strrchr(path
, '/');
1972 if (pathsep
== NULL
) {
1973 pathsep
= strrchr(path
, ':');
1974 if (pathsep
== NULL
) {
1975 warnx("Invalid URL `%s'", path
);
1976 goto cleanup_auto_put
;
1979 uargv
[2] = ftp_strdup(pathsep
);
1982 uargv
[2] = ftp_strdup(pathsep
+ 1);
1987 DPRINTF("auto_put: URL `%s' argv[2] `%s'\n",
1988 path
, STRorNULL(uargv
[2]));
1990 /* connect and cwd */
1991 rval
= auto_fetch(1, &path
);
1993 goto cleanup_auto_put
;
1997 /* target filename provided; upload 1 file */
1998 /* XXX : is this the best way? */
2002 if ((code
/ 100) != COMPLETE
)
2004 } else { /* otherwise a target dir: upload all files to it */
2005 for(argpos
= 0; argv
[argpos
] != NULL
; argpos
++) {
2006 uargv
[1] = argv
[argpos
];
2008 if ((code
/ 100) != COMPLETE
) {