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