r12608: Remove some unused #include lines.
[jelmer/samba4-debian.git] / source / winbind / wb_init_domain.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    A composite API for initializing a domain
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 "libcli/smb_composite/smb_composite.h"
26 #include "winbind/wb_server.h"
27 #include "smbd/service_task.h"
28
29 #include "libcli/auth/credentials.h"
30
31 #include "libcli/ldap/ldap_client.h"
32
33
34 /*
35  * Initialize a domain:
36  *
37  * - With schannel credentials, try to open the SMB connection with the
38  *   machine creds. This works against W2k3SP1 with an NTLMSSP session
39  *   setup. Fall back to anonymous.
40  *
41  * - If we have schannel creds, do the auth2 and open the schannel'ed netlogon
42  *   pipe.
43  *
44  * - Open LSA. If we have machine creds, try to open with ntlmssp. Fall back
45  *   to schannel and then to anon bind.
46  *
47  * - With queryinfopolicy, verify that we're talking to the right domain
48  *
49  * A bit complex, but with all the combinations I think it's the best we can
50  * get. NT4, W2k3 and W2k all have different combinations, but in the end we
51  * have a signed&sealed lsa connection on all of them.
52  *
53  * Not sure if it is overkill, but it seems to work.
54  */
55
56 struct init_domain_state {
57         struct composite_context *ctx;
58         struct wbsrv_domain *domain;
59         struct wbsrv_service *service;
60
61         struct smb_composite_connect conn;
62
63         struct lsa_QueryInfoPolicy queryinfo;
64 };
65
66 static void init_domain_recv_tree(struct composite_context *ctx);
67 static void init_domain_recv_netlogoncreds(struct composite_context *ctx);
68 static void init_domain_recv_netlogonpipe(struct composite_context *ctx);
69 static void init_domain_recv_schannel(struct composite_context *ctx);
70 static void init_domain_recv_lsa(struct composite_context *ctx);
71 static void init_domain_recv_queryinfo(struct rpc_request *req);
72 static void init_domain_recv_ldapconn(struct composite_context *ctx);
73 static void init_domain_recv_samr(struct composite_context *ctx);
74
75 struct composite_context *wb_init_domain_send(TALLOC_CTX *mem_ctx,
76                                               struct wbsrv_service *service,
77                                               struct wb_dom_info *dom_info)
78 {
79         struct composite_context *result, *ctx;
80         struct init_domain_state *state;
81
82         result = talloc(mem_ctx, struct composite_context);
83         if (result == NULL) goto failed;
84         result->state = COMPOSITE_STATE_IN_PROGRESS;
85         result->async.fn = NULL;
86         result->event_ctx = service->task->event_ctx;
87
88         state = talloc_zero(result, struct init_domain_state);
89         if (state == NULL) goto failed;
90         state->ctx = result;
91         result->private_data = state;
92
93         state->service = service;
94
95         state->domain = talloc(state, struct wbsrv_domain);
96         if (state->domain == NULL) goto failed;
97
98         state->domain->info = talloc_reference(state->domain, dom_info);
99         if (state->domain->info == NULL) goto failed;
100
101         state->domain->schannel_creds = cli_credentials_init(state->domain);
102         if (state->domain->schannel_creds == NULL) goto failed;
103
104         cli_credentials_set_conf(state->domain->schannel_creds);
105         state->ctx->status =
106                 cli_credentials_set_machine_account(state->domain->
107                                                     schannel_creds);
108         if (!NT_STATUS_IS_OK(state->ctx->status)) goto failed;
109
110         state->conn.in.dest_host = dom_info->dc_address;
111         state->conn.in.port = 0;
112         state->conn.in.called_name = dom_info->dc_name;
113         state->conn.in.service = "IPC$";
114         state->conn.in.service_type = "IPC";
115         state->conn.in.workgroup = dom_info->name;
116         state->conn.in.credentials = state->domain->schannel_creds;
117
118         state->conn.in.fallback_to_anonymous = True;
119
120         ctx = smb_composite_connect_send(&state->conn, state->domain,
121                                          result->event_ctx);
122         if (ctx == NULL) goto failed;
123
124         ctx->async.fn = init_domain_recv_tree;
125         ctx->async.private_data = state;
126         return result;
127
128  failed:
129         talloc_free(result);
130         return NULL;
131 }
132
133 static void init_domain_recv_tree(struct composite_context *ctx)
134 {
135         struct init_domain_state *state =
136                 talloc_get_type(ctx->async.private_data,
137                                 struct init_domain_state);
138         state->ctx->status = smb_composite_connect_recv(ctx, state);
139         if (!composite_is_ok(state->ctx)) return;
140
141         if ((state->domain->schannel_creds != NULL) &&
142             (!cli_credentials_is_anonymous(state->domain->schannel_creds)) &&
143             ((lp_server_role() == ROLE_DOMAIN_MEMBER) &&
144              (dom_sid_equal(state->domain->info->sid,
145                             state->service->primary_sid)))) {
146                 ctx = wb_get_schannel_creds_send(state,
147                                                  state->domain->schannel_creds,
148                                                  state->conn.out.tree,
149                                                  state->ctx->event_ctx);
150                 composite_continue(state->ctx, ctx,
151                                    init_domain_recv_netlogoncreds, state);
152                 return;
153         }
154
155         ctx = wb_connect_lsa_send(state, state->conn.out.tree, NULL);
156         composite_continue(state->ctx, ctx, init_domain_recv_lsa, state);
157 }
158
159 static void init_domain_recv_netlogoncreds(struct composite_context *ctx)
160 {
161         struct init_domain_state *state =
162                 talloc_get_type(ctx->async.private_data,
163                                 struct init_domain_state);
164         struct dcerpc_pipe *auth2_pipe;
165         struct smbcli_tree *tree = NULL;
166
167         state->ctx->status =
168                 wb_get_schannel_creds_recv(ctx, state, &auth2_pipe);
169         if (!composite_is_ok(state->ctx)) return;
170
171         if (!lp_winbind_sealed_pipes()) {
172                 state->domain->netlogon_pipe = talloc_reference(state->domain,
173                                                                 auth2_pipe);
174                 ctx = wb_connect_lsa_send(state, state->conn.out.tree, NULL);
175                 composite_continue(state->ctx, ctx, init_domain_recv_lsa,
176                                    state);
177                 return;
178         }
179
180         state->domain->netlogon_pipe =
181                 dcerpc_pipe_init(state->domain, state->ctx->event_ctx);
182         if (composite_nomem(state->domain->netlogon_pipe, state->ctx)) return;
183
184         tree = dcerpc_smb_tree(auth2_pipe->conn);
185         if (tree == NULL) {
186                 composite_error(state->ctx, NT_STATUS_INTERNAL_ERROR);
187                 return;
188         }
189
190         ctx = dcerpc_pipe_open_smb_send(state->domain->netlogon_pipe->conn,
191                                         tree, "\\netlogon");
192         composite_continue(state->ctx, ctx, init_domain_recv_netlogonpipe,
193                            state);
194 }
195
196 static void init_domain_recv_netlogonpipe(struct composite_context *ctx)
197 {
198         struct init_domain_state *state =
199                 talloc_get_type(ctx->async.private_data,
200                                 struct init_domain_state);
201
202         state->ctx->status = dcerpc_pipe_open_smb_recv(ctx);
203         if (!composite_is_ok(state->ctx)) return;
204
205         state->domain->netlogon_pipe->conn->flags |=
206                 (DCERPC_SIGN | DCERPC_SEAL);
207         ctx = dcerpc_bind_auth_send(state, state->domain->netlogon_pipe,
208                                                                 &dcerpc_table_netlogon,
209                                     state->domain->schannel_creds,
210                                     DCERPC_AUTH_TYPE_SCHANNEL,
211                                     NULL);
212         composite_continue(state->ctx, ctx, init_domain_recv_schannel, state);
213 }
214
215 static void init_domain_recv_schannel(struct composite_context *ctx)
216 {
217         struct init_domain_state *state =
218                 talloc_get_type(ctx->async.private_data,
219                                 struct init_domain_state);
220
221         state->ctx->status = dcerpc_bind_auth_recv(ctx);
222         if (!composite_is_ok(state->ctx)) return;
223
224         ctx = wb_connect_lsa_send(state, state->conn.out.tree,
225                                   state->domain->schannel_creds);
226         composite_continue(state->ctx, ctx, init_domain_recv_lsa, state);
227 }
228
229 static void init_domain_recv_lsa(struct composite_context *ctx)
230 {
231         struct init_domain_state *state =
232                 talloc_get_type(ctx->async.private_data,
233                                 struct init_domain_state);
234
235         struct rpc_request *req;
236
237         state->ctx->status = wb_connect_lsa_recv(ctx, state->domain,
238                                                  &state->domain->lsa_auth_type,
239                                                  &state->domain->lsa_pipe,
240                                                  &state->domain->lsa_policy);
241         if (!composite_is_ok(state->ctx)) return;
242
243         /* Give the tree to the pipes. */
244         talloc_unlink(state, state->conn.out.tree);
245
246         state->queryinfo.in.handle = state->domain->lsa_policy;
247         state->queryinfo.in.level = LSA_POLICY_INFO_ACCOUNT_DOMAIN;
248
249         req = dcerpc_lsa_QueryInfoPolicy_send(state->domain->lsa_pipe, state,
250                                               &state->queryinfo);
251         composite_continue_rpc(state->ctx, req,
252                                init_domain_recv_queryinfo, state);
253 }
254
255 static void init_domain_recv_queryinfo(struct rpc_request *req)
256 {
257         struct init_domain_state *state =
258                 talloc_get_type(req->async.private, struct init_domain_state);
259         struct lsa_DomainInfo *dominfo;
260         struct composite_context *ctx;
261         const char *ldap_url;
262
263         state->ctx->status = dcerpc_ndr_request_recv(req);
264         if (!composite_is_ok(state->ctx)) return;
265         state->ctx->status = state->queryinfo.out.result;
266         if (!composite_is_ok(state->ctx)) return;
267
268         dominfo = &state->queryinfo.out.info->account_domain;
269
270         if (strcasecmp(state->domain->info->name, dominfo->name.string) != 0) {
271                 DEBUG(2, ("Expected domain name %s, DC %s said %s\n",
272                           state->domain->info->name,
273                           dcerpc_server_name(state->domain->lsa_pipe),
274                           dominfo->name.string));
275                 composite_error(state->ctx, NT_STATUS_INVALID_DOMAIN_STATE);
276                 return;
277         }
278
279         if (!dom_sid_equal(state->domain->info->sid, dominfo->sid)) {
280                 DEBUG(2, ("Expected domain sid %s, DC %s said %s\n",
281                           dom_sid_string(state, state->domain->info->sid),
282                           dcerpc_server_name(state->domain->lsa_pipe),
283                           dom_sid_string(state, dominfo->sid)));
284                 composite_error(state->ctx, NT_STATUS_INVALID_DOMAIN_STATE);
285                 return;
286         }
287
288         state->domain->ldap_conn =
289                 ldap_new_connection(state->domain, state->ctx->event_ctx);
290         composite_nomem(state->domain->ldap_conn, state->ctx);
291
292         ldap_url = talloc_asprintf(state, "ldap://%s/",
293                                    state->domain->info->dc_address);
294         composite_nomem(ldap_url, state->ctx);
295
296         ctx = ldap_connect_send(state->domain->ldap_conn, ldap_url);
297         composite_continue(state->ctx, ctx, init_domain_recv_ldapconn, state);
298 }
299
300 static void init_domain_recv_ldapconn(struct composite_context *ctx)
301 {
302         struct init_domain_state *state =
303                 talloc_get_type(ctx->async.private_data,
304                                 struct init_domain_state);
305
306         state->ctx->status = ldap_connect_recv(ctx);
307         if (NT_STATUS_IS_OK(state->ctx->status)) {
308                 state->domain->ldap_conn->host =
309                         talloc_strdup(state->domain->ldap_conn,
310                                       state->domain->info->dc_name);
311                 state->ctx->status =
312                         ldap_bind_sasl(state->domain->ldap_conn,
313                                        state->domain->schannel_creds);
314                 DEBUG(0, ("ldap_bind returned %s\n",
315                           nt_errstr(state->ctx->status)));
316         }
317
318         state->domain->samr_pipe =
319                 dcerpc_pipe_init(state->domain, state->ctx->event_ctx);
320         if (composite_nomem(state->domain->samr_pipe, state->ctx)) return;
321
322         ctx = wb_connect_sam_send(state, state->conn.out.tree,
323                                   state->domain->lsa_auth_type,
324                                   state->domain->schannel_creds,
325                                   state->domain->info->sid);
326         composite_continue(state->ctx, ctx, init_domain_recv_samr, state);
327 }
328
329 static void init_domain_recv_samr(struct composite_context *ctx)
330 {
331         struct init_domain_state *state =
332                 talloc_get_type(ctx->async.private_data,
333                                 struct init_domain_state);
334
335         state->ctx->status = wb_connect_sam_recv(
336                 ctx, state->domain, &state->domain->samr_pipe,
337                 &state->domain->samr_handle, &state->domain->domain_handle);
338         if (!composite_is_ok(state->ctx)) return;
339
340         composite_done(state->ctx);
341 }
342
343 NTSTATUS wb_init_domain_recv(struct composite_context *c,
344                              TALLOC_CTX *mem_ctx,
345                              struct wbsrv_domain **result)
346 {
347         NTSTATUS status = composite_wait(c);
348         if (NT_STATUS_IS_OK(status)) {
349                 struct init_domain_state *state =
350                         talloc_get_type(c->private_data,
351                                         struct init_domain_state);
352                 *result = talloc_steal(mem_ctx, state->domain);
353         }
354         talloc_free(c);
355         return status;
356 }
357
358 NTSTATUS wb_init_domain(TALLOC_CTX *mem_ctx, struct wbsrv_service *service,
359                         struct wb_dom_info *dom_info,
360                         struct wbsrv_domain **result)
361 {
362         struct composite_context *c =
363                 wb_init_domain_send(mem_ctx, service, dom_info);
364         return wb_init_domain_recv(c, mem_ctx, result);
365 }