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