858616be182f38e4ae7880ecb6d4628b30a7297f
[samba.git] / source3 / winbindd / wb_lookupsids.c
1 /*
2    Unix SMB/CIFS implementation.
3    async lookupsids
4    Copyright (C) Volker Lendecke 2011
5
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.
10
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.
15
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/>.
18 */
19
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
27 struct wb_lookupsids_domain {
28         struct winbindd_domain *domain;
29
30         /*
31          * Array of sids to be passed into wbint_LookupSids. Preallocated with
32          * num_sids.
33          */
34         struct lsa_SidArray sids;
35
36         /*
37          * Indexes into wb_lookupsids_state->sids and thus
38          * wb_lookupsids_state->res_names. Preallocated with num_sids.
39          */
40         uint32_t *sid_indexes;
41 };
42
43 struct wb_translated_name {
44         const char *domain_name;
45         const char *name;
46         enum lsa_SidType type;
47 };
48
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);
52
53 struct wb_lookupsids_state {
54         struct tevent_context *ev;
55
56         /*
57          * SIDs passed in
58          */
59         struct dom_sid *sids;
60         uint32_t num_sids;
61
62         /*
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.
66          */
67         struct wb_lookupsids_domain *domains;
68         uint32_t domains_done;
69
70         /*
71          * These SIDs are looked up individually via
72          * wbint_LookupSid. Preallocated with num_sids.
73          */
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;
79
80         /*
81          * Intermediate store for wbint_LookupRids to passdb. These are
82          * spliced into res_domains/res_names in wb_lookupsids_move_name.
83          */
84         struct wbint_RidArray rids;
85         const char *domain_name;
86         struct wbint_Principals rid_names;
87
88         /*
89          * Intermediate results for wbint_LookupSids. These results are
90          * spliced into res_domains/res_names in wb_lookupsids_move_name.
91          */
92         struct lsa_RefDomainList tmp_domains;
93         struct lsa_TransNameArray tmp_names;
94
95         /*
96          * Results
97          */
98         struct lsa_RefDomainList *res_domains;
99         /*
100          * Indexed as "sids" in this structure
101          */
102         struct lsa_TransNameArray *res_names;
103 };
104
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);
110
111 struct tevent_req *wb_lookupsids_send(TALLOC_CTX *mem_ctx,
112                                       struct tevent_context *ev,
113                                       struct dom_sid *sids,
114                                       uint32_t num_sids)
115 {
116         struct tevent_req *req;
117         struct wb_lookupsids_state *state;
118         uint32_t i;
119
120         req = tevent_req_create(mem_ctx, &state, struct wb_lookupsids_state);
121         if (req == NULL) {
122                 return NULL;
123         }
124         state->ev = ev;
125         state->sids = sids;
126         state->num_sids = num_sids;
127
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);
131         }
132         state->single_domains = talloc_zero_array(state,
133                                                   struct wb_lookupsids_domain *,
134                                                   num_sids);
135         if (tevent_req_nomem(state->single_domains, req)) {
136                 return tevent_req_post(req, ev);
137         }
138
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);
142         }
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);
147         }
148
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);
152         }
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);
157         }
158
159         if (num_sids == 0) {
160                 tevent_req_done(req);
161                 return tevent_req_post(req, ev);
162         }
163
164         for (i=0; i<num_sids; i++) {
165                 struct wb_lookupsids_domain *d;
166
167                 d = wb_lookupsids_get_domain(&sids[i], state, &state->domains,
168                                              num_sids);
169                 if (d != NULL) {
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;
173                 } else {
174                         state->single_sids[state->num_single_sids] = i;
175                         state->num_single_sids += 1;
176                 }
177         }
178
179         if (!wb_lookupsids_next(req, state)) {
180                 return tevent_req_post(req, ev);
181         }
182         return req;
183 }
184
185 static bool wb_lookupsids_next(struct tevent_req *req,
186                                struct wb_lookupsids_state *state)
187 {
188         struct tevent_req *subreq;
189
190         if (state->domains_done < talloc_array_length(state->domains)) {
191                 struct wb_lookupsids_domain *d;
192                 uint32_t i;
193
194                 d = &state->domains[state->domains_done];
195
196                 if (d->domain->internal) {
197                         /*
198                          * This is only our local SAM,
199                          * see wb_lookupsids_bulk() and
200                          * wb_lookupsids_get_domain().
201                          */
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)) {
206                                 return false;
207                         }
208                         for (i=0; i<state->rids.num_rids; i++) {
209                                 sid_peek_rid(d->sids.sids[i].sid,
210                                              &state->rids.rids[i]);
211                         }
212                         subreq = dcerpc_wbint_LookupRids_send(
213                                 state, state->ev, dom_child_handle(d->domain),
214                                 &d->domain->sid, &state->rids, &state->domain_name,
215                                 &state->rid_names);
216                         if (tevent_req_nomem(subreq, req)) {
217                                 return false;
218                         }
219                         tevent_req_set_callback(
220                                 subreq, wb_lookupsids_lookuprids_done, req);
221                         return true;
222                 }
223
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)) {
228                         return false;
229                 }
230                 tevent_req_set_callback(subreq, wb_lookupsids_done, req);
231                 return true;
232         }
233
234         if (state->single_sids_done < state->num_single_sids) {
235                 uint32_t sid_idx;
236                 const struct dom_sid *sid;
237
238                 sid_idx = state->single_sids[state->single_sids_done];
239                 sid = &state->sids[sid_idx];
240
241                 subreq = wb_lookupsid_send(state, state->ev, sid);
242                 if (tevent_req_nomem(subreq, req)) {
243                         return false;
244                 }
245                 tevent_req_set_callback(subreq, wb_lookupsids_single_done,
246                                         req);
247                 return true;
248         }
249
250         tevent_req_done(req);
251         return false;
252 }
253
254 /*
255  * Decide whether to do bulk lookupsids. We have optimizations for
256  * passdb via lookuprids and to remote DCs via lookupsids.
257  */
258
259 static bool wb_lookupsids_bulk(const struct dom_sid *sid)
260 {
261         if (sid->num_auths != 5) {
262                 /*
263                  * Only do "S-1-5-21-x-y-z-rid" domains via bulk
264                  * lookup
265                  */
266                 DEBUG(10, ("No bulk setup for SID %s with %d subauths\n",
267                            sid_string_dbg(sid), sid->num_auths));
268                 return false;
269         }
270
271         if (sid_check_is_in_our_sam(sid)) {
272                 /*
273                  * Passdb lookup via lookuprids
274                  */
275                 DEBUG(10, ("%s is in our domain\n", sid_string_tos(sid)));
276                 return true;
277         }
278
279         if (IS_DC) {
280                 /*
281                  * Bulk lookups to trusted DCs
282                  */
283                 return (find_domain_from_sid_noinit(sid) != NULL);
284         }
285
286         if (lp_server_role() != ROLE_DOMAIN_MEMBER) {
287                 /*
288                  * Don't do bulk lookups as standalone, the only bulk
289                  * lookup left is for domain members.
290                  */
291                 return false;
292         }
293
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))
302         {
303                 /*
304                  * These are locally done piece by piece anyway, no
305                  * need for bulk optimizations.
306                  */
307                 return false;
308         }
309
310         /*
311          * All other SIDs are sent to the DC we're connected to as
312          * member via a single lsa_lookupsids call.
313          */
314         return true;
315 }
316
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)
320 {
321         struct wb_lookupsids_domain *domains, *domain;
322         struct winbindd_domain *wb_domain;
323         uint32_t i, num_domains;
324
325         if (!wb_lookupsids_bulk(sid)) {
326                 return NULL;
327         }
328
329         domains = *pdomains;
330         num_domains = talloc_array_length(domains);
331
332         wb_domain = find_lookup_domain_from_sid(sid);
333         if (wb_domain == NULL) {
334                 return NULL;
335         }
336
337         for (i=0; i<num_domains; i++) {
338                 if (domains[i].domain != wb_domain) {
339                         continue;
340                 }
341
342                 if (!domains[i].domain->internal) {
343                         /*
344                          * If it's not our local sam,
345                          * we can re-use the domain without
346                          * checking the sid.
347                          *
348                          * Note the wb_lookupsids_bulk() above
349                          * already catched special SIDs,
350                          * e.g. the unix and builtin domains.
351                          */
352                         return &domains[i];
353                 }
354
355                 if (dom_sid_compare_domain(sid, &domains[i].domain->sid) == 0) {
356                         /*
357                          * If it's out local sam we can also use it.
358                          */
359                         return &domains[i];
360                 }
361
362                 /*
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.
367                  */
368                 return NULL;
369         }
370
371         domains = talloc_realloc(
372                 mem_ctx, domains, struct wb_lookupsids_domain, num_domains+1);
373         if (domains == NULL) {
374                 return NULL;
375         }
376         *pdomains = domains;
377
378         domain = &domains[num_domains];
379         domain->domain = wb_domain;
380
381         domain->sids.sids = talloc_array(domains, struct lsa_SidPtr, num_sids);
382         if (domains->sids.sids == NULL) {
383                 goto fail;
384         }
385         domain->sids.num_sids = 0;
386
387         domain->sid_indexes = talloc_array(domains, uint32_t, num_sids);
388         if (domain->sid_indexes == NULL) {
389                 TALLOC_FREE(domain->sids.sids);
390                 goto fail;
391         }
392         return domain;
393
394 fail:
395         /*
396          * Realloc to the state it was in before
397          */
398         *pdomains = talloc_realloc(
399                 mem_ctx, domains, struct wb_lookupsids_domain, num_domains);
400         return NULL;
401 }
402
403 static bool wb_lookupsids_find_dom_idx(struct lsa_DomainInfo *domain,
404                                        struct lsa_RefDomainList *list,
405                                        uint32_t *idx)
406 {
407         uint32_t i;
408         struct lsa_DomainInfo *new_domain;
409
410         for (i=0; i<list->count; i++) {
411                 if (dom_sid_equal(domain->sid, list->domains[i].sid)) {
412                         *idx = i;
413                         return true;
414                 }
415         }
416
417         new_domain = &list->domains[list->count];
418
419         new_domain->name.string = talloc_strdup(
420                 list->domains, domain->name.string);
421         if (new_domain->name.string == NULL) {
422                 return false;
423         }
424
425         new_domain->sid = dom_sid_dup(list->domains, domain->sid);
426         if (new_domain->sid == NULL) {
427                 return false;
428         }
429
430         *idx = list->count;
431         list->count += 1;
432         return true;
433 }
434
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)
440 {
441         struct lsa_TranslatedName *dst_name;
442         struct lsa_DomainInfo *src_domain;
443         uint32_t src_domain_index, dst_domain_index;
444
445         src_domain_index = src_name->sid_index;
446         if (src_domain_index >= src_domains->count) {
447                 return false;
448         }
449         src_domain = &src_domains->domains[src_domain_index];
450
451         if (!wb_lookupsids_find_dom_idx(
452                     src_domain, dst_domains, &dst_domain_index)) {
453                 return false;
454         }
455
456         dst_name = &dst_names->names[dst_name_index];
457
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;
463
464         return true;
465 }
466
467 static void wb_lookupsids_done(struct tevent_req *subreq)
468 {
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;
474         uint32_t i;
475         bool fallback = false;
476
477         NTSTATUS status, result;
478
479         status = dcerpc_wbint_LookupSids_recv(subreq, state, &result);
480         TALLOC_FREE(subreq);
481         if (tevent_req_nterror(req, status)) {
482                 return;
483         }
484
485         d = &state->domains[state->domains_done];
486
487         if (NT_STATUS_IS_ERR(result)) {
488                 fallback = true;
489         } else if (state->tmp_names.count != d->sids.num_sids) {
490                 fallback = true;
491         }
492
493         if (fallback) {
494                 for (i=0; i < d->sids.num_sids; i++) {
495                         uint32_t res_sid_index = d->sid_indexes[i];
496
497                         state->single_sids[state->num_single_sids] =
498                                 res_sid_index;
499                         state->single_domains[state->num_single_sids] = d;
500                         state->num_single_sids += 1;
501                 }
502                 state->domains_done += 1;
503                 wb_lookupsids_next(req, state);
504                 return;
505         }
506
507         /*
508          * Look at the individual states in the translated names.
509          */
510
511         for (i=0; i<state->tmp_names.count; i++) {
512
513                 uint32_t res_sid_index = d->sid_indexes[i];
514
515                 if (state->tmp_names.names[i].sid_type == SID_NAME_UNKNOWN) {
516                         /*
517                          * Make unknown SIDs go through
518                          * wb_lookupsid. This retries the forest root.
519                          */
520                         state->single_sids[state->num_single_sids] =
521                                 res_sid_index;
522                         state->num_single_sids += 1;
523                         continue;
524                 }
525                 if (!wb_lookupsids_move_name(
526                             &state->tmp_domains, &state->tmp_names.names[i],
527                             state->res_domains, state->res_names,
528                             res_sid_index)) {
529                         tevent_req_oom(req);
530                         return;
531                 }
532         }
533         state->domains_done += 1;
534         wb_lookupsids_next(req, state);
535 }
536
537 static void wb_lookupsids_single_done(struct tevent_req *subreq)
538 {
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;
546         uint32_t src_rid;
547
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;
552
553         NTSTATUS status;
554
555         status = wb_lookupsid_recv(subreq, talloc_tos(), &type,
556                                    &domain_name, &name);
557         TALLOC_FREE(subreq);
558         if (!NT_STATUS_IS_OK(status)) {
559                 struct wb_lookupsids_domain *wb_domain;
560                 const char *tmpname;
561
562                 type = SID_NAME_UNKNOWN;
563
564                 wb_domain = state->single_domains[state->single_sids_done];
565                 if (wb_domain != NULL) {
566                         /*
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
570                          * there.
571                          *
572                          * Callers like sid2xid will use the domain
573                          * name in the idmap backend to figure out
574                          * which domain to use in processing.
575                          */
576                         tmpname = wb_domain->domain->name;
577                 } else {
578                         tmpname = "";
579                 }
580                 domain_name = talloc_strdup(talloc_tos(), tmpname);
581                 if (tevent_req_nomem(domain_name, req)) {
582                         return;
583                 }
584                 name = talloc_strdup(talloc_tos(), "");
585                 if (tevent_req_nomem(name, req)) {
586                         return;
587                 }
588         }
589
590         /*
591          * Fake up structs for wb_lookupsids_move_name
592          */
593         res_sid_index = state->single_sids[state->single_sids_done];
594
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;
599
600         src_domains.count = 1;
601         src_domains.domains = &src_domain;
602
603         src_name.sid_type = type;
604         src_name.name.string = name;
605         src_name.sid_index = 0;
606
607         if (!wb_lookupsids_move_name(
608                     &src_domains, &src_name,
609                     state->res_domains, state->res_names,
610                     res_sid_index)) {
611                 tevent_req_oom(req);
612                 return;
613         }
614         state->single_sids_done += 1;
615         wb_lookupsids_next(req, state);
616 }
617
618 static void wb_lookupsids_lookuprids_done(struct tevent_req *subreq)
619 {
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;
629         uint32_t i;
630         bool fallback = false;
631
632         status = dcerpc_wbint_LookupRids_recv(subreq, state, &result);
633         TALLOC_FREE(subreq);
634         if (tevent_req_nterror(req, status)) {
635                 return;
636         }
637
638         d = &state->domains[state->domains_done];
639
640         if (NT_STATUS_IS_ERR(result)) {
641                 fallback = true;
642         } else if (state->rid_names.num_principals != d->sids.num_sids) {
643                 fallback = true;
644         }
645
646         if (fallback) {
647                 for (i=0; i < d->sids.num_sids; i++) {
648                         uint32_t res_sid_index = d->sid_indexes[i];
649
650                         state->single_sids[state->num_single_sids] =
651                                 res_sid_index;
652                         state->num_single_sids += 1;
653                 }
654                 state->domains_done += 1;
655                 wb_lookupsids_next(req, state);
656                 return;
657         }
658
659         /*
660          * Look at the individual states in the translated names.
661          */
662
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;
668
669         for (i=0; i<state->rid_names.num_principals; i++) {
670                 struct lsa_TranslatedName src_name;
671                 uint32_t res_sid_index;
672
673                 /*
674                  * Fake up structs for wb_lookupsids_move_name
675                  */
676                 res_sid_index = d->sid_indexes[i];
677
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;
681
682                 if (!wb_lookupsids_move_name(
683                             &src_domains, &src_name,
684                             state->res_domains, state->res_names,
685                             res_sid_index)) {
686                         tevent_req_oom(req);
687                         return;
688                 }
689         }
690
691         state->domains_done += 1;
692         wb_lookupsids_next(req, state);
693 }
694
695 NTSTATUS wb_lookupsids_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
696                             struct lsa_RefDomainList **domains,
697                             struct lsa_TransNameArray **names)
698 {
699         struct wb_lookupsids_state *state = tevent_req_data(
700                 req, struct wb_lookupsids_state);
701         NTSTATUS status;
702
703         if (tevent_req_is_nterror(req, &status)) {
704                 return status;
705         }
706
707         /*
708          * The returned names need to match the given sids,
709          * if not we have a bug in the code!
710          *
711          */
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;
716         }
717
718         /*
719          * Not strictly needed, but it might make debugging in the callers
720          * easier in future, if the talloc_array_length() returns the
721          * expected result...
722          */
723         state->res_domains->domains = talloc_realloc(state->res_domains,
724                                                      state->res_domains->domains,
725                                                      struct lsa_DomainInfo,
726                                                      state->res_domains->count);
727
728         *domains = talloc_move(mem_ctx, &state->res_domains);
729         *names = talloc_move(mem_ctx, &state->res_names);
730         return NT_STATUS_OK;
731 }