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>
31 #if defined(HAVE_GPGME)
33 #elif defined(HAVE_OPENSSL)
34 #include <openssl/bio.h>
35 #include <openssl/objects.h>
36 #include <openssl/x509.h>
37 #include <openssl/pem.h>
38 #include <openssl/hmac.h>
42 #include "opkg_download.h"
43 #include "opkg_message.h"
44 #include "opkg_state.h"
46 #include "sprintf_alloc.h"
48 #include "file_util.h"
50 #include "opkg_defines.h"
52 #if defined(HAVE_OPENSSL) || defined(HAVE_SSLCURL)
53 static void openssl_init(void);
57 static X509_STORE
*setup_verify(opkg_conf_t
*conf
, char *CAfile
, char *CApath
);
62 * Make curl an instance variable so we don't have to instanciate it
65 static CURL
*curl
= NULL
;
66 static void opkg_curl_cleanup(void);
67 static CURL
*opkg_curl_init(opkg_conf_t
*conf
, curl_progress_func cb
, void *data
);
70 int opkg_download(opkg_conf_t
*conf
, const char *src
,
71 const char *dest_file_name
, curl_progress_func cb
, void *data
)
75 char *src_basec
= strdup(src
);
76 char *src_base
= basename(src_basec
);
77 char *tmp_file_location
;
79 opkg_message(conf
,OPKG_NOTICE
,"Downloading %s\n", src
);
81 if (str_starts_with(src
, "file:")) {
83 const char *file_src
= src
+ 5;
84 opkg_message(conf
,OPKG_INFO
,"Copying %s to %s...", file_src
, dest_file_name
);
85 ret
= file_copy(src
+ 5, dest_file_name
);
86 opkg_message(conf
,OPKG_INFO
,"Done\n");
91 sprintf_alloc(&tmp_file_location
, "%s/%s", conf
->tmp_dir
, src_base
);
92 err
= unlink(tmp_file_location
);
93 if (err
&& errno
!= ENOENT
) {
94 opkg_message(conf
,OPKG_ERROR
, "%s: ERROR: failed to unlink %s: %s\n",
95 __FUNCTION__
, tmp_file_location
, strerror(errno
));
96 free(tmp_file_location
);
101 if (conf
->http_proxy
) {
102 opkg_message(conf
,OPKG_DEBUG
,"Setting environment variable: http_proxy = %s\n", conf
->http_proxy
);
103 setenv("http_proxy", conf
->http_proxy
, 1);
105 if (conf
->ftp_proxy
) {
106 opkg_message(conf
,OPKG_DEBUG
,"Setting environment variable: ftp_proxy = %s\n", conf
->ftp_proxy
);
107 setenv("ftp_proxy", conf
->ftp_proxy
, 1);
109 if (conf
->no_proxy
) {
110 opkg_message(conf
,OPKG_DEBUG
,"Setting environment variable: no_proxy = %s\n", conf
->no_proxy
);
111 setenv("no_proxy", conf
->no_proxy
, 1);
117 FILE * file
= fopen (tmp_file_location
, "w");
119 curl
= opkg_curl_init (conf
, cb
, data
);
122 curl_easy_setopt (curl
, CURLOPT_URL
, src
);
123 curl_easy_setopt (curl
, CURLOPT_WRITEDATA
, file
);
125 res
= curl_easy_perform (curl
);
130 curl_easy_getinfo(curl
, CURLINFO_RESPONSE_CODE
, &error_code
);
131 opkg_message(conf
, OPKG_ERROR
, "Failed to download %s. \nerror detail: %s\n", src
, curl_easy_strerror(res
));
132 free(tmp_file_location
);
140 free(tmp_file_location
);
149 wgetopts
= getenv("OPKG_WGETOPTS");
150 sprintf_alloc(&wgetcmd
, "wget -q %s%s -O \"%s\" \"%s\"",
151 (conf
->http_proxy
|| conf
->ftp_proxy
) ? "-Y on " : "",
152 (wgetopts
!=NULL
) ? wgetopts
: "",
153 tmp_file_location
, src
);
154 opkg_message(conf
, OPKG_INFO
, "Executing: %s\n", wgetcmd
);
155 res
= xsystem(wgetcmd
);
158 opkg_message(conf
, OPKG_ERROR
, "Failed to download %s, error %d\n", src
, res
);
159 free(tmp_file_location
);
166 err
= file_move(tmp_file_location
, dest_file_name
);
168 free(tmp_file_location
);
178 static int opkg_download_cache(opkg_conf_t
*conf
, const char *src
,
179 const char *dest_file_name
, curl_progress_func cb
, void *data
)
181 char *cache_name
= strdup(src
);
182 char *cache_location
, *p
;
185 if (!conf
->cache
|| str_starts_with(src
, "file:")) {
186 err
= opkg_download(conf
, src
, dest_file_name
, cb
, data
);
190 for (p
= cache_name
; *p
; p
++)
192 *p
= ','; /* looks nicer than | or # */
194 sprintf_alloc(&cache_location
, "%s/%s", conf
->cache
, cache_name
);
195 if (file_exists(cache_location
))
196 opkg_message(conf
, OPKG_NOTICE
, "Copying %s\n", cache_location
);
198 err
= opkg_download(conf
, src
, cache_location
, cb
, data
);
200 (void) unlink(cache_location
);
205 err
= file_copy(cache_location
, dest_file_name
);
209 free(cache_location
);
215 int opkg_download_pkg(opkg_conf_t
*conf
, pkg_t
*pkg
, const char *dir
)
220 char *stripped_filename
;
222 if (pkg
->src
== NULL
) {
223 opkg_message(conf
,OPKG_ERROR
, "ERROR: Package %s (parent %s) is not available from any configured src.\n",
224 pkg
->name
, pkg
->parent
->name
);
227 if (pkg
->filename
== NULL
) {
228 opkg_message(conf
,OPKG_ERROR
, "ERROR: Package %s (parent %s) does not have a valid filename field.\n",pkg
->name
, pkg
->parent
->name
);
232 sprintf_alloc (&pkgid
, "%s;%s;%s;", pkg
->name
, pkg
->version
, pkg
->architecture
);
233 opkg_set_current_state (conf
, OPKG_STATE_DOWNLOADING_PKG
, pkgid
);
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
);
252 opkg_set_current_state (conf
, OPKG_STATE_NONE
, NULL
);
257 * Downloads file from url, installs in package database, return package name.
259 int opkg_prepare_url_for_install(opkg_conf_t
*conf
, const char *url
, char **namep
)
267 if (str_starts_with(url
, "http://")
268 || str_starts_with(url
, "ftp://")) {
270 char *file_basec
= strdup(url
);
271 char *file_base
= basename(file_basec
);
273 sprintf_alloc(&tmp_file
, "%s/%s", conf
->tmp_dir
, file_base
);
274 err
= opkg_download(conf
, url
, tmp_file
, NULL
, NULL
);
278 err
= pkg_init_from_file(pkg
, tmp_file
);
285 } else if (strcmp(&url
[strlen(url
) - 4], OPKG_PKG_EXTENSION
) == 0
286 || strcmp(&url
[strlen(url
) - 4], IPKG_PKG_EXTENSION
) == 0
287 || strcmp(&url
[strlen(url
) - 4], DPKG_PKG_EXTENSION
) == 0) {
289 err
= pkg_init_from_file(pkg
, url
);
292 opkg_message(conf
, OPKG_DEBUG2
, "Package %s provided by hand (%s).\n", pkg
->name
,pkg
->local_filename
);
293 pkg
->provided_by_hand
= 1;
301 if (!pkg
->architecture
) {
302 opkg_message(conf
, OPKG_ERROR
, "Package %s has no Architecture defined.\n", pkg
->name
);
306 pkg
->dest
= conf
->default_dest
;
307 pkg
->state_want
= SW_INSTALL
;
308 pkg
->state_flag
|= SF_PREFER
;
309 pkg
= hash_insert_pkg(&conf
->pkg_hash
, pkg
, 1,conf
);
311 fprintf(stderr
, "%s : This should never happen. Report this Bug in bugzilla please \n ",__FUNCTION__
);
315 *namep
= strdup(pkg
->name
);
321 opkg_verify_file (opkg_conf_t
*conf
, char *text_file
, char *sig_file
)
323 #if defined HAVE_GPGME
324 if (conf
->check_signature
== 0 )
328 gpgme_data_t sig
, text
, key
;
329 gpgme_error_t err
= -1;
330 gpgme_verify_result_t result
;
332 char *trusted_path
= NULL
;
334 err
= gpgme_new (&ctx
);
339 sprintf_alloc(&trusted_path
, "%s/%s", conf
->offline_root
, "/etc/opkg/trusted.gpg");
340 err
= gpgme_data_new_from_file (&key
, trusted_path
, 1);
346 err
= gpgme_op_import (ctx
, key
);
349 gpgme_data_release (key
);
352 gpgme_data_release (key
);
354 err
= gpgme_data_new_from_file (&sig
, sig_file
, 1);
361 err
= gpgme_data_new_from_file (&text
, text_file
, 1);
364 gpgme_data_release (sig
);
369 err
= gpgme_op_verify (ctx
, sig
, text
, NULL
);
371 result
= gpgme_op_verify_result (ctx
);
375 /* see if any of the signitures matched */
376 s
= result
->signatures
;
379 status
= gpg_err_code (s
->status
);
380 if (status
== GPG_ERR_NO_ERROR
)
386 gpgme_data_release (sig
);
387 gpgme_data_release (text
);
391 #elif defined HAVE_OPENSSL
392 X509_STORE
*store
= NULL
;
394 BIO
*in
= NULL
, *indata
= NULL
;
396 // Sig check failed by default !
401 // Set-up the key store
402 if(!(store
= setup_verify(conf
, conf
->signature_ca_file
, conf
->signature_ca_path
))){
403 opkg_message(conf
, OPKG_ERROR
,
404 "Can't open CA certificates\n");
405 goto verify_file_end
;
408 // Open a BIO to read the sig file
409 if (!(in
= BIO_new_file(sig_file
, "rb"))){
410 opkg_message(conf
, OPKG_ERROR
,
411 "Can't open signature file %s\n", sig_file
);
412 goto verify_file_end
;
415 // Read the PKCS7 block contained in the sig file
416 p7
= PEM_read_bio_PKCS7(in
, NULL
, NULL
, NULL
);
418 opkg_message(conf
, OPKG_ERROR
,
419 "Can't read signature file (Corrupted ?)\n");
420 goto verify_file_end
;
423 // Open the Package file to authenticate
424 if (!(indata
= BIO_new_file(text_file
, "rb"))){
425 opkg_message(conf
, OPKG_ERROR
,
426 "Can't open file %s\n", text_file
);
427 goto verify_file_end
;
430 // Let's verify the autenticity !
431 if (PKCS7_verify(p7
, NULL
, store
, indata
, NULL
, PKCS7_BINARY
) != 1){
433 opkg_message(conf
, OPKG_ERROR
,
434 "Verification failure\n");
444 X509_STORE_free(store
);
448 /* mute `unused variable' warnings. */
457 #if defined(HAVE_OPENSSL) || defined(HAVE_SSLCURL)
458 static void openssl_init(void){
462 OPENSSL_config(NULL
);
463 OpenSSL_add_all_algorithms();
464 ERR_load_crypto_strings();
472 #if defined HAVE_OPENSSL
473 static X509_STORE
*setup_verify(opkg_conf_t
*conf
, char *CAfile
, char *CApath
){
474 X509_STORE
*store
= NULL
;
475 X509_LOOKUP
*lookup
= NULL
;
477 if(!(store
= X509_STORE_new())){
478 // Something bad is happening...
482 // adds the X509 file lookup method
483 lookup
= X509_STORE_add_lookup(store
,X509_LOOKUP_file());
488 // Autenticating against one CA file
490 if(!X509_LOOKUP_load_file(lookup
,CAfile
,X509_FILETYPE_PEM
)) {
491 // Invalid CA => Bye bye
492 opkg_message(conf
, OPKG_ERROR
,
493 "Error loading file %s\n", CAfile
);
497 X509_LOOKUP_load_file(lookup
,NULL
,X509_FILETYPE_DEFAULT
);
500 // Now look into CApath directory if supplied
501 lookup
= X509_STORE_add_lookup(store
,X509_LOOKUP_hash_dir());
507 if(!X509_LOOKUP_add_dir(lookup
,CApath
,X509_FILETYPE_PEM
)) {
508 opkg_message(conf
, OPKG_ERROR
,
509 "Error loading directory %s\n", CApath
);
513 X509_LOOKUP_add_dir(lookup
,NULL
,X509_FILETYPE_DEFAULT
);
522 X509_STORE_free(store
);
530 static void opkg_curl_cleanup(void){
532 curl_easy_cleanup (curl
);
537 static CURL
*opkg_curl_init(opkg_conf_t
*conf
, curl_progress_func cb
, void *data
){
540 curl
= curl_easy_init();
545 if (conf
->ssl_engine
) {
547 /* use crypto engine */
548 if (curl_easy_setopt(curl
, CURLOPT_SSLENGINE
, conf
->ssl_engine
) != CURLE_OK
){
549 opkg_message(conf
, OPKG_ERROR
, "can't set crypto engine: '%s'\n",
555 /* set the crypto engine as default */
556 if (curl_easy_setopt(curl
, CURLOPT_SSLENGINE_DEFAULT
, 1L) != CURLE_OK
){
557 opkg_message(conf
, OPKG_ERROR
, "can't set crypto engine as default\n");
564 /* cert & key can only be in PEM case in the same file */
565 if(conf
->ssl_key_passwd
){
566 if (curl_easy_setopt(curl
, CURLOPT_SSLKEYPASSWD
, conf
->ssl_key_passwd
) != CURLE_OK
)
568 opkg_message(conf
, OPKG_DEBUG
, "Failed to set key password\n");
572 /* sets the client certificate and its type */
573 if(conf
->ssl_cert_type
){
574 if (curl_easy_setopt(curl
, CURLOPT_SSLCERTTYPE
, conf
->ssl_cert_type
) != CURLE_OK
)
576 opkg_message(conf
, OPKG_DEBUG
, "Failed to set certificate format\n");
579 /* SSL cert name isn't mandatory */
581 curl_easy_setopt(curl
, CURLOPT_SSLCERT
, conf
->ssl_cert
);
584 /* sets the client key and its type */
585 if(conf
->ssl_key_type
){
586 if (curl_easy_setopt(curl
, CURLOPT_SSLKEYTYPE
, conf
->ssl_key_type
) != CURLE_OK
)
588 opkg_message(conf
, OPKG_DEBUG
, "Failed to set key format\n");
592 if (curl_easy_setopt(curl
, CURLOPT_SSLKEY
, conf
->ssl_key
) != CURLE_OK
)
594 opkg_message(conf
, OPKG_DEBUG
, "Failed to set key\n");
598 /* Should we verify the peer certificate ? */
599 if(conf
->ssl_dont_verify_peer
){
601 * CURLOPT_SSL_VERIFYPEER default is nonzero (curl => 7.10)
603 curl_easy_setopt(curl
, CURLOPT_SSL_VERIFYPEER
, 0);
606 /* certification authority file and/or path */
607 if(conf
->ssl_ca_file
){
608 curl_easy_setopt(curl
, CURLOPT_CAINFO
, conf
->ssl_ca_file
);
610 if(conf
->ssl_ca_path
){
611 curl_easy_setopt(curl
, CURLOPT_CAPATH
, conf
->ssl_ca_path
);
615 curl_easy_setopt (curl
, CURLOPT_FOLLOWLOCATION
, 1);
616 curl_easy_setopt (curl
, CURLOPT_FAILONERROR
, 1);
617 if (conf
->http_proxy
|| conf
->ftp_proxy
)
620 sprintf_alloc (&userpwd
, "%s:%s", conf
->proxy_user
,
622 curl_easy_setopt(curl
, CURLOPT_PROXYUSERPWD
, userpwd
);
626 /* add curl cleanup callback */
627 if(!atexit(opkg_curl_cleanup
)){
628 opkg_message(conf
,OPKG_DEBUG
, "Failed to register atexit curl cleanup function\n");
633 curl_easy_setopt (curl
, CURLOPT_NOPROGRESS
, (cb
== NULL
));
636 curl_easy_setopt (curl
, CURLOPT_PROGRESSDATA
, data
);
637 curl_easy_setopt (curl
, CURLOPT_PROGRESSFUNCTION
, cb
);