fetch.c revision 1.5 1 /* $NetBSD: fetch.c,v 1.5 1997/04/05 03:27: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.5 1997/04/05 03:27: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(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 (strncmp(line, HTTP_URL, sizeof(HTTP_URL) - 1) == 0)
100 host = line + sizeof(HTTP_URL) - 1;
101 else if (strncmp(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 (strncmp(proxy, HTTP_URL, sizeof(HTTP_URL) - 1) == 0)
126 host = proxy + sizeof(HTTP_URL) - 1;
127 else if (strncmp(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 *ftpproxy, *httpproxy;
375 int rval, xargc, argpos;
376 int dirhasglob, filehasglob;
377 char rempath[MAXPATHLEN];
378
379 argpos = 0;
380
381 if (setjmp(toplevel)) {
382 if (connected)
383 disconnect(0, NULL);
384 return (argpos + 1);
385 }
386 (void)signal(SIGINT, (sig_t)intr);
387 (void)signal(SIGPIPE, (sig_t)lostpeer);
388
389 ftpproxy = getenv(FTP_PROXY);
390 httpproxy = getenv(HTTP_PROXY);
391
392 /*
393 * Loop through as long as there's files to fetch.
394 */
395 for (rval = 0; (rval == 0) && (argpos < argc); free(line), argpos++) {
396 if (strchr(argv[argpos], ':') == NULL)
397 break;
398 host = dir = file = portnum = NULL;
399
400 /*
401 * We muck with the string, so we make a copy.
402 */
403 line = strdup(argv[argpos]);
404 if (line == NULL)
405 errx(1, "Can't allocate memory for auto-fetch.");
406
407 /*
408 * Try HTTP URL-style arguments first.
409 */
410 if (strncmp(line, HTTP_URL, sizeof(HTTP_URL) - 1) == 0) {
411 if (url_get(line, httpproxy) == -1)
412 rval = argpos + 1;
413 continue;
414 }
415
416 /*
417 * Try FTP URL-style arguments next. If ftpproxy is
418 * set, use url_get() instead of standard ftp.
419 * Finally, try host:file.
420 */
421 host = line;
422 if (strncmp(line, FTP_URL, sizeof(FTP_URL) - 1) == 0) {
423 if (ftpproxy) {
424 if (url_get(line, ftpproxy) == -1)
425 rval = argpos + 1;
426 continue;
427 }
428 host += sizeof(FTP_URL) - 1;
429 cp = strchr(host, '/');
430
431 /* Look for a port number after the host name. */
432 portnum = strchr(host, ':');
433 if (portnum != NULL)
434 *portnum++ = '\0';
435 } else /* classic style `host:file' */
436 cp = strchr(host, ':');
437 if (EMPTYSTRING(host)) {
438 rval = argpos + 1;
439 continue;
440 }
441
442 /*
443 * If cp is NULL, the file wasn't specified
444 * (URL looked something like ftp://host)
445 */
446 if (cp != NULL)
447 *cp++ = '\0';
448
449 /*
450 * Extract the file and (if present) directory name.
451 */
452 dir = cp;
453 if (! EMPTYSTRING(dir)) {
454 cp = strrchr(cp, '/');
455 if (cp != NULL) {
456 *cp++ = '\0';
457 file = cp;
458 } else {
459 file = dir;
460 dir = NULL;
461 }
462 }
463 if (debug)
464 printf("host '%s', dir '%s', file '%s'\n",
465 host, dir, file);
466
467 /*
468 * Set up the connection if we don't have one.
469 */
470 if (strcmp(host, lasthost) != 0) {
471 (void)strcpy(lasthost, host);
472 if (connected)
473 disconnect(0, NULL);
474 xargv[0] = __progname;
475 xargv[1] = host;
476 xargv[2] = NULL;
477 xargc = 2;
478 if (portnum != NULL) {
479 xargv[2] = portnum;
480 xargv[3] = NULL;
481 xargc = 3;
482 }
483 setpeer(xargc, xargv);
484 if (connected == 0) {
485 warnx("Can't connect to host `%s'", host);
486 rval = argpos + 1;
487 continue;
488 }
489
490 /* Always use binary transfers. */
491 setbinary(0, NULL);
492 }
493 else /* already have connection, cd back to '/' */
494 {
495 xargv[0] = "cd";
496 xargv[1] = "/";
497 xargv[2] = NULL;
498 cd(2, xargv);
499 if (! dirchange) {
500 rval = argpos + 1;
501 continue;
502 }
503 }
504
505 dirhasglob = filehasglob = 0;
506 if (doglob) {
507 if (! EMPTYSTRING(dir) &&
508 strpbrk(dir, "*?[]{}") != NULL)
509 dirhasglob = 1;
510 if (! EMPTYSTRING(file) &&
511 strpbrk(file, "*?[]{}") != NULL)
512 filehasglob = 1;
513 }
514
515 /* Change directories, if necessary. */
516 if (! EMPTYSTRING(dir) && !dirhasglob) {
517 xargv[0] = "cd";
518 xargv[1] = dir;
519 xargv[2] = NULL;
520 cd(2, xargv);
521 if (! dirchange) {
522 rval = argpos + 1;
523 continue;
524 }
525 }
526
527 if (EMPTYSTRING(file)) {
528 rval = -1;
529 continue;
530 }
531
532 if (!verbose)
533 printf("Retrieving %s/%s\n", dir ? dir : "", file);
534
535 if (dirhasglob) {
536 snprintf(rempath, sizeof(rempath), "%s/%s", dir, file);
537 file = rempath;
538 }
539
540 /* Fetch the file(s). */
541 xargv[0] = "get";
542 xargv[1] = file;
543 xargv[2] = NULL;
544 if (dirhasglob || filehasglob) {
545 int ointeractive;
546
547 ointeractive = interactive;
548 interactive = 0;
549 xargv[0] = "mget";
550 mget(2, xargv);
551 interactive = ointeractive;
552 } else
553 get(2, xargv);
554
555 if ((code / 100) != COMPLETE)
556 rval = argpos + 1;
557 }
558 if (connected && rval != -1)
559 disconnect(0, NULL);
560 return (rval);
561 }
562