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"
50 #include "opkg_defines.h"
51 #include "libbb/libbb.h"
53 #ifdef HAVE_PATHFINDER
54 #include "opkg_pathfinder.h"
57 #if defined(HAVE_OPENSSL) || defined(HAVE_SSLCURL)
58 static void openssl_init(void);
62 static X509_STORE *setup_verify(opkg_conf_t *conf, char *CAfile, char *CApath);
67 * Make curl an instance variable so we don't have to instanciate it
70 static CURL *curl = NULL;
71 static void opkg_curl_cleanup(void);
72 static CURL *opkg_curl_init(opkg_conf_t *conf, curl_progress_func cb, void *data);
75 int opkg_download(opkg_conf_t *conf, const char *src,
76 const char *dest_file_name, curl_progress_func cb, void *data)
80 char *src_basec = xstrdup(src);
81 char *src_base = basename(src_basec);
82 char *tmp_file_location;
84 opkg_message(conf,OPKG_NOTICE,"Downloading %s\n", src);
86 if (str_starts_with(src, "file:")) {
88 const char *file_src = src + 5;
89 opkg_message(conf,OPKG_INFO,"Copying %s to %s...", file_src, dest_file_name);
90 ret = file_copy(src + 5, dest_file_name);
91 opkg_message(conf,OPKG_INFO,"Done\n");
96 sprintf_alloc(&tmp_file_location, "%s/%s", conf->tmp_dir, src_base);
97 err = unlink(tmp_file_location);
98 if (err && errno != ENOENT) {
99 opkg_message(conf,OPKG_ERROR, "%s: ERROR: failed to unlink %s: %s\n",
100 __FUNCTION__, tmp_file_location, strerror(errno));
101 free(tmp_file_location);
106 if (conf->http_proxy) {
107 opkg_message(conf,OPKG_DEBUG,"Setting environment variable: http_proxy = %s\n", conf->http_proxy);
108 setenv("http_proxy", conf->http_proxy, 1);
110 if (conf->ftp_proxy) {
111 opkg_message(conf,OPKG_DEBUG,"Setting environment variable: ftp_proxy = %s\n", conf->ftp_proxy);
112 setenv("ftp_proxy", conf->ftp_proxy, 1);
114 if (conf->no_proxy) {
115 opkg_message(conf,OPKG_DEBUG,"Setting environment variable: no_proxy = %s\n", conf->no_proxy);
116 setenv("no_proxy", conf->no_proxy, 1);
122 FILE * file = fopen (tmp_file_location, "w");
124 curl = opkg_curl_init (conf, cb, data);
127 curl_easy_setopt (curl, CURLOPT_URL, src);
128 curl_easy_setopt (curl, CURLOPT_WRITEDATA, file);
130 res = curl_easy_perform (curl);
135 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &error_code);
136 opkg_message(conf, OPKG_ERROR, "Failed to download %s. \nerror detail: %s\n", src, curl_easy_strerror(res));
137 free(tmp_file_location);
145 free(tmp_file_location);
154 wgetopts = getenv("OPKG_WGETOPTS");
155 sprintf_alloc(&wgetcmd, "wget -q %s%s -O \"%s\" \"%s\"",
156 (conf->http_proxy || conf->ftp_proxy) ? "-Y on " : "",
157 (wgetopts!=NULL) ? wgetopts : "",
158 tmp_file_location, src);
159 opkg_message(conf, OPKG_INFO, "Executing: %s\n", wgetcmd);
160 res = xsystem(wgetcmd);
163 opkg_message(conf, OPKG_ERROR, "Failed to download %s, error %d\n", src, res);
164 free(tmp_file_location);
171 err = file_move(tmp_file_location, dest_file_name);
173 free(tmp_file_location);
183 static int opkg_download_cache(opkg_conf_t *conf, const char *src,
184 const char *dest_file_name, curl_progress_func cb, void *data)
186 char *cache_name = xstrdup(src);
187 char *cache_location, *p;
190 if (!conf->cache || str_starts_with(src, "file:")) {
191 err = opkg_download(conf, src, dest_file_name, cb, data);
195 for (p = cache_name; *p; p++)
197 *p = ','; /* looks nicer than | or # */
199 sprintf_alloc(&cache_location, "%s/%s", conf->cache, cache_name);
200 if (file_exists(cache_location))
201 opkg_message(conf, OPKG_NOTICE, "Copying %s\n", cache_location);
203 err = opkg_download(conf, src, cache_location, cb, data);
205 (void) unlink(cache_location);
210 err = file_copy(cache_location, dest_file_name);
214 free(cache_location);
220 int opkg_download_pkg(opkg_conf_t *conf, pkg_t *pkg, const char *dir)
224 char *stripped_filename;
226 if (pkg->src == NULL) {
227 opkg_message(conf,OPKG_ERROR, "ERROR: Package %s (parent %s) is not available from any configured src.\n",
228 pkg->name, pkg->parent->name);
231 if (pkg->filename == NULL) {
232 opkg_message(conf,OPKG_ERROR, "ERROR: Package %s (parent %s) does not have a valid filename field.\n",pkg->name, pkg->parent->name);
236 sprintf_alloc(&url, "%s/%s", pkg->src->value, pkg->filename);
238 /* XXX: BUG: The pkg->filename might be something like
239 "../../foo.opk". While this is correct, and exactly what we
240 want to use to construct url above, here we actually need to
241 use just the filename part, without any directory. */
243 stripped_filename = strrchr(pkg->filename, '/');
244 if ( ! stripped_filename )
245 stripped_filename = pkg->filename;
247 sprintf_alloc(&pkg->local_filename, "%s/%s", dir, stripped_filename);
249 err = opkg_download_cache(conf, url, pkg->local_filename, NULL, NULL);
256 * Downloads file from url, installs in package database, return package name.
258 int opkg_prepare_url_for_install(opkg_conf_t *conf, const char *url, char **namep)
266 if (str_starts_with(url, "http://")
267 || str_starts_with(url, "ftp://")) {
269 char *file_basec = xstrdup(url);
270 char *file_base = basename(file_basec);
272 sprintf_alloc(&tmp_file, "%s/%s", conf->tmp_dir, file_base);
273 err = opkg_download(conf, url, tmp_file, NULL, NULL);
277 err = pkg_init_from_file(pkg, tmp_file);
284 } else if (strcmp(&url[strlen(url) - 4], OPKG_PKG_EXTENSION) == 0
285 || strcmp(&url[strlen(url) - 4], IPKG_PKG_EXTENSION) == 0
286 || strcmp(&url[strlen(url) - 4], DPKG_PKG_EXTENSION) == 0) {
288 err = pkg_init_from_file(pkg, url);
291 opkg_message(conf, OPKG_DEBUG2, "Package %s provided by hand (%s).\n", pkg->name,pkg->local_filename);
292 pkg->provided_by_hand = 1;
300 if (!pkg->architecture) {
301 opkg_message(conf, OPKG_ERROR, "Package %s has no Architecture defined.\n", pkg->name);
305 pkg->dest = conf->default_dest;
306 pkg->state_want = SW_INSTALL;
307 pkg->state_flag |= SF_PREFER;
308 pkg = hash_insert_pkg(&conf->pkg_hash, pkg, 1,conf);
310 fprintf(stderr, "%s : This should never happen. Report this Bug in bugzilla please \n ",__FUNCTION__);
314 *namep = xstrdup(pkg->name);
320 opkg_verify_file (opkg_conf_t *conf, char *text_file, char *sig_file)
322 #if defined HAVE_GPGME
323 if (conf->check_signature == 0 )
327 gpgme_data_t sig, text, key;
328 gpgme_error_t err = -1;
329 gpgme_verify_result_t result;
331 char *trusted_path = NULL;
333 err = gpgme_new (&ctx);
338 sprintf_alloc(&trusted_path, "%s/%s", conf->offline_root, "/etc/opkg/trusted.gpg");
339 err = gpgme_data_new_from_file (&key, trusted_path, 1);
345 err = gpgme_op_import (ctx, key);
348 gpgme_data_release (key);
351 gpgme_data_release (key);
353 err = gpgme_data_new_from_file (&sig, sig_file, 1);
360 err = gpgme_data_new_from_file (&text, text_file, 1);
363 gpgme_data_release (sig);
368 err = gpgme_op_verify (ctx, sig, text, NULL);
370 result = gpgme_op_verify_result (ctx);
374 /* see if any of the signitures matched */
375 s = result->signatures;
378 status = gpg_err_code (s->status);
379 if (status == GPG_ERR_NO_ERROR)
385 gpgme_data_release (sig);
386 gpgme_data_release (text);
390 #elif defined HAVE_OPENSSL
391 X509_STORE *store = NULL;
393 BIO *in = NULL, *indata = NULL;
395 // Sig check failed by default !
400 // Set-up the key store
401 if(!(store = setup_verify(conf, conf->signature_ca_file, conf->signature_ca_path))){
402 opkg_message(conf, OPKG_ERROR,
403 "Can't open CA certificates\n");
404 goto verify_file_end;
407 // Open a BIO to read the sig file
408 if (!(in = BIO_new_file(sig_file, "rb"))){
409 opkg_message(conf, OPKG_ERROR,
410 "Can't open signature file %s\n", sig_file);
411 goto verify_file_end;
414 // Read the PKCS7 block contained in the sig file
415 p7 = PEM_read_bio_PKCS7(in, NULL, NULL, NULL);
417 opkg_message(conf, OPKG_ERROR,
418 "Can't read signature file (Corrupted ?)\n");
419 goto verify_file_end;
421 #if defined(HAVE_PATHFINDER)
422 if(conf->check_x509_path){
423 if(!pkcs7_pathfinder_verify_signers(p7)){
424 opkg_message(conf, OPKG_ERROR, "pkcs7_pathfinder_verify_signers: "
425 "Path verification failed\n");
426 goto verify_file_end;
431 // Open the Package file to authenticate
432 if (!(indata = BIO_new_file(text_file, "rb"))){
433 opkg_message(conf, OPKG_ERROR,
434 "Can't open file %s\n", text_file);
435 goto verify_file_end;
438 // Let's verify the autenticity !
439 if (PKCS7_verify(p7, NULL, store, indata, NULL, PKCS7_BINARY) != 1){
441 opkg_message(conf, OPKG_ERROR,
442 "Verification failure\n");
452 X509_STORE_free(store);
456 /* mute `unused variable' warnings. */
465 #if defined(HAVE_OPENSSL) || defined(HAVE_SSLCURL)
466 static void openssl_init(void){
470 OPENSSL_config(NULL);
471 OpenSSL_add_all_algorithms();
472 ERR_load_crypto_strings();
480 #if defined HAVE_OPENSSL
481 static X509_STORE *setup_verify(opkg_conf_t *conf, char *CAfile, char *CApath){
482 X509_STORE *store = NULL;
483 X509_LOOKUP *lookup = NULL;
485 if(!(store = X509_STORE_new())){
486 // Something bad is happening...
490 // adds the X509 file lookup method
491 lookup = X509_STORE_add_lookup(store,X509_LOOKUP_file());
496 // Autenticating against one CA file
498 if(!X509_LOOKUP_load_file(lookup,CAfile,X509_FILETYPE_PEM)) {
499 // Invalid CA => Bye bye
500 opkg_message(conf, OPKG_ERROR,
501 "Error loading file %s\n", CAfile);
505 X509_LOOKUP_load_file(lookup,NULL,X509_FILETYPE_DEFAULT);
508 // Now look into CApath directory if supplied
509 lookup = X509_STORE_add_lookup(store,X509_LOOKUP_hash_dir());
515 if(!X509_LOOKUP_add_dir(lookup,CApath,X509_FILETYPE_PEM)) {
516 opkg_message(conf, OPKG_ERROR,
517 "Error loading directory %s\n", CApath);
521 X509_LOOKUP_add_dir(lookup,NULL,X509_FILETYPE_DEFAULT);
530 X509_STORE_free(store);
538 static void opkg_curl_cleanup(void){
540 curl_easy_cleanup (curl);
545 static CURL *opkg_curl_init(opkg_conf_t *conf, curl_progress_func cb, void *data){
548 curl = curl_easy_init();
553 if (conf->ssl_engine) {
555 /* use crypto engine */
556 if (curl_easy_setopt(curl, CURLOPT_SSLENGINE, conf->ssl_engine) != CURLE_OK){
557 opkg_message(conf, OPKG_ERROR, "can't set crypto engine: '%s'\n",
563 /* set the crypto engine as default */
564 if (curl_easy_setopt(curl, CURLOPT_SSLENGINE_DEFAULT, 1L) != CURLE_OK){
565 opkg_message(conf, OPKG_ERROR, "can't set crypto engine as default\n");
572 /* cert & key can only be in PEM case in the same file */
573 if(conf->ssl_key_passwd){
574 if (curl_easy_setopt(curl, CURLOPT_SSLKEYPASSWD, conf->ssl_key_passwd) != CURLE_OK)
576 opkg_message(conf, OPKG_DEBUG, "Failed to set key password\n");
580 /* sets the client certificate and its type */
581 if(conf->ssl_cert_type){
582 if (curl_easy_setopt(curl, CURLOPT_SSLCERTTYPE, conf->ssl_cert_type) != CURLE_OK)
584 opkg_message(conf, OPKG_DEBUG, "Failed to set certificate format\n");
587 /* SSL cert name isn't mandatory */
589 curl_easy_setopt(curl, CURLOPT_SSLCERT, conf->ssl_cert);
592 /* sets the client key and its type */
593 if(conf->ssl_key_type){
594 if (curl_easy_setopt(curl, CURLOPT_SSLKEYTYPE, conf->ssl_key_type) != CURLE_OK)
596 opkg_message(conf, OPKG_DEBUG, "Failed to set key format\n");
600 if (curl_easy_setopt(curl, CURLOPT_SSLKEY, conf->ssl_key) != CURLE_OK)
602 opkg_message(conf, OPKG_DEBUG, "Failed to set key\n");
606 /* Should we verify the peer certificate ? */
607 if(conf->ssl_dont_verify_peer){
609 * CURLOPT_SSL_VERIFYPEER default is nonzero (curl => 7.10)
611 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
613 #ifdef HAVE_PATHFINDER
614 if(conf->check_x509_path){
615 if (curl_easy_setopt(curl, CURLOPT_SSL_CTX_FUNCTION, curl_ssl_ctx_function) != CURLE_OK){
616 opkg_message(conf, OPKG_DEBUG, "Failed to set ssl path verification callback\n");
618 curl_easy_setopt(curl, CURLOPT_SSL_CTX_DATA, NULL);
624 /* certification authority file and/or path */
625 if(conf->ssl_ca_file){
626 curl_easy_setopt(curl, CURLOPT_CAINFO, conf->ssl_ca_file);
628 if(conf->ssl_ca_path){
629 curl_easy_setopt(curl, CURLOPT_CAPATH, conf->ssl_ca_path);
633 curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1);
634 curl_easy_setopt (curl, CURLOPT_FAILONERROR, 1);
635 if (conf->http_proxy || conf->ftp_proxy)
638 sprintf_alloc (&userpwd, "%s:%s", conf->proxy_user,
640 curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, userpwd);
644 /* add curl cleanup callback */
645 if(!atexit(opkg_curl_cleanup)){
646 opkg_message(conf,OPKG_DEBUG, "Failed to register atexit curl cleanup function\n");
651 curl_easy_setopt (curl, CURLOPT_NOPROGRESS, (cb == NULL));
654 curl_easy_setopt (curl, CURLOPT_PROGRESSDATA, data);
655 curl_easy_setopt (curl, CURLOPT_PROGRESSFUNCTION, cb);