Add smbldap_pull_sid
[ira/wip.git] / source3 / libads / ldap.c
index 61e04d1e7c42acd618f393c4cbbb8021ddb72cac..3e5764a598b369082f367ba4c0a9a8f2aed745cb 100644 (file)
@@ -22,7 +22,7 @@
 */
 
 #include "includes.h"
-#include "lib/ldb/include/includes.h"
+#include "lib/ldb/include/ldb.h"
 
 #ifdef HAVE_LDAP
 
@@ -118,6 +118,16 @@ static int ldap_search_with_timeout(LDAP *ld,
        if (gotalarm != 0)
                return LDAP_TIMELIMIT_EXCEEDED;
 
+       /*
+        * A bug in OpenLDAP means ldap_search_ext_s can return
+        * LDAP_SUCCESS but with a NULL res pointer. Cope with
+        * this. See bug #6279 for details. JRA.
+        */
+
+       if (*res == NULL) {
+               return LDAP_TIMELIMIT_EXCEEDED;
+       }
+
        return result;
 }
 
@@ -162,6 +172,11 @@ bool ads_closest_dc(ADS_STRUCT *ads)
                return True;
        }
 
+       if (ads->config.client_site_name == NULL) {
+               DEBUG(10,("ads_closest_dc: client belongs to no site\n"));
+               return True;
+       }
+
        DEBUG(10,("ads_closest_dc: %s is not the closest DC\n", 
                ads->config.ldap_server_name));
 
@@ -576,9 +591,20 @@ ADS_STATUS ads_connect(ADS_STRUCT *ads)
                TALLOC_FREE(s);
        }
 
