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