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