summaryrefslogtreecommitdiffhomepage
path: root/src/object/ntapi_tt_keyed_object_directory.c
blob: c744326cbbf35168e826a6542104008cb0267360 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/********************************************************/
/*  ntapi: Native API core library                      */
/*  Copyright (C) 2013--2017  Z. Gilboa                 */
/*  Released under GPLv2 and GPLv3; see COPYING.NTAPI.  */
/********************************************************/

#include <psxtypes/psxtypes.h>
#include <ntapi/nt_object.h>
#include <ntapi/nt_guid.h>
#include <ntapi/nt_string.h>
#include "ntapi_impl.h"

typedef ntapi_zw_open_directory_object objdir_open_fn;

static  nt_keyed_objdir_name __template_name = {__NT_BASED_NAMED_OBJECTS,
						0,
						{0,0,0,0,0,0},
						{0,{0},0},
						{0}};


static int32_t __stdcall __tt_create_ipc_object_directory(
	__out	void **			hdir,
	__in	uint32_t		desired_access,
	__in	const wchar16_t		prefix[6],
	__in	const nt_guid *		guid)
{
	int32_t			status;
	nt_ipc_objdir_name	objdir_name;
	nt_unicode_string	name;
	nt_oa			oa;
	nt_sqos			sqos = {
					sizeof(sqos),
					NT_SECURITY_IMPERSONATION,
					NT_SECURITY_TRACKING_DYNAMIC,
					1};

	__ntapi->tt_generic_memcpy(
		&objdir_name,
		&__template_name,
		sizeof(objdir_name));

	__ntapi->tt_memcpy_utf16(
		objdir_name.prefix,
		prefix,
		sizeof(objdir_name.prefix));

	__ntapi->tt_guid_to_string_utf16(
		guid,
		(nt_guid_str_utf16 *)&objdir_name.objdir_guid);

	objdir_name.backslash = '\\';
	objdir_name.objdir_guid.uscore_guid = '_';

	name.strlen	= sizeof(objdir_name);
	name.maxlen	= 0;
	name.buffer	= (uint16_t *)&objdir_name;

	oa.len		= sizeof(oa);
	oa.root_dir	= 0;
	oa.obj_name	= &name;
	oa.obj_attr	= NT_OBJ_INHERIT
			  | NT_OBJ_OPENIF
			  | NT_OBJ_CASE_INSENSITIVE;
	oa.sec_desc	= 0;
	oa.sec_qos	= &sqos;

	status = __ntapi->zw_create_directory_object(
		hdir,desired_access,&oa);

	return (status == NT_STATUS_OBJECT_NAME_EXISTS)
		? NT_STATUS_SUCCESS
		: status;
}


static int32_t __stdcall __tt_create_keyed_object_directory(
	__out	void **			hdir,
	__in	uint32_t		desired_access,
	__in	const wchar16_t		prefix[6],
	__in	const nt_guid *		guid,
	__in	uint32_t		key,
	__in	objdir_open_fn *	openfn)
{
	nt_keyed_objdir_name	objdir_name;
	nt_unicode_string	name;
	nt_oa			oa;
	nt_sqos			sqos = {
					sizeof(sqos),
					NT_SECURITY_IMPERSONATION,
					NT_SECURITY_TRACKING_DYNAMIC,
					1};

	__ntapi->tt_generic_memcpy(
		&objdir_name,
		&__template_name,
		sizeof(__template_name));

	__ntapi->tt_memcpy_utf16(
		objdir_name.prefix,
		prefix,
		sizeof(objdir_name.prefix));

	__ntapi->tt_guid_to_string_utf16(
		guid,
		(nt_guid_str_utf16 *)&objdir_name.objdir_guid);

	__ntapi->tt_uint32_to_hex_utf16(
		key,objdir_name.key);

	objdir_name.backslash = '\\';
	objdir_name.objdir_guid.uscore_guid = '_';
	objdir_name.objdir_guid.uscore_key  = '_';

	name.strlen	= sizeof(objdir_name);
	name.maxlen	= 0;
	name.buffer	= (uint16_t *)&objdir_name;

	oa.len		= sizeof(oa);
	oa.root_dir	= 0;
	oa.obj_name	= &name;
	oa.obj_attr	= NT_OBJ_INHERIT;
	oa.sec_desc	= 0;
	oa.sec_qos	= &sqos;

	return openfn(hdir,desired_access,&oa);
}


