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 - not considering\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 = SEC_FLAG_MAXIMUM_ALLOWED;
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 = 0x00;
376 r.in.buffer = r.out.buffer = talloc_zero_array(mem_ctx, uint8_t, *r.in.buffer_size);
379 *r.in.buffer_size += 0x20;
381 status = dcerpc_winreg_QueryMultipleValues(p, mem_ctx, &r);
383 if(NT_STATUS_IS_ERR(status)) {
384 printf("QueryMultipleValues failed - %s\n", nt_errstr(status));
388 } while (W_ERROR_EQUAL(r.out.result, WERR_MORE_DATA));
390 if (!W_ERROR_IS_OK(r.out.result)) {
391 printf("QueryMultipleValues failed - %s\n", win_errstr(r.out.result));
398 static BOOL test_QueryValue(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, struct policy_handle *handle, const char *valuename)
400 struct winreg_QueryValue r;
403 uint32_t offered = 0xfff;
405 printf("Testing QueryValue\n");
407 r.in.handle = handle;
409 r.in.value_name.name = valuename;
411 r.in.size = &offered;
414 status = dcerpc_winreg_QueryValue(p, mem_ctx, &r);
415 if(NT_STATUS_IS_ERR(status)) {
416 printf("QueryValue failed - %s\n", nt_errstr(status));
420 if (!W_ERROR_IS_OK(r.out.result)) {
421 printf("QueryValue failed - %s\n", win_errstr(r.out.result));
428 static BOOL test_EnumValue(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
429 struct policy_handle *handle, int max_valnamelen, int max_valbufsize)
431 struct winreg_EnumValue r;
433 uint32_t size = max_valbufsize, zero = 0;
438 printf("testing EnumValue\n");
440 r.in.handle = handle;
442 r.in.name_in.length = 0;
443 r.in.name_in.size = 0x200;
444 r.in.name_in.name = &buf16;
451 NTSTATUS status = dcerpc_winreg_EnumValue(p, mem_ctx, &r);
452 if(NT_STATUS_IS_ERR(status)) {
453 printf("EnumValue failed - %s\n", nt_errstr(status));
457 if (W_ERROR_IS_OK(r.out.result)) {
458 ret &= test_QueryValue(p, mem_ctx, handle, r.out.name_out.name);
459 ret &= test_QueryMultipleValues(p, mem_ctx, handle, r.out.name_out.name);
463 } while (W_ERROR_IS_OK(r.out.result));
465 if(!W_ERROR_EQUAL(r.out.result, WERR_NO_MORE_ITEMS)) {
466 printf("EnumValue failed - %s\n", win_errstr(r.out.result));
473 static BOOL test_OpenHKLM(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
474 struct policy_handle *handle)
477 struct winreg_OpenHKLM r;
480 printf("\ntesting OpenHKLM\n");
482 r.in.system_name = 0;
483 r.in.access_required = SEC_FLAG_MAXIMUM_ALLOWED;
484 r.out.handle = handle;
486 status = dcerpc_winreg_OpenHKLM(p, mem_ctx, &r);
488 if (!NT_STATUS_IS_OK(status)) {
489 printf("OpenHKLM failed - %s\n", nt_errstr(status));
493 if (!W_ERROR_IS_OK(r.out.result)) {
494 printf("OpenHKLM failed - %s\n", win_errstr(r.out.result));
501 static BOOL test_OpenHKU(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
502 struct policy_handle *handle)
505 struct winreg_OpenHKU r;
508 printf("\ntesting OpenHKU\n");
510 r.in.system_name = 0;
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;
536 printf("\ntesting OpenHKCR\n");
538 r.in.system_name = 0;
539 r.in.access_required = SEC_FLAG_MAXIMUM_ALLOWED;
540 r.out.handle = handle;
542 status = dcerpc_winreg_OpenHKCR(p, mem_ctx, &r);
544 if (!NT_STATUS_IS_OK(status)) {
545 printf("OpenHKCR failed - %s\n", nt_errstr(status));
549 if (!W_ERROR_IS_OK(r.out.result)) {
550 printf("OpenHKU failed - %s\n", win_errstr(r.out.result));
556 static BOOL test_InitiateSystemShutdown(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
557 const char *msg, uint32_t timeout)
559 struct winreg_InitiateSystemShutdown r;
561 uint16_t hostname = 0x0;
563 r.in.hostname = &hostname;
564 r.in.message = talloc(mem_ctx, struct initshutdown_String);
565 init_initshutdown_String(mem_ctx, r.in.message, msg);
567 r.in.timeout = timeout;
570 status = dcerpc_winreg_InitiateSystemShutdown(p, mem_ctx, &r);
572 if (!NT_STATUS_IS_OK(status)) {
573 printf("InitiateSystemShutdown failed - %s\n", nt_errstr(status));
577 if (!W_ERROR_IS_OK(r.out.result)) {
578 printf("InitiateSystemShutdown failed - %s\n", win_errstr(r.out.result));
585 static BOOL test_InitiateSystemShutdownEx(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
586 const char *msg, uint32_t timeout)
588 struct winreg_InitiateSystemShutdownEx r;
590 uint16_t hostname = 0x0;
592 r.in.hostname = &hostname;
593 r.in.message = talloc(mem_ctx, struct initshutdown_String);
594 init_initshutdown_String(mem_ctx, r.in.message, msg);
596 r.in.timeout = timeout;
600 status = dcerpc_winreg_InitiateSystemShutdownEx(p, mem_ctx, &r);
602 if (!NT_STATUS_IS_OK(status)) {
603 printf("InitiateSystemShutdownEx failed - %s\n", nt_errstr(status));
607 if (!W_ERROR_IS_OK(r.out.result)) {
608 printf("InitiateSystemShutdownEx failed - %s\n", win_errstr(r.out.result));
615 static BOOL test_AbortSystemShutdown(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
617 struct winreg_AbortSystemShutdown r;
619 uint16_t server = 0x0;
621 r.in.server = &server;
623 status = dcerpc_winreg_AbortSystemShutdown(p, mem_ctx, &r);
625 if (!NT_STATUS_IS_OK(status)) {
626 printf("AbortSystemShutdown failed - %s\n", nt_errstr(status));
630 if (!W_ERROR_IS_OK(r.out.result)) {
631 printf("AbortSystemShutdown failed - %s\n", win_errstr(r.out.result));
638 static BOOL test_OpenHKCU(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
639 struct policy_handle *handle)
642 struct winreg_OpenHKCU r;
645 printf("\ntesting OpenHKCU\n");
647 r.in.system_name = 0;
648 r.in.access_required = SEC_FLAG_MAXIMUM_ALLOWED;
649 r.out.handle = handle;
651 status = dcerpc_winreg_OpenHKCU(p, mem_ctx, &r);
653 if (!NT_STATUS_IS_OK(status)) {
654 printf("OpenHKCU failed - %s\n", nt_errstr(status));
661 #define MAX_DEPTH 2 /* Only go this far down the tree */
663 static BOOL test_key(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
664 struct policy_handle *handle, int depth)
666 if (depth == MAX_DEPTH)
669 if (!test_QueryInfoKey(p, mem_ctx, handle, NULL)) {
673 if (!test_NotifyChangeKeyValue(p, mem_ctx, handle)) {
676 if (!test_GetKeySecurity(p, mem_ctx, handle)) {
679 if (!test_EnumKey(p, mem_ctx, handle, depth)) {
682 if (!test_EnumValue(p, mem_ctx, handle, 0xFF, 0xFFFF)) {
686 test_CloseKey(p, mem_ctx, handle);
691 typedef BOOL winreg_open_fn(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
692 struct policy_handle *handle);
694 static BOOL test_Open(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, void *fn)
696 struct policy_handle handle, newhandle;
697 BOOL ret = True, created = False, deleted = False;
698 winreg_open_fn *open_fn = (winreg_open_fn *)fn;
700 if (!open_fn(p, mem_ctx, &handle)) {
704 if (!test_CreateKey(p, mem_ctx, &handle, "spottyfoot", NULL)) {
705 printf("CreateKey failed - not considering a failure\n");
710 if (created && !test_FlushKey(p, mem_ctx, &handle)) {
711 printf("FlushKey failed\n");
715 if (created && !test_OpenKey(p, mem_ctx, &handle, "spottyfoot", &newhandle)) {
716 printf("CreateKey failed (OpenKey after Create didn't work)\n");
720 if (created && !test_DeleteKey(p, mem_ctx, &handle, "spottyfoot")) {
721 printf("DeleteKey failed\n");
727 if (created && !test_FlushKey(p, mem_ctx, &handle)) {
728 printf("FlushKey failed\n");
732 if (deleted && test_OpenKey(p, mem_ctx, &handle, "spottyfoot", &newhandle)) {
733 printf("DeleteKey failed (OpenKey after Delete didn't work)\n");
737 if (!test_GetVersion(p, mem_ctx, &handle)) {
738 printf("GetVersion failed\n");
742 /* The HKCR hive has a very large fanout */
744 if (open_fn == test_OpenHKCR) {
745 if(!test_key(p, mem_ctx, &handle, MAX_DEPTH - 1)) {
750 if(!test_key(p, mem_ctx, &handle, 0)) {
757 BOOL torture_rpc_winreg(void)
760 struct dcerpc_pipe *p;
763 winreg_open_fn *open_fns[] = { test_OpenHKLM, test_OpenHKU,
764 test_OpenHKCR, test_OpenHKCU };
767 mem_ctx = talloc_init("torture_rpc_winreg");
769 status = torture_rpc_connection(mem_ctx,
773 DCERPC_WINREG_VERSION);
775 if (!NT_STATUS_IS_OK(status)) {
776 talloc_free(mem_ctx);
780 if (!lp_parm_bool(-1, "torture", "dangerous", False)) {
781 printf("winreg_InitiateShutdown disabled - enable dangerous tests to use\n");
783 ret &= test_InitiateSystemShutdown(p, mem_ctx, "spottyfood", 30);
784 ret &= test_AbortSystemShutdown(p, mem_ctx);
785 ret &= test_InitiateSystemShutdownEx(p, mem_ctx, "spottyfood", 30);
786 ret &= test_AbortSystemShutdown(p, mem_ctx);
789 for (i = 0; i < ARRAY_SIZE(open_fns); i++) {
790 if (!test_Open(p, mem_ctx, open_fns[i]))
794 talloc_free(mem_ctx);