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"
33 #include "librpc/gen_ndr/ndr_winbind_c.h"
36 #define DBGC_CLASS DBGC_AUTH
38 #define KRBTGT_ATTRS \
39 /* required for the krb5 kdc */ \
42 "userPrincipalName", \
43 "servicePrincipalName", \
44 "msDS-KeyVersionNumber", \
45 "msDS-SecondaryKrbTgtNumber", \
46 "msDS-SupportedEncryptionTypes", \
47 "supplementalCredentials", \
48 "msDS-AllowedToDelegateTo", \
54 "userAccountControl", \
55 "msDS-User-Account-Control-Computed", \
59 "msDS-UserPasswordExpiryTimeComputed", \
62 const char *krbtgt_attrs[] = {
66 const char *server_attrs[] = {
70 const char *user_attrs[] = {
76 * To allow us to zero the badPwdCount and lockoutTime on
77 * successful logon, without database churn
82 * Needed for SendToSAM requests
86 /* check 'allowed workstations' */
89 /* required for user_info_dc, not access control: */
109 /****************************************************************************
110 Check if a user is allowed to logon at this time. Note this is the
111 servers local time, as logon hours are just specified as a weekly
113 ****************************************************************************/
115 static bool logon_hours_ok(struct ldb_message *msg, const char *name_for_logs)
117 /* In logon hours first bit is Sunday from 12AM to 1AM */
118 const struct ldb_val *hours;
122 uint8_t bitmask, bitpos;
124 hours = ldb_msg_find_ldb_val(msg, "logonHours");
126 DEBUG(5,("logon_hours_ok: No hours restrictions for user %s\n", name_for_logs));
130 if (hours->length != 168/8) {
131 DEBUG(5,("logon_hours_ok: malformed logon hours restrictions for user %s\n", name_for_logs));
135 lasttime = time(NULL);
136 utctime = gmtime(&lasttime);
138 DEBUG(1, ("logon_hours_ok: failed to get gmtime. Failing logon for user %s\n",
143 /* find the corresponding byte and bit */
144 bitpos = (utctime->tm_wday * 24 + utctime->tm_hour) % 168;
145 bitmask = 1 << (bitpos % 8);
147 if (! (hours->data[bitpos/8] & bitmask)) {
148 struct tm *t = localtime(&lasttime);
150 asct = "INVALID TIME";
154 asct = "INVALID TIME";
158 DEBUG(1, ("logon_hours_ok: Account for user %s not allowed to "
159 "logon at this time (%s).\n",
160 name_for_logs, asct ));
164 asct = asctime(utctime);
165 DEBUG(5,("logon_hours_ok: user %s allowed to logon at this time (%s)\n",
166 name_for_logs, asct ? asct : "UNKNOWN TIME" ));
171 /****************************************************************************
172 Do a specific test for a SAM_ACCOUNT being valid for this connection
173 (ie not disabled, expired and the like).
174 ****************************************************************************/
175 _PUBLIC_ NTSTATUS authsam_account_ok(TALLOC_CTX *mem_ctx,
176 struct ldb_context *sam_ctx,
177 uint32_t logon_parameters,
178 struct ldb_dn *domain_dn,
179 struct ldb_message *msg,
180 const char *logon_workstation,
181 const char *name_for_logs,
182 bool allow_domain_trust,
183 bool password_change)
186 const char *workstation_list;
188 NTTIME must_change_time;
189 struct timeval tv_now = timeval_current();
190 NTTIME now = timeval_to_nttime(&tv_now);
192 DEBUG(4,("authsam_account_ok: Checking SMB password for user %s\n", name_for_logs));
194 acct_flags = samdb_result_acct_flags(msg, "msDS-User-Account-Control-Computed");
196 acct_expiry = samdb_result_account_expires(msg);
198 /* Check for when we must change this password, taking the
199 * userAccountControl flags into account */
200 must_change_time = samdb_result_nttime(msg,
201 "msDS-UserPasswordExpiryTimeComputed", 0);
203 workstation_list = ldb_msg_find_attr_as_string(msg, "userWorkstations", NULL);
205 /* Quit if the account was disabled. */
206 if (acct_flags & ACB_DISABLED) {
207 DEBUG(2,("authsam_account_ok: Account for user '%s' was disabled.\n", name_for_logs));
208 return NT_STATUS_ACCOUNT_DISABLED;
211 /* Quit if the account was locked out. */
212 if (acct_flags & ACB_AUTOLOCK) {
213 DEBUG(2,("authsam_account_ok: Account for user %s was locked out.\n", name_for_logs));
214 return NT_STATUS_ACCOUNT_LOCKED_OUT;
217 /* Test account expire time */
218 if (now > acct_expiry) {
219 DEBUG(2,("authsam_account_ok: Account for user '%s' has expired.\n", name_for_logs));
220 DEBUG(3,("authsam_account_ok: Account expired at '%s'.\n",
221 nt_time_string(mem_ctx, acct_expiry)));
222 return NT_STATUS_ACCOUNT_EXPIRED;
225 /* check for immediate expiry "must change at next logon" (but not if this is a password change request) */
226 if ((must_change_time == 0) && !password_change) {
227 DEBUG(2,("sam_account_ok: Account for user '%s' password must change!.\n",
229 return NT_STATUS_PASSWORD_MUST_CHANGE;
232 /* check for expired password (but not if this is a password change request) */
233 if ((must_change_time < now) && !password_change) {
234 DEBUG(2,("sam_account_ok: Account for user '%s' password expired!.\n",
236 DEBUG(2,("sam_account_ok: Password expired at '%s' unix time.\n",
237 nt_time_string(mem_ctx, must_change_time)));
238 return NT_STATUS_PASSWORD_EXPIRED;
241 /* Test workstation. Workstation list is comma separated. */
242 if (logon_workstation && workstation_list && *workstation_list) {
243 bool invalid_ws = true;
245 char **workstations = str_list_make(mem_ctx, workstation_list, ",");
247 for (i = 0; workstations && workstations[i]; i++) {
248 DEBUG(10,("sam_account_ok: checking for workstation match '%s' and '%s'\n",
249 workstations[i], logon_workstation));
251 if (strequal(workstations[i], logon_workstation)) {
257 talloc_free(workstations);
260 return NT_STATUS_INVALID_WORKSTATION;
264 if (!logon_hours_ok(msg, name_for_logs)) {
265 return NT_STATUS_INVALID_LOGON_HOURS;
268 if (!allow_domain_trust) {
269 if (acct_flags & ACB_DOMTRUST) {
270 DEBUG(2,("sam_account_ok: Domain trust account %s denied by server\n", name_for_logs));
271 return NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT;
274 if (!(logon_parameters & MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT)) {
275 if (acct_flags & ACB_SVRTRUST) {
276 DEBUG(2,("sam_account_ok: Server trust account %s denied by server\n", name_for_logs));
277 return NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT;
280 if (!(logon_parameters & MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT)) {
281 /* TODO: this fails with current solaris client. We
282 need to work with Gordon to work out why */
283 if (acct_flags & ACB_WSTRUST) {
284 DEBUG(4,("sam_account_ok: Wksta trust account %s denied by server\n", name_for_logs));
285 return NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT;
292 static NTSTATUS authsam_domain_group_filter(TALLOC_CTX *mem_ctx,
299 filter = talloc_strdup(mem_ctx, "(&(objectClass=group)");
300 if (filter == NULL) {
301 return NT_STATUS_NO_MEMORY;
305 * Skip all builtin groups, they're added later.
307 filter = talloc_asprintf_append_buffer(filter,
308 "(!(groupType:1.2.840.113556.1.4.803:=%u))",
309 GROUP_TYPE_BUILTIN_LOCAL_GROUP);
310 if (filter == NULL) {
311 return NT_STATUS_NO_MEMORY;
314 * Only include security groups.
316 filter = talloc_asprintf_append_buffer(filter,
317 "(groupType:1.2.840.113556.1.4.803:=%u))",
318 GROUP_TYPE_SECURITY_ENABLED);
319 if (filter == NULL) {
320 return NT_STATUS_NO_MEMORY;
327 _PUBLIC_ NTSTATUS authsam_make_user_info_dc(TALLOC_CTX *mem_ctx,
328 struct ldb_context *sam_ctx,
329 const char *netbios_name,
330 const char *domain_name,
331 const char *dns_domain_name,
332 struct ldb_dn *domain_dn,
333 struct ldb_message *msg,
334 DATA_BLOB user_sess_key,
335 DATA_BLOB lm_sess_key,
336 struct auth_user_info_dc **_user_info_dc)
339 struct auth_user_info_dc *user_info_dc;
340 struct auth_user_info *info;
341 const char *str = NULL;
343 /* SIDs for the account and his primary group */
344 struct dom_sid *account_sid;
345 const char *primary_group_string;
346 const char *primary_group_dn;
347 DATA_BLOB primary_group_blob;
348 /* SID structures for the expanded group memberships */
349 struct dom_sid *sids = NULL;
350 unsigned int num_sids = 0, i;
351 struct dom_sid *domain_sid;
353 struct ldb_message_element *el;
355 user_info_dc = talloc(mem_ctx, struct auth_user_info_dc);
356 NT_STATUS_HAVE_NO_MEMORY(user_info_dc);
358 tmp_ctx = talloc_new(user_info_dc);
359 if (user_info_dc == NULL) {
360 TALLOC_FREE(user_info_dc);
361 return NT_STATUS_NO_MEMORY;
364 sids = talloc_array(user_info_dc, struct dom_sid, 2);
366 TALLOC_FREE(user_info_dc);
367 return NT_STATUS_NO_MEMORY;
372 account_sid = samdb_result_dom_sid(user_info_dc, msg, "objectSid");
373 if (account_sid == NULL) {
374 TALLOC_FREE(user_info_dc);
375 return NT_STATUS_NO_MEMORY;
378 status = dom_sid_split_rid(tmp_ctx, account_sid, &domain_sid, NULL);
379 if (!NT_STATUS_IS_OK(status)) {
380 talloc_free(user_info_dc);
384 sids[PRIMARY_USER_SID_INDEX] = *account_sid;
385 sids[PRIMARY_GROUP_SID_INDEX] = *domain_sid;
386 sid_append_rid(&sids[PRIMARY_GROUP_SID_INDEX], ldb_msg_find_attr_as_uint(msg, "primaryGroupID", ~0));
389 * Filter out builtin groups from this token. We will search
390 * for builtin groups later, and not include them in the PAC
391 * or SamLogon validation info.
393 status = authsam_domain_group_filter(tmp_ctx, &filter);
394 if (!NT_STATUS_IS_OK(status)) {
395 TALLOC_FREE(user_info_dc);
399 primary_group_string = dom_sid_string(tmp_ctx, &sids[PRIMARY_GROUP_SID_INDEX]);
400 if (primary_group_string == NULL) {
401 TALLOC_FREE(user_info_dc);
402 return NT_STATUS_NO_MEMORY;
405 primary_group_dn = talloc_asprintf(tmp_ctx, "<SID=%s>", primary_group_string);
406 if (primary_group_dn == NULL) {
407 TALLOC_FREE(user_info_dc);
408 return NT_STATUS_NO_MEMORY;
411 primary_group_blob = data_blob_string_const(primary_group_dn);
413 /* Expands the primary group - this function takes in
414 * memberOf-like values, so we fake one up with the
415 * <SID=S-...> format of DN and then let it expand
416 * them, as long as they meet the filter - so only
417 * domain groups, not builtin groups
419 * The primary group is still treated specially, so we set the
420 * 'only childs' flag to true
422 status = dsdb_expand_nested_groups(sam_ctx, &primary_group_blob, true, filter,
423 user_info_dc, &sids, &num_sids);
424 if (!NT_STATUS_IS_OK(status)) {
425 talloc_free(user_info_dc);
429 /* Expands the additional groups */
430 el = ldb_msg_find_element(msg, "memberOf");
431 for (i = 0; el && i < el->num_values; i++) {
432 /* This function takes in memberOf values and expands
433 * them, as long as they meet the filter - so only
434 * domain groups, not builtin groups */
435 status = dsdb_expand_nested_groups(sam_ctx, &el->values[i], false, filter,
436 user_info_dc, &sids, &num_sids);
437 if (!NT_STATUS_IS_OK(status)) {
438 talloc_free(user_info_dc);
443 user_info_dc->sids = sids;
444 user_info_dc->num_sids = num_sids;
446 user_info_dc->info = info = talloc_zero(user_info_dc, struct auth_user_info);
447 NT_STATUS_HAVE_NO_MEMORY(user_info_dc->info);
449 info->account_name = talloc_steal(info,
450 ldb_msg_find_attr_as_string(msg, "sAMAccountName", NULL));
452 info->user_principal_name = talloc_steal(info,
453 ldb_msg_find_attr_as_string(msg, "userPrincipalName", NULL));
454 if (info->user_principal_name == NULL && dns_domain_name != NULL) {
455 info->user_principal_name = talloc_asprintf(info, "%s@%s",
458 if (info->user_principal_name == NULL) {
459 TALLOC_FREE(user_info_dc);
460 return NT_STATUS_NO_MEMORY;
462 info->user_principal_constructed = true;
465 info->domain_name = talloc_strdup(info, domain_name);
466 if (info->domain_name == NULL) {
467 TALLOC_FREE(user_info_dc);
468 return NT_STATUS_NO_MEMORY;
471 if (dns_domain_name != NULL) {
472 info->dns_domain_name = talloc_strdup(info, dns_domain_name);
473 if (info->dns_domain_name == NULL) {
474 TALLOC_FREE(user_info_dc);
475 return NT_STATUS_NO_MEMORY;
479 str = ldb_msg_find_attr_as_string(msg, "displayName", "");
480 info->full_name = talloc_strdup(info, str);
481 if (info->full_name == NULL) {
482 TALLOC_FREE(user_info_dc);
483 return NT_STATUS_NO_MEMORY;
486 str = ldb_msg_find_attr_as_string(msg, "scriptPath", "");
487 info->logon_script = talloc_strdup(info, str);
488 if (info->logon_script == NULL) {
489 TALLOC_FREE(user_info_dc);
490 return NT_STATUS_NO_MEMORY;
493 str = ldb_msg_find_attr_as_string(msg, "profilePath", "");
494 info->profile_path = talloc_strdup(info, str);
495 if (info->profile_path == NULL) {
496 TALLOC_FREE(user_info_dc);
497 return NT_STATUS_NO_MEMORY;
500 str = ldb_msg_find_attr_as_string(msg, "homeDirectory", "");
501 info->home_directory = talloc_strdup(info, str);
502 if (info->home_directory == NULL) {
503 TALLOC_FREE(user_info_dc);
504 return NT_STATUS_NO_MEMORY;
507 str = ldb_msg_find_attr_as_string(msg, "homeDrive", "");
508 info->home_drive = talloc_strdup(info, str);
509 if (info->home_drive == NULL) {
510 TALLOC_FREE(user_info_dc);
511 return NT_STATUS_NO_MEMORY;
514 info->logon_server = talloc_strdup(info, netbios_name);
515 if (info->logon_server == NULL) {
516 TALLOC_FREE(user_info_dc);
517 return NT_STATUS_NO_MEMORY;
520 info->last_logon = samdb_result_nttime(msg, "lastLogon", 0);
521 info->last_logoff = samdb_result_last_logoff(msg);
522 info->acct_expiry = samdb_result_account_expires(msg);
523 info->last_password_change = samdb_result_nttime(msg,
525 info->allow_password_change
526 = samdb_result_allow_password_change(sam_ctx, mem_ctx,
527 domain_dn, msg, "pwdLastSet");
528 info->force_password_change = samdb_result_nttime(msg,
529 "msDS-UserPasswordExpiryTimeComputed", 0);
530 info->logon_count = ldb_msg_find_attr_as_uint(msg, "logonCount", 0);
531 info->bad_password_count = ldb_msg_find_attr_as_uint(msg, "badPwdCount",
534 info->acct_flags = samdb_result_acct_flags(msg, "msDS-User-Account-Control-Computed");
536 user_info_dc->user_session_key = data_blob_talloc(user_info_dc,
538 user_sess_key.length);
539 if (user_sess_key.data) {
540 if (user_info_dc->user_session_key.data == NULL) {
541 TALLOC_FREE(user_info_dc);
542 return NT_STATUS_NO_MEMORY;
545 user_info_dc->lm_session_key = data_blob_talloc(user_info_dc,
548 if (lm_sess_key.data) {
549 if (user_info_dc->lm_session_key.data == NULL) {
550 TALLOC_FREE(user_info_dc);
551 return NT_STATUS_NO_MEMORY;
555 if (info->acct_flags & ACB_SVRTRUST) {
556 /* the SID_NT_ENTERPRISE_DCS SID gets added into the
558 user_info_dc->sids = talloc_realloc(user_info_dc,
561 user_info_dc->num_sids+1);
562 if (user_info_dc->sids == NULL) {
563 TALLOC_FREE(user_info_dc);
564 return NT_STATUS_NO_MEMORY;
566 user_info_dc->sids[user_info_dc->num_sids] = global_sid_Enterprise_DCs;
567 user_info_dc->num_sids++;
570 if ((info->acct_flags & (ACB_PARTIAL_SECRETS_ACCOUNT | ACB_WSTRUST)) ==
571 (ACB_PARTIAL_SECRETS_ACCOUNT | ACB_WSTRUST)) {
572 /* the DOMAIN_RID_ENTERPRISE_READONLY_DCS PAC */
573 user_info_dc->sids = talloc_realloc(user_info_dc,
576 user_info_dc->num_sids+1);
577 if (user_info_dc->sids == NULL) {
578 TALLOC_FREE(user_info_dc);
579 return NT_STATUS_NO_MEMORY;
581 user_info_dc->sids[user_info_dc->num_sids] = *domain_sid;
582 sid_append_rid(&user_info_dc->sids[user_info_dc->num_sids],
583 DOMAIN_RID_ENTERPRISE_READONLY_DCS);
584 user_info_dc->num_sids++;
587 info->authenticated = true;
589 talloc_free(tmp_ctx);
590 *_user_info_dc = user_info_dc;
595 _PUBLIC_ NTSTATUS authsam_update_user_info_dc(TALLOC_CTX *mem_ctx,
596 struct ldb_context *sam_ctx,
597 struct auth_user_info_dc *user_info_dc)
605 * This function exists to expand group memberships
606 * in the local domain (forest), as the token
607 * may come from a different domain.
611 * Filter out builtin groups from this token. We will search
612 * for builtin groups later.
614 status = authsam_domain_group_filter(mem_ctx, &filter);
615 if (!NT_STATUS_IS_OK(status)) {
616 TALLOC_FREE(user_info_dc);
621 * We loop only over the existing number of
624 n = user_info_dc->num_sids;
625 for (i = 0; i < n; i++) {
626 struct dom_sid *sid = &user_info_dc->sids[i];
627 char sid_buf[DOM_SID_STR_BUFLEN] = {0,};
628 char dn_str[DOM_SID_STR_BUFLEN*2] = {0,};
629 DATA_BLOB dn_blob = data_blob_null;
632 len = dom_sid_string_buf(sid, sid_buf, sizeof(sid_buf));
633 if (len+1 > sizeof(sid_buf)) {
634 return NT_STATUS_INVALID_SID;
636 snprintf(dn_str, sizeof(dn_str), "<SID=%s>", sid_buf);
637 dn_blob = data_blob_string_const(dn_str);
640 * We already have the SID in the token, so set
641 * 'only childs' flag to true and add all
642 * groups which match the filter.
644 status = dsdb_expand_nested_groups(sam_ctx, &dn_blob,
648 &user_info_dc->num_sids);
649 if (!NT_STATUS_IS_OK(status)) {
657 NTSTATUS sam_get_results_principal(struct ldb_context *sam_ctx,
658 TALLOC_CTX *mem_ctx, const char *principal,
660 struct ldb_dn **domain_dn,
661 struct ldb_message **msg)
663 struct ldb_dn *user_dn;
665 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
669 return NT_STATUS_NO_MEMORY;
672 nt_status = crack_user_principal_name(sam_ctx, tmp_ctx, principal,
673 &user_dn, domain_dn);
674 if (!NT_STATUS_IS_OK(nt_status)) {
675 talloc_free(tmp_ctx);
679 /* pull the user attributes */
680 ret = dsdb_search_one(sam_ctx, tmp_ctx, msg, user_dn,
681 LDB_SCOPE_BASE, attrs,
682 DSDB_SEARCH_SHOW_EXTENDED_DN | DSDB_SEARCH_NO_GLOBAL_CATALOG,
684 if (ret != LDB_SUCCESS) {
685 talloc_free(tmp_ctx);
686 return NT_STATUS_INTERNAL_DB_CORRUPTION;
688 talloc_steal(mem_ctx, *msg);
689 talloc_steal(mem_ctx, *domain_dn);
690 talloc_free(tmp_ctx);
695 /* Used in the gensec_gssapi and gensec_krb5 server-side code, where the PAC isn't available, and for tokenGroups in the DSDB stack.
697 Supply either a principal or a DN
699 NTSTATUS authsam_get_user_info_dc_principal(TALLOC_CTX *mem_ctx,
700 struct loadparm_context *lp_ctx,
701 struct ldb_context *sam_ctx,
702 const char *principal,
703 struct ldb_dn *user_dn,
704 struct auth_user_info_dc **user_info_dc)
707 DATA_BLOB user_sess_key = data_blob(NULL, 0);
708 DATA_BLOB lm_sess_key = data_blob(NULL, 0);
710 struct ldb_message *msg;
711 struct ldb_dn *domain_dn;
713 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
715 return NT_STATUS_NO_MEMORY;
719 nt_status = sam_get_results_principal(sam_ctx, tmp_ctx, principal,
720 user_attrs, &domain_dn, &msg);
721 if (!NT_STATUS_IS_OK(nt_status)) {
722 talloc_free(tmp_ctx);
725 } else if (user_dn) {
726 struct dom_sid *user_sid, *domain_sid;
728 /* pull the user attributes */
729 ret = dsdb_search_one(sam_ctx, tmp_ctx, &msg, user_dn,
730 LDB_SCOPE_BASE, user_attrs,
731 DSDB_SEARCH_SHOW_EXTENDED_DN | DSDB_SEARCH_NO_GLOBAL_CATALOG,
733 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
734 talloc_free(tmp_ctx);
735 return NT_STATUS_NO_SUCH_USER;
736 } else if (ret != LDB_SUCCESS) {
737 talloc_free(tmp_ctx);
738 return NT_STATUS_INTERNAL_DB_CORRUPTION;
741 user_sid = samdb_result_dom_sid(msg, msg, "objectSid");
743 nt_status = dom_sid_split_rid(tmp_ctx, user_sid, &domain_sid, NULL);
744 if (!NT_STATUS_IS_OK(nt_status)) {
748 domain_dn = samdb_search_dn(sam_ctx, mem_ctx, NULL,
749 "(&(objectSid=%s)(objectClass=domain))",
750 ldap_encode_ndr_dom_sid(tmp_ctx, domain_sid));
752 DEBUG(3, ("authsam_get_user_info_dc_principal: Failed to find domain with: SID %s\n",
753 dom_sid_string(tmp_ctx, domain_sid)));
754 return NT_STATUS_NO_SUCH_USER;
758 return NT_STATUS_INVALID_PARAMETER;
761 nt_status = authsam_make_user_info_dc(tmp_ctx, sam_ctx,
762 lpcfg_netbios_name(lp_ctx),
763 lpcfg_sam_name(lp_ctx),
764 lpcfg_sam_dnsname(lp_ctx),
767 user_sess_key, lm_sess_key,
769 if (!NT_STATUS_IS_OK(nt_status)) {
770 talloc_free(tmp_ctx);
774 talloc_steal(mem_ctx, *user_info_dc);
775 talloc_free(tmp_ctx);
780 NTSTATUS authsam_update_bad_pwd_count(struct ldb_context *sam_ctx,
781 struct ldb_message *msg,
782 struct ldb_dn *domain_dn)
784 const char *attrs[] = { "lockoutThreshold",
785 "lockOutObservationWindow",
791 struct ldb_result *domain_res;
792 struct ldb_message *msg_mod = NULL;
795 mem_ctx = talloc_new(msg);
796 if (mem_ctx == NULL) {
797 return NT_STATUS_NO_MEMORY;
800 ret = dsdb_search_dn(sam_ctx, mem_ctx, &domain_res, domain_dn, attrs, 0);
801 if (ret != LDB_SUCCESS) {
802 TALLOC_FREE(mem_ctx);
803 return NT_STATUS_INTERNAL_DB_CORRUPTION;
806 status = dsdb_update_bad_pwd_count(mem_ctx, sam_ctx,
807 msg, domain_res->msgs[0], &msg_mod);
808 if (!NT_STATUS_IS_OK(status)) {
809 TALLOC_FREE(mem_ctx);
813 if (msg_mod != NULL) {
814 struct ldb_request *req;
816 ret = ldb_build_mod_req(&req, sam_ctx, sam_ctx,
820 ldb_op_default_callback,
822 if (ret != LDB_SUCCESS) {
826 ret = ldb_request_add_control(req,
827 DSDB_CONTROL_FORCE_RODC_LOCAL_CHANGE,
829 if (ret != LDB_SUCCESS) {
834 ret = dsdb_autotransaction_request(sam_ctx, req);
839 if (ret != LDB_SUCCESS) {
840 DEBUG(0, ("Failed to update badPwdCount, badPasswordTime or set lockoutTime on %s: %s\n",
841 ldb_dn_get_linearized(msg_mod->dn), ldb_errstring(sam_ctx)));
842 TALLOC_FREE(mem_ctx);
843 return NT_STATUS_INTERNAL_ERROR;
846 TALLOC_FREE(mem_ctx);
851 static NTSTATUS authsam_update_lastlogon_timestamp(struct ldb_context *sam_ctx,
852 struct ldb_message *msg_mod,
853 struct ldb_dn *domain_dn,
854 NTTIME old_timestamp,
858 * We only set lastLogonTimestamp if the current value is older than
859 * now - msDS-LogonTimeSyncInterval days.
861 * msDS-LogonTimeSyncInterval is an int32_t number of days, while
862 * lastLogonTimestamp is in the 64 bit 100ns NTTIME format.
864 * The docs say: "the initial update, after the domain functional
865 * level is raised to DS_BEHAVIOR_WIN2003 or higher, is calculated as
866 * 14 days minus a random percentage of 5 days", but we aren't doing
867 * that. The blogosphere seems to think that this randomised update
868 * happens everytime, but [MS-ADA1] doesn't agree.
870 * Dochelp referred us to the following blog post:
871 * http://blogs.technet.com/b/askds/archive/2009/04/15/the-lastlogontimestamp-attribute-what-it-was-designed-for-and-how-it-works.aspx
873 * en msDS-LogonTimeSyncInterval is zero, the lastLogonTimestamp is
876 static const char *attrs[] = { "msDS-LogonTimeSyncInterval",
879 struct ldb_result *domain_res = NULL;
880 TALLOC_CTX *mem_ctx = NULL;
881 int32_t sync_interval;
882 NTTIME sync_interval_nt;
884 mem_ctx = talloc_new(msg_mod);
885 if (mem_ctx == NULL) {
886 return NT_STATUS_NO_MEMORY;
889 ret = dsdb_search_dn(sam_ctx, mem_ctx, &domain_res, domain_dn, attrs,
891 if (ret != LDB_SUCCESS || domain_res->count != 1) {
892 TALLOC_FREE(mem_ctx);
893 return NT_STATUS_INTERNAL_DB_CORRUPTION;
896 sync_interval = ldb_msg_find_attr_as_int(domain_res->msgs[0],
897 "msDS-LogonTimeSyncInterval",
899 DEBUG(5, ("sync interval is %d\n", sync_interval));
900 if (sync_interval == 0){
902 * Setting msDS-LogonTimeSyncInterval to zero is how you ask
903 * that nothing happens here.
905 TALLOC_FREE(mem_ctx);
908 else if (sync_interval >= 5){
910 * Subtract "a random percentage of 5" days. Presumably this
911 * percentage is between 0 and 100, and modulus is accurate
914 uint32_t r = generate_random() % 6;
916 DEBUG(5, ("randomised sync interval is %d (-%d)\n", sync_interval, r));
918 /* In the case where sync_interval < 5 there is no randomisation */
920 sync_interval_nt = sync_interval * 24LL * 3600LL * 10000000LL;
922 DEBUG(5, ("old timestamp is %lld, threshold %lld, diff %lld\n",
923 (long long int)old_timestamp,
924 (long long int)(now - sync_interval_nt),
925 (long long int)(old_timestamp - now + sync_interval_nt)));
927 if (old_timestamp > now){
928 DEBUG(0, ("lastLogonTimestamp is in the future! (%lld > %lld)\n",
929 (long long int)old_timestamp, (long long int)now));
932 } else if (old_timestamp < now - sync_interval_nt){
933 DEBUG(5, ("updating lastLogonTimestamp to %lld\n",
934 (long long int)now));
936 /* The time has come to update lastLogonTimestamp */
937 ret = samdb_msg_add_int64(sam_ctx, msg_mod, msg_mod,
938 "lastLogonTimestamp", now);
940 if (ret != LDB_SUCCESS) {
941 TALLOC_FREE(mem_ctx);
942 return NT_STATUS_NO_MEMORY;
945 TALLOC_FREE(mem_ctx);
949 /****************************************************************************
950 Look for the specified user in the sam, return ldb result structures
951 ****************************************************************************/
953 NTSTATUS authsam_search_account(TALLOC_CTX *mem_ctx, struct ldb_context *sam_ctx,
954 const char *account_name,
955 struct ldb_dn *domain_dn,
956 struct ldb_message **ret_msg)
960 /* pull the user attributes */
961 ret = dsdb_search_one(sam_ctx, mem_ctx, ret_msg, domain_dn, LDB_SCOPE_SUBTREE,
963 DSDB_SEARCH_SHOW_EXTENDED_DN,
964 "(&(sAMAccountName=%s)(objectclass=user))",
965 ldb_binary_encode_string(mem_ctx, account_name));
966 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
967 DEBUG(3,("sam_search_user: Couldn't find user [%s] in samdb, under %s\n",
968 account_name, ldb_dn_get_linearized(domain_dn)));
969 return NT_STATUS_NO_SUCH_USER;
971 if (ret != LDB_SUCCESS) {
972 return NT_STATUS_INTERNAL_DB_CORRUPTION;
979 /* Reset the badPwdCount to zero and update the lastLogon time. */
980 NTSTATUS authsam_logon_success_accounting(struct ldb_context *sam_ctx,
981 const struct ldb_message *msg,
982 struct ldb_dn *domain_dn,
983 bool interactive_or_kerberos,
984 struct netr_SendToSamBase **send_to_sam)
991 struct ldb_message *msg_mod;
993 struct timeval tv_now;
995 NTTIME lastLogonTimestamp;
996 bool am_rodc = false;
998 mem_ctx = talloc_new(msg);
999 if (mem_ctx == NULL) {
1000 return NT_STATUS_NO_MEMORY;
1003 lockoutTime = ldb_msg_find_attr_as_int64(msg, "lockoutTime", 0);
1004 dbBadPwdCount = ldb_msg_find_attr_as_int(msg, "badPwdCount", 0);
1005 if (interactive_or_kerberos) {
1006 badPwdCount = dbBadPwdCount;
1008 badPwdCount = samdb_result_effective_badPwdCount(sam_ctx, mem_ctx,
1011 lastLogonTimestamp =
1012 ldb_msg_find_attr_as_int64(msg, "lastLogonTimestamp", 0);
1014 DEBUG(5, ("lastLogonTimestamp is %lld\n",
1015 (long long int)lastLogonTimestamp));
1017 msg_mod = ldb_msg_new(mem_ctx);
1018 if (msg_mod == NULL) {
1019 TALLOC_FREE(mem_ctx);
1020 return NT_STATUS_NO_MEMORY;
1022 msg_mod->dn = msg->dn;
1024 if (lockoutTime != 0) {
1026 * This implies "badPwdCount" = 0, see samldb_lockout_time()
1028 ret = samdb_msg_add_int(sam_ctx, msg_mod, msg_mod, "lockoutTime", 0);
1029 if (ret != LDB_SUCCESS) {
1030 TALLOC_FREE(mem_ctx);
1031 return NT_STATUS_NO_MEMORY;
1033 } else if (badPwdCount != 0) {
1034 ret = samdb_msg_add_int(sam_ctx, msg_mod, msg_mod, "badPwdCount", 0);
1035 if (ret != LDB_SUCCESS) {
1036 TALLOC_FREE(mem_ctx);
1037 return NT_STATUS_NO_MEMORY;
1041 tv_now = timeval_current();
1042 now = timeval_to_nttime(&tv_now);
1044 if (interactive_or_kerberos ||
1045 (badPwdCount != 0 && lockoutTime == 0)) {
1046 ret = samdb_msg_add_int64(sam_ctx, msg_mod, msg_mod,
1048 if (ret != LDB_SUCCESS) {
1049 TALLOC_FREE(mem_ctx);
1050 return NT_STATUS_NO_MEMORY;
1054 if (interactive_or_kerberos) {
1057 logonCount = ldb_msg_find_attr_as_int(msg, "logonCount", 0);
1061 ret = samdb_msg_add_int(sam_ctx, msg_mod, msg_mod,
1062 "logonCount", logonCount);
1063 if (ret != LDB_SUCCESS) {
1064 TALLOC_FREE(mem_ctx);
1065 return NT_STATUS_NO_MEMORY;
1068 /* Set an unset logonCount to 0 on first successful login */
1069 if (ldb_msg_find_ldb_val(msg, "logonCount") == NULL) {
1070 ret = samdb_msg_add_int(sam_ctx, msg_mod, msg_mod,
1072 if (ret != LDB_SUCCESS) {
1073 TALLOC_FREE(mem_ctx);
1074 return NT_STATUS_NO_MEMORY;
1079 ret = samdb_rodc(sam_ctx, &am_rodc);
1080 if (ret != LDB_SUCCESS) {
1081 TALLOC_FREE(mem_ctx);
1082 return NT_STATUS_INTERNAL_ERROR;
1086 status = authsam_update_lastlogon_timestamp(sam_ctx, msg_mod, domain_dn,
1087 lastLogonTimestamp, now);
1088 if (!NT_STATUS_IS_OK(status)) {
1089 TALLOC_FREE(mem_ctx);
1090 return NT_STATUS_NO_MEMORY;
1093 /* Perform the (async) SendToSAM calls for MS-SAMS */
1094 if (dbBadPwdCount != 0 && send_to_sam != NULL) {
1095 struct netr_SendToSamBase *base_msg;
1096 struct GUID guid = samdb_result_guid(msg, "objectGUID");
1097 base_msg = talloc_zero(msg, struct netr_SendToSamBase);
1099 base_msg->message_type = SendToSamResetBadPasswordCount;
1100 base_msg->message_size = 16;
1101 base_msg->message.reset_bad_password.guid = guid;
1102 *send_to_sam = base_msg;
1106 if (msg_mod->num_elements > 0) {
1108 struct ldb_request *req;
1110 /* mark all the message elements as LDB_FLAG_MOD_REPLACE */
1111 for (i=0;i<msg_mod->num_elements;i++) {
1112 msg_mod->elements[i].flags = LDB_FLAG_MOD_REPLACE;
1115 ret = ldb_build_mod_req(&req, sam_ctx, sam_ctx,
1119 ldb_op_default_callback,
1121 if (ret != LDB_SUCCESS) {
1125 ret = ldb_request_add_control(req,
1126 DSDB_CONTROL_FORCE_RODC_LOCAL_CHANGE,
1128 if (ret != LDB_SUCCESS) {
1133 ret = dsdb_autotransaction_request(sam_ctx, req);
1138 if (ret != LDB_SUCCESS) {
1139 DEBUG(0, ("Failed to set badPwdCount and lockoutTime "
1140 "to 0 and/or lastlogon to now (%lld) "
1141 "%s: %s\n", (long long int)now,
1142 ldb_dn_get_linearized(msg_mod->dn),
1143 ldb_errstring(sam_ctx)));
1144 TALLOC_FREE(mem_ctx);
1145 return NT_STATUS_INTERNAL_ERROR;
1148 TALLOC_FREE(mem_ctx);
1149 return NT_STATUS_OK;