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 static struct composite_context *wb_init_lsa_send(struct smbcli_tree *tree,
50 struct cli_credentials *creds)
52 struct composite_context *result, *ctx;
53 struct init_lsa_state *state;
55 result = talloc(NULL, struct composite_context);
56 if (result == NULL) goto failed;
57 result->state = COMPOSITE_STATE_IN_PROGRESS;
58 result->async.fn = NULL;
59 result->event_ctx = tree->session->transport->socket->event.ctx;
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->conn, 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);
88 struct rpc_request *req;
90 state->ctx->status = dcerpc_pipe_open_smb_recv(ctx);
91 if (!composite_is_ok(state->ctx)) return;
93 switch (state->auth_type) {
94 case DCERPC_AUTH_TYPE_NONE:
96 dcerpc_bind_auth_none(state->lsa_pipe,
98 DCERPC_LSARPC_VERSION);
100 case DCERPC_AUTH_TYPE_NTLMSSP:
101 case DCERPC_AUTH_TYPE_SCHANNEL:
102 if (state->creds == NULL) {
103 composite_error(state->ctx, NT_STATUS_INTERNAL_ERROR);
106 state->lsa_pipe->conn->flags |= (DCERPC_SIGN | DCERPC_SEAL);
108 dcerpc_bind_auth_password(state->lsa_pipe,
110 DCERPC_LSARPC_VERSION,
116 state->ctx->status = NT_STATUS_INTERNAL_ERROR;
120 state->handle = talloc(state, struct policy_handle);
121 if (composite_nomem(state->handle, state->ctx)) return;
123 state->openpolicy.in.system_name =
124 talloc_asprintf(state, "\\\\%s",
125 dcerpc_server_name(state->lsa_pipe));
126 ZERO_STRUCT(state->objectattr);
127 state->openpolicy.in.attr = &state->objectattr;
128 state->openpolicy.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
129 state->openpolicy.out.handle = state->handle;
131 req = dcerpc_lsa_OpenPolicy2_send(state->lsa_pipe, state,
133 composite_continue_rpc(state->ctx, req, init_lsa_recv_openpol, state);
136 static void init_lsa_recv_openpol(struct rpc_request *req)
138 struct init_lsa_state *state =
139 talloc_get_type(req->async.private,
140 struct init_lsa_state);
142 state->ctx->status = dcerpc_ndr_request_recv(req);
143 if (!composite_is_ok(state->ctx)) return;
144 state->ctx->status = state->openpolicy.out.result;
145 if (!composite_is_ok(state->ctx)) return;
147 composite_done(state->ctx);
150 static NTSTATUS wb_init_lsa_recv(struct composite_context *c,
152 struct dcerpc_pipe **lsa_pipe,
153 struct policy_handle **lsa_policy)
155 NTSTATUS status = composite_wait(c);
156 if (NT_STATUS_IS_OK(status)) {
157 struct init_lsa_state *state =
158 talloc_get_type(c->private_data,
159 struct init_lsa_state);
160 *lsa_pipe = talloc_steal(mem_ctx, state->lsa_pipe);
161 *lsa_policy = talloc_steal(mem_ctx, state->handle);
169 * Connect to LSA using the credentials, try NTLMSSP and SCHANNEL using the
170 * given credentials. If both fail or no credentials are available, fall back
171 * to an anonymous bind.
174 struct connect_lsa_state {
175 struct composite_context *ctx;
176 struct smbcli_tree *tree;
177 struct cli_credentials *credentials;
180 struct dcerpc_pipe *lsa_pipe;
181 struct policy_handle *lsa_policy;
184 static void connect_lsa_recv_ntlmssp(struct composite_context *ctx);
185 static void connect_lsa_recv_schannel(struct composite_context *ctx);
186 static void connect_lsa_recv_anon(struct composite_context *ctx);
188 struct composite_context *wb_connect_lsa_send(struct smbcli_tree *tree,
189 struct cli_credentials *credentials)
191 struct composite_context *result, *ctx;
192 struct connect_lsa_state *state;
194 result = talloc(NULL, struct composite_context);
195 if (result == NULL) goto failed;
196 result->state = COMPOSITE_STATE_IN_PROGRESS;
197 result->async.fn = NULL;
198 result->event_ctx = tree->session->transport->socket->event.ctx;
200 state = talloc(result, struct connect_lsa_state);
201 if (state == NULL) goto failed;
203 result->private_data = state;
206 state->credentials = credentials;
208 if (credentials == NULL) {
209 ctx = wb_init_lsa_send(tree, DCERPC_AUTH_TYPE_NONE, NULL);
210 if (ctx == NULL) goto failed;
211 ctx->async.fn = connect_lsa_recv_anon;
212 ctx->async.private_data = state;
216 ctx = wb_init_lsa_send(tree, DCERPC_AUTH_TYPE_NTLMSSP, credentials);
217 if (ctx == NULL) goto failed;
218 ctx->async.fn = connect_lsa_recv_ntlmssp;
219 ctx->async.private_data = state;
227 static void connect_lsa_recv_ntlmssp(struct composite_context *ctx)
229 struct connect_lsa_state *state =
230 talloc_get_type(ctx->async.private_data,
231 struct connect_lsa_state);
233 state->ctx->status = wb_init_lsa_recv(ctx, state, &state->lsa_pipe,
236 if (NT_STATUS_IS_OK(state->ctx->status)) {
237 state->auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
238 composite_done(state->ctx);
242 ctx = wb_init_lsa_send(state->tree, DCERPC_AUTH_TYPE_SCHANNEL,
244 composite_continue(state->ctx, ctx,
245 connect_lsa_recv_schannel, state);
248 static void connect_lsa_recv_schannel(struct composite_context *ctx)
250 struct connect_lsa_state *state =
251 talloc_get_type(ctx->async.private_data,
252 struct connect_lsa_state);
254 state->ctx->status = wb_init_lsa_recv(ctx, state, &state->lsa_pipe,
257 if (NT_STATUS_IS_OK(state->ctx->status)) {
258 state->auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
259 composite_done(state->ctx);
263 ctx = wb_init_lsa_send(state->tree, DCERPC_AUTH_TYPE_NONE,
265 composite_continue(state->ctx, ctx,
266 connect_lsa_recv_anon, state);
269 static void connect_lsa_recv_anon(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,
277 if (!composite_is_ok(state->ctx)) return;
279 state->auth_type = DCERPC_AUTH_TYPE_NONE;
280 composite_done(state->ctx);
283 NTSTATUS wb_connect_lsa_recv(struct composite_context *c,
286 struct dcerpc_pipe **lsa_pipe,
287 struct policy_handle **lsa_policy)
289 NTSTATUS status = composite_wait(c);
290 if (NT_STATUS_IS_OK(status)) {
291 struct connect_lsa_state *state =
292 talloc_get_type(c->private_data,
293 struct connect_lsa_state);
294 *auth_type = state->auth_type;
295 *lsa_pipe = talloc_steal(mem_ctx, state->lsa_pipe);
296 *lsa_policy = talloc_steal(mem_ctx, state->lsa_policy);
302 NTSTATUS wb_connect_lsa(struct smbcli_tree *tree,
303 struct cli_credentials *credentials,
306 struct dcerpc_pipe **lsa_pipe,
307 struct policy_handle **lsa_policy)
309 struct composite_context *c;
310 c = wb_connect_lsa_send(tree, credentials);
311 return wb_connect_lsa_recv(c, mem_ctx, auth_type, lsa_pipe,