2020-10-05 15:52:51 +00:00
|
|
|
/* context.c - Alpine Package Keeper (APK)
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005-2008 Natanael Copa <n@tanael.org>
|
|
|
|
* Copyright (C) 2008-2020 Timo Teräs <timo.teras@iki.fi>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
*/
|
|
|
|
|
2020-10-09 09:44:32 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/stat.h>
|
2020-10-05 15:52:51 +00:00
|
|
|
#include "apk_context.h"
|
|
|
|
|
|
|
|
void apk_ctx_init(struct apk_ctx *ac)
|
|
|
|
{
|
|
|
|
memset(ac, 0, sizeof *ac);
|
|
|
|
apk_string_array_init(&ac->repository_list);
|
2021-08-23 12:17:11 +00:00
|
|
|
apk_trust_init(&ac->trust);
|
2020-10-05 15:52:51 +00:00
|
|
|
apk_out_reset(&ac->out);
|
|
|
|
ac->out.out = stdout;
|
|
|
|
ac->out.err = stderr;
|
|
|
|
ac->out.verbosity = 1;
|
2021-11-05 11:20:19 +00:00
|
|
|
apk_digest_ctx_init(&ac->dctx, APK_DIGEST_SHA256);
|
2020-10-05 15:52:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void apk_ctx_free(struct apk_ctx *ac)
|
|
|
|
{
|
2020-10-09 09:44:32 +00:00
|
|
|
apk_id_cache_free(&ac->id_cache);
|
2020-10-09 10:40:14 +00:00
|
|
|
apk_trust_free(&ac->trust);
|
2020-10-05 15:52:51 +00:00
|
|
|
apk_string_array_free(&ac->repository_list);
|
2021-02-14 15:01:02 +00:00
|
|
|
if (ac->out.log) fclose(ac->out.log);
|
2020-10-05 15:52:51 +00:00
|
|
|
}
|
2020-10-09 09:44:32 +00:00
|
|
|
|
|
|
|
int apk_ctx_prepare(struct apk_ctx *ac)
|
|
|
|
{
|
|
|
|
if (ac->flags & APK_SIMULATE &&
|
|
|
|
ac->open_flags & (APK_OPENF_CREATE | APK_OPENF_WRITE)) {
|
|
|
|
ac->open_flags &= ~(APK_OPENF_CREATE | APK_OPENF_WRITE);
|
|
|
|
ac->open_flags |= APK_OPENF_READ;
|
|
|
|
}
|
2021-09-13 10:17:26 +00:00
|
|
|
if (ac->flags & APK_ALLOW_UNTRUSTED) ac->trust.allow_untrusted = 1;
|
2020-10-09 09:44:32 +00:00
|
|
|
if (!ac->cache_dir) ac->cache_dir = "etc/apk/cache";
|
|
|
|
if (!ac->keys_dir) ac->keys_dir = "etc/apk/keys";
|
|
|
|
if (!ac->root) ac->root = "/";
|
|
|
|
if (!ac->cache_max_age) ac->cache_max_age = 4*60*60; /* 4 hours default */
|
2021-11-05 11:20:19 +00:00
|
|
|
|
|
|
|
if (!strcmp(ac->root, "/")) {
|
|
|
|
// No chroot needed if using system root
|
|
|
|
ac->flags |= APK_NO_CHROOT;
|
|
|
|
|
|
|
|
// Check uvol availability
|
2021-12-17 19:09:42 +00:00
|
|
|
ac->uvol = getenv("APK_UVOL") ?: "/usr/sbin/uvol";
|
2021-11-05 11:20:19 +00:00
|
|
|
if (access(ac->uvol, X_OK) != 0)
|
|
|
|
ac->uvol = ERR_PTR(-APKE_UVOL_NOT_AVAILABLE);
|
|
|
|
} else {
|
|
|
|
ac->uvol = ERR_PTR(-APKE_UVOL_ROOT);
|
|
|
|
}
|
|
|
|
|
2020-10-09 09:44:32 +00:00
|
|
|
|
|
|
|
ac->root_fd = openat(AT_FDCWD, ac->root, O_RDONLY | O_CLOEXEC);
|
|
|
|
if (ac->root_fd < 0 && (ac->open_flags & APK_OPENF_CREATE)) {
|
|
|
|
mkdirat(AT_FDCWD, ac->root, 0755);
|
|
|
|
ac->root_fd = openat(AT_FDCWD, ac->root, O_RDONLY | O_CLOEXEC);
|
|
|
|
}
|
|
|
|
if (ac->root_fd < 0) {
|
|
|
|
apk_err(&ac->out, "Unable to open root: %s", apk_error_str(errno));
|
|
|
|
return -errno;
|
|
|
|
}
|
2021-11-05 11:20:19 +00:00
|
|
|
ac->dest_fd = ac->root_fd;
|
2021-02-14 15:01:02 +00:00
|
|
|
|
2021-12-09 15:52:09 +00:00
|
|
|
if ((ac->open_flags & APK_OPENF_WRITE) && !(ac->flags & APK_NO_LOGFILE)) {
|
2021-11-05 11:20:19 +00:00
|
|
|
const char *log_path = "var/log/apk.log";
|
2021-02-14 15:01:02 +00:00
|
|
|
const int lflags = O_WRONLY | O_APPEND | O_CREAT | O_CLOEXEC;
|
|
|
|
int fd = openat(ac->root_fd, log_path, lflags, 0644);
|
|
|
|
if (fd < 0 && (ac->open_flags & APK_OPENF_CREATE)) {
|
2021-11-05 11:20:19 +00:00
|
|
|
mkdirat(ac->root_fd, "var", 0755);
|
|
|
|
mkdirat(ac->root_fd, "var/log", 0755);
|
2021-02-14 15:01:02 +00:00
|
|
|
fd = openat(ac->root_fd, log_path, lflags, 0644);
|
|
|
|
}
|
|
|
|
if (fd < 0) {
|
|
|
|
apk_err(&ac->out, "Unable to open log: %s", apk_error_str(errno));
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
ac->out.log = fdopen(fd, "a");
|
|
|
|
}
|
2020-10-09 09:44:32 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-10-09 10:40:14 +00:00
|
|
|
struct apk_trust *apk_ctx_get_trust(struct apk_ctx *ac)
|
2020-10-09 09:44:32 +00:00
|
|
|
{
|
2021-08-23 12:17:11 +00:00
|
|
|
if (!ac->trust.keys_loaded) {
|
|
|
|
int r = apk_trust_load_keys(&ac->trust,
|
|
|
|
openat(ac->root_fd, ac->keys_dir, O_RDONLY | O_CLOEXEC));
|
|
|
|
if (r != 0) apk_err(&ac->out, "Unable to load trust keys: %s", apk_error_str(r));
|
2020-10-09 09:44:32 +00:00
|
|
|
}
|
|
|
|
return &ac->trust;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct apk_id_cache *apk_ctx_get_id_cache(struct apk_ctx *ac)
|
|
|
|
{
|
|
|
|
if (!ac->id_cache.root_fd)
|
|
|
|
apk_id_cache_init(&ac->id_cache, apk_ctx_fd_root(ac));
|
|
|
|
return &ac->id_cache;
|
|
|
|
}
|