From: Michael Tremer Date: Mon, 8 Mar 2021 11:33:52 +0000 (+0000) Subject: archive: extract: Set user permissions correctly X-Git-Tag: 0.9.28~1285^2~607 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ce389fc8521ad79757370fce54099facd6dfbf78;p=pakfire.git archive: extract: Set user permissions correctly Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index 701029e8e..d11b72c8e 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 \ diff --git a/src/libpakfire/archive.c b/src/libpakfire/archive.c index 424f0996c..3d694b44e 100644 --- a/src/libpakfire/archive.c +++ b/src/libpakfire/archive.c @@ -48,6 +48,7 @@ #include #include #include +#include #include #include #include @@ -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 index 000000000..118629e67 --- /dev/null +++ b/src/libpakfire/include/pakfire/pwd.h @@ -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 . # +# # +#############################################################################*/ + +#ifndef PAKFIRE_PWD_H +#define PAKFIRE_PWD_H + +#ifdef PAKFIRE_PRIVATE + +#include + +#include + +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 index 000000000..47143fab0 --- /dev/null +++ b/src/libpakfire/pwd.c @@ -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 . # +# # +#############################################################################*/ + +#include +#include +#include + +#include +#include + +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); +}