2 Unix SMB/CIFS implementation.
3 async implementation of WINBINDD_LIST_GROUPS
4 Copyright (C) Volker Lendecke 2009
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "librpc/gen_ndr/ndr_winbind_c.h"
24 struct winbindd_list_groups_domstate
{
25 struct tevent_req
*subreq
;
26 struct winbindd_domain
*domain
;
27 struct wbint_Principals groups
;
30 struct winbindd_list_groups_state
{
34 struct winbindd_list_groups_domstate
*domains
;
37 static void winbindd_list_groups_done(struct tevent_req
*subreq
);
39 struct tevent_req
*winbindd_list_groups_send(TALLOC_CTX
*mem_ctx
,
40 struct tevent_context
*ev
,
41 struct winbindd_cli_state
*cli
,
42 struct winbindd_request
*request
)
44 struct tevent_req
*req
;
45 struct winbindd_list_groups_state
*state
;
46 struct winbindd_domain
*domain
;
49 req
= tevent_req_create(mem_ctx
, &state
,
50 struct winbindd_list_groups_state
);
55 if (request
->wb_flags
& WBFLAG_FROM_NSS
&& !lp_winbind_enum_groups()) {
57 return tevent_req_post(req
, ev
);
60 /* Ensure null termination */
61 request
->domain_name
[sizeof(request
->domain_name
)-1]='\0';
63 DEBUG(3, ("list_groups %s\n", request
->domain_name
));
65 if (request
->domain_name
[0] != '\0') {
66 state
->num_domains
= 1;
68 state
->num_domains
= 0;
69 for (domain
= domain_list(); domain
; domain
= domain
->next
) {
70 state
->num_domains
+= 1;
74 state
->domains
= talloc_array(state
,
75 struct winbindd_list_groups_domstate
,
77 if (tevent_req_nomem(state
->domains
, req
)) {
78 return tevent_req_post(req
, ev
);
81 if (request
->domain_name
[0] != '\0') {
82 ZERO_STRUCT(state
->domains
[0].groups
);
84 state
->domains
[0].domain
= find_domain_from_name_noinit(
85 request
->domain_name
);
86 if (state
->domains
[0].domain
== NULL
) {
87 tevent_req_nterror(req
, NT_STATUS_NO_SUCH_DOMAIN
);
88 return tevent_req_post(req
, ev
);
92 for (domain
= domain_list(); domain
; domain
= domain
->next
) {
93 ZERO_STRUCT(state
->domains
[i
].groups
);
95 state
->domains
[i
].domain
= domain
;
100 for (i
=0; i
<state
->num_domains
; i
++) {
101 struct winbindd_list_groups_domstate
*d
= &state
->domains
[i
];
103 d
->subreq
= dcerpc_wbint_QueryGroupList_send(
104 state
->domains
, ev
, dom_child_handle(d
->domain
),
106 if (tevent_req_nomem(d
->subreq
, req
)) {
107 TALLOC_FREE(state
->domains
);
108 return tevent_req_post(req
, ev
);
110 tevent_req_set_callback(d
->subreq
, winbindd_list_groups_done
,
113 state
->num_received
= 0;
117 static void winbindd_list_groups_done(struct tevent_req
*subreq
)
119 struct tevent_req
*req
= tevent_req_callback_data(
120 subreq
, struct tevent_req
);
121 struct winbindd_list_groups_state
*state
= tevent_req_data(
122 req
, struct winbindd_list_groups_state
);
123 NTSTATUS status
, result
;
126 status
= dcerpc_wbint_QueryGroupList_recv(subreq
, state
->domains
,
129 for (i
=0; i
<state
->num_domains
; i
++) {
130 if (subreq
== state
->domains
[i
].subreq
) {
134 if (i
< state
->num_domains
) {
135 struct winbindd_list_groups_domstate
*d
= &state
->domains
[i
];
137 DEBUG(10, ("Domain %s returned %d groups\n", d
->domain
->name
,
138 d
->groups
.num_principals
));
142 if (!NT_STATUS_IS_OK(status
) || !NT_STATUS_IS_OK(result
)) {
143 DEBUG(10, ("list_groups for domain %s failed\n",
145 d
->groups
.num_principals
= 0;
151 state
->num_received
+= 1;
153 if (state
->num_received
>= state
->num_domains
) {
154 tevent_req_done(req
);
158 NTSTATUS
winbindd_list_groups_recv(struct tevent_req
*req
,
159 struct winbindd_response
*response
)
161 struct winbindd_list_groups_state
*state
= tevent_req_data(
162 req
, struct winbindd_list_groups_state
);
166 uint32_t j
, num_entries
= 0;
169 if (tevent_req_is_nterror(req
, &status
)) {
174 response
->data
.num_entries
= 0;
175 for (i
=0; i
<state
->num_domains
; i
++) {
176 struct winbindd_list_groups_domstate
*d
= &state
->domains
[i
];
178 for (j
=0; j
<d
->groups
.num_principals
; j
++) {
180 name
= fill_domain_username_talloc(response
, d
->domain
->name
,
181 d
->groups
.principals
[j
].name
,
184 return NT_STATUS_NO_MEMORY
;
186 len
+= strlen(name
)+1;
188 response
->data
.num_entries
+= d
->groups
.num_principals
;
191 result
= talloc_array(response
, char, len
+1);
193 return NT_STATUS_NO_MEMORY
;
197 for (i
=0; i
<state
->num_domains
; i
++) {
198 struct winbindd_list_groups_domstate
*d
= &state
->domains
[i
];
200 for (j
=0; j
<d
->groups
.num_principals
; j
++) {
203 name
= fill_domain_username_talloc(response
, d
->domain
->name
,
204 d
->groups
.principals
[j
].name
,
207 return NT_STATUS_NO_MEMORY
;
209 this_len
= strlen(name
);
210 memcpy(result
+len
, name
, this_len
);
217 result
[len
-1] = '\0';
219 response
->data
.num_entries
= num_entries
;
220 response
->extra_data
.data
= result
;
221 response
->length
+= len
;