2 * Copyright (c) 2000-2014 Dag-Erling Smørgrav
3 * Copyright (c) 2013 Michael Gmelin <freebsd@grem.de>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer
11 * in this position and unchanged.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * $FreeBSD: head/usr.bin/fetch/fetch.c 261234 2014-01-28 14:32:04Z des $
32 #include <sys/param.h>
33 #include <sys/socket.h>
51 #define MINBUFSIZE 16384
55 static int A_flag; /* -A: do not follow 302 redirects */
56 static int a_flag; /* -a: auto retry */
57 static off_t B_size; /* -B: buffer size */
58 static int b_flag; /*! -b: workaround TCP bug */
59 static char *c_dirname; /* -c: remote directory */
60 static int d_flag; /* -d: direct connection */
61 static int F_flag; /* -F: restart without checking mtime */
62 static char *f_filename; /* -f: file to fetch */
63 static char *h_hostname; /* -h: host to fetch from */
64 static int i_flag; /* -i: specify file for mtime comparison */
65 static char *i_filename; /* name of input file */
66 static int l_flag; /* -l: link rather than copy file: URLs */
67 static int m_flag; /* -[Mm]: mirror mode */
68 static char *N_filename; /* -N: netrc file name */
69 static int n_flag; /* -n: do not preserve modification time */
70 static int o_flag; /* -o: specify output file */
71 static int o_directory; /* output file is a directory */
72 static char *o_filename; /* name of output file */
73 static int o_stdout; /* output file is stdout */
74 static int once_flag; /* -1: stop at first successful file */
75 static int p_flag; /* -[Pp]: use passive FTP */
76 static int R_flag; /* -R: don't delete partial files */
77 static int r_flag; /* -r: restart previous transfer */
78 static off_t S_size; /* -S: require size to match */
79 static int s_flag; /* -s: show size, don't fetch */
80 static long T_secs; /* -T: transfer timeout in seconds */
81 static int t_flag; /*! -t: workaround TCP bug */
82 static int U_flag; /* -U: do not use high ports */
83 static int v_level = 1; /* -v: verbosity level */
84 static int v_tty; /* stdout is a tty */
85 static pid_t pgrp; /* our process group */
86 static long w_secs; /* -w: retry delay */
87 static int family = PF_UNSPEC; /* -[46]: address family to use */
89 static int sigalrm; /* SIGALRM received */
90 static int siginfo; /* SIGINFO received */
91 static int sigint; /* SIGINT received */
93 static long ftp_timeout = TIMEOUT; /* default timeout for FTP transfers */
94 static long http_timeout = TIMEOUT;/* default timeout for HTTP transfers */
95 static char *buf; /* transfer buffer */
100 OPTION_NO_FTP_PASSIVE_MODE,
102 OPTION_HTTP_USER_AGENT,
104 OPTION_SSL_ALLOW_SSL2,
105 OPTION_SSL_CA_CERT_FILE,
106 OPTION_SSL_CA_CERT_PATH,
107 OPTION_SSL_CLIENT_CERT_FILE,
108 OPTION_SSL_CLIENT_KEY_FILE,
112 OPTION_SSL_NO_VERIFY_HOSTNAME,
113 OPTION_SSL_NO_VERIFY_PEER
117 static struct option longopts[] =
119 /* mapping to single character argument */
120 { "one-file", no_argument, NULL, '1' },
121 { "ipv4-only", no_argument, NULL, '4' },
122 { "ipv6-only", no_argument, NULL, '6' },
123 { "no-redirect", no_argument, NULL, 'A' },
124 { "retry", no_argument, NULL, 'a' },
125 { "buffer-size", required_argument, NULL, 'B' },
126 /* -c not mapped, since it's deprecated */
127 { "direct", no_argument, NULL, 'd' },
128 { "force-restart", no_argument, NULL, 'F' },
129 /* -f not mapped, since it's deprecated */
130 /* -h not mapped, since it's deprecated */
131 { "if-modified-since", required_argument, NULL, 'i' },
132 { "symlink", no_argument, NULL, 'l' },
133 /* -M not mapped since it's the same as -m */
134 { "mirror", no_argument, NULL, 'm' },
135 { "netrc", required_argument, NULL, 'N' },
136 { "no-mtime", no_argument, NULL, 'n' },
137 { "output", required_argument, NULL, 'o' },
138 /* -P not mapped since it's the same as -p */
139 { "passive", no_argument, NULL, 'p' },
140 { "quiet", no_argument, NULL, 'q' },
141 { "keep-output", no_argument, NULL, 'R' },
142 { "restart", no_argument, NULL, 'r' },
143 { "require-size", required_argument, NULL, 'S' },
144 { "print-size", no_argument, NULL, 's' },
145 { "timeout", required_argument, NULL, 'T' },
146 { "passive-portrange-default", no_argument, NULL, 'T' },
147 { "verbose", no_argument, NULL, 'v' },
148 { "retry-delay", required_argument, NULL, 'w' },
150 /* options without a single character equivalent */
151 { "bind-address", required_argument, NULL, OPTION_BIND_ADDRESS },
152 { "no-passive", no_argument, NULL, OPTION_NO_FTP_PASSIVE_MODE },
153 { "referer", required_argument, NULL, OPTION_HTTP_REFERER },
154 { "user-agent", required_argument, NULL, OPTION_HTTP_USER_AGENT },
155 { "no-proxy", required_argument, NULL, OPTION_NO_PROXY },
156 { "allow-sslv2", no_argument, NULL, OPTION_SSL_ALLOW_SSL2 },
157 { "ca-cert", required_argument, NULL, OPTION_SSL_CA_CERT_FILE },
158 { "ca-path", required_argument, NULL, OPTION_SSL_CA_CERT_PATH },
159 { "cert", required_argument, NULL, OPTION_SSL_CLIENT_CERT_FILE },
160 { "key", required_argument, NULL, OPTION_SSL_CLIENT_KEY_FILE },
161 { "crl", required_argument, NULL, OPTION_SSL_CRL_FILE },
162 { "no-sslv3", no_argument, NULL, OPTION_SSL_NO_SSL3 },
163 { "no-tlsv1", no_argument, NULL, OPTION_SSL_NO_TLS1 },
164 { "no-verify-hostname", no_argument, NULL, OPTION_SSL_NO_VERIFY_HOSTNAME },
165 { "no-verify-peer", no_argument, NULL, OPTION_SSL_NO_VERIFY_PEER },
191 struct timeval start; /* start of transfer */
192 struct timeval last; /* time of last update */
193 struct timeval last2; /* time of previous last update */
194 off_t size; /* size of file per HTTP hdr */
195 off_t offset; /* starting offset in file */
196 off_t rcvd; /* bytes already received */
197 off_t lastrcvd; /* bytes received since last update */
201 * Compute and display ETA
204 stat_eta(struct xferstat *xs)
208 off_t received, expected;
210 elapsed = xs->last.tv_sec - xs->start.tv_sec;
211 received = xs->rcvd - xs->offset;
212 expected = xs->size - xs->rcvd;
213 eta = (long)((double)elapsed * expected / received);
215 snprintf(str, sizeof str, "%02ldh%02ldm",
216 eta / 3600, (eta % 3600) / 60);
218 snprintf(str, sizeof str, "%02ldm%02lds",
221 snprintf(str, sizeof str, "%02ldm%02lds",
222 elapsed / 60, elapsed % 60);
227 * Format a number as "xxxx YB" where Y is ' ', 'k', 'M'...
229 static const char *prefixes = " kMGTP";
231 stat_bytes(off_t bytes)
234 const char *prefix = prefixes;
236 while (bytes > 9999 && prefix[1] != '\0') {
240 snprintf(str, sizeof str, "%4jd %cB", (intmax_t)bytes, *prefix);
245 * Compute and display transfer rate
248 stat_bps(struct xferstat *xs)
253 delta = (xs->last.tv_sec + (xs->last.tv_usec / 1.e6))
254 - (xs->last2.tv_sec + (xs->last2.tv_usec / 1.e6));
257 snprintf(str, sizeof str, "?? Bps");
259 bps = (xs->rcvd - xs->lastrcvd) / delta;
260 snprintf(str, sizeof str, "%sps", stat_bytes((off_t)bps));
266 * Update the stats display
269 stat_display(struct xferstat *xs, int force)
274 /* check if we're the foreground process */
275 if (ioctl(STDERR_FILENO, TIOCGPGRP, &ctty_pgrp) == -1 ||
276 (pid_t)ctty_pgrp != pgrp)
279 gettimeofday(&now, NULL);
280 if (!force && now.tv_sec <= xs->last.tv_sec)
282 xs->last2 = xs->last;
285 fprintf(stderr, "\r%-46.46s", xs->name);
287 setproctitle("%s [%s]", xs->name, stat_bytes(xs->rcvd));
288 fprintf(stderr, " %s", stat_bytes(xs->rcvd));
290 setproctitle("%s [%d%% of %s]", xs->name,
291 (int)((100.0 * xs->rcvd) / xs->size),
292 stat_bytes(xs->size));
293 fprintf(stderr, "%3d%% of %s",
294 (int)((100.0 * xs->rcvd) / xs->size),
295 stat_bytes(xs->size));
298 xs->lastrcvd = xs->offset;
299 xs->last2 = xs->start;
301 fprintf(stderr, " %s", stat_bps(xs));
302 if ((xs->size > 0 && xs->rcvd > 0 &&
303 xs->last.tv_sec >= xs->start.tv_sec + 3) ||
305 fprintf(stderr, " %s", stat_eta(xs));
306 xs->lastrcvd = xs->rcvd;
310 * Initialize the transfer statistics
313 stat_start(struct xferstat *xs, const char *name, off_t size, off_t offset)
315 snprintf(xs->name, sizeof xs->name, "%s", name);
316 gettimeofday(&xs->start, NULL);
317 xs->last.tv_sec = xs->last.tv_usec = 0;
321 xs->lastrcvd = offset;
322 if (v_tty && v_level > 0)
324 else if (v_level > 0)
325 fprintf(stderr, "%-46s", xs->name);
329 * Update the transfer statistics
332 stat_update(struct xferstat *xs, off_t rcvd)
335 if (v_tty && v_level > 0)
340 * Finalize the transfer statistics
343 stat_end(struct xferstat *xs)
345 gettimeofday(&xs->last, NULL);
346 if (v_tty && v_level > 0) {
349 } else if (v_level > 0) {
350 fprintf(stderr, " %s %s\n",
351 stat_bytes(xs->size), stat_bps(xs));
356 * Ask the user for authentication details
359 query_auth(struct url *URL)
362 tcflag_t saved_flags;
365 fprintf(stderr, "Authentication required for <%s://%s:%d/>!\n",
366 URL->scheme, URL->host, URL->port);
368 fprintf(stderr, "Login: ");
369 if (fgets(URL->user, sizeof URL->user, stdin) == NULL)
371 for (i = strlen(URL->user); i >= 0; --i)
372 if (URL->user[i] == '\r' || URL->user[i] == '\n')
375 fprintf(stderr, "Password: ");
376 if (tcgetattr(STDIN_FILENO, &tios) == 0) {
377 saved_flags = tios.c_lflag;
378 tios.c_lflag &= ~ECHO;
379 tios.c_lflag |= ECHONL|ICANON;
380 tcsetattr(STDIN_FILENO, TCSAFLUSH|TCSASOFT, &tios);
381 nopwd = (fgets(URL->pwd, sizeof URL->pwd, stdin) == NULL);
382 tios.c_lflag = saved_flags;
383 tcsetattr(STDIN_FILENO, TCSANOW|TCSASOFT, &tios);
385 nopwd = (fgets(URL->pwd, sizeof URL->pwd, stdin) == NULL);
389 for (i = strlen(URL->pwd); i >= 0; --i)
390 if (URL->pwd[i] == '\r' || URL->pwd[i] == '\n')
400 fetch(char *URL, const char *path)
407 size_t size, readcnt, wr;
423 /* set verbosity level */
435 if ((url = fetchParseURL(URL)) == NULL) {
436 warnx("%s: parse error", URL);
440 /* if no scheme was specified, take a guess */
443 strcpy(url->scheme, SCHEME_FILE);
444 else if (strncasecmp(url->host, "ftp.", 4) == 0)
445 strcpy(url->scheme, SCHEME_FTP);
446 else if (strncasecmp(url->host, "www.", 4) == 0)
447 strcpy(url->scheme, SCHEME_HTTP);
460 /* FTP specific flags */
461 if (strcmp(url->scheme, SCHEME_FTP) == 0) {
468 timeout = T_secs ? T_secs : ftp_timeout;
471 /* HTTP specific flags */
472 if (strcmp(url->scheme, SCHEME_HTTP) == 0 ||
473 strcmp(url->scheme, SCHEME_HTTPS) == 0) {
478 timeout = T_secs ? T_secs : http_timeout;
480 if (stat(i_filename, &sb)) {
481 warn("%s: stat()", i_filename);
484 url->ims_time = sb.st_mtime;
489 /* set the protocol timeout. */
490 fetchTimeout = timeout;
492 /* just print size */
496 r = fetchStat(url, &us, flags);
499 if (sigalrm || sigint)
502 warnx("%s", fetchLastErrString);
508 printf("%jd\n", (intmax_t)us.size);
513 * If the -r flag was specified, we have to compare the local
514 * and remote files, so we should really do a fetchStat()
515 * first, but I know of at least one HTTP server that only
516 * sends the content size in response to GET requests, and
517 * leaves it out of replies to HEAD requests. Also, in the
518 * (frequent) case that the local and remote files match but
519 * the local file is truncated, we have sufficient information
520 * before the compare to issue a correct request. Therefore,
521 * we always issue a GET request as if we were sure the local
522 * file was a truncated copy of the remote file; we can drop
523 * the connection later if we change our minds.
528 if (r == 0 && r_flag && S_ISREG(sb.st_mode)) {
529 url->offset = sb.st_size;
530 } else if (r == -1 || !S_ISREG(sb.st_mode)) {
532 * Whatever value sb.st_size has now is either
533 * wrong (if stat(2) failed) or irrelevant (if the
534 * path does not refer to a regular file)
538 if (r == -1 && errno != ENOENT) {
539 warnx("%s: stat()", path);
544 /* start the transfer */
547 f = fetchXGet(url, &us, flags);
550 if (sigalrm || sigint)
553 warnx("%s: %s", URL, fetchLastErrString);
554 if (i_flag && strcmp(url->scheme, SCHEME_HTTP) == 0
555 && fetchLastErrCode == FETCH_OK
556 && strcmp(fetchLastErrString, "Not Modified") == 0) {
557 /* HTTP Not Modified Response, return OK. */
566 /* check that size is as expected */
569 warnx("%s: size unknown", URL);
570 } else if (us.size != S_size) {
571 warnx("%s: size mismatch: expected %jd, actual %jd",
572 URL, (intmax_t)S_size, (intmax_t)us.size);
577 /* symlink instead of copy */
578 if (l_flag && strcmp(url->scheme, "file") == 0 && !o_stdout) {
579 if (symlink(url->doc, path) == -1) {
580 warn("%s: symlink()", path);
586 if (us.size == -1 && !o_stdout && v_level > 0)
587 warnx("%s: size of remote file is not known", URL);
589 if (sb.st_size != -1)
590 fprintf(stderr, "local size / mtime: %jd / %ld\n",
591 (intmax_t)sb.st_size, (long)sb.st_mtime);
593 fprintf(stderr, "remote size / mtime: %jd / %ld\n",
594 (intmax_t)us.size, (long)us.mtime);
597 /* open output file */
599 /* output to stdout */
601 } else if (r_flag && sb.st_size != -1) {
602 /* resume mode, local file exists */
603 if (!F_flag && us.mtime && sb.st_mtime != us.mtime) {
604 /* no match! have to refetch */
606 /* if precious, warn the user and give up */
608 warnx("%s: local modification time "
609 "does not match remote", path);
612 } else if (url->offset > sb.st_size) {
613 /* gap between what we asked for and what we got */
614 warnx("%s: gap in resume mode", URL);
617 /* picked up again later */
618 } else if (us.size != -1) {
619 if (us.size == sb.st_size)
622 if (sb.st_size > us.size) {
623 /* local file too long! */
624 warnx("%s: local file (%jd bytes) is longer "
625 "than remote file (%jd bytes)", path,
626 (intmax_t)sb.st_size, (intmax_t)us.size);
629 /* we got it, open local file */
630 if ((of = fopen(path, "r+")) == NULL) {
631 warn("%s: fopen()", path);
634 /* check that it didn't move under our feet */
635 if (fstat(fileno(of), &nsb) == -1) {
637 warn("%s: fstat()", path);
640 if (nsb.st_dev != sb.st_dev ||
641 nsb.st_ino != sb.st_ino ||
642 nsb.st_size != sb.st_size) {
643 warnx("%s: file has changed", URL);
647 /* picked up again later */
650 /* seek to where we left off */
651 if (of != NULL && fseeko(of, url->offset, SEEK_SET) != 0) {
652 warn("%s: fseeko()", path);
655 /* picked up again later */
657 } else if (m_flag && sb.st_size != -1) {
658 /* mirror mode, local file exists */
659 if (sb.st_size == us.size && sb.st_mtime == us.mtime)
665 * We don't yet have an output file; either this is a
666 * vanilla run with no special flags, or the local and
667 * remote files didn't match.
670 if (url->offset > 0) {
672 * We tried to restart a transfer, but for
673 * some reason gave up - so we have to restart
674 * from scratch if we want the whole file
677 if ((f = fetchXGet(url, &us, flags)) == NULL) {
678 warnx("%s: %s", URL, fetchLastErrString);
685 /* construct a temp file name */
686 if (sb.st_size != -1 && S_ISREG(sb.st_mode)) {
687 if ((slash = strrchr(path, '/')) == NULL)
691 asprintf(&tmppath, "%.*s.fetch.XXXXXX.%s",
692 (int)(slash - path), path, slash);
693 if (tmppath != NULL) {
694 if (mkstemps(tmppath, strlen(slash) + 1) == -1) {
695 warn("%s: mkstemps()", path);
698 of = fopen(tmppath, "w");
699 chown(tmppath, sb.st_uid, sb.st_gid);
700 chmod(tmppath, sb.st_mode & ALLPERMS);
704 of = fopen(path, "w");
706 warn("%s: open()", path);
712 /* start the counter */
713 stat_start(&xs, path, us.size, count);
715 sigalrm = siginfo = sigint = 0;
717 /* suck in the data */
718 setvbuf(f, NULL, _IOFBF, B_size);
719 signal(SIGINFO, sig_handler);
721 if (us.size != -1 && us.size - count < B_size &&
722 us.size - count >= 0)
723 size = us.size - count;
734 if ((readcnt = fread(buf, 1, size, f)) < size) {
735 if (ferror(f) && errno == EINTR && !sigint)
737 else if (readcnt == 0)
741 stat_update(&xs, count += readcnt);
742 for (ptr = buf; readcnt > 0; ptr += wr, readcnt -= wr)
743 if ((wr = fwrite(ptr, 1, readcnt, of)) < readcnt) {
744 if (ferror(of) && errno == EINTR && !sigint)
753 sigalrm = ferror(f) && errno == ETIMEDOUT;
754 signal(SIGINFO, SIG_DFL);
759 * If the transfer timed out or was interrupted, we still want to
760 * set the mtime in case the file is not removed (-r or -R) and
761 * the user later restarts the transfer.
764 /* set mtime of local file */
765 if (!n_flag && us.mtime && !o_stdout && of != NULL &&
766 (stat(path, &sb) != -1) && sb.st_mode & S_IFREG) {
767 struct timeval tv[2];
770 tv[0].tv_sec = (long)(us.atime ? us.atime : us.mtime);
771 tv[1].tv_sec = (long)us.mtime;
772 tv[0].tv_usec = tv[1].tv_usec = 0;
773 if (utimes(tmppath ? tmppath : path, tv))
774 warn("%s: utimes()", tmppath ? tmppath : path);
777 /* timed out or interrupted? */
779 warnx("transfer timed out");
781 warnx("transfer interrupted");
785 /* timeout / interrupt before connection completley established? */
790 /* check the status of our files */
795 if (ferror(f) || ferror(of))
799 /* did the transfer complete normally? */
800 if (us.size != -1 && count < us.size) {
801 warnx("%s appears to be truncated: %jd/%jd bytes",
802 path, (intmax_t)count, (intmax_t)us.size);
807 * If the transfer timed out and we didn't know how much to
808 * expect, assume the worst (i.e. we didn't get all of it)
810 if (sigalrm && us.size == -1) {
811 warnx("%s may be truncated", path);
817 if (tmppath != NULL && rename(tmppath, path) == -1) {
818 warn("%s: rename()", path);
823 if (of && of != stdout && !R_flag && !r_flag)
824 if (stat(path, &sb) != -1 && (sb.st_mode & S_IFREG))
825 unlink(tmppath ? tmppath : path);
826 if (R_flag && tmppath != NULL && sb.st_size == -1)
827 rename(tmppath, path); /* ignore errors here */
834 if (of && of != stdout)
846 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",
847 "usage: fetch [-146AadFlMmnPpqRrsUv] [--allow-sslv2] [-B bytes]",
848 " [--bind-address=host] [--ca-cert=file] [--ca-path=dir] [--cert=file]",
849 " [--crl=file] [-i file] [--key=file] [-N file] [--no-passive]",
850 " [--no-proxy=list] [--no-sslv3] [--no-tlsv1] [--no-verify-hostname]",
851 " [--no-verify-peer] [-o file] [--referer=URL] [-S bytes] [-T seconds]",
852 " [--user-agent=agent-string] [-w seconds] URL ...",
853 " fetch [-146AadFlMmnPpqRrsUv] [--allow-sslv2] [-B bytes]",
854 " [--bind-address=host] [--ca-cert=file] [--ca-path=dir] [--cert=file]",
855 " [--crl=file] [-i file] [--key=file] [-N file] [--no-passive]",
856 " [--no-proxy=list] [--no-sslv3] [--no-tlsv1] [--no-verify-hostname]",
857 " [--no-verify-peer] [-o file] [--referer=URL] [-S bytes] [-T seconds]",
858 " [--user-agent=agent-string] [-w seconds] -h host -f file [-c dir]");
866 main(int argc, char *argv[])
875 while ((c = getopt_long(argc, argv,
876 "146AaB:bc:dFf:Hh:i:lMmN:nPpo:qRrS:sT:tUvw:",
877 longopts, NULL)) != -1)
895 B_size = (off_t)strtol(optarg, &end, 10);
896 if (*optarg == '\0' || *end != '\0')
897 errx(1, "invalid buffer size (%s)", optarg);
900 warnx("warning: the -b option is deprecated");
916 warnx("the -H option is now implicit, "
917 "use -U to disable");
936 errx(1, "the -m and -r flags "
937 "are mutually exclusive");
958 errx(1, "the -m and -r flags "
959 "are mutually exclusive");
963 S_size = (off_t)strtol(optarg, &end, 10);
964 if (*optarg == '\0' || *end != '\0')
965 errx(1, "invalid size (%s)", optarg);
971 T_secs = strtol(optarg, &end, 10);
972 if (*optarg == '\0' || *end != '\0')
973 errx(1, "invalid timeout (%s)", optarg);
977 warnx("warning: the -t option is deprecated");
987 w_secs = strtol(optarg, &end, 10);
988 if (*optarg == '\0' || *end != '\0')
989 errx(1, "invalid delay (%s)", optarg);
991 case OPTION_BIND_ADDRESS:
992 setenv("FETCH_BIND_ADDRESS", optarg, 1);
994 case OPTION_NO_FTP_PASSIVE_MODE:
995 setenv("FTP_PASSIVE_MODE", "no", 1);
997 case OPTION_HTTP_REFERER:
998 setenv("HTTP_REFERER", optarg, 1);
1000 case OPTION_HTTP_USER_AGENT:
1001 setenv("HTTP_USER_AGENT", optarg, 1);
1003 case OPTION_NO_PROXY:
1004 setenv("NO_PROXY", optarg, 1);
1006 case OPTION_SSL_ALLOW_SSL2:
1007 setenv("SSL_ALLOW_SSL2", "", 1);
1009 case OPTION_SSL_CA_CERT_FILE:
1010 setenv("SSL_CA_CERT_FILE", optarg, 1);
1012 case OPTION_SSL_CA_CERT_PATH:
1013 setenv("SSL_CA_CERT_PATH", optarg, 1);
1015 case OPTION_SSL_CLIENT_CERT_FILE:
1016 setenv("SSL_CLIENT_CERT_FILE", optarg, 1);
1018 case OPTION_SSL_CLIENT_KEY_FILE:
1019 setenv("SSL_CLIENT_KEY_FILE", optarg, 1);
1021 case OPTION_SSL_CRL_FILE:
1022 setenv("SSL_CLIENT_CRL_FILE", optarg, 1);
1024 case OPTION_SSL_NO_SSL3:
1025 setenv("SSL_NO_SSL3", "", 1);
1027 case OPTION_SSL_NO_TLS1:
1028 setenv("SSL_NO_TLS1", "", 1);
1030 case OPTION_SSL_NO_VERIFY_HOSTNAME:
1031 setenv("SSL_NO_VERIFY_HOSTNAME", "", 1);
1033 case OPTION_SSL_NO_VERIFY_PEER:
1034 setenv("SSL_NO_VERIFY_PEER", "", 1);
1044 if (h_hostname || f_filename || c_dirname) {
1045 if (!h_hostname || !f_filename || argc) {
1049 /* XXX this is a hack. */
1050 if (strcspn(h_hostname, "@:/") != strlen(h_hostname))
1051 errx(1, "invalid hostname");
1052 if (asprintf(argv, "ftp://%s/%s/%s", h_hostname,
1053 c_dirname ? c_dirname : "", f_filename) == -1)
1054 errx(1, "%s", strerror(ENOMEM));
1063 /* allocate buffer */
1064 if (B_size < MINBUFSIZE)
1065 B_size = MINBUFSIZE;
1066 if ((buf = malloc(B_size)) == NULL)
1067 errx(1, "%s", strerror(ENOMEM));
1070 if ((s = getenv("FTP_TIMEOUT")) != NULL) {
1071 ftp_timeout = strtol(s, &end, 10);
1072 if (*s == '\0' || *end != '\0' || ftp_timeout < 0) {
1073 warnx("FTP_TIMEOUT (%s) is not a positive integer", s);
1077 if ((s = getenv("HTTP_TIMEOUT")) != NULL) {
1078 http_timeout = strtol(s, &end, 10);
1079 if (*s == '\0' || *end != '\0' || http_timeout < 0) {
1080 warnx("HTTP_TIMEOUT (%s) is not a positive integer", s);
1085 /* signal handling */
1087 sa.sa_handler = sig_handler;
1088 sigemptyset(&sa.sa_mask);
1089 sigaction(SIGALRM, &sa, NULL);
1090 sa.sa_flags = SA_RESETHAND;
1091 sigaction(SIGINT, &sa, NULL);
1092 fetchRestartCalls = 0;
1096 if (strcmp(o_filename, "-") == 0) {
1098 } else if (stat(o_filename, &sb) == -1) {
1099 if (errno == ENOENT) {
1101 errx(1, "%s is not a directory",
1104 err(1, "%s", o_filename);
1107 if (sb.st_mode & S_IFDIR)
1112 /* check if output is to a tty (for progress report) */
1113 v_tty = isatty(STDERR_FILENO);
1119 /* authentication */
1121 fetchAuthMethod = query_auth;
1122 if (N_filename != NULL) {
1123 if (setenv("NETRC", N_filename, 1) == -1)
1124 err(1, "setenv: cannot set NETRC=%s", N_filename);
1128 if ((p = strrchr(*argv, '/')) == NULL)
1136 fetchLastErrCode = 0;
1140 e = fetch(*argv, "-");
1141 } else if (o_directory) {
1142 asprintf(&q, "%s/%s", o_filename, p);
1143 e = fetch(*argv, q);
1146 e = fetch(*argv, o_filename);
1149 e = fetch(*argv, p);
1153 kill(getpid(), SIGINT);
1155 if (e == 0 && once_flag)
1160 if ((fetchLastErrCode
1161 && fetchLastErrCode != FETCH_UNAVAIL
1162 && fetchLastErrCode != FETCH_MOVED
1163 && fetchLastErrCode != FETCH_URL
1164 && fetchLastErrCode != FETCH_RESOLV
1165 && fetchLastErrCode != FETCH_UNKNOWN)) {
1166 if (w_secs && v_level)
1167 fprintf(stderr, "Waiting %ld seconds "
1168 "before retrying\n", w_secs);