2 * Unix SMB/CIFS implementation.
4 * Copyright (C) Volker Lendecke 2015
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/>.
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 #include "passdb/lookup_sid.h"
28 struct wb_xids2sids_dom_map {
36 * Map idmap ranges to domain names, taken from smb.conf. This is
37 * stored in the parent winbind and used to assemble xid2sid calls
38 * into per-idmap-domain chunks.
40 static struct wb_xids2sids_dom_map *dom_maps;
42 static bool wb_xids2sids_add_dom(const char *domname,
45 struct wb_xids2sids_dom_map *map = NULL;
46 size_t num_maps = talloc_array_length(dom_maps);
49 unsigned low_id, high_id;
52 range = idmap_config_const_string(domname, "range", NULL);
54 DBG_DEBUG("No range for domain %s found\n", domname);
58 ret = sscanf(range, "%u - %u", &low_id, &high_id);
60 DBG_DEBUG("Invalid range spec \"%s\" for domain %s\n",
65 if (low_id > high_id) {
66 DBG_DEBUG("Invalid range %u - %u for domain %s\n",
67 low_id, high_id, domname);
71 for (i=0; i<num_maps; i++) {
72 if (strequal(domname, dom_maps[i].name)) {
79 struct wb_xids2sids_dom_map *tmp;
82 name = talloc_strdup(talloc_tos(), domname);
84 DBG_DEBUG("talloc failed\n");
89 NULL, dom_maps, struct wb_xids2sids_dom_map,
97 map = &dom_maps[num_maps];
99 map->name = talloc_move(dom_maps, &name);
102 map->low_id = low_id;
103 map->high_id = high_id;
108 struct wb_xids2sids_init_dom_maps_state {
109 struct tevent_context *ev;
110 struct tevent_req *req;
114 static void wb_xids2sids_init_dom_maps_lookupname_next(
115 struct wb_xids2sids_init_dom_maps_state *state);
117 static void wb_xids2sids_init_dom_maps_lookupname_done(
118 struct tevent_req *subreq);
120 static struct tevent_req *wb_xids2sids_init_dom_maps_send(
121 TALLOC_CTX *mem_ctx, struct tevent_context *ev)
123 struct tevent_req *req = NULL;
124 struct wb_xids2sids_init_dom_maps_state *state = NULL;
126 req = tevent_req_create(mem_ctx, &state,
127 struct wb_xids2sids_init_dom_maps_state);
131 *state = (struct wb_xids2sids_init_dom_maps_state) {
137 if (dom_maps != NULL) {
138 tevent_req_done(req);
139 return tevent_req_post(req, ev);
142 * Put the passdb idmap domain first. We always need to try
146 dom_maps = talloc_zero_array(NULL, struct wb_xids2sids_dom_map, 1);
147 if (tevent_req_nomem(dom_maps, req)) {
148 return tevent_req_post(req, ev);
150 dom_maps[0].low_id = 0;
151 dom_maps[0].high_id = UINT_MAX;
152 dom_maps[0].name = talloc_strdup(dom_maps, get_global_sam_name());
153 if (tevent_req_nomem(dom_maps[0].name, req)) {
154 TALLOC_FREE(dom_maps);
155 return tevent_req_post(req, ev);
158 lp_scan_idmap_domains(wb_xids2sids_add_dom, NULL);
160 wb_xids2sids_init_dom_maps_lookupname_next(state);
161 if (!tevent_req_is_in_progress(req)) {
162 tevent_req_post(req, ev);
167 static void wb_xids2sids_init_dom_maps_lookupname_next(
168 struct wb_xids2sids_init_dom_maps_state *state)
170 struct tevent_req *subreq = NULL;
172 if (state->dom_idx == talloc_array_length(dom_maps)) {
173 tevent_req_done(state->req);
177 if (strequal(dom_maps[state->dom_idx].name, "*")) {
179 if (state->dom_idx == talloc_array_length(dom_maps)) {
180 tevent_req_done(state->req);
185 subreq = wb_lookupname_send(state,
187 dom_maps[state->dom_idx].name,
188 dom_maps[state->dom_idx].name,
191 if (tevent_req_nomem(subreq, state->req)) {
194 tevent_req_set_callback(subreq,
195 wb_xids2sids_init_dom_maps_lookupname_done,
199 static void wb_xids2sids_init_dom_maps_lookupname_done(
200 struct tevent_req *subreq)
202 struct tevent_req *req = tevent_req_callback_data(
203 subreq, struct tevent_req);
204 struct wb_xids2sids_init_dom_maps_state *state = tevent_req_data(
205 req, struct wb_xids2sids_init_dom_maps_state);
206 enum lsa_SidType type;
209 status = wb_lookupname_recv(subreq,
210 &dom_maps[state->dom_idx].sid,
213 if (!NT_STATUS_IS_OK(status)) {
214 DBG_WARNING("Lookup domain name '%s' failed '%s'\n",
215 dom_maps[state->dom_idx].name,
219 wb_xids2sids_init_dom_maps_lookupname_next(state);
223 if (type != SID_NAME_DOMAIN) {
224 struct dom_sid_buf buf;
226 DBG_WARNING("SID %s for idmap domain name '%s' "
227 "not a domain SID\n",
228 dom_sid_str_buf(&dom_maps[state->dom_idx].sid,
230 dom_maps[state->dom_idx].name);
232 ZERO_STRUCT(dom_maps[state->dom_idx].sid);
236 wb_xids2sids_init_dom_maps_lookupname_next(state);
241 static NTSTATUS wb_xids2sids_init_dom_maps_recv(struct tevent_req *req)
243 return tevent_req_simple_recv_ntstatus(req);
246 struct wb_xids2sids_dom_state {
247 struct tevent_context *ev;
248 struct unixid *all_xids;
251 struct dom_sid *all_sids;
252 struct wb_xids2sids_dom_map *dom_map;
256 struct unixid *dom_xids;
257 struct dom_sid *dom_sids;
260 static void wb_xids2sids_dom_done(struct tevent_req *subreq);
261 static void wb_xids2sids_dom_gotdc(struct tevent_req *subreq);
263 static struct tevent_req *wb_xids2sids_dom_send(
264 TALLOC_CTX *mem_ctx, struct tevent_context *ev,
265 struct wb_xids2sids_dom_map *dom_map,
269 struct dom_sid *sids)
271 struct tevent_req *req, *subreq;
272 struct wb_xids2sids_dom_state *state;
273 struct winbindd_child *child;
276 req = tevent_req_create(mem_ctx, &state,
277 struct wb_xids2sids_dom_state);
282 state->all_xids = xids;
283 state->cached = cached;
284 state->num_all_xids = num_xids;
285 state->all_sids = sids;
286 state->dom_map = dom_map;
288 state->dom_xids = talloc_array(state, struct unixid, num_xids);
289 if (tevent_req_nomem(state->dom_xids, req)) {
290 return tevent_req_post(req, ev);
292 state->dom_sids = talloc_array(state, struct dom_sid, num_xids);
293 if (tevent_req_nomem(state->dom_sids, req)) {
294 return tevent_req_post(req, ev);
297 for (i=0; i<num_xids; i++) {
298 struct unixid id = state->all_xids[i];
300 if ((id.id < dom_map->low_id) || (id.id > dom_map->high_id)) {
304 if (state->cached[i]) {
308 state->dom_xids[state->num_dom_xids++] = id;
311 if (state->num_dom_xids == 0) {
312 tevent_req_done(req);
313 return tevent_req_post(req, ev);
316 child = idmap_child();
317 subreq = dcerpc_wbint_UnixIDs2Sids_send(
318 state, ev, child->binding_handle, dom_map->name, dom_map->sid,
319 state->num_dom_xids, state->dom_xids, state->dom_sids);
320 if (tevent_req_nomem(subreq, req)) {
321 return tevent_req_post(req, ev);
323 tevent_req_set_callback(subreq, wb_xids2sids_dom_done, req);
327 static void wb_xids2sids_dom_done(struct tevent_req *subreq)
329 struct tevent_req *req = tevent_req_callback_data(
330 subreq, struct tevent_req);
331 struct wb_xids2sids_dom_state *state = tevent_req_data(
332 req, struct wb_xids2sids_dom_state);
333 struct wb_xids2sids_dom_map *dom_map = state->dom_map;
334 NTSTATUS status, result;
338 status = dcerpc_wbint_UnixIDs2Sids_recv(subreq, state, &result);
340 if (tevent_req_nterror(req, status)) {
344 if (NT_STATUS_EQUAL(result, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND) &&
345 !state->tried_dclookup) {
347 subreq = wb_dsgetdcname_send(
348 state, state->ev, state->dom_map->name, NULL, NULL,
350 if (tevent_req_nomem(subreq, req)) {
353 tevent_req_set_callback(subreq, wb_xids2sids_dom_gotdc, req);
357 if (!NT_STATUS_EQUAL(result, NT_STATUS_NONE_MAPPED) &&
358 tevent_req_nterror(req, result)) {
364 for (i=0; i<state->num_all_xids; i++) {
365 struct unixid *id = &state->all_xids[i];
367 if ((id->id < dom_map->low_id) || (id->id > dom_map->high_id)) {
371 if (state->cached[i]) {
376 sid_copy(&state->all_sids[i], &state->dom_sids[dom_sid_idx]);
377 *id = state->dom_xids[dom_sid_idx];
382 tevent_req_done(req);
385 static void wb_xids2sids_dom_gotdc(struct tevent_req *subreq)
387 struct tevent_req *req = tevent_req_callback_data(
388 subreq, struct tevent_req);
389 struct wb_xids2sids_dom_state *state = tevent_req_data(
390 req, struct wb_xids2sids_dom_state);
391 struct winbindd_child *child = idmap_child();
392 struct netr_DsRGetDCNameInfo *dcinfo;
395 status = wb_dsgetdcname_recv(subreq, state, &dcinfo);
397 if (tevent_req_nterror(req, status)) {
401 state->tried_dclookup = true;
403 status = wb_dsgetdcname_gencache_set(state->dom_map->name, dcinfo);
404 if (tevent_req_nterror(req, status)) {
408 child = idmap_child();
409 subreq = dcerpc_wbint_UnixIDs2Sids_send(
410 state, state->ev, child->binding_handle, state->dom_map->name,
411 state->dom_map->sid, state->num_dom_xids,
412 state->dom_xids, state->dom_sids);
413 if (tevent_req_nomem(subreq, req)) {
416 tevent_req_set_callback(subreq, wb_xids2sids_dom_done, req);
419 static NTSTATUS wb_xids2sids_dom_recv(struct tevent_req *req)
421 return tevent_req_simple_recv_ntstatus(req);
424 struct wb_xids2sids_state {
425 struct tevent_context *ev;
428 struct dom_sid *sids;
434 static void wb_xids2sids_done(struct tevent_req *subreq);
435 static void wb_xids2sids_init_dom_maps_done(struct tevent_req *subreq);
437 struct tevent_req *wb_xids2sids_send(TALLOC_CTX *mem_ctx,
438 struct tevent_context *ev,
439 const struct unixid *xids,
442 struct tevent_req *req, *subreq;
443 struct wb_xids2sids_state *state;
445 req = tevent_req_create(mem_ctx, &state,
446 struct wb_xids2sids_state);
451 state->num_xids = num_xids;
453 state->xids = talloc_array(state, struct unixid, num_xids);
454 if (tevent_req_nomem(state->xids, req)) {
455 return tevent_req_post(req, ev);
457 memcpy(state->xids, xids, num_xids * sizeof(struct unixid));
459 state->sids = talloc_zero_array(state, struct dom_sid, num_xids);
460 if (tevent_req_nomem(state->sids, req)) {
461 return tevent_req_post(req, ev);
464 state->cached = talloc_zero_array(state, bool, num_xids);
465 if (tevent_req_nomem(state->cached, req)) {
466 return tevent_req_post(req, ev);
469 if (winbindd_use_idmap_cache()) {
472 for (i=0; i<num_xids; i++) {
473 struct dom_sid sid = {0};
474 bool ok, expired = true;
476 ok = idmap_cache_find_xid2sid(
477 &xids[i], &sid, &expired);
478 if (ok && !expired) {
479 struct dom_sid_buf buf;
480 DBG_DEBUG("Found %cID in cache: %s\n",
481 xids[i].type == ID_TYPE_UID?'U':'G',
482 dom_sid_str_buf(&sid, &buf));
484 sid_copy(&state->sids[i], &sid);
485 state->cached[i] = true;
490 subreq = wb_xids2sids_init_dom_maps_send(
492 if (tevent_req_nomem(subreq, req)) {
493 return tevent_req_post(req, ev);
495 tevent_req_set_callback(subreq, wb_xids2sids_init_dom_maps_done, req);
499 static void wb_xids2sids_init_dom_maps_done(struct tevent_req *subreq)
501 struct tevent_req *req = tevent_req_callback_data(
502 subreq, struct tevent_req);
503 struct wb_xids2sids_state *state = tevent_req_data(
504 req, struct wb_xids2sids_state);
508 status = wb_xids2sids_init_dom_maps_recv(subreq);
510 if (tevent_req_nterror(req, status)) {
514 num_domains = talloc_array_length(dom_maps);
515 if (num_domains == 0) {
516 tevent_req_done(req);
520 subreq = wb_xids2sids_dom_send(
521 state, state->ev, &dom_maps[state->dom_idx],
522 state->xids, state->cached, state->num_xids, state->sids);
523 if (tevent_req_nomem(subreq, req)) {
526 tevent_req_set_callback(subreq, wb_xids2sids_done, req);
530 static void wb_xids2sids_done(struct tevent_req *subreq)
532 struct tevent_req *req = tevent_req_callback_data(
533 subreq, struct tevent_req);
534 struct wb_xids2sids_state *state = tevent_req_data(
535 req, struct wb_xids2sids_state);
536 size_t num_domains = talloc_array_length(dom_maps);
540 status = wb_xids2sids_dom_recv(subreq);
542 if (tevent_req_nterror(req, status)) {
548 if (state->dom_idx < num_domains) {
549 subreq = wb_xids2sids_dom_send(state,
551 &dom_maps[state->dom_idx],
556 if (tevent_req_nomem(subreq, req)) {
559 tevent_req_set_callback(subreq, wb_xids2sids_done, req);
564 for (i = 0; i < state->num_xids; i++) {
566 * Prime the cache after an xid2sid call. It's important that we
567 * use the xid value returned from the backend for the xid value
568 * passed to idmap_cache_set_sid2unixid(), not the input to
569 * wb_xids2sids_send: the input carries what was asked for,
570 * e.g. a ID_TYPE_UID. The result from the backend something the
571 * idmap child possibly changed to ID_TYPE_BOTH.
573 * And of course If the value was from the cache don't update
577 if (state->cached[i]) {
581 idmap_cache_set_sid2unixid(&state->sids[i], &state->xids[i]);
584 tevent_req_done(req);
588 NTSTATUS wb_xids2sids_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
589 struct dom_sid **sids)
591 struct wb_xids2sids_state *state = tevent_req_data(
592 req, struct wb_xids2sids_state);
595 if (tevent_req_is_nterror(req, &status)) {
596 DEBUG(5, ("wb_sids_to_xids failed: %s\n", nt_errstr(status)));
600 *sids = talloc_move(mem_ctx, &state->sids);