Blame SOURCES/libnfsidmap-0.26-rc3.patch

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