]> git.ipfire.org Git - pakfire.git/commitdiff
pwd: Directly pass the context
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 29 Jun 2025 14:07:16 +0000 (14:07 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 29 Jun 2025 14:07:16 +0000 (14:07 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/file.c
src/pakfire/pwd.c
src/pakfire/pwd.h
src/pakfire/root.c

index 8dbee5c97fa5d45e15165badc19e864905a94059..5edcec31544c6ce57045a9cfa0933f02aa136075 100644 (file)
@@ -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) {
index c1a993c5738544c8d8c7754c35cedaf2474bf25a..8c3fa9d00197f953e2ab99ee85f379ddf5b5b241 100644 (file)
@@ -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
index 0bbb4e0acf7e487581616779501243e5a1d05fcd..2af38654352d203bc9a089a6a90b84087f95856a 100644 (file)
@@ -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 <pakfire/ctx.h>
 
-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 */
index 0aea2f308d2b4b2dbeff127217747f299f6c15e8..9a5a239e19d2ccf12d6fd281756b770bc23086cd 100644 (file)
@@ -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;