Annotation of src/usr.bin/ftp/fetch.c, Revision 1.54
1.54 ! fgsch 1: /* $OpenBSD: fetch.c,v 1.53 2005/04/11 15:16:50 deraadt Exp $ */
1.15 millert 2: /* $NetBSD: fetch.c,v 1.14 1997/08/18 10:20:20 lukem Exp $ */
1.1 millert 3:
4: /*-
5: * Copyright (c) 1997 The NetBSD Foundation, Inc.
6: * All rights reserved.
7: *
8: * This code is derived from software contributed to The NetBSD Foundation
9: * by Jason Thorpe and Luke Mewburn.
10: *
11: * Redistribution and use in source and binary forms, with or without
12: * modification, are permitted provided that the following conditions
13: * are met:
14: * 1. Redistributions of source code must retain the above copyright
15: * notice, this list of conditions and the following disclaimer.
16: * 2. Redistributions in binary form must reproduce the above copyright
17: * notice, this list of conditions and the following disclaimer in the
18: * documentation and/or other materials provided with the distribution.
19: * 3. All advertising materials mentioning features or use of this software
20: * must display the following acknowledgement:
21: * This product includes software developed by the NetBSD
22: * Foundation, Inc. and its contributors.
23: * 4. Neither the name of The NetBSD Foundation nor the names of its
24: * contributors may be used to endorse or promote products derived
25: * from this software without specific prior written permission.
26: *
27: * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29: * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1.15 millert 30: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
1.1 millert 32: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37: * POSSIBILITY OF SUCH DAMAGE.
38: */
39:
1.48 deraadt 40: #if !defined(lint) && !defined(SMALL)
1.54 ! fgsch 41: static char rcsid[] = "$OpenBSD: fetch.c,v 1.53 2005/04/11 15:16:50 deraadt Exp $";
1.48 deraadt 42: #endif /* not lint and not SMALL */
1.1 millert 43:
44: /*
45: * FTP User Program -- Command line file retrieval
46: */
47:
48: #include <sys/types.h>
49: #include <sys/param.h>
50: #include <sys/socket.h>
1.22 deraadt 51: #include <sys/stat.h>
1.1 millert 52:
53: #include <netinet/in.h>
54:
55: #include <arpa/ftp.h>
56: #include <arpa/inet.h>
57:
58: #include <ctype.h>
59: #include <err.h>
1.17 millert 60: #include <libgen.h>
1.1 millert 61: #include <netdb.h>
62: #include <fcntl.h>
1.3 millert 63: #include <signal.h>
1.1 millert 64: #include <stdio.h>
1.19 deraadt 65: #include <errno.h>
1.1 millert 66: #include <stdlib.h>
67: #include <string.h>
68: #include <unistd.h>
1.40 fgsch 69: #include <util.h>
1.1 millert 70:
71: #include "ftp_var.h"
72:
1.38 millert 73: static int url_get(const char *, const char *, const char *);
74: void aborthttp(int);
75: void abortfile(int);
1.42 deraadt 76: char hextochar(const char *);
77: char *urldecode(const char *);
1.14 millert 78:
1.1 millert 79: #define FTP_URL "ftp://" /* ftp URL prefix */
80: #define HTTP_URL "http://" /* http URL prefix */
1.22 deraadt 81: #define FILE_URL "file:" /* file URL prefix */
1.6 millert 82: #define FTP_PROXY "ftp_proxy" /* env var with ftp proxy location */
1.1 millert 83: #define HTTP_PROXY "http_proxy" /* env var with http proxy location */
84:
85:
86: #define EMPTYSTRING(x) ((x) == NULL || (*(x) == '\0'))
87:
1.37 heko 88: static const char *at_encoding_warning =
1.53 deraadt 89: "Extra `@' characters in usernames and passwords should be encoded as %%40";
1.37 heko 90:
1.1 millert 91: jmp_buf httpabort;
92:
1.54 ! fgsch 93: static int redirect_loop;
! 94:
1.1 millert 95: /*
1.6 millert 96: * Retrieve URL, via the proxy in $proxyvar if necessary.
1.1 millert 97: * Modifies the string argument given.
98: * Returns -1 on failure, 0 on success
99: */
1.14 millert 100: static int
1.50 deraadt 101: url_get(const char *origline, const char *proxyenv, const char *outfile)
1.1 millert 102: {
1.53 deraadt 103: char pbuf[NI_MAXSERV], hbuf[NI_MAXHOST], *cp, *ep, *portnum, *path;
104: char *hosttail, *cause = "unknown", *line, *host, *port, *buf = NULL;
105: int error, i, isftpurl = 0, isfileurl = 0, isredirect = 0, rval = -1;
1.25 itojun 106: struct addrinfo hints, *res0, *res;
1.34 millert 107: const char * volatile savefile;
1.53 deraadt 108: char * volatile proxy = NULL;
109: volatile int s = -1, out;
1.14 millert 110: volatile sig_t oldintr;
1.53 deraadt 111: FILE *fin = NULL;
1.1 millert 112: off_t hashbytes;
1.53 deraadt 113: size_t len;
1.14 millert 114:
115: line = strdup(origline);
116: if (line == NULL)
117: errx(1, "Can't allocate memory to parse URL");
1.7 millert 118: if (strncasecmp(line, HTTP_URL, sizeof(HTTP_URL) - 1) == 0)
1.6 millert 119: host = line + sizeof(HTTP_URL) - 1;
1.14 millert 120: else if (strncasecmp(line, FTP_URL, sizeof(FTP_URL) - 1) == 0) {
1.6 millert 121: host = line + sizeof(FTP_URL) - 1;
1.14 millert 122: isftpurl = 1;
1.22 deraadt 123: } else if (strncasecmp(line, FILE_URL, sizeof(FILE_URL) - 1) == 0) {
124: host = line + sizeof(FILE_URL) - 1;
125: isfileurl = 1;
1.14 millert 126: } else
127: errx(1, "url_get: Invalid URL '%s'", line);
1.6 millert 128:
1.22 deraadt 129: if (isfileurl) {
130: path = host;
131: } else {
132: path = strchr(host, '/'); /* find path */
133: if (EMPTYSTRING(path)) {
134: if (isftpurl)
135: goto noftpautologin;
136: warnx("Invalid URL (no `/' after host): %s", origline);
137: goto cleanup_url_get;
138: }
139: *path++ = '\0';
140: if (EMPTYSTRING(path)) {
141: if (isftpurl)
142: goto noftpautologin;
143: warnx("Invalid URL (no file after host): %s", origline);
144: goto cleanup_url_get;
145: }
1.14 millert 146: }
1.1 millert 147:
1.17 millert 148: if (outfile)
149: savefile = outfile;
1.1 millert 150: else
1.17 millert 151: savefile = basename(path);
152:
1.14 millert 153: if (EMPTYSTRING(savefile)) {
154: if (isftpurl)
155: goto noftpautologin;
156: warnx("Invalid URL (no file after directory): %s", origline);
1.6 millert 157: goto cleanup_url_get;
1.14 millert 158: }
1.1 millert 159:
160: if (proxyenv != NULL) { /* use proxy */
161: proxy = strdup(proxyenv);
162: if (proxy == NULL)
1.14 millert 163: errx(1, "Can't allocate memory for proxy URL.");
1.7 millert 164: if (strncasecmp(proxy, HTTP_URL, sizeof(HTTP_URL) - 1) == 0)
1.6 millert 165: host = proxy + sizeof(HTTP_URL) - 1;
1.7 millert 166: else if (strncasecmp(proxy, FTP_URL, sizeof(FTP_URL) - 1) == 0)
1.6 millert 167: host = proxy + sizeof(FTP_URL) - 1;
168: else {
1.14 millert 169: warnx("Malformed proxy URL: %s", proxyenv);
1.6 millert 170: goto cleanup_url_get;
171: }
1.14 millert 172: if (EMPTYSTRING(host)) {
173: warnx("Malformed proxy URL: %s", proxyenv);
1.6 millert 174: goto cleanup_url_get;
1.14 millert 175: }
1.1 millert 176: *--path = '/'; /* add / back to real path */
177: path = strchr(host, '/'); /* remove trailing / on host */
1.42 deraadt 178: if (!EMPTYSTRING(path))
1.1 millert 179: *path++ = '\0';
180: path = line;
181: }
182:
1.22 deraadt 183: if (isfileurl) {
184: struct stat st;
185:
186: s = open(path, O_RDONLY);
187: if (s == -1) {
188: warn("Can't open file %s", path);
189: goto cleanup_url_get;
190: }
191:
192: if (fstat(s, &st) == -1)
193: filesize = -1;
194: else
195: filesize = st.st_size;
196:
197: /* Open the output file. */
198: if (strcmp(savefile, "-") != 0) {
199: out = open(savefile, O_CREAT | O_WRONLY | O_TRUNC, 0666);
200: if (out < 0) {
201: warn("Can't open %s", savefile);
202: goto cleanup_url_get;
203: }
204: } else
205: out = fileno(stdout);
206:
207: /* Trap signals */
208: oldintr = NULL;
209: if (setjmp(httpabort)) {
210: if (oldintr)
211: (void)signal(SIGINT, oldintr);
212: goto cleanup_url_get;
213: }
214: oldintr = signal(SIGINT, abortfile);
1.42 deraadt 215:
1.22 deraadt 216: bytes = 0;
217: hashbytes = mark;
218: progressmeter(-1);
1.40 fgsch 219:
220: if ((buf = malloc(4096)) == NULL)
1.47 deraadt 221: errx(1, "Can't allocate memory for transfer buffer");
1.42 deraadt 222:
1.22 deraadt 223: /* Finally, suck down the file. */
224: i = 0;
1.40 fgsch 225: while ((len = read(s, buf, 4096)) > 0) {
1.22 deraadt 226: bytes += len;
227: for (cp = buf; len > 0; len -= i, cp += i) {
228: if ((i = write(out, cp, len)) == -1) {
229: warn("Writing %s", savefile);
230: goto cleanup_url_get;
231: }
232: else if (i == 0)
233: break;
234: }
235: if (hash && !progress) {
236: while (bytes >= hashbytes) {
237: (void)putc('#', ttyout);
238: hashbytes += mark;
239: }
240: (void)fflush(ttyout);
241: }
242: }
243: if (hash && !progress && bytes > 0) {
244: if (bytes < mark)
245: (void)putc('#', ttyout);
246: (void)putc('\n', ttyout);
247: (void)fflush(ttyout);
248: }
249: if (len != 0) {
250: warn("Reading from file");
251: goto cleanup_url_get;
252: }
253: progressmeter(1);
254: if (verbose)
255: fputs("Successfully retrieved file.\n", ttyout);
256: (void)signal(SIGINT, oldintr);
1.42 deraadt 257:
1.40 fgsch 258: rval = 0;
259: goto cleanup_url_get;
1.22 deraadt 260: }
261:
1.28 itojun 262: if (*host == '[' && (hosttail = strrchr(host, ']')) != NULL &&
263: (hosttail[1] == '\0' || hosttail[1] == ':')) {
264: host++;
265: *hosttail++ = '\0';
266: } else
267: hosttail = host;
268:
269: portnum = strrchr(hosttail, ':'); /* find portnum */
1.1 millert 270: if (portnum != NULL)
271: *portnum++ = '\0';
272:
273: if (debug)
1.10 deraadt 274: fprintf(ttyout, "host %s, port %s, path %s, save as %s.\n",
1.1 millert 275: host, portnum, path, savefile);
276:
1.25 itojun 277: memset(&hints, 0, sizeof(hints));
1.39 deraadt 278: hints.ai_family = family;
1.25 itojun 279: hints.ai_socktype = SOCK_STREAM;
280: port = portnum ? portnum : httpport;
281: error = getaddrinfo(host, port, &hints, &res0);
1.30 deraadt 282: if (error == EAI_SERVICE && port == httpport) {
283: /*
284: * If the services file is corrupt/missing, fall back
285: * on our hard-coded defines.
286: */
287: snprintf(pbuf, sizeof(pbuf), "%d", HTTP_PORT);
288: error = getaddrinfo(host, pbuf, &hints, &res0);
289: }
1.25 itojun 290: if (error) {
291: warnx("%s: %s", gai_strerror(error), host);
292: goto cleanup_url_get;
1.1 millert 293: }
294:
1.25 itojun 295: s = -1;
296: for (res = res0; res; res = res->ai_next) {
1.44 itojun 297: if (getnameinfo(res->ai_addr, res->ai_addrlen, hbuf,
298: sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0)
299: strlcpy(hbuf, "(unknown)", sizeof(hbuf));
1.41 deraadt 300: if (verbose)
1.44 itojun 301: fprintf(ttyout, "Trying %s...\n", hbuf);
1.14 millert 302:
1.25 itojun 303: s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
304: if (s == -1) {
305: cause = "socket";
306: continue;
1.1 millert 307: }
308:
1.25 itojun 309: again:
310: if (connect(s, res->ai_addr, res->ai_addrlen) < 0) {
311: if (errno == EINTR)
312: goto again;
313: close(s);
314: s = -1;
315: cause = "connect";
1.19 deraadt 316: continue;
317: }
1.25 itojun 318:
1.29 itojun 319: /* get port in numeric */
320: if (getnameinfo(res->ai_addr, res->ai_addrlen, NULL, 0,
321: pbuf, sizeof(pbuf), NI_NUMERICSERV) == 0)
322: port = pbuf;
323: else
324: port = NULL;
325:
1.25 itojun 326: break;
327: }
328: freeaddrinfo(res0);
329: if (s < 0) {
1.33 millert 330: warn("%s", cause);
1.6 millert 331: goto cleanup_url_get;
1.1 millert 332: }
333:
1.40 fgsch 334: fin = fdopen(s, "r+");
335:
1.1 millert 336: /*
1.40 fgsch 337: * Construct and send the request. Proxy requests don't want leading /.
1.1 millert 338: */
1.32 itojun 339: if (proxy) {
340: /*
341: * Host: directive must use the destination host address for
342: * the original URI (path). We do not attach it at this moment.
343: */
344: if (verbose)
1.23 deraadt 345: fprintf(ttyout, "Requesting %s (via %s)\n",
346: origline, proxyenv);
1.40 fgsch 347: fprintf(fin, "GET %s HTTP/1.0\r\n%s\r\n\r\n", path, HTTP_USER_AGENT);
1.28 itojun 348: } else {
1.32 itojun 349: if (verbose)
350: fprintf(ttyout, "Requesting %s\n", origline);
351: if (strchr(host, ':')) {
352: char *h, *p;
353:
354: /* strip off scoped address portion, since it's local to node */
355: h = strdup(host);
356: if (h == NULL)
357: errx(1, "Can't allocate memory.");
358: if ((p = strchr(h, '%')) != NULL)
359: *p = '\0';
1.35 heko 360: /*
361: * Send port number only if it's specified and does not equal
362: * 80. Some broken HTTP servers get confused if you explicitly
363: * send them the port number.
364: */
365: if (port && strcmp(port, "80") != 0)
1.42 deraadt 366: fprintf(fin,
367: "GET /%s HTTP/1.0\r\nHost: [%s]:%s\r\n%s\r\n\r\n",
1.36 heko 368: path, h, port, HTTP_USER_AGENT);
1.35 heko 369: else
1.42 deraadt 370: fprintf(fin,
371: "GET /%s HTTP/1.0\r\nHost: [%s]\r\n%s\r\n\r\n",
1.36 heko 372: path, h, HTTP_USER_AGENT);
1.32 itojun 373: free(h);
374: } else {
1.35 heko 375: if (port && strcmp(port, "80") != 0)
1.42 deraadt 376: fprintf(fin,
377: "GET /%s HTTP/1.0\r\nHost: %s:%s\r\n%s\r\n\r\n",
1.36 heko 378: path, host, port, HTTP_USER_AGENT);
1.35 heko 379: else
1.42 deraadt 380: fprintf(fin,
381: "GET /%s HTTP/1.0\r\nHost: %s\r\n%s\r\n\r\n",
1.36 heko 382: path, host, HTTP_USER_AGENT);
1.32 itojun 383: }
1.28 itojun 384: }
1.40 fgsch 385: if (fflush(fin) == EOF) {
1.14 millert 386: warn("Writing HTTP request");
1.6 millert 387: goto cleanup_url_get;
1.1 millert 388: }
1.40 fgsch 389:
390: if ((buf = fparseln(fin, &len, NULL, "\0\0\0", 0)) == NULL) {
391: warn("Receiving HTTP reply");
392: goto cleanup_url_get;
1.1 millert 393: }
1.40 fgsch 394:
395: while (len > 0 && (buf[len-1] == '\r' || buf[len-1] == '\n'))
396: buf[--len] = '\0';
397: if (debug)
398: fprintf(ttyout, "received '%s'\n", buf);
399:
1.1 millert 400: cp = strchr(buf, ' ');
401: if (cp == NULL)
402: goto improper;
403: else
404: cp++;
1.40 fgsch 405: if (strncmp(cp, "301", 3) == 0 || strncmp(cp, "302", 3) == 0) {
406: isredirect++;
1.54 ! fgsch 407: if (redirect_loop++ > 10) {
! 408: warnx("Too many redirections requested");
! 409: goto cleanup_url_get;
! 410: }
1.40 fgsch 411: } else if (strncmp(cp, "200", 3)) {
1.1 millert 412: warnx("Error retrieving file: %s", cp);
1.6 millert 413: goto cleanup_url_get;
1.1 millert 414: }
415:
416: /*
417: * Read the rest of the header.
418: */
1.40 fgsch 419: free(buf);
420: filesize = -1;
421:
422: while (1) {
423: if ((buf = fparseln(fin, &len, NULL, "\0\0\0", 0)) == NULL) {
424: warn("Receiving HTTP reply");
425: goto cleanup_url_get;
426: }
427: while (len > 0 && (buf[len-1] == '\r' || buf[len-1] == '\n'))
428: buf[--len] = '\0';
429: if (len == 0)
1.1 millert 430: break;
1.40 fgsch 431: if (debug)
432: fprintf(ttyout, "received '%s'\n", buf);
1.1 millert 433:
1.40 fgsch 434: /* Look for some headers */
435: cp = buf;
1.1 millert 436: #define CONTENTLEN "Content-Length: "
1.40 fgsch 437: if (strncasecmp(cp, CONTENTLEN, sizeof(CONTENTLEN) - 1) == 0) {
438: cp += sizeof(CONTENTLEN) - 1;
439: filesize = strtol(cp, &ep, 10);
440: if (filesize < 1 || *ep != '\0')
441: goto improper;
442: #define LOCATION "Location: "
443: } else if (isredirect &&
444: strncasecmp(cp, LOCATION, sizeof(LOCATION) - 1) == 0) {
445: cp += sizeof(LOCATION) - 1;
446: if (verbose)
447: fprintf(ttyout, "Redirected to %s\n", cp);
448: if (fin != NULL)
449: fclose(fin);
450: else if (s != -1)
451: close(s);
452: if (proxy)
453: free(proxy);
454: free(line);
455: rval = url_get(cp, proxyenv, outfile);
456: if (buf)
457: free(buf);
458: return (rval);
459: }
1.1 millert 460: }
461:
1.17 millert 462: /* Open the output file. */
463: if (strcmp(savefile, "-") != 0) {
1.10 deraadt 464: out = open(savefile, O_CREAT | O_WRONLY | O_TRUNC, 0666);
465: if (out < 0) {
466: warn("Can't open %s", savefile);
467: goto cleanup_url_get;
468: }
469: } else
1.17 millert 470: out = fileno(stdout);
1.1 millert 471:
472: /* Trap signals */
473: oldintr = NULL;
474: if (setjmp(httpabort)) {
475: if (oldintr)
1.2 millert 476: (void)signal(SIGINT, oldintr);
1.6 millert 477: goto cleanup_url_get;
1.1 millert 478: }
479: oldintr = signal(SIGINT, aborthttp);
480:
481: bytes = 0;
482: hashbytes = mark;
483: progressmeter(-1);
1.43 millert 484:
485: free(buf);
1.1 millert 486:
487: /* Finally, suck down the file. */
1.40 fgsch 488: if ((buf = malloc(4096)) == NULL)
1.47 deraadt 489: errx(1, "Can't allocate memory for transfer buffer");
1.1 millert 490: i = 0;
1.40 fgsch 491: while ((len = fread(buf, sizeof(char), 4096, fin)) > 0) {
1.1 millert 492: bytes += len;
493: for (cp = buf; len > 0; len -= i, cp += i) {
494: if ((i = write(out, cp, len)) == -1) {
495: warn("Writing %s", savefile);
1.6 millert 496: goto cleanup_url_get;
1.1 millert 497: }
498: else if (i == 0)
499: break;
500: }
501: if (hash && !progress) {
502: while (bytes >= hashbytes) {
1.10 deraadt 503: (void)putc('#', ttyout);
1.1 millert 504: hashbytes += mark;
505: }
1.10 deraadt 506: (void)fflush(ttyout);
1.1 millert 507: }
508: }
509: if (hash && !progress && bytes > 0) {
510: if (bytes < mark)
1.10 deraadt 511: (void)putc('#', ttyout);
512: (void)putc('\n', ttyout);
513: (void)fflush(ttyout);
1.1 millert 514: }
515: if (len != 0) {
516: warn("Reading from socket");
1.6 millert 517: goto cleanup_url_get;
1.1 millert 518: }
519: progressmeter(1);
1.24 deraadt 520: if (filesize != -1 && len == 0 && bytes != filesize) {
521: if (verbose)
522: fputs("Read short file.\n", ttyout);
523: goto cleanup_url_get;
524: }
525:
1.1 millert 526: if (verbose)
1.10 deraadt 527: fputs("Successfully retrieved file.\n", ttyout);
1.2 millert 528: (void)signal(SIGINT, oldintr);
1.1 millert 529:
1.40 fgsch 530: rval = 0;
531: goto cleanup_url_get;
1.1 millert 532:
1.14 millert 533: noftpautologin:
534: warnx(
535: "Auto-login using ftp URLs isn't supported when using $ftp_proxy");
536: goto cleanup_url_get;
537:
1.1 millert 538: improper:
1.8 millert 539: warnx("Improper response from %s", host);
1.14 millert 540:
1.6 millert 541: cleanup_url_get:
1.40 fgsch 542: if (fin != NULL)
543: fclose(fin);
544: else if (s != -1)
1.1 millert 545: close(s);
1.40 fgsch 546: if (buf)
547: free(buf);
1.1 millert 548: if (proxy)
549: free(proxy);
1.14 millert 550: free(line);
1.40 fgsch 551: return (rval);
1.1 millert 552: }
553:
554: /*
555: * Abort a http retrieval
556: */
1.51 deraadt 557: /* ARGSUSED */
1.1 millert 558: void
1.51 deraadt 559: aborthttp(int signo)
1.1 millert 560: {
561:
562: alarmtimer(0);
1.10 deraadt 563: fputs("\nhttp fetch aborted.\n", ttyout);
564: (void)fflush(ttyout);
1.1 millert 565: longjmp(httpabort, 1);
566: }
567:
568: /*
1.22 deraadt 569: * Abort a http retrieval
570: */
1.51 deraadt 571: /* ARGSUSED */
1.22 deraadt 572: void
1.51 deraadt 573: abortfile(int signo)
1.22 deraadt 574: {
575:
576: alarmtimer(0);
577: fputs("\nfile fetch aborted.\n", ttyout);
578: (void)fflush(ttyout);
579: longjmp(httpabort, 1);
580: }
581:
582: /*
1.1 millert 583: * Retrieve multiple files from the command line, transferring
584: * files of the form "host:path", "ftp://host/path" using the
585: * ftp protocol, and files of the form "http://host/path" using
586: * the http protocol.
1.2 millert 587: * If path has a trailing "/", then return (-1);
1.1 millert 588: * the path will be cd-ed into and the connection remains open,
589: * and the function will return -1 (to indicate the connection
590: * is alive).
591: * If an error occurs the return value will be the offset+1 in
592: * argv[] of the file that caused a problem (i.e, argv[x]
593: * returns x+1)
594: * Otherwise, 0 is returned if all files retrieved successfully.
595: */
596: int
1.50 deraadt 597: auto_fetch(int argc, char *argv[], char *outfile)
1.1 millert 598: {
599: char *xargv[5];
600: char *cp, *line, *host, *dir, *file, *portnum;
1.52 henning 601: char *user, *pass, *pathstart;
1.6 millert 602: char *ftpproxy, *httpproxy;
1.14 millert 603: int rval, xargc;
604: volatile int argpos;
1.49 krw 605: int dirhasglob, filehasglob, oautologin;
1.14 millert 606: char rempath[MAXPATHLEN];
1.1 millert 607:
608: argpos = 0;
609:
610: if (setjmp(toplevel)) {
611: if (connected)
612: disconnect(0, NULL);
1.2 millert 613: return (argpos + 1);
1.1 millert 614: }
1.3 millert 615: (void)signal(SIGINT, (sig_t)intr);
616: (void)signal(SIGPIPE, (sig_t)lostpeer);
1.1 millert 617:
1.45 millert 618: if ((ftpproxy = getenv(FTP_PROXY)) != NULL && *ftpproxy == '\0')
619: ftpproxy = NULL;
620: if ((httpproxy = getenv(HTTP_PROXY)) != NULL && *httpproxy == '\0')
621: httpproxy = NULL;
1.6 millert 622:
1.1 millert 623: /*
624: * Loop through as long as there's files to fetch.
625: */
626: for (rval = 0; (rval == 0) && (argpos < argc); free(line), argpos++) {
627: if (strchr(argv[argpos], ':') == NULL)
628: break;
1.8 millert 629: host = dir = file = portnum = user = pass = NULL;
1.1 millert 630:
631: /*
632: * We muck with the string, so we make a copy.
633: */
634: line = strdup(argv[argpos]);
635: if (line == NULL)
636: errx(1, "Can't allocate memory for auto-fetch.");
637:
638: /*
639: * Try HTTP URL-style arguments first.
640: */
1.22 deraadt 641: if (strncasecmp(line, HTTP_URL, sizeof(HTTP_URL) - 1) == 0 ||
642: strncasecmp(line, FILE_URL, sizeof(FILE_URL) - 1) == 0) {
1.54 ! fgsch 643: redirect_loop = 0;
1.17 millert 644: if (url_get(line, httpproxy, outfile) == -1)
1.1 millert 645: rval = argpos + 1;
646: continue;
647: }
648:
649: /*
1.6 millert 650: * Try FTP URL-style arguments next. If ftpproxy is
651: * set, use url_get() instead of standard ftp.
652: * Finally, try host:file.
1.1 millert 653: */
654: host = line;
1.7 millert 655: if (strncasecmp(line, FTP_URL, sizeof(FTP_URL) - 1) == 0) {
1.37 heko 656: char *passend, *passagain, *userend;
1.31 itojun 657:
1.6 millert 658: if (ftpproxy) {
1.17 millert 659: if (url_get(line, ftpproxy, outfile) == -1)
1.6 millert 660: rval = argpos + 1;
661: continue;
662: }
1.1 millert 663: host += sizeof(FTP_URL) - 1;
1.8 millert 664: dir = strchr(host, '/');
1.1 millert 665:
1.8 millert 666: /* Look for [user:pass@]host[:port] */
1.31 itojun 667:
668: /* check if we have "user:pass@" */
1.37 heko 669: userend = strchr(host, ':');
1.31 itojun 670: passend = strchr(host, '@');
671: if (passend && userend && userend < passend &&
672: (!dir || passend < dir)) {
673: user = host;
674: pass = userend + 1;
675: host = passend + 1;
676: *userend = *passend = '\0';
1.37 heko 677: passagain = strchr(host, '@');
1.42 deraadt 678: if (strchr(pass, '@') != NULL ||
1.37 heko 679: (passagain != NULL && passagain < dir)) {
680: warnx(at_encoding_warning);
681: goto bad_ftp_url;
1.42 deraadt 682: }
1.31 itojun 683:
684: if (EMPTYSTRING(user) || EMPTYSTRING(pass)) {
1.11 millert 685: bad_ftp_url:
1.31 itojun 686: warnx("Invalid URL: %s", argv[argpos]);
687: rval = argpos + 1;
688: continue;
689: }
1.37 heko 690: user = urldecode(user);
691: pass = urldecode(pass);
1.8 millert 692: }
1.31 itojun 693:
694: #ifdef INET6
695: /* check [host]:port, or [host] */
696: if (host[0] == '[') {
697: cp = strchr(host, ']');
698: if (cp && (!dir || cp < dir)) {
699: if (cp + 1 == dir || cp[1] == ':') {
700: host++;
701: *cp++ = '\0';
702: } else
703: cp = NULL;
704: } else
705: cp = host;
1.25 itojun 706: } else
707: cp = host;
1.31 itojun 708: #else
709: cp = host;
1.25 itojun 710: #endif
1.31 itojun 711:
712: /* split off host[:port] if there is */
713: if (cp) {
714: portnum = strchr(cp, ':');
1.52 henning 715: pathstart = strchr(cp, '/');
716: /* : in path is not a port # indicator */
717: if (portnum && pathstart &&
718: pathstart < portnum)
719: portnum = NULL;
720:
1.31 itojun 721: if (!portnum)
722: ;
723: else {
724: if (!dir)
725: ;
726: else if (portnum + 1 < dir) {
727: *portnum++ = '\0';
728: /*
729: * XXX should check if portnum
730: * is decimal number
731: */
732: } else {
733: /* empty portnum */
734: goto bad_ftp_url;
735: }
736: }
737: } else
738: portnum = NULL;
1.8 millert 739: } else { /* classic style `host:file' */
740: dir = strchr(host, ':');
741: }
1.1 millert 742: if (EMPTYSTRING(host)) {
743: rval = argpos + 1;
744: continue;
745: }
746:
747: /*
1.9 millert 748: * If dir is NULL, the file wasn't specified
1.1 millert 749: * (URL looked something like ftp://host)
750: */
1.8 millert 751: if (dir != NULL)
752: *dir++ = '\0';
1.1 millert 753:
754: /*
755: * Extract the file and (if present) directory name.
756: */
1.42 deraadt 757: if (!EMPTYSTRING(dir)) {
1.8 millert 758: cp = strrchr(dir, '/');
1.1 millert 759: if (cp != NULL) {
760: *cp++ = '\0';
761: file = cp;
762: } else {
763: file = dir;
764: dir = NULL;
765: }
766: }
767: if (debug)
1.42 deraadt 768: fprintf(ttyout,
769: "user %s:%s host %s port %s dir %s file %s\n",
1.8 millert 770: user, pass, host, portnum, dir, file);
1.1 millert 771:
772: /*
1.49 krw 773: * Set up the connection.
1.1 millert 774: */
1.49 krw 775: if (connected)
776: disconnect(0, NULL);
777: xargv[0] = __progname;
778: xargv[1] = host;
1.8 millert 779: xargv[2] = NULL;
1.49 krw 780: xargc = 2;
781: if (!EMPTYSTRING(portnum)) {
782: xargv[2] = portnum;
783: xargv[3] = NULL;
784: xargc = 3;
785: }
786: oautologin = autologin;
787: if (user != NULL)
788: autologin = 0;
789: setpeer(xargc, xargv);
790: autologin = oautologin;
791: if ((connected == 0) ||
792: ((connected == 1) && !ftp_login(host, user, pass))) {
793: warnx("Can't connect or login to host `%s'", host);
1.8 millert 794: rval = argpos + 1;
795: continue;
1.1 millert 796: }
1.49 krw 797:
798: /* Always use binary transfers. */
799: setbinary(0, NULL);
1.1 millert 800:
1.4 millert 801: dirhasglob = filehasglob = 0;
802: if (doglob) {
1.42 deraadt 803: if (!EMPTYSTRING(dir) &&
1.4 millert 804: strpbrk(dir, "*?[]{}") != NULL)
805: dirhasglob = 1;
1.42 deraadt 806: if (!EMPTYSTRING(file) &&
1.4 millert 807: strpbrk(file, "*?[]{}") != NULL)
808: filehasglob = 1;
809: }
810:
1.1 millert 811: /* Change directories, if necessary. */
1.42 deraadt 812: if (!EMPTYSTRING(dir) && !dirhasglob) {
1.1 millert 813: xargv[0] = "cd";
814: xargv[1] = dir;
815: xargv[2] = NULL;
816: cd(2, xargv);
1.42 deraadt 817: if (!dirchange) {
1.1 millert 818: rval = argpos + 1;
819: continue;
820: }
821: }
822:
823: if (EMPTYSTRING(file)) {
824: rval = -1;
825: continue;
826: }
827:
1.21 marc 828: if (verbose)
1.10 deraadt 829: fprintf(ttyout, "Retrieving %s/%s\n", dir ? dir : "", file);
1.1 millert 830:
1.4 millert 831: if (dirhasglob) {
832: snprintf(rempath, sizeof(rempath), "%s/%s", dir, file);
833: file = rempath;
834: }
835:
836: /* Fetch the file(s). */
1.10 deraadt 837: xargc = 2;
1.1 millert 838: xargv[0] = "get";
839: xargv[1] = file;
840: xargv[2] = NULL;
1.4 millert 841: if (dirhasglob || filehasglob) {
842: int ointeractive;
843:
844: ointeractive = interactive;
845: interactive = 0;
846: xargv[0] = "mget";
1.10 deraadt 847: mget(xargc, xargv);
1.5 millert 848: interactive = ointeractive;
1.10 deraadt 849: } else {
1.17 millert 850: if (outfile != NULL) {
851: xargv[2] = outfile;
852: xargv[3] = NULL;
1.10 deraadt 853: xargc++;
854: }
855: get(xargc, xargv);
856: }
1.1 millert 857:
1.4 millert 858: if ((code / 100) != COMPLETE)
1.1 millert 859: rval = argpos + 1;
860: }
861: if (connected && rval != -1)
862: disconnect(0, NULL);
863: return (rval);
1.37 heko 864: }
865:
866: char *
1.50 deraadt 867: urldecode(const char *str)
1.37 heko 868: {
1.53 deraadt 869: char *ret, c;
870: int i, reallen;
1.37 heko 871:
1.53 deraadt 872: if (str == NULL)
873: return NULL;
874: if ((ret = malloc(strlen(str)+1)) == NULL)
875: err(1, "Can't allocate memory for URL decoding");
876: for (i = 0, reallen = 0; str[i] != '\0'; i++, reallen++, ret++) {
877: c = str[i];
878: if (c == '+') {
879: *ret = ' ';
880: continue;
881: }
882: /* Can't use strtol here because next char after %xx may be
883: * a digit. */
884: if (c == '%' && isxdigit(str[i+1]) && isxdigit(str[i+2])) {
885: *ret = hextochar(&str[i+1]);
886: i+=2;
887: continue;
888: }
889: *ret = c;
890: }
891: *ret = '\0';
892:
893: return ret-reallen;
1.37 heko 894: }
895:
896: char
1.50 deraadt 897: hextochar(const char *str)
1.37 heko 898: {
1.53 deraadt 899: char c, ret;
1.37 heko 900:
1.53 deraadt 901: c = str[0];
902: ret = c;
903: if (isalpha(c))
904: ret -= isupper(c) ? 'A' - 10 : 'a' - 10;
905: else
906: ret -= '0';
907: ret *= 16;
908:
909: c = str[1];
910: ret += c;
911: if (isalpha(c))
912: ret -= isupper(c) ? 'A' - 10 : 'a' - 10;
913: else
914: ret -= '0';
915: return ret;
1.25 itojun 916: }
917:
918: int
1.50 deraadt 919: isurl(const char *p)
1.25 itojun 920: {
1.27 millert 921:
1.26 deraadt 922: if (strncasecmp(p, FTP_URL, sizeof(FTP_URL) - 1) == 0 ||
923: strncasecmp(p, HTTP_URL, sizeof(HTTP_URL) - 1) == 0 ||
1.27 millert 924: strncasecmp(p, FILE_URL, sizeof(FILE_URL) - 1) == 0 ||
925: strstr(p, ":/"))
926: return (1);
927: return (0);
1.1 millert 928: }