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