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>
35 #include <sys/utsname.h>
37 #include <sys/sysctl.h>
50 #define roundup2(x, y) (((x)+((y)-1))&(~((y)-1))) /* if y is powers of two */
54 STAILQ_ENTRY(config_value) next;
62 STAILQ_HEAD(, config_value) *list;
64 bool main_only; /* Only set in pkg.conf. */
67 static struct config_entry c[] = {
71 URL_SCHEME_PREFIX "http://pkg.FreeBSD.org/${ABI}/latest",
95 [ASSUME_ALWAYS_YES] = {
143 pkg_get_myabi(char *dest, size_t sz)
146 char machine_arch[255];
154 len = sizeof(machine_arch);
155 error = sysctlbyname("hw.machine_arch", machine_arch, &len, NULL, 0);
158 machine_arch[len] = '\0';
161 * Use __FreeBSD_version rather than kernel version (uts.release) for
162 * use in jails. This is equivalent to the value of uname -U.
164 snprintf(dest, sz, "%s:%d:%s", uts.sysname, __FreeBSD_version/100000,
171 subst_packagesite(const char *abi)
174 const char *variable_string;
177 if (c[PACKAGESITE].value != NULL)
178 oldval = c[PACKAGESITE].value;
180 oldval = c[PACKAGESITE].val;
182 if ((variable_string = strstr(oldval, "${ABI}")) == NULL)
185 asprintf(&newval, "%.*s%s%s",
186 (int)(variable_string - oldval), oldval, abi,
187 variable_string + strlen("${ABI}"));
189 errx(EXIT_FAILURE, "asprintf");
191 free(c[PACKAGESITE].value);
192 c[PACKAGESITE].value = newval;
196 boolstr_to_bool(const char *str)
198 if (str != NULL && (strcasecmp(str, "true") == 0 ||
199 strcasecmp(str, "yes") == 0 || strcasecmp(str, "on") == 0 ||
207 config_parse(const ucl_object_t *obj, pkg_conf_file_t conftype)
209 struct sbuf *buf = sbuf_new_auto();
210 const ucl_object_t *cur, *seq;
211 ucl_object_iter_t it = NULL, itseq = NULL;
212 struct config_entry *temp_config;
213 struct config_value *cv;
218 /* Temporary config for configs that may be disabled. */
219 temp_config = calloc(CONFIG_SIZE, sizeof(struct config_entry));
221 while ((cur = ucl_iterate_object(obj, &it, true))) {
222 key = ucl_object_key(cur);
227 if (conftype == CONFFILE_PKG) {
228 for (j = 0; j < strlen(key); ++j)
229 sbuf_putc(buf, key[j]);
231 } else if (conftype == CONFFILE_REPO) {
232 if (strcasecmp(key, "url") == 0)
233 sbuf_cpy(buf, "PACKAGESITE");
234 else if (strcasecmp(key, "mirror_type") == 0)
235 sbuf_cpy(buf, "MIRROR_TYPE");
236 else if (strcasecmp(key, "signature_type") == 0)
237 sbuf_cpy(buf, "SIGNATURE_TYPE");
238 else if (strcasecmp(key, "fingerprints") == 0)
239 sbuf_cpy(buf, "FINGERPRINTS");
240 else if (strcasecmp(key, "pubkey") == 0)
241 sbuf_cpy(buf, "PUBKEY");
242 else if (strcasecmp(key, "enabled") == 0) {
243 if ((cur->type != UCL_BOOLEAN) ||
244 !ucl_object_toboolean(cur))
251 for (i = 0; i < CONFIG_SIZE; i++) {
252 if (strcmp(sbuf_data(buf), c[i].key) == 0)
256 /* Silently skip unknown keys to be future compatible. */
257 if (i == CONFIG_SIZE)
260 /* env has priority over config file */
264 /* Parse sequence value ["item1", "item2"] */
266 case PKG_CONFIG_LIST:
267 if (cur->type != UCL_ARRAY) {
268 warnx("Skipping invalid array "
269 "value for %s.\n", c[i].key);
272 temp_config[i].list =
273 malloc(sizeof(*temp_config[i].list));
274 STAILQ_INIT(temp_config[i].list);
276 while ((seq = ucl_iterate_object(cur, &itseq, true))) {
277 if (seq->type != UCL_STRING)
279 cv = malloc(sizeof(struct config_value));
281 strdup(ucl_object_tostring(seq));
282 STAILQ_INSERT_TAIL(temp_config[i].list, cv,
286 case PKG_CONFIG_BOOL:
287 temp_config[i].value =
288 strdup(ucl_object_toboolean(cur) ? "yes" : "no");
291 /* Normal string value. */
292 temp_config[i].value = strdup(ucl_object_tostring(cur));
297 /* Repo is enabled, copy over all settings from temp_config. */
298 for (i = 0; i < CONFIG_SIZE; i++) {
301 /* Prevent overriding ABI, ASSUME_ALWAYS_YES, etc. */
302 if (conftype != CONFFILE_PKG && c[i].main_only == true)
305 case PKG_CONFIG_LIST:
306 c[i].list = temp_config[i].list;
309 c[i].value = temp_config[i].value;
320 * Parse new repo style configs in style:
327 parse_repo_file(ucl_object_t *obj)
329 ucl_object_iter_t it = NULL;
330 const ucl_object_t *cur;
333 while ((cur = ucl_iterate_object(obj, &it, true))) {
334 key = ucl_object_key(cur);
339 if (cur->type != UCL_OBJECT)
342 config_parse(cur, CONFFILE_REPO);
348 read_conf_file(const char *confpath, pkg_conf_file_t conftype)
350 struct ucl_parser *p;
351 ucl_object_t *obj = NULL;
353 p = ucl_parser_new(0);
355 if (!ucl_parser_add_file(p, confpath)) {
357 errx(EXIT_FAILURE, "Unable to parse configuration "
358 "file %s: %s", confpath, ucl_parser_get_error(p));
360 /* no configuration present */
364 obj = ucl_parser_get_object(p);
365 if (obj->type != UCL_OBJECT)
366 warnx("Invalid configuration format, ignoring the "
367 "configuration file %s", confpath);
369 if (conftype == CONFFILE_PKG)
370 config_parse(obj, conftype);
371 else if (conftype == CONFFILE_REPO)
372 parse_repo_file(obj);
375 ucl_object_unref(obj);
382 load_repositories(const char *repodir)
388 char path[MAXPATHLEN];
393 if ((d = opendir(repodir)) == NULL)
396 while ((ent = readdir(d))) {
397 /* Trim out 'repos'. */
398 if ((n = strlen(ent->d_name)) <= 5)
400 p = &ent->d_name[n - 5];
401 if (strcmp(p, ".conf") == 0) {
402 snprintf(path, sizeof(path), "%s%s%s",
404 repodir[strlen(repodir) - 1] == '/' ? "" : "/",
406 if (access(path, F_OK) == 0 &&
407 read_conf_file(path, CONFFILE_REPO)) {
425 const char *localbase;
427 char confpath[MAXPATHLEN];
428 struct config_value *cv;
431 for (i = 0; i < CONFIG_SIZE; i++) {
432 val = getenv(c[i].key);
436 case PKG_CONFIG_LIST:
437 /* Split up comma-separated items from env. */
438 c[i].list = malloc(sizeof(*c[i].list));
439 STAILQ_INIT(c[i].list);
440 for (env_list_item = strtok(val, ",");
441 env_list_item != NULL;
442 env_list_item = strtok(NULL, ",")) {
444 malloc(sizeof(struct config_value));
446 strdup(env_list_item);
447 STAILQ_INSERT_TAIL(c[i].list, cv,
458 /* Read LOCALBASE/etc/pkg.conf first. */
459 localbase = getlocalbase();
460 snprintf(confpath, sizeof(confpath), "%s/etc/pkg.conf", localbase);
462 if (access(confpath, F_OK) == 0 && read_conf_file(confpath,
466 /* Then read in all repos from REPOS_DIR list of directories. */
467 if (c[REPOS_DIR].list == NULL) {
468 c[REPOS_DIR].list = malloc(sizeof(*c[REPOS_DIR].list));
469 STAILQ_INIT(c[REPOS_DIR].list);
470 cv = malloc(sizeof(struct config_value));
471 cv->value = strdup("/etc/pkg");
472 STAILQ_INSERT_TAIL(c[REPOS_DIR].list, cv, next);
473 cv = malloc(sizeof(struct config_value));
474 if (asprintf(&cv->value, "%s/etc/pkg/repos", localbase) < 0)
476 STAILQ_INSERT_TAIL(c[REPOS_DIR].list, cv, next);
479 STAILQ_FOREACH(cv, c[REPOS_DIR].list, next)
480 if (load_repositories(cv->value))
484 if (c[ABI].val == NULL && c[ABI].value == NULL) {
485 if (pkg_get_myabi(abi, BUFSIZ) != 0)
486 errx(EXIT_FAILURE, "Failed to determine the system "
491 subst_packagesite(c[ABI].value != NULL ? c[ABI].value : c[ABI].val);
497 config_string(pkg_config_key k, const char **val)
499 if (c[k].type != PKG_CONFIG_STRING)
502 if (c[k].value != NULL)
511 config_bool(pkg_config_key k, bool *val)
515 if (c[k].type != PKG_CONFIG_BOOL)
520 if (c[k].value != NULL)
525 if (boolstr_to_bool(value))
532 config_finish(void) {
535 for (i = 0; i < CONFIG_SIZE; i++)