s3:winbindd: remove unused idmap_sid_to_gid()
[Samba/bb.git] / source3 / winbindd / wb_sids2xids.c
blobcbd4444566f2e10ddeb9b6ba8ba47c6a190ff5e2
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->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,
180 &state->ids);
181 if (tevent_req_nomem(subreq, req)) {
182 return;
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)
189 enum id_type type;
191 switch(sid_type) {
192 case SID_NAME_COMPUTER:
193 case SID_NAME_USER:
194 type = ID_TYPE_UID;
195 break;
196 case SID_NAME_DOM_GRP:
197 case SID_NAME_ALIAS:
198 case SID_NAME_WKN_GRP:
199 type = ID_TYPE_GID;
200 break;
201 default:
202 type = ID_TYPE_NOT_SPECIFIED;
203 break;
206 return type;
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);
219 TALLOC_FREE(subreq);
220 if (any_nt_status_not_ok(status, result, &status)) {
221 tevent_req_nterror(req, status);
222 return;
224 tevent_req_done(req);
227 NTSTATUS wb_sids2xids_recv(struct tevent_req *req,
228 struct unixid *xids)
230 struct wb_sids2xids_state *state = tevent_req_data(
231 req, struct wb_sids2xids_state);
232 NTSTATUS status;
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)));
237 return status;
240 num_non_cached = 0;
242 for (i=0; i<state->num_sids; i++) {
243 struct unixid xid;
245 xid.id = UINT32_MAX;
247 if (state->cached[i].sid != NULL) {
248 xid = state->cached[i].xid;
249 } else {
250 xid = state->ids.ids[num_non_cached].xid;
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;