-       if (ads->server.ldap_server &&
-           ads_try_connect(ads, ads->server.ldap_server, ads->server.gc)) {
-               goto got_connection;
+       if (ads->server.ldap_server)
+       {
+               if (ads_try_connect(ads, ads->server.ldap_server, ads->server.gc)) {
+                       goto got_connection;
+               }
+
+               /* The choice of which GC use is handled one level up in
+                  ads_connect_gc().  If we continue on from here with
+                  ads_find_dc() we will get GC searches on port 389 which
+                  doesn't work.   --jerry */
+
+               if (ads->server.gc == true) {
+                       return ADS_ERROR(LDAP_OPERATIONS_ERROR);
+               }
        }
 
        ntstatus = ads_find_dc(ads);
@@ -598,7 +624,10 @@ got_connection:
                /* Must use the userPrincipalName value here or sAMAccountName
                   and not servicePrincipalName; found by Guenther Deschner */
 
-               asprintf(&ads->auth.user_name, "%s$", global_myname() );
+               if (asprintf(&ads->auth.user_name, "%s$", global_myname() ) == -1) {
+                       DEBUG(0,("ads_connect: asprintf fail.\n"));
+                       ads->auth.user_name = NULL;
+               }
        }
 
        if (!ads->auth.realm) {
@@ -614,10 +643,11 @@ got_connection:
        /* this is a really nasty hack to avoid ADS DNS problems. It needs a patch
           to MIT kerberos to work (tridge) */
        {
-               char *env;
-               asprintf(&env, "KRB5_KDC_ADDRESS_%s", ads->config.realm);
-               setenv(env, ads->auth.kdc_server, 1);
-               free(env);
+               char *env = NULL;
+               if (asprintf(&env, "KRB5_KDC_ADDRESS_%s", ads->config.realm) > 0) {
+                       setenv(env, ads->auth.kdc_server, 1);
+                       free(env);
+               }
        }
 #endif
 
@@ -646,16 +676,17 @@ got_connection:
 
        /* cache the successful connection for workgroup and realm */
        if (ads_closest_dc(ads)) {
-               print_sockaddr(addr, sizeof(addr), &ads->ldap.ss);
-               saf_store( ads->server.workgroup, addr);
-               saf_store( ads->server.realm, addr);
+               saf_store( ads->server.workgroup, ads->config.ldap_server_name);
+               saf_store( ads->server.realm, ads->config.ldap_server_name);
        }
 
        ldap_set_option(ads->ldap.ld, LDAP_OPT_PROTOCOL_VERSION, &version);
 
-       status = ADS_ERROR(smb_ldap_start_tls(ads->ldap.ld, version));
-       if (!ADS_ERR_OK(status)) {
-               goto out;
+       if ( lp_ldap_ssl_ads() ) {
+               status = ADS_ERROR(smb_ldap_start_tls(ads->ldap.ld, version));
+               if (!ADS_ERR_OK(status)) {
+                       goto out;
+               }
        }
 
        /* fill in the current time and offsets */
@@ -1245,23 +1276,13 @@ ADS_STATUS ads_do_search_all_fn(ADS_STRUCT *ads, const char *bind_path,
        ldap_msgfree(msg);
 }
 
-/**
- * Free up memory from various ads requests
- * @param ads connection to ads server
- * @param mem Area to free
- **/
-void ads_memfree(ADS_STRUCT *ads, void *mem)
-{
-       SAFE_FREE(mem);
-}
-
 /**
  * Get a dn from search results
  * @param ads connection to ads server
  * @param msg Search result
  * @return dn string
  **/
- char *ads_get_dn(ADS_STRUCT *ads, LDAPMessage *msg)
+ char *ads_get_dn(ADS_STRUCT *ads, TALLOC_CTX *mem_ctx, LDAPMessage *msg)
 {
        char *utf8_dn, *unix_dn;
        size_t converted_size;
@@ -1273,7 +1294,7 @@ void ads_memfree(ADS_STRUCT *ads, void *mem)
                return NULL;
        }
 
-       if (!pull_utf8_allocate(&unix_dn, utf8_dn, &converted_size)) {
+       if (!pull_utf8_talloc(mem_ctx, &unix_dn, utf8_dn, &converted_size)) {
                DEBUG(0,("ads_get_dn: string conversion failure utf8 [%s]\n",
                        utf8_dn ));
                return NULL;
@@ -1489,7 +1510,7 @@ ADS_STATUS ads_gen_mod(ADS_STRUCT *ads, const char *mod_dn, ADS_MODLIST mods)
        controls[0] = &PermitModify;
        controls[1] = NULL;
 
-       if (!push_utf8_allocate(&utf8_dn, mod_dn, &converted_size)) {
+       if (!push_utf8_talloc(talloc_tos(), &utf8_dn, mod_dn, &converted_size)) {
                return ADS_ERROR_NT(NT_STATUS_NO_MEMORY);
        }
 
@@ -1499,7 +1520,7 @@ ADS_STATUS ads_gen_mod(ADS_STRUCT *ads, const char *mod_dn, ADS_MODLIST mods)
        mods[i] = NULL;
        ret = ldap_modify_ext_s(ads->ldap.ld, utf8_dn,
                                (LDAPMod **) mods, controls, NULL);
-       SAFE_FREE(utf8_dn);
+       TALLOC_FREE(utf8_dn);
        return ADS_ERROR(ret);
 }
 
@@ -1516,8 +1537,8 @@ ADS_STATUS ads_gen_add(ADS_STRUCT *ads, const char *new_dn, ADS_MODLIST mods)
        char *utf8_dn = NULL;
        size_t converted_size;
 
-       if (!push_utf8_allocate(&utf8_dn, new_dn, &converted_size)) {
-               DEBUG(1, ("ads_gen_add: push_utf8_allocate failed!"));
+       if (!push_utf8_talloc(talloc_tos(), &utf8_dn, new_dn, &converted_size)) {
+               DEBUG(1, ("ads_gen_add: push_utf8_talloc failed!"));
                return ADS_ERROR_NT(NT_STATUS_NO_MEMORY);
        }
        
@@ -1527,7 +1548,7 @@ ADS_STATUS ads_gen_add(ADS_STRUCT *ads, const char *new_dn, ADS_MODLIST mods)
        mods[i] = NULL;
 
        ret = ldap_add_s(ads->ldap.ld, utf8_dn, (LDAPMod**)mods);
-       SAFE_FREE(utf8_dn);
+       TALLOC_FREE(utf8_dn);
        return ADS_ERROR(ret);
 }
 
@@ -1542,13 +1563,13 @@ ADS_STATUS ads_del_dn(ADS_STRUCT *ads, char *del_dn)
        int ret;
        char *utf8_dn = NULL;
        size_t converted_size;
-       if (!push_utf8_allocate(&utf8_dn, del_dn, &converted_size)) {
-               DEBUG(1, ("ads_del_dn: push_utf8_allocate failed!"));
+       if (!push_utf8_talloc(talloc_tos(), &utf8_dn, del_dn, &converted_size)) {
+               DEBUG(1, ("ads_del_dn: push_utf8_talloc failed!"));
                return ADS_ERROR_NT(NT_STATUS_NO_MEMORY);
        }
        
        ret = ldap_delete_s(ads->ldap.ld, utf8_dn);
-       SAFE_FREE(utf8_dn);
+       TALLOC_FREE(utf8_dn);
        return ADS_ERROR(ret);
 }
 
@@ -1618,7 +1639,7 @@ char *ads_default_ou_string(ADS_STRUCT *ads, const char *wknguid)
        }
 
        /* substitute the bind-path from the well-known-guid-search result */
-       wkn_dn = ads_get_dn(ads, res);
+       wkn_dn = ads_get_dn(ads, talloc_tos(), res);
        if (!wkn_dn) {
                goto out;
        }
@@ -1664,7 +1685,7 @@ char *ads_default_ou_string(ADS_STRUCT *ads, const char *wknguid)
  out:
        SAFE_FREE(base);
        ads_msgfree(ads, res);
-       ads_memfree(ads, wkn_dn);
+       TALLOC_FREE(wkn_dn);
        if (wkn_dn_exp) {
                ldap_value_free(wkn_dn_exp);
        }
@@ -1720,14 +1741,14 @@ uint32 ads_get_kvno(ADS_STRUCT *ads, const char *account_name)
                return kvno;
        }
 
-       dn_string = ads_get_dn(ads, res);
+       dn_string = ads_get_dn(ads, talloc_tos(), res);
        if (!dn_string) {
                DEBUG(0,("ads_get_kvno: out of memory.\n"));
                ads_msgfree(ads, res);
                return kvno;
        }
        DEBUG(5,("ads_get_kvno: Using: %s\n", dn_string));
-       ads_memfree(ads, dn_string);
+       TALLOC_FREE(dn_string);
 
        /* ---------------------------------------------------------
         * 0 is returned as a default KVNO from this point on...
@@ -1815,14 +1836,14 @@ ADS_STATUS ads_clear_service_principal_names(ADS_STRUCT *ads, const char *machin
                talloc_destroy(ctx);
                return ret;
        }
-       dn_string = ads_get_dn(ads, res);
+       dn_string = ads_get_dn(ads, talloc_tos(), res);
        if (!dn_string) {
                talloc_destroy(ctx);
                ads_msgfree(ads, res);
                return ADS_ERROR(LDAP_NO_MEMORY);
        }
        ret = ads_gen_mod(ads, dn_string, mods);
-       ads_memfree(ads,dn_string);
+       TALLOC_FREE(dn_string);
        if (!ADS_ERR_OK(ret)) {
                DEBUG(1,("ads_clear_service_principal_names: Error: Updating Service Principals for machine %s in LDAP\n",
                        machine_name));
@@ -1912,13 +1933,12 @@ ADS_STATUS ads_add_service_principal_name(ADS_STRUCT *ads, const char *machine_n
                goto out;
        }
        
-       if ( (dn_string = ads_get_dn(ads, res)) == NULL ) {
+       if ( (dn_string = ads_get_dn(ads, ctx, res)) == NULL ) {
                ret = ADS_ERROR(LDAP_NO_MEMORY);
                goto out;
        }
        
        ret = ads_gen_mod(ads, dn_string, mods);
-       ads_memfree(ads,dn_string);
        if (!ADS_ERR_OK(ret)) {
                DEBUG(1,("ads_add_service_principal_name: Error: Updating Service Principals in LDAP\n"));
                goto out;
@@ -2031,7 +2051,7 @@ ADS_STATUS ads_move_machine_acct(ADS_STRUCT *ads, const char *machine_name,
                goto done;
        }
 
-       computer_dn = ads_get_dn(ads, res);
+       computer_dn = ads_get_dn(ads, talloc_tos(), res);
        if (!computer_dn) {
                rc = ADS_ERROR(LDAP_NO_MEMORY);
                goto done;
@@ -2599,19 +2619,7 @@ int ads_count_replies(ADS_STRUCT *ads, void *res)
  bool ads_pull_sid(ADS_STRUCT *ads, LDAPMessage *msg, const char *field,
                   DOM_SID *sid)
 {
-       struct berval **values;
-       bool ret = False;
-
-       values = ldap_get_values_len(ads->ldap.ld, msg, field);
-
-       if (!values)
-               return False;
-
-       if (values[0])
-               ret = sid_parse(values[0]->bv_val, values[0]->bv_len, sid);
-       
-       ldap_value_free_len(values);
-       return ret;
+       return smbldap_pull_sid(ads->ldap.ld, msg, field, sid);
 }
 
 /**
@@ -3008,7 +3016,7 @@ ADS_STATUS ads_site_dn_for_machine(ADS_STRUCT *ads, TALLOC_CTX *mem_ctx, const c
                return ADS_ERROR(LDAP_NO_SUCH_OBJECT);
        }
 
-       dn = ads_get_dn(ads, res);
+       dn = ads_get_dn(ads, mem_ctx, res);
        if (dn == NULL) {
                ads_msgfree(ads, res);
                return ADS_ERROR(LDAP_NO_MEMORY);
@@ -3018,18 +3026,18 @@ ADS_STATUS ads_site_dn_for_machine(ADS_STRUCT *ads, TALLOC_CTX *mem_ctx, const c
        parent = ads_parent_dn(ads_parent_dn(ads_parent_dn(dn)));
        if (parent == NULL) {
                ads_msgfree(ads, res);
-               ads_memfree(ads, dn);
+               TALLOC_FREE(dn);
                return ADS_ERROR(LDAP_NO_MEMORY);
        }
 
        *site_dn = talloc_strdup(mem_ctx, parent);
        if (*site_dn == NULL) {
                ads_msgfree(ads, res);
-               ads_memfree(ads, dn);
+               TALLOC_FREE(dn);
                return ADS_ERROR(LDAP_NO_MEMORY);
        }
 
-       ads_memfree(ads, dn);
+       TALLOC_FREE(dn);
        ads_msgfree(ads, res);
 
        return status;
@@ -3119,7 +3127,7 @@ ADS_STATUS ads_get_joinable_ous(ADS_STRUCT *ads,
 
                char *dn = NULL;
 
-               dn = ads_get_dn(ads, msg);
+               dn = ads_get_dn(ads, talloc_tos(), msg);
                if (!dn) {
                        ads_msgfree(ads, res);
                        return ADS_ERROR(LDAP_NO_MEMORY);
@@ -3128,12 +3136,12 @@ ADS_STATUS ads_get_joinable_ous(ADS_STRUCT *ads,
                if (!add_string_to_array(mem_ctx, dn,
                                         (const char ***)ous,
                                         (int *)num_ous)) {
-                       ads_memfree(ads, dn);
+                       TALLOC_FREE(dn);
                        ads_msgfree(ads, res);
                        return ADS_ERROR(LDAP_NO_MEMORY);
                }
 
-               ads_memfree(ads, dn);
+               TALLOC_FREE(dn);
        }
 
        ads_msgfree(ads, res);
@@ -3473,7 +3481,7 @@ ADS_STATUS ads_leave_realm(ADS_STRUCT *ads, const char *hostname)
                return ADS_ERROR_SYSTEM(ENOENT);
        }
 
-       hostnameDN = ads_get_dn(ads, (LDAPMessage *)msg);
+       hostnameDN = ads_get_dn(ads, talloc_tos(), (LDAPMessage *)msg);
 
        rc = ldap_delete_ext_s(ads->ldap.ld, hostnameDN, pldap_control, NULL);
        if (rc) {
@@ -3495,7 +3503,7 @@ ADS_STATUS ads_leave_realm(ADS_STRUCT *ads, const char *hostname)
 
                if (!ADS_ERR_OK(status)) {
                        SAFE_FREE(host);
-                       ads_memfree(ads, hostnameDN);
+                       TALLOC_FREE(hostnameDN);
                        return status;
                }
 
@@ -3504,9 +3512,9 @@ ADS_STATUS ads_leave_realm(ADS_STRUCT *ads, const char *hostname)
 
                        char *dn = NULL;
 
-                       if ((dn = ads_get_dn(ads, msg_sub)) == NULL) {
+                       if ((dn = ads_get_dn(ads, talloc_tos(), msg_sub)) == NULL) {
                                SAFE_FREE(host);
-                               ads_memfree(ads, hostnameDN);
+                               TALLOC_FREE(hostnameDN);
                                return ADS_ERROR(LDAP_NO_MEMORY);
                        }
 
@@ -3514,12 +3522,12 @@ ADS_STATUS ads_leave_realm(ADS_STRUCT *ads, const char *hostname)
                        if (!ADS_ERR_OK(status)) {
                                DEBUG(3,("failed to delete dn %s: %s\n", dn, ads_errstr(status)));
                                SAFE_FREE(host);
-                               ads_memfree(ads, dn);
-                               ads_memfree(ads, hostnameDN);
+                               TALLOC_FREE(dn);
+                               TALLOC_FREE(hostnameDN);
                                return status;
                        }
 
-                       ads_memfree(ads, dn);
+                       TALLOC_FREE(dn);
                }
 
                /* there should be no subordinate objects anymore */
@@ -3529,7 +3537,7 @@ ADS_STATUS ads_leave_realm(ADS_STRUCT *ads, const char *hostname)
 
                if (!ADS_ERR_OK(status) || ( (ads_count_replies(ads, res)) > 0 ) ) {
                        SAFE_FREE(host);
-                       ads_memfree(ads, hostnameDN);
+                       TALLOC_FREE(hostnameDN);
                        return status;
                }
 
@@ -3538,12 +3546,12 @@ ADS_STATUS ads_leave_realm(ADS_STRUCT *ads, const char *hostname)
                if (!ADS_ERR_OK(status)) {
                        SAFE_FREE(host);
                        DEBUG(3,("failed to delete dn %s: %s\n", hostnameDN, ads_errstr(status)));
-                       ads_memfree(ads, hostnameDN);
+                       TALLOC_FREE(hostnameDN);
                        return status;
                }
        }
 
-       ads_memfree(ads, hostnameDN);
+       TALLOC_FREE(hostnameDN);
 
        status = ads_find_machine_acct(ads, &res, host);
        if (ADS_ERR_OK(status) && ads_count_replies(ads, res) == 1) {
@@ -3702,7 +3710,7 @@ ADS_STATUS ads_find_samaccount(ADS_STRUCT *ads,
                goto out;
        }
 
-       dn = ads_get_dn(ads, res);
+       dn = ads_get_dn(ads, talloc_tos(), res);
        if (dn == NULL) {
                status = ADS_ERROR(LDAP_NO_MEMORY);
                goto out;
@@ -3725,7 +3733,7 @@ ADS_STATUS ads_find_samaccount(ADS_STRUCT *ads,
                }
        }
  out:
-       ads_memfree(ads, dn);
+       TALLOC_FREE(dn);
        ads_msgfree(ads, res);
 
        return status;
@@ -3838,34 +3846,36 @@ ADS_STATUS ads_check_ou_dn(TALLOC_CTX *mem_ctx,
        struct ldb_dn *name_dn = NULL;
        const char *name = NULL;
        char *ou_string = NULL;
+       struct ldb_context *ldb = ldb_init(mem_ctx, NULL);
 
-       name_dn = ldb_dn_explode(mem_ctx, *account_ou);
-       if (name_dn) {
+       name_dn = ldb_dn_new(mem_ctx, ldb, *account_ou);
+       if (name_dn && ldb_dn_validate(name_dn)) {
+               talloc_free(ldb);
                return ADS_SUCCESS;
        }
 
        ou_string = ads_ou_string(ads, *account_ou);
        if (!ou_string) {
+               talloc_free(ldb);
                return ADS_ERROR_LDAP(LDAP_INVALID_DN_SYNTAX);
        }
 
-       name = talloc_asprintf(mem_ctx, "%s,%s", ou_string,
-                              ads->config.bind_path);
+       name_dn = ldb_dn_new_fmt(mem_ctx, ldb, "%s,%s", ou_string,
+                                ads->config.bind_path);
        SAFE_FREE(ou_string);
-       if (!name) {
-               return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
-       }
 
-       name_dn = ldb_dn_explode(mem_ctx, name);
-       if (!name_dn) {
+       if (!name_dn || !ldb_dn_validate(name_dn)) {
+               talloc_free(ldb);
                return ADS_ERROR_LDAP(LDAP_INVALID_DN_SYNTAX);
        }
 
        *account_ou = talloc_strdup(mem_ctx, name);
        if (!*account_ou) {
+               talloc_free(ldb);
                return ADS_ERROR_LDAP(LDAP_NO_MEMORY);
        }
 
+       talloc_free(ldb);
        return ADS_SUCCESS;
 }