Blame src/driver/toks_driver_ctx.c

2420c2
/*********************************************************/
2420c2
/*  toksvc: a framework-native token broker service      */
2420c2
/*  Copyright (C) 2020  Z. Gilboa                        */
2420c2
/*  Released under GPLv2 and GPLv3; see COPYING.TOKSVC.  */
2420c2
/*********************************************************/
2420c2
2420c2
#include <ntapi/ntapi.h>
2420c2
#include <ntapi/nt_atomic.h>
2420c2
2420c2
#include <stdint.h>
2420c2
2420c2
#include <toksvc/toksvc.h>
2420c2
#include "toksvc_init_impl.h"
2420c2
#include "toksvc_nolibc_impl.h"
2420c2
2420c2
#define ARGV_DRIVER
2420c2
2420c2
#include "toksvc_version.h"
2420c2
#include "toksvc_daemon_impl.h"
2420c2
#include "toksvc_dprintf_impl.h"
2420c2
#include "toksvc_driver_impl.h"
2420c2
#include "argv/argv.h"
2420c2
2420c2
/* pty integration */
2420c2
#include <psxtypes/section/freestd.h>
2420c2
2420c2
__attr_section_decl__(".freestd")
2420c2
static const nt_tty_affiliation tty_affiliation
2420c2
	__attr_section__(".freestd")
2420c2
	= NT_TTY_AFFILIATION_DEFAULT;
2420c2
2420c2
/* ntapi accessor table */
2420c2
const ntapi_vtbl * toks_ntapi;
2420c2
2420c2
/* daemon */
2420c2
static struct toks_daemon_ctx toks_daemon_ctx;
618937
static const  nt_guid toks_daemon_default_guid = TOKS_PORT_GUID_DAEMON;
00069c
static const  wchar16_t toks_service_name[6]   = TOKS_PORT_NAME_PREFIX;
2420c2
2420c2
/* package info */
2420c2
static const struct toks_source_version toks_src_version = {
2420c2
	TOKS_TAG_VER_MAJOR,
2420c2
	TOKS_TAG_VER_MINOR,
2420c2
	TOKS_TAG_VER_PATCH,
2420c2
	TOKSVC_GIT_VERSION
2420c2
};
2420c2
2420c2
struct toks_driver_ctx_alloc {
2420c2
	struct argv_meta *		meta;
2420c2
	struct toks_driver_ctx_impl	ctx;
2420c2
	uint64_t			guard;
2420c2
};
2420c2
2420c2
struct toks_split_vector {
2420c2
	char **		targv;
2420c2
	char **		eargv;
2420c2
};
2420c2
2420c2
static uint32_t toks_argv_flags(uint32_t flags)
2420c2
{
2420c2
	uint32_t ret = ARGV_CLONE_VECTOR;
2420c2
2420c2
	if (flags & TOKS_DRIVER_VERBOSITY_NONE)
2420c2
		ret |= ARGV_VERBOSITY_NONE;
2420c2
2420c2
	if (flags & TOKS_DRIVER_VERBOSITY_ERRORS)
2420c2
		ret |= ARGV_VERBOSITY_ERRORS;
2420c2
2420c2
	if (flags & TOKS_DRIVER_VERBOSITY_STATUS)
2420c2
		ret |= ARGV_VERBOSITY_STATUS;
2420c2
2420c2
	return ret;
2420c2
}
2420c2
2420c2
static int toks_driver_usage(
2420c2
	const char *			program,
2420c2
	const char *			arg,
2420c2
	const struct argv_option **	optv,
2420c2
	struct argv_meta *		meta)
2420c2
{
2420c2
	char header[512];
2420c2
2420c2
	snprintf(header,sizeof(header),
2420c2
		"Usage: %s [options] <file>...\n" "Options:\n",
2420c2
		program);
2420c2
2420c2
	argv_usage(STDOUT_FILENO,header,optv,arg);
2420c2
	argv_free(meta);
2420c2
2420c2
	return TOKS_USAGE;
2420c2
}
2420c2
2f270e
static int32_t toks_query_performance_counters_failover(nt_filetime * ticks)
2f270e
{
2f270e
	(void)ticks;
2f270e
	return 0;
2f270e
}
2f270e
2420c2
static struct toks_driver_ctx_impl * toks_driver_ctx_alloc(
2420c2
	struct argv_meta *		meta,
2420c2
	const struct toks_common_ctx *	cctx)
