libcli:smb: Use smb2_signing_key in smb2_signing_encrypt_pdu()
[Samba.git] / source3 / winbindd / wb_lookupsids.c
blob5c73d3843ea1e397af23d5a49558df6273b07e7d
1 /*
2 Unix SMB/CIFS implementation.
3 async lookupsids
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/>.
20 #include "includes.h"
21 #include "winbindd.h"
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"
26 #include "lsa.h"
28 struct wb_lookupsids_domain {
29 struct winbindd_domain *domain;
32 * Array of sids to be passed into wbint_LookupSids. Preallocated with
33 * num_sids.
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;
46 const char *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;
58 * SIDs passed in
60 struct dom_sid *sids;
61 uint32_t num_sids;
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;
95 * Results
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,
113 uint32_t num_sids)
115 struct tevent_req *req;
116 struct wb_lookupsids_state *state;
117 uint32_t i;
119 req = tevent_req_create(mem_ctx, &state, struct wb_lookupsids_state);
120 if (req == NULL) {
121 return NULL;
123 state->ev = ev;
124 state->sids = sids;
125 state->num_sids = num_sids;
127 state->single_sids = talloc_zero_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_zero_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_zero_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);
152 if (num_sids == 0) {
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,
161 num_sids);
162 if (d != NULL) {
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;
166 } else {
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);
175 return req;
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;
185 uint32_t i;
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)) {
199 return false;
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,
208 &state->rid_names);
209 if (tevent_req_nomem(subreq, req)) {
210 return false;
212 tevent_req_set_callback(
213 subreq, wb_lookupsids_lookuprids_done, req);
214 return true;
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)) {
221 return false;
223 tevent_req_set_callback(subreq, wb_lookupsids_done, req);
224 return true;
227 if (state->single_sids_done < state->num_single_sids) {
228 uint32_t sid_idx;
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)) {
236 return false;
238 tevent_req_set_callback(subreq, wb_lookupsids_single_done,
239 req);
240 return true;
243 tevent_req_done(req);
244 return false;
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 struct dom_sid_buf sidbuf;
256 if (sid->num_auths != 5) {
258 * Only do "S-1-5-21-x-y-z-rid" domains via bulk
259 * lookup
261 DBG_DEBUG("No bulk setup for SID %s with %"PRIi8" subauths\n",
262 dom_sid_str_buf(sid, &sidbuf),
263 sid->num_auths);
264 return false;
267 if (sid_check_is_in_our_sam(sid)) {
269 * Passdb lookup via lookuprids
271 DBG_DEBUG("%s is in our domain\n",
272 dom_sid_str_buf(sid, &sidbuf));
273 return true;
276 if (IS_DC) {
278 * Bulk lookups to trusted DCs
280 return (find_domain_from_sid_noinit(sid) != NULL);
283 if (lp_server_role() != ROLE_DOMAIN_MEMBER) {
285 * Don't do bulk lookups as standalone, the only bulk
286 * lookup left is for domain members.
288 return false;
291 if (sid_check_is_in_unix_groups(sid) ||
292 sid_check_is_unix_groups(sid) ||
293 sid_check_is_in_unix_users(sid) ||
294 sid_check_is_unix_users(sid) ||
295 sid_check_is_in_builtin(sid) ||
296 sid_check_is_builtin(sid) ||
297 sid_check_is_wellknown_domain(sid, NULL) ||
298 sid_check_is_in_wellknown_domain(sid))
301 * These are locally done piece by piece anyway, no
302 * need for bulk optimizations.
304 return false;
308 * All other SIDs are sent to the DC we're connected to as
309 * member via a single lsa_lookupsids call.
311 return true;
314 static struct wb_lookupsids_domain *wb_lookupsids_get_domain(
315 const struct dom_sid *sid, TALLOC_CTX *mem_ctx,
316 struct wb_lookupsids_domain **pdomains, uint32_t num_sids)
318 struct wb_lookupsids_domain *domains, *domain;
319 struct winbindd_domain *wb_domain;
320 uint32_t i, num_domains;
322 if (!wb_lookupsids_bulk(sid)) {
323 return NULL;
326 domains = *pdomains;
327 num_domains = talloc_array_length(domains);
329 wb_domain = find_lookup_domain_from_sid(sid);
330 if (wb_domain == NULL) {
331 return NULL;
334 for (i=0; i<num_domains; i++) {
335 if (domains[i].domain != wb_domain) {
336 continue;
339 if (!domains[i].domain->internal) {
341 * If it's not our local sam,
342 * we can re-use the domain without
343 * checking the sid.
345 * Note the wb_lookupsids_bulk() above
346 * already catched special SIDs,
347 * e.g. the unix and builtin domains.
349 return &domains[i];
352 if (dom_sid_compare_domain(sid, &domains[i].domain->sid) == 0) {
354 * If it's out local sam we can also use it.
356 return &domains[i];
360 * I'm not sure if this can be triggered,
361 * as wb_lookupsids_bulk() should also catch this,
362 * but we need to make sure that we don't use
363 * wbint_LookupRids() without a SID match.
365 return NULL;
368 domains = talloc_realloc(
369 mem_ctx, domains, struct wb_lookupsids_domain, num_domains+1);
370 if (domains == NULL) {
371 return NULL;
373 *pdomains = domains;
375 domain = &domains[num_domains];
376 domain->domain = wb_domain;
378 domain->sids.sids = talloc_zero_array(domains, struct lsa_SidPtr, num_sids);
379 if (domains->sids.sids == NULL) {
380 goto fail;
382 domain->sids.num_sids = 0;
384 domain->sid_indexes = talloc_zero_array(domains, uint32_t, num_sids);
385 if (domain->sid_indexes == NULL) {
386 TALLOC_FREE(domain->sids.sids);
387 goto fail;
389 return domain;
391 fail:
393 * Realloc to the state it was in before
395 *pdomains = talloc_realloc(
396 mem_ctx, domains, struct wb_lookupsids_domain, num_domains);
397 return NULL;
400 static bool wb_lookupsids_find_dom_idx(struct lsa_DomainInfo *domain,
401 struct lsa_RefDomainList *list,
402 uint32_t *idx)
404 uint32_t i;
405 struct lsa_DomainInfo *new_domain;
407 for (i=0; i<list->count; i++) {
408 if (dom_sid_equal(domain->sid, list->domains[i].sid)) {
409 *idx = i;
410 return true;
414 new_domain = &list->domains[list->count];
416 new_domain->name.string = talloc_strdup(
417 list->domains, domain->name.string);
418 if (new_domain->name.string == NULL) {
419 return false;
422 new_domain->sid = dom_sid_dup(list->domains, domain->sid);
423 if (new_domain->sid == NULL) {
424 return false;
427 *idx = list->count;
428 list->count += 1;
429 return true;
432 static bool wb_lookupsids_move_name(struct lsa_RefDomainList *src_domains,
433 struct lsa_TranslatedName *src_name,
434 struct lsa_RefDomainList *dst_domains,
435 struct lsa_TransNameArray *dst_names,
436 uint32_t dst_name_index)
438 struct lsa_TranslatedName *dst_name;
439 struct lsa_DomainInfo *src_domain;
440 uint32_t src_domain_index;
441 uint32_t dst_domain_index = UINT32_MAX;
442 bool ok;
444 src_domain_index = src_name->sid_index;
445 if ((src_domain_index != UINT32_MAX) && (src_domains != NULL)) {
446 if (src_domain_index >= src_domains->count) {
447 return false;
449 src_domain = &src_domains->domains[src_domain_index];
451 ok = wb_lookupsids_find_dom_idx(src_domain,
452 dst_domains,
453 &dst_domain_index);
454 if (!ok) {
455 return false;
459 dst_name = &dst_names->names[dst_name_index];
461 dst_name->sid_type = src_name->sid_type;
462 dst_name->name.string = talloc_move(dst_names->names,
463 &src_name->name.string);
464 dst_name->sid_index = dst_domain_index;
465 dst_names->count += 1;
467 return true;
470 static void wb_lookupsids_done(struct tevent_req *subreq)
472 struct tevent_req *req = tevent_req_callback_data(
473 subreq, struct tevent_req);
474 struct wb_lookupsids_state *state = tevent_req_data(
475 req, struct wb_lookupsids_state);
476 struct wb_lookupsids_domain *d;
477 uint32_t i;
479 NTSTATUS status, result;
481 status = dcerpc_wbint_LookupSids_recv(subreq, state, &result);
482 TALLOC_FREE(subreq);
483 if (tevent_req_nterror(req, status)) {
484 return;
486 if (NT_STATUS_LOOKUP_ERR(result)) {
487 tevent_req_nterror(req, result);
488 return;
492 * Look at the individual states in the translated names.
495 d = &state->domains[state->domains_done];
497 for (i=0; i<state->tmp_names.count; i++) {
498 uint32_t res_sid_index = d->sid_indexes[i];
500 if (!wb_lookupsids_move_name(
501 &state->tmp_domains, &state->tmp_names.names[i],
502 state->res_domains, state->res_names,
503 res_sid_index)) {
504 tevent_req_oom(req);
505 return;
508 state->domains_done += 1;
509 wb_lookupsids_next(req, state);
512 static void wb_lookupsids_single_done(struct tevent_req *subreq)
514 struct tevent_req *req = tevent_req_callback_data(
515 subreq, struct tevent_req);
516 struct wb_lookupsids_state *state = tevent_req_data(
517 req, struct wb_lookupsids_state);
518 const char *domain_name = NULL;
519 const char *name = NULL;
520 enum lsa_SidType type = SID_NAME_UNKNOWN;
521 uint32_t res_sid_index;
522 uint32_t src_rid;
524 struct dom_sid src_domain_sid;
525 struct lsa_DomainInfo src_domain;
526 struct lsa_RefDomainList src_domains;
527 struct lsa_RefDomainList *psrc_domains = NULL;
528 struct lsa_TranslatedName src_name;
530 uint32_t domain_idx = UINT32_MAX;
531 NTSTATUS status;
532 bool ok;
534 status = wb_lookupsid_recv(subreq, talloc_tos(), &type,
535 &domain_name, &name);
536 TALLOC_FREE(subreq);
537 if (NT_STATUS_LOOKUP_ERR(status)) {
538 tevent_req_nterror(req, status);
539 return;
542 res_sid_index = state->single_sids[state->single_sids_done];
544 if ((domain_name != NULL) && (domain_name[0] != '\0')) {
546 * Build structs with the domain name for
547 * wb_lookupsids_move_name(). If we didn't get a name, we will
548 * pass NULL and UINT32_MAX.
551 sid_copy(&src_domain_sid, &state->sids[res_sid_index]);
552 if (type != SID_NAME_DOMAIN) {
553 sid_split_rid(&src_domain_sid, &src_rid);
556 src_domain.name.string = domain_name;
557 src_domain.sid = &src_domain_sid;
559 src_domains.count = 1;
560 src_domains.domains = &src_domain;
561 psrc_domains = &src_domains;
563 domain_idx = 0;
566 src_name.sid_type = type;
567 src_name.name.string = name;
568 src_name.sid_index = domain_idx;
570 ok = wb_lookupsids_move_name(psrc_domains,
571 &src_name,
572 state->res_domains,
573 state->res_names,
574 res_sid_index);
575 if (!ok) {
576 tevent_req_oom(req);
577 return;
579 state->single_sids_done += 1;
580 wb_lookupsids_next(req, state);
583 static void wb_lookupsids_lookuprids_done(struct tevent_req *subreq)
585 struct tevent_req *req = tevent_req_callback_data(
586 subreq, struct tevent_req);
587 struct wb_lookupsids_state *state = tevent_req_data(
588 req, struct wb_lookupsids_state);
589 struct dom_sid src_domain_sid;
590 struct lsa_DomainInfo src_domain;
591 struct lsa_RefDomainList src_domains;
592 NTSTATUS status, result;
593 struct wb_lookupsids_domain *d;
594 uint32_t i;
596 status = dcerpc_wbint_LookupRids_recv(subreq, state, &result);
597 TALLOC_FREE(subreq);
598 if (tevent_req_nterror(req, status)) {
599 return;
601 if (NT_STATUS_LOOKUP_ERR(result)) {
602 tevent_req_nterror(req, result);
603 return;
607 * Look at the individual states in the translated names.
610 d = &state->domains[state->domains_done];
612 sid_copy(&src_domain_sid, get_global_sam_sid());
613 src_domain.name.string = get_global_sam_name();
614 src_domain.sid = &src_domain_sid;
615 src_domains.count = 1;
616 src_domains.domains = &src_domain;
618 for (i=0; i<state->rid_names.num_principals; i++) {
619 struct lsa_TranslatedName src_name;
620 uint32_t res_sid_index;
623 * Fake up structs for wb_lookupsids_move_name
625 res_sid_index = d->sid_indexes[i];
627 src_name.sid_type = state->rid_names.principals[i].type;
628 src_name.name.string = state->rid_names.principals[i].name;
629 src_name.sid_index = 0;
631 if (!wb_lookupsids_move_name(
632 &src_domains, &src_name,
633 state->res_domains, state->res_names,
634 res_sid_index)) {
635 tevent_req_oom(req);
636 return;
640 state->domains_done += 1;
641 wb_lookupsids_next(req, state);
644 NTSTATUS wb_lookupsids_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
645 struct lsa_RefDomainList **domains,
646 struct lsa_TransNameArray **names)
648 struct wb_lookupsids_state *state = tevent_req_data(
649 req, struct wb_lookupsids_state);
650 NTSTATUS status;
652 if (tevent_req_is_nterror(req, &status)) {
653 return status;
657 * The returned names need to match the given sids,
658 * if not we have a bug in the code!
661 if (state->res_names->count != state->num_sids) {
662 DEBUG(0, ("res_names->count = %d, expected %d\n",
663 state->res_names->count, state->num_sids));
664 return NT_STATUS_INTERNAL_ERROR;
668 * Not strictly needed, but it might make debugging in the callers
669 * easier in future, if the talloc_array_length() returns the
670 * expected result...
672 state->res_domains->domains = talloc_realloc(state->res_domains,
673 state->res_domains->domains,
674 struct lsa_DomainInfo,
675 state->res_domains->count);
677 *domains = talloc_move(mem_ctx, &state->res_domains);
678 *names = talloc_move(mem_ctx, &state->res_names);
679 return NT_STATUS_OK;