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 /****************************************************************************
27 core of smb password checking routine.
28 ****************************************************************************/
29 static BOOL smb_pwd_check_ntlmv1(const uchar *password,
30 const uchar *part_passwd,
32 char user_sess_key[16])
34 /* Finish the encryption of part_passwd. */
37 if (part_passwd == NULL) {
38 DEBUG(10,("No password set - DISALLOWING access\n"));
39 /* No password set - always false ! */
43 SMBOWFencrypt(part_passwd, c8, p24);
44 if (user_sess_key != NULL)
46 SMBsesskeygen_ntv1(part_passwd, NULL, user_sess_key);
52 DEBUG(100,("Part password (P16) was |"));
53 dump_data(100, part_passwd, 16);
54 DEBUG(100,("Password from client was |"));
55 dump_data(100, password, 24);
56 DEBUG(100,("Given challenge was |"));
57 dump_data(100, c8, 8);
58 DEBUG(100,("Value from encryption was |"));
59 dump_data(100, p24, 24);
61 return (memcmp(p24, password, 24) == 0);
64 /****************************************************************************
65 core of smb password checking routine.
66 ****************************************************************************/
67 static BOOL smb_pwd_check_ntlmv2(const uchar *password, size_t pwd_len,
68 const uchar *part_passwd,
70 const char *user, const char *domain,
71 char user_sess_key[16])
73 /* Finish the encryption of part_passwd. */
77 if (part_passwd == NULL)
79 DEBUG(10,("No password set - DISALLOWING access\n"));
80 /* No password set - always False */
84 ntv2_owf_gen(part_passwd, user, domain, kr);
85 SMBOWFencrypt_ntv2(kr, c8, 8, password+16, pwd_len-16, (char *)resp);
86 if (user_sess_key != NULL)
88 SMBsesskeygen_ntv2(kr, resp, user_sess_key);
92 DEBUG(100,("Part password (P16) was |"));
93 dump_data(100, part_passwd, 16);
94 DEBUG(100,("Password from client was |"));
95 dump_data(100, password, pwd_len);
96 DEBUG(100,("Given challenge was |"));
97 dump_data(100, c8, 8);
98 DEBUG(100,("Value from encryption was |"));
99 dump_data(100, resp, 16);
102 return (memcmp(resp, password, 16) == 0);
106 /****************************************************************************
107 Do a specific test for an smb password being correct, given a smb_password and
108 the lanman and NT responses.
109 ****************************************************************************/
110 NTSTATUS sam_password_ok(SAM_ACCOUNT *sampass, const auth_usersupplied_info *user_info, char user_sess_key[16])
112 const uint8 *nt_pw, *lm_pw;
113 uint16 acct_ctrl = pdb_get_acct_ctrl(sampass);
115 if (!user_info || !sampass)
116 return NT_STATUS_LOGON_FAILURE;
118 if (acct_ctrl & ACB_PWNOTREQ)
120 if (lp_null_passwords())
122 DEBUG(3,("Account for user '%s' has no password and null passwords are allowed.\n", sampass->username));
123 return(NT_STATUS_OK);
127 DEBUG(3,("Account for user '%s' has no password and null passwords are NOT allowed.\n", sampass->username));
128 return(NT_STATUS_LOGON_FAILURE);
132 nt_pw = pdb_get_nt_passwd(sampass);
133 lm_pw = pdb_get_lanman_passwd(sampass);
135 if (nt_pw != NULL && user_info->nt_resp.len > 0) {
136 if ((user_info->nt_resp.len > 24 )) {
137 /* We have the NT MD4 hash challenge available - see if we can
138 use it (ie. does it exist in the smbpasswd file).
140 DEBUG(4,("smb_password_ok: Checking NTLMv2 password\n"));
141 if (smb_pwd_check_ntlmv2( user_info->nt_resp.buffer,
142 user_info->nt_resp.len,
144 user_info->chal, user_info->smb_username.str,
145 user_info->requested_domain.str,
150 DEBUG(4,("smb_password_ok: NTLMv2 password check failed\n"));
151 return NT_STATUS_WRONG_PASSWORD;
154 } else if (lp_ntlm_auth() && (user_info->nt_resp.len == 24)) {
155 /* We have the NT MD4 hash challenge available - see if we can
156 use it (ie. does it exist in the smbpasswd file).
158 DEBUG(4,("smb_password_ok: Checking NT MD4 password\n"));
159 if (smb_pwd_check_ntlmv1(user_info->nt_resp.buffer,
160 nt_pw, user_info->chal,
165 DEBUG(4,("smb_password_ok: NT MD4 password check failed\n"));
166 return NT_STATUS_WRONG_PASSWORD;
169 return NT_STATUS_LOGON_FAILURE;
173 if (lm_pw != NULL && user_info->lm_resp.len == 24) {
174 if (lp_lanman_auth()) {
175 DEBUG(4,("smb_password_ok: Checking LM password\n"));
176 if (smb_pwd_check_ntlmv1(user_info->lm_resp.buffer,
177 lm_pw, user_info->chal,
182 DEBUG(4,("smb_password_ok: LM password check failed\n"));
183 return NT_STATUS_WRONG_PASSWORD;
188 /* Should not be reached */
189 return NT_STATUS_LOGON_FAILURE;
192 /****************************************************************************
193 Do a specific test for a SAM_ACCOUNT being vaild for this connection
194 (ie not disabled, expired and the like).
195 ****************************************************************************/
196 NTSTATUS sam_account_ok(SAM_ACCOUNT *sampass, const auth_usersupplied_info *user_info)
198 uint16 acct_ctrl = pdb_get_acct_ctrl(sampass);
199 char *workstation_list;
202 if (!user_info || !sampass)
203 return NT_STATUS_LOGON_FAILURE;
205 DEBUG(4,("smb_password_ok: Checking SMB password for user %s\n",pdb_get_username(sampass)));
207 /* Quit if the account was disabled. */
208 if (acct_ctrl & ACB_DISABLED) {
209 DEBUG(1,("Account for user '%s' was disabled.\n", pdb_get_username(sampass)));
210 return NT_STATUS_ACCOUNT_DISABLED;
213 /* Test account expire time */
215 kickoff_time = pdb_get_kickoff_time(sampass);
216 if (kickoff_time != 0 && time(NULL) > kickoff_time) {
217 DEBUG(1,("Account for user '%s' has expried.\n", pdb_get_username(sampass)));
218 DEBUG(3,("Account expired at '%ld' unix time.\n", (long)kickoff_time));
219 return NT_STATUS_ACCOUNT_EXPIRED;
222 /* Test workstation. Workstation list is comma separated. */
224 workstation_list = strdup(pdb_get_workstations(sampass));
226 if (!workstation_list) return NT_STATUS_NO_MEMORY;
228 if (*workstation_list) {
229 BOOL invalid_ws = True;
230 char *s = workstation_list;
234 while (next_token(&s, tok, ",", sizeof(tok))) {
235 DEBUG(10,("checking for workstation match %s and %s (len=%d)\n",
236 tok, user_info->wksta_name.str, user_info->wksta_name.len));
237 if(strequal(tok, user_info->wksta_name.str)) {
243 SAFE_FREE(workstation_list);
245 return NT_STATUS_INVALID_WORKSTATION;
247 SAFE_FREE(workstation_list);
250 if (!(pdb_get_acct_ctrl(sampass) & ACB_PWNOEXP)) {
251 time_t must_change_time = pdb_get_pass_must_change_time(sampass);
252 time_t last_set_time = pdb_get_pass_last_set_time(sampass);
254 /* check for immediate expiry "must change at next logon" */
255 if (must_change_time == 0 && last_set_time != 0) {
256 DEBUG(1,("Account for user '%s' password must change!.\n", pdb_get_username(sampass)));
257 return NT_STATUS_PASSWORD_MUST_CHANGE;
260 /* check for expired password */
261 if (must_change_time < time(NULL) && must_change_time != 0) {
262 DEBUG(1,("Account for user '%s' password expired!.\n", pdb_get_username(sampass)));
263 DEBUG(1,("Password expired at '%s' (%ld) unix time.\n", http_timestring(must_change_time), (long)must_change_time));
264 return NT_STATUS_PASSWORD_EXPIRED;
268 if (acct_ctrl & ACB_DOMTRUST) {
269 DEBUG(2,("session_trust_account: Domain trust account %s denied by server\n", pdb_get_username(sampass)));
270 return NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT;
273 if (acct_ctrl & ACB_SVRTRUST) {
274 DEBUG(2,("session_trust_account: Server trust account %s denied by server\n", pdb_get_username(sampass)));
275 return NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT;
278 if (acct_ctrl & ACB_WSTRUST) {
279 DEBUG(4,("session_trust_account: Wksta trust account %s denied by server\n", pdb_get_username(sampass)));
280 return NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT;
287 /****************************************************************************
288 check if a username/password is OK assuming the password is a 24 byte
289 SMB hash supplied in the user_info structure
290 return an NT_STATUS constant.
291 ****************************************************************************/
293 NTSTATUS check_smbpasswd_security(const auth_usersupplied_info *user_info, auth_serversupplied_info *server_info)
295 SAM_ACCOUNT *sampass=NULL;
299 pdb_init_sam(&sampass);
301 /* get the account information */
304 ret = pdb_getsampwnam(sampass, user_info->unix_username.str);
309 DEBUG(1,("Couldn't find user '%s' in passdb file.\n", user_info->unix_username.str));
310 pdb_free_sam(&sampass);
311 return NT_STATUS_NO_SUCH_USER;
314 nt_status = sam_password_ok(sampass, user_info, (char *)(server_info->session_key));
316 if NT_STATUS_IS_OK(nt_status) {
317 nt_status = sam_account_ok(sampass, user_info);
320 pdb_free_sam(&sampass);