Blob Blame History Raw
/*********************************************************/
/*  toksvc: a framework-native token broker service      */
/*  Copyright (C) 2020  SysDeer Technologies, LLC        */
/*  Released under GPLv2 and GPLv3; see COPYING.TOKSVC.  */
/*********************************************************/

#include <psxtypes/psxtypes.h>
#include <ntapi/ntapi.h>

#include <toksvc/toksvc.h>
#include "toksvc_driver_impl.h"
#include "toksvc_daemon_impl.h"

int32_t toks_client_connect(struct toks_driver_ctx * dctx)
{
	int32_t			status;
	void *			hsvclink;
	void *			hservice;
	void *			hserver;
	struct _nt_tty_sync_msg msg;
	nt_unicode_string	name;
	nt_guid_str_utf16	guid;
	nt_oa			oa;
	nt_sqos			sqos = {
					sizeof(sqos),
					NT_SECURITY_ANONYMOUS,
					NT_SECURITY_TRACKING_STATIC,
					1};

	ntapi->tt_guid_to_string_utf16(
		toks_get_driver_guid(dctx),
		&guid);

	name.strlen = sizeof(guid);
	name.maxlen = 0;
	name.buffer = &guid.lbrace;

	oa.len		= sizeof(oa);
	oa.root_dir	= toks_get_driver_hsvcdir(dctx);
	oa.obj_name	= &name;
	oa.obj_attr	= NT_OBJ_INHERIT;
	oa.sec_desc	= 0;
	oa.sec_qos	= &sqos;

	if ((status = ntapi->zw_open_symbolic_link_object(
			&hsvclink,
			NT_SYMBOLIC_LINK_QUERY,
			&oa)))
		return status;

	if ((status = ntapi->ipc_connect_by_symlink(&hservice,hsvclink,false))) {
		ntapi->zw_close(hsvclink);
		return status;
	}

	ntapi->zw_close(hsvclink);

	ntapi->tt_aligned_block_memset(
		&msg,0,sizeof(msg));

	msg.header.msg_type	= NT_LPC_NEW_MESSAGE;
	msg.header.data_size	= sizeof(msg.data);
	msg.header.msg_size	= sizeof(msg);
	msg.data.ttyinfo.opcode	= TOKS_DAEMON_CIDQUERY;

	if ((status = ntapi->zw_request_wait_reply_port(hservice,&msg,&msg))) {
		ntapi->zw_close(hservice);
		return status;

	} else if (msg.data.ttyinfo.status) {
		ntapi->zw_close(hservice);
		return msg.data.ttyinfo.status;
	}

	oa.len		= sizeof(oa);
	oa.root_dir	= 0;
	oa.obj_name	= 0;
	oa.obj_attr	= 0;
	oa.sec_desc	= 0;
	oa.sec_qos	= &sqos;

	if ((status = ntapi->zw_open_process(
			&hserver,NT_PROCESS_SYNCHRONIZE,
			&oa,&msg.data.syncinfo.cid))) {
		ntapi->zw_close(hservice);
		return status;
	}

	toks_set_driver_hservice(
		dctx,hservice);

	toks_set_driver_hserver(
		dctx,hserver);

	return NT_STATUS_SUCCESS;
}