torture: convert torture_comment() -> torture_result() so we can knownfail flapping...
[Samba/wip.git] / source4 / auth / sam.c
blobf7bc6939dd71febd70fe0d456c238e94f190dc78
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 "lmPwdHistory",
94 "ntPwdHistory",
95 NULL,
98 /****************************************************************************
99 Check if a user is allowed to logon at this time. Note this is the
100 servers local time, as logon hours are just specified as a weekly
101 bitmask.
102 ****************************************************************************/
104 static bool logon_hours_ok(struct ldb_message *msg, const char *name_for_logs)
106 /* In logon hours first bit is Sunday from 12AM to 1AM */
107 const struct ldb_val *hours;
108 struct tm *utctime;
109 time_t lasttime;
110 const char *asct;
111 uint8_t bitmask, bitpos;
113 hours = ldb_msg_find_ldb_val(msg, "logonHours");
114 if (!hours) {
115 DEBUG(5,("logon_hours_ok: No hours restrictions for user %s\n", name_for_logs));
116 return true;
119 if (hours->length != 168/8) {
120 DEBUG(5,("logon_hours_ok: malformed logon hours restrictions for user %s\n", name_for_logs));
121 return true;
124 lasttime = time(NULL);
125 utctime = gmtime(&lasttime);
126 if (!utctime) {
127 DEBUG(1, ("logon_hours_ok: failed to get gmtime. Failing logon for user %s\n",
128 name_for_logs));
129 return false;
132 /* find the corresponding byte and bit */
133 bitpos = (utctime->tm_wday * 24 + utctime->tm_hour) % 168;
134 bitmask = 1 << (bitpos % 8);
136 if (! (hours->data[bitpos/8] & bitmask)) {
137 struct tm *t = localtime(&lasttime);
138 if (!t) {
139 asct = "INVALID TIME";
140 } else {
141 asct = asctime(t);
142 if (!asct) {
143 asct = "INVALID TIME";
147 DEBUG(1, ("logon_hours_ok: Account for user %s not allowed to "
148 "logon at this time (%s).\n",
149 name_for_logs, asct ));
150 return false;
153 asct = asctime(utctime);
154 DEBUG(5,("logon_hours_ok: user %s allowed to logon at this time (%s)\n",
155 name_for_logs, asct ? asct : "UNKNOWN TIME" ));
157 return true;
160 /****************************************************************************
161 Do a specific test for a SAM_ACCOUNT being valid for this connection
162 (ie not disabled, expired and the like).
163 ****************************************************************************/
164 _PUBLIC_ NTSTATUS authsam_account_ok(TALLOC_CTX *mem_ctx,
165 struct ldb_context *sam_ctx,
166 uint32_t logon_parameters,
167 struct ldb_dn *domain_dn,
168 struct ldb_message *msg,
169 const char *logon_workstation,
170 const char *name_for_logs,
171 bool allow_domain_trust,
172 bool password_change)
174 uint16_t acct_flags;
175 const char *workstation_list;
176 NTTIME acct_expiry;
177 NTTIME must_change_time;
178 struct timeval tv_now = timeval_current();
179 NTTIME now = timeval_to_nttime(&tv_now);
181 DEBUG(4,("authsam_account_ok: Checking SMB password for user %s\n", name_for_logs));
183 acct_flags = samdb_result_acct_flags(msg, "msDS-User-Account-Control-Computed");
185 acct_expiry = samdb_result_account_expires(msg);
187 /* Check for when we must change this password, taking the
188 * userAccountControl flags into account */
189 must_change_time = samdb_result_force_password_change(sam_ctx, mem_ctx,
190 domain_dn, msg);
192 workstation_list = ldb_msg_find_attr_as_string(msg, "userWorkstations", NULL);
194 /* Quit if the account was disabled. */
195 if (acct_flags & ACB_DISABLED) {
196 DEBUG(2,("authsam_account_ok: Account for user '%s' was disabled.\n", name_for_logs));
197 return NT_STATUS_ACCOUNT_DISABLED;
200 /* Quit if the account was locked out. */
201 if (acct_flags & ACB_AUTOLOCK) {
202 DEBUG(2,("authsam_account_ok: Account for user %s was locked out.\n", name_for_logs));
203 return NT_STATUS_ACCOUNT_LOCKED_OUT;
206 /* Test account expire time */
207 if (now > acct_expiry) {
208 DEBUG(2,("authsam_account_ok: Account for user '%s' has expired.\n", name_for_logs));
209 DEBUG(3,("authsam_account_ok: Account expired at '%s'.\n",
210 nt_time_string(mem_ctx, acct_expiry)));
211 return NT_STATUS_ACCOUNT_EXPIRED;
214 /* check for immediate expiry "must change at next logon" (but not if this is a password change request) */
215 if ((must_change_time == 0) && !password_change) {
216 DEBUG(2,("sam_account_ok: Account for user '%s' password must change!.\n",
217 name_for_logs));
218 return NT_STATUS_PASSWORD_MUST_CHANGE;
221 /* check for expired password (but not if this is a password change request) */
222 if ((must_change_time < now) && !password_change) {
223 DEBUG(2,("sam_account_ok: Account for user '%s' password expired!.\n",
224 name_for_logs));
225 DEBUG(2,("sam_account_ok: Password expired at '%s' unix time.\n",
226 nt_time_string(mem_ctx, must_change_time)));
227 return NT_STATUS_PASSWORD_EXPIRED;
230 /* Test workstation. Workstation list is comma separated. */
231 if (logon_workstation && workstation_list && *workstation_list) {
232 bool invalid_ws = true;
233 int i;
234 char **workstations = str_list_make(mem_ctx, workstation_list, ",");
236 for (i = 0; workstations && workstations[i]; i++) {
237 DEBUG(10,("sam_account_ok: checking for workstation match '%s' and '%s'\n",
238 workstations[i], logon_workstation));
240 if (strequal(workstations[i], logon_workstation)) {
241 invalid_ws = false;
242 break;
246 talloc_free(workstations);
248 if (invalid_ws) {
249 return NT_STATUS_INVALID_WORKSTATION;
253 if (!logon_hours_ok(msg, name_for_logs)) {
254 return NT_STATUS_INVALID_LOGON_HOURS;
257 if (!allow_domain_trust) {
258 if (acct_flags & ACB_DOMTRUST) {
259 DEBUG(2,("sam_account_ok: Domain trust account %s denied by server\n", name_for_logs));
260 return NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT;
263 if (!(logon_parameters & MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT)) {
264 if (acct_flags & ACB_SVRTRUST) {
265 DEBUG(2,("sam_account_ok: Server trust account %s denied by server\n", name_for_logs));
266 return NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT;
269 if (!(logon_parameters & MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT)) {
270 /* TODO: this fails with current solaris client. We
271 need to work with Gordon to work out why */
272 if (acct_flags & ACB_WSTRUST) {
273 DEBUG(4,("sam_account_ok: Wksta trust account %s denied by server\n", name_for_logs));
274 return NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT;
278 return NT_STATUS_OK;
281 _PUBLIC_ NTSTATUS authsam_make_user_info_dc(TALLOC_CTX *mem_ctx,
282 struct ldb_context *sam_ctx,
283 const char *netbios_name,
284 const char *domain_name,
285 struct ldb_dn *domain_dn,
286 struct ldb_message *msg,
287 DATA_BLOB user_sess_key,
288 DATA_BLOB lm_sess_key,
289 struct auth_user_info_dc **_user_info_dc)
291 NTSTATUS status;
292 struct auth_user_info_dc *user_info_dc;
293 struct auth_user_info *info;
294 const char *str, *filter;
295 /* SIDs for the account and his primary group */
296 struct dom_sid *account_sid;
297 const char *primary_group_string;
298 const char *primary_group_dn;
299 DATA_BLOB primary_group_blob;
300 /* SID structures for the expanded group memberships */
301 struct dom_sid *sids = NULL;
302 unsigned int num_sids = 0, i;
303 struct dom_sid *domain_sid;
304 TALLOC_CTX *tmp_ctx;
305 struct ldb_message_element *el;
307 user_info_dc = talloc(mem_ctx, struct auth_user_info_dc);
308 NT_STATUS_HAVE_NO_MEMORY(user_info_dc);
310 tmp_ctx = talloc_new(user_info_dc);
311 if (user_info_dc == NULL) {
312 TALLOC_FREE(user_info_dc);
313 return NT_STATUS_NO_MEMORY;
316 sids = talloc_array(user_info_dc, struct dom_sid, 2);
317 if (sids == NULL) {
318 TALLOC_FREE(user_info_dc);
319 return NT_STATUS_NO_MEMORY;
322 num_sids = 2;
324 account_sid = samdb_result_dom_sid(user_info_dc, msg, "objectSid");
325 if (account_sid == NULL) {
326 TALLOC_FREE(user_info_dc);
327 return NT_STATUS_NO_MEMORY;
330 status = dom_sid_split_rid(tmp_ctx, account_sid, &domain_sid, NULL);
331 if (!NT_STATUS_IS_OK(status)) {
332 talloc_free(user_info_dc);
333 return status;
336 sids[PRIMARY_USER_SID_INDEX] = *account_sid;
337 sids[PRIMARY_GROUP_SID_INDEX] = *domain_sid;
338 sid_append_rid(&sids[PRIMARY_GROUP_SID_INDEX], ldb_msg_find_attr_as_uint(msg, "primaryGroupID", ~0));
340 /* Filter out builtin groups from this token. We will search
341 * for builtin groups later, and not include them in the PAC
342 * on SamLogon validation info */
343 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);
344 if (filter == NULL) {
345 TALLOC_FREE(user_info_dc);
346 return NT_STATUS_NO_MEMORY;
349 primary_group_string = dom_sid_string(tmp_ctx, &sids[PRIMARY_GROUP_SID_INDEX]);
350 if (primary_group_string == NULL) {
351 TALLOC_FREE(user_info_dc);
352 return NT_STATUS_NO_MEMORY;
355 primary_group_dn = talloc_asprintf(tmp_ctx, "<SID=%s>", primary_group_string);
356 if (primary_group_dn == NULL) {
357 TALLOC_FREE(user_info_dc);
358 return NT_STATUS_NO_MEMORY;
361 primary_group_blob = data_blob_string_const(primary_group_dn);
363 /* Expands the primary group - this function takes in
364 * memberOf-like values, so we fake one up with the
365 * <SID=S-...> format of DN and then let it expand
366 * them, as long as they meet the filter - so only
367 * domain groups, not builtin groups
369 * The primary group is still treated specially, so we set the
370 * 'only childs' flag to true
372 status = dsdb_expand_nested_groups(sam_ctx, &primary_group_blob, true, filter,
373 user_info_dc, &sids, &num_sids);
374 if (!NT_STATUS_IS_OK(status)) {
375 talloc_free(user_info_dc);
376 return status;
379 /* Expands the additional groups */
380 el = ldb_msg_find_element(msg, "memberOf");
381 for (i = 0; el && i < el->num_values; i++) {
382 /* This function takes in memberOf values and expands
383 * them, as long as they meet the filter - so only
384 * domain groups, not builtin groups */
385 status = dsdb_expand_nested_groups(sam_ctx, &el->values[i], false, filter,
386 user_info_dc, &sids, &num_sids);
387 if (!NT_STATUS_IS_OK(status)) {
388 talloc_free(user_info_dc);
389 return status;
393 user_info_dc->sids = sids;
394 user_info_dc->num_sids = num_sids;
396 user_info_dc->info = info = talloc_zero(user_info_dc, struct auth_user_info);
397 NT_STATUS_HAVE_NO_MEMORY(user_info_dc->info);
399 info->account_name = talloc_steal(info,
400 ldb_msg_find_attr_as_string(msg, "sAMAccountName", NULL));
402 info->domain_name = talloc_strdup(info, domain_name);
403 if (info->domain_name == NULL) {
404 TALLOC_FREE(user_info_dc);
405 return NT_STATUS_NO_MEMORY;
408 str = ldb_msg_find_attr_as_string(msg, "displayName", "");
409 info->full_name = talloc_strdup(info, str);
410 if (info->full_name == NULL) {
411 TALLOC_FREE(user_info_dc);
412 return NT_STATUS_NO_MEMORY;
415 str = ldb_msg_find_attr_as_string(msg, "scriptPath", "");
416 info->logon_script = talloc_strdup(info, str);
417 if (info->logon_script == NULL) {
418 TALLOC_FREE(user_info_dc);
419 return NT_STATUS_NO_MEMORY;
422 str = ldb_msg_find_attr_as_string(msg, "profilePath", "");
423 info->profile_path = talloc_strdup(info, str);
424 if (info->profile_path == NULL) {
425 TALLOC_FREE(user_info_dc);
426 return NT_STATUS_NO_MEMORY;
429 str = ldb_msg_find_attr_as_string(msg, "homeDirectory", "");
430 info->home_directory = talloc_strdup(info, str);
431 if (info->home_directory == NULL) {
432 TALLOC_FREE(user_info_dc);
433 return NT_STATUS_NO_MEMORY;
436 str = ldb_msg_find_attr_as_string(msg, "homeDrive", "");
437 info->home_drive = talloc_strdup(info, str);
438 if (info->home_drive == NULL) {
439 TALLOC_FREE(user_info_dc);
440 return NT_STATUS_NO_MEMORY;
443 info->logon_server = talloc_strdup(info, netbios_name);
444 if (info->logon_server == NULL) {
445 TALLOC_FREE(user_info_dc);
446 return NT_STATUS_NO_MEMORY;
449 info->last_logon = samdb_result_nttime(msg, "lastLogon", 0);
450 info->last_logoff = samdb_result_last_logoff(msg);
451 info->acct_expiry = samdb_result_account_expires(msg);
452 info->last_password_change = samdb_result_nttime(msg,
453 "pwdLastSet", 0);
454 info->allow_password_change
455 = samdb_result_allow_password_change(sam_ctx, mem_ctx,
456 domain_dn, msg, "pwdLastSet");
457 info->force_password_change
458 = samdb_result_force_password_change(sam_ctx, mem_ctx,
459 domain_dn, msg);
460 info->logon_count = ldb_msg_find_attr_as_uint(msg, "logonCount", 0);
461 info->bad_password_count = ldb_msg_find_attr_as_uint(msg, "badPwdCount",
464 info->acct_flags = samdb_result_acct_flags(msg, "msDS-User-Account-Control-Computed");
466 user_info_dc->user_session_key = data_blob_talloc(user_info_dc,
467 user_sess_key.data,
468 user_sess_key.length);
469 if (user_sess_key.data) {
470 if (user_info_dc->user_session_key.data == NULL) {
471 TALLOC_FREE(user_info_dc);
472 return NT_STATUS_NO_MEMORY;
475 user_info_dc->lm_session_key = data_blob_talloc(user_info_dc,
476 lm_sess_key.data,
477 lm_sess_key.length);
478 if (lm_sess_key.data) {
479 if (user_info_dc->lm_session_key.data == NULL) {
480 TALLOC_FREE(user_info_dc);
481 return NT_STATUS_NO_MEMORY;
485 if (info->acct_flags & ACB_SVRTRUST) {
486 /* the SID_NT_ENTERPRISE_DCS SID gets added into the
487 PAC */
488 user_info_dc->sids = talloc_realloc(user_info_dc,
489 user_info_dc->sids,
490 struct dom_sid,
491 user_info_dc->num_sids+1);
492 if (user_info_dc->sids == NULL) {
493 TALLOC_FREE(user_info_dc);
494 return NT_STATUS_NO_MEMORY;
496 user_info_dc->sids[user_info_dc->num_sids] = global_sid_Enterprise_DCs;
497 user_info_dc->num_sids++;
500 if ((info->acct_flags & (ACB_PARTIAL_SECRETS_ACCOUNT | ACB_WSTRUST)) ==
501 (ACB_PARTIAL_SECRETS_ACCOUNT | ACB_WSTRUST)) {
502 /* the DOMAIN_RID_ENTERPRISE_READONLY_DCS PAC */
503 user_info_dc->sids = talloc_realloc(user_info_dc,
504 user_info_dc->sids,
505 struct dom_sid,
506 user_info_dc->num_sids+1);
507 if (user_info_dc->sids == NULL) {
508 TALLOC_FREE(user_info_dc);
509 return NT_STATUS_NO_MEMORY;
511 user_info_dc->sids[user_info_dc->num_sids] = *domain_sid;
512 sid_append_rid(&user_info_dc->sids[user_info_dc->num_sids],
513 DOMAIN_RID_ENTERPRISE_READONLY_DCS);
514 user_info_dc->num_sids++;
517 info->authenticated = true;
519 talloc_free(tmp_ctx);
520 *_user_info_dc = user_info_dc;
522 return NT_STATUS_OK;
525 NTSTATUS sam_get_results_principal(struct ldb_context *sam_ctx,
526 TALLOC_CTX *mem_ctx, const char *principal,
527 const char **attrs,
528 struct ldb_dn **domain_dn,
529 struct ldb_message **msg)
531 struct ldb_dn *user_dn;
532 NTSTATUS nt_status;
533 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
534 int ret;
536 if (!tmp_ctx) {
537 return NT_STATUS_NO_MEMORY;
540 nt_status = crack_user_principal_name(sam_ctx, tmp_ctx, principal,
541 &user_dn, domain_dn);
542 if (!NT_STATUS_IS_OK(nt_status)) {
543 talloc_free(tmp_ctx);
544 return nt_status;
547 /* pull the user attributes */
548 ret = dsdb_search_one(sam_ctx, tmp_ctx, msg, user_dn,
549 LDB_SCOPE_BASE, attrs,
550 DSDB_SEARCH_SHOW_EXTENDED_DN | DSDB_SEARCH_NO_GLOBAL_CATALOG,
551 "(objectClass=*)");
552 if (ret != LDB_SUCCESS) {
553 talloc_free(tmp_ctx);
554 return NT_STATUS_INTERNAL_DB_CORRUPTION;
556 talloc_steal(mem_ctx, *msg);
557 talloc_steal(mem_ctx, *domain_dn);
558 talloc_free(tmp_ctx);
560 return NT_STATUS_OK;
563 /* Used in the gensec_gssapi and gensec_krb5 server-side code, where the PAC isn't available, and for tokenGroups in the DSDB stack.
565 Supply either a principal or a DN
567 NTSTATUS authsam_get_user_info_dc_principal(TALLOC_CTX *mem_ctx,
568 struct loadparm_context *lp_ctx,
569 struct ldb_context *sam_ctx,
570 const char *principal,
571 struct ldb_dn *user_dn,
572 struct auth_user_info_dc **user_info_dc)
574 NTSTATUS nt_status;
575 DATA_BLOB user_sess_key = data_blob(NULL, 0);
576 DATA_BLOB lm_sess_key = data_blob(NULL, 0);
578 struct ldb_message *msg;
579 struct ldb_dn *domain_dn;
581 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
582 if (!tmp_ctx) {
583 return NT_STATUS_NO_MEMORY;
586 if (principal) {
587 nt_status = sam_get_results_principal(sam_ctx, tmp_ctx, principal,
588 user_attrs, &domain_dn, &msg);
589 if (!NT_STATUS_IS_OK(nt_status)) {
590 talloc_free(tmp_ctx);
591 return nt_status;
593 } else if (user_dn) {
594 struct dom_sid *user_sid, *domain_sid;
595 int ret;
596 /* pull the user attributes */
597 ret = dsdb_search_one(sam_ctx, tmp_ctx, &msg, user_dn,
598 LDB_SCOPE_BASE, user_attrs,
599 DSDB_SEARCH_SHOW_EXTENDED_DN | DSDB_SEARCH_NO_GLOBAL_CATALOG,
600 "(objectClass=*)");
601 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
602 talloc_free(tmp_ctx);
603 return NT_STATUS_NO_SUCH_USER;
604 } else if (ret != LDB_SUCCESS) {
605 talloc_free(tmp_ctx);
606 return NT_STATUS_INTERNAL_DB_CORRUPTION;
609 user_sid = samdb_result_dom_sid(msg, msg, "objectSid");
611 nt_status = dom_sid_split_rid(tmp_ctx, user_sid, &domain_sid, NULL);
612 if (!NT_STATUS_IS_OK(nt_status)) {
613 return nt_status;
616 domain_dn = samdb_search_dn(sam_ctx, mem_ctx, NULL,
617 "(&(objectSid=%s)(objectClass=domain))",
618 ldap_encode_ndr_dom_sid(tmp_ctx, domain_sid));
619 if (!domain_dn) {
620 DEBUG(3, ("authsam_get_user_info_dc_principal: Failed to find domain with: SID %s\n",
621 dom_sid_string(tmp_ctx, domain_sid)));
622 return NT_STATUS_NO_SUCH_USER;
625 } else {
626 return NT_STATUS_INVALID_PARAMETER;
629 nt_status = authsam_make_user_info_dc(tmp_ctx, sam_ctx,
630 lpcfg_netbios_name(lp_ctx),
631 lpcfg_workgroup(lp_ctx),
632 domain_dn,
633 msg,
634 user_sess_key, lm_sess_key,
635 user_info_dc);
636 if (!NT_STATUS_IS_OK(nt_status)) {
637 talloc_free(tmp_ctx);
638 return nt_status;
641 talloc_steal(mem_ctx, *user_info_dc);
642 talloc_free(tmp_ctx);
644 return NT_STATUS_OK;
647 NTSTATUS authsam_update_bad_pwd_count(struct ldb_context *sam_ctx,
648 struct ldb_message *msg,
649 struct ldb_dn *domain_dn)
651 const char *attrs[] = { "lockoutThreshold",
652 "lockOutObservationWindow",
653 "lockoutDuration",
654 "pwdProperties",
655 NULL };
656 int ret;
657 NTSTATUS status;
658 struct ldb_result *domain_res;
659 struct ldb_message *msg_mod = NULL;
660 TALLOC_CTX *mem_ctx;
662 mem_ctx = talloc_new(msg);
663 if (mem_ctx == NULL) {
664 return NT_STATUS_NO_MEMORY;
667 ret = dsdb_search_dn(sam_ctx, mem_ctx, &domain_res, domain_dn, attrs, 0);
668 if (ret != LDB_SUCCESS) {
669 TALLOC_FREE(mem_ctx);
670 return NT_STATUS_INTERNAL_DB_CORRUPTION;
673 status = dsdb_update_bad_pwd_count(mem_ctx, sam_ctx,
674 msg, domain_res->msgs[0], &msg_mod);
675 if (!NT_STATUS_IS_OK(status)) {
676 TALLOC_FREE(mem_ctx);
677 return status;
680 if (msg_mod != NULL) {
681 ret = dsdb_modify(sam_ctx, msg_mod, 0);
682 if (ret != LDB_SUCCESS) {
683 DEBUG(0, ("Failed to update badPwdCount, badPasswordTime or set lockoutTime on %s: %s\n",
684 ldb_dn_get_linearized(msg_mod->dn), ldb_errstring(sam_ctx)));
685 TALLOC_FREE(mem_ctx);
686 return NT_STATUS_INTERNAL_ERROR;
690 TALLOC_FREE(mem_ctx);
691 return NT_STATUS_OK;
694 NTSTATUS authsam_zero_bad_pwd_count(struct ldb_context *sam_ctx,
695 const struct ldb_message *msg)
697 int ret;
698 int badPwdCount;
699 int64_t lockoutTime;
700 struct ldb_message *msg_mod;
701 TALLOC_CTX *mem_ctx;
703 lockoutTime = ldb_msg_find_attr_as_int64(msg, "lockoutTime", 0);
704 badPwdCount = ldb_msg_find_attr_as_int(msg, "badPwdCount", 0);
705 if (lockoutTime == 0 && badPwdCount == 0) {
706 return NT_STATUS_OK;
709 mem_ctx = talloc_new(msg);
710 if (mem_ctx == NULL) {
711 return NT_STATUS_NO_MEMORY;
713 msg_mod = ldb_msg_new(mem_ctx);
714 if (msg_mod == NULL) {
715 TALLOC_FREE(mem_ctx);
716 return NT_STATUS_NO_MEMORY;
718 msg_mod->dn = msg->dn;
720 if (lockoutTime != 0) {
722 * This implies "badPwdCount" = 0, see samldb_lockout_time()
724 ret = samdb_msg_add_int(sam_ctx, msg_mod, msg_mod, "lockoutTime", 0);
725 if (ret != LDB_SUCCESS) {
726 TALLOC_FREE(mem_ctx);
727 return NT_STATUS_NO_MEMORY;
729 } else {
730 ret = samdb_msg_add_int(sam_ctx, msg_mod, msg_mod, "badPwdCount", 0);
731 if (ret != LDB_SUCCESS) {
732 TALLOC_FREE(mem_ctx);
733 return NT_STATUS_NO_MEMORY;
737 ret = dsdb_replace(sam_ctx, msg_mod, 0);
738 if (ret != LDB_SUCCESS) {
739 DEBUG(0, ("Failed to set badPwdCount and lockoutTime to 0 on %s: %s\n",
740 ldb_dn_get_linearized(msg_mod->dn), ldb_errstring(sam_ctx)));
741 TALLOC_FREE(mem_ctx);
742 return NT_STATUS_INTERNAL_ERROR;
745 TALLOC_FREE(mem_ctx);
746 return NT_STATUS_OK;