From: Samuel Cabrero Date: Wed, 25 May 2022 15:33:02 +0000 (+0200) Subject: s3:net: Refactor ads_user_delete(), allocate a talloc context X-Git-Tag: tevent-0.13.0~278 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3c2b813a0b15c967bc90fd010bd99fb8efdb17fb;p=thirdparty%2Fsamba.git s3:net: Refactor ads_user_delete(), allocate a talloc context ADS_STRUCT will be allocated in the talloc context. Signed-off-by: Samuel Cabrero Reviewed-by: Jeremy Allison --- diff --git a/source3/utils/net_ads.c b/source3/utils/net_ads.c index 0e1d344e3cc..81da81ca7e2 100644 --- a/source3/utils/net_ads.c +++ b/source3/utils/net_ads.c @@ -1066,39 +1066,49 @@ out: static int ads_user_delete(struct net_context *c, int argc, const char **argv) { - ADS_STRUCT *ads; - ADS_STATUS rc; + TALLOC_CTX *tmp_ctx = talloc_stackframe(); + ADS_STRUCT *ads = NULL; + ADS_STATUS status; LDAPMessage *res = NULL; - char *userdn; + char *userdn = NULL; + int ret = -1; if (argc < 1) { + TALLOC_FREE(tmp_ctx); return net_ads_user_usage(c, argc, argv); } - if (!ADS_ERR_OK(ads_startup(c, false, &ads))) { - return -1; + status = ads_startup(c, false, &ads); + if (!ADS_ERR_OK(status)) { + goto out; } - rc = ads_find_user_acct(ads, &res, argv[0]); - if (!ADS_ERR_OK(rc) || ads_count_replies(ads, res) != 1) { + status = ads_find_user_acct(ads, &res, argv[0]); + if (!ADS_ERR_OK(status) || ads_count_replies(ads, res) != 1) { d_printf(_("User %s does not exist.\n"), argv[0]); - ads_msgfree(ads, res); - ads_destroy(&ads); - return -1; + goto out; } - userdn = ads_get_dn(ads, talloc_tos(), res); - ads_msgfree(ads, res); - rc = ads_del_dn(ads, userdn); - TALLOC_FREE(userdn); - if (ADS_ERR_OK(rc)) { - d_printf(_("User %s deleted\n"), argv[0]); - ads_destroy(&ads); - return 0; + + userdn = ads_get_dn(ads, tmp_ctx, res); + if (userdn == NULL) { + goto out; } - d_fprintf(stderr, _("Error deleting user %s: %s\n"), argv[0], - ads_errstr(rc)); + + status = ads_del_dn(ads, userdn); + if (!ADS_ERR_OK(status)) { + d_fprintf(stderr, _("Error deleting user %s: %s\n"), argv[0], + ads_errstr(status)); + goto out; + } + + d_printf(_("User %s deleted\n"), argv[0]); + + ret = 0; +out: + ads_msgfree(ads, res); ads_destroy(&ads); - return -1; + TALLOC_FREE(tmp_ctx); + return ret; } int net_ads_user(struct net_context *c, int argc, const char **argv)