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