From: Samuel Cabrero Date: Wed, 25 May 2022 15:41:50 +0000 (+0200) Subject: s3:net: Refactor net_ads_user(), allocate a talloc context X-Git-Tag: tevent-0.13.0~277 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=937021d51bb79ffb5cd7322f07013f55e040ea33;p=thirdparty%2Fsamba.git s3:net: Refactor net_ads_user(), allocate a talloc context ADS_STRUCT will be allocated in the talloc context. Best viewed using "git diff -b". Signed-off-by: Samuel Cabrero Reviewed-by: Jeremy Allison --- diff --git a/source3/utils/net_ads.c b/source3/utils/net_ads.c index 81da81ca7e2..ebdc1f79af2 100644 --- a/source3/utils/net_ads.c +++ b/source3/utils/net_ads.c @@ -1140,42 +1140,56 @@ int net_ads_user(struct net_context *c, int argc, const char **argv) }, {NULL, NULL, 0, NULL, NULL} }; - ADS_STRUCT *ads; - ADS_STATUS rc; + TALLOC_CTX *tmp_ctx = talloc_stackframe(); + ADS_STRUCT *ads = NULL; + ADS_STATUS status; const char *shortattrs[] = {"sAMAccountName", NULL}; const char *longattrs[] = {"sAMAccountName", "description", NULL}; char *disp_fields[2] = {NULL, NULL}; + int ret = -1; - if (argc == 0) { - if (c->display_usage) { - d_printf( "%s\n" - "net ads user\n" - " %s\n", - _("Usage:"), - _("List AD users")); - net_display_usage_from_functable(func); - return 0; - } + if (argc > 0) { + TALLOC_FREE(tmp_ctx); + return net_run_function(c, argc, argv, "net ads user", func); + } - if (!ADS_ERR_OK(ads_startup(c, false, &ads))) { - return -1; - } + if (c->display_usage) { + d_printf( "%s\n" + "net ads user\n" + " %s\n", + _("Usage:"), + _("List AD users")); + net_display_usage_from_functable(func); + TALLOC_FREE(tmp_ctx); + return 0; + } - if (c->opt_long_list_entries) - d_printf(_("\nUser name Comment" - "\n-----------------------------\n")); + status = ads_startup(c, false, &ads); + if (!ADS_ERR_OK(status)) { + goto out; + } - rc = ads_do_search_all_fn(ads, ads->config.bind_path, - LDAP_SCOPE_SUBTREE, - "(objectCategory=user)", - c->opt_long_list_entries ? longattrs : - shortattrs, usergrp_display, - disp_fields); - ads_destroy(&ads); - return ADS_ERR_OK(rc) ? 0 : -1; + if (c->opt_long_list_entries) + d_printf(_("\nUser name Comment" + "\n-----------------------------\n")); + + status = ads_do_search_all_fn(ads, + ads->config.bind_path, + LDAP_SCOPE_SUBTREE, + "(objectCategory=user)", + c->opt_long_list_entries ? + longattrs : shortattrs, + usergrp_display, + disp_fields); + if (!ADS_ERR_OK(status)) { + goto out; } - return net_run_function(c, argc, argv, "net ads user", func); + ret = 0; +out: + ads_destroy(&ads); + TALLOC_FREE(tmp_ctx); + return ret; } static int net_ads_group_usage(struct net_context *c, int argc, const char **argv)