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