summaryrefslogtreecommitdiff
path: root/source3/winbindd/winbindd_list_groups.c
blob: fba36e009968866b1925b6d4810c83ef921f5c18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
   Unix SMB/CIFS implementation.
   async implementation of WINBINDD_LIST_GROUPS
   Copyright (C) Volker Lendecke 2009

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include "util/debug.h"
#include "winbindd.h"
#include "librpc/gen_ndr/ndr_winbind_c.h"

struct winbindd_list_groups_domstate {
	struct tevent_req *subreq;
	struct winbindd_domain_ref domain;
	struct wbint_Principals groups;
};

struct winbindd_list_groups_state {
	uint32_t num_received;
	/* All domains */
	uint32_t num_domains;
	struct winbindd_list_groups_domstate *domains;
};

static void winbindd_list_groups_done(struct tevent_req *subreq);

struct tevent_req *winbindd_list_groups_send(TALLOC_CTX *mem_ctx,
					     struct tevent_context *ev,
					     struct winbindd_cli_state *cli,
					     struct winbindd_request *request)
{
	struct tevent_req *req;
	struct winbindd_list_groups_state *state;
	struct winbindd_domain *domain;
	uint32_t i;

	req = tevent_req_create(mem_ctx, &state,
				struct winbindd_list_groups_state);
	if (req == NULL) {
		return NULL;
	}

	D_NOTICE("[%s (%u)] Winbind external command LIST_GROUPS start.\n"
		 "WBFLAG_FROM_NSS is %s, winbind enum groups is %d.\n",
		 cli->client_name,
		 (unsigned int)cli->pid,
		 request->wb_flags & WBFLAG_FROM_NSS ? "Set" : "Unset",
		 lp_winbind_enum_groups());

	if (request->wb_flags & WBFLAG_FROM_NSS && !lp_winbind_enum_groups()) {
		tevent_req_done(req);
		return tevent_req_post(req, ev);
	}

	/* Ensure null termination */
	request->domain_name[sizeof(request->domain_name)-1]='\0';

	if (request->domain_name[0] != '\0') {
		state->num_domains = 1;
		D_DEBUG("List groups for domain %s.\n", request->domain_name);
	} else {
		state->num_domains = 0;
		for (domain = domain_list(); domain; domain = domain->next) {
			state->num_domains += 1;
		}
		D_DEBUG("List groups for %"PRIu32" domain(s).\n", state->num_domains);
	}

	state->domains = talloc_array(state,
				      struct winbindd_list_groups_domstate,
				      state->num_domains);
	if (tevent_req_nomem(state->domains, req)) {
		return tevent_req_post(req, ev);
	}

	if (request->domain_name[0] != '\0') {
		ZERO_STRUCT(state->domains[0].groups);

		domain = find_domain_from_name_noinit(
			request->domain_name);
		if (domain == NULL) {
			tevent_req_nterror(req, NT_STATUS_NO_SUCH_DOMAIN);
			return tevent_req_post(req, ev);
		}
		winbindd_domain_ref_set(&state->domains[0].domain,
					domain);
	} else {
		i = 0;
		for (domain = domain_list(); domain; domain = domain->next) {
			ZERO_STRUCT(state->domains[i].groups);

			winbindd_domain_ref_set(&state->domains[i].domain,
						domain);
			i++;
		}
	}

	for (i=0; i<state->num_domains; i++) {
		struct winbindd_list_groups_domstate *d = &state->domains[i];
		bool valid;

		/*
		 * We set the ref a few lines above, it must be valid!
		 */
		valid = winbindd_domain_ref_get(&d->domain, &domain);
		SMB_ASSERT(valid);

		d->subreq = dcerpc_wbint_QueryGroupList_send(
			state->domains, ev, dom_child_handle(domain),
			&d->groups);
		if (tevent_req_nomem(d->subreq, req)) {
			TALLOC_FREE(state->domains);
			return tevent_req_post(req, ev);
		}
		tevent_req_set_callback(d->subreq, winbindd_list_groups_done,
					req);
	}
	state->num_received = 0;
	return req;
}

