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