fetch.c revision 1.1 1 1.1 lukem /* $NetBSD: fetch.c,v 1.1 1997/01/19 14:19:10 lukem Exp $ */
2 1.1 lukem
3 1.1 lukem /*-
4 1.1 lukem * Copyright (c) 1997 The NetBSD Foundation, Inc.
5 1.1 lukem * All rights reserved.
6 1.1 lukem *
7 1.1 lukem * This code is derived from software contributed to The NetBSD Foundation
8 1.1 lukem * by Jason Thorpe and Luke Mewburn.
9 1.1 lukem *
10 1.1 lukem * Redistribution and use in source and binary forms, with or without
11 1.1 lukem * modification, are permitted provided that the following conditions
12 1.1 lukem * are met:
13 1.1 lukem * 1. Redistributions of source code must retain the above copyright
14 1.1 lukem * notice, this list of conditions and the following disclaimer.
15 1.1 lukem * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 lukem * notice, this list of conditions and the following disclaimer in the
17 1.1 lukem * documentation and/or other materials provided with the distribution.
18 1.1 lukem * 3. All advertising materials mentioning features or use of this software
19 1.1 lukem * must display the following acknowledgement:
20 1.1 lukem * This product includes software developed by the NetBSD
21 1.1 lukem * Foundation, Inc. and its contributors.
22 1.1 lukem * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 lukem * contributors may be used to endorse or promote products derived
24 1.1 lukem * from this software without specific prior written permission.
25 1.1 lukem *
26 1.1 lukem * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 lukem * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 lukem * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 lukem * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
30 1.1 lukem * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 lukem * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 lukem * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 lukem * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 lukem * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 lukem * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 lukem * POSSIBILITY OF SUCH DAMAGE.
37 1.1 lukem */
38 1.1 lukem
39 1.1 lukem #ifndef lint
40 1.1 lukem static char rcsid[] = "$NetBSD: fetch.c,v 1.1 1997/01/19 14:19:10 lukem Exp $";
41 1.1 lukem #endif /* not lint */
42 1.1 lukem
43 1.1 lukem /*
44 1.1 lukem * FTP User Program -- Command line file retrieval
45 1.1 lukem */
46 1.1 lukem
47 1.1 lukem #include <sys/types.h>
48 1.1 lukem #include <sys/param.h>
49 1.1 lukem #include <sys/socket.h>
50 1.1 lukem
51 1.1 lukem #include <netinet/in.h>
52 1.1 lukem
53 1.1 lukem #include <arpa/inet.h>
54 1.1 lukem
55 1.1 lukem #include <ctype.h>
56 1.1 lukem #include <err.h>
57 1.1 lukem #include <netdb.h>
58 1.1 lukem #include <fcntl.h>
59 1.1 lukem #include <stdio.h>
60 1.1 lukem #include <stdlib.h>
61 1.1 lukem #include <string.h>
62 1.1 lukem #include <unistd.h>
63 1.1 lukem
64 1.1 lukem #include "ftp_var.h"
65 1.1 lukem
66 1.1 lukem #define FTP_URL "ftp://" /* ftp URL prefix */
67 1.1 lukem #define HTTP_URL "http://" /* http URL prefix */
68 1.1 lukem #define HTTP_PROXY "http_proxy" /* env var with http proxy location */
69 1.1 lukem
70 1.1 lukem
71 1.1 lukem #define EMPTYSTRING(x) ((x) == NULL || (*(x) == '\0'))
72 1.1 lukem
73 1.1 lukem /*
74 1.1 lukem * Retrieve an http:// URL, via a proxy if necessary.
75 1.1 lukem * Modifies the string argument given.
76 1.1 lukem * Returns -1 on failure, 0 on success
77 1.1 lukem */
78 1.1 lukem int
79 1.1 lukem http_get(line)
80 1.1 lukem char *line;
81 1.1 lukem {
82 1.1 lukem struct sockaddr_in sin;
83 1.1 lukem int i, out, port, s;
84 1.1 lukem size_t buflen, len;
85 1.1 lukem char c, *cp, *cp2, *savefile, *portnum, *path, buf[4096];
86 1.1 lukem char *proxy, *host;
87 1.1 lukem off_t hashbytes;
88 1.1 lukem
89 1.1 lukem s = -1;
90 1.1 lukem
91 1.1 lukem host = line + sizeof(HTTP_URL) - 1;
92 1.1 lukem path = strchr(host, '/'); /* find path */
93 1.1 lukem if (EMPTYSTRING(path))
94 1.1 lukem goto cleanup_http_get;
95 1.1 lukem *path++ = '\0';
96 1.1 lukem if (EMPTYSTRING(path))
97 1.1 lukem goto cleanup_http_get;
98 1.1 lukem
99 1.1 lukem savefile = strrchr(path, '/'); /* find savefile */
100 1.1 lukem if (savefile != NULL)
101 1.1 lukem savefile++;
102 1.1 lukem else
103 1.1 lukem savefile = path;
104 1.1 lukem if (EMPTYSTRING(savefile))
105 1.1 lukem goto cleanup_http_get;
106 1.1 lukem
107 1.1 lukem proxy = getenv(HTTP_PROXY);
108 1.1 lukem if (proxy != NULL) { /* use proxy */
109 1.1 lukem if (strncmp(proxy, HTTP_URL, sizeof(HTTP_URL) - 1) != 0) {
110 1.1 lukem warnx("Malformed proxy url: %s", proxy);
111 1.1 lukem goto cleanup_http_get;
112 1.1 lukem }
113 1.1 lukem proxy = strdup(proxy);
114 1.1 lukem if (proxy == NULL)
115 1.1 lukem errx(1, "Can't allocate memory for proxy url.");
116 1.1 lukem host = proxy + sizeof(HTTP_URL) - 1;
117 1.1 lukem if (EMPTYSTRING(host))
118 1.1 lukem goto cleanup_http_get;
119 1.1 lukem *--path = '/'; /* add / back to real path */
120 1.1 lukem path = strchr(host, '/'); /* remove trailing / on host */
121 1.1 lukem if (! EMPTYSTRING(path))
122 1.1 lukem *path++ = '\0';
123 1.1 lukem path = line;
124 1.1 lukem }
125 1.1 lukem
126 1.1 lukem portnum = strchr(host, ':'); /* find portnum */
127 1.1 lukem if (portnum != NULL)
128 1.1 lukem *portnum++ = '\0';
129 1.1 lukem
130 1.1 lukem if (debug)
131 1.1 lukem printf("host %s, port %s, path %s, save as %s.\n",
132 1.1 lukem host, portnum, path, savefile);
133 1.1 lukem
134 1.1 lukem memset(&sin, 0, sizeof(sin));
135 1.1 lukem sin.sin_family = AF_INET;
136 1.1 lukem
137 1.1 lukem if (isdigit(host[0])) {
138 1.1 lukem if (inet_aton(host, &sin.sin_addr) == 0) {
139 1.1 lukem warnx("invalid IP address: %s", host);
140 1.1 lukem goto cleanup_http_get;
141 1.1 lukem }
142 1.1 lukem } else {
143 1.1 lukem struct hostent *hp;
144 1.1 lukem
145 1.1 lukem hp = gethostbyname(host);
146 1.1 lukem if (hp == NULL) {
147 1.1 lukem warnx("unknown host: %s", host);
148 1.1 lukem goto cleanup_http_get;
149 1.1 lukem }
150 1.1 lukem if (hp->h_addrtype != AF_INET) {
151 1.1 lukem warnx("%s: not an Internet address?", host);
152 1.1 lukem goto cleanup_http_get;
153 1.1 lukem }
154 1.1 lukem memcpy(&sin.sin_addr, hp->h_addr, hp->h_length);
155 1.1 lukem }
156 1.1 lukem
157 1.1 lukem if (! EMPTYSTRING(portnum)) {
158 1.1 lukem port = atoi(portnum);
159 1.1 lukem if (port < 1 || (port & 0xffff) != port) {
160 1.1 lukem warnx("invalid port: %s", portnum);
161 1.1 lukem goto cleanup_http_get;
162 1.1 lukem }
163 1.1 lukem port = htons(port);
164 1.1 lukem } else
165 1.1 lukem port = httpport;
166 1.1 lukem sin.sin_port = port;
167 1.1 lukem
168 1.1 lukem s = socket(AF_INET, SOCK_STREAM, 0);
169 1.1 lukem if (s == -1) {
170 1.1 lukem warnx("Can't create socket");
171 1.1 lukem goto cleanup_http_get;
172 1.1 lukem }
173 1.1 lukem
174 1.1 lukem if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
175 1.1 lukem warn("Can't connect to %s", host);
176 1.1 lukem goto cleanup_http_get;
177 1.1 lukem }
178 1.1 lukem printf("Connected to %s%s.\n", proxy ? "proxy " : "", host);
179 1.1 lukem
180 1.1 lukem /*
181 1.1 lukem * Construct and send the request. We're expecting a return
182 1.1 lukem * status of "200". Proxy requests don't want leading /.
183 1.1 lukem */
184 1.1 lukem printf("Requesting %s%s\n", proxy ? "" : "/", path);
185 1.1 lukem snprintf(buf, sizeof(buf), "GET %s%s HTTP/1.0\n\n",
186 1.1 lukem proxy ? "" : "/", path);
187 1.1 lukem buflen = strlen(buf);
188 1.1 lukem if (write(s, buf, buflen) < buflen) {
189 1.1 lukem warn("write");
190 1.1 lukem goto cleanup_http_get;
191 1.1 lukem }
192 1.1 lukem memset(buf, 0, sizeof(buf));
193 1.1 lukem for (i = 0, buflen = sizeof(buf), cp = buf; i < buflen; cp++, i++) {
194 1.1 lukem if (read(s, cp, 1) != 1)
195 1.1 lukem goto improper;
196 1.1 lukem if (*cp == '\n')
197 1.1 lukem break;
198 1.1 lukem }
199 1.1 lukem buf[buflen - 1] = '\0'; /* sanity */
200 1.1 lukem cp = strchr(buf, ' ');
201 1.1 lukem if (cp == NULL)
202 1.1 lukem goto improper;
203 1.1 lukem else
204 1.1 lukem cp++;
205 1.1 lukem if (strncmp(cp, "200", 3)) {
206 1.1 lukem warnx("Error retrieving file: %s", cp);
207 1.1 lukem goto cleanup_http_get;
208 1.1 lukem }
209 1.1 lukem
210 1.1 lukem /*
211 1.1 lukem * Read the rest of the header.
212 1.1 lukem */
213 1.1 lukem memset(buf, 0, sizeof(buf));
214 1.1 lukem c = '\0';
215 1.1 lukem for (i = 0, buflen = sizeof(buf), cp = buf; i < buflen; cp++, i++) {
216 1.1 lukem if (read(s, cp, 1) != 1)
217 1.1 lukem goto improper;
218 1.1 lukem if (*cp == '\n' && c == '\n')
219 1.1 lukem break;
220 1.1 lukem c = *cp;
221 1.1 lukem }
222 1.1 lukem buf[buflen - 1] = '\0'; /* sanity */
223 1.1 lukem
224 1.1 lukem /*
225 1.1 lukem * Look for the "Content-length: " header.
226 1.1 lukem */
227 1.1 lukem #define CONTENTLEN "Content-Length: "
228 1.1 lukem for (cp = buf; *cp != '\0'; cp++) {
229 1.1 lukem if (tolower(*cp) == 'c' &&
230 1.1 lukem strncasecmp(cp, CONTENTLEN, sizeof(CONTENTLEN) - 1) == 0)
231 1.1 lukem break;
232 1.1 lukem }
233 1.1 lukem if (*cp == '\0')
234 1.1 lukem goto improper;
235 1.1 lukem cp += sizeof(CONTENTLEN) - 1;
236 1.1 lukem cp2 = strchr(cp, '\n');
237 1.1 lukem if (cp2 == NULL)
238 1.1 lukem goto improper;
239 1.1 lukem else
240 1.1 lukem *cp2 = '\0';
241 1.1 lukem filesize = atoi(cp);
242 1.1 lukem if (filesize < 1)
243 1.1 lukem goto improper;
244 1.1 lukem
245 1.1 lukem /* Open the output file. */
246 1.1 lukem out = open(savefile, O_CREAT | O_WRONLY | O_TRUNC, 0666);
247 1.1 lukem if (out < 0) {
248 1.1 lukem warn("Can't open %s", savefile);
249 1.1 lukem goto cleanup_http_get;
250 1.1 lukem }
251 1.1 lukem
252 1.1 lukem bytes = 0;
253 1.1 lukem hashbytes = mark;
254 1.1 lukem progressmeter(-1);
255 1.1 lukem /* XXX trap signals... */
256 1.1 lukem /*
257 1.1 lukem * Finally, suck down the file.
258 1.1 lukem */
259 1.1 lukem i = 0;
260 1.1 lukem while ((len = read(s, buf, sizeof(buf))) > 0) {
261 1.1 lukem bytes += len;
262 1.1 lukem for (cp = buf; len > 0; len -= i, cp += i) {
263 1.1 lukem if ((i = write(out, cp, len)) == -1) {
264 1.1 lukem warn("Writing %s", savefile);
265 1.1 lukem goto cleanup_http_get;
266 1.1 lukem }
267 1.1 lukem else if (i == 0)
268 1.1 lukem break;
269 1.1 lukem }
270 1.1 lukem if (hash && !progress) {
271 1.1 lukem while (bytes >= hashbytes) {
272 1.1 lukem (void) putchar('#');
273 1.1 lukem hashbytes += mark;
274 1.1 lukem }
275 1.1 lukem (void) fflush(stdout);
276 1.1 lukem }
277 1.1 lukem progressmeter(0);
278 1.1 lukem }
279 1.1 lukem if (hash && !progress && bytes > 0) {
280 1.1 lukem if (bytes < mark)
281 1.1 lukem (void) putchar('#');
282 1.1 lukem (void) putchar('\n');
283 1.1 lukem (void) fflush(stdout);
284 1.1 lukem }
285 1.1 lukem if (len != 0) {
286 1.1 lukem warn("Reading from socket");
287 1.1 lukem goto cleanup_http_get;
288 1.1 lukem }
289 1.1 lukem if (bytes > 0)
290 1.1 lukem progressmeter(1);
291 1.1 lukem printf("Successfully retrieved file.\n");
292 1.1 lukem
293 1.1 lukem close(s);
294 1.1 lukem close(out);
295 1.1 lukem if (proxy)
296 1.1 lukem free(host);
297 1.1 lukem return(0);
298 1.1 lukem
299 1.1 lukem improper:
300 1.1 lukem warnx("improper response from %s", host);
301 1.1 lukem cleanup_http_get:
302 1.1 lukem if (s != -1)
303 1.1 lukem close(s);
304 1.1 lukem if (proxy)
305 1.1 lukem free(proxy);
306 1.1 lukem return(-1);
307 1.1 lukem }
308 1.1 lukem
309 1.1 lukem /*
310 1.1 lukem * Retrieve multiple files from the command line, transferring
311 1.1 lukem * files of the form "host:path", "ftp://host/path" using the
312 1.1 lukem * ftp protocol, and files of the form "http://host/path" using
313 1.1 lukem * the http protocol.
314 1.1 lukem * If path has a trailing "/", then return(-1);
315 1.1 lukem * the path will be cd-ed into and the connection remains open,
316 1.1 lukem * and the function will return -1 (to indicate the connection
317 1.1 lukem * is alive).
318 1.1 lukem * If an error occurs the return value will be the offset+1 in
319 1.1 lukem * argv[] of the file that caused a problem (i.e, argv[x]
320 1.1 lukem * returns x+1)
321 1.1 lukem * Otherwise, 0 is returned if all files retrieved successfully.
322 1.1 lukem */
323 1.1 lukem int
324 1.1 lukem auto_fetch(argc, argv)
325 1.1 lukem int argc;
326 1.1 lukem char *argv[];
327 1.1 lukem {
328 1.1 lukem static char lasthost[MAXHOSTNAMELEN];
329 1.1 lukem char *xargv[5];
330 1.1 lukem char *cp, *line, *host, *dir, *file, *portnum;
331 1.1 lukem int rval, xargc, argpos;
332 1.1 lukem
333 1.1 lukem rval = 0;
334 1.1 lukem
335 1.1 lukem if (setjmp(toplevel))
336 1.1 lukem exit(0);
337 1.1 lukem (void) signal(SIGINT, intr);
338 1.1 lukem (void) signal(SIGPIPE, lostpeer);
339 1.1 lukem
340 1.1 lukem /*
341 1.1 lukem * Loop through as long as there's files to fetch.
342 1.1 lukem */
343 1.1 lukem for (argpos = 0 ; rval == 0 && argpos < argc ; free(line), argpos++) {
344 1.1 lukem if (strchr(argv[argpos], ':') == NULL)
345 1.1 lukem break;
346 1.1 lukem host = dir = file = portnum = NULL;
347 1.1 lukem
348 1.1 lukem /*
349 1.1 lukem * We muck with the string, so we make a copy.
350 1.1 lukem */
351 1.1 lukem line = strdup(argv[argpos]);
352 1.1 lukem if (line == NULL)
353 1.1 lukem errx(1, "Can't allocate memory for auto-fetch.");
354 1.1 lukem
355 1.1 lukem /*
356 1.1 lukem * Try HTTP URL-style arguments first.
357 1.1 lukem */
358 1.1 lukem if (strncmp(line, HTTP_URL, sizeof(HTTP_URL) - 1) == 0) {
359 1.1 lukem if (http_get(line) == -1)
360 1.1 lukem rval = argpos + 1;
361 1.1 lukem continue;
362 1.1 lukem }
363 1.1 lukem
364 1.1 lukem /*
365 1.1 lukem * Try FTP URL-style arguments next, then host:file.
366 1.1 lukem */
367 1.1 lukem host = line;
368 1.1 lukem if (strncmp(line, FTP_URL, sizeof(FTP_URL) - 1) == 0) {
369 1.1 lukem host += sizeof(FTP_URL) - 1;
370 1.1 lukem cp = strchr(host, '/');
371 1.1 lukem
372 1.1 lukem /* Look for a port number after the host name. */
373 1.1 lukem portnum = strchr(host, ':');
374 1.1 lukem if (portnum != NULL)
375 1.1 lukem *portnum++ = '\0';
376 1.1 lukem } else /* classic style `host:file' */
377 1.1 lukem cp = strchr(host, ':');
378 1.1 lukem if (EMPTYSTRING(host)) {
379 1.1 lukem rval = argpos + 1;
380 1.1 lukem continue;
381 1.1 lukem }
382 1.1 lukem
383 1.1 lukem /*
384 1.1 lukem * If cp is NULL, the file wasn't specified
385 1.1 lukem * (URL looked something like ftp://host)
386 1.1 lukem */
387 1.1 lukem if (cp != NULL)
388 1.1 lukem *cp++ = '\0';
389 1.1 lukem
390 1.1 lukem /*
391 1.1 lukem * Extract the file and (if present) directory name.
392 1.1 lukem */
393 1.1 lukem dir = cp;
394 1.1 lukem if (! EMPTYSTRING(dir)) {
395 1.1 lukem cp = strrchr(cp, '/');
396 1.1 lukem if (cp != NULL) {
397 1.1 lukem *cp++ = '\0';
398 1.1 lukem file = cp;
399 1.1 lukem } else {
400 1.1 lukem file = dir;
401 1.1 lukem dir = NULL;
402 1.1 lukem }
403 1.1 lukem }
404 1.1 lukem if (debug)
405 1.1 lukem printf("host '%s', dir '%s', file '%s'\n",
406 1.1 lukem host, dir, file);
407 1.1 lukem
408 1.1 lukem /*
409 1.1 lukem * Set up the connection if we don't have one.
410 1.1 lukem */
411 1.1 lukem if (strcmp(host, lasthost) != 0) {
412 1.1 lukem strcpy(lasthost, host);
413 1.1 lukem if (connected)
414 1.1 lukem disconnect(0, NULL);
415 1.1 lukem xargv[0] = __progname;
416 1.1 lukem xargv[1] = host;
417 1.1 lukem xargv[2] = NULL;
418 1.1 lukem xargc = 2;
419 1.1 lukem if (portnum != NULL) {
420 1.1 lukem xargv[2] = portnum;
421 1.1 lukem xargv[3] = NULL;
422 1.1 lukem xargc = 3;
423 1.1 lukem }
424 1.1 lukem setpeer(xargc, xargv);
425 1.1 lukem if (connected == 0) {
426 1.1 lukem warnx("Can't connect to host `%s'.", host);
427 1.1 lukem rval = argpos + 1;
428 1.1 lukem continue;
429 1.1 lukem }
430 1.1 lukem
431 1.1 lukem /* Always use binary transfers. */
432 1.1 lukem setbinary(0, NULL);
433 1.1 lukem }
434 1.1 lukem else /* already have connection, cd back to '/' */
435 1.1 lukem {
436 1.1 lukem xargv[0] = "cd";
437 1.1 lukem xargv[1] = "/";
438 1.1 lukem xargv[2] = NULL;
439 1.1 lukem cd(2, xargv);
440 1.1 lukem if (! dirchange) {
441 1.1 lukem rval = argpos + 1;
442 1.1 lukem continue;
443 1.1 lukem }
444 1.1 lukem }
445 1.1 lukem
446 1.1 lukem /* Change directories, if necessary. */
447 1.1 lukem if (! EMPTYSTRING(dir)) {
448 1.1 lukem xargv[0] = "cd";
449 1.1 lukem xargv[1] = dir;
450 1.1 lukem xargv[2] = NULL;
451 1.1 lukem cd(2, xargv);
452 1.1 lukem if (! dirchange) {
453 1.1 lukem rval = argpos + 1;
454 1.1 lukem continue;
455 1.1 lukem }
456 1.1 lukem }
457 1.1 lukem
458 1.1 lukem if (EMPTYSTRING(file)) {
459 1.1 lukem rval = -1;
460 1.1 lukem continue;
461 1.1 lukem }
462 1.1 lukem
463 1.1 lukem /* Fetch the file. */
464 1.1 lukem xargv[0] = "get";
465 1.1 lukem xargv[1] = file;
466 1.1 lukem xargv[2] = NULL;
467 1.1 lukem get(2, xargv);
468 1.1 lukem }
469 1.1 lukem if (connected && rval != -1)
470 1.1 lukem disconnect(0, NULL);
471 1.1 lukem return (rval);
472 1.1 lukem }
473