1 /* opkg_conf.c - the opkg package management system
3 Copyright (C) 2009 Ubiq Technologies <graham.gower@gmail.com>
6 Copyright (C) 2001 University of Southern California
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2, or (at
11 your option) any later version.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
20 #include <sys/types.h>
26 #include "opkg_conf.h"
30 #include "sprintf_alloc.h"
31 #include "opkg_message.h"
32 #include "file_util.h"
33 #include "opkg_defines.h"
34 #include "libbb/libbb.h"
37 static char *lock_file = NULL;
39 static opkg_conf_t _conf;
40 opkg_conf_t *conf = &_conf;
45 opkg_option_t options[] = {
46 {"cache", OPKG_OPT_TYPE_STRING, &_conf.cache},
47 {"force_defaults", OPKG_OPT_TYPE_BOOL, &_conf.force_defaults},
48 {"force_maintainer", OPKG_OPT_TYPE_BOOL, &_conf.force_maintainer},
49 {"force_depends", OPKG_OPT_TYPE_BOOL, &_conf.force_depends},
50 {"force_overwrite", OPKG_OPT_TYPE_BOOL, &_conf.force_overwrite},
51 {"force_downgrade", OPKG_OPT_TYPE_BOOL, &_conf.force_downgrade},
52 {"force_reinstall", OPKG_OPT_TYPE_BOOL, &_conf.force_reinstall},
53 {"force_space", OPKG_OPT_TYPE_BOOL, &_conf.force_space},
54 {"force_postinstall", OPKG_OPT_TYPE_BOOL, &_conf.force_postinstall},
55 {"force_checksum", OPKG_OPT_TYPE_BOOL, &_conf.force_checksum},
56 {"check_signature", OPKG_OPT_TYPE_BOOL, &_conf.check_signature},
57 {"ftp_proxy", OPKG_OPT_TYPE_STRING, &_conf.ftp_proxy},
58 {"http_proxy", OPKG_OPT_TYPE_STRING, &_conf.http_proxy},
59 {"no_proxy", OPKG_OPT_TYPE_STRING, &_conf.no_proxy},
60 {"test", OPKG_OPT_TYPE_BOOL, &_conf.noaction},
61 {"noaction", OPKG_OPT_TYPE_BOOL, &_conf.noaction},
62 {"download_only", OPKG_OPT_TYPE_BOOL, &_conf.download_only},
63 {"nodeps", OPKG_OPT_TYPE_BOOL, &_conf.nodeps},
64 {"nocase", OPKG_OPT_TYPE_BOOL, &_conf.nocase},
65 {"offline_root", OPKG_OPT_TYPE_STRING, &_conf.offline_root},
66 {"overlay_root", OPKG_OPT_TYPE_STRING, &_conf.overlay_root},
67 {"proxy_passwd", OPKG_OPT_TYPE_STRING, &_conf.proxy_passwd},
68 {"proxy_user", OPKG_OPT_TYPE_STRING, &_conf.proxy_user},
69 {"query-all", OPKG_OPT_TYPE_BOOL, &_conf.query_all},
70 {"size", OPKG_OPT_TYPE_BOOL, &_conf.size},
71 {"tmp_dir", OPKG_OPT_TYPE_STRING, &_conf.tmp_dir},
72 {"verbosity", OPKG_OPT_TYPE_INT, &_conf.verbosity},
73 #if defined(HAVE_OPENSSL)
74 {"signature_ca_file", OPKG_OPT_TYPE_STRING, &_conf.signature_ca_file},
75 {"signature_ca_path", OPKG_OPT_TYPE_STRING, &_conf.signature_ca_path},
77 #if defined(HAVE_PATHFINDER)
78 {"check_x509_path", OPKG_OPT_TYPE_BOOL, &_conf.check_x509_path},
80 #if defined(HAVE_SSLCURL) && defined(HAVE_CURL)
81 {"ssl_engine", OPKG_OPT_TYPE_STRING, &_conf.ssl_engine},
82 {"ssl_cert", OPKG_OPT_TYPE_STRING, &_conf.ssl_cert},
83 {"ssl_cert_type", OPKG_OPT_TYPE_STRING, &_conf.ssl_cert_type},
84 {"ssl_key", OPKG_OPT_TYPE_STRING, &_conf.ssl_key},
85 {"ssl_key_type", OPKG_OPT_TYPE_STRING, &_conf.ssl_key_type},
86 {"ssl_key_passwd", OPKG_OPT_TYPE_STRING, &_conf.ssl_key_passwd},
87 {"ssl_ca_file", OPKG_OPT_TYPE_STRING, &_conf.ssl_ca_file},
88 {"ssl_ca_path", OPKG_OPT_TYPE_STRING, &_conf.ssl_ca_path},
89 {"ssl_dont_verify_peer", OPKG_OPT_TYPE_BOOL,
90 &_conf.ssl_dont_verify_peer},
95 static int resolve_pkg_dest_list(void)
97 nv_pair_list_elt_t *iter;
102 for (iter = nv_pair_list_first(&conf->tmp_dest_list); iter;
103 iter = nv_pair_list_next(&conf->tmp_dest_list, iter)) {
104 nv_pair = (nv_pair_t *) iter->data;
106 if (conf->offline_root) {
107 sprintf_alloc(&root_dir, "%s%s", conf->offline_root,
110 root_dir = xstrdup(nv_pair->value);
114 pkg_dest_list_append(&conf->pkg_dest_list, nv_pair->name,
115 root_dir, conf->lists_dir);
118 if (conf->default_dest == NULL)
119 conf->default_dest = dest;
121 if (conf->dest_str && !strcmp(dest->name, conf->dest_str)) {
122 conf->default_dest = dest;
123 conf->restrict_to_default_dest = 1;
127 if (conf->dest_str && !conf->restrict_to_default_dest) {
128 opkg_msg(ERROR, "Unknown dest name: `%s'.\n", conf->dest_str);
135 static int opkg_conf_set_option(const char *name, const char *value)
139 while (options[i].name) {
140 if (strcmp(options[i].name, name) == 0) {
141 switch (options[i].type) {
142 case OPKG_OPT_TYPE_BOOL:
143 if (*(int *)options[i].value) {
145 "Duplicate boolean option %s, "
146 "leaving this option on.\n",
150 *((int *const)options[i].value) = 1;
152 case OPKG_OPT_TYPE_INT:
154 if (*(int *)options[i].value) {
156 "Duplicate option %s, "
157 "using first seen value \"%d\".\n",
163 *((int *const)options[i].value) =
168 "Option %s needs an argument\n",
172 case OPKG_OPT_TYPE_STRING:
174 if (*(char **)options[i].value) {
176 "Duplicate option %s, "
177 "using first seen value \"%s\".\n",
179 *((char **)options[i].
183 *((char **const)options[i].value) =
188 "Option %s needs an argument\n",
197 opkg_msg(ERROR, "Unrecognized option: %s=%s\n", name, value);
202 opkg_conf_parse_file(const char *filename,
203 pkg_src_list_t * pkg_src_list,
204 pkg_src_list_t * dist_src_list)
209 regex_t valid_line_re, comment_re;
210 #define regmatch_size 14
211 regmatch_t regmatch[regmatch_size];
213 file = fopen(filename, "r");
215 opkg_perror(ERROR, "Failed to open %s", filename);
220 opkg_msg(INFO, "Loading conf file %s.\n", filename);
222 err = xregcomp(&comment_re,
223 "^[[:space:]]*(#.*|[[:space:]]*)$", REG_EXTENDED);
227 err = xregcomp(&valid_line_re,
228 "^[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))"
229 "[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))"
230 "[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))"
231 "([[:space:]]+([^[:space:]]+))?([[:space:]]+(.*))?[[:space:]]*$",
238 char *type, *name, *value, *extra;
242 line = file_read_line_alloc(file);
246 if (regexec(&comment_re, line, 0, 0, 0) == 0)
249 if (regexec(&valid_line_re, line, regmatch_size, regmatch, 0) ==
251 opkg_msg(ERROR, "%s:%d: Ignoring invalid line: `%s'\n",
252 filename, line_num, line);
256 /* This has to be so ugly to deal with optional quotation marks */
257 if (regmatch[2].rm_so > 0) {
258 type = xstrndup(line + regmatch[2].rm_so,
259 regmatch[2].rm_eo - regmatch[2].rm_so);
261 type = xstrndup(line + regmatch[3].rm_so,
262 regmatch[3].rm_eo - regmatch[3].rm_so);
265 if (regmatch[5].rm_so > 0) {
266 name = xstrndup(line + regmatch[5].rm_so,
267 regmatch[5].rm_eo - regmatch[5].rm_so);
269 name = xstrndup(line + regmatch[6].rm_so,
270 regmatch[6].rm_eo - regmatch[6].rm_so);
273 if (regmatch[8].rm_so > 0) {
274 value = xstrndup(line + regmatch[8].rm_so,
275 regmatch[8].rm_eo - regmatch[8].rm_so);
277 value = xstrndup(line + regmatch[9].rm_so,
278 regmatch[9].rm_eo - regmatch[9].rm_so);
282 if (regmatch[11].rm_so > 0) {
283 if (regmatch[13].rm_so > 0
284 && regmatch[13].rm_so != regmatch[13].rm_eo)
286 xstrndup(line + regmatch[11].rm_so,
290 extra = xstrndup(line + regmatch[11].rm_so,
295 if (regmatch[13].rm_so != regmatch[13].rm_eo
296 && strncmp(type, "dist", 4) != 0) {
298 "%s:%d: Ignoring config line with trailing garbage: `%s'\n",
299 filename, line_num, line);
302 /* We use the conf->tmp_dest_list below instead of
303 conf->pkg_dest_list because we might encounter an
304 offline_root option later and that would invalidate the
305 directories we would have computed in
306 pkg_dest_list_init. (We do a similar thing with
307 tmp_src_nv_pair_list for sake of symmetry.) */
308 if (strcmp(type, "option") == 0) {
309 opkg_conf_set_option(name, value);
310 } else if (strcmp(type, "dist") == 0) {
311 if (!nv_pair_list_find
312 ((nv_pair_list_t *) dist_src_list, name)) {
313 pkg_src_list_append(dist_src_list, name,
317 "Duplicate dist declaration (%s %s). "
318 "Skipping.\n", name, value);
320 } else if (strcmp(type, "dist/gz") == 0) {
321 if (!nv_pair_list_find
322 ((nv_pair_list_t *) dist_src_list, name)) {
323 pkg_src_list_append(dist_src_list, name,
327 "Duplicate dist declaration (%s %s). "
328 "Skipping.\n", name, value);
330 } else if (strcmp(type, "src") == 0) {
331 if (!nv_pair_list_find
332 ((nv_pair_list_t *) pkg_src_list, name)) {
333 pkg_src_list_append(pkg_src_list, name,
337 "Duplicate src declaration (%s %s). "
338 "Skipping.\n", name, value);
340 } else if (strcmp(type, "src/gz") == 0) {
341 if (!nv_pair_list_find
342 ((nv_pair_list_t *) pkg_src_list, name)) {
343 pkg_src_list_append(pkg_src_list, name,
347 "Duplicate src declaration (%s %s). "
348 "Skipping.\n", name, value);
350 } else if (strcmp(type, "dest") == 0) {
351 nv_pair_list_append(&conf->tmp_dest_list, name,
353 } else if (strcmp(type, "lists_dir") == 0) {
354 conf->lists_dir = xstrdup(value);
355 } else if (strcmp(type, "arch") == 0) {
357 "Supported arch %s priority (%s)\n",
361 "No priority given for architecture %s,"
362 "defaulting to 10\n", name);
363 value = xstrdup("10");
365 nv_pair_list_append(&conf->arch_list, name,
369 "%s:%d: Ignoring invalid line: `%s'\n",
370 filename, line_num, line);
385 regfree(&valid_line_re);
387 regfree(&comment_re);
389 if (fclose(file) == EOF) {
390 opkg_perror(ERROR, "Couldn't close %s", filename);
397 int opkg_conf_write_status_files(void)
399 pkg_dest_list_elt_t *iter;
408 list_for_each_entry(iter, &conf->pkg_dest_list.head, node) {
409 dest = (pkg_dest_t *) iter->data;
411 dest->status_fp = fopen(dest->status_file_name, "w");
412 if (dest->status_fp == NULL && errno != EROFS) {
413 opkg_perror(ERROR, "Can't open status file %s",
414 dest->status_file_name);
419 all = pkg_vec_alloc();
420 pkg_hash_fetch_available(all);
422 for (i = 0; i < all->len; i++) {
424 /* We don't need most uninstalled packages in the status file */
425 if (pkg->state_status == SS_NOT_INSTALLED
426 && (pkg->state_want == SW_UNKNOWN
427 || (pkg->state_want == SW_DEINSTALL
428 && pkg->state_flag != SF_HOLD)
429 || pkg->state_want == SW_PURGE)) {
432 if (pkg->dest == NULL) {
434 "Internal error: package %s has a NULL dest\n",
438 if (pkg->dest->status_fp)
439 pkg_print_status(pkg, pkg->dest->status_fp);
444 list_for_each_entry(iter, &conf->pkg_dest_list.head, node) {
445 dest = (pkg_dest_t *) iter->data;
446 if (dest->status_fp && fclose(dest->status_fp) == EOF) {
447 opkg_perror(ERROR, "Couldn't close %s",
448 dest->status_file_name);
456 char *root_filename_alloc(char *filename)
459 sprintf_alloc(&root_filename, "%s%s",
460 (conf->offline_root ? conf->offline_root : ""), filename);
461 return root_filename;
464 static int glob_errfunc(const char *epath, int eerrno)
466 if (eerrno == ENOENT)
467 /* If leading dir does not exist, we get GLOB_NOMATCH. */
470 opkg_msg(ERROR, "glob failed for %s: %s\n", epath, strerror(eerrno));
474 int opkg_conf_init(void)
476 pkg_src_list_init(&conf->pkg_src_list);
477 pkg_src_list_init(&conf->dist_src_list);
478 pkg_dest_list_init(&conf->pkg_dest_list);
479 pkg_dest_list_init(&conf->tmp_dest_list);
480 nv_pair_list_init(&conf->arch_list);
485 int opkg_conf_load(void)
488 char *tmp, *tmp_dir_base, **tmp_val;
490 char *etc_opkg_conf_pattern;
492 conf->restrict_to_default_dest = 0;
493 conf->default_dest = NULL;
494 #if defined(HAVE_PATHFINDER)
495 conf->check_x509_path = 1;
498 if (!conf->offline_root)
499 conf->offline_root = xstrdup(getenv("OFFLINE_ROOT"));
501 if (conf->conf_file) {
503 if (stat(conf->conf_file, &st) == -1) {
504 opkg_perror(ERROR, "Couldn't stat %s", conf->conf_file);
507 if (opkg_conf_parse_file(conf->conf_file,
509 &conf->dist_src_list))
513 if (conf->offline_root)
514 sprintf_alloc(&etc_opkg_conf_pattern, "%s/etc/opkg/*.conf",
517 const char *conf_file_dir = getenv("OPKG_CONF_DIR");
518 if (conf_file_dir == NULL)
519 conf_file_dir = OPKG_CONF_DEFAULT_CONF_FILE_DIR;
520 sprintf_alloc(&etc_opkg_conf_pattern, "%s/*.conf",
524 memset(&globbuf, 0, sizeof(globbuf));
525 glob_ret = glob(etc_opkg_conf_pattern, 0, glob_errfunc, &globbuf);
526 if (glob_ret && glob_ret != GLOB_NOMATCH) {
527 free(etc_opkg_conf_pattern);
532 free(etc_opkg_conf_pattern);
534 for (i = 0; i < globbuf.gl_pathc; i++) {
535 if (globbuf.gl_pathv[i])
536 if (conf->conf_file &&
537 !strcmp(conf->conf_file, globbuf.gl_pathv[i]))
539 if (opkg_conf_parse_file(globbuf.gl_pathv[i],
541 &conf->dist_src_list) < 0) {
549 if (conf->offline_root)
550 sprintf_alloc(&lock_file, "%s/%s", conf->offline_root,
553 sprintf_alloc(&lock_file, "%s", OPKGLOCKFILE);
555 lock_fd = creat(lock_file, S_IRUSR | S_IWUSR | S_IRGRP);
557 opkg_perror(ERROR, "Could not create lock file %s", lock_file);
561 if (lockf(lock_fd, F_TLOCK, (off_t) 0) == -1) {
562 opkg_perror(ERROR, "Could not lock %s", lock_file);
563 if (close(lock_fd) == -1)
564 opkg_perror(ERROR, "Couldn't close descriptor %d (%s)",
571 tmp_dir_base = conf->tmp_dir;
573 tmp_dir_base = getenv("TMPDIR");
575 sprintf_alloc(&tmp, "%s/%s",
576 tmp_dir_base ? tmp_dir_base :
577 OPKG_CONF_DEFAULT_TMP_DIR_BASE, OPKG_CONF_TMP_DIR_SUFFIX);
580 conf->tmp_dir = mkdtemp(tmp);
581 if (conf->tmp_dir == NULL) {
582 opkg_perror(ERROR, "Creating temp dir %s failed", tmp);
587 hash_table_init("file-hash", &conf->file_hash,
588 OPKG_CONF_DEFAULT_HASH_LEN);
589 hash_table_init("obs-file-hash", &conf->obs_file_hash,
590 OPKG_CONF_DEFAULT_HASH_LEN / 16);
592 if (conf->lists_dir == NULL)
593 conf->lists_dir = xstrdup(OPKG_CONF_LISTS_DIR);
595 if (conf->offline_root) {
596 sprintf_alloc(&tmp, "%s/%s", conf->offline_root,
598 free(conf->lists_dir);
599 conf->lists_dir = tmp;
602 /* if no architectures were defined, then default all, noarch, and host architecture */
603 if (nv_pair_list_empty(&conf->arch_list)) {
604 nv_pair_list_append(&conf->arch_list, "all", "1");
605 nv_pair_list_append(&conf->arch_list, "noarch", "1");
606 nv_pair_list_append(&conf->arch_list, HOST_CPU_STR, "10");
609 /* Even if there is no conf file, we'll need at least one dest. */
610 if (nv_pair_list_empty(&conf->tmp_dest_list)) {
611 nv_pair_list_append(&conf->tmp_dest_list,
612 OPKG_CONF_DEFAULT_DEST_NAME,
613 OPKG_CONF_DEFAULT_DEST_ROOT_DIR);
616 if (resolve_pkg_dest_list())
619 nv_pair_list_deinit(&conf->tmp_dest_list);
624 free(conf->lists_dir);
627 hash_table_deinit(&conf->file_hash);
628 hash_table_deinit(&conf->obs_file_hash);
630 if (rmdir(conf->tmp_dir) == -1)
631 opkg_perror(ERROR, "Couldn't remove dir %s", conf->tmp_dir);
633 if (lockf(lock_fd, F_ULOCK, (off_t) 0) == -1)
634 opkg_perror(ERROR, "Couldn't unlock %s", lock_file);
636 if (close(lock_fd) == -1)
637 opkg_perror(ERROR, "Couldn't close descriptor %d (%s)",
639 if (unlink(lock_file) == -1)
640 opkg_perror(ERROR, "Couldn't unlink %s", lock_file);
647 pkg_src_list_deinit(&conf->pkg_src_list);
648 pkg_src_list_deinit(&conf->dist_src_list);
649 pkg_dest_list_deinit(&conf->pkg_dest_list);
650 nv_pair_list_deinit(&conf->arch_list);
652 for (i = 0; options[i].name; i++) {
653 if (options[i].type == OPKG_OPT_TYPE_STRING) {
654 tmp_val = (char **)options[i].value;
662 nv_pair_list_deinit(&conf->tmp_dest_list);
664 free(conf->dest_str);
666 free(conf->conf_file);
671 void opkg_conf_deinit(void)
680 free(conf->lists_dir);
683 free(conf->dest_str);
686 free(conf->conf_file);
688 pkg_src_list_deinit(&conf->pkg_src_list);
689 pkg_src_list_deinit(&conf->dist_src_list);
690 pkg_dest_list_deinit(&conf->pkg_dest_list);
691 nv_pair_list_deinit(&conf->arch_list);
693 for (i = 0; options[i].name; i++) {
694 if (options[i].type == OPKG_OPT_TYPE_STRING) {
695 tmp = (char **)options[i].value;
703 if (conf->verbosity >= DEBUG) {
704 hash_print_stats(&conf->pkg_hash);
705 hash_print_stats(&conf->file_hash);
706 hash_print_stats(&conf->obs_file_hash);
710 hash_table_deinit(&conf->file_hash);
711 hash_table_deinit(&conf->obs_file_hash);
714 if (lockf(lock_fd, F_ULOCK, (off_t) 0) == -1)
715 opkg_perror(ERROR, "Couldn't unlock %s", lock_file);
717 if (close(lock_fd) == -1)
718 opkg_perror(ERROR, "Couldn't close descriptor %d (%s)",
724 if (unlink(lock_file) == -1)
725 opkg_perror(ERROR, "Couldn't unlink %s", lock_file);