Blame src/daemon/toks_daemon_pidopen.c

500bc3
/*********************************************************/
500bc3
/*  toksvc: a framework-native token broker service      */
500bc3
/*  Copyright (C) 2020  Z. Gilboa                        */
500bc3
/*  Released under GPLv2 and GPLv3; see COPYING.TOKSVC.  */
500bc3
/*********************************************************/
500bc3
500bc3
#include <psxtypes/psxtypes.h>
500bc3
#include <ntapi/ntapi.h>
500bc3
500bc3
#include <toksvc/toksvc.h>
500bc3
#include "toksvc_daemon_impl.h"
500bc3
#include "toksvc_driver_impl.h"
500bc3
500bc3
#include <psxabi/sys_daemon.h>
500bc3
500bc3
static const nt_guid	g_pidany    = NT_PROCESS_GUID_PIDANY;
500bc3
static const wchar16_t	p_pidany[6] = NT_PROCESS_OBJDIR_PREFIX_PIDANY;
500bc3
500bc3
static int32_t toks_daemon_pid_connect(
344692
	struct toks_daemon_ctx * dctx,
344692
	void **                  hipc)
500bc3
{
500bc3
	int32_t			status;
500bc3
	void *			hpidany;
500bc3
	void *			hpiddir;
500bc3
	nt_oa			ipcoa;
500bc3
	nt_unicode_string	pidname;
500bc3
	wchar16_t		pidnamebuf[8];
500bc3
500bc3
	/* hpiddir */
344692
	if (!(hpiddir = toks_get_driver_hpiddir(dctx->driver_ctx))) {
500bc3
		if ((status = ntapi->tt_open_ipc_object_directory(
500bc3
				&hpiddir,
500bc3
				NT_SEC_READ_CONTROL
500bc3
					| NT_DIRECTORY_QUERY
500bc3
					| NT_DIRECTORY_TRAVERSE
500bc3
					| NT_DIRECTORY_CREATE_OBJECT
500bc3
					| NT_DIRECTORY_CREATE_SUBDIRECTORY,
500bc3
				p_pidany,&g_pidany)))
500bc3
			return status;
500bc3
344692
		toks_set_driver_hpiddir(dctx->driver_ctx,hpiddir);
500bc3
	}
500bc3
500bc3
	/* pid entry name */
500bc3
	ntapi->tt_uint32_to_hex_utf16(
344692
		dctx->reqtokpid,pidnamebuf);
500bc3
500bc3
	pidname.strlen = sizeof(pidnamebuf);
500bc3
	pidname.maxlen = sizeof(pidnamebuf);;
500bc3
	pidname.buffer = pidnamebuf;
500bc3
500bc3
        /* open symlink */
500bc3
        ipcoa.len      = sizeof(ipcoa);
500bc3
        ipcoa.root_dir = hpiddir;
500bc3
        ipcoa.obj_name = &pidname;
500bc3
        ipcoa.obj_attr = 0;
500bc3
        ipcoa.sec_desc = 0;
500bc3
        ipcoa.sec_qos  = 0;
500bc3
500bc3
        if ((status = ntapi->zw_open_symbolic_link_object(
500bc3
			&hpidany,
500bc3
			NT_SEC_STANDARD_RIGHTS_READ | NT_GENERIC_READ,
500bc3
			&ipcoa)))
500bc3
		return status;
500bc3
500bc3
	status = ntapi->ipc_connect_by_symlink(
500bc3
		hipc,hpidany);
500bc3
500bc3
	ntapi->zw_close(hpidany);
500bc3
500bc3
	return status;
500bc3
}
500bc3
500bc3
int32_t toks_daemon_pidopen(struct toks_daemon_ctx * dctx)
500bc3
{
500bc3
	int32_t			status;
500bc3
	nt_tty_session_msg	msg;
500bc3
	void *			hipc;
500bc3
344692
	if ((status = toks_daemon_pid_connect(dctx,&hipc)))
500bc3
		return status;
500bc3
500bc3
	ntapi->tt_aligned_block_memset(
500bc3
		&msg,0,sizeof(msg));
500bc3
500bc3
	msg.header.msg_type		= NT_LPC_NEW_MESSAGE;
500bc3
	msg.header.data_size		= sizeof(msg.data);
500bc3
	msg.header.msg_size		= sizeof(msg);
500bc3
	msg.data.ttyinfo.opcode		= PSX_DAEMON_IOCTL_PIDQUERY;
500bc3
500bc3
	if ((status = ntapi->zw_request_wait_reply_port(hipc,&msg,&msg)))
500bc3
		return status;
500bc3
	else if (msg.data.ttyinfo.status)
500bc3
		return msg.data.ttyinfo.status;
500bc3
344692
	dctx->reqsyspid = msg.data.sessioninfo.syspid;
500bc3
500bc3
	return NT_STATUS_SUCCESS;
500bc3
}