s3:libads Make ads_get_dn() take a talloc context
authorAndrew Bartlett <abartlet@samba.org>
Wed, 18 Mar 2009 06:35:03 +0000 (17:35 +1100)
committerGünther Deschner <gd@samba.org>
Mon, 6 Apr 2009 13:54:41 +0000 (15:54 +0200)
Also remove ads_memfree(), which was only ever a wrapper around
SAFE_FREE, used only to free the DN from ads_get_ds().

This actually makes libgpo more consistant, as it mixed a talloc and a
malloc based string on the same element.

Andrew Bartlett

Signed-off-by: Günther Deschner <gd@samba.org>
source3/include/ads_protos.h
source3/libads/ldap.c
source3/libgpo/gpo_ldap.c
source3/libnet/libnet_join.c
source3/printing/nt_printing.c
source3/utils/net_ads.c
source3/utils/net_ads_gpo.c
source3/winbindd/idmap_adex/gc_util.c
source3/winbindd/idmap_adex/likewise_cell.c
source3/winbindd/idmap_adex/provider_unified.c
source3/winbindd/winbindd_ads.c

index a372010b796e91c0231381975db1105c0b9ebabc..502eaa82d9756d5d2f95eea509ebcff0f57c127e 100644 (file)
@@ -3,7 +3,7 @@
  */
 
 void ads_msgfree(ADS_STRUCT *ads, LDAPMessage *msg);
-char *ads_get_dn(ADS_STRUCT *ads, LDAPMessage *msg);
+char *ads_get_dn(ADS_STRUCT *ads, TALLOC_CTX *mem_ctx, LDAPMessage *msg);
 char *ads_get_dn_canonical(ADS_STRUCT *ads, LDAPMessage *msg);
 
 char *ads_pull_string(ADS_STRUCT *ads, TALLOC_CTX *mem_ctx, LDAPMessage *msg,
index edbd69392c7a2d0f42d1463c0a3490e036ab9709..d2743772590c58ffa5e5f9ba4b56bc9580807869 100644 (file)
@@ -1266,23 +1266,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;
@@ -1294,7 +1284,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;
@@ -1639,7 +1629,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, NULL, res);
        if (!wkn_dn) {
                goto out;
        }
@@ -1685,7 +1675,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);
        }
@@ -1741,14 +1731,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, NULL, 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...
@@ -1836,14 +1826,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, NULL, 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));
@@ -1933,13 +1923,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;
@@ -2052,7 +2041,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, NULL, res);
        if (!computer_dn) {
                rc = ADS_ERROR(LDAP_NO_MEMORY);
                goto done;
@@ -3029,7 +3018,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);
@@ -3039,18 +3028,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;
@@ -3140,7 +3129,7 @@ ADS_STATUS ads_get_joinable_ous(ADS_STRUCT *ads,
 
                char *dn = NULL;
 
-               dn = ads_get_dn(ads, msg);
+               dn = ads_get_dn(ads, NULL, msg);
                if (!dn) {
                        ads_msgfree(ads, res);
                        return ADS_ERROR(LDAP_NO_MEMORY);
@@ -3149,12 +3138,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);
@@ -3494,7 +3483,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, NULL, (LDAPMessage *)msg);
 
        rc = ldap_delete_ext_s(ads->ldap.ld, hostnameDN, pldap_control, NULL);
        if (rc) {
@@ -3516,7 +3505,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;
                }
 
@@ -3525,9 +3514,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, NULL, msg_sub)) == NULL) {
                                SAFE_FREE(host);
-                               ads_memfree(ads, hostnameDN);
+                               TALLOC_FREE(hostnameDN);
                                return ADS_ERROR(LDAP_NO_MEMORY);
                        }
 
@@ -3535,12 +3524,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 */
@@ -3550,7 +3539,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;
                }
 
