s3-libads: Use a reducing page size to try and cope with a slow LDAP server
[samba.git] / source3 / libads / ldap_utils.c
index 6417e92e9269b709715d4d52dad9e8c50d998a3d..dee3c03a2166dd22febea561977cc62f28fd9233 100644 (file)
@@ -8,7 +8,7 @@
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
@@ -17,8 +17,7 @@
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
@@ -39,8 +38,8 @@ static ADS_STATUS ads_do_search_retry_internal(ADS_STRUCT *ads, const char *bind
 
        *res = NULL;
 
-       if (!ads->ld &&
-           time(NULL) - ads->last_attempt < ADS_RECONNECT_TIME) {
+       if (!ads->ldap.ld &&
+           time(NULL) - ads->ldap.last_attempt < ADS_RECONNECT_TIME) {
                return ADS_ERROR(LDAP_SERVER_DOWN);
        }
 
@@ -61,14 +60,21 @@ static ADS_STATUS ads_do_search_retry_internal(ADS_STRUCT *ads, const char *bind
                status = ads_do_search_all_args(ads, bp, scope, expr, attrs, args, res);
        }
        if (ADS_ERR_OK(status)) {
-               DEBUG(5,("Search for %s in <%s> gave %d replies\n",
-                       expr, bp, ads_count_replies(ads, *res)));
+               DEBUG(5,("Search for %s in <%s> gave %d replies\n",
+                        expr, bp, ads_count_replies(ads, *res)));
                SAFE_FREE(bp);
                return status;
        }
 
        while (--count) {
 
+               if (NT_STATUS_EQUAL(ads_ntstatus(status), NT_STATUS_IO_TIMEOUT) && ads->config.ldap_page_size >= 250) {
+                       int new_page_size = (ads->config.ldap_page_size / 2);
+                       DEBUG(1, ("Reducing LDAP page size from %d to %d due to IO_TIMEOUT\n",
+                                 ads->config.ldap_page_size, new_page_size));
+                       ads->config.ldap_page_size = new_page_size;
+               }
+
                if (*res) 
                        ads_msgfree(ads, *res);
                *res = NULL;
@@ -76,11 +82,7 @@ static ADS_STATUS ads_do_search_retry_internal(ADS_STRUCT *ads, const char *bind
                DEBUG(3,("Reopening ads connection to realm '%s' after error %s\n", 
                         ads->config.realm, ads_errstr(status)));
                         
-               if (ads->ld) {
-                       ldap_unbind(ads->ld); 
-               }
-               
-               ads->ld = NULL;
+               ads_disconnect(ads);
                status = ads_connect(ads);
                
                if (!ADS_ERR_OK(status)) {
@@ -164,6 +166,21 @@ static ADS_STATUS ads_do_search_retry_internal(ADS_STRUCT *ads, const char *bind
                                        "(objectclass=*)", attrs, &args, res);
 }
 
+ ADS_STATUS ads_search_retry_dn_sd_flags(ADS_STRUCT *ads, LDAPMessage **res, 
+                                        uint32 sd_flags,
+                                        const char *dn, 
+                                        const char **attrs)
+{
+       ads_control args;
+
+       args.control = ADS_SD_FLAGS_OID;
+       args.val = sd_flags;
+       args.critical = True;
+
+       return ads_do_search_retry_args(ads, dn, LDAP_SCOPE_BASE,
+                                       "(objectclass=*)", attrs, &args, res);
+}
+
  ADS_STATUS ads_search_retry_extended_dn_ranged(ADS_STRUCT *ads, TALLOC_CTX *mem_ctx, 
                                                const char *dn, 
                                                const char **attrs,
@@ -186,21 +203,6 @@ static ADS_STATUS ads_do_search_retry_internal(ADS_STRUCT *ads, const char *bind
                                 "(objectclass=*)", &args, attrs[0],
                                 strings, num_strings);
 
-}
-
- ADS_STATUS ads_search_retry_dn_sd_flags(ADS_STRUCT *ads, LDAPMessage **res, 
-                                        uint32 sd_flags,
-                                        const char *dn, 
-                                        const char **attrs)
-{
-       ads_control args;
-
-       args.control = ADS_SD_FLAGS_OID;
-       args.val = sd_flags;
-       args.critical = True;
-
-       return ads_do_search_retry_args(ads, dn, LDAP_SCOPE_BASE,
-                                       "(objectclass=*)", attrs, &args, res);
 }
 
  ADS_STATUS ads_search_retry_sid(ADS_STRUCT *ads, LDAPMessage **res, 
@@ -241,7 +243,7 @@ ADS_STATUS ads_ranged_search(ADS_STRUCT *ads,
        uint32 first_usn;
        int num_retries = 0;
        const char **attrs;
-       BOOL more_values = False;
+       bool more_values = False;
 
        *num_strings = 0;
        *strings = NULL;
@@ -294,7 +296,7 @@ ADS_STATUS ads_ranged_search_internal(ADS_STRUCT *ads,
                                      size_t *num_strings,
                                      uint32 *first_usn,
                                      int *num_retries,
-                                     BOOL *more_values)
+                                     bool *more_values)
 {
        LDAPMessage *res = NULL;
        ADS_STATUS status;