8ff60a
From 9691c65234f2833792977d6e25a314baca724c64 Mon Sep 17 00:00:00 2001
8ff60a
From: =?UTF-8?q?Bj=C3=B6rn=20Baumbach?= <bb@sernet.de>
8ff60a
Date: Mon, 10 Feb 2020 19:19:44 +0100
8ff60a
Subject: [PATCH 1/7] s3-libads: use dns name to open a ldap session
8ff60a
MIME-Version: 1.0
8ff60a
Content-Type: text/plain; charset=UTF-8
8ff60a
Content-Transfer-Encoding: 8bit
8ff60a
8ff60a
Required for working certificate verification.
8ff60a
8ff60a
Bug: https://bugzilla.samba.org/show_bug.cgi?id=13124
8ff60a
Signed-off-by: Björn Baumbach <bb@sernet.de>
8ff60a
Reviewed-by: Bjoern Jacke <bjacke@samba.org>
8ff60a
8ff60a
Autobuild-User(master): Björn Baumbach <bb@sernet.de>
8ff60a
Autobuild-Date(master): Thu Mar  5 12:29:26 UTC 2020 on sn-devel-184
8ff60a
8ff60a
(cherry picked from commit e45e0912d99335f4feec7f937180ea21f7f62a72)
8ff60a
---
8ff60a
 source3/libads/ldap.c | 2 +-
8ff60a
 1 file changed, 1 insertion(+), 1 deletion(-)
8ff60a
8ff60a
diff --git a/source3/libads/ldap.c b/source3/libads/ldap.c
8ff60a
index 7ef7e7e8420..b7f819d876b 100755
8ff60a
--- a/source3/libads/ldap.c
8ff60a
+++ b/source3/libads/ldap.c
8ff60a
@@ -669,7 +669,7 @@ got_connection:
8ff60a
 
8ff60a
 	/* Otherwise setup the TCP LDAP session */
8ff60a
 
