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.
27 #include "opkg_download.h"
28 #include "opkg_message.h"
30 #include "sprintf_alloc.h"
32 #include "file_util.h"
33 #include "opkg_defines.h"
34 #include "libbb/libbb.h"
37 #include <curl/curl.h>
40 #if defined(HAVE_SSLCURL) || defined(HAVE_OPENSSL)
41 #include <openssl/conf.h>
42 #include <openssl/evp.h>
43 #include <openssl/err.h>
44 #include <openssl/ssl.h>
47 #if defined(HAVE_GPGME)
49 #elif defined(HAVE_OPENSSL)
50 #include <openssl/bio.h>
51 #include <openssl/objects.h>
52 #include <openssl/x509.h>
53 #include <openssl/pem.h>
54 #include <openssl/hmac.h>
57 #ifdef HAVE_PATHFINDER
58 #include "opkg_pathfinder.h"
61 #if defined(HAVE_OPENSSL) || defined(HAVE_SSLCURL)
62 static void openssl_init(void);
66 static X509_STORE
*setup_verify(char *CAfile
, char *CApath
);
71 * Make curl an instance variable so we don't have to instanciate it
74 static CURL
*curl
= NULL
;
75 static CURL
*opkg_curl_init(curl_progress_func cb
, void *data
);
78 static int str_starts_with(const char *str
, const char *prefix
)
80 return (strncmp(str
, prefix
, strlen(prefix
)) == 0);
84 opkg_download(const char *src
, const char *dest_file_name
,
85 curl_progress_func cb
, void *data
, const short hide_error
)
89 char *src_basec
= xstrdup(src
);
90 char *src_base
= basename(src_basec
);
91 char *tmp_file_location
;
93 opkg_msg(NOTICE
, "Downloading %s\n", src
);
95 if (str_starts_with(src
, "file:")) {
96 const char *file_src
= src
+ 5;
97 opkg_msg(INFO
, "Copying %s to %s...", file_src
, dest_file_name
);
98 err
= file_copy(file_src
, dest_file_name
);
99 opkg_msg(INFO
, "Done.\n");
104 sprintf_alloc(&tmp_file_location
, "%s/%s", conf
->tmp_dir
, src_base
);
106 err
= unlink(tmp_file_location
);
107 if (err
&& errno
!= ENOENT
) {
108 opkg_perror(ERROR
, "Failed to unlink %s", tmp_file_location
);
109 free(tmp_file_location
);
113 if (conf
->http_proxy
) {
115 "Setting environment variable: http_proxy = %s.\n",
117 setenv("http_proxy", conf
->http_proxy
, 1);
119 if (conf
->ftp_proxy
) {
121 "Setting environment variable: ftp_proxy = %s.\n",
123 setenv("ftp_proxy", conf
->ftp_proxy
, 1);
125 if (conf
->no_proxy
) {
127 "Setting environment variable: no_proxy = %s.\n",
129 setenv("no_proxy", conf
->no_proxy
, 1);
133 FILE *file
= fopen(tmp_file_location
, "w");
135 curl
= opkg_curl_init(cb
, data
);
137 curl_easy_setopt(curl
, CURLOPT_URL
, src
);
138 curl_easy_setopt(curl
, CURLOPT_WRITEDATA
, file
);
140 res
= curl_easy_perform(curl
);
144 curl_easy_getinfo(curl
, CURLINFO_RESPONSE_CODE
,
146 opkg_msg(hide_error
? DEBUG2
: ERROR
,
147 "Failed to download %s: %s.\n", src
,
148 curl_easy_strerror(res
));
149 free(tmp_file_location
);
154 free(tmp_file_location
);
165 if (conf
->http_proxy
|| conf
->ftp_proxy
) {
170 argv
[i
++] = tmp_file_location
;
177 "Failed to download %s, wget returned %d.\n",
181 "Check your network settings and connectivity.\n\n");
182 free(tmp_file_location
);
188 err
= file_move(tmp_file_location
, dest_file_name
);
190 free(tmp_file_location
);
196 opkg_download_cache(const char *src
, const char *dest_file_name
,
197 curl_progress_func cb
, void *data
)
199 char *cache_name
= xstrdup(src
);
200 char *cache_location
, *p
;
203 if (!conf
->cache
|| str_starts_with(src
, "file:")) {
204 err
= opkg_download(src
, dest_file_name
, cb
, data
, 0);
208 if (!file_is_dir(conf
->cache
)) {
209 opkg_msg(ERROR
, "%s is not a directory.\n", conf
->cache
);
214 for (p
= cache_name
; *p
; p
++)
216 *p
= ','; /* looks nicer than | or # */
218 sprintf_alloc(&cache_location
, "%s/%s", conf
->cache
, cache_name
);
219 if (file_exists(cache_location
))
220 opkg_msg(NOTICE
, "Copying %s.\n", cache_location
);
222 /* cache file with funky name not found, try simple name */
224 char *filename
= strrchr(dest_file_name
, '/');
226 cache_name
= xstrdup(filename
+ 1); // strip leading '/'
228 cache_name
= xstrdup(dest_file_name
);
229 free(cache_location
);
230 sprintf_alloc(&cache_location
, "%s/%s", conf
->cache
,
232 if (file_exists(cache_location
))
233 opkg_msg(NOTICE
, "Copying %s.\n", cache_location
);
235 err
= opkg_download(src
, cache_location
, cb
, data
, 0);
237 (void)unlink(cache_location
);
243 err
= file_copy(cache_location
, dest_file_name
);
246 free(cache_location
);
252 int opkg_download_pkg(pkg_t
* pkg
, const char *dir
)
256 char *local_filename
;
257 char *stripped_filename
;
260 if (pkg
->src
== NULL
) {
262 "Package %s is not available from any configured src.\n",
267 filename
= pkg_get_string(pkg
, PKG_FILENAME
);
269 if (filename
== NULL
) {
271 "Package %s does not have a valid filename field.\n",
276 sprintf_alloc(&url
, "%s/%s", pkg
->src
->value
, filename
);
278 /* The filename might be something like
279 "../../foo.opk". While this is correct, and exactly what we
280 want to use to construct url above, here we actually need to
281 use just the filename part, without any directory. */
283 stripped_filename
= strrchr(filename
, '/');
284 if (!stripped_filename
)
285 stripped_filename
= filename
;
287 sprintf_alloc(&local_filename
, "%s/%s", dir
, stripped_filename
);
288 pkg_set_string(pkg
, PKG_LOCAL_FILENAME
, local_filename
);
290 err
= opkg_download_cache(url
, local_filename
, NULL
, NULL
);
297 * Downloads file from url, installs in package database, return package name.
299 int opkg_prepare_url_for_install(const char *url
, char **namep
)
306 if (str_starts_with(url
, "http://")
307 || str_starts_with(url
, "ftp://")) {
309 char *file_basec
= xstrdup(url
);
310 char *file_base
= basename(file_basec
);
312 sprintf_alloc(&tmp_file
, "%s/%s", conf
->tmp_dir
, file_base
);
313 err
= opkg_download(url
, tmp_file
, NULL
, NULL
, 0);
317 err
= pkg_init_from_file(pkg
, tmp_file
);
324 } else if (strcmp(&url
[strlen(url
) - 4], OPKG_PKG_EXTENSION
) == 0
325 || strcmp(&url
[strlen(url
) - 4], IPKG_PKG_EXTENSION
) == 0
326 || strcmp(&url
[strlen(url
) - 4], DPKG_PKG_EXTENSION
) == 0) {
328 err
= pkg_init_from_file(pkg
, url
);
331 opkg_msg(DEBUG2
, "Package %s provided by hand (%s).\n",
332 pkg
->name
, pkg_get_string(pkg
, PKG_LOCAL_FILENAME
));
333 pkg
->provided_by_hand
= 1;
341 pkg
->dest
= conf
->default_dest
;
342 pkg
->state_want
= SW_INSTALL
;
343 pkg
->state_flag
|= SF_PREFER
;
344 hash_insert_pkg(pkg
, 1);
347 *namep
= xstrdup(pkg
->name
);
352 int opkg_verify_file(char *text_file
, char *sig_file
)
354 #if defined HAVE_USIGN
358 if (conf
->check_signature
== 0)
366 execl("/usr/sbin/opkg-key", "opkg-key", "verify", sig_file
,
371 waitpid(pid
, &status
, 0);
372 if (!WIFEXITED(status
) || WEXITSTATUS(status
))
376 #elif defined HAVE_GPGME
377 if (conf
->check_signature
== 0)
381 gpgme_data_t sig
, text
, key
;
383 gpgme_verify_result_t result
;
385 char *trusted_path
= NULL
;
387 gpgme_check_version(NULL
);
389 err
= gpgme_new(&ctx
);
394 sprintf_alloc(&trusted_path
, "%s/%s", conf
->offline_root
,
395 "/etc/opkg/trusted.gpg");
396 err
= gpgme_data_new_from_file(&key
, trusted_path
, 1);
401 err
= gpgme_op_import(ctx
, key
);
403 gpgme_data_release(key
);
406 gpgme_data_release(key
);
408 err
= gpgme_data_new_from_file(&sig
, sig_file
, 1);
414 err
= gpgme_data_new_from_file(&text
, text_file
, 1);
416 gpgme_data_release(sig
);
421 err
= gpgme_op_verify(ctx
, sig
, text
, NULL
);
423 result
= gpgme_op_verify_result(ctx
);
427 /* see if any of the signitures matched */
428 s
= result
->signatures
;
430 status
= gpg_err_code(s
->status
);
431 if (status
== GPG_ERR_NO_ERROR
)
436 gpgme_data_release(sig
);
437 gpgme_data_release(text
);
441 #elif defined HAVE_OPENSSL
442 X509_STORE
*store
= NULL
;
444 BIO
*in
= NULL
, *indata
= NULL
;
446 // Sig check failed by default !
451 // Set-up the key store
454 setup_verify(conf
->signature_ca_file
, conf
->signature_ca_path
))) {
455 opkg_msg(ERROR
, "Can't open CA certificates.\n");
456 goto verify_file_end
;
458 // Open a BIO to read the sig file
459 if (!(in
= BIO_new_file(sig_file
, "rb"))) {
460 opkg_msg(ERROR
, "Can't open signature file %s.\n", sig_file
);
461 goto verify_file_end
;
463 // Read the PKCS7 block contained in the sig file
464 p7
= PEM_read_bio_PKCS7(in
, NULL
, NULL
, NULL
);
466 opkg_msg(ERROR
, "Can't read signature file %s (Corrupted ?).\n",
468 goto verify_file_end
;
470 #if defined(HAVE_PATHFINDER)
471 if (conf
->check_x509_path
) {
472 if (!pkcs7_pathfinder_verify_signers(p7
)) {
473 opkg_msg(ERROR
, "pkcs7_pathfinder_verify_signers: "
474 "Path verification failed.\n");
475 goto verify_file_end
;
480 // Open the Package file to authenticate
481 if (!(indata
= BIO_new_file(text_file
, "rb"))) {
482 opkg_msg(ERROR
, "Can't open file %s.\n", text_file
);
483 goto verify_file_end
;
485 // Let's verify the autenticity !
486 if (PKCS7_verify(p7
, NULL
, store
, indata
, NULL
, PKCS7_BINARY
) != 1) {
488 opkg_msg(ERROR
, "Verification failure.\n");
498 X509_STORE_free(store
);
502 /* mute `unused variable' warnings. */
510 #if defined(HAVE_OPENSSL) || defined(HAVE_SSLCURL)
511 static void openssl_init(void)
516 OPENSSL_config(NULL
);
517 OpenSSL_add_all_algorithms();
518 ERR_load_crypto_strings();
525 #if defined HAVE_OPENSSL
526 static X509_STORE
*setup_verify(char *CAfile
, char *CApath
)
528 X509_STORE
*store
= NULL
;
529 X509_LOOKUP
*lookup
= NULL
;
531 if (!(store
= X509_STORE_new())) {
532 // Something bad is happening...
535 // adds the X509 file lookup method
536 lookup
= X509_STORE_add_lookup(store
, X509_LOOKUP_file());
537 if (lookup
== NULL
) {
540 // Autenticating against one CA file
542 if (!X509_LOOKUP_load_file(lookup
, CAfile
, X509_FILETYPE_PEM
)) {
543 // Invalid CA => Bye bye
544 opkg_msg(ERROR
, "Error loading file %s.\n", CAfile
);
548 X509_LOOKUP_load_file(lookup
, NULL
, X509_FILETYPE_DEFAULT
);
551 // Now look into CApath directory if supplied
552 lookup
= X509_STORE_add_lookup(store
, X509_LOOKUP_hash_dir());
553 if (lookup
== NULL
) {
558 if (!X509_LOOKUP_add_dir(lookup
, CApath
, X509_FILETYPE_PEM
)) {
559 opkg_msg(ERROR
, "Error loading directory %s.\n",
564 X509_LOOKUP_add_dir(lookup
, NULL
, X509_FILETYPE_DEFAULT
);
573 X509_STORE_free(store
);
581 void opkg_curl_cleanup(void)
584 curl_easy_cleanup(curl
);
589 static CURL
*opkg_curl_init(curl_progress_func cb
, void *data
)
593 curl
= curl_easy_init();
598 if (conf
->ssl_engine
) {
600 /* use crypto engine */
602 (curl
, CURLOPT_SSLENGINE
,
603 conf
->ssl_engine
) != CURLE_OK
) {
605 "Can't set crypto engine '%s'.\n",
611 /* set the crypto engine as default */
613 (curl
, CURLOPT_SSLENGINE_DEFAULT
, 1L) != CURLE_OK
) {
615 "Can't set crypto engine '%s' as default.\n",
623 /* cert & key can only be in PEM case in the same file */
624 if (conf
->ssl_key_passwd
) {
626 (curl
, CURLOPT_SSLKEYPASSWD
,
627 conf
->ssl_key_passwd
) != CURLE_OK
) {
629 "Failed to set key password.\n");
633 /* sets the client certificate and its type */
634 if (conf
->ssl_cert_type
) {
636 (curl
, CURLOPT_SSLCERTTYPE
,
637 conf
->ssl_cert_type
) != CURLE_OK
) {
639 "Failed to set certificate format.\n");
642 /* SSL cert name isn't mandatory */
643 if (conf
->ssl_cert
) {
644 curl_easy_setopt(curl
, CURLOPT_SSLCERT
, conf
->ssl_cert
);
647 /* sets the client key and its type */
648 if (conf
->ssl_key_type
) {
650 (curl
, CURLOPT_SSLKEYTYPE
,
651 conf
->ssl_key_type
) != CURLE_OK
) {
652 opkg_msg(DEBUG
, "Failed to set key format.\n");
657 (curl
, CURLOPT_SSLKEY
, conf
->ssl_key
) != CURLE_OK
) {
658 opkg_msg(DEBUG
, "Failed to set key.\n");
662 /* Should we verify the peer certificate ? */
663 if (conf
->ssl_dont_verify_peer
) {
665 * CURLOPT_SSL_VERIFYPEER default is nonzero (curl => 7.10)
667 curl_easy_setopt(curl
, CURLOPT_SSL_VERIFYPEER
, 0);
669 #ifdef HAVE_PATHFINDER
670 if (conf
->check_x509_path
) {
672 (curl
, CURLOPT_SSL_CTX_FUNCTION
,
673 curl_ssl_ctx_function
) != CURLE_OK
) {
675 "Failed to set ssl path verification callback.\n");
677 curl_easy_setopt(curl
,
678 CURLOPT_SSL_CTX_DATA
,
685 /* certification authority file and/or path */
686 if (conf
->ssl_ca_file
) {
687 curl_easy_setopt(curl
, CURLOPT_CAINFO
,
690 if (conf
->ssl_ca_path
) {
691 curl_easy_setopt(curl
, CURLOPT_CAPATH
,
696 curl_easy_setopt(curl
, CURLOPT_FOLLOWLOCATION
, 1);
697 curl_easy_setopt(curl
, CURLOPT_FAILONERROR
, 1);
698 if (conf
->http_proxy
|| conf
->ftp_proxy
) {
700 sprintf_alloc(&userpwd
, "%s:%s", conf
->proxy_user
,
702 curl_easy_setopt(curl
, CURLOPT_PROXYUSERPWD
, userpwd
);
707 curl_easy_setopt(curl
, CURLOPT_NOPROGRESS
, (cb
== NULL
));
709 curl_easy_setopt(curl
, CURLOPT_PROGRESSDATA
, data
);
710 curl_easy_setopt(curl
, CURLOPT_PROGRESSFUNCTION
, cb
);