lib: Make sid_parse return the parsed length
[samba.git] / source4 / dsdb / common / util.c
index dcbfc8c60a3d46ba647632fc5292d802e92e8789..411d1dd22b05d2d55fe674b5f9837b7d7b69cf45 100644 (file)
@@ -45,7 +45,6 @@
 #include "lib/socket/socket.h"
 #include "librpc/gen_ndr/irpc.h"
 #include "libds/common/flag_mapping.h"
-#include "../lib/util/util_runcmd.h"
 #include "lib/util/access.h"
 #include "lib/util/util_str_hex.h"
 #include "libcli/util/ntstatus.h"
@@ -56,6 +55,9 @@
  */
 #include "dsdb/samdb/ldb_modules/util.h"
 
+/* default is 30 minutes: -1e7 * 30 * 60 */
+#define DEFAULT_OBSERVATION_WINDOW              -18000000000
+
 /*
   search the sam for the specified attributes in a specific domain, filter on
   objectSid being in domain_sid.
@@ -200,26 +202,6 @@ struct dom_sid *samdb_search_dom_sid(struct ldb_context *sam_ldb,
        return sid;     
 }
 
-/*
-  return the count of the number of records in the sam matching the query
-*/
-int samdb_search_count(struct ldb_context *sam_ldb,
-                      TALLOC_CTX *mem_ctx,
-                      struct ldb_dn *basedn,
-                      const char *format, ...) _PRINTF_ATTRIBUTE(4,5)
-{
-       va_list ap;
-       const char *attrs[] = { NULL };
-       int ret;
-
-       va_start(ap, format);
-       ret = gendb_search_v(sam_ldb, mem_ctx, basedn, NULL, attrs, format, ap);
-       va_end(ap);
-
-       return ret;
-}
-
-
 /*
   search the sam for a single integer attribute in exactly 1 record
 */
