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