Blame src/fs/ntapi_tt_open_physical_parent_directory.c

dd89bb
/********************************************************/
dd89bb
/*  ntapi: Native API core library                      */
dde53a
/*  Copyright (C) 2013--2017  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,
baceb0
	__out	void *		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
{
8bdef0
	int32_t			status;
8bdef0
	nt_oa			oa;
8bdef0
	nt_iosb			iosb;
8bdef0
	wchar16_t *		wch;
8bdef0
	wchar16_t *		root;
8bdef0
	nt_unicode_string *	path;
8bdef0
	uint32_t		len;
8bdef0
	int			mup;
baceb0
	uintptr_t		addr;
baceb0
	uintptr_t		addr_cap;
dd89bb
4d8da7
	(void)reserved;
4d8da7
baceb0
	addr     = (uintptr_t)buffer;
baceb0
	addr_cap = addr + buffer_size;
baceb0
baceb0
	addr += 0xf;
baceb0
	addr |= 0xf;
baceb0
	addr ^= 0xf;
baceb0
baceb0
	path        = (nt_unicode_string *)addr;
baceb0
	buffer_size = addr_cap - addr;
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
895156
	if ((wch < &root[8])
895156
			|| (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
895156
	mup = (wch > &root[11])
895156
		&& (root[8]=='M')
895156
		&& (root[9]=='u')
895156
		&& (root[10]=='p')
895156
		&& (root[11]=='\\');
895156
895156
	root = mup ? &root[12] : &root[8];
895156
895156
	for (; (root
8076b5
		root++;
8076b5
895156
	if (root == wch)
895156
		return NT_STATUS_INTERNAL_ERROR;
895156
895156
	if (mup)
895156
		for (root++; (root
895156
			root++;
895156
895156
	if (root == wch)
895156
		return NT_STATUS_INTERNAL_ERROR;
895156
895156
	if (&root[1] == wch)
dd89bb
		return NT_STATUS_MORE_PROCESSING_REQUIRED;
dd89bb
895156
	if (wch[-1] == '\\')
895156
		wch--;
895156
8076b5
	/* physical parent directory path */
895156
	for (root++; (wch>=root) && (wch[-1]!='\\'); )
8076b5
		wch--;
8076b5
895156
	path->strlen = (uint16_t)(wch - path->buffer) * sizeof(uint16_t);
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
}