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/>.
28 #include "winbindd_rpc.h"
29 #include "lib/util_unixsids.h"
30 #include "rpc_client/rpc_client.h"
31 #include "../librpc/gen_ndr/ndr_samr_c.h"
32 #include "rpc_client/cli_samr.h"
33 #include "../librpc/gen_ndr/ndr_lsa_c.h"
34 #include "rpc_client/cli_lsarpc.h"
35 #include "rpc_server/rpc_ncacn_np.h"
36 #include "../libcli/security/security.h"
37 #include "passdb/machine_sid.h"
41 #define DBGC_CLASS DBGC_WINBIND
43 NTSTATUS open_internal_samr_conn(TALLOC_CTX *mem_ctx,
44 struct winbindd_domain *domain,
45 struct rpc_pipe_client **samr_pipe,
46 struct policy_handle *samr_domain_hnd)
48 NTSTATUS status, result;
49 struct policy_handle samr_connect_hnd;
50 struct dcerpc_binding_handle *b;
52 status = wb_open_internal_pipe(mem_ctx, &ndr_table_samr, samr_pipe);
53 if (!NT_STATUS_IS_OK(status)) {
57 b = (*samr_pipe)->binding_handle;
59 status = dcerpc_samr_Connect2(b, mem_ctx,
60 (*samr_pipe)->desthost,
61 SEC_FLAG_MAXIMUM_ALLOWED,
64 if (!NT_STATUS_IS_OK(status)) {
67 if (!NT_STATUS_IS_OK(result)) {
71 status = dcerpc_samr_OpenDomain(b, mem_ctx,
73 SEC_FLAG_MAXIMUM_ALLOWED,
77 if (!NT_STATUS_IS_OK(status)) {
84 NTSTATUS open_internal_lsa_conn(TALLOC_CTX *mem_ctx,
85 struct rpc_pipe_client **lsa_pipe,
86 struct policy_handle *lsa_hnd)
90 status = wb_open_internal_pipe(mem_ctx, &ndr_table_lsarpc, lsa_pipe);
91 if (!NT_STATUS_IS_OK(status)) {
95 status = rpccli_lsa_open_policy((*lsa_pipe),
98 SEC_FLAG_MAXIMUM_ALLOWED,
104 /*********************************************************************
105 SAM specific functions.
106 *********************************************************************/
108 /* List all domain groups */
109 static NTSTATUS sam_enum_dom_groups(struct winbindd_domain *domain,
112 struct wb_acct_info **pinfo)
114 struct rpc_pipe_client *samr_pipe;
115 struct policy_handle dom_pol = { 0 };
116 struct wb_acct_info *info = NULL;
117 uint32_t num_info = 0;
119 NTSTATUS status, result;
120 struct dcerpc_binding_handle *b = NULL;
122 DEBUG(3,("sam_enum_dom_groups\n"));
128 tmp_ctx = talloc_stackframe();
129 if (tmp_ctx == NULL) {
130 return NT_STATUS_NO_MEMORY;
133 status = open_internal_samr_conn(tmp_ctx, domain, &samr_pipe, &dom_pol);
134 if (!NT_STATUS_IS_OK(status)) {
138 b = samr_pipe->binding_handle;
140 status = rpc_enum_dom_groups(tmp_ctx,
145 if (!NT_STATUS_IS_OK(status)) {
150 *pnum_info = num_info;
154 *pinfo = talloc_move(mem_ctx, &info);
158 if (b && is_valid_policy_hnd(&dom_pol)) {
159 dcerpc_samr_Close(b, mem_ctx, &dom_pol, &result);
161 TALLOC_FREE(tmp_ctx);
165 /* Query display info for a domain */
166 static NTSTATUS sam_query_user_list(struct winbindd_domain *domain,
170 struct rpc_pipe_client *samr_pipe = NULL;
171 struct policy_handle dom_pol = { 0 };
172 uint32_t *rids = NULL;
174 NTSTATUS status, result;
175 struct dcerpc_binding_handle *b = NULL;
177 DEBUG(3,("samr_query_user_list\n"));
179 tmp_ctx = talloc_stackframe();
180 if (tmp_ctx == NULL) {
181 return NT_STATUS_NO_MEMORY;
184 status = open_internal_samr_conn(tmp_ctx, domain, &samr_pipe, &dom_pol);
185 if (!NT_STATUS_IS_OK(status)) {
189 b = samr_pipe->binding_handle;
191 status = rpc_query_user_list(tmp_ctx,
196 if (!NT_STATUS_IS_OK(status)) {
201 *prids = talloc_move(mem_ctx, &rids);
205 if (b && is_valid_policy_hnd(&dom_pol)) {
206 dcerpc_samr_Close(b, mem_ctx, &dom_pol, &result);
210 TALLOC_FREE(tmp_ctx);
214 /* get a list of trusted domains - builtin domain */
215 static NTSTATUS sam_trusted_domains(struct winbindd_domain *domain,
217 struct netr_DomainTrustList *ptrust_list)
219 struct rpc_pipe_client *lsa_pipe;
220 struct policy_handle lsa_policy = { 0 };
221 struct netr_DomainTrust *trusts = NULL;
222 uint32_t num_trusts = 0;
224 NTSTATUS status, result;
225 struct dcerpc_binding_handle *b = NULL;
227 DEBUG(3,("samr: trusted domains\n"));
230 ZERO_STRUCTP(ptrust_list);
233 tmp_ctx = talloc_stackframe();
234 if (tmp_ctx == NULL) {
235 return NT_STATUS_NO_MEMORY;
238 status = open_internal_lsa_conn(tmp_ctx, &lsa_pipe, &lsa_policy);
239 if (!NT_STATUS_IS_OK(status)) {
243 b = lsa_pipe->binding_handle;
245 status = rpc_trusted_domains(tmp_ctx,
250 if (!NT_STATUS_IS_OK(status)) {
255 ptrust_list->count = num_trusts;
256 ptrust_list->array = talloc_move(mem_ctx, &trusts);
260 if (b && is_valid_policy_hnd(&lsa_policy)) {
261 dcerpc_lsa_Close(b, mem_ctx, &lsa_policy, &result);
264 TALLOC_FREE(tmp_ctx);
268 /* Lookup group membership given a rid. */
269 static NTSTATUS sam_lookup_groupmem(struct winbindd_domain *domain,
271 const struct dom_sid *group_sid,
272 enum lsa_SidType type,
273 uint32_t *pnum_names,
274 struct dom_sid **psid_mem,
276 uint32_t **pname_types)
278 struct rpc_pipe_client *samr_pipe;
279 struct policy_handle dom_pol = { 0 };
281 uint32_t num_names = 0;
282 struct dom_sid *sid_mem = NULL;
284 uint32_t *name_types = NULL;
287 NTSTATUS status, result;
288 struct dcerpc_binding_handle *b = NULL;
290 DEBUG(3,("sam_lookup_groupmem\n"));
293 if (sid_check_is_in_builtin(group_sid) && (type != SID_NAME_ALIAS)) {
294 /* There's no groups, only aliases in BUILTIN */
295 return NT_STATUS_NO_SUCH_GROUP;
302 tmp_ctx = talloc_stackframe();
303 if (tmp_ctx == NULL) {
304 return NT_STATUS_NO_MEMORY;
307 status = open_internal_samr_conn(tmp_ctx, domain, &samr_pipe, &dom_pol);
308 if (!NT_STATUS_IS_OK(status)) {
312 b = samr_pipe->binding_handle;
314 status = rpc_lookup_groupmem(tmp_ctx,
327 *pnum_names = num_names;
331 *pnames = talloc_move(mem_ctx, &names);
335 *pname_types = talloc_move(mem_ctx, &name_types);
339 *psid_mem = talloc_move(mem_ctx, &sid_mem);
343 if (b && is_valid_policy_hnd(&dom_pol)) {
344 dcerpc_samr_Close(b, mem_ctx, &dom_pol, &result);
347 TALLOC_FREE(tmp_ctx);
351 /*********************************************************************
352 BUILTIN specific functions.
353 *********************************************************************/
355 /* List all domain groups */
356 static NTSTATUS builtin_enum_dom_groups(struct winbindd_domain *domain,
358 uint32_t *num_entries,
359 struct wb_acct_info **info)
361 /* BUILTIN doesn't have domain groups */
367 /* Query display info for a domain */
368 static NTSTATUS builtin_query_user_list(struct winbindd_domain *domain,
372 /* We don't have users */
377 /* get a list of trusted domains - builtin domain */
378 static NTSTATUS builtin_trusted_domains(struct winbindd_domain *domain,
380 struct netr_DomainTrustList *trusts)
382 ZERO_STRUCTP(trusts);
386 /*********************************************************************
388 *********************************************************************/
390 /* List all local groups (aliases) */
391 static NTSTATUS sam_enum_local_groups(struct winbindd_domain *domain,
394 struct wb_acct_info **pinfo)
396 struct rpc_pipe_client *samr_pipe;
397 struct policy_handle dom_pol = { 0 };
398 struct wb_acct_info *info = NULL;
399 uint32_t num_info = 0;
401 NTSTATUS status, result;
402 struct dcerpc_binding_handle *b = NULL;
404 DEBUG(3,("samr: enum local groups\n"));
410 tmp_ctx = talloc_stackframe();
411 if (tmp_ctx == NULL) {
412 return NT_STATUS_NO_MEMORY;
415 status = open_internal_samr_conn(tmp_ctx, domain, &samr_pipe, &dom_pol);
416 if (!NT_STATUS_IS_OK(status)) {
420 b = samr_pipe->binding_handle;
422 status = rpc_enum_local_groups(mem_ctx,
427 if (!NT_STATUS_IS_OK(status)) {
432 *pnum_info = num_info;
436 *pinfo = talloc_move(mem_ctx, &info);
440 if (b && is_valid_policy_hnd(&dom_pol)) {
441 dcerpc_samr_Close(b, mem_ctx, &dom_pol, &result);
444 TALLOC_FREE(tmp_ctx);
448 /* convert a single name to a sid in a domain */
449 static NTSTATUS sam_name_to_sid(struct winbindd_domain *domain,
451 const char *domain_name,
454 struct dom_sid *psid,
455 enum lsa_SidType *ptype)
457 struct rpc_pipe_client *lsa_pipe;
458 struct policy_handle lsa_policy = { 0 };
460 enum lsa_SidType type;
462 NTSTATUS status, result;
463 struct dcerpc_binding_handle *b = NULL;
465 DEBUG(3,("sam_name_to_sid\n"));
467 tmp_ctx = talloc_stackframe();
468 if (tmp_ctx == NULL) {
469 return NT_STATUS_NO_MEMORY;
472 status = open_internal_lsa_conn(tmp_ctx, &lsa_pipe, &lsa_policy);
473 if (!NT_STATUS_IS_OK(status)) {
477 b = lsa_pipe->binding_handle;
479 status = rpc_name_to_sid(tmp_ctx,
487 if (!NT_STATUS_IS_OK(status)) {
492 sid_copy(psid, &sid);
499 if (b && is_valid_policy_hnd(&lsa_policy)) {
500 dcerpc_lsa_Close(b, mem_ctx, &lsa_policy, &result);
503 TALLOC_FREE(tmp_ctx);
507 /* convert a domain SID to a user or group name */
508 static NTSTATUS sam_sid_to_name(struct winbindd_domain *domain,
510 const struct dom_sid *sid,
513 enum lsa_SidType *ptype)
515 struct rpc_pipe_client *lsa_pipe;
516 struct policy_handle lsa_policy = { 0 };
517 char *domain_name = NULL;
519 enum lsa_SidType type;
521 NTSTATUS status, result;
522 struct dcerpc_binding_handle *b = NULL;
524 DEBUG(3,("sam_sid_to_name\n"));
527 if (!sid_check_is_in_builtin(sid) &&
528 !sid_check_is_builtin(sid) &&
529 !sid_check_is_in_our_sam(sid) &&
530 !sid_check_is_our_sam(sid) &&
531 !sid_check_is_in_unix_users(sid) &&
532 !sid_check_is_unix_users(sid) &&
533 !sid_check_is_in_unix_groups(sid) &&
534 !sid_check_is_unix_groups(sid) &&
535 !sid_check_is_in_wellknown_domain(sid)) {
536 DEBUG(0, ("sam_sid_to_name: possible deadlock - trying to "
537 "lookup SID %s\n", sid_string_dbg(sid)));
538 return NT_STATUS_NONE_MAPPED;
541 tmp_ctx = talloc_stackframe();
542 if (tmp_ctx == NULL) {
543 return NT_STATUS_NO_MEMORY;
546 status = open_internal_lsa_conn(tmp_ctx, &lsa_pipe, &lsa_policy);
547 if (!NT_STATUS_IS_OK(status)) {
551 b = lsa_pipe->binding_handle;
553 status = rpc_sid_to_name(tmp_ctx,
567 *pname = talloc_move(mem_ctx, &name);
571 *pdomain_name = talloc_move(mem_ctx, &domain_name);
575 if (b && is_valid_policy_hnd(&lsa_policy)) {
576 dcerpc_lsa_Close(b, mem_ctx, &lsa_policy, &result);
579 TALLOC_FREE(tmp_ctx);
583 static NTSTATUS sam_rids_to_names(struct winbindd_domain *domain,
585 const struct dom_sid *domain_sid,
590 enum lsa_SidType **ptypes)
592 struct rpc_pipe_client *lsa_pipe;
593 struct policy_handle lsa_policy = { 0 };
594 enum lsa_SidType *types = NULL;
595 char *domain_name = NULL;
598 NTSTATUS status, result;
599 struct dcerpc_binding_handle *b = NULL;
601 DEBUG(3,("sam_rids_to_names for %s\n", domain->name));
604 if (!sid_check_is_builtin(domain_sid) &&
605 !sid_check_is_our_sam(domain_sid) &&
606 !sid_check_is_unix_users(domain_sid) &&
607 !sid_check_is_unix_groups(domain_sid) &&
608 !sid_check_is_in_wellknown_domain(domain_sid)) {
609 DEBUG(0, ("sam_rids_to_names: possible deadlock - trying to "
610 "lookup SID %s\n", sid_string_dbg(domain_sid)));
611 return NT_STATUS_NONE_MAPPED;
614 tmp_ctx = talloc_stackframe();
615 if (tmp_ctx == NULL) {
616 return NT_STATUS_NO_MEMORY;
619 status = open_internal_lsa_conn(tmp_ctx, &lsa_pipe, &lsa_policy);
620 if (!NT_STATUS_IS_OK(status)) {
624 b = lsa_pipe->binding_handle;
626 status = rpc_rids_to_names(tmp_ctx,
636 if (!NT_STATUS_IS_OK(status)) {
641 *pdomain_name = talloc_move(mem_ctx, &domain_name);
645 *ptypes = talloc_move(mem_ctx, &types);
649 *pnames = talloc_move(mem_ctx, &names);
653 if (b && is_valid_policy_hnd(&lsa_policy)) {
654 dcerpc_lsa_Close(b, mem_ctx, &lsa_policy, &result);
657 TALLOC_FREE(tmp_ctx);
661 static NTSTATUS sam_lockout_policy(struct winbindd_domain *domain,
663 struct samr_DomInfo12 *lockout_policy)
665 struct rpc_pipe_client *samr_pipe;
666 struct policy_handle dom_pol = { 0 };
667 union samr_DomainInfo *info = NULL;
669 NTSTATUS status, result;
670 struct dcerpc_binding_handle *b = NULL;
672 DEBUG(3,("sam_lockout_policy\n"));
674 tmp_ctx = talloc_stackframe();
675 if (tmp_ctx == NULL) {
676 return NT_STATUS_NO_MEMORY;
679 status = open_internal_samr_conn(tmp_ctx, domain, &samr_pipe, &dom_pol);
680 if (!NT_STATUS_IS_OK(status)) {
684 b = samr_pipe->binding_handle;
686 status = dcerpc_samr_QueryDomainInfo(b,
689 DomainLockoutInformation,
692 if (!NT_STATUS_IS_OK(status)) {
695 if (!NT_STATUS_IS_OK(result)) {
700 *lockout_policy = info->info12;
703 if (b && is_valid_policy_hnd(&dom_pol)) {
704 dcerpc_samr_Close(b, mem_ctx, &dom_pol, &result);
707 TALLOC_FREE(tmp_ctx);
711 static NTSTATUS sam_password_policy(struct winbindd_domain *domain,
713 struct samr_DomInfo1 *passwd_policy)
715 struct rpc_pipe_client *samr_pipe;
716 struct policy_handle dom_pol = { 0 };
717 union samr_DomainInfo *info = NULL;
719 NTSTATUS status, result;
720 struct dcerpc_binding_handle *b = NULL;
722 DEBUG(3,("sam_password_policy\n"));
724 tmp_ctx = talloc_stackframe();
725 if (tmp_ctx == NULL) {
726 return NT_STATUS_NO_MEMORY;
729 status = open_internal_samr_conn(tmp_ctx, domain, &samr_pipe, &dom_pol);
730 if (!NT_STATUS_IS_OK(status)) {
734 b = samr_pipe->binding_handle;
736 status = dcerpc_samr_QueryDomainInfo(b,
739 DomainPasswordInformation,
742 if (!NT_STATUS_IS_OK(status)) {
745 if (!NT_STATUS_IS_OK(result)) {
750 *passwd_policy = info->info1;
753 if (b && is_valid_policy_hnd(&dom_pol)) {
754 dcerpc_samr_Close(b, mem_ctx, &dom_pol, &result);
757 TALLOC_FREE(tmp_ctx);
761 /* Lookup groups a user is a member of. */
762 static NTSTATUS sam_lookup_usergroups(struct winbindd_domain *domain,
764 const struct dom_sid *user_sid,
765 uint32_t *pnum_groups,
766 struct dom_sid **puser_grpsids)
768 struct rpc_pipe_client *samr_pipe;
769 struct policy_handle dom_pol;
770 struct dom_sid *user_grpsids = NULL;
771 uint32_t num_groups = 0;
773 NTSTATUS status, result;
774 struct dcerpc_binding_handle *b = NULL;
776 DEBUG(3,("sam_lookup_usergroups\n"));
778 ZERO_STRUCT(dom_pol);
784 tmp_ctx = talloc_stackframe();
785 if (tmp_ctx == NULL) {
786 return NT_STATUS_NO_MEMORY;
789 status = open_internal_samr_conn(tmp_ctx, domain, &samr_pipe, &dom_pol);
790 if (!NT_STATUS_IS_OK(status)) {
794 b = samr_pipe->binding_handle;
796 status = rpc_lookup_usergroups(tmp_ctx,
803 if (!NT_STATUS_IS_OK(status)) {
808 *pnum_groups = num_groups;
812 *puser_grpsids = talloc_move(mem_ctx, &user_grpsids);
816 if (b && is_valid_policy_hnd(&dom_pol)) {
817 dcerpc_samr_Close(b, mem_ctx, &dom_pol, &result);
820 TALLOC_FREE(tmp_ctx);
824 static NTSTATUS sam_lookup_useraliases(struct winbindd_domain *domain,
827 const struct dom_sid *sids,
828 uint32_t *pnum_aliases,
829 uint32_t **palias_rids)
831 struct rpc_pipe_client *samr_pipe;
832 struct policy_handle dom_pol = { 0 };
833 uint32_t num_aliases = 0;
834 uint32_t *alias_rids = NULL;
836 NTSTATUS status, result;
837 struct dcerpc_binding_handle *b = NULL;
839 DEBUG(3,("sam_lookup_useraliases\n"));
845 tmp_ctx = talloc_stackframe();
846 if (tmp_ctx == NULL) {
847 return NT_STATUS_NO_MEMORY;
850 status = open_internal_samr_conn(tmp_ctx, domain, &samr_pipe, &dom_pol);
851 if (!NT_STATUS_IS_OK(status)) {
855 b = samr_pipe->binding_handle;
857 status = rpc_lookup_useraliases(tmp_ctx,
864 if (!NT_STATUS_IS_OK(status)) {
869 *pnum_aliases = num_aliases;
873 *palias_rids = talloc_move(mem_ctx, &alias_rids);
877 if (b && is_valid_policy_hnd(&dom_pol)) {
878 dcerpc_samr_Close(b, mem_ctx, &dom_pol, &result);
881 TALLOC_FREE(tmp_ctx);
885 /* find the sequence number for a domain */
886 static NTSTATUS sam_sequence_number(struct winbindd_domain *domain,
889 struct rpc_pipe_client *samr_pipe;
890 struct policy_handle dom_pol = { 0 };
891 uint32_t seq = DOM_SEQUENCE_NONE;
893 NTSTATUS status, result;
894 struct dcerpc_binding_handle *b = NULL;
896 DEBUG(3,("samr: sequence number\n"));
899 *pseq = DOM_SEQUENCE_NONE;
902 tmp_ctx = talloc_stackframe();
903 if (tmp_ctx == NULL) {
904 return NT_STATUS_NO_MEMORY;
907 status = open_internal_samr_conn(tmp_ctx, domain, &samr_pipe, &dom_pol);
908 if (!NT_STATUS_IS_OK(status)) {
912 b = samr_pipe->binding_handle;
914 status = rpc_sequence_number(tmp_ctx,
919 if (!NT_STATUS_IS_OK(status)) {
927 if (b && is_valid_policy_hnd(&dom_pol)) {
928 dcerpc_samr_Close(b, tmp_ctx, &dom_pol, &result);
931 TALLOC_FREE(tmp_ctx);
935 /* the rpc backend methods are exposed via this structure */
936 struct winbindd_methods builtin_passdb_methods = {
939 .query_user_list = builtin_query_user_list,
940 .enum_dom_groups = builtin_enum_dom_groups,
941 .enum_local_groups = sam_enum_local_groups,
942 .name_to_sid = sam_name_to_sid,
943 .sid_to_name = sam_sid_to_name,
944 .rids_to_names = sam_rids_to_names,
945 .lookup_usergroups = sam_lookup_usergroups,
946 .lookup_useraliases = sam_lookup_useraliases,
947 .lookup_groupmem = sam_lookup_groupmem,
948 .sequence_number = sam_sequence_number,
949 .lockout_policy = sam_lockout_policy,
950 .password_policy = sam_password_policy,
951 .trusted_domains = builtin_trusted_domains
954 /* the rpc backend methods are exposed via this structure */
955 struct winbindd_methods sam_passdb_methods = {
958 .query_user_list = sam_query_user_list,
959 .enum_dom_groups = sam_enum_dom_groups,
960 .enum_local_groups = sam_enum_local_groups,
961 .name_to_sid = sam_name_to_sid,
962 .sid_to_name = sam_sid_to_name,
963 .rids_to_names = sam_rids_to_names,
964 .lookup_usergroups = sam_lookup_usergroups,
965 .lookup_useraliases = sam_lookup_useraliases,
966 .lookup_groupmem = sam_lookup_groupmem,
967 .sequence_number = sam_sequence_number,
968 .lockout_policy = sam_lockout_policy,
969 .password_policy = sam_password_policy,
970 .trusted_domains = sam_trusted_domains