Blame SOURCES/0002-add-option-use-ldaps.patch

463fb6
From 196163b419f3a22c78bd14095b91822fe08aa595 Mon Sep 17 00:00:00 2001
463fb6
From: Sumit Bose <sbose@redhat.com>
463fb6
Date: Thu, 19 Dec 2019 07:22:33 +0100
463fb6
Subject: [PATCH 2/2] add option use-ldaps
463fb6
463fb6
In general using the LDAP port with GSS-SPNEGO should satifiy all
463fb6
requirements an AD DC should have for authentication on an encrypted
463fb6
LDAP connection.
463fb6
463fb6
But if e.g. the LDAP port is blocked by a firewall using the LDAPS port
463fb6
with TLS encryption might be an alternative. For this use case the
463fb6
--use-ldaps option is added.
463fb6
463fb6
Related to https://bugzilla.redhat.com/show_bug.cgi?id=1762420
463fb6
---
463fb6
 doc/adcli.xml    | 24 +++++++++++++++
463fb6
 library/adconn.c | 79 ++++++++++++++++++++++++++++++++++++++++++------
463fb6
 library/adconn.h |  4 +++
463fb6
 tools/computer.c |  9 ++++++
463fb6
 tools/entry.c    | 11 +++++++
463fb6
 5 files changed, 118 insertions(+), 9 deletions(-)
463fb6
463fb6
diff --git a/doc/adcli.xml b/doc/adcli.xml
463fb6
index 9605b4a..3cefe48 100644
463fb6
--- a/doc/adcli.xml
463fb6
+++ b/doc/adcli.xml
463fb6
@@ -123,6 +123,30 @@
463fb6
 			If not specified, then an appropriate domain controller
463fb6
 			is automatically discovered.</para></listitem>
463fb6
 		</varlistentry>
463fb6
+		<varlistentry>
463fb6
+			<term><option>--use-ldaps</option></term>
463fb6
+			<listitem><para>Connect to the domain controller
463fb6
+			with LDAPS. By default the LDAP port is used and SASL
463fb6
+			GSS-SPNEGO or GSSAPI is used for authentication and to
463fb6
+			establish encryption. This should satisfy all
463fb6
+			requirements set on the server side and LDAPS should
463fb6
+			only be used if the LDAP port is not accessible due to
463fb6
+			firewalls or other reasons.</para>
463fb6
+			<para> Please note that the place where CA certificates
463fb6
+			can be found to validate the AD DC certificates
463fb6
+			must be configured in the OpenLDAP configuration
463fb6
+			file, e.g. <filename>/etc/openldap/ldap.conf</filename>.
463fb6
+			As an alternative it can be specified with the help of
463fb6
+			an environment variable, e.g.
463fb6
+<programlisting>
463fb6
+$ LDAPTLS_CACERT=/path/to/ad_dc_ca_cert.pem adcli join --use-ldaps -D domain.example.com
463fb6
+...
463fb6
+</programlisting>
463fb6
+			Please see
463fb6
+			<citerefentry><refentrytitle>ldap.conf</refentrytitle>
463fb6
+			<manvolnum>5</manvolnum></citerefentry> for details.
463fb6
+			</para></listitem>
463fb6
+		</varlistentry>
463fb6
 		<varlistentry>
463fb6
 			<term><option>-C, --login-ccache=<parameter>ccache_name</parameter></option></term>
463fb6
 			<listitem><para>Use the specified kerberos credential
