s3-winbind: Added a common rpc_enum_dom_groups function.
[kamenim/samba.git] / source3 / winbindd / winbindd_rpc.c
1 /*
2  * Unix SMB/CIFS implementation.
3  *
4  * Winbind rpc backend functions
5  *
6  * Copyright (c) 2000-2003 Tim Potter
7  * Copyright (c) 2001      Andrew Tridgell
8  * Copyright (c) 2005      Volker Lendecke
9  * Copyright (c) 2008      Guenther Deschner (pidl conversion)
10  * Copyright (c) 2010      Andreas Schneider <asn@samba.org>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24  */
25
26 #include "includes.h"
27 #include "winbindd.h"
28 #include "winbindd_rpc.h"
29
30 #include "librpc/gen_ndr/cli_samr.h"
31 #include "librpc/gen_ndr/srv_samr.h"
32 #include "librpc/gen_ndr/cli_lsa.h"
33 #include "librpc/gen_ndr/srv_lsa.h"
34 #include "rpc_client/cli_samr.h"
35 #include "rpc_client/cli_lsarpc.h"
36
37 /* List all domain groups */
38 NTSTATUS rpc_enum_dom_groups(TALLOC_CTX *mem_ctx,
39                              struct rpc_pipe_client *samr_pipe,
40                              struct policy_handle *samr_policy,
41                              uint32_t *pnum_info,
42                              struct acct_info **pinfo)
43 {
44         struct acct_info *info = NULL;
45         uint32_t start = 0;
46         uint32_t num_info = 0;
47         NTSTATUS status;
48
49         *pnum_info = 0;
50
51         do {
52                 struct samr_SamArray *sam_array = NULL;
53                 uint32_t count = 0;
54                 uint32_t g;
55
56                 /* start is updated by this call. */
57                 status = rpccli_samr_EnumDomainGroups(samr_pipe,
58                                                       mem_ctx,
59                                                       samr_policy,
60                                                       &start,
61                                                       &sam_array,
62                                                       0xFFFF, /* buffer size? */
63                                                       &count);
64                 if (!NT_STATUS_IS_OK(status)) {
65                         if (!NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
66                                 DEBUG(2,("query_user_list: failed to enum domain groups: %s\n",
67                                          nt_errstr(status)));
68                                 return status;
69                         }
70                 }
71
72                 info = TALLOC_REALLOC_ARRAY(mem_ctx,
73                                             info,
74                                             struct acct_info,
75                                             num_info + count);
76                 if (info == NULL) {
77                         return NT_STATUS_NO_MEMORY;
78                 }
79
80                 for (g = 0; g < count; g++) {
81                         fstrcpy(info[num_info + g].acct_name,
82                                 sam_array->entries[g].name.string);
83
84                         info[num_info + g].rid = sam_array->entries[g].idx;
85                 }
86
87                 num_info += count;
88         } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
89
90         *pnum_info = num_info;
91         *pinfo = info;
92
93         return NT_STATUS_OK;
94 }