2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2014 Baptiste Daroussin <bapt@FreeBSD.org>
5 * Copyright (c) 2013 Bryan Drewery <bdrewery@FreeBSD.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/queue.h>
36 #include <sys/utsname.h>
37 #include <sys/sysctl.h>
48 #define roundup2(x, y) (((x)+((y)-1))&(~((y)-1))) /* if y is powers of two */
52 STAILQ_ENTRY(config_value) next;
60 STAILQ_HEAD(, config_value) *list;
62 bool main_only; /* Only set in pkg.conf. */
65 static struct config_entry c[] = {
69 URL_SCHEME_PREFIX "http://pkg.FreeBSD.org/${ABI}/latest",
93 [ASSUME_ALWAYS_YES] = {
141 pkg_get_myabi(char *dest, size_t sz)
144 char machine_arch[255];
152 len = sizeof(machine_arch);
153 error = sysctlbyname("hw.machine_arch", machine_arch, &len, NULL, 0);
156 machine_arch[len] = '\0';
159 * Use __FreeBSD_version rather than kernel version (uts.release) for
160 * use in jails. This is equivalent to the value of uname -U.
162 snprintf(dest, sz, "%s:%d:%s", uts.sysname, __FreeBSD_version/100000,
169 subst_packagesite(const char *abi)
172 const char *variable_string;
175 if (c[PACKAGESITE].value != NULL)
176 oldval = c[PACKAGESITE].value;
178 oldval = c[PACKAGESITE].val;
180 if ((variable_string = strstr(oldval, "${ABI}")) == NULL)
183 newval = sbuf_new_auto();
184 sbuf_bcat(newval, oldval, variable_string - oldval);
185 sbuf_cat(newval, abi);
186 sbuf_cat(newval, variable_string + strlen("${ABI}"));
189 free(c[PACKAGESITE].value);
190 c[PACKAGESITE].value = strdup(sbuf_data(newval));
194 boolstr_to_bool(const char *str)
196 if (str != NULL && (strcasecmp(str, "true") == 0 ||
197 strcasecmp(str, "yes") == 0 || strcasecmp(str, "on") == 0 ||
205 config_parse(const ucl_object_t *obj, pkg_conf_file_t conftype)
207 struct sbuf *buf = sbuf_new_auto();
208 const ucl_object_t *cur, *seq;
209 ucl_object_iter_t it = NULL, itseq = NULL;
210 struct config_entry *temp_config;
211 struct config_value *cv;
216 /* Temporary config for configs that may be disabled. */
217 temp_config = calloc(CONFIG_SIZE, sizeof(struct config_entry));
219 while ((cur = ucl_iterate_object(obj, &it, true))) {
220 key = ucl_object_key(cur);
225 if (conftype == CONFFILE_PKG) {
226 for (j = 0; j < strlen(key); ++j)
227 sbuf_putc(buf, key[j]);
229 } else if (conftype == CONFFILE_REPO) {
230 if (strcasecmp(key, "url") == 0)
231 sbuf_cpy(buf, "PACKAGESITE");
232 else if (strcasecmp(key, "mirror_type") == 0)
233 sbuf_cpy(buf, "MIRROR_TYPE");
234 else if (strcasecmp(key, "signature_type") == 0)
235 sbuf_cpy(buf, "SIGNATURE_TYPE");
236 else if (strcasecmp(key, "fingerprints") == 0)
237 sbuf_cpy(buf, "FINGERPRINTS");
238 else if (strcasecmp(key, "pubkey") == 0)
239 sbuf_cpy(buf, "PUBKEY");
240 else if (strcasecmp(key, "enabled") == 0) {
241 if ((cur->type != UCL_BOOLEAN) ||
242 !ucl_object_toboolean(cur))
249 for (i = 0; i < CONFIG_SIZE; i++) {
250 if (strcmp(sbuf_data(buf), c[i].key) == 0)
254 /* Silently skip unknown keys to be future compatible. */
255 if (i == CONFIG_SIZE)
258 /* env has priority over config file */
262 /* Parse sequence value ["item1", "item2"] */
264 case PKG_CONFIG_LIST:
265 if (cur->type != UCL_ARRAY) {
266 warnx("Skipping invalid array "
267 "value for %s.\n", c[i].key);
270 temp_config[i].list =
271 malloc(sizeof(*temp_config[i].list));
272 STAILQ_INIT(temp_config[i].list);
274 while ((seq = ucl_iterate_object(cur, &itseq, true))) {
275 if (seq->type != UCL_STRING)
277 cv = malloc(sizeof(struct config_value));
279 strdup(ucl_object_tostring(seq));
280 STAILQ_INSERT_TAIL(temp_config[i].list, cv,
284 case PKG_CONFIG_BOOL:
285 temp_config[i].value =
286 strdup(ucl_object_toboolean(cur) ? "yes" : "no");
289 /* Normal string value. */
290 temp_config[i].value = strdup(ucl_object_tostring(cur));
295 /* Repo is enabled, copy over all settings from temp_config. */
296 for (i = 0; i < CONFIG_SIZE; i++) {
299 /* Prevent overriding ABI, ASSUME_ALWAYS_YES, etc. */
300 if (conftype != CONFFILE_PKG && c[i].main_only == true)
303 case PKG_CONFIG_LIST:
304 c[i].list = temp_config[i].list;
307 c[i].value = temp_config[i].value;
318 * Parse new repo style configs in style:
325 parse_repo_file(ucl_object_t *obj)
327 ucl_object_iter_t it = NULL;
328 const ucl_object_t *cur;
331 while ((cur = ucl_iterate_object(obj, &it, true))) {
332 key = ucl_object_key(cur);
337 if (cur->type != UCL_OBJECT)
340 config_parse(cur, CONFFILE_REPO);
346 read_conf_file(const char *confpath, pkg_conf_file_t conftype)
348 struct ucl_parser *p;
349 ucl_object_t *obj = NULL;
351 p = ucl_parser_new(0);
353 if (!ucl_parser_add_file(p, confpath)) {
355 errx(EXIT_FAILURE, "Unable to parse configuration "
356 "file %s: %s", confpath, ucl_parser_get_error(p));
358 /* no configuration present */
362 obj = ucl_parser_get_object(p);
363 if (obj->type != UCL_OBJECT)
364 warnx("Invalid configuration format, ignoring the "
365 "configuration file %s", confpath);
367 if (conftype == CONFFILE_PKG)
368 config_parse(obj, conftype);
369 else if (conftype == CONFFILE_REPO)
370 parse_repo_file(obj);
373 ucl_object_unref(obj);
380 load_repositories(const char *repodir)
386 char path[MAXPATHLEN];
391 if ((d = opendir(repodir)) == NULL)
394 while ((ent = readdir(d))) {
395 /* Trim out 'repos'. */
396 if ((n = strlen(ent->d_name)) <= 5)
398 p = &ent->d_name[n - 5];
399 if (strcmp(p, ".conf") == 0) {
400 snprintf(path, sizeof(path), "%s%s%s",
402 repodir[strlen(repodir) - 1] == '/' ? "" : "/",
404 if (access(path, F_OK) == 0 &&
405 read_conf_file(path, CONFFILE_REPO)) {
423 const char *localbase;
425 char confpath[MAXPATHLEN];
426 struct config_value *cv;
429 for (i = 0; i < CONFIG_SIZE; i++) {
430 val = getenv(c[i].key);
434 case PKG_CONFIG_LIST:
435 /* Split up comma-separated items from env. */
436 c[i].list = malloc(sizeof(*c[i].list));
437 STAILQ_INIT(c[i].list);
438 for (env_list_item = strtok(val, ",");
439 env_list_item != NULL;
440 env_list_item = strtok(NULL, ",")) {
442 malloc(sizeof(struct config_value));
444 strdup(env_list_item);
445 STAILQ_INSERT_TAIL(c[i].list, cv,
456 /* Read LOCALBASE/etc/pkg.conf first. */
457 localbase = getenv("LOCALBASE") ? getenv("LOCALBASE") : _LOCALBASE;
458 snprintf(confpath, sizeof(confpath), "%s/etc/pkg.conf",
461 if (access(confpath, F_OK) == 0 && read_conf_file(confpath,
465 /* Then read in all repos from REPOS_DIR list of directories. */
466 if (c[REPOS_DIR].list == NULL) {
467 c[REPOS_DIR].list = malloc(sizeof(*c[REPOS_DIR].list));
468 STAILQ_INIT(c[REPOS_DIR].list);
469 cv = malloc(sizeof(struct config_value));
470 cv->value = strdup("/etc/pkg");
471 STAILQ_INSERT_TAIL(c[REPOS_DIR].list, cv, next);
472 cv = malloc(sizeof(struct config_value));
473 if (asprintf(&cv->value, "%s/etc/pkg/repos", localbase) < 0)
475 STAILQ_INSERT_TAIL(c[REPOS_DIR].list, cv, next);
478 STAILQ_FOREACH(cv, c[REPOS_DIR].list, next)
479 if (load_repositories(cv->value))
483 if (c[ABI].val == NULL && c[ABI].value == NULL) {
484 if (pkg_get_myabi(abi, BUFSIZ) != 0)
485 errx(EXIT_FAILURE, "Failed to determine the system "
490 subst_packagesite(c[ABI].value != NULL ? c[ABI].value : c[ABI].val);
496 config_string(pkg_config_key k, const char **val)
498 if (c[k].type != PKG_CONFIG_STRING)
501 if (c[k].value != NULL)
510 config_bool(pkg_config_key k, bool *val)
514 if (c[k].type != PKG_CONFIG_BOOL)
519 if (c[k].value != NULL)
524 if (boolstr_to_bool(value))
531 config_finish(void) {
534 for (i = 0; i < CONFIG_SIZE; i++)