Merge branch 'master' of ctdb into 'master' of samba
[samba.git] / source3 / winbindd / wb_sids2xids.c
1 /*
2    Unix SMB/CIFS implementation.
3    async sids2xids
4    Copyright (C) Volker Lendecke 2011
5    Copyright (C) Michael Adam 2012
6
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.
11
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.
16
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/>.
19 */
20
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"
26
27 struct wb_sids2xids_state {
28         struct tevent_context *ev;
29
30         struct dom_sid *sids;
31         uint32_t num_sids;
32
33         struct id_map *cached;
34
35         struct dom_sid *non_cached;
36         uint32_t num_non_cached;
37
38         struct lsa_RefDomainList *domains;
39         struct lsa_TransNameArray *names;
40
41         struct wbint_TransIDArray ids;
42 };
43
44
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);
48
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)
53 {
54         struct tevent_req *req, *subreq;
55         struct wb_sids2xids_state *state;
56         uint32_t i;
57
58         req = tevent_req_create(mem_ctx, &state,
59                                 struct wb_sids2xids_state);
60         if (req == NULL) {
61                 return NULL;
62         }
63
64         state->ev = ev;
65
66         state->num_sids = num_sids;
67
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);
71         }
72
73         for (i = 0; i < num_sids; i++) {
74                 sid_copy(&state->sids[i], &sids[i]);
75         }
76
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);
80         }
81
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);
85         }
86
87         /*
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.
91          */
92         for (i=0; i<state->num_sids; i++) {
93
94                 DEBUG(10, ("SID %d: %s\n", (int)i,
95                            sid_string_dbg(&state->sids[i])));
96
97                 if (wb_sids2xids_in_cache(&state->sids[i], &state->cached[i])) {
98                         continue;
99                 }
100                 sid_copy(&state->non_cached[state->num_non_cached],
101                          &state->sids[i]);
102                 state->num_non_cached += 1;
103         }
104
105         if (state->num_non_cached == 0) {
106                 tevent_req_done(req);
107                 return tevent_req_post(req, ev);
108         }
109
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);
114         }
115         tevent_req_set_callback(subreq, wb_sids2xids_lookupsids_done, req);
116         return req;
117 }
118
119 static bool wb_sids2xids_in_cache(struct dom_sid *sid, struct id_map *map)
120 {
121         struct unixid id;
122         bool expired;
123
124         if (!winbindd_use_idmap_cache()) {
125                 return false;
126         }
127         if (idmap_cache_find_sid2unixid(sid, &id, &expired)) {
128                 if (expired && is_domain_online(find_our_domain())) {
129                         return false;
130                 }
131                 map->sid = sid;
132                 map->xid = id;
133                 map->status = ID_MAPPED;
134                 return true;
135         }
136         return false;
137 }
138
139 static enum id_type lsa_SidType_to_id_type(const enum lsa_SidType sid_type);
140
141 static void wb_sids2xids_lookupsids_done(struct tevent_req *subreq)
142 {
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;
150
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;
156         }
157
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;
163         }
164
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];
168
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;
174         }
175
176         child = idmap_child();
177
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;
183         }
184         tevent_req_set_callback(subreq, wb_sids2xids_done, req);
185 }
186
187 static enum id_type lsa_SidType_to_id_type(const enum lsa_SidType sid_type)
188 {
189         enum id_type type;
190
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;
204         }
205
206         return type;
207 }
208
209
210 static void wb_sids2xids_done(struct tevent_req *subreq)
211 {
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;
217
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;
223         }
224         tevent_req_done(req);
225 }
226
227 NTSTATUS wb_sids2xids_recv(struct tevent_req *req,
228                            struct unixid *xids)
229 {
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;
234
235         if (tevent_req_is_nterror(req, &status)) {
236                 DEBUG(5, ("wb_sids_to_xids failed: %s\n", nt_errstr(status)));
237                 return status;
238         }
239
240         num_non_cached = 0;
241
242         for (i=0; i<state->num_sids; i++) {
243                 struct unixid xid;
244
245                 xid.id = UINT32_MAX;
246
247                 if (state->cached[i].sid != NULL) {
248                         xid = state->cached[i].xid;
249                 } else {
250                         xid = state->ids.ids[num_non_cached].xid;
251
252                         idmap_cache_set_sid2unixid(
253                                 &state->non_cached[num_non_cached],
254                                 &xid);
255
256                         num_non_cached += 1;
257                 }
258
259                 xids[i] = xid;
260         }
261
262         return NT_STATUS_OK;
263 }