Fix some errno abuse.
[oweals/opkg-lede.git] / libopkg / opkg_download.c
1 /* vi: set noexpandtab sw=4 sts=4: */
2 /* opkg_download.c - the opkg package management system
3
4    Carl D. Worth
5
6    Copyright (C) 2001 University of Southern California
7    Copyright (C) 2008 OpenMoko Inc
8
9    This program is free software; you can redistribute it and/or
10    modify it under the terms of the GNU General Public License as
11    published by the Free Software Foundation; either version 2, or (at
12    your option) any later version.
13
14    This program is distributed in the hope that it will be useful, but
15    WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17    General Public License for more details.
18 */
19 #include "config.h"
20
21 #ifdef HAVE_CURL
22 #include <curl/curl.h>
23 #endif
24
25 #if defined(HAVE_SSLCURL) || defined(HAVE_OPENSSL)
26 #include <openssl/conf.h>
27 #include <openssl/evp.h>
28 #include <openssl/err.h>
29 #include <openssl/ssl.h>
30 #endif
31
32 #if defined(HAVE_GPGME)
33 #include <gpgme.h>
34 #elif defined(HAVE_OPENSSL)
35 #include <openssl/bio.h>
36 #include <openssl/objects.h>
37 #include <openssl/x509.h>
38 #include <openssl/pem.h>
39 #include <openssl/hmac.h>
40 #endif
41
42 #include "includes.h"
43 #include "opkg_download.h"
44 #include "opkg_message.h"
45
46 #include "sprintf_alloc.h"
47 #include "xsystem.h"
48 #include "file_util.h"
49 #include "opkg_defines.h"
50 #include "libbb/libbb.h"
51
52 #ifdef HAVE_PATHFINDER
53 #include "opkg_pathfinder.h"
54 #endif
55
56 #if defined(HAVE_OPENSSL) || defined(HAVE_SSLCURL)
57 static void openssl_init(void);
58 #endif
59
60 #ifdef HAVE_OPENSSL
61 static X509_STORE *setup_verify(opkg_conf_t *conf, char *CAfile, char *CApath);
62 #endif
63
64 #ifdef HAVE_CURL
65 /*
66  * Make curl an instance variable so we don't have to instanciate it
67  * each time
68  */
69 static CURL *curl = NULL;
70 static CURL *opkg_curl_init(opkg_conf_t *conf, curl_progress_func cb, void *data);
71 #endif
72
73 static int
74 str_starts_with(const char *str, const char *prefix)
75 {
76     return (strncmp(str, prefix, strlen(prefix)) == 0);
77 }
78
79 int opkg_download(opkg_conf_t *conf, const char *src,
80   const char *dest_file_name, curl_progress_func cb, void *data)
81 {
82     int err = 0;
83
84     char *src_basec = xstrdup(src);
85     char *src_base = basename(src_basec);
86     char *tmp_file_location;
87
88     opkg_message(conf,OPKG_NOTICE,"Downloading %s\n", src);
89         
90     if (str_starts_with(src, "file:")) {
91         const char *file_src = src + 5;
92         opkg_message(conf,OPKG_INFO,"Copying %s to %s...", file_src, dest_file_name);
93         err = file_copy(file_src, dest_file_name);
94         opkg_message(conf,OPKG_INFO,"Done\n");
95         free(src_basec);
96         return err;
97     }
98
99     sprintf_alloc(&tmp_file_location, "%s/%s", conf->tmp_dir, src_base);
100     err = unlink(tmp_file_location);
101     if (err && errno != ENOENT) {
102         opkg_message(conf,OPKG_ERROR, "%s: ERROR: failed to unlink %s: %s\n",
103                 __FUNCTION__, tmp_file_location, strerror(errno));
104         free(tmp_file_location);
105         free(src_basec);
106         return -1;
107     }
108
109     if (conf->http_proxy) {
110         opkg_message(conf,OPKG_DEBUG,"Setting environment variable: http_proxy = %s\n", conf->http_proxy);
111         setenv("http_proxy", conf->http_proxy, 1);
112     }
113     if (conf->ftp_proxy) {
114         opkg_message(conf,OPKG_DEBUG,"Setting environment variable: ftp_proxy = %s\n", conf->ftp_proxy);
115         setenv("ftp_proxy", conf->ftp_proxy, 1);
116     }
117     if (conf->no_proxy) {
118         opkg_message(conf,OPKG_DEBUG,"Setting environment variable: no_proxy = %s\n", conf->no_proxy);
119         setenv("no_proxy", conf->no_proxy, 1);
120     }
121
122 #ifdef HAVE_CURL
123     CURLcode res;
124     FILE * file = fopen (tmp_file_location, "w");
125
126     curl = opkg_curl_init (conf, cb, data);
127     if (curl)
128     {
129         curl_easy_setopt (curl, CURLOPT_URL, src);
130         curl_easy_setopt (curl, CURLOPT_WRITEDATA, file);
131
132         res = curl_easy_perform (curl);
133         fclose (file);
134         if (res)
135         {
136             long error_code;
137             curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &error_code);
138             opkg_message(conf, OPKG_ERROR, "Failed to download %s. \nerror detail: %s\n", src, curl_easy_strerror(res));
139             free(tmp_file_location);
140             free(src_basec);
141             return res;
142         }
143
144     }
145     else
146     {
147         free(tmp_file_location);
148         free(src_basec);
149         return -1;
150     }
151 #else
152     {
153       int res;
154       const char *argv[8];
155       int i = 0;
156
157       argv[i++] = "wget";
158       argv[i++] = "-q";
159       if (conf->http_proxy || conf->ftp_proxy) {
160         argv[i++] = "-Y";
161         argv[i++] = "on";
162       }
163       argv[i++] = "-O";
164       argv[i++] = tmp_file_location;
165       argv[i++] = src;
166       argv[i++] = NULL;
167       res = xsystem(argv);
168
169       if (res) {
170         opkg_message(conf, OPKG_ERROR, "Failed to download %s, error %d\n", src, res);
171         free(tmp_file_location);
172         free(src_basec);
173         return res;
174       }
175     }
176 #endif
177
178     err = file_move(tmp_file_location, dest_file_name);
179
180     free(tmp_file_location);
181     free(src_basec);
182
183     return err;
184 }
185
186 static int opkg_download_cache(opkg_conf_t *conf, const char *src,
187   const char *dest_file_name, curl_progress_func cb, void *data)
188 {
189     char *cache_name = xstrdup(src);
190     char *cache_location, *p;
191     int err = 0;
192
193     if (!conf->cache || str_starts_with(src, "file:")) {
194         err = opkg_download(conf, src, dest_file_name, cb, data);
195         goto out1;
196     }
197
198     for (p = cache_name; *p; p++)
199         if (*p == '/')
200             *p = ',';   /* looks nicer than | or # */
201
202     sprintf_alloc(&cache_location, "%s/%s", conf->cache, cache_name);
203     if (file_exists(cache_location))
204         opkg_message(conf, OPKG_NOTICE, "Copying %s\n", cache_location);
205     else {
206         err = opkg_download(conf, src, cache_location, cb, data);
207         if (err) {
208             (void) unlink(cache_location);
209             goto out2;
210         }
211     }
212
213     err = file_copy(cache_location, dest_file_name);
214
215
216 out2:
217     free(cache_location);
218 out1:
219     free(cache_name);
220     return err;
221 }
222
223 int opkg_download_pkg(opkg_conf_t *conf, pkg_t *pkg, const char *dir)
224 {
225     int err;
226     char *url;
227     char *stripped_filename;
228
229     if (pkg->src == NULL) {
230         opkg_message(conf,OPKG_ERROR, "ERROR: Package %s (parent %s) is not available from any configured src.\n",
231                 pkg->name, pkg->parent->name);
232         return -1;
233     }
234     if (pkg->filename == NULL) {
235         opkg_message(conf,OPKG_ERROR, "ERROR: Package %s (parent %s) does not have a valid filename field.\n",pkg->name, pkg->parent->name);
236         return -1;
237     }
238
239     sprintf_alloc(&url, "%s/%s", pkg->src->value, pkg->filename);
240
241     /* XXX: BUG: The pkg->filename might be something like
242        "../../foo.opk". While this is correct, and exactly what we
243        want to use to construct url above, here we actually need to
244        use just the filename part, without any directory. */
245
246     stripped_filename = strrchr(pkg->filename, '/');
247     if ( ! stripped_filename )
248         stripped_filename = pkg->filename;
249
250     sprintf_alloc(&pkg->local_filename, "%s/%s", dir, stripped_filename);
251
252     err = opkg_download_cache(conf, url, pkg->local_filename, NULL, NULL);
253     free(url);
254
255     return err;
256 }
257
258 /*
259  * Downloads file from url, installs in package database, return package name. 
260  */
261 int opkg_prepare_url_for_install(opkg_conf_t *conf, const char *url, char **namep)
262 {
263      int err = 0;
264      pkg_t *pkg;
265
266      pkg = pkg_new();
267
268      if (str_starts_with(url, "http://")
269          || str_starts_with(url, "ftp://")) {
270           char *tmp_file;
271           char *file_basec = xstrdup(url);
272           char *file_base = basename(file_basec);
273
274           sprintf_alloc(&tmp_file, "%s/%s", conf->tmp_dir, file_base);
275           err = opkg_download(conf, url, tmp_file, NULL, NULL);
276           if (err)
277                return err;
278
279           err = pkg_init_from_file(conf, pkg, tmp_file);
280           if (err)
281                return err;
282
283           free(tmp_file);
284           free(file_basec);
285
286      } else if (strcmp(&url[strlen(url) - 4], OPKG_PKG_EXTENSION) == 0
287                 || strcmp(&url[strlen(url) - 4], IPKG_PKG_EXTENSION) == 0
288                 || strcmp(&url[strlen(url) - 4], DPKG_PKG_EXTENSION) == 0) {
289
290           err = pkg_init_from_file(conf, pkg, url);
291           if (err)
292                return err;
293           opkg_message(conf, OPKG_DEBUG2, "Package %s provided by hand (%s).\n", pkg->name,pkg->local_filename);
294           pkg->provided_by_hand = 1;
295
296      } else {
297        pkg_deinit(pkg);
298        free(pkg);
299        return 0;
300      }
301
302      if (!pkg->architecture) {
303           opkg_message(conf, OPKG_ERROR, "Package %s has no Architecture defined.\n", pkg->name);
304           return -1;
305      }
306
307      pkg->dest = conf->default_dest;
308      pkg->state_want = SW_INSTALL;
309      pkg->state_flag |= SF_PREFER;
310      pkg = hash_insert_pkg(&conf->pkg_hash, pkg, 1,conf);  
311
312      if (namep) {
313           *namep = pkg->name;
314      }
315      return 0;
316 }
317
318 int
319 opkg_verify_file (opkg_conf_t *conf, char *text_file, char *sig_file)
320 {
321 #if defined HAVE_GPGME
322     if (conf->check_signature == 0 )
323         return 0;
324     int status = -1;
325     gpgme_ctx_t ctx;
326     gpgme_data_t sig, text, key;
327     gpgme_error_t err = -1;
328     gpgme_verify_result_t result;
329     gpgme_signature_t s;
330     char *trusted_path = NULL;
331     
332     err = gpgme_new (&ctx);
333
334     if (err)
335         return -1;
336
337     sprintf_alloc(&trusted_path, "%s/%s", conf->offline_root, "/etc/opkg/trusted.gpg");
338     err = gpgme_data_new_from_file (&key, trusted_path, 1); 
339     free (trusted_path);
340     if (err)
341     {
342       return -1;
343     }
344     err = gpgme_op_import (ctx, key);
345     if (err)
346     {
347       gpgme_data_release (key);
348       return -1;
349     }
350     gpgme_data_release (key);
351
352     err = gpgme_data_new_from_file (&sig, sig_file, 1); 
353     if (err)
354     {
355         gpgme_release (ctx);
356         return -1;
357     }
358
359     err = gpgme_data_new_from_file (&text, text_file, 1); 
360     if (err)
361     {
362         gpgme_data_release (sig);
363         gpgme_release (ctx);
364         return -1;
365     }
366
367     err = gpgme_op_verify (ctx, sig, text, NULL);
368
369     result = gpgme_op_verify_result (ctx);
370     if (!result)
371         return -1;
372
373     /* see if any of the signitures matched */
374     s = result->signatures;
375     while (s)
376     {
377         status = gpg_err_code (s->status);
378         if (status == GPG_ERR_NO_ERROR)
379             break;
380         s = s->next;
381     }
382
383
384     gpgme_data_release (sig);
385     gpgme_data_release (text);
386     gpgme_release (ctx);
387
388     return status;
389 #elif defined HAVE_OPENSSL
390     X509_STORE *store = NULL;
391     PKCS7 *p7 = NULL;
392     BIO *in = NULL, *indata = NULL;
393
394     // Sig check failed by default !
395     int status = -1;
396
397     openssl_init();
398
399     // Set-up the key store
400     if(!(store = setup_verify(conf, conf->signature_ca_file, conf->signature_ca_path))){
401         opkg_message(conf, OPKG_ERROR,
402                 "Can't open CA certificates\n");
403         goto verify_file_end;
404     }
405
406     // Open a BIO to read the sig file
407     if (!(in = BIO_new_file(sig_file, "rb"))){
408         opkg_message(conf, OPKG_ERROR,
409                 "Can't open signature file %s\n", sig_file);
410         goto verify_file_end;
411     }
412
413     // Read the PKCS7 block contained in the sig file
414     p7 = PEM_read_bio_PKCS7(in, NULL, NULL, NULL);
415     if(!p7){
416         opkg_message(conf, OPKG_ERROR,
417                 "Can't read signature file (Corrupted ?)\n");
418         goto verify_file_end;
419     }
420 #if defined(HAVE_PATHFINDER)
421     if(conf->check_x509_path){
422         if(!pkcs7_pathfinder_verify_signers(p7)){
423             opkg_message(conf,  OPKG_ERROR, "pkcs7_pathfinder_verify_signers: "
424                     "Path verification failed\n");
425             goto verify_file_end;
426         }
427     }
428 #endif
429
430     // Open the Package file to authenticate
431     if (!(indata = BIO_new_file(text_file, "rb"))){
432         opkg_message(conf, OPKG_ERROR,
433                 "Can't open file %s\n", text_file);
434         goto verify_file_end;
435     }
436
437     // Let's verify the autenticity !
438     if (PKCS7_verify(p7, NULL, store, indata, NULL, PKCS7_BINARY) != 1){
439         // Get Off My Lawn!
440         opkg_message(conf, OPKG_ERROR,
441                 "Verification failure\n");
442     }else{
443         // Victory !
444         status = 0;
445     }
446
447 verify_file_end:
448     BIO_free(in);
449     BIO_free(indata);
450     PKCS7_free(p7);
451     X509_STORE_free(store);
452
453     return status;
454 #else
455     /* mute `unused variable' warnings. */
456     (void) sig_file;
457     (void) text_file;
458     (void) conf;
459     return 0;
460 #endif
461 }
462
463
464 #if defined(HAVE_OPENSSL) || defined(HAVE_SSLCURL)
465 static void openssl_init(void){
466     static int init = 0;
467
468     if(!init){
469         OPENSSL_config(NULL);
470         OpenSSL_add_all_algorithms();
471         ERR_load_crypto_strings();
472         init = 1;
473     }
474 }
475
476 #endif
477
478
479 #if defined HAVE_OPENSSL
480 static X509_STORE *setup_verify(opkg_conf_t *conf, char *CAfile, char *CApath){
481     X509_STORE *store = NULL;
482     X509_LOOKUP *lookup = NULL;
483
484     if(!(store = X509_STORE_new())){
485         // Something bad is happening...
486         goto end;
487     }
488
489     // adds the X509 file lookup method
490     lookup = X509_STORE_add_lookup(store,X509_LOOKUP_file());
491     if (lookup == NULL){
492         goto end;
493     }
494
495     // Autenticating against one CA file
496     if (CAfile) {
497         if(!X509_LOOKUP_load_file(lookup,CAfile,X509_FILETYPE_PEM)) {
498             // Invalid CA => Bye bye
499             opkg_message(conf, OPKG_ERROR,
500                     "Error loading file %s\n", CAfile);
501             goto end;
502         }
503     } else {
504         X509_LOOKUP_load_file(lookup,NULL,X509_FILETYPE_DEFAULT);
505     }
506
507     // Now look into CApath directory if supplied
508     lookup = X509_STORE_add_lookup(store,X509_LOOKUP_hash_dir());
509     if (lookup == NULL){
510         goto end;
511     }
512
513     if (CApath) {
514         if(!X509_LOOKUP_add_dir(lookup,CApath,X509_FILETYPE_PEM)) {
515             opkg_message(conf, OPKG_ERROR,
516                     "Error loading directory %s\n", CApath);
517             goto end;
518         }
519     } else {
520         X509_LOOKUP_add_dir(lookup,NULL,X509_FILETYPE_DEFAULT);
521     }
522
523     // All right !
524     ERR_clear_error();
525     return store;
526
527 end:
528
529     X509_STORE_free(store);
530     return NULL;
531
532 }
533
534 #endif
535
536 #ifdef HAVE_CURL
537 void opkg_curl_cleanup(void){
538     if(curl != NULL){
539         curl_easy_cleanup (curl);
540         curl = NULL;
541     }
542 }
543
544 static CURL *opkg_curl_init(opkg_conf_t *conf, curl_progress_func cb, void *data){
545
546     if(curl == NULL){
547         curl = curl_easy_init();
548
549 #ifdef HAVE_SSLCURL
550         openssl_init();
551
552         if (conf->ssl_engine) {
553
554             /* use crypto engine */
555             if (curl_easy_setopt(curl, CURLOPT_SSLENGINE, conf->ssl_engine) != CURLE_OK){
556                 opkg_message(conf, OPKG_ERROR, "can't set crypto engine: '%s'\n",
557                         conf->ssl_engine);
558
559                 opkg_curl_cleanup();
560                 return NULL;
561             }
562             /* set the crypto engine as default */
563             if (curl_easy_setopt(curl, CURLOPT_SSLENGINE_DEFAULT, 1L) != CURLE_OK){
564                 opkg_message(conf, OPKG_ERROR, "can't set crypto engine as default\n");
565
566                 opkg_curl_cleanup();
567                 return NULL;
568             }
569         }
570
571         /* cert & key can only be in PEM case in the same file */
572         if(conf->ssl_key_passwd){
573             if (curl_easy_setopt(curl, CURLOPT_SSLKEYPASSWD, conf->ssl_key_passwd) != CURLE_OK)
574             {
575                 opkg_message(conf, OPKG_DEBUG, "Failed to set key password\n");
576             }
577         }
578
579         /* sets the client certificate and its type */
580         if(conf->ssl_cert_type){
581             if (curl_easy_setopt(curl, CURLOPT_SSLCERTTYPE, conf->ssl_cert_type) != CURLE_OK)
582             {
583                 opkg_message(conf, OPKG_DEBUG, "Failed to set certificate format\n");
584             }
585         }
586         /* SSL cert name isn't mandatory */
587         if(conf->ssl_cert){
588                 curl_easy_setopt(curl, CURLOPT_SSLCERT, conf->ssl_cert);
589         }
590
591         /* sets the client key and its type */
592         if(conf->ssl_key_type){
593             if (curl_easy_setopt(curl, CURLOPT_SSLKEYTYPE, conf->ssl_key_type) != CURLE_OK)
594             {
595                 opkg_message(conf, OPKG_DEBUG, "Failed to set key format\n");
596             }
597         }
598         if(conf->ssl_key){
599             if (curl_easy_setopt(curl, CURLOPT_SSLKEY, conf->ssl_key) != CURLE_OK)
600             {
601                 opkg_message(conf, OPKG_DEBUG, "Failed to set key\n");
602             }
603         }
604
605         /* Should we verify the peer certificate ? */
606         if(conf->ssl_dont_verify_peer){
607             /*
608              * CURLOPT_SSL_VERIFYPEER default is nonzero (curl => 7.10)
609              */
610             curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
611         }else{
612 #ifdef HAVE_PATHFINDER
613             if(conf->check_x509_path){
614                 if (curl_easy_setopt(curl, CURLOPT_SSL_CTX_FUNCTION, curl_ssl_ctx_function) != CURLE_OK){
615                     opkg_message(conf, OPKG_DEBUG, "Failed to set ssl path verification callback\n");
616                 }else{
617                     curl_easy_setopt(curl, CURLOPT_SSL_CTX_DATA, NULL);
618                 }
619             }
620 #endif
621         }
622
623         /* certification authority file and/or path */
624         if(conf->ssl_ca_file){
625             curl_easy_setopt(curl, CURLOPT_CAINFO, conf->ssl_ca_file);
626         }
627         if(conf->ssl_ca_path){
628             curl_easy_setopt(curl, CURLOPT_CAPATH, conf->ssl_ca_path);
629         }
630 #endif
631
632         curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1);
633         curl_easy_setopt (curl, CURLOPT_FAILONERROR, 1);
634         if (conf->http_proxy || conf->ftp_proxy)
635         {
636             char *userpwd;
637             sprintf_alloc (&userpwd, "%s:%s", conf->proxy_user,
638                     conf->proxy_passwd);
639             curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, userpwd);
640             free (userpwd);
641         }
642     }
643
644     curl_easy_setopt (curl, CURLOPT_NOPROGRESS, (cb == NULL));
645     if (cb)
646     {
647         curl_easy_setopt (curl, CURLOPT_PROGRESSDATA, data);
648         curl_easy_setopt (curl, CURLOPT_PROGRESSFUNCTION, cb);
649     }
650
651     return curl;
652
653 }
654 #endif