Remove unused opkg_set_current_state bits.
[project/opkg-lede.git] / libopkg / opkg_download.c
1 /* vi: set noexpandtab sw=4 sts=4: */
2 /* opkg_download.c - the opkg package management system
3
4 Carl D. Worth
5
6 Copyright (C) 2001 University of Southern California
7 Copyright (C) 2008 OpenMoko Inc
8
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.
13
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.
18 */
19 #include "config.h"
20
21 #ifdef HAVE_CURL
22 #include <curl/curl.h>
23 #endif
24
25 #if defined(HAVE_SSLCURL) || defined(HAVE_OPENSSL)
26 #include <openssl/conf.h>
27 #include <openssl/evp.h>
28 #include <openssl/err.h>
29 #endif
30
31 #if defined(HAVE_GPGME)
32 #include <gpgme.h>
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>
39 #endif
40
41 #include "includes.h"
42 #include "opkg_download.h"
43 #include "opkg_message.h"
44
45 #include "sprintf_alloc.h"
46 #include "xsystem.h"
47 #include "file_util.h"
48 #include "str_util.h"
49 #include "opkg_defines.h"
50
51 #if defined(HAVE_OPENSSL) || defined(HAVE_SSLCURL)
52 static void openssl_init(void);
53 #endif
54
55 #ifdef HAVE_OPENSSL
56 static X509_STORE *setup_verify(opkg_conf_t *conf, char *CAfile, char *CApath);
57 #endif
58
59 #ifdef HAVE_CURL
60 /*
61 * Make curl an instance variable so we don't have to instanciate it
62 * each time
63 */
64 static CURL *curl = NULL;
65 static void opkg_curl_cleanup(void);
66 static CURL *opkg_curl_init(opkg_conf_t *conf, curl_progress_func cb, void *data);
67 #endif
68
69 int opkg_download(opkg_conf_t *conf, const char *src,
70 const char *dest_file_name, curl_progress_func cb, void *data)
71 {
72 int err = 0;
73
74 char *src_basec = strdup(src);
75 char *src_base = basename(src_basec);
76 char *tmp_file_location;
77
78 opkg_message(conf,OPKG_NOTICE,"Downloading %s\n", src);
79
80 if (str_starts_with(src, "file:")) {
81 int ret;
82 const char *file_src = src + 5;
83 opkg_message(conf,OPKG_INFO,"Copying %s to %s...", file_src, dest_file_name);
84 ret = file_copy(src + 5, dest_file_name);
85 opkg_message(conf,OPKG_INFO,"Done\n");
86 free(src_basec);
87 return ret;
88 }
89
90 sprintf_alloc(&tmp_file_location, "%s/%s", conf->tmp_dir, src_base);
91 err = unlink(tmp_file_location);
92 if (err && errno != ENOENT) {
93 opkg_message(conf,OPKG_ERROR, "%s: ERROR: failed to unlink %s: %s\n",
94 __FUNCTION__, tmp_file_location, strerror(errno));
95 free(tmp_file_location);
96 free(src_basec);
97 return errno;
98 }
99
100 if (conf->http_proxy) {
101 opkg_message(conf,OPKG_DEBUG,"Setting environment variable: http_proxy = %s\n", conf->http_proxy);
102 setenv("http_proxy", conf->http_proxy, 1);
103 }
104 if (conf->ftp_proxy) {
105 opkg_message(conf,OPKG_DEBUG,"Setting environment variable: ftp_proxy = %s\n", conf->ftp_proxy);
106 setenv("ftp_proxy", conf->ftp_proxy, 1);
107 }
108 if (conf->no_proxy) {
109 opkg_message(conf,OPKG_DEBUG,"Setting environment variable: no_proxy = %s\n", conf->no_proxy);
110 setenv("no_proxy", conf->no_proxy, 1);
111 }
112
113 #ifdef HAVE_CURL
114 CURL *curl;
115 CURLcode res;
116 FILE * file = fopen (tmp_file_location, "w");
117
118 curl = opkg_curl_init (conf, cb, data);
119 if (curl)
120 {
121 curl_easy_setopt (curl, CURLOPT_URL, src);
122 curl_easy_setopt (curl, CURLOPT_WRITEDATA, file);
123
124 res = curl_easy_perform (curl);
125 fclose (file);
126 if (res)
127 {
128 long error_code;
129 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &error_code);
130 opkg_message(conf, OPKG_ERROR, "Failed to download %s. \nerror detail: %s\n", src, curl_easy_strerror(res));
131 free(tmp_file_location);
132 free(src_basec);
133 return res;
134 }
135
136 }
137 else
138 {
139 free(tmp_file_location);
140 free(src_basec);
141 return -1;
142 }
143 #else
144 {
145 int res;
146 char *wgetcmd;
147 char *wgetopts;
148 wgetopts = getenv("OPKG_WGETOPTS");
149 sprintf_alloc(&wgetcmd, "wget -q %s%s -O \"%s\" \"%s\"",
150 (conf->http_proxy || conf->ftp_proxy) ? "-Y on " : "",
151 (wgetopts!=NULL) ? wgetopts : "",
152 tmp_file_location, src);
153 opkg_message(conf, OPKG_INFO, "Executing: %s\n", wgetcmd);
154 res = xsystem(wgetcmd);
155 free(wgetcmd);
156 if (res) {
157 opkg_message(conf, OPKG_ERROR, "Failed to download %s, error %d\n", src, res);
158 free(tmp_file_location);
159 free(src_basec);
160 return res;
161 }
162 }
163 #endif
164
165 err = file_move(tmp_file_location, dest_file_name);
166
167 free(tmp_file_location);
168 free(src_basec);
169
170 if (err) {
171 return err;
172 }
173
174 return 0;
175 }
176
177 static int opkg_download_cache(opkg_conf_t *conf, const char *src,
178 const char *dest_file_name, curl_progress_func cb, void *data)
179 {
180 char *cache_name = strdup(src);
181 char *cache_location, *p;
182 int err = 0;
183
184 if (!conf->cache || str_starts_with(src, "file:")) {
185 err = opkg_download(conf, src, dest_file_name, cb, data);
186 goto out1;
187 }
188
189 for (p = cache_name; *p; p++)
190 if (*p == '/')
191 *p = ','; /* looks nicer than | or # */
192
193 sprintf_alloc(&cache_location, "%s/%s", conf->cache, cache_name);
194 if (file_exists(cache_location))
195 opkg_message(conf, OPKG_NOTICE, "Copying %s\n", cache_location);
196 else {
197 err = opkg_download(conf, src, cache_location, cb, data);
198 if (err) {
199 (void) unlink(cache_location);
200 goto out2;
201 }
202 }
203
204 err = file_copy(cache_location, dest_file_name);
205
206
207 out2:
208 free(cache_location);
209 out1:
210 free(cache_name);
211 return err;
212 }
213
214 int opkg_download_pkg(opkg_conf_t *conf, pkg_t *pkg, const char *dir)
215 {
216 int err;
217 char *url;
218 char *stripped_filename;
219
220 if (pkg->src == NULL) {
221 opkg_message(conf,OPKG_ERROR, "ERROR: Package %s (parent %s) is not available from any configured src.\n",
222 pkg->name, pkg->parent->name);
223 return -1;
224 }
225 if (pkg->filename == NULL) {
226 opkg_message(conf,OPKG_ERROR, "ERROR: Package %s (parent %s) does not have a valid filename field.\n",pkg->name, pkg->parent->name);
227 return -1;
228 }
229
230 sprintf_alloc(&url, "%s/%s", pkg->src->value, pkg->filename);
231
232 /* XXX: BUG: The pkg->filename might be something like
233 "../../foo.opk". While this is correct, and exactly what we
234 want to use to construct url above, here we actually need to
235 use just the filename part, without any directory. */
236
237 stripped_filename = strrchr(pkg->filename, '/');
238 if ( ! stripped_filename )
239 stripped_filename = pkg->filename;
240
241 sprintf_alloc(&pkg->local_filename, "%s/%s", dir, stripped_filename);
242
243 err = opkg_download_cache(conf, url, pkg->local_filename, NULL, NULL);
244 free(url);
245
246 return err;
247 }
248
249 /*
250 * Downloads file from url, installs in package database, return package name.
251 */
252 int opkg_prepare_url_for_install(opkg_conf_t *conf, const char *url, char **namep)
253 {
254 int err = 0;
255 pkg_t *pkg;
256 pkg = pkg_new();
257 if (pkg == NULL)
258 return ENOMEM;
259
260 if (str_starts_with(url, "http://")
261 || str_starts_with(url, "ftp://")) {
262 char *tmp_file;
263 char *file_basec = strdup(url);
264 char *file_base = basename(file_basec);
265
266 sprintf_alloc(&tmp_file, "%s/%s", conf->tmp_dir, file_base);
267 err = opkg_download(conf, url, tmp_file, NULL, NULL);
268 if (err)
269 return err;
270
271 err = pkg_init_from_file(pkg, tmp_file);
272 if (err)
273 return err;
274
275 free(tmp_file);
276 free(file_basec);
277
278 } else if (strcmp(&url[strlen(url) - 4], OPKG_PKG_EXTENSION) == 0
279 || strcmp(&url[strlen(url) - 4], IPKG_PKG_EXTENSION) == 0
280 || strcmp(&url[strlen(url) - 4], DPKG_PKG_EXTENSION) == 0) {
281
282 err = pkg_init_from_file(pkg, url);
283 if (err)
284 return err;
285 opkg_message(conf, OPKG_DEBUG2, "Package %s provided by hand (%s).\n", pkg->name,pkg->local_filename);
286 pkg->provided_by_hand = 1;
287
288 } else {
289 pkg_deinit(pkg);
290 free(pkg);
291 return 0;
292 }
293
294 if (!pkg->architecture) {
295 opkg_message(conf, OPKG_ERROR, "Package %s has no Architecture defined.\n", pkg->name);
296 return -EINVAL;
297 }
298
299 pkg->dest = conf->default_dest;
300 pkg->state_want = SW_INSTALL;
301 pkg->state_flag |= SF_PREFER;
302 pkg = hash_insert_pkg(&conf->pkg_hash, pkg, 1,conf);
303 if ( pkg == NULL ){
304 fprintf(stderr, "%s : This should never happen. Report this Bug in bugzilla please \n ",__FUNCTION__);
305 return 0;
306 }
307 if (namep) {
308 *namep = strdup(pkg->name);
309 }
310 return 0;
311 }
312
313 int
314 opkg_verify_file (opkg_conf_t *conf, char *text_file, char *sig_file)
315 {
316 #if defined HAVE_GPGME
317 if (conf->check_signature == 0 )
318 return 0;
319 int status = -1;
320 gpgme_ctx_t ctx;
321 gpgme_data_t sig, text, key;
322 gpgme_error_t err = -1;
323 gpgme_verify_result_t result;
324 gpgme_signature_t s;
325 char *trusted_path = NULL;
326
327 err = gpgme_new (&ctx);
328
329 if (err)
330 return -1;
331
332 sprintf_alloc(&trusted_path, "%s/%s", conf->offline_root, "/etc/opkg/trusted.gpg");
333 err = gpgme_data_new_from_file (&key, trusted_path, 1);
334 free (trusted_path);
335 if (err)
336 {
337 return -1;
338 }
339 err = gpgme_op_import (ctx, key);
340 if (err)
341 {
342 gpgme_data_release (key);
343 return -1;
344 }
345 gpgme_data_release (key);
346
347 err = gpgme_data_new_from_file (&sig, sig_file, 1);
348 if (err)
349 {
350 gpgme_release (ctx);
351 return -1;
352 }
353
354 err = gpgme_data_new_from_file (&text, text_file, 1);
355 if (err)
356 {
357 gpgme_data_release (sig);
358 gpgme_release (ctx);
359 return -1;
360 }
361
362 err = gpgme_op_verify (ctx, sig, text, NULL);
363
364 result = gpgme_op_verify_result (ctx);
365 if (!result)
366 return -1;
367
368 /* see if any of the signitures matched */
369 s = result->signatures;
370 while (s)
371 {
372 status = gpg_err_code (s->status);
373 if (status == GPG_ERR_NO_ERROR)
374 break;
375 s = s->next;
376 }
377
378
379 gpgme_data_release (sig);
380 gpgme_data_release (text);
381 gpgme_release (ctx);
382
383 return status;
384 #elif defined HAVE_OPENSSL
385 X509_STORE *store = NULL;
386 PKCS7 *p7 = NULL;
387 BIO *in = NULL, *indata = NULL;
388
389 // Sig check failed by default !
390 int status = -1;
391
392 openssl_init();
393
394 // Set-up the key store
395 if(!(store = setup_verify(conf, conf->signature_ca_file, conf->signature_ca_path))){
396 opkg_message(conf, OPKG_ERROR,
397 "Can't open CA certificates\n");
398 goto verify_file_end;
399 }
400
401 // Open a BIO to read the sig file
402 if (!(in = BIO_new_file(sig_file, "rb"))){
403 opkg_message(conf, OPKG_ERROR,
404 "Can't open signature file %s\n", sig_file);
405 goto verify_file_end;
406 }
407
408 // Read the PKCS7 block contained in the sig file
409 p7 = PEM_read_bio_PKCS7(in, NULL, NULL, NULL);
410 if(!p7){
411 opkg_message(conf, OPKG_ERROR,
412 "Can't read signature file (Corrupted ?)\n");
413 goto verify_file_end;
414 }
415
416 // Open the Package file to authenticate
417 if (!(indata = BIO_new_file(text_file, "rb"))){
418 opkg_message(conf, OPKG_ERROR,
419 "Can't open file %s\n", text_file);
420 goto verify_file_end;
421 }
422
423 // Let's verify the autenticity !
424 if (PKCS7_verify(p7, NULL, store, indata, NULL, PKCS7_BINARY) != 1){
425 // Get Off My Lawn!
426 opkg_message(conf, OPKG_ERROR,
427 "Verification failure\n");
428 }else{
429 // Victory !
430 status = 0;
431 }
432
433 verify_file_end:
434 BIO_free(in);
435 BIO_free(indata);
436 PKCS7_free(p7);
437 X509_STORE_free(store);
438
439 return status;
440 #else
441 /* mute `unused variable' warnings. */
442 (void) sig_file;
443 (void) text_file;
444 (void) conf;
445 return 0;
446 #endif
447 }
448
449
450 #if defined(HAVE_OPENSSL) || defined(HAVE_SSLCURL)
451 static void openssl_init(void){
452 static int init = 0;
453
454 if(!init){
455 OPENSSL_config(NULL);
456 OpenSSL_add_all_algorithms();
457 ERR_load_crypto_strings();
458 init = 1;
459 }
460 }
461
462 #endif
463
464
465 #if defined HAVE_OPENSSL
466 static X509_STORE *setup_verify(opkg_conf_t *conf, char *CAfile, char *CApath){
467 X509_STORE *store = NULL;
468 X509_LOOKUP *lookup = NULL;
469
470 if(!(store = X509_STORE_new())){
471 // Something bad is happening...
472 goto end;
473 }
474
475 // adds the X509 file lookup method
476 lookup = X509_STORE_add_lookup(store,X509_LOOKUP_file());
477 if (lookup == NULL){
478 goto end;
479 }
480
481 // Autenticating against one CA file
482 if (CAfile) {
483 if(!X509_LOOKUP_load_file(lookup,CAfile,X509_FILETYPE_PEM)) {
484 // Invalid CA => Bye bye
485 opkg_message(conf, OPKG_ERROR,
486 "Error loading file %s\n", CAfile);
487 goto end;
488 }
489 } else {
490 X509_LOOKUP_load_file(lookup,NULL,X509_FILETYPE_DEFAULT);
491 }
492
493 // Now look into CApath directory if supplied
494 lookup = X509_STORE_add_lookup(store,X509_LOOKUP_hash_dir());
495 if (lookup == NULL){
496 goto end;
497 }
498
499 if (CApath) {
500 if(!X509_LOOKUP_add_dir(lookup,CApath,X509_FILETYPE_PEM)) {
501 opkg_message(conf, OPKG_ERROR,
502 "Error loading directory %s\n", CApath);
503 goto end;
504 }
505 } else {
506 X509_LOOKUP_add_dir(lookup,NULL,X509_FILETYPE_DEFAULT);
507 }
508
509 // All right !
510 ERR_clear_error();
511 return store;
512
513 end:
514
515 X509_STORE_free(store);
516 return NULL;
517
518 }
519
520 #endif
521
522 #ifdef HAVE_CURL
523 static void opkg_curl_cleanup(void){
524 if(curl != NULL){
525 curl_easy_cleanup (curl);
526 curl = NULL;
527 }
528 }
529
530 static CURL *opkg_curl_init(opkg_conf_t *conf, curl_progress_func cb, void *data){
531
532 if(curl == NULL){
533 curl = curl_easy_init();
534
535 #ifdef HAVE_SSLCURL
536 openssl_init();
537
538 if (conf->ssl_engine) {
539
540 /* use crypto engine */
541 if (curl_easy_setopt(curl, CURLOPT_SSLENGINE, conf->ssl_engine) != CURLE_OK){
542 opkg_message(conf, OPKG_ERROR, "can't set crypto engine: '%s'\n",
543 conf->ssl_engine);
544
545 opkg_curl_cleanup();
546 return NULL;
547 }
548 /* set the crypto engine as default */
549 if (curl_easy_setopt(curl, CURLOPT_SSLENGINE_DEFAULT, 1L) != CURLE_OK){
550 opkg_message(conf, OPKG_ERROR, "can't set crypto engine as default\n");
551
552 opkg_curl_cleanup();
553 return NULL;
554 }
555 }
556
557 /* cert & key can only be in PEM case in the same file */
558 if(conf->ssl_key_passwd){
559 if (curl_easy_setopt(curl, CURLOPT_SSLKEYPASSWD, conf->ssl_key_passwd) != CURLE_OK)
560 {
561 opkg_message(conf, OPKG_DEBUG, "Failed to set key password\n");
562 }
563 }
564
565 /* sets the client certificate and its type */
566 if(conf->ssl_cert_type){
567 if (curl_easy_setopt(curl, CURLOPT_SSLCERTTYPE, conf->ssl_cert_type) != CURLE_OK)
568 {
569 opkg_message(conf, OPKG_DEBUG, "Failed to set certificate format\n");
570 }
571 }
572 /* SSL cert name isn't mandatory */
573 if(conf->ssl_cert){
574 curl_easy_setopt(curl, CURLOPT_SSLCERT, conf->ssl_cert);
575 }
576
577 /* sets the client key and its type */
578 if(conf->ssl_key_type){
579 if (curl_easy_setopt(curl, CURLOPT_SSLKEYTYPE, conf->ssl_key_type) != CURLE_OK)
580 {
581 opkg_message(conf, OPKG_DEBUG, "Failed to set key format\n");
582 }
583 }
584 if(conf->ssl_key){
585 if (curl_easy_setopt(curl, CURLOPT_SSLKEY, conf->ssl_key) != CURLE_OK)
586 {
587 opkg_message(conf, OPKG_DEBUG, "Failed to set key\n");
588 }
589 }
590
591 /* Should we verify the peer certificate ? */
592 if(conf->ssl_dont_verify_peer){
593 /*
594 * CURLOPT_SSL_VERIFYPEER default is nonzero (curl => 7.10)
595 */
596 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
597 }
598
599 /* certification authority file and/or path */
600 if(conf->ssl_ca_file){
601 curl_easy_setopt(curl, CURLOPT_CAINFO, conf->ssl_ca_file);
602 }
603 if(conf->ssl_ca_path){
604 curl_easy_setopt(curl, CURLOPT_CAPATH, conf->ssl_ca_path);
605 }
606 #endif
607
608 curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1);
609 curl_easy_setopt (curl, CURLOPT_FAILONERROR, 1);
610 if (conf->http_proxy || conf->ftp_proxy)
611 {
612 char *userpwd;
613 sprintf_alloc (&userpwd, "%s:%s", conf->proxy_user,
614 conf->proxy_passwd);
615 curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, userpwd);
616 free (userpwd);
617 }
618
619 /* add curl cleanup callback */
620 if(!atexit(opkg_curl_cleanup)){
621 opkg_message(conf,OPKG_DEBUG, "Failed to register atexit curl cleanup function\n");
622 }
623
624 }
625
626 curl_easy_setopt (curl, CURLOPT_NOPROGRESS, (cb == NULL));
627 if (cb)
628 {
629 curl_easy_setopt (curl, CURLOPT_PROGRESSDATA, data);
630 curl_easy_setopt (curl, CURLOPT_PROGRESSFUNCTION, cb);
631 }
632
633 return curl;
634
635 }
636 #endif