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.
24 #include "librpc/gen_ndr/ndr_winreg.h"
26 static void init_winreg_String(struct winreg_String *name, const char *s)
30 name->name_len = 2 * (strlen_m(s) + 1);
31 name->name_size = name->name_len;
38 static BOOL test_GetVersion(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
39 struct policy_handle *handle)
42 struct winreg_GetVersion r;
44 printf("\ntesting GetVersion\n");
48 status = dcerpc_winreg_GetVersion(p, mem_ctx, &r);
50 if (!NT_STATUS_IS_OK(status)) {
51 printf("GetVersion failed - %s\n", nt_errstr(status));
55 if (!W_ERROR_IS_OK(r.out.result)) {
56 printf("GetVersion failed - %s\n", win_errstr(r.out.result));
63 static BOOL test_NotifyChangeKeyValue(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
64 struct policy_handle *handle)
66 struct winreg_NotifyChangeKeyValue r;
69 printf("\ntesting NotifyChangeKeyValue\n");
72 r.in.watch_subtree = 1;
73 r.in.notify_filter = 0;
74 r.in.unknown = r.in.unknown2 = 0;
75 init_winreg_String(&r.in.string1, NULL);
76 init_winreg_String(&r.in.string2, NULL);
78 status = dcerpc_winreg_NotifyChangeKeyValue(p, mem_ctx, &r);
80 if (!NT_STATUS_IS_OK(status)) {
81 printf("NotifyChangeKeyValue failed - %s\n", nt_errstr(status));
85 if (!W_ERROR_IS_OK(r.out.result)) {
86 printf("NotifyChangeKeyValue failed - %s\n", win_errstr(r.out.result));
93 static BOOL test_CreateKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
94 struct policy_handle *handle, const char *name,
97 struct winreg_CreateKey r;
98 struct policy_handle newhandle;
100 uint32_t action_taken = 0;
102 printf("\ntesting CreateKey\n");
104 r.in.handle = handle;
105 r.out.handle = &newhandle;
106 init_winreg_String(&r.in.key, name);
107 init_winreg_String(&r.in.class, class);
109 r.in.access_mask = 0x02000000;
110 r.in.action_taken = r.out.action_taken = &action_taken;
111 r.in.sec_desc = NULL;
113 status = dcerpc_winreg_CreateKey(p, mem_ctx, &r);
115 if (!NT_STATUS_IS_OK(status)) {
116 printf("CreateKey failed - %s\n", nt_errstr(status));
120 if (!W_ERROR_IS_OK(r.out.result)) {
121 printf("CreateKey failed - %s\n", win_errstr(r.out.result));
128 static BOOL test_GetKeySecurity(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
129 struct policy_handle *handle)
132 struct winreg_GetKeySecurity r;
134 printf("\ntesting GetKeySecurity\n");
138 r.in.handle = handle;
139 r.in.data = r.out.data = talloc_zero_p(mem_ctx, struct KeySecurityData);
140 r.in.data->size = 0xffff;
141 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
143 status = dcerpc_winreg_GetKeySecurity(p, mem_ctx, &r);
145 if (!NT_STATUS_IS_OK(status)) {
146 printf("GetKeySecurity failed - %s\n", nt_errstr(status));
150 if (!W_ERROR_IS_OK(r.out.result)) {
151 printf("GetKeySecurity failed - %s\n", win_errstr(r.out.result));
158 static BOOL test_CloseKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
159 struct policy_handle *handle)
162 struct winreg_CloseKey r;
164 printf("\ntesting CloseKey\n");
166 r.in.handle = r.out.handle = handle;
168 status = dcerpc_winreg_CloseKey(p, mem_ctx, &r);
170 if (!NT_STATUS_IS_OK(status)) {
171 printf("CloseKey failed - %s\n", nt_errstr(status));
175 if (!W_ERROR_IS_OK(r.out.result)) {
176 printf("CloseKey failed - %s\n", win_errstr(r.out.result));
183 static BOOL test_FlushKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
184 struct policy_handle *handle)
187 struct winreg_FlushKey r;
189 printf("\ntesting FlushKey\n");
191 r.in.handle = handle;
193 status = dcerpc_winreg_FlushKey(p, mem_ctx, &r);
195 if (!NT_STATUS_IS_OK(status)) {
196 printf("FlushKey failed - %s\n", nt_errstr(status));
200 if (!W_ERROR_IS_OK(r.out.result)) {
201 printf("FlushKey failed - %s\n", win_errstr(r.out.result));
208 static BOOL test_OpenKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
209 struct policy_handle *hive_handle,
210 const char *keyname, struct policy_handle *key_handle)
213 struct winreg_OpenKey r;
215 printf("\ntesting OpenKey\n");
217 r.in.handle = hive_handle;
218 init_winreg_String(&r.in.keyname, keyname);
219 r.in.unknown = 0x00000000;
220 r.in.access_mask = 0x02000000;
221 r.out.handle = key_handle;
223 status = dcerpc_winreg_OpenKey(p, mem_ctx, &r);
225 if (!NT_STATUS_IS_OK(status)) {
226 printf("OpenKey failed - %s\n", nt_errstr(status));
230 if (!W_ERROR_IS_OK(r.out.result)) {
231 printf("OpenKey failed - %s\n", win_errstr(r.out.result));
239 static BOOL test_DeleteKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
240 struct policy_handle *handle, const char *key)
243 struct winreg_DeleteKey r;
245 printf("\ntesting DeleteKey\n");
247 r.in.handle = handle;
248 init_winreg_String(&r.in.key, key);
250 status = dcerpc_winreg_DeleteKey(p, mem_ctx, &r);
252 if (!NT_STATUS_IS_OK(status)) {
253 printf("DeleteKey failed - %s\n", nt_errstr(status));
257 if (!W_ERROR_IS_OK(r.out.result)) {
258 printf("DeleteKey failed - %s\n", win_errstr(r.out.result));
265 static BOOL test_QueryInfoKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
266 struct policy_handle *handle, char *class)
269 struct winreg_QueryInfoKey r;
271 printf("\ntesting QueryInfoKey\n");
273 r.in.handle = handle;
274 init_winreg_String(&r.in.class, class);
276 status = dcerpc_winreg_QueryInfoKey(p, mem_ctx, &r);
278 if (!NT_STATUS_IS_OK(status)) {
279 printf("QueryInfoKey failed - %s\n", nt_errstr(status));
283 if (!W_ERROR_IS_OK(r.out.result)) {
284 printf("QueryInfoKey failed - %s\n", win_errstr(r.out.result));
291 static BOOL test_key(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
292 struct policy_handle *handle, int depth);
294 static BOOL test_EnumKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
295 struct policy_handle *handle, int depth)
297 struct winreg_EnumKey r;
298 struct winreg_EnumKeyNameRequest keyname;
299 struct winreg_String classname;
300 struct winreg_Time tm;
303 printf("Testing EnumKey\n\n");
305 r.in.handle = handle;
307 r.in.key_name_len = r.out.key_name_len = 0;
308 r.in.unknown = r.out.unknown = 0x0414;
309 keyname.unknown = 0x0000020a;
310 init_winreg_String(&keyname.key_name, NULL);
311 init_winreg_String(&classname, NULL);
312 r.in.in_name = &keyname;
313 r.in.class = &classname;
314 tm.low = tm.high = 0x7fffffff;
315 r.in.last_changed_time = &tm;
318 status = dcerpc_winreg_EnumKey(p, mem_ctx, &r);
320 if (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(r.out.result)) {
321 struct policy_handle key_handle;
323 printf("EnumKey: %d: %s\n", r.in.enum_index, r.out.out_name->name);
326 p, mem_ctx, handle, r.out.out_name->name,
329 test_key(p, mem_ctx, &key_handle, depth + 1);
335 } while (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(r.out.result));
337 if (!NT_STATUS_IS_OK(status)) {
338 printf("EnumKey failed - %s\n", nt_errstr(status));
342 if (!W_ERROR_IS_OK(r.out.result) && !W_ERROR_EQUAL(r.out.result, WERR_NO_MORE_ITEMS)) {
343 printf("EnumKey failed - %s\n", win_errstr(r.out.result));
352 static BOOL test_QueryMultipleValues(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, struct policy_handle *handle, const char *valuename)
354 struct winreg_QueryMultipleValues r;
357 printf("Testing QueryMultipleValues\n");
359 r.in.key_handle = handle;
360 r.in.values = r.out.values = talloc_array_p(mem_ctx, struct QueryMultipleValue, 1);
361 r.in.values[0].name = talloc_p(mem_ctx, struct winreg_String);
362 r.in.values[0].name->name = valuename;
363 r.in.values[0].offset = 0;
364 r.in.values[0].length = 0;
365 r.in.values[0].type = 0;
368 r.in.buffer_size = r.out.buffer_size = talloc_p(mem_ctx, uint32);
369 *r.in.buffer_size = 0x20;
370 r.in.buffer = r.out.buffer = talloc_zero_array(mem_ctx, uint8, *r.in.buffer_size);
372 status = dcerpc_winreg_QueryMultipleValues(p, mem_ctx, &r);
373 if(NT_STATUS_IS_ERR(status)) {
374 printf("QueryMultipleValues failed - %s\n", nt_errstr(status));
378 if (!W_ERROR_IS_OK(r.out.result)) {
379 printf("QueryMultipleValues failed - %s\n", win_errstr(r.out.result));
386 static BOOL test_QueryValue(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, struct policy_handle *handle, const char *valuename)
388 struct winreg_QueryValue r;
391 uint32 offered = 0xfff;
393 printf("Testing QueryValue\n");
395 r.in.handle = handle;
397 r.in.value_name.name = valuename;
399 r.in.size = &offered;
402 status = dcerpc_winreg_QueryValue(p, mem_ctx, &r);
403 if(NT_STATUS_IS_ERR(status)) {
404 printf("QueryValue failed - %s\n", nt_errstr(status));
408 if (!W_ERROR_IS_OK(r.out.result)) {
409 printf("QueryValue failed - %s\n", win_errstr(r.out.result));
416 static BOOL test_EnumValue(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
417 struct policy_handle *handle, int max_valnamelen, int max_valbufsize)
419 struct winreg_EnumValue r;
421 uint32 size = max_valbufsize, zero = 0;
426 printf("testing EnumValue\n");
428 r.in.handle = handle;
430 r.in.name_in.length = 0;
431 r.in.name_in.size = 0x200;
432 r.in.name_in.name = &buf16;
439 NTSTATUS status = dcerpc_winreg_EnumValue(p, mem_ctx, &r);
440 if(NT_STATUS_IS_ERR(status)) {
441 printf("EnumValue failed - %s\n", nt_errstr(status));
445 if (W_ERROR_IS_OK(r.out.result)) {
446 ret &= test_QueryValue(p, mem_ctx, handle, r.out.name_out.name);
447 ret &= test_QueryMultipleValues(p, mem_ctx, handle, r.out.name_out.name);
451 } while (W_ERROR_IS_OK(r.out.result));
453 if(!W_ERROR_EQUAL(r.out.result, WERR_NO_MORE_ITEMS)) {
454 printf("EnumValue failed - %s\n", win_errstr(r.out.result));
461 static BOOL test_OpenHKLM(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
462 struct policy_handle *handle)
465 struct winreg_OpenHKLM r;
466 struct winreg_OpenUnknown unknown;
469 printf("\ntesting OpenHKLM\n");
471 unknown.unknown0 = 0x84e0;
472 unknown.unknown1 = 0x0000;
473 r.in.unknown = &unknown;
474 r.in.access_required = SEC_FLAG_MAXIMUM_ALLOWED;
475 r.out.handle = handle;
477 status = dcerpc_winreg_OpenHKLM(p, mem_ctx, &r);
479 if (!NT_STATUS_IS_OK(status)) {
480 printf("OpenHKLM failed - %s\n", nt_errstr(status));
484 if (!W_ERROR_IS_OK(r.out.result)) {
485 printf("OpenHKLM failed - %s\n", win_errstr(r.out.result));
492 static BOOL test_OpenHKU(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
493 struct policy_handle *handle)
496 struct winreg_OpenHKU r;
497 struct winreg_OpenUnknown unknown;
500 printf("\ntesting OpenHKU\n");
502 unknown.unknown0 = 0x84e0;
503 unknown.unknown1 = 0x0000;
504 r.in.unknown = &unknown;
505 r.in.access_required = SEC_FLAG_MAXIMUM_ALLOWED;
506 r.out.handle = handle;
508 status = dcerpc_winreg_OpenHKU(p, mem_ctx, &r);
510 if (!NT_STATUS_IS_OK(status)) {
511 printf("OpenHKU failed - %s\n", nt_errstr(status));
515 if (!W_ERROR_IS_OK(r.out.result)) {
516 printf("OpenHKU failed - %s\n", win_errstr(r.out.result));
523 static BOOL test_OpenHKCR(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
524 struct policy_handle *handle)
527 struct winreg_OpenHKCR r;
528 struct winreg_OpenUnknown unknown;
531 printf("\ntesting OpenHKCR\n");
533 unknown.unknown0 = 0x84e0;
534 unknown.unknown1 = 0x0000;
535 r.in.unknown = &unknown;
536 r.in.access_required = SEC_FLAG_MAXIMUM_ALLOWED;
537 r.out.handle = handle;
539 status = dcerpc_winreg_OpenHKCR(p, mem_ctx, &r);
541 if (!NT_STATUS_IS_OK(status)) {
542 printf("OpenHKCR failed - %s\n", nt_errstr(status));
546 if (!W_ERROR_IS_OK(r.out.result)) {
547 printf("OpenHKU failed - %s\n", win_errstr(r.out.result));
553 static BOOL test_InitiateSystemShutdown(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
554 const char *msg, uint32_t timeout)
556 struct winreg_InitiateSystemShutdown r;
559 r.in.hostname = NULL;
560 r.in.message = talloc_p(mem_ctx, struct winreg_String);
561 init_winreg_String(r.in.message, msg);
563 r.in.timeout = timeout;
566 status = dcerpc_winreg_InitiateSystemShutdown(p, mem_ctx, &r);
568 if (!NT_STATUS_IS_OK(status)) {
569 printf("InitiateSystemShutdown failed - %s\n", nt_errstr(status));
573 if (!W_ERROR_IS_OK(r.out.result)) {
574 printf("InitiateSystemShutdown failed - %s\n", win_errstr(r.out.result));
581 static BOOL test_InitiateSystemShutdownEx(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
582 const char *msg, uint32_t timeout)
584 struct winreg_InitiateSystemShutdownEx r;
587 r.in.hostname = NULL;
588 r.in.message = talloc_p(mem_ctx, struct winreg_String);
589 init_winreg_String(r.in.message, msg);
591 r.in.timeout = timeout;
595 status = dcerpc_winreg_InitiateSystemShutdownEx(p, mem_ctx, &r);
597 if (!NT_STATUS_IS_OK(status)) {
598 printf("InitiateSystemShutdownEx failed - %s\n", nt_errstr(status));
602 if (!W_ERROR_IS_OK(r.out.result)) {
603 printf("InitiateSystemShutdownEx failed - %s\n", win_errstr(r.out.result));
610 static BOOL test_AbortSystemShutdown(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
612 struct winreg_AbortSystemShutdown r;
614 uint16_t server = 0x0;
616 r.in.server = &server;
618 status = dcerpc_winreg_AbortSystemShutdown(p, mem_ctx, &r);
620 if (!NT_STATUS_IS_OK(status)) {
621 printf("AbortSystemShutdown failed - %s\n", nt_errstr(status));
625 if (!W_ERROR_IS_OK(r.out.result)) {
626 printf("AbortSystemShutdown failed - %s\n", win_errstr(r.out.result));
633 static BOOL test_OpenHKCU(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
634 struct policy_handle *handle)
637 struct winreg_OpenHKCU r;
638 struct winreg_OpenUnknown unknown;
641 printf("\ntesting OpenHKCU\n");
643 unknown.unknown0 = 0x84e0;
644 unknown.unknown1 = 0x0000;
645 r.in.unknown = &unknown;
646 r.in.access_required = SEC_FLAG_MAXIMUM_ALLOWED;
647 r.out.handle = handle;
649 status = dcerpc_winreg_OpenHKCU(p, mem_ctx, &r);
651 if (!NT_STATUS_IS_OK(status)) {
652 printf("OpenHKCU failed - %s\n", nt_errstr(status));
659 #define MAX_DEPTH 2 /* Only go this far down the tree */
661 static BOOL test_key(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
662 struct policy_handle *handle, int depth)
664 if (depth == MAX_DEPTH)
667 if (!test_QueryInfoKey(p, mem_ctx, handle, NULL)) {
671 if (!test_NotifyChangeKeyValue(p, mem_ctx, handle)) {
674 if (!test_GetKeySecurity(p, mem_ctx, handle)) {
677 if (!test_EnumKey(p, mem_ctx, handle, depth)) {
680 if (!test_EnumValue(p, mem_ctx, handle, 0xFF, 0xFFFF)) {
684 test_CloseKey(p, mem_ctx, handle);
689 typedef BOOL winreg_open_fn(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
690 struct policy_handle *handle);
692 static BOOL test_Open(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, void *fn)
694 struct policy_handle handle, newhandle;
696 winreg_open_fn *open_fn = (winreg_open_fn *)fn;
698 if (!open_fn(p, mem_ctx, &handle)) {
702 if (!test_CreateKey(p, mem_ctx, &handle, "spottyfoot", NULL)) {
703 printf("CreateKey failed\n");
707 if (!test_FlushKey(p, mem_ctx, &handle)) {
708 printf("FlushKey failed\n");
712 if (!test_OpenKey(p, mem_ctx, &handle, "spottyfoot", &newhandle)) {
713 printf("CreateKey failed (OpenKey after Create didn't work)\n");
717 if (!test_DeleteKey(p, mem_ctx, &handle, "spottyfoot")) {
718 printf("DeleteKey failed\n");
722 if (!test_FlushKey(p, mem_ctx, &handle)) {
723 printf("FlushKey failed\n");
727 if (test_OpenKey(p, mem_ctx, &handle, "spottyfoot", &newhandle)) {
728 printf("DeleteKey failed (OpenKey after Delete didn't work)\n");
732 if (!test_GetVersion(p, mem_ctx, &handle)) {
733 printf("GetVersion failed\n");
737 /* The HKCR hive has a very large fanout */
739 if (open_fn == test_OpenHKCR) {
740 if(!test_key(p, mem_ctx, &handle, MAX_DEPTH - 1)) {
745 if(!test_key(p, mem_ctx, &handle, 0)) {
752 BOOL torture_rpc_winreg(void)
755 struct dcerpc_pipe *p;
758 winreg_open_fn *open_fns[] = { test_OpenHKLM, test_OpenHKU,
759 test_OpenHKCR, test_OpenHKCU };
762 mem_ctx = talloc_init("torture_rpc_winreg");
764 status = torture_rpc_connection(&p,
767 DCERPC_WINREG_VERSION);
769 if (!NT_STATUS_IS_OK(status)) {
773 if (!lp_parm_bool(-1, "torture", "dangerous", False)) {
774 printf("winreg_InitiateShutdown disabled - enable dangerous tests to use\n");
776 ret &= test_InitiateSystemShutdown(p, mem_ctx, "spottyfood", 30);
777 ret &= test_AbortSystemShutdown(p, mem_ctx);
778 ret &= test_InitiateSystemShutdownEx(p, mem_ctx, "spottyfood", 30);
779 ret &= test_AbortSystemShutdown(p, mem_ctx);
782 for (i = 0; i < ARRAY_SIZE(open_fns); i++) {
783 if (!test_Open(p, mem_ctx, open_fns[i]))
787 talloc_destroy(mem_ctx);
789 torture_rpc_close(p);