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 #if defined(HAVE_OPENSSL) || defined(HAVE_SSLCURL)
56 static void openssl_init(void);
60 static X509_STORE
*setup_verify(char *CAfile
, char *CApath
);
65 * Make curl an instance variable so we don't have to instanciate it
68 static CURL
*curl
= NULL
;
69 static CURL
*opkg_curl_init(curl_progress_func cb
, void *data
);
72 static int str_starts_with(const char *str
, const char *prefix
)
74 return (strncmp(str
, prefix
, strlen(prefix
)) == 0);
78 opkg_download(const char *src
, const char *dest_file_name
,
79 curl_progress_func cb
, void *data
, const short hide_error
)
83 char *src_basec
= xstrdup(src
);
84 char *src_base
= basename(src_basec
);
85 char *tmp_file_location
;
87 opkg_msg(NOTICE
, "Downloading %s\n", src
);
89 if (str_starts_with(src
, "file:")) {
90 const char *file_src
= src
+ 5;
91 opkg_msg(INFO
, "Copying %s to %s...", file_src
, dest_file_name
);
92 err
= file_copy(file_src
, dest_file_name
);
93 opkg_msg(INFO
, "Done.\n");
98 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_perror(ERROR
, "Failed to unlink %s", tmp_file_location
);
103 free(tmp_file_location
);
107 if (conf
->http_proxy
) {
109 "Setting environment variable: http_proxy = %s.\n",
111 setenv("http_proxy", conf
->http_proxy
, 1);
113 if (conf
->ftp_proxy
) {
115 "Setting environment variable: ftp_proxy = %s.\n",
117 setenv("ftp_proxy", conf
->ftp_proxy
, 1);
119 if (conf
->no_proxy
) {
121 "Setting environment variable: no_proxy = %s.\n",
123 setenv("no_proxy", conf
->no_proxy
, 1);
127 FILE *file
= fopen(tmp_file_location
, "w");
129 curl
= opkg_curl_init(cb
, data
);
131 curl_easy_setopt(curl
, CURLOPT_URL
, src
);
132 curl_easy_setopt(curl
, CURLOPT_WRITEDATA
, file
);
134 res
= curl_easy_perform(curl
);
138 curl_easy_getinfo(curl
, CURLINFO_RESPONSE_CODE
,
140 opkg_msg(hide_error
? DEBUG2
: ERROR
,
141 "Failed to download %s: %s.\n", src
,
142 curl_easy_strerror(res
));
143 free(tmp_file_location
);
148 free(tmp_file_location
);
159 if (conf
->http_proxy
|| conf
->ftp_proxy
) {
164 argv
[i
++] = tmp_file_location
;
171 "Failed to download %s, wget returned %d.\n",
175 "Check your network settings and connectivity.\n\n");
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
, 0);
202 if (!file_is_dir(conf
->cache
)) {
203 opkg_msg(ERROR
, "%s is not a directory.\n", conf
->cache
);
208 for (p
= cache_name
; *p
; p
++)
210 *p
= ','; /* looks nicer than | or # */
212 sprintf_alloc(&cache_location
, "%s/%s", conf
->cache
, cache_name
);
213 if (file_exists(cache_location
))
214 opkg_msg(NOTICE
, "Copying %s.\n", cache_location
);
216 /* cache file with funky name not found, try simple name */
218 char *filename
= strrchr(dest_file_name
, '/');
220 cache_name
= xstrdup(filename
+ 1); // strip leading '/'
222 cache_name
= xstrdup(dest_file_name
);
223 free(cache_location
);
224 sprintf_alloc(&cache_location
, "%s/%s", conf
->cache
,
226 if (file_exists(cache_location
))
227 opkg_msg(NOTICE
, "Copying %s.\n", cache_location
);
229 err
= opkg_download(src
, cache_location
, cb
, data
, 0);
231 (void)unlink(cache_location
);
237 err
= file_copy(cache_location
, dest_file_name
);
240 free(cache_location
);
246 int opkg_download_pkg(pkg_t
* pkg
, const char *dir
)
250 char *local_filename
;
251 char *stripped_filename
;
254 if (pkg
->src
== NULL
) {
256 "Package %s is not available from any configured src.\n",
261 filename
= pkg_get_string(pkg
, PKG_FILENAME
);
263 if (filename
== NULL
) {
265 "Package %s does not have a valid filename field.\n",
270 sprintf_alloc(&url
, "%s/%s", pkg
->src
->value
, filename
);
272 /* The filename might be something like
273 "../../foo.opk". While this is correct, and exactly what we
274 want to use to construct url above, here we actually need to
275 use just the filename part, without any directory. */
277 stripped_filename
= strrchr(filename
, '/');
278 if (!stripped_filename
)
279 stripped_filename
= filename
;
281 sprintf_alloc(&local_filename
, "%s/%s", dir
, stripped_filename
);
282 pkg_set_string(pkg
, PKG_LOCAL_FILENAME
, local_filename
);
284 err
= opkg_download_cache(url
, local_filename
, NULL
, NULL
);
291 * Downloads file from url, installs in package database, return package name.
293 int opkg_prepare_url_for_install(const char *url
, char **namep
)
297 abstract_pkg_t
*ab_pkg
;
301 if (str_starts_with(url
, "http://")
302 || str_starts_with(url
, "ftp://")) {
304 char *file_basec
= xstrdup(url
);
305 char *file_base
= basename(file_basec
);
307 sprintf_alloc(&tmp_file
, "%s/%s", conf
->tmp_dir
, file_base
);
308 err
= opkg_download(url
, tmp_file
, NULL
, NULL
, 0);
312 err
= pkg_init_from_file(pkg
, tmp_file
);
319 } else if (strcmp(&url
[strlen(url
) - 4], OPKG_PKG_EXTENSION
) == 0
320 || strcmp(&url
[strlen(url
) - 4], IPKG_PKG_EXTENSION
) == 0
321 || strcmp(&url
[strlen(url
) - 4], DPKG_PKG_EXTENSION
) == 0) {
323 err
= pkg_init_from_file(pkg
, url
);
326 opkg_msg(DEBUG2
, "Package %s provided by hand (%s).\n",
327 pkg
->name
, pkg_get_string(pkg
, PKG_LOCAL_FILENAME
));
328 pkg
->provided_by_hand
= 1;
331 ab_pkg
= ensure_abstract_pkg_by_name(url
);
333 if (!(ab_pkg
->state_flag
& SF_NEED_DETAIL
)) {
334 opkg_msg(DEBUG
, "applying abpkg flag to %s\n", ab_pkg
->name
);
335 ab_pkg
->state_flag
|= SF_NEED_DETAIL
;
343 pkg
->dest
= conf
->default_dest
;
344 pkg
->state_want
= SW_INSTALL
;
345 pkg
->state_flag
|= SF_PREFER
;
346 hash_insert_pkg(pkg
, 1);
349 *namep
= xstrdup(pkg
->name
);
354 int opkg_verify_file(char *text_file
, char *sig_file
)
356 #if defined HAVE_USIGN
360 if (conf
->check_signature
== 0)
368 execl("/usr/sbin/opkg-key", "opkg-key", "verify", sig_file
,
373 waitpid(pid
, &status
, 0);
374 if (!WIFEXITED(status
) || WEXITSTATUS(status
))
378 #elif defined HAVE_GPGME
379 if (conf
->check_signature
== 0)
383 gpgme_data_t sig
, text
, key
;
385 gpgme_verify_result_t result
;
387 char *trusted_path
= NULL
;
389 gpgme_check_version(NULL
);
391 err
= gpgme_new(&ctx
);
396 sprintf_alloc(&trusted_path
, "%s/%s", conf
->offline_root
,
397 "/etc/opkg/trusted.gpg");
398 err
= gpgme_data_new_from_file(&key
, trusted_path
, 1);
403 err
= gpgme_op_import(ctx
, key
);
405 gpgme_data_release(key
);
408 gpgme_data_release(key
);
410 err
= gpgme_data_new_from_file(&sig
, sig_file
, 1);
416 err
= gpgme_data_new_from_file(&text
, text_file
, 1);
418 gpgme_data_release(sig
);
423 err
= gpgme_op_verify(ctx
, sig
, text
, NULL
);
425 result
= gpgme_op_verify_result(ctx
);
429 /* see if any of the signitures matched */
430 s
= result
->signatures
;
432 status
= gpg_err_code(s
->status
);
433 if (status
== GPG_ERR_NO_ERROR
)
438 gpgme_data_release(sig
);
439 gpgme_data_release(text
);
443 #elif defined HAVE_OPENSSL
444 X509_STORE
*store
= NULL
;
446 BIO
*in
= NULL
, *indata
= NULL
;
448 // Sig check failed by default !
453 // Set-up the key store
456 setup_verify(conf
->signature_ca_file
, conf
->signature_ca_path
))) {
457 opkg_msg(ERROR
, "Can't open CA certificates.\n");
458 goto verify_file_end
;
460 // Open a BIO to read the sig file
461 if (!(in
= BIO_new_file(sig_file
, "rb"))) {
462 opkg_msg(ERROR
, "Can't open signature file %s.\n", sig_file
);
463 goto verify_file_end
;
465 // Read the PKCS7 block contained in the sig file
466 p7
= PEM_read_bio_PKCS7(in
, NULL
, NULL
, NULL
);
468 opkg_msg(ERROR
, "Can't read signature file %s (Corrupted ?).\n",
470 goto verify_file_end
;
473 // Open the Package file to authenticate
474 if (!(indata
= BIO_new_file(text_file
, "rb"))) {
475 opkg_msg(ERROR
, "Can't open file %s.\n", text_file
);
476 goto verify_file_end
;
478 // Let's verify the autenticity !
479 if (PKCS7_verify(p7
, NULL
, store
, indata
, NULL
, PKCS7_BINARY
) != 1) {
481 opkg_msg(ERROR
, "Verification failure.\n");
491 X509_STORE_free(store
);
495 /* mute `unused variable' warnings. */
503 #if defined(HAVE_OPENSSL) || defined(HAVE_SSLCURL)
504 static void openssl_init(void)
509 OPENSSL_config(NULL
);
510 OpenSSL_add_all_algorithms();
511 ERR_load_crypto_strings();
518 #if defined HAVE_OPENSSL
519 static X509_STORE
*setup_verify(char *CAfile
, char *CApath
)
521 X509_STORE
*store
= NULL
;
522 X509_LOOKUP
*lookup
= NULL
;
524 if (!(store
= X509_STORE_new())) {
525 // Something bad is happening...
528 // adds the X509 file lookup method
529 lookup
= X509_STORE_add_lookup(store
, X509_LOOKUP_file());
530 if (lookup
== NULL
) {
533 // Autenticating against one CA file
535 if (!X509_LOOKUP_load_file(lookup
, CAfile
, X509_FILETYPE_PEM
)) {
536 // Invalid CA => Bye bye
537 opkg_msg(ERROR
, "Error loading file %s.\n", CAfile
);
541 X509_LOOKUP_load_file(lookup
, NULL
, X509_FILETYPE_DEFAULT
);
544 // Now look into CApath directory if supplied
545 lookup
= X509_STORE_add_lookup(store
, X509_LOOKUP_hash_dir());
546 if (lookup
== NULL
) {
551 if (!X509_LOOKUP_add_dir(lookup
, CApath
, X509_FILETYPE_PEM
)) {
552 opkg_msg(ERROR
, "Error loading directory %s.\n",
557 X509_LOOKUP_add_dir(lookup
, NULL
, X509_FILETYPE_DEFAULT
);
566 X509_STORE_free(store
);
574 void opkg_curl_cleanup(void)
577 curl_easy_cleanup(curl
);
582 static CURL
*opkg_curl_init(curl_progress_func cb
, void *data
)
586 curl
= curl_easy_init();
591 if (conf
->ssl_engine
) {
593 /* use crypto engine */
595 (curl
, CURLOPT_SSLENGINE
,
596 conf
->ssl_engine
) != CURLE_OK
) {
598 "Can't set crypto engine '%s'.\n",
604 /* set the crypto engine as default */
606 (curl
, CURLOPT_SSLENGINE_DEFAULT
, 1L) != CURLE_OK
) {
608 "Can't set crypto engine '%s' as default.\n",
616 /* cert & key can only be in PEM case in the same file */
617 if (conf
->ssl_key_passwd
) {
619 (curl
, CURLOPT_SSLKEYPASSWD
,
620 conf
->ssl_key_passwd
) != CURLE_OK
) {
622 "Failed to set key password.\n");
626 /* sets the client certificate and its type */
627 if (conf
->ssl_cert_type
) {
629 (curl
, CURLOPT_SSLCERTTYPE
,
630 conf
->ssl_cert_type
) != CURLE_OK
) {
632 "Failed to set certificate format.\n");
635 /* SSL cert name isn't mandatory */
636 if (conf
->ssl_cert
) {
637 curl_easy_setopt(curl
, CURLOPT_SSLCERT
, conf
->ssl_cert
);
640 /* sets the client key and its type */
641 if (conf
->ssl_key_type
) {
643 (curl
, CURLOPT_SSLKEYTYPE
,
644 conf
->ssl_key_type
) != CURLE_OK
) {
645 opkg_msg(DEBUG
, "Failed to set key format.\n");
650 (curl
, CURLOPT_SSLKEY
, conf
->ssl_key
) != CURLE_OK
) {
651 opkg_msg(DEBUG
, "Failed to set key.\n");
655 /* Should we verify the peer certificate ? */
656 if (conf
->ssl_dont_verify_peer
) {
658 * CURLOPT_SSL_VERIFYPEER default is nonzero (curl => 7.10)
660 curl_easy_setopt(curl
, CURLOPT_SSL_VERIFYPEER
, 0);
663 /* certification authority file and/or path */
664 if (conf
->ssl_ca_file
) {
665 curl_easy_setopt(curl
, CURLOPT_CAINFO
,
668 if (conf
->ssl_ca_path
) {
669 curl_easy_setopt(curl
, CURLOPT_CAPATH
,
674 curl_easy_setopt(curl
, CURLOPT_FOLLOWLOCATION
, 1);
675 curl_easy_setopt(curl
, CURLOPT_FAILONERROR
, 1);
676 if (conf
->http_proxy
|| conf
->ftp_proxy
) {
678 sprintf_alloc(&userpwd
, "%s:%s", conf
->proxy_user
,
680 curl_easy_setopt(curl
, CURLOPT_PROXYUSERPWD
, userpwd
);
685 curl_easy_setopt(curl
, CURLOPT_NOPROGRESS
, (cb
== NULL
));
687 curl_easy_setopt(curl
, CURLOPT_PROGRESSDATA
, data
);
688 curl_easy_setopt(curl
, CURLOPT_PROGRESSFUNCTION
, cb
);