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