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