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/nt_object.h>
#include <ntapi/nt_fsctl.h>
#include <ntapi/nt_guid.h>
#include <ntapi/ntapi.h>

#include <toksvc/toksvc.h>
#include "toksvc_init_impl.h"
#include "toksvc_driver_impl.h"
#include "toksvc_nolibc_impl.h"

static int32_t toks_uuid_path_open(void ** hfile, const char * path)
{
	return toks_open_dir(hfile,0,path,true)
		? toks_open_file(hfile,0,path,true)
		: NT_STATUS_SUCCESS;
}

int toks_uuid_from_path(const char * path, struct toks_uuid * uuid)
{
	int32_t                         status;
	void *				hfile;
	void *				hevent;
	nt_iosb                         iosb;
	nt_file_object_id_buffer        objid;

	toks_init();

	if ((status = toks_uuid_path_open(&hfile,path)))
		return status;

	status = ntapi->tt_create_private_event(
		&hevent,
		NT_NOTIFICATION_EVENT,
		NT_EVENT_NOT_SIGNALED);

	if (status) {
		ntapi->zw_close(hfile);
		return status;
	}

	iosb.status = NT_STATUS_PENDING;

	status = ntapi->zw_fs_control_file(
		hfile,
		hevent,0,0,
		&iosb,
		NT_FSCTL_CREATE_OR_GET_OBJECT_ID,
		0,0,&objid,sizeof(objid));

	if (status == NT_STATUS_PENDING)
		ntapi->zw_wait_for_single_object(
			hevent,
			NT_SYNC_NON_ALERTABLE,
			0);

	ntapi->zw_close(hfile);
	ntapi->zw_close(hevent);

	if (iosb.status == NT_STATUS_SUCCESS) {
		uuid->data1    = (objid.obj_id[0]);
		uuid->data1   |= (objid.obj_id[1] << 8);
		uuid->data1   |= (objid.obj_id[2] << 16);
		uuid->data1   |= (objid.obj_id[3] << 24);

		uuid->data2    = (objid.obj_id[4]);
		uuid->data2   |= (objid.obj_id[5] << 8);

		uuid->data3    = (objid.obj_id[6]);
		uuid->data3   |= (objid.obj_id[7] << 8);

		uuid->data4[0] = (objid.obj_id[8]);
		uuid->data4[1] = (objid.obj_id[9]);
		uuid->data4[2] = (objid.obj_id[10]);
		uuid->data4[3] = (objid.obj_id[11]);
		uuid->data4[4] = (objid.obj_id[12]);
		uuid->data4[5] = (objid.obj_id[13]);
		uuid->data4[6] = (objid.obj_id[14]);
		uuid->data4[7] = (objid.obj_id[15]);
	}

	return iosb.status;
}