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