Remove event context tracking from the credentials struct.
[jra/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    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 3 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, see <http://www.gnu.org/licenses/>.
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 "winbind/wb_async_helpers.h"
28 #include "winbind/wb_helper.h"
29 #include "smbd/service_task.h"
30 #include "librpc/gen_ndr/ndr_netlogon.h"
31 #include "librpc/gen_ndr/ndr_lsa_c.h"
32 #include "librpc/gen_ndr/ndr_samr_c.h"
33 #include "libcli/libcli.h"
34
35 #include "libcli/auth/credentials.h"
36 #include "libcli/security/security.h"
37
38 #include "libcli/ldap/ldap_client.h"
39
40 #include "auth/credentials/credentials.h"
41 #include "param/param.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 ndr_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                                         state->service->task->lp_ctx);
106         if (!NT_STATUS_IS_OK(status)) {
107                 return NULL;
108         }
109
110         return binding;
111 }
112
113 struct composite_context *wb_init_domain_send(TALLOC_CTX *mem_ctx,
114                                               struct wbsrv_service *service,
115                                               struct wb_dom_info *dom_info)
116 {
117         struct composite_context *result, *ctx;
118         struct init_domain_state *state;
119
120         result = composite_create(mem_ctx, service->task->event_ctx);
121         if (result == NULL) goto failed;
122
123         state = talloc_zero(result, struct init_domain_state);
124         if (state == NULL) goto failed;
125         state->ctx = result;
126         result->private_data = state;
127
128         state->service = service;
129
130         state->domain = talloc(state, struct wbsrv_domain);
131         if (state->domain == NULL) goto failed;
132
133         state->domain->info = talloc_reference(state->domain, dom_info);
134         if (state->domain->info == NULL) goto failed;
135
136         /* Caller should check, but to be safe: */
137         if (dom_info->num_dcs < 1) {
138                 goto failed;
139         }
140         
141         /* For now, we just pick the first.  The next step will be to
142          * walk the entire list.  Also need to fix finddcs() to return
143          * the entire list */
144         state->domain->dc_name = dom_info->dcs[0].name;
145         state->domain->dc_address = dom_info->dcs[0].address;
146
147         state->domain->libnet_ctx = libnet_context_init(service->task->event_ctx, 
148                                                         service->task->lp_ctx);
149
150         /* Create a credentials structure */
151         state->domain->libnet_ctx->cred = cli_credentials_init(state->domain);
152         if (state->domain->libnet_ctx->cred == NULL) goto failed;
153
154         cli_credentials_set_conf(state->domain->libnet_ctx->cred, service->task->lp_ctx);
155
156         /* Connect the machine account to the credentials */
157         state->ctx->status =
158                 cli_credentials_set_machine_account(state->domain->libnet_ctx->cred, state->domain->libnet_ctx->lp_ctx);
159         if (!NT_STATUS_IS_OK(state->ctx->status)) goto failed;
160
161         state->domain->netlogon_binding = init_domain_binding(state, &ndr_table_netlogon);
162
163         state->domain->netlogon_pipe = NULL;
164
165         if ((!cli_credentials_is_anonymous(state->domain->libnet_ctx->cred)) &&
166             ((lp_server_role(service->task->lp_ctx) == ROLE_DOMAIN_MEMBER) ||
167              (lp_server_role(service->task->lp_ctx) == ROLE_DOMAIN_CONTROLLER)) &&
168             (dom_sid_equal(state->domain->info->sid,
169                            state->service->primary_sid))) {
170                 state->domain->netlogon_binding->flags |= DCERPC_SCHANNEL;
171
172                 /* For debugging, it can be a real pain if all the traffic is encrypted */
173                 if (lp_winbind_sealed_pipes(service->task->lp_ctx)) {
174                         state->domain->netlogon_binding->flags |= (DCERPC_SIGN | DCERPC_SEAL );
175                 } else {
176                         state->domain->netlogon_binding->flags |= (DCERPC_SIGN);
177                 }
178         }
179
180         /* No encryption on anonymous pipes */
181
182         ctx = dcerpc_pipe_connect_b_send(state, state->domain->netlogon_binding, 
183                                          &ndr_table_netlogon,
184                                          state->domain->libnet_ctx->cred,
185                                          service->task->event_ctx,
186                                          service->task->lp_ctx);
187         
188         if (composite_nomem(ctx, state->ctx)) {
189                 goto failed;
190         }
191         
192         composite_continue(state->ctx, ctx, init_domain_recv_netlogonpipe,
193                            state);
194         return result;
195  failed:
196         talloc_free(result);
197         return NULL;
198 }
199
200 /* Having make a netlogon connection (possibly secured with schannel),
201  * make an LSA connection to the same DC, on the same IPC$ share */
202 static void init_domain_recv_netlogonpipe(struct composite_context *ctx)
203 {
204         struct init_domain_state *state =
205                 talloc_get_type(ctx->async.private_data,
206                                 struct init_domain_state);
207
208         state->ctx->status = dcerpc_pipe_connect_b_recv(ctx, state->domain, 
209                                                    &state->domain->netlogon_pipe);
210         
211         if (!composite_is_ok(state->ctx)) {
212                 talloc_free(state->domain->netlogon_binding);
213                 return;
214         }
215         talloc_steal(state->domain->netlogon_pipe, state->domain->netlogon_binding);
216
217         state->domain->lsa_binding = init_domain_binding(state, &ndr_table_lsarpc);
218
219         /* For debugging, it can be a real pain if all the traffic is encrypted */
220         if (lp_winbind_sealed_pipes(state->service->task->lp_ctx)) {
221                 state->domain->lsa_binding->flags |= (DCERPC_SIGN | DCERPC_SEAL );
222         } else {
223                 state->domain->lsa_binding->flags |= (DCERPC_SIGN);
224         }
225
226         state->domain->libnet_ctx->lsa.pipe = NULL;
227
228         /* this will make the secondary connection on the same IPC$ share, 
229            secured with SPNEGO or NTLMSSP */
230         ctx = dcerpc_secondary_auth_connection_send(state->domain->netlogon_pipe,
231                                                     state->domain->lsa_binding,
232                                                     &ndr_table_lsarpc,
233                                                     state->domain->libnet_ctx->cred,
234                                                     state->domain->libnet_ctx->lp_ctx
235                 );
236         composite_continue(state->ctx, ctx, init_domain_recv_lsa_pipe, state);
237 }
238
239 static bool retry_with_schannel(struct init_domain_state *state, 
240                                 struct dcerpc_binding *binding,
241                                 const struct ndr_interface_table *table,
242                                 void (*continuation)(struct composite_context *))
243 {
244         struct composite_context *ctx;
245         state->ctx->status = NT_STATUS_OK;
246         if (state->domain->netlogon_binding->flags & DCERPC_SCHANNEL 
247             && !(binding->flags & DCERPC_SCHANNEL)) {
248                 /* Opening a policy handle failed, perhaps it was
249                  * because we don't get a 'wrong password' error on
250                  * NTLMSSP binds */
251
252                 /* Try again with schannel */
253                 binding->flags |= DCERPC_SCHANNEL;
254
255                 /* Try again, likewise on the same IPC$ share, 
256                    secured with SCHANNEL */
257                 ctx = dcerpc_secondary_auth_connection_send(state->domain->netlogon_pipe,
258                                                             binding,
259                                                             table, 
260                                                             state->domain->libnet_ctx->cred,
261                                                             state->domain->libnet_ctx->lp_ctx);
262                 composite_continue(state->ctx, ctx, continuation, state);               
263                 return true;
264         } else {
265                 return false;
266         }
267 }
268 /* We should now have either an authenticated LSA pipe, or an error.  
269  * On success, open a policy handle
270  */     
271 static void init_domain_recv_lsa_pipe(struct composite_context *ctx)
272 {
273         struct rpc_request *req;
274         struct init_domain_state *state =
275                 talloc_get_type(ctx->async.private_data,
276                                 struct init_domain_state);
277
278         state->ctx->status = dcerpc_secondary_auth_connection_recv(ctx, state->domain,
279                                                                    &state->domain->libnet_ctx->lsa.pipe);
280         if (NT_STATUS_EQUAL(state->ctx->status, NT_STATUS_LOGON_FAILURE)) {
281                 if (retry_with_schannel(state, state->domain->lsa_binding, 
282                                         &ndr_table_lsarpc,
283                                         init_domain_recv_lsa_pipe)) {
284                         return;
285                 }
286         }
287         if (!composite_is_ok(state->ctx)) return;
288
289         talloc_steal(state->domain->libnet_ctx, state->domain->libnet_ctx->lsa.pipe);
290         talloc_steal(state->domain->libnet_ctx->lsa.pipe, state->domain->lsa_binding);
291         state->domain->libnet_ctx->lsa.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
292         state->domain->libnet_ctx->lsa.name = state->domain->info->name;
293
294         ZERO_STRUCT(state->domain->libnet_ctx->lsa.handle);
295         state->lsa_openpolicy.in.system_name =
296                 talloc_asprintf(state, "\\\\%s",
297                                 dcerpc_server_name(state->domain->libnet_ctx->lsa.pipe));
298         ZERO_STRUCT(state->objectattr);
299         state->lsa_openpolicy.in.attr = &state->objectattr;
300         state->lsa_openpolicy.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
301         state->lsa_openpolicy.out.handle = &state->domain->libnet_ctx->lsa.handle;
302
303         req = dcerpc_lsa_OpenPolicy2_send(state->domain->libnet_ctx->lsa.pipe, state,
304                                           &state->lsa_openpolicy);
305
306         composite_continue_rpc(state->ctx, req, init_domain_recv_lsa_policy, state);
307 }
308
309 /* Receive a policy handle (or not, and retry the authentication) and
310  * obtain some basic information about the domain */
311
312 static void init_domain_recv_lsa_policy(struct rpc_request *req)
313 {
314         struct init_domain_state *state =
315                 talloc_get_type(req->async.private_data,
316                                 struct init_domain_state);
317
318         state->ctx->status = dcerpc_ndr_request_recv(req);
319         if ((!NT_STATUS_IS_OK(state->ctx->status)
320               || !NT_STATUS_IS_OK(state->lsa_openpolicy.out.result))) {
321                 if (retry_with_schannel(state, state->domain->lsa_binding, 
322                                         &ndr_table_lsarpc,
323                                         init_domain_recv_lsa_pipe)) {
324                         return;
325                 }
326         }
327         if (!composite_is_ok(state->ctx)) return;
328         state->ctx->status = state->lsa_openpolicy.out.result;
329         if (!composite_is_ok(state->ctx)) return;
330
331         state->queryinfo.in.handle = &state->domain->libnet_ctx->lsa.handle;
332         state->queryinfo.in.level = LSA_POLICY_INFO_ACCOUNT_DOMAIN;
333
334         req = dcerpc_lsa_QueryInfoPolicy_send(state->domain->libnet_ctx->lsa.pipe, state,
335                                               &state->queryinfo);
336         composite_continue_rpc(state->ctx, req,
337                                init_domain_recv_queryinfo, state);
338 }
339
340 static void init_domain_recv_queryinfo(struct rpc_request *req)
341 {
342         struct init_domain_state *state =
343                 talloc_get_type(req->async.private_data, struct init_domain_state);
344         struct lsa_DomainInfo *dominfo;
345         struct composite_context *ctx;
346
347         state->ctx->status = dcerpc_ndr_request_recv(req);
348         if (!composite_is_ok(state->ctx)) return;
349         state->ctx->status = state->queryinfo.out.result;
350         if (!composite_is_ok(state->ctx)) return;
351
352         dominfo = &state->queryinfo.out.info->account_domain;
353
354         if (strcasecmp(state->domain->info->name, dominfo->name.string) != 0) {
355                 DEBUG(2, ("Expected domain name %s, DC %s said %s\n",
356                           state->domain->info->name,
357                           dcerpc_server_name(state->domain->libnet_ctx->lsa.pipe),
358                           dominfo->name.string));
359                 composite_error(state->ctx, NT_STATUS_INVALID_DOMAIN_STATE);
360                 return;
361         }
362
363         if (!dom_sid_equal(state->domain->info->sid, dominfo->sid)) {
364                 DEBUG(2, ("Expected domain sid %s, DC %s said %s\n",
365                           dom_sid_string(state, state->domain->info->sid),
366                           dcerpc_server_name(state->domain->libnet_ctx->lsa.pipe),
367                           dom_sid_string(state, dominfo->sid)));
368                 composite_error(state->ctx, NT_STATUS_INVALID_DOMAIN_STATE);
369                 return;
370         }
371
372         state->domain->samr_binding = init_domain_binding(state, &ndr_table_samr);
373
374         /* We want to use the same flags as the LSA pipe did (so, if
375          * it needed schannel, then we need that here too) */
376         state->domain->samr_binding->flags = state->domain->lsa_binding->flags;
377
378         state->domain->libnet_ctx->samr.pipe = NULL;
379
380         ctx = wb_connect_samr_send(state, state->domain);
381         composite_continue(state->ctx, ctx, init_domain_recv_samr, state);
382 }
383
384 /* Recv the SAMR details (SamrConnect and SamrOpenDomain handle) and
385  * open an LDAP connection */
386 static void init_domain_recv_samr(struct composite_context *ctx)
387 {
388         const char *ldap_url;
389         struct init_domain_state *state =
390                 talloc_get_type(ctx->async.private_data,
391                                 struct init_domain_state);
392
393         state->ctx->status = wb_connect_samr_recv(
394                 ctx, state->domain,
395                 &state->domain->libnet_ctx->samr.pipe,
396                 &state->domain->libnet_ctx->samr.handle, 
397                 &state->domain->libnet_ctx->samr.handle);
398         if (!composite_is_ok(state->ctx)) return;
399
400         talloc_steal(state->domain->libnet_ctx->samr.pipe, state->domain->samr_binding);
401         state->domain->libnet_ctx->samr.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
402         state->domain->libnet_ctx->samr.name = state->domain->info->name;
403         state->domain->libnet_ctx->samr.sid = state->domain->info->sid;
404
405         state->domain->ldap_conn =
406                 ldap4_new_connection(state->domain, state->domain->libnet_ctx->lp_ctx, state->ctx->event_ctx);
407         composite_nomem(state->domain->ldap_conn, state->ctx);
408
409         ldap_url = talloc_asprintf(state, "ldap://%s/",
410                                    state->domain->dc_address);
411         composite_nomem(ldap_url, state->ctx);
412
413         ctx = ldap_connect_send(state->domain->ldap_conn, ldap_url);
414         composite_continue(state->ctx, ctx, init_domain_recv_ldapconn, state);
415 }
416
417 static void init_domain_recv_ldapconn(struct composite_context *ctx)
418 {
419         struct init_domain_state *state =
420                 talloc_get_type(ctx->async.private_data,
421                                 struct init_domain_state);
422
423         state->ctx->status = ldap_connect_recv(ctx);
424         if (NT_STATUS_IS_OK(state->ctx->status)) {
425                 state->domain->ldap_conn->host =
426                         talloc_strdup(state->domain->ldap_conn,
427                                       state->domain->dc_name);
428                 state->ctx->status =
429                         ldap_bind_sasl(state->domain->ldap_conn,
430                                        state->domain->libnet_ctx->cred,
431                                        state->domain->libnet_ctx->lp_ctx);
432                 DEBUG(0, ("ldap_bind returned %s\n",
433                           nt_errstr(state->ctx->status)));
434         }
435
436         composite_done(state->ctx);
437 }
438
439 NTSTATUS wb_init_domain_recv(struct composite_context *c,
440                              TALLOC_CTX *mem_ctx,
441                              struct wbsrv_domain **result)
442 {
443         NTSTATUS status = composite_wait(c);
444         if (NT_STATUS_IS_OK(status)) {
445                 struct init_domain_state *state =
446                         talloc_get_type(c->private_data,
447                                         struct init_domain_state);
448                 *result = talloc_steal(mem_ctx, state->domain);
449         }
450         talloc_free(c);
451         return status;
452 }
453
454 NTSTATUS wb_init_domain(TALLOC_CTX *mem_ctx, struct wbsrv_service *service,
455                         struct wb_dom_info *dom_info,
456                         struct wbsrv_domain **result)
457 {
458         struct composite_context *c =
459                 wb_init_domain_send(mem_ctx, service, dom_info);
460         return wb_init_domain_recv(c, mem_ctx, result);
461 }