r13252: Cleanup, both in code, comments and talloc use:
[gd/samba-autobuild/.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 2 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, write to the Free Software
22    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25 #include "includes.h"
26 #include "auth/auth.h"
27 #include "auth/ntlmssp/ntlmssp.h"
28 #include "lib/crypto/crypto.h"
29 #include "pstring.h"
30 #include "system/filesys.h"
31
32 /** 
33  * Set a username on an NTLMSSP context - ensures it is talloc()ed 
34  *
35  */
36
37 static NTSTATUS ntlmssp_set_username(struct gensec_ntlmssp_state *gensec_ntlmssp_state, const char *user) 
38 {
39         if (!user) {
40                 /* it should be at least "" */
41                 DEBUG(1, ("NTLMSSP failed to set username - cannot accept NULL username\n"));
42                 return NT_STATUS_INVALID_PARAMETER;
43         }
44         gensec_ntlmssp_state->user = talloc_strdup(gensec_ntlmssp_state, user);
45         if (!gensec_ntlmssp_state->user) {
46                 return NT_STATUS_NO_MEMORY;
47         }
48         return NT_STATUS_OK;
49 }
50
51 /** 
52  * Set a domain on an NTLMSSP context - ensures it is talloc()ed 
53  *
54  */
55 static NTSTATUS ntlmssp_set_domain(struct gensec_ntlmssp_state *gensec_ntlmssp_state, const char *domain) 
56 {
57         gensec_ntlmssp_state->domain = talloc_strdup(gensec_ntlmssp_state, domain);
58         if (!gensec_ntlmssp_state->domain) {
59                 return NT_STATUS_NO_MEMORY;
60         }
61         return NT_STATUS_OK;
62 }
63
64 /** 
65  * Set a workstation on an NTLMSSP context - ensures it is talloc()ed 
66  *
67  */
68 static NTSTATUS ntlmssp_set_workstation(struct gensec_ntlmssp_state *gensec_ntlmssp_state, const char *workstation) 
69 {
70         gensec_ntlmssp_state->workstation = talloc_strdup(gensec_ntlmssp_state, workstation);
71         if (!gensec_ntlmssp_state->workstation) {
72                 return NT_STATUS_NO_MEMORY;
73         }
74         return NT_STATUS_OK;
75 }
76
77 /**
78  * Determine correct target name flags for reply, given server role 
79  * and negotiated flags
80  * 
81  * @param gensec_ntlmssp_state NTLMSSP State
82  * @param neg_flags The flags from the packet
83  * @param chal_flags The flags to be set in the reply packet
84  * @return The 'target name' string.
85  */
86
87 static const char *ntlmssp_target_name(struct gensec_ntlmssp_state *gensec_ntlmssp_state,
88                                        uint32_t neg_flags, uint32_t *chal_flags) 
89 {
90         if (neg_flags & NTLMSSP_REQUEST_TARGET) {
91                 *chal_flags |= NTLMSSP_CHAL_TARGET_INFO;
92                 *chal_flags |= NTLMSSP_REQUEST_TARGET;
93                 if (gensec_ntlmssp_state->server_role == ROLE_STANDALONE) {
94                         *chal_flags |= NTLMSSP_TARGET_TYPE_SERVER;
95                         return gensec_ntlmssp_state->server_name;
96                 } else {
97                         *chal_flags |= NTLMSSP_TARGET_TYPE_DOMAIN;
98                         return gensec_ntlmssp_state->get_domain();
99                 };
100         } else {
101                 return "";
102         }
103 }
104
105 /*
106   Andrew, please remove these totally bogus calls when you get time
107 */
108 static BOOL get_myfullname(char *my_name)
109 {
110         pstring hostname;
111
112         *hostname = 0;
113
114         /* get my host name */
115         if (gethostname(hostname, sizeof(hostname)) == -1) {
116                 DEBUG(0,("gethostname failed\n"));
117                 return False;
118         } 
119
120         /* Ensure null termination. */
121         hostname[sizeof(hostname)-1] = '\0';
122
123         if (my_name)
124                 fstrcpy(my_name, hostname);
125         return True;
126 }
127
128 static BOOL get_mydomname(char *my_domname)
129 {
130         pstring hostname;
131         char *p;
132
133         /* arrgh! relies on full name in system */
134
135         *hostname = 0;
136         /* get my host name */
137         if (gethostname(hostname, sizeof(hostname)) == -1) {
138                 DEBUG(0,("gethostname failed\n"));
139                 return False;
140         } 
141
142         /* Ensure null termination. */
143         hostname[sizeof(hostname)-1] = '\0';
144
145         p = strchr_m(hostname, '.');
146
147         if (!p)
148                 return False;
149
150         p++;
151         
152         if (my_domname)
153                 fstrcpy(my_domname, p);
154
155         return True;
156 }
157
158
159
160 /**
161  * Next state function for the Negotiate packet
162  * 
163  * @param gensec_security GENSEC state
164  * @param out_mem_ctx Memory context for *out
165  * @param in The request, as a DATA_BLOB.  reply.data must be NULL
166  * @param out The reply, as an allocated DATA_BLOB, caller to free.
167  * @return Errors or MORE_PROCESSING_REQUIRED if (normal) a reply is required. 
168  */
169
170 NTSTATUS ntlmssp_server_negotiate(struct gensec_security *gensec_security, 
171                                   TALLOC_CTX *out_mem_ctx, 
172                                   const DATA_BLOB in, DATA_BLOB *out) 
173 {
174         struct gensec_ntlmssp_state *gensec_ntlmssp_state = gensec_security->private_data;
175         DATA_BLOB struct_blob;
176         fstring dnsname, dnsdomname;
177         uint32_t neg_flags = 0;
178         uint32_t ntlmssp_command, chal_flags;
179         const uint8_t *cryptkey;
180         const char *target_name;
181
182         /* parse the NTLMSSP packet */
183 #if 0
184         file_save("ntlmssp_negotiate.dat", request.data, request.length);
185 #endif
186
187         if (in.length) {
188                 if ((in.length < 16) || !msrpc_parse(out_mem_ctx, &in, "Cdd",
189                                                          "NTLMSSP",
190                                                          &ntlmssp_command,
191                                                          &neg_flags)) {
192                         DEBUG(1, ("ntlmssp_server_negotiate: failed to parse "
193                                 "NTLMSSP Negotiate of length %u:\n",
194                                 (unsigned int)in.length ));
195                         dump_data(2, in.data, in.length);
196                         return NT_STATUS_INVALID_PARAMETER;
197                 }
198                 debug_ntlmssp_flags(neg_flags);
199         }
200         
201         ntlmssp_handle_neg_flags(gensec_ntlmssp_state, neg_flags, gensec_ntlmssp_state->allow_lm_key);
202
203         /* Ask our caller what challenge they would like in the packet */
204         cryptkey = gensec_ntlmssp_state->get_challenge(gensec_ntlmssp_state);
205
206         /* Check if we may set the challenge */
207         if (!gensec_ntlmssp_state->may_set_challenge(gensec_ntlmssp_state)) {
208                 gensec_ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_NTLM2;
209         }
210
211         /* The flags we send back are not just the negotiated flags,
212          * they are also 'what is in this packet'.  Therfore, we
213          * operate on 'chal_flags' from here on 
214          */
215
216         chal_flags = gensec_ntlmssp_state->neg_flags;
217
218         /* get the right name to fill in as 'target' */
219         target_name = ntlmssp_target_name(gensec_ntlmssp_state, 
220                                           neg_flags, &chal_flags); 
221         if (target_name == NULL) 
222                 return NT_STATUS_INVALID_PARAMETER;
223
224         gensec_ntlmssp_state->chal = data_blob_talloc(gensec_ntlmssp_state, cryptkey, 8);
225         gensec_ntlmssp_state->internal_chal = data_blob_talloc(gensec_ntlmssp_state, cryptkey, 8);
226
227         /* This should be a 'netbios domain -> DNS domain' mapping */
228         dnsdomname[0] = '\0';
229         get_mydomname(dnsdomname);
230         strlower_m(dnsdomname);
231         
232         dnsname[0] = '\0';
233         get_myfullname(dnsname);
234         
235         /* This creates the 'blob' of names that appears at the end of the packet */
236         if (chal_flags & NTLMSSP_CHAL_TARGET_INFO) 
237         {
238                 const char *target_name_dns = "";
239                 if (chal_flags |= NTLMSSP_TARGET_TYPE_DOMAIN) {
240                         target_name_dns = dnsdomname;
241                 } else if (chal_flags |= NTLMSSP_TARGET_TYPE_SERVER) {
242                         target_name_dns = dnsname;
243                 }
244
245                 msrpc_gen(out_mem_ctx, 
246                           &struct_blob, "aaaaa",
247                           NTLMSSP_NAME_TYPE_DOMAIN, target_name,
248                           NTLMSSP_NAME_TYPE_SERVER, gensec_ntlmssp_state->server_name,
249                           NTLMSSP_NAME_TYPE_DOMAIN_DNS, dnsdomname,
250                           NTLMSSP_NAME_TYPE_SERVER_DNS, dnsname,
251                           0, "");
252         } else {
253                 struct_blob = data_blob(NULL, 0);
254         }
255
256         {
257                 /* Marshel the packet in the right format, be it unicode or ASCII */
258                 const char *gen_string;
259                 if (gensec_ntlmssp_state->unicode) {
260                         gen_string = "CdUdbddB";
261                 } else {
262                         gen_string = "CdAdbddB";
263                 }
264                 
265                 msrpc_gen(out_mem_ctx, 
266                           out, gen_string,
267                           "NTLMSSP", 
268                           NTLMSSP_CHALLENGE,
269                           target_name,
270                           chal_flags,
271                           cryptkey, 8,
272                           0, 0,
273                           struct_blob.data, struct_blob.length);
274         }
275                 
276         gensec_ntlmssp_state->expected_state = NTLMSSP_AUTH;
277
278         return NT_STATUS_MORE_PROCESSING_REQUIRED;
279 }
280
281 /**
282  * Next state function for the Authenticate packet
283  * 
284  * @param gensec_ntlmssp_state NTLMSSP State
285  * @param request The request, as a DATA_BLOB
286  * @return Errors or NT_STATUS_OK. 
287  */
288
289 static NTSTATUS ntlmssp_server_preauth(struct gensec_ntlmssp_state *gensec_ntlmssp_state,
290                                        const DATA_BLOB request) 
291 {
292         uint32_t ntlmssp_command, auth_flags;
293         NTSTATUS nt_status;
294
295         uint8_t session_nonce_hash[16];
296
297         const char *parse_string;
298         char *domain = NULL;
299         char *user = NULL;
300         char *workstation = NULL;
301
302 #if 0
303         file_save("ntlmssp_auth.dat", request.data, request.length);
304 #endif
305
306         if (gensec_ntlmssp_state->unicode) {
307                 parse_string = "CdBBUUUBd";
308         } else {
309                 parse_string = "CdBBAAABd";
310         }
311
312         /* zero these out */
313         data_blob_free(&gensec_ntlmssp_state->lm_resp);
314         data_blob_free(&gensec_ntlmssp_state->nt_resp);
315         data_blob_free(&gensec_ntlmssp_state->encrypted_session_key);
316
317         gensec_ntlmssp_state->user = NULL;
318         gensec_ntlmssp_state->domain = NULL;
319         gensec_ntlmssp_state->workstation = NULL;
320
321         /* now the NTLMSSP encoded auth hashes */
322         if (!msrpc_parse(gensec_ntlmssp_state, 
323                          &request, parse_string,
324                          "NTLMSSP", 
325                          &ntlmssp_command, 
326                          &gensec_ntlmssp_state->lm_resp,
327                          &gensec_ntlmssp_state->nt_resp,
328                          &domain, 
329                          &user, 
330                          &workstation,
331                          &gensec_ntlmssp_state->encrypted_session_key,
332                          &auth_flags)) {
333                 DEBUG(10, ("ntlmssp_server_auth: failed to parse NTLMSSP (nonfatal):\n"));
334                 dump_data(10, request.data, request.length);
335
336                 /* zero this out */
337                 data_blob_free(&gensec_ntlmssp_state->encrypted_session_key);
338                 auth_flags = 0;
339                 
340                 /* Try again with a shorter string (Win9X truncates this packet) */
341                 if (gensec_ntlmssp_state->unicode) {
342                         parse_string = "CdBBUUU";
343                 } else {
344                         parse_string = "CdBBAAA";
345                 }
346
347                 /* now the NTLMSSP encoded auth hashes */
348                 if (!msrpc_parse(gensec_ntlmssp_state, 
349                                  &request, parse_string,
350                                  "NTLMSSP", 
351                                  &ntlmssp_command, 
352                                  &gensec_ntlmssp_state->lm_resp,
353                                  &gensec_ntlmssp_state->nt_resp,
354                                  &domain, 
355                                  &user, 
356                                  &workstation)) {
357                         DEBUG(1, ("ntlmssp_server_auth: failed to parse NTLMSSP:\n"));
358                         dump_data(2, request.data, request.length);
359
360                         return NT_STATUS_INVALID_PARAMETER;
361                 }
362         }
363
364         if (auth_flags)
365                 ntlmssp_handle_neg_flags(gensec_ntlmssp_state, auth_flags, gensec_ntlmssp_state->allow_lm_key);
366
367         if (!NT_STATUS_IS_OK(nt_status = ntlmssp_set_domain(gensec_ntlmssp_state, domain))) {
368                 /* zero this out */
369                 data_blob_free(&gensec_ntlmssp_state->encrypted_session_key);
370                 return nt_status;
371         }
372
373         if (!NT_STATUS_IS_OK(nt_status = ntlmssp_set_username(gensec_ntlmssp_state, user))) {
374                 /* zero this out */
375                 data_blob_free(&gensec_ntlmssp_state->encrypted_session_key);
376                 return nt_status;
377         }
378
379         if (!NT_STATUS_IS_OK(nt_status = ntlmssp_set_workstation(gensec_ntlmssp_state, workstation))) {
380                 /* zero this out */
381                 data_blob_free(&gensec_ntlmssp_state->encrypted_session_key);
382                 return nt_status;
383         }
384
385         DEBUG(3,("Got user=[%s] domain=[%s] workstation=[%s] len1=%lu len2=%lu\n",
386                  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));
387
388 #if 0
389         file_save("nthash1.dat",  &gensec_ntlmssp_state->nt_resp.data,  &gensec_ntlmssp_state->nt_resp.length);
390         file_save("lmhash1.dat",  &gensec_ntlmssp_state->lm_resp.data,  &gensec_ntlmssp_state->lm_resp.length);
391 #endif
392
393         /* NTLM2 uses a 'challenge' that is made of up both the server challenge, and a 
394            client challenge 
395         
396            However, the NTLM2 flag may still be set for the real NTLMv2 logins, be careful.
397         */
398         if (gensec_ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_NTLM2) {
399                 if (gensec_ntlmssp_state->nt_resp.length == 24 && gensec_ntlmssp_state->lm_resp.length == 24) {
400                         struct MD5Context md5_session_nonce_ctx;
401                         SMB_ASSERT(gensec_ntlmssp_state->internal_chal.data 
402                                    && gensec_ntlmssp_state->internal_chal.length == 8);
403                         
404                         gensec_ntlmssp_state->doing_ntlm2 = True;
405
406                         memcpy(gensec_ntlmssp_state->crypt.ntlm2.session_nonce, gensec_ntlmssp_state->internal_chal.data, 8);
407                         memcpy(&gensec_ntlmssp_state->crypt.ntlm2.session_nonce[8], gensec_ntlmssp_state->lm_resp.data, 8);
408                         
409                         MD5Init(&md5_session_nonce_ctx);
410                         MD5Update(&md5_session_nonce_ctx, gensec_ntlmssp_state->crypt.ntlm2.session_nonce, 16);
411                         MD5Final(session_nonce_hash, &md5_session_nonce_ctx);
412                         
413                         gensec_ntlmssp_state->chal = data_blob_talloc(gensec_ntlmssp_state, 
414                                                                session_nonce_hash, 8);
415
416                         /* LM response is no longer useful, zero it out */
417                         data_blob_free(&gensec_ntlmssp_state->lm_resp);
418
419                         /* We changed the effective challenge - set it */
420                         if (!NT_STATUS_IS_OK(nt_status = 
421                                              gensec_ntlmssp_state->set_challenge(gensec_ntlmssp_state, 
422                                                                                  &gensec_ntlmssp_state->chal))) {
423                                 /* zero this out */
424                                 data_blob_free(&gensec_ntlmssp_state->encrypted_session_key);
425                                 return nt_status;
426                         }
427
428                         /* LM Key is incompatible... */
429                         gensec_ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
430                 }
431         }
432         return NT_STATUS_OK;
433 }
434
435 /**
436  * Next state function for the Authenticate packet 
437  * (after authentication - figures out the session keys etc)
438  * 
439  * @param gensec_ntlmssp_state NTLMSSP State
440  * @return Errors or NT_STATUS_OK. 
441  */
442
443 static NTSTATUS ntlmssp_server_postauth(struct gensec_security *gensec_security, 
444                                         DATA_BLOB *user_session_key, 
445                                         DATA_BLOB *lm_session_key) 
446 {
447         struct gensec_ntlmssp_state *gensec_ntlmssp_state = gensec_security->private_data;
448         NTSTATUS nt_status;
449         DATA_BLOB session_key = data_blob(NULL, 0);
450
451         if (user_session_key)
452                 dump_data_pw("USER session key:\n", user_session_key->data, user_session_key->length);
453
454         if (lm_session_key) 
455                 dump_data_pw("LM first-8:\n", lm_session_key->data, lm_session_key->length);
456
457         /* Handle the different session key derivation for NTLM2 */
458         if (gensec_ntlmssp_state->doing_ntlm2) {
459                 if (user_session_key && user_session_key->data && user_session_key->length == 16) {
460                         session_key = data_blob_talloc(gensec_ntlmssp_state, NULL, 16);
461                         hmac_md5(user_session_key->data, gensec_ntlmssp_state->crypt.ntlm2.session_nonce, 
462                                  sizeof(gensec_ntlmssp_state->crypt.ntlm2.session_nonce), session_key.data);
463                         DEBUG(10,("ntlmssp_server_auth: Created NTLM2 session key.\n"));
464                         dump_data_pw("NTLM2 session key:\n", session_key.data, session_key.length);
465                         
466                 } else {
467                         DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM2 session key.\n"));
468                         session_key = data_blob(NULL, 0);
469                 }
470         } else if ((gensec_ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_LM_KEY) 
471                 /* Ensure we can never get here on NTLMv2 */
472                 && (gensec_ntlmssp_state->nt_resp.length == 0 || gensec_ntlmssp_state->nt_resp.length == 24)) {
473
474                 if (lm_session_key && lm_session_key->data && lm_session_key->length >= 8) {
475                         if (gensec_ntlmssp_state->lm_resp.data && gensec_ntlmssp_state->lm_resp.length == 24) {
476                                 session_key = data_blob_talloc(gensec_ntlmssp_state, NULL, 16);
477                                 SMBsesskeygen_lm_sess_key(lm_session_key->data, gensec_ntlmssp_state->lm_resp.data, 
478                                                           session_key.data);
479                                 DEBUG(10,("ntlmssp_server_auth: Created NTLM session key.\n"));
480                                 dump_data_pw("LM session key:\n", session_key.data, session_key.length);
481                         } else {
482                                 
483                                 /* When there is no LM response, just use zeros */
484                                 static const uint8_t zeros[24];
485                                 session_key = data_blob_talloc(gensec_ntlmssp_state, NULL, 16);
486                                 SMBsesskeygen_lm_sess_key(zeros, zeros, 
487                                                           session_key.data);
488                                 DEBUG(10,("ntlmssp_server_auth: Created NTLM session key.\n"));
489                                 dump_data_pw("LM session key:\n", session_key.data, session_key.length);
490                         }
491                 } else {
492                         /* LM Key not selected */
493                         gensec_ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
494
495                         DEBUG(10,("ntlmssp_server_auth: Failed to create NTLM session key.\n"));
496                         session_key = data_blob(NULL, 0);
497                 }
498
499         } else if (user_session_key && user_session_key->data) {
500                 session_key = *user_session_key;
501                 DEBUG(10,("ntlmssp_server_auth: Using unmodified nt session key.\n"));
502                 dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
503
504                 /* LM Key not selected */
505                 gensec_ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
506
507         } else if (lm_session_key && lm_session_key->data) {
508                 /* Very weird to have LM key, but no user session key, but anyway.. */
509                 session_key = *lm_session_key;
510                 DEBUG(10,("ntlmssp_server_auth: Using unmodified lm session key.\n"));
511                 dump_data_pw("unmodified session key:\n", session_key.data, session_key.length);
512
513                 /* LM Key not selected */
514                 gensec_ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
515
516         } else {
517                 DEBUG(10,("ntlmssp_server_auth: Failed to create unmodified session key.\n"));
518                 session_key = data_blob(NULL, 0);
519
520                 /* LM Key not selected */
521                 gensec_ntlmssp_state->neg_flags &= ~NTLMSSP_NEGOTIATE_LM_KEY;
522         }
523
524         /* With KEY_EXCH, the client supplies the proposed session key, 
525            but encrypts it with the long-term key */
526         if (gensec_ntlmssp_state->neg_flags & NTLMSSP_NEGOTIATE_KEY_EXCH) {
527                 if (!gensec_ntlmssp_state->encrypted_session_key.data 
528                     || gensec_ntlmssp_state->encrypted_session_key.length != 16) {
529                         data_blob_free(&gensec_ntlmssp_state->encrypted_session_key);
530                         DEBUG(1, ("Client-supplied KEY_EXCH session key was of invalid length (%u)!\n", 
531                                   (unsigned)gensec_ntlmssp_state->encrypted_session_key.length));
532                         return NT_STATUS_INVALID_PARAMETER;
533                 } else if (!session_key.data || session_key.length != 16) {
534                         DEBUG(5, ("server session key is invalid (len == %u), cannot do KEY_EXCH!\n", 
535                                   (unsigned)session_key.length));
536                         gensec_ntlmssp_state->session_key = session_key;
537                 } else {
538                         dump_data_pw("KEY_EXCH session key (enc):\n", 
539                                      gensec_ntlmssp_state->encrypted_session_key.data, 
540                                      gensec_ntlmssp_state->encrypted_session_key.length);
541                         arcfour_crypt(gensec_ntlmssp_state->encrypted_session_key.data, 
542                                       session_key.data, 
543                                       gensec_ntlmssp_state->encrypted_session_key.length);
544                         gensec_ntlmssp_state->session_key = data_blob_talloc(gensec_ntlmssp_state, 
545                                                                       gensec_ntlmssp_state->encrypted_session_key.data, 
546                                                                       gensec_ntlmssp_state->encrypted_session_key.length);
547                         dump_data_pw("KEY_EXCH session key:\n", gensec_ntlmssp_state->encrypted_session_key.data, 
548                                      gensec_ntlmssp_state->encrypted_session_key.length);
549                 }
550         } else {
551                 gensec_ntlmssp_state->session_key = session_key;
552         }
553
554         /* keep the session key around on the new context */
555         talloc_steal(gensec_ntlmssp_state, session_key.data);
556
557         /* The server might need us to use a partial-strength session key */
558         ntlmssp_weaken_keys(gensec_ntlmssp_state);
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 = 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 = 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 = 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;
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", "keyexchange", True)) {
815                 gensec_ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_KEY_EXCH;          
816         }
817
818         if (lp_parm_bool(-1, "ntlmssp_server", "ntlm2", True)) {
819                 gensec_ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_NTLM2;             
820         }
821
822         if (gensec_security->want_features & GENSEC_FEATURE_SIGN) {
823                 gensec_ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SIGN;
824         }
825         if (gensec_security->want_features & GENSEC_FEATURE_SEAL) {
826                 gensec_ntlmssp_state->neg_flags |= NTLMSSP_NEGOTIATE_SEAL;
827         }
828
829         nt_status = auth_context_create(gensec_ntlmssp_state, lp_auth_methods(), 
830                                         &gensec_ntlmssp_state->auth_context,
831                                         gensec_security->event_ctx);
832         NT_STATUS_NOT_OK_RETURN(nt_status);
833
834         gensec_ntlmssp_state->get_challenge = auth_ntlmssp_get_challenge;
835         gensec_ntlmssp_state->may_set_challenge = auth_ntlmssp_may_set_challenge;
836         gensec_ntlmssp_state->set_challenge = auth_ntlmssp_set_challenge;
837         gensec_ntlmssp_state->check_password = auth_ntlmssp_check_password;
838         gensec_ntlmssp_state->server_role = lp_server_role();
839
840         return NT_STATUS_OK;
841 }
842