1 /* vi: set noexpandtab sw=4 sts=4: */
2 /* opkg_download.c - the opkg package management system
6 Copyright (C) 2001 University of Southern California
7 Copyright (C) 2008 OpenMoko Inc
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.
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.
25 #include "opkg_download.h"
26 #include "opkg_message.h"
28 #include "sprintf_alloc.h"
30 #include "file_util.h"
31 #include "opkg_defines.h"
32 #include "libbb/libbb.h"
35 #include <curl/curl.h>
38 #if defined(HAVE_SSLCURL) || defined(HAVE_OPENSSL)
39 #include <openssl/conf.h>
40 #include <openssl/evp.h>
41 #include <openssl/err.h>
42 #include <openssl/ssl.h>
45 #if defined(HAVE_GPGME)
47 #elif defined(HAVE_OPENSSL)
48 #include <openssl/bio.h>
49 #include <openssl/objects.h>
50 #include <openssl/x509.h>
51 #include <openssl/pem.h>
52 #include <openssl/hmac.h>
55 #ifdef HAVE_PATHFINDER
56 #include "opkg_pathfinder.h"
59 #if defined(HAVE_OPENSSL) || defined(HAVE_SSLCURL)
60 static void openssl_init(void);
64 static X509_STORE
*setup_verify(char *CAfile
, char *CApath
);
69 * Make curl an instance variable so we don't have to instanciate it
72 static CURL
*curl
= NULL
;
73 static CURL
*opkg_curl_init(curl_progress_func cb
, void *data
);
77 str_starts_with(const char *str
, const char *prefix
)
79 return (strncmp(str
, prefix
, strlen(prefix
)) == 0);
83 opkg_download(const char *src
, const char *dest_file_name
,
84 curl_progress_func cb
, void *data
)
88 char *src_basec
= xstrdup(src
);
89 char *src_base
= basename(src_basec
);
90 char *tmp_file_location
;
92 opkg_msg(NOTICE
,"Downloading %s.\n", src
);
94 if (str_starts_with(src
, "file:")) {
95 const char *file_src
= src
+ 5;
96 opkg_msg(INFO
, "Copying %s to %s...", file_src
, dest_file_name
);
97 err
= file_copy(file_src
, dest_file_name
);
98 opkg_msg(INFO
, "Done.\n");
103 sprintf_alloc(&tmp_file_location
, "%s/%s", conf
->tmp_dir
, src_base
);
105 err
= unlink(tmp_file_location
);
106 if (err
&& errno
!= ENOENT
) {
107 opkg_perror(ERROR
, "Failed to unlink %s", tmp_file_location
);
108 free(tmp_file_location
);
112 if (conf
->http_proxy
) {
113 opkg_msg(DEBUG
, "Setting environment variable: http_proxy = %s.\n",
115 setenv("http_proxy", conf
->http_proxy
, 1);
117 if (conf
->ftp_proxy
) {
118 opkg_msg(DEBUG
, "Setting environment variable: ftp_proxy = %s.\n",
120 setenv("ftp_proxy", conf
->ftp_proxy
, 1);
122 if (conf
->no_proxy
) {
123 opkg_msg(DEBUG
,"Setting environment variable: no_proxy = %s.\n",
125 setenv("no_proxy", conf
->no_proxy
, 1);
130 FILE * file
= fopen (tmp_file_location
, "w");
132 curl
= opkg_curl_init (cb
, data
);
135 curl_easy_setopt (curl
, CURLOPT_URL
, src
);
136 curl_easy_setopt (curl
, CURLOPT_WRITEDATA
, file
);
138 res
= curl_easy_perform (curl
);
143 curl_easy_getinfo(curl
, CURLINFO_RESPONSE_CODE
, &error_code
);
144 opkg_msg(ERROR
, "Failed to download %s: %s.\n",
145 src
, curl_easy_strerror(res
));
146 free(tmp_file_location
);
153 free(tmp_file_location
);
164 if (conf
->http_proxy
|| conf
->ftp_proxy
) {
169 argv
[i
++] = tmp_file_location
;
175 opkg_msg(ERROR
, "Failed to download %s, wget returned %d.\n", src
, res
);
176 free(tmp_file_location
);
182 err
= file_move(tmp_file_location
, dest_file_name
);
184 free(tmp_file_location
);
190 opkg_download_cache(const char *src
, const char *dest_file_name
,
191 curl_progress_func cb
, void *data
)
193 char *cache_name
= xstrdup(src
);
194 char *cache_location
, *p
;
197 if (!conf
->cache
|| str_starts_with(src
, "file:")) {
198 err
= opkg_download(src
, dest_file_name
, cb
, data
);
202 if(!file_is_dir(conf
->cache
)){
203 opkg_msg(ERROR
, "%s is not a directory.\n",
209 for (p
= cache_name
; *p
; p
++)
211 *p
= ','; /* looks nicer than | or # */
213 sprintf_alloc(&cache_location
, "%s/%s", conf
->cache
, cache_name
);
214 if (file_exists(cache_location
))
215 opkg_msg(NOTICE
, "Copying %s.\n", cache_location
);
217 err
= opkg_download(src
, cache_location
, cb
, data
);
219 (void) unlink(cache_location
);
224 err
= file_copy(cache_location
, dest_file_name
);
228 free(cache_location
);
235 opkg_download_pkg(pkg_t
*pkg
, const char *dir
)
239 char *stripped_filename
;
241 if (pkg
->src
== NULL
) {
242 opkg_msg(ERROR
, "Package %s is not available from any configured src.\n",
246 if (pkg
->filename
== NULL
) {
247 opkg_msg(ERROR
, "Package %s does not have a valid filename field.\n",
252 sprintf_alloc(&url
, "%s/%s", pkg
->src
->value
, pkg
->filename
);
254 /* XXX: BUG: The pkg->filename might be something like
255 "../../foo.opk". While this is correct, and exactly what we
256 want to use to construct url above, here we actually need to
257 use just the filename part, without any directory. */
259 stripped_filename
= strrchr(pkg
->filename
, '/');
260 if ( ! stripped_filename
)
261 stripped_filename
= pkg
->filename
;
263 sprintf_alloc(&pkg
->local_filename
, "%s/%s", dir
, stripped_filename
);
265 err
= opkg_download_cache(url
, pkg
->local_filename
, NULL
, NULL
);
272 * Downloads file from url, installs in package database, return package name.
275 opkg_prepare_url_for_install(const char *url
, char **namep
)
282 if (str_starts_with(url
, "http://")
283 || str_starts_with(url
, "ftp://")) {
285 char *file_basec
= xstrdup(url
);
286 char *file_base
= basename(file_basec
);
288 sprintf_alloc(&tmp_file
, "%s/%s", conf
->tmp_dir
, file_base
);
289 err
= opkg_download(url
, tmp_file
, NULL
, NULL
);
293 err
= pkg_init_from_file(pkg
, tmp_file
);
300 } else if (strcmp(&url
[strlen(url
) - 4], OPKG_PKG_EXTENSION
) == 0
301 || strcmp(&url
[strlen(url
) - 4], IPKG_PKG_EXTENSION
) == 0
302 || strcmp(&url
[strlen(url
) - 4], DPKG_PKG_EXTENSION
) == 0) {
304 err
= pkg_init_from_file(pkg
, url
);
307 opkg_msg(DEBUG2
, "Package %s provided by hand (%s).\n",
308 pkg
->name
, pkg
->local_filename
);
309 pkg
->provided_by_hand
= 1;
317 pkg
->dest
= conf
->default_dest
;
318 pkg
->state_want
= SW_INSTALL
;
319 pkg
->state_flag
|= SF_PREFER
;
320 hash_insert_pkg(pkg
, 1);
329 opkg_verify_file (char *text_file
, char *sig_file
)
331 #if defined HAVE_GPGME
332 if (conf
->check_signature
== 0 )
336 gpgme_data_t sig
, text
, key
;
338 gpgme_verify_result_t result
;
340 char *trusted_path
= NULL
;
342 err
= gpgme_new (&ctx
);
347 sprintf_alloc(&trusted_path
, "%s/%s", conf
->offline_root
, "/etc/opkg/trusted.gpg");
348 err
= gpgme_data_new_from_file (&key
, trusted_path
, 1);
354 err
= gpgme_op_import (ctx
, key
);
357 gpgme_data_release (key
);
360 gpgme_data_release (key
);
362 err
= gpgme_data_new_from_file (&sig
, sig_file
, 1);
369 err
= gpgme_data_new_from_file (&text
, text_file
, 1);
372 gpgme_data_release (sig
);
377 err
= gpgme_op_verify (ctx
, sig
, text
, NULL
);
379 result
= gpgme_op_verify_result (ctx
);
383 /* see if any of the signitures matched */
384 s
= result
->signatures
;
387 status
= gpg_err_code (s
->status
);
388 if (status
== GPG_ERR_NO_ERROR
)
394 gpgme_data_release (sig
);
395 gpgme_data_release (text
);
399 #elif defined HAVE_OPENSSL
400 X509_STORE
*store
= NULL
;
402 BIO
*in
= NULL
, *indata
= NULL
;
404 // Sig check failed by default !
409 // Set-up the key store
410 if(!(store
= setup_verify(conf
->signature_ca_file
, conf
->signature_ca_path
))){
411 opkg_msg(ERROR
, "Can't open CA certificates.\n");
412 goto verify_file_end
;
415 // Open a BIO to read the sig file
416 if (!(in
= BIO_new_file(sig_file
, "rb"))){
417 opkg_msg(ERROR
, "Can't open signature file %s.\n", sig_file
);
418 goto verify_file_end
;
421 // Read the PKCS7 block contained in the sig file
422 p7
= PEM_read_bio_PKCS7(in
, NULL
, NULL
, NULL
);
424 opkg_msg(ERROR
, "Can't read signature file %s (Corrupted ?).\n",
426 goto verify_file_end
;
428 #if defined(HAVE_PATHFINDER)
429 if(conf
->check_x509_path
){
430 if(!pkcs7_pathfinder_verify_signers(p7
)){
431 opkg_msg(ERROR
, "pkcs7_pathfinder_verify_signers: "
432 "Path verification failed.\n");
433 goto verify_file_end
;
438 // Open the Package file to authenticate
439 if (!(indata
= BIO_new_file(text_file
, "rb"))){
440 opkg_msg(ERROR
, "Can't open file %s.\n", text_file
);
441 goto verify_file_end
;
444 // Let's verify the autenticity !
445 if (PKCS7_verify(p7
, NULL
, store
, indata
, NULL
, PKCS7_BINARY
) != 1){
447 opkg_msg(ERROR
, "Verification failure.\n");
457 X509_STORE_free(store
);
461 /* mute `unused variable' warnings. */
470 #if defined(HAVE_OPENSSL) || defined(HAVE_SSLCURL)
471 static void openssl_init(void){
475 OPENSSL_config(NULL
);
476 OpenSSL_add_all_algorithms();
477 ERR_load_crypto_strings();
485 #if defined HAVE_OPENSSL
487 setup_verify(char *CAfile
, char *CApath
)
489 X509_STORE
*store
= NULL
;
490 X509_LOOKUP
*lookup
= NULL
;
492 if(!(store
= X509_STORE_new())){
493 // Something bad is happening...
497 // adds the X509 file lookup method
498 lookup
= X509_STORE_add_lookup(store
,X509_LOOKUP_file());
503 // Autenticating against one CA file
505 if(!X509_LOOKUP_load_file(lookup
,CAfile
,X509_FILETYPE_PEM
)) {
506 // Invalid CA => Bye bye
507 opkg_msg(ERROR
, "Error loading file %s.\n", CAfile
);
511 X509_LOOKUP_load_file(lookup
,NULL
,X509_FILETYPE_DEFAULT
);
514 // Now look into CApath directory if supplied
515 lookup
= X509_STORE_add_lookup(store
,X509_LOOKUP_hash_dir());
521 if(!X509_LOOKUP_add_dir(lookup
,CApath
,X509_FILETYPE_PEM
)) {
522 opkg_msg(ERROR
, "Error loading directory %s.\n", CApath
);
526 X509_LOOKUP_add_dir(lookup
,NULL
,X509_FILETYPE_DEFAULT
);
535 X509_STORE_free(store
);
543 void opkg_curl_cleanup(void){
545 curl_easy_cleanup (curl
);
551 opkg_curl_init(curl_progress_func cb
, void *data
)
555 curl
= curl_easy_init();
560 if (conf
->ssl_engine
) {
562 /* use crypto engine */
563 if (curl_easy_setopt(curl
, CURLOPT_SSLENGINE
, conf
->ssl_engine
) != CURLE_OK
){
564 opkg_msg(ERROR
, "Can't set crypto engine '%s'.\n",
570 /* set the crypto engine as default */
571 if (curl_easy_setopt(curl
, CURLOPT_SSLENGINE_DEFAULT
, 1L) != CURLE_OK
){
572 opkg_msg(ERROR
, "Can't set crypto engine '%s' as default.\n",
580 /* cert & key can only be in PEM case in the same file */
581 if(conf
->ssl_key_passwd
){
582 if (curl_easy_setopt(curl
, CURLOPT_SSLKEYPASSWD
, conf
->ssl_key_passwd
) != CURLE_OK
)
584 opkg_msg(DEBUG
, "Failed to set key password.\n");
588 /* sets the client certificate and its type */
589 if(conf
->ssl_cert_type
){
590 if (curl_easy_setopt(curl
, CURLOPT_SSLCERTTYPE
, conf
->ssl_cert_type
) != CURLE_OK
)
592 opkg_msg(DEBUG
, "Failed to set certificate format.\n");
595 /* SSL cert name isn't mandatory */
597 curl_easy_setopt(curl
, CURLOPT_SSLCERT
, conf
->ssl_cert
);
600 /* sets the client key and its type */
601 if(conf
->ssl_key_type
){
602 if (curl_easy_setopt(curl
, CURLOPT_SSLKEYTYPE
, conf
->ssl_key_type
) != CURLE_OK
)
604 opkg_msg(DEBUG
, "Failed to set key format.\n");
608 if (curl_easy_setopt(curl
, CURLOPT_SSLKEY
, conf
->ssl_key
) != CURLE_OK
)
610 opkg_msg(DEBUG
, "Failed to set key.\n");
614 /* Should we verify the peer certificate ? */
615 if(conf
->ssl_dont_verify_peer
){
617 * CURLOPT_SSL_VERIFYPEER default is nonzero (curl => 7.10)
619 curl_easy_setopt(curl
, CURLOPT_SSL_VERIFYPEER
, 0);
621 #ifdef HAVE_PATHFINDER
622 if(conf
->check_x509_path
){
623 if (curl_easy_setopt(curl
, CURLOPT_SSL_CTX_FUNCTION
, curl_ssl_ctx_function
) != CURLE_OK
){
624 opkg_msg(DEBUG
, "Failed to set ssl path verification callback.\n");
626 curl_easy_setopt(curl
, CURLOPT_SSL_CTX_DATA
, NULL
);
632 /* certification authority file and/or path */
633 if(conf
->ssl_ca_file
){
634 curl_easy_setopt(curl
, CURLOPT_CAINFO
, conf
->ssl_ca_file
);
636 if(conf
->ssl_ca_path
){
637 curl_easy_setopt(curl
, CURLOPT_CAPATH
, conf
->ssl_ca_path
);
641 curl_easy_setopt (curl
, CURLOPT_FOLLOWLOCATION
, 1);
642 curl_easy_setopt (curl
, CURLOPT_FAILONERROR
, 1);
643 if (conf
->http_proxy
|| conf
->ftp_proxy
)
646 sprintf_alloc (&userpwd
, "%s:%s", conf
->proxy_user
,
648 curl_easy_setopt(curl
, CURLOPT_PROXYUSERPWD
, userpwd
);
653 curl_easy_setopt (curl
, CURLOPT_NOPROGRESS
, (cb
== NULL
));
656 curl_easy_setopt (curl
, CURLOPT_PROGRESSDATA
, data
);
657 curl_easy_setopt (curl
, CURLOPT_PROGRESSFUNCTION
, cb
);