From: Matthew Harm Bekkema Date: Wed, 15 Apr 2020 12:49:54 +0000 (+1000) Subject: lib/pwdutils: add xgetgrnam X-Git-Tag: v2.36-rc1~140^2~1 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=dc96ca29d872a611801bdfd671c43c10c045cd3a;p=thirdparty%2Futil-linux.git lib/pwdutils: add xgetgrnam Signed-off-by: Matthew Harm Bekkema --- diff --git a/include/pwdutils.h b/include/pwdutils.h index bea46e57e3..b58268d775 100644 --- a/include/pwdutils.h +++ b/include/pwdutils.h @@ -3,8 +3,10 @@ #include #include +#include extern struct passwd *xgetpwnam(const char *username, char **pwdbuf); +extern struct group *xgetgrnam(const char *groupname, char **grpbuf); extern struct passwd *xgetpwuid(uid_t uid, char **pwdbuf); extern char *xgetlogin(void); diff --git a/lib/pwdutils.c b/lib/pwdutils.c index d54458d65d..d5f4d2e232 100644 --- a/lib/pwdutils.c +++ b/lib/pwdutils.c @@ -36,6 +36,38 @@ failed: return NULL; } +/* Returns allocated group and allocated grpbuf to store group strings + * fields. In case of error returns NULL and set errno, for unknown group set + * errno to EINVAL + */ +struct group *xgetgrnam(const char *groupname, char **grpbuf) +{ + struct group *grp = NULL, *res = NULL; + int rc; + + if (!grpbuf || !groupname) + return NULL; + + *grpbuf = xmalloc(UL_GETPW_BUFSIZ); + grp = xcalloc(1, sizeof(struct group)); + + errno = 0; + rc = getgrnam_r(groupname, grp, *grpbuf, UL_GETPW_BUFSIZ, &res); + if (rc != 0) { + errno = rc; + goto failed; + } + if (!res) { + errno = EINVAL; + goto failed; + } + return grp; +failed: + free(grp); + free(*grpbuf); + return NULL; +} + struct passwd *xgetpwuid(uid_t uid, char **pwdbuf) { struct passwd *pwd = NULL, *res = NULL;