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

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