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(DATA_BLOB nt_response,
30 const uchar *part_passwd,
32 uint8 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 if (sec_blob.length != 8) {
44 DEBUG(0, ("smb_pwd_check_ntlmv1: incorrect challange size (%d)\n", sec_blob.length));
48 if (nt_response.length != 24) {
49 DEBUG(0, ("smb_pwd_check_ntlmv1: incorrect password length (%d)\n", nt_response.length));
53 SMBOWFencrypt(part_passwd, sec_blob.data, p24);
54 if (user_sess_key != NULL)
56 SMBsesskeygen_ntv1(part_passwd, NULL, user_sess_key);
62 DEBUG(100,("Part password (P16) was |"));
63 dump_data(100, part_passwd, 16);
64 DEBUG(100,("Password from client was |"));
65 dump_data(100, nt_response.data, nt_response.length);
66 DEBUG(100,("Given challenge was |"));
67 dump_data(100, sec_blob.data, sec_blob.length);
68 DEBUG(100,("Value from encryption was |"));
69 dump_data(100, p24, 24);
71 return (memcmp(p24, nt_response.data, 24) == 0);
74 /****************************************************************************
75 core of smb password checking routine.
76 ****************************************************************************/
77 static BOOL smb_pwd_check_ntlmv2(const DATA_BLOB ntv2_response,
78 const uchar *part_passwd,
79 const DATA_BLOB sec_blob,
80 const char *user, const char *domain,
81 uint8 user_sess_key[16])
83 /* Finish the encryption of part_passwd. */
85 uchar value_from_encryption[16];
86 uchar client_response[16];
87 DATA_BLOB client_key_data;
89 if (part_passwd == NULL)
91 DEBUG(10,("No password set - DISALLOWING access\n"));
92 /* No password set - always False */
96 if (ntv2_response.length < 16) {
97 /* We MUST have more than 16 bytes, or the stuff below will go
99 DEBUG(0, ("smb_pwd_check_ntlmv1: incorrect password length (%d)\n",
100 ntv2_response.length));
104 client_key_data = data_blob(ntv2_response.data+16, ntv2_response.length-16);
105 memcpy(client_response, ntv2_response.data, sizeof(client_response));
107 ntv2_owf_gen(part_passwd, user, domain, kr);
108 SMBOWFencrypt_ntv2(kr, sec_blob, client_key_data, (char *)value_from_encryption);
109 if (user_sess_key != NULL)
111 SMBsesskeygen_ntv2(kr, value_from_encryption, user_sess_key);
115 DEBUG(100,("Part password (P16) was |"));
116 dump_data(100, part_passwd, 16);
117 DEBUG(100,("Password from client was |"));
118 dump_data(100, ntv2_response.data, ntv2_response.length);
119 DEBUG(100,("Variable data from client was |"));
120 dump_data(100, client_key_data.data, client_key_data.length);
121 DEBUG(100,("Given challenge was |"));
122 dump_data(100, sec_blob.data, sec_blob.length);
123 DEBUG(100,("Value from encryption was |"));
124 dump_data(100, value_from_encryption, 16);
126 data_blob_clear_free(&client_key_data);
127 return (memcmp(value_from_encryption, client_response, 16) == 0);
131 /****************************************************************************
132 Do a specific test for an smb password being correct, given a smb_password and
133 the lanman and NT responses.
134 ****************************************************************************/
135 NTSTATUS sam_password_ok(SAM_ACCOUNT *sampass, const auth_usersupplied_info *user_info, uint8 user_sess_key[16])
137 const uint8 *nt_pw, *lm_pw;
138 uint16 acct_ctrl = pdb_get_acct_ctrl(sampass);
139 uint32 ntlmssp_flags;
141 if (!user_info || !sampass)
142 return NT_STATUS_LOGON_FAILURE;
144 if (acct_ctrl & ACB_PWNOTREQ)
146 if (lp_null_passwords())
148 DEBUG(3,("Account for user '%s' has no password and null passwords are allowed.\n", pdb_get_username(sampass)));
149 return(NT_STATUS_OK);
153 DEBUG(3,("Account for user '%s' has no password and null passwords are NOT allowed.\n", pdb_get_username(sampass)));
154 return(NT_STATUS_LOGON_FAILURE);
158 nt_pw = pdb_get_nt_passwd(sampass);
159 lm_pw = pdb_get_lanman_passwd(sampass);
161 ntlmssp_flags = user_info->ntlmssp_flags;
164 DEBUG(3,("smb_password_ok: NO NT password stored for user %s.\n",
165 pdb_get_username(sampass)));
166 /* No return, we want to check the LM hash below in this case */
167 ntlmssp_flags &= (~(NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_NTLM2));
170 if (ntlmssp_flags & NTLMSSP_NEGOTIATE_NTLM2) {
171 /* We have the NT MD4 hash challenge available - see if we can
172 use it (ie. does it exist in the smbpasswd file).
174 DEBUG(4,("smb_password_ok: Checking NTLMv2 password\n"));
175 if (smb_pwd_check_ntlmv2( user_info->nt_resp,
177 user_info->sec_blob, user_info->smb_name.str,
178 user_info->client_domain.str,
183 DEBUG(3,("smb_password_ok: NTLMv2 password check failed\n"));
184 return NT_STATUS_WRONG_PASSWORD;
186 } else if (ntlmssp_flags & NTLMSSP_NEGOTIATE_NTLM) {
187 if (lp_ntlm_auth()) {
188 /* We have the NT MD4 hash challenge available - see if we can
189 use it (ie. does it exist in the smbpasswd file).
191 DEBUG(4,("smb_password_ok: Checking NT MD4 password\n"));
192 if (smb_pwd_check_ntlmv1(user_info->nt_resp,
193 nt_pw, user_info->sec_blob,
198 DEBUG(3,("smb_password_ok: NT MD4 password check failed for user %s\n",pdb_get_username(sampass)));
199 return NT_STATUS_WRONG_PASSWORD;
202 DEBUG(2,("smb_password_ok: NTLMv1 passwords NOT PERMITTED for user %s\n",pdb_get_username(sampass)));
203 /* No return, we want to check the LM hash below in this case */
208 DEBUG(3,("smb_password_ok: NO LanMan password set for user %s (and no NT password supplied)\n",pdb_get_username(sampass)));
209 ntlmssp_flags &= (~NTLMSSP_NEGOTIATE_OEM);
212 if (ntlmssp_flags & NTLMSSP_NEGOTIATE_OEM) {
214 if (user_info->lm_resp.length != 24) {
215 DEBUG(2,("smb_password_ok: invalid LanMan password length (%d) for user %s\n",
216 user_info->nt_resp.length, pdb_get_username(sampass)));
219 if (!lp_lanman_auth()) {
220 DEBUG(3,("smb_password_ok: Lanman passwords NOT PERMITTED for user %s\n",pdb_get_username(sampass)));
221 return NT_STATUS_LOGON_FAILURE;
224 DEBUG(4,("smb_password_ok: Checking LM password\n"));
225 if (smb_pwd_check_ntlmv1(user_info->lm_resp,
226 lm_pw, user_info->sec_blob,
231 DEBUG(4,("smb_password_ok: LM password check failed for user %s\n",pdb_get_username(sampass)));
232 return NT_STATUS_WRONG_PASSWORD;
236 /* Should not be reached, but if they send nothing... */
237 DEBUG(3,("smb_password_ok: NEITHER LanMan nor NT password supplied for user %s\n",pdb_get_username(sampass)));
238 return NT_STATUS_WRONG_PASSWORD;
241 /****************************************************************************
242 Do a specific test for a SAM_ACCOUNT being vaild for this connection
243 (ie not disabled, expired and the like).
244 ****************************************************************************/
245 static NTSTATUS sam_account_ok(SAM_ACCOUNT *sampass, const auth_usersupplied_info *user_info)
247 uint16 acct_ctrl = pdb_get_acct_ctrl(sampass);
248 char *workstation_list;
251 DEBUG(4,("smb_password_ok: Checking SMB password for user %s\n",pdb_get_username(sampass)));
253 /* Quit if the account was disabled. */
254 if (acct_ctrl & ACB_DISABLED) {
255 DEBUG(1,("Account for user '%s' was disabled.\n", pdb_get_username(sampass)));
256 return NT_STATUS_ACCOUNT_DISABLED;
259 /* Test account expire time */
261 kickoff_time = pdb_get_kickoff_time(sampass);
262 if (kickoff_time != 0 && time(NULL) > kickoff_time) {
263 DEBUG(1,("Account for user '%s' has expried.\n", pdb_get_username(sampass)));
264 DEBUG(3,("Account expired at '%ld' unix time.\n", (long)kickoff_time));
265 return NT_STATUS_ACCOUNT_EXPIRED;
268 /* Test workstation. Workstation list is comma separated. */
270 workstation_list = strdup(pdb_get_workstations(sampass));
272 if (!workstation_list) return NT_STATUS_NO_MEMORY;
274 if (*workstation_list) {
275 BOOL invalid_ws = True;
276 char *s = workstation_list;
280 while (next_token(&s, tok, ",", sizeof(tok))) {
281 DEBUG(10,("checking for workstation match %s and %s (len=%d)\n",
282 tok, user_info->wksta_name.str, user_info->wksta_name.len));
283 if(strequal(tok, user_info->wksta_name.str)) {
289 SAFE_FREE(workstation_list);
291 return NT_STATUS_INVALID_WORKSTATION;
293 SAFE_FREE(workstation_list);
296 if (!(pdb_get_acct_ctrl(sampass) & ACB_PWNOEXP)) {
297 time_t must_change_time = pdb_get_pass_must_change_time(sampass);
298 time_t last_set_time = pdb_get_pass_last_set_time(sampass);
300 /* check for immediate expiry "must change at next logon" */
301 if (must_change_time == 0 && last_set_time != 0) {
302 DEBUG(1,("Account for user '%s' password must change!.\n", pdb_get_username(sampass)));
303 return NT_STATUS_PASSWORD_MUST_CHANGE;
306 /* check for expired password */
307 if (must_change_time < time(NULL) && must_change_time != 0) {
308 DEBUG(1,("Account for user '%s' password expired!.\n", pdb_get_username(sampass)));
309 DEBUG(1,("Password expired at '%s' (%ld) unix time.\n", http_timestring(must_change_time), (long)must_change_time));
310 return NT_STATUS_PASSWORD_EXPIRED;
314 if (acct_ctrl & ACB_DOMTRUST) {
315 DEBUG(2,("session_trust_account: Domain trust account %s denied by server\n", pdb_get_username(sampass)));
316 return NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT;
319 if (acct_ctrl & ACB_SVRTRUST) {
320 DEBUG(2,("session_trust_account: Server trust account %s denied by server\n", pdb_get_username(sampass)));
321 return NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT;
324 if (acct_ctrl & ACB_WSTRUST) {
325 DEBUG(4,("session_trust_account: Wksta trust account %s denied by server\n", pdb_get_username(sampass)));
326 return NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT;
333 /****************************************************************************
334 check if a username/password is OK assuming the password is a 24 byte
335 SMB hash supplied in the user_info structure
336 return an NT_STATUS constant.
337 ****************************************************************************/
339 NTSTATUS check_smbpasswd_security(const auth_usersupplied_info *user_info, auth_serversupplied_info **server_info)
341 SAM_ACCOUNT *sampass=NULL;
344 uint8 user_sess_key[16];
345 const uint8* lm_hash;
348 return NT_STATUS_LOGON_FAILURE;
351 if (!pdb_init_sam(&sampass)) {
352 return NT_STATUS_NO_MEMORY;
355 /* get the account information */
358 ret = pdb_getsampwnam(sampass, user_info->internal_username.str);
363 DEBUG(1,("Couldn't find user '%s' in passdb file.\n", user_info->internal_username.str));
364 pdb_free_sam(&sampass);
365 return NT_STATUS_NO_SUCH_USER;
368 nt_status = sam_password_ok(sampass, user_info, user_sess_key);
370 if (!NT_STATUS_IS_OK(nt_status)) {
371 pdb_free_sam(&sampass);
375 nt_status = sam_account_ok(sampass, user_info);
377 if (!NT_STATUS_IS_OK(nt_status)) {
378 pdb_free_sam(&sampass);
382 if (!make_server_info_sam(server_info, sampass)) {
383 DEBUG(0,("failed to malloc memory for server_info\n"));
384 return NT_STATUS_NO_MEMORY;
387 lm_hash = pdb_get_lanman_passwd((*server_info)->sam_account);
389 memcpy((*server_info)->first_8_lm_hash, lm_hash, 8);
392 memcpy((*server_info)->session_key, user_sess_key, sizeof(user_sess_key));