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_daemon_impl.h"
#include "toksvc_driver_impl.h"

#include <psxabi/sys_daemon.h>

static const nt_guid	g_pidany    = NT_PROCESS_GUID_PIDANY;
static const wchar16_t	p_pidany[6] = NT_PROCESS_OBJDIR_PREFIX_PIDANY;

static int32_t toks_daemon_pid_connect(
	struct toks_daemon_ctx * dctx,
	void **                  hipc)
{
	int32_t			status;
	void *			hpidany;
	void *			hpiddir;
	nt_oa			ipcoa;
	nt_unicode_string	pidname;
	wchar16_t		pidnamebuf[8];

	/* hpiddir */
	if (!(hpiddir = toks_get_driver_hpiddir(dctx->driver_ctx))) {
		if ((status = ntapi->tt_open_ipc_object_directory(
				&hpiddir,
				NT_SEC_READ_CONTROL
					| NT_DIRECTORY_QUERY
					| NT_DIRECTORY_TRAVERSE
					| NT_DIRECTORY_CREATE_OBJECT
					| NT_DIRECTORY_CREATE_SUBDIRECTORY,
				p_pidany,&g_pidany)))
			return status;

		toks_set_driver_hpiddir(dctx->driver_ctx,hpiddir);
	}

	/* pid entry name */
	ntapi->tt_uint32_to_hex_utf16(
		dctx->reqtokpid,pidnamebuf);

	pidname.strlen = sizeof(pidnamebuf);
	pidname.maxlen = sizeof(pidnamebuf);;
	pidname.buffer = pidnamebuf;

        /* open symlink */
        ipcoa.len      = sizeof(ipcoa);
        ipcoa.root_dir = hpiddir;
        ipcoa.obj_name = &pidname;
        ipcoa.obj_attr = 0;
        ipcoa.sec_desc = 0;
        ipcoa.sec_qos  = 0;

        if ((status = ntapi->zw_open_symbolic_link_object(
			&hpidany,
			NT_SEC_STANDARD_RIGHTS_READ | NT_GENERIC_READ,
			&ipcoa)))
		return status;

	status = ntapi->ipc_connect_by_symlink(
		hipc,hpidany);

	ntapi->zw_close(hpidany);

	return status;
}

int32_t toks_daemon_pidopen(struct toks_daemon_ctx * dctx)
{
	int32_t			status;
	nt_tty_session_msg	msg;
	void *			hipc;

	if ((status = toks_daemon_pid_connect(dctx,&hipc)))
		return status;

	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		= PSX_DAEMON_IOCTL_PIDQUERY;

	if ((status = ntapi->zw_request_wait_reply_port(hipc,&msg,&msg)))
		return status;
	else if (msg.data.ttyinfo.status)
		return msg.data.ttyinfo.status;

	dctx->reqsyspid = msg.data.sessioninfo.syspid;

	return NT_STATUS_SUCCESS;
}