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