summaryrefslogtreecommitdiff
path: root/source3/winbindd/winbindd_group.c
blob: 4e4c961e564472eacb422959b620d37da61f7d43 (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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/* 
   Unix SMB/CIFS implementation.

   Winbind daemon for ntdom nss module

   Copyright (C) Tim Potter 2000
   Copyright (C) Jeremy Allison 2001.
   Copyright (C) Gerald (Jerry) Carter 2003.
   Copyright (C) Volker Lendecke 2005
   
   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 "winbindd.h"

#undef DBGC_CLASS
#define DBGC_CLASS DBGC_WINBIND

/* Fill a grent structure from various other information */

bool fill_grent(TALLOC_CTX *mem_ctx, struct winbindd_gr *gr,
		const char *dom_name, const char *gr_name, gid_t unix_gid)
{
	fstring full_group_name;
	char *mapped_name = NULL;
	struct winbindd_domain *domain = find_domain_from_name_noinit(dom_name);
	NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;

	nt_status = normalize_name_map(mem_ctx, domain, gr_name,
				       &mapped_name);

	/* Basic whitespace replacement */
	if (NT_STATUS_IS_OK(nt_status)) {
		fill_domain_username(full_group_name, dom_name,
				     mapped_name, true);
	}
	/* Mapped to an aliase */
	else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_FILE_RENAMED)) {
		fstrcpy(full_group_name, mapped_name);
	}
	/* no change */
	else {
		fill_domain_username( full_group_name, dom_name,
				      gr_name, True );
	}

	gr->gr_gid = unix_gid;

	/* Group name and password */

	safe_strcpy(gr->gr_name, full_group_name, sizeof(gr->gr_name) - 1);
	safe_strcpy(gr->gr_passwd, "x", sizeof(gr->gr_passwd) - 1);

	return True;
}

/* Get the list of domain groups and domain aliases for a domain.  We fill in
   the sam_entries and num_sam_entries fields with domain group information.
   Return True if some groups were returned, False otherwise. */

bool get_sam_group_entries(struct getent_state *ent)
{
	NTSTATUS status;
	uint32 num_entries;
	struct acct_info *name_list = NULL;
	TALLOC_CTX *mem_ctx;
	bool result = False;
	struct acct_info *sam_grp_entries = NULL;
	struct winbindd_domain *domain;

	if (ent->got_sam_entries)
		return False;

	if (!(mem_ctx = talloc_init("get_sam_group_entries(%s)",
					  ent->domain_name))) {
		DEBUG(1, ("get_sam_group_entries: "
			  "could not create talloc context!\n"));
		return False;
	}

	/* Free any existing group info */

	SAFE_FREE(ent->sam_entries);
	ent->num_sam_entries = 0;
	ent->got_sam_entries = True;

	/* Enumerate domain groups */

	num_entries = 0;

	if (!(domain = find_domain_from_name(ent->domain_name))) {
		DEBUG(3, ("no such domain %s in get_sam_group_entries\n",
			  ent->domain_name));
		goto done;
	}

	/* always get the domain global groups */

	status = domain->methods->enum_dom_groups(domain, mem_ctx, &num_entries,
						  &sam_grp_entries);

	if (!NT_STATUS_IS_OK(status)) {
		DEBUG(3, ("get_sam_group_entries: "
			  "could not enumerate domain groups! Error: %s\n",
			  nt_errstr(status)));
		result = False;
		goto done;
	}

	/* Copy entries into return buffer */

	if (num_entries) {
		name_list = SMB_MALLOC_ARRAY(struct acct_info, num_entries);
		if (!name_list) {
			DEBUG(0,("get_sam_group_entries: Failed to malloc "
				 "memory for %d domain groups!\n",
				 num_entries));
			result = False;
			goto done;
		}
		memcpy(name_list, sam_grp_entries,
			num_entries * sizeof(struct acct_info));
	}

	ent->num_sam_entries = num_entries;

	/* get the domain local groups if we are a member of a native win2k
	 * domain and are not using LDAP to get the groups */

	if ( ( lp_security() != SEC_ADS && domain->native_mode
		&& domain->primary) || domain->internal )
	{
		DEBUG(4,("get_sam_group_entries: %s domain; "
			 "enumerating local groups as well\n",
			 domain->native_mode ? "Native Mode 2k":
						"BUILTIN or local"));

		status = domain->methods->enum_local_groups(domain, mem_ctx,
							    &num_entries,
							    &sam_grp_entries);

		if ( !NT_STATUS_IS_OK(status) ) {
			DEBUG(3,("get_sam_group_entries: "
				"Failed to enumerate "
				"domain local groups with error %s!\n",
				nt_errstr(status)));
			num_entries = 0;
		}
		else
			DEBUG(4,("get_sam_group_entries: "
				 "Returned %d local groups\n",
				 num_entries));

		/* Copy entries into return buffer */

		if ( num_entries ) {
			name_list = SMB_REALLOC_ARRAY(name_list,
						      struct acct_info,
						      ent->num_sam_entries+
							num_entries);
			if (!name_list) {
				DEBUG(0,("get_sam_group_entries: "
					 "Failed to realloc more memory "
					 "for %d local groups!\n",
					 num_entries));
				result = False;
				goto done;
			}

			memcpy(&name_list[ent->num_sam_entries],
				sam_grp_entries,
				num_entries * sizeof(struct acct_info));
		}

		ent->num_sam_entries += num_entries;
	}


	/* Fill in remaining fields */

	ent->sam_entries = name_list;
	ent->sam_entry_index = 0;

	result = (ent->num_sam_entries > 0);

 done:
	talloc_destroy(mem_ctx);

	return result;
}

