static NTSTATUS pdb_samba_dsdb_msg_to_trusted_domain(const struct ldb_message *msg,
						TALLOC_CTX *mem_ctx,
						struct pdb_trusted_domain **_d)
{
	struct pdb_trusted_domain *d = NULL;
	const char *str = NULL;
	struct dom_sid *sid = NULL;
	const struct ldb_val *val = NULL;
	uint64_t val64;

	*_d = NULL;

	d = talloc_zero(mem_ctx, struct pdb_trusted_domain);
	if (d == NULL) {
		return NT_STATUS_NO_MEMORY;
	}

	str = ldb_msg_find_attr_as_string(msg, "flatName", NULL);
	if (str == NULL) {
		TALLOC_FREE(d);
		return NT_STATUS_INTERNAL_DB_CORRUPTION;
	}
	d->netbios_name = talloc_strdup(d, str);
	if (d->netbios_name == NULL) {
		TALLOC_FREE(d);
		return NT_STATUS_NO_MEMORY;
	}

	str = ldb_msg_find_attr_as_string(msg, "trustPartner", NULL);
	if (str != NULL) {
		d->domain_name = talloc_strdup(d, str);
		if (d->domain_name == NULL) {
			TALLOC_FREE(d);
			return NT_STATUS_NO_MEMORY;
		}
	}

	sid = samdb_result_dom_sid(d, msg, "securityIdentifier");
	if (sid != NULL) {
		d->security_identifier = *sid;
		TALLOC_FREE(sid);
	}

	val = ldb_msg_find_ldb_val(msg, "trustAuthOutgoing");
	if (val != NULL) {
		d->trust_auth_outgoing = data_blob_dup_talloc(d, *val);
		if (d->trust_auth_outgoing.data == NULL) {
			TALLOC_FREE(d);
			return NT_STATUS_NO_MEMORY;
		}
	}
	val = ldb_msg_find_ldb_val(msg, "trustAuthIncoming");
	if (val != NULL) {
		d->trust_auth_incoming = data_blob_dup_talloc(d, *val);
		if (d->trust_auth_incoming.data == NULL) {
			TALLOC_FREE(d);
			return NT_STATUS_NO_MEMORY;
		}
	}

	d->trust_direction = ldb_msg_find_attr_as_uint(msg, "trustDirection", 0);
	d->trust_type = ldb_msg_find_attr_as_uint(msg, "trustType", 0);
	d->trust_attributes = ldb_msg_find_attr_as_uint(msg, "trustAttributes", 0);

	val64 = ldb_msg_find_attr_as_uint64(msg, "trustPosixOffset", UINT64_MAX);
	if (val64 != UINT64_MAX) {
		d->trust_posix_offset = talloc(d, uint32_t);
		if (d->trust_posix_offset == NULL) {
			TALLOC_FREE(d);
			return NT_STATUS_NO_MEMORY;
		}
		*d->trust_posix_offset = (uint32_t)val64;
	}

	val64 = ldb_msg_find_attr_as_uint64(msg, "msDS-SupportedEncryptionTypes", UINT64_MAX);
	if (val64 != UINT64_MAX) {
		d->supported_enc_type = talloc(d, uint32_t);
		if (d->supported_enc_type == NULL) {
			TALLOC_FREE(d);
			return NT_STATUS_NO_MEMORY;
		}
		*d->supported_enc_type = (uint32_t)val64;
	}

	val = ldb_msg_find_ldb_val(msg, "msDS-TrustForestTrustInfo");
	if (val != NULL) {
		d->trust_forest_trust_info = data_blob_dup_talloc(d, *val);
		if (d->trust_forest_trust_info.data == NULL) {
			TALLOC_FREE(d);
			return NT_STATUS_NO_MEMORY;
		}
	}

	*_d = d;
	return NT_STATUS_OK;
}
static NTSTATUS pdb_samba_dsdb_msg_to_trusted_domain(const struct ldb_message *msg,
						TALLOC_CTX *mem_ctx,
						struct pdb_trusted_domain **_d)
{
	struct pdb_trusted_domain *d = NULL;
	const char *str = NULL;
	struct dom_sid *sid = NULL;
	const struct ldb_val *val = NULL;
	uint64_t val64;

	*_d = NULL;

	d = talloc_zero(mem_ctx, struct pdb_trusted_domain);
	if (d == NULL) {
		return NT_STATUS_NO_MEMORY;
	}

	str = ldb_msg_find_attr_as_string(msg, "flatName", NULL);
	if (str == NULL) {
		TALLOC_FREE(d);
		return NT_STATUS_INTERNAL_DB_CORRUPTION;
	}
	d->netbios_name = talloc_strdup(d, str);
	if (d->netbios_name == NULL) {
		TALLOC_FREE(d);
		return NT_STATUS_NO_MEMORY;
	}

	str = ldb_msg_find_attr_as_string(msg, "trustPartner", NULL);
	if (str != NULL) {
		d->domain_name = talloc_strdup(d, str);
		if (d->domain_name == NULL) {
			TALLOC_FREE(d);
			return NT_STATUS_NO_MEMORY;
		}
	}

	sid = samdb_result_dom_sid(d, msg, "securityIdentifier");
	if (sid != NULL) {
		d->security_identifier = *sid;
		TALLOC_FREE(sid);
	}

	val = ldb_msg_find_ldb_val(msg, "trustAuthOutgoing");
	if (val != NULL) {
		d->trust_auth_outgoing = data_blob_dup_talloc(d, *val);
		if (d->trust_auth_outgoing.data == NULL) {
			TALLOC_FREE(d);
			return NT_STATUS_NO_MEMORY;
		}
	}
	val = ldb_msg_find_ldb_val(msg, "trustAuthIncoming");
	if (val != NULL) {
		d->trust_auth_incoming = data_blob_dup_talloc(d, *val);
		if (d->trust_auth_incoming.data == NULL) {
			TALLOC_FREE(d);
			return NT_STATUS_NO_MEMORY;
		}
	}

	d->trust_direction = ldb_msg_find_attr_as_uint(msg, "trustDirection", 0);
	d->trust_type = ldb_msg_find_attr_as_uint(msg, "trustType", 0);
	d->trust_attributes = ldb_msg_find_attr_as_uint(msg, "trustAttributes", 0);

	val64 = ldb_msg_find_attr_as_uint64(msg, "trustPosixOffset", UINT64_MAX);
	if (val64 != UINT64_MAX) {
		d->trust_posix_offset = talloc(d, uint32_t);
		if (d->trust_posix_offset == NULL) {
			TALLOC_FREE(d);
			return NT_STATUS_NO_MEMORY;
		}
		*d->trust_posix_offset = (uint32_t)val64;
	}

	val64 = ldb_msg_find_attr_as_uint64(msg, "msDS-SupportedEncryptionTypes", UINT64_MAX);
	if (val64 != UINT64_MAX) {
		d->supported_enc_type = talloc(d, uint32_t);
		if (d->supported_enc_type == NULL) {
			TALLOC_FREE(d);
			return NT_STATUS_NO_MEMORY;
		}
		*d->supported_enc_type = (uint32_t)val64;
	}

	val = ldb_msg_find_ldb_val(msg, "msDS-TrustForestTrustInfo");
	if (val != NULL) {
		d->trust_forest_trust_info = data_blob_dup_talloc(d, *val);
		if (d->trust_forest_trust_info.data == NULL) {
			TALLOC_FREE(d);
			return NT_STATUS_NO_MEMORY;
		}
	}

	*_d = d;
	return NT_STATUS_OK;
}