2 Unix SMB/CIFS implementation.
4 Winbind rpc backend functions
6 Copyright (C) Tim Potter 2000-2001
7 Copyright (C) Andrew Tridgell 2001
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 /* Query display info for a domain. This returns enough information plus a
27 bit extra to give an overview of domain users for the User Manager
29 static NTSTATUS query_user_list(struct winbindd_domain *domain,
32 WINBIND_USERINFO **info)
35 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
37 BOOL got_dom_pol = False;
38 uint32 des_access = SEC_RIGHTS_MAXIMUM_ALLOWED;
46 if (!(hnd = cm_get_sam_handle(domain->name)))
49 /* Get domain handle */
51 result = cli_samr_open_domain(hnd->cli, mem_ctx, &hnd->pol,
52 des_access, &domain->sid, &dom_pol);
54 if (!NT_STATUS_IS_OK(result))
63 uint32 count = 0, start=i;
67 ctr.sam.info1 = &info1;
69 ctx2 = talloc_init_named("winbindd dispinfo");
71 result = NT_STATUS_NO_MEMORY;
75 /* Query display info level 1 */
76 result = cli_samr_query_dispinfo(hnd->cli, ctx2,
78 &count, 0xFFFF, &ctr);
80 if (!NT_STATUS_IS_OK(result) &&
81 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) break;
83 (*num_entries) += count;
85 /* now map the result into the WINBIND_USERINFO structure */
86 (*info) = talloc_realloc(mem_ctx, *info,
87 (*num_entries)*sizeof(WINBIND_USERINFO));
89 result = NT_STATUS_NO_MEMORY;
94 for (j=0;j<count;i++, j++) {
95 (*info)[i].acct_name = unistr2_tdup(mem_ctx, &info1.str[j].uni_acct_name);
96 (*info)[i].full_name = unistr2_tdup(mem_ctx, &info1.str[j].uni_full_name);
97 (*info)[i].user_rid = info1.sam[j].rid_user;
98 /* For the moment we set the primary group for
99 every user to be the Domain Users group.
100 There are serious problems with determining
101 the actual primary group for large domains.
102 This should really be made into a 'winbind
103 force group' smb.conf parameter or
104 something like that. */
105 (*info)[i].group_rid = DOMAIN_GROUP_RID_USERS;
108 talloc_destroy(ctx2);
109 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
114 cli_samr_close(hnd->cli, mem_ctx, &dom_pol);
119 /* list all domain groups */
120 static NTSTATUS enum_dom_groups(struct winbindd_domain *domain,
123 struct acct_info **info)
125 uint32 des_access = SEC_RIGHTS_MAXIMUM_ALLOWED;
133 if (!(hnd = cm_get_sam_handle(domain->name))) {
134 return NT_STATUS_UNSUCCESSFUL;
137 status = cli_samr_open_domain(hnd->cli, mem_ctx,
138 &hnd->pol, des_access, &domain->sid, &dom_pol);
139 if (!NT_STATUS_IS_OK(status)) {
144 struct acct_info *info2 = NULL;
145 uint32 count = 0, start = *num_entries;
146 TALLOC_CTX *mem_ctx2;
148 mem_ctx2 = talloc_init_named("enum_dom_groups[rpc]");
150 status = cli_samr_enum_dom_groups(hnd->cli, mem_ctx2, &dom_pol,
152 0xFFFF, /* buffer size? */
155 if (!NT_STATUS_IS_OK(status) &&
156 !NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
157 talloc_destroy(mem_ctx2);
161 (*info) = talloc_realloc(mem_ctx, *info,
162 sizeof(**info) * ((*num_entries) + count));
164 talloc_destroy(mem_ctx2);
165 cli_samr_close(hnd->cli, mem_ctx, &dom_pol);
166 return NT_STATUS_NO_MEMORY;
169 memcpy(&(*info)[*num_entries], info2, count*sizeof(*info2));
170 (*num_entries) += count;
171 talloc_destroy(mem_ctx2);
172 } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
174 cli_samr_close(hnd->cli, mem_ctx, &dom_pol);
179 /* convert a single name to a sid in a domain */
180 static NTSTATUS name_to_sid(struct winbindd_domain *domain,
183 enum SID_NAME_USE *type)
188 DOM_SID *sids = NULL;
189 uint32 *types = NULL;
191 const char *full_name;
193 if (!(mem_ctx = talloc_init_named("name_to_sid[rpc] for [%s]\\[%s]", domain->name, name))) {
194 DEBUG(0, ("talloc_init failed!\n"));
195 return NT_STATUS_NO_MEMORY;
198 if (!(hnd = cm_get_lsa_handle(domain->name))) {
199 talloc_destroy(mem_ctx);
200 return NT_STATUS_UNSUCCESSFUL;
203 full_name = talloc_asprintf(mem_ctx, "%s\\%s", domain->name, name);
206 DEBUG(0, ("talloc_asprintf failed!\n"));
207 talloc_destroy(mem_ctx);
208 return NT_STATUS_NO_MEMORY;
211 status = cli_lsa_lookup_names(hnd->cli, mem_ctx, &hnd->pol, 1,
212 &full_name, &sids, &types, &num_sids);
214 /* Return rid and type if lookup successful */
215 if (NT_STATUS_IS_OK(status)) {
216 sid_copy(sid, &sids[0]);
220 talloc_destroy(mem_ctx);
225 convert a domain SID to a user or group name
227 static NTSTATUS sid_to_name(struct winbindd_domain *domain,
231 enum SID_NAME_USE *type)
240 if (!(hnd = cm_get_lsa_handle(domain->name)))
241 return NT_STATUS_UNSUCCESSFUL;
243 status = cli_lsa_lookup_sids(hnd->cli, mem_ctx, &hnd->pol,
244 1, sid, &domains, &names, &types,
247 if (NT_STATUS_IS_OK(status)) {
250 DEBUG(5,("Mapped sid to [%s]\\[%s]\n", domains[0], *name));
253 if (strcasecmp(domain->name, domains[0]) != 0) {
254 DEBUG(1, ("domain name from domain param and PDC lookup return differ! (%s vs %s)\n", domain->name, domains[0]));
255 return NT_STATUS_UNSUCCESSFUL;
261 /* Lookup user information from a rid or username. */
262 static NTSTATUS query_user(struct winbindd_domain *domain,
265 WINBIND_USERINFO *user_info)
269 POLICY_HND dom_pol, user_pol;
270 BOOL got_dom_pol = False, got_user_pol = False;
271 SAM_USERINFO_CTR *ctr;
274 if (!(hnd = cm_get_sam_handle(domain->name)))
277 /* Get domain handle */
279 result = cli_samr_open_domain(hnd->cli, mem_ctx, &hnd->pol,
280 SEC_RIGHTS_MAXIMUM_ALLOWED,
281 &domain->sid, &dom_pol);
283 if (!NT_STATUS_IS_OK(result))
288 /* Get user handle */
289 result = cli_samr_open_user(hnd->cli, mem_ctx, &dom_pol,
290 SEC_RIGHTS_MAXIMUM_ALLOWED, user_rid, &user_pol);
292 if (!NT_STATUS_IS_OK(result))
298 result = cli_samr_query_userinfo(hnd->cli, mem_ctx, &user_pol,
301 cli_samr_close(hnd->cli, mem_ctx, &user_pol);
302 got_user_pol = False;
304 user_info->group_rid = ctr->info.id21->group_rid;
305 user_info->acct_name = unistr2_tdup(mem_ctx,
306 &ctr->info.id21->uni_user_name);
307 user_info->full_name = unistr2_tdup(mem_ctx,
308 &ctr->info.id21->uni_full_name);
311 /* Clean up policy handles */
313 cli_samr_close(hnd->cli, mem_ctx, &user_pol);
316 cli_samr_close(hnd->cli, mem_ctx, &dom_pol);
321 /* Lookup groups a user is a member of. I wish Unix had a call like this! */
322 static NTSTATUS lookup_usergroups(struct winbindd_domain *domain,
325 uint32 *num_groups, uint32 **user_gids)
328 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
329 POLICY_HND dom_pol, user_pol;
330 uint32 des_access = SEC_RIGHTS_MAXIMUM_ALLOWED;
331 BOOL got_dom_pol = False, got_user_pol = False;
332 DOM_GID *user_groups;
337 /* First try cached universal groups from logon */
338 *user_gids = uni_group_cache_fetch(&domain->sid, user_rid, mem_ctx, num_groups);
339 if((*num_groups > 0) && *user_gids) {
347 if (!(hnd = cm_get_sam_handle(domain->name)))
350 /* Get domain handle */
351 result = cli_samr_open_domain(hnd->cli, mem_ctx, &hnd->pol,
352 des_access, &domain->sid, &dom_pol);
354 if (!NT_STATUS_IS_OK(result))
359 /* Get user handle */
360 result = cli_samr_open_user(hnd->cli, mem_ctx, &dom_pol,
361 des_access, user_rid, &user_pol);
363 if (!NT_STATUS_IS_OK(result))
368 /* Query user rids */
369 result = cli_samr_query_usergroups(hnd->cli, mem_ctx, &user_pol,
370 num_groups, &user_groups);
372 if (!NT_STATUS_IS_OK(result) || (*num_groups) == 0)
375 (*user_gids) = talloc(mem_ctx, sizeof(uint32) * (*num_groups));
376 for (i=0;i<(*num_groups);i++) {
377 (*user_gids)[i] = user_groups[i].g_rid;
381 /* Clean up policy handles */
383 cli_samr_close(hnd->cli, mem_ctx, &user_pol);
386 cli_samr_close(hnd->cli, mem_ctx, &dom_pol);
392 /* Lookup group membership given a rid. */
393 static NTSTATUS lookup_groupmem(struct winbindd_domain *domain,
395 uint32 group_rid, uint32 *num_names,
396 uint32 **rid_mem, char ***names,
400 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
401 uint32 i, total_names = 0;
402 POLICY_HND dom_pol, group_pol;
403 uint32 des_access = SEC_RIGHTS_MAXIMUM_ALLOWED;
404 BOOL got_dom_pol = False, got_group_pol = False;
410 if (!(hnd = cm_get_sam_handle(domain->name)))
413 /* Get domain handle */
415 result = cli_samr_open_domain(hnd->cli, mem_ctx, &hnd->pol,
416 des_access, &domain->sid, &dom_pol);
418 if (!NT_STATUS_IS_OK(result))
423 /* Get group handle */
425 result = cli_samr_open_group(hnd->cli, mem_ctx, &dom_pol,
426 des_access, group_rid, &group_pol);
428 if (!NT_STATUS_IS_OK(result))
431 got_group_pol = True;
433 /* Step #1: Get a list of user rids that are the members of the
436 result = cli_samr_query_groupmem(hnd->cli, mem_ctx,
437 &group_pol, num_names, rid_mem,
440 if (!NT_STATUS_IS_OK(result))
443 /* Step #2: Convert list of rids into list of usernames. Do this
444 in bunches of ~1000 to avoid crashing NT4. It looks like there
445 is a buffer overflow or something like that lurking around
448 #define MAX_LOOKUP_RIDS 900
450 *names = talloc_zero(mem_ctx, *num_names * sizeof(char *));
451 *name_types = talloc_zero(mem_ctx, *num_names * sizeof(uint32));
453 for (i = 0; i < *num_names; i += MAX_LOOKUP_RIDS) {
454 int num_lookup_rids = MIN(*num_names - i, MAX_LOOKUP_RIDS);
455 uint32 tmp_num_names = 0;
456 char **tmp_names = NULL;
457 uint32 *tmp_types = NULL;
459 /* Lookup a chunk of rids */
461 result = cli_samr_lookup_rids(hnd->cli, mem_ctx,
462 &dom_pol, 1000, /* flags */
466 &tmp_names, &tmp_types);
468 if (!NT_STATUS_IS_OK(result))
471 /* Copy result into array. The talloc system will take
472 care of freeing the temporary arrays later on. */
474 memcpy(&(*names)[i], tmp_names, sizeof(char *) *
477 memcpy(&(*name_types)[i], tmp_types, sizeof(uint32) *
480 total_names += tmp_num_names;
483 *num_names = total_names;
487 cli_samr_close(hnd->cli, mem_ctx, &group_pol);
490 cli_samr_close(hnd->cli, mem_ctx, &dom_pol);
495 /* find the sequence number for a domain */
496 static NTSTATUS sequence_number(struct winbindd_domain *domain, uint32 *seq)
501 uint16 switch_value = 2;
503 uint32 seqnum = DOM_SEQUENCE_NONE;
505 BOOL got_dom_pol = False;
506 uint32 des_access = SEC_RIGHTS_MAXIMUM_ALLOWED;
508 *seq = DOM_SEQUENCE_NONE;
510 if (!(mem_ctx = talloc_init_named("sequence_number[rpc]")))
511 return NT_STATUS_NO_MEMORY;
515 if (!(hnd = cm_get_sam_handle(domain->name)))
518 /* Get domain handle */
520 result = cli_samr_open_domain(hnd->cli, mem_ctx, &hnd->pol,
521 des_access, &domain->sid, &dom_pol);
523 if (!NT_STATUS_IS_OK(result))
528 /* Query domain info */
530 result = cli_samr_query_dom_info(hnd->cli, mem_ctx, &dom_pol,
533 if (NT_STATUS_IS_OK(result)) {
534 seqnum = ctr.info.inf2.seq_num;
535 DEBUG(10,("domain_sequence_number: for domain %s is %u\n", domain->name, (unsigned)seqnum ));
537 DEBUG(10,("domain_sequence_number: failed to get sequence number (%u) for domain %s\n",
538 (unsigned)seqnum, domain->name ));
544 cli_samr_close(hnd->cli, mem_ctx, &dom_pol);
546 talloc_destroy(mem_ctx);
553 /* get a list of trusted domains */
554 static NTSTATUS trusted_domains(struct winbindd_domain *domain,
561 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
566 if (!(hnd = cm_get_lsa_handle(lp_workgroup())))
569 result = cli_lsa_enum_trust_dom(hnd->cli, mem_ctx,
570 &hnd->pol, &enum_ctx, num_domains,
576 /* find the domain sid for a domain */
577 static NTSTATUS domain_sid(struct winbindd_domain *domain, DOM_SID *sid)
579 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
584 if (!(mem_ctx = talloc_init_named("domain_sid[rpc]")))
585 return NT_STATUS_NO_MEMORY;
588 if (!(hnd = cm_get_lsa_handle(domain->name)))
591 status = cli_lsa_query_info_policy(hnd->cli, mem_ctx,
592 &hnd->pol, 0x05, level5_dom, sid);
595 talloc_destroy(mem_ctx);
599 /* the rpc backend methods are exposed via this structure */
600 struct winbindd_methods msrpc_methods = {