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