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");
70 if (!ctx2) return NT_STATUS_NO_MEMORY;
72 /* Query display info level 1 */
73 result = cli_samr_query_dispinfo(hnd->cli, ctx2,
75 &count, 0xFFFF, &ctr);
77 if (!NT_STATUS_IS_OK(result) &&
78 !NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES)) break;
80 (*num_entries) += count;
82 /* now map the result into the WINBIND_USERINFO structure */
83 (*info) = talloc_realloc(mem_ctx, *info,
84 (*num_entries)*sizeof(WINBIND_USERINFO));
86 return NT_STATUS_NO_MEMORY;
89 for (j=0;j<count;i++, j++) {
90 (*info)[i].acct_name = unistr2_tdup(mem_ctx, &info1.str[j].uni_acct_name);
91 (*info)[i].full_name = unistr2_tdup(mem_ctx, &info1.str[j].uni_full_name);
92 (*info)[i].user_rid = info1.sam[j].rid_user;
93 /* For the moment we set the primary group for
94 every user to be the Domain Users group.
95 There are serious problems with determining
96 the actual primary group for large domains.
97 This should really be made into a 'winbind
98 force group' smb.conf parameter or
99 something like that. */
100 (*info)[i].group_rid = DOMAIN_GROUP_RID_USERS;
103 talloc_destroy(ctx2);
104 } while (NT_STATUS_EQUAL(result, STATUS_MORE_ENTRIES));
109 cli_samr_close(hnd->cli, mem_ctx, &dom_pol);
114 /* list all domain groups */
115 static NTSTATUS enum_dom_groups(struct winbindd_domain *domain,
118 struct acct_info **info)
120 uint32 des_access = SEC_RIGHTS_MAXIMUM_ALLOWED;
128 if (!(hnd = cm_get_sam_handle(domain->name))) {
129 return NT_STATUS_UNSUCCESSFUL;
132 status = cli_samr_open_domain(hnd->cli, mem_ctx,
133 &hnd->pol, des_access, &domain->sid, &dom_pol);
134 if (!NT_STATUS_IS_OK(status)) {
139 struct acct_info *info2 = NULL;
140 uint32 count = 0, start = *num_entries;
141 TALLOC_CTX *mem_ctx2;
143 mem_ctx2 = talloc_init_named("enum_dom_groups[rpc]");
145 status = cli_samr_enum_dom_groups(hnd->cli, mem_ctx2, &dom_pol,
147 0xFFFF, /* buffer size? */
150 if (!NT_STATUS_IS_OK(status) &&
151 !NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES)) {
152 talloc_destroy(mem_ctx2);
156 (*info) = talloc_realloc(mem_ctx, *info,
157 sizeof(**info) * ((*num_entries) + count));
159 talloc_destroy(mem_ctx2);
160 return NT_STATUS_NO_MEMORY;
163 memcpy(&(*info)[*num_entries], info2, count*sizeof(*info2));
164 (*num_entries) += count;
165 talloc_destroy(mem_ctx2);
166 } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
168 cli_samr_close(hnd->cli, mem_ctx, &dom_pol);
173 /* convert a single name to a sid in a domain */
174 static NTSTATUS name_to_sid(struct winbindd_domain *domain,
177 enum SID_NAME_USE *type)
182 DOM_SID *sids = NULL;
183 uint32 *types = NULL;
185 const char *full_name;
187 if (!(mem_ctx = talloc_init_named("name_to_sid[rpc] for [%s]\\[%s]", domain->name, name))) {
188 DEBUG(0, ("talloc_init failed!\n"));
189 return NT_STATUS_NO_MEMORY;
192 if (!(hnd = cm_get_lsa_handle(domain->name))) {
193 talloc_destroy(mem_ctx);
194 return NT_STATUS_UNSUCCESSFUL;
197 full_name = talloc_asprintf(mem_ctx, "%s\\%s", domain->name, name);
200 DEBUG(0, ("talloc_asprintf failed!\n"));
201 talloc_destroy(mem_ctx);
202 return NT_STATUS_NO_MEMORY;
205 status = cli_lsa_lookup_names(hnd->cli, mem_ctx, &hnd->pol, 1,
206 &full_name, &sids, &types, &num_sids);
208 /* Return rid and type if lookup successful */
209 if (NT_STATUS_IS_OK(status)) {
210 sid_copy(sid, &sids[0]);
214 talloc_destroy(mem_ctx);
219 convert a domain SID to a user or group name
221 static NTSTATUS sid_to_name(struct winbindd_domain *domain,
225 enum SID_NAME_USE *type)
234 if (!(hnd = cm_get_lsa_handle(domain->name)))
235 return NT_STATUS_UNSUCCESSFUL;
237 status = cli_lsa_lookup_sids(hnd->cli, mem_ctx, &hnd->pol,
238 1, sid, &domains, &names, &types,
241 if (NT_STATUS_IS_OK(status)) {
244 DEBUG(5,("Mapped sid to [%s]\\[%s]\n", domains[0], *name));
247 if (strcasecmp(domain->name, domains[0]) != 0) {
248 DEBUG(1, ("domain name from domain param and PDC lookup return differ! (%s vs %s)\n", domain->name, domains[0]));
249 return NT_STATUS_UNSUCCESSFUL;
255 /* Lookup user information from a rid or username. */
256 static NTSTATUS query_user(struct winbindd_domain *domain,
259 WINBIND_USERINFO *user_info)
263 POLICY_HND dom_pol, user_pol;
264 BOOL got_dom_pol = False, got_user_pol = False;
265 SAM_USERINFO_CTR *ctr;
268 if (!(hnd = cm_get_sam_handle(domain->name)))
271 /* Get domain handle */
273 result = cli_samr_open_domain(hnd->cli, mem_ctx, &hnd->pol,
274 SEC_RIGHTS_MAXIMUM_ALLOWED,
275 &domain->sid, &dom_pol);
277 if (!NT_STATUS_IS_OK(result))
282 /* Get user handle */
283 result = cli_samr_open_user(hnd->cli, mem_ctx, &dom_pol,
284 SEC_RIGHTS_MAXIMUM_ALLOWED, user_rid, &user_pol);
286 if (!NT_STATUS_IS_OK(result))
290 result = cli_samr_query_userinfo(hnd->cli, mem_ctx, &user_pol,
293 cli_samr_close(hnd->cli, mem_ctx, &user_pol);
295 user_info->group_rid = ctr->info.id21->group_rid;
296 user_info->acct_name = unistr2_tdup(mem_ctx,
297 &ctr->info.id21->uni_user_name);
298 user_info->full_name = unistr2_tdup(mem_ctx,
299 &ctr->info.id21->uni_full_name);
302 /* Clean up policy handles */
304 cli_samr_close(hnd->cli, mem_ctx, &user_pol);
307 cli_samr_close(hnd->cli, mem_ctx, &dom_pol);
312 /* Lookup groups a user is a member of. I wish Unix had a call like this! */
313 static NTSTATUS lookup_usergroups(struct winbindd_domain *domain,
316 uint32 *num_groups, uint32 **user_gids)
319 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
320 POLICY_HND dom_pol, user_pol;
321 uint32 des_access = SEC_RIGHTS_MAXIMUM_ALLOWED;
322 BOOL got_dom_pol = False, got_user_pol = False;
323 DOM_GID *user_groups;
328 /* First try cached universal groups from logon */
329 *user_gids = uni_group_cache_fetch(&domain->sid, user_rid, mem_ctx, num_groups);
330 if((*num_groups > 0) && *user_gids) {
338 if (!(hnd = cm_get_sam_handle(domain->name)))
341 /* Get domain handle */
342 result = cli_samr_open_domain(hnd->cli, mem_ctx, &hnd->pol,
343 des_access, &domain->sid, &dom_pol);
345 if (!NT_STATUS_IS_OK(result))
350 /* Get user handle */
351 result = cli_samr_open_user(hnd->cli, mem_ctx, &dom_pol,
352 des_access, user_rid, &user_pol);
354 if (!NT_STATUS_IS_OK(result))
359 /* Query user rids */
360 result = cli_samr_query_usergroups(hnd->cli, mem_ctx, &user_pol,
361 num_groups, &user_groups);
363 if (!NT_STATUS_IS_OK(result) || (*num_groups) == 0)
366 (*user_gids) = talloc(mem_ctx, sizeof(uint32) * (*num_groups));
367 for (i=0;i<(*num_groups);i++) {
368 (*user_gids)[i] = user_groups[i].g_rid;
372 /* Clean up policy handles */
374 cli_samr_close(hnd->cli, mem_ctx, &user_pol);
377 cli_samr_close(hnd->cli, mem_ctx, &dom_pol);
383 /* Lookup group membership given a rid. */
384 static NTSTATUS lookup_groupmem(struct winbindd_domain *domain,
386 uint32 group_rid, uint32 *num_names,
387 uint32 **rid_mem, char ***names,
391 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
392 uint32 i, total_names = 0;
393 POLICY_HND dom_pol, group_pol;
394 uint32 des_access = SEC_RIGHTS_MAXIMUM_ALLOWED;
395 BOOL got_dom_pol = False, got_group_pol = False;
401 if (!(hnd = cm_get_sam_handle(domain->name)))
404 /* Get domain handle */
406 result = cli_samr_open_domain(hnd->cli, mem_ctx, &hnd->pol,
407 des_access, &domain->sid, &dom_pol);
409 if (!NT_STATUS_IS_OK(result))
414 /* Get group handle */
416 result = cli_samr_open_group(hnd->cli, mem_ctx, &dom_pol,
417 des_access, group_rid, &group_pol);
419 if (!NT_STATUS_IS_OK(result))
422 got_group_pol = True;
424 /* Step #1: Get a list of user rids that are the members of the
427 result = cli_samr_query_groupmem(hnd->cli, mem_ctx,
428 &group_pol, num_names, rid_mem,
431 if (!NT_STATUS_IS_OK(result))
434 /* Step #2: Convert list of rids into list of usernames. Do this
435 in bunches of ~1000 to avoid crashing NT4. It looks like there
436 is a buffer overflow or something like that lurking around
439 #define MAX_LOOKUP_RIDS 900
441 *names = talloc(mem_ctx, *num_names * sizeof(char *));
442 *name_types = talloc(mem_ctx, *num_names * sizeof(uint32));
444 for (i = 0; i < *num_names; i += MAX_LOOKUP_RIDS) {
445 int num_lookup_rids = MIN(*num_names - i, MAX_LOOKUP_RIDS);
446 uint32 tmp_num_names = 0;
447 char **tmp_names = NULL;
448 uint32 *tmp_types = NULL;
450 /* Lookup a chunk of rids */
452 result = cli_samr_lookup_rids(hnd->cli, mem_ctx,
453 &dom_pol, 1000, /* flags */
457 &tmp_names, &tmp_types);
459 if (!NT_STATUS_IS_OK(result))
462 /* Copy result into array. The talloc system will take
463 care of freeing the temporary arrays later on. */
465 memcpy(&(*names)[i], tmp_names, sizeof(char *) *
468 memcpy(&(*name_types)[i], tmp_types, sizeof(uint32) *
471 total_names += tmp_num_names;
474 *num_names = total_names;
478 cli_samr_close(hnd->cli, mem_ctx, &group_pol);
481 cli_samr_close(hnd->cli, mem_ctx, &dom_pol);
486 /* find the sequence number for a domain */
487 static NTSTATUS sequence_number(struct winbindd_domain *domain, uint32 *seq)
492 uint16 switch_value = 2;
494 uint32 seqnum = DOM_SEQUENCE_NONE;
496 BOOL got_dom_pol = False;
497 uint32 des_access = SEC_RIGHTS_MAXIMUM_ALLOWED;
499 *seq = DOM_SEQUENCE_NONE;
501 if (!(mem_ctx = talloc_init_named("sequence_number[rpc]")))
502 return NT_STATUS_NO_MEMORY;
506 if (!(hnd = cm_get_sam_handle(domain->name)))
509 /* Get domain handle */
511 result = cli_samr_open_domain(hnd->cli, mem_ctx, &hnd->pol,
512 des_access, &domain->sid, &dom_pol);
514 if (!NT_STATUS_IS_OK(result))
519 /* Query domain info */
521 result = cli_samr_query_dom_info(hnd->cli, mem_ctx, &dom_pol,
524 if (NT_STATUS_IS_OK(result)) {
525 seqnum = ctr.info.inf2.seq_num;
526 DEBUG(10,("domain_sequence_number: for domain %s is %u\n", domain->name, (unsigned)seqnum ));
528 DEBUG(10,("domain_sequence_number: failed to get sequence number (%u) for domain %s\n",
529 (unsigned)seqnum, domain->name ));
535 cli_samr_close(hnd->cli, mem_ctx, &dom_pol);
537 talloc_destroy(mem_ctx);
544 /* get a list of trusted domains */
545 static NTSTATUS trusted_domains(struct winbindd_domain *domain,
552 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
557 if (!(hnd = cm_get_lsa_handle(lp_workgroup())))
560 result = cli_lsa_enum_trust_dom(hnd->cli, mem_ctx,
561 &hnd->pol, &enum_ctx, num_domains,
567 /* find the domain sid for a domain */
568 static NTSTATUS domain_sid(struct winbindd_domain *domain, DOM_SID *sid)
570 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
575 if (!(mem_ctx = talloc_init_named("domain_sid[rpc]")))
576 return NT_STATUS_NO_MEMORY;
579 if (!(hnd = cm_get_lsa_handle(domain->name)))
582 status = cli_lsa_query_info_policy(hnd->cli, mem_ctx,
583 &hnd->pol, 0x05, level5_dom, sid);
586 talloc_destroy(mem_ctx);
590 /* the rpc backend methods are exposed via this structure */
591 struct winbindd_methods msrpc_methods = {