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