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"
34 #include "libcli/auth/credentials.h"
35 #include "libcli/security/security.h"
37 #include "libcli/ldap/ldap_client.h"
39 #include "auth/credentials/credentials.h"
42 * Initialize a domain:
44 * - With schannel credentials, try to open the SMB connection with the
45 * machine creds. This works against W2k3SP1 with an NTLMSSP session
46 * setup. Fall back to anonymous.
48 * - If we have schannel creds, do the auth2 and open the schannel'ed netlogon
51 * - Open LSA. If we have machine creds, try to open with ntlmssp. Fall back
52 * to schannel and then to anon bind.
54 * - With queryinfopolicy, verify that we're talking to the right domain
56 * A bit complex, but with all the combinations I think it's the best we can
57 * get. NT4, W2k3 and W2k all have different combinations, but in the end we
58 * have a signed&sealed lsa connection on all of them.
60 * Not sure if it is overkill, but it seems to work.
63 struct init_domain_state {
64 struct composite_context *ctx;
65 struct wbsrv_domain *domain;
66 struct wbsrv_service *service;
68 struct smb_composite_connect conn;
70 struct lsa_QueryInfoPolicy queryinfo;
73 static void init_domain_recv_tree(struct composite_context *ctx);
74 static void init_domain_recv_netlogoncreds(struct composite_context *ctx);
75 static void init_domain_recv_netlogonpipe(struct composite_context *ctx);
76 static void init_domain_recv_schannel(struct composite_context *ctx);
77 static void init_domain_recv_lsa(struct composite_context *ctx);
78 static void init_domain_recv_queryinfo(struct rpc_request *req);
79 static void init_domain_recv_ldapconn(struct composite_context *ctx);
80 static void init_domain_recv_samr(struct composite_context *ctx);
82 struct composite_context *wb_init_domain_send(TALLOC_CTX *mem_ctx,
83 struct wbsrv_service *service,
84 struct wb_dom_info *dom_info)
86 struct composite_context *result, *ctx;
87 struct init_domain_state *state;
89 result = talloc(mem_ctx, struct composite_context);
90 if (result == NULL) goto failed;
91 result->state = COMPOSITE_STATE_IN_PROGRESS;
92 result->async.fn = NULL;
93 result->event_ctx = service->task->event_ctx;
95 state = talloc_zero(result, struct init_domain_state);
96 if (state == NULL) goto failed;
98 result->private_data = state;
100 state->service = service;
102 state->domain = talloc(state, struct wbsrv_domain);
103 if (state->domain == NULL) goto failed;
105 state->domain->info = talloc_reference(state->domain, dom_info);
106 if (state->domain->info == NULL) goto failed;
108 state->domain->schannel_creds = cli_credentials_init(state->domain);
109 if (state->domain->schannel_creds == NULL) goto failed;
111 cli_credentials_set_conf(state->domain->schannel_creds);
113 cli_credentials_set_machine_account(state->domain->
115 if (!NT_STATUS_IS_OK(state->ctx->status)) goto failed;
117 state->conn.in.dest_host = dom_info->dc_address;
118 state->conn.in.port = 0;
119 state->conn.in.called_name = dom_info->dc_name;
120 state->conn.in.service = "IPC$";
121 state->conn.in.service_type = "IPC";
122 state->conn.in.workgroup = dom_info->name;
123 state->conn.in.credentials = state->domain->schannel_creds;
125 state->conn.in.fallback_to_anonymous = True;
127 ctx = smb_composite_connect_send(&state->conn, state->domain,
129 if (ctx == NULL) goto failed;
131 ctx->async.fn = init_domain_recv_tree;
132 ctx->async.private_data = state;
140 static void init_domain_recv_tree(struct composite_context *ctx)
142 struct init_domain_state *state =
143 talloc_get_type(ctx->async.private_data,
144 struct init_domain_state);
145 state->ctx->status = smb_composite_connect_recv(ctx, state);
146 if (!composite_is_ok(state->ctx)) return;
148 if ((state->domain->schannel_creds != NULL) &&
149 (!cli_credentials_is_anonymous(state->domain->schannel_creds)) &&
150 ((lp_server_role() == ROLE_DOMAIN_MEMBER) &&
151 (dom_sid_equal(state->domain->info->sid,
152 state->service->primary_sid)))) {
153 ctx = wb_get_schannel_creds_send(state,
154 state->domain->schannel_creds,
155 state->conn.out.tree,
156 state->ctx->event_ctx);
157 composite_continue(state->ctx, ctx,
158 init_domain_recv_netlogoncreds, state);
162 ctx = wb_connect_lsa_send(state, state->conn.out.tree, NULL);
163 composite_continue(state->ctx, ctx, init_domain_recv_lsa, state);
166 static void init_domain_recv_netlogoncreds(struct composite_context *ctx)
168 struct init_domain_state *state =
169 talloc_get_type(ctx->async.private_data,
170 struct init_domain_state);
171 struct dcerpc_pipe *auth2_pipe;
172 struct smbcli_tree *tree = NULL;
175 wb_get_schannel_creds_recv(ctx, state, &auth2_pipe);
176 if (!composite_is_ok(state->ctx)) return;
178 if (!lp_winbind_sealed_pipes()) {
179 state->domain->netlogon_pipe = talloc_reference(state->domain,
181 ctx = wb_connect_lsa_send(state, state->conn.out.tree, NULL);
182 composite_continue(state->ctx, ctx, init_domain_recv_lsa,
187 state->domain->netlogon_pipe =
188 dcerpc_pipe_init(state->domain, state->ctx->event_ctx);
189 if (composite_nomem(state->domain->netlogon_pipe, state->ctx)) return;
191 tree = dcerpc_smb_tree(auth2_pipe->conn);
193 composite_error(state->ctx, NT_STATUS_INTERNAL_ERROR);
197 ctx = dcerpc_pipe_open_smb_send(state->domain->netlogon_pipe->conn,
199 composite_continue(state->ctx, ctx, init_domain_recv_netlogonpipe,
203 static void init_domain_recv_netlogonpipe(struct composite_context *ctx)
205 struct init_domain_state *state =
206 talloc_get_type(ctx->async.private_data,
207 struct init_domain_state);
209 state->ctx->status = dcerpc_pipe_open_smb_recv(ctx);
210 if (!composite_is_ok(state->ctx)) return;
212 state->domain->netlogon_pipe->conn->flags |=
213 (DCERPC_SIGN | DCERPC_SEAL);
214 ctx = dcerpc_bind_auth_send(state, state->domain->netlogon_pipe,
215 &dcerpc_table_netlogon,
216 state->domain->schannel_creds,
217 DCERPC_AUTH_TYPE_SCHANNEL,
218 DCERPC_AUTH_LEVEL_PRIVACY,
220 composite_continue(state->ctx, ctx, init_domain_recv_schannel, state);
223 static void init_domain_recv_schannel(struct composite_context *ctx)
225 struct init_domain_state *state =
226 talloc_get_type(ctx->async.private_data,
227 struct init_domain_state);
229 state->ctx->status = dcerpc_bind_auth_recv(ctx);
230 if (!composite_is_ok(state->ctx)) return;
232 ctx = wb_connect_lsa_send(state, state->conn.out.tree,
233 state->domain->schannel_creds);
234 composite_continue(state->ctx, ctx, init_domain_recv_lsa, state);
237 static void init_domain_recv_lsa(struct composite_context *ctx)
239 struct init_domain_state *state =
240 talloc_get_type(ctx->async.private_data,
241 struct init_domain_state);
243 struct rpc_request *req;
245 state->ctx->status = wb_connect_lsa_recv(ctx, state->domain,
246 &state->domain->lsa_auth_type,
247 &state->domain->lsa_pipe,
248 &state->domain->lsa_policy);
249 if (!composite_is_ok(state->ctx)) return;
251 /* Give the tree to the pipes. */
252 talloc_unlink(state, state->conn.out.tree);
254 state->queryinfo.in.handle = state->domain->lsa_policy;
255 state->queryinfo.in.level = LSA_POLICY_INFO_ACCOUNT_DOMAIN;
257 req = dcerpc_lsa_QueryInfoPolicy_send(state->domain->lsa_pipe, state,
259 composite_continue_rpc(state->ctx, req,
260 init_domain_recv_queryinfo, state);
263 static void init_domain_recv_queryinfo(struct rpc_request *req)
265 struct init_domain_state *state =
266 talloc_get_type(req->async.private, struct init_domain_state);
267 struct lsa_DomainInfo *dominfo;
268 struct composite_context *ctx;
269 const char *ldap_url;
271 state->ctx->status = dcerpc_ndr_request_recv(req);
272 if (!composite_is_ok(state->ctx)) return;
273 state->ctx->status = state->queryinfo.out.result;
274 if (!composite_is_ok(state->ctx)) return;
276 dominfo = &state->queryinfo.out.info->account_domain;
278 if (strcasecmp(state->domain->info->name, dominfo->name.string) != 0) {
279 DEBUG(2, ("Expected domain name %s, DC %s said %s\n",
280 state->domain->info->name,
281 dcerpc_server_name(state->domain->lsa_pipe),
282 dominfo->name.string));
283 composite_error(state->ctx, NT_STATUS_INVALID_DOMAIN_STATE);
287 if (!dom_sid_equal(state->domain->info->sid, dominfo->sid)) {
288 DEBUG(2, ("Expected domain sid %s, DC %s said %s\n",
289 dom_sid_string(state, state->domain->info->sid),
290 dcerpc_server_name(state->domain->lsa_pipe),
291 dom_sid_string(state, dominfo->sid)));
292 composite_error(state->ctx, NT_STATUS_INVALID_DOMAIN_STATE);
296 state->domain->ldap_conn =
297 ldap_new_connection(state->domain, state->ctx->event_ctx);
298 composite_nomem(state->domain->ldap_conn, state->ctx);
300 ldap_url = talloc_asprintf(state, "ldap://%s/",
301 state->domain->info->dc_address);
302 composite_nomem(ldap_url, state->ctx);
304 ctx = ldap_connect_send(state->domain->ldap_conn, ldap_url);
305 composite_continue(state->ctx, ctx, init_domain_recv_ldapconn, state);
308 static void init_domain_recv_ldapconn(struct composite_context *ctx)
310 struct init_domain_state *state =
311 talloc_get_type(ctx->async.private_data,
312 struct init_domain_state);
314 state->ctx->status = ldap_connect_recv(ctx);
315 if (NT_STATUS_IS_OK(state->ctx->status)) {
316 state->domain->ldap_conn->host =
317 talloc_strdup(state->domain->ldap_conn,
318 state->domain->info->dc_name);
320 ldap_bind_sasl(state->domain->ldap_conn,
321 state->domain->schannel_creds);
322 DEBUG(0, ("ldap_bind returned %s\n",
323 nt_errstr(state->ctx->status)));
326 state->domain->samr_pipe =
327 dcerpc_pipe_init(state->domain, state->ctx->event_ctx);
328 if (composite_nomem(state->domain->samr_pipe, state->ctx)) return;
330 ctx = wb_connect_sam_send(state, state->conn.out.tree,
331 state->domain->lsa_auth_type,
332 state->domain->schannel_creds,
333 state->domain->info->sid);
334 composite_continue(state->ctx, ctx, init_domain_recv_samr, state);
337 static void init_domain_recv_samr(struct composite_context *ctx)
339 struct init_domain_state *state =
340 talloc_get_type(ctx->async.private_data,
341 struct init_domain_state);
343 state->ctx->status = wb_connect_sam_recv(
344 ctx, state->domain, &state->domain->samr_pipe,
345 &state->domain->samr_handle, &state->domain->domain_handle);
346 if (!composite_is_ok(state->ctx)) return;
348 composite_done(state->ctx);
351 NTSTATUS wb_init_domain_recv(struct composite_context *c,
353 struct wbsrv_domain **result)
355 NTSTATUS status = composite_wait(c);
356 if (NT_STATUS_IS_OK(status)) {
357 struct init_domain_state *state =
358 talloc_get_type(c->private_data,
359 struct init_domain_state);
360 *result = talloc_steal(mem_ctx, state->domain);
366 NTSTATUS wb_init_domain(TALLOC_CTX *mem_ctx, struct wbsrv_service *service,
367 struct wb_dom_info *dom_info,
368 struct wbsrv_domain **result)
370 struct composite_context *c =
371 wb_init_domain_send(mem_ctx, service, dom_info);
372 return wb_init_domain_recv(c, mem_ctx, result);