@@ -3559,12 +3548,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) {
@@ -3723,7 +3712,7 @@ ADS_STATUS ads_find_samaccount(ADS_STRUCT *ads,
                goto out;
        }
 
-       dn = ads_get_dn(ads, res);
+       dn = ads_get_dn(ads, NULL, res);
        if (dn == NULL) {
                status = ADS_ERROR(LDAP_NO_MEMORY);
                goto out;
@@ -3746,7 +3735,7 @@ ADS_STATUS ads_find_samaccount(ADS_STRUCT *ads,
                }
        }
  out:
-       ads_memfree(ads, dn);
+       TALLOC_FREE(dn);
        ads_msgfree(ads, res);
 
        return status;
index 26813864e5601d2b8b3c480536296217d48a5b73..882804b09a8b666a4cd6c0c25fe5eea4ac91e497 100644 (file)
@@ -415,7 +415,7 @@ ADS_STATUS ads_delete_gpo_link(ADS_STRUCT *ads,
        if (gpo_dn) {
                gpo->ds_path = talloc_strdup(mem_ctx, gpo_dn);
        } else {
-               gpo->ds_path = ads_get_dn(ads, res);
+               gpo->ds_path = ads_get_dn(ads, mem_ctx, res);
        }
 
        ADS_ERROR_HAVE_NO_MEMORY(gpo->ds_path);
@@ -523,7 +523,7 @@ ADS_STATUS ads_get_gpo(ADS_STRUCT *ads,
                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);
@@ -531,7 +531,7 @@ ADS_STATUS ads_get_gpo(ADS_STRUCT *ads,
 
        status = ads_parse_gpo(ads, mem_ctx, res, dn, gpo);
        ads_msgfree(ads, res);
-       ads_memfree(ads, dn);
+       TALLOC_FREE(dn);
 
        return status;
 }
index ccfd943a8de1c61aa4b3dab2ab8fff15570dfb04..fe9fd66fdc24bfca36d81d678734f880b3bfe593 100644 (file)
@@ -303,7 +303,7 @@ static ADS_STATUS libnet_join_find_machine_acct(TALLOC_CTX *mem_ctx,
                goto done;
        }
 
-       dn = ads_get_dn(r->in.ads, res);
+       dn = ads_get_dn(r->in.ads, mem_ctx, res);
        if (!dn) {
                status = ADS_ERROR_LDAP(LDAP_NO_MEMORY);
                goto done;
@@ -317,7 +317,7 @@ static ADS_STATUS libnet_join_find_machine_acct(TALLOC_CTX *mem_ctx,
 
  done:
        ads_msgfree(r->in.ads, res);
-       ads_memfree(r->in.ads, dn);
+       TALLOC_FREE(dn);
 
        return status;
 }
index a219380177b0ae053f43bc8cc14722f2a356d6e4..40d7cd6347d619be3693ba714bdca6111e7d7a21 100644 (file)
@@ -3357,13 +3357,13 @@ static WERROR nt_printer_unpublish_ads(ADS_STRUCT *ads,
                            printer->info_2->sharename, global_myname());
 
        if (ADS_ERR_OK(ads_rc) && res && ads_count_replies(ads, res)) {
-               prt_dn = ads_get_dn(ads, res);
+               prt_dn = ads_get_dn(ads, NULL, res);
                if (!prt_dn) {
                        ads_msgfree(ads, res);
                        return WERR_NOMEM;
                }
                ads_rc = ads_del_dn(ads, prt_dn);
-               ads_memfree(ads, prt_dn);
+               TALLOC_FREE(prt_dn);
        }
 
        if (res) {
index 2a666194381cd8fa3da5941ac893023485125526..8ff0cdfcf428f8ea31590001887a53f1af94ce43 100644 (file)
@@ -503,9 +503,9 @@ static int ads_user_add(struct net_context *c, int argc, const char **argv)
        ads_msgfree(ads, res);
        status=ads_find_user_acct(ads, &res, argv[0]);
        if (ADS_ERR_OK(status)) {
-               userdn = ads_get_dn(ads, res);
+               userdn = ads_get_dn(ads, NULL, res);
                ads_del_dn(ads, userdn);
-               ads_memfree(ads, userdn);
+               TALLOC_FREE(userdn);
        }
 
  done:
@@ -598,10 +598,10 @@ static int ads_user_delete(struct net_context *c, int argc, const char **argv)
                ads_destroy(&ads);
                return -1;
        }
-       userdn = ads_get_dn(ads, res);
+       userdn = ads_get_dn(ads, NULL, res);
        ads_msgfree(ads, res);
        rc = ads_del_dn(ads, userdn);
-       ads_memfree(ads, userdn);
+       TALLOC_FREE(userdn);
        if (ADS_ERR_OK(rc)) {
                d_printf("User %s deleted\n", argv[0]);
                ads_destroy(&ads);
@@ -757,10 +757,10 @@ static int ads_group_delete(struct net_context *c, int argc, const char **argv)
                ads_destroy(&ads);
                return -1;
        }
-       groupdn = ads_get_dn(ads, res);
+       groupdn = ads_get_dn(ads, NULL, res);
        ads_msgfree(ads, res);
        rc = ads_del_dn(ads, groupdn);
-       ads_memfree(ads, groupdn);
+       TALLOC_FREE(groupdn);
        if (ADS_ERR_OK(rc)) {
                d_printf("Group %s deleted\n", argv[0]);
                ads_destroy(&ads);
@@ -1740,10 +1740,10 @@ static int net_ads_printer_remove(struct net_context *c, int argc, const char **
                return -1;
        }
 
-       prt_dn = ads_get_dn(ads, res);
+       prt_dn = ads_get_dn(ads, NULL, res);
        ads_msgfree(ads, res);
        rc = ads_del_dn(ads, prt_dn);
-       ads_memfree(ads, prt_dn);
+       TALLOC_FREE(prt_dn);
 
        if (!ADS_ERR_OK(rc)) {
                d_fprintf(stderr, "ads_del_dn: %s\n", ads_errstr(rc));
index 181cba221d82c92763dcc59c76e98c7454afa7bc..910c78d05a0e13a65c4c2bd2636c8028d2b5ba2a 100644 (file)
@@ -257,7 +257,7 @@ static int net_ads_gpo_list_all(struct net_context *c, int argc, const char **ar
             msg;
             msg = ads_next_entry(ads, msg)) {
 
-               if ((dn = ads_get_dn(ads, msg)) == NULL) {
+               if ((dn = ads_get_dn(ads, mem_ctx, msg)) == NULL) {
                        goto out;
                }
 
@@ -266,18 +266,16 @@ static int net_ads_gpo_list_all(struct net_context *c, int argc, const char **ar
                if (!ADS_ERR_OK(status)) {
                        d_printf("ads_parse_gpo failed: %s\n",
                                ads_errstr(status));
-                       ads_memfree(ads, dn);
                        goto out;
                }
 
                dump_gpo(ads, mem_ctx, &gpo, 0);
-               ads_memfree(ads, dn);
        }
 
 out:
        ads_msgfree(ads, res);
 
-       talloc_destroy(mem_ctx);
+       TALLOC_FREE(mem_ctx);
        ads_destroy(&ads);
 
        return 0;
index 6dc02336d5d6b290f5d24002cdcb3e06a4933713..58e641b630199b1d1337f03b07a6f378adabb1fc 100644 (file)
@@ -592,11 +592,11 @@ done:
                while (e) {
                        struct winbindd_tdc_domain *domain_rec;
 
-                       dn = ads_get_dn(ads, e);
+                       dn = ads_get_dn(ads, frame, e);
                        BAIL_ON_PTR_ERROR(dn, nt_status);
 
                        dns_domain = cell_dn_to_dns(dn);
-                       SAFE_FREE(dn);
+                       TALLOC_FREE(dn);
                        BAIL_ON_PTR_ERROR(dns_domain, nt_status);
 
                        domain_rec = wcache_tdc_fetch_domain(frame, dns_domain);
@@ -666,13 +666,13 @@ static NTSTATUS get_object_account_name(ADS_STRUCT *ads,
 
        /* get the name and domain */
 
-       dn = ads_get_dn(ads, msg);
+       dn = ads_get_dn(ads, frame, msg);
        BAIL_ON_PTR_ERROR(dn, nt_status);
 
        DEBUG(10,("get_object_account_name: dn = \"%s\"\n", dn));
 
        dns_domain = cell_dn_to_dns(dn);
-       SAFE_FREE(dn);
+       TALLOC_FREE(dn);
        BAIL_ON_PTR_ERROR(dns_domain, nt_status);
 
        domain_rec = wcache_tdc_fetch_domain(frame, dns_domain);
index 7723b3e0153fb490363db30042b49a5bac243cfc..7354a367b8fbc515afc6358b47080a5eb8446a3a 100644 (file)
@@ -400,10 +400,10 @@ done:
                                     e!=NULL;
                                     e = ads_next_entry(c->conn, e))
                                {
-                                       char *dn = ads_get_dn(c->conn, e);
+                                       char *dn = ads_get_dn(c->conn, NULL, e);
 
                                        DEBUGADD(10,("   dn: %s\n", dn ? dn : "<NULL>"));
-                                       SAFE_FREE(dn);
+                                       TALLOC_FREE(dn);
                                }
                        }
 
index f18534797e7e8ca4e49fa130219d24dd5fd500a4..9134eb4e645a400739610265b8a5f4d167f39ee7 100644 (file)
@@ -368,7 +368,7 @@ static NTSTATUS check_result_unique_scoped(ADS_STRUCT **ads_list,
                LDAPMessage *e = ads_first_entry(ads_list[i], msg_list[i]);
 
                while (e) {
-                       entry_dn = ads_get_dn(ads_list[i], e);
+                       entry_dn = ads_get_dn(ads_list[i], NULL, e);
                        BAIL_ON_PTR_ERROR(entry_dn, nt_status);
 
                        if (check_forest_scope(entry_dn)) {
@@ -389,7 +389,7 @@ static NTSTATUS check_result_unique_scoped(ADS_STRUCT **ads_list,
                        }
 
                        e = ads_next_entry(ads_list[i], e);
-                       SAFE_FREE(entry_dn);
+                       TALLOC_FREE(entry_dn);
                }
        }
 
@@ -439,7 +439,7 @@ done:
        }
 
        talloc_destroy(frame);
-       SAFE_FREE(entry_dn);
+       TALLOC_FREE(entry_dn);
 
        return nt_status;
 }
index a76faa7a25298cb1dbfa0c99bd3aa0ce1d7d3c0c..dcf5623d29aa353a5a64ab1710fc1c27731932e5 100644 (file)
@@ -865,7 +865,7 @@ static NTSTATUS lookup_usergroups(struct winbindd_domain *domain,
                goto done;
        }
 
-       user_dn = ads_get_dn(ads, msg);
+       user_dn = ads_get_dn(ads, mem_ctx, msg);
        if (user_dn == NULL) {
                status = NT_STATUS_NO_MEMORY;
                goto done;
@@ -942,7 +942,7 @@ static NTSTATUS lookup_usergroups(struct winbindd_domain *domain,
        DEBUG(3,("ads lookup_usergroups (tokenGroups) succeeded for sid=%s\n",
                 sid_string_dbg(sid)));
 done:
-       ads_memfree(ads, user_dn);
+       TALLOC_FREE(user_dn);
        ads_msgfree(ads, msg);
        return status;
 }