enum winbindd_result winbindd_dual_getsidaliases(struct winbindd_domain *domain,
						 struct winbindd_cli_state *state)
{
	DOM_SID *sids = NULL;
	size_t num_sids = 0;
	char *sidstr = NULL;
	ssize_t len;
	size_t i;
	uint32 num_aliases;
	uint32 *alias_rids;
	NTSTATUS result;

	DEBUG(3, ("[%5lu]: getsidaliases\n", (unsigned long)state->pid));

	sidstr = state->request->extra_data.data;
	if (sidstr == NULL) {
		sidstr = talloc_strdup(state->mem_ctx, "\n"); /* No SID */
		if (!sidstr) {
			DEBUG(0, ("Out of memory\n"));
			return WINBINDD_ERROR;
		}
	}

	DEBUG(10, ("Sidlist: %s\n", sidstr));

	if (!parse_sidlist(state->mem_ctx, sidstr, &sids, &num_sids)) {
		DEBUG(0, ("Could not parse SID list: %s\n", sidstr));
		return WINBINDD_ERROR;
	}

	num_aliases = 0;
	alias_rids = NULL;

	result = domain->methods->lookup_useraliases(domain,
						     state->mem_ctx,
						     num_sids, sids,
						     &num_aliases,
						     &alias_rids);

	if (!NT_STATUS_IS_OK(result)) {
		DEBUG(3, ("Could not lookup_useraliases: %s\n",
			  nt_errstr(result)));
		return WINBINDD_ERROR;
	}

	num_sids = 0;
	sids = NULL;
	sidstr = NULL;

	DEBUG(10, ("Got %d aliases\n", num_aliases));

	for (i=0; i<num_aliases; i++) {
		DOM_SID sid;
		DEBUGADD(10, (" rid %d\n", alias_rids[i]));
		sid_copy(&sid, &domain->sid);
		sid_append_rid(&sid, alias_rids[i]);
		result = add_sid_to_array(state->mem_ctx, &sid, &sids,
					  &num_sids);
		if (!NT_STATUS_IS_OK(result)) {
			return WINBINDD_ERROR;
		}
	}


	if (!print_sidlist(state->mem_ctx, sids, num_sids, &sidstr, &len)) {
		DEBUG(0, ("Could not print_sidlist\n"));
		state->response->extra_data.data = NULL;
		return WINBINDD_ERROR;
	}

	state->response->extra_data.data = NULL;

	if (sidstr) {
		state->response->extra_data.data = sidstr;
		DEBUG(10, ("aliases_list: %s\n",
			   (char *)state->response->extra_data.data));
		state->response->length += len+1;
		state->response->data.num_entries = num_sids;
	}

	return WINBINDD_OK;
}

struct getgr_countmem {
	int num;
	size_t len;
};

static int getgr_calc_memberlen(DATA_BLOB key, void *data, void *priv)
{
	struct wbint_Principal *m = talloc_get_type_abort(
		data, struct wbint_Principal);
	struct getgr_countmem *buf = (struct getgr_countmem *)priv;

	buf->num += 1;
	buf->len += strlen(m->name) + 1;
	return 0;
}

struct getgr_stringmem {
	size_t ofs;
	char *buf;
};

static int getgr_unparse_members(DATA_BLOB key, void *data, void *priv)
{
	struct wbint_Principal *m = talloc_get_type_abort(
		data, struct wbint_Principal);
	struct getgr_stringmem *buf = (struct getgr_stringmem *)priv;
	int len;

	len = strlen(m->name);

	memcpy(buf->buf + buf->ofs, m->name, len);
	buf->ofs += len;
	buf->buf[buf->ofs] = ',';
	buf->ofs += 1;
	return 0;
}

NTSTATUS winbindd_print_groupmembers(struct talloc_dict *members,
				     TALLOC_CTX *mem_ctx,
				     int *num_members, char **result)
{
	struct getgr_countmem c;
	struct getgr_stringmem m;
	int res;

	c.num = 0;
	c.len = 0;

	res = talloc_dict_traverse(members, getgr_calc_memberlen, &c);
	if (res != 0) {
		DEBUG(5, ("talloc_dict_traverse failed\n"));
		return NT_STATUS_INTERNAL_ERROR;
	}

	m.ofs = 0;
	m.buf = talloc_array(mem_ctx, char, c.len);
	if (m.buf == NULL) {
		DEBUG(5, ("talloc failed\n"));
		return NT_STATUS_NO_MEMORY;
	}

	res = talloc_dict_traverse(members, getgr_unparse_members, &m);
	if (res != 0) {
		DEBUG(5, ("talloc_dict_traverse failed\n"));
		TALLOC_FREE(m.buf);
		return NT_STATUS_INTERNAL_ERROR;
	}
	m.buf[c.len-1] = '\0';

	*num_members = c.num;
	*result = m.buf;
	return NT_STATUS_OK;
}