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