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 "lib/util_unixsids.h"
23 #include "librpc/gen_ndr/ndr_winbind_c.h"
24 #include "../libcli/security/security.h"
25 #include "passdb/machine_sid.h"
27 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 (d->domain->internal) {
198 * This is only our local SAM,
199 * see wb_lookupsids_bulk() and
200 * wb_lookupsids_get_domain().
202 state->rids.num_rids = d->sids.num_sids;
203 state->rids.rids = talloc_array(state, uint32_t,
204 state->rids.num_rids);
205 if (tevent_req_nomem(state->rids.rids, req)) {
208 for (i=0; i<state->rids.num_rids; i++) {
209 sid_peek_rid(d->sids.sids[i].sid,
210 &state->rids.rids[i]);
212 subreq = dcerpc_wbint_LookupRids_send(
213 state, state->ev, dom_child_handle(d->domain),
214 &d->domain->sid, &state->rids, &state->domain_name,
216 if (tevent_req_nomem(subreq, req)) {
219 tevent_req_set_callback(
220 subreq, wb_lookupsids_lookuprids_done, req);
224 subreq = dcerpc_wbint_LookupSids_send(
225 state, state->ev, dom_child_handle(d->domain),
226 &d->sids, &state->tmp_domains, &state->tmp_names);
227 if (tevent_req_nomem(subreq, req)) {
230 tevent_req_set_callback(subreq, wb_lookupsids_done, req);
234 if (state->single_sids_done < state->num_single_sids) {
236 const struct dom_sid *sid;
238 sid_idx = state->single_sids[state->single_sids_done];
239 sid = &state->sids[sid_idx];
241 subreq = wb_lookupsid_send(state, state->ev, sid);
242 if (tevent_req_nomem(subreq, req)) {
245 tevent_req_set_callback(subreq, wb_lookupsids_single_done,
250 tevent_req_done(req);
255 * Decide whether to do bulk lookupsids. We have optimizations for
256 * passdb via lookuprids and to remote DCs via lookupsids.
259 static bool wb_lookupsids_bulk(const struct dom_sid *sid)
261 if (sid->num_auths != 5) {
263 * Only do "S-1-5-21-x-y-z-rid" domains via bulk
266 DEBUG(10, ("No bulk setup for SID %s with %d subauths\n",
267 sid_string_dbg(sid), sid->num_auths));
271 if (sid_check_is_in_our_sam(sid)) {
273 * Passdb lookup via lookuprids
275 DEBUG(10, ("%s is in our domain\n", sid_string_tos(sid)));
281 * Bulk lookups to trusted DCs
283 return (find_domain_from_sid_noinit(sid) != NULL);
286 if (lp_server_role() != ROLE_DOMAIN_MEMBER) {
288 * Don't do bulk lookups as standalone, the only bulk
289 * lookup left is for domain members.
294 if (sid_check_is_in_unix_groups(sid) ||
295 sid_check_is_unix_groups(sid) ||
296 sid_check_is_in_unix_users(sid) ||
297 sid_check_is_unix_users(sid) ||
298 sid_check_is_in_builtin(sid) ||
299 sid_check_is_builtin(sid) ||
300 sid_check_is_wellknown_domain(sid, NULL) ||
301 sid_check_is_in_wellknown_domain(sid))
304 * These are locally done piece by piece anyway, no
305 * need for bulk optimizations.
311 * All other SIDs are sent to the DC we're connected to as
312 * member via a single lsa_lookupsids call.
317 static struct wb_lookupsids_domain *wb_lookupsids_get_domain(
318 const struct dom_sid *sid, TALLOC_CTX *mem_ctx,
319 struct wb_lookupsids_domain **pdomains, uint32_t num_sids)
321 struct wb_lookupsids_domain *domains, *domain;
322 struct winbindd_domain *wb_domain;
323 uint32_t i, num_domains;
325 if (!wb_lookupsids_bulk(sid)) {
330 num_domains = talloc_array_length(domains);
332 wb_domain = find_lookup_domain_from_sid(sid);
333 if (wb_domain == NULL) {
337 for (i=0; i<num_domains; i++) {
338 if (domains[i].domain != wb_domain) {
342 if (!domains[i].domain->internal) {
344 * If it's not our local sam,
345 * we can re-use the domain without
348 * Note the wb_lookupsids_bulk() above
349 * already catched special SIDs,
350 * e.g. the unix and builtin domains.
355 if (dom_sid_compare_domain(sid, &domains[i].domain->sid) == 0) {
357 * If it's out local sam we can also use it.
363 * I'm not sure if this can be triggered,
364 * as wb_lookupsids_bulk() should also catch this,
365 * but we need to make sure that we don't use
366 * wbint_LookupRids() without a SID match.
371 domains = talloc_realloc(
372 mem_ctx, domains, struct wb_lookupsids_domain, num_domains+1);
373 if (domains == NULL) {
378 domain = &domains[num_domains];
379 domain->domain = wb_domain;
381 domain->sids.sids = talloc_array(domains, struct lsa_SidPtr, num_sids);
382 if (domains->sids.sids == NULL) {
385 domain->sids.num_sids = 0;
387 domain->sid_indexes = talloc_array(domains, uint32_t, num_sids);
388 if (domain->sid_indexes == NULL) {
389 TALLOC_FREE(domain->sids.sids);
396 * Realloc to the state it was in before
398 *pdomains = talloc_realloc(
399 mem_ctx, domains, struct wb_lookupsids_domain, num_domains);
403 static bool wb_lookupsids_find_dom_idx(struct lsa_DomainInfo *domain,
404 struct lsa_RefDomainList *list,
408 struct lsa_DomainInfo *new_domain;
410 for (i=0; i<list->count; i++) {
411 if (dom_sid_equal(domain->sid, list->domains[i].sid)) {
417 new_domain = &list->domains[list->count];
419 new_domain->name.string = talloc_strdup(
420 list->domains, domain->name.string);
421 if (new_domain->name.string == NULL) {
425 new_domain->sid = dom_sid_dup(list->domains, domain->sid);
426 if (new_domain->sid == NULL) {
435 static bool wb_lookupsids_move_name(struct lsa_RefDomainList *src_domains,
436 struct lsa_TranslatedName *src_name,
437 struct lsa_RefDomainList *dst_domains,
438 struct lsa_TransNameArray *dst_names,
439 uint32_t dst_name_index)
441 struct lsa_TranslatedName *dst_name;
442 struct lsa_DomainInfo *src_domain;
443 uint32_t src_domain_index, dst_domain_index;
445 src_domain_index = src_name->sid_index;
446 if (src_domain_index >= src_domains->count) {
449 src_domain = &src_domains->domains[src_domain_index];
451 if (!wb_lookupsids_find_dom_idx(
452 src_domain, dst_domains, &dst_domain_index)) {
456 dst_name = &dst_names->names[dst_name_index];
458 dst_name->sid_type = src_name->sid_type;
459 dst_name->name.string = talloc_move(dst_names->names,
460 &src_name->name.string);
461 dst_name->sid_index = dst_domain_index;
462 dst_names->count += 1;
467 static void wb_lookupsids_done(struct tevent_req *subreq)
469 struct tevent_req *req = tevent_req_callback_data(
470 subreq, struct tevent_req);
471 struct wb_lookupsids_state *state = tevent_req_data(
472 req, struct wb_lookupsids_state);
473 struct wb_lookupsids_domain *d;
475 bool fallback = false;
477 NTSTATUS status, result;
479 status = dcerpc_wbint_LookupSids_recv(subreq, state, &result);
481 if (tevent_req_nterror(req, status)) {
485 d = &state->domains[state->domains_done];
487 if (NT_STATUS_IS_ERR(result)) {
489 } else if (state->tmp_names.count != d->sids.num_sids) {
494 for (i=0; i < d->sids.num_sids; i++) {
495 uint32_t res_sid_index = d->sid_indexes[i];
497 state->single_sids[state->num_single_sids] =
499 state->single_domains[state->num_single_sids] = d;
500 state->num_single_sids += 1;
502 state->domains_done += 1;
503 wb_lookupsids_next(req, state);
508 * Look at the individual states in the translated names.
511 for (i=0; i<state->tmp_names.count; i++) {
513 uint32_t res_sid_index = d->sid_indexes[i];
515 if (state->tmp_names.names[i].sid_type == SID_NAME_UNKNOWN) {
517 * Make unknown SIDs go through
518 * wb_lookupsid. This retries the forest root.
520 state->single_sids[state->num_single_sids] =
522 state->num_single_sids += 1;
525 if (!wb_lookupsids_move_name(
526 &state->tmp_domains, &state->tmp_names.names[i],
527 state->res_domains, state->res_names,
533 state->domains_done += 1;
534 wb_lookupsids_next(req, state);
537 static void wb_lookupsids_single_done(struct tevent_req *subreq)
539 struct tevent_req *req = tevent_req_callback_data(
540 subreq, struct tevent_req);
541 struct wb_lookupsids_state *state = tevent_req_data(
542 req, struct wb_lookupsids_state);
543 const char *domain_name, *name;
544 enum lsa_SidType type;
545 uint32_t res_sid_index;
548 struct dom_sid src_domain_sid;
549 struct lsa_DomainInfo src_domain;
550 struct lsa_RefDomainList src_domains;
551 struct lsa_TranslatedName src_name;
555 status = wb_lookupsid_recv(subreq, talloc_tos(), &type,
556 &domain_name, &name);
558 if (!NT_STATUS_IS_OK(status)) {
559 struct wb_lookupsids_domain *wb_domain;
562 type = SID_NAME_UNKNOWN;
564 wb_domain = state->single_domains[state->single_sids_done];
565 if (wb_domain != NULL) {
567 * If the lookupsid failed because the rid not
568 * found in a domain and we have a reference
569 * to the lookup domain, use the name from
572 * Callers like sid2xid will use the domain
573 * name in the idmap backend to figure out
574 * which domain to use in processing.
576 tmpname = wb_domain->domain->name;
580 domain_name = talloc_strdup(talloc_tos(), tmpname);
581 if (tevent_req_nomem(domain_name, req)) {
584 name = talloc_strdup(talloc_tos(), "");
585 if (tevent_req_nomem(name, req)) {
591 * Fake up structs for wb_lookupsids_move_name
593 res_sid_index = state->single_sids[state->single_sids_done];
595 sid_copy(&src_domain_sid, &state->sids[res_sid_index]);
596 sid_split_rid(&src_domain_sid, &src_rid);
597 src_domain.name.string = domain_name;
598 src_domain.sid = &src_domain_sid;
600 src_domains.count = 1;
601 src_domains.domains = &src_domain;
603 src_name.sid_type = type;
604 src_name.name.string = name;
605 src_name.sid_index = 0;
607 if (!wb_lookupsids_move_name(
608 &src_domains, &src_name,
609 state->res_domains, state->res_names,
614 state->single_sids_done += 1;
615 wb_lookupsids_next(req, state);
618 static void wb_lookupsids_lookuprids_done(struct tevent_req *subreq)
620 struct tevent_req *req = tevent_req_callback_data(
621 subreq, struct tevent_req);
622 struct wb_lookupsids_state *state = tevent_req_data(
623 req, struct wb_lookupsids_state);
624 struct dom_sid src_domain_sid;
625 struct lsa_DomainInfo src_domain;
626 struct lsa_RefDomainList src_domains;
627 NTSTATUS status, result;
628 struct wb_lookupsids_domain *d;
630 bool fallback = false;
632 status = dcerpc_wbint_LookupRids_recv(subreq, state, &result);
634 if (tevent_req_nterror(req, status)) {
638 d = &state->domains[state->domains_done];
640 if (NT_STATUS_IS_ERR(result)) {
642 } else if (state->rid_names.num_principals != d->sids.num_sids) {
647 for (i=0; i < d->sids.num_sids; i++) {
648 uint32_t res_sid_index = d->sid_indexes[i];
650 state->single_sids[state->num_single_sids] =
652 state->num_single_sids += 1;
654 state->domains_done += 1;
655 wb_lookupsids_next(req, state);
660 * Look at the individual states in the translated names.
663 sid_copy(&src_domain_sid, get_global_sam_sid());
664 src_domain.name.string = get_global_sam_name();
665 src_domain.sid = &src_domain_sid;
666 src_domains.count = 1;
667 src_domains.domains = &src_domain;
669 for (i=0; i<state->rid_names.num_principals; i++) {
670 struct lsa_TranslatedName src_name;
671 uint32_t res_sid_index;
674 * Fake up structs for wb_lookupsids_move_name
676 res_sid_index = d->sid_indexes[i];
678 src_name.sid_type = state->rid_names.principals[i].type;
679 src_name.name.string = state->rid_names.principals[i].name;
680 src_name.sid_index = 0;
682 if (!wb_lookupsids_move_name(
683 &src_domains, &src_name,
684 state->res_domains, state->res_names,
691 state->domains_done += 1;
692 wb_lookupsids_next(req, state);
695 NTSTATUS wb_lookupsids_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
696 struct lsa_RefDomainList **domains,
697 struct lsa_TransNameArray **names)
699 struct wb_lookupsids_state *state = tevent_req_data(
700 req, struct wb_lookupsids_state);
703 if (tevent_req_is_nterror(req, &status)) {
708 * The returned names need to match the given sids,
709 * if not we have a bug in the code!
712 if (state->res_names->count != state->num_sids) {
713 DEBUG(0, ("res_names->count = %d, expected %d\n",
714 state->res_names->count, state->num_sids));
715 return NT_STATUS_INTERNAL_ERROR;
719 * Not strictly needed, but it might make debugging in the callers
720 * easier in future, if the talloc_array_length() returns the
723 state->res_domains->domains = talloc_realloc(state->res_domains,
724 state->res_domains->domains,
725 struct lsa_DomainInfo,
726 state->res_domains->count);
728 *domains = talloc_move(mem_ctx, &state->res_domains);
729 *names = talloc_move(mem_ctx, &state->res_names);