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 const char *post_data
;
40 static struct ustream_ssl_ctx
*ssl_ctx
;
41 static const struct ustream_ssl_ops
*ssl_ops
;
42 static int quiet
= false;
43 static bool verify
= true;
44 static bool default_certs
= false;
45 static bool no_output
;
46 static const char *output_file
;
47 static int output_fd
= -1;
50 static char *username
;
51 static char *password
;
52 static char *auth_str
;
57 static void request_done(struct uclient
*cl
);
59 static int open_output_file(const char *path
, bool create
)
66 flags
|= O_CREAT
| O_EXCL
;
69 if (!strcmp(output_file
, "-"))
73 fprintf(stderr
, "Writing to stdout\n");
76 return open(output_file
, flags
, 0644);
79 filename
= uclient_get_url_filename(path
, "index.html");
81 fprintf(stderr
, "Writing to '%s'\n", filename
);
82 ret
= open(filename
, flags
, 0644);
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
) {
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
)
129 if (!no_output
&& output_fd
< 0)
133 len
= uclient_read(cl
, buf
, sizeof(buf
));
139 write(output_fd
, buf
, len
);
143 static void msg_connecting(struct uclient
*cl
)
145 char addr
[INET6_ADDRSTRLEN
];
151 uclient_get_addr(addr
, &port
, &cl
->remote_addr
);
152 fprintf(stderr
, "Connecting to %s:%d\n", addr
, port
);
155 static int init_request(struct uclient
*cl
)
160 uclient_http_set_ssl_ctx(cl
, ssl_ops
, ssl_ctx
, verify
);
163 cl
->timeout_msecs
= timeout
* 1000;
165 rc
= uclient_connect(cl
);
171 rc
= uclient_http_set_request_type(cl
, post_data
? "POST" : "GET");
175 uclient_http_reset_headers(cl
);
176 uclient_http_set_header(cl
, "User-Agent", user_agent
);
179 uclient_http_set_header(cl
, "Content-Type", "application/x-www-form-urlencoded");
180 uclient_write(cl
, post_data
, strlen(post_data
));
183 rc
= uclient_request(cl
);
190 static void request_done(struct uclient
*cl
)
193 uclient_set_url(cl
, *urls
, auth_str
);
195 error_ret
= init_request(cl
);
200 if (output_fd
>= 0 && !output_file
) {
204 uclient_disconnect(cl
);
209 static void eof_cb(struct uclient
*cl
)
213 fprintf(stderr
, "Connection reset prematurely\n");
216 fprintf(stderr
, "Download completed (%d bytes)\n", out_bytes
);
221 static void handle_uclient_error(struct uclient
*cl
, int code
)
223 const char *type
= "Unknown error";
227 case UCLIENT_ERROR_CONNECT
:
228 type
= "Connection failed";
231 case UCLIENT_ERROR_TIMEDOUT
:
232 type
= "Connection timed out";
235 case UCLIENT_ERROR_SSL_INVALID_CERT
:
236 type
= "Invalid SSL certificate";
240 case UCLIENT_ERROR_SSL_CN_MISMATCH
:
241 type
= "Server hostname does not match SSL certificate";
251 fprintf(stderr
, "Connection error: %s%s\n", type
, ignore
? " (ignored)" : "");
259 static const struct uclient_cb cb
= {
260 .header_done
= header_done_cb
,
261 .data_read
= read_data_cb
,
263 .error
= handle_uclient_error
,
266 static int usage(const char *progname
)
269 "Usage: %s [options] <URL>\n"
271 " -q: Turn off status messages\n"
272 " -O <file>: Redirect output to file (use \"-\" for stdout)\n"
273 " --user=<user> HTTP authentication username\n"
274 " --password=<password> HTTP authentication password\n"
275 " --user-agent|-U <str> Set HTTP user agent\n"
276 " --post-data=STRING use the POST method; send STRING as the data\n"
277 " --spider|-s Spider mode - only check file existence\n"
278 " --timeout=N|-T N Set connect/request timeout to N seconds\n"
281 " --ca-certificate=<cert>: Load CA certificates from file <cert>\n"
282 " --no-check-certificate: don't validate the server's certificate\n"
287 static void init_ca_cert(void)
292 glob("/etc/ssl/certs/*.crt", 0, NULL
, &gl
);
293 for (i
= 0; i
< gl
.gl_pathc
; i
++)
294 ssl_ops
->context_add_ca_crt_file(ssl_ctx
, gl
.gl_pathv
[i
]);
297 static void init_ustream_ssl(void)
301 dlh
= dlopen("libustream-ssl." LIB_EXT
, RTLD_LAZY
| RTLD_LOCAL
);
305 ssl_ops
= dlsym(dlh
, "ustream_ssl_ops");
309 ssl_ctx
= ssl_ops
->context_new(false);
312 static int no_ssl(const char *progname
)
314 fprintf(stderr
, "%s: SSL support not available, please install ustream-ssl\n", progname
);
319 L_NO_CHECK_CERTIFICATE
,
329 static const struct option longopts
[] = {
330 [L_NO_CHECK_CERTIFICATE
] = { "no-check-certificate", no_argument
},
331 [L_CA_CERTIFICATE
] = { "ca-certificate", required_argument
},
332 [L_USER
] = { "user", required_argument
},
333 [L_PASSWORD
] = { "password", required_argument
},
334 [L_USER_AGENT
] = { "user-agent", required_argument
},
335 [L_POST_DATA
] = { "post-data", required_argument
},
336 [L_SPIDER
] = { "spider", no_argument
},
337 [L_TIMEOUT
] = { "timeout", required_argument
},
343 int main(int argc
, char **argv
)
345 const char *progname
= argv
[0];
348 bool has_cert
= false;
354 while ((ch
= getopt_long(argc
, argv
, "O:qsU:", longopts
, &longopt_idx
)) != -1) {
357 switch (longopt_idx
) {
358 case L_NO_CHECK_CERTIFICATE
:
361 case L_CA_CERTIFICATE
:
364 ssl_ops
->context_add_ca_crt_file(ssl_ctx
, optarg
);
369 username
= strdup(optarg
);
370 memset(optarg
, '*', strlen(optarg
));
375 password
= strdup(optarg
);
376 memset(optarg
, '*', strlen(optarg
));
388 timeout
= atoi(optarg
);
391 return usage(progname
);
398 output_file
= optarg
;
407 timeout
= atoi(optarg
);
410 return usage(progname
);
417 if (verify
&& !has_cert
)
418 default_certs
= true;
421 return usage(progname
);
424 for (i
= 0; i
< argc
; i
++) {
425 if (!strncmp(argv
[i
], "https", 5))
426 return no_ssl(progname
);
437 asprintf(&auth_str
, "%s:%s", username
, password
);
443 fprintf(stderr
, "Downloading '%s'\n", argv
[0]);
445 cl
= uclient_new(argv
[0], auth_str
, &cb
);
447 fprintf(stderr
, "Failed to allocate uclient context\n");
451 if (ssl_ctx
&& default_certs
)
454 rc
= init_request(cl
);
456 /* no error received, we can enter main loop */
459 fprintf(stderr
, "Failed to establish connection\n");
467 if (output_fd
>= 0 && output_fd
!= STDOUT_FILENO
)
471 ssl_ops
->context_free(ssl_ctx
);