s4:rpc_server: only use context within op_bind() hooks and dcesrv_interface_bind_...
[samba.git] / source4 / rpc_server / samr / dcesrv_samr.c
index c7d692a1b56f32cf7b5d40ce98888322f66fd210..0ddbaae835d4230b147314c7c000248712950eea 100644 (file)
@@ -46,7 +46,8 @@
 static NTSTATUS dcesrv_interface_samr_bind(struct dcesrv_call_state *dce_call,
                                             const struct dcesrv_interface *iface)
 {
-       return dcesrv_interface_bind_reject_connect(dce_call, 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 */
@@ -145,7 +146,62 @@ static NTSTATUS dcesrv_interface_samr_bind(struct dcesrv_call_state *dce_call,
        }                                                               \
 } 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
@@ -155,6 +211,8 @@ static NTSTATUS dcesrv_interface_samr_bind(struct dcesrv_call_state *dce_call,
 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;
 
@@ -166,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;
@@ -379,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);
 
@@ -430,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;
@@ -1038,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;
        }
@@ -1060,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
 */
@@ -1069,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;
@@ -1082,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;
 }
 
 
@@ -1216,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;
        }
@@ -1266,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;
@@ -1280,73 +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(res[i], NULL) & 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;
 }
 
 
@@ -1401,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;
 
@@ -1527,7 +1898,6 @@ static NTSTATUS dcesrv_samr_GetAliasMembership(struct dcesrv_call_state *dce_cal
        struct ldb_message **res;
        uint32_t i;
        int count = 0;
-       char membersidstr[DOM_SID_STR_BUFLEN];
 
        DCESRV_PULL_HANDLE(h, r->in.domain_handle, SAMR_HANDLE_DOMAIN);
 
@@ -1543,11 +1913,13 @@ static NTSTATUS dcesrv_samr_GetAliasMembership(struct dcesrv_call_state *dce_cal
        }
 
        for (i=0; i<r->in.sids->num_sids; i++) {
-               dom_sid_string_buf(r->in.sids->sids[i].sid,
-                                  membersidstr, sizeof(membersidstr));
+               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_append(filter, "(member=<SID=%s>)",
-                                               membersidstr);
                if (filter == NULL) {
                        return NT_STATUS_NO_MEMORY;
                }
@@ -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;
        }
@@ -2251,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;
        }
@@ -2634,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;
        }
@@ -2776,6 +3148,7 @@ static NTSTATUS dcesrv_samr_QueryUserInfo(struct dcesrv_call_state *dce_call, TA
                                                      "badPasswordTime",
                                                      "logonCount",
                                                      "pwdLastSet",
+                                                     "msDS-ResultantPSO",
                                                      "msDS-UserPasswordExpiryTimeComputed",
                                                      "accountExpires",
                                                      "userAccountControl",
@@ -2882,6 +3255,7 @@ 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",
@@ -3699,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_result *res;
-       unsigned int i;
-       uint32_t count;
-       const char * const attrs[] = { "objectSid", "sAMAccountName",
-               "displayName", "description", "userAccountControl",
-               "pwdLastSet", NULL };
+       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=%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;
-       }
+       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 all domains. This could
-          possibly be cached and resumed based on resume_key */
-       ret = dsdb_search(d_state->sam_ctx, mem_ctx, &res, ldb_get_default_basedn(d_state->sam_ctx),
-                         LDB_SCOPE_SUBTREE, attrs, 0, "%s", filter);
-       if (ret != LDB_SUCCESS) {
-               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 ((res->count == 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,
-                                             res->count);
+               entriesGeneral = talloc_array(
+                   mem_ctx, struct samr_DispEntryGeneral, results);
                break;
        case 2:
-               entriesFull = talloc_array(mem_ctx,
-                                          struct samr_DispEntryFull,
-                                          res->count);
+               entriesFull =
+                   talloc_array(mem_ctx, struct samr_DispEntryFull, results);
                break;
        case 3:
-               entriesFullGroup = talloc_array(mem_ctx,
-                                               struct samr_DispEntryFullGroup,
-                                               res->count);
+               entriesFullGroup = talloc_array(
+                   mem_ctx, struct samr_DispEntryFullGroup, results);
                break;
        case 4:
        case 5:
-               entriesAscii = talloc_array(mem_ctx,
-                                           struct samr_DispEntryAscii,
-                                           res->count);
+               entriesAscii =
+                   talloc_array(mem_ctx, struct samr_DispEntryAscii, results);
                break;
        }
 
@@ -3788,135 +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 < res->count; 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->msgs[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(res->msgs[i], NULL);
+                           samdb_result_acct_flags(rec->msgs[0], NULL);
                        entriesGeneral[count].account_name.string =
-                               ldb_msg_find_attr_as_string(res->msgs[i],
-                                                           "sAMAccountName", "");
+                           ldb_msg_find_attr_as_string(
+                               rec->msgs[0], "sAMAccountName", "");
                        entriesGeneral[count].full_name.string =
-                               ldb_msg_find_attr_as_string(res->msgs[i],
-                                                           "displayName", "");
+                           ldb_msg_find_attr_as_string(
+                               rec->msgs[0], "displayName", "");
                        entriesGeneral[count].description.string =
-                               ldb_msg_find_attr_as_string(res->msgs[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(res->msgs[i],
-                                                       NULL) | ACB_NORMAL;
+                           samdb_result_acct_flags(rec->msgs[0], NULL) |
+                           ACB_NORMAL;
                        entriesFull[count].account_name.string =
-                               ldb_msg_find_attr_as_string(res->msgs[i],
-                                                           "sAMAccountName", "");
+                           ldb_msg_find_attr_as_string(
+                               rec->msgs[0], "sAMAccountName", "");
                        entriesFull[count].description.string =
-                               ldb_msg_find_attr_as_string(res->msgs[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->msgs[i],
-                                                           "sAMAccountName", "");
+                           ldb_msg_find_attr_as_string(
+                               rec->msgs[0], "sAMAccountName", "");
                        entriesFullGroup[count].description.string =
-                               ldb_msg_find_attr_as_string(res->msgs[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->msgs[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;
 }
 
 
@@ -4050,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);
 
@@ -4093,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);
 
@@ -4127,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);
 
@@ -4173,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 };
@@ -4180,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;
        }
@@ -4222,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);
 }
@@ -4240,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);
 }
@@ -4277,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);
 }
@@ -4293,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);
 }
@@ -4310,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);
 
@@ -4374,19 +4877,24 @@ static NTSTATUS dcesrv_samr_ValidatePassword(struct dcesrv_call_state *dce_call,
        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);
        }
 
-       if (dce_call->conn->auth_state.auth_level != DCERPC_AUTH_LEVEL_PRIVACY) {
+       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;