937546
From 9cb4436694d2fa5f7a56fa774e5283f0b46cc18f Mon Sep 17 00:00:00 2001
937546
From: Alexander Bokovoy <abokovoy@redhat.com>
937546
Date: Sun, 31 Mar 2019 12:37:21 +0300
937546
Subject: [PATCH 2/2] ipasam: use SID formatting calls to libsss_idmap
937546
937546
Samba 4.10 moved away to private libraries two functions we used to
937546
convert a binary SID structre to strings:
937546
 - sid_talloc_string()
937546
 - sid_string_dbg()
937546
937546
We already used libsss_idmap to convert textual representation of SIDs
937546
to a binary one, use the reverse function too.
937546
937546
libsss_idmap code operates on talloc structures, so we need to adopt a
937546
bit a place where sid_string_dbg() was used because it assumed a static
937546
buffer was provided by sid_string_dbg().
937546
937546
Finally, sid_talloc_string()'s replacement moves allocated memory to the
937546
right context so that a memory will be freed earlier. Our SSSD idmap
937546
context is a long-living one while in all cases where we were using
937546
sid_talloc_string() we free the context much earlier.
937546
937546
Resolves: https://pagure.io/freeipa/issue/7893
937546
Reviewed-By: Christian Heimes <cheimes@redhat.com>
937546
(cherry picked from commit 137af1d2c38925404dc92f70321ac0f5fb1cf5eb)
937546
---
937546
 daemons/ipa-sam/ipa_sam.c | 52 ++++++++++++++++++++++++++++-----------
937546
 1 file changed, 37 insertions(+), 15 deletions(-)