static void winbindd_list_groups_done(struct tevent_req *subreq)
{
	struct tevent_req *req = tevent_req_callback_data(
		subreq, struct tevent_req);
	struct winbindd_list_groups_state *state = tevent_req_data(
		req, struct winbindd_list_groups_state);
	NTSTATUS status, result;
	uint32_t i;

	status = dcerpc_wbint_QueryGroupList_recv(subreq, state->domains,
						  &result);

	for (i=0; i<state->num_domains; i++) {
		if (subreq == state->domains[i].subreq) {
			break;
		}
	}
	if (i < state->num_domains) {
		struct winbindd_list_groups_domstate *d = &state->domains[i];
		struct winbindd_domain *domain = NULL;
		bool valid;

		valid = winbindd_domain_ref_get(&d->domain, &domain);
		if (!valid) {
			/*
			 * winbindd_domain_ref_get() already generated
			 * a debug message for the stale domain!
			 */
			d->subreq = NULL;
			d->groups.num_principals = 0;
			goto skip;
		}

		D_DEBUG("Domain %s returned %"PRIu32" groups\n", domain->name,
			   d->groups.num_principals);

		d->subreq = NULL;

		if (!NT_STATUS_IS_OK(status) || !NT_STATUS_IS_OK(result)) {
			D_WARNING("list_groups for domain %s failed\n",
				   domain->name);
			d->groups.num_principals = 0;
		}
	}

skip:
	TALLOC_FREE(subreq);

	state->num_received += 1;

	if (state->num_received >= state->num_domains) {
		tevent_req_done(req);
	}
}

NTSTATUS winbindd_list_groups_recv(struct tevent_req *req,
				   struct winbindd_response *response)
{
	struct winbindd_list_groups_state *state = tevent_req_data(
		req, struct winbindd_list_groups_state);
	NTSTATUS status;
	char *result;
	uint32_t i, j, num_entries = 0;
	size_t len;

	D_NOTICE("Winbind external command LIST_GROUPS end.\n");
	if (tevent_req_is_nterror(req, &status)) {
		D_WARNING("Failed with %s.\n", nt_errstr(status));
		return status;
	}

	len = 0;
	response->data.num_entries = 0;
	for (i=0; i<state->num_domains; i++) {
		struct winbindd_list_groups_domstate *d = &state->domains[i];
		struct winbindd_domain *domain = NULL;
		bool valid;

		valid = winbindd_domain_ref_get(&d->domain, &domain);
		if (!valid) {
			/*
			 * winbindd_domain_ref_get() already generated
			 * a debug message for the stale domain!
			 */
			d->groups.num_principals = 0;
			continue;
		}

		for (j=0; j<d->groups.num_principals; j++) {
			const char *name;
			name = fill_domain_username_talloc(response, domain->name,
					     d->groups.principals[j].name,
					     True);
			if (name == NULL) {
				return NT_STATUS_NO_MEMORY;
			}
			len += strlen(name)+1;
		}
		response->data.num_entries += d->groups.num_principals;
	}

	result = talloc_array(response, char, len+1);
	if (result == 0) {
		return NT_STATUS_NO_MEMORY;
	}

	len = 0;
	for (i=0; i<state->num_domains; i++) {
		struct winbindd_list_groups_domstate *d = &state->domains[i];
		struct winbindd_domain *domain = NULL;
		bool valid;

		valid = winbindd_domain_ref_get(&d->domain, &domain);
		if (!valid) {
			/*
			 * winbindd_domain_ref_get() already generated
			 * a debug message for the stale domain!
			 */
			d->groups.num_principals = 0;
			continue;
		}

		for (j=0; j<d->groups.num_principals; j++) {
			const char *name;
			size_t this_len;
			name = fill_domain_username_talloc(response, domain->name,
					     d->groups.principals[j].name,
					     True);
			if (name == NULL) {
				return NT_STATUS_NO_MEMORY;
			}
			this_len = strlen(name);
			memcpy(result+len, name, this_len);
			len += this_len;
			result[len] = ',';
			len += 1;
			num_entries++;
		}
	}
	result[len-1] = '\0';

	response->data.num_entries = num_entries;
	response->extra_data.data = result;
	response->length += len;

	return NT_STATUS_OK;
}