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

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