winbindd: Do not use group_list->out.resume_index after free
[samba.git] / source4 / winbind / wb_sid2domain.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Find and init a domain struct for a SID
5
6    Copyright (C) Volker Lendecke 2005
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include <tevent.h>
24 #include "../lib/util/tevent_ntstatus.h"
25 #include "libcli/composite/composite.h"
26 #include "winbind/wb_server.h"
27 #include "smbd/service_task.h"
28 #include "libcli/security/security.h"
29 #include "../lib/util/dlinklist.h"
30 #include "param/param.h"
31
32 static struct wbsrv_domain *find_domain_from_sid(struct wbsrv_service *service,
33                                                  const struct dom_sid *sid)
34 {
35         struct wbsrv_domain *domain;
36
37         for (domain = service->domains; domain!=NULL; domain = domain->next) {
38                 if (dom_sid_equal(domain->info->sid, sid)) {
39                         break;
40                 }
41                 if (dom_sid_in_domain(domain->info->sid, sid)) {
42                         break;
43                 }
44         }
45         return domain;
46 }
47
48 struct wb_sid2domain_state {
49         struct wbsrv_service *service;
50         struct dom_sid sid;
51
52         struct wbsrv_domain *domain;
53 };
54
55 static void wb_sid2domain_recv_dom_info(struct composite_context *ctx);
56 static void wb_sid2domain_recv_name(struct composite_context *ctx);
57 static void wb_sid2domain_recv_trusted_dom_info(struct composite_context *ctx);
58 static void wb_sid2domain_recv_init(struct composite_context *ctx);
59
60 static struct tevent_req *_wb_sid2domain_send(TALLOC_CTX *mem_ctx,
61                                               struct tevent_context *ev,
62                                               struct wbsrv_service *service,
63                                               const struct dom_sid *sid)
64 {
65         struct tevent_req *req;
66         struct wb_sid2domain_state *state;
67         struct composite_context *ctx;
68
69         DEBUG(5, ("wb_sid2domain_send called\n"));
70
71         req = tevent_req_create(mem_ctx, &state,
72                                 struct wb_sid2domain_state);
73         if (req == NULL) {
74                 return NULL;
75         }
76
77         state->service = service;
78         state->sid = *sid;
79
80         state->domain = find_domain_from_sid(service, sid);
81         if (state->domain != NULL) {
82                 tevent_req_done(req);
83                 return tevent_req_post(req, ev);
84         }
85
86         if (dom_sid_equal(service->primary_sid, sid) ||
87             dom_sid_in_domain(service->primary_sid, sid)) {
88                 ctx = wb_get_dom_info_send(state, service,
89                                            lpcfg_workgroup(service->task->lp_ctx),
90                                            lpcfg_realm(service->task->lp_ctx),
91                                            service->primary_sid);
92                 if (tevent_req_nomem(ctx, req)) {
93                         return tevent_req_post(req, ev);
94                 }
95                 ctx->async.fn = wb_sid2domain_recv_dom_info;
96                 ctx->async.private_data = req;
97
98                 return req;
99         }
100
101         if (dom_sid_equal(&global_sid_Builtin, sid) ||
102             dom_sid_in_domain(&global_sid_Builtin, sid)) {
103                 ctx = wb_get_dom_info_send(state, service,
104                                            "BUILTIN", NULL,
105                                            &global_sid_Builtin);
106                 if (tevent_req_nomem(ctx, req)) {
107                         return tevent_req_post(req, ev);
108                 }
109                 ctx->async.fn = wb_sid2domain_recv_dom_info;
110                 ctx->async.private_data = req;
111
112                 return req;
113         }
114
115         ctx = wb_cmd_lookupsid_send(state, service, &state->sid);
116         if (tevent_req_nomem(ctx, req)) {
117                 return tevent_req_post(req, ev);
118         }
119         ctx->async.fn = wb_sid2domain_recv_name;
120         ctx->async.private_data = req;
121
122         return req;
123 }
124
125 static void wb_sid2domain_recv_dom_info(struct composite_context *ctx)
126 {
127         struct tevent_req *req =
128                 talloc_get_type_abort(ctx->async.private_data,
129                 struct tevent_req);
130         struct wb_sid2domain_state *state =
131                 tevent_req_data(req,
132                 struct wb_sid2domain_state);
133         struct wb_dom_info *info;
134         NTSTATUS status;
135
136         status = wb_get_dom_info_recv(ctx, state, &info);
137         if (tevent_req_nterror(req, status)) {
138                 return;
139         }
140
141         ctx = wb_init_domain_send(state, state->service, info);
142         if (tevent_req_nomem(ctx, req)) {
143                 return;
144         }
145         ctx->async.fn = wb_sid2domain_recv_init;
146         ctx->async.private_data = req;
147 }
148
149 static void wb_sid2domain_recv_name(struct composite_context *ctx)
150 {
151         struct tevent_req *req =
152                 talloc_get_type_abort(ctx->async.private_data,
153                 struct tevent_req);
154         struct wb_sid2domain_state *state =
155                 tevent_req_data(req,
156                 struct wb_sid2domain_state);
157         struct wb_sid_object *name;
158         NTSTATUS status;
159
160         status = wb_cmd_lookupsid_recv(ctx, state, &name);
161         if (tevent_req_nterror(req, status)) {
162                 return;
163         }
164
165         if (name->type == SID_NAME_UNKNOWN) {
166                 tevent_req_nterror(req, NT_STATUS_NO_SUCH_DOMAIN);
167                 return;
168         }
169
170         if (name->type != SID_NAME_DOMAIN) {
171                 state->sid.num_auths -= 1;
172         }
173
174         ctx = wb_trusted_dom_info_send(state, state->service, name->domain,
175                                        &state->sid);
176         if (tevent_req_nomem(ctx, req)) {
177                 return;
178         }
179         ctx->async.fn = wb_sid2domain_recv_trusted_dom_info;
180         ctx->async.private_data = req;
181 }
182
183 static void wb_sid2domain_recv_trusted_dom_info(struct composite_context *ctx)
184 {
185         struct tevent_req *req =
186                 talloc_get_type_abort(ctx->async.private_data,
187                 struct tevent_req);
188         struct wb_sid2domain_state *state =
189                 tevent_req_data(req,
190                 struct wb_sid2domain_state);
191         struct wb_dom_info *info;
192         NTSTATUS status;
193
194         status = wb_trusted_dom_info_recv(ctx, state, &info);
195         if (tevent_req_nterror(req, status)) {
196                 return;
197         }
198
199         ctx = wb_init_domain_send(state, state->service, info);
200         if (tevent_req_nomem(ctx, req)) {
201                 return;
202         }
203         ctx->async.fn = wb_sid2domain_recv_init;
204         ctx->async.private_data = req;
205 }
206
207 static void wb_sid2domain_recv_init(struct composite_context *ctx)
208 {
209         struct tevent_req *req =
210                 talloc_get_type_abort(ctx->async.private_data,
211                 struct tevent_req);
212         struct wb_sid2domain_state *state =
213                 tevent_req_data(req,
214                 struct wb_sid2domain_state);
215         struct wbsrv_domain *existing;
216         NTSTATUS status;
217
218         status = wb_init_domain_recv(ctx, state, &state->domain);
219         if (tevent_req_nterror(req, status)) {
220                 DEBUG(10, ("Could not init domain\n"));
221                 return;
222         }
223
224         existing = find_domain_from_sid(state->service, &state->sid);
225         if (existing != NULL) {
226                 DEBUG(5, ("Initialized domain twice, dropping second one\n"));
227                 talloc_free(state->domain);
228                 state->domain = existing;
229         } else {
230                 talloc_steal(state->service, state->domain);
231                 DLIST_ADD(state->service->domains, state->domain);
232         }
233
234         tevent_req_done(req);
235 }
236
237 static NTSTATUS _wb_sid2domain_recv(struct tevent_req *req,
238                                     struct wbsrv_domain **result)
239 {
240         struct wb_sid2domain_state *state =
241                 tevent_req_data(req,
242                 struct wb_sid2domain_state);
243         NTSTATUS status;
244
245         if (tevent_req_is_nterror(req, &status)) {
246                 tevent_req_received(req);
247                 return status;
248         }
249
250         *result = state->domain;
251         tevent_req_received(req);
252         return NT_STATUS_OK;
253 }
254
255 struct sid2domain_state {
256         struct composite_context *ctx;
257         struct wbsrv_domain *domain;
258 };
259
260 static void sid2domain_recv_domain(struct tevent_req *subreq);
261
262 struct composite_context *wb_sid2domain_send(TALLOC_CTX *mem_ctx,
263                                              struct wbsrv_service *service,
264                                              const struct dom_sid *sid)
265 {
266         struct composite_context *result;
267         struct sid2domain_state *state;
268         struct tevent_req *subreq;
269
270         DEBUG(5, ("wb_sid2domain_send called\n"));
271         result = composite_create(mem_ctx, service->task->event_ctx);
272         if (result == NULL) goto failed;
273
274         state = talloc(result, struct sid2domain_state);
275         if (state == NULL) goto failed;
276         state->ctx = result;
277         result->private_data = state;
278
279         subreq = _wb_sid2domain_send(state,
280                                      result->event_ctx,
281                                      service, sid);
282         if (subreq == NULL) goto failed;
283         tevent_req_set_callback(subreq, sid2domain_recv_domain, state);
284
285         return result;
286
287  failed:
288         talloc_free(result);
289         return NULL;
290
291 }
292
293 static void sid2domain_recv_domain(struct tevent_req *subreq)
294 {
295         struct sid2domain_state *state =
296                 tevent_req_callback_data(subreq,
297                                 struct sid2domain_state);
298
299         state->ctx->status = _wb_sid2domain_recv(subreq, &state->domain);
300         TALLOC_FREE(subreq);
301         if (!composite_is_ok(state->ctx)) return;
302
303         composite_done(state->ctx);
304 }
305
306 NTSTATUS wb_sid2domain_recv(struct composite_context *ctx,
307                             struct wbsrv_domain **result)
308 {
309         NTSTATUS status = composite_wait(ctx);
310         if (NT_STATUS_IS_OK(status)) {
311                 struct sid2domain_state *state =
312                         talloc_get_type(ctx->private_data,
313                                         struct sid2domain_state);
314                 *result = state->domain;
315         }
316         talloc_free(ctx);
317         return status;
318 }
319
320 NTSTATUS wb_sid2domain(TALLOC_CTX *mem_ctx, struct wbsrv_service *service,
321                        const struct dom_sid *sid,
322                        struct wbsrv_domain **result)
323 {
324         struct composite_context *c = wb_sid2domain_send(mem_ctx, service,
325                                                          sid);
326         return wb_sid2domain_recv(c, result);
327 }