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.
22 #include <sys/types.h>
28 #include "opkg_conf.h"
32 #include "sprintf_alloc.h"
33 #include "opkg_message.h"
34 #include "file_util.h"
35 #include "opkg_defines.h"
36 #include "libbb/libbb.h"
39 static char *lock_file
= NULL
;
41 static opkg_conf_t _conf
;
42 opkg_conf_t
*conf
= &_conf
;
47 opkg_option_t options
[] = {
48 { "cache", OPKG_OPT_TYPE_STRING
, &_conf
.cache
},
49 { "force_defaults", OPKG_OPT_TYPE_BOOL
, &_conf
.force_defaults
},
50 { "force_maintainer", OPKG_OPT_TYPE_BOOL
, &_conf
.force_maintainer
},
51 { "force_depends", OPKG_OPT_TYPE_BOOL
, &_conf
.force_depends
},
52 { "force_overwrite", OPKG_OPT_TYPE_BOOL
, &_conf
.force_overwrite
},
53 { "force_downgrade", OPKG_OPT_TYPE_BOOL
, &_conf
.force_downgrade
},
54 { "force_reinstall", OPKG_OPT_TYPE_BOOL
, &_conf
.force_reinstall
},
55 { "force_space", OPKG_OPT_TYPE_BOOL
, &_conf
.force_space
},
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 { "nodeps", OPKG_OPT_TYPE_BOOL
, &_conf
.nodeps
},
63 { "offline_root", OPKG_OPT_TYPE_STRING
, &_conf
.offline_root
},
64 { "proxy_passwd", OPKG_OPT_TYPE_STRING
, &_conf
.proxy_passwd
},
65 { "proxy_user", OPKG_OPT_TYPE_STRING
, &_conf
.proxy_user
},
66 { "query-all", OPKG_OPT_TYPE_BOOL
, &_conf
.query_all
},
67 { "tmp_dir", OPKG_OPT_TYPE_STRING
, &_conf
.tmp_dir
},
68 { "verbosity", OPKG_OPT_TYPE_INT
, &_conf
.verbosity
},
69 #if defined(HAVE_OPENSSL)
70 { "signature_ca_file", OPKG_OPT_TYPE_STRING
, &_conf
.signature_ca_file
},
71 { "signature_ca_path", OPKG_OPT_TYPE_STRING
, &_conf
.signature_ca_path
},
73 #if defined(HAVE_PATHFINDER)
74 { "check_x509_path", OPKG_OPT_TYPE_BOOL
, &_conf
.check_x509_path
},
76 #if defined(HAVE_SSLCURL) && defined(HAVE_CURL)
77 { "ssl_engine", OPKG_OPT_TYPE_STRING
, &_conf
.ssl_engine
},
78 { "ssl_cert", OPKG_OPT_TYPE_STRING
, &_conf
.ssl_cert
},
79 { "ssl_cert_type", OPKG_OPT_TYPE_STRING
, &_conf
.ssl_cert_type
},
80 { "ssl_key", OPKG_OPT_TYPE_STRING
, &_conf
.ssl_key
},
81 { "ssl_key_type", OPKG_OPT_TYPE_STRING
, &_conf
.ssl_key_type
},
82 { "ssl_key_passwd", OPKG_OPT_TYPE_STRING
, &_conf
.ssl_key_passwd
},
83 { "ssl_ca_file", OPKG_OPT_TYPE_STRING
, &_conf
.ssl_ca_file
},
84 { "ssl_ca_path", OPKG_OPT_TYPE_STRING
, &_conf
.ssl_ca_path
},
85 { "ssl_dont_verify_peer", OPKG_OPT_TYPE_BOOL
, &_conf
.ssl_dont_verify_peer
},
91 resolve_pkg_dest_list(nv_pair_list_t
*nv_pair_list
)
93 nv_pair_list_elt_t
*iter
;
98 for (iter
= nv_pair_list_first(nv_pair_list
); iter
;
99 iter
= nv_pair_list_next(nv_pair_list
, iter
)) {
100 nv_pair
= (nv_pair_t
*)iter
->data
;
102 if (conf
->offline_root
) {
103 sprintf_alloc(&root_dir
, "%s%s", conf
->offline_root
, nv_pair
->value
);
105 root_dir
= xstrdup(nv_pair
->value
);
108 dest
= pkg_dest_list_append(&conf
->pkg_dest_list
, nv_pair
->name
, root_dir
, conf
->lists_dir
);
111 if (conf
->default_dest
== NULL
)
112 conf
->default_dest
= dest
;
114 if (conf
->dest_str
&& !strcmp(dest
->name
, conf
->dest_str
)) {
115 conf
->default_dest
= dest
;
116 conf
->restrict_to_default_dest
= 1;
120 if (conf
->dest_str
&& !conf
->restrict_to_default_dest
) {
121 opkg_msg(ERROR
, "Unknown dest name: `%s'.\n", conf
->dest_str
);
129 opkg_conf_set_option(const char *name
, const char *value
)
133 while (options
[i
].name
) {
134 if (strcmp(options
[i
].name
, name
) == 0) {
135 switch (options
[i
].type
) {
136 case OPKG_OPT_TYPE_BOOL
:
137 if (*(int *)options
[i
].value
) {
138 opkg_msg(ERROR
, "Duplicate boolean option %s, "
139 "leaving this option on.\n", name
);
142 *((int * const)options
[i
].value
) = 1;
144 case OPKG_OPT_TYPE_INT
:
146 if (*(int *)options
[i
].value
) {
147 opkg_msg(ERROR
, "Duplicate option %s, "
148 "using first seen value \"%d\".\n",
149 name
, *((int *)options
[i
].value
));
152 *((int * const)options
[i
].value
) = atoi(value
);
155 opkg_msg(ERROR
, "Option %s needs an argument\n",
159 case OPKG_OPT_TYPE_STRING
:
161 if (*(char **)options
[i
].value
) {
162 opkg_msg(ERROR
, "Duplicate option %s, "
163 "using first seen value \"%s\".\n",
164 name
, *((char **)options
[i
].value
));
167 *((char ** const)options
[i
].value
) = xstrdup(value
);
170 opkg_msg(ERROR
, "Option %s needs an argument\n",
179 opkg_msg(ERROR
, "Unrecognized option: %s=%s\n", name
, value
);
184 opkg_conf_parse_file(const char *filename
,
185 pkg_src_list_t
*pkg_src_list
,
186 nv_pair_list_t
*tmp_dest_nv_pair_list
)
191 regex_t valid_line_re
, comment_re
;
192 #define regmatch_size 12
193 regmatch_t regmatch
[regmatch_size
];
195 file
= fopen(filename
, "r");
197 opkg_perror(ERROR
, "Failed to open %s", filename
);
202 opkg_msg(INFO
, "Loading conf file %s.\n", filename
);
204 err
= xregcomp(&comment_re
,
205 "^[[:space:]]*(#.*|[[:space:]]*)$",
210 err
= xregcomp(&valid_line_re
,
211 "^[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))"
212 "[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))"
213 "[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))"
214 "([[:space:]]+([^[:space:]]+))?[[:space:]]*$",
221 char *type
, *name
, *value
, *extra
;
225 line
= file_read_line_alloc(file
);
229 if (regexec(&comment_re
, line
, 0, 0, 0) == 0)
232 if (regexec(&valid_line_re
, line
, regmatch_size
, regmatch
, 0) == REG_NOMATCH
) {
233 opkg_msg(ERROR
, "%s:%d: Ignoring invalid line: `%s'\n",
234 filename
, line_num
, line
);
238 /* This has to be so ugly to deal with optional quotation marks */
239 if (regmatch
[2].rm_so
> 0) {
240 type
= xstrndup(line
+ regmatch
[2].rm_so
,
241 regmatch
[2].rm_eo
- regmatch
[2].rm_so
);
243 type
= xstrndup(line
+ regmatch
[3].rm_so
,
244 regmatch
[3].rm_eo
- regmatch
[3].rm_so
);
247 if (regmatch
[5].rm_so
> 0) {
248 name
= xstrndup(line
+ regmatch
[5].rm_so
,
249 regmatch
[5].rm_eo
- regmatch
[5].rm_so
);
251 name
= xstrndup(line
+ regmatch
[6].rm_so
,
252 regmatch
[6].rm_eo
- regmatch
[6].rm_so
);
255 if (regmatch
[8].rm_so
> 0) {
256 value
= xstrndup(line
+ regmatch
[8].rm_so
,
257 regmatch
[8].rm_eo
- regmatch
[8].rm_so
);
259 value
= xstrndup(line
+ regmatch
[9].rm_so
,
260 regmatch
[9].rm_eo
- regmatch
[9].rm_so
);
264 if (regmatch
[11].rm_so
> 0) {
265 extra
= xstrndup (line
+ regmatch
[11].rm_so
,
266 regmatch
[11].rm_eo
- regmatch
[11].rm_so
);
269 /* We use the tmp_dest_nv_pair_list below instead of
270 conf->pkg_dest_list because we might encounter an
271 offline_root option later and that would invalidate the
272 directories we would have computed in
273 pkg_dest_list_init. (We do a similar thing with
274 tmp_src_nv_pair_list for sake of symmetry.) */
275 if (strcmp(type
, "option") == 0) {
276 opkg_conf_set_option(name
, value
);
277 } else if (strcmp(type
, "src") == 0) {
278 if (!nv_pair_list_find((nv_pair_list_t
*) pkg_src_list
, name
)) {
279 pkg_src_list_append (pkg_src_list
, name
, value
, extra
, 0);
281 opkg_msg(ERROR
, "Duplicate src declaration (%s %s). "
282 "Skipping.\n", name
, value
);
284 } else if (strcmp(type
, "src/gz") == 0) {
285 if (!nv_pair_list_find((nv_pair_list_t
*) pkg_src_list
, name
)) {
286 pkg_src_list_append (pkg_src_list
, name
, value
, extra
, 1);
288 opkg_msg(ERROR
, "Duplicate src declaration (%s %s). "
289 "Skipping.\n", name
, value
);
291 } else if (strcmp(type
, "dest") == 0) {
292 nv_pair_list_append(tmp_dest_nv_pair_list
, name
, value
);
293 } else if (strcmp(type
, "lists_dir") == 0) {
294 conf
->lists_dir
= xstrdup(value
);
295 } else if (strcmp(type
, "arch") == 0) {
296 opkg_msg(INFO
, "Supported arch %s priority (%s)\n", name
, value
);
298 opkg_msg(NOTICE
, "No priority given for architecture %s,"
299 "defaulting to 10\n", name
);
300 value
= xstrdup("10");
302 nv_pair_list_append(&conf
->arch_list
, name
, value
);
304 opkg_msg(ERROR
, "%s:%d: Ignoring invalid line: `%s'\n",
305 filename
, line_num
, line
);
318 regfree(&valid_line_re
);
320 regfree(&comment_re
);
322 if (fclose(file
) == EOF
) {
323 opkg_perror(ERROR
, "Couldn't close %s", filename
);
331 opkg_conf_write_status_files(void)
333 pkg_dest_list_elt_t
*iter
;
342 list_for_each_entry(iter
, &conf
->pkg_dest_list
.head
, node
) {
343 dest
= (pkg_dest_t
*)iter
->data
;
345 dest
->status_fp
= fopen(dest
->status_file_name
, "w");
346 if (dest
->status_fp
== NULL
) {
347 opkg_perror(ERROR
, "Can't open status file %s",
348 dest
->status_file_name
);
353 all
= pkg_vec_alloc();
354 pkg_hash_fetch_available(all
);
356 for(i
= 0; i
< all
->len
; i
++) {
358 /* We don't need most uninstalled packages in the status file */
359 if (pkg
->state_status
== SS_NOT_INSTALLED
360 && (pkg
->state_want
== SW_UNKNOWN
361 || pkg
->state_want
== SW_DEINSTALL
362 || pkg
->state_want
== SW_PURGE
)) {
365 if (pkg
->dest
== NULL
) {
366 opkg_msg(ERROR
, "Internal error: package %s has a NULL dest\n",
370 if (pkg
->dest
->status_fp
)
371 pkg_print_status(pkg
, pkg
->dest
->status_fp
);
376 list_for_each_entry(iter
, &conf
->pkg_dest_list
.head
, node
) {
377 dest
= (pkg_dest_t
*)iter
->data
;
378 if (fclose(dest
->status_fp
) == EOF
) {
379 opkg_perror(ERROR
, "Couldn't close %s", dest
->status_file_name
);
389 root_filename_alloc(char *filename
)
392 sprintf_alloc(&root_filename
, "%s%s",
393 (conf
->offline_root
? conf
->offline_root
: ""), filename
);
394 return root_filename
;
398 glob_errfunc(const char *epath
, int eerrno
)
400 if (eerrno
== ENOENT
)
401 /* If leading dir does not exist, we get GLOB_NOMATCH. */
404 opkg_msg(ERROR
, "glob failed for %s: %s\n", epath
, strerror(eerrno
));
412 char *tmp
, *tmp_dir_base
, **tmp_val
;
413 nv_pair_list_t tmp_dest_nv_pair_list
;
415 char *etc_opkg_conf_pattern
;
417 conf
->restrict_to_default_dest
= 0;
418 conf
->default_dest
= NULL
;
419 #if defined(HAVE_PATHFINDER)
420 conf
->check_x509_path
= 1;
423 pkg_src_list_init(&conf
->pkg_src_list
);
424 pkg_dest_list_init(&conf
->pkg_dest_list
);
425 nv_pair_list_init(&conf
->arch_list
);
426 nv_pair_list_init(&tmp_dest_nv_pair_list
);
428 if (!conf
->offline_root
)
429 conf
->offline_root
= xstrdup(getenv("OFFLINE_ROOT"));
431 if (conf
->conf_file
) {
433 if (stat(conf
->conf_file
, &st
) == -1) {
434 opkg_perror(ERROR
, "Couldn't stat %s", conf
->conf_file
);
437 if (opkg_conf_parse_file(conf
->conf_file
,
438 &conf
->pkg_src_list
, &tmp_dest_nv_pair_list
))
442 if (conf
->offline_root
)
443 sprintf_alloc(&etc_opkg_conf_pattern
, "%s/etc/opkg/*.conf", conf
->offline_root
);
445 const char *conf_file_dir
= getenv("OPKG_CONF_DIR");
446 if (conf_file_dir
== NULL
)
447 conf_file_dir
= OPKG_CONF_DEFAULT_CONF_FILE_DIR
;
448 sprintf_alloc(&etc_opkg_conf_pattern
, "%s/*.conf", conf_file_dir
);
451 memset(&globbuf
, 0, sizeof(globbuf
));
452 glob_ret
= glob(etc_opkg_conf_pattern
, 0, glob_errfunc
, &globbuf
);
453 if (glob_ret
&& glob_ret
!= GLOB_NOMATCH
) {
454 free(etc_opkg_conf_pattern
);
459 free(etc_opkg_conf_pattern
);
461 for (i
= 0; i
< globbuf
.gl_pathc
; i
++) {
462 if (globbuf
.gl_pathv
[i
])
463 if (conf
->conf_file
&&
464 !strcmp(conf
->conf_file
, globbuf
.gl_pathv
[i
]))
466 if ( opkg_conf_parse_file(globbuf
.gl_pathv
[i
],
467 &conf
->pkg_src_list
, &tmp_dest_nv_pair_list
)<0) {
475 if (conf
->offline_root
)
476 sprintf_alloc (&lock_file
, "%s/%s/lock", conf
->offline_root
, OPKG_STATE_DIR_PREFIX
);
478 sprintf_alloc (&lock_file
, "%s/lock", OPKG_STATE_DIR_PREFIX
);
480 lock_fd
= creat(lock_file
, S_IRUSR
| S_IWUSR
| S_IRGRP
);
482 opkg_perror(ERROR
, "Could not create lock file %s", lock_file
);
486 if (lockf(lock_fd
, F_TLOCK
, (off_t
)0) == -1) {
487 opkg_perror(ERROR
, "Could not lock %s", lock_file
);
492 tmp_dir_base
= conf
->tmp_dir
;
494 tmp_dir_base
= getenv("TMPDIR");
496 sprintf_alloc(&tmp
, "%s/%s",
497 tmp_dir_base
? tmp_dir_base
: OPKG_CONF_DEFAULT_TMP_DIR_BASE
,
498 OPKG_CONF_TMP_DIR_SUFFIX
);
501 conf
->tmp_dir
= mkdtemp(tmp
);
502 if (conf
->tmp_dir
== NULL
) {
503 opkg_perror(ERROR
, "Creating temp dir %s failed", tmp
);
508 hash_table_init("file-hash", &conf
->file_hash
, OPKG_CONF_DEFAULT_HASH_LEN
);
509 hash_table_init("obs-file-hash", &conf
->obs_file_hash
, OPKG_CONF_DEFAULT_HASH_LEN
/16);
511 if (conf
->lists_dir
== NULL
)
512 conf
->lists_dir
= xstrdup(OPKG_CONF_LISTS_DIR
);
514 if (conf
->offline_root
) {
515 sprintf_alloc(&tmp
, "%s/%s", conf
->offline_root
, conf
->lists_dir
);
516 free(conf
->lists_dir
);
517 conf
->lists_dir
= tmp
;
520 /* if no architectures were defined, then default all, noarch, and host architecture */
521 if (nv_pair_list_empty(&conf
->arch_list
)) {
522 nv_pair_list_append(&conf
->arch_list
, "all", "1");
523 nv_pair_list_append(&conf
->arch_list
, "noarch", "1");
524 nv_pair_list_append(&conf
->arch_list
, HOST_CPU_STR
, "10");
527 /* Even if there is no conf file, we'll need at least one dest. */
528 if (nv_pair_list_empty(&tmp_dest_nv_pair_list
)) {
529 nv_pair_list_append(&tmp_dest_nv_pair_list
,
530 OPKG_CONF_DEFAULT_DEST_NAME
,
531 OPKG_CONF_DEFAULT_DEST_ROOT_DIR
);
534 if (resolve_pkg_dest_list(&tmp_dest_nv_pair_list
))
537 nv_pair_list_deinit(&tmp_dest_nv_pair_list
);
543 free(conf
->lists_dir
);
546 hash_table_deinit(&conf
->file_hash
);
547 hash_table_deinit(&conf
->obs_file_hash
);
549 if (rmdir(conf
->tmp_dir
) == -1)
550 opkg_perror(ERROR
, "Couldn't remove dir %s", conf
->tmp_dir
);
552 if (lockf(lock_fd
, F_ULOCK
, (off_t
)0) == -1)
553 opkg_perror(ERROR
, "Couldn't unlock %s", lock_file
);
555 if (close(lock_fd
) == -1)
556 opkg_perror(ERROR
, "Couldn't close descriptor %d (%s)",
558 if (unlink(lock_file
) == -1)
559 opkg_perror(ERROR
, "Couldn't unlink %s", lock_file
);
563 pkg_src_list_deinit(&conf
->pkg_src_list
);
564 pkg_dest_list_deinit(&conf
->pkg_dest_list
);
565 nv_pair_list_deinit(&conf
->arch_list
);
567 for (i
=0; options
[i
].name
; i
++) {
568 if (options
[i
].type
== OPKG_OPT_TYPE_STRING
) {
569 tmp_val
= (char **)options
[i
].value
;
577 nv_pair_list_deinit(&tmp_dest_nv_pair_list
);
579 free(conf
->dest_str
);
581 free(conf
->conf_file
);
587 opkg_conf_deinit(void)
594 free(conf
->lists_dir
);
597 free(conf
->dest_str
);
600 free(conf
->conf_file
);
602 pkg_src_list_deinit(&conf
->pkg_src_list
);
603 pkg_dest_list_deinit(&conf
->pkg_dest_list
);
604 nv_pair_list_deinit(&conf
->arch_list
);
606 for (i
=0; options
[i
].name
; i
++) {
607 if (options
[i
].type
== OPKG_OPT_TYPE_STRING
) {
608 tmp
= (char **)options
[i
].value
;
616 if (conf
->verbosity
>= DEBUG
) {
617 hash_print_stats(&conf
->pkg_hash
);
618 hash_print_stats(&conf
->file_hash
);
619 hash_print_stats(&conf
->obs_file_hash
);
623 hash_table_deinit(&conf
->file_hash
);
624 hash_table_deinit(&conf
->obs_file_hash
);
626 if (lockf(lock_fd
, F_ULOCK
, (off_t
)0) == -1)
627 opkg_perror(ERROR
, "Couldn't unlock %s", lock_file
);
629 if (close(lock_fd
) == -1)
630 opkg_perror(ERROR
, "Couldn't close descriptor %d (%s)",
633 if (unlink(lock_file
) == -1)
634 opkg_perror(ERROR
, "Couldn't unlink %s", lock_file
);