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