dsdb: Move dsdb_update_bad_pwd_count to dsdb/common/util.c
[Samba.git] / source4 / auth / sam.c
blobe4df612e26feb79fcd8ad4e155e96657cafb46d6
1 /*
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/>.
23 #include "includes.h"
24 #include "system/time.h"
25 #include "auth/auth.h"
26 #include <ldb.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 */ \
36 "objectClass", \
37 "sAMAccountName", \
38 "userPrincipalName", \
39 "servicePrincipalName", \
40 "msDS-KeyVersionNumber", \
41 "msDS-SecondaryKrbTgtNumber", \
42 "msDS-SupportedEncryptionTypes", \
43 "supplementalCredentials", \
44 "msDS-AllowedToDelegateTo", \
46 /* passwords */ \
47 "dBCSPwd", \
48 "unicodePwd", \
50 "userAccountControl", \
51 "msDS-User-Account-Control-Computed", \
52 "objectSid", \
54 "pwdLastSet", \
55 "accountExpires"
57 const char *krbtgt_attrs[] = {
58 KRBTGT_ATTRS, NULL
61 const char *server_attrs[] = {
62 KRBTGT_ATTRS, NULL
65 const char *user_attrs[] = {
66 KRBTGT_ATTRS,
68 "logonHours",
71 * To allow us to zero the badPwdCount and lockoutTime on
72 * successful logon, without database churn
74 "lockoutTime",
76 /* check 'allowed workstations' */
77 "userWorkstations",
79 /* required for user_info_dc, not access control: */
80 "displayName",
81 "scriptPath",
82 "profilePath",
83 "homeDirectory",
84 "homeDrive",
85 "lastLogon",
86 "lastLogoff",
87 "accountExpires",
88 "badPwdCount",
89 "logonCount",
90 "primaryGroupID",
91 "memberOf",
92 "badPasswordTime",
93 NULL,
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
99 bitmask.
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;
106 struct tm *utctime;
107 time_t lasttime;
108 const char *asct;
109 uint8_t bitmask, bitpos;
111 hours = ldb_msg_find_ldb_val(msg, "logonHours");
112 if (!hours) {
113 DEBUG(5,("logon_hours_ok: No hours restrictions for user %s\n", name_for_logs));
114 return true;
117 if (hours->length != 168/8) {
118 DEBUG(5,("logon_hours_ok: malformed logon hours restrictions for user %s\n", name_for_logs));
119 return true;
122 lasttime = time(NULL);
123 utctime = gmtime(&lasttime);
124 if (!utctime) {
125 DEBUG(1, ("logon_hours_ok: failed to get gmtime. Failing logon for user %s\n",
126 name_for_logs));
127 return false;
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);
136 if (!t) {
137 asct = "INVALID TIME";
138 } else {
139 asct = asctime(t);
140 if (!asct) {
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 ));
148 return false;
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" ));
155 return true;
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)
172 uint16_t acct_flags;
173 const char *workstation_list;
174 NTTIME acct_expiry;
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,
188 domain_dn, msg);
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",
215 name_for_logs));
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",
222 name_for_logs));
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;
231 int i;
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)) {
239 invalid_ws = false;
240 break;
244 talloc_free(workstations);
246 if (invalid_ws) {
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;
276 return NT_STATUS_OK;
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)
289 NTSTATUS status;
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;
302 TALLOC_CTX *tmp_ctx;
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);
315 if (sids == NULL) {
316 TALLOC_FREE(user_info_dc);
317 return NT_STATUS_NO_MEMORY;
320 num_sids = 2;
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);
331 return status;
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);
374 return status;
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);
387 return status;
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,
451 "pwdLastSet", 0);
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,
457 domain_dn, msg);
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,
465 user_sess_key.data,
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,
474 lm_sess_key.data,
475 lm_sess_key.length);
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
485 PAC */
486 user_info_dc->sids = talloc_realloc(user_info_dc,
487 user_info_dc->sids,
488 struct dom_sid,
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,
502 user_info_dc->sids,
503 struct dom_sid,
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;
520 return NT_STATUS_OK;
523 NTSTATUS sam_get_results_principal(struct ldb_context *sam_ctx,
524 TALLOC_CTX *mem_ctx, const char *principal,
525 const char **attrs,
526 struct ldb_dn **domain_dn,
527 struct ldb_message **msg)
529 struct ldb_dn *user_dn;
530 NTSTATUS nt_status;
531 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
532 int ret;
534 if (!tmp_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);
542 return nt_status;
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,
549 "(objectClass=*)");
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);
558 return NT_STATUS_OK;
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)
572 NTSTATUS nt_status;
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);
580 if (!tmp_ctx) {
581 return NT_STATUS_NO_MEMORY;
584 if (principal) {
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);
589 return nt_status;
591 } else if (user_dn) {
592 struct dom_sid *user_sid, *domain_sid;
593 int ret;
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,
598 "(objectClass=*)");
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)) {
611 return 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));
617 if (!domain_dn) {
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;
623 } else {
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),
630 domain_dn,
631 msg,
632 user_sess_key, lm_sess_key,
633 user_info_dc);
634 if (!NT_STATUS_IS_OK(nt_status)) {
635 talloc_free(tmp_ctx);
636 return nt_status;
639 talloc_steal(mem_ctx, *user_info_dc);
640 talloc_free(tmp_ctx);
642 return NT_STATUS_OK;
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",
651 "lockoutDuration",
652 "pwdProperties",
653 NULL };
654 int ret;
655 NTSTATUS status;
656 struct ldb_result *domain_res;
657 struct ldb_message *msg_mod = NULL;
658 TALLOC_CTX *mem_ctx;
660 mem_ctx = talloc_new(msg);
661 if (mem_ctx == NULL) {
662 return NT_STATUS_NO_MEMORY;
665 ret = dsdb_search_dn(sam_ctx, mem_ctx, &domain_res, domain_dn, attrs, 0);
666 if (ret != LDB_SUCCESS) {
667 TALLOC_FREE(mem_ctx);
668 return NT_STATUS_INTERNAL_DB_CORRUPTION;
671 status = dsdb_update_bad_pwd_count(mem_ctx, sam_ctx,
672 msg, domain_res->msgs[0], &msg_mod);
673 if (!NT_STATUS_IS_OK(status)) {
674 TALLOC_FREE(mem_ctx);
675 return status;
678 if (msg_mod != NULL) {
679 ret = dsdb_modify(sam_ctx, msg_mod, 0);
680 if (ret != LDB_SUCCESS) {
681 DEBUG(0, ("Failed to update badPwdCount, badPasswordTime or set lockoutTime on %s: %s\n",
682 ldb_dn_get_linearized(msg_mod->dn), ldb_errstring(sam_ctx)));
683 TALLOC_FREE(mem_ctx);
684 return NT_STATUS_INTERNAL_ERROR;
688 TALLOC_FREE(mem_ctx);
689 return NT_STATUS_OK;
692 NTSTATUS authsam_zero_bad_pwd_count(struct ldb_context *sam_ctx,
693 const struct ldb_message *msg)
695 int ret;
696 int badPwdCount;
697 int64_t lockoutTime;
698 struct ldb_message *msg_mod;
699 TALLOC_CTX *mem_ctx;
701 lockoutTime = ldb_msg_find_attr_as_int64(msg, "lockoutTime", 0);
702 badPwdCount = ldb_msg_find_attr_as_int(msg, "badPwdCount", 0);
703 if (lockoutTime == 0 && badPwdCount == 0) {
704 return NT_STATUS_OK;
707 mem_ctx = talloc_new(msg);
708 if (mem_ctx == NULL) {
709 return NT_STATUS_NO_MEMORY;
711 msg_mod = ldb_msg_new(mem_ctx);
712 if (msg_mod == NULL) {
713 TALLOC_FREE(mem_ctx);
714 return NT_STATUS_NO_MEMORY;
716 msg_mod->dn = msg->dn;
718 if (lockoutTime != 0) {
720 * This implies "badPwdCount" = 0, see samldb_lockout_time()
722 ret = samdb_msg_add_int(sam_ctx, msg_mod, msg_mod, "lockoutTime", 0);
723 if (ret != LDB_SUCCESS) {
724 TALLOC_FREE(mem_ctx);
725 return NT_STATUS_NO_MEMORY;
727 } else {
728 ret = samdb_msg_add_int(sam_ctx, msg_mod, msg_mod, "badPwdCount", 0);
729 if (ret != LDB_SUCCESS) {
730 TALLOC_FREE(mem_ctx);
731 return NT_STATUS_NO_MEMORY;
735 ret = dsdb_replace(sam_ctx, msg_mod, 0);
736 if (ret != LDB_SUCCESS) {
737 DEBUG(0, ("Failed to set badPwdCount and lockoutTime to 0 on %s: %s\n",
738 ldb_dn_get_linearized(msg_mod->dn), ldb_errstring(sam_ctx)));
739 TALLOC_FREE(mem_ctx);
740 return NT_STATUS_INTERNAL_ERROR;
743 TALLOC_FREE(mem_ctx);
744 return NT_STATUS_OK;