Home | History | Annotate | Line # | Download | only in ftp
fetch.c revision 1.31
      1 /*	$NetBSD: fetch.c,v 1.31 1998/08/08 03:33:20 lukem Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997, 1998 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 FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 #ifndef lint
     41 __RCSID("$NetBSD: fetch.c,v 1.31 1998/08/08 03:33:20 lukem Exp $");
     42 #endif /* not lint */
     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>
     51 #include <sys/stat.h>
     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>
     60 #include <errno.h>
     61 #include <netdb.h>
     62 #include <fcntl.h>
     63 #include <signal.h>
     64 #include <stdio.h>
     65 #include <stdlib.h>
     66 #include <string.h>
     67 #include <unistd.h>
     68 #include <util.h>
     69 
     70 #include "ftp_var.h"
     71 
     72 typedef enum {
     73 	UNKNOWN_URL_T=-1,
     74 	HTTP_URL_T,
     75 	FTP_URL_T,
     76 	FILE_URL_T
     77 } url_t;
     78 
     79 static int	parse_url __P((const char *, const char *, url_t *, char **,
     80 				char **, char **, in_port_t *, char **));
     81 static int	url_get __P((const char *, const char *, const char *));
     82 void    	aborthttp __P((int));
     83 
     84 
     85 #define	ABOUT_URL	"about:"	/* propaganda */
     86 #define	FILE_URL	"file://"	/* file URL prefix */
     87 #define	FTP_URL		"ftp://"	/* ftp URL prefix */
     88 #define	HTTP_URL	"http://"	/* http URL prefix */
     89 #define FTP_PROXY	"ftp_proxy"	/* env var with ftp proxy location */
     90 #define HTTP_PROXY	"http_proxy"	/* env var with http proxy location */
     91 #define NO_PROXY	"no_proxy"	/* env var with list of non-proxied
     92 					 * hosts, comma or space separated */
     93 
     94 
     95 #define EMPTYSTRING(x)	((x) == NULL || (*(x) == '\0'))
     96 #define FREEPTR(x)	if ((x) != NULL) { free(x); (x) = NULL; }
     97 
     98 /*
     99  * Parse URL of form:
    100  *	<type>://[<user>[:<password>@]]<host>[:<port>]/<url-path>
    101  * Returns -1 if a parse error occurred, otherwise 0.
    102  * Sets type to url_t, each of the given char ** pointers to a
    103  * malloc(3)ed strings of the relevant section, and port to
    104  * 0 if not given, or the number given.
    105  */
    106 static int
    107 parse_url(url, desc, type, user, pass, host, port, path)
    108 	const char	 *url;
    109 	const char	 *desc;
    110 	url_t		 *type;
    111 	char		**user;
    112 	char		**pass;
    113 	char		**host;
    114 	in_port_t	 *port;
    115 	char		**path;
    116 {
    117 	char *cp, *ep, *thost;
    118 
    119 	if (url == NULL || desc == NULL || type == NULL || user == NULL
    120 	    || pass == NULL || host == NULL || port == NULL || path == NULL)
    121 		errx(1, "parse_url: invoked with NULL argument!");
    122 
    123 	*type = UNKNOWN_URL_T;
    124 	*user = *pass = *host = *path = NULL;
    125 	*port = 0;
    126 
    127 	if (strncasecmp(url, HTTP_URL, sizeof(HTTP_URL) - 1) == 0) {
    128 		url += sizeof(HTTP_URL) - 1;
    129 		*type = HTTP_URL_T;
    130 	} else if (strncasecmp(url, FTP_URL, sizeof(FTP_URL) - 1) == 0) {
    131 		url += sizeof(FTP_URL) - 1;
    132 		*type = FTP_URL_T;
    133 	} else if (strncasecmp(url, FILE_URL, sizeof(FILE_URL) - 1) == 0) {
    134 		url += sizeof(FILE_URL) - 1;
    135 		*type = FILE_URL_T;
    136 	} else {
    137 		warnx("Invalid %s `%s'", desc, url);
    138 cleanup_parse_url:
    139 		FREEPTR(*user);
    140 		FREEPTR(*pass);
    141 		FREEPTR(*host);
    142 		FREEPTR(*path);
    143 		return (-1);
    144 	}
    145 
    146 	if (*url == '\0')
    147 		return (0);
    148 
    149 			/* find [user[:pass]@]host[:port] */
    150 	ep = strchr(url, '/');
    151 	if (ep == NULL)
    152 		thost = xstrdup(url);
    153 	else {
    154 		size_t len = ep - url;
    155 		thost = (char *)xmalloc(len + 1);
    156 		strncpy(thost, url, len);
    157 		thost[len] = '\0';
    158 		*path = xstrdup(ep);
    159 	}
    160 
    161 	cp = strchr(thost, '@');
    162 	if (cp != NULL) {
    163 		*user = thost;
    164 		*cp = '\0';
    165 		*host = xstrdup(cp + 1);
    166 		cp = strchr(*user, ':');
    167 		if (cp != NULL) {
    168 			*cp = '\0';
    169 			*pass = xstrdup(cp + 1);
    170 		}
    171 	} else
    172 		*host = thost;
    173 
    174 			/* look for [:port] */
    175 	cp = strrchr(*host, ':');
    176 	if (cp != NULL) {
    177 		long nport;
    178 
    179 		*cp = '\0';
    180 		nport = strtol(cp + 1, &ep, 10);
    181 		if (nport < 1 || nport > MAX_IN_PORT_T || *ep != '\0') {
    182 			warnx("Invalid port `%s' in %s `%s'", cp, desc, line);
    183 			goto cleanup_parse_url;
    184 		}
    185 		*port = htons((in_port_t)nport);
    186 	}
    187 
    188 	if (debug)
    189 		fprintf(ttyout,
    190 		    "parse_url: user `%s', pass `%s', host %s:%d, path `%s'\n",
    191 		    *user ? *user : "", *pass ? *pass : "", *host ? *host : "",
    192 		    ntohs(*port), *path ? *path : "");
    193 
    194 	return (0);
    195 }
    196 
    197 
    198 jmp_buf	httpabort;
    199 
    200 /*
    201  * Retrieve URL, via the proxy in $proxyvar if necessary.
    202  * Modifies the string argument given.
    203  * Returns -1 on failure, 0 on success
    204  */
    205 static int
    206 url_get(url, proxyenv, outfile)
    207 	const char *url;
    208 	const char *proxyenv;
    209 	const char *outfile;
    210 {
    211 	struct sockaddr_in sin;
    212 	int isredirected, isproxy;
    213 	volatile int s;
    214 	size_t len;
    215 	char *cp, *ep;
    216 	char *buf, *savefile;
    217 	volatile sig_t oldintr, oldintp;
    218 	off_t hashbytes;
    219 	struct hostent *hp = NULL;
    220 	int (*closefunc) __P((FILE *));
    221 	FILE *fin, *fout;
    222 	int retval;
    223 	time_t mtime;
    224 	url_t urltype;
    225 	char *user, *pass, *host;
    226 	in_port_t port;
    227 	char *path;
    228 
    229 	closefunc = NULL;
    230 	fin = fout = NULL;
    231 	s = -1;
    232 	buf = savefile = NULL;
    233 	isredirected = isproxy = 0;
    234 	retval = -1;
    235 
    236 #ifdef __GNUC__			/* shut up gcc warnings */
    237 	(void)&closefunc;
    238 	(void)&fin;
    239 	(void)&fout;
    240 	(void)&buf;
    241 	(void)&savefile;
    242 	(void)&retval;
    243 	(void)&isproxy;
    244 #endif
    245 
    246 	if (parse_url(url, "URL", &urltype, &user, &pass, &host, &port, &path)
    247 	    == -1)
    248 		goto cleanup_url_get;
    249 	if (port == 0)
    250 		port = httpport;
    251 
    252 	if (urltype == FILE_URL_T && ! EMPTYSTRING(host)
    253 	    && strcasecmp(host, "localhost") != 0) {
    254 		warnx("No support for non local file URL `%s'", url);
    255 		goto cleanup_url_get;
    256 	}
    257 
    258 	if (EMPTYSTRING(path)) {
    259 		if (urltype == FTP_URL_T)
    260 			goto noftpautologin;
    261 		if (urltype != HTTP_URL_T || outfile == NULL)  {
    262 			warnx("Invalid URL (no file after host) `%s'", url);
    263 			goto cleanup_url_get;
    264 		}
    265 	}
    266 
    267 	if (outfile)
    268 		savefile = xstrdup(outfile);
    269 	else {
    270 		cp = strrchr(path, '/');		/* find savefile */
    271 		if (cp != NULL)
    272 			savefile = xstrdup(cp + 1);
    273 		else
    274 			savefile = xstrdup(path);
    275 	}
    276 	if (EMPTYSTRING(savefile)) {
    277 		if (urltype == FTP_URL_T)
    278 			goto noftpautologin;
    279 		warnx("Invalid URL (no file after directory) `%s'", url);
    280 		goto cleanup_url_get;
    281 	}
    282 
    283 	filesize = -1;
    284 	mtime = -1;
    285 	if (urltype == FILE_URL_T) {		/* file:// URLs */
    286 		struct stat sb;
    287 
    288 		direction = "copied";
    289 		fin = fopen(path, "r");
    290 		if (fin == NULL) {
    291 			warn("Cannot open file `%s'", path);
    292 			goto cleanup_url_get;
    293 		}
    294 		if (fstat(fileno(fin), &sb) == 0) {
    295 			mtime = sb.st_mtime;
    296 			filesize = sb.st_size;
    297 		}
    298 		fprintf(ttyout, "Copying %s\n", path);
    299 	} else {				/* ftp:// or http:// URLs */
    300 		direction = "retrieved";
    301 		if (proxyenv != NULL) {				/* use proxy */
    302 			url_t purltype;
    303 			char *puser, *ppass, *phost;
    304 			in_port_t pport;
    305 			char *ppath;
    306 			char *no_proxy;
    307 
    308 			isproxy = 1;
    309 
    310 				/* check URL against list of no_proxied sites */
    311 			no_proxy = getenv(NO_PROXY);
    312 			if (no_proxy != NULL) {
    313 				char *np, *np_copy;
    314 				long np_port;
    315 				size_t hlen, plen;
    316 
    317 				np_copy = xstrdup(no_proxy);
    318 				hlen = strlen(host);
    319 				while ((cp = strsep(&np_copy, " ,")) != NULL) {
    320 					if (*cp == '\0')
    321 						continue;
    322 					if ((np = strchr(cp, ':')) != NULL) {
    323 						*np = '\0';
    324 						np_port =
    325 						    strtol(np + 1, &ep, 10);
    326 						if (*ep != '\0')
    327 							continue;
    328 						if (port !=
    329 						    htons((in_port_t)np_port))
    330 							continue;
    331 					}
    332 					plen = strlen(cp);
    333 					if (strncasecmp(host + hlen - plen,
    334 					    cp, plen) == 0) {
    335 						isproxy = 0;
    336 						break;
    337 					}
    338 				}
    339 				FREEPTR(np_copy);
    340 			}
    341 
    342 			if (isproxy) {
    343 				if (parse_url(proxyenv, "proxy URL", &purltype,
    344 				    &puser, &ppass, &phost, &pport, &ppath)
    345 				    == -1)
    346 					goto cleanup_url_get;
    347 
    348 				if ((purltype != HTTP_URL_T
    349 				     && purltype != FTP_URL_T) ||
    350 				    EMPTYSTRING(phost) ||
    351 				    (! EMPTYSTRING(ppath)
    352 				     && strcmp(ppath, "/") != 0)) {
    353 					warnx("Malformed proxy URL `%s'",
    354 					    proxyenv);
    355 					FREEPTR(puser);
    356 					FREEPTR(ppass);
    357 					FREEPTR(phost);
    358 					FREEPTR(ppath);
    359 					goto cleanup_url_get;
    360 				}
    361 
    362 				FREEPTR(user);
    363 				user = puser;
    364 				FREEPTR(pass);
    365 				pass = ppass;
    366 				FREEPTR(host);
    367 				host = phost;
    368 				if (pport == 0)
    369 					port = httpport;
    370 				else
    371 					port = pport;
    372 				FREEPTR(path);
    373 				FREEPTR(ppath);
    374 				path = xstrdup(url);
    375 			}
    376 		} /* proxyenv != NULL */
    377 
    378 		memset(&sin, 0, sizeof(sin));
    379 		sin.sin_family = AF_INET;
    380 
    381 		if (isdigit((unsigned char)host[0])) {
    382 			if (inet_aton(host, &sin.sin_addr) == 0) {
    383 				warnx("Invalid IP address `%s'", host);
    384 				goto cleanup_url_get;
    385 			}
    386 		} else {
    387 			hp = gethostbyname(host);
    388 			if (hp == NULL) {
    389 				warnx("%s: %s", host, hstrerror(h_errno));
    390 				goto cleanup_url_get;
    391 			}
    392 			if (hp->h_addrtype != AF_INET) {
    393 				warnx("`%s': not an Internet address?", host);
    394 				goto cleanup_url_get;
    395 			}
    396 			memcpy(&sin.sin_addr, hp->h_addr, hp->h_length);
    397 		}
    398 
    399 		if (port == 0)
    400 			port = httpport;
    401 		sin.sin_port = port;
    402 
    403 		s = socket(AF_INET, SOCK_STREAM, 0);
    404 		if (s == -1) {
    405 			warn("Can't create socket");
    406 			goto cleanup_url_get;
    407 		}
    408 
    409 		while (xconnect(s, (struct sockaddr *)&sin,
    410 		    sizeof(sin)) == -1) {
    411 			if (errno == EINTR)
    412 				continue;
    413 			if (hp && hp->h_addr_list[1]) {
    414 				int oerrno = errno;
    415 				char *ia;
    416 
    417 				ia = inet_ntoa(sin.sin_addr);
    418 				errno = oerrno;
    419 				warn("Connect to address `%s'", ia);
    420 				hp->h_addr_list++;
    421 				memcpy(&sin.sin_addr, hp->h_addr_list[0],
    422 				    (size_t)hp->h_length);
    423 				fprintf(ttyout, "Trying %s...\n",
    424 				    inet_ntoa(sin.sin_addr));
    425 				(void)close(s);
    426 				s = socket(AF_INET, SOCK_STREAM, 0);
    427 				if (s < 0) {
    428 					warn("Can't create socket");
    429 					goto cleanup_url_get;
    430 				}
    431 				continue;
    432 			}
    433 			warn("Can't connect to `%s'", host);
    434 			goto cleanup_url_get;
    435 		}
    436 
    437 		fin = fdopen(s, "r+");
    438 		/*
    439 		 * Construct and send the request.
    440 		 * Proxy requests don't want leading /.
    441 		 */
    442 		if (isproxy) {
    443 			fprintf(ttyout, "Requesting %s\n  (via %s)\n",
    444 			    url, proxyenv);
    445 			fprintf(fin, "GET %s HTTP/1.0\r\n\r\n", path);
    446 		} else {
    447 			fprintf(ttyout, "Requesting %s\n", url);
    448 			fprintf(fin, "GET %s HTTP/1.1\r\n", path);
    449 			fprintf(fin, "Host: %s\r\n", host);
    450 			fprintf(fin, "Connection: close\r\n\r\n");
    451 		}
    452 		if (fflush(fin) == EOF) {
    453 			warn("Writing HTTP request");
    454 			goto cleanup_url_get;
    455 		}
    456 
    457 				/* Read the response */
    458 		if ((buf = fparseln(fin, &len, NULL, "\0\0\0", 0)) == NULL) {
    459 			warn("Receiving HTTP reply");
    460 			goto cleanup_url_get;
    461 		}
    462 		while (len > 0 && (buf[len-1] == '\r' || buf[len-1] == '\n'))
    463 			buf[--len] = '\0';
    464 		if (debug)
    465 			fprintf(ttyout, "received `%s'\n", buf);
    466 
    467 		cp = strchr(buf, ' ');
    468 		if (cp == NULL)
    469 			goto improper;
    470 		else
    471 			cp++;
    472 		if (strncmp(cp, "301", 3) == 0 || strncmp(cp, "302", 3) == 0) {
    473 			isredirected++;
    474 		} else if (strncmp(cp, "200", 3)) {
    475 			warnx("Error retrieving file `%s'", cp);
    476 			goto cleanup_url_get;
    477 		}
    478 
    479 				/* Read the rest of the header. */
    480 		FREEPTR(buf);
    481 		while (1) {
    482 			if ((buf = fparseln(fin, &len, NULL, "\0\0\0", 0))
    483 			    == NULL) {
    484 				warn("Receiving HTTP reply");
    485 				goto cleanup_url_get;
    486 			}
    487 			while (len > 0 &&
    488 			    (buf[len-1] == '\r' || buf[len-1] == '\n'))
    489 				buf[--len] = '\0';
    490 			if (len == 0)
    491 				break;
    492 			if (debug)
    493 				fprintf(ttyout, "received `%s'\n", buf);
    494 
    495 				/* Look for some headers */
    496 			cp = buf;
    497 #define CONTENTLEN "Content-Length: "
    498 			if (strncasecmp(cp, CONTENTLEN,
    499 			    sizeof(CONTENTLEN) - 1) == 0) {
    500 				cp += sizeof(CONTENTLEN) - 1;
    501 				filesize = strtol(cp, &ep, 10);
    502 				if (filesize < 1 || *ep != '\0')
    503 					goto improper;
    504 				if (debug)
    505 					fprintf(ttyout,
    506 #ifndef NO_QUAD
    507 					    "parsed length as: %qd\n",
    508 					    (long long)filesize);
    509 #else
    510 					    "parsed length as: %ld\n",
    511 					    (long)filesize);
    512 #endif
    513 #define LASTMOD "Last-Modified: "
    514 			} else if (strncasecmp(cp, LASTMOD,
    515 			    sizeof(LASTMOD) - 1) == 0) {
    516 				struct tm parsed;
    517 				char *t;
    518 
    519 				cp += sizeof(LASTMOD) - 1;
    520 							/* RFC 1123 */
    521 				if ((t = strptime(cp,
    522 						"%a, %d %b %Y %H:%M:%S GMT",
    523 						&parsed))
    524 							/* RFC 850 */
    525 				    || (t = strptime(cp,
    526 						"%a, %d-%b-%y %H:%M:%S GMT",
    527 						&parsed))
    528 							/* asctime */
    529 				    || (t = strptime(cp,
    530 						"%a, %b %d %H:%M:%S %Y",
    531 						&parsed))) {
    532 					if (*t == '\0')
    533 						mtime = mktime(&parsed);
    534 					if (debug && mtime != -1)
    535 						fprintf(ttyout,
    536 						    "parsed date as: %s",
    537 						    ctime(&mtime));
    538 				}
    539 #define LOCATION "Location: "
    540 			} else if (isredirected &&
    541 			    strncasecmp(cp, LOCATION,
    542 				sizeof(LOCATION) - 1) == 0) {
    543 				cp += sizeof(LOCATION) - 1;
    544 				if (debug)
    545 					fprintf(ttyout,
    546 					    "parsed location as: %s\n", cp);
    547 				if (verbose)
    548 					fprintf(ttyout,
    549 					    "Redirected to %s\n", cp);
    550 				retval = url_get(cp, proxyenv, outfile);
    551 				goto cleanup_url_get;
    552 			}
    553 		}
    554 		FREEPTR(buf);
    555 	}
    556 
    557 	oldintr = oldintp = NULL;
    558 
    559 			/* Open the output file. */
    560 	if (strcmp(savefile, "-") == 0) {
    561 		fout = stdout;
    562 	} else if (*savefile == '|') {
    563 		oldintp = signal(SIGPIPE, SIG_IGN);
    564 		fout = popen(savefile + 1, "w");
    565 		if (fout == NULL) {
    566 			warn("Can't run `%s'", savefile + 1);
    567 			goto cleanup_url_get;
    568 		}
    569 		closefunc = pclose;
    570 	} else {
    571 		fout = fopen(savefile, "w");
    572 		if (fout == NULL) {
    573 			warn("Can't open `%s'", savefile);
    574 			goto cleanup_url_get;
    575 		}
    576 		closefunc = fclose;
    577 	}
    578 
    579 			/* Trap signals */
    580 	if (setjmp(httpabort)) {
    581 		if (oldintr)
    582 			(void)signal(SIGINT, oldintr);
    583 		if (oldintp)
    584 			(void)signal(SIGPIPE, oldintp);
    585 		goto cleanup_url_get;
    586 	}
    587 	oldintr = signal(SIGINT, aborthttp);
    588 
    589 	bytes = 0;
    590 	hashbytes = mark;
    591 	progressmeter(-1);
    592 
    593 			/* Finally, suck down the file. */
    594 	buf = xmalloc(BUFSIZ);
    595 	while ((len = fread(buf, sizeof(char), BUFSIZ, fin)) > 0) {
    596 		bytes += len;
    597 		if (fwrite(buf, sizeof(char), len, fout) != len) {
    598 			warn("Writing `%s'", savefile);
    599 			goto cleanup_url_get;
    600 		}
    601 		if (hash && !progress) {
    602 			while (bytes >= hashbytes) {
    603 				(void)putc('#', ttyout);
    604 				hashbytes += mark;
    605 			}
    606 			(void)fflush(ttyout);
    607 		}
    608 	}
    609 	if (hash && !progress && bytes > 0) {
    610 		if (bytes < mark)
    611 			(void)putc('#', ttyout);
    612 		(void)putc('\n', ttyout);
    613 		(void)fflush(ttyout);
    614 	}
    615 	if (ferror(fin)) {
    616 		warn("Reading file");
    617 		goto cleanup_url_get;
    618 	}
    619 	progressmeter(1);
    620 	(void)fflush(fout);
    621 	(void)signal(SIGINT, oldintr);
    622 	if (oldintp)
    623 		(void)signal(SIGPIPE, oldintp);
    624 	if (closefunc == fclose && mtime != -1) {
    625 		struct timeval tval[2];
    626 
    627 		(void)gettimeofday(&tval[0], NULL);
    628 		tval[1].tv_sec = mtime;
    629 		tval[1].tv_usec = 0;
    630 		(*closefunc)(fout);
    631 		fout = NULL;
    632 
    633 		if (utimes(savefile, tval) == -1) {
    634 			fprintf(ttyout,
    635 			    "Can't change modification time to %s",
    636 			    asctime(localtime(&mtime)));
    637 		}
    638 	}
    639 	if (bytes > 0)
    640 		ptransfer(0);
    641 
    642 	retval = 0;
    643 	goto cleanup_url_get;
    644 
    645 noftpautologin:
    646 	warnx(
    647 	    "Auto-login using ftp URLs isn't supported when using $ftp_proxy");
    648 	goto cleanup_url_get;
    649 
    650 improper:
    651 	warnx("Improper response from `%s'", host);
    652 
    653 cleanup_url_get:
    654 	resetsockbufsize();
    655 	if (fin != NULL)
    656 		fclose(fin);
    657 	else if (s != -1)
    658 		close(s);
    659 	if (closefunc != NULL && fout != NULL)
    660 		(*closefunc)(fout);
    661 	FREEPTR(savefile);
    662 	FREEPTR(user);
    663 	FREEPTR(pass);
    664 	FREEPTR(host);
    665 	FREEPTR(path);
    666 	FREEPTR(buf);
    667 	return (retval);
    668 }
    669 
    670 /*
    671  * Abort a http retrieval
    672  */
    673 void
    674 aborthttp(notused)
    675 	int notused;
    676 {
    677 
    678 	alarmtimer(0);
    679 	fputs("\nHTTP fetch aborted.\n", ttyout);
    680 	(void)fflush(ttyout);
    681 	longjmp(httpabort, 1);
    682 }
    683 
    684 /*
    685  * Retrieve multiple files from the command line, transferring
    686  * URLs of the form "host:path", "ftp://host/path" using the
    687  * ftp protocol, URLs of the form "http://host/path" using the
    688  * http protocol, and URLs of the form "file:///" by simple
    689  * copying.
    690  * If path has a trailing "/", then return (-1);
    691  * the path will be cd-ed into and the connection remains open,
    692  * and the function will return -1 (to indicate the connection
    693  * is alive).
    694  * If an error occurs the return value will be the offset+1 in
    695  * argv[] of the file that caused a problem (i.e, argv[x]
    696  * returns x+1)
    697  * Otherwise, 0 is returned if all files retrieved successfully.
    698  */
    699 int
    700 auto_fetch(argc, argv, outfile)
    701 	int argc;
    702 	char *argv[];
    703 	char *outfile;
    704 {
    705 	static char lasthost[MAXHOSTNAMELEN];
    706 	char portnum[6];		/* large enough for "65535\0" */
    707 	char *xargv[5];
    708 	const char *line;
    709 	char *cp, *host, *path, *dir, *file;
    710 	char *user, *pass;
    711 	in_port_t port;
    712 	char *ftpproxy, *httpproxy;
    713 	int rval, xargc;
    714 	volatile int argpos;
    715 	int dirhasglob, filehasglob;
    716 	char rempath[MAXPATHLEN];
    717 
    718 #ifdef __GNUC__			/* to shut up gcc warnings */
    719 	(void)&outfile;
    720 #endif
    721 
    722 	argpos = 0;
    723 
    724 	if (setjmp(toplevel)) {
    725 		if (connected)
    726 			disconnect(0, NULL);
    727 		return (argpos + 1);
    728 	}
    729 	(void)signal(SIGINT, (sig_t)intr);
    730 	(void)signal(SIGPIPE, (sig_t)lostpeer);
    731 
    732 	ftpproxy = getenv(FTP_PROXY);
    733 	httpproxy = getenv(HTTP_PROXY);
    734 	host = path = dir = file = user = pass = NULL;
    735 
    736 	/*
    737 	 * Loop through as long as there's files to fetch.
    738 	 */
    739 	for (rval = 0; (rval == 0) && (argpos < argc); argpos++) {
    740 		if (strchr(argv[argpos], ':') == NULL)
    741 			break;
    742 		host = path = dir = file = user = pass = NULL;
    743 		line = argv[argpos];
    744 
    745 #ifndef SMALL
    746 		/*
    747 		 * Check for about:*
    748 		 */
    749 		if (strncasecmp(line, ABOUT_URL, sizeof(ABOUT_URL) - 1) == 0) {
    750 			line += sizeof(ABOUT_URL) -1;
    751 			if (strcasecmp(line, "ftp") == 0) {
    752 				fprintf(ttyout, "%s\n%s\n",
    753 "This version of ftp has been enhanced by Luke Mewburn <lukem (at) netbsd.org>.",
    754 "Execute 'man ftp' for more details");
    755 			} else if (strcasecmp(line, "netbsd") == 0) {
    756 				fprintf(ttyout, "%s\n%s\n",
    757 "NetBSD is a freely available and redistributable UNIX-like operating system.",
    758 "For more information, see http://www.netbsd.org/index.html");
    759 			} else {
    760 				fprintf(ttyout,
    761 				    "`%s' is an interesting topic.\n", line);
    762 			}
    763 			continue;
    764 		}
    765 #endif /* SMALL */
    766 
    767 		/*
    768 		 * Check for file:// and http:// URLs.
    769 		 */
    770 		if (strncasecmp(line, HTTP_URL, sizeof(HTTP_URL) - 1) == 0 ||
    771 		    strncasecmp(line, FILE_URL, sizeof(FILE_URL) - 1) == 0) {
    772 			if (url_get(line, httpproxy, outfile) == -1)
    773 				rval = argpos + 1;
    774 			continue;
    775 		}
    776 
    777 		/*
    778 		 * Try FTP URL-style arguments next. If ftpproxy is
    779 		 * set, use url_get() instead of standard ftp.
    780 		 * Finally, try host:file.
    781 		 */
    782 		if (strncasecmp(line, FTP_URL, sizeof(FTP_URL) - 1) == 0) {
    783 			int urltype;
    784 
    785 			if (ftpproxy) {
    786 				if (url_get(line, ftpproxy, outfile) == -1)
    787 					rval = argpos + 1;
    788 				continue;
    789 			}
    790 			if ((parse_url(line, "URL", &urltype, &user, &pass,
    791 			    &host, &port, &path) == -1) ||
    792 			    (user != NULL && *user == '\0') ||
    793 			    (pass != NULL && *pass == '\0') ||
    794 			    EMPTYSTRING(host)) {
    795 				warnx("Invalid URL `%s'", argv[argpos]);
    796 				rval = argpos + 1;
    797 				break;
    798 			}
    799 		} else {			/* classic style `host:file' */
    800 			host = xstrdup(line);
    801 			cp = strchr(host, ':');
    802 			if (cp != NULL) {
    803 				*cp = '\0';
    804 				path = xstrdup(cp + 1);
    805 			}
    806 		}
    807 		if (EMPTYSTRING(host)) {
    808 			rval = argpos + 1;
    809 			break;
    810 		}
    811 
    812 		/*
    813 		 * If dir is NULL, the file wasn't specified
    814 		 * (URL looked something like ftp://host)
    815 		 */
    816 		dir = path;
    817 		if (dir != NULL)
    818 			*dir++ = '\0';
    819 
    820 		/*
    821 		 * Extract the file and (if present) directory name.
    822 		 */
    823 		if (! EMPTYSTRING(dir)) {
    824 			cp = strrchr(dir, '/');
    825 			if (cp != NULL) {
    826 				*cp++ = '\0';
    827 				file = cp;
    828 			} else {
    829 				file = dir;
    830 				dir = NULL;
    831 			}
    832 		}
    833 		if (debug)
    834 			fprintf(ttyout,
    835 "auto_fetch: user `%s', pass `%s', host %s:%d, path, `%s', dir `%s', file `%s'\n",
    836 			    user ? user : "", pass ? pass : "",
    837 			    host ? host : "", ntohs(port), path ? path : "",
    838 			    dir ? dir : "", file ? file : "");
    839 
    840 		/*
    841 		 * Set up the connection if we don't have one.
    842 		 */
    843 		if (strcasecmp(host, lasthost) != 0) {
    844 			int oautologin;
    845 
    846 			(void)strcpy(lasthost, host);
    847 			if (connected)
    848 				disconnect(0, NULL);
    849 			xargv[0] = __progname;
    850 			xargv[1] = host;
    851 			xargv[2] = NULL;
    852 			xargc = 2;
    853 			if (port) {
    854 				snprintf(portnum, sizeof(portnum),
    855 				    "%d", (int)port);
    856 				xargv[2] = portnum;
    857 				xargv[3] = NULL;
    858 				xargc = 3;
    859 			}
    860 			oautologin = autologin;
    861 			if (user != NULL)
    862 				autologin = 0;
    863 			setpeer(xargc, xargv);
    864 			autologin = oautologin;
    865 			if ((connected == 0)
    866 			 || ((connected == 1) &&
    867 			     !ftp_login(host, user, pass)) ) {
    868 				warnx("Can't connect or login to host `%s'",
    869 				    host);
    870 				rval = argpos + 1;
    871 				break;
    872 			}
    873 
    874 			/* Always use binary transfers. */
    875 			setbinary(0, NULL);
    876 		}
    877 			/* cd back to `/' */
    878 		xargv[0] = "cd";
    879 		xargv[1] = "/";
    880 		xargv[2] = NULL;
    881 		cd(2, xargv);
    882 		if (! dirchange) {
    883 			rval = argpos + 1;
    884 			break;
    885 		}
    886 
    887 		dirhasglob = filehasglob = 0;
    888 		if (doglob) {
    889 			if (! EMPTYSTRING(dir) &&
    890 			    strpbrk(dir, "*?[]{}") != NULL)
    891 				dirhasglob = 1;
    892 			if (! EMPTYSTRING(file) &&
    893 			    strpbrk(file, "*?[]{}") != NULL)
    894 				filehasglob = 1;
    895 		}
    896 
    897 				/* Change directories, if necessary. */
    898 		if (! EMPTYSTRING(dir) && !dirhasglob) {
    899 			xargv[0] = "cd";
    900 			xargv[1] = dir;
    901 			xargv[2] = NULL;
    902 			cd(2, xargv);
    903 			if (! dirchange) {
    904 				rval = argpos + 1;
    905 				break;
    906 			}
    907 		}
    908 
    909 		if (EMPTYSTRING(file)) {
    910 			rval = -1;
    911 			break;
    912 		}
    913 
    914 		if (!verbose)
    915 			fprintf(ttyout, "Retrieving %s/%s\n", dir ? dir : "",
    916 			    file);
    917 
    918 		if (dirhasglob) {
    919 			snprintf(rempath, sizeof(rempath), "%s/%s", dir, file);
    920 			file = rempath;
    921 		}
    922 
    923 				/* Fetch the file(s). */
    924 		xargc = 2;
    925 		xargv[0] = "get";
    926 		xargv[1] = file;
    927 		xargv[2] = NULL;
    928 		if (dirhasglob || filehasglob) {
    929 			int ointeractive;
    930 
    931 			ointeractive = interactive;
    932 			interactive = 0;
    933 			xargv[0] = "mget";
    934 			mget(xargc, xargv);
    935 			interactive = ointeractive;
    936 		} else {
    937 			if (outfile != NULL) {
    938 				xargv[2] = outfile;
    939 				xargv[3] = NULL;
    940 				xargc++;
    941 			}
    942 			get(xargc, xargv);
    943 			if (outfile != NULL && strcmp(outfile, "-") != 0
    944 			    && outfile[0] != '|')
    945 				outfile = NULL;
    946 		}
    947 
    948 		if ((code / 100) != COMPLETE)
    949 			rval = argpos + 1;
    950 	}
    951 	if (connected && rval != -1)
    952 		disconnect(0, NULL);
    953 	FREEPTR(host);
    954 	FREEPTR(path);
    955 	FREEPTR(user);
    956 	FREEPTR(pass);
    957 	return (rval);
    958 }
    959