summaryrefslogtreecommitdiffstats
path: root/src/util/usertools.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/usertools.c')
-rw-r--r--src/util/usertools.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/util/usertools.c b/src/util/usertools.c
index 809b42d67..a0b914e2f 100644
--- a/src/util/usertools.c
+++ b/src/util/usertools.c
@@ -23,8 +23,11 @@
#include <pcre.h>
#include <errno.h>
#include <talloc.h>
+#include <pwd.h>
+#include <grp.h>
#include "confdb/confdb.h"
+#include "util/strtonum.h"
#include "util/util.h"
#include "util/safe-format-string.h"
#include "responder/common/responder.h"
@@ -659,3 +662,44 @@ sss_get_domain_name(TALLOC_CTX *mem_ctx,
return user_name;
}
+
+errno_t sss_user_by_name_or_uid(const char *input, uid_t *_uid, gid_t *_gid)
+{
+ uid_t uid;
+ errno_t ret;
+ char *endptr;
+ struct passwd *pwd;
+
+ /* Try if it's an ID first */
+ errno = 0;
+ uid = strtouint32(input, &endptr, 10);
+ if (errno != 0 || *endptr != '\0') {
+ ret = errno;
+ if (ret == ERANGE) {
+ DEBUG(SSSDBG_OP_FAILURE,
+ "UID [%s] is out of range.\n", input);
+ return ret;
+ }
+
+ /* Nope, maybe a username? */
+ pwd = getpwnam(input);
+ } else {
+ pwd = getpwuid(uid);
+ }
+
+ if (pwd == NULL) {
+ DEBUG(SSSDBG_OP_FAILURE,
+ "[%s] is neither a valid UID nor a user name which could be "
+ "resolved by getpwnam().\n", input);
+ return EINVAL;
+ }
+
+ if (_uid) {
+ *_uid = pwd->pw_uid;
+ }
+
+ if (_gid) {
+ *_gid = pwd->pw_gid;
+ }
+ return EOK;
+}