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 extern pstring global_myname;
30 /****************************************************************************
31 Check user is in correct domain if required
32 ****************************************************************************/
34 static BOOL check_domain_match(char *user, char *domain)
37 * If we aren't serving to trusted domains, we must make sure that
38 * the validation request comes from an account in the same domain
42 if (!lp_allow_trusted_domains() &&
43 !strequal(lp_workgroup(), domain) ) {
44 DEBUG(1, ("check_domain_match: Attempt to connect as user %s from domain %s denied.\n", user, domain));
51 /****************************************************************************
52 Check a users password, as given in the user-info struct and return various
53 interesting details in the server_info struct.
55 This functions does NOT need to be in a become_root()/unbecome_root() pair
56 as it makes the calls itself when needed.
57 ****************************************************************************/
59 NTSTATUS check_password(const auth_usersupplied_info *user_info,
60 auth_serversupplied_info *server_info)
63 NTSTATUS nt_status = NT_STATUS_LOGON_FAILURE;
64 BOOL done_pam = False;
66 DEBUG(3, ("check_password: Checking password for user %s with the new password interface\n", user_info->smb_username.str));
67 if (!check_domain_match(user_info->smb_username.str, user_info->domain.str)) {
68 return NT_STATUS_LOGON_FAILURE;
71 if (nt_status != NT_STATUS_NOPROBLEMO) {
72 nt_status = check_rhosts_security(user_info, server_info);
75 if ((lp_security() == SEC_DOMAIN) && (nt_status != NT_STATUS_NOPROBLEMO)) {
76 nt_status = check_domain_security(user_info, server_info);
79 if ((lp_security() == SEC_SERVER) && (nt_status != NT_STATUS_NOPROBLEMO)) {
80 nt_status = check_server_security(user_info, server_info);
83 if (lp_security() >= SEC_SERVER) {
84 smb_user_control(user_info->smb_username.str, nt_status);
87 if (nt_status != NT_STATUS_NOPROBLEMO) {
88 if ((user_info->plaintext_password.len > 0)
89 && (!lp_plaintext_to_smbpasswd())) {
90 nt_status = check_unix_security(user_info, server_info);
93 nt_status = check_smbpasswd_security(user_info, server_info);
97 if ((nt_status == NT_STATUS_NOPROBLEMO) && !done_pam) {
98 /* We might not be root if we are an RPC call */
100 nt_status = smb_pam_accountcheck(user_info->smb_username.str);
104 if (nt_status == NT_STATUS_NOPROBLEMO) {
105 DEBUG(5, ("check_password: Password for user %s suceeded\n", user_info->smb_username.str));
107 DEBUG(3, ("check_password: Password for user %s FAILED with error %s\n", user_info->smb_username.str, get_nt_error_msg(nt_status)));
114 /****************************************************************************
115 COMPATABILITY INTERFACES:
116 ***************************************************************************/
118 /****************************************************************************
119 check if a username/password is OK assuming the password is a 24 byte
121 return True if the password is correct, False otherwise
122 ****************************************************************************/
124 NTSTATUS pass_check_smb_with_chal(char *user, char *domain, uchar chal[8],
125 uchar *lm_pwd, int lm_pwd_len,
126 uchar *nt_pwd, int nt_pwd_len)
129 auth_usersupplied_info user_info;
130 auth_serversupplied_info server_info;
131 AUTH_STR ourdomain, theirdomain, smb_username, wksta_name;
133 ZERO_STRUCT(user_info);
134 ZERO_STRUCT(ourdomain);
135 ZERO_STRUCT(theirdomain);
136 ZERO_STRUCT(smb_username);
137 ZERO_STRUCT(wksta_name);
139 ourdomain.str = lp_workgroup();
140 ourdomain.len = strlen(ourdomain.str);
142 theirdomain.str = domain;
143 theirdomain.len = strlen(theirdomain.str);
145 user_info.requested_domain = theirdomain;
146 user_info.domain = ourdomain;
148 smb_username.str = user;
149 smb_username.len = strlen(smb_username.str);
151 user_info.requested_username = smb_username; /* For the time-being */
152 user_info.smb_username = smb_username;
154 user_info.wksta_name.str = client_name();
155 user_info.wksta_name.len = strlen(client_name());
157 user_info.wksta_name = wksta_name;
159 memcpy(user_info.chal, chal, 8);
161 if ((lm_pwd_len >= 24 || nt_pwd_len >= 24) ||
162 (lp_encrypted_passwords() && (lm_pwd_len == 0) && lp_null_passwords())) {
163 /* if 24 bytes long assume it is an encrypted password */
165 user_info.lm_resp.buffer = (uint8 *)lm_pwd;
166 user_info.lm_resp.len = lm_pwd_len;
167 user_info.nt_resp.buffer = (uint8 *)nt_pwd;
168 user_info.nt_resp.len = nt_pwd_len;
171 unsigned char local_lm_response[24];
172 unsigned char local_nt_response[24];
175 * Not encrypted - do so.
178 DEBUG(5,("pass_check_smb: User passwords not in encrypted format.\n"));
180 if (lm_pwd_len > 0) {
181 SMBencrypt( (uchar *)lm_pwd, user_info.chal, local_lm_response);
182 user_info.lm_resp.buffer = (uint8 *)local_lm_response;
183 user_info.lm_resp.len = 24;
185 /* This encrypts the lm_pwd feild, which actualy contains the password
186 rather than the nt_pwd field becouse that contains nothing */
187 SMBNTencrypt((uchar *)lm_pwd, user_info.chal, local_nt_response);
188 user_info.nt_resp.buffer = (uint8 *)local_nt_response;
189 user_info.nt_resp.len = 24;
192 user_info.plaintext_password.str = (char *)lm_pwd;
193 user_info.plaintext_password.len = lm_pwd_len;
197 return check_password(&user_info, &server_info);
200 NTSTATUS pass_check_smb(char *user, char *domain,
201 uchar *lm_pwd, int lm_pwd_len,
202 uchar *nt_pwd, int nt_pwd_len)
206 if (!last_challenge(chal)) {
207 generate_random_buffer( chal, 8, False);
210 return pass_check_smb_with_chal(user, domain, chal,
216 /****************************************************************************
217 check if a username/password pair is OK either via the system password
218 database or the encrypted SMB password database
219 return True if the password is correct, False otherwise
220 ****************************************************************************/
221 BOOL password_ok(char *user, char *password, int pwlen)
225 * This hack must die! But until I rewrite the rest of samba
226 * it must stay - abartlet 2001-08-03
229 if ((pwlen == 0) && !lp_null_passwords()) {
230 DEBUG(4,("Null passwords not allowed.\n"));
234 /* The password could be either NTLM or plain LM. Try NTLM first, but fall-through as
236 if (pass_check_smb(user, lp_workgroup(), NULL, 0, (unsigned char *)password, pwlen) == NT_STATUS_NOPROBLEMO) {
240 if (pass_check_smb(user, lp_workgroup(), (unsigned char *)password, pwlen, NULL, 0) == NT_STATUS_NOPROBLEMO) {