463fb6
diff --git a/library/adconn.c b/library/adconn.c
463fb6
index a3f4548..8a55776 100644
463fb6
--- a/library/adconn.c
463fb6
+++ b/library/adconn.c
463fb6
@@ -70,6 +70,7 @@ struct _adcli_conn_ctx {
463fb6
 	char *domain_name;
463fb6
 	char *domain_realm;
463fb6
 	char *domain_controller;
463fb6
+	bool use_ldaps;
463fb6
 	char *canonical_host;
463fb6
 	char *domain_short;
463fb6
 	char *domain_sid;
463fb6
@@ -773,7 +774,8 @@ int ldap_init_fd (ber_socket_t fd, int proto, LDAP_CONST char *url, struct ldap
463fb6
 
463fb6
 static LDAP *
463fb6
 connect_to_address (const char *host,
463fb6
-                    const char *canonical_host)
463fb6
+                    const char *canonical_host,
463fb6
+                    bool use_ldaps)
463fb6
 {
463fb6
 	struct addrinfo *res = NULL;
463fb6
 	struct addrinfo *ai;
463fb6
@@ -783,6 +785,16 @@ connect_to_address (const char *host,
463fb6
 	char *url;
463fb6
 	int sock;
463fb6
 	int rc;
463fb6
+	int opt_rc;
463fb6
+	const char *port = "389";
463fb6
+	const char *proto = "ldap";
463fb6
+	const char *errmsg = NULL;
463fb6
+
463fb6
+	if (use_ldaps) {
463fb6
+		port = "636";
463fb6
+		proto = "ldaps";
463fb6
+		_adcli_info ("Using LDAPS to connect to %s", host);
463fb6
+	}
463fb6
 
463fb6
 	memset (&hints, '\0', sizeof(hints));
463fb6
 #ifdef AI_ADDRCONFIG
463fb6
@@ -794,7 +806,7 @@ connect_to_address (const char *host,
463fb6
 	if (!canonical_host)
463fb6
 		canonical_host = host;
463fb6
 
463fb6
-	rc = getaddrinfo (host, "389", &hints, &res;;
463fb6
+	rc = getaddrinfo (host, port, &hints, &res;;
463fb6
 	if (rc != 0) {
463fb6
 		_adcli_err ("Couldn't resolve host name: %s: %s", host, gai_strerror (rc));
463fb6
 		return NULL;
463fb6
@@ -810,7 +822,7 @@ connect_to_address (const char *host,
463fb6
 			close (sock);
463fb6
 		} else {
463fb6
 			error = 0;
463fb6
-			if (asprintf (&url, "ldap://%s", canonical_host) < 0)
463fb6
+			if (asprintf (&url, "%s://%s", proto, canonical_host) < 0)
463fb6
 				return_val_if_reached (NULL);
463fb6
 			rc = ldap_init_fd (sock, 1, url, &ldap);
463fb6
 			free (url);
463fb6
@@ -820,6 +832,25 @@ connect_to_address (const char *host,
463fb6
 				            ldap_err2string (rc));
463fb6
 				break;
463fb6
 			}
463fb6
+
463fb6
+			if (use_ldaps) {
463fb6
+				rc = ldap_install_tls (ldap);
463fb6
+				if (rc != LDAP_SUCCESS) {
463fb6
+					opt_rc = ldap_get_option (ldap,
463fb6
+					                          LDAP_OPT_DIAGNOSTIC_MESSAGE,
463fb6
+					                          (void *) &errmsg);
463fb6
+					if (opt_rc != LDAP_SUCCESS) {
463fb6
+						errmsg = NULL;
463fb6
+					}
463fb6
+					_adcli_err ("Couldn't initialize TLS [%s]: %s",
463fb6
+					            ldap_err2string (rc),
463fb6
+					            errmsg == NULL ? "- no details -"
463fb6
+					                           : errmsg);
463fb6
+					ldap_unbind_ext_s (ldap, NULL, NULL);
463fb6
+					ldap = NULL;
463fb6
+					break;
463fb6
+				}
463fb6
+			}
463fb6
 		}
463fb6
 	}
463fb6
 
463fb6
@@ -856,7 +887,8 @@ connect_and_lookup_naming (adcli_conn *conn,
463fb6
 	if (!canonical_host)
463fb6
 		canonical_host = disco->host_addr;
463fb6
 
463fb6
-	ldap = connect_to_address (disco->host_addr, canonical_host);
463fb6
+	ldap = connect_to_address (disco->host_addr, canonical_host,
463fb6
+	                           adcli_conn_get_use_ldaps (conn));
463fb6
 	if (ldap == NULL)
463fb6
 		return ADCLI_ERR_DIRECTORY;
463fb6
 
463fb6
@@ -1041,14 +1073,28 @@ authenticate_to_directory (adcli_conn *conn)
463fb6
 	status = gss_krb5_ccache_name (&minor, conn->login_ccache_name, NULL);
463fb6
 	return_unexpected_if_fail (status == 0);
463fb6
 
463fb6
-	/* Clumsily tell ldap + cyrus-sasl that we want encryption */
463fb6
-	ssf = 1;
463fb6
-	ret = ldap_set_option (conn->ldap, LDAP_OPT_X_SASL_SSF_MIN, &ssf;;
463fb6
-	return_unexpected_if_fail (ret == 0);
463fb6
+	if (adcli_conn_get_use_ldaps (conn)) {
463fb6
+		/* do not use SASL encryption on LDAPS connection */
463fb6
+		ssf = 0;
463fb6
+		ret = ldap_set_option (conn->ldap, LDAP_OPT_X_SASL_SSF_MIN, &ssf;;
463fb6
+		return_unexpected_if_fail (ret == 0);
463fb6
+		ret = ldap_set_option (conn->ldap, LDAP_OPT_X_SASL_SSF_MAX, &ssf;;
463fb6
+		return_unexpected_if_fail (ret == 0);
463fb6
+	} else {
463fb6
+		/* Clumsily tell ldap + cyrus-sasl that we want encryption */
463fb6
+		ssf = 1;
463fb6
+		ret = ldap_set_option (conn->ldap, LDAP_OPT_X_SASL_SSF_MIN, &ssf;;
463fb6
+		return_unexpected_if_fail (ret == 0);
463fb6
+	}
463fb6
 
463fb6
-	if (adcli_conn_server_has_sasl_mech (conn, "GSS-SPNEGO")) {
463fb6
+	/* There are issues with cryrus-sasl and GSS-SPNEGO with TLS even if
463fb6
+	 * ssf_max is set to 0. To be on the safe side GSS-SPNEGO is only used
463fb6
+	 * without LDAPS. */
463fb6
+	if (adcli_conn_server_has_sasl_mech (conn, "GSS-SPNEGO")
463fb6
+	                     && !adcli_conn_get_use_ldaps (conn)) {
463fb6
 		mech =  "GSS-SPNEGO";
463fb6
 	}
463fb6
+	_adcli_info ("Using %s for SASL bind", mech);
463fb6
 
463fb6
 	ret = ldap_sasl_interactive_bind_s (conn->ldap, NULL, mech, NULL, NULL,
463fb6
 	                                    LDAP_SASL_QUIET, sasl_interact, NULL);
463fb6
@@ -1230,6 +1276,7 @@ adcli_conn_new (const char *domain_name)
463fb6
 	conn->refs = 1;
463fb6
 	conn->logins_allowed = ADCLI_LOGIN_COMPUTER_ACCOUNT | ADCLI_LOGIN_USER_ACCOUNT;
463fb6
 	adcli_conn_set_domain_name (conn, domain_name);
463fb6
+	adcli_conn_set_use_ldaps (conn, false);
463fb6
 	return conn;
463fb6
 }
463fb6
 
463fb6
@@ -1389,6 +1436,20 @@ adcli_conn_set_domain_controller (adcli_conn *conn,
463fb6
 	no_more_disco (conn);
463fb6
 }
463fb6
 
463fb6
+bool
463fb6
+adcli_conn_get_use_ldaps (adcli_conn *conn)
463fb6
+{
463fb6
+	return_val_if_fail (conn != NULL, NULL);
463fb6
+	return conn->use_ldaps;
463fb6
+}
463fb6
+
463fb6
+void
463fb6
+adcli_conn_set_use_ldaps (adcli_conn *conn, bool value)
463fb6
+{
463fb6
+	return_if_fail (conn != NULL);
463fb6
+	conn->use_ldaps = value;
463fb6
+}
463fb6
+
463fb6
 const char *
463fb6
 adcli_conn_get_domain_short (adcli_conn *conn)
463fb6
 {
463fb6
diff --git a/library/adconn.h b/library/adconn.h
463fb6
index 8e88045..3e287b1 100644
463fb6
--- a/library/adconn.h
463fb6
+++ b/library/adconn.h
463fb6
@@ -89,6 +89,10 @@ const char *        adcli_conn_get_domain_controller (adcli_conn *conn);
463fb6
 void                adcli_conn_set_domain_controller (adcli_conn *conn,
463fb6
                                                       const char *value);
463fb6
 
463fb6
+bool                adcli_conn_get_use_ldaps         (adcli_conn *conn);
463fb6
+void                adcli_conn_set_use_ldaps         (adcli_conn *conn,
463fb6
+                                                      bool value);
463fb6
+
463fb6
 const char *        adcli_conn_get_domain_short      (adcli_conn *conn);
463fb6
 
463fb6
 const char *        adcli_conn_get_domain_sid        (adcli_conn *conn);
463fb6
diff --git a/tools/computer.c b/tools/computer.c
463fb6
index ac8a203..7bf8f9b 100644
463fb6
--- a/tools/computer.c
463fb6
+++ b/tools/computer.c
463fb6
@@ -112,12 +112,14 @@ typedef enum {
463fb6
 	opt_trusted_for_delegation,
463fb6
 	opt_add_service_principal,
463fb6
 	opt_remove_service_principal,
463fb6
+	opt_use_ldaps,
463fb6
 } Option;
463fb6
 
463fb6
 static adcli_tool_desc common_usages[] = {
463fb6
 	{ opt_domain, "active directory domain name" },
463fb6
 	{ opt_domain_realm, "kerberos realm for the domain" },
463fb6
 	{ opt_domain_controller, "domain controller to connect to" },
463fb6
+	{ opt_use_ldaps, "use LDAPS port for communication" },
463fb6
 	{ opt_host_fqdn, "override the fully qualified domain name of the\n"
463fb6
 	                 "local machine" },
463fb6
 	{ opt_host_keytab, "filename for the host kerberos keytab" },
463fb6
@@ -306,6 +308,9 @@ parse_option (Option opt,
463fb6
 	case opt_remove_service_principal:
463fb6
 		adcli_enroll_add_service_principal_to_remove (enroll, optarg);
463fb6
 		return ADCLI_SUCCESS;
463fb6
+	case opt_use_ldaps:
463fb6
+		adcli_conn_set_use_ldaps (conn, true);
463fb6
+		return ADCLI_SUCCESS;
463fb6
 	case opt_verbose:
463fb6
 		return ADCLI_SUCCESS;
463fb6
 
463fb6
@@ -352,6 +357,7 @@ adcli_tool_computer_join (adcli_conn *conn,
463fb6
 		{ "domain-realm", required_argument, NULL, opt_domain_realm },
463fb6
 		{ "domain-controller", required_argument, NULL, opt_domain_controller },
463fb6
 		{ "domain-server", required_argument, NULL, opt_domain_controller }, /* compat */
463fb6
+		{ "use-ldaps", no_argument, 0, opt_use_ldaps },
463fb6
 		{ "login-user", required_argument, NULL, opt_login_user },
463fb6
 		{ "user", required_argument, NULL, opt_login_user }, /* compat */
463fb6
 		{ "login-ccache", optional_argument, NULL, opt_login_ccache },
463fb6
@@ -681,6 +687,7 @@ adcli_tool_computer_preset (adcli_conn *conn,
463fb6
 		{ "domain", required_argument, NULL, opt_domain },
463fb6
 		{ "domain-realm", required_argument, NULL, opt_domain_realm },
463fb6
 		{ "domain-controller", required_argument, NULL, opt_domain_controller },
463fb6
+		{ "use-ldaps", no_argument, 0, opt_use_ldaps },
463fb6
 		{ "domain-ou", required_argument, NULL, opt_domain_ou },
463fb6
 		{ "login-user", required_argument, NULL, opt_login_user },
463fb6
 		{ "login-ccache", optional_argument, NULL, opt_login_ccache },
463fb6
@@ -793,6 +800,7 @@ adcli_tool_computer_reset (adcli_conn *conn,
463fb6
 		{ "domain", required_argument, NULL, opt_domain },
463fb6
 		{ "domain-realm", required_argument, NULL, opt_domain_realm },
463fb6
 		{ "domain-controller", required_argument, NULL, opt_domain_controller },
463fb6
+		{ "use-ldaps", no_argument, 0, opt_use_ldaps },
463fb6
 		{ "login-user", required_argument, NULL, opt_login_user },
463fb6
 		{ "login-ccache", optional_argument, NULL, opt_login_ccache },
463fb6
 		{ "login-type", required_argument, NULL, opt_login_type },
463fb6
@@ -881,6 +889,7 @@ adcli_tool_computer_delete (adcli_conn *conn,
463fb6
 		{ "domain", required_argument, NULL, opt_domain },
463fb6
 		{ "domain-realm", required_argument, NULL, opt_domain_realm },
463fb6
 		{ "domain-controller", required_argument, NULL, opt_domain_controller },
463fb6
+		{ "use-ldaps", no_argument, 0, opt_use_ldaps },
463fb6
 		{ "login-user", required_argument, NULL, opt_login_user },
463fb6
 		{ "login-ccache", optional_argument, NULL, opt_login_ccache },
463fb6
 		{ "no-password", no_argument, 0, opt_no_password },
463fb6
diff --git a/tools/entry.c b/tools/entry.c
463fb6
index f361845..05e4313 100644
463fb6
--- a/tools/entry.c
463fb6
+++ b/tools/entry.c
463fb6
@@ -53,6 +53,7 @@ typedef enum {
463fb6
 	opt_unix_gid,
463fb6
 	opt_unix_shell,
463fb6
 	opt_nis_domain,
463fb6
+	opt_use_ldaps,
463fb6
 } Option;
463fb6
 
463fb6
 static adcli_tool_desc common_usages[] = {
463fb6
@@ -67,6 +68,7 @@ static adcli_tool_desc common_usages[] = {
463fb6
 	{ opt_domain, "active directory domain name" },
463fb6
 	{ opt_domain_realm, "kerberos realm for the domain" },
463fb6
 	{ opt_domain_controller, "domain directory server to connect to" },
463fb6
+	{ opt_use_ldaps, "use LDAPS port for communication" },
463fb6
 	{ opt_login_ccache, "kerberos credential cache file which contains\n"
463fb6
 	                    "ticket to used to connect to the domain" },
463fb6
 	{ opt_login_user, "user (usually administrative) login name of\n"
463fb6
@@ -136,6 +138,9 @@ parse_option (Option opt,
463fb6
 			stdin_password = 1;
463fb6
 		}
463fb6
 		return ADCLI_SUCCESS;
463fb6
+	case opt_use_ldaps:
463fb6
+		adcli_conn_set_use_ldaps (conn, true);
463fb6
+		return ADCLI_SUCCESS;
463fb6
 	case opt_verbose:
463fb6
 		return ADCLI_SUCCESS;
463fb6
 	default:
463fb6
@@ -172,6 +177,7 @@ adcli_tool_user_create (adcli_conn *conn,
463fb6
 		{ "domain", required_argument, NULL, opt_domain },
463fb6
 		{ "domain-realm", required_argument, NULL, opt_domain_realm },
463fb6
 		{ "domain-controller", required_argument, NULL, opt_domain_controller },
463fb6
+		{ "use-ldaps", no_argument, 0, opt_use_ldaps },
463fb6
 		{ "login-user", required_argument, NULL, opt_login_user },
463fb6
 		{ "login-ccache", optional_argument, NULL, opt_login_ccache },
463fb6
 		{ "no-password", no_argument, 0, opt_no_password },
463fb6
@@ -306,6 +312,7 @@ adcli_tool_user_delete (adcli_conn *conn,
463fb6
 		{ "domain", required_argument, NULL, opt_domain },
463fb6
 		{ "domain-realm", required_argument, NULL, opt_domain_realm },
463fb6
 		{ "domain-controller", required_argument, NULL, opt_domain_controller },
463fb6
+		{ "use-ldaps", no_argument, 0, opt_use_ldaps },
463fb6
 		{ "login-user", required_argument, NULL, opt_login_user },
463fb6
 		{ "login-ccache", optional_argument, NULL, opt_login_ccache },
463fb6
 		{ "no-password", no_argument, 0, opt_no_password },
463fb6
@@ -394,6 +401,7 @@ adcli_tool_group_create (adcli_conn *conn,
463fb6
 		{ "domain", required_argument, NULL, opt_domain },
463fb6
 		{ "domain-realm", required_argument, NULL, opt_domain_realm },
463fb6
 		{ "domain-controller", required_argument, NULL, opt_domain_controller },
463fb6
+		{ "use-ldaps", no_argument, 0, opt_use_ldaps },
463fb6
 		{ "domain-ou", required_argument, NULL, opt_domain_ou },
463fb6
 		{ "login-user", required_argument, NULL, opt_login_user },
463fb6
 		{ "login-ccache", optional_argument, NULL, opt_login_ccache },
463fb6
@@ -496,6 +504,7 @@ adcli_tool_group_delete (adcli_conn *conn,
463fb6
 		{ "domain", required_argument, NULL, opt_domain },
463fb6
 		{ "domain-realm", required_argument, NULL, opt_domain_realm },
463fb6
 		{ "domain-controller", required_argument, NULL, opt_domain_controller },
463fb6
+		{ "use-ldaps", no_argument, 0, opt_use_ldaps },
463fb6
 		{ "login-user", required_argument, NULL, opt_login_user },
463fb6
 		{ "login-ccache", optional_argument, NULL, opt_login_ccache },
463fb6
 		{ "no-password", no_argument, 0, opt_no_password },
463fb6
@@ -622,6 +631,7 @@ adcli_tool_member_add (adcli_conn *conn,
463fb6
 		{ "domain", required_argument, NULL, opt_domain },
463fb6
 		{ "domain-realm", required_argument, NULL, opt_domain_realm },
463fb6
 		{ "domain-controller", required_argument, NULL, opt_domain_controller },
463fb6
+		{ "use-ldaps", no_argument, 0, opt_use_ldaps },
463fb6
 		{ "login-user", required_argument, NULL, opt_login_user },
463fb6
 		{ "login-ccache", optional_argument, NULL, opt_login_ccache },
463fb6
 		{ "no-password", no_argument, 0, opt_no_password },
463fb6
@@ -722,6 +732,7 @@ adcli_tool_member_remove (adcli_conn *conn,
463fb6
 		{ "domain", required_argument, NULL, opt_domain },
463fb6
 		{ "domain-realm", required_argument, NULL, opt_domain_realm },
463fb6
 		{ "domain-controller", required_argument, NULL, opt_domain_controller },
463fb6
+		{ "use-ldaps", no_argument, 0, opt_use_ldaps },
463fb6
 		{ "login-user", required_argument, NULL, opt_login_user },
463fb6
 		{ "login-ccache", optional_argument, NULL, opt_login_ccache },
463fb6
 		{ "no-password", no_argument, 0, opt_no_password },
463fb6
-- 
463fb6
2.21.1
463fb6