2 Unix SMB/Netbios implementation.
4 Password and authentication handling
5 Copyright (C) Andrew Tridgell 1992-2000
6 Copyright (C) Luke Kenneth Casson Leighton 1996-2000
7 Copyright (C) Andrew Bartlett 2001
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.
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.
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.
26 extern int DEBUGLEVEL;
28 /****************************************************************************
29 Check user is in correct domain if required
30 ****************************************************************************/
32 static BOOL check_domain_match(char *user, char *domain)
35 * If we aren't serving to trusted domains, we must make sure that
36 * the validation request comes from an account in the same domain
40 if (!lp_allow_trusted_domains() &&
41 !strequal(lp_workgroup(), domain) ) {
42 DEBUG(1, ("check_domain_match: Attempt to connect as user %s from domain %s denied.\n", user, domain));
49 /****************************************************************************
50 Check a users password, as given in the user-info struct and return various
51 interesting details in the server_info struct.
53 This functions does NOT need to be in a become_root()/unbecome_root() pair
54 as it makes the calls itself when needed.
55 ****************************************************************************/
57 NTSTATUS check_password(const auth_usersupplied_info *user_info,
58 auth_serversupplied_info *server_info)
61 NTSTATUS nt_status = NT_STATUS_LOGON_FAILURE;
62 BOOL done_pam = False;
64 DEBUG(3, ("check_password: Checking password for smb user %s\\%s@%s with the new password interface\n",
65 user_info->smb_username.str, user_info->requested_domain.str, user_info->wksta_name.str));
66 if (!check_domain_match(user_info->smb_username.str, user_info->domain.str)) {
67 return NT_STATUS_LOGON_FAILURE;
70 if (!NT_STATUS_IS_OK(nt_status)) {
71 nt_status = check_rhosts_security(user_info, server_info);
74 if ((lp_security() == SEC_DOMAIN) && !NT_STATUS_IS_OK(nt_status)) {
75 nt_status = check_domain_security(user_info, server_info);
78 if ((lp_security() == SEC_SERVER) && !NT_STATUS_IS_OK(nt_status)) {
79 nt_status = check_server_security(user_info, server_info);
82 if (lp_security() >= SEC_SERVER) {
83 smb_user_control(user_info->unix_username.str, nt_status);
86 if (!NT_STATUS_IS_OK(nt_status)) {
87 if ((user_info->plaintext_password.len > 0)
88 && (!lp_plaintext_to_smbpasswd())) {
89 nt_status = check_unix_security(user_info, server_info);
92 nt_status = check_smbpasswd_security(user_info, server_info);
96 if (NT_STATUS_IS_OK(nt_status) && !done_pam) {
97 /* We might not be root if we are an RPC call */
99 nt_status = smb_pam_accountcheck(user_info->unix_username.str);
103 if (NT_STATUS_IS_OK(nt_status)) {
104 DEBUG(5, ("check_password: Password for smb user %s suceeded\n", user_info->smb_username.str));
106 DEBUG(3, ("check_password: Password for smb user %s FAILED with error %s\n", user_info->smb_username.str, get_nt_error_msg(nt_status)));
113 /****************************************************************************
114 COMPATABILITY INTERFACES:
115 ***************************************************************************/
117 /****************************************************************************
118 check if a username/password is OK assuming the password is a 24 byte
120 return True if the password is correct, False otherwise
121 ****************************************************************************/
123 NTSTATUS pass_check_smb_with_chal(char *smb_user, char *unix_user,
124 char *domain, char* workstation,
126 uchar *lm_pwd, int lm_pwd_len,
127 uchar *nt_pwd, int nt_pwd_len)
130 auth_usersupplied_info user_info;
131 auth_serversupplied_info server_info;
132 AUTH_STR ourdomain, theirdomain, unix_username, smb_username,
135 ZERO_STRUCT(user_info);
136 ZERO_STRUCT(ourdomain);
137 ZERO_STRUCT(theirdomain);
138 ZERO_STRUCT(smb_username);
139 ZERO_STRUCT(wksta_name);
141 ourdomain.str = lp_workgroup();
142 ourdomain.len = strlen(ourdomain.str);
144 theirdomain.str = domain;
145 theirdomain.len = strlen(theirdomain.str);
147 user_info.requested_domain = theirdomain;
148 user_info.domain = ourdomain;
150 smb_username.str = smb_user;
151 smb_username.len = strlen(smb_username.str);
153 /* If unix user is NULL, use smb user */
155 unix_username.str = unix_user ? unix_user : smb_user;
156 unix_username.len = strlen(unix_username.str);
158 user_info.unix_username = unix_username;
159 user_info.smb_username = smb_username;
161 wksta_name.str = workstation;
162 wksta_name.len = strlen(workstation);
164 user_info.wksta_name = wksta_name;
166 memcpy(user_info.chal, chal, 8);
168 if ((lm_pwd_len >= 24 || nt_pwd_len >= 24) ||
169 (lp_encrypted_passwords() && (lm_pwd_len == 0) && lp_null_passwords())) {
170 /* if 24 bytes long assume it is an encrypted password */
172 user_info.lm_resp.buffer = (uint8 *)lm_pwd;
173 user_info.lm_resp.len = lm_pwd_len;
174 user_info.nt_resp.buffer = (uint8 *)nt_pwd;
175 user_info.nt_resp.len = nt_pwd_len;
178 unsigned char local_lm_response[24];
179 unsigned char local_nt_response[24];
182 * Not encrypted - do so.
185 DEBUG(5,("pass_check_smb: User passwords not in encrypted format.\n"));
187 if (lm_pwd_len > 0) {
188 SMBencrypt( (uchar *)lm_pwd, user_info.chal, local_lm_response);
189 user_info.lm_resp.buffer = (uint8 *)local_lm_response;
190 user_info.lm_resp.len = 24;
193 /* WATCH OUT. This doesn't work if the incoming password is incorrectly cased.
194 We might want to add a check here and only do an LM in that case */
196 /* This encrypts the lm_pwd feild, which actualy contains the password
197 rather than the nt_pwd field becouse that contains nothing */
198 SMBNTencrypt((uchar *)lm_pwd, user_info.chal, local_nt_response);
199 user_info.nt_resp.buffer = (uint8 *)local_nt_response;
200 user_info.nt_resp.len = 24;
203 user_info.plaintext_password.str = (char *)lm_pwd;
204 user_info.plaintext_password.len = lm_pwd_len;
208 return check_password(&user_info, &server_info);
211 NTSTATUS pass_check_smb(char *smb_user, char *unix_user,
212 char *domain, char *workstation,
213 uchar *lm_pwd, int lm_pwd_len,
214 uchar *nt_pwd, int nt_pwd_len)
218 if (!last_challenge(chal)) {
219 generate_random_buffer( chal, 8, False);
222 return pass_check_smb_with_chal(smb_user, unix_user,
223 domain, workstation, chal,
229 /****************************************************************************
230 check if a username/password pair is OK either via the system password
231 database or the encrypted SMB password database
232 return True if the password is correct, False otherwise
233 ****************************************************************************/
234 BOOL password_ok(char *user, char *password, int pwlen)
236 extern fstring remote_machine;
239 * This hack must die! But until I rewrite the rest of samba
240 * it must stay - abartlet 2001-08-03
243 if ((pwlen == 0) && !lp_null_passwords()) {
244 DEBUG(4,("Null passwords not allowed.\n"));
248 /* The password could be either NTLM or plain LM. Try NTLM first, but fall-through as
250 if (NT_STATUS_IS_OK(pass_check_smb(user, NULL, remote_machine, lp_workgroup(), NULL, 0, (unsigned char *)password, pwlen))) {
254 if (NT_STATUS_IS_OK(pass_check_smb(user, NULL, remote_machine, lp_workgroup(), (unsigned char *)password, pwlen, NULL, 0))) {