WERROR dns_common_wildcard_lookup(struct ldb_context *samdb,
				  TALLOC_CTX *mem_ctx,
				  struct ldb_dn *dn,
				  struct dnsp_DnssrvRpcRecord **records,
				  uint16_t *num_records)
{
	const struct timeval start = timeval_current();
	int ret;
	WERROR werr = WERR_OK;
	struct ldb_message *msg = NULL;
	struct ldb_message_element *el = NULL;
	const struct ldb_val *name = NULL;

	*records = NULL;
	*num_records = 0;

	name = ldb_dn_get_rdn_val(dn);
	if (name == NULL) {
		werr = DNS_ERR(NAME_ERROR);
		goto exit;
	}

	/* Don't look for a wildcard for @ */
	if (name->length == 1 && name->data[0] == '@') {
		werr = dns_common_lookup(samdb,
					 mem_ctx,
					 dn,
					 records,
					 num_records,
					 NULL);
		goto exit;
	}

	werr =  dns_name_check(
			mem_ctx,
			strlen((const char*)name->data),
			(const char*) name->data);
	if (!W_ERROR_IS_OK(werr)) {
		goto exit;
	}

	/*
	 * Do a point search first, then fall back to a wildcard
	 * lookup if it does not exist
	 */
	werr = dns_common_lookup(samdb,
				 mem_ctx,
				 dn,
				 records,
				 num_records,
				 NULL);
	if (!W_ERROR_EQUAL(werr, WERR_DNS_ERROR_NAME_DOES_NOT_EXIST)) {
		goto exit;
	}

	ret = dns_wildcard_lookup(samdb, mem_ctx, dn, &msg);
	if (ret == LDB_ERR_OPERATIONS_ERROR) {
		werr = DNS_ERR(SERVER_FAILURE);
		goto exit;
	}
	if (ret != LDB_SUCCESS) {
		werr = DNS_ERR(NAME_ERROR);
		goto exit;
	}

	el = ldb_msg_find_element(msg, "dnsRecord");
	if (el == NULL) {
		werr = WERR_DNS_ERROR_NAME_DOES_NOT_EXIST;
		goto exit;
	}

	werr = dns_common_extract(samdb, el, mem_ctx, records, num_records);
	TALLOC_FREE(msg);
	if (!W_ERROR_IS_OK(werr)) {
		goto exit;
	}

	werr = WERR_OK;
exit:
	DNS_COMMON_LOG_OPERATION(
		win_errstr(werr),
		&start,
		NULL,
		dn == NULL ? NULL : ldb_dn_get_linearized(dn),
		NULL);
	return werr;
}
WERROR dns_common_wildcard_lookup(struct ldb_context *samdb,
				  TALLOC_CTX *mem_ctx,
				  struct ldb_dn *dn,
				  struct dnsp_DnssrvRpcRecord **records,
				  uint16_t *num_records)
{
	const struct timeval start = timeval_current();
	int ret;
	WERROR werr = WERR_OK;
	struct ldb_message *msg = NULL;
	struct ldb_message_element *el = NULL;
	const struct ldb_val *name = NULL;

	*records = NULL;
	*num_records = 0;

	name = ldb_dn_get_rdn_val(dn);
	if (name == NULL) {
		werr = DNS_ERR(NAME_ERROR);
		goto exit;
	}

	 
	if (name->length == 1 && name->data[0] == '@') {
		werr = dns_common_lookup(samdb,
					 mem_ctx,
					 dn,
					 records,
					 num_records,
					 NULL);
		goto exit;
	}

	werr =  dns_name_check(
			mem_ctx,
			strlen((const char*)name->data),
			(const char*) name->data);
	if (!W_ERROR_IS_OK(werr)) {
		goto exit;
	}

	 
	werr = dns_common_lookup(samdb,
				 mem_ctx,
				 dn,
				 records,
				 num_records,
				 NULL);
	if (!W_ERROR_EQUAL(werr, WERR_DNS_ERROR_NAME_DOES_NOT_EXIST)) {
		goto exit;
	}

	ret = dns_wildcard_lookup(samdb, mem_ctx, dn, &msg);
	if (ret == LDB_ERR_OPERATIONS_ERROR) {
		werr = DNS_ERR(SERVER_FAILURE);
		goto exit;
	}
	if (ret != LDB_SUCCESS) {
		werr = DNS_ERR(NAME_ERROR);
		goto exit;
	}

	el = ldb_msg_find_element(msg, "dnsRecord");
	if (el == NULL) {
		werr = WERR_DNS_ERROR_NAME_DOES_NOT_EXIST;
		goto exit;
	}

	werr = dns_common_extract(samdb, el, mem_ctx, records, num_records);
	TALLOC_FREE(msg);
	if (!W_ERROR_IS_OK(werr)) {
		goto exit;
	}

	werr = WERR_OK;
exit:
	DNS_COMMON_LOG_OPERATION(
		win_errstr(werr),
		&start,
		NULL,
		dn == NULL ? NULL : ldb_dn_get_linearized(dn),
		NULL);
	return werr;
}