2 Unix SMB/CIFS implementation.
4 A composite API for initializing a domain
6 Copyright (C) Volker Lendecke 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "libcli/composite/composite.h"
25 #include "libcli/smb_composite/smb_composite.h"
26 #include "winbind/wb_async_helpers.h"
27 #include "winbind/wb_server.h"
28 #include "smbd/service_stream.h"
29 #include "smbd/service_task.h"
30 #include "dlinklist.h"
32 #include "librpc/gen_ndr/nbt.h"
33 #include "librpc/gen_ndr/samr.h"
34 #include "lib/messaging/irpc.h"
35 #include "librpc/gen_ndr/irpc.h"
36 #include "librpc/gen_ndr/ndr_irpc.h"
37 #include "libcli/raw/libcliraw.h"
38 #include "librpc/gen_ndr/ndr_netlogon.h"
39 #include "librpc/gen_ndr/ndr_lsa.h"
40 #include "libcli/auth/credentials.h"
44 * Initialize a domain:
46 * - With schannel credentials, try to open the SMB connection with the
47 * machine creds. This works against W2k3SP1 with an NTLMSSP session
48 * setup. Fall back to anonymous.
50 * - If we have schannel creds, do the auth2 and open the schannel'ed netlogon
53 * - Open LSA. If we have machine creds, try to open with ntlmssp. Fall back
54 * to schannel and then to anon bind.
56 * - With queryinfopolicy, verify that we're talking to the right domain
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.
62 * Not sure if it is overkill, but it seems to work.
65 struct init_domain_state {
66 struct composite_context *ctx;
67 struct wbsrv_domain *domain;
68 struct wbsrv_service *service;
71 struct nbt_dc_name *dcs;
74 struct smb_composite_connect conn;
76 struct dcerpc_pipe *auth2_pipe;
77 struct dcerpc_pipe *netlogon_pipe;
79 struct dcerpc_pipe *lsa_pipe;
80 struct policy_handle *lsa_policy;
82 struct dcerpc_pipe *samr_pipe;
83 struct policy_handle *samr_handle;
84 struct policy_handle *domain_handle;
86 struct ldap_connection *ldap_conn;
88 struct lsa_QueryInfoPolicy queryinfo;
91 static void init_domain_recv_dcs(struct composite_context *ctx);
92 static void init_domain_recv_dcip(struct composite_context *ctx);
93 static void init_domain_recv_tree(struct composite_context *ctx);
94 static void init_domain_recv_netlogoncreds(struct composite_context *ctx);
95 static void init_domain_recv_netlogonpipe(struct composite_context *ctx);
96 static void init_domain_recv_lsa(struct composite_context *ctx);
97 static void init_domain_recv_queryinfo(struct rpc_request *req);
98 static void init_domain_recv_ldapconn(struct composite_context *ctx);
99 static void init_domain_recv_samr(struct composite_context *ctx);
101 struct composite_context *wb_init_domain_send(struct wbsrv_service *service,
102 struct wbsrv_domain *domain)
104 struct composite_context *result, *ctx;
105 struct init_domain_state *state;
107 result = talloc(domain, struct composite_context);
108 if (result == NULL) goto failed;
109 result->state = COMPOSITE_STATE_IN_PROGRESS;
110 result->async.fn = NULL;
111 result->event_ctx = service->task->event_ctx;
113 state = talloc_zero(result, struct init_domain_state);
114 if (state == NULL) goto failed;
116 result->private_data = state;
118 state->service = service;
119 state->domain = domain;
121 if (domain->dcname != NULL) {
122 struct nbt_name name;
123 make_nbt_name(&name, domain->dcname, 0x20);
124 ctx = resolve_name_send(&name, result->event_ctx,
125 lp_name_resolve_order());
126 if (ctx == NULL) goto failed;
127 ctx->async.fn = init_domain_recv_dcip;
128 ctx->async.private_data = state;
132 if (state->domain->schannel_creds != NULL) {
133 talloc_free(state->domain->schannel_creds);
136 state->domain->schannel_creds = cli_credentials_init(state->domain);
137 if (state->domain->schannel_creds == NULL) goto failed;
138 cli_credentials_set_conf(state->domain->schannel_creds);
140 cli_credentials_set_machine_account(state->domain->
142 if (!NT_STATUS_IS_OK(state->ctx->status)) goto failed;
144 ctx = wb_finddcs_send(domain->name, domain->sid, result->event_ctx,
145 service->task->msg_ctx);
146 if (ctx == NULL) goto failed;
148 ctx->async.fn = init_domain_recv_dcs;
149 ctx->async.private_data = state;
157 static void init_domain_recv_dcs(struct composite_context *ctx)
159 struct init_domain_state *state =
160 talloc_get_type(ctx->async.private_data,
161 struct init_domain_state);
163 state->ctx->status = wb_finddcs_recv(ctx, state, &state->num_dcs,
165 if (!composite_is_ok(state->ctx)) return;
167 if (state->num_dcs < 1) {
168 composite_error(state->ctx, NT_STATUS_NO_LOGON_SERVERS);
172 state->dcaddr = state->dcs[0].address;
174 state->conn.in.dest_host = state->dcs[0].address;
175 state->conn.in.port = 0;
176 state->conn.in.called_name = state->dcs[0].name;
177 state->conn.in.service = "IPC$";
178 state->conn.in.service_type = "IPC";
179 state->conn.in.workgroup = state->domain->name;
181 state->conn.in.credentials = state->domain->schannel_creds;
183 if (state->conn.in.credentials == NULL) {
184 state->conn.in.credentials = cli_credentials_init(state);
185 if (composite_nomem(state->conn.in.credentials, state->ctx)) {
188 cli_credentials_set_conf(state->conn.in.credentials);
189 cli_credentials_set_anonymous(state->conn.in.credentials);
192 state->conn.in.fallback_to_anonymous = True;
194 ctx = smb_composite_connect_send(&state->conn, state,
195 state->ctx->event_ctx);
196 composite_continue(state->ctx, ctx, init_domain_recv_tree, state);
199 static void init_domain_recv_dcip(struct composite_context *ctx)
201 struct init_domain_state *state =
202 talloc_get_type(ctx->async.private_data,
203 struct init_domain_state);
205 state->ctx->status = resolve_name_recv(ctx, state, &state->dcaddr);
206 if (!composite_is_ok(state->ctx)) return;
208 state->conn.in.dest_host = state->dcaddr;
209 state->conn.in.port = 0;
210 state->conn.in.called_name = state->domain->dcname;
211 state->conn.in.service = "IPC$";
212 state->conn.in.service_type = "IPC";
213 state->conn.in.workgroup = state->domain->name;
214 state->conn.in.credentials = state->domain->schannel_creds;
216 state->conn.in.fallback_to_anonymous = True;
218 ctx = smb_composite_connect_send(&state->conn, state,
219 state->ctx->event_ctx);
220 composite_continue(state->ctx, ctx, init_domain_recv_tree, state);
223 static void init_domain_recv_tree(struct composite_context *ctx)
225 struct init_domain_state *state =
226 talloc_get_type(ctx->async.private_data,
227 struct init_domain_state);
228 state->ctx->status = smb_composite_connect_recv(ctx, state);
229 if (!composite_is_ok(state->ctx)) return;
231 if ((state->domain->schannel_creds != NULL) &&
232 (!cli_credentials_is_anonymous(state->domain->schannel_creds)) &&
233 ((lp_server_role() == ROLE_DOMAIN_MEMBER) &&
234 (dom_sid_equal(state->domain->sid,
235 state->service->primary_sid)))) {
236 ctx = wb_get_schannel_creds_send(state->domain->schannel_creds,
237 state->conn.out.tree,
238 state->ctx->event_ctx);
239 composite_continue(state->ctx, ctx,
240 init_domain_recv_netlogoncreds, state);
244 ctx = wb_connect_lsa_send(state->conn.out.tree, NULL);
245 composite_continue(state->ctx, ctx, init_domain_recv_lsa, state);
248 static void init_domain_recv_netlogoncreds(struct composite_context *ctx)
250 struct init_domain_state *state =
251 talloc_get_type(ctx->async.private_data,
252 struct init_domain_state);
253 struct smbcli_tree *tree = NULL;
255 state->ctx->status = wb_get_schannel_creds_recv(ctx, state,
257 if (!composite_is_ok(state->ctx)) return;
259 talloc_unlink(state, state->conn.out.tree); /* The pipe owns it now */
261 if (!lp_winbind_sealed_pipes()) {
262 state->netlogon_pipe = talloc_reference(state,
264 ctx = wb_connect_lsa_send(state->conn.out.tree, NULL);
265 composite_continue(state->ctx, ctx, init_domain_recv_lsa,
270 state->netlogon_pipe = dcerpc_pipe_init(state, state->ctx->event_ctx);
271 if (composite_nomem(state->netlogon_pipe, state->ctx)) return;
273 if (state->auth2_pipe != NULL) {
274 tree = dcerpc_smb_tree(state->auth2_pipe->conn);
278 composite_error(state->ctx, NT_STATUS_INTERNAL_ERROR);
282 ctx = dcerpc_pipe_open_smb_send(state->netlogon_pipe->conn, tree,
284 composite_continue(state->ctx, ctx,
285 init_domain_recv_netlogonpipe, state);
288 static void init_domain_recv_netlogonpipe(struct composite_context *ctx)
290 struct init_domain_state *state =
291 talloc_get_type(ctx->async.private_data,
292 struct init_domain_state);
294 state->ctx->status = dcerpc_pipe_open_smb_recv(ctx);
295 if (!composite_is_ok(state->ctx)) return;
297 state->netlogon_pipe->conn->flags |= (DCERPC_SIGN | DCERPC_SEAL);
299 dcerpc_bind_auth_password(state->netlogon_pipe,
300 DCERPC_NETLOGON_UUID,
301 DCERPC_NETLOGON_VERSION,
302 state->domain->schannel_creds,
303 DCERPC_AUTH_TYPE_SCHANNEL,
305 if (!composite_is_ok(state->ctx)) return;
307 ctx = wb_connect_lsa_send(state->conn.out.tree,
308 state->domain->schannel_creds);
309 composite_continue(state->ctx, ctx, init_domain_recv_lsa, state);
312 static void init_domain_recv_lsa(struct composite_context *ctx)
314 struct init_domain_state *state =
315 talloc_get_type(ctx->async.private_data,
316 struct init_domain_state);
318 struct rpc_request *req;
320 state->ctx->status = wb_connect_lsa_recv(ctx, state,
321 &state->domain->lsa_auth_type,
324 if (!composite_is_ok(state->ctx)) return;
326 if (state->auth2_pipe == NULL) {
327 /* Give the tree to the LSA pipe. If auth2_pipe exists we have
328 * given it to that already */
329 talloc_unlink(state, state->conn.out.tree);
332 state->queryinfo.in.handle = state->lsa_policy;
333 state->queryinfo.in.level = LSA_POLICY_INFO_ACCOUNT_DOMAIN;
335 req = dcerpc_lsa_QueryInfoPolicy_send(state->lsa_pipe, state,
337 composite_continue_rpc(state->ctx, req,
338 init_domain_recv_queryinfo, state);
341 static void init_domain_recv_queryinfo(struct rpc_request *req)
343 struct init_domain_state *state =
344 talloc_get_type(req->async.private, struct init_domain_state);
345 struct lsa_DomainInfo *dominfo;
346 struct composite_context *ctx;
347 const char *ldap_url;
349 state->ctx->status = dcerpc_ndr_request_recv(req);
350 if (!composite_is_ok(state->ctx)) return;
351 state->ctx->status = state->queryinfo.out.result;
352 if (!composite_is_ok(state->ctx)) return;
354 dominfo = &state->queryinfo.out.info->account_domain;
356 if (strcasecmp(state->domain->name, dominfo->name.string) != 0) {
357 DEBUG(2, ("Expected domain name %s, DC %s said %s\n",
359 dcerpc_server_name(state->lsa_pipe),
360 dominfo->name.string));
361 composite_error(state->ctx, NT_STATUS_INVALID_DOMAIN_STATE);
365 if (!dom_sid_equal(state->domain->sid, dominfo->sid)) {
366 DEBUG(2, ("Expected domain sid %s, DC %s said %s\n",
367 dom_sid_string(state, state->domain->sid),
368 dcerpc_server_name(state->lsa_pipe),
369 dom_sid_string(state, dominfo->sid)));
370 composite_error(state->ctx, NT_STATUS_INVALID_DOMAIN_STATE);
374 state->ldap_conn = ldap_new_connection(state, state->ctx->event_ctx);
375 composite_nomem(state->ldap_conn, state->ctx);
377 ldap_url = talloc_asprintf(state, "ldap://%s/", state->dcaddr);
378 composite_nomem(ldap_url, state->ctx);
380 ctx = ldap_connect_send(state->ldap_conn, ldap_url);
381 composite_continue(state->ctx, ctx, init_domain_recv_ldapconn, state);
384 static void init_domain_recv_ldapconn(struct composite_context *ctx)
386 struct init_domain_state *state =
387 talloc_get_type(ctx->async.private_data,
388 struct init_domain_state);
390 state->ctx->status = ldap_connect_recv(ctx);
391 DEBUG(0, ("ldap_connect returned %s\n",
392 nt_errstr(state->ctx->status)));
394 state->samr_pipe = dcerpc_pipe_init(state, state->ctx->event_ctx);
395 if (composite_nomem(state->samr_pipe, state->ctx)) return;
397 ctx = wb_connect_sam_send(state->conn.out.tree,
398 state->domain->lsa_auth_type,
399 state->domain->schannel_creds,
401 composite_continue(state->ctx, ctx, init_domain_recv_samr, state);
404 static void init_domain_recv_samr(struct composite_context *ctx)
406 struct init_domain_state *state =
407 talloc_get_type(ctx->async.private_data,
408 struct init_domain_state);
410 state->ctx->status = wb_connect_sam_recv(ctx, state, &state->samr_pipe,
412 &state->domain_handle);
413 if (!composite_is_ok(state->ctx)) return;
415 composite_done(state->ctx);
418 NTSTATUS wb_init_domain_recv(struct composite_context *c)
420 NTSTATUS status = composite_wait(c);
421 if (NT_STATUS_IS_OK(status)) {
422 struct init_domain_state *state =
423 talloc_get_type(c->private_data,
424 struct init_domain_state);
425 struct wbsrv_domain *domain = state->domain;
427 talloc_free(domain->netlogon_auth2_pipe);
428 domain->netlogon_auth2_pipe =
429 talloc_steal(domain, state->auth2_pipe);
431 talloc_free(domain->netlogon_pipe);
432 domain->netlogon_pipe =
433 talloc_steal(domain, state->netlogon_pipe);
435 talloc_free(domain->lsa_pipe);
437 talloc_steal(domain, state->lsa_pipe);
439 talloc_free(domain->lsa_policy);
441 talloc_steal(domain, state->lsa_policy);
443 talloc_free(domain->samr_pipe);
445 talloc_steal(domain, state->samr_pipe);
447 talloc_free(domain->samr_handle);
448 domain->samr_handle =
449 talloc_steal(domain, state->samr_handle);
451 talloc_free(domain->domain_handle);
452 domain->domain_handle =
453 talloc_steal(domain, state->domain_handle);
455 domain->initialized = True;
461 NTSTATUS wb_init_domain(struct wbsrv_service *service,
462 struct wbsrv_domain *domain)
464 struct composite_context *c =
465 wb_init_domain_send(service, domain);
466 return wb_init_domain_recv(c);