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