From 5f3477a5c92ab62e622a81674df88877528719ce Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sun, 29 Jun 2025 14:07:16 +0000 Subject: [PATCH] pwd: Directly pass the context Signed-off-by: Michael Tremer --- src/pakfire/file.c | 4 +-- src/pakfire/pwd.c | 64 ++++++++++++++++++++-------------------------- src/pakfire/pwd.h | 16 +++++++----- src/pakfire/root.c | 12 ++++----- 4 files changed, 46 insertions(+), 50 deletions(-) diff --git a/src/pakfire/file.c b/src/pakfire/file.c index 8dbee5c9..5edcec31 100644 --- a/src/pakfire/file.c +++ b/src/pakfire/file.c @@ -1545,8 +1545,8 @@ static int pakfire_file_verify_ownership(pakfire_file* file, const struct stat* const char* gname = pakfire_file_get_gname(file); // Fetch owner & group - struct passwd* owner = pakfire_getpwnam(file->root, uname); - struct group* group = pakfire_getgrnam(file->root, gname); + struct passwd* owner = pakfire_getpwnam(file->ctx, file->root, uname); + struct group* group = pakfire_getgrnam(file->ctx, file->root, gname); // Check if owner matches if (!owner || owner->pw_uid != uid) { diff --git a/src/pakfire/pwd.c b/src/pakfire/pwd.c index c1a993c5..8c3fa9d0 100644 --- a/src/pakfire/pwd.c +++ b/src/pakfire/pwd.c @@ -39,7 +39,7 @@ #define ETC_SUBUID "/etc/subuid" #define ETC_SUBGID "/etc/subgid" -static struct passwd* pakfire_getpwent(pakfire_root* root, +static struct passwd* pakfire_getpwent(pakfire_ctx* ctx, pakfire_root* root, int(*cmp)(struct passwd* entry, const void* value), const void* value) { struct passwd* entry = NULL; char path[PATH_MAX]; @@ -87,8 +87,8 @@ static int __pakfire_getpwnam(struct passwd* entry, const void* value) { return 0; } -struct passwd* pakfire_getpwnam(pakfire_root* root, const char* name) { - return pakfire_getpwent(root, __pakfire_getpwnam, name); +struct passwd* pakfire_getpwnam(pakfire_ctx* ctx, pakfire_root* root, const char* name) { + return pakfire_getpwent(ctx, root, __pakfire_getpwnam, name); } static int __pakfire_getpwuid(struct passwd* entry, const void* value) { @@ -100,11 +100,11 @@ static int __pakfire_getpwuid(struct passwd* entry, const void* value) { return 0; } -struct passwd* pakfire_getpwuid(pakfire_root* root, uid_t uid) { - return pakfire_getpwent(root, __pakfire_getpwuid, &uid); +struct passwd* pakfire_getpwuid(pakfire_ctx* ctx, pakfire_root* root, uid_t uid) { + return pakfire_getpwent(ctx, root, __pakfire_getpwuid, &uid); } -static struct group* pakfire_getgrent(pakfire_root* root, +static struct group* pakfire_getgrent(pakfire_ctx* ctx, pakfire_root* root, int(*cmp)(struct group* entry, const void* value), const void* value) { struct group* entry = NULL; char path[PATH_MAX]; @@ -152,8 +152,8 @@ static int __pakfire_getgrnam(struct group* entry, const void* value) { return 0; } -struct group* pakfire_getgrnam(pakfire_root* root, const char* name) { - return pakfire_getgrent(root, __pakfire_getgrnam, name); +struct group* pakfire_getgrnam(pakfire_ctx* ctx, pakfire_root* root, const char* name) { + return pakfire_getgrent(ctx, root, __pakfire_getgrnam, name); } static int __pakfire_getgrgid(struct group* entry, const void* value) { @@ -165,22 +165,20 @@ static int __pakfire_getgrgid(struct group* entry, const void* value) { return 0; } -struct group* pakfire_getgrgid(pakfire_root* root, gid_t gid) { - return pakfire_getgrent(root, __pakfire_getgrgid, &gid); +struct group* pakfire_getgrgid(pakfire_ctx* ctx, pakfire_root* root, gid_t gid) { + return pakfire_getgrent(ctx, root, __pakfire_getgrgid, &gid); } // SUBUID/SUBGID #ifdef HAVE_SUBID -static int pakfire_getsubid(pakfire_root* root, const char* owner, +static int pakfire_getsubid(pakfire_ctx* ctx, pakfire_root* root, const char* owner, struct pakfire_subid* subid, int (callback)(const char* owner, struct subid_range** ranges)) { struct subid_range* ranges = NULL; int count; int r = -1; - pakfire_ctx* ctx = pakfire_root_get_ctx(root); - r = subid_init(PACKAGE_NAME, stderr); if (r) { ERROR(ctx, "Could not setup subid: %m\n"); @@ -204,31 +202,30 @@ static int pakfire_getsubid(pakfire_root* root, const char* owner, ERROR: if (ranges) free(ranges); - if (ctx) - pakfire_ctx_unref(ctx); return r; } -int pakfire_getsubuid(pakfire_root* root, const char* owner, struct pakfire_subid* subid) { - return pakfire_getsubid(root, owner, subid, subid_get_uid_ranges); +int pakfire_getsubuid(pakfire_ctx* ctx, pakfire_root* root, + const char* owner, struct pakfire_subid* subid) { + return pakfire_getsubid(ctx, root, owner, subid, subid_get_uid_ranges); } -int pakfire_getsubgid(pakfire_root* root, const char* owner, struct pakfire_subid* subid) { - return pakfire_getsubid(root, owner, subid, subid_get_gid_ranges); +int pakfire_getsubgid(pakfire_ctx* ctx, pakfire_root* root, + const char* owner, struct pakfire_subid* subid) { + return pakfire_getsubid(ctx, root, owner, subid, subid_get_gid_ranges); } # else /* Our own implementation */ -static int pakfire_fgetsubid(pakfire_root* root, struct pakfire_subid* subid, FILE* f) { +static int pakfire_fgetsubid(pakfire_ctx* ctx, pakfire_root* root, + struct pakfire_subid* subid, FILE* f) { int r; char* line = NULL; size_t length = 0; char* p = NULL; - pakfire_ctx* ctx = pakfire_root_get_ctx(root); - // Read the next line while (1) { r = getline(&line, &length, f); @@ -286,9 +283,6 @@ ERROR: DEBUG(ctx, "Parsed SUBID entry: name=%s, id=%u, length=%zu\n", subid->name, subid->id, subid->length); - if (ctx) - pakfire_ctx_unref(ctx); - return r; } @@ -299,8 +293,8 @@ ERROR: If nothing was found, the function returns 1. If there was an error, the function may return something else. */ -static int pakfire_getsubid(pakfire_root* root, const char* path, const char* owner, - struct pakfire_subid* subid) { +static int pakfire_getsubid(pakfire_ctx* ctx, pakfire_root* root, + const char* path, const char* owner, struct pakfire_subid* subid) { struct pakfire_subid entry = {}; int r; @@ -308,8 +302,6 @@ static int pakfire_getsubid(pakfire_root* root, const char* path, const char* ow if (!owner) return 1; - pakfire_ctx* ctx = pakfire_root_get_ctx(root); - DEBUG(ctx, "Fetching SUBID from %s for %s\n", path, owner); // Open /etc/subuid @@ -331,7 +323,7 @@ static int pakfire_getsubid(pakfire_root* root, const char* path, const char* ow // Walk through all entries while (1) { - r = pakfire_fgetsubid(root, &entry, f); + r = pakfire_fgetsubid(ctx, root, &entry, f); switch (r) { case 0: break; @@ -361,18 +353,18 @@ static int pakfire_getsubid(pakfire_root* root, const char* path, const char* ow END: if (f) fclose(f); - if (ctx) - pakfire_ctx_unref(ctx); return r; } -int pakfire_getsubuid(pakfire_root* root, const char* owner, struct pakfire_subid* subid) { - return pakfire_getsubid(root, ETC_SUBUID, owner, subid); +int pakfire_getsubuid(pakfire_ctx* ctx, pakfire_root* root, + const char* owner, struct pakfire_subid* subid) { + return pakfire_getsubid(ctx, root, ETC_SUBUID, owner, subid); } -int pakfire_getsubgid(pakfire_root* root, const char* owner, struct pakfire_subid* subid) { - return pakfire_getsubid(root, ETC_SUBGID, owner, subid); +int pakfire_getsubgid(pakfire_ctx* ctx, pakfire_root* root, + const char* owner, struct pakfire_subid* subid) { + return pakfire_getsubid(ctx, root, ETC_SUBGID, owner, subid); } #endif diff --git a/src/pakfire/pwd.h b/src/pakfire/pwd.h index 0bbb4e0a..2af38654 100644 --- a/src/pakfire/pwd.h +++ b/src/pakfire/pwd.h @@ -30,13 +30,17 @@ struct pakfire_subid { size_t length; }; -struct passwd* pakfire_getpwnam(pakfire_root* root, const char* name); -struct passwd* pakfire_getpwuid(pakfire_root* root, uid_t uid); +#include -struct group* pakfire_getgrnam(pakfire_root* root, const char* name); -struct group* pakfire_getgrgid(pakfire_root* root, gid_t gid); +struct passwd* pakfire_getpwnam(pakfire_ctx* ctx, pakfire_root* root, const char* name); +struct passwd* pakfire_getpwuid(pakfire_ctx* ctx, pakfire_root* root, uid_t uid); -int pakfire_getsubuid(pakfire_root* root, const char* owner, struct pakfire_subid* subid); -int pakfire_getsubgid(pakfire_root* root, const char* owner, struct pakfire_subid* subid); +struct group* pakfire_getgrnam(pakfire_ctx* ctx, pakfire_root* root, const char* name); +struct group* pakfire_getgrgid(pakfire_ctx* ctx, pakfire_root* root, gid_t gid); + +int pakfire_getsubuid(pakfire_ctx* ctx, pakfire_root* root, + const char* owner, struct pakfire_subid* subid); +int pakfire_getsubgid(pakfire_ctx* ctx, pakfire_root* root, + const char* owner, struct pakfire_subid* subid); #endif /* PAKFIRE_PWD_H */ diff --git a/src/pakfire/root.c b/src/pakfire/root.c index 0aea2f30..9a5a239e 100644 --- a/src/pakfire/root.c +++ b/src/pakfire/root.c @@ -942,7 +942,7 @@ static int pakfire_root_setup_user(pakfire_root* self) { // Read SUBUID/SUBGIDs from file if (!pakfire_root_on_root(self)) { // Fetch SUBUIDs - r = pakfire_getsubuid(self, self->user.name, &self->user.subuids); + r = pakfire_getsubuid(self->ctx, self, self->user.name, &self->user.subuids); switch (r) { case 0: case 1: @@ -953,7 +953,7 @@ static int pakfire_root_setup_user(pakfire_root* self) { } // Fetch SUBGIDs - r = pakfire_getsubgid(self, self->user.name, &self->group.subgids); + r = pakfire_getsubgid(self->ctx, self, self->user.name, &self->group.subgids); switch (r) { case 0: case 1: @@ -1693,7 +1693,7 @@ static const char* pakfire_root_user_lookup(void* data, la_int64_t uid) { DEBUG(self->ctx, "Looking up name for UID %ld\n", uid); // Find a matching entry in /etc/passwd - struct passwd* entry = pakfire_getpwuid(self, uid); + struct passwd* entry = pakfire_getpwuid(self->ctx, self, uid); if (!entry) { ERROR(self->ctx, "Could not retrieve uname for %ld: %m\n", uid); return 0; @@ -1717,7 +1717,7 @@ static const char* pakfire_root_group_lookup(void* data, la_int64_t gid) { DEBUG(self->ctx, "Looking up name for GID %ld\n", gid); // Find a matching entry in /etc/group - struct group* entry = pakfire_getgrgid(self, gid); + struct group* entry = pakfire_getgrgid(self->ctx, self, gid); if (!entry) { ERROR(self->ctx, "Could not retrieve gname for %ld: %m\n", gid); return 0; @@ -1774,7 +1774,7 @@ static la_int64_t pakfire_root_uid_lookup(void* data, const char* name, la_int64 DEBUG(self->ctx, "Looking up UID for '%s' (%ld)\n", name, uid); // Find a matching entry in /etc/passwd - struct passwd* entry = pakfire_getpwnam(self, name); + struct passwd* entry = pakfire_getpwnam(self->ctx, self, name); if (!entry) { ERROR(self->ctx, "Could not retrieve UID for '%s': %m\n", name); goto ERROR; @@ -1803,7 +1803,7 @@ static la_int64_t pakfire_root_gid_lookup(void* data, const char* name, la_int64 DEBUG(self->ctx, "Looking up GID for '%s' (%ld)\n", name, gid); // Find a matching entry in /etc/group - struct group* entry = pakfire_getgrnam(self, name); + struct group* entry = pakfire_getgrnam(self->ctx, self, name); if (!entry) { ERROR(self->ctx, "Could not retrieve GID for '%s': %m\n", name); goto ERROR; -- 2.47.3