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_initshutdown_String(TALLOC_CTX *mem_ctx, struct initshutdown_String *name, const char *s)
28 name->name = talloc(mem_ctx, struct initshutdown_String_sub);
32 static void init_winreg_String(struct winreg_String *name, const char *s)
36 name->name_len = 2 * (strlen_m(s) + 1);
37 name->name_size = name->name_len;
44 static BOOL test_GetVersion(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
45 struct policy_handle *handle)
48 struct winreg_GetVersion r;
50 printf("\ntesting GetVersion\n");
54 status = dcerpc_winreg_GetVersion(p, mem_ctx, &r);
56 if (!NT_STATUS_IS_OK(status)) {
57 printf("GetVersion failed - %s\n", nt_errstr(status));
61 if (!W_ERROR_IS_OK(r.out.result)) {
62 printf("GetVersion failed - %s\n", win_errstr(r.out.result));
69 static BOOL test_NotifyChangeKeyValue(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
70 struct policy_handle *handle)
72 struct winreg_NotifyChangeKeyValue r;
75 printf("\ntesting NotifyChangeKeyValue\n");
78 r.in.watch_subtree = 1;
79 r.in.notify_filter = 0;
80 r.in.unknown = r.in.unknown2 = 0;
81 init_winreg_String(&r.in.string1, NULL);
82 init_winreg_String(&r.in.string2, NULL);
84 status = dcerpc_winreg_NotifyChangeKeyValue(p, mem_ctx, &r);
86 if (!NT_STATUS_IS_OK(status)) {
87 printf("NotifyChangeKeyValue failed - %s\n", nt_errstr(status));
91 if (!W_ERROR_IS_OK(r.out.result)) {
92 printf("NotifyChangeKeyValue failed - %s\n", win_errstr(r.out.result));
99 static BOOL test_CreateKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
100 struct policy_handle *handle, const char *name,
103 struct winreg_CreateKey r;
104 struct policy_handle newhandle;
106 uint32_t action_taken = 0;
108 printf("\ntesting CreateKey\n");
110 r.in.handle = handle;
111 r.out.handle = &newhandle;
112 init_winreg_String(&r.in.key, name);
113 init_winreg_String(&r.in.class, class);
115 r.in.access_mask = 0x02000000;
116 r.in.action_taken = r.out.action_taken = &action_taken;
117 r.in.sec_desc = NULL;
119 status = dcerpc_winreg_CreateKey(p, mem_ctx, &r);
121 if (!NT_STATUS_IS_OK(status)) {
122 printf("CreateKey failed - %s\n", nt_errstr(status));
126 if (!W_ERROR_IS_OK(r.out.result)) {
127 printf("CreateKey failed - %s\n", win_errstr(r.out.result));
134 static BOOL test_GetKeySecurity(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
135 struct policy_handle *handle)
138 struct winreg_GetKeySecurity r;
140 printf("\ntesting GetKeySecurity\n");
144 r.in.handle = handle;
145 r.in.data = r.out.data = talloc_zero(mem_ctx, struct KeySecurityData);
146 r.in.data->size = 0xffff;
147 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
149 status = dcerpc_winreg_GetKeySecurity(p, mem_ctx, &r);
151 if (!NT_STATUS_IS_OK(status)) {
152 printf("GetKeySecurity failed - %s\n", nt_errstr(status));
156 if (!W_ERROR_IS_OK(r.out.result)) {
157 printf("GetKeySecurity failed - %s\n", win_errstr(r.out.result));
164 static BOOL test_CloseKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
165 struct policy_handle *handle)
168 struct winreg_CloseKey r;
170 printf("\ntesting CloseKey\n");
172 r.in.handle = r.out.handle = handle;
174 status = dcerpc_winreg_CloseKey(p, mem_ctx, &r);
176 if (!NT_STATUS_IS_OK(status)) {
177 printf("CloseKey failed - %s\n", nt_errstr(status));
181 if (!W_ERROR_IS_OK(r.out.result)) {
182 printf("CloseKey failed - %s\n", win_errstr(r.out.result));
189 static BOOL test_FlushKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
190 struct policy_handle *handle)
193 struct winreg_FlushKey r;
195 printf("\ntesting FlushKey\n");
197 r.in.handle = handle;
199 status = dcerpc_winreg_FlushKey(p, mem_ctx, &r);
201 if (!NT_STATUS_IS_OK(status)) {
202 printf("FlushKey failed - %s\n", nt_errstr(status));
206 if (!W_ERROR_IS_OK(r.out.result)) {
207 printf("FlushKey failed - %s\n", win_errstr(r.out.result));
214 static BOOL test_OpenKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
215 struct policy_handle *hive_handle,
216 const char *keyname, struct policy_handle *key_handle)
219 struct winreg_OpenKey r;
221 printf("\ntesting OpenKey\n");
223 r.in.handle = hive_handle;
224 init_winreg_String(&r.in.keyname, keyname);
225 r.in.unknown = 0x00000000;
226 r.in.access_mask = 0x02000000;
227 r.out.handle = key_handle;
229 status = dcerpc_winreg_OpenKey(p, mem_ctx, &r);
231 if (!NT_STATUS_IS_OK(status)) {
232 printf("OpenKey failed - %s\n", nt_errstr(status));
236 if (!W_ERROR_IS_OK(r.out.result)) {
237 printf("OpenKey failed - %s\n", win_errstr(r.out.result));
245 static BOOL test_DeleteKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
246 struct policy_handle *handle, const char *key)
249 struct winreg_DeleteKey r;
251 printf("\ntesting DeleteKey\n");
253 r.in.handle = handle;
254 init_winreg_String(&r.in.key, key);
256 status = dcerpc_winreg_DeleteKey(p, mem_ctx, &r);
258 if (!NT_STATUS_IS_OK(status)) {
259 printf("DeleteKey failed - %s\n", nt_errstr(status));
263 if (!W_ERROR_IS_OK(r.out.result)) {
264 printf("DeleteKey failed - %s\n", win_errstr(r.out.result));
271 static BOOL test_QueryInfoKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
272 struct policy_handle *handle, char *class)
275 struct winreg_QueryInfoKey r;
277 printf("\ntesting QueryInfoKey\n");
279 r.in.handle = handle;
280 init_winreg_String(&r.in.class, class);
282 status = dcerpc_winreg_QueryInfoKey(p, mem_ctx, &r);
284 if (!NT_STATUS_IS_OK(status)) {
285 printf("QueryInfoKey failed - %s\n", nt_errstr(status));
289 if (!W_ERROR_IS_OK(r.out.result)) {
290 printf("QueryInfoKey failed - %s\n", win_errstr(r.out.result));
297 static BOOL test_key(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
298 struct policy_handle *handle, int depth);
300 static BOOL test_EnumKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
301 struct policy_handle *handle, int depth)
303 struct winreg_EnumKey r;
304 struct winreg_EnumKeyNameRequest keyname;
305 struct winreg_String classname;
306 struct winreg_Time tm;
309 printf("Testing EnumKey\n\n");
311 r.in.handle = handle;
313 r.in.key_name_len = r.out.key_name_len = 0;
314 r.in.unknown = r.out.unknown = 0x0414;
315 keyname.unknown = 0x0000020a;
316 init_winreg_String(&keyname.key_name, NULL);
317 init_winreg_String(&classname, NULL);
318 r.in.in_name = &keyname;
319 r.in.class = &classname;
320 tm.low = tm.high = 0x7fffffff;
321 r.in.last_changed_time = &tm;
324 status = dcerpc_winreg_EnumKey(p, mem_ctx, &r);
326 if (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(r.out.result)) {
327 struct policy_handle key_handle;
329 printf("EnumKey: %d: %s\n", r.in.enum_index, r.out.out_name->name);
332 p, mem_ctx, handle, r.out.out_name->name,
335 test_key(p, mem_ctx, &key_handle, depth + 1);
341 } while (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(r.out.result));
343 if (!NT_STATUS_IS_OK(status)) {
344 printf("EnumKey failed - %s\n", nt_errstr(status));
348 if (!W_ERROR_IS_OK(r.out.result) && !W_ERROR_EQUAL(r.out.result, WERR_NO_MORE_ITEMS)) {
349 printf("EnumKey failed - %s\n", win_errstr(r.out.result));
358 static BOOL test_QueryMultipleValues(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, struct policy_handle *handle, const char *valuename)
360 struct winreg_QueryMultipleValues r;
363 printf("Testing QueryMultipleValues\n");
365 r.in.key_handle = handle;
366 r.in.values = r.out.values = talloc_array(mem_ctx, struct QueryMultipleValue, 1);
367 r.in.values[0].name = talloc(mem_ctx, struct winreg_String);
368 r.in.values[0].name->name = valuename;
369 r.in.values[0].offset = 0;
370 r.in.values[0].length = 0;
371 r.in.values[0].type = 0;
374 r.in.buffer_size = r.out.buffer_size = talloc(mem_ctx, uint32_t);
375 *r.in.buffer_size = 0x20;
376 r.in.buffer = r.out.buffer = talloc_zero_array(mem_ctx, uint8_t, *r.in.buffer_size);
378 status = dcerpc_winreg_QueryMultipleValues(p, mem_ctx, &r);
379 if(NT_STATUS_IS_ERR(status)) {
380 printf("QueryMultipleValues failed - %s\n", nt_errstr(status));
384 if (!W_ERROR_IS_OK(r.out.result)) {
385 printf("QueryMultipleValues failed - %s\n", win_errstr(r.out.result));
392 static BOOL test_QueryValue(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, struct policy_handle *handle, const char *valuename)
394 struct winreg_QueryValue r;
397 uint32_t offered = 0xfff;
399 printf("Testing QueryValue\n");
401 r.in.handle = handle;
403 r.in.value_name.name = valuename;
405 r.in.size = &offered;
408 status = dcerpc_winreg_QueryValue(p, mem_ctx, &r);
409 if(NT_STATUS_IS_ERR(status)) {
410 printf("QueryValue failed - %s\n", nt_errstr(status));
414 if (!W_ERROR_IS_OK(r.out.result)) {
415 printf("QueryValue failed - %s\n", win_errstr(r.out.result));
422 static BOOL test_EnumValue(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
423 struct policy_handle *handle, int max_valnamelen, int max_valbufsize)
425 struct winreg_EnumValue r;
427 uint32_t size = max_valbufsize, zero = 0;
432 printf("testing EnumValue\n");
434 r.in.handle = handle;
436 r.in.name_in.length = 0;
437 r.in.name_in.size = 0x200;
438 r.in.name_in.name = &buf16;
445 NTSTATUS status = dcerpc_winreg_EnumValue(p, mem_ctx, &r);
446 if(NT_STATUS_IS_ERR(status)) {
447 printf("EnumValue failed - %s\n", nt_errstr(status));
451 if (W_ERROR_IS_OK(r.out.result)) {
452 ret &= test_QueryValue(p, mem_ctx, handle, r.out.name_out.name);
453 ret &= test_QueryMultipleValues(p, mem_ctx, handle, r.out.name_out.name);
457 } while (W_ERROR_IS_OK(r.out.result));
459 if(!W_ERROR_EQUAL(r.out.result, WERR_NO_MORE_ITEMS)) {
460 printf("EnumValue failed - %s\n", win_errstr(r.out.result));
467 static BOOL test_OpenHKLM(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
468 struct policy_handle *handle)
471 struct winreg_OpenHKLM r;
472 struct winreg_OpenUnknown unknown;
475 printf("\ntesting OpenHKLM\n");
477 unknown.unknown0 = 0x84e0;
478 unknown.unknown1 = 0x0000;
479 r.in.unknown = &unknown;
480 r.in.access_required = SEC_FLAG_MAXIMUM_ALLOWED;
481 r.out.handle = handle;
483 status = dcerpc_winreg_OpenHKLM(p, mem_ctx, &r);
485 if (!NT_STATUS_IS_OK(status)) {
486 printf("OpenHKLM failed - %s\n", nt_errstr(status));
490 if (!W_ERROR_IS_OK(r.out.result)) {
491 printf("OpenHKLM failed - %s\n", win_errstr(r.out.result));
498 static BOOL test_OpenHKU(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
499 struct policy_handle *handle)
502 struct winreg_OpenHKU r;
503 struct winreg_OpenUnknown unknown;
506 printf("\ntesting OpenHKU\n");
508 unknown.unknown0 = 0x84e0;
509 unknown.unknown1 = 0x0000;
510 r.in.unknown = &unknown;
511 r.in.access_required = SEC_FLAG_MAXIMUM_ALLOWED;
512 r.out.handle = handle;
514 status = dcerpc_winreg_OpenHKU(p, mem_ctx, &r);
516 if (!NT_STATUS_IS_OK(status)) {
517 printf("OpenHKU failed - %s\n", nt_errstr(status));
521 if (!W_ERROR_IS_OK(r.out.result)) {
522 printf("OpenHKU failed - %s\n", win_errstr(r.out.result));
529 static BOOL test_OpenHKCR(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
530 struct policy_handle *handle)
533 struct winreg_OpenHKCR r;
534 struct winreg_OpenUnknown unknown;
537 printf("\ntesting OpenHKCR\n");
539 unknown.unknown0 = 0x84e0;
540 unknown.unknown1 = 0x0000;
541 r.in.unknown = &unknown;
542 r.in.access_required = SEC_FLAG_MAXIMUM_ALLOWED;
543 r.out.handle = handle;
545 status = dcerpc_winreg_OpenHKCR(p, mem_ctx, &r);
547 if (!NT_STATUS_IS_OK(status)) {
548 printf("OpenHKCR failed - %s\n", nt_errstr(status));
552 if (!W_ERROR_IS_OK(r.out.result)) {
553 printf("OpenHKU failed - %s\n", win_errstr(r.out.result));
559 static BOOL test_InitiateSystemShutdown(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
560 const char *msg, uint32_t timeout)
562 struct winreg_InitiateSystemShutdown r;
564 uint16_t hostname = 0x0;
566 r.in.hostname = &hostname;
567 r.in.message = talloc(mem_ctx, struct initshutdown_String);
568 init_initshutdown_String(mem_ctx, r.in.message, msg);
570 r.in.timeout = timeout;
573 status = dcerpc_winreg_InitiateSystemShutdown(p, mem_ctx, &r);
575 if (!NT_STATUS_IS_OK(status)) {
576 printf("InitiateSystemShutdown failed - %s\n", nt_errstr(status));
580 if (!W_ERROR_IS_OK(r.out.result)) {
581 printf("InitiateSystemShutdown failed - %s\n", win_errstr(r.out.result));
588 static BOOL test_InitiateSystemShutdownEx(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
589 const char *msg, uint32_t timeout)
591 struct winreg_InitiateSystemShutdownEx r;
593 uint16_t hostname = 0x0;
595 r.in.hostname = &hostname;
596 r.in.message = talloc(mem_ctx, struct initshutdown_String);
597 init_initshutdown_String(mem_ctx, r.in.message, msg);
599 r.in.timeout = timeout;
603 status = dcerpc_winreg_InitiateSystemShutdownEx(p, mem_ctx, &r);
605 if (!NT_STATUS_IS_OK(status)) {
606 printf("InitiateSystemShutdownEx failed - %s\n", nt_errstr(status));
610 if (!W_ERROR_IS_OK(r.out.result)) {
611 printf("InitiateSystemShutdownEx failed - %s\n", win_errstr(r.out.result));
618 static BOOL test_AbortSystemShutdown(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
620 struct winreg_AbortSystemShutdown r;
622 uint16_t server = 0x0;
624 r.in.server = &server;
626 status = dcerpc_winreg_AbortSystemShutdown(p, mem_ctx, &r);
628 if (!NT_STATUS_IS_OK(status)) {
629 printf("AbortSystemShutdown failed - %s\n", nt_errstr(status));
633 if (!W_ERROR_IS_OK(r.out.result)) {
634 printf("AbortSystemShutdown failed - %s\n", win_errstr(r.out.result));
641 static BOOL test_OpenHKCU(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
642 struct policy_handle *handle)
645 struct winreg_OpenHKCU r;
646 struct winreg_OpenUnknown unknown;
649 printf("\ntesting OpenHKCU\n");
651 unknown.unknown0 = 0x84e0;
652 unknown.unknown1 = 0x0000;
653 r.in.unknown = &unknown;
654 r.in.access_required = SEC_FLAG_MAXIMUM_ALLOWED;
655 r.out.handle = handle;
657 status = dcerpc_winreg_OpenHKCU(p, mem_ctx, &r);
659 if (!NT_STATUS_IS_OK(status)) {
660 printf("OpenHKCU failed - %s\n", nt_errstr(status));
667 #define MAX_DEPTH 2 /* Only go this far down the tree */
669 static BOOL test_key(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
670 struct policy_handle *handle, int depth)
672 if (depth == MAX_DEPTH)
675 if (!test_QueryInfoKey(p, mem_ctx, handle, NULL)) {
679 if (!test_NotifyChangeKeyValue(p, mem_ctx, handle)) {
682 if (!test_GetKeySecurity(p, mem_ctx, handle)) {
685 if (!test_EnumKey(p, mem_ctx, handle, depth)) {
688 if (!test_EnumValue(p, mem_ctx, handle, 0xFF, 0xFFFF)) {
692 test_CloseKey(p, mem_ctx, handle);
697 typedef BOOL winreg_open_fn(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
698 struct policy_handle *handle);
700 static BOOL test_Open(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, void *fn)
702 struct policy_handle handle, newhandle;
704 winreg_open_fn *open_fn = (winreg_open_fn *)fn;
706 if (!open_fn(p, mem_ctx, &handle)) {
710 if (!test_CreateKey(p, mem_ctx, &handle, "spottyfoot", NULL)) {
711 printf("CreateKey failed\n");
715 if (!test_FlushKey(p, mem_ctx, &handle)) {
716 printf("FlushKey failed\n");
720 if (!test_OpenKey(p, mem_ctx, &handle, "spottyfoot", &newhandle)) {
721 printf("CreateKey failed (OpenKey after Create didn't work)\n");
725 if (!test_DeleteKey(p, mem_ctx, &handle, "spottyfoot")) {
726 printf("DeleteKey failed\n");
730 if (!test_FlushKey(p, mem_ctx, &handle)) {
731 printf("FlushKey failed\n");
735 if (test_OpenKey(p, mem_ctx, &handle, "spottyfoot", &newhandle)) {
736 printf("DeleteKey failed (OpenKey after Delete didn't work)\n");
740 if (!test_GetVersion(p, mem_ctx, &handle)) {
741 printf("GetVersion failed\n");
745 /* The HKCR hive has a very large fanout */
747 if (open_fn == test_OpenHKCR) {
748 if(!test_key(p, mem_ctx, &handle, MAX_DEPTH - 1)) {
753 if(!test_key(p, mem_ctx, &handle, 0)) {
760 BOOL torture_rpc_winreg(void)
763 struct dcerpc_pipe *p;
766 winreg_open_fn *open_fns[] = { test_OpenHKLM, test_OpenHKU,
767 test_OpenHKCR, test_OpenHKCU };
770 mem_ctx = talloc_init("torture_rpc_winreg");
772 status = torture_rpc_connection(mem_ctx,
776 DCERPC_WINREG_VERSION);
778 if (!NT_STATUS_IS_OK(status)) {
779 talloc_free(mem_ctx);
783 if (!lp_parm_bool(-1, "torture", "dangerous", False)) {
784 printf("winreg_InitiateShutdown disabled - enable dangerous tests to use\n");
786 ret &= test_InitiateSystemShutdown(p, mem_ctx, "spottyfood", 30);
787 ret &= test_AbortSystemShutdown(p, mem_ctx);
788 ret &= test_InitiateSystemShutdownEx(p, mem_ctx, "spottyfood", 30);
789 ret &= test_AbortSystemShutdown(p, mem_ctx);
792 for (i = 0; i < ARRAY_SIZE(open_fns); i++) {
793 if (!test_Open(p, mem_ctx, open_fns[i]))
797 talloc_free(mem_ctx);