debug statement fixups.
[tprouty/samba.git] / source / auth / auth_sam.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    Password and authentication handling
5    Copyright (C) Andrew Tridgell              1992-2000
6    Copyright (C) Luke Kenneth Casson Leighton 1996-2000
7    Copyright (C) Andrew Bartlett              2001
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 /****************************************************************************
27 core of smb password checking routine.
28 ****************************************************************************/
29 static BOOL smb_pwd_check_ntlmv1(DATA_BLOB nt_response,
30                                 const uchar *part_passwd,
31                                 DATA_BLOB sec_blob,
32                                 uint8 user_sess_key[16])
33 {
34         /* Finish the encryption of part_passwd. */
35         uchar p24[24];
36         
37         if (part_passwd == NULL) {
38                 DEBUG(10,("No password set - DISALLOWING access\n"));
39                 /* No password set - always false ! */
40                 return False;
41         }
42         
43         if (sec_blob.length != 8) {
44                 DEBUG(0, ("smb_pwd_check_ntlmv1: incorrect challenge size (%d)\n", sec_blob.length));
45                 return False;
46         }
47         
48         if (nt_response.length != 24) {
49                 DEBUG(0, ("smb_pwd_check_ntlmv1: incorrect password length (%d)\n", nt_response.length));
50                 return False;
51         }
52
53         SMBOWFencrypt(part_passwd, sec_blob.data, p24);
54         if (user_sess_key != NULL)
55         {
56                 SMBsesskeygen_ntv1(part_passwd, NULL, user_sess_key);
57         }
58         
59         
60         
61 #if DEBUG_PASSWORD
62         DEBUG(100,("Part password (P16) was |"));
63         dump_data(100, part_passwd, 16);
64         DEBUG(100,("Password from client was |"));
65         dump_data(100, nt_response.data, nt_response.length);
66         DEBUG(100,("Given challenge was |"));
67         dump_data(100, sec_blob.data, sec_blob.length);
68         DEBUG(100,("Value from encryption was |"));
69         dump_data(100, p24, 24);
70 #endif
71   return (memcmp(p24, nt_response.data, 24) == 0);
72 }
73
74 /****************************************************************************
75 core of smb password checking routine.
76 ****************************************************************************/
77 static BOOL smb_pwd_check_ntlmv2(const DATA_BLOB ntv2_response,
78                                 const uchar *part_passwd,
79                                 const DATA_BLOB sec_blob,
80                                 const char *user, const char *domain,
81                                 uint8 user_sess_key[16])
82 {
83         /* Finish the encryption of part_passwd. */
84         uchar kr[16];
85         uchar value_from_encryption[16];
86         uchar client_response[16];
87         DATA_BLOB client_key_data;
88
89         if (part_passwd == NULL)
90         {
91                 DEBUG(10,("No password set - DISALLOWING access\n"));
92                 /* No password set - always False */
93                 return False;
94         }
95
96         if (ntv2_response.length < 16) {
97                 /* We MUST have more than 16 bytes, or the stuff below will go
98                    crazy... */
99                 DEBUG(0, ("smb_pwd_check_ntlmv2: incorrect password length (%d)\n", 
100                           ntv2_response.length));
101                 return False;
102         }
103
104         client_key_data = data_blob(ntv2_response.data+16, ntv2_response.length-16);
105         memcpy(client_response, ntv2_response.data, sizeof(client_response));
106
107         ntv2_owf_gen(part_passwd, user, domain, kr);
108         SMBOWFencrypt_ntv2(kr, sec_blob, client_key_data, (char *)value_from_encryption);
109         if (user_sess_key != NULL)
110         {
111                 SMBsesskeygen_ntv2(kr, value_from_encryption, user_sess_key);
112         }
113
114 #if DEBUG_PASSWORD
115         DEBUG(100,("Part password (P16) was |"));
116         dump_data(100, part_passwd, 16);
117         DEBUG(100,("Password from client was |"));
118         dump_data(100, ntv2_response.data, ntv2_response.length);
119         DEBUG(100,("Variable data from client was |"));
120         dump_data(100, client_key_data.data, client_key_data.length);
121         DEBUG(100,("Given challenge was |"));
122         dump_data(100, sec_blob.data, sec_blob.length);
123         DEBUG(100,("Value from encryption was |"));
124         dump_data(100, value_from_encryption, 16);
125 #endif
126         data_blob_clear_free(&client_key_data);
127         return (memcmp(value_from_encryption, client_response, 16) == 0);
128 }
129
130
131 /****************************************************************************
132  Do a specific test for an smb password being correct, given a smb_password and
133  the lanman and NT responses.
134 ****************************************************************************/
135 static NTSTATUS sam_password_ok(TALLOC_CTX *mem_ctx,
136                                 SAM_ACCOUNT *sampass, 
137                                 const auth_usersupplied_info *user_info, 
138                                 const auth_authsupplied_info *auth_info,
139                                 uint8 user_sess_key[16])
140 {
141         uint16 acct_ctrl;
142         const uint8 *nt_pw, *lm_pw;
143         uint32 ntlmssp_flags;
144
145         acct_ctrl = pdb_get_acct_ctrl(sampass);
146         if (acct_ctrl & ACB_PWNOTREQ) 
147         {
148                 if (lp_null_passwords()) 
149                 {
150                         DEBUG(3,("Account for user '%s' has no password and null passwords are allowed.\n", pdb_get_username(sampass)));
151                         return(NT_STATUS_OK);
152                 } 
153                 else 
154                 {
155                         DEBUG(3,("Account for user '%s' has no password and null passwords are NOT allowed.\n", pdb_get_username(sampass)));
156                         return(NT_STATUS_LOGON_FAILURE);
157                 }               
158         }
159
160         nt_pw = pdb_get_nt_passwd(sampass);
161         lm_pw = pdb_get_lanman_passwd(sampass);
162         
163         ntlmssp_flags = user_info->ntlmssp_flags;
164
165         if (nt_pw == NULL) {
166                 DEBUG(3,("sam_password_ok: NO NT password stored for user %s.\n", 
167                          pdb_get_username(sampass)));
168                 /* No return, we want to check the LM hash below in this case */
169                 ntlmssp_flags &= (~(NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_NTLM2));
170         }
171         
172         if (ntlmssp_flags & NTLMSSP_NEGOTIATE_NTLM2) {
173                 /* We have the NT MD4 hash challenge available - see if we can
174                    use it (ie. does it exist in the smbpasswd file).
175                 */
176                 DEBUG(4,("sam_password_ok: Checking NTLMv2 password\n"));
177                 if (smb_pwd_check_ntlmv2( user_info->nt_resp, 
178                                           nt_pw, auth_info->challenge, 
179                                           user_info->smb_name.str, 
180                                           user_info->client_domain.str,
181                                           user_sess_key))
182                 {
183                         return NT_STATUS_OK;
184                 } else {
185                         DEBUG(3,("sam_password_ok: NTLMv2 password check failed\n"));
186                         return NT_STATUS_WRONG_PASSWORD;
187                 }
188         } else if (ntlmssp_flags & NTLMSSP_NEGOTIATE_NTLM) {
189                 if (lp_ntlm_auth()) {                           
190                         /* We have the NT MD4 hash challenge available - see if we can
191                            use it (ie. does it exist in the smbpasswd file).
192                         */
193                         DEBUG(4,("sam_password_ok: Checking NT MD4 password\n"));
194                         if (smb_pwd_check_ntlmv1(user_info->nt_resp, 
195                                                  nt_pw, auth_info->challenge,
196                                                  user_sess_key)) 
197                         {
198                                 return NT_STATUS_OK;
199                         } else {
200                                 DEBUG(3,("sam_password_ok: NT MD4 password check failed for user %s\n",pdb_get_username(sampass)));
201                                 return NT_STATUS_WRONG_PASSWORD;
202                         }
203                 } else {
204                         DEBUG(2,("sam_password_ok: NTLMv1 passwords NOT PERMITTED for user %s\n",pdb_get_username(sampass)));                   
205                                 /* No return, we want to check the LM hash below in this case */
206                 }
207         }
208         
209         if (lm_pw == NULL) {
210                 DEBUG(3,("sam_password_ok: NO LanMan password set for user %s (and no NT password supplied)\n",pdb_get_username(sampass)));
211                 ntlmssp_flags &= (~NTLMSSP_NEGOTIATE_OEM);              
212         }
213         
214         if (ntlmssp_flags & NTLMSSP_NEGOTIATE_OEM) {
215                 
216                 if (user_info->lm_resp.length != 24) {
217                         DEBUG(2,("sam_password_ok: invalid LanMan password length (%d) for user %s\n", 
218                                  user_info->nt_resp.length, pdb_get_username(sampass)));                
219                 }
220                 
221                 if (!lp_lanman_auth()) {
222                         DEBUG(3,("sam_password_ok: Lanman passwords NOT PERMITTED for user %s\n",pdb_get_username(sampass)));                   
223                         return NT_STATUS_LOGON_FAILURE;
224                 }
225                 
226                 DEBUG(4,("sam_password_ok: Checking LM password\n"));
227                 if (smb_pwd_check_ntlmv1(user_info->lm_resp, 
228                                          lm_pw, auth_info->challenge,
229                                          user_sess_key)) 
230                 {
231                         return NT_STATUS_OK;
232                 } else {
233                         DEBUG(4,("sam_password_ok: LM password check failed for user %s\n",pdb_get_username(sampass)));
234                         return NT_STATUS_WRONG_PASSWORD;
235                 } 
236         }
237
238         /* Should not be reached, but if they send nothing... */
239         DEBUG(3,("sam_password_ok: NEITHER LanMan nor NT password supplied for user %s\n",pdb_get_username(sampass)));
240         return NT_STATUS_WRONG_PASSWORD;
241 }
242
243 /****************************************************************************
244  Do a specific test for a SAM_ACCOUNT being vaild for this connection 
245  (ie not disabled, expired and the like).
246 ****************************************************************************/
247 static NTSTATUS sam_account_ok(TALLOC_CTX *mem_ctx,
248                                SAM_ACCOUNT *sampass, 
249                                const auth_usersupplied_info *user_info)
250 {
251         uint16  acct_ctrl = pdb_get_acct_ctrl(sampass);
252         char *workstation_list;
253         time_t kickoff_time;
254         
255         DEBUG(4,("sam_account_ok: Checking SMB password for user %s\n",pdb_get_username(sampass)));
256
257         /* Quit if the account was disabled. */
258         if (acct_ctrl & ACB_DISABLED) {
259                 DEBUG(1,("Account for user '%s' was disabled.\n", pdb_get_username(sampass)));
260                 return NT_STATUS_ACCOUNT_DISABLED;
261         }
262
263         /* Test account expire time */
264         
265         kickoff_time = pdb_get_kickoff_time(sampass);
266         if (kickoff_time != 0 && time(NULL) > kickoff_time) {
267                 DEBUG(1,("Account for user '%s' has expried.\n", pdb_get_username(sampass)));
268                 DEBUG(3,("Account expired at '%ld' unix time.\n", (long)kickoff_time));
269                 return NT_STATUS_ACCOUNT_EXPIRED;
270         }
271
272         if (!(pdb_get_acct_ctrl(sampass) & ACB_PWNOEXP)) {
273                 time_t must_change_time = pdb_get_pass_must_change_time(sampass);
274                 time_t last_set_time = pdb_get_pass_last_set_time(sampass);
275
276                 /* check for immediate expiry "must change at next logon" */
277                 if (must_change_time == 0 && last_set_time != 0) {
278                         DEBUG(1,("Account for user '%s' password must change!.\n", pdb_get_username(sampass)));
279                         return NT_STATUS_PASSWORD_MUST_CHANGE;
280                 }
281
282                 /* check for expired password */
283                 if (must_change_time < time(NULL) && must_change_time != 0) {
284                         DEBUG(1,("Account for user '%s' password expired!.\n", pdb_get_username(sampass)));
285                         DEBUG(1,("Password expired at '%s' (%ld) unix time.\n", http_timestring(must_change_time), (long)must_change_time));
286                         return NT_STATUS_PASSWORD_EXPIRED;
287                 }
288         }
289
290         /* Test workstation. Workstation list is comma separated. */
291
292         workstation_list = talloc_strdup(mem_ctx, pdb_get_workstations(sampass));
293
294         if (!workstation_list) return NT_STATUS_NO_MEMORY;
295
296         if (*workstation_list) {
297                 BOOL invalid_ws = True;
298                 char *s = workstation_list;
299                         
300                 fstring tok;
301                         
302                 while (next_token(&s, tok, ",", sizeof(tok))) {
303                         DEBUG(10,("checking for workstation match %s and %s (len=%d)\n",
304                                   tok, user_info->wksta_name.str, user_info->wksta_name.len));
305                         if(strequal(tok, user_info->wksta_name.str)) {
306                                 invalid_ws = False;
307                                 break;
308                         }
309                 }
310                 
311                 if (invalid_ws) 
312                         return NT_STATUS_INVALID_WORKSTATION;
313         }
314
315         if (acct_ctrl & ACB_DOMTRUST) {
316                 DEBUG(2,("sam_account_ok: Domain trust account %s denied by server\n", pdb_get_username(sampass)));
317                 return NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT;
318         }
319         
320         if (acct_ctrl & ACB_SVRTRUST) {
321                 DEBUG(2,("sam_account_ok: Server trust account %s denied by server\n", pdb_get_username(sampass)));
322                 return NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT;
323         }
324         
325         if (acct_ctrl & ACB_WSTRUST) {
326                 DEBUG(4,("sam_account_ok: Wksta trust account %s denied by server\n", pdb_get_username(sampass)));
327                 return NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT;
328         }
329         
330         return NT_STATUS_OK;
331 }
332
333
334 /****************************************************************************
335 check if a username/password is OK assuming the password is a 24 byte
336 SMB hash supplied in the user_info structure
337 return an NT_STATUS constant.
338 ****************************************************************************/
339
340 static NTSTATUS check_sam_security(void *my_private_data,
341                                    TALLOC_CTX *mem_ctx,
342                                    const auth_usersupplied_info *user_info, 
343                                    const auth_authsupplied_info *auth_info,
344                                    auth_serversupplied_info **server_info)
345 {
346         SAM_ACCOUNT *sampass=NULL;
347         BOOL ret;
348         NTSTATUS nt_status;
349         uint8 user_sess_key[16];
350         const uint8* lm_hash;
351
352         if (!user_info || !auth_info) {
353                 return NT_STATUS_LOGON_FAILURE;
354         }
355
356         if (!pdb_init_sam(&sampass)) {
357                 return NT_STATUS_NO_MEMORY;
358         }
359
360         /* get the account information */
361
362         become_root();
363         ret = pdb_getsampwnam(sampass, user_info->internal_username.str);
364         unbecome_root();
365
366         if (ret == False)
367         {
368                 DEBUG(1,("Couldn't find user '%s' in passdb file.\n", user_info->internal_username.str));
369                 pdb_free_sam(&sampass);
370                 return NT_STATUS_NO_SUCH_USER;
371         }
372
373         nt_status = sam_password_ok(mem_ctx, sampass, user_info, auth_info, user_sess_key);
374
375         if (!NT_STATUS_IS_OK(nt_status)) {
376                 pdb_free_sam(&sampass);
377                 return nt_status;
378         }
379
380         nt_status = sam_account_ok(mem_ctx, sampass, user_info);
381         
382         if (!NT_STATUS_IS_OK(nt_status)) {
383                 pdb_free_sam(&sampass);
384                 return nt_status;
385         }
386
387         if (!make_server_info_sam(server_info, sampass)) {              
388                 DEBUG(0,("failed to malloc memory for server_info\n"));
389                 return NT_STATUS_NO_MEMORY;
390         }
391
392         lm_hash = pdb_get_lanman_passwd((*server_info)->sam_account);
393         if (lm_hash) {
394                 memcpy((*server_info)->first_8_lm_hash, lm_hash, 8);
395         }
396         
397         memcpy((*server_info)->session_key, user_sess_key, sizeof(user_sess_key));
398
399         return nt_status;
400 }
401
402 BOOL auth_init_sam(auth_methods **auth_method) 
403 {
404         if (!make_auth_methods(auth_method)) {
405                 return False;
406         }
407
408         (*auth_method)->auth = check_sam_security;
409         return True;
410 }
411
412 /****************************************************************************
413 check if a username/password is OK assuming the password is a 24 byte
414 SMB hash supplied in the user_info structure
415 return an NT_STATUS constant.
416 ****************************************************************************/
417
418 static NTSTATUS check_samstrict_security(void *my_private_data,
419                                          TALLOC_CTX *mem_ctx,
420                                          const auth_usersupplied_info *user_info, 
421                                          const auth_authsupplied_info *auth_info,
422                                          auth_serversupplied_info **server_info)
423 {
424
425         if (!user_info || !auth_info) {
426                 return NT_STATUS_LOGON_FAILURE;
427         }
428
429         /* If we are a domain member, we must not 
430            attempt to check the password locally,
431            unless it is one of our aliases. */
432         
433         if (!is_netbios_alias_or_name(user_info->domain.str)) {
434                 return NT_STATUS_NO_SUCH_USER;
435         }
436         
437         return check_sam_security(my_private_data, mem_ctx, user_info, auth_info, server_info);
438 }
439
440 BOOL auth_init_samstrict(auth_methods **auth_method) 
441 {
442         if (!make_auth_methods(auth_method)) {
443                 return False;
444         }
445
446         (*auth_method)->auth = check_samstrict_security;
447         return True;
448 }
449