static NTSTATUS dcesrv_lsa_OpenTrustedDomain_common(
					struct dcesrv_call_state *dce_call,
					TALLOC_CTX *tmp_mem,
					struct lsa_policy_state *policy_state,
					const char *filter,
					uint32_t access_mask,
					struct dcesrv_handle **_handle)
{
	struct lsa_trusted_domain_state *trusted_domain_state;
	struct dcesrv_handle *handle;
	struct ldb_message **msgs;
	const char *attrs[] = {
		"trustDirection",
		"flatname",
		NULL
	};
	uint32_t direction;
	int ret;

        /* TODO: perform access checks */

	/* search for the trusted_domain record */
	ret = gendb_search(policy_state->sam_ldb, tmp_mem,
			   policy_state->system_dn,
			   &msgs, attrs, "%s", filter);
	if (ret == 0) {
		return NT_STATUS_OBJECT_NAME_NOT_FOUND;
	}

	if (ret != 1) {
		DEBUG(0,("Found %d records matching %s under %s\n", ret,
			 filter,
			 ldb_dn_get_linearized(policy_state->system_dn)));
		return NT_STATUS_INTERNAL_DB_CORRUPTION;
	}

	trusted_domain_state = talloc_zero(tmp_mem,
					   struct lsa_trusted_domain_state);
	if (!trusted_domain_state) {
		return NT_STATUS_NO_MEMORY;
	}
	trusted_domain_state->policy = policy_state;

	trusted_domain_state->trusted_domain_dn =
		talloc_steal(trusted_domain_state, msgs[0]->dn);

	direction = ldb_msg_find_attr_as_int(msgs[0], "trustDirection", 0);
	if (direction & LSA_TRUST_DIRECTION_INBOUND) {
		const char *flatname = ldb_msg_find_attr_as_string(msgs[0],
							"flatname", NULL);

		/* search for the trusted_domain account */
		ret = gendb_search(policy_state->sam_ldb, tmp_mem,
				   policy_state->domain_dn,
				   &msgs, attrs,
				   "(&(samaccountname=%s$)(objectclass=user)"
				   "(userAccountControl:%s:=%u))",
				   flatname,
				   LDB_OID_COMPARATOR_AND,
				   UF_INTERDOMAIN_TRUST_ACCOUNT);
		if (ret == 1) {
			trusted_domain_state->trusted_domain_user_dn =
				talloc_steal(trusted_domain_state, msgs[0]->dn);
		}
	}

	handle = dcesrv_handle_create(dce_call, LSA_HANDLE_TRUSTED_DOMAIN);
	if (!handle) {
		return NT_STATUS_NO_MEMORY;
	}

	handle->data = talloc_steal(handle, trusted_domain_state);

	trusted_domain_state->access_mask = access_mask;
	trusted_domain_state->policy = talloc_reference(trusted_domain_state,
							policy_state);

	*_handle = handle;

	return NT_STATUS_OK;
}
static NTSTATUS dcesrv_lsa_OpenTrustedDomain_common(
					struct dcesrv_call_state *dce_call,
					TALLOC_CTX *tmp_mem,
					struct lsa_policy_state *policy_state,
					const char *filter,
					uint32_t access_mask,
					struct dcesrv_handle **_handle)
{
	struct lsa_trusted_domain_state *trusted_domain_state;
	struct dcesrv_handle *handle;
	struct ldb_message **msgs;
	const char *attrs[] = {
		"trustDirection",
		"flatname",
		NULL
	};
	uint32_t direction;
	int ret;

         

	 
	ret = gendb_search(policy_state->sam_ldb, tmp_mem,
			   policy_state->system_dn,
			   &msgs, attrs, "%s", filter);
	if (ret == 0) {
		return NT_STATUS_OBJECT_NAME_NOT_FOUND;
	}

	if (ret != 1) {
		DEBUG(0,("Found %d records matching %s under %s\n", ret,
			 filter,
			 ldb_dn_get_linearized(policy_state->system_dn)));
		return NT_STATUS_INTERNAL_DB_CORRUPTION;
	}

	trusted_domain_state = talloc_zero(tmp_mem,
					   struct lsa_trusted_domain_state);
	if (!trusted_domain_state) {
		return NT_STATUS_NO_MEMORY;
	}
	trusted_domain_state->policy = policy_state;

	trusted_domain_state->trusted_domain_dn =
		talloc_steal(trusted_domain_state, msgs[0]->dn);

	direction = ldb_msg_find_attr_as_int(msgs[0], "trustDirection", 0);
	if (direction & LSA_TRUST_DIRECTION_INBOUND) {
		const char *flatname = ldb_msg_find_attr_as_string(msgs[0],
							"flatname", NULL);

		 
		ret = gendb_search(policy_state->sam_ldb, tmp_mem,
				   policy_state->domain_dn,
				   &msgs, attrs,
				   "(&(samaccountname=%s$)(objectclass=user)"
				   "(userAccountControl:%s:=%u))",
				   flatname,
				   LDB_OID_COMPARATOR_AND,
				   UF_INTERDOMAIN_TRUST_ACCOUNT);
		if (ret == 1) {
			trusted_domain_state->trusted_domain_user_dn =
				talloc_steal(trusted_domain_state, msgs[0]->dn);
		}
	}

	handle = dcesrv_handle_create(dce_call, LSA_HANDLE_TRUSTED_DOMAIN);
	if (!handle) {
		return NT_STATUS_NO_MEMORY;
	}

	handle->data = talloc_steal(handle, trusted_domain_state);

	trusted_domain_state->access_mask = access_mask;
	trusted_domain_state->policy = talloc_reference(trusted_domain_state,
							policy_state);

	*_handle = handle;

	return NT_STATUS_OK;
}