More strlcat/strlcpy truncate checks.
[amitay/samba.git] / source3 / libads / ads_struct.c
index be0374225b0ea32a8761911ff2088a543ead913d..e6220fd320a38262499d237cda2017571ee4af6f 100644 (file)
 /* 
-   Unix SMB/Netbios implementation.
-   Version 3.0
+   Unix SMB/CIFS implementation.
    ads (active directory) utility library
    Copyright (C) Andrew Tridgell 2001
    Copyright (C) Andrew Bartlett 2001
-   
+
    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,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    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"
+#include "ads.h"
 
-static char *ads_build_dn(const char *realm)
+/* return a ldap dn path from a string, given separators and field name
+   caller must free
+*/
+char *ads_build_path(const char *realm, const char *sep, const char *field, int reverse)
 {
        char *p, *r;
-       int numdots = 0;
+       int numbits = 0;
        char *ret;
        int len;
-       
-       r = strdup(realm);
+       char *saveptr;
 
-       if (!r || !*r) return r;
+       r = SMB_STRDUP(realm);
 
-       for (p=r; *p; p++) {
-               if (*p == '.') numdots++;
+       if (!r || !*r) {
+               return r;
        }
 
-       len = (numdots+1)*4 + strlen(r) + 1;
-
-       ret = malloc(len);
-       strlcpy(ret,"dc=", len);
-       p=strtok(r,"."); 
-       strlcat(ret, p, len);
-
-       while ((p=strtok(NULL,"."))) {
-               strlcat(ret,",dc=", len);
-               strlcat(ret, p, len);
+       for (p=r; *p; p++) {
+               if (strchr(sep, *p)) {
+                       numbits++;
+               }
        }
 
-       free(r);
-
-       return ret;
-}
-
-#ifdef HAVE_KRB5
+       len = (numbits+1)*(strlen(field)+1) + strlen(r) + 1;
 
-/*
-  get the default relm from krb5.conf
-*/
-static char *get_default_realm(ADS_STRUCT *ads)
-{
-       BOOL ret;
-       krb5_context context;
-       char *realm;
-
-       ret = krb5_init_context(&context);
-       if (ret) {
-               DEBUG(1,("krb5_init_context failed (%s)\n", error_message(ret)));
+       ret = (char *)SMB_MALLOC(len);
+       if (!ret) {
+               free(r);
                return NULL;
        }
 
-       ret = krb5_get_default_realm(context, &realm);
-       if (ret) {
-               DEBUG(1,("krb5_get_default_realm failed (%s)\n", error_message(ret)));
-               krb5_free_context(context);
+       if (strlcpy(ret,field, len) >= len) {
+               /* Truncate ! */
+               free(r);
                return NULL;
-       } else {
-               DEBUG(5,("krb5_get_default_realm got (%s)\n", realm));
        }
-       krb5_free_context(context);
-               
-       return realm;
+       p=strtok_r(r, sep, &saveptr);
+       if (p) {
+               if (strlcat(ret, p, len) >= len) {
+                       free(r);
+                       return NULL;
+               }
+
+               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(r);
+       return ret;
 }
 
-#else 
-static char *get_default_realm(ADS_STRUCT *ads)
+/* return a dn of the form "dc=AA,dc=BB,dc=CC" from a 
+   realm of the form AA.BB.CC 
+   caller must free
+*/
+char *ads_build_dn(const char *realm)
 {
-       /* We can't do this if we don't have krb5, 
-          but save linking nightmares */
-       DEBUG(5,("get_default_realm:  not compiled with krb5.\n"));
-       return NULL;
+       return ads_build_path(realm, ".", "dc=", 0);
 }
 
-#endif
-
-#ifdef HAVE_LDAP
-/*
-  find the ldap server from DNS
+/* return a DNS name in the for aa.bb.cc from the DN  
+   "dc=AA,dc=BB,dc=CC".  caller must free
 */
-static char *find_ldap_server(ADS_STRUCT *ads)
+char *ads_build_domain(const char *dn)
 {
-       char *list = NULL;
+       char *dnsdomain = NULL;
 
-       if (ldap_domain2hostlist(ads->realm, &list) == LDAP_SUCCESS) {
-               char *p;
-               p = strchr(list, ':');
-               if (p) *p = 0;
-               return list;
-       }
+       /* result should always be shorter than the DN */
 
-       return NULL;
-}
+       if ( (dnsdomain = SMB_STRDUP( dn )) == NULL ) {
+               DEBUG(0,("ads_build_domain: malloc() failed!\n"));              
+               return NULL;            
+       }       
 
-#else 
+       strlower_m( dnsdomain );        
+       all_string_sub( dnsdomain, "dc=", "", 0);
+       all_string_sub( dnsdomain, ",", ".", 0 );
 
-static char *find_ldap_server(ADS_STRUCT *ads)
-{
-       /* Without LDAP this doesn't make much sense */
-       return NULL;
+       return dnsdomain;       
 }
 
-#endif 
 
 
+#ifndef LDAP_PORT
+#define LDAP_PORT 389
+#endif
+
 /*
   initialise a ADS_STRUCT, ready for some ads_ ops
 */
 ADS_STRUCT *ads_init(const char *realm, 
-                    const char *ldap_server,
-                    const char *bind_path)
+                    const char *workgroup,
+                    const char *ldap_server)
 {
        ADS_STRUCT *ads;
-       
-       ads = (ADS_STRUCT *)smb_xmalloc(sizeof(*ads));
-       memset(ads, 0, sizeof(*ads));
-       
-       ads->realm = realm? strdup(realm) : NULL;
-       ads->ldap_server = ldap_server? strdup(ldap_server) : NULL;
-       ads->bind_path = bind_path? strdup(bind_path) : NULL;
-       ads->ldap_port = LDAP_PORT;
-
-       if (!ads->realm) {
-               ads->realm = lp_realm();
-               if (!ads->realm[0]) {
-                       ads->realm = get_default_realm(ads);
-               }
+       int wrap_flags;
+
+       ads = SMB_XMALLOC_P(ADS_STRUCT);
+       ZERO_STRUCTP(ads);
+
+       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 */
+       if (realm && *realm && !strequal(lp_realm(), realm)) {
+               ads->server.foreign = 1;
        }
-       if (!ads->bind_path) {
-               ads->bind_path = ads_build_dn(ads->realm);
+       if (workgroup && *workgroup && !strequal(lp_workgroup(), workgroup)) {
+               ads->server.foreign = 1;
        }
-       if (!ads->ldap_server) {
-               ads->ldap_server = lp_ads_server();
-               if (!ads->ldap_server[0]) {
-                       ads->ldap_server = find_ldap_server(ads);
-               }
-       }
-       if (!ads->kdc_server) {
-               /* assume its the same as LDAP */
-               ads->kdc_server = ads->ldap_server? strdup(ads->ldap_server) : NULL;
+
+       /* the caller will own the memory by default */
+       ads->is_mine = 1;
+
+       wrap_flags = lp_client_ldap_sasl_wrapping();
+       if (wrap_flags == -1) {
+               wrap_flags = 0;
        }
 
+       ads->auth.flags = wrap_flags;
+
        return ads;
 }
 
+/****************************************************************
+****************************************************************/
+
+bool ads_set_sasl_wrap_flags(ADS_STRUCT *ads, int flags)
+{
+       if (!ads) {
+               return false;
+       }
+
+       ads->auth.flags = flags;
+
+       return true;
+}
+
 /*
   free the memory used by the ADS structure initialized with 'ads_init(...)'
 */
 void ads_destroy(ADS_STRUCT **ads)
 {
-       if (False && (ads) && (*ads)) {
-               if ((*ads)->ld) ldap_unbind((*ads)->ld);
-               SAFE_FREE((*ads)->realm);
-               SAFE_FREE((*ads)->ldap_server);
-               SAFE_FREE((*ads)->kdc_server);
-               SAFE_FREE((*ads)->bind_path);
+       if (ads && *ads) {
+               bool is_mine;
+
+               is_mine = (*ads)->is_mine;
+#if HAVE_LDAP
+               ads_disconnect(*ads);
+#endif
+               SAFE_FREE((*ads)->server.realm);
+               SAFE_FREE((*ads)->server.workgroup);
+               SAFE_FREE((*ads)->server.ldap_server);
+
+               SAFE_FREE((*ads)->auth.realm);
+               SAFE_FREE((*ads)->auth.password);
+               SAFE_FREE((*ads)->auth.user_name);
+               SAFE_FREE((*ads)->auth.kdc_server);
+
+               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);
        }
 }
-