selftest: fix domain name of nt4_dc_smb1 environment
[Samba.git] / source3 / winbindd / winbindd_msrpc.c
bloba7bd9be4377b36b176f419a79d297d1d9eaf4eb1
1 /*
2 Unix SMB/CIFS implementation.
4 Winbind rpc backend functions
6 Copyright (C) Tim Potter 2000-2001,2003
7 Copyright (C) Andrew Tridgell 2001
8 Copyright (C) Volker Lendecke 2005
9 Copyright (C) Guenther Deschner 2008 (pidl conversion)
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "includes.h"
26 #include "winbindd.h"
27 #include "winbindd_rpc.h"
29 #include "../librpc/gen_ndr/ndr_samr_c.h"
30 #include "rpc_client/cli_pipe.h"
31 #include "rpc_client/cli_samr.h"
32 #include "rpc_client/cli_lsarpc.h"
33 #include "../libcli/security/security.h"
34 #include "libsmb/samlogon_cache.h"
36 #undef DBGC_CLASS
37 #define DBGC_CLASS DBGC_WINBIND
39 static NTSTATUS winbindd_lookup_names(TALLOC_CTX *mem_ctx,
40 struct winbindd_domain *domain,
41 uint32_t num_names,
42 const char **names,
43 const char ***domains,
44 struct dom_sid **sids,
45 enum lsa_SidType **types);
47 /* Query display info for a domain. This returns enough information plus a
48 bit extra to give an overview of domain users for the User Manager
49 application. */
50 static NTSTATUS msrpc_query_user_list(struct winbindd_domain *domain,
51 TALLOC_CTX *mem_ctx,
52 uint32_t **prids)
54 struct rpc_pipe_client *samr_pipe = NULL;
55 struct policy_handle dom_pol;
56 uint32_t *rids = NULL;
57 TALLOC_CTX *tmp_ctx;
58 NTSTATUS status;
60 DEBUG(3, ("msrpc_query_user_list\n"));
62 tmp_ctx = talloc_stackframe();
63 if (tmp_ctx == NULL) {
64 return NT_STATUS_NO_MEMORY;
67 if ( !winbindd_can_contact_domain( domain ) ) {
68 DEBUG(10,("query_user_list: No incoming trust for domain %s\n",
69 domain->name));
70 status = NT_STATUS_OK;
71 goto done;
74 status = cm_connect_sam(domain, tmp_ctx, false, &samr_pipe, &dom_pol);
75 if (!NT_STATUS_IS_OK(status)) {
76 goto done;
79 status = rpc_query_user_list(tmp_ctx,
80 samr_pipe,
81 &dom_pol,
82 &domain->sid,
83 &rids);
84 if (!NT_STATUS_IS_OK(status)) {
85 goto done;
88 if (prids) {
89 *prids = talloc_move(mem_ctx, &rids);
92 done:
93 TALLOC_FREE(rids);
94 TALLOC_FREE(tmp_ctx);
95 return status;
98 /* list all domain groups */
99 static NTSTATUS msrpc_enum_dom_groups(struct winbindd_domain *domain,
100 TALLOC_CTX *mem_ctx,
101 uint32_t *pnum_info,
102 struct wb_acct_info **pinfo)
104 struct rpc_pipe_client *samr_pipe;
105 struct policy_handle dom_pol;
106 struct wb_acct_info *info = NULL;
107 uint32_t num_info = 0;
108 TALLOC_CTX *tmp_ctx;
109 NTSTATUS status;
111 DEBUG(3,("msrpc_enum_dom_groups\n"));
113 if (pnum_info) {
114 *pnum_info = 0;
117 tmp_ctx = talloc_stackframe();
118 if (tmp_ctx == NULL) {
119 return NT_STATUS_NO_MEMORY;
122 if ( !winbindd_can_contact_domain( domain ) ) {
123 DEBUG(10,("enum_domain_groups: No incoming trust for domain %s\n",
124 domain->name));
125 status = NT_STATUS_OK;
126 goto done;
129 status = cm_connect_sam(domain, tmp_ctx, false, &samr_pipe, &dom_pol);
130 if (!NT_STATUS_IS_OK(status)) {
131 goto done;
134 status = rpc_enum_dom_groups(tmp_ctx,
135 samr_pipe,
136 &dom_pol,
137 &num_info,
138 &info);
139 if (!NT_STATUS_IS_OK(status)) {
140 goto done;
143 if (pnum_info) {
144 *pnum_info = num_info;
147 if (pinfo) {
148 *pinfo = talloc_move(mem_ctx, &info);
151 done:
152 TALLOC_FREE(tmp_ctx);
153 return status;
156 /* List all domain groups */
158 static NTSTATUS msrpc_enum_local_groups(struct winbindd_domain *domain,
159 TALLOC_CTX *mem_ctx,
160 uint32_t *pnum_info,
161 struct wb_acct_info **pinfo)
163 struct rpc_pipe_client *samr_pipe;
164 struct policy_handle dom_pol;
165 struct wb_acct_info *info = NULL;
166 uint32_t num_info = 0;
167 TALLOC_CTX *tmp_ctx;
168 NTSTATUS status;
170 DEBUG(3,("msrpc_enum_local_groups\n"));
172 if (pnum_info) {
173 *pnum_info = 0;
176 tmp_ctx = talloc_stackframe();
177 if (tmp_ctx == NULL) {
178 return NT_STATUS_NO_MEMORY;
181 if ( !winbindd_can_contact_domain( domain ) ) {
182 DEBUG(10,("enum_local_groups: No incoming trust for domain %s\n",
183 domain->name));
184 status = NT_STATUS_OK;
185 goto done;
188 status = cm_connect_sam(domain, tmp_ctx, false, &samr_pipe, &dom_pol);
189 if (!NT_STATUS_IS_OK(status)) {
190 goto done;
193 status = rpc_enum_local_groups(mem_ctx,
194 samr_pipe,
195 &dom_pol,
196 &num_info,
197 &info);
198 if (!NT_STATUS_IS_OK(status)) {
199 goto done;
202 if (pnum_info) {
203 *pnum_info = num_info;
206 if (pinfo) {
207 *pinfo = talloc_move(mem_ctx, &info);
210 done:
211 TALLOC_FREE(tmp_ctx);
212 return status;
215 /* convert a single name to a sid in a domain */
216 static NTSTATUS msrpc_name_to_sid(struct winbindd_domain *domain,
217 TALLOC_CTX *mem_ctx,
218 const char *domain_name,
219 const char *name,
220 uint32_t flags,
221 const char **pdom_name,
222 struct dom_sid *sid,
223 enum lsa_SidType *type)
225 NTSTATUS result;
226 struct dom_sid *sids = NULL;
227 enum lsa_SidType *types = NULL;
228 char *full_name = NULL;
229 const char *names[1];
230 const char **domains;
231 NTSTATUS name_map_status = NT_STATUS_UNSUCCESSFUL;
232 char *mapped_name = NULL;
234 if (name == NULL || *name=='\0') {
235 full_name = talloc_asprintf(mem_ctx, "%s", domain_name);
236 } else if (domain_name == NULL || *domain_name == '\0') {
237 full_name = talloc_asprintf(mem_ctx, "%s", name);
238 } else {
239 full_name = talloc_asprintf(mem_ctx, "%s\\%s", domain_name, name);
241 if (!full_name) {
242 DEBUG(0, ("talloc_asprintf failed!\n"));
243 return NT_STATUS_NO_MEMORY;
246 DEBUG(3, ("msrpc_name_to_sid: name=%s\n", full_name));
248 name_map_status = normalize_name_unmap(mem_ctx, full_name,
249 &mapped_name);
251 /* Reset the full_name pointer if we mapped anything */
253 if (NT_STATUS_IS_OK(name_map_status) ||
254 NT_STATUS_EQUAL(name_map_status, NT_STATUS_FILE_RENAMED))
256 full_name = mapped_name;
259 DEBUG(3,("name_to_sid [rpc] %s for domain %s\n",
260 full_name?full_name:"", domain_name ));
262 names[0] = full_name;
264 result = winbindd_lookup_names(mem_ctx, domain, 1,
265 names, &domains,
266 &sids, &types);
267 if (!NT_STATUS_IS_OK(result))
268 return result;
270 /* Return rid and type if lookup successful */
272 if (pdom_name != NULL) {
273 const char *dom_name;
275 dom_name = talloc_strdup(mem_ctx, domains[0]);
276 if (dom_name == NULL) {
277 return NT_STATUS_NO_MEMORY;
280 *pdom_name = dom_name;
283 sid_copy(sid, &sids[0]);
284 *type = types[0];
286 return NT_STATUS_OK;
290 convert a domain SID to a user or group name
292 static NTSTATUS msrpc_sid_to_name(struct winbindd_domain *domain,
293 TALLOC_CTX *mem_ctx,
294 const struct dom_sid *sid,
295 char **domain_name,
296 char **name,
297 enum lsa_SidType *type)
299 char **domains;
300 char **names;
301 enum lsa_SidType *types = NULL;
302 NTSTATUS result;
303 NTSTATUS name_map_status = NT_STATUS_UNSUCCESSFUL;
304 char *mapped_name = NULL;
305 struct dom_sid_buf buf;
307 DEBUG(3, ("msrpc_sid_to_name: %s for domain %s\n",
308 dom_sid_str_buf(sid, &buf),
309 domain->name));
311 result = winbindd_lookup_sids(mem_ctx,
312 domain,
314 sid,
315 &domains,
316 &names,
317 &types);
318 if (!NT_STATUS_IS_OK(result)) {
319 DEBUG(2,("msrpc_sid_to_name: failed to lookup sids: %s\n",
320 nt_errstr(result)));
321 return result;
325 *type = (enum lsa_SidType)types[0];
326 *domain_name = domains[0];
327 *name = names[0];
329 DEBUG(5,("Mapped sid to [%s]\\[%s]\n", domains[0], *name));
331 name_map_status = normalize_name_map(mem_ctx, domain->name, *name,
332 &mapped_name);
333 if (NT_STATUS_IS_OK(name_map_status) ||
334 NT_STATUS_EQUAL(name_map_status, NT_STATUS_FILE_RENAMED))
336 *name = mapped_name;
337 DEBUG(5,("returning mapped name -- %s\n", *name));
340 return NT_STATUS_OK;
343 static NTSTATUS msrpc_rids_to_names(struct winbindd_domain *domain,
344 TALLOC_CTX *mem_ctx,
345 const struct dom_sid *sid,
346 uint32_t *rids,
347 size_t num_rids,
348 char **domain_name,
349 char ***names,
350 enum lsa_SidType **types)
352 char **domains;
353 NTSTATUS result;
354 struct dom_sid *sids;
355 size_t i;
356 char **ret_names;
358 DEBUG(3, ("msrpc_rids_to_names: domain %s\n", domain->name ));
360 if (num_rids) {
361 sids = talloc_array(mem_ctx, struct dom_sid, num_rids);
362 if (sids == NULL) {
363 return NT_STATUS_NO_MEMORY;
365 } else {
366 sids = NULL;
369 for (i=0; i<num_rids; i++) {
370 if (!sid_compose(&sids[i], sid, rids[i])) {
371 return NT_STATUS_INTERNAL_ERROR;
375 result = winbindd_lookup_sids(mem_ctx,
376 domain,
377 num_rids,
378 sids,
379 &domains,
380 names,
381 types);
383 if (!NT_STATUS_IS_OK(result) &&
384 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED)) {
385 return result;
388 ret_names = *names;
389 for (i=0; i<num_rids; i++) {
390 NTSTATUS name_map_status = NT_STATUS_UNSUCCESSFUL;
391 char *mapped_name = NULL;
393 if ((*types)[i] != SID_NAME_UNKNOWN) {
394 name_map_status = normalize_name_map(mem_ctx,
395 domain->name,
396 ret_names[i],
397 &mapped_name);
398 if (NT_STATUS_IS_OK(name_map_status) ||
399 NT_STATUS_EQUAL(name_map_status, NT_STATUS_FILE_RENAMED))
401 ret_names[i] = mapped_name;
404 *domain_name = domains[i];
408 return result;
411 /* Lookup groups a user is a member of. I wish Unix had a call like this! */
412 static NTSTATUS msrpc_lookup_usergroups(struct winbindd_domain *domain,
413 TALLOC_CTX *mem_ctx,
414 const struct dom_sid *user_sid,
415 uint32_t *pnum_groups,
416 struct dom_sid **puser_grpsids)
418 struct rpc_pipe_client *samr_pipe;
419 struct policy_handle dom_pol;
420 struct dom_sid *user_grpsids = NULL;
421 struct dom_sid_buf buf;
422 uint32_t num_groups = 0;
423 TALLOC_CTX *tmp_ctx;
424 NTSTATUS status;
426 DEBUG(3,("msrpc_lookup_usergroups sid=%s\n",
427 dom_sid_str_buf(user_sid, &buf)));
429 *pnum_groups = 0;
431 tmp_ctx = talloc_stackframe();
432 if (tmp_ctx == NULL) {
433 return NT_STATUS_NO_MEMORY;
436 /* Check if we have a cached user_info_3 */
437 status = lookup_usergroups_cached(tmp_ctx,
438 user_sid,
439 &num_groups,
440 &user_grpsids);
441 if (NT_STATUS_IS_OK(status)) {
442 goto cached;
445 if ( !winbindd_can_contact_domain( domain ) ) {
446 DEBUG(10,("lookup_usergroups: No incoming trust for domain %s\n",
447 domain->name));
449 /* Tell the cache manager not to remember this one */
450 status = NT_STATUS_SYNCHRONIZATION_REQUIRED;
451 goto done;
454 /* no cache; hit the wire */
455 status = cm_connect_sam(domain, tmp_ctx, false, &samr_pipe, &dom_pol);
456 if (!NT_STATUS_IS_OK(status)) {
457 goto done;
460 status = rpc_lookup_usergroups(tmp_ctx,
461 samr_pipe,
462 &dom_pol,
463 &domain->sid,
464 user_sid,
465 &num_groups,
466 &user_grpsids);
467 if (!NT_STATUS_IS_OK(status)) {
468 goto done;
471 cached:
472 *pnum_groups = num_groups;
474 if (puser_grpsids) {
475 *puser_grpsids = talloc_move(mem_ctx, &user_grpsids);
478 done:
479 TALLOC_FREE(tmp_ctx);
480 return status;
481 return NT_STATUS_OK;
484 #define MAX_SAM_ENTRIES_W2K 0x400 /* 1024 */
486 static NTSTATUS msrpc_lookup_useraliases(struct winbindd_domain *domain,
487 TALLOC_CTX *mem_ctx,
488 uint32_t num_sids, const struct dom_sid *sids,
489 uint32_t *pnum_aliases,
490 uint32_t **palias_rids)
492 struct rpc_pipe_client *samr_pipe;
493 struct policy_handle dom_pol;
494 uint32_t num_aliases = 0;
495 uint32_t *alias_rids = NULL;
496 TALLOC_CTX *tmp_ctx;
497 NTSTATUS status;
499 DEBUG(3,("msrpc_lookup_useraliases\n"));
501 if (pnum_aliases) {
502 *pnum_aliases = 0;
505 tmp_ctx = talloc_stackframe();
506 if (tmp_ctx == NULL) {
507 return NT_STATUS_NO_MEMORY;
510 if (!winbindd_can_contact_domain(domain)) {
511 DEBUG(10,("msrpc_lookup_useraliases: No incoming trust for domain %s\n",
512 domain->name));
513 /* Tell the cache manager not to remember this one */
514 status = NT_STATUS_SYNCHRONIZATION_REQUIRED;
515 goto done;
518 status = cm_connect_sam(domain, tmp_ctx, false, &samr_pipe, &dom_pol);
519 if (!NT_STATUS_IS_OK(status)) {
520 goto done;
523 status = rpc_lookup_useraliases(tmp_ctx,
524 samr_pipe,
525 &dom_pol,
526 num_sids,
527 sids,
528 &num_aliases,
529 &alias_rids);
530 if (!NT_STATUS_IS_OK(status)) {
531 goto done;
534 if (pnum_aliases) {
535 *pnum_aliases = num_aliases;
538 if (palias_rids) {
539 *palias_rids = talloc_move(mem_ctx, &alias_rids);
542 done:
543 TALLOC_FREE(tmp_ctx);
544 return status;
547 /* lookup alias membership */
548 static NTSTATUS msrpc_lookup_aliasmem(struct winbindd_domain *domain,
549 TALLOC_CTX *mem_ctx,
550 const struct dom_sid *alias_sid,
551 enum lsa_SidType type,
552 uint32_t *pnum_sids,
553 struct dom_sid **sid_mem)
555 struct rpc_pipe_client *samr_pipe = NULL;
556 struct policy_handle dom_pol;
557 struct dom_sid *alias_members = NULL;
558 struct dom_sid_buf buf;
559 uint32_t num_groups = 0;
560 TALLOC_CTX *tmp_ctx = talloc_stackframe();
561 NTSTATUS status;
563 D_INFO("Lookup alias members in domain=%s for sid=%s.\n",
564 domain->name,
565 dom_sid_str_buf(alias_sid, &buf));
567 *pnum_sids = 0;
569 if (!winbindd_can_contact_domain(domain)) {
570 D_DEBUG("No incoming trust for domain %s\n", domain->name);
571 status = NT_STATUS_OK;
572 goto done;
575 status = cm_connect_sam(domain, tmp_ctx, false, &samr_pipe, &dom_pol);
576 if (!NT_STATUS_IS_OK(status)) {
577 goto done;
580 status = rpc_lookup_aliasmem(tmp_ctx,
581 samr_pipe,
582 &dom_pol,
583 &domain->sid,
584 alias_sid,
585 type,
586 &num_groups,
587 &alias_members);
588 if (!NT_STATUS_IS_OK(status)) {
589 goto done;
592 *pnum_sids = num_groups;
593 if (sid_mem) {
594 *sid_mem = talloc_move(mem_ctx, &alias_members);
597 done:
598 talloc_free(tmp_ctx);
599 return status;
602 /* Lookup group membership given a rid. */
603 static NTSTATUS msrpc_lookup_groupmem(struct winbindd_domain *domain,
604 TALLOC_CTX *mem_ctx,
605 const struct dom_sid *group_sid,
606 enum lsa_SidType type,
607 uint32_t *num_names,
608 struct dom_sid **sid_mem,
609 char ***names,
610 uint32_t **name_types)
612 NTSTATUS status, result;
613 uint32_t i, total_names = 0;
614 struct policy_handle dom_pol, group_pol;
615 uint32_t des_access = SEC_FLAG_MAXIMUM_ALLOWED;
616 uint32_t *rid_mem = NULL;
617 uint32_t group_rid;
618 unsigned int j, r;
619 struct rpc_pipe_client *cli;
620 unsigned int orig_timeout;
621 struct samr_RidAttrArray *rids = NULL;
622 struct dcerpc_binding_handle *b;
623 struct dom_sid_buf buf;
625 DEBUG(3,("msrpc_lookup_groupmem: %s sid=%s\n", domain->name,
626 dom_sid_str_buf(group_sid, &buf)));
628 if ( !winbindd_can_contact_domain( domain ) ) {
629 DEBUG(10,("lookup_groupmem: No incoming trust for domain %s\n",
630 domain->name));
631 return NT_STATUS_OK;
634 if (!sid_peek_check_rid(&domain->sid, group_sid, &group_rid))
635 return NT_STATUS_UNSUCCESSFUL;
637 *num_names = 0;
639 result = cm_connect_sam(domain, mem_ctx, false, &cli, &dom_pol);
640 if (!NT_STATUS_IS_OK(result))
641 return result;
643 b = cli->binding_handle;
645 status = dcerpc_samr_OpenGroup(b, mem_ctx,
646 &dom_pol,
647 des_access,
648 group_rid,
649 &group_pol,
650 &result);
651 if (any_nt_status_not_ok(status, result, &status)) {
652 return status;
655 /* Step #1: Get a list of user rids that are the members of the
656 group. */
658 /* This call can take a long time - allow the server to time out.
659 35 seconds should do it. */
661 orig_timeout = rpccli_set_timeout(cli, 35000);
663 status = dcerpc_samr_QueryGroupMember(b, mem_ctx,
664 &group_pol,
665 &rids,
666 &result);
668 /* And restore our original timeout. */
669 rpccli_set_timeout(cli, orig_timeout);
672 NTSTATUS _result;
673 dcerpc_samr_Close(b, mem_ctx, &group_pol, &_result);
676 if (any_nt_status_not_ok(status, result, &status)) {
677 return status;
680 if (!rids || !rids->count) {
681 names = NULL;
682 name_types = NULL;
683 sid_mem = NULL;
684 return NT_STATUS_OK;
687 *num_names = rids->count;
688 rid_mem = rids->rids;
690 /* Step #2: Convert list of rids into list of usernames. Do this
691 in bunches of ~1000 to avoid crashing NT4. It looks like there
692 is a buffer overflow or something like that lurking around
693 somewhere. */
695 #define MAX_LOOKUP_RIDS 900
697 *names = talloc_zero_array(mem_ctx, char *, *num_names);
698 *name_types = talloc_zero_array(mem_ctx, uint32_t, *num_names);
699 *sid_mem = talloc_zero_array(mem_ctx, struct dom_sid, *num_names);
701 for (j=0;j<(*num_names);j++)
702 sid_compose(&(*sid_mem)[j], &domain->sid, rid_mem[j]);
704 if (*num_names>0 && (!*names || !*name_types))
705 return NT_STATUS_NO_MEMORY;
707 for (i = 0; i < *num_names; i += MAX_LOOKUP_RIDS) {
708 int num_lookup_rids = MIN(*num_names - i, MAX_LOOKUP_RIDS);
709 struct lsa_Strings tmp_names;
710 struct samr_Ids tmp_types;
712 /* Lookup a chunk of rids */
714 status = dcerpc_samr_LookupRids(b, mem_ctx,
715 &dom_pol,
716 num_lookup_rids,
717 &rid_mem[i],
718 &tmp_names,
719 &tmp_types,
720 &result);
721 if (!NT_STATUS_IS_OK(status)) {
722 return status;
725 /* see if we have a real error (and yes the
726 STATUS_SOME_UNMAPPED is the one returned from 2k) */
728 if (!NT_STATUS_IS_OK(result) &&
729 !NT_STATUS_EQUAL(result, STATUS_SOME_UNMAPPED))
730 return result;
732 /* Copy result into array. The talloc system will take
733 care of freeing the temporary arrays later on. */
735 if (tmp_names.count != num_lookup_rids) {
736 return NT_STATUS_INVALID_NETWORK_RESPONSE;
738 if (tmp_types.count != num_lookup_rids) {
739 return NT_STATUS_INVALID_NETWORK_RESPONSE;
742 for (r=0; r<tmp_names.count; r++) {
743 if (tmp_types.ids[r] == SID_NAME_UNKNOWN) {
744 continue;
746 if (total_names >= *num_names) {
747 break;
749 (*names)[total_names] = fill_domain_username_talloc(
750 mem_ctx, domain->name,
751 tmp_names.names[r].string, true);
752 (*name_types)[total_names] = tmp_types.ids[r];
753 total_names += 1;
757 *num_names = total_names;
759 return NT_STATUS_OK;
762 /* get a list of trusted domains */
763 static NTSTATUS msrpc_trusted_domains(struct winbindd_domain *domain,
764 TALLOC_CTX *mem_ctx,
765 struct netr_DomainTrustList *ptrust_list)
767 struct rpc_pipe_client *lsa_pipe;
768 struct policy_handle lsa_policy;
769 struct netr_DomainTrust *trusts = NULL;
770 uint32_t num_trusts = 0;
771 TALLOC_CTX *tmp_ctx;
772 NTSTATUS status;
774 DEBUG(3,("msrpc_trusted_domains\n"));
776 if (ptrust_list) {
777 ZERO_STRUCTP(ptrust_list);
780 tmp_ctx = talloc_stackframe();
781 if (tmp_ctx == NULL) {
782 return NT_STATUS_NO_MEMORY;
785 status = cm_connect_lsa(domain, tmp_ctx, &lsa_pipe, &lsa_policy);
786 if (!NT_STATUS_IS_OK(status)) {
787 goto done;
790 status = rpc_trusted_domains(tmp_ctx,
791 lsa_pipe,
792 &lsa_policy,
793 &num_trusts,
794 &trusts);
795 if (!NT_STATUS_IS_OK(status)) {
796 goto done;
799 if (ptrust_list) {
800 ptrust_list->count = num_trusts;
801 ptrust_list->array = talloc_move(mem_ctx, &trusts);
804 done:
805 TALLOC_FREE(tmp_ctx);
806 return status;
809 /* find the lockout policy for a domain */
810 static NTSTATUS msrpc_lockout_policy(struct winbindd_domain *domain,
811 TALLOC_CTX *mem_ctx,
812 struct samr_DomInfo12 *lockout_policy)
814 NTSTATUS status, result;
815 struct rpc_pipe_client *cli;
816 struct policy_handle dom_pol;
817 union samr_DomainInfo *info = NULL;
818 struct dcerpc_binding_handle *b;
820 DEBUG(3, ("msrpc_lockout_policy: fetch lockout policy for %s\n", domain->name));
822 if ( !winbindd_can_contact_domain( domain ) ) {
823 DEBUG(10,("msrpc_lockout_policy: No incoming trust for domain %s\n",
824 domain->name));
825 return NT_STATUS_NOT_SUPPORTED;
828 status = cm_connect_sam(domain, mem_ctx, false, &cli, &dom_pol);
829 if (!NT_STATUS_IS_OK(status)) {
830 goto done;
833 b = cli->binding_handle;
835 status = dcerpc_samr_QueryDomainInfo(b, mem_ctx,
836 &dom_pol,
837 DomainLockoutInformation,
838 &info,
839 &result);
840 if (any_nt_status_not_ok(status, result, &status)) {
841 return status;
844 *lockout_policy = info->info12;
846 DEBUG(10,("msrpc_lockout_policy: lockout_threshold %d\n",
847 info->info12.lockout_threshold));
849 done:
851 return status;
854 /* find the password policy for a domain */
855 static NTSTATUS msrpc_password_policy(struct winbindd_domain *domain,
856 TALLOC_CTX *mem_ctx,
857 struct samr_DomInfo1 *password_policy)
859 NTSTATUS status, result;
860 struct rpc_pipe_client *cli;
861 struct policy_handle dom_pol;
862 union samr_DomainInfo *info = NULL;
863 struct dcerpc_binding_handle *b;
865 DEBUG(3, ("msrpc_password_policy: fetch password policy for %s\n",
866 domain->name));
868 if ( !winbindd_can_contact_domain( domain ) ) {
869 DEBUG(10,("msrpc_password_policy: No incoming trust for domain %s\n",
870 domain->name));
871 return NT_STATUS_NOT_SUPPORTED;
874 status = cm_connect_sam(domain, mem_ctx, false, &cli, &dom_pol);
875 if (!NT_STATUS_IS_OK(status)) {
876 goto done;
879 b = cli->binding_handle;
881 status = dcerpc_samr_QueryDomainInfo(b, mem_ctx,
882 &dom_pol,
883 DomainPasswordInformation,
884 &info,
885 &result);
886 if (!NT_STATUS_IS_OK(status)) {
887 goto done;
889 if (!NT_STATUS_IS_OK(result)) {
890 goto done;
893 *password_policy = info->info1;
895 DEBUG(10,("msrpc_password_policy: min_length_password %d\n",
896 info->info1.min_password_length));
898 done:
900 return status;
903 static enum lsa_LookupNamesLevel winbindd_lookup_level(
904 struct winbindd_domain *domain)
906 enum lsa_LookupNamesLevel level = LSA_LOOKUP_NAMES_DOMAINS_ONLY;
908 if (domain->internal) {
909 level = LSA_LOOKUP_NAMES_ALL;
910 } else if (domain->secure_channel_type == SEC_CHAN_DNS_DOMAIN) {
911 if (domain->domain_flags & NETR_TRUST_FLAG_IN_FOREST) {
913 * TODO:
915 * Depending on what we want to resolve. We need to use:
916 * 1. LsapLookupXForestReferral(5)/LSA_LOOKUP_NAMES_FOREST_TRUSTS_ONLY
917 * if we want to pass the request into the direction of the forest
918 * root domain. The forest root domain uses
919 * LsapLookupXForestResolve(6)/LSA_LOOKUP_NAMES_UPLEVEL_TRUSTS_ONLY2
920 * when passing the request to trusted forests.
921 * 2. LsapLookupGC(4)/LSA_LOOKUP_NAMES_UPLEVEL_TRUSTS_ONLY
922 * if we're not a GC and want to resolve a name within our own forest.
924 * As we don't support more than one domain in our own forest
925 * and always try to be a GC for now, we just set
926 * LSA_LOOKUP_NAMES_FOREST_TRUSTS_ONLY.
928 level = LSA_LOOKUP_NAMES_FOREST_TRUSTS_ONLY;
929 } else if (domain->domain_trust_attribs & LSA_TRUST_ATTRIBUTE_FOREST_TRANSITIVE) {
931 * This is LsapLookupXForestResolve(6)/LSA_LOOKUP_NAMES_UPLEVEL_TRUSTS_ONLY2
933 level = LSA_LOOKUP_NAMES_UPLEVEL_TRUSTS_ONLY2;
934 } else {
936 * This is LsapLookupTDL(3)/LSA_LOOKUP_NAMES_PRIMARY_DOMAIN_ONLY
938 level = LSA_LOOKUP_NAMES_PRIMARY_DOMAIN_ONLY;
940 } else if (domain->secure_channel_type == SEC_CHAN_DOMAIN) {
942 * This is LsapLookupTDL(3)/LSA_LOOKUP_NAMES_PRIMARY_DOMAIN_ONLY
944 level = LSA_LOOKUP_NAMES_PRIMARY_DOMAIN_ONLY;
945 } else if (domain->rodc) {
946 level = LSA_LOOKUP_NAMES_RODC_REFERRAL_TO_FULL_DC;
947 } else {
949 * This is LsapLookupPDC(2)/LSA_LOOKUP_NAMES_DOMAINS_ONLY
951 level = LSA_LOOKUP_NAMES_DOMAINS_ONLY;
954 return level;
957 NTSTATUS winbindd_lookup_sids(TALLOC_CTX *mem_ctx,
958 struct winbindd_domain *domain,
959 uint32_t num_sids,
960 const struct dom_sid *sids,
961 char ***domains,
962 char ***names,
963 enum lsa_SidType **types)
965 NTSTATUS status;
966 NTSTATUS result;
967 struct rpc_pipe_client *cli = NULL;
968 struct dcerpc_binding_handle *b = NULL;
969 struct policy_handle lsa_policy;
970 unsigned int orig_timeout;
971 bool use_lookupsids3 = false;
972 bool retried = false;
973 enum lsa_LookupNamesLevel level = LSA_LOOKUP_NAMES_ALL;
975 connect:
976 status = cm_connect_lsat(domain, mem_ctx, &cli, &lsa_policy);
977 if (!NT_STATUS_IS_OK(status)) {
978 return status;
981 b = cli->binding_handle;
983 if (cli->transport->transport == NCACN_IP_TCP) {
984 use_lookupsids3 = true;
987 level = winbindd_lookup_level(domain);
990 * This call can take a long time
991 * allow the server to time out.
992 * 35 seconds should do it.
994 orig_timeout = dcerpc_binding_handle_set_timeout(b, 35000);
996 status = dcerpc_lsa_lookup_sids_generic(b,
997 mem_ctx,
998 &lsa_policy,
999 num_sids,
1000 sids,
1001 level,
1002 domains,
1003 names,
1004 types,
1005 use_lookupsids3,
1006 &result);
1008 /* And restore our original timeout. */
1009 dcerpc_binding_handle_set_timeout(b, orig_timeout);
1011 if (reset_cm_connection_on_error(domain, b, status)) {
1013 * This can happen if the schannel key is not
1014 * valid anymore, we need to invalidate the
1015 * all connections to the dc and reestablish
1016 * a netlogon connection first.
1018 domain->can_do_ncacn_ip_tcp = domain->active_directory;
1019 if (!retried) {
1020 retried = true;
1021 goto connect;
1023 status = NT_STATUS_ACCESS_DENIED;
1026 if (any_nt_status_not_ok(status, result, &status)) {
1027 return status;
1030 return NT_STATUS_OK;
1033 static NTSTATUS winbindd_lookup_names(TALLOC_CTX *mem_ctx,
1034 struct winbindd_domain *domain,
1035 uint32_t num_names,
1036 const char **names,
1037 const char ***domains,
1038 struct dom_sid **sids,
1039 enum lsa_SidType **types)
1041 NTSTATUS status;
1042 NTSTATUS result;
1043 struct rpc_pipe_client *cli = NULL;
1044 struct dcerpc_binding_handle *b = NULL;
1045 struct policy_handle lsa_policy;
1046 unsigned int orig_timeout = 0;
1047 bool use_lookupnames4 = false;
1048 bool retried = false;
1049 enum lsa_LookupNamesLevel level = LSA_LOOKUP_NAMES_ALL;
1051 connect:
1052 status = cm_connect_lsat(domain, mem_ctx, &cli, &lsa_policy);
1053 if (!NT_STATUS_IS_OK(status)) {
1054 return status;
1057 b = cli->binding_handle;
1059 if (cli->transport->transport == NCACN_IP_TCP) {
1060 use_lookupnames4 = true;
1063 level = winbindd_lookup_level(domain);
1066 * This call can take a long time
1067 * allow the server to time out.
1068 * 35 seconds should do it.
1070 orig_timeout = dcerpc_binding_handle_set_timeout(b, 35000);
1072 status = dcerpc_lsa_lookup_names_generic(b,
1073 mem_ctx,
1074 &lsa_policy,
1075 num_names,
1076 (const char **) names,
1077 domains,
1078 level,
1079 sids,
1080 types,
1081 use_lookupnames4,
1082 &result);
1084 /* And restore our original timeout. */
1085 dcerpc_binding_handle_set_timeout(b, orig_timeout);
1087 if (reset_cm_connection_on_error(domain, b, status)) {
1089 * This can happen if the schannel key is not
1090 * valid anymore, we need to invalidate the
1091 * all connections to the dc and reestablish
1092 * a netlogon connection first.
1094 if (!retried) {
1095 retried = true;
1096 goto connect;
1098 status = NT_STATUS_ACCESS_DENIED;
1101 if (any_nt_status_not_ok(status, result, &status)) {
1102 return status;
1105 return NT_STATUS_OK;
1108 /* the rpc backend methods are exposed via this structure */
1109 struct winbindd_methods msrpc_methods = {
1110 False,
1111 msrpc_query_user_list,
1112 msrpc_enum_dom_groups,
1113 msrpc_enum_local_groups,
1114 msrpc_name_to_sid,
1115 msrpc_sid_to_name,
1116 msrpc_rids_to_names,
1117 msrpc_lookup_usergroups,
1118 msrpc_lookup_useraliases,
1119 msrpc_lookup_groupmem,
1120 msrpc_lookup_aliasmem,
1121 msrpc_lockout_policy,
1122 msrpc_password_policy,
1123 msrpc_trusted_domains,