s3:winbindd: factor winbindd_sids_to_xids into external and internal part
[Samba/gebeck_regimport.git] / source3 / winbindd / wb_sids2xids.c
blob7a198c18cea7469de46f4571eb10a8aec091dca5
1 /*
2 Unix SMB/CIFS implementation.
3 async sids2xids
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/>.
21 #include "includes.h"
22 #include "winbindd.h"
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;
30 struct dom_sid *sids;
31 uint32_t num_sids;
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;
56 uint32_t i;
58 req = tevent_req_create(mem_ctx, &state,
59 struct wb_sids2xids_state);
60 if (req == NULL) {
61 return NULL;
64 state->ev = ev;
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
90 * the same index.
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])) {
98 continue;
100 sid_copy(&state->non_cached[state->num_non_cached],
101 &state->sids[i]);
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);
116 return req;
119 static bool wb_sids2xids_in_cache(struct dom_sid *sid, struct id_map *map)
121 struct unixid id;
122 bool expired;
124 if (!winbindd_use_idmap_cache()) {
125 return false;
127 if (idmap_cache_find_sid2unixid(sid, &id, &expired)) {
128 if (expired && is_domain_online(find_our_domain())) {
129 return false;
131 map->sid = sid;
132 map->xid = id;
133 map->status = ID_MAPPED;
134 return true;
136 return false;
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;
148 NTSTATUS status;
149 int i;
151 status = wb_lookupsids_recv(subreq, state, &state->domains,
152 &state->names);
153 TALLOC_FREE(subreq);
154 if (tevent_req_nterror(req, status)) {
155 return;
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)) {
162 return;
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->unix_id = (uint64_t)-1;
175 child = idmap_child();
177 subreq = dcerpc_wbint_Sids2UnixIDs_send(
178 state, state->ev, child->binding_handle, state->domains,
179 &state->ids);
180 if (tevent_req_nomem(subreq, req)) {
181 return;
183 tevent_req_set_callback(subreq, wb_sids2xids_done, req);
186 static enum id_type lsa_SidType_to_id_type(const enum lsa_SidType sid_type)
188 enum id_type type;
190 switch(sid_type) {
191 case SID_NAME_COMPUTER:
192 case SID_NAME_USER:
193 type = ID_TYPE_UID;
194 break;
195 case SID_NAME_DOM_GRP:
196 case SID_NAME_ALIAS:
197 case SID_NAME_WKN_GRP:
198 type = ID_TYPE_GID;
199 break;
200 default:
201 type = ID_TYPE_NOT_SPECIFIED;
202 break;
205 return type;
209 static void wb_sids2xids_done(struct tevent_req *subreq)
211 struct tevent_req *req = tevent_req_callback_data(
212 subreq, struct tevent_req);
213 struct wb_sids2xids_state *state = tevent_req_data(
214 req, struct wb_sids2xids_state);
215 NTSTATUS status, result;
217 status = dcerpc_wbint_Sids2UnixIDs_recv(subreq, state, &result);
218 TALLOC_FREE(subreq);
219 if (any_nt_status_not_ok(status, result, &status)) {
220 tevent_req_nterror(req, status);
221 return;
223 tevent_req_done(req);
226 NTSTATUS wb_sids2xids_recv(struct tevent_req *req,
227 struct unixid *xids)
229 struct wb_sids2xids_state *state = tevent_req_data(
230 req, struct wb_sids2xids_state);
231 NTSTATUS status;
232 uint32_t i, num_non_cached;
234 if (tevent_req_is_nterror(req, &status)) {
235 DEBUG(5, ("wb_sids_to_xids failed: %s\n", nt_errstr(status)));
236 return status;
239 num_non_cached = 0;
241 for (i=0; i<state->num_sids; i++) {
242 struct unixid xid;
244 xid.id = UINT32_MAX;
246 if (state->cached[i].sid != NULL) {
247 xid = state->cached[i].xid;
248 } else {
249 xid.id = state->ids.ids[num_non_cached].unix_id;
250 xid.type = state->ids.ids[num_non_cached].type;
252 idmap_cache_set_sid2unixid(
253 &state->non_cached[num_non_cached],
254 &xid);
256 num_non_cached += 1;
259 xids[i] = xid;
262 return NT_STATUS_OK;