r11812: Convert winbind to the async bind routines. Also remove tridge's hack for the
[samba.git] / source4 / winbind / wb_connect_lsa.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Connect to the LSA pipe, given an smbcli_tree and possibly some
5    credentials. Try ntlmssp, schannel and anon in that order.
6
7    Copyright (C) Volker Lendecke 2005
8    
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.
13    
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.
18    
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.
22 */
23
24 #include "includes.h"
25 #include "libcli/composite/composite.h"
26
27 #include "libcli/raw/libcliraw.h"
28 #include "librpc/gen_ndr/ndr_lsa.h"
29
30 /* Helper to initialize LSA with a specific auth methods. Verify by opening
31  * the LSA policy. */
32
33 struct init_lsa_state {
34         struct composite_context *ctx;
35         struct dcerpc_pipe *lsa_pipe;
36
37         uint8_t auth_type;
38         struct cli_credentials *creds;
39
40         struct lsa_ObjectAttribute objectattr;
41         struct lsa_OpenPolicy2 openpolicy;
42         struct policy_handle *handle;
43 };
44
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);
49
50 struct composite_context *wb_init_lsa_send(TALLOC_CTX *mem_ctx,
51                                            struct smbcli_tree *tree,
52                                            uint8_t auth_type,
53                                            struct cli_credentials *creds)
54 {
55         struct composite_context *result, *ctx;
56         struct init_lsa_state *state;
57
58         result = talloc(mem_ctx, struct composite_context);
59         if (result == NULL) goto failed;
60         result->state = COMPOSITE_STATE_IN_PROGRESS;
61         result->async.fn = NULL;
62         result->event_ctx = tree->session->transport->socket->event.ctx;
63
64         state = talloc(result, struct init_lsa_state);
65         if (state == NULL) goto failed;
66         state->ctx = result;
67         result->private_data = state;
68
69         state->auth_type = auth_type;
70         state->creds = creds;
71
72         state->lsa_pipe = dcerpc_pipe_init(state, result->event_ctx);
73         if (state->lsa_pipe == NULL) goto failed;
74
75         ctx = dcerpc_pipe_open_smb_send(state->lsa_pipe->conn, tree,
76                                         "\\lsarpc");
77         ctx->async.fn = init_lsa_recv_pipe;
78         ctx->async.private_data = state;
79         return result;
80         
81  failed:
82         talloc_free(result);
83         return NULL;
84 }
85
86 static void init_lsa_recv_pipe(struct composite_context *ctx)
87 {
88         struct init_lsa_state *state =
89                 talloc_get_type(ctx->async.private_data,
90                                 struct init_lsa_state);
91
92         state->ctx->status = dcerpc_pipe_open_smb_recv(ctx);
93         if (!composite_is_ok(state->ctx)) return;
94
95         switch (state->auth_type) {
96         case DCERPC_AUTH_TYPE_NONE:
97                 ctx = dcerpc_bind_auth_none_send(state, state->lsa_pipe,
98                                                  DCERPC_LSARPC_UUID,
99                                                  DCERPC_LSARPC_VERSION);
100                 composite_continue(state->ctx, ctx, init_lsa_recv_anon_bind,
101                                    state);
102                 break;
103         case DCERPC_AUTH_TYPE_NTLMSSP:
104         case DCERPC_AUTH_TYPE_SCHANNEL:
105                 if (state->creds == NULL) {
106                         composite_error(state->ctx, NT_STATUS_INTERNAL_ERROR);
107                         return;
108                 }
109                 state->lsa_pipe->conn->flags |= (DCERPC_SIGN | DCERPC_SEAL);
110                 ctx = dcerpc_bind_auth_send(state, state->lsa_pipe,
111                                             DCERPC_LSARPC_UUID,
112                                             DCERPC_LSARPC_VERSION,
113                                             state->creds, state->auth_type,
114                                             NULL);
115                 composite_continue(state->ctx, ctx, init_lsa_recv_auth_bind,
116                                    state);
117                 break;
118         default:
119                 composite_error(state->ctx, NT_STATUS_INTERNAL_ERROR);
120         }
121 }
122
123 static void init_lsa_recv_anon_bind(struct composite_context *ctx)
124 {
125         struct init_lsa_state *state =
126                 talloc_get_type(ctx->async.private_data,
127                                 struct init_lsa_state);
128         struct rpc_request *req;
129
130         state->ctx->status = dcerpc_bind_auth_none_recv(ctx);
131         if (!composite_is_ok(state->ctx)) return;
132                         
133         state->handle = talloc(state, struct policy_handle);
134         if (composite_nomem(state->handle, state->ctx)) return;
135
136         state->openpolicy.in.system_name =
137                 talloc_asprintf(state, "\\\\%s",
138                                 dcerpc_server_name(state->lsa_pipe));
139         ZERO_STRUCT(state->objectattr);
140         state->openpolicy.in.attr = &state->objectattr;
141         state->openpolicy.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
142         state->openpolicy.out.handle = state->handle;
143
144         req = dcerpc_lsa_OpenPolicy2_send(state->lsa_pipe, state,
145                                           &state->openpolicy);
146         composite_continue_rpc(state->ctx, req, init_lsa_recv_openpol, state);
147 }
148
149 static void init_lsa_recv_auth_bind(struct composite_context *ctx)
150 {
151         struct init_lsa_state *state =
152                 talloc_get_type(ctx->async.private_data,
153                                 struct init_lsa_state);
154         struct rpc_request *req;
155
156         state->ctx->status = dcerpc_bind_auth_recv(ctx);
157         if (!composite_is_ok(state->ctx)) return;
158                         
159         state->handle = talloc(state, struct policy_handle);
160         if (composite_nomem(state->handle, state->ctx)) return;
161
162         state->openpolicy.in.system_name =
163                 talloc_asprintf(state, "\\\\%s",
164                                 dcerpc_server_name(state->lsa_pipe));
165         ZERO_STRUCT(state->objectattr);
166         state->openpolicy.in.attr = &state->objectattr;
167         state->openpolicy.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
168         state->openpolicy.out.handle = state->handle;
169
170         req = dcerpc_lsa_OpenPolicy2_send(state->lsa_pipe, state,
171                                           &state->openpolicy);
172         composite_continue_rpc(state->ctx, req, init_lsa_recv_openpol, state);
173 }
174
175 static void init_lsa_recv_openpol(struct rpc_request *req)
176 {
177         struct init_lsa_state *state =
178                 talloc_get_type(req->async.private,
179                                 struct init_lsa_state);
180
181         state->ctx->status = dcerpc_ndr_request_recv(req);
182         if (!composite_is_ok(state->ctx)) return;
183         state->ctx->status = state->openpolicy.out.result;
184         if (!composite_is_ok(state->ctx)) return;
185
186         composite_done(state->ctx);
187 }
188
189 NTSTATUS wb_init_lsa_recv(struct composite_context *c,
190                           TALLOC_CTX *mem_ctx,
191                           struct dcerpc_pipe **lsa_pipe,
192                           struct policy_handle **lsa_policy)
193 {
194         NTSTATUS status = composite_wait(c);
195         if (NT_STATUS_IS_OK(status)) {
196                 struct init_lsa_state *state =
197                         talloc_get_type(c->private_data,
198                                         struct init_lsa_state);
199                 *lsa_pipe = talloc_steal(mem_ctx, state->lsa_pipe);
200                 *lsa_policy = talloc_steal(mem_ctx, state->handle);
201         }
202         talloc_free(c);
203         return status;
204 }
205
206
207 /*
208  * Connect to LSA using the credentials, try NTLMSSP and SCHANNEL using the
209  * given credentials. If both fail or no credentials are available, fall back
210  * to an anonymous bind.
211  */
212
213 struct connect_lsa_state {
214         struct composite_context *ctx;
215         struct smbcli_tree *tree;
216         struct cli_credentials *credentials;
217
218         uint8_t auth_type;
219         struct dcerpc_pipe *lsa_pipe;
220         struct policy_handle *lsa_policy;
221 };
222
223 static void connect_lsa_recv_ntlmssp(struct composite_context *ctx);
224 static void connect_lsa_recv_schannel(struct composite_context *ctx);
225 static void connect_lsa_recv_anon(struct composite_context *ctx);
226
227 struct composite_context *wb_connect_lsa_send(TALLOC_CTX *mem_ctx,
228                                               struct smbcli_tree *tree,
229                                               struct cli_credentials *credentials)
230 {
231         struct composite_context *result, *ctx;
232         struct connect_lsa_state *state;
233
234         result = talloc(mem_ctx, struct composite_context);
235         if (result == NULL) goto failed;
236         result->state = COMPOSITE_STATE_IN_PROGRESS;
237         result->async.fn = NULL;
238         result->event_ctx = tree->session->transport->socket->event.ctx;
239
240         state = talloc(result, struct connect_lsa_state);
241         if (state == NULL) goto failed;
242         state->ctx = result;
243         result->private_data = state;
244
245         state->tree = tree;
246         state->credentials = credentials;
247
248         if (credentials == NULL) {
249                 ctx = wb_init_lsa_send(state, tree, DCERPC_AUTH_TYPE_NONE,
250                                        NULL);
251                 if (ctx == NULL) goto failed;
252                 ctx->async.fn = connect_lsa_recv_anon;
253                 ctx->async.private_data = state;
254                 return result;
255         }
256
257         ctx = wb_init_lsa_send(state, tree, DCERPC_AUTH_TYPE_NTLMSSP,
258                                credentials);
259         if (ctx == NULL) goto failed;
260         ctx->async.fn = connect_lsa_recv_ntlmssp;
261         ctx->async.private_data = state;
262         return result;
263
264  failed:
265         talloc_free(result);
266         return NULL;
267 }
268
269 static void connect_lsa_recv_ntlmssp(struct composite_context *ctx)
270 {
271         struct connect_lsa_state *state =
272                 talloc_get_type(ctx->async.private_data,
273                                 struct connect_lsa_state);
274
275         state->ctx->status = wb_init_lsa_recv(ctx, state, &state->lsa_pipe,
276                                               &state->lsa_policy);
277
278         if (NT_STATUS_IS_OK(state->ctx->status)) {
279                 state->auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
280                 composite_done(state->ctx);
281                 return;
282         }
283
284         ctx = wb_init_lsa_send(state, state->tree, DCERPC_AUTH_TYPE_SCHANNEL,
285                                state->credentials);
286         composite_continue(state->ctx, ctx,
287                            connect_lsa_recv_schannel, state);
288 }
289
290 static void connect_lsa_recv_schannel(struct composite_context *ctx)
291 {
292         struct connect_lsa_state *state =
293                 talloc_get_type(ctx->async.private_data,
294                                 struct connect_lsa_state);
295
296         state->ctx->status = wb_init_lsa_recv(ctx, state, &state->lsa_pipe,
297                                               &state->lsa_policy);
298
299         if (NT_STATUS_IS_OK(state->ctx->status)) {
300                 state->auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
301                 composite_done(state->ctx);
302                 return;
303         }
304
305         ctx = wb_init_lsa_send(state, state->tree, DCERPC_AUTH_TYPE_NONE,
306                                state->credentials);
307         composite_continue(state->ctx, ctx,
308                            connect_lsa_recv_anon, state);
309 }
310
311 static void connect_lsa_recv_anon(struct composite_context *ctx)
312 {
313         struct connect_lsa_state *state =
314                 talloc_get_type(ctx->async.private_data,
315                                 struct connect_lsa_state);
316
317         state->ctx->status = wb_init_lsa_recv(ctx, state, &state->lsa_pipe,
318                                               &state->lsa_policy);
319         if (!composite_is_ok(state->ctx)) return;
320
321         state->auth_type = DCERPC_AUTH_TYPE_NONE;
322         composite_done(state->ctx);
323 }
324
325 NTSTATUS wb_connect_lsa_recv(struct composite_context *c,
326                              TALLOC_CTX *mem_ctx,
327                              uint8_t *auth_type,
328                              struct dcerpc_pipe **lsa_pipe,
329                              struct policy_handle **lsa_policy)
330 {
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);
339         }
340         talloc_free(c);
341         return status;
342 }
343
344 NTSTATUS wb_connect_lsa(TALLOC_CTX *mem_ctx,
345                         struct smbcli_tree *tree,
346                         struct cli_credentials *credentials,
347                         uint8_t *auth_type,
348                         struct dcerpc_pipe **lsa_pipe,
349                         struct policy_handle **lsa_policy)
350 {
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,
354                                    lsa_policy);
355 }