Blame SOURCES/libnfsidmap-0.26-rc3.patch

76e38b
diff --git a/.gitignore b/.gitignore
76e38b
index 6244609..d24d727 100644
76e38b
--- a/.gitignore
76e38b
+++ b/.gitignore
76e38b
@@ -17,18 +17,13 @@ libnfsidmap.pc
76e38b
 libtool
76e38b
 .libs/
76e38b
 libnfsidmap.la
76e38b
-libnfsidmap_la-cfg.lo
76e38b
-libnfsidmap_la-libnfsidmap.lo
76e38b
-libnfsidmap_la-strlcpy.lo
76e38b
-nss.lo
76e38b
 nsswitch.la
76e38b
 static.la
76e38b
-static.lo
76e38b
 umich_ldap.la
76e38b
-umich_ldap.lo
76e38b
 configure.in~
76e38b
 m4/
76e38b
 *.o
76e38b
+*.lo
76e38b
 cscope.*
76e38b
 config.h
76e38b
 config.h.in
76e38b
diff --git a/autogen.sh b/autogen.sh
76e38b
index ee89987..c17f6be 100755
76e38b
--- a/autogen.sh
76e38b
+++ b/autogen.sh
76e38b
@@ -37,5 +37,6 @@ fi
76e38b
 
76e38b
 aclocal
76e38b
 libtoolize --force --copy
76e38b
-autoupdate
76e38b
+autoheader
76e38b
+automake --add-missing --copy --gnu
76e38b
 autoreconf -vi -Wall
76e38b
diff --git a/idmapd.conf.5 b/idmapd.conf.5
76e38b
index 9c7f1ae..de1bfa9 100644
76e38b
--- a/idmapd.conf.5
76e38b
+++ b/idmapd.conf.5
76e38b
@@ -31,7 +31,7 @@
76e38b
 .\"
76e38b
 .TH idmapd.conf 5 "19 Nov 2008"
76e38b
 .SH NAME
76e38b
-idmapd.conf
76e38b
+idmapd.conf \- configuration file for libnfsidmap
76e38b
 .SH SYNOPSIS
76e38b
 Configuration file for libnfsidmap.  Used by idmapd and svcgssd to map NFSv4 name to and from ids.
76e38b
 .SH DESCRIPTION
76e38b
@@ -234,7 +234,6 @@ Number of seconds before timing out an LDAP request
76e38b
 .\" -------------------------------------------------------------------
76e38b
 .\"
76e38b
 .SH EXAMPLES
76e38b
-."
76e38b
 An example
76e38b
 .I /etc/idmapd.conf
76e38b
 file:
76e38b
@@ -266,7 +265,7 @@ johndoe@OTHER.DOMAIN.ORG = johnny
76e38b
 LDAP_server = ldap.domain.org
76e38b
 LDAP_base = dc=org,dc=domain
76e38b
 
76e38b
-.fo
76e38b
+.fi
76e38b
 .\"
76e38b
 .\" -------------------------------------------------------------------
76e38b
 .\" Additional sections
76e38b
@@ -275,11 +274,11 @@ LDAP_base = dc=org,dc=domain
76e38b
 .SH SEE ALSO
76e38b
 .BR idmapd (8)
76e38b
 .BR svcgssd (8)
76e38b
-.".SH COMPATIBILITY
76e38b
-.".SH STANDARDS
76e38b
-.".SH ACKNOWLEDGEMENTS
76e38b
-.".SH AUTHORS
76e38b
-.".SH HISTORY
76e38b
+.\".SH COMPATIBILITY
76e38b
+.\".SH STANDARDS
76e38b
+.\".SH ACKNOWLEDGEMENTS
76e38b
+.\".SH AUTHORS
76e38b
+.\".SH HISTORY
76e38b
 .SH BUGS
76e38b
 Report bugs to <nfsv4@linux-nfs.org>
76e38b
-.".SH CAVEATS
76e38b
+.\".SH CAVEATS
76e38b
diff --git a/libnfsidmap.c b/libnfsidmap.c
76e38b
index 57bb6c3..641d766 100644
76e38b
--- a/libnfsidmap.c
76e38b
+++ b/libnfsidmap.c
76e38b
@@ -285,8 +285,9 @@ int nfs4_init_name_mapping(char *conffile)
76e38b
 			}
76e38b
 			buf = malloc(siz);
