util-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Quentin Rameau <quinq@fifth.space>
To: util-linux@vger.kernel.org
Cc: Quentin Rameau <quinq@fifth.space>
Subject: [PATCH 1/2] lib/pwdutils: add xgetpwuid
Date: Sat, 21 Sep 2019 20:50:20 +0200	[thread overview]
Message-ID: <20190921185021.10568-2-quinq@fifth.space> (raw)
In-Reply-To: <20190921185021.10568-1-quinq@fifth.space>

---
 include/pwdutils.h |  1 +
 lib/pwdutils.c     | 28 ++++++++++++++++++++++++++++
 2 files changed, 29 insertions(+)

diff --git a/include/pwdutils.h b/include/pwdutils.h
index a69dd6b45..bea46e57e 100644
--- a/include/pwdutils.h
+++ b/include/pwdutils.h
@@ -5,6 +5,7 @@
 #include <pwd.h>
 
 extern struct passwd *xgetpwnam(const char *username, char **pwdbuf);
+extern struct passwd *xgetpwuid(uid_t uid, char **pwdbuf);
 extern char *xgetlogin(void);
 
 #endif /* UTIL_LINUX_PWDUTILS_H */
diff --git a/lib/pwdutils.c b/lib/pwdutils.c
index 25b4daed0..d54458d65 100644
--- a/lib/pwdutils.c
+++ b/lib/pwdutils.c
@@ -36,6 +36,34 @@ failed:
 	return NULL;
 }
 
+struct passwd *xgetpwuid(uid_t uid, char **pwdbuf)
+{
+	struct passwd *pwd = NULL, *res = NULL;
+	int rc;
+
+	if (!pwdbuf)
+		return NULL;
+
+	*pwdbuf = xmalloc(UL_GETPW_BUFSIZ);
+	pwd = xcalloc(1, sizeof(struct passwd));
+
+	errno = 0;
+	rc = getpwuid_r(uid, pwd, *pwdbuf, UL_GETPW_BUFSIZ, &res);
+	if (rc != 0) {
+		errno = rc;
+		goto failed;
+	}
+	if (!res) {
+		errno = EINVAL;
+		goto failed;
+	}
+	return pwd;
+failed:
+	free(pwd);
+	free(*pwdbuf);
+	return NULL;
+}
+
 char *xgetlogin(void)
 {
 	struct passwd *pw = NULL;
-- 
2.21.0


  reply	other threads:[~2019-09-21 18:57 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-09-21 18:50 [PATCH 0/2] Fix chsh when called as root Quentin Rameau
2019-09-21 18:50 ` Quentin Rameau [this message]
2019-09-21 18:50 ` [PATCH 2/2] chsh: replace getpw unsafe functions with xgetpw Quentin Rameau
2019-09-26 13:57 ` [PATCH 0/2] Fix chsh when called as root Karel Zak
2019-09-26 21:34   ` Quentin Rameau

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190921185021.10568-2-quinq@fifth.space \
    --to=quinq@fifth.space \
    --cc=util-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).