auth/ntlmssp: prepare update_send/recv for real async processing
[sfrench/samba-autobuild/.git] / auth / ntlmssp / ntlmssp.c
1 /*
2    Unix SMB/Netbios implementation.
3    Version 3.0
4    handle NLTMSSP, client server side parsing
5
6    Copyright (C) Andrew Tridgell      2001
7    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2001-2005
8    Copyright (C) Stefan Metzmacher 2005
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 struct auth_session_info;
25
26 #include "includes.h"
27 #include <tevent.h>
28 #include "lib/util/tevent_ntstatus.h"
29 #include "auth/ntlmssp/ntlmssp.h"
30 #include "auth/ntlmssp/ntlmssp_private.h"
31 #include "../libcli/auth/libcli_auth.h"
32 #include "librpc/gen_ndr/ndr_dcerpc.h"
33 #include "auth/gensec/gensec.h"
34 #include "auth/gensec/gensec_internal.h"
35
36 /**
37  * Callbacks for NTLMSSP - for both client and server operating modes
38  *
39  */
40
41 static const struct ntlmssp_callbacks {
42         enum ntlmssp_role role;
43         enum ntlmssp_message_type command;
44         NTSTATUS (*sync_fn)(struct gensec_security *gensec_security,
45                             TALLOC_CTX *out_mem_ctx,
46                             DATA_BLOB in, DATA_BLOB *out);
47         struct tevent_req *(*send_fn)(TALLOC_CTX *mem_ctx,
48                                       struct tevent_context *ev,
49                                       struct gensec_security *gensec_security,
50                                       const DATA_BLOB in);
51         NTSTATUS (*recv_fn)(struct tevent_req *req,
52                             TALLOC_CTX *out_mem_ctx,
53                             DATA_BLOB *out);
54 } ntlmssp_callbacks[] = {
55         {
56                 .role           = NTLMSSP_CLIENT,
57                 .command        = NTLMSSP_INITIAL,
58                 .sync_fn        = ntlmssp_client_initial,
59         },{
60                 .role           = NTLMSSP_CLIENT,
61                 .command        = NTLMSSP_NEGOTIATE,
62                 .sync_fn        = gensec_ntlmssp_resume_ccache,
63         },{
64                 .role           = NTLMSSP_SERVER,
65                 .command        = NTLMSSP_NEGOTIATE,
66                 .sync_fn        = gensec_ntlmssp_server_negotiate,
67         },{
68                 .role           = NTLMSSP_CLIENT,
69                 .command        = NTLMSSP_CHALLENGE,
70                 .sync_fn        = ntlmssp_client_challenge,
71         },{
72                 .role           = NTLMSSP_SERVER,
73                 .command        = NTLMSSP_AUTH,
74                 .sync_fn        = gensec_ntlmssp_server_auth,
75         }
76 };
77
78
79 static NTSTATUS gensec_ntlmssp_update_find(struct gensec_security *gensec_security,
80                                            struct gensec_ntlmssp_context *gensec_ntlmssp,
81                                            const DATA_BLOB input, uint32_t *idx)
82 {
83         uint32_t ntlmssp_command;
84         uint32_t i;
85
86         if (gensec_ntlmssp->ntlmssp_state->expected_state == NTLMSSP_DONE) {
87                 /* We are strict here because other modules, which we
88                  * don't fully control (such as GSSAPI) are also
89                  * strict, but are tested less often */
90
91                 DEBUG(1, ("Called NTLMSSP after state machine was 'done'\n"));
92                 return NT_STATUS_INVALID_PARAMETER;
93         }
94
95         if (!input.length) {
96                 switch (gensec_ntlmssp->ntlmssp_state->role) {
97                 case NTLMSSP_CLIENT:
98                         if (gensec_ntlmssp->ntlmssp_state->resume_ccache) {
99                                 /*
100                                  * make sure gensec_ntlmssp_resume_ccache()
101                                  * will be called
102                                  */
103                                 ntlmssp_command = NTLMSSP_NEGOTIATE;
104                                 break;
105                         }
106
107                         ntlmssp_command = NTLMSSP_INITIAL;
108                         break;
109                 case NTLMSSP_SERVER:
110                         if (gensec_security->want_features & GENSEC_FEATURE_DATAGRAM_MODE) {
111                                 /* 'datagram' mode - no neg packet */
112                                 ntlmssp_command = NTLMSSP_NEGOTIATE;
113                         } else {
114                                 /* This is normal in SPNEGO mech negotiation fallback */
115                                 DEBUG(2, ("Failed to parse NTLMSSP packet: zero length\n"));
116                                 return NT_STATUS_INVALID_PARAMETER;
117                         }
118                         break;
119                 default:
120                         DEBUG(1, ("NTLMSSP state has invalid role %d\n",
121                                   gensec_ntlmssp->ntlmssp_state->role));
122                         return NT_STATUS_INVALID_PARAMETER;
123                 }
124         } else {
125                 if (!msrpc_parse(gensec_ntlmssp->ntlmssp_state,
126                                  &input, "Cd",
127                                  "NTLMSSP",
128                                  &ntlmssp_command)) {
129                         DEBUG(1, ("Failed to parse NTLMSSP packet, could not extract NTLMSSP command\n"));
130                         dump_data(2, input.data, input.length);
131                         return NT_STATUS_INVALID_PARAMETER;
132                 }
133         }
134
135         if (ntlmssp_command != gensec_ntlmssp->ntlmssp_state->expected_state) {
136                 DEBUG(2, ("got NTLMSSP command %u, expected %u\n", ntlmssp_command,
137                           gensec_ntlmssp->ntlmssp_state->expected_state));
138                 return NT_STATUS_INVALID_PARAMETER;
139         }
140
141         for (i=0; i < ARRAY_SIZE(ntlmssp_callbacks); i++) {
142                 if (ntlmssp_callbacks[i].role == gensec_ntlmssp->ntlmssp_state->role &&
143                     ntlmssp_callbacks[i].command == ntlmssp_command) {
144                         *idx = i;
145                         return NT_STATUS_OK;
146                 }
147         }
148
149         DEBUG(1, ("failed to find NTLMSSP callback for NTLMSSP mode %u, command %u\n",
150                   gensec_ntlmssp->ntlmssp_state->role, ntlmssp_command));
151
152         return NT_STATUS_INVALID_PARAMETER;
153 }
154
155 struct gensec_ntlmssp_update_state {
156         const struct ntlmssp_callbacks *cb;
157         NTSTATUS status;
158         DATA_BLOB out;
159 };
160
161 static void gensec_ntlmssp_update_done(struct tevent_req *subreq);
162
163 static struct tevent_req *gensec_ntlmssp_update_send(TALLOC_CTX *mem_ctx,
164                                                      struct tevent_context *ev,
165                                                      struct gensec_security *gensec_security,
166                                                      const DATA_BLOB in)
167 {
168         struct gensec_ntlmssp_context *gensec_ntlmssp =
169                 talloc_get_type_abort(gensec_security->private_data,
170                                       struct gensec_ntlmssp_context);
171         struct tevent_req *req = NULL;
172         struct gensec_ntlmssp_update_state *state = NULL;
173         NTSTATUS status;
174         uint32_t i = 0;
175
176         req = tevent_req_create(mem_ctx, &state,
177                                 struct gensec_ntlmssp_update_state);
178         if (req == NULL) {
179                 return NULL;
180         }
181
182         status = gensec_ntlmssp_update_find(gensec_security,
183                                             gensec_ntlmssp,
184                                             in, &i);
185         if (tevent_req_nterror(req, status)) {
186                 return tevent_req_post(req, ev);
187         }
188
189         if (ntlmssp_callbacks[i].send_fn != NULL) {
190                 struct tevent_req *subreq = NULL;
191
192                 state->cb = &ntlmssp_callbacks[i];
193
194                 subreq = state->cb->send_fn(state, ev,
195                                             gensec_security,
196                                             in);
197                 if (tevent_req_nomem(subreq, req)) {
198                         return tevent_req_post(req, ev);
199                 }
200                 tevent_req_set_callback(subreq,
201                                         gensec_ntlmssp_update_done,
202                                         req);
203                 return req;
204         }
205
206         status = ntlmssp_callbacks[i].sync_fn(gensec_security,
207                                               state,
208                                               in, &state->out);
209         state->status = status;
210         if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
211                 tevent_req_done(req);
212                 return tevent_req_post(req, ev);
213         }
214         if (tevent_req_nterror(req, status)) {
215                 return tevent_req_post(req, ev);
216         }
217
218         tevent_req_done(req);
219         return tevent_req_post(req, ev);
220 }
221
222 static void gensec_ntlmssp_update_done(struct tevent_req *subreq)
223 {
224         struct tevent_req *req =
225                 tevent_req_callback_data(subreq,
226                 struct tevent_req);
227         struct gensec_ntlmssp_update_state *state =
228                 tevent_req_data(req,
229                 struct gensec_ntlmssp_update_state);
230         NTSTATUS status;
231
232         status = state->cb->recv_fn(subreq, state, &state->out);
233         TALLOC_FREE(subreq);
234         if (GENSEC_UPDATE_IS_NTERROR(status)) {
235                 tevent_req_nterror(req, status);
236                 return;
237         }
238
239         state->status = status;
240         tevent_req_done(req);
241 }
242
243 static NTSTATUS gensec_ntlmssp_update_recv(struct tevent_req *req,
244                                            TALLOC_CTX *out_mem_ctx,
245                                            DATA_BLOB *out)
246 {
247         struct gensec_ntlmssp_update_state *state =
248                 tevent_req_data(req,
249                 struct gensec_ntlmssp_update_state);
250         NTSTATUS status;
251
252         *out = data_blob_null;
253
254         if (tevent_req_is_nterror(req, &status)) {
255                 tevent_req_received(req);
256                 return status;
257         }
258
259         *out = state->out;
260         talloc_steal(out_mem_ctx, state->out.data);
261         status = state->status;
262         tevent_req_received(req);
263         return status;
264 }
265
266 static NTSTATUS gensec_ntlmssp_may_reset_crypto(struct gensec_security *gensec_security,
267                                                 bool full_reset)
268 {
269         struct gensec_ntlmssp_context *gensec_ntlmssp =
270                 talloc_get_type_abort(gensec_security->private_data,
271                                       struct gensec_ntlmssp_context);
272         struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp->ntlmssp_state;
273         NTSTATUS status;
274         bool reset_seqnums = full_reset;
275
276         if (!gensec_ntlmssp_have_feature(gensec_security, GENSEC_FEATURE_SIGN)) {
277                 return NT_STATUS_OK;
278         }
279
280         status = ntlmssp_sign_reset(ntlmssp_state, reset_seqnums);
281         if (!NT_STATUS_IS_OK(status)) {
282                 DEBUG(1, ("Could not reset NTLMSSP signing/sealing system (error was: %s)\n",
283                           nt_errstr(status)));
284                 return status;
285         }
286
287         return NT_STATUS_OK;
288 }
289
290 static const char *gensec_ntlmssp_final_auth_type(struct gensec_security *gensec_security)
291 {
292         return GENSEC_FINAL_AUTH_TYPE_NTLMSSP;
293 }
294
295 static const char *gensec_ntlmssp_oids[] = {
296         GENSEC_OID_NTLMSSP,
297         NULL
298 };
299
300 static const struct gensec_security_ops gensec_ntlmssp_security_ops = {
301         .name           = "ntlmssp",
302         .sasl_name      = GENSEC_SASL_NAME_NTLMSSP, /* "NTLM" */
303         .auth_type      = DCERPC_AUTH_TYPE_NTLMSSP,
304         .oid            = gensec_ntlmssp_oids,
305         .client_start   = gensec_ntlmssp_client_start,
306         .server_start   = gensec_ntlmssp_server_start,
307         .magic          = gensec_ntlmssp_magic,
308         .update_send    = gensec_ntlmssp_update_send,
309         .update_recv    = gensec_ntlmssp_update_recv,
310         .may_reset_crypto= gensec_ntlmssp_may_reset_crypto,
311         .sig_size       = gensec_ntlmssp_sig_size,
312         .sign_packet    = gensec_ntlmssp_sign_packet,
313         .check_packet   = gensec_ntlmssp_check_packet,
314         .seal_packet    = gensec_ntlmssp_seal_packet,
315         .unseal_packet  = gensec_ntlmssp_unseal_packet,
316         .wrap           = gensec_ntlmssp_wrap,
317         .unwrap         = gensec_ntlmssp_unwrap,
318         .session_key    = gensec_ntlmssp_session_key,
319         .session_info   = gensec_ntlmssp_session_info,
320         .have_feature   = gensec_ntlmssp_have_feature,
321         .final_auth_type = gensec_ntlmssp_final_auth_type,
322         .enabled        = true,
323         .priority       = GENSEC_NTLMSSP
324 };
325
326 static const struct gensec_security_ops gensec_ntlmssp_resume_ccache_ops = {
327         .name           = "ntlmssp_resume_ccache",
328         .client_start   = gensec_ntlmssp_resume_ccache_start,
329         .update_send    = gensec_ntlmssp_update_send,
330         .update_recv    = gensec_ntlmssp_update_recv,
331         .session_key    = gensec_ntlmssp_session_key,
332         .have_feature   = gensec_ntlmssp_have_feature,
333         .enabled        = true,
334         .priority       = GENSEC_NTLMSSP
335 };
336
337 _PUBLIC_ NTSTATUS gensec_ntlmssp_init(TALLOC_CTX *ctx)
338 {
339         NTSTATUS ret;
340
341         ret = gensec_register(ctx, &gensec_ntlmssp_security_ops);
342         if (!NT_STATUS_IS_OK(ret)) {
343                 DEBUG(0,("Failed to register '%s' gensec backend!\n",
344                         gensec_ntlmssp_security_ops.name));
345                 return ret;
346         }
347
348         ret = gensec_register(ctx, &gensec_ntlmssp_resume_ccache_ops);
349         if (!NT_STATUS_IS_OK(ret)) {
350                 DEBUG(0,("Failed to register '%s' gensec backend!\n",
351                         gensec_ntlmssp_resume_ccache_ops.name));
352                 return ret;
353         }
354
355         return ret;
356 }
357
358 static struct gensec_security *gensec_find_child_by_ops(struct gensec_security *gensec_security,
359                                                         const struct gensec_security_ops *ops)
360 {
361         struct gensec_security *current = gensec_security;
362
363         while (current != NULL) {
364                 if (current->ops == ops) {
365                         return current;
366                 }
367
368                 current = current->child_security;
369         }
370
371         return NULL;
372 }
373
374 uint32_t gensec_ntlmssp_neg_flags(struct gensec_security *gensec_security)
375 {
376         struct gensec_ntlmssp_context *gensec_ntlmssp;
377
378         gensec_security = gensec_find_child_by_ops(gensec_security,
379                                         &gensec_ntlmssp_security_ops);
380         if (gensec_security == NULL) {
381                 return 0;
382         }
383
384         gensec_ntlmssp = talloc_get_type_abort(gensec_security->private_data,
385                                                struct gensec_ntlmssp_context);
386         return gensec_ntlmssp->ntlmssp_state->neg_flags;
387 }
388
389 const char *gensec_ntlmssp_server_domain(struct gensec_security *gensec_security)
390 {
391         struct gensec_ntlmssp_context *gensec_ntlmssp;
392
393         gensec_security = gensec_find_child_by_ops(gensec_security,
394                                         &gensec_ntlmssp_security_ops);
395         if (gensec_security == NULL) {
396                 return NULL;
397         }
398
399         gensec_ntlmssp = talloc_get_type_abort(gensec_security->private_data,
400                                                struct gensec_ntlmssp_context);
401         return gensec_ntlmssp->ntlmssp_state->server.netbios_domain;
402 }