s4:rpc_server: only use context within op_bind() hooks and dcesrv_interface_bind_...
[samba.git] / source4 / rpc_server / samr / dcesrv_samr.c
index 391bc7fcb055d2a2ed85602874a3fc63fbedee6f..0ddbaae835d4230b147314c7c000248712950eea 100644 (file)
@@ -28,8 +28,8 @@
 #include "rpc_server/common/common.h"
 #include "rpc_server/samr/dcesrv_samr.h"
 #include "system/time.h"
-#include "lib/ldb/include/ldb.h"
-#include "lib/ldb/include/ldb_errors.h"
+#include <ldb.h>
+#include <ldb_errors.h>
 #include "../libds/common/flags.h"
 #include "dsdb/samdb/samdb.h"
 #include "dsdb/common/util.h"
 #include "../lib/util/util_ldb.h"
 #include "param/param.h"
 #include "lib/util/tsort.h"
+#include "libds/common/flag_mapping.h"
+
+#define DCESRV_INTERFACE_SAMR_BIND(call, iface) \
+       dcesrv_interface_samr_bind(call, iface)
+static NTSTATUS dcesrv_interface_samr_bind(struct dcesrv_call_state *dce_call,
+                                            const struct dcesrv_interface *iface)
+{
+       struct dcesrv_connection_context *context = dce_call->context;
+       return dcesrv_interface_bind_reject_connect(context, iface);
+}
 
 /* these query macros make samr_Query[User|Group|Alias]Info a bit easier to read */
 
 #define QUERY_APASSC(msg, field, attr) \
        info->field = samdb_result_allow_password_change(sam_ctx, mem_ctx, \
                                                         a_state->domain_state->domain_dn, msg, attr);
-#define QUERY_FPASSC(msg, field, attr) \
-       info->field = samdb_result_force_password_change(sam_ctx, mem_ctx, \
+#define QUERY_BPWDCT(msg, field, attr) \
+       info->field = samdb_result_effective_badPwdCount(sam_ctx, mem_ctx, \
                                                         a_state->domain_state->domain_dn, msg);
 #define QUERY_LHOURS(msg, field, attr) \
        info->field = samdb_result_logon_hours(mem_ctx, msg, attr);
 #define QUERY_AFLAGS(msg, field, attr) \
-       info->field = samdb_result_acct_flags(sam_ctx, mem_ctx, msg, a_state->domain_state->domain_dn);
-#define QUERY_PARAMETERS(msg, field, attr) \
-       info->field = samdb_result_parameters(mem_ctx, msg, attr);
+       info->field = samdb_result_acct_flags(msg, attr);
 
 
 /* these are used to make the Set[User|Group]Info code easier to follow */
        set_el->flags = LDB_FLAG_MOD_REPLACE;                           \
 } while (0)
 
-#define CHECK_FOR_MULTIPLES(value, flag, poss_flags)   \
-       do { \
-               if ((value & flag) && ((value & flag) != (value & (poss_flags)))) { \
-                       return NT_STATUS_INVALID_PARAMETER;             \
-               }                                                       \
-       } while (0)                                                     \
-
 /* Set account flags, discarding flags that cannot be set with SAMR */
 #define SET_AFLAGS(msg, field, attr) do {                              \
        struct ldb_message_element *set_el;                             \
-       if ((r->in.info->field & (ACB_NORMAL | ACB_DOMTRUST | ACB_WSTRUST | ACB_SVRTRUST)) == 0) { \
-               return NT_STATUS_INVALID_PARAMETER; \
-       }                                                               \
-       CHECK_FOR_MULTIPLES(r->in.info->field, ACB_NORMAL, ACB_NORMAL | ACB_DOMTRUST | ACB_WSTRUST | ACB_SVRTRUST); \
-       CHECK_FOR_MULTIPLES(r->in.info->field, ACB_DOMTRUST, ACB_NORMAL | ACB_DOMTRUST | ACB_WSTRUST | ACB_SVRTRUST); \
-       CHECK_FOR_MULTIPLES(r->in.info->field, ACB_WSTRUST, ACB_NORMAL | ACB_DOMTRUST | ACB_WSTRUST | ACB_SVRTRUST); \
-       CHECK_FOR_MULTIPLES(r->in.info->field, ACB_SVRTRUST, ACB_NORMAL | ACB_DOMTRUST | ACB_WSTRUST | ACB_SVRTRUST); \
-       if (samdb_msg_add_acct_flags(sam_ctx, mem_ctx, msg, attr, (r->in.info->field & ~(ACB_AUTOLOCK|ACB_PW_EXPIRED))) != 0) { \
+       if (samdb_msg_add_acct_flags(sam_ctx, mem_ctx, msg, attr, r->in.info->field) != 0) { \
                return NT_STATUS_NO_MEMORY;                             \
        }                                                               \
         set_el = ldb_msg_find_element(msg, attr);                      \
        }                                                               \
 } while (0)
 
+/*
+ * Clear a GUID cache
+ */
+static void clear_guid_cache(struct samr_guid_cache *cache)
+{
+       cache->handle = 0;
+       cache->size = 0;
+       TALLOC_FREE(cache->entries);
+}
+
+/*
+ * initialize a GUID cache
+ */
+static void initialize_guid_cache(struct samr_guid_cache *cache)
+{
+       cache->handle = 0;
+       cache->size = 0;
+       cache->entries = NULL;
+}
 
+static NTSTATUS load_guid_cache(
+       struct samr_guid_cache *cache,
+       struct samr_domain_state *d_state,
+       unsigned int ldb_cnt,
+       struct ldb_message **res)
+{
+       NTSTATUS status = NT_STATUS_OK;
+       unsigned int i;
+       TALLOC_CTX *frame = talloc_stackframe();
+
+       clear_guid_cache(cache);
+
+       /*
+        * Store the GUID's in the cache.
+        */
+       cache->handle = 0;
+       cache->size = ldb_cnt;
+       cache->entries = talloc_array(d_state, struct GUID, ldb_cnt);
+       if (cache->entries == NULL) {
+               clear_guid_cache(cache);
+               status = NT_STATUS_NO_MEMORY;
+               goto exit;
+       }
+
+       /*
+        * Extract a list of the GUIDs for all the matching objects
+        * we cache just the GUIDS to reduce the memory overhead of
+        * the result cache.
+        */
+       for (i = 0; i < ldb_cnt; i++) {
+               cache->entries[i] = samdb_result_guid(res[i], "objectGUID");
+       }
+exit:
+       TALLOC_FREE(frame);
+       return status;
+}
 
 /*
   samr_Connect
 static NTSTATUS dcesrv_samr_Connect(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
                             struct samr_Connect *r)
 {
+       struct auth_session_info *session_info =
+               dcesrv_call_session_info(dce_call);
        struct samr_connect_state *c_state;
        struct dcesrv_handle *handle;
 
@@ -173,14 +224,19 @@ static NTSTATUS dcesrv_samr_Connect(struct dcesrv_call_state *dce_call, TALLOC_C
        }
 
        /* make sure the sam database is accessible */
-       c_state->sam_ctx = samdb_connect(c_state, dce_call->event_ctx, dce_call->conn->dce_ctx->lp_ctx, dce_call->conn->auth_state.session_info, 0);
+       c_state->sam_ctx = samdb_connect(c_state,
+                                        dce_call->event_ctx,
+                                        dce_call->conn->dce_ctx->lp_ctx,
+                                        session_info,
+                                        dce_call->conn->remote_address,
+                                        0);
        if (c_state->sam_ctx == NULL) {
                talloc_free(c_state);
                return NT_STATUS_INVALID_SYSTEM_SERVICE;
        }
 
 
