Blame SOURCES/libnfsidmap-0.25-nobody.patch

76e38b
commit f139de79d91e7773b5b98fe5aca5570f77c7aee9
76e38b
Author: Christian Seiler <christian@iwakd.de>
76e38b
Date:   Wed Aug 13 12:42:14 2014 -0400
76e38b
76e38b
    libnfsidmap: respect Nobody-User/Nobody-Group
76e38b
    
76e38b
    Previous behavior of libnfsidmap was to do a name lookup of
76e38b
    nobody@DEFAULTDOMAIN (for both user and group), which does not match
76e38b
    the behavior of rpc.idmapd.
76e38b
    
76e38b
    This patch makes libnfsidmap respect Nobody-User/Nobody-Group for
76e38b
    lookups, thus making the nfsidmap utility properly handle the case if
76e38b
    nobody@DEFAULTDOMAIN does not directly map to any user/group on the
76e38b
    system.
76e38b
    
76e38b
    Signed-off-by: Christian Seiler <christian@iwakd.de>
76e38b
    Signed-off-by: Steve Dickson <steved@redhat.com>
76e38b
76e38b
diff --git a/libnfsidmap.c b/libnfsidmap.c
76e38b
index 92bc493..ec5c141 100644
76e38b
--- a/libnfsidmap.c
76e38b
+++ b/libnfsidmap.c
76e38b
@@ -62,6 +62,8 @@ static struct conf_list *local_realms;
76e38b
 int idmap_verbosity = 0;
76e38b
 static struct mapping_plugin **nfs4_plugins = NULL;
76e38b
 static struct mapping_plugin **gss_plugins = NULL;
76e38b
+uid_t nobody_uid = (uid_t)-1;
76e38b
+gid_t nobody_gid = (gid_t)-1;
76e38b
 
76e38b
 #ifndef PATH_PLUGINS
76e38b
 #define PATH_PLUGINS "/usr/lib/libnfsidmap"
76e38b
@@ -228,6 +230,7 @@ int nfs4_init_name_mapping(char *conffile)
76e38b
 	int ret = -ENOENT;
76e38b
 	int dflt = 0;
76e38b
 	struct conf_list *nfs4_methods, *gss_methods;
76e38b
+	char *nobody_user, *nobody_group;
76e38b
 
76e38b
 	/* XXX: need to be able to reload configurations... */
76e38b
 	if (nfs4_plugins) /* already succesfully initialized */
76e38b
@@ -324,6 +327,49 @@ int nfs4_init_name_mapping(char *conffile)
76e38b
 		if (load_plugins(gss_methods, &gss_plugins) == -1)
76e38b
 			goto out;
76e38b
 	}
76e38b
+
76e38b
+	nobody_user = conf_get_str("Mapping", "Nobody-User");
76e38b
+	if (nobody_user) {
76e38b
+		size_t buflen = sysconf(_SC_GETPW_R_SIZE_MAX);
76e38b
+		struct passwd *buf;
76e38b
+		struct passwd *pw = NULL;
76e38b
+		int err;
76e38b
+
76e38b
+		buf = malloc(sizeof(*buf) + buflen);
76e38b
+		if (buf) {
76e38b
+			err = getpwnam_r(nobody_user, buf, ((char *)buf) + sizeof(*buf), buflen, &pw;;
76e38b
+			if (err == 0 && pw != NULL)
76e38b
+				nobody_uid = pw->pw_uid;
76e38b
+			else
76e38b
+				IDMAP_LOG(1, ("libnfsidmap: Nobody-User (%s) not found: %s\n", 
76e38b
+					nobody_user, strerror(errno)));
76e38b
+			free(buf);
76e38b
+		} else
76e38b
+			IDMAP_LOG(0,("libnfsidmap: Nobody-User: no memory : %s\n", 
76e38b
+					nobody_user, strerror(errno)));
76e38b
+	}
76e38b
+
76e38b
+	nobody_group = conf_get_str("Mapping", "Nobody-Group");
76e38b
+	if (nobody_group) {
76e38b
+		size_t buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
76e38b
+		struct group *buf;
76e38b
+		struct group *gr = NULL;
76e38b
+		int err;
76e38b
+
76e38b
+		buf = malloc(sizeof(*buf) + buflen);
76e38b
+		if (buf) {
76e38b
+			err = getgrnam_r(nobody_group, buf, ((char *)buf) + sizeof(*buf), buflen, &gr);
76e38b
+			if (err == 0 && gr != NULL)
76e38b
+				nobody_gid = gr->gr_gid;
76e38b
+			else
76e38b
+				IDMAP_LOG(1, ("libnfsidmap: Nobody-Group (%s) not found: %s\n", 
76e38b
+					nobody_group, strerror(errno)));
76e38b
+			free(buf);
76e38b
+		} else
76e38b
+			IDMAP_LOG(0,("libnfsidmap: Nobody-Group: no memory : %s\n", 
76e38b
+					nobody_group, strerror(errno)));
76e38b
+	}
76e38b
+
76e38b
 	ret = 0;
76e38b
 out:
76e38b
 	if (ret) {
76e38b
@@ -453,6 +499,18 @@ static int set_id_to_nobody(int *id, int is_uid)
76e38b
 	int rc = 0;
76e38b
 	const char name[] = "nobody@";
76e38b
 	char nobody[strlen(name) + strlen(get_default_domain()) + 1];
76e38b
+
76e38b
+	/* First try to see whether a Nobody-User/Nobody-Group was
76e38b
+         * configured, before we try to do a full lookup for the
76e38b
+         * NFS nobody user. */
76e38b
+	if (is_uid && nobody_uid != (uid_t)-1) {
76e38b
+		*id = (int)nobody_uid;
76e38b
+		return 0;
76e38b
+	} else if (!is_uid && nobody_gid != (gid_t)-1) {
76e38b
+		*id = (int)nobody_gid;
76e38b
+		return 0;
76e38b
+	}
76e38b
+
76e38b
 	strcpy(nobody, name);
76e38b
 	strcat(nobody, get_default_domain());
76e38b