2 Unix SMB/CIFS implementation.
3 Password and authentication handling
4 Copyright (C) Andrew Tridgell 1992-2000
5 Copyright (C) Luke Kenneth Casson Leighton 1996-2000
6 Copyright (C) Andrew Bartlett 2001
7 Copyright (C) Gerald Carter 2003
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.
27 #define DBGC_CLASS DBGC_AUTH
29 /****************************************************************************
30 core of smb password checking routine.
31 ****************************************************************************/
32 static BOOL smb_pwd_check_ntlmv1(const DATA_BLOB *nt_response,
33 const uchar *part_passwd,
34 const DATA_BLOB *sec_blob,
35 uint8 user_sess_key[16])
37 /* Finish the encryption of part_passwd. */
40 if (part_passwd == NULL) {
41 DEBUG(10,("No password set - DISALLOWING access\n"));
42 /* No password set - always false ! */
46 if (sec_blob->length != 8) {
47 DEBUG(0, ("smb_pwd_check_ntlmv1: incorrect challenge size (%lu)\n", (unsigned long)sec_blob->length));
51 if (nt_response->length != 24) {
52 DEBUG(0, ("smb_pwd_check_ntlmv1: incorrect password length (%lu)\n", (unsigned long)nt_response->length));
56 SMBOWFencrypt(part_passwd, sec_blob->data, p24);
57 if (user_sess_key != NULL)
59 SMBsesskeygen_ntv1(part_passwd, NULL, user_sess_key);
65 DEBUG(100,("Part password (P16) was |\n"));
66 dump_data(100, part_passwd, 16);
67 DEBUGADD(100,("Password from client was |\n"));
68 dump_data(100, nt_response->data, nt_response->length);
69 DEBUGADD(100,("Given challenge was |\n"));
70 dump_data(100, sec_blob->data, sec_blob->length);
71 DEBUGADD(100,("Value from encryption was |\n"));
72 dump_data(100, p24, 24);
74 return (memcmp(p24, nt_response->data, 24) == 0);
78 /****************************************************************************
79 core of smb password checking routine. (NTLMv2, LMv2)
81 Note: The same code works with both NTLMv2 and LMv2.
82 ****************************************************************************/
83 static BOOL smb_pwd_check_ntlmv2(const DATA_BLOB *ntv2_response,
84 const uchar *part_passwd,
85 const DATA_BLOB *sec_blob,
86 const char *user, const char *domain,
87 uint8 user_sess_key[16])
89 /* Finish the encryption of part_passwd. */
91 uchar value_from_encryption[16];
92 uchar client_response[16];
93 DATA_BLOB client_key_data;
95 if (part_passwd == NULL)
97 DEBUG(10,("No password set - DISALLOWING access\n"));
98 /* No password set - always False */
102 if (ntv2_response->length < 24) {
103 /* We MUST have more than 16 bytes, or the stuff below will go
104 crazy. No known implementation sends less than the 24 bytes
105 for LMv2, let alone NTLMv2. */
106 DEBUG(0, ("smb_pwd_check_ntlmv2: incorrect password length (%lu)\n",
107 (unsigned long)ntv2_response->length));
111 client_key_data = data_blob(ntv2_response->data+16, ntv2_response->length-16);
113 todo: should we be checking this for anything? We can't for LMv2,
114 but for NTLMv2 it is meant to contain the current time etc.
117 memcpy(client_response, ntv2_response->data, sizeof(client_response));
119 if (!ntv2_owf_gen(part_passwd, user, domain, kr)) {
123 SMBOWFencrypt_ntv2(kr, sec_blob, &client_key_data, value_from_encryption);
124 if (user_sess_key != NULL)
126 SMBsesskeygen_ntv2(kr, value_from_encryption, user_sess_key);
130 DEBUG(100,("Part password (P16) was |\n"));
131 dump_data(100, part_passwd, 16);
132 DEBUGADD(100,("Password from client was |\n"));
133 dump_data(100, ntv2_response->data, ntv2_response->length);
134 DEBUGADD(100,("Variable data from client was |\n"));
135 dump_data(100, client_key_data.data, client_key_data.length);
136 DEBUGADD(100,("Given challenge was |\n"));
137 dump_data(100, sec_blob->data, sec_blob->length);
138 DEBUGADD(100,("Value from encryption was |\n"));
139 dump_data(100, value_from_encryption, 16);
141 data_blob_clear_free(&client_key_data);
142 return (memcmp(value_from_encryption, client_response, 16) == 0);
146 /****************************************************************************
147 Do a specific test for an smb password being correct, given a smb_password and
148 the lanman and NT responses.
149 ****************************************************************************/
150 static NTSTATUS sam_password_ok(const struct auth_context *auth_context,
152 SAM_ACCOUNT *sampass,
153 const auth_usersupplied_info *user_info,
154 uint8 user_sess_key[16])
157 const uint8 *nt_pw, *lm_pw;
160 acct_ctrl = pdb_get_acct_ctrl(sampass);
161 if (acct_ctrl & ACB_PWNOTREQ)
163 if (lp_null_passwords())
165 DEBUG(3,("Account for user '%s' has no password and null passwords are allowed.\n", pdb_get_username(sampass)));
166 return(NT_STATUS_OK);
170 DEBUG(3,("Account for user '%s' has no password and null passwords are NOT allowed.\n", pdb_get_username(sampass)));
171 return(NT_STATUS_LOGON_FAILURE);
175 auth_flags = user_info->auth_flags;
177 if (IS_SAM_DEFAULT(sampass, PDB_NTPASSWD)) {
178 DEBUG(3,("sam_password_ok: NO NT password stored for user %s.\n",
179 pdb_get_username(sampass)));
180 /* No return, we want to check the LM hash below in this case */
181 auth_flags &= (~(AUTH_FLAG_NTLMv2_RESP | AUTH_FLAG_NTLM_RESP));
184 if (auth_flags & AUTH_FLAG_NTLMv2_RESP) {
185 nt_pw = pdb_get_nt_passwd(sampass);
186 /* We have the NT MD4 hash challenge available - see if we can
187 use it (ie. does it exist in the smbpasswd file).
189 DEBUG(4,("sam_password_ok: Checking NTLMv2 password with domain [%s]\n", user_info->client_domain.str));
190 if (smb_pwd_check_ntlmv2( &user_info->nt_resp,
191 nt_pw, &auth_context->challenge,
192 user_info->smb_name.str,
193 user_info->client_domain.str,
199 DEBUG(4,("sam_password_ok: Checking NTLMv2 password without a domain\n"));
200 if (smb_pwd_check_ntlmv2( &user_info->nt_resp,
201 nt_pw, &auth_context->challenge,
202 user_info->smb_name.str,
209 DEBUG(3,("sam_password_ok: NTLMv2 password check failed\n"));
210 return NT_STATUS_WRONG_PASSWORD;
212 } else if (auth_flags & AUTH_FLAG_NTLM_RESP) {
213 if (lp_ntlm_auth()) {
214 nt_pw = pdb_get_nt_passwd(sampass);
215 /* We have the NT MD4 hash challenge available - see if we can
216 use it (ie. does it exist in the smbpasswd file).
218 DEBUG(4,("sam_password_ok: Checking NT MD4 password\n"));
219 if (smb_pwd_check_ntlmv1(&user_info->nt_resp,
220 nt_pw, &auth_context->challenge,
225 DEBUG(3,("sam_password_ok: NT MD4 password check failed for user %s\n",pdb_get_username(sampass)));
226 return NT_STATUS_WRONG_PASSWORD;
229 DEBUG(2,("sam_password_ok: NTLMv1 passwords NOT PERMITTED for user %s\n",pdb_get_username(sampass)));
230 /* no return, becouse we might pick up LMv2 in the LM field */
234 if (auth_flags & AUTH_FLAG_LM_RESP) {
235 if (user_info->lm_resp.length != 24) {
236 DEBUG(2,("sam_password_ok: invalid LanMan password length (%lu) for user %s\n",
237 (unsigned long)user_info->nt_resp.length, pdb_get_username(sampass)));
240 if (!lp_lanman_auth()) {
241 DEBUG(3,("sam_password_ok: Lanman passwords NOT PERMITTED for user %s\n",pdb_get_username(sampass)));
242 } else if (IS_SAM_DEFAULT(sampass, PDB_LMPASSWD)) {
243 DEBUG(3,("sam_password_ok: NO LanMan password set for user %s (and no NT password supplied)\n",pdb_get_username(sampass)));
245 lm_pw = pdb_get_lanman_passwd(sampass);
247 DEBUG(4,("sam_password_ok: Checking LM password\n"));
248 if (smb_pwd_check_ntlmv1(&user_info->lm_resp,
249 lm_pw, &auth_context->challenge,
256 if (IS_SAM_DEFAULT(sampass, PDB_NTPASSWD)) {
257 DEBUG(4,("sam_password_ok: LM password check failed for user, no NT password %s\n",pdb_get_username(sampass)));
258 return NT_STATUS_WRONG_PASSWORD;
261 nt_pw = pdb_get_nt_passwd(sampass);
263 /* This is for 'LMv2' authentication. almost NTLMv2 but limited to 24 bytes.
264 - related to Win9X, legacy NAS pass-though authentication
266 DEBUG(4,("sam_password_ok: Checking LMv2 password with domain %s\n", user_info->client_domain.str));
267 if (smb_pwd_check_ntlmv2( &user_info->lm_resp,
268 nt_pw, &auth_context->challenge,
269 user_info->smb_name.str,
270 user_info->client_domain.str,
276 DEBUG(4,("sam_password_ok: Checking LMv2 password without a domain\n"));
277 if (smb_pwd_check_ntlmv2( &user_info->lm_resp,
278 nt_pw, &auth_context->challenge,
279 user_info->smb_name.str,
286 /* Apparently NT accepts NT responses in the LM field
287 - I think this is related to Win9X pass-though authentication
289 DEBUG(4,("sam_password_ok: Checking NT MD4 password in LM field\n"));
292 if (smb_pwd_check_ntlmv1(&user_info->lm_resp,
293 nt_pw, &auth_context->challenge,
298 DEBUG(3,("sam_password_ok: LM password, NT MD4 password in LM field and LMv2 failed for user %s\n",pdb_get_username(sampass)));
299 return NT_STATUS_WRONG_PASSWORD;
301 DEBUG(3,("sam_password_ok: LM password and LMv2 failed for user %s, and NT MD4 password in LM field not permitted\n",pdb_get_username(sampass)));
302 return NT_STATUS_WRONG_PASSWORD;
307 /* Should not be reached, but if they send nothing... */
308 DEBUG(3,("sam_password_ok: NEITHER LanMan nor NT password supplied for user %s\n",pdb_get_username(sampass)));
309 return NT_STATUS_WRONG_PASSWORD;
312 /****************************************************************************
313 Do a specific test for a SAM_ACCOUNT being vaild for this connection
314 (ie not disabled, expired and the like).
315 ****************************************************************************/
316 static NTSTATUS sam_account_ok(TALLOC_CTX *mem_ctx,
317 SAM_ACCOUNT *sampass,
318 const auth_usersupplied_info *user_info)
320 uint16 acct_ctrl = pdb_get_acct_ctrl(sampass);
321 char *workstation_list;
324 DEBUG(4,("sam_account_ok: Checking SMB password for user %s\n",pdb_get_username(sampass)));
326 /* Quit if the account was disabled. */
327 if (acct_ctrl & ACB_DISABLED) {
328 DEBUG(1,("Account for user '%s' was disabled.\n", pdb_get_username(sampass)));
329 return NT_STATUS_ACCOUNT_DISABLED;
332 /* Test account expire time */
334 kickoff_time = pdb_get_kickoff_time(sampass);
335 if (kickoff_time != 0 && time(NULL) > kickoff_time) {
336 DEBUG(1,("Account for user '%s' has expired.\n", pdb_get_username(sampass)));
337 DEBUG(3,("Account expired at '%ld' unix time.\n", (long)kickoff_time));
338 return NT_STATUS_ACCOUNT_EXPIRED;
341 if (!(pdb_get_acct_ctrl(sampass) & ACB_PWNOEXP)) {
342 time_t must_change_time = pdb_get_pass_must_change_time(sampass);
343 time_t last_set_time = pdb_get_pass_last_set_time(sampass);
345 /* check for immediate expiry "must change at next logon" */
346 if (must_change_time == 0 && last_set_time != 0) {
347 DEBUG(1,("Account for user '%s' password must change!.\n", pdb_get_username(sampass)));
348 return NT_STATUS_PASSWORD_MUST_CHANGE;
351 /* check for expired password */
352 if (must_change_time < time(NULL) && must_change_time != 0) {
353 DEBUG(1,("Account for user '%s' password expired!.\n", pdb_get_username(sampass)));
354 DEBUG(1,("Password expired at '%s' (%ld) unix time.\n", http_timestring(must_change_time), (long)must_change_time));
355 return NT_STATUS_PASSWORD_EXPIRED;
359 /* Test workstation. Workstation list is comma separated. */
361 workstation_list = talloc_strdup(mem_ctx, pdb_get_workstations(sampass));
363 if (!workstation_list) return NT_STATUS_NO_MEMORY;
365 if (*workstation_list) {
366 BOOL invalid_ws = True;
367 const char *s = workstation_list;
371 while (next_token(&s, tok, ",", sizeof(tok))) {
372 DEBUG(10,("checking for workstation match %s and %s (len=%d)\n",
373 tok, user_info->wksta_name.str, user_info->wksta_name.len));
374 if(strequal(tok, user_info->wksta_name.str)) {
381 return NT_STATUS_INVALID_WORKSTATION;
384 if (acct_ctrl & ACB_DOMTRUST) {
385 DEBUG(2,("sam_account_ok: Domain trust account %s denied by server\n", pdb_get_username(sampass)));
386 return NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT;
389 if (acct_ctrl & ACB_SVRTRUST) {
390 DEBUG(2,("sam_account_ok: Server trust account %s denied by server\n", pdb_get_username(sampass)));
391 return NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT;
394 if (acct_ctrl & ACB_WSTRUST) {
395 DEBUG(4,("sam_account_ok: Wksta trust account %s denied by server\n", pdb_get_username(sampass)));
396 return NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT;
403 /****************************************************************************
404 check if a username/password is OK assuming the password is a 24 byte
405 SMB hash supplied in the user_info structure
406 return an NT_STATUS constant.
407 ****************************************************************************/
409 static NTSTATUS check_sam_security(const struct auth_context *auth_context,
410 void *my_private_data,
412 const auth_usersupplied_info *user_info,
413 auth_serversupplied_info **server_info)
415 SAM_ACCOUNT *sampass=NULL;
418 uint8 user_sess_key[16];
419 const uint8* lm_hash;
421 if (!user_info || !auth_context) {
422 return NT_STATUS_UNSUCCESSFUL;
425 /* Can't use the talloc version here, because the returned struct gets
426 kept on the server_info */
427 if (!NT_STATUS_IS_OK(nt_status = pdb_init_sam(&sampass))) {
431 /* get the account information */
434 ret = pdb_getsampwnam(sampass, user_info->internal_username.str);
439 DEBUG(3,("Couldn't find user '%s' in passdb file.\n", user_info->internal_username.str));
440 pdb_free_sam(&sampass);
441 return NT_STATUS_NO_SUCH_USER;
444 nt_status = sam_password_ok(auth_context, mem_ctx, sampass, user_info, user_sess_key);
446 if (!NT_STATUS_IS_OK(nt_status)) {
447 pdb_free_sam(&sampass);
451 nt_status = sam_account_ok(mem_ctx, sampass, user_info);
453 if (!NT_STATUS_IS_OK(nt_status)) {
454 pdb_free_sam(&sampass);
458 if (!NT_STATUS_IS_OK(nt_status = make_server_info_sam(server_info, sampass))) {
459 DEBUG(0,("check_sam_security: make_server_info_sam() failed with '%s'\n", nt_errstr(nt_status)));
463 lm_hash = pdb_get_lanman_passwd((*server_info)->sam_account);
465 memcpy((*server_info)->first_8_lm_hash, lm_hash, 8);
468 memcpy((*server_info)->session_key, user_sess_key, sizeof(user_sess_key));
473 /* module initialisation */
474 static NTSTATUS auth_init_sam_ignoredomain(struct auth_context *auth_context, const char *param, auth_methods **auth_method)
476 if (!make_auth_methods(auth_context, auth_method)) {
477 return NT_STATUS_NO_MEMORY;
480 (*auth_method)->auth = check_sam_security;
481 (*auth_method)->name = "sam_ignoredomain";
486 /****************************************************************************
487 Check SAM security (above) but with a few extra checks.
488 ****************************************************************************/
490 static NTSTATUS check_samstrict_security(const struct auth_context *auth_context,
491 void *my_private_data,
493 const auth_usersupplied_info *user_info,
494 auth_serversupplied_info **server_info)
496 BOOL is_local_name, is_my_domain;
498 if (!user_info || !auth_context) {
499 return NT_STATUS_LOGON_FAILURE;
502 is_local_name = is_myname(user_info->domain.str);
503 is_my_domain = strequal(user_info->domain.str, lp_workgroup());
505 /* check whether or not we service this domain/workgroup name */
507 switch ( lp_server_role() ) {
508 case ROLE_STANDALONE:
509 case ROLE_DOMAIN_MEMBER:
510 if ( !is_local_name ) {
511 DEBUG(6,("check_samstrict_security: %s is not one of my local names (%s)\n",
512 user_info->domain.str, (lp_server_role() == ROLE_DOMAIN_MEMBER
513 ? "ROLE_DOMAIN_MEMBER" : "ROLE_STANDALONE") ));
514 return NT_STATUS_NOT_IMPLEMENTED;
516 case ROLE_DOMAIN_PDC:
517 case ROLE_DOMAIN_BDC:
518 if ( !is_local_name && !is_my_domain ) {
519 DEBUG(6,("check_samstrict_security: %s is not one of my local names or domain name (DC)\n",
520 user_info->domain.str));
521 return NT_STATUS_NOT_IMPLEMENTED;
523 default: /* name is ok */
527 return check_sam_security(auth_context, my_private_data, mem_ctx, user_info, server_info);
530 /* module initialisation */
531 static NTSTATUS auth_init_sam(struct auth_context *auth_context, const char *param, auth_methods **auth_method)
533 if (!make_auth_methods(auth_context, auth_method)) {
534 return NT_STATUS_NO_MEMORY;
537 (*auth_method)->auth = check_samstrict_security;
538 (*auth_method)->name = "sam";
542 NTSTATUS auth_sam_init(void)
544 smb_register_auth(AUTH_INTERFACE_VERSION, "sam", auth_init_sam);
545 smb_register_auth(AUTH_INTERFACE_VERSION, "sam_ignoredomain", auth_init_sam_ignoredomain);