static NTSTATUS pdb_samba_dsdb_enum_trusted_domains(struct pdb_methods *m,
						    TALLOC_CTX *mem_ctx,
						    uint32_t *_num_domains,
						    struct pdb_trusted_domain ***_domains)
{
	struct pdb_samba_dsdb_state *state = talloc_get_type_abort(
		m->private_data, struct pdb_samba_dsdb_state);
	TALLOC_CTX *tmp_ctx = talloc_stackframe();
	const char * const attrs[] = {
		"securityIdentifier",
		"flatName",
		"trustPartner",
		"trustAuthOutgoing",
		"trustAuthIncoming",
		"trustAttributes",
		"trustDirection",
		"trustType",
		"trustPosixOffset",
		"msDS-SupportedEncryptionTypes",
		"msDS-TrustForestTrustInfo",
		NULL
	};
	struct ldb_result *res = NULL;
	unsigned int i;
	struct pdb_trusted_domain **domains = NULL;
	NTSTATUS status;
	uint32_t di = 0;

	*_num_domains = 0;
	*_domains = NULL;

	status = dsdb_trust_search_tdos(state->ldb, NULL,
					attrs, tmp_ctx, &res);
	if (!NT_STATUS_IS_OK(status)) {
		DBG_ERR("dsdb_trust_search_tdos() - %s ", nt_errstr(status));
		TALLOC_FREE(tmp_ctx);
		return status;
	}

	if (res->count == 0) {
		TALLOC_FREE(tmp_ctx);
		return NT_STATUS_OK;
	}

	domains = talloc_zero_array(tmp_ctx, struct pdb_trusted_domain *,
				    res->count);
	if (domains == NULL) {
		TALLOC_FREE(tmp_ctx);
		return NT_STATUS_NO_MEMORY;
	}

	for (i = 0; i < res->count; i++) {
		struct ldb_message *msg = res->msgs[i];
		struct pdb_trusted_domain *d = NULL;

		status = pdb_samba_dsdb_msg_to_trusted_domain(msg, domains, &d);
		if (!NT_STATUS_IS_OK(status)) {
			DBG_ERR("pdb_samba_dsdb_msg_to_trusted_domain() - %s ",
				nt_errstr(status));
			TALLOC_FREE(tmp_ctx);
			return status;
		}

		domains[di++] = d;
	}

	domains = talloc_realloc(domains, domains, struct pdb_trusted_domain *,
				 di);
	*_domains = talloc_move(mem_ctx, &domains);
	*_num_domains = di;
	TALLOC_FREE(tmp_ctx);
	return NT_STATUS_OK;
}
static NTSTATUS pdb_samba_dsdb_enum_trusted_domains(struct pdb_methods *m,
						    TALLOC_CTX *mem_ctx,
						    uint32_t *_num_domains,
						    struct pdb_trusted_domain ***_domains)
{
	struct pdb_samba_dsdb_state *state = talloc_get_type_abort(
		m->private_data, struct pdb_samba_dsdb_state);
	TALLOC_CTX *tmp_ctx = talloc_stackframe();
	const char * const attrs[] = {
		"securityIdentifier",
		"flatName",
		"trustPartner",
		"trustAuthOutgoing",
		"trustAuthIncoming",
		"trustAttributes",
		"trustDirection",
		"trustType",
		"trustPosixOffset",
		"msDS-SupportedEncryptionTypes",
		"msDS-TrustForestTrustInfo",
		NULL
	};
	struct ldb_result *res = NULL;
	unsigned int i;
	struct pdb_trusted_domain **domains = NULL;
	NTSTATUS status;
	uint32_t di = 0;

	*_num_domains = 0;
	*_domains = NULL;

	status = dsdb_trust_search_tdos(state->ldb, NULL,
					attrs, tmp_ctx, &res);
	if (!NT_STATUS_IS_OK(status)) {
		DBG_ERR("dsdb_trust_search_tdos() - %s ", nt_errstr(status));
		TALLOC_FREE(tmp_ctx);
		return status;
	}

	if (res->count == 0) {
		TALLOC_FREE(tmp_ctx);
		return NT_STATUS_OK;
	}

	domains = talloc_zero_array(tmp_ctx, struct pdb_trusted_domain *,
				    res->count);
	if (domains == NULL) {
		TALLOC_FREE(tmp_ctx);
		return NT_STATUS_NO_MEMORY;
	}

	for (i = 0; i < res->count; i++) {
		struct ldb_message *msg = res->msgs[i];
		struct pdb_trusted_domain *d = NULL;

		status = pdb_samba_dsdb_msg_to_trusted_domain(msg, domains, &d);
		if (!NT_STATUS_IS_OK(status)) {
			DBG_ERR("pdb_samba_dsdb_msg_to_trusted_domain() - %s ",
				nt_errstr(status));
			TALLOC_FREE(tmp_ctx);
			return status;
		}

		domains[di++] = d;
	}

	domains = talloc_realloc(domains, domains, struct pdb_trusted_domain *,
				 di);
	*_domains = talloc_move(mem_ctx, &domains);
	*_num_domains = di;
	TALLOC_FREE(tmp_ctx);
	return NT_STATUS_OK;
}