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