8ff60a
-	ads->ldap.ld = ldap_open_with_timeout(addr,
8ff60a
+	ads->ldap.ld = ldap_open_with_timeout(ads->config.ldap_server_name,
8ff60a
 					      &ads->ldap.ss,
8ff60a
 					      ads->ldap.port, lp_ldap_timeout());
8ff60a
 	if (ads->ldap.ld == NULL) {
8ff60a
-- 
8ff60a
2.25.4
8ff60a
8ff60a
8ff60a
From b0cdea726ef5d90c531a49d2bf8b343cdb788719 Mon Sep 17 00:00:00 2001
8ff60a
From: =?UTF-8?q?Bj=C3=B6rn=20Baumbach?= <bb@sernet.de>
8ff60a
Date: Wed, 3 Jun 2020 19:40:59 +0200
8ff60a
Subject: [PATCH 2/7] s3-libads: use ldap_init_fd() to initialize a ldap
8ff60a
 session if possible
8ff60a
MIME-Version: 1.0
8ff60a
Content-Type: text/plain; charset=UTF-8
8ff60a
Content-Transfer-Encoding: 8bit
8ff60a
8ff60a
Use the known ip address of the ldap server to open the connection and
8ff60a
initialize the ldap session with ldap_init_fd().
8ff60a
8ff60a
This avoid unnecessary DNS lookups which might block or prevent the
8ff60a
successful connection.
8ff60a
8ff60a
Bug: https://bugzilla.samba.org/show_bug.cgi?id=13124
8ff60a
8ff60a
Signed-off-by: Björn Baumbach <bb@sernet.de>
8ff60a
Reviewed-by: Stefan Metzmacher <metze@samba.org>
8ff60a
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
8ff60a
(cherry picked from commit c8080bbd708eaa3212fa516861ac9e3b267989a0)
8ff60a
---
8ff60a
 source3/libads/ldap.c | 18 +++++++++++++++++-
8ff60a
 1 file changed, 17 insertions(+), 1 deletion(-)
8ff60a
8ff60a
diff --git a/source3/libads/ldap.c b/source3/libads/ldap.c
8ff60a
index b7f819d876b..36e73440495 100755
8ff60a
--- a/source3/libads/ldap.c
8ff60a
+++ b/source3/libads/ldap.c
8ff60a
@@ -92,7 +92,23 @@ static void gotalarm_sig(int signum)
8ff60a
 		return NULL;
8ff60a
 	}
8ff60a
 
8ff60a
-#ifdef HAVE_LDAP_INITIALIZE
8ff60a
+#ifdef HAVE_LDAP_INIT_FD
8ff60a
+	{
8ff60a
+		int fd = -1;
8ff60a
+		NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
8ff60a
+
8ff60a
+		status = open_socket_out(ss, port, to, &fd;;
8ff60a
+		if (!NT_STATUS_IS_OK(status)) {
8ff60a
+			return NULL;
8ff60a
+		}
8ff60a
+
8ff60a
+/* define LDAP_PROTO_TCP from openldap.h if required */
8ff60a
+#ifndef LDAP_PROTO_TCP
8ff60a
+#define LDAP_PROTO_TCP 1
8ff60a
+#endif
8ff60a
+		ldap_err = ldap_init_fd(fd, LDAP_PROTO_TCP, uri, &ldp;;
8ff60a
+	}
8ff60a
+#elif defined(HAVE_LDAP_INITIALIZE)
8ff60a
 	ldap_err = ldap_initialize(&ldp, uri);
8ff60a
 #else
8ff60a
 	ldp = ldap_open(server, port);
8ff60a
-- 
8ff60a
2.25.4
8ff60a
8ff60a
8ff60a
From 6c5b4317b150d3d2aed77c207dd3cb0039392bd6 Mon Sep 17 00:00:00 2001
8ff60a
From: Isaac Boukris <iboukris@gmail.com>
8ff60a
Date: Mon, 29 Jun 2020 16:55:33 +0300
8ff60a
Subject: [PATCH 3/7] selftest: add tests for net-ads over TLS
8ff60a
8ff60a
BUG: https://bugzilla.samba.org/show_bug.cgi?id=14439
8ff60a
8ff60a
Signed-off-by: Isaac Boukris <iboukris@samba.org>
8ff60a
Reviewed-by: Andreas Schneider <asn@samba.org>
8ff60a
---
8ff60a
 selftest/knownfail.d/net_ads_ntlm_fallback | 10 +++
8ff60a
 selftest/knownfail.d/net_ads_tls           |  1 +
8ff60a
 source4/selftest/tests.py                  |  7 ++
8ff60a
 testprogs/blackbox/test_net_ads_base.sh    | 76 ++++++++++++++++++++++
8ff60a
 4 files changed, 94 insertions(+)
8ff60a
 create mode 100644 selftest/knownfail.d/net_ads_ntlm_fallback
8ff60a
 create mode 100644 selftest/knownfail.d/net_ads_tls
8ff60a
 create mode 100755 testprogs/blackbox/test_net_ads_base.sh
8ff60a
8ff60a
diff --git a/selftest/knownfail.d/net_ads_ntlm_fallback b/selftest/knownfail.d/net_ads_ntlm_fallback
8ff60a
new file mode 100644
8ff60a
index 00000000000..b16a39d134d
8ff60a
--- /dev/null
8ff60a
+++ b/selftest/knownfail.d/net_ads_ntlm_fallback
8ff60a
@@ -0,0 +1,10 @@
8ff60a
+# net-ads commands that fail with: --option=gensec:gse_krb5=no
8ff60a
+^samba4.blackbox.net_ads_base.nomech=gse_krb5.testjoin
8ff60a
+^samba4.blackbox.net_ads_base.nomech=gse_krb5.check dNSHostName
8ff60a
+^samba4.blackbox.net_ads_base.nomech=gse_krb5.check SPN
8ff60a
+^samba4.blackbox.net_ads_base.nomech=gse_krb5.test setspn list
8ff60a
+^samba4.blackbox.net_ads_tls.nomech=gse_krb5.testjoin
8ff60a
+^samba4.blackbox.net_ads_tls.nomech=gse_krb5.check dNSHostName
8ff60a
+^samba4.blackbox.net_ads_tls.nomech=gse_krb5.check ldapssl=off
8ff60a
+^samba4.blackbox.net_ads_tls.nomech=gse_krb5.check SPN
8ff60a
+^samba4.blackbox.net_ads_tls.nomech=gse_krb5.test setspn list
8ff60a
diff --git a/selftest/knownfail.d/net_ads_tls b/selftest/knownfail.d/net_ads_tls
8ff60a
new file mode 100644
8ff60a
index 00000000000..251c948b6a9
8ff60a
--- /dev/null
8ff60a
+++ b/selftest/knownfail.d/net_ads_tls
8ff60a
@@ -0,0 +1 @@
8ff60a
+^samba4.blackbox.net_ads_tls
8ff60a
diff --git a/source4/selftest/tests.py b/source4/selftest/tests.py
8ff60a
index 1d965c751a4..a394afa177f 100755
8ff60a
--- a/source4/selftest/tests.py
8ff60a
+++ b/source4/selftest/tests.py
8ff60a
@@ -511,6 +511,13 @@ plantestsuite("samba4.blackbox.client_etypes_legacy(ad_dc:client)", "ad_dc:clien
8ff60a
 plantestsuite("samba4.blackbox.client_etypes_strong(ad_dc:client)", "ad_dc:client", [os.path.join(bbdir, "test_client_etypes.sh"), '$DC_SERVER', '$DC_USERNAME', '$DC_PASSWORD', '$PREFIX_ABS', 'strong', '17_18'])
8ff60a
 plantestsuite("samba4.blackbox.net_ads_dns(ad_member:local)", "ad_member:local", [os.path.join(bbdir, "test_net_ads_dns.sh"), '$DC_SERVER', '$DC_USERNAME', '$DC_PASSWORD', '$REALM', '$USERNAME', '$PASSWORD'])
8ff60a
 plantestsuite("samba4.blackbox.samba-tool_ntacl(ad_member:local)", "ad_member:local", [os.path.join(bbdir, "test_samba-tool_ntacl.sh"), '$PREFIX', '$DOMSID'])
8ff60a
+
8ff60a
+for nomech in ["none", "gse_krb5", "ntlmssp"]:
8ff60a
+    # we can't test TLS with ad_dc env as it doesn't allow SASL over TLS
8ff60a
+    plantestsuite("samba4.blackbox.net_ads_base.nomech=%s" % nomech, "ad_dc:client", [os.path.join(bbdir, "test_net_ads_base.sh"), '$DC_SERVER', '$DC_USERNAME', '$DC_PASSWORD', 'no', nomech, '$PREFIX_ABS'])
8ff60a
+    plantestsuite("samba4.blackbox.net_ads_tls.nomech=%s" % nomech, "fl2008dc:client", [os.path.join(bbdir, "test_net_ads_base.sh"), '$DC_SERVER', '$DC_USERNAME', '$DC_PASSWORD', 'yes', nomech, '$PREFIX_ABS'])
8ff60a
+    plantestsuite("samba4.blackbox.net_ads_tls.nomech=%s" % nomech, "fl2008r2dc:client", [os.path.join(bbdir, "test_net_ads_base.sh"), '$DC_SERVER', '$DC_USERNAME', '$DC_PASSWORD', 'noverify', nomech, '$PREFIX_ABS'])
8ff60a
+
8ff60a
 plantestsuite_loadlist("samba4.rpc.echo against NetBIOS alias", "ad_dc_ntvfs", [valgrindify(smbtorture4), "$LISTOPT", "$LOADLIST", 'ncacn_np:$NETBIOSALIAS', '-U$DOMAIN/$USERNAME%$PASSWORD', 'rpc.echo'])
8ff60a
 # json tests hook into ``chgdcpass'' to make them run in contributor CI on
8ff60a
 # gitlab
8ff60a
diff --git a/testprogs/blackbox/test_net_ads_base.sh b/testprogs/blackbox/test_net_ads_base.sh
8ff60a
new file mode 100755
8ff60a
index 00000000000..59e3da67a7f
8ff60a
--- /dev/null
8ff60a
+++ b/testprogs/blackbox/test_net_ads_base.sh
8ff60a
@@ -0,0 +1,76 @@
8ff60a
+#!/bin/sh
8ff60a
+
8ff60a
+if [ $# -lt 5 ]; then
8ff60a
+cat <
8ff60a
+Usage: test_net_ads_base.sh DC_SERVER DC_USERNAME DC_PASSWORD TLS_MODE NO_MECH PREFIX_ABS
8ff60a
+EOF
8ff60a
+exit 1;
8ff60a
+fi
8ff60a
+
8ff60a
+DC_SERVER=$1
8ff60a
+DC_USERNAME=$2
8ff60a
+DC_PASSWORD=$3
8ff60a
+TLS_MODE=$4
8ff60a
+NO_MECH=$5
8ff60a
+BASEDIR=$6
8ff60a
+shift 6
8ff60a
+
8ff60a
+HOSTNAME=`dd if=/dev/urandom bs=1 count=32 2>/dev/null | sha1sum | cut -b 1-10`
8ff60a
+HOSTNAME=`echo hn$HOSTNAME | tr '[:lower:]' '[:upper:]'`
8ff60a
+LCHOSTNAME=`echo $HOSTNAME | tr '[:upper:]' '[:lower:]'`
8ff60a
+
8ff60a
+RUNDIR=`pwd`
8ff60a
+cd $BASEDIR
8ff60a
+WORKDIR=`mktemp -d -p .`
8ff60a
+WORKDIR=`basename $WORKDIR`
8ff60a
+cp -a client/* $WORKDIR/
8ff60a
+sed -ri "s@(dir|directory) = (.*)/client/@\1 = \2/$WORKDIR/@" $WORKDIR/client.conf
8ff60a
+sed -ri "s/netbios name = .*/netbios name = $HOSTNAME/" $WORKDIR/client.conf
8ff60a
+sed -ri "s/workgroup = .*/workgroup = $DOMAIN/" $WORKDIR/client.conf
8ff60a
+sed -ri "s/realm = .*/realm = $REALM/" $WORKDIR/client.conf
8ff60a
+rm -f $WORKDIR/private/secrets.tdb
8ff60a
+cd $RUNDIR
8ff60a
+
8ff60a
+failed=0
8ff60a
+
8ff60a
+export LDAPTLS_CACERT=$(grep "tls cafile" $BASEDIR/$WORKDIR/client.conf | cut -f2 -d= | awk '{$1=$1};1')
8ff60a
+
8ff60a
+xoptions=""
8ff60a
+if [ $TLS_MODE != "no" ]; then
8ff60a
+	xoptions="--option=ldapsslads=yes"
8ff60a
+fi
8ff60a
+
8ff60a
+if [ $NO_MECH != "none" ]; then
8ff60a
+	xoptions="$xoptions --option=gensec:$NO_MECH=no"
8ff60a
+fi
8ff60a
+
8ff60a
+if [ $TLS_MODE = "noverify" ]; then
8ff60a
+	export LDAPTLS_REQCERT=allow
8ff60a
+fi
8ff60a
+
8ff60a
+net_tool="$VALGRIND $BINDIR/net -s $BASEDIR/$WORKDIR/client.conf --option=security=ads -k $xoptions"
8ff60a
+
8ff60a
+# Load test functions
8ff60a
+. `dirname $0`/subunit.sh
8ff60a
+
8ff60a
+testit "join" $net_tool ads join -U$DC_USERNAME%$DC_PASSWORD --no-dns-updates || failed=`expr $failed + 1`
8ff60a
+
8ff60a
+testit "testjoin" $net_tool ads testjoin -P || failed=`expr $failed + 1`
8ff60a
+
8ff60a
+testit_grep "check dNSHostName" $LCHOSTNAME $net_tool ads search -P samaccountname=$HOSTNAME\$ dNSHostName || failed=`expr $failed + 1`
8ff60a
+
8ff60a
+tls_log="StartTLS issued: using a TLS connection"
8ff60a
+opt="-d3 --option=ldapssl=off"
8ff60a
+if [ $TLS_MODE != "no" ]; then
8ff60a
+	testit_grep "check ldapssl=off" "$tls_log" $net_tool $opt ads search -P samaccountname=$HOSTNAME\$ dn || failed=`expr $failed + 1`
8ff60a
+fi
8ff60a
+
8ff60a
+testit_grep "check SPN" "HOST/$HOSTNAME" $net_tool ads search -P samaccountname=$HOSTNAME\$ servicePrincipalName || failed=`expr $failed + 1`
8ff60a
+
8ff60a
+testit_grep "test setspn list" "HOST/$HOSTNAME" $net_tool ads setspn list $HOSTNAME -P || failed=`expr $failed + 1`
8ff60a
+
8ff60a
+testit "leave" $net_tool ads leave -U$DC_USERNAME%$DC_PASSWORD || failed=`expr $failed + 1`
8ff60a
+
8ff60a
+rm -rf $BASEDIR/$WORKDIR
8ff60a
+
8ff60a
+exit $failed
8ff60a
-- 
8ff60a
2.25.4
8ff60a
8ff60a
8ff60a
From 94d20b09d565c0f4b0809e1cd778f7082e4733f8 Mon Sep 17 00:00:00 2001
8ff60a
From: Isaac Boukris <iboukris@gmail.com>
8ff60a
Date: Wed, 24 Jun 2020 15:28:45 +0300
8ff60a
Subject: [PATCH 4/7] Decouple ldap-ssl-ads from ldap-ssl option
8ff60a
8ff60a
BUG: https://bugzilla.samba.org/show_bug.cgi?id=14439
8ff60a
8ff60a
Signed-off-by: Isaac Boukris <iboukris@samba.org>
8ff60a
Reviewed-by: Andreas Schneider <asn@samba.org>
8ff60a
---
8ff60a
 WHATSNEW.txt                            |  6 +++++
8ff60a
 docs-xml/smbdotconf/ldap/ldapsslads.xml |  7 ++----
8ff60a
 source3/include/smbldap.h               |  1 +
8ff60a
 source3/lib/ABI/smbldap-2.1.0.sigs      | 33 +++++++++++++++++++++++++
8ff60a
 source3/lib/smbldap.c                   | 19 +++++++++-----
8ff60a
 source3/libads/ldap.c                   |  2 +-
8ff60a
 source3/wscript_build                   |  2 +-
8ff60a
 7 files changed, 57 insertions(+), 13 deletions(-)
8ff60a
 create mode 100644 source3/lib/ABI/smbldap-2.1.0.sigs
8ff60a
8ff60a
diff --git a/WHATSNEW.txt b/WHATSNEW.txt
8ff60a
index a5b554fe11f..8935876d247 100644
8ff60a
--- a/WHATSNEW.txt
8ff60a
+++ b/WHATSNEW.txt
8ff60a
@@ -557,6 +557,12 @@ CTDB changes
8ff60a
   helper exits.  This triggers an election.
8ff60a
 
8ff60a
 
8ff60a
+The "ldap ssl ads" option no longer depends on "ldap ssl" option:
8ff60a
+-----------------------------------------------------------------
8ff60a
+With this release, the "ldap ssl ads" can be set to "yes" even if "ldap ssl"
8ff60a
+is off.
8ff60a
+
8ff60a
+
8ff60a
 REMOVED FEATURES
8ff60a
 ================
8ff60a
 
8ff60a
diff --git a/docs-xml/smbdotconf/ldap/ldapsslads.xml b/docs-xml/smbdotconf/ldap/ldapsslads.xml
8ff60a
index 98c39651f1e..f99afe5bbad 100644
8ff60a
--- a/docs-xml/smbdotconf/ldap/ldapsslads.xml
8ff60a
+++ b/docs-xml/smbdotconf/ldap/ldapsslads.xml
8ff60a
@@ -7,13 +7,10 @@
8ff60a
 	<para>This option is used to define whether or not Samba should
8ff60a
 	use SSL when connecting to the ldap server using
8ff60a
 	<emphasis>ads</emphasis> methods.
8ff60a
-	Rpc methods are not affected by this parameter. Please note, that
8ff60a
-	this parameter won't have any effect if <smbconfoption name="ldap ssl"/>
8ff60a
-	is set to <parameter>no</parameter>.
8ff60a
+	Rpc methods are not affected by this parameter.
8ff60a
 	</para>
8ff60a
 
8ff60a
-	<para>See <refentrytitle>smb.conf</refentrytitle><manvolnum>5</manvolnum>
8ff60a
-	for more information on <smbconfoption name="ldap ssl"/>.
8ff60a
+	<para>See also <smbconfoption name="ldap ssl"/>.
8ff60a
 	</para>
8ff60a
 
8ff60a
 </description>
8ff60a
diff --git a/source3/include/smbldap.h b/source3/include/smbldap.h
8ff60a
index 878268aebd6..d063f44afbc 100644
8ff60a
--- a/source3/include/smbldap.h
8ff60a
+++ b/source3/include/smbldap.h
8ff60a
@@ -72,6 +72,7 @@ int smbldap_modify(struct smbldap_state *ldap_state,
8ff60a
                    const char *dn,
8ff60a
                    LDAPMod *attrs[]);
8ff60a
 int smbldap_start_tls(LDAP *ldap_struct, int version);
8ff60a
+int smbldap_start_tls_start(LDAP *ldap_struct, int version);
8ff60a
 int smbldap_setup_full_conn(LDAP **ldap_struct, const char *uri);
8ff60a
 int smbldap_search(struct smbldap_state *ldap_state,
8ff60a
 		   const char *base, int scope, const char *filter,
8ff60a
diff --git a/source3/lib/ABI/smbldap-2.1.0.sigs b/source3/lib/ABI/smbldap-2.1.0.sigs
8ff60a
new file mode 100644
8ff60a
index 00000000000..67dcc9a8a78
8ff60a
--- /dev/null
8ff60a
+++ b/source3/lib/ABI/smbldap-2.1.0.sigs
8ff60a
@@ -0,0 +1,33 @@
8ff60a
+smbldap_add: int (struct smbldap_state *, const char *, LDAPMod **)
8ff60a
+smbldap_delete: int (struct smbldap_state *, const char *)
8ff60a
+smbldap_extended_operation: int (struct smbldap_state *, const char *, struct berval *, LDAPControl **, LDAPControl **, char **, struct berval **)
8ff60a
+smbldap_free_struct: void (struct smbldap_state **)
8ff60a
+smbldap_get_ldap: LDAP *(struct smbldap_state *)
8ff60a
+smbldap_get_paged_results: bool (struct smbldap_state *)
8ff60a
+smbldap_get_single_attribute: bool (LDAP *, LDAPMessage *, const char *, char *, int)
8ff60a
+smbldap_has_control: bool (LDAP *, const char *)
8ff60a
+smbldap_has_extension: bool (LDAP *, const char *)
8ff60a
+smbldap_has_naming_context: bool (LDAP *, const char *)
8ff60a
+smbldap_init: NTSTATUS (TALLOC_CTX *, struct tevent_context *, const char *, bool, const char *, const char *, struct smbldap_state **)
8ff60a
+smbldap_make_mod: void (LDAP *, LDAPMessage *, LDAPMod ***, const char *, const char *)
8ff60a
+smbldap_make_mod_blob: void (LDAP *, LDAPMessage *, LDAPMod ***, const char *, const DATA_BLOB *)
8ff60a
+smbldap_modify: int (struct smbldap_state *, const char *, LDAPMod **)
8ff60a
+smbldap_pull_sid: bool (LDAP *, LDAPMessage *, const char *, struct dom_sid *)
8ff60a
+smbldap_search: int (struct smbldap_state *, const char *, int, const char *, const char **, int, LDAPMessage **)
8ff60a
+smbldap_search_paged: int (struct smbldap_state *, const char *, int, const char *, const char **, int, int, LDAPMessage **, void **)
8ff60a
+smbldap_search_suffix: int (struct smbldap_state *, const char *, const char **, LDAPMessage **)
8ff60a
+smbldap_set_bind_callback: void (struct smbldap_state *, smbldap_bind_callback_fn, void *)
8ff60a
+smbldap_set_creds: bool (struct smbldap_state *, bool, const char *, const char *)
8ff60a
+smbldap_set_mod: void (LDAPMod ***, int, const char *, const char *)
8ff60a
+smbldap_set_mod_blob: void (LDAPMod ***, int, const char *, const DATA_BLOB *)
8ff60a
+smbldap_set_paged_results: void (struct smbldap_state *, bool)
8ff60a
+smbldap_setup_full_conn: int (LDAP **, const char *)
8ff60a
+smbldap_start_tls: int (LDAP *, int)
8ff60a
+smbldap_start_tls_start: int (LDAP *, int)
8ff60a
+smbldap_talloc_autofree_ldapmod: void (TALLOC_CTX *, LDAPMod **)
8ff60a
+smbldap_talloc_autofree_ldapmsg: void (TALLOC_CTX *, LDAPMessage *)
8ff60a
+smbldap_talloc_dn: char *(TALLOC_CTX *, LDAP *, LDAPMessage *)
8ff60a
+smbldap_talloc_first_attribute: char *(LDAP *, LDAPMessage *, const char *, TALLOC_CTX *)
8ff60a
+smbldap_talloc_single_attribute: char *(LDAP *, LDAPMessage *, const char *, TALLOC_CTX *)
8ff60a
+smbldap_talloc_single_blob: bool (TALLOC_CTX *, LDAP *, LDAPMessage *, const char *, DATA_BLOB *)
8ff60a
+smbldap_talloc_smallest_attribute: char *(LDAP *, LDAPMessage *, const char *, TALLOC_CTX *)
8ff60a
diff --git a/source3/lib/smbldap.c b/source3/lib/smbldap.c
8ff60a
index 34c841f9243..4815dd81fc3 100644
8ff60a
--- a/source3/lib/smbldap.c
8ff60a
+++ b/source3/lib/smbldap.c
8ff60a
@@ -598,20 +598,27 @@ static void smbldap_store_state(LDAP *ld, struct smbldap_state *smbldap_state)
8ff60a
 }
8ff60a
 
8ff60a
 /********************************************************************
8ff60a
- start TLS on an existing LDAP connection
8ff60a
+ start TLS on an existing LDAP connection per config
8ff60a
 *******************************************************************/
8ff60a
 
8ff60a
 int smbldap_start_tls(LDAP *ldap_struct, int version)
8ff60a
-{ 
8ff60a
-#ifdef LDAP_OPT_X_TLS
8ff60a
-	int rc,tls;
8ff60a
-#endif
8ff60a
-
8ff60a
+{
8ff60a
 	if (lp_ldap_ssl() != LDAP_SSL_START_TLS) {
8ff60a
 		return LDAP_SUCCESS;
8ff60a
 	}
8ff60a
 
8ff60a
+	return smbldap_start_tls_start(ldap_struct, version);
8ff60a
+}
8ff60a
+
8ff60a
+/********************************************************************
8ff60a
+ start TLS on an existing LDAP connection unconditionally
8ff60a
+*******************************************************************/
8ff60a
+
8ff60a
+int smbldap_start_tls_start(LDAP *ldap_struct, int version)
8ff60a
+{
8ff60a
 #ifdef LDAP_OPT_X_TLS
8ff60a
+	int rc,tls;
8ff60a
+
8ff60a
 	/* check if we use ldaps already */
8ff60a
 	ldap_get_option(ldap_struct, LDAP_OPT_X_TLS, &tls);
8ff60a
 	if (tls == LDAP_OPT_X_TLS_HARD) {
8ff60a
diff --git a/source3/libads/ldap.c b/source3/libads/ldap.c
8ff60a
index 36e73440495..16c32b2d5a7 100755
8ff60a
--- a/source3/libads/ldap.c
8ff60a
+++ b/source3/libads/ldap.c
8ff60a
@@ -703,7 +703,7 @@ got_connection:
8ff60a
 	ldap_set_option(ads->ldap.ld, LDAP_OPT_PROTOCOL_VERSION, &version);
8ff60a
 
8ff60a
 	if ( lp_ldap_ssl_ads() ) {
8ff60a
-		status = ADS_ERROR(smbldap_start_tls(ads->ldap.ld, version));
8ff60a
+		status = ADS_ERROR(smbldap_start_tls_start(ads->ldap.ld, version));
8ff60a
 		if (!ADS_ERR_OK(status)) {
8ff60a
 			goto out;
8ff60a
 		}
8ff60a
diff --git a/source3/wscript_build b/source3/wscript_build
8ff60a
index 10d9f71ae76..76d01a78f64 100644
8ff60a
--- a/source3/wscript_build
8ff60a
+++ b/source3/wscript_build
8ff60a
@@ -520,7 +520,7 @@ bld.SAMBA3_LIBRARY('smbldap',
8ff60a
                     abi_directory='lib/ABI',
8ff60a
                     abi_match='smbldap_*',
8ff60a
                     pc_files=[],
8ff60a
-                    vnum='2',
8ff60a
+                    vnum='2.1.0',
8ff60a
                     public_headers='include/smbldap.h include/smb_ldap.h')
8ff60a
 
8ff60a
 bld.SAMBA3_LIBRARY('ads',
8ff60a
-- 
8ff60a
2.25.4
8ff60a
8ff60a
8ff60a
From a7d674b519b363c6e20fa5784ab998fc622c9859 Mon Sep 17 00:00:00 2001
8ff60a
From: Isaac Boukris <iboukris@gmail.com>
8ff60a
Date: Thu, 2 Jul 2020 10:59:18 +0200
8ff60a
Subject: [PATCH 5/7] Fix ads_set_sasl_wrap_flags to only change sasl flags
8ff60a
8ff60a
BUG: https://bugzilla.samba.org/show_bug.cgi?id=14439
8ff60a
8ff60a
Signed-off-by: Isaac Boukris <iboukris@samba.org>
8ff60a
Reviewed-by: Andreas Schneider <asn@samba.org>
8ff60a
---
8ff60a
 source3/libads/ads_proto.h  | 2 +-
8ff60a
 source3/libads/ads_struct.c | 8 ++++++--
8ff60a
 2 files changed, 7 insertions(+), 3 deletions(-)
8ff60a
8ff60a
diff --git a/source3/libads/ads_proto.h b/source3/libads/ads_proto.h
8ff60a
index cd9c1082681..6cdde0cf6eb 100644
8ff60a
--- a/source3/libads/ads_proto.h
8ff60a
+++ b/source3/libads/ads_proto.h
8ff60a
@@ -47,7 +47,7 @@ ADS_STRUCT *ads_init(const char *realm,
8ff60a
 		     const char *workgroup,
8ff60a
 		     const char *ldap_server,
8ff60a
 		     enum ads_sasl_state_e sasl_state);
8ff60a
-bool ads_set_sasl_wrap_flags(ADS_STRUCT *ads, int flags);
8ff60a
+bool ads_set_sasl_wrap_flags(ADS_STRUCT *ads, unsigned flags);
8ff60a
 void ads_destroy(ADS_STRUCT **ads);
8ff60a
 
8ff60a
 /* The following definitions come from libads/disp_sec.c  */
8ff60a
diff --git a/source3/libads/ads_struct.c b/source3/libads/ads_struct.c
8ff60a
index 043a1b21247..67a9a7cf75e 100644
8ff60a
--- a/source3/libads/ads_struct.c
8ff60a
+++ b/source3/libads/ads_struct.c
8ff60a
@@ -176,13 +176,17 @@ ADS_STRUCT *ads_init(const char *realm,
8ff60a
 /****************************************************************
8ff60a
 ****************************************************************/
8ff60a
 
8ff60a
-bool ads_set_sasl_wrap_flags(ADS_STRUCT *ads, int flags)
8ff60a
+bool ads_set_sasl_wrap_flags(ADS_STRUCT *ads, unsigned flags)
8ff60a
 {
8ff60a
+	unsigned other_flags;
8ff60a
+
8ff60a
 	if (!ads) {
8ff60a
 		return false;
8ff60a
 	}
8ff60a
 
8ff60a
-	ads->auth.flags = flags;
8ff60a
+	other_flags = ads->auth.flags & ~(ADS_AUTH_SASL_SIGN|ADS_AUTH_SASL_SEAL);
8ff60a
+
8ff60a
+	ads->auth.flags = flags | other_flags;
8ff60a
 
8ff60a
 	return true;
8ff60a
 }
8ff60a
-- 
8ff60a
2.25.4
8ff60a
8ff60a
8ff60a
From e75511bf6b6b516db3336cd5f1d8f27307805801 Mon Sep 17 00:00:00 2001
8ff60a
From: Isaac Boukris <iboukris@gmail.com>
8ff60a
Date: Thu, 2 Jul 2020 09:33:12 +0200
8ff60a
Subject: [PATCH 6/7] ads: set sasl-wrapping to plain when over TLS
8ff60a
8ff60a
BUG: https://bugzilla.samba.org/show_bug.cgi?id=14439
8ff60a
8ff60a
Signed-off-by: Isaac Boukris <iboukris@samba.org>
8ff60a
Reviewed-by: Andreas Schneider <asn@samba.org>
8ff60a
---
8ff60a
 WHATSNEW.txt                     | 5 +++++
8ff60a
 selftest/knownfail.d/net_ads_tls | 1 -
8ff60a
 source3/libads/ldap.c            | 4 ++++
8ff60a
 3 files changed, 9 insertions(+), 1 deletion(-)
8ff60a
 delete mode 100644 selftest/knownfail.d/net_ads_tls
8ff60a
8ff60a
diff --git a/WHATSNEW.txt b/WHATSNEW.txt
8ff60a
index 8935876d247..927b9a0fa59 100644
8ff60a
--- a/WHATSNEW.txt
8ff60a
+++ b/WHATSNEW.txt
8ff60a
@@ -562,6 +562,11 @@ The "ldap ssl ads" option no longer depends on "ldap ssl" option:
8ff60a
 With this release, the "ldap ssl ads" can be set to "yes" even if "ldap ssl"
8ff60a
 is off.
8ff60a
 
8ff60a
+The "ldap ssl ads" no longer requires sasl-wrapping to be set to plain:
8ff60a
+-----------------------------------------------------------------------
8ff60a
+This is now done implicitly when over TLS, so "client ldap sasl wrapping"
8ff60a
+does not need to be set to "plain" in order for it to work.
8ff60a
+
8ff60a
 
8ff60a
 REMOVED FEATURES
8ff60a
 ================
8ff60a
diff --git a/selftest/knownfail.d/net_ads_tls b/selftest/knownfail.d/net_ads_tls
8ff60a
deleted file mode 100644
8ff60a
index 251c948b6a9..00000000000
8ff60a
--- a/selftest/knownfail.d/net_ads_tls
8ff60a
+++ /dev/null
8ff60a
@@ -1 +0,0 @@
8ff60a
-^samba4.blackbox.net_ads_tls
8ff60a
diff --git a/source3/libads/ldap.c b/source3/libads/ldap.c
8ff60a
index 16c32b2d5a7..3f41e990085 100755
8ff60a
--- a/source3/libads/ldap.c
8ff60a
+++ b/source3/libads/ldap.c
8ff60a
@@ -707,6 +707,10 @@ got_connection:
8ff60a
 		if (!ADS_ERR_OK(status)) {
8ff60a
 			goto out;
8ff60a
 		}
8ff60a
+		if (!ads_set_sasl_wrap_flags(ads, 0)) {
8ff60a
+			status = ADS_ERROR(LDAP_OPERATIONS_ERROR);
8ff60a
+			goto out;
8ff60a
+		}
8ff60a
 	}
8ff60a
 
8ff60a
 	/* fill in the current time and offsets */
8ff60a
-- 
8ff60a
2.25.4
8ff60a
8ff60a
8ff60a
From 43694fbfa79b255a27a4becaf8743d2b110495e9 Mon Sep 17 00:00:00 2001
8ff60a
From: Isaac Boukris <iboukris@gmail.com>
8ff60a
Date: Sat, 11 Jul 2020 05:04:59 +0200
8ff60a
Subject: [PATCH 7/7] net: ignore possible SIGPIPE upon ldap_unbind when over
8ff60a
 TLS
8ff60a
8ff60a
From local tests with strace:
8ff60a
8ff60a
socket(AF_UNIX, SOCK_STREAM, 0) = 12
8ff60a
write(2, "Connecting to 10.53.57.21 at por"..., 38) = 38
8ff60a
...
8ff60a
write(2, "ads_domain_func_level: 3\n", 25) = 25
8ff60a
write(12, "\27\3\3\0\37\0\0\0\0\0\0\0\16nl[\374\375i\325\334\25\227kxG@\326\311R\225x"..., 36) = 36
8ff60a
write(12, "\25\3\3\0\32\0\0\0\0\0\0\0\17Hh\304\254\244\17\342<\334\210L&\20_\177\307\232P", 31) = -1 EPIPE (Broken pipe)
8ff60a
--- SIGPIPE {si_signo=SIGPIPE, si_code=SI_USER, si_pid=12089, si_uid=1000} ---
8ff60a
+++ killed by SIGPIPE +++
8ff60a
8ff60a
BUG: https://bugzilla.samba.org/show_bug.cgi?id=14439
8ff60a
8ff60a
Signed-off-by: Isaac Boukris <iboukris@samba.org>
8ff60a
Reviewed-by: Andreas Schneider <asn@samba.org>
8ff60a
8ff60a
Autobuild-User(master): Isaac Boukris <iboukris@samba.org>
8ff60a
Autobuild-Date(master): Mon Jul 13 12:06:07 UTC 2020 on sn-devel-184
8ff60a
---
8ff60a
 source3/utils/net.c | 3 +++
8ff60a
 1 file changed, 3 insertions(+)
8ff60a
8ff60a
diff --git a/source3/utils/net.c b/source3/utils/net.c
8ff60a
index 683b46794e4..e289b2814bc 100644
8ff60a
--- a/source3/utils/net.c
8ff60a
+++ b/source3/utils/net.c
8ff60a
@@ -1289,6 +1289,9 @@ static void get_credentials_file(struct net_context *c,
8ff60a
 		POPT_TABLEEND
8ff60a
 	};
8ff60a
 
8ff60a
+	/* Ignore possible SIGPIPE upon ldap_unbind when over TLS */
8ff60a
+	BlockSignals(True, SIGPIPE);
8ff60a
+
8ff60a
 	zero_sockaddr(&c->opt_dest_ip);
8ff60a
 
8ff60a
 	setup_logging(argv[0], DEBUG_STDERR);
8ff60a
-- 
8ff60a
2.25.4
8ff60a
8ff60a
From 0a58060cb223a1ee6629f4ba706834369dd42a3d Mon Sep 17 00:00:00 2001
8ff60a
From: Isaac Boukris <iboukris@gmail.com>
8ff60a
Date: Tue, 14 Jul 2020 22:38:06 +0200
8ff60a
Subject: [PATCH] s3-libads: pass timeout to open_socket_out in ms
8ff60a
8ff60a
BUG: https://bugzilla.samba.org/show_bug.cgi?id=13124
8ff60a
8ff60a
Signed-off-by: Isaac Boukris <iboukris@samba.org>
8ff60a
---
8ff60a
 source3/libads/ldap.c | 4 +++-
8ff60a
 1 file changed, 3 insertions(+), 1 deletion(-)
8ff60a
8ff60a
diff --git a/source3/libads/ldap.c b/source3/libads/ldap.c
8ff60a
index 1ffe96d32c9..d431156912f 100755
8ff60a
--- a/source3/libads/ldap.c
8ff60a
+++ b/source3/libads/ldap.c
8ff60a
@@ -96,9 +96,11 @@ static void gotalarm_sig(int signum)
8ff60a
 	{
8ff60a
 		int fd = -1;
8ff60a
 		NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
8ff60a
+		unsigned timeout_ms = 1000 * to;
8ff60a
 
8ff60a
-		status = open_socket_out(ss, port, to, &fd;;
8ff60a
+		status = open_socket_out(ss, port, timeout_ms, &fd;;
8ff60a
 		if (!NT_STATUS_IS_OK(status)) {
8ff60a
+			DEBUG(3, ("open_socket_out: failed to open socket\n"));
8ff60a
 			return NULL;
8ff60a
 		}
8ff60a
 
8ff60a
-- 
8ff60a
2.25.4
8ff60a