From 11610a4e996fd70a2df3b1124f8e658cc9188827 Mon Sep 17 00:00:00 2001
From: Andreas Schneider <asn@samba.org>
Date: Thu, 17 Jun 2010 13:56:54 +0200
Subject: s3-winbind: Use rpc_enum_dom_groups in msrpc.

---
 source3/winbindd/winbindd_msrpc.c | 94 +++++++++++++++++----------------------
 1 file changed, 42 insertions(+), 52 deletions(-)

(limited to 'source3/winbindd')

diff --git a/source3/winbindd/winbindd_msrpc.c b/source3/winbindd/winbindd_msrpc.c
index 1515cfa6e3..9dba883c47 100644
--- a/source3/winbindd/winbindd_msrpc.c
+++ b/source3/winbindd/winbindd_msrpc.c
@@ -24,6 +24,8 @@
 
 #include "includes.h"
 #include "winbindd.h"
+#include "winbindd_rpc.h"
+
 #include "../librpc/gen_ndr/cli_samr.h"
 #include "rpc_client/cli_samr.h"
 #include "../librpc/gen_ndr/cli_lsa.h"
@@ -135,72 +137,60 @@ static NTSTATUS query_user_list(struct winbindd_domain *domain,
 }
 
 /* list all domain groups */
-static NTSTATUS enum_dom_groups(struct winbindd_domain *domain,
-				TALLOC_CTX *mem_ctx,
-				uint32 *num_entries, 
-				struct acct_info **info)
+static NTSTATUS msrpc_enum_dom_groups(struct winbindd_domain *domain,
+				      TALLOC_CTX *mem_ctx,
+				      uint32_t *pnum_info,
+				      struct acct_info **pinfo)
 {
+	struct rpc_pipe_client *samr_pipe;
 	struct policy_handle dom_pol;
+	struct acct_info *info = NULL;
+	uint32_t num_info = 0;
+	TALLOC_CTX *tmp_ctx;
 	NTSTATUS status;
-	uint32 start = 0;
-	struct rpc_pipe_client *cli;
 
-	*num_entries = 0;
-	*info = NULL;
+	DEBUG(3,("msrpc_enum_dom_groups\n"));
+
+	if (pnum_info) {
+		*pnum_info = 0;
+	}
 
-	DEBUG(3,("rpc: enum_dom_groups\n"));
+	tmp_ctx = talloc_stackframe();
+	if (tmp_ctx == NULL) {
+		return NT_STATUS_NO_MEMORY;
+	}
 
 	if ( !winbindd_can_contact_domain( domain ) ) {
 		DEBUG(10,("enum_domain_groups: No incoming trust for domain %s\n",
 			  domain->name));
-		return NT_STATUS_OK;
+		status = NT_STATUS_OK;
+		goto done;
 	}
 
-	status = cm_connect_sam(domain, mem_ctx, &cli, &dom_pol);
-	if (!NT_STATUS_IS_OK(status))
-		return status;
-
-	do {
-		struct samr_SamArray *sam_array = NULL;
-		uint32 count = 0;
-		TALLOC_CTX *mem_ctx2;
-		int g;
-
-		mem_ctx2 = talloc_init("enum_dom_groups[rpc]");
-
-		/* start is updated by this call. */
-		status = rpccli_samr_EnumDomainGroups(cli, mem_ctx2,
-						      &dom_pol,
-						      &start,
-						      &sam_array,
-						      0xFFFF, /* buffer size? */
-						      &count);
-
-		if (!NT_STATUS_IS_OK(status) &&
-		    !NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
-			talloc_destroy(mem_ctx2);
-			break;
-		}
-
-		(*info) = TALLOC_REALLOC_ARRAY(mem_ctx, *info,
-					       struct acct_info,
-					       (*num_entries) + count);
-		if (! *info) {
-			talloc_destroy(mem_ctx2);
-			return NT_STATUS_NO_MEMORY;
-		}
+	status = cm_connect_sam(domain, tmp_ctx, &samr_pipe, &dom_pol);
+	if (!NT_STATUS_IS_OK(status)) {
+		goto done;
+	}
 
-		for (g=0; g < count; g++) {
+	status = rpc_enum_dom_groups(tmp_ctx,
+				     samr_pipe,
+				     &dom_pol,
+				     &num_info,
+				     &info);
+	if (!NT_STATUS_IS_OK(status)) {
+		goto done;
+	}
 
-			fstrcpy((*info)[*num_entries + g].acct_name,
-				sam_array->entries[g].name.string);
-			(*info)[*num_entries + g].rid = sam_array->entries[g].idx;
-		}
+	if (pnum_info) {
+		*pnum_info = num_info;
+	}
 
-		(*num_entries) += count;
-		talloc_destroy(mem_ctx2);
-	} while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
+	if (pinfo) {
+		*pinfo = talloc_move(mem_ctx, &info);
+	}
 
+done:
+	TALLOC_FREE(tmp_ctx);
 	return status;
 }
 
@@ -1314,7 +1304,7 @@ NTSTATUS winbindd_lookup_names(TALLOC_CTX *mem_ctx,
 struct winbindd_methods msrpc_methods = {
 	False,
 	query_user_list,
-	enum_dom_groups,
+	msrpc_enum_dom_groups,
 	enum_local_groups,
 	msrpc_name_to_sid,
 	msrpc_sid_to_name,
-- 
cgit