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