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_server.h"
27 #include "smbd/service_task.h"
29 #include "libcli/auth/credentials.h"
31 #include "libcli/ldap/ldap_client.h"
35 * Initialize a domain:
37 * - With schannel credentials, try to open the SMB connection with the
38 * machine creds. This works against W2k3SP1 with an NTLMSSP session
39 * setup. Fall back to anonymous.
41 * - If we have schannel creds, do the auth2 and open the schannel'ed netlogon
44 * - Open LSA. If we have machine creds, try to open with ntlmssp. Fall back
45 * to schannel and then to anon bind.
47 * - With queryinfopolicy, verify that we're talking to the right domain
49 * A bit complex, but with all the combinations I think it's the best we can
50 * get. NT4, W2k3 and W2k all have different combinations, but in the end we
51 * have a signed&sealed lsa connection on all of them.
53 * Not sure if it is overkill, but it seems to work.
56 struct init_domain_state {
57 struct composite_context *ctx;
58 struct wbsrv_domain *domain;
59 struct wbsrv_service *service;
61 struct smb_composite_connect conn;
63 struct lsa_QueryInfoPolicy queryinfo;
66 static void init_domain_recv_tree(struct composite_context *ctx);
67 static void init_domain_recv_netlogoncreds(struct composite_context *ctx);
68 static void init_domain_recv_netlogonpipe(struct composite_context *ctx);
69 static void init_domain_recv_schannel(struct composite_context *ctx);
70 static void init_domain_recv_lsa(struct composite_context *ctx);
71 static void init_domain_recv_queryinfo(struct rpc_request *req);
72 static void init_domain_recv_ldapconn(struct composite_context *ctx);
73 static void init_domain_recv_samr(struct composite_context *ctx);
75 struct composite_context *wb_init_domain_send(TALLOC_CTX *mem_ctx,
76 struct wbsrv_service *service,
77 struct wb_dom_info *dom_info)
79 struct composite_context *result, *ctx;
80 struct init_domain_state *state;
82 result = talloc(mem_ctx, struct composite_context);
83 if (result == NULL) goto failed;
84 result->state = COMPOSITE_STATE_IN_PROGRESS;
85 result->async.fn = NULL;
86 result->event_ctx = service->task->event_ctx;
88 state = talloc_zero(result, struct init_domain_state);
89 if (state == NULL) goto failed;
91 result->private_data = state;
93 state->service = service;
95 state->domain = talloc(state, struct wbsrv_domain);
96 if (state->domain == NULL) goto failed;
98 state->domain->info = talloc_reference(state->domain, dom_info);
99 if (state->domain->info == NULL) goto failed;
101 state->domain->schannel_creds = cli_credentials_init(state->domain);
102 if (state->domain->schannel_creds == NULL) goto failed;
104 cli_credentials_set_conf(state->domain->schannel_creds);
106 cli_credentials_set_machine_account(state->domain->
108 if (!NT_STATUS_IS_OK(state->ctx->status)) goto failed;
110 state->conn.in.dest_host = dom_info->dc_address;
111 state->conn.in.port = 0;
112 state->conn.in.called_name = dom_info->dc_name;
113 state->conn.in.service = "IPC$";
114 state->conn.in.service_type = "IPC";
115 state->conn.in.workgroup = dom_info->name;
116 state->conn.in.credentials = state->domain->schannel_creds;
118 state->conn.in.fallback_to_anonymous = True;
120 ctx = smb_composite_connect_send(&state->conn, state->domain,
122 if (ctx == NULL) goto failed;
124 ctx->async.fn = init_domain_recv_tree;
125 ctx->async.private_data = state;
133 static void init_domain_recv_tree(struct composite_context *ctx)
135 struct init_domain_state *state =
136 talloc_get_type(ctx->async.private_data,
137 struct init_domain_state);
138 state->ctx->status = smb_composite_connect_recv(ctx, state);
139 if (!composite_is_ok(state->ctx)) return;
141 if ((state->domain->schannel_creds != NULL) &&
142 (!cli_credentials_is_anonymous(state->domain->schannel_creds)) &&
143 ((lp_server_role() == ROLE_DOMAIN_MEMBER) &&
144 (dom_sid_equal(state->domain->info->sid,
145 state->service->primary_sid)))) {
146 ctx = wb_get_schannel_creds_send(state,
147 state->domain->schannel_creds,
148 state->conn.out.tree,
149 state->ctx->event_ctx);
150 composite_continue(state->ctx, ctx,
151 init_domain_recv_netlogoncreds, state);
155 ctx = wb_connect_lsa_send(state, state->conn.out.tree, NULL);
156 composite_continue(state->ctx, ctx, init_domain_recv_lsa, state);
159 static void init_domain_recv_netlogoncreds(struct composite_context *ctx)
161 struct init_domain_state *state =
162 talloc_get_type(ctx->async.private_data,
163 struct init_domain_state);
164 struct dcerpc_pipe *auth2_pipe;
165 struct smbcli_tree *tree = NULL;
168 wb_get_schannel_creds_recv(ctx, state, &auth2_pipe);
169 if (!composite_is_ok(state->ctx)) return;
171 if (!lp_winbind_sealed_pipes()) {
172 state->domain->netlogon_pipe = talloc_reference(state->domain,
174 ctx = wb_connect_lsa_send(state, state->conn.out.tree, NULL);
175 composite_continue(state->ctx, ctx, init_domain_recv_lsa,
180 state->domain->netlogon_pipe =
181 dcerpc_pipe_init(state->domain, state->ctx->event_ctx);
182 if (composite_nomem(state->domain->netlogon_pipe, state->ctx)) return;
184 tree = dcerpc_smb_tree(auth2_pipe->conn);
186 composite_error(state->ctx, NT_STATUS_INTERNAL_ERROR);
190 ctx = dcerpc_pipe_open_smb_send(state->domain->netlogon_pipe->conn,
192 composite_continue(state->ctx, ctx, init_domain_recv_netlogonpipe,
196 static void init_domain_recv_netlogonpipe(struct composite_context *ctx)
198 struct init_domain_state *state =
199 talloc_get_type(ctx->async.private_data,
200 struct init_domain_state);
202 state->ctx->status = dcerpc_pipe_open_smb_recv(ctx);
203 if (!composite_is_ok(state->ctx)) return;
205 state->domain->netlogon_pipe->conn->flags |=
206 (DCERPC_SIGN | DCERPC_SEAL);
207 ctx = dcerpc_bind_auth_send(state, state->domain->netlogon_pipe,
208 &dcerpc_table_netlogon,
209 state->domain->schannel_creds,
210 DCERPC_AUTH_TYPE_SCHANNEL,
212 composite_continue(state->ctx, ctx, init_domain_recv_schannel, state);
215 static void init_domain_recv_schannel(struct composite_context *ctx)
217 struct init_domain_state *state =
218 talloc_get_type(ctx->async.private_data,
219 struct init_domain_state);
221 state->ctx->status = dcerpc_bind_auth_recv(ctx);
222 if (!composite_is_ok(state->ctx)) return;
224 ctx = wb_connect_lsa_send(state, state->conn.out.tree,
225 state->domain->schannel_creds);
226 composite_continue(state->ctx, ctx, init_domain_recv_lsa, state);
229 static void init_domain_recv_lsa(struct composite_context *ctx)
231 struct init_domain_state *state =
232 talloc_get_type(ctx->async.private_data,
233 struct init_domain_state);
235 struct rpc_request *req;
237 state->ctx->status = wb_connect_lsa_recv(ctx, state->domain,
238 &state->domain->lsa_auth_type,
239 &state->domain->lsa_pipe,
240 &state->domain->lsa_policy);
241 if (!composite_is_ok(state->ctx)) return;
243 /* Give the tree to the pipes. */
244 talloc_unlink(state, state->conn.out.tree);
246 state->queryinfo.in.handle = state->domain->lsa_policy;
247 state->queryinfo.in.level = LSA_POLICY_INFO_ACCOUNT_DOMAIN;
249 req = dcerpc_lsa_QueryInfoPolicy_send(state->domain->lsa_pipe, state,
251 composite_continue_rpc(state->ctx, req,
252 init_domain_recv_queryinfo, state);
255 static void init_domain_recv_queryinfo(struct rpc_request *req)
257 struct init_domain_state *state =
258 talloc_get_type(req->async.private, struct init_domain_state);
259 struct lsa_DomainInfo *dominfo;
260 struct composite_context *ctx;
261 const char *ldap_url;
263 state->ctx->status = dcerpc_ndr_request_recv(req);
264 if (!composite_is_ok(state->ctx)) return;
265 state->ctx->status = state->queryinfo.out.result;
266 if (!composite_is_ok(state->ctx)) return;
268 dominfo = &state->queryinfo.out.info->account_domain;
270 if (strcasecmp(state->domain->info->name, dominfo->name.string) != 0) {
271 DEBUG(2, ("Expected domain name %s, DC %s said %s\n",
272 state->domain->info->name,
273 dcerpc_server_name(state->domain->lsa_pipe),
274 dominfo->name.string));
275 composite_error(state->ctx, NT_STATUS_INVALID_DOMAIN_STATE);
279 if (!dom_sid_equal(state->domain->info->sid, dominfo->sid)) {
280 DEBUG(2, ("Expected domain sid %s, DC %s said %s\n",
281 dom_sid_string(state, state->domain->info->sid),
282 dcerpc_server_name(state->domain->lsa_pipe),
283 dom_sid_string(state, dominfo->sid)));
284 composite_error(state->ctx, NT_STATUS_INVALID_DOMAIN_STATE);
288 state->domain->ldap_conn =
289 ldap_new_connection(state->domain, state->ctx->event_ctx);
290 composite_nomem(state->domain->ldap_conn, state->ctx);
292 ldap_url = talloc_asprintf(state, "ldap://%s/",
293 state->domain->info->dc_address);
294 composite_nomem(ldap_url, state->ctx);
296 ctx = ldap_connect_send(state->domain->ldap_conn, ldap_url);
297 composite_continue(state->ctx, ctx, init_domain_recv_ldapconn, state);
300 static void init_domain_recv_ldapconn(struct composite_context *ctx)
302 struct init_domain_state *state =
303 talloc_get_type(ctx->async.private_data,
304 struct init_domain_state);
306 state->ctx->status = ldap_connect_recv(ctx);
307 if (NT_STATUS_IS_OK(state->ctx->status)) {
308 state->domain->ldap_conn->host =
309 talloc_strdup(state->domain->ldap_conn,
310 state->domain->info->dc_name);
312 ldap_bind_sasl(state->domain->ldap_conn,
313 state->domain->schannel_creds);
314 DEBUG(0, ("ldap_bind returned %s\n",
315 nt_errstr(state->ctx->status)));
318 state->domain->samr_pipe =
319 dcerpc_pipe_init(state->domain, state->ctx->event_ctx);
320 if (composite_nomem(state->domain->samr_pipe, state->ctx)) return;
322 ctx = wb_connect_sam_send(state, state->conn.out.tree,
323 state->domain->lsa_auth_type,
324 state->domain->schannel_creds,
325 state->domain->info->sid);
326 composite_continue(state->ctx, ctx, init_domain_recv_samr, state);
329 static void init_domain_recv_samr(struct composite_context *ctx)
331 struct init_domain_state *state =
332 talloc_get_type(ctx->async.private_data,
333 struct init_domain_state);
335 state->ctx->status = wb_connect_sam_recv(
336 ctx, state->domain, &state->domain->samr_pipe,
337 &state->domain->samr_handle, &state->domain->domain_handle);
338 if (!composite_is_ok(state->ctx)) return;
340 composite_done(state->ctx);
343 NTSTATUS wb_init_domain_recv(struct composite_context *c,
345 struct wbsrv_domain **result)
347 NTSTATUS status = composite_wait(c);
348 if (NT_STATUS_IS_OK(status)) {
349 struct init_domain_state *state =
350 talloc_get_type(c->private_data,
351 struct init_domain_state);
352 *result = talloc_steal(mem_ctx, state->domain);
358 NTSTATUS wb_init_domain(TALLOC_CTX *mem_ctx, struct wbsrv_service *service,
359 struct wb_dom_info *dom_info,
360 struct wbsrv_domain **result)
362 struct composite_context *c =
363 wb_init_domain_send(mem_ctx, service, dom_info);
364 return wb_init_domain_recv(c, mem_ctx, result);