static NTSTATUS get_tdo(struct ldb_context *sam, TALLOC_CTX *mem_ctx,
			struct ldb_dn *basedn, const char *dns_domain,
			const char *netbios, struct dom_sid2 *sid,
			struct ldb_message ***msgs)
{
	const char *attrs[] = { "flatname", "trustPartner",
				"securityIdentifier", "trustDirection",
				"trustType", "trustAttributes",
				"trustPosixOffset",
				"msDs-supportedEncryptionTypes",
				"msDS-TrustForestTrustInfo",
				NULL
	};
	char *dns = NULL;
	char *nbn = NULL;
	char *filter;
	int ret;


	if (dns_domain || netbios || sid) {
		filter = talloc_strdup(mem_ctx,
				   "(&(objectclass=trustedDomain)(|");
	} else {
		filter = talloc_strdup(mem_ctx,
				       "(objectclass=trustedDomain)");
	}

	if (dns_domain) {
		dns = ldb_binary_encode_string(mem_ctx, dns_domain);
		if (!dns) {
			return NT_STATUS_NO_MEMORY;
		}
		talloc_asprintf_addbuf(&filter, "(trustPartner=%s)", dns);
	}
	if (netbios) {
		nbn = ldb_binary_encode_string(mem_ctx, netbios);
		if (!nbn) {
			return NT_STATUS_NO_MEMORY;
		}
		talloc_asprintf_addbuf(&filter, "(flatname=%s)", nbn);
	}
	if (sid) {
		struct dom_sid_buf buf;
		char *sidstr = dom_sid_str_buf(sid, &buf);
		talloc_asprintf_addbuf(
			&filter, "(securityIdentifier=%s)", sidstr);
	}
	if (dns_domain || netbios || sid) {
		talloc_asprintf_addbuf(&filter, "))");
	}
	if (filter == NULL) {
		return NT_STATUS_NO_MEMORY;
	}

	ret = gendb_search(sam, mem_ctx, basedn, msgs, attrs, "%s", filter);
	if (ret == 0) {
		return NT_STATUS_OBJECT_NAME_NOT_FOUND;
	}

	if (ret != 1) {
		return NT_STATUS_OBJECT_NAME_COLLISION;
	}

	return NT_STATUS_OK;
}
static NTSTATUS get_tdo(struct ldb_context *sam, TALLOC_CTX *mem_ctx,
			struct ldb_dn *basedn, const char *dns_domain,
			const char *netbios, struct dom_sid2 *sid,
			struct ldb_message ***msgs)
{
	const char *attrs[] = { "flatname", "trustPartner",
				"securityIdentifier", "trustDirection",
				"trustType", "trustAttributes",
				"trustPosixOffset",
				"msDs-supportedEncryptionTypes",
				"msDS-TrustForestTrustInfo",
				NULL
	};
	char *dns = NULL;
	char *nbn = NULL;
	char *filter;
	int ret;


	if (dns_domain || netbios || sid) {
		filter = talloc_strdup(mem_ctx,
				   "(&(objectclass=trustedDomain)(|");
	} else {
		filter = talloc_strdup(mem_ctx,
				       "(objectclass=trustedDomain)");
	}

	if (dns_domain) {
		dns = ldb_binary_encode_string(mem_ctx, dns_domain);
		if (!dns) {
			return NT_STATUS_NO_MEMORY;
		}
		talloc_asprintf_addbuf(&filter, "(trustPartner=%s)", dns);
	}
	if (netbios) {
		nbn = ldb_binary_encode_string(mem_ctx, netbios);
		if (!nbn) {
			return NT_STATUS_NO_MEMORY;
		}
		talloc_asprintf_addbuf(&filter, "(flatname=%s)", nbn);
	}
	if (sid) {
		struct dom_sid_buf buf;
		char *sidstr = dom_sid_str_buf(sid, &buf);
		talloc_asprintf_addbuf(
			&filter, "(securityIdentifier=%s)", sidstr);
	}
	if (dns_domain || netbios || sid) {
		talloc_asprintf_addbuf(&filter, "))");
	}
	if (filter == NULL) {
		return NT_STATUS_NO_MEMORY;
	}

	ret = gendb_search(sam, mem_ctx, basedn, msgs, attrs, "%s", filter);
	if (ret == 0) {
		return NT_STATUS_OBJECT_NAME_NOT_FOUND;
	}

	if (ret != 1) {
		return NT_STATUS_OBJECT_NAME_COLLISION;
	}

	return NT_STATUS_OK;
}