2 Unix SMB/CIFS implementation.
4 Copyright (C) Volker Lendecke 2011
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "librpc/gen_ndr/ndr_winbind_c.h"
23 #include "../libcli/security/security.h"
24 #include "passdb/machine_sid.h"
26 struct wb_lookupsids_domain
{
28 struct winbindd_domain
*domain
;
31 * Array of sids to be passed into wbint_LookupSids. Preallocated with
34 struct lsa_SidArray sids
;
37 * Indexes into wb_lookupsids_state->sids and thus
38 * wb_lookupsids_state->res_names. Preallocated with num_sids.
40 uint32_t *sid_indexes
;
43 struct wb_translated_name
{
44 const char *domain_name
;
46 enum lsa_SidType type
;
49 static struct wb_lookupsids_domain
*wb_lookupsids_get_domain(
50 const struct dom_sid
*sid
, TALLOC_CTX
*mem_ctx
,
51 struct wb_lookupsids_domain
**domains
, uint32_t num_sids
);
53 struct wb_lookupsids_state
{
54 struct tevent_context
*ev
;
63 * The domains we're using for bulk lookup via wbint_LookupRids or
64 * wbint_LookupSids. We expect very few domains, so we do a
65 * talloc_realloc and rely on talloc_array_length.
67 struct wb_lookupsids_domain
*domains
;
68 uint32_t domains_done
;
71 * These SIDs are looked up individually via
72 * wbint_LookupSid. Preallocated with num_sids.
74 uint32_t *single_sids
;
75 /* Pointer into the "domains" array above*/
76 struct wb_lookupsids_domain
**single_domains
;
77 uint32_t num_single_sids
;
78 uint32_t single_sids_done
;
81 * Intermediate store for wbint_LookupRids to passdb. These are
82 * spliced into res_domains/res_names in wb_lookupsids_move_name.
84 struct wbint_RidArray rids
;
85 const char *domain_name
;
86 struct wbint_Principals rid_names
;
89 * Intermediate results for wbint_LookupSids. These results are
90 * spliced into res_domains/res_names in wb_lookupsids_move_name.
92 struct lsa_RefDomainList tmp_domains
;
93 struct lsa_TransNameArray tmp_names
;
98 struct lsa_RefDomainList
*res_domains
;
100 * Indexed as "sids" in this structure
102 struct lsa_TransNameArray
*res_names
;
105 static bool wb_lookupsids_next(struct tevent_req
*req
,
106 struct wb_lookupsids_state
*state
);
107 static void wb_lookupsids_single_done(struct tevent_req
*subreq
);
108 static void wb_lookupsids_lookuprids_done(struct tevent_req
*subreq
);
109 static void wb_lookupsids_done(struct tevent_req
*subreq
);
111 struct tevent_req
*wb_lookupsids_send(TALLOC_CTX
*mem_ctx
,
112 struct tevent_context
*ev
,
113 struct dom_sid
*sids
,
116 struct tevent_req
*req
;
117 struct wb_lookupsids_state
*state
;
120 req
= tevent_req_create(mem_ctx
, &state
, struct wb_lookupsids_state
);
126 state
->num_sids
= num_sids
;
128 state
->single_sids
= talloc_array(state
, uint32_t, num_sids
);
129 if (tevent_req_nomem(state
->single_sids
, req
)) {
130 return tevent_req_post(req
, ev
);
132 state
->single_domains
= talloc_zero_array(state
,
133 struct wb_lookupsids_domain
*,
135 if (tevent_req_nomem(state
->single_domains
, req
)) {
136 return tevent_req_post(req
, ev
);
139 state
->res_domains
= talloc_zero(state
, struct lsa_RefDomainList
);
140 if (tevent_req_nomem(state
->res_domains
, req
)) {
141 return tevent_req_post(req
, ev
);
143 state
->res_domains
->domains
= talloc_array(
144 state
->res_domains
, struct lsa_DomainInfo
, num_sids
);
145 if (tevent_req_nomem(state
->res_domains
->domains
, req
)) {
146 return tevent_req_post(req
, ev
);
149 state
->res_names
= talloc_zero(state
, struct lsa_TransNameArray
);
150 if (tevent_req_nomem(state
->res_names
, req
)) {
151 return tevent_req_post(req
, ev
);
153 state
->res_names
->names
= talloc_array(
154 state
->res_names
, struct lsa_TranslatedName
, num_sids
);
155 if (tevent_req_nomem(state
->res_names
->names
, req
)) {
156 return tevent_req_post(req
, ev
);
160 tevent_req_done(req
);
161 return tevent_req_post(req
, ev
);
164 for (i
=0; i
<num_sids
; i
++) {
165 struct wb_lookupsids_domain
*d
;
167 d
= wb_lookupsids_get_domain(&sids
[i
], state
, &state
->domains
,
170 d
->sids
.sids
[d
->sids
.num_sids
].sid
= &sids
[i
];
171 d
->sid_indexes
[d
->sids
.num_sids
] = i
;
172 d
->sids
.num_sids
+= 1;
174 state
->single_sids
[state
->num_single_sids
] = i
;
175 state
->num_single_sids
+= 1;
179 if (!wb_lookupsids_next(req
, state
)) {
180 return tevent_req_post(req
, ev
);
185 static bool wb_lookupsids_next(struct tevent_req
*req
,
186 struct wb_lookupsids_state
*state
)
188 struct tevent_req
*subreq
;
190 if (state
->domains_done
< talloc_array_length(state
->domains
)) {
191 struct wb_lookupsids_domain
*d
;
194 d
= &state
->domains
[state
->domains_done
];
196 if (sid_check_is_our_sam(&d
->sid
)) {
197 state
->rids
.num_rids
= d
->sids
.num_sids
;
198 state
->rids
.rids
= talloc_array(state
, uint32_t,
199 state
->rids
.num_rids
);
200 if (tevent_req_nomem(state
->rids
.rids
, req
)) {
203 for (i
=0; i
<state
->rids
.num_rids
; i
++) {
204 sid_peek_rid(d
->sids
.sids
[i
].sid
,
205 &state
->rids
.rids
[i
]);
207 subreq
= dcerpc_wbint_LookupRids_send(
208 state
, state
->ev
, dom_child_handle(d
->domain
),
209 &d
->sid
, &state
->rids
, &state
->domain_name
,
211 if (tevent_req_nomem(subreq
, req
)) {
214 tevent_req_set_callback(
215 subreq
, wb_lookupsids_lookuprids_done
, req
);
219 subreq
= dcerpc_wbint_LookupSids_send(
220 state
, state
->ev
, dom_child_handle(d
->domain
),
221 &d
->sids
, &state
->tmp_domains
, &state
->tmp_names
);
222 if (tevent_req_nomem(subreq
, req
)) {
225 tevent_req_set_callback(subreq
, wb_lookupsids_done
, req
);
229 if (state
->single_sids_done
< state
->num_single_sids
) {
231 const struct dom_sid
*sid
;
233 sid_idx
= state
->single_sids
[state
->single_sids_done
];
234 sid
= &state
->sids
[sid_idx
];
236 subreq
= wb_lookupsid_send(state
, state
->ev
, sid
);
237 if (tevent_req_nomem(subreq
, req
)) {
240 tevent_req_set_callback(subreq
, wb_lookupsids_single_done
,
245 tevent_req_done(req
);
250 * Decide whether to do bulk lookupsids. We have optimizations for
251 * passdb via lookuprids and to remote DCs via lookupsids.
254 static bool wb_lookupsids_bulk(const struct dom_sid
*sid
)
256 if (sid
->num_auths
!= 5) {
258 * Only do "S-1-5-21-x-y-z-rid" domains via bulk
261 DEBUG(10, ("No bulk setup for SID %s with %d subauths\n",
262 sid_string_dbg(sid
), sid
->num_auths
));
266 if (sid_check_is_in_our_sam(sid
)) {
268 * Passdb lookup via lookuprids
270 DEBUG(10, ("%s is in our domain\n", sid_string_tos(sid
)));
274 if ((lp_server_role() == ROLE_DOMAIN_PDC
) ||
275 (lp_server_role() == ROLE_DOMAIN_BDC
)) {
277 * Bulk lookups to trusted DCs
279 return (find_domain_from_sid_noinit(sid
) != NULL
);
282 if (lp_server_role() != ROLE_DOMAIN_MEMBER
) {
284 * Don't do bulk lookups as standalone, the only bulk
285 * lookup left is for domain members.
290 if (sid_check_is_in_unix_groups(sid
) ||
291 sid_check_is_unix_groups(sid
) ||
292 sid_check_is_in_unix_users(sid
) ||
293 sid_check_is_unix_users(sid
) ||
294 sid_check_is_in_builtin(sid
) ||
295 sid_check_is_builtin(sid
)) {
297 * These are locally done piece by piece anyway, no
298 * need for bulk optimizations.
304 * All other SIDs are sent to the DC we're connected to as
305 * member via a single lsa_lookupsids call.
310 static struct wb_lookupsids_domain
*wb_lookupsids_get_domain(
311 const struct dom_sid
*sid
, TALLOC_CTX
*mem_ctx
,
312 struct wb_lookupsids_domain
**pdomains
, uint32_t num_sids
)
314 struct wb_lookupsids_domain
*domains
, *domain
;
315 struct winbindd_domain
*wb_domain
;
316 uint32_t i
, num_domains
;
318 if (!wb_lookupsids_bulk(sid
)) {
323 num_domains
= talloc_array_length(domains
);
325 for (i
=0; i
<num_domains
; i
++) {
326 if (dom_sid_compare_domain(sid
, &domains
[i
].sid
) == 0) {
331 wb_domain
= find_lookup_domain_from_sid(sid
);
332 if (wb_domain
== NULL
) {
336 domains
= talloc_realloc(
337 mem_ctx
, domains
, struct wb_lookupsids_domain
, num_domains
+1);
338 if (domains
== NULL
) {
343 domain
= &domains
[num_domains
];
344 sid_copy(&domain
->sid
, sid
);
345 sid_split_rid(&domain
->sid
, NULL
);
346 domain
->domain
= wb_domain
;
348 domain
->sids
.sids
= talloc_array(domains
, struct lsa_SidPtr
, num_sids
);
349 if (domains
->sids
.sids
== NULL
) {
352 domain
->sids
.num_sids
= 0;
354 domain
->sid_indexes
= talloc_array(domains
, uint32_t, num_sids
);
355 if (domain
->sid_indexes
== NULL
) {
356 TALLOC_FREE(domain
->sids
.sids
);
363 * Realloc to the state it was in before
365 *pdomains
= talloc_realloc(
366 mem_ctx
, domains
, struct wb_lookupsids_domain
, num_domains
);
370 static bool wb_lookupsids_find_dom_idx(struct lsa_DomainInfo
*domain
,
371 struct lsa_RefDomainList
*list
,
375 struct lsa_DomainInfo
*new_domain
;
377 for (i
=0; i
<list
->count
; i
++) {
378 if (dom_sid_equal(domain
->sid
, list
->domains
[i
].sid
)) {
384 new_domain
= &list
->domains
[list
->count
];
386 new_domain
->name
.string
= talloc_strdup(
387 list
->domains
, domain
->name
.string
);
388 if (new_domain
->name
.string
== NULL
) {
392 new_domain
->sid
= dom_sid_dup(list
->domains
, domain
->sid
);
393 if (new_domain
->sid
== NULL
) {
402 static bool wb_lookupsids_move_name(struct lsa_RefDomainList
*src_domains
,
403 struct lsa_TranslatedName
*src_name
,
404 struct lsa_RefDomainList
*dst_domains
,
405 struct lsa_TransNameArray
*dst_names
,
406 uint32_t dst_name_index
)
408 struct lsa_TranslatedName
*dst_name
;
409 struct lsa_DomainInfo
*src_domain
;
410 uint32_t src_domain_index
, dst_domain_index
;
412 src_domain_index
= src_name
->sid_index
;
413 if (src_domain_index
>= src_domains
->count
) {
416 src_domain
= &src_domains
->domains
[src_domain_index
];
418 if (!wb_lookupsids_find_dom_idx(
419 src_domain
, dst_domains
, &dst_domain_index
)) {
423 dst_name
= &dst_names
->names
[dst_name_index
];
425 dst_name
->sid_type
= src_name
->sid_type
;
426 dst_name
->name
.string
= talloc_move(dst_names
->names
,
427 &src_name
->name
.string
);
428 dst_name
->sid_index
= dst_domain_index
;
429 dst_names
->count
+= 1;
434 static void wb_lookupsids_done(struct tevent_req
*subreq
)
436 struct tevent_req
*req
= tevent_req_callback_data(
437 subreq
, struct tevent_req
);
438 struct wb_lookupsids_state
*state
= tevent_req_data(
439 req
, struct wb_lookupsids_state
);
440 struct wb_lookupsids_domain
*d
;
442 bool fallback
= false;
444 NTSTATUS status
, result
;
446 status
= dcerpc_wbint_LookupSids_recv(subreq
, state
, &result
);
448 if (tevent_req_nterror(req
, status
)) {
452 d
= &state
->domains
[state
->domains_done
];
454 if (NT_STATUS_IS_ERR(result
)) {
456 } else if (state
->tmp_names
.count
!= d
->sids
.num_sids
) {
461 for (i
=0; i
< d
->sids
.num_sids
; i
++) {
462 uint32_t res_sid_index
= d
->sid_indexes
[i
];
464 state
->single_sids
[state
->num_single_sids
] =
466 state
->single_domains
[state
->num_single_sids
] = d
;
467 state
->num_single_sids
+= 1;
469 state
->domains_done
+= 1;
470 wb_lookupsids_next(req
, state
);
475 * Look at the individual states in the translated names.
478 for (i
=0; i
<state
->tmp_names
.count
; i
++) {
480 uint32_t res_sid_index
= d
->sid_indexes
[i
];
482 if (state
->tmp_names
.names
[i
].sid_type
== SID_NAME_UNKNOWN
) {
484 * Make unknown SIDs go through
485 * wb_lookupsid. This retries the forest root.
487 state
->single_sids
[state
->num_single_sids
] =
489 state
->num_single_sids
+= 1;
492 if (!wb_lookupsids_move_name(
493 &state
->tmp_domains
, &state
->tmp_names
.names
[i
],
494 state
->res_domains
, state
->res_names
,
500 state
->domains_done
+= 1;
501 wb_lookupsids_next(req
, state
);
504 static void wb_lookupsids_single_done(struct tevent_req
*subreq
)
506 struct tevent_req
*req
= tevent_req_callback_data(
507 subreq
, struct tevent_req
);
508 struct wb_lookupsids_state
*state
= tevent_req_data(
509 req
, struct wb_lookupsids_state
);
510 const char *domain_name
, *name
;
511 enum lsa_SidType type
;
512 uint32_t res_sid_index
;
515 struct dom_sid src_domain_sid
;
516 struct lsa_DomainInfo src_domain
;
517 struct lsa_RefDomainList src_domains
;
518 struct lsa_TranslatedName src_name
;
522 status
= wb_lookupsid_recv(subreq
, talloc_tos(), &type
,
523 &domain_name
, &name
);
525 if (!NT_STATUS_IS_OK(status
)) {
526 struct wb_lookupsids_domain
*wb_domain
;
529 type
= SID_NAME_UNKNOWN
;
531 wb_domain
= state
->single_domains
[state
->single_sids_done
];
532 if (wb_domain
!= NULL
) {
534 * If the lookupsid failed because the rid not
535 * found in a domain and we have a reference
536 * to the lookup domain, use the name from
539 * Callers like sid2xid will use the domain
540 * name in the idmap backend to figure out
541 * which domain to use in processing.
543 tmpname
= wb_domain
->domain
->name
;
547 domain_name
= talloc_strdup(talloc_tos(), tmpname
);
548 if (tevent_req_nomem(domain_name
, req
)) {
551 name
= talloc_strdup(talloc_tos(), "");
552 if (tevent_req_nomem(name
, req
)) {
558 * Fake up structs for wb_lookupsids_move_name
560 res_sid_index
= state
->single_sids
[state
->single_sids_done
];
562 sid_copy(&src_domain_sid
, &state
->sids
[res_sid_index
]);
563 sid_split_rid(&src_domain_sid
, &src_rid
);
564 src_domain
.name
.string
= domain_name
;
565 src_domain
.sid
= &src_domain_sid
;
567 src_domains
.count
= 1;
568 src_domains
.domains
= &src_domain
;
570 src_name
.sid_type
= type
;
571 src_name
.name
.string
= name
;
572 src_name
.sid_index
= 0;
574 if (!wb_lookupsids_move_name(
575 &src_domains
, &src_name
,
576 state
->res_domains
, state
->res_names
,
581 state
->single_sids_done
+= 1;
582 wb_lookupsids_next(req
, state
);
585 static void wb_lookupsids_lookuprids_done(struct tevent_req
*subreq
)
587 struct tevent_req
*req
= tevent_req_callback_data(
588 subreq
, struct tevent_req
);
589 struct wb_lookupsids_state
*state
= tevent_req_data(
590 req
, struct wb_lookupsids_state
);
591 struct dom_sid src_domain_sid
;
592 struct lsa_DomainInfo src_domain
;
593 struct lsa_RefDomainList src_domains
;
594 NTSTATUS status
, result
;
595 struct wb_lookupsids_domain
*d
;
597 bool fallback
= false;
599 status
= dcerpc_wbint_LookupRids_recv(subreq
, state
, &result
);
601 if (tevent_req_nterror(req
, status
)) {
605 d
= &state
->domains
[state
->domains_done
];
607 if (NT_STATUS_IS_ERR(result
)) {
609 } else if (state
->rid_names
.num_principals
!= d
->sids
.num_sids
) {
614 for (i
=0; i
< d
->sids
.num_sids
; i
++) {
615 uint32_t res_sid_index
= d
->sid_indexes
[i
];
617 state
->single_sids
[state
->num_single_sids
] =
619 state
->num_single_sids
+= 1;
621 state
->domains_done
+= 1;
622 wb_lookupsids_next(req
, state
);
627 * Look at the individual states in the translated names.
630 sid_copy(&src_domain_sid
, get_global_sam_sid());
631 src_domain
.name
.string
= get_global_sam_name();
632 src_domain
.sid
= &src_domain_sid
;
633 src_domains
.count
= 1;
634 src_domains
.domains
= &src_domain
;
636 for (i
=0; i
<state
->rid_names
.num_principals
; i
++) {
637 struct lsa_TranslatedName src_name
;
638 uint32_t res_sid_index
;
641 * Fake up structs for wb_lookupsids_move_name
643 res_sid_index
= d
->sid_indexes
[i
];
645 src_name
.sid_type
= state
->rid_names
.principals
[i
].type
;
646 src_name
.name
.string
= state
->rid_names
.principals
[i
].name
;
647 src_name
.sid_index
= 0;
649 if (!wb_lookupsids_move_name(
650 &src_domains
, &src_name
,
651 state
->res_domains
, state
->res_names
,
658 state
->domains_done
+= 1;
659 wb_lookupsids_next(req
, state
);
662 NTSTATUS
wb_lookupsids_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
663 struct lsa_RefDomainList
**domains
,
664 struct lsa_TransNameArray
**names
)
666 struct wb_lookupsids_state
*state
= tevent_req_data(
667 req
, struct wb_lookupsids_state
);
670 if (tevent_req_is_nterror(req
, &status
)) {
675 * The returned names need to match the given sids,
676 * if not we have a bug in the code!
679 if (state
->res_names
->count
!= state
->num_sids
) {
680 DEBUG(0, ("res_names->count = %d, expected %d\n",
681 state
->res_names
->count
, state
->num_sids
));
682 return NT_STATUS_INTERNAL_ERROR
;
686 * Not strictly needed, but it might make debugging in the callers
687 * easier in future, if the talloc_array_length() returns the
690 state
->res_domains
->domains
= talloc_realloc(state
->res_domains
,
691 state
->res_domains
->domains
,
692 struct lsa_DomainInfo
,
693 state
->res_domains
->count
);
695 *domains
= talloc_move(mem_ctx
, &state
->res_domains
);
696 *names
= talloc_move(mem_ctx
, &state
->res_names
);