xref: /netbsd-src/usr.bin/ftp/fetch.c (revision d0fed6c87ddc40a8bffa6f99e7433ddfc864dd83)
1 /*	$NetBSD: fetch.c,v 1.8 1997/04/21 18:45:47 lukem Exp $	*/
2 
3 /*-
4  * Copyright (c) 1997 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason Thorpe and Luke Mewburn.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
30  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #ifndef lint
40 static char rcsid[] = "$NetBSD: fetch.c,v 1.8 1997/04/21 18:45:47 lukem Exp $";
41 #endif /* not lint */
42 
43 /*
44  * FTP User Program -- Command line file retrieval
45  */
46 
47 #include <sys/types.h>
48 #include <sys/param.h>
49 #include <sys/socket.h>
50 
51 #include <netinet/in.h>
52 
53 #include <arpa/ftp.h>
54 #include <arpa/inet.h>
55 
56 #include <ctype.h>
57 #include <err.h>
58 #include <netdb.h>
59 #include <fcntl.h>
60 #include <signal.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #include <unistd.h>
65 
66 #include "ftp_var.h"
67 
68 #define	FTP_URL		"ftp://"	/* ftp URL prefix */
69 #define	HTTP_URL	"http://"	/* http URL prefix */
70 #define FTP_PROXY	"ftp_proxy"	/* env var with ftp proxy location */
71 #define HTTP_PROXY	"http_proxy"	/* env var with http proxy location */
72 
73 
74 #define EMPTYSTRING(x)	((x) == NULL || (*(x) == '\0'))
75 
76 jmp_buf	httpabort;
77 
78 /*
79  * Retrieve URL, via the proxy in $proxyvar if necessary.
80  * Modifies the string argument given.
81  * Returns -1 on failure, 0 on success
82  */
83 int
84 url_get(line, proxyenv)
85 	char *line;
86 	char *proxyenv;
87 {
88 	struct sockaddr_in sin;
89 	int i, out, port, s;
90 	size_t buflen, len;
91 	char c, *cp, *cp2, *savefile, *portnum, *path, buf[4096];
92 	char *proxy, *host;
93 	sig_t oldintr;
94 	off_t hashbytes;
95 
96 	s = -1;
97 	proxy = NULL;
98 
99 	if (strncasecmp(line, HTTP_URL, sizeof(HTTP_URL) - 1) == 0)
100 		host = line + sizeof(HTTP_URL) - 1;
101 	else if (strncasecmp(line, FTP_URL, sizeof(FTP_URL) - 1) == 0)
102 		host = line + sizeof(FTP_URL) - 1;
103 	else
104 		errx(1, "url_get: invalid url '%s'", line);
105 
106 	path = strchr(host, '/');		/* find path */
107 	if (EMPTYSTRING(path))
108 		goto cleanup_url_get;
109 	*path++ = '\0';
110 	if (EMPTYSTRING(path))
111 		goto cleanup_url_get;
112 
113 	savefile = strrchr(path, '/');			/* find savefile */
114 	if (savefile != NULL)
115 		savefile++;
116 	else
117 		savefile = path;
118 	if (EMPTYSTRING(savefile))
119 		goto cleanup_url_get;
120 
121 	if (proxyenv != NULL) {				/* use proxy */
122 		proxy = strdup(proxyenv);
123 		if (proxy == NULL)
124 			errx(1, "Can't allocate memory for proxy url.");
125 		if (strncasecmp(proxy, HTTP_URL, sizeof(HTTP_URL) - 1) == 0)
126 			host = proxy + sizeof(HTTP_URL) - 1;
127 		else if (strncasecmp(proxy, FTP_URL, sizeof(FTP_URL) - 1) == 0)
128 			host = proxy + sizeof(FTP_URL) - 1;
129 		else {
130 			warnx("Malformed proxy URL: %s", proxy);
131 			goto cleanup_url_get;
132 		}
133 		if (EMPTYSTRING(host))
134 			goto cleanup_url_get;
135 		*--path = '/';			/* add / back to real path */
136 		path = strchr(host, '/');	/* remove trailing / on host */
137 		if (! EMPTYSTRING(path))
138 			*path++ = '\0';
139 		path = line;
140 	}
141 
142 	portnum = strchr(host, ':');			/* find portnum */
143 	if (portnum != NULL)
144 		*portnum++ = '\0';
145 
146 	if (debug)
147 		printf("host %s, port %s, path %s, save as %s.\n",
148 		    host, portnum, path, savefile);
149 
150 	memset(&sin, 0, sizeof(sin));
151 	sin.sin_family = AF_INET;
152 
153 	if (isdigit(host[0])) {
154 		if (inet_aton(host, &sin.sin_addr) == 0) {
155 			warnx("Invalid IP address: %s", host);
156 			goto cleanup_url_get;
157 		}
158 	} else {
159 		struct hostent *hp;
160 
161 		hp = gethostbyname(host);
162 		if (hp == NULL) {
163 			warnx("%s: %s", host, hstrerror(h_errno));
164 			goto cleanup_url_get;
165 		}
166 		if (hp->h_addrtype != AF_INET) {
167 			warnx("%s: not an Internet address?", host);
168 			goto cleanup_url_get;
169 		}
170 		memcpy(&sin.sin_addr, hp->h_addr, hp->h_length);
171 	}
172 
173 	if (! EMPTYSTRING(portnum)) {
174 		port = atoi(portnum);
175 		if (port < 1 || (port & 0xffff) != port) {
176 			warnx("Invalid port: %s", portnum);
177 			goto cleanup_url_get;
178 		}
179 		port = htons(port);
180 	} else
181 		port = httpport;
182 	sin.sin_port = port;
183 
184 	s = socket(AF_INET, SOCK_STREAM, 0);
185 	if (s == -1) {
186 		warnx("Can't create socket");
187 		goto cleanup_url_get;
188 	}
189 
190 	if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
191 		warn("Can't connect to %s", host);
192 		goto cleanup_url_get;
193 	}
194 
195 	/*
196 	 * Construct and send the request.  We're expecting a return
197 	 * status of "200". Proxy requests don't want leading /.
198 	 */
199 	if (!proxy)
200 		printf("Requesting %s:%d/%s\n", line, ntohs(port), path);
201 	else
202 		printf("Requesting %s (via %s)\n", line, proxyenv);
203 	snprintf(buf, sizeof(buf), "GET %s%s HTTP/1.0\n\n",
204 	    proxy ? "" : "/", path);
205 	buflen = strlen(buf);
206 	if (write(s, buf, buflen) < buflen) {
207 		warn("write");
208 		goto cleanup_url_get;
209 	}
210 	memset(buf, 0, sizeof(buf));
211 	for (i = 0, buflen = sizeof(buf), cp = buf; i < buflen; cp++, i++) {
212 		if (read(s, cp, 1) != 1)
213 			goto improper;
214 		if (*cp == '\r')
215 			continue;
216 		if (*cp == '\n')
217 			break;
218 	}
219 	buf[buflen - 1] = '\0';		/* sanity */
220 	cp = strchr(buf, ' ');
221 	if (cp == NULL)
222 		goto improper;
223 	else
224 		cp++;
225 	if (strncmp(cp, "200", 3)) {
226 		warnx("Error retrieving file: %s", cp);
227 		goto cleanup_url_get;
228 	}
229 
230 	/*
231 	 * Read the rest of the header.
232 	 */
233 	memset(buf, 0, sizeof(buf));
234 	c = '\0';
235 	for (i = 0, buflen = sizeof(buf), cp = buf; i < buflen; cp++, i++) {
236 		if (read(s, cp, 1) != 1)
237 			goto improper;
238 		if (*cp == '\r')
239 			continue;
240 		if (*cp == '\n' && c == '\n')
241 			break;
242 		c = *cp;
243 	}
244 	buf[buflen - 1] = '\0';		/* sanity */
245 
246 	/*
247 	 * Look for the "Content-length: " header.
248 	 */
249 #define CONTENTLEN "Content-Length: "
250 	for (cp = buf; *cp != '\0'; cp++) {
251 		if (tolower(*cp) == 'c' &&
252 		    strncasecmp(cp, CONTENTLEN, sizeof(CONTENTLEN) - 1) == 0)
253 			break;
254 	}
255 	if (*cp == '\0')
256 		goto improper;
257 	cp += sizeof(CONTENTLEN) - 1;
258 	cp2 = strchr(cp, '\n');
259 	if (cp2 == NULL)
260 		goto improper;
261 	else
262 		*cp2 = '\0';
263 	filesize = atoi(cp);
264 	if (filesize < 1)
265 		goto improper;
266 
267 	/* Open the output file. */
268 	out = open(savefile, O_CREAT | O_WRONLY | O_TRUNC, 0666);
269 	if (out < 0) {
270 		warn("Can't open %s", savefile);
271 		goto cleanup_url_get;
272 	}
273 
274 	/* Trap signals */
275 	oldintr = NULL;
276 	if (setjmp(httpabort)) {
277 		if (oldintr)
278 			(void)signal(SIGINT, oldintr);
279 		goto cleanup_url_get;
280 	}
281 	oldintr = signal(SIGINT, aborthttp);
282 
283 	bytes = 0;
284 	hashbytes = mark;
285 	progressmeter(-1);
286 
287 	/* Finally, suck down the file. */
288 	i = 0;
289 	while ((len = read(s, buf, sizeof(buf))) > 0) {
290 		bytes += len;
291 		for (cp = buf; len > 0; len -= i, cp += i) {
292 			if ((i = write(out, cp, len)) == -1) {
293 				warn("Writing %s", savefile);
294 				goto cleanup_url_get;
295 			}
296 			else if (i == 0)
297 				break;
298 		}
299 		if (hash && !progress) {
300 			while (bytes >= hashbytes) {
301 				(void)putchar('#');
302 				hashbytes += mark;
303 			}
304 			(void)fflush(stdout);
305 		}
306 	}
307 	if (hash && !progress && bytes > 0) {
308 		if (bytes < mark)
309 			(void)putchar('#');
310 		(void)putchar('\n');
311 		(void)fflush(stdout);
312 	}
313 	if (len != 0) {
314 		warn("Reading from socket");
315 		goto cleanup_url_get;
316 	}
317 	progressmeter(1);
318 	if (verbose)
319 		puts("Successfully retrieved file.");
320 	(void)signal(SIGINT, oldintr);
321 
322 	close(s);
323 	close(out);
324 	if (proxy)
325 		free(proxy);
326 	return (0);
327 
328 improper:
329 	warnx("Improper response from %s", host);
330 cleanup_url_get:
331 	if (s != -1)
332 		close(s);
333 	if (proxy)
334 		free(proxy);
335 	return (-1);
336 }
337 
338 /*
339  * Abort a http retrieval
340  */
341 void
342 aborthttp(notused)
343 	int notused;
344 {
345 
346 	alarmtimer(0);
347 	puts("\nhttp fetch aborted.");
348 	(void)fflush(stdout);
349 	longjmp(httpabort, 1);
350 }
351 
352 /*
353  * Retrieve multiple files from the command line, transferring
354  * files of the form "host:path", "ftp://host/path" using the
355  * ftp protocol, and files of the form "http://host/path" using
356  * the http protocol.
357  * If path has a trailing "/", then return (-1);
358  * the path will be cd-ed into and the connection remains open,
359  * and the function will return -1 (to indicate the connection
360  * is alive).
361  * If an error occurs the return value will be the offset+1 in
362  * argv[] of the file that caused a problem (i.e, argv[x]
363  * returns x+1)
364  * Otherwise, 0 is returned if all files retrieved successfully.
365  */
366 int
367 auto_fetch(argc, argv)
368 	int argc;
369 	char *argv[];
370 {
371 	static char lasthost[MAXHOSTNAMELEN];
372 	char *xargv[5];
373 	char *cp, *line, *host, *dir, *file, *portnum;
374 	char *user, *pass;
375 	char *ftpproxy, *httpproxy;
376 	int rval, xargc, argpos;
377 	int dirhasglob, filehasglob;
378 	char rempath[MAXPATHLEN];
379 
380 	argpos = 0;
381 
382 	if (setjmp(toplevel)) {
383 		if (connected)
384 			disconnect(0, NULL);
385 		return (argpos + 1);
386 	}
387 	(void)signal(SIGINT, (sig_t)intr);
388 	(void)signal(SIGPIPE, (sig_t)lostpeer);
389 
390 	ftpproxy = getenv(FTP_PROXY);
391 	httpproxy = getenv(HTTP_PROXY);
392 
393 	/*
394 	 * Loop through as long as there's files to fetch.
395 	 */
396 	for (rval = 0; (rval == 0) && (argpos < argc); free(line), argpos++) {
397 		if (strchr(argv[argpos], ':') == NULL)
398 			break;
399 		host = dir = file = portnum = user = pass = NULL;
400 
401 		/*
402 		 * We muck with the string, so we make a copy.
403 		 */
404 		line = strdup(argv[argpos]);
405 		if (line == NULL)
406 			errx(1, "Can't allocate memory for auto-fetch.");
407 
408 		/*
409 		 * Try HTTP URL-style arguments first.
410 		 */
411 		if (strncasecmp(line, HTTP_URL, sizeof(HTTP_URL) - 1) == 0) {
412 			if (url_get(line, httpproxy) == -1)
413 				rval = argpos + 1;
414 			continue;
415 		}
416 
417 		/*
418 		 * Try FTP URL-style arguments next. If ftpproxy is
419 		 * set, use url_get() instead of standard ftp.
420 		 * Finally, try host:file.
421 		 */
422 		host = line;
423 		if (strncasecmp(line, FTP_URL, sizeof(FTP_URL) - 1) == 0) {
424 			if (ftpproxy) {
425 				if (url_get(line, ftpproxy) == -1)
426 					rval = argpos + 1;
427 				continue;
428 			}
429 			host += sizeof(FTP_URL) - 1;
430 			dir = strchr(host, '/');
431 
432 				/* look for [user:pass@]host[:port] */
433 			pass = strpbrk(host, ":@/");
434 			if (pass == NULL || *pass == '/') {
435 				pass = NULL;
436 				goto parsed_url;
437 			}
438 			if (pass == host || *pass == '@') {
439 bad_ftp_url:
440 				warnx("Bad ftp URL: %s", argv[argpos]);
441 				rval = argpos + 1;
442 				continue;
443 			}
444 			*pass++ = '\0';
445 			cp = strpbrk(pass, ":@/");
446 			if (cp == NULL || *cp == '/') {
447 				portnum = pass;
448 				pass = NULL;
449 				goto parsed_url;
450 			}
451 			if (EMPTYSTRING(cp) || *cp == ':')
452 				goto bad_ftp_url;
453 			*cp++ = '\0';
454 			user = host;
455 			if (EMPTYSTRING(user))
456 				goto bad_ftp_url;
457 			host = cp;
458 			portnum = strchr(host, ':');
459 			if (portnum != NULL)
460 				*portnum++ = '\0';
461 parsed_url:
462 		} else {			/* classic style `host:file' */
463 			dir = strchr(host, ':');
464 		}
465 		if (EMPTYSTRING(host)) {
466 			rval = argpos + 1;
467 			continue;
468 		}
469 
470 		/*
471 		 * If cp is NULL, the file wasn't specified
472 		 * (URL looked something like ftp://host)
473 		 */
474 		if (dir != NULL)
475 			*dir++ = '\0';
476 
477 		/*
478 		 * Extract the file and (if present) directory name.
479 		 */
480 		if (! EMPTYSTRING(dir)) {
481 			cp = strrchr(dir, '/');
482 			if (cp != NULL) {
483 				*cp++ = '\0';
484 				file = cp;
485 			} else {
486 				file = dir;
487 				dir = NULL;
488 			}
489 		}
490 		if (debug)
491 			printf("user %s:%s host %s port %s dir %s file %s\n",
492 			    user, pass, host, portnum, dir, file);
493 
494 		/*
495 		 * Set up the connection if we don't have one.
496 		 */
497 		if (strcmp(host, lasthost) != 0) {
498 			int oautologin;
499 
500 			(void)strcpy(lasthost, host);
501 			if (connected)
502 				disconnect(0, NULL);
503 			xargv[0] = __progname;
504 			xargv[1] = host;
505 			xargv[2] = NULL;
506 			xargc = 2;
507 			if (! EMPTYSTRING(portnum)) {
508 				xargv[2] = portnum;
509 				xargv[3] = NULL;
510 				xargc = 3;
511 			}
512 			oautologin = autologin;
513 			if (user != NULL)
514 				autologin = 0;
515 			setpeer(xargc, xargv);
516 			autologin = oautologin;
517 			if ((connected == 0)
518 			 || ((connected == 1) && !login(host, user, pass)) ) {
519 				warnx("Can't connect or login to host `%s'",
520 				    host);
521 				rval = argpos + 1;
522 				continue;
523 			}
524 
525 			/* Always use binary transfers. */
526 			setbinary(0, NULL);
527 		}
528 			/* cd back to '/' */
529 		xargv[0] = "cd";
530 		xargv[1] = "/";
531 		xargv[2] = NULL;
532 		cd(2, xargv);
533 		if (! dirchange) {
534 			rval = argpos + 1;
535 			continue;
536 		}
537 
538 		dirhasglob = filehasglob = 0;
539 		if (doglob) {
540 			if (! EMPTYSTRING(dir) &&
541 			    strpbrk(dir, "*?[]{}") != NULL)
542 				dirhasglob = 1;
543 			if (! EMPTYSTRING(file) &&
544 			    strpbrk(file, "*?[]{}") != NULL)
545 				filehasglob = 1;
546 		}
547 
548 		/* Change directories, if necessary. */
549 		if (! EMPTYSTRING(dir) && !dirhasglob) {
550 			xargv[0] = "cd";
551 			xargv[1] = dir;
552 			xargv[2] = NULL;
553 			cd(2, xargv);
554 			if (! dirchange) {
555 				rval = argpos + 1;
556 				continue;
557 			}
558 		}
559 
560 		if (EMPTYSTRING(file)) {
561 			rval = -1;
562 			continue;
563 		}
564 
565 		if (!verbose)
566 			printf("Retrieving %s/%s\n", dir ? dir : "", file);
567 
568 		if (dirhasglob) {
569 			snprintf(rempath, sizeof(rempath), "%s/%s", dir, file);
570 			file = rempath;
571 		}
572 
573 		/* Fetch the file(s). */
574 		xargv[0] = "get";
575 		xargv[1] = file;
576 		xargv[2] = NULL;
577 		if (dirhasglob || filehasglob) {
578 			int ointeractive;
579 
580 			ointeractive = interactive;
581 			interactive = 0;
582 			xargv[0] = "mget";
583 			mget(2, xargv);
584 			interactive = ointeractive;
585 		} else
586 			get(2, xargv);
587 
588 		if ((code / 100) != COMPLETE)
589 			rval = argpos + 1;
590 	}
591 	if (connected && rval != -1)
592 		disconnect(0, NULL);
593 	return (rval);
594 }
595