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"
28 struct wb_lookupsids_domain
{
29 struct winbindd_domain
*domain
;
32 * Array of sids to be passed into wbint_LookupSids. Preallocated with
35 struct lsa_SidArray sids
;
38 * Indexes into wb_lookupsids_state->sids and thus
39 * wb_lookupsids_state->res_names. Preallocated with num_sids.
41 uint32_t *sid_indexes
;
44 struct wb_translated_name
{
45 const char *domain_name
;
47 enum lsa_SidType type
;
50 static struct wb_lookupsids_domain
*wb_lookupsids_get_domain(
51 const struct dom_sid
*sid
, TALLOC_CTX
*mem_ctx
,
52 struct wb_lookupsids_domain
**domains
, uint32_t num_sids
);
54 struct wb_lookupsids_state
{
55 struct tevent_context
*ev
;
64 * The domains we're using for bulk lookup via wbint_LookupRids or
65 * wbint_LookupSids. We expect very few domains, so we do a
66 * talloc_realloc and rely on talloc_array_length.
68 struct wb_lookupsids_domain
*domains
;
69 uint32_t domains_done
;
72 * These SIDs are looked up individually via
73 * wbint_LookupSid. Preallocated with num_sids.
75 uint32_t *single_sids
;
76 uint32_t num_single_sids
;
77 uint32_t single_sids_done
;
80 * Intermediate store for wbint_LookupRids to passdb. These are
81 * spliced into res_domains/res_names in wb_lookupsids_move_name.
83 struct wbint_RidArray rids
;
84 const char *domain_name
;
85 struct wbint_Principals rid_names
;
88 * Intermediate results for wbint_LookupSids. These results are
89 * spliced into res_domains/res_names in wb_lookupsids_move_name.
91 struct lsa_RefDomainList tmp_domains
;
92 struct lsa_TransNameArray tmp_names
;
97 struct lsa_RefDomainList
*res_domains
;
99 * Indexed as "sids" in this structure
101 struct lsa_TransNameArray
*res_names
;
104 static bool wb_lookupsids_next(struct tevent_req
*req
,
105 struct wb_lookupsids_state
*state
);
106 static void wb_lookupsids_single_done(struct tevent_req
*subreq
);
107 static void wb_lookupsids_lookuprids_done(struct tevent_req
*subreq
);
108 static void wb_lookupsids_done(struct tevent_req
*subreq
);
110 struct tevent_req
*wb_lookupsids_send(TALLOC_CTX
*mem_ctx
,
111 struct tevent_context
*ev
,
112 struct dom_sid
*sids
,
115 struct tevent_req
*req
;
116 struct wb_lookupsids_state
*state
;
119 req
= tevent_req_create(mem_ctx
, &state
, struct wb_lookupsids_state
);
125 state
->num_sids
= num_sids
;
127 state
->single_sids
= talloc_array(state
, uint32_t, num_sids
);
128 if (tevent_req_nomem(state
->single_sids
, req
)) {
129 return tevent_req_post(req
, ev
);
132 state
->res_domains
= talloc_zero(state
, struct lsa_RefDomainList
);
133 if (tevent_req_nomem(state
->res_domains
, req
)) {
134 return tevent_req_post(req
, ev
);
136 state
->res_domains
->domains
= talloc_array(
137 state
->res_domains
, struct lsa_DomainInfo
, num_sids
);
138 if (tevent_req_nomem(state
->res_domains
->domains
, req
)) {
139 return tevent_req_post(req
, ev
);
142 state
->res_names
= talloc_zero(state
, struct lsa_TransNameArray
);
143 if (tevent_req_nomem(state
->res_names
, req
)) {
144 return tevent_req_post(req
, ev
);
146 state
->res_names
->names
= talloc_array(
147 state
->res_names
, struct lsa_TranslatedName
, num_sids
);
148 if (tevent_req_nomem(state
->res_names
->names
, req
)) {
149 return tevent_req_post(req
, ev
);
153 tevent_req_done(req
);
154 return tevent_req_post(req
, ev
);
157 for (i
=0; i
<num_sids
; i
++) {
158 struct wb_lookupsids_domain
*d
;
160 d
= wb_lookupsids_get_domain(&sids
[i
], state
, &state
->domains
,
163 d
->sids
.sids
[d
->sids
.num_sids
].sid
= &sids
[i
];
164 d
->sid_indexes
[d
->sids
.num_sids
] = i
;
165 d
->sids
.num_sids
+= 1;
167 state
->single_sids
[state
->num_single_sids
] = i
;
168 state
->num_single_sids
+= 1;
172 if (!wb_lookupsids_next(req
, state
)) {
173 return tevent_req_post(req
, ev
);
178 static bool wb_lookupsids_next(struct tevent_req
*req
,
179 struct wb_lookupsids_state
*state
)
181 struct tevent_req
*subreq
;
183 if (state
->domains_done
< talloc_array_length(state
->domains
)) {
184 struct wb_lookupsids_domain
*d
;
187 d
= &state
->domains
[state
->domains_done
];
189 if (d
->domain
->internal
) {
191 * This is only our local SAM,
192 * see wb_lookupsids_bulk() and
193 * wb_lookupsids_get_domain().
195 state
->rids
.num_rids
= d
->sids
.num_sids
;
196 state
->rids
.rids
= talloc_array(state
, uint32_t,
197 state
->rids
.num_rids
);
198 if (tevent_req_nomem(state
->rids
.rids
, req
)) {
201 for (i
=0; i
<state
->rids
.num_rids
; i
++) {
202 sid_peek_rid(d
->sids
.sids
[i
].sid
,
203 &state
->rids
.rids
[i
]);
205 subreq
= dcerpc_wbint_LookupRids_send(
206 state
, state
->ev
, dom_child_handle(d
->domain
),
207 &d
->domain
->sid
, &state
->rids
, &state
->domain_name
,
209 if (tevent_req_nomem(subreq
, req
)) {
212 tevent_req_set_callback(
213 subreq
, wb_lookupsids_lookuprids_done
, req
);
217 subreq
= dcerpc_wbint_LookupSids_send(
218 state
, state
->ev
, dom_child_handle(d
->domain
),
219 &d
->sids
, &state
->tmp_domains
, &state
->tmp_names
);
220 if (tevent_req_nomem(subreq
, req
)) {
223 tevent_req_set_callback(subreq
, wb_lookupsids_done
, req
);
227 if (state
->single_sids_done
< state
->num_single_sids
) {
229 const struct dom_sid
*sid
;
231 sid_idx
= state
->single_sids
[state
->single_sids_done
];
232 sid
= &state
->sids
[sid_idx
];
234 subreq
= wb_lookupsid_send(state
, state
->ev
, sid
);
235 if (tevent_req_nomem(subreq
, req
)) {
238 tevent_req_set_callback(subreq
, wb_lookupsids_single_done
,
243 tevent_req_done(req
);
248 * Decide whether to do bulk lookupsids. We have optimizations for
249 * passdb via lookuprids and to remote DCs via lookupsids.
252 static bool wb_lookupsids_bulk(const struct dom_sid
*sid
)
254 if (sid
->num_auths
!= 5) {
256 * Only do "S-1-5-21-x-y-z-rid" domains via bulk
259 DEBUG(10, ("No bulk setup for SID %s with %d subauths\n",
260 sid_string_dbg(sid
), sid
->num_auths
));
264 if (sid_check_is_in_our_sam(sid
)) {
266 * Passdb lookup via lookuprids
268 DEBUG(10, ("%s is in our domain\n", sid_string_tos(sid
)));
274 * Bulk lookups to trusted DCs
276 return (find_domain_from_sid_noinit(sid
) != NULL
);
279 if (lp_server_role() != ROLE_DOMAIN_MEMBER
) {
281 * Don't do bulk lookups as standalone, the only bulk
282 * lookup left is for domain members.
287 if (sid_check_is_in_unix_groups(sid
) ||
288 sid_check_is_unix_groups(sid
) ||
289 sid_check_is_in_unix_users(sid
) ||
290 sid_check_is_unix_users(sid
) ||
291 sid_check_is_in_builtin(sid
) ||
292 sid_check_is_builtin(sid
) ||
293 sid_check_is_wellknown_domain(sid
, NULL
) ||
294 sid_check_is_in_wellknown_domain(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 wb_domain
= find_lookup_domain_from_sid(sid
);
326 if (wb_domain
== NULL
) {
330 for (i
=0; i
<num_domains
; i
++) {
331 if (domains
[i
].domain
!= wb_domain
) {
335 if (!domains
[i
].domain
->internal
) {
337 * If it's not our local sam,
338 * we can re-use the domain without
341 * Note the wb_lookupsids_bulk() above
342 * already catched special SIDs,
343 * e.g. the unix and builtin domains.
348 if (dom_sid_compare_domain(sid
, &domains
[i
].domain
->sid
) == 0) {
350 * If it's out local sam we can also use it.
356 * I'm not sure if this can be triggered,
357 * as wb_lookupsids_bulk() should also catch this,
358 * but we need to make sure that we don't use
359 * wbint_LookupRids() without a SID match.
364 domains
= talloc_realloc(
365 mem_ctx
, domains
, struct wb_lookupsids_domain
, num_domains
+1);
366 if (domains
== NULL
) {
371 domain
= &domains
[num_domains
];
372 domain
->domain
= wb_domain
;
374 domain
->sids
.sids
= talloc_array(domains
, struct lsa_SidPtr
, num_sids
);
375 if (domains
->sids
.sids
== NULL
) {
378 domain
->sids
.num_sids
= 0;
380 domain
->sid_indexes
= talloc_array(domains
, uint32_t, num_sids
);
381 if (domain
->sid_indexes
== NULL
) {
382 TALLOC_FREE(domain
->sids
.sids
);
389 * Realloc to the state it was in before
391 *pdomains
= talloc_realloc(
392 mem_ctx
, domains
, struct wb_lookupsids_domain
, num_domains
);
396 static bool wb_lookupsids_find_dom_idx(struct lsa_DomainInfo
*domain
,
397 struct lsa_RefDomainList
*list
,
401 struct lsa_DomainInfo
*new_domain
;
403 for (i
=0; i
<list
->count
; i
++) {
404 if (dom_sid_equal(domain
->sid
, list
->domains
[i
].sid
)) {
410 new_domain
= &list
->domains
[list
->count
];
412 new_domain
->name
.string
= talloc_strdup(
413 list
->domains
, domain
->name
.string
);
414 if (new_domain
->name
.string
== NULL
) {
418 new_domain
->sid
= dom_sid_dup(list
->domains
, domain
->sid
);
419 if (new_domain
->sid
== NULL
) {
428 static bool wb_lookupsids_move_name(struct lsa_RefDomainList
*src_domains
,
429 struct lsa_TranslatedName
*src_name
,
430 struct lsa_RefDomainList
*dst_domains
,
431 struct lsa_TransNameArray
*dst_names
,
432 uint32_t dst_name_index
)
434 struct lsa_TranslatedName
*dst_name
;
435 struct lsa_DomainInfo
*src_domain
;
436 uint32_t src_domain_index
;
437 uint32_t dst_domain_index
= UINT32_MAX
;
440 src_domain_index
= src_name
->sid_index
;
441 if ((src_domain_index
!= UINT32_MAX
) && (src_domains
!= NULL
)) {
442 if (src_domain_index
>= src_domains
->count
) {
445 src_domain
= &src_domains
->domains
[src_domain_index
];
447 ok
= wb_lookupsids_find_dom_idx(src_domain
,
455 dst_name
= &dst_names
->names
[dst_name_index
];
457 dst_name
->sid_type
= src_name
->sid_type
;
458 dst_name
->name
.string
= talloc_move(dst_names
->names
,
459 &src_name
->name
.string
);
460 dst_name
->sid_index
= dst_domain_index
;
461 dst_names
->count
+= 1;
466 static void wb_lookupsids_done(struct tevent_req
*subreq
)
468 struct tevent_req
*req
= tevent_req_callback_data(
469 subreq
, struct tevent_req
);
470 struct wb_lookupsids_state
*state
= tevent_req_data(
471 req
, struct wb_lookupsids_state
);
472 struct wb_lookupsids_domain
*d
;
475 NTSTATUS status
, result
;
477 status
= dcerpc_wbint_LookupSids_recv(subreq
, state
, &result
);
479 if (tevent_req_nterror(req
, status
)) {
482 if (NT_STATUS_LOOKUP_ERR(result
)) {
483 tevent_req_nterror(req
, result
);
488 * Look at the individual states in the translated names.
491 d
= &state
->domains
[state
->domains_done
];
493 for (i
=0; i
<state
->tmp_names
.count
; i
++) {
494 uint32_t res_sid_index
= d
->sid_indexes
[i
];
496 if (!wb_lookupsids_move_name(
497 &state
->tmp_domains
, &state
->tmp_names
.names
[i
],
498 state
->res_domains
, state
->res_names
,
504 state
->domains_done
+= 1;
505 wb_lookupsids_next(req
, state
);
508 static void wb_lookupsids_single_done(struct tevent_req
*subreq
)
510 struct tevent_req
*req
= tevent_req_callback_data(
511 subreq
, struct tevent_req
);
512 struct wb_lookupsids_state
*state
= tevent_req_data(
513 req
, struct wb_lookupsids_state
);
514 const char *domain_name
= NULL
;
515 const char *name
= NULL
;
516 enum lsa_SidType type
;
517 uint32_t res_sid_index
;
520 struct dom_sid src_domain_sid
;
521 struct lsa_DomainInfo src_domain
;
522 struct lsa_RefDomainList src_domains
;
523 struct lsa_RefDomainList
*psrc_domains
= NULL
;
524 struct lsa_TranslatedName src_name
;
526 uint32_t domain_idx
= UINT32_MAX
;
530 status
= wb_lookupsid_recv(subreq
, talloc_tos(), &type
,
531 &domain_name
, &name
);
533 if (NT_STATUS_LOOKUP_ERR(status
)) {
534 tevent_req_nterror(req
, status
);
538 res_sid_index
= state
->single_sids
[state
->single_sids_done
];
540 if ((domain_name
!= NULL
) && (domain_name
[0] != '\0')) {
542 * Build structs with the domain name for
543 * wb_lookupsids_move_name(). If we didn't get a name, we will
544 * pass NULL and UINT32_MAX.
547 sid_copy(&src_domain_sid
, &state
->sids
[res_sid_index
]);
548 sid_split_rid(&src_domain_sid
, &src_rid
);
550 src_domain
.name
.string
= domain_name
;
551 src_domain
.sid
= &src_domain_sid
;
553 src_domains
.count
= 1;
554 src_domains
.domains
= &src_domain
;
555 psrc_domains
= &src_domains
;
560 src_name
.sid_type
= type
;
561 src_name
.name
.string
= name
;
562 src_name
.sid_index
= domain_idx
;
564 ok
= wb_lookupsids_move_name(psrc_domains
,
573 state
->single_sids_done
+= 1;
574 wb_lookupsids_next(req
, state
);
577 static void wb_lookupsids_lookuprids_done(struct tevent_req
*subreq
)
579 struct tevent_req
*req
= tevent_req_callback_data(
580 subreq
, struct tevent_req
);
581 struct wb_lookupsids_state
*state
= tevent_req_data(
582 req
, struct wb_lookupsids_state
);
583 struct dom_sid src_domain_sid
;
584 struct lsa_DomainInfo src_domain
;
585 struct lsa_RefDomainList src_domains
;
586 NTSTATUS status
, result
;
587 struct wb_lookupsids_domain
*d
;
590 status
= dcerpc_wbint_LookupRids_recv(subreq
, state
, &result
);
592 if (tevent_req_nterror(req
, status
)) {
595 if (NT_STATUS_LOOKUP_ERR(result
)) {
596 tevent_req_nterror(req
, result
);
601 * Look at the individual states in the translated names.
604 d
= &state
->domains
[state
->domains_done
];
606 sid_copy(&src_domain_sid
, get_global_sam_sid());
607 src_domain
.name
.string
= get_global_sam_name();
608 src_domain
.sid
= &src_domain_sid
;
609 src_domains
.count
= 1;
610 src_domains
.domains
= &src_domain
;
612 for (i
=0; i
<state
->rid_names
.num_principals
; i
++) {
613 struct lsa_TranslatedName src_name
;
614 uint32_t res_sid_index
;
617 * Fake up structs for wb_lookupsids_move_name
619 res_sid_index
= d
->sid_indexes
[i
];
621 src_name
.sid_type
= state
->rid_names
.principals
[i
].type
;
622 src_name
.name
.string
= state
->rid_names
.principals
[i
].name
;
623 src_name
.sid_index
= 0;
625 if (!wb_lookupsids_move_name(
626 &src_domains
, &src_name
,
627 state
->res_domains
, state
->res_names
,
634 state
->domains_done
+= 1;
635 wb_lookupsids_next(req
, state
);
638 NTSTATUS
wb_lookupsids_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
639 struct lsa_RefDomainList
**domains
,
640 struct lsa_TransNameArray
**names
)
642 struct wb_lookupsids_state
*state
= tevent_req_data(
643 req
, struct wb_lookupsids_state
);
646 if (tevent_req_is_nterror(req
, &status
)) {
651 * The returned names need to match the given sids,
652 * if not we have a bug in the code!
655 if (state
->res_names
->count
!= state
->num_sids
) {
656 DEBUG(0, ("res_names->count = %d, expected %d\n",
657 state
->res_names
->count
, state
->num_sids
));
658 return NT_STATUS_INTERNAL_ERROR
;
662 * Not strictly needed, but it might make debugging in the callers
663 * easier in future, if the talloc_array_length() returns the
666 state
->res_domains
->domains
= talloc_realloc(state
->res_domains
,
667 state
->res_domains
->domains
,
668 struct lsa_DomainInfo
,
669 state
->res_domains
->count
);
671 *domains
= talloc_move(mem_ctx
, &state
->res_domains
);
672 *names
= talloc_move(mem_ctx
, &state
->res_names
);