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/cli_wbint.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 /* Ensure null termination */
56 request
->domain_name
[sizeof(request
->domain_name
)-1]='\0';
58 DEBUG(3, ("list_groups %s\n", request
->domain_name
));
60 if (request
->domain_name
[0] != '\0') {
61 state
->num_domains
= 1;
63 state
->num_domains
= 0;
64 for (domain
= domain_list(); domain
; domain
= domain
->next
) {
65 state
->num_domains
+= 1;
69 state
->domains
= talloc_array(state
,
70 struct winbindd_list_groups_domstate
,
72 if (tevent_req_nomem(state
->domains
, req
)) {
73 return tevent_req_post(req
, ev
);
76 if (request
->domain_name
[0] != '\0') {
77 state
->domains
[0].domain
= find_domain_from_name_noinit(
78 request
->domain_name
);
79 if (state
->domains
[0].domain
== NULL
) {
80 tevent_req_nterror(req
, NT_STATUS_NO_SUCH_DOMAIN
);
81 return tevent_req_post(req
, ev
);
85 for (domain
= domain_list(); domain
; domain
= domain
->next
) {
86 state
->domains
[i
++].domain
= domain
;
90 for (i
=0; i
<state
->num_domains
; i
++) {
91 struct winbindd_list_groups_domstate
*d
= &state
->domains
[i
];
93 d
->subreq
= rpccli_wbint_QueryGroupList_send(
94 state
->domains
, ev
, d
->domain
->child
.rpccli
,
96 if (tevent_req_nomem(d
->subreq
, req
)) {
97 TALLOC_FREE(state
->domains
);
98 return tevent_req_post(req
, ev
);
100 tevent_req_set_callback(d
->subreq
, winbindd_list_groups_done
,
103 state
->num_received
= 0;
107 static void winbindd_list_groups_done(struct tevent_req
*subreq
)
109 struct tevent_req
*req
= tevent_req_callback_data(
110 subreq
, struct tevent_req
);
111 struct winbindd_list_groups_state
*state
= tevent_req_data(
112 req
, struct winbindd_list_groups_state
);
113 NTSTATUS status
, result
;
116 status
= rpccli_wbint_QueryGroupList_recv(subreq
, state
->domains
,
119 for (i
=0; i
<state
->num_domains
; i
++) {
120 if (subreq
== state
->domains
[i
].subreq
) {
124 if (i
< state
->num_domains
) {
125 struct winbindd_list_groups_domstate
*d
= &state
->domains
[i
];
127 DEBUG(10, ("Domain %s returned %d users\n", d
->domain
->name
,
128 d
->groups
.num_principals
));
132 if (!NT_STATUS_IS_OK(status
) || !NT_STATUS_IS_OK(result
)) {
133 DEBUG(10, ("list_groups for domain %s failed\n",
135 d
->groups
.num_principals
= 0;
141 state
->num_received
+= 1;
143 if (state
->num_received
>= state
->num_domains
) {
144 tevent_req_done(req
);
148 NTSTATUS
winbindd_list_groups_recv(struct tevent_req
*req
,
149 struct winbindd_response
*response
)
151 struct winbindd_list_groups_state
*state
= tevent_req_data(
152 req
, struct winbindd_list_groups_state
);
156 uint32_t j
, num_entries
= 0;
159 if (tevent_req_is_nterror(req
, &status
)) {
164 for (i
=0; i
<state
->num_domains
; i
++) {
165 struct winbindd_list_groups_domstate
*d
= &state
->domains
[i
];
167 for (j
=0; j
<d
->groups
.num_principals
; j
++) {
169 fill_domain_username(name
, d
->domain
->name
,
170 d
->groups
.principals
[j
].name
,
172 len
+= strlen(name
)+1;
176 result
= talloc_array(response
, char, len
+1);
178 return NT_STATUS_NO_MEMORY
;
182 for (i
=0; i
<state
->num_domains
; i
++) {
183 struct winbindd_list_groups_domstate
*d
= &state
->domains
[i
];
185 for (j
=0; j
<d
->groups
.num_principals
; j
++) {
188 fill_domain_username(name
, d
->domain
->name
,
189 d
->groups
.principals
[j
].name
,
191 this_len
= strlen(name
);
192 memcpy(result
+len
, name
, this_len
);
199 result
[len
-1] = '\0';
201 response
->data
.num_entries
= num_entries
;
202 response
->extra_data
.data
= result
;
203 response
->length
+= len
;