Blame src/fs/ntapi_tt_open_physical_parent_directory.c

dd89bb
/********************************************************/
dd89bb
/*  ntapi: Native API core library                      */
4256e2
/*  Copyright (C) 2013--2016  Z. Gilboa                 */
dd89bb
/*  Released under GPLv2 and GPLv3; see COPYING.NTAPI.  */
dd89bb
/********************************************************/
dd89bb
dd89bb
#include <ntapi/ntapi.h>
dd89bb
#include <ntapi/nt_file.h>
dd89bb
#include "ntapi_impl.h"
dd89bb
dd89bb
int32_t __stdcall __ntapi_tt_open_physical_parent_directory(
dd89bb
	__out	void **		hparent,
dd89bb
	__in	void *		hdir,
dd89bb
	__out	uintptr_t *	buffer,
dd89bb
	__in	uint32_t	buffer_size,
a75a96
	__in	uint32_t	oattr,
dd89bb
	__in	uint32_t	desired_access,
a75a96
	__in	uint32_t	share_access,
4d8da7
	__in	uint32_t	open_options,
4d8da7
	__out	int *		reserved)
dd89bb
{
dd89bb
	int32_t				status;
dd89bb
	nt_oa				oa;
dd89bb
	nt_iosb				iosb;
dd89bb
	wchar16_t *			wch;
8076b5
	wchar16_t *			root;
dd89bb
	nt_unicode_string *		path;
dd89bb
	uint32_t			len;
dd89bb
4d8da7
	(void)reserved;
4d8da7
dd89bb
	path = (nt_unicode_string *)buffer;
dd89bb
dd89bb
	if ((status = __ntapi->zw_query_object(
dd89bb
			hdir,
dd89bb
			NT_OBJECT_NAME_INFORMATION,
dd89bb
			path,
dd89bb
			buffer_size,
dd89bb
			&len)))
dd89bb
		return status;
8076b5
8076b5
	/* integrity */
8076b5
	if (len == sizeof(nt_unicode_string))
dd89bb
		return NT_STATUS_BAD_FILE_TYPE;
dd89bb
8076b5
	/* device root directory */
8076b5
	root = path->buffer;
8076b5
	wch  = path->buffer + (path->strlen / sizeof(uint16_t));
8076b5
dd89bb
8076b5
	if ((root[0] != '\\')
8076b5
			|| (root[1] != 'D') || (root[2] != 'e')
8076b5
			|| (root[3] != 'v') || (root[4] != 'i')
8076b5
			|| (root[5] != 'c') || (root[6] != 'e')
8076b5
			|| (root[7] != '\\'))
8076b5
		return NT_STATUS_INTERNAL_ERROR;
4fdb5b
8076b5
	for (root=&root[8]; (root
8076b5
		root++;
8076b5
8076b5
	if ((uint16_t)((++root - path->buffer) * sizeof(uint16_t)) == path->strlen)
dd89bb
		return NT_STATUS_MORE_PROCESSING_REQUIRED;
dd89bb
8076b5
	/* physical parent directory path */
8076b5
	for (; (wch>root) && (wch[-1]!='\\'); )
8076b5
		wch--;
8076b5
dd89bb
	path->strlen = sizeof(uint16_t) * (uint16_t)(wch-path->buffer);
dd89bb
	path->maxlen = 0;
dd89bb
dd89bb
	/* oa */
dd89bb
	oa.len = sizeof(nt_oa);
dd89bb
	oa.root_dir = 0;
dd89bb
	oa.obj_name = path;
a75a96
	oa.obj_attr = oattr,
dd89bb
	oa.sec_desc = 0;
dd89bb
	oa.sec_qos  = 0;
dd89bb
dd89bb
	/* default access */
dd89bb
	desired_access = desired_access
dd89bb
		? desired_access
dd89bb
		: NT_SEC_SYNCHRONIZE | NT_FILE_READ_ATTRIBUTES | NT_FILE_READ_ACCESS;
dd89bb
dd89bb
	/* open parent directory */
dd89bb
	return __ntapi->zw_open_file(
dd89bb
		hparent,
dd89bb
		desired_access,
a75a96
		&oa,&iosb,
a75a96
		share_access,
dd89bb
		open_options | NT_FILE_DIRECTORY_FILE);
dd89bb
}