-       handle = dcesrv_handle_new(dce_call->context, SAMR_HANDLE_CONNECT);
+       handle = dcesrv_handle_create(dce_call, SAMR_HANDLE_CONNECT);
        if (!handle) {
                talloc_free(c_state);
                return NT_STATUS_NO_MEMORY;
@@ -325,7 +381,6 @@ static NTSTATUS dcesrv_samr_LookupDomain(struct dcesrv_call_state *dce_call, TAL
 static NTSTATUS dcesrv_samr_EnumDomains(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
                                 struct samr_EnumDomains *r)
 {
-       struct samr_connect_state *c_state;
        struct dcesrv_handle *h;
        struct samr_SamArray *array;
        uint32_t i, start_i;
@@ -336,8 +391,6 @@ static NTSTATUS dcesrv_samr_EnumDomains(struct dcesrv_call_state *dce_call, TALL
 
        DCESRV_PULL_HANDLE(h, r->in.connect_handle, SAMR_HANDLE_CONNECT);
 
-       c_state = h->data;
-
        *r->out.resume_handle = 2;
 
        start_i = *r->in.resume_handle;
@@ -389,6 +442,7 @@ static NTSTATUS dcesrv_samr_OpenDomain(struct dcesrv_call_state *dce_call, TALLO
        const char * const dom_attrs[] = { "cn", NULL};
        struct ldb_message **dom_msgs;
        int ret;
+       unsigned int i;
 
        ZERO_STRUCTP(r->out.domain_handle);
 
@@ -440,7 +494,11 @@ static NTSTATUS dcesrv_samr_OpenDomain(struct dcesrv_call_state *dce_call, TALLO
 
        d_state->lp_ctx = dce_call->conn->dce_ctx->lp_ctx;
 
-       h_domain = dcesrv_handle_new(dce_call->context, SAMR_HANDLE_DOMAIN);
+       for (i = 0; i < SAMR_LAST_CACHE; i++) {
+               initialize_guid_cache(&d_state->guid_caches[i]);
+       }
+
+       h_domain = dcesrv_handle_create(dce_call, SAMR_HANDLE_DOMAIN);
        if (!h_domain) {
                talloc_free(d_state);
                return NT_STATUS_NO_MEMORY;
@@ -499,7 +557,7 @@ static NTSTATUS dcesrv_samr_info_DomGeneralInformation(struct samr_domain_state
        info->sequence_num = ldb_msg_find_attr_as_uint64(dom_msgs[0], "modifiedCount",
                                                 0);
        switch (state->role) {
-       case ROLE_DOMAIN_CONTROLLER:
+       case ROLE_ACTIVE_DIRECTORY_DC:
                /* This pulls the NetBIOS name from the
                   cn=NTDS Settings,cn=<NETBIOS name of PDC>,....
                   string */
@@ -509,6 +567,10 @@ static NTSTATUS dcesrv_samr_info_DomGeneralInformation(struct samr_domain_state
                        info->role = SAMR_ROLE_DOMAIN_BDC;
                }
                break;
+       case ROLE_DOMAIN_PDC:
+       case ROLE_DOMAIN_BDC:
+       case ROLE_AUTO:
+               return NT_STATUS_INTERNAL_ERROR;
        case ROLE_DOMAIN_MEMBER:
                info->role = SAMR_ROLE_DOMAIN_MEMBER;
                break;
@@ -522,12 +584,12 @@ static NTSTATUS dcesrv_samr_info_DomGeneralInformation(struct samr_domain_state
                                             "(objectClass=user)");
        info->num_groups = samdb_search_count(state->sam_ctx, mem_ctx,
                                              state->domain_dn,
-                                             "(&(objectClass=group)(|(groupType=%u)(groupType=%u)))",
+                                             "(&(objectClass=group)(|(groupType=%d)(groupType=%d)))",
                                              GTYPE_SECURITY_UNIVERSAL_GROUP,
                                              GTYPE_SECURITY_GLOBAL_GROUP);
        info->num_aliases = samdb_search_count(state->sam_ctx, mem_ctx,
                                               state->domain_dn,
-                                              "(&(objectClass=group)(|(groupType=%u)(groupType=%u)))",
+                                              "(&(objectClass=group)(|(groupType=%d)(groupType=%d)))",
                                               GTYPE_SECURITY_BUILTIN_LOCAL_GROUP,
                                               GTYPE_SECURITY_DOMAIN_LOCAL_GROUP);
 
@@ -602,7 +664,7 @@ static NTSTATUS dcesrv_samr_info_DomInfo7(struct samr_domain_state *state,
 {
 
        switch (state->role) {
-       case ROLE_DOMAIN_CONTROLLER:
+       case ROLE_ACTIVE_DIRECTORY_DC:
                /* This pulls the NetBIOS name from the
                   cn=NTDS Settings,cn=<NETBIOS name of PDC>,....
                   string */
@@ -612,6 +674,10 @@ static NTSTATUS dcesrv_samr_info_DomInfo7(struct samr_domain_state *state,
                        info->role = SAMR_ROLE_DOMAIN_BDC;
                }
                break;
+       case ROLE_DOMAIN_PDC:
+       case ROLE_DOMAIN_BDC:
+       case ROLE_AUTO:
+               return NT_STATUS_INTERNAL_ERROR;
        case ROLE_DOMAIN_MEMBER:
                info->role = SAMR_ROLE_DOMAIN_MEMBER;
                break;
@@ -984,9 +1050,7 @@ static NTSTATUS dcesrv_samr_SetDomainInfo(struct dcesrv_call_state *dce_call, TA
                DEBUG(1,("Failed to modify record %s: %s\n",
                         ldb_dn_get_linearized(d_state->domain_dn),
                         ldb_errstring(sam_ctx)));
-
-               /* we really need samdb.c to return NTSTATUS */
-               return NT_STATUS_UNSUCCESSFUL;
+               return dsdb_ldb_err_to_ntstatus(ret);
        }
 
        return NT_STATUS_OK;
@@ -1042,7 +1106,7 @@ static NTSTATUS dcesrv_samr_CreateDomainGroup(struct dcesrv_call_state *dce_call
        a_state->account_name = talloc_steal(a_state, groupname);
 
        /* create the policy handle */
-       g_handle = dcesrv_handle_new(dce_call->context, SAMR_HANDLE_GROUP);
+       g_handle = dcesrv_handle_create(dce_call, SAMR_HANDLE_GROUP);
        if (!g_handle) {
                return NT_STATUS_NO_MEMORY;
        }
@@ -1064,6 +1128,63 @@ static int compare_SamEntry(struct samr_SamEntry *e1, struct samr_SamEntry *e2)
        return e1->idx - e2->idx;
 }
 
+static int compare_msgRid(struct ldb_message **m1, struct ldb_message **m2) {
+       struct dom_sid *sid1 = NULL;
+       struct dom_sid *sid2 = NULL;
+       uint32_t rid1;
+       uint32_t rid2;
+       int res = 0;
+       NTSTATUS status;
+       TALLOC_CTX *frame = talloc_stackframe();
+
+       sid1 = samdb_result_dom_sid(frame, *m1, "objectSid");
+       sid2 = samdb_result_dom_sid(frame, *m2, "objectSid");
+
+       /*
+        * If entries don't have a SID we want to sort them to the end of
+        * the list.
+        */
+       if (sid1 == NULL && sid2 == NULL) {
+               res = 0;
+               goto exit;
+       } else if (sid2 == NULL) {
+               res = 1;
+               goto exit;
+       } else if (sid1 == NULL) {
+               res = -1;
+               goto exit;
+       }
+
+       /*
+        * Get and compare the rids, if we fail to extract a rid treat it as a
+        * missing SID and sort to the end of the list
+        */
+       status = dom_sid_split_rid(NULL, sid1, NULL, &rid1);
+       if (!NT_STATUS_IS_OK(status)) {
+               res = 1;
+               goto exit;
+       }
+
+       status = dom_sid_split_rid(NULL, sid2, NULL, &rid2);
+       if (!NT_STATUS_IS_OK(status)) {
+               res = -1;
+               goto exit;
+       }
+
+       if (rid1 == rid2) {
+               res = 0;
+       }
+       else if (rid1 > rid2) {
+               res = 1;
+       }
+       else {
+               res = -1;
+       }
+exit:
+       TALLOC_FREE(frame);
+       return res;
+}
+
 /*
   samr_EnumDomainGroups
 */
@@ -1073,11 +1194,17 @@ static NTSTATUS dcesrv_samr_EnumDomainGroups(struct dcesrv_call_state *dce_call,
        struct dcesrv_handle *h;
        struct samr_domain_state *d_state;
        struct ldb_message **res;
-       int i, ldb_cnt;
-       uint32_t first, count;
+       uint32_t i;
+       uint32_t count;
+       uint32_t results;
+       uint32_t max_entries;
+       uint32_t remaining_entries;
+       uint32_t resume_handle;
        struct samr_SamEntry *entries;
        const char * const attrs[] = { "objectSid", "sAMAccountName", NULL };
+       const char * const cache_attrs[] = { "objectSid", "objectGUID", NULL };
        struct samr_SamArray *sam;
+       struct samr_guid_cache *cache = NULL;
 
        *r->out.resume_handle = 0;
        *r->out.sam = NULL;
@@ -1086,77 +1213,192 @@ static NTSTATUS dcesrv_samr_EnumDomainGroups(struct dcesrv_call_state *dce_call,
        DCESRV_PULL_HANDLE(h, r->in.domain_handle, SAMR_HANDLE_DOMAIN);
 
        d_state = h->data;
+       cache = &d_state->guid_caches[SAMR_ENUM_DOMAIN_GROUPS_CACHE];
+
+       /*
+        * If the resume_handle is zero, query the database and cache the
+        * matching GUID's
+        */
+       if (*r->in.resume_handle == 0) {
+               NTSTATUS status;
+               int ldb_cnt;
+               clear_guid_cache(cache);
+               /*
+                * search for all domain groups in this domain.
+                */
+               ldb_cnt = samdb_search_domain(
+                   d_state->sam_ctx,
+                   mem_ctx,
+                   d_state->domain_dn,
+                   &res,
+                   cache_attrs,
+                   d_state->domain_sid,
+                   "(&(|(groupType=%d)(groupType=%d))(objectClass=group))",
+                   GTYPE_SECURITY_UNIVERSAL_GROUP,
+                   GTYPE_SECURITY_GLOBAL_GROUP);
+               if (ldb_cnt < 0) {
+                       return NT_STATUS_INTERNAL_DB_CORRUPTION;
+               }
+               /*
+                * Sort the results into RID order, while the spec states there
+                * is no order, Windows appears to sort the results by RID and
+                * so it is possible that there are clients that depend on
+                * this ordering
+                */
+               TYPESAFE_QSORT(res, ldb_cnt, compare_msgRid);
 
-       /* search for all domain groups in this domain. This could possibly be
-          cached and resumed based on resume_key */
-       ldb_cnt = samdb_search_domain(d_state->sam_ctx, mem_ctx,
-                                     d_state->domain_dn, &res, attrs,
-                                     d_state->domain_sid,
-                                     "(&(|(groupType=%d)(groupType=%d))(objectClass=group))",
-                                     GTYPE_SECURITY_UNIVERSAL_GROUP,
-                                     GTYPE_SECURITY_GLOBAL_GROUP);
-       if (ldb_cnt < 0) {
-               return NT_STATUS_INTERNAL_DB_CORRUPTION;
-       }
+               /*
+                * cache the sorted GUID's
+                */
+               status = load_guid_cache(cache, d_state, ldb_cnt, res);
+               TALLOC_FREE(res);
+               if (!NT_STATUS_IS_OK(status)) {
+                       return status;
+               }
+               cache->handle = 0;
+       }
+
+
+       /*
+        * If the resume handle is out of range we return an empty response
+        * and invalidate the cache.
+        *
+        * From the specification:
+        * Servers SHOULD validate that EnumerationContext is an expected
+        * value for the server's implementation. Windows does NOT validate
+        * the input, though the result of malformed information merely results
+        * in inconsistent output to the client.
+        */
+       if (*r->in.resume_handle >= cache->size) {
+               clear_guid_cache(cache);
+               sam = talloc(mem_ctx, struct samr_SamArray);
+               if (!sam) {
+                       return NT_STATUS_NO_MEMORY;
+               }
+               sam->entries = NULL;
+               sam->count = 0;
 
-       /* convert to SamEntry format */
-       entries = talloc_array(mem_ctx, struct samr_SamEntry, ldb_cnt);
-       if (!entries) {
-               return NT_STATUS_NO_MEMORY;
+               *r->out.sam = sam;
+               *r->out.resume_handle = 0;
+               return NT_STATUS_OK;
        }
 
-       count = 0;
 
-       for (i=0;i<ldb_cnt;i++) {
-               struct dom_sid *group_sid;
+       /*
+        * Calculate the number of entries to return limit by max_size.
+        * Note that we use the w2k3 element size value of 54
+        */
+       max_entries = 1 + (r->in.max_size/SAMR_ENUM_USERS_MULTIPLIER);
+       remaining_entries = cache->size - *r->in.resume_handle;
+       results = MIN(remaining_entries, max_entries);
 
-               group_sid = samdb_result_dom_sid(mem_ctx, res[i],
-                                                "objectSid");
-               if (group_sid == NULL) {
+       /*
+        * Process the list of result GUID's.
+        * Read the details of each object and populate the Entries
+        * for the current level.
+        */
+       count = 0;
+       resume_handle = *r->in.resume_handle;
+       entries = talloc_array(mem_ctx, struct samr_SamEntry, results);
+       if (entries == NULL) {
+               clear_guid_cache(cache);
+               return NT_STATUS_NO_MEMORY;
+       }
+       for (i = 0; i < results; i++) {
+               struct dom_sid *objectsid;
+               uint32_t rid;
+               struct ldb_result *rec;
+               const uint32_t idx = *r->in.resume_handle + i;
+               int ret;
+               NTSTATUS status;
+               const char *name = NULL;
+               resume_handle++;
+               /*
+                * Read an object from disk using the GUID as the key
+                *
+                * If the object can not be read, or it does not have a SID
+                * it is ignored.
+                *
+                * As a consequence of this, if all the remaining GUID's
+                * have been deleted an empty result will be returned.
+                * i.e. even if the previous call returned a non zero
+                * resume_handle it is possible for no results to be returned.
+                *
+                */
+               ret = dsdb_search_by_dn_guid(d_state->sam_ctx,
+                                            mem_ctx,
+                                            &rec,
+                                            &cache->entries[idx],
+                                            attrs,
+                                            0);
+               if (ret == LDB_ERR_NO_SUCH_OBJECT) {
+                       struct GUID_txt_buf guid_buf;
+                       DBG_WARNING(
+                           "GUID [%s] not found\n",
+                           GUID_buf_string(&cache->entries[idx], &guid_buf));
+                       continue;
+               } else if (ret != LDB_SUCCESS) {
+                       clear_guid_cache(cache);
                        return NT_STATUS_INTERNAL_DB_CORRUPTION;
                }
 
-               entries[count].idx =
-                       group_sid->sub_auths[group_sid->num_auths-1];
-               entries[count].name.string =
-                       ldb_msg_find_attr_as_string(res[i], "sAMAccountName", "");
-               count += 1;
-       }
-
-       /* sort the results by rid */
-       TYPESAFE_QSORT(entries, count, compare_SamEntry);
-
-       /* find the first entry to return */
-       for (first=0;
-            first<count && entries[first].idx <= *r->in.resume_handle;
-            first++) ;
+               objectsid = samdb_result_dom_sid(mem_ctx,
+                                                rec->msgs[0],
+                                                "objectSID");
+               if (objectsid == NULL) {
+                       struct GUID_txt_buf guid_buf;
+                       DBG_WARNING(
+                           "objectSID for GUID [%s] not found\n",
+                           GUID_buf_string(&cache->entries[idx], &guid_buf));
+                       continue;
+               }
+               status = dom_sid_split_rid(NULL,
+                                          objectsid,
+                                          NULL,
+                                          &rid);
+               if (!NT_STATUS_IS_OK(status)) {
+                       struct dom_sid_buf sid_buf;
+                       struct GUID_txt_buf guid_buf;
+                       DBG_WARNING(
+                           "objectSID [%s] for GUID [%s] invalid\n",
+                           dom_sid_str_buf(objectsid, &sid_buf),
+                           GUID_buf_string(&cache->entries[idx], &guid_buf));
+                       continue;
+               }
 
-       /* return the rest, limit by max_size. Note that we
-          use the w2k3 element size value of 54 */
-       *r->out.num_entries = count - first;
-       *r->out.num_entries = MIN(*r->out.num_entries,
-                                1+(r->in.max_size/SAMR_ENUM_USERS_MULTIPLIER));
+               entries[count].idx = rid;
+               name = ldb_msg_find_attr_as_string(
+                   rec->msgs[0], "sAMAccountName", "");
+               entries[count].name.string = talloc_strdup(entries, name);
+               count++;
+       }
 
        sam = talloc(mem_ctx, struct samr_SamArray);
        if (!sam) {
+               clear_guid_cache(cache);
                return NT_STATUS_NO_MEMORY;
        }
 
-       sam->entries = entries+first;
-       sam->count = *r->out.num_entries;
+       sam->entries = entries;
+       sam->count = count;
 
        *r->out.sam = sam;
-
-       if (first == count) {
+       *r->out.resume_handle = resume_handle;
+       *r->out.num_entries = count;
+
+       /*
+        * Signal no more results by returning zero resume handle,
+        * the cache is also cleared at this point
+        */
+       if (*r->out.resume_handle >= cache->size) {
+               *r->out.resume_handle = 0;
+               clear_guid_cache(cache);
                return NT_STATUS_OK;
        }
-
-       if (*r->out.num_entries < count - first) {
-               *r->out.resume_handle = entries[first+*r->out.num_entries-1].idx;
-               return STATUS_MORE_ENTRIES;
-       }
-
-       return NT_STATUS_OK;
+       /*
+        * There are more results to be returned.
+        */
+       return STATUS_MORE_ENTRIES;
 }
 
 
@@ -1200,13 +1442,13 @@ static NTSTATUS dcesrv_samr_CreateUser2(struct dcesrv_call_state *dce_call, TALL
                return NT_STATUS_INVALID_PARAMETER;
        }
 
-       status = dsdb_add_user(d_state->sam_ctx, mem_ctx, account_name, r->in.acct_flags, &sid, &dn);
+       status = dsdb_add_user(d_state->sam_ctx, mem_ctx, account_name, r->in.acct_flags, NULL,
+                              &sid, &dn);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
        a_state = talloc(mem_ctx, struct samr_account_state);
        if (!a_state) {
-               ldb_transaction_cancel(d_state->sam_ctx);
                return NT_STATUS_NO_MEMORY;
        }
        a_state->sam_ctx = d_state->sam_ctx;
@@ -1220,7 +1462,7 @@ static NTSTATUS dcesrv_samr_CreateUser2(struct dcesrv_call_state *dce_call, TALL
        }
 
        /* create the policy handle */
-       u_handle = dcesrv_handle_new(dce_call->context, SAMR_HANDLE_USER);
+       u_handle = dcesrv_handle_create(dce_call, SAMR_HANDLE_USER);
        if (!u_handle) {
                return NT_STATUS_NO_MEMORY;
        }
@@ -1247,13 +1489,16 @@ static NTSTATUS dcesrv_samr_CreateUser(struct dcesrv_call_state *dce_call, TALLO
 
 
        /* a simple wrapper around samr_CreateUser2 works nicely */
-       r2.in.domain_handle = r->in.domain_handle;
-       r2.in.account_name = r->in.account_name;
-       r2.in.acct_flags = ACB_NORMAL;
-       r2.in.access_mask = r->in.access_mask;
-       r2.out.user_handle = r->out.user_handle;
-       r2.out.access_granted = &access_granted;
-       r2.out.rid = r->out.rid;
+
+       r2 = (struct samr_CreateUser2) {
+               .in.domain_handle = r->in.domain_handle,
+               .in.account_name = r->in.account_name,
+               .in.acct_flags = ACB_NORMAL,
+               .in.access_mask = r->in.access_mask,
+               .out.user_handle = r->out.user_handle,
+               .out.access_granted = &access_granted,
+               .out.rid = r->out.rid
+       };
 
        return dcesrv_samr_CreateUser2(dce_call, mem_ctx, &r2);
 }
@@ -1267,12 +1512,18 @@ static NTSTATUS dcesrv_samr_EnumDomainUsers(struct dcesrv_call_state *dce_call,
        struct dcesrv_handle *h;
        struct samr_domain_state *d_state;
        struct ldb_message **res;
-       int i, ldb_cnt;
-       uint32_t first, count;
+       uint32_t i;
+       uint32_t count;
+       uint32_t results;
+       uint32_t max_entries;
+       uint32_t remaining_entries;
+       uint32_t resume_handle;
        struct samr_SamEntry *entries;
        const char * const attrs[] = { "objectSid", "sAMAccountName",
                "userAccountControl", NULL };
+       const char *const cache_attrs[] = {"objectSid", "objectGUID", NULL};
        struct samr_SamArray *sam;
+       struct samr_guid_cache *cache = NULL;
 
        *r->out.resume_handle = 0;
        *r->out.sam = NULL;
@@ -1281,74 +1532,192 @@ static NTSTATUS dcesrv_samr_EnumDomainUsers(struct dcesrv_call_state *dce_call,
        DCESRV_PULL_HANDLE(h, r->in.domain_handle, SAMR_HANDLE_DOMAIN);
 
        d_state = h->data;
+       cache = &d_state->guid_caches[SAMR_ENUM_DOMAIN_USERS_CACHE];
+
+       /*
+        * If the resume_handle is zero, query the database and cache the
+        * matching GUID's
+        */
+       if (*r->in.resume_handle == 0) {
+               NTSTATUS status;
+               int ldb_cnt;
+               clear_guid_cache(cache);
+               /*
+                * search for all domain users in this domain.
+                */
+               ldb_cnt = samdb_search_domain(d_state->sam_ctx,
+                                             mem_ctx,
+                                             d_state->domain_dn,
+                                             &res,
+                                             cache_attrs,
+                                             d_state->domain_sid,
+                                             "(objectClass=user)");
+               if (ldb_cnt < 0) {
+                       return NT_STATUS_INTERNAL_DB_CORRUPTION;
+               }
+               /*
+                * Sort the results into RID order, while the spec states there
+                * is no order, Windows appears to sort the results by RID and
+                * so it is possible that there are clients that depend on
+                * this ordering
+                */
+               TYPESAFE_QSORT(res, ldb_cnt, compare_msgRid);
 
-       /* search for all domain users in this domain. This could possibly be
-          cached and resumed on resume_key */
-       ldb_cnt = samdb_search_domain(d_state->sam_ctx, mem_ctx,
-                                     d_state->domain_dn,
-                                     &res, attrs,
-                                     d_state->domain_sid,
-                                     "(objectClass=user)");
-       if (ldb_cnt < 0) {
-               return NT_STATUS_INTERNAL_DB_CORRUPTION;
-       }
+               /*
+                * cache the sorted GUID's
+                */
+               status = load_guid_cache(cache, d_state, ldb_cnt, res);
+               TALLOC_FREE(res);
+               if (!NT_STATUS_IS_OK(status)) {
+                       return status;
+               }
+               cache->handle = 0;
+       }
+
+       /*
+        * If the resume handle is out of range we return an empty response
+        * and invalidate the cache.
+        *
+        * From the specification:
+        * Servers SHOULD validate that EnumerationContext is an expected
+        * value for the server's implementation. Windows does NOT validate
+        * the input, though the result of malformed information merely results
+        * in inconsistent output to the client.
+        */
+       if (*r->in.resume_handle >= cache->size) {
+               clear_guid_cache(cache);
+               sam = talloc(mem_ctx, struct samr_SamArray);
+               if (!sam) {
+                       return NT_STATUS_NO_MEMORY;
+               }
+               sam->entries = NULL;
+               sam->count = 0;
 
-       /* convert to SamEntry format */
-       entries = talloc_array(mem_ctx, struct samr_SamEntry, ldb_cnt);
-       if (!entries) {
-               return NT_STATUS_NO_MEMORY;
+               *r->out.sam = sam;
+               *r->out.resume_handle = 0;
+               return NT_STATUS_OK;
        }
 
+       /*
+        * Calculate the number of entries to return limit by max_size.
+        * Note that we use the w2k3 element size value of 54
+        */
+       max_entries = 1 + (r->in.max_size / SAMR_ENUM_USERS_MULTIPLIER);
+       remaining_entries = cache->size - *r->in.resume_handle;
+       results = MIN(remaining_entries, max_entries);
+
+       /*
+        * Process the list of result GUID's.
+        * Read the details of each object and populate the Entries
+        * for the current level.
+        */
        count = 0;
+       resume_handle = *r->in.resume_handle;
+       entries = talloc_array(mem_ctx, struct samr_SamEntry, results);
+       if (entries == NULL) {
+               clear_guid_cache(cache);
+               return NT_STATUS_NO_MEMORY;
+       }
+       for (i = 0; i < results; i++) {
+               struct dom_sid *objectsid;
+               uint32_t rid;
+               struct ldb_result *rec;
+               const uint32_t idx = *r->in.resume_handle + i;
+               int ret;
+               NTSTATUS status;
+               const char *name = NULL;
 
-       for (i=0;i<ldb_cnt;i++) {
-               /* Check if a mask has been requested */
-               if (r->in.acct_flags
-                   && ((samdb_result_acct_flags(d_state->sam_ctx, mem_ctx,
-                                                res[i], d_state->domain_dn) & r->in.acct_flags) == 0)) {
+               resume_handle++;
+               /*
+                * Read an object from disk using the GUID as the key
+                *
+                * If the object can not be read, or it does not have a SID
+                * it is ignored.
+                *
+                * As a consequence of this, if all the remaining GUID's
+                * have been deleted an empty result will be returned.
+                * i.e. even if the previous call returned a non zero
+                * resume_handle it is possible for no results to be returned.
+                *
+                */
+               ret = dsdb_search_by_dn_guid(d_state->sam_ctx,
+                                            mem_ctx,
+                                            &rec,
+                                            &cache->entries[idx],
+                                            attrs,
+                                            0);
+               if (ret == LDB_ERR_NO_SUCH_OBJECT) {
+                       struct GUID_txt_buf guid_buf;
+                       DBG_WARNING(
+                           "GUID [%s] not found\n",
+                           GUID_buf_string(&cache->entries[idx], &guid_buf));
+                       continue;
+               } else if (ret != LDB_SUCCESS) {
+                       clear_guid_cache(cache);
+                       return NT_STATUS_INTERNAL_DB_CORRUPTION;
+               }
+               objectsid = samdb_result_dom_sid(mem_ctx,
+                                                rec->msgs[0],
+                                                "objectSID");
+               if (objectsid == NULL) {
+                       struct GUID_txt_buf guid_buf;
+                       DBG_WARNING(
+                           "objectSID for GUID [%s] not found\n",
+                           GUID_buf_string(&cache->entries[idx], &guid_buf));
+                       continue;
+               }
+               if (r->in.acct_flags &&
+                   ((samdb_result_acct_flags(rec->msgs[0], NULL) &
+                     r->in.acct_flags) == 0)) {
+                       continue;
+               }
+               status = dom_sid_split_rid(NULL,
+                                          objectsid,
+                                          NULL,
+                                          &rid);
+               if (!NT_STATUS_IS_OK(status)) {
+                       struct dom_sid_buf sid_buf;
+                       struct GUID_txt_buf guid_buf;
+                       DBG_WARNING(
+                           "objectSID [%s] for GUID [%s] invalid\n",
+                           dom_sid_str_buf(objectsid, &sid_buf),
+                           GUID_buf_string(&cache->entries[idx], &guid_buf));
                        continue;
                }
-               entries[count].idx = samdb_result_rid_from_sid(mem_ctx, res[i],
-                                                              "objectSid", 0);
-               entries[count].name.string = ldb_msg_find_attr_as_string(res[i],
-                                                                "sAMAccountName", "");
-               count += 1;
-       }
-
-       /* sort the results by rid */
-       TYPESAFE_QSORT(entries, count, compare_SamEntry);
-
-       /* find the first entry to return */
-       for (first=0;
-            first<count && entries[first].idx <= *r->in.resume_handle;
-            first++) ;
 
-       /* return the rest, limit by max_size. Note that we
-          use the w2k3 element size value of 54 */
-       *r->out.num_entries = count - first;
-       *r->out.num_entries = MIN(*r->out.num_entries,
-                                1+(r->in.max_size/SAMR_ENUM_USERS_MULTIPLIER));
+               entries[count].idx = rid;
+               name = ldb_msg_find_attr_as_string(
+                   rec->msgs[0], "sAMAccountName", "");
+               entries[count].name.string = talloc_strdup(entries, name);
+               count++;
+       }
 
        sam = talloc(mem_ctx, struct samr_SamArray);
        if (!sam) {
+               clear_guid_cache(cache);
                return NT_STATUS_NO_MEMORY;
        }
 
-       sam->entries = entries+first;
-       sam->count = *r->out.num_entries;
+       sam->entries = entries;
+       sam->count = count;
 
        *r->out.sam = sam;
-
-       if (first == count) {
+       *r->out.resume_handle = resume_handle;
+       *r->out.num_entries = count;
+
+       /*
+        * Signal no more results by returning zero resume handle,
+        * the cache is also cleared at this point
+        */
+       if (*r->out.resume_handle >= cache->size) {
+               *r->out.resume_handle = 0;
+               clear_guid_cache(cache);
                return NT_STATUS_OK;
        }
-
-       if (*r->out.num_entries < count - first) {
-               *r->out.resume_handle = entries[first+*r->out.num_entries-1].idx;
-               return STATUS_MORE_ENTRIES;
-       }
-
-       return NT_STATUS_OK;
+       /*
+        * There are more results to be returned.
+        */
+       return STATUS_MORE_ENTRIES;
 }
 
 
@@ -1403,7 +1772,7 @@ static NTSTATUS dcesrv_samr_CreateDomAlias(struct dcesrv_call_state *dce_call, T
        a_state->account_name = talloc_steal(a_state, alias_name);
 
        /* create the policy handle */
-       a_handle = dcesrv_handle_new(dce_call->context, SAMR_HANDLE_ALIAS);
+       a_handle = dcesrv_handle_create(dce_call, SAMR_HANDLE_ALIAS);
        if (a_handle == NULL)
                return NT_STATUS_NO_MEMORY;
 
@@ -1524,7 +1893,7 @@ static NTSTATUS dcesrv_samr_GetAliasMembership(struct dcesrv_call_state *dce_cal
 {
        struct dcesrv_handle *h;
        struct samr_domain_state *d_state;
-       const char *filter;
+       char *filter;
        const char * const attrs[] = { "objectSid", NULL };
        struct ldb_message **res;
        uint32_t i;
@@ -1544,19 +1913,13 @@ static NTSTATUS dcesrv_samr_GetAliasMembership(struct dcesrv_call_state *dce_cal
        }
 
        for (i=0; i<r->in.sids->num_sids; i++) {
-               const char *memberdn;
-
-               memberdn = samdb_search_string(d_state->sam_ctx, mem_ctx, NULL,
-                                              "distinguishedName",
-                                              "(objectSid=%s)",
-                                              ldap_encode_ndr_dom_sid(mem_ctx,
-                                                                      r->in.sids->sids[i].sid));
-               if (memberdn == NULL) {
-                       continue;
-               }
+               struct dom_sid_buf buf;
+
+               filter = talloc_asprintf_append(
+                       filter,
+                       "(member=<SID=%s>)",
+                       dom_sid_str_buf(r->in.sids->sids[i].sid, &buf));
 
-               filter = talloc_asprintf(mem_ctx, "%s(member=%s)", filter,
-                                        memberdn);
                if (filter == NULL) {
                        return NT_STATUS_NO_MEMORY;
                }
@@ -1753,13 +2116,22 @@ static NTSTATUS dcesrv_samr_OpenGroup(struct dcesrv_call_state *dce_call, TALLOC
        }
 
        /* search for the group record */
-       ret = gendb_search(d_state->sam_ctx,
-                          mem_ctx, d_state->domain_dn, &msgs, attrs,
-                          "(&(objectSid=%s)(objectClass=group)"
-                          "(|(groupType=%d)(groupType=%d)))",
-                          ldap_encode_ndr_dom_sid(mem_ctx, sid),
-                          GTYPE_SECURITY_UNIVERSAL_GROUP,
-                          GTYPE_SECURITY_GLOBAL_GROUP);
+       if (d_state->builtin) {
+               ret = gendb_search(d_state->sam_ctx,
+                                  mem_ctx, d_state->domain_dn, &msgs, attrs,
+                                  "(&(objectSid=%s)(objectClass=group)"
+                                  "(groupType=%d))",
+                                  ldap_encode_ndr_dom_sid(mem_ctx, sid),
+                                  GTYPE_SECURITY_BUILTIN_LOCAL_GROUP);
+       } else {
+               ret = gendb_search(d_state->sam_ctx,
+                                  mem_ctx, d_state->domain_dn, &msgs, attrs,
+                                  "(&(objectSid=%s)(objectClass=group)"
+                                  "(|(groupType=%d)(groupType=%d)))",
+                                  ldap_encode_ndr_dom_sid(mem_ctx, sid),
+                                  GTYPE_SECURITY_UNIVERSAL_GROUP,
+                                  GTYPE_SECURITY_GLOBAL_GROUP);
+       }
        if (ret == 0) {
                return NT_STATUS_NO_SUCH_GROUP;
        }
@@ -1791,7 +2163,7 @@ static NTSTATUS dcesrv_samr_OpenGroup(struct dcesrv_call_state *dce_call, TALLOC
        }
 
        /* create the policy handle */
-       g_handle = dcesrv_handle_new(dce_call->context, SAMR_HANDLE_GROUP);
+       g_handle = dcesrv_handle_create(dce_call, SAMR_HANDLE_GROUP);
        if (!g_handle) {
                return NT_STATUS_NO_MEMORY;
        }
@@ -1883,13 +2255,11 @@ static NTSTATUS dcesrv_samr_SetGroupInfo(struct dcesrv_call_state *dce_call, TAL
        struct dcesrv_handle *h;
        struct samr_account_state *g_state;
        struct ldb_message *msg;
-       struct ldb_context *sam_ctx;
        int ret;
 
        DCESRV_PULL_HANDLE(h, r->in.group_handle, SAMR_HANDLE_GROUP);
 
        g_state = h->data;
-       sam_ctx = g_state->sam_ctx;
 
        msg = ldb_msg_new(mem_ctx);
        if (msg == NULL) {
@@ -1920,8 +2290,7 @@ static NTSTATUS dcesrv_samr_SetGroupInfo(struct dcesrv_call_state *dce_call, TAL
        /* modify the samdb record */
        ret = ldb_modify(g_state->sam_ctx, msg);
        if (ret != LDB_SUCCESS) {
-               /* we really need samdb.c to return NTSTATUS */
-               return NT_STATUS_UNSUCCESSFUL;
+               return dsdb_ldb_err_to_ntstatus(ret);
        }
 
        return NT_STATUS_OK;
@@ -1987,7 +2356,7 @@ static NTSTATUS dcesrv_samr_AddGroupMember(struct dcesrv_call_state *dce_call, T
        ret = samdb_msg_add_addval(d_state->sam_ctx, mem_ctx, mod, "member",
                                                                memberdn);
        if (ret != LDB_SUCCESS) {
-               return NT_STATUS_UNSUCCESSFUL;
+               return dsdb_ldb_err_to_ntstatus(ret);
        }
 
        ret = ldb_modify(a_state->sam_ctx, mod);
@@ -1999,7 +2368,7 @@ static NTSTATUS dcesrv_samr_AddGroupMember(struct dcesrv_call_state *dce_call, T
        case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
                return NT_STATUS_ACCESS_DENIED;
        default:
-               return NT_STATUS_UNSUCCESSFUL;
+               return dsdb_ldb_err_to_ntstatus(ret);
        }
 }
 
@@ -2022,7 +2391,7 @@ static NTSTATUS dcesrv_samr_DeleteDomainGroup(struct dcesrv_call_state *dce_call
 
        ret = ldb_delete(a_state->sam_ctx, a_state->account_dn);
        if (ret != LDB_SUCCESS) {
-               return NT_STATUS_UNSUCCESSFUL;
+               return dsdb_ldb_err_to_ntstatus(ret);
        }
 
        talloc_free(h);
@@ -2103,7 +2472,7 @@ static NTSTATUS dcesrv_samr_DeleteGroupMember(struct dcesrv_call_state *dce_call
        case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
                return NT_STATUS_ACCESS_DENIED;
        default:
-               return NT_STATUS_UNSUCCESSFUL;
+               return dsdb_ldb_err_to_ntstatus(ret);
        }
 }
 
@@ -2254,7 +2623,7 @@ static NTSTATUS dcesrv_samr_OpenAlias(struct dcesrv_call_state *dce_call, TALLOC
        }
 
        /* create the policy handle */
-       g_handle = dcesrv_handle_new(dce_call->context, SAMR_HANDLE_ALIAS);
+       g_handle = dcesrv_handle_create(dce_call, SAMR_HANDLE_ALIAS);
        if (!g_handle) {
                return NT_STATUS_NO_MEMORY;
        }
@@ -2336,13 +2705,11 @@ static NTSTATUS dcesrv_samr_SetAliasInfo(struct dcesrv_call_state *dce_call, TAL
        struct dcesrv_handle *h;
        struct samr_account_state *a_state;
        struct ldb_message *msg;
-       struct ldb_context *sam_ctx;
        int ret;
 
        DCESRV_PULL_HANDLE(h, r->in.alias_handle, SAMR_HANDLE_ALIAS);
 
        a_state = h->data;
-       sam_ctx = a_state->sam_ctx;
 
        msg = ldb_msg_new(mem_ctx);
        if (msg == NULL) {
@@ -2370,8 +2737,7 @@ static NTSTATUS dcesrv_samr_SetAliasInfo(struct dcesrv_call_state *dce_call, TAL
        /* modify the samdb record */
        ret = ldb_modify(a_state->sam_ctx, msg);
        if (ret != LDB_SUCCESS) {
-               /* we really need samdb.c to return NTSTATUS */
-               return NT_STATUS_UNSUCCESSFUL;
+               return dsdb_ldb_err_to_ntstatus(ret);
        }
 
        return NT_STATUS_OK;
@@ -2396,7 +2762,7 @@ static NTSTATUS dcesrv_samr_DeleteDomAlias(struct dcesrv_call_state *dce_call, T
 
        ret = ldb_delete(a_state->sam_ctx, a_state->account_dn);
        if (ret != LDB_SUCCESS) {
-               return NT_STATUS_UNSUCCESSFUL;
+               return dsdb_ldb_err_to_ntstatus(ret);
        }
 
        talloc_free(h);
@@ -2460,7 +2826,7 @@ static NTSTATUS dcesrv_samr_AddAliasMember(struct dcesrv_call_state *dce_call, T
        ret = samdb_msg_add_addval(d_state->sam_ctx, mem_ctx, mod, "member",
                                 ldb_dn_alloc_linearized(mem_ctx, memberdn));
        if (ret != LDB_SUCCESS) {
-               return NT_STATUS_UNSUCCESSFUL;
+               return dsdb_ldb_err_to_ntstatus(ret);
        }
 
        ret = ldb_modify(a_state->sam_ctx, mod);
@@ -2472,7 +2838,7 @@ static NTSTATUS dcesrv_samr_AddAliasMember(struct dcesrv_call_state *dce_call, T
        case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
                return NT_STATUS_ACCESS_DENIED;
        default:
-               return NT_STATUS_UNSUCCESSFUL;
+               return dsdb_ldb_err_to_ntstatus(ret);
        }
 }
 
@@ -2512,7 +2878,7 @@ static NTSTATUS dcesrv_samr_DeleteAliasMember(struct dcesrv_call_state *dce_call
        ret = samdb_msg_add_delval(d_state->sam_ctx, mem_ctx, mod, "member",
                                                                 memberdn);
        if (ret != LDB_SUCCESS) {
-               return NT_STATUS_UNSUCCESSFUL;
+               return dsdb_ldb_err_to_ntstatus(ret);
        }
 
        ret = ldb_modify(a_state->sam_ctx, mod);
@@ -2524,7 +2890,7 @@ static NTSTATUS dcesrv_samr_DeleteAliasMember(struct dcesrv_call_state *dce_call
        case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
                return NT_STATUS_ACCESS_DENIED;
        default:
-               return NT_STATUS_UNSUCCESSFUL;
+               return dsdb_ldb_err_to_ntstatus(ret);
        }
 }
 
@@ -2640,7 +3006,7 @@ static NTSTATUS dcesrv_samr_OpenUser(struct dcesrv_call_state *dce_call, TALLOC_
        }
 
        /* create the policy handle */
-       u_handle = dcesrv_handle_new(dce_call->context, SAMR_HANDLE_USER);
+       u_handle = dcesrv_handle_create(dce_call, SAMR_HANDLE_USER);
        if (!u_handle) {
                return NT_STATUS_NO_MEMORY;
        }
@@ -2675,7 +3041,7 @@ static NTSTATUS dcesrv_samr_DeleteUser(struct dcesrv_call_state *dce_call, TALLO
                DEBUG(1, ("Failed to delete user: %s: %s\n",
                          ldb_dn_get_linearized(a_state->account_dn),
                          ldb_errstring(a_state->sam_ctx)));
-               return NT_STATUS_UNSUCCESSFUL;
+               return dsdb_ldb_err_to_ntstatus(ret);
        }
 
        talloc_free(h);
@@ -2700,6 +3066,8 @@ static NTSTATUS dcesrv_samr_QueryUserInfo(struct dcesrv_call_state *dce_call, TA
        const char * const *attrs = NULL;
        union samr_UserInfo *info;
 
+       NTSTATUS status;
+
        *r->out.info = NULL;
 
        DCESRV_PULL_HANDLE(h, r->in.user_handle, SAMR_HANDLE_USER);
@@ -2713,7 +3081,7 @@ static NTSTATUS dcesrv_samr_QueryUserInfo(struct dcesrv_call_state *dce_call, TA
        {
                static const char * const attrs2[] = {"sAMAccountName",
                                                      "displayName",
-                                                     "primaryroupID",
+                                                     "primaryGroupID",
                                                      "description",
                                                      "comment",
                                                      NULL};
@@ -2743,10 +3111,13 @@ static NTSTATUS dcesrv_samr_QueryUserInfo(struct dcesrv_call_state *dce_call, TA
                                                      "lastLogon",
                                                      "lastLogoff",
                                                      "pwdLastSet",
+                                                     "msDS-UserPasswordExpiryTimeComputed",
                                                      "logonHours",
                                                      "badPwdCount",
+                                                     "badPasswordTime",
                                                      "logonCount",
                                                      "userAccountControl",
+                                                     "msDS-User-Account-Control-Computed",
                                                      NULL};
                attrs = attrs2;
                break;
@@ -2774,10 +3145,14 @@ static NTSTATUS dcesrv_samr_QueryUserInfo(struct dcesrv_call_state *dce_call, TA
                                                      "lastLogoff",
                                                      "logonHours",
                                                      "badPwdCount",
+                                                     "badPasswordTime",
                                                      "logonCount",
                                                      "pwdLastSet",
+                                                     "msDS-ResultantPSO",
+                                                     "msDS-UserPasswordExpiryTimeComputed",
                                                      "accountExpires",
                                                      "userAccountControl",
+                                                     "msDS-User-Account-Control-Computed",
                                                      NULL};
                attrs = attrs2;
                break;
@@ -2850,7 +3225,9 @@ static NTSTATUS dcesrv_samr_QueryUserInfo(struct dcesrv_call_state *dce_call, TA
        case 16:
        {
                static const char * const attrs2[] = {"userAccountControl",
+                                                     "msDS-User-Account-Control-Computed",
                                                      "pwdLastSet",
+                                                     "msDS-UserPasswordExpiryTimeComputed",
                                                      NULL};
                attrs = attrs2;
                break;
@@ -2878,6 +3255,8 @@ static NTSTATUS dcesrv_samr_QueryUserInfo(struct dcesrv_call_state *dce_call, TA
                static const char * const attrs2[] = {"lastLogon",
                                                      "lastLogoff",
                                                      "pwdLastSet",
+                                                     "msDS-ResultantPSO",
+                                                     "msDS-UserPasswordExpiryTimeComputed",
                                                      "accountExpires",
                                                      "sAMAccountName",
                                                      "displayName",
@@ -2892,8 +3271,10 @@ static NTSTATUS dcesrv_samr_QueryUserInfo(struct dcesrv_call_state *dce_call, TA
                                                      "objectSid",
                                                      "primaryGroupID",
                                                      "userAccountControl",
+                                                     "msDS-User-Account-Control-Computed",
                                                      "logonHours",
                                                      "badPwdCount",
+                                                     "badPasswordTime",
                                                      "logonCount",
                                                      "countryCode",
                                                      "codePage",
@@ -2961,11 +3342,12 @@ static NTSTATUS dcesrv_samr_QueryUserInfo(struct dcesrv_call_state *dce_call, TA
                QUERY_UINT64(msg, info3.last_logoff,           "lastLogoff");
                QUERY_UINT64(msg, info3.last_password_change,  "pwdLastSet");
                QUERY_APASSC(msg, info3.allow_password_change, "pwdLastSet");
-               QUERY_FPASSC(msg, info3.force_password_change, "pwdLastSet");
+               QUERY_UINT64(msg, info3.force_password_change, "msDS-UserPasswordExpiryTimeComputed");
                QUERY_LHOURS(msg, info3.logon_hours,           "logonHours");
+               /* level 3 gives the raw badPwdCount value */
                QUERY_UINT  (msg, info3.bad_password_count,    "badPwdCount");
                QUERY_UINT  (msg, info3.logon_count,           "logonCount");
-               QUERY_AFLAGS(msg, info3.acct_flags,            "userAccountControl");
+               QUERY_AFLAGS(msg, info3.acct_flags,            "msDS-User-Account-Control-Computed");
                break;
 
        case 4:
@@ -2986,11 +3368,11 @@ static NTSTATUS dcesrv_samr_QueryUserInfo(struct dcesrv_call_state *dce_call, TA
                QUERY_UINT64(msg, info5.last_logon,            "lastLogon");
                QUERY_UINT64(msg, info5.last_logoff,           "lastLogoff");
                QUERY_LHOURS(msg, info5.logon_hours,           "logonHours");
-               QUERY_UINT  (msg, info5.bad_password_count,    "badPwdCount");
+               QUERY_BPWDCT(msg, info5.bad_password_count,    "badPwdCount");
                QUERY_UINT  (msg, info5.logon_count,           "logonCount");
                QUERY_UINT64(msg, info5.last_password_change,  "pwdLastSet");
                QUERY_UINT64(msg, info5.acct_expiry,           "accountExpires");
-               QUERY_AFLAGS(msg, info5.acct_flags,            "userAccountControl");
+               QUERY_AFLAGS(msg, info5.acct_flags,            "msDS-User-Account-Control-Computed");
                break;
 
        case 6:
@@ -3032,7 +3414,7 @@ static NTSTATUS dcesrv_samr_QueryUserInfo(struct dcesrv_call_state *dce_call, TA
                break;
 
        case 16:
-               QUERY_AFLAGS(msg, info16.acct_flags,    "userAccountControl");
+               QUERY_AFLAGS(msg, info16.acct_flags,    "msDS-User-Account-Control-Computed");
                break;
 
        case 17:
@@ -3040,7 +3422,11 @@ static NTSTATUS dcesrv_samr_QueryUserInfo(struct dcesrv_call_state *dce_call, TA
                break;
 
        case 20:
-               QUERY_PARAMETERS(msg, info20.parameters,    "userParameters");
+               status = samdb_result_parameters(mem_ctx, msg, "userParameters", &info->info20.parameters);
+               if (!NT_STATUS_IS_OK(status)) {
+                       talloc_free(info);
+                       return status;
+               }
                break;
 
        case 21:
@@ -3049,7 +3435,7 @@ static NTSTATUS dcesrv_samr_QueryUserInfo(struct dcesrv_call_state *dce_call, TA
                QUERY_UINT64(msg, info21.last_password_change, "pwdLastSet");
                QUERY_UINT64(msg, info21.acct_expiry,          "accountExpires");
                QUERY_APASSC(msg, info21.allow_password_change,"pwdLastSet");
-               QUERY_FPASSC(msg, info21.force_password_change,"pwdLastSet");
+               QUERY_UINT64(msg, info21.force_password_change, "msDS-UserPasswordExpiryTimeComputed");
                QUERY_STRING(msg, info21.account_name,         "sAMAccountName");
                QUERY_STRING(msg, info21.full_name,            "displayName");
                QUERY_STRING(msg, info21.home_directory,       "homeDirectory");
@@ -3059,13 +3445,18 @@ static NTSTATUS dcesrv_samr_QueryUserInfo(struct dcesrv_call_state *dce_call, TA
                QUERY_STRING(msg, info21.description,          "description");
                QUERY_STRING(msg, info21.workstations,         "userWorkstations");
                QUERY_STRING(msg, info21.comment,              "comment");
-               QUERY_PARAMETERS(msg, info21.parameters,       "userParameters");
+               status = samdb_result_parameters(mem_ctx, msg, "userParameters", &info->info21.parameters);
+               if (!NT_STATUS_IS_OK(status)) {
+                       talloc_free(info);
+                       return status;
+               }
+
                QUERY_RID   (msg, info21.rid,                  "objectSid");
                QUERY_UINT  (msg, info21.primary_gid,          "primaryGroupID");
-               QUERY_AFLAGS(msg, info21.acct_flags,           "userAccountControl");
+               QUERY_AFLAGS(msg, info21.acct_flags,           "msDS-User-Account-Control-Computed");
                info->info21.fields_present = 0x08FFFFFF;
                QUERY_LHOURS(msg, info21.logon_hours,          "logonHours");
-               QUERY_UINT  (msg, info21.bad_password_count,   "badPwdCount");
+               QUERY_BPWDCT(msg, info21.bad_password_count,   "badPwdCount");
                QUERY_UINT  (msg, info21.logon_count,          "logonCount");
                if ((info->info21.acct_flags & ACB_PW_EXPIRED) != 0) {
                        info->info21.password_expired = PASS_MUST_CHANGE_AT_NEXT_LOGON;
@@ -3283,14 +3674,13 @@ static NTSTATUS dcesrv_samr_SetUserInfo(struct dcesrv_call_state *dce_call, TALL
 
 
                IFSET(SAMR_FIELD_EXPIRED_FLAG) {
-                       NTTIME t = 0;
+                       const char *t = "0";
                        struct ldb_message_element *set_el;
                        if (r->in.info->info21.password_expired
                                        == PASS_DONT_CHANGE_AT_NEXT_LOGON) {
-                               unix_to_nt_time(&t, time(NULL));
+                               t = "-1";
                        }
-                       if (samdb_msg_add_uint64(sam_ctx, mem_ctx, msg,
-                                                "pwdLastSet", t) != LDB_SUCCESS) {
+                       if (ldb_msg_add_string(msg, "pwdLastSet", t) != LDB_SUCCESS) {
                                return NT_STATUS_NO_MEMORY;
                        }
                        set_el = ldb_msg_find_element(msg, "pwdLastSet");
@@ -3370,14 +3760,13 @@ static NTSTATUS dcesrv_samr_SetUserInfo(struct dcesrv_call_state *dce_call, TALL
                }
 
                IFSET(SAMR_FIELD_EXPIRED_FLAG) {
-                       NTTIME t = 0;
+                       const char *t = "0";
                        struct ldb_message_element *set_el;
                        if (r->in.info->info23.info.password_expired
                                        == PASS_DONT_CHANGE_AT_NEXT_LOGON) {
-                               unix_to_nt_time(&t, time(NULL));
+                               t = "-1";
                        }
-                       if (samdb_msg_add_uint64(sam_ctx, mem_ctx, msg,
-                                                "pwdLastSet", t) != LDB_SUCCESS) {
+                       if (ldb_msg_add_string(msg, "pwdLastSet", t) != LDB_SUCCESS) {
                                return NT_STATUS_NO_MEMORY;
                        }
                        set_el = ldb_msg_find_element(msg, "pwdLastSet");
@@ -3478,14 +3867,13 @@ static NTSTATUS dcesrv_samr_SetUserInfo(struct dcesrv_call_state *dce_call, TALL
                }
 
                IFSET(SAMR_FIELD_EXPIRED_FLAG) {
-                       NTTIME t = 0;
+                       const char *t = "0";
                        struct ldb_message_element *set_el;
                        if (r->in.info->info25.info.password_expired
                                        == PASS_DONT_CHANGE_AT_NEXT_LOGON) {
-                               unix_to_nt_time(&t, time(NULL));
+                               t = "-1";
                        }
-                       if (samdb_msg_add_uint64(sam_ctx, mem_ctx, msg,
-                                                "pwdLastSet", t) != LDB_SUCCESS) {
+                       if (ldb_msg_add_string(msg, "pwdLastSet", t) != LDB_SUCCESS) {
                                return NT_STATUS_NO_MEMORY;
                        }
                        set_el = ldb_msg_find_element(msg, "pwdLastSet");
@@ -3507,8 +3895,13 @@ static NTSTATUS dcesrv_samr_SetUserInfo(struct dcesrv_call_state *dce_call, TALL
                }
 
                if (r->in.info->info26.password_expired > 0) {
+                       const char *t = "0";
                        struct ldb_message_element *set_el;
-                       if (samdb_msg_add_uint64(sam_ctx, mem_ctx, msg, "pwdLastSet", 0) != LDB_SUCCESS) {
+                       if (r->in.info->info26.password_expired
+                                       == PASS_DONT_CHANGE_AT_NEXT_LOGON) {
+                               t = "-1";
+                       }
+                       if (ldb_msg_add_string(msg, "pwdLastSet", t) != LDB_SUCCESS) {
                                return NT_STATUS_NO_MEMORY;
                        }
                        set_el = ldb_msg_find_element(msg, "pwdLastSet");
@@ -3533,8 +3926,7 @@ static NTSTATUS dcesrv_samr_SetUserInfo(struct dcesrv_call_state *dce_call, TALL
                                 ldb_dn_get_linearized(a_state->account_dn),
                                 ldb_errstring(a_state->sam_ctx)));
 
-                       /* we really need samdb.c to return NTSTATUS */
-                       return NT_STATUS_UNSUCCESSFUL;
+                       return dsdb_ldb_err_to_ntstatus(ret);
                }
        }
 
@@ -3551,25 +3943,93 @@ static NTSTATUS dcesrv_samr_GetGroupsForUser(struct dcesrv_call_state *dce_call,
        struct dcesrv_handle *h;
        struct samr_account_state *a_state;
        struct samr_domain_state *d_state;
-       struct ldb_message **res;
-       const char * const attrs[2] = { "objectSid", NULL };
+       struct ldb_result *res, *res_memberof;
+       const char * const attrs[] = { "primaryGroupID",
+                                      "memberOf",
+                                      NULL };
+       const char * const group_attrs[] = { "objectSid",
+                                            NULL };
+
        struct samr_RidWithAttributeArray *array;
-       int i, count;
+       struct ldb_message_element *memberof_el;
+       int i, ret, count = 0;
+       uint32_t primary_group_id;
+       char *filter;
 
        DCESRV_PULL_HANDLE(h, r->in.user_handle, SAMR_HANDLE_USER);
 
        a_state = h->data;
        d_state = a_state->domain_state;
 
-       count = samdb_search_domain(a_state->sam_ctx, mem_ctx,
-                                   d_state->domain_dn, &res,
-                                   attrs, d_state->domain_sid,
-                                   "(&(member=%s)(|(grouptype=%d)(grouptype=%d))(objectclass=group))",
-                                   ldb_dn_get_linearized(a_state->account_dn),
-                                   GTYPE_SECURITY_UNIVERSAL_GROUP,
-                                   GTYPE_SECURITY_GLOBAL_GROUP);
-       if (count < 0)
+       ret = dsdb_search_dn(a_state->sam_ctx, mem_ctx,
+                            &res,
+                            a_state->account_dn,
+                            attrs, DSDB_SEARCH_SHOW_EXTENDED_DN);
+
+       if (ret == LDB_ERR_NO_SUCH_OBJECT) {
+               return NT_STATUS_NO_SUCH_USER;
+       } else if (ret != LDB_SUCCESS) {
                return NT_STATUS_INTERNAL_DB_CORRUPTION;
+       } else if (res->count != 1) {
+               return NT_STATUS_NO_SUCH_USER;
+       }
+
+       primary_group_id = ldb_msg_find_attr_as_uint(res->msgs[0], "primaryGroupID",
+                                                    0);
+
+       filter = talloc_asprintf(mem_ctx,
+                                "(&(|(grouptype=%d)(grouptype=%d))"
+                                "(objectclass=group)(|",
+                                GTYPE_SECURITY_UNIVERSAL_GROUP,
+                                GTYPE_SECURITY_GLOBAL_GROUP);
+       if (filter == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       memberof_el = ldb_msg_find_element(res->msgs[0], "memberOf");
+       if (memberof_el != NULL) {
+               for (i = 0; i < memberof_el->num_values; i++) {
+                       const struct ldb_val *memberof_sid_binary;
+                       char *memberof_sid_escaped;
+                       struct ldb_dn *memberof_dn
+                               = ldb_dn_from_ldb_val(mem_ctx,
+                                                     a_state->sam_ctx,
+                                                     &memberof_el->values[i]);
+                       if (memberof_dn == NULL) {
+                               return NT_STATUS_INTERNAL_DB_CORRUPTION;
+                       }
+
+                       memberof_sid_binary
+                               = ldb_dn_get_extended_component(memberof_dn,
+                                                               "SID");
+                       if (memberof_sid_binary == NULL) {
+                               return NT_STATUS_INTERNAL_DB_CORRUPTION;
+                       }
+
+                       memberof_sid_escaped = ldb_binary_encode(mem_ctx,
+                                                                *memberof_sid_binary);
+                       if (memberof_sid_escaped == NULL) {
+                               return NT_STATUS_NO_MEMORY;
+                       }
+                       filter = talloc_asprintf_append(filter, "(objectSID=%s)",
+                                                       memberof_sid_escaped);
+                       if (filter == NULL) {
+                               return NT_STATUS_NO_MEMORY;
+                       }
+               }
+
+               ret = dsdb_search(a_state->sam_ctx, mem_ctx,
+                                 &res_memberof,
+                                 d_state->domain_dn,
+                                 LDB_SCOPE_SUBTREE,
+                                 group_attrs, 0,
+                                 "%s))", filter);
+
+               if (ret != LDB_SUCCESS) {
+                       return NT_STATUS_INTERNAL_DB_CORRUPTION;
+               }
+               count = res_memberof->count;
+       }
 
        array = talloc(mem_ctx, struct samr_RidWithAttributeArray);
        if (array == NULL)
@@ -3579,23 +4039,24 @@ static NTSTATUS dcesrv_samr_GetGroupsForUser(struct dcesrv_call_state *dce_call,
        array->rids = NULL;
 
        array->rids = talloc_array(mem_ctx, struct samr_RidWithAttribute,
-                                           count + 1);
+                                  count + 1);
        if (array->rids == NULL)
                return NT_STATUS_NO_MEMORY;
 
        /* Adds the primary group */
-       array->rids[0].rid = samdb_search_uint(a_state->sam_ctx, mem_ctx,
-                                              ~0, a_state->account_dn,
-                                              "primaryGroupID", NULL);
+
+       array->rids[0].rid = primary_group_id;
        array->rids[0].attributes = SE_GROUP_MANDATORY
-                       | SE_GROUP_ENABLED_BY_DEFAULT | SE_GROUP_ENABLED;
+               | SE_GROUP_ENABLED_BY_DEFAULT | SE_GROUP_ENABLED;
        array->count += 1;
 
        /* Adds the additional groups */
        for (i = 0; i < count; i++) {
                struct dom_sid *group_sid;
 
-               group_sid = samdb_result_dom_sid(mem_ctx, res[i], "objectSid");
+               group_sid = samdb_result_dom_sid(mem_ctx,
+                                                res_memberof->msgs[i],
+                                                "objectSid");
                if (group_sid == NULL) {
                        return NT_STATUS_INTERNAL_DB_CORRUPTION;
                }
@@ -3612,88 +4073,164 @@ static NTSTATUS dcesrv_samr_GetGroupsForUser(struct dcesrv_call_state *dce_call,
        return NT_STATUS_OK;
 }
 
-
 /*
-  samr_QueryDisplayInfo
-*/
+ * samr_QueryDisplayInfo
+ *
+ * A cache of the GUID's matching the last query is maintained
+ * in the SAMR_QUERY_DISPLAY_INFO_CACHE guid_cache maintained o
+ * n the dcesrv_handle.
+ */
 static NTSTATUS dcesrv_samr_QueryDisplayInfo(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
                       struct samr_QueryDisplayInfo *r)
 {
        struct dcesrv_handle *h;
        struct samr_domain_state *d_state;
-       struct ldb_message **res;
-       int i, ldb_cnt;
-       uint32_t count;
-       const char * const attrs[] = { "objectSid", "sAMAccountName",
-               "displayName", "description", "userAccountControl",
-               "pwdLastSet", NULL };
+       struct ldb_result *res;
+       uint32_t i;
+       uint32_t results = 0;
+       uint32_t count = 0;
+       const char *const cache_attrs[] = {"objectGUID", NULL};
+       const char *const attrs[] = {
+           "objectSID", "sAMAccountName", "displayName", "description", NULL};
        struct samr_DispEntryFull *entriesFull = NULL;
        struct samr_DispEntryFullGroup *entriesFullGroup = NULL;
        struct samr_DispEntryAscii *entriesAscii = NULL;
        struct samr_DispEntryGeneral *entriesGeneral = NULL;
        const char *filter;
+       int ret;
+       NTSTATUS status;
+       struct samr_guid_cache *cache = NULL;
 
        DCESRV_PULL_HANDLE(h, r->in.domain_handle, SAMR_HANDLE_DOMAIN);
 
        d_state = h->data;
 
-       switch (r->in.level) {
-       case 1:
-       case 4:
-               filter = talloc_asprintf(mem_ctx, "(&(objectclass=user)"
-                                        "(sAMAccountType=%u))",
-                                        ATYPE_NORMAL_ACCOUNT);
-               break;
-       case 2:
-               filter = talloc_asprintf(mem_ctx, "(&(objectclass=user)"
-                                        "(sAMAccountType=%u))",
-                                        ATYPE_WORKSTATION_TRUST);
-               break;
-       case 3:
-       case 5:
-               filter = talloc_asprintf(mem_ctx,
-                                        "(&(|(groupType=%d)(groupType=%d))"
-                                        "(objectClass=group))",
-                                        GTYPE_SECURITY_UNIVERSAL_GROUP,
-                                        GTYPE_SECURITY_GLOBAL_GROUP);
-               break;
-       default:
-               return NT_STATUS_INVALID_INFO_CLASS;
-       }
+       cache = &d_state->guid_caches[SAMR_QUERY_DISPLAY_INFO_CACHE];
+       /*
+        * Can the cached results be used?
+        * The cache is discarded if the start index is zero, or the requested
+        * level is different from that in the cache.
+        */
+       if ((r->in.start_idx == 0) || (r->in.level != cache->handle)) {
+               /*
+                * The cached results can not be used, so will need to query
+                * the database.
+                */
 
-       /* search for all requested objects in this domain. This could
-          possibly be cached and resumed based on resume_key */
-       ldb_cnt = samdb_search_domain(d_state->sam_ctx, mem_ctx,
-                                     d_state->domain_dn, &res, attrs,
-                                     d_state->domain_sid, "%s", filter);
-       if (ldb_cnt == -1) {
-               return NT_STATUS_INTERNAL_DB_CORRUPTION;
+               /*
+                * Get the search filter for the current level
+                */
+               switch (r->in.level) {
+               case 1:
+               case 4:
+                       filter = talloc_asprintf(mem_ctx,
+                                                "(&(objectclass=user)"
+                                                "(sAMAccountType=%d))",
+                                                ATYPE_NORMAL_ACCOUNT);
+                       break;
+               case 2:
+                       filter = talloc_asprintf(mem_ctx,
+                                                "(&(objectclass=user)"
+                                                "(sAMAccountType=%d))",
+                                                ATYPE_WORKSTATION_TRUST);
+                       break;
+               case 3:
+               case 5:
+                       filter =
+                           talloc_asprintf(mem_ctx,
+                                           "(&(|(groupType=%d)(groupType=%d))"
+                                           "(objectClass=group))",
+                                           GTYPE_SECURITY_UNIVERSAL_GROUP,
+                                           GTYPE_SECURITY_GLOBAL_GROUP);
+                       break;
+               default:
+                       return NT_STATUS_INVALID_INFO_CLASS;
+               }
+               clear_guid_cache(cache);
+
+               /*
+                * search for all requested objects in all domains.
+                */
+               ret = dsdb_search(d_state->sam_ctx,
+                                 mem_ctx,
+                                 &res,
+                                 ldb_get_default_basedn(d_state->sam_ctx),
+                                 LDB_SCOPE_SUBTREE,
+                                 cache_attrs,
+                                 0,
+                                 "%s",
+                                 filter);
+               if (ret != LDB_SUCCESS) {
+                       return NT_STATUS_INTERNAL_DB_CORRUPTION;
+               }
+               if ((res->count == 0) || (r->in.max_entries == 0)) {
+                       return NT_STATUS_OK;
+               }
+
+               status = load_guid_cache(cache, d_state, res->count, res->msgs);
+               TALLOC_FREE(res);
+               if (!NT_STATUS_IS_OK(status)) {
+                       return status;
+               }
+               cache->handle = r->in.level;
        }
-       if (ldb_cnt == 0 || r->in.max_entries == 0) {
+       *r->out.total_size = cache->size;
+
+       /*
+        * if there are no entries or the requested start index is greater
+        * than the number of entries, we return an empty response.
+        */
+       if (r->in.start_idx >= cache->size) {
+               *r->out.returned_size = 0;
+               switch(r->in.level) {
+               case 1:
+                       r->out.info->info1.count = *r->out.returned_size;
+                       r->out.info->info1.entries = NULL;
+                       break;
+               case 2:
+                       r->out.info->info2.count = *r->out.returned_size;
+                       r->out.info->info2.entries = NULL;
+                       break;
+               case 3:
+                       r->out.info->info3.count = *r->out.returned_size;
+                       r->out.info->info3.entries = NULL;
+                       break;
+               case 4:
+                       r->out.info->info4.count = *r->out.returned_size;
+                       r->out.info->info4.entries = NULL;
+                       break;
+               case 5:
+                       r->out.info->info5.count = *r->out.returned_size;
+                       r->out.info->info5.entries = NULL;
+                       break;
+               }
                return NT_STATUS_OK;
        }
 
+       /*
+        * Allocate an array of the appropriate result structures for the
+        * current query level.
+        *
+        * r->in.start_idx is always < cache->size due to the check above
+        */
+       results = MIN((cache->size - r->in.start_idx), r->in.max_entries);
        switch (r->in.level) {
        case 1:
-               entriesGeneral = talloc_array(mem_ctx,
-                                            struct samr_DispEntryGeneral,
-                                            ldb_cnt);
+               entriesGeneral = talloc_array(
+                   mem_ctx, struct samr_DispEntryGeneral, results);
                break;
        case 2:
-               entriesFull = talloc_array(mem_ctx,
-                                            struct samr_DispEntryFull,
-                                            ldb_cnt);
+               entriesFull =
+                   talloc_array(mem_ctx, struct samr_DispEntryFull, results);
                break;
        case 3:
-               entriesFullGroup = talloc_array(mem_ctx,
-                                            struct samr_DispEntryFullGroup,
-                                            ldb_cnt);
+               entriesFullGroup = talloc_array(
+                   mem_ctx, struct samr_DispEntryFullGroup, results);
                break;
        case 4:
        case 5:
-               entriesAscii = talloc_array(mem_ctx,
-                                             struct samr_DispEntryAscii,
-                                             ldb_cnt);
+               entriesAscii =
+                   talloc_array(mem_ctx, struct samr_DispEntryAscii, results);
                break;
        }
 
@@ -3701,134 +4238,168 @@ static NTSTATUS dcesrv_samr_QueryDisplayInfo(struct dcesrv_call_state *dce_call,
            (entriesAscii == NULL) && (entriesFullGroup == NULL))
                return NT_STATUS_NO_MEMORY;
 
+       /*
+        * Process the list of result GUID's.
+        * Read the details of each object and populate the result structure
+        * for the current level.
+        */
        count = 0;
-
-       for (i=0; i<ldb_cnt; i++) {
+       for (i = 0; i < results; i++) {
                struct dom_sid *objectsid;
+               struct ldb_result *rec;
+               const uint32_t idx = r->in.start_idx + i;
+               uint32_t rid;
 
-               objectsid = samdb_result_dom_sid(mem_ctx, res[i],
-                                                "objectSid");
-               if (objectsid == NULL)
+               /*
+                * Read an object from disk using the GUID as the key
+                *
+                * If the object can not be read, or it does not have a SID
+                * it is ignored.  In this case the number of entries returned
+                * will be less than the requested size, there will also be
+                * a gap in the idx numbers in the returned elements e.g. if
+                * there are 3 GUIDs a, b, c in the cache and b is deleted from
+                * disk then details for a, and c will be returned with
+                * idx values of 1 and 3 respectively.
+                *
+                */
+               ret = dsdb_search_by_dn_guid(d_state->sam_ctx,
+                                            mem_ctx,
+                                            &rec,
+                                            &cache->entries[idx],
+                                            attrs,
+                                            0);
+               if (ret == LDB_ERR_NO_SUCH_OBJECT) {
+                       struct GUID_txt_buf guid_buf;
+                       char *guid_str =
+                               GUID_buf_string(&cache->entries[idx],
+                                               &guid_buf);
+                       DBG_WARNING("GUID [%s] not found\n", guid_str);
+                       continue;
+               } else if (ret != LDB_SUCCESS) {
+                       clear_guid_cache(cache);
+                       return NT_STATUS_INTERNAL_DB_CORRUPTION;
+               }
+               objectsid = samdb_result_dom_sid(mem_ctx,
+                                                rec->msgs[0],
+                                                "objectSID");
+               if (objectsid == NULL) {
+                       struct GUID_txt_buf guid_buf;
+                       DBG_WARNING(
+                           "objectSID for GUID [%s] not found\n",
+                           GUID_buf_string(&cache->entries[idx], &guid_buf));
+                       continue;
+               }
+               status = dom_sid_split_rid(NULL,
+                                          objectsid,
+                                          NULL,
+                                          &rid);
+               if (!NT_STATUS_IS_OK(status)) {
+                       struct dom_sid_buf sid_buf;
+                       struct GUID_txt_buf guid_buf;
+                       DBG_WARNING(
+                           "objectSID [%s] for GUID [%s] invalid\n",
+                           dom_sid_str_buf(objectsid, &sid_buf),
+                           GUID_buf_string(&cache->entries[idx], &guid_buf));
                        continue;
+               }
 
+               /*
+                * Populate the result structure for the current object
+                */
                switch(r->in.level) {
                case 1:
-                       entriesGeneral[count].idx = count + 1;
-                       entriesGeneral[count].rid =
-                               objectsid->sub_auths[objectsid->num_auths-1];
+
+                       entriesGeneral[count].idx = idx + 1;
+                       entriesGeneral[count].rid = rid;
+
                        entriesGeneral[count].acct_flags =
-                               samdb_result_acct_flags(d_state->sam_ctx, mem_ctx,
-                                                       res[i],
-                                                       d_state->domain_dn);
+                           samdb_result_acct_flags(rec->msgs[0], NULL);
                        entriesGeneral[count].account_name.string =
-                               ldb_msg_find_attr_as_string(res[i],
-                                                   "sAMAccountName", "");
+                           ldb_msg_find_attr_as_string(
+                               rec->msgs[0], "sAMAccountName", "");
                        entriesGeneral[count].full_name.string =
-                               ldb_msg_find_attr_as_string(res[i], "displayName", "");
+                           ldb_msg_find_attr_as_string(
+                               rec->msgs[0], "displayName", "");
                        entriesGeneral[count].description.string =
-                               ldb_msg_find_attr_as_string(res[i], "description", "");
+                           ldb_msg_find_attr_as_string(
+                               rec->msgs[0], "description", "");
                        break;
                case 2:
-                       entriesFull[count].idx = count + 1;
-                       entriesFull[count].rid =
-                               objectsid->sub_auths[objectsid->num_auths-1];
+                       entriesFull[count].idx = idx + 1;
+                       entriesFull[count].rid = rid;
 
-                       /* No idea why we need to or in ACB_NORMAL here, but this is what Win2k3 seems to do... */
+                       /*
+                        * No idea why we need to or in ACB_NORMAL here,
+                        * but this is what Win2k3 seems to do...
+                        */
                        entriesFull[count].acct_flags =
-                               samdb_result_acct_flags(d_state->sam_ctx, mem_ctx,
-                                                       res[i],
-                                                       d_state->domain_dn) | ACB_NORMAL;
+                           samdb_result_acct_flags(rec->msgs[0], NULL) |
+                           ACB_NORMAL;
                        entriesFull[count].account_name.string =
-                               ldb_msg_find_attr_as_string(res[i], "sAMAccountName",
-                                                   "");
+                           ldb_msg_find_attr_as_string(
+                               rec->msgs[0], "sAMAccountName", "");
                        entriesFull[count].description.string =
-                               ldb_msg_find_attr_as_string(res[i], "description", "");
+                           ldb_msg_find_attr_as_string(
+                               rec->msgs[0], "description", "");
                        break;
                case 3:
-                       entriesFullGroup[count].idx = count + 1;
-                       entriesFullGroup[count].rid =
-                               objectsid->sub_auths[objectsid->num_auths-1];
-                       /* We get a "7" here for groups */
-                       entriesFullGroup[count].acct_flags
-                               = SE_GROUP_MANDATORY | SE_GROUP_ENABLED_BY_DEFAULT | SE_GROUP_ENABLED;
+                       entriesFullGroup[count].idx = idx + 1;
+                       entriesFullGroup[count].rid = rid;
+
+                       /*
+                        * We get a "7" here for groups
+                        */
+                       entriesFullGroup[count].acct_flags =
+                           SE_GROUP_MANDATORY | SE_GROUP_ENABLED_BY_DEFAULT |
+                           SE_GROUP_ENABLED;
                        entriesFullGroup[count].account_name.string =
-                               ldb_msg_find_attr_as_string(res[i], "sAMAccountName",
-                                                   "");
+                           ldb_msg_find_attr_as_string(
+                               rec->msgs[0], "sAMAccountName", "");
                        entriesFullGroup[count].description.string =
-                               ldb_msg_find_attr_as_string(res[i], "description", "");
+                           ldb_msg_find_attr_as_string(
+                               rec->msgs[0], "description", "");
                        break;
                case 4:
                case 5:
-                       entriesAscii[count].idx = count + 1;
+                       entriesAscii[count].idx = idx + 1;
                        entriesAscii[count].account_name.string =
-                               ldb_msg_find_attr_as_string(res[i], "sAMAccountName",
-                                                   "");
+                           ldb_msg_find_attr_as_string(
+                               rec->msgs[0], "sAMAccountName", "");
                        break;
                }
-
-               count += 1;
+               count++;
        }
 
-       *r->out.total_size = count;
-
-       if (r->in.start_idx >= count) {
-               *r->out.returned_size = 0;
-               switch(r->in.level) {
-               case 1:
-                       r->out.info->info1.count = *r->out.returned_size;
-                       r->out.info->info1.entries = NULL;
-                       break;
-               case 2:
-                       r->out.info->info2.count = *r->out.returned_size;
-                       r->out.info->info2.entries = NULL;
-                       break;
-               case 3:
-                       r->out.info->info3.count = *r->out.returned_size;
-                       r->out.info->info3.entries = NULL;
-                       break;
-               case 4:
-                       r->out.info->info4.count = *r->out.returned_size;
-                       r->out.info->info4.entries = NULL;
-                       break;
-               case 5:
-                       r->out.info->info5.count = *r->out.returned_size;
-                       r->out.info->info5.entries = NULL;
-                       break;
-               }
-       } else {
-               *r->out.returned_size = MIN(count - r->in.start_idx,
-                                          r->in.max_entries);
-               switch(r->in.level) {
-               case 1:
-                       r->out.info->info1.count = *r->out.returned_size;
-                       r->out.info->info1.entries =
-                               &(entriesGeneral[r->in.start_idx]);
-                       break;
-               case 2:
-                       r->out.info->info2.count = *r->out.returned_size;
-                       r->out.info->info2.entries =
-                               &(entriesFull[r->in.start_idx]);
-                       break;
-               case 3:
-                       r->out.info->info3.count = *r->out.returned_size;
-                       r->out.info->info3.entries =
-                               &(entriesFullGroup[r->in.start_idx]);
-                       break;
-               case 4:
-                       r->out.info->info4.count = *r->out.returned_size;
-                       r->out.info->info4.entries =
-                               &(entriesAscii[r->in.start_idx]);
-                       break;
-               case 5:
-                       r->out.info->info5.count = *r->out.returned_size;
-                       r->out.info->info5.entries =
-                               &(entriesAscii[r->in.start_idx]);
-                       break;
-               }
+       /*
+        * Build the response based on the request level.
+        */
+       *r->out.returned_size = count;
+       switch(r->in.level) {
+       case 1:
+               r->out.info->info1.count = count;
+               r->out.info->info1.entries = entriesGeneral;
+               break;
+       case 2:
+               r->out.info->info2.count = count;
+               r->out.info->info2.entries = entriesFull;
+               break;
+       case 3:
+               r->out.info->info3.count = count;
+               r->out.info->info3.entries = entriesFullGroup;
+               break;
+       case 4:
+               r->out.info->info4.count = count;
+               r->out.info->info4.entries = entriesAscii;
+               break;
+       case 5:
+               r->out.info->info5.count = count;
+               r->out.info->info5.entries = entriesAscii;
+               break;
        }
 
-       return (*r->out.returned_size < (count - r->in.start_idx)) ?
-               STATUS_MORE_ENTRIES : NT_STATUS_OK;
+       return ((r->in.start_idx + results) < cache->size)
+                  ? STATUS_MORE_ENTRIES
+                  : NT_STATUS_OK;
 }
 
 
@@ -3927,6 +4498,7 @@ static NTSTATUS dcesrv_samr_RemoveMemberFromForeignDomain(struct dcesrv_call_sta
 
        for (i=0; i<count; i++) {
                struct ldb_message *mod;
+               int ret;
 
                mod = ldb_msg_new(mem_ctx);
                if (mod == NULL) {
@@ -3939,10 +4511,11 @@ static NTSTATUS dcesrv_samr_RemoveMemberFromForeignDomain(struct dcesrv_call_sta
                                         "member", memberdn) != LDB_SUCCESS)
                        return NT_STATUS_NO_MEMORY;
 
-               if (ldb_modify(d_state->sam_ctx, mod) != LDB_SUCCESS)
-                       return NT_STATUS_UNSUCCESSFUL;
-
+               ret = ldb_modify(d_state->sam_ctx, mod);
                talloc_free(mod);
+               if (ret != LDB_SUCCESS) {
+                       return dsdb_ldb_err_to_ntstatus(ret);
+               }
        }
 
        return NT_STATUS_OK;
@@ -3960,10 +4533,11 @@ static NTSTATUS dcesrv_samr_QueryDomainInfo2(struct dcesrv_call_state *dce_call,
        struct samr_QueryDomainInfo r1;
        NTSTATUS status;
 
-       ZERO_STRUCT(r1.out);
-       r1.in.domain_handle = r->in.domain_handle;
-       r1.in.level  = r->in.level;
-       r1.out.info  = r->out.info;
+       r1 = (struct samr_QueryDomainInfo) {
+               .in.domain_handle = r->in.domain_handle,
+               .in.level  = r->in.level,
+               .out.info  = r->out.info,
+       };
 
        status = dcesrv_samr_QueryDomainInfo(dce_call, mem_ctx, &r1);
 
@@ -3982,9 +4556,11 @@ static NTSTATUS dcesrv_samr_QueryUserInfo2(struct dcesrv_call_state *dce_call, T
        struct samr_QueryUserInfo r1;
        NTSTATUS status;
 
-       r1.in.user_handle = r->in.user_handle;
-       r1.in.level  = r->in.level;
-       r1.out.info  = r->out.info;
+       r1 = (struct samr_QueryUserInfo) {
+               .in.user_handle = r->in.user_handle,
+               .in.level  = r->in.level,
+               .out.info  = r->out.info
+       };
 
        status = dcesrv_samr_QueryUserInfo(dce_call, mem_ctx, &r1);
 
@@ -4001,14 +4577,16 @@ static NTSTATUS dcesrv_samr_QueryDisplayInfo2(struct dcesrv_call_state *dce_call
        struct samr_QueryDisplayInfo q;
        NTSTATUS result;
 
-       q.in.domain_handle = r->in.domain_handle;
-       q.in.level = r->in.level;
-       q.in.start_idx = r->in.start_idx;
-       q.in.max_entries = r->in.max_entries;
-       q.in.buf_size = r->in.buf_size;
-       q.out.total_size = r->out.total_size;
-       q.out.returned_size = r->out.returned_size;
-       q.out.info = r->out.info;
+       q = (struct samr_QueryDisplayInfo) {
+               .in.domain_handle = r->in.domain_handle,
+               .in.level = r->in.level,
+               .in.start_idx = r->in.start_idx,
+               .in.max_entries = r->in.max_entries,
+               .in.buf_size = r->in.buf_size,
+               .out.total_size = r->out.total_size,
+               .out.returned_size = r->out.returned_size,
+               .out.info = r->out.info,
+       };
 
        result = dcesrv_samr_QueryDisplayInfo(dce_call, mem_ctx, &q);
 
@@ -4035,14 +4613,16 @@ static NTSTATUS dcesrv_samr_QueryDisplayInfo3(struct dcesrv_call_state *dce_call
        struct samr_QueryDisplayInfo q;
        NTSTATUS result;
 
-       q.in.domain_handle = r->in.domain_handle;
-       q.in.level = r->in.level;
-       q.in.start_idx = r->in.start_idx;
-       q.in.max_entries = r->in.max_entries;
-       q.in.buf_size = r->in.buf_size;
-       q.out.total_size = r->out.total_size;
-       q.out.returned_size = r->out.returned_size;
-       q.out.info = r->out.info;
+       q = (struct samr_QueryDisplayInfo) {
+               .in.domain_handle = r->in.domain_handle,
+               .in.level = r->in.level,
+               .in.start_idx = r->in.start_idx,
+               .in.max_entries = r->in.max_entries,
+               .in.buf_size = r->in.buf_size,
+               .out.total_size = r->out.total_size,
+               .out.returned_size = r->out.returned_size,
+               .out.info = r->out.info,
+       };
 
        result = dcesrv_samr_QueryDisplayInfo(dce_call, mem_ctx, &q);
 
@@ -4081,6 +4661,8 @@ static NTSTATUS dcesrv_samr_RemoveMultipleMembersFromAlias(struct dcesrv_call_st
 static NTSTATUS dcesrv_samr_GetDomPwInfo(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
                                  struct samr_GetDomPwInfo *r)
 {
+       struct auth_session_info *session_info =
+               dcesrv_call_session_info(dce_call);
        struct ldb_message **msgs;
        int ret;
        const char * const attrs[] = {"minPwdLength", "pwdProperties", NULL };
@@ -4088,9 +4670,12 @@ static NTSTATUS dcesrv_samr_GetDomPwInfo(struct dcesrv_call_state *dce_call, TAL
 
        ZERO_STRUCTP(r->out.info);
 
-       sam_ctx = samdb_connect(mem_ctx, dce_call->event_ctx,
-                                        dce_call->conn->dce_ctx->lp_ctx,
-                                        dce_call->conn->auth_state.session_info, 0);
+       sam_ctx = samdb_connect(mem_ctx,
+                               dce_call->event_ctx,
+                               dce_call->conn->dce_ctx->lp_ctx,
+                               session_info,
+                               dce_call->conn->remote_address,
+                               0);
        if (sam_ctx == NULL) {
                return NT_STATUS_INVALID_SYSTEM_SERVICE;
        }
@@ -4130,9 +4715,11 @@ static NTSTATUS dcesrv_samr_Connect2(struct dcesrv_call_state *dce_call, TALLOC_
 {
        struct samr_Connect c;
 
-       c.in.system_name = NULL;
-       c.in.access_mask = r->in.access_mask;
-       c.out.connect_handle = r->out.connect_handle;
+       c = (struct samr_Connect) {
+               .in.system_name = NULL,
+               .in.access_mask = r->in.access_mask,
+               .out.connect_handle = r->out.connect_handle,
+       };
 
        return dcesrv_samr_Connect(dce_call, mem_ctx, &c);
 }
@@ -4148,9 +4735,11 @@ static NTSTATUS dcesrv_samr_SetUserInfo2(struct dcesrv_call_state *dce_call, TAL
 {
        struct samr_SetUserInfo r2;
 
-       r2.in.user_handle = r->in.user_handle;
-       r2.in.level = r->in.level;
-       r2.in.info = r->in.info;
+       r2 = (struct samr_SetUserInfo) {
+               .in.user_handle = r->in.user_handle,
+               .in.level = r->in.level,
+               .in.info = r->in.info,
+       };
 
        return dcesrv_samr_SetUserInfo(dce_call, mem_ctx, &r2);
 }
@@ -4172,7 +4761,8 @@ static NTSTATUS dcesrv_samr_SetBootKeyInformation(struct dcesrv_call_state *dce_
 static NTSTATUS dcesrv_samr_GetBootKeyInformation(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
                       struct samr_GetBootKeyInformation *r)
 {
-       DCESRV_FAULT(DCERPC_FAULT_OP_RNG_ERROR);
+       /* Windows Server 2008 returns this */
+       return NT_STATUS_NOT_SUPPORTED;
 }
 
 
@@ -4184,9 +4774,11 @@ static NTSTATUS dcesrv_samr_Connect3(struct dcesrv_call_state *dce_call, TALLOC_
 {
        struct samr_Connect c;
 
-       c.in.system_name = NULL;
-       c.in.access_mask = r->in.access_mask;
-       c.out.connect_handle = r->out.connect_handle;
+       c = (struct samr_Connect) {
+               .in.system_name = NULL,
+               .in.access_mask = r->in.access_mask,
+               .out.connect_handle = r->out.connect_handle,
+       };
 
        return dcesrv_samr_Connect(dce_call, mem_ctx, &c);
 }
@@ -4200,9 +4792,11 @@ static NTSTATUS dcesrv_samr_Connect4(struct dcesrv_call_state *dce_call, TALLOC_
 {
        struct samr_Connect c;
 
-       c.in.system_name = NULL;
-       c.in.access_mask = r->in.access_mask;
-       c.out.connect_handle = r->out.connect_handle;
+       c = (struct samr_Connect) {
+               .in.system_name = NULL,
+               .in.access_mask = r->in.access_mask,
+               .out.connect_handle = r->out.connect_handle,
+       };
 
        return dcesrv_samr_Connect(dce_call, mem_ctx, &c);
 }
@@ -4217,9 +4811,11 @@ static NTSTATUS dcesrv_samr_Connect5(struct dcesrv_call_state *dce_call, TALLOC_
        struct samr_Connect c;
        NTSTATUS status;
 
-       c.in.system_name = NULL;
-       c.in.access_mask = r->in.access_mask;
-       c.out.connect_handle = r->out.connect_handle;
+       c = (struct samr_Connect) {
+               .in.system_name = NULL,
+               .in.access_mask = r->in.access_mask,
+               .out.connect_handle = r->out.connect_handle,
+       };
 
        status = dcesrv_samr_Connect(dce_call, mem_ctx, &c);
 
@@ -4279,11 +4875,26 @@ static NTSTATUS dcesrv_samr_ValidatePassword(struct dcesrv_call_state *dce_call,
        DATA_BLOB password;
        enum samr_ValidationStatus res;
        NTSTATUS status;
+       enum dcerpc_transport_t transport =
+               dcerpc_binding_get_transport(dce_call->conn->endpoint->ep_description);
+       enum dcerpc_AuthLevel auth_level = DCERPC_AUTH_LEVEL_NONE;
+
+       if (transport != NCACN_IP_TCP && transport != NCALRPC) {
+               DCESRV_FAULT(DCERPC_FAULT_ACCESS_DENIED);
+       }
+
+       dcesrv_call_auth_info(dce_call, NULL, &auth_level);
+       if (auth_level != DCERPC_AUTH_LEVEL_PRIVACY) {
+               DCESRV_FAULT(DCERPC_FAULT_ACCESS_DENIED);
+       }
 
        (*r->out.rep) = talloc_zero(mem_ctx, union samr_ValidatePasswordRep);
 
-       r2.in.domain_name = NULL;
-       r2.out.info = &pwInfo;
+       r2 = (struct samr_GetDomPwInfo) {
+               .in.domain_name = NULL,
+               .out.info = &pwInfo,
+       };
+
        status = dcesrv_samr_GetDomPwInfo(dce_call, mem_ctx, &r2);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
@@ -4297,7 +4908,9 @@ static NTSTATUS dcesrv_samr_ValidatePassword(struct dcesrv_call_state *dce_call,
        case NetValidatePasswordChange:
                password = data_blob_const(r->in.req->req2.password.string,
                                           r->in.req->req2.password.length);
-               res = samdb_check_password(&password,
+               res = samdb_check_password(mem_ctx,
+                                          dce_call->conn->dce_ctx->lp_ctx,
+                                          &password,
                                           pwInfo.password_properties,
                                           pwInfo.min_password_length);
                (*r->out.rep)->ctr2.status = res;
@@ -4305,7 +4918,9 @@ static NTSTATUS dcesrv_samr_ValidatePassword(struct dcesrv_call_state *dce_call,
        case NetValidatePasswordReset:
                password = data_blob_const(r->in.req->req3.password.string,
                                           r->in.req->req3.password.length);
-               res = samdb_check_password(&password,
+               res = samdb_check_password(mem_ctx,
+                                          dce_call->conn->dce_ctx->lp_ctx,
+                                          &password,
                                           pwInfo.password_properties,
                                           pwInfo.min_password_length);
                (*r->out.rep)->ctr3.status = res;