2420c2
{
2420c2
	struct toks_driver_ctx_alloc *	ictx;
2420c2
	size_t				size;
2420c2
	nt_runtime_data *		rtdata;
2420c2
2420c2
	if (ntapi->tt_get_runtime_data(&rtdata,0))
2420c2
		return 0;
2420c2
2420c2
	size =  sizeof(struct toks_driver_ctx_alloc);
2420c2
2420c2
	if (!(ictx = calloc(1,size)))
2420c2
		return 0;
2420c2
2420c2
	if (cctx)
2420c2
		memcpy(&ictx->ctx.cctx,cctx,sizeof(*cctx));
2420c2
2f270e
	ictx->ctx.ticks.qpc         = toks_query_performance_counters_failover;
2f270e
	ictx->ctx.ticks.pcfreq.quad = 0;
2f270e
2420c2
	ictx->meta = meta;
2420c2
	ictx->ctx.rtdata = rtdata;
2420c2
	return &ictx->ctx;
2420c2
}
2420c2
2420c2
static int toks_get_driver_ctx_fail(struct argv_meta * meta)
2420c2
{
2420c2
	argv_free(meta);
2420c2
	return -1;
2420c2
}
2420c2
2420c2
#define TOKS_SARGV_ELEMENTS		1024
2420c2
2420c2
static int toks_split_argv(
2420c2
	char **				argv,
2420c2
	struct toks_split_vector *	sargv)
2420c2
{
2420c2
	ptrdiff_t	argc;
2420c2
	char **		parg;
2420c2
2420c2
	/* argc */
2420c2
	for (parg=argv; *parg; )
2420c2
		parg++;
2420c2
2420c2
	if ((argc = parg - argv) >= TOKS_SARGV_ELEMENTS)
2420c2
		return -1;
2420c2
2420c2
	/* clone argv into targv */
2420c2
	ntapi->tt_aligned_block_memset(
2420c2
		(uintptr_t *)sargv->targv,
2420c2
		0,TOKS_SARGV_ELEMENTS*sizeof(char *));
2420c2
2420c2
	ntapi->tt_aligned_block_memcpy(
2420c2
		(uintptr_t *)sargv->targv,
2420c2
		(uintptr_t *)argv,
2420c2
		argc*sizeof(char *));
2420c2
2420c2
	/* eargv */
2420c2
	for (parg=sargv->targv; *parg; parg++) {
2420c2
		if (!(strcmp(*parg,"-e")) || !(strcmp(*parg,"--exec"))) {
2420c2
			sargv->eargv = &argv[parg-sargv->targv];
2420c2
			sargv->eargv++;
2420c2
			*parg = 0;
2420c2
			return 0;
2420c2
		}
2420c2
	}
2420c2
2420c2
	return 0;
2420c2
}
2420c2
2420c2
int toks_get_driver_ctx(
2420c2
	char **				argv,
2420c2
	char **				envp,
2420c2
	uint32_t			flags,
2420c2
	struct toks_driver_ctx **	pctx)
