s3:idmap_ldap: rename idmap_ldap_allocate_id to idmap_ldap_allocate_id_internal
[samba.git] / source3 / winbindd / winbindd_sids_to_xids.c
1 /*
2    Unix SMB/CIFS implementation.
3    async implementation of WINBINDD_SIDS_TO_XIDS
4    Copyright (C) Volker Lendecke 2011
5
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.
10
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.
15
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/>.
18 */
19
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"
25
26 struct winbindd_sids_to_xids_state {
27         struct tevent_context *ev;
28         struct dom_sid *sids;
29         uint32_t num_sids;
30
31         struct id_map *cached;
32
33         struct dom_sid *non_cached;
34         uint32_t num_non_cached;
35
36         struct lsa_RefDomainList *domains;
37         struct lsa_TransNameArray *names;
38
39         struct wbint_TransIDArray ids;
40 };
41
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);
46
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)
51 {
52         struct tevent_req *req, *subreq;
53         struct winbindd_sids_to_xids_state *state;
54         uint32_t i;
55
56         req = tevent_req_create(mem_ctx, &state,
57                                 struct winbindd_sids_to_xids_state);
58         if (req == NULL) {
59                 return NULL;
60         }
61         state->ev = ev;
62
63         DEBUG(3, ("sids_to_xids\n"));
64
65         if (request->extra_len == 0) {
66                 tevent_req_done(req);
67                 return tevent_req_post(req, ev);
68         }
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);
73         }
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);
79         }
80
81         DEBUG(10, ("num_sids: %d\n", (int)state->num_sids));
82
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);
87         }
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);
92         }
93
94         for (i=0; i<state->num_sids; i++) {
95
96                 DEBUG(10, ("SID %d: %s\n", (int)i,
97                            sid_string_dbg(&state->sids[i])));
98
99                 if (winbindd_sids_to_xids_in_cache(&state->sids[i],
100                                                    &state->cached[i])) {
101                         continue;
102                 }
103                 sid_copy(&state->non_cached[state->num_non_cached],
104                          &state->sids[i]);
105                 state->num_non_cached += 1;
106         }
107
108         if (state->num_non_cached == 0) {
109                 tevent_req_done(req);
110                 return tevent_req_post(req, ev);
111         }
112
113         subreq = wb_lookupsids_send(state, ev, state->non_cached,
114                                     state->num_non_cached);
115         if (tevent_req_nomem(subreq, req)) {
116                 return tevent_req_post(req, ev);
117         }
118         tevent_req_set_callback(subreq, winbindd_sids_to_xids_lookupsids_done,
119                                 req);
120         return req;
121 }
122
123 static bool winbindd_sids_to_xids_in_cache(struct dom_sid *sid,
124                                            struct id_map *map)
125 {
126         uid_t uid;
127         gid_t gid;
128         bool expired;
129
130         if (!winbindd_use_idmap_cache()) {
131                 return false;
132         }
133         /*
134          * SIDS_TO_XIDS is primarily used to resolve the user's group
135          * sids. So we check groups before users.
136          */
137         if (idmap_cache_find_sid2gid(sid, &gid, &expired)) {
138                 if (expired && is_domain_offline(find_our_domain())) {
139                         return false;
140                 }
141                 map->sid = sid;
142                 map->xid.id = gid;
143                 map->xid.type = ID_TYPE_GID;
144                 map->status = ID_MAPPED;
145                 return true;
146         }
147         if (idmap_cache_find_sid2uid(sid, &uid, &expired)) {
148                 if (expired && is_domain_online(find_our_domain())) {
149                         return false;
150                 }
151                 map->sid = sid;
152                 map->xid.id = uid;
153                 map->xid.type = ID_TYPE_UID;
154                 map->status = ID_MAPPED;
155                 return true;
156         }
157         return false;
158 }
159
160
161 static void winbindd_sids_to_xids_lookupsids_done(struct tevent_req *subreq)
162 {
163         struct tevent_req *req = tevent_req_callback_data(
164                 subreq, struct tevent_req);
165         struct winbindd_sids_to_xids_state *state = tevent_req_data(
166                 req, struct winbindd_sids_to_xids_state);
167         struct winbindd_child *child;
168         NTSTATUS status;
169         int i;
170
171         status = wb_lookupsids_recv(subreq, state, &state->domains,
172                                     &state->names);
173         TALLOC_FREE(subreq);
174         if (tevent_req_nterror(req, status)) {
175                 return;
176         }
177
178         state->ids.num_ids = state->num_non_cached;
179         state->ids.ids = TALLOC_ARRAY(state, struct wbint_TransID,
180                                       state->num_non_cached);
181         if (tevent_req_nomem(state->ids.ids, req)) {
182                 return;
183         }
184
185         for (i=0; i<state->num_non_cached; i++) {
186                 struct lsa_TranslatedName *n = &state->names->names[i];
187                 struct wbint_TransID *t = &state->ids.ids[i];
188
189                 switch (n->sid_type) {
190                 case SID_NAME_USER:
191                 case SID_NAME_COMPUTER:
192                         t->type = WBC_ID_TYPE_UID;
193                         break;
194                 case SID_NAME_DOM_GRP:
195                 case SID_NAME_ALIAS:
196                 case SID_NAME_WKN_GRP:
197                         t->type = WBC_ID_TYPE_GID;
198                         break;
199                 default:
200                         t->type = WBC_ID_TYPE_NOT_SPECIFIED;
201                         break;
202                 };
203                 t->domain_index = n->sid_index;
204                 sid_peek_rid(&state->sids[i], &t->rid);
205                 t->unix_id = (uint64_t)-1;
206         }
207
208         child = idmap_child();
209
210         subreq = dcerpc_wbint_Sids2UnixIDs_send(
211                 state, state->ev, child->binding_handle, state->domains,
212                 &state->ids);
213         if (tevent_req_nomem(subreq, req)) {
214                 return;
215         }
216         tevent_req_set_callback(subreq, winbindd_sids_to_xids_done, req);
217 }
218
219 static void winbindd_sids_to_xids_done(struct tevent_req *subreq)
220 {
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;
226
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;
232         }
233         tevent_req_done(req);
234 }
235
236 NTSTATUS winbindd_sids_to_xids_recv(struct tevent_req *req,
237                                     struct winbindd_response *response)
238 {
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;
243         uint32_t i, num_non_cached;
244
245         if (tevent_req_is_nterror(req, &status)) {
246                 DEBUG(5, ("wb_sids_to_xids failed: %s\n", nt_errstr(status)));
247                 return status;
248         }
249
250         result = talloc_strdup(response, "");
251         if (result == NULL) {
252                 return NT_STATUS_NO_MEMORY;
253         }
254
255         num_non_cached = 0;
256
257         for (i=0; i<state->num_sids; i++) {
258                 char type;
259                 uint64_t unix_id = (uint64_t)-1;
260                 bool found = true;
261
262                 if (state->cached[i].sid != NULL) {
263                         unix_id = state->cached[i].xid.id;
264                         if (state->cached[i].xid.type == ID_TYPE_UID) {
265                                 type = 'U';
266                         } else {
267                                 type = 'G';
268                         }
269                 } else {
270                         unix_id = state->ids.ids[num_non_cached].unix_id;
271                         switch(state->ids.ids[num_non_cached].type) {
272                         case WBC_ID_TYPE_UID:
273                                 type = 'U';
274                                 idmap_cache_set_sid2uid(
275                                         &state->non_cached[num_non_cached],
276                                         unix_id);
277                                 break;
278                         case WBC_ID_TYPE_GID:
279                                 type = 'G';
280                                 idmap_cache_set_sid2gid(
281                                         &state->non_cached[num_non_cached],
282                                         unix_id);
283                                 break;
284                         default:
285                                 found = false;
286                         }
287                         num_non_cached += 1;
288                 }
289
290                 if (found) {
291                         result = talloc_asprintf_append_buffer(
292                                 result, "%c%lu\n", type,
293                                 (unsigned long)unix_id);
294                 } else {
295                         result = talloc_asprintf_append_buffer(result, "\n");
296                 }
297                 if (result == NULL) {
298                         return NT_STATUS_NO_MEMORY;
299                 }
300         }
301
302         response->extra_data.data = result;
303         response->length += talloc_get_size(result);
304         return NT_STATUS_OK;
305 }