static int32_t __stdcall __tt_create_object_directory_entry(
	__out	void **			hentry,
	__in	uint32_t		desired_access,
	__in	void *			hdir,
	__in	void *			htarget,
	__in	nt_unicode_string *	target_name,
	__in	uint32_t		key)
{
	int32_t			status;
	nt_oa			oa;
	nt_unicode_string	name;
	wchar16_t		keystr[8];
	uintptr_t		buffer[2048/sizeof(uintptr_t)];
	nt_sqos			sqos = {
					sizeof(sqos),
					NT_SECURITY_IMPERSONATION,
					NT_SECURITY_TRACKING_DYNAMIC,
					1};

	if (!target_name) {
		if ((status = __ntapi->zw_query_object(
				htarget,
				NT_OBJECT_NAME_INFORMATION,
				buffer,sizeof(buffer),0)))
			return status;
		target_name = (nt_unicode_string *)buffer;
	}

	__ntapi->tt_uint32_to_hex_utf16(key,keystr);

	name.strlen = sizeof(keystr);
	name.maxlen = 0;
	name.buffer = keystr;

	oa.len		= sizeof(oa);
	oa.root_dir	= hdir;
	oa.obj_name	= &name;
	oa.obj_attr	= 0;
	oa.sec_desc	= 0;
	oa.sec_qos	= &sqos;

	return __ntapi->zw_create_symbolic_link_object(
		hentry,
		desired_access,
		&oa,target_name);
}

int32_t __stdcall __ntapi_tt_open_ipc_object_directory(
	__out	void **			hdir,
	__in	uint32_t		desired_access,
	__in	const wchar16_t		prefix[6],
	__in	const nt_guid *		guid)
{
	return __tt_create_ipc_object_directory(
		hdir,desired_access,
		prefix,guid);
}

int32_t __stdcall __ntapi_tt_create_ipc_object_directory_entry(
	__out	void **			hentry,
	__in	uint32_t		desired_access,
	__in	void *			hdir,
	__in	void *			htarget,
	__in	nt_unicode_string *	target_name,
	__in	uint32_t		key)
{
	return __tt_create_object_directory_entry(
		hentry,desired_access,
		hdir,htarget,target_name,key);
}

int32_t __stdcall __ntapi_tt_create_keyed_object_directory(
	__out	void **			hdir,
	__in	uint32_t		desired_access,
	__in	const wchar16_t		prefix[6],
	__in	const nt_guid *		guid,
	__in	uint32_t		key)
{
	return __tt_create_keyed_object_directory(
		hdir,desired_access,
		prefix,guid,key,
		__ntapi->zw_create_directory_object);
}

int32_t __stdcall __ntapi_tt_open_keyed_object_directory(
	__out	void **			hdir,
	__in	uint32_t		desired_access,
	__in	const wchar16_t		prefix[6],
	__in	const nt_guid *		guid,
	__in	uint32_t		key)
{
	return __tt_create_keyed_object_directory(
		hdir,desired_access,
		prefix,guid,key,
		__ntapi->zw_open_directory_object);
}

int32_t __stdcall __ntapi_tt_create_keyed_object_directory_entry(
	__out	void **			hentry,
	__in	uint32_t		desired_access,
	__in	void *			hdir,
	__in	void *			htarget,
	__in	nt_unicode_string *	target_name,
	__in	uint32_t		key)
{
	return __tt_create_object_directory_entry(
		hentry,desired_access,
		hdir,htarget,target_name,key);
}