Blob Blame History Raw
/*********************************************************/
/*  toksvc: a framework-native token broker service      */
/*  Copyright (C) 2020  Z. Gilboa                        */
/*  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"

static void toks_daemon_token_reset(struct toks_token * token)
{
	token->self        = 0;
	token->keys.key[0] = 0;
	token->keys.key[1] = 0;
	token->keys.key[2] = 0;
	token->keys.key[3] = 0;
	token->keys.key[4] = 0;
	token->keys.key[5] = 0;
}

int32_t __stdcall toks_daemon_release(struct toks_daemon_ctx * dctx)
{
	nt_tty_port_msg *           msg;
	struct toks_token *         token;
	struct toks_token *         toptr;
	struct toks_token *         tocap;
	uint32_t *                  keys;

	msg   = &dctx->reply;
	keys  = msg->ipcinfo.ipckeys;

	toptr = toks_get_driver_tokens(dctx->driver_ctx);
	tocap = &toptr[toks_get_driver_ntokens(dctx->driver_ctx)];

	for (token=0; !token && (toptr<tocap); toptr++)
		if ((toptr->self)
				&& (toptr->keys.key[0] == keys[0])
				&& (toptr->keys.key[1] == keys[1])
				&& (toptr->keys.key[2] == keys[2])
				&& (toptr->keys.key[3] == keys[3])
				&& (toptr->keys.key[4] == keys[4])
				&& (toptr->keys.key[5] == keys[5]))
			token = toptr;

	if (!token)
		return NT_STATUS_INVALID_PARAMETER;

	toks_daemon_token_reset(token);

	ntapi->zw_alert_thread(token->client.hinstance);
	ntapi->zw_close(token->client.hinstance);

	return NT_STATUS_SUCCESS;
}