f1553abca7ae5501d178822cdd14fe7e52d784f6
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"
34 #if defined(HAVE_OPENSSL)
35 #include <openssl/conf.h>
36 #include <openssl/evp.h>
37 #include <openssl/err.h>
38 #include <openssl/ssl.h>
41 #if defined(HAVE_OPENSSL)
42 #include <openssl/bio.h>
43 #include <openssl/objects.h>
44 #include <openssl/x509.h>
45 #include <openssl/pem.h>
46 #include <openssl/hmac.h>
49 #if defined(HAVE_OPENSSL)
50 static void openssl_init(void);
54 static X509_STORE
*setup_verify(char *CAfile
, char *CApath
);
57 static int str_starts_with(const char *str
, const char *prefix
)
59 return (strncmp(str
, prefix
, strlen(prefix
)) == 0);
63 opkg_download(const char *src
, const char *dest_file_name
,
64 curl_progress_func cb
, void *data
, const short hide_error
)
68 char *src_basec
= xstrdup(src
);
69 char *src_base
= basename(src_basec
);
70 char *tmp_file_location
;
72 opkg_msg(NOTICE
, "Downloading %s\n", src
);
74 if (str_starts_with(src
, "file:")) {
75 const char *file_src
= src
+ 5;
76 opkg_msg(INFO
, "Copying %s to %s...", file_src
, dest_file_name
);
77 err
= file_copy(file_src
, dest_file_name
);
78 opkg_msg(INFO
, "Done.\n");
83 sprintf_alloc(&tmp_file_location
, "%s/%s", conf
->tmp_dir
, src_base
);
85 err
= unlink(tmp_file_location
);
86 if (err
&& errno
!= ENOENT
) {
87 opkg_perror(ERROR
, "Failed to unlink %s", tmp_file_location
);
88 free(tmp_file_location
);
92 if (conf
->http_proxy
) {
94 "Setting environment variable: http_proxy = %s.\n",
96 setenv("http_proxy", conf
->http_proxy
, 1);
98 if (conf
->ftp_proxy
) {
100 "Setting environment variable: ftp_proxy = %s.\n",
102 setenv("ftp_proxy", conf
->ftp_proxy
, 1);
104 if (conf
->no_proxy
) {
106 "Setting environment variable: no_proxy = %s.\n",
108 setenv("no_proxy", conf
->no_proxy
, 1);
118 if (conf
->http_proxy
|| conf
->ftp_proxy
) {
123 argv
[i
++] = tmp_file_location
;
130 "Failed to download %s, wget returned %d.\n",
134 "Check your network settings and connectivity.\n\n");
135 free(tmp_file_location
);
140 err
= file_move(tmp_file_location
, dest_file_name
);
142 free(tmp_file_location
);
148 opkg_download_cache(const char *src
, const char *dest_file_name
,
149 curl_progress_func cb
, void *data
)
151 char *cache_name
= xstrdup(src
);
152 char *cache_location
, *p
;
155 if (!conf
->cache
|| str_starts_with(src
, "file:")) {
156 err
= opkg_download(src
, dest_file_name
, cb
, data
, 0);
160 if (!file_is_dir(conf
->cache
)) {
161 opkg_msg(ERROR
, "%s is not a directory.\n", conf
->cache
);
166 for (p
= cache_name
; *p
; p
++)
168 *p
= ','; /* looks nicer than | or # */
170 sprintf_alloc(&cache_location
, "%s/%s", conf
->cache
, cache_name
);
171 if (file_exists(cache_location
))
172 opkg_msg(NOTICE
, "Copying %s.\n", cache_location
);
174 /* cache file with funky name not found, try simple name */
176 char *filename
= strrchr(dest_file_name
, '/');
178 cache_name
= xstrdup(filename
+ 1); // strip leading '/'
180 cache_name
= xstrdup(dest_file_name
);
181 free(cache_location
);
182 sprintf_alloc(&cache_location
, "%s/%s", conf
->cache
,
184 if (file_exists(cache_location
))
185 opkg_msg(NOTICE
, "Copying %s.\n", cache_location
);
187 err
= opkg_download(src
, cache_location
, cb
, data
, 0);
189 (void)unlink(cache_location
);
195 err
= file_copy(cache_location
, dest_file_name
);
198 free(cache_location
);
204 int opkg_download_pkg(pkg_t
* pkg
, const char *dir
)
208 char *local_filename
;
209 char *stripped_filename
;
212 if (pkg
->src
== NULL
) {
214 "Package %s is not available from any configured src.\n",
219 filename
= pkg_get_string(pkg
, PKG_FILENAME
);
221 if (filename
== NULL
) {
223 "Package %s does not have a valid filename field.\n",
228 sprintf_alloc(&url
, "%s/%s", pkg
->src
->value
, filename
);
230 /* The filename might be something like
231 "../../foo.opk". While this is correct, and exactly what we
232 want to use to construct url above, here we actually need to
233 use just the filename part, without any directory. */
235 stripped_filename
= strrchr(filename
, '/');
236 if (!stripped_filename
)
237 stripped_filename
= filename
;
239 sprintf_alloc(&local_filename
, "%s/%s", dir
, stripped_filename
);
240 pkg_set_string(pkg
, PKG_LOCAL_FILENAME
, local_filename
);
242 err
= opkg_download_cache(url
, local_filename
, NULL
, NULL
);
249 * Downloads file from url, installs in package database, return package name.
251 int opkg_prepare_url_for_install(const char *url
, char **namep
)
255 abstract_pkg_t
*ab_pkg
;
259 if (str_starts_with(url
, "http://")
260 || str_starts_with(url
, "ftp://")) {
262 char *file_basec
= xstrdup(url
);
263 char *file_base
= basename(file_basec
);
265 sprintf_alloc(&tmp_file
, "%s/%s", conf
->tmp_dir
, file_base
);
266 err
= opkg_download(url
, tmp_file
, NULL
, NULL
, 0);
270 err
= pkg_init_from_file(pkg
, tmp_file
);
277 } else if (strcmp(&url
[strlen(url
) - 4], OPKG_PKG_EXTENSION
) == 0
278 || strcmp(&url
[strlen(url
) - 4], IPKG_PKG_EXTENSION
) == 0
279 || strcmp(&url
[strlen(url
) - 4], DPKG_PKG_EXTENSION
) == 0) {
281 err
= pkg_init_from_file(pkg
, url
);
284 opkg_msg(DEBUG2
, "Package %s provided by hand (%s).\n",
285 pkg
->name
, pkg_get_string(pkg
, PKG_LOCAL_FILENAME
));
286 pkg
->provided_by_hand
= 1;
289 ab_pkg
= ensure_abstract_pkg_by_name(url
);
291 if (!(ab_pkg
->state_flag
& SF_NEED_DETAIL
)) {
292 opkg_msg(DEBUG
, "applying abpkg flag to %s\n", ab_pkg
->name
);
293 ab_pkg
->state_flag
|= SF_NEED_DETAIL
;
301 pkg
->dest
= conf
->default_dest
;
302 pkg
->state_want
= SW_INSTALL
;
303 pkg
->state_flag
|= SF_PREFER
;
304 hash_insert_pkg(pkg
, 1);
307 *namep
= xstrdup(pkg
->name
);
312 int opkg_verify_file(char *text_file
, char *sig_file
)
314 #if defined HAVE_USIGN
318 if (conf
->check_signature
== 0)
326 execl("/usr/sbin/opkg-key", "opkg-key", "verify", sig_file
,
331 waitpid(pid
, &status
, 0);
332 if (!WIFEXITED(status
) || WEXITSTATUS(status
))
336 #elif defined HAVE_OPENSSL
337 X509_STORE
*store
= NULL
;
339 BIO
*in
= NULL
, *indata
= NULL
;
341 // Sig check failed by default !
346 // Set-up the key store
349 setup_verify(conf
->signature_ca_file
, conf
->signature_ca_path
))) {
350 opkg_msg(ERROR
, "Can't open CA certificates.\n");
351 goto verify_file_end
;
353 // Open a BIO to read the sig file
354 if (!(in
= BIO_new_file(sig_file
, "rb"))) {
355 opkg_msg(ERROR
, "Can't open signature file %s.\n", sig_file
);
356 goto verify_file_end
;
358 // Read the PKCS7 block contained in the sig file
359 p7
= PEM_read_bio_PKCS7(in
, NULL
, NULL
, NULL
);
361 opkg_msg(ERROR
, "Can't read signature file %s (Corrupted ?).\n",
363 goto verify_file_end
;
366 // Open the Package file to authenticate
367 if (!(indata
= BIO_new_file(text_file
, "rb"))) {
368 opkg_msg(ERROR
, "Can't open file %s.\n", text_file
);
369 goto verify_file_end
;
371 // Let's verify the autenticity !
372 if (PKCS7_verify(p7
, NULL
, store
, indata
, NULL
, PKCS7_BINARY
) != 1) {
374 opkg_msg(ERROR
, "Verification failure.\n");
384 X509_STORE_free(store
);
388 /* mute `unused variable' warnings. */
396 #if defined(HAVE_OPENSSL)
397 static void openssl_init(void)
402 OPENSSL_config(NULL
);
403 OpenSSL_add_all_algorithms();
404 ERR_load_crypto_strings();
411 #if defined HAVE_OPENSSL
412 static X509_STORE
*setup_verify(char *CAfile
, char *CApath
)
414 X509_STORE
*store
= NULL
;
415 X509_LOOKUP
*lookup
= NULL
;
417 if (!(store
= X509_STORE_new())) {
418 // Something bad is happening...
421 // adds the X509 file lookup method
422 lookup
= X509_STORE_add_lookup(store
, X509_LOOKUP_file());
423 if (lookup
== NULL
) {
426 // Autenticating against one CA file
428 if (!X509_LOOKUP_load_file(lookup
, CAfile
, X509_FILETYPE_PEM
)) {
429 // Invalid CA => Bye bye
430 opkg_msg(ERROR
, "Error loading file %s.\n", CAfile
);
434 X509_LOOKUP_load_file(lookup
, NULL
, X509_FILETYPE_DEFAULT
);
437 // Now look into CApath directory if supplied
438 lookup
= X509_STORE_add_lookup(store
, X509_LOOKUP_hash_dir());
439 if (lookup
== NULL
) {
444 if (!X509_LOOKUP_add_dir(lookup
, CApath
, X509_FILETYPE_PEM
)) {
445 opkg_msg(ERROR
, "Error loading directory %s.\n",
450 X509_LOOKUP_add_dir(lookup
, NULL
, X509_FILETYPE_DEFAULT
);
459 X509_STORE_free(store
);