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-2003
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 3 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, see <http://www.gnu.org/licenses/>.
25 #include "../libcli/auth/libcli_auth.h"
27 #include "lib/util/memcache.h"
30 #define DBGC_CLASS DBGC_AUTH
32 /****************************************************************************
33 Do a specific test for an smb password being correct, given a smb_password and
34 the lanman and NT responses.
35 ****************************************************************************/
37 static NTSTATUS
sam_password_ok(TALLOC_CTX
*mem_ctx
,
40 const DATA_BLOB
*challenge
,
43 const struct auth_usersupplied_info
*user_info
,
44 DATA_BLOB
*user_sess_key
,
45 DATA_BLOB
*lm_sess_key
)
48 struct samr_Password _lm_hash
, _nt_hash
;
49 struct samr_Password
*lm_hash
= NULL
;
50 struct samr_Password
*nt_hash
= NULL
;
52 *user_sess_key
= data_blob_null
;
53 *lm_sess_key
= data_blob_null
;
55 if (acct_ctrl
& ACB_PWNOTREQ
) {
56 if (lp_null_passwords()) {
57 DEBUG(3,("Account for user '%s' has no password and null passwords are allowed.\n", username
));
60 DEBUG(3,("Account for user '%s' has no password and null passwords are NOT allowed.\n", username
));
61 return NT_STATUS_LOGON_FAILURE
;
66 memcpy(_lm_hash
.hash
, lm_pw
, sizeof(_lm_hash
.hash
));
70 memcpy(_nt_hash
.hash
, nt_pw
, sizeof(_nt_hash
.hash
));
73 switch (user_info
->password_state
) {
74 case AUTH_PASSWORD_HASH
:
75 status
= hash_password_check(mem_ctx
, lp_lanman_auth(),
77 user_info
->password
.hash
.lanman
,
78 user_info
->password
.hash
.nt
,
82 if (NT_STATUS_IS_OK(status
)) {
84 *user_sess_key
= data_blob_talloc(mem_ctx
, NULL
, 16);
85 if (!user_sess_key
->data
) {
86 status
= NT_STATUS_NO_MEMORY
;
89 SMBsesskeygen_ntv1(nt_pw
, user_sess_key
->data
);
94 /* Eventually we should test plaintext passwords in their own
95 * function, not assuming the caller has done a
97 case AUTH_PASSWORD_PLAIN
:
98 case AUTH_PASSWORD_RESPONSE
:
99 status
= ntlm_password_check(mem_ctx
, lp_lanman_auth(),
101 user_info
->logon_parameters
,
103 &user_info
->password
.response
.lanman
, &user_info
->password
.response
.nt
,
105 user_info
->client
.account_name
,
106 user_info
->client
.domain_name
,
109 user_sess_key
, lm_sess_key
);
112 DEBUG(0,("user_info constructed for user '%s' was invalid - password_state=%u invalid.\n", username
, user_info
->password_state
));
113 status
= NT_STATUS_INTERNAL_ERROR
;
116 ZERO_STRUCTP(lm_hash
);
117 ZERO_STRUCTP(nt_hash
);
121 /****************************************************************************
122 Check if a user is allowed to logon at this time. Note this is the
123 servers local time, as logon hours are just specified as a weekly
125 ****************************************************************************/
127 static bool logon_hours_ok(struct samu
*sampass
)
129 /* In logon hours first bit is Sunday from 12AM to 1AM */
130 const uint8_t *hours
;
134 uint8_t bitmask
, bitpos
;
136 hours
= pdb_get_hours(sampass
);
138 DEBUG(5,("logon_hours_ok: No hours restrictions for user %s\n",pdb_get_username(sampass
)));
142 lasttime
= time(NULL
);
143 utctime
= gmtime(&lasttime
);
145 DEBUG(1, ("logon_hours_ok: failed to get gmtime. Failing logon for user %s\n",
146 pdb_get_username(sampass
) ));
150 /* find the corresponding byte and bit */
151 bitpos
= (utctime
->tm_wday
* 24 + utctime
->tm_hour
) % 168;
152 bitmask
= 1 << (bitpos
% 8);
154 if (! (hours
[bitpos
/8] & bitmask
)) {
155 struct tm
*t
= localtime(&lasttime
);
157 asct
= "INVALID TIME";
161 asct
= "INVALID TIME";
165 DEBUG(1, ("logon_hours_ok: Account for user %s not allowed to "
166 "logon at this time (%s).\n",
167 pdb_get_username(sampass
), asct
));
171 asct
= asctime(utctime
);
172 DEBUG(5,("logon_hours_ok: user %s allowed to logon at this time (%s)\n",
173 pdb_get_username(sampass
), asct
? asct
: "UNKNOWN TIME" ));
178 /****************************************************************************
179 Do a specific test for a struct samu being valid for this connection
180 (ie not disabled, expired and the like).
181 ****************************************************************************/
183 static NTSTATUS
sam_account_ok(TALLOC_CTX
*mem_ctx
,
184 struct samu
*sampass
,
185 const struct auth_usersupplied_info
*user_info
)
187 uint32_t acct_ctrl
= pdb_get_acct_ctrl(sampass
);
188 char *workstation_list
;
191 DEBUG(4,("sam_account_ok: Checking SMB password for user %s\n",pdb_get_username(sampass
)));
193 /* Quit if the account was disabled. */
194 if (acct_ctrl
& ACB_DISABLED
) {
195 DEBUG(1,("sam_account_ok: Account for user '%s' was disabled.\n", pdb_get_username(sampass
)));
196 return NT_STATUS_ACCOUNT_DISABLED
;
199 /* Quit if the account was locked out. */
200 if (acct_ctrl
& ACB_AUTOLOCK
) {
201 DEBUG(1,("sam_account_ok: Account for user %s was locked out.\n", pdb_get_username(sampass
)));
202 return NT_STATUS_ACCOUNT_LOCKED_OUT
;
205 /* Quit if the account is not allowed to logon at this time. */
206 if (! logon_hours_ok(sampass
)) {
207 return NT_STATUS_INVALID_LOGON_HOURS
;
210 /* Test account expire time */
212 kickoff_time
= pdb_get_kickoff_time(sampass
);
213 if (kickoff_time
!= 0 && time(NULL
) > kickoff_time
) {
214 DEBUG(1,("sam_account_ok: Account for user '%s' has expired.\n", pdb_get_username(sampass
)));
215 DEBUG(3,("sam_account_ok: Account expired at '%ld' unix time.\n", (long)kickoff_time
));
216 return NT_STATUS_ACCOUNT_EXPIRED
;
219 if (!(pdb_get_acct_ctrl(sampass
) & ACB_PWNOEXP
) && !(pdb_get_acct_ctrl(sampass
) & ACB_PWNOTREQ
)) {
220 time_t must_change_time
= pdb_get_pass_must_change_time(sampass
);
221 time_t last_set_time
= pdb_get_pass_last_set_time(sampass
);
223 /* check for immediate expiry "must change at next logon"
224 * for a user account. */
225 if (((acct_ctrl
& (ACB_WSTRUST
|ACB_SVRTRUST
)) == 0) && (last_set_time
== 0)) {
226 DEBUG(1,("sam_account_ok: Account for user '%s' password must change!\n", pdb_get_username(sampass
)));
227 return NT_STATUS_PASSWORD_MUST_CHANGE
;
230 /* check for expired password */
231 if (must_change_time
< time(NULL
) && must_change_time
!= 0) {
232 DEBUG(1,("sam_account_ok: Account for user '%s' password expired!\n", pdb_get_username(sampass
)));
233 DEBUG(1,("sam_account_ok: Password expired at '%s' (%ld) unix time.\n", http_timestring(talloc_tos(), must_change_time
), (long)must_change_time
));
234 return NT_STATUS_PASSWORD_EXPIRED
;
238 /* Test workstation. Workstation list is comma separated. */
240 workstation_list
= talloc_strdup(mem_ctx
, pdb_get_workstations(sampass
));
241 if (!workstation_list
)
242 return NT_STATUS_NO_MEMORY
;
244 if (*workstation_list
) {
245 bool invalid_ws
= True
;
247 const char *s
= workstation_list
;
248 char *machine_name
= talloc_asprintf(mem_ctx
, "%s$", user_info
->workstation_name
);
250 if (machine_name
== NULL
)
251 return NT_STATUS_NO_MEMORY
;
253 while (next_token_talloc(mem_ctx
, &s
, &tok
, ",")) {
254 DEBUG(10,("sam_account_ok: checking for workstation match %s and %s\n",
255 tok
, user_info
->workstation_name
));
256 if(strequal(tok
, user_info
->workstation_name
)) {
261 DEBUG(10,("sam_account_ok: checking for workstation %s in group: %s\n",
262 machine_name
, tok
+ 1));
263 if (user_in_group(machine_name
, tok
+ 1)) {
271 TALLOC_FREE(machine_name
);
274 return NT_STATUS_INVALID_WORKSTATION
;
277 if (acct_ctrl
& ACB_DOMTRUST
) {
278 DEBUG(2,("sam_account_ok: Domain trust account %s denied by server\n", pdb_get_username(sampass
)));
279 return NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT
;
282 if (acct_ctrl
& ACB_SVRTRUST
) {
283 if (!(user_info
->logon_parameters
& MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT
)) {
284 DEBUG(2,("sam_account_ok: Server trust account %s denied by server\n", pdb_get_username(sampass
)));
285 return NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT
;
289 if (acct_ctrl
& ACB_WSTRUST
) {
290 if (!(user_info
->logon_parameters
& MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT
)) {
291 DEBUG(2,("sam_account_ok: Wksta trust account %s denied by server\n", pdb_get_username(sampass
)));
292 return NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT
;
299 * Check whether the given password is one of the last two
300 * password history entries. If so, the bad pwcount should
301 * not be incremented even though the actual password check
304 static bool need_to_increment_bad_pw_count(
305 const DATA_BLOB
*challenge
,
306 struct samu
* sampass
,
307 const struct auth_usersupplied_info
*user_info
)
310 const uint8_t *pwhistory
;
311 uint32_t pwhistory_len
;
312 uint32_t policy_pwhistory_len
;
314 const char *username
;
315 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
318 pdb_get_account_policy(PDB_POLICY_PASSWORD_HISTORY
,
319 &policy_pwhistory_len
);
320 if (policy_pwhistory_len
== 0) {
324 pwhistory
= pdb_get_pw_history(sampass
, &pwhistory_len
);
325 if (!pwhistory
|| pwhistory_len
== 0) {
329 acct_ctrl
= pdb_get_acct_ctrl(sampass
);
330 username
= pdb_get_username(sampass
);
332 for (i
=1; i
< MIN(MIN(3, policy_pwhistory_len
), pwhistory_len
); i
++) {
334 const uint8_t *nt_pw
;
336 DATA_BLOB user_sess_key
= data_blob_null
;
337 DATA_BLOB lm_sess_key
= data_blob_null
;
339 salt
= &pwhistory
[i
*PW_HISTORY_ENTRY_LEN
];
340 nt_pw
= salt
+ PW_HISTORY_SALT_LEN
;
342 if (all_zero(nt_pw
, NT_HASH_LEN
)) {
343 /* skip zero password hash */
347 if (!all_zero(salt
, PW_HISTORY_SALT_LEN
)) {
348 /* skip nonzero salt (old format entry) */
352 status
= sam_password_ok(mem_ctx
,
356 user_info
, &user_sess_key
, &lm_sess_key
);
357 if (NT_STATUS_IS_OK(status
)) {
364 TALLOC_FREE(mem_ctx
);
368 /****************************************************************************
369 check if a username/password is OK assuming the password is a 24 byte
370 SMB hash supplied in the user_info structure
371 return an NT_STATUS constant.
372 ****************************************************************************/
374 NTSTATUS
check_sam_security(const DATA_BLOB
*challenge
,
376 const struct auth_usersupplied_info
*user_info
,
377 struct auth_serversupplied_info
**server_info
)
379 struct samu
*sampass
=NULL
;
382 NTSTATUS update_login_attempts_status
;
383 DATA_BLOB user_sess_key
= data_blob_null
;
384 DATA_BLOB lm_sess_key
= data_blob_null
;
385 bool updated_badpw
= False
;
386 const char *username
;
387 const uint8_t *nt_pw
;
388 const uint8_t *lm_pw
;
390 char *mutex_name_by_user
= NULL
;
391 struct named_mutex
*mtx
= NULL
;
393 /* the returned struct gets kept on the server_info, by means
394 of a steal further down */
396 sampass
= samu_new(mem_ctx
);
397 if (sampass
== NULL
) {
398 return NT_STATUS_NO_MEMORY
;
401 /* get the account information */
404 ret
= pdb_getsampwnam(sampass
, user_info
->mapped
.account_name
);
408 DEBUG(3,("check_sam_security: Couldn't find user '%s' in "
409 "passdb.\n", user_info
->mapped
.account_name
));
410 TALLOC_FREE(sampass
);
411 return NT_STATUS_NO_SUCH_USER
;
414 acct_ctrl
= pdb_get_acct_ctrl(sampass
);
415 username
= pdb_get_username(sampass
);
416 nt_pw
= pdb_get_nt_passwd(sampass
);
417 lm_pw
= pdb_get_lanman_passwd(sampass
);
419 /* Quit if the account was locked out. */
420 if (acct_ctrl
& ACB_AUTOLOCK
) {
421 DEBUG(3,("check_sam_security: Account for user %s was locked out.\n", username
));
422 TALLOC_FREE(sampass
);
423 return NT_STATUS_ACCOUNT_LOCKED_OUT
;
426 nt_status
= sam_password_ok(mem_ctx
,
428 challenge
, lm_pw
, nt_pw
,
429 user_info
, &user_sess_key
, &lm_sess_key
);
432 * We must re-load the sam account information under a mutex
433 * lock to ensure we don't miss any concurrent account lockout
437 /* Clear out old sampass info. */
438 TALLOC_FREE(sampass
);
444 sampass
= samu_new(mem_ctx
);
445 if (sampass
== NULL
) {
446 return NT_STATUS_NO_MEMORY
;
449 mutex_name_by_user
= talloc_asprintf(mem_ctx
,
450 "check_sam_security_mutex_%s",
451 user_info
->mapped
.account_name
);
452 if (mutex_name_by_user
== NULL
) {
453 nt_status
= NT_STATUS_NO_MEMORY
;
457 /* Grab the named mutex under root with 30 second timeout. */
459 mtx
= grab_named_mutex(mem_ctx
, mutex_name_by_user
, 30);
461 /* Re-load the account information if we got the mutex. */
462 ret
= pdb_getsampwnam(sampass
, user_info
->mapped
.account_name
);
466 /* Everything from here on until mtx is freed is done under the mutex.*/
469 DBG_ERR("Acquisition of mutex %s failed "
472 user_info
->mapped
.account_name
);
473 nt_status
= NT_STATUS_INTERNAL_ERROR
;
479 * Re-load of account failed. This could only happen if the
480 * user was deleted in the meantime.
482 DBG_NOTICE("reload of user '%s' in passdb failed.\n",
483 user_info
->mapped
.account_name
);
484 nt_status
= NT_STATUS_NO_SUCH_USER
;
488 /* Re-load the account control info. */
489 acct_ctrl
= pdb_get_acct_ctrl(sampass
);
490 username
= pdb_get_username(sampass
);
493 * Check if the account is now locked out - now under the mutex.
494 * This can happen if the server is under
495 * a password guess attack and the ACB_AUTOLOCK is set by
498 if (acct_ctrl
& ACB_AUTOLOCK
) {
499 DBG_NOTICE("Account for user %s was locked out.\n", username
);
500 nt_status
= NT_STATUS_ACCOUNT_LOCKED_OUT
;
504 /* Notify passdb backend of login success/failure. If not
505 NT_STATUS_OK the backend doesn't like the login */
507 update_login_attempts_status
= pdb_update_login_attempts(sampass
, NT_STATUS_IS_OK(nt_status
));
509 if (!NT_STATUS_IS_OK(nt_status
)) {
510 bool increment_bad_pw_count
= false;
512 if (NT_STATUS_EQUAL(nt_status
,NT_STATUS_WRONG_PASSWORD
) &&
513 (acct_ctrl
& ACB_NORMAL
) &&
514 NT_STATUS_IS_OK(update_login_attempts_status
))
516 increment_bad_pw_count
=
517 need_to_increment_bad_pw_count(
518 challenge
, sampass
, user_info
);
521 if (increment_bad_pw_count
) {
522 pdb_increment_bad_password_count(sampass
);
523 updated_badpw
= True
;
525 pdb_update_bad_password_count(sampass
,
532 status
= pdb_update_sam_account(sampass
);
535 if (!NT_STATUS_IS_OK(status
)) {
536 DEBUG(1, ("Failed to modify entry: %s\n",
544 * We must only reset the bad password count if the login was
545 * successful, including checking account policies
547 nt_status
= sam_account_ok(mem_ctx
, sampass
, user_info
);
548 if (!NT_STATUS_IS_OK(nt_status
)) {
552 if ((acct_ctrl
& ACB_NORMAL
) &&
553 (pdb_get_bad_password_count(sampass
) > 0)){
556 pdb_set_bad_password_count(sampass
, 0, PDB_CHANGED
);
557 pdb_set_bad_password_time(sampass
, 0, PDB_CHANGED
);
560 status
= pdb_update_sam_account(sampass
);
563 if (!NT_STATUS_IS_OK(status
)) {
564 DEBUG(1, ("Failed to modify entry: %s\n",
570 nt_status
= make_server_info_sam(mem_ctx
, sampass
, server_info
);
573 if (!NT_STATUS_IS_OK(nt_status
)) {
574 DEBUG(0,("check_sam_security: make_server_info_sam() failed with '%s'\n", nt_errstr(nt_status
)));
578 (*server_info
)->info3
->base
.user_flags
|= NETLOGON_NTLMV2_ENABLED
;
580 (*server_info
)->session_key
=
581 data_blob_talloc(*server_info
, user_sess_key
.data
,
582 user_sess_key
.length
);
583 data_blob_free(&user_sess_key
);
585 (*server_info
)->lm_session_key
=
586 data_blob_talloc(*server_info
, lm_sess_key
.data
,
588 data_blob_free(&lm_sess_key
);
590 (*server_info
)->nss_token
|= user_info
->was_mapped
;
594 * Always flush the getpwsid cache or this will grow indefinitely for
595 * each NTLM auththentication.
597 memcache_flush(NULL
, PDB_GETPWSID_CACHE
);
598 TALLOC_FREE(sampass
);
599 data_blob_free(&user_sess_key
);
600 data_blob_free(&lm_sess_key
);
601 TALLOC_FREE(mutex_name_by_user
);
606 /* This helper function for winbindd returns a very similar value to
607 * what a NETLOGON call would give, without the indirection */
608 NTSTATUS
check_sam_security_info3(const DATA_BLOB
*challenge
,
610 const struct auth_usersupplied_info
*user_info
,
611 struct netr_SamInfo3
**pinfo3
)
613 struct auth_serversupplied_info
*server_info
= NULL
;
614 struct netr_SamInfo3
*info3
;
616 TALLOC_CTX
*frame
= talloc_stackframe();
618 status
= check_sam_security(challenge
, talloc_tos(), user_info
,
620 if (!NT_STATUS_IS_OK(status
)) {
621 DEBUG(10, ("check_sam_security failed: %s\n",
626 info3
= talloc_zero(mem_ctx
, struct netr_SamInfo3
);
628 status
= NT_STATUS_NO_MEMORY
;
632 status
= serverinfo_to_SamInfo3(server_info
, info3
);
633 if (!NT_STATUS_IS_OK(status
)) {
634 DEBUG(10, ("serverinfo_to_SamInfo3 failed: %s\n",
639 status
= NT_STATUS_OK
;