s4:rpc_server: only use context within op_bind() hooks and dcesrv_interface_bind_...
[samba.git] / source4 / rpc_server / samr / dcesrv_samr.c
index 58be23a0a2794bbce89bf5d501f0005f6e143e52..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 */
@@ -210,6 +211,8 @@ exit:
 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;
 
@@ -224,7 +227,7 @@ static NTSTATUS dcesrv_samr_Connect(struct dcesrv_call_state *dce_call, TALLOC_C
        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,
+                                        session_info,
                                         dce_call->conn->remote_address,
                                         0);
        if (c_state->sam_ctx == NULL) {
@@ -233,7 +236,7 @@ static NTSTATUS dcesrv_samr_Connect(struct dcesrv_call_state *dce_call, TALLOC_C
        }
 
 
-       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;
@@ -495,7 +498,7 @@ static NTSTATUS dcesrv_samr_OpenDomain(struct dcesrv_call_state *dce_call, TALLO
                initialize_guid_cache(&d_state->guid_caches[i]);
        }
 
-       h_domain = dcesrv_handle_new(dce_call->context, SAMR_HANDLE_DOMAIN);
+       h_domain = dcesrv_handle_create(dce_call, SAMR_HANDLE_DOMAIN);
        if (!h_domain) {
                talloc_free(d_state);
                return NT_STATUS_NO_MEMORY;
@@ -1103,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;
        }
@@ -1125,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
 */
@@ -1134,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;
@@ -1147,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];
 
-       /* 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;
-       }
+       /*
+        * 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);
 
-       /* convert to SamEntry format */
-       entries = talloc_array(mem_ctx, struct samr_SamEntry, ldb_cnt);
-       if (!entries) {
-               return NT_STATUS_NO_MEMORY;
+               /*
+                * 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;
        }
 
-       count = 0;
 
-       for (i=0;i<ldb_cnt;i++) {
-               struct dom_sid *group_sid;
-
-               group_sid = samdb_result_dom_sid(mem_ctx, res[i],
-                                                "objectSid");
-               if (group_sid == NULL) {
-                       return NT_STATUS_INTERNAL_DB_CORRUPTION;
+       /*
+        * 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;
 
-               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;
+               *r->out.sam = sam;
+               *r->out.resume_handle = 0;
+               return NT_STATUS_OK;
        }
 
-       /* 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++) ;
+       /*
+        * 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);
 
-       /* 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));
+       /*
+        * 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;
+               }
+
+               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;
+               }
+
+               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;
+       *r->out.resume_handle = resume_handle;
+       *r->out.num_entries = count;
 
-       if (first == 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;
 }
 
 
@@ -1281,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;
        }
@@ -1331,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;
@@ -1345,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];
 
-       /* 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;
+       /*
+        * 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);
+
+               /*
+                * 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;
        }
 
-       /* convert to SamEntry format */
-       entries = talloc_array(mem_ctx, struct samr_SamEntry, ldb_cnt);
-       if (!entries) {
-               return NT_STATUS_NO_MEMORY;
+       /*
+        * 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;
+
+               *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;
+       *r->out.resume_handle = resume_handle;
+       *r->out.num_entries = count;
 
-       if (first == 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;
 }
 
 
@@ -1466,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;
 
@@ -1857,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;
        }
@@ -2317,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;
        }
@@ -2700,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;
        }
@@ -3978,11 +4284,9 @@ static NTSTATUS dcesrv_samr_QueryDisplayInfo(struct dcesrv_call_state *dce_call,
                                                 "objectSID");
                if (objectsid == NULL) {
                        struct GUID_txt_buf guid_buf;
-                       char *guid_str =
-                               GUID_buf_string(&cache->entries[idx],
-                                               &guid_buf);
-                       DBG_WARNING("objectSID for GUID [%s] not found\n",
-                                   guid_str);
+                       DBG_WARNING(
+                           "objectSID for GUID [%s] not found\n",
+                           GUID_buf_string(&cache->entries[idx], &guid_buf));
                        continue;
                }
                status = dom_sid_split_rid(NULL,
@@ -3991,16 +4295,11 @@ static NTSTATUS dcesrv_samr_QueryDisplayInfo(struct dcesrv_call_state *dce_call,
                                           &rid);
                if (!NT_STATUS_IS_OK(status)) {
                        struct dom_sid_buf sid_buf;
-                       char *sid_str =
-                               dom_sid_str_buf(objectsid,
-                                               &sid_buf);
                        struct GUID_txt_buf guid_buf;
-                       char *guid_str =
-                               GUID_buf_string(&cache->entries[idx],
-                                               &guid_buf);
-                       DBG_WARNING("objectSID [%s] for GUID [%s] invalid\n",
-                                   sid_str,
-                                   guid_str);
+                       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;
                }
 
@@ -4234,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);
 
@@ -4277,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);
 
@@ -4311,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);
 
@@ -4357,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 };
@@ -4367,7 +4673,7 @@ static NTSTATUS dcesrv_samr_GetDomPwInfo(struct dcesrv_call_state *dce_call, TAL
        sam_ctx = samdb_connect(mem_ctx,
                                dce_call->event_ctx,
                                dce_call->conn->dce_ctx->lp_ctx,
-                               dce_call->conn->auth_state.session_info,
+                               session_info,
                                dce_call->conn->remote_address,
                                0);
        if (sam_ctx == NULL) {
@@ -4409,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);
 }
@@ -4427,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);
 }
@@ -4464,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);
 }
@@ -4480,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);
 }
@@ -4497,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);
 
@@ -4561,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;