auth_winbind: Allow badPwdCount to be set to 0 with this auth method
[sfrench/samba-autobuild/.git] / source4 / auth / sam.c
index 7d2f249b064b71f7da7639c334a2fd351bdb7a53..9119ef54f439b11cbdb94c07eb9aa9e18527da2a 100644 (file)
@@ -1,7 +1,7 @@
 /* 
    Unix SMB/CIFS implementation.
    Password and authentication handling
-   Copyright (C) Andrew Bartlett <abartlet@samba.org> 2001-2004
+   Copyright (C) Andrew Bartlett <abartlet@samba.org> 2001-2010
    Copyright (C) Gerald Carter                             2003
    Copyright (C) Stefan Metzmacher                         2005
    Copyright (C) Matthias Dieter Wallnöfer                 2009
 #include "system/time.h"
 #include "auth/auth.h"
 #include <ldb.h>
-#include "../lib/util/util_ldb.h"
 #include "dsdb/samdb/samdb.h"
 #include "libcli/security/security.h"
-#include "libcli/ldap/ldap.h"
-#include "../libcli/ldap/ldap_ndr.h"
-#include "librpc/gen_ndr/ndr_netlogon.h"
-#include "librpc/gen_ndr/ndr_security.h"
-#include "param/param.h"
 #include "auth/auth_sam.h"
 #include "dsdb/common/util.h"
+#include "libcli/ldap/ldap_ndr.h"
+#include "param/param.h"
 
 #define KRBTGT_ATTRS \
        /* required for the krb5 kdc */         \
        "userPrincipalName",                    \
        "servicePrincipalName",                 \
        "msDS-KeyVersionNumber",                \
+       "msDS-SecondaryKrbTgtNumber",           \
+       "msDS-SupportedEncryptionTypes",        \
        "supplementalCredentials",              \
+       "msDS-AllowedToDelegateTo",             \
                                                \
        /* passwords */                         \
        "dBCSPwd",                              \
        "unicodePwd",                           \
                                                \
-       "userAccountControl",                   \
+       "userAccountControl",                   \
+       "msDS-User-Account-Control-Computed",   \
        "objectSid",                            \
                                                \
        "pwdLastSet",                           \
