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.
26 #include <libubox/blobmsg.h>
29 #include "uclient-utils.h"
32 #define LIB_EXT "dylib"
37 static struct ustream_ssl_ctx *ssl_ctx;
38 static const struct ustream_ssl_ops *ssl_ops;
39 static int quiet = false;
40 static bool verify = true;
41 static const char *output_file;
42 static int output_fd = -1;
45 static char *username;
46 static char *password;
47 static char *auth_str;
49 static int open_output_file(const char *path, bool create)
56 flags |= O_CREAT | O_EXCL;
59 if (!strcmp(output_file, "-"))
63 fprintf(stderr, "Writing to stdout\n");
66 return open(output_file, flags, 0644);
69 filename = uclient_get_url_filename(path, "index.html");
71 fprintf(stderr, "Writing to '%s'\n", filename);
72 ret = open(filename, flags, 0644);
78 static void request_done(struct uclient *cl)
80 if (output_fd >= 0 && !output_file) {
84 uclient_disconnect(cl);
88 static void header_done_cb(struct uclient *cl)
92 if (retries < 10 && uclient_http_redirect(cl)) {
94 fprintf(stderr, "Redirected to %s on %s\n", cl->url->location, cl->url->host);
101 switch (cl->status_code) {
104 output_fd = open_output_file(cl->url->location, true);
107 perror("Cannot open output file");
115 fprintf(stderr, "HTTP error %d\n", cl->status_code);
122 static void read_data_cb(struct uclient *cl)
131 len = uclient_read(cl, buf, sizeof(buf));
136 write(output_fd, buf, len);
140 static void msg_connecting(struct uclient *cl)
142 char addr[INET6_ADDRSTRLEN];
148 uclient_get_addr(addr, &port, &cl->remote_addr);
149 fprintf(stderr, "Connecting to %s:%d\n", addr, port);
152 static int init_request(struct uclient *cl)
158 rc = uclient_connect(cl);
164 rc = uclient_http_set_request_type(cl, "GET");
168 rc = uclient_request(cl);
175 static void eof_cb(struct uclient *cl)
179 fprintf(stderr, "Connection reset prematurely\n");
182 fprintf(stderr, "Download completed (%d bytes)\n", out_bytes);
187 static void handle_uclient_error(struct uclient *cl, int code)
189 const char *type = "Unknown error";
193 case UCLIENT_ERROR_CONNECT:
194 type = "Connection failed";
197 case UCLIENT_ERROR_TIMEDOUT:
198 type = "Connection timed out";
201 case UCLIENT_ERROR_SSL_INVALID_CERT:
202 type = "Invalid SSL certificate";
206 case UCLIENT_ERROR_SSL_CN_MISMATCH:
207 type = "Server hostname does not match SSL certificate";
217 fprintf(stderr, "Connection error: %s%s\n", type, ignore ? " (ignored)" : "");
225 static const struct uclient_cb cb = {
226 .header_done = header_done_cb,
227 .data_read = read_data_cb,
229 .error = handle_uclient_error,
232 static int usage(const char *progname)
235 "Usage: %s [options] <URL>\n"
237 " -q: Turn off status messages\n"
238 " -O <file>: Redirect output to file (use \"-\" for stdout)\n"
241 " --ca-certificate=<cert>: Load CA certificates from file <cert>\n"
242 " --no-check-certificate: don't validate the server's certificate\n"
248 static void init_ustream_ssl(void)
252 dlh = dlopen("libustream-ssl." LIB_EXT, RTLD_LAZY | RTLD_LOCAL);
256 ssl_ops = dlsym(dlh, "ustream_ssl_ops");
260 ssl_ctx = ssl_ops->context_new(false);
263 static int no_ssl(const char *progname)
265 fprintf(stderr, "%s: SSL support not available, please install ustream-ssl\n", progname);
270 L_NO_CHECK_CERTIFICATE,
276 static const struct option longopts[] = {
277 [L_NO_CHECK_CERTIFICATE] = { "no-check-certificate", no_argument },
278 [L_CA_CERTIFICATE] = { "ca-certificate", required_argument },
279 [L_USER] = { "user", required_argument },
280 [L_PASSWORD] = { "password", required_argument },
284 int main(int argc, char **argv)
286 const char *progname = argv[0];
294 while ((ch = getopt_long(argc, argv, "qO:", longopts, &longopt_idx)) != -1) {
297 switch (longopt_idx) {
298 case L_NO_CHECK_CERTIFICATE:
301 case L_CA_CERTIFICATE:
303 ssl_ops->context_add_ca_crt_file(ssl_ctx, optarg);
308 username = strdup(optarg);
309 memset(optarg, '*', strlen(optarg));
314 password = strdup(optarg);
315 memset(optarg, '*', strlen(optarg));
318 return usage(progname);
322 output_file = optarg;
328 return usage(progname);
336 return usage(progname);
338 if (!strncmp(argv[0], "https", 5) && !ssl_ctx)
339 return no_ssl(progname);
345 asprintf(&auth_str, "%s:%s", username, password);
351 fprintf(stderr, "Downloading '%s'\n", argv[0]);
353 cl = uclient_new(argv[0], auth_str, &cb);
355 fprintf(stderr, "Failed to allocate uclient context\n");
360 uclient_http_set_ssl_ctx(cl, ssl_ops, ssl_ctx, verify);
362 rc = init_request(cl);
364 /* no error received, we can enter main loop */
367 fprintf(stderr, "Failed to establish connection\n");
375 if (output_fd >= 0 && output_fd != STDOUT_FILENO)
379 ssl_ops->context_free(ssl_ctx);