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 "opkg_conf.h"
23 #include "sprintf_alloc.h"
25 #include "opkg_message.h"
26 #include "file_util.h"
27 #include "opkg_defines.h"
28 #include "libbb/libbb.h"
30 #include <sys/types.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 { "check_signature", OPKG_OPT_TYPE_BOOL, &_conf.check_signature },
55 { "ftp_proxy", OPKG_OPT_TYPE_STRING, &_conf.ftp_proxy },
56 { "http_proxy", OPKG_OPT_TYPE_STRING, &_conf.http_proxy },
57 { "no_proxy", OPKG_OPT_TYPE_STRING, &_conf.no_proxy },
58 { "test", OPKG_OPT_TYPE_BOOL, &_conf.noaction },
59 { "noaction", OPKG_OPT_TYPE_BOOL, &_conf.noaction },
60 { "nodeps", OPKG_OPT_TYPE_BOOL, &_conf.nodeps },
61 { "offline_root", OPKG_OPT_TYPE_STRING, &_conf.offline_root },
62 { "proxy_passwd", OPKG_OPT_TYPE_STRING, &_conf.proxy_passwd },
63 { "proxy_user", OPKG_OPT_TYPE_STRING, &_conf.proxy_user },
64 { "query-all", OPKG_OPT_TYPE_BOOL, &_conf.query_all },
65 { "tmp_dir", OPKG_OPT_TYPE_STRING, &_conf.tmp_dir },
66 { "verbosity", OPKG_OPT_TYPE_INT, &_conf.verbosity },
67 #if defined(HAVE_OPENSSL)
68 { "signature_ca_file", OPKG_OPT_TYPE_STRING, &_conf.signature_ca_file },
69 { "signature_ca_path", OPKG_OPT_TYPE_STRING, &_conf.signature_ca_path },
71 #if defined(HAVE_PATHFINDER)
72 { "check_x509_path", OPKG_OPT_TYPE_BOOL, &_conf.check_x509_path },
74 #if defined(HAVE_SSLCURL) && defined(HAVE_CURL)
75 { "ssl_engine", OPKG_OPT_TYPE_STRING, &_conf.ssl_engine },
76 { "ssl_cert", OPKG_OPT_TYPE_STRING, &_conf.ssl_cert },
77 { "ssl_cert_type", OPKG_OPT_TYPE_STRING, &_conf.ssl_cert_type },
78 { "ssl_key", OPKG_OPT_TYPE_STRING, &_conf.ssl_key },
79 { "ssl_key_type", OPKG_OPT_TYPE_STRING, &_conf.ssl_key_type },
80 { "ssl_key_passwd", OPKG_OPT_TYPE_STRING, &_conf.ssl_key_passwd },
81 { "ssl_ca_file", OPKG_OPT_TYPE_STRING, &_conf.ssl_ca_file },
82 { "ssl_ca_path", OPKG_OPT_TYPE_STRING, &_conf.ssl_ca_path },
83 { "ssl_dont_verify_peer", OPKG_OPT_TYPE_BOOL, &_conf.ssl_dont_verify_peer },
89 resolve_pkg_dest_list(nv_pair_list_t *nv_pair_list)
91 nv_pair_list_elt_t *iter;
96 for (iter = nv_pair_list_first(nv_pair_list); iter;
97 iter = nv_pair_list_next(nv_pair_list, iter)) {
98 nv_pair = (nv_pair_t *)iter->data;
100 if (conf->offline_root) {
101 sprintf_alloc(&root_dir, "%s%s", conf->offline_root, nv_pair->value);
103 root_dir = xstrdup(nv_pair->value);
106 dest = pkg_dest_list_append(&conf->pkg_dest_list, nv_pair->name, root_dir, conf->lists_dir);
109 if (conf->default_dest == NULL)
110 conf->default_dest = dest;
112 if (conf->dest_str && !strcmp(dest->name, conf->dest_str)) {
113 conf->default_dest = dest;
114 conf->restrict_to_default_dest = 1;
118 if (conf->dest_str && !conf->restrict_to_default_dest) {
119 opkg_msg(ERROR, "Unknown dest name: `%s'.\n", conf->dest_str);
127 opkg_conf_set_option(const char *name, const char *value)
131 while (options[i].name) {
132 if (strcmp(options[i].name, name) == 0) {
133 switch (options[i].type) {
134 case OPKG_OPT_TYPE_BOOL:
135 if (*(int *)options[i].value) {
136 opkg_msg(ERROR, "Duplicate boolean option %s, "
137 "leaving this option on.\n", name);
140 *((int * const)options[i].value) = 1;
142 case OPKG_OPT_TYPE_INT:
144 if (*(int *)options[i].value) {
145 opkg_msg(ERROR, "Duplicate option %s, "
146 "using first seen value \"%d\".\n",
147 name, *((int *)options[i].value));
150 *((int * const)options[i].value) = atoi(value);
153 opkg_msg(ERROR, "Option %s needs an argument\n",
157 case OPKG_OPT_TYPE_STRING:
159 if (*(char **)options[i].value) {
160 opkg_msg(ERROR, "Duplicate option %s, "
161 "using first seen value \"%s\".\n",
162 name, *((char **)options[i].value));
165 *((char ** const)options[i].value) = xstrdup(value);
168 opkg_msg(ERROR, "Option %s needs an argument\n",
177 opkg_msg(ERROR, "Unrecognized option: %s=%s\n", name, value);
182 opkg_conf_parse_file(const char *filename,
183 pkg_src_list_t *pkg_src_list,
184 nv_pair_list_t *tmp_dest_nv_pair_list)
189 regex_t valid_line_re, comment_re;
190 #define regmatch_size 12
191 regmatch_t regmatch[regmatch_size];
193 file = fopen(filename, "r");
195 opkg_perror(ERROR, "Failed to open %s", filename);
200 opkg_msg(INFO, "Loading conf file %s.\n", filename);
202 err = xregcomp(&comment_re,
203 "^[[:space:]]*(#.*|[[:space:]]*)$",
208 err = xregcomp(&valid_line_re,
209 "^[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))"
210 "[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))"
211 "[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))"
212 "([[:space:]]+([^[:space:]]+))?[[:space:]]*$",
219 char *type, *name, *value, *extra;
223 line = file_read_line_alloc(file);
227 if (regexec(&comment_re, line, 0, 0, 0) == 0)
230 if (regexec(&valid_line_re, line, regmatch_size, regmatch, 0) == REG_NOMATCH) {
231 opkg_msg(ERROR, "%s:%d: Ignoring invalid line: `%s'\n",
232 filename, line_num, line);
236 /* This has to be so ugly to deal with optional quotation marks */
237 if (regmatch[2].rm_so > 0) {
238 type = xstrndup(line + regmatch[2].rm_so,
239 regmatch[2].rm_eo - regmatch[2].rm_so);
241 type = xstrndup(line + regmatch[3].rm_so,
242 regmatch[3].rm_eo - regmatch[3].rm_so);
245 if (regmatch[5].rm_so > 0) {
246 name = xstrndup(line + regmatch[5].rm_so,
247 regmatch[5].rm_eo - regmatch[5].rm_so);
249 name = xstrndup(line + regmatch[6].rm_so,
250 regmatch[6].rm_eo - regmatch[6].rm_so);
253 if (regmatch[8].rm_so > 0) {
254 value = xstrndup(line + regmatch[8].rm_so,
255 regmatch[8].rm_eo - regmatch[8].rm_so);
257 value = xstrndup(line + regmatch[9].rm_so,
258 regmatch[9].rm_eo - regmatch[9].rm_so);
262 if (regmatch[11].rm_so > 0) {
263 extra = xstrndup (line + regmatch[11].rm_so,
264 regmatch[11].rm_eo - regmatch[11].rm_so);
267 /* We use the tmp_dest_nv_pair_list below instead of
268 conf->pkg_dest_list because we might encounter an
269 offline_root option later and that would invalidate the
270 directories we would have computed in
271 pkg_dest_list_init. (We do a similar thing with
272 tmp_src_nv_pair_list for sake of symmetry.) */
273 if (strcmp(type, "option") == 0) {
274 opkg_conf_set_option(name, value);
275 } else if (strcmp(type, "src") == 0) {
276 if (!nv_pair_list_find((nv_pair_list_t*) pkg_src_list, name)) {
277 pkg_src_list_append (pkg_src_list, name, value, extra, 0);
279 opkg_msg(ERROR, "Duplicate src declaration (%s %s). "
280 "Skipping.\n", name, value);
282 } else if (strcmp(type, "src/gz") == 0) {
283 if (!nv_pair_list_find((nv_pair_list_t*) pkg_src_list, name)) {
284 pkg_src_list_append (pkg_src_list, name, value, extra, 1);
286 opkg_msg(ERROR, "Duplicate src declaration (%s %s). "
287 "Skipping.\n", name, value);
289 } else if (strcmp(type, "dest") == 0) {
290 nv_pair_list_append(tmp_dest_nv_pair_list, name, value);
291 } else if (strcmp(type, "lists_dir") == 0) {
292 conf->lists_dir = xstrdup(value);
293 } else if (strcmp(type, "arch") == 0) {
294 opkg_msg(INFO, "Supported arch %s priority (%s)\n", name, value);
296 opkg_msg(NOTICE, "No priority given for architecture %s,"
297 "defaulting to 10\n", name);
298 value = xstrdup("10");
300 nv_pair_list_append(&conf->arch_list, name, value);
302 opkg_msg(ERROR, "%s:%d: Ignoring invalid line: `%s'\n",
303 filename, line_num, line);
316 regfree(&valid_line_re);
318 regfree(&comment_re);
320 if (fclose(file) == EOF) {
321 opkg_perror(ERROR, "Couldn't close %s", filename);
329 opkg_conf_write_status_files(void)
331 pkg_dest_list_elt_t *iter;
340 list_for_each_entry(iter, &conf->pkg_dest_list.head, node) {
341 dest = (pkg_dest_t *)iter->data;
343 dest->status_fp = fopen(dest->status_file_name, "w");
344 if (dest->status_fp == NULL) {
345 opkg_perror(ERROR, "Can't open status file %s",
346 dest->status_file_name);
351 all = pkg_vec_alloc();
352 pkg_hash_fetch_available(all);
354 for(i = 0; i < all->len; i++) {
356 /* We don't need most uninstalled packages in the status file */
357 if (pkg->state_status == SS_NOT_INSTALLED
358 && (pkg->state_want == SW_UNKNOWN
359 || pkg->state_want == SW_DEINSTALL
360 || pkg->state_want == SW_PURGE)) {
363 if (pkg->dest == NULL) {
364 opkg_msg(ERROR, "Internal error: package %s has a NULL dest\n",
368 if (pkg->dest->status_fp)
369 pkg_print_status(pkg, pkg->dest->status_fp);
374 list_for_each_entry(iter, &conf->pkg_dest_list.head, node) {
375 dest = (pkg_dest_t *)iter->data;
376 if (fclose(dest->status_fp) == EOF) {
377 opkg_perror(ERROR, "Couldn't close %s", dest->status_file_name);
387 root_filename_alloc(char *filename)
390 sprintf_alloc(&root_filename, "%s%s",
391 (conf->offline_root ? conf->offline_root : ""), filename);
392 return root_filename;
396 glob_errfunc(const char *epath, int eerrno)
398 if (eerrno == ENOENT)
399 /* If leading dir does not exist, we get GLOB_NOMATCH. */
402 opkg_msg(ERROR, "glob failed for %s: %s\n", epath, strerror(eerrno));
410 char *tmp, *tmp_dir_base, **tmp_val;
411 nv_pair_list_t tmp_dest_nv_pair_list;
413 char *etc_opkg_conf_pattern;
415 conf->restrict_to_default_dest = 0;
416 conf->default_dest = NULL;
417 #if defined(HAVE_PATHFINDER)
418 conf->check_x509_path = 1;
421 pkg_src_list_init(&conf->pkg_src_list);
422 pkg_dest_list_init(&conf->pkg_dest_list);
423 nv_pair_list_init(&conf->arch_list);
424 nv_pair_list_init(&tmp_dest_nv_pair_list);
426 if (!conf->offline_root)
427 conf->offline_root = xstrdup(getenv("OFFLINE_ROOT"));
429 if (conf->conf_file) {
431 if (stat(conf->conf_file, &st) == -1) {
432 opkg_perror(ERROR, "Couldn't stat %s", conf->conf_file);
435 if (opkg_conf_parse_file(conf->conf_file,
436 &conf->pkg_src_list, &tmp_dest_nv_pair_list))
440 if (conf->offline_root)
441 sprintf_alloc(&etc_opkg_conf_pattern, "%s/etc/opkg/*.conf", conf->offline_root);
443 const char *conf_file_dir = getenv("OPKG_CONF_DIR");
444 if (conf_file_dir == NULL)
445 conf_file_dir = OPKG_CONF_DEFAULT_CONF_FILE_DIR;
446 sprintf_alloc(&etc_opkg_conf_pattern, "%s/*.conf", conf_file_dir);
449 memset(&globbuf, 0, sizeof(globbuf));
450 glob_ret = glob(etc_opkg_conf_pattern, 0, glob_errfunc, &globbuf);
451 if (glob_ret && glob_ret != GLOB_NOMATCH) {
452 free(etc_opkg_conf_pattern);
457 free(etc_opkg_conf_pattern);
459 for (i = 0; i < globbuf.gl_pathc; i++) {
460 if (globbuf.gl_pathv[i])
461 if (conf->conf_file &&
462 !strcmp(conf->conf_file, globbuf.gl_pathv[i]))
464 if ( opkg_conf_parse_file(globbuf.gl_pathv[i],
465 &conf->pkg_src_list, &tmp_dest_nv_pair_list)<0) {
473 if (conf->offline_root)
474 sprintf_alloc (&lock_file, "%s/%s/lock", conf->offline_root, OPKG_STATE_DIR_PREFIX);
476 sprintf_alloc (&lock_file, "%s/lock", OPKG_STATE_DIR_PREFIX);
478 lock_fd = creat(lock_file, S_IRUSR | S_IWUSR | S_IRGRP);
480 opkg_perror(ERROR, "Could not create lock file %s", lock_file);
484 if (lockf(lock_fd, F_TLOCK, (off_t)0) == -1) {
485 opkg_perror(ERROR, "Could not lock %s", lock_file);
490 tmp_dir_base = conf->tmp_dir;
492 tmp_dir_base = getenv("TMPDIR");
494 sprintf_alloc(&tmp, "%s/%s",
495 tmp_dir_base ? tmp_dir_base : OPKG_CONF_DEFAULT_TMP_DIR_BASE,
496 OPKG_CONF_TMP_DIR_SUFFIX);
499 conf->tmp_dir = mkdtemp(tmp);
500 if (conf->tmp_dir == NULL) {
501 opkg_perror(ERROR, "Creating temp dir %s failed", tmp);
506 hash_table_init("file-hash", &conf->file_hash, OPKG_CONF_DEFAULT_HASH_LEN);
507 hash_table_init("obs-file-hash", &conf->obs_file_hash, OPKG_CONF_DEFAULT_HASH_LEN/16);
509 if (conf->lists_dir == NULL)
510 conf->lists_dir = xstrdup(OPKG_CONF_LISTS_DIR);
512 if (conf->offline_root) {
513 sprintf_alloc(&tmp, "%s/%s", conf->offline_root, conf->lists_dir);
514 free(conf->lists_dir);
515 conf->lists_dir = tmp;
518 /* if no architectures were defined, then default all, noarch, and host architecture */
519 if (nv_pair_list_empty(&conf->arch_list)) {
520 nv_pair_list_append(&conf->arch_list, "all", "1");
521 nv_pair_list_append(&conf->arch_list, "noarch", "1");
522 nv_pair_list_append(&conf->arch_list, HOST_CPU_STR, "10");
525 /* Even if there is no conf file, we'll need at least one dest. */
526 if (nv_pair_list_empty(&tmp_dest_nv_pair_list)) {
527 nv_pair_list_append(&tmp_dest_nv_pair_list,
528 OPKG_CONF_DEFAULT_DEST_NAME,
529 OPKG_CONF_DEFAULT_DEST_ROOT_DIR);
532 if (resolve_pkg_dest_list(&tmp_dest_nv_pair_list))
535 nv_pair_list_deinit(&tmp_dest_nv_pair_list);
541 free(conf->lists_dir);
544 hash_table_deinit(&conf->file_hash);
545 hash_table_deinit(&conf->obs_file_hash);
547 if (rmdir(conf->tmp_dir) == -1)
548 opkg_perror(ERROR, "Couldn't remove dir %s", conf->tmp_dir);
550 if (lockf(lock_fd, F_ULOCK, (off_t)0) == -1)
551 opkg_perror(ERROR, "Couldn't unlock %s", lock_file);
553 if (close(lock_fd) == -1)
554 opkg_perror(ERROR, "Couldn't close descriptor %d (%s)",
556 if (unlink(lock_file) == -1)
557 opkg_perror(ERROR, "Couldn't unlink %s", lock_file);
561 pkg_src_list_deinit(&conf->pkg_src_list);
562 pkg_dest_list_deinit(&conf->pkg_dest_list);
563 nv_pair_list_deinit(&conf->arch_list);
565 for (i=0; options[i].name; i++) {
566 if (options[i].type == OPKG_OPT_TYPE_STRING) {
567 tmp_val = (char **)options[i].value;
575 nv_pair_list_deinit(&tmp_dest_nv_pair_list);
577 free(conf->dest_str);
579 free(conf->conf_file);
585 opkg_conf_deinit(void)
592 free(conf->lists_dir);
595 free(conf->dest_str);
598 free(conf->conf_file);
600 pkg_src_list_deinit(&conf->pkg_src_list);
601 pkg_dest_list_deinit(&conf->pkg_dest_list);
602 nv_pair_list_deinit(&conf->arch_list);
604 for (i=0; options[i].name; i++) {
605 if (options[i].type == OPKG_OPT_TYPE_STRING) {
606 tmp = (char **)options[i].value;
614 if (conf->verbosity >= DEBUG) {
615 hash_print_stats(&conf->pkg_hash);
616 hash_print_stats(&conf->file_hash);
617 hash_print_stats(&conf->obs_file_hash);
621 hash_table_deinit(&conf->file_hash);
622 hash_table_deinit(&conf->obs_file_hash);
624 if (lockf(lock_fd, F_ULOCK, (off_t)0) == -1)
625 opkg_perror(ERROR, "Couldn't unlock %s", lock_file);
627 if (close(lock_fd) == -1)
628 opkg_perror(ERROR, "Couldn't close descriptor %d (%s)",
631 if (unlink(lock_file) == -1)
632 opkg_perror(ERROR, "Couldn't unlink %s", lock_file);