build: Change bin/default/python -> bin/python symlink to bin/default/python_modules
[nivanova/samba-autobuild/.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         ctx = wb_cmd_lookupsid_send(state, service, &state->sid);
102         if (tevent_req_nomem(ctx, req)) {
103                 return tevent_req_post(req, ev);
104         }
105         ctx->async.fn = wb_sid2domain_recv_name;
106         ctx->async.private_data = req;
107
108         return req;
109 }
110
111 static void wb_sid2domain_recv_dom_info(struct composite_context *ctx)
112 {
113         struct tevent_req *req =
114                 talloc_get_type_abort(ctx->async.private_data,
115                 struct tevent_req);
116         struct wb_sid2domain_state *state =
117                 tevent_req_data(req,
118                 struct wb_sid2domain_state);
119         struct wb_dom_info *info;
120         NTSTATUS status;
121
122         status = wb_get_dom_info_recv(ctx, state, &info);
123         if (tevent_req_nterror(req, status)) {
124                 return;
125         }
126
127         ctx = wb_init_domain_send(state, state->service, info);
128         if (tevent_req_nomem(ctx, req)) {
129                 return;
130         }
131         ctx->async.fn = wb_sid2domain_recv_init;
132         ctx->async.private_data = req;
133 }
134
135 static void wb_sid2domain_recv_name(struct composite_context *ctx)
136 {
137         struct tevent_req *req =
138                 talloc_get_type_abort(ctx->async.private_data,
139                 struct tevent_req);
140         struct wb_sid2domain_state *state =
141                 tevent_req_data(req,
142                 struct wb_sid2domain_state);
143         struct wb_sid_object *name;
144         NTSTATUS status;
145
146         status = wb_cmd_lookupsid_recv(ctx, state, &name);
147         if (tevent_req_nterror(req, status)) {
148                 return;
149         }
150
151         if (name->type == SID_NAME_UNKNOWN) {
152                 tevent_req_nterror(req, NT_STATUS_NO_SUCH_DOMAIN);
153                 return;
154         }
155
156         if (name->type != SID_NAME_DOMAIN) {
157                 state->sid.num_auths -= 1;
158         }
159
160         ctx = wb_trusted_dom_info_send(state, state->service, name->domain,
161                                        &state->sid);
162         if (tevent_req_nomem(ctx, req)) {
163                 return;
164         }
165         ctx->async.fn = wb_sid2domain_recv_trusted_dom_info;
166         ctx->async.private_data = req;
167 }
168
169 static void wb_sid2domain_recv_trusted_dom_info(struct composite_context *ctx)
170 {
171         struct tevent_req *req =
172                 talloc_get_type_abort(ctx->async.private_data,
173                 struct tevent_req);
174         struct wb_sid2domain_state *state =
175                 tevent_req_data(req,
176                 struct wb_sid2domain_state);
177         struct wb_dom_info *info;
178         NTSTATUS status;
179
180         status = wb_trusted_dom_info_recv(ctx, state, &info);
181         if (tevent_req_nterror(req, status)) {
182                 return;
183         }
184
185         ctx = wb_init_domain_send(state, state->service, info);
186         if (tevent_req_nomem(ctx, req)) {
187                 return;
188         }
189         ctx->async.fn = wb_sid2domain_recv_init;
190         ctx->async.private_data = req;
191 }
192
193 static void wb_sid2domain_recv_init(struct composite_context *ctx)
194 {
195         struct tevent_req *req =
196                 talloc_get_type_abort(ctx->async.private_data,
197                 struct tevent_req);
198         struct wb_sid2domain_state *state =
199                 tevent_req_data(req,
200                 struct wb_sid2domain_state);
201         struct wbsrv_domain *existing;
202         NTSTATUS status;
203
204         status = wb_init_domain_recv(ctx, state, &state->domain);
205         if (tevent_req_nterror(req, status)) {
206                 DEBUG(10, ("Could not init domain\n"));
207                 return;
208         }
209
210         existing = find_domain_from_sid(state->service, &state->sid);
211         if (existing != NULL) {
212                 DEBUG(5, ("Initialized domain twice, dropping second one\n"));
213                 talloc_free(state->domain);
214                 state->domain = existing;
215         } else {
216                 talloc_steal(state->service, state->domain);
217                 DLIST_ADD(state->service->domains, state->domain);
218         }
219
220         tevent_req_done(req);
221 }
222
223 static NTSTATUS _wb_sid2domain_recv(struct tevent_req *req,
224                                     struct wbsrv_domain **result)
225 {
226         struct wb_sid2domain_state *state =
227                 tevent_req_data(req,
228                 struct wb_sid2domain_state);
229         NTSTATUS status;
230
231         if (tevent_req_is_nterror(req, &status)) {
232                 tevent_req_received(req);
233                 return status;
234         }
235
236         *result = state->domain;
237         tevent_req_received(req);
238         return NT_STATUS_OK;
239 }
240
241 struct sid2domain_state {
242         struct composite_context *ctx;
243         struct wbsrv_domain *domain;
244 };
245
246 static void sid2domain_recv_domain(struct tevent_req *subreq);
247
248 struct composite_context *wb_sid2domain_send(TALLOC_CTX *mem_ctx,
249                                              struct wbsrv_service *service,
250                                              const struct dom_sid *sid)
251 {
252         struct composite_context *result;
253         struct sid2domain_state *state;
254         struct tevent_req *subreq;
255
256         DEBUG(5, ("wb_sid2domain_send called\n"));
257         result = composite_create(mem_ctx, service->task->event_ctx);
258         if (result == NULL) goto failed;
259
260         state = talloc(result, struct sid2domain_state);
261         if (state == NULL) goto failed;
262         state->ctx = result;
263         result->private_data = state;
264
265         subreq = _wb_sid2domain_send(state,
266                                      result->event_ctx,
267                                      service, sid);
268         if (subreq == NULL) goto failed;
269         tevent_req_set_callback(subreq, sid2domain_recv_domain, state);
270
271         return result;
272
273  failed:
274         talloc_free(result);
275         return NULL;
276
277 }
278
279 static void sid2domain_recv_domain(struct tevent_req *subreq)
280 {
281         struct sid2domain_state *state =
282                 tevent_req_callback_data(subreq,
283                                 struct sid2domain_state);
284
285         state->ctx->status = _wb_sid2domain_recv(subreq, &state->domain);
286         TALLOC_FREE(subreq);
287         if (!composite_is_ok(state->ctx)) return;
288
289         composite_done(state->ctx);
290 }
291
292 NTSTATUS wb_sid2domain_recv(struct composite_context *ctx,
293                             struct wbsrv_domain **result)
294 {
295         NTSTATUS status = composite_wait(ctx);
296         if (NT_STATUS_IS_OK(status)) {
297                 struct sid2domain_state *state =
298                         talloc_get_type(ctx->private_data,
299                                         struct sid2domain_state);
300                 *result = state->domain;
301         }
302         talloc_free(ctx);
303         return status;
304 }
305
306 NTSTATUS wb_sid2domain(TALLOC_CTX *mem_ctx, struct wbsrv_service *service,
307                        const struct dom_sid *sid,
308                        struct wbsrv_domain **result)
309 {
310         struct composite_context *c = wb_sid2domain_send(mem_ctx, service,
311                                                          sid);
312         return wb_sid2domain_recv(c, result);
313 }