76e38b
 			if (buf) {
76e38b
+				*buf = 0;
76e38b
 				TAILQ_FOREACH(r, &local_realms->fields, link) {
76e38b
-					sprintf(buf, "'%s' ", r->field);
76e38b
+					sprintf(buf+strlen(buf), "'%s' ", r->field);
76e38b
 				}
76e38b
 				IDMAP_LOG(1, ("libnfsidmap: Realms list: %s", buf));
76e38b
 				free(buf);
76e38b
diff --git a/static.c b/static.c
76e38b
index fffd458..8be87e8 100644
76e38b
--- a/static.c
76e38b
+++ b/static.c
76e38b
@@ -40,6 +40,7 @@
76e38b
 #include <grp.h>
76e38b
 #include <errno.h>
76e38b
 
76e38b
+#include "queue.h"
76e38b
 #include "cfg.h"
76e38b
 #include "nfsidmap.h"
76e38b
 #include "nfsidmap_internal.h"
76e38b
@@ -57,6 +58,40 @@ struct pwbuf {
76e38b
 	char buf[1];
76e38b
 };
76e38b
 
76e38b
+struct grbuf {
76e38b
+	struct group grbuf;
76e38b
+	char buf[1];
76e38b
+};
76e38b
+
76e38b
+struct uid_mapping {
76e38b
+	LIST_ENTRY (uid_mapping) link;
76e38b
+	uid_t uid;
76e38b
+	char * principal;
76e38b
+	char * localname;
76e38b
+};
76e38b
+
76e38b
+struct gid_mapping {
76e38b
+	LIST_ENTRY (gid_mapping) link;
76e38b
+	gid_t gid;
76e38b
+	char * principal;
76e38b
+	char * localgroup;
76e38b
+};
76e38b
+
76e38b
+static __inline__ u_int8_t uid_hash (uid_t uid)
76e38b
+{
76e38b
+	return uid % 256;
76e38b
+}
76e38b
+
76e38b
+static __inline__ u_int8_t gid_hash (gid_t gid)
76e38b
+{
76e38b
+	return gid % 256;
76e38b
+}
76e38b
+
76e38b
+//Hash tables of uid and guids to principals mappings.
76e38b
+//We reuse some queue/hash functions from cfg.c.
76e38b
+LIST_HEAD (uid_mappings, uid_mapping) uid_mappings[256];
76e38b
+LIST_HEAD (gid_mappings, gid_mapping) gid_mappings[256];
76e38b
+
76e38b
 static struct passwd *static_getpwnam(const char *name, const char *domain,
76e38b
 				      int *err_p)
76e38b
 {
76e38b
@@ -75,12 +110,9 @@ static struct passwd *static_getpwnam(const char *name, const char *domain,
76e38b
 	localname = conf_get_str("Static", (char *)name);
76e38b
 	if (!localname) {
76e38b
 		err = ENOENT;
76e38b
-		goto err;
76e38b
+		goto err_free_buf;
76e38b
 	}
76e38b
 
76e38b
-	IDMAP_LOG(4, ("static_getpwnam: name '%s' mapped to '%s'\n",
76e38b
-		  name, localname));
76e38b
-
76e38b
 again:
76e38b
 	err = getpwnam_r(localname, &buf->pwbuf, buf->buf, buflen, &pw;;
76e38b
 
76e38b
@@ -91,12 +123,15 @@ again:
76e38b
 		if (err == 0)
76e38b
 			err = ENOENT;
76e38b
 
76e38b
-		IDMAP_LOG(0, ("static_getpwnam: name '%s' not found\n",
76e38b
-			  localname));
76e38b
+		IDMAP_LOG(0, ("static_getpwnam: localname '%s' for '%s' not found\n",
76e38b
+		  localname, name));
76e38b
 
76e38b
 		goto err_free_buf;
76e38b
 	}
76e38b
 
76e38b
+	IDMAP_LOG(4, ("static_getpwnam: name '%s' mapped to '%s'\n",
76e38b
+		  name, localname));
76e38b
+
76e38b
 	*err_p = 0;
76e38b
 	return pw;
76e38b
 
76e38b
@@ -107,6 +142,56 @@ err:
76e38b
 	return NULL;
76e38b
 }
76e38b
 
76e38b
+static struct group *static_getgrnam(const char *name, const char *domain,
76e38b
+				      int *err_p)
76e38b
+{
76e38b
+	struct group *gr;
76e38b
+	struct grbuf *buf;
76e38b
+	size_t buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
76e38b
+	char *localgroup;
76e38b
+	int err;
76e38b
+
76e38b
+	buf = malloc(sizeof(*buf) + buflen);
76e38b
+	if (!buf) {
76e38b
+		err = ENOMEM;
76e38b
+		goto err;
76e38b
+	}
76e38b
+
76e38b
+	localgroup = conf_get_str("Static", (char *)name);
76e38b
+	if (!localgroup) {
76e38b
+		err = ENOENT;
76e38b
+		goto err_free_buf;
76e38b
+	}
76e38b
+
76e38b
+again:
76e38b
+	err = getgrnam_r(localgroup, &buf->grbuf, buf->buf, buflen, &gr);
76e38b
+
76e38b
+	if (err == EINTR)
76e38b
+		goto again;
76e38b
+
76e38b
+	if (!gr) {
76e38b
+		if (err == 0)
76e38b
+			err = ENOENT;
76e38b
+
76e38b
+		IDMAP_LOG(0, ("static_getgrnam: local group '%s' for '%s' not found\n",
76e38b
+			  localgroup, name));
76e38b
+
76e38b
+		goto err_free_buf;
76e38b
+	}
76e38b
+
76e38b
+	IDMAP_LOG(4, ("static_getgrnam: group '%s' mapped to '%s'\n",
76e38b
+		  name, localgroup));
76e38b
+
76e38b
+	*err_p = 0;
76e38b
+	return gr;
76e38b
+
76e38b
+err_free_buf:
76e38b
+	free(buf);
76e38b
+err:
76e38b
+	*err_p = err;
76e38b
+	return NULL;
76e38b
+}
76e38b
+
76e38b
 static int static_gss_princ_to_ids(char *secname, char *princ,
76e38b
 				   uid_t *uid, uid_t *gid,
76e38b
 				   extra_mapping_params **ex)
76e38b
@@ -151,14 +236,173 @@ static int static_gss_princ_to_grouplist(char *secname, char *princ,
76e38b
 	return -err;
76e38b
 }
76e38b
 
76e38b
+static int static_name_to_uid(char *name, uid_t *uid)
76e38b
+{
76e38b
+	struct passwd *pw;
76e38b
+	int err;
76e38b
+
76e38b
+	pw = static_getpwnam(name, NULL, &err;;
76e38b
+
76e38b
+	if (pw) {
76e38b
+		*uid = pw->pw_uid;
76e38b
+		free(pw);
76e38b
+	}
76e38b
+
76e38b
+	return -err;
76e38b
+}
76e38b
+
76e38b
+static int static_name_to_gid(char *name, gid_t *gid)
76e38b
+{
76e38b
+	struct group *gr;
76e38b
+	int err;
76e38b
+
76e38b
+	gr = static_getgrnam(name, NULL, &err;;
76e38b
+
76e38b
+	if (gr) {
76e38b
+		*gid = gr->gr_gid;
76e38b
+		free(gr);
76e38b
+	}
76e38b
+
76e38b
+	return -err;
76e38b
+}
76e38b
+
76e38b
+static int static_uid_to_name(uid_t uid, char *domain, char *name, size_t len)
76e38b
+{
76e38b
+	struct passwd *pw;
76e38b
+	struct uid_mapping * um;
76e38b
+
76e38b
+	for (um = LIST_FIRST (&uid_mappings[uid_hash (uid)]); um;
76e38b
+		um = LIST_NEXT (um, link)) {
76e38b
+		if (um->uid == uid) {
76e38b
+			strcpy(name, um->principal);
76e38b
+			return 0;
76e38b
+		}
76e38b
+	}
76e38b
+
76e38b
+	return -ENOENT;
76e38b
+}
76e38b
+
76e38b
+static int static_gid_to_name(gid_t gid, char *domain, char *name, size_t len)
76e38b
+{
76e38b
+	struct group *gr;
76e38b
+	struct gid_mapping * gm;
76e38b
+
76e38b
+	for (gm = LIST_FIRST (&gid_mappings[gid_hash (gid)]); gm;
76e38b
+		gm = LIST_NEXT (gm, link)) {
76e38b
+		if (gm->gid == gid) {
76e38b
+			strcpy(name, gm->principal);
76e38b
+			return 0;
76e38b
+		}
76e38b
+	}
76e38b
+
76e38b
+	return -ENOENT;
76e38b
+}
76e38b
+
76e38b
+/*
76e38b
+ * We buffer all UID's for which static mappings is defined in advance, so the
76e38b
+ * uid_to_name functions will be fast enough.
76e38b
+ */
76e38b
+
76e38b
+static int static_init() {	
76e38b
+	int err;
76e38b
+	uid_t uid;
76e38b
+	struct conf_list * princ_list = NULL;
76e38b
+	struct conf_list_node * cln, *next;
76e38b
+	struct uid_mapping * unode;
76e38b
+	struct gid_mapping * gnode;
76e38b
+	struct passwd * pw = NULL;
76e38b
+	struct group * gr = NULL;
76e38b
+	unsigned int i;
76e38b
+
76e38b
+	//init hash_table first
76e38b
+	for (i = 0; i < sizeof uid_mappings / sizeof uid_mappings[0]; i++)
76e38b
+		LIST_INIT (&uid_mappings[i]);
76e38b
+
76e38b
+	//get all principals for which we have mappings
76e38b
+	princ_list = conf_get_tag_list("Static");
76e38b
+
76e38b
+	if (!princ_list) {
76e38b
+		return -ENOENT;
76e38b
+	}
76e38b
+
76e38b
+	/* As we can not distinguish between mappings for users and groups, we try to
76e38b
+	 * resolve all mappings for both cases.
76e38b
+	 */
76e38b
+
76e38b
+	//resolve uid of localname account for all such principals and cache it
76e38b
+	for (cln = TAILQ_FIRST (&princ_list->fields); cln; cln = next) 
76e38b
+	{ 
76e38b
+		next = TAILQ_NEXT (cln, link); 
76e38b
+
76e38b
+		pw = static_getpwnam(cln->field, NULL, &err;;
76e38b
+		if (!pw) {
76e38b
+			continue;
76e38b
+		}
76e38b
+		
76e38b
+		unode = calloc (1, sizeof *unode);
76e38b
+		if (!unode)
76e38b
+		{
76e38b
+			warnx("static_init: calloc (1, %lu) failed",
76e38b
+				(unsigned long)sizeof *unode);
76e38b
+			free(pw);
76e38b
+			return -ENOMEM;
76e38b
+		}
76e38b
+		unode->uid = pw->pw_uid;
76e38b
+		unode->principal = strdup(cln->field);
76e38b
+
76e38b
+		unode->localname = conf_get_str("Static", cln->field);
76e38b
+		if (!unode->localname) {
76e38b
+			free(pw);
76e38b
+			return -ENOENT;
76e38b
+		}
76e38b
+
76e38b
+		free(pw);
76e38b
+
76e38b
+		LIST_INSERT_HEAD (&uid_mappings[uid_hash(unode->uid)], unode, link);
76e38b
+	}
76e38b
+
76e38b
+	//resolve gid of localgroup accounts and cache it
76e38b
+	for (cln = TAILQ_FIRST (&princ_list->fields); cln; cln = next) 
76e38b
+	{ 
76e38b
+		next = TAILQ_NEXT (cln, link); 
76e38b
+
76e38b
+		gr = static_getgrnam(cln->field, NULL, &err;;
76e38b
+		if (!pw) {
76e38b
+			continue;
76e38b
+		}
76e38b
+		
76e38b
+		gnode = calloc (1, sizeof *gnode);
76e38b
+		if (!gnode)
76e38b
+		{
76e38b
+			warnx("static_init: calloc (1, %lu) failed",
76e38b
+				(unsigned long)sizeof *gnode);
76e38b
+			free(pw);
76e38b
+			return -ENOMEM;
76e38b
+		}
76e38b
+		gnode->gid = pw->pw_uid;
76e38b
+		gnode->principal = strdup(cln->field);
76e38b
+
76e38b
+		gnode->localgroup = conf_get_str("Static", cln->field);
76e38b
+		if (!gnode->localgroup) {
76e38b
+			free(pw);
76e38b
+			return -ENOENT;
76e38b
+		}
76e38b
+
76e38b
+		free(pw);
76e38b
+
76e38b
+		LIST_INSERT_HEAD (&gid_mappings[gid_hash(gnode->gid)], gnode, link);
76e38b
+	}
76e38b
+	return 0;
76e38b
+}
76e38b
+
76e38b
 
76e38b
 struct trans_func static_trans = {
76e38b
 	.name			= "static",
76e38b
-	.init			= NULL,
76e38b
-	.name_to_uid		= NULL,
76e38b
-	.name_to_gid		= NULL,
76e38b
-	.uid_to_name		= NULL,
76e38b
-	.gid_to_name		= NULL,
76e38b
+	.init			= static_init,
76e38b
+	.name_to_uid		= static_name_to_uid,
76e38b
+	.name_to_gid		= static_name_to_gid,
76e38b
+	.uid_to_name		= static_uid_to_name,
76e38b
+	.gid_to_name		= static_gid_to_name,
76e38b
 	.princ_to_ids		= static_gss_princ_to_ids,
76e38b
 	.gss_princ_to_grouplist	= static_gss_princ_to_grouplist,
76e38b
 };
76e38b
diff --git a/umich_ldap.c b/umich_ldap.c
76e38b
index f482b0a..b527c5d 100644
76e38b
--- a/umich_ldap.c
76e38b
+++ b/umich_ldap.c
76e38b
@@ -32,8 +32,6 @@
76e38b
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
76e38b
  */
76e38b
 
76e38b
-#ifdef ENABLE_LDAP
76e38b
-
76e38b
 #include <sys/types.h>
76e38b
 #include <sys/socket.h>
76e38b
 #include <netdb.h>
76e38b
@@ -1302,4 +1300,3 @@ struct trans_func *libnfsidmap_plugin_init()
76e38b
 {
76e38b
 	return (&umichldap_trans);
76e38b
 }
76e38b
-#endif