Blame src/driver/toks_amain.c

8b5736
/*********************************************************/
8b5736
/*  toksvc: a framework-native token broker service      */
8b5736
/*  Copyright (C) 2020  Z. Gilboa                        */
8b5736
/*  Released under GPLv2 and GPLv3; see COPYING.TOKSVC.  */
8b5736
/*********************************************************/
8b5736
8b5736
#include <ntapi/ntapi.h>
8b5736
#include <stdio.h>
8b5736
#include <unistd.h>
8b5736
8b5736
#include <toksvc/toksvc.h>
8b5736
#include "toksvc_init_impl.h"
8b5736
#include "toksvc_driver_impl.h"
8b5736
#include "toksvc_dprintf_impl.h"
8b5736
#include "toksvc_nolibc_impl.h"
b27e56
#include "toksvc_log_impl.h"
8b5736
8b5736
#ifndef TOKS_DRIVER_FLAGS
8b5736
#define TOKS_DRIVER_FLAGS	TOKS_DRIVER_VERBOSITY_ERRORS \
8b5736
				| TOKS_DRIVER_VERBOSITY_USAGE
8b5736
#endif
8b5736
8b5736
static const char vermsg[] = "%s%s%s (git://midipix.org/toksvc): "
8b5736
			     "version %s%d.%d.%d%s.\n"
8b5736
			     "[commit reference: %s%s%s]\n";
8b5736
8b5736
static const char * const toks_ver_color[6] = {
8b5736
		"\x1b[1m\x1b[35m","\x1b[0m",
8b5736
		"\x1b[1m\x1b[32m","\x1b[0m",
8b5736
		"\x1b[1m\x1b[34m","\x1b[0m"
8b5736
};
8b5736
8b5736
static const char * const toks_ver_plain[6] = {
8b5736
		"","",
8b5736
		"","",
8b5736
		"",""
8b5736
};
8b5736
8b5736
static ssize_t toks_version(struct toks_driver_ctx * dctx)
8b5736
{
8b5736
	const struct toks_source_version * verinfo;
8b5736
	const char * const * verclr;
8b5736
8b5736
	verinfo = toks_source_version();
8b5736
	verclr  = isatty(STDOUT_FILENO) ? toks_ver_color : toks_ver_plain;
8b5736
8b5736
	return toks_dprintf(
8b5736
			STDOUT_FILENO,vermsg,
8b5736
			verclr[0],dctx->program,verclr[1],
8b5736
			verclr[2],verinfo->major,verinfo->minor,
8b5736
			verinfo->revision,verclr[3],
8b5736
			verclr[4],verinfo->commit,verclr[5]);
8b5736
}
8b5736
8b5736
static int toks_exit(struct toks_driver_ctx * dctx, int nerrors)
8b5736
{
8b5736
	toks_free_driver_ctx(dctx);
8b5736
	return nerrors ? 2 : 0;
8b5736
}
8b5736
8b5736
int toks_main(char ** argv, char ** envp)
8b5736
{
8b5736
	int				ret;
b27e56
	int32_t				status;
8b5736
	struct toks_driver_ctx *	dctx;
2dcff1
	struct toks_token_string	toks;
8b5736
8b5736
	if ((ret = toks_init()))
8b5736
		return ret;
8b5736
8b5736
	if ((ret = toks_get_driver_ctx(argv,envp,TOKS_DRIVER_FLAGS,&dctx)))
8b5736
		return (ret == TOKS_USAGE)
8b5736
			? !argv || !argv[0] || !argv[1]
8b5736
			: 2;
8b5736
8b5736
	if (dctx->cctx->drvflags & TOKS_DRIVER_VERSION)
8b5736
		if ((toks_version(dctx)) < 0)
8b5736
			return toks_exit(dctx,2);
8b5736
2dcff1
	if (dctx->cctx->drvflags & TOKS_DRIVER_ACTION_ACQUIRE) {
b27e56
		ret = (status = toks_client_acquire(dctx))
02451d
			? 2 : 0;
02451d
b27e56
		switch (status) {
b27e56
			case NT_STATUS_SUCCESS:
b27e56
				break;
b27e56
b27e56
			case NT_STATUS_TIMEOUT:
b27e56
				toks_dprintf(STDERR_FILENO,
b27e56
					"%s: the operataion timed out [0x%x].\n",
b27e56
					toks_log_basename(argv[0]),status);
b27e56
				break;
b27e56
b27e56
			default:
b27e56
				toks_dprintf(STDERR_FILENO,
b27e56
					"%s: the operataion has failed [0x%x].\n",
b27e56
					toks_log_basename(argv[0]),status);
b27e56
		}
b27e56
2dcff1
		if (ret == 0) {
2dcff1
			toks_client_token_to_str(dctx,&toks);
2dcff1
			toks_dprintf(STDOUT_FILENO,"%s\n",toks.token);
2dcff1
		}
2dcff1
	}
2dcff1
867b30
	if (dctx->cctx->drvflags & TOKS_DRIVER_ACTION_RELEASE) {
867b30
		ret = toks_client_release(dctx)
867b30
			? 2 : 0;
867b30
867b30
		if (ret == 0) {
867b30
			toks_client_token_to_str(dctx,&toks);
867b30
			toks_dprintf(STDOUT_FILENO,"token %s has been released\n",toks.token);
867b30
		}
867b30
867b30
		if (ret == 2) {
867b30
			toks_dprintf(STDERR_FILENO,"error: invalid token");
867b30
		}
867b30
	}
867b30
520f51
	if (dctx->cctx->drvflags & TOKS_DRIVER_ACTION_ABORT) {
520f51
		ret = (status = toks_service_abort(dctx))
520f51
			? 2 : 0;
520f51
520f51
		switch (status) {
520f51
			case NT_STATUS_SUCCESS:
520f51
				toks_dprintf(STDERR_FILENO,
520f51
					"%s: the server responded with no error.\n",
520f51
					toks_log_basename(argv[0]),status);
520f51
				break;
520f51
520f51
			case NT_STATUS_ACCESS_DENIED:
520f51
				toks_dprintf(STDERR_FILENO,
520f51
					"%s: the abort operataion timed (access denied) [0x%x].\n",
520f51
					toks_log_basename(argv[0]),status);
520f51
				break;
520f51
520f51
			default:
520f51
				toks_dprintf(STDERR_FILENO,
520f51
					"%s: the abort operataion failed ",
520f51
					"(check the system's documentation) [0x%x].",
520f51
					toks_log_basename(argv[0]),status);
520f51
		}
520f51
	}
520f51
618937
	return (dctx->cctx->drvflags & TOKS_DRIVER_MODE_SERVER)
618937
		? NT_STATUS_SERVICE_NOTIFICATION
618937
		: toks_exit(dctx,ret);
8b5736
}