s4-torture: the new krb5 kdc tests are heimdal, not dc specific.
[Samba.git] / source4 / auth / sam.c
blob6e9e63b4d461a92081b198187e0cc7a5e4c99946
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 NTSTATUS sam_get_results_trust(struct ldb_context *sam_ctx,
564 TALLOC_CTX *mem_ctx, const char *domain,
565 const char *realm, const char * const *attrs,
566 struct ldb_message **msg)
568 TALLOC_CTX *frame = talloc_stackframe();
569 int lret;
570 struct ldb_dn *system_dn;
571 char *filter;
572 struct ldb_result *res = NULL;
573 char *domain_encoded;
575 system_dn = ldb_dn_copy(frame, ldb_get_default_basedn(sam_ctx));
576 if (system_dn == NULL) {
577 TALLOC_FREE(frame);
578 return NT_STATUS_NO_MEMORY;
581 if (!ldb_dn_add_child_fmt(system_dn, "CN=System")) {
582 TALLOC_FREE(frame);
583 return NT_STATUS_NO_MEMORY;
586 domain_encoded = ldb_binary_encode_string(mem_ctx, domain);
587 if (!domain_encoded) {
588 TALLOC_FREE(frame);
589 return NT_STATUS_NO_MEMORY;
591 if (realm == NULL) {
592 filter = talloc_asprintf(mem_ctx,
593 "(&(objectClass=trustedDomain)(flatname=%s))",
594 domain_encoded);
595 if (!filter) {
596 TALLOC_FREE(frame);
597 return NT_STATUS_NO_MEMORY;
599 } else {
600 char *realm_encoded = ldb_binary_encode_string(mem_ctx, realm);
601 if (!realm_encoded) {
602 TALLOC_FREE(frame);
603 return NT_STATUS_NO_MEMORY;
606 filter = talloc_asprintf(mem_ctx,
607 "(&(objectClass=trustedDomain)"
608 "(|(trustPartner=%s)(flatname=%s))"
609 ")",
610 realm_encoded, domain_encoded);
611 if (!filter) {
612 TALLOC_FREE(frame);
613 return NT_STATUS_NO_MEMORY;
617 lret = dsdb_search(sam_ctx, frame, &res,
618 system_dn,
619 LDB_SCOPE_ONELEVEL, attrs,
620 DSDB_SEARCH_NO_GLOBAL_CATALOG|DSDB_SEARCH_ONE_ONLY,
621 "%s", filter);
622 if (lret == LDB_ERR_NO_SUCH_OBJECT) {
623 DEBUG(3, ("Failed to find result for %s: %s\n", filter, ldb_errstring(sam_ctx)));
624 TALLOC_FREE(frame);
625 return NT_STATUS_NOT_FOUND;
626 } else if (lret != LDB_SUCCESS) {
627 DEBUG(3, ("Failed to search for %s: %s\n", filter, ldb_errstring(sam_ctx)));
628 TALLOC_FREE(frame);
629 return NT_STATUS_INTERNAL_DB_CORRUPTION;
631 talloc_steal(mem_ctx, res->msgs);
632 *msg = res->msgs[0];
633 TALLOC_FREE(frame);
634 return NT_STATUS_OK;
637 /* Used in the gensec_gssapi and gensec_krb5 server-side code, where the PAC isn't available, and for tokenGroups in the DSDB stack.
639 Supply either a principal or a DN
641 NTSTATUS authsam_get_user_info_dc_principal(TALLOC_CTX *mem_ctx,
642 struct loadparm_context *lp_ctx,
643 struct ldb_context *sam_ctx,
644 const char *principal,
645 struct ldb_dn *user_dn,
646 struct auth_user_info_dc **user_info_dc)
648 NTSTATUS nt_status;
649 DATA_BLOB user_sess_key = data_blob(NULL, 0);
650 DATA_BLOB lm_sess_key = data_blob(NULL, 0);
652 struct ldb_message *msg;
653 struct ldb_dn *domain_dn;
655 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
656 if (!tmp_ctx) {
657 return NT_STATUS_NO_MEMORY;
660 if (principal) {
661 nt_status = sam_get_results_principal(sam_ctx, tmp_ctx, principal,
662 user_attrs, &domain_dn, &msg);
663 if (!NT_STATUS_IS_OK(nt_status)) {
664 talloc_free(tmp_ctx);
665 return nt_status;
667 } else if (user_dn) {
668 struct dom_sid *user_sid, *domain_sid;
669 int ret;
670 /* pull the user attributes */
671 ret = dsdb_search_one(sam_ctx, tmp_ctx, &msg, user_dn,
672 LDB_SCOPE_BASE, user_attrs,
673 DSDB_SEARCH_SHOW_EXTENDED_DN | DSDB_SEARCH_NO_GLOBAL_CATALOG,
674 "(objectClass=*)");
675 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
676 talloc_free(tmp_ctx);
677 return NT_STATUS_NO_SUCH_USER;
678 } else if (ret != LDB_SUCCESS) {
679 talloc_free(tmp_ctx);
680 return NT_STATUS_INTERNAL_DB_CORRUPTION;
683 user_sid = samdb_result_dom_sid(msg, msg, "objectSid");
685 nt_status = dom_sid_split_rid(tmp_ctx, user_sid, &domain_sid, NULL);
686 if (!NT_STATUS_IS_OK(nt_status)) {
687 return nt_status;
690 domain_dn = samdb_search_dn(sam_ctx, mem_ctx, NULL,
691 "(&(objectSid=%s)(objectClass=domain))",
692 ldap_encode_ndr_dom_sid(tmp_ctx, domain_sid));
693 if (!domain_dn) {
694 DEBUG(3, ("authsam_get_user_info_dc_principal: Failed to find domain with: SID %s\n",
695 dom_sid_string(tmp_ctx, domain_sid)));
696 return NT_STATUS_NO_SUCH_USER;
699 } else {
700 return NT_STATUS_INVALID_PARAMETER;
703 nt_status = authsam_make_user_info_dc(tmp_ctx, sam_ctx,
704 lpcfg_netbios_name(lp_ctx),
705 lpcfg_workgroup(lp_ctx),
706 domain_dn,
707 msg,
708 user_sess_key, lm_sess_key,
709 user_info_dc);
710 if (!NT_STATUS_IS_OK(nt_status)) {
711 talloc_free(tmp_ctx);
712 return nt_status;
715 talloc_steal(mem_ctx, *user_info_dc);
716 talloc_free(tmp_ctx);
718 return NT_STATUS_OK;
721 NTSTATUS authsam_update_bad_pwd_count(struct ldb_context *sam_ctx,
722 struct ldb_message *msg,
723 struct ldb_dn *domain_dn)
725 const char *attrs[] = { "lockoutThreshold",
726 "lockOutObservationWindow",
727 "lockoutDuration",
728 "pwdProperties",
729 NULL };
730 int ret;
731 NTSTATUS status;
732 struct ldb_result *domain_res;
733 struct ldb_message *msg_mod = NULL;
734 TALLOC_CTX *mem_ctx;
736 mem_ctx = talloc_new(msg);
737 if (mem_ctx == NULL) {
738 return NT_STATUS_NO_MEMORY;
741 ret = dsdb_search_dn(sam_ctx, mem_ctx, &domain_res, domain_dn, attrs, 0);
742 if (ret != LDB_SUCCESS) {
743 TALLOC_FREE(mem_ctx);
744 return NT_STATUS_INTERNAL_DB_CORRUPTION;
747 status = dsdb_update_bad_pwd_count(mem_ctx, sam_ctx,
748 msg, domain_res->msgs[0], &msg_mod);
749 if (!NT_STATUS_IS_OK(status)) {
750 TALLOC_FREE(mem_ctx);
751 return status;
754 if (msg_mod != NULL) {
755 ret = dsdb_modify(sam_ctx, msg_mod, 0);
756 if (ret != LDB_SUCCESS) {
757 DEBUG(0, ("Failed to update badPwdCount, badPasswordTime or set lockoutTime on %s: %s\n",
758 ldb_dn_get_linearized(msg_mod->dn), ldb_errstring(sam_ctx)));
759 TALLOC_FREE(mem_ctx);
760 return NT_STATUS_INTERNAL_ERROR;
764 TALLOC_FREE(mem_ctx);
765 return NT_STATUS_OK;
768 NTSTATUS authsam_zero_bad_pwd_count(struct ldb_context *sam_ctx,
769 const struct ldb_message *msg)
771 int ret;
772 int badPwdCount;
773 int64_t lockoutTime;
774 struct ldb_message *msg_mod;
775 TALLOC_CTX *mem_ctx;
777 lockoutTime = ldb_msg_find_attr_as_int64(msg, "lockoutTime", 0);
778 badPwdCount = ldb_msg_find_attr_as_int(msg, "badPwdCount", 0);
779 if (lockoutTime == 0 && badPwdCount == 0) {
780 return NT_STATUS_OK;
783 mem_ctx = talloc_new(msg);
784 if (mem_ctx == NULL) {
785 return NT_STATUS_NO_MEMORY;
787 msg_mod = ldb_msg_new(mem_ctx);
788 if (msg_mod == NULL) {
789 TALLOC_FREE(mem_ctx);
790 return NT_STATUS_NO_MEMORY;
792 msg_mod->dn = msg->dn;
794 if (lockoutTime != 0) {
796 * This implies "badPwdCount" = 0, see samldb_lockout_time()
798 ret = samdb_msg_add_int(sam_ctx, msg_mod, msg_mod, "lockoutTime", 0);
799 if (ret != LDB_SUCCESS) {
800 TALLOC_FREE(mem_ctx);
801 return NT_STATUS_NO_MEMORY;
803 } else {
804 ret = samdb_msg_add_int(sam_ctx, msg_mod, msg_mod, "badPwdCount", 0);
805 if (ret != LDB_SUCCESS) {
806 TALLOC_FREE(mem_ctx);
807 return NT_STATUS_NO_MEMORY;
811 ret = dsdb_replace(sam_ctx, msg_mod, 0);
812 if (ret != LDB_SUCCESS) {
813 DEBUG(0, ("Failed to set badPwdCount and lockoutTime to 0 on %s: %s\n",
814 ldb_dn_get_linearized(msg_mod->dn), ldb_errstring(sam_ctx)));
815 TALLOC_FREE(mem_ctx);
816 return NT_STATUS_INTERNAL_ERROR;
819 TALLOC_FREE(mem_ctx);
820 return NT_STATUS_OK;