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 struct ustream_ssl_ctx
*ssl_ctx
;
39 static const struct ustream_ssl_ops
*ssl_ops
;
40 static int quiet
= false;
41 static bool verify
= true;
42 static bool default_certs
= false;
43 static const char *output_file
;
44 static int output_fd
= -1;
47 static char *username
;
48 static char *password
;
49 static char *auth_str
;
51 static int open_output_file(const char *path
, bool create
)
58 flags
|= O_CREAT
| O_EXCL
;
61 if (!strcmp(output_file
, "-"))
65 fprintf(stderr
, "Writing to stdout\n");
68 return open(output_file
, flags
, 0644);
71 filename
= uclient_get_url_filename(path
, "index.html");
73 fprintf(stderr
, "Writing to '%s'\n", filename
);
74 ret
= open(filename
, flags
, 0644);
80 static void request_done(struct uclient
*cl
)
82 if (output_fd
>= 0 && !output_file
) {
86 uclient_disconnect(cl
);
90 static void header_done_cb(struct uclient
*cl
)
94 if (retries
< 10 && uclient_http_redirect(cl
)) {
96 fprintf(stderr
, "Redirected to %s on %s\n", cl
->url
->location
, cl
->url
->host
);
103 switch (cl
->status_code
) {
106 output_fd
= open_output_file(cl
->url
->location
, true);
109 perror("Cannot open output file");
117 fprintf(stderr
, "HTTP error %d\n", cl
->status_code
);
124 static void read_data_cb(struct uclient
*cl
)
133 len
= uclient_read(cl
, buf
, sizeof(buf
));
138 write(output_fd
, buf
, len
);
142 static void msg_connecting(struct uclient
*cl
)
144 char addr
[INET6_ADDRSTRLEN
];
150 uclient_get_addr(addr
, &port
, &cl
->remote_addr
);
151 fprintf(stderr
, "Connecting to %s:%d\n", addr
, port
);
154 static int init_request(struct uclient
*cl
)
160 rc
= uclient_connect(cl
);
166 rc
= uclient_http_set_request_type(cl
, "GET");
170 rc
= uclient_request(cl
);
177 static void eof_cb(struct uclient
*cl
)
181 fprintf(stderr
, "Connection reset prematurely\n");
184 fprintf(stderr
, "Download completed (%d bytes)\n", out_bytes
);
189 static void handle_uclient_error(struct uclient
*cl
, int code
)
191 const char *type
= "Unknown error";
195 case UCLIENT_ERROR_CONNECT
:
196 type
= "Connection failed";
199 case UCLIENT_ERROR_TIMEDOUT
:
200 type
= "Connection timed out";
203 case UCLIENT_ERROR_SSL_INVALID_CERT
:
204 type
= "Invalid SSL certificate";
208 case UCLIENT_ERROR_SSL_CN_MISMATCH
:
209 type
= "Server hostname does not match SSL certificate";
219 fprintf(stderr
, "Connection error: %s%s\n", type
, ignore
? " (ignored)" : "");
227 static const struct uclient_cb cb
= {
228 .header_done
= header_done_cb
,
229 .data_read
= read_data_cb
,
231 .error
= handle_uclient_error
,
234 static int usage(const char *progname
)
237 "Usage: %s [options] <URL>\n"
239 " -q: Turn off status messages\n"
240 " -O <file>: Redirect output to file (use \"-\" for stdout)\n"
241 " --user=<user> HTTP authentication username\n"
242 " --password=<password> HTTP authentication password\n"
245 " --ca-certificate=<cert>: Load CA certificates from file <cert>\n"
246 " --no-check-certificate: don't validate the server's certificate\n"
251 static void init_ca_cert(void)
256 glob("/etc/ssl/certs/*.crt", 0, NULL
, &gl
);
257 for (i
= 0; i
< gl
.gl_pathc
; i
++)
258 ssl_ops
->context_add_ca_crt_file(ssl_ctx
, gl
.gl_pathv
[i
]);
261 static void init_ustream_ssl(void)
265 dlh
= dlopen("libustream-ssl." LIB_EXT
, RTLD_LAZY
| RTLD_LOCAL
);
269 ssl_ops
= dlsym(dlh
, "ustream_ssl_ops");
273 ssl_ctx
= ssl_ops
->context_new(false);
276 static int no_ssl(const char *progname
)
278 fprintf(stderr
, "%s: SSL support not available, please install ustream-ssl\n", progname
);
283 L_NO_CHECK_CERTIFICATE
,
289 static const struct option longopts
[] = {
290 [L_NO_CHECK_CERTIFICATE
] = { "no-check-certificate", no_argument
},
291 [L_CA_CERTIFICATE
] = { "ca-certificate", required_argument
},
292 [L_USER
] = { "user", required_argument
},
293 [L_PASSWORD
] = { "password", required_argument
},
297 int main(int argc
, char **argv
)
299 const char *progname
= argv
[0];
303 bool has_cert
= false;
308 while ((ch
= getopt_long(argc
, argv
, "qO:", longopts
, &longopt_idx
)) != -1) {
311 switch (longopt_idx
) {
312 case L_NO_CHECK_CERTIFICATE
:
315 case L_CA_CERTIFICATE
:
318 ssl_ops
->context_add_ca_crt_file(ssl_ctx
, optarg
);
323 username
= strdup(optarg
);
324 memset(optarg
, '*', strlen(optarg
));
329 password
= strdup(optarg
);
330 memset(optarg
, '*', strlen(optarg
));
333 return usage(progname
);
337 output_file
= optarg
;
343 return usage(progname
);
350 if (verify
&& !has_cert
)
351 default_certs
= true;
354 return usage(progname
);
356 if (!strncmp(argv
[0], "https", 5) && !ssl_ctx
)
357 return no_ssl(progname
);
363 asprintf(&auth_str
, "%s:%s", username
, password
);
369 fprintf(stderr
, "Downloading '%s'\n", argv
[0]);
371 cl
= uclient_new(argv
[0], auth_str
, &cb
);
373 fprintf(stderr
, "Failed to allocate uclient context\n");
379 uclient_http_set_ssl_ctx(cl
, ssl_ops
, ssl_ctx
, verify
);
382 rc
= init_request(cl
);
384 /* no error received, we can enter main loop */
387 fprintf(stderr
, "Failed to establish connection\n");
395 if (output_fd
>= 0 && output_fd
!= STDOUT_FILENO
)
399 ssl_ops
->context_free(ssl_ctx
);