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