WERROR dns_common_extract(struct ldb_context *samdb,
			  const struct ldb_message_element *el,
			  TALLOC_CTX *mem_ctx,
			  struct dnsp_DnssrvRpcRecord **records,
			  uint16_t *num_records)
{
	uint16_t ri;
	struct dnsp_DnssrvRpcRecord *recs;

	*records = NULL;
	*num_records = 0;

	recs = talloc_zero_array(mem_ctx, struct dnsp_DnssrvRpcRecord,
				 el->num_values);
	if (recs == NULL) {
		return WERR_NOT_ENOUGH_MEMORY;
	}
	for (ri = 0; ri < el->num_values; ri++) {
		bool am_rodc;
		int ret;
		const char *dnsHostName = NULL;
		struct ldb_val *v = &el->values[ri];
		enum ndr_err_code ndr_err;
		ndr_err = ndr_pull_struct_blob(v, recs, &recs[ri],
				(ndr_pull_flags_fn_t)ndr_pull_dnsp_DnssrvRpcRecord);
		if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
			TALLOC_FREE(recs);
			DEBUG(0, ("Failed to grab dnsp_DnssrvRpcRecord\n"));
			return DNS_ERR(SERVER_FAILURE);
		}

		/*
		 * In AD, except on an RODC (where we should list a random RWDC,
		 * we should over-stamp the MNAME with our own hostname
		 */
		if (recs[ri].wType != DNS_TYPE_SOA) {
			continue;
		}

		ret = samdb_rodc(samdb, &am_rodc);
		if (ret != LDB_SUCCESS) {
			DEBUG(0, ("Failed to confirm we are not an RODC: %s\n",
				  ldb_errstring(samdb)));
			return DNS_ERR(SERVER_FAILURE);
		}

		if (am_rodc) {
			continue;
		}

		ret = samdb_dns_host_name(samdb, &dnsHostName);
		if (ret != LDB_SUCCESS || dnsHostName == NULL) {
			DEBUG(0, ("Failed to get dnsHostName from rootDSE"));
			return DNS_ERR(SERVER_FAILURE);
		}

		recs[ri].data.soa.mname = talloc_strdup(recs, dnsHostName);
	}

	*records = recs;
	*num_records = el->num_values;
	return WERR_OK;
}
WERROR dns_common_extract(struct ldb_context *samdb,
			  const struct ldb_message_element *el,
			  TALLOC_CTX *mem_ctx,
			  struct dnsp_DnssrvRpcRecord **records,
			  uint16_t *num_records)
{
	uint16_t ri;
	struct dnsp_DnssrvRpcRecord *recs;

	*records = NULL;
	*num_records = 0;

	recs = talloc_zero_array(mem_ctx, struct dnsp_DnssrvRpcRecord,
				 el->num_values);
	if (recs == NULL) {
		return WERR_NOT_ENOUGH_MEMORY;
	}
	for (ri = 0; ri < el->num_values; ri++) {
		bool am_rodc;
		int ret;
		const char *dnsHostName = NULL;
		struct ldb_val *v = &el->values[ri];
		enum ndr_err_code ndr_err;
		ndr_err = ndr_pull_struct_blob(v, recs, &recs[ri],
				(ndr_pull_flags_fn_t)ndr_pull_dnsp_DnssrvRpcRecord);
		if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
			TALLOC_FREE(recs);
			DEBUG(0, ("Failed to grab dnsp_DnssrvRpcRecord\n"));
			return DNS_ERR(SERVER_FAILURE);
		}

		 
		if (recs[ri].wType != DNS_TYPE_SOA) {
			continue;
		}

		ret = samdb_rodc(samdb, &am_rodc);
		if (ret != LDB_SUCCESS) {
			DEBUG(0, ("Failed to confirm we are not an RODC: %s\n",
				  ldb_errstring(samdb)));
			return DNS_ERR(SERVER_FAILURE);
		}

		if (am_rodc) {
			continue;
		}

		ret = samdb_dns_host_name(samdb, &dnsHostName);
		if (ret != LDB_SUCCESS || dnsHostName == NULL) {
			DEBUG(0, ("Failed to get dnsHostName from rootDSE"));
			return DNS_ERR(SERVER_FAILURE);
		}

		recs[ri].data.soa.mname = talloc_strdup(recs, dnsHostName);
	}

	*records = recs;
	*num_records = el->num_values;
	return WERR_OK;
}