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;
		ssize_t 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 == -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;
}
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;
		ssize_t 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 == -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;
}