]> git.ipfire.org Git - pakfire.git/commitdiff
archive: extract: Set user permissions correctly
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 8 Mar 2021 11:33:52 +0000 (11:33 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 8 Mar 2021 11:33:52 +0000 (11:33 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/libpakfire/archive.c
src/libpakfire/include/pakfire/pwd.h [new file with mode: 0644]
src/libpakfire/pwd.c [new file with mode: 0644]

index 701029e8ead2c8f6c0861da8685645b28265f35a..d11b72c8e5eec5c99bfa568e983c8a1f649aab3e 100644 (file)
@@ -277,6 +277,7 @@ libpakfire_la_SOURCES = \
        src/libpakfire/pakfire.c \
        src/libpakfire/parser.c \
        src/libpakfire/problem.c \
+       src/libpakfire/pwd.c \
        src/libpakfire/relation.c \
        src/libpakfire/relationlist.c \
        src/libpakfire/repo.c \
@@ -308,6 +309,7 @@ pkginclude_HEADERS += \
        src/libpakfire/include/pakfire/parser.h \
        src/libpakfire/include/pakfire/private.h \
        src/libpakfire/include/pakfire/problem.h \
+       src/libpakfire/include/pakfire/pwd.h \
        src/libpakfire/include/pakfire/relation.h \
        src/libpakfire/include/pakfire/relationlist.h \
        src/libpakfire/include/pakfire/repo.h \
index 424f0996c27f4869c2aa1a2873393989fda57f27..3d694b44e95b0fc0cbb78b1cf3b2c3acc399baeb 100644 (file)
@@ -48,6 +48,7 @@
 #include <pakfire/pakfire.h>
 #include <pakfire/parser.h>
 #include <pakfire/private.h>
+#include <pakfire/pwd.h>
 #include <pakfire/repo.h>
 #include <pakfire/scriptlet.h>
 #include <pakfire/util.h>
@@ -699,6 +700,25 @@ static int archive_copy_data(PakfireArchive archive, struct archive* in, struct
        return 0;
 }
 
+static la_int64_t archive_user_lookup(void* data, const char* name, la_int64_t uid) {
+       Pakfire pakfire = (Pakfire)data;
+
+       // Fast path for "root"
+       if (strcmp(name, "root") == 0)
+               return 0;
+
+       // Find a matching entry in /etc/passwd
+       struct passwd* entry = pakfire_getpwnam(pakfire, name);
+       if (!entry) {
+               ERROR(pakfire, "Could not retrieve UID for '%s': %s\n", name, strerror(errno));
+               return 0;
+       }
+
+       DEBUG(pakfire, "Mapping %s to UID %d\n", name, entry->pw_uid);
+
+       return entry->pw_uid;
+}
+
 static int archive_extract(PakfireArchive archive, struct archive* a, const char* prefix) {
        struct archive_entry* entry;
        int r;
@@ -723,6 +743,9 @@ static int archive_extract(PakfireArchive archive, struct archive* a, const char
        archive_write_disk_set_options(ext, flags);
        archive_write_disk_set_standard_lookup(ext);
 
+       // Install our own routine for user lookups
+       archive_write_disk_set_user_lookup(ext, archive->pakfire, archive_user_lookup, NULL);
+
        for (;;) {
                r = archive_read_next_header(a, &entry);
 
diff --git a/src/libpakfire/include/pakfire/pwd.h b/src/libpakfire/include/pakfire/pwd.h
new file mode 100644 (file)
index 0000000..118629e
--- /dev/null
@@ -0,0 +1,35 @@
+/*#############################################################################
+#                                                                             #
+# Pakfire - The IPFire package management system                              #
+# Copyright (C) 2021 Pakfire development team                                 #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#ifndef PAKFIRE_PWD_H
+#define PAKFIRE_PWD_H
+
+#ifdef PAKFIRE_PRIVATE
+
+#include <pwd.h>
+
+#include <pakfire/types.h>
+
+struct passwd* pakfire_getpwnam(Pakfire pakfire, const char* name);
+struct passwd* pakfire_getpwuid(Pakfire pakfire, uid_t uid);
+
+#endif
+
+#endif /* PAKFIRE_PWD_H */
diff --git a/src/libpakfire/pwd.c b/src/libpakfire/pwd.c
new file mode 100644 (file)
index 0000000..47143fa
--- /dev/null
@@ -0,0 +1,87 @@
+/*#############################################################################
+#                                                                             #
+# Pakfire - The IPFire package management system                              #
+# Copyright (C) 2021 Pakfire development team                                 #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#include <pwd.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <pakfire/pakfire.h>
+#include <pakfire/pwd.h>
+
+static struct passwd* pakfire_getpwent(Pakfire pakfire,
+               int(*cmp)(struct passwd* entry, const void* value), const void* value) {
+       struct passwd* entry = NULL;
+
+       // Get path to /etc/passwd
+       char* path = pakfire_make_path(pakfire, "/etc/passwd");
+       if (!path)
+               return NULL;
+
+       FILE* f = fopen(path, "r");
+       if (!f)
+               goto END;
+
+       for (;;) {
+               // Parse entry
+               entry = fgetpwent(f);
+               if (!entry)
+                       goto END;
+
+               // Check if this is what we are looking for
+               if (cmp(entry, value))
+                       break;
+               else
+                       entry = NULL;
+       }
+
+END:
+       free(path);
+
+       if (f)
+               fclose(f);
+
+       return entry;
+}
+
+static int __pakfire_getpwnam(struct passwd* entry, const void* value) {
+       const char* name = (const char*)value;
+
+       if (strcmp(entry->pw_name, name) == 0)
+               return 1;
+
+       return 0;
+}
+
+struct passwd* pakfire_getpwnam(Pakfire pakfire, const char* name) {
+       return pakfire_getpwent(pakfire, __pakfire_getpwnam, name);
+}
+
+static int __pakfire_getpwuid(struct passwd* entry, const void* value) {
+       uid_t* uid = (uid_t*)value;
+
+       if (entry->pw_uid == *uid)
+               return 1;
+
+       return 0;
+}
+
+struct passwd* pakfire_getpwuid(Pakfire pakfire, uid_t uid) {
+       return pakfire_getpwent(pakfire, __pakfire_getpwuid, &uid);
+}