This is an automated email from the git hooks/post-receive script. guillem pushed a commit to branch main in repository dpkg.
View the commit online: https://git.dpkg.org/cgit/dpkg/dpkg.git/commit/?id=15b738f17ad253cef005a440f208b306eba89ab1 commit 15b738f17ad253cef005a440f208b306eba89ab1 Author: Guillem Jover <[email protected]> AuthorDate: Sun Aug 27 03:52:23 2023 +0200 libdpkg: Refactor passwd and group fetching into functions These are necessary for --root operation and to add support for sysusers and sysgroups. --- lib/dpkg/Makefile.am | 2 ++ lib/dpkg/db-fsys-override.c | 5 +++-- lib/dpkg/{glob.c => sysuser.c} | 48 ++++++++++++++++++++++++------------------ lib/dpkg/{fdio.h => sysuser.h} | 35 +++++++++++++++--------------- lib/dpkg/tarfn.c | 5 +++-- po/POTFILES.in | 1 + src/statoverride/main.c | 5 +++-- 7 files changed, 56 insertions(+), 45 deletions(-) diff --git a/lib/dpkg/Makefile.am b/lib/dpkg/Makefile.am index fad8e14d4..3f609ac60 100644 --- a/lib/dpkg/Makefile.am +++ b/lib/dpkg/Makefile.am @@ -128,6 +128,7 @@ libdpkg_la_SOURCES = \ strvec.c \ strwide.c \ subproc.c \ + sysuser.c \ tarfn.c \ test.h \ treewalk.c \ @@ -185,6 +186,7 @@ pkginclude_HEADERS = \ string.h \ strvec.h \ subproc.h \ + sysuser.h \ tarfn.h \ treewalk.h \ trigdeferred.h \ diff --git a/lib/dpkg/db-fsys-override.c b/lib/dpkg/db-fsys-override.c index 120bc3dd8..40141b753 100644 --- a/lib/dpkg/db-fsys-override.c +++ b/lib/dpkg/db-fsys-override.c @@ -38,6 +38,7 @@ #include <dpkg/dpkg-db.h> #include <dpkg/fdio.h> #include <dpkg/debug.h> +#include <dpkg/sysuser.h> #include <dpkg/db-fsys.h> uid_t @@ -55,7 +56,7 @@ statdb_parse_uid(const char *str) ohshit(_("invalid statoverride uid %s"), str); uid = (uid_t)value; } else { - const struct passwd *pw = getpwnam(str); + const struct passwd *pw = dpkg_sysuser_from_name(str); if (pw == NULL) uid = (uid_t)-1; @@ -81,7 +82,7 @@ statdb_parse_gid(const char *str) ohshit(_("invalid statoverride gid %s"), str); gid = (gid_t)value; } else { - const struct group *gr = getgrnam(str); + const struct group *gr = dpkg_sysgroup_from_name(str); if (gr == NULL) gid = (gid_t)-1; diff --git a/lib/dpkg/glob.c b/lib/dpkg/sysuser.c similarity index 59% copy from lib/dpkg/glob.c copy to lib/dpkg/sysuser.c index 90e401ccd..343a45a4b 100644 --- a/lib/dpkg/glob.c +++ b/lib/dpkg/sysuser.c @@ -1,8 +1,8 @@ /* * libdpkg - Debian packaging suite library routines - * glob.c - file globing functions + * sysuser.c - system user and group handling * - * Copyright © 2009, 2010 Guillem Jover <[email protected]> + * Copyright © 2023 Guillem Jover <[email protected]> * * This is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -21,30 +21,36 @@ #include <config.h> #include <compat.h> -#include <stdlib.h> +#include <dpkg/sysuser.h> -#include <dpkg/dpkg.h> -#include <dpkg/glob.h> - -void -glob_list_prepend(struct glob_node **list, char *pattern) +struct passwd * +dpkg_sysuser_from_name(const char *uname) { - struct glob_node *node; + struct passwd *pw; + + pw = getpwnam(uname); - node = m_malloc(sizeof(*node)); - node->pattern = pattern; - node->next = *list; - *list = node; + return pw; +} + +struct passwd * +dpkg_sysuser_from_uid(uid_t uid) +{ + return getpwuid(uid); } -void -glob_list_free(struct glob_node *head) +struct group * +dpkg_sysgroup_from_name(const char *gname) { - while (head) { - struct glob_node *node = head; + struct group *gr; - head = head->next; - free(node->pattern); - free(node); - } + gr = getgrnam(gname); + + return gr; +} + +struct group * +dpkg_sysgroup_from_gid(gid_t gid) +{ + return getgrgid(gid); } diff --git a/lib/dpkg/fdio.h b/lib/dpkg/sysuser.h similarity index 62% copy from lib/dpkg/fdio.h copy to lib/dpkg/sysuser.h index d714dbdad..9b73e88e9 100644 --- a/lib/dpkg/fdio.h +++ b/lib/dpkg/sysuser.h @@ -1,8 +1,8 @@ /* * libdpkg - Debian packaging suite library routines - * fdio.h - safe file descriptor based input/output + * sysuser.h - system user and group handling * - * Copyright © 2009-2010 Guillem Jover <[email protected]> + * Copyright © 2023 Guillem Jover <[email protected]> * * This is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -18,29 +18,28 @@ * along with this program. If not, see <https://www.gnu.org/licenses/>. */ -#ifndef LIBDPKG_FDIO_H -#define LIBDPKG_FDIO_H +#ifndef LIBDPKG_SYSUSER_H +#define LIBDPKG_SYSUSER_H #include <sys/types.h> -#include <dpkg/macros.h> +#include <pwd.h> +#include <grp.h> -DPKG_BEGIN_DECLS - -/** - * @defgroup fdio File descriptor I/O - * @ingroup dpkg-internal - * @{ - */ +#include <dpkg/dpkg.h> -ssize_t fd_read(int fd, void *buf, size_t len); -ssize_t fd_write(int fd, const void *buf, size_t len); +DPKG_BEGIN_DECLS -int -fd_allocate_size(int fd, off_t offset, off_t len); +struct passwd * +dpkg_sysuser_from_name(const char *uname); +struct passwd * +dpkg_sysuser_from_uid(uid_t uid); -/** @} */ +struct group * +dpkg_sysgroup_from_name(const char *gname); +struct group * +dpkg_sysgroup_from_gid(gid_t gid); DPKG_END_DECLS -#endif /* LIBDPKG_FDIO_H */ +#endif /* LIBDPKG_SYSUSER_H */ diff --git a/lib/dpkg/tarfn.c b/lib/dpkg/tarfn.c index 2ce07a0e4..4e8ed67bb 100644 --- a/lib/dpkg/tarfn.c +++ b/lib/dpkg/tarfn.c @@ -40,6 +40,7 @@ #include <dpkg/dpkg.h> #include <dpkg/i18n.h> #include <dpkg/error.h> +#include <dpkg/sysuser.h> #include <dpkg/tarfn.h> #define TAR_MAGIC_USTAR "ustar\0" "00" @@ -437,12 +438,12 @@ tar_entry_update_from_system(struct tar_entry *te) struct group *group; if (te->stat.uname) { - passwd = getpwnam(te->stat.uname); + passwd = dpkg_sysuser_from_name(te->stat.uname); if (passwd) te->stat.uid = passwd->pw_uid; } if (te->stat.gname) { - group = getgrnam(te->stat.gname); + group = dpkg_sysgroup_from_name(te->stat.gname); if (group) te->stat.gid = group->gr_gid; } diff --git a/po/POTFILES.in b/po/POTFILES.in index cbb2298b6..8614668b3 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -67,6 +67,7 @@ lib/dpkg/string.c lib/dpkg/strvec.c lib/dpkg/strwide.c lib/dpkg/subproc.c +lib/dpkg/sysuser.c lib/dpkg/tarfn.c lib/dpkg/treewalk.c lib/dpkg/trigdeferred.c diff --git a/src/statoverride/main.c b/src/statoverride/main.c index 1ba091f70..3b3fc464c 100644 --- a/src/statoverride/main.c +++ b/src/statoverride/main.c @@ -45,6 +45,7 @@ #include <dpkg/path.h> #include <dpkg/dir.h> #include <dpkg/glob.h> +#include <dpkg/sysuser.h> #include <dpkg/db-fsys.h> #include <dpkg/options.h> @@ -195,7 +196,7 @@ statdb_node_print(FILE *out, struct fsys_namenode *file) if (!filestat) return; - pw = getpwuid(filestat->uid); + pw = dpkg_sysuser_from_uid(filestat->uid); if (pw) fprintf(out, "%s ", pw->pw_name); else if (filestat->uname) @@ -203,7 +204,7 @@ statdb_node_print(FILE *out, struct fsys_namenode *file) else fprintf(out, "#%d ", filestat->uid); - gr = getgrgid(filestat->gid); + gr = dpkg_sysgroup_from_gid(filestat->gid); if (gr) fprintf(out, "%s ", gr->gr_name); else if (filestat->gname) -- Dpkg.Org's dpkg

