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