dbwrap: add parse_record_send/recv to struct db_context
[Samba.git] / source3 / winbindd / wb_lookupsids.c
blobf2b2768bda681d0ecae01e5882b5433acd75671f
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_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);
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 if (sid->num_auths != 5) {
256 * Only do "S-1-5-21-x-y-z-rid" domains via bulk
257 * lookup
259 DEBUG(10, ("No bulk setup for SID %s with %d subauths\n",
260 sid_string_dbg(sid), sid->num_auths));
261 return false;
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)));
269 return true;
272 if (IS_DC) {
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.
284 return false;
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.
300 return false;
304 * All other SIDs are sent to the DC we're connected to as
305 * member via a single lsa_lookupsids call.
307 return true;
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)) {
319 return NULL;
322 domains = *pdomains;
323 num_domains = talloc_array_length(domains);
325 wb_domain = find_lookup_domain_from_sid(sid);
326 if (wb_domain == NULL) {
327 return NULL;
330 for (i=0; i<num_domains; i++) {
331 if (domains[i].domain != wb_domain) {
332 continue;
335 if (!domains[i].domain->internal) {
337 * If it's not our local sam,
338 * we can re-use the domain without
339 * checking the sid.
341 * Note the wb_lookupsids_bulk() above
342 * already catched special SIDs,
343 * e.g. the unix and builtin domains.
345 return &domains[i];
348 if (dom_sid_compare_domain(sid, &domains[i].domain->sid) == 0) {
350 * If it's out local sam we can also use it.
352 return &domains[i];
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.
361 return NULL;
364 domains = talloc_realloc(
365 mem_ctx, domains, struct wb_lookupsids_domain, num_domains+1);
366 if (domains == NULL) {
367 return NULL;
369 *pdomains = domains;
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) {
376 goto fail;
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);
383 goto fail;
385 return domain;
387 fail:
389 * Realloc to the state it was in before
391 *pdomains = talloc_realloc(
392 mem_ctx, domains, struct wb_lookupsids_domain, num_domains);
393 return NULL;
396 static bool wb_lookupsids_find_dom_idx(struct lsa_DomainInfo *domain,
397 struct lsa_RefDomainList *list,
398 uint32_t *idx)
400 uint32_t i;
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)) {
405 *idx = i;
406 return true;
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) {
415 return false;
418 new_domain->sid = dom_sid_dup(list->domains, domain->sid);
419 if (new_domain->sid == NULL) {
420 return false;
423 *idx = list->count;
424 list->count += 1;
425 return true;
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;
438 bool ok;
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) {
443 return false;
445 src_domain = &src_domains->domains[src_domain_index];
447 ok = wb_lookupsids_find_dom_idx(src_domain,
448 dst_domains,
449 &dst_domain_index);
450 if (!ok) {
451 return false;
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;
463 return true;
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;
473 uint32_t i;
475 NTSTATUS status, result;
477 status = dcerpc_wbint_LookupSids_recv(subreq, state, &result);
478 TALLOC_FREE(subreq);
479 if (tevent_req_nterror(req, status)) {
480 return;
482 if (NT_STATUS_LOOKUP_ERR(result)) {
483 tevent_req_nterror(req, result);
484 return;
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,
499 res_sid_index)) {
500 tevent_req_oom(req);
501 return;
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;
518 uint32_t src_rid;
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;
527 NTSTATUS status;
528 bool ok;
530 status = wb_lookupsid_recv(subreq, talloc_tos(), &type,
531 &domain_name, &name);
532 TALLOC_FREE(subreq);
533 if (NT_STATUS_LOOKUP_ERR(status)) {
534 tevent_req_nterror(req, status);
535 return;
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;
557 domain_idx = 0;
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,
565 &src_name,
566 state->res_domains,
567 state->res_names,
568 res_sid_index);
569 if (!ok) {
570 tevent_req_oom(req);
571 return;
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;
588 uint32_t i;
590 status = dcerpc_wbint_LookupRids_recv(subreq, state, &result);
591 TALLOC_FREE(subreq);
592 if (tevent_req_nterror(req, status)) {
593 return;
595 if (NT_STATUS_LOOKUP_ERR(result)) {
596 tevent_req_nterror(req, result);
597 return;
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,
628 res_sid_index)) {
629 tevent_req_oom(req);
630 return;
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);
644 NTSTATUS status;
646 if (tevent_req_is_nterror(req, &status)) {
647 return 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
664 * expected result...
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);
673 return NT_STATUS_OK;