Blame SOURCES/0001-Make-adcli-info-DC-location-mechanism-more-compliant.patch

3e55d6
From 0a0d0f66409eb83e06b7dc50543c2f6c15a36bc4 Mon Sep 17 00:00:00 2001
3e55d6
From: Alexey A Nikitin <nikitin@amazon.com>
3e55d6
Date: Mon, 29 Oct 2018 20:40:36 -0700
3e55d6
Subject: [PATCH] Make 'adcli info' DC location mechanism more compliant with
3e55d6
 [MS-ADTS] and [MS-NRPC]
3e55d6
3e55d6
AD specifications say that DC locator must attempt to find a suitable DC for the client. That means going through all of the DCs in SRV RRs one by one until one of them answers.
3e55d6
3e55d6
The problem with adcli's original behavior is that it queries only five DCs from SRV, ever. This becomes a problem if for any reason there is a large number of DCs in the domain from which the client cannot get a CLDAP response.
3e55d6
---
3e55d6
 library/addisco.c | 146 +++++++++++++++++++++++++++++-----------------
3e55d6
 1 file changed, 94 insertions(+), 52 deletions(-)
3e55d6
3e55d6
diff --git a/library/addisco.c b/library/addisco.c
3e55d6
index 8cc5bf0..6e73ead 100644
3e55d6
--- a/library/addisco.c
3e55d6
+++ b/library/addisco.c
3e55d6
@@ -41,8 +41,10 @@
3e55d6
 #include <string.h>
3e55d6
 #include <time.h>
3e55d6
 
3e55d6
-/* Number of servers to do discovery against */
3e55d6
-#define DISCO_COUNT 5
3e55d6
+/* Number of servers to do discovery against.
3e55d6
+ * For AD DS maximum number of DCs is 1200.
3e55d6
+ */
3e55d6
+#define DISCO_COUNT 1200
3e55d6
 
3e55d6
 /* The time period in which to do rapid requests */
3e55d6
 #define DISCO_FEVER  1
3e55d6
@@ -453,6 +455,51 @@ parse_disco (LDAP *ldap,
3e55d6
 	return usability;
3e55d6
 }
3e55d6
 
