winbindd: Do not use group_list->out.resume_index after free
[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 "winbind/wb_server.h"
26 #include "smbd/service_task.h"
27 #include "librpc/gen_ndr/ndr_netlogon.h"
28 #include "librpc/gen_ndr/ndr_lsa_c.h"
29 #include "librpc/gen_ndr/ndr_samr_c.h"
30 #include "libcli/libcli.h"
31
32 #include "libcli/security/security.h"
33
34
35 #include "auth/credentials/credentials.h"
36 #include "param/param.h"
37
38 /*
39  * Initialize a domain:
40  *
41  * - With schannel credentials, try to open the SMB connection and
42  *   NETLOGON pipe with the machine creds. This works against W2k3SP1
43  *   with an NTLMSSP session setup. Fall back to anonymous (for the CIFS level).
44  *
45  * - If we have schannel creds, do the auth2 and open the schannel'ed netlogon
46  *   pipe.
47  *
48  * - Open LSA. If we have machine creds, try to open with SPNEGO or NTLMSSP. Fall back
49  *   to schannel.
50  *
51  * - With queryinfopolicy, verify that we're talking to the right domain
52  *
53  * A bit complex, but with all the combinations I think it's the best we can
54  * get. NT4, W2k3 and W2k all have different combinations, but in the end we
55  * have a signed&sealed lsa connection on all of them.
56  *
57  * Not sure if it is overkill, but it seems to work.
58  */
59
60 struct init_domain_state {
61         struct composite_context *ctx;
62         struct wbsrv_domain *domain;
63         struct wbsrv_service *service;
64
65         struct lsa_ObjectAttribute objectattr;
66         struct lsa_OpenPolicy2 lsa_openpolicy;
67         struct lsa_QueryInfoPolicy queryinfo;
68         union lsa_PolicyInformation *info;
69 };
70
71 static void init_domain_recv_netlogonpipe(struct composite_context *ctx);
72 static void init_domain_recv_lsa_pipe(struct composite_context *ctx);
73 static void init_domain_recv_lsa_policy(struct tevent_req *subreq);
74 static void init_domain_recv_queryinfo(struct tevent_req *subreq);
75 static void init_domain_recv_samr(struct composite_context *ctx);
76
77 static struct dcerpc_binding *init_domain_binding(struct init_domain_state *state, 
78                                                   const struct ndr_interface_table *table) 
79 {
80         struct dcerpc_binding *binding;
81         enum dcerpc_transport_t transport;
82         char *s;
83         NTSTATUS status;
84
85         /* Make a binding string */
86         if ((lpcfg_server_role(state->service->task->lp_ctx) != ROLE_DOMAIN_MEMBER) &&
87             dom_sid_equal(state->domain->info->sid, state->service->primary_sid) &&
88             state->service->sec_channel_type != SEC_CHAN_RODC) {
89                 s = talloc_asprintf(state, "ncalrpc:%s[target_hostname=%s]",
90                                     state->domain->dc_address,
91                                     state->domain->dc_name);
92                 if (s == NULL) return NULL;
93         } else {
94                 s = talloc_asprintf(state, "ncacn_np:%s[target_hostname=%s]",
95                                     state->domain->dc_address,
96                                     state->domain->dc_name);
97                 if (s == NULL) return NULL;
98
99         }
100         status = dcerpc_parse_binding(state, s, &binding);
101         talloc_free(s);
102         if (!NT_STATUS_IS_OK(status)) {
103                 return NULL;
104         }
105
106         transport = dcerpc_binding_get_transport(binding);
107         if (transport == NCALRPC) {
108                 return binding;
109         }
110
111         /* This shouldn't make a network call, as the mappings for named pipes are well known */
112         status = dcerpc_epm_map_binding(binding, binding, table, state->service->task->event_ctx,
113                                         state->service->task->lp_ctx);
114         if (!NT_STATUS_IS_OK(status)) {
115                 return NULL;
116         }
117
118         return binding;
119 }
120
121 struct composite_context *wb_init_domain_send(TALLOC_CTX *mem_ctx,
122                                               struct wbsrv_service *service,
123                                               struct wb_dom_info *dom_info)
124 {
125         struct composite_context *result, *ctx;
126         struct init_domain_state *state;
127
128         result = composite_create(mem_ctx, service->task->event_ctx);
129         if (result == NULL) goto failed;
130
131         state = talloc_zero(result, struct init_domain_state);
132         if (state == NULL) goto failed;
133         state->ctx = result;
134         result->private_data = state;
135
136         state->service = service;
137
138         state->domain = talloc(state, struct wbsrv_domain);
139         if (state->domain == NULL) goto failed;
140
141         state->domain->service = service;
142
143         state->domain->info = talloc_reference(state->domain, dom_info);
144         if (state->domain->info == NULL) goto failed;
145
146         state->domain->dc_name = dom_info->dc->name;
147         state->domain->dc_address = dom_info->dc->address;
148
149         state->domain->libnet_ctx = libnet_context_init(service->task->event_ctx, 
150                                                         service->task->lp_ctx);
151         if (state->domain->libnet_ctx == NULL) goto failed;
152         talloc_steal(state->domain, state->domain->libnet_ctx);
153
154         /* Create a credentials structure */
155         state->domain->libnet_ctx->cred = cli_credentials_init(state->domain);
156         if (state->domain->libnet_ctx->cred == NULL) goto failed;
157
158         cli_credentials_set_conf(state->domain->libnet_ctx->cred, service->task->lp_ctx);
159
160         /* Connect the machine account to the credentials */
161         state->ctx->status =
162                 cli_credentials_set_machine_account(state->domain->libnet_ctx->cred, state->domain->libnet_ctx->lp_ctx);
163         if (!NT_STATUS_IS_OK(state->ctx->status)) goto failed;
164
165         state->domain->netlogon_binding = init_domain_binding(state, &ndr_table_netlogon);
166
167         state->domain->netlogon_pipe = NULL;
168
169         state->domain->netlogon_queue = tevent_queue_create(state->domain,
170                                                             "netlogon_queue");
171         if (state->domain->netlogon_queue == NULL) goto failed;
172
173         /* We start the queue when the connection is usable */
174         tevent_queue_stop(state->domain->netlogon_queue);
175
176         if ((!cli_credentials_is_anonymous(state->domain->libnet_ctx->cred)) &&
177             ((lpcfg_server_role(service->task->lp_ctx) == ROLE_DOMAIN_MEMBER) ||
178              (lpcfg_server_role(service->task->lp_ctx) == ROLE_ACTIVE_DIRECTORY_DC)) &&
179             (dom_sid_equal(state->domain->info->sid,
180                            state->service->primary_sid))) {
181                 uint32_t flags = DCERPC_SCHANNEL | DCERPC_SCHANNEL_AUTO;
182
183                 /* For debugging, it can be a real pain if all the traffic is encrypted */
184                 if (lpcfg_winbind_sealed_pipes(service->task->lp_ctx)) {
185                         flags |= DCERPC_SIGN | DCERPC_SEAL;
186                 } else {
187                         flags |= DCERPC_SIGN;
188                 }
189                 state->ctx->status = dcerpc_binding_set_flags(state->domain->netlogon_binding,
190                                                               flags, 0);
191                 if (!NT_STATUS_IS_OK(state->ctx->status)) goto failed;
192         }
193
194         /* No encryption on anonymous pipes */
195
196         ctx = dcerpc_pipe_connect_b_send(state, state->domain->netlogon_binding, 
197                                          &ndr_table_netlogon,
198                                          state->domain->libnet_ctx->cred,
199                                          service->task->event_ctx,
200                                          service->task->lp_ctx);
201         
202         if (composite_nomem(ctx, state->ctx)) {
203                 goto failed;
204         }
205         
206         composite_continue(state->ctx, ctx, init_domain_recv_netlogonpipe,
207                            state);
208         return result;
209  failed:
210         talloc_free(result);
211         return NULL;
212 }
213
214 /* Having make a netlogon connection (possibly secured with schannel),
215  * make an LSA connection to the same DC, on the same IPC$ share */
216 static void init_domain_recv_netlogonpipe(struct composite_context *ctx)
217 {
218         struct init_domain_state *state =
219                 talloc_get_type(ctx->async.private_data,
220                                 struct init_domain_state);
221         uint32_t flags;
222
223         state->ctx->status = dcerpc_pipe_connect_b_recv(ctx, state->domain, 
224                                                    &state->domain->netlogon_pipe);
225         
226         if (!composite_is_ok(state->ctx)) {
227                 return;
228         }
229         talloc_reparent(state, state->domain->netlogon_pipe, state->domain->netlogon_binding);
230
231         /* the netlogon connection is ready */
232         tevent_queue_start(state->domain->netlogon_queue);
233
234         state->domain->lsa_binding = init_domain_binding(state, &ndr_table_lsarpc);
235
236         /* For debugging, it can be a real pain if all the traffic is encrypted */
237         if (lpcfg_winbind_sealed_pipes(state->service->task->lp_ctx)) {
238                 flags = DCERPC_SIGN | DCERPC_SEAL;
239         } else {
240                 flags = DCERPC_SIGN;
241         }
242         state->ctx->status = dcerpc_binding_set_flags(state->domain->lsa_binding,
243                                                       flags, 0);
244         if (!composite_is_ok(state->ctx)) {
245                 return;
246         }
247
248         state->domain->libnet_ctx->lsa.pipe = NULL;
249         state->domain->libnet_ctx->lsa.lsa_handle = NULL;
250
251         /* this will make the secondary connection on the same IPC$ share, 
252            secured with SPNEGO or NTLMSSP */
253         ctx = dcerpc_secondary_auth_connection_send(state->domain->netlogon_pipe,
254                                                     state->domain->lsa_binding,
255                                                     &ndr_table_lsarpc,
256                                                     state->domain->libnet_ctx->cred,
257                                                     state->domain->libnet_ctx->lp_ctx
258                 );
259         composite_continue(state->ctx, ctx, init_domain_recv_lsa_pipe, state);
260 }
261
262 static bool retry_with_schannel(struct init_domain_state *state, 
263                                 struct dcerpc_binding *binding,
264                                 const struct ndr_interface_table *table,
265                                 void (*continuation)(struct composite_context *))
266 {
267         struct composite_context *ctx;
268         uint32_t nflags;
269         uint32_t bflags;
270
271         state->ctx->status = NT_STATUS_OK;
272
273         nflags = dcerpc_binding_get_flags(state->domain->netlogon_binding);
274         bflags = dcerpc_binding_get_flags(binding);
275
276         if ((nflags & DCERPC_SCHANNEL) && !(bflags & DCERPC_SCHANNEL)) {
277                 /* Opening a policy handle failed, perhaps it was
278                  * because we don't get a 'wrong password' error on
279                  * NTLMSSP binds */
280
281                 /* Try again with schannel */
282                 bflags |= DCERPC_SCHANNEL | DCERPC_SCHANNEL_AUTO;
283
284                 state->ctx->status = dcerpc_binding_set_flags(binding, bflags, 0);
285                 if (!composite_is_ok(state->ctx)) return true;
286
287                 /* Try again, likewise on the same IPC$ share, 
288                    secured with SCHANNEL */
289                 ctx = dcerpc_secondary_auth_connection_send(state->domain->netlogon_pipe,
290                                                             binding,
291                                                             table, 
292                                                             state->domain->libnet_ctx->cred,
293                                                             state->domain->libnet_ctx->lp_ctx);
294                 composite_continue(state->ctx, ctx, continuation, state);               
295                 return true;
296         } else {
297                 return false;
298         }
299 }
300 /* We should now have either an authenticated LSA pipe, or an error.  
301  * On success, open a policy handle
302  */     
303 static void init_domain_recv_lsa_pipe(struct composite_context *ctx)
304 {
305         struct init_domain_state *state =
306                 talloc_get_type(ctx->async.private_data,
307                                 struct init_domain_state);
308         struct tevent_req *subreq;
309
310         state->ctx->status = dcerpc_secondary_auth_connection_recv(ctx, state->domain,
311                                                                    &state->domain->libnet_ctx->lsa.pipe);
312         if (NT_STATUS_EQUAL(state->ctx->status, NT_STATUS_LOGON_FAILURE)) {
313                 if (retry_with_schannel(state, state->domain->lsa_binding, 
314                                         &ndr_table_lsarpc,
315                                         init_domain_recv_lsa_pipe)) {
316                         return;
317                 }
318         }
319         if (!composite_is_ok(state->ctx)) return;
320
321         talloc_steal(state->domain->libnet_ctx, state->domain->libnet_ctx->lsa.pipe);
322         talloc_reparent(state, state->domain->libnet_ctx->lsa.pipe, state->domain->lsa_binding);
323         state->domain->libnet_ctx->lsa.lsa_handle =
324                 state->domain->libnet_ctx->lsa.pipe->binding_handle;
325         state->domain->libnet_ctx->lsa.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
326         state->domain->libnet_ctx->lsa.name = state->domain->info->name;
327
328         ZERO_STRUCT(state->domain->libnet_ctx->lsa.handle);
329         state->lsa_openpolicy.in.system_name =
330                 talloc_asprintf(state, "\\\\%s",
331                                 dcerpc_server_name(state->domain->libnet_ctx->lsa.pipe));
332         ZERO_STRUCT(state->objectattr);
333         state->lsa_openpolicy.in.attr = &state->objectattr;
334         state->lsa_openpolicy.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
335         state->lsa_openpolicy.out.handle = &state->domain->libnet_ctx->lsa.handle;
336
337         subreq = dcerpc_lsa_OpenPolicy2_r_send(state,
338                                                state->ctx->event_ctx,
339                                                state->domain->libnet_ctx->lsa.pipe->binding_handle,
340                                                &state->lsa_openpolicy);
341         if (composite_nomem(subreq, state->ctx)) return;
342         tevent_req_set_callback(subreq, init_domain_recv_lsa_policy, state);
343 }
344
345 /* Receive a policy handle (or not, and retry the authentication) and
346  * obtain some basic information about the domain */
347
348 static void init_domain_recv_lsa_policy(struct tevent_req *subreq)
349 {
350         struct init_domain_state *state =
351                 tevent_req_callback_data(subreq,
352                 struct init_domain_state);
353
354         state->ctx->status = dcerpc_lsa_OpenPolicy2_r_recv(subreq, state);
355         TALLOC_FREE(subreq);
356         if ((!NT_STATUS_IS_OK(state->ctx->status)
357               || !NT_STATUS_IS_OK(state->lsa_openpolicy.out.result))) {
358                 if (retry_with_schannel(state, state->domain->lsa_binding, 
359                                         &ndr_table_lsarpc,
360                                         init_domain_recv_lsa_pipe)) {
361                         return;
362                 }
363         }
364         if (!composite_is_ok(state->ctx)) return;
365         state->ctx->status = state->lsa_openpolicy.out.result;
366         if (!composite_is_ok(state->ctx)) return;
367
368         state->info = talloc_zero(state->ctx, union lsa_PolicyInformation);
369         if (composite_nomem(state->info, state->ctx)) return;
370
371         state->queryinfo.in.handle = &state->domain->libnet_ctx->lsa.handle;
372         state->queryinfo.in.level = LSA_POLICY_INFO_ACCOUNT_DOMAIN;
373         state->queryinfo.out.info = &state->info;
374
375         subreq = dcerpc_lsa_QueryInfoPolicy_r_send(state,
376                                                    state->ctx->event_ctx,
377                                                    state->domain->libnet_ctx->lsa.pipe->binding_handle,
378                                                    &state->queryinfo);
379         if (composite_nomem(subreq, state->ctx)) return;
380         tevent_req_set_callback(subreq, init_domain_recv_queryinfo, state);
381 }
382
383 static void init_domain_recv_queryinfo(struct tevent_req *subreq)
384 {
385         struct init_domain_state *state =
386                 tevent_req_callback_data(subreq,
387                 struct init_domain_state);
388         struct lsa_DomainInfo *dominfo;
389         struct composite_context *ctx;
390         uint32_t lflags;
391
392         state->ctx->status = dcerpc_lsa_QueryInfoPolicy_r_recv(subreq, state);
393         TALLOC_FREE(subreq);
394         if (!composite_is_ok(state->ctx)) return;
395         state->ctx->status = state->queryinfo.out.result;
396         if (!composite_is_ok(state->ctx)) return;
397
398         if (!dom_sid_equal(state->domain->info->sid, &global_sid_Builtin)) {
399                 dominfo = &(*state->queryinfo.out.info)->account_domain;
400                 
401                 if (strcasecmp(state->domain->info->name, dominfo->name.string) != 0) {
402                         DEBUG(2, ("Expected domain name %s, DC %s said %s\n",
403                                   state->domain->info->name,
404                                   dcerpc_server_name(state->domain->libnet_ctx->lsa.pipe),
405                                   dominfo->name.string));
406                         composite_error(state->ctx, NT_STATUS_INVALID_DOMAIN_STATE);
407                         return;
408                 }
409                 
410                 if (!dom_sid_equal(state->domain->info->sid, dominfo->sid)) {
411                         DEBUG(2, ("Expected domain sid %s, DC %s said %s\n",
412                                   dom_sid_string(state, state->domain->info->sid),
413                                   dcerpc_server_name(state->domain->libnet_ctx->lsa.pipe),
414                                   dom_sid_string(state, dominfo->sid)));
415                         composite_error(state->ctx, NT_STATUS_INVALID_DOMAIN_STATE);
416                         return;
417                 }
418         }
419
420         state->domain->samr_binding = init_domain_binding(state, &ndr_table_samr);
421
422         /* We want to use the same flags as the LSA pipe did (so, if
423          * it needed schannel, then we need that here too) */
424         lflags = dcerpc_binding_get_flags(state->domain->lsa_binding);
425         state->ctx->status = dcerpc_binding_set_flags(state->domain->samr_binding,
426                                                       lflags, 0);
427         if (!composite_is_ok(state->ctx)) return;
428
429         state->domain->libnet_ctx->samr.pipe = NULL;
430         state->domain->libnet_ctx->samr.samr_handle = NULL;
431
432         ctx = wb_connect_samr_send(state, state->domain);
433         composite_continue(state->ctx, ctx, init_domain_recv_samr, state);
434 }
435
436 /* Recv the SAMR details (SamrConnect and SamrOpenDomain handle) and
437  * open an LDAP connection */
438 static void init_domain_recv_samr(struct composite_context *ctx)
439 {
440         struct init_domain_state *state =
441                 talloc_get_type(ctx->async.private_data,
442                                 struct init_domain_state);
443
444         state->ctx->status = wb_connect_samr_recv(
445                 ctx, state->domain,
446                 &state->domain->libnet_ctx->samr.pipe,
447                 &state->domain->libnet_ctx->samr.connect_handle,
448                 &state->domain->libnet_ctx->samr.handle);
449         if (!composite_is_ok(state->ctx)) return;
450
451         talloc_reparent(state, state->domain->libnet_ctx->samr.pipe, state->domain->samr_binding);
452         state->domain->libnet_ctx->samr.samr_handle =
453                 state->domain->libnet_ctx->samr.pipe->binding_handle;
454         state->domain->libnet_ctx->samr.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
455         state->domain->libnet_ctx->samr.name = state->domain->info->name;
456         state->domain->libnet_ctx->samr.sid = dom_sid_dup(
457                                                 state->domain->libnet_ctx,
458                                                 state->domain->info->sid);
459
460         composite_done(state->ctx);
461 }
462
463 NTSTATUS wb_init_domain_recv(struct composite_context *c,
464                              TALLOC_CTX *mem_ctx,
465                              struct wbsrv_domain **result)
466 {
467         NTSTATUS status = composite_wait(c);
468         if (NT_STATUS_IS_OK(status)) {
469                 struct init_domain_state *state =
470                         talloc_get_type(c->private_data,
471                                         struct init_domain_state);
472                 *result = talloc_steal(mem_ctx, state->domain);
473         }
474         talloc_free(c);
475         return status;
476 }
477
478 NTSTATUS wb_init_domain(TALLOC_CTX *mem_ctx, struct wbsrv_service *service,
479                         struct wb_dom_info *dom_info,
480                         struct wbsrv_domain **result)
481 {
482         struct composite_context *c =
483                 wb_init_domain_send(mem_ctx, service, dom_info);
484         return wb_init_domain_recv(c, mem_ctx, result);
485 }