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