int PRINTF_ATTRIBUTE(6, 7) 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;
}
int PRINTF_ATTRIBUTE(6, 7) 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;
}