]> git.ipfire.org Git - pakfire.git/commitdiff
pakfire: Don't fail when looking up empty UID/GIDs
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 12 Oct 2024 14:15:55 +0000 (14:15 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 12 Oct 2024 14:15:55 +0000 (14:15 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/pakfire.c

index c4a0590d3db5e3df108536c305093377f0b30509..1cae2522a8b3254c7255618aa20b068c988ab63c 100644 (file)
@@ -1543,9 +1543,13 @@ struct archive* pakfire_make_archive_disk_reader(struct pakfire* pakfire, int in
 static la_int64_t pakfire_uid_lookup(void* data, const char* name, la_int64_t uid) {
        struct pakfire* pakfire = (struct pakfire*)data;
 
+       // Cannot handle empty names
+       if (!*name)
+               goto ERROR;
+
        // Fast path for "root"
        if (strcmp(name, "root") == 0)
-               return pakfire_map_id(pakfire, &pakfire->user.subuids, 0);
+               goto ERROR;
 
        CTX_DEBUG(pakfire->ctx, "Looking up UID for '%s' (%ld)\n", name, uid);
 
@@ -1553,20 +1557,28 @@ static la_int64_t pakfire_uid_lookup(void* data, const char* name, la_int64_t ui
        struct passwd* entry = pakfire_getpwnam(pakfire, name);
        if (!entry) {
                CTX_ERROR(pakfire->ctx, "Could not retrieve UID for '%s': %m\n", name);
-               return pakfire_map_id(pakfire, &pakfire->user.subuids, 0);
+               goto ERROR;
        }
 
        CTX_DEBUG(pakfire->ctx, "Mapping %s to UID %u\n", name, entry->pw_uid);
 
        return pakfire_map_id(pakfire, &pakfire->user.subuids, entry->pw_uid);
+
+ERROR:
+       // Return root on error
+       return pakfire_map_id(pakfire, &pakfire->user.subuids, 0);
 }
 
 static la_int64_t pakfire_gid_lookup(void* data, const char* name, la_int64_t gid) {
        struct pakfire* pakfire = (struct pakfire*)data;
 
+       // Cannot handle empty names
+       if (!*name)
+               goto ERROR;
+
        // Fast path for "root"
        if (strcmp(name, "root") == 0)
-               return pakfire_map_id(pakfire, &pakfire->group.subgids, 0);
+               goto ERROR;
 
        CTX_DEBUG(pakfire->ctx, "Looking up GID for '%s' (%ld)\n", name, gid);
 
@@ -1574,12 +1586,16 @@ static la_int64_t pakfire_gid_lookup(void* data, const char* name, la_int64_t gi
        struct group* entry = pakfire_getgrnam(pakfire, name);
        if (!entry) {
                CTX_ERROR(pakfire->ctx, "Could not retrieve GID for '%s': %m\n", name);
-               return pakfire_map_id(pakfire, &pakfire->group.subgids, 0);
+               goto ERROR;
        }
 
        CTX_DEBUG(pakfire->ctx, "Mapping %s to GID %u\n", name, entry->gr_gid);
 
        return pakfire_map_id(pakfire, &pakfire->group.subgids, entry->gr_gid);
+
+ERROR:
+       // Return root on error
+       return pakfire_map_id(pakfire, &pakfire->group.subgids, 0);
 }
 
 struct archive* pakfire_make_archive_disk_writer(struct pakfire* pakfire, int internal) {