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