37434fbb0c2242073cb44111bf4f9a0ed61081bb
[metze/samba/wip.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 #undef DBGC_CLASS
37 #define DBGC_CLASS DBGC_AUTH
38
39 /**
40  * Callbacks for NTLMSSP - for both client and server operating modes
41  *
42  */
43
44 static const struct ntlmssp_callbacks {
45         enum ntlmssp_role role;
46         enum ntlmssp_message_type command;
47         NTSTATUS (*sync_fn)(struct gensec_security *gensec_security,
48                             TALLOC_CTX *out_mem_ctx,
49                             DATA_BLOB in, DATA_BLOB *out);
50         struct tevent_req *(*send_fn)(TALLOC_CTX *mem_ctx,
51                                       struct tevent_context *ev,
52                                       struct gensec_security *gensec_security,
53                                       const DATA_BLOB in);
54         NTSTATUS (*recv_fn)(struct tevent_req *req,
55                             TALLOC_CTX *out_mem_ctx,
56                             DATA_BLOB *out);
57 } ntlmssp_callbacks[] = {
58         {
59                 .role           = NTLMSSP_CLIENT,
60                 .command        = NTLMSSP_INITIAL,
61                 .sync_fn        = ntlmssp_client_initial,
62         },{
63                 .role           = NTLMSSP_CLIENT,
64                 .command        = NTLMSSP_NEGOTIATE,
65                 .sync_fn        = gensec_ntlmssp_resume_ccache,
66         },{
67                 .role           = NTLMSSP_SERVER,
68                 .command        = NTLMSSP_NEGOTIATE,
69                 .sync_fn        = gensec_ntlmssp_server_negotiate,
70         },{
71                 .role           = NTLMSSP_CLIENT,
72                 .command        = NTLMSSP_CHALLENGE,
73                 .sync_fn        = ntlmssp_client_challenge,
74         },{
75                 .role           = NTLMSSP_SERVER,
76                 .command        = NTLMSSP_AUTH,
77                 .send_fn        = ntlmssp_server_auth_send,
78                 .recv_fn        = ntlmssp_server_auth_recv,
79         }
80 };
81
82
83 static NTSTATUS gensec_ntlmssp_update_find(struct gensec_security *gensec_security,
84                                            struct gensec_ntlmssp_context *gensec_ntlmssp,
85                                            const DATA_BLOB input, uint32_t *idx)
86 {
87         uint32_t ntlmssp_command;
88         uint32_t i;
89
90         if (gensec_ntlmssp->ntlmssp_state->expected_state == NTLMSSP_DONE) {
91                 /* We are strict here because other modules, which we
92                  * don't fully control (such as GSSAPI) are also
93                  * strict, but are tested less often */
94
95                 DEBUG(1, ("Called NTLMSSP after state machine was 'done'\n"));
96                 return NT_STATUS_INVALID_PARAMETER;
97         }
98
99         if (!input.length) {
100                 switch (gensec_ntlmssp->ntlmssp_state->role) {
101                 case NTLMSSP_CLIENT:
102                         if (gensec_ntlmssp->ntlmssp_state->resume_ccache) {
103                                 /*
104                                  * make sure gensec_ntlmssp_resume_ccache()
105                                  * will be called
106                                  */
107                                 ntlmssp_command = NTLMSSP_NEGOTIATE;
108                                 break;
109                         }
110
111                         ntlmssp_command = NTLMSSP_INITIAL;
112                         break;
113                 case NTLMSSP_SERVER:
114                         if (gensec_security->want_features & GENSEC_FEATURE_DATAGRAM_MODE) {
115                                 /* 'datagram' mode - no neg packet */
116                                 ntlmssp_command = NTLMSSP_NEGOTIATE;
117                         } else {
118                                 /* This is normal in SPNEGO mech negotiation fallback */
119                                 DEBUG(2, ("Failed to parse NTLMSSP packet: zero length\n"));
120                                 return NT_STATUS_INVALID_PARAMETER;
121                         }
122                         break;
123                 default:
124                         DEBUG(1, ("NTLMSSP state has invalid role %d\n",
125                                   gensec_ntlmssp->ntlmssp_state->role));
126                         return NT_STATUS_INVALID_PARAMETER;
127                 }
128         } else {
129                 if (!msrpc_parse(gensec_ntlmssp->ntlmssp_state,
130                                  &input, "Cd",
131                                  "NTLMSSP",
132                                  &ntlmssp_command)) {
133                         DEBUG(1, ("Failed to parse NTLMSSP packet, could not extract NTLMSSP command\n"));
134                         dump_data(2, input.data, input.length);
135                         return NT_STATUS_INVALID_PARAMETER;
136                 }
137         }
138
139         if (ntlmssp_command != gensec_ntlmssp->ntlmssp_state->expected_state) {
140                 DEBUG(2, ("got NTLMSSP command %u, expected %u\n", ntlmssp_command,
141                           gensec_ntlmssp->ntlmssp_state->expected_state));
142                 return NT_STATUS_INVALID_PARAMETER;
143         }
144
145         for (i=0; i < ARRAY_SIZE(ntlmssp_callbacks); i++) {
146                 if (ntlmssp_callbacks[i].role == gensec_ntlmssp->ntlmssp_state->role &&
147                     ntlmssp_callbacks[i].command == ntlmssp_command) {
148                         *idx = i;
149                         return NT_STATUS_OK;
150                 }
151         }
152
153         DEBUG(1, ("failed to find NTLMSSP callback for NTLMSSP mode %u, command %u\n",
154                   gensec_ntlmssp->ntlmssp_state->role, ntlmssp_command));
155
156         return NT_STATUS_INVALID_PARAMETER;
157 }
158
159 struct gensec_ntlmssp_update_state {
160         const struct ntlmssp_callbacks *cb;
161         NTSTATUS status;
162         DATA_BLOB out;
163 };
164
165 static void gensec_ntlmssp_update_done(struct tevent_req *subreq);
166
167 static struct tevent_req *gensec_ntlmssp_update_send(TALLOC_CTX *mem_ctx,
168                                                      struct tevent_context *ev,
169                                                      struct gensec_security *gensec_security,
170                                                      const DATA_BLOB in)
171 {
172         struct gensec_ntlmssp_context *gensec_ntlmssp =
173                 talloc_get_type_abort(gensec_security->private_data,
174                                       struct gensec_ntlmssp_context);
175         struct tevent_req *req = NULL;
176         struct gensec_ntlmssp_update_state *state = NULL;
177         NTSTATUS status;
178         uint32_t i = 0;
179
180         req = tevent_req_create(mem_ctx, &state,
181                                 struct gensec_ntlmssp_update_state);
182         if (req == NULL) {
183                 return NULL;
184         }
185
186         status = gensec_ntlmssp_update_find(gensec_security,
187                                             gensec_ntlmssp,
188                                             in, &i);
189         if (tevent_req_nterror(req, status)) {
190                 return tevent_req_post(req, ev);
191         }
192
193         if (ntlmssp_callbacks[i].send_fn != NULL) {
194                 struct tevent_req *subreq = NULL;
195
196                 state->cb = &ntlmssp_callbacks[i];
197
198                 subreq = state->cb->send_fn(state, ev,
199                                             gensec_security,
200                                             in);
201                 if (tevent_req_nomem(subreq, req)) {
202                         return tevent_req_post(req, ev);
203                 }
204                 tevent_req_set_callback(subreq,
205                                         gensec_ntlmssp_update_done,
206                                         req);
207                 return req;
208         }
209
210         status = ntlmssp_callbacks[i].sync_fn(gensec_security,
211                                               state,
212                                               in, &state->out);
213         state->status = status;
214         if (NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
215                 tevent_req_done(req);
216                 return tevent_req_post(req, ev);
217         }
218         if (tevent_req_nterror(req, status)) {
219                 return tevent_req_post(req, ev);
220         }
221
222         tevent_req_done(req);
223         return tevent_req_post(req, ev);
224 }
225
226 static void gensec_ntlmssp_update_done(struct tevent_req *subreq)
227 {
228         struct tevent_req *req =
229                 tevent_req_callback_data(subreq,
230                 struct tevent_req);
231         struct gensec_ntlmssp_update_state *state =
232                 tevent_req_data(req,
233                 struct gensec_ntlmssp_update_state);
234         NTSTATUS status;
235
236         status = state->cb->recv_fn(subreq, state, &state->out);
237         TALLOC_FREE(subreq);
238         if (GENSEC_UPDATE_IS_NTERROR(status)) {
239                 tevent_req_nterror(req, status);
240                 return;
241         }
242
243         state->status = status;
244         tevent_req_done(req);
245 }
246
247 static NTSTATUS gensec_ntlmssp_update_recv(struct tevent_req *req,
248                                            TALLOC_CTX *out_mem_ctx,
249                                            DATA_BLOB *out)
250 {
251         struct gensec_ntlmssp_update_state *state =
252                 tevent_req_data(req,
253                 struct gensec_ntlmssp_update_state);
254         NTSTATUS status;
255
256         *out = data_blob_null;
257
258         if (tevent_req_is_nterror(req, &status)) {
259                 tevent_req_received(req);
260                 return status;
261         }
262
263         *out = state->out;
264         talloc_steal(out_mem_ctx, state->out.data);
265         status = state->status;
266         tevent_req_received(req);
267         return status;
268 }
269
270 static NTSTATUS gensec_ntlmssp_may_reset_crypto(struct gensec_security *gensec_security,
271                                                 bool full_reset)
272 {
273         struct gensec_ntlmssp_context *gensec_ntlmssp =
274                 talloc_get_type_abort(gensec_security->private_data,
275                                       struct gensec_ntlmssp_context);
276         struct ntlmssp_state *ntlmssp_state = gensec_ntlmssp->ntlmssp_state;
277         NTSTATUS status;
278         bool reset_seqnums = full_reset;
279
280         if (!gensec_ntlmssp_have_feature(gensec_security, GENSEC_FEATURE_SIGN)) {
281                 return NT_STATUS_OK;
282         }
283
284         status = ntlmssp_sign_reset(ntlmssp_state, reset_seqnums);
285         if (!NT_STATUS_IS_OK(status)) {
286                 DEBUG(1, ("Could not reset NTLMSSP signing/sealing system (error was: %s)\n",
287                           nt_errstr(status)));
288                 return status;
289         }
290
291         return NT_STATUS_OK;
292 }
293
294 static const char *gensec_ntlmssp_final_auth_type(struct gensec_security *gensec_security)
295 {
296         return GENSEC_FINAL_AUTH_TYPE_NTLMSSP;
297 }
298
299 static const char *gensec_ntlmssp_oids[] = {
300         GENSEC_OID_NTLMSSP,
301         NULL
302 };
303
304 static const struct gensec_security_ops gensec_ntlmssp_security_ops = {
305         .name           = "ntlmssp",
306         .sasl_name      = GENSEC_SASL_NAME_NTLMSSP, /* "NTLM" */
307         .auth_type      = DCERPC_AUTH_TYPE_NTLMSSP,
308         .oid            = gensec_ntlmssp_oids,
309         .client_start   = gensec_ntlmssp_client_start,
310         .server_start   = gensec_ntlmssp_server_start,
311         .magic          = gensec_ntlmssp_magic,
312         .update_send    = gensec_ntlmssp_update_send,
313         .update_recv    = gensec_ntlmssp_update_recv,
314         .may_reset_crypto= gensec_ntlmssp_may_reset_crypto,
315         .sig_size       = gensec_ntlmssp_sig_size,
316         .sign_packet    = gensec_ntlmssp_sign_packet,
317         .check_packet   = gensec_ntlmssp_check_packet,
318         .seal_packet    = gensec_ntlmssp_seal_packet,
319         .unseal_packet  = gensec_ntlmssp_unseal_packet,
320         .wrap           = gensec_ntlmssp_wrap,
321         .unwrap         = gensec_ntlmssp_unwrap,
322         .session_key    = gensec_ntlmssp_session_key,
323         .session_info   = gensec_ntlmssp_session_info,
324         .have_feature   = gensec_ntlmssp_have_feature,
325         .final_auth_type = gensec_ntlmssp_final_auth_type,
326         .enabled        = true,
327         .priority       = GENSEC_NTLMSSP
328 };
329
330 static const struct gensec_security_ops gensec_ntlmssp_resume_ccache_ops = {
331         .name           = "ntlmssp_resume_ccache",
332         .client_start   = gensec_ntlmssp_resume_ccache_start,
333         .update_send    = gensec_ntlmssp_update_send,
334         .update_recv    = gensec_ntlmssp_update_recv,
335         .session_key    = gensec_ntlmssp_session_key,
336         .have_feature   = gensec_ntlmssp_have_feature,
337         .enabled        = true,
338         .priority       = GENSEC_NTLMSSP
339 };
340
341 _PUBLIC_ NTSTATUS gensec_ntlmssp_init(TALLOC_CTX *ctx)
342 {
343         NTSTATUS ret;
344
345         ret = gensec_register(ctx, &gensec_ntlmssp_security_ops);
346         if (!NT_STATUS_IS_OK(ret)) {
347                 DEBUG(0,("Failed to register '%s' gensec backend!\n",
348                         gensec_ntlmssp_security_ops.name));
349                 return ret;
350         }
351
352         ret = gensec_register(ctx, &gensec_ntlmssp_resume_ccache_ops);
353         if (!NT_STATUS_IS_OK(ret)) {
354                 DEBUG(0,("Failed to register '%s' gensec backend!\n",
355                         gensec_ntlmssp_resume_ccache_ops.name));
356                 return ret;
357         }
358
359         return ret;
360 }
361
362 static struct gensec_security *gensec_find_child_by_ops(struct gensec_security *gensec_security,
363                                                         const struct gensec_security_ops *ops)
364 {
365         struct gensec_security *current = gensec_security;
366
367         while (current != NULL) {
368                 if (current->ops == ops) {
369                         return current;
370                 }
371
372                 current = current->child_security;
373         }
374
375         return NULL;
376 }
377
378 uint32_t gensec_ntlmssp_neg_flags(struct gensec_security *gensec_security)
379 {
380         struct gensec_ntlmssp_context *gensec_ntlmssp;
381
382         gensec_security = gensec_find_child_by_ops(gensec_security,
383                                         &gensec_ntlmssp_security_ops);
384         if (gensec_security == NULL) {
385                 return 0;
386         }
387
388         gensec_ntlmssp = talloc_get_type_abort(gensec_security->private_data,
389                                                struct gensec_ntlmssp_context);
390         return gensec_ntlmssp->ntlmssp_state->neg_flags;
391 }
392
393 const char *gensec_ntlmssp_server_domain(struct gensec_security *gensec_security)
394 {
395         struct gensec_ntlmssp_context *gensec_ntlmssp;
396
397         gensec_security = gensec_find_child_by_ops(gensec_security,
398                                         &gensec_ntlmssp_security_ops);
399         if (gensec_security == NULL) {
400                 return NULL;
401         }
402
403         gensec_ntlmssp = talloc_get_type_abort(gensec_security->private_data,
404                                                struct gensec_ntlmssp_context);
405         return gensec_ntlmssp->ntlmssp_state->server.netbios_domain;
406 }