]> git.ipfire.org Git - people/ms/pakfire.git/commitdiff
packager: Correctly resolve UIDs/GIDs
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 8 Mar 2021 23:32:37 +0000 (23:32 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 8 Mar 2021 23:32:37 +0000 (23:32 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/include/pakfire/pwd.h
src/libpakfire/packager.c
src/libpakfire/pwd.c

index 372cf1a9fafe5382fc3b09fbe3e0977120929bd0..fc04a52d8bf1568402f85ac25b44a645630d0ecb 100644 (file)
@@ -32,7 +32,7 @@ struct passwd* pakfire_getpwnam(Pakfire pakfire, const char* name);
 struct passwd* pakfire_getpwuid(Pakfire pakfire, uid_t uid);
 
 struct group* pakfire_getgrnam(Pakfire pakfire, const char* name);
-struct group* pakfire_getgruid(Pakfire pakfire, gid_t gid);
+struct group* pakfire_getgrgid(Pakfire pakfire, gid_t gid);
 
 #endif
 
index a7ecc3d75b511d3f1ac4a6967c7d4665e2f20f35..d9687011f3f54a1756f4e70fb76a006c8a490299 100644 (file)
@@ -38,6 +38,7 @@
 #include <pakfire/packager.h>
 #include <pakfire/pakfire.h>
 #include <pakfire/private.h>
+#include <pakfire/pwd.h>
 #include <pakfire/types.h>
 
 #define BUFFER_SIZE 64 * 1024
@@ -134,6 +135,44 @@ static void pakfire_packager_free(struct pakfire_packager* packager) {
        pakfire_unref(packager->pakfire);
 }
 
+static const char* pakfire_packager_user_lookup(void* data, la_int64_t uid) {
+       Pakfire pakfire = (Pakfire)data;
+
+       // Fast path for "root"
+       if (uid == 0)
+               return "root";
+
+       // Find a matching entry in /etc/passwd
+       struct passwd* entry = pakfire_getpwuid(pakfire, uid);
+       if (!entry) {
+               ERROR(pakfire, "Could not retrieve uname for %ld: %s\n", uid, strerror(errno));
+               return 0;
+       }
+
+       DEBUG(pakfire, "Mapping UID %ld to %s\n", uid, entry->pw_name);
+
+       return entry->pw_name;
+}
+
+static const char* pakfire_packager_group_lookup(void* data, la_int64_t gid) {
+       Pakfire pakfire = (Pakfire)data;
+
+       // Fast path for "root"
+       if (gid == 0)
+               return "root";
+
+       // Find a matching entry in /etc/group
+       struct group* entry = pakfire_getgrgid(pakfire, gid);
+       if (!entry) {
+               ERROR(pakfire, "Could not retrieve gname for %ld: %s\n", gid, strerror(errno));
+               return 0;
+       }
+
+       DEBUG(pakfire, "Mapping GID %ld to %s\n", gid, entry->gr_name);
+
+       return entry->gr_name;
+}
+
 static int pakfire_packager_create_reader(struct pakfire_packager* p) {
        // Open a reader
        p->reader = archive_read_disk_new();
@@ -150,6 +189,12 @@ static int pakfire_packager_create_reader(struct pakfire_packager* p) {
                return 1;
        }
 
+       // Install our own routine for user/group lookups
+       archive_read_disk_set_uname_lookup(p->reader, p->pakfire,
+               pakfire_packager_user_lookup, NULL);
+       archive_read_disk_set_gname_lookup(p->reader, p->pakfire,
+               pakfire_packager_group_lookup, NULL);
+
        return 0;
 }
 
index 819e31a3389cfdcc26b0bb12e781d4472371b617..01a965399bc195d780e9e9b29fedb7cf05eb0836 100644 (file)
@@ -144,6 +144,6 @@ static int __pakfire_getgrgid(struct group* entry, const void* value) {
        return 0;
 }
 
-struct group* pakfire_getgruid(Pakfire pakfire, gid_t gid) {
+struct group* pakfire_getgrgid(Pakfire pakfire, gid_t gid) {
        return pakfire_getgrent(pakfire, __pakfire_getgrgid, &gid);
 }