d0f40d3f99f34ef1f45c27a7f0bf8af3a91f77f9
[dragonfly.git] / contrib / tnftp / src / fetch.c
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     */
3
4 /*-
5  * Copyright (c) 1997-2015 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Luke Mewburn.
10  *
11  * This code is derived from software contributed to The NetBSD Foundation
12  * by Scott Aaron Bamford.
13  *
14  * This code is derived from software contributed to The NetBSD Foundation
15  * by Thomas Klausner.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
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.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38
39 #include "tnftp.h"
40
41 #if 0   /* tnftp */
42
43 #include <sys/cdefs.h>
44 #ifndef lint
45 __RCSID(" NetBSD: fetch.c,v 1.207 2015/09/12 19:38:42 wiz Exp  ");
46 #endif /* not lint */
47
48 /*
49  * FTP User Program -- Command line file retrieval
50  */
51
52 #include <sys/types.h>
53 #include <sys/param.h>
54 #include <sys/socket.h>
55 #include <sys/stat.h>
56 #include <sys/time.h>
57
58 #include <netinet/in.h>
59
60 #include <arpa/ftp.h>
61 #include <arpa/inet.h>
62
63 #include <assert.h>
64 #include <ctype.h>
65 #include <err.h>
66 #include <errno.h>
67 #include <netdb.h>
68 #include <fcntl.h>
69 #include <stdio.h>
70 #include <stdlib.h>
71 #include <string.h>
72 #include <unistd.h>
73 #include <time.h>
74
75 #endif  /* tnftp */
76
77 #include "ssl.h"
78 #include "ftp_var.h"
79 #include "version.h"
80
81 typedef enum {
82         UNKNOWN_URL_T=-1,
83         HTTP_URL_T,
84 #ifdef WITH_SSL
85         HTTPS_URL_T,
86 #endif
87         FTP_URL_T,
88         FILE_URL_T,
89         CLASSIC_URL_T
90 } url_t;
91
92 __dead static void      aborthttp(int);
93 __dead static void      timeouthttp(int);
94 #ifndef NO_AUTH
95 static int      auth_url(const char *, char **, const char *, const char *);
96 static void     base64_encode(const unsigned char *, size_t, unsigned char *);
97 #endif
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 *);
105
106 static int      redirect_loop;
107
108
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)
112
113
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 */
118 #ifdef WITH_SSL
119 #define HTTPS_URL       "https://"      /* https URL prefix */
120
121 #define IS_HTTP_TYPE(urltype) \
122         (((urltype) == HTTP_URL_T) || ((urltype) == HTTPS_URL_T))
123 #else
124 #define IS_HTTP_TYPE(urltype) \
125         ((urltype) == HTTP_URL_T)
126 #endif
127
128 /*
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).
134  */
135 static const char *
136 match_token(const char **buf, const char *token)
137 {
138         const char      *p, *orig;
139         size_t          tlen;
140
141         tlen = strlen(token);
142         p = *buf;
143         SKIPLWS(p);
144         orig = p;
145         if (strncasecmp(p, token, tlen) != 0)
146                 return NULL;
147         p += tlen;
148         if (*p != '\0' && !ISLWS(*p))
149                 return NULL;
150         SKIPLWS(p);
151         orig = *buf;
152         *buf = p;
153         return orig;
154 }
155
156 #ifndef NO_AUTH
157 /*
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.
161  */
162 static int
163 auth_url(const char *challenge, char **response, const char *guser,
164         const char *gpass)
165 {
166         const char      *cp, *scheme, *errormsg;
167         char            *ep, *clear, *realm;
168         char             uuser[BUFSIZ], *gotpass;
169         const char      *upass;
170         int              rval;
171         size_t           len, clen, rlen;
172
173         *response = NULL;
174         clear = realm = NULL;
175         rval = -1;
176         cp = challenge;
177         scheme = "Basic";       /* only support Basic authentication */
178         gotpass = NULL;
179
180         DPRINTF("auth_url: challenge `%s'\n", challenge);
181
182         if (! match_token(&cp, scheme)) {
183                 warnx("Unsupported authentication challenge `%s'",
184                     challenge);
185                 goto cleanup_auth_url;
186         }
187
188 #define REALM "realm=\""
189         if (STRNEQUAL(cp, REALM))
190                 cp += sizeof(REALM) - 1;
191         else {
192                 warnx("Unsupported authentication challenge `%s'",
193                     challenge);
194                 goto cleanup_auth_url;
195         }
196 /* XXX: need to improve quoted-string parsing to support \ quoting, etc. */
197         if ((ep = strchr(cp, '\"')) != NULL) {
198                 len = ep - cp;
199                 realm = (char *)ftp_malloc(len + 1);
200                 (void)strlcpy(realm, cp, len + 1);
201         } else {
202                 warnx("Unsupported authentication challenge `%s'",
203                     challenge);
204                 goto cleanup_auth_url;
205         }
206
207         fprintf(ttyout, "Username for `%s': ", realm);
208         if (guser != NULL) {
209                 (void)strlcpy(uuser, guser, sizeof(uuser));
210                 fprintf(ttyout, "%s\n", uuser);
211         } else {
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;
216                 }
217         }
218         if (gpass != NULL)
219                 upass = gpass;
220         else {
221                 gotpass = getpass("Password: ");
222                 if (gotpass == NULL) {
223                         warnx("Can't read password");
224                         goto cleanup_auth_url;
225                 }
226                 upass = gotpass;
227         }
228
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);
234         if (gotpass)
235                 memset(gotpass, 0, strlen(gotpass));
236
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);
246         rval = 0;
247
248  cleanup_auth_url:
249         FREEPTR(clear);
250         FREEPTR(realm);
251         return (rval);
252 }
253
254 /*
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.
257  */
258 static void
259 base64_encode(const unsigned char *clear, size_t len, unsigned char *encoded)
260 {
261         static const unsigned char enc[] =
262             "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
263         unsigned char   *cp;
264         size_t   i;
265
266         cp = encoded;
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)];
274         }
275         *cp = '\0';
276         while (i-- > len)
277                 *(--cp) = '=';
278 }
279 #endif
280
281 /*
282  * Decode %xx escapes in given string, `in-place'.
283  */
284 static void
285 url_decode(char *url)
286 {
287         unsigned char *p, *q;
288
289         if (EMPTYSTRING(url))
290                 return;
291         p = q = (unsigned char *)url;
292
293 #define HEXTOINT(x) (x - (isdigit(x) ? '0' : (islower(x) ? 'a' : 'A') - 10))
294         while (*p) {
295                 if (p[0] == '%'
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]);
299                         p+=3;
300                 } else
301                         *q++ = *p++;
302         }
303         *q = '\0';
304 }
305
306
307 /*
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.
313  *
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://.
317  *
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).
324  *
325  * Examples:
326  *       input URL                       output path
327  *       ---------                       -----------
328  *      "http://host"                   "/"
329  *      "http://host/"                  "/"
330  *      "http://host/path"              "/path"
331  *      "file://host/dir/file"          "dir/file"
332  *      "ftp://host"                    ""
333  *      "ftp://host/"                   ""
334  *      "ftp://host//"                  "/"
335  *      "ftp://host/dir/file"           "dir/file"
336  *      "ftp://host//dir/file"          "/dir/file"
337  */
338 static int
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)
342 {
343         const char      *origurl, *tport;
344         char            *cp, *ep, *thost;
345         size_t           len;
346
347         if (url == NULL || desc == NULL || utype == NULL || uuser == NULL
348             || pass == NULL || host == NULL || port == NULL || portnum == NULL
349             || path == NULL)
350                 errx(1, "parse_url: invoked with NULL argument!");
351         DPRINTF("parse_url: %s `%s'\n", desc, url);
352
353         origurl = url;
354         *utype = UNKNOWN_URL_T;
355         *uuser = *pass = *host = *port = *path = NULL;
356         *portnum = 0;
357         tport = NULL;
358
359         if (STRNEQUAL(url, HTTP_URL)) {
360                 url += sizeof(HTTP_URL) - 1;
361                 *utype = HTTP_URL_T;
362                 *portnum = HTTP_PORT;
363                 tport = httpport;
364         } else if (STRNEQUAL(url, FTP_URL)) {
365                 url += sizeof(FTP_URL) - 1;
366                 *utype = FTP_URL_T;
367                 *portnum = FTP_PORT;
368                 tport = ftpport;
369         } else if (STRNEQUAL(url, FILE_URL)) {
370                 url += sizeof(FILE_URL) - 1;
371                 *utype = FILE_URL_T;
372 #ifdef WITH_SSL
373         } else if (STRNEQUAL(url, HTTPS_URL)) {
374                 url += sizeof(HTTPS_URL) - 1;
375                 *utype = HTTPS_URL_T;
376                 *portnum = HTTPS_PORT;
377                 tport = httpsport;
378 #endif
379         } else {
380                 warnx("Invalid %s `%s'", desc, url);
381  cleanup_parse_url:
382                 FREEPTR(*uuser);
383                 if (*pass != NULL)
384                         memset(*pass, 0, strlen(*pass));
385                 FREEPTR(*pass);
386                 FREEPTR(*host);
387                 FREEPTR(*port);
388                 FREEPTR(*path);
389                 return (-1);
390         }
391
392         if (*url == '\0')
393                 return (0);
394
395                         /* find [user[:pass]@]host[:port] */
396         ep = strchr(url, '/');
397         if (ep == NULL)
398                 thost = ftp_strdup(url);
399         else {
400                 len = ep - 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 */
404                         ep++;
405                 *path = ftp_strdup(ep);
406         }
407
408         cp = strchr(thost, '@');        /* look for user[:pass]@ in URLs */
409         if (cp != NULL) {
410                 if (*utype == FTP_URL_T)
411                         anonftp = 0;    /* disable anonftp */
412                 *uuser = thost;
413                 *cp = '\0';
414                 thost = ftp_strdup(cp + 1);
415                 cp = strchr(*uuser, ':');
416                 if (cp != NULL) {
417                         *cp = '\0';
418                         *pass = ftp_strdup(cp + 1);
419                 }
420                 url_decode(*uuser);
421                 if (*pass)
422                         url_decode(*pass);
423         }
424
425 #ifdef INET6
426                         /*
427                          * Check if thost is an encoded IPv6 address, as per
428                          * RFC 3986:
429                          *      `[' ipv6-address ']'
430                          */
431         if (*thost == '[') {
432                 cp = thost + 1;
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;
438                 }
439                 len = ep - cp;          /* change `[xyz]' -> `xyz' */
440                 memmove(thost, thost + 1, len);
441                 thost[len] = '\0';
442                 if (! isipv6addr(thost)) {
443                         warnx("Invalid IPv6 address `%s' in %s `%s'",
444                             thost, desc, origurl);
445                         goto cleanup_parse_url;
446                 }
447                 cp = ep + 1;
448                 if (*cp == ':')
449                         cp++;
450                 else
451                         cp = NULL;
452         } else
453 #endif /* INET6 */
454                 if ((cp = strchr(thost, ':')) != NULL)
455                         *cp++ = '\0';
456         *host = thost;
457
458                         /* look for [:port] */
459         if (cp != NULL) {
460                 unsigned long   nport;
461
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'",
466                             cp, desc, origurl);
467                         goto cleanup_parse_url;
468                 }
469                 *portnum = nport;
470                 tport = cp;
471         }
472
473         if (tport != NULL)
474                 *port = ftp_strdup(tport);
475         if (*path == NULL) {
476                 const char *emptypath = "/";
477                 if (*utype == FTP_URL_T)        /* skip first / for ftp URLs */
478                         emptypath++;
479                 *path = ftp_strdup(emptypath);
480         }
481
482         DPRINTF("parse_url: user `%s' pass `%s' host %s port %s(%d) "
483             "path `%s'\n",
484             STRorNULL(*uuser), STRorNULL(*pass),
485             STRorNULL(*host), STRorNULL(*port),
486             *portnum ? *portnum : -1, STRorNULL(*path));
487
488         return (0);
489 }
490
491 sigjmp_buf      httpabort;
492
493 /*
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 /)
500  */
501 static int
502 fetch_url(const char *url, const char *proxyenv, char *proxyauth, char *wwwauth)
503 {
504         struct addrinfo         hints, *res, *res0 = NULL;
505         int                     error;
506         sigfunc volatile        oldint;
507         sigfunc volatile        oldpipe;
508         sigfunc volatile        oldalrm;
509         sigfunc volatile        oldquit;
510         int volatile            s;
511         struct stat             sb;
512         int volatile            ischunked;
513         int volatile            isproxy;
514         int volatile            rval;
515         int volatile            hcode;
516         int                     len;
517         size_t                  flen;
518         static size_t           bufsize;
519         static char             *xferbuf;
520         const char              *cp, *token;
521         char                    *ep;
522         char                    buf[FTPBUFLEN];
523         const char              *errormsg;
524         char                    *volatile savefile;
525         char                    *volatile auth;
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 *);
533         FETCH                   *volatile fin;
534         FILE                    *volatile fout;
535         const char              *volatile penv = proxyenv;
536         time_t                  mtime;
537         url_t                   urltype;
538         in_port_t               portnum;
539 #ifdef WITH_SSL
540         void                    *ssl;
541 #endif
542
543         DPRINTF("%s: `%s' proxyenv `%s'\n", __func__, url, STRorNULL(penv));
544
545         oldquit = oldalrm = oldint = oldpipe = NULL;
546         closefunc = NULL;
547         fin = NULL;
548         fout = NULL;
549         s = -1;
550         savefile = NULL;
551         auth = location = message = NULL;
552         ischunked = isproxy = hcode = 0;
553         rval = 1;
554         uuser = pass = host = path = decodedpath = puser = ppass = NULL;
555
556         if (sigsetjmp(httpabort, 1))
557                 goto cleanup_fetch_url;
558
559         if (parse_url(url, "URL", &urltype, &uuser, &pass, &host, &port,
560             &portnum, &path) == -1)
561                 goto cleanup_fetch_url;
562
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;
567         }
568
569         if (EMPTYSTRING(path)) {
570                 if (urltype == FTP_URL_T) {
571                         rval = fetch_ftp(url);
572                         goto cleanup_fetch_url;
573                 }
574                 if (!IS_HTTP_TYPE(urltype) || outfile == NULL)  {
575                         warnx("Invalid URL (no file after host) `%s'", url);
576                         goto cleanup_fetch_url;
577                 }
578         }
579
580         decodedpath = ftp_strdup(path);
581         url_decode(decodedpath);
582
583         if (outfile)
584                 savefile = outfile;
585         else {
586                 cp = strrchr(decodedpath, '/');         /* find savefile */
587                 if (cp != NULL)
588                         savefile = ftp_strdup(cp + 1);
589                 else
590                         savefile = ftp_strdup(decodedpath);
591         }
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;
597                 }
598                 warnx("No file after directory (you must specify an "
599                     "output file) `%s'", url);
600                 goto cleanup_fetch_url;
601         }
602
603         restart_point = 0;
604         filesize = -1;
605         rangestart = rangeend = entitylen = -1;
606         mtime = -1;
607         if (restartautofetch) {
608                 if (stat(savefile, &sb) == 0)
609                         restart_point = sb.st_size;
610         }
611         if (urltype == FILE_URL_T) {            /* file:// URLs */
612                 direction = "copied";
613                 fin = fetch_open(decodedpath, "r");
614                 if (fin == NULL) {
615                         warn("Can't open `%s'", decodedpath);
616                         goto cleanup_fetch_url;
617                 }
618                 if (fstat(fetch_fileno(fin), &sb) == 0) {
619                         mtime = sb.st_mtime;
620                         filesize = sb.st_size;
621                 }
622                 if (restart_point) {
623                         if (lseek(fetch_fileno(fin), restart_point, SEEK_SET) < 0) {
624                                 warn("Can't seek to restart `%s'",
625                                     decodedpath);
626                                 goto cleanup_fetch_url;
627                         }
628                 }
629                 if (verbose) {
630                         fprintf(ttyout, "Copying %s", decodedpath);
631                         if (restart_point)
632                                 fprintf(ttyout, " (restarting at " LLF ")",
633                                     (LLT)restart_point);
634                         fputs("\n", ttyout);
635                 }
636                 if (0 == rcvbuf_size) {
637                         rcvbuf_size = 8 * 1024; /* XXX */
638                 }
639         } else {                                /* ftp:// or http:// URLs */
640                 const char *leading;
641                 int hasleading;
642
643                 if (penv == NULL) {
644 #ifdef WITH_SSL
645                         if (urltype == HTTPS_URL_T)
646                                 penv = getoptionvalue("https_proxy");
647 #endif
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");
652                 }
653                 direction = "retrieved";
654                 if (! EMPTYSTRING(penv)) {                      /* use proxy */
655                         url_t purltype;
656                         char *phost, *ppath;
657                         char *pport, *no_proxy;
658                         in_port_t pportnum;
659
660                         isproxy = 1;
661
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;
667                                 size_t hlen, plen;
668
669                                 np_iter = np_copy = ftp_strdup(no_proxy);
670                                 hlen = strlen(host);
671                                 while ((cp = strsep(&np_iter, " ,")) != NULL) {
672                                         if (*cp == '\0')
673                                                 continue;
674                                         if ((np = strrchr(cp, ':')) != NULL) {
675                                                 *np++ =  '\0';
676                                                 np_port = strtoul(np, &ep, 10);
677                                                 if (*np == '\0' || *ep != '\0')
678                                                         continue;
679                                                 if (np_port != portnum)
680                                                         continue;
681                                         }
682                                         plen = strlen(cp);
683                                         if (hlen < plen)
684                                                 continue;
685                                         if (strncasecmp(host + hlen - plen,
686                                             cp, plen) == 0) {
687                                                 isproxy = 0;
688                                                 break;
689                                         }
690                                 }
691                                 FREEPTR(np_copy);
692                                 if (isproxy == 0 && urltype == FTP_URL_T) {
693                                         rval = fetch_ftp(url);
694                                         goto cleanup_fetch_url;
695                                 }
696                         }
697
698                         if (isproxy) {
699                                 if (restart_point) {
700                                         warnx("Can't restart via proxy URL `%s'",
701                                             penv);
702                                         goto cleanup_fetch_url;
703                                 }
704                                 if (parse_url(penv, "proxy URL", &purltype,
705                                     &puser, &ppass, &phost, &pport, &pportnum,
706                                     &ppath) == -1)
707                                         goto cleanup_fetch_url;
708
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);
715                                         FREEPTR(phost);
716                                         FREEPTR(pport);
717                                         FREEPTR(ppath);
718                                         goto cleanup_fetch_url;
719                                 }
720                                 if (isipv6addr(host) &&
721                                     strchr(host, '%') != NULL) {
722                                         warnx(
723 "Scoped address notation `%s' disallowed via web proxy",
724                                             host);
725                                         FREEPTR(phost);
726                                         FREEPTR(pport);
727                                         FREEPTR(ppath);
728                                         goto cleanup_fetch_url;
729                                 }
730
731                                 FREEPTR(host);
732                                 host = phost;
733                                 FREEPTR(port);
734                                 port = pport;
735                                 FREEPTR(path);
736                                 path = ftp_strdup(url);
737                                 FREEPTR(ppath);
738                                 urltype = purltype;
739                         }
740                 } /* ! EMPTYSTRING(penv) */
741
742                 memset(&hints, 0, sizeof(hints));
743                 hints.ai_flags = 0;
744                 hints.ai_family = family;
745                 hints.ai_socktype = SOCK_STREAM;
746                 hints.ai_protocol = 0;
747                 error = getaddrinfo(host, port, &hints, &res0);
748                 if (error) {
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;
753                 }
754                 if (res0->ai_canonname)
755                         host = res0->ai_canonname;
756
757                 s = -1;
758 #ifdef WITH_SSL
759                 ssl = NULL;
760 #endif
761                 for (res = res0; res; res = res->ai_next) {
762                         char    hname[NI_MAXHOST], sname[NI_MAXSERV];
763
764                         ai_unmapped(res);
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));
770                         }
771
772                         if (verbose && res0->ai_next) {
773                                 fprintf(ttyout, "Trying %s:%s ...\n",
774                                     hname, sname);
775                         }
776
777                         s = socket(res->ai_family, SOCK_STREAM,
778                             res->ai_protocol);
779                         if (s < 0) {
780                                 warn(
781                                     "Can't create socket for connection to "
782                                     "`%s:%s'", hname, sname);
783                                 continue;
784                         }
785
786                         if (ftp_connect(s, res->ai_addr, res->ai_addrlen,
787                             verbose || !res->ai_next) < 0) {
788                                 close(s);
789                                 s = -1;
790                                 continue;
791                         }
792
793 #ifdef WITH_SSL
794                         if (urltype == HTTPS_URL_T) {
795                                 if ((ssl = fetch_start_ssl(s, host)) == NULL) {
796                                         close(s);
797                                         s = -1;
798                                         continue;
799                                 }
800                         }
801 #endif
802
803                         /* success */
804                         break;
805                 }
806
807                 if (s < 0) {
808                         warnx("Can't connect to `%s:%s'", host, port);
809                         goto cleanup_fetch_url;
810                 }
811
812                 oldalrm = xsignal(SIGALRM, timeouthttp);
813                 alarmtimer(quit_time ? quit_time : 60);
814                 fin = fetch_fdopen(s, "r+");
815                 fetch_set_ssl(fin, ssl);
816                 alarmtimer(0);
817
818                 alarmtimer(quit_time ? quit_time : 60);
819                 /*
820                  * Construct and send the request.
821                  */
822                 if (verbose)
823                         fprintf(ttyout, "Requesting %s\n", url);
824                 leading = "  (";
825                 hasleading = 0;
826                 if (isproxy) {
827                         if (verbose) {
828                                 fprintf(ttyout, "%svia %s:%s", leading,
829                                     host, port);
830                                 leading = ", ";
831                                 hasleading++;
832                         }
833                         fetch_printf(fin, "GET %s HTTP/1.0\r\n", path);
834                         if (flushcache)
835                                 fetch_printf(fin, "Pragma: no-cache\r\n");
836                 } else {
837                         fetch_printf(fin, "GET %s HTTP/1.1\r\n", path);
838                         if (strchr(host, ':')) {
839                                 char *h, *p;
840
841                                 /*
842                                  * strip off IPv6 scope identifier, since it is
843                                  * local to the node
844                                  */
845                                 h = ftp_strdup(host);
846                                 if (isipv6addr(h) &&
847                                     (p = strchr(h, '%')) != NULL) {
848                                         *p = '\0';
849                                 }
850                                 fetch_printf(fin, "Host: [%s]", h);
851                                 free(h);
852                         } else
853                                 fetch_printf(fin, "Host: %s", host);
854 #ifdef WITH_SSL
855                         if ((urltype == HTTP_URL_T && portnum != HTTP_PORT) ||
856                             (urltype == HTTPS_URL_T && portnum != HTTPS_PORT))
857 #else
858                         if (portnum != HTTP_PORT)
859 #endif
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");
864                         if (restart_point) {
865                                 fputs(leading, ttyout);
866                                 fetch_printf(fin, "Range: bytes=" LLF "-\r\n",
867                                     (LLT)restart_point);
868                                 fprintf(ttyout, "restarting at " LLF,
869                                     (LLT)restart_point);
870                                 leading = ", ";
871                                 hasleading++;
872                         }
873                         if (flushcache)
874                                 fetch_printf(fin, "Cache-Control: no-cache\r\n");
875                 }
876                 if ((useragent=getenv("FTPUSERAGENT")) != NULL) {
877                         fetch_printf(fin, "User-Agent: %s\r\n", useragent);
878                 } else {
879                         fetch_printf(fin, "User-Agent: %s/%s\r\n",
880                             FTP_PRODUCT, FTP_VERSION);
881                 }
882                 if (wwwauth) {
883                         if (verbose) {
884                                 fprintf(ttyout, "%swith authorization",
885                                     leading);
886                                 leading = ", ";
887                                 hasleading++;
888                         }
889                         fetch_printf(fin, "Authorization: %s\r\n", wwwauth);
890                 }
891                 if (proxyauth) {
892                         if (verbose) {
893                                 fprintf(ttyout,
894                                     "%swith proxy authorization", leading);
895                                 leading = ", ";
896                                 hasleading++;
897                         }
898                         fetch_printf(fin, "Proxy-Authorization: %s\r\n", proxyauth);
899                 }
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");
905                         alarmtimer(0);
906                         goto cleanup_fetch_url;
907                 }
908                 alarmtimer(0);
909
910                                 /* Read the response */
911                 alarmtimer(quit_time ? quit_time : 60);
912                 len = fetch_getline(fin, buf, sizeof(buf), &errormsg);
913                 alarmtimer(0);
914                 if (len < 0) {
915                         if (*errormsg == '\n')
916                                 errormsg++;
917                         warnx("Receiving HTTP reply: %s", errormsg);
918                         goto cleanup_fetch_url;
919                 }
920                 while (len > 0 && (ISLWS(buf[len-1])))
921                         buf[--len] = '\0';
922                 DPRINTF("%s: received `%s'\n", __func__, buf);
923
924                                 /* Determine HTTP response code */
925                 cp = strchr(buf, ' ');
926                 if (cp == NULL)
927                         goto improper;
928                 else
929                         cp++;
930                 hcode = strtol(cp, &ep, 10);
931                 if (*ep != '\0' && !isspace((unsigned char)*ep))
932                         goto improper;
933                 message = ftp_strdup(cp);
934
935                                 /* Read the rest of the header. */
936                 while (1) {
937                         alarmtimer(quit_time ? quit_time : 60);
938                         len = fetch_getline(fin, buf, sizeof(buf), &errormsg);
939                         alarmtimer(0);
940                         if (len < 0) {
941                                 if (*errormsg == '\n')
942                                         errormsg++;
943                                 warnx("Receiving HTTP reply: %s", errormsg);
944                                 goto cleanup_fetch_url;
945                         }
946                         while (len > 0 && (ISLWS(buf[len-1])))
947                                 buf[--len] = '\0';
948                         if (len == 0)
949                                 break;
950                         DPRINTF("%s: received `%s'\n", __func__, buf);
951
952                 /*
953                  * Look for some headers
954                  */
955
956                         cp = buf;
957
958                         if (match_token(&cp, "Content-Length:")) {
959                                 filesize = STRTOLL(cp, &ep, 10);
960                                 if (filesize < 0 || *ep != '\0')
961                                         goto improper;
962                                 DPRINTF("%s: parsed len as: " LLF "\n",
963                                     __func__, (LLT)filesize);
964
965                         } else if (match_token(&cp, "Content-Range:")) {
966                                 if (! match_token(&cp, "bytes"))
967                                         goto improper;
968
969                                 if (*cp == '*')
970                                         cp++;
971                                 else {
972                                         rangestart = STRTOLL(cp, &ep, 10);
973                                         if (rangestart < 0 || *ep != '-')
974                                                 goto improper;
975                                         cp = ep + 1;
976                                         rangeend = STRTOLL(cp, &ep, 10);
977                                         if (rangeend < 0 || rangeend < rangestart)
978                                                 goto improper;
979                                         cp = ep;
980                                 }
981                                 if (*cp != '/')
982                                         goto improper;
983                                 cp++;
984                                 if (*cp == '*')
985                                         cp++;
986                                 else {
987                                         entitylen = STRTOLL(cp, &ep, 10);
988                                         if (entitylen < 0)
989                                                 goto improper;
990                                         cp = ep;
991                                 }
992                                 if (*cp != '\0')
993                                         goto improper;
994
995 #ifndef NO_DEBUG
996                                 if (ftp_debug) {
997                                         fprintf(ttyout, "parsed range as: ");
998                                         if (rangestart == -1)
999                                                 fprintf(ttyout, "*");
1000                                         else
1001                                                 fprintf(ttyout, LLF "-" LLF,
1002                                                     (LLT)rangestart,
1003                                                     (LLT)rangeend);
1004                                         fprintf(ttyout, "/" LLF "\n", (LLT)entitylen);
1005                                 }
1006 #endif
1007                                 if (! restart_point) {
1008                                         warnx(
1009                                     "Received unexpected Content-Range header");
1010                                         goto cleanup_fetch_url;
1011                                 }
1012
1013                         } else if (match_token(&cp, "Last-Modified:")) {
1014                                 struct tm parsed;
1015                                 const char *t;
1016
1017                                 memset(&parsed, 0, sizeof(parsed));
1018                                 t = parse_rfc2616time(&parsed, cp);
1019                                 if (t != NULL) {
1020                                         parsed.tm_isdst = -1;
1021                                         if (*t == '\0')
1022                                                 mtime = timegm(&parsed);
1023 #ifndef NO_DEBUG
1024                                         if (ftp_debug && mtime != -1) {
1025                                                 fprintf(ttyout,
1026                                                     "parsed time as: %s",
1027                                                 rfc2822time(localtime(&mtime)));
1028                                         }
1029 #endif
1030                                 }
1031
1032                         } else if (match_token(&cp, "Location:")) {
1033                                 location = ftp_strdup(cp);
1034                                 DPRINTF("%s: parsed location as `%s'\n",
1035                                     __func__, cp);
1036
1037                         } else if (match_token(&cp, "Transfer-Encoding:")) {
1038                                 if (match_token(&cp, "binary")) {
1039                                         warnx(
1040                         "Bogus transfer encoding `binary' (fetching anyway)");
1041                                         continue;
1042                                 }
1043                                 if (! (token = match_token(&cp, "chunked"))) {
1044                                         warnx(
1045                                     "Unsupported transfer encoding `%s'",
1046                                             token);
1047                                         goto cleanup_fetch_url;
1048                                 }
1049                                 ischunked++;
1050                                 DPRINTF("%s: using chunked encoding\n",
1051                                     __func__);
1052
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);
1058                                         continue;
1059                                 }
1060                                 FREEPTR(auth);
1061                                 auth = ftp_strdup(token);
1062                                 DPRINTF("%s: parsed auth as `%s'\n",
1063                                     __func__, cp);
1064                         }
1065
1066                 }
1067                                 /* finished parsing header */
1068
1069                 switch (hcode) {
1070                 case 200:
1071                         break;
1072                 case 206:
1073                         if (! restart_point) {
1074                                 warnx("Not expecting partial content header");
1075                                 goto cleanup_fetch_url;
1076                         }
1077                         break;
1078                 case 300:
1079                 case 301:
1080                 case 302:
1081                 case 303:
1082                 case 305:
1083                 case 307:
1084                         if (EMPTYSTRING(location)) {
1085                                 warnx(
1086                                 "No redirection Location provided by server");
1087                                 goto cleanup_fetch_url;
1088                         }
1089                         if (redirect_loop++ > 5) {
1090                                 warnx("Too many redirections requested");
1091                                 goto cleanup_fetch_url;
1092                         }
1093                         if (hcode == 305) {
1094                                 if (verbose)
1095                                         fprintf(ttyout, "Redirected via %s\n",
1096                                             location);
1097                                 rval = fetch_url(url, location,
1098                                     proxyauth, wwwauth);
1099                         } else {
1100                                 if (verbose)
1101                                         fprintf(ttyout, "Redirected to %s\n",
1102                                             location);
1103                                 rval = go_fetch(location);
1104                         }
1105                         goto cleanup_fetch_url;
1106 #ifndef NO_AUTH
1107                 case 401:
1108                 case 407:
1109                     {
1110                         char **authp;
1111                         char *auser, *apass;
1112
1113                         if (hcode == 401) {
1114                                 authp = &wwwauth;
1115                                 auser = uuser;
1116                                 apass = pass;
1117                         } else {
1118                                 authp = &proxyauth;
1119                                 auser = puser;
1120                                 apass = ppass;
1121                         }
1122                         if (verbose || *authp == NULL ||
1123                             auser == NULL || apass == NULL)
1124                                 fprintf(ttyout, "%s\n", message);
1125                         if (EMPTYSTRING(auth)) {
1126                                 warnx(
1127                             "No authentication challenge provided by server");
1128                                 goto cleanup_fetch_url;
1129                         }
1130                         if (*authp != NULL) {
1131                                 char reply[10];
1132
1133                                 fprintf(ttyout,
1134                                     "Authorization failed. Retry (y/n)? ");
1135                                 if (get_line(stdin, reply, sizeof(reply), NULL)
1136                                     < 0) {
1137                                         goto cleanup_fetch_url;
1138                                 }
1139                                 if (tolower((unsigned char)reply[0]) != 'y')
1140                                         goto cleanup_fetch_url;
1141                                 auser = NULL;
1142                                 apass = NULL;
1143                         }
1144                         if (auth_url(auth, authp, auser, apass) == 0) {
1145                                 rval = fetch_url(url, penv,
1146                                     proxyauth, wwwauth);
1147                                 memset(*authp, 0, strlen(*authp));
1148                                 FREEPTR(*authp);
1149                         }
1150                         goto cleanup_fetch_url;
1151                     }
1152 #endif
1153                 default:
1154                         if (message)
1155                                 warnx("Error retrieving file `%s'", message);
1156                         else
1157                                 warnx("Unknown error retrieving file");
1158                         goto cleanup_fetch_url;
1159                 }
1160         }               /* end of ftp:// or http:// specific setup */
1161
1162         /* Open the output file. */
1163
1164         /*
1165          * Only trust filenames with special meaning if they came from
1166          * the command line
1167          */
1168         if (outfile == savefile) {
1169                 if (strcmp(savefile, "-") == 0) {
1170                         fout = stdout;
1171                 } else if (*savefile == '|') {
1172                         oldpipe = xsignal(SIGPIPE, SIG_IGN);
1173                         fout = popen(savefile + 1, "w");
1174                         if (fout == NULL) {
1175                                 warn("Can't execute `%s'", savefile + 1);
1176                                 goto cleanup_fetch_url;
1177                         }
1178                         closefunc = pclose;
1179                 }
1180         }
1181         if (fout == NULL) {
1182                 if ((rangeend != -1 && rangeend <= restart_point) ||
1183                     (rangestart == -1 && filesize != -1 && filesize <= restart_point)) {
1184                         /* already done */
1185                         if (verbose)
1186                                 fprintf(ttyout, "already done\n");
1187                         rval = 0;
1188                         goto cleanup_fetch_url;
1189                 }
1190                 if (restart_point && rangestart != -1) {
1191                         if (entitylen != -1)
1192                                 filesize = entitylen;
1193                         if (rangestart != restart_point) {
1194                                 warnx(
1195                                     "Size of `%s' differs from save file `%s'",
1196                                     url, savefile);
1197                                 goto cleanup_fetch_url;
1198                         }
1199                         fout = fopen(savefile, "a");
1200                 } else
1201                         fout = fopen(savefile, "w");
1202                 if (fout == NULL) {
1203                         warn("Can't open `%s'", savefile);
1204                         goto cleanup_fetch_url;
1205                 }
1206                 closefunc = fclose;
1207         }
1208
1209                         /* Trap signals */
1210         oldquit = xsignal(SIGQUIT, psummary);
1211         oldint = xsignal(SIGINT, aborthttp);
1212
1213         assert(rcvbuf_size > 0);
1214         if ((size_t)rcvbuf_size > bufsize) {
1215                 if (xferbuf)
1216                         (void)free(xferbuf);
1217                 bufsize = rcvbuf_size;
1218                 xferbuf = ftp_malloc(bufsize);
1219         }
1220
1221         bytes = 0;
1222         hashbytes = mark;
1223         if (oldalrm) {
1224                 (void)xsignal(SIGALRM, oldalrm);
1225                 oldalrm = NULL;
1226         }
1227         progressmeter(-1);
1228
1229                         /* Finally, suck down the file. */
1230         do {
1231                 long chunksize;
1232                 short lastchunk;
1233
1234                 chunksize = 0;
1235                 lastchunk = 0;
1236                                         /* read chunk-size */
1237                 if (ischunked) {
1238                         if (fetch_getln(xferbuf, bufsize, fin) == NULL) {
1239                                 warnx("Unexpected EOF reading chunk-size");
1240                                 goto cleanup_fetch_url;
1241                         }
1242                         errno = 0;
1243                         chunksize = strtol(xferbuf, &ep, 16);
1244                         if (ep == xferbuf) {
1245                                 warnx("Invalid chunk-size");
1246                                 goto cleanup_fetch_url;
1247                         }
1248                         if (errno == ERANGE || chunksize < 0) {
1249                                 errno = ERANGE;
1250                                 warn("Chunk-size `%.*s'",
1251                                     (int)(ep-xferbuf), xferbuf);
1252                                 goto cleanup_fetch_url;
1253                         }
1254
1255                                 /*
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.
1259                                  */
1260                         while (*ep == ' ')
1261                                 ep++;
1262
1263                                         /* skip [ chunk-ext ] */
1264                         if (*ep == ';') {
1265                                 while (*ep && *ep != '\r')
1266                                         ep++;
1267                         }
1268
1269                         if (strcmp(ep, "\r\n") != 0) {
1270                                 warnx("Unexpected data following chunk-size");
1271                                 goto cleanup_fetch_url;
1272                         }
1273                         DPRINTF("%s: got chunk-size of " LLF "\n", __func__,
1274                             (LLT)chunksize);
1275                         if (chunksize == 0) {
1276                                 lastchunk = 1;
1277                                 goto chunkdone;
1278                         }
1279                 }
1280                                         /* transfer file or chunk */
1281                 while (1) {
1282                         struct timeval then, now, td;
1283                         volatile off_t bufrem;
1284
1285                         if (rate_get)
1286                                 (void)gettimeofday(&then, NULL);
1287                         bufrem = rate_get ? rate_get : (off_t)bufsize;
1288                         if (ischunked)
1289                                 bufrem = MIN(chunksize, bufrem);
1290                         while (bufrem > 0) {
1291                                 flen = fetch_read(xferbuf, sizeof(char),
1292                                     MIN((off_t)bufsize, bufrem), fin);
1293                                 if (flen <= 0)
1294                                         goto chunkdone;
1295                                 bytes += flen;
1296                                 bufrem -= flen;
1297                                 if (fwrite(xferbuf, sizeof(char), flen, fout)
1298                                     != flen) {
1299                                         warn("Writing `%s'", savefile);
1300                                         goto cleanup_fetch_url;
1301                                 }
1302                                 if (hash && !progress) {
1303                                         while (bytes >= hashbytes) {
1304                                                 (void)putc('#', ttyout);
1305                                                 hashbytes += mark;
1306                                         }
1307                                         (void)fflush(ttyout);
1308                                 }
1309                                 if (ischunked) {
1310                                         chunksize -= flen;
1311                                         if (chunksize <= 0)
1312                                                 break;
1313                                 }
1314                         }
1315                         if (rate_get) {
1316                                 while (1) {
1317                                         (void)gettimeofday(&now, NULL);
1318                                         timersub(&now, &then, &td);
1319                                         if (td.tv_sec > 0)
1320                                                 break;
1321                                         usleep(1000000 - td.tv_usec);
1322                                 }
1323                         }
1324                         if (ischunked && chunksize <= 0)
1325                                 break;
1326                 }
1327                                         /* read CRLF after chunk*/
1328  chunkdone:
1329                 if (ischunked) {
1330                         if (fetch_getln(xferbuf, bufsize, fin) == NULL) {
1331                                 alarmtimer(0);
1332                                 warnx("Unexpected EOF reading chunk CRLF");
1333                                 goto cleanup_fetch_url;
1334                         }
1335                         if (strcmp(xferbuf, "\r\n") != 0) {
1336                                 warnx("Unexpected data following chunk");
1337                                 goto cleanup_fetch_url;
1338                         }
1339                         if (lastchunk)
1340                                 break;
1341                 }
1342         } while (ischunked);
1343
1344 /* XXX: deal with optional trailer & CRLF here? */
1345
1346         if (hash && !progress && bytes > 0) {
1347                 if (bytes < mark)
1348                         (void)putc('#', ttyout);
1349                 (void)putc('\n', ttyout);
1350         }
1351         if (fetch_error(fin)) {
1352                 warn("Reading file");
1353                 goto cleanup_fetch_url;
1354         }
1355         progressmeter(1);
1356         (void)fflush(fout);
1357         if (closefunc == fclose && mtime != -1) {
1358                 struct timeval tval[2];
1359
1360                 (void)gettimeofday(&tval[0], NULL);
1361                 tval[1].tv_sec = mtime;
1362                 tval[1].tv_usec = 0;
1363                 (*closefunc)(fout);
1364                 fout = NULL;
1365
1366                 if (utimes(savefile, tval) == -1) {
1367                         fprintf(ttyout,
1368                             "Can't change modification time to %s",
1369                             rfc2822time(localtime(&mtime)));
1370                 }
1371         }
1372         if (bytes > 0)
1373                 ptransfer(0);
1374         bytes = 0;
1375
1376         rval = 0;
1377         goto cleanup_fetch_url;
1378
1379  improper:
1380         warnx("Improper response from `%s:%s'", host, port);
1381
1382  cleanup_fetch_url:
1383         if (oldint)
1384                 (void)xsignal(SIGINT, oldint);
1385         if (oldpipe)
1386                 (void)xsignal(SIGPIPE, oldpipe);
1387         if (oldalrm)
1388                 (void)xsignal(SIGALRM, oldalrm);
1389         if (oldquit)
1390                 (void)xsignal(SIGQUIT, oldpipe);
1391         if (fin != NULL)
1392                 fetch_close(fin);
1393         else if (s != -1)
1394                 close(s);
1395         if (closefunc != NULL && fout != NULL)
1396                 (*closefunc)(fout);
1397         if (res0)
1398                 freeaddrinfo(res0);
1399         if (savefile != outfile)
1400                 FREEPTR(savefile);
1401         FREEPTR(uuser);
1402         if (pass != NULL)
1403                 memset(pass, 0, strlen(pass));
1404         FREEPTR(pass);
1405         FREEPTR(host);
1406         FREEPTR(port);
1407         FREEPTR(path);
1408         FREEPTR(decodedpath);
1409         FREEPTR(puser);
1410         if (ppass != NULL)
1411                 memset(ppass, 0, strlen(ppass));
1412         FREEPTR(ppass);
1413         FREEPTR(auth);
1414         FREEPTR(location);
1415         FREEPTR(message);
1416         return (rval);
1417 }
1418
1419 /*
1420  * Abort a HTTP retrieval
1421  */
1422 static void
1423 aborthttp(int notused)
1424 {
1425         char msgbuf[100];
1426         int len;
1427
1428         sigint_raised = 1;
1429         alarmtimer(0);
1430         if (fromatty) {
1431                 len = snprintf(msgbuf, sizeof(msgbuf),
1432                     "\n%s: HTTP fetch aborted.\n", getprogname());
1433                 if (len > 0)
1434                         write(fileno(ttyout), msgbuf, len);
1435         }
1436         siglongjmp(httpabort, 1);
1437 }
1438
1439 static void
1440 timeouthttp(int notused)
1441 {
1442         char msgbuf[100];
1443         int len;
1444
1445         alarmtimer(0);
1446         if (fromatty) {
1447                 len = snprintf(msgbuf, sizeof(msgbuf),
1448                     "\n%s: HTTP fetch timeout.\n", getprogname());
1449                 if (len > 0)
1450                         write(fileno(ttyout), msgbuf, len);
1451         }
1452         siglongjmp(httpabort, 1);
1453 }
1454
1455 /*
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 /)
1459  */
1460 static int
1461 fetch_ftp(const char *url)
1462 {
1463         char            *cp, *xargv[5], rempath[MAXPATHLEN];
1464         char            *host, *path, *dir, *file, *uuser, *pass;
1465         char            *port;
1466         char             cmdbuf[MAXPATHLEN];
1467         char             dirbuf[4];
1468         int              dirhasglob, filehasglob, rval, transtype, xargc;
1469         int              oanonftp, oautologin;
1470         in_port_t        portnum;
1471         url_t            urltype;
1472
1473         DPRINTF("fetch_ftp: `%s'\n", url);
1474         host = path = dir = file = uuser = pass = NULL;
1475         port = NULL;
1476         rval = 1;
1477         transtype = TYPE_I;
1478
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;
1486                 }
1487                 /*
1488                  * Note: Don't url_decode(path) here.  We need to keep the
1489                  * distinction between "/" and "%2F" until later.
1490                  */
1491
1492                                         /* check for trailing ';type=[aid]' */
1493                 if (! EMPTYSTRING(path) && (cp = strrchr(path, ';')) != NULL) {
1494                         if (strcasecmp(cp, ";type=a") == 0)
1495                                 transtype = TYPE_A;
1496                         else if (strcasecmp(cp, ";type=i") == 0)
1497                                 transtype = TYPE_I;
1498                         else if (strcasecmp(cp, ";type=d") == 0) {
1499                                 warnx(
1500                             "Directory listing via a URL is not supported");
1501                                 goto cleanup_fetch_ftp;
1502                         } else {
1503                                 warnx("Invalid suffix `%s' in URL `%s'", cp,
1504                                     url);
1505                                 goto cleanup_fetch_ftp;
1506                         }
1507                         *cp = 0;
1508                 }
1509         } else {                        /* classic style `[user@]host:[file]' */
1510                 urltype = CLASSIC_URL_T;
1511                 host = ftp_strdup(url);
1512                 cp = strchr(host, '@');
1513                 if (cp != NULL) {
1514                         *cp = '\0';
1515                         uuser = host;
1516                         anonftp = 0;    /* disable anonftp */
1517                         host = ftp_strdup(cp + 1);
1518                 }
1519                 cp = strchr(host, ':');
1520                 if (cp != NULL) {
1521                         *cp = '\0';
1522                         path = ftp_strdup(cp + 1);
1523                 }
1524         }
1525         if (EMPTYSTRING(host))
1526                 goto cleanup_fetch_ftp;
1527
1528                         /* Extract the file and (if present) directory name. */
1529         dir = path;
1530         if (! EMPTYSTRING(dir)) {
1531                 /*
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".
1536                  *
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".
1542                  *
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'.)
1546                  *
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'.)
1552                  */
1553                 cp = strrchr(dir, '/');
1554                 if (cp == dir && urltype == CLASSIC_URL_T) {
1555                         file = cp + 1;
1556                         (void)strlcpy(dirbuf, "/", sizeof(dirbuf));
1557                         dir = dirbuf;
1558                 } else if (cp != NULL) {
1559                         *cp++ = '\0';
1560                         file = cp;
1561                 } else {
1562                         file = dir;
1563                         dir = NULL;
1564                 }
1565         } else
1566                 dir = NULL;
1567         if (urltype == FTP_URL_T && file != NULL) {
1568                 url_decode(file);
1569                 /* but still don't url_decode(dir) */
1570         }
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));
1576
1577         dirhasglob = filehasglob = 0;
1578         if (doglob && urltype == CLASSIC_URL_T) {
1579                 if (! EMPTYSTRING(dir) && strpbrk(dir, "*?[]{}") != NULL)
1580                         dirhasglob = 1;
1581                 if (! EMPTYSTRING(file) && strpbrk(file, "*?[]{}") != NULL)
1582                         filehasglob = 1;
1583         }
1584
1585                         /* Set up the connection */
1586         oanonftp = anonftp;
1587         if (connected)
1588                 disconnect(0, NULL);
1589         anonftp = oanonftp;
1590         (void)strlcpy(cmdbuf, getprogname(), sizeof(cmdbuf));
1591         xargv[0] = cmdbuf;
1592         xargv[1] = host;
1593         xargv[2] = NULL;
1594         xargc = 2;
1595         if (port) {
1596                 xargv[2] = port;
1597                 xargv[3] = NULL;
1598                 xargc = 3;
1599         }
1600         oautologin = autologin;
1601                 /* don't autologin in setpeer(), use ftp_login() below */
1602         autologin = 0;
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;
1610         }
1611
1612         switch (transtype) {
1613         case TYPE_A:
1614                 setascii(1, xargv);
1615                 break;
1616         case TYPE_I:
1617                 setbinary(1, xargv);
1618                 break;
1619         default:
1620                 errx(1, "fetch_ftp: unknown transfer type %d", transtype);
1621         }
1622
1623                 /*
1624                  * Change directories, if necessary.
1625                  *
1626                  * Note: don't use EMPTYSTRING(dir) below, because
1627                  * dir=="" means something different from dir==NULL.
1628                  */
1629         if (dir != NULL && !dirhasglob) {
1630                 char *nextpart;
1631
1632                 /*
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.
1637                  *
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.
1646                  *
1647                  * Many ftp servers don't support `CWD ', so if there's an
1648                  * error performing that command, bail out with a descriptive
1649                  * message.
1650                  *
1651                  * Examples:
1652                  *
1653                  * host:                        dir="", urltype=CLASSIC_URL_T
1654                  *              logged in (to default directory)
1655                  * host:file                    dir=NULL, urltype=CLASSIC_URL_T
1656                  *              "RETR file"
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
1664                  *              "RETR file"
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"
1689                  *
1690                  * Note that we don't need `dir' after this point.
1691                  */
1692                 do {
1693                         if (urltype == FTP_URL_T) {
1694                                 nextpart = strchr(dir, '/');
1695                                 if (nextpart) {
1696                                         *nextpart = '\0';
1697                                         nextpart++;
1698                                 }
1699                                 url_decode(dir);
1700                         } else
1701                                 nextpart = NULL;
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));
1706                                 xargv[0] = cmdbuf;
1707                                 xargv[1] = dir;
1708                                 xargv[2] = NULL;
1709                                 dirchange = 0;
1710                                 cd(2, xargv);
1711                                 if (! dirchange) {
1712                                         if (*dir == '\0' && code == 500)
1713                                                 fprintf(stderr,
1714 "\n"
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"
1719 "\n");
1720                                         goto cleanup_fetch_ftp;
1721                                 }
1722                         }
1723                         dir = nextpart;
1724                 } while (dir != NULL);
1725         }
1726
1727         if (EMPTYSTRING(file)) {
1728                 rval = -1;
1729                 goto cleanup_fetch_ftp;
1730         }
1731
1732         if (dirhasglob) {
1733                 (void)strlcpy(rempath, dir,     sizeof(rempath));
1734                 (void)strlcat(rempath, "/",     sizeof(rempath));
1735                 (void)strlcat(rempath, file,    sizeof(rempath));
1736                 file = rempath;
1737         }
1738
1739                         /* Fetch the file(s). */
1740         xargc = 2;
1741         (void)strlcpy(cmdbuf, "get", sizeof(cmdbuf));
1742         xargv[0] = cmdbuf;
1743         xargv[1] = file;
1744         xargv[2] = NULL;
1745         if (dirhasglob || filehasglob) {
1746                 int ointeractive;
1747
1748                 ointeractive = interactive;
1749                 interactive = 0;
1750                 if (restartautofetch)
1751                         (void)strlcpy(cmdbuf, "mreget", sizeof(cmdbuf));
1752                 else
1753                         (void)strlcpy(cmdbuf, "mget", sizeof(cmdbuf));
1754                 xargv[0] = cmdbuf;
1755                 mget(xargc, xargv);
1756                 interactive = ointeractive;
1757         } else {
1758                 if (outfile == NULL) {
1759                         cp = strrchr(file, '/');        /* find savefile */
1760                         if (cp != NULL)
1761                                 outfile = cp + 1;
1762                         else
1763                                 outfile = file;
1764                 }
1765                 xargv[2] = (char *)outfile;
1766                 xargv[3] = NULL;
1767                 xargc++;
1768                 if (restartautofetch)
1769                         reget(xargc, xargv);
1770                 else
1771                         get(xargc, xargv);
1772         }
1773
1774         if ((code / 100) == COMPLETE)
1775                 rval = 0;
1776
1777  cleanup_fetch_ftp:
1778         FREEPTR(port);
1779         FREEPTR(host);
1780         FREEPTR(path);
1781         FREEPTR(uuser);
1782         if (pass)
1783                 memset(pass, 0, strlen(pass));
1784         FREEPTR(pass);
1785         return (rval);
1786 }
1787
1788 /*
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
1792  *                                      call fetch_ftp()
1793  *      "http://host/path"              call fetch_url() to use HTTP
1794  *      "file:///path"                  call fetch_url() to copy
1795  *      "about:..."                     print a message
1796  *
1797  * Returns 1 on failure, 0 on completed xfer, -1 if ftp connection
1798  * is still open (e.g, ftp xfer with trailing /)
1799  */
1800 static int
1801 go_fetch(const char *url)
1802 {
1803         char *proxyenv;
1804         char *p;
1805
1806 #ifndef NO_ABOUT
1807         /*
1808          * Check for about:*
1809          */
1810         if (STRNEQUAL(url, ABOUT_URL)) {
1811                 url += sizeof(ABOUT_URL) -1;
1812                 if (strcasecmp(url, "ftp") == 0 ||
1813                     strcasecmp(url, "tnftp") == 0) {
1814                         fputs(
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) {
1818                         fputs(
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) {
1822                         fputs(
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,
1828 #ifdef INET6
1829                             ""
1830 #else
1831                             " (-IPv6)"
1832 #endif
1833                         );
1834                 } else {
1835                         fprintf(ttyout, "`%s' is an interesting topic.\n", url);
1836                 }
1837                 fputs("\n", ttyout);
1838                 return (0);
1839         }
1840 #endif
1841
1842         /*
1843          * Check for file:// and http:// URLs.
1844          */
1845         if (STRNEQUAL(url, HTTP_URL)
1846 #ifdef WITH_SSL
1847             || STRNEQUAL(url, HTTPS_URL)
1848 #endif
1849             || STRNEQUAL(url, FILE_URL))
1850                 return (fetch_url(url, NULL, NULL, NULL));
1851
1852         /*
1853          * If it contains "://" but does not begin with ftp://
1854          * or something that was already handled, then it's
1855          * unsupported.
1856          *
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.
1860          */
1861         if ((p = strstr(url, "://")) != NULL && ! STRNEQUAL(url, FTP_URL))
1862                 errx(1, "Unsupported URL scheme `%.*s'", (int)(p - url), url);
1863
1864         /*
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().
1868          */
1869         proxyenv = getoptionvalue("ftp_proxy");
1870         if (!EMPTYSTRING(proxyenv) && STRNEQUAL(url, FTP_URL))
1871                 return (fetch_url(url, NULL, NULL, NULL));
1872
1873         return (fetch_ftp(url));
1874 }
1875
1876 /*
1877  * Retrieve multiple files from the command line,
1878  * calling go_fetch() for each file.
1879  *
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]
1885  * returns x+1)
1886  * Otherwise, 0 is returned if all files retrieved successfully.
1887  */
1888 int
1889 auto_fetch(int argc, char *argv[])
1890 {
1891         volatile int    argpos, rval;
1892
1893         argpos = rval = 0;
1894
1895         if (sigsetjmp(toplevel, 1)) {
1896                 if (connected)
1897                         disconnect(0, NULL);
1898                 if (rval > 0)
1899                         rval = argpos + 1;
1900                 return (rval);
1901         }
1902         (void)xsignal(SIGINT, intr);
1903         (void)xsignal(SIGPIPE, lostpeer);
1904
1905         /*
1906          * Loop through as long as there's files to fetch.
1907          */
1908         for (; (rval == 0) && (argpos < argc); argpos++) {
1909                 if (strchr(argv[argpos], ':') == NULL)
1910                         break;
1911                 redirect_loop = 0;
1912                 if (!anonftp)
1913                         anonftp = 2;    /* Handle "automatic" transfers. */
1914                 rval = go_fetch(argv[argpos]);
1915                 if (outfile != NULL && strcmp(outfile, "-") != 0
1916                     && outfile[0] != '|')
1917                         outfile = NULL;
1918                 if (rval > 0)
1919                         rval = argpos + 1;
1920         }
1921
1922         if (connected && rval != -1)
1923                 disconnect(0, NULL);
1924         return (rval);
1925 }
1926
1927
1928 /*
1929  * Upload multiple files from the command line.
1930  *
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]
1933  * returns x+1)
1934  * Otherwise, 0 is returned if all files uploaded successfully.
1935  */
1936 int
1937 auto_put(int argc, char **argv, const char *uploadserver)
1938 {
1939         char    *uargv[4], *path, *pathsep;
1940         int      uargc, rval, argpos;
1941         size_t   len;
1942         char     cmdbuf[MAX_C_NAME];
1943
1944         (void)strlcpy(cmdbuf, "mput", sizeof(cmdbuf));
1945         uargv[0] = cmdbuf;
1946         uargv[1] = argv[0];
1947         uargc = 2;
1948         uargv[2] = uargv[3] = NULL;
1949         pathsep = NULL;
1950         rval = 1;
1951
1952         DPRINTF("auto_put: target `%s'\n", uploadserver);
1953
1954         path = ftp_strdup(uploadserver);
1955         len = strlen(path);
1956         if (path[len - 1] != '/' && path[len - 1] != ':') {
1957                         /*
1958                          * make sure we always pass a directory to auto_fetch
1959                          */
1960                 if (argc > 1) {         /* more than one file to upload */
1961                         len = strlen(uploadserver) + 2; /* path + "/" + "\0" */
1962                         free(path);
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));
1968                         uargv[0] = 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;
1975                                 }
1976                                 pathsep++;
1977                                 uargv[2] = ftp_strdup(pathsep);
1978                                 pathsep[0] = '/';
1979                         } else
1980                                 uargv[2] = ftp_strdup(pathsep + 1);
1981                         pathsep[1] = '\0';
1982                         uargc++;
1983                 }
1984         }
1985         DPRINTF("auto_put: URL `%s' argv[2] `%s'\n",
1986             path, STRorNULL(uargv[2]));
1987
1988                         /* connect and cwd */
1989         rval = auto_fetch(1, &path);
1990         if(rval >= 0)
1991                 goto cleanup_auto_put;
1992
1993         rval = 0;
1994
1995                         /* target filename provided; upload 1 file */
1996                         /* XXX : is this the best way? */
1997         if (uargc == 3) {
1998                 uargv[1] = argv[0];
1999                 put(uargc, uargv);
2000                 if ((code / 100) != COMPLETE)
2001                         rval = 1;
2002         } else {        /* otherwise a target dir: upload all files to it */
2003                 for(argpos = 0; argv[argpos] != NULL; argpos++) {
2004                         uargv[1] = argv[argpos];
2005                         mput(uargc, uargv);
2006                         if ((code / 100) != COMPLETE) {
2007                                 rval = argpos + 1;
2008                                 break;
2009                         }
2010                 }
2011         }
2012
2013  cleanup_auto_put:
2014         free(path);
2015         FREEPTR(uargv[2]);
2016         return (rval);
2017 }