-       "accountExpires"                        
+       "msDS-UserPasswordExpiryTimeComputed",  \
+       "accountExpires"
 
 const char *krbtgt_attrs[] = {
-       KRBTGT_ATTRS
+       KRBTGT_ATTRS, NULL
 };
 
 const char *server_attrs[] = {
-       KRBTGT_ATTRS
+       KRBTGT_ATTRS, NULL
 };
 
 const char *user_attrs[] = {
@@ -67,22 +68,32 @@ const char *user_attrs[] = {
 
        "logonHours",
 
+       /*
+        * To allow us to zero the badPwdCount and lockoutTime on
+        * successful logon, without database churn
+        */
+       "lockoutTime",
+
        /* check 'allowed workstations' */
        "userWorkstations",
                       
-       /* required for server_info, not access control: */
+       /* required for user_info_dc, not access control: */
        "displayName",
        "scriptPath",
        "profilePath",
        "homeDirectory",
        "homeDrive",
        "lastLogon",
+       "lastLogonTimestamp",
        "lastLogoff",
        "accountExpires",
        "badPwdCount",
        "logonCount",
        "primaryGroupID",
        "memberOf",
+       "badPasswordTime",
+       "lmPwdHistory",
+       "ntPwdHistory",
        NULL,
 };
 
@@ -166,37 +177,37 @@ _PUBLIC_ NTSTATUS authsam_account_ok(TALLOC_CTX *mem_ctx,
        const char *workstation_list;
        NTTIME acct_expiry;
        NTTIME must_change_time;
+       struct timeval tv_now = timeval_current();
+       NTTIME now = timeval_to_nttime(&tv_now);
 
-       NTTIME now;
        DEBUG(4,("authsam_account_ok: Checking SMB password for user %s\n", name_for_logs));
 
-       acct_flags = samdb_result_acct_flags(sam_ctx, mem_ctx, msg, domain_dn);
+       acct_flags = samdb_result_acct_flags(msg, "msDS-User-Account-Control-Computed");
        
        acct_expiry = samdb_result_account_expires(msg);
 
        /* Check for when we must change this password, taking the
         * userAccountControl flags into account */
-       must_change_time = samdb_result_force_password_change(sam_ctx, mem_ctx, 
-                                                             domain_dn, msg);
+       must_change_time = samdb_result_nttime(msg,
+                       "msDS-UserPasswordExpiryTimeComputed", 0);
 
-       workstation_list = samdb_result_string(msg, "userWorkstations", NULL);
+       workstation_list = ldb_msg_find_attr_as_string(msg, "userWorkstations", NULL);
 
        /* Quit if the account was disabled. */
        if (acct_flags & ACB_DISABLED) {
-               DEBUG(1,("authsam_account_ok: Account for user '%s' was disabled.\n", name_for_logs));
+               DEBUG(2,("authsam_account_ok: Account for user '%s' was disabled.\n", name_for_logs));
                return NT_STATUS_ACCOUNT_DISABLED;
        }
 
        /* Quit if the account was locked out. */
        if (acct_flags & ACB_AUTOLOCK) {
-               DEBUG(1,("authsam_account_ok: Account for user %s was locked out.\n", name_for_logs));
+               DEBUG(2,("authsam_account_ok: Account for user %s was locked out.\n", name_for_logs));
                return NT_STATUS_ACCOUNT_LOCKED_OUT;
        }
 
        /* Test account expire time */
-       unix_to_nt_time(&now, time(NULL));
        if (now > acct_expiry) {
-               DEBUG(1,("authsam_account_ok: Account for user '%s' has expired.\n", name_for_logs));
+               DEBUG(2,("authsam_account_ok: Account for user '%s' has expired.\n", name_for_logs));
                DEBUG(3,("authsam_account_ok: Account expired at '%s'.\n", 
                         nt_time_string(mem_ctx, acct_expiry)));
                return NT_STATUS_ACCOUNT_EXPIRED;
@@ -204,16 +215,16 @@ _PUBLIC_ NTSTATUS authsam_account_ok(TALLOC_CTX *mem_ctx,
 
        /* check for immediate expiry "must change at next logon" (but not if this is a password change request) */
        if ((must_change_time == 0) && !password_change) {
-               DEBUG(1,("sam_account_ok: Account for user '%s' password must change!.\n", 
+               DEBUG(2,("sam_account_ok: Account for user '%s' password must change!.\n",
                         name_for_logs));
                return NT_STATUS_PASSWORD_MUST_CHANGE;
        }
 
        /* check for expired password (but not if this is a password change request) */
        if ((must_change_time < now) && !password_change) {
-               DEBUG(1,("sam_account_ok: Account for user '%s' password expired!.\n", 
+               DEBUG(2,("sam_account_ok: Account for user '%s' password expired!.\n",
                         name_for_logs));
-               DEBUG(1,("sam_account_ok: Password expired at '%s' unix time.\n", 
+               DEBUG(2,("sam_account_ok: Password expired at '%s' unix time.\n",
                         nt_time_string(mem_ctx, must_change_time)));
                return NT_STATUS_PASSWORD_EXPIRED;
        }
@@ -222,8 +233,8 @@ _PUBLIC_ NTSTATUS authsam_account_ok(TALLOC_CTX *mem_ctx,
        if (logon_workstation && workstation_list && *workstation_list) {
                bool invalid_ws = true;
                int i;
-               const char **workstations = (const char **)str_list_make(mem_ctx, workstation_list, ",");
-               
+               char **workstations = str_list_make(mem_ctx, workstation_list, ",");
+
                for (i = 0; workstations && workstations[i]; i++) {
                        DEBUG(10,("sam_account_ok: checking for workstation match '%s' and '%s'\n",
                                  workstations[i], logon_workstation));
@@ -269,259 +280,267 @@ _PUBLIC_ NTSTATUS authsam_account_ok(TALLOC_CTX *mem_ctx,
        return NT_STATUS_OK;
 }
 
-/* This function tests if a SID structure "sids" contains the SID "sid" */
-static bool sids_contains_sid(const struct dom_sid **sids, const int num_sids,
-       const struct dom_sid *sid)
-{
-       int i;
-
-       for (i = 0; i < num_sids; i++) {
-               if (dom_sid_equal(sids[i], sid))
-                       return true;
-       }
-       return false;
-}
-
-/*
- * This function generates the transitive closure of a given SID "sid" (it
- * basically expands nested groups of a SID).
- * If the SID isn't located in the "res_sids" structure yet and the
- * "only_childs" flag is negative, we add it to "res_sids".
- * Then we've always to consider the "memberOf" attributes. We invoke the
- * function recursively on each item of it with the "only_childs" flag set to
- * "false".
- * The "only_childs" flag is particularly useful if you have a user SID and
- * want to include all his groups (referenced with "memberOf") without his SID
- * itself.
- *
- * At the beginning "res_sids" should reference to a NULL pointer.
- */
-static NTSTATUS authsam_expand_nested_groups(struct ldb_context *sam_ctx,
-       const struct dom_sid *sid, const bool only_childs,
-       TALLOC_CTX *res_sids_ctx, struct dom_sid ***res_sids,
-       int *num_res_sids)
+_PUBLIC_ NTSTATUS authsam_make_user_info_dc(TALLOC_CTX *mem_ctx,
+                                          struct ldb_context *sam_ctx,
+                                          const char *netbios_name,
+                                          const char *domain_name,
+                                          const char *dns_domain_name,
+                                          struct ldb_dn *domain_dn, 
+                                          struct ldb_message *msg,
+                                          DATA_BLOB user_sess_key,
+                                          DATA_BLOB lm_sess_key,
+                                          struct auth_user_info_dc **_user_info_dc)
 {
-       const char * const attrs[] = { "memberOf", NULL };
-       int i, ret;
-       bool already_there;
-       struct ldb_dn *tmp_dn;
-       struct dom_sid *tmp_sid;
-       TALLOC_CTX *tmp_ctx;
-       struct ldb_message **res;
        NTSTATUS status;
+       struct auth_user_info_dc *user_info_dc;
+       struct auth_user_info *info;
+       const char *str, *filter;
+       /* SIDs for the account and his primary group */
+       struct dom_sid *account_sid;
+       const char *primary_group_string;
+       const char *primary_group_dn;
+       DATA_BLOB primary_group_blob;
+       /* SID structures for the expanded group memberships */
+       struct dom_sid *sids = NULL;
+       unsigned int num_sids = 0, i;
+       struct dom_sid *domain_sid;
+       TALLOC_CTX *tmp_ctx;
+       struct ldb_message_element *el;
+
+       user_info_dc = talloc(mem_ctx, struct auth_user_info_dc);
+       NT_STATUS_HAVE_NO_MEMORY(user_info_dc);
 
-       if (*res_sids == NULL) {
-               *num_res_sids = 0;
+       tmp_ctx = talloc_new(user_info_dc);
+       if (user_info_dc == NULL) {
+               TALLOC_FREE(user_info_dc);
+               return NT_STATUS_NO_MEMORY;
        }
 
-       if (sid == NULL) {
-               return NT_STATUS_OK;
+       sids = talloc_array(user_info_dc, struct dom_sid, 2);
+       if (sids == NULL) {
+               TALLOC_FREE(user_info_dc);
+               return NT_STATUS_NO_MEMORY;
        }
 
-       already_there = sids_contains_sid((const struct dom_sid**) *res_sids,
-               *num_res_sids, sid);
-       if (already_there) {
-               return NT_STATUS_OK;
+       num_sids = 2;
+
+       account_sid = samdb_result_dom_sid(user_info_dc, msg, "objectSid");
+       if (account_sid == NULL) {
+               TALLOC_FREE(user_info_dc);
+               return NT_STATUS_NO_MEMORY;
        }
 
-       if (!only_childs) {
-               tmp_sid = dom_sid_dup(res_sids_ctx, sid);
-               NT_STATUS_HAVE_NO_MEMORY(tmp_sid);
-               *res_sids = talloc_realloc(res_sids_ctx, *res_sids,
-                       struct dom_sid *, *num_res_sids + 1);
-               NT_STATUS_HAVE_NO_MEMORY(*res_sids);
-               (*res_sids)[*num_res_sids] = tmp_sid;
-               ++(*num_res_sids);
+       status = dom_sid_split_rid(tmp_ctx, account_sid, &domain_sid, NULL);
+       if (!NT_STATUS_IS_OK(status)) {
+               talloc_free(user_info_dc);
+               return status;
        }
 
-       tmp_ctx = talloc_new(sam_ctx);
+       sids[PRIMARY_USER_SID_INDEX] = *account_sid;
+       sids[PRIMARY_GROUP_SID_INDEX] = *domain_sid;
+       sid_append_rid(&sids[PRIMARY_GROUP_SID_INDEX], ldb_msg_find_attr_as_uint(msg, "primaryGroupID", ~0));
 
-       ret = gendb_search(sam_ctx, tmp_ctx, NULL, &res, attrs,
-               "objectSid=%s", ldap_encode_ndr_dom_sid(tmp_ctx, sid));
-       if (ret != 1) {
-               talloc_free(tmp_ctx);
-               return NT_STATUS_INTERNAL_DB_CORRUPTION;
+       /* Filter out builtin groups from this token.  We will search
+        * for builtin groups later, and not include them in the PAC
+        * on SamLogon validation info */
+       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);
+       if (filter == NULL) {
+               TALLOC_FREE(user_info_dc);
+               return NT_STATUS_NO_MEMORY;
        }
 
-       if (res[0]->num_elements == 0) {
-               talloc_free(res);
-               talloc_free(tmp_ctx);
-               return NT_STATUS_OK;
+       primary_group_string = dom_sid_string(tmp_ctx, &sids[PRIMARY_GROUP_SID_INDEX]);
+       if (primary_group_string == NULL) {
+               TALLOC_FREE(user_info_dc);
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       primary_group_dn = talloc_asprintf(tmp_ctx, "<SID=%s>", primary_group_string);
+       if (primary_group_dn == NULL) {
+               TALLOC_FREE(user_info_dc);
+               return NT_STATUS_NO_MEMORY;
        }
 
-       for (i = 0; i < res[0]->elements[0].num_values; i++) {
-               tmp_dn = ldb_dn_from_ldb_val(tmp_ctx, sam_ctx,
-                       &res[0]->elements[0].values[i]);
-               tmp_sid = samdb_search_dom_sid(sam_ctx, tmp_ctx, tmp_dn,
-                       "objectSid", NULL);
+       primary_group_blob = data_blob_string_const(primary_group_dn);
+
+       /* Expands the primary group - this function takes in
+        * memberOf-like values, so we fake one up with the
+        * <SID=S-...> format of DN and then let it expand
+        * them, as long as they meet the filter - so only
+        * domain groups, not builtin groups
+        *
+        * The primary group is still treated specially, so we set the
+        * 'only childs' flag to true
+        */
+       status = dsdb_expand_nested_groups(sam_ctx, &primary_group_blob, true, filter,
+                                          user_info_dc, &sids, &num_sids);
+       if (!NT_STATUS_IS_OK(status)) {
+               talloc_free(user_info_dc);
+               return status;
+       }
 
-               status = authsam_expand_nested_groups(sam_ctx, tmp_sid,
-                       false, res_sids_ctx, res_sids, num_res_sids);
+       /* Expands the additional groups */
+       el = ldb_msg_find_element(msg, "memberOf");
+       for (i = 0; el && i < el->num_values; i++) {
+               /* This function takes in memberOf values and expands
+                * them, as long as they meet the filter - so only
+                * domain groups, not builtin groups */
+               status = dsdb_expand_nested_groups(sam_ctx, &el->values[i], false, filter,
+                                                  user_info_dc, &sids, &num_sids);
                if (!NT_STATUS_IS_OK(status)) {
-                       talloc_free(res);
-                       talloc_free(tmp_ctx);
+                       talloc_free(user_info_dc);
                        return status;
                }
        }
 
-       talloc_free(res);
-       talloc_free(tmp_ctx);
+       user_info_dc->sids = sids;
+       user_info_dc->num_sids = num_sids;
 
-       return NT_STATUS_OK;
-}
+       user_info_dc->info = info = talloc_zero(user_info_dc, struct auth_user_info);
+       NT_STATUS_HAVE_NO_MEMORY(user_info_dc->info);
 
-_PUBLIC_ NTSTATUS authsam_make_server_info(TALLOC_CTX *mem_ctx,
-                                          struct ldb_context *sam_ctx,
-                                          const char *netbios_name,
-                                          const char *domain_name,
-                                          struct ldb_dn *domain_dn, 
-                                          struct ldb_message *msg,
-                                          DATA_BLOB user_sess_key,
-                                          DATA_BLOB lm_sess_key,
-                                          struct auth_serversupplied_info
-                                                  **_server_info)
-{
-       NTSTATUS status;
-       struct auth_serversupplied_info *server_info;
-       const char *str;
-       struct dom_sid *tmp_sid;
-       /* SIDs for the account and his primary group */
-       struct dom_sid *account_sid;
-       struct dom_sid *primary_group_sid;
-       /* SID structures for the expanded group memberships */
-       struct dom_sid **groupSIDs = NULL, **groupSIDs_2 = NULL;
-       int num_groupSIDs = 0, num_groupSIDs_2 = 0, i;
-       uint32_t userAccountControl;
+       info->account_name = talloc_steal(info,
+               ldb_msg_find_attr_as_string(msg, "sAMAccountName", NULL));
 
-       server_info = talloc(mem_ctx, struct auth_serversupplied_info);
-       NT_STATUS_HAVE_NO_MEMORY(server_info);
+       info->user_principal_name = talloc_steal(info,
+               ldb_msg_find_attr_as_string(msg, "userPrincipalName", NULL));
+       if (info->user_principal_name == NULL && dns_domain_name != NULL) {
+               info->user_principal_name = talloc_asprintf(info, "%s@%s",
+                                       info->account_name,
+                                       dns_domain_name);
+               if (info->user_principal_name == NULL) {
+                       TALLOC_FREE(user_info_dc);
+                       return NT_STATUS_NO_MEMORY;
+               }
+               info->user_principal_constructed = true;
+       }
 
-       account_sid = samdb_result_dom_sid(server_info, msg, "objectSid");
-       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(account_sid, server_info);
+       info->domain_name = talloc_strdup(info, domain_name);
+       if (info->domain_name == NULL) {
+               TALLOC_FREE(user_info_dc);
+               return NT_STATUS_NO_MEMORY;
+       }
 
-       primary_group_sid = dom_sid_add_rid(server_info,
-               samdb_domain_sid(sam_ctx),
-               samdb_result_uint(msg, "primaryGroupID", ~0));
-       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(primary_group_sid, server_info);
+       if (dns_domain_name != NULL) {
+               info->dns_domain_name = talloc_strdup(info, dns_domain_name);
+               if (info->dns_domain_name == NULL) {
+                       TALLOC_FREE(user_info_dc);
+                       return NT_STATUS_NO_MEMORY;
+               }
+       }
 
-       /* Expands the primary group */
-       status = authsam_expand_nested_groups(sam_ctx, primary_group_sid, false,
-                                             server_info, &groupSIDs, &num_groupSIDs);
-       if (!NT_STATUS_IS_OK(status)) {
-               talloc_free(server_info);
-               return status;
+       str = ldb_msg_find_attr_as_string(msg, "displayName", "");
+       info->full_name = talloc_strdup(info, str);
+       if (info->full_name == NULL) {
+               TALLOC_FREE(user_info_dc);
+               return NT_STATUS_NO_MEMORY;
        }
 
-       /* Expands the additional groups */
-       status = authsam_expand_nested_groups(sam_ctx, account_sid, true,
-               server_info, &groupSIDs_2, &num_groupSIDs_2);
-       if (!NT_STATUS_IS_OK(status)) {
-               talloc_free(server_info);
-               return status;
+       str = ldb_msg_find_attr_as_string(msg, "scriptPath", "");
+       info->logon_script = talloc_strdup(info, str);
+       if (info->logon_script == NULL) {
+               TALLOC_FREE(user_info_dc);
+               return NT_STATUS_NO_MEMORY;
        }
 
-       /* Merge the two expanded structures (groupSIDs, groupSIDs_2) */
-       for (i = 0; i < num_groupSIDs_2; i++)
-               if (!sids_contains_sid((const struct dom_sid **) groupSIDs,
-                               num_groupSIDs, groupSIDs_2[i])) {
-                       tmp_sid = dom_sid_dup(server_info, groupSIDs_2[i]);
-                       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(tmp_sid, server_info);
-                       groupSIDs = talloc_realloc(server_info, groupSIDs,
-                               struct dom_sid *, num_groupSIDs + 1);
-                       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(groupSIDs,
-                               server_info);
-                       groupSIDs[num_groupSIDs] = tmp_sid;
-                       ++num_groupSIDs;
-               }
-       talloc_free(groupSIDs_2);
+       str = ldb_msg_find_attr_as_string(msg, "profilePath", "");
+       info->profile_path = talloc_strdup(info, str);
+       if (info->profile_path == NULL) {
+               TALLOC_FREE(user_info_dc);
+               return NT_STATUS_NO_MEMORY;
+       }
 
-       server_info->account_sid = account_sid;
-       server_info->primary_group_sid = primary_group_sid;
-       
-       /* DCs also get SID_NT_ENTERPRISE_DCS */
-       userAccountControl = ldb_msg_find_attr_as_uint(msg, "userAccountControl", 0);
-       if (userAccountControl & UF_SERVER_TRUST_ACCOUNT) {
-               groupSIDs = talloc_realloc(server_info, groupSIDs, struct dom_sid *,
-                                          num_groupSIDs+1);
-               NT_STATUS_HAVE_NO_MEMORY_AND_FREE(groupSIDs, server_info);
-               groupSIDs[num_groupSIDs] = dom_sid_parse_talloc(groupSIDs, SID_NT_ENTERPRISE_DCS);
-               NT_STATUS_HAVE_NO_MEMORY_AND_FREE(groupSIDs[num_groupSIDs], server_info);
-               num_groupSIDs++;
-       }
-
-       server_info->domain_groups = groupSIDs;
-       server_info->n_domain_groups = num_groupSIDs;
-
-       server_info->account_name = talloc_steal(server_info,
-               samdb_result_string(msg, "sAMAccountName", NULL));
-
-       server_info->domain_name = talloc_strdup(server_info, domain_name);
-       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(server_info->domain_name,
-               server_info);
-
-       str = samdb_result_string(msg, "displayName", "");
-       server_info->full_name = talloc_strdup(server_info, str);
-       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(server_info->full_name, server_info);
-
-       str = samdb_result_string(msg, "scriptPath", "");
-       server_info->logon_script = talloc_strdup(server_info, str);
-       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(server_info->logon_script,
-               server_info);
-
-       str = samdb_result_string(msg, "profilePath", "");
-       server_info->profile_path = talloc_strdup(server_info, str);
-       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(server_info->profile_path,
-               server_info);
-
-       str = samdb_result_string(msg, "homeDirectory", "");
-       server_info->home_directory = talloc_strdup(server_info, str);
-       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(server_info->home_directory,
-               server_info);
-
-       str = samdb_result_string(msg, "homeDrive", "");
-       server_info->home_drive = talloc_strdup(server_info, str);
-       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(server_info->home_drive, server_info);
-
-       server_info->logon_server = talloc_strdup(server_info, netbios_name);
-       NT_STATUS_HAVE_NO_MEMORY_AND_FREE(server_info->logon_server,
-               server_info);
-
-       server_info->last_logon = samdb_result_nttime(msg, "lastLogon", 0);
-       server_info->last_logoff = samdb_result_last_logoff(msg);
-       server_info->acct_expiry = samdb_result_account_expires(msg);
-       server_info->last_password_change = samdb_result_nttime(msg,
+       str = ldb_msg_find_attr_as_string(msg, "homeDirectory", "");
+       info->home_directory = talloc_strdup(info, str);
+       if (info->home_directory == NULL) {
+               TALLOC_FREE(user_info_dc);
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       str = ldb_msg_find_attr_as_string(msg, "homeDrive", "");
+       info->home_drive = talloc_strdup(info, str);
+       if (info->home_drive == NULL) {
+               TALLOC_FREE(user_info_dc);
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       info->logon_server = talloc_strdup(info, netbios_name);
+       if (info->logon_server == NULL) {
+               TALLOC_FREE(user_info_dc);
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       info->last_logon = samdb_result_nttime(msg, "lastLogon", 0);
+       info->last_logoff = samdb_result_last_logoff(msg);
+       info->acct_expiry = samdb_result_account_expires(msg);
+       info->last_password_change = samdb_result_nttime(msg,
                "pwdLastSet", 0);
-       server_info->allow_password_change
+       info->allow_password_change
                = samdb_result_allow_password_change(sam_ctx, mem_ctx, 
                        domain_dn, msg, "pwdLastSet");
-       server_info->force_password_change
-               = samdb_result_force_password_change(sam_ctx, mem_ctx,
-                       domain_dn, msg);
-       server_info->logon_count = samdb_result_uint(msg, "logonCount", 0);
-       server_info->bad_password_count = samdb_result_uint(msg, "badPwdCount",
+       info->force_password_change = samdb_result_nttime(msg,
+               "msDS-UserPasswordExpiryTimeComputed", 0);
+       info->logon_count = ldb_msg_find_attr_as_uint(msg, "logonCount", 0);
+       info->bad_password_count = ldb_msg_find_attr_as_uint(msg, "badPwdCount",
                0);
 
-       server_info->acct_flags = samdb_result_acct_flags(sam_ctx, mem_ctx, 
-                                                         msg, domain_dn);
+       info->acct_flags = samdb_result_acct_flags(msg, "msDS-User-Account-Control-Computed");
 
-       server_info->user_session_key = data_blob_talloc(server_info,
+       user_info_dc->user_session_key = data_blob_talloc(user_info_dc,
                                                         user_sess_key.data,
                                                         user_sess_key.length);
        if (user_sess_key.data) {
-               NT_STATUS_HAVE_NO_MEMORY_AND_FREE(server_info->user_session_key.data,
-                                                 server_info);
+               if (user_info_dc->user_session_key.data == NULL) {
+                       TALLOC_FREE(user_info_dc);
+                       return NT_STATUS_NO_MEMORY;
+               }
        }
-       server_info->lm_session_key = data_blob_talloc(server_info,
+       user_info_dc->lm_session_key = data_blob_talloc(user_info_dc,
                                                       lm_sess_key.data,
                                                       lm_sess_key.length);
        if (lm_sess_key.data) {
-               NT_STATUS_HAVE_NO_MEMORY_AND_FREE(server_info->lm_session_key.data,
-                                                 server_info);
+               if (user_info_dc->lm_session_key.data == NULL) {
+                       TALLOC_FREE(user_info_dc);
+                       return NT_STATUS_NO_MEMORY;
+               }
+       }
+
+       if (info->acct_flags & ACB_SVRTRUST) {
+               /* the SID_NT_ENTERPRISE_DCS SID gets added into the
+                  PAC */
+               user_info_dc->sids = talloc_realloc(user_info_dc,
+                                                  user_info_dc->sids,
+                                                  struct dom_sid,
+                                                  user_info_dc->num_sids+1);
+               if (user_info_dc->sids == NULL) {
+                       TALLOC_FREE(user_info_dc);
+                       return NT_STATUS_NO_MEMORY;
+               }
+               user_info_dc->sids[user_info_dc->num_sids] = global_sid_Enterprise_DCs;
+               user_info_dc->num_sids++;
+       }
+
+       if ((info->acct_flags & (ACB_PARTIAL_SECRETS_ACCOUNT | ACB_WSTRUST)) ==
+           (ACB_PARTIAL_SECRETS_ACCOUNT | ACB_WSTRUST)) {
+               /* the DOMAIN_RID_ENTERPRISE_READONLY_DCS PAC */
+               user_info_dc->sids = talloc_realloc(user_info_dc,
+                                                  user_info_dc->sids,
+                                                  struct dom_sid,
+                                                  user_info_dc->num_sids+1);
+               if (user_info_dc->sids == NULL) {
+                       TALLOC_FREE(user_info_dc);
+                       return NT_STATUS_NO_MEMORY;
+               }
+               user_info_dc->sids[user_info_dc->num_sids] = *domain_sid;
+               sid_append_rid(&user_info_dc->sids[user_info_dc->num_sids],
+                           DOMAIN_RID_ENTERPRISE_READONLY_DCS);
+               user_info_dc->num_sids++;
        }
 
-       server_info->authenticated = true;
+       info->authenticated = true;
 
-       *_server_info = server_info;
+       talloc_free(tmp_ctx);
+       *_user_info_dc = user_info_dc;
 
        return NT_STATUS_OK;
 }
@@ -550,7 +569,9 @@ NTSTATUS sam_get_results_principal(struct ldb_context *sam_ctx,
        
        /* pull the user attributes */
        ret = dsdb_search_one(sam_ctx, tmp_ctx, msg, user_dn,
-                             LDB_SCOPE_BASE, attrs, DSDB_SEARCH_SHOW_EXTENDED_DN, "(objectClass=*)");
+                             LDB_SCOPE_BASE, attrs,
+                             DSDB_SEARCH_SHOW_EXTENDED_DN | DSDB_SEARCH_NO_GLOBAL_CATALOG,
+                             "(objectClass=*)");
        if (ret != LDB_SUCCESS) {
                talloc_free(tmp_ctx);
                return NT_STATUS_INTERNAL_DB_CORRUPTION;
@@ -561,3 +582,446 @@ NTSTATUS sam_get_results_principal(struct ldb_context *sam_ctx,
        
        return NT_STATUS_OK;
 }
+
+/* Used in the gensec_gssapi and gensec_krb5 server-side code, where the PAC isn't available, and for tokenGroups in the DSDB stack.
+
+ Supply either a principal or a DN
+*/
+NTSTATUS authsam_get_user_info_dc_principal(TALLOC_CTX *mem_ctx,
+                                          struct loadparm_context *lp_ctx,
+                                          struct ldb_context *sam_ctx,
+                                          const char *principal,
+                                          struct ldb_dn *user_dn,
+                                          struct auth_user_info_dc **user_info_dc)
+{
+       NTSTATUS nt_status;
+       DATA_BLOB user_sess_key = data_blob(NULL, 0);
+       DATA_BLOB lm_sess_key = data_blob(NULL, 0);
+
+       struct ldb_message *msg;
+       struct ldb_dn *domain_dn;
+
+       TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
+       if (!tmp_ctx) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       if (principal) {
+               nt_status = sam_get_results_principal(sam_ctx, tmp_ctx, principal,
+                                                     user_attrs, &domain_dn, &msg);
+               if (!NT_STATUS_IS_OK(nt_status)) {
+                       talloc_free(tmp_ctx);
+                       return nt_status;
+               }
+       } else if (user_dn) {
+               struct dom_sid *user_sid, *domain_sid;
+               int ret;
+               /* pull the user attributes */
+               ret = dsdb_search_one(sam_ctx, tmp_ctx, &msg, user_dn,
+                                     LDB_SCOPE_BASE, user_attrs,
+                                     DSDB_SEARCH_SHOW_EXTENDED_DN | DSDB_SEARCH_NO_GLOBAL_CATALOG,
+                                     "(objectClass=*)");
+               if (ret == LDB_ERR_NO_SUCH_OBJECT) {
+                       talloc_free(tmp_ctx);
+                       return NT_STATUS_NO_SUCH_USER;
+               } else if (ret != LDB_SUCCESS) {
+                       talloc_free(tmp_ctx);
+                       return NT_STATUS_INTERNAL_DB_CORRUPTION;
+               }
+
+               user_sid = samdb_result_dom_sid(msg, msg, "objectSid");
+
+               nt_status = dom_sid_split_rid(tmp_ctx, user_sid, &domain_sid, NULL);
+               if (!NT_STATUS_IS_OK(nt_status)) {
+                       return nt_status;
+               }
+
+               domain_dn = samdb_search_dn(sam_ctx, mem_ctx, NULL,
+                                         "(&(objectSid=%s)(objectClass=domain))",
+                                           ldap_encode_ndr_dom_sid(tmp_ctx, domain_sid));
+               if (!domain_dn) {
+                       DEBUG(3, ("authsam_get_user_info_dc_principal: Failed to find domain with: SID %s\n",
+                                 dom_sid_string(tmp_ctx, domain_sid)));
+                       return NT_STATUS_NO_SUCH_USER;
+               }
+
+       } else {
+               return NT_STATUS_INVALID_PARAMETER;
+       }
+
+       nt_status = authsam_make_user_info_dc(tmp_ctx, sam_ctx,
+                                            lpcfg_netbios_name(lp_ctx),
+                                            lpcfg_sam_name(lp_ctx),
+                                            lpcfg_sam_dnsname(lp_ctx),
+                                            domain_dn,
+                                            msg,
+                                            user_sess_key, lm_sess_key,
+                                            user_info_dc);
+       if (!NT_STATUS_IS_OK(nt_status)) {
+               talloc_free(tmp_ctx);
+               return nt_status;
+       }
+
+       talloc_steal(mem_ctx, *user_info_dc);
+       talloc_free(tmp_ctx);
+
+       return NT_STATUS_OK;
+}
+
+NTSTATUS authsam_update_bad_pwd_count(struct ldb_context *sam_ctx,
+                                     struct ldb_message *msg,
+                                     struct ldb_dn *domain_dn)
+{
+       const char *attrs[] = { "lockoutThreshold",
+                               "lockOutObservationWindow",
+                               "lockoutDuration",
+                               "pwdProperties",
+                               NULL };
+       int ret;
+       NTSTATUS status;
+       struct ldb_result *domain_res;
+       struct ldb_message *msg_mod = NULL;
+       TALLOC_CTX *mem_ctx;
+
+       mem_ctx = talloc_new(msg);
+       if (mem_ctx == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       ret = dsdb_search_dn(sam_ctx, mem_ctx, &domain_res, domain_dn, attrs, 0);
+       if (ret != LDB_SUCCESS) {
+               TALLOC_FREE(mem_ctx);
+               return NT_STATUS_INTERNAL_DB_CORRUPTION;
+       }
+
+       status = dsdb_update_bad_pwd_count(mem_ctx, sam_ctx,
+                                          msg, domain_res->msgs[0], &msg_mod);
+       if (!NT_STATUS_IS_OK(status)) {
+               TALLOC_FREE(mem_ctx);
+               return status;
+       }
+
+       if (msg_mod != NULL) {
+               struct ldb_request *req;
+
+               ret = ldb_build_mod_req(&req, sam_ctx, sam_ctx,
+                                       msg_mod,
+                                       NULL,
+                                       NULL,
+                                       ldb_op_default_callback,
+                                       NULL);
+               if (ret != LDB_SUCCESS) {
+                       goto done;
+               }
+
+               ret = ldb_request_add_control(req,
+                                             DSDB_CONTROL_FORCE_RODC_LOCAL_CHANGE,
+                                             false, NULL);
+               if (ret != LDB_SUCCESS) {
+                       talloc_free(req);
+                       goto done;
+               }
+
+               ret = dsdb_autotransaction_request(sam_ctx, req);
+               talloc_free(req);
+       }
+
+done:
+       if (ret != LDB_SUCCESS) {
+               DEBUG(0, ("Failed to update badPwdCount, badPasswordTime or set lockoutTime on %s: %s\n",
+                         ldb_dn_get_linearized(msg_mod->dn), ldb_errstring(sam_ctx)));
+               TALLOC_FREE(mem_ctx);
+               return NT_STATUS_INTERNAL_ERROR;
+       }
+
+       TALLOC_FREE(mem_ctx);
+       return NT_STATUS_OK;
+}
+
+
+static NTSTATUS authsam_update_lastlogon_timestamp(struct ldb_context *sam_ctx,
+                                           struct ldb_message *msg_mod,
+                                           struct ldb_dn *domain_dn,
+                                           NTTIME old_timestamp,
+                                           NTTIME now)
+{
+       /*
+        * We only set lastLogonTimestamp if the current value is older than
+        * now - msDS-LogonTimeSyncInterval days.
+        *
+        * msDS-LogonTimeSyncInterval is an int32_t number of days, while
+        * lastLogonTimestamp is in the 64 bit 100ns NTTIME format.
+        *
+        * The docs say: "the initial update, after the domain functional
+        * level is raised to DS_BEHAVIOR_WIN2003 or higher, is calculated as
+        * 14 days minus a random percentage of 5 days", but we aren't doing
+        * that. The blogosphere seems to think that this randomised update
+        * happens everytime, but [MS-ADA1] doesn't agree.
+        *
+        * Dochelp referred us to the following blog post:
+        * http://blogs.technet.com/b/askds/archive/2009/04/15/the-lastlogontimestamp-attribute-what-it-was-designed-for-and-how-it-works.aspx
+        *
+        * en msDS-LogonTimeSyncInterval is zero, the lastLogonTimestamp is
+        * not changed.
+        */
+       static const char *attrs[] = { "msDS-LogonTimeSyncInterval",
+                                       NULL };
+       int ret;
+       struct ldb_result *domain_res = NULL;
+       TALLOC_CTX *mem_ctx = NULL;
+       int32_t sync_interval;
+       NTTIME sync_interval_nt;
+
+       mem_ctx = talloc_new(msg_mod);
+       if (mem_ctx == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       ret = dsdb_search_dn(sam_ctx, mem_ctx, &domain_res, domain_dn, attrs,
+                            0);
+       if (ret != LDB_SUCCESS || domain_res->count != 1) {
+               TALLOC_FREE(mem_ctx);
+               return NT_STATUS_INTERNAL_DB_CORRUPTION;
+       }
+
+       sync_interval = ldb_msg_find_attr_as_int(domain_res->msgs[0],
+                                                "msDS-LogonTimeSyncInterval",
+                                                14);
+       DEBUG(5, ("sync interval is %d\n", sync_interval));
+       if (sync_interval == 0){
+               /*
+                * Setting msDS-LogonTimeSyncInterval to zero is how you ask
+                * that nothing happens here.
+                */
+               TALLOC_FREE(mem_ctx);
+               return NT_STATUS_OK;
+       }
+       else if (sync_interval >= 5){
+               /*
+                * Subtract "a random percentage of 5" days. Presumably this
+                * percentage is between 0 and 100, and modulus is accurate
+                * enough.
+                */
+               uint32_t r = generate_random() % 6;
+               sync_interval -= r;
+               DEBUG(5, ("randomised sync interval is %d (-%d)\n", sync_interval, r));
+       }
+       /* In the case where sync_interval < 5 there is no randomisation */
+
+       sync_interval_nt = sync_interval * 24LL * 3600LL * 10000000LL;
+
+       DEBUG(5, ("old timestamp is %lld, threshold %lld, diff %lld\n",
+                 (long long int)old_timestamp,
+                 (long long int)(now - sync_interval_nt),
+                 (long long int)(old_timestamp - now + sync_interval_nt)));
+
+       if (old_timestamp > now){
+               DEBUG(0, ("lastLogonTimestamp is in the future! (%lld > %lld)\n",
+                         (long long int)old_timestamp, (long long int)now));
+               /* then what? */
+
+       } else if (old_timestamp < now - sync_interval_nt){
+               DEBUG(5, ("updating lastLogonTimestamp to %lld\n",
+                         (long long int)now));
+
+               /* The time has come to update lastLogonTimestamp */
+               ret = samdb_msg_add_int64(sam_ctx, msg_mod, msg_mod,
+                                         "lastLogonTimestamp", now);
+
+               if (ret != LDB_SUCCESS) {
+                       TALLOC_FREE(mem_ctx);
+                       return NT_STATUS_NO_MEMORY;
+               }
+       }
+       TALLOC_FREE(mem_ctx);
+       return NT_STATUS_OK;
+}
+
+/****************************************************************************
+ Look for the specified user in the sam, return ldb result structures
+****************************************************************************/
+
+NTSTATUS authsam_search_account(TALLOC_CTX *mem_ctx, struct ldb_context *sam_ctx,
+                                        const char *account_name,
+                                        struct ldb_dn *domain_dn,
+                                        struct ldb_message **ret_msg)
+{
+       int ret;
+
+       /* pull the user attributes */
+       ret = dsdb_search_one(sam_ctx, mem_ctx, ret_msg, domain_dn, LDB_SCOPE_SUBTREE,
+                             user_attrs,
+                             DSDB_SEARCH_SHOW_EXTENDED_DN,
+                             "(&(sAMAccountName=%s)(objectclass=user))",
+                             ldb_binary_encode_string(mem_ctx, account_name));
+       if (ret == LDB_ERR_NO_SUCH_OBJECT) {
+               DEBUG(3,("sam_search_user: Couldn't find user [%s] in samdb, under %s\n",
+                        account_name, ldb_dn_get_linearized(domain_dn)));
+               return NT_STATUS_NO_SUCH_USER;
+       }
+       if (ret != LDB_SUCCESS) {
+               return NT_STATUS_INTERNAL_DB_CORRUPTION;
+       }
+
+       return NT_STATUS_OK;
+}
+
+
+/* Reset the badPwdCount to zero and update the lastLogon time. */
+NTSTATUS authsam_logon_success_accounting(struct ldb_context *sam_ctx,
+                                         const struct ldb_message *msg,
+                                         struct ldb_dn *domain_dn,
+                                         bool interactive_or_kerberos)
+{
+       int ret;
+       NTSTATUS status;
+       int badPwdCount;
+       int64_t lockoutTime;
+       struct ldb_message *msg_mod;
+       TALLOC_CTX *mem_ctx;
+       struct timeval tv_now;
+       NTTIME now;
+       NTTIME lastLogonTimestamp;
+       bool am_rodc = false;
+
+       mem_ctx = talloc_new(msg);
+       if (mem_ctx == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       lockoutTime = ldb_msg_find_attr_as_int64(msg, "lockoutTime", 0);
+       if (interactive_or_kerberos) {
+               badPwdCount = ldb_msg_find_attr_as_int(msg, "badPwdCount", 0);
+       } else {
+               badPwdCount = samdb_result_effective_badPwdCount(sam_ctx, mem_ctx,
+                                                                domain_dn, msg);
+       }
+       lastLogonTimestamp =
+               ldb_msg_find_attr_as_int64(msg, "lastLogonTimestamp", 0);
+
+       DEBUG(5, ("lastLogonTimestamp is %lld\n",
+                 (long long int)lastLogonTimestamp));
+
+       msg_mod = ldb_msg_new(mem_ctx);
+       if (msg_mod == NULL) {
+               TALLOC_FREE(mem_ctx);
+               return NT_STATUS_NO_MEMORY;
+       }
+       msg_mod->dn = msg->dn;
+
+       if (lockoutTime != 0) {
+               /*
+                * This implies "badPwdCount" = 0, see samldb_lockout_time()
+                */
+               ret = samdb_msg_add_int(sam_ctx, msg_mod, msg_mod, "lockoutTime", 0);
+               if (ret != LDB_SUCCESS) {
+                       TALLOC_FREE(mem_ctx);
+                       return NT_STATUS_NO_MEMORY;
+               }
+       } else if (badPwdCount != 0) {
+               ret = samdb_msg_add_int(sam_ctx, msg_mod, msg_mod, "badPwdCount", 0);
+               if (ret != LDB_SUCCESS) {
+                       TALLOC_FREE(mem_ctx);
+                       return NT_STATUS_NO_MEMORY;
+               }
+       }
+
+       tv_now = timeval_current();
+       now = timeval_to_nttime(&tv_now);
+
+       if (interactive_or_kerberos ||
+           (badPwdCount != 0 && lockoutTime == 0)) {
+               ret = samdb_msg_add_int64(sam_ctx, msg_mod, msg_mod,
+                                         "lastLogon", now);
+               if (ret != LDB_SUCCESS) {
+                       TALLOC_FREE(mem_ctx);
+                       return NT_STATUS_NO_MEMORY;
+               }
+       }
+
+       if (interactive_or_kerberos) {
+               int logonCount;
+
+               logonCount = ldb_msg_find_attr_as_int(msg, "logonCount", 0);
+
+               logonCount += 1;
+
+               ret = samdb_msg_add_int(sam_ctx, msg_mod, msg_mod,
+                                       "logonCount", logonCount);
+               if (ret != LDB_SUCCESS) {
+                       TALLOC_FREE(mem_ctx);
+                       return NT_STATUS_NO_MEMORY;
+               }
+       } else {
+               /* Set an unset logonCount to 0 on first successful login */
+               if (ldb_msg_find_ldb_val(msg, "logonCount") == NULL) {
+                       ret = samdb_msg_add_int(sam_ctx, msg_mod, msg_mod,
+                                               "logonCount", 0);
+                       if (ret != LDB_SUCCESS) {
+                               TALLOC_FREE(mem_ctx);
+                               return NT_STATUS_NO_MEMORY;
+                       }
+               }
+       }
+
+       ret = samdb_rodc(sam_ctx, &am_rodc);
+       if (ret != LDB_SUCCESS) {
+               TALLOC_FREE(mem_ctx);
+               return NT_STATUS_INTERNAL_ERROR;
+       }
+
+       if (!am_rodc) {
+               /* TODO Perform the (async) SendToSAM calls for MS-SAMS */
+               status = authsam_update_lastlogon_timestamp(sam_ctx, msg_mod, domain_dn,
+                                                           lastLogonTimestamp, now);
+               if (!NT_STATUS_IS_OK(status)) {
+                       TALLOC_FREE(mem_ctx);
+                       return NT_STATUS_NO_MEMORY;
+               }
+       }
+
+       if (msg_mod->num_elements > 0) {
+               unsigned int i;
+               struct ldb_request *req;
+
+               /* mark all the message elements as LDB_FLAG_MOD_REPLACE */
+               for (i=0;i<msg_mod->num_elements;i++) {
+                       msg_mod->elements[i].flags = LDB_FLAG_MOD_REPLACE;
+               }
+
+               ret = ldb_build_mod_req(&req, sam_ctx, sam_ctx,
+                                       msg_mod,
+                                       NULL,
+                                       NULL,
+                                       ldb_op_default_callback,
+                                       NULL);
+               if (ret != LDB_SUCCESS) {
+                       goto done;
+               }
+
+               ret = ldb_request_add_control(req,
+                                             DSDB_CONTROL_FORCE_RODC_LOCAL_CHANGE,
+                                             false, NULL);
+               if (ret != LDB_SUCCESS) {
+                       talloc_free(req);
+                       goto done;
+               }
+
+               ret = dsdb_autotransaction_request(sam_ctx, req);
+               talloc_free(req);
+       }
+
+done:
+       if (ret != LDB_SUCCESS) {
+               DEBUG(0, ("Failed to set badPwdCount and lockoutTime "
+                         "to 0 and/or  lastlogon to now (%lld) "
+                         "%s: %s\n", (long long int)now,
+                         ldb_dn_get_linearized(msg_mod->dn),
+                         ldb_errstring(sam_ctx)));
+               TALLOC_FREE(mem_ctx);
+               return NT_STATUS_INTERNAL_ERROR;
+       }
+
+       TALLOC_FREE(mem_ctx);
+       return NT_STATUS_OK;
+}