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.
26 #include "opkg_download.h"
27 #include "opkg_message.h"
29 #include "sprintf_alloc.h"
31 #include "file_util.h"
32 #include "opkg_defines.h"
33 #include "libbb/libbb.h"
36 #include <curl/curl.h>
39 #if defined(HAVE_SSLCURL) || defined(HAVE_OPENSSL)
40 #include <openssl/conf.h>
41 #include <openssl/evp.h>
42 #include <openssl/err.h>
43 #include <openssl/ssl.h>
46 #if defined(HAVE_GPGME)
48 #elif defined(HAVE_OPENSSL)
49 #include <openssl/bio.h>
50 #include <openssl/objects.h>
51 #include <openssl/x509.h>
52 #include <openssl/pem.h>
53 #include <openssl/hmac.h>
56 #ifdef HAVE_PATHFINDER
57 #include "opkg_pathfinder.h"
60 #if defined(HAVE_OPENSSL) || defined(HAVE_SSLCURL)
61 static void openssl_init(void);
65 static X509_STORE *setup_verify(char *CAfile, char *CApath);
70 * Make curl an instance variable so we don't have to instanciate it
73 static CURL *curl = NULL;
74 static CURL *opkg_curl_init(curl_progress_func cb, void *data);
78 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)
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) {
114 opkg_msg(DEBUG, "Setting environment variable: http_proxy = %s.\n",
116 setenv("http_proxy", conf->http_proxy, 1);
118 if (conf->ftp_proxy) {
119 opkg_msg(DEBUG, "Setting environment variable: ftp_proxy = %s.\n",
121 setenv("ftp_proxy", conf->ftp_proxy, 1);
123 if (conf->no_proxy) {
124 opkg_msg(DEBUG,"Setting environment variable: no_proxy = %s.\n",
126 setenv("no_proxy", conf->no_proxy, 1);
131 FILE * file = fopen (tmp_file_location, "w");
133 curl = opkg_curl_init (cb, data);
136 curl_easy_setopt (curl, CURLOPT_URL, src);
137 curl_easy_setopt (curl, CURLOPT_WRITEDATA, file);
139 res = curl_easy_perform (curl);
144 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &error_code);
145 opkg_msg(ERROR, "Failed to download %s: %s.\n",
146 src, curl_easy_strerror(res));
147 free(tmp_file_location);
154 free(tmp_file_location);
165 if (conf->http_proxy || conf->ftp_proxy) {
170 argv[i++] = tmp_file_location;
176 opkg_msg(ERROR, "Failed to download %s, wget returned %d.\n", src, res);
177 free(tmp_file_location);
183 err = file_move(tmp_file_location, dest_file_name);
185 free(tmp_file_location);
191 opkg_download_cache(const char *src, const char *dest_file_name,
192 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(src, dest_file_name, cb, data);
203 if(!file_is_dir(conf->cache)){
204 opkg_msg(ERROR, "%s is not a directory.\n",
210 for (p = cache_name; *p; p++)
212 *p = ','; /* looks nicer than | or # */
214 sprintf_alloc(&cache_location, "%s/%s", conf->cache, cache_name);
215 if (file_exists(cache_location))
216 opkg_msg(NOTICE, "Copying %s.\n", cache_location);
218 err = opkg_download(src, cache_location, cb, data);
220 (void) unlink(cache_location);
225 err = file_copy(cache_location, dest_file_name);
229 free(cache_location);
236 opkg_download_pkg(pkg_t *pkg, const char *dir)
240 char *stripped_filename;
242 if (pkg->src == NULL) {
243 opkg_msg(ERROR, "Package %s is not available from any configured src.\n",
247 if (pkg->filename == NULL) {
248 opkg_msg(ERROR, "Package %s does not have a valid filename field.\n",
253 sprintf_alloc(&url, "%s/%s", pkg->src->value, pkg->filename);
255 /* The pkg->filename might be something like
256 "../../foo.opk". While this is correct, and exactly what we
257 want to use to construct url above, here we actually need to
258 use just the filename part, without any directory. */
260 stripped_filename = strrchr(pkg->filename, '/');
261 if ( ! stripped_filename )
262 stripped_filename = pkg->filename;
264 sprintf_alloc(&pkg->local_filename, "%s/%s", dir, stripped_filename);
266 err = opkg_download_cache(url, pkg->local_filename, NULL, NULL);
273 * Downloads file from url, installs in package database, return package name.
276 opkg_prepare_url_for_install(const char *url, char **namep)
283 if (str_starts_with(url, "http://")
284 || str_starts_with(url, "ftp://")) {
286 char *file_basec = xstrdup(url);
287 char *file_base = basename(file_basec);
289 sprintf_alloc(&tmp_file, "%s/%s", conf->tmp_dir, file_base);
290 err = opkg_download(url, tmp_file, NULL, NULL);
294 err = pkg_init_from_file(pkg, tmp_file);
301 } else if (strcmp(&url[strlen(url) - 4], OPKG_PKG_EXTENSION) == 0
302 || strcmp(&url[strlen(url) - 4], IPKG_PKG_EXTENSION) == 0
303 || strcmp(&url[strlen(url) - 4], DPKG_PKG_EXTENSION) == 0) {
305 err = pkg_init_from_file(pkg, url);
308 opkg_msg(DEBUG2, "Package %s provided by hand (%s).\n",
309 pkg->name, pkg->local_filename);
310 pkg->provided_by_hand = 1;
318 pkg->dest = conf->default_dest;
319 pkg->state_want = SW_INSTALL;
320 pkg->state_flag |= SF_PREFER;
321 hash_insert_pkg(pkg, 1);
330 opkg_verify_file (char *text_file, char *sig_file)
332 #if defined HAVE_GPGME
333 if (conf->check_signature == 0 )
337 gpgme_data_t sig, text, key;
339 gpgme_verify_result_t result;
341 char *trusted_path = NULL;
343 gpgme_check_version (NULL);
345 err = gpgme_new (&ctx);
350 sprintf_alloc(&trusted_path, "%s/%s", conf->offline_root, "/etc/opkg/trusted.gpg");
351 err = gpgme_data_new_from_file (&key, trusted_path, 1);
357 err = gpgme_op_import (ctx, key);
360 gpgme_data_release (key);
363 gpgme_data_release (key);
365 err = gpgme_data_new_from_file (&sig, sig_file, 1);
372 err = gpgme_data_new_from_file (&text, text_file, 1);
375 gpgme_data_release (sig);
380 err = gpgme_op_verify (ctx, sig, text, NULL);
382 result = gpgme_op_verify_result (ctx);
386 /* see if any of the signitures matched */
387 s = result->signatures;
390 status = gpg_err_code (s->status);
391 if (status == GPG_ERR_NO_ERROR)
397 gpgme_data_release (sig);
398 gpgme_data_release (text);
402 #elif defined HAVE_OPENSSL
403 X509_STORE *store = NULL;
405 BIO *in = NULL, *indata = NULL;
407 // Sig check failed by default !
412 // Set-up the key store
413 if(!(store = setup_verify(conf->signature_ca_file, conf->signature_ca_path))){
414 opkg_msg(ERROR, "Can't open CA certificates.\n");
415 goto verify_file_end;
418 // Open a BIO to read the sig file
419 if (!(in = BIO_new_file(sig_file, "rb"))){
420 opkg_msg(ERROR, "Can't open signature file %s.\n", sig_file);
421 goto verify_file_end;
424 // Read the PKCS7 block contained in the sig file
425 p7 = PEM_read_bio_PKCS7(in, NULL, NULL, NULL);
427 opkg_msg(ERROR, "Can't read signature file %s (Corrupted ?).\n",
429 goto verify_file_end;
431 #if defined(HAVE_PATHFINDER)
432 if(conf->check_x509_path){
433 if(!pkcs7_pathfinder_verify_signers(p7)){
434 opkg_msg(ERROR, "pkcs7_pathfinder_verify_signers: "
435 "Path verification failed.\n");
436 goto verify_file_end;
441 // Open the Package file to authenticate
442 if (!(indata = BIO_new_file(text_file, "rb"))){
443 opkg_msg(ERROR, "Can't open file %s.\n", text_file);
444 goto verify_file_end;
447 // Let's verify the autenticity !
448 if (PKCS7_verify(p7, NULL, store, indata, NULL, PKCS7_BINARY) != 1){
450 opkg_msg(ERROR, "Verification failure.\n");
460 X509_STORE_free(store);
464 /* mute `unused variable' warnings. */
473 #if defined(HAVE_OPENSSL) || defined(HAVE_SSLCURL)
474 static void openssl_init(void){
478 OPENSSL_config(NULL);
479 OpenSSL_add_all_algorithms();
480 ERR_load_crypto_strings();
488 #if defined HAVE_OPENSSL
490 setup_verify(char *CAfile, char *CApath)
492 X509_STORE *store = NULL;
493 X509_LOOKUP *lookup = NULL;
495 if(!(store = X509_STORE_new())){
496 // Something bad is happening...
500 // adds the X509 file lookup method
501 lookup = X509_STORE_add_lookup(store,X509_LOOKUP_file());
506 // Autenticating against one CA file
508 if(!X509_LOOKUP_load_file(lookup,CAfile,X509_FILETYPE_PEM)) {
509 // Invalid CA => Bye bye
510 opkg_msg(ERROR, "Error loading file %s.\n", CAfile);
514 X509_LOOKUP_load_file(lookup,NULL,X509_FILETYPE_DEFAULT);
517 // Now look into CApath directory if supplied
518 lookup = X509_STORE_add_lookup(store,X509_LOOKUP_hash_dir());
524 if(!X509_LOOKUP_add_dir(lookup,CApath,X509_FILETYPE_PEM)) {
525 opkg_msg(ERROR, "Error loading directory %s.\n", CApath);
529 X509_LOOKUP_add_dir(lookup,NULL,X509_FILETYPE_DEFAULT);
538 X509_STORE_free(store);
546 void opkg_curl_cleanup(void){
548 curl_easy_cleanup (curl);
554 opkg_curl_init(curl_progress_func cb, void *data)
558 curl = curl_easy_init();
563 if (conf->ssl_engine) {
565 /* use crypto engine */
566 if (curl_easy_setopt(curl, CURLOPT_SSLENGINE, conf->ssl_engine) != CURLE_OK){
567 opkg_msg(ERROR, "Can't set crypto engine '%s'.\n",
573 /* set the crypto engine as default */
574 if (curl_easy_setopt(curl, CURLOPT_SSLENGINE_DEFAULT, 1L) != CURLE_OK){
575 opkg_msg(ERROR, "Can't set crypto engine '%s' as default.\n",
583 /* cert & key can only be in PEM case in the same file */
584 if(conf->ssl_key_passwd){
585 if (curl_easy_setopt(curl, CURLOPT_SSLKEYPASSWD, conf->ssl_key_passwd) != CURLE_OK)
587 opkg_msg(DEBUG, "Failed to set key password.\n");
591 /* sets the client certificate and its type */
592 if(conf->ssl_cert_type){
593 if (curl_easy_setopt(curl, CURLOPT_SSLCERTTYPE, conf->ssl_cert_type) != CURLE_OK)
595 opkg_msg(DEBUG, "Failed to set certificate format.\n");
598 /* SSL cert name isn't mandatory */
600 curl_easy_setopt(curl, CURLOPT_SSLCERT, conf->ssl_cert);
603 /* sets the client key and its type */
604 if(conf->ssl_key_type){
605 if (curl_easy_setopt(curl, CURLOPT_SSLKEYTYPE, conf->ssl_key_type) != CURLE_OK)
607 opkg_msg(DEBUG, "Failed to set key format.\n");
611 if (curl_easy_setopt(curl, CURLOPT_SSLKEY, conf->ssl_key) != CURLE_OK)
613 opkg_msg(DEBUG, "Failed to set key.\n");
617 /* Should we verify the peer certificate ? */
618 if(conf->ssl_dont_verify_peer){
620 * CURLOPT_SSL_VERIFYPEER default is nonzero (curl => 7.10)
622 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
624 #ifdef HAVE_PATHFINDER
625 if(conf->check_x509_path){
626 if (curl_easy_setopt(curl, CURLOPT_SSL_CTX_FUNCTION, curl_ssl_ctx_function) != CURLE_OK){
627 opkg_msg(DEBUG, "Failed to set ssl path verification callback.\n");
629 curl_easy_setopt(curl, CURLOPT_SSL_CTX_DATA, NULL);
635 /* certification authority file and/or path */
636 if(conf->ssl_ca_file){
637 curl_easy_setopt(curl, CURLOPT_CAINFO, conf->ssl_ca_file);
639 if(conf->ssl_ca_path){
640 curl_easy_setopt(curl, CURLOPT_CAPATH, conf->ssl_ca_path);
644 curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1);
645 curl_easy_setopt (curl, CURLOPT_FAILONERROR, 1);
646 if (conf->http_proxy || conf->ftp_proxy)
649 sprintf_alloc (&userpwd, "%s:%s", conf->proxy_user,
651 curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, userpwd);
656 curl_easy_setopt (curl, CURLOPT_NOPROGRESS, (cb == NULL));
659 curl_easy_setopt (curl, CURLOPT_PROGRESSDATA, data);
660 curl_easy_setopt (curl, CURLOPT_PROGRESSFUNCTION, cb);