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.
22 #include <curl/curl.h>
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>
32 #if defined(HAVE_GPGME)
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>
43 #include "opkg_download.h"
44 #include "opkg_message.h"
46 #include "sprintf_alloc.h"
48 #include "file_util.h"
49 #include "opkg_defines.h"
50 #include "libbb/libbb.h"
52 #ifdef HAVE_PATHFINDER
53 #include "opkg_pathfinder.h"
56 #if defined(HAVE_OPENSSL) || defined(HAVE_SSLCURL)
57 static void openssl_init(void);
61 static X509_STORE
*setup_verify(opkg_conf_t
*conf
, char *CAfile
, char *CApath
);
66 * Make curl an instance variable so we don't have to instanciate it
69 static CURL
*curl
= NULL
;
70 static CURL
*opkg_curl_init(opkg_conf_t
*conf
, curl_progress_func cb
, void *data
);
74 str_starts_with(const char *str
, const char *prefix
)
76 return (strncmp(str
, prefix
, strlen(prefix
)) == 0);
79 int opkg_download(opkg_conf_t
*conf
, const char *src
,
80 const char *dest_file_name
, curl_progress_func cb
, void *data
)
84 char *src_basec
= xstrdup(src
);
85 char *src_base
= basename(src_basec
);
86 char *tmp_file_location
;
88 opkg_message(conf
,OPKG_NOTICE
,"Downloading %s\n", src
);
90 if (str_starts_with(src
, "file:")) {
92 const char *file_src
= src
+ 5;
93 opkg_message(conf
,OPKG_INFO
,"Copying %s to %s...", file_src
, dest_file_name
);
94 ret
= file_copy(src
+ 5, dest_file_name
);
95 opkg_message(conf
,OPKG_INFO
,"Done\n");
100 sprintf_alloc(&tmp_file_location
, "%s/%s", conf
->tmp_dir
, src_base
);
101 err
= unlink(tmp_file_location
);
102 if (err
&& errno
!= ENOENT
) {
103 opkg_message(conf
,OPKG_ERROR
, "%s: ERROR: failed to unlink %s: %s\n",
104 __FUNCTION__
, tmp_file_location
, strerror(errno
));
105 free(tmp_file_location
);
110 if (conf
->http_proxy
) {
111 opkg_message(conf
,OPKG_DEBUG
,"Setting environment variable: http_proxy = %s\n", conf
->http_proxy
);
112 setenv("http_proxy", conf
->http_proxy
, 1);
114 if (conf
->ftp_proxy
) {
115 opkg_message(conf
,OPKG_DEBUG
,"Setting environment variable: ftp_proxy = %s\n", conf
->ftp_proxy
);
116 setenv("ftp_proxy", conf
->ftp_proxy
, 1);
118 if (conf
->no_proxy
) {
119 opkg_message(conf
,OPKG_DEBUG
,"Setting environment variable: no_proxy = %s\n", conf
->no_proxy
);
120 setenv("no_proxy", conf
->no_proxy
, 1);
125 FILE * file
= fopen (tmp_file_location
, "w");
127 curl
= opkg_curl_init (conf
, cb
, data
);
130 curl_easy_setopt (curl
, CURLOPT_URL
, src
);
131 curl_easy_setopt (curl
, CURLOPT_WRITEDATA
, file
);
133 res
= curl_easy_perform (curl
);
138 curl_easy_getinfo(curl
, CURLINFO_RESPONSE_CODE
, &error_code
);
139 opkg_message(conf
, OPKG_ERROR
, "Failed to download %s. \nerror detail: %s\n", src
, curl_easy_strerror(res
));
140 free(tmp_file_location
);
148 free(tmp_file_location
);
160 if (conf
->http_proxy
|| conf
->ftp_proxy
) {
165 argv
[i
++] = tmp_file_location
;
171 opkg_message(conf
, OPKG_ERROR
, "Failed to download %s, error %d\n", src
, res
);
172 free(tmp_file_location
);
179 err
= file_move(tmp_file_location
, dest_file_name
);
181 free(tmp_file_location
);
191 static int opkg_download_cache(opkg_conf_t
*conf
, const char *src
,
192 const char *dest_file_name
, curl_progress_func cb
, void *data
)
194 char *cache_name
= xstrdup(src
);
195 char *cache_location
, *p
;
198 if (!conf
->cache
|| str_starts_with(src
, "file:")) {
199 err
= opkg_download(conf
, src
, dest_file_name
, cb
, data
);
203 for (p
= cache_name
; *p
; p
++)
205 *p
= ','; /* looks nicer than | or # */
207 sprintf_alloc(&cache_location
, "%s/%s", conf
->cache
, cache_name
);
208 if (file_exists(cache_location
))
209 opkg_message(conf
, OPKG_NOTICE
, "Copying %s\n", cache_location
);
211 err
= opkg_download(conf
, src
, cache_location
, cb
, data
);
213 (void) unlink(cache_location
);
218 err
= file_copy(cache_location
, dest_file_name
);
222 free(cache_location
);
228 int opkg_download_pkg(opkg_conf_t
*conf
, pkg_t
*pkg
, const char *dir
)
232 char *stripped_filename
;
234 if (pkg
->src
== NULL
) {
235 opkg_message(conf
,OPKG_ERROR
, "ERROR: Package %s (parent %s) is not available from any configured src.\n",
236 pkg
->name
, pkg
->parent
->name
);
239 if (pkg
->filename
== NULL
) {
240 opkg_message(conf
,OPKG_ERROR
, "ERROR: Package %s (parent %s) does not have a valid filename field.\n",pkg
->name
, pkg
->parent
->name
);
244 sprintf_alloc(&url
, "%s/%s", pkg
->src
->value
, pkg
->filename
);
246 /* XXX: BUG: The pkg->filename might be something like
247 "../../foo.opk". While this is correct, and exactly what we
248 want to use to construct url above, here we actually need to
249 use just the filename part, without any directory. */
251 stripped_filename
= strrchr(pkg
->filename
, '/');
252 if ( ! stripped_filename
)
253 stripped_filename
= pkg
->filename
;
255 sprintf_alloc(&pkg
->local_filename
, "%s/%s", dir
, stripped_filename
);
257 err
= opkg_download_cache(conf
, url
, pkg
->local_filename
, NULL
, NULL
);
264 * Downloads file from url, installs in package database, return package name.
266 int opkg_prepare_url_for_install(opkg_conf_t
*conf
, const char *url
, char **namep
)
273 if (str_starts_with(url
, "http://")
274 || str_starts_with(url
, "ftp://")) {
276 char *file_basec
= xstrdup(url
);
277 char *file_base
= basename(file_basec
);
279 sprintf_alloc(&tmp_file
, "%s/%s", conf
->tmp_dir
, file_base
);
280 err
= opkg_download(conf
, url
, tmp_file
, NULL
, NULL
);
284 err
= pkg_init_from_file(conf
, pkg
, tmp_file
);
291 } else if (strcmp(&url
[strlen(url
) - 4], OPKG_PKG_EXTENSION
) == 0
292 || strcmp(&url
[strlen(url
) - 4], IPKG_PKG_EXTENSION
) == 0
293 || strcmp(&url
[strlen(url
) - 4], DPKG_PKG_EXTENSION
) == 0) {
295 err
= pkg_init_from_file(conf
, pkg
, url
);
298 opkg_message(conf
, OPKG_DEBUG2
, "Package %s provided by hand (%s).\n", pkg
->name
,pkg
->local_filename
);
299 pkg
->provided_by_hand
= 1;
307 if (!pkg
->architecture
) {
308 opkg_message(conf
, OPKG_ERROR
, "Package %s has no Architecture defined.\n", pkg
->name
);
312 pkg
->dest
= conf
->default_dest
;
313 pkg
->state_want
= SW_INSTALL
;
314 pkg
->state_flag
|= SF_PREFER
;
315 pkg
= hash_insert_pkg(&conf
->pkg_hash
, pkg
, 1,conf
);
324 opkg_verify_file (opkg_conf_t
*conf
, char *text_file
, char *sig_file
)
326 #if defined HAVE_GPGME
327 if (conf
->check_signature
== 0 )
331 gpgme_data_t sig
, text
, key
;
332 gpgme_error_t err
= -1;
333 gpgme_verify_result_t result
;
335 char *trusted_path
= NULL
;
337 err
= gpgme_new (&ctx
);
342 sprintf_alloc(&trusted_path
, "%s/%s", conf
->offline_root
, "/etc/opkg/trusted.gpg");
343 err
= gpgme_data_new_from_file (&key
, trusted_path
, 1);
349 err
= gpgme_op_import (ctx
, key
);
352 gpgme_data_release (key
);
355 gpgme_data_release (key
);
357 err
= gpgme_data_new_from_file (&sig
, sig_file
, 1);
364 err
= gpgme_data_new_from_file (&text
, text_file
, 1);
367 gpgme_data_release (sig
);
372 err
= gpgme_op_verify (ctx
, sig
, text
, NULL
);
374 result
= gpgme_op_verify_result (ctx
);
378 /* see if any of the signitures matched */
379 s
= result
->signatures
;
382 status
= gpg_err_code (s
->status
);
383 if (status
== GPG_ERR_NO_ERROR
)
389 gpgme_data_release (sig
);
390 gpgme_data_release (text
);
394 #elif defined HAVE_OPENSSL
395 X509_STORE
*store
= NULL
;
397 BIO
*in
= NULL
, *indata
= NULL
;
399 // Sig check failed by default !
404 // Set-up the key store
405 if(!(store
= setup_verify(conf
, conf
->signature_ca_file
, conf
->signature_ca_path
))){
406 opkg_message(conf
, OPKG_ERROR
,
407 "Can't open CA certificates\n");
408 goto verify_file_end
;
411 // Open a BIO to read the sig file
412 if (!(in
= BIO_new_file(sig_file
, "rb"))){
413 opkg_message(conf
, OPKG_ERROR
,
414 "Can't open signature file %s\n", sig_file
);
415 goto verify_file_end
;
418 // Read the PKCS7 block contained in the sig file
419 p7
= PEM_read_bio_PKCS7(in
, NULL
, NULL
, NULL
);
421 opkg_message(conf
, OPKG_ERROR
,
422 "Can't read signature file (Corrupted ?)\n");
423 goto verify_file_end
;
425 #if defined(HAVE_PATHFINDER)
426 if(conf
->check_x509_path
){
427 if(!pkcs7_pathfinder_verify_signers(p7
)){
428 opkg_message(conf
, OPKG_ERROR
, "pkcs7_pathfinder_verify_signers: "
429 "Path verification failed\n");
430 goto verify_file_end
;
435 // Open the Package file to authenticate
436 if (!(indata
= BIO_new_file(text_file
, "rb"))){
437 opkg_message(conf
, OPKG_ERROR
,
438 "Can't open file %s\n", text_file
);
439 goto verify_file_end
;
442 // Let's verify the autenticity !
443 if (PKCS7_verify(p7
, NULL
, store
, indata
, NULL
, PKCS7_BINARY
) != 1){
445 opkg_message(conf
, OPKG_ERROR
,
446 "Verification failure\n");
456 X509_STORE_free(store
);
460 /* mute `unused variable' warnings. */
469 #if defined(HAVE_OPENSSL) || defined(HAVE_SSLCURL)
470 static void openssl_init(void){
474 OPENSSL_config(NULL
);
475 OpenSSL_add_all_algorithms();
476 ERR_load_crypto_strings();
484 #if defined HAVE_OPENSSL
485 static X509_STORE
*setup_verify(opkg_conf_t
*conf
, char *CAfile
, char *CApath
){
486 X509_STORE
*store
= NULL
;
487 X509_LOOKUP
*lookup
= NULL
;
489 if(!(store
= X509_STORE_new())){
490 // Something bad is happening...
494 // adds the X509 file lookup method
495 lookup
= X509_STORE_add_lookup(store
,X509_LOOKUP_file());
500 // Autenticating against one CA file
502 if(!X509_LOOKUP_load_file(lookup
,CAfile
,X509_FILETYPE_PEM
)) {
503 // Invalid CA => Bye bye
504 opkg_message(conf
, OPKG_ERROR
,
505 "Error loading file %s\n", CAfile
);
509 X509_LOOKUP_load_file(lookup
,NULL
,X509_FILETYPE_DEFAULT
);
512 // Now look into CApath directory if supplied
513 lookup
= X509_STORE_add_lookup(store
,X509_LOOKUP_hash_dir());
519 if(!X509_LOOKUP_add_dir(lookup
,CApath
,X509_FILETYPE_PEM
)) {
520 opkg_message(conf
, OPKG_ERROR
,
521 "Error loading directory %s\n", CApath
);
525 X509_LOOKUP_add_dir(lookup
,NULL
,X509_FILETYPE_DEFAULT
);
534 X509_STORE_free(store
);
542 void opkg_curl_cleanup(void){
544 curl_easy_cleanup (curl
);
549 static CURL
*opkg_curl_init(opkg_conf_t
*conf
, curl_progress_func cb
, void *data
){
552 curl
= curl_easy_init();
557 if (conf
->ssl_engine
) {
559 /* use crypto engine */
560 if (curl_easy_setopt(curl
, CURLOPT_SSLENGINE
, conf
->ssl_engine
) != CURLE_OK
){
561 opkg_message(conf
, OPKG_ERROR
, "can't set crypto engine: '%s'\n",
567 /* set the crypto engine as default */
568 if (curl_easy_setopt(curl
, CURLOPT_SSLENGINE_DEFAULT
, 1L) != CURLE_OK
){
569 opkg_message(conf
, OPKG_ERROR
, "can't set crypto engine as default\n");
576 /* cert & key can only be in PEM case in the same file */
577 if(conf
->ssl_key_passwd
){
578 if (curl_easy_setopt(curl
, CURLOPT_SSLKEYPASSWD
, conf
->ssl_key_passwd
) != CURLE_OK
)
580 opkg_message(conf
, OPKG_DEBUG
, "Failed to set key password\n");
584 /* sets the client certificate and its type */
585 if(conf
->ssl_cert_type
){
586 if (curl_easy_setopt(curl
, CURLOPT_SSLCERTTYPE
, conf
->ssl_cert_type
) != CURLE_OK
)
588 opkg_message(conf
, OPKG_DEBUG
, "Failed to set certificate format\n");
591 /* SSL cert name isn't mandatory */
593 curl_easy_setopt(curl
, CURLOPT_SSLCERT
, conf
->ssl_cert
);
596 /* sets the client key and its type */
597 if(conf
->ssl_key_type
){
598 if (curl_easy_setopt(curl
, CURLOPT_SSLKEYTYPE
, conf
->ssl_key_type
) != CURLE_OK
)
600 opkg_message(conf
, OPKG_DEBUG
, "Failed to set key format\n");
604 if (curl_easy_setopt(curl
, CURLOPT_SSLKEY
, conf
->ssl_key
) != CURLE_OK
)
606 opkg_message(conf
, OPKG_DEBUG
, "Failed to set key\n");
610 /* Should we verify the peer certificate ? */
611 if(conf
->ssl_dont_verify_peer
){
613 * CURLOPT_SSL_VERIFYPEER default is nonzero (curl => 7.10)
615 curl_easy_setopt(curl
, CURLOPT_SSL_VERIFYPEER
, 0);
617 #ifdef HAVE_PATHFINDER
618 if(conf
->check_x509_path
){
619 if (curl_easy_setopt(curl
, CURLOPT_SSL_CTX_FUNCTION
, curl_ssl_ctx_function
) != CURLE_OK
){
620 opkg_message(conf
, OPKG_DEBUG
, "Failed to set ssl path verification callback\n");
622 curl_easy_setopt(curl
, CURLOPT_SSL_CTX_DATA
, NULL
);
628 /* certification authority file and/or path */
629 if(conf
->ssl_ca_file
){
630 curl_easy_setopt(curl
, CURLOPT_CAINFO
, conf
->ssl_ca_file
);
632 if(conf
->ssl_ca_path
){
633 curl_easy_setopt(curl
, CURLOPT_CAPATH
, conf
->ssl_ca_path
);
637 curl_easy_setopt (curl
, CURLOPT_FOLLOWLOCATION
, 1);
638 curl_easy_setopt (curl
, CURLOPT_FAILONERROR
, 1);
639 if (conf
->http_proxy
|| conf
->ftp_proxy
)
642 sprintf_alloc (&userpwd
, "%s:%s", conf
->proxy_user
,
644 curl_easy_setopt(curl
, CURLOPT_PROXYUSERPWD
, userpwd
);
649 curl_easy_setopt (curl
, CURLOPT_NOPROGRESS
, (cb
== NULL
));
652 curl_easy_setopt (curl
, CURLOPT_PROGRESSDATA
, data
);
653 curl_easy_setopt (curl
, CURLOPT_PROGRESSFUNCTION
, cb
);