Fix the wbinfo test on the LDAP backend.
[amitay/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_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                                         state->service->task->lp_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         state->domain->libnet_ctx = libnet_context_init(service->task->event_ctx, 
147                                                         service->task->lp_ctx);
148
149         /* Create a credentials structure */
150         state->domain->libnet_ctx->cred = cli_credentials_init(state->domain);
151         if (state->domain->libnet_ctx->cred == NULL) goto failed;
152
153         cli_credentials_set_conf(state->domain->libnet_ctx->cred, service->task->lp_ctx);
154
155         /* Connect the machine account to the credentials */
156         state->ctx->status =
157                 cli_credentials_set_machine_account(state->domain->libnet_ctx->cred, state->domain->libnet_ctx->lp_ctx);
158         if (!NT_STATUS_IS_OK(state->ctx->status)) goto failed;
159
160         state->domain->netlogon_binding = init_domain_binding(state, &ndr_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(service->task->lp_ctx) == ROLE_DOMAIN_MEMBER) ||
166              (lp_server_role(service->task->lp_ctx) == 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(service->task->lp_ctx)) {
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                                          &ndr_table_netlogon,
183                                          state->domain->libnet_ctx->cred,
184                                          service->task->event_ctx,
185                                          service->task->lp_ctx);
186         
187         if (composite_nomem(ctx, state->ctx)) {
188                 goto failed;
189         }
190         
191         composite_continue(state->ctx, ctx, init_domain_recv_netlogonpipe,
192                            state);
193         return result;
194  failed:
195         talloc_free(result);
196         return NULL;
197 }
198
199 /* Having make a netlogon connection (possibly secured with schannel),
200  * make an LSA connection to the same DC, on the same IPC$ share */
201 static void init_domain_recv_netlogonpipe(struct composite_context *ctx)
202 {
203         struct init_domain_state *state =
204                 talloc_get_type(ctx->async.private_data,
205                                 struct init_domain_state);
206
207         state->ctx->status = dcerpc_pipe_connect_b_recv(ctx, state->domain, 
208                                                    &state->domain->netlogon_pipe);
209         
210         if (!composite_is_ok(state->ctx)) {
211                 return;
212         }
213         talloc_steal(state->domain->netlogon_pipe, state->domain->netlogon_binding);
214
215         state->domain->lsa_binding = init_domain_binding(state, &ndr_table_lsarpc);
216
217         /* For debugging, it can be a real pain if all the traffic is encrypted */
218         if (lp_winbind_sealed_pipes(state->service->task->lp_ctx)) {
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                                                     &ndr_table_lsarpc,
231                                                     state->domain->libnet_ctx->cred,
232                                                     state->domain->libnet_ctx->lp_ctx
233                 );
234         composite_continue(state->ctx, ctx, init_domain_recv_lsa_pipe, state);
235 }
236
237 static bool retry_with_schannel(struct init_domain_state *state, 
238                                 struct dcerpc_binding *binding,
239                                 const struct ndr_interface_table *table,
240                                 void (*continuation)(struct composite_context *))
241 {
242         struct composite_context *ctx;
243         state->ctx->status = NT_STATUS_OK;
244         if (state->domain->netlogon_binding->flags & DCERPC_SCHANNEL 
245             && !(binding->flags & DCERPC_SCHANNEL)) {
246                 /* Opening a policy handle failed, perhaps it was
247                  * because we don't get a 'wrong password' error on
248                  * NTLMSSP binds */
249
250                 /* Try again with schannel */
251                 binding->flags |= DCERPC_SCHANNEL;
252
253                 /* Try again, likewise on the same IPC$ share, 
254                    secured with SCHANNEL */
255                 ctx = dcerpc_secondary_auth_connection_send(state->domain->netlogon_pipe,
256                                                             binding,
257                                                             table, 
258                                                             state->domain->libnet_ctx->cred,
259                                                             state->domain->libnet_ctx->lp_ctx);
260                 composite_continue(state->ctx, ctx, continuation, state);               
261                 return true;
262         } else {
263                 return false;
264         }
265 }
266 /* We should now have either an authenticated LSA pipe, or an error.  
267  * On success, open a policy handle
268  */     
269 static void init_domain_recv_lsa_pipe(struct composite_context *ctx)
270 {
271         struct rpc_request *req;
272         struct init_domain_state *state =
273                 talloc_get_type(ctx->async.private_data,
274                                 struct init_domain_state);
275
276         state->ctx->status = dcerpc_secondary_auth_connection_recv(ctx, state->domain,
277                                                                    &state->domain->libnet_ctx->lsa.pipe);
278         if (NT_STATUS_EQUAL(state->ctx->status, NT_STATUS_LOGON_FAILURE)) {
279                 if (retry_with_schannel(state, state->domain->lsa_binding, 
280                                         &ndr_table_lsarpc,
281                                         init_domain_recv_lsa_pipe)) {
282                         return;
283                 }
284         }
285         if (!composite_is_ok(state->ctx)) return;
286
287         talloc_steal(state->domain->libnet_ctx, state->domain->libnet_ctx->lsa.pipe);
288         talloc_steal(state->domain->libnet_ctx->lsa.pipe, state->domain->lsa_binding);
289         state->domain->libnet_ctx->lsa.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
290         state->domain->libnet_ctx->lsa.name = state->domain->info->name;
291
292         ZERO_STRUCT(state->domain->libnet_ctx->lsa.handle);
293         state->lsa_openpolicy.in.system_name =
294                 talloc_asprintf(state, "\\\\%s",
295                                 dcerpc_server_name(state->domain->libnet_ctx->lsa.pipe));
296         ZERO_STRUCT(state->objectattr);
297         state->lsa_openpolicy.in.attr = &state->objectattr;
298         state->lsa_openpolicy.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
299         state->lsa_openpolicy.out.handle = &state->domain->libnet_ctx->lsa.handle;
300
301         req = dcerpc_lsa_OpenPolicy2_send(state->domain->libnet_ctx->lsa.pipe, state,
302                                           &state->lsa_openpolicy);
303
304         composite_continue_rpc(state->ctx, req, init_domain_recv_lsa_policy, state);
305 }
306
307 /* Receive a policy handle (or not, and retry the authentication) and
308  * obtain some basic information about the domain */
309
310 static void init_domain_recv_lsa_policy(struct rpc_request *req)
311 {
312         struct init_domain_state *state =
313                 talloc_get_type(req->async.private_data,
314                                 struct init_domain_state);
315
316         state->ctx->status = dcerpc_ndr_request_recv(req);
317         if ((!NT_STATUS_IS_OK(state->ctx->status)
318               || !NT_STATUS_IS_OK(state->lsa_openpolicy.out.result))) {
319                 if (retry_with_schannel(state, state->domain->lsa_binding, 
320                                         &ndr_table_lsarpc,
321                                         init_domain_recv_lsa_pipe)) {
322                         return;
323                 }
324         }
325         if (!composite_is_ok(state->ctx)) return;
326         state->ctx->status = state->lsa_openpolicy.out.result;
327         if (!composite_is_ok(state->ctx)) return;
328
329         state->queryinfo.in.handle = &state->domain->libnet_ctx->lsa.handle;
330         state->queryinfo.in.level = LSA_POLICY_INFO_ACCOUNT_DOMAIN;
331
332         req = dcerpc_lsa_QueryInfoPolicy_send(state->domain->libnet_ctx->lsa.pipe, state,
333                                               &state->queryinfo);
334         composite_continue_rpc(state->ctx, req,
335                                init_domain_recv_queryinfo, state);
336 }
337
338 static void init_domain_recv_queryinfo(struct rpc_request *req)
339 {
340         struct init_domain_state *state =
341                 talloc_get_type(req->async.private_data, struct init_domain_state);
342         struct lsa_DomainInfo *dominfo;
343         struct composite_context *ctx;
344
345         state->ctx->status = dcerpc_ndr_request_recv(req);
346         if (!composite_is_ok(state->ctx)) return;
347         state->ctx->status = state->queryinfo.out.result;
348         if (!composite_is_ok(state->ctx)) return;
349
350         dominfo = &state->queryinfo.out.info->account_domain;
351
352         if (strcasecmp(state->domain->info->name, dominfo->name.string) != 0) {
353                 DEBUG(2, ("Expected domain name %s, DC %s said %s\n",
354                           state->domain->info->name,
355                           dcerpc_server_name(state->domain->libnet_ctx->lsa.pipe),
356                           dominfo->name.string));
357                 composite_error(state->ctx, NT_STATUS_INVALID_DOMAIN_STATE);
358                 return;
359         }
360
361         if (!dom_sid_equal(state->domain->info->sid, dominfo->sid)) {
362                 DEBUG(2, ("Expected domain sid %s, DC %s said %s\n",
363                           dom_sid_string(state, state->domain->info->sid),
364                           dcerpc_server_name(state->domain->libnet_ctx->lsa.pipe),
365                           dom_sid_string(state, dominfo->sid)));
366                 composite_error(state->ctx, NT_STATUS_INVALID_DOMAIN_STATE);
367                 return;
368         }
369
370         state->domain->samr_binding = init_domain_binding(state, &ndr_table_samr);
371
372         /* We want to use the same flags as the LSA pipe did (so, if
373          * it needed schannel, then we need that here too) */
374         state->domain->samr_binding->flags = state->domain->lsa_binding->flags;
375
376         state->domain->libnet_ctx->samr.pipe = NULL;
377
378         ctx = wb_connect_samr_send(state, state->domain);
379         composite_continue(state->ctx, ctx, init_domain_recv_samr, state);
380 }
381
382 /* Recv the SAMR details (SamrConnect and SamrOpenDomain handle) and
383  * open an LDAP connection */
384 static void init_domain_recv_samr(struct composite_context *ctx)
385 {
386         const char *ldap_url;
387         struct init_domain_state *state =
388                 talloc_get_type(ctx->async.private_data,
389                                 struct init_domain_state);
390
391         state->ctx->status = wb_connect_samr_recv(
392                 ctx, state->domain,
393                 &state->domain->libnet_ctx->samr.pipe,
394                 &state->domain->libnet_ctx->samr.handle, 
395                 &state->domain->libnet_ctx->samr.handle);
396         if (!composite_is_ok(state->ctx)) return;
397
398         talloc_steal(state->domain->libnet_ctx->samr.pipe, state->domain->samr_binding);
399         state->domain->libnet_ctx->samr.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
400         state->domain->libnet_ctx->samr.name = state->domain->info->name;
401         state->domain->libnet_ctx->samr.sid = state->domain->info->sid;
402
403         composite_done(state->ctx);
404 }
405
406 NTSTATUS wb_init_domain_recv(struct composite_context *c,
407                              TALLOC_CTX *mem_ctx,
408                              struct wbsrv_domain **result)
409 {
410         NTSTATUS status = composite_wait(c);
411         if (NT_STATUS_IS_OK(status)) {
412                 struct init_domain_state *state =
413                         talloc_get_type(c->private_data,
414                                         struct init_domain_state);
415                 *result = talloc_steal(mem_ctx, state->domain);
416         }
417         talloc_free(c);
418         return status;
419 }
420
421 NTSTATUS wb_init_domain(TALLOC_CTX *mem_ctx, struct wbsrv_service *service,
422                         struct wb_dom_info *dom_info,
423                         struct wbsrv_domain **result)
424 {
425         struct composite_context *c =
426                 wb_init_domain_send(mem_ctx, service, dom_info);
427         return wb_init_domain_recv(c, mem_ctx, result);
428 }