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