937546
937546
diff --git a/daemons/ipa-sam/ipa_sam.c b/daemons/ipa-sam/ipa_sam.c
937546
index 2f78f82f9..851cbc39a 100644
937546
--- a/daemons/ipa-sam/ipa_sam.c
937546
+++ b/daemons/ipa-sam/ipa_sam.c
937546
@@ -104,8 +104,6 @@ enum ndr_err_code ndr_pull_trustAuthInOutBlob(struct ndr_pull *ndr, int ndr_flag
937546
 bool sid_check_is_builtin(const struct dom_sid *sid); /* available in libpdb.so */
937546
 /* available in libpdb.so, renamed from sid_check_is_domain() in c43505b621725c9a754f0ee98318d451b093f2ed */
937546
 bool sid_linearize(char *outbuf, size_t len, const struct dom_sid *sid); /* available in libsmbconf.so */
937546
-char *sid_string_talloc(TALLOC_CTX *mem_ctx, const struct dom_sid *sid); /* available in libsmbconf.so */
937546
-char *sid_string_dbg(const struct dom_sid *sid); /* available in libsmbconf.so */
937546
 char *escape_ldap_string(TALLOC_CTX *mem_ctx, const char *s); /* available in libsmbconf.so */
937546
 bool secrets_store(const char *key, const void *data, size_t size); /* available in libpdb.so */
937546
 void idmap_cache_set_sid2unixid(const struct dom_sid *sid, struct unixid *unix_id); /* available in libsmbconf.so */
937546
@@ -261,6 +259,18 @@ static bool sid_compose(struct dom_sid *dst, const struct dom_sid *dom_sid,
937546
 	return true;
937546
 }
937546
 
937546
+static char *sid_talloc_string(struct sss_idmap_ctx *ctx, void *final_ctx, const struct dom_sid *dom_sid)
937546
+{
937546
+	enum idmap_error_code ret;
937546
+	char *result = NULL;
937546
+	ret = sss_idmap_smb_sid_to_sid(ctx, discard_const(dom_sid), &result);
937546
+	if (ret != IDMAP_SUCCESS) {
937546
+		return NULL;
937546
+	}
937546
+
937546
+	return talloc_move(final_ctx, &result);
937546
+}
937546
+
937546
 static bool is_null_sid(const struct dom_sid *sid)
937546
 {
937546
 	size_t c;
937546
@@ -519,8 +529,18 @@ static bool ldapsam_extract_rid_from_entry(LDAP *ldap_struct,
937546
 	}
937546
 
937546
 	if (dom_sid_compare_domain(sid, domain_sid) != 0) {
937546
-		DEBUG(10, ("SID %s is not in expected domain %s\n",
937546
-			   str, sid_string_dbg(domain_sid)));
937546
+		char *debug_domain_sid = NULL;
937546
+		err = sss_idmap_smb_sid_to_sid(idmap_ctx,
937546
+					       discard_const(domain_sid),
937546
+					       &debug_domain_sid);
937546
+		if (err != IDMAP_SUCCESS) {
937546
+			DEBUG(10, ("SID %s is not in expected domain.\n",
937546
+				   str));
937546
+		} else {
937546
+			DEBUG(10, ("SID %s is not in expected domain %s\n",
937546
+				   str, debug_domain_sid));
937546
+			talloc_free(debug_domain_sid);
937546
+		}
937546
 		res = false;
937546
 		goto done;
937546
 	}
937546
@@ -589,7 +609,7 @@ static NTSTATUS ldapsam_lookup_rids(struct pdb_methods *methods,
937546
 		allsids = talloc_asprintf_append_buffer(
937546
 			allsids, "(%s=%s)",
937546
 			LDAP_ATTRIBUTE_SID,
937546
-			sid_string_talloc(mem_ctx, &sid));
937546
+			sid_talloc_string(ipasam_state->idmap_ctx, mem_ctx, &sid));
937546
 		if (allsids == NULL) {
937546
 			goto done;
937546
 		}
937546
@@ -790,7 +810,8 @@ static bool ldapsam_sid_to_id(struct pdb_methods *methods,
937546
 	filter = talloc_asprintf(mem_ctx,
937546
 				 "(&(%s=%s)"
937546
 				 "(|(objectClass=%s)(objectClass=%s)))",
937546
-				 LDAP_ATTRIBUTE_SID, sid_string_talloc(mem_ctx, sid),
937546
+				 LDAP_ATTRIBUTE_SID,
937546
+				 sid_talloc_string(priv->idmap_ctx, mem_ctx, sid),
937546
 				 LDAP_OBJ_GROUPMAP, LDAP_OBJ_SAMBASAMACCOUNT);
937546
 	if (filter == NULL) {
937546
 		DEBUG(5, ("talloc_asprintf failed\n"));
937546
@@ -936,7 +957,7 @@ static bool ipasam_uid_to_sid(struct pdb_methods *methods, uid_t uid,
937546
 	err = sss_idmap_sid_to_smb_sid(priv->idmap_ctx,
937546
 				       user_sid_string, &user_sid);
937546
 	if (err != IDMAP_SUCCESS) {
937546
-		DEBUG(3, ("Error calling sid_string_talloc for sid '%s'\n",
937546
+		DEBUG(3, ("Error creating sid structure for sid '%s'\n",
937546
 			  user_sid_string));
937546
 		goto done;
937546
 	}
937546
@@ -1052,7 +1073,7 @@ found:
937546
 	err = sss_idmap_sid_to_smb_sid(priv->idmap_ctx,
937546
 				       group_sid_string, &group_sid);
937546
 	if (err != IDMAP_SUCCESS) {
937546
-		DEBUG(3, ("Error calling sid_string_talloc for sid '%s'\n",
937546
+		DEBUG(3, ("Error creating sid structure for sid '%s'\n",
937546
 			  group_sid_string));
937546
 		goto done;
937546
 	}
937546
@@ -1595,11 +1616,11 @@ static bool ipasam_search_grouptype(struct pdb_methods *methods,
937546
 	state->base = talloc_strdup(search, ipasam_state->base_dn);
937546
 	state->connection = ipasam_state->ldap_state;
937546
 	state->scope = LDAP_SCOPE_SUBTREE;
937546
-	state->filter =	talloc_asprintf(search, "(&(objectclass=%s)"
937546
-					"(%s=%s*))",
937546
-					 LDAP_OBJ_GROUPMAP,
937546
-					 LDAP_ATTRIBUTE_SID,
937546
-					 sid_string_talloc(search, sid));
937546
+	state->filter =	talloc_asprintf(search, "(&(objectclass=%s)(%s=%s*))",
937546
+					LDAP_OBJ_GROUPMAP, LDAP_ATTRIBUTE_SID,
937546
+					sid_talloc_string(
937546
+						ipasam_state->idmap_ctx,
937546
+						search, sid));
937546
 	state->attrs = talloc_attrs(search, "cn", LDAP_ATTRIBUTE_SID,
937546
 				    "displayName", "description",
937546
 				     NULL);
937546
@@ -2412,7 +2433,7 @@ static NTSTATUS ipasam_get_trusted_domain_by_sid(struct pdb_methods *methods,
937546
 	char *sid_str;
937546
 	bool ok;
937546
 
937546
-	sid_str = sid_string_talloc(mem_ctx, sid);
937546
+	sid_str = sid_talloc_string(ipasam_state->idmap_ctx, mem_ctx, sid);
937546
 	if (sid_str == NULL) {
937546
 		return NT_STATUS_NO_MEMORY;
937546
 	}
937546
@@ -2593,7 +2614,8 @@ static NTSTATUS ipasam_set_trusted_domain(struct pdb_methods *methods,
937546
 	if (!is_null_sid(&td->security_identifier)) {
937546
 		smbldap_make_mod(priv2ld(ipasam_state), entry, &mods,
937546
 				 LDAP_ATTRIBUTE_TRUST_SID,
937546
-				 sid_string_talloc(tmp_ctx, &td->security_identifier));
937546
+				 sid_talloc_string(ipasam_state->idmap_ctx,
937546
+						   tmp_ctx, &td->security_identifier));
937546
 	}
937546
 
937546
 	if (td->trust_type != 0) {
937546
-- 
937546
2.21.0
937546