winbind: Use dom_sid_str_buf
[nivanova/samba-autobuild/.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 #include "lsa.h"
27
28 struct wb_lookupsids_domain {
29         struct winbindd_domain *domain;
30
31         /*
32          * Array of sids to be passed into wbint_LookupSids. Preallocated with
33          * num_sids.
34          */
35         struct lsa_SidArray sids;
36
37         /*
38          * Indexes into wb_lookupsids_state->sids and thus
39          * wb_lookupsids_state->res_names. Preallocated with num_sids.
40          */
41         uint32_t *sid_indexes;
42 };
43
44 struct wb_translated_name {
45         const char *domain_name;
46         const char *name;
47         enum lsa_SidType type;
48 };
49
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);
53
54 struct wb_lookupsids_state {
55         struct tevent_context *ev;
56
57         /*
58          * SIDs passed in
59          */
60         struct dom_sid *sids;
61         uint32_t num_sids;
62
63         /*
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.
67          */
68         struct wb_lookupsids_domain *domains;
69         uint32_t domains_done;
70
71         /*
72          * These SIDs are looked up individually via
73          * wbint_LookupSid. Preallocated with num_sids.
74          */
75         uint32_t *single_sids;
76         uint32_t num_single_sids;
77         uint32_t single_sids_done;
78
79         /*
80          * Intermediate store for wbint_LookupRids to passdb. These are
81          * spliced into res_domains/res_names in wb_lookupsids_move_name.
82          */
83         struct wbint_RidArray rids;
84         const char *domain_name;
85         struct wbint_Principals rid_names;
86
87         /*
88          * Intermediate results for wbint_LookupSids. These results are
89          * spliced into res_domains/res_names in wb_lookupsids_move_name.
90          */
91         struct lsa_RefDomainList tmp_domains;
92         struct lsa_TransNameArray tmp_names;
93
94         /*
95          * Results
96          */
97         struct lsa_RefDomainList *res_domains;
98         /*
99          * Indexed as "sids" in this structure
100          */
101         struct lsa_TransNameArray *res_names;
102 };
103
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);
109
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)
114 {
115         struct tevent_req *req;
116         struct wb_lookupsids_state *state;
117         uint32_t i;
118
119         req = tevent_req_create(mem_ctx, &state, struct wb_lookupsids_state);
120         if (req == NULL) {
121                 return NULL;
122         }
123         state->ev = ev;
124         state->sids = sids;
125         state->num_sids = num_sids;
126
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);
130         }
131
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);
135         }
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);
140         }
141
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);
145         }
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);
150         }
151
152         if (num_sids == 0) {
153                 tevent_req_done(req);
154                 return tevent_req_post(req, ev);
155         }
156
157         for (i=0; i<num_sids; i++) {
158                 struct wb_lookupsids_domain *d;
159
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;
169                 }
170         }
171
172         if (!wb_lookupsids_next(req, state)) {
173                 return tevent_req_post(req, ev);
174         }
175         return req;
176 }
177
178 static bool wb_lookupsids_next(struct tevent_req *req,
179                                struct wb_lookupsids_state *state)
180 {
181         struct tevent_req *subreq;
182
183         if (state->domains_done < talloc_array_length(state->domains)) {
184                 struct wb_lookupsids_domain *d;
185                 uint32_t i;
186
187                 d = &state->domains[state->domains_done];
188
189                 if (d->domain->internal) {
190                         /*
191                          * This is only our local SAM,
192                          * see wb_lookupsids_bulk() and
193                          * wb_lookupsids_get_domain().
194                          */
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;
200                         }
201                         for (i=0; i<state->rids.num_rids; i++) {
202                                 sid_peek_rid(d->sids.sids[i].sid,
203                                              &state->rids.rids[i]);
204                         }
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;
211                         }
212                         tevent_req_set_callback(
213                                 subreq, wb_lookupsids_lookuprids_done, req);
214                         return true;
215                 }
216
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;
222                 }
223                 tevent_req_set_callback(subreq, wb_lookupsids_done, req);
224                 return true;
225         }
226
227         if (state->single_sids_done < state->num_single_sids) {
228                 uint32_t sid_idx;
229                 const struct dom_sid *sid;
230
231                 sid_idx = state->single_sids[state->single_sids_done];
232                 sid = &state->sids[sid_idx];
233
234                 subreq = wb_lookupsid_send(state, state->ev, sid);
235                 if (tevent_req_nomem(subreq, req)) {
236                         return false;
237                 }
238                 tevent_req_set_callback(subreq, wb_lookupsids_single_done,
239                                         req);
240                 return true;
241         }
242
243         tevent_req_done(req);
244         return false;
245 }
246
247 /*
248  * Decide whether to do bulk lookupsids. We have optimizations for
249  * passdb via lookuprids and to remote DCs via lookupsids.
250  */
251
252 static bool wb_lookupsids_bulk(const struct dom_sid *sid)
253 {
254         struct dom_sid_buf sidbuf;
255
256         if (sid->num_auths != 5) {
257                 /*
258                  * Only do "S-1-5-21-x-y-z-rid" domains via bulk
259                  * lookup
260                  */
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;
265         }
266
267         if (sid_check_is_in_our_sam(sid)) {
268                 /*
269                  * Passdb lookup via lookuprids
270                  */
271                 DBG_DEBUG("%s is in our domain\n",
272                           dom_sid_str_buf(sid, &sidbuf));
273                 return true;
274         }
275
276         if (IS_DC) {
277                 /*
278                  * Bulk lookups to trusted DCs
279                  */
280                 return (find_domain_from_sid_noinit(sid) != NULL);
281         }
282
283         if (lp_server_role() != ROLE_DOMAIN_MEMBER) {
284                 /*
285                  * Don't do bulk lookups as standalone, the only bulk
286                  * lookup left is for domain members.
287                  */
288                 return false;
289         }
290
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))
299         {
300                 /*
301                  * These are locally done piece by piece anyway, no
302                  * need for bulk optimizations.
303                  */
304                 return false;
305         }
306
307         /*
308          * All other SIDs are sent to the DC we're connected to as
309          * member via a single lsa_lookupsids call.
310          */
311         return true;
312 }
313
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)
317 {
318         struct wb_lookupsids_domain *domains, *domain;
319         struct winbindd_domain *wb_domain;
320         uint32_t i, num_domains;
321
322         if (!wb_lookupsids_bulk(sid)) {
323                 return NULL;
324         }
325
326         domains = *pdomains;
327         num_domains = talloc_array_length(domains);
328
329         wb_domain = find_lookup_domain_from_sid(sid);
330         if (wb_domain == NULL) {
331                 return NULL;
332         }
333
334         for (i=0; i<num_domains; i++) {
335                 if (domains[i].domain != wb_domain) {
336                         continue;
337                 }
338
339                 if (!domains[i].domain->internal) {
340                         /*
341                          * If it's not our local sam,
342                          * we can re-use the domain without
343                          * checking the sid.
344                          *
345                          * Note the wb_lookupsids_bulk() above
346                          * already catched special SIDs,
347                          * e.g. the unix and builtin domains.
348                          */
349                         return &domains[i];
350                 }
351
352                 if (dom_sid_compare_domain(sid, &domains[i].domain->sid) == 0) {
353                         /*
354                          * If it's out local sam we can also use it.
355                          */
356                         return &domains[i];
357                 }
358
359                 /*
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.
364                  */
365                 return NULL;
366         }
367
368         domains = talloc_realloc(
369                 mem_ctx, domains, struct wb_lookupsids_domain, num_domains+1);
370         if (domains == NULL) {
371                 return NULL;
372         }
373         *pdomains = domains;
374
375         domain = &domains[num_domains];
376         domain->domain = wb_domain;
377
378         domain->sids.sids = talloc_zero_array(domains, struct lsa_SidPtr, num_sids);
379         if (domains->sids.sids == NULL) {
380                 goto fail;
381         }
382         domain->sids.num_sids = 0;
383
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;
388         }
389         return domain;
390
391 fail:
392         /*
393          * Realloc to the state it was in before
394          */
395         *pdomains = talloc_realloc(
396                 mem_ctx, domains, struct wb_lookupsids_domain, num_domains);
397         return NULL;
398 }
399
400 static bool wb_lookupsids_find_dom_idx(struct lsa_DomainInfo *domain,
401                                        struct lsa_RefDomainList *list,
402                                        uint32_t *idx)
403 {
404         uint32_t i;
405         struct lsa_DomainInfo *new_domain;
406
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;
411                 }
412         }
413
414         new_domain = &list->domains[list->count];
415
416         new_domain->name.string = talloc_strdup(
417                 list->domains, domain->name.string);
418         if (new_domain->name.string == NULL) {
419                 return false;
420         }
421
422         new_domain->sid = dom_sid_dup(list->domains, domain->sid);
423         if (new_domain->sid == NULL) {
424                 return false;
425         }
426
427         *idx = list->count;
428         list->count += 1;
429         return true;
430 }
431
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)
437 {
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;
443
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;
448                 }
449                 src_domain = &src_domains->domains[src_domain_index];
450
451                 ok = wb_lookupsids_find_dom_idx(src_domain,
452                                                 dst_domains,
453                                                 &dst_domain_index);
454                 if (!ok) {
455                         return false;
456                 }
457         }
458
459         dst_name = &dst_names->names[dst_name_index];
460
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;
466
467         return true;
468 }
469
470 static void wb_lookupsids_done(struct tevent_req *subreq)
471 {
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;
478
479         NTSTATUS status, result;
480
481         status = dcerpc_wbint_LookupSids_recv(subreq, state, &result);
482         TALLOC_FREE(subreq);
483         if (tevent_req_nterror(req, status)) {
484                 return;
485         }
486         if (NT_STATUS_LOOKUP_ERR(result)) {
487                 tevent_req_nterror(req, result);
488                 return;
489         }
490
491         /*
492          * Look at the individual states in the translated names.
493          */
494
495         d = &state->domains[state->domains_done];
496
497         for (i=0; i<state->tmp_names.count; i++) {
498                 uint32_t res_sid_index = d->sid_indexes[i];
499
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;
506                 }
507         }
508         state->domains_done += 1;
509         wb_lookupsids_next(req, state);
510 }
511
512 static void wb_lookupsids_single_done(struct tevent_req *subreq)
513 {
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;
523
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;
529
530         uint32_t domain_idx = UINT32_MAX;
531         NTSTATUS status;
532         bool ok;
533
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;
540         }
541
542         res_sid_index = state->single_sids[state->single_sids_done];
543
544         if ((domain_name != NULL) && (domain_name[0] != '\0')) {
545                 /*
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.
549                  */
550
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);
554                 }
555
556                 src_domain.name.string = domain_name;
557                 src_domain.sid = &src_domain_sid;
558
559                 src_domains.count = 1;
560                 src_domains.domains = &src_domain;
561                 psrc_domains = &src_domains;
562
563                 domain_idx = 0;
564         }
565
566         src_name.sid_type = type;
567         src_name.name.string = name;
568         src_name.sid_index = domain_idx;
569
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;
578         }
579         state->single_sids_done += 1;
580         wb_lookupsids_next(req, state);
581 }
582
583 static void wb_lookupsids_lookuprids_done(struct tevent_req *subreq)
584 {
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;
595
596         status = dcerpc_wbint_LookupRids_recv(subreq, state, &result);
597         TALLOC_FREE(subreq);
598         if (tevent_req_nterror(req, status)) {
599                 return;
600         }
601         if (NT_STATUS_LOOKUP_ERR(result)) {
602                 tevent_req_nterror(req, result);
603                 return;
604         }
605
606         /*
607          * Look at the individual states in the translated names.
608          */
609
610         d = &state->domains[state->domains_done];
611
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;
617
618         for (i=0; i<state->rid_names.num_principals; i++) {
619                 struct lsa_TranslatedName src_name;
620                 uint32_t res_sid_index;
621
622                 /*
623                  * Fake up structs for wb_lookupsids_move_name
624                  */
625                 res_sid_index = d->sid_indexes[i];
626
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;
630
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;
637                 }
638         }
639
640         state->domains_done += 1;
641         wb_lookupsids_next(req, state);
642 }
643
644 NTSTATUS wb_lookupsids_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
645                             struct lsa_RefDomainList **domains,
646                             struct lsa_TransNameArray **names)
647 {
648         struct wb_lookupsids_state *state = tevent_req_data(
649                 req, struct wb_lookupsids_state);
650         NTSTATUS status;
651
652         if (tevent_req_is_nterror(req, &status)) {
653                 return status;
654         }
655
656         /*
657          * The returned names need to match the given sids,
658          * if not we have a bug in the code!
659          *
660          */
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;
665         }
666
667         /*
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...
671          */
672         state->res_domains->domains = talloc_realloc(state->res_domains,
673                                                      state->res_domains->domains,
674                                                      struct lsa_DomainInfo,
675                                                      state->res_domains->count);
676
677         *domains = talloc_move(mem_ctx, &state->res_domains);
678         *names = talloc_move(mem_ctx, &state->res_names);
679         return NT_STATUS_OK;
680 }