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