WHATSNEW: Add release notes for Samba 4.9.17.
[Samba.git] / source3 / winbindd / winbindd_samr.c
blob55af168dabf429b2b132e6698660c5f94dc60e02
1 /*
2 * Unix SMB/CIFS implementation.
4 * Winbind rpc backend functions
6 * Copyright (c) 2000-2003 Tim Potter
7 * Copyright (c) 2001 Andrew Tridgell
8 * Copyright (c) 2005 Volker Lendecke
9 * Copyright (c) 2008 Guenther Deschner (pidl conversion)
10 * Copyright (c) 2010 Andreas Schneider <asn@samba.org>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 3 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include "includes.h"
27 #include "winbindd.h"
28 #include "winbindd_rpc.h"
29 #include "lib/util_unixsids.h"
30 #include "rpc_client/rpc_client.h"
31 #include "rpc_client/cli_pipe.h"
32 #include "../librpc/gen_ndr/ndr_samr_c.h"
33 #include "rpc_client/cli_samr.h"
34 #include "../librpc/gen_ndr/ndr_lsa_c.h"
35 #include "rpc_client/cli_lsarpc.h"
36 #include "rpc_server/rpc_ncacn_np.h"
37 #include "../libcli/security/security.h"
38 #include "passdb/machine_sid.h"
39 #include "auth.h"
41 #undef DBGC_CLASS
42 #define DBGC_CLASS DBGC_WINBIND
45 * The other end of this won't go away easily, so we can trust it
47 * It is either a long-lived process with the same lifetime as
48 * winbindd or a part of this process
50 struct winbind_internal_pipes {
51 struct rpc_pipe_client *samr_pipe;
52 struct policy_handle samr_domain_hnd;
53 struct rpc_pipe_client *lsa_pipe;
54 struct policy_handle lsa_hnd;
58 NTSTATUS open_internal_samr_conn(TALLOC_CTX *mem_ctx,
59 struct winbindd_domain *domain,
60 struct rpc_pipe_client **samr_pipe,
61 struct policy_handle *samr_domain_hnd)
63 NTSTATUS status, result;
64 struct policy_handle samr_connect_hnd;
65 struct dcerpc_binding_handle *b;
67 status = wb_open_internal_pipe(mem_ctx, &ndr_table_samr, samr_pipe);
68 if (!NT_STATUS_IS_OK(status)) {
69 return status;
72 b = (*samr_pipe)->binding_handle;
74 status = dcerpc_samr_Connect2(b, mem_ctx,
75 (*samr_pipe)->desthost,
76 SEC_FLAG_MAXIMUM_ALLOWED,
77 &samr_connect_hnd,
78 &result);
79 if (!NT_STATUS_IS_OK(status)) {
80 return status;
82 if (!NT_STATUS_IS_OK(result)) {
83 return result;
86 status = dcerpc_samr_OpenDomain(b, mem_ctx,
87 &samr_connect_hnd,
88 SEC_FLAG_MAXIMUM_ALLOWED,
89 &domain->sid,
90 samr_domain_hnd,
91 &result);
92 if (!NT_STATUS_IS_OK(status)) {
93 return status;
96 return result;
99 NTSTATUS open_internal_lsa_conn(TALLOC_CTX *mem_ctx,
100 struct rpc_pipe_client **lsa_pipe,
101 struct policy_handle *lsa_hnd)
103 NTSTATUS status;
105 status = wb_open_internal_pipe(mem_ctx, &ndr_table_lsarpc, lsa_pipe);
106 if (!NT_STATUS_IS_OK(status)) {
107 return status;
110 status = rpccli_lsa_open_policy((*lsa_pipe),
111 mem_ctx,
112 true,
113 SEC_FLAG_MAXIMUM_ALLOWED,
114 lsa_hnd);
116 return status;
120 static NTSTATUS open_cached_internal_pipe_conn(
121 struct winbindd_domain *domain,
122 struct rpc_pipe_client **samr_pipe,
123 struct policy_handle *samr_domain_hnd,
124 struct rpc_pipe_client **lsa_pipe,
125 struct policy_handle *lsa_hnd)
127 struct winbind_internal_pipes *internal_pipes = NULL;
129 if (domain->private_data == NULL) {
130 TALLOC_CTX *frame = talloc_stackframe();
131 NTSTATUS status;
133 internal_pipes = talloc_zero(frame,
134 struct winbind_internal_pipes);
136 status = open_internal_samr_conn(
137 internal_pipes,
138 domain,
139 &internal_pipes->samr_pipe,
140 &internal_pipes->samr_domain_hnd);
141 if (!NT_STATUS_IS_OK(status)) {
142 TALLOC_FREE(frame);
143 return status;
146 status = open_internal_lsa_conn(internal_pipes,
147 &internal_pipes->lsa_pipe,
148 &internal_pipes->lsa_hnd);
150 if (!NT_STATUS_IS_OK(status)) {
151 TALLOC_FREE(frame);
152 return status;
155 domain->private_data = talloc_move(domain, &internal_pipes);
157 TALLOC_FREE(frame);
161 internal_pipes = talloc_get_type_abort(
162 domain->private_data, struct winbind_internal_pipes);
164 if (samr_domain_hnd) {
165 *samr_domain_hnd = internal_pipes->samr_domain_hnd;
168 if (samr_pipe) {
169 *samr_pipe = internal_pipes->samr_pipe;
172 if (lsa_hnd) {
173 *lsa_hnd = internal_pipes->lsa_hnd;
176 if (lsa_pipe) {
177 *lsa_pipe = internal_pipes->lsa_pipe;
180 return NT_STATUS_OK;
183 static bool reset_connection_on_error(struct winbindd_domain *domain,
184 struct rpc_pipe_client *p,
185 NTSTATUS status)
187 struct winbind_internal_pipes *internal_pipes = NULL;
189 internal_pipes = talloc_get_type_abort(
190 domain->private_data, struct winbind_internal_pipes);
192 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT) ||
193 NT_STATUS_EQUAL(status, NT_STATUS_IO_DEVICE_ERROR))
195 TALLOC_FREE(internal_pipes);
196 domain->private_data = NULL;
197 return true;
200 if (!rpccli_is_connected(p)) {
201 TALLOC_FREE(internal_pipes);
202 domain->private_data = NULL;
203 return true;
206 return false;
209 /*********************************************************************
210 SAM specific functions.
211 *********************************************************************/
213 /* List all domain groups */
214 static NTSTATUS sam_enum_dom_groups(struct winbindd_domain *domain,
215 TALLOC_CTX *mem_ctx,
216 uint32_t *pnum_info,
217 struct wb_acct_info **pinfo)
219 struct rpc_pipe_client *samr_pipe;
220 struct policy_handle dom_pol = { 0 };
221 struct wb_acct_info *info = NULL;
222 uint32_t num_info = 0;
223 TALLOC_CTX *tmp_ctx;
224 NTSTATUS status;
225 bool retry = false;
227 DEBUG(3,("sam_enum_dom_groups\n"));
229 if (pnum_info) {
230 *pnum_info = 0;
233 tmp_ctx = talloc_stackframe();
234 if (tmp_ctx == NULL) {
235 return NT_STATUS_NO_MEMORY;
238 again:
239 status = open_cached_internal_pipe_conn(domain,
240 &samr_pipe,
241 &dom_pol,
242 NULL,
243 NULL);
244 if (!NT_STATUS_IS_OK(status)) {
245 TALLOC_FREE(tmp_ctx);
246 return status;
249 status = rpc_enum_dom_groups(tmp_ctx,
250 samr_pipe,
251 &dom_pol,
252 &num_info,
253 &info);
255 if (!retry && reset_connection_on_error(domain, samr_pipe, status)) {
256 retry = true;
257 goto again;
260 if (!NT_STATUS_IS_OK(status)) {
261 TALLOC_FREE(tmp_ctx);
262 return status;
265 if (pnum_info) {
266 *pnum_info = num_info;
269 if (pinfo) {
270 *pinfo = talloc_move(mem_ctx, &info);
273 TALLOC_FREE(tmp_ctx);
274 return status;
277 /* Query display info for a domain */
278 static NTSTATUS sam_query_user_list(struct winbindd_domain *domain,
279 TALLOC_CTX *mem_ctx,
280 uint32_t **prids)
282 struct rpc_pipe_client *samr_pipe = NULL;
283 struct policy_handle dom_pol = { 0 };
284 uint32_t *rids = NULL;
285 TALLOC_CTX *tmp_ctx;
286 NTSTATUS status;
287 bool retry = false;
289 DEBUG(3,("samr_query_user_list\n"));
291 tmp_ctx = talloc_stackframe();
292 if (tmp_ctx == NULL) {
293 return NT_STATUS_NO_MEMORY;
296 again:
297 status = open_cached_internal_pipe_conn(domain,
298 &samr_pipe,
299 &dom_pol,
300 NULL,
301 NULL);
302 if (!NT_STATUS_IS_OK(status)) {
303 goto done;
306 status = rpc_query_user_list(tmp_ctx,
307 samr_pipe,
308 &dom_pol,
309 &domain->sid,
310 &rids);
311 if (!retry && reset_connection_on_error(domain, samr_pipe, status)) {
312 retry = true;
313 goto again;
316 if (!NT_STATUS_IS_OK(status)) {
317 goto done;
320 if (prids != NULL) {
321 *prids = talloc_move(mem_ctx, &rids);
324 done:
325 TALLOC_FREE(rids);
326 TALLOC_FREE(tmp_ctx);
327 return status;
330 /* get a list of trusted domains - builtin domain */
331 static NTSTATUS sam_trusted_domains(struct winbindd_domain *domain,
332 TALLOC_CTX *mem_ctx,
333 struct netr_DomainTrustList *ptrust_list)
335 struct rpc_pipe_client *lsa_pipe;
336 struct policy_handle lsa_policy = { 0 };
337 struct netr_DomainTrust *trusts = NULL;
338 uint32_t num_trusts = 0;
339 TALLOC_CTX *tmp_ctx;
340 NTSTATUS status;
341 bool retry = false;
343 DEBUG(3,("samr: trusted domains\n"));
345 if (ptrust_list) {
346 ZERO_STRUCTP(ptrust_list);
349 tmp_ctx = talloc_stackframe();
350 if (tmp_ctx == NULL) {
351 return NT_STATUS_NO_MEMORY;
354 again:
355 status = open_cached_internal_pipe_conn(domain,
356 NULL,
357 NULL,
358 &lsa_pipe,
359 &lsa_policy);
360 if (!NT_STATUS_IS_OK(status)) {
361 goto done;
364 status = rpc_trusted_domains(tmp_ctx,
365 lsa_pipe,
366 &lsa_policy,
367 &num_trusts,
368 &trusts);
370 if (!retry && reset_connection_on_error(domain, lsa_pipe, status)) {
371 retry = true;
372 goto again;
375 if (!NT_STATUS_IS_OK(status)) {
376 goto done;
379 if (ptrust_list) {
380 ptrust_list->count = num_trusts;
381 ptrust_list->array = talloc_move(mem_ctx, &trusts);
384 done:
385 TALLOC_FREE(tmp_ctx);
386 return status;
389 /* Lookup group membership given a rid. */
390 static NTSTATUS sam_lookup_groupmem(struct winbindd_domain *domain,
391 TALLOC_CTX *mem_ctx,
392 const struct dom_sid *group_sid,
393 enum lsa_SidType type,
394 uint32_t *pnum_names,
395 struct dom_sid **psid_mem,
396 char ***pnames,
397 uint32_t **pname_types)
399 struct rpc_pipe_client *samr_pipe;
400 struct policy_handle dom_pol = { 0 };
402 uint32_t num_names = 0;
403 struct dom_sid *sid_mem = NULL;
404 char **names = NULL;
405 uint32_t *name_types = NULL;
407 TALLOC_CTX *tmp_ctx;
408 NTSTATUS status;
409 bool retry = false;
411 DEBUG(3,("sam_lookup_groupmem\n"));
413 /* Paranoia check */
414 if (sid_check_is_in_builtin(group_sid) && (type != SID_NAME_ALIAS)) {
415 /* There's no groups, only aliases in BUILTIN */
416 return NT_STATUS_NO_SUCH_GROUP;
419 if (pnum_names) {
420 *pnum_names = 0;
423 tmp_ctx = talloc_stackframe();
424 if (tmp_ctx == NULL) {
425 return NT_STATUS_NO_MEMORY;
428 again:
429 status = open_cached_internal_pipe_conn(domain,
430 &samr_pipe,
431 &dom_pol,
432 NULL,
433 NULL);
434 if (!NT_STATUS_IS_OK(status)) {
435 goto done;
438 status = rpc_lookup_groupmem(tmp_ctx,
439 samr_pipe,
440 &dom_pol,
441 domain->name,
442 &domain->sid,
443 group_sid,
444 type,
445 &num_names,
446 &sid_mem,
447 &names,
448 &name_types);
450 if (!retry && reset_connection_on_error(domain, samr_pipe, status)) {
451 retry = true;
452 goto again;
455 if (pnum_names) {
456 *pnum_names = num_names;
459 if (pnames) {
460 *pnames = talloc_move(mem_ctx, &names);
463 if (pname_types) {
464 *pname_types = talloc_move(mem_ctx, &name_types);
467 if (psid_mem) {
468 *psid_mem = talloc_move(mem_ctx, &sid_mem);
471 done:
472 TALLOC_FREE(tmp_ctx);
473 return status;
476 /*********************************************************************
477 BUILTIN specific functions.
478 *********************************************************************/
480 /* List all domain groups */
481 static NTSTATUS builtin_enum_dom_groups(struct winbindd_domain *domain,
482 TALLOC_CTX *mem_ctx,
483 uint32_t *num_entries,
484 struct wb_acct_info **info)
486 /* BUILTIN doesn't have domain groups */
487 *num_entries = 0;
488 *info = NULL;
489 return NT_STATUS_OK;
492 /* Query display info for a domain */
493 static NTSTATUS builtin_query_user_list(struct winbindd_domain *domain,
494 TALLOC_CTX *mem_ctx,
495 uint32_t **rids)
497 /* We don't have users */
498 *rids = NULL;
499 return NT_STATUS_OK;
502 /* get a list of trusted domains - builtin domain */
503 static NTSTATUS builtin_trusted_domains(struct winbindd_domain *domain,
504 TALLOC_CTX *mem_ctx,
505 struct netr_DomainTrustList *trusts)
507 ZERO_STRUCTP(trusts);
508 return NT_STATUS_OK;
511 /*********************************************************************
512 COMMON functions.
513 *********************************************************************/
515 /* List all local groups (aliases) */
516 static NTSTATUS sam_enum_local_groups(struct winbindd_domain *domain,
517 TALLOC_CTX *mem_ctx,
518 uint32_t *pnum_info,
519 struct wb_acct_info **pinfo)
521 struct rpc_pipe_client *samr_pipe;
522 struct policy_handle dom_pol = { 0 };
523 struct wb_acct_info *info = NULL;
524 uint32_t num_info = 0;
525 TALLOC_CTX *tmp_ctx;
526 NTSTATUS status;
527 bool retry = false;
529 DEBUG(3,("samr: enum local groups\n"));
531 if (pnum_info) {
532 *pnum_info = 0;
535 tmp_ctx = talloc_stackframe();
536 if (tmp_ctx == NULL) {
537 return NT_STATUS_NO_MEMORY;
540 again:
541 status = open_cached_internal_pipe_conn(domain,
542 &samr_pipe,
543 &dom_pol,
544 NULL,
545 NULL);
546 if (!NT_STATUS_IS_OK(status)) {
547 goto done;
550 status = rpc_enum_local_groups(mem_ctx,
551 samr_pipe,
552 &dom_pol,
553 &num_info,
555 &info);
556 if (!retry && reset_connection_on_error(domain, samr_pipe, status)) {
557 retry = true;
558 goto again;
561 if (!NT_STATUS_IS_OK(status)) {
562 goto done;
565 if (pnum_info) {
566 *pnum_info = num_info;
569 if (pinfo) {
570 *pinfo = talloc_move(mem_ctx, &info);
573 done:
574 TALLOC_FREE(tmp_ctx);
575 return status;
578 /* convert a single name to a sid in a domain */
579 static NTSTATUS sam_name_to_sid(struct winbindd_domain *domain,
580 TALLOC_CTX *mem_ctx,
581 const char *domain_name,
582 const char *name,
583 uint32_t flags,
584 const char **pdom_name,
585 struct dom_sid *psid,
586 enum lsa_SidType *ptype)
588 struct rpc_pipe_client *lsa_pipe;
589 struct policy_handle lsa_policy = { 0 };
590 struct dom_sid sid;
591 const char *dom_name;
592 enum lsa_SidType type;
593 TALLOC_CTX *tmp_ctx;
594 NTSTATUS status;
595 bool retry = false;
597 DEBUG(3,("sam_name_to_sid\n"));
599 tmp_ctx = talloc_stackframe();
600 if (tmp_ctx == NULL) {
601 return NT_STATUS_NO_MEMORY;
604 again:
605 status = open_cached_internal_pipe_conn(domain,
606 NULL,
607 NULL,
608 &lsa_pipe,
609 &lsa_policy);
610 if (!NT_STATUS_IS_OK(status)) {
611 goto done;
614 status = rpc_name_to_sid(tmp_ctx,
615 lsa_pipe,
616 &lsa_policy,
617 domain_name,
618 name,
619 flags,
620 &dom_name,
621 &sid,
622 &type);
624 if (!retry && reset_connection_on_error(domain, lsa_pipe, status)) {
625 retry = true;
626 goto again;
629 if (!NT_STATUS_IS_OK(status)) {
630 goto done;
633 if (pdom_name != NULL) {
634 *pdom_name = talloc_strdup(mem_ctx, dom_name);
635 if (*pdom_name == NULL) {
636 status = NT_STATUS_NO_MEMORY;
637 goto done;
641 if (psid) {
642 sid_copy(psid, &sid);
644 if (ptype) {
645 *ptype = type;
648 done:
649 TALLOC_FREE(tmp_ctx);
650 return status;
653 /* convert a domain SID to a user or group name */
654 static NTSTATUS sam_sid_to_name(struct winbindd_domain *domain,
655 TALLOC_CTX *mem_ctx,
656 const struct dom_sid *sid,
657 char **pdomain_name,
658 char **pname,
659 enum lsa_SidType *ptype)
661 struct rpc_pipe_client *lsa_pipe;
662 struct policy_handle lsa_policy = { 0 };
663 char *domain_name = NULL;
664 char *name = NULL;
665 enum lsa_SidType type;
666 TALLOC_CTX *tmp_ctx;
667 NTSTATUS status;
668 bool retry = false;
670 DEBUG(3,("sam_sid_to_name\n"));
672 /* Paranoia check */
673 if (!sid_check_is_in_builtin(sid) &&
674 !sid_check_is_builtin(sid) &&
675 !sid_check_is_in_our_sam(sid) &&
676 !sid_check_is_our_sam(sid) &&
677 !sid_check_is_in_unix_users(sid) &&
678 !sid_check_is_unix_users(sid) &&
679 !sid_check_is_in_unix_groups(sid) &&
680 !sid_check_is_unix_groups(sid) &&
681 !sid_check_is_in_wellknown_domain(sid)) {
682 DEBUG(0, ("sam_sid_to_name: possible deadlock - trying to "
683 "lookup SID %s\n", sid_string_dbg(sid)));
684 return NT_STATUS_NONE_MAPPED;
687 tmp_ctx = talloc_stackframe();
688 if (tmp_ctx == NULL) {
689 return NT_STATUS_NO_MEMORY;
692 again:
693 status = open_cached_internal_pipe_conn(domain,
694 NULL,
695 NULL,
696 &lsa_pipe,
697 &lsa_policy);
698 if (!NT_STATUS_IS_OK(status)) {
699 goto done;
702 status = rpc_sid_to_name(tmp_ctx,
703 lsa_pipe,
704 &lsa_policy,
705 domain,
706 sid,
707 &domain_name,
708 &name,
709 &type);
711 if (!retry && reset_connection_on_error(domain, lsa_pipe, status)) {
712 retry = true;
713 goto again;
716 if (ptype) {
717 *ptype = type;
720 if (pname) {
721 *pname = talloc_move(mem_ctx, &name);
724 if (pdomain_name) {
725 *pdomain_name = talloc_move(mem_ctx, &domain_name);
728 done:
730 TALLOC_FREE(tmp_ctx);
731 return status;
734 static NTSTATUS sam_rids_to_names(struct winbindd_domain *domain,
735 TALLOC_CTX *mem_ctx,
736 const struct dom_sid *domain_sid,
737 uint32_t *rids,
738 size_t num_rids,
739 char **pdomain_name,
740 char ***pnames,
741 enum lsa_SidType **ptypes)
743 struct rpc_pipe_client *lsa_pipe;
744 struct policy_handle lsa_policy = { 0 };
745 enum lsa_SidType *types = NULL;
746 char *domain_name = NULL;
747 char **names = NULL;
748 TALLOC_CTX *tmp_ctx;
749 NTSTATUS status;
750 bool retry = false;
752 DEBUG(3,("sam_rids_to_names for %s\n", domain->name));
754 /* Paranoia check */
755 if (!sid_check_is_builtin(domain_sid) &&
756 !sid_check_is_our_sam(domain_sid) &&
757 !sid_check_is_unix_users(domain_sid) &&
758 !sid_check_is_unix_groups(domain_sid) &&
759 !sid_check_is_in_wellknown_domain(domain_sid)) {
760 DEBUG(0, ("sam_rids_to_names: possible deadlock - trying to "
761 "lookup SID %s\n", sid_string_dbg(domain_sid)));
762 return NT_STATUS_NONE_MAPPED;
765 tmp_ctx = talloc_stackframe();
766 if (tmp_ctx == NULL) {
767 return NT_STATUS_NO_MEMORY;
770 again:
771 status = open_cached_internal_pipe_conn(domain,
772 NULL,
773 NULL,
774 &lsa_pipe,
775 &lsa_policy);
776 if (!NT_STATUS_IS_OK(status)) {
777 goto done;
780 status = rpc_rids_to_names(tmp_ctx,
781 lsa_pipe,
782 &lsa_policy,
783 domain,
784 domain_sid,
785 rids,
786 num_rids,
787 &domain_name,
788 &names,
789 &types);
791 if (!retry && reset_connection_on_error(domain, lsa_pipe, status)) {
792 retry = true;
793 goto again;
796 if (!NT_STATUS_IS_OK(status)) {
797 goto done;
800 if (pdomain_name) {
801 *pdomain_name = talloc_move(mem_ctx, &domain_name);
804 if (ptypes) {
805 *ptypes = talloc_move(mem_ctx, &types);
808 if (pnames) {
809 *pnames = talloc_move(mem_ctx, &names);
812 done:
813 TALLOC_FREE(tmp_ctx);
814 return status;
817 static NTSTATUS sam_lockout_policy(struct winbindd_domain *domain,
818 TALLOC_CTX *mem_ctx,
819 struct samr_DomInfo12 *lockout_policy)
821 struct rpc_pipe_client *samr_pipe;
822 struct policy_handle dom_pol = { 0 };
823 union samr_DomainInfo *info = NULL;
824 TALLOC_CTX *tmp_ctx;
825 NTSTATUS status, result;
826 struct dcerpc_binding_handle *b = NULL;
827 bool retry = false;
829 DEBUG(3,("sam_lockout_policy\n"));
831 tmp_ctx = talloc_stackframe();
832 if (tmp_ctx == NULL) {
833 return NT_STATUS_NO_MEMORY;
836 again:
837 status = open_cached_internal_pipe_conn(domain,
838 &samr_pipe,
839 &dom_pol,
840 NULL,
841 NULL);
842 if (!NT_STATUS_IS_OK(status)) {
843 goto error;
846 b = samr_pipe->binding_handle;
848 status = dcerpc_samr_QueryDomainInfo(b,
849 mem_ctx,
850 &dom_pol,
851 DomainLockoutInformation,
852 &info,
853 &result);
855 if (!retry && reset_connection_on_error(domain, samr_pipe, status)) {
856 retry = true;
857 goto again;
860 if (!NT_STATUS_IS_OK(status)) {
861 goto error;
863 if (!NT_STATUS_IS_OK(result)) {
864 status = result;
865 goto error;
868 *lockout_policy = info->info12;
870 error:
871 TALLOC_FREE(tmp_ctx);
872 return status;
875 static NTSTATUS sam_password_policy(struct winbindd_domain *domain,
876 TALLOC_CTX *mem_ctx,
877 struct samr_DomInfo1 *passwd_policy)
879 struct rpc_pipe_client *samr_pipe;
880 struct policy_handle dom_pol = { 0 };
881 union samr_DomainInfo *info = NULL;
882 TALLOC_CTX *tmp_ctx;
883 NTSTATUS status, result;
884 struct dcerpc_binding_handle *b = NULL;
885 bool retry = false;
887 DEBUG(3,("sam_password_policy\n"));
889 tmp_ctx = talloc_stackframe();
890 if (tmp_ctx == NULL) {
891 return NT_STATUS_NO_MEMORY;
894 again:
895 status = open_cached_internal_pipe_conn(domain,
896 &samr_pipe,
897 &dom_pol,
898 NULL,
899 NULL);
900 if (!NT_STATUS_IS_OK(status)) {
901 goto error;
904 b = samr_pipe->binding_handle;
906 status = dcerpc_samr_QueryDomainInfo(b,
907 mem_ctx,
908 &dom_pol,
909 DomainPasswordInformation,
910 &info,
911 &result);
913 if (!retry && reset_connection_on_error(domain, samr_pipe, status)) {
914 retry = true;
915 goto again;
918 if (!NT_STATUS_IS_OK(status)) {
919 goto error;
921 if (!NT_STATUS_IS_OK(result)) {
922 status = result;
923 goto error;
926 *passwd_policy = info->info1;
928 error:
929 TALLOC_FREE(tmp_ctx);
930 return status;
933 /* Lookup groups a user is a member of. */
934 static NTSTATUS sam_lookup_usergroups(struct winbindd_domain *domain,
935 TALLOC_CTX *mem_ctx,
936 const struct dom_sid *user_sid,
937 uint32_t *pnum_groups,
938 struct dom_sid **puser_grpsids)
940 struct rpc_pipe_client *samr_pipe;
941 struct policy_handle dom_pol;
942 struct dom_sid *user_grpsids = NULL;
943 uint32_t num_groups = 0;
944 TALLOC_CTX *tmp_ctx;
945 NTSTATUS status;
946 bool retry = false;
948 DEBUG(3,("sam_lookup_usergroups\n"));
950 ZERO_STRUCT(dom_pol);
952 if (pnum_groups) {
953 *pnum_groups = 0;
956 tmp_ctx = talloc_stackframe();
957 if (tmp_ctx == NULL) {
958 return NT_STATUS_NO_MEMORY;
961 again:
962 status = open_cached_internal_pipe_conn(domain,
963 &samr_pipe,
964 &dom_pol,
965 NULL,
966 NULL);
967 if (!NT_STATUS_IS_OK(status)) {
968 goto done;
971 status = rpc_lookup_usergroups(tmp_ctx,
972 samr_pipe,
973 &dom_pol,
974 &domain->sid,
975 user_sid,
976 &num_groups,
977 &user_grpsids);
979 if (!retry && reset_connection_on_error(domain, samr_pipe, status)) {
980 retry = true;
981 goto again;
984 if (!NT_STATUS_IS_OK(status)) {
985 goto done;
988 if (pnum_groups) {
989 *pnum_groups = num_groups;
992 if (puser_grpsids) {
993 *puser_grpsids = talloc_move(mem_ctx, &user_grpsids);
996 done:
998 TALLOC_FREE(tmp_ctx);
999 return status;
1002 static NTSTATUS sam_lookup_useraliases(struct winbindd_domain *domain,
1003 TALLOC_CTX *mem_ctx,
1004 uint32_t num_sids,
1005 const struct dom_sid *sids,
1006 uint32_t *pnum_aliases,
1007 uint32_t **palias_rids)
1009 struct rpc_pipe_client *samr_pipe;
1010 struct policy_handle dom_pol = { 0 };
1011 uint32_t num_aliases = 0;
1012 uint32_t *alias_rids = NULL;
1013 TALLOC_CTX *tmp_ctx;
1014 NTSTATUS status;
1015 bool retry = false;
1017 DEBUG(3,("sam_lookup_useraliases\n"));
1019 if (pnum_aliases) {
1020 *pnum_aliases = 0;
1023 tmp_ctx = talloc_stackframe();
1024 if (tmp_ctx == NULL) {
1025 return NT_STATUS_NO_MEMORY;
1028 again:
1029 status = open_cached_internal_pipe_conn(domain,
1030 &samr_pipe,
1031 &dom_pol,
1032 NULL,
1033 NULL);
1034 if (!NT_STATUS_IS_OK(status)) {
1035 goto done;
1038 status = rpc_lookup_useraliases(tmp_ctx,
1039 samr_pipe,
1040 &dom_pol,
1041 num_sids,
1042 sids,
1043 &num_aliases,
1044 &alias_rids);
1046 if (!retry && reset_connection_on_error(domain, samr_pipe, status)) {
1047 retry = true;
1048 goto again;
1051 if (!NT_STATUS_IS_OK(status)) {
1052 goto done;
1055 if (pnum_aliases) {
1056 *pnum_aliases = num_aliases;
1059 if (palias_rids) {
1060 *palias_rids = talloc_move(mem_ctx, &alias_rids);
1063 done:
1065 TALLOC_FREE(tmp_ctx);
1066 return status;
1069 /* find the sequence number for a domain */
1070 static NTSTATUS sam_sequence_number(struct winbindd_domain *domain,
1071 uint32_t *pseq)
1073 struct rpc_pipe_client *samr_pipe;
1074 struct policy_handle dom_pol = { 0 };
1075 uint32_t seq = DOM_SEQUENCE_NONE;
1076 TALLOC_CTX *tmp_ctx;
1077 NTSTATUS status;
1078 bool retry = false;
1080 DEBUG(3,("samr: sequence number\n"));
1082 if (pseq) {
1083 *pseq = DOM_SEQUENCE_NONE;
1086 tmp_ctx = talloc_stackframe();
1087 if (tmp_ctx == NULL) {
1088 return NT_STATUS_NO_MEMORY;
1091 again:
1092 status = open_cached_internal_pipe_conn(domain,
1093 &samr_pipe,
1094 &dom_pol,
1095 NULL,
1096 NULL);
1097 if (!NT_STATUS_IS_OK(status)) {
1098 goto done;
1101 status = rpc_sequence_number(tmp_ctx,
1102 samr_pipe,
1103 &dom_pol,
1104 domain->name,
1105 &seq);
1107 if (!retry && reset_connection_on_error(domain, samr_pipe, status)) {
1108 retry = true;
1109 goto again;
1112 if (!NT_STATUS_IS_OK(status)) {
1113 goto done;
1116 if (pseq) {
1117 *pseq = seq;
1120 done:
1121 TALLOC_FREE(tmp_ctx);
1122 return status;
1125 /* the rpc backend methods are exposed via this structure */
1126 struct winbindd_methods builtin_passdb_methods = {
1127 .consistent = false,
1129 .query_user_list = builtin_query_user_list,
1130 .enum_dom_groups = builtin_enum_dom_groups,
1131 .enum_local_groups = sam_enum_local_groups,
1132 .name_to_sid = sam_name_to_sid,
1133 .sid_to_name = sam_sid_to_name,
1134 .rids_to_names = sam_rids_to_names,
1135 .lookup_usergroups = sam_lookup_usergroups,
1136 .lookup_useraliases = sam_lookup_useraliases,
1137 .lookup_groupmem = sam_lookup_groupmem,
1138 .sequence_number = sam_sequence_number,
1139 .lockout_policy = sam_lockout_policy,
1140 .password_policy = sam_password_policy,
1141 .trusted_domains = builtin_trusted_domains
1144 /* the rpc backend methods are exposed via this structure */
1145 struct winbindd_methods sam_passdb_methods = {
1146 .consistent = false,
1148 .query_user_list = sam_query_user_list,
1149 .enum_dom_groups = sam_enum_dom_groups,
1150 .enum_local_groups = sam_enum_local_groups,
1151 .name_to_sid = sam_name_to_sid,
1152 .sid_to_name = sam_sid_to_name,
1153 .rids_to_names = sam_rids_to_names,
1154 .lookup_usergroups = sam_lookup_usergroups,
1155 .lookup_useraliases = sam_lookup_useraliases,
1156 .lookup_groupmem = sam_lookup_groupmem,
1157 .sequence_number = sam_sequence_number,
1158 .lockout_policy = sam_lockout_policy,
1159 .password_policy = sam_password_policy,
1160 .trusted_domains = sam_trusted_domains