From d0dc0171ad6abd969e834f8eb2bbfc08bef4547a Mon Sep 17 00:00:00 2001 From: Samuel Cabrero Date: Thu, 26 May 2022 17:28:34 +0200 Subject: [PATCH] s3:libads: Allocate ADS_STRUCT under a talloc context The ads_destroy() function is now static and only called from the ADS_STRUCT destructor. Signed-off-by: Samuel Cabrero Reviewed-by: Jeremy Allison --- libgpo/pygpo.c | 11 +++-- source3/lib/netapi/joindomain.c | 7 +--- source3/libads/ads_proto.h | 4 +- source3/libads/ads_struct.c | 32 +++++++-------- source3/libads/ldap.c | 18 +++----- source3/libads/ldap_utils.c | 6 +-- source3/libnet/libnet_join.c | 21 ++++------ source3/libnet/libnet_keytab.c | 4 +- source3/libsmb/namequery_dc.c | 9 +--- source3/printing/nt_printing_ads.c | 26 +++++++----- source3/utils/net_ads.c | 66 +++++++++--------------------- source3/utils/net_ads_gpo.c | 6 --- source3/utils/net_ads_join_dns.c | 9 ++-- source3/winbindd/idmap_rfc2307.c | 7 +--- source3/winbindd/winbindd_ads.c | 16 ++------ source3/winbindd/winbindd_cm.c | 5 +-- 16 files changed, 94 insertions(+), 153 deletions(-) diff --git a/libgpo/pygpo.c b/libgpo/pygpo.c index 221900e62fe..710f7fa896d 100644 --- a/libgpo/pygpo.c +++ b/libgpo/pygpo.c @@ -142,7 +142,7 @@ typedef struct { static void py_ads_dealloc(ADS* self) { - ads_destroy(&(self->ads_ptr)); + TALLOC_FREE(self->ads_ptr); Py_CLEAR(self->py_creds); Py_TYPE(self)->tp_free((PyObject*)self); } @@ -207,11 +207,14 @@ static int py_ads_init(ADS *self, PyObject *args, PyObject *kwds) /* in case __init__ is called more than once */ if (self->ads_ptr) { - ads_destroy(&self->ads_ptr); - self->ads_ptr = NULL; + TALLOC_FREE(self->ads_ptr); } /* always succeeds or crashes */ - self->ads_ptr = ads_init(realm, workgroup, ldap_server, ADS_SASL_PLAIN); + self->ads_ptr = ads_init(pytalloc_get_mem_ctx(args), + realm, + workgroup, + ldap_server, + ADS_SASL_PLAIN); return 0; } diff --git a/source3/lib/netapi/joindomain.c b/source3/lib/netapi/joindomain.c index 7eba430d8a0..7438e8407be 100644 --- a/source3/lib/netapi/joindomain.c +++ b/source3/lib/netapi/joindomain.c @@ -424,7 +424,8 @@ WERROR NetGetJoinableOUs_l(struct libnetapi_ctx *ctx, dc = strip_hostname(info->dc_unc); - ads = ads_init(info->domain_name, + ads = ads_init(tmp_ctx, + info->domain_name, info->domain_name, dc, ADS_SASL_PLAIN); @@ -459,22 +460,18 @@ WERROR NetGetJoinableOUs_l(struct libnetapi_ctx *ctx, ads_status = ads_connect_user_creds(ads); if (!ADS_ERR_OK(ads_status)) { - ads_destroy(&ads); ret = WERR_NERR_DEFAULTJOINREQUIRED; goto out; } ads_status = ads_get_joinable_ous(ads, ctx, &p, &s); if (!ADS_ERR_OK(ads_status)) { - ads_destroy(&ads); ret = WERR_NERR_DEFAULTJOINREQUIRED; goto out; } *r->out.ous = discard_const_p(const char *, p); *r->out.ou_count = s; - ads_destroy(&ads); - ret = WERR_OK; out: TALLOC_FREE(tmp_ctx); diff --git a/source3/libads/ads_proto.h b/source3/libads/ads_proto.h index 5701a5d79d4..8f75e77a94e 100644 --- a/source3/libads/ads_proto.h +++ b/source3/libads/ads_proto.h @@ -43,12 +43,12 @@ enum ads_sasl_state_e { char *ads_build_path(const char *realm, const char *sep, const char *field, int reverse); char *ads_build_dn(const char *realm); char *ads_build_domain(const char *dn); -ADS_STRUCT *ads_init(const char *realm, +ADS_STRUCT *ads_init(TALLOC_CTX *mem_ctx, + const char *realm, const char *workgroup, const char *ldap_server, enum ads_sasl_state_e sasl_state); bool ads_set_sasl_wrap_flags(ADS_STRUCT *ads, unsigned flags); -void ads_destroy(ADS_STRUCT **ads); /* The following definitions come from libads/disp_sec.c */ diff --git a/source3/libads/ads_struct.c b/source3/libads/ads_struct.c index 1d3f41f0269..184185fa148 100644 --- a/source3/libads/ads_struct.c +++ b/source3/libads/ads_struct.c @@ -124,12 +124,9 @@ char *ads_build_domain(const char *dn) /* free the memory used by the ADS structure initialized with 'ads_init(...)' */ -void ads_destroy(ADS_STRUCT **ads) +static void ads_destroy(ADS_STRUCT **ads) { if (ads && *ads) { - bool is_mine; - - is_mine = (*ads)->is_mine; #ifdef HAVE_LDAP ads_disconnect(*ads); #endif @@ -150,30 +147,33 @@ void ads_destroy(ADS_STRUCT **ads) SAFE_FREE((*ads)->config.client_site_name); SAFE_FREE((*ads)->config.schema_path); SAFE_FREE((*ads)->config.config_path); - - ZERO_STRUCTP(*ads); -#ifdef HAVE_LDAP - ads_zero_ldap(*ads); -#endif - - if ( is_mine ) - SAFE_FREE(*ads); } } +static int ads_destructor(ADS_STRUCT *ads) +{ + ads_destroy(&ads); + return 0; +} + /* initialise a ADS_STRUCT, ready for some ads_ ops */ -ADS_STRUCT *ads_init(const char *realm, +ADS_STRUCT *ads_init(TALLOC_CTX *mem_ctx, + const char *realm, const char *workgroup, const char *ldap_server, enum ads_sasl_state_e sasl_state) { - ADS_STRUCT *ads; + ADS_STRUCT *ads = NULL; int wrap_flags; - ads = SMB_XMALLOC_P(ADS_STRUCT); - ZERO_STRUCTP(ads); + ads = talloc_zero(mem_ctx, ADS_STRUCT); + if (ads == NULL) { + return NULL; + } + talloc_set_destructor(ads, ads_destructor); + #ifdef HAVE_LDAP ads_zero_ldap(ads); #endif diff --git a/source3/libads/ldap.c b/source3/libads/ldap.c index 23a2dacfb4c..acec42be166 100755 --- a/source3/libads/ldap.c +++ b/source3/libads/ldap.c @@ -622,8 +622,8 @@ ADS_STATUS ads_connect(ADS_STRUCT *ads) * to ads_find_dc() in the reuse case. * * If a caller wants a clean ADS_STRUCT they - * will re-initialize by calling ads_init(), or - * call ads_destroy() both of which ensures + * will TALLOC_FREE it and allocate a new one + * by calling ads_init(), which ensures * ads->ldap.ss is a properly zero'ed out valid IP * address. */ @@ -3292,7 +3292,8 @@ ADS_STATUS ads_current_time(ADS_STRUCT *ads) * through ads_find_dc() again we want to avoid repeating. */ if (is_zero_addr(&ads->ldap.ss)) { - ads_s = ads_init(ads->server.realm, + ads_s = ads_init(tmp_ctx, + ads->server.realm, ads->server.workgroup, ads->server.ldap_server, ADS_SASL_PLAIN ); @@ -3340,10 +3341,6 @@ ADS_STATUS ads_current_time(ADS_STRUCT *ads) status = ADS_SUCCESS; done: - /* free any temporary ads connections */ - if ( ads_s != ads ) { - ads_destroy( &ads_s ); - } TALLOC_FREE(tmp_ctx); return status; @@ -3379,7 +3376,8 @@ ADS_STATUS ads_domain_func_level(ADS_STRUCT *ads, uint32_t *val) * through ads_find_dc() again we want to avoid repeating. */ if (is_zero_addr(&ads->ldap.ss)) { - ads_s = ads_init(ads->server.realm, + ads_s = ads_init(tmp_ctx, + ads->server.realm, ads->server.workgroup, ads->server.ldap_server, ADS_SASL_PLAIN ); @@ -3421,10 +3419,6 @@ ADS_STATUS ads_domain_func_level(ADS_STRUCT *ads, uint32_t *val) ads_msgfree(ads_s, res); done: - /* free any temporary ads connections */ - if ( ads_s != ads ) { - ads_destroy( &ads_s ); - } TALLOC_FREE(tmp_ctx); return status; diff --git a/source3/libads/ldap_utils.c b/source3/libads/ldap_utils.c index c9039684bf0..c08f046a405 100644 --- a/source3/libads/ldap_utils.c +++ b/source3/libads/ldap_utils.c @@ -105,8 +105,6 @@ static ADS_STATUS ads_do_search_retry_internal(ADS_STRUCT *ads, const char *bind status = ads_connect(ads); if (!ADS_ERR_OK(status)) { - bool orig_is_mine = ads->is_mine; - DEBUG(1,("ads_search_retry: failed to reconnect (%s)\n", ads_errstr(status))); /* @@ -114,9 +112,7 @@ static ADS_STATUS ads_do_search_retry_internal(ADS_STRUCT *ads, const char *bind * from being freed here as we don't own it and * callers depend on it being around. */ - ads->is_mine = false; - ads_destroy(&ads); - ads->is_mine = orig_is_mine; + ads_disconnect(ads); SAFE_FREE(bp); return status; } diff --git a/source3/libnet/libnet_join.c b/source3/libnet/libnet_join.c index e075432a827..f2fa2e5f60b 100644 --- a/source3/libnet/libnet_join.c +++ b/source3/libnet/libnet_join.c @@ -143,7 +143,8 @@ static ADS_STATUS libnet_connect_ads(const char *dns_domain_name, char *cp; enum credentials_use_kerberos krb5_state; - my_ads = ads_init(dns_domain_name, + my_ads = ads_init(tmp_ctx, + dns_domain_name, netbios_domain_name, dc_name, ADS_SASL_SEAL); @@ -177,7 +178,6 @@ static ADS_STATUS libnet_connect_ads(const char *dns_domain_name, SAFE_FREE(my_ads->auth.realm); my_ads->auth.realm = smb_xstrdup(cp); if (!strupper_m(my_ads->auth.realm)) { - ads_destroy(&my_ads); status = ADS_ERROR_LDAP(LDAP_NO_MEMORY); goto out; } @@ -197,11 +197,10 @@ static ADS_STATUS libnet_connect_ads(const char *dns_domain_name, status = ads_connect_user_creds(my_ads); if (!ADS_ERR_OK(status)) { - ads_destroy(&my_ads); goto out; } - *ads = my_ads; + *ads = talloc_move(mem_ctx, &my_ads); status = ADS_SUCCESS; out: @@ -265,7 +264,7 @@ static ADS_STATUS libnet_join_connect_ads(TALLOC_CTX *mem_ctx, username, password, ccname, - mem_ctx, + r, &r->in.ads); if (!ADS_ERR_OK(status)) { libnet_join_set_error_string(mem_ctx, r, @@ -323,7 +322,7 @@ static ADS_STATUS libnet_unjoin_connect_ads(TALLOC_CTX *mem_ctx, r->in.admin_account, r->in.admin_password, NULL, - mem_ctx, + r, &r->in.ads); if (!ADS_ERR_OK(status)) { libnet_unjoin_set_error_string(mem_ctx, r, @@ -1037,7 +1036,7 @@ static ADS_STATUS libnet_join_post_processing_ads_modify(TALLOC_CTX *mem_ctx, r->in.ads->auth.ccache_name = NULL; } - ads_destroy(&r->in.ads); + TALLOC_FREE(r->in.ads); status = libnet_join_connect_ads_machine(mem_ctx, r); if (!ADS_ERR_OK(status)) { @@ -2486,9 +2485,7 @@ static WERROR libnet_join_post_processing(TALLOC_CTX *mem_ctx, static int libnet_destroy_JoinCtx(struct libnet_JoinCtx *r) { - if (r->in.ads) { - ads_destroy(&r->in.ads); - } + TALLOC_FREE(r->in.ads); return 0; } @@ -2498,9 +2495,7 @@ static int libnet_destroy_JoinCtx(struct libnet_JoinCtx *r) static int libnet_destroy_UnjoinCtx(struct libnet_UnjoinCtx *r) { - if (r->in.ads) { - ads_destroy(&r->in.ads); - } + TALLOC_FREE(r->in.ads); return 0; } diff --git a/source3/libnet/libnet_keytab.c b/source3/libnet/libnet_keytab.c index cdf22c2ceb9..31d06056b18 100644 --- a/source3/libnet/libnet_keytab.c +++ b/source3/libnet/libnet_keytab.c @@ -44,9 +44,7 @@ static int keytab_close(struct libnet_keytab_context *ctx) krb5_free_context(ctx->context); } - if (ctx->ads) { - ads_destroy(&ctx->ads); - } + TALLOC_FREE(ctx->ads); TALLOC_FREE(ctx); diff --git a/source3/libsmb/namequery_dc.c b/source3/libsmb/namequery_dc.c index 2bb5dd53f67..f2598ed223f 100644 --- a/source3/libsmb/namequery_dc.c +++ b/source3/libsmb/namequery_dc.c @@ -72,7 +72,7 @@ static bool ads_dc_name(const char *domain, /* Try this 3 times then give up. */ for( i =0 ; i < 3; i++) { - ads = ads_init(realm, domain, NULL, ADS_SASL_PLAIN); + ads = ads_init(tmp_ctx, realm, domain, NULL, ADS_SASL_PLAIN); if (!ads) { ok = false; goto out; @@ -87,7 +87,6 @@ static bool ads_dc_name(const char *domain, #endif if (!ads->config.realm) { - ads_destroy(&ads); ok = false; goto out; } @@ -98,7 +97,7 @@ static bool ads_dc_name(const char *domain, if (stored_sitename_changed(realm, sitename)) { sitename = sitename_fetch(tmp_ctx, realm); - ads_destroy(&ads); + TALLOC_FREE(ads); /* Ensure we don't cache the DC we just connected to. */ namecache_delete(realm, 0x1C); namecache_delete(domain, 0x1C); @@ -130,14 +129,12 @@ static bool ads_dc_name(const char *domain, if (i == 3) { DEBUG(1,("ads_dc_name: sitename (now \"%s\") keeps changing ???\n", sitename ? sitename : "")); - ads_destroy(&ads); ok = false; goto out; } fstrcpy(srv_name, ads->config.ldap_server_name); if (!strupper_m(srv_name)) { - ads_destroy(&ads); ok = false; goto out; } @@ -146,8 +143,6 @@ static bool ads_dc_name(const char *domain, #else zero_sockaddr(dc_ss); #endif - ads_destroy(&ads); - print_sockaddr(addr, sizeof(addr), dc_ss); DEBUG(4,("ads_dc_name: using server='%s' IP=%s\n", srv_name, addr)); diff --git a/source3/printing/nt_printing_ads.c b/source3/printing/nt_printing_ads.c index ea200f35661..026605372c0 100644 --- a/source3/printing/nt_printing_ads.c +++ b/source3/printing/nt_printing_ads.c @@ -227,7 +227,11 @@ WERROR nt_printer_guid_retrieve(TALLOC_CTX *mem_ctx, const char *printer, return WERR_NOT_ENOUGH_MEMORY; } - ads = ads_init(lp_realm(), lp_workgroup(), NULL, ADS_SASL_PLAIN); + ads = ads_init(tmp_ctx, + lp_realm(), + lp_workgroup(), + NULL, + ADS_SASL_PLAIN); if (ads == NULL) { result = WERR_RPC_S_SERVER_UNAVAILABLE; goto out; @@ -254,7 +258,6 @@ WERROR nt_printer_guid_retrieve(TALLOC_CTX *mem_ctx, const char *printer, result = nt_printer_guid_retrieve_internal(ads, printer_dn, pguid); out: TALLOC_FREE(tmp_ctx); - ads_destroy(&ads); ads_kdestroy("MEMORY:prtpub_cache"); unsetenv(KRB5_ENV_CCNAME); if (old_krb5ccname != NULL) { @@ -678,7 +681,11 @@ WERROR nt_printer_publish(TALLOC_CTX *mem_ctx, TALLOC_FREE(sinfo2); - ads = ads_init(lp_realm(), lp_workgroup(), NULL, ADS_SASL_PLAIN); + ads = ads_init(tmp_ctx, + lp_realm(), + lp_workgroup(), + NULL, + ADS_SASL_PLAIN); if (!ads) { DEBUG(3, ("ads_init() failed\n")); win_rc = WERR_RPC_S_SERVER_UNAVAILABLE; @@ -709,7 +716,6 @@ WERROR nt_printer_publish(TALLOC_CTX *mem_ctx, } done: - ads_destroy(&ads); ads_kdestroy("MEMORY:prtpub_cache"); unsetenv(KRB5_ENV_CCNAME); if (old_krb5ccname) { @@ -729,17 +735,18 @@ WERROR check_published_printers(struct messaging_context *msg_ctx) ADS_STRUCT *ads = NULL; int snum; int n_services = lp_numservices(); - TALLOC_CTX *tmp_ctx = NULL; + TALLOC_CTX *tmp_ctx = talloc_stackframe(); struct auth_session_info *session_info = NULL; struct spoolss_PrinterInfo2 *pinfo2; NTSTATUS status; WERROR result; char *old_krb5ccname = NULL; - tmp_ctx = talloc_new(NULL); - if (!tmp_ctx) return WERR_NOT_ENOUGH_MEMORY; - - ads = ads_init(lp_realm(), lp_workgroup(), NULL, ADS_SASL_PLAIN); + ads = ads_init(tmp_ctx, + lp_realm(), + lp_workgroup(), + NULL, + ADS_SASL_PLAIN); if (!ads) { DEBUG(3, ("ads_init() failed\n")); TALLOC_FREE(tmp_ctx); @@ -788,7 +795,6 @@ WERROR check_published_printers(struct messaging_context *msg_ctx) result = WERR_OK; done: - ads_destroy(&ads); ads_kdestroy("MEMORY:prtpub_cache"); unsetenv(KRB5_ENV_CCNAME); if (old_krb5ccname) { diff --git a/source3/utils/net_ads.c b/source3/utils/net_ads.c index 17ec7fade8b..4718d4bed97 100644 --- a/source3/utils/net_ads.c +++ b/source3/utils/net_ads.c @@ -442,7 +442,6 @@ static int net_ads_lookup(struct net_context *c, int argc, const char **argv) ret = net_ads_cldap_netlogon(c, ads); out: - ads_destroy(&ads); TALLOC_FREE(tmp_ctx); return ret; } @@ -598,7 +597,6 @@ static int net_ads_info(struct net_context *c, int argc, const char **argv) ret = 0; out: - ads_destroy(&ads); TALLOC_FREE(tmp_ctx); return ret; } @@ -633,10 +631,14 @@ retry_connect: realm = assume_own_realm(c); } - ads = ads_init(realm, - c->opt_target_workgroup, - c->opt_host, - ADS_SASL_PLAIN); + ads = ads_init(mem_ctx, + realm, + c->opt_target_workgroup, + c->opt_host, + ADS_SASL_PLAIN); + if (ads == NULL) { + return ADS_ERROR_NT(NT_STATUS_NO_MEMORY); + } if (!c->opt_user_name) { c->opt_user_name = "administrator"; @@ -650,7 +652,7 @@ retry: if (!c->opt_password && need_password && !c->opt_machine_pass) { c->opt_password = net_prompt_pass(c, c->opt_user_name); if (!c->opt_password) { - ads_destroy(&ads); + TALLOC_FREE(ads); return ADS_ERROR(LDAP_NO_MEMORY); } } @@ -693,7 +695,7 @@ retry: SAFE_FREE(ads->auth.realm); ads->auth.realm = smb_xstrdup(cp); if (!strupper_m(ads->auth.realm)) { - ads_destroy(&ads); + TALLOC_FREE(ads); return ADS_ERROR(LDAP_NO_MEMORY); } } @@ -705,7 +707,7 @@ retry: if (NT_STATUS_EQUAL(ads_ntstatus(status), NT_STATUS_NO_LOGON_SERVERS)) { DEBUG(0,("ads_connect: %s\n", ads_errstr(status))); - ads_destroy(&ads); + TALLOC_FREE(ads); return status; } @@ -714,7 +716,7 @@ retry: second_time = true; goto retry; } else { - ads_destroy(&ads); + TALLOC_FREE(ads); return status; } } @@ -732,14 +734,13 @@ retry: namecache_delete(ads->server.realm, 0x1C); namecache_delete(ads->server.workgroup, 0x1C); - ads_destroy(&ads); - ads = NULL; + TALLOC_FREE(ads); goto retry_connect; } } - *ads_ret = ads; + *ads_ret = talloc_move(mem_ctx, &ads); return status; } @@ -778,7 +779,7 @@ static int net_ads_check_int(struct net_context *c, ADS_STATUS status; int ret = -1; - ads = ads_init(realm, workgroup, host, ADS_SASL_PLAIN); + ads = ads_init(tmp_ctx, realm, workgroup, host, ADS_SASL_PLAIN); if (ads == NULL) { goto out; } @@ -792,7 +793,6 @@ static int net_ads_check_int(struct net_context *c, ret = 0; out: - ads_destroy(&ads); TALLOC_FREE(tmp_ctx); return ret; } @@ -851,7 +851,6 @@ static int net_ads_workgroup(struct net_context *c, int argc, const char **argv) ret = 0; out: - ads_destroy(&ads); TALLOC_FREE(tmp_ctx); return ret; @@ -979,7 +978,6 @@ static int ads_user_add(struct net_context *c, int argc, const char **argv) done: ads_msgfree(ads, res); - ads_destroy(&ads); SAFE_FREE(ou_str); TALLOC_FREE(tmp_ctx); return rc; @@ -1077,7 +1075,6 @@ static int ads_user_info(struct net_context *c, int argc, const char **argv) ret = 0; out: ads_msgfree(ads, res); - ads_destroy(&ads); TALLOC_FREE(tmp_ctx); return ret; } @@ -1124,7 +1121,6 @@ static int ads_user_delete(struct net_context *c, int argc, const char **argv) ret = 0; out: ads_msgfree(ads, res); - ads_destroy(&ads); TALLOC_FREE(tmp_ctx); return ret; } @@ -1205,7 +1201,6 @@ int net_ads_user(struct net_context *c, int argc, const char **argv) ret = 0; out: - ads_destroy(&ads); TALLOC_FREE(tmp_ctx); return ret; } @@ -1263,7 +1258,6 @@ static int ads_group_add(struct net_context *c, int argc, const char **argv) ret = 0; out: ads_msgfree(ads, res); - ads_destroy(&ads); SAFE_FREE(ou_str); TALLOC_FREE(tmp_ctx); return ret; @@ -1310,7 +1304,6 @@ static int ads_group_delete(struct net_context *c, int argc, const char **argv) ret = 0; out: ads_msgfree(ads, res); - ads_destroy(&ads); TALLOC_FREE(tmp_ctx); return ret; } @@ -1383,7 +1376,6 @@ int net_ads_group(struct net_context *c, int argc, const char **argv) ret = 0; out: - ads_destroy(&ads); TALLOC_FREE(tmp_ctx); return ret; } @@ -1429,7 +1421,6 @@ static int net_ads_status(struct net_context *c, int argc, const char **argv) ret = 0; out: ads_msgfree(ads, res); - ads_destroy(&ads); TALLOC_FREE(tmp_ctx); return ret; } @@ -1560,7 +1551,6 @@ static ADS_STATUS net_ads_join_ok(struct net_context *c) status = ADS_ERROR_NT(NT_STATUS_OK); out: - ads_destroy(&ads); TALLOC_FREE(tmp_ctx); return status; } @@ -1939,7 +1929,6 @@ static int net_ads_dns_register(struct net_context *c, int argc, const char **ar ret = 0; out: - ads_destroy(&ads); TALLOC_FREE(tmp_ctx); return ret; @@ -2006,7 +1995,6 @@ static int net_ads_dns_unregister(struct net_context *c, ret = 0; out: - ads_destroy(&ads); TALLOC_FREE(tmp_ctx); return ret; @@ -2190,7 +2178,6 @@ static int net_ads_printer_search(struct net_context *c, ret = 0; out: ads_msgfree(ads, res); - ads_destroy(&ads); TALLOC_FREE(tmp_ctx); return ret; } @@ -2252,7 +2239,6 @@ static int net_ads_printer_info(struct net_context *c, ret = 0; out: ads_msgfree(ads, res); - ads_destroy(&ads); TALLOC_FREE(tmp_ctx); return ret; } @@ -2399,7 +2385,6 @@ static int net_ads_printer_publish(struct net_context *c, ret = 0; out: - ads_destroy(&ads); talloc_destroy(tmp_ctx); return ret; @@ -2466,7 +2451,6 @@ static int net_ads_printer_remove(struct net_context *c, ret = 0; out: ads_msgfree(ads, res); - ads_destroy(&ads); TALLOC_FREE(tmp_ctx); return ret; } @@ -2572,7 +2556,11 @@ static int net_ads_password(struct net_context *c, int argc, const char **argv) /* use the realm so we can eventually change passwords for users in realms other than default */ - ads = ads_init(realm, c->opt_workgroup, c->opt_host, ADS_SASL_PLAIN); + ads = ads_init(tmp_ctx, + realm, + c->opt_workgroup, + c->opt_host, + ADS_SASL_PLAIN); if (ads == NULL) { goto out; } @@ -2627,7 +2615,6 @@ static int net_ads_password(struct net_context *c, int argc, const char **argv) ret = 0; out: - ads_destroy(&ads); TALLOC_FREE(tmp_ctx); return ret; } @@ -2696,7 +2683,6 @@ int net_ads_changetrustpw(struct net_context *c, int argc, const char **argv) ret = 0; out: - ads_destroy(&ads); TALLOC_FREE(tmp_ctx); return ret; @@ -2764,7 +2750,6 @@ static int net_ads_search(struct net_context *c, int argc, const char **argv) ret = 0; out: ads_msgfree(ads, res); - ads_destroy(&ads); TALLOC_FREE(tmp_ctx); return ret; } @@ -2833,7 +2818,6 @@ static int net_ads_dn(struct net_context *c, int argc, const char **argv) ret = 0; out: ads_msgfree(ads, res); - ads_destroy(&ads); TALLOC_FREE(tmp_ctx); return ret; } @@ -2901,7 +2885,6 @@ static int net_ads_sid(struct net_context *c, int argc, const char **argv) ret = 0; out: ads_msgfree(ads, res); - ads_destroy(&ads); TALLOC_FREE(tmp_ctx); return ret; } @@ -2936,7 +2919,6 @@ static int net_ads_keytab_flush(struct net_context *c, ret = ads_keytab_flush(ads); out: - ads_destroy(&ads); TALLOC_FREE(tmp_ctx); return ret; } @@ -2978,7 +2960,6 @@ static int net_ads_keytab_add(struct net_context *c, ret |= ads_keytab_add_entry(ads, argv[i], update_ads); } out: - ads_destroy(&ads); TALLOC_FREE(tmp_ctx); return ret; } @@ -3025,7 +3006,6 @@ static int net_ads_keytab_create(struct net_context *c, int argc, const char **a ret = ads_keytab_create_default(ads); out: - ads_destroy(&ads); TALLOC_FREE(tmp_ctx); return ret; } @@ -3427,7 +3407,6 @@ static int net_ads_setspn_list(struct net_context *c, ret = ok ? 0 : -1; out: - ads_destroy(&ads); TALLOC_FREE(tmp_ctx); return ret; } @@ -3461,7 +3440,6 @@ static int net_ads_setspn_add(struct net_context *c, int argc, const char **argv ret = ok ? 0 : -1; out: - ads_destroy(&ads); TALLOC_FREE(tmp_ctx); return ret; } @@ -3495,7 +3473,6 @@ static int net_ads_setspn_delete(struct net_context *c, int argc, const char **a ret = ok ? 0 : -1; out: - ads_destroy(&ads); TALLOC_FREE(tmp_ctx); return ret; } @@ -3645,7 +3622,6 @@ static int net_ads_enctypes_list(struct net_context *c, int argc, const char **a ret = 0; out: ads_msgfree(ads, res); - ads_destroy(&ads); TALLOC_FREE(tmp_ctx); return ret; } @@ -3736,7 +3712,6 @@ static int net_ads_enctypes_set(struct net_context *c, int argc, const char **ar ret = 0; done: ads_msgfree(ads, res); - ads_destroy(&ads); TALLOC_FREE(tmp_ctx); return ret; } @@ -3797,7 +3772,6 @@ static int net_ads_enctypes_delete(struct net_context *c, int argc, const char * done: ads_msgfree(ads, res); - ads_destroy(&ads); TALLOC_FREE(tmp_ctx); return ret; } diff --git a/source3/utils/net_ads_gpo.c b/source3/utils/net_ads_gpo.c index 376d846026c..1bae3e99e4a 100644 --- a/source3/utils/net_ads_gpo.c +++ b/source3/utils/net_ads_gpo.c @@ -107,7 +107,6 @@ out: ads_msgfree(ads, res); TALLOC_FREE(mem_ctx); - ads_destroy(&ads); return 0; } @@ -178,7 +177,6 @@ out: ads_msgfree(ads, res); talloc_destroy(mem_ctx); - ads_destroy(&ads); return 0; } @@ -220,7 +218,6 @@ static int net_ads_gpo_link_get(struct net_context *c, int argc, const char **ar out: talloc_destroy(mem_ctx); - ads_destroy(&ads); return 0; } @@ -266,7 +263,6 @@ static int net_ads_gpo_link_add(struct net_context *c, int argc, const char **ar out: talloc_destroy(mem_ctx); - ads_destroy(&ads); return 0; } @@ -306,7 +302,6 @@ static int net_ads_gpo_link_delete(struct net_context *c, int argc, const char * out: talloc_destroy(mem_ctx); - ads_destroy(&ads); return 0; } @@ -367,7 +362,6 @@ static int net_ads_gpo_get_gpo(struct net_context *c, int argc, const char **arg out: talloc_destroy(mem_ctx); - ads_destroy(&ads); return 0; } diff --git a/source3/utils/net_ads_join_dns.c b/source3/utils/net_ads_join_dns.c index 995fd5e7cca..59bf17ed102 100644 --- a/source3/utils/net_ads_join_dns.c +++ b/source3/utils/net_ads_join_dns.c @@ -271,8 +271,11 @@ void net_ads_join_dns_updates(struct net_context *c, TALLOC_CTX *ctx, struct lib * kinit with the machine password to do dns update. */ - ads_dns = ads_init(lp_realm(), NULL, r->in.dc_name, ADS_SASL_PLAIN); - + ads_dns = ads_init(ctx, + lp_realm(), + NULL, + r->in.dc_name, + ADS_SASL_PLAIN); if (ads_dns == NULL) { d_fprintf(stderr, _("DNS update failed: out of memory!\n")); goto done; @@ -319,7 +322,7 @@ void net_ads_join_dns_updates(struct net_context *c, TALLOC_CTX *ctx, struct lib } done: - ads_destroy(&ads_dns); + TALLOC_FREE(ads_dns); #endif return; diff --git a/source3/winbindd/idmap_rfc2307.c b/source3/winbindd/idmap_rfc2307.c index 6d9eeee7527..d84cf930f75 100644 --- a/source3/winbindd/idmap_rfc2307.c +++ b/source3/winbindd/idmap_rfc2307.c @@ -749,12 +749,7 @@ out: static int idmap_rfc2307_context_destructor(struct idmap_rfc2307_context *ctx) { - if (ctx->ads != NULL) { - /* we own this ADS_STRUCT so make sure it goes away */ - ctx->ads->is_mine = True; - ads_destroy( &ctx->ads ); - ctx->ads = NULL; - } + TALLOC_FREE(ctx->ads); if (ctx->smbldap_state != NULL) { smbldap_free_struct(&ctx->smbldap_state); diff --git a/source3/winbindd/winbindd_ads.c b/source3/winbindd/winbindd_ads.c index 6d2d37ae9ab..8425dbc1693 100644 --- a/source3/winbindd/winbindd_ads.c +++ b/source3/winbindd/winbindd_ads.c @@ -70,8 +70,7 @@ static void ads_cached_connection_reuse(ADS_STRUCT **adsp) } else { /* we own this ADS_STRUCT so make sure it goes away */ DEBUG(7,("Deleting expired krb5 credential cache\n")); - ads->is_mine = True; - ads_destroy( &ads ); + TALLOC_FREE(ads); ads_kdestroy(WINBIND_CCACHE_NAME); *adsp = NULL; } @@ -115,7 +114,8 @@ static ADS_STATUS ads_cached_connection_connect(const char *target_realm, /* we don't want this to affect the users ccache */ setenv("KRB5CCNAME", WINBIND_CCACHE_NAME, 1); - ads = ads_init(target_realm, + ads = ads_init(tmp_ctx, + target_realm, target_dom_name, ldap_server, ADS_SASL_SEAL); @@ -150,7 +150,6 @@ static ADS_STATUS ads_cached_connection_connect(const char *target_realm, ads->auth.realm = SMB_STRDUP(auth_realm); if (!strupper_m(ads->auth.realm)) { - ads_destroy(&ads); status = ADS_ERROR_NT(NT_STATUS_INTERNAL_ERROR); goto out; } @@ -164,17 +163,10 @@ static ADS_STATUS ads_cached_connection_connect(const char *target_realm, if (!ADS_ERR_OK(status)) { DEBUG(1,("ads_connect for domain %s failed: %s\n", target_dom_name, ads_errstr(status))); - ads_destroy(&ads); goto out; } - /* set the flag that says we don't own the memory even - though we do so that ads_destroy() won't destroy the - structure we pass back by reference */ - - ads->is_mine = False; - - *adsp = ads; + *adsp = talloc_move(mem_ctx, &ads); out: TALLOC_FREE(tmp_ctx); return status; diff --git a/source3/winbindd/winbindd_cm.c b/source3/winbindd/winbindd_cm.c index 4595be280eb..64441b4db16 100644 --- a/source3/winbindd/winbindd_cm.c +++ b/source3/winbindd/winbindd_cm.c @@ -1094,7 +1094,8 @@ static bool dcip_check_name_ads(const struct winbindd_domain *domain, print_sockaddr(addr, sizeof(addr), &sa->u.ss); - ads = ads_init(domain->alt_name, + ads = ads_init(tmp_ctx, + domain->alt_name, domain->name, addr, ADS_SASL_PLAIN); @@ -1149,8 +1150,6 @@ static bool dcip_check_name_ads(const struct winbindd_domain *domain, *namep = talloc_move(mem_ctx, &name); out: - ads_destroy( &ads ); - TALLOC_FREE(tmp_ctx); return ADS_ERR_OK(ads_status) ? true : false; -- 2.47.3