@@ -360,7 +342,7 @@ uint32_t samdb_result_rid_from_sid(TALLOC_CTX *mem_ctx, const struct ldb_message
 struct dom_sid *samdb_result_dom_sid(TALLOC_CTX *mem_ctx, const struct ldb_message *msg, 
                                     const char *attr)
 {
-       bool ok;
+       struct sid_parse_ret ret;
        const struct ldb_val *v;
        struct dom_sid *sid;
        v = ldb_msg_find_ldb_val(msg, attr);
@@ -371,8 +353,8 @@ struct dom_sid *samdb_result_dom_sid(TALLOC_CTX *mem_ctx, const struct ldb_messa
        if (sid == NULL) {
                return NULL;
        }
-       ok = sid_parse(v->data, v->length, sid);
-       if (!ok) {
+       ret = sid_parse(v->data, v->length, sid);
+       if (ret.len == -1) {
                talloc_free(sid);
                return NULL;
        }
@@ -1439,21 +1421,24 @@ failed:
 }
 
 /*
-  work out the ntds settings invocationId for the current open ldb
+  work out the ntds settings invocationID/objectGUID for the current open ldb
 */
-const struct GUID *samdb_ntds_invocation_id(struct ldb_context *ldb)
+static const struct GUID *samdb_ntds_GUID(struct ldb_context *ldb,
+                                         const char *attribute,
+                                         const char *cache_name)
 {
        TALLOC_CTX *tmp_ctx;
-       const char *attrs[] = { "invocationId", NULL };
+       const char *attrs[] = { attribute, NULL };
        int ret;
        struct ldb_result *res;
-       struct GUID *invocation_id;
+       struct GUID *ntds_guid;
+       struct ldb_dn *ntds_settings_dn = NULL;
+       const char *errstr = NULL;
 
        /* see if we have a cached copy */
-       invocation_id = (struct GUID *)ldb_get_opaque(ldb, "cache.invocation_id");
-       if (invocation_id) {
-               SMB_ASSERT(!GUID_all_zero(invocation_id));
-               return invocation_id;
+       ntds_guid = (struct GUID *)ldb_get_opaque(ldb, cache_name);
+       if (ntds_guid != NULL) {
+               return ntds_guid;
        }
 
        tmp_ctx = talloc_new(ldb);
@@ -1461,148 +1446,85 @@ const struct GUID *samdb_ntds_invocation_id(struct ldb_context *ldb)
                goto failed;
        }
 
-       ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb, tmp_ctx), LDB_SCOPE_BASE, attrs, NULL);
+       ntds_settings_dn = samdb_ntds_settings_dn(ldb, tmp_ctx);
+       if (ntds_settings_dn == NULL) {
+               errstr = "samdb_ntds_settings_dn() returned NULL";
+               goto failed;
+       }
+
+       ret = ldb_search(ldb, tmp_ctx, &res, ntds_settings_dn,
+                        LDB_SCOPE_BASE, attrs, NULL);
        if (ret) {
+               errstr = ldb_errstring(ldb);
                goto failed;
        }
 
        if (res->count != 1) {
+               errstr = "incorrect number of results from base search";
                goto failed;
        }
 
-       invocation_id = talloc(tmp_ctx, struct GUID);
-       if (!invocation_id) {
+       ntds_guid = talloc(tmp_ctx, struct GUID);
+       if (ntds_guid == NULL) {
                goto failed;
        }
 
-       *invocation_id = samdb_result_guid(res->msgs[0], "invocationId");
-       if (GUID_all_zero(invocation_id)) {
-               if (ldb_msg_find_ldb_val(res->msgs[0], "invocationId")) {
-                       DEBUG(0, ("Failed to find our own NTDS Settings invocationId in the ldb!\n"));  
+       *ntds_guid = samdb_result_guid(res->msgs[0], attribute);
+
+       if (GUID_all_zero(ntds_guid)) {
+               if (ldb_msg_find_ldb_val(res->msgs[0], attribute)) {
+                       errstr = "failed to find the GUID attribute";
                } else {
-                       DEBUG(0, ("Failed to find parse own NTDS Settings invocationId from the ldb!\n"));
+                       errstr = "failed to parse the GUID";
                }
                goto failed;
        }
 
        /* cache the domain_sid in the ldb */
-       if (ldb_set_opaque(ldb, "cache.invocation_id", invocation_id) != LDB_SUCCESS) {
+       if (ldb_set_opaque(ldb, cache_name, ntds_guid) != LDB_SUCCESS) {
+               errstr = "ldb_set_opaque() failed";
                goto failed;
        }
 
-       talloc_steal(ldb, invocation_id);
+       talloc_steal(ldb, ntds_guid);
        talloc_free(tmp_ctx);
 
-       return invocation_id;
+       return ntds_guid;
 
 failed:
-       DEBUG(1,("Failed to find our own NTDS Settings invocationId in the ldb!\n"));
+       DBG_WARNING("Failed to find our own NTDS Settings %s in the ldb: %s!\n",
+                   attribute, errstr);
        talloc_free(tmp_ctx);
        return NULL;
 }
 
-bool samdb_set_ntds_invocation_id(struct ldb_context *ldb, const struct GUID *invocation_id_in)
-{
-       TALLOC_CTX *tmp_ctx;
-       struct GUID *invocation_id_new;
-       struct GUID *invocation_id_old;
-
-       /* see if we have a cached copy */
-       invocation_id_old = (struct GUID *)ldb_get_opaque(ldb, 
-                                                        "cache.invocation_id");
-
-       tmp_ctx = talloc_new(ldb);
-       if (tmp_ctx == NULL) {
-               goto failed;
-       }
-
-       invocation_id_new = talloc(tmp_ctx, struct GUID);
-       if (!invocation_id_new) {
-               goto failed;
-       }
-
-       SMB_ASSERT(!GUID_all_zero(invocation_id_in));
-       *invocation_id_new = *invocation_id_in;
-
-       /* cache the domain_sid in the ldb */
-       if (ldb_set_opaque(ldb, "cache.invocation_id", invocation_id_new) != LDB_SUCCESS) {
-               goto failed;
-       }
-
-       talloc_steal(ldb, invocation_id_new);
-       talloc_free(tmp_ctx);
-       talloc_free(invocation_id_old);
-
-       return true;
-
-failed:
-       DEBUG(1,("Failed to set our own cached invocationId in the ldb!\n"));
-       talloc_free(tmp_ctx);
-       return false;
-}
-
 /*
   work out the ntds settings objectGUID for the current open ldb
 */
 const struct GUID *samdb_ntds_objectGUID(struct ldb_context *ldb)
 {
-       TALLOC_CTX *tmp_ctx;
-       const char *attrs[] = { "objectGUID", NULL };
-       int ret;
-       struct ldb_result *res;
-       struct GUID *ntds_guid;
-
-       /* see if we have a cached copy */
-       ntds_guid = (struct GUID *)ldb_get_opaque(ldb, "cache.ntds_guid");
-       if (ntds_guid) {
-               return ntds_guid;
-       }
-
-       tmp_ctx = talloc_new(ldb);
-       if (tmp_ctx == NULL) {
-               goto failed;
-       }
-
-       ret = ldb_search(ldb, tmp_ctx, &res, samdb_ntds_settings_dn(ldb, tmp_ctx), LDB_SCOPE_BASE, attrs, NULL);
-       if (ret) {
-               goto failed;
-       }
-
-       if (res->count != 1) {
-               goto failed;
-       }
-
-       ntds_guid = talloc(tmp_ctx, struct GUID);
-       if (!ntds_guid) {
-               goto failed;
-       }
-
-       *ntds_guid = samdb_result_guid(res->msgs[0], "objectGUID");
-
-       /* cache the domain_sid in the ldb */
-       if (ldb_set_opaque(ldb, "cache.ntds_guid", ntds_guid) != LDB_SUCCESS) {
-               goto failed;
-       }
-
-       talloc_steal(ldb, ntds_guid);
-       talloc_free(tmp_ctx);
-
-       return ntds_guid;
+       return samdb_ntds_GUID(ldb, "objectGUID", "cache.ntds_guid");
+}
 
-failed:
-       DEBUG(1,("Failed to find our own NTDS Settings objectGUID in the ldb!\n"));
-       talloc_free(tmp_ctx);
-       return NULL;
+/*
+  work out the ntds settings invocationId for the current open ldb
+*/
+const struct GUID *samdb_ntds_invocation_id(struct ldb_context *ldb)
+{
+       return samdb_ntds_GUID(ldb, "invocationId", "cache.invocation_id");
 }
 
-bool samdb_set_ntds_objectGUID(struct ldb_context *ldb, const struct GUID *ntds_guid_in)
+static bool samdb_set_ntds_GUID(struct ldb_context *ldb,
+                               const struct GUID *ntds_guid_in,
+                               const char *attribute,
+                               const char *cache_name)
 {
        TALLOC_CTX *tmp_ctx;
        struct GUID *ntds_guid_new;
        struct GUID *ntds_guid_old;
 
        /* see if we have a cached copy */
-       ntds_guid_old = (struct GUID *)ldb_get_opaque(ldb, "cache.ntds_guid");
+       ntds_guid_old = (struct GUID *)ldb_get_opaque(ldb, cache_name);
 
        tmp_ctx = talloc_new(ldb);
        if (tmp_ctx == NULL) {
@@ -1617,7 +1539,7 @@ bool samdb_set_ntds_objectGUID(struct ldb_context *ldb, const struct GUID *ntds_
        *ntds_guid_new = *ntds_guid_in;
 
        /* cache the domain_sid in the ldb */
-       if (ldb_set_opaque(ldb, "cache.ntds_guid", ntds_guid_new) != LDB_SUCCESS) {
+       if (ldb_set_opaque(ldb, cache_name, ntds_guid_new) != LDB_SUCCESS) {
                goto failed;
        }
 
@@ -1628,11 +1550,28 @@ bool samdb_set_ntds_objectGUID(struct ldb_context *ldb, const struct GUID *ntds_
        return true;
 
 failed:
-       DEBUG(1,("Failed to set our own cached invocationId in the ldb!\n"));
+       DBG_WARNING("Failed to set our own cached %s in the ldb!\n",
+                   attribute);
        talloc_free(tmp_ctx);
        return false;
 }
 
+bool samdb_set_ntds_objectGUID(struct ldb_context *ldb, const struct GUID *ntds_guid_in)
+{
+       return samdb_set_ntds_GUID(ldb,
+                                  ntds_guid_in,
+                                  "objectGUID",
+                                  "cache.ntds_guid");
+}
+
+bool samdb_set_ntds_invocation_id(struct ldb_context *ldb, const struct GUID *invocation_id_in)
+{
+       return samdb_set_ntds_GUID(ldb,
+                                  invocation_id_in,
+                                  "invocationId",
+                                  "cache.invocation_id");
+}
+
 /*
   work out the server dn for the current open ldb
 */
@@ -1891,13 +1830,16 @@ const char *samdb_client_site_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
                                   bool fallback)
 {
        const char *attrs[] = { "cn", "siteObject", NULL };
-       struct ldb_dn *sites_container_dn, *subnets_dn, *sites_dn;
-       struct ldb_result *res;
-       const struct ldb_val *val;
-       const char *site_name = NULL, *l_subnet_name = NULL;
+       struct ldb_dn *sites_container_dn = NULL;
+       struct ldb_dn *subnets_dn = NULL;
+       struct ldb_dn *sites_dn = NULL;
+       struct ldb_result *res = NULL;
+       const struct ldb_val *val = NULL;
+       const char *site_name = NULL;
+       const char *l_subnet_name = NULL;
        const char *allow_list[2] = { NULL, NULL };
        unsigned int i, count;
-       int cnt, ret;
+       int ret;
 
        /*
         * if we don't have a client ip e.g. ncalrpc
@@ -1909,14 +1851,12 @@ const char *samdb_client_site_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
 
        sites_container_dn = samdb_sites_dn(ldb, mem_ctx);
        if (sites_container_dn == NULL) {
-               return NULL;
+               goto exit;
        }
 
        subnets_dn = ldb_dn_copy(mem_ctx, sites_container_dn);
        if ( ! ldb_dn_add_child_fmt(subnets_dn, "CN=Subnets")) {
-               talloc_free(sites_container_dn);
-               talloc_free(subnets_dn);
-               return NULL;
+               goto exit;
        }
 
        ret = ldb_search(ldb, mem_ctx, &res, subnets_dn, LDB_SCOPE_ONELEVEL,
@@ -1924,9 +1864,7 @@ const char *samdb_client_site_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
        if (ret == LDB_ERR_NO_SUCH_OBJECT) {
                count = 0;
        } else if (ret != LDB_SUCCESS) {
-               talloc_free(sites_container_dn);
-               talloc_free(subnets_dn);
-               return NULL;
+               goto exit;
        } else {
                count = res->count;
        }
@@ -1951,7 +1889,7 @@ const char *samdb_client_site_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
                        site_name = talloc_strdup(mem_ctx,
                                                  (const char *) val->data);
 
-                       talloc_free(sites_dn);
+                       TALLOC_FREE(sites_dn);
 
                        break;
                }
@@ -1963,8 +1901,17 @@ const char *samdb_client_site_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
                 * is for sure the same as our server site). If more sites do
                 * exist then we don't know which one to use and set the site
                 * name to "". */
-               cnt = samdb_search_count(ldb, mem_ctx, sites_container_dn,
-                                        "(objectClass=site)");
+               size_t cnt = 0;
+               ret = dsdb_domain_count(
+                       ldb,
+                       &cnt,
+                       sites_container_dn,
+                       NULL,
+                       LDB_SCOPE_SUBTREE,
+                       "(objectClass=site)");
+               if (ret != LDB_SUCCESS) {
+                       goto exit;
+               }
                if (cnt == 1) {
                        site_name = samdb_server_site_name(ldb, mem_ctx);
                } else {
@@ -1977,9 +1924,10 @@ const char *samdb_client_site_name(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
                *subnet_name = talloc_strdup(mem_ctx, l_subnet_name);
        }
 
-       talloc_free(sites_container_dn);
-       talloc_free(subnets_dn);
-       talloc_free(res);
+exit:
+       TALLOC_FREE(sites_container_dn);
+       TALLOC_FREE(subnets_dn);
+       TALLOC_FREE(res);
 
        return site_name;
 }
@@ -2081,6 +2029,9 @@ static void pwd_timeout_debug(struct tevent_context *unused1,
  */
 enum samr_ValidationStatus samdb_check_password(TALLOC_CTX *mem_ctx,
                                                struct loadparm_context *lp_ctx,
+                                               const char *account_name,
+                                               const char *user_principal_name,
+                                               const char *full_name,
                                                const DATA_BLOB *utf8_blob,
                                                const uint32_t pwdProperties,
                                                const uint32_t minPwdLength)
@@ -2109,7 +2060,7 @@ enum samr_ValidationStatus samdb_check_password(TALLOC_CTX *mem_ctx,
                int error = 0;
                struct tevent_context *event_ctx = NULL;
                struct tevent_req *req = NULL;
-               struct samba_runcmd_state *run_cmd = NULL;
+               int cps_stdin = -1;
                const char * const cmd[4] = {
                        "/bin/sh", "-c",
                        password_script,
@@ -2127,18 +2078,58 @@ enum samr_ValidationStatus samdb_check_password(TALLOC_CTX *mem_ctx,
                                 tevent_timeval_current_ofs(1, 0),
                                 pwd_timeout_debug, NULL);
 
+               check_ret = setenv("SAMBA_CPS_ACCOUNT_NAME", account_name, 1);
+               if (check_ret != 0) {
+                       TALLOC_FREE(password_script);
+                       TALLOC_FREE(event_ctx);
+                       return SAMR_VALIDATION_STATUS_PASSWORD_FILTER_ERROR;
+               }
+               if (user_principal_name != NULL) {
+                       check_ret = setenv("SAMBA_CPS_USER_PRINCIPAL_NAME",
+                                          user_principal_name, 1);
+               } else {
+                       unsetenv("SAMBA_CPS_USER_PRINCIPAL_NAME");
+               }
+               if (check_ret != 0) {
+                       TALLOC_FREE(password_script);
+                       TALLOC_FREE(event_ctx);
+                       return SAMR_VALIDATION_STATUS_PASSWORD_FILTER_ERROR;
+               }
+               if (full_name != NULL) {
+                       check_ret = setenv("SAMBA_CPS_FULL_NAME", full_name, 1);
+               } else {
+                       unsetenv("SAMBA_CPS_FULL_NAME");
+               }
+               if (check_ret != 0) {
+                       TALLOC_FREE(password_script);
+                       TALLOC_FREE(event_ctx);
+                       return SAMR_VALIDATION_STATUS_PASSWORD_FILTER_ERROR;
+               }
+
                req = samba_runcmd_send(event_ctx, event_ctx,
                                        tevent_timeval_current_ofs(10, 0),
                                        100, 100, cmd, NULL);
-               run_cmd = tevent_req_data(req, struct samba_runcmd_state);
-               if (write(run_cmd->fd_stdin, utf8_pw, utf8_len) != utf8_len) {
+               unsetenv("SAMBA_CPS_ACCOUNT_NAME");
+               unsetenv("SAMBA_CPS_USER_PRINCIPAL_NAME");
+               unsetenv("SAMBA_CPS_FULL_NAME");
+               if (req == NULL) {
                        TALLOC_FREE(password_script);
                        TALLOC_FREE(event_ctx);
                        return SAMR_VALIDATION_STATUS_PASSWORD_FILTER_ERROR;
                }
 
-               close(run_cmd->fd_stdin);
-               run_cmd->fd_stdin = -1;
+               cps_stdin = samba_runcmd_export_stdin(req);
+
+               if (write(cps_stdin, utf8_pw, utf8_len) != utf8_len) {
+                       close(cps_stdin);
+                       cps_stdin = -1;
+                       TALLOC_FREE(password_script);
+                       TALLOC_FREE(event_ctx);
+                       return SAMR_VALIDATION_STATUS_PASSWORD_FILTER_ERROR;
+               }
+
+               close(cps_stdin);
+               cps_stdin = -1;
 
                if (!tevent_req_poll(req, event_ctx)) {
                        TALLOC_FREE(password_script);
@@ -2522,12 +2513,18 @@ NTSTATUS samdb_set_password_sid(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
                uint32_t trust_direction;
                uint32_t i;
                const struct ldb_val *old_val = NULL;
-               struct trustAuthInOutBlob old_blob = {};
+               struct trustAuthInOutBlob old_blob = {
+                       .count = 0,
+               };
                uint32_t old_version = 0;
                struct AuthenticationInformation *old_version_a = NULL;
                uint32_t _new_version = 0;
-               struct trustAuthInOutBlob new_blob = {};
-               struct ldb_val new_val = {};
+               struct trustAuthInOutBlob new_blob = {
+                       .count = 0,
+               };
+               struct ldb_val new_val = {
+                       .length = 0,
+               };
                struct timeval tv = timeval_current();
                NTTIME now = timeval_to_nttime(&tv);
                enum ndr_err_code ndr_err;
@@ -3833,6 +3830,7 @@ NTSTATUS dsdb_get_extended_dn_guid(struct ldb_dn *dn, struct GUID *guid, const c
 NTSTATUS dsdb_get_extended_dn_uint64(struct ldb_dn *dn, uint64_t *val, const char *component_name)
 {
        const struct ldb_val *v;
+       int error = 0;
 
        v = ldb_dn_get_extended_component(dn, component_name);
        if (v == NULL) {
@@ -3847,7 +3845,10 @@ NTSTATUS dsdb_get_extended_dn_uint64(struct ldb_dn *dn, uint64_t *val, const cha
                memcpy(s, v->data, v->length);
                s[v->length] = 0;
 
-               *val = strtoull(s, NULL, 0);
+               *val = strtoull_err(s, NULL, 0, &error);
+               if (error != 0) {
+                       return NT_STATUS_INVALID_PARAMETER;
+               }
        }
        return NT_STATUS_OK;
 }
@@ -3866,6 +3867,7 @@ NTSTATUS dsdb_get_extended_dn_nttime(struct ldb_dn *dn, NTTIME *nttime, const ch
 NTSTATUS dsdb_get_extended_dn_uint32(struct ldb_dn *dn, uint32_t *val, const char *component_name)
 {
        const struct ldb_val *v;
+       int error = 0;
 
        v = ldb_dn_get_extended_component(dn, component_name);
        if (v == NULL) {
@@ -3879,7 +3881,10 @@ NTSTATUS dsdb_get_extended_dn_uint32(struct ldb_dn *dn, uint32_t *val, const cha
                char s[v->length + 1];
                memcpy(s, v->data, v->length);
                s[v->length] = 0;
-               *val = strtoul(s, NULL, 0);
+               *val = strtoul_err(s, NULL, 0, &error);
+               if (error != 0) {
+                       return NT_STATUS_INVALID_PARAMETER;
+               }
        }
 
        return NT_STATUS_OK;
@@ -3933,6 +3938,7 @@ uint32_t dsdb_dn_val_rmd_flags(const struct ldb_val *val)
        const char *p;
        uint32_t flags;
        char *end;
+       int error = 0;
 
        if (val->length < 13) {
                return 0;
@@ -3941,8 +3947,8 @@ uint32_t dsdb_dn_val_rmd_flags(const struct ldb_val *val)
        if (!p) {
                return 0;
        }
-       flags = strtoul(p+11, &end, 10);
-       if (!end || *end != '>') {
+       flags = strtoul_err(p+11, &end, 10, &error);
+       if (!end || *end != '>' || error != 0) {
                /* it must end in a > */
                return 0;
        }
@@ -5368,9 +5374,9 @@ int samdb_result_effective_badPwdCount(struct ldb_context *sam_ldb,
 
        if (res != NULL) {
                lockOutObservationWindow =
-                       ldb_msg_find_attr_as_int(res->msgs[0],
-                                                "msDS-LockoutObservationWindow",
-                                                 0);
+                       ldb_msg_find_attr_as_int64(res->msgs[0],
+                                                  "msDS-LockoutObservationWindow",
+                                                   DEFAULT_OBSERVATION_WINDOW);
                talloc_free(res);
        } else {
 
@@ -5409,10 +5415,11 @@ static int64_t get_lockout_observation_window(struct ldb_message *domain_msg,
        if (pso_msg != NULL) {
                return ldb_msg_find_attr_as_int64(pso_msg,
                                                  "msDS-LockoutObservationWindow",
-                                                  0);
+                                                  DEFAULT_OBSERVATION_WINDOW);
        } else {
                return ldb_msg_find_attr_as_int64(domain_msg,
-                                                 "lockOutObservationWindow", 0);
+                                                 "lockOutObservationWindow",
+                                                  DEFAULT_OBSERVATION_WINDOW);
        }
 }
 
@@ -5753,3 +5760,182 @@ bool dsdb_objects_have_same_nc(struct ldb_context *ldb,
 
        return same_nc;
 }
+/*
+ * Context for dsdb_count_domain_callback
+ */
+struct dsdb_count_domain_context {
+       /*
+        * Number of matching records
+        */
+       size_t count;
+       /*
+        * sid of the domain that the records must belong to.
+        * if NULL records can belong to any domain.
+        */
+       struct dom_sid *dom_sid;
+};
+
+/*
+ * @brief ldb aysnc callback for dsdb_domain_count.
+ *
+ * count the number of records in the database matching an LDAP query,
+ * optionally filtering for domain membership.
+ *
+ * @param [in,out] req the ldb request being processed
+ *                    req->context contains:
+ *                        count   The number of matching records
+ *                        dom_sid The domain sid, if present records must belong
+ *                                to the domain to be counted.
+ *@param [in,out] ares The query result.
+ *
+ * @return an LDB error code
+ *
+ */
+static int dsdb_count_domain_callback(
+       struct ldb_request *req,
+       struct ldb_reply *ares)
+{
+
+       if (ares == NULL) {
+               return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR);
+       }
+       if (ares->error != LDB_SUCCESS) {
+               int error = ares->error;
+               TALLOC_FREE(ares);
+               return ldb_request_done(req, error);
+       }
+
+       switch (ares->type) {
+       case LDB_REPLY_ENTRY:
+       {
+               struct dsdb_count_domain_context *context = NULL;
+               struct sid_parse_ret ret;
+               bool in_domain;
+               struct dom_sid sid;
+               const struct ldb_val *v;
+
+               context = req->context;
+               if (context->dom_sid == NULL) {
+                       context->count++;
+                       break;
+               }
+
+               v = ldb_msg_find_ldb_val(ares->message, "objectSid");
+               if (v == NULL) {
+                       break;
+               }
+
+               ret = sid_parse(v->data, v->length, &sid);
+               if (ret.len == -1) {
+                       break;
+               }
+
+               in_domain = dom_sid_in_domain(context->dom_sid, &sid);
+               if (!in_domain) {
+                       break;
+               }
+
+               context->count++;
+               break;
+       }
+       case LDB_REPLY_REFERRAL:
+               break;
+
+       case LDB_REPLY_DONE:
+               TALLOC_FREE(ares);
+               return ldb_request_done(req, LDB_SUCCESS);
+       }
+
+       TALLOC_FREE(ares);
+
+       return LDB_SUCCESS;
+}
+
+/*
+ * @brief Count the number of records matching a query.
+ *
+ * Count the number of entries in the database matching the supplied query,
+ * optionally filtering only those entries belonging to the supplied domain.
+ *
+ * @param ldb [in] Current ldb context
+ * @param count [out] Pointer to the count
+ * @param base [in] The base dn for the quey
+ * @param dom_sid [in] The domain sid, if non NULL records that are not a member
+ *                     of the domain are ignored.
+ * @param scope [in] Search scope.
+ * @param exp_fmt [in] format string for the query.
+ *
+ * @return LDB_STATUS code.
+ */
+int dsdb_domain_count(
+       struct ldb_context *ldb,
+       size_t *count,
+       struct ldb_dn *base,
+       struct dom_sid *dom_sid,
+       enum ldb_scope scope,
+       const char *exp_fmt, ...)
+{
+       TALLOC_CTX *tmp_ctx = NULL;
+       struct ldb_request *req = NULL;
+       struct dsdb_count_domain_context *context = NULL;
+       char *expression = NULL;
+       const char *object_sid[] = {"objectSid", NULL};
+       const char *none[] = {NULL};
+       va_list ap;
+       int ret;
+
+       *count = 0;
+       tmp_ctx = talloc_new(ldb);
+
+       context = talloc_zero(tmp_ctx, struct dsdb_count_domain_context);
+       if (context == NULL) {
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
+       context->dom_sid = dom_sid;
+
+       if (exp_fmt) {
+               va_start(ap, exp_fmt);
+               expression = talloc_vasprintf(tmp_ctx, exp_fmt, ap);
+               va_end(ap);
+
+               if (expression == NULL) {
+                       TALLOC_FREE(context);
+                       TALLOC_FREE(tmp_ctx);
+                       return LDB_ERR_OPERATIONS_ERROR;
+               }
+       }
+
+       ret = ldb_build_search_req(
+               &req,
+               ldb,
+               tmp_ctx,
+               base,
+               scope,
+               expression,
+               (dom_sid == NULL) ? none : object_sid,
+               NULL,
+               context,
+               dsdb_count_domain_callback,
+               NULL);
+       ldb_req_set_location(req, "dsdb_domain_count");
+
+       if (ret != LDB_SUCCESS) goto done;
+
+       ret = ldb_request(ldb, req);
+
+       if (ret == LDB_SUCCESS) {
+               ret = ldb_wait(req->handle, LDB_WAIT_ALL);
+               if (ret == LDB_SUCCESS) {
+                       *count = context->count;
+               }
+       }
+
+
+done:
+       TALLOC_FREE(expression);
+       TALLOC_FREE(req);
+       TALLOC_FREE(context);
+       TALLOC_FREE(tmp_ctx);
+
+       return ret;
+}