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 == '|') {
1172 oldpipe = xsignal(SIGPIPE, SIG_IGN);
1173 fout = popen(savefile + 1, "w");
1175 warn("Can't execute `%s'", savefile + 1);
1176 goto cleanup_fetch_url;
1182 if ((rangeend != -1 && rangeend <= restart_point) ||
1183 (rangestart == -1 && filesize != -1 && filesize <= restart_point)) {
1186 fprintf(ttyout, "already done\n");
1188 goto cleanup_fetch_url;
1190 if (restart_point && rangestart != -1) {
1191 if (entitylen != -1)
1192 filesize = entitylen;
1193 if (rangestart != restart_point) {
1195 "Size of `%s' differs from save file `%s'",
1197 goto cleanup_fetch_url;
1199 fout = fopen(savefile, "a");
1201 fout = fopen(savefile, "w");
1203 warn("Can't open `%s'", savefile);
1204 goto cleanup_fetch_url;
1210 oldquit = xsignal(SIGQUIT, psummary);
1211 oldint = xsignal(SIGINT, aborthttp);
1213 assert(rcvbuf_size > 0);
1214 if ((size_t)rcvbuf_size > bufsize) {
1216 (void)free(xferbuf);
1217 bufsize = rcvbuf_size;
1218 xferbuf = ftp_malloc(bufsize);
1224 (void)xsignal(SIGALRM, oldalrm);
1229 /* Finally, suck down the file. */
1236 /* read chunk-size */
1238 if (fetch_getln(xferbuf, bufsize, fin) == NULL) {
1239 warnx("Unexpected EOF reading chunk-size");
1240 goto cleanup_fetch_url;
1243 chunksize = strtol(xferbuf, &ep, 16);
1244 if (ep == xferbuf) {
1245 warnx("Invalid chunk-size");
1246 goto cleanup_fetch_url;
1248 if (errno == ERANGE || chunksize < 0) {
1250 warn("Chunk-size `%.*s'",
1251 (int)(ep-xferbuf), xferbuf);
1252 goto cleanup_fetch_url;
1256 * XXX: Work around bug in Apache 1.3.9 and
1257 * 1.3.11, which incorrectly put trailing
1258 * space after the chunk-size.
1263 /* skip [ chunk-ext ] */
1265 while (*ep && *ep != '\r')
1269 if (strcmp(ep, "\r\n") != 0) {
1270 warnx("Unexpected data following chunk-size");
1271 goto cleanup_fetch_url;
1273 DPRINTF("%s: got chunk-size of " LLF "\n", __func__,
1275 if (chunksize == 0) {
1280 /* transfer file or chunk */
1282 struct timeval then, now, td;
1283 volatile off_t bufrem;
1286 (void)gettimeofday(&then, NULL);
1287 bufrem = rate_get ? rate_get : (off_t)bufsize;
1289 bufrem = MIN(chunksize, bufrem);
1290 while (bufrem > 0) {
1291 flen = fetch_read(xferbuf, sizeof(char),
1292 MIN((off_t)bufsize, bufrem), fin);
1297 if (fwrite(xferbuf, sizeof(char), flen, fout)
1299 warn("Writing `%s'", savefile);
1300 goto cleanup_fetch_url;
1302 if (hash && !progress) {
1303 while (bytes >= hashbytes) {
1304 (void)putc('#', ttyout);
1307 (void)fflush(ttyout);
1317 (void)gettimeofday(&now, NULL);
1318 timersub(&now, &then, &td);
1321 usleep(1000000 - td.tv_usec);
1324 if (ischunked && chunksize <= 0)
1327 /* read CRLF after chunk*/
1330 if (fetch_getln(xferbuf, bufsize, fin) == NULL) {
1332 warnx("Unexpected EOF reading chunk CRLF");
1333 goto cleanup_fetch_url;
1335 if (strcmp(xferbuf, "\r\n") != 0) {
1336 warnx("Unexpected data following chunk");
1337 goto cleanup_fetch_url;
1342 } while (ischunked);
1344 /* XXX: deal with optional trailer & CRLF here? */
1346 if (hash && !progress && bytes > 0) {
1348 (void)putc('#', ttyout);
1349 (void)putc('\n', ttyout);
1351 if (fetch_error(fin)) {
1352 warn("Reading file");
1353 goto cleanup_fetch_url;
1357 if (closefunc == fclose && mtime != -1) {
1358 struct timeval tval[2];
1360 (void)gettimeofday(&tval[0], NULL);
1361 tval[1].tv_sec = mtime;
1362 tval[1].tv_usec = 0;
1366 if (utimes(savefile, tval) == -1) {
1368 "Can't change modification time to %s",
1369 rfc2822time(localtime(&mtime)));
1377 goto cleanup_fetch_url;
1380 warnx("Improper response from `%s:%s'", host, port);
1384 (void)xsignal(SIGINT, oldint);
1386 (void)xsignal(SIGPIPE, oldpipe);
1388 (void)xsignal(SIGALRM, oldalrm);
1390 (void)xsignal(SIGQUIT, oldpipe);
1395 if (closefunc != NULL && fout != NULL)
1399 if (savefile != outfile)
1403 memset(pass, 0, strlen(pass));
1408 FREEPTR(decodedpath);
1411 memset(ppass, 0, strlen(ppass));
1420 * Abort a HTTP retrieval
1423 aborthttp(int notused)
1431 len = snprintf(msgbuf, sizeof(msgbuf),
1432 "\n%s: HTTP fetch aborted.\n", getprogname());
1434 write(fileno(ttyout), msgbuf, len);
1436 siglongjmp(httpabort, 1);
1440 timeouthttp(int notused)
1447 len = snprintf(msgbuf, sizeof(msgbuf),
1448 "\n%s: HTTP fetch timeout.\n", getprogname());
1450 write(fileno(ttyout), msgbuf, len);
1452 siglongjmp(httpabort, 1);
1456 * Retrieve ftp URL or classic ftp argument using FTP.
1457 * Returns 1 on failure, 0 on completed xfer, -1 if ftp connection
1458 * is still open (e.g, ftp xfer with trailing /)
1461 fetch_ftp(const char *url)
1463 char *cp, *xargv[5], rempath[MAXPATHLEN];
1464 char *host, *path, *dir, *file, *uuser, *pass;
1466 char cmdbuf[MAXPATHLEN];
1468 int dirhasglob, filehasglob, rval, transtype, xargc;
1469 int oanonftp, oautologin;
1473 DPRINTF("fetch_ftp: `%s'\n", url);
1474 host = path = dir = file = uuser = pass = NULL;
1479 if (STRNEQUAL(url, FTP_URL)) {
1480 if ((parse_url(url, "URL", &urltype, &uuser, &pass,
1481 &host, &port, &portnum, &path) == -1) ||
1482 (uuser != NULL && *uuser == '\0') ||
1483 EMPTYSTRING(host)) {
1484 warnx("Invalid URL `%s'", url);
1485 goto cleanup_fetch_ftp;
1488 * Note: Don't url_decode(path) here. We need to keep the
1489 * distinction between "/" and "%2F" until later.
1492 /* check for trailing ';type=[aid]' */
1493 if (! EMPTYSTRING(path) && (cp = strrchr(path, ';')) != NULL) {
1494 if (strcasecmp(cp, ";type=a") == 0)
1496 else if (strcasecmp(cp, ";type=i") == 0)
1498 else if (strcasecmp(cp, ";type=d") == 0) {
1500 "Directory listing via a URL is not supported");
1501 goto cleanup_fetch_ftp;
1503 warnx("Invalid suffix `%s' in URL `%s'", cp,
1505 goto cleanup_fetch_ftp;
1509 } else { /* classic style `[user@]host:[file]' */
1510 urltype = CLASSIC_URL_T;
1511 host = ftp_strdup(url);
1512 cp = strchr(host, '@');
1516 anonftp = 0; /* disable anonftp */
1517 host = ftp_strdup(cp + 1);
1519 cp = strchr(host, ':');
1522 path = ftp_strdup(cp + 1);
1525 if (EMPTYSTRING(host))
1526 goto cleanup_fetch_ftp;
1528 /* Extract the file and (if present) directory name. */
1530 if (! EMPTYSTRING(dir)) {
1532 * If we are dealing with classic `[user@]host:[path]' syntax,
1533 * then a path of the form `/file' (resulting from input of the
1534 * form `host:/file') means that we should do "CWD /" before
1535 * retrieving the file. So we set dir="/" and file="file".
1537 * But if we are dealing with URLs like `ftp://host/path' then
1538 * a path of the form `/file' (resulting from a URL of the form
1539 * `ftp://host//file') means that we should do `CWD ' (with an
1540 * empty argument) before retrieving the file. So we set
1541 * dir="" and file="file".
1543 * If the path does not contain / at all, we set dir=NULL.
1544 * (We get a path without any slashes if we are dealing with
1545 * classic `[user@]host:[file]' or URL `ftp://host/file'.)
1547 * In all other cases, we set dir to a string that does not
1548 * include the final '/' that separates the dir part from the
1549 * file part of the path. (This will be the empty string if
1550 * and only if we are dealing with a path of the form `/file'
1551 * resulting from an URL of the form `ftp://host//file'.)
1553 cp = strrchr(dir, '/');
1554 if (cp == dir && urltype == CLASSIC_URL_T) {
1556 (void)strlcpy(dirbuf, "/", sizeof(dirbuf));
1558 } else if (cp != NULL) {
1567 if (urltype == FTP_URL_T && file != NULL) {
1569 /* but still don't url_decode(dir) */
1571 DPRINTF("fetch_ftp: user `%s' pass `%s' host %s port %s "
1572 "path `%s' dir `%s' file `%s'\n",
1573 STRorNULL(uuser), STRorNULL(pass),
1574 STRorNULL(host), STRorNULL(port),
1575 STRorNULL(path), STRorNULL(dir), STRorNULL(file));
1577 dirhasglob = filehasglob = 0;
1578 if (doglob && urltype == CLASSIC_URL_T) {
1579 if (! EMPTYSTRING(dir) && strpbrk(dir, "*?[]{}") != NULL)
1581 if (! EMPTYSTRING(file) && strpbrk(file, "*?[]{}") != NULL)
1585 /* Set up the connection */
1588 disconnect(0, NULL);
1590 (void)strlcpy(cmdbuf, getprogname(), sizeof(cmdbuf));
1600 oautologin = autologin;
1601 /* don't autologin in setpeer(), use ftp_login() below */
1603 setpeer(xargc, xargv);
1604 autologin = oautologin;
1605 if ((connected == 0) ||
1606 (connected == 1 && !ftp_login(host, uuser, pass))) {
1607 warnx("Can't connect or login to host `%s:%s'",
1608 host, port ? port : "?");
1609 goto cleanup_fetch_ftp;
1612 switch (transtype) {
1617 setbinary(1, xargv);
1620 errx(1, "fetch_ftp: unknown transfer type %d", transtype);
1624 * Change directories, if necessary.
1626 * Note: don't use EMPTYSTRING(dir) below, because
1627 * dir=="" means something different from dir==NULL.
1629 if (dir != NULL && !dirhasglob) {
1633 * If we are dealing with a classic `[user@]host:[path]'
1634 * (urltype is CLASSIC_URL_T) then we have a raw directory
1635 * name (not encoded in any way) and we can change
1636 * directories in one step.
1638 * If we are dealing with an `ftp://host/path' URL
1639 * (urltype is FTP_URL_T), then RFC 3986 says we need to
1640 * send a separate CWD command for each unescaped "/"
1641 * in the path, and we have to interpret %hex escaping
1642 * *after* we find the slashes. It's possible to get
1643 * empty components here, (from multiple adjacent
1644 * slashes in the path) and RFC 3986 says that we should
1645 * still do `CWD ' (with a null argument) in such cases.
1647 * Many ftp servers don't support `CWD ', so if there's an
1648 * error performing that command, bail out with a descriptive
1653 * host: dir="", urltype=CLASSIC_URL_T
1654 * logged in (to default directory)
1655 * host:file dir=NULL, urltype=CLASSIC_URL_T
1657 * host:dir/ dir="dir", urltype=CLASSIC_URL_T
1658 * "CWD dir", logged in
1659 * ftp://host/ dir="", urltype=FTP_URL_T
1660 * logged in (to default directory)
1661 * ftp://host/dir/ dir="dir", urltype=FTP_URL_T
1662 * "CWD dir", logged in
1663 * ftp://host/file dir=NULL, urltype=FTP_URL_T
1665 * ftp://host//file dir="", urltype=FTP_URL_T
1666 * "CWD ", "RETR file"
1667 * host:/file dir="/", urltype=CLASSIC_URL_T
1668 * "CWD /", "RETR file"
1669 * ftp://host///file dir="/", urltype=FTP_URL_T
1670 * "CWD ", "CWD ", "RETR file"
1671 * ftp://host/%2F/file dir="%2F", urltype=FTP_URL_T
1672 * "CWD /", "RETR file"
1673 * ftp://host/foo/file dir="foo", urltype=FTP_URL_T
1674 * "CWD foo", "RETR file"
1675 * ftp://host/foo/bar/file dir="foo/bar"
1676 * "CWD foo", "CWD bar", "RETR file"
1677 * ftp://host//foo/bar/file dir="/foo/bar"
1678 * "CWD ", "CWD foo", "CWD bar", "RETR file"
1679 * ftp://host/foo//bar/file dir="foo//bar"
1680 * "CWD foo", "CWD ", "CWD bar", "RETR file"
1681 * ftp://host/%2F/foo/bar/file dir="%2F/foo/bar"
1682 * "CWD /", "CWD foo", "CWD bar", "RETR file"
1683 * ftp://host/%2Ffoo/bar/file dir="%2Ffoo/bar"
1684 * "CWD /foo", "CWD bar", "RETR file"
1685 * ftp://host/%2Ffoo%2Fbar/file dir="%2Ffoo%2Fbar"
1686 * "CWD /foo/bar", "RETR file"
1687 * ftp://host/%2Ffoo%2Fbar%2Ffile dir=NULL
1688 * "RETR /foo/bar/file"
1690 * Note that we don't need `dir' after this point.
1693 if (urltype == FTP_URL_T) {
1694 nextpart = strchr(dir, '/');
1702 DPRINTF("fetch_ftp: dir `%s', nextpart `%s'\n",
1703 STRorNULL(dir), STRorNULL(nextpart));
1704 if (urltype == FTP_URL_T || *dir != '\0') {
1705 (void)strlcpy(cmdbuf, "cd", sizeof(cmdbuf));
1712 if (*dir == '\0' && code == 500)
1715 "ftp: The `CWD ' command (without a directory), which is required by\n"
1716 " RFC 3986 to support the empty directory in the URL pathname (`//'),\n"
1717 " conflicts with the server's conformance to RFC 959.\n"
1718 " Try the same URL without the `//' in the URL pathname.\n"
1720 goto cleanup_fetch_ftp;
1724 } while (dir != NULL);
1727 if (EMPTYSTRING(file)) {
1729 goto cleanup_fetch_ftp;
1733 (void)strlcpy(rempath, dir, sizeof(rempath));
1734 (void)strlcat(rempath, "/", sizeof(rempath));
1735 (void)strlcat(rempath, file, sizeof(rempath));
1739 /* Fetch the file(s). */
1741 (void)strlcpy(cmdbuf, "get", sizeof(cmdbuf));
1745 if (dirhasglob || filehasglob) {
1748 ointeractive = interactive;
1750 if (restartautofetch)
1751 (void)strlcpy(cmdbuf, "mreget", sizeof(cmdbuf));
1753 (void)strlcpy(cmdbuf, "mget", sizeof(cmdbuf));
1756 interactive = ointeractive;
1758 if (outfile == NULL) {
1759 cp = strrchr(file, '/'); /* find savefile */
1765 xargv[2] = (char *)outfile;
1768 if (restartautofetch)
1769 reget(xargc, xargv);
1774 if ((code / 100) == COMPLETE)
1783 memset(pass, 0, strlen(pass));
1789 * Retrieve the given file to outfile.
1790 * Supports arguments of the form:
1791 * "host:path", "ftp://host/path" if $ftpproxy, call fetch_url() else
1793 * "http://host/path" call fetch_url() to use HTTP
1794 * "file:///path" call fetch_url() to copy
1795 * "about:..." print a message
1797 * Returns 1 on failure, 0 on completed xfer, -1 if ftp connection
1798 * is still open (e.g, ftp xfer with trailing /)
1801 go_fetch(const char *url)
1810 if (STRNEQUAL(url, ABOUT_URL)) {
1811 url += sizeof(ABOUT_URL) -1;
1812 if (strcasecmp(url, "ftp") == 0 ||
1813 strcasecmp(url, "tnftp") == 0) {
1815 "This version of ftp has been enhanced by Luke Mewburn <lukem@NetBSD.org>\n"
1816 "for the NetBSD project. Execute `man ftp' for more details.\n", ttyout);
1817 } else if (strcasecmp(url, "lukem") == 0) {
1819 "Luke Mewburn is the author of most of the enhancements in this ftp client.\n"
1820 "Please email feedback to <lukem@NetBSD.org>.\n", ttyout);
1821 } else if (strcasecmp(url, "netbsd") == 0) {
1823 "NetBSD is a freely available and redistributable UNIX-like operating system.\n"
1824 "For more information, see http://www.NetBSD.org/\n", ttyout);
1825 } else if (strcasecmp(url, "version") == 0) {
1826 fprintf(ttyout, "Version: %s %s%s\n",
1827 FTP_PRODUCT, FTP_VERSION,
1835 fprintf(ttyout, "`%s' is an interesting topic.\n", url);
1837 fputs("\n", ttyout);
1843 * Check for file:// and http:// URLs.
1845 if (STRNEQUAL(url, HTTP_URL)
1847 || STRNEQUAL(url, HTTPS_URL)
1849 || STRNEQUAL(url, FILE_URL))
1850 return (fetch_url(url, NULL, NULL, NULL));
1853 * If it contains "://" but does not begin with ftp://
1854 * or something that was already handled, then it's
1857 * If it contains ":" but not "://" then we assume the
1858 * part before the colon is a host name, not an URL scheme,
1859 * so we don't try to match that here.
1861 if ((p = strstr(url, "://")) != NULL && ! STRNEQUAL(url, FTP_URL))
1862 errx(1, "Unsupported URL scheme `%.*s'", (int)(p - url), url);
1865 * Try FTP URL-style and host:file arguments next.
1866 * If ftpproxy is set with an FTP URL, use fetch_url()
1867 * Othewise, use fetch_ftp().
1869 proxyenv = getoptionvalue("ftp_proxy");
1870 if (!EMPTYSTRING(proxyenv) && STRNEQUAL(url, FTP_URL))
1871 return (fetch_url(url, NULL, NULL, NULL));
1873 return (fetch_ftp(url));
1877 * Retrieve multiple files from the command line,
1878 * calling go_fetch() for each file.
1880 * If an ftp path has a trailing "/", the path will be cd-ed into and
1881 * the connection remains open, and the function will return -1
1882 * (to indicate the connection is alive).
1883 * If an error occurs the return value will be the offset+1 in
1884 * argv[] of the file that caused a problem (i.e, argv[x]
1886 * Otherwise, 0 is returned if all files retrieved successfully.
1889 auto_fetch(int argc, char *argv[])
1891 volatile int argpos, rval;
1895 if (sigsetjmp(toplevel, 1)) {
1897 disconnect(0, NULL);
1902 (void)xsignal(SIGINT, intr);
1903 (void)xsignal(SIGPIPE, lostpeer);
1906 * Loop through as long as there's files to fetch.
1908 for (; (rval == 0) && (argpos < argc); argpos++) {
1909 if (strchr(argv[argpos], ':') == NULL)
1913 anonftp = 2; /* Handle "automatic" transfers. */
1914 rval = go_fetch(argv[argpos]);
1915 if (outfile != NULL && strcmp(outfile, "-") != 0
1916 && outfile[0] != '|')
1922 if (connected && rval != -1)
1923 disconnect(0, NULL);
1929 * Upload multiple files from the command line.
1931 * If an error occurs the return value will be the offset+1 in
1932 * argv[] of the file that caused a problem (i.e, argv[x]
1934 * Otherwise, 0 is returned if all files uploaded successfully.
1937 auto_put(int argc, char **argv, const char *uploadserver)
1939 char *uargv[4], *path, *pathsep;
1940 int uargc, rval, argpos;
1942 char cmdbuf[MAX_C_NAME];
1944 (void)strlcpy(cmdbuf, "mput", sizeof(cmdbuf));
1948 uargv[2] = uargv[3] = NULL;
1952 DPRINTF("auto_put: target `%s'\n", uploadserver);
1954 path = ftp_strdup(uploadserver);
1956 if (path[len - 1] != '/' && path[len - 1] != ':') {
1958 * make sure we always pass a directory to auto_fetch
1960 if (argc > 1) { /* more than one file to upload */
1961 len = strlen(uploadserver) + 2; /* path + "/" + "\0" */
1963 path = (char *)ftp_malloc(len);
1964 (void)strlcpy(path, uploadserver, len);
1965 (void)strlcat(path, "/", len);
1966 } else { /* single file to upload */
1967 (void)strlcpy(cmdbuf, "put", sizeof(cmdbuf));
1969 pathsep = strrchr(path, '/');
1970 if (pathsep == NULL) {
1971 pathsep = strrchr(path, ':');
1972 if (pathsep == NULL) {
1973 warnx("Invalid URL `%s'", path);
1974 goto cleanup_auto_put;
1977 uargv[2] = ftp_strdup(pathsep);
1980 uargv[2] = ftp_strdup(pathsep + 1);
1985 DPRINTF("auto_put: URL `%s' argv[2] `%s'\n",
1986 path, STRorNULL(uargv[2]));
1988 /* connect and cwd */
1989 rval = auto_fetch(1, &path);
1991 goto cleanup_auto_put;
1995 /* target filename provided; upload 1 file */
1996 /* XXX : is this the best way? */
2000 if ((code / 100) != COMPLETE)
2002 } else { /* otherwise a target dir: upload all files to it */
2003 for(argpos = 0; argv[argpos] != NULL; argpos++) {
2004 uargv[1] = argv[argpos];
2006 if ((code / 100) != COMPLETE) {