fetch.c revision 1.23 1 1.23 thorpej /* $NetBSD: fetch.c,v 1.23 1998/07/10 04:39:04 thorpej 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.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.23 thorpej __RCSID("$NetBSD: fetch.c,v 1.23 1998/07/10 04:39:04 thorpej 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.1 lukem
68 1.1 lukem #include "ftp_var.h"
69 1.1 lukem
70 1.22 lukem static int url_get __P((const char *, const char *, const char *));
71 1.12 lukem void aborthttp __P((int));
72 1.12 lukem
73 1.12 lukem
74 1.1 lukem #define FTP_URL "ftp://" /* ftp URL prefix */
75 1.1 lukem #define HTTP_URL "http://" /* http URL prefix */
76 1.5 lukem #define FTP_PROXY "ftp_proxy" /* env var with ftp proxy location */
77 1.1 lukem #define HTTP_PROXY "http_proxy" /* env var with http proxy location */
78 1.1 lukem
79 1.1 lukem
80 1.1 lukem #define EMPTYSTRING(x) ((x) == NULL || (*(x) == '\0'))
81 1.1 lukem
82 1.2 lukem jmp_buf httpabort;
83 1.2 lukem
84 1.1 lukem /*
85 1.5 lukem * Retrieve URL, via the proxy in $proxyvar if necessary.
86 1.1 lukem * Modifies the string argument given.
87 1.1 lukem * Returns -1 on failure, 0 on success
88 1.1 lukem */
89 1.12 lukem static int
90 1.22 lukem url_get(origline, proxyenv, outfile)
91 1.9 lukem const char *origline;
92 1.9 lukem const char *proxyenv;
93 1.22 lukem const char *outfile;
94 1.1 lukem {
95 1.1 lukem struct sockaddr_in sin;
96 1.12 lukem int i, out, isftpurl;
97 1.18 lukem in_port_t port;
98 1.12 lukem volatile int s;
99 1.13 lukem size_t len;
100 1.12 lukem char c, *cp, *ep, *portnum, *path, buf[4096];
101 1.12 lukem const char *savefile;
102 1.9 lukem char *line, *proxy, *host;
103 1.22 lukem volatile sig_t oldintr, oldintp;
104 1.1 lukem off_t hashbytes;
105 1.22 lukem struct hostent *hp = NULL;
106 1.22 lukem int (*closefunc) __P((FILE *));
107 1.22 lukem FILE *fout;
108 1.1 lukem
109 1.22 lukem closefunc = NULL;
110 1.22 lukem fout = NULL;
111 1.1 lukem s = -1;
112 1.2 lukem proxy = NULL;
113 1.11 lukem isftpurl = 0;
114 1.1 lukem
115 1.22 lukem #ifdef __GNUC__ /* to shut up gcc warnings */
116 1.22 lukem (void)&closefunc;
117 1.22 lukem (void)&fout;
118 1.22 lukem (void)&proxy;
119 1.12 lukem (void)&savefile;
120 1.12 lukem #endif
121 1.12 lukem
122 1.9 lukem line = strdup(origline);
123 1.9 lukem if (line == NULL)
124 1.9 lukem errx(1, "Can't allocate memory to parse URL");
125 1.6 lukem if (strncasecmp(line, HTTP_URL, sizeof(HTTP_URL) - 1) == 0)
126 1.5 lukem host = line + sizeof(HTTP_URL) - 1;
127 1.11 lukem else if (strncasecmp(line, FTP_URL, sizeof(FTP_URL) - 1) == 0) {
128 1.5 lukem host = line + sizeof(FTP_URL) - 1;
129 1.11 lukem isftpurl = 1;
130 1.11 lukem } else
131 1.9 lukem errx(1, "url_get: Invalid URL '%s'", line);
132 1.5 lukem
133 1.1 lukem path = strchr(host, '/'); /* find path */
134 1.9 lukem if (EMPTYSTRING(path)) {
135 1.11 lukem if (isftpurl)
136 1.11 lukem goto noftpautologin;
137 1.11 lukem warnx("Invalid URL (no `/' after host): %s", origline);
138 1.5 lukem goto cleanup_url_get;
139 1.9 lukem }
140 1.1 lukem *path++ = '\0';
141 1.9 lukem if (EMPTYSTRING(path)) {
142 1.11 lukem if (isftpurl)
143 1.11 lukem goto noftpautologin;
144 1.11 lukem warnx("Invalid URL (no file after host): %s", origline);
145 1.5 lukem goto cleanup_url_get;
146 1.9 lukem }
147 1.1 lukem
148 1.22 lukem if (outfile)
149 1.22 lukem savefile = outfile;
150 1.22 lukem else {
151 1.22 lukem savefile = strrchr(path, '/'); /* find savefile */
152 1.22 lukem if (savefile != NULL)
153 1.22 lukem savefile++;
154 1.22 lukem else
155 1.22 lukem savefile = path;
156 1.22 lukem }
157 1.9 lukem if (EMPTYSTRING(savefile)) {
158 1.11 lukem if (isftpurl)
159 1.11 lukem goto noftpautologin;
160 1.11 lukem warnx("Invalid URL (no file after directory): %s", origline);
161 1.5 lukem goto cleanup_url_get;
162 1.9 lukem }
163 1.1 lukem
164 1.2 lukem if (proxyenv != NULL) { /* use proxy */
165 1.2 lukem proxy = strdup(proxyenv);
166 1.1 lukem if (proxy == NULL)
167 1.9 lukem errx(1, "Can't allocate memory for proxy URL.");
168 1.6 lukem if (strncasecmp(proxy, HTTP_URL, sizeof(HTTP_URL) - 1) == 0)
169 1.5 lukem host = proxy + sizeof(HTTP_URL) - 1;
170 1.6 lukem else if (strncasecmp(proxy, FTP_URL, sizeof(FTP_URL) - 1) == 0)
171 1.5 lukem host = proxy + sizeof(FTP_URL) - 1;
172 1.5 lukem else {
173 1.9 lukem warnx("Malformed proxy URL: %s", proxyenv);
174 1.5 lukem goto cleanup_url_get;
175 1.5 lukem }
176 1.9 lukem if (EMPTYSTRING(host)) {
177 1.9 lukem warnx("Malformed proxy URL: %s", proxyenv);
178 1.5 lukem goto cleanup_url_get;
179 1.9 lukem }
180 1.1 lukem *--path = '/'; /* add / back to real path */
181 1.1 lukem path = strchr(host, '/'); /* remove trailing / on host */
182 1.1 lukem if (! EMPTYSTRING(path))
183 1.1 lukem *path++ = '\0';
184 1.1 lukem path = line;
185 1.1 lukem }
186 1.1 lukem
187 1.1 lukem portnum = strchr(host, ':'); /* find portnum */
188 1.1 lukem if (portnum != NULL)
189 1.1 lukem *portnum++ = '\0';
190 1.1 lukem
191 1.1 lukem if (debug)
192 1.22 lukem fprintf(ttyout, "host %s, port %s, path %s, save as %s.\n",
193 1.1 lukem host, portnum, path, savefile);
194 1.1 lukem
195 1.1 lukem memset(&sin, 0, sizeof(sin));
196 1.1 lukem sin.sin_family = AF_INET;
197 1.1 lukem
198 1.20 christos if (isdigit((unsigned char)host[0])) {
199 1.1 lukem if (inet_aton(host, &sin.sin_addr) == 0) {
200 1.6 lukem warnx("Invalid IP address: %s", host);
201 1.5 lukem goto cleanup_url_get;
202 1.1 lukem }
203 1.1 lukem } else {
204 1.1 lukem hp = gethostbyname(host);
205 1.1 lukem if (hp == NULL) {
206 1.2 lukem warnx("%s: %s", host, hstrerror(h_errno));
207 1.5 lukem goto cleanup_url_get;
208 1.1 lukem }
209 1.1 lukem if (hp->h_addrtype != AF_INET) {
210 1.1 lukem warnx("%s: not an Internet address?", host);
211 1.5 lukem goto cleanup_url_get;
212 1.1 lukem }
213 1.1 lukem memcpy(&sin.sin_addr, hp->h_addr, hp->h_length);
214 1.1 lukem }
215 1.1 lukem
216 1.1 lukem if (! EMPTYSTRING(portnum)) {
217 1.12 lukem char *ep;
218 1.12 lukem long nport;
219 1.12 lukem
220 1.12 lukem nport = strtol(portnum, &ep, 10);
221 1.19 lukem if (nport < 1 || nport > MAX_IN_PORT_T || *ep != '\0') {
222 1.6 lukem warnx("Invalid port: %s", portnum);
223 1.5 lukem goto cleanup_url_get;
224 1.1 lukem }
225 1.18 lukem port = htons((in_port_t)nport);
226 1.1 lukem } else
227 1.1 lukem port = httpport;
228 1.1 lukem sin.sin_port = port;
229 1.1 lukem
230 1.1 lukem s = socket(AF_INET, SOCK_STREAM, 0);
231 1.1 lukem if (s == -1) {
232 1.9 lukem warn("Can't create socket");
233 1.5 lukem goto cleanup_url_get;
234 1.1 lukem }
235 1.1 lukem
236 1.23 thorpej while (xconnect(s, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
237 1.22 lukem if (errno == EINTR)
238 1.22 lukem continue;
239 1.22 lukem if (hp && hp->h_addr_list[1]) {
240 1.22 lukem int oerrno = errno;
241 1.22 lukem char *ia;
242 1.22 lukem
243 1.22 lukem ia = inet_ntoa(sin.sin_addr);
244 1.22 lukem errno = oerrno;
245 1.22 lukem warn("connect to address %s", ia);
246 1.22 lukem hp->h_addr_list++;
247 1.22 lukem memcpy(&sin.sin_addr, hp->h_addr_list[0],
248 1.22 lukem (size_t)hp->h_length);
249 1.22 lukem fprintf(ttyout, "Trying %s...\n",
250 1.22 lukem inet_ntoa(sin.sin_addr));
251 1.22 lukem (void)close(s);
252 1.22 lukem s = socket(AF_INET, SOCK_STREAM, 0);
253 1.22 lukem if (s < 0) {
254 1.22 lukem warn("socket");
255 1.22 lukem goto cleanup_url_get;
256 1.22 lukem }
257 1.22 lukem continue;
258 1.22 lukem }
259 1.1 lukem warn("Can't connect to %s", host);
260 1.5 lukem goto cleanup_url_get;
261 1.1 lukem }
262 1.1 lukem
263 1.1 lukem /*
264 1.1 lukem * Construct and send the request. We're expecting a return
265 1.1 lukem * status of "200". Proxy requests don't want leading /.
266 1.1 lukem */
267 1.21 tv if (!proxy) {
268 1.22 lukem fprintf(ttyout, "Requesting %s\n", origline);
269 1.21 tv len = snprintf(buf, sizeof(buf),
270 1.21 tv "GET /%s HTTP/1.1\r\nHost: %s\r\n\r\n", path, host);
271 1.21 tv } else {
272 1.22 lukem fprintf(ttyout, "Requesting %s (via %s)\n", origline, proxyenv);
273 1.21 tv len = snprintf(buf, sizeof(buf), "GET %s HTTP/1.0\r\n\r\n",
274 1.21 tv path);
275 1.21 tv }
276 1.13 lukem if (write(s, buf, len) < len) {
277 1.9 lukem warn("Writing HTTP request");
278 1.5 lukem goto cleanup_url_get;
279 1.1 lukem }
280 1.1 lukem memset(buf, 0, sizeof(buf));
281 1.13 lukem for (cp = buf; cp < buf + sizeof(buf); ) {
282 1.1 lukem if (read(s, cp, 1) != 1)
283 1.1 lukem goto improper;
284 1.3 lukem if (*cp == '\r')
285 1.3 lukem continue;
286 1.1 lukem if (*cp == '\n')
287 1.1 lukem break;
288 1.13 lukem cp++;
289 1.1 lukem }
290 1.13 lukem buf[sizeof(buf) - 1] = '\0'; /* sanity */
291 1.1 lukem cp = strchr(buf, ' ');
292 1.1 lukem if (cp == NULL)
293 1.1 lukem goto improper;
294 1.1 lukem else
295 1.1 lukem cp++;
296 1.1 lukem if (strncmp(cp, "200", 3)) {
297 1.1 lukem warnx("Error retrieving file: %s", cp);
298 1.5 lukem goto cleanup_url_get;
299 1.1 lukem }
300 1.1 lukem
301 1.22 lukem /* Read the rest of the header. */
302 1.1 lukem memset(buf, 0, sizeof(buf));
303 1.1 lukem c = '\0';
304 1.13 lukem for (cp = buf; cp < buf + sizeof(buf); ) {
305 1.1 lukem if (read(s, cp, 1) != 1)
306 1.1 lukem goto improper;
307 1.3 lukem if (*cp == '\r')
308 1.3 lukem continue;
309 1.1 lukem if (*cp == '\n' && c == '\n')
310 1.1 lukem break;
311 1.1 lukem c = *cp;
312 1.13 lukem cp++;
313 1.1 lukem }
314 1.13 lukem buf[sizeof(buf) - 1] = '\0'; /* sanity */
315 1.1 lukem
316 1.22 lukem /* Look for the "Content-length: " header. */
317 1.1 lukem #define CONTENTLEN "Content-Length: "
318 1.1 lukem for (cp = buf; *cp != '\0'; cp++) {
319 1.1 lukem if (tolower(*cp) == 'c' &&
320 1.1 lukem strncasecmp(cp, CONTENTLEN, sizeof(CONTENTLEN) - 1) == 0)
321 1.1 lukem break;
322 1.1 lukem }
323 1.13 lukem if (*cp != '\0') {
324 1.13 lukem cp += sizeof(CONTENTLEN) - 1;
325 1.13 lukem ep = strchr(cp, '\n');
326 1.13 lukem if (ep == NULL)
327 1.13 lukem goto improper;
328 1.13 lukem else
329 1.13 lukem *ep = '\0';
330 1.13 lukem filesize = strtol(cp, &ep, 10);
331 1.13 lukem if (filesize < 1 || *ep != '\0')
332 1.13 lukem goto improper;
333 1.13 lukem } else
334 1.13 lukem filesize = -1;
335 1.1 lukem
336 1.22 lukem oldintr = oldintp = NULL;
337 1.22 lukem
338 1.22 lukem /* Open the output file. */
339 1.22 lukem if (strcmp(savefile, "-") == 0) {
340 1.22 lukem fout = stdout;
341 1.22 lukem } else if (*savefile == '|') {
342 1.22 lukem oldintp = signal(SIGPIPE, SIG_IGN);
343 1.22 lukem fout = popen(savefile + 1, "w");
344 1.22 lukem if (fout == NULL) {
345 1.22 lukem warn("Can't run %s", savefile + 1);
346 1.22 lukem goto cleanup_url_get;
347 1.22 lukem }
348 1.22 lukem closefunc = pclose;
349 1.22 lukem } else {
350 1.22 lukem fout = fopen(savefile, "w");
351 1.22 lukem if (fout == NULL) {
352 1.22 lukem warn("Can't open %s", savefile);
353 1.22 lukem goto cleanup_url_get;
354 1.22 lukem }
355 1.22 lukem closefunc = fclose;
356 1.1 lukem }
357 1.1 lukem
358 1.22 lukem /* Trap signals */
359 1.2 lukem if (setjmp(httpabort)) {
360 1.2 lukem if (oldintr)
361 1.3 lukem (void)signal(SIGINT, oldintr);
362 1.22 lukem if (oldintp)
363 1.22 lukem (void)signal(SIGPIPE, oldintp);
364 1.5 lukem goto cleanup_url_get;
365 1.2 lukem }
366 1.2 lukem oldintr = signal(SIGINT, aborthttp);
367 1.2 lukem
368 1.1 lukem bytes = 0;
369 1.1 lukem hashbytes = mark;
370 1.1 lukem progressmeter(-1);
371 1.2 lukem
372 1.2 lukem /* Finally, suck down the file. */
373 1.1 lukem i = 0;
374 1.22 lukem out = fileno(fout);
375 1.1 lukem while ((len = read(s, buf, sizeof(buf))) > 0) {
376 1.1 lukem bytes += len;
377 1.1 lukem for (cp = buf; len > 0; len -= i, cp += i) {
378 1.1 lukem if ((i = write(out, cp, len)) == -1) {
379 1.1 lukem warn("Writing %s", savefile);
380 1.5 lukem goto cleanup_url_get;
381 1.1 lukem }
382 1.1 lukem else if (i == 0)
383 1.1 lukem break;
384 1.1 lukem }
385 1.1 lukem if (hash && !progress) {
386 1.1 lukem while (bytes >= hashbytes) {
387 1.22 lukem (void)putc('#', ttyout);
388 1.1 lukem hashbytes += mark;
389 1.1 lukem }
390 1.22 lukem (void)fflush(ttyout);
391 1.1 lukem }
392 1.1 lukem }
393 1.1 lukem if (hash && !progress && bytes > 0) {
394 1.1 lukem if (bytes < mark)
395 1.22 lukem (void)putc('#', ttyout);
396 1.22 lukem (void)putc('\n', ttyout);
397 1.22 lukem (void)fflush(ttyout);
398 1.1 lukem }
399 1.1 lukem if (len != 0) {
400 1.1 lukem warn("Reading from socket");
401 1.5 lukem goto cleanup_url_get;
402 1.1 lukem }
403 1.2 lukem progressmeter(1);
404 1.2 lukem if (verbose)
405 1.22 lukem fputs("Successfully retrieved file.\n", ttyout);
406 1.3 lukem (void)signal(SIGINT, oldintr);
407 1.22 lukem if (oldintp)
408 1.22 lukem (void)signal(SIGPIPE, oldintp);
409 1.1 lukem
410 1.23 thorpej resetsockbufsize();
411 1.1 lukem close(s);
412 1.22 lukem if (closefunc != NULL)
413 1.22 lukem (*closefunc)(fout);
414 1.1 lukem if (proxy)
415 1.2 lukem free(proxy);
416 1.10 lukem free(line);
417 1.3 lukem return (0);
418 1.1 lukem
419 1.11 lukem noftpautologin:
420 1.11 lukem warnx(
421 1.11 lukem "Auto-login using ftp URLs isn't supported when using $ftp_proxy");
422 1.11 lukem goto cleanup_url_get;
423 1.11 lukem
424 1.1 lukem improper:
425 1.6 lukem warnx("Improper response from %s", host);
426 1.11 lukem
427 1.5 lukem cleanup_url_get:
428 1.23 thorpej resetsockbufsize();
429 1.1 lukem if (s != -1)
430 1.1 lukem close(s);
431 1.22 lukem if (closefunc != NULL && fout != NULL)
432 1.22 lukem (*closefunc)(fout);
433 1.1 lukem if (proxy)
434 1.1 lukem free(proxy);
435 1.10 lukem free(line);
436 1.3 lukem return (-1);
437 1.1 lukem }
438 1.1 lukem
439 1.1 lukem /*
440 1.2 lukem * Abort a http retrieval
441 1.2 lukem */
442 1.2 lukem void
443 1.3 lukem aborthttp(notused)
444 1.3 lukem int notused;
445 1.2 lukem {
446 1.2 lukem
447 1.2 lukem alarmtimer(0);
448 1.22 lukem fputs("\nhttp fetch aborted.\n", ttyout);
449 1.22 lukem (void)fflush(ttyout);
450 1.2 lukem longjmp(httpabort, 1);
451 1.2 lukem }
452 1.2 lukem
453 1.2 lukem /*
454 1.1 lukem * Retrieve multiple files from the command line, transferring
455 1.1 lukem * files of the form "host:path", "ftp://host/path" using the
456 1.1 lukem * ftp protocol, and files of the form "http://host/path" using
457 1.1 lukem * the http protocol.
458 1.3 lukem * If path has a trailing "/", then return (-1);
459 1.1 lukem * the path will be cd-ed into and the connection remains open,
460 1.1 lukem * and the function will return -1 (to indicate the connection
461 1.1 lukem * is alive).
462 1.1 lukem * If an error occurs the return value will be the offset+1 in
463 1.1 lukem * argv[] of the file that caused a problem (i.e, argv[x]
464 1.1 lukem * returns x+1)
465 1.1 lukem * Otherwise, 0 is returned if all files retrieved successfully.
466 1.1 lukem */
467 1.1 lukem int
468 1.22 lukem auto_fetch(argc, argv, outfile)
469 1.1 lukem int argc;
470 1.1 lukem char *argv[];
471 1.22 lukem char *outfile;
472 1.1 lukem {
473 1.1 lukem static char lasthost[MAXHOSTNAMELEN];
474 1.1 lukem char *xargv[5];
475 1.1 lukem char *cp, *line, *host, *dir, *file, *portnum;
476 1.6 lukem char *user, *pass;
477 1.5 lukem char *ftpproxy, *httpproxy;
478 1.12 lukem int rval, xargc;
479 1.12 lukem volatile int argpos;
480 1.3 lukem int dirhasglob, filehasglob;
481 1.3 lukem char rempath[MAXPATHLEN];
482 1.1 lukem
483 1.22 lukem #ifdef __GNUC__ /* to shut up gcc warnings */
484 1.22 lukem (void)&outfile;
485 1.22 lukem #endif
486 1.22 lukem
487 1.2 lukem argpos = 0;
488 1.1 lukem
489 1.2 lukem if (setjmp(toplevel)) {
490 1.2 lukem if (connected)
491 1.2 lukem disconnect(0, NULL);
492 1.3 lukem return (argpos + 1);
493 1.2 lukem }
494 1.3 lukem (void)signal(SIGINT, (sig_t)intr);
495 1.3 lukem (void)signal(SIGPIPE, (sig_t)lostpeer);
496 1.1 lukem
497 1.5 lukem ftpproxy = getenv(FTP_PROXY);
498 1.5 lukem httpproxy = getenv(HTTP_PROXY);
499 1.5 lukem
500 1.1 lukem /*
501 1.1 lukem * Loop through as long as there's files to fetch.
502 1.1 lukem */
503 1.2 lukem for (rval = 0; (rval == 0) && (argpos < argc); free(line), argpos++) {
504 1.1 lukem if (strchr(argv[argpos], ':') == NULL)
505 1.1 lukem break;
506 1.6 lukem host = dir = file = portnum = user = pass = NULL;
507 1.1 lukem
508 1.1 lukem /*
509 1.1 lukem * We muck with the string, so we make a copy.
510 1.1 lukem */
511 1.1 lukem line = strdup(argv[argpos]);
512 1.1 lukem if (line == NULL)
513 1.1 lukem errx(1, "Can't allocate memory for auto-fetch.");
514 1.1 lukem
515 1.1 lukem /*
516 1.1 lukem * Try HTTP URL-style arguments first.
517 1.1 lukem */
518 1.6 lukem if (strncasecmp(line, HTTP_URL, sizeof(HTTP_URL) - 1) == 0) {
519 1.22 lukem if (url_get(line, httpproxy, outfile) == -1)
520 1.1 lukem rval = argpos + 1;
521 1.1 lukem continue;
522 1.1 lukem }
523 1.1 lukem
524 1.1 lukem /*
525 1.5 lukem * Try FTP URL-style arguments next. If ftpproxy is
526 1.5 lukem * set, use url_get() instead of standard ftp.
527 1.5 lukem * Finally, try host:file.
528 1.1 lukem */
529 1.1 lukem host = line;
530 1.6 lukem if (strncasecmp(line, FTP_URL, sizeof(FTP_URL) - 1) == 0) {
531 1.5 lukem if (ftpproxy) {
532 1.22 lukem if (url_get(line, ftpproxy, outfile) == -1)
533 1.5 lukem rval = argpos + 1;
534 1.5 lukem continue;
535 1.5 lukem }
536 1.1 lukem host += sizeof(FTP_URL) - 1;
537 1.6 lukem dir = strchr(host, '/');
538 1.1 lukem
539 1.6 lukem /* look for [user:pass@]host[:port] */
540 1.7 lukem pass = strpbrk(host, ":@/");
541 1.7 lukem if (pass == NULL || *pass == '/') {
542 1.7 lukem pass = NULL;
543 1.6 lukem goto parsed_url;
544 1.7 lukem }
545 1.7 lukem if (pass == host || *pass == '@') {
546 1.8 lukem bad_ftp_url:
547 1.9 lukem warnx("Invalid URL: %s", argv[argpos]);
548 1.6 lukem rval = argpos + 1;
549 1.6 lukem continue;
550 1.6 lukem }
551 1.6 lukem *pass++ = '\0';
552 1.6 lukem cp = strpbrk(pass, ":@/");
553 1.6 lukem if (cp == NULL || *cp == '/') {
554 1.6 lukem portnum = pass;
555 1.7 lukem pass = NULL;
556 1.6 lukem goto parsed_url;
557 1.6 lukem }
558 1.7 lukem if (EMPTYSTRING(cp) || *cp == ':')
559 1.8 lukem goto bad_ftp_url;
560 1.6 lukem *cp++ = '\0';
561 1.7 lukem user = host;
562 1.7 lukem if (EMPTYSTRING(user))
563 1.8 lukem goto bad_ftp_url;
564 1.6 lukem host = cp;
565 1.1 lukem portnum = strchr(host, ':');
566 1.1 lukem if (portnum != NULL)
567 1.1 lukem *portnum++ = '\0';
568 1.6 lukem } else { /* classic style `host:file' */
569 1.6 lukem dir = strchr(host, ':');
570 1.6 lukem }
571 1.16 lukem parsed_url:
572 1.1 lukem if (EMPTYSTRING(host)) {
573 1.1 lukem rval = argpos + 1;
574 1.1 lukem continue;
575 1.1 lukem }
576 1.1 lukem
577 1.1 lukem /*
578 1.15 lukem * If dir is NULL, the file wasn't specified
579 1.1 lukem * (URL looked something like ftp://host)
580 1.1 lukem */
581 1.6 lukem if (dir != NULL)
582 1.6 lukem *dir++ = '\0';
583 1.1 lukem
584 1.1 lukem /*
585 1.1 lukem * Extract the file and (if present) directory name.
586 1.1 lukem */
587 1.1 lukem if (! EMPTYSTRING(dir)) {
588 1.6 lukem cp = strrchr(dir, '/');
589 1.1 lukem if (cp != NULL) {
590 1.1 lukem *cp++ = '\0';
591 1.1 lukem file = cp;
592 1.1 lukem } else {
593 1.1 lukem file = dir;
594 1.1 lukem dir = NULL;
595 1.1 lukem }
596 1.1 lukem }
597 1.1 lukem if (debug)
598 1.22 lukem fprintf(ttyout,
599 1.22 lukem "user %s:%s host %s port %s dir %s file %s\n",
600 1.6 lukem user, pass, host, portnum, dir, file);
601 1.1 lukem
602 1.1 lukem /*
603 1.1 lukem * Set up the connection if we don't have one.
604 1.1 lukem */
605 1.1 lukem if (strcmp(host, lasthost) != 0) {
606 1.6 lukem int oautologin;
607 1.6 lukem
608 1.4 lukem (void)strcpy(lasthost, host);
609 1.1 lukem if (connected)
610 1.1 lukem disconnect(0, NULL);
611 1.1 lukem xargv[0] = __progname;
612 1.1 lukem xargv[1] = host;
613 1.1 lukem xargv[2] = NULL;
614 1.1 lukem xargc = 2;
615 1.6 lukem if (! EMPTYSTRING(portnum)) {
616 1.1 lukem xargv[2] = portnum;
617 1.1 lukem xargv[3] = NULL;
618 1.1 lukem xargc = 3;
619 1.1 lukem }
620 1.6 lukem oautologin = autologin;
621 1.6 lukem if (user != NULL)
622 1.6 lukem autologin = 0;
623 1.1 lukem setpeer(xargc, xargv);
624 1.6 lukem autologin = oautologin;
625 1.6 lukem if ((connected == 0)
626 1.6 lukem || ((connected == 1) && !login(host, user, pass)) ) {
627 1.6 lukem warnx("Can't connect or login to host `%s'",
628 1.6 lukem host);
629 1.1 lukem rval = argpos + 1;
630 1.1 lukem continue;
631 1.1 lukem }
632 1.1 lukem
633 1.1 lukem /* Always use binary transfers. */
634 1.1 lukem setbinary(0, NULL);
635 1.1 lukem }
636 1.6 lukem /* cd back to '/' */
637 1.6 lukem xargv[0] = "cd";
638 1.6 lukem xargv[1] = "/";
639 1.6 lukem xargv[2] = NULL;
640 1.6 lukem cd(2, xargv);
641 1.6 lukem if (! dirchange) {
642 1.6 lukem rval = argpos + 1;
643 1.6 lukem continue;
644 1.1 lukem }
645 1.1 lukem
646 1.3 lukem dirhasglob = filehasglob = 0;
647 1.3 lukem if (doglob) {
648 1.3 lukem if (! EMPTYSTRING(dir) &&
649 1.3 lukem strpbrk(dir, "*?[]{}") != NULL)
650 1.3 lukem dirhasglob = 1;
651 1.3 lukem if (! EMPTYSTRING(file) &&
652 1.3 lukem strpbrk(file, "*?[]{}") != NULL)
653 1.3 lukem filehasglob = 1;
654 1.3 lukem }
655 1.3 lukem
656 1.1 lukem /* Change directories, if necessary. */
657 1.3 lukem if (! EMPTYSTRING(dir) && !dirhasglob) {
658 1.1 lukem xargv[0] = "cd";
659 1.1 lukem xargv[1] = dir;
660 1.1 lukem xargv[2] = NULL;
661 1.1 lukem cd(2, xargv);
662 1.1 lukem if (! dirchange) {
663 1.1 lukem rval = argpos + 1;
664 1.1 lukem continue;
665 1.1 lukem }
666 1.1 lukem }
667 1.1 lukem
668 1.1 lukem if (EMPTYSTRING(file)) {
669 1.1 lukem rval = -1;
670 1.1 lukem continue;
671 1.1 lukem }
672 1.1 lukem
673 1.2 lukem if (!verbose)
674 1.22 lukem fprintf(ttyout, "Retrieving %s/%s\n", dir ? dir : "",
675 1.22 lukem file);
676 1.2 lukem
677 1.3 lukem if (dirhasglob) {
678 1.3 lukem snprintf(rempath, sizeof(rempath), "%s/%s", dir, file);
679 1.3 lukem file = rempath;
680 1.3 lukem }
681 1.3 lukem
682 1.3 lukem /* Fetch the file(s). */
683 1.22 lukem xargc = 2;
684 1.1 lukem xargv[0] = "get";
685 1.1 lukem xargv[1] = file;
686 1.1 lukem xargv[2] = NULL;
687 1.3 lukem if (dirhasglob || filehasglob) {
688 1.3 lukem int ointeractive;
689 1.3 lukem
690 1.3 lukem ointeractive = interactive;
691 1.3 lukem interactive = 0;
692 1.3 lukem xargv[0] = "mget";
693 1.22 lukem mget(xargc, xargv);
694 1.4 lukem interactive = ointeractive;
695 1.22 lukem } else {
696 1.22 lukem if (outfile != NULL) {
697 1.22 lukem xargv[2] = outfile;
698 1.22 lukem xargv[3] = NULL;
699 1.22 lukem xargc++;
700 1.22 lukem }
701 1.22 lukem get(xargc, xargv);
702 1.22 lukem if (outfile != NULL && strcmp(outfile, "-") != 0
703 1.22 lukem && outfile[0] != '|')
704 1.22 lukem outfile = NULL;
705 1.22 lukem }
706 1.2 lukem
707 1.3 lukem if ((code / 100) != COMPLETE)
708 1.2 lukem rval = argpos + 1;
709 1.1 lukem }
710 1.1 lukem if (connected && rval != -1)
711 1.1 lukem disconnect(0, NULL);
712 1.1 lukem return (rval);
713 1.1 lukem }
714