Home | History | Annotate | Line # | Download | only in ftp
fetch.c revision 1.24
      1  1.24     lukem /*	$NetBSD: fetch.c,v 1.24 1998/07/22 16:06:28 lukem Exp $	*/
      2   1.1     lukem 
      3   1.1     lukem /*-
      4   1.1     lukem  * Copyright (c) 1997 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.24     lukem __RCSID("$NetBSD: fetch.c,v 1.24 1998/07/22 16:06:28 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.1     lukem 
     52   1.1     lukem #include <netinet/in.h>
     53   1.1     lukem 
     54   1.2     lukem #include <arpa/ftp.h>
     55   1.1     lukem #include <arpa/inet.h>
     56   1.1     lukem 
     57   1.1     lukem #include <ctype.h>
     58   1.1     lukem #include <err.h>
     59  1.22     lukem #include <errno.h>
     60   1.1     lukem #include <netdb.h>
     61   1.1     lukem #include <fcntl.h>
     62   1.3     lukem #include <signal.h>
     63   1.1     lukem #include <stdio.h>
     64   1.1     lukem #include <stdlib.h>
     65   1.1     lukem #include <string.h>
     66   1.1     lukem #include <unistd.h>
     67  1.24     lukem #include <util.h>
     68   1.1     lukem 
     69   1.1     lukem #include "ftp_var.h"
     70   1.1     lukem 
     71  1.22     lukem static int	url_get __P((const char *, const char *, const char *));
     72  1.12     lukem void    	aborthttp __P((int));
     73  1.12     lukem 
     74  1.12     lukem 
     75   1.1     lukem #define	FTP_URL		"ftp://"	/* ftp URL prefix */
     76   1.1     lukem #define	HTTP_URL	"http://"	/* http URL prefix */
     77   1.5     lukem #define FTP_PROXY	"ftp_proxy"	/* env var with ftp proxy location */
     78   1.1     lukem #define HTTP_PROXY	"http_proxy"	/* env var with http proxy location */
     79   1.1     lukem 
     80   1.1     lukem 
     81   1.1     lukem #define EMPTYSTRING(x)	((x) == NULL || (*(x) == '\0'))
     82   1.1     lukem 
     83   1.2     lukem jmp_buf	httpabort;
     84   1.2     lukem 
     85   1.1     lukem /*
     86   1.5     lukem  * Retrieve URL, via the proxy in $proxyvar if necessary.
     87   1.1     lukem  * Modifies the string argument given.
     88   1.1     lukem  * Returns -1 on failure, 0 on success
     89   1.1     lukem  */
     90  1.12     lukem static int
     91  1.22     lukem url_get(origline, proxyenv, outfile)
     92   1.9     lukem 	const char *origline;
     93   1.9     lukem 	const char *proxyenv;
     94  1.22     lukem 	const char *outfile;
     95   1.1     lukem {
     96   1.1     lukem 	struct sockaddr_in sin;
     97  1.24     lukem 	int i, out, isftpurl, isredirected;
     98  1.18     lukem 	in_port_t port;
     99  1.12     lukem 	volatile int s;
    100  1.13     lukem 	size_t len;
    101  1.24     lukem 	char *cp, *ep, *portnum, *path;
    102  1.12     lukem 	const char *savefile;
    103  1.24     lukem 	char *line, *proxy, *host, *buf;
    104  1.22     lukem 	volatile sig_t oldintr, oldintp;
    105   1.1     lukem 	off_t hashbytes;
    106  1.22     lukem 	struct hostent *hp = NULL;
    107  1.22     lukem 	int (*closefunc) __P((FILE *));
    108  1.24     lukem 	FILE *fin, *fout;
    109  1.24     lukem 	int retval;
    110   1.1     lukem 
    111  1.22     lukem 	closefunc = NULL;
    112  1.24     lukem 	fin = fout = NULL;
    113   1.1     lukem 	s = -1;
    114  1.24     lukem 	proxy = buf = NULL;
    115  1.24     lukem 	isftpurl = isredirected = 0;
    116  1.24     lukem 	retval = -1;
    117   1.1     lukem 
    118  1.22     lukem #ifdef __GNUC__			/* to shut up gcc warnings */
    119  1.22     lukem 	(void)&closefunc;
    120  1.24     lukem 	(void)&fin;
    121  1.22     lukem 	(void)&fout;
    122  1.24     lukem 	(void)&buf;
    123  1.22     lukem 	(void)&proxy;
    124  1.12     lukem 	(void)&savefile;
    125  1.24     lukem 	(void)&retval;
    126  1.12     lukem #endif
    127  1.12     lukem 
    128   1.9     lukem 	line = strdup(origline);
    129   1.9     lukem 	if (line == NULL)
    130   1.9     lukem 		errx(1, "Can't allocate memory to parse URL");
    131   1.6     lukem 	if (strncasecmp(line, HTTP_URL, sizeof(HTTP_URL) - 1) == 0)
    132   1.5     lukem 		host = line + sizeof(HTTP_URL) - 1;
    133  1.11     lukem 	else if (strncasecmp(line, FTP_URL, sizeof(FTP_URL) - 1) == 0) {
    134   1.5     lukem 		host = line + sizeof(FTP_URL) - 1;
    135  1.11     lukem 		isftpurl = 1;
    136  1.11     lukem 	} else
    137   1.9     lukem 		errx(1, "url_get: Invalid URL '%s'", line);
    138   1.5     lukem 
    139   1.1     lukem 	path = strchr(host, '/');		/* find path */
    140   1.9     lukem 	if (EMPTYSTRING(path)) {
    141  1.11     lukem 		if (isftpurl)
    142  1.11     lukem 			goto noftpautologin;
    143  1.11     lukem 		warnx("Invalid URL (no `/' after host): %s", origline);
    144   1.5     lukem 		goto cleanup_url_get;
    145   1.9     lukem 	}
    146   1.1     lukem 	*path++ = '\0';
    147   1.9     lukem 	if (EMPTYSTRING(path)) {
    148  1.11     lukem 		if (isftpurl)
    149  1.11     lukem 			goto noftpautologin;
    150  1.11     lukem 		warnx("Invalid URL (no file after host): %s", origline);
    151   1.5     lukem 		goto cleanup_url_get;
    152   1.9     lukem 	}
    153   1.1     lukem 
    154  1.22     lukem 	if (outfile)
    155  1.22     lukem 		savefile = outfile;
    156  1.22     lukem 	else {
    157  1.22     lukem 		savefile = strrchr(path, '/');		/* find savefile */
    158  1.22     lukem 		if (savefile != NULL)
    159  1.22     lukem 			savefile++;
    160  1.22     lukem 		else
    161  1.22     lukem 			savefile = path;
    162  1.22     lukem 	}
    163   1.9     lukem 	if (EMPTYSTRING(savefile)) {
    164  1.11     lukem 		if (isftpurl)
    165  1.11     lukem 			goto noftpautologin;
    166  1.11     lukem 		warnx("Invalid URL (no file after directory): %s", origline);
    167   1.5     lukem 		goto cleanup_url_get;
    168   1.9     lukem 	}
    169   1.1     lukem 
    170   1.2     lukem 	if (proxyenv != NULL) {				/* use proxy */
    171   1.2     lukem 		proxy = strdup(proxyenv);
    172   1.1     lukem 		if (proxy == NULL)
    173   1.9     lukem 			errx(1, "Can't allocate memory for proxy URL.");
    174   1.6     lukem 		if (strncasecmp(proxy, HTTP_URL, sizeof(HTTP_URL) - 1) == 0)
    175   1.5     lukem 			host = proxy + sizeof(HTTP_URL) - 1;
    176   1.6     lukem 		else if (strncasecmp(proxy, FTP_URL, sizeof(FTP_URL) - 1) == 0)
    177   1.5     lukem 			host = proxy + sizeof(FTP_URL) - 1;
    178   1.5     lukem 		else {
    179   1.9     lukem 			warnx("Malformed proxy URL: %s", proxyenv);
    180   1.5     lukem 			goto cleanup_url_get;
    181   1.5     lukem 		}
    182   1.9     lukem 		if (EMPTYSTRING(host)) {
    183   1.9     lukem 			warnx("Malformed proxy URL: %s", proxyenv);
    184   1.5     lukem 			goto cleanup_url_get;
    185   1.9     lukem 		}
    186   1.1     lukem 		*--path = '/';			/* add / back to real path */
    187   1.1     lukem 		path = strchr(host, '/');	/* remove trailing / on host */
    188   1.1     lukem 		if (! EMPTYSTRING(path))
    189   1.1     lukem 			*path++ = '\0';
    190   1.1     lukem 		path = line;
    191   1.1     lukem 	}
    192   1.1     lukem 
    193   1.1     lukem 	portnum = strchr(host, ':');			/* find portnum */
    194   1.1     lukem 	if (portnum != NULL)
    195   1.1     lukem 		*portnum++ = '\0';
    196   1.1     lukem 
    197   1.1     lukem 	if (debug)
    198  1.22     lukem 		fprintf(ttyout, "host %s, port %s, path %s, save as %s.\n",
    199   1.1     lukem 		    host, portnum, path, savefile);
    200   1.1     lukem 
    201   1.1     lukem 	memset(&sin, 0, sizeof(sin));
    202   1.1     lukem 	sin.sin_family = AF_INET;
    203   1.1     lukem 
    204  1.20  christos 	if (isdigit((unsigned char)host[0])) {
    205   1.1     lukem 		if (inet_aton(host, &sin.sin_addr) == 0) {
    206   1.6     lukem 			warnx("Invalid IP address: %s", host);
    207   1.5     lukem 			goto cleanup_url_get;
    208   1.1     lukem 		}
    209   1.1     lukem 	} else {
    210   1.1     lukem 		hp = gethostbyname(host);
    211   1.1     lukem 		if (hp == NULL) {
    212   1.2     lukem 			warnx("%s: %s", host, hstrerror(h_errno));
    213   1.5     lukem 			goto cleanup_url_get;
    214   1.1     lukem 		}
    215   1.1     lukem 		if (hp->h_addrtype != AF_INET) {
    216   1.1     lukem 			warnx("%s: not an Internet address?", host);
    217   1.5     lukem 			goto cleanup_url_get;
    218   1.1     lukem 		}
    219   1.1     lukem 		memcpy(&sin.sin_addr, hp->h_addr, hp->h_length);
    220   1.1     lukem 	}
    221   1.1     lukem 
    222   1.1     lukem 	if (! EMPTYSTRING(portnum)) {
    223  1.12     lukem 		char *ep;
    224  1.12     lukem 		long nport;
    225  1.12     lukem 
    226  1.12     lukem 		nport = strtol(portnum, &ep, 10);
    227  1.19     lukem 		if (nport < 1 || nport > MAX_IN_PORT_T || *ep != '\0') {
    228   1.6     lukem 			warnx("Invalid port: %s", portnum);
    229   1.5     lukem 			goto cleanup_url_get;
    230   1.1     lukem 		}
    231  1.18     lukem 		port = htons((in_port_t)nport);
    232   1.1     lukem 	} else
    233   1.1     lukem 		port = httpport;
    234   1.1     lukem 	sin.sin_port = port;
    235   1.1     lukem 
    236   1.1     lukem 	s = socket(AF_INET, SOCK_STREAM, 0);
    237   1.1     lukem 	if (s == -1) {
    238   1.9     lukem 		warn("Can't create socket");
    239   1.5     lukem 		goto cleanup_url_get;
    240   1.1     lukem 	}
    241   1.1     lukem 
    242  1.23   thorpej 	while (xconnect(s, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
    243  1.22     lukem 		if (errno == EINTR)
    244  1.22     lukem 			continue;
    245  1.22     lukem 		if (hp && hp->h_addr_list[1]) {
    246  1.22     lukem 			int oerrno = errno;
    247  1.22     lukem 			char *ia;
    248  1.22     lukem 
    249  1.22     lukem 			ia = inet_ntoa(sin.sin_addr);
    250  1.22     lukem 			errno = oerrno;
    251  1.22     lukem 			warn("connect to address %s", ia);
    252  1.22     lukem 			hp->h_addr_list++;
    253  1.22     lukem 			memcpy(&sin.sin_addr, hp->h_addr_list[0],
    254  1.22     lukem 			    (size_t)hp->h_length);
    255  1.22     lukem 			fprintf(ttyout, "Trying %s...\n",
    256  1.22     lukem 			    inet_ntoa(sin.sin_addr));
    257  1.22     lukem 			(void)close(s);
    258  1.22     lukem 			s = socket(AF_INET, SOCK_STREAM, 0);
    259  1.22     lukem 			if (s < 0) {
    260  1.24     lukem 				warn("Can't create socket");
    261  1.22     lukem 				goto cleanup_url_get;
    262  1.22     lukem 			}
    263  1.22     lukem 			continue;
    264  1.22     lukem 		}
    265   1.1     lukem 		warn("Can't connect to %s", host);
    266   1.5     lukem 		goto cleanup_url_get;
    267   1.1     lukem 	}
    268   1.1     lukem 
    269  1.24     lukem 	fin = fdopen(s, "r+");
    270   1.1     lukem 	/*
    271  1.24     lukem 	 * Construct and send the request. Proxy requests don't want leading /.
    272   1.1     lukem 	 */
    273  1.21        tv 	if (!proxy) {
    274  1.22     lukem 		fprintf(ttyout, "Requesting %s\n", origline);
    275  1.24     lukem 		fprintf(fin,
    276  1.24     lukem 		    "GET /%s HTTP/1.1\r\nHost: %s\r\nConnection: close\r\n\r\n",
    277  1.24     lukem 		    path, host);
    278  1.21        tv 	} else {
    279  1.24     lukem 		fprintf(ttyout, "Requesting %s\n  (via %s)\n",
    280  1.24     lukem 		    origline, proxyenv);
    281  1.24     lukem 		fprintf(fin, "GET %s HTTP/1.0\r\n\r\n", path);
    282  1.21        tv 	}
    283  1.24     lukem 	if (fflush(fin) == EOF) {
    284   1.9     lukem 		warn("Writing HTTP request");
    285   1.5     lukem 		goto cleanup_url_get;
    286   1.1     lukem 	}
    287  1.24     lukem 
    288  1.24     lukem 			/* Read the response */
    289  1.24     lukem 	if ((buf = fparseln(fin, &len, NULL, "\0\0\0", 0)) == NULL) {
    290  1.24     lukem 		warn("Receiving HTTP reply");
    291  1.24     lukem 		goto cleanup_url_get;
    292   1.1     lukem 	}
    293  1.24     lukem 	while (len > 0 && (buf[len-1] == '\r' || buf[len-1] == '\n'))
    294  1.24     lukem 		buf[--len] = '\0';
    295  1.24     lukem 	if (debug)
    296  1.24     lukem 		fprintf(ttyout, "received '%s'\n", buf);
    297  1.24     lukem 
    298   1.1     lukem 	cp = strchr(buf, ' ');
    299   1.1     lukem 	if (cp == NULL)
    300   1.1     lukem 		goto improper;
    301   1.1     lukem 	else
    302   1.1     lukem 		cp++;
    303  1.24     lukem 	if (strncmp(cp, "301", 3) == 0 || strncmp(cp, "302", 3) == 0) {
    304  1.24     lukem 		isredirected++;
    305  1.24     lukem 	} else if (strncmp(cp, "200", 3)) {
    306   1.1     lukem 		warnx("Error retrieving file: %s", cp);
    307   1.5     lukem 		goto cleanup_url_get;
    308   1.1     lukem 	}
    309   1.1     lukem 
    310  1.22     lukem 			/* Read the rest of the header. */
    311  1.24     lukem 	free(buf);
    312  1.24     lukem 	filesize = -1;
    313  1.24     lukem 	while (1) {
    314  1.24     lukem 		if ((buf = fparseln(fin, &len, NULL, "\0\0\0", 0)) == NULL) {
    315  1.24     lukem 			warn("Receiving HTTP reply");
    316  1.24     lukem 			goto cleanup_url_get;
    317  1.24     lukem 		}
    318  1.24     lukem 		while (len > 0 && (buf[len-1] == '\r' || buf[len-1] == '\n'))
    319  1.24     lukem 			buf[--len] = '\0';
    320  1.24     lukem 		if (len == 0)
    321   1.1     lukem 			break;
    322  1.24     lukem 		if (debug)
    323  1.24     lukem 			fprintf(ttyout, "received '%s'\n", buf);
    324   1.1     lukem 
    325  1.24     lukem 			/* Look for some headers */
    326  1.24     lukem 		cp = buf;
    327   1.1     lukem #define CONTENTLEN "Content-Length: "
    328  1.24     lukem 		if (strncasecmp(cp, CONTENTLEN, sizeof(CONTENTLEN) - 1) == 0) {
    329  1.24     lukem 			cp += sizeof(CONTENTLEN) - 1;
    330  1.24     lukem 			filesize = strtol(cp, &ep, 10);
    331  1.24     lukem 			if (filesize < 1 || *ep != '\0')
    332  1.24     lukem 				goto improper;
    333  1.24     lukem #define LOCATION "Location: "
    334  1.24     lukem 		} else if (isredirected &&
    335  1.24     lukem 		    strncasecmp(cp, LOCATION, sizeof(LOCATION) - 1) == 0) {
    336  1.24     lukem 			cp += sizeof(LOCATION) - 1;
    337  1.24     lukem 			if (verbose)
    338  1.24     lukem 				fprintf(ttyout, "Redirected to %s\n", cp);
    339  1.24     lukem 			resetsockbufsize();
    340  1.24     lukem 			if (fin != NULL)
    341  1.24     lukem 				fclose(fin);
    342  1.24     lukem 			else if (s != -1)
    343  1.24     lukem 				close(s);
    344  1.24     lukem 			if (closefunc != NULL && fout != NULL)
    345  1.24     lukem 				(*closefunc)(fout);
    346  1.24     lukem 			if (proxy)
    347  1.24     lukem 				free(proxy);
    348  1.24     lukem 			free(line);
    349  1.24     lukem 			retval = url_get(cp, proxyenv, outfile);
    350  1.24     lukem 			if (buf)
    351  1.24     lukem 				free(buf);
    352  1.24     lukem 			return retval;
    353  1.24     lukem 		}
    354   1.1     lukem 	}
    355  1.24     lukem 	free(buf);
    356   1.1     lukem 
    357  1.22     lukem 	oldintr = oldintp = NULL;
    358  1.22     lukem 
    359  1.22     lukem 			/* Open the output file. */
    360  1.22     lukem 	if (strcmp(savefile, "-") == 0) {
    361  1.22     lukem 		fout = stdout;
    362  1.22     lukem 	} else if (*savefile == '|') {
    363  1.22     lukem 		oldintp = signal(SIGPIPE, SIG_IGN);
    364  1.22     lukem 		fout = popen(savefile + 1, "w");
    365  1.22     lukem 		if (fout == NULL) {
    366  1.22     lukem 			warn("Can't run %s", savefile + 1);
    367  1.22     lukem 			goto cleanup_url_get;
    368  1.22     lukem 		}
    369  1.22     lukem 		closefunc = pclose;
    370  1.22     lukem 	} else {
    371  1.22     lukem 		fout = fopen(savefile, "w");
    372  1.22     lukem 		if (fout == NULL) {
    373  1.22     lukem 			warn("Can't open %s", savefile);
    374  1.22     lukem 			goto cleanup_url_get;
    375  1.22     lukem 		}
    376  1.22     lukem 		closefunc = fclose;
    377   1.1     lukem 	}
    378   1.1     lukem 
    379  1.22     lukem 			/* Trap signals */
    380   1.2     lukem 	if (setjmp(httpabort)) {
    381   1.2     lukem 		if (oldintr)
    382   1.3     lukem 			(void)signal(SIGINT, oldintr);
    383  1.22     lukem 		if (oldintp)
    384  1.22     lukem 			(void)signal(SIGPIPE, oldintp);
    385   1.5     lukem 		goto cleanup_url_get;
    386   1.2     lukem 	}
    387   1.2     lukem 	oldintr = signal(SIGINT, aborthttp);
    388   1.2     lukem 
    389   1.1     lukem 	bytes = 0;
    390   1.1     lukem 	hashbytes = mark;
    391   1.1     lukem 	progressmeter(-1);
    392   1.2     lukem 
    393  1.24     lukem 			/* Finally, suck down the file. */
    394  1.24     lukem 	if ((buf = malloc(BUFSIZ)) == NULL)
    395  1.24     lukem 		errx(1, "Can't allocate memory for transfer buffer\n");
    396   1.1     lukem 	i = 0;
    397  1.22     lukem 	out = fileno(fout);
    398  1.24     lukem 	while ((len = fread(buf, sizeof(char), BUFSIZ, fin)) > 0) {
    399   1.1     lukem 		bytes += len;
    400   1.1     lukem 		for (cp = buf; len > 0; len -= i, cp += i) {
    401   1.1     lukem 			if ((i = write(out, cp, len)) == -1) {
    402   1.1     lukem 				warn("Writing %s", savefile);
    403   1.5     lukem 				goto cleanup_url_get;
    404   1.1     lukem 			}
    405   1.1     lukem 			else if (i == 0)
    406   1.1     lukem 				break;
    407   1.1     lukem 		}
    408   1.1     lukem 		if (hash && !progress) {
    409   1.1     lukem 			while (bytes >= hashbytes) {
    410  1.22     lukem 				(void)putc('#', ttyout);
    411   1.1     lukem 				hashbytes += mark;
    412   1.1     lukem 			}
    413  1.22     lukem 			(void)fflush(ttyout);
    414   1.1     lukem 		}
    415   1.1     lukem 	}
    416   1.1     lukem 	if (hash && !progress && bytes > 0) {
    417   1.1     lukem 		if (bytes < mark)
    418  1.22     lukem 			(void)putc('#', ttyout);
    419  1.22     lukem 		(void)putc('\n', ttyout);
    420  1.22     lukem 		(void)fflush(ttyout);
    421   1.1     lukem 	}
    422   1.1     lukem 	if (len != 0) {
    423   1.1     lukem 		warn("Reading from socket");
    424   1.5     lukem 		goto cleanup_url_get;
    425   1.1     lukem 	}
    426   1.2     lukem 	progressmeter(1);
    427   1.2     lukem 	if (verbose)
    428  1.22     lukem 		fputs("Successfully retrieved file.\n", ttyout);
    429   1.3     lukem 	(void)signal(SIGINT, oldintr);
    430  1.22     lukem 	if (oldintp)
    431  1.22     lukem 		(void)signal(SIGPIPE, oldintp);
    432   1.1     lukem 
    433  1.24     lukem 	retval = 0;
    434  1.24     lukem 	goto cleanup_url_get;
    435   1.1     lukem 
    436  1.11     lukem noftpautologin:
    437  1.11     lukem 	warnx(
    438  1.11     lukem 	    "Auto-login using ftp URLs isn't supported when using $ftp_proxy");
    439  1.11     lukem 	goto cleanup_url_get;
    440  1.11     lukem 
    441   1.1     lukem improper:
    442   1.6     lukem 	warnx("Improper response from %s", host);
    443  1.11     lukem 
    444   1.5     lukem cleanup_url_get:
    445  1.23   thorpej 	resetsockbufsize();
    446  1.24     lukem 	if (fin != NULL)
    447  1.24     lukem 		fclose(fin);
    448  1.24     lukem 	else if (s != -1)
    449   1.1     lukem 		close(s);
    450  1.22     lukem 	if (closefunc != NULL && fout != NULL)
    451  1.22     lukem 		(*closefunc)(fout);
    452  1.24     lukem 	if (buf)
    453  1.24     lukem 		free(buf);
    454   1.1     lukem 	if (proxy)
    455   1.1     lukem 		free(proxy);
    456  1.10     lukem 	free(line);
    457  1.24     lukem 	return (retval);
    458   1.1     lukem }
    459   1.1     lukem 
    460   1.1     lukem /*
    461   1.2     lukem  * Abort a http retrieval
    462   1.2     lukem  */
    463   1.2     lukem void
    464   1.3     lukem aborthttp(notused)
    465   1.3     lukem 	int notused;
    466   1.2     lukem {
    467   1.2     lukem 
    468   1.2     lukem 	alarmtimer(0);
    469  1.24     lukem 	fputs("\nHTTP fetch aborted.\n", ttyout);
    470  1.22     lukem 	(void)fflush(ttyout);
    471   1.2     lukem 	longjmp(httpabort, 1);
    472   1.2     lukem }
    473   1.2     lukem 
    474   1.2     lukem /*
    475   1.1     lukem  * Retrieve multiple files from the command line, transferring
    476   1.1     lukem  * files of the form "host:path", "ftp://host/path" using the
    477   1.1     lukem  * ftp protocol, and files of the form "http://host/path" using
    478   1.1     lukem  * the http protocol.
    479   1.3     lukem  * If path has a trailing "/", then return (-1);
    480   1.1     lukem  * the path will be cd-ed into and the connection remains open,
    481   1.1     lukem  * and the function will return -1 (to indicate the connection
    482   1.1     lukem  * is alive).
    483   1.1     lukem  * If an error occurs the return value will be the offset+1 in
    484   1.1     lukem  * argv[] of the file that caused a problem (i.e, argv[x]
    485   1.1     lukem  * returns x+1)
    486   1.1     lukem  * Otherwise, 0 is returned if all files retrieved successfully.
    487   1.1     lukem  */
    488   1.1     lukem int
    489  1.22     lukem auto_fetch(argc, argv, outfile)
    490   1.1     lukem 	int argc;
    491   1.1     lukem 	char *argv[];
    492  1.22     lukem 	char *outfile;
    493   1.1     lukem {
    494   1.1     lukem 	static char lasthost[MAXHOSTNAMELEN];
    495   1.1     lukem 	char *xargv[5];
    496   1.1     lukem 	char *cp, *line, *host, *dir, *file, *portnum;
    497   1.6     lukem 	char *user, *pass;
    498   1.5     lukem 	char *ftpproxy, *httpproxy;
    499  1.12     lukem 	int rval, xargc;
    500  1.12     lukem 	volatile int argpos;
    501   1.3     lukem 	int dirhasglob, filehasglob;
    502   1.3     lukem 	char rempath[MAXPATHLEN];
    503   1.1     lukem 
    504  1.22     lukem #ifdef __GNUC__			/* to shut up gcc warnings */
    505  1.22     lukem 	(void)&outfile;
    506  1.22     lukem #endif
    507  1.22     lukem 
    508   1.2     lukem 	argpos = 0;
    509   1.1     lukem 
    510   1.2     lukem 	if (setjmp(toplevel)) {
    511   1.2     lukem 		if (connected)
    512   1.2     lukem 			disconnect(0, NULL);
    513   1.3     lukem 		return (argpos + 1);
    514   1.2     lukem 	}
    515   1.3     lukem 	(void)signal(SIGINT, (sig_t)intr);
    516   1.3     lukem 	(void)signal(SIGPIPE, (sig_t)lostpeer);
    517   1.1     lukem 
    518   1.5     lukem 	ftpproxy = getenv(FTP_PROXY);
    519   1.5     lukem 	httpproxy = getenv(HTTP_PROXY);
    520   1.5     lukem 
    521   1.1     lukem 	/*
    522   1.1     lukem 	 * Loop through as long as there's files to fetch.
    523   1.1     lukem 	 */
    524   1.2     lukem 	for (rval = 0; (rval == 0) && (argpos < argc); free(line), argpos++) {
    525   1.1     lukem 		if (strchr(argv[argpos], ':') == NULL)
    526   1.1     lukem 			break;
    527   1.6     lukem 		host = dir = file = portnum = user = pass = NULL;
    528   1.1     lukem 
    529   1.1     lukem 		/*
    530   1.1     lukem 		 * We muck with the string, so we make a copy.
    531   1.1     lukem 		 */
    532   1.1     lukem 		line = strdup(argv[argpos]);
    533   1.1     lukem 		if (line == NULL)
    534   1.1     lukem 			errx(1, "Can't allocate memory for auto-fetch.");
    535   1.1     lukem 
    536   1.1     lukem 		/*
    537   1.1     lukem 		 * Try HTTP URL-style arguments first.
    538   1.1     lukem 		 */
    539   1.6     lukem 		if (strncasecmp(line, HTTP_URL, sizeof(HTTP_URL) - 1) == 0) {
    540  1.22     lukem 			if (url_get(line, httpproxy, outfile) == -1)
    541   1.1     lukem 				rval = argpos + 1;
    542   1.1     lukem 			continue;
    543   1.1     lukem 		}
    544   1.1     lukem 
    545   1.1     lukem 		/*
    546   1.5     lukem 		 * Try FTP URL-style arguments next. If ftpproxy is
    547   1.5     lukem 		 * set, use url_get() instead of standard ftp.
    548   1.5     lukem 		 * Finally, try host:file.
    549   1.1     lukem 		 */
    550   1.1     lukem 		host = line;
    551   1.6     lukem 		if (strncasecmp(line, FTP_URL, sizeof(FTP_URL) - 1) == 0) {
    552   1.5     lukem 			if (ftpproxy) {
    553  1.22     lukem 				if (url_get(line, ftpproxy, outfile) == -1)
    554   1.5     lukem 					rval = argpos + 1;
    555   1.5     lukem 				continue;
    556   1.5     lukem 			}
    557   1.1     lukem 			host += sizeof(FTP_URL) - 1;
    558   1.6     lukem 			dir = strchr(host, '/');
    559   1.1     lukem 
    560   1.6     lukem 				/* look for [user:pass@]host[:port] */
    561   1.7     lukem 			pass = strpbrk(host, ":@/");
    562   1.7     lukem 			if (pass == NULL || *pass == '/') {
    563   1.7     lukem 				pass = NULL;
    564   1.6     lukem 				goto parsed_url;
    565   1.7     lukem 			}
    566   1.7     lukem 			if (pass == host || *pass == '@') {
    567   1.8     lukem bad_ftp_url:
    568   1.9     lukem 				warnx("Invalid URL: %s", argv[argpos]);
    569   1.6     lukem 				rval = argpos + 1;
    570   1.6     lukem 				continue;
    571   1.6     lukem 			}
    572   1.6     lukem 			*pass++ = '\0';
    573   1.6     lukem 			cp = strpbrk(pass, ":@/");
    574   1.6     lukem 			if (cp == NULL || *cp == '/') {
    575   1.6     lukem 				portnum = pass;
    576   1.7     lukem 				pass = NULL;
    577   1.6     lukem 				goto parsed_url;
    578   1.6     lukem 			}
    579   1.7     lukem 			if (EMPTYSTRING(cp) || *cp == ':')
    580   1.8     lukem 				goto bad_ftp_url;
    581   1.6     lukem 			*cp++ = '\0';
    582   1.7     lukem 			user = host;
    583   1.7     lukem 			if (EMPTYSTRING(user))
    584   1.8     lukem 				goto bad_ftp_url;
    585   1.6     lukem 			host = cp;
    586   1.1     lukem 			portnum = strchr(host, ':');
    587   1.1     lukem 			if (portnum != NULL)
    588   1.1     lukem 				*portnum++ = '\0';
    589   1.6     lukem 		} else {			/* classic style `host:file' */
    590   1.6     lukem 			dir = strchr(host, ':');
    591   1.6     lukem 		}
    592  1.16     lukem parsed_url:
    593   1.1     lukem 		if (EMPTYSTRING(host)) {
    594   1.1     lukem 			rval = argpos + 1;
    595   1.1     lukem 			continue;
    596   1.1     lukem 		}
    597   1.1     lukem 
    598   1.1     lukem 		/*
    599  1.15     lukem 		 * If dir is NULL, the file wasn't specified
    600   1.1     lukem 		 * (URL looked something like ftp://host)
    601   1.1     lukem 		 */
    602   1.6     lukem 		if (dir != NULL)
    603   1.6     lukem 			*dir++ = '\0';
    604   1.1     lukem 
    605   1.1     lukem 		/*
    606   1.1     lukem 		 * Extract the file and (if present) directory name.
    607   1.1     lukem 		 */
    608   1.1     lukem 		if (! EMPTYSTRING(dir)) {
    609   1.6     lukem 			cp = strrchr(dir, '/');
    610   1.1     lukem 			if (cp != NULL) {
    611   1.1     lukem 				*cp++ = '\0';
    612   1.1     lukem 				file = cp;
    613   1.1     lukem 			} else {
    614   1.1     lukem 				file = dir;
    615   1.1     lukem 				dir = NULL;
    616   1.1     lukem 			}
    617   1.1     lukem 		}
    618   1.1     lukem 		if (debug)
    619  1.22     lukem 			fprintf(ttyout,
    620  1.22     lukem 			    "user %s:%s host %s port %s dir %s file %s\n",
    621   1.6     lukem 			    user, pass, host, portnum, dir, file);
    622   1.1     lukem 
    623   1.1     lukem 		/*
    624   1.1     lukem 		 * Set up the connection if we don't have one.
    625   1.1     lukem 		 */
    626   1.1     lukem 		if (strcmp(host, lasthost) != 0) {
    627   1.6     lukem 			int oautologin;
    628   1.6     lukem 
    629   1.4     lukem 			(void)strcpy(lasthost, host);
    630   1.1     lukem 			if (connected)
    631   1.1     lukem 				disconnect(0, NULL);
    632   1.1     lukem 			xargv[0] = __progname;
    633   1.1     lukem 			xargv[1] = host;
    634   1.1     lukem 			xargv[2] = NULL;
    635   1.1     lukem 			xargc = 2;
    636   1.6     lukem 			if (! EMPTYSTRING(portnum)) {
    637   1.1     lukem 				xargv[2] = portnum;
    638   1.1     lukem 				xargv[3] = NULL;
    639   1.1     lukem 				xargc = 3;
    640   1.1     lukem 			}
    641   1.6     lukem 			oautologin = autologin;
    642   1.6     lukem 			if (user != NULL)
    643   1.6     lukem 				autologin = 0;
    644   1.1     lukem 			setpeer(xargc, xargv);
    645   1.6     lukem 			autologin = oautologin;
    646   1.6     lukem 			if ((connected == 0)
    647  1.24     lukem 			 || ((connected == 1) &&
    648  1.24     lukem 			     !ftp_login(host, user, pass)) ) {
    649   1.6     lukem 				warnx("Can't connect or login to host `%s'",
    650   1.6     lukem 				    host);
    651   1.1     lukem 				rval = argpos + 1;
    652   1.1     lukem 				continue;
    653   1.1     lukem 			}
    654   1.1     lukem 
    655   1.1     lukem 			/* Always use binary transfers. */
    656   1.1     lukem 			setbinary(0, NULL);
    657   1.1     lukem 		}
    658   1.6     lukem 			/* cd back to '/' */
    659   1.6     lukem 		xargv[0] = "cd";
    660   1.6     lukem 		xargv[1] = "/";
    661   1.6     lukem 		xargv[2] = NULL;
    662   1.6     lukem 		cd(2, xargv);
    663   1.6     lukem 		if (! dirchange) {
    664   1.6     lukem 			rval = argpos + 1;
    665   1.6     lukem 			continue;
    666   1.1     lukem 		}
    667   1.1     lukem 
    668   1.3     lukem 		dirhasglob = filehasglob = 0;
    669   1.3     lukem 		if (doglob) {
    670   1.3     lukem 			if (! EMPTYSTRING(dir) &&
    671   1.3     lukem 			    strpbrk(dir, "*?[]{}") != NULL)
    672   1.3     lukem 				dirhasglob = 1;
    673   1.3     lukem 			if (! EMPTYSTRING(file) &&
    674   1.3     lukem 			    strpbrk(file, "*?[]{}") != NULL)
    675   1.3     lukem 				filehasglob = 1;
    676   1.3     lukem 		}
    677   1.3     lukem 
    678   1.1     lukem 		/* Change directories, if necessary. */
    679   1.3     lukem 		if (! EMPTYSTRING(dir) && !dirhasglob) {
    680   1.1     lukem 			xargv[0] = "cd";
    681   1.1     lukem 			xargv[1] = dir;
    682   1.1     lukem 			xargv[2] = NULL;
    683   1.1     lukem 			cd(2, xargv);
    684   1.1     lukem 			if (! dirchange) {
    685   1.1     lukem 				rval = argpos + 1;
    686   1.1     lukem 				continue;
    687   1.1     lukem 			}
    688   1.1     lukem 		}
    689   1.1     lukem 
    690   1.1     lukem 		if (EMPTYSTRING(file)) {
    691   1.1     lukem 			rval = -1;
    692   1.1     lukem 			continue;
    693   1.1     lukem 		}
    694   1.1     lukem 
    695   1.2     lukem 		if (!verbose)
    696  1.22     lukem 			fprintf(ttyout, "Retrieving %s/%s\n", dir ? dir : "",
    697  1.22     lukem 			    file);
    698   1.2     lukem 
    699   1.3     lukem 		if (dirhasglob) {
    700   1.3     lukem 			snprintf(rempath, sizeof(rempath), "%s/%s", dir, file);
    701   1.3     lukem 			file = rempath;
    702   1.3     lukem 		}
    703   1.3     lukem 
    704   1.3     lukem 		/* Fetch the file(s). */
    705  1.22     lukem 		xargc = 2;
    706   1.1     lukem 		xargv[0] = "get";
    707   1.1     lukem 		xargv[1] = file;
    708   1.1     lukem 		xargv[2] = NULL;
    709   1.3     lukem 		if (dirhasglob || filehasglob) {
    710   1.3     lukem 			int ointeractive;
    711   1.3     lukem 
    712   1.3     lukem 			ointeractive = interactive;
    713   1.3     lukem 			interactive = 0;
    714   1.3     lukem 			xargv[0] = "mget";
    715  1.22     lukem 			mget(xargc, xargv);
    716   1.4     lukem 			interactive = ointeractive;
    717  1.22     lukem 		} else {
    718  1.22     lukem 			if (outfile != NULL) {
    719  1.22     lukem 				xargv[2] = outfile;
    720  1.22     lukem 				xargv[3] = NULL;
    721  1.22     lukem 				xargc++;
    722  1.22     lukem 			}
    723  1.22     lukem 			get(xargc, xargv);
    724  1.22     lukem 			if (outfile != NULL && strcmp(outfile, "-") != 0
    725  1.22     lukem 			    && outfile[0] != '|')
    726  1.22     lukem 				outfile = NULL;
    727  1.22     lukem 		}
    728   1.2     lukem 
    729   1.3     lukem 		if ((code / 100) != COMPLETE)
    730   1.2     lukem 			rval = argpos + 1;
    731   1.1     lukem 	}
    732   1.1     lukem 	if (connected && rval != -1)
    733   1.1     lukem 		disconnect(0, NULL);
    734   1.1     lukem 	return (rval);
    735   1.1     lukem }
    736