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"
35 #define KRBTGT_ATTRS \
36 /* required for the krb5 kdc */ \
39 "userPrincipalName", \
40 "servicePrincipalName", \
41 "msDS-KeyVersionNumber", \
42 "msDS-SecondaryKrbTgtNumber", \
43 "msDS-SupportedEncryptionTypes", \
44 "supplementalCredentials", \
45 "msDS-AllowedToDelegateTo", \
51 "userAccountControl", \
52 "msDS-User-Account-Control-Computed", \
56 "msDS-UserPasswordExpiryTimeComputed", \
59 const char *krbtgt_attrs
[] = {
63 const char *server_attrs
[] = {
67 const char *user_attrs
[] = {
73 * To allow us to zero the badPwdCount and lockoutTime on
74 * successful logon, without database churn
79 * Needed for SendToSAM requests
83 /* check 'allowed workstations' */
86 /* required for user_info_dc, not access control: */
106 /****************************************************************************
107 Check if a user is allowed to logon at this time. Note this is the
108 servers local time, as logon hours are just specified as a weekly
110 ****************************************************************************/
112 static bool logon_hours_ok(struct ldb_message
*msg
, const char *name_for_logs
)
114 /* In logon hours first bit is Sunday from 12AM to 1AM */
115 const struct ldb_val
*hours
;
119 uint8_t bitmask
, bitpos
;
121 hours
= ldb_msg_find_ldb_val(msg
, "logonHours");
123 DEBUG(5,("logon_hours_ok: No hours restrictions for user %s\n", name_for_logs
));
127 if (hours
->length
!= 168/8) {
128 DEBUG(5,("logon_hours_ok: malformed logon hours restrictions for user %s\n", name_for_logs
));
132 lasttime
= time(NULL
);
133 utctime
= gmtime(&lasttime
);
135 DEBUG(1, ("logon_hours_ok: failed to get gmtime. Failing logon for user %s\n",
140 /* find the corresponding byte and bit */
141 bitpos
= (utctime
->tm_wday
* 24 + utctime
->tm_hour
) % 168;
142 bitmask
= 1 << (bitpos
% 8);
144 if (! (hours
->data
[bitpos
/8] & bitmask
)) {
145 struct tm
*t
= localtime(&lasttime
);
147 asct
= "INVALID TIME";
151 asct
= "INVALID TIME";
155 DEBUG(1, ("logon_hours_ok: Account for user %s not allowed to "
156 "logon at this time (%s).\n",
157 name_for_logs
, asct
));
161 asct
= asctime(utctime
);
162 DEBUG(5,("logon_hours_ok: user %s allowed to logon at this time (%s)\n",
163 name_for_logs
, asct
? asct
: "UNKNOWN TIME" ));
168 /****************************************************************************
169 Do a specific test for a SAM_ACCOUNT being valid for this connection
170 (ie not disabled, expired and the like).
171 ****************************************************************************/
172 _PUBLIC_ NTSTATUS
authsam_account_ok(TALLOC_CTX
*mem_ctx
,
173 struct ldb_context
*sam_ctx
,
174 uint32_t logon_parameters
,
175 struct ldb_dn
*domain_dn
,
176 struct ldb_message
*msg
,
177 const char *logon_workstation
,
178 const char *name_for_logs
,
179 bool allow_domain_trust
,
180 bool password_change
)
183 const char *workstation_list
;
185 NTTIME must_change_time
;
186 struct timeval tv_now
= timeval_current();
187 NTTIME now
= timeval_to_nttime(&tv_now
);
189 DEBUG(4,("authsam_account_ok: Checking SMB password for user %s\n", name_for_logs
));
191 acct_flags
= samdb_result_acct_flags(msg
, "msDS-User-Account-Control-Computed");
193 acct_expiry
= samdb_result_account_expires(msg
);
195 /* Check for when we must change this password, taking the
196 * userAccountControl flags into account */
197 must_change_time
= samdb_result_nttime(msg
,
198 "msDS-UserPasswordExpiryTimeComputed", 0);
200 workstation_list
= ldb_msg_find_attr_as_string(msg
, "userWorkstations", NULL
);
202 /* Quit if the account was disabled. */
203 if (acct_flags
& ACB_DISABLED
) {
204 DEBUG(2,("authsam_account_ok: Account for user '%s' was disabled.\n", name_for_logs
));
205 return NT_STATUS_ACCOUNT_DISABLED
;
208 /* Quit if the account was locked out. */
209 if (acct_flags
& ACB_AUTOLOCK
) {
210 DEBUG(2,("authsam_account_ok: Account for user %s was locked out.\n", name_for_logs
));
211 return NT_STATUS_ACCOUNT_LOCKED_OUT
;
214 /* Test account expire time */
215 if (now
> acct_expiry
) {
216 DEBUG(2,("authsam_account_ok: Account for user '%s' has expired.\n", name_for_logs
));
217 DEBUG(3,("authsam_account_ok: Account expired at '%s'.\n",
218 nt_time_string(mem_ctx
, acct_expiry
)));
219 return NT_STATUS_ACCOUNT_EXPIRED
;
222 /* check for immediate expiry "must change at next logon" (but not if this is a password change request) */
223 if ((must_change_time
== 0) && !password_change
) {
224 DEBUG(2,("sam_account_ok: Account for user '%s' password must change!.\n",
226 return NT_STATUS_PASSWORD_MUST_CHANGE
;
229 /* check for expired password (but not if this is a password change request) */
230 if ((must_change_time
< now
) && !password_change
) {
231 DEBUG(2,("sam_account_ok: Account for user '%s' password expired!.\n",
233 DEBUG(2,("sam_account_ok: Password expired at '%s' unix time.\n",
234 nt_time_string(mem_ctx
, must_change_time
)));
235 return NT_STATUS_PASSWORD_EXPIRED
;
238 /* Test workstation. Workstation list is comma separated. */
239 if (logon_workstation
&& workstation_list
&& *workstation_list
) {
240 bool invalid_ws
= true;
242 char **workstations
= str_list_make(mem_ctx
, workstation_list
, ",");
244 for (i
= 0; workstations
&& workstations
[i
]; i
++) {
245 DEBUG(10,("sam_account_ok: checking for workstation match '%s' and '%s'\n",
246 workstations
[i
], logon_workstation
));
248 if (strequal(workstations
[i
], logon_workstation
)) {
254 talloc_free(workstations
);
257 return NT_STATUS_INVALID_WORKSTATION
;
261 if (!logon_hours_ok(msg
, name_for_logs
)) {
262 return NT_STATUS_INVALID_LOGON_HOURS
;
265 if (!allow_domain_trust
) {
266 if (acct_flags
& ACB_DOMTRUST
) {
267 DEBUG(2,("sam_account_ok: Domain trust account %s denied by server\n", name_for_logs
));
268 return NT_STATUS_NOLOGON_INTERDOMAIN_TRUST_ACCOUNT
;
271 if (!(logon_parameters
& MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT
)) {
272 if (acct_flags
& ACB_SVRTRUST
) {
273 DEBUG(2,("sam_account_ok: Server trust account %s denied by server\n", name_for_logs
));
274 return NT_STATUS_NOLOGON_SERVER_TRUST_ACCOUNT
;
277 if (!(logon_parameters
& MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT
)) {
278 /* TODO: this fails with current solaris client. We
279 need to work with Gordon to work out why */
280 if (acct_flags
& ACB_WSTRUST
) {
281 DEBUG(4,("sam_account_ok: Wksta trust account %s denied by server\n", name_for_logs
));
282 return NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT
;
289 _PUBLIC_ NTSTATUS
authsam_make_user_info_dc(TALLOC_CTX
*mem_ctx
,
290 struct ldb_context
*sam_ctx
,
291 const char *netbios_name
,
292 const char *domain_name
,
293 const char *dns_domain_name
,
294 struct ldb_dn
*domain_dn
,
295 struct ldb_message
*msg
,
296 DATA_BLOB user_sess_key
,
297 DATA_BLOB lm_sess_key
,
298 struct auth_user_info_dc
**_user_info_dc
)
301 struct auth_user_info_dc
*user_info_dc
;
302 struct auth_user_info
*info
;
303 const char *str
, *filter
;
304 /* SIDs for the account and his primary group */
305 struct dom_sid
*account_sid
;
306 const char *primary_group_string
;
307 const char *primary_group_dn
;
308 DATA_BLOB primary_group_blob
;
309 /* SID structures for the expanded group memberships */
310 struct dom_sid
*sids
= NULL
;
311 unsigned int num_sids
= 0, i
;
312 struct dom_sid
*domain_sid
;
314 struct ldb_message_element
*el
;
316 user_info_dc
= talloc(mem_ctx
, struct auth_user_info_dc
);
317 NT_STATUS_HAVE_NO_MEMORY(user_info_dc
);
319 tmp_ctx
= talloc_new(user_info_dc
);
320 if (user_info_dc
== NULL
) {
321 TALLOC_FREE(user_info_dc
);
322 return NT_STATUS_NO_MEMORY
;
325 sids
= talloc_array(user_info_dc
, struct dom_sid
, 2);
327 TALLOC_FREE(user_info_dc
);
328 return NT_STATUS_NO_MEMORY
;
333 account_sid
= samdb_result_dom_sid(user_info_dc
, msg
, "objectSid");
334 if (account_sid
== NULL
) {
335 TALLOC_FREE(user_info_dc
);
336 return NT_STATUS_NO_MEMORY
;
339 status
= dom_sid_split_rid(tmp_ctx
, account_sid
, &domain_sid
, NULL
);
340 if (!NT_STATUS_IS_OK(status
)) {
341 talloc_free(user_info_dc
);
345 sids
[PRIMARY_USER_SID_INDEX
] = *account_sid
;
346 sids
[PRIMARY_GROUP_SID_INDEX
] = *domain_sid
;
347 sid_append_rid(&sids
[PRIMARY_GROUP_SID_INDEX
], ldb_msg_find_attr_as_uint(msg
, "primaryGroupID", ~0));
349 /* Filter out builtin groups from this token. We will search
350 * for builtin groups later, and not include them in the PAC
351 * on SamLogon validation info */
352 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
);
353 if (filter
== NULL
) {
354 TALLOC_FREE(user_info_dc
);
355 return NT_STATUS_NO_MEMORY
;
358 primary_group_string
= dom_sid_string(tmp_ctx
, &sids
[PRIMARY_GROUP_SID_INDEX
]);
359 if (primary_group_string
== NULL
) {
360 TALLOC_FREE(user_info_dc
);
361 return NT_STATUS_NO_MEMORY
;
364 primary_group_dn
= talloc_asprintf(tmp_ctx
, "<SID=%s>", primary_group_string
);
365 if (primary_group_dn
== NULL
) {
366 TALLOC_FREE(user_info_dc
);
367 return NT_STATUS_NO_MEMORY
;
370 primary_group_blob
= data_blob_string_const(primary_group_dn
);
372 /* Expands the primary group - this function takes in
373 * memberOf-like values, so we fake one up with the
374 * <SID=S-...> format of DN and then let it expand
375 * them, as long as they meet the filter - so only
376 * domain groups, not builtin groups
378 * The primary group is still treated specially, so we set the
379 * 'only childs' flag to true
381 status
= dsdb_expand_nested_groups(sam_ctx
, &primary_group_blob
, true, filter
,
382 user_info_dc
, &sids
, &num_sids
);
383 if (!NT_STATUS_IS_OK(status
)) {
384 talloc_free(user_info_dc
);
388 /* Expands the additional groups */
389 el
= ldb_msg_find_element(msg
, "memberOf");
390 for (i
= 0; el
&& i
< el
->num_values
; i
++) {
391 /* This function takes in memberOf values and expands
392 * them, as long as they meet the filter - so only
393 * domain groups, not builtin groups */
394 status
= dsdb_expand_nested_groups(sam_ctx
, &el
->values
[i
], false, filter
,
395 user_info_dc
, &sids
, &num_sids
);
396 if (!NT_STATUS_IS_OK(status
)) {
397 talloc_free(user_info_dc
);
402 user_info_dc
->sids
= sids
;
403 user_info_dc
->num_sids
= num_sids
;
405 user_info_dc
->info
= info
= talloc_zero(user_info_dc
, struct auth_user_info
);
406 NT_STATUS_HAVE_NO_MEMORY(user_info_dc
->info
);
408 info
->account_name
= talloc_steal(info
,
409 ldb_msg_find_attr_as_string(msg
, "sAMAccountName", NULL
));
411 info
->user_principal_name
= talloc_steal(info
,
412 ldb_msg_find_attr_as_string(msg
, "userPrincipalName", NULL
));
413 if (info
->user_principal_name
== NULL
&& dns_domain_name
!= NULL
) {
414 info
->user_principal_name
= talloc_asprintf(info
, "%s@%s",
417 if (info
->user_principal_name
== NULL
) {
418 TALLOC_FREE(user_info_dc
);
419 return NT_STATUS_NO_MEMORY
;
421 info
->user_principal_constructed
= true;
424 info
->domain_name
= talloc_strdup(info
, domain_name
);
425 if (info
->domain_name
== NULL
) {
426 TALLOC_FREE(user_info_dc
);
427 return NT_STATUS_NO_MEMORY
;
430 if (dns_domain_name
!= NULL
) {
431 info
->dns_domain_name
= talloc_strdup(info
, dns_domain_name
);
432 if (info
->dns_domain_name
== NULL
) {
433 TALLOC_FREE(user_info_dc
);
434 return NT_STATUS_NO_MEMORY
;
438 str
= ldb_msg_find_attr_as_string(msg
, "displayName", "");
439 info
->full_name
= talloc_strdup(info
, str
);
440 if (info
->full_name
== NULL
) {
441 TALLOC_FREE(user_info_dc
);
442 return NT_STATUS_NO_MEMORY
;
445 str
= ldb_msg_find_attr_as_string(msg
, "scriptPath", "");
446 info
->logon_script
= talloc_strdup(info
, str
);
447 if (info
->logon_script
== NULL
) {
448 TALLOC_FREE(user_info_dc
);
449 return NT_STATUS_NO_MEMORY
;
452 str
= ldb_msg_find_attr_as_string(msg
, "profilePath", "");
453 info
->profile_path
= talloc_strdup(info
, str
);
454 if (info
->profile_path
== NULL
) {
455 TALLOC_FREE(user_info_dc
);
456 return NT_STATUS_NO_MEMORY
;
459 str
= ldb_msg_find_attr_as_string(msg
, "homeDirectory", "");
460 info
->home_directory
= talloc_strdup(info
, str
);
461 if (info
->home_directory
== NULL
) {
462 TALLOC_FREE(user_info_dc
);
463 return NT_STATUS_NO_MEMORY
;
466 str
= ldb_msg_find_attr_as_string(msg
, "homeDrive", "");
467 info
->home_drive
= talloc_strdup(info
, str
);
468 if (info
->home_drive
== NULL
) {
469 TALLOC_FREE(user_info_dc
);
470 return NT_STATUS_NO_MEMORY
;
473 info
->logon_server
= talloc_strdup(info
, netbios_name
);
474 if (info
->logon_server
== NULL
) {
475 TALLOC_FREE(user_info_dc
);
476 return NT_STATUS_NO_MEMORY
;
479 info
->last_logon
= samdb_result_nttime(msg
, "lastLogon", 0);
480 info
->last_logoff
= samdb_result_last_logoff(msg
);
481 info
->acct_expiry
= samdb_result_account_expires(msg
);
482 info
->last_password_change
= samdb_result_nttime(msg
,
484 info
->allow_password_change
485 = samdb_result_allow_password_change(sam_ctx
, mem_ctx
,
486 domain_dn
, msg
, "pwdLastSet");
487 info
->force_password_change
= samdb_result_nttime(msg
,
488 "msDS-UserPasswordExpiryTimeComputed", 0);
489 info
->logon_count
= ldb_msg_find_attr_as_uint(msg
, "logonCount", 0);
490 info
->bad_password_count
= ldb_msg_find_attr_as_uint(msg
, "badPwdCount",
493 info
->acct_flags
= samdb_result_acct_flags(msg
, "msDS-User-Account-Control-Computed");
495 user_info_dc
->user_session_key
= data_blob_talloc(user_info_dc
,
497 user_sess_key
.length
);
498 if (user_sess_key
.data
) {
499 if (user_info_dc
->user_session_key
.data
== NULL
) {
500 TALLOC_FREE(user_info_dc
);
501 return NT_STATUS_NO_MEMORY
;
504 user_info_dc
->lm_session_key
= data_blob_talloc(user_info_dc
,
507 if (lm_sess_key
.data
) {
508 if (user_info_dc
->lm_session_key
.data
== NULL
) {
509 TALLOC_FREE(user_info_dc
);
510 return NT_STATUS_NO_MEMORY
;
514 if (info
->acct_flags
& ACB_SVRTRUST
) {
515 /* the SID_NT_ENTERPRISE_DCS SID gets added into the
517 user_info_dc
->sids
= talloc_realloc(user_info_dc
,
520 user_info_dc
->num_sids
+1);
521 if (user_info_dc
->sids
== NULL
) {
522 TALLOC_FREE(user_info_dc
);
523 return NT_STATUS_NO_MEMORY
;
525 user_info_dc
->sids
[user_info_dc
->num_sids
] = global_sid_Enterprise_DCs
;
526 user_info_dc
->num_sids
++;
529 if ((info
->acct_flags
& (ACB_PARTIAL_SECRETS_ACCOUNT
| ACB_WSTRUST
)) ==
530 (ACB_PARTIAL_SECRETS_ACCOUNT
| ACB_WSTRUST
)) {
531 /* the DOMAIN_RID_ENTERPRISE_READONLY_DCS PAC */
532 user_info_dc
->sids
= talloc_realloc(user_info_dc
,
535 user_info_dc
->num_sids
+1);
536 if (user_info_dc
->sids
== NULL
) {
537 TALLOC_FREE(user_info_dc
);
538 return NT_STATUS_NO_MEMORY
;
540 user_info_dc
->sids
[user_info_dc
->num_sids
] = *domain_sid
;
541 sid_append_rid(&user_info_dc
->sids
[user_info_dc
->num_sids
],
542 DOMAIN_RID_ENTERPRISE_READONLY_DCS
);
543 user_info_dc
->num_sids
++;
546 info
->authenticated
= true;
548 talloc_free(tmp_ctx
);
549 *_user_info_dc
= user_info_dc
;
554 NTSTATUS
sam_get_results_principal(struct ldb_context
*sam_ctx
,
555 TALLOC_CTX
*mem_ctx
, const char *principal
,
557 struct ldb_dn
**domain_dn
,
558 struct ldb_message
**msg
)
560 struct ldb_dn
*user_dn
;
562 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
566 return NT_STATUS_NO_MEMORY
;
569 nt_status
= crack_user_principal_name(sam_ctx
, tmp_ctx
, principal
,
570 &user_dn
, domain_dn
);
571 if (!NT_STATUS_IS_OK(nt_status
)) {
572 talloc_free(tmp_ctx
);
576 /* pull the user attributes */
577 ret
= dsdb_search_one(sam_ctx
, tmp_ctx
, msg
, user_dn
,
578 LDB_SCOPE_BASE
, attrs
,
579 DSDB_SEARCH_SHOW_EXTENDED_DN
| DSDB_SEARCH_NO_GLOBAL_CATALOG
,
581 if (ret
!= LDB_SUCCESS
) {
582 talloc_free(tmp_ctx
);
583 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
585 talloc_steal(mem_ctx
, *msg
);
586 talloc_steal(mem_ctx
, *domain_dn
);
587 talloc_free(tmp_ctx
);
592 /* Used in the gensec_gssapi and gensec_krb5 server-side code, where the PAC isn't available, and for tokenGroups in the DSDB stack.
594 Supply either a principal or a DN
596 NTSTATUS
authsam_get_user_info_dc_principal(TALLOC_CTX
*mem_ctx
,
597 struct loadparm_context
*lp_ctx
,
598 struct ldb_context
*sam_ctx
,
599 const char *principal
,
600 struct ldb_dn
*user_dn
,
601 struct auth_user_info_dc
**user_info_dc
)
604 DATA_BLOB user_sess_key
= data_blob(NULL
, 0);
605 DATA_BLOB lm_sess_key
= data_blob(NULL
, 0);
607 struct ldb_message
*msg
;
608 struct ldb_dn
*domain_dn
;
610 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
612 return NT_STATUS_NO_MEMORY
;
616 nt_status
= sam_get_results_principal(sam_ctx
, tmp_ctx
, principal
,
617 user_attrs
, &domain_dn
, &msg
);
618 if (!NT_STATUS_IS_OK(nt_status
)) {
619 talloc_free(tmp_ctx
);
622 } else if (user_dn
) {
623 struct dom_sid
*user_sid
, *domain_sid
;
625 /* pull the user attributes */
626 ret
= dsdb_search_one(sam_ctx
, tmp_ctx
, &msg
, user_dn
,
627 LDB_SCOPE_BASE
, user_attrs
,
628 DSDB_SEARCH_SHOW_EXTENDED_DN
| DSDB_SEARCH_NO_GLOBAL_CATALOG
,
630 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
631 talloc_free(tmp_ctx
);
632 return NT_STATUS_NO_SUCH_USER
;
633 } else if (ret
!= LDB_SUCCESS
) {
634 talloc_free(tmp_ctx
);
635 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
638 user_sid
= samdb_result_dom_sid(msg
, msg
, "objectSid");
640 nt_status
= dom_sid_split_rid(tmp_ctx
, user_sid
, &domain_sid
, NULL
);
641 if (!NT_STATUS_IS_OK(nt_status
)) {
645 domain_dn
= samdb_search_dn(sam_ctx
, mem_ctx
, NULL
,
646 "(&(objectSid=%s)(objectClass=domain))",
647 ldap_encode_ndr_dom_sid(tmp_ctx
, domain_sid
));
649 DEBUG(3, ("authsam_get_user_info_dc_principal: Failed to find domain with: SID %s\n",
650 dom_sid_string(tmp_ctx
, domain_sid
)));
651 return NT_STATUS_NO_SUCH_USER
;
655 return NT_STATUS_INVALID_PARAMETER
;
658 nt_status
= authsam_make_user_info_dc(tmp_ctx
, sam_ctx
,
659 lpcfg_netbios_name(lp_ctx
),
660 lpcfg_sam_name(lp_ctx
),
661 lpcfg_sam_dnsname(lp_ctx
),
664 user_sess_key
, lm_sess_key
,
666 if (!NT_STATUS_IS_OK(nt_status
)) {
667 talloc_free(tmp_ctx
);
671 talloc_steal(mem_ctx
, *user_info_dc
);
672 talloc_free(tmp_ctx
);
677 NTSTATUS
authsam_update_bad_pwd_count(struct ldb_context
*sam_ctx
,
678 struct ldb_message
*msg
,
679 struct ldb_dn
*domain_dn
)
681 const char *attrs
[] = { "lockoutThreshold",
682 "lockOutObservationWindow",
688 struct ldb_result
*domain_res
;
689 struct ldb_message
*msg_mod
= NULL
;
692 mem_ctx
= talloc_new(msg
);
693 if (mem_ctx
== NULL
) {
694 return NT_STATUS_NO_MEMORY
;
697 ret
= dsdb_search_dn(sam_ctx
, mem_ctx
, &domain_res
, domain_dn
, attrs
, 0);
698 if (ret
!= LDB_SUCCESS
) {
699 TALLOC_FREE(mem_ctx
);
700 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
703 status
= dsdb_update_bad_pwd_count(mem_ctx
, sam_ctx
,
704 msg
, domain_res
->msgs
[0], &msg_mod
);
705 if (!NT_STATUS_IS_OK(status
)) {
706 TALLOC_FREE(mem_ctx
);
710 if (msg_mod
!= NULL
) {
711 struct ldb_request
*req
;
713 ret
= ldb_build_mod_req(&req
, sam_ctx
, sam_ctx
,
717 ldb_op_default_callback
,
719 if (ret
!= LDB_SUCCESS
) {
723 ret
= ldb_request_add_control(req
,
724 DSDB_CONTROL_FORCE_RODC_LOCAL_CHANGE
,
726 if (ret
!= LDB_SUCCESS
) {
731 ret
= dsdb_autotransaction_request(sam_ctx
, req
);
736 if (ret
!= LDB_SUCCESS
) {
737 DEBUG(0, ("Failed to update badPwdCount, badPasswordTime or set lockoutTime 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
);
748 static NTSTATUS
authsam_update_lastlogon_timestamp(struct ldb_context
*sam_ctx
,
749 struct ldb_message
*msg_mod
,
750 struct ldb_dn
*domain_dn
,
751 NTTIME old_timestamp
,
755 * We only set lastLogonTimestamp if the current value is older than
756 * now - msDS-LogonTimeSyncInterval days.
758 * msDS-LogonTimeSyncInterval is an int32_t number of days, while
759 * lastLogonTimestamp is in the 64 bit 100ns NTTIME format.
761 * The docs say: "the initial update, after the domain functional
762 * level is raised to DS_BEHAVIOR_WIN2003 or higher, is calculated as
763 * 14 days minus a random percentage of 5 days", but we aren't doing
764 * that. The blogosphere seems to think that this randomised update
765 * happens everytime, but [MS-ADA1] doesn't agree.
767 * Dochelp referred us to the following blog post:
768 * http://blogs.technet.com/b/askds/archive/2009/04/15/the-lastlogontimestamp-attribute-what-it-was-designed-for-and-how-it-works.aspx
770 * en msDS-LogonTimeSyncInterval is zero, the lastLogonTimestamp is
773 static const char *attrs
[] = { "msDS-LogonTimeSyncInterval",
776 struct ldb_result
*domain_res
= NULL
;
777 TALLOC_CTX
*mem_ctx
= NULL
;
778 int32_t sync_interval
;
779 NTTIME sync_interval_nt
;
781 mem_ctx
= talloc_new(msg_mod
);
782 if (mem_ctx
== NULL
) {
783 return NT_STATUS_NO_MEMORY
;
786 ret
= dsdb_search_dn(sam_ctx
, mem_ctx
, &domain_res
, domain_dn
, attrs
,
788 if (ret
!= LDB_SUCCESS
|| domain_res
->count
!= 1) {
789 TALLOC_FREE(mem_ctx
);
790 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
793 sync_interval
= ldb_msg_find_attr_as_int(domain_res
->msgs
[0],
794 "msDS-LogonTimeSyncInterval",
796 DEBUG(5, ("sync interval is %d\n", sync_interval
));
797 if (sync_interval
== 0){
799 * Setting msDS-LogonTimeSyncInterval to zero is how you ask
800 * that nothing happens here.
802 TALLOC_FREE(mem_ctx
);
805 else if (sync_interval
>= 5){
807 * Subtract "a random percentage of 5" days. Presumably this
808 * percentage is between 0 and 100, and modulus is accurate
811 uint32_t r
= generate_random() % 6;
813 DEBUG(5, ("randomised sync interval is %d (-%d)\n", sync_interval
, r
));
815 /* In the case where sync_interval < 5 there is no randomisation */
817 sync_interval_nt
= sync_interval
* 24LL * 3600LL * 10000000LL;
819 DEBUG(5, ("old timestamp is %lld, threshold %lld, diff %lld\n",
820 (long long int)old_timestamp
,
821 (long long int)(now
- sync_interval_nt
),
822 (long long int)(old_timestamp
- now
+ sync_interval_nt
)));
824 if (old_timestamp
> now
){
825 DEBUG(0, ("lastLogonTimestamp is in the future! (%lld > %lld)\n",
826 (long long int)old_timestamp
, (long long int)now
));
829 } else if (old_timestamp
< now
- sync_interval_nt
){
830 DEBUG(5, ("updating lastLogonTimestamp to %lld\n",
831 (long long int)now
));
833 /* The time has come to update lastLogonTimestamp */
834 ret
= samdb_msg_add_int64(sam_ctx
, msg_mod
, msg_mod
,
835 "lastLogonTimestamp", now
);
837 if (ret
!= LDB_SUCCESS
) {
838 TALLOC_FREE(mem_ctx
);
839 return NT_STATUS_NO_MEMORY
;
842 TALLOC_FREE(mem_ctx
);
846 /****************************************************************************
847 Look for the specified user in the sam, return ldb result structures
848 ****************************************************************************/
850 NTSTATUS
authsam_search_account(TALLOC_CTX
*mem_ctx
, struct ldb_context
*sam_ctx
,
851 const char *account_name
,
852 struct ldb_dn
*domain_dn
,
853 struct ldb_message
**ret_msg
)
857 /* pull the user attributes */
858 ret
= dsdb_search_one(sam_ctx
, mem_ctx
, ret_msg
, domain_dn
, LDB_SCOPE_SUBTREE
,
860 DSDB_SEARCH_SHOW_EXTENDED_DN
,
861 "(&(sAMAccountName=%s)(objectclass=user))",
862 ldb_binary_encode_string(mem_ctx
, account_name
));
863 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
864 DEBUG(3,("sam_search_user: Couldn't find user [%s] in samdb, under %s\n",
865 account_name
, ldb_dn_get_linearized(domain_dn
)));
866 return NT_STATUS_NO_SUCH_USER
;
868 if (ret
!= LDB_SUCCESS
) {
869 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
876 /* Reset the badPwdCount to zero and update the lastLogon time. */
877 NTSTATUS
authsam_logon_success_accounting(struct ldb_context
*sam_ctx
,
878 const struct ldb_message
*msg
,
879 struct ldb_dn
*domain_dn
,
880 bool interactive_or_kerberos
,
881 struct netr_SendToSamBase
**send_to_sam
)
888 struct ldb_message
*msg_mod
;
890 struct timeval tv_now
;
892 NTTIME lastLogonTimestamp
;
893 bool am_rodc
= false;
895 mem_ctx
= talloc_new(msg
);
896 if (mem_ctx
== NULL
) {
897 return NT_STATUS_NO_MEMORY
;
900 lockoutTime
= ldb_msg_find_attr_as_int64(msg
, "lockoutTime", 0);
901 dbBadPwdCount
= ldb_msg_find_attr_as_int(msg
, "badPwdCount", 0);
902 if (interactive_or_kerberos
) {
903 badPwdCount
= dbBadPwdCount
;
905 badPwdCount
= samdb_result_effective_badPwdCount(sam_ctx
, mem_ctx
,
909 ldb_msg_find_attr_as_int64(msg
, "lastLogonTimestamp", 0);
911 DEBUG(5, ("lastLogonTimestamp is %lld\n",
912 (long long int)lastLogonTimestamp
));
914 msg_mod
= ldb_msg_new(mem_ctx
);
915 if (msg_mod
== NULL
) {
916 TALLOC_FREE(mem_ctx
);
917 return NT_STATUS_NO_MEMORY
;
919 msg_mod
->dn
= msg
->dn
;
921 if (lockoutTime
!= 0) {
923 * This implies "badPwdCount" = 0, see samldb_lockout_time()
925 ret
= samdb_msg_add_int(sam_ctx
, msg_mod
, msg_mod
, "lockoutTime", 0);
926 if (ret
!= LDB_SUCCESS
) {
927 TALLOC_FREE(mem_ctx
);
928 return NT_STATUS_NO_MEMORY
;
930 } else if (badPwdCount
!= 0) {
931 ret
= samdb_msg_add_int(sam_ctx
, msg_mod
, msg_mod
, "badPwdCount", 0);
932 if (ret
!= LDB_SUCCESS
) {
933 TALLOC_FREE(mem_ctx
);
934 return NT_STATUS_NO_MEMORY
;
938 tv_now
= timeval_current();
939 now
= timeval_to_nttime(&tv_now
);
941 if (interactive_or_kerberos
||
942 (badPwdCount
!= 0 && lockoutTime
== 0)) {
943 ret
= samdb_msg_add_int64(sam_ctx
, msg_mod
, msg_mod
,
945 if (ret
!= LDB_SUCCESS
) {
946 TALLOC_FREE(mem_ctx
);
947 return NT_STATUS_NO_MEMORY
;
951 if (interactive_or_kerberos
) {
954 logonCount
= ldb_msg_find_attr_as_int(msg
, "logonCount", 0);
958 ret
= samdb_msg_add_int(sam_ctx
, msg_mod
, msg_mod
,
959 "logonCount", logonCount
);
960 if (ret
!= LDB_SUCCESS
) {
961 TALLOC_FREE(mem_ctx
);
962 return NT_STATUS_NO_MEMORY
;
965 /* Set an unset logonCount to 0 on first successful login */
966 if (ldb_msg_find_ldb_val(msg
, "logonCount") == NULL
) {
967 ret
= samdb_msg_add_int(sam_ctx
, msg_mod
, msg_mod
,
969 if (ret
!= LDB_SUCCESS
) {
970 TALLOC_FREE(mem_ctx
);
971 return NT_STATUS_NO_MEMORY
;
976 ret
= samdb_rodc(sam_ctx
, &am_rodc
);
977 if (ret
!= LDB_SUCCESS
) {
978 TALLOC_FREE(mem_ctx
);
979 return NT_STATUS_INTERNAL_ERROR
;
983 status
= authsam_update_lastlogon_timestamp(sam_ctx
, msg_mod
, domain_dn
,
984 lastLogonTimestamp
, now
);
985 if (!NT_STATUS_IS_OK(status
)) {
986 TALLOC_FREE(mem_ctx
);
987 return NT_STATUS_NO_MEMORY
;
990 /* Perform the (async) SendToSAM calls for MS-SAMS */
991 if (dbBadPwdCount
!= 0 && send_to_sam
!= NULL
) {
992 struct netr_SendToSamBase
*base_msg
;
993 struct GUID guid
= samdb_result_guid(msg
, "objectGUID");
994 base_msg
= talloc_zero(msg
, struct netr_SendToSamBase
);
996 base_msg
->message_type
= SendToSamResetBadPasswordCount
;
997 base_msg
->message_size
= 16;
998 base_msg
->message
.reset_bad_password
.guid
= guid
;
999 *send_to_sam
= base_msg
;
1003 if (msg_mod
->num_elements
> 0) {
1005 struct ldb_request
*req
;
1007 /* mark all the message elements as LDB_FLAG_MOD_REPLACE */
1008 for (i
=0;i
<msg_mod
->num_elements
;i
++) {
1009 msg_mod
->elements
[i
].flags
= LDB_FLAG_MOD_REPLACE
;
1012 ret
= ldb_build_mod_req(&req
, sam_ctx
, sam_ctx
,
1016 ldb_op_default_callback
,
1018 if (ret
!= LDB_SUCCESS
) {
1022 ret
= ldb_request_add_control(req
,
1023 DSDB_CONTROL_FORCE_RODC_LOCAL_CHANGE
,
1025 if (ret
!= LDB_SUCCESS
) {
1030 ret
= dsdb_autotransaction_request(sam_ctx
, req
);
1035 if (ret
!= LDB_SUCCESS
) {
1036 DEBUG(0, ("Failed to set badPwdCount and lockoutTime "
1037 "to 0 and/or lastlogon to now (%lld) "
1038 "%s: %s\n", (long long int)now
,
1039 ldb_dn_get_linearized(msg_mod
->dn
),
1040 ldb_errstring(sam_ctx
)));
1041 TALLOC_FREE(mem_ctx
);
1042 return NT_STATUS_INTERNAL_ERROR
;
1045 TALLOC_FREE(mem_ctx
);
1046 return NT_STATUS_OK
;