]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
s3:net: Refactor ads_group_delete(), allocate a talloc context
authorSamuel Cabrero <scabrero@samba.org>
Wed, 25 May 2022 16:02:11 +0000 (18:02 +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 6da0af0cb9e60dc2b5331b9b8cd73adf0d8e0484..226350491ccf438476b169b075f3e480a5972f8b 100644 (file)
@@ -1253,39 +1253,48 @@ static int ads_group_add(struct net_context *c, int argc, const char **argv)
 
 static int ads_group_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 *groupdn;
+       char *groupdn = NULL;
+       int ret = -1;
 
        if (argc < 1 || c->display_usage) {
+               TALLOC_FREE(tmp_ctx);
                return net_ads_group_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(_("Group %s does not exist.\n"), argv[0]);
-               ads_msgfree(ads, res);
-               ads_destroy(&ads);
-               return -1;
+               goto out;
        }
-       groupdn = ads_get_dn(ads, talloc_tos(), res);
-       ads_msgfree(ads, res);
-       rc = ads_del_dn(ads, groupdn);
-       TALLOC_FREE(groupdn);
-       if (ADS_ERR_OK(rc)) {
-               d_printf(_("Group %s deleted\n"), argv[0]);
-               ads_destroy(&ads);
-               return 0;
+
+       groupdn = ads_get_dn(ads, tmp_ctx, res);
+       if (groupdn == NULL) {
+               goto out;
        }
-       d_fprintf(stderr, _("Error deleting group %s: %s\n"), argv[0],
-                ads_errstr(rc));
+
+       status = ads_del_dn(ads, groupdn);
+       if (!ADS_ERR_OK(status)) {
+               d_fprintf(stderr, _("Error deleting group %s: %s\n"), argv[0],
+                         ads_errstr(status));
+               goto out;
+       }
+       d_printf(_("Group %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_group(struct net_context *c, int argc, const char **argv)