s3:winbindd: convert some spaces to tabs in winbindd_sids_to_xids_send()
[Samba/gebeck_regimport.git] / source3 / winbindd / winbindd_sids_to_xids.c
blob7456d3a307156342c228a1700d37896e86486745
1 /*
2 Unix SMB/CIFS implementation.
3 async implementation of WINBINDD_SIDS_TO_XIDS
4 Copyright (C) Volker Lendecke 2011
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/>.
20 #include "includes.h"
21 #include "winbindd.h"
22 #include "../libcli/security/security.h"
23 #include "librpc/gen_ndr/ndr_wbint_c.h"
24 #include "idmap_cache.h"
26 struct winbindd_sids_to_xids_state {
27 struct tevent_context *ev;
28 struct dom_sid *sids;
29 uint32_t num_sids;
31 struct id_map *cached;
33 struct dom_sid *non_cached;
34 uint32_t num_non_cached;
36 struct lsa_RefDomainList *domains;
37 struct lsa_TransNameArray *names;
39 struct wbint_TransIDArray ids;
42 static bool winbindd_sids_to_xids_in_cache(struct dom_sid *sid,
43 struct id_map *map);
44 static void winbindd_sids_to_xids_lookupsids_done(struct tevent_req *subreq);
45 static void winbindd_sids_to_xids_done(struct tevent_req *subreq);
47 struct tevent_req *winbindd_sids_to_xids_send(TALLOC_CTX *mem_ctx,
48 struct tevent_context *ev,
49 struct winbindd_cli_state *cli,
50 struct winbindd_request *request)
52 struct tevent_req *req, *subreq;
53 struct winbindd_sids_to_xids_state *state;
54 uint32_t i;
56 req = tevent_req_create(mem_ctx, &state,
57 struct winbindd_sids_to_xids_state);
58 if (req == NULL) {
59 return NULL;
61 state->ev = ev;
63 DEBUG(3, ("sids_to_xids\n"));
65 if (request->extra_len == 0) {
66 tevent_req_done(req);
67 return tevent_req_post(req, ev);
69 if (request->extra_data.data[request->extra_len-1] != '\0') {
70 DEBUG(10, ("Got invalid sids list\n"));
71 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
72 return tevent_req_post(req, ev);
74 if (!parse_sidlist(state, request->extra_data.data,
75 &state->sids, &state->num_sids)) {
76 DEBUG(10, ("parse_sidlist failed\n"));
77 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
78 return tevent_req_post(req, ev);
81 DEBUG(10, ("num_sids: %d\n", (int)state->num_sids));
83 state->cached = talloc_zero_array(state, struct id_map,
84 state->num_sids);
85 if (tevent_req_nomem(state->cached, req)) {
86 return tevent_req_post(req, ev);
88 state->non_cached = talloc_array(state, struct dom_sid,
89 state->num_sids);
90 if (tevent_req_nomem(state->non_cached, req)) {
91 return tevent_req_post(req, ev);
95 * Extract those sids that can not be resolved from cache
96 * into a separate list to be handed to id mapping, keeping
97 * the same index.
99 for (i=0; i<state->num_sids; i++) {
101 DEBUG(10, ("SID %d: %s\n", (int)i,
102 sid_string_dbg(&state->sids[i])));
104 if (winbindd_sids_to_xids_in_cache(&state->sids[i],
105 &state->cached[i])) {
106 continue;
108 sid_copy(&state->non_cached[state->num_non_cached],
109 &state->sids[i]);
110 state->num_non_cached += 1;
113 if (state->num_non_cached == 0) {
114 tevent_req_done(req);
115 return tevent_req_post(req, ev);
118 subreq = wb_lookupsids_send(state, ev, state->non_cached,
119 state->num_non_cached);
120 if (tevent_req_nomem(subreq, req)) {
121 return tevent_req_post(req, ev);
123 tevent_req_set_callback(subreq, winbindd_sids_to_xids_lookupsids_done,
124 req);
125 return req;
128 static bool winbindd_sids_to_xids_in_cache(struct dom_sid *sid,
129 struct id_map *map)
131 struct unixid id;
132 bool expired;
134 if (!winbindd_use_idmap_cache()) {
135 return false;
137 if (idmap_cache_find_sid2unixid(sid, &id, &expired)) {
138 if (expired && is_domain_online(find_our_domain())) {
139 return false;
141 map->sid = sid;
142 map->xid = id;
143 map->status = ID_MAPPED;
144 return true;
146 return false;
149 static enum id_type lsa_SidType_to_id_type(const enum lsa_SidType sid_type);
151 static void winbindd_sids_to_xids_lookupsids_done(struct tevent_req *subreq)
153 struct tevent_req *req = tevent_req_callback_data(
154 subreq, struct tevent_req);
155 struct winbindd_sids_to_xids_state *state = tevent_req_data(
156 req, struct winbindd_sids_to_xids_state);
157 struct winbindd_child *child;
158 NTSTATUS status;
159 int i;
161 status = wb_lookupsids_recv(subreq, state, &state->domains,
162 &state->names);
163 TALLOC_FREE(subreq);
164 if (tevent_req_nterror(req, status)) {
165 return;
168 state->ids.num_ids = state->num_non_cached;
169 state->ids.ids = talloc_array(state, struct wbint_TransID,
170 state->num_non_cached);
171 if (tevent_req_nomem(state->ids.ids, req)) {
172 return;
175 for (i=0; i<state->num_non_cached; i++) {
176 struct lsa_TranslatedName *n = &state->names->names[i];
177 struct wbint_TransID *t = &state->ids.ids[i];
179 t->type = lsa_SidType_to_id_type(n->sid_type);
180 t->domain_index = n->sid_index;
181 sid_peek_rid(&state->non_cached[i], &t->rid);
182 t->unix_id = (uint64_t)-1;
185 child = idmap_child();
187 subreq = dcerpc_wbint_Sids2UnixIDs_send(
188 state, state->ev, child->binding_handle, state->domains,
189 &state->ids);
190 if (tevent_req_nomem(subreq, req)) {
191 return;
193 tevent_req_set_callback(subreq, winbindd_sids_to_xids_done, req);
196 static enum id_type lsa_SidType_to_id_type(const enum lsa_SidType sid_type)
198 enum id_type type;
200 switch(sid_type) {
201 case SID_NAME_COMPUTER:
202 case SID_NAME_USER:
203 type = ID_TYPE_UID;
204 break;
205 case SID_NAME_DOM_GRP:
206 case SID_NAME_ALIAS:
207 case SID_NAME_WKN_GRP:
208 type = ID_TYPE_GID;
209 break;
210 default:
211 type = ID_TYPE_NOT_SPECIFIED;
212 break;
215 return type;
219 static void winbindd_sids_to_xids_done(struct tevent_req *subreq)
221 struct tevent_req *req = tevent_req_callback_data(
222 subreq, struct tevent_req);
223 struct winbindd_sids_to_xids_state *state = tevent_req_data(
224 req, struct winbindd_sids_to_xids_state);
225 NTSTATUS status, result;
227 status = dcerpc_wbint_Sids2UnixIDs_recv(subreq, state, &result);
228 TALLOC_FREE(subreq);
229 if (any_nt_status_not_ok(status, result, &status)) {
230 tevent_req_nterror(req, status);
231 return;
233 tevent_req_done(req);
236 NTSTATUS winbindd_sids_to_xids_recv(struct tevent_req *req,
237 struct winbindd_response *response)
239 struct winbindd_sids_to_xids_state *state = tevent_req_data(
240 req, struct winbindd_sids_to_xids_state);
241 NTSTATUS status;
242 char *result = NULL;
243 uint32_t i, num_non_cached;
245 if (tevent_req_is_nterror(req, &status)) {
246 DEBUG(5, ("wb_sids_to_xids failed: %s\n", nt_errstr(status)));
247 return status;
250 result = talloc_strdup(response, "");
251 if (result == NULL) {
252 return NT_STATUS_NO_MEMORY;
255 num_non_cached = 0;
257 for (i=0; i<state->num_sids; i++) {
258 char type = '\0';
259 bool found = true;
260 struct unixid xid;
262 xid.id = UINT32_MAX;
264 if (state->cached[i].sid != NULL) {
265 xid = state->cached[i].xid;
266 } else {
267 xid.id = state->ids.ids[num_non_cached].unix_id;
268 xid.type = state->ids.ids[num_non_cached].type;
270 idmap_cache_set_sid2unixid(
271 &state->non_cached[num_non_cached],
272 &xid);
274 num_non_cached += 1;
277 switch (xid.type) {
278 case ID_TYPE_UID:
279 type = 'U';
280 break;
281 case ID_TYPE_GID:
282 type = 'G';
283 break;
284 case ID_TYPE_BOTH:
285 type = 'B';
286 break;
287 default:
288 found = false;
289 break;
292 if (xid.id == UINT32_MAX) {
293 found = false;
296 if (found) {
297 result = talloc_asprintf_append_buffer(
298 result, "%c%lu\n", type,
299 (unsigned long)xid.id);
300 } else {
301 result = talloc_asprintf_append_buffer(result, "\n");
303 if (result == NULL) {
304 return NT_STATUS_NO_MEMORY;
308 response->extra_data.data = result;
309 response->length += talloc_get_size(result);
310 return NT_STATUS_OK;