From a1e8af753cfe312d22c1b497243ef1f98d97052c Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Tue, 21 Sep 2010 21:44:19 +0200 Subject: [PATCH] libmount: add utils for work with uid/gid Signed-off-by: Karel Zak --- shlibs/mount/src/mountP.h | 3 +++ shlibs/mount/src/utils.c | 43 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/shlibs/mount/src/mountP.h b/shlibs/mount/src/mountP.h index f5958957..230f51cb 100644 --- a/shlibs/mount/src/mountP.h +++ b/shlibs/mount/src/mountP.h @@ -107,6 +107,9 @@ extern int endswith(const char *s, const char *sx); extern int startswith(const char *s, const char *sx); extern char *mnt_get_username(const uid_t uid); +extern int mnt_get_uid(const char *username, uid_t *uid); +extern int mnt_get_gid(const char *groupname, gid_t *gid); + extern int mnt_has_regular_mtab(void); extern char *mnt_get_mountpoint(const char *path); diff --git a/shlibs/mount/src/utils.c b/shlibs/mount/src/utils.c index f48e533d..108bb2eb 100644 --- a/shlibs/mount/src/utils.c +++ b/shlibs/mount/src/utils.c @@ -27,6 +27,7 @@ #include #include #include +#include #include "strutils.h" #include "pathnames.h" @@ -316,6 +317,48 @@ char *mnt_get_username(const uid_t uid) return username; } +int mnt_get_uid(const char *username, uid_t *uid) +{ + struct passwd pwd; + struct passwd *pw; + size_t sz = sysconf(_SC_GETPW_R_SIZE_MAX); + char *buf; + + if (sz <= 0) + sz = 16384; /* Should be more than enough */ + + buf = malloc(sz); + if (!buf) + return -ENOMEM; + + if (!getpwnam_r(username, &pwd, buf, sz, &pw) && pw) + *uid= pw->pw_uid; + + free(buf); + return 0; +} + +int mnt_get_gid(const char *groupname, gid_t *gid) +{ + struct group grp; + struct group *gr; + size_t sz = sysconf(_SC_GETGR_R_SIZE_MAX); + char *buf; + + if (sz <= 0) + sz = 16384; /* Should be more than enough */ + + buf = malloc(sz); + if (!buf) + return -ENOMEM; + + if (!getgrnam_r(groupname, &grp, buf, sz, &gr) && gr) + *gid= gr->gr_gid; + + free(buf); + return 0; +} + /* * Returns 1 if /etc/mtab is a reqular file. */ -- 2.39.5