libopkg: add support for signature checking through usign
[oweals/opkg-lede.git] / libopkg / opkg.c
1 /* opkg.c - the opkg  package management system
2
3    Thomas Wood <thomas@openedhand.com>
4
5    Copyright (C) 2008 OpenMoko Inc
6
7    This program is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public License as
9    published by the Free Software Foundation; either version 2, or (at
10    your option) any later version.
11
12    This program is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    General Public License for more details.
16  */
17
18 #include "config.h"
19
20 #include <stdio.h>
21 #include <unistd.h>
22 #include <fnmatch.h>
23
24 #include "opkg.h"
25 #include "opkg_conf.h"
26
27 #include "opkg_install.h"
28 #include "opkg_configure.h"
29 #include "opkg_download.h"
30 #include "opkg_remove.h"
31 #include "opkg_upgrade.h"
32
33 #include "sprintf_alloc.h"
34 #include "file_util.h"
35
36 #include <libbb/libbb.h>
37
38 #define opkg_assert(expr) if (!(expr)) { \
39     printf ("opkg: file %s: line %d (%s): Assertation '%s' failed",\
40             __FILE__, __LINE__, __PRETTY_FUNCTION__, # expr); abort (); }
41
42 #define progress(d, p) d.percentage = p; if (progress_callback) progress_callback (&d, user_data);
43
44 /** Private Functions ***/
45
46 static int
47 opkg_configure_packages(char *pkg_name)
48 {
49         pkg_vec_t *all;
50         int i;
51         pkg_t *pkg;
52         int r, err = 0;
53
54         all = pkg_vec_alloc();
55         pkg_hash_fetch_available(all);
56
57         for (i = 0; i < all->len; i++) {
58                 pkg = all->pkgs[i];
59
60                 if (pkg_name && fnmatch(pkg_name, pkg->name, 0))
61                         continue;
62
63                 if (pkg->state_status == SS_UNPACKED) {
64                         r = opkg_configure(pkg);
65                         if (r == 0) {
66                                 pkg->state_status = SS_INSTALLED;
67                                 pkg->parent->state_status = SS_INSTALLED;
68                                 pkg->state_flag &= ~SF_PREFER;
69                         } else {
70                                 if (!err)
71                                         err = r;
72                         }
73                 }
74         }
75
76         pkg_vec_free(all);
77         return err;
78 }
79
80 struct _curl_cb_data {
81         opkg_progress_callback_t cb;
82         opkg_progress_data_t *progress_data;
83         void *user_data;
84         int start_range;
85         int finish_range;
86 };
87
88 int
89 curl_progress_cb(struct _curl_cb_data *cb_data, double t,       /* dltotal */
90                 double d,       /* dlnow */
91                 double ultotal, double ulnow)
92 {
93         int p = (t) ? d * 100 / t : 0;
94         static int prev = -1;
95         int progress = 0;
96
97         /* prevent the same value being sent twice (can occur due to rounding) */
98         if (p == prev)
99                 return 0;
100         prev = p;
101
102         if (t < 1)
103                 return 0;
104
105         progress = cb_data->start_range +
106             (d / t * ((cb_data->finish_range - cb_data->start_range)));
107         cb_data->progress_data->percentage = progress;
108
109         (cb_data->cb) (cb_data->progress_data, cb_data->user_data);
110
111         return 0;
112 }
113
114
115 static struct opkg_conf saved_conf;
116 /*** Public API ***/
117
118 int
119 opkg_new()
120 {
121         saved_conf = *conf;
122
123         if (opkg_conf_init())
124                 goto err0;
125
126         if (opkg_conf_load())
127                 goto err0;
128
129         if (pkg_hash_load_feeds())
130                 goto err1;
131
132         if (pkg_hash_load_status_files())
133                 goto err1;
134
135         return 0;
136
137 err1:
138         pkg_hash_deinit();
139 err0:
140         opkg_conf_deinit();
141         return -1;
142 }
143
144 void
145 opkg_free(void)
146 {
147 #ifdef HAVE_CURL
148         opkg_curl_cleanup();
149 #endif
150         opkg_conf_deinit();
151 }
152
153 int
154 opkg_re_read_config_files(void)
155 {
156         opkg_free();
157         *conf = saved_conf;
158         return opkg_new();
159 }
160
161 void
162 opkg_get_option(char *option, void **value)
163 {
164         int i = 0;
165         extern opkg_option_t options[];
166
167         /* look up the option
168          * TODO: this would be much better as a hash table
169          */
170         while (options[i].name) {
171                 if (strcmp(options[i].name, option) != 0) {
172                         i++;
173                         continue;
174                 }
175         }
176
177         /* get the option */
178         switch (options[i].type) {
179         case OPKG_OPT_TYPE_BOOL:
180                 *((int *) value) = *((int *) options[i].value);
181                 return;
182
183         case OPKG_OPT_TYPE_INT:
184                 *((int *) value) = *((int *) options[i].value);
185                 return;
186
187         case OPKG_OPT_TYPE_STRING:
188                 *((char **) value) = xstrdup(options[i].value);
189                 return;
190         }
191
192 }
193
194 void
195 opkg_set_option(char *option, void *value)
196 {
197         int i = 0, found = 0;
198         extern opkg_option_t options[];
199
200         opkg_assert(option != NULL);
201         opkg_assert(value != NULL);
202
203         /* look up the option
204          * TODO: this would be much better as a hash table
205          */
206         while (options[i].name) {
207                 if (strcmp(options[i].name, option) == 0) {
208                         found = 1;
209                         break;
210                 }
211                 i++;
212         }
213
214         if (!found) {
215                 opkg_msg(ERROR, "Invalid option: %s\n", option);
216                 return;
217         }
218
219         /* set the option */
220         switch (options[i].type) {
221         case OPKG_OPT_TYPE_BOOL:
222                 if (*((int *) value) == 0)
223                         *((int *) options[i].value) = 0;
224                 else
225                         *((int *) options[i].value) = 1;
226                 return;
227
228         case OPKG_OPT_TYPE_INT:
229                 *((int *) options[i].value) = *((int *) value);
230                 return;
231
232         case OPKG_OPT_TYPE_STRING:
233                 *((char **) options[i].value) = xstrdup(value);
234                 return;
235         }
236
237 }
238
239 /**
240  * @brief libopkg API: Install package
241  * @param package_name The name of package in which is going to install
242  * @param progress_callback The callback function that report the status to caller.
243  */
244 int
245 opkg_install_package(const char *package_name,
246                 opkg_progress_callback_t progress_callback,
247                 void *user_data)
248 {
249         int err;
250         char *stripped_filename;
251         opkg_progress_data_t pdata;
252         pkg_t *old, *new;
253         pkg_vec_t *deps, *all;
254         int i, ndepends;
255         char **unresolved = NULL;
256
257         opkg_assert(package_name != NULL);
258
259         /* ... */
260         pkg_info_preinstall_check();
261
262
263         /* check to ensure package is not already installed */
264         old = pkg_hash_fetch_installed_by_name(package_name);
265         if (old) {
266                 opkg_msg(ERROR, "Package %s is already installed\n",
267                                 package_name);
268                 return -1;
269         }
270
271         new = pkg_hash_fetch_best_installation_candidate_by_name(package_name);
272         if (!new) {
273                 opkg_msg(ERROR, "Couldn't find package %s\n", package_name);
274                 return -1;
275         }
276
277         new->state_flag |= SF_USER;
278
279         pdata.action = -1;
280         pdata.pkg = new;
281
282         progress(pdata, 0);
283
284         /* find dependancies and download them */
285         deps = pkg_vec_alloc();
286         /* this function does not return the original package, so we insert it later */
287         ndepends = pkg_hash_fetch_unsatisfied_dependencies(new, deps,
288                         &unresolved);
289         if (unresolved) {
290                 char **tmp = unresolved;
291                 opkg_msg(ERROR, "Couldn't satisfy the following dependencies"
292                                " for %s:\n", package_name);
293                 while (*tmp) {
294                         opkg_msg(ERROR, "\t%s", *tmp);
295                         free(*tmp);
296                         tmp++;
297                 }
298                 free(unresolved);
299                 pkg_vec_free(deps);
300                 opkg_message(ERROR, "\n");
301                 return -1;
302         }
303
304         /* insert the package we are installing so that we download it */
305         pkg_vec_insert(deps, new);
306
307         /* download package and dependencies */
308         for (i = 0; i < deps->len; i++) {
309                 pkg_t *pkg;
310                 struct _curl_cb_data cb_data;
311                 char *url;
312
313                 pkg = deps->pkgs[i];
314                 if (pkg->local_filename)
315                         continue;
316
317                 pdata.pkg = pkg;
318                 pdata.action = OPKG_DOWNLOAD;
319
320                 if (pkg->src == NULL) {
321                         opkg_msg(ERROR, "Package %s not available from any "
322                                         "configured src\n", package_name);
323                         return -1;
324                 }
325
326                 sprintf_alloc(&url, "%s/%s", pkg->src->value, pkg->filename);
327
328                 /* Get the filename part, without any directory */
329                 stripped_filename = strrchr(pkg->filename, '/');
330                 if (!stripped_filename)
331                         stripped_filename = pkg->filename;
332
333                 sprintf_alloc(&pkg->local_filename, "%s/%s", conf->tmp_dir,
334                               stripped_filename);
335
336                 cb_data.cb = progress_callback;
337                 cb_data.progress_data = &pdata;
338                 cb_data.user_data = user_data;
339                 /* 75% of "install" progress is for downloading */
340                 cb_data.start_range = 75 * i / deps->len;
341                 cb_data.finish_range = 75 * (i + 1) / deps->len;
342
343                 err = opkg_download(url, pkg->local_filename,
344                                     (curl_progress_func) curl_progress_cb,
345                                     &cb_data, 0);
346                 free(url);
347
348                 if (err) {
349                         pkg_vec_free(deps);
350                         return -1;
351                 }
352
353         }
354         pkg_vec_free(deps);
355
356         /* clear depenacy checked marks, left by pkg_hash_fetch_unsatisfied_dependencies */
357         all = pkg_vec_alloc();
358         pkg_hash_fetch_available(all);
359         for (i = 0; i < all->len; i++) {
360                 all->pkgs[i]->parent->dependencies_checked = 0;
361         }
362         pkg_vec_free(all);
363
364
365         /* 75% of "install" progress is for downloading */
366         pdata.pkg = new;
367         pdata.action = OPKG_INSTALL;
368         progress(pdata, 75);
369
370         /* unpack the package */
371         err = opkg_install_pkg(new, 0);
372
373         if (err) {
374                 return -1;
375         }
376
377         progress(pdata, 75);
378
379         /* run configure scripts, etc. */
380         err = opkg_configure_packages(NULL);
381         if (err) {
382                 return -1;
383         }
384
385         /* write out status files and file lists */
386         opkg_conf_write_status_files();
387         pkg_write_changed_filelists();
388
389         progress(pdata, 100);
390         return 0;
391 }
392
393 int
394 opkg_remove_package(const char *package_name,
395                 opkg_progress_callback_t progress_callback, void *user_data)
396 {
397         int err;
398         pkg_t *pkg = NULL;
399         pkg_t *pkg_to_remove;
400         opkg_progress_data_t pdata;
401
402         opkg_assert(package_name != NULL);
403
404         pkg_info_preinstall_check();
405
406         pkg = pkg_hash_fetch_installed_by_name(package_name);
407
408         if (pkg == NULL || pkg->state_status == SS_NOT_INSTALLED) {
409                 opkg_msg(ERROR, "Package %s not installed\n", package_name);
410                 return -1;
411         }
412
413         pdata.action = OPKG_REMOVE;
414         pdata.pkg = pkg;
415         progress(pdata, 0);
416
417         if (conf->restrict_to_default_dest) {
418                 pkg_to_remove = pkg_hash_fetch_installed_by_name_dest(pkg->name,
419                                                       conf->default_dest);
420         } else {
421                 pkg_to_remove = pkg_hash_fetch_installed_by_name(pkg->name);
422         }
423
424
425         progress(pdata, 75);
426
427         err = opkg_remove_pkg(pkg_to_remove, 0);
428
429         /* write out status files and file lists */
430         opkg_conf_write_status_files();
431         pkg_write_changed_filelists();
432
433
434         progress(pdata, 100);
435         return (err) ? -1 : 0;
436 }
437
438 int
439 opkg_upgrade_package(const char *package_name,
440                 opkg_progress_callback_t progress_callback,
441                 void *user_data)
442 {
443         int err;
444         pkg_t *pkg;
445         opkg_progress_data_t pdata;
446
447         opkg_assert(package_name != NULL);
448
449         pkg_info_preinstall_check();
450
451         if (conf->restrict_to_default_dest) {
452                 pkg = pkg_hash_fetch_installed_by_name_dest(package_name,
453                                                             conf->default_dest);
454         } else {
455                 pkg = pkg_hash_fetch_installed_by_name(package_name);
456         }
457
458         if (!pkg) {
459                 opkg_msg(ERROR, "Package %s not installed\n", package_name);
460                 return -1;
461         }
462
463         pdata.action = OPKG_INSTALL;
464         pdata.pkg = pkg;
465         progress(pdata, 0);
466
467         err = opkg_upgrade_pkg(pkg);
468         if (err) {
469                 return -1;
470         }
471         progress(pdata, 75);
472
473         err = opkg_configure_packages(NULL);
474         if (err) {
475                 return -1;
476         }
477
478         /* write out status files and file lists */
479         opkg_conf_write_status_files();
480         pkg_write_changed_filelists();
481
482         progress(pdata, 100);
483         return 0;
484 }
485
486 int
487 opkg_upgrade_all(opkg_progress_callback_t progress_callback, void *user_data)
488 {
489         pkg_vec_t *installed;
490         int err = 0;
491         int i;
492         pkg_t *pkg;
493         opkg_progress_data_t pdata;
494
495         pdata.action = OPKG_INSTALL;
496         pdata.pkg = NULL;
497
498         progress(pdata, 0);
499
500         installed = pkg_vec_alloc();
501         pkg_info_preinstall_check();
502
503         pkg_hash_fetch_all_installed(installed);
504         for (i = 0; i < installed->len; i++) {
505                 pkg = installed->pkgs[i];
506
507                 pdata.pkg = pkg;
508                 progress(pdata, 99 * i / installed->len);
509
510                 err += opkg_upgrade_pkg(pkg);
511         }
512         pkg_vec_free(installed);
513
514         if (err)
515                 return 1;
516
517         err = opkg_configure_packages(NULL);
518         if (err)
519                 return 1;
520
521         /* write out status files and file lists */
522         opkg_conf_write_status_files();
523         pkg_write_changed_filelists();
524
525         pdata.pkg = NULL;
526         progress(pdata, 100);
527         return 0;
528 }
529
530 int
531 opkg_update_package_lists(opkg_progress_callback_t progress_callback,
532                         void *user_data)
533 {
534         char *tmp;
535         int err, result = 0;
536         char *lists_dir;
537         pkg_src_list_elt_t *iter;
538         pkg_src_t *src;
539         int sources_list_count, sources_done;
540         opkg_progress_data_t pdata;
541
542         pdata.action = OPKG_DOWNLOAD;
543         pdata.pkg = NULL;
544         progress(pdata, 0);
545
546         sprintf_alloc(&lists_dir, "%s", (conf->restrict_to_default_dest)
547                 ? conf->default_dest->lists_dir : conf->lists_dir);
548
549         if (!file_is_dir(lists_dir)) {
550                 if (file_exists(lists_dir)) {
551                         opkg_msg(ERROR, "%s is not a directory\n", lists_dir);
552                         free(lists_dir);
553                         return 1;
554                 }
555
556                 err = file_mkdir_hier(lists_dir, 0755);
557                 if (err) {
558                         opkg_msg(ERROR, "Couldn't create lists_dir %s\n",
559                                         lists_dir);
560                         free(lists_dir);
561                         return 1;
562                 }
563         }
564
565         sprintf_alloc(&tmp, "%s/update-XXXXXX", conf->tmp_dir);
566         if (mkdtemp(tmp) == NULL) {
567                 opkg_perror(ERROR, "Coundn't create temporary directory %s",
568                                 tmp);
569                 free(lists_dir);
570                 free(tmp);
571                 return 1;
572         }
573
574         /* count the number of sources so we can give some progress updates */
575         sources_list_count = 0;
576         sources_done = 0;
577         list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
578                 sources_list_count++;
579         }
580
581         list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
582                 char *url, *list_file_name = NULL;
583
584                 src = (pkg_src_t *) iter->data;
585
586                 if (src->extra_data)    /* debian style? */
587                         sprintf_alloc(&url, "%s/%s/%s", src->value,
588                                       src->extra_data,
589                                       src->gzip ? "Packages.gz" : "Packages");
590                 else
591                         sprintf_alloc(&url, "%s/%s", src->value,
592                                       src->gzip ? "Packages.gz" : "Packages");
593
594                 sprintf_alloc(&list_file_name, "%s/%s", lists_dir, src->name);
595
596                 if (opkg_download(url, list_file_name, NULL, NULL, 0)) {
597                         opkg_msg(ERROR, "Couldn't retrieve %s\n", url);
598                         result = -1;
599                 }
600                 free(url);
601
602 #if defined(HAVE_GPGME) || defined(HAVE_OPENSSL) || defined(HAVE_USIGN)
603                 if (conf->check_signature) {
604                         char *sig_file_name;
605                         /* download detached signitures to verify the package lists */
606                         /* get the url for the sig file */
607                         if (src->extra_data)    /* debian style? */
608                                 sprintf_alloc(&url, "%s/%s/%s", src->value,
609                                               src->extra_data, "Packages.sig");
610                         else
611                                 sprintf_alloc(&url, "%s/%s", src->value,
612                                               "Packages.sig");
613
614                         /* create filename for signature */
615                         sprintf_alloc(&sig_file_name, "%s/%s.sig", lists_dir,
616                                       src->name);
617
618                         /* make sure there is no existing signature file */
619                         unlink(sig_file_name);
620
621                         err = opkg_download(url, sig_file_name, NULL, NULL, 0);
622                         if (err) {
623                                 opkg_msg(ERROR, "Couldn't retrieve %s\n", url);
624                         } else {
625                                 int err;
626                                 err = opkg_verify_file(list_file_name,
627                                                      sig_file_name);
628                                 if (err == 0) {
629                                         opkg_msg(INFO, "Signature check "
630                                                         "passed for %s",
631                                                         list_file_name);
632                                 } else {
633                                         opkg_msg(ERROR, "Signature check "
634                                                         "failed for %s",
635                                                         list_file_name);
636                                 }
637                         }
638                         free(sig_file_name);
639                         free(url);
640                 }
641 #else
642                 opkg_msg(INFO, "Signature check skipped for %s as GPG support"
643                                 " has not been enabled in this build\n",
644                                 list_file_name);
645 #endif
646                 free(list_file_name);
647
648                 sources_done++;
649                 progress(pdata, 100 * sources_done / sources_list_count);
650         }
651
652         rmdir(tmp);
653         free(tmp);
654         free(lists_dir);
655
656         /* Now re-read the package lists to update package hash tables. */
657         opkg_re_read_config_files();
658
659         return result;
660 }
661
662 static int
663 pkg_compare_names_and_version(const void *a0, const void *b0)
664 {
665         const pkg_t *a = *(const pkg_t **)a0;
666         const pkg_t *b = *(const pkg_t **)b0;
667         int ret;
668
669         ret = strcmp(a->name, b->name);
670
671         if (ret == 0)
672                 ret = pkg_compare_versions(a, b);
673
674         return ret;
675 }
676
677 int
678 opkg_list_packages(opkg_package_callback_t callback, void *user_data)
679 {
680         pkg_vec_t *all;
681         int i;
682
683         opkg_assert(callback);
684
685         all = pkg_vec_alloc();
686         pkg_hash_fetch_available(all);
687
688         pkg_vec_sort(all, pkg_compare_names_and_version);
689
690         for (i = 0; i < all->len; i++) {
691                 pkg_t *pkg;
692
693                 pkg = all->pkgs[i];
694
695                 callback(pkg, user_data);
696         }
697
698         pkg_vec_free(all);
699
700         return 0;
701 }
702
703 int
704 opkg_list_upgradable_packages(opkg_package_callback_t callback, void *user_data)
705 {
706         struct active_list *head;
707         struct active_list *node;
708         pkg_t *old = NULL, *new = NULL;
709
710         opkg_assert(callback);
711
712         /* ensure all data is valid */
713         pkg_info_preinstall_check();
714
715         head = prepare_upgrade_list();
716         for (node = active_list_next(head, head); node;
717              node = active_list_next(head, node)) {
718                 old = list_entry(node, pkg_t, list);
719                 new = pkg_hash_fetch_best_installation_candidate_by_name(old->name);
720                 if (new == NULL)
721                         continue;
722                 callback(new, user_data);
723         }
724         active_list_head_delete(head);
725         return 0;
726 }
727
728 pkg_t *
729 opkg_find_package(const char *name, const char *ver, const char *arch,
730                 const char *repo)
731 {
732         int pkg_found = 0;
733         pkg_t *pkg = NULL;
734         pkg_vec_t *all;
735         int i;
736 #define sstrcmp(x,y) (x && y) ? strcmp (x, y) : 0
737
738         all = pkg_vec_alloc();
739         pkg_hash_fetch_available(all);
740         for (i = 0; i < all->len; i++) {
741                 char *pkgv;
742
743                 pkg = all->pkgs[i];
744
745                 /* check name */
746                 if (sstrcmp(pkg->name, name))
747                         continue;
748
749                 /* check version */
750                 pkgv = pkg_version_str_alloc(pkg);
751                 if (sstrcmp(pkgv, ver)) {
752                         free(pkgv);
753                         continue;
754                 }
755                 free(pkgv);
756
757                 /* check architecture */
758                 if (arch) {
759                         if (sstrcmp(pkg->architecture, arch))
760                                 continue;
761                 }
762
763                 /* check repository */
764                 if (repo) {
765                         if (sstrcmp(pkg->src->name, repo))
766                                 continue;
767                 }
768
769                 /* match found */
770                 pkg_found = 1;
771                 break;
772         }
773
774         pkg_vec_free(all);
775
776         return pkg_found ? pkg : NULL;
777 }
778
779 /**
780  * @brief Check the accessibility of repositories.
781  * @return return how many repositories cannot access. 0 means all okay.
782  */
783 int
784 opkg_repository_accessibility_check(void)
785 {
786         pkg_src_list_elt_t *iter;
787         str_list_elt_t *iter1;
788         str_list_t *src;
789         int repositories = 0;
790         int ret = 0;
791         char *repo_ptr;
792         char *stmp;
793         char *host, *end;
794
795         src = str_list_alloc();
796
797         list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
798                 host = strstr(((pkg_src_t *)iter->data)->value, "://") + 3;
799                 end = index(host, '/');
800                 if (strstr(((pkg_src_t *) iter->data)->value, "://") && end)
801                         stmp = xstrndup(((pkg_src_t *) iter->data)->value,
802                                      end - ((pkg_src_t *) iter->data)->value);
803                 else
804                         stmp = xstrdup(((pkg_src_t *) iter->data)->value);
805
806                 for (iter1 = str_list_first(src); iter1;
807                      iter1 = str_list_next(src, iter1)) {
808                         if (strstr(iter1->data, stmp))
809                                 break;
810                 }
811                 if (iter1)
812                         continue;
813
814                 sprintf_alloc(&repo_ptr, "%s/index.html", stmp);
815                 free(stmp);
816
817                 str_list_append(src, repo_ptr);
818                 free(repo_ptr);
819                 repositories++;
820         }
821
822         while (repositories > 0) {
823                 iter1 = str_list_pop(src);
824                 repositories--;
825
826                 if (opkg_download(iter1->data, "/dev/null", NULL, NULL, 0))
827                         ret++;
828                 str_list_elt_deinit(iter1);
829         }
830
831         free(src);
832
833         return ret;
834 }