s3: Lift talloc_autofree_context() from make_auth_context_subsystem()
[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 in plaintext
34 return True if the password is correct, False otherwise
35 ****************************************************************************/
36
37 NTSTATUS check_plaintext_password(const char *smb_name,
38                                   DATA_BLOB plaintext_blob,
39                                   struct auth_serversupplied_info **server_info)
40 {
41         struct auth_context *plaintext_auth_context = NULL;
42         struct auth_usersupplied_info *user_info = NULL;
43         uint8_t chal[8];
44         NTSTATUS nt_status;
45
46         nt_status = make_auth_context_subsystem(talloc_tos(),
47                                                 &plaintext_auth_context);
48         if (!NT_STATUS_IS_OK(nt_status)) {
49                 return nt_status;
50         }
51
52         plaintext_auth_context->get_ntlm_challenge(plaintext_auth_context,
53                                                    chal);
54
55         if (!make_user_info_for_reply(&user_info, 
56                                       smb_name, lp_workgroup(), chal,
57                                       plaintext_blob)) {
58                 return NT_STATUS_NO_MEMORY;
59         }
60
61         nt_status = plaintext_auth_context->check_ntlm_password(plaintext_auth_context, 
62                                                                 user_info, server_info); 
63
64         TALLOC_FREE(plaintext_auth_context);
65         free_user_info(&user_info);
66         return nt_status;
67 }
68
69 static NTSTATUS pass_check_smb(struct auth_context *actx,
70                                const char *smb_name,
71                                const char *domain, 
72                                DATA_BLOB lm_pwd,
73                                DATA_BLOB nt_pwd)
74
75 {
76         NTSTATUS nt_status;
77         struct auth_serversupplied_info *server_info = NULL;
78         struct auth_usersupplied_info *user_info = NULL;
79         if (actx == NULL) {
80                 return NT_STATUS_INTERNAL_ERROR;
81         }
82         make_user_info_for_reply_enc(&user_info, smb_name,
83                                      domain,
84                                      lm_pwd,
85                                      nt_pwd);
86         nt_status = actx->check_ntlm_password(actx, user_info, &server_info);
87         free_user_info(&user_info);
88         TALLOC_FREE(server_info);
89         return nt_status;
90 }
91
92 /****************************************************************************
93 check if a username/password pair is ok via the auth subsystem.
94 return True if the password is correct, False otherwise
95 ****************************************************************************/
96
97 bool password_ok(struct auth_context *actx, bool global_encrypted,
98                  const char *session_workgroup,
99                  const char *smb_name, DATA_BLOB password_blob)
100 {
101
102         DATA_BLOB null_password = data_blob_null;
103         bool encrypted = (global_encrypted && (password_blob.length == 24 || password_blob.length > 46));
104
105         if (encrypted) {
106                 /* 
107                  * The password could be either NTLM or plain LM.  Try NTLM first, 
108                  * but fall-through as required.
109                  * Vista sends NTLMv2 here - we need to try the client given workgroup.
110                  */
111                 if (session_workgroup) {
112                         if (NT_STATUS_IS_OK(pass_check_smb(actx, smb_name, session_workgroup, null_password, password_blob))) {
113                                 return True;
114                         }
115                         if (NT_STATUS_IS_OK(pass_check_smb(actx, smb_name, session_workgroup, password_blob, null_password))) {
116                                 return True;
117                         }
118                 }
119
120                 if (NT_STATUS_IS_OK(pass_check_smb(actx, smb_name, lp_workgroup(), null_password, password_blob))) {
121                         return True;
122                 }
123
124                 if (NT_STATUS_IS_OK(pass_check_smb(actx, smb_name, lp_workgroup(), password_blob, null_password))) {
125                         return True;
126                 }
127         } else {
128                 struct auth_serversupplied_info *server_info = NULL;
129                 NTSTATUS nt_status = check_plaintext_password(smb_name, password_blob, &server_info);
130                 TALLOC_FREE(server_info);
131                 if (NT_STATUS_IS_OK(nt_status)) {
132                         return True;
133                 }
134         }
135
136         return False;
137 }