2420c2
{
2420c2
	struct toks_driver_ctx_impl *	ctx;
2420c2
	struct toks_common_ctx		cctx;
2420c2
	struct toks_split_vector	sargv;
2420c2
	const struct argv_option *	optv[TOKS_OPTV_ELEMENTS];
2420c2
	struct argv_meta *		meta;
2420c2
	struct argv_entry *		entry;
618937
	struct argv_entry *		uuid;
618937
	nt_guid				svcguid;
2420c2
	const char *			program;
c847e3
	const char *			ch;
c847e3
	int				ntokens;
2f270e
	void *				hkernel32;
2420c2
	char *				targv[TOKS_SARGV_ELEMENTS];
2420c2
2420c2
	(void)envp;
2420c2
2420c2
	if (toks_init())
2420c2
		return -1;
2420c2
2420c2
	argv_optv_init(toks_default_options,optv);
2420c2
2420c2
	sargv.targv = targv;
2420c2
	sargv.eargv = 0;
2420c2
2420c2
	if (toks_split_argv(argv,&sargv))
2420c2
		return -1;
2420c2
2420c2
	if (!(meta = argv_get(
2420c2
			sargv.targv,optv,
2420c2
			toks_argv_flags(flags),
2420c2
			STDERR_FILENO)))
2420c2
		return -1;
2420c2
618937
	if (!(flags & TOKS_DRIVER_MODE_CLIENT))
618937
		flags |= TOKS_DRIVER_MODE_SERVER;
618937
618937
	uuid    = 0;
c847e3
	ntokens = 0;
2420c2
	program = argv_program_name(argv[0]);
618937
2420c2
	memset(&cctx,0,sizeof(cctx));
2420c2
	cctx.drvflags = flags;
2420c2
	cctx.eargv = sargv.eargv;
2420c2
2420c2
	if (!argv[1] && (flags & TOKS_DRIVER_VERBOSITY_USAGE))
2420c2
		return toks_driver_usage(program,0,optv,meta);
2420c2
2420c2
	for (entry=meta->entries; entry->fopt || entry->arg; entry++) {
2420c2
		if (entry->fopt) {
2420c2
			switch (entry->tag) {
2420c2
				case TAG_HELP:
2420c2
					if (flags & TOKS_DRIVER_VERBOSITY_USAGE)
2420c2
						return toks_driver_usage(program,entry->arg,optv,meta);
2420c2
2420c2
				case TAG_VERSION:
2420c2
					cctx.drvflags |= TOKS_DRIVER_VERSION;
2420c2
					break;
2420c2
2420c2
				case TAG_DAEMON:
2420c2
					if (!strcmp("always",entry->arg))
2420c2
						cctx.drvflags |= TOKS_DRIVER_DAEMON_ALWAYS;
2420c2
2420c2
					else if (!strcmp("never",entry->arg))
2420c2
						cctx.drvflags |= TOKS_DRIVER_DAEMON_NEVER;
2420c2
2420c2
					break;
2420c2
2420c2
				case TAG_SYSROOT:
2420c2
					cctx.sysroot = entry->arg;
2420c2
					break;
618937
618937
				case TAG_UUID:
618937
					uuid = entry;
618937
					break;
486fcd
c847e3
				case TAG_TOKENS:
c847e3
					for (ntokens=0, ch=entry->arg; *ch && (ntokens>=0); ch++)
c847e3
						if ((*ch < '0') || (*ch >'9'))
c847e3
							ntokens = -1;
c847e3
						else if (ntokens >= 1000)
c847e3
							ntokens = -1;
c847e3
						else
c847e3
							ntokens = ntokens * 10 + (*ch - '0');
c847e3
					break;
c847e3
486fcd
				case TAG_CONNECT:
486fcd
					cctx.drvflags &= ~(uint64_t)TOKS_DRIVER_MODE_SERVER;
486fcd
					cctx.drvflags |= TOKS_DRIVER_MODE_CLIENT;
486fcd
					break;
02451d
02451d
				case TAG_ACQUIRE:
02451d
					cctx.drvflags &= ~(uint64_t)TOKS_DRIVER_MODE_SERVER;
02451d
					cctx.drvflags |= TOKS_DRIVER_MODE_CLIENT;
02451d
					cctx.drvflags |= TOKS_DRIVER_ACTION_ACQUIRE;
02451d
					break;
2420c2
			}
2420c2
		} else
2420c2
			/* strict */
2420c2
			return toks_driver_usage(program,0,optv,meta);
2420c2
	}
2420c2
c847e3
	if ((cctx.drvflags & TOKS_DRIVER_MODE_SERVER) && (ntokens <= 0)) {
c847e3
		if (flags & TOKS_DRIVER_VERBOSITY_ERRORS)
c847e3
			toks_dprintf(STDERR_FILENO,
c847e3
				"%s: error: number of tokens not set or is invalid.",
c847e3
				program);
c847e3
		return toks_get_driver_ctx_fail(meta);
c847e3
	}
c847e3
618937
	if (uuid && ntapi->tt_string_to_guid_utf8(uuid->arg,&svcguid)) {
618937
		if (flags & TOKS_DRIVER_VERBOSITY_ERRORS)
618937
			toks_dprintf(STDERR_FILENO,
618937
				"%s: error: '%s' is not a valid service guid (did you forget the braces?)",
618937
				program,uuid->arg);
618937
		return toks_get_driver_ctx_fail(meta);
618937
618937
	}
618937
2420c2
	if (cctx.sysroot && toks_open_dir(&cctx.hroot,0,cctx.sysroot,false)) {
2420c2
		if (flags & TOKS_DRIVER_VERBOSITY_ERRORS)
2420c2
			toks_dprintf(STDERR_FILENO,
2420c2
				"%s: error: could not open sysroot directory '%s'",
2420c2
				program,cctx.sysroot);
2420c2
		return toks_get_driver_ctx_fail(meta);
2420c2
	}
2420c2
2420c2
	if (!(ctx = toks_driver_ctx_alloc(meta,&cctx)))
2420c2
		return toks_get_driver_ctx_fail(meta);
2420c2
618937
	ntapi->tt_guid_copy(
618937
		&ctx->uuid,
618937
		uuid ? &svcguid : &ctx->rtdata->srv_guid);
618937
2f270e
	if ((toks_ntapi->tt_create_private_event(
2f270e
			&ctx->hevent,
2f270e
			NT_NOTIFICATION_EVENT,
2f270e
			NT_EVENT_NOT_SIGNALED)))
2f270e
		return toks_get_driver_ctx_fail(meta);
2f270e
00069c
	if ((ntapi->tt_open_dev_object_directory(
00069c
			&ctx->hsvcdir,
00069c
			NT_DIRECTORY_ALL_ACCESS,
00069c
			toks_service_name,
00069c
			&toks_daemon_default_guid)))
00069c
		return toks_get_driver_ctx_fail(meta);
00069c
2f270e
	if ((hkernel32 = pe_get_kernel32_module_handle()))
2f270e
		if ((ctx->ticks.qpc = pe_get_procedure_address(
2f270e
				hkernel32,"QueryPerformanceCounter")))
2f270e
			ntapi->zw_query_performance_counter(
2f270e
				&(nt_filetime){{0,0}},
2f270e
				&ctx->ticks.pcfreq);
2f270e
c847e3
	ctx->ntokens            = ntokens;
2420c2
	ctx->ctx.program	= program;
2420c2
	ctx->ctx.cctx		= &ctx->cctx;
618937
	ctx->cctx.uuid          = &ctx->uuid;
2420c2
f2a689
	toks_daemon_ctx.driver_ctx = &ctx->ctx;
f2a689
c847e3
	if (cctx.drvflags & TOKS_DRIVER_MODE_SERVER) {
c847e3
		if (!(ctx->tokens = toks_calloc(ntokens,sizeof(*ctx->tokens))))
c847e3
			return toks_get_driver_ctx_fail(meta);
c847e3
f2a689
		if (toks_daemon_init(&toks_daemon_ctx,&ctx->uuid))
f2a689
			return toks_get_driver_ctx_fail(meta);
c847e3
	}
f2a689
486fcd
	if (cctx.drvflags & TOKS_DRIVER_MODE_CLIENT)
486fcd
		if (toks_client_connect(&ctx->ctx))
486fcd
			return toks_get_driver_ctx_fail(meta);
486fcd
2420c2
	*pctx = &ctx->ctx;
2420c2
	return TOKS_OK;
2420c2
}
2420c2
2420c2
static void toks_free_driver_ctx_impl(struct toks_driver_ctx_alloc * ictx)
2420c2
{
2f270e
	if (ictx->ctx.hevent)
2f270e
		ntapi->zw_close(ictx->ctx.hevent);
2f270e
00069c
	if (ictx->ctx.hsvcdir)
00069c
		ntapi->zw_close(ictx->ctx.hsvcdir);
00069c
00069c
	if (ictx->ctx.hsvclink)
00069c
		ntapi->zw_close(ictx->ctx.hsvclink);
00069c
c847e3
	if (ictx->ctx.tokens)
c847e3
		toks_free(ictx->ctx.tokens);
c847e3
2420c2
	argv_free(ictx->meta);
2420c2
	free(ictx);
2420c2
}
2420c2
2420c2
void toks_free_driver_ctx(struct toks_driver_ctx * ctx)
2420c2
{
2420c2
	struct toks_driver_ctx_alloc *	ictx;
2420c2
	uintptr_t			addr;
2420c2
2420c2
	if (ctx) {
2420c2
		addr = (uintptr_t)ctx - offsetof(struct toks_driver_ctx_impl,ctx);
2420c2
		addr = addr - offsetof(struct toks_driver_ctx_alloc,ctx);
2420c2
		ictx = (struct toks_driver_ctx_alloc *)addr;
2420c2
		toks_free_driver_ctx_impl(ictx);
2420c2
	}
2420c2
}
2420c2
2420c2
const struct toks_source_version * toks_source_version(void)
2420c2
{
2420c2
	return &toks_src_version;
2420c2
}