s3: libads: Don't re-do DNS lookups in ads_current_time() if not needed.
authorJeremy Allison <jra@samba.org>
Thu, 23 Jul 2020 02:00:52 +0000 (19:00 -0700)
committerAndreas Schneider <asn@cryptomilk.org>
Tue, 18 Aug 2020 08:25:39 +0000 (08:25 +0000)
ADS_STRUCT may be being reused after a
DC lookup from ads_find_dc(), so ads->ldap.ss may already have a
good address (even if ads->server.ldap_server == NULL).
Only re-initialize the ADS_STRUCT and redo the ads_find_fc()
DNS lookups if we have to.

Signed-off-by: Jeremy Allison <jra@samba.org>
Reviewed-by: Andreas Schneider <asn@samba.org>
source3/libads/ldap.c

index e38fce111e728a7018819f70d4c72814d63d8364..04d699c348ac4ca5851e2630eb402e39ec90f7ef 100755 (executable)
@@ -3272,11 +3272,28 @@ ADS_STATUS ads_current_time(ADS_STRUCT *ads)
         /* establish a new ldap tcp session if necessary */
 
        if ( !ads->ldap.ld ) {
-               if ( (ads_s = ads_init( ads->server.realm, ads->server.workgroup, 
-                       ads->server.ldap_server, ADS_SASL_PLAIN )) == NULL )
-               {
-                       status = ADS_ERROR(LDAP_NO_MEMORY);
-                       goto done;
+               /*
+                * ADS_STRUCT may be being reused after a
+                * DC lookup, so ads->ldap.ss may already have a
+                * good address. If not, re-initialize the passed-in
+                * ADS_STRUCT with the given server.XXXX parameters.
+                *
+                * Note that this doesn't depend on
+                * ads->server.ldap_server != NULL,
+                * as the case where ads->server.ldap_server==NULL and
+                * ads->ldap.ss != zero_address is precisely the DC
+                * lookup case where ads->ldap.ss was found by going
+                * through ads_find_dc() again we want to avoid repeating.
+                */
+               if (is_zero_addr(&ads->ldap.ss)) {
+                       ads_s = ads_init(ads->server.realm,
+                                        ads->server.workgroup,
+                                        ads->server.ldap_server,
+                                        ADS_SASL_PLAIN );
+                       if (ads_s == NULL) {
+                               status = ADS_ERROR(LDAP_NO_MEMORY);
+                               goto done;
+                       }
                }
                ads_s->auth.flags = ADS_AUTH_ANON_BIND;
                status = ads_connect( ads_s );