2 Unix SMB/CIFS implementation.
3 test suite for winreg rpc operations
5 Copyright (C) Tim Potter 2003
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 static void init_winreg_String(struct winreg_String *name, const char *s)
28 name->name_len = 2 * (strlen_m(s) + 1);
29 name->name_size = name->name_len;
36 static BOOL test_GetVersion(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
37 struct policy_handle *handle)
40 struct winreg_GetVersion r;
42 printf("\ntesting GetVersion\n");
46 status = dcerpc_winreg_GetVersion(p, mem_ctx, &r);
48 if (!NT_STATUS_IS_OK(status)) {
49 printf("GetVersion failed - %s\n", nt_errstr(status));
56 static BOOL test_CloseKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
57 struct policy_handle *handle)
60 struct winreg_CloseKey r;
62 printf("\ntesting CloseKey\n");
64 r.in.handle = r.out.handle = handle;
66 status = dcerpc_winreg_CloseKey(p, mem_ctx, &r);
68 if (!NT_STATUS_IS_OK(status)) {
69 printf("CloseKey failed - %s\n", nt_errstr(status));
76 static BOOL test_FlushKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
77 struct policy_handle *handle)
80 struct winreg_FlushKey r;
82 printf("\ntesting FlushKey\n");
86 status = dcerpc_winreg_FlushKey(p, mem_ctx, &r);
88 if (!NT_STATUS_IS_OK(status)) {
89 printf("FlushKey failed - %s\n", nt_errstr(status));
96 static BOOL test_OpenKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
97 struct policy_handle *hive_handle,
98 char *keyname, struct policy_handle *key_handle)
101 struct winreg_OpenKey r;
103 printf("\ntesting OpenKey\n");
105 r.in.handle = hive_handle;
106 init_winreg_String(&r.in.keyname, keyname);
107 r.in.unknown = 0x00000000;
108 r.in.access_mask = 0x02000000;
109 r.out.handle = key_handle;
111 status = dcerpc_winreg_OpenKey(p, mem_ctx, &r);
113 if (!W_ERROR_IS_OK(r.out.result)) {
114 printf("OpenKey failed - %s\n", win_errstr(r.out.result));
121 static BOOL test_DeleteKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
122 struct policy_handle *handle, char *key)
125 struct winreg_DeleteKey r;
127 printf("\ntesting DeleteKey\n");
129 r.in.handle = handle;
130 init_winreg_String(&r.in.key, key);
132 status = dcerpc_winreg_DeleteKey(p, mem_ctx, &r);
134 if (!NT_STATUS_IS_OK(status)) {
135 printf("DeleteKey failed - %s\n", nt_errstr(status));
142 static BOOL test_QueryInfoKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
143 struct policy_handle *handle, char *class)
146 struct winreg_QueryInfoKey r;
148 printf("\ntesting QueryInfoKey\n");
150 r.in.handle = handle;
151 init_winreg_String(&r.in.class, class);
153 status = dcerpc_winreg_QueryInfoKey(p, mem_ctx, &r);
155 if (!W_ERROR_IS_OK(r.out.result)) {
156 printf("QueryInfoKey failed - %s\n", win_errstr(r.out.result));
163 static BOOL test_key(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
164 struct policy_handle *handle, int depth);
166 static BOOL test_EnumKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
167 struct policy_handle *handle, int depth)
169 struct winreg_EnumKey r;
170 struct winreg_EnumKeyNameRequest keyname;
171 struct winreg_String classname;
172 struct winreg_Time tm;
175 r.in.handle = handle;
177 r.in.key_name_len = r.out.key_name_len = 0;
178 r.in.unknown = r.out.unknown = 0x0414;
179 keyname.unknown = 0x0000020a;
180 init_winreg_String(&keyname.key_name, NULL);
181 init_winreg_String(&classname, NULL);
182 r.in.in_name = &keyname;
183 r.in.class = &classname;
184 tm.low = tm.high = 0x7fffffff;
185 r.in.last_changed_time = &tm;
188 status = dcerpc_winreg_EnumKey(p, mem_ctx, &r);
190 if (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(r.out.result)) {
191 struct policy_handle key_handle;
194 p, mem_ctx, handle, r.out.out_name->name,
196 printf("OpenKey(%s) failed - %s\n",
197 r.out.out_name->name,
198 win_errstr(r.out.result));
202 test_key(p, mem_ctx, &key_handle, depth + 1);
209 } while (W_ERROR_IS_OK(r.out.result));
214 static BOOL test_EnumValue(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
215 struct policy_handle *handle)
217 struct winreg_EnumValue r;
218 struct winreg_EnumValueName name;
219 struct winreg_Uint8buf value;
220 struct winreg_Uint16buf buf;
221 uint32 type, requested_len, returned_len;
224 r.in.handle = handle;
227 buf.max_len = 0x7fff;
233 name.max_len = buf.max_len * 2;
236 r.in.name = r.out.name = &name;
241 value.max_len = 0xffff;
248 requested_len = value.max_len;
249 r.in.requested_len = &requested_len;
251 r.in.returned_len = &returned_len;
255 status = dcerpc_winreg_EnumValue(p, mem_ctx, &r);
257 } while (W_ERROR_IS_OK(r.out.result));
262 static BOOL test_OpenHKLM(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
263 struct policy_handle *handle)
266 struct winreg_OpenHKLM r;
267 struct winreg_OpenUnknown unknown;
270 printf("\ntesting OpenHKLM\n");
272 unknown.unknown0 = 0x84e0;
273 unknown.unknown1 = 0x0000;
274 r.in.unknown = &unknown;
275 r.in.access_required = SEC_RIGHTS_MAXIMUM_ALLOWED;
276 r.out.handle = handle;
278 status = dcerpc_winreg_OpenHKLM(p, mem_ctx, &r);
280 if (!NT_STATUS_IS_OK(status)) {
281 printf("OpenHKLM failed - %s\n", nt_errstr(status));
288 static BOOL test_OpenHKU(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
289 struct policy_handle *handle)
292 struct winreg_OpenHKU r;
293 struct winreg_OpenUnknown unknown;
296 printf("\ntesting OpenHKU\n");
298 unknown.unknown0 = 0x84e0;
299 unknown.unknown1 = 0x0000;
300 r.in.unknown = &unknown;
301 r.in.access_required = SEC_RIGHTS_MAXIMUM_ALLOWED;
302 r.out.handle = handle;
304 status = dcerpc_winreg_OpenHKU(p, mem_ctx, &r);
306 if (!NT_STATUS_IS_OK(status)) {
307 printf("OpenHKU failed - %s\n", nt_errstr(status));
314 static BOOL test_OpenHKCR(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
315 struct policy_handle *handle)
318 struct winreg_OpenHKCR r;
319 struct winreg_OpenUnknown unknown;
322 printf("\ntesting OpenHKCR\n");
324 unknown.unknown0 = 0x84e0;
325 unknown.unknown1 = 0x0000;
326 r.in.unknown = &unknown;
327 r.in.access_required = SEC_RIGHTS_MAXIMUM_ALLOWED;
328 r.out.handle = handle;
330 status = dcerpc_winreg_OpenHKCR(p, mem_ctx, &r);
332 if (!NT_STATUS_IS_OK(status)) {
333 printf("OpenHKCR failed - %s\n", nt_errstr(status));
340 static BOOL test_OpenHKCU(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
341 struct policy_handle *handle)
344 struct winreg_OpenHKCU r;
345 struct winreg_OpenUnknown unknown;
348 printf("\ntesting OpenHKCU\n");
350 unknown.unknown0 = 0x84e0;
351 unknown.unknown1 = 0x0000;
352 r.in.unknown = &unknown;
353 r.in.access_required = SEC_RIGHTS_MAXIMUM_ALLOWED;
354 r.out.handle = handle;
356 status = dcerpc_winreg_OpenHKCU(p, mem_ctx, &r);
358 if (!NT_STATUS_IS_OK(status)) {
359 printf("OpenHKCU failed - %s\n", nt_errstr(status));
366 #define MAX_DEPTH 2 /* Only go this far down the tree */
368 static BOOL test_key(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
369 struct policy_handle *handle, int depth)
371 if (depth == MAX_DEPTH)
374 if (!test_QueryInfoKey(p, mem_ctx, handle, NULL)) {
377 if (!test_EnumKey(p, mem_ctx, handle, depth)) {
380 if (!test_EnumValue(p, mem_ctx, handle)) {
383 /* Enumerate values */
385 test_CloseKey(p, mem_ctx, handle);
390 typedef BOOL winreg_open_fn(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
391 struct policy_handle *handle);
393 static BOOL test_Open(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, void *fn)
395 struct policy_handle handle;
396 winreg_open_fn *open_fn = (winreg_open_fn *)fn;
398 if (!open_fn(p, mem_ctx, &handle))
401 if (!test_GetVersion(p, mem_ctx, &handle)) {
402 printf("GetVersion failed\n");
406 if (!test_FlushKey(p, mem_ctx, &handle)) {
407 printf("FlushKey failed\n");
411 if (!test_DeleteKey(p, mem_ctx, &handle, "spottyfoot")) {
412 printf("DeleteKey failed\n");
416 /* The HKCR hive has a very large fanout */
418 if (open_fn == test_OpenHKCR)
419 return test_key(p, mem_ctx, &handle, MAX_DEPTH - 1);
421 return test_key(p, mem_ctx, &handle, 0);
424 BOOL torture_rpc_winreg(int dummy)
427 struct dcerpc_pipe *p;
430 winreg_open_fn *open_fns[] = { test_OpenHKLM, test_OpenHKU,
431 test_OpenHKCR, test_OpenHKCU };
434 mem_ctx = talloc_init("torture_rpc_winreg");
436 status = torture_rpc_connection(&p,
439 DCERPC_WINREG_VERSION);
441 if (!NT_STATUS_IS_OK(status)) {
445 for (i = 0; i < ARRAY_SIZE(open_fns); i++) {
446 if (!test_Open(p, mem_ctx, open_fns[i]))
450 talloc_destroy(mem_ctx);
452 torture_rpc_close(p);