]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
s3:net: Refactor net_ads_user(), allocate a talloc context
authorSamuel Cabrero <scabrero@samba.org>
Wed, 25 May 2022 15:41:50 +0000 (17:41 +0200)
committerJeremy Allison <jra@samba.org>
Mon, 27 Jun 2022 15:50:29 +0000 (15:50 +0000)
ADS_STRUCT will be allocated in the talloc context.

Best viewed using "git diff -b".

Signed-off-by: Samuel Cabrero <scabrero@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
source3/utils/net_ads.c

index 81da81ca7e210179fa5940b49d091c444ecfa2db..ebdc1f79af2820813b1f571c9cb87f32a8c11533 100644 (file)
@@ -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)