diff --git a/include/toksvc/toksvc.h b/include/toksvc/toksvc.h index 71f490f..7e7128d 100644 --- a/include/toksvc/toksvc.h +++ b/include/toksvc/toksvc.h @@ -51,6 +51,8 @@ extern "C" { #define TOKS_DRIVER_MODE_SERVER 0X10000 #define TOKS_DRIVER_MODE_CLIENT 0X20000 +#define TOKS_DRIVER_ACTION_ACQUIRE 0X100000 + /* error flags */ #define TOKS_ERROR_TOP_LEVEL 0x0001 #define TOKS_ERROR_NESTED 0x0002 @@ -121,6 +123,7 @@ toks_api void toks_free_driver_ctx (struct toks_driver_ctx *); /* client api */ toks_api int toks_client_connect (struct toks_driver_ctx *); +toks_api int toks_client_acquire (struct toks_driver_ctx *); /* utility api */ toks_api int toks_main (char **, char **); diff --git a/project/common.mk b/project/common.mk index 7ef2ee9..525dc87 100644 --- a/project/common.mk +++ b/project/common.mk @@ -1,6 +1,7 @@ API_SRCS = \ INTERNAL_SRCS = \ + src/client/toks_client_acquire.c \ src/client/toks_client_connect.c \ src/daemon/toks_daemon_acquire.c \ src/daemon/toks_daemon_connect.c \ diff --git a/src/client/toks_client_acquire.c b/src/client/toks_client_acquire.c new file mode 100644 index 0000000..05dbab8 --- /dev/null +++ b/src/client/toks_client_acquire.c @@ -0,0 +1,46 @@ +/*********************************************************/ +/* toksvc: a framework-native token broker service */ +/* Copyright (C) 2020 Z. Gilboa */ +/* Released under GPLv2 and GPLv3; see COPYING.TOKSVC. */ +/*********************************************************/ + +#include +#include + +#include +#include "toksvc_driver_impl.h" +#include "toksvc_daemon_impl.h" + +int32_t toks_client_acquire(struct toks_driver_ctx * dctx) +{ + int32_t status; + void * hport; + struct _nt_tty_ipc_msg msg; + struct _nt_port_keys * keys; + + hport = toks_get_driver_hservice(dctx); + keys = toks_get_driver_keys(dctx); + + 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 = TOKS_DAEMON_ACQUIRE; + + if ((status = ntapi->zw_request_wait_reply_port(hport,&msg,&msg))) + return status; + + else if (msg.data.ttyinfo.status) + return msg.data.ttyinfo.status; + + keys->key[0] = msg.data.ipcinfo.ipckeys[0]; + keys->key[1] = msg.data.ipcinfo.ipckeys[1]; + keys->key[2] = msg.data.ipcinfo.ipckeys[2]; + keys->key[3] = msg.data.ipcinfo.ipckeys[3]; + keys->key[4] = msg.data.ipcinfo.ipckeys[4]; + keys->key[5] = msg.data.ipcinfo.ipckeys[5]; + + return NT_STATUS_SUCCESS; +} diff --git a/src/driver/toks_amain.c b/src/driver/toks_amain.c index b4b3764..45739e1 100644 --- a/src/driver/toks_amain.c +++ b/src/driver/toks_amain.c @@ -74,6 +74,10 @@ int toks_main(char ** argv, char ** envp) if ((toks_version(dctx)) < 0) return toks_exit(dctx,2); + if (dctx->cctx->drvflags & TOKS_DRIVER_ACTION_ACQUIRE) + ret = toks_client_acquire(dctx) + ? 2 : 0; + return (dctx->cctx->drvflags & TOKS_DRIVER_MODE_SERVER) ? NT_STATUS_SERVICE_NOTIFICATION : toks_exit(dctx,ret); diff --git a/src/driver/toks_driver_ctx.c b/src/driver/toks_driver_ctx.c index cbf025c..4c748f3 100644 --- a/src/driver/toks_driver_ctx.c +++ b/src/driver/toks_driver_ctx.c @@ -263,6 +263,12 @@ int toks_get_driver_ctx( cctx.drvflags &= ~(uint64_t)TOKS_DRIVER_MODE_SERVER; cctx.drvflags |= TOKS_DRIVER_MODE_CLIENT; break; + + case TAG_ACQUIRE: + cctx.drvflags &= ~(uint64_t)TOKS_DRIVER_MODE_SERVER; + cctx.drvflags |= TOKS_DRIVER_MODE_CLIENT; + cctx.drvflags |= TOKS_DRIVER_ACTION_ACQUIRE; + break; } } else /* strict */ diff --git a/src/internal/toksvc_driver_impl.h b/src/internal/toksvc_driver_impl.h index c0df86f..a643ae6 100644 --- a/src/internal/toksvc_driver_impl.h +++ b/src/internal/toksvc_driver_impl.h @@ -25,6 +25,7 @@ enum app_tags { TAG_UUID, TAG_CONNECT, TAG_TOKENS, + TAG_ACQUIRE, }; struct toks_ticks { @@ -38,6 +39,7 @@ struct toks_driver_ctx_impl { struct toks_driver_ctx ctx; struct toks_ticks ticks; struct toks_token * tokens; + struct _nt_port_keys keys; void * hevent; void * hsvcdir; void * hsvclink; @@ -114,6 +116,13 @@ static inline void toks_set_driver_hservice(const struct toks_driver_ctx * dctx, ictx->hservice = hservice; } +static inline struct _nt_port_keys * toks_get_driver_keys(const struct toks_driver_ctx * dctx) +{ + struct toks_driver_ctx_impl * ictx; + ictx = toks_get_driver_ictx(dctx); + return &ictx->keys; +} + static inline const nt_guid * toks_get_driver_uuid(const struct toks_driver_ctx * dctx) { struct toks_driver_ctx_impl * ictx; diff --git a/src/skin/toks_skin_default.c b/src/skin/toks_skin_default.c index 60a1c9f..2a806ea 100644 --- a/src/skin/toks_skin_default.c +++ b/src/skin/toks_skin_default.c @@ -29,5 +29,8 @@ const struct argv_option toks_default_options[] = { {"connect", 'c',TAG_CONNECT,ARGV_OPTARG_NONE,0,0,0, "connect to the server without placing any request"}, + {"acquire", 'a',TAG_ACQUIRE,ARGV_OPTARG_NONE,0,0,0, + "acquire the next available token"}, + {0,0,0,0,0,0,0,0} };