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