s3-torture: run_fdsesstest(): replace cli_read_old() with cli_read()
[Samba/gebeck_regimport.git] / source4 / winbind / wb_init_domain.c
blob9847afbba052ca4545822253ee4cc932e17e01bf
1 /*
2 Unix SMB/CIFS implementation.
4 A composite API for initializing a domain
6 Copyright (C) Volker Lendecke 2005
7 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2007
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.
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.
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/>.
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"
32 #include "libcli/security/security.h"
35 #include "auth/credentials/credentials.h"
36 #include "param/param.h"
39 * Initialize a domain:
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).
45 * - If we have schannel creds, do the auth2 and open the schannel'ed netlogon
46 * pipe.
48 * - Open LSA. If we have machine creds, try to open with SPNEGO or NTLMSSP. Fall back
49 * to schannel.
51 * - With queryinfopolicy, verify that we're talking to the right domain
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.
57 * Not sure if it is overkill, but it seems to work.
60 struct init_domain_state {
61 struct composite_context *ctx;
62 struct wbsrv_domain *domain;
63 struct wbsrv_service *service;
65 struct lsa_ObjectAttribute objectattr;
66 struct lsa_OpenPolicy2 lsa_openpolicy;
67 struct lsa_QueryInfoPolicy queryinfo;
68 union lsa_PolicyInformation *info;
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);
77 static struct dcerpc_binding *init_domain_binding(struct init_domain_state *state,
78 const struct ndr_interface_table *table)
80 struct dcerpc_binding *binding;
81 NTSTATUS status;
83 /* Make a binding string */
85 char *s = talloc_asprintf(state, "ncacn_np:%s", state->domain->dc_name);
86 if (s == NULL) return NULL;
87 status = dcerpc_parse_binding(state, s, &binding);
88 talloc_free(s);
89 if (!NT_STATUS_IS_OK(status)) {
90 return NULL;
94 /* Alter binding to contain hostname, but also address (so we don't look it up twice) */
95 binding->target_hostname = state->domain->dc_name;
96 binding->host = state->domain->dc_address;
98 /* This shouldn't make a network call, as the mappings for named pipes are well known */
99 status = dcerpc_epm_map_binding(binding, binding, table, state->service->task->event_ctx,
100 state->service->task->lp_ctx);
101 if (!NT_STATUS_IS_OK(status)) {
102 return NULL;
105 return binding;
108 struct composite_context *wb_init_domain_send(TALLOC_CTX *mem_ctx,
109 struct wbsrv_service *service,
110 struct wb_dom_info *dom_info)
112 struct composite_context *result, *ctx;
113 struct init_domain_state *state;
115 result = composite_create(mem_ctx, service->task->event_ctx);
116 if (result == NULL) goto failed;
118 state = talloc_zero(result, struct init_domain_state);
119 if (state == NULL) goto failed;
120 state->ctx = result;
121 result->private_data = state;
123 state->service = service;
125 state->domain = talloc(state, struct wbsrv_domain);
126 if (state->domain == NULL) goto failed;
128 state->domain->info = talloc_reference(state->domain, dom_info);
129 if (state->domain->info == NULL) goto failed;
131 state->domain->dc_name = dom_info->dc->name;
132 state->domain->dc_address = dom_info->dc->address;
134 state->domain->libnet_ctx = libnet_context_init(service->task->event_ctx,
135 service->task->lp_ctx);
137 /* Create a credentials structure */
138 state->domain->libnet_ctx->cred = cli_credentials_init(state->domain);
139 if (state->domain->libnet_ctx->cred == NULL) goto failed;
141 cli_credentials_set_conf(state->domain->libnet_ctx->cred, service->task->lp_ctx);
143 /* Connect the machine account to the credentials */
144 state->ctx->status =
145 cli_credentials_set_machine_account(state->domain->libnet_ctx->cred, state->domain->libnet_ctx->lp_ctx);
146 if (!NT_STATUS_IS_OK(state->ctx->status)) goto failed;
148 state->domain->netlogon_binding = init_domain_binding(state, &ndr_table_netlogon);
150 state->domain->netlogon_pipe = NULL;
152 if ((!cli_credentials_is_anonymous(state->domain->libnet_ctx->cred)) &&
153 ((lpcfg_server_role(service->task->lp_ctx) == ROLE_DOMAIN_MEMBER) ||
154 (lpcfg_server_role(service->task->lp_ctx) == ROLE_DOMAIN_CONTROLLER)) &&
155 (dom_sid_equal(state->domain->info->sid,
156 state->service->primary_sid))) {
157 state->domain->netlogon_binding->flags |= DCERPC_SCHANNEL | DCERPC_SCHANNEL_128;
159 /* For debugging, it can be a real pain if all the traffic is encrypted */
160 if (lpcfg_winbind_sealed_pipes(service->task->lp_ctx)) {
161 state->domain->netlogon_binding->flags |= (DCERPC_SIGN | DCERPC_SEAL );
162 } else {
163 state->domain->netlogon_binding->flags |= (DCERPC_SIGN);
167 /* No encryption on anonymous pipes */
169 ctx = dcerpc_pipe_connect_b_send(state, state->domain->netlogon_binding,
170 &ndr_table_netlogon,
171 state->domain->libnet_ctx->cred,
172 service->task->event_ctx,
173 service->task->lp_ctx);
175 if (composite_nomem(ctx, state->ctx)) {
176 goto failed;
179 composite_continue(state->ctx, ctx, init_domain_recv_netlogonpipe,
180 state);
181 return result;
182 failed:
183 talloc_free(result);
184 return NULL;
187 /* Having make a netlogon connection (possibly secured with schannel),
188 * make an LSA connection to the same DC, on the same IPC$ share */
189 static void init_domain_recv_netlogonpipe(struct composite_context *ctx)
191 struct init_domain_state *state =
192 talloc_get_type(ctx->async.private_data,
193 struct init_domain_state);
195 state->ctx->status = dcerpc_pipe_connect_b_recv(ctx, state->domain,
196 &state->domain->netlogon_pipe);
198 if (!composite_is_ok(state->ctx)) {
199 return;
201 talloc_reparent(state, state->domain->netlogon_pipe, state->domain->netlogon_binding);
203 state->domain->lsa_binding = init_domain_binding(state, &ndr_table_lsarpc);
205 /* For debugging, it can be a real pain if all the traffic is encrypted */
206 if (lpcfg_winbind_sealed_pipes(state->service->task->lp_ctx)) {
207 state->domain->lsa_binding->flags |= (DCERPC_SIGN | DCERPC_SEAL );
208 } else {
209 state->domain->lsa_binding->flags |= (DCERPC_SIGN);
212 state->domain->libnet_ctx->lsa.pipe = NULL;
214 /* this will make the secondary connection on the same IPC$ share,
215 secured with SPNEGO or NTLMSSP */
216 ctx = dcerpc_secondary_auth_connection_send(state->domain->netlogon_pipe,
217 state->domain->lsa_binding,
218 &ndr_table_lsarpc,
219 state->domain->libnet_ctx->cred,
220 state->domain->libnet_ctx->lp_ctx
222 composite_continue(state->ctx, ctx, init_domain_recv_lsa_pipe, state);
225 static bool retry_with_schannel(struct init_domain_state *state,
226 struct dcerpc_binding *binding,
227 const struct ndr_interface_table *table,
228 void (*continuation)(struct composite_context *))
230 struct composite_context *ctx;
231 state->ctx->status = NT_STATUS_OK;
232 if (state->domain->netlogon_binding->flags & DCERPC_SCHANNEL
233 && !(binding->flags & DCERPC_SCHANNEL)) {
234 /* Opening a policy handle failed, perhaps it was
235 * because we don't get a 'wrong password' error on
236 * NTLMSSP binds */
238 /* Try again with schannel */
239 binding->flags |= DCERPC_SCHANNEL;
241 /* Try again, likewise on the same IPC$ share,
242 secured with SCHANNEL */
243 ctx = dcerpc_secondary_auth_connection_send(state->domain->netlogon_pipe,
244 binding,
245 table,
246 state->domain->libnet_ctx->cred,
247 state->domain->libnet_ctx->lp_ctx);
248 composite_continue(state->ctx, ctx, continuation, state);
249 return true;
250 } else {
251 return false;
254 /* We should now have either an authenticated LSA pipe, or an error.
255 * On success, open a policy handle
257 static void init_domain_recv_lsa_pipe(struct composite_context *ctx)
259 struct init_domain_state *state =
260 talloc_get_type(ctx->async.private_data,
261 struct init_domain_state);
262 struct tevent_req *subreq;
264 state->ctx->status = dcerpc_secondary_auth_connection_recv(ctx, state->domain,
265 &state->domain->libnet_ctx->lsa.pipe);
266 if (NT_STATUS_EQUAL(state->ctx->status, NT_STATUS_LOGON_FAILURE)) {
267 if (retry_with_schannel(state, state->domain->lsa_binding,
268 &ndr_table_lsarpc,
269 init_domain_recv_lsa_pipe)) {
270 return;
273 if (!composite_is_ok(state->ctx)) return;
275 talloc_steal(state->domain->libnet_ctx, state->domain->libnet_ctx->lsa.pipe);
276 talloc_reparent(state, state->domain->libnet_ctx->lsa.pipe, state->domain->lsa_binding);
277 state->domain->libnet_ctx->lsa.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
278 state->domain->libnet_ctx->lsa.name = state->domain->info->name;
280 ZERO_STRUCT(state->domain->libnet_ctx->lsa.handle);
281 state->lsa_openpolicy.in.system_name =
282 talloc_asprintf(state, "\\\\%s",
283 dcerpc_server_name(state->domain->libnet_ctx->lsa.pipe));
284 ZERO_STRUCT(state->objectattr);
285 state->lsa_openpolicy.in.attr = &state->objectattr;
286 state->lsa_openpolicy.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
287 state->lsa_openpolicy.out.handle = &state->domain->libnet_ctx->lsa.handle;
289 subreq = dcerpc_lsa_OpenPolicy2_r_send(state,
290 state->ctx->event_ctx,
291 state->domain->libnet_ctx->lsa.pipe->binding_handle,
292 &state->lsa_openpolicy);
293 if (composite_nomem(subreq, state->ctx)) return;
294 tevent_req_set_callback(subreq, init_domain_recv_lsa_policy, state);
297 /* Receive a policy handle (or not, and retry the authentication) and
298 * obtain some basic information about the domain */
300 static void init_domain_recv_lsa_policy(struct tevent_req *subreq)
302 struct init_domain_state *state =
303 tevent_req_callback_data(subreq,
304 struct init_domain_state);
306 state->ctx->status = dcerpc_lsa_OpenPolicy2_r_recv(subreq, state);
307 TALLOC_FREE(subreq);
308 if ((!NT_STATUS_IS_OK(state->ctx->status)
309 || !NT_STATUS_IS_OK(state->lsa_openpolicy.out.result))) {
310 if (retry_with_schannel(state, state->domain->lsa_binding,
311 &ndr_table_lsarpc,
312 init_domain_recv_lsa_pipe)) {
313 return;
316 if (!composite_is_ok(state->ctx)) return;
317 state->ctx->status = state->lsa_openpolicy.out.result;
318 if (!composite_is_ok(state->ctx)) return;
320 state->info = talloc_zero(state->ctx, union lsa_PolicyInformation);
321 if (composite_nomem(state->info, state->ctx)) return;
323 state->queryinfo.in.handle = &state->domain->libnet_ctx->lsa.handle;
324 state->queryinfo.in.level = LSA_POLICY_INFO_ACCOUNT_DOMAIN;
325 state->queryinfo.out.info = &state->info;
327 subreq = dcerpc_lsa_QueryInfoPolicy_r_send(state,
328 state->ctx->event_ctx,
329 state->domain->libnet_ctx->lsa.pipe->binding_handle,
330 &state->queryinfo);
331 if (composite_nomem(subreq, state->ctx)) return;
332 tevent_req_set_callback(subreq, init_domain_recv_queryinfo, state);
335 static void init_domain_recv_queryinfo(struct tevent_req *subreq)
337 struct init_domain_state *state =
338 tevent_req_callback_data(subreq,
339 struct init_domain_state);
340 struct lsa_DomainInfo *dominfo;
341 struct composite_context *ctx;
343 state->ctx->status = dcerpc_lsa_QueryInfoPolicy_r_recv(subreq, state);
344 TALLOC_FREE(subreq);
345 if (!composite_is_ok(state->ctx)) return;
346 state->ctx->status = state->queryinfo.out.result;
347 if (!composite_is_ok(state->ctx)) return;
349 dominfo = &(*state->queryinfo.out.info)->account_domain;
351 if (strcasecmp(state->domain->info->name, dominfo->name.string) != 0) {
352 DEBUG(2, ("Expected domain name %s, DC %s said %s\n",
353 state->domain->info->name,
354 dcerpc_server_name(state->domain->libnet_ctx->lsa.pipe),
355 dominfo->name.string));
356 composite_error(state->ctx, NT_STATUS_INVALID_DOMAIN_STATE);
357 return;
360 if (!dom_sid_equal(state->domain->info->sid, dominfo->sid)) {
361 DEBUG(2, ("Expected domain sid %s, DC %s said %s\n",
362 dom_sid_string(state, state->domain->info->sid),
363 dcerpc_server_name(state->domain->libnet_ctx->lsa.pipe),
364 dom_sid_string(state, dominfo->sid)));
365 composite_error(state->ctx, NT_STATUS_INVALID_DOMAIN_STATE);
366 return;
369 state->domain->samr_binding = init_domain_binding(state, &ndr_table_samr);
371 /* We want to use the same flags as the LSA pipe did (so, if
372 * it needed schannel, then we need that here too) */
373 state->domain->samr_binding->flags = state->domain->lsa_binding->flags;
375 state->domain->libnet_ctx->samr.pipe = NULL;
377 ctx = wb_connect_samr_send(state, state->domain);
378 composite_continue(state->ctx, ctx, init_domain_recv_samr, state);
381 /* Recv the SAMR details (SamrConnect and SamrOpenDomain handle) and
382 * open an LDAP connection */
383 static void init_domain_recv_samr(struct composite_context *ctx)
385 struct init_domain_state *state =
386 talloc_get_type(ctx->async.private_data,
387 struct init_domain_state);
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.connect_handle,
393 &state->domain->libnet_ctx->samr.handle);
394 if (!composite_is_ok(state->ctx)) return;
396 talloc_reparent(state, 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 = dom_sid_dup(
400 state->domain->libnet_ctx,
401 state->domain->info->sid);
403 composite_done(state->ctx);
406 NTSTATUS wb_init_domain_recv(struct composite_context *c,
407 TALLOC_CTX *mem_ctx,
408 struct wbsrv_domain **result)
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);
417 talloc_free(c);
418 return status;
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)
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);