[2/2] nss: allow domainless lookups
diff mbox

Message ID 1297420176-24960-3-git-send-email-jengelh@medozas.de
State RFC, archived
Headers show

Commit Message

Jan Engelhardt Feb. 11, 2011, 10:29 a.m. UTC
None

Patch
diff mbox

diff --git a/nss.c b/nss.c
index 04aff19..095794c 100644
--- a/nss.c
+++ b/nss.c
@@ -37,7 +37,9 @@ 
 #include <sys/types.h>
 #include <errno.h>
 #include <unistd.h>
+#include <stdbool.h>
 #include <stdlib.h>
+#include <stdio.h>
 #include <string.h>
 #include <pwd.h>
 #include <grp.h>
@@ -160,6 +162,14 @@  struct pwbuf {
 	char buf[1];
 };
 
+static bool only_digits(const char *s)
+{
+	for (; *s != '\0'; ++s)
+		if (!isdigit(*s))
+			return false;
+	return true;
+}
+
 static struct passwd *nss_getpwnam(const char *name, const char *domain, int *err_p)
 {
 	struct passwd *pw;
@@ -172,6 +182,16 @@  static struct passwd *nss_getpwnam(const char *name, const char *domain, int *er
 	if (buf == NULL)
 		goto err;
 
+	if (strchr(name, '@') == NULL && only_digits(name)) {
+		char *end;
+		long uid = strtoul(name, &end, 10);
+		err = getpwuid_r(uid, &buf->pwbuf, buf->buf, buflen, &pw);
+		IDMAP_LOG(0, ("%s: pure UID %ld lookup (result=%d,errno=%s)\n",
+			__func__, uid, err, strerror(errno)));
+		if (err == 0)
+			goto done;
+	}
+
 	err = EINVAL;
 	localname = strip_domain(name, domain);
 	IDMAP_LOG(4, ("nss_getpwnam: name '%s' domain '%s': "
@@ -189,6 +209,7 @@  static struct passwd *nss_getpwnam(const char *name, const char *domain, int *er
 			("nss_getpwnam: name '%s' not found in domain '%s'\n",
 			localname, domain));
 	free(localname);
+done:
 	if (err == 0 && pw != NULL) {
 		*err_p = 0;
 		return pw;