s3:net: Refactor net_ads_status(), allocate a talloc context
authorSamuel Cabrero <scabrero@samba.org>
Thu, 26 May 2022 07:10:06 +0000 (09:10 +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.

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

index ad973149bdcb598510d83e3f46bcb30b64fe3a2f..925a296637b1040c04ae6af4be6c2927b947c4c6 100644 (file)
@@ -1372,9 +1372,11 @@ out:
 
 static int net_ads_status(struct net_context *c, int argc, const char **argv)
 {
-       ADS_STRUCT *ads;
-       ADS_STATUS rc;
-       LDAPMessage *res;
+       TALLOC_CTX *tmp_ctx = talloc_stackframe();
+       ADS_STRUCT *ads = NULL;
+       ADS_STATUS status;
+       LDAPMessage *res = NULL;
+       int ret = -1;
 
        if (c->display_usage) {
                d_printf(  "%s\n"
@@ -1382,29 +1384,36 @@ static int net_ads_status(struct net_context *c, int argc, const char **argv)
                           "    %s\n",
                         _("Usage:"),
                         _("Display machine account details"));
+               TALLOC_FREE(tmp_ctx);
                return 0;
        }
 
-       if (!ADS_ERR_OK(ads_startup(c, true, &ads))) {
-               return -1;
+       status = ads_startup(c, true, &ads);
+       if (!ADS_ERR_OK(status)) {
+               goto out;
        }
 
-       rc = ads_find_machine_acct(ads, &res, lp_netbios_name());
-       if (!ADS_ERR_OK(rc)) {
-               d_fprintf(stderr, _("ads_find_machine_acct: %s\n"), ads_errstr(rc));
-               ads_destroy(&ads);
-               return -1;
+       status = ads_find_machine_acct(ads, &res, lp_netbios_name());
+       if (!ADS_ERR_OK(status)) {
+               d_fprintf(stderr, _("ads_find_machine_acct: %s\n"),
+                         ads_errstr(status));
+               goto out;
        }
 
        if (ads_count_replies(ads, res) == 0) {
-               d_fprintf(stderr, _("No machine account for '%s' found\n"), lp_netbios_name());
-               ads_destroy(&ads);
-               return -1;
+               d_fprintf(stderr, _("No machine account for '%s' found\n"),
+                         lp_netbios_name());
+               goto out;
        }
 
        ads_dump(ads, res);
+
+       ret = 0;
+out:
+       ads_msgfree(ads, res);
        ads_destroy(&ads);
-       return 0;
+       TALLOC_FREE(tmp_ctx);
+       return ret;
 }
 
 /*******************************************************************