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 "rpc_client/rpc_client.h"
30 #include "librpc/gen_ndr/ndr_samr_c.h"
31 #include "librpc/gen_ndr/ndr_lsa_c.h"
32 #include "rpc_client/cli_samr.h"
33 #include "rpc_client/cli_lsarpc.h"
34 #include "../libcli/security/security.h"
37 /* Query display info for a domain */
38 NTSTATUS
rpc_query_user_list(TALLOC_CTX
*mem_ctx
,
39 struct rpc_pipe_client
*samr_pipe
,
40 struct policy_handle
*samr_policy
,
41 const struct dom_sid
*domain_sid
,
44 struct dcerpc_binding_handle
*b
= samr_pipe
->binding_handle
;
45 uint32_t *rids
= NULL
;
46 uint32_t num_rids
= 0;
48 uint32_t resume_handle
= 0;
49 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
50 NTSTATUS status
= NT_STATUS_UNSUCCESSFUL
;
55 tmp_ctx
= talloc_stackframe();
56 if (tmp_ctx
== NULL
) {
57 return NT_STATUS_NO_MEMORY
;
61 struct samr_SamArray
*sam_array
= NULL
;
65 status
= dcerpc_samr_EnumDomainUsers(
66 b
, tmp_ctx
, samr_policy
, &resume_handle
,
67 ACB_NORMAL
, &sam_array
, 0xffff, &count
, &result
);
68 if (!NT_STATUS_IS_OK(status
)) {
71 if (!NT_STATUS_IS_OK(result
)) {
72 if (!NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
)) {
73 DBG_WARNING("EnumDomainUsers failed: %s\n",
80 if (num_rids
+ count
< num_rids
) {
81 status
= NT_STATUS_INTEGER_OVERFLOW
;
85 tmp
= talloc_realloc(tmp_ctx
, rids
, uint32_t, num_rids
+count
);
87 status
= NT_STATUS_NO_MEMORY
;
92 for (i
=0; i
<count
; i
++) {
93 rids
[num_rids
++] = sam_array
->entries
[i
].idx
;
96 TALLOC_FREE(sam_array
);
97 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
99 *prids
= talloc_steal(mem_ctx
, rids
);
100 status
= NT_STATUS_OK
;
103 TALLOC_FREE(tmp_ctx
);
107 /* List all domain groups */
108 NTSTATUS
rpc_enum_dom_groups(TALLOC_CTX
*mem_ctx
,
109 struct rpc_pipe_client
*samr_pipe
,
110 struct policy_handle
*samr_policy
,
112 struct wb_acct_info
**pinfo
)
114 struct wb_acct_info
*info
= NULL
;
116 uint32_t num_info
= 0;
117 NTSTATUS status
, result
;
118 struct dcerpc_binding_handle
*b
= samr_pipe
->binding_handle
;
123 struct samr_SamArray
*sam_array
= NULL
;
127 /* start is updated by this call. */
128 status
= dcerpc_samr_EnumDomainGroups(b
,
133 0xFFFF, /* buffer size? */
136 if (!NT_STATUS_IS_OK(status
)) {
139 if (!NT_STATUS_IS_OK(result
)) {
140 if (!NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
)) {
141 DEBUG(2,("query_user_list: failed to enum domain groups: %s\n",
147 info
= talloc_realloc(mem_ctx
,
152 return NT_STATUS_NO_MEMORY
;
155 for (g
= 0; g
< count
; g
++) {
156 struct wb_acct_info
*i
= &info
[num_info
+ g
];
158 i
->acct_name
= talloc_strdup(info
,
159 sam_array
->entries
[g
].name
.string
);
160 if (i
->acct_name
== NULL
) {
162 return NT_STATUS_NO_MEMORY
;
165 i
->rid
= sam_array
->entries
[g
].idx
;
169 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
171 *pnum_info
= num_info
;
177 NTSTATUS
rpc_enum_local_groups(TALLOC_CTX
*mem_ctx
,
178 struct rpc_pipe_client
*samr_pipe
,
179 struct policy_handle
*samr_policy
,
181 struct wb_acct_info
**pinfo
)
183 struct wb_acct_info
*info
= NULL
;
184 uint32_t num_info
= 0;
185 NTSTATUS status
, result
;
186 struct dcerpc_binding_handle
*b
= samr_pipe
->binding_handle
;
191 struct samr_SamArray
*sam_array
= NULL
;
193 uint32_t start
= num_info
;
196 status
= dcerpc_samr_EnumDomainAliases(b
,
201 0xFFFF, /* buffer size? */
204 if (!NT_STATUS_IS_OK(status
)) {
207 if (!NT_STATUS_IS_OK(result
)) {
208 if (!NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
)) {
213 info
= talloc_realloc(mem_ctx
,
218 return NT_STATUS_NO_MEMORY
;
221 for (g
= 0; g
< count
; g
++) {
222 struct wb_acct_info
*i
= &info
[num_info
+ g
];
224 i
->acct_name
= talloc_strdup(info
,
225 sam_array
->entries
[g
].name
.string
);
226 if (i
->acct_name
== NULL
) {
228 return NT_STATUS_NO_MEMORY
;
231 i
->rid
= sam_array
->entries
[g
].idx
;
235 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
237 *pnum_info
= num_info
;
243 /* convert a single name to a sid in a domain */
244 NTSTATUS
rpc_name_to_sid(TALLOC_CTX
*mem_ctx
,
245 struct rpc_pipe_client
*lsa_pipe
,
246 struct policy_handle
*lsa_policy
,
247 const char *domain_name
,
250 const char **pdom_name
,
252 enum lsa_SidType
*type
)
254 enum lsa_SidType
*types
= NULL
;
255 struct dom_sid
*sids
= NULL
;
256 char *full_name
= NULL
;
257 const char *names
[1];
258 const char **domains
;
259 char *mapped_name
= NULL
;
262 if (name
== NULL
|| name
[0] == '\0') {
263 full_name
= talloc_asprintf(mem_ctx
, "%s", domain_name
);
264 } else if (domain_name
== NULL
|| domain_name
[0] == '\0') {
265 full_name
= talloc_asprintf(mem_ctx
, "%s", name
);
267 full_name
= talloc_asprintf(mem_ctx
, "%s\\%s", domain_name
, name
);
270 if (full_name
== NULL
) {
271 return NT_STATUS_NO_MEMORY
;
274 status
= normalize_name_unmap(mem_ctx
, full_name
, &mapped_name
);
275 /* Reset the full_name pointer if we mapped anything */
276 if (NT_STATUS_IS_OK(status
) ||
277 NT_STATUS_EQUAL(status
, NT_STATUS_FILE_RENAMED
)) {
278 full_name
= mapped_name
;
281 DEBUG(3,("name_to_sid: %s for domain %s\n",
282 full_name
? full_name
: "", domain_name
));
284 names
[0] = full_name
;
287 * We don't run into deadlocks here, cause winbind_off() is
288 * called in the main function.
290 status
= rpccli_lsa_lookup_names(lsa_pipe
,
299 if (!NT_STATUS_IS_OK(status
)) {
300 DEBUG(2,("name_to_sid: failed to lookup name: %s\n",
305 if (pdom_name
!= NULL
) {
306 const char *dom_name
;
308 dom_name
= talloc_strdup(mem_ctx
, domains
[0]);
309 if (dom_name
== NULL
) {
310 return NT_STATUS_NO_MEMORY
;
313 *pdom_name
= dom_name
;
316 sid_copy(sid
, &sids
[0]);
322 /* Convert a domain SID to a user or group name */
323 NTSTATUS
rpc_sid_to_name(TALLOC_CTX
*mem_ctx
,
324 struct rpc_pipe_client
*lsa_pipe
,
325 struct policy_handle
*lsa_policy
,
326 struct winbindd_domain
*domain
,
327 const struct dom_sid
*sid
,
330 enum lsa_SidType
*ptype
)
332 char *mapped_name
= NULL
;
333 char **domains
= NULL
;
335 enum lsa_SidType
*types
= NULL
;
339 status
= rpccli_lsa_lookup_sids(lsa_pipe
,
347 if (!NT_STATUS_IS_OK(status
)) {
348 DEBUG(2,("sid_to_name: failed to lookup sids: %s\n",
353 *ptype
= (enum lsa_SidType
) types
[0];
355 map_status
= normalize_name_map(mem_ctx
,
359 if (NT_STATUS_IS_OK(map_status
) ||
360 NT_STATUS_EQUAL(map_status
, NT_STATUS_FILE_RENAMED
)) {
361 *pname
= talloc_strdup(mem_ctx
, mapped_name
);
362 DEBUG(5,("returning mapped name -- %s\n", *pname
));
364 *pname
= talloc_strdup(mem_ctx
, names
[0]);
366 if ((names
[0] != NULL
) && (*pname
== NULL
)) {
367 return NT_STATUS_NO_MEMORY
;
370 *pdomain_name
= talloc_strdup(mem_ctx
, domains
[0]);
371 if (*pdomain_name
== NULL
) {
372 return NT_STATUS_NO_MEMORY
;
378 /* Convert a bunch of rids to user or group names */
379 NTSTATUS
rpc_rids_to_names(TALLOC_CTX
*mem_ctx
,
380 struct rpc_pipe_client
*lsa_pipe
,
381 struct policy_handle
*lsa_policy
,
382 struct winbindd_domain
*domain
,
383 const struct dom_sid
*sid
,
388 enum lsa_SidType
**ptypes
)
390 enum lsa_SidType
*types
= NULL
;
391 char *domain_name
= NULL
;
392 char **domains
= NULL
;
394 struct dom_sid
*sids
;
399 sids
= talloc_array(mem_ctx
, struct dom_sid
, num_rids
);
401 return NT_STATUS_NO_MEMORY
;
407 for (i
= 0; i
< num_rids
; i
++) {
408 if (!sid_compose(&sids
[i
], sid
, rids
[i
])) {
409 return NT_STATUS_INTERNAL_ERROR
;
413 status
= rpccli_lsa_lookup_sids(lsa_pipe
,
421 if (!NT_STATUS_IS_OK(status
) &&
422 !NT_STATUS_EQUAL(status
, STATUS_SOME_UNMAPPED
)) {
423 DEBUG(2,("rids_to_names: failed to lookup sids: %s\n",
428 for (i
= 0; i
< num_rids
; i
++) {
429 char *mapped_name
= NULL
;
432 if (types
[i
] != SID_NAME_UNKNOWN
) {
433 map_status
= normalize_name_map(mem_ctx
,
437 if (NT_STATUS_IS_OK(map_status
) ||
438 NT_STATUS_EQUAL(map_status
, NT_STATUS_FILE_RENAMED
)) {
439 TALLOC_FREE(names
[i
]);
440 names
[i
] = talloc_strdup(names
, mapped_name
);
441 if (names
[i
] == NULL
) {
442 return NT_STATUS_NO_MEMORY
;
446 domain_name
= domains
[i
];
450 *pdomain_name
= domain_name
;
457 /* Lookup groups a user is a member of. */
458 NTSTATUS
rpc_lookup_usergroups(TALLOC_CTX
*mem_ctx
,
459 struct rpc_pipe_client
*samr_pipe
,
460 struct policy_handle
*samr_policy
,
461 const struct dom_sid
*domain_sid
,
462 const struct dom_sid
*user_sid
,
463 uint32_t *pnum_groups
,
464 struct dom_sid
**puser_grpsids
)
466 struct policy_handle user_policy
;
467 struct samr_RidWithAttributeArray
*rid_array
= NULL
;
468 struct dom_sid
*user_grpsids
= NULL
;
469 uint32_t num_groups
= 0, i
;
471 NTSTATUS status
, result
;
472 struct dcerpc_binding_handle
*b
= samr_pipe
->binding_handle
;
474 if (!sid_peek_check_rid(domain_sid
, user_sid
, &user_rid
)) {
475 return NT_STATUS_UNSUCCESSFUL
;
478 /* Get user handle */
479 status
= dcerpc_samr_OpenUser(b
,
482 SEC_FLAG_MAXIMUM_ALLOWED
,
486 if (!NT_STATUS_IS_OK(status
)) {
489 if (!NT_STATUS_IS_OK(result
)) {
493 /* Query user rids */
494 status
= dcerpc_samr_GetGroupsForUser(b
,
501 dcerpc_samr_Close(b
, mem_ctx
, &user_policy
, &_result
);
504 if (!NT_STATUS_IS_OK(status
)) {
507 if (!NT_STATUS_IS_OK(result
)) {
511 num_groups
= rid_array
->count
;
513 user_grpsids
= talloc_array(mem_ctx
, struct dom_sid
, num_groups
);
514 if (user_grpsids
== NULL
) {
515 status
= NT_STATUS_NO_MEMORY
;
519 for (i
= 0; i
< num_groups
; i
++) {
520 sid_compose(&(user_grpsids
[i
]), domain_sid
,
521 rid_array
->rids
[i
].rid
);
524 *pnum_groups
= num_groups
;
526 *puser_grpsids
= user_grpsids
;
531 NTSTATUS
rpc_lookup_useraliases(TALLOC_CTX
*mem_ctx
,
532 struct rpc_pipe_client
*samr_pipe
,
533 struct policy_handle
*samr_policy
,
535 const struct dom_sid
*sids
,
536 uint32_t *pnum_aliases
,
537 uint32_t **palias_rids
)
539 #define MAX_SAM_ENTRIES_W2K 0x400 /* 1024 */
540 uint32_t num_query_sids
= 0;
541 uint32_t num_queries
= 1;
542 uint32_t num_aliases
= 0;
543 uint32_t total_sids
= 0;
544 uint32_t *alias_rids
= NULL
;
545 uint32_t rangesize
= MAX_SAM_ENTRIES_W2K
;
547 struct samr_Ids alias_rids_query
;
548 NTSTATUS status
, result
;
549 struct dcerpc_binding_handle
*b
= samr_pipe
->binding_handle
;
553 struct lsa_SidArray sid_array
;
555 ZERO_STRUCT(sid_array
);
557 num_query_sids
= MIN(num_sids
- total_sids
, rangesize
);
559 DEBUG(10,("rpc: lookup_useraliases: entering query %d for %d sids\n",
560 num_queries
, num_query_sids
));
562 if (num_query_sids
) {
563 sid_array
.sids
= talloc_zero_array(mem_ctx
, struct lsa_SidPtr
, num_query_sids
);
564 if (sid_array
.sids
== NULL
) {
565 return NT_STATUS_NO_MEMORY
;
568 sid_array
.sids
= NULL
;
571 for (i
= 0; i
< num_query_sids
; i
++) {
572 sid_array
.sids
[i
].sid
= dom_sid_dup(mem_ctx
, &sids
[total_sids
++]);
573 if (sid_array
.sids
[i
].sid
== NULL
) {
574 return NT_STATUS_NO_MEMORY
;
577 sid_array
.num_sids
= num_query_sids
;
580 status
= dcerpc_samr_GetAliasMembership(b
,
586 if (!NT_STATUS_IS_OK(status
)) {
589 if (!NT_STATUS_IS_OK(result
)) {
594 for (i
= 0; i
< alias_rids_query
.count
; i
++) {
595 size_t na
= num_aliases
;
597 if (!add_rid_to_array_unique(mem_ctx
,
598 alias_rids_query
.ids
[i
],
601 return NT_STATUS_NO_MEMORY
;
608 } while (total_sids
< num_sids
);
610 DEBUG(10,("rpc: rpc_lookup_useraliases: got %d aliases in %d queries "
611 "(rangesize: %d)\n", num_aliases
, num_queries
, rangesize
));
613 *pnum_aliases
= num_aliases
;
614 *palias_rids
= alias_rids
;
617 #undef MAX_SAM_ENTRIES_W2K
620 /* Lookup group membership given a rid. */
621 NTSTATUS
rpc_lookup_groupmem(TALLOC_CTX
*mem_ctx
,
622 struct rpc_pipe_client
*samr_pipe
,
623 struct policy_handle
*samr_policy
,
624 const char *domain_name
,
625 const struct dom_sid
*domain_sid
,
626 const struct dom_sid
*group_sid
,
627 enum lsa_SidType type
,
628 uint32_t *pnum_names
,
629 struct dom_sid
**psid_mem
,
631 uint32_t **pname_types
)
633 struct policy_handle group_policy
;
635 uint32_t *rid_mem
= NULL
;
637 uint32_t num_names
= 0;
638 uint32_t total_names
= 0;
639 struct dom_sid
*sid_mem
= NULL
;
641 uint32_t *name_types
= NULL
;
643 struct lsa_Strings tmp_names
;
644 struct samr_Ids tmp_types
;
647 NTSTATUS status
, result
;
648 struct dcerpc_binding_handle
*b
= samr_pipe
->binding_handle
;
650 if (!sid_peek_check_rid(domain_sid
, group_sid
, &group_rid
)) {
651 return NT_STATUS_UNSUCCESSFUL
;
655 case SID_NAME_DOM_GRP
:
657 struct samr_RidAttrArray
*rids
= NULL
;
659 status
= dcerpc_samr_OpenGroup(b
,
662 SEC_FLAG_MAXIMUM_ALLOWED
,
666 if (!NT_STATUS_IS_OK(status
)) {
669 if (!NT_STATUS_IS_OK(result
)) {
674 * Step #1: Get a list of user rids that are the members of the group.
676 status
= dcerpc_samr_QueryGroupMember(b
,
683 dcerpc_samr_Close(b
, mem_ctx
, &group_policy
, &_result
);
686 if (!NT_STATUS_IS_OK(status
)) {
689 if (!NT_STATUS_IS_OK(result
)) {
694 if (rids
== NULL
|| rids
->count
== 0) {
703 num_names
= rids
->count
;
704 rid_mem
= rids
->rids
;
708 case SID_NAME_WKN_GRP
:
711 struct lsa_SidArray sid_array
;
712 struct lsa_SidPtr sid_ptr
;
713 struct samr_Ids rids_query
;
715 sid_ptr
.sid
= dom_sid_dup(mem_ctx
, group_sid
);
716 if (sid_ptr
.sid
== NULL
) {
717 return NT_STATUS_NO_MEMORY
;
720 sid_array
.num_sids
= 1;
721 sid_array
.sids
= &sid_ptr
;
723 status
= dcerpc_samr_GetAliasMembership(b
,
729 if (!NT_STATUS_IS_OK(status
)) {
732 if (!NT_STATUS_IS_OK(result
)) {
736 if (rids_query
.count
== 0) {
745 num_names
= rids_query
.count
;
746 rid_mem
= rids_query
.ids
;
751 return NT_STATUS_UNSUCCESSFUL
;
755 * Step #2: Convert list of rids into list of usernames.
758 names
= talloc_zero_array(mem_ctx
, char *, num_names
);
759 name_types
= talloc_zero_array(mem_ctx
, uint32_t, num_names
);
760 sid_mem
= talloc_zero_array(mem_ctx
, struct dom_sid
, num_names
);
761 if (names
== NULL
|| name_types
== NULL
|| sid_mem
== NULL
) {
762 return NT_STATUS_NO_MEMORY
;
766 for (j
= 0; j
< num_names
; j
++) {
767 sid_compose(&sid_mem
[j
], domain_sid
, rid_mem
[j
]);
770 status
= dcerpc_samr_LookupRids(b
,
778 if (!NT_STATUS_IS_OK(status
)) {
782 if (!NT_STATUS_IS_OK(result
)) {
783 if (!NT_STATUS_EQUAL(result
, STATUS_SOME_UNMAPPED
)) {
788 /* Copy result into array. The talloc system will take
789 care of freeing the temporary arrays later on. */
790 if (tmp_names
.count
!= num_names
) {
791 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
793 if (tmp_types
.count
!= num_names
) {
794 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
797 for (r
= 0; r
< tmp_names
.count
; r
++) {
798 if (tmp_types
.ids
[r
] == SID_NAME_UNKNOWN
) {
801 if (total_names
>= num_names
) {
804 names
[total_names
] = fill_domain_username_talloc(names
,
806 tmp_names
.names
[r
].string
,
808 if (names
[total_names
] == NULL
) {
809 return NT_STATUS_NO_MEMORY
;
811 name_types
[total_names
] = tmp_types
.ids
[r
];
815 *pnum_names
= total_names
;
817 *pname_types
= name_types
;
823 /* Find the sequence number for a domain */
824 NTSTATUS
rpc_sequence_number(TALLOC_CTX
*mem_ctx
,
825 struct rpc_pipe_client
*samr_pipe
,
826 struct policy_handle
*samr_policy
,
827 const char *domain_name
,
830 union samr_DomainInfo
*info
= NULL
;
831 bool got_seq_num
= false;
832 NTSTATUS status
, result
;
833 struct dcerpc_binding_handle
*b
= samr_pipe
->binding_handle
;
835 /* query domain info */
836 status
= dcerpc_samr_QueryDomainInfo(b
,
842 if (NT_STATUS_IS_OK(status
) && NT_STATUS_IS_OK(result
)) {
843 *pseq
= info
->info8
.sequence_num
;
848 /* retry with info-level 2 in case the dc does not support info-level 8
849 * (like all older samba2 and samba3 dc's) - Guenther */
850 status
= dcerpc_samr_QueryDomainInfo(b
,
856 if (NT_STATUS_IS_OK(status
) && NT_STATUS_IS_OK(result
)) {
857 *pseq
= info
->general
.sequence_num
;
862 if (!NT_STATUS_IS_OK(status
)) {
870 DEBUG(10,("domain_sequence_number: for domain %s is %u\n",
871 domain_name
, (unsigned) *pseq
));
873 DEBUG(10,("domain_sequence_number: failed to get sequence "
874 "number (%u) for domain %s\n",
875 (unsigned) *pseq
, domain_name
));
876 status
= NT_STATUS_OK
;
882 /* Get a list of trusted domains */
883 NTSTATUS
rpc_trusted_domains(TALLOC_CTX
*mem_ctx
,
884 struct rpc_pipe_client
*lsa_pipe
,
885 struct policy_handle
*lsa_policy
,
886 uint32_t *pnum_trusts
,
887 struct netr_DomainTrust
**ptrusts
)
889 struct netr_DomainTrust
*array
= NULL
;
890 uint32_t enum_ctx
= 0;
892 NTSTATUS status
, result
;
893 struct dcerpc_binding_handle
*b
= lsa_pipe
->binding_handle
;
896 struct lsa_DomainList dom_list
;
897 struct lsa_DomainListEx dom_list_ex
;
902 * We don't run into deadlocks here, cause winbind_off() is
903 * called in the main function.
905 status
= dcerpc_lsa_EnumTrustedDomainsEx(b
,
912 if (NT_STATUS_IS_OK(status
) && !NT_STATUS_IS_ERR(result
) &&
913 dom_list_ex
.count
> 0) {
914 count
+= dom_list_ex
.count
;
917 status
= dcerpc_lsa_EnumTrustDom(b
,
924 if (!NT_STATUS_IS_OK(status
)) {
927 if (!NT_STATUS_IS_OK(result
)) {
928 if (!NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
)) {
933 count
+= dom_list
.count
;
936 array
= talloc_realloc(mem_ctx
,
938 struct netr_DomainTrust
,
941 return NT_STATUS_NO_MEMORY
;
944 for (i
= 0; i
< count
; i
++) {
945 struct netr_DomainTrust
*trust
= &array
[i
];
950 sid
= talloc(array
, struct dom_sid
);
952 return NT_STATUS_NO_MEMORY
;
955 if (dom_list_ex
.domains
[i
].sid
== NULL
) {
956 DBG_ERR("Trusted domain %s has no SID, "
963 trust
->netbios_name
= talloc_move(array
,
964 &dom_list_ex
.domains
[i
].netbios_name
.string
);
965 trust
->dns_name
= talloc_move(array
,
966 &dom_list_ex
.domains
[i
].domain_name
.string
);
967 sid_copy(sid
, dom_list_ex
.domains
[i
].sid
);
969 trust
->netbios_name
= talloc_move(array
,
970 &dom_list
.domains
[i
].name
.string
);
971 trust
->dns_name
= NULL
;
973 sid_copy(sid
, dom_list
.domains
[i
].sid
);
978 } while (NT_STATUS_EQUAL(result
, STATUS_MORE_ENTRIES
));
980 *pnum_trusts
= count
;
986 static NTSTATUS
rpc_try_lookup_sids3(TALLOC_CTX
*mem_ctx
,
987 struct winbindd_domain
*domain
,
988 struct rpc_pipe_client
*cli
,
989 struct lsa_SidArray
*sids
,
990 struct lsa_RefDomainList
**pdomains
,
991 struct lsa_TransNameArray
**pnames
)
993 struct lsa_TransNameArray2 lsa_names2
;
994 struct lsa_TransNameArray
*names
= *pnames
;
995 uint32_t i
, count
= 0;
996 NTSTATUS status
, result
;
998 ZERO_STRUCT(lsa_names2
);
999 status
= dcerpc_lsa_LookupSids3(cli
->binding_handle
,
1004 LSA_LOOKUP_NAMES_ALL
,
1006 LSA_LOOKUP_OPTION_SEARCH_ISOLATED_NAMES
,
1007 LSA_CLIENT_REVISION_2
,
1009 if (!NT_STATUS_IS_OK(status
)) {
1012 if (NT_STATUS_LOOKUP_ERR(result
)) {
1015 if (sids
->num_sids
!= lsa_names2
.count
) {
1016 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1019 names
->count
= lsa_names2
.count
;
1020 names
->names
= talloc_array(names
, struct lsa_TranslatedName
,
1022 if (names
->names
== NULL
) {
1023 return NT_STATUS_NO_MEMORY
;
1025 for (i
=0; i
<names
->count
; i
++) {
1026 names
->names
[i
].sid_type
= lsa_names2
.names
[i
].sid_type
;
1027 names
->names
[i
].name
.string
= talloc_move(
1028 names
->names
, &lsa_names2
.names
[i
].name
.string
);
1029 names
->names
[i
].sid_index
= lsa_names2
.names
[i
].sid_index
;
1031 if (names
->names
[i
].sid_index
== UINT32_MAX
) {
1034 if ((*pdomains
) == NULL
) {
1035 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1037 if (names
->names
[i
].sid_index
>= (*pdomains
)->count
) {
1038 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1041 return NT_STATUS_OK
;
1044 NTSTATUS
rpc_lookup_sids(TALLOC_CTX
*mem_ctx
,
1045 struct winbindd_domain
*domain
,
1046 struct lsa_SidArray
*sids
,
1047 struct lsa_RefDomainList
**pdomains
,
1048 struct lsa_TransNameArray
**pnames
)
1050 struct lsa_TransNameArray
*names
= *pnames
;
1051 struct rpc_pipe_client
*cli
= NULL
;
1052 struct policy_handle lsa_policy
;
1055 NTSTATUS status
, result
;
1057 status
= cm_connect_lsat(domain
, mem_ctx
, &cli
, &lsa_policy
);
1058 if (!NT_STATUS_IS_OK(status
)) {
1062 if (cli
->transport
->transport
== NCACN_IP_TCP
) {
1063 return rpc_try_lookup_sids3(mem_ctx
, domain
, cli
, sids
,
1067 status
= dcerpc_lsa_LookupSids(cli
->binding_handle
, mem_ctx
,
1068 &lsa_policy
, sids
, pdomains
,
1069 names
, LSA_LOOKUP_NAMES_ALL
,
1071 if (!NT_STATUS_IS_OK(status
)) {
1074 if (NT_STATUS_LOOKUP_ERR(result
)) {
1078 if (sids
->num_sids
!= names
->count
) {
1079 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1082 for (i
=0; i
< names
->count
; i
++) {
1083 if (names
->names
[i
].sid_index
== UINT32_MAX
) {
1086 if ((*pdomains
) == NULL
) {
1087 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1089 if (names
->names
[i
].sid_index
>= (*pdomains
)->count
) {
1090 return NT_STATUS_INVALID_NETWORK_RESPONSE
;
1094 return NT_STATUS_OK
;