From 3fc30f8764bcc7c41edad9109cbb645c5564e40e Mon Sep 17 00:00:00 2001 From: midipix Date: Jun 26 2020 20:48:26 +0000 Subject: driver: toks_set_tmpfs_from_uuid(): initial implementation and integration. --- diff --git a/src/driver/toks_driver_ctx.c b/src/driver/toks_driver_ctx.c index 8853e17..efcfc86 100644 --- a/src/driver/toks_driver_ctx.c +++ b/src/driver/toks_driver_ctx.c @@ -22,6 +22,8 @@ #include "toksvc_driver_impl.h" #include "argv/argv.h" +#undef strlen + /* pty integration */ #include @@ -267,6 +269,135 @@ static int toks_split_argv( return 0; } +static int32_t toks_set_tmpfs_from_uuid(struct toks_driver_ctx_impl * ctx) +{ + int32_t status; + void * htmproot; + void * hsvcroot; + nt_oa oa; + nt_iosb iosb; + nt_unicode_string path; + uint32_t access; + nt_sd_common_buffer sd; + nt_large_integer alloc_size; + nt_guid_str_utf16 guidstr; + uint32_t access_owner; + uint32_t access_group; + uint32_t access_other; + uint32_t access_admin; + wchar16_t tmproot[3] = {'t','m','p'}; + wchar16_t toksvc[6] = {'t','o','k','s','v','c'}; + + /* sd */ + access_owner = NT_GENERIC_ALL|NT_SEC_SPECIFIC_RIGHTS_ALL; + access_admin = access_owner; + + access_group = NT_GENERIC_READ | NT_GENERIC_WRITE | NT_GENERIC_EXECUTE; + access_group |= NT_SEC_READ_CONTROL | NT_FILE_READ_ATTRIBUTES; + + access_other = NT_GENERIC_READ | NT_GENERIC_EXECUTE; + access_other |= NT_SEC_READ_CONTROL | NT_FILE_READ_ATTRIBUTES; + + ntapi->acl_init_common_descriptor( + &sd,0,0,0,0, + access_owner,access_group,access_other, + access_admin,access_owner, + 0); + + /* oa */ + oa.len = sizeof(nt_oa); + oa.root_dir = 0; + oa.obj_name = &path; + oa.obj_attr = 0; + oa.sec_desc = &sd.sd; + oa.sec_qos = 0; + + /* alloc_size (always zero) */ + alloc_size.quad = 0; + + /* access */ + access = NT_SEC_SYNCHRONIZE | NT_SEC_READ_CONTROL | NT_FILE_READ_ATTRIBUTES; + access |= NT_FILE_LIST_DIRECTORY | NT_FILE_ADD_FILE | NT_FILE_ADD_SUBDIRECTORY; + access |= NT_FILE_TRAVERSE | NT_FILE_DELETE_CHILD; + + /* /tmp or alternative */ + if (ctx->rtdata->htmpfslroot) { + if ((status = ntapi->zw_duplicate_object( + ctx->rtdata->hself, + ctx->rtdata->htmpfslroot, + ctx->rtdata->hself, + &htmproot, + 0,0, + NT_DUPLICATE_SAME_ATTRIBUTES|NT_DUPLICATE_SAME_ACCESS))) + return status; + } else { + path.maxlen = 0; + path.strlen = sizeof(tmproot); + path.buffer = tmproot; + + oa.root_dir = ctx->rtdata->hroot; + + if ((status = ntapi->zw_create_file( + &htmproot, + access,&oa,&iosb, + &alloc_size, + NT_FILE_ATTRIBUTE_DIRECTORY, + NT_FILE_SHARE_READ | NT_FILE_SHARE_WRITE | NT_FILE_SHARE_DELETE, + NT_FILE_OPEN_IF, + NT_FILE_DIRECTORY_FILE | NT_FILE_SYNCHRONOUS_IO_ALERT, + 0,0))) + return status; + } + + /* service tmpfs root */ + path.maxlen = 0; + path.strlen = sizeof(toksvc); + path.buffer = toksvc; + + oa.root_dir = htmproot; + + status = ntapi->zw_create_file( + &hsvcroot, + access,&oa,&iosb, + &alloc_size, + NT_FILE_ATTRIBUTE_DIRECTORY, + NT_FILE_SHARE_READ | NT_FILE_SHARE_WRITE | NT_FILE_SHARE_DELETE, + NT_FILE_OPEN_IF, + NT_FILE_DIRECTORY_FILE | NT_FILE_SYNCHRONOUS_IO_ALERT, + 0,0); + + if (status) { + ntapi->zw_close(htmproot); + return status; + } + + /* instance-specific tmpfs root */ + ntapi->tt_guid_to_string_utf16( + &ctx->uuid, + &guidstr); + + path.maxlen = 0; + path.strlen = sizeof(guidstr); + path.buffer = &guidstr.lbrace; + + oa.root_dir = hsvcroot; + + status = ntapi->zw_create_file( + &ctx->htmpfs, + access,&oa,&iosb, + &alloc_size, + NT_FILE_ATTRIBUTE_DIRECTORY, + NT_FILE_SHARE_READ | NT_FILE_SHARE_WRITE | NT_FILE_SHARE_DELETE, + NT_FILE_OPEN_IF, + NT_FILE_DIRECTORY_FILE | NT_FILE_SYNCHRONOUS_IO_ALERT, + 0,0); + + ntapi->zw_close(hsvcroot); + ntapi->zw_close(htmproot); + + return status; +} + static int32_t toks_set_uuid_from_root( struct toks_driver_ctx_impl * ctx, struct _nt_guid * svcguid) @@ -712,6 +843,15 @@ int toks_get_driver_ctx( toks_set_driver_refstr(&ctx->ctx,refstr); if (cctx.drvflags & TOKS_DRIVER_MODE_SERVER) { + if ((status = toks_set_tmpfs_from_uuid(ctx))) { + if (flags & TOKS_DRIVER_VERBOSITY_ERRORS) + toks_dprintf(STDERR_FILENO, + "%s: error: could not create or open " + "the instance-specific tmpfs directory [0x%08x].", + program,status); + return toks_get_driver_ctx_fail(meta); + } + if (!(ctx->waiters = toks_calloc( TOKS_MAX_WAITERS, sizeof(struct toks_waiter)))) @@ -821,6 +961,9 @@ int toks_get_driver_ctx( static void toks_free_driver_ctx_impl(struct toks_driver_ctx_alloc * ictx) { + if (ictx->ctx.htmpfs) + ntapi->zw_close(ictx->ctx.htmpfs); + if (ictx->ctx.hevent) ntapi->zw_close(ictx->ctx.hevent); diff --git a/src/internal/toksvc_driver_impl.h b/src/internal/toksvc_driver_impl.h index ad88393..a18939e 100644 --- a/src/internal/toksvc_driver_impl.h +++ b/src/internal/toksvc_driver_impl.h @@ -70,6 +70,7 @@ struct toks_driver_ctx_impl { void * hservice; void * hserver; void * hlog; + void * htmpfs; int nwaiters; int ntokens; int atokens;