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