2 * uclient - ustream based protocol client library
4 * Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
27 #include <libubox/blobmsg.h>
30 #include "uclient-utils.h"
33 #define LIB_EXT "dylib"
38 static const char *user_agent = "uclient-fetch";
39 static struct ustream_ssl_ctx *ssl_ctx;
40 static const struct ustream_ssl_ops *ssl_ops;
41 static int quiet = false;
42 static bool verify = true;
43 static bool default_certs = false;
44 static const char *output_file;
45 static int output_fd = -1;
48 static char *username;
49 static char *password;
50 static char *auth_str;
54 static void request_done(struct uclient *cl);
56 static int open_output_file(const char *path, bool create)
63 flags |= O_CREAT | O_EXCL;
66 if (!strcmp(output_file, "-"))
70 fprintf(stderr, "Writing to stdout\n");
73 return open(output_file, flags, 0644);
76 filename = uclient_get_url_filename(path, "index.html");
78 fprintf(stderr, "Writing to '%s'\n", filename);
79 ret = open(filename, flags, 0644);
85 static void header_done_cb(struct uclient *cl)
89 if (retries < 10 && uclient_http_redirect(cl)) {
91 fprintf(stderr, "Redirected to %s on %s\n", cl->url->location, cl->url->host);
98 switch (cl->status_code) {
101 output_fd = open_output_file(cl->url->location, true);
104 perror("Cannot open output file");
112 fprintf(stderr, "HTTP error %d\n", cl->status_code);
119 static void read_data_cb(struct uclient *cl)
128 len = uclient_read(cl, buf, sizeof(buf));
133 write(output_fd, buf, len);
137 static void msg_connecting(struct uclient *cl)
139 char addr[INET6_ADDRSTRLEN];
145 uclient_get_addr(addr, &port, &cl->remote_addr);
146 fprintf(stderr, "Connecting to %s:%d\n", addr, port);
149 static int init_request(struct uclient *cl)
154 uclient_http_set_ssl_ctx(cl, ssl_ops, ssl_ctx, verify);
156 rc = uclient_connect(cl);
162 rc = uclient_http_set_request_type(cl, "GET");
166 uclient_http_reset_headers(cl);
167 uclient_http_set_header(cl, "User-Agent", user_agent);
169 rc = uclient_request(cl);
176 static void request_done(struct uclient *cl)
179 uclient_set_url(cl, *urls, auth_str);
181 error_ret = init_request(cl);
186 if (output_fd >= 0 && !output_file) {
190 uclient_disconnect(cl);
195 static void eof_cb(struct uclient *cl)
199 fprintf(stderr, "Connection reset prematurely\n");
202 fprintf(stderr, "Download completed (%d bytes)\n", out_bytes);
207 static void handle_uclient_error(struct uclient *cl, int code)
209 const char *type = "Unknown error";
213 case UCLIENT_ERROR_CONNECT:
214 type = "Connection failed";
217 case UCLIENT_ERROR_TIMEDOUT:
218 type = "Connection timed out";
221 case UCLIENT_ERROR_SSL_INVALID_CERT:
222 type = "Invalid SSL certificate";
226 case UCLIENT_ERROR_SSL_CN_MISMATCH:
227 type = "Server hostname does not match SSL certificate";
237 fprintf(stderr, "Connection error: %s%s\n", type, ignore ? " (ignored)" : "");
245 static const struct uclient_cb cb = {
246 .header_done = header_done_cb,
247 .data_read = read_data_cb,
249 .error = handle_uclient_error,
252 static int usage(const char *progname)
255 "Usage: %s [options] <URL>\n"
257 " -q: Turn off status messages\n"
258 " -O <file>: Redirect output to file (use \"-\" for stdout)\n"
259 " --user=<user> HTTP authentication username\n"
260 " --password=<password> HTTP authentication password\n"
261 " --user-agent|-U <str> Set HTTP user agent\n"
264 " --ca-certificate=<cert>: Load CA certificates from file <cert>\n"
265 " --no-check-certificate: don't validate the server's certificate\n"
270 static void init_ca_cert(void)
275 glob("/etc/ssl/certs/*.crt", 0, NULL, &gl);
276 for (i = 0; i < gl.gl_pathc; i++)
277 ssl_ops->context_add_ca_crt_file(ssl_ctx, gl.gl_pathv[i]);
280 static void init_ustream_ssl(void)
284 dlh = dlopen("libustream-ssl." LIB_EXT, RTLD_LAZY | RTLD_LOCAL);
288 ssl_ops = dlsym(dlh, "ustream_ssl_ops");
292 ssl_ctx = ssl_ops->context_new(false);
295 static int no_ssl(const char *progname)
297 fprintf(stderr, "%s: SSL support not available, please install ustream-ssl\n", progname);
302 L_NO_CHECK_CERTIFICATE,
309 static const struct option longopts[] = {
310 [L_NO_CHECK_CERTIFICATE] = { "no-check-certificate", no_argument },
311 [L_CA_CERTIFICATE] = { "ca-certificate", required_argument },
312 [L_USER] = { "user", required_argument },
313 [L_PASSWORD] = { "password", required_argument },
314 [L_USER_AGENT] = { "user-agent", required_argument },
320 int main(int argc, char **argv)
322 const char *progname = argv[0];
325 bool has_cert = false;
331 while ((ch = getopt_long(argc, argv, "qO:U:", longopts, &longopt_idx)) != -1) {
334 switch (longopt_idx) {
335 case L_NO_CHECK_CERTIFICATE:
338 case L_CA_CERTIFICATE:
341 ssl_ops->context_add_ca_crt_file(ssl_ctx, optarg);
346 username = strdup(optarg);
347 memset(optarg, '*', strlen(optarg));
352 password = strdup(optarg);
353 memset(optarg, '*', strlen(optarg));
359 return usage(progname);
366 output_file = optarg;
372 return usage(progname);
379 if (verify && !has_cert)
380 default_certs = true;
383 return usage(progname);
386 for (i = 0; i < argc; i++) {
387 if (!strncmp(argv[i], "https", 5))
388 return no_ssl(progname);
399 asprintf(&auth_str, "%s:%s", username, password);
405 fprintf(stderr, "Downloading '%s'\n", argv[0]);
407 cl = uclient_new(argv[0], auth_str, &cb);
409 fprintf(stderr, "Failed to allocate uclient context\n");
416 rc = init_request(cl);
418 /* no error received, we can enter main loop */
421 fprintf(stderr, "Failed to establish connection\n");
429 if (output_fd >= 0 && output_fd != STDOUT_FILENO)
433 ssl_ops->context_free(ssl_ctx);