c64afb7e0b2b10ded49c967547af69a3c7f5343f
[project/uclient.git] / uclient-fetch.c
1 /*
2 * uclient - ustream based protocol client library
3 *
4 * Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
5 *
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.
9 *
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.
17 */
18
19 #define _GNU_SOURCE
20 #include <sys/stat.h>
21 #include <unistd.h>
22 #include <stdio.h>
23 #include <dlfcn.h>
24 #include <getopt.h>
25 #include <fcntl.h>
26 #include <glob.h>
27 #include <stdint.h>
28 #include <inttypes.h>
29
30 #include <libubox/blobmsg.h>
31
32 #include "uclient.h"
33 #include "uclient-utils.h"
34
35 #ifdef __APPLE__
36 #define LIB_EXT "dylib"
37 #else
38 #define LIB_EXT "so"
39 #endif
40
41 static const char *user_agent = "uclient-fetch";
42 static const char *post_data;
43 static struct ustream_ssl_ctx *ssl_ctx;
44 static const struct ustream_ssl_ops *ssl_ops;
45 static int quiet = false;
46 static bool verify = true;
47 static bool proxy = true;
48 static bool default_certs = false;
49 static bool no_output;
50 static const char *output_file;
51 static int output_fd = -1;
52 static int error_ret;
53 static int out_bytes;
54 static char *auth_str;
55 static char **urls;
56 static int n_urls;
57 static int timeout;
58 static bool resume, cur_resume;
59
60 static int init_request(struct uclient *cl);
61 static void request_done(struct uclient *cl);
62
63 static const char *
64 get_proxy_url(char *url)
65 {
66 char prefix[16];
67 char *sep;
68
69 if (!proxy)
70 return NULL;
71
72 sep = strchr(url, ':');
73 if (!sep)
74 return NULL;
75
76 if (sep - url > 5)
77 return NULL;
78
79 memcpy(prefix, url, sep - url);
80 strcpy(prefix + (sep - url), "_proxy");
81 return getenv(prefix);
82 }
83
84 static int open_output_file(const char *path, uint64_t resume_offset)
85 {
86 char *filename = NULL;
87 int flags;
88 int ret;
89
90 if (cur_resume)
91 flags = O_RDWR;
92 else
93 flags = O_WRONLY | O_EXCL;
94
95 flags |= O_CREAT;
96
97 if (output_file) {
98 if (!strcmp(output_file, "-")) {
99 if (!quiet)
100 fprintf(stderr, "Writing to stdout\n");
101
102 return STDOUT_FILENO;
103 }
104 } else {
105 filename = uclient_get_url_filename(path, "index.html");
106 output_file = filename;
107 }
108
109 if (!quiet)
110 fprintf(stderr, "Writing to '%s'\n", output_file);
111 ret = open(output_file, flags, 0644);
112 free(filename);
113
114 if (ret < 0)
115 return ret;
116
117 if (resume_offset &&
118 lseek(ret, resume_offset, SEEK_SET) < 0) {
119 if (!quiet)
120 fprintf(stderr, "Failed to seek %"PRIu64" bytes in output file\n", resume_offset);
121 close(ret);
122 return -1;
123 }
124
125 out_bytes += resume_offset;
126
127 return ret;
128 }
129
130 static void header_done_cb(struct uclient *cl)
131 {
132 static const struct blobmsg_policy policy = {
133 .name = "content-range",
134 .type = BLOBMSG_TYPE_STRING
135 };
136 struct blob_attr *attr;
137 uint64_t resume_offset = 0, resume_end, resume_size;
138 static int retries;
139
140 if (retries < 10 && uclient_http_redirect(cl)) {
141 if (!quiet)
142 fprintf(stderr, "Redirected to %s on %s\n", cl->url->location, cl->url->host);
143
144 retries++;
145 return;
146 }
147
148 if (cl->status_code == 204 && cur_resume) {
149 /* Resume attempt failed, try normal download */
150 cur_resume = false;
151 init_request(cl);
152 return;
153 }
154
155 switch (cl->status_code) {
156 case 416:
157 if (!quiet)
158 fprintf(stderr, "File download already fully retrieved; nothing to do.\n");
159 request_done(cl);
160 break;
161 case 206:
162 if (!cur_resume) {
163 if (!quiet)
164 fprintf(stderr, "Error: Partial content received, full content requested\n");
165 error_ret = 8;
166 request_done(cl);
167 break;
168 }
169
170 blobmsg_parse(&policy, 1, &attr, blob_data(cl->meta), blob_len(cl->meta));
171 if (!attr) {
172 if (!quiet)
173 fprintf(stderr, "Content-Range header is missing\n");
174 error_ret = 8;
175 break;
176 }
177
178 if (sscanf(blobmsg_get_string(attr), "bytes %"PRIu64"-%"PRIu64"/%"PRIu64,
179 &resume_offset, &resume_end, &resume_size) != 3) {
180 if (!quiet)
181 fprintf(stderr, "Content-Range header is invalid\n");
182 error_ret = 8;
183 break;
184 }
185 case 204:
186 case 200:
187 if (no_output)
188 break;
189 output_fd = open_output_file(cl->url->location, resume_offset);
190 if (output_fd < 0) {
191 if (!quiet)
192 perror("Cannot open output file");
193 error_ret = 3;
194 request_done(cl);
195 }
196 break;
197
198 default:
199 if (!quiet)
200 fprintf(stderr, "HTTP error %d\n", cl->status_code);
201 request_done(cl);
202 error_ret = 8;
203 break;
204 }
205 }
206
207 static void read_data_cb(struct uclient *cl)
208 {
209 char buf[256];
210 int len;
211
212 if (!no_output && output_fd < 0)
213 return;
214
215 while (1) {
216 len = uclient_read(cl, buf, sizeof(buf));
217 if (!len)
218 return;
219
220 out_bytes += len;
221 if (!no_output)
222 write(output_fd, buf, len);
223 }
224 }
225
226 static void msg_connecting(struct uclient *cl)
227 {
228 char addr[INET6_ADDRSTRLEN];
229 int port;
230
231 if (quiet)
232 return;
233
234 uclient_get_addr(addr, &port, &cl->remote_addr);
235 fprintf(stderr, "Connecting to %s:%d\n", addr, port);
236 }
237
238 static void check_resume_offset(struct uclient *cl)
239 {
240 char range_str[64];
241 struct stat st;
242 char *file;
243 int ret;
244
245 file = uclient_get_url_filename(cl->url->location, "index.html");
246 if (!file)
247 return;
248
249 ret = stat(file, &st);
250 free(file);
251 if (ret)
252 return;
253
254 if (!st.st_size)
255 return;
256
257 snprintf(range_str, sizeof(range_str), "bytes=%"PRIu64"-", (uint64_t) st.st_size);
258 uclient_http_set_header(cl, "Range", range_str);
259 }
260
261 static int init_request(struct uclient *cl)
262 {
263 int rc;
264
265 out_bytes = 0;
266 uclient_http_set_ssl_ctx(cl, ssl_ops, ssl_ctx, verify);
267
268 if (timeout)
269 cl->timeout_msecs = timeout * 1000;
270
271 rc = uclient_connect(cl);
272 if (rc)
273 return rc;
274
275 msg_connecting(cl);
276
277 rc = uclient_http_set_request_type(cl, post_data ? "POST" : "GET");
278 if (rc)
279 return rc;
280
281 uclient_http_reset_headers(cl);
282 uclient_http_set_header(cl, "User-Agent", user_agent);
283 if (cur_resume)
284 check_resume_offset(cl);
285
286 if (post_data) {
287 uclient_http_set_header(cl, "Content-Type", "application/x-www-form-urlencoded");
288 uclient_write(cl, post_data, strlen(post_data));
289 }
290
291 rc = uclient_request(cl);
292 if (rc)
293 return rc;
294
295 return 0;
296 }
297
298 static void request_done(struct uclient *cl)
299 {
300 const char *proxy_url;
301
302 if (n_urls) {
303 proxy_url = get_proxy_url(*urls);
304 if (proxy_url) {
305 uclient_set_url(cl, proxy_url, NULL);
306 uclient_set_proxy_url(cl, *urls, auth_str);
307 } else {
308 uclient_set_url(cl, *urls, auth_str);
309 }
310 n_urls--;
311 cur_resume = resume;
312 error_ret = init_request(cl);
313 if (error_ret == 0)
314 return;
315 }
316
317 if (output_fd >= 0 && !output_file) {
318 close(output_fd);
319 output_fd = -1;
320 }
321 uclient_disconnect(cl);
322 uloop_end();
323 }
324
325
326 static void eof_cb(struct uclient *cl)
327 {
328 if (!cl->data_eof) {
329 if (!quiet)
330 fprintf(stderr, "Connection reset prematurely\n");
331 error_ret = 4;
332 } else if (!quiet) {
333 fprintf(stderr, "Download completed (%d bytes)\n", out_bytes);
334 }
335 request_done(cl);
336 }
337
338 static void handle_uclient_error(struct uclient *cl, int code)
339 {
340 const char *type = "Unknown error";
341 bool ignore = false;
342
343 switch(code) {
344 case UCLIENT_ERROR_CONNECT:
345 type = "Connection failed";
346 error_ret = 4;
347 break;
348 case UCLIENT_ERROR_TIMEDOUT:
349 type = "Connection timed out";
350 error_ret = 4;
351 break;
352 case UCLIENT_ERROR_SSL_INVALID_CERT:
353 type = "Invalid SSL certificate";
354 ignore = !verify;
355 error_ret = 5;
356 break;
357 case UCLIENT_ERROR_SSL_CN_MISMATCH:
358 type = "Server hostname does not match SSL certificate";
359 ignore = !verify;
360 error_ret = 5;
361 break;
362 default:
363 error_ret = 1;
364 break;
365 }
366
367 if (!quiet)
368 fprintf(stderr, "Connection error: %s%s\n", type, ignore ? " (ignored)" : "");
369
370 if (ignore)
371 error_ret = 0;
372 else
373 request_done(cl);
374 }
375
376 static const struct uclient_cb cb = {
377 .header_done = header_done_cb,
378 .data_read = read_data_cb,
379 .data_eof = eof_cb,
380 .error = handle_uclient_error,
381 };
382
383 static int usage(const char *progname)
384 {
385 fprintf(stderr,
386 "Usage: %s [options] <URL>\n"
387 "Options:\n"
388 " -q: Turn off status messages\n"
389 " -O <file>: Redirect output to file (use \"-\" for stdout)\n"
390 " --user=<user> HTTP authentication username\n"
391 " --password=<password> HTTP authentication password\n"
392 " --user-agent|-U <str> Set HTTP user agent\n"
393 " --post-data=STRING use the POST method; send STRING as the data\n"
394 " --spider|-s Spider mode - only check file existence\n"
395 " --timeout=N|-T N Set connect/request timeout to N seconds\n"
396 " --proxy=on|off|-Y on|off Enable/disable env var configured proxy\n"
397 "\n"
398 "HTTPS options:\n"
399 " --ca-certificate=<cert>: Load CA certificates from file <cert>\n"
400 " --no-check-certificate: don't validate the server's certificate\n"
401 "\n", progname);
402 return 1;
403 }
404
405 static void init_ca_cert(void)
406 {
407 glob_t gl;
408 int i;
409
410 glob("/etc/ssl/certs/*.crt", 0, NULL, &gl);
411 for (i = 0; i < gl.gl_pathc; i++)
412 ssl_ops->context_add_ca_crt_file(ssl_ctx, gl.gl_pathv[i]);
413 }
414
415 static void init_ustream_ssl(void)
416 {
417 void *dlh;
418
419 dlh = dlopen("libustream-ssl." LIB_EXT, RTLD_LAZY | RTLD_LOCAL);
420 if (!dlh)
421 return;
422
423 ssl_ops = dlsym(dlh, "ustream_ssl_ops");
424 if (!ssl_ops)
425 return;
426
427 ssl_ctx = ssl_ops->context_new(false);
428 }
429
430 static int no_ssl(const char *progname)
431 {
432 fprintf(stderr, "%s: SSL support not available, please install ustream-ssl\n", progname);
433 return 1;
434 }
435
436 enum {
437 L_NO_CHECK_CERTIFICATE,
438 L_CA_CERTIFICATE,
439 L_USER,
440 L_PASSWORD,
441 L_USER_AGENT,
442 L_POST_DATA,
443 L_SPIDER,
444 L_TIMEOUT,
445 L_CONTINUE,
446 L_PROXY,
447 L_NO_PROXY,
448 };
449
450 static const struct option longopts[] = {
451 [L_NO_CHECK_CERTIFICATE] = { "no-check-certificate", no_argument },
452 [L_CA_CERTIFICATE] = { "ca-certificate", required_argument },
453 [L_USER] = { "user", required_argument },
454 [L_PASSWORD] = { "password", required_argument },
455 [L_USER_AGENT] = { "user-agent", required_argument },
456 [L_POST_DATA] = { "post-data", required_argument },
457 [L_SPIDER] = { "spider", no_argument },
458 [L_TIMEOUT] = { "timeout", required_argument },
459 [L_CONTINUE] = { "continue", no_argument },
460 [L_PROXY] = { "proxy", required_argument },
461 [L_NO_PROXY] = { "no-proxy", no_argument },
462 {}
463 };
464
465
466
467 int main(int argc, char **argv)
468 {
469 const char *progname = argv[0];
470 const char *proxy_url;
471 char *username = NULL;
472 char *password = NULL;
473 struct uclient *cl;
474 int longopt_idx = 0;
475 bool has_cert = false;
476 int i, ch;
477 int rc;
478
479 init_ustream_ssl();
480
481 while ((ch = getopt_long(argc, argv, "cO:qsU:Y:", longopts, &longopt_idx)) != -1) {
482 switch(ch) {
483 case 0:
484 switch (longopt_idx) {
485 case L_NO_CHECK_CERTIFICATE:
486 verify = false;
487 break;
488 case L_CA_CERTIFICATE:
489 has_cert = true;
490 if (ssl_ctx)
491 ssl_ops->context_add_ca_crt_file(ssl_ctx, optarg);
492 break;
493 case L_USER:
494 if (!strlen(optarg))
495 break;
496 username = strdup(optarg);
497 memset(optarg, '*', strlen(optarg));
498 break;
499 case L_PASSWORD:
500 if (!strlen(optarg))
501 break;
502 password = strdup(optarg);
503 memset(optarg, '*', strlen(optarg));
504 break;
505 case L_USER_AGENT:
506 user_agent = optarg;
507 break;
508 case L_POST_DATA:
509 post_data = optarg;
510 break;
511 case L_SPIDER:
512 no_output = true;
513 break;
514 case L_TIMEOUT:
515 timeout = atoi(optarg);
516 break;
517 case L_CONTINUE:
518 resume = true;
519 break;
520 case L_PROXY:
521 if (strcmp(optarg, "on") != 0)
522 proxy = false;
523 break;
524 case L_NO_PROXY:
525 proxy = false;
526 break;
527 default:
528 return usage(progname);
529 }
530 break;
531 case 'c':
532 resume = true;
533 break;
534 case 'U':
535 user_agent = optarg;
536 break;
537 case 'O':
538 output_file = optarg;
539 break;
540 case 'q':
541 quiet = true;
542 break;
543 case 's':
544 no_output = true;
545 break;
546 case 'T':
547 timeout = atoi(optarg);
548 break;
549 case 'Y':
550 if (strcmp(optarg, "on") != 0)
551 proxy = false;
552 break;
553 default:
554 return usage(progname);
555 }
556 }
557
558 argv += optind;
559 argc -= optind;
560
561 if (verify && !has_cert)
562 default_certs = true;
563
564 if (argc < 1)
565 return usage(progname);
566
567 if (!ssl_ctx) {
568 for (i = 0; i < argc; i++) {
569 if (!strncmp(argv[i], "https", 5))
570 return no_ssl(progname);
571 }
572 }
573
574 urls = argv + 1;
575 n_urls = argc - 1;
576
577 uloop_init();
578
579 if (username) {
580 if (password)
581 asprintf(&auth_str, "%s:%s", username, password);
582 else
583 auth_str = username;
584 }
585
586 if (!quiet)
587 fprintf(stderr, "Downloading '%s'\n", argv[0]);
588
589 proxy_url = get_proxy_url(argv[0]);
590 if (proxy_url) {
591 cl = uclient_new(proxy_url, auth_str, &cb);
592 uclient_set_proxy_url(cl, argv[0], NULL);
593 } else {
594 cl = uclient_new(argv[0], auth_str, &cb);
595 }
596 if (!cl) {
597 fprintf(stderr, "Failed to allocate uclient context\n");
598 return 1;
599 }
600
601 if (ssl_ctx && default_certs)
602 init_ca_cert();
603
604 cur_resume = resume;
605 rc = init_request(cl);
606 if (!rc) {
607 /* no error received, we can enter main loop */
608 uloop_run();
609 } else {
610 fprintf(stderr, "Failed to establish connection\n");
611 error_ret = 4;
612 }
613
614 uloop_done();
615
616 uclient_free(cl);
617
618 if (output_fd >= 0 && output_fd != STDOUT_FILENO)
619 close(output_fd);
620
621 if (ssl_ctx)
622 ssl_ops->context_free(ssl_ctx);
623
624 return error_ret;
625 }