9991ea
From e5bd495a9a7d71f5dde5661d960e372bb2609965 Mon Sep 17 00:00:00 2001
9991ea
From: Jason Woods <devel@jasonwoods.me.uk>
9991ea
Date: Fri, 7 Mar 2014 16:38:24 +0000
9991ea
Subject: [PATCH 58/58] ipa-sam: cache gid to sid and uid to sid requests in
9991ea
 idmap cache
9991ea
9991ea
Add idmap_cache calls to ipa-sam to prevent huge numbers of LDAP calls to the
9991ea
directory service for gid/uid<->sid resolution.
9991ea
9991ea
Additionally, this patch further reduces number of queries by:
9991ea
 - fast fail on uidNumber=0 which doesn't exist in FreeIPA,
9991ea
 - return fallback group correctly when looking up user primary group as is
9991ea
   done during init,
9991ea
 - checking for group objectclass in case insensitive way
9991ea
9991ea
Patch by Jason Woods <devel@jasonwoods.me.uk>
9991ea
9991ea
Reviewed-by: Alexander Bokovoy <abokovoy@redhat.com>
9991ea
9991ea
https://fedorahosted.org/freeipa/ticket/4234
9991ea
and
9991ea
https://bugzilla.redhat.com/show_bug.cgi?id=1073829
9991ea
https://bugzilla.redhat.com/show_bug.cgi?id=1074314
9991ea
9991ea
Reviewed-By: Sumit Bose <sbose@redhat.com>
9991ea
---
9991ea
 daemons/ipa-sam/ipa_sam.c | 128 +++++++++++++++++++++++++++++++++++++++++-----
9991ea
 1 file changed, 114 insertions(+), 14 deletions(-)
9991ea
9991ea
diff --git a/daemons/ipa-sam/ipa_sam.c b/daemons/ipa-sam/ipa_sam.c
9991ea
index 1ca504db4e442c834ebe44d7e3503abafd6f9602..3b69f9e8255490aac17d08033e2a3584c6bf9e24 100644
9991ea
--- a/daemons/ipa-sam/ipa_sam.c
9991ea
+++ b/daemons/ipa-sam/ipa_sam.c
9991ea
@@ -82,6 +82,18 @@ struct trustAuthInOutBlob {
9991ea
 	struct AuthenticationInformationArray previous;/* [subcontext(0),flag(LIBNDR_FLAG_REMAINING)] */
9991ea
 }/* [gensize,public,nopush] */;
9991ea
 
9991ea
+/* from generated idmap.h - hopefully OK */
9991ea
+enum id_type {
9991ea
+	ID_TYPE_NOT_SPECIFIED,
9991ea
+	ID_TYPE_UID,
9991ea
+	ID_TYPE_GID,
9991ea
+	ID_TYPE_BOTH
9991ea
+};
9991ea
+
9991ea
+struct unixid {
9991ea
+	uint32_t id;
9991ea
+	enum id_type type;
9991ea
+}/* [public] */;
9991ea
 
9991ea
 enum ndr_err_code ndr_pull_trustAuthInOutBlob(struct ndr_pull *ndr, int ndr_flags, struct trustAuthInOutBlob *r); /*available in libndr-samba.so */
9991ea
 bool sid_check_is_builtin(const struct dom_sid *sid); /* available in libpdb.so */
9991ea
@@ -91,6 +103,7 @@ char *sid_string_talloc(TALLOC_CTX *mem_ctx, const struct dom_sid *sid); /* avai
9991ea
 char *sid_string_dbg(const struct dom_sid *sid); /* available in libsmbconf.so */
9991ea
 char *escape_ldap_string(TALLOC_CTX *mem_ctx, const char *s); /* available in libsmbconf.so */
9991ea
 bool secrets_store(const char *key, const void *data, size_t size); /* available in libpdb.so */
9991ea
+void idmap_cache_set_sid2unixid(const struct dom_sid *sid, struct unixid *unix_id); /* available in libsmbconf.so */
9991ea
 
9991ea
 #define LDAP_PAGE_SIZE 1024
9991ea
 #define LDAP_OBJ_SAMBASAMACCOUNT "ipaNTUserAttrs"
9991ea
@@ -750,8 +763,8 @@ static bool ldapsam_sid_to_id(struct pdb_methods *methods,
9991ea
 	}
