2 Unix SMB/CIFS implementation.
3 Password and authentication handling
4 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2001-2010
5 Copyright (C) Gerald Carter 2003
6 Copyright (C) Stefan Metzmacher 2005
7 Copyright (C) Matthias Dieter Wallnöfer 2009
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/>.
24 #include "system/time.h"
25 #include "auth/auth.h"
27 #include "dsdb/samdb/samdb.h"
28 #include "libcli/security/security.h"
29 #include "auth/auth_sam.h"
30 #include "dsdb/common/util.h"
31 #include "libcli/ldap/ldap_ndr.h"
32 #include "param/param.h"
34 #define KRBTGT_ATTRS \
35 /* required for the krb5 kdc */ \
38 "userPrincipalName", \
39 "servicePrincipalName", \
40 "msDS-KeyVersionNumber", \
41 "msDS-SecondaryKrbTgtNumber", \
42 "msDS-SupportedEncryptionTypes", \
43 "supplementalCredentials", \
44 "msDS-AllowedToDelegateTo", \
50 "userAccountControl", \
51 "msDS-User-Account-Control-Computed", \
57 const char *krbtgt_attrs
[] = {
61 const char *server_attrs
[] = {
65 const char *user_attrs
[] = {
71 * To allow us to zero the badPwdCount and lockoutTime on
72 * successful logon, without database churn
76 /* check 'allowed workstations' */
79 /* required for user_info_dc, not access control: */
96 /****************************************************************************
97 Check if a user is allowed to logon at this time. Note this is the
98 servers local time, as logon hours are just specified as a weekly
100 ****************************************************************************/
102 static bool logon_hours_ok(struct ldb_message
*msg
, const char *name_for_logs
)
104 /* In logon hours first bit is Sunday from 12AM to 1AM */
105 const struct ldb_val
*hours
;
109 uint8_t bitmask
, bitpos
;
111 hours
= ldb_msg_find_ldb_val(msg
, "logonHours");
113 DEBUG(5,("logon_hours_ok: No hours restrictions for user %s\n", name_for_logs
));
117 if (hours
->length
!= 168/8) {
118 DEBUG(5,("logon_hours_ok: malformed logon hours restrictions for user %s\n", name_for_logs
));
122 lasttime
= time(NULL
);
123 utctime
= gmtime(&lasttime
);
125 DEBUG(1, ("logon_hours_ok: failed to get gmtime. Failing logon for user %s\n",
130 /* find the corresponding byte and bit */
131 bitpos
= (utctime
->tm_wday
* 24 + utctime
->tm_hour
) % 168;
132 bitmask
= 1 << (bitpos
% 8);
134 if (! (hours
->data
[bitpos
/8] & bitmask
)) {
135 struct tm
*t
= localtime(&lasttime
);
137 asct
= "INVALID TIME";
141 asct
= "INVALID TIME";
145 DEBUG(1, ("logon_hours_ok: Account for user %s not allowed to "
146 "logon at this time (%s).\n",
147 name_for_logs
, asct
));
151 asct
= asctime(utctime
);
152 DEBUG(5,("logon_hours_ok: user %s allowed to logon at this time (%s)\n",
153 name_for_logs
, asct
? asct
: "UNKNOWN TIME" ));
158 /****************************************************************************
159 Do a specific test for a SAM_ACCOUNT being valid for this connection
160 (ie not disabled, expired and the like).
161 ****************************************************************************/
162 _PUBLIC_ NTSTATUS
authsam_account_ok(TALLOC_CTX
*mem_ctx
,
163 struct ldb_context
*sam_ctx
,
164 uint32_t logon_parameters
,
165 struct ldb_dn
*domain_dn
,
166 struct ldb_message
*msg
,
167 const char *logon_workstation
,
168 const char *name_for_logs
,
169 bool allow_domain_trust
,
170 bool password_change
)
173 const char *workstation_list
;
175 NTTIME must_change_time
;
176 struct timeval tv_now
= timeval_current();
177 NTTIME now
= timeval_to_nttime(&tv_now
);
179 DEBUG(4,("authsam_account_ok: Checking SMB password for user %s\n", name_for_logs
));
181 acct_flags
= samdb_result_acct_flags(msg
, "msDS-User-Account-Control-Computed");
183 acct_expiry
= samdb_result_account_expires(msg
);
185 /* Check for when we must change this password, taking the
186 * userAccountControl flags into account */
187 must_change_time
= samdb_result_force_password_change(sam_ctx
, mem_ctx
,
190 workstation_list
= ldb_msg_find_attr_as_string(msg
, "userWorkstations", NULL
);
192 /* Quit if the account was disabled. */
193 if (acct_flags
& ACB_DISABLED
) {
194 DEBUG(2,("authsam_account_ok: Account for user '%s' was disabled.\n", name_for_logs
));
195 return NT_STATUS_ACCOUNT_DISABLED
;
198 /* Quit if the account was locked out. */
199 if (acct_flags
& ACB_AUTOLOCK
) {
200 DEBUG(2,("authsam_account_ok: Account for user %s was locked out.\n", name_for_logs
));
201 return NT_STATUS_ACCOUNT_LOCKED_OUT
;
204 /* Test account expire time */
205 if (now
> acct_expiry
) {
206 DEBUG(2,("authsam_account_ok: Account for user '%s' has expired.\n", name_for_logs
));
207 DEBUG(3,("authsam_account_ok: Account expired at '%s'.\n",
208 nt_time_string(mem_ctx
, acct_expiry
)));
209 return NT_STATUS_ACCOUNT_EXPIRED
;
212 /* check for immediate expiry "must change at next logon" (but not if this is a password change request) */
213 if ((must_change_time
== 0) && !password_change
) {
214 DEBUG(2,("sam_account_ok: Account for user '%s' password must change!.\n",
216 return NT_STATUS_PASSWORD_MUST_CHANGE
;
219 /* check for expired password (but not if this is a password change request) */
220 if ((must_change_time
< now
) && !password_change
) {
221 DEBUG(2,("sam_account_ok: Account for user '%s' password expired!.\n",
223 DEBUG(2,("sam_account_ok: Password expired at '%s' unix time.\n",
224 nt_time_string(mem_ctx
, must_change_time
)));
225 return NT_STATUS_PASSWORD_EXPIRED
;
228 /* Test workstation. Workstation list is comma separated. */
229 if (logon_workstation
&& workstation_list
&& *workstation_list
) {
230 bool invalid_ws
= true;
232 char **workstations
= str_list_make(mem_ctx
, workstation_list
, ",");
234 for (i
= 0; workstations
&& workstations
[i
]; i
++) {
235 DEBUG(10,("sam_account_ok: checking for workstation match '%s' and '%s'\n",
236 workstations
[i
], logon_workstation
));
238 if (strequal(workstations
[i
], logon_workstation
)) {
244 talloc_free(workstations
);
247 return NT_STATUS_INVALID_WORKSTATION
;
251 if (!logon_hours_ok(msg
, name_for_logs
)) {
252 return NT_STATUS_INVALID_LOGON_HOURS
;
255 if (!allow_domain_trust
) {
256 if (acct_flags
& ACB_DOMTRUST
) {
257 DEBUG(2,("sam_account_ok: Domain trust account %s denied by server\n", name_for_logs
));
258 return NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT
;
261 if (!(logon_parameters
& MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT
)) {
262 if (acct_flags
& ACB_SVRTRUST
) {
263 DEBUG(2,("sam_account_ok: Server trust account %s denied by server\n", name_for_logs
));
264 return NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT
;
267 if (!(logon_parameters
& MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT
)) {
268 /* TODO: this fails with current solaris client. We
269 need to work with Gordon to work out why */
270 if (acct_flags
& ACB_WSTRUST
) {
271 DEBUG(4,("sam_account_ok: Wksta trust account %s denied by server\n", name_for_logs
));
272 return NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT
;
279 _PUBLIC_ NTSTATUS
authsam_make_user_info_dc(TALLOC_CTX
*mem_ctx
,
280 struct ldb_context
*sam_ctx
,
281 const char *netbios_name
,
282 const char *domain_name
,
283 struct ldb_dn
*domain_dn
,
284 struct ldb_message
*msg
,
285 DATA_BLOB user_sess_key
,
286 DATA_BLOB lm_sess_key
,
287 struct auth_user_info_dc
**_user_info_dc
)
290 struct auth_user_info_dc
*user_info_dc
;
291 struct auth_user_info
*info
;
292 const char *str
, *filter
;
293 /* SIDs for the account and his primary group */
294 struct dom_sid
*account_sid
;
295 const char *primary_group_string
;
296 const char *primary_group_dn
;
297 DATA_BLOB primary_group_blob
;
298 /* SID structures for the expanded group memberships */
299 struct dom_sid
*sids
= NULL
;
300 unsigned int num_sids
= 0, i
;
301 struct dom_sid
*domain_sid
;
303 struct ldb_message_element
*el
;
305 user_info_dc
= talloc(mem_ctx
, struct auth_user_info_dc
);
306 NT_STATUS_HAVE_NO_MEMORY(user_info_dc
);
308 tmp_ctx
= talloc_new(user_info_dc
);
309 if (user_info_dc
== NULL
) {
310 TALLOC_FREE(user_info_dc
);
311 return NT_STATUS_NO_MEMORY
;
314 sids
= talloc_array(user_info_dc
, struct dom_sid
, 2);
316 TALLOC_FREE(user_info_dc
);
317 return NT_STATUS_NO_MEMORY
;
322 account_sid
= samdb_result_dom_sid(user_info_dc
, msg
, "objectSid");
323 if (account_sid
== NULL
) {
324 TALLOC_FREE(user_info_dc
);
325 return NT_STATUS_NO_MEMORY
;
328 status
= dom_sid_split_rid(tmp_ctx
, account_sid
, &domain_sid
, NULL
);
329 if (!NT_STATUS_IS_OK(status
)) {
330 talloc_free(user_info_dc
);
334 sids
[PRIMARY_USER_SID_INDEX
] = *account_sid
;
335 sids
[PRIMARY_GROUP_SID_INDEX
] = *domain_sid
;
336 sid_append_rid(&sids
[PRIMARY_GROUP_SID_INDEX
], ldb_msg_find_attr_as_uint(msg
, "primaryGroupID", ~0));
338 /* Filter out builtin groups from this token. We will search
339 * for builtin groups later, and not include them in the PAC
340 * on SamLogon validation info */
341 filter
= talloc_asprintf(tmp_ctx
, "(&(objectClass=group)(!(groupType:1.2.840.113556.1.4.803:=%u))(groupType:1.2.840.113556.1.4.803:=%u))", GROUP_TYPE_BUILTIN_LOCAL_GROUP
, GROUP_TYPE_SECURITY_ENABLED
);
342 if (filter
== NULL
) {
343 TALLOC_FREE(user_info_dc
);
344 return NT_STATUS_NO_MEMORY
;
347 primary_group_string
= dom_sid_string(tmp_ctx
, &sids
[PRIMARY_GROUP_SID_INDEX
]);
348 if (primary_group_string
== NULL
) {
349 TALLOC_FREE(user_info_dc
);
350 return NT_STATUS_NO_MEMORY
;
353 primary_group_dn
= talloc_asprintf(tmp_ctx
, "<SID=%s>", primary_group_string
);
354 if (primary_group_dn
== NULL
) {
355 TALLOC_FREE(user_info_dc
);
356 return NT_STATUS_NO_MEMORY
;
359 primary_group_blob
= data_blob_string_const(primary_group_dn
);
361 /* Expands the primary group - this function takes in
362 * memberOf-like values, so we fake one up with the
363 * <SID=S-...> format of DN and then let it expand
364 * them, as long as they meet the filter - so only
365 * domain groups, not builtin groups
367 * The primary group is still treated specially, so we set the
368 * 'only childs' flag to true
370 status
= dsdb_expand_nested_groups(sam_ctx
, &primary_group_blob
, true, filter
,
371 user_info_dc
, &sids
, &num_sids
);
372 if (!NT_STATUS_IS_OK(status
)) {
373 talloc_free(user_info_dc
);
377 /* Expands the additional groups */
378 el
= ldb_msg_find_element(msg
, "memberOf");
379 for (i
= 0; el
&& i
< el
->num_values
; i
++) {
380 /* This function takes in memberOf values and expands
381 * them, as long as they meet the filter - so only
382 * domain groups, not builtin groups */
383 status
= dsdb_expand_nested_groups(sam_ctx
, &el
->values
[i
], false, filter
,
384 user_info_dc
, &sids
, &num_sids
);
385 if (!NT_STATUS_IS_OK(status
)) {
386 talloc_free(user_info_dc
);
391 user_info_dc
->sids
= sids
;
392 user_info_dc
->num_sids
= num_sids
;
394 user_info_dc
->info
= info
= talloc_zero(user_info_dc
, struct auth_user_info
);
395 NT_STATUS_HAVE_NO_MEMORY(user_info_dc
->info
);
397 info
->account_name
= talloc_steal(info
,
398 ldb_msg_find_attr_as_string(msg
, "sAMAccountName", NULL
));
400 info
->domain_name
= talloc_strdup(info
, domain_name
);
401 if (info
->domain_name
== NULL
) {
402 TALLOC_FREE(user_info_dc
);
403 return NT_STATUS_NO_MEMORY
;
406 str
= ldb_msg_find_attr_as_string(msg
, "displayName", "");
407 info
->full_name
= talloc_strdup(info
, str
);
408 if (info
->full_name
== NULL
) {
409 TALLOC_FREE(user_info_dc
);
410 return NT_STATUS_NO_MEMORY
;
413 str
= ldb_msg_find_attr_as_string(msg
, "scriptPath", "");
414 info
->logon_script
= talloc_strdup(info
, str
);
415 if (info
->logon_script
== NULL
) {
416 TALLOC_FREE(user_info_dc
);
417 return NT_STATUS_NO_MEMORY
;
420 str
= ldb_msg_find_attr_as_string(msg
, "profilePath", "");
421 info
->profile_path
= talloc_strdup(info
, str
);
422 if (info
->profile_path
== NULL
) {
423 TALLOC_FREE(user_info_dc
);
424 return NT_STATUS_NO_MEMORY
;
427 str
= ldb_msg_find_attr_as_string(msg
, "homeDirectory", "");
428 info
->home_directory
= talloc_strdup(info
, str
);
429 if (info
->home_directory
== NULL
) {
430 TALLOC_FREE(user_info_dc
);
431 return NT_STATUS_NO_MEMORY
;
434 str
= ldb_msg_find_attr_as_string(msg
, "homeDrive", "");
435 info
->home_drive
= talloc_strdup(info
, str
);
436 if (info
->home_drive
== NULL
) {
437 TALLOC_FREE(user_info_dc
);
438 return NT_STATUS_NO_MEMORY
;
441 info
->logon_server
= talloc_strdup(info
, netbios_name
);
442 if (info
->logon_server
== NULL
) {
443 TALLOC_FREE(user_info_dc
);
444 return NT_STATUS_NO_MEMORY
;
447 info
->last_logon
= samdb_result_nttime(msg
, "lastLogon", 0);
448 info
->last_logoff
= samdb_result_last_logoff(msg
);
449 info
->acct_expiry
= samdb_result_account_expires(msg
);
450 info
->last_password_change
= samdb_result_nttime(msg
,
452 info
->allow_password_change
453 = samdb_result_allow_password_change(sam_ctx
, mem_ctx
,
454 domain_dn
, msg
, "pwdLastSet");
455 info
->force_password_change
456 = samdb_result_force_password_change(sam_ctx
, mem_ctx
,
458 info
->logon_count
= ldb_msg_find_attr_as_uint(msg
, "logonCount", 0);
459 info
->bad_password_count
= ldb_msg_find_attr_as_uint(msg
, "badPwdCount",
462 info
->acct_flags
= samdb_result_acct_flags(msg
, "msDS-User-Account-Control-Computed");
464 user_info_dc
->user_session_key
= data_blob_talloc(user_info_dc
,
466 user_sess_key
.length
);
467 if (user_sess_key
.data
) {
468 if (user_info_dc
->user_session_key
.data
== NULL
) {
469 TALLOC_FREE(user_info_dc
);
470 return NT_STATUS_NO_MEMORY
;
473 user_info_dc
->lm_session_key
= data_blob_talloc(user_info_dc
,
476 if (lm_sess_key
.data
) {
477 if (user_info_dc
->lm_session_key
.data
== NULL
) {
478 TALLOC_FREE(user_info_dc
);
479 return NT_STATUS_NO_MEMORY
;
483 if (info
->acct_flags
& ACB_SVRTRUST
) {
484 /* the SID_NT_ENTERPRISE_DCS SID gets added into the
486 user_info_dc
->sids
= talloc_realloc(user_info_dc
,
489 user_info_dc
->num_sids
+1);
490 if (user_info_dc
->sids
== NULL
) {
491 TALLOC_FREE(user_info_dc
);
492 return NT_STATUS_NO_MEMORY
;
494 user_info_dc
->sids
[user_info_dc
->num_sids
] = global_sid_Enterprise_DCs
;
495 user_info_dc
->num_sids
++;
498 if ((info
->acct_flags
& (ACB_PARTIAL_SECRETS_ACCOUNT
| ACB_WSTRUST
)) ==
499 (ACB_PARTIAL_SECRETS_ACCOUNT
| ACB_WSTRUST
)) {
500 /* the DOMAIN_RID_ENTERPRISE_READONLY_DCS PAC */
501 user_info_dc
->sids
= talloc_realloc(user_info_dc
,
504 user_info_dc
->num_sids
+1);
505 if (user_info_dc
->sids
== NULL
) {
506 TALLOC_FREE(user_info_dc
);
507 return NT_STATUS_NO_MEMORY
;
509 user_info_dc
->sids
[user_info_dc
->num_sids
] = *domain_sid
;
510 sid_append_rid(&user_info_dc
->sids
[user_info_dc
->num_sids
],
511 DOMAIN_RID_ENTERPRISE_READONLY_DCS
);
512 user_info_dc
->num_sids
++;
515 info
->authenticated
= true;
517 talloc_free(tmp_ctx
);
518 *_user_info_dc
= user_info_dc
;
523 NTSTATUS
sam_get_results_principal(struct ldb_context
*sam_ctx
,
524 TALLOC_CTX
*mem_ctx
, const char *principal
,
526 struct ldb_dn
**domain_dn
,
527 struct ldb_message
**msg
)
529 struct ldb_dn
*user_dn
;
531 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
535 return NT_STATUS_NO_MEMORY
;
538 nt_status
= crack_user_principal_name(sam_ctx
, tmp_ctx
, principal
,
539 &user_dn
, domain_dn
);
540 if (!NT_STATUS_IS_OK(nt_status
)) {
541 talloc_free(tmp_ctx
);
545 /* pull the user attributes */
546 ret
= dsdb_search_one(sam_ctx
, tmp_ctx
, msg
, user_dn
,
547 LDB_SCOPE_BASE
, attrs
,
548 DSDB_SEARCH_SHOW_EXTENDED_DN
| DSDB_SEARCH_NO_GLOBAL_CATALOG
,
550 if (ret
!= LDB_SUCCESS
) {
551 talloc_free(tmp_ctx
);
552 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
554 talloc_steal(mem_ctx
, *msg
);
555 talloc_steal(mem_ctx
, *domain_dn
);
556 talloc_free(tmp_ctx
);
561 /* Used in the gensec_gssapi and gensec_krb5 server-side code, where the PAC isn't available, and for tokenGroups in the DSDB stack.
563 Supply either a principal or a DN
565 NTSTATUS
authsam_get_user_info_dc_principal(TALLOC_CTX
*mem_ctx
,
566 struct loadparm_context
*lp_ctx
,
567 struct ldb_context
*sam_ctx
,
568 const char *principal
,
569 struct ldb_dn
*user_dn
,
570 struct auth_user_info_dc
**user_info_dc
)
573 DATA_BLOB user_sess_key
= data_blob(NULL
, 0);
574 DATA_BLOB lm_sess_key
= data_blob(NULL
, 0);
576 struct ldb_message
*msg
;
577 struct ldb_dn
*domain_dn
;
579 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
581 return NT_STATUS_NO_MEMORY
;
585 nt_status
= sam_get_results_principal(sam_ctx
, tmp_ctx
, principal
,
586 user_attrs
, &domain_dn
, &msg
);
587 if (!NT_STATUS_IS_OK(nt_status
)) {
588 talloc_free(tmp_ctx
);
591 } else if (user_dn
) {
592 struct dom_sid
*user_sid
, *domain_sid
;
594 /* pull the user attributes */
595 ret
= dsdb_search_one(sam_ctx
, tmp_ctx
, &msg
, user_dn
,
596 LDB_SCOPE_BASE
, user_attrs
,
597 DSDB_SEARCH_SHOW_EXTENDED_DN
| DSDB_SEARCH_NO_GLOBAL_CATALOG
,
599 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
600 talloc_free(tmp_ctx
);
601 return NT_STATUS_NO_SUCH_USER
;
602 } else if (ret
!= LDB_SUCCESS
) {
603 talloc_free(tmp_ctx
);
604 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
607 user_sid
= samdb_result_dom_sid(msg
, msg
, "objectSid");
609 nt_status
= dom_sid_split_rid(tmp_ctx
, user_sid
, &domain_sid
, NULL
);
610 if (!NT_STATUS_IS_OK(nt_status
)) {
614 domain_dn
= samdb_search_dn(sam_ctx
, mem_ctx
, NULL
,
615 "(&(objectSid=%s)(objectClass=domain))",
616 ldap_encode_ndr_dom_sid(tmp_ctx
, domain_sid
));
618 DEBUG(3, ("authsam_get_user_info_dc_principal: Failed to find domain with: SID %s\n",
619 dom_sid_string(tmp_ctx
, domain_sid
)));
620 return NT_STATUS_NO_SUCH_USER
;
624 return NT_STATUS_INVALID_PARAMETER
;
627 nt_status
= authsam_make_user_info_dc(tmp_ctx
, sam_ctx
,
628 lpcfg_netbios_name(lp_ctx
),
629 lpcfg_workgroup(lp_ctx
),
632 user_sess_key
, lm_sess_key
,
634 if (!NT_STATUS_IS_OK(nt_status
)) {
635 talloc_free(tmp_ctx
);
639 talloc_steal(mem_ctx
, *user_info_dc
);
640 talloc_free(tmp_ctx
);
645 NTSTATUS
authsam_update_bad_pwd_count(struct ldb_context
*sam_ctx
,
646 struct ldb_message
*msg
,
647 struct ldb_dn
*domain_dn
)
649 const char *attrs
[] = { "lockoutThreshold",
650 "lockOutObservationWindow",
654 int ret
, badPwdCount
;
655 int64_t lockoutThreshold
, lockOutObservationWindow
, badPasswordTime
;
657 struct ldb_result
*domain_res
;
658 struct ldb_message
*msg_mod
;
659 struct timeval tv_now
= timeval_current();
660 NTTIME now
= timeval_to_nttime(&tv_now
);
662 uint32_t pwdProperties
, rid
= 0;
665 mem_ctx
= talloc_new(msg
);
666 if (mem_ctx
== NULL
) {
667 return NT_STATUS_NO_MEMORY
;
670 sid
= samdb_result_dom_sid(mem_ctx
, msg
, "objectSid");
672 ret
= dsdb_search_dn(sam_ctx
, mem_ctx
, &domain_res
, domain_dn
, attrs
, 0);
673 if (ret
!= LDB_SUCCESS
) {
674 TALLOC_FREE(mem_ctx
);
675 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
678 pwdProperties
= ldb_msg_find_attr_as_uint(domain_res
->msgs
[0],
679 "pwdProperties", -1);
680 if (sid
&& !(pwdProperties
& DOMAIN_PASSWORD_LOCKOUT_ADMINS
)) {
681 status
= dom_sid_split_rid(NULL
, sid
, NULL
, &rid
);
682 if (!NT_STATUS_IS_OK(status
)) {
684 * This can't happen anyway, but always try
685 * and update the badPwdCount on failure
692 * Work out if we are doing password lockout on the domain.
693 * Also, the built in administrator account is exempt:
694 * http://msdn.microsoft.com/en-us/library/windows/desktop/aa375371%28v=vs.85%29.aspx
696 lockoutThreshold
= ldb_msg_find_attr_as_int(domain_res
->msgs
[0],
697 "lockoutThreshold", 0);
698 if (lockoutThreshold
== 0 || (rid
== DOMAIN_RID_ADMINISTRATOR
)) {
699 DEBUG(5, ("Not updating badPwdCount on %s after wrong password\n",
700 ldb_dn_get_linearized(msg
->dn
)));
701 TALLOC_FREE(mem_ctx
);
705 lockOutObservationWindow
= ldb_msg_find_attr_as_int64(domain_res
->msgs
[0],
706 "lockOutObservationWindow", 0);
708 badPasswordTime
= ldb_msg_find_attr_as_int64(msg
, "badPasswordTime", 0);
710 msg_mod
= ldb_msg_new(mem_ctx
);
711 if (msg_mod
== NULL
) {
712 TALLOC_FREE(mem_ctx
);
713 return NT_STATUS_NO_MEMORY
;
715 msg_mod
->dn
= msg
->dn
;
717 if (badPasswordTime
- lockOutObservationWindow
>= now
) {
718 badPwdCount
= ldb_msg_find_attr_as_int(msg
, "badPwdCount", 0);
725 ret
= samdb_msg_add_int(sam_ctx
, msg_mod
, msg_mod
, "badPwdCount", badPwdCount
);
726 if (ret
!= LDB_SUCCESS
) {
727 TALLOC_FREE(mem_ctx
);
728 return NT_STATUS_NO_MEMORY
;
730 ret
= samdb_msg_add_int64(sam_ctx
, msg_mod
, msg_mod
, "badPasswordTime", now
);
731 if (ret
!= LDB_SUCCESS
) {
732 TALLOC_FREE(mem_ctx
);
733 return NT_STATUS_NO_MEMORY
;
736 if (badPwdCount
>= lockoutThreshold
) {
737 ret
= samdb_msg_add_int64(sam_ctx
, msg_mod
, msg_mod
, "lockoutTime", now
);
738 if (ret
!= LDB_SUCCESS
) {
739 TALLOC_FREE(mem_ctx
);
740 return NT_STATUS_NO_MEMORY
;
742 DEBUG(5, ("Locked out user %s after %d wrong passwords\n",
743 ldb_dn_get_linearized(msg
->dn
), badPwdCount
));
746 ret
= dsdb_replace(sam_ctx
, msg_mod
, 0);
747 if (ret
!= LDB_SUCCESS
) {
748 DEBUG(0, ("Failed to upate badPwdCount, badPasswordTime or set lockoutTime on %s: %s\n",
749 ldb_dn_get_linearized(msg_mod
->dn
), ldb_errstring(sam_ctx
)));
750 TALLOC_FREE(mem_ctx
);
751 return NT_STATUS_INTERNAL_ERROR
;
754 DEBUG(5, ("Updated badPwdCount on %s after %d wrong passwords\n",
755 ldb_dn_get_linearized(msg
->dn
), badPwdCount
));
757 TALLOC_FREE(mem_ctx
);
761 NTSTATUS
authsam_zero_bad_pwd_count(struct ldb_context
*sam_ctx
,
762 const struct ldb_message
*msg
)
767 struct ldb_message
*msg_mod
;
770 lockoutTime
= ldb_msg_find_attr_as_int64(msg
, "lockoutTime", 0);
771 badPwdCount
= ldb_msg_find_attr_as_int(msg
, "badPwdCount", 0);
772 if (lockoutTime
== 0 && badPwdCount
== 0) {
776 mem_ctx
= talloc_new(msg
);
777 if (mem_ctx
== NULL
) {
778 return NT_STATUS_NO_MEMORY
;
780 msg_mod
= ldb_msg_new(mem_ctx
);
781 if (msg_mod
== NULL
) {
782 TALLOC_FREE(mem_ctx
);
783 return NT_STATUS_NO_MEMORY
;
785 msg_mod
->dn
= msg
->dn
;
787 if (lockoutTime
!= 0) {
789 * This implies "badPwdCount" = 0, see samldb_lockout_time()
791 ret
= samdb_msg_add_int(sam_ctx
, msg_mod
, msg_mod
, "lockoutTime", 0);
792 if (ret
!= LDB_SUCCESS
) {
793 TALLOC_FREE(mem_ctx
);
794 return NT_STATUS_NO_MEMORY
;
797 ret
= samdb_msg_add_int(sam_ctx
, msg_mod
, msg_mod
, "badPwdCount", 0);
798 if (ret
!= LDB_SUCCESS
) {
799 TALLOC_FREE(mem_ctx
);
800 return NT_STATUS_NO_MEMORY
;
804 ret
= dsdb_replace(sam_ctx
, msg_mod
, 0);
805 if (ret
!= LDB_SUCCESS
) {
806 DEBUG(0, ("Failed to set badPwdCount and lockoutTime to 0 on %s: %s\n",
807 ldb_dn_get_linearized(msg_mod
->dn
), ldb_errstring(sam_ctx
)));
808 TALLOC_FREE(mem_ctx
);
809 return NT_STATUS_INTERNAL_ERROR
;
812 TALLOC_FREE(mem_ctx
);