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.
25 #include "libcli/composite/composite.h"
26 #include "libcli/smb_composite/smb_composite.h"
27 #include "winbind/wb_server.h"
28 #include "winbind/wb_async_helpers.h"
29 #include "winbind/wb_helper.h"
30 #include "smbd/service_task.h"
31 #include "librpc/gen_ndr/ndr_netlogon.h"
32 #include "librpc/gen_ndr/ndr_lsa_c.h"
33 #include "librpc/gen_ndr/ndr_security.h"
35 #include "libcli/auth/credentials.h"
36 #include "libcli/security/proto.h"
38 #include "libcli/ldap/ldap_client.h"
40 #include "auth/credentials/credentials.h"
43 * Initialize a domain:
45 * - With schannel credentials, try to open the SMB connection with the
46 * machine creds. This works against W2k3SP1 with an NTLMSSP session
47 * setup. Fall back to anonymous.
49 * - If we have schannel creds, do the auth2 and open the schannel'ed netlogon
52 * - Open LSA. If we have machine creds, try to open with ntlmssp. Fall back
53 * to schannel and then to anon bind.
55 * - With queryinfopolicy, verify that we're talking to the right domain
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.
61 * Not sure if it is overkill, but it seems to work.
64 struct init_domain_state {
65 struct composite_context *ctx;
66 struct wbsrv_domain *domain;
67 struct wbsrv_service *service;
69 struct smb_composite_connect conn;
71 struct lsa_QueryInfoPolicy queryinfo;
74 static void init_domain_recv_tree(struct composite_context *ctx);
75 static void init_domain_recv_netlogoncreds(struct composite_context *ctx);
76 static void init_domain_recv_netlogonpipe(struct composite_context *ctx);
77 static void init_domain_recv_schannel(struct composite_context *ctx);
78 static void init_domain_recv_lsa(struct composite_context *ctx);
79 static void init_domain_recv_queryinfo(struct rpc_request *req);
80 static void init_domain_recv_ldapconn(struct composite_context *ctx);
81 static void init_domain_recv_samr(struct composite_context *ctx);
83 struct composite_context *wb_init_domain_send(TALLOC_CTX *mem_ctx,
84 struct wbsrv_service *service,
85 struct wb_dom_info *dom_info)
87 struct composite_context *result, *ctx;
88 struct init_domain_state *state;
90 result = talloc(mem_ctx, struct composite_context);
91 if (result == NULL) goto failed;
92 result->state = COMPOSITE_STATE_IN_PROGRESS;
93 result->async.fn = NULL;
94 result->event_ctx = service->task->event_ctx;
96 state = talloc_zero(result, struct init_domain_state);
97 if (state == NULL) goto failed;
99 result->private_data = state;
101 state->service = service;
103 state->domain = talloc(state, struct wbsrv_domain);
104 if (state->domain == NULL) goto failed;
106 state->domain->info = talloc_reference(state->domain, dom_info);
107 if (state->domain->info == NULL) goto failed;
109 state->domain->schannel_creds = cli_credentials_init(state->domain);
110 if (state->domain->schannel_creds == NULL) goto failed;
112 cli_credentials_set_conf(state->domain->schannel_creds);
114 cli_credentials_set_machine_account(state->domain->
116 if (!NT_STATUS_IS_OK(state->ctx->status)) goto failed;
118 state->conn.in.dest_host = dom_info->dc_address;
119 state->conn.in.port = 0;
120 state->conn.in.called_name = dom_info->dc_name;
121 state->conn.in.service = "IPC$";
122 state->conn.in.service_type = "IPC";
123 state->conn.in.workgroup = dom_info->name;
124 state->conn.in.credentials = state->domain->schannel_creds;
126 state->conn.in.fallback_to_anonymous = True;
128 ctx = smb_composite_connect_send(&state->conn, state->domain,
130 if (ctx == NULL) goto failed;
132 ctx->async.fn = init_domain_recv_tree;
133 ctx->async.private_data = state;
141 static void init_domain_recv_tree(struct composite_context *ctx)
143 struct init_domain_state *state =
144 talloc_get_type(ctx->async.private_data,
145 struct init_domain_state);
146 state->ctx->status = smb_composite_connect_recv(ctx, state);
147 if (!composite_is_ok(state->ctx)) return;
149 if ((state->domain->schannel_creds != NULL) &&
150 (!cli_credentials_is_anonymous(state->domain->schannel_creds)) &&
151 ((lp_server_role() == ROLE_DOMAIN_MEMBER) &&
152 (dom_sid_equal(state->domain->info->sid,
153 state->service->primary_sid)))) {
154 ctx = wb_get_schannel_creds_send(state,
155 state->domain->schannel_creds,
156 state->conn.out.tree,
157 state->ctx->event_ctx);
158 composite_continue(state->ctx, ctx,
159 init_domain_recv_netlogoncreds, state);
163 ctx = wb_connect_lsa_send(state, state->conn.out.tree, NULL);
164 composite_continue(state->ctx, ctx, init_domain_recv_lsa, state);
167 static void init_domain_recv_netlogoncreds(struct composite_context *ctx)
169 struct init_domain_state *state =
170 talloc_get_type(ctx->async.private_data,
171 struct init_domain_state);
172 struct dcerpc_pipe *auth2_pipe;
173 struct smbcli_tree *tree = NULL;
176 wb_get_schannel_creds_recv(ctx, state, &auth2_pipe);
177 if (!composite_is_ok(state->ctx)) return;
179 if (!lp_winbind_sealed_pipes()) {
180 state->domain->netlogon_pipe = talloc_reference(state->domain,
182 ctx = wb_connect_lsa_send(state, state->conn.out.tree, NULL);
183 composite_continue(state->ctx, ctx, init_domain_recv_lsa,
188 state->domain->netlogon_pipe =
189 dcerpc_pipe_init(state->domain, state->ctx->event_ctx);
190 if (composite_nomem(state->domain->netlogon_pipe, state->ctx)) return;
192 tree = dcerpc_smb_tree(auth2_pipe->conn);
194 composite_error(state->ctx, NT_STATUS_INTERNAL_ERROR);
198 ctx = dcerpc_pipe_open_smb_send(state->domain->netlogon_pipe->conn,
200 composite_continue(state->ctx, ctx, init_domain_recv_netlogonpipe,
204 static void init_domain_recv_netlogonpipe(struct composite_context *ctx)
206 struct init_domain_state *state =
207 talloc_get_type(ctx->async.private_data,
208 struct init_domain_state);
210 state->ctx->status = dcerpc_pipe_open_smb_recv(ctx);
211 if (!composite_is_ok(state->ctx)) return;
213 state->domain->netlogon_pipe->conn->flags |=
214 (DCERPC_SIGN | DCERPC_SEAL);
215 ctx = dcerpc_bind_auth_send(state, state->domain->netlogon_pipe,
216 &dcerpc_table_netlogon,
217 state->domain->schannel_creds,
218 DCERPC_AUTH_TYPE_SCHANNEL,
219 DCERPC_AUTH_LEVEL_PRIVACY,
221 composite_continue(state->ctx, ctx, init_domain_recv_schannel, state);
224 static void init_domain_recv_schannel(struct composite_context *ctx)
226 struct init_domain_state *state =
227 talloc_get_type(ctx->async.private_data,
228 struct init_domain_state);
230 state->ctx->status = dcerpc_bind_auth_recv(ctx);
231 if (!composite_is_ok(state->ctx)) return;
233 ctx = wb_connect_lsa_send(state, state->conn.out.tree,
234 state->domain->schannel_creds);
235 composite_continue(state->ctx, ctx, init_domain_recv_lsa, state);
238 static void init_domain_recv_lsa(struct composite_context *ctx)
240 struct init_domain_state *state =
241 talloc_get_type(ctx->async.private_data,
242 struct init_domain_state);
244 struct rpc_request *req;
246 state->ctx->status = wb_connect_lsa_recv(ctx, state->domain,
247 &state->domain->lsa_auth_type,
248 &state->domain->lsa_pipe,
249 &state->domain->lsa_policy);
250 if (!composite_is_ok(state->ctx)) return;
252 /* Give the tree to the pipes. */
253 talloc_unlink(state, state->conn.out.tree);
255 state->queryinfo.in.handle = state->domain->lsa_policy;
256 state->queryinfo.in.level = LSA_POLICY_INFO_ACCOUNT_DOMAIN;
258 req = dcerpc_lsa_QueryInfoPolicy_send(state->domain->lsa_pipe, state,
260 composite_continue_rpc(state->ctx, req,
261 init_domain_recv_queryinfo, state);
264 static void init_domain_recv_queryinfo(struct rpc_request *req)
266 struct init_domain_state *state =
267 talloc_get_type(req->async.private, struct init_domain_state);
268 struct lsa_DomainInfo *dominfo;
269 struct composite_context *ctx;
270 const char *ldap_url;
272 state->ctx->status = dcerpc_ndr_request_recv(req);
273 if (!composite_is_ok(state->ctx)) return;
274 state->ctx->status = state->queryinfo.out.result;
275 if (!composite_is_ok(state->ctx)) return;
277 dominfo = &state->queryinfo.out.info->account_domain;
279 if (strcasecmp(state->domain->info->name, dominfo->name.string) != 0) {
280 DEBUG(2, ("Expected domain name %s, DC %s said %s\n",
281 state->domain->info->name,
282 dcerpc_server_name(state->domain->lsa_pipe),
283 dominfo->name.string));
284 composite_error(state->ctx, NT_STATUS_INVALID_DOMAIN_STATE);
288 if (!dom_sid_equal(state->domain->info->sid, dominfo->sid)) {
289 DEBUG(2, ("Expected domain sid %s, DC %s said %s\n",
290 dom_sid_string(state, state->domain->info->sid),
291 dcerpc_server_name(state->domain->lsa_pipe),
292 dom_sid_string(state, dominfo->sid)));
293 composite_error(state->ctx, NT_STATUS_INVALID_DOMAIN_STATE);
297 state->domain->ldap_conn =
298 ldap_new_connection(state->domain, state->ctx->event_ctx);
299 composite_nomem(state->domain->ldap_conn, state->ctx);
301 ldap_url = talloc_asprintf(state, "ldap://%s/",
302 state->domain->info->dc_address);
303 composite_nomem(ldap_url, state->ctx);
305 ctx = ldap_connect_send(state->domain->ldap_conn, ldap_url);
306 composite_continue(state->ctx, ctx, init_domain_recv_ldapconn, state);
309 static void init_domain_recv_ldapconn(struct composite_context *ctx)
311 struct init_domain_state *state =
312 talloc_get_type(ctx->async.private_data,
313 struct init_domain_state);
315 state->ctx->status = ldap_connect_recv(ctx);
316 if (NT_STATUS_IS_OK(state->ctx->status)) {
317 state->domain->ldap_conn->host =
318 talloc_strdup(state->domain->ldap_conn,
319 state->domain->info->dc_name);
321 ldap_bind_sasl(state->domain->ldap_conn,
322 state->domain->schannel_creds);
323 DEBUG(0, ("ldap_bind returned %s\n",
324 nt_errstr(state->ctx->status)));
327 state->domain->samr_pipe =
328 dcerpc_pipe_init(state->domain, state->ctx->event_ctx);
329 if (composite_nomem(state->domain->samr_pipe, state->ctx)) return;
331 ctx = wb_connect_sam_send(state, state->conn.out.tree,
332 state->domain->lsa_auth_type,
333 state->domain->schannel_creds,
334 state->domain->info->sid);
335 composite_continue(state->ctx, ctx, init_domain_recv_samr, state);
338 static void init_domain_recv_samr(struct composite_context *ctx)
340 struct init_domain_state *state =
341 talloc_get_type(ctx->async.private_data,
342 struct init_domain_state);
344 state->ctx->status = wb_connect_sam_recv(
345 ctx, state->domain, &state->domain->samr_pipe,
346 &state->domain->samr_handle, &state->domain->domain_handle);
347 if (!composite_is_ok(state->ctx)) return;
349 composite_done(state->ctx);
352 NTSTATUS wb_init_domain_recv(struct composite_context *c,
354 struct wbsrv_domain **result)
356 NTSTATUS status = composite_wait(c);
357 if (NT_STATUS_IS_OK(status)) {
358 struct init_domain_state *state =
359 talloc_get_type(c->private_data,
360 struct init_domain_state);
361 *result = talloc_steal(mem_ctx, state->domain);
367 NTSTATUS wb_init_domain(TALLOC_CTX *mem_ctx, struct wbsrv_service *service,
368 struct wb_dom_info *dom_info,
369 struct wbsrv_domain **result)
371 struct composite_context *c =
372 wb_init_domain_send(mem_ctx, service, dom_info);
373 return wb_init_domain_recv(c, mem_ctx, result);