winbindd: only use the domain name from lookup sids if the domain matches
[samba.git] / source3 / winbindd / wb_xids2sids.c
1 /*
2  * Unix SMB/CIFS implementation.
3  * async xids2sids
4  * Copyright (C) Volker Lendecke 2015
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 "../libcli/security/security.h"
23 #include "idmap_cache.h"
24 #include "librpc/gen_ndr/ndr_winbind_c.h"
25 #include "librpc/gen_ndr/ndr_netlogon.h"
26
27 struct wb_xids2sids_dom_map {
28         unsigned low_id;
29         unsigned high_id;
30         const char *name;
31 };
32
33 /*
34  * Map idmap ranges to domain names, taken from smb.conf. This is
35  * stored in the parent winbind and used to assemble xid2sid calls
36  * into per-idmap-domain chunks.
37  */
38 static struct wb_xids2sids_dom_map *dom_maps;
39
40 static bool wb_xids2sids_add_dom(const char *domname,
41                                  void *private_data)
42 {
43         struct wb_xids2sids_dom_map *map = NULL;
44         size_t num_maps = talloc_array_length(dom_maps);
45         size_t i;
46         const char *range;
47         unsigned low_id, high_id;
48         int ret;
49
50         range = idmap_config_const_string(domname, "range", NULL);
51         if (range == NULL) {
52                 DBG_DEBUG("No range for domain %s found\n", domname);
53                 return false;
54         }
55
56         ret = sscanf(range, "%u - %u", &low_id, &high_id);
57         if (ret != 2) {
58                 DBG_DEBUG("Invalid range spec \"%s\" for domain %s\n",
59                           range, domname);
60                 return false;
61         }
62
63         if (low_id > high_id) {
64                 DBG_DEBUG("Invalid range %u - %u for domain %s\n",
65                           low_id, high_id, domname);
66                 return false;
67         }
68
69         for (i=0; i<num_maps; i++) {
70                 if (strequal(domname, dom_maps[i].name)) {
71                         map = &dom_maps[i];
72                         break;
73                 }
74         }
75
76         if (map == NULL) {
77                 struct wb_xids2sids_dom_map *tmp;
78                 char *name;
79
80                 name = talloc_strdup(talloc_tos(), domname);
81                 if (name == NULL) {
82                         DBG_DEBUG("talloc failed\n");
83                         return false;
84                 }
85
86                 tmp = talloc_realloc(
87                         NULL, dom_maps, struct wb_xids2sids_dom_map,
88                         num_maps+1);
89                 if (tmp == NULL) {
90                         TALLOC_FREE(name);
91                         return false;
92                 }
93                 dom_maps = tmp;
94
95                 map = &dom_maps[num_maps];
96                 map->name = talloc_move(dom_maps, &name);
97         }
98
99         map->low_id = low_id;
100         map->high_id = high_id;
101
102         return false;
103 }
104
105 static void wb_xids2sids_init_dom_maps(void)
106 {
107         if (dom_maps != NULL) {
108                 return;
109         }
110
111         /*
112          * Put the passdb idmap domain first. We always need to try
113          * there first.
114          */
115
116         dom_maps = talloc_array(NULL, struct wb_xids2sids_dom_map, 1);
117         if (dom_maps == NULL) {
118                 return;
119         }
120         dom_maps[0].low_id = 0;
121         dom_maps[0].high_id = UINT_MAX;
122         dom_maps[0].name = talloc_strdup(dom_maps, get_global_sam_name());
123         if (dom_maps[0].name == NULL) {
124                 TALLOC_FREE(dom_maps);
125                 return;
126         }
127
128         lp_scan_idmap_domains(wb_xids2sids_add_dom, NULL);
129 }
130
131 struct wb_xids2sids_dom_state {
132         struct tevent_context *ev;
133         struct unixid *all_xids;
134         size_t num_all_xids;
135         struct dom_sid *all_sids;
136         struct wb_xids2sids_dom_map *dom_map;
137         bool tried_dclookup;
138
139         size_t num_dom_xids;
140         struct unixid *dom_xids;
141         struct dom_sid *dom_sids;
142 };
143
144 static void wb_xids2sids_dom_done(struct tevent_req *subreq);
145 static void wb_xids2sids_dom_gotdc(struct tevent_req *subreq);
146
147 static struct tevent_req *wb_xids2sids_dom_send(
148         TALLOC_CTX *mem_ctx, struct tevent_context *ev,
149         struct wb_xids2sids_dom_map *dom_map,
150         struct unixid *xids, size_t num_xids, struct dom_sid *sids)
151 {
152         struct tevent_req *req, *subreq;
153         struct wb_xids2sids_dom_state *state;
154         struct winbindd_child *child;
155         size_t i;
156
157         req = tevent_req_create(mem_ctx, &state,
158                                 struct wb_xids2sids_dom_state);
159         if (req == NULL) {
160                 return NULL;
161         }
162         state->ev = ev;
163         state->all_xids = xids;
164         state->num_all_xids = num_xids;
165         state->all_sids = sids;
166         state->dom_map = dom_map;
167
168         state->dom_xids = talloc_array(state, struct unixid, num_xids);
169         if (tevent_req_nomem(state->dom_xids, req)) {
170                 return tevent_req_post(req, ev);
171         }
172         state->dom_sids = talloc_array(state, struct dom_sid, num_xids);
173         if (tevent_req_nomem(state->dom_sids, req)) {
174                 return tevent_req_post(req, ev);
175         }
176
177         for (i=0; i<num_xids; i++) {
178                 struct unixid id = state->all_xids[i];
179
180                 if ((id.id < dom_map->low_id) || (id.id > dom_map->high_id)) {
181                         /* out of range */
182                         continue;
183                 }
184                 if (!is_null_sid(&state->all_sids[i])) {
185                         /* already mapped */
186                         continue;
187                 }
188
189                 state->dom_xids[state->num_dom_xids++] = id;
190         }
191
192         if (state->num_dom_xids == 0) {
193                 tevent_req_done(req);
194                 return tevent_req_post(req, ev);
195         }
196
197         child = idmap_child();
198         subreq = dcerpc_wbint_UnixIDs2Sids_send(
199                 state, ev, child->binding_handle, dom_map->name,
200                 state->num_dom_xids, state->dom_xids, state->dom_sids);
201         if (tevent_req_nomem(subreq, req)) {
202                 return tevent_req_post(req, ev);
203         }
204         tevent_req_set_callback(subreq, wb_xids2sids_dom_done, req);
205         return req;
206 }
207
208 static void wb_xids2sids_dom_done(struct tevent_req *subreq)
209 {
210         struct tevent_req *req = tevent_req_callback_data(
211                 subreq, struct tevent_req);
212         struct wb_xids2sids_dom_state *state = tevent_req_data(
213                 req, struct wb_xids2sids_dom_state);
214         struct wb_xids2sids_dom_map *dom_map = state->dom_map;
215         NTSTATUS status, result;
216         size_t i;
217         size_t dom_sid_idx;
218
219         status = dcerpc_wbint_UnixIDs2Sids_recv(subreq, state, &result);
220         TALLOC_FREE(subreq);
221         if (tevent_req_nterror(req, status)) {
222                 return;
223         }
224
225         if (NT_STATUS_EQUAL(result, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND) &&
226             !state->tried_dclookup) {
227
228                 subreq = wb_dsgetdcname_send(
229                         state, state->ev, state->dom_map->name, NULL, NULL,
230                         DS_RETURN_DNS_NAME);
231                 if (tevent_req_nomem(subreq, req)) {
232                         return;
233                 }
234                 tevent_req_set_callback(subreq, wb_xids2sids_dom_gotdc, req);
235                 return;
236         }
237
238         if (!NT_STATUS_EQUAL(result, NT_STATUS_NONE_MAPPED) &&
239             tevent_req_nterror(req, result)) {
240                 return;
241         }
242
243         dom_sid_idx = 0;
244
245         for (i=0; i<state->num_all_xids; i++) {
246                 struct unixid id = state->all_xids[i];
247
248                 if ((id.id < dom_map->low_id) || (id.id > dom_map->high_id)) {
249                         /* out of range */
250                         continue;
251                 }
252                 if (!is_null_sid(&state->all_sids[i])) {
253                         /* already mapped */
254                         continue;
255                 }
256
257                 sid_copy(&state->all_sids[i], &state->dom_sids[dom_sid_idx]);
258
259                 /*
260                  * Prime the cache after an xid2sid call. It's
261                  * important that we use state->dom_xids for the xid
262                  * value, not state->all_xids: state->all_xids carries
263                  * what we asked for, e.g. a
264                  * ID_TYPE_UID. state->dom_xids holds something the
265                  * idmap child possibly changed to ID_TYPE_BOTH.
266                  */
267                 idmap_cache_set_sid2unixid(
268                         &state->all_sids[i], &state->dom_xids[dom_sid_idx]);
269
270                 dom_sid_idx += 1;
271         }
272
273         tevent_req_done(req);
274 }
275
276 static void wb_xids2sids_dom_gotdc(struct tevent_req *subreq)
277 {
278         struct tevent_req *req = tevent_req_callback_data(
279                 subreq, struct tevent_req);
280         struct wb_xids2sids_dom_state *state = tevent_req_data(
281                 req, struct wb_xids2sids_dom_state);
282         struct winbindd_child *child = idmap_child();
283         struct netr_DsRGetDCNameInfo *dcinfo;
284         NTSTATUS status;
285
286         status = wb_dsgetdcname_recv(subreq, state, &dcinfo);
287         TALLOC_FREE(subreq);
288         if (tevent_req_nterror(req, status)) {
289                 return;
290         }
291
292         state->tried_dclookup = true;
293
294         status = wb_dsgetdcname_gencache_set(state->dom_map->name, dcinfo);
295         if (tevent_req_nterror(req, status)) {
296                 return;
297         }
298
299         child = idmap_child();
300         subreq = dcerpc_wbint_UnixIDs2Sids_send(
301                 state, state->ev, child->binding_handle, state->dom_map->name,
302                 state->num_dom_xids, state->dom_xids, state->dom_sids);
303         if (tevent_req_nomem(subreq, req)) {
304                 return;
305         }
306         tevent_req_set_callback(subreq, wb_xids2sids_dom_done, req);
307 }
308
309 static NTSTATUS wb_xids2sids_dom_recv(struct tevent_req *req)
310 {
311         return tevent_req_simple_recv_ntstatus(req);
312 }
313
314 struct wb_xids2sids_state {
315         struct tevent_context *ev;
316         struct unixid *xids;
317         size_t num_xids;
318         struct dom_sid *sids;
319
320         size_t dom_idx;
321 };
322
323 static void wb_xids2sids_done(struct tevent_req *subreq);
324
325 struct tevent_req *wb_xids2sids_send(TALLOC_CTX *mem_ctx,
326                                      struct tevent_context *ev,
327                                      struct unixid *xids,
328                                      uint32_t num_xids)
329 {
330         struct tevent_req *req, *subreq;
331         struct wb_xids2sids_state *state;
332         size_t num_domains;
333
334         req = tevent_req_create(mem_ctx, &state,
335                                 struct wb_xids2sids_state);
336         if (req == NULL) {
337                 return NULL;
338         }
339         state->ev = ev;
340         state->xids = xids;
341         state->num_xids = num_xids;
342
343         state->sids = talloc_zero_array(state, struct dom_sid, num_xids);
344         if (tevent_req_nomem(state->sids, req)) {
345                 return tevent_req_post(req, ev);
346         }
347
348         if (winbindd_use_idmap_cache()) {
349                 uint32_t i;
350
351                 for (i=0; i<num_xids; i++) {
352                         struct dom_sid sid;
353                         bool ok, expired;
354
355                         switch (xids[i].type) {
356                             case ID_TYPE_UID:
357                                     ok = idmap_cache_find_uid2sid(
358                                             xids[i].id, &sid, &expired);
359                                     break;
360                             case ID_TYPE_GID:
361                                     ok = idmap_cache_find_gid2sid(
362                                             xids[i].id, &sid, &expired);
363                                     break;
364                             default:
365                                     ok = false;
366                         }
367
368                         if (ok && !expired) {
369                                 sid_copy(&state->sids[i], &sid);
370                         }
371                 }
372         }
373
374         wb_xids2sids_init_dom_maps();
375         num_domains = talloc_array_length(dom_maps);
376
377         if (num_domains == 0) {
378                 tevent_req_done(req);
379                 return tevent_req_post(req, ev);
380         }
381
382         subreq = wb_xids2sids_dom_send(
383                 state, state->ev, &dom_maps[state->dom_idx],
384                 state->xids, state->num_xids, state->sids);
385         if (tevent_req_nomem(subreq, req)) {
386                 return tevent_req_post(req, ev);
387         }
388         tevent_req_set_callback(subreq, wb_xids2sids_done, req);
389         return req;
390 }
391
392 static void wb_xids2sids_done(struct tevent_req *subreq)
393 {
394         struct tevent_req *req = tevent_req_callback_data(
395                 subreq, struct tevent_req);
396         struct wb_xids2sids_state *state = tevent_req_data(
397                 req, struct wb_xids2sids_state);
398         size_t num_domains = talloc_array_length(dom_maps);
399         NTSTATUS status;
400
401         status = wb_xids2sids_dom_recv(subreq);
402         TALLOC_FREE(subreq);
403         if (tevent_req_nterror(req, status)) {
404                 return;
405         }
406
407         state->dom_idx += 1;
408
409         if (state->dom_idx >= num_domains) {
410                 tevent_req_done(req);
411                 return;
412         }
413
414         subreq = wb_xids2sids_dom_send(
415                 state, state->ev, &dom_maps[state->dom_idx],
416                 state->xids, state->num_xids, state->sids);
417         if (tevent_req_nomem(subreq, req)) {
418                 return;
419         }
420         tevent_req_set_callback(subreq, wb_xids2sids_done, req);
421 }
422
423 NTSTATUS wb_xids2sids_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
424                            struct dom_sid **sids)
425 {
426         struct wb_xids2sids_state *state = tevent_req_data(
427                 req, struct wb_xids2sids_state);
428         NTSTATUS status;
429
430         if (tevent_req_is_nterror(req, &status)) {
431                 DEBUG(5, ("wb_sids_to_xids failed: %s\n", nt_errstr(status)));
432                 return status;
433         }
434
435         *sids = talloc_move(mem_ctx, &state->sids);
436         return NT_STATUS_OK;
437 }