r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text
[kai/samba.git] / source3 / auth / auth_compat.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Password and authentication handling
4    Copyright (C) Andrew Bartlett         2001-2002
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21
22 extern struct auth_context *negprot_global_auth_context;
23 extern BOOL global_encrypted_passwords_negotiated;
24
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_AUTH
27
28 /****************************************************************************
29  COMPATIBILITY INTERFACES:
30  ***************************************************************************/
31
32 /****************************************************************************
33 check if a username/password is OK assuming the password is a 24 byte
34 SMB hash
35 return True if the password is correct, False otherwise
36 ****************************************************************************/
37
38 NTSTATUS check_plaintext_password(const char *smb_name, DATA_BLOB plaintext_password, auth_serversupplied_info **server_info)
39 {
40         struct auth_context *plaintext_auth_context = NULL;
41         auth_usersupplied_info *user_info = NULL;
42         const uint8 *chal;
43         NTSTATUS nt_status;
44         if (!NT_STATUS_IS_OK(nt_status = make_auth_context_subsystem(&plaintext_auth_context))) {
45                 return nt_status;
46         }
47         
48         chal = plaintext_auth_context->get_ntlm_challenge(plaintext_auth_context);
49         
50         if (!make_user_info_for_reply(&user_info, 
51                                       smb_name, lp_workgroup(), chal,
52                                       plaintext_password)) {
53                 return NT_STATUS_NO_MEMORY;
54         }
55         
56         nt_status = plaintext_auth_context->check_ntlm_password(plaintext_auth_context, 
57                                                                 user_info, server_info); 
58         
59         (plaintext_auth_context->free)(&plaintext_auth_context);
60         free_user_info(&user_info);
61         return nt_status;
62 }
63
64 static NTSTATUS pass_check_smb(const char *smb_name,
65                                const char *domain, 
66                                DATA_BLOB lm_pwd,
67                                DATA_BLOB nt_pwd,
68                                DATA_BLOB plaintext_password,
69                                BOOL encrypted)
70
71 {
72         NTSTATUS nt_status;
73         auth_serversupplied_info *server_info = NULL;
74         if (encrypted) {                
75                 auth_usersupplied_info *user_info = NULL;
76                 make_user_info_for_reply_enc(&user_info, smb_name, 
77                                              domain,
78                                              lm_pwd, 
79                                              nt_pwd);
80                 nt_status = negprot_global_auth_context->check_ntlm_password(negprot_global_auth_context, 
81                                                                              user_info, &server_info);
82                 free_user_info(&user_info);
83         } else {
84                 nt_status = check_plaintext_password(smb_name, plaintext_password, &server_info);
85         }               
86         TALLOC_FREE(server_info);
87         return nt_status;
88 }
89
90 /****************************************************************************
91 check if a username/password pair is ok via the auth subsystem.
92 return True if the password is correct, False otherwise
93 ****************************************************************************/
94
95 BOOL password_ok(char *smb_name, DATA_BLOB password_blob)
96 {
97
98         DATA_BLOB null_password = data_blob_null;
99         BOOL encrypted = (global_encrypted_passwords_negotiated && (password_blob.length == 24 || password_blob.length > 46));
100         
101         if (encrypted) {
102                 /* 
103                  * The password could be either NTLM or plain LM.  Try NTLM first, 
104                  * but fall-through as required.
105                  * Vista sends NTLMv2 here - we need to try the client given workgroup.
106                  */
107                 if (get_session_workgroup()) {
108                         if (NT_STATUS_IS_OK(pass_check_smb(smb_name, get_session_workgroup(), null_password, password_blob, null_password, encrypted))) {
109                                 return True;
110                         }
111                         if (NT_STATUS_IS_OK(pass_check_smb(smb_name, get_session_workgroup(), password_blob, null_password, null_password, encrypted))) {
112                                 return True;
113                         }
114                 }
115
116                 if (NT_STATUS_IS_OK(pass_check_smb(smb_name, lp_workgroup(), null_password, password_blob, null_password, encrypted))) {
117                         return True;
118                 }
119                 
120                 if (NT_STATUS_IS_OK(pass_check_smb(smb_name, lp_workgroup(), password_blob, null_password, null_password, encrypted))) {
121                         return True;
122                 }
123         } else {
124                 if (NT_STATUS_IS_OK(pass_check_smb(smb_name, lp_workgroup(), null_password, null_password, password_blob, encrypted))) {
125                         return True;
126                 }
127         }
128
129         return False;
130 }