Merge branch 'master' of ssh://git.samba.org/data/git/samba
[ira/wip.git] / source4 / auth / ntlmssp / ntlmssp_server.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 #include "includes.h"
25 #include "auth/ntlmssp/ntlmssp.h"
26 #include "auth/ntlmssp/msrpc_parse.h"
27 #include "../lib/crypto/crypto.h"
28 #include "libcli/auth/libcli_auth.h"
29 #include "auth/credentials/credentials.h"
30 #include "auth/gensec/gensec.h"
31 #include "auth/auth.h"
32 #include "auth/ntlm/auth_proto.h"
33 #include "param/param.h"
34 #include "auth/session_proto.h"
35
36 /** 
37  * Set a username on an NTLMSSP context - ensures it is talloc()ed 
38  *
39  */
40
41 static NTSTATUS ntlmssp_set_username(struct gensec_ntlmssp_state *gensec_ntlmssp_state, const char *user) 
42 {
43         if (!user) {
44                 /* it should be at least "" */
45                 DEBUG(1, ("NTLMSSP failed to set username - cannot accept NULL username\n"));
46                 return NT_STATUS_INVALID_PARAMETER;
47         }
48         gensec_ntlmssp_state->user = talloc_strdup(gensec_ntlmssp_state, user);
49         if (!gensec_ntlmssp_state->user) {
50                 return NT_STATUS_NO_MEMORY;
51         }
52         return NT_STATUS_OK;
53 }
54
55 /** 
56  * Set a domain on an NTLMSSP context - ensures it is talloc()ed 
57  *
58  */
59 static NTSTATUS ntlmssp_set_domain(struct gensec_ntlmssp_state *gensec_ntlmssp_state, const char *domain) 
60 {
61         gensec_ntlmssp_state->domain = talloc_strdup(gensec_ntlmssp_state, domain);
62         if (!gensec_ntlmssp_state->domain) {
63                 return NT_STATUS_NO_MEMORY;
64         }
65         return NT_STATUS_OK;
66 }
67
68 /** 
69  * Set a workstation on an NTLMSSP context - ensures it is talloc()ed 
70  *
71  */
72 static NTSTATUS ntlmssp_set_workstation(struct gensec_ntlmssp_state *gensec_ntlmssp_state, const char *workstation) 
73 {
74         gensec_ntlmssp_state->workstation = talloc_strdup(gensec_ntlmssp_state, workstation);
75         if (!gensec_ntlmssp_state->workstation) {
76                 return NT_STATUS_NO_MEMORY;
77         }
78         return NT_STATUS_OK;
79 }
80
81 /**
82  * Determine correct target name flags for reply, given server role 
83  * and negotiated flags
84  * 
85  * @param gensec_ntlmssp_state NTLMSSP State
86  * @param neg_flags The flags from the packet
87  * @param chal_flags The flags to be set in the reply packet
88  * @return The 'target name' string.
89  */
90
91 static const char *ntlmssp_target_name(struct gensec_ntlmssp_state *gensec_ntlmssp_state,
92                                        uint32_t neg_flags, uint32_t *chal_flags) 
93 {
94         if (neg_flags & NTLMSSP_REQUEST_TARGET) {
95                 *chal_flags |= NTLMSSP_CHAL_TARGET_INFO;
96                 *chal_flags |= NTLMSSP_REQUEST_TARGET;
97                 if (gensec_ntlmssp_state->server_role == ROLE_STANDALONE) {
98                         *chal_flags |= NTLMSSP_TARGET_TYPE_SERVER;
99                         return gensec_ntlmssp_state->server_name;
100                 } else {
101                         *chal_flags |= NTLMSSP_TARGET_TYPE_DOMAIN;
102                         return gensec_ntlmssp_state->domain;
103                 };
104         } else {
105                 return "";
106         }
107 }
108
109
110
111 /**
112  * Next state function for the Negotiate packet
113  * 
114  * @param gensec_security GENSEC state
115  * @param out_mem_ctx Memory context for *out
116  * @param in The request, as a DATA_BLOB.  reply.data must be NULL
117  * @param out The reply, as an allocated DATA_BLOB, caller to free.
118  * @return Errors or MORE_PROCESSING_REQUIRED if (normal) a reply is required. 
119  */
120
121 NTSTATUS ntlmssp_server_negotiate(struct gensec_security *gensec_security, 
122                                   TALLOC_CTX *out_mem_ctx, 
123                                   const DATA_BLOB in, DATA_BLOB *out) 
124 {
125         struct gensec_ntlmssp_state *gensec_ntlmssp_state = (struct gensec_ntlmssp_state *)gensec_security->private_data;
126         DATA_BLOB struct_blob;
127         uint32_t neg_flags = 0;
128         uint32_t ntlmssp_command, chal_flags;
129         const uint8_t *cryptkey;
130         const char *target_name;
131
132         /* parse the NTLMSSP packet */
133 #if 0
134         file_save("ntlmssp_negotiate.dat", request.data, request.length);
135 #endif
136
137         if (in.length) {
138                 if ((in.length < 16) || !msrpc_parse(out_mem_ctx, 
139                                  lp_iconv_convenience(gensec_security->lp_ctx),
140                                                          &in, "Cdd",
141                                                          "NTLMSSP",
142                                                          &ntlmssp_command,
143                                                          &neg_flags)) {
144                         DEBUG(1, ("ntlmssp_server_negotiate: failed to parse "
145                                 "NTLMSSP Negotiate of length %u:\n",
146                                 (unsigned int)in.length ));
147                         dump_data(2, in.data, in.length);
148                         return NT_STATUS_INVALID_PARAMETER;
149                 }
150                 debug_ntlmssp_flags(neg_flags);
151         }
152         
153         ntlmssp_handle_neg_flags(gensec_ntlmssp_state, neg_flags, gensec_ntlmssp_state->allow_lm_key);
154
155         /* Ask our caller what challenge they would like in the packet */
156         cryptkey = gensec_ntlmssp_state->get_challenge(gensec_ntlmssp_state);
157         if (!cryptkey) {
158                 DEBUG(1, ("ntlmssp_server_negotiate: backend doesn't give a challenge\n"));
159                 return NT_STATUS_INTERNAL_ERROR;
160         }
161
162         /* Check if we may set the challenge */
163         if (!gensec_ntlmssp_state->may_set_challenge(gensec_ntlmssp_state)) {
164                 gensec_ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
165         }
166
167         /* The flags we send back are not just the negotiated flags,
168          * they are also 'what is in this packet'.  Therfore, we
169          * operate on 'chal_flags' from here on 
170          */
171
172         chal_flags = gensec_ntlmssp_state->neg_flags;
173
174         /* get the right name to fill in as 'target' */
175         target_name = ntlmssp_target_name(gensec_ntlmssp_state, 
176                                           neg_flags, &chal_flags); 
177         if (target_name == NULL) 
178                 return NT_STATUS_INVALID_PARAMETER;
179
180         gensec_ntlmssp_state->chal = data_blob_talloc(gensec_ntlmssp_state, cryptkey, 8);
181         gensec_ntlmssp_state->internal_chal = data_blob_talloc(gensec_ntlmssp_state, cryptkey, 8);
182
183         /* This creates the 'blob' of names that appears at the end of the packet */
184         if (chal_flags & NTLMSSP_CHAL_TARGET_INFO) {
185                 char dnsdomname[MAXHOSTNAMELEN], dnsname[MAXHOSTNAMELEN];
186                 const char *target_name_dns = "";
187
188                 /* Find out the DNS domain name */
189                 dnsdomname[0] = '\0';
190                 safe_strcpy(dnsdomname, lp_realm(gensec_security->lp_ctx), sizeof(dnsdomname) - 1);
191                 strlower_m(dnsdomname);
192
193                 /* Find out the DNS host name */
194                 safe_strcpy(dnsname, gensec_ntlmssp_state->server_name, sizeof(dnsname) - 1);
195                 if (dnsdomname[0] != '\0') {
196                         safe_strcat(dnsname, ".", sizeof(dnsname) - 1);
197                         safe_strcat(dnsname, dnsdomname, sizeof(dnsname) - 1);
198                 }
199                 strlower_m(dnsname);
200
201                 if (chal_flags |= NTLMSSP_TARGET_TYPE_DOMAIN) {
202                         target_name_dns = dnsdomname;
203                 } else if (chal_flags |= NTLMSSP_TARGET_TYPE_SERVER) {
204                         target_name_dns = dnsname;
205                 }
206
207                 msrpc_gen(out_mem_ctx, 
208                           &struct_blob, "aaaaa",
209                           NTLMSSP_NAME_TYPE_DOMAIN, target_name,
210                           NTLMSSP_NAME_TYPE_SERVER, gensec_ntlmssp_state->server_name,
211                           NTLMSSP_NAME_TYPE_DOMAIN_DNS, dnsdomname,
212                           NTLMSSP_NAME_TYPE_SERVER_DNS, dnsname,
213                           0, "");
214         } else {
215                 struct_blob = data_blob(NULL, 0);
216         }
217
218         {
219                 /* Marshal the packet in the right format, be it unicode or ASCII */
220                 const char *gen_string;
221                 if (gensec_ntlmssp_state->unicode) {
222                         gen_string = "CdUdbddB";
223                 } else {
224                         gen_string = "CdAdbddB";
225                 }
226                 
227                 msrpc_gen(out_mem_ctx, 
228                           out, gen_string,
229                           "NTLMSSP", 
230                           NTLMSSP_CHALLENGE,
231                           target_name,
232                           chal_flags,
233                           cryptkey, 8,
234                           0, 0,
235                           struct_blob.data, struct_blob.length);
236         }
237                 
238         gensec_ntlmssp_state->expected_state = NTLMSSP_AUTH;
239
240         return NT_STATUS_MORE_PROCESSING_REQUIRED;
241 }
242
243 /**
244  * Next state function for the Authenticate packet
245  * 
246  * @param gensec_ntlmssp_state NTLMSSP State
247  * @param request The request, as a DATA_BLOB
248  * @return Errors or NT_STATUS_OK. 
249  */
250
251 static NTSTATUS ntlmssp_server_preauth(struct gensec_ntlmssp_state *gensec_ntlmssp_state,
252                                        const DATA_BLOB request) 
253 {
254         uint32_t ntlmssp_command, auth_flags;
255         NTSTATUS nt_status;
256
257         uint8_t session_nonce_hash[16];
258
259         const char *parse_string;
260         char *domain = NULL;
261         char *user = NULL;
262         char *workstation = NULL;
263
264 #if 0
265         file_save("ntlmssp_auth.dat", request.data, request.length);
266 #endif
267
268         if (gensec_ntlmssp_state->unicode) {
269                 parse_string = "CdBBUUUBd";
270         } else {
271                 parse_string = "CdBBAAABd";
272         }
273
274         /* zero these out */
275         data_blob_free(&gensec_ntlmssp_state->lm_resp);
276         data_blob_free(&gensec_ntlmssp_state->nt_resp);
277         data_blob_free(&gensec_ntlmssp_state->encrypted_session_key);
278
279         gensec_ntlmssp_state->user = NULL;
280         gensec_ntlmssp_state->domain = NULL;
281         gensec_ntlmssp_state->workstation = NULL;
282
283         /* now the NTLMSSP encoded auth hashes */
284         if (!msrpc_parse(gensec_ntlmssp_state, 
285                          lp_iconv_convenience(gensec_ntlmssp_state->gensec_security->lp_ctx),
286                          &request, parse_string,
287                          "NTLMSSP", 
288                          &ntlmssp_command, 
289                          &gensec_ntlmssp_state->lm_resp,
290                          &gensec_ntlmssp_state->nt_resp,
291                          &domain, 
292                          &user, 
293                          &workstation,
294                          &gensec_ntlmssp_state->encrypted_session_key,
295                          &auth_flags)) {
296                 DEBUG(10, ("ntlmssp_server_auth: failed to parse NTLMSSP (nonfatal):\n"));
297                 dump_data(10, request.data, request.length);
298
299                 /* zero this out */
300                 data_blob_free(&gensec_ntlmssp_state->encrypted_session_key);
301                 auth_flags = 0;
302                 
303                 /* Try again with a shorter string (Win9X truncates this packet) */
304                 if (gensec_ntlmssp_state->unicode) {
305                         parse_string = "CdBBUUU";
306                 } else {
307                         parse_string = "CdBBAAA";
308                 }
309
310                 /* now the NTLMSSP encoded auth hashes */
311                 if (!msrpc_parse(gensec_ntlmssp_state, 
312                                  lp_iconv_convenience(gensec_ntlmssp_state->gensec_security->lp_ctx),
313                                  &request, parse_string,
314                                  "NTLMSSP", 
315                                  &ntlmssp_command, 
316                                  &gensec_ntlmssp_state->lm_resp,
317                                  &gensec_ntlmssp_state->nt_resp,
318                                  &domain, 
319                                  &user, 
320                                  &workstation)) {
321                         DEBUG(1, ("ntlmssp_server_auth: failed to parse NTLMSSP:\n"));
322                         dump_data(2, request.data, request.length);
323
324                         return NT_STATUS_INVALID_PARAMETER;
325                 }
326         }
327
328         if (auth_flags)
329                 ntlmssp_handle_neg_flags(gensec_ntlmssp_state, auth_flags, gensec_ntlmssp_state->allow_lm_key);
330
331         if (!NT_STATUS_IS_OK(nt_status = ntlmssp_set_domain(gensec_ntlmssp_state, domain))) {
332                 /* zero this out */
333                 data_blob_free(&gensec_ntlmssp_state->encrypted_session_key);
334                 return nt_status;
335         }
336
337         if (!NT_STATUS_IS_OK(nt_status = ntlmssp_set_username(gensec_ntlmssp_state, user))) {
338                 /* zero this out */
339                 data_blob_free(&gensec_ntlmssp_state->encrypted_session_key);
340                 return nt_status;
341         }
342
343         if (!NT_STATUS_IS_OK(nt_status = ntlmssp_set_workstation(gensec_ntlmssp_state, workstation))) {
344                 /* zero this out */
345                 data_blob_free(&gensec_ntlmssp_state->encrypted_session_key);
346                 return nt_status;
347         }
348
349         DEBUG(3,("Got user=[%s] domain=[%s] workstation=[%s] len1=%lu len2=%lu\n",
350                  gensec_ntlmssp_state->user, gensec_ntlmssp_state->domain, gensec_ntlmssp_state->workstation, (unsigned long)gensec_ntlmssp_state->lm_resp.length, (unsigned long)gensec_ntlmssp_state->nt_resp.length));
351
352 #if 0
353         file_save("nthash1.dat",  &gensec_ntlmssp_state->nt_resp.data,  &gensec_ntlmssp_state->nt_resp.length);
354         file_save("lmhash1.dat",  &gensec_ntlmssp_state->lm_resp.data,  &gensec_ntlmssp_state->lm_resp.length);
355 #endif
356
357         /* NTLM2 uses a 'challenge' that is made of up both the server challenge, and a 
358            client challenge 
359         
360            However, the NTLM2 flag may still be set for the real NTLMv2 logins, be careful.
361         */
362         if (gensec_ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
363                 if (gensec_ntlmssp_state->nt_resp.length == 24 && gensec_ntlmssp_state->lm_resp.length == 24) {
364                         struct MD5Context md5_session_nonce_ctx;
365                         SMB_ASSERT(gensec_ntlmssp_state->internal_chal.data 
366                                    && gensec_ntlmssp_state->internal_chal.length == 8);
367                         
368                         gensec_ntlmssp_state->doing_ntlm2 = true;
369
370                         memcpy(gensec_ntlmssp_state->crypt.ntlm2.session_nonce, gensec_ntlmssp_state->internal_chal.data, 8);
371                         memcpy(&gensec_ntlmssp_state->crypt.ntlm2.session_nonce[8], gensec_ntlmssp_state->lm_resp.data, 8);
372                         
373                         MD5Init(&md5_session_nonce_ctx);
374                         MD5Update(&md5_session_nonce_ctx, gensec_ntlmssp_state->crypt.ntlm2.session_nonce, 16);
375                         MD5Final(session_nonce_hash, &md5_session_nonce_ctx);
376                         
377                         gensec_ntlmssp_state->chal = data_blob_talloc(gensec_ntlmssp_state, 
378                                                                session_nonce_hash, 8);
379
380                         /* LM response is no longer useful, zero it out */
381                         data_blob_free(&gensec_ntlmssp_state->lm_resp);
382
383                         /* We changed the effective challenge - set it */
384                         if (!NT_STATUS_IS_OK(nt_status = 
385                                              gensec_ntlmssp_state->set_challenge(gensec_ntlmssp_state, 
386                                                                                  &gensec_ntlmssp_state->chal))) {
387                                 /* zero this out */
388                                 data_blob_free(&gensec_ntlmssp_state->encrypted_session_key);
389                                 return nt_status;
390                         }
391
392                         /* LM Key is incompatible... */
393                         gensec_ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
394                 }
395         }
396         return NT_STATUS_OK;
397 }
398
399 /**
400  * Next state function for the Authenticate packet 
401  * (after authentication - figures out the session keys etc)
402  * 
403  * @param gensec_ntlmssp_state NTLMSSP State
404  * @return Errors or NT_STATUS_OK. 
405  */
406
407 static NTSTATUS ntlmssp_server_postauth(struct gensec_security *gensec_security, 
408                                         DATA_BLOB *user_session_key, 
409                                         DATA_BLOB *lm_session_key) 
410 {
411         struct gensec_ntlmssp_state *gensec_ntlmssp_state = (struct gensec_ntlmssp_state *)gensec_security->private_data;
412         NTSTATUS nt_status;
413         DATA_BLOB session_key = data_blob(NULL, 0);
414
415         if (user_session_key)
416                 dump_data_pw("USER session key:\n", user_session_key->data, user_session_key->length);
417
418         if (lm_session_key) 
419                 dump_data_pw("LM first-8:\n", lm_session_key->data, lm_session_key->length);
420
421         /* Handle the different session key derivation for NTLM2 */
422         if (gensec_ntlmssp_state->doing_ntlm2) {
423                 if (user_session_key && user_session_key->data && user_session_key->length == 16) {
424                         session_key = data_blob_talloc(gensec_ntlmssp_state, NULL, 16);
425                         hmac_md5(user_session_key->data, gensec_ntlmssp_state->crypt.ntlm2.session_nonce, 
426                                  sizeof(gensec_ntlmssp_state->crypt.ntlm2.session_nonce), session_key.data);
427                         DEBUG(10,("ntlmssp_server_auth: Created NTLM2 session key.\n"));
428                         dump_data_pw("NTLM2 session key:\n", session_key.data, session_key.length);
429                         
430                 } else {
431                         DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM2 session key.\n"));
432                         session_key = data_blob(NULL, 0);
433                 }
434         } else if ((gensec_ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY) 
435                 /* Ensure we can never get here on NTLMv2 */
436                 && (gensec_ntlmssp_state->nt_resp.length == 0 || gensec_ntlmssp_state->nt_resp.length == 24)) {
437
438                 if (lm_session_key && lm_session_key->data && lm_session_key->length >= 8) {
439                         if (gensec_ntlmssp_state->lm_resp.data && gensec_ntlmssp_state->lm_resp.length == 24) {
440                                 session_key = data_blob_talloc(gensec_ntlmssp_state, NULL, 16);
441                                 SMBsesskeygen_lm_sess_key(lm_session_key->data, gensec_ntlmssp_state->lm_resp.data, 
442                                                           session_key.data);
443                                 DEBUG(10,("ntlmssp_server_auth: Created NTLM session key.\n"));
444                                 dump_data_pw("LM session key:\n", session_key.data, session_key.length);
445                         } else {
446                                 
447                                 /* When there is no LM response, just use zeros */
448                                 static const uint8_t zeros[24];
449                                 session_key = data_blob_talloc(gensec_ntlmssp_state, NULL, 16);
450                                 SMBsesskeygen_lm_sess_key(zeros, zeros, 
451                                                           session_key.data);
452                                 DEBUG(10,("ntlmssp_server_auth: Created NTLM session key.\n"));
453                                 dump_data_pw("LM session key:\n", session_key.data, session_key.length);
454                         }
455                 } else {
456                         /* LM Key not selected */
457                         gensec_ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
458
459                         DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM session key.\n"));
460                         session_key = data_blob(NULL, 0);
461                 }
462
463         } else if (user_session_key && user_session_key->data) {
464                 session_key = *user_session_key;
465                 DEBUG(10,("ntlmssp_server_auth: Using unmodified nt session key.\n"));
466                 dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
467
468                 /* LM Key not selected */
469                 gensec_ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
470
471         } else if (lm_session_key && lm_session_key->data) {
472                 /* Very weird to have LM key, but no user session key, but anyway.. */
473                 session_key = *lm_session_key;
474                 DEBUG(10,("ntlmssp_server_auth: Using unmodified lm session key.\n"));
475                 dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
476
477                 /* LM Key not selected */
478                 gensec_ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
479
480         } else {
481                 DEBUG(10,("ntlmssp_server_auth: Failed to create unmodified session key.\n"));
482                 session_key = data_blob(NULL, 0);
483
484                 /* LM Key not selected */
485                 gensec_ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
486         }
487
488         /* With KEY_EXCH, the client supplies the proposed session key, 
489            but encrypts it with the long-term key */
490         if (gensec_ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
491                 if (!gensec_ntlmssp_state->encrypted_session_key.data 
492                     || gensec_ntlmssp_state->encrypted_session_key.length != 16) {
493                         data_blob_free(&gensec_ntlmssp_state->encrypted_session_key);
494                         DEBUG(1, ("Client-supplied KEY_EXCH session key was of invalid length (%u)!\n", 
495                                   (unsigned)gensec_ntlmssp_state->encrypted_session_key.length));
496                         return NT_STATUS_INVALID_PARAMETER;
497                 } else if (!session_key.data || session_key.length != 16) {
498                         DEBUG(5, ("server session key is invalid (len == %u), cannot do KEY_EXCH!\n", 
499                                   (unsigned)session_key.length));
500                         gensec_ntlmssp_state->session_key = session_key;
501                 } else {
502                         dump_data_pw("KEY_EXCH session key (enc):\n", 
503                                      gensec_ntlmssp_state->encrypted_session_key.data, 
504                                      gensec_ntlmssp_state->encrypted_session_key.length);
505                         arcfour_crypt(gensec_ntlmssp_state->encrypted_session_key.data, 
506                                       session_key.data, 
507                                       gensec_ntlmssp_state->encrypted_session_key.length);
508                         gensec_ntlmssp_state->session_key = data_blob_talloc(gensec_ntlmssp_state, 
509                                                                       gensec_ntlmssp_state->encrypted_session_key.data, 
510                                                                       gensec_ntlmssp_state->encrypted_session_key.length);
511                         dump_data_pw("KEY_EXCH session key:\n", gensec_ntlmssp_state->encrypted_session_key.data, 
512                                      gensec_ntlmssp_state->encrypted_session_key.length);
513                 }
514         } else {
515                 gensec_ntlmssp_state->session_key = session_key;
516         }
517
518         /* keep the session key around on the new context */
519         talloc_steal(gensec_ntlmssp_state, session_key.data);
520
521         if ((gensec_security->want_features & GENSEC_FEATURE_SIGN)
522             || (gensec_security->want_features & GENSEC_FEATURE_SEAL)) {
523                 nt_status = ntlmssp_sign_init(gensec_ntlmssp_state);
524         } else {
525                 nt_status = NT_STATUS_OK;
526         }
527
528         data_blob_free(&gensec_ntlmssp_state->encrypted_session_key);
529         
530         /* allow arbitarily many authentications, but watch that this will cause a 
531            memory leak, until the gensec_ntlmssp_state is shutdown 
532         */
533
534         if (gensec_ntlmssp_state->server_multiple_authentications) {
535                 gensec_ntlmssp_state->expected_state = NTLMSSP_AUTH;
536         } else {
537                 gensec_ntlmssp_state->expected_state = NTLMSSP_DONE;
538         }
539
540         return nt_status;
541 }
542
543
544 /**
545  * Next state function for the Authenticate packet
546  * 
547  * @param gensec_security GENSEC state
548  * @param out_mem_ctx Memory context for *out
549  * @param in The request, as a DATA_BLOB.  reply.data must be NULL
550  * @param out The reply, as an allocated DATA_BLOB, caller to free.
551  * @return Errors or NT_STATUS_OK if authentication sucessful
552  */
553
554 NTSTATUS ntlmssp_server_auth(struct gensec_security *gensec_security, 
555                              TALLOC_CTX *out_mem_ctx, 
556                              const DATA_BLOB in, DATA_BLOB *out) 
557 {       
558         struct gensec_ntlmssp_state *gensec_ntlmssp_state = (struct gensec_ntlmssp_state *)gensec_security->private_data;
559         DATA_BLOB user_session_key = data_blob(NULL, 0);
560         DATA_BLOB lm_session_key = data_blob(NULL, 0);
561         NTSTATUS nt_status;
562
563         TALLOC_CTX *mem_ctx = talloc_new(out_mem_ctx);
564         if (!mem_ctx) {
565                 return NT_STATUS_NO_MEMORY;
566         }
567
568         /* zero the outbound NTLMSSP packet */
569         *out = data_blob_talloc(out_mem_ctx, NULL, 0);
570
571         if (!NT_STATUS_IS_OK(nt_status = ntlmssp_server_preauth(gensec_ntlmssp_state, in))) {
572                 talloc_free(mem_ctx);
573                 return nt_status;
574         }
575
576         /*
577          * Note we don't check here for NTLMv2 auth settings. If NTLMv2 auth
578          * is required (by "ntlm auth = no" and "lm auth = no" being set in the
579          * smb.conf file) and no NTLMv2 response was sent then the password check
580          * will fail here. JRA.
581          */
582
583         /* Finally, actually ask if the password is OK */
584
585         if (!NT_STATUS_IS_OK(nt_status = gensec_ntlmssp_state->check_password(gensec_ntlmssp_state, mem_ctx,
586                                                                               &user_session_key, &lm_session_key))) {
587                 talloc_free(mem_ctx);
588                 return nt_status;
589         }
590         
591         if (gensec_security->want_features
592             & (GENSEC_FEATURE_SIGN|GENSEC_FEATURE_SEAL|GENSEC_FEATURE_SESSION_KEY)) {
593                 nt_status = ntlmssp_server_postauth(gensec_security, &user_session_key, &lm_session_key);
594                 talloc_free(mem_ctx);
595                 return nt_status;
596         } else {
597                 gensec_ntlmssp_state->session_key = data_blob(NULL, 0);
598                 talloc_free(mem_ctx);
599                 return NT_STATUS_OK;
600         }
601 }
602
603 /**
604  * Return the challenge as determined by the authentication subsystem 
605  * @return an 8 byte random challenge
606  */
607
608 static const uint8_t *auth_ntlmssp_get_challenge(const struct gensec_ntlmssp_state *gensec_ntlmssp_state)
609 {
610         NTSTATUS status;
611         const uint8_t *chal;
612
613         status = auth_get_challenge(gensec_ntlmssp_state->auth_context, &chal);
614         if (!NT_STATUS_IS_OK(status)) {
615                 DEBUG(1, ("auth_ntlmssp_get_challenge: failed to get challenge: %s\n",
616                         nt_errstr(status)));
617                 return NULL;
618         }
619
620         return chal;
621 }
622
623 /**
624  * Some authentication methods 'fix' the challenge, so we may not be able to set it
625  *
626  * @return If the effective challenge used by the auth subsystem may be modified
627  */
628 static bool auth_ntlmssp_may_set_challenge(const struct gensec_ntlmssp_state *gensec_ntlmssp_state)
629 {
630         return auth_challenge_may_be_modified(gensec_ntlmssp_state->auth_context);
631 }
632
633 /**
634  * NTLM2 authentication modifies the effective challenge, 
635  * @param challenge The new challenge value
636  */
637 static NTSTATUS auth_ntlmssp_set_challenge(struct gensec_ntlmssp_state *gensec_ntlmssp_state, DATA_BLOB *challenge)
638 {
639         NTSTATUS nt_status;
640         struct auth_context *auth_context = gensec_ntlmssp_state->auth_context;
641         const uint8_t *chal;
642
643         if (challenge->length != 8) {
644                 return NT_STATUS_INVALID_PARAMETER;
645         }
646
647         chal = challenge->data;
648
649         nt_status = auth_context_set_challenge(auth_context, chal, "NTLMSSP callback (NTLM2)");
650
651         return nt_status;
652 }
653
654 /**
655  * Check the password on an NTLMSSP login.  
656  *
657  * Return the session keys used on the connection.
658  */
659
660 static NTSTATUS auth_ntlmssp_check_password(struct gensec_ntlmssp_state *gensec_ntlmssp_state, 
661                                             TALLOC_CTX *mem_ctx, 
662                                             DATA_BLOB *user_session_key, DATA_BLOB *lm_session_key) 
663 {
664         NTSTATUS nt_status;
665         struct auth_usersupplied_info *user_info = talloc(mem_ctx, struct auth_usersupplied_info);
666         if (!user_info) {
667                 return NT_STATUS_NO_MEMORY;
668         }
669
670         user_info->logon_parameters = MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT | MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT;
671         user_info->flags = 0;
672         user_info->mapped_state = false;
673         user_info->client.account_name = gensec_ntlmssp_state->user;
674         user_info->client.domain_name = gensec_ntlmssp_state->domain;
675         user_info->workstation_name = gensec_ntlmssp_state->workstation;
676         user_info->remote_host = gensec_get_peer_addr(gensec_ntlmssp_state->gensec_security);
677
678         user_info->password_state = AUTH_PASSWORD_RESPONSE;
679         user_info->password.response.lanman = gensec_ntlmssp_state->lm_resp;
680         user_info->password.response.lanman.data = talloc_steal(user_info, gensec_ntlmssp_state->lm_resp.data);
681         user_info->password.response.nt = gensec_ntlmssp_state->nt_resp;
682         user_info->password.response.nt.data = talloc_steal(user_info, gensec_ntlmssp_state->nt_resp.data);
683
684         nt_status = auth_check_password(gensec_ntlmssp_state->auth_context, mem_ctx,
685                                         user_info, &gensec_ntlmssp_state->server_info);
686         talloc_free(user_info);
687         NT_STATUS_NOT_OK_RETURN(nt_status);
688
689         talloc_steal(gensec_ntlmssp_state, gensec_ntlmssp_state->server_info);
690
691         if (gensec_ntlmssp_state->server_info->user_session_key.length) {
692                 DEBUG(10, ("Got NT session key of length %u\n", 
693                            (unsigned)gensec_ntlmssp_state->server_info->user_session_key.length));
694                 if (!talloc_reference(mem_ctx, gensec_ntlmssp_state->server_info->user_session_key.data)) {
695                         return NT_STATUS_NO_MEMORY;
696                 }
697
698                 *user_session_key = gensec_ntlmssp_state->server_info->user_session_key;
699         }
700         if (gensec_ntlmssp_state->server_info->lm_session_key.length) {
701                 DEBUG(10, ("Got LM session key of length %u\n", 
702                            (unsigned)gensec_ntlmssp_state->server_info->lm_session_key.length));
703                 if (!talloc_reference(mem_ctx, gensec_ntlmssp_state->server_info->lm_session_key.data)) {
704                         return NT_STATUS_NO_MEMORY;
705                 }
706
707                 *lm_session_key = gensec_ntlmssp_state->server_info->lm_session_key;
708         }
709         return nt_status;
710 }
711
712 /** 
713  * Return the credentials of a logged on user, including session keys
714  * etc.
715  *
716  * Only valid after a successful authentication
717  *
718  * May only be called once per authentication.
719  *
720  */
721
722 NTSTATUS gensec_ntlmssp_session_info(struct gensec_security *gensec_security,
723                                      struct auth_session_info **session_info) 
724 {
725         NTSTATUS nt_status;
726         struct gensec_ntlmssp_state *gensec_ntlmssp_state = (struct gensec_ntlmssp_state *)gensec_security->private_data;
727
728         nt_status = auth_generate_session_info(gensec_ntlmssp_state, gensec_security->event_ctx, gensec_security->lp_ctx, gensec_ntlmssp_state->server_info, session_info);
729         NT_STATUS_NOT_OK_RETURN(nt_status);
730
731         (*session_info)->session_key = data_blob_talloc(*session_info, 
732                                                         gensec_ntlmssp_state->session_key.data,
733                                                         gensec_ntlmssp_state->session_key.length);
734
735         return NT_STATUS_OK;
736 }
737
738 /**
739  * Start NTLMSSP on the server side 
740  *
741  */
742 NTSTATUS gensec_ntlmssp_server_start(struct gensec_security *gensec_security)
743 {
744         NTSTATUS nt_status;
745         struct gensec_ntlmssp_state *gensec_ntlmssp_state;
746
747         nt_status = gensec_ntlmssp_start(gensec_security);
748         NT_STATUS_NOT_OK_RETURN(nt_status);
749
750         gensec_ntlmssp_state = (struct gensec_ntlmssp_state *)gensec_security->private_data;
751
752         gensec_ntlmssp_state->role = NTLMSSP_SERVER;
753
754         gensec_ntlmssp_state->workstation = NULL;
755         gensec_ntlmssp_state->server_name = lp_netbios_name(gensec_security->lp_ctx);
756
757         gensec_ntlmssp_state->domain = lp_workgroup(gensec_security->lp_ctx);
758
759         gensec_ntlmssp_state->expected_state = NTLMSSP_NEGOTIATE;
760
761         gensec_ntlmssp_state->allow_lm_key = (lp_lanman_auth(gensec_security->lp_ctx) 
762                                           && lp_parm_bool(gensec_security->lp_ctx, NULL, "ntlmssp_server", "allow_lm_key", false));
763
764         gensec_ntlmssp_state->server_multiple_authentications = false;
765         
766         gensec_ntlmssp_state->neg_flags = 
767                 NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_UNKNOWN_02000000;
768
769         gensec_ntlmssp_state->lm_resp = data_blob(NULL, 0);
770         gensec_ntlmssp_state->nt_resp = data_blob(NULL, 0);
771         gensec_ntlmssp_state->encrypted_session_key = data_blob(NULL, 0);
772
773         if (lp_parm_bool(gensec_security->lp_ctx, NULL, "ntlmssp_server", "128bit", true)) {
774                 gensec_ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_128;               
775         }
776
777         if (lp_parm_bool(gensec_security->lp_ctx, NULL, "ntlmssp_server", "56bit", true)) {
778                 gensec_ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_56;                
779         }
780
781         if (lp_parm_bool(gensec_security->lp_ctx, NULL, "ntlmssp_server", "keyexchange", true)) {
782                 gensec_ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_KEY_EXCH;          
783         }
784
785         if (lp_parm_bool(gensec_security->lp_ctx, NULL, "ntlmssp_server", "alwayssign", true)) {
786                 gensec_ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_ALWAYS_SIGN;               
787         }
788
789         if (lp_parm_bool(gensec_security->lp_ctx, NULL, "ntlmssp_server", "ntlm2", true)) {
790                 gensec_ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_NTLM2;             
791         }
792
793         if (gensec_security->want_features & GENSEC_FEATURE_SIGN) {
794                 gensec_ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
795         }
796         if (gensec_security->want_features & GENSEC_FEATURE_SEAL) {
797                 gensec_ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
798         }
799
800         nt_status = auth_context_create(gensec_ntlmssp_state, 
801                                         gensec_security->event_ctx,
802                                         gensec_security->msg_ctx,
803                                         gensec_security->lp_ctx,
804                                         &gensec_ntlmssp_state->auth_context);
805         NT_STATUS_NOT_OK_RETURN(nt_status);
806
807         gensec_ntlmssp_state->get_challenge = auth_ntlmssp_get_challenge;
808         gensec_ntlmssp_state->may_set_challenge = auth_ntlmssp_may_set_challenge;
809         gensec_ntlmssp_state->set_challenge = auth_ntlmssp_set_challenge;
810         gensec_ntlmssp_state->check_password = auth_ntlmssp_check_password;
811         gensec_ntlmssp_state->server_role = lp_server_role(gensec_security->lp_ctx);
812
813         return NT_STATUS_OK;
814 }
815