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