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_OPENSSL)
46 #include <openssl/bio.h>
47 #include <openssl/objects.h>
48 #include <openssl/x509.h>
49 #include <openssl/pem.h>
50 #include <openssl/hmac.h>
53 #if defined(HAVE_OPENSSL) || defined(HAVE_SSLCURL)
54 static void openssl_init(void);
58 static X509_STORE *setup_verify(char *CAfile, char *CApath);
63 * Make curl an instance variable so we don't have to instanciate it
66 static CURL *curl = NULL;
67 static CURL *opkg_curl_init(curl_progress_func cb, void *data);
70 static int str_starts_with(const char *str, const char *prefix)
72 return (strncmp(str, prefix, strlen(prefix)) == 0);
76 opkg_download(const char *src, const char *dest_file_name,
77 curl_progress_func cb, void *data, const short hide_error)
81 char *src_basec = xstrdup(src);
82 char *src_base = basename(src_basec);
83 char *tmp_file_location;
85 opkg_msg(NOTICE, "Downloading %s\n", src);
87 if (str_starts_with(src, "file:")) {
88 const char *file_src = src + 5;
89 opkg_msg(INFO, "Copying %s to %s...", file_src, dest_file_name);
90 err = file_copy(file_src, dest_file_name);
91 opkg_msg(INFO, "Done.\n");
96 sprintf_alloc(&tmp_file_location, "%s/%s", conf->tmp_dir, src_base);
98 err = unlink(tmp_file_location);
99 if (err && errno != ENOENT) {
100 opkg_perror(ERROR, "Failed to unlink %s", tmp_file_location);
101 free(tmp_file_location);
105 if (conf->http_proxy) {
107 "Setting environment variable: http_proxy = %s.\n",
109 setenv("http_proxy", conf->http_proxy, 1);
111 if (conf->ftp_proxy) {
113 "Setting environment variable: ftp_proxy = %s.\n",
115 setenv("ftp_proxy", conf->ftp_proxy, 1);
117 if (conf->no_proxy) {
119 "Setting environment variable: no_proxy = %s.\n",
121 setenv("no_proxy", conf->no_proxy, 1);
125 FILE *file = fopen(tmp_file_location, "w");
127 curl = opkg_curl_init(cb, data);
129 curl_easy_setopt(curl, CURLOPT_URL, src);
130 curl_easy_setopt(curl, CURLOPT_WRITEDATA, file);
132 res = curl_easy_perform(curl);
136 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE,
138 opkg_msg(hide_error ? DEBUG2 : ERROR,
139 "Failed to download %s: %s.\n", src,
140 curl_easy_strerror(res));
141 free(tmp_file_location);
146 free(tmp_file_location);
157 if (conf->http_proxy || conf->ftp_proxy) {
162 argv[i++] = tmp_file_location;
169 "Failed to download %s, wget returned %d.\n",
173 "Check your network settings and connectivity.\n\n");
174 free(tmp_file_location);
180 err = file_move(tmp_file_location, dest_file_name);
182 free(tmp_file_location);
188 opkg_download_cache(const char *src, const char *dest_file_name,
189 curl_progress_func cb, void *data)
191 char *cache_name = xstrdup(src);
192 char *cache_location, *p;
195 if (!conf->cache || str_starts_with(src, "file:")) {
196 err = opkg_download(src, dest_file_name, cb, data, 0);
200 if (!file_is_dir(conf->cache)) {
201 opkg_msg(ERROR, "%s is not a directory.\n", conf->cache);
206 for (p = cache_name; *p; p++)
208 *p = ','; /* looks nicer than | or # */
210 sprintf_alloc(&cache_location, "%s/%s", conf->cache, cache_name);
211 if (file_exists(cache_location))
212 opkg_msg(NOTICE, "Copying %s.\n", cache_location);
214 /* cache file with funky name not found, try simple name */
216 char *filename = strrchr(dest_file_name, '/');
218 cache_name = xstrdup(filename + 1); // strip leading '/'
220 cache_name = xstrdup(dest_file_name);
221 free(cache_location);
222 sprintf_alloc(&cache_location, "%s/%s", conf->cache,
224 if (file_exists(cache_location))
225 opkg_msg(NOTICE, "Copying %s.\n", cache_location);
227 err = opkg_download(src, cache_location, cb, data, 0);
229 (void)unlink(cache_location);
235 err = file_copy(cache_location, dest_file_name);
238 free(cache_location);
244 int opkg_download_pkg(pkg_t * pkg, const char *dir)
248 char *local_filename;
249 char *stripped_filename;
252 if (pkg->src == NULL) {
254 "Package %s is not available from any configured src.\n",
259 filename = pkg_get_string(pkg, PKG_FILENAME);
261 if (filename == NULL) {
263 "Package %s does not have a valid filename field.\n",
268 sprintf_alloc(&url, "%s/%s", pkg->src->value, filename);
270 /* The filename might be something like
271 "../../foo.opk". While this is correct, and exactly what we
272 want to use to construct url above, here we actually need to
273 use just the filename part, without any directory. */
275 stripped_filename = strrchr(filename, '/');
276 if (!stripped_filename)
277 stripped_filename = filename;
279 sprintf_alloc(&local_filename, "%s/%s", dir, stripped_filename);
280 pkg_set_string(pkg, PKG_LOCAL_FILENAME, local_filename);
282 err = opkg_download_cache(url, local_filename, NULL, NULL);
289 * Downloads file from url, installs in package database, return package name.
291 int opkg_prepare_url_for_install(const char *url, char **namep)
295 abstract_pkg_t *ab_pkg;
299 if (str_starts_with(url, "http://")
300 || str_starts_with(url, "ftp://")) {
302 char *file_basec = xstrdup(url);
303 char *file_base = basename(file_basec);
305 sprintf_alloc(&tmp_file, "%s/%s", conf->tmp_dir, file_base);
306 err = opkg_download(url, tmp_file, NULL, NULL, 0);
310 err = pkg_init_from_file(pkg, tmp_file);
317 } else if (strcmp(&url[strlen(url) - 4], OPKG_PKG_EXTENSION) == 0
318 || strcmp(&url[strlen(url) - 4], IPKG_PKG_EXTENSION) == 0
319 || strcmp(&url[strlen(url) - 4], DPKG_PKG_EXTENSION) == 0) {
321 err = pkg_init_from_file(pkg, url);
324 opkg_msg(DEBUG2, "Package %s provided by hand (%s).\n",
325 pkg->name, pkg_get_string(pkg, PKG_LOCAL_FILENAME));
326 pkg->provided_by_hand = 1;
329 ab_pkg = ensure_abstract_pkg_by_name(url);
331 if (!(ab_pkg->state_flag & SF_NEED_DETAIL)) {
332 opkg_msg(DEBUG, "applying abpkg flag to %s\n", ab_pkg->name);
333 ab_pkg->state_flag |= SF_NEED_DETAIL;
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_OPENSSL
377 X509_STORE *store = NULL;
379 BIO *in = NULL, *indata = NULL;
381 // Sig check failed by default !
386 // Set-up the key store
389 setup_verify(conf->signature_ca_file, conf->signature_ca_path))) {
390 opkg_msg(ERROR, "Can't open CA certificates.\n");
391 goto verify_file_end;
393 // Open a BIO to read the sig file
394 if (!(in = BIO_new_file(sig_file, "rb"))) {
395 opkg_msg(ERROR, "Can't open signature file %s.\n", sig_file);
396 goto verify_file_end;
398 // Read the PKCS7 block contained in the sig file
399 p7 = PEM_read_bio_PKCS7(in, NULL, NULL, NULL);
401 opkg_msg(ERROR, "Can't read signature file %s (Corrupted ?).\n",
403 goto verify_file_end;
406 // Open the Package file to authenticate
407 if (!(indata = BIO_new_file(text_file, "rb"))) {
408 opkg_msg(ERROR, "Can't open file %s.\n", text_file);
409 goto verify_file_end;
411 // Let's verify the autenticity !
412 if (PKCS7_verify(p7, NULL, store, indata, NULL, PKCS7_BINARY) != 1) {
414 opkg_msg(ERROR, "Verification failure.\n");
424 X509_STORE_free(store);
428 /* mute `unused variable' warnings. */
436 #if defined(HAVE_OPENSSL) || defined(HAVE_SSLCURL)
437 static void openssl_init(void)
442 OPENSSL_config(NULL);
443 OpenSSL_add_all_algorithms();
444 ERR_load_crypto_strings();
451 #if defined HAVE_OPENSSL
452 static X509_STORE *setup_verify(char *CAfile, char *CApath)
454 X509_STORE *store = NULL;
455 X509_LOOKUP *lookup = NULL;
457 if (!(store = X509_STORE_new())) {
458 // Something bad is happening...
461 // adds the X509 file lookup method
462 lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
463 if (lookup == NULL) {
466 // Autenticating against one CA file
468 if (!X509_LOOKUP_load_file(lookup, CAfile, X509_FILETYPE_PEM)) {
469 // Invalid CA => Bye bye
470 opkg_msg(ERROR, "Error loading file %s.\n", CAfile);
474 X509_LOOKUP_load_file(lookup, NULL, X509_FILETYPE_DEFAULT);
477 // Now look into CApath directory if supplied
478 lookup = X509_STORE_add_lookup(store, X509_LOOKUP_hash_dir());
479 if (lookup == NULL) {
484 if (!X509_LOOKUP_add_dir(lookup, CApath, X509_FILETYPE_PEM)) {
485 opkg_msg(ERROR, "Error loading directory %s.\n",
490 X509_LOOKUP_add_dir(lookup, NULL, X509_FILETYPE_DEFAULT);
499 X509_STORE_free(store);
507 void opkg_curl_cleanup(void)
510 curl_easy_cleanup(curl);
515 static CURL *opkg_curl_init(curl_progress_func cb, void *data)
519 curl = curl_easy_init();
524 if (conf->ssl_engine) {
526 /* use crypto engine */
528 (curl, CURLOPT_SSLENGINE,
529 conf->ssl_engine) != CURLE_OK) {
531 "Can't set crypto engine '%s'.\n",
537 /* set the crypto engine as default */
539 (curl, CURLOPT_SSLENGINE_DEFAULT, 1L) != CURLE_OK) {
541 "Can't set crypto engine '%s' as default.\n",
549 /* cert & key can only be in PEM case in the same file */
550 if (conf->ssl_key_passwd) {
552 (curl, CURLOPT_SSLKEYPASSWD,
553 conf->ssl_key_passwd) != CURLE_OK) {
555 "Failed to set key password.\n");
559 /* sets the client certificate and its type */
560 if (conf->ssl_cert_type) {
562 (curl, CURLOPT_SSLCERTTYPE,
563 conf->ssl_cert_type) != CURLE_OK) {
565 "Failed to set certificate format.\n");
568 /* SSL cert name isn't mandatory */
569 if (conf->ssl_cert) {
570 curl_easy_setopt(curl, CURLOPT_SSLCERT, conf->ssl_cert);
573 /* sets the client key and its type */
574 if (conf->ssl_key_type) {
576 (curl, CURLOPT_SSLKEYTYPE,
577 conf->ssl_key_type) != CURLE_OK) {
578 opkg_msg(DEBUG, "Failed to set key format.\n");
583 (curl, CURLOPT_SSLKEY, conf->ssl_key) != CURLE_OK) {
584 opkg_msg(DEBUG, "Failed to set key.\n");
588 /* Should we verify the peer certificate ? */
589 if (conf->ssl_dont_verify_peer) {
591 * CURLOPT_SSL_VERIFYPEER default is nonzero (curl => 7.10)
593 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
596 /* certification authority file and/or path */
597 if (conf->ssl_ca_file) {
598 curl_easy_setopt(curl, CURLOPT_CAINFO,
601 if (conf->ssl_ca_path) {
602 curl_easy_setopt(curl, CURLOPT_CAPATH,
607 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
608 curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1);
609 if (conf->http_proxy || conf->ftp_proxy) {
611 sprintf_alloc(&userpwd, "%s:%s", conf->proxy_user,
613 curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, userpwd);
618 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, (cb == NULL));
620 curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, data);
621 curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, cb);