Home | History | Annotate | Line # | Download | only in ftp
fetch.c revision 1.10
      1 /*	$NetBSD: fetch.c,v 1.10 1997/05/23 18:54:18 lukem Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason Thorpe and Luke Mewburn.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
     30  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #ifndef lint
     40 static char rcsid[] = "$NetBSD: fetch.c,v 1.10 1997/05/23 18:54:18 lukem 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 
     51 #include <netinet/in.h>
     52 
     53 #include <arpa/ftp.h>
     54 #include <arpa/inet.h>
     55 
     56 #include <ctype.h>
     57 #include <err.h>
     58 #include <netdb.h>
     59 #include <fcntl.h>
     60 #include <signal.h>
     61 #include <stdio.h>
     62 #include <stdlib.h>
     63 #include <string.h>
     64 #include <unistd.h>
     65 
     66 #include "ftp_var.h"
     67 
     68 #define	FTP_URL		"ftp://"	/* ftp URL prefix */
     69 #define	HTTP_URL	"http://"	/* http URL prefix */
     70 #define FTP_PROXY	"ftp_proxy"	/* env var with ftp proxy location */
     71 #define HTTP_PROXY	"http_proxy"	/* env var with http proxy location */
     72 
     73 
     74 #define EMPTYSTRING(x)	((x) == NULL || (*(x) == '\0'))
     75 
     76 jmp_buf	httpabort;
     77 
     78 /*
     79  * Retrieve URL, via the proxy in $proxyvar if necessary.
     80  * Modifies the string argument given.
     81  * Returns -1 on failure, 0 on success
     82  */
     83 int
     84 url_get(origline, proxyenv)
     85 	const char *origline;
     86 	const char *proxyenv;
     87 {
     88 	struct sockaddr_in sin;
     89 	int i, out, port, s;
     90 	size_t buflen, len;
     91 	char c, *cp, *cp2, *savefile, *portnum, *path, buf[4096];
     92 	char *line, *proxy, *host;
     93 	sig_t oldintr;
     94 	off_t hashbytes;
     95 
     96 	s = -1;
     97 	proxy = NULL;
     98 
     99 	line = strdup(origline);
    100 	if (line == NULL)
    101 		errx(1, "Can't allocate memory to parse URL");
    102 	if (strncasecmp(line, HTTP_URL, sizeof(HTTP_URL) - 1) == 0)
    103 		host = line + sizeof(HTTP_URL) - 1;
    104 	else if (strncasecmp(line, FTP_URL, sizeof(FTP_URL) - 1) == 0)
    105 		host = line + sizeof(FTP_URL) - 1;
    106 	else
    107 		errx(1, "url_get: Invalid URL '%s'", line);
    108 
    109 	path = strchr(host, '/');		/* find path */
    110 	if (EMPTYSTRING(path)) {
    111 		warnx("Invalid URL: %s", origline);
    112 		goto cleanup_url_get;
    113 	}
    114 	*path++ = '\0';
    115 	if (EMPTYSTRING(path)) {
    116 		warnx("Invalid URL: %s", origline);
    117 		goto cleanup_url_get;
    118 	}
    119 
    120 	savefile = strrchr(path, '/');			/* find savefile */
    121 	if (savefile != NULL)
    122 		savefile++;
    123 	else
    124 		savefile = path;
    125 	if (EMPTYSTRING(savefile)) {
    126 		warnx("Invalid URL: %s", origline);
    127 		goto cleanup_url_get;
    128 	}
    129 
    130 	if (proxyenv != NULL) {				/* use proxy */
    131 		proxy = strdup(proxyenv);
    132 		if (proxy == NULL)
    133 			errx(1, "Can't allocate memory for proxy URL.");
    134 		if (strncasecmp(proxy, HTTP_URL, sizeof(HTTP_URL) - 1) == 0)
    135 			host = proxy + sizeof(HTTP_URL) - 1;
    136 		else if (strncasecmp(proxy, FTP_URL, sizeof(FTP_URL) - 1) == 0)
    137 			host = proxy + sizeof(FTP_URL) - 1;
    138 		else {
    139 			warnx("Malformed proxy URL: %s", proxyenv);
    140 			goto cleanup_url_get;
    141 		}
    142 		if (EMPTYSTRING(host)) {
    143 			warnx("Malformed proxy URL: %s", proxyenv);
    144 			goto cleanup_url_get;
    145 		}
    146 		*--path = '/';			/* add / back to real path */
    147 		path = strchr(host, '/');	/* remove trailing / on host */
    148 		if (! EMPTYSTRING(path))
    149 			*path++ = '\0';
    150 		path = line;
    151 	}
    152 
    153 	portnum = strchr(host, ':');			/* find portnum */
    154 	if (portnum != NULL)
    155 		*portnum++ = '\0';
    156 
    157 	if (debug)
    158 		printf("host %s, port %s, path %s, save as %s.\n",
    159 		    host, portnum, path, savefile);
    160 
    161 	memset(&sin, 0, sizeof(sin));
    162 	sin.sin_family = AF_INET;
    163 
    164 	if (isdigit(host[0])) {
    165 		if (inet_aton(host, &sin.sin_addr) == 0) {
    166 			warnx("Invalid IP address: %s", host);
    167 			goto cleanup_url_get;
    168 		}
    169 	} else {
    170 		struct hostent *hp;
    171 
    172 		hp = gethostbyname(host);
    173 		if (hp == NULL) {
    174 			warnx("%s: %s", host, hstrerror(h_errno));
    175 			goto cleanup_url_get;
    176 		}
    177 		if (hp->h_addrtype != AF_INET) {
    178 			warnx("%s: not an Internet address?", host);
    179 			goto cleanup_url_get;
    180 		}
    181 		memcpy(&sin.sin_addr, hp->h_addr, hp->h_length);
    182 	}
    183 
    184 	if (! EMPTYSTRING(portnum)) {
    185 		port = atoi(portnum);
    186 		if (port < 1 || (port & 0xffff) != port) {
    187 			warnx("Invalid port: %s", portnum);
    188 			goto cleanup_url_get;
    189 		}
    190 		port = htons(port);
    191 	} else
    192 		port = httpport;
    193 	sin.sin_port = port;
    194 
    195 	s = socket(AF_INET, SOCK_STREAM, 0);
    196 	if (s == -1) {
    197 		warn("Can't create socket");
    198 		goto cleanup_url_get;
    199 	}
    200 
    201 	if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
    202 		warn("Can't connect to %s", host);
    203 		goto cleanup_url_get;
    204 	}
    205 
    206 	/*
    207 	 * Construct and send the request.  We're expecting a return
    208 	 * status of "200". Proxy requests don't want leading /.
    209 	 */
    210 	if (!proxy)
    211 		printf("Requesting %s\n", origline);
    212 	else
    213 		printf("Requesting %s (via %s)\n", origline, proxyenv);
    214 	snprintf(buf, sizeof(buf), "GET %s%s HTTP/1.0\n\n",
    215 	    proxy ? "" : "/", path);
    216 	buflen = strlen(buf);
    217 	if (write(s, buf, buflen) < buflen) {
    218 		warn("Writing HTTP request");
    219 		goto cleanup_url_get;
    220 	}
    221 	memset(buf, 0, sizeof(buf));
    222 	for (i = 0, buflen = sizeof(buf), cp = buf; i < buflen; cp++, i++) {
    223 		if (read(s, cp, 1) != 1)
    224 			goto improper;
    225 		if (*cp == '\r')
    226 			continue;
    227 		if (*cp == '\n')
    228 			break;
    229 	}
    230 	buf[buflen - 1] = '\0';		/* sanity */
    231 	cp = strchr(buf, ' ');
    232 	if (cp == NULL)
    233 		goto improper;
    234 	else
    235 		cp++;
    236 	if (strncmp(cp, "200", 3)) {
    237 		warnx("Error retrieving file: %s", cp);
    238 		goto cleanup_url_get;
    239 	}
    240 
    241 	/*
    242 	 * Read the rest of the header.
    243 	 */
    244 	memset(buf, 0, sizeof(buf));
    245 	c = '\0';
    246 	for (i = 0, buflen = sizeof(buf), cp = buf; i < buflen; cp++, i++) {
    247 		if (read(s, cp, 1) != 1)
    248 			goto improper;
    249 		if (*cp == '\r')
    250 			continue;
    251 		if (*cp == '\n' && c == '\n')
    252 			break;
    253 		c = *cp;
    254 	}
    255 	buf[buflen - 1] = '\0';		/* sanity */
    256 
    257 	/*
    258 	 * Look for the "Content-length: " header.
    259 	 */
    260 #define CONTENTLEN "Content-Length: "
    261 	for (cp = buf; *cp != '\0'; cp++) {
    262 		if (tolower(*cp) == 'c' &&
    263 		    strncasecmp(cp, CONTENTLEN, sizeof(CONTENTLEN) - 1) == 0)
    264 			break;
    265 	}
    266 	if (*cp == '\0')
    267 		goto improper;
    268 	cp += sizeof(CONTENTLEN) - 1;
    269 	cp2 = strchr(cp, '\n');
    270 	if (cp2 == NULL)
    271 		goto improper;
    272 	else
    273 		*cp2 = '\0';
    274 	filesize = atoi(cp);
    275 	if (filesize < 1)
    276 		goto improper;
    277 
    278 	/* Open the output file. */
    279 	out = open(savefile, O_CREAT | O_WRONLY | O_TRUNC, 0666);
    280 	if (out < 0) {
    281 		warn("Can't open %s", savefile);
    282 		goto cleanup_url_get;
    283 	}
    284 
    285 	/* Trap signals */
    286 	oldintr = NULL;
    287 	if (setjmp(httpabort)) {
    288 		if (oldintr)
    289 			(void)signal(SIGINT, oldintr);
    290 		goto cleanup_url_get;
    291 	}
    292 	oldintr = signal(SIGINT, aborthttp);
    293 
    294 	bytes = 0;
    295 	hashbytes = mark;
    296 	progressmeter(-1);
    297 
    298 	/* Finally, suck down the file. */
    299 	i = 0;
    300 	while ((len = read(s, buf, sizeof(buf))) > 0) {
    301 		bytes += len;
    302 		for (cp = buf; len > 0; len -= i, cp += i) {
    303 			if ((i = write(out, cp, len)) == -1) {
    304 				warn("Writing %s", savefile);
    305 				goto cleanup_url_get;
    306 			}
    307 			else if (i == 0)
    308 				break;
    309 		}
    310 		if (hash && !progress) {
    311 			while (bytes >= hashbytes) {
    312 				(void)putchar('#');
    313 				hashbytes += mark;
    314 			}
    315 			(void)fflush(stdout);
    316 		}
    317 	}
    318 	if (hash && !progress && bytes > 0) {
    319 		if (bytes < mark)
    320 			(void)putchar('#');
    321 		(void)putchar('\n');
    322 		(void)fflush(stdout);
    323 	}
    324 	if (len != 0) {
    325 		warn("Reading from socket");
    326 		goto cleanup_url_get;
    327 	}
    328 	progressmeter(1);
    329 	if (verbose)
    330 		puts("Successfully retrieved file.");
    331 	(void)signal(SIGINT, oldintr);
    332 
    333 	close(s);
    334 	close(out);
    335 	if (proxy)
    336 		free(proxy);
    337 	free(line);
    338 	return (0);
    339 
    340 improper:
    341 	warnx("Improper response from %s", host);
    342 cleanup_url_get:
    343 	if (s != -1)
    344 		close(s);
    345 	if (proxy)
    346 		free(proxy);
    347 	free(line);
    348 	return (-1);
    349 }
    350 
    351 /*
    352  * Abort a http retrieval
    353  */
    354 void
    355 aborthttp(notused)
    356 	int notused;
    357 {
    358 
    359 	alarmtimer(0);
    360 	puts("\nhttp fetch aborted.");
    361 	(void)fflush(stdout);
    362 	longjmp(httpabort, 1);
    363 }
    364 
    365 /*
    366  * Retrieve multiple files from the command line, transferring
    367  * files of the form "host:path", "ftp://host/path" using the
    368  * ftp protocol, and files of the form "http://host/path" using
    369  * the http protocol.
    370  * If path has a trailing "/", then return (-1);
    371  * the path will be cd-ed into and the connection remains open,
    372  * and the function will return -1 (to indicate the connection
    373  * is alive).
    374  * If an error occurs the return value will be the offset+1 in
    375  * argv[] of the file that caused a problem (i.e, argv[x]
    376  * returns x+1)
    377  * Otherwise, 0 is returned if all files retrieved successfully.
    378  */
    379 int
    380 auto_fetch(argc, argv)
    381 	int argc;
    382 	char *argv[];
    383 {
    384 	static char lasthost[MAXHOSTNAMELEN];
    385 	char *xargv[5];
    386 	char *cp, *line, *host, *dir, *file, *portnum;
    387 	char *user, *pass;
    388 	char *ftpproxy, *httpproxy;
    389 	int rval, xargc, argpos;
    390 	int dirhasglob, filehasglob;
    391 	char rempath[MAXPATHLEN];
    392 
    393 	argpos = 0;
    394 
    395 	if (setjmp(toplevel)) {
    396 		if (connected)
    397 			disconnect(0, NULL);
    398 		return (argpos + 1);
    399 	}
    400 	(void)signal(SIGINT, (sig_t)intr);
    401 	(void)signal(SIGPIPE, (sig_t)lostpeer);
    402 
    403 	ftpproxy = getenv(FTP_PROXY);
    404 	httpproxy = getenv(HTTP_PROXY);
    405 
    406 	/*
    407 	 * Loop through as long as there's files to fetch.
    408 	 */
    409 	for (rval = 0; (rval == 0) && (argpos < argc); free(line), argpos++) {
    410 		if (strchr(argv[argpos], ':') == NULL)
    411 			break;
    412 		host = dir = file = portnum = user = pass = NULL;
    413 
    414 		/*
    415 		 * We muck with the string, so we make a copy.
    416 		 */
    417 		line = strdup(argv[argpos]);
    418 		if (line == NULL)
    419 			errx(1, "Can't allocate memory for auto-fetch.");
    420 
    421 		/*
    422 		 * Try HTTP URL-style arguments first.
    423 		 */
    424 		if (strncasecmp(line, HTTP_URL, sizeof(HTTP_URL) - 1) == 0) {
    425 			if (url_get(line, httpproxy) == -1)
    426 				rval = argpos + 1;
    427 			continue;
    428 		}
    429 
    430 		/*
    431 		 * Try FTP URL-style arguments next. If ftpproxy is
    432 		 * set, use url_get() instead of standard ftp.
    433 		 * Finally, try host:file.
    434 		 */
    435 		host = line;
    436 		if (strncasecmp(line, FTP_URL, sizeof(FTP_URL) - 1) == 0) {
    437 			if (ftpproxy) {
    438 				if (url_get(line, ftpproxy) == -1)
    439 					rval = argpos + 1;
    440 				continue;
    441 			}
    442 			host += sizeof(FTP_URL) - 1;
    443 			dir = strchr(host, '/');
    444 
    445 				/* look for [user:pass@]host[:port] */
    446 			pass = strpbrk(host, ":@/");
    447 			if (pass == NULL || *pass == '/') {
    448 				pass = NULL;
    449 				goto parsed_url;
    450 			}
    451 			if (pass == host || *pass == '@') {
    452 bad_ftp_url:
    453 				warnx("Invalid URL: %s", argv[argpos]);
    454 				rval = argpos + 1;
    455 				continue;
    456 			}
    457 			*pass++ = '\0';
    458 			cp = strpbrk(pass, ":@/");
    459 			if (cp == NULL || *cp == '/') {
    460 				portnum = pass;
    461 				pass = NULL;
    462 				goto parsed_url;
    463 			}
    464 			if (EMPTYSTRING(cp) || *cp == ':')
    465 				goto bad_ftp_url;
    466 			*cp++ = '\0';
    467 			user = host;
    468 			if (EMPTYSTRING(user))
    469 				goto bad_ftp_url;
    470 			host = cp;
    471 			portnum = strchr(host, ':');
    472 			if (portnum != NULL)
    473 				*portnum++ = '\0';
    474 parsed_url:
    475 		} else {			/* classic style `host:file' */
    476 			dir = strchr(host, ':');
    477 		}
    478 		if (EMPTYSTRING(host)) {
    479 			rval = argpos + 1;
    480 			continue;
    481 		}
    482 
    483 		/*
    484 		 * If cp is NULL, the file wasn't specified
    485 		 * (URL looked something like ftp://host)
    486 		 */
    487 		if (dir != NULL)
    488 			*dir++ = '\0';
    489 
    490 		/*
    491 		 * Extract the file and (if present) directory name.
    492 		 */
    493 		if (! EMPTYSTRING(dir)) {
    494 			cp = strrchr(dir, '/');
    495 			if (cp != NULL) {
    496 				*cp++ = '\0';
    497 				file = cp;
    498 			} else {
    499 				file = dir;
    500 				dir = NULL;
    501 			}
    502 		}
    503 		if (debug)
    504 			printf("user %s:%s host %s port %s dir %s file %s\n",
    505 			    user, pass, host, portnum, dir, file);
    506 
    507 		/*
    508 		 * Set up the connection if we don't have one.
    509 		 */
    510 		if (strcmp(host, lasthost) != 0) {
    511 			int oautologin;
    512 
    513 			(void)strcpy(lasthost, host);
    514 			if (connected)
    515 				disconnect(0, NULL);
    516 			xargv[0] = __progname;
    517 			xargv[1] = host;
    518 			xargv[2] = NULL;
    519 			xargc = 2;
    520 			if (! EMPTYSTRING(portnum)) {
    521 				xargv[2] = portnum;
    522 				xargv[3] = NULL;
    523 				xargc = 3;
    524 			}
    525 			oautologin = autologin;
    526 			if (user != NULL)
    527 				autologin = 0;
    528 			setpeer(xargc, xargv);
    529 			autologin = oautologin;
    530 			if ((connected == 0)
    531 			 || ((connected == 1) && !login(host, user, pass)) ) {
    532 				warnx("Can't connect or login to host `%s'",
    533 				    host);
    534 				rval = argpos + 1;
    535 				continue;
    536 			}
    537 
    538 			/* Always use binary transfers. */
    539 			setbinary(0, NULL);
    540 		}
    541 			/* cd back to '/' */
    542 		xargv[0] = "cd";
    543 		xargv[1] = "/";
    544 		xargv[2] = NULL;
    545 		cd(2, xargv);
    546 		if (! dirchange) {
    547 			rval = argpos + 1;
    548 			continue;
    549 		}
    550 
    551 		dirhasglob = filehasglob = 0;
    552 		if (doglob) {
    553 			if (! EMPTYSTRING(dir) &&
    554 			    strpbrk(dir, "*?[]{}") != NULL)
    555 				dirhasglob = 1;
    556 			if (! EMPTYSTRING(file) &&
    557 			    strpbrk(file, "*?[]{}") != NULL)
    558 				filehasglob = 1;
    559 		}
    560 
    561 		/* Change directories, if necessary. */
    562 		if (! EMPTYSTRING(dir) && !dirhasglob) {
    563 			xargv[0] = "cd";
    564 			xargv[1] = dir;
    565 			xargv[2] = NULL;
    566 			cd(2, xargv);
    567 			if (! dirchange) {
    568 				rval = argpos + 1;
    569 				continue;
    570 			}
    571 		}
    572 
    573 		if (EMPTYSTRING(file)) {
    574 			rval = -1;
    575 			continue;
    576 		}
    577 
    578 		if (!verbose)
    579 			printf("Retrieving %s/%s\n", dir ? dir : "", file);
    580 
    581 		if (dirhasglob) {
    582 			snprintf(rempath, sizeof(rempath), "%s/%s", dir, file);
    583 			file = rempath;
    584 		}
    585 
    586 		/* Fetch the file(s). */
    587 		xargv[0] = "get";
    588 		xargv[1] = file;
    589 		xargv[2] = NULL;
    590 		if (dirhasglob || filehasglob) {
    591 			int ointeractive;
    592 
    593 			ointeractive = interactive;
    594 			interactive = 0;
    595 			xargv[0] = "mget";
    596 			mget(2, xargv);
    597 			interactive = ointeractive;
    598 		} else
    599 			get(2, xargv);
    600 
    601 		if ((code / 100) != COMPLETE)
    602 			rval = argpos + 1;
    603 	}
    604 	if (connected && rval != -1)
    605 		disconnect(0, NULL);
    606 	return (rval);
    607 }
    608