cmake: Find libubox/ustream-ssl.h header file
[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         int len;
258
259         if (!no_output && output_fd < 0)
260                 return;
261
262         while (1) {
263                 len = uclient_read(cl, buf, sizeof(buf));
264                 if (!len)
265                         return;
266
267                 out_bytes += len;
268                 if (!no_output)
269                         write(output_fd, buf, len);
270         }
271 }
272
273 static void msg_connecting(struct uclient *cl)
274 {
275         char addr[INET6_ADDRSTRLEN];
276         int port;
277
278         if (quiet)
279                 return;
280
281         uclient_get_addr(addr, &port, &cl->remote_addr);
282         fprintf(stderr, "Connecting to %s:%d\n", addr, port);
283 }
284
285 static void check_resume_offset(struct uclient *cl)
286 {
287         char range_str[64];
288         struct stat st;
289         char *file;
290         int ret;
291
292         file = uclient_get_url_filename(cl->url->location, "index.html");
293         if (!file)
294                 return;
295
296         ret = stat(file, &st);
297         free(file);
298         if (ret)
299                 return;
300
301         if (!st.st_size)
302                 return;
303
304         snprintf(range_str, sizeof(range_str), "bytes=%"PRIu64"-", (uint64_t) st.st_size);
305         uclient_http_set_header(cl, "Range", range_str);
306 }
307
308 static int init_request(struct uclient *cl)
309 {
310         int rc;
311
312         out_offset = 0;
313         out_bytes = 0;
314         out_len = 0;
315         uclient_http_set_ssl_ctx(cl, ssl_ops, ssl_ctx, verify);
316
317         if (timeout)
318                 cl->timeout_msecs = timeout * 1000;
319
320         rc = uclient_connect(cl);
321         if (rc)
322                 return rc;
323
324         msg_connecting(cl);
325
326         rc = uclient_http_set_request_type(cl, post_data ? "POST" : "GET");
327         if (rc)
328                 return rc;
329
330         uclient_http_reset_headers(cl);
331         uclient_http_set_header(cl, "User-Agent", user_agent);
332         if (cur_resume)
333                 check_resume_offset(cl);
334
335         if (post_data) {
336                 uclient_http_set_header(cl, "Content-Type", "application/x-www-form-urlencoded");
337                 uclient_write(cl, post_data, strlen(post_data));
338         }
339
340         rc = uclient_request(cl);
341         if (rc)
342                 return rc;
343
344         return 0;
345 }
346
347 static void request_done(struct uclient *cl)
348 {
349         const char *proxy_url;
350
351         if (n_urls) {
352                 proxy_url = get_proxy_url(*urls);
353                 if (proxy_url) {
354                         uclient_set_url(cl, proxy_url, NULL);
355                         uclient_set_proxy_url(cl, *urls, auth_str);
356                 } else {
357                         uclient_set_url(cl, *urls, auth_str);
358                 }
359                 n_urls--;
360                 cur_resume = resume;
361                 error_ret = init_request(cl);
362                 if (error_ret == 0)
363                         return;
364         }
365
366         if (output_fd >= 0 && !output_file) {
367                 close(output_fd);
368                 output_fd = -1;
369         }
370         uclient_disconnect(cl);
371         uloop_end();
372 }
373
374
375 static void eof_cb(struct uclient *cl)
376 {
377         if (!quiet) {
378                 pmt_update(&pmt_timer);
379                 uloop_timeout_cancel(&pmt_timer);
380                 fprintf(stderr, "\n");
381         }
382
383         if (!cl->data_eof) {
384                 if (!quiet)
385                         fprintf(stderr, "Connection reset prematurely\n");
386                 error_ret = 4;
387         } else if (!quiet) {
388                 fprintf(stderr, "Download completed (%"PRIu64" bytes)\n", (uint64_t) out_bytes);
389         }
390         request_done(cl);
391 }
392
393 static void handle_uclient_error(struct uclient *cl, int code)
394 {
395         const char *type = "Unknown error";
396         bool ignore = false;
397
398         switch(code) {
399         case UCLIENT_ERROR_CONNECT:
400                 type = "Connection failed";
401                 error_ret = 4;
402                 break;
403         case UCLIENT_ERROR_TIMEDOUT:
404                 type = "Connection timed out";
405                 error_ret = 4;
406                 break;
407         case UCLIENT_ERROR_SSL_INVALID_CERT:
408                 type = "Invalid SSL certificate";
409                 ignore = !verify;
410                 error_ret = 5;
411                 break;
412         case UCLIENT_ERROR_SSL_CN_MISMATCH:
413                 type = "Server hostname does not match SSL certificate";
414                 ignore = !verify;
415                 error_ret = 5;
416                 break;
417         default:
418                 error_ret = 1;
419                 break;
420         }
421
422         if (!quiet)
423                 fprintf(stderr, "Connection error: %s%s\n", type, ignore ? " (ignored)" : "");
424
425         if (ignore)
426                 error_ret = 0;
427         else
428                 request_done(cl);
429 }
430
431 static const struct uclient_cb cb = {
432         .header_done = header_done_cb,
433         .data_read = read_data_cb,
434         .data_eof = eof_cb,
435         .error = handle_uclient_error,
436 };
437
438 static int usage(const char *progname)
439 {
440         fprintf(stderr,
441                 "Usage: %s [options] <URL>\n"
442                 "Options:\n"
443                 "       -4                              Use IPv4 only\n"
444                 "       -6                              Use IPv6 only\n"
445                 "       -q                              Turn off status messages\n"
446                 "       -O <file>                       Redirect output to file (use \"-\" for stdout)\n"
447                 "       -P <dir>                        Set directory for output files\n"
448                 "       --user=<user>                   HTTP authentication username\n"
449                 "       --password=<password>           HTTP authentication password\n"
450                 "       --user-agent|-U <str>           Set HTTP user agent\n"
451                 "       --post-data=STRING              use the POST method; send STRING as the data\n"
452                 "       --spider|-s                     Spider mode - only check file existence\n"
453                 "       --timeout=N|-T N                Set connect/request timeout to N seconds\n"
454                 "       --proxy=on|off|-Y on|off        Enable/disable env var configured proxy\n"
455                 "\n"
456                 "HTTPS options:\n"
457                 "       --ca-certificate=<cert>         Load CA certificates from file <cert>\n"
458                 "       --no-check-certificate          don't validate the server's certificate\n"
459                 "\n", progname);
460         return 1;
461 }
462
463 static void init_ca_cert(void)
464 {
465         glob_t gl;
466         int i;
467
468         glob("/etc/ssl/certs/*.crt", 0, NULL, &gl);
469         for (i = 0; i < gl.gl_pathc; i++)
470                 ssl_ops->context_add_ca_crt_file(ssl_ctx, gl.gl_pathv[i]);
471 }
472
473 static void init_ustream_ssl(void)
474 {
475         void *dlh;
476
477         dlh = dlopen("libustream-ssl." LIB_EXT, RTLD_LAZY | RTLD_LOCAL);
478         if (!dlh)
479                 return;
480
481         ssl_ops = dlsym(dlh, "ustream_ssl_ops");
482         if (!ssl_ops)
483                 return;
484
485         ssl_ctx = ssl_ops->context_new(false);
486 }
487
488 static int no_ssl(const char *progname)
489 {
490         fprintf(stderr, "%s: SSL support not available, please install ustream-ssl\n", progname);
491         return 1;
492 }
493
494 enum {
495         L_NO_CHECK_CERTIFICATE,
496         L_CA_CERTIFICATE,
497         L_USER,
498         L_PASSWORD,
499         L_USER_AGENT,
500         L_POST_DATA,
501         L_SPIDER,
502         L_TIMEOUT,
503         L_CONTINUE,
504         L_PROXY,
505         L_NO_PROXY,
506 };
507
508 static const struct option longopts[] = {
509         [L_NO_CHECK_CERTIFICATE] = { "no-check-certificate", no_argument },
510         [L_CA_CERTIFICATE] = { "ca-certificate", required_argument },
511         [L_USER] = { "user", required_argument },
512         [L_PASSWORD] = { "password", required_argument },
513         [L_USER_AGENT] = { "user-agent", required_argument },
514         [L_POST_DATA] = { "post-data", required_argument },
515         [L_SPIDER] = { "spider", no_argument },
516         [L_TIMEOUT] = { "timeout", required_argument },
517         [L_CONTINUE] = { "continue", no_argument },
518         [L_PROXY] = { "proxy", required_argument },
519         [L_NO_PROXY] = { "no-proxy", no_argument },
520         {}
521 };
522
523
524
525 int main(int argc, char **argv)
526 {
527         const char *progname = argv[0];
528         const char *proxy_url;
529         char *username = NULL;
530         char *password = NULL;
531         struct uclient *cl;
532         int longopt_idx = 0;
533         bool has_cert = false;
534         int i, ch;
535         int rc;
536         int af = -1;
537
538         signal(SIGPIPE, SIG_IGN);
539         init_ustream_ssl();
540
541         while ((ch = getopt_long(argc, argv, "46cO:P:qsT:U:Y:", longopts, &longopt_idx)) != -1) {
542                 switch(ch) {
543                 case 0:
544                         switch (longopt_idx) {
545                         case L_NO_CHECK_CERTIFICATE:
546                                 verify = false;
547                                 break;
548                         case L_CA_CERTIFICATE:
549                                 has_cert = true;
550                                 if (ssl_ctx)
551                                         ssl_ops->context_add_ca_crt_file(ssl_ctx, optarg);
552                                 break;
553                         case L_USER:
554                                 if (!strlen(optarg))
555                                         break;
556                                 username = strdup(optarg);
557                                 memset(optarg, '*', strlen(optarg));
558                                 break;
559                         case L_PASSWORD:
560                                 if (!strlen(optarg))
561                                         break;
562                                 password = strdup(optarg);
563                                 memset(optarg, '*', strlen(optarg));
564                                 break;
565                         case L_USER_AGENT:
566                                 user_agent = optarg;
567                                 break;
568                         case L_POST_DATA:
569                                 post_data = optarg;
570                                 break;
571                         case L_SPIDER:
572                                 no_output = true;
573                                 break;
574                         case L_TIMEOUT:
575                                 timeout = atoi(optarg);
576                                 break;
577                         case L_CONTINUE:
578                                 resume = true;
579                                 break;
580                         case L_PROXY:
581                                 if (strcmp(optarg, "on") != 0)
582                                         proxy = false;
583                                 break;
584                         case L_NO_PROXY:
585                                 proxy = false;
586                                 break;
587                         default:
588                                 return usage(progname);
589                         }
590                         break;
591                 case '4':
592                         af = AF_INET;
593                         break;
594                 case '6':
595                         af = AF_INET6;
596                         break;
597                 case 'c':
598                         resume = true;
599                         break;
600                 case 'U':
601                         user_agent = optarg;
602                         break;
603                 case 'O':
604                         output_file = optarg;
605                         break;
606                 case 'P':
607                         if (chdir(optarg)) {
608                                 if (!quiet)
609                                         perror("Change output directory");
610                                 exit(1);
611                         }
612                         break;
613                 case 'q':
614                         quiet = true;
615                         break;
616                 case 's':
617                         no_output = true;
618                         break;
619                 case 'T':
620                         timeout = atoi(optarg);
621                         break;
622                 case 'Y':
623                         if (strcmp(optarg, "on") != 0)
624                                 proxy = false;
625                         break;
626                 default:
627                         return usage(progname);
628                 }
629         }
630
631         argv += optind;
632         argc -= optind;
633
634         if (verify && !has_cert)
635                 default_certs = true;
636
637         if (argc < 1)
638                 return usage(progname);
639
640         if (!ssl_ctx) {
641                 for (i = 0; i < argc; i++) {
642                         if (!strncmp(argv[i], "https", 5))
643                                 return no_ssl(progname);
644                 }
645         }
646
647         urls = argv + 1;
648         n_urls = argc - 1;
649
650         uloop_init();
651
652         if (username) {
653                 if (password)
654                         asprintf(&auth_str, "%s:%s", username, password);
655                 else
656                         auth_str = username;
657         }
658
659         if (!quiet)
660                 fprintf(stderr, "Downloading '%s'\n", argv[0]);
661
662         proxy_url = get_proxy_url(argv[0]);
663         if (proxy_url) {
664                 cl = uclient_new(proxy_url, auth_str, &cb);
665                 if (cl)
666                     uclient_set_proxy_url(cl, argv[0], NULL);
667         } else {
668                 cl = uclient_new(argv[0], auth_str, &cb);
669         }
670         if (!cl) {
671                 fprintf(stderr, "Failed to allocate uclient context\n");
672                 return 1;
673         }
674         if (af >= 0)
675             uclient_http_set_address_family(cl, af);
676
677         if (ssl_ctx && default_certs)
678                 init_ca_cert();
679
680         cur_resume = resume;
681         rc = init_request(cl);
682         if (!rc) {
683                 /* no error received, we can enter main loop */
684                 uloop_run();
685         } else {
686                 fprintf(stderr, "Failed to establish connection\n");
687                 error_ret = 4;
688         }
689
690         uloop_done();
691
692         uclient_free(cl);
693
694         if (output_fd >= 0 && output_fd != STDOUT_FILENO)
695                 close(output_fd);
696
697         if (ssl_ctx)
698                 ssl_ops->context_free(ssl_ctx);
699
700         return error_ret;
701 }