868320
From 05f7e9a72a1769af9d41b1ca40fe6a14b3f069d1 Mon Sep 17 00:00:00 2001
868320
From: Isaac Boukris <iboukris@gmail.com>
868320
Date: Fri, 30 Aug 2019 00:22:15 +0300
868320
Subject: [PATCH 1/6] libnet_join: build dnsHostName from netbios name and
868320
 lp_dnsdomain()
868320
868320
This make the join process much more reliable, and avoids "Constraint
868320
violation" error when the fqdn returned from getaddrinfo has already
868320
got assigned an SPN.
868320
868320
BUG: https://bugzilla.samba.org/show_bug.cgi?id=14116
868320
868320
Signed-off-by: Isaac Boukris <iboukris@redhat.com>
868320
Reviewed-by: Ralph Boehme <slow@samba.org>
868320
Reviewed-by: Alexander Bokovoy <ab@samba.org>
868320
---
868320
 source3/libnet/libnet_join.c       | 31 +++++++++++-------------------
868320
 testprogs/blackbox/test_net_ads.sh |  7 +++++--
868320
 2 files changed, 16 insertions(+), 22 deletions(-)
868320
868320
diff --git a/source3/libnet/libnet_join.c b/source3/libnet/libnet_join.c
868320
index 7943bef2cf6..818b3039cb9 100644
868320
--- a/source3/libnet/libnet_join.c
868320
+++ b/source3/libnet/libnet_join.c
868320
@@ -533,29 +533,23 @@ static ADS_STATUS libnet_join_set_machine_spn(TALLOC_CTX *mem_ctx,
868320
 		}
868320
 	}
868320
 
868320
-	if (!name_to_fqdn(my_fqdn, r->in.machine_name)
868320
-	    || (strchr(my_fqdn, '.') == NULL)) {
868320
-		fstr_sprintf(my_fqdn, "%s.%s", r->in.machine_name,
868320
-			     r->out.dns_domain_name);
868320
-	}
868320
+	fstr_sprintf(my_fqdn, "%s.%s", r->in.machine_name, lp_dnsdomain());
868320
 
868320
 	if (!strlower_m(my_fqdn)) {
868320
 		return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
868320
 	}
868320
 
