s3:winbind: Use a talloc stackframe for rpc_query_user_list
authorAndreas Schneider <asn@samba.org>
Mon, 8 May 2017 09:34:54 +0000 (11:34 +0200)
committerVolker Lendecke <vl@samba.org>
Tue, 16 May 2017 10:38:20 +0000 (12:38 +0200)
CID #1401581

Signed-off-by: Andreas Schneider <asn@samba.org>
Reviewed-by: Volker Lendecke <vl@samba.org>
source3/winbindd/winbindd_rpc.c

index 0023e2aa8d764ad3a27eb6644203b97061f4e708..2c76e1abd6cd95e05644768ad85b51d92795bde9 100644 (file)
@@ -46,54 +46,62 @@ NTSTATUS rpc_query_user_list(TALLOC_CTX *mem_ctx,
        uint32_t num_rids = 0;
        uint32_t i = 0;
        uint32_t resume_handle = 0;
-       NTSTATUS result;
+       NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
+       NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
+       TALLOC_CTX *tmp_ctx;
 
        *prids = NULL;
 
+       tmp_ctx = talloc_stackframe();
+       if (tmp_ctx == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
        do {
                struct samr_SamArray *sam_array = NULL;
                uint32_t count = 0;
-               NTSTATUS status;
                uint32_t *tmp;
 
                status = dcerpc_samr_EnumDomainUsers(
-                       b, mem_ctx, samr_policy, &resume_handle,
+                       b, tmp_ctx, samr_policy, &resume_handle,
                        ACB_NORMAL, &sam_array, 0xffff, &count, &result);
                if (!NT_STATUS_IS_OK(status)) {
-                       return status;
+                       goto done;
                }
                if (!NT_STATUS_IS_OK(result)) {
                        if (!NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) {
                                DBG_WARNING("EnumDomainUsers failed: %s\n",
                                            nt_errstr(result));
-                               TALLOC_FREE(rids);
-                               TALLOC_FREE(sam_array);
-                               return result;
+                               status = result;
+                               goto done;
                        }
                }
 
                if (num_rids + count < num_rids) {
-                       TALLOC_FREE(sam_array);
-                       TALLOC_FREE(rids);
-                       return NT_STATUS_INTEGER_OVERFLOW;
+                       status = NT_STATUS_INTEGER_OVERFLOW;
+                       goto done;
                }
 
-               tmp = talloc_realloc(mem_ctx, rids, uint32_t, num_rids+count);
+               tmp = talloc_realloc(tmp_ctx, rids, uint32_t, num_rids+count);
                if (tmp == NULL) {
-                       TALLOC_FREE(sam_array);
-                       TALLOC_FREE(rids);
-                       return NT_STATUS_NO_MEMORY;
+                       status = NT_STATUS_NO_MEMORY;
+                       goto done;
                }
                rids = tmp;
 
                for (i=0; i<count; i++) {
                        rids[num_rids++] = sam_array->entries[i].idx;
                }
+
+               TALLOC_FREE(sam_array);
        } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
 
-       *prids = rids;
+       *prids = talloc_steal(mem_ctx, rids);
+       status = NT_STATUS_OK;
 
-       return NT_STATUS_OK;
+done:
+       TALLOC_FREE(tmp_ctx);
+       return status;
 }
 
 /* List all domain groups */