diff --git a/source3/utils/net_ads.c b/source3/utils/net_ads.c index 226350491cc..ad973149bdc 100644 --- a/source3/utils/net_ads.c +++ b/source3/utils/net_ads.c @@ -1318,41 +1318,56 @@ int net_ads_group(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 group\n" - " %s\n", - _("Usage:"), - _("List AD groups")); - net_display_usage_from_functable(func); - return 0; - } - - if (!ADS_ERR_OK(ads_startup(c, false, &ads))) { - return -1; - } - - if (c->opt_long_list_entries) - d_printf(_("\nGroup name Comment" - "\n-----------------------------\n")); - rc = ads_do_search_all_fn(ads, ads->config.bind_path, - LDAP_SCOPE_SUBTREE, - "(objectCategory=group)", - c->opt_long_list_entries ? longattrs : - shortattrs, usergrp_display, - disp_fields); - - ads_destroy(&ads); - return ADS_ERR_OK(rc) ? 0 : -1; + if (argc >= 0) { + TALLOC_FREE(tmp_ctx); + return net_run_function(c, argc, argv, "net ads group", func); } - return net_run_function(c, argc, argv, "net ads group", func); + + if (c->display_usage) { + d_printf( "%s\n" + "net ads group\n" + " %s\n", + _("Usage:"), + _("List AD groups")); + net_display_usage_from_functable(func); + TALLOC_FREE(tmp_ctx); + return 0; + } + + status = ads_startup(c, false, &ads); + if (!ADS_ERR_OK(status)) { + goto out; + } + + if (c->opt_long_list_entries) + d_printf(_("\nGroup name Comment" + "\n-----------------------------\n")); + + status = ads_do_search_all_fn(ads, + ads->config.bind_path, + LDAP_SCOPE_SUBTREE, + "(objectCategory=group)", + c->opt_long_list_entries ? + longattrs : shortattrs, + usergrp_display, + disp_fields); + if (!ADS_ERR_OK(status)) { + goto out; + } + + ret = 0; +out: + ads_destroy(&ads); + TALLOC_FREE(tmp_ctx); + return ret; } static int net_ads_status(struct net_context *c, int argc, const char **argv)