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"
42 #include "libcli/ldap/ldap_client.h"
46 * Initialize a domain:
48 * - With schannel credentials, try to open the SMB connection with the
49 * machine creds. This works against W2k3SP1 with an NTLMSSP session
50 * setup. Fall back to anonymous.
52 * - If we have schannel creds, do the auth2 and open the schannel'ed netlogon
55 * - Open LSA. If we have machine creds, try to open with ntlmssp. Fall back
56 * to schannel and then to anon bind.
58 * - With queryinfopolicy, verify that we're talking to the right domain
60 * A bit complex, but with all the combinations I think it's the best we can
61 * get. NT4, W2k3 and W2k all have different combinations, but in the end we
62 * have a signed&sealed lsa connection on all of them.
64 * Not sure if it is overkill, but it seems to work.
67 struct init_domain_state {
68 struct composite_context *ctx;
69 struct wbsrv_domain *domain;
70 struct wbsrv_service *service;
72 struct smb_composite_connect conn;
74 struct lsa_QueryInfoPolicy queryinfo;
77 static void init_domain_recv_tree(struct composite_context *ctx);
78 static void init_domain_recv_netlogoncreds(struct composite_context *ctx);
79 static void init_domain_recv_netlogonpipe(struct composite_context *ctx);
80 static void init_domain_recv_lsa(struct composite_context *ctx);
81 static void init_domain_recv_queryinfo(struct rpc_request *req);
82 static void init_domain_recv_ldapconn(struct composite_context *ctx);
83 static void init_domain_recv_samr(struct composite_context *ctx);
85 struct composite_context *wb_init_domain_send(TALLOC_CTX *mem_ctx,
86 struct wbsrv_service *service,
87 struct wb_dom_info *dom_info)
89 struct composite_context *result, *ctx;
90 struct init_domain_state *state;
92 result = talloc(mem_ctx, struct composite_context);
93 if (result == NULL) goto failed;
94 result->state = COMPOSITE_STATE_IN_PROGRESS;
95 result->async.fn = NULL;
96 result->event_ctx = service->task->event_ctx;
98 state = talloc_zero(result, struct init_domain_state);
99 if (state == NULL) goto failed;
101 result->private_data = state;
103 state->service = service;
105 state->domain = talloc(state, struct wbsrv_domain);
106 if (state->domain == NULL) goto failed;
108 state->domain->info = talloc_reference(state->domain, dom_info);
109 if (state->domain->info == NULL) goto failed;
111 state->domain->schannel_creds = cli_credentials_init(state->domain);
112 if (state->domain->schannel_creds == NULL) goto failed;
114 cli_credentials_set_conf(state->domain->schannel_creds);
116 cli_credentials_set_machine_account(state->domain->
118 if (!NT_STATUS_IS_OK(state->ctx->status)) goto failed;
120 state->conn.in.dest_host = dom_info->dc_address;
121 state->conn.in.port = 0;
122 state->conn.in.called_name = dom_info->dc_name;
123 state->conn.in.service = "IPC$";
124 state->conn.in.service_type = "IPC";
125 state->conn.in.workgroup = dom_info->name;
126 state->conn.in.credentials = state->domain->schannel_creds;
128 state->conn.in.fallback_to_anonymous = True;
130 ctx = smb_composite_connect_send(&state->conn, state->domain,
132 if (ctx == NULL) goto failed;
134 ctx->async.fn = init_domain_recv_tree;
135 ctx->async.private_data = state;
143 static void init_domain_recv_tree(struct composite_context *ctx)
145 struct init_domain_state *state =
146 talloc_get_type(ctx->async.private_data,
147 struct init_domain_state);
148 state->ctx->status = smb_composite_connect_recv(ctx, state);
149 if (!composite_is_ok(state->ctx)) return;
151 if ((state->domain->schannel_creds != NULL) &&
152 (!cli_credentials_is_anonymous(state->domain->schannel_creds)) &&
153 ((lp_server_role() == ROLE_DOMAIN_MEMBER) &&
154 (dom_sid_equal(state->domain->info->sid,
155 state->service->primary_sid)))) {
156 ctx = wb_get_schannel_creds_send(state,
157 state->domain->schannel_creds,
158 state->conn.out.tree,
159 state->ctx->event_ctx);
160 composite_continue(state->ctx, ctx,
161 init_domain_recv_netlogoncreds, state);
165 ctx = wb_connect_lsa_send(state, state->conn.out.tree, NULL);
166 composite_continue(state->ctx, ctx, init_domain_recv_lsa, state);
169 static void init_domain_recv_netlogoncreds(struct composite_context *ctx)
171 struct init_domain_state *state =
172 talloc_get_type(ctx->async.private_data,
173 struct init_domain_state);
174 struct dcerpc_pipe *auth2_pipe;
175 struct smbcli_tree *tree = NULL;
178 wb_get_schannel_creds_recv(ctx, state, &auth2_pipe);
179 if (!composite_is_ok(state->ctx)) return;
181 if (!lp_winbind_sealed_pipes()) {
182 state->domain->netlogon_pipe = talloc_reference(state->domain,
184 ctx = wb_connect_lsa_send(state, state->conn.out.tree, NULL);
185 composite_continue(state->ctx, ctx, init_domain_recv_lsa,
190 state->domain->netlogon_pipe =
191 dcerpc_pipe_init(state->domain, state->ctx->event_ctx);
192 if (composite_nomem(state->domain->netlogon_pipe, state->ctx)) return;
194 tree = dcerpc_smb_tree(auth2_pipe->conn);
196 composite_error(state->ctx, NT_STATUS_INTERNAL_ERROR);
200 ctx = dcerpc_pipe_open_smb_send(state->domain->netlogon_pipe->conn,
202 composite_continue(state->ctx, ctx, init_domain_recv_netlogonpipe,
206 static void init_domain_recv_netlogonpipe(struct composite_context *ctx)
208 struct init_domain_state *state =
209 talloc_get_type(ctx->async.private_data,
210 struct init_domain_state);
212 state->ctx->status = dcerpc_pipe_open_smb_recv(ctx);
213 if (!composite_is_ok(state->ctx)) return;
215 state->domain->netlogon_pipe->conn->flags |=
216 (DCERPC_SIGN | DCERPC_SEAL);
218 dcerpc_bind_auth(state->domain->netlogon_pipe,
219 DCERPC_NETLOGON_UUID,
220 DCERPC_NETLOGON_VERSION,
221 state->domain->schannel_creds,
222 DCERPC_AUTH_TYPE_SCHANNEL,
224 if (!composite_is_ok(state->ctx)) return;
226 ctx = wb_connect_lsa_send(state, state->conn.out.tree,
227 state->domain->schannel_creds);
228 composite_continue(state->ctx, ctx, init_domain_recv_lsa, state);
231 static void init_domain_recv_lsa(struct composite_context *ctx)
233 struct init_domain_state *state =
234 talloc_get_type(ctx->async.private_data,
235 struct init_domain_state);
237 struct rpc_request *req;
239 state->ctx->status = wb_connect_lsa_recv(ctx, state->domain,
240 &state->domain->lsa_auth_type,
241 &state->domain->lsa_pipe,
242 &state->domain->lsa_policy);
243 if (!composite_is_ok(state->ctx)) return;
245 /* Give the tree to the pipes. */
246 talloc_unlink(state, state->conn.out.tree);
248 state->queryinfo.in.handle = state->domain->lsa_policy;
249 state->queryinfo.in.level = LSA_POLICY_INFO_ACCOUNT_DOMAIN;
251 req = dcerpc_lsa_QueryInfoPolicy_send(state->domain->lsa_pipe, state,
253 composite_continue_rpc(state->ctx, req,
254 init_domain_recv_queryinfo, state);
257 static void init_domain_recv_queryinfo(struct rpc_request *req)
259 struct init_domain_state *state =
260 talloc_get_type(req->async.private, struct init_domain_state);
261 struct lsa_DomainInfo *dominfo;
262 struct composite_context *ctx;
263 const char *ldap_url;
265 state->ctx->status = dcerpc_ndr_request_recv(req);
266 if (!composite_is_ok(state->ctx)) return;
267 state->ctx->status = state->queryinfo.out.result;
268 if (!composite_is_ok(state->ctx)) return;
270 dominfo = &state->queryinfo.out.info->account_domain;
272 if (strcasecmp(state->domain->info->name, dominfo->name.string) != 0) {
273 DEBUG(2, ("Expected domain name %s, DC %s said %s\n",
274 state->domain->info->name,
275 dcerpc_server_name(state->domain->lsa_pipe),
276 dominfo->name.string));
277 composite_error(state->ctx, NT_STATUS_INVALID_DOMAIN_STATE);
281 if (!dom_sid_equal(state->domain->info->sid, dominfo->sid)) {
282 DEBUG(2, ("Expected domain sid %s, DC %s said %s\n",
283 dom_sid_string(state, state->domain->info->sid),
284 dcerpc_server_name(state->domain->lsa_pipe),
285 dom_sid_string(state, dominfo->sid)));
286 composite_error(state->ctx, NT_STATUS_INVALID_DOMAIN_STATE);
290 state->domain->ldap_conn =
291 ldap_new_connection(state->domain, state->ctx->event_ctx);
292 composite_nomem(state->domain->ldap_conn, state->ctx);
294 ldap_url = talloc_asprintf(state, "ldap://%s/",
295 state->domain->info->dc_address);
296 composite_nomem(ldap_url, state->ctx);
298 ctx = ldap_connect_send(state->domain->ldap_conn, ldap_url);
299 composite_continue(state->ctx, ctx, init_domain_recv_ldapconn, state);
302 static void init_domain_recv_ldapconn(struct composite_context *ctx)
304 struct init_domain_state *state =
305 talloc_get_type(ctx->async.private_data,
306 struct init_domain_state);
308 state->ctx->status = ldap_connect_recv(ctx);
309 if (NT_STATUS_IS_OK(state->ctx->status)) {
310 state->domain->ldap_conn->host =
311 talloc_strdup(state->domain->ldap_conn,
312 state->domain->info->dc_name);
314 ldap_bind_sasl(state->domain->ldap_conn,
315 state->domain->schannel_creds);
316 DEBUG(0, ("ldap_bind returned %s\n",
317 nt_errstr(state->ctx->status)));
320 state->domain->samr_pipe =
321 dcerpc_pipe_init(state->domain, state->ctx->event_ctx);
322 if (composite_nomem(state->domain->samr_pipe, state->ctx)) return;
324 ctx = wb_connect_sam_send(state, state->conn.out.tree,
325 state->domain->lsa_auth_type,
326 state->domain->schannel_creds,
327 state->domain->info->sid);
328 composite_continue(state->ctx, ctx, init_domain_recv_samr, state);
331 static void init_domain_recv_samr(struct composite_context *ctx)
333 struct init_domain_state *state =
334 talloc_get_type(ctx->async.private_data,
335 struct init_domain_state);
337 state->ctx->status = wb_connect_sam_recv(
338 ctx, state->domain, &state->domain->samr_pipe,
339 &state->domain->samr_handle, &state->domain->domain_handle);
340 if (!composite_is_ok(state->ctx)) return;
342 composite_done(state->ctx);
345 NTSTATUS wb_init_domain_recv(struct composite_context *c,
347 struct wbsrv_domain **result)
349 NTSTATUS status = composite_wait(c);
350 if (NT_STATUS_IS_OK(status)) {
351 struct init_domain_state *state =
352 talloc_get_type(c->private_data,
353 struct init_domain_state);
354 *result = talloc_steal(mem_ctx, state->domain);
360 NTSTATUS wb_init_domain(TALLOC_CTX *mem_ctx, struct wbsrv_service *service,
361 struct wb_dom_info *dom_info,
362 struct wbsrv_domain **result)
364 struct composite_context *c =
365 wb_init_domain_send(mem_ctx, service, dom_info);
366 return wb_init_domain_recv(c, mem_ctx, result);