2 Unix SMB/CIFS implementation.
4 Connect to the LSA pipe, given an smbcli_tree and possibly some
5 credentials. Try ntlmssp, schannel and anon in that order.
7 Copyright (C) Volker Lendecke 2005
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "libcli/composite/composite.h"
27 #include "libcli/raw/libcliraw.h"
28 #include "librpc/gen_ndr/ndr_lsa_c.h"
30 /* Helper to initialize LSA with a specific auth methods. Verify by opening
33 struct init_lsa_state {
34 struct composite_context *ctx;
35 struct dcerpc_pipe *lsa_pipe;
38 struct cli_credentials *creds;
40 struct lsa_ObjectAttribute objectattr;
41 struct lsa_OpenPolicy2 openpolicy;
42 struct policy_handle *handle;
45 static void init_lsa_recv_pipe(struct composite_context *ctx);
46 static void init_lsa_recv_anon_bind(struct composite_context *ctx);
47 static void init_lsa_recv_auth_bind(struct composite_context *ctx);
48 static void init_lsa_recv_openpol(struct rpc_request *req);
50 struct composite_context *wb_init_lsa_send(TALLOC_CTX *mem_ctx,
51 struct smbcli_tree *tree,
53 struct cli_credentials *creds)
55 struct composite_context *result, *ctx;
56 struct init_lsa_state *state;
58 result = composite_create(mem_ctx, tree->session->transport->socket->event.ctx);
59 if (result == NULL) goto failed;
61 state = talloc(result, struct init_lsa_state);
62 if (state == NULL) goto failed;
64 result->private_data = state;
66 state->auth_type = auth_type;
69 state->lsa_pipe = dcerpc_pipe_init(state, result->event_ctx);
70 if (state->lsa_pipe == NULL) goto failed;
72 ctx = dcerpc_pipe_open_smb_send(state->lsa_pipe, tree,
74 ctx->async.fn = init_lsa_recv_pipe;
75 ctx->async.private_data = state;
83 static void init_lsa_recv_pipe(struct composite_context *ctx)
85 struct init_lsa_state *state =
86 talloc_get_type(ctx->async.private_data,
87 struct init_lsa_state);
89 state->ctx->status = dcerpc_pipe_open_smb_recv(ctx);
90 if (!composite_is_ok(state->ctx)) return;
92 switch (state->auth_type) {
93 case DCERPC_AUTH_TYPE_NONE:
94 ctx = dcerpc_bind_auth_none_send(state, state->lsa_pipe,
95 &dcerpc_table_lsarpc);
96 composite_continue(state->ctx, ctx, init_lsa_recv_anon_bind,
99 case DCERPC_AUTH_TYPE_NTLMSSP:
100 case DCERPC_AUTH_TYPE_SCHANNEL:
103 if (lp_winbind_sealed_pipes()) {
104 auth_type = DCERPC_AUTH_LEVEL_PRIVACY;
106 auth_type = DCERPC_AUTH_LEVEL_INTEGRITY;
108 if (state->creds == NULL) {
109 composite_error(state->ctx, NT_STATUS_INTERNAL_ERROR);
112 ctx = dcerpc_bind_auth_send(state, state->lsa_pipe,
113 &dcerpc_table_lsarpc,
114 state->creds, state->auth_type,
117 composite_continue(state->ctx, ctx, init_lsa_recv_auth_bind,
122 composite_error(state->ctx, NT_STATUS_INTERNAL_ERROR);
126 static void init_lsa_recv_anon_bind(struct composite_context *ctx)
128 struct init_lsa_state *state =
129 talloc_get_type(ctx->async.private_data,
130 struct init_lsa_state);
131 struct rpc_request *req;
133 state->ctx->status = dcerpc_bind_auth_none_recv(ctx);
134 if (!composite_is_ok(state->ctx)) return;
136 state->handle = talloc(state, struct policy_handle);
137 if (composite_nomem(state->handle, state->ctx)) return;
139 state->openpolicy.in.system_name =
140 talloc_asprintf(state, "\\\\%s",
141 dcerpc_server_name(state->lsa_pipe));
142 ZERO_STRUCT(state->objectattr);
143 state->openpolicy.in.attr = &state->objectattr;
144 state->openpolicy.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
145 state->openpolicy.out.handle = state->handle;
147 req = dcerpc_lsa_OpenPolicy2_send(state->lsa_pipe, state,
149 composite_continue_rpc(state->ctx, req, init_lsa_recv_openpol, state);
152 static void init_lsa_recv_auth_bind(struct composite_context *ctx)
154 struct init_lsa_state *state =
155 talloc_get_type(ctx->async.private_data,
156 struct init_lsa_state);
157 struct rpc_request *req;
159 state->ctx->status = dcerpc_bind_auth_recv(ctx);
160 if (!composite_is_ok(state->ctx)) return;
162 state->handle = talloc(state, struct policy_handle);
163 if (composite_nomem(state->handle, state->ctx)) return;
165 state->openpolicy.in.system_name =
166 talloc_asprintf(state, "\\\\%s",
167 dcerpc_server_name(state->lsa_pipe));
168 ZERO_STRUCT(state->objectattr);
169 state->openpolicy.in.attr = &state->objectattr;
170 state->openpolicy.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
171 state->openpolicy.out.handle = state->handle;
173 req = dcerpc_lsa_OpenPolicy2_send(state->lsa_pipe, state,
175 composite_continue_rpc(state->ctx, req, init_lsa_recv_openpol, state);
178 static void init_lsa_recv_openpol(struct rpc_request *req)
180 struct init_lsa_state *state =
181 talloc_get_type(req->async.private,
182 struct init_lsa_state);
184 state->ctx->status = dcerpc_ndr_request_recv(req);
185 if (!composite_is_ok(state->ctx)) return;
186 state->ctx->status = state->openpolicy.out.result;
187 if (!composite_is_ok(state->ctx)) return;
189 composite_done(state->ctx);
192 NTSTATUS wb_init_lsa_recv(struct composite_context *c,
194 struct dcerpc_pipe **lsa_pipe,
195 struct policy_handle **lsa_policy)
197 NTSTATUS status = composite_wait(c);
198 if (NT_STATUS_IS_OK(status)) {
199 struct init_lsa_state *state =
200 talloc_get_type(c->private_data,
201 struct init_lsa_state);
202 *lsa_pipe = talloc_steal(mem_ctx, state->lsa_pipe);
203 *lsa_policy = talloc_steal(mem_ctx, state->handle);
211 * Connect to LSA using the credentials, try NTLMSSP and SCHANNEL using the
212 * given credentials. If both fail or no credentials are available, fall back
213 * to an anonymous bind.
216 struct connect_lsa_state {
217 struct composite_context *ctx;
218 struct smbcli_tree *tree;
219 struct cli_credentials *credentials;
222 struct dcerpc_pipe *lsa_pipe;
223 struct policy_handle *lsa_policy;
226 static void connect_lsa_recv_ntlmssp(struct composite_context *ctx);
227 static void connect_lsa_recv_schannel(struct composite_context *ctx);
228 static void connect_lsa_recv_anon(struct composite_context *ctx);
230 struct composite_context *wb_connect_lsa_send(TALLOC_CTX *mem_ctx,
231 struct smbcli_tree *tree,
232 struct cli_credentials *credentials)
234 struct composite_context *result, *ctx;
235 struct connect_lsa_state *state;
237 result = composite_create(mem_ctx, tree->session->transport->socket->event.ctx);
238 if (result == NULL) goto failed;
240 state = talloc(result, struct connect_lsa_state);
241 if (state == NULL) goto failed;
243 result->private_data = state;
246 state->credentials = credentials;
248 if (credentials == NULL) {
249 ctx = wb_init_lsa_send(state, tree, DCERPC_AUTH_TYPE_NONE,
251 if (ctx == NULL) goto failed;
252 ctx->async.fn = connect_lsa_recv_anon;
253 ctx->async.private_data = state;
257 ctx = wb_init_lsa_send(state, tree, DCERPC_AUTH_TYPE_NTLMSSP,
259 if (ctx == NULL) goto failed;
260 ctx->async.fn = connect_lsa_recv_ntlmssp;
261 ctx->async.private_data = state;
269 static void connect_lsa_recv_ntlmssp(struct composite_context *ctx)
271 struct connect_lsa_state *state =
272 talloc_get_type(ctx->async.private_data,
273 struct connect_lsa_state);
275 state->ctx->status = wb_init_lsa_recv(ctx, state, &state->lsa_pipe,
278 if (NT_STATUS_IS_OK(state->ctx->status)) {
279 state->auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
280 composite_done(state->ctx);
284 ctx = wb_init_lsa_send(state, state->tree, DCERPC_AUTH_TYPE_SCHANNEL,
286 composite_continue(state->ctx, ctx,
287 connect_lsa_recv_schannel, state);
290 static void connect_lsa_recv_schannel(struct composite_context *ctx)
292 struct connect_lsa_state *state =
293 talloc_get_type(ctx->async.private_data,
294 struct connect_lsa_state);
296 state->ctx->status = wb_init_lsa_recv(ctx, state, &state->lsa_pipe,
299 if (NT_STATUS_IS_OK(state->ctx->status)) {
300 state->auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
301 composite_done(state->ctx);
305 ctx = wb_init_lsa_send(state, state->tree, DCERPC_AUTH_TYPE_NONE,
307 composite_continue(state->ctx, ctx,
308 connect_lsa_recv_anon, state);
311 static void connect_lsa_recv_anon(struct composite_context *ctx)
313 struct connect_lsa_state *state =
314 talloc_get_type(ctx->async.private_data,
315 struct connect_lsa_state);
317 state->ctx->status = wb_init_lsa_recv(ctx, state, &state->lsa_pipe,
319 if (!composite_is_ok(state->ctx)) return;
321 state->auth_type = DCERPC_AUTH_TYPE_NONE;
322 composite_done(state->ctx);
325 NTSTATUS wb_connect_lsa_recv(struct composite_context *c,
328 struct dcerpc_pipe **lsa_pipe,
329 struct policy_handle **lsa_policy)
331 NTSTATUS status = composite_wait(c);
332 if (NT_STATUS_IS_OK(status)) {
333 struct connect_lsa_state *state =
334 talloc_get_type(c->private_data,
335 struct connect_lsa_state);
336 *auth_type = state->auth_type;
337 *lsa_pipe = talloc_steal(mem_ctx, state->lsa_pipe);
338 *lsa_policy = talloc_steal(mem_ctx, state->lsa_policy);
344 NTSTATUS wb_connect_lsa(TALLOC_CTX *mem_ctx,
345 struct smbcli_tree *tree,
346 struct cli_credentials *credentials,
348 struct dcerpc_pipe **lsa_pipe,
349 struct policy_handle **lsa_policy)
351 struct composite_context *c;
352 c = wb_connect_lsa_send(mem_ctx, tree, credentials);
353 return wb_connect_lsa_recv(c, mem_ctx, auth_type, lsa_pipe,