xref: /freebsd/usr.bin/fetch/fetch.c (revision 7ef62cebc2f965b0f640263e179276928885e33d)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2000-2014 Dag-Erling Smørgrav
5  * Copyright (c) 2013 Michael Gmelin <freebsd@grem.de>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer
13  *    in this position and unchanged.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include <sys/param.h>
36 #include <sys/socket.h>
37 #include <sys/stat.h>
38 #include <sys/time.h>
39 
40 #include <ctype.h>
41 #include <err.h>
42 #include <errno.h>
43 #include <getopt.h>
44 #include <signal.h>
45 #include <stdint.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <termios.h>
50 #include <unistd.h>
51 
52 #include <fetch.h>
53 
54 #define MINBUFSIZE	16384
55 #define TIMEOUT		120
56 
57 /* Option flags */
58 static int	 A_flag;	/*    -A: do not follow 302 redirects */
59 static int	 a_flag;	/*    -a: auto retry */
60 static off_t	 B_size;	/*    -B: buffer size */
61 static int	 b_flag;	/*!   -b: workaround TCP bug */
62 static char    *c_dirname;	/*    -c: remote directory */
63 static int	 d_flag;	/*    -d: direct connection */
64 static int	 F_flag;	/*    -F: restart without checking mtime  */
65 static char	*f_filename;	/*    -f: file to fetch */
66 static char	*h_hostname;	/*    -h: host to fetch from */
67 static int	 i_flag;	/*    -i: specify file for mtime comparison */
68 static char	*i_filename;	/*        name of input file */
69 static int	 l_flag;	/*    -l: link rather than copy file: URLs */
70 static int	 m_flag;	/* -[Mm]: mirror mode */
71 static char	*N_filename;	/*    -N: netrc file name */
72 static int	 n_flag;	/*    -n: do not preserve modification time */
73 static int	 o_flag;	/*    -o: specify output file */
74 static int	 o_directory;	/*        output file is a directory */
75 static char	*o_filename;	/*        name of output file */
76 static int	 o_stdout;	/*        output file is stdout */
77 static int	 once_flag;	/*    -1: stop at first successful file */
78 static int	 p_flag;	/* -[Pp]: use passive FTP */
79 static int	 R_flag;	/*    -R: don't delete partial files */
80 static int	 r_flag;	/*    -r: restart previous transfer */
81 static off_t	 S_size;        /*    -S: require size to match */
82 static int	 s_flag;        /*    -s: show size, don't fetch */
83 static long	 T_secs;	/*    -T: transfer timeout in seconds */
84 static int	 t_flag;	/*!   -t: workaround TCP bug */
85 static int	 U_flag;	/*    -U: do not use high ports */
86 static int	 v_level = 1;	/*    -v: verbosity level */
87 static int	 v_tty;		/*        stdout is a tty */
88 static int	 v_progress;	/*        whether to display progress */
89 static pid_t	 pgrp;		/*        our process group */
90 static long	 w_secs;	/*    -w: retry delay */
91 static int	 family = PF_UNSPEC;	/* -[46]: address family to use */
92 
93 static int	 sigalrm;	/* SIGALRM received */
94 static int	 siginfo;	/* SIGINFO received */
95 static int	 sigint;	/* SIGINT received */
96 
97 static long	 ftp_timeout = TIMEOUT;	/* default timeout for FTP transfers */
98 static long	 http_timeout = TIMEOUT;/* default timeout for HTTP transfers */
99 static char	*buf;		/* transfer buffer */
100 
101 enum options
102 {
103 	OPTION_BIND_ADDRESS,
104 	OPTION_NO_FTP_PASSIVE_MODE,
105 	OPTION_HTTP_REFERER,
106 	OPTION_HTTP_USER_AGENT,
107 	OPTION_NO_PROXY,
108 	OPTION_SSL_CA_CERT_FILE,
109 	OPTION_SSL_CA_CERT_PATH,
110 	OPTION_SSL_CLIENT_CERT_FILE,
111 	OPTION_SSL_CLIENT_KEY_FILE,
112 	OPTION_SSL_CRL_FILE,
113 	OPTION_SSL_NO_SSL3,
114 	OPTION_SSL_NO_TLS1,
115 	OPTION_SSL_NO_VERIFY_HOSTNAME,
116 	OPTION_SSL_NO_VERIFY_PEER
117 };
118 
119 
120 static struct option longopts[] =
121 {
122 	/* mapping to single character argument */
123 	{ "one-file", no_argument, NULL, '1' },
124 	{ "ipv4-only", no_argument, NULL, '4' },
125 	{ "ipv6-only", no_argument, NULL, '6' },
126 	{ "no-redirect", no_argument, NULL, 'A' },
127 	{ "retry", no_argument, NULL, 'a' },
128 	{ "buffer-size", required_argument, NULL, 'B' },
129 	/* -c not mapped, since it's deprecated */
130 	{ "direct", no_argument, NULL, 'd' },
131 	{ "force-restart", no_argument, NULL, 'F' },
132 	/* -f not mapped, since it's deprecated */
133 	/* -h not mapped, since it's deprecated */
134 	{ "if-modified-since", required_argument, NULL, 'i' },
135 	{ "symlink", no_argument, NULL, 'l' },
136 	/* -M not mapped since it's the same as -m */
137 	{ "mirror", no_argument, NULL, 'm' },
138 	{ "netrc", required_argument, NULL, 'N' },
139 	{ "no-mtime", no_argument, NULL, 'n' },
140 	{ "output", required_argument, NULL, 'o' },
141 	/* -P not mapped since it's the same as -p */
142 	{ "passive", no_argument, NULL, 'p' },
143 	{ "quiet", no_argument, NULL, 'q' },
144 	{ "keep-output", no_argument, NULL, 'R' },
145 	{ "restart", no_argument, NULL, 'r' },
146 	{ "require-size", required_argument, NULL, 'S' },
147 	{ "print-size", no_argument, NULL, 's' },
148 	{ "timeout", required_argument, NULL, 'T' },
149 	{ "passive-portrange-default", no_argument, NULL, 'T' },
150 	{ "verbose", no_argument, NULL, 'v' },
151 	{ "retry-delay", required_argument, NULL, 'w' },
152 
153 	/* options without a single character equivalent */
154 	{ "bind-address", required_argument, NULL, OPTION_BIND_ADDRESS },
155 	{ "no-passive", no_argument, NULL, OPTION_NO_FTP_PASSIVE_MODE },
156 	{ "referer", required_argument, NULL, OPTION_HTTP_REFERER },
157 	{ "user-agent", required_argument, NULL, OPTION_HTTP_USER_AGENT },
158 	{ "no-proxy", required_argument, NULL, OPTION_NO_PROXY },
159 	{ "ca-cert", required_argument, NULL, OPTION_SSL_CA_CERT_FILE },
160 	{ "ca-path", required_argument, NULL, OPTION_SSL_CA_CERT_PATH },
161 	{ "cert", required_argument, NULL, OPTION_SSL_CLIENT_CERT_FILE },
162 	{ "key", required_argument, NULL, OPTION_SSL_CLIENT_KEY_FILE },
163 	{ "crl", required_argument, NULL, OPTION_SSL_CRL_FILE },
164 	{ "no-sslv3", no_argument, NULL, OPTION_SSL_NO_SSL3 },
165 	{ "no-tlsv1", no_argument, NULL, OPTION_SSL_NO_TLS1 },
166 	{ "no-verify-hostname", no_argument, NULL, OPTION_SSL_NO_VERIFY_HOSTNAME },
167 	{ "no-verify-peer", no_argument, NULL, OPTION_SSL_NO_VERIFY_PEER },
168 
169 	{ NULL, 0, NULL, 0 }
170 };
171 
172 /*
173  * Signal handler
174  */
175 static void
176 sig_handler(int sig)
177 {
178 	switch (sig) {
179 	case SIGALRM:
180 		sigalrm = 1;
181 		break;
182 	case SIGINFO:
183 		siginfo = 1;
184 		break;
185 	case SIGINT:
186 		sigint = 1;
187 		break;
188 	}
189 }
190 
191 struct xferstat {
192 	char		 name[64];
193 	struct timeval	 start;		/* start of transfer */
194 	struct timeval	 last;		/* time of last update */
195 	struct timeval	 last2;		/* time of previous last update */
196 	off_t		 size;		/* size of file per HTTP hdr */
197 	off_t		 offset;	/* starting offset in file */
198 	off_t		 rcvd;		/* bytes already received */
199 	off_t		 lastrcvd;	/* bytes received since last update */
200 };
201 
202 /*
203  * Format a number of seconds as either XXdYYh, XXhYYm, XXmYYs, or XXs
204  * depending on its magnitude
205  */
206 static void
207 stat_seconds(char *str, size_t strsz, long seconds)
208 {
209 
210 	if (seconds > 86400)
211 		snprintf(str, strsz, "%02ldd%02ldh",
212 		    seconds / 86400, (seconds % 86400) / 3600);
213 	else if (seconds > 3600)
214 		snprintf(str, strsz, "%02ldh%02ldm",
215 		    seconds / 3600, (seconds % 3600) / 60);
216 	else if (seconds > 60)
217 		snprintf(str, strsz, "%02ldm%02lds",
218 		    seconds / 60, seconds % 60);
219 	else
220 		snprintf(str, strsz, "   %02lds",
221 		    seconds);
222 }
223 
224 /*
225  * Compute and display ETA
226  */
227 static void
228 stat_eta(char *str, size_t strsz, const struct xferstat *xs)
229 {
230 	long elapsed, eta;
231 	off_t received, expected;
232 
233 	elapsed = xs->last.tv_sec - xs->start.tv_sec;
234 	received = xs->rcvd - xs->offset;
235 	expected = xs->size - xs->rcvd;
236 	eta = (long)((double)elapsed * expected / received);
237 	if (eta > 0)
238 		stat_seconds(str, strsz, eta);
239 	else
240 		stat_seconds(str, strsz, elapsed);
241 }
242 
243 /*
244  * Format a number as "xxxx YB" where Y is ' ', 'k', 'M'...
245  */
246 static const char *prefixes = " kMGTP";
247 static void
248 stat_bytes(char *str, size_t strsz, off_t bytes)
249 {
250 	const char *prefix = prefixes;
251 
252 	while (bytes > 9999 && prefix[1] != '\0') {
253 		bytes /= 1024;
254 		prefix++;
255 	}
256 	snprintf(str, strsz, "%4ju %cB", (uintmax_t)bytes, *prefix);
257 }
258 
259 /*
260  * Compute and display transfer rate
261  */
262 static void
263 stat_bps(char *str, size_t strsz, struct xferstat *xs)
264 {
265 	char bytes[16];
266 	double delta, bps;
267 
268 	delta = ((double)xs->last.tv_sec + (xs->last.tv_usec / 1.e6))
269 	    - ((double)xs->last2.tv_sec + (xs->last2.tv_usec / 1.e6));
270 
271 	if (delta == 0.0) {
272 		snprintf(str, strsz, "?? Bps");
273 	} else {
274 		bps = (xs->rcvd - xs->lastrcvd) / delta;
275 		stat_bytes(bytes, sizeof bytes, (off_t)bps);
276 		snprintf(str, strsz, "%sps", bytes);
277 	}
278 }
279 
280 /*
281  * Update the stats display
282  */
283 static void
284 stat_display(struct xferstat *xs, int force)
285 {
286 	char bytes[16], bps[16], eta[16];
287 	struct timeval now;
288 	int ctty_pgrp;
289 
290 	/* check if we're the foreground process */
291 	if (ioctl(STDERR_FILENO, TIOCGPGRP, &ctty_pgrp) != 0 ||
292 	    (pid_t)ctty_pgrp != pgrp)
293 		return;
294 
295 	gettimeofday(&now, NULL);
296 	if (!force && now.tv_sec <= xs->last.tv_sec)
297 		return;
298 	xs->last2 = xs->last;
299 	xs->last = now;
300 
301 	fprintf(stderr, "\r%-46.46s", xs->name);
302 	if (xs->rcvd >= xs->size) {
303 		stat_bytes(bytes, sizeof bytes, xs->rcvd);
304 		setproctitle("%s [%s]", xs->name, bytes);
305 		fprintf(stderr, "        %s", bytes);
306 	} else {
307 		stat_bytes(bytes, sizeof bytes, xs->size);
308 		setproctitle("%s [%d%% of %s]", xs->name,
309 		    (int)((100.0 * xs->rcvd) / xs->size),
310 		    bytes);
311 		fprintf(stderr, "%3d%% of %s",
312 		    (int)((100.0 * xs->rcvd) / xs->size),
313 		    bytes);
314 	}
315 	if (force == 2) {
316 		xs->lastrcvd = xs->offset;
317 		xs->last2 = xs->start;
318 	}
319 	stat_bps(bps, sizeof bps, xs);
320 	fprintf(stderr, " %s", bps);
321 	if ((xs->size > 0 && xs->rcvd > 0 &&
322 	     xs->last.tv_sec >= xs->start.tv_sec + 3) ||
323 	    force == 2) {
324 		stat_eta(eta, sizeof eta, xs);
325 		fprintf(stderr, " %s", eta);
326 	}
327 	xs->lastrcvd = xs->rcvd;
328 }
329 
330 /*
331  * Initialize the transfer statistics
332  */
333 static void
334 stat_start(struct xferstat *xs, const char *name, off_t size, off_t offset)
335 {
336 
337 	memset(xs, 0, sizeof *xs);
338 	snprintf(xs->name, sizeof xs->name, "%s", name);
339 	gettimeofday(&xs->start, NULL);
340 	xs->last2 = xs->last = xs->start;
341 	xs->size = size;
342 	xs->offset = offset;
343 	xs->rcvd = offset;
344 	xs->lastrcvd = offset;
345 	if (v_progress)
346 		stat_display(xs, 1);
347 	else if (v_level > 0)
348 		fprintf(stderr, "%-46s", xs->name);
349 }
350 
351 /*
352  * Update the transfer statistics
353  */
354 static void
355 stat_update(struct xferstat *xs, off_t rcvd)
356 {
357 
358 	xs->rcvd = rcvd;
359 	if (v_progress)
360 		stat_display(xs, 0);
361 }
362 
363 /*
364  * Finalize the transfer statistics
365  */
366 static void
367 stat_end(struct xferstat *xs)
368 {
369 	char bytes[16], bps[16], eta[16];
370 
371 	gettimeofday(&xs->last, NULL);
372 	if (v_progress) {
373 		stat_display(xs, 2);
374 		putc('\n', stderr);
375 	} else if (v_level > 0) {
376 		stat_bytes(bytes, sizeof bytes, xs->rcvd);
377 		stat_bps(bps, sizeof bps, xs);
378 		stat_eta(eta, sizeof eta, xs);
379 		fprintf(stderr, "        %s %s %s\n", bytes, bps, eta);
380 	}
381 }
382 
383 /*
384  * Ask the user for authentication details
385  */
386 static int
387 query_auth(struct url *URL)
388 {
389 	struct termios tios;
390 	tcflag_t saved_flags;
391 	int i, nopwd;
392 
393 	fprintf(stderr, "Authentication required for <%s://%s:%d/>!\n",
394 	    URL->scheme, URL->host, URL->port);
395 
396 	fprintf(stderr, "Login: ");
397 	if (fgets(URL->user, sizeof URL->user, stdin) == NULL)
398 		return (-1);
399 	for (i = strlen(URL->user); i >= 0; --i)
400 		if (URL->user[i] == '\r' || URL->user[i] == '\n')
401 			URL->user[i] = '\0';
402 
403 	fprintf(stderr, "Password: ");
404 	if (tcgetattr(STDIN_FILENO, &tios) == 0) {
405 		saved_flags = tios.c_lflag;
406 		tios.c_lflag &= ~ECHO;
407 		tios.c_lflag |= ECHONL|ICANON;
408 		tcsetattr(STDIN_FILENO, TCSAFLUSH|TCSASOFT, &tios);
409 		nopwd = (fgets(URL->pwd, sizeof URL->pwd, stdin) == NULL);
410 		tios.c_lflag = saved_flags;
411 		tcsetattr(STDIN_FILENO, TCSANOW|TCSASOFT, &tios);
412 	} else {
413 		nopwd = (fgets(URL->pwd, sizeof URL->pwd, stdin) == NULL);
414 	}
415 	if (nopwd)
416 		return (-1);
417 	for (i = strlen(URL->pwd); i >= 0; --i)
418 		if (URL->pwd[i] == '\r' || URL->pwd[i] == '\n')
419 			URL->pwd[i] = '\0';
420 
421 	return (0);
422 }
423 
424 /*
425  * Fetch a file
426  */
427 static int
428 fetch(char *URL, const char *path, int *is_http)
429 {
430 	struct url *url;
431 	struct url_stat us;
432 	struct stat sb, nsb;
433 	struct xferstat xs;
434 	FILE *f, *of;
435 	size_t size, readcnt, wr;
436 	off_t count, size_prev;
437 	char flags[8];
438 	const char *slash;
439 	char *tmppath;
440 	int r, tries;
441 	unsigned timeout;
442 	char *ptr;
443 
444 	f = of = NULL;
445 	tmppath = NULL;
446 
447 	timeout = 0;
448 	*flags = 0;
449 	count = 0;
450 
451 	/* set verbosity level */
452 	if (v_level > 1)
453 		strcat(flags, "v");
454 	if (v_level > 2)
455 		fetchDebug = 1;
456 
457 	/* parse URL */
458 	url = NULL;
459 	if (*URL == '\0') {
460 		warnx("empty URL");
461 		goto failure;
462 	}
463 	if ((url = fetchParseURL(URL)) == NULL) {
464 		warnx("%s: parse error", URL);
465 		goto failure;
466 	}
467 
468 	/* if no scheme was specified, take a guess */
469 	if (!*url->scheme) {
470 		if (!*url->host)
471 			strcpy(url->scheme, SCHEME_FILE);
472 		else if (strncasecmp(url->host, "ftp.", 4) == 0)
473 			strcpy(url->scheme, SCHEME_FTP);
474 		else if (strncasecmp(url->host, "www.", 4) == 0)
475 			strcpy(url->scheme, SCHEME_HTTP);
476 	}
477 
478 	/* for both of http and https */
479 	*is_http = strncmp(url->scheme, "http", 4) == 0;
480 
481 	/* common flags */
482 	switch (family) {
483 	case PF_INET:
484 		strcat(flags, "4");
485 		break;
486 	case PF_INET6:
487 		strcat(flags, "6");
488 		break;
489 	}
490 
491 	/* FTP specific flags */
492 	if (strcmp(url->scheme, SCHEME_FTP) == 0) {
493 		if (p_flag)
494 			strcat(flags, "p");
495 		if (d_flag)
496 			strcat(flags, "d");
497 		if (U_flag)
498 			strcat(flags, "l");
499 		timeout = T_secs ? T_secs : ftp_timeout;
500 	}
501 
502 	/* HTTP specific flags */
503 	if (strcmp(url->scheme, SCHEME_HTTP) == 0 ||
504 	    strcmp(url->scheme, SCHEME_HTTPS) == 0) {
505 		if (d_flag)
506 			strcat(flags, "d");
507 		if (A_flag)
508 			strcat(flags, "A");
509 		timeout = T_secs ? T_secs : http_timeout;
510 		if (i_flag) {
511 			if (stat(i_filename, &sb)) {
512 				warn("%s: stat()", i_filename);
513 				goto failure;
514 			}
515 			url->ims_time = sb.st_mtime;
516 			strcat(flags, "i");
517 		}
518 	}
519 
520 	/* set the protocol timeout. */
521 	fetchTimeout = timeout;
522 
523 	/* just print size */
524 	if (s_flag) {
525 		if (timeout)
526 			alarm(timeout);
527 		r = fetchStat(url, &us, flags);
528 		if (timeout)
529 			alarm(0);
530 		if (sigalrm || sigint)
531 			goto signal;
532 		if (r == -1) {
533 			warnx("%s", fetchLastErrString);
534 			goto failure;
535 		}
536 		if (us.size == -1)
537 			printf("Unknown\n");
538 		else
539 			printf("%jd\n", (intmax_t)us.size);
540 		goto success;
541 	}
542 
543 	tries = 1;
544 again:
545 	r = 0;
546 	/*
547 	 * If the -r flag was specified, we have to compare the local
548 	 * and remote files, so we should really do a fetchStat()
549 	 * first, but I know of at least one HTTP server that only
550 	 * sends the content size in response to GET requests, and
551 	 * leaves it out of replies to HEAD requests.  Also, in the
552 	 * (frequent) case that the local and remote files match but
553 	 * the local file is truncated, we have sufficient information
554 	 * before the compare to issue a correct request.  Therefore,
555 	 * we always issue a GET request as if we were sure the local
556 	 * file was a truncated copy of the remote file; we can drop
557 	 * the connection later if we change our minds.
558 	 */
559 	sb.st_size = -1;
560 	if (!o_stdout) {
561 		r = stat(path, &sb);
562 		if (r == 0 && (r_flag || tries > 1) && S_ISREG(sb.st_mode)) {
563 			url->offset = sb.st_size;
564 		} else if (r == -1 || !S_ISREG(sb.st_mode)) {
565 			/*
566 			 * Whatever value sb.st_size has now is either
567 			 * wrong (if stat(2) failed) or irrelevant (if the
568 			 * path does not refer to a regular file)
569 			 */
570 			sb.st_size = -1;
571 		}
572 		if (r == -1 && errno != ENOENT) {
573 			warnx("%s: stat()", path);
574 			goto failure;
575 		}
576 	}
577 	size_prev = sb.st_size;
578 
579 	/* start the transfer */
580 	if (timeout)
581 		alarm(timeout);
582 	f = fetchXGet(url, &us, flags);
583 	if (timeout)
584 		alarm(0);
585 	if (sigalrm || sigint)
586 		goto signal;
587 	if (f == NULL) {
588 		warnx("%s: %s", URL, fetchLastErrString);
589 		if (i_flag && (strcmp(url->scheme, SCHEME_HTTP) == 0 ||
590 		    strcmp(url->scheme, SCHEME_HTTPS) == 0) &&
591 		    fetchLastErrCode == FETCH_OK &&
592 		    strcmp(fetchLastErrString, "Not Modified") == 0) {
593 			/* HTTP Not Modified Response, return OK. */
594 			r = 0;
595 			goto done;
596 		} else
597 			goto failure;
598 	}
599 	if (sigint)
600 		goto signal;
601 
602 	/* check that size is as expected */
603 	if (S_size) {
604 		if (us.size == -1) {
605 			warnx("%s: size unknown", URL);
606 		} else if (us.size != S_size) {
607 			warnx("%s: size mismatch: expected %jd, actual %jd",
608 			    URL, (intmax_t)S_size, (intmax_t)us.size);
609 			goto failure;
610 		}
611 	}
612 
613 	/* symlink instead of copy */
614 	if (l_flag && strcmp(url->scheme, "file") == 0 && !o_stdout) {
615 		if (symlink(url->doc, path) == -1) {
616 			warn("%s: symlink()", path);
617 			goto failure;
618 		}
619 		goto success;
620 	}
621 
622 	if (us.size == -1 && !o_stdout && v_level > 0)
623 		warnx("%s: size of remote file is not known", URL);
624 	if (v_level > 1) {
625 		if (sb.st_size != -1)
626 			fprintf(stderr, "local size / mtime: %jd / %ld\n",
627 			    (intmax_t)sb.st_size, (long)sb.st_mtime);
628 		if (us.size != -1)
629 			fprintf(stderr, "remote size / mtime: %jd / %ld\n",
630 			    (intmax_t)us.size, (long)us.mtime);
631 	}
632 
633 	/* open output file */
634 	if (o_stdout) {
635 		/* output to stdout */
636 		of = stdout;
637 	} else if (r_flag && sb.st_size != -1) {
638 		/* resume mode, local file exists */
639 		if (!F_flag && us.mtime && sb.st_mtime != us.mtime && tries == 1) {
640 			/* no match! have to refetch */
641 			fclose(f);
642 			/* if precious, warn the user and give up */
643 			if (R_flag) {
644 				warnx("%s: local modification time "
645 				    "does not match remote", path);
646 				goto failure_keep;
647 			}
648 		} else if (url->offset > sb.st_size) {
649 			/* gap between what we asked for and what we got */
650 			warnx("%s: gap in resume mode", URL);
651 			fclose(of);
652 			of = NULL;
653 			/* picked up again later */
654 		} else if (us.size != -1) {
655 			if (us.size == sb.st_size)
656 				/* nothing to do */
657 				goto success;
658 			if (sb.st_size > us.size) {
659 				/* local file too long! */
660 				warnx("%s: local file (%jd bytes) is longer "
661 				    "than remote file (%jd bytes)", path,
662 				    (intmax_t)sb.st_size, (intmax_t)us.size);
663 				goto failure;
664 			}
665 			/* we got it, open local file */
666 			if ((of = fopen(path, "r+")) == NULL) {
667 				warn("%s: fopen()", path);
668 				goto failure;
669 			}
670 			/* check that it didn't move under our feet */
671 			if (fstat(fileno(of), &nsb) == -1) {
672 				/* can't happen! */
673 				warn("%s: fstat()", path);
674 				goto failure;
675 			}
676 			if (nsb.st_dev != sb.st_dev ||
677 			    nsb.st_ino != sb.st_ino ||
678 			    nsb.st_size != sb.st_size) {
679 				warnx("%s: file has changed", URL);
680 				fclose(of);
681 				of = NULL;
682 				sb = nsb;
683 				/* picked up again later */
684 			}
685 		}
686 		/* seek to where we left off */
687 		if (of != NULL && fseeko(of, url->offset, SEEK_SET) != 0) {
688 			warn("%s: fseeko()", path);
689 			fclose(of);
690 			of = NULL;
691 			/* picked up again later */
692 		}
693 	} else if (m_flag && sb.st_size != -1) {
694 		/* mirror mode, local file exists */
695 		if (sb.st_size == us.size && sb.st_mtime == us.mtime)
696 			goto success;
697 	}
698 
699 	if (of == NULL) {
700 		/*
701 		 * We don't yet have an output file; either this is a
702 		 * vanilla run with no special flags, or the local and
703 		 * remote files didn't match.
704 		 */
705 
706 		if (url->offset > 0) {
707 			/*
708 			 * We tried to restart a transfer, but for
709 			 * some reason gave up - so we have to restart
710 			 * from scratch if we want the whole file
711 			 */
712 			url->offset = 0;
713 			if ((f = fetchXGet(url, &us, flags)) == NULL) {
714 				warnx("%s: %s", URL, fetchLastErrString);
715 				goto failure;
716 			}
717 			if (sigint)
718 				goto signal;
719 		}
720 
721 		/* construct a temp file name */
722 		if (sb.st_size != -1 && S_ISREG(sb.st_mode)) {
723 			if ((slash = strrchr(path, '/')) == NULL)
724 				slash = path;
725 			else
726 				++slash;
727 			if(tmppath != NULL)
728 				free(tmppath);
729 			asprintf(&tmppath, "%.*s.fetch.XXXXXX.%s",
730 			    (int)(slash - path), path, slash);
731 			if (tmppath != NULL) {
732 				if (mkstemps(tmppath, strlen(slash) + 1) == -1) {
733 					warn("%s: mkstemps()", path);
734 					goto failure;
735 				}
736 				of = fopen(tmppath, "w");
737 				chown(tmppath, sb.st_uid, sb.st_gid);
738 				chmod(tmppath, sb.st_mode & ALLPERMS);
739 			}
740 		}
741 		if (of == NULL)
742 			of = fopen(path, "w");
743 		if (of == NULL) {
744 			warn("%s: open()", path);
745 			goto failure;
746 		}
747 	}
748 	count = url->offset;
749 
750 	/* start the counter */
751 	stat_start(&xs, path, us.size, count);
752 
753 	sigalrm = siginfo = sigint = 0;
754 
755 	/* suck in the data */
756 	setvbuf(f, NULL, _IOFBF, B_size);
757 	signal(SIGINFO, sig_handler);
758 	while (!sigint) {
759 		if (us.size != -1 && us.size - count < B_size &&
760 		    us.size - count >= 0)
761 			size = us.size - count;
762 		else
763 			size = B_size;
764 		if (siginfo) {
765 			stat_end(&xs);
766 			siginfo = 0;
767 		}
768 
769 		if (size == 0)
770 			break;
771 
772 		if ((readcnt = fread(buf, 1, size, f)) < size) {
773 			if (ferror(f) && errno == EINTR && !sigint)
774 				clearerr(f);
775 			else if (readcnt == 0)
776 				break;
777 		}
778 
779 		stat_update(&xs, count += readcnt);
780 		for (ptr = buf; readcnt > 0; ptr += wr, readcnt -= wr)
781 			if ((wr = fwrite(ptr, 1, readcnt, of)) < readcnt) {
782 				if (ferror(of) && errno == EINTR && !sigint)
783 					clearerr(of);
784 				else
785 					break;
786 			}
787 		if (readcnt != 0)
788 			break;
789 	}
790 	if (!sigalrm)
791 		sigalrm = ferror(f) && errno == ETIMEDOUT;
792 	signal(SIGINFO, SIG_DFL);
793 
794 	stat_end(&xs);
795 
796 	/*
797 	 * If the transfer timed out or was interrupted, we still want to
798 	 * set the mtime in case the file is not removed (-r or -R) and
799 	 * the user later restarts the transfer.
800 	 */
801  signal:
802 	/* set mtime of local file */
803 	if (!n_flag && us.mtime && !o_stdout && of != NULL &&
804 	    (stat(path, &sb) != -1) && sb.st_mode & S_IFREG) {
805 		struct timeval tv[2];
806 
807 		fflush(of);
808 		tv[0].tv_sec = (long)(us.atime ? us.atime : us.mtime);
809 		tv[1].tv_sec = (long)us.mtime;
810 		tv[0].tv_usec = tv[1].tv_usec = 0;
811 		if (utimes(tmppath ? tmppath : path, tv))
812 			warn("%s: utimes()", tmppath ? tmppath : path);
813 	}
814 
815 	/* timed out or interrupted? */
816 	if (sigalrm)
817 		warnx("transfer timed out");
818 	if (sigint) {
819 		warnx("transfer interrupted");
820 		goto failure;
821 	}
822 
823 	/* timeout / interrupt before connection completley established? */
824 	if (f == NULL)
825 		goto failure;
826 
827 	if (!sigalrm) {
828 		/* check the status of our files */
829 		if (ferror(f))
830 			warn("%s", URL);
831 		if (ferror(of))
832 			warn("%s", path);
833 		if (ferror(f) || ferror(of))
834 			goto failure;
835 	}
836 
837 	/* did the transfer complete normally? */
838 	if (us.size != -1 && count < us.size) {
839 		warnx("%s appears to be truncated: %jd/%jd bytes",
840 		    path, (intmax_t)count, (intmax_t)us.size);
841 		if(!o_stdout && a_flag && count > size_prev) {
842 			fclose(f);
843 			if (w_secs)
844 				sleep(w_secs);
845 			tries++;
846 			goto again;
847 		}
848 		goto failure_keep;
849 	}
850 
851 	/*
852 	 * If the transfer timed out and we didn't know how much to
853 	 * expect, assume the worst (i.e. we didn't get all of it)
854 	 */
855 	if (sigalrm && us.size == -1) {
856 		warnx("%s may be truncated", path);
857 		goto failure_keep;
858 	}
859 
860  success:
861 	r = 0;
862 	if (tmppath != NULL && rename(tmppath, path) == -1) {
863 		warn("%s: rename()", path);
864 		goto failure_keep;
865 	}
866 	goto done;
867  failure:
868 	if (of && of != stdout && !R_flag && !r_flag)
869 		if (stat(path, &sb) != -1 && (sb.st_mode & S_IFREG))
870 			unlink(tmppath ? tmppath : path);
871 	if (R_flag && tmppath != NULL && sb.st_size == -1)
872 		rename(tmppath, path); /* ignore errors here */
873  failure_keep:
874 	r = -1;
875 	goto done;
876  done:
877 	if (f)
878 		fclose(f);
879 	if (of && of != stdout)
880 		fclose(of);
881 	if (url)
882 		fetchFreeURL(url);
883 	if (tmppath != NULL)
884 		free(tmppath);
885 	return (r);
886 }
887 
888 static void
889 usage(void)
890 {
891 	fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
892 "usage: fetch [-146AadFlMmnPpqRrsUv] [-B bytes] [--bind-address=host]",
893 "       [--ca-cert=file] [--ca-path=dir] [--cert=file] [--crl=file]",
894 "       [-i file] [--key=file] [-N file] [--no-passive] [--no-proxy=list]",
895 "       [--no-sslv3] [--no-tlsv1] [--no-verify-hostname] [--no-verify-peer]",
896 "       [-o file] [--referer=URL] [-S bytes] [-T seconds]",
897 "       [--user-agent=agent-string] [-w seconds] URL ...",
898 "       fetch [-146AadFlMmnPpqRrsUv] [-B bytes] [--bind-address=host]",
899 "       [--ca-cert=file] [--ca-path=dir] [--cert=file] [--crl=file]",
900 "       [-i file] [--key=file] [-N file] [--no-passive] [--no-proxy=list]",
901 "       [--no-sslv3] [--no-tlsv1] [--no-verify-hostname] [--no-verify-peer]",
902 "       [-o file] [--referer=URL] [-S bytes] [-T seconds]",
903 "       [--user-agent=agent-string] [-w seconds] -h host -f file [-c dir]");
904 }
905 
906 
907 /*
908  * Entry point
909  */
910 int
911 main(int argc, char *argv[])
912 {
913 	struct stat sb;
914 	struct sigaction sa;
915 	const char *p, *s;
916 	char *end, *q;
917 	int c, e, is_http, r;
918 
919 
920 	while ((c = getopt_long(argc, argv,
921 	    "146AaB:bc:dFf:Hh:i:lMmN:nPpo:qRrS:sT:tUvw:",
922 	    longopts, NULL)) != -1)
923 		switch (c) {
924 		case '1':
925 			once_flag = 1;
926 			break;
927 		case '4':
928 			family = PF_INET;
929 			break;
930 		case '6':
931 			family = PF_INET6;
932 			break;
933 		case 'A':
934 			A_flag = 1;
935 			break;
936 		case 'a':
937 			a_flag = 1;
938 			break;
939 		case 'B':
940 			B_size = (off_t)strtol(optarg, &end, 10);
941 			if (*optarg == '\0' || *end != '\0')
942 				errx(1, "invalid buffer size (%s)", optarg);
943 			break;
944 		case 'b':
945 			warnx("warning: the -b option is deprecated");
946 			b_flag = 1;
947 			break;
948 		case 'c':
949 			c_dirname = optarg;
950 			break;
951 		case 'd':
952 			d_flag = 1;
953 			break;
954 		case 'F':
955 			F_flag = 1;
956 			break;
957 		case 'f':
958 			f_filename = optarg;
959 			break;
960 		case 'H':
961 			warnx("the -H option is now implicit, "
962 			    "use -U to disable");
963 			break;
964 		case 'h':
965 			h_hostname = optarg;
966 			break;
967 		case 'i':
968 			i_flag = 1;
969 			i_filename = optarg;
970 			break;
971 		case 'l':
972 			l_flag = 1;
973 			break;
974 		case 'o':
975 			o_flag = 1;
976 			o_filename = optarg;
977 			break;
978 		case 'M':
979 		case 'm':
980 			if (r_flag)
981 				errx(1, "the -m and -r flags "
982 				    "are mutually exclusive");
983 			m_flag = 1;
984 			break;
985 		case 'N':
986 			N_filename = optarg;
987 			break;
988 		case 'n':
989 			n_flag = 1;
990 			break;
991 		case 'P':
992 		case 'p':
993 			p_flag = 1;
994 			break;
995 		case 'q':
996 			v_level = 0;
997 			break;
998 		case 'R':
999 			R_flag = 1;
1000 			break;
1001 		case 'r':
1002 			if (m_flag)
1003 				errx(1, "the -m and -r flags "
1004 				    "are mutually exclusive");
1005 			r_flag = 1;
1006 			break;
1007 		case 'S':
1008 			S_size = strtoll(optarg, &end, 10);
1009 			if (*optarg == '\0' || *end != '\0')
1010 				errx(1, "invalid size (%s)", optarg);
1011 			break;
1012 		case 's':
1013 			s_flag = 1;
1014 			break;
1015 		case 'T':
1016 			T_secs = strtol(optarg, &end, 10);
1017 			if (*optarg == '\0' || *end != '\0')
1018 				errx(1, "invalid timeout (%s)", optarg);
1019 			break;
1020 		case 't':
1021 			t_flag = 1;
1022 			warnx("warning: the -t option is deprecated");
1023 			break;
1024 		case 'U':
1025 			U_flag = 1;
1026 			break;
1027 		case 'v':
1028 			v_level++;
1029 			break;
1030 		case 'w':
1031 			a_flag = 1;
1032 			w_secs = strtol(optarg, &end, 10);
1033 			if (*optarg == '\0' || *end != '\0')
1034 				errx(1, "invalid delay (%s)", optarg);
1035 			break;
1036 		case OPTION_BIND_ADDRESS:
1037 			setenv("FETCH_BIND_ADDRESS", optarg, 1);
1038 			break;
1039 		case OPTION_NO_FTP_PASSIVE_MODE:
1040 			setenv("FTP_PASSIVE_MODE", "no", 1);
1041 			break;
1042 		case OPTION_HTTP_REFERER:
1043 			setenv("HTTP_REFERER", optarg, 1);
1044 			break;
1045 		case OPTION_HTTP_USER_AGENT:
1046 			setenv("HTTP_USER_AGENT", optarg, 1);
1047 			break;
1048 		case OPTION_NO_PROXY:
1049 			setenv("NO_PROXY", optarg, 1);
1050 			break;
1051 		case OPTION_SSL_CA_CERT_FILE:
1052 			setenv("SSL_CA_CERT_FILE", optarg, 1);
1053 			break;
1054 		case OPTION_SSL_CA_CERT_PATH:
1055 			setenv("SSL_CA_CERT_PATH", optarg, 1);
1056 			break;
1057 		case OPTION_SSL_CLIENT_CERT_FILE:
1058 			setenv("SSL_CLIENT_CERT_FILE", optarg, 1);
1059 			break;
1060 		case OPTION_SSL_CLIENT_KEY_FILE:
1061 			setenv("SSL_CLIENT_KEY_FILE", optarg, 1);
1062 			break;
1063 		case OPTION_SSL_CRL_FILE:
1064 			setenv("SSL_CLIENT_CRL_FILE", optarg, 1);
1065 			break;
1066 		case OPTION_SSL_NO_SSL3:
1067 			setenv("SSL_NO_SSL3", "", 1);
1068 			break;
1069 		case OPTION_SSL_NO_TLS1:
1070 			setenv("SSL_NO_TLS1", "", 1);
1071 			break;
1072 		case OPTION_SSL_NO_VERIFY_HOSTNAME:
1073 			setenv("SSL_NO_VERIFY_HOSTNAME", "", 1);
1074 			break;
1075 		case OPTION_SSL_NO_VERIFY_PEER:
1076 			setenv("SSL_NO_VERIFY_PEER", "", 1);
1077 			break;
1078 		default:
1079 			usage();
1080 			exit(1);
1081 		}
1082 
1083 	argc -= optind;
1084 	argv += optind;
1085 
1086 	if (h_hostname || f_filename || c_dirname) {
1087 		if (!h_hostname || !f_filename || argc) {
1088 			usage();
1089 			exit(1);
1090 		}
1091 		/* XXX this is a hack. */
1092 		if (strcspn(h_hostname, "@:/") != strlen(h_hostname))
1093 			errx(1, "invalid hostname");
1094 		if (asprintf(argv, "ftp://%s/%s/%s", h_hostname,
1095 		    c_dirname ? c_dirname : "", f_filename) == -1)
1096 			errx(1, "%s", strerror(ENOMEM));
1097 		argc++;
1098 	}
1099 
1100 	if (!argc) {
1101 		usage();
1102 		exit(1);
1103 	}
1104 
1105 	/* allocate buffer */
1106 	if (B_size < MINBUFSIZE)
1107 		B_size = MINBUFSIZE;
1108 	if ((buf = malloc(B_size)) == NULL)
1109 		errx(1, "%s", strerror(ENOMEM));
1110 
1111 	/* timeouts */
1112 	if ((s = getenv("FTP_TIMEOUT")) != NULL) {
1113 		ftp_timeout = strtol(s, &end, 10);
1114 		if (*s == '\0' || *end != '\0' || ftp_timeout < 0) {
1115 			warnx("FTP_TIMEOUT (%s) is not a positive integer", s);
1116 			ftp_timeout = 0;
1117 		}
1118 	}
1119 	if ((s = getenv("HTTP_TIMEOUT")) != NULL) {
1120 		http_timeout = strtol(s, &end, 10);
1121 		if (*s == '\0' || *end != '\0' || http_timeout < 0) {
1122 			warnx("HTTP_TIMEOUT (%s) is not a positive integer", s);
1123 			http_timeout = 0;
1124 		}
1125 	}
1126 
1127 	/* signal handling */
1128 	sa.sa_flags = 0;
1129 	sa.sa_handler = sig_handler;
1130 	sigemptyset(&sa.sa_mask);
1131 	sigaction(SIGALRM, &sa, NULL);
1132 	sa.sa_flags = SA_RESETHAND;
1133 	sigaction(SIGINT, &sa, NULL);
1134 	fetchRestartCalls = 0;
1135 
1136 	/* output file */
1137 	if (o_flag) {
1138 		if (strcmp(o_filename, "-") == 0) {
1139 			o_stdout = 1;
1140 		} else if (stat(o_filename, &sb) == -1) {
1141 			if (errno == ENOENT) {
1142 				if (argc > 1)
1143 					errx(1, "%s is not a directory",
1144 					    o_filename);
1145 			} else {
1146 				err(1, "%s", o_filename);
1147 			}
1148 		} else {
1149 			if (sb.st_mode & S_IFDIR)
1150 				o_directory = 1;
1151 		}
1152 	}
1153 
1154 	/* check if output is to a tty (for progress report) */
1155 	v_tty = isatty(STDERR_FILENO);
1156 	v_progress = v_tty && v_level > 0;
1157 	if (v_progress)
1158 		pgrp = getpgrp();
1159 
1160 	r = 0;
1161 
1162 	/* authentication */
1163 	if (v_tty)
1164 		fetchAuthMethod = query_auth;
1165 	if (N_filename != NULL)
1166 		if (setenv("NETRC", N_filename, 1) == -1)
1167 			err(1, "setenv: cannot set NETRC=%s", N_filename);
1168 
1169 	while (argc) {
1170 		if ((p = strrchr(*argv, '/')) == NULL)
1171 			p = *argv;
1172 		else
1173 			p++;
1174 
1175 		if (!*p)
1176 			p = "fetch.out";
1177 
1178 		fetchLastErrCode = 0;
1179 
1180 		if (o_flag) {
1181 			if (o_stdout) {
1182 				e = fetch(*argv, "-", &is_http);
1183 			} else if (o_directory) {
1184 				asprintf(&q, "%s/%s", o_filename, p);
1185 				e = fetch(*argv, q, &is_http);
1186 				free(q);
1187 			} else {
1188 				e = fetch(*argv, o_filename, &is_http);
1189 			}
1190 		} else {
1191 			e = fetch(*argv, p, &is_http);
1192 		}
1193 
1194 		if (sigint)
1195 			kill(getpid(), SIGINT);
1196 
1197 		if (e == 0 && once_flag)
1198 			exit(0);
1199 
1200 		if (e) {
1201 			r = 1;
1202 			if ((fetchLastErrCode
1203 			    && fetchLastErrCode != FETCH_AUTH
1204 			    && fetchLastErrCode != FETCH_UNAVAIL
1205 			    && fetchLastErrCode != FETCH_MOVED
1206 			    && fetchLastErrCode != FETCH_URL
1207 			    && fetchLastErrCode != FETCH_RESOLV
1208 			    && fetchLastErrCode != FETCH_UNKNOWN
1209 			    && (!is_http || (
1210 			    	   fetchLastErrCode != FETCH_PROTO
1211 			    	&& fetchLastErrCode != FETCH_SERVER
1212 			    	&& fetchLastErrCode != FETCH_TEMP
1213 			    	&& fetchLastErrCode != FETCH_TIMEOUT
1214 			    )))) {
1215 				if (w_secs && v_level)
1216 					fprintf(stderr, "Waiting %ld seconds "
1217 					    "before retrying\n", w_secs);
1218 				if (w_secs)
1219 					sleep(w_secs);
1220 				if (a_flag)
1221 					continue;
1222 			}
1223 		}
1224 
1225 		argc--, argv++;
1226 	}
1227 
1228 	exit(r);
1229 }
1230