2 Unix SMB/CIFS implementation.
4 Copyright (C) Volker Lendecke 2011
5 Copyright (C) Michael Adam 2012
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "../libcli/security/security.h"
24 #include "idmap_cache.h"
25 #include "librpc/gen_ndr/ndr_wbint_c.h"
27 struct wb_sids2xids_state {
28 struct tevent_context *ev;
33 struct id_map *cached;
35 struct dom_sid *non_cached;
36 uint32_t num_non_cached;
38 struct lsa_RefDomainList *domains;
39 struct lsa_TransNameArray *names;
41 struct wbint_TransIDArray ids;
45 static bool wb_sids2xids_in_cache(struct dom_sid *sid, struct id_map *map);
46 static void wb_sids2xids_lookupsids_done(struct tevent_req *subreq);
47 static void wb_sids2xids_done(struct tevent_req *subreq);
49 struct tevent_req *wb_sids2xids_send(TALLOC_CTX *mem_ctx,
50 struct tevent_context *ev,
51 const struct dom_sid *sids,
52 const uint32_t num_sids)
54 struct tevent_req *req, *subreq;
55 struct wb_sids2xids_state *state;
58 req = tevent_req_create(mem_ctx, &state,
59 struct wb_sids2xids_state);
66 state->num_sids = num_sids;
68 state->sids = talloc_zero_array(state, struct dom_sid, num_sids);
69 if (tevent_req_nomem(state->sids, req)) {
70 return tevent_req_post(req, ev);
73 for (i = 0; i < num_sids; i++) {
74 sid_copy(&state->sids[i], &sids[i]);
77 state->cached = talloc_zero_array(state, struct id_map, num_sids);
78 if (tevent_req_nomem(state->cached, req)) {
79 return tevent_req_post(req, ev);
82 state->non_cached = talloc_array(state, struct dom_sid, num_sids);
83 if (tevent_req_nomem(state->non_cached, req)) {
84 return tevent_req_post(req, ev);
88 * Extract those sids that can not be resolved from cache
89 * into a separate list to be handed to id mapping, keeping
92 for (i=0; i<state->num_sids; i++) {
94 DEBUG(10, ("SID %d: %s\n", (int)i,
95 sid_string_dbg(&state->sids[i])));
97 if (wb_sids2xids_in_cache(&state->sids[i], &state->cached[i])) {
100 sid_copy(&state->non_cached[state->num_non_cached],
102 state->num_non_cached += 1;
105 if (state->num_non_cached == 0) {
106 tevent_req_done(req);
107 return tevent_req_post(req, ev);
110 subreq = wb_lookupsids_send(state, ev, state->non_cached,
111 state->num_non_cached);
112 if (tevent_req_nomem(subreq, req)) {
113 return tevent_req_post(req, ev);
115 tevent_req_set_callback(subreq, wb_sids2xids_lookupsids_done, req);
119 static bool wb_sids2xids_in_cache(struct dom_sid *sid, struct id_map *map)
124 if (!winbindd_use_idmap_cache()) {
127 if (idmap_cache_find_sid2unixid(sid, &id, &expired)) {
128 if (expired && is_domain_online(find_our_domain())) {
133 map->status = ID_MAPPED;
139 static enum id_type lsa_SidType_to_id_type(const enum lsa_SidType sid_type);
141 static void wb_sids2xids_lookupsids_done(struct tevent_req *subreq)
143 struct tevent_req *req = tevent_req_callback_data(
144 subreq, struct tevent_req);
145 struct wb_sids2xids_state *state = tevent_req_data(
146 req, struct wb_sids2xids_state);
147 struct winbindd_child *child;
151 status = wb_lookupsids_recv(subreq, state, &state->domains,
154 if (tevent_req_nterror(req, status)) {
158 state->ids.num_ids = state->num_non_cached;
159 state->ids.ids = talloc_array(state, struct wbint_TransID,
160 state->num_non_cached);
161 if (tevent_req_nomem(state->ids.ids, req)) {
165 for (i=0; i<state->num_non_cached; i++) {
166 struct lsa_TranslatedName *n = &state->names->names[i];
167 struct wbint_TransID *t = &state->ids.ids[i];
169 t->type = lsa_SidType_to_id_type(n->sid_type);
170 t->domain_index = n->sid_index;
171 sid_peek_rid(&state->non_cached[i], &t->rid);
172 t->xid.id = UINT32_MAX;
173 t->xid.type = t->type;
176 child = idmap_child();
178 subreq = dcerpc_wbint_Sids2UnixIDs_send(
179 state, state->ev, child->binding_handle, state->domains,
181 if (tevent_req_nomem(subreq, req)) {
184 tevent_req_set_callback(subreq, wb_sids2xids_done, req);
187 static enum id_type lsa_SidType_to_id_type(const enum lsa_SidType sid_type)
192 case SID_NAME_COMPUTER:
196 case SID_NAME_DOM_GRP:
198 case SID_NAME_WKN_GRP:
202 type = ID_TYPE_NOT_SPECIFIED;
210 static void wb_sids2xids_done(struct tevent_req *subreq)
212 struct tevent_req *req = tevent_req_callback_data(
213 subreq, struct tevent_req);
214 struct wb_sids2xids_state *state = tevent_req_data(
215 req, struct wb_sids2xids_state);
216 NTSTATUS status, result;
218 status = dcerpc_wbint_Sids2UnixIDs_recv(subreq, state, &result);
220 if (any_nt_status_not_ok(status, result, &status)) {
221 tevent_req_nterror(req, status);
224 tevent_req_done(req);
227 NTSTATUS wb_sids2xids_recv(struct tevent_req *req,
230 struct wb_sids2xids_state *state = tevent_req_data(
231 req, struct wb_sids2xids_state);
233 uint32_t i, num_non_cached;
235 if (tevent_req_is_nterror(req, &status)) {
236 DEBUG(5, ("wb_sids_to_xids failed: %s\n", nt_errstr(status)));
242 for (i=0; i<state->num_sids; i++) {
247 if (state->cached[i].sid != NULL) {
248 xid = state->cached[i].xid;
250 xid = state->ids.ids[num_non_cached].xid;
252 idmap_cache_set_sid2unixid(
253 &state->non_cached[num_non_cached],