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 { "force_postinstall", OPKG_OPT_TYPE_BOOL
, &_conf
.force_postinstall
},
57 { "check_signature", OPKG_OPT_TYPE_BOOL
, &_conf
.check_signature
},
58 { "ftp_proxy", OPKG_OPT_TYPE_STRING
, &_conf
.ftp_proxy
},
59 { "http_proxy", OPKG_OPT_TYPE_STRING
, &_conf
.http_proxy
},
60 { "no_proxy", OPKG_OPT_TYPE_STRING
, &_conf
.no_proxy
},
61 { "test", OPKG_OPT_TYPE_BOOL
, &_conf
.noaction
},
62 { "noaction", OPKG_OPT_TYPE_BOOL
, &_conf
.noaction
},
63 { "download_only", OPKG_OPT_TYPE_BOOL
, &_conf
.download_only
},
64 { "nodeps", OPKG_OPT_TYPE_BOOL
, &_conf
.nodeps
},
65 { "offline_root", OPKG_OPT_TYPE_STRING
, &_conf
.offline_root
},
66 { "proxy_passwd", OPKG_OPT_TYPE_STRING
, &_conf
.proxy_passwd
},
67 { "proxy_user", OPKG_OPT_TYPE_STRING
, &_conf
.proxy_user
},
68 { "query-all", OPKG_OPT_TYPE_BOOL
, &_conf
.query_all
},
69 { "tmp_dir", OPKG_OPT_TYPE_STRING
, &_conf
.tmp_dir
},
70 { "verbosity", OPKG_OPT_TYPE_INT
, &_conf
.verbosity
},
71 #if defined(HAVE_OPENSSL)
72 { "signature_ca_file", OPKG_OPT_TYPE_STRING
, &_conf
.signature_ca_file
},
73 { "signature_ca_path", OPKG_OPT_TYPE_STRING
, &_conf
.signature_ca_path
},
75 #if defined(HAVE_PATHFINDER)
76 { "check_x509_path", OPKG_OPT_TYPE_BOOL
, &_conf
.check_x509_path
},
78 #if defined(HAVE_SSLCURL) && defined(HAVE_CURL)
79 { "ssl_engine", OPKG_OPT_TYPE_STRING
, &_conf
.ssl_engine
},
80 { "ssl_cert", OPKG_OPT_TYPE_STRING
, &_conf
.ssl_cert
},
81 { "ssl_cert_type", OPKG_OPT_TYPE_STRING
, &_conf
.ssl_cert_type
},
82 { "ssl_key", OPKG_OPT_TYPE_STRING
, &_conf
.ssl_key
},
83 { "ssl_key_type", OPKG_OPT_TYPE_STRING
, &_conf
.ssl_key_type
},
84 { "ssl_key_passwd", OPKG_OPT_TYPE_STRING
, &_conf
.ssl_key_passwd
},
85 { "ssl_ca_file", OPKG_OPT_TYPE_STRING
, &_conf
.ssl_ca_file
},
86 { "ssl_ca_path", OPKG_OPT_TYPE_STRING
, &_conf
.ssl_ca_path
},
87 { "ssl_dont_verify_peer", OPKG_OPT_TYPE_BOOL
, &_conf
.ssl_dont_verify_peer
},
93 resolve_pkg_dest_list(nv_pair_list_t
*nv_pair_list
)
95 nv_pair_list_elt_t
*iter
;
100 for (iter
= nv_pair_list_first(nv_pair_list
); iter
;
101 iter
= nv_pair_list_next(nv_pair_list
, iter
)) {
102 nv_pair
= (nv_pair_t
*)iter
->data
;
104 if (conf
->offline_root
) {
105 sprintf_alloc(&root_dir
, "%s%s", conf
->offline_root
, nv_pair
->value
);
107 root_dir
= xstrdup(nv_pair
->value
);
110 dest
= pkg_dest_list_append(&conf
->pkg_dest_list
, nv_pair
->name
, root_dir
, conf
->lists_dir
);
113 if (conf
->default_dest
== NULL
)
114 conf
->default_dest
= dest
;
116 if (conf
->dest_str
&& !strcmp(dest
->name
, conf
->dest_str
)) {
117 conf
->default_dest
= dest
;
118 conf
->restrict_to_default_dest
= 1;
122 if (conf
->dest_str
&& !conf
->restrict_to_default_dest
) {
123 opkg_msg(ERROR
, "Unknown dest name: `%s'.\n", conf
->dest_str
);
131 opkg_conf_set_option(const char *name
, const char *value
)
135 while (options
[i
].name
) {
136 if (strcmp(options
[i
].name
, name
) == 0) {
137 switch (options
[i
].type
) {
138 case OPKG_OPT_TYPE_BOOL
:
139 if (*(int *)options
[i
].value
) {
140 opkg_msg(ERROR
, "Duplicate boolean option %s, "
141 "leaving this option on.\n", name
);
144 *((int * const)options
[i
].value
) = 1;
146 case OPKG_OPT_TYPE_INT
:
148 if (*(int *)options
[i
].value
) {
149 opkg_msg(ERROR
, "Duplicate option %s, "
150 "using first seen value \"%d\".\n",
151 name
, *((int *)options
[i
].value
));
154 *((int * const)options
[i
].value
) = atoi(value
);
157 opkg_msg(ERROR
, "Option %s needs an argument\n",
161 case OPKG_OPT_TYPE_STRING
:
163 if (*(char **)options
[i
].value
) {
164 opkg_msg(ERROR
, "Duplicate option %s, "
165 "using first seen value \"%s\".\n",
166 name
, *((char **)options
[i
].value
));
169 *((char ** const)options
[i
].value
) = xstrdup(value
);
172 opkg_msg(ERROR
, "Option %s needs an argument\n",
181 opkg_msg(ERROR
, "Unrecognized option: %s=%s\n", name
, value
);
186 opkg_conf_parse_file(const char *filename
,
187 pkg_src_list_t
*pkg_src_list
,
188 nv_pair_list_t
*tmp_dest_nv_pair_list
)
193 regex_t valid_line_re
, comment_re
;
194 #define regmatch_size 12
195 regmatch_t regmatch
[regmatch_size
];
197 file
= fopen(filename
, "r");
199 opkg_perror(ERROR
, "Failed to open %s", filename
);
204 opkg_msg(INFO
, "Loading conf file %s.\n", filename
);
206 err
= xregcomp(&comment_re
,
207 "^[[:space:]]*(#.*|[[:space:]]*)$",
212 err
= xregcomp(&valid_line_re
,
213 "^[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))"
214 "[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))"
215 "[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))"
216 "([[:space:]]+([^[:space:]]+))?[[:space:]]*$",
223 char *type
, *name
, *value
, *extra
;
227 line
= file_read_line_alloc(file
);
231 if (regexec(&comment_re
, line
, 0, 0, 0) == 0)
234 if (regexec(&valid_line_re
, line
, regmatch_size
, regmatch
, 0) == REG_NOMATCH
) {
235 opkg_msg(ERROR
, "%s:%d: Ignoring invalid line: `%s'\n",
236 filename
, line_num
, line
);
240 /* This has to be so ugly to deal with optional quotation marks */
241 if (regmatch
[2].rm_so
> 0) {
242 type
= xstrndup(line
+ regmatch
[2].rm_so
,
243 regmatch
[2].rm_eo
- regmatch
[2].rm_so
);
245 type
= xstrndup(line
+ regmatch
[3].rm_so
,
246 regmatch
[3].rm_eo
- regmatch
[3].rm_so
);
249 if (regmatch
[5].rm_so
> 0) {
250 name
= xstrndup(line
+ regmatch
[5].rm_so
,
251 regmatch
[5].rm_eo
- regmatch
[5].rm_so
);
253 name
= xstrndup(line
+ regmatch
[6].rm_so
,
254 regmatch
[6].rm_eo
- regmatch
[6].rm_so
);
257 if (regmatch
[8].rm_so
> 0) {
258 value
= xstrndup(line
+ regmatch
[8].rm_so
,
259 regmatch
[8].rm_eo
- regmatch
[8].rm_so
);
261 value
= xstrndup(line
+ regmatch
[9].rm_so
,
262 regmatch
[9].rm_eo
- regmatch
[9].rm_so
);
266 if (regmatch
[11].rm_so
> 0) {
267 extra
= xstrndup (line
+ regmatch
[11].rm_so
,
268 regmatch
[11].rm_eo
- regmatch
[11].rm_so
);
271 /* We use the tmp_dest_nv_pair_list below instead of
272 conf->pkg_dest_list because we might encounter an
273 offline_root option later and that would invalidate the
274 directories we would have computed in
275 pkg_dest_list_init. (We do a similar thing with
276 tmp_src_nv_pair_list for sake of symmetry.) */
277 if (strcmp(type
, "option") == 0) {
278 opkg_conf_set_option(name
, value
);
279 } else if (strcmp(type
, "src") == 0) {
280 if (!nv_pair_list_find((nv_pair_list_t
*) pkg_src_list
, name
)) {
281 pkg_src_list_append (pkg_src_list
, name
, value
, extra
, 0);
283 opkg_msg(ERROR
, "Duplicate src declaration (%s %s). "
284 "Skipping.\n", name
, value
);
286 } else if (strcmp(type
, "src/gz") == 0) {
287 if (!nv_pair_list_find((nv_pair_list_t
*) pkg_src_list
, name
)) {
288 pkg_src_list_append (pkg_src_list
, name
, value
, extra
, 1);
290 opkg_msg(ERROR
, "Duplicate src declaration (%s %s). "
291 "Skipping.\n", name
, value
);
293 } else if (strcmp(type
, "dest") == 0) {
294 nv_pair_list_append(tmp_dest_nv_pair_list
, name
, value
);
295 } else if (strcmp(type
, "lists_dir") == 0) {
296 conf
->lists_dir
= xstrdup(value
);
297 } else if (strcmp(type
, "arch") == 0) {
298 opkg_msg(INFO
, "Supported arch %s priority (%s)\n", name
, value
);
300 opkg_msg(NOTICE
, "No priority given for architecture %s,"
301 "defaulting to 10\n", name
);
302 value
= xstrdup("10");
304 nv_pair_list_append(&conf
->arch_list
, name
, value
);
306 opkg_msg(ERROR
, "%s:%d: Ignoring invalid line: `%s'\n",
307 filename
, line_num
, line
);
320 regfree(&valid_line_re
);
322 regfree(&comment_re
);
324 if (fclose(file
) == EOF
) {
325 opkg_perror(ERROR
, "Couldn't close %s", filename
);
333 opkg_conf_write_status_files(void)
335 pkg_dest_list_elt_t
*iter
;
344 list_for_each_entry(iter
, &conf
->pkg_dest_list
.head
, node
) {
345 dest
= (pkg_dest_t
*)iter
->data
;
347 dest
->status_fp
= fopen(dest
->status_file_name
, "w");
348 if (dest
->status_fp
== NULL
&& errno
!= EROFS
) {
349 opkg_perror(ERROR
, "Can't open status file %s",
350 dest
->status_file_name
);
355 all
= pkg_vec_alloc();
356 pkg_hash_fetch_available(all
);
358 for(i
= 0; i
< all
->len
; i
++) {
360 /* We don't need most uninstalled packages in the status file */
361 if (pkg
->state_status
== SS_NOT_INSTALLED
362 && (pkg
->state_want
== SW_UNKNOWN
363 || (pkg
->state_want
== SW_DEINSTALL
364 && pkg
->state_flag
!= SF_HOLD
)
365 || pkg
->state_want
== SW_PURGE
)) {
368 if (pkg
->dest
== NULL
) {
369 opkg_msg(ERROR
, "Internal error: package %s has a NULL dest\n",
373 if (pkg
->dest
->status_fp
)
374 pkg_print_status(pkg
, pkg
->dest
->status_fp
);
379 list_for_each_entry(iter
, &conf
->pkg_dest_list
.head
, node
) {
380 dest
= (pkg_dest_t
*)iter
->data
;
381 if (dest
->status_fp
&& fclose(dest
->status_fp
) == EOF
) {
382 opkg_perror(ERROR
, "Couldn't close %s", dest
->status_file_name
);
392 root_filename_alloc(char *filename
)
395 sprintf_alloc(&root_filename
, "%s%s",
396 (conf
->offline_root
? conf
->offline_root
: ""), filename
);
397 return root_filename
;
401 glob_errfunc(const char *epath
, int eerrno
)
403 if (eerrno
== ENOENT
)
404 /* If leading dir does not exist, we get GLOB_NOMATCH. */
407 opkg_msg(ERROR
, "glob failed for %s: %s\n", epath
, strerror(eerrno
));
415 char *tmp
, *tmp_dir_base
, **tmp_val
;
416 nv_pair_list_t tmp_dest_nv_pair_list
;
418 char *etc_opkg_conf_pattern
;
420 conf
->restrict_to_default_dest
= 0;
421 conf
->default_dest
= NULL
;
422 #if defined(HAVE_PATHFINDER)
423 conf
->check_x509_path
= 1;
426 pkg_src_list_init(&conf
->pkg_src_list
);
427 pkg_dest_list_init(&conf
->pkg_dest_list
);
428 nv_pair_list_init(&conf
->arch_list
);
429 nv_pair_list_init(&tmp_dest_nv_pair_list
);
431 if (!conf
->offline_root
)
432 conf
->offline_root
= xstrdup(getenv("OFFLINE_ROOT"));
434 if (conf
->conf_file
) {
436 if (stat(conf
->conf_file
, &st
) == -1) {
437 opkg_perror(ERROR
, "Couldn't stat %s", conf
->conf_file
);
440 if (opkg_conf_parse_file(conf
->conf_file
,
441 &conf
->pkg_src_list
, &tmp_dest_nv_pair_list
))
445 if (conf
->offline_root
)
446 sprintf_alloc(&etc_opkg_conf_pattern
, "%s/etc/opkg/*.conf", conf
->offline_root
);
448 const char *conf_file_dir
= getenv("OPKG_CONF_DIR");
449 if (conf_file_dir
== NULL
)
450 conf_file_dir
= OPKG_CONF_DEFAULT_CONF_FILE_DIR
;
451 sprintf_alloc(&etc_opkg_conf_pattern
, "%s/*.conf", conf_file_dir
);
454 memset(&globbuf
, 0, sizeof(globbuf
));
455 glob_ret
= glob(etc_opkg_conf_pattern
, 0, glob_errfunc
, &globbuf
);
456 if (glob_ret
&& glob_ret
!= GLOB_NOMATCH
) {
457 free(etc_opkg_conf_pattern
);
462 free(etc_opkg_conf_pattern
);
464 for (i
= 0; i
< globbuf
.gl_pathc
; i
++) {
465 if (globbuf
.gl_pathv
[i
])
466 if (conf
->conf_file
&&
467 !strcmp(conf
->conf_file
, globbuf
.gl_pathv
[i
]))
469 if ( opkg_conf_parse_file(globbuf
.gl_pathv
[i
],
470 &conf
->pkg_src_list
, &tmp_dest_nv_pair_list
)<0) {
478 if (conf
->offline_root
)
479 sprintf_alloc (&lock_file
, "%s/%s", conf
->offline_root
, OPKGLOCKFILE
);
481 sprintf_alloc (&lock_file
, "%s", OPKGLOCKFILE
);
483 lock_fd
= creat(lock_file
, S_IRUSR
| S_IWUSR
| S_IRGRP
);
485 opkg_perror(ERROR
, "Could not create lock file %s", lock_file
);
489 if (lockf(lock_fd
, F_TLOCK
, (off_t
)0) == -1) {
490 opkg_perror(ERROR
, "Could not lock %s", lock_file
);
491 if (close(lock_fd
) == -1)
492 opkg_perror(ERROR
, "Couldn't close descriptor %d (%s)",
498 tmp_dir_base
= conf
->tmp_dir
;
500 tmp_dir_base
= getenv("TMPDIR");
502 sprintf_alloc(&tmp
, "%s/%s",
503 tmp_dir_base
? tmp_dir_base
: OPKG_CONF_DEFAULT_TMP_DIR_BASE
,
504 OPKG_CONF_TMP_DIR_SUFFIX
);
507 conf
->tmp_dir
= mkdtemp(tmp
);
508 if (conf
->tmp_dir
== NULL
) {
509 opkg_perror(ERROR
, "Creating temp dir %s failed", tmp
);
514 hash_table_init("file-hash", &conf
->file_hash
, OPKG_CONF_DEFAULT_HASH_LEN
);
515 hash_table_init("obs-file-hash", &conf
->obs_file_hash
, OPKG_CONF_DEFAULT_HASH_LEN
/16);
517 if (conf
->lists_dir
== NULL
)
518 conf
->lists_dir
= xstrdup(OPKG_CONF_LISTS_DIR
);
520 if (conf
->offline_root
) {
521 sprintf_alloc(&tmp
, "%s/%s", conf
->offline_root
, conf
->lists_dir
);
522 free(conf
->lists_dir
);
523 conf
->lists_dir
= tmp
;
526 /* if no architectures were defined, then default all, noarch, and host architecture */
527 if (nv_pair_list_empty(&conf
->arch_list
)) {
528 nv_pair_list_append(&conf
->arch_list
, "all", "1");
529 nv_pair_list_append(&conf
->arch_list
, "noarch", "1");
530 nv_pair_list_append(&conf
->arch_list
, HOST_CPU_STR
, "10");
533 /* Even if there is no conf file, we'll need at least one dest. */
534 if (nv_pair_list_empty(&tmp_dest_nv_pair_list
)) {
535 nv_pair_list_append(&tmp_dest_nv_pair_list
,
536 OPKG_CONF_DEFAULT_DEST_NAME
,
537 OPKG_CONF_DEFAULT_DEST_ROOT_DIR
);
540 if (resolve_pkg_dest_list(&tmp_dest_nv_pair_list
))
543 nv_pair_list_deinit(&tmp_dest_nv_pair_list
);
549 free(conf
->lists_dir
);
552 hash_table_deinit(&conf
->file_hash
);
553 hash_table_deinit(&conf
->obs_file_hash
);
555 if (rmdir(conf
->tmp_dir
) == -1)
556 opkg_perror(ERROR
, "Couldn't remove dir %s", conf
->tmp_dir
);
558 if (lockf(lock_fd
, F_ULOCK
, (off_t
)0) == -1)
559 opkg_perror(ERROR
, "Couldn't unlock %s", lock_file
);
561 if (close(lock_fd
) == -1)
562 opkg_perror(ERROR
, "Couldn't close descriptor %d (%s)",
564 if (unlink(lock_file
) == -1)
565 opkg_perror(ERROR
, "Couldn't unlink %s", lock_file
);
569 pkg_src_list_deinit(&conf
->pkg_src_list
);
570 pkg_dest_list_deinit(&conf
->pkg_dest_list
);
571 nv_pair_list_deinit(&conf
->arch_list
);
573 for (i
=0; options
[i
].name
; i
++) {
574 if (options
[i
].type
== OPKG_OPT_TYPE_STRING
) {
575 tmp_val
= (char **)options
[i
].value
;
583 nv_pair_list_deinit(&tmp_dest_nv_pair_list
);
585 free(conf
->dest_str
);
587 free(conf
->conf_file
);
593 opkg_conf_deinit(void)
600 free(conf
->lists_dir
);
603 free(conf
->dest_str
);
606 free(conf
->conf_file
);
608 pkg_src_list_deinit(&conf
->pkg_src_list
);
609 pkg_dest_list_deinit(&conf
->pkg_dest_list
);
610 nv_pair_list_deinit(&conf
->arch_list
);
612 for (i
=0; options
[i
].name
; i
++) {
613 if (options
[i
].type
== OPKG_OPT_TYPE_STRING
) {
614 tmp
= (char **)options
[i
].value
;
622 if (conf
->verbosity
>= DEBUG
) {
623 hash_print_stats(&conf
->pkg_hash
);
624 hash_print_stats(&conf
->file_hash
);
625 hash_print_stats(&conf
->obs_file_hash
);
629 hash_table_deinit(&conf
->file_hash
);
630 hash_table_deinit(&conf
->obs_file_hash
);
632 if (lockf(lock_fd
, F_ULOCK
, (off_t
)0) == -1)
633 opkg_perror(ERROR
, "Couldn't unlock %s", lock_file
);
635 if (close(lock_fd
) == -1)
636 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
);