3e55d6
+static int
3e55d6
+ldap_disco_poller (LDAP **ldap,
3e55d6
+                   LDAPMessage **message,
3e55d6
+                   adcli_disco **results,
3e55d6
+                   const char **addrs)
3e55d6
+{
3e55d6
+	int found = ADCLI_DISCO_UNUSABLE;
3e55d6
+	int close_ldap;
3e55d6
+	int parsed;
3e55d6
+	int ret = 0;
3e55d6
+	struct timeval tvpoll = { 0, 0 };
3e55d6
+
3e55d6
+	switch (ldap_result (*ldap, LDAP_RES_ANY, 1, &tvpoll, message)) {
3e55d6
+		case LDAP_RES_SEARCH_ENTRY:
3e55d6
+		case LDAP_RES_SEARCH_RESULT:
3e55d6
+			parsed = parse_disco (*ldap, *addrs, *message, results);
3e55d6
+			if (parsed > found)
3e55d6
+				found = parsed;
3e55d6
+			ldap_msgfree (*message);
3e55d6
+			close_ldap = 1;
3e55d6
+			break;
3e55d6
+		case -1:
3e55d6
+			ldap_get_option (*ldap, LDAP_OPT_RESULT_CODE, &ret;;
3e55d6
+			close_ldap = 1;
3e55d6
+			break;
3e55d6
+		default:
3e55d6
+			ldap_msgfree (*message);
3e55d6
+			close_ldap = 0;
3e55d6
+			break;
3e55d6
+	}
3e55d6
+
3e55d6
+	if (ret != LDAP_SUCCESS) {
3e55d6
+		_adcli_ldap_handle_failure (*ldap, ADCLI_ERR_CONFIG,
3e55d6
+		                            "Couldn't perform discovery search");
3e55d6
+	}
3e55d6
+
3e55d6
+	/* Done with this connection */
3e55d6
+	if (close_ldap) {
3e55d6
+		ldap_unbind_ext_s (*ldap, NULL, NULL);
3e55d6
+		*ldap = NULL;
3e55d6
+	}
3e55d6
+
3e55d6
+	return found;
3e55d6
+}
3e55d6
+
3e55d6
 static int
3e55d6
 ldap_disco (const char *domain,
3e55d6
             srvinfo *srv,
3e55d6
@@ -477,6 +524,7 @@ ldap_disco (const char *domain,
3e55d6
 	int num, i;
3e55d6
 	int ret;
3e55d6
 	int have_any = 0;
3e55d6
+	struct timeval interval;
3e55d6
 
3e55d6
 	if (domain) {
3e55d6
 		value = _adcli_ldap_escape_filter (domain);
3e55d6
@@ -540,7 +588,6 @@ ldap_disco (const char *domain,
3e55d6
 				version = LDAP_VERSION3;
3e55d6
 				ldap_set_option (ldap[num], LDAP_OPT_PROTOCOL_VERSION, &version);
3e55d6
 				ldap_set_option (ldap[num], LDAP_OPT_REFERRALS , 0);
3e55d6
-				_adcli_info ("Sending netlogon pings to domain controller: %s", url);
3e55d6
 				addrs[num] = srv->hostname;
3e55d6
 				have_any = 1;
3e55d6
 				num++;
3e55d6
@@ -555,70 +602,65 @@ ldap_disco (const char *domain,
3e55d6
 		freeaddrinfo (res);
3e55d6
 	}
3e55d6
 
3e55d6
-	/* Wait for the first response. Poor mans fd watch */
3e55d6
-	for (started = now = time (NULL);
3e55d6
-	     have_any && found != ADCLI_DISCO_USABLE && now < started + DISCO_TIME;
3e55d6
-	     now = time (NULL)) {
3e55d6
+	/* Initial send and short time wait */
3e55d6
+	interval.tv_sec = 0;
3e55d6
+	for (i = 0; ADCLI_DISCO_UNUSABLE == found && i < num; ++i) {
3e55d6
+		int parsed;
3e55d6
+
3e55d6
+		if (NULL == ldap[i])
3e55d6
+			continue;
3e55d6
 
3e55d6
-		struct timeval tvpoll = { 0, 0 };
3e55d6
-		struct timeval interval;
3e55d6
+		have_any = 1;
3e55d6
+		_adcli_info ("Sending NetLogon ping to domain controller: %s", addrs[i]);
3e55d6
 
3e55d6
-		/* If in the initial period, send feverishly */
3e55d6
-		if (now < started + DISCO_FEVER) {
3e55d6
-			interval.tv_sec = 0;
3e55d6
-			interval.tv_usec = 100 * 1000;
3e55d6
+		ret = ldap_search_ext (ldap[i], "", LDAP_SCOPE_BASE,
3e55d6
+		                       filter, attrs, 0, NULL, NULL, NULL,
3e55d6
+		                       -1, &msgidp);
3e55d6
+
3e55d6
+		if (ret != LDAP_SUCCESS) {
3e55d6
+			_adcli_ldap_handle_failure (ldap[i], ADCLI_ERR_CONFIG,
3e55d6
+			                            "Couldn't perform discovery search");
3e55d6
+			ldap_unbind_ext_s (ldap[i], NULL, NULL);
3e55d6
+			ldap[i] = NULL;
3e55d6
+		}
3e55d6
+
3e55d6
+		/* From https://msdn.microsoft.com/en-us/library/ff718294.aspx first
3e55d6
+		 * five DCs are given 0.4 seconds timeout, next five are given 0.2
3e55d6
+		 * seconds, and the rest are given 0.1 seconds
3e55d6
+		 */
3e55d6
+		if (i < 5) {
3e55d6
+			interval.tv_usec = 400000;
3e55d6
+		} else if (i < 10) {
3e55d6
+			interval.tv_usec = 200000;
3e55d6
 		} else {
3e55d6
-			interval.tv_sec = 1;
3e55d6
-			interval.tv_usec = 0;
3e55d6
+			interval.tv_usec = 100000;
3e55d6
 		}
3e55d6
+		select (0, NULL, NULL, NULL, &interval);
3e55d6
+
3e55d6
+		parsed = ldap_disco_poller (&(ldap[i]), &message, results, &(addrs[i]));
3e55d6
+		if (parsed > found)
3e55d6
+			found = parsed;
3e55d6
+	}
3e55d6
+
3e55d6
+	/* Wait some more until LDAP timeout (DISCO_TIME) */
3e55d6
+	for (started = now = time (NULL);
3e55d6
+	     have_any && ADCLI_DISCO_UNUSABLE == found && now < started + DISCO_TIME;
3e55d6
+	     now = time (NULL)) {
3e55d6
 
3e55d6
 		select (0, NULL, NULL, NULL, &interval);
3e55d6
 
3e55d6
 		have_any = 0;
3e55d6
-		for (i = 0; found != ADCLI_DISCO_USABLE && i < num; i++) {
3e55d6
-			int close_ldap;
3e55d6
+		for (i = 0; ADCLI_DISCO_UNUSABLE == found && i < num; ++i) {
3e55d6
 			int parsed;
3e55d6
 
3e55d6
 			if (ldap[i] == NULL)
3e55d6
 				continue;
3e55d6
 
3e55d6
-			ret = 0;
3e55d6
 			have_any = 1;
3e55d6
-			switch (ldap_result (ldap[i], LDAP_RES_ANY, 1, &tvpoll, &message)) {
3e55d6
-			case LDAP_RES_SEARCH_ENTRY:
3e55d6
-			case LDAP_RES_SEARCH_RESULT:
3e55d6
-				parsed = parse_disco (ldap[i], addrs[i], message, results);
3e55d6
-				if (parsed > found)
3e55d6
-					found = parsed;
3e55d6
-				ldap_msgfree (message);
3e55d6
-				close_ldap = 1;
3e55d6
-				break;
3e55d6
-			case 0:
3e55d6
-				ret = ldap_search_ext (ldap[i], "", LDAP_SCOPE_BASE,
3e55d6
-				                       filter, attrs, 0, NULL, NULL, NULL,
3e55d6
-				                       -1, &msgidp);
3e55d6
-				close_ldap = (ret != 0);
3e55d6
-				break;
3e55d6
-			case -1:
3e55d6
-				ldap_get_option (ldap[i], LDAP_OPT_RESULT_CODE, &ret;;
3e55d6
-				close_ldap = 1;
3e55d6
-				break;
3e55d6
-			default:
3e55d6
-				ldap_msgfree (message);
3e55d6
-				close_ldap = 0;
3e55d6
-				break;
3e55d6
-			}
3e55d6
-
3e55d6
-			if (ret != LDAP_SUCCESS) {
3e55d6
-				_adcli_ldap_handle_failure (ldap[i], ADCLI_ERR_CONFIG,
3e55d6
-				                            "Couldn't perform discovery search");
3e55d6
-			}
3e55d6
 
3e55d6
-			/* Done with this connection */
3e55d6
-			if (close_ldap) {
3e55d6
-				ldap_unbind_ext_s (ldap[i], NULL, NULL);
3e55d6
-				ldap[i] = NULL;
3e55d6
-			}
3e55d6
+			parsed = ldap_disco_poller (&(ldap[i]), &message, results, &(addrs[i]));
3e55d6
+			if (parsed > found)
3e55d6
+				found = parsed;
3e55d6
 		}
3e55d6
 	}
3e55d6
 
3e55d6
-- 
3e55d6
2.26.2
3e55d6