9991ea
 
9991ea
 	for (c = 0; values[c] != NULL; c++) {
9991ea
-		if (strncmp(LDAP_OBJ_GROUPMAP, values[c]->bv_val,
9991ea
-			                       values[c]->bv_len) == 0) {
9991ea
+		if (strncasecmp(LDAP_OBJ_GROUPMAP, values[c]->bv_val,
9991ea
+						   values[c]->bv_len) == 0) {
9991ea
 			break;
9991ea
 		}
9991ea
 	}
9991ea
@@ -769,6 +782,9 @@ static bool ldapsam_sid_to_id(struct pdb_methods *methods,
9991ea
 		}
9991ea
 
9991ea
 		unixid_from_gid(id, strtoul(gid_str, NULL, 10));
9991ea
+
9991ea
+		idmap_cache_set_sid2unixid(sid, id);
9991ea
+
9991ea
 		ret = true;
9991ea
 		goto done;
9991ea
 	}
9991ea
@@ -785,8 +801,11 @@ static bool ldapsam_sid_to_id(struct pdb_methods *methods,
9991ea
 
9991ea
 	unixid_from_uid(id, strtoul(value, NULL, 10));
9991ea
 
9991ea
+	idmap_cache_set_sid2unixid(sid, id);
9991ea
+
9991ea
 	ret = true;
9991ea
  done:
9991ea
+
9991ea
 	TALLOC_FREE(mem_ctx);
9991ea
 	return ret;
9991ea
 }
