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