Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 3 | * wget - retrieve a file using HTTP or FTP |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 4 | * |
Eric Andersen | 4e573f4 | 2000-11-14 23:29:24 +0000 | [diff] [blame] | 5 | * Chip Rosenthal Covad Communications <chip@laserlink.net> |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 6 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Bradley M. Kuhn | c97131c | 2010-08-08 02:51:20 +0200 | [diff] [blame] | 7 | * |
| 8 | * Copyright (C) 2010 Bradley M. Kuhn <bkuhn@ebb.org> |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 9 | * Kuhn's copyrights are licensed GPLv2-or-later. File as a whole remains GPLv2. |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 10 | */ |
Denys Vlasenko | 1c6c670 | 2015-10-07 01:39:40 +0200 | [diff] [blame] | 11 | //config:config WGET |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 12 | //config: bool "wget (38 kb)" |
Denys Vlasenko | 1c6c670 | 2015-10-07 01:39:40 +0200 | [diff] [blame] | 13 | //config: default y |
| 14 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 15 | //config: wget is a utility for non-interactive download of files from HTTP |
| 16 | //config: and FTP servers. |
Denys Vlasenko | 1c6c670 | 2015-10-07 01:39:40 +0200 | [diff] [blame] | 17 | //config: |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 18 | //config:config FEATURE_WGET_LONG_OPTIONS |
| 19 | //config: bool "Enable long options" |
| 20 | //config: default y |
| 21 | //config: depends on WGET && LONG_OPTS |
| 22 | //config: |
Denys Vlasenko | 1c6c670 | 2015-10-07 01:39:40 +0200 | [diff] [blame] | 23 | //config:config FEATURE_WGET_STATUSBAR |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 24 | //config: bool "Enable progress bar (+2k)" |
Denys Vlasenko | 1c6c670 | 2015-10-07 01:39:40 +0200 | [diff] [blame] | 25 | //config: default y |
| 26 | //config: depends on WGET |
Denys Vlasenko | 1c6c670 | 2015-10-07 01:39:40 +0200 | [diff] [blame] | 27 | //config: |
Sergey Ponomarev | b6e6c83 | 2021-01-17 20:35:08 +0200 | [diff] [blame] | 28 | //config:config FEATURE_WGET_FTP |
| 29 | //config: bool "Enable FTP protocol (+1k)" |
| 30 | //config: default y |
| 31 | //config: depends on WGET |
| 32 | //config: help |
| 33 | //config: To support FTPS, enable FEATURE_WGET_HTTPS as well. |
| 34 | //config: |
Denys Vlasenko | 1c6c670 | 2015-10-07 01:39:40 +0200 | [diff] [blame] | 35 | //config:config FEATURE_WGET_AUTHENTICATION |
| 36 | //config: bool "Enable HTTP authentication" |
| 37 | //config: default y |
| 38 | //config: depends on WGET |
| 39 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 40 | //config: Support authenticated HTTP transfers. |
Denys Vlasenko | 1c6c670 | 2015-10-07 01:39:40 +0200 | [diff] [blame] | 41 | //config: |
Denys Vlasenko | 1c6c670 | 2015-10-07 01:39:40 +0200 | [diff] [blame] | 42 | //config:config FEATURE_WGET_TIMEOUT |
| 43 | //config: bool "Enable timeout option -T SEC" |
| 44 | //config: default y |
| 45 | //config: depends on WGET |
| 46 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 47 | //config: Supports network read and connect timeouts for wget, |
| 48 | //config: so that wget will give up and timeout, through the -T |
| 49 | //config: command line option. |
Denys Vlasenko | 1c6c670 | 2015-10-07 01:39:40 +0200 | [diff] [blame] | 50 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 51 | //config: Currently only connect and network data read timeout are |
| 52 | //config: supported (i.e., timeout is not applied to the DNS query). When |
| 53 | //config: FEATURE_WGET_LONG_OPTIONS is also enabled, the --timeout option |
| 54 | //config: will work in addition to -T. |
Denys Vlasenko | 1c6c670 | 2015-10-07 01:39:40 +0200 | [diff] [blame] | 55 | //config: |
Denys Vlasenko | 9a647c3 | 2017-01-23 01:08:16 +0100 | [diff] [blame] | 56 | //config:config FEATURE_WGET_HTTPS |
| 57 | //config: bool "Support HTTPS using internal TLS code" |
| 58 | //config: default y |
| 59 | //config: depends on WGET |
| 60 | //config: select TLS |
| 61 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 62 | //config: wget will use internal TLS code to connect to https:// URLs. |
Sergey Ponomarev | b6e6c83 | 2021-01-17 20:35:08 +0200 | [diff] [blame] | 63 | //config: It also enables FTPS support, but it's not well tested yet. |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 64 | //config: Note: |
| 65 | //config: On NOMMU machines, ssl_helper applet should be available |
| 66 | //config: in the $PATH for this to work. Make sure to select that applet. |
Denys Vlasenko | 9a647c3 | 2017-01-23 01:08:16 +0100 | [diff] [blame] | 67 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 68 | //config: Note: currently, TLS code only makes TLS I/O work, it |
| 69 | //config: does *not* check that the peer is who it claims to be, etc. |
| 70 | //config: IOW: it uses peer-supplied public keys to establish encryption |
| 71 | //config: and signing keys, then encrypts and signs outgoing data and |
| 72 | //config: decrypts incoming data. |
| 73 | //config: It does not check signature hashes on the incoming data: |
| 74 | //config: this means that attackers manipulating TCP packets can |
| 75 | //config: send altered data and we unknowingly receive garbage. |
| 76 | //config: (This check might be relatively easy to add). |
| 77 | //config: It does not check public key's certificate: |
| 78 | //config: this means that the peer may be an attacker impersonating |
| 79 | //config: the server we think we are talking to. |
Denys Vlasenko | 67f6db6 | 2017-01-30 16:27:37 +0100 | [diff] [blame] | 80 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 81 | //config: If you think this is unacceptable, consider this. As more and more |
| 82 | //config: servers switch to HTTPS-only operation, without such "crippled" |
| 83 | //config: TLS code it is *impossible* to simply download a kernel source |
| 84 | //config: from kernel.org. Which can in real world translate into |
| 85 | //config: "my small automatic tooling to build cross-compilers from sources |
| 86 | //config: no longer works, I need to additionally keep a local copy |
| 87 | //config: of ~4 megabyte source tarball of a SSL library and ~2 megabyte |
| 88 | //config: source of wget, need to compile and built both before I can |
| 89 | //config: download anything. All this despite the fact that the build |
| 90 | //config: is done in a QEMU sandbox on a machine with absolutely nothing |
| 91 | //config: worth stealing, so I don't care if someone would go to a lot |
| 92 | //config: of trouble to intercept my HTTPS download to send me an altered |
| 93 | //config: kernel tarball". |
Denys Vlasenko | 67f6db6 | 2017-01-30 16:27:37 +0100 | [diff] [blame] | 94 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 95 | //config: If you still think this is unacceptable, send patches. |
Denys Vlasenko | 67f6db6 | 2017-01-30 16:27:37 +0100 | [diff] [blame] | 96 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 97 | //config: If you still think this is unacceptable, do not want to send |
| 98 | //config: patches, but do want to waste bandwidth expaining how wrong |
| 99 | //config: it is, you will be ignored. |
Denys Vlasenko | 67f6db6 | 2017-01-30 16:27:37 +0100 | [diff] [blame] | 100 | //config: |
Dimitri John Ledkov | 45fa3f1 | 2020-05-19 18:20:39 +0100 | [diff] [blame] | 101 | //config: FEATURE_WGET_OPENSSL does implement TLS verification |
| 102 | //config: using the certificates available to OpenSSL. |
| 103 | //config: |
Denys Vlasenko | 2007ef5 | 2015-10-07 02:40:53 +0200 | [diff] [blame] | 104 | //config:config FEATURE_WGET_OPENSSL |
| 105 | //config: bool "Try to connect to HTTPS using openssl" |
| 106 | //config: default y |
Denys Vlasenko | 1c6c670 | 2015-10-07 01:39:40 +0200 | [diff] [blame] | 107 | //config: depends on WGET |
Denys Vlasenko | 1c6c670 | 2015-10-07 01:39:40 +0200 | [diff] [blame] | 108 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 109 | //config: Try to use openssl to handle HTTPS. |
Denys Vlasenko | 1c6c670 | 2015-10-07 01:39:40 +0200 | [diff] [blame] | 110 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 111 | //config: OpenSSL has a simple SSL client for debug purposes. |
| 112 | //config: If you select this option, wget will effectively run: |
| 113 | //config: "openssl s_client -quiet -connect hostname:443 |
| 114 | //config: -servername hostname 2>/dev/null" and pipe its data |
| 115 | //config: through it. -servername is not used if hostname is numeric. |
| 116 | //config: Note inconvenient API: host resolution is done twice, |
| 117 | //config: and there is no guarantee openssl's idea of IPv6 address |
| 118 | //config: format is the same as ours. |
| 119 | //config: Another problem is that s_client prints debug information |
| 120 | //config: to stderr, and it needs to be suppressed. This means |
| 121 | //config: all error messages get suppressed too. |
| 122 | //config: openssl is also a big binary, often dynamically linked |
| 123 | //config: against ~15 libraries. |
Denys Vlasenko | 1c6c670 | 2015-10-07 01:39:40 +0200 | [diff] [blame] | 124 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 125 | //config: If openssl can't be executed, internal TLS code will be used |
| 126 | //config: (if you enabled it); if openssl can be executed but fails later, |
| 127 | //config: wget can't detect this, and download will fail. |
Dimitri John Ledkov | 45fa3f1 | 2020-05-19 18:20:39 +0100 | [diff] [blame] | 128 | //config: |
| 129 | //config: By default TLS verification is performed, unless |
| 130 | //config: --no-check-certificate option is passed. |
Denys Vlasenko | 1c6c670 | 2015-10-07 01:39:40 +0200 | [diff] [blame] | 131 | |
| 132 | //applet:IF_WGET(APPLET(wget, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 133 | |
| 134 | //kbuild:lib-$(CONFIG_WGET) += wget.o |
| 135 | |
Denys Vlasenko | e2e55b0 | 2011-03-21 00:37:05 +0100 | [diff] [blame] | 136 | //usage:#define wget_trivial_usage |
| 137 | //usage: IF_FEATURE_WGET_LONG_OPTIONS( |
Denys Vlasenko | a2f18d9 | 2020-12-18 04:12:51 +0100 | [diff] [blame] | 138 | //usage: "[-cqS] [--spider] [-O FILE] [-o LOGFILE] [--header 'HEADER: VALUE'] [-Y on/off]\n" |
Vladimir Dronnikov | f5abc78 | 2012-06-13 17:29:41 +0200 | [diff] [blame] | 139 | /* Since we ignore these opts, we don't show them in --help */ |
Dimitri John Ledkov | 45fa3f1 | 2020-05-19 18:20:39 +0100 | [diff] [blame] | 140 | /* //usage: " [--no-cache] [--passive-ftp] [-t TRIES]" */ |
Denys Vlasenko | 92e1b08 | 2015-10-20 21:51:52 +0200 | [diff] [blame] | 141 | /* //usage: " [-nv] [-nc] [-nH] [-np]" */ |
Denys Vlasenko | a2f18d9 | 2020-12-18 04:12:51 +0100 | [diff] [blame] | 142 | //usage: " "IF_FEATURE_WGET_OPENSSL("[--no-check-certificate] ")"[-P DIR] [-U AGENT]"IF_FEATURE_WGET_TIMEOUT(" [-T SEC]")" URL..." |
Denys Vlasenko | e2e55b0 | 2011-03-21 00:37:05 +0100 | [diff] [blame] | 143 | //usage: ) |
| 144 | //usage: IF_NOT_FEATURE_WGET_LONG_OPTIONS( |
Denys Vlasenko | a2f18d9 | 2020-12-18 04:12:51 +0100 | [diff] [blame] | 145 | //usage: "[-cqS] [-O FILE] [-o LOGFILE] [-Y on/off] [-P DIR] [-U AGENT]"IF_FEATURE_WGET_TIMEOUT(" [-T SEC]")" URL..." |
Denys Vlasenko | e2e55b0 | 2011-03-21 00:37:05 +0100 | [diff] [blame] | 146 | //usage: ) |
| 147 | //usage:#define wget_full_usage "\n\n" |
| 148 | //usage: "Retrieve files via HTTP or FTP\n" |
Denys Vlasenko | 2972e2c | 2016-10-04 04:23:09 +0200 | [diff] [blame] | 149 | //usage: IF_FEATURE_WGET_LONG_OPTIONS( |
Denys Vlasenko | dff9fef | 2017-01-24 21:41:43 +0100 | [diff] [blame] | 150 | //usage: "\n --spider Only check URL existence: $? is 0 if exists" |
Dimitri John Ledkov | 45fa3f1 | 2020-05-19 18:20:39 +0100 | [diff] [blame] | 151 | //usage: IF_FEATURE_WGET_OPENSSL( |
| 152 | //usage: "\n --no-check-certificate Don't validate the server's certificate" |
| 153 | //usage: ) |
Denys Vlasenko | e2e55b0 | 2011-03-21 00:37:05 +0100 | [diff] [blame] | 154 | //usage: ) |
Denys Vlasenko | 2972e2c | 2016-10-04 04:23:09 +0200 | [diff] [blame] | 155 | //usage: "\n -c Continue retrieval of aborted transfer" |
| 156 | //usage: "\n -q Quiet" |
| 157 | //usage: "\n -P DIR Save to DIR (default .)" |
Denys Vlasenko | dff9fef | 2017-01-24 21:41:43 +0100 | [diff] [blame] | 158 | //usage: "\n -S Show server response" |
Denys Vlasenko | 2972e2c | 2016-10-04 04:23:09 +0200 | [diff] [blame] | 159 | //usage: IF_FEATURE_WGET_TIMEOUT( |
| 160 | //usage: "\n -T SEC Network read timeout is SEC seconds" |
| 161 | //usage: ) |
| 162 | //usage: "\n -O FILE Save to FILE ('-' for stdout)" |
Denys Vlasenko | a2f18d9 | 2020-12-18 04:12:51 +0100 | [diff] [blame] | 163 | //usage: "\n -o LOGFILE Log messages to FILE" |
Denys Vlasenko | 2972e2c | 2016-10-04 04:23:09 +0200 | [diff] [blame] | 164 | //usage: "\n -U STR Use STR for User-Agent header" |
| 165 | //usage: "\n -Y on/off Use proxy" |
Denys Vlasenko | e2e55b0 | 2011-03-21 00:37:05 +0100 | [diff] [blame] | 166 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 167 | #include "libbb.h" |
Denis Vlasenko | a552eeb | 2006-09-26 09:22:12 +0000 | [diff] [blame] | 168 | |
Vladimir Dronnikov | f5abc78 | 2012-06-13 17:29:41 +0200 | [diff] [blame] | 169 | #if 0 |
| 170 | # define log_io(...) bb_error_msg(__VA_ARGS__) |
Bernhard Reutner-Fischer | d7bfee1 | 2015-02-18 20:41:02 +0100 | [diff] [blame] | 171 | # define SENDFMT(fp, fmt, ...) \ |
| 172 | do { \ |
| 173 | log_io("> " fmt, ##__VA_ARGS__); \ |
| 174 | fprintf(fp, fmt, ##__VA_ARGS__); \ |
| 175 | } while (0); |
Vladimir Dronnikov | f5abc78 | 2012-06-13 17:29:41 +0200 | [diff] [blame] | 176 | #else |
| 177 | # define log_io(...) ((void)0) |
Bernhard Reutner-Fischer | d7bfee1 | 2015-02-18 20:41:02 +0100 | [diff] [blame] | 178 | # define SENDFMT(fp, fmt, ...) fprintf(fp, fmt, ##__VA_ARGS__) |
Vladimir Dronnikov | f5abc78 | 2012-06-13 17:29:41 +0200 | [diff] [blame] | 179 | #endif |
Denys Vlasenko | f836f01 | 2011-02-10 23:02:28 +0100 | [diff] [blame] | 180 | |
| 181 | |
Denys Vlasenko | 9a647c3 | 2017-01-23 01:08:16 +0100 | [diff] [blame] | 182 | #define SSL_SUPPORTED (ENABLE_FEATURE_WGET_OPENSSL || ENABLE_FEATURE_WGET_HTTPS) |
Sergey Ponomarev | b6e6c83 | 2021-01-17 20:35:08 +0200 | [diff] [blame] | 183 | #define FTPS_SUPPORTED (ENABLE_FEATURE_WGET_FTP && ENABLE_FEATURE_WGET_HTTPS) |
Denys Vlasenko | a6f8651 | 2017-01-11 20:16:45 +0100 | [diff] [blame] | 184 | |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 185 | struct host_info { |
Denys Vlasenko | a366109 | 2011-02-13 02:33:11 +0100 | [diff] [blame] | 186 | char *allocated; |
Denis Vlasenko | 818322b | 2007-09-24 18:27:04 +0000 | [diff] [blame] | 187 | const char *path; |
Denys Vlasenko | d353bff | 2014-02-03 14:09:42 +0100 | [diff] [blame] | 188 | char *user; |
Denys Vlasenko | 8b7e8ae | 2014-02-22 14:12:29 +0100 | [diff] [blame] | 189 | const char *protocol; |
Denis Vlasenko | 818322b | 2007-09-24 18:27:04 +0000 | [diff] [blame] | 190 | char *host; |
| 191 | int port; |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 192 | }; |
Denys Vlasenko | 3e134eb | 2016-04-22 18:09:21 +0200 | [diff] [blame] | 193 | static const char P_HTTP[] ALIGN1 = "http"; |
Denys Vlasenko | a6f8651 | 2017-01-11 20:16:45 +0100 | [diff] [blame] | 194 | #if SSL_SUPPORTED |
Denys Vlasenko | 3e134eb | 2016-04-22 18:09:21 +0200 | [diff] [blame] | 195 | static const char P_HTTPS[] ALIGN1 = "https"; |
Ron Yorston | 4d0c1ea | 2015-10-12 10:51:25 +0100 | [diff] [blame] | 196 | #endif |
Sergey Ponomarev | b6e6c83 | 2021-01-17 20:35:08 +0200 | [diff] [blame] | 197 | #if ENABLE_FEATURE_WGET_FTP |
| 198 | static const char P_FTP[] ALIGN1 = "ftp"; |
| 199 | #endif |
| 200 | #if FTPS_SUPPORTED |
| 201 | static const char P_FTPS[] ALIGN1 = "ftps"; |
| 202 | #endif |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 203 | |
Bernhard Reutner-Fischer | d7bfee1 | 2015-02-18 20:41:02 +0100 | [diff] [blame] | 204 | #if ENABLE_FEATURE_WGET_LONG_OPTIONS |
| 205 | /* User-specified headers prevent using our corresponding built-in headers. */ |
| 206 | enum { |
| 207 | HDR_HOST = (1<<0), |
| 208 | HDR_USER_AGENT = (1<<1), |
| 209 | HDR_RANGE = (1<<2), |
| 210 | HDR_AUTH = (1<<3) * ENABLE_FEATURE_WGET_AUTHENTICATION, |
| 211 | HDR_PROXY_AUTH = (1<<4) * ENABLE_FEATURE_WGET_AUTHENTICATION, |
| 212 | }; |
| 213 | static const char wget_user_headers[] ALIGN1 = |
| 214 | "Host:\0" |
| 215 | "User-Agent:\0" |
| 216 | "Range:\0" |
| 217 | # if ENABLE_FEATURE_WGET_AUTHENTICATION |
| 218 | "Authorization:\0" |
| 219 | "Proxy-Authorization:\0" |
| 220 | # endif |
| 221 | ; |
| 222 | # define USR_HEADER_HOST (G.user_headers & HDR_HOST) |
| 223 | # define USR_HEADER_USER_AGENT (G.user_headers & HDR_USER_AGENT) |
| 224 | # define USR_HEADER_RANGE (G.user_headers & HDR_RANGE) |
| 225 | # define USR_HEADER_AUTH (G.user_headers & HDR_AUTH) |
| 226 | # define USR_HEADER_PROXY_AUTH (G.user_headers & HDR_PROXY_AUTH) |
| 227 | #else /* No long options, no user-headers :( */ |
| 228 | # define USR_HEADER_HOST 0 |
| 229 | # define USR_HEADER_USER_AGENT 0 |
| 230 | # define USR_HEADER_RANGE 0 |
| 231 | # define USR_HEADER_AUTH 0 |
| 232 | # define USR_HEADER_PROXY_AUTH 0 |
| 233 | #endif |
Denis Vlasenko | 7710563 | 2007-09-24 15:04:00 +0000 | [diff] [blame] | 234 | |
Bradley M. Kuhn | c97131c | 2010-08-08 02:51:20 +0200 | [diff] [blame] | 235 | /* Globals */ |
Denis Vlasenko | 7710563 | 2007-09-24 15:04:00 +0000 | [diff] [blame] | 236 | struct globals { |
| 237 | off_t content_len; /* Content-length of the file */ |
| 238 | off_t beg_range; /* Range at which continue begins */ |
| 239 | #if ENABLE_FEATURE_WGET_STATUSBAR |
Denis Vlasenko | 7710563 | 2007-09-24 15:04:00 +0000 | [diff] [blame] | 240 | off_t transferred; /* Number of bytes transferred so far */ |
| 241 | const char *curfile; /* Name of current file being transferred */ |
Magnus Damm | f591499 | 2009-11-08 16:34:43 +0100 | [diff] [blame] | 242 | bb_progress_t pmt; |
Denis Vlasenko | 7710563 | 2007-09-24 15:04:00 +0000 | [diff] [blame] | 243 | #endif |
Denys Vlasenko | 982e87f | 2013-07-30 11:52:58 +0200 | [diff] [blame] | 244 | char *dir_prefix; |
Denys Vlasenko | a366109 | 2011-02-13 02:33:11 +0100 | [diff] [blame] | 245 | #if ENABLE_FEATURE_WGET_LONG_OPTIONS |
Denys Vlasenko | 982e87f | 2013-07-30 11:52:58 +0200 | [diff] [blame] | 246 | char *post_data; |
| 247 | char *extra_headers; |
Bernhard Reutner-Fischer | d7bfee1 | 2015-02-18 20:41:02 +0100 | [diff] [blame] | 248 | unsigned char user_headers; /* Headers mentioned by the user */ |
Denys Vlasenko | a366109 | 2011-02-13 02:33:11 +0100 | [diff] [blame] | 249 | #endif |
Denys Vlasenko | 982e87f | 2013-07-30 11:52:58 +0200 | [diff] [blame] | 250 | char *fname_out; /* where to direct output (-O) */ |
Martin Lewis | 64f3536 | 2018-12-26 16:28:45 +0100 | [diff] [blame] | 251 | char *fname_log; /* where to direct log (-o) */ |
Denys Vlasenko | 982e87f | 2013-07-30 11:52:58 +0200 | [diff] [blame] | 252 | const char *proxy_flag; /* Use proxies if env vars are set */ |
| 253 | const char *user_agent; /* "User-Agent" header field */ |
Denys Vlasenko | 2384a35 | 2011-02-15 00:58:36 +0100 | [diff] [blame] | 254 | int output_fd; |
Martin Lewis | 64f3536 | 2018-12-26 16:28:45 +0100 | [diff] [blame] | 255 | int log_fd; |
Denys Vlasenko | 2384a35 | 2011-02-15 00:58:36 +0100 | [diff] [blame] | 256 | int o_flags; |
Denys Vlasenko | 5084bae | 2018-11-24 21:56:21 +0100 | [diff] [blame] | 257 | #if ENABLE_FEATURE_WGET_TIMEOUT |
| 258 | unsigned timeout_seconds; |
| 259 | smallint die_if_timed_out; |
| 260 | #endif |
Denys Vlasenko | 7f43280 | 2009-06-28 01:02:24 +0200 | [diff] [blame] | 261 | smallint chunked; /* chunked transfer encoding */ |
| 262 | smallint got_clen; /* got content-length: from server */ |
Denys Vlasenko | df4e16c | 2011-02-10 06:29:06 +0100 | [diff] [blame] | 263 | /* Local downloads do benefit from big buffer. |
| 264 | * With 512 byte buffer, it was measured to be |
| 265 | * an order of magnitude slower than with big one. |
| 266 | */ |
Denys Vlasenko | 9b313dd | 2019-01-21 13:53:26 +0100 | [diff] [blame] | 267 | char wget_buf[CONFIG_FEATURE_COPYBUF_KB*1024] ALIGNED(16); |
Denys Vlasenko | 98a4c7c | 2010-02-04 15:00:15 +0100 | [diff] [blame] | 268 | } FIX_ALIASING; |
Denys Vlasenko | df4e16c | 2011-02-10 06:29:06 +0100 | [diff] [blame] | 269 | #define G (*ptr_to_globals) |
Bradley M. Kuhn | c97131c | 2010-08-08 02:51:20 +0200 | [diff] [blame] | 270 | #define INIT_G() do { \ |
Denys Vlasenko | 982e87f | 2013-07-30 11:52:58 +0200 | [diff] [blame] | 271 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
Bradley M. Kuhn | c97131c | 2010-08-08 02:51:20 +0200 | [diff] [blame] | 272 | } while (0) |
Guilherme Maciel Ferreira | 840ef17 | 2013-10-16 14:43:30 +0200 | [diff] [blame] | 273 | #define FINI_G() do { \ |
| 274 | FREE_PTR_TO_GLOBALS(); \ |
| 275 | } while (0) |
Denis Vlasenko | 7710563 | 2007-09-24 15:04:00 +0000 | [diff] [blame] | 276 | |
| 277 | |
Bradley M. Kuhn | c97131c | 2010-08-08 02:51:20 +0200 | [diff] [blame] | 278 | /* Must match option string! */ |
| 279 | enum { |
| 280 | WGET_OPT_CONTINUE = (1 << 0), |
Denys Vlasenko | 2972e2c | 2016-10-04 04:23:09 +0200 | [diff] [blame] | 281 | WGET_OPT_QUIET = (1 << 1), |
Denys Vlasenko | dff9fef | 2017-01-24 21:41:43 +0100 | [diff] [blame] | 282 | WGET_OPT_SERVER_RESPONSE = (1 << 2), |
| 283 | WGET_OPT_OUTNAME = (1 << 3), |
Martin Lewis | 64f3536 | 2018-12-26 16:28:45 +0100 | [diff] [blame] | 284 | WGET_OPT_LOGNAME = (1 << 4), |
| 285 | WGET_OPT_PREFIX = (1 << 5), |
| 286 | WGET_OPT_PROXY = (1 << 6), |
| 287 | WGET_OPT_USER_AGENT = (1 << 7), |
| 288 | WGET_OPT_NETWORK_READ_TIMEOUT = (1 << 8), |
| 289 | WGET_OPT_RETRIES = (1 << 9), |
| 290 | WGET_OPT_nsomething = (1 << 10), |
| 291 | WGET_OPT_HEADER = (1 << 11) * ENABLE_FEATURE_WGET_LONG_OPTIONS, |
| 292 | WGET_OPT_POST_DATA = (1 << 12) * ENABLE_FEATURE_WGET_LONG_OPTIONS, |
| 293 | WGET_OPT_SPIDER = (1 << 13) * ENABLE_FEATURE_WGET_LONG_OPTIONS, |
| 294 | WGET_OPT_NO_CHECK_CERT = (1 << 14) * ENABLE_FEATURE_WGET_LONG_OPTIONS, |
Denys Vlasenko | e7d853b | 2020-12-08 19:06:28 +0100 | [diff] [blame] | 295 | /* hijack this bit for other than opts purposes: */ |
| 296 | WGET_NO_FTRUNCATE = (1 << 31) |
Bradley M. Kuhn | c97131c | 2010-08-08 02:51:20 +0200 | [diff] [blame] | 297 | }; |
| 298 | |
| 299 | enum { |
| 300 | PROGRESS_START = -1, |
| 301 | PROGRESS_END = 0, |
| 302 | PROGRESS_BUMP = 1, |
| 303 | }; |
Denis Vlasenko | 9cade08 | 2006-11-21 10:43:02 +0000 | [diff] [blame] | 304 | #if ENABLE_FEATURE_WGET_STATUSBAR |
Denis Vlasenko | 00d8417 | 2008-11-24 07:34:42 +0000 | [diff] [blame] | 305 | static void progress_meter(int flag) |
Denis Vlasenko | 47ddd01 | 2007-09-24 18:24:17 +0000 | [diff] [blame] | 306 | { |
Denys Vlasenko | 26602b8 | 2018-11-23 19:14:52 +0100 | [diff] [blame] | 307 | int notty; |
| 308 | |
Bradley M. Kuhn | c97131c | 2010-08-08 02:51:20 +0200 | [diff] [blame] | 309 | if (option_mask32 & WGET_OPT_QUIET) |
| 310 | return; |
Denis Vlasenko | 47ddd01 | 2007-09-24 18:24:17 +0000 | [diff] [blame] | 311 | |
Martin Lewis | 64f3536 | 2018-12-26 16:28:45 +0100 | [diff] [blame] | 312 | /* Don't save progress to log file */ |
| 313 | if (G.log_fd >= 0) |
| 314 | return; |
| 315 | |
Bradley M. Kuhn | c97131c | 2010-08-08 02:51:20 +0200 | [diff] [blame] | 316 | if (flag == PROGRESS_START) |
Denys Vlasenko | d55e139 | 2011-02-11 18:56:13 +0100 | [diff] [blame] | 317 | bb_progress_init(&G.pmt, G.curfile); |
Denis Vlasenko | 47ddd01 | 2007-09-24 18:24:17 +0000 | [diff] [blame] | 318 | |
Denys Vlasenko | 26602b8 | 2018-11-23 19:14:52 +0100 | [diff] [blame] | 319 | notty = bb_progress_update(&G.pmt, |
Denys Vlasenko | 2384a35 | 2011-02-15 00:58:36 +0100 | [diff] [blame] | 320 | G.beg_range, |
| 321 | G.transferred, |
| 322 | (G.chunked || !G.got_clen) ? 0 : G.beg_range + G.transferred + G.content_len |
| 323 | ); |
Denis Vlasenko | 47ddd01 | 2007-09-24 18:24:17 +0000 | [diff] [blame] | 324 | |
Bradley M. Kuhn | c97131c | 2010-08-08 02:51:20 +0200 | [diff] [blame] | 325 | if (flag == PROGRESS_END) { |
Denys Vlasenko | a366109 | 2011-02-13 02:33:11 +0100 | [diff] [blame] | 326 | bb_progress_free(&G.pmt); |
Denys Vlasenko | 26602b8 | 2018-11-23 19:14:52 +0100 | [diff] [blame] | 327 | if (notty == 0) |
| 328 | bb_putchar_stderr('\n'); /* it's tty */ |
Denys Vlasenko | a3aa3e3 | 2009-12-11 12:36:10 +0100 | [diff] [blame] | 329 | G.transferred = 0; |
Denis Vlasenko | 47ddd01 | 2007-09-24 18:24:17 +0000 | [diff] [blame] | 330 | } |
| 331 | } |
Bradley M. Kuhn | c97131c | 2010-08-08 02:51:20 +0200 | [diff] [blame] | 332 | #else |
Denys Vlasenko | 8c317f0 | 2019-05-14 17:26:47 +0200 | [diff] [blame] | 333 | static ALWAYS_INLINE void progress_meter(int flag UNUSED_PARAM) {} |
Eric Andersen | b520e08 | 2000-10-03 00:21:45 +0000 | [diff] [blame] | 334 | #endif |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 335 | |
Denis Vlasenko | 47ddd01 | 2007-09-24 18:24:17 +0000 | [diff] [blame] | 336 | |
Denys Vlasenko | 7d5ddf1 | 2009-06-30 20:36:27 +0200 | [diff] [blame] | 337 | /* IPv6 knows scoped address types i.e. link and site local addresses. Link |
| 338 | * local addresses can have a scope identifier to specify the |
| 339 | * interface/link an address is valid on (e.g. fe80::1%eth0). This scope |
| 340 | * identifier is only valid on a single node. |
| 341 | * |
| 342 | * RFC 4007 says that the scope identifier MUST NOT be sent across the wire, |
| 343 | * unless all nodes agree on the semantic. Apache e.g. regards zone identifiers |
| 344 | * in the Host header as invalid requests, see |
| 345 | * https://issues.apache.org/bugzilla/show_bug.cgi?id=35122 |
| 346 | */ |
| 347 | static void strip_ipv6_scope_id(char *host) |
| 348 | { |
| 349 | char *scope, *cp; |
| 350 | |
| 351 | /* bbox wget actually handles IPv6 addresses without [], like |
| 352 | * wget "http://::1/xxx", but this is not standard. |
| 353 | * To save code, _here_ we do not support it. */ |
| 354 | |
| 355 | if (host[0] != '[') |
| 356 | return; /* not IPv6 */ |
| 357 | |
| 358 | scope = strchr(host, '%'); |
| 359 | if (!scope) |
| 360 | return; |
| 361 | |
| 362 | /* Remove the IPv6 zone identifier from the host address */ |
| 363 | cp = strchr(host, ']'); |
| 364 | if (!cp || (cp[1] != ':' && cp[1] != '\0')) { |
| 365 | /* malformed address (not "[xx]:nn" or "[xx]") */ |
| 366 | return; |
| 367 | } |
| 368 | |
| 369 | /* cp points to "]...", scope points to "%eth0]..." */ |
| 370 | overlapping_strcpy(scope, cp); |
| 371 | } |
| 372 | |
Denis Vlasenko | 9cade08 | 2006-11-21 10:43:02 +0000 | [diff] [blame] | 373 | #if ENABLE_FEATURE_WGET_AUTHENTICATION |
Denys Vlasenko | df4e16c | 2011-02-10 06:29:06 +0100 | [diff] [blame] | 374 | /* Base64-encode character string. */ |
| 375 | static char *base64enc(const char *str) |
Denis Vlasenko | 3526a13 | 2006-09-09 12:20:57 +0000 | [diff] [blame] | 376 | { |
Denys Vlasenko | 5084bae | 2018-11-24 21:56:21 +0100 | [diff] [blame] | 377 | /* paranoia */ |
| 378 | unsigned len = strnlen(str, sizeof(G.wget_buf)/4*3 - 10); |
Denys Vlasenko | df4e16c | 2011-02-10 06:29:06 +0100 | [diff] [blame] | 379 | bb_uuencode(G.wget_buf, str, len, bb_uuenc_tbl_base64); |
| 380 | return G.wget_buf; |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 381 | } |
| 382 | #endif |
| 383 | |
Lauri Kasanen | d074b41 | 2013-10-12 21:47:07 +0200 | [diff] [blame] | 384 | #if ENABLE_FEATURE_WGET_TIMEOUT |
| 385 | static void alarm_handler(int sig UNUSED_PARAM) |
| 386 | { |
| 387 | /* This is theoretically unsafe (uses stdio and malloc in signal handler) */ |
Denys Vlasenko | 6701e91 | 2016-03-17 15:58:16 +0100 | [diff] [blame] | 388 | if (G.die_if_timed_out) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 389 | bb_simple_error_msg_and_die("download timed out"); |
Lauri Kasanen | d074b41 | 2013-10-12 21:47:07 +0200 | [diff] [blame] | 390 | } |
Denys Vlasenko | 6701e91 | 2016-03-17 15:58:16 +0100 | [diff] [blame] | 391 | static void set_alarm(void) |
| 392 | { |
| 393 | if (G.timeout_seconds) { |
| 394 | alarm(G.timeout_seconds); |
| 395 | G.die_if_timed_out = 1; |
| 396 | } |
| 397 | } |
| 398 | # define clear_alarm() ((void)(G.die_if_timed_out = 0)) |
| 399 | #else |
| 400 | # define set_alarm() ((void)0) |
| 401 | # define clear_alarm() ((void)0) |
Lauri Kasanen | d074b41 | 2013-10-12 21:47:07 +0200 | [diff] [blame] | 402 | #endif |
| 403 | |
Denys Vlasenko | ed72761 | 2016-07-25 21:34:57 +0200 | [diff] [blame] | 404 | #if ENABLE_FEATURE_WGET_OPENSSL |
| 405 | /* |
| 406 | * is_ip_address() attempts to verify whether or not a string |
| 407 | * contains an IPv4 or IPv6 address (vs. an FQDN). The result |
| 408 | * of inet_pton() can be used to determine this. |
Denys Vlasenko | ed72761 | 2016-07-25 21:34:57 +0200 | [diff] [blame] | 409 | */ |
| 410 | static int is_ip_address(const char *string) |
| 411 | { |
| 412 | struct sockaddr_in sa; |
| 413 | |
| 414 | int result = inet_pton(AF_INET, string, &(sa.sin_addr)); |
| 415 | # if ENABLE_FEATURE_IPV6 |
| 416 | if (result == 0) { |
| 417 | struct sockaddr_in6 sa6; |
| 418 | result = inet_pton(AF_INET6, string, &(sa6.sin6_addr)); |
| 419 | } |
| 420 | # endif |
| 421 | return (result == 1); |
| 422 | } |
| 423 | #endif |
| 424 | |
Denis Vlasenko | 47ddd01 | 2007-09-24 18:24:17 +0000 | [diff] [blame] | 425 | static FILE *open_socket(len_and_sockaddr *lsa) |
| 426 | { |
Lauri Kasanen | d074b41 | 2013-10-12 21:47:07 +0200 | [diff] [blame] | 427 | int fd; |
Denis Vlasenko | 47ddd01 | 2007-09-24 18:24:17 +0000 | [diff] [blame] | 428 | FILE *fp; |
| 429 | |
Denys Vlasenko | 6701e91 | 2016-03-17 15:58:16 +0100 | [diff] [blame] | 430 | set_alarm(); |
Lauri Kasanen | d074b41 | 2013-10-12 21:47:07 +0200 | [diff] [blame] | 431 | fd = xconnect_stream(lsa); |
Denys Vlasenko | 6701e91 | 2016-03-17 15:58:16 +0100 | [diff] [blame] | 432 | clear_alarm(); |
Lauri Kasanen | d074b41 | 2013-10-12 21:47:07 +0200 | [diff] [blame] | 433 | |
Denis Vlasenko | 47ddd01 | 2007-09-24 18:24:17 +0000 | [diff] [blame] | 434 | /* glibc 2.4 seems to try seeking on it - ??! */ |
| 435 | /* hopefully it understands what ESPIPE means... */ |
Lauri Kasanen | d074b41 | 2013-10-12 21:47:07 +0200 | [diff] [blame] | 436 | fp = fdopen(fd, "r+"); |
Denys Vlasenko | 8b7e8ae | 2014-02-22 14:12:29 +0100 | [diff] [blame] | 437 | if (!fp) |
Denys Vlasenko | 899ae53 | 2018-04-01 19:59:37 +0200 | [diff] [blame] | 438 | bb_die_memory_exhausted(); |
Denis Vlasenko | 47ddd01 | 2007-09-24 18:24:17 +0000 | [diff] [blame] | 439 | |
| 440 | return fp; |
| 441 | } |
| 442 | |
Denys Vlasenko | 3459024 | 2018-02-12 16:46:13 +0100 | [diff] [blame] | 443 | /* We balk at any control chars in other side's messages. |
| 444 | * This prevents nasty surprises (e.g. ESC sequences) in "Location:" URLs |
| 445 | * and error messages. |
| 446 | * |
| 447 | * The only exception is tabs, which are converted to (one) space: |
| 448 | * HTTP's "headers: <whitespace> values" may have those. |
| 449 | */ |
| 450 | static char* sanitize_string(char *s) |
| 451 | { |
| 452 | unsigned char *p = (void *) s; |
| 453 | while (*p) { |
| 454 | if (*p < ' ') { |
| 455 | if (*p != '\t') |
| 456 | break; |
| 457 | *p = ' '; |
| 458 | } |
| 459 | p++; |
| 460 | } |
| 461 | *p = '\0'; |
| 462 | return s; |
| 463 | } |
| 464 | |
Denys Vlasenko | f836f01 | 2011-02-10 23:02:28 +0100 | [diff] [blame] | 465 | /* Returns '\n' if it was seen, else '\0'. Trims at first '\r' or '\n' */ |
Denys Vlasenko | 3459024 | 2018-02-12 16:46:13 +0100 | [diff] [blame] | 466 | static char fgets_trim_sanitize(FILE *fp, const char *fmt) |
Denys Vlasenko | f836f01 | 2011-02-10 23:02:28 +0100 | [diff] [blame] | 467 | { |
| 468 | char c; |
| 469 | char *buf_ptr; |
| 470 | |
Denys Vlasenko | 6701e91 | 2016-03-17 15:58:16 +0100 | [diff] [blame] | 471 | set_alarm(); |
Denys Vlasenko | 3459024 | 2018-02-12 16:46:13 +0100 | [diff] [blame] | 472 | if (fgets(G.wget_buf, sizeof(G.wget_buf), fp) == NULL) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 473 | bb_simple_perror_msg_and_die("error getting response"); |
Denys Vlasenko | 6701e91 | 2016-03-17 15:58:16 +0100 | [diff] [blame] | 474 | clear_alarm(); |
Denys Vlasenko | f836f01 | 2011-02-10 23:02:28 +0100 | [diff] [blame] | 475 | |
| 476 | buf_ptr = strchrnul(G.wget_buf, '\n'); |
| 477 | c = *buf_ptr; |
Denys Vlasenko | 3459024 | 2018-02-12 16:46:13 +0100 | [diff] [blame] | 478 | #if 1 |
| 479 | /* Disallow any control chars: trim at first char < 0x20 */ |
| 480 | sanitize_string(G.wget_buf); |
| 481 | #else |
Denys Vlasenko | f836f01 | 2011-02-10 23:02:28 +0100 | [diff] [blame] | 482 | *buf_ptr = '\0'; |
| 483 | buf_ptr = strchrnul(G.wget_buf, '\r'); |
| 484 | *buf_ptr = '\0'; |
Denys Vlasenko | 3459024 | 2018-02-12 16:46:13 +0100 | [diff] [blame] | 485 | #endif |
Denys Vlasenko | f836f01 | 2011-02-10 23:02:28 +0100 | [diff] [blame] | 486 | |
| 487 | log_io("< %s", G.wget_buf); |
| 488 | |
Denys Vlasenko | dff9fef | 2017-01-24 21:41:43 +0100 | [diff] [blame] | 489 | if (fmt && (option_mask32 & WGET_OPT_SERVER_RESPONSE)) |
| 490 | fprintf(stderr, fmt, G.wget_buf); |
| 491 | |
Denys Vlasenko | f836f01 | 2011-02-10 23:02:28 +0100 | [diff] [blame] | 492 | return c; |
| 493 | } |
| 494 | |
Sergey Ponomarev | b6e6c83 | 2021-01-17 20:35:08 +0200 | [diff] [blame] | 495 | #if ENABLE_FEATURE_WGET_FTP |
Denys Vlasenko | df4e16c | 2011-02-10 06:29:06 +0100 | [diff] [blame] | 496 | static int ftpcmd(const char *s1, const char *s2, FILE *fp) |
Denis Vlasenko | 47ddd01 | 2007-09-24 18:24:17 +0000 | [diff] [blame] | 497 | { |
| 498 | int result; |
| 499 | if (s1) { |
Denys Vlasenko | f836f01 | 2011-02-10 23:02:28 +0100 | [diff] [blame] | 500 | if (!s2) |
| 501 | s2 = ""; |
Denis Vlasenko | 47ddd01 | 2007-09-24 18:24:17 +0000 | [diff] [blame] | 502 | fprintf(fp, "%s%s\r\n", s1, s2); |
Denys Vlasenko | dff9fef | 2017-01-24 21:41:43 +0100 | [diff] [blame] | 503 | /* With --server-response, wget also shows its ftp commands */ |
| 504 | if (option_mask32 & WGET_OPT_SERVER_RESPONSE) |
| 505 | fprintf(stderr, "--> %s%s\n\n", s1, s2); |
Denis Vlasenko | 47ddd01 | 2007-09-24 18:24:17 +0000 | [diff] [blame] | 506 | fflush(fp); |
Denys Vlasenko | f836f01 | 2011-02-10 23:02:28 +0100 | [diff] [blame] | 507 | log_io("> %s%s", s1, s2); |
Denis Vlasenko | 47ddd01 | 2007-09-24 18:24:17 +0000 | [diff] [blame] | 508 | } |
| 509 | |
Denys Vlasenko | 3459024 | 2018-02-12 16:46:13 +0100 | [diff] [blame] | 510 | /* Read until "Nxx something" is received */ |
| 511 | G.wget_buf[3] = 0; |
Denis Vlasenko | 47ddd01 | 2007-09-24 18:24:17 +0000 | [diff] [blame] | 512 | do { |
Denys Vlasenko | 3459024 | 2018-02-12 16:46:13 +0100 | [diff] [blame] | 513 | fgets_trim_sanitize(fp, "%s\n"); |
Denys Vlasenko | df4e16c | 2011-02-10 06:29:06 +0100 | [diff] [blame] | 514 | } while (!isdigit(G.wget_buf[0]) || G.wget_buf[3] != ' '); |
Denis Vlasenko | 47ddd01 | 2007-09-24 18:24:17 +0000 | [diff] [blame] | 515 | |
Denys Vlasenko | df4e16c | 2011-02-10 06:29:06 +0100 | [diff] [blame] | 516 | G.wget_buf[3] = '\0'; |
| 517 | result = xatoi_positive(G.wget_buf); |
| 518 | G.wget_buf[3] = ' '; |
Denis Vlasenko | 47ddd01 | 2007-09-24 18:24:17 +0000 | [diff] [blame] | 519 | return result; |
| 520 | } |
Sergey Ponomarev | b6e6c83 | 2021-01-17 20:35:08 +0200 | [diff] [blame] | 521 | #endif |
Denis Vlasenko | 47ddd01 | 2007-09-24 18:24:17 +0000 | [diff] [blame] | 522 | |
Denys Vlasenko | a366109 | 2011-02-13 02:33:11 +0100 | [diff] [blame] | 523 | static void parse_url(const char *src_url, struct host_info *h) |
Denis Vlasenko | 47ddd01 | 2007-09-24 18:24:17 +0000 | [diff] [blame] | 524 | { |
| 525 | char *url, *p, *sp; |
| 526 | |
Denys Vlasenko | a366109 | 2011-02-13 02:33:11 +0100 | [diff] [blame] | 527 | free(h->allocated); |
| 528 | h->allocated = url = xstrdup(src_url); |
Denis Vlasenko | 47ddd01 | 2007-09-24 18:24:17 +0000 | [diff] [blame] | 529 | |
Sergey Ponomarev | b6e6c83 | 2021-01-17 20:35:08 +0200 | [diff] [blame] | 530 | h->protocol = P_HTTP; |
Denys Vlasenko | 8b7e8ae | 2014-02-22 14:12:29 +0100 | [diff] [blame] | 531 | p = strstr(url, "://"); |
| 532 | if (p) { |
| 533 | *p = '\0'; |
| 534 | h->host = p + 3; |
Sergey Ponomarev | b6e6c83 | 2021-01-17 20:35:08 +0200 | [diff] [blame] | 535 | #if ENABLE_FEATURE_WGET_FTP |
Denys Vlasenko | 8b7e8ae | 2014-02-22 14:12:29 +0100 | [diff] [blame] | 536 | if (strcmp(url, P_FTP) == 0) { |
Denys Vlasenko | 2aeb201 | 2018-04-17 12:43:54 +0200 | [diff] [blame] | 537 | h->port = bb_lookup_std_port(P_FTP, "tcp", 21); |
Sergey Ponomarev | b6e6c83 | 2021-01-17 20:35:08 +0200 | [diff] [blame] | 538 | h->protocol = P_FTP; |
Denys Vlasenko | 8b7e8ae | 2014-02-22 14:12:29 +0100 | [diff] [blame] | 539 | } else |
Sergey Ponomarev | b6e6c83 | 2021-01-17 20:35:08 +0200 | [diff] [blame] | 540 | #endif |
| 541 | #if FTPS_SUPPORTED |
Denys Vlasenko | 403f299 | 2018-02-06 15:15:08 +0100 | [diff] [blame] | 542 | if (strcmp(url, P_FTPS) == 0) { |
Denys Vlasenko | 2aeb201 | 2018-04-17 12:43:54 +0200 | [diff] [blame] | 543 | h->port = bb_lookup_std_port(P_FTPS, "tcp", 990); |
Denys Vlasenko | 403f299 | 2018-02-06 15:15:08 +0100 | [diff] [blame] | 544 | h->protocol = P_FTPS; |
| 545 | } else |
Sergey Ponomarev | b6e6c83 | 2021-01-17 20:35:08 +0200 | [diff] [blame] | 546 | #endif |
| 547 | #if SSL_SUPPORTED |
Denys Vlasenko | 8b7e8ae | 2014-02-22 14:12:29 +0100 | [diff] [blame] | 548 | if (strcmp(url, P_HTTPS) == 0) { |
Denys Vlasenko | 2aeb201 | 2018-04-17 12:43:54 +0200 | [diff] [blame] | 549 | h->port = bb_lookup_std_port(P_HTTPS, "tcp", 443); |
Denys Vlasenko | 8b7e8ae | 2014-02-22 14:12:29 +0100 | [diff] [blame] | 550 | h->protocol = P_HTTPS; |
| 551 | } else |
Ron Yorston | 4d0c1ea | 2015-10-12 10:51:25 +0100 | [diff] [blame] | 552 | #endif |
Denys Vlasenko | 8b7e8ae | 2014-02-22 14:12:29 +0100 | [diff] [blame] | 553 | if (strcmp(url, P_HTTP) == 0) { |
Sergey Ponomarev | b6e6c83 | 2021-01-17 20:35:08 +0200 | [diff] [blame] | 554 | goto http; |
Denys Vlasenko | 8b7e8ae | 2014-02-22 14:12:29 +0100 | [diff] [blame] | 555 | } else { |
| 556 | *p = ':'; |
Denys Vlasenko | 3459024 | 2018-02-12 16:46:13 +0100 | [diff] [blame] | 557 | bb_error_msg_and_die("not an http or ftp url: %s", url); |
Denys Vlasenko | 8b7e8ae | 2014-02-22 14:12:29 +0100 | [diff] [blame] | 558 | } |
| 559 | } else { |
Lauri Kasanen | 4967a41 | 2013-12-17 19:03:41 +0100 | [diff] [blame] | 560 | // GNU wget is user-friendly and falls back to http:// |
| 561 | h->host = url; |
Sergey Ponomarev | b6e6c83 | 2021-01-17 20:35:08 +0200 | [diff] [blame] | 562 | http: |
| 563 | h->port = bb_lookup_std_port(P_HTTP, "tcp", 80); |
Denys Vlasenko | 8b7e8ae | 2014-02-22 14:12:29 +0100 | [diff] [blame] | 564 | } |
Denis Vlasenko | 47ddd01 | 2007-09-24 18:24:17 +0000 | [diff] [blame] | 565 | |
| 566 | // FYI: |
| 567 | // "Real" wget 'http://busybox.net?var=a/b' sends this request: |
Denys Vlasenko | a0aae9f | 2017-01-20 14:12:10 +0100 | [diff] [blame] | 568 | // 'GET /?var=a/b HTTP/1.0' |
Denis Vlasenko | 47ddd01 | 2007-09-24 18:24:17 +0000 | [diff] [blame] | 569 | // and saves 'index.html?var=a%2Fb' (we save 'b') |
| 570 | // wget 'http://busybox.net?login=john@doe': |
| 571 | // request: 'GET /?login=john@doe HTTP/1.0' |
Denys Vlasenko | df45eb4 | 2018-04-24 13:35:32 +0200 | [diff] [blame] | 572 | // saves: 'index.html?login=john@doe' (we save 'login=john@doe') |
Denis Vlasenko | 47ddd01 | 2007-09-24 18:24:17 +0000 | [diff] [blame] | 573 | // wget 'http://busybox.net#test/test': |
| 574 | // request: 'GET / HTTP/1.0' |
| 575 | // saves: 'index.html' (we save 'test') |
| 576 | // |
| 577 | // We also don't add unique .N suffix if file exists... |
| 578 | sp = strchr(h->host, '/'); |
| 579 | p = strchr(h->host, '?'); if (!sp || (p && sp > p)) sp = p; |
| 580 | p = strchr(h->host, '#'); if (!sp || (p && sp > p)) sp = p; |
| 581 | if (!sp) { |
Denis Vlasenko | 818322b | 2007-09-24 18:27:04 +0000 | [diff] [blame] | 582 | h->path = ""; |
Denis Vlasenko | 47ddd01 | 2007-09-24 18:24:17 +0000 | [diff] [blame] | 583 | } else if (*sp == '/') { |
| 584 | *sp = '\0'; |
| 585 | h->path = sp + 1; |
Denys Vlasenko | df45eb4 | 2018-04-24 13:35:32 +0200 | [diff] [blame] | 586 | } else { |
| 587 | // sp points to '#' or '?' |
| 588 | // Note: |
Denis Vlasenko | 47ddd01 | 2007-09-24 18:24:17 +0000 | [diff] [blame] | 589 | // http://busybox.net?login=john@doe is a valid URL |
Denys Vlasenko | df45eb4 | 2018-04-24 13:35:32 +0200 | [diff] [blame] | 590 | // (without '/' between ".net" and "?"), |
| 591 | // can't store NUL at sp[-1] - this destroys hostname. |
| 592 | *sp++ = '\0'; |
Denis Vlasenko | 47ddd01 | 2007-09-24 18:24:17 +0000 | [diff] [blame] | 593 | h->path = sp; |
| 594 | } |
| 595 | |
| 596 | sp = strrchr(h->host, '@'); |
Denis Vlasenko | 47ddd01 | 2007-09-24 18:24:17 +0000 | [diff] [blame] | 597 | if (sp != NULL) { |
Denys Vlasenko | dd1061b | 2011-09-11 21:04:02 +0200 | [diff] [blame] | 598 | // URL-decode "user:password" string before base64-encoding: |
| 599 | // wget http://test:my%20pass@example.com should send |
| 600 | // Authorization: Basic dGVzdDpteSBwYXNz |
| 601 | // which decodes to "test:my pass". |
| 602 | // Standard wget and curl do this too. |
Denis Vlasenko | 47ddd01 | 2007-09-24 18:24:17 +0000 | [diff] [blame] | 603 | *sp = '\0'; |
Denys Vlasenko | d353bff | 2014-02-03 14:09:42 +0100 | [diff] [blame] | 604 | free(h->user); |
| 605 | h->user = xstrdup(percent_decode_in_place(h->host, /*strict:*/ 0)); |
Denis Vlasenko | 47ddd01 | 2007-09-24 18:24:17 +0000 | [diff] [blame] | 606 | h->host = sp + 1; |
| 607 | } |
Denys Vlasenko | d353bff | 2014-02-03 14:09:42 +0100 | [diff] [blame] | 608 | /* else: h->user remains NULL, or as set by original request |
| 609 | * before redirect (if we are here after a redirect). |
| 610 | */ |
Denis Vlasenko | 47ddd01 | 2007-09-24 18:24:17 +0000 | [diff] [blame] | 611 | } |
| 612 | |
Denys Vlasenko | 3459024 | 2018-02-12 16:46:13 +0100 | [diff] [blame] | 613 | static char *get_sanitized_hdr(FILE *fp) |
Denis Vlasenko | 47ddd01 | 2007-09-24 18:24:17 +0000 | [diff] [blame] | 614 | { |
| 615 | char *s, *hdrval; |
| 616 | int c; |
| 617 | |
Denis Vlasenko | 47ddd01 | 2007-09-24 18:24:17 +0000 | [diff] [blame] | 618 | /* retrieve header line */ |
Denys Vlasenko | 3459024 | 2018-02-12 16:46:13 +0100 | [diff] [blame] | 619 | c = fgets_trim_sanitize(fp, " %s\n"); |
Denis Vlasenko | 47ddd01 | 2007-09-24 18:24:17 +0000 | [diff] [blame] | 620 | |
Denys Vlasenko | f836f01 | 2011-02-10 23:02:28 +0100 | [diff] [blame] | 621 | /* end of the headers? */ |
| 622 | if (G.wget_buf[0] == '\0') |
Denis Vlasenko | 47ddd01 | 2007-09-24 18:24:17 +0000 | [diff] [blame] | 623 | return NULL; |
| 624 | |
| 625 | /* convert the header name to lower case */ |
Denys Vlasenko | ea267d5 | 2013-07-01 15:01:50 +0200 | [diff] [blame] | 626 | for (s = G.wget_buf; isalnum(*s) || *s == '-' || *s == '.' || *s == '_'; ++s) { |
| 627 | /* |
| 628 | * No-op for 20-3f and 60-7f. "0-9a-z-." are in these ranges. |
| 629 | * 40-5f range ("@A-Z[\]^_") maps to 60-7f. |
| 630 | * "A-Z" maps to "a-z". |
| 631 | * "@[\]" can't occur in header names. |
| 632 | * "^_" maps to "~,DEL" (which is wrong). |
| 633 | * "^" was never seen yet, "_" was seen from web.archive.org |
| 634 | * (x-archive-orig-x_commoncrawl_Signature: HEXSTRING). |
| 635 | */ |
Denys Vlasenko | f836f01 | 2011-02-10 23:02:28 +0100 | [diff] [blame] | 636 | *s |= 0x20; |
Denys Vlasenko | 4836331 | 2010-04-04 15:29:32 +0200 | [diff] [blame] | 637 | } |
Denis Vlasenko | 47ddd01 | 2007-09-24 18:24:17 +0000 | [diff] [blame] | 638 | |
| 639 | /* verify we are at the end of the header name */ |
| 640 | if (*s != ':') |
Denys Vlasenko | 3459024 | 2018-02-12 16:46:13 +0100 | [diff] [blame] | 641 | bb_error_msg_and_die("bad header line: %s", G.wget_buf); |
Denis Vlasenko | 47ddd01 | 2007-09-24 18:24:17 +0000 | [diff] [blame] | 642 | |
| 643 | /* locate the start of the header value */ |
| 644 | *s++ = '\0'; |
| 645 | hdrval = skip_whitespace(s); |
| 646 | |
Denys Vlasenko | f836f01 | 2011-02-10 23:02:28 +0100 | [diff] [blame] | 647 | if (c != '\n') { |
| 648 | /* Rats! The buffer isn't big enough to hold the entire header value */ |
| 649 | while (c = getc(fp), c != EOF && c != '\n') |
| 650 | continue; |
Denis Vlasenko | 47ddd01 | 2007-09-24 18:24:17 +0000 | [diff] [blame] | 651 | } |
| 652 | |
Denis Vlasenko | 47ddd01 | 2007-09-24 18:24:17 +0000 | [diff] [blame] | 653 | return hdrval; |
| 654 | } |
| 655 | |
Denys Vlasenko | bf146b8 | 2012-06-13 17:31:07 +0200 | [diff] [blame] | 656 | static void reset_beg_range_to_zero(void) |
| 657 | { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 658 | bb_simple_error_msg("restart failed"); |
Denys Vlasenko | bf146b8 | 2012-06-13 17:31:07 +0200 | [diff] [blame] | 659 | G.beg_range = 0; |
| 660 | xlseek(G.output_fd, 0, SEEK_SET); |
Denys Vlasenko | 6144124 | 2012-06-17 19:52:25 +0200 | [diff] [blame] | 661 | /* Done at the end instead: */ |
| 662 | /* ftruncate(G.output_fd, 0); */ |
Denys Vlasenko | bf146b8 | 2012-06-13 17:31:07 +0200 | [diff] [blame] | 663 | } |
| 664 | |
Denys Vlasenko | 1c6c670 | 2015-10-07 01:39:40 +0200 | [diff] [blame] | 665 | #if ENABLE_FEATURE_WGET_OPENSSL |
Denys Vlasenko | 2007ef5 | 2015-10-07 02:40:53 +0200 | [diff] [blame] | 666 | static int spawn_https_helper_openssl(const char *host, unsigned port) |
Denys Vlasenko | 8b7e8ae | 2014-02-22 14:12:29 +0100 | [diff] [blame] | 667 | { |
| 668 | char *allocated = NULL; |
Denys Vlasenko | ed72761 | 2016-07-25 21:34:57 +0200 | [diff] [blame] | 669 | char *servername; |
Denys Vlasenko | 8b7e8ae | 2014-02-22 14:12:29 +0100 | [diff] [blame] | 670 | int sp[2]; |
| 671 | int pid; |
Denys Vlasenko | 9a647c3 | 2017-01-23 01:08:16 +0100 | [diff] [blame] | 672 | IF_FEATURE_WGET_HTTPS(volatile int child_failed = 0;) |
Denys Vlasenko | 8b7e8ae | 2014-02-22 14:12:29 +0100 | [diff] [blame] | 673 | |
| 674 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, sp) != 0) |
| 675 | /* Kernel can have AF_UNIX support disabled */ |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 676 | bb_simple_perror_msg_and_die("socketpair"); |
Denys Vlasenko | 8b7e8ae | 2014-02-22 14:12:29 +0100 | [diff] [blame] | 677 | |
| 678 | if (!strchr(host, ':')) |
| 679 | host = allocated = xasprintf("%s:%u", host, port); |
Denys Vlasenko | ed72761 | 2016-07-25 21:34:57 +0200 | [diff] [blame] | 680 | servername = xstrdup(host); |
| 681 | strrchr(servername, ':')[0] = '\0'; |
Denys Vlasenko | 8b7e8ae | 2014-02-22 14:12:29 +0100 | [diff] [blame] | 682 | |
Denys Vlasenko | 2007ef5 | 2015-10-07 02:40:53 +0200 | [diff] [blame] | 683 | fflush_all(); |
| 684 | pid = xvfork(); |
Denys Vlasenko | 8b7e8ae | 2014-02-22 14:12:29 +0100 | [diff] [blame] | 685 | if (pid == 0) { |
| 686 | /* Child */ |
Scott Court | fc2ce04 | 2020-06-29 14:30:12 +0200 | [diff] [blame] | 687 | char *argv[13]; |
| 688 | char **argp; |
Denys Vlasenko | 8b7e8ae | 2014-02-22 14:12:29 +0100 | [diff] [blame] | 689 | |
| 690 | close(sp[0]); |
| 691 | xmove_fd(sp[1], 0); |
| 692 | xdup2(0, 1); |
| 693 | /* |
Denys Vlasenko | 8b7e8ae | 2014-02-22 14:12:29 +0100 | [diff] [blame] | 694 | * openssl s_client -quiet -connect www.kernel.org:443 2>/dev/null |
| 695 | * It prints some debug stuff on stderr, don't know how to suppress it. |
| 696 | * Work around by dev-nulling stderr. We lose all error messages :( |
| 697 | */ |
| 698 | xmove_fd(2, 3); |
| 699 | xopen("/dev/null", O_RDWR); |
Denys Vlasenko | ed72761 | 2016-07-25 21:34:57 +0200 | [diff] [blame] | 700 | memset(&argv, 0, sizeof(argv)); |
Denys Vlasenko | 8b7e8ae | 2014-02-22 14:12:29 +0100 | [diff] [blame] | 701 | argv[0] = (char*)"openssl"; |
| 702 | argv[1] = (char*)"s_client"; |
| 703 | argv[2] = (char*)"-quiet"; |
| 704 | argv[3] = (char*)"-connect"; |
| 705 | argv[4] = (char*)host; |
Denys Vlasenko | ed72761 | 2016-07-25 21:34:57 +0200 | [diff] [blame] | 706 | /* |
| 707 | * Per RFC 6066 Section 3, the only permitted values in the |
| 708 | * TLS server_name (SNI) field are FQDNs (DNS hostnames). |
| 709 | * IPv4 and IPv6 addresses, port numbers are not allowed. |
| 710 | */ |
Scott Court | fc2ce04 | 2020-06-29 14:30:12 +0200 | [diff] [blame] | 711 | argp = &argv[5]; |
Denys Vlasenko | ed72761 | 2016-07-25 21:34:57 +0200 | [diff] [blame] | 712 | if (!is_ip_address(servername)) { |
Scott Court | fc2ce04 | 2020-06-29 14:30:12 +0200 | [diff] [blame] | 713 | *argp++ = (char*)"-servername"; //[5] |
| 714 | *argp++ = (char*)servername; //[6] |
Denys Vlasenko | ed72761 | 2016-07-25 21:34:57 +0200 | [diff] [blame] | 715 | } |
Dimitri John Ledkov | 45fa3f1 | 2020-05-19 18:20:39 +0100 | [diff] [blame] | 716 | if (!(option_mask32 & WGET_OPT_NO_CHECK_CERT)) { |
Scott Court | fc2ce04 | 2020-06-29 14:30:12 +0200 | [diff] [blame] | 717 | /* Abort on bad server certificate */ |
| 718 | *argp++ = (char*)"-verify"; //[7] |
| 719 | *argp++ = (char*)"100"; //[8] |
| 720 | *argp++ = (char*)"-verify_return_error"; //[9] |
| 721 | if (!is_ip_address(servername)) { |
| 722 | *argp++ = (char*)"-verify_hostname"; //[10] |
| 723 | *argp++ = (char*)servername; //[11] |
| 724 | } else { |
| 725 | *argp++ = (char*)"-verify_ip"; //[10] |
| 726 | *argp++ = (char*)host; //[11] |
| 727 | } |
Dimitri John Ledkov | 45fa3f1 | 2020-05-19 18:20:39 +0100 | [diff] [blame] | 728 | } |
Scott Court | fc2ce04 | 2020-06-29 14:30:12 +0200 | [diff] [blame] | 729 | //[12] (or earlier) is NULL terminator |
Denys Vlasenko | ed72761 | 2016-07-25 21:34:57 +0200 | [diff] [blame] | 730 | |
Denys Vlasenko | 8b7e8ae | 2014-02-22 14:12:29 +0100 | [diff] [blame] | 731 | BB_EXECVP(argv[0], argv); |
| 732 | xmove_fd(3, 2); |
Denys Vlasenko | 9a647c3 | 2017-01-23 01:08:16 +0100 | [diff] [blame] | 733 | # if ENABLE_FEATURE_WGET_HTTPS |
Denys Vlasenko | 2007ef5 | 2015-10-07 02:40:53 +0200 | [diff] [blame] | 734 | child_failed = 1; |
| 735 | xfunc_die(); |
| 736 | # else |
Denys Vlasenko | 8b7e8ae | 2014-02-22 14:12:29 +0100 | [diff] [blame] | 737 | bb_perror_msg_and_die("can't execute '%s'", argv[0]); |
Denys Vlasenko | 2007ef5 | 2015-10-07 02:40:53 +0200 | [diff] [blame] | 738 | # endif |
Denys Vlasenko | 8b7e8ae | 2014-02-22 14:12:29 +0100 | [diff] [blame] | 739 | /* notreached */ |
| 740 | } |
| 741 | |
Denys Vlasenko | 5331557 | 2014-02-23 23:39:47 +0100 | [diff] [blame] | 742 | /* Parent */ |
Denys Vlasenko | ed72761 | 2016-07-25 21:34:57 +0200 | [diff] [blame] | 743 | free(servername); |
Denys Vlasenko | 8b7e8ae | 2014-02-22 14:12:29 +0100 | [diff] [blame] | 744 | free(allocated); |
| 745 | close(sp[1]); |
Denys Vlasenko | 9a647c3 | 2017-01-23 01:08:16 +0100 | [diff] [blame] | 746 | # if ENABLE_FEATURE_WGET_HTTPS |
Denys Vlasenko | 2007ef5 | 2015-10-07 02:40:53 +0200 | [diff] [blame] | 747 | if (child_failed) { |
| 748 | close(sp[0]); |
| 749 | return -1; |
| 750 | } |
| 751 | # endif |
Denys Vlasenko | 8b7e8ae | 2014-02-22 14:12:29 +0100 | [diff] [blame] | 752 | return sp[0]; |
| 753 | } |
Denys Vlasenko | 1c6c670 | 2015-10-07 01:39:40 +0200 | [diff] [blame] | 754 | #endif |
Denys Vlasenko | 8b7e8ae | 2014-02-22 14:12:29 +0100 | [diff] [blame] | 755 | |
Denys Vlasenko | 9a647c3 | 2017-01-23 01:08:16 +0100 | [diff] [blame] | 756 | #if ENABLE_FEATURE_WGET_HTTPS |
Denys Vlasenko | 403f299 | 2018-02-06 15:15:08 +0100 | [diff] [blame] | 757 | static void spawn_ssl_client(const char *host, int network_fd, int flags) |
Denys Vlasenko | 5331557 | 2014-02-23 23:39:47 +0100 | [diff] [blame] | 758 | { |
| 759 | int sp[2]; |
| 760 | int pid; |
Denys Vlasenko | 9a647c3 | 2017-01-23 01:08:16 +0100 | [diff] [blame] | 761 | char *servername, *p; |
| 762 | |
Denys Vlasenko | dbe9568 | 2018-11-13 12:00:19 +0100 | [diff] [blame] | 763 | if (!(option_mask32 & WGET_OPT_NO_CHECK_CERT)) { |
Denys Vlasenko | dbe9568 | 2018-11-13 12:00:19 +0100 | [diff] [blame] | 764 | option_mask32 |= WGET_OPT_NO_CHECK_CERT; |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 765 | bb_simple_error_msg("note: TLS certificate validation not implemented"); |
Denys Vlasenko | dbe9568 | 2018-11-13 12:00:19 +0100 | [diff] [blame] | 766 | } |
Denys Vlasenko | 0972c7f | 2018-05-28 14:36:26 +0200 | [diff] [blame] | 767 | |
Denys Vlasenko | 9a647c3 | 2017-01-23 01:08:16 +0100 | [diff] [blame] | 768 | servername = xstrdup(host); |
| 769 | p = strrchr(servername, ':'); |
| 770 | if (p) *p = '\0'; |
Denys Vlasenko | 5331557 | 2014-02-23 23:39:47 +0100 | [diff] [blame] | 771 | |
| 772 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, sp) != 0) |
| 773 | /* Kernel can have AF_UNIX support disabled */ |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 774 | bb_simple_perror_msg_and_die("socketpair"); |
Denys Vlasenko | 5331557 | 2014-02-23 23:39:47 +0100 | [diff] [blame] | 775 | |
Denys Vlasenko | 9a647c3 | 2017-01-23 01:08:16 +0100 | [diff] [blame] | 776 | fflush_all(); |
Denys Vlasenko | 5331557 | 2014-02-23 23:39:47 +0100 | [diff] [blame] | 777 | pid = BB_MMU ? xfork() : xvfork(); |
| 778 | if (pid == 0) { |
| 779 | /* Child */ |
Denys Vlasenko | 5331557 | 2014-02-23 23:39:47 +0100 | [diff] [blame] | 780 | close(sp[0]); |
| 781 | xmove_fd(sp[1], 0); |
| 782 | xdup2(0, 1); |
Denys Vlasenko | 9a647c3 | 2017-01-23 01:08:16 +0100 | [diff] [blame] | 783 | if (BB_MMU) { |
| 784 | tls_state_t *tls = new_tls_state(); |
| 785 | tls->ifd = tls->ofd = network_fd; |
| 786 | tls_handshake(tls, servername); |
Denys Vlasenko | 403f299 | 2018-02-06 15:15:08 +0100 | [diff] [blame] | 787 | tls_run_copy_loop(tls, flags); |
Denys Vlasenko | 9a647c3 | 2017-01-23 01:08:16 +0100 | [diff] [blame] | 788 | exit(0); |
| 789 | } else { |
Denys Vlasenko | 403f299 | 2018-02-06 15:15:08 +0100 | [diff] [blame] | 790 | char *argv[6]; |
| 791 | |
Denys Vlasenko | 9a647c3 | 2017-01-23 01:08:16 +0100 | [diff] [blame] | 792 | xmove_fd(network_fd, 3); |
| 793 | argv[0] = (char*)"ssl_client"; |
| 794 | argv[1] = (char*)"-s3"; |
| 795 | //TODO: if (!is_ip_address(servername))... |
| 796 | argv[2] = (char*)"-n"; |
| 797 | argv[3] = servername; |
Denys Vlasenko | 403f299 | 2018-02-06 15:15:08 +0100 | [diff] [blame] | 798 | argv[4] = (flags & TLSLOOP_EXIT_ON_LOCAL_EOF ? (char*)"-e" : NULL); |
| 799 | argv[5] = NULL; |
Denys Vlasenko | 9a647c3 | 2017-01-23 01:08:16 +0100 | [diff] [blame] | 800 | BB_EXECVP(argv[0], argv); |
| 801 | bb_perror_msg_and_die("can't execute '%s'", argv[0]); |
| 802 | } |
Denys Vlasenko | 5331557 | 2014-02-23 23:39:47 +0100 | [diff] [blame] | 803 | /* notreached */ |
| 804 | } |
| 805 | |
| 806 | /* Parent */ |
Denys Vlasenko | 9a647c3 | 2017-01-23 01:08:16 +0100 | [diff] [blame] | 807 | free(servername); |
Denys Vlasenko | 5331557 | 2014-02-23 23:39:47 +0100 | [diff] [blame] | 808 | close(sp[1]); |
| 809 | xmove_fd(sp[0], network_fd); |
| 810 | } |
| 811 | #endif |
| 812 | |
Sergey Ponomarev | b6e6c83 | 2021-01-17 20:35:08 +0200 | [diff] [blame] | 813 | #if ENABLE_FEATURE_WGET_FTP |
Denys Vlasenko | e999657 | 2018-02-06 15:02:16 +0100 | [diff] [blame] | 814 | static FILE* prepare_ftp_session(FILE **dfpp, struct host_info *target, len_and_sockaddr *lsa) |
| 815 | { |
| 816 | FILE *sfp; |
Denys Vlasenko | 32c3e3a | 2018-04-07 13:22:52 +0200 | [diff] [blame] | 817 | char *pass; |
Denys Vlasenko | e999657 | 2018-02-06 15:02:16 +0100 | [diff] [blame] | 818 | int port; |
| 819 | |
Denys Vlasenko | e999657 | 2018-02-06 15:02:16 +0100 | [diff] [blame] | 820 | sfp = open_socket(lsa); |
Sergey Ponomarev | b6e6c83 | 2021-01-17 20:35:08 +0200 | [diff] [blame] | 821 | #if FTPS_SUPPORTED |
Denys Vlasenko | 403f299 | 2018-02-06 15:15:08 +0100 | [diff] [blame] | 822 | if (target->protocol == P_FTPS) |
| 823 | spawn_ssl_client(target->host, fileno(sfp), TLSLOOP_EXIT_ON_LOCAL_EOF); |
| 824 | #endif |
| 825 | |
Denys Vlasenko | e999657 | 2018-02-06 15:02:16 +0100 | [diff] [blame] | 826 | if (ftpcmd(NULL, NULL, sfp) != 220) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 827 | bb_simple_error_msg_and_die(G.wget_buf); |
Denys Vlasenko | 3459024 | 2018-02-12 16:46:13 +0100 | [diff] [blame] | 828 | /* note: ftpcmd() sanitizes G.wget_buf, ok to print */ |
Denys Vlasenko | e999657 | 2018-02-06 15:02:16 +0100 | [diff] [blame] | 829 | |
Denys Vlasenko | 32c3e3a | 2018-04-07 13:22:52 +0200 | [diff] [blame] | 830 | /* Split username:password pair */ |
| 831 | pass = (char*)"busybox"; /* password for "anonymous" */ |
| 832 | if (target->user) { |
| 833 | pass = strchr(target->user, ':'); |
| 834 | if (pass) |
| 835 | *pass++ = '\0'; |
| 836 | } |
| 837 | |
| 838 | /* Log in */ |
| 839 | switch (ftpcmd("USER ", target->user ?: "anonymous", sfp)) { |
Denys Vlasenko | e999657 | 2018-02-06 15:02:16 +0100 | [diff] [blame] | 840 | case 230: |
| 841 | break; |
| 842 | case 331: |
Denys Vlasenko | 32c3e3a | 2018-04-07 13:22:52 +0200 | [diff] [blame] | 843 | if (ftpcmd("PASS ", pass, sfp) == 230) |
Denys Vlasenko | e999657 | 2018-02-06 15:02:16 +0100 | [diff] [blame] | 844 | break; |
| 845 | /* fall through (failed login) */ |
| 846 | default: |
Denys Vlasenko | 3459024 | 2018-02-12 16:46:13 +0100 | [diff] [blame] | 847 | bb_error_msg_and_die("ftp login: %s", G.wget_buf); |
Denys Vlasenko | e999657 | 2018-02-06 15:02:16 +0100 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | ftpcmd("TYPE I", NULL, sfp); |
| 851 | |
Denys Vlasenko | 32c3e3a | 2018-04-07 13:22:52 +0200 | [diff] [blame] | 852 | /* Query file size */ |
Denys Vlasenko | e999657 | 2018-02-06 15:02:16 +0100 | [diff] [blame] | 853 | if (ftpcmd("SIZE ", target->path, sfp) == 213) { |
| 854 | G.content_len = BB_STRTOOFF(G.wget_buf + 4, NULL, 10); |
| 855 | if (G.content_len < 0 || errno) { |
Denys Vlasenko | 8e2174e | 2018-04-08 18:06:24 +0200 | [diff] [blame] | 856 | bb_error_msg_and_die("bad SIZE value '%s'", G.wget_buf + 4); |
Denys Vlasenko | e999657 | 2018-02-06 15:02:16 +0100 | [diff] [blame] | 857 | } |
| 858 | G.got_clen = 1; |
| 859 | } |
| 860 | |
Denys Vlasenko | 32c3e3a | 2018-04-07 13:22:52 +0200 | [diff] [blame] | 861 | /* Enter passive mode */ |
Denys Vlasenko | 1783ffa | 2018-02-06 15:48:12 +0100 | [diff] [blame] | 862 | if (ENABLE_FEATURE_IPV6 && ftpcmd("EPSV", NULL, sfp) == 229) { |
| 863 | /* good */ |
| 864 | } else |
Denys Vlasenko | e999657 | 2018-02-06 15:02:16 +0100 | [diff] [blame] | 865 | if (ftpcmd("PASV", NULL, sfp) != 227) { |
| 866 | pasv_error: |
Denys Vlasenko | 3459024 | 2018-02-12 16:46:13 +0100 | [diff] [blame] | 867 | bb_error_msg_and_die("bad response to %s: %s", "PASV", G.wget_buf); |
Denys Vlasenko | e999657 | 2018-02-06 15:02:16 +0100 | [diff] [blame] | 868 | } |
Denys Vlasenko | 1783ffa | 2018-02-06 15:48:12 +0100 | [diff] [blame] | 869 | port = parse_pasv_epsv(G.wget_buf); |
| 870 | if (port < 0) |
| 871 | goto pasv_error; |
| 872 | |
Denys Vlasenko | e999657 | 2018-02-06 15:02:16 +0100 | [diff] [blame] | 873 | set_nport(&lsa->u.sa, htons(port)); |
| 874 | |
| 875 | *dfpp = open_socket(lsa); |
| 876 | |
Sergey Ponomarev | b6e6c83 | 2021-01-17 20:35:08 +0200 | [diff] [blame] | 877 | #if FTPS_SUPPORTED |
Denys Vlasenko | 237a900 | 2018-02-08 00:28:30 +0100 | [diff] [blame] | 878 | if (target->protocol == P_FTPS) { |
| 879 | /* "PROT P" enables encryption of data stream. |
| 880 | * Without it (or with "PROT C"), data is sent unencrypted. |
| 881 | */ |
| 882 | if (ftpcmd("PROT P", NULL, sfp) == 200) |
| 883 | spawn_ssl_client(target->host, fileno(*dfpp), /*flags*/ 0); |
| 884 | } |
Denys Vlasenko | 2b75157 | 2018-02-06 20:49:27 +0100 | [diff] [blame] | 885 | #endif |
Denys Vlasenko | 403f299 | 2018-02-06 15:15:08 +0100 | [diff] [blame] | 886 | |
Denys Vlasenko | e999657 | 2018-02-06 15:02:16 +0100 | [diff] [blame] | 887 | if (G.beg_range != 0) { |
| 888 | sprintf(G.wget_buf, "REST %"OFF_FMT"u", G.beg_range); |
| 889 | if (ftpcmd(G.wget_buf, NULL, sfp) == 350) |
| 890 | G.content_len -= G.beg_range; |
| 891 | else |
| 892 | reset_beg_range_to_zero(); |
| 893 | } |
| 894 | |
Denys Vlasenko | 3459024 | 2018-02-12 16:46:13 +0100 | [diff] [blame] | 895 | //TODO: needs ftp-escaping 0xff and '\n' bytes here. |
| 896 | //Or disallow '\n' altogether via sanitize_string() in parse_url(). |
| 897 | //But 0xff's are possible in valid utf8 filenames. |
Denys Vlasenko | e999657 | 2018-02-06 15:02:16 +0100 | [diff] [blame] | 898 | if (ftpcmd("RETR ", target->path, sfp) > 150) |
Denys Vlasenko | 3459024 | 2018-02-12 16:46:13 +0100 | [diff] [blame] | 899 | bb_error_msg_and_die("bad response to %s: %s", "RETR", G.wget_buf); |
Denys Vlasenko | e999657 | 2018-02-06 15:02:16 +0100 | [diff] [blame] | 900 | |
| 901 | return sfp; |
| 902 | } |
Sergey Ponomarev | b6e6c83 | 2021-01-17 20:35:08 +0200 | [diff] [blame] | 903 | #endif |
Denys Vlasenko | e999657 | 2018-02-06 15:02:16 +0100 | [diff] [blame] | 904 | |
Denys Vlasenko | 2384a35 | 2011-02-15 00:58:36 +0100 | [diff] [blame] | 905 | static void NOINLINE retrieve_file_data(FILE *dfp) |
Denys Vlasenko | 7f43280 | 2009-06-28 01:02:24 +0200 | [diff] [blame] | 906 | { |
Bradley M. Kuhn | c97131c | 2010-08-08 02:51:20 +0200 | [diff] [blame] | 907 | #if ENABLE_FEATURE_WGET_STATUSBAR || ENABLE_FEATURE_WGET_TIMEOUT |
| 908 | # if ENABLE_FEATURE_WGET_TIMEOUT |
Denys Vlasenko | b7812ce | 2012-09-03 12:49:30 +0200 | [diff] [blame] | 909 | unsigned second_cnt = G.timeout_seconds; |
Bradley M. Kuhn | c97131c | 2010-08-08 02:51:20 +0200 | [diff] [blame] | 910 | # endif |
| 911 | struct pollfd polldata; |
Denys Vlasenko | 7f43280 | 2009-06-28 01:02:24 +0200 | [diff] [blame] | 912 | |
Bradley M. Kuhn | c97131c | 2010-08-08 02:51:20 +0200 | [diff] [blame] | 913 | polldata.fd = fileno(dfp); |
| 914 | polldata.events = POLLIN | POLLPRI; |
Bradley M. Kuhn | c97131c | 2010-08-08 02:51:20 +0200 | [diff] [blame] | 915 | #endif |
Martin Lewis | 94e748d | 2019-01-10 13:59:30 +0100 | [diff] [blame] | 916 | if (!(option_mask32 & WGET_OPT_QUIET)) { |
| 917 | if (G.output_fd == 1) |
| 918 | fprintf(stderr, "writing to stdout\n"); |
| 919 | else |
| 920 | fprintf(stderr, "saving to '%s'\n", G.fname_out); |
| 921 | } |
Bradley M. Kuhn | c97131c | 2010-08-08 02:51:20 +0200 | [diff] [blame] | 922 | progress_meter(PROGRESS_START); |
Denys Vlasenko | 7f43280 | 2009-06-28 01:02:24 +0200 | [diff] [blame] | 923 | |
| 924 | if (G.chunked) |
| 925 | goto get_clen; |
| 926 | |
| 927 | /* Loops only if chunked */ |
| 928 | while (1) { |
Denys Vlasenko | c60f446 | 2011-02-11 22:23:23 +0100 | [diff] [blame] | 929 | |
| 930 | #if ENABLE_FEATURE_WGET_STATUSBAR || ENABLE_FEATURE_WGET_TIMEOUT |
| 931 | /* Must use nonblocking I/O, otherwise fread will loop |
| 932 | * and *block* until it reads full buffer, |
| 933 | * which messes up progress bar and/or timeout logic. |
| 934 | * Because of nonblocking I/O, we need to dance |
| 935 | * very carefully around EAGAIN. See explanation at |
Denys Vlasenko | b7812ce | 2012-09-03 12:49:30 +0200 | [diff] [blame] | 936 | * clearerr() calls. |
Denys Vlasenko | c60f446 | 2011-02-11 22:23:23 +0100 | [diff] [blame] | 937 | */ |
| 938 | ndelay_on(polldata.fd); |
| 939 | #endif |
Denys Vlasenko | a3aa3e3 | 2009-12-11 12:36:10 +0100 | [diff] [blame] | 940 | while (1) { |
Denys Vlasenko | 7f43280 | 2009-06-28 01:02:24 +0200 | [diff] [blame] | 941 | int n; |
Denys Vlasenko | a3aa3e3 | 2009-12-11 12:36:10 +0100 | [diff] [blame] | 942 | unsigned rdsz; |
Denys Vlasenko | 7f43280 | 2009-06-28 01:02:24 +0200 | [diff] [blame] | 943 | |
Bradley M. Kuhn | c97131c | 2010-08-08 02:51:20 +0200 | [diff] [blame] | 944 | #if ENABLE_FEATURE_WGET_STATUSBAR || ENABLE_FEATURE_WGET_TIMEOUT |
Denys Vlasenko | 8766a79 | 2011-02-11 21:42:00 +0100 | [diff] [blame] | 945 | /* fread internally uses read loop, which in our case |
| 946 | * is usually exited when we get EAGAIN. |
| 947 | * In this case, libc sets error marker on the stream. |
| 948 | * Need to clear it before next fread to avoid possible |
| 949 | * rare false positive ferror below. Rare because usually |
| 950 | * fread gets more than zero bytes, and we don't fall |
| 951 | * into if (n <= 0) ... |
| 952 | */ |
| 953 | clearerr(dfp); |
Denys Vlasenko | f9af375 | 2011-02-11 22:01:33 +0100 | [diff] [blame] | 954 | #endif |
Denys Vlasenko | b7812ce | 2012-09-03 12:49:30 +0200 | [diff] [blame] | 955 | errno = 0; |
| 956 | rdsz = sizeof(G.wget_buf); |
| 957 | if (G.got_clen) { |
| 958 | if (G.content_len < (off_t)sizeof(G.wget_buf)) { |
| 959 | if ((int)G.content_len <= 0) |
| 960 | break; |
| 961 | rdsz = (unsigned)G.content_len; |
| 962 | } |
| 963 | } |
Denys Vlasenko | 0fac2f7 | 2011-02-10 09:55:05 +0100 | [diff] [blame] | 964 | n = fread(G.wget_buf, 1, rdsz, dfp); |
Denys Vlasenko | b7812ce | 2012-09-03 12:49:30 +0200 | [diff] [blame] | 965 | |
| 966 | if (n > 0) { |
| 967 | xwrite(G.output_fd, G.wget_buf, n); |
| 968 | #if ENABLE_FEATURE_WGET_STATUSBAR |
| 969 | G.transferred += n; |
| 970 | #endif |
| 971 | if (G.got_clen) { |
| 972 | G.content_len -= n; |
| 973 | if (G.content_len == 0) |
| 974 | break; |
| 975 | } |
| 976 | #if ENABLE_FEATURE_WGET_TIMEOUT |
| 977 | second_cnt = G.timeout_seconds; |
| 978 | #endif |
Denys Vlasenko | faa9e94 | 2014-03-27 16:50:29 +0100 | [diff] [blame] | 979 | goto bump; |
Denys Vlasenko | b7812ce | 2012-09-03 12:49:30 +0200 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | /* n <= 0. |
| 983 | * man fread: |
Denys Vlasenko | 8766a79 | 2011-02-11 21:42:00 +0100 | [diff] [blame] | 984 | * If error occurs, or EOF is reached, the return value |
| 985 | * is a short item count (or zero). |
| 986 | * fread does not distinguish between EOF and error. |
| 987 | */ |
Denys Vlasenko | b7812ce | 2012-09-03 12:49:30 +0200 | [diff] [blame] | 988 | if (errno != EAGAIN) { |
| 989 | if (ferror(dfp)) { |
| 990 | progress_meter(PROGRESS_END); |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 991 | bb_simple_perror_msg_and_die(bb_msg_read_error); |
Denys Vlasenko | b7812ce | 2012-09-03 12:49:30 +0200 | [diff] [blame] | 992 | } |
Denys Vlasenko | 8766a79 | 2011-02-11 21:42:00 +0100 | [diff] [blame] | 993 | break; /* EOF, not error */ |
Denys Vlasenko | 7f43280 | 2009-06-28 01:02:24 +0200 | [diff] [blame] | 994 | } |
Denys Vlasenko | 8766a79 | 2011-02-11 21:42:00 +0100 | [diff] [blame] | 995 | |
Denys Vlasenko | b7812ce | 2012-09-03 12:49:30 +0200 | [diff] [blame] | 996 | #if ENABLE_FEATURE_WGET_STATUSBAR || ENABLE_FEATURE_WGET_TIMEOUT |
| 997 | /* It was EAGAIN. There is no data. Wait up to one second |
| 998 | * then abort if timed out, or update the bar and try reading again. |
| 999 | */ |
| 1000 | if (safe_poll(&polldata, 1, 1000) == 0) { |
| 1001 | # if ENABLE_FEATURE_WGET_TIMEOUT |
| 1002 | if (second_cnt != 0 && --second_cnt == 0) { |
| 1003 | progress_meter(PROGRESS_END); |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 1004 | bb_simple_error_msg_and_die("download timed out"); |
Denys Vlasenko | b7812ce | 2012-09-03 12:49:30 +0200 | [diff] [blame] | 1005 | } |
| 1006 | # endif |
| 1007 | /* We used to loop back to poll here, |
| 1008 | * but there is no great harm in letting fread |
| 1009 | * to try reading anyway. |
| 1010 | */ |
| 1011 | } |
Denys Vlasenko | faa9e94 | 2014-03-27 16:50:29 +0100 | [diff] [blame] | 1012 | #endif |
| 1013 | bump: |
Denys Vlasenko | b7812ce | 2012-09-03 12:49:30 +0200 | [diff] [blame] | 1014 | /* Need to do it _every_ second for "stalled" indicator |
| 1015 | * to be shown properly. |
| 1016 | */ |
Bradley M. Kuhn | c97131c | 2010-08-08 02:51:20 +0200 | [diff] [blame] | 1017 | progress_meter(PROGRESS_BUMP); |
Denys Vlasenko | b7812ce | 2012-09-03 12:49:30 +0200 | [diff] [blame] | 1018 | } /* while (reading data) */ |
| 1019 | |
Denys Vlasenko | c60f446 | 2011-02-11 22:23:23 +0100 | [diff] [blame] | 1020 | #if ENABLE_FEATURE_WGET_STATUSBAR || ENABLE_FEATURE_WGET_TIMEOUT |
| 1021 | clearerr(dfp); |
Denys Vlasenko | 88ad9da | 2011-02-11 23:06:21 +0100 | [diff] [blame] | 1022 | ndelay_off(polldata.fd); /* else fgets can get very unhappy */ |
Denys Vlasenko | c60f446 | 2011-02-11 22:23:23 +0100 | [diff] [blame] | 1023 | #endif |
Denys Vlasenko | 7f43280 | 2009-06-28 01:02:24 +0200 | [diff] [blame] | 1024 | if (!G.chunked) |
| 1025 | break; |
| 1026 | |
Denys Vlasenko | 8e2174e | 2018-04-08 18:06:24 +0200 | [diff] [blame] | 1027 | /* Each chunk ends with "\r\n" - eat it */ |
Denys Vlasenko | 3459024 | 2018-02-12 16:46:13 +0100 | [diff] [blame] | 1028 | fgets_trim_sanitize(dfp, NULL); |
Denys Vlasenko | 8e2174e | 2018-04-08 18:06:24 +0200 | [diff] [blame] | 1029 | get_clen: |
| 1030 | /* chunk size format is "HEXNUM[;name[=val]]\r\n" */ |
| 1031 | fgets_trim_sanitize(dfp, NULL); |
| 1032 | errno = 0; |
Denys Vlasenko | df4e16c | 2011-02-10 06:29:06 +0100 | [diff] [blame] | 1033 | G.content_len = STRTOOFF(G.wget_buf, NULL, 16); |
Denys Vlasenko | 8e2174e | 2018-04-08 18:06:24 +0200 | [diff] [blame] | 1034 | /* |
| 1035 | * Had a bug with inputs like "ffffffff0001f400" |
| 1036 | * smashing the heap later. Ensure >= 0. |
| 1037 | */ |
| 1038 | if (G.content_len < 0 || errno) |
| 1039 | bb_error_msg_and_die("bad chunk length '%s'", G.wget_buf); |
Denys Vlasenko | a3aa3e3 | 2009-12-11 12:36:10 +0100 | [diff] [blame] | 1040 | if (G.content_len == 0) |
Denys Vlasenko | 7f43280 | 2009-06-28 01:02:24 +0200 | [diff] [blame] | 1041 | break; /* all done! */ |
Denys Vlasenko | a3aa3e3 | 2009-12-11 12:36:10 +0100 | [diff] [blame] | 1042 | G.got_clen = 1; |
Denys Vlasenko | b7812ce | 2012-09-03 12:49:30 +0200 | [diff] [blame] | 1043 | /* |
| 1044 | * Note that fgets may result in some data being buffered in dfp. |
| 1045 | * We loop back to fread, which will retrieve this data. |
| 1046 | * Also note that code has to be arranged so that fread |
| 1047 | * is done _before_ one-second poll wait - poll doesn't know |
| 1048 | * about stdio buffering and can result in spurious one second waits! |
| 1049 | */ |
Denys Vlasenko | 7f43280 | 2009-06-28 01:02:24 +0200 | [diff] [blame] | 1050 | } |
| 1051 | |
Denys Vlasenko | 9b313dd | 2019-01-21 13:53:26 +0100 | [diff] [blame] | 1052 | /* Draw full bar and free its resources */ |
| 1053 | G.chunked = 0; /* makes it show 100% even for chunked download */ |
| 1054 | G.got_clen = 1; /* makes it show 100% even for download of (formerly) unknown size */ |
| 1055 | progress_meter(PROGRESS_END); |
| 1056 | if (G.content_len != 0) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 1057 | bb_simple_perror_msg_and_die("connection closed prematurely"); |
Denys Vlasenko | 9b313dd | 2019-01-21 13:53:26 +0100 | [diff] [blame] | 1058 | /* GNU wget says "DATE TIME (NN MB/s) - Connection closed at byte NNN. Retrying." */ |
| 1059 | } |
| 1060 | |
Denys Vlasenko | 6144124 | 2012-06-17 19:52:25 +0200 | [diff] [blame] | 1061 | /* If -c failed, we restart from the beginning, |
| 1062 | * but we do not truncate file then, we do it only now, at the end. |
| 1063 | * This lets user to ^C if his 99% complete 10 GB file download |
| 1064 | * failed to restart *without* losing the almost complete file. |
| 1065 | */ |
| 1066 | { |
| 1067 | off_t pos = lseek(G.output_fd, 0, SEEK_CUR); |
Denys Vlasenko | e7d853b | 2020-12-08 19:06:28 +0100 | [diff] [blame] | 1068 | if (pos != (off_t)-1) { |
| 1069 | /* do not truncate if -O- is in use, a user complained about |
| 1070 | * "wget -qO- 'http://example.com/empty' >>FILE" truncating FILE. |
| 1071 | */ |
| 1072 | if (!(option_mask32 & WGET_NO_FTRUNCATE)) |
| 1073 | ftruncate(G.output_fd, pos); |
| 1074 | } |
Denys Vlasenko | 6144124 | 2012-06-17 19:52:25 +0200 | [diff] [blame] | 1075 | } |
| 1076 | |
Martin Lewis | 94e748d | 2019-01-10 13:59:30 +0100 | [diff] [blame] | 1077 | if (!(option_mask32 & WGET_OPT_QUIET)) { |
| 1078 | if (G.output_fd == 1) |
| 1079 | fprintf(stderr, "written to stdout\n"); |
| 1080 | else |
| 1081 | fprintf(stderr, "'%s' saved\n", G.fname_out); |
| 1082 | } |
Denys Vlasenko | 7f43280 | 2009-06-28 01:02:24 +0200 | [diff] [blame] | 1083 | } |
| 1084 | |
Pere Orga | 5369563 | 2011-02-16 20:09:36 +0100 | [diff] [blame] | 1085 | static void download_one_url(const char *url) |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 1086 | { |
Denys Vlasenko | a366109 | 2011-02-13 02:33:11 +0100 | [diff] [blame] | 1087 | bool use_proxy; /* Use proxies if env vars are set */ |
Denys Vlasenko | f1fab09 | 2009-06-28 03:33:57 +0200 | [diff] [blame] | 1088 | int redir_limit; |
Denys Vlasenko | a366109 | 2011-02-13 02:33:11 +0100 | [diff] [blame] | 1089 | len_and_sockaddr *lsa; |
Denys Vlasenko | 7f43280 | 2009-06-28 01:02:24 +0200 | [diff] [blame] | 1090 | FILE *sfp; /* socket to web/ftp server */ |
Denis Vlasenko | a36535b | 2007-09-27 15:07:23 +0000 | [diff] [blame] | 1091 | FILE *dfp; /* socket to ftp server (data) */ |
Denys Vlasenko | a366109 | 2011-02-13 02:33:11 +0100 | [diff] [blame] | 1092 | char *fname_out_alloc; |
Denys Vlasenko | 93b4a60 | 2011-12-18 05:11:56 +0100 | [diff] [blame] | 1093 | char *redirected_path = NULL; |
Denys Vlasenko | a366109 | 2011-02-13 02:33:11 +0100 | [diff] [blame] | 1094 | struct host_info server; |
| 1095 | struct host_info target; |
Denis Vlasenko | 7710563 | 2007-09-24 15:04:00 +0000 | [diff] [blame] | 1096 | |
Denys Vlasenko | a366109 | 2011-02-13 02:33:11 +0100 | [diff] [blame] | 1097 | server.allocated = NULL; |
| 1098 | target.allocated = NULL; |
| 1099 | server.user = NULL; |
Vladimir Dronnikov | be168b1 | 2009-10-05 02:18:01 +0200 | [diff] [blame] | 1100 | target.user = NULL; |
Denys Vlasenko | a366109 | 2011-02-13 02:33:11 +0100 | [diff] [blame] | 1101 | |
| 1102 | parse_url(url, &target); |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 1103 | |
Bernhard Reutner-Fischer | 7e8a53a | 2007-04-10 09:37:29 +0000 | [diff] [blame] | 1104 | /* Use the proxy if necessary */ |
Denys Vlasenko | a366109 | 2011-02-13 02:33:11 +0100 | [diff] [blame] | 1105 | use_proxy = (strcmp(G.proxy_flag, "off") != 0); |
Glenn L McGrath | f1c4b11 | 2004-02-22 00:27:34 +0000 | [diff] [blame] | 1106 | if (use_proxy) { |
Peter Lloyd | 804ce5a | 2018-03-05 00:17:02 +0100 | [diff] [blame] | 1107 | char *proxy = getenv(target.protocol[0] == 'f' ? "ftp_proxy" : "http_proxy"); |
Denys Vlasenko | 8b7e8ae | 2014-02-22 14:12:29 +0100 | [diff] [blame] | 1108 | //FIXME: what if protocol is https? Ok to use http_proxy? |
Denys Vlasenko | 2384a35 | 2011-02-15 00:58:36 +0100 | [diff] [blame] | 1109 | use_proxy = (proxy && proxy[0]); |
| 1110 | if (use_proxy) |
Denis Vlasenko | 96e9d3c | 2006-10-07 14:28:55 +0000 | [diff] [blame] | 1111 | parse_url(proxy, &server); |
Robert Griebl | d776011 | 2002-05-14 23:36:45 +0000 | [diff] [blame] | 1112 | } |
Denys Vlasenko | 7d5ddf1 | 2009-06-30 20:36:27 +0200 | [diff] [blame] | 1113 | if (!use_proxy) { |
Peter Lloyd | 804ce5a | 2018-03-05 00:17:02 +0100 | [diff] [blame] | 1114 | server.protocol = target.protocol; |
Denys Vlasenko | 7d5ddf1 | 2009-06-30 20:36:27 +0200 | [diff] [blame] | 1115 | server.port = target.port; |
| 1116 | if (ENABLE_FEATURE_IPV6) { |
Denys Vlasenko | a366109 | 2011-02-13 02:33:11 +0100 | [diff] [blame] | 1117 | //free(server.allocated); - can't be non-NULL |
| 1118 | server.host = server.allocated = xstrdup(target.host); |
Denys Vlasenko | 7d5ddf1 | 2009-06-30 20:36:27 +0200 | [diff] [blame] | 1119 | } else { |
| 1120 | server.host = target.host; |
| 1121 | } |
| 1122 | } |
| 1123 | |
| 1124 | if (ENABLE_FEATURE_IPV6) |
| 1125 | strip_ipv6_scope_id(target.host); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1126 | |
Denys Vlasenko | a366109 | 2011-02-13 02:33:11 +0100 | [diff] [blame] | 1127 | /* If there was no -O FILE, guess output filename */ |
Denys Vlasenko | a366109 | 2011-02-13 02:33:11 +0100 | [diff] [blame] | 1128 | fname_out_alloc = NULL; |
Denys Vlasenko | 9a5b7f6 | 2011-02-13 02:49:43 +0100 | [diff] [blame] | 1129 | if (!(option_mask32 & WGET_OPT_OUTNAME)) { |
Denys Vlasenko | a366109 | 2011-02-13 02:33:11 +0100 | [diff] [blame] | 1130 | G.fname_out = bb_get_last_path_component_nostrip(target.path); |
Denis Vlasenko | 818322b | 2007-09-24 18:27:04 +0000 | [diff] [blame] | 1131 | /* handle "wget http://kernel.org//" */ |
Denys Vlasenko | a366109 | 2011-02-13 02:33:11 +0100 | [diff] [blame] | 1132 | if (G.fname_out[0] == '/' || !G.fname_out[0]) |
| 1133 | G.fname_out = (char*)"index.html"; |
Denis Vlasenko | 818322b | 2007-09-24 18:27:04 +0000 | [diff] [blame] | 1134 | /* -P DIR is considered only if there was no -O FILE */ |
Denys Vlasenko | aacd448 | 2012-06-17 20:21:30 +0200 | [diff] [blame] | 1135 | if (G.dir_prefix) |
| 1136 | G.fname_out = fname_out_alloc = concat_path_file(G.dir_prefix, G.fname_out); |
Denys Vlasenko | 625f218 | 2011-03-21 00:29:37 +0100 | [diff] [blame] | 1137 | else { |
Denys Vlasenko | aacd448 | 2012-06-17 20:21:30 +0200 | [diff] [blame] | 1138 | /* redirects may free target.path later, need to make a copy */ |
| 1139 | G.fname_out = fname_out_alloc = xstrdup(G.fname_out); |
Denys Vlasenko | 625f218 | 2011-03-21 00:29:37 +0100 | [diff] [blame] | 1140 | } |
Eric Andersen | 29edd00 | 2000-12-09 16:55:35 +0000 | [diff] [blame] | 1141 | } |
Denis Vlasenko | 818322b | 2007-09-24 18:27:04 +0000 | [diff] [blame] | 1142 | #if ENABLE_FEATURE_WGET_STATUSBAR |
Denys Vlasenko | a366109 | 2011-02-13 02:33:11 +0100 | [diff] [blame] | 1143 | G.curfile = bb_get_last_path_component_nostrip(G.fname_out); |
Denis Vlasenko | 818322b | 2007-09-24 18:27:04 +0000 | [diff] [blame] | 1144 | #endif |
| 1145 | |
Bernhard Reutner-Fischer | 7e8a53a | 2007-04-10 09:37:29 +0000 | [diff] [blame] | 1146 | /* Determine where to start transfer */ |
Denys Vlasenko | 2384a35 | 2011-02-15 00:58:36 +0100 | [diff] [blame] | 1147 | G.beg_range = 0; |
Denys Vlasenko | a366109 | 2011-02-13 02:33:11 +0100 | [diff] [blame] | 1148 | if (option_mask32 & WGET_OPT_CONTINUE) { |
Denys Vlasenko | 2384a35 | 2011-02-15 00:58:36 +0100 | [diff] [blame] | 1149 | G.output_fd = open(G.fname_out, O_WRONLY); |
| 1150 | if (G.output_fd >= 0) { |
| 1151 | G.beg_range = xlseek(G.output_fd, 0, SEEK_END); |
Denis Vlasenko | a94554d | 2006-09-23 17:49:09 +0000 | [diff] [blame] | 1152 | } |
| 1153 | /* File doesn't exist. We do not create file here yet. |
Denys Vlasenko | a84eadf | 2011-02-12 23:40:31 +0100 | [diff] [blame] | 1154 | * We are not sure it exists on remote side */ |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 1155 | } |
| 1156 | |
David Demelier | 4a9daf2 | 2019-08-29 14:05:27 +0200 | [diff] [blame] | 1157 | redir_limit = 16; |
Denys Vlasenko | f1fab09 | 2009-06-28 03:33:57 +0200 | [diff] [blame] | 1158 | resolve_lsa: |
Denis Vlasenko | 42823d5 | 2007-02-04 02:39:08 +0000 | [diff] [blame] | 1159 | lsa = xhost2sockaddr(server.host, server.port); |
Denys Vlasenko | a366109 | 2011-02-13 02:33:11 +0100 | [diff] [blame] | 1160 | if (!(option_mask32 & WGET_OPT_QUIET)) { |
Denys Vlasenko | f1fab09 | 2009-06-28 03:33:57 +0200 | [diff] [blame] | 1161 | char *s = xmalloc_sockaddr2dotted(&lsa->u.sa); |
| 1162 | fprintf(stderr, "Connecting to %s (%s)\n", server.host, s); |
| 1163 | free(s); |
Eric Andersen | e6dc439 | 2003-10-31 09:31:46 +0000 | [diff] [blame] | 1164 | } |
Denys Vlasenko | f1fab09 | 2009-06-28 03:33:57 +0200 | [diff] [blame] | 1165 | establish_session: |
Denys Vlasenko | 2384a35 | 2011-02-15 00:58:36 +0100 | [diff] [blame] | 1166 | /*G.content_len = 0; - redundant, got_clen = 0 is enough */ |
| 1167 | G.got_clen = 0; |
| 1168 | G.chunked = 0; |
Denys Vlasenko | 403f299 | 2018-02-06 15:15:08 +0100 | [diff] [blame] | 1169 | if (use_proxy || target.protocol[0] != 'f' /*not ftp[s]*/) { |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 1170 | /* |
| 1171 | * HTTP session |
| 1172 | */ |
Denys Vlasenko | f1fab09 | 2009-06-28 03:33:57 +0200 | [diff] [blame] | 1173 | char *str; |
Denys Vlasenko | 7f43280 | 2009-06-28 01:02:24 +0200 | [diff] [blame] | 1174 | int status; |
Denys Vlasenko | 7f43280 | 2009-06-28 01:02:24 +0200 | [diff] [blame] | 1175 | |
Denys Vlasenko | 8b7e8ae | 2014-02-22 14:12:29 +0100 | [diff] [blame] | 1176 | /* Open socket to http(s) server */ |
Denys Vlasenko | 1c6c670 | 2015-10-07 01:39:40 +0200 | [diff] [blame] | 1177 | #if ENABLE_FEATURE_WGET_OPENSSL |
Denys Vlasenko | 9a647c3 | 2017-01-23 01:08:16 +0100 | [diff] [blame] | 1178 | /* openssl (and maybe internal TLS) support is configured */ |
Peter Lloyd | 804ce5a | 2018-03-05 00:17:02 +0100 | [diff] [blame] | 1179 | if (server.protocol == P_HTTPS) { |
Denys Vlasenko | 1c6c670 | 2015-10-07 01:39:40 +0200 | [diff] [blame] | 1180 | /* openssl-based helper |
| 1181 | * Inconvenient API since we can't give it an open fd |
| 1182 | */ |
Denys Vlasenko | 2007ef5 | 2015-10-07 02:40:53 +0200 | [diff] [blame] | 1183 | int fd = spawn_https_helper_openssl(server.host, server.port); |
Denys Vlasenko | 9a647c3 | 2017-01-23 01:08:16 +0100 | [diff] [blame] | 1184 | # if ENABLE_FEATURE_WGET_HTTPS |
| 1185 | if (fd < 0) { /* no openssl? try internal */ |
Denys Vlasenko | 2007ef5 | 2015-10-07 02:40:53 +0200 | [diff] [blame] | 1186 | sfp = open_socket(lsa); |
Denys Vlasenko | 403f299 | 2018-02-06 15:15:08 +0100 | [diff] [blame] | 1187 | spawn_ssl_client(server.host, fileno(sfp), /*flags*/ 0); |
Denys Vlasenko | 2007ef5 | 2015-10-07 02:40:53 +0200 | [diff] [blame] | 1188 | goto socket_opened; |
| 1189 | } |
| 1190 | # else |
| 1191 | /* We don't check for exec("openssl") failure in this case */ |
| 1192 | # endif |
Denys Vlasenko | 8b7e8ae | 2014-02-22 14:12:29 +0100 | [diff] [blame] | 1193 | sfp = fdopen(fd, "r+"); |
| 1194 | if (!sfp) |
Denys Vlasenko | 899ae53 | 2018-04-01 19:59:37 +0200 | [diff] [blame] | 1195 | bb_die_memory_exhausted(); |
Denys Vlasenko | 2007ef5 | 2015-10-07 02:40:53 +0200 | [diff] [blame] | 1196 | goto socket_opened; |
| 1197 | } |
| 1198 | sfp = open_socket(lsa); |
| 1199 | socket_opened: |
Denys Vlasenko | 9a647c3 | 2017-01-23 01:08:16 +0100 | [diff] [blame] | 1200 | #elif ENABLE_FEATURE_WGET_HTTPS |
| 1201 | /* Only internal TLS support is configured */ |
Denys Vlasenko | 2007ef5 | 2015-10-07 02:40:53 +0200 | [diff] [blame] | 1202 | sfp = open_socket(lsa); |
Peter Lloyd | 804ce5a | 2018-03-05 00:17:02 +0100 | [diff] [blame] | 1203 | if (server.protocol == P_HTTPS) |
Denys Vlasenko | 403f299 | 2018-02-06 15:15:08 +0100 | [diff] [blame] | 1204 | spawn_ssl_client(server.host, fileno(sfp), /*flags*/ 0); |
Denys Vlasenko | 2007ef5 | 2015-10-07 02:40:53 +0200 | [diff] [blame] | 1205 | #else |
| 1206 | /* ssl (https) support is not configured */ |
| 1207 | sfp = open_socket(lsa); |
Denys Vlasenko | 5331557 | 2014-02-23 23:39:47 +0100 | [diff] [blame] | 1208 | #endif |
Denys Vlasenko | f1fab09 | 2009-06-28 03:33:57 +0200 | [diff] [blame] | 1209 | /* Send HTTP request */ |
| 1210 | if (use_proxy) { |
Bernhard Reutner-Fischer | d7bfee1 | 2015-02-18 20:41:02 +0100 | [diff] [blame] | 1211 | SENDFMT(sfp, "GET %s://%s/%s HTTP/1.1\r\n", |
Denys Vlasenko | 8b7e8ae | 2014-02-22 14:12:29 +0100 | [diff] [blame] | 1212 | target.protocol, target.host, |
Denys Vlasenko | f1fab09 | 2009-06-28 03:33:57 +0200 | [diff] [blame] | 1213 | target.path); |
| 1214 | } else { |
Bernhard Reutner-Fischer | d7bfee1 | 2015-02-18 20:41:02 +0100 | [diff] [blame] | 1215 | SENDFMT(sfp, "%s /%s HTTP/1.1\r\n", |
Denys Vlasenko | 8b7e8ae | 2014-02-22 14:12:29 +0100 | [diff] [blame] | 1216 | (option_mask32 & WGET_OPT_POST_DATA) ? "POST" : "GET", |
| 1217 | target.path); |
Denys Vlasenko | f1fab09 | 2009-06-28 03:33:57 +0200 | [diff] [blame] | 1218 | } |
Bernhard Reutner-Fischer | d7bfee1 | 2015-02-18 20:41:02 +0100 | [diff] [blame] | 1219 | if (!USR_HEADER_HOST) |
| 1220 | SENDFMT(sfp, "Host: %s\r\n", target.host); |
| 1221 | if (!USR_HEADER_USER_AGENT) |
| 1222 | SENDFMT(sfp, "User-Agent: %s\r\n", G.user_agent); |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 1223 | |
Denys Vlasenko | 9213a55 | 2011-02-10 13:23:45 +0100 | [diff] [blame] | 1224 | /* Ask server to close the connection as soon as we are done |
| 1225 | * (IOW: we do not intend to send more requests) |
| 1226 | */ |
Bernhard Reutner-Fischer | d7bfee1 | 2015-02-18 20:41:02 +0100 | [diff] [blame] | 1227 | SENDFMT(sfp, "Connection: close\r\n"); |
Denys Vlasenko | 9213a55 | 2011-02-10 13:23:45 +0100 | [diff] [blame] | 1228 | |
Denis Vlasenko | 9cade08 | 2006-11-21 10:43:02 +0000 | [diff] [blame] | 1229 | #if ENABLE_FEATURE_WGET_AUTHENTICATION |
Bernhard Reutner-Fischer | d7bfee1 | 2015-02-18 20:41:02 +0100 | [diff] [blame] | 1230 | if (target.user && !USR_HEADER_AUTH) { |
| 1231 | SENDFMT(sfp, "Proxy-Authorization: Basic %s\r\n"+6, |
Denys Vlasenko | df4e16c | 2011-02-10 06:29:06 +0100 | [diff] [blame] | 1232 | base64enc(target.user)); |
Denys Vlasenko | f1fab09 | 2009-06-28 03:33:57 +0200 | [diff] [blame] | 1233 | } |
Bernhard Reutner-Fischer | d7bfee1 | 2015-02-18 20:41:02 +0100 | [diff] [blame] | 1234 | if (use_proxy && server.user && !USR_HEADER_PROXY_AUTH) { |
| 1235 | SENDFMT(sfp, "Proxy-Authorization: Basic %s\r\n", |
Denys Vlasenko | df4e16c | 2011-02-10 06:29:06 +0100 | [diff] [blame] | 1236 | base64enc(server.user)); |
Denys Vlasenko | f1fab09 | 2009-06-28 03:33:57 +0200 | [diff] [blame] | 1237 | } |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 1238 | #endif |
| 1239 | |
Bernhard Reutner-Fischer | d7bfee1 | 2015-02-18 20:41:02 +0100 | [diff] [blame] | 1240 | if (G.beg_range != 0 && !USR_HEADER_RANGE) |
| 1241 | SENDFMT(sfp, "Range: bytes=%"OFF_FMT"u-\r\n", G.beg_range); |
Denys Vlasenko | 9213a55 | 2011-02-10 13:23:45 +0100 | [diff] [blame] | 1242 | |
Denis Vlasenko | c8400a2 | 2006-10-25 00:33:44 +0000 | [diff] [blame] | 1243 | #if ENABLE_FEATURE_WGET_LONG_OPTIONS |
Bernhard Reutner-Fischer | d7bfee1 | 2015-02-18 20:41:02 +0100 | [diff] [blame] | 1244 | if (G.extra_headers) { |
| 1245 | log_io(G.extra_headers); |
Denys Vlasenko | a366109 | 2011-02-13 02:33:11 +0100 | [diff] [blame] | 1246 | fputs(G.extra_headers, sfp); |
Bernhard Reutner-Fischer | d7bfee1 | 2015-02-18 20:41:02 +0100 | [diff] [blame] | 1247 | } |
Denis Vlasenko | 5a2ad69 | 2009-03-04 14:13:37 +0000 | [diff] [blame] | 1248 | |
Denys Vlasenko | a366109 | 2011-02-13 02:33:11 +0100 | [diff] [blame] | 1249 | if (option_mask32 & WGET_OPT_POST_DATA) { |
Bernhard Reutner-Fischer | d7bfee1 | 2015-02-18 20:41:02 +0100 | [diff] [blame] | 1250 | SENDFMT(sfp, |
Denys Vlasenko | 9213a55 | 2011-02-10 13:23:45 +0100 | [diff] [blame] | 1251 | "Content-Type: application/x-www-form-urlencoded\r\n" |
| 1252 | "Content-Length: %u\r\n" |
| 1253 | "\r\n" |
| 1254 | "%s", |
Vitaly Magerya | 700fbc3 | 2011-03-27 22:33:13 +0200 | [diff] [blame] | 1255 | (int) strlen(G.post_data), G.post_data |
Denys Vlasenko | 9213a55 | 2011-02-10 13:23:45 +0100 | [diff] [blame] | 1256 | ); |
Denys Vlasenko | f1fab09 | 2009-06-28 03:33:57 +0200 | [diff] [blame] | 1257 | } else |
Denis Vlasenko | c8400a2 | 2006-10-25 00:33:44 +0000 | [diff] [blame] | 1258 | #endif |
Denys Vlasenko | 9213a55 | 2011-02-10 13:23:45 +0100 | [diff] [blame] | 1259 | { |
Bernhard Reutner-Fischer | d7bfee1 | 2015-02-18 20:41:02 +0100 | [diff] [blame] | 1260 | SENDFMT(sfp, "\r\n"); |
Denys Vlasenko | f1fab09 | 2009-06-28 03:33:57 +0200 | [diff] [blame] | 1261 | } |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 1262 | |
Nguyễn Thái Ngọc Duy | ebec11d | 2010-09-23 15:18:41 +0200 | [diff] [blame] | 1263 | fflush(sfp); |
Denys Vlasenko | a6f8651 | 2017-01-11 20:16:45 +0100 | [diff] [blame] | 1264 | |
Denys Vlasenko | 4e08a12 | 2017-01-16 17:31:05 +0100 | [diff] [blame] | 1265 | /* Tried doing this unconditionally. |
| 1266 | * Cloudflare and nginx/1.11.5 are shocked to see SHUT_WR on non-HTTPS. |
| 1267 | */ |
Denys Vlasenko | a6f8651 | 2017-01-11 20:16:45 +0100 | [diff] [blame] | 1268 | #if SSL_SUPPORTED |
| 1269 | if (target.protocol == P_HTTPS) { |
| 1270 | /* If we use SSL helper, keeping our end of the socket open for writing |
| 1271 | * makes our end (i.e. the same fd!) readable (EAGAIN instead of EOF) |
| 1272 | * even after child closes its copy of the fd. |
| 1273 | * This helps: |
| 1274 | */ |
| 1275 | shutdown(fileno(sfp), SHUT_WR); |
| 1276 | } |
| 1277 | #endif |
Nguyễn Thái Ngọc Duy | ebec11d | 2010-09-23 15:18:41 +0200 | [diff] [blame] | 1278 | |
Denys Vlasenko | f1fab09 | 2009-06-28 03:33:57 +0200 | [diff] [blame] | 1279 | /* |
| 1280 | * Retrieve HTTP response line and check for "200" status code. |
| 1281 | */ |
Denis Vlasenko | 023b57d | 2006-10-15 17:05:55 +0000 | [diff] [blame] | 1282 | read_response: |
Denys Vlasenko | 3459024 | 2018-02-12 16:46:13 +0100 | [diff] [blame] | 1283 | fgets_trim_sanitize(sfp, " %s\n"); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1284 | |
Denys Vlasenko | df4e16c | 2011-02-10 06:29:06 +0100 | [diff] [blame] | 1285 | str = G.wget_buf; |
Denys Vlasenko | f1fab09 | 2009-06-28 03:33:57 +0200 | [diff] [blame] | 1286 | str = skip_non_whitespace(str); |
| 1287 | str = skip_whitespace(str); |
| 1288 | // FIXME: no error check |
| 1289 | // xatou wouldn't work: "200 OK" |
| 1290 | status = atoi(str); |
| 1291 | switch (status) { |
| 1292 | case 0: |
| 1293 | case 100: |
Denys Vlasenko | 3459024 | 2018-02-12 16:46:13 +0100 | [diff] [blame] | 1294 | while (get_sanitized_hdr(sfp) != NULL) |
Denys Vlasenko | f1fab09 | 2009-06-28 03:33:57 +0200 | [diff] [blame] | 1295 | /* eat all remaining headers */; |
| 1296 | goto read_response; |
Denys Vlasenko | 9ff910d | 2016-08-31 13:28:53 +0200 | [diff] [blame] | 1297 | |
| 1298 | /* Success responses */ |
Denys Vlasenko | f1fab09 | 2009-06-28 03:33:57 +0200 | [diff] [blame] | 1299 | case 200: |
Denys Vlasenko | 9ff910d | 2016-08-31 13:28:53 +0200 | [diff] [blame] | 1300 | /* fall through */ |
| 1301 | case 201: /* 201 Created */ |
| 1302 | /* "The request has been fulfilled and resulted in a new resource being created" */ |
Denys Vlasenko | ef15970 | 2016-09-01 11:16:22 +0200 | [diff] [blame] | 1303 | /* Standard wget is reported to treat this as success */ |
Denys Vlasenko | 9ff910d | 2016-08-31 13:28:53 +0200 | [diff] [blame] | 1304 | /* fall through */ |
| 1305 | case 202: /* 202 Accepted */ |
| 1306 | /* "The request has been accepted for processing, but the processing has not been completed" */ |
| 1307 | /* Treat as success: fall through */ |
| 1308 | case 203: /* 203 Non-Authoritative Information */ |
| 1309 | /* "Use of this response code is not required and is only appropriate when the response would otherwise be 200 (OK)" */ |
| 1310 | /* fall through */ |
| 1311 | case 204: /* 204 No Content */ |
Denis Vlasenko | 50b5cac | 2008-06-22 16:28:02 +0000 | [diff] [blame] | 1312 | /* |
| 1313 | Response 204 doesn't say "null file", it says "metadata |
| 1314 | has changed but data didn't": |
| 1315 | |
| 1316 | "10.2.5 204 No Content |
| 1317 | The server has fulfilled the request but does not need to return |
| 1318 | an entity-body, and might want to return updated metainformation. |
| 1319 | The response MAY include new or updated metainformation in the form |
| 1320 | of entity-headers, which if present SHOULD be associated with |
| 1321 | the requested variant. |
| 1322 | |
| 1323 | If the client is a user agent, it SHOULD NOT change its document |
| 1324 | view from that which caused the request to be sent. This response |
| 1325 | is primarily intended to allow input for actions to take place |
| 1326 | without causing a change to the user agent's active document view, |
| 1327 | although any new or updated metainformation SHOULD be applied |
| 1328 | to the document currently in the user agent's active view. |
| 1329 | |
| 1330 | The 204 response MUST NOT include a message-body, and thus |
| 1331 | is always terminated by the first empty line after the header fields." |
| 1332 | |
| 1333 | However, in real world it was observed that some web servers |
| 1334 | (e.g. Boa/0.94.14rc21) simply use code 204 when file size is zero. |
| 1335 | */ |
Denys Vlasenko | bf146b8 | 2012-06-13 17:31:07 +0200 | [diff] [blame] | 1336 | if (G.beg_range != 0) { |
| 1337 | /* "Range:..." was not honored by the server. |
| 1338 | * Restart download from the beginning. |
| 1339 | */ |
| 1340 | reset_beg_range_to_zero(); |
| 1341 | } |
Denys Vlasenko | f1fab09 | 2009-06-28 03:33:57 +0200 | [diff] [blame] | 1342 | break; |
Denys Vlasenko | 9ff910d | 2016-08-31 13:28:53 +0200 | [diff] [blame] | 1343 | /* 205 Reset Content ?? what to do on this ?? */ |
| 1344 | |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 1345 | case 300: /* redirection */ |
Denys Vlasenko | f1fab09 | 2009-06-28 03:33:57 +0200 | [diff] [blame] | 1346 | case 301: |
| 1347 | case 302: |
| 1348 | case 303: |
| 1349 | break; |
Denys Vlasenko | 9ff910d | 2016-08-31 13:28:53 +0200 | [diff] [blame] | 1350 | |
Vladimir Dronnikov | f5abc78 | 2012-06-13 17:29:41 +0200 | [diff] [blame] | 1351 | case 206: /* Partial Content */ |
| 1352 | if (G.beg_range != 0) |
| 1353 | /* "Range:..." worked. Good. */ |
Denis Vlasenko | 023b57d | 2006-10-15 17:05:55 +0000 | [diff] [blame] | 1354 | break; |
Vladimir Dronnikov | f5abc78 | 2012-06-13 17:29:41 +0200 | [diff] [blame] | 1355 | /* Partial Content even though we did not ask for it??? */ |
Denys Vlasenko | f1fab09 | 2009-06-28 03:33:57 +0200 | [diff] [blame] | 1356 | /* fall through */ |
| 1357 | default: |
Denys Vlasenko | 3459024 | 2018-02-12 16:46:13 +0100 | [diff] [blame] | 1358 | bb_error_msg_and_die("server returned error: %s", G.wget_buf); |
Denys Vlasenko | f1fab09 | 2009-06-28 03:33:57 +0200 | [diff] [blame] | 1359 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1360 | |
Denys Vlasenko | f1fab09 | 2009-06-28 03:33:57 +0200 | [diff] [blame] | 1361 | /* |
| 1362 | * Retrieve HTTP headers. |
| 1363 | */ |
Denys Vlasenko | 3459024 | 2018-02-12 16:46:13 +0100 | [diff] [blame] | 1364 | while ((str = get_sanitized_hdr(sfp)) != NULL) { |
Denys Vlasenko | a366109 | 2011-02-13 02:33:11 +0100 | [diff] [blame] | 1365 | static const char keywords[] ALIGN1 = |
| 1366 | "content-length\0""transfer-encoding\0""location\0"; |
| 1367 | enum { |
| 1368 | KEY_content_length = 1, KEY_transfer_encoding, KEY_location |
| 1369 | }; |
Matthijs van de Water | 0d58666 | 2009-08-22 20:19:48 +0200 | [diff] [blame] | 1370 | smalluint key; |
Denys Vlasenko | a366109 | 2011-02-13 02:33:11 +0100 | [diff] [blame] | 1371 | |
Denys Vlasenko | 3459024 | 2018-02-12 16:46:13 +0100 | [diff] [blame] | 1372 | /* get_sanitized_hdr converted "FOO:" string to lowercase */ |
Denys Vlasenko | a366109 | 2011-02-13 02:33:11 +0100 | [diff] [blame] | 1373 | |
Matthijs van de Water | 0d58666 | 2009-08-22 20:19:48 +0200 | [diff] [blame] | 1374 | /* strip trailing whitespace */ |
| 1375 | char *s = strchrnul(str, '\0') - 1; |
| 1376 | while (s >= str && (*s == ' ' || *s == '\t')) { |
| 1377 | *s = '\0'; |
| 1378 | s--; |
| 1379 | } |
Denys Vlasenko | df4e16c | 2011-02-10 06:29:06 +0100 | [diff] [blame] | 1380 | key = index_in_strings(keywords, G.wget_buf) + 1; |
Denys Vlasenko | f1fab09 | 2009-06-28 03:33:57 +0200 | [diff] [blame] | 1381 | if (key == KEY_content_length) { |
Denys Vlasenko | a3aa3e3 | 2009-12-11 12:36:10 +0100 | [diff] [blame] | 1382 | G.content_len = BB_STRTOOFF(str, NULL, 10); |
| 1383 | if (G.content_len < 0 || errno) { |
Denys Vlasenko | 3459024 | 2018-02-12 16:46:13 +0100 | [diff] [blame] | 1384 | bb_error_msg_and_die("content-length %s is garbage", str); |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 1385 | } |
Denys Vlasenko | f1fab09 | 2009-06-28 03:33:57 +0200 | [diff] [blame] | 1386 | G.got_clen = 1; |
| 1387 | continue; |
| 1388 | } |
| 1389 | if (key == KEY_transfer_encoding) { |
Denys Vlasenko | a366109 | 2011-02-13 02:33:11 +0100 | [diff] [blame] | 1390 | if (strcmp(str_tolower(str), "chunked") != 0) |
Denys Vlasenko | 3459024 | 2018-02-12 16:46:13 +0100 | [diff] [blame] | 1391 | bb_error_msg_and_die("transfer encoding '%s' is not supported", str); |
Denys Vlasenko | a366109 | 2011-02-13 02:33:11 +0100 | [diff] [blame] | 1392 | G.chunked = 1; |
Denys Vlasenko | f1fab09 | 2009-06-28 03:33:57 +0200 | [diff] [blame] | 1393 | } |
| 1394 | if (key == KEY_location && status >= 300) { |
| 1395 | if (--redir_limit == 0) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 1396 | bb_simple_error_msg_and_die("too many redirections"); |
Denys Vlasenko | f1fab09 | 2009-06-28 03:33:57 +0200 | [diff] [blame] | 1397 | fclose(sfp); |
Denys Vlasenko | a366109 | 2011-02-13 02:33:11 +0100 | [diff] [blame] | 1398 | if (str[0] == '/') { |
Denys Vlasenko | 93b4a60 | 2011-12-18 05:11:56 +0100 | [diff] [blame] | 1399 | free(redirected_path); |
Denys Vlasenko | 3459024 | 2018-02-12 16:46:13 +0100 | [diff] [blame] | 1400 | target.path = redirected_path = xstrdup(str + 1); |
Denys Vlasenko | f1fab09 | 2009-06-28 03:33:57 +0200 | [diff] [blame] | 1401 | /* lsa stays the same: it's on the same server */ |
Denys Vlasenko | a366109 | 2011-02-13 02:33:11 +0100 | [diff] [blame] | 1402 | } else { |
Denys Vlasenko | f1fab09 | 2009-06-28 03:33:57 +0200 | [diff] [blame] | 1403 | parse_url(str, &target); |
| 1404 | if (!use_proxy) { |
Denys Vlasenko | d353bff | 2014-02-03 14:09:42 +0100 | [diff] [blame] | 1405 | /* server.user remains untouched */ |
Denys Vlasenko | a366109 | 2011-02-13 02:33:11 +0100 | [diff] [blame] | 1406 | free(server.allocated); |
Pere Orga | 57b4909 | 2011-02-14 23:56:07 +0100 | [diff] [blame] | 1407 | server.allocated = NULL; |
Denys Vlasenko | 9634e8a | 2018-07-02 18:31:02 +0200 | [diff] [blame] | 1408 | server.protocol = target.protocol; |
Denys Vlasenko | f1fab09 | 2009-06-28 03:33:57 +0200 | [diff] [blame] | 1409 | server.host = target.host; |
Denys Vlasenko | 7d5ddf1 | 2009-06-30 20:36:27 +0200 | [diff] [blame] | 1410 | /* strip_ipv6_scope_id(target.host); - no! */ |
| 1411 | /* we assume remote never gives us IPv6 addr with scope id */ |
Denys Vlasenko | f1fab09 | 2009-06-28 03:33:57 +0200 | [diff] [blame] | 1412 | server.port = target.port; |
Denis Vlasenko | 6536a9b | 2007-01-12 10:35:23 +0000 | [diff] [blame] | 1413 | free(lsa); |
Denys Vlasenko | f1fab09 | 2009-06-28 03:33:57 +0200 | [diff] [blame] | 1414 | goto resolve_lsa; |
| 1415 | } /* else: lsa stays the same: we use proxy */ |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 1416 | } |
Denys Vlasenko | f1fab09 | 2009-06-28 03:33:57 +0200 | [diff] [blame] | 1417 | goto establish_session; |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 1418 | } |
Denys Vlasenko | f1fab09 | 2009-06-28 03:33:57 +0200 | [diff] [blame] | 1419 | } |
| 1420 | // if (status >= 300) |
| 1421 | // bb_error_msg_and_die("bad redirection (no Location: header from server)"); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1422 | |
Denys Vlasenko | f1fab09 | 2009-06-28 03:33:57 +0200 | [diff] [blame] | 1423 | /* For HTTP, data is pumped over the same connection */ |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 1424 | dfp = sfp; |
Denis Vlasenko | 96e9d3c | 2006-10-07 14:28:55 +0000 | [diff] [blame] | 1425 | } else { |
Sergey Ponomarev | b6e6c83 | 2021-01-17 20:35:08 +0200 | [diff] [blame] | 1426 | #if ENABLE_FEATURE_WGET_FTP |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 1427 | /* |
| 1428 | * FTP session |
| 1429 | */ |
Denys Vlasenko | 7f43280 | 2009-06-28 01:02:24 +0200 | [diff] [blame] | 1430 | sfp = prepare_ftp_session(&dfp, &target, lsa); |
Sergey Ponomarev | b6e6c83 | 2021-01-17 20:35:08 +0200 | [diff] [blame] | 1431 | #endif |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 1432 | } |
Denis Vlasenko | 7710563 | 2007-09-24 15:04:00 +0000 | [diff] [blame] | 1433 | |
Denys Vlasenko | a366109 | 2011-02-13 02:33:11 +0100 | [diff] [blame] | 1434 | free(lsa); |
Denys Vlasenko | a366109 | 2011-02-13 02:33:11 +0100 | [diff] [blame] | 1435 | |
Denys Vlasenko | 9a5b7f6 | 2011-02-13 02:49:43 +0100 | [diff] [blame] | 1436 | if (!(option_mask32 & WGET_OPT_SPIDER)) { |
Denys Vlasenko | 2384a35 | 2011-02-15 00:58:36 +0100 | [diff] [blame] | 1437 | if (G.output_fd < 0) |
| 1438 | G.output_fd = xopen(G.fname_out, G.o_flags); |
| 1439 | retrieve_file_data(dfp); |
| 1440 | if (!(option_mask32 & WGET_OPT_OUTNAME)) { |
| 1441 | xclose(G.output_fd); |
| 1442 | G.output_fd = -1; |
Denys Vlasenko | 9a5b7f6 | 2011-02-13 02:49:43 +0100 | [diff] [blame] | 1443 | } |
Martin Lewis | 46fc329 | 2019-01-04 18:26:04 +0100 | [diff] [blame] | 1444 | } else { |
Martin Lewis | 94e748d | 2019-01-10 13:59:30 +0100 | [diff] [blame] | 1445 | if (!(option_mask32 & WGET_OPT_QUIET)) |
| 1446 | fprintf(stderr, "remote file exists\n"); |
Bernhard Reutner-Fischer | 2e75dcc | 2007-04-05 10:31:47 +0000 | [diff] [blame] | 1447 | } |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 1448 | |
Sergey Ponomarev | b6e6c83 | 2021-01-17 20:35:08 +0200 | [diff] [blame] | 1449 | #if ENABLE_FEATURE_WGET_FTP |
Denys Vlasenko | f1fab09 | 2009-06-28 03:33:57 +0200 | [diff] [blame] | 1450 | if (dfp != sfp) { |
Denys Vlasenko | a366109 | 2011-02-13 02:33:11 +0100 | [diff] [blame] | 1451 | /* It's ftp. Close data connection properly */ |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 1452 | fclose(dfp); |
Denys Vlasenko | df4e16c | 2011-02-10 06:29:06 +0100 | [diff] [blame] | 1453 | if (ftpcmd(NULL, NULL, sfp) != 226) |
Denys Vlasenko | 3459024 | 2018-02-12 16:46:13 +0100 | [diff] [blame] | 1454 | bb_error_msg_and_die("ftp error: %s", G.wget_buf); |
Denys Vlasenko | df4e16c | 2011-02-10 06:29:06 +0100 | [diff] [blame] | 1455 | /* ftpcmd("QUIT", NULL, sfp); - why bother? */ |
Eric Andersen | 79757c9 | 2001-04-05 21:45:54 +0000 | [diff] [blame] | 1456 | } |
Sergey Ponomarev | b6e6c83 | 2021-01-17 20:35:08 +0200 | [diff] [blame] | 1457 | #endif |
Denys Vlasenko | a366109 | 2011-02-13 02:33:11 +0100 | [diff] [blame] | 1458 | fclose(sfp); |
Denis Vlasenko | 7710563 | 2007-09-24 15:04:00 +0000 | [diff] [blame] | 1459 | |
Denys Vlasenko | 9a5b7f6 | 2011-02-13 02:49:43 +0100 | [diff] [blame] | 1460 | free(server.allocated); |
| 1461 | free(target.allocated); |
Denys Vlasenko | d353bff | 2014-02-03 14:09:42 +0100 | [diff] [blame] | 1462 | free(server.user); |
| 1463 | free(target.user); |
Denys Vlasenko | 9a5b7f6 | 2011-02-13 02:49:43 +0100 | [diff] [blame] | 1464 | free(fname_out_alloc); |
Denys Vlasenko | 93b4a60 | 2011-12-18 05:11:56 +0100 | [diff] [blame] | 1465 | free(redirected_path); |
Eric Andersen | 9670083 | 2000-09-04 15:15:55 +0000 | [diff] [blame] | 1466 | } |
Denys Vlasenko | a366109 | 2011-02-13 02:33:11 +0100 | [diff] [blame] | 1467 | |
| 1468 | int wget_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 1469 | int wget_main(int argc UNUSED_PARAM, char **argv) |
| 1470 | { |
| 1471 | #if ENABLE_FEATURE_WGET_LONG_OPTIONS |
| 1472 | static const char wget_longopts[] ALIGN1 = |
| 1473 | /* name, has_arg, val */ |
| 1474 | "continue\0" No_argument "c" |
Denys Vlasenko | a366109 | 2011-02-13 02:33:11 +0100 | [diff] [blame] | 1475 | "quiet\0" No_argument "q" |
Denys Vlasenko | dff9fef | 2017-01-24 21:41:43 +0100 | [diff] [blame] | 1476 | "server-response\0" No_argument "S" |
Denys Vlasenko | a366109 | 2011-02-13 02:33:11 +0100 | [diff] [blame] | 1477 | "output-document\0" Required_argument "O" |
Martin Lewis | 64f3536 | 2018-12-26 16:28:45 +0100 | [diff] [blame] | 1478 | "output-file\0" Required_argument "o" |
Denys Vlasenko | a366109 | 2011-02-13 02:33:11 +0100 | [diff] [blame] | 1479 | "directory-prefix\0" Required_argument "P" |
| 1480 | "proxy\0" Required_argument "Y" |
| 1481 | "user-agent\0" Required_argument "U" |
Denys Vlasenko | 92e1b08 | 2015-10-20 21:51:52 +0200 | [diff] [blame] | 1482 | IF_FEATURE_WGET_TIMEOUT( |
| 1483 | "timeout\0" Required_argument "T") |
Denys Vlasenko | a366109 | 2011-02-13 02:33:11 +0100 | [diff] [blame] | 1484 | /* Ignored: */ |
Denys Vlasenko | 92e1b08 | 2015-10-20 21:51:52 +0200 | [diff] [blame] | 1485 | IF_DESKTOP( "tries\0" Required_argument "t") |
| 1486 | "header\0" Required_argument "\xff" |
| 1487 | "post-data\0" Required_argument "\xfe" |
Denys Vlasenko | 2972e2c | 2016-10-04 04:23:09 +0200 | [diff] [blame] | 1488 | "spider\0" No_argument "\xfd" |
Denys Vlasenko | 0972c7f | 2018-05-28 14:36:26 +0200 | [diff] [blame] | 1489 | "no-check-certificate\0" No_argument "\xfc" |
Denys Vlasenko | a366109 | 2011-02-13 02:33:11 +0100 | [diff] [blame] | 1490 | /* Ignored (we always use PASV): */ |
Denys Vlasenko | 92e1b08 | 2015-10-20 21:51:52 +0200 | [diff] [blame] | 1491 | IF_DESKTOP( "passive-ftp\0" No_argument "\xf0") |
Vladimir Dronnikov | f5abc78 | 2012-06-13 17:29:41 +0200 | [diff] [blame] | 1492 | /* Ignored (we don't support caching) */ |
Denys Vlasenko | 92e1b08 | 2015-10-20 21:51:52 +0200 | [diff] [blame] | 1493 | IF_DESKTOP( "no-cache\0" No_argument "\xf0") |
| 1494 | IF_DESKTOP( "no-verbose\0" No_argument "\xf0") |
| 1495 | IF_DESKTOP( "no-clobber\0" No_argument "\xf0") |
| 1496 | IF_DESKTOP( "no-host-directories\0" No_argument "\xf0") |
| 1497 | IF_DESKTOP( "no-parent\0" No_argument "\xf0") |
Denys Vlasenko | a366109 | 2011-02-13 02:33:11 +0100 | [diff] [blame] | 1498 | ; |
Denys Vlasenko | 036585a | 2017-08-08 16:38:18 +0200 | [diff] [blame] | 1499 | # define GETOPT32 getopt32long |
| 1500 | # define LONGOPTS ,wget_longopts |
| 1501 | #else |
| 1502 | # define GETOPT32 getopt32 |
| 1503 | # define LONGOPTS |
Denys Vlasenko | a366109 | 2011-02-13 02:33:11 +0100 | [diff] [blame] | 1504 | #endif |
| 1505 | |
Denys Vlasenko | a366109 | 2011-02-13 02:33:11 +0100 | [diff] [blame] | 1506 | #if ENABLE_FEATURE_WGET_LONG_OPTIONS |
| 1507 | llist_t *headers_llist = NULL; |
| 1508 | #endif |
| 1509 | |
| 1510 | INIT_G(); |
| 1511 | |
Lauri Kasanen | d074b41 | 2013-10-12 21:47:07 +0200 | [diff] [blame] | 1512 | #if ENABLE_FEATURE_WGET_TIMEOUT |
| 1513 | G.timeout_seconds = 900; |
| 1514 | signal(SIGALRM, alarm_handler); |
| 1515 | #endif |
Denys Vlasenko | a366109 | 2011-02-13 02:33:11 +0100 | [diff] [blame] | 1516 | G.proxy_flag = "on"; /* use proxies if env vars are set */ |
| 1517 | G.user_agent = "Wget"; /* "User-Agent" header field */ |
| 1518 | |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 1519 | GETOPT32(argv, "^" |
Martin Lewis | 64f3536 | 2018-12-26 16:28:45 +0100 | [diff] [blame] | 1520 | "cqSO:o:P:Y:U:T:+" |
Denys Vlasenko | 92e1b08 | 2015-10-20 21:51:52 +0200 | [diff] [blame] | 1521 | /*ignored:*/ "t:" |
| 1522 | /*ignored:*/ "n::" |
| 1523 | /* wget has exactly four -n<letter> opts, all of which we can ignore: |
| 1524 | * -nv --no-verbose: be moderately quiet (-q is full quiet) |
| 1525 | * -nc --no-clobber: abort if exists, neither download to FILE.n nor overwrite FILE |
| 1526 | * -nH --no-host-directories: wget -r http://host/ won't create host/ |
| 1527 | * -np --no-parent |
| 1528 | * "n::" above says that we accept -n[ARG]. |
| 1529 | * Specifying "n:" would be a bug: "-n ARG" would eat ARG! |
| 1530 | */ |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 1531 | "\0" |
| 1532 | "-1" /* at least one URL */ |
| 1533 | IF_FEATURE_WGET_LONG_OPTIONS(":\xff::") /* --header is a list */ |
Denys Vlasenko | 036585a | 2017-08-08 16:38:18 +0200 | [diff] [blame] | 1534 | LONGOPTS |
Martin Lewis | 64f3536 | 2018-12-26 16:28:45 +0100 | [diff] [blame] | 1535 | , &G.fname_out, &G.fname_log, &G.dir_prefix, |
Denys Vlasenko | a366109 | 2011-02-13 02:33:11 +0100 | [diff] [blame] | 1536 | &G.proxy_flag, &G.user_agent, |
| 1537 | IF_FEATURE_WGET_TIMEOUT(&G.timeout_seconds) IF_NOT_FEATURE_WGET_TIMEOUT(NULL), |
Denys Vlasenko | 92e1b08 | 2015-10-20 21:51:52 +0200 | [diff] [blame] | 1538 | NULL, /* -t RETRIES */ |
| 1539 | NULL /* -n[ARG] */ |
Denys Vlasenko | a366109 | 2011-02-13 02:33:11 +0100 | [diff] [blame] | 1540 | IF_FEATURE_WGET_LONG_OPTIONS(, &headers_llist) |
| 1541 | IF_FEATURE_WGET_LONG_OPTIONS(, &G.post_data) |
| 1542 | ); |
Denys Vlasenko | 2972e2c | 2016-10-04 04:23:09 +0200 | [diff] [blame] | 1543 | #if 0 /* option bits debug */ |
| 1544 | if (option_mask32 & WGET_OPT_RETRIES) bb_error_msg("-t NUM"); |
| 1545 | if (option_mask32 & WGET_OPT_nsomething) bb_error_msg("-nsomething"); |
| 1546 | if (option_mask32 & WGET_OPT_HEADER) bb_error_msg("--header"); |
| 1547 | if (option_mask32 & WGET_OPT_POST_DATA) bb_error_msg("--post-data"); |
| 1548 | if (option_mask32 & WGET_OPT_SPIDER) bb_error_msg("--spider"); |
Denys Vlasenko | 0972c7f | 2018-05-28 14:36:26 +0200 | [diff] [blame] | 1549 | if (option_mask32 & WGET_OPT_NO_CHECK_CERT) bb_error_msg("--no-check-certificate"); |
Denys Vlasenko | 2972e2c | 2016-10-04 04:23:09 +0200 | [diff] [blame] | 1550 | exit(0); |
| 1551 | #endif |
Denys Vlasenko | a366109 | 2011-02-13 02:33:11 +0100 | [diff] [blame] | 1552 | argv += optind; |
| 1553 | |
| 1554 | #if ENABLE_FEATURE_WGET_LONG_OPTIONS |
| 1555 | if (headers_llist) { |
Bernhard Reutner-Fischer | d7bfee1 | 2015-02-18 20:41:02 +0100 | [diff] [blame] | 1556 | int size = 0; |
| 1557 | char *hdr; |
Denys Vlasenko | a366109 | 2011-02-13 02:33:11 +0100 | [diff] [blame] | 1558 | llist_t *ll = headers_llist; |
| 1559 | while (ll) { |
| 1560 | size += strlen(ll->data) + 2; |
| 1561 | ll = ll->link; |
| 1562 | } |
Bernhard Reutner-Fischer | d7bfee1 | 2015-02-18 20:41:02 +0100 | [diff] [blame] | 1563 | G.extra_headers = hdr = xmalloc(size + 1); |
Denys Vlasenko | a366109 | 2011-02-13 02:33:11 +0100 | [diff] [blame] | 1564 | while (headers_llist) { |
Bernhard Reutner-Fischer | d7bfee1 | 2015-02-18 20:41:02 +0100 | [diff] [blame] | 1565 | int bit; |
| 1566 | const char *words; |
| 1567 | |
| 1568 | size = sprintf(hdr, "%s\r\n", |
| 1569 | (char*)llist_pop(&headers_llist)); |
| 1570 | /* a bit like index_in_substrings but don't match full key */ |
| 1571 | bit = 1; |
| 1572 | words = wget_user_headers; |
| 1573 | while (*words) { |
| 1574 | if (strstr(hdr, words) == hdr) { |
| 1575 | G.user_headers |= bit; |
| 1576 | break; |
| 1577 | } |
| 1578 | bit <<= 1; |
| 1579 | words += strlen(words) + 1; |
| 1580 | } |
| 1581 | hdr += size; |
Denys Vlasenko | a366109 | 2011-02-13 02:33:11 +0100 | [diff] [blame] | 1582 | } |
| 1583 | } |
| 1584 | #endif |
| 1585 | |
Denys Vlasenko | 2384a35 | 2011-02-15 00:58:36 +0100 | [diff] [blame] | 1586 | G.output_fd = -1; |
| 1587 | G.o_flags = O_WRONLY | O_CREAT | O_TRUNC | O_EXCL; |
| 1588 | if (G.fname_out) { /* -O FILE ? */ |
| 1589 | if (LONE_DASH(G.fname_out)) { /* -O - ? */ |
| 1590 | G.output_fd = 1; |
Denys Vlasenko | e7d853b | 2020-12-08 19:06:28 +0100 | [diff] [blame] | 1591 | option_mask32 = (option_mask32 & (~WGET_OPT_CONTINUE)) | WGET_NO_FTRUNCATE; |
Denys Vlasenko | 2384a35 | 2011-02-15 00:58:36 +0100 | [diff] [blame] | 1592 | } |
| 1593 | /* compat with wget: -O FILE can overwrite */ |
| 1594 | G.o_flags = O_WRONLY | O_CREAT | O_TRUNC; |
| 1595 | } |
| 1596 | |
Martin Lewis | 64f3536 | 2018-12-26 16:28:45 +0100 | [diff] [blame] | 1597 | G.log_fd = -1; |
| 1598 | if (G.fname_log) { /* -o FILE ? */ |
| 1599 | if (!LONE_DASH(G.fname_log)) { /* not -o - ? */ |
| 1600 | /* compat with wget: -o FILE can overwrite */ |
| 1601 | G.log_fd = xopen(G.fname_log, O_WRONLY | O_CREAT | O_TRUNC); |
| 1602 | /* Redirect only stderr to log file, so -O - will work */ |
| 1603 | xdup2(G.log_fd, STDERR_FILENO); |
| 1604 | } |
| 1605 | } |
| 1606 | |
Denys Vlasenko | a366109 | 2011-02-13 02:33:11 +0100 | [diff] [blame] | 1607 | while (*argv) |
Pere Orga | 5369563 | 2011-02-16 20:09:36 +0100 | [diff] [blame] | 1608 | download_one_url(*argv++); |
Denys Vlasenko | a366109 | 2011-02-13 02:33:11 +0100 | [diff] [blame] | 1609 | |
Denys Vlasenko | 28556b9 | 2011-02-15 11:03:53 +0100 | [diff] [blame] | 1610 | if (G.output_fd >= 0) |
| 1611 | xclose(G.output_fd); |
| 1612 | |
Martin Lewis | 64f3536 | 2018-12-26 16:28:45 +0100 | [diff] [blame] | 1613 | if (G.log_fd >= 0) |
| 1614 | xclose(G.log_fd); |
| 1615 | |
Guilherme Maciel Ferreira | 840ef17 | 2013-10-16 14:43:30 +0200 | [diff] [blame] | 1616 | #if ENABLE_FEATURE_CLEAN_UP && ENABLE_FEATURE_WGET_LONG_OPTIONS |
| 1617 | free(G.extra_headers); |
| 1618 | #endif |
| 1619 | FINI_G(); |
| 1620 | |
Pere Orga | 5369563 | 2011-02-16 20:09:36 +0100 | [diff] [blame] | 1621 | return EXIT_SUCCESS; |
Denys Vlasenko | a366109 | 2011-02-13 02:33:11 +0100 | [diff] [blame] | 1622 | } |