9991ea
@@ -806,6 +825,18 @@ static bool ldapsam_uid_to_sid(struct pdb_methods *methods, uid_t uid,
9991ea
 	int rc;
9991ea
 	enum idmap_error_code err;
9991ea
 	TALLOC_CTX *tmp_ctx = talloc_stackframe();
9991ea
+	struct unixid id;
9991ea
+
9991ea
+	/* Fast fail if we get a request for uidNumber=0 because it currently
9991ea
+	 * will never exist in the directory
9991ea
+	 * Saves an expensive LDAP call of which failure will never be cached
9991ea
+	 */
9991ea
+	if (uid == 0) {
9991ea
+		DEBUG(3, ("ERROR: Received request for uid %u, "
9991ea
+			  "fast failing as it will never exist\n",
9991ea
+			  (unsigned int)uid));
9991ea
+		goto done;
9991ea
+	}
9991ea
 
9991ea
 	filter = talloc_asprintf(tmp_ctx,
9991ea
 				 "(&(uidNumber=%u)"
9991ea
@@ -852,6 +883,10 @@ static bool ldapsam_uid_to_sid(struct pdb_methods *methods, uid_t uid,
9991ea
 
9991ea
 	sid_copy(sid, user_sid);
9991ea
 
9991ea
+	unixid_from_uid(&id, uid);
9991ea
+
9991ea
+	idmap_cache_set_sid2unixid(sid, &id;;
9991ea
+
9991ea
 	ret = true;
9991ea
 
9991ea
 done:
9991ea
@@ -866,21 +901,30 @@ static bool ldapsam_gid_to_sid(struct pdb_methods *methods, gid_t gid,
9991ea
 	struct ldapsam_privates *priv =
9991ea
 		(struct ldapsam_privates *)methods->private_data;
9991ea
 	char *filter;
9991ea
-	const char *attrs[] = { LDAP_ATTRIBUTE_SID, NULL };
9991ea
+	const char *attrs[] = { LDAP_ATTRIBUTE_SID, LDAP_ATTRIBUTE_OBJECTCLASS, NULL };
9991ea
 	LDAPMessage *result = NULL;
9991ea
 	LDAPMessage *entry = NULL;
9991ea
 	bool ret = false;
9991ea
-	char *group_sid_string;
9991ea
+	char *group_sid_string = NULL;
9991ea
 	struct dom_sid *group_sid = NULL;
9991ea
+	struct berval **values;
9991ea
+	size_t c;
9991ea
 	int rc;
9991ea
 	enum idmap_error_code err;
9991ea
 	TALLOC_CTX *tmp_ctx = talloc_stackframe();
9991ea
+	struct unixid id;
9991ea
 
9991ea
 	filter = talloc_asprintf(tmp_ctx,
9991ea
-				 "(&(gidNumber=%u)"
9991ea
-				 "(objectClass=%s))",
9991ea
+				 "(|(&(gidNumber=%u)"
9991ea
+				     "(objectClass=%s))"
9991ea
+				   "(&(uidNumber=%u)"
9991ea
+				     "(objectClass=%s)"
9991ea
+				     "(objectClass=%s)))",
9991ea
 				 (unsigned int)gid,
9991ea
-				 LDAP_OBJ_GROUPMAP);
9991ea
+				 LDAP_OBJ_GROUPMAP,
9991ea
+				 (unsigned int)gid,
9991ea
+				 LDAP_OBJ_POSIXACCOUNT,
9991ea
+				 LDAP_OBJ_SAMBASAMACCOUNT);
9991ea
 	if (filter == NULL) {
9991ea
 		DEBUG(3, ("talloc_asprintf failed\n"));
9991ea
 		goto done;
9991ea
@@ -892,14 +936,46 @@ static bool ldapsam_gid_to_sid(struct pdb_methods *methods, gid_t gid,
9991ea
 	}
9991ea
 	smbldap_talloc_autofree_ldapmsg(tmp_ctx, result);
9991ea
 
9991ea
-	if (ldap_count_entries(priv2ld(priv), result) != 1) {
9991ea
-		DEBUG(3, ("ERROR: Got %d entries for gid %u, expected one\n",
9991ea
+	if (ldap_count_entries(priv2ld(priv), result) == 0) {
9991ea
+		DEBUG(3, ("ERROR: Got %d entries for gid %u, expected at least one\n",
9991ea
 			   ldap_count_entries(priv2ld(priv), result),
9991ea
 			   (unsigned int)gid));
9991ea
 		goto done;
9991ea
 	}
9991ea
 
9991ea
-	entry = ldap_first_entry(priv2ld(priv), result);
9991ea
+	for (entry = ldap_first_entry(priv2ld(priv), result);
9991ea
+		 entry != NULL;
9991ea
+		 entry = ldap_next_entry(priv2ld(priv), entry)) {
9991ea
+
9991ea
+		values = ldap_get_values_len(priv2ld(priv), entry, "objectClass");
9991ea
+		if (values == NULL) {
9991ea
+			DEBUG(10, ("Cannot find any objectclasses.\n"));
9991ea
+			goto done;
9991ea
+		}
9991ea
+
9991ea
+		for (c = 0; values[c] != NULL; c++) {
9991ea
+			if (strncasecmp(LDAP_OBJ_GROUPMAP, values[c]->bv_val,
9991ea
+							   values[c]->bv_len) == 0) {
9991ea
+				goto found;
9991ea
+			}
9991ea
+		}
9991ea
+
9991ea
+	}
9991ea
+
9991ea
+found:
9991ea
+	/* If we didn't find a group we found a user - so this is a primary group
9991ea
+	 * For user private group, use fallback group */
9991ea
+	if (entry == NULL) {
9991ea
+
9991ea
+		DEBUG(10, ("Did not find user private group %u, "
9991ea
+			   "returning fallback group.\n", (unsigned int)gid));
9991ea
+
9991ea
+		sid_copy(sid,
9991ea
+			 &priv->ipasam_privates->fallback_primary_group);
9991ea
+		ret = true;
9991ea
+		goto done;
9991ea
+
9991ea
+	}
9991ea
 
9991ea
 	group_sid_string = get_single_attribute(tmp_ctx, priv2ld(priv), entry,
9991ea
 						LDAP_ATTRIBUTE_SID);
9991ea
@@ -910,7 +986,7 @@ static bool ldapsam_gid_to_sid(struct pdb_methods *methods, gid_t gid,
9991ea
 	}
9991ea
 
9991ea
 	err = sss_idmap_sid_to_smb_sid(priv->ipasam_privates->idmap_ctx,
9991ea
-				       group_sid_string, &group_sid);
9991ea
+					   group_sid_string, &group_sid);
9991ea
 	if (err != IDMAP_SUCCESS) {
9991ea
 		DEBUG(3, ("Error calling sid_string_talloc for sid '%s'\n",
9991ea
 			  group_sid_string));
9991ea
@@ -919,6 +995,10 @@ static bool ldapsam_gid_to_sid(struct pdb_methods *methods, gid_t gid,
9991ea
 
9991ea
 	sid_copy(sid, group_sid);
9991ea
 
9991ea
+	unixid_from_gid(&id, gid);
9991ea
+
9991ea
+	idmap_cache_set_sid2unixid(sid, &id;;
9991ea
+
9991ea
 	ret = true;
9991ea
 
9991ea
 done:
9991ea
@@ -2456,10 +2536,16 @@ static int delete_subtree(struct ldapsam_privates *ldap_state, char* dn)
9991ea
 	rc = smbldap_search(ldap_state->smbldap_state, dn, scope, filter, NULL, 0, &result);
9991ea
 	TALLOC_FREE(filter);
9991ea
 
9991ea
-	if (result != NULL) {
9991ea
-		smbldap_talloc_autofree_ldapmsg(dn, result);
9991ea
+	if (rc != LDAP_SUCCESS) {
9991ea
+		return rc;
9991ea
 	}
9991ea
 
9991ea
+	if (result == NULL) {
9991ea
+		return LDAP_NO_MEMORY;
9991ea
+	}
9991ea
+
9991ea
+	smbldap_talloc_autofree_ldapmsg(dn, result);
9991ea
+
9991ea
 	for (entry = ldap_first_entry(state, result);
9991ea
 	     entry != NULL;
9991ea
 	     entry = ldap_next_entry(state, entry)) {
9991ea
@@ -2467,6 +2553,9 @@ static int delete_subtree(struct ldapsam_privates *ldap_state, char* dn)
9991ea
 		/* remove child entries */
9991ea
 		if ((entry_dn != NULL) && (strcmp(entry_dn, dn) != 0)) {
9991ea
 			rc = smbldap_delete(ldap_state->smbldap_state, entry_dn);
9991ea
+			if (rc != LDAP_SUCCESS) {
9991ea
+				return rc;
9991ea
+			}
9991ea
 		}
9991ea
 	}
9991ea
 	rc = smbldap_delete(ldap_state->smbldap_state, dn);
9991ea
@@ -2856,6 +2945,7 @@ static int ipasam_get_sid_by_gid(struct ldapsam_privates *ldap_state,
9991ea
 	struct dom_sid *sid = NULL;
9991ea
 	int count;
9991ea
 	enum idmap_error_code err;
9991ea
+	struct unixid id;
9991ea
 
9991ea
 	tmp_ctx = talloc_new("ipasam_get_sid_by_gid");
9991ea
 	if (tmp_ctx == NULL) {
9991ea
@@ -2910,6 +3000,10 @@ static int ipasam_get_sid_by_gid(struct ldapsam_privates *ldap_state,
9991ea
 	}
9991ea
 	sid_copy(_sid, sid);
9991ea
 
9991ea
+	unixid_from_gid(&id, gid);
9991ea
+
9991ea
+	idmap_cache_set_sid2unixid(sid, &id;;
9991ea
+
9991ea
 	ret = 0;
9991ea
 
9991ea
 done:
9991ea
@@ -2929,6 +3023,7 @@ static int ipasam_get_primary_group_sid(TALLOC_CTX *mem_ctx,
9991ea
 	uint32_t uid;
9991ea
 	uint32_t gid;
9991ea
 	struct dom_sid *group_sid;
9991ea
+	struct unixid id;
9991ea
 
9991ea
 	TALLOC_CTX *tmp_ctx = talloc_init("ipasam_get_primary_group_sid");
9991ea
 	if (tmp_ctx == NULL) {
9991ea
@@ -2967,8 +3062,13 @@ static int ipasam_get_primary_group_sid(TALLOC_CTX *mem_ctx,
9991ea
 		}
9991ea
 	}
9991ea
 
9991ea
-        ret = 0;
9991ea
+	unixid_from_gid(&id, gid);
9991ea
+
9991ea
+	idmap_cache_set_sid2unixid(group_sid, &id;;
9991ea
+
9991ea
+	ret = 0;
9991ea
 done:
9991ea
+
9991ea
 	if (ret == 0) {
9991ea
 		*_group_sid = talloc_steal(mem_ctx, group_sid);
9991ea
 	}
9991ea
-- 
9991ea
1.8.5.3
9991ea