r23141: Use the finddcs() library call rather than a winbind-specific version.
[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    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2007
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.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_samr_c.h"
34 #include "libcli/libcli.h"
35
36 #include "libcli/auth/credentials.h"
37 #include "libcli/security/security.h"
38
39 #include "libcli/ldap/ldap_client.h"
40
41 #include "auth/credentials/credentials.h"
42
43 /*
44  * Initialize a domain:
45  *
46  * - With schannel credentials, try to open the SMB connection and
47  *   NETLOGON pipe with the machine creds. This works against W2k3SP1
48  *   with an NTLMSSP session setup. Fall back to anonymous (for the CIFS level).
49  *
50  * - If we have schannel creds, do the auth2 and open the schannel'ed netlogon
51  *   pipe.
52  *
53  * - Open LSA. If we have machine creds, try to open with SPNEGO or NTLMSSP. Fall back
54  *   to schannel.
55  *
56  * - With queryinfopolicy, verify that we're talking to the right domain
57  *
58  * A bit complex, but with all the combinations I think it's the best we can
59  * get. NT4, W2k3 and W2k all have different combinations, but in the end we
60  * have a signed&sealed lsa connection on all of them.
61  *
62  * Not sure if it is overkill, but it seems to work.
63  */
64
65 struct init_domain_state {
66         struct composite_context *ctx;
67         struct wbsrv_domain *domain;
68         struct wbsrv_service *service;
69
70         struct lsa_ObjectAttribute objectattr;
71         struct lsa_OpenPolicy2 lsa_openpolicy;
72         struct lsa_QueryInfoPolicy queryinfo;
73 };
74
75 static void init_domain_recv_netlogonpipe(struct composite_context *ctx);
76 static void init_domain_recv_lsa_pipe(struct composite_context *ctx);
77 static void init_domain_recv_lsa_policy(struct rpc_request *req);
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 static struct dcerpc_binding *init_domain_binding(struct init_domain_state *state, 
83                                                   const struct dcerpc_interface_table *table) 
84 {
85         struct dcerpc_binding *binding;
86         NTSTATUS status;
87
88         /* Make a binding string */
89         {
90                 char *s = talloc_asprintf(state, "ncacn_np:%s", state->domain->dc_name);
91                 if (s == NULL) return NULL;
92                 status = dcerpc_parse_binding(state, s, &binding);
93                 talloc_free(s);
94                 if (!NT_STATUS_IS_OK(status)) {
95                         return NULL;
96                 }
97         }
98
99         /* Alter binding to contain hostname, but also address (so we don't look it up twice) */
100         binding->target_hostname = state->domain->dc_name;
101         binding->host = state->domain->dc_address;
102
103         /* This shouldn't make a network call, as the mappings for named pipes are well known */
104         status = dcerpc_epm_map_binding(binding, binding, table, state->service->task->event_ctx);
105         if (!NT_STATUS_IS_OK(status)) {
106                 return NULL;
107         }
108
109         return binding;
110 }
111
112 struct composite_context *wb_init_domain_send(TALLOC_CTX *mem_ctx,
113                                               struct wbsrv_service *service,
114                                               struct wb_dom_info *dom_info)
115 {
116         struct composite_context *result, *ctx;
117         struct init_domain_state *state;
118
119         result = composite_create(mem_ctx, service->task->event_ctx);
120         if (result == NULL) goto failed;
121
122         state = talloc_zero(result, struct init_domain_state);
123         if (state == NULL) goto failed;
124         state->ctx = result;
125         result->private_data = state;
126
127         state->service = service;
128
129         state->domain = talloc(state, struct wbsrv_domain);
130         if (state->domain == NULL) goto failed;
131
132         state->domain->info = talloc_reference(state->domain, dom_info);
133         if (state->domain->info == NULL) goto failed;
134
135         /* Caller should check, but to be safe: */
136         if (dom_info->num_dcs < 1) {
137                 goto failed;
138         }
139         
140         /* For now, we just pick the first.  The next step will be to
141          * walk the entire list.  Also need to fix finddcs() to return
142          * the entire list */
143         state->domain->dc_name = dom_info->dcs[0].name;
144         state->domain->dc_address = dom_info->dcs[0].address;
145
146         /* Create a credentials structure */
147         state->domain->schannel_creds = cli_credentials_init(state->domain);
148         if (state->domain->schannel_creds == NULL) goto failed;
149
150         cli_credentials_set_event_context(state->domain->schannel_creds, service->task->event_ctx);
151
152         cli_credentials_set_conf(state->domain->schannel_creds);
153
154         /* Connect the machine account to the credentials */
155         state->ctx->status =
156                 cli_credentials_set_machine_account(state->domain->
157                                                     schannel_creds);
158         if (!NT_STATUS_IS_OK(state->ctx->status)) goto failed;
159
160         state->domain->netlogon_binding = init_domain_binding(state, &dcerpc_table_netlogon);
161
162         state->domain->netlogon_pipe = NULL;
163
164         if ((!cli_credentials_is_anonymous(state->domain->schannel_creds)) &&
165             ((lp_server_role() == ROLE_DOMAIN_MEMBER) &&
166              (dom_sid_equal(state->domain->info->sid,
167                             state->service->primary_sid)))) {
168                 state->domain->netlogon_binding->flags |= DCERPC_SCHANNEL;
169
170                 /* For debugging, it can be a real pain if all the traffic is encrypted */
171                 if (lp_winbind_sealed_pipes()) {
172                         state->domain->netlogon_binding->flags |= (DCERPC_SIGN | DCERPC_SEAL );
173                 } else {
174                         state->domain->netlogon_binding->flags |= (DCERPC_SIGN);
175                 }
176         }
177
178         /* No encryption on anonymous pipes */
179
180         ctx = dcerpc_pipe_connect_b_send(state, state->domain->netlogon_binding, 
181                                          &dcerpc_table_netlogon,
182                                          state->domain->schannel_creds,
183                                          service->task->event_ctx);
184         
185         if (composite_nomem(ctx, state->ctx)) {
186                 goto failed;
187         }
188         
189         composite_continue(state->ctx, ctx, init_domain_recv_netlogonpipe,
190                            state);
191         return result;
192  failed:
193         talloc_free(result);
194         return NULL;
195 }
196
197 /* Having make a netlogon connection (possibly secured with schannel),
198  * make an LSA connection to the same DC, on the same IPC$ share */
199 static void init_domain_recv_netlogonpipe(struct composite_context *ctx)
200 {
201         struct init_domain_state *state =
202                 talloc_get_type(ctx->async.private_data,
203                                 struct init_domain_state);
204
205         state->ctx->status = dcerpc_pipe_connect_b_recv(ctx, state, 
206                                                    &state->domain->netlogon_pipe);
207         
208         if (!composite_is_ok(state->ctx)) {
209                 talloc_free(state->domain->netlogon_binding);
210                 return;
211         }
212         talloc_steal(state->domain->netlogon_pipe, state->domain->netlogon_binding);
213
214         state->domain->lsa_binding = init_domain_binding(state, &dcerpc_table_lsarpc);
215
216         /* For debugging, it can be a real pain if all the traffic is encrypted */
217         if (lp_winbind_sealed_pipes()) {
218                 state->domain->lsa_binding->flags |= (DCERPC_SIGN | DCERPC_SEAL );
219         } else {
220                 state->domain->lsa_binding->flags |= (DCERPC_SIGN);
221         }
222
223         state->domain->lsa_pipe = NULL;
224
225         /* this will make the secondary connection on the same IPC$ share, 
226            secured with SPNEGO or NTLMSSP */
227         ctx = dcerpc_secondary_connection_send(state->domain->netlogon_pipe,
228                                                state->domain->lsa_binding);
229         composite_continue(state->ctx, ctx, init_domain_recv_lsa_pipe, state);
230 }
231
232 static bool retry_with_schannel(struct init_domain_state *state, 
233                                 struct dcerpc_binding *binding,
234                                 void (*continuation)(struct composite_context *))
235 {
236         struct composite_context *ctx;
237         if (state->domain->netlogon_binding->flags & DCERPC_SCHANNEL 
238             && !(binding->flags & DCERPC_SCHANNEL)) {
239                 /* Opening a policy handle failed, perhaps it was
240                  * because we don't get a 'wrong password' error on
241                  * NTLMSSP binds */
242
243                 /* Try again with schannel */
244                 binding->flags |= DCERPC_SCHANNEL;
245
246                 /* Try again, likewise on the same IPC$ share, 
247                    secured with SCHANNEL */
248                 ctx = dcerpc_secondary_connection_send(state->domain->netlogon_pipe,
249                                                        binding);
250                 composite_continue(state->ctx, ctx, continuation, state);               
251                 return true;
252         } else {
253                 return false;
254         }
255 }
256 /* We should now have either an authenticated LSA pipe, or an error.  
257  * On success, open a policy handle
258  */     
259 static void init_domain_recv_lsa_pipe(struct composite_context *ctx)
260 {
261         struct rpc_request *req;
262         struct init_domain_state *state =
263                 talloc_get_type(ctx->async.private_data,
264                                 struct init_domain_state);
265
266         state->ctx->status = dcerpc_secondary_connection_recv(ctx, 
267                                                               &state->domain->lsa_pipe);
268         if (NT_STATUS_EQUAL(state->ctx->status, NT_STATUS_LOGON_FAILURE)) {
269                 if (retry_with_schannel(state, state->domain->lsa_binding, 
270                                         init_domain_recv_lsa_pipe)) {
271                         return;
272                 }
273         }
274         if (!composite_is_ok(state->ctx)) return;
275
276         talloc_steal(state->domain, state->domain->lsa_pipe);
277         talloc_steal(state->domain->lsa_pipe, state->domain->lsa_binding);
278
279         state->domain->lsa_policy_handle = talloc(state, struct policy_handle);
280         if (composite_nomem(state->domain->lsa_policy_handle, state->ctx)) return;
281
282         state->lsa_openpolicy.in.system_name =
283                 talloc_asprintf(state, "\\\\%s",
284                                 dcerpc_server_name(state->domain->lsa_pipe));
285         ZERO_STRUCT(state->objectattr);
286         state->lsa_openpolicy.in.attr = &state->objectattr;
287         state->lsa_openpolicy.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
288         state->lsa_openpolicy.out.handle = state->domain->lsa_policy_handle;
289
290         req = dcerpc_lsa_OpenPolicy2_send(state->domain->lsa_pipe, state,
291                                           &state->lsa_openpolicy);
292
293         composite_continue_rpc(state->ctx, req, init_domain_recv_lsa_policy, state);
294 }
295
296 /* Receive a policy handle (or not, and retry the authentication) and
297  * obtain some basic information about the domain */
298
299 static void init_domain_recv_lsa_policy(struct rpc_request *req)
300 {
301         struct init_domain_state *state =
302                 talloc_get_type(req->async.private_data,
303                                 struct init_domain_state);
304
305         state->ctx->status = dcerpc_ndr_request_recv(req);
306         if (!(NT_STATUS_IS_OK(state->ctx->status)
307               && NT_STATUS_IS_OK(state->lsa_openpolicy.out.result))) {
308                 if (retry_with_schannel(state, state->domain->lsa_binding, 
309                                         init_domain_recv_lsa_pipe)) {
310                         return;
311                 }
312         }
313         if (!composite_is_ok(state->ctx)) return;
314         state->ctx->status = state->lsa_openpolicy.out.result;
315         if (!composite_is_ok(state->ctx)) return;
316
317         state->queryinfo.in.handle = state->domain->lsa_policy_handle;
318         state->queryinfo.in.level = LSA_POLICY_INFO_ACCOUNT_DOMAIN;
319
320         req = dcerpc_lsa_QueryInfoPolicy_send(state->domain->lsa_pipe, state,
321                                               &state->queryinfo);
322         composite_continue_rpc(state->ctx, req,
323                                init_domain_recv_queryinfo, state);
324 }
325
326 static void init_domain_recv_queryinfo(struct rpc_request *req)
327 {
328         struct init_domain_state *state =
329                 talloc_get_type(req->async.private_data, struct init_domain_state);
330         struct lsa_DomainInfo *dominfo;
331         struct composite_context *ctx;
332
333         state->ctx->status = dcerpc_ndr_request_recv(req);
334         if (!composite_is_ok(state->ctx)) return;
335         state->ctx->status = state->queryinfo.out.result;
336         if (!composite_is_ok(state->ctx)) return;
337
338         dominfo = &state->queryinfo.out.info->account_domain;
339
340         if (strcasecmp(state->domain->info->name, dominfo->name.string) != 0) {
341                 DEBUG(2, ("Expected domain name %s, DC %s said %s\n",
342                           state->domain->info->name,
343                           dcerpc_server_name(state->domain->lsa_pipe),
344                           dominfo->name.string));
345                 composite_error(state->ctx, NT_STATUS_INVALID_DOMAIN_STATE);
346                 return;
347         }
348
349         if (!dom_sid_equal(state->domain->info->sid, dominfo->sid)) {
350                 DEBUG(2, ("Expected domain sid %s, DC %s said %s\n",
351                           dom_sid_string(state, state->domain->info->sid),
352                           dcerpc_server_name(state->domain->lsa_pipe),
353                           dom_sid_string(state, dominfo->sid)));
354                 composite_error(state->ctx, NT_STATUS_INVALID_DOMAIN_STATE);
355                 return;
356         }
357
358         state->domain->samr_binding = init_domain_binding(state, &dcerpc_table_samr);
359
360         /* We want to use the same flags as the LSA pipe did (so, if
361          * it needed schannel, then we need that here too) */
362         state->domain->samr_binding->flags = state->domain->lsa_binding->flags;
363
364         state->domain->samr_pipe = NULL;
365
366         ctx = wb_connect_samr_send(state, state->domain);
367         composite_continue(state->ctx, ctx, init_domain_recv_samr, state);
368 }
369
370 /* Recv the SAMR details (SamrConnect and SamrOpenDomain handle) and
371  * open an LDAP connection */
372 static void init_domain_recv_samr(struct composite_context *ctx)
373 {
374         const char *ldap_url;
375         struct init_domain_state *state =
376                 talloc_get_type(ctx->async.private_data,
377                                 struct init_domain_state);
378
379         state->ctx->status = wb_connect_samr_recv(
380                 ctx, state->domain,
381                 &state->domain->samr_pipe,
382                 &state->domain->samr_handle, 
383                 &state->domain->domain_handle);
384         if (!composite_is_ok(state->ctx)) return;
385
386         talloc_steal(state->domain->samr_pipe, state->domain->samr_binding);
387
388         state->domain->ldap_conn =
389                 ldap4_new_connection(state->domain, state->ctx->event_ctx);
390         composite_nomem(state->domain->ldap_conn, state->ctx);
391
392         ldap_url = talloc_asprintf(state, "ldap://%s/",
393                                    state->domain->dc_address);
394         composite_nomem(ldap_url, state->ctx);
395
396         ctx = ldap_connect_send(state->domain->ldap_conn, ldap_url);
397         composite_continue(state->ctx, ctx, init_domain_recv_ldapconn, state);
398 }
399
400 static void init_domain_recv_ldapconn(struct composite_context *ctx)
401 {
402         struct init_domain_state *state =
403                 talloc_get_type(ctx->async.private_data,
404                                 struct init_domain_state);
405
406         state->ctx->status = ldap_connect_recv(ctx);
407         if (NT_STATUS_IS_OK(state->ctx->status)) {
408                 state->domain->ldap_conn->host =
409                         talloc_strdup(state->domain->ldap_conn,
410                                       state->domain->dc_name);
411                 state->ctx->status =
412                         ldap_bind_sasl(state->domain->ldap_conn,
413                                        state->domain->schannel_creds);
414                 DEBUG(0, ("ldap_bind returned %s\n",
415                           nt_errstr(state->ctx->status)));
416         }
417
418         composite_done(state->ctx);
419 }
420
421 NTSTATUS wb_init_domain_recv(struct composite_context *c,
422                              TALLOC_CTX *mem_ctx,
423                              struct wbsrv_domain **result)
424 {
425         NTSTATUS status = composite_wait(c);
426         if (NT_STATUS_IS_OK(status)) {
427                 struct init_domain_state *state =
428                         talloc_get_type(c->private_data,
429                                         struct init_domain_state);
430                 *result = talloc_steal(mem_ctx, state->domain);
431         }
432         talloc_free(c);
433         return status;
434 }
435
436 NTSTATUS wb_init_domain(TALLOC_CTX *mem_ctx, struct wbsrv_service *service,
437                         struct wb_dom_info *dom_info,
438                         struct wbsrv_domain **result)
439 {
440         struct composite_context *c =
441                 wb_init_domain_send(mem_ctx, service, dom_info);
442         return wb_init_domain_recv(c, mem_ctx, result);
443 }