516f8a115e0d259c379b16a153ce514b838871a4
[kai/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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "libcli/composite/composite.h"
25 #include "winbind/wb_server.h"
26 #include "smbd/service_task.h"
27 #include "winbind/wb_async_helpers.h"
28 #include "libcli/security/security.h"
29 #include "lib/util/dlinklist.h"
30
31 static struct wbsrv_domain *find_domain_from_sid(struct wbsrv_service *service,
32                                                  const struct dom_sid *sid)
33 {
34         struct wbsrv_domain *domain;
35
36         for (domain = service->domains; domain!=NULL; domain = domain->next) {
37                 if (dom_sid_equal(domain->info->sid, sid)) {
38                         break;
39                 }
40                 if (dom_sid_in_domain(domain->info->sid, sid)) {
41                         break;
42                 }
43         }
44         return domain;
45 }
46
47 struct sid2domain_state {
48         struct composite_context *ctx;
49         struct wbsrv_service *service;
50         struct dom_sid *sid;
51
52         struct wbsrv_domain *domain;
53 };
54
55 static void sid2domain_recv_dom_info(struct composite_context *ctx);
56 static void sid2domain_recv_name(struct composite_context *ctx);
57 static void sid2domain_recv_trusted_dom_info(struct composite_context *ctx);
58 static void sid2domain_recv_init(struct composite_context *ctx);
59
60 struct composite_context *wb_sid2domain_send(TALLOC_CTX *mem_ctx,
61                                              struct wbsrv_service *service,
62                                              const struct dom_sid *sid)
63 {
64         struct composite_context *result, *ctx;
65         struct sid2domain_state *state;
66
67         result = talloc_zero(mem_ctx, struct composite_context);
68         if (result == NULL) goto failed;
69         result->state = COMPOSITE_STATE_IN_PROGRESS;
70         result->async.fn = NULL;
71         result->event_ctx = service->task->event_ctx;
72
73         state = talloc(result, struct sid2domain_state);
74         if (state == NULL) goto failed;
75         state->ctx = result;
76         result->private_data = state;
77
78         state->service = service;
79         state->sid = dom_sid_dup(state, sid);
80         if (state->sid == NULL) goto failed;
81
82         state->domain = find_domain_from_sid(service, sid);
83         if (state->domain != NULL) {
84                 result->status = NT_STATUS_OK;
85                 composite_done(result);
86                 return result;
87         }
88
89         if (dom_sid_equal(service->primary_sid, sid) ||
90             dom_sid_in_domain(service->primary_sid, sid)) {
91                 ctx = wb_get_dom_info_send(state, service, lp_workgroup(),
92                                            service->primary_sid);
93                 if (ctx == NULL) goto failed;
94                 ctx->async.fn = sid2domain_recv_dom_info;
95                 ctx->async.private_data = state;
96                 return result;
97         }
98
99         ctx = wb_cmd_lookupsid_send(state, service, state->sid);
100         if (ctx == NULL) goto failed;
101         ctx->async.fn = sid2domain_recv_name;
102         ctx->async.private_data = state;
103         return result;
104
105  failed:
106         talloc_free(result);
107         return NULL;
108
109 }
110
111 static void sid2domain_recv_dom_info(struct composite_context *ctx)
112 {
113         struct sid2domain_state *state =
114                 talloc_get_type(ctx->async.private_data,
115                                 struct sid2domain_state);
116         struct wb_dom_info *info;
117
118         state->ctx->status = wb_get_dom_info_recv(ctx, state, &info);
119         if (!composite_is_ok(state->ctx)) return;
120
121         ctx = wb_init_domain_send(state, state->service, info);
122
123         composite_continue(state->ctx, ctx, sid2domain_recv_init, state);
124 }
125
126 static void sid2domain_recv_name(struct composite_context *ctx)
127 {
128         struct sid2domain_state *state =
129                 talloc_get_type(ctx->async.private_data,
130                                 struct sid2domain_state);
131         struct wb_sid_object *name;
132
133         state->ctx->status = wb_cmd_lookupsid_recv(ctx, state, &name);
134         if (!composite_is_ok(state->ctx)) return;
135
136         if (name->type == SID_NAME_UNKNOWN) {
137                 composite_error(state->ctx, NT_STATUS_NO_SUCH_DOMAIN);
138                 return;
139         }
140
141         if (name->type != SID_NAME_DOMAIN) {
142                 state->sid->num_auths -= 1;
143         }
144
145         ctx = wb_trusted_dom_info_send(state, state->service, name->domain,
146                                        state->sid);
147
148         composite_continue(state->ctx, ctx, sid2domain_recv_trusted_dom_info,
149                            state);
150 }
151
152 static void sid2domain_recv_trusted_dom_info(struct composite_context *ctx)
153 {
154         struct sid2domain_state *state =
155                 talloc_get_type(ctx->async.private_data,
156                                 struct sid2domain_state);
157         struct wb_dom_info *info;
158
159         state->ctx->status = wb_trusted_dom_info_recv(ctx, state, &info);
160         if (!composite_is_ok(state->ctx)) return;
161
162         ctx = wb_init_domain_send(state, state->service, info);
163
164         composite_continue(state->ctx, ctx, sid2domain_recv_init, state);
165 }
166
167 static void sid2domain_recv_init(struct composite_context *ctx)
168 {
169         struct sid2domain_state *state =
170                 talloc_get_type(ctx->async.private_data,
171                                 struct sid2domain_state);
172         struct wbsrv_domain *existing;
173
174         state->ctx->status = wb_init_domain_recv(ctx, state,
175                                                  &state->domain);
176         if (!composite_is_ok(state->ctx)) {
177                 DEBUG(10, ("Could not init domain\n"));
178                 return;
179         }
180
181         existing = find_domain_from_sid(state->service, state->sid);
182         if (existing != NULL) {
183                 DEBUG(5, ("Initialized domain twice, dropping second one\n"));
184                 talloc_free(state->domain);
185                 state->domain = existing;
186         }
187
188         talloc_steal(state->service, state->domain);
189         DLIST_ADD(state->service->domains, state->domain);
190
191         composite_done(state->ctx);
192 }
193
194 NTSTATUS wb_sid2domain_recv(struct composite_context *ctx,
195                             struct wbsrv_domain **result)
196 {
197         NTSTATUS status = composite_wait(ctx);
198         if (NT_STATUS_IS_OK(status)) {
199                 struct sid2domain_state *state =
200                         talloc_get_type(ctx->private_data,
201                                         struct sid2domain_state);
202                 *result = state->domain;
203         }
204         talloc_free(ctx);
205         return status;
206 }
207
208 NTSTATUS wb_sid2domain(TALLOC_CTX *mem_ctx, struct wbsrv_service *service,
209                        const struct dom_sid *sid,
210                        struct wbsrv_domain **result)
211 {
212         struct composite_context *c = wb_sid2domain_send(mem_ctx, service,
213                                                          sid);
214         return wb_sid2domain_recv(c, result);
215 }