s3-libads: Use a reducing page size to try and cope with a slow LDAP server
[samba.git] / source3 / libads / ads_struct.c
index 3cdd015bf46909c02a9a5efc0e26fc497c21ff3d..2d9ea17faac8147c44e988359ddea3a40cc32ff3 100644 (file)
@@ -6,7 +6,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,
@@ -15,8 +15,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"
@@ -30,35 +29,51 @@ char *ads_build_path(const char *realm, const char *sep, const char *field, int
        int numbits = 0;
        char *ret;
        int len;
-       
-       r = strdup(realm);
+       char *saveptr;
+
+       r = SMB_STRDUP(realm);
 
-       if (!r || !*r) return r;
+       if (!r || !*r) {
+               return r;
+       }
 
        for (p=r; *p; p++) {
-               if (strchr(sep, *p)) numbits++;
+               if (strchr(sep, *p)) {
+                       numbits++;
+               }
        }
 
        len = (numbits+1)*(strlen(field)+1) + strlen(r) + 1;
 
-       ret = malloc(len);
+       ret = (char *)SMB_MALLOC(len);
+       if (!ret) {
+               free(r);
+               return NULL;
+       }
+
        strlcpy(ret,field, len);
-       p=strtok(r,sep); 
-       strlcat(ret, p, len);
-
-       while ((p=strtok(NULL,sep))) {
-               char *s;
-               if (reverse) {
-                       asprintf(&s, "%s%s,%s", field, p, ret);
-               } else {
-                       asprintf(&s, "%s,%s%s", ret, field, p);
+       p=strtok_r(r, sep, &saveptr);
+       if (p) {
+               strlcat(ret, p, len);
+       
+               while ((p=strtok_r(NULL, sep, &saveptr)) != NULL) {
+                       int retval;
+                       char *s = NULL;
+                       if (reverse)
+                               retval = asprintf(&s, "%s%s,%s", field, p, ret);
+                       else
+                               retval = asprintf(&s, "%s,%s%s", ret, field, p);
+                       free(ret);
+                       if (retval == -1) {
+                               free(r);
+                               return NULL;
+                       }
+                       ret = SMB_STRDUP(s);
+                       free(s);
                }
-               free(ret);
-               ret = s;
        }
 
        free(r);
-
        return ret;
 }
 
@@ -71,6 +86,28 @@ char *ads_build_dn(const char *realm)
        return ads_build_path(realm, ".", "dc=", 0);
 }
 
+/* return a DNS name in the for aa.bb.cc from the DN  
+   "dc=AA,dc=BB,dc=CC".  caller must free
+*/
+char *ads_build_domain(const char *dn)
+{
+       char *dnsdomain = NULL;
+       
+       /* result should always be shorter than the DN */
+
+       if ( (dnsdomain = SMB_STRDUP( dn )) == NULL ) {
+               DEBUG(0,("ads_build_domain: malloc() failed!\n"));              
+               return NULL;            
+       }       
+
+       strlower_m( dnsdomain );        
+       all_string_sub( dnsdomain, "dc=", "", 0);
+       all_string_sub( dnsdomain, ",", ".", 0 );
+
+       return dnsdomain;       
+}
+
+
 
 #ifndef LDAP_PORT
 #define LDAP_PORT 389
@@ -84,30 +121,38 @@ ADS_STRUCT *ads_init(const char *realm,
                     const char *ldap_server)
 {
        ADS_STRUCT *ads;
+       int wrap_flags;
        
-       ads = (ADS_STRUCT *)smb_xmalloc(sizeof(*ads));
+       ads = SMB_XMALLOC_P(ADS_STRUCT);
        ZERO_STRUCTP(ads);
        
-       ads->server.realm = realm? strdup(realm) : NULL;
-       ads->server.workgroup = workgroup ? strdup(workgroup) : NULL;
-       ads->server.ldap_server = ldap_server? strdup(ldap_server) : NULL;
+       ads->server.realm = realm? SMB_STRDUP(realm) : NULL;
+       ads->server.workgroup = workgroup ? SMB_STRDUP(workgroup) : NULL;
+       ads->server.ldap_server = ldap_server? SMB_STRDUP(ldap_server) : NULL;
 
-       /* we need to know if this is a foreign realm to know if we can
-          use lp_ads_server() */
-       if (realm && strcasecmp(lp_realm(), realm) != 0) {
+       /* we need to know if this is a foreign realm */
+       if (realm && *realm && !strequal(lp_realm(), realm)) {
                ads->server.foreign = 1;
        }
-       if (workgroup && strcasecmp(lp_workgroup(), workgroup) != 0) {
+       if (workgroup && *workgroup && !strequal(lp_workgroup(), workgroup)) {
                ads->server.foreign = 1;
        }
 
-       return ads;
-}
+       /* the caller will own the memory by default */
+       ads->is_mine = 1;
 
-/* a simpler ads_init() interface using all defaults */
-ADS_STRUCT *ads_init_simple(void)
-{
-       return ads_init(NULL, NULL, NULL);
+       wrap_flags = lp_client_ldap_sasl_wrapping();
+       if (wrap_flags == -1) {
+               wrap_flags = 0;
+       }
+
+       ads->auth.flags = wrap_flags;
+
+       /* Start with a page size of 1000 when the connection is new,
+        * we will drop it by half we get a timeout.   */
+       ads->config.ldap_page_size     = 1000;
+
+       return ads;
 }
 
 /*
@@ -116,13 +161,15 @@ ADS_STRUCT *ads_init_simple(void)
 void ads_destroy(ADS_STRUCT **ads)
 {
        if (ads && *ads) {
+               bool is_mine;
+
+               is_mine = (*ads)->is_mine;
 #if HAVE_LDAP
-               if ((*ads)->ld) ldap_unbind((*ads)->ld);
+               ads_disconnect(*ads);
 #endif
                SAFE_FREE((*ads)->server.realm);
                SAFE_FREE((*ads)->server.workgroup);
                SAFE_FREE((*ads)->server.ldap_server);
-               SAFE_FREE((*ads)->server.ldap_uri);
 
                SAFE_FREE((*ads)->auth.realm);
                SAFE_FREE((*ads)->auth.password);
@@ -132,8 +179,14 @@ void ads_destroy(ADS_STRUCT **ads)
                SAFE_FREE((*ads)->config.realm);
                SAFE_FREE((*ads)->config.bind_path);
                SAFE_FREE((*ads)->config.ldap_server_name);
-
+               SAFE_FREE((*ads)->config.server_site_name);
+               SAFE_FREE((*ads)->config.client_site_name);
+               SAFE_FREE((*ads)->config.schema_path);
+               SAFE_FREE((*ads)->config.config_path);
+               
                ZERO_STRUCTP(*ads);
-               SAFE_FREE(*ads);
+
+               if ( is_mine )
+                       SAFE_FREE(*ads);
        }
 }