2 Unix SMB/CIFS implementation.
3 test suite for winreg rpc operations
5 Copyright (C) Tim Potter 2003
6 Copyright (C) Jelmer Vernooij 2004
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 static void init_winreg_String(struct winreg_String *name, const char *s)
29 name->name_len = 2 * (strlen_m(s) + 1);
30 name->name_size = name->name_len;
37 static BOOL test_GetVersion(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
38 struct policy_handle *handle)
41 struct winreg_GetVersion r;
43 printf("\ntesting GetVersion\n");
47 status = dcerpc_winreg_GetVersion(p, mem_ctx, &r);
49 if (!NT_STATUS_IS_OK(status)) {
50 printf("GetVersion failed - %s\n", nt_errstr(status));
54 if (!W_ERROR_IS_OK(r.out.result)) {
55 printf("GetVersion failed - %s\n", win_errstr(r.out.result));
62 static BOOL test_CreateKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
63 struct policy_handle *handle, const char *name,
66 struct winreg_CreateKey r;
67 struct policy_handle newhandle;
69 uint32_t sec_info = 0;
71 printf("\ntesting CreateKey\n");
74 r.out.handle = &newhandle;
75 init_winreg_String(&r.in.key, name);
76 init_winreg_String(&r.in.class, class);
78 r.in.access_mask = 0x02000000;
79 r.in.sec_info = &sec_info;
82 status = dcerpc_winreg_CreateKey(p, mem_ctx, &r);
84 if (!NT_STATUS_IS_OK(status)) {
85 printf("CreateKey failed - %s\n", nt_errstr(status));
89 if (!W_ERROR_IS_OK(r.out.result)) {
90 printf("CreateKey failed - %s\n", win_errstr(r.out.result));
97 static BOOL test_CloseKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
98 struct policy_handle *handle)
101 struct winreg_CloseKey r;
103 printf("\ntesting CloseKey\n");
105 r.in.handle = r.out.handle = handle;
107 status = dcerpc_winreg_CloseKey(p, mem_ctx, &r);
109 if (!NT_STATUS_IS_OK(status)) {
110 printf("CloseKey failed - %s\n", nt_errstr(status));
114 if (!W_ERROR_IS_OK(r.out.result)) {
115 printf("CloseKey failed - %s\n", win_errstr(r.out.result));
122 static BOOL test_FlushKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
123 struct policy_handle *handle)
126 struct winreg_FlushKey r;
128 printf("\ntesting FlushKey\n");
130 r.in.handle = handle;
132 status = dcerpc_winreg_FlushKey(p, mem_ctx, &r);
134 if (!NT_STATUS_IS_OK(status)) {
135 printf("FlushKey failed - %s\n", nt_errstr(status));
139 if (!W_ERROR_IS_OK(r.out.result)) {
140 printf("FlushKey failed - %s\n", win_errstr(r.out.result));
147 static BOOL test_OpenKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
148 struct policy_handle *hive_handle,
149 const char *keyname, struct policy_handle *key_handle)
152 struct winreg_OpenKey r;
154 printf("\ntesting OpenKey\n");
156 r.in.handle = hive_handle;
157 init_winreg_String(&r.in.keyname, keyname);
158 r.in.unknown = 0x00000000;
159 r.in.access_mask = 0x02000000;
160 r.out.handle = key_handle;
162 status = dcerpc_winreg_OpenKey(p, mem_ctx, &r);
164 if (!NT_STATUS_IS_OK(status)) {
165 printf("OpenKey failed - %s\n", nt_errstr(status));
169 if (!W_ERROR_IS_OK(r.out.result)) {
170 printf("OpenKey failed - %s\n", win_errstr(r.out.result));
177 static BOOL test_DeleteKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
178 struct policy_handle *handle, const char *key)
181 struct winreg_DeleteKey r;
183 printf("\ntesting DeleteKey\n");
185 r.in.handle = handle;
186 init_winreg_String(&r.in.key, key);
188 status = dcerpc_winreg_DeleteKey(p, mem_ctx, &r);
190 if (!NT_STATUS_IS_OK(status)) {
191 printf("DeleteKey failed - %s\n", nt_errstr(status));
195 if (!W_ERROR_IS_OK(r.out.result)) {
196 printf("DeleteKey failed - %s\n", win_errstr(r.out.result));
203 static BOOL test_QueryInfoKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
204 struct policy_handle *handle, char *class)
207 struct winreg_QueryInfoKey r;
209 printf("\ntesting QueryInfoKey\n");
211 r.in.handle = handle;
212 init_winreg_String(&r.in.class, class);
214 status = dcerpc_winreg_QueryInfoKey(p, mem_ctx, &r);
216 if (!NT_STATUS_IS_OK(status)) {
217 printf("QueryInfoKey failed - %s\n", nt_errstr(status));
221 if (!W_ERROR_IS_OK(r.out.result)) {
222 printf("QueryInfoKey failed - %s\n", win_errstr(r.out.result));
229 static BOOL test_key(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
230 struct policy_handle *handle, int depth);
232 static BOOL test_EnumKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
233 struct policy_handle *handle, int depth)
235 struct winreg_EnumKey r;
236 struct winreg_EnumKeyNameRequest keyname;
237 struct winreg_String classname;
238 struct winreg_Time tm;
241 printf("Testing EnumKey\n\n");
243 r.in.handle = handle;
245 r.in.key_name_len = r.out.key_name_len = 0;
246 r.in.unknown = r.out.unknown = 0x0414;
247 keyname.unknown = 0x0000020a;
248 init_winreg_String(&keyname.key_name, NULL);
249 init_winreg_String(&classname, NULL);
250 r.in.in_name = &keyname;
251 r.in.class = &classname;
252 tm.low = tm.high = 0x7fffffff;
253 r.in.last_changed_time = &tm;
256 status = dcerpc_winreg_EnumKey(p, mem_ctx, &r);
258 if (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(r.out.result)) {
259 struct policy_handle key_handle;
262 p, mem_ctx, handle, r.out.out_name->name,
264 printf("OpenKey(%s) failed - %s\n",
265 r.out.out_name->name,
266 win_errstr(r.out.result));
270 test_key(p, mem_ctx, &key_handle, depth + 1);
277 } while (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(r.out.result));
282 static BOOL test_EnumValue(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
283 struct policy_handle *handle, int max_valnamelen, int max_valbufsize)
285 struct winreg_EnumValue r;
286 struct EnumValueIn buf_name;
287 struct EnumValueIn buf_val;
289 uint32 len1 = max_valbufsize, len2 = 0;
291 printf("testing EnumValue\n");
293 r.in.handle = handle;
295 r.in.name_in.len = 0;
296 r.in.name_in.max_len = max_valnamelen * 2;
297 buf_name.max_len = max_valnamelen;
300 r.in.name_in.buffer = &buf_name;
302 buf_val.max_len = max_valbufsize;
305 r.in.value_in = &buf_val;
306 r.in.value_len1 = &len1;
307 r.in.value_len2 = &len2;
310 NTSTATUS status = dcerpc_winreg_EnumValue(p, mem_ctx, &r);
311 if(NT_STATUS_IS_ERR(status)) {
312 printf("EnumValue failed - %s\n", nt_errstr(status));
317 } while (W_ERROR_IS_OK(r.out.result));
319 if(!W_ERROR_EQUAL(r.out.result, WERR_NO_MORE_ITEMS)) {
320 printf("EnumValue failed - %s\n", win_errstr(r.out.result));
327 static BOOL test_OpenHKLM(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
328 struct policy_handle *handle)
331 struct winreg_OpenHKLM r;
332 struct winreg_OpenUnknown unknown;
335 printf("\ntesting OpenHKLM\n");
337 unknown.unknown0 = 0x84e0;
338 unknown.unknown1 = 0x0000;
339 r.in.unknown = &unknown;
340 r.in.access_required = SEC_RIGHTS_MAXIMUM_ALLOWED;
341 r.out.handle = handle;
343 status = dcerpc_winreg_OpenHKLM(p, mem_ctx, &r);
345 if (!NT_STATUS_IS_OK(status)) {
346 printf("OpenHKLM failed - %s\n", nt_errstr(status));
350 if (!W_ERROR_IS_OK(r.out.result)) {
351 printf("OpenHKLM failed - %s\n", win_errstr(r.out.result));
358 static BOOL test_OpenHKU(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
359 struct policy_handle *handle)
362 struct winreg_OpenHKU r;
363 struct winreg_OpenUnknown unknown;
366 printf("\ntesting OpenHKU\n");
368 unknown.unknown0 = 0x84e0;
369 unknown.unknown1 = 0x0000;
370 r.in.unknown = &unknown;
371 r.in.access_required = SEC_RIGHTS_MAXIMUM_ALLOWED;
372 r.out.handle = handle;
374 status = dcerpc_winreg_OpenHKU(p, mem_ctx, &r);
376 if (!NT_STATUS_IS_OK(status)) {
377 printf("OpenHKU failed - %s\n", nt_errstr(status));
384 static BOOL test_OpenHKCR(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
385 struct policy_handle *handle)
388 struct winreg_OpenHKCR r;
389 struct winreg_OpenUnknown unknown;
392 printf("\ntesting OpenHKCR\n");
394 unknown.unknown0 = 0x84e0;
395 unknown.unknown1 = 0x0000;
396 r.in.unknown = &unknown;
397 r.in.access_required = SEC_RIGHTS_MAXIMUM_ALLOWED;
398 r.out.handle = handle;
400 status = dcerpc_winreg_OpenHKCR(p, mem_ctx, &r);
402 if (!NT_STATUS_IS_OK(status)) {
403 printf("OpenHKCR failed - %s\n", nt_errstr(status));
410 static BOOL test_InitiateSystemShutdown(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
411 const char *msg, uint32_t timeout)
413 struct winreg_InitiateSystemShutdown r;
416 init_winreg_String(&r.in.message, msg);
418 r.in.timeout = timeout;
420 status = dcerpc_winreg_InitiateSystemShutdown(p, mem_ctx, &r);
422 if (!NT_STATUS_IS_OK(status)) {
423 printf("InitiateSystemShutdown failed - %s\n", nt_errstr(status));
427 if (!W_ERROR_IS_OK(r.out.result)) {
428 printf("InitiateSystemShutdown failed - %s\n", win_errstr(r.out.result));
435 static BOOL test_AbortSystemShutdown(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
437 struct winreg_AbortSystemShutdown r;
439 uint16_t server = 0x0;
441 r.in.server = &server;
443 status = dcerpc_winreg_AbortSystemShutdown(p, mem_ctx, &r);
445 if (!NT_STATUS_IS_OK(status)) {
446 printf("AbortSystemShutdown failed - %s\n", nt_errstr(status));
450 if (!W_ERROR_IS_OK(r.out.result)) {
451 printf("AbortSystemShutdown failed - %s\n", win_errstr(r.out.result));
458 static BOOL test_OpenHKCU(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
459 struct policy_handle *handle)
462 struct winreg_OpenHKCU r;
463 struct winreg_OpenUnknown unknown;
466 printf("\ntesting OpenHKCU\n");
468 unknown.unknown0 = 0x84e0;
469 unknown.unknown1 = 0x0000;
470 r.in.unknown = &unknown;
471 r.in.access_required = SEC_RIGHTS_MAXIMUM_ALLOWED;
472 r.out.handle = handle;
474 status = dcerpc_winreg_OpenHKCU(p, mem_ctx, &r);
476 if (!NT_STATUS_IS_OK(status)) {
477 printf("OpenHKCU failed - %s\n", nt_errstr(status));
484 #define MAX_DEPTH 2 /* Only go this far down the tree */
486 static BOOL test_key(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
487 struct policy_handle *handle, int depth)
489 if (depth == MAX_DEPTH)
492 if (!test_QueryInfoKey(p, mem_ctx, handle, NULL)) {
495 if (!test_EnumKey(p, mem_ctx, handle, depth)) {
498 if (!test_EnumValue(p, mem_ctx, handle, 0xFF, 0xFFFF)) {
501 test_CloseKey(p, mem_ctx, handle);
506 typedef BOOL winreg_open_fn(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
507 struct policy_handle *handle);
509 static BOOL test_Open(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, void *fn)
511 struct policy_handle handle, newhandle;
513 winreg_open_fn *open_fn = (winreg_open_fn *)fn;
515 if (!open_fn(p, mem_ctx, &handle)) {
519 if (!test_CreateKey(p, mem_ctx, &handle, "spottyfoot", NULL)) {
520 printf("CreateKey failed\n");
524 if (!test_FlushKey(p, mem_ctx, &handle)) {
525 printf("FlushKey failed\n");
529 if (!test_OpenKey(p, mem_ctx, &handle, "spottyfoot", &newhandle)) {
530 printf("CreateKey failed (OpenKey after Create didn't work)\n");
534 if (!test_DeleteKey(p, mem_ctx, &handle, "spottyfoot")) {
535 printf("DeleteKey failed\n");
539 if (!test_FlushKey(p, mem_ctx, &handle)) {
540 printf("FlushKey failed\n");
544 if (test_OpenKey(p, mem_ctx, &handle, "spottyfoot", &newhandle)) {
545 printf("DeleteKey failed (OpenKey after Delete didn't work)\n");
549 if (!test_GetVersion(p, mem_ctx, &handle)) {
550 printf("GetVersion failed\n");
554 /* The HKCR hive has a very large fanout */
556 if (open_fn == test_OpenHKCR) {
557 if(!test_key(p, mem_ctx, &handle, MAX_DEPTH - 1)) {
562 if(!test_key(p, mem_ctx, &handle, 0)) {
569 BOOL torture_rpc_winreg(void)
572 struct dcerpc_pipe *p;
575 winreg_open_fn *open_fns[] = { test_OpenHKLM, test_OpenHKU,
576 test_OpenHKCR, test_OpenHKCU };
579 mem_ctx = talloc_init("torture_rpc_winreg");
581 status = torture_rpc_connection(&p,
584 DCERPC_WINREG_VERSION);
586 if (!NT_STATUS_IS_OK(status)) {
590 if(!test_InitiateSystemShutdown(p, mem_ctx, "spottyfood", 30))
593 if(!test_AbortSystemShutdown(p, mem_ctx))
596 for (i = 0; i < ARRAY_SIZE(open_fns); i++) {
597 if (!test_Open(p, mem_ctx, open_fns[i]))
601 talloc_destroy(mem_ctx);
603 torture_rpc_close(p);