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.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_openpol(struct rpc_request *req);
48 struct composite_context *wb_init_lsa_send(TALLOC_CTX *mem_ctx,
49 struct smbcli_tree *tree,
51 struct cli_credentials *creds)
53 struct composite_context *result, *ctx;
54 struct init_lsa_state *state;
56 result = talloc(mem_ctx, struct composite_context);
57 if (result == NULL) goto failed;
58 result->state = COMPOSITE_STATE_IN_PROGRESS;
59 result->async.fn = NULL;
60 result->event_ctx = tree->session->transport->socket->event.ctx;
62 state = talloc(result, struct init_lsa_state);
63 if (state == NULL) goto failed;
65 result->private_data = state;
67 state->auth_type = auth_type;
70 state->lsa_pipe = dcerpc_pipe_init(state, result->event_ctx);
71 if (state->lsa_pipe == NULL) goto failed;
73 ctx = dcerpc_pipe_open_smb_send(state->lsa_pipe->conn, tree,
75 ctx->async.fn = init_lsa_recv_pipe;
76 ctx->async.private_data = state;
84 static void init_lsa_recv_pipe(struct composite_context *ctx)
86 struct init_lsa_state *state =
87 talloc_get_type(ctx->async.private_data,
88 struct init_lsa_state);
89 struct rpc_request *req;
91 state->ctx->status = dcerpc_pipe_open_smb_recv(ctx);
92 if (!composite_is_ok(state->ctx)) return;
94 switch (state->auth_type) {
95 case DCERPC_AUTH_TYPE_NONE:
97 dcerpc_bind_auth_none(state->lsa_pipe,
99 DCERPC_LSARPC_VERSION);
101 case DCERPC_AUTH_TYPE_NTLMSSP:
102 case DCERPC_AUTH_TYPE_SCHANNEL:
103 if (state->creds == NULL) {
104 composite_error(state->ctx, NT_STATUS_INTERNAL_ERROR);
107 state->lsa_pipe->conn->flags |= (DCERPC_SIGN | DCERPC_SEAL);
109 dcerpc_bind_auth_password(state->lsa_pipe,
111 DCERPC_LSARPC_VERSION,
117 state->ctx->status = NT_STATUS_INTERNAL_ERROR;
121 state->handle = talloc(state, struct policy_handle);
122 if (composite_nomem(state->handle, state->ctx)) return;
124 state->openpolicy.in.system_name =
125 talloc_asprintf(state, "\\\\%s",
126 dcerpc_server_name(state->lsa_pipe));
127 ZERO_STRUCT(state->objectattr);
128 state->openpolicy.in.attr = &state->objectattr;
129 state->openpolicy.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
130 state->openpolicy.out.handle = state->handle;
132 req = dcerpc_lsa_OpenPolicy2_send(state->lsa_pipe, state,
134 composite_continue_rpc(state->ctx, req, init_lsa_recv_openpol, state);
137 static void init_lsa_recv_openpol(struct rpc_request *req)
139 struct init_lsa_state *state =
140 talloc_get_type(req->async.private,
141 struct init_lsa_state);
143 state->ctx->status = dcerpc_ndr_request_recv(req);
144 if (!composite_is_ok(state->ctx)) return;
145 state->ctx->status = state->openpolicy.out.result;
146 if (!composite_is_ok(state->ctx)) return;
148 composite_done(state->ctx);
151 NTSTATUS wb_init_lsa_recv(struct composite_context *c,
153 struct dcerpc_pipe **lsa_pipe,
154 struct policy_handle **lsa_policy)
156 NTSTATUS status = composite_wait(c);
157 if (NT_STATUS_IS_OK(status)) {
158 struct init_lsa_state *state =
159 talloc_get_type(c->private_data,
160 struct init_lsa_state);
161 *lsa_pipe = talloc_steal(mem_ctx, state->lsa_pipe);
162 *lsa_policy = talloc_steal(mem_ctx, state->handle);
170 * Connect to LSA using the credentials, try NTLMSSP and SCHANNEL using the
171 * given credentials. If both fail or no credentials are available, fall back
172 * to an anonymous bind.
175 struct connect_lsa_state {
176 struct composite_context *ctx;
177 struct smbcli_tree *tree;
178 struct cli_credentials *credentials;
181 struct dcerpc_pipe *lsa_pipe;
182 struct policy_handle *lsa_policy;
185 static void connect_lsa_recv_ntlmssp(struct composite_context *ctx);
186 static void connect_lsa_recv_schannel(struct composite_context *ctx);
187 static void connect_lsa_recv_anon(struct composite_context *ctx);
189 struct composite_context *wb_connect_lsa_send(TALLOC_CTX *mem_ctx,
190 struct smbcli_tree *tree,
191 struct cli_credentials *credentials)
193 struct composite_context *result, *ctx;
194 struct connect_lsa_state *state;
196 result = talloc(mem_ctx, struct composite_context);
197 if (result == NULL) goto failed;
198 result->state = COMPOSITE_STATE_IN_PROGRESS;
199 result->async.fn = NULL;
200 result->event_ctx = tree->session->transport->socket->event.ctx;
202 state = talloc(result, struct connect_lsa_state);
203 if (state == NULL) goto failed;
205 result->private_data = state;
208 state->credentials = credentials;
210 if (credentials == NULL) {
211 ctx = wb_init_lsa_send(state, tree, DCERPC_AUTH_TYPE_NONE,
213 if (ctx == NULL) goto failed;
214 ctx->async.fn = connect_lsa_recv_anon;
215 ctx->async.private_data = state;
219 ctx = wb_init_lsa_send(state, tree, DCERPC_AUTH_TYPE_NTLMSSP,
221 if (ctx == NULL) goto failed;
222 ctx->async.fn = connect_lsa_recv_ntlmssp;
223 ctx->async.private_data = state;
231 static void connect_lsa_recv_ntlmssp(struct composite_context *ctx)
233 struct connect_lsa_state *state =
234 talloc_get_type(ctx->async.private_data,
235 struct connect_lsa_state);
237 state->ctx->status = wb_init_lsa_recv(ctx, state, &state->lsa_pipe,
240 if (NT_STATUS_IS_OK(state->ctx->status)) {
241 state->auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
242 composite_done(state->ctx);
246 ctx = wb_init_lsa_send(state, state->tree, DCERPC_AUTH_TYPE_SCHANNEL,
248 composite_continue(state->ctx, ctx,
249 connect_lsa_recv_schannel, state);
252 static void connect_lsa_recv_schannel(struct composite_context *ctx)
254 struct connect_lsa_state *state =
255 talloc_get_type(ctx->async.private_data,
256 struct connect_lsa_state);
258 state->ctx->status = wb_init_lsa_recv(ctx, state, &state->lsa_pipe,
261 if (NT_STATUS_IS_OK(state->ctx->status)) {
262 state->auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
263 composite_done(state->ctx);
267 ctx = wb_init_lsa_send(state, state->tree, DCERPC_AUTH_TYPE_NONE,
269 composite_continue(state->ctx, ctx,
270 connect_lsa_recv_anon, state);
273 static void connect_lsa_recv_anon(struct composite_context *ctx)
275 struct connect_lsa_state *state =
276 talloc_get_type(ctx->async.private_data,
277 struct connect_lsa_state);
279 state->ctx->status = wb_init_lsa_recv(ctx, state, &state->lsa_pipe,
281 if (!composite_is_ok(state->ctx)) return;
283 state->auth_type = DCERPC_AUTH_TYPE_NONE;
284 composite_done(state->ctx);
287 NTSTATUS wb_connect_lsa_recv(struct composite_context *c,
290 struct dcerpc_pipe **lsa_pipe,
291 struct policy_handle **lsa_policy)
293 NTSTATUS status = composite_wait(c);
294 if (NT_STATUS_IS_OK(status)) {
295 struct connect_lsa_state *state =
296 talloc_get_type(c->private_data,
297 struct connect_lsa_state);
298 *auth_type = state->auth_type;
299 *lsa_pipe = talloc_steal(mem_ctx, state->lsa_pipe);
300 *lsa_policy = talloc_steal(mem_ctx, state->lsa_policy);
306 NTSTATUS wb_connect_lsa(TALLOC_CTX *mem_ctx,
307 struct smbcli_tree *tree,
308 struct cli_credentials *credentials,
310 struct dcerpc_pipe **lsa_pipe,
311 struct policy_handle **lsa_policy)
313 struct composite_context *c;
314 c = wb_connect_lsa_send(mem_ctx, tree, credentials);
315 return wb_connect_lsa_recv(c, mem_ctx, auth_type, lsa_pipe,