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"
24 #include "opkg_message.h"
25 #include "file_util.h"
26 #include "opkg_defines.h"
27 #include "libbb/libbb.h"
29 #include <sys/types.h>
36 static char *lock_file = NULL;
38 static opkg_conf_t _conf;
39 opkg_conf_t *conf = &_conf;
44 opkg_option_t options[] = {
45 { "cache", OPKG_OPT_TYPE_STRING, &_conf.cache},
46 { "force_defaults", OPKG_OPT_TYPE_BOOL, &_conf.force_defaults },
47 { "force_maintainer", OPKG_OPT_TYPE_BOOL, &_conf.force_maintainer },
48 { "force_depends", OPKG_OPT_TYPE_BOOL, &_conf.force_depends },
49 { "force_overwrite", OPKG_OPT_TYPE_BOOL, &_conf.force_overwrite },
50 { "force_downgrade", OPKG_OPT_TYPE_BOOL, &_conf.force_downgrade },
51 { "force_reinstall", OPKG_OPT_TYPE_BOOL, &_conf.force_reinstall },
52 { "force_space", OPKG_OPT_TYPE_BOOL, &_conf.force_space },
53 { "check_signature", OPKG_OPT_TYPE_BOOL, &_conf.check_signature },
54 { "ftp_proxy", OPKG_OPT_TYPE_STRING, &_conf.ftp_proxy },
55 { "http_proxy", OPKG_OPT_TYPE_STRING, &_conf.http_proxy },
56 { "no_proxy", OPKG_OPT_TYPE_STRING, &_conf.no_proxy },
57 { "test", OPKG_OPT_TYPE_BOOL, &_conf.noaction },
58 { "noaction", OPKG_OPT_TYPE_BOOL, &_conf.noaction },
59 { "nodeps", OPKG_OPT_TYPE_BOOL, &_conf.nodeps },
60 { "offline_root", OPKG_OPT_TYPE_STRING, &_conf.offline_root },
61 { "proxy_passwd", OPKG_OPT_TYPE_STRING, &_conf.proxy_passwd },
62 { "proxy_user", OPKG_OPT_TYPE_STRING, &_conf.proxy_user },
63 { "query-all", OPKG_OPT_TYPE_BOOL, &_conf.query_all },
64 { "tmp_dir", OPKG_OPT_TYPE_STRING, &_conf.tmp_dir },
65 { "verbosity", OPKG_OPT_TYPE_INT, &_conf.verbosity },
66 #if defined(HAVE_OPENSSL)
67 { "signature_ca_file", OPKG_OPT_TYPE_STRING, &_conf.signature_ca_file },
68 { "signature_ca_path", OPKG_OPT_TYPE_STRING, &_conf.signature_ca_path },
70 #if defined(HAVE_PATHFINDER)
71 { "check_x509_path", OPKG_OPT_TYPE_BOOL, &_conf.check_x509_path },
73 #if defined(HAVE_SSLCURL) && defined(HAVE_CURL)
74 { "ssl_engine", OPKG_OPT_TYPE_STRING, &_conf.ssl_engine },
75 { "ssl_cert", OPKG_OPT_TYPE_STRING, &_conf.ssl_cert },
76 { "ssl_cert_type", OPKG_OPT_TYPE_STRING, &_conf.ssl_cert_type },
77 { "ssl_key", OPKG_OPT_TYPE_STRING, &_conf.ssl_key },
78 { "ssl_key_type", OPKG_OPT_TYPE_STRING, &_conf.ssl_key_type },
79 { "ssl_key_passwd", OPKG_OPT_TYPE_STRING, &_conf.ssl_key_passwd },
80 { "ssl_ca_file", OPKG_OPT_TYPE_STRING, &_conf.ssl_ca_file },
81 { "ssl_ca_path", OPKG_OPT_TYPE_STRING, &_conf.ssl_ca_path },
82 { "ssl_dont_verify_peer", OPKG_OPT_TYPE_BOOL, &_conf.ssl_dont_verify_peer },
88 resolve_pkg_dest_list(nv_pair_list_t *nv_pair_list)
90 nv_pair_list_elt_t *iter;
95 for (iter = nv_pair_list_first(nv_pair_list); iter;
96 iter = nv_pair_list_next(nv_pair_list, iter)) {
97 nv_pair = (nv_pair_t *)iter->data;
99 if (conf->offline_root) {
100 sprintf_alloc(&root_dir, "%s%s", conf->offline_root, nv_pair->value);
102 root_dir = xstrdup(nv_pair->value);
105 dest = pkg_dest_list_append(&conf->pkg_dest_list, nv_pair->name, root_dir, conf->lists_dir);
108 if (conf->default_dest == NULL)
109 conf->default_dest = dest;
111 if (conf->dest_str && !strcmp(dest->name, conf->dest_str)) {
112 conf->default_dest = dest;
113 conf->restrict_to_default_dest = 1;
117 if (conf->dest_str && !conf->restrict_to_default_dest) {
118 opkg_msg(ERROR, "Unknown dest name: `%s'.\n", conf->dest_str);
126 opkg_conf_set_option(const char *name, const char *value)
130 while (options[i].name) {
131 if (strcmp(options[i].name, name) == 0) {
132 switch (options[i].type) {
133 case OPKG_OPT_TYPE_BOOL:
134 if (*(int *)options[i].value) {
135 opkg_msg(ERROR, "Duplicate boolean option %s, "
136 "leaving this option on.\n", name);
139 *((int * const)options[i].value) = 1;
141 case OPKG_OPT_TYPE_INT:
143 if (*(int *)options[i].value) {
144 opkg_msg(ERROR, "Duplicate option %s, "
145 "using first seen value \"%d\".\n",
146 name, *((int *)options[i].value));
149 *((int * const)options[i].value) = atoi(value);
152 opkg_msg(ERROR, "Option %s needs an argument\n",
156 case OPKG_OPT_TYPE_STRING:
158 if (*(char **)options[i].value) {
159 opkg_msg(ERROR, "Duplicate option %s, "
160 "using first seen value \"%s\".\n",
161 name, *((char **)options[i].value));
164 *((char ** const)options[i].value) = xstrdup(value);
167 opkg_msg(ERROR, "Option %s needs an argument\n",
176 opkg_msg(ERROR, "Unrecognized option: %s=%s\n", name, value);
181 opkg_conf_parse_file(const char *filename,
182 pkg_src_list_t *pkg_src_list,
183 nv_pair_list_t *tmp_dest_nv_pair_list)
188 regex_t valid_line_re, comment_re;
189 #define regmatch_size 12
190 regmatch_t regmatch[regmatch_size];
192 file = fopen(filename, "r");
194 opkg_perror(ERROR, "Failed to open %s", filename);
199 opkg_msg(INFO, "Loading conf file %s.\n", filename);
201 err = xregcomp(&comment_re,
202 "^[[:space:]]*(#.*|[[:space:]]*)$",
207 err = xregcomp(&valid_line_re,
208 "^[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))"
209 "[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))"
210 "[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))"
211 "([[:space:]]+([^[:space:]]+))?[[:space:]]*$",
218 char *type, *name, *value, *extra;
222 line = file_read_line_alloc(file);
226 if (regexec(&comment_re, line, 0, 0, 0) == 0)
229 if (regexec(&valid_line_re, line, regmatch_size, regmatch, 0) == REG_NOMATCH) {
230 opkg_msg(ERROR, "%s:%d: Ignoring invalid line: `%s'\n",
231 filename, line_num, line);
235 /* This has to be so ugly to deal with optional quotation marks */
236 if (regmatch[2].rm_so > 0) {
237 type = xstrndup(line + regmatch[2].rm_so,
238 regmatch[2].rm_eo - regmatch[2].rm_so);
240 type = xstrndup(line + regmatch[3].rm_so,
241 regmatch[3].rm_eo - regmatch[3].rm_so);
244 if (regmatch[5].rm_so > 0) {
245 name = xstrndup(line + regmatch[5].rm_so,
246 regmatch[5].rm_eo - regmatch[5].rm_so);
248 name = xstrndup(line + regmatch[6].rm_so,
249 regmatch[6].rm_eo - regmatch[6].rm_so);
252 if (regmatch[8].rm_so > 0) {
253 value = xstrndup(line + regmatch[8].rm_so,
254 regmatch[8].rm_eo - regmatch[8].rm_so);
256 value = xstrndup(line + regmatch[9].rm_so,
257 regmatch[9].rm_eo - regmatch[9].rm_so);
261 if (regmatch[11].rm_so > 0) {
262 extra = xstrndup (line + regmatch[11].rm_so,
263 regmatch[11].rm_eo - regmatch[11].rm_so);
266 /* We use the tmp_dest_nv_pair_list below instead of
267 conf->pkg_dest_list because we might encounter an
268 offline_root option later and that would invalidate the
269 directories we would have computed in
270 pkg_dest_list_init. (We do a similar thing with
271 tmp_src_nv_pair_list for sake of symmetry.) */
272 if (strcmp(type, "option") == 0) {
273 opkg_conf_set_option(name, value);
274 } else if (strcmp(type, "src") == 0) {
275 if (!nv_pair_list_find((nv_pair_list_t*) pkg_src_list, name)) {
276 pkg_src_list_append (pkg_src_list, name, value, extra, 0);
278 opkg_msg(ERROR, "Duplicate src declaration (%s %s). "
279 "Skipping.\n", name, value);
281 } else if (strcmp(type, "src/gz") == 0) {
282 if (!nv_pair_list_find((nv_pair_list_t*) pkg_src_list, name)) {
283 pkg_src_list_append (pkg_src_list, name, value, extra, 1);
285 opkg_msg(ERROR, "Duplicate src declaration (%s %s). "
286 "Skipping.\n", name, value);
288 } else if (strcmp(type, "dest") == 0) {
289 nv_pair_list_append(tmp_dest_nv_pair_list, name, value);
290 } else if (strcmp(type, "lists_dir") == 0) {
291 conf->lists_dir = xstrdup(value);
292 } else if (strcmp(type, "arch") == 0) {
293 opkg_msg(INFO, "Supported arch %s priority (%s)\n", name, value);
295 opkg_msg(NOTICE, "No priority given for architecture %s,"
296 "defaulting to 10\n", name);
297 value = xstrdup("10");
299 nv_pair_list_append(&conf->arch_list, name, value);
301 opkg_msg(ERROR, "%s:%d: Ignoring invalid line: `%s'\n",
302 filename, line_num, line);
315 regfree(&valid_line_re);
317 regfree(&comment_re);
319 if (fclose(file) == EOF) {
320 opkg_perror(ERROR, "Couldn't close %s", filename);
328 opkg_conf_write_status_files(void)
330 pkg_dest_list_elt_t *iter;
339 list_for_each_entry(iter, &conf->pkg_dest_list.head, node) {
340 dest = (pkg_dest_t *)iter->data;
342 dest->status_fp = fopen(dest->status_file_name, "w");
343 if (dest->status_fp == NULL) {
344 opkg_perror(ERROR, "Can't open status file %s",
345 dest->status_file_name);
350 all = pkg_vec_alloc();
351 pkg_hash_fetch_available(all);
353 for(i = 0; i < all->len; i++) {
355 /* We don't need most uninstalled packages in the status file */
356 if (pkg->state_status == SS_NOT_INSTALLED
357 && (pkg->state_want == SW_UNKNOWN
358 || pkg->state_want == SW_DEINSTALL
359 || pkg->state_want == SW_PURGE)) {
362 if (pkg->dest == NULL) {
363 opkg_msg(ERROR, "Internal error: package %s has a NULL dest\n",
367 if (pkg->dest->status_fp)
368 pkg_print_status(pkg, pkg->dest->status_fp);
373 list_for_each_entry(iter, &conf->pkg_dest_list.head, node) {
374 dest = (pkg_dest_t *)iter->data;
375 if (fclose(dest->status_fp) == EOF) {
376 opkg_perror(ERROR, "Couldn't close %s", dest->status_file_name);
386 root_filename_alloc(char *filename)
389 sprintf_alloc(&root_filename, "%s%s",
390 (conf->offline_root ? conf->offline_root : ""), filename);
391 return root_filename;
395 glob_errfunc(const char *epath, int eerrno)
397 if (eerrno == ENOENT)
398 /* If leading dir does not exist, we get GLOB_NOMATCH. */
401 opkg_msg(ERROR, "glob failed for %s: %s\n", epath, strerror(eerrno));
409 char *tmp, *tmp_dir_base, **tmp_val;
410 nv_pair_list_t tmp_dest_nv_pair_list;
412 char *etc_opkg_conf_pattern;
414 conf->restrict_to_default_dest = 0;
415 conf->default_dest = NULL;
416 #if defined(HAVE_PATHFINDER)
417 conf->check_x509_path = 1;
420 pkg_src_list_init(&conf->pkg_src_list);
421 pkg_dest_list_init(&conf->pkg_dest_list);
422 nv_pair_list_init(&conf->arch_list);
423 nv_pair_list_init(&tmp_dest_nv_pair_list);
425 if (!conf->offline_root)
426 conf->offline_root = xstrdup(getenv("OFFLINE_ROOT"));
428 if (conf->conf_file) {
430 if (stat(conf->conf_file, &st) == -1) {
431 opkg_perror(ERROR, "Couldn't stat %s", conf->conf_file);
434 if (opkg_conf_parse_file(conf->conf_file,
435 &conf->pkg_src_list, &tmp_dest_nv_pair_list))
439 if (conf->offline_root)
440 sprintf_alloc(&etc_opkg_conf_pattern, "%s/etc/opkg/*.conf", conf->offline_root);
442 const char *conf_file_dir = getenv("OPKG_CONF_DIR");
443 if (conf_file_dir == NULL)
444 conf_file_dir = OPKG_CONF_DEFAULT_CONF_FILE_DIR;
445 sprintf_alloc(&etc_opkg_conf_pattern, "%s/*.conf", conf_file_dir);
448 memset(&globbuf, 0, sizeof(globbuf));
449 glob_ret = glob(etc_opkg_conf_pattern, 0, glob_errfunc, &globbuf);
450 if (glob_ret && glob_ret != GLOB_NOMATCH) {
451 free(etc_opkg_conf_pattern);
456 free(etc_opkg_conf_pattern);
458 for (i = 0; i < globbuf.gl_pathc; i++) {
459 if (globbuf.gl_pathv[i])
460 if (conf->conf_file &&
461 !strcmp(conf->conf_file, globbuf.gl_pathv[i]))
463 if ( opkg_conf_parse_file(globbuf.gl_pathv[i],
464 &conf->pkg_src_list, &tmp_dest_nv_pair_list)<0) {
472 if (conf->offline_root)
473 sprintf_alloc (&lock_file, "%s/%s/lock", conf->offline_root, OPKG_STATE_DIR_PREFIX);
475 sprintf_alloc (&lock_file, "%s/lock", OPKG_STATE_DIR_PREFIX);
477 lock_fd = creat(lock_file, S_IRUSR | S_IWUSR | S_IRGRP);
479 opkg_perror(ERROR, "Could not create lock file %s", lock_file);
483 if (lockf(lock_fd, F_TLOCK, (off_t)0) == -1) {
484 opkg_perror(ERROR, "Could not lock %s", lock_file);
489 tmp_dir_base = conf->tmp_dir;
491 tmp_dir_base = getenv("TMPDIR");
493 sprintf_alloc(&tmp, "%s/%s",
494 tmp_dir_base ? tmp_dir_base : OPKG_CONF_DEFAULT_TMP_DIR_BASE,
495 OPKG_CONF_TMP_DIR_SUFFIX);
498 conf->tmp_dir = mkdtemp(tmp);
499 if (conf->tmp_dir == NULL) {
500 opkg_perror(ERROR, "Creating temp dir %s failed", tmp);
505 hash_table_init("file-hash", &conf->file_hash, OPKG_CONF_DEFAULT_HASH_LEN);
506 hash_table_init("obs-file-hash", &conf->obs_file_hash, OPKG_CONF_DEFAULT_HASH_LEN/16);
508 if (conf->lists_dir == NULL)
509 conf->lists_dir = xstrdup(OPKG_CONF_LISTS_DIR);
511 if (conf->offline_root) {
512 sprintf_alloc(&tmp, "%s/%s", conf->offline_root, conf->lists_dir);
513 free(conf->lists_dir);
514 conf->lists_dir = tmp;
517 /* if no architectures were defined, then default all, noarch, and host architecture */
518 if (nv_pair_list_empty(&conf->arch_list)) {
519 nv_pair_list_append(&conf->arch_list, "all", "1");
520 nv_pair_list_append(&conf->arch_list, "noarch", "1");
521 nv_pair_list_append(&conf->arch_list, HOST_CPU_STR, "10");
524 /* Even if there is no conf file, we'll need at least one dest. */
525 if (nv_pair_list_empty(&tmp_dest_nv_pair_list)) {
526 nv_pair_list_append(&tmp_dest_nv_pair_list,
527 OPKG_CONF_DEFAULT_DEST_NAME,
528 OPKG_CONF_DEFAULT_DEST_ROOT_DIR);
531 if (resolve_pkg_dest_list(&tmp_dest_nv_pair_list))
534 nv_pair_list_deinit(&tmp_dest_nv_pair_list);
540 free(conf->lists_dir);
543 hash_table_deinit(&conf->file_hash);
544 hash_table_deinit(&conf->obs_file_hash);
546 if (rmdir(conf->tmp_dir) == -1)
547 opkg_perror(ERROR, "Couldn't remove dir %s", conf->tmp_dir);
549 if (lockf(lock_fd, F_ULOCK, (off_t)0) == -1)
550 opkg_perror(ERROR, "Couldn't unlock %s", lock_file);
552 if (close(lock_fd) == -1)
553 opkg_perror(ERROR, "Couldn't close descriptor %d (%s)",
555 if (unlink(lock_file) == -1)
556 opkg_perror(ERROR, "Couldn't unlink %s", lock_file);
560 pkg_src_list_deinit(&conf->pkg_src_list);
561 pkg_dest_list_deinit(&conf->pkg_dest_list);
562 nv_pair_list_deinit(&conf->arch_list);
564 for (i=0; options[i].name; i++) {
565 if (options[i].type == OPKG_OPT_TYPE_STRING) {
566 tmp_val = (char **)options[i].value;
574 nv_pair_list_deinit(&tmp_dest_nv_pair_list);
576 free(conf->dest_str);
578 free(conf->conf_file);
584 opkg_conf_deinit(void)
591 free(conf->lists_dir);
594 free(conf->dest_str);
597 free(conf->conf_file);
599 pkg_src_list_deinit(&conf->pkg_src_list);
600 pkg_dest_list_deinit(&conf->pkg_dest_list);
601 nv_pair_list_deinit(&conf->arch_list);
603 for (i=0; options[i].name; i++) {
604 if (options[i].type == OPKG_OPT_TYPE_STRING) {
605 tmp = (char **)options[i].value;
613 if (conf->verbosity >= DEBUG) {
614 hash_print_stats(&conf->pkg_hash);
615 hash_print_stats(&conf->file_hash);
616 hash_print_stats(&conf->obs_file_hash);
620 hash_table_deinit(&conf->file_hash);
621 hash_table_deinit(&conf->obs_file_hash);
623 if (lockf(lock_fd, F_ULOCK, (off_t)0) == -1)
624 opkg_perror(ERROR, "Couldn't unlock %s", lock_file);
626 if (close(lock_fd) == -1)
627 opkg_perror(ERROR, "Couldn't close descriptor %d (%s)",
630 if (unlink(lock_file) == -1)
631 opkg_perror(ERROR, "Couldn't unlink %s", lock_file);