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