From 1931780a2051db6fe228df3ba4b7f30baf42fd0f Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Thu, 4 May 2023 16:41:47 +0000 Subject: [PATCH] pakfire: Fetch more user/group information at startup Signed-off-by: Michael Tremer --- src/libpakfire/pakfire.c | 81 ++++++++++++++++++++++++++++++++++------ 1 file changed, 69 insertions(+), 12 deletions(-) diff --git a/src/libpakfire/pakfire.c b/src/libpakfire/pakfire.c index 765f075b3..bb2831ab2 100644 --- a/src/libpakfire/pakfire.c +++ b/src/libpakfire/pakfire.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -81,8 +82,16 @@ struct pakfire { FILE* lock; // UID/GID of running user - uid_t uid; - gid_t gid; + struct pakfire_user { + uid_t uid; + char name[NAME_MAX]; + char home[PATH_MAX]; + } user; + + struct pakfire_group { + gid_t gid; + char name[NAME_MAX]; + } group; // Mapped UID/GID struct pakfire_subid subuid; @@ -168,11 +177,11 @@ int pakfire_on_root(struct pakfire* pakfire) { } uid_t pakfire_uid(struct pakfire* pakfire) { - return pakfire->uid; + return pakfire->user.uid; } gid_t pakfire_gid(struct pakfire* pakfire) { - return pakfire->gid; + return pakfire->group.gid; } const struct pakfire_subid* pakfire_subuid(struct pakfire* pakfire) { @@ -416,7 +425,7 @@ static int pakfire_safety_checks(struct pakfire* pakfire) { return 0; // We must be root in order to operate in / - if (pakfire->uid) { + if (pakfire->user.uid) { ERROR(pakfire, "Must be running as root on /\n"); errno = EPERM; return 1; @@ -728,7 +737,7 @@ static int pakfire_set_cache_path(struct pakfire* pakfire) { // Fetch the path from the configuration file const char* path = pakfire_config_get(pakfire->config, "general", "cache_path", NULL); if (!path) { - const char* home = pakfire_get_home(pakfire, pakfire->uid); + const char* home = pakfire_get_home(pakfire, pakfire->user.uid); if (!home) return 1; @@ -746,6 +755,53 @@ static int pakfire_set_cache_path(struct pakfire* pakfire) { path, pakfire->distro.id, pakfire->distro.version_id, pakfire->arch); } +static int pakfire_setup_user(struct pakfire* pakfire) { + struct passwd user; + struct passwd* u = NULL; + struct group group; + struct group* g = NULL; + char buffer[1024]; + int r; + + // Fetch the UID/GID we are running as + const uid_t uid = geteuid(); + const gid_t gid = getegid(); + + // Fetch all user information + r = getpwuid_r(uid, &user, buffer, sizeof(buffer), &u); + if (r) + goto ERROR; + + // Store UID + pakfire->user.uid = uid; + + // Store username + r = pakfire_string_set(pakfire->user.name, user.pw_name); + if (r) + goto ERROR; + + // Store home directory + r = pakfire_string_set(pakfire->user.home, user.pw_dir); + if (r) + goto ERROR; + + // Fetch all group information + r = getgrgid_r(gid, &group, buffer, sizeof(buffer), &g); + if (r) + goto ERROR; + + // Store GID + pakfire->group.gid = gid; + + // Store name + r = pakfire_string_set(pakfire->group.name, group.gr_name); + if (r) + goto ERROR; + +ERROR: + return r; +} + PAKFIRE_EXPORT int pakfire_create(struct pakfire** pakfire, const char* path, const char* arch, const char* conf, int flags, int loglevel, pakfire_log_callback log_callback, void* log_data) { @@ -779,10 +835,6 @@ PAKFIRE_EXPORT int pakfire_create(struct pakfire** pakfire, const char* path, p->nrefs = 1; p->flags = flags; - // Store the UID/GID we are running as - p->uid = geteuid(); - p->gid = getegid(); - // Set architecture pakfire_string_set(p->arch, arch); @@ -801,6 +853,11 @@ PAKFIRE_EXPORT int pakfire_create(struct pakfire** pakfire, const char* path, pakfire_log_set_priority(p, log_priority(env)); } + // Setup user/group + r = pakfire_setup_user(p); + if (r) + goto ERROR; + // Initialise configuration r = pakfire_config_create(&p->config); if (r) @@ -838,14 +895,14 @@ PAKFIRE_EXPORT int pakfire_create(struct pakfire** pakfire, const char* path, // Fetch sub UID/GIDs if (!pakfire_on_root(p)) { // UID - r = pakfire_getsubid(p, "/etc/subuid", p->uid, &p->subuid); + r = pakfire_getsubid(p, "/etc/subuid", p->user.uid, &p->subuid); if (r) { ERROR(p, "Could not fetch subuid: %m\n"); goto ERROR; } // GID - r = pakfire_getsubid(p, "/etc/subgid", p->uid, &p->subgid); + r = pakfire_getsubid(p, "/etc/subgid", p->user.uid, &p->subgid); if (r) { ERROR(p, "Could not fetch subgid: %m\n"); goto ERROR; -- 2.39.5