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