fetch.c revision 1.41 1 /* $NetBSD: fetch.c,v 1.41 1998/12/27 05:49:53 lukem Exp $ */
2
3 /*-
4 * Copyright (c) 1997, 1998 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.41 1998/12/27 05:49:53 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 #include <sys/stat.h>
52 #include <sys/time.h>
53
54 #include <netinet/in.h>
55
56 #include <arpa/ftp.h>
57 #include <arpa/inet.h>
58
59 #include <ctype.h>
60 #include <err.h>
61 #include <errno.h>
62 #include <netdb.h>
63 #include <fcntl.h>
64 #include <signal.h>
65 #include <stdio.h>
66 #include <stdlib.h>
67 #include <string.h>
68 #include <unistd.h>
69 #include <util.h>
70
71 #include "ftp_var.h"
72
73 typedef enum {
74 UNKNOWN_URL_T=-1,
75 HTTP_URL_T,
76 FTP_URL_T,
77 FILE_URL_T
78 } url_t;
79
80 static int parse_url __P((const char *, const char *, url_t *, char **,
81 char **, char **, in_port_t *, char **));
82 static int url_get __P((const char *, const char *));
83 void aborthttp __P((int));
84
85
86 #define ABOUT_URL "about:" /* propaganda */
87 #define FILE_URL "file://" /* file URL prefix */
88 #define FTP_URL "ftp://" /* ftp URL prefix */
89 #define HTTP_URL "http://" /* http URL prefix */
90
91
92 #define EMPTYSTRING(x) ((x) == NULL || (*(x) == '\0'))
93 #define FREEPTR(x) if ((x) != NULL) { free(x); (x) = NULL; }
94
95 /*
96 * Parse URL of form:
97 * <type>://[<user>[:<password>@]]<host>[:<port>]/<url-path>
98 * Returns -1 if a parse error occurred, otherwise 0.
99 * Sets type to url_t, each of the given char ** pointers to a
100 * malloc(3)ed strings of the relevant section, and port to
101 * the number given, or ftpport if ftp://, or httpport if http://.
102 */
103 static int
104 parse_url(url, desc, type, user, pass, host, port, path)
105 const char *url;
106 const char *desc;
107 url_t *type;
108 char **user;
109 char **pass;
110 char **host;
111 in_port_t *port;
112 char **path;
113 {
114 char *cp, *ep, *thost;
115
116 if (url == NULL || desc == NULL || type == NULL || user == NULL
117 || pass == NULL || host == NULL || port == NULL || path == NULL)
118 errx(1, "parse_url: invoked with NULL argument!");
119
120 *type = UNKNOWN_URL_T;
121 *user = *pass = *host = *path = NULL;
122 *port = 0;
123
124 if (strncasecmp(url, HTTP_URL, sizeof(HTTP_URL) - 1) == 0) {
125 url += sizeof(HTTP_URL) - 1;
126 *type = HTTP_URL_T;
127 *port = httpport;
128 } else if (strncasecmp(url, FTP_URL, sizeof(FTP_URL) - 1) == 0) {
129 url += sizeof(FTP_URL) - 1;
130 *type = FTP_URL_T;
131 *port = ftpport;
132 } else if (strncasecmp(url, FILE_URL, sizeof(FILE_URL) - 1) == 0) {
133 url += sizeof(FILE_URL) - 1;
134 *type = FILE_URL_T;
135 } else {
136 warnx("Invalid %s `%s'", desc, url);
137 cleanup_parse_url:
138 FREEPTR(*user);
139 FREEPTR(*pass);
140 FREEPTR(*host);
141 FREEPTR(*path);
142 return (-1);
143 }
144
145 if (*url == '\0')
146 return (0);
147
148 /* find [user[:pass]@]host[:port] */
149 ep = strchr(url, '/');
150 if (ep == NULL)
151 thost = xstrdup(url);
152 else {
153 size_t len = ep - url;
154 thost = (char *)xmalloc(len + 1);
155 strncpy(thost, url, len);
156 thost[len] = '\0';
157 *path = xstrdup(ep);
158 }
159
160 cp = strchr(thost, '@');
161 if (cp != NULL) {
162 *user = thost;
163 *cp = '\0';
164 *host = xstrdup(cp + 1);
165 cp = strchr(*user, ':');
166 if (cp != NULL) {
167 *cp = '\0';
168 *pass = xstrdup(cp + 1);
169 }
170 } else
171 *host = thost;
172
173 /* look for [:port] */
174 cp = strrchr(*host, ':');
175 if (cp != NULL) {
176 long nport;
177
178 *cp = '\0';
179 nport = strtol(cp + 1, &ep, 10);
180 if (nport < 1 || nport > MAX_IN_PORT_T || *ep != '\0') {
181 warnx("Invalid port `%s' in %s `%s'", cp, desc, line);
182 goto cleanup_parse_url;
183 }
184 *port = htons((in_port_t)nport);
185 }
186
187 if (debug)
188 fprintf(ttyout,
189 "parse_url: user `%s', pass `%s', host %s:%d, path `%s'\n",
190 *user ? *user : "", *pass ? *pass : "", *host ? *host : "",
191 ntohs(*port), *path ? *path : "");
192
193 return (0);
194 }
195
196
197 jmp_buf httpabort;
198
199 /*
200 * Retrieve URL or classic ftp argument, via the proxy in $proxyvar if
201 * necessary.
202 * Returns -1 on failure, 0 on completed xfer, 1 if ftp connection
203 * is still open (e.g, ftp xfer with trailing /)
204 */
205 static int
206 url_get(url, outfile)
207 const char *url;
208 const char *outfile;
209 {
210 struct sockaddr_in sin;
211 int isredirected, isproxy;
212 volatile int s;
213 size_t len;
214 char *cp, *ep;
215 char *buf, *savefile;
216 volatile sig_t oldintr, oldintp;
217 off_t hashbytes;
218 struct hostent *hp = NULL;
219 int (*closefunc) __P((FILE *));
220 FILE *fin, *fout;
221 int retval;
222 time_t mtime;
223 url_t urltype;
224 char *user, *pass, *host;
225 in_port_t port;
226 char *path;
227 const char *proxyenv;
228
229 closefunc = NULL;
230 fin = fout = NULL;
231 s = -1;
232 buf = savefile = NULL;
233 isredirected = isproxy = 0;
234 retval = -1;
235 proxyenv = NULL;
236
237 #ifdef __GNUC__ /* shut up gcc warnings */
238 (void)&closefunc;
239 (void)&fin;
240 (void)&fout;
241 (void)&buf;
242 (void)&savefile;
243 (void)&retval;
244 (void)&isproxy;
245 #endif
246
247 if (parse_url(url, "URL", &urltype, &user, &pass, &host, &port, &path)
248 == -1)
249 goto cleanup_url_get;
250
251 if (urltype == FILE_URL_T && ! EMPTYSTRING(host)
252 && strcasecmp(host, "localhost") != 0) {
253 warnx("No support for non local file URL `%s'", url);
254 goto cleanup_url_get;
255 }
256
257 if (EMPTYSTRING(path)) {
258 if (urltype == FTP_URL_T)
259 goto noftpautologin;
260 if (urltype != HTTP_URL_T || outfile == NULL) {
261 warnx("Invalid URL (no file after host) `%s'", url);
262 goto cleanup_url_get;
263 }
264 }
265
266 if (outfile)
267 savefile = xstrdup(outfile);
268 else {
269 cp = strrchr(path, '/'); /* find savefile */
270 if (cp != NULL)
271 savefile = xstrdup(cp + 1);
272 else
273 savefile = xstrdup(path);
274 }
275 if (EMPTYSTRING(savefile)) {
276 if (urltype == FTP_URL_T)
277 goto noftpautologin;
278 warnx("Invalid URL (no file after directory) `%s'", url);
279 goto cleanup_url_get;
280 }
281
282 filesize = -1;
283 mtime = -1;
284 if (urltype == FILE_URL_T) { /* file:// URLs */
285 struct stat sb;
286
287 direction = "copied";
288 fin = fopen(path, "r");
289 if (fin == NULL) {
290 warn("Cannot open file `%s'", path);
291 goto cleanup_url_get;
292 }
293 if (fstat(fileno(fin), &sb) == 0) {
294 mtime = sb.st_mtime;
295 filesize = sb.st_size;
296 }
297 fprintf(ttyout, "Copying %s\n", path);
298 } else { /* ftp:// or http:// URLs */
299 if (urltype == HTTP_URL_T)
300 proxyenv = httpproxy;
301 else if (urltype == FTP_URL_T)
302 proxyenv = ftpproxy;
303 direction = "retrieved";
304 if (proxyenv != NULL) { /* use proxy */
305 url_t purltype;
306 char *puser, *ppass, *phost;
307 char *ppath;
308
309 isproxy = 1;
310
311 /* check URL against list of no_proxied sites */
312 if (no_proxy != NULL) {
313 char *np, *np_copy;
314 long np_port;
315 size_t hlen, plen;
316
317 np_copy = xstrdup(no_proxy);
318 hlen = strlen(host);
319 while ((cp = strsep(&np_copy, " ,")) != NULL) {
320 if (*cp == '\0')
321 continue;
322 if ((np = strchr(cp, ':')) != NULL) {
323 *np = '\0';
324 np_port =
325 strtol(np + 1, &ep, 10);
326 if (*ep != '\0')
327 continue;
328 if (port !=
329 htons((in_port_t)np_port))
330 continue;
331 }
332 plen = strlen(cp);
333 if (strncasecmp(host + hlen - plen,
334 cp, plen) == 0) {
335 isproxy = 0;
336 break;
337 }
338 }
339 FREEPTR(np_copy);
340 }
341
342 if (isproxy) {
343 if (parse_url(proxyenv, "proxy URL", &purltype,
344 &puser, &ppass, &phost, &port, &ppath)
345 == -1)
346 goto cleanup_url_get;
347
348 if ((purltype != HTTP_URL_T
349 && purltype != FTP_URL_T) ||
350 EMPTYSTRING(phost) ||
351 (! EMPTYSTRING(ppath)
352 && strcmp(ppath, "/") != 0)) {
353 warnx("Malformed proxy URL `%s'",
354 proxyenv);
355 FREEPTR(puser);
356 FREEPTR(ppass);
357 FREEPTR(phost);
358 FREEPTR(ppath);
359 goto cleanup_url_get;
360 }
361
362 FREEPTR(user);
363 user = puser;
364 FREEPTR(pass);
365 pass = ppass;
366 FREEPTR(host);
367 host = phost;
368 FREEPTR(path);
369 FREEPTR(ppath);
370 path = xstrdup(url);
371 }
372 } /* proxyenv != NULL */
373
374 memset(&sin, 0, sizeof(sin));
375 sin.sin_family = AF_INET;
376
377 if (isdigit((unsigned char)host[0])) {
378 if (inet_aton(host, &sin.sin_addr) == 0) {
379 warnx("Invalid IP address `%s'", host);
380 goto cleanup_url_get;
381 }
382 } else {
383 hp = gethostbyname(host);
384 if (hp == NULL) {
385 warnx("%s: %s", host, hstrerror(h_errno));
386 goto cleanup_url_get;
387 }
388 if (hp->h_addrtype != AF_INET) {
389 warnx("`%s': not an Internet address?", host);
390 goto cleanup_url_get;
391 }
392 memcpy(&sin.sin_addr, hp->h_addr, hp->h_length);
393 }
394
395 if (port == 0) {
396 warnx("Unknown port for URL `%s'", url);
397 goto cleanup_url_get;
398 }
399 sin.sin_port = port;
400
401 s = socket(AF_INET, SOCK_STREAM, 0);
402 if (s == -1) {
403 warn("Can't create socket");
404 goto cleanup_url_get;
405 }
406
407 while (xconnect(s, (struct sockaddr *)&sin,
408 sizeof(sin)) == -1) {
409 if (errno == EINTR)
410 continue;
411 if (hp && hp->h_addr_list[1]) {
412 int oerrno = errno;
413 char *ia;
414
415 ia = inet_ntoa(sin.sin_addr);
416 errno = oerrno;
417 warn("Connect to address `%s'", ia);
418 hp->h_addr_list++;
419 memcpy(&sin.sin_addr, hp->h_addr_list[0],
420 (size_t)hp->h_length);
421 fprintf(ttyout, "Trying %s...\n",
422 inet_ntoa(sin.sin_addr));
423 (void)close(s);
424 s = socket(AF_INET, SOCK_STREAM, 0);
425 if (s < 0) {
426 warn("Can't create socket");
427 goto cleanup_url_get;
428 }
429 continue;
430 }
431 warn("Can't connect to `%s'", host);
432 goto cleanup_url_get;
433 }
434
435 fin = fdopen(s, "r+");
436 /*
437 * Construct and send the request.
438 * Proxy requests don't want leading /.
439 */
440 if (isproxy) {
441 fprintf(ttyout, "Requesting %s\n (via %s)\n",
442 url, proxyenv);
443 fprintf(fin, "GET %s HTTP/1.0\r\n\r\n", path);
444 } else {
445 fprintf(ttyout, "Requesting %s\n", url);
446 fprintf(fin, "GET %s HTTP/1.1\r\n", path);
447 fprintf(fin, "Host: %s\r\n", host);
448 fprintf(fin, "Accept: */*\r\n");
449 fprintf(fin, "Connection: close\r\n\r\n");
450 }
451 if (fflush(fin) == EOF) {
452 warn("Writing HTTP request");
453 goto cleanup_url_get;
454 }
455
456 /* Read the response */
457 if ((buf = fparseln(fin, &len, NULL, "\0\0\0", 0)) == NULL) {
458 warn("Receiving HTTP reply");
459 goto cleanup_url_get;
460 }
461 while (len > 0 && (buf[len-1] == '\r' || buf[len-1] == '\n'))
462 buf[--len] = '\0';
463 if (debug)
464 fprintf(ttyout, "received `%s'\n", buf);
465
466 cp = strchr(buf, ' ');
467 if (cp == NULL)
468 goto improper;
469 else
470 cp++;
471 if (strncmp(cp, "301", 3) == 0 || strncmp(cp, "302", 3) == 0) {
472 isredirected++;
473 } else if (strncmp(cp, "200", 3)) {
474 warnx("Error retrieving file `%s'", cp);
475 goto cleanup_url_get;
476 }
477
478 /* Read the rest of the header. */
479 FREEPTR(buf);
480 while (1) {
481 if ((buf = fparseln(fin, &len, NULL, "\0\0\0", 0))
482 == NULL) {
483 warn("Receiving HTTP reply");
484 goto cleanup_url_get;
485 }
486 while (len > 0 &&
487 (buf[len-1] == '\r' || buf[len-1] == '\n'))
488 buf[--len] = '\0';
489 if (len == 0)
490 break;
491 if (debug)
492 fprintf(ttyout, "received `%s'\n", buf);
493
494 /* Look for some headers */
495 cp = buf;
496 #define CONTENTLEN "Content-Length: "
497 if (strncasecmp(cp, CONTENTLEN,
498 sizeof(CONTENTLEN) - 1) == 0) {
499 cp += sizeof(CONTENTLEN) - 1;
500 filesize = strtol(cp, &ep, 10);
501 if (filesize < 1 || *ep != '\0')
502 goto improper;
503 if (debug)
504 fprintf(ttyout,
505 #ifndef NO_QUAD
506 "parsed length as: %qd\n",
507 (long long)filesize);
508 #else
509 "parsed length as: %ld\n",
510 (long)filesize);
511 #endif
512 #define LASTMOD "Last-Modified: "
513 } else if (strncasecmp(cp, LASTMOD,
514 sizeof(LASTMOD) - 1) == 0) {
515 struct tm parsed;
516 char *t;
517
518 cp += sizeof(LASTMOD) - 1;
519 /* RFC 1123 */
520 if ((t = strptime(cp,
521 "%a, %d %b %Y %H:%M:%S GMT",
522 &parsed))
523 /* RFC 850 */
524 || (t = strptime(cp,
525 "%a, %d-%b-%y %H:%M:%S GMT",
526 &parsed))
527 /* asctime */
528 || (t = strptime(cp,
529 "%a, %b %d %H:%M:%S %Y",
530 &parsed))) {
531 parsed.tm_isdst = -1;
532 if (*t == '\0')
533 mtime = mkgmtime(&parsed);
534 if (debug && mtime != -1) {
535 fprintf(ttyout,
536 "parsed date as: %s",
537 ctime(&mtime));
538 }
539 }
540 #define LOCATION "Location: "
541 } else if (isredirected &&
542 strncasecmp(cp, LOCATION,
543 sizeof(LOCATION) - 1) == 0) {
544 cp += sizeof(LOCATION) - 1;
545 if (debug)
546 fprintf(ttyout,
547 "parsed location as: %s\n", cp);
548 if (verbose)
549 fprintf(ttyout,
550 "Redirected to %s\n", cp);
551 retval = url_get(cp, outfile);
552 goto cleanup_url_get;
553 }
554 }
555 FREEPTR(buf);
556 }
557
558 oldintr = oldintp = NULL;
559
560 /* Open the output file. */
561 if (strcmp(savefile, "-") == 0) {
562 fout = stdout;
563 } else if (*savefile == '|') {
564 oldintp = signal(SIGPIPE, SIG_IGN);
565 fout = popen(savefile + 1, "w");
566 if (fout == NULL) {
567 warn("Can't run `%s'", savefile + 1);
568 goto cleanup_url_get;
569 }
570 closefunc = pclose;
571 } else {
572 fout = fopen(savefile, "w");
573 if (fout == NULL) {
574 warn("Can't open `%s'", savefile);
575 goto cleanup_url_get;
576 }
577 closefunc = fclose;
578 }
579
580 /* Trap signals */
581 if (setjmp(httpabort)) {
582 if (oldintr)
583 (void)signal(SIGINT, oldintr);
584 if (oldintp)
585 (void)signal(SIGPIPE, oldintp);
586 goto cleanup_url_get;
587 }
588 oldintr = signal(SIGINT, aborthttp);
589
590 bytes = 0;
591 hashbytes = mark;
592 progressmeter(-1);
593
594 /* Finally, suck down the file. */
595 buf = xmalloc(BUFSIZ);
596 while ((len = fread(buf, sizeof(char), BUFSIZ, fin)) > 0) {
597 bytes += len;
598 if (fwrite(buf, sizeof(char), len, fout) != len) {
599 warn("Writing `%s'", savefile);
600 goto cleanup_url_get;
601 }
602 if (hash && !progress) {
603 while (bytes >= hashbytes) {
604 (void)putc('#', ttyout);
605 hashbytes += mark;
606 }
607 (void)fflush(ttyout);
608 }
609 }
610 if (hash && !progress && bytes > 0) {
611 if (bytes < mark)
612 (void)putc('#', ttyout);
613 (void)putc('\n', ttyout);
614 (void)fflush(ttyout);
615 }
616 if (ferror(fin)) {
617 warn("Reading file");
618 goto cleanup_url_get;
619 }
620 progressmeter(1);
621 (void)fflush(fout);
622 (void)signal(SIGINT, oldintr);
623 if (oldintp)
624 (void)signal(SIGPIPE, oldintp);
625 if (closefunc == fclose && mtime != -1) {
626 struct timeval tval[2];
627
628 (void)gettimeofday(&tval[0], NULL);
629 tval[1].tv_sec = mtime;
630 tval[1].tv_usec = 0;
631 (*closefunc)(fout);
632 fout = NULL;
633
634 if (utimes(savefile, tval) == -1) {
635 fprintf(ttyout,
636 "Can't change modification time to %s",
637 asctime(localtime(&mtime)));
638 }
639 }
640 if (bytes > 0)
641 ptransfer(0);
642
643 retval = 0;
644 goto cleanup_url_get;
645
646 noftpautologin:
647 warnx(
648 "Auto-login using ftp URLs isn't supported when using $ftp_proxy");
649 goto cleanup_url_get;
650
651 improper:
652 warnx("Improper response from `%s'", host);
653
654 cleanup_url_get:
655 resetsockbufsize();
656 if (fin != NULL)
657 fclose(fin);
658 else if (s != -1)
659 close(s);
660 if (closefunc != NULL && fout != NULL)
661 (*closefunc)(fout);
662 FREEPTR(savefile);
663 FREEPTR(user);
664 FREEPTR(pass);
665 FREEPTR(host);
666 FREEPTR(path);
667 FREEPTR(buf);
668 return (retval);
669 }
670
671 /*
672 * Abort a http retrieval
673 */
674 void
675 aborthttp(notused)
676 int notused;
677 {
678
679 alarmtimer(0);
680 fputs("\nHTTP fetch aborted.\n", ttyout);
681 (void)fflush(ttyout);
682 longjmp(httpabort, 1);
683 }
684
685 /*
686 * Retrieve multiple files from the command line, transferring
687 * URLs of the form "host:path", "ftp://host/path" using the
688 * ftp protocol, URLs of the form "http://host/path" using the
689 * http protocol, and URLs of the form "file:///" by simple
690 * copying.
691 * If path has a trailing "/", the path will be cd-ed into and
692 * the connection remains open, and the function will return -1
693 * (to indicate the connection is alive).
694 * If an error occurs the return value will be the offset+1 in
695 * argv[] of the file that caused a problem (i.e, argv[x]
696 * returns x+1)
697 * Otherwise, 0 is returned if all files retrieved successfully.
698 */
699 int
700 auto_fetch(argc, argv, outfile)
701 int argc;
702 char *argv[];
703 char *outfile;
704 {
705 static char lasthost[MAXHOSTNAMELEN];
706 char portnum[6]; /* large enough for "65535\0" */
707 char *xargv[5];
708 const char *line;
709 char *cp, *host, *path, *dir, *file;
710 char *user, *pass;
711 in_port_t port;
712 int rval, xargc;
713 volatile int argpos;
714 int dirhasglob, filehasglob;
715 char rempath[MAXPATHLEN];
716
717 #ifdef __GNUC__ /* to shut up gcc warnings */
718 (void)&outfile;
719 #endif
720
721 argpos = 0;
722
723 if (setjmp(toplevel)) {
724 if (connected)
725 disconnect(0, NULL);
726 return (argpos + 1);
727 }
728 (void)signal(SIGINT, (sig_t)intr);
729 (void)signal(SIGPIPE, (sig_t)lostpeer);
730
731 /*
732 * Loop through as long as there's files to fetch.
733 */
734 for (rval = 0; (rval == 0) && (argpos < argc); argpos++) {
735 if (strchr(argv[argpos], ':') == NULL)
736 break;
737 host = path = dir = file = user = pass = NULL;
738 port = 0;
739 line = argv[argpos];
740
741 #ifndef SMALL
742 /*
743 * Check for about:*
744 */
745 if (strncasecmp(line, ABOUT_URL, sizeof(ABOUT_URL) - 1) == 0) {
746 line += sizeof(ABOUT_URL) -1;
747 if (strcasecmp(line, "ftp") == 0) {
748 fprintf(ttyout, "%s\n%s\n",
749 "This version of ftp has been enhanced by Luke Mewburn <lukem (at) netbsd.org>.",
750 "Execute 'man ftp' for more details");
751 } else if (strcasecmp(line, "netbsd") == 0) {
752 fprintf(ttyout, "%s\n%s\n",
753 "NetBSD is a freely available and redistributable UNIX-like operating system.",
754 "For more information, see http://www.netbsd.org/index.html");
755 } else {
756 fprintf(ttyout,
757 "`%s' is an interesting topic.\n", line);
758 }
759 continue;
760 }
761 #endif /* SMALL */
762
763 /*
764 * Check for file:// and http:// URLs.
765 */
766 if (strncasecmp(line, HTTP_URL, sizeof(HTTP_URL) - 1) == 0 ||
767 strncasecmp(line, FILE_URL, sizeof(FILE_URL) - 1) == 0) {
768 if (url_get(line, outfile) == -1)
769 rval = argpos + 1;
770 continue;
771 }
772
773 /*
774 * Try FTP URL-style arguments next. If ftpproxy is
775 * set, use url_get() instead of standard ftp.
776 * Finally, try host:file.
777 */
778 if (strncasecmp(line, FTP_URL, sizeof(FTP_URL) - 1) == 0) {
779 url_t urltype;
780
781 if (ftpproxy) {
782 if (url_get(line, outfile) == -1)
783 rval = argpos + 1;
784 continue;
785 }
786 if ((parse_url(line, "URL", &urltype, &user, &pass,
787 &host, &port, &path) == -1) ||
788 (user != NULL && *user == '\0') ||
789 (pass != NULL && *pass == '\0') ||
790 EMPTYSTRING(host)) {
791 warnx("Invalid URL `%s'", argv[argpos]);
792 rval = argpos + 1;
793 break;
794 }
795 } else { /* classic style `host:file' */
796 host = xstrdup(line);
797 cp = strchr(host, ':');
798 if (cp != NULL) {
799 *cp = '\0';
800 path = xstrdup(cp + 1);
801 }
802 }
803 if (EMPTYSTRING(host)) {
804 rval = argpos + 1;
805 break;
806 }
807
808 /*
809 * Extract the file and (if present) directory name.
810 */
811 dir = path;
812 if (! EMPTYSTRING(dir)) {
813 if (*dir == '/')
814 dir++; /* skip leading / */
815 cp = strrchr(dir, '/');
816 if (cp != NULL) {
817 *cp++ = '\0';
818 file = cp;
819 } else {
820 file = dir;
821 dir = NULL;
822 }
823 }
824 if (debug)
825 fprintf(ttyout,
826 "auto_fetch: user `%s', pass `%s', host %s:%d, path, `%s', dir `%s', file `%s'\n",
827 user ? user : "", pass ? pass : "",
828 host ? host : "", ntohs(port), path ? path : "",
829 dir ? dir : "", file ? file : "");
830
831 dirhasglob = filehasglob = 0;
832 if (doglob) {
833 if (! EMPTYSTRING(dir) &&
834 strpbrk(dir, "*?[]{}") != NULL)
835 dirhasglob = 1;
836 if (! EMPTYSTRING(file) &&
837 strpbrk(file, "*?[]{}") != NULL)
838 filehasglob = 1;
839 }
840
841 /*
842 * Set up the connection if we don't have one.
843 */
844 if (strcasecmp(host, lasthost) != 0) {
845 int oautologin;
846
847 (void)strcpy(lasthost, host);
848 if (connected)
849 disconnect(0, NULL);
850 xargv[0] = __progname;
851 xargv[1] = host;
852 xargv[2] = NULL;
853 xargc = 2;
854 if (port) {
855 snprintf(portnum, sizeof(portnum),
856 "%d", ntohs(port));
857 xargv[2] = portnum;
858 xargv[3] = NULL;
859 xargc = 3;
860 }
861 oautologin = autologin;
862 if (user != NULL)
863 autologin = 0;
864 setpeer(xargc, xargv);
865 autologin = oautologin;
866 if ((connected == 0)
867 || ((connected == 1) &&
868 !ftp_login(host, user, pass)) ) {
869 warnx("Can't connect or login to host `%s'",
870 host);
871 rval = argpos + 1;
872 break;
873 }
874
875 /* Always use binary transfers. */
876 setbinary(0, NULL);
877 } else {
878 /* connection exists, cd back to `/' */
879 xargv[0] = "cd";
880 xargv[1] = "/";
881 xargv[2] = NULL;
882 dirchange = 0;
883 cd(2, xargv);
884 if (! dirchange) {
885 rval = argpos + 1;
886 break;
887 }
888 }
889
890 /* Change directories, if necessary. */
891 if (! EMPTYSTRING(dir) && !dirhasglob) {
892 xargv[0] = "cd";
893 xargv[1] = dir;
894 xargv[2] = NULL;
895 dirchange = 0;
896 cd(2, xargv);
897 if (! dirchange) {
898 rval = argpos + 1;
899 break;
900 }
901 }
902
903 if (EMPTYSTRING(file)) {
904 rval = -1;
905 break;
906 }
907
908 if (!verbose)
909 fprintf(ttyout, "Retrieving %s/%s\n", dir ? dir : "",
910 file);
911
912 if (dirhasglob) {
913 snprintf(rempath, sizeof(rempath), "%s/%s", dir, file);
914 file = rempath;
915 }
916
917 /* Fetch the file(s). */
918 xargc = 2;
919 xargv[0] = "get";
920 xargv[1] = file;
921 xargv[2] = NULL;
922 if (dirhasglob || filehasglob) {
923 int ointeractive;
924
925 ointeractive = interactive;
926 interactive = 0;
927 xargv[0] = "mget";
928 mget(xargc, xargv);
929 interactive = ointeractive;
930 } else {
931 if (outfile != NULL) {
932 xargv[2] = outfile;
933 xargv[3] = NULL;
934 xargc++;
935 }
936 get(xargc, xargv);
937 if (outfile != NULL && strcmp(outfile, "-") != 0
938 && outfile[0] != '|')
939 outfile = NULL;
940 }
941
942 if ((code / 100) != COMPLETE)
943 rval = argpos + 1;
944 }
945 if (connected && rval != -1)
946 disconnect(0, NULL);
947 FREEPTR(host);
948 FREEPTR(path);
949 FREEPTR(user);
950 FREEPTR(pass);
951 return (rval);
952 }
953