samba-tool group: Add --special parameter to add predefined special group
[Samba.git] / source3 / winbindd / winbindd_list_groups.c
blob6541de5be0f488849c94e120c408709c84f6e3c3
1 /*
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/>.
20 #include "includes.h"
21 #include "winbindd.h"
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 {
31 int num_received;
32 /* All domains */
33 int num_domains;
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;
47 int i;
49 req = tevent_req_create(mem_ctx, &state,
50 struct winbindd_list_groups_state);
51 if (req == NULL) {
52 return NULL;
55 if (request->wb_flags & WBFLAG_FROM_NSS && !lp_winbind_enum_groups()) {
56 tevent_req_done(req);
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;
67 } else {
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,
76 state->num_domains);
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);
90 } else {
91 i = 0;
92 for (domain = domain_list(); domain; domain = domain->next) {
93 ZERO_STRUCT(state->domains[i].groups);
95 state->domains[i].domain = domain;
96 i++;
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),
105 &d->groups);
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,
111 req);
113 state->num_received = 0;
114 return req;
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;
124 int i;
126 status = dcerpc_wbint_QueryGroupList_recv(subreq, state->domains,
127 &result);
129 for (i=0; i<state->num_domains; i++) {
130 if (subreq == state->domains[i].subreq) {
131 break;
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));
140 d->subreq = NULL;
142 if (!NT_STATUS_IS_OK(status) || !NT_STATUS_IS_OK(result)) {
143 DEBUG(10, ("list_groups for domain %s failed\n",
144 d->domain->name));
145 d->groups.num_principals = 0;
149 TALLOC_FREE(subreq);
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);
163 NTSTATUS status;
164 char *result;
165 int i;
166 uint32_t j, num_entries = 0;
167 size_t len;
169 if (tevent_req_is_nterror(req, &status)) {
170 return status;
173 len = 0;
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++) {
179 const char *name;
180 name = fill_domain_username_talloc(response, d->domain->name,
181 d->groups.principals[j].name,
182 True);
183 if (name == NULL) {
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);
192 if (result == 0) {
193 return NT_STATUS_NO_MEMORY;
196 len = 0;
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++) {
201 const char *name;
202 size_t this_len;
203 name = fill_domain_username_talloc(response, d->domain->name,
204 d->groups.principals[j].name,
205 True);
206 if (name == NULL) {
207 return NT_STATUS_NO_MEMORY;
209 this_len = strlen(name);
210 memcpy(result+len, name, this_len);
211 len += this_len;
212 result[len] = ',';
213 len += 1;
214 num_entries++;
217 result[len-1] = '\0';
219 response->data.num_entries = num_entries;
220 response->extra_data.data = result;
221 response->length += len;
223 return NT_STATUS_OK;