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