868320
-	if (!strequal(my_fqdn, r->in.machine_name)) {
868320
-		spn = talloc_asprintf(mem_ctx, "HOST/%s", my_fqdn);
868320
-		if (!spn) {
868320
-			return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
868320
-		}
868320
+	spn = talloc_asprintf(mem_ctx, "HOST/%s", my_fqdn);
868320
+	if (spn == NULL) {
868320
+		return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
868320
+	}
868320
 
868320
-		ok = ads_element_in_array(spn_array, num_spns, spn);
868320
+	ok = ads_element_in_array(spn_array, num_spns, spn);
868320
+	if (!ok) {
868320
+		ok = add_string_to_array(spn_array, spn,
868320
+					 &spn_array, &num_spns);
868320
 		if (!ok) {
868320
-			ok = add_string_to_array(spn_array, spn,
868320
-						 &spn_array, &num_spns);
868320
-			if (!ok) {
868320
-				return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
868320
-			}
868320
+			return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
868320
 		}
868320
 	}
868320
 
868320
@@ -591,12 +585,9 @@ static ADS_STATUS libnet_join_set_machine_spn(TALLOC_CTX *mem_ctx,
868320
 			/*
868320
 			 * Add HOST/netbiosname.domainname
868320
 			 */
868320
-			if (r->out.dns_domain_name == NULL) {
868320
-				continue;
868320
-			}
868320
 			fstr_sprintf(my_fqdn, "%s.%s",
868320
 				     *netbios_aliases,
868320
-				     r->out.dns_domain_name);
868320
+				     lp_dnsdomain());
868320
 
868320
 			spn = talloc_asprintf(mem_ctx, "HOST/%s", my_fqdn);
868320
 			if (spn == NULL) {
868320
diff --git a/testprogs/blackbox/test_net_ads.sh b/testprogs/blackbox/test_net_ads.sh
868320
index cc8345c4624..ef6f99ddea4 100755
868320
--- a/testprogs/blackbox/test_net_ads.sh
868320
+++ b/testprogs/blackbox/test_net_ads.sh
868320
@@ -81,7 +81,7 @@ testit "testjoin (dedicated keytab)" $VALGRIND $net_tool ads testjoin -kP || fai
868320
 netbios=$(grep "netbios name" $BASEDIR/$WORKDIR/client.conf | cut -f2 -d= | awk '{$1=$1};1')
868320
 uc_netbios=$(echo $netbios | tr '[:lower:]' '[:upper:]')
868320
 lc_realm=$(echo $REALM | tr '[:upper:]' '[:lower:]')
868320
-fqdns="$netbios.$lc_realm"
868320
+fqdn="$netbios.$lc_realm"
868320
 
868320
 krb_princ="primary/instance@$REALM"
868320
 testit "test (dedicated keytab) add a fully qualified krb5 principal" $VALGRIND $net_tool ads keytab add $krb_princ -U$DC_USERNAME%$DC_PASSWORD --option="kerberosmethod=dedicatedkeytab" --option="dedicatedkeytabfile=$dedicated_keytab_file" || failed=`expr $failed + 1`
868320
@@ -99,7 +99,7 @@ testit "test (dedicated keytab) at least one krb5 principal created from $machin
868320
 service="nfs"
868320
 testit "test (dedicated keytab) add a $service service to keytab" $VALGRIND $net_tool ads keytab add $service -U$DC_USERNAME%$DC_PASSWORD --option="kerberosmethod=dedicatedkeytab" --option="dedicatedkeytabfile=$dedicated_keytab_file" || failed=`expr $failed + 1`
868320
 
868320
-search_str="$service/$fqdns@$REALM"
868320
+search_str="$service/$fqdn@$REALM"
868320
 found=`$net_tool ads keytab list -U$DC_USERNAME%$DC_PASSWORD --option="kerberosmethod=dedicatedkeytab" --option="dedicatedkeytabfile=$dedicated_keytab_file" | grep $search_str | wc -l`
868320
 testit "test (dedicated keytab) at least one (long form) krb5 principal created from service added is present in keytab" test $found -gt 1 || failed=`expr $failed + 1`
868320
 
868320
@@ -206,6 +206,9 @@ testit "join" $VALGRIND $net_tool ads join -U$DC_USERNAME%$DC_PASSWORD || failed
868320
 
868320
 testit "testjoin" $VALGRIND $net_tool ads testjoin || failed=`expr $failed + 1`
868320
 
868320
+testit_grep "check dNSHostName" $fqdn $VALGRIND $net_tool ads search -P samaccountname=$netbios\$ dNSHostName || failed=`expr $failed + 1`
868320
+testit_grep "check SPN" ${uc_netbios}.${lc_realm} $VALGRIND $net_tool ads search -P samaccountname=$netbios\$ servicePrincipalName || failed=`expr $failed + 1`
868320
+
868320
 ##Goodbye...
868320
 testit "leave" $VALGRIND $net_tool ads leave -U$DC_USERNAME%$DC_PASSWORD || failed=`expr $failed + 1`
868320
 
868320
-- 
868320
2.21.0
868320
868320
868320
From 4cbad1eb46896bbd74c5b19dbb0a8937ffde90c2 Mon Sep 17 00:00:00 2001
868320
From: Isaac Boukris <iboukris@gmail.com>
868320
Date: Wed, 18 Sep 2019 20:00:34 +0300
868320
Subject: [PATCH 2/6] libnet_join_set_machine_spn: improve style and make a bit
868320
 room for indentation
868320
868320
BUG: https://bugzilla.samba.org/show_bug.cgi?id=14116
868320
868320
Signed-off-by: Isaac Boukris <iboukris@redhat.com>
868320
Reviewed-by: Ralph Boehme <slow@samba.org>
868320
Reviewed-by: Alexander Bokovoy <ab@samba.org>
868320
---
868320
 source3/libnet/libnet_join.c | 95 ++++++++++++++++++------------------
868320
 1 file changed, 47 insertions(+), 48 deletions(-)
868320
868320
diff --git a/source3/libnet/libnet_join.c b/source3/libnet/libnet_join.c
868320
index 818b3039cb9..67ab50c68a8 100644
868320
--- a/source3/libnet/libnet_join.c
868320
+++ b/source3/libnet/libnet_join.c
868320
@@ -517,7 +517,7 @@ static ADS_STATUS libnet_join_set_machine_spn(TALLOC_CTX *mem_ctx,
868320
 	/* Windows only creates HOST/shortname & HOST/fqdn. */
868320
 
868320
 	spn = talloc_asprintf(mem_ctx, "HOST/%s", r->in.machine_name);
868320
-	if (!spn) {
868320
+	if (spn == NULL) {
868320
 		return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
868320
 	}
868320
 	if (!strupper_m(spn)) {
868320
@@ -553,60 +553,59 @@ static ADS_STATUS libnet_join_set_machine_spn(TALLOC_CTX *mem_ctx,
868320
 		}
868320
 	}
868320
 
868320
-	netbios_aliases = lp_netbios_aliases();
868320
-	if (netbios_aliases != NULL) {
868320
-		for (; *netbios_aliases != NULL; netbios_aliases++) {
868320
-			/*
868320
-			 * Add HOST/NETBIOSNAME
868320
-			 */
868320
-			spn = talloc_asprintf(mem_ctx, "HOST/%s", *netbios_aliases);
868320
-			if (spn == NULL) {
868320
-				TALLOC_FREE(spn);
868320
-				return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
868320
-			}
868320
-			if (!strupper_m(spn)) {
868320
-				TALLOC_FREE(spn);
868320
-				return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
868320
-			}
868320
+	for (netbios_aliases = lp_netbios_aliases();
868320
+	     netbios_aliases != NULL && *netbios_aliases != NULL;
868320
+	     netbios_aliases++) {
868320
+		/*
868320
+		 * Add HOST/NETBIOSNAME
868320
+		 */
868320
+		spn = talloc_asprintf(mem_ctx, "HOST/%s", *netbios_aliases);
868320
+		if (spn == NULL) {
868320
+			TALLOC_FREE(spn);
868320
+			return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
868320
+		}
868320
+		if (!strupper_m(spn)) {
868320
+			TALLOC_FREE(spn);
868320
+			return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
868320
+		}
868320
 
868320
-			ok = ads_element_in_array(spn_array, num_spns, spn);
868320
-			if (ok) {
868320
-				TALLOC_FREE(spn);
868320
-				continue;
868320
-			}
868320
-			ok = add_string_to_array(spn_array, spn,
868320
-						 &spn_array, &num_spns);
868320
-			if (!ok) {
868320
-				TALLOC_FREE(spn);
868320
-				return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
868320
-			}
868320
+		ok = ads_element_in_array(spn_array, num_spns, spn);
868320
+		if (ok) {
868320
+			TALLOC_FREE(spn);
868320
+			continue;
868320
+		}
868320
+		ok = add_string_to_array(spn_array, spn,
868320
+					 &spn_array, &num_spns);
868320
+		if (!ok) {
868320
 			TALLOC_FREE(spn);
868320
+			return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
868320
+		}
868320
+		TALLOC_FREE(spn);
868320
 
868320
-			/*
868320
-			 * Add HOST/netbiosname.domainname
868320
-			 */
868320
-			fstr_sprintf(my_fqdn, "%s.%s",
868320
-				     *netbios_aliases,
868320
-				     lp_dnsdomain());
868320
+		/*
868320
+		 * Add HOST/netbiosname.domainname
868320
+		 */
868320
+		fstr_sprintf(my_fqdn, "%s.%s",
868320
+			     *netbios_aliases,
868320
+			     lp_dnsdomain());
868320
 
868320
-			spn = talloc_asprintf(mem_ctx, "HOST/%s", my_fqdn);
868320
-			if (spn == NULL) {
868320
-				return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
868320
-			}
868320
+		spn = talloc_asprintf(mem_ctx, "HOST/%s", my_fqdn);
868320
+		if (spn == NULL) {
868320
+			return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
868320
+		}
868320
 
868320
-			ok = ads_element_in_array(spn_array, num_spns, spn);
868320
-			if (ok) {
868320
-				TALLOC_FREE(spn);
868320
-				continue;
868320
-			}
868320
-			ok = add_string_to_array(spn_array, spn,
868320
-						 &spn_array, &num_spns);
868320
-			if (!ok) {
868320
-				TALLOC_FREE(spn);
868320
-				return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
868320
-			}
868320
+		ok = ads_element_in_array(spn_array, num_spns, spn);
868320
+		if (ok) {
868320
+			TALLOC_FREE(spn);
868320
+			continue;
868320
+		}
868320
+		ok = add_string_to_array(spn_array, spn,
868320
+					 &spn_array, &num_spns);
868320
+		if (!ok) {
868320
 			TALLOC_FREE(spn);
868320
+			return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
868320
 		}
868320
+		TALLOC_FREE(spn);
868320
 	}
868320
 
868320
 	/* make sure to NULL terminate the array */
868320
-- 
868320
2.21.0
868320
868320
868320
From b8e1264ececf38681ca9a519a51e8336044673f0 Mon Sep 17 00:00:00 2001
868320
From: Isaac Boukris <iboukris@gmail.com>
868320
Date: Wed, 18 Sep 2019 21:29:47 +0300
868320
Subject: [PATCH 3/6] libnet_join_set_machine_spn: simplify memory handling
868320
868320
and avoid a possible memory leak when passing null to
868320
add_string_to_array() as mem_ctx.
868320
868320
BUG: https://bugzilla.samba.org/show_bug.cgi?id=14116
868320
868320
Signed-off-by: Isaac Boukris <iboukris@redhat.com>
868320
Reviewed-by: Ralph Boehme <slow@samba.org>
868320
Reviewed-by: Alexander Bokovoy <ab@samba.org>
868320
---
868320
 source3/libnet/libnet_join.c | 74 ++++++++++++++++++++----------------
868320
 1 file changed, 42 insertions(+), 32 deletions(-)
868320
868320
diff --git a/source3/libnet/libnet_join.c b/source3/libnet/libnet_join.c
868320
index 67ab50c68a8..43035370526 100644
868320
--- a/source3/libnet/libnet_join.c
868320
+++ b/source3/libnet/libnet_join.c
868320
@@ -490,6 +490,7 @@ static ADS_STATUS libnet_join_get_machine_spns(TALLOC_CTX *mem_ctx,
868320
 static ADS_STATUS libnet_join_set_machine_spn(TALLOC_CTX *mem_ctx,
868320
 					      struct libnet_JoinCtx *r)
868320
 {
868320
+	TALLOC_CTX *frame = talloc_stackframe();
868320
 	ADS_STATUS status;
868320
 	ADS_MODLIST mods;
868320
 	fstring my_fqdn;
868320
@@ -506,7 +507,7 @@ static ADS_STATUS libnet_join_set_machine_spn(TALLOC_CTX *mem_ctx,
868320
 		return status;
868320
 	}
868320
 
868320
-	status = libnet_join_get_machine_spns(mem_ctx,
868320
+	status = libnet_join_get_machine_spns(frame,
868320
 					      r,
868320
 					      discard_const_p(char **, &spn_array),
868320
 					      &num_spns);
868320
@@ -516,40 +517,46 @@ static ADS_STATUS libnet_join_set_machine_spn(TALLOC_CTX *mem_ctx,
868320
 
868320
 	/* Windows only creates HOST/shortname & HOST/fqdn. */
868320
 
868320
-	spn = talloc_asprintf(mem_ctx, "HOST/%s", r->in.machine_name);
868320
+	spn = talloc_asprintf(frame, "HOST/%s", r->in.machine_name);
868320
 	if (spn == NULL) {
868320
-		return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
868320
+		status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
868320
+		goto done;
868320
 	}
868320
 	if (!strupper_m(spn)) {
868320
-		return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
868320
+		status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
868320
+		goto done;
868320
 	}
868320
 
868320
 	ok = ads_element_in_array(spn_array, num_spns, spn);
868320
 	if (!ok) {
868320
-		ok = add_string_to_array(spn_array, spn,
868320
+		ok = add_string_to_array(frame, spn,
868320
 					 &spn_array, &num_spns);
868320
 		if (!ok) {
868320
-			return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
868320
+			status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
868320
+			goto done;
868320
 		}
868320
 	}
868320
 
868320
 	fstr_sprintf(my_fqdn, "%s.%s", r->in.machine_name, lp_dnsdomain());
868320
 
868320
 	if (!strlower_m(my_fqdn)) {
868320
-		return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
868320
+		status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
868320
+		goto done;
868320
 	}
868320
 
868320
-	spn = talloc_asprintf(mem_ctx, "HOST/%s", my_fqdn);
868320
+	spn = talloc_asprintf(frame, "HOST/%s", my_fqdn);
868320
 	if (spn == NULL) {
868320
-		return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
868320
+		status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
868320
+		goto done;
868320
 	}
868320
 
868320
 	ok = ads_element_in_array(spn_array, num_spns, spn);
868320
 	if (!ok) {
868320
-		ok = add_string_to_array(spn_array, spn,
868320
+		ok = add_string_to_array(frame, spn,
868320
 					 &spn_array, &num_spns);
868320
 		if (!ok) {
868320
-			return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
868320
+			status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
868320
+			goto done;
868320
 		}
868320
 	}
868320
 
868320
@@ -559,28 +566,26 @@ static ADS_STATUS libnet_join_set_machine_spn(TALLOC_CTX *mem_ctx,
868320
 		/*
868320
 		 * Add HOST/NETBIOSNAME
868320
 		 */
868320
-		spn = talloc_asprintf(mem_ctx, "HOST/%s", *netbios_aliases);
868320
+		spn = talloc_asprintf(frame, "HOST/%s", *netbios_aliases);
868320
 		if (spn == NULL) {
868320
-			TALLOC_FREE(spn);
868320
-			return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
868320
+			status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
868320
+			goto done;
868320
 		}
868320
 		if (!strupper_m(spn)) {
868320
-			TALLOC_FREE(spn);
868320
-			return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
868320
+			status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
868320
+			goto done;
868320
 		}
868320
 
868320
 		ok = ads_element_in_array(spn_array, num_spns, spn);
868320
 		if (ok) {
868320
-			TALLOC_FREE(spn);
868320
 			continue;
868320
 		}
868320
 		ok = add_string_to_array(spn_array, spn,
868320
 					 &spn_array, &num_spns);
868320
 		if (!ok) {
868320
-			TALLOC_FREE(spn);
868320
-			return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
868320
+			status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
868320
+			goto done;
868320
 		}
868320
-		TALLOC_FREE(spn);
868320
 
868320
 		/*
868320
 		 * Add HOST/netbiosname.domainname
868320
@@ -589,51 +594,56 @@ static ADS_STATUS libnet_join_set_machine_spn(TALLOC_CTX *mem_ctx,
868320
 			     *netbios_aliases,
868320
 			     lp_dnsdomain());
868320
 
868320
-		spn = talloc_asprintf(mem_ctx, "HOST/%s", my_fqdn);
868320
+		spn = talloc_asprintf(frame, "HOST/%s", my_fqdn);
868320
 		if (spn == NULL) {
868320
-			return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
868320
+			status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
868320
+			goto done;
868320
 		}
868320
 
868320
 		ok = ads_element_in_array(spn_array, num_spns, spn);
868320
 		if (ok) {
868320
-			TALLOC_FREE(spn);
868320
 			continue;
868320
 		}
868320
 		ok = add_string_to_array(spn_array, spn,
868320
 					 &spn_array, &num_spns);
868320
 		if (!ok) {
868320
-			TALLOC_FREE(spn);
868320
-			return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
868320
+			status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
868320
+			goto done;
868320
 		}
868320
-		TALLOC_FREE(spn);
868320
 	}
868320
 
868320
 	/* make sure to NULL terminate the array */
868320
-	spn_array = talloc_realloc(mem_ctx, spn_array, const char *, num_spns + 1);
868320
+	spn_array = talloc_realloc(frame, spn_array, const char *, num_spns + 1);
868320
 	if (spn_array == NULL) {
868320
-		return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
868320
+		status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
868320
+		goto done;
868320
 	}
868320
 	spn_array[num_spns] = NULL;
868320
 
868320
 	mods = ads_init_mods(mem_ctx);
868320
 	if (!mods) {
868320
-		return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
868320
+		status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
868320
+		goto done;
868320
 	}
868320
 
868320
 	/* fields of primary importance */
868320
 
868320
 	status = ads_mod_str(mem_ctx, &mods, "dNSHostName", my_fqdn);
868320
 	if (!ADS_ERR_OK(status)) {
868320
-		return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
868320
+		goto done;
868320
 	}
868320
 
868320
 	status = ads_mod_strlist(mem_ctx, &mods, "servicePrincipalName",
868320
 				 spn_array);
868320
 	if (!ADS_ERR_OK(status)) {
868320
-		return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
868320
+		goto done;
868320
 	}
868320
 
868320
-	return ads_gen_mod(r->in.ads, r->out.dn, mods);
868320
+	status = ads_gen_mod(r->in.ads, r->out.dn, mods);
868320
+
868320
+done:
868320
+	TALLOC_FREE(frame);
868320
+	return status;
868320
 }
868320
 
868320
 /****************************************************************
868320
-- 
868320
2.21.0
868320
868320
868320
From 3e65f72b141a7ee256ae581e5f48f1d930aed76a Mon Sep 17 00:00:00 2001
868320
From: Isaac Boukris <iboukris@gmail.com>
868320
Date: Wed, 18 Sep 2019 23:15:57 +0300
868320
Subject: [PATCH 4/6] libnet_join_set_machine_spn: simplify adding uniq spn to
868320
 array
868320
868320
and do not skip adding a fully qualified spn to netbios-aliases
868320
in case a short spn already existed.
868320
868320
BUG: https://bugzilla.samba.org/show_bug.cgi?id=14116
868320
868320
Signed-off-by: Isaac Boukris <iboukris@redhat.com>
868320
Reviewed-by: Ralph Boehme <slow@samba.org>
868320
Reviewed-by: Alexander Bokovoy <ab@samba.org>
868320
---
868320
 source3/libnet/libnet_join.c | 56 +++++++++++++++---------------------
868320
 1 file changed, 23 insertions(+), 33 deletions(-)
868320
868320
diff --git a/source3/libnet/libnet_join.c b/source3/libnet/libnet_join.c
868320
index 43035370526..a1d8a25bbc2 100644
868320
--- a/source3/libnet/libnet_join.c
868320
+++ b/source3/libnet/libnet_join.c
868320
@@ -483,6 +483,19 @@ static ADS_STATUS libnet_join_get_machine_spns(TALLOC_CTX *mem_ctx,
868320
 	return status;
868320
 }
868320
 
868320
+static ADS_STATUS add_uniq_spn(TALLOC_CTX *mem_ctx, const  char *spn,
868320
+			       const char ***array, size_t *num)
868320
+{
868320
+	bool ok = ads_element_in_array(*array, *num, spn);
868320
+	if (!ok) {
868320
+		ok = add_string_to_array(mem_ctx, spn, array, num);
868320
+		if (!ok) {
868320
+			return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
868320
+		}
868320
+	}
868320
+	return ADS_SUCCESS;
868320
+}
868320
+
868320
 /****************************************************************
868320
  Set a machines dNSHostName and servicePrincipalName attributes
868320
 ****************************************************************/
868320
@@ -497,7 +510,6 @@ static ADS_STATUS libnet_join_set_machine_spn(TALLOC_CTX *mem_ctx,
868320
 	const char **spn_array = NULL;
868320
 	size_t num_spns = 0;
868320
 	char *spn = NULL;
868320
-	bool ok;
868320
 	const char **netbios_aliases = NULL;
868320
 
868320
 	/* Find our DN */
868320
@@ -527,14 +539,9 @@ static ADS_STATUS libnet_join_set_machine_spn(TALLOC_CTX *mem_ctx,
868320
 		goto done;
868320
 	}
868320
 
868320
-	ok = ads_element_in_array(spn_array, num_spns, spn);
868320
-	if (!ok) {
868320
-		ok = add_string_to_array(frame, spn,
868320
-					 &spn_array, &num_spns);
868320
-		if (!ok) {
868320
-			status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
868320
-			goto done;
868320
-		}
868320
+	status = add_uniq_spn(frame, spn, &spn_array, &num_spns);
868320
+	if (!ADS_ERR_OK(status)) {
868320
+		goto done;
868320
 	}
868320
 
868320
 	fstr_sprintf(my_fqdn, "%s.%s", r->in.machine_name, lp_dnsdomain());
868320
@@ -550,14 +557,9 @@ static ADS_STATUS libnet_join_set_machine_spn(TALLOC_CTX *mem_ctx,
868320
 		goto done;
868320
 	}
868320
 
868320
-	ok = ads_element_in_array(spn_array, num_spns, spn);
868320
-	if (!ok) {
868320
-		ok = add_string_to_array(frame, spn,
868320
-					 &spn_array, &num_spns);
868320
-		if (!ok) {
868320
-			status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
868320
-			goto done;
868320
-		}
868320
+	status = add_uniq_spn(frame, spn, &spn_array, &num_spns);
868320
+	if (!ADS_ERR_OK(status)) {
868320
+		goto done;
868320
 	}
868320
 
868320
 	for (netbios_aliases = lp_netbios_aliases();
868320
@@ -576,14 +578,8 @@ static ADS_STATUS libnet_join_set_machine_spn(TALLOC_CTX *mem_ctx,
868320
 			goto done;
868320
 		}
868320
 
868320
-		ok = ads_element_in_array(spn_array, num_spns, spn);
868320
-		if (ok) {
868320
-			continue;
868320
-		}
868320
-		ok = add_string_to_array(spn_array, spn,
868320
-					 &spn_array, &num_spns);
868320
-		if (!ok) {
868320
-			status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
868320
+		status = add_uniq_spn(frame, spn, &spn_array, &num_spns);
868320
+		if (!ADS_ERR_OK(status)) {
868320
 			goto done;
868320
 		}
868320
 
868320
@@ -600,14 +596,8 @@ static ADS_STATUS libnet_join_set_machine_spn(TALLOC_CTX *mem_ctx,
868320
 			goto done;
868320
 		}
868320
 
868320
-		ok = ads_element_in_array(spn_array, num_spns, spn);
868320
-		if (ok) {
868320
-			continue;
868320
-		}
868320
-		ok = add_string_to_array(spn_array, spn,
868320
-					 &spn_array, &num_spns);
868320
-		if (!ok) {
868320
-			status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
868320
+		status = add_uniq_spn(frame, spn, &spn_array, &num_spns);
868320
+		if (!ADS_ERR_OK(status)) {
868320
 			goto done;
868320
 		}
868320
 	}
868320
-- 
868320
2.21.0
868320
868320
868320
From db7560ff0fb861552406bb4c422cff55c82f58bf Mon Sep 17 00:00:00 2001
868320
From: Isaac Boukris <iboukris@gmail.com>
868320
Date: Tue, 17 Sep 2019 21:38:07 +0300
868320
Subject: [PATCH 5/6] docs-xml: add "additional dns hostnames" smb.conf option
868320
868320
BUG: https://bugzilla.samba.org/show_bug.cgi?id=14116
868320
868320
Signed-off-by: Isaac Boukris <iboukris@redhat.com>
868320
Reviewed-by: Ralph Boehme <slow@samba.org>
868320
Reviewed-by: Alexander Bokovoy <ab@samba.org>
868320
---
868320
 docs-xml/smbdotconf/base/additionaldnshostnames.xml | 11 +++++++++++
868320
 1 file changed, 11 insertions(+)
868320
 create mode 100644 docs-xml/smbdotconf/base/additionaldnshostnames.xml
868320
868320
diff --git a/docs-xml/smbdotconf/base/additionaldnshostnames.xml b/docs-xml/smbdotconf/base/additionaldnshostnames.xml
868320
new file mode 100644
868320
index 00000000000..ddc04ee9f81
868320
--- /dev/null
868320
+++ b/docs-xml/smbdotconf/base/additionaldnshostnames.xml
868320
@@ -0,0 +1,11 @@
868320
+
868320
+                 context="G"
868320
+                 type="cmdlist"
868320
+                 xmlns:samba="http://www.samba.org/samba/DTD/samba-doc">
868320
+ <description>
868320
+        <para> A list of additional DNS names by which this host can be identified
868320
+        </para>
868320
+</description>
868320
+<value type="default"><comment>empty string (no additional dns names)</comment></value>
868320
+<value type="example"> host2.example.com host3.other.com </value>
868320
+</samba:parameter>
868320
-- 
868320
2.21.0
868320
868320
868320
From 2669cecc51f8f7d6675b4dac9b345b3c5a7fc879 Mon Sep 17 00:00:00 2001
868320
From: Isaac Boukris <iboukris@gmail.com>
868320
Date: Fri, 13 Sep 2019 10:56:10 +0300
868320
Subject: [PATCH 6/6] libnet_join: add SPNs for additional-dns-hostnames
868320
 entries
868320
MIME-Version: 1.0
868320
Content-Type: text/plain; charset=UTF-8
868320
Content-Transfer-Encoding: 8bit
868320
868320
and set msDS-AdditionalDnsHostName to the specified list.
868320
868320
BUG: https://bugzilla.samba.org/show_bug.cgi?id=14116
868320
868320
Signed-off-by: Isaac Boukris <iboukris@redhat.com>
868320
Reviewed-by: Ralph Boehme <slow@samba.org>
868320
Reviewed-by: Alexander Bokovoy <ab@samba.org>
868320
868320
Autobuild-User(master): Ralph Böhme <slow@samba.org>
868320
Autobuild-Date(master): Fri Oct 25 10:43:08 UTC 2019 on sn-devel-184
868320
---
868320
 source3/libnet/libnet_join.c       | 27 +++++++++++++++++++++++++++
868320
 testprogs/blackbox/test_net_ads.sh | 10 +++++++++-
868320
 2 files changed, 36 insertions(+), 1 deletion(-)
868320
868320
diff --git a/source3/libnet/libnet_join.c b/source3/libnet/libnet_join.c
868320
index a1d8a25bbc2..eb8e0ea17f7 100644
868320
--- a/source3/libnet/libnet_join.c
868320
+++ b/source3/libnet/libnet_join.c
868320
@@ -511,6 +511,7 @@ static ADS_STATUS libnet_join_set_machine_spn(TALLOC_CTX *mem_ctx,
868320
 	size_t num_spns = 0;
868320
 	char *spn = NULL;
868320
 	const char **netbios_aliases = NULL;
868320
+	const char **addl_hostnames = NULL;
868320
 
868320
 	/* Find our DN */
868320
 
868320
@@ -602,6 +603,22 @@ static ADS_STATUS libnet_join_set_machine_spn(TALLOC_CTX *mem_ctx,
868320
 		}
868320
 	}
868320
 
868320
+	for (addl_hostnames = lp_additional_dns_hostnames();
868320
+	     addl_hostnames != NULL && *addl_hostnames != NULL;
868320
+	     addl_hostnames++) {
868320
+
868320
+		spn = talloc_asprintf(frame, "HOST/%s", *addl_hostnames);
868320
+		if (spn == NULL) {
868320
+			status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
868320
+			goto done;
868320
+		}
868320
+
868320
+		status = add_uniq_spn(frame, spn, &spn_array, &num_spns);
868320
+		if (!ADS_ERR_OK(status)) {
868320
+			goto done;
868320
+		}
868320
+	}
868320
+
868320
 	/* make sure to NULL terminate the array */
868320
 	spn_array = talloc_realloc(frame, spn_array, const char *, num_spns + 1);
868320
 	if (spn_array == NULL) {
868320
@@ -629,6 +646,16 @@ static ADS_STATUS libnet_join_set_machine_spn(TALLOC_CTX *mem_ctx,
868320
 		goto done;
868320
 	}
868320
 
868320
+	addl_hostnames = lp_additional_dns_hostnames();
868320
+	if (addl_hostnames != NULL && *addl_hostnames != NULL) {
868320
+		status = ads_mod_strlist(mem_ctx, &mods,
868320
+					 "msDS-AdditionalDnsHostName",
868320
+					 addl_hostnames);
868320
+		if (!ADS_ERR_OK(status)) {
868320
+			goto done;
868320
+		}
868320
+	}
868320
+
868320
 	status = ads_gen_mod(r->in.ads, r->out.dn, mods);
868320
 
868320
 done:
868320
diff --git a/testprogs/blackbox/test_net_ads.sh b/testprogs/blackbox/test_net_ads.sh
868320
index ef6f99ddea4..8bcff006b8e 100755
868320
--- a/testprogs/blackbox/test_net_ads.sh
868320
+++ b/testprogs/blackbox/test_net_ads.sh
868320
@@ -202,13 +202,21 @@ base_dn="DC=addom,DC=samba,DC=example,DC=com"
868320
 computers_dn="CN=Computers,$base_dn"
868320
 testit "ldb check for existence of machine account" $ldbsearch -U$DC_USERNAME%$DC_PASSWORD -H ldap://$SERVER.$REALM -s base -b "cn=$HOSTNAME,$computers_dn" || failed=`expr $failed + 1`
868320
 
868320
-testit "join" $VALGRIND $net_tool ads join -U$DC_USERNAME%$DC_PASSWORD || failed=`expr $failed + 1`
868320
+dns_alias1="${netbios}_alias1.other.${lc_realm}"
868320
+dns_alias2="${netbios}_alias2.other2.${lc_realm}"
868320
+testit "join" $VALGRIND $net_tool --option=additionaldnshostnames=$dns_alias1,$dns_alias2 ads join -U$DC_USERNAME%$DC_PASSWORD || failed=`expr $failed + 1`
868320
 
868320
 testit "testjoin" $VALGRIND $net_tool ads testjoin || failed=`expr $failed + 1`
868320
 
868320
 testit_grep "check dNSHostName" $fqdn $VALGRIND $net_tool ads search -P samaccountname=$netbios\$ dNSHostName || failed=`expr $failed + 1`
868320
 testit_grep "check SPN" ${uc_netbios}.${lc_realm} $VALGRIND $net_tool ads search -P samaccountname=$netbios\$ servicePrincipalName || failed=`expr $failed + 1`
868320
 
868320
+testit_grep "dns alias SPN" $dns_alias1 $VALGRIND $net_tool ads search -P samaccountname=$netbios\$ servicePrincipalName || failed=`expr $failed + 1`
868320
+testit_grep "dns alias SPN" $dns_alias2 $VALGRIND $net_tool ads search -P samaccountname=$netbios\$ servicePrincipalName || failed=`expr $failed + 1`
868320
+
868320
+testit_grep "dns alias addl" $dns_alias1 $VALGRIND $net_tool ads search -P samaccountname=$netbios\$ msDS-AdditionalDnsHostName || failed=`expr $failed + 1`
868320
+testit_grep "dns alias addl" $dns_alias2 $VALGRIND $net_tool ads search -P samaccountname=$netbios\$ msDS-AdditionalDnsHostName || failed=`expr $failed + 1`
868320
+
868320
 ##Goodbye...
868320
 testit "leave" $VALGRIND $net_tool ads leave -U$DC_USERNAME%$DC_PASSWORD || failed=`expr $failed + 1`
868320
 
868320
-- 
868320
2.21.0
868320