2 * Copyright (c) 2014 Baptiste Daroussin <bapt@FreeBSD.org>
3 * Copyright (c) 2013 Bryan Drewery <bdrewery@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/queue.h>
34 #include <sys/types.h>
35 #include <sys/utsname.h>
36 #include <sys/sysctl.h>
53 #define roundup2(x, y) (((x)+((y)-1))&(~((y)-1))) /* if y is powers of two */
57 STAILQ_ENTRY(config_value) next;
65 STAILQ_HEAD(, config_value) *list;
67 bool main_only; /* Only set in pkg.conf. */
70 static struct config_entry c[] = {
74 URL_SCHEME_PREFIX "http://pkg.FreeBSD.org/${ABI}/latest",
98 [ASSUME_ALWAYS_YES] = {
137 pkg_get_myabi(char *dest, size_t sz)
140 char machine_arch[255];
148 len = sizeof(machine_arch);
149 error = sysctlbyname("hw.machine_arch", machine_arch, &len, NULL, 0);
152 machine_arch[len] = '\0';
155 * Use __FreeBSD_version rather than kernel version (uts.release) for
156 * use in jails. This is equivalent to the value of uname -U.
158 snprintf(dest, sz, "%s:%d:%s", uts.sysname, __FreeBSD_version/100000,
165 subst_packagesite(const char *abi)
168 const char *variable_string;
171 if (c[PACKAGESITE].value != NULL)
172 oldval = c[PACKAGESITE].value;
174 oldval = c[PACKAGESITE].val;
176 if ((variable_string = strstr(oldval, "${ABI}")) == NULL)
179 newval = sbuf_new_auto();
180 sbuf_bcat(newval, oldval, variable_string - oldval);
181 sbuf_cat(newval, abi);
182 sbuf_cat(newval, variable_string + strlen("${ABI}"));
185 free(c[PACKAGESITE].value);
186 c[PACKAGESITE].value = strdup(sbuf_data(newval));
190 boolstr_to_bool(const char *str)
192 if (str != NULL && (strcasecmp(str, "true") == 0 ||
193 strcasecmp(str, "yes") == 0 || strcasecmp(str, "on") == 0 ||
201 config_parse(const ucl_object_t *obj, pkg_conf_file_t conftype)
203 struct sbuf *buf = sbuf_new_auto();
204 const ucl_object_t *cur, *seq;
205 ucl_object_iter_t it = NULL, itseq = NULL;
206 struct config_entry *temp_config;
207 struct config_value *cv;
212 /* Temporary config for configs that may be disabled. */
213 temp_config = calloc(CONFIG_SIZE, sizeof(struct config_entry));
215 while ((cur = ucl_iterate_object(obj, &it, true))) {
216 key = ucl_object_key(cur);
221 if (conftype == CONFFILE_PKG) {
222 for (j = 0; j < strlen(key); ++j)
223 sbuf_putc(buf, key[j]);
225 } else if (conftype == CONFFILE_REPO) {
226 if (strcasecmp(key, "url") == 0)
227 sbuf_cpy(buf, "PACKAGESITE");
228 else if (strcasecmp(key, "mirror_type") == 0)
229 sbuf_cpy(buf, "MIRROR_TYPE");
230 else if (strcasecmp(key, "signature_type") == 0)
231 sbuf_cpy(buf, "SIGNATURE_TYPE");
232 else if (strcasecmp(key, "fingerprints") == 0)
233 sbuf_cpy(buf, "FINGERPRINTS");
234 else if (strcasecmp(key, "enabled") == 0) {
235 if ((cur->type != UCL_BOOLEAN) ||
236 !ucl_object_toboolean(cur))
243 for (i = 0; i < CONFIG_SIZE; i++) {
244 if (strcmp(sbuf_data(buf), c[i].key) == 0)
248 /* Silently skip unknown keys to be future compatible. */
249 if (i == CONFIG_SIZE)
252 /* env has priority over config file */
256 /* Parse sequence value ["item1", "item2"] */
258 case PKG_CONFIG_LIST:
259 if (cur->type != UCL_ARRAY) {
260 warnx("Skipping invalid array "
261 "value for %s.\n", c[i].key);
264 temp_config[i].list =
265 malloc(sizeof(*temp_config[i].list));
266 STAILQ_INIT(temp_config[i].list);
268 while ((seq = ucl_iterate_object(cur, &itseq, true))) {
269 if (seq->type != UCL_STRING)
271 cv = malloc(sizeof(struct config_value));
273 strdup(ucl_object_tostring(seq));
274 STAILQ_INSERT_TAIL(temp_config[i].list, cv,
278 case PKG_CONFIG_BOOL:
279 temp_config[i].value =
280 strdup(ucl_object_toboolean(cur) ? "yes" : "no");
283 /* Normal string value. */
284 temp_config[i].value = strdup(ucl_object_tostring(cur));
289 /* Repo is enabled, copy over all settings from temp_config. */
290 for (i = 0; i < CONFIG_SIZE; i++) {
293 /* Prevent overriding ABI, ASSUME_ALWAYS_YES, etc. */
294 if (conftype != CONFFILE_PKG && c[i].main_only == true)
297 case PKG_CONFIG_LIST:
298 c[i].list = temp_config[i].list;
301 c[i].value = temp_config[i].value;
312 * Parse new repo style configs in style:
319 parse_repo_file(ucl_object_t *obj)
321 ucl_object_iter_t it = NULL;
322 const ucl_object_t *cur;
325 while ((cur = ucl_iterate_object(obj, &it, true))) {
326 key = ucl_object_key(cur);
331 if (cur->type != UCL_OBJECT)
334 config_parse(cur, CONFFILE_REPO);
340 read_conf_file(const char *confpath, pkg_conf_file_t conftype)
342 struct ucl_parser *p;
343 ucl_object_t *obj = NULL;
345 p = ucl_parser_new(0);
347 if (!ucl_parser_add_file(p, confpath)) {
349 errx(EXIT_FAILURE, "Unable to parse configuration "
350 "file %s: %s", confpath, ucl_parser_get_error(p));
352 /* no configuration present */
356 obj = ucl_parser_get_object(p);
357 if (obj->type != UCL_OBJECT)
358 warnx("Invalid configuration format, ignoring the "
359 "configuration file %s", confpath);
361 if (conftype == CONFFILE_PKG)
362 config_parse(obj, conftype);
363 else if (conftype == CONFFILE_REPO)
364 parse_repo_file(obj);
367 ucl_object_unref(obj);
374 load_repositories(const char *repodir)
380 char path[MAXPATHLEN];
385 if ((d = opendir(repodir)) == NULL)
388 while ((ent = readdir(d))) {
389 /* Trim out 'repos'. */
390 if ((n = strlen(ent->d_name)) <= 5)
392 p = &ent->d_name[n - 5];
393 if (strcmp(p, ".conf") == 0) {
394 snprintf(path, sizeof(path), "%s%s%s",
396 repodir[strlen(repodir) - 1] == '/' ? "" : "/",
398 if (access(path, F_OK) == 0 &&
399 read_conf_file(path, CONFFILE_REPO)) {
417 const char *localbase;
419 char confpath[MAXPATHLEN];
420 struct config_value *cv;
423 for (i = 0; i < CONFIG_SIZE; i++) {
424 val = getenv(c[i].key);
428 case PKG_CONFIG_LIST:
429 /* Split up comma-separated items from env. */
430 c[i].list = malloc(sizeof(*c[i].list));
431 STAILQ_INIT(c[i].list);
432 for (env_list_item = strtok(val, ",");
433 env_list_item != NULL;
434 env_list_item = strtok(NULL, ",")) {
436 malloc(sizeof(struct config_value));
438 strdup(env_list_item);
439 STAILQ_INSERT_TAIL(c[i].list, cv,
450 /* Read LOCALBASE/etc/pkg.conf first. */
451 localbase = getenv("LOCALBASE") ? getenv("LOCALBASE") : _LOCALBASE;
452 snprintf(confpath, sizeof(confpath), "%s/etc/pkg.conf",
455 if (access(confpath, F_OK) == 0 && read_conf_file(confpath,
459 /* Then read in all repos from REPOS_DIR list of directories. */
460 if (c[REPOS_DIR].list == NULL) {
461 c[REPOS_DIR].list = malloc(sizeof(*c[REPOS_DIR].list));
462 STAILQ_INIT(c[REPOS_DIR].list);
463 cv = malloc(sizeof(struct config_value));
464 cv->value = strdup("/etc/pkg");
465 STAILQ_INSERT_TAIL(c[REPOS_DIR].list, cv, next);
466 cv = malloc(sizeof(struct config_value));
467 if (asprintf(&cv->value, "%s/etc/pkg/repos", localbase) < 0)
469 STAILQ_INSERT_TAIL(c[REPOS_DIR].list, cv, next);
472 STAILQ_FOREACH(cv, c[REPOS_DIR].list, next)
473 if (load_repositories(cv->value))
477 if (c[ABI].val == NULL && c[ABI].value == NULL) {
478 if (pkg_get_myabi(abi, BUFSIZ) != 0)
479 errx(EXIT_FAILURE, "Failed to determine the system "
484 subst_packagesite(c[ABI].value != NULL ? c[ABI].value : c[ABI].val);
490 config_string(pkg_config_key k, const char **val)
492 if (c[k].type != PKG_CONFIG_STRING)
495 if (c[k].value != NULL)
504 config_bool(pkg_config_key k, bool *val)
508 if (c[k].type != PKG_CONFIG_BOOL)
513 if (c[k].value != NULL)
518 if (boolstr_to_bool(value))
525 config_finish(void) {
528 for (i = 0; i < CONFIG_SIZE; i++)