r3462: separate out the crypto includes
[abartlet/samba.git/.git] / source4 / auth / ntlm_check.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Password and authentication handling
4    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2001-2004
5    Copyright (C) Gerald Carter                             2003
6    Copyright (C) Luke Kenneth Casson Leighton         1996-2000
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "lib/crypto/crypto.h"
25
26 #undef DBGC_CLASS
27 #define DBGC_CLASS DBGC_AUTH
28
29 /****************************************************************************
30  Core of smb password checking routine.
31 ****************************************************************************/
32
33 static BOOL smb_pwd_check_ntlmv1(TALLOC_CTX *mem_ctx,
34                                  const DATA_BLOB *nt_response,
35                                  const uint8_t *part_passwd,
36                                  const DATA_BLOB *sec_blob,
37                                  DATA_BLOB *user_sess_key)
38 {
39         /* Finish the encryption of part_passwd. */
40         uint8_t p24[24];
41         
42         if (part_passwd == NULL) {
43                 DEBUG(10,("No password set - DISALLOWING access\n"));
44                 /* No password set - always false ! */
45                 return False;
46         }
47         
48         if (sec_blob->length != 8) {
49                 DEBUG(0, ("smb_pwd_check_ntlmv1: incorrect challenge size (%lu)\n", 
50                           (unsigned long)sec_blob->length));
51                 return False;
52         }
53         
54         if (nt_response->length != 24) {
55                 DEBUG(0, ("smb_pwd_check_ntlmv1: incorrect password length (%lu)\n", 
56                           (unsigned long)nt_response->length));
57                 return False;
58         }
59
60         SMBOWFencrypt(part_passwd, sec_blob->data, p24);
61         if (user_sess_key != NULL) {
62                 *user_sess_key = data_blob_talloc(mem_ctx, NULL, 16);
63                 SMBsesskeygen_ntv1(part_passwd, user_sess_key->data);
64         }
65         
66         
67 #if DEBUG_PASSWORD
68         DEBUG(100,("Part password (P16) was |\n"));
69         dump_data(100, part_passwd, 16);
70         DEBUGADD(100,("Password from client was |\n"));
71         dump_data(100, nt_response->data, nt_response->length);
72         DEBUGADD(100,("Given challenge was |\n"));
73         dump_data(100, sec_blob->data, sec_blob->length);
74         DEBUGADD(100,("Value from encryption was |\n"));
75         dump_data(100, p24, 24);
76 #endif
77         return (memcmp(p24, nt_response->data, 24) == 0);
78 }
79
80 /****************************************************************************
81  Core of smb password checking routine. (NTLMv2, LMv2)
82  Note:  The same code works with both NTLMv2 and LMv2.
83 ****************************************************************************/
84
85 static BOOL smb_pwd_check_ntlmv2(TALLOC_CTX *mem_ctx,
86                                  const DATA_BLOB *ntv2_response,
87                                  const uint8_t *part_passwd,
88                                  const DATA_BLOB *sec_blob,
89                                  const char *user, const char *domain,
90                                  BOOL upper_case_domain, /* should the domain be transformed into upper case? */
91                                  DATA_BLOB *user_sess_key)
92 {
93         /* Finish the encryption of part_passwd. */
94         uint8_t kr[16];
95         uint8_t value_from_encryption[16];
96         uint8_t client_response[16];
97         DATA_BLOB client_key_data;
98
99         if (part_passwd == NULL) {
100                 DEBUG(10,("No password set - DISALLOWING access\n"));
101                 /* No password set - always False */
102                 return False;
103         }
104
105         if (sec_blob->length != 8) {
106                 DEBUG(0, ("smb_pwd_check_ntlmv2: incorrect challenge size (%lu)\n", 
107                           (unsigned long)sec_blob->length));
108                 return False;
109         }
110         
111         if (ntv2_response->length < 24) {
112                 /* We MUST have more than 16 bytes, or the stuff below will go
113                    crazy.  No known implementation sends less than the 24 bytes
114                    for LMv2, let alone NTLMv2. */
115                 DEBUG(0, ("smb_pwd_check_ntlmv2: incorrect password length (%lu)\n", 
116                           (unsigned long)ntv2_response->length));
117                 return False;
118         }
119
120         client_key_data = data_blob_talloc(mem_ctx, ntv2_response->data+16, ntv2_response->length-16);
121         /* 
122            todo:  should we be checking this for anything?  We can't for LMv2, 
123            but for NTLMv2 it is meant to contain the current time etc.
124         */
125
126         memcpy(client_response, ntv2_response->data, sizeof(client_response));
127
128         if (!ntv2_owf_gen(part_passwd, user, domain, upper_case_domain, kr)) {
129                 return False;
130         }
131
132         SMBOWFencrypt_ntv2(kr, sec_blob, &client_key_data, value_from_encryption);
133         if (user_sess_key != NULL) {
134                 *user_sess_key = data_blob_talloc(mem_ctx, NULL, 16);
135                 SMBsesskeygen_ntv2(kr, value_from_encryption, user_sess_key->data);
136         }
137
138 #if DEBUG_PASSWORD
139         DEBUG(100,("Part password (P16) was |\n"));
140         dump_data(100, part_passwd, 16);
141         DEBUGADD(100,("Password from client was |\n"));
142         dump_data(100, ntv2_response->data, ntv2_response->length);
143         DEBUGADD(100,("Variable data from client was |\n"));
144         dump_data(100, client_key_data.data, client_key_data.length);
145         DEBUGADD(100,("Given challenge was |\n"));
146         dump_data(100, sec_blob->data, sec_blob->length);
147         DEBUGADD(100,("Value from encryption was |\n"));
148         dump_data(100, value_from_encryption, 16);
149 #endif
150         data_blob_clear_free(&client_key_data);
151         return (memcmp(value_from_encryption, client_response, 16) == 0);
152 }
153
154 /**
155  * Check a challenge-response password against the value of the NT or
156  * LM password hash.
157  *
158  * @param mem_ctx talloc context
159  * @param challenge 8-byte challenge.  If all zero, forces plaintext comparison
160  * @param nt_response 'unicode' NT response to the challenge, or unicode password
161  * @param lm_response ASCII or LANMAN response to the challenge, or password in DOS code page
162  * @param username internal Samba username, for log messages
163  * @param client_username username the client used
164  * @param client_domain domain name the client used (may be mapped)
165  * @param nt_pw MD4 unicode password from our passdb or similar
166  * @param lm_pw LANMAN ASCII password from our passdb or similar
167  * @param user_sess_key User session key
168  * @param lm_sess_key LM session key (first 8 bytes of the LM hash)
169  */
170
171 NTSTATUS ntlm_password_check(TALLOC_CTX *mem_ctx,
172                              const DATA_BLOB *challenge,
173                              const DATA_BLOB *lm_response,
174                              const DATA_BLOB *nt_response,
175                              const DATA_BLOB *lm_interactive_password,
176                              const DATA_BLOB *nt_interactive_password,
177                              const char *username, 
178                              const char *client_username, 
179                              const char *client_domain,
180                              const uint8_t *lm_pw, const uint8_t *nt_pw, 
181                              DATA_BLOB *user_sess_key, 
182                              DATA_BLOB *lm_sess_key)
183 {
184         static const uint8_t zeros[8];
185         if (nt_pw == NULL) {
186                 DEBUG(3,("ntlm_password_check: NO NT password stored for user %s.\n", 
187                          username));
188         }
189
190         if (nt_interactive_password && nt_interactive_password->length && nt_pw) { 
191                 if (nt_interactive_password->length != 16) {
192                         DEBUG(3,("ntlm_password_check: Interactive logon: Invalid NT password length (%d) supplied for user %s\n", (int)nt_interactive_password->length,
193                                  username));
194                         return NT_STATUS_WRONG_PASSWORD;
195                 }
196
197                 if (memcmp(nt_interactive_password->data, nt_pw, 16) == 0) {
198                         if (user_sess_key) {
199                                 *user_sess_key = data_blob_talloc(mem_ctx, NULL, 16);
200                                 SMBsesskeygen_ntv1(nt_pw, user_sess_key->data);
201                         }
202                         return NT_STATUS_OK;
203                 } else {
204                         DEBUG(3,("ntlm_password_check: Interactive logon: NT password check failed for user %s\n",
205                                  username));
206                         return NT_STATUS_WRONG_PASSWORD;
207                 }
208
209         } else if (lm_interactive_password && lm_interactive_password->length && lm_pw) { 
210                 if (lm_interactive_password->length != 16) {
211                         DEBUG(3,("ntlm_password_check: Interactive logon: Invalid LANMAN password length (%d) supplied for user %s\n", (int)lm_interactive_password->length,
212                                  username));
213                         return NT_STATUS_WRONG_PASSWORD;
214                 }
215
216                 if (!lp_lanman_auth()) {
217                         DEBUG(3,("ntlm_password_check: Interactive logon: only LANMAN password supplied for user %s, and LM passwords are disabled!\n",
218                                  username));
219                         return NT_STATUS_WRONG_PASSWORD;
220                 }
221
222                 if (memcmp(lm_interactive_password->data, lm_pw, 16) == 0) {
223                         return NT_STATUS_OK;
224                 } else {
225                         DEBUG(3,("ntlm_password_check: Interactive logon: LANMAN password check failed for user %s\n",
226                                  username));
227                         return NT_STATUS_WRONG_PASSWORD;
228                 }
229         }
230
231         /* Check for cleartext netlogon. Used by Exchange 5.5. */
232         if (challenge->length == sizeof(zeros) && 
233             (memcmp(challenge->data, zeros, challenge->length) == 0 )) {
234
235                 DEBUG(4,("ntlm_password_check: checking plaintext passwords for user %s\n",
236                          username));
237                 if (nt_pw && nt_response->length) {
238                         uint8_t pwhash[16];
239                         mdfour(pwhash, nt_response->data, nt_response->length);
240                         if (memcmp(pwhash, nt_pw, sizeof(pwhash)) == 0) {
241                                 return NT_STATUS_OK;
242                         } else {
243                                 DEBUG(3,("ntlm_password_check: NT (Unicode) plaintext password check failed for user %s\n",
244                                          username));
245                                 return NT_STATUS_WRONG_PASSWORD;
246                         }
247
248                 } else if (!lp_lanman_auth()) {
249                         DEBUG(3,("ntlm_password_check: (plaintext password check) LANMAN passwords NOT PERMITTED for user %s\n",
250                                  username));
251
252                 } else if (lm_pw && lm_response->length) {
253                         uint8_t dospwd[14]; 
254                         uint8_t p16[16]; 
255                         ZERO_STRUCT(dospwd);
256                         
257                         memcpy(dospwd, lm_response->data, MIN(lm_response->length, sizeof(dospwd)));
258                         /* Only the fisrt 14 chars are considered, password need not be null terminated. */
259
260                         /* we *might* need to upper-case the string here */
261                         E_P16((const uint8_t *)dospwd, p16);
262
263                         if (memcmp(p16, lm_pw, sizeof(p16)) == 0) {
264                                 return NT_STATUS_OK;
265                         } else {
266                                 DEBUG(3,("ntlm_password_check: LANMAN (ASCII) plaintext password check failed for user %s\n",
267                                          username));
268                                 return NT_STATUS_WRONG_PASSWORD;
269                         }
270                 } else {
271                         DEBUG(3, ("Plaintext authentication for user %s attempted, but neither NT nor LM passwords available\n", username));
272                         return NT_STATUS_WRONG_PASSWORD;
273                 }
274         }
275
276         if (nt_response->length != 0 && nt_response->length < 24) {
277                 DEBUG(2,("ntlm_password_check: invalid NT password length (%lu) for user %s\n", 
278                          (unsigned long)nt_response->length, username));                
279         }
280         
281         if (nt_response->length >= 24 && nt_pw) {
282                 if (nt_response->length > 24) {
283                         /* We have the NT MD4 hash challenge available - see if we can
284                            use it 
285                         */
286                         DEBUG(4,("ntlm_password_check: Checking NTLMv2 password with domain [%s]\n", client_domain));
287                         if (smb_pwd_check_ntlmv2(mem_ctx,
288                                                  nt_response, 
289                                                  nt_pw, challenge, 
290                                                  client_username, 
291                                                  client_domain,
292                                                  False,
293                                                  user_sess_key)) {
294                                 return NT_STATUS_OK;
295                         }
296                         
297                         DEBUG(4,("ntlm_password_check: Checking NTLMv2 password with uppercased version of domain [%s]\n", client_domain));
298                         if (smb_pwd_check_ntlmv2(mem_ctx,
299                                                  nt_response, 
300                                                  nt_pw, challenge, 
301                                                  client_username, 
302                                                  client_domain,
303                                                  True,
304                                                  user_sess_key)) {
305                                 return NT_STATUS_OK;
306                         }
307                         
308                         DEBUG(4,("ntlm_password_check: Checking NTLMv2 password without a domain\n"));
309                         if (smb_pwd_check_ntlmv2(mem_ctx,
310                                                  nt_response, 
311                                                  nt_pw, challenge, 
312                                                  client_username, 
313                                                  "",
314                                                  False,
315                                                  user_sess_key)) {
316                                 return NT_STATUS_OK;
317                         } else {
318                                 DEBUG(3,("ntlm_password_check: NTLMv2 password check failed\n"));
319                                 return NT_STATUS_WRONG_PASSWORD;
320                         }
321                 }
322
323                 if (lp_ntlm_auth()) {           
324                         /* We have the NT MD4 hash challenge available - see if we can
325                            use it (ie. does it exist in the smbpasswd file).
326                         */
327                         DEBUG(4,("ntlm_password_check: Checking NT MD4 password\n"));
328                         if (smb_pwd_check_ntlmv1(mem_ctx, 
329                                                  nt_response, 
330                                                  nt_pw, challenge,
331                                                  user_sess_key)) {
332                                 /* The LM session key for this response is not very secure, 
333                                    so use it only if we otherwise allow LM authentication */
334
335                                 if (lp_lanman_auth() && lm_pw) {
336                                         *lm_sess_key = data_blob_talloc(mem_ctx, lm_pw, 8);
337                                 }
338                                 return NT_STATUS_OK;
339                         } else {
340                                 DEBUG(3,("ntlm_password_check: NT MD4 password check failed for user %s\n",
341                                          username));
342                                 return NT_STATUS_WRONG_PASSWORD;
343                         }
344                 } else {
345                         DEBUG(2,("ntlm_password_check: NTLMv1 passwords NOT PERMITTED for user %s\n",
346                                  username));                    
347                         /* no return, becouse we might pick up LMv2 in the LM field */
348                 }
349         }
350         
351         if (lm_response->length == 0) {
352                 DEBUG(3,("ntlm_password_check: NEITHER LanMan nor NT password supplied for user %s\n",
353                          username));
354                 return NT_STATUS_WRONG_PASSWORD;
355         }
356         
357         if (lm_response->length < 24) {
358                 DEBUG(2,("ntlm_password_check: invalid LanMan password length (%lu) for user %s\n", 
359                          (unsigned long)nt_response->length, username));                
360                 return NT_STATUS_WRONG_PASSWORD;
361         }
362                 
363         if (!lp_lanman_auth()) {
364                 DEBUG(3,("ntlm_password_check: Lanman passwords NOT PERMITTED for user %s\n",
365                          username));
366         } else if (!lm_pw) {
367                 DEBUG(3,("ntlm_password_check: NO LanMan password set for user %s (and no NT password supplied)\n",
368                          username));
369         } else {
370                 DEBUG(4,("ntlm_password_check: Checking LM password\n"));
371                 if (smb_pwd_check_ntlmv1(mem_ctx,
372                                          lm_response, 
373                                          lm_pw, challenge,
374                                          NULL)) {
375                         /* The session key for this response is still very odd.  
376                            It not very secure, so use it only if we otherwise 
377                            allow LM authentication */
378
379                         if (lp_lanman_auth() && lm_pw) {
380                                 uint8_t first_8_lm_hash[16];
381                                 memcpy(first_8_lm_hash, lm_pw, 8);
382                                 memset(first_8_lm_hash + 8, '\0', 8);
383                                 *user_sess_key = data_blob_talloc(mem_ctx, first_8_lm_hash, 16);
384                                 *lm_sess_key = data_blob_talloc(mem_ctx, lm_pw, 8);
385                         }
386                         return NT_STATUS_OK;
387                 }
388         }
389         
390         if (!nt_pw) {
391                 DEBUG(4,("ntlm_password_check: LM password check failed for user, no NT password %s\n",username));
392                 return NT_STATUS_WRONG_PASSWORD;
393         }
394         
395         /* This is for 'LMv2' authentication.  almost NTLMv2 but limited to 24 bytes.
396            - related to Win9X, legacy NAS pass-though authentication
397         */
398         DEBUG(4,("ntlm_password_check: Checking LMv2 password with domain %s\n", client_domain));
399         if (smb_pwd_check_ntlmv2(mem_ctx,
400                                  lm_response, 
401                                  nt_pw, challenge, 
402                                  client_username,
403                                  client_domain,
404                                  False,
405                                  NULL)) {
406                 return NT_STATUS_OK;
407         }
408         
409         DEBUG(4,("ntlm_password_check: Checking LMv2 password with upper-cased version of domain %s\n", client_domain));
410         if (smb_pwd_check_ntlmv2(mem_ctx,
411                                  lm_response, 
412                                  nt_pw, challenge, 
413                                  client_username,
414                                  client_domain,
415                                  True,
416                                  NULL)) {
417                 return NT_STATUS_OK;
418         }
419         
420         DEBUG(4,("ntlm_password_check: Checking LMv2 password without a domain\n"));
421         if (smb_pwd_check_ntlmv2(mem_ctx,
422                                  lm_response, 
423                                  nt_pw, challenge, 
424                                  client_username,
425                                  "",
426                                  False,
427                                  NULL)) {
428                 return NT_STATUS_OK;
429         }
430
431         /* Apparently NT accepts NT responses in the LM field
432            - I think this is related to Win9X pass-though authentication
433         */
434         DEBUG(4,("ntlm_password_check: Checking NT MD4 password in LM field\n"));
435         if (lp_ntlm_auth()) {
436                 if (smb_pwd_check_ntlmv1(mem_ctx, 
437                                          lm_response, 
438                                          nt_pw, challenge,
439                                          NULL)) {
440                         /* The session key for this response is still very odd.  
441                            It not very secure, so use it only if we otherwise 
442                            allow LM authentication */
443
444                         if (lp_lanman_auth() && lm_pw) {
445                                 uint8_t first_8_lm_hash[16];
446                                 memcpy(first_8_lm_hash, lm_pw, 8);
447                                 memset(first_8_lm_hash + 8, '\0', 8);
448                                 *user_sess_key = data_blob_talloc(mem_ctx, first_8_lm_hash, 16);
449                                 *lm_sess_key = data_blob_talloc(mem_ctx, lm_pw, 8);
450                         }
451                         return NT_STATUS_OK;
452                 }
453                 DEBUG(3,("ntlm_password_check: LM password, NT MD4 password in LM field and LMv2 failed for user %s\n",username));
454         } else {
455                 DEBUG(3,("ntlm_password_check: LM password and LMv2 failed for user %s, and NT MD4 password in LM field not permitted\n",username));
456         }
457         return NT_STATUS_WRONG_PASSWORD;
458 }
459