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_CreateKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
64 struct policy_handle *handle, const char *name,
67 struct winreg_CreateKey r;
68 struct policy_handle newhandle;
70 uint32_t sec_info = 0;
72 printf("\ntesting CreateKey\n");
75 r.out.handle = &newhandle;
76 init_winreg_String(&r.in.key, name);
77 init_winreg_String(&r.in.class, class);
79 r.in.access_mask = 0x02000000;
80 r.in.sec_info = &sec_info;
83 status = dcerpc_winreg_CreateKey(p, mem_ctx, &r);
85 if (!NT_STATUS_IS_OK(status)) {
86 printf("CreateKey failed - %s\n", nt_errstr(status));
90 if (!W_ERROR_IS_OK(r.out.result)) {
91 printf("CreateKey failed - %s\n", win_errstr(r.out.result));
98 static BOOL test_GetKeySecurity(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
99 struct policy_handle *handle)
102 struct winreg_GetKeySecurity r;
104 printf("\ntesting GetKeySecurity\n");
108 r.in.handle = handle;
109 r.in.data = r.out.data = talloc_zero_p(mem_ctx, struct KeySecurityData);
110 r.in.data->size = 0xffff;
111 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
113 status = dcerpc_winreg_GetKeySecurity(p, mem_ctx, &r);
115 if (!NT_STATUS_IS_OK(status)) {
116 printf("GetKeySecurity failed - %s\n", nt_errstr(status));
120 if (!W_ERROR_IS_OK(r.out.result)) {
121 printf("GetKeySecurity failed - %s\n", win_errstr(r.out.result));
128 static BOOL test_CloseKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
129 struct policy_handle *handle)
132 struct winreg_CloseKey r;
134 printf("\ntesting CloseKey\n");
136 r.in.handle = r.out.handle = handle;
138 status = dcerpc_winreg_CloseKey(p, mem_ctx, &r);
140 if (!NT_STATUS_IS_OK(status)) {
141 printf("CloseKey failed - %s\n", nt_errstr(status));
145 if (!W_ERROR_IS_OK(r.out.result)) {
146 printf("CloseKey failed - %s\n", win_errstr(r.out.result));
153 static BOOL test_FlushKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
154 struct policy_handle *handle)
157 struct winreg_FlushKey r;
159 printf("\ntesting FlushKey\n");
161 r.in.handle = handle;
163 status = dcerpc_winreg_FlushKey(p, mem_ctx, &r);
165 if (!NT_STATUS_IS_OK(status)) {
166 printf("FlushKey failed - %s\n", nt_errstr(status));
170 if (!W_ERROR_IS_OK(r.out.result)) {
171 printf("FlushKey failed - %s\n", win_errstr(r.out.result));
178 static BOOL test_OpenKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
179 struct policy_handle *hive_handle,
180 const char *keyname, struct policy_handle *key_handle)
183 struct winreg_OpenKey r;
185 printf("\ntesting OpenKey\n");
187 r.in.handle = hive_handle;
188 init_winreg_String(&r.in.keyname, keyname);
189 r.in.unknown = 0x00000000;
190 r.in.access_mask = 0x02000000;
191 r.out.handle = key_handle;
193 status = dcerpc_winreg_OpenKey(p, mem_ctx, &r);
195 if (!NT_STATUS_IS_OK(status)) {
196 printf("OpenKey failed - %s\n", nt_errstr(status));
200 if (!W_ERROR_IS_OK(r.out.result)) {
201 printf("OpenKey failed - %s\n", win_errstr(r.out.result));
209 static BOOL test_DeleteKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
210 struct policy_handle *handle, const char *key)
213 struct winreg_DeleteKey r;
215 printf("\ntesting DeleteKey\n");
217 r.in.handle = handle;
218 init_winreg_String(&r.in.key, key);
220 status = dcerpc_winreg_DeleteKey(p, mem_ctx, &r);
222 if (!NT_STATUS_IS_OK(status)) {
223 printf("DeleteKey failed - %s\n", nt_errstr(status));
227 if (!W_ERROR_IS_OK(r.out.result)) {
228 printf("DeleteKey failed - %s\n", win_errstr(r.out.result));
235 static BOOL test_QueryInfoKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
236 struct policy_handle *handle, char *class)
239 struct winreg_QueryInfoKey r;
241 printf("\ntesting QueryInfoKey\n");
243 r.in.handle = handle;
244 init_winreg_String(&r.in.class, class);
246 status = dcerpc_winreg_QueryInfoKey(p, mem_ctx, &r);
248 if (!NT_STATUS_IS_OK(status)) {
249 printf("QueryInfoKey failed - %s\n", nt_errstr(status));
253 if (!W_ERROR_IS_OK(r.out.result)) {
254 printf("QueryInfoKey failed - %s\n", win_errstr(r.out.result));
261 static BOOL test_key(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
262 struct policy_handle *handle, int depth);
264 static BOOL test_EnumKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
265 struct policy_handle *handle, int depth)
267 struct winreg_EnumKey r;
268 struct winreg_EnumKeyNameRequest keyname;
269 struct winreg_String classname;
270 struct winreg_Time tm;
273 printf("Testing EnumKey\n\n");
275 r.in.handle = handle;
277 r.in.key_name_len = r.out.key_name_len = 0;
278 r.in.unknown = r.out.unknown = 0x0414;
279 keyname.unknown = 0x0000020a;
280 init_winreg_String(&keyname.key_name, NULL);
281 init_winreg_String(&classname, NULL);
282 r.in.in_name = &keyname;
283 r.in.class = &classname;
284 tm.low = tm.high = 0x7fffffff;
285 r.in.last_changed_time = &tm;
288 status = dcerpc_winreg_EnumKey(p, mem_ctx, &r);
290 if (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(r.out.result)) {
291 struct policy_handle key_handle;
293 printf("EnumKey: %d: %s\n", r.in.enum_index, r.out.out_name->name);
296 p, mem_ctx, handle, r.out.out_name->name,
299 test_key(p, mem_ctx, &key_handle, depth + 1);
305 } while (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(r.out.result));
307 if (!NT_STATUS_IS_OK(status)) {
308 printf("EnumKey failed - %s\n", nt_errstr(status));
312 if (!W_ERROR_IS_OK(r.out.result) && !W_ERROR_EQUAL(r.out.result, WERR_NO_MORE_ITEMS)) {
313 printf("EnumKey failed - %s\n", win_errstr(r.out.result));
322 static BOOL test_QueryMultipleValues(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, struct policy_handle *handle, const char *valuename)
324 struct winreg_QueryMultipleValues r;
327 printf("Testing QueryMultipleValues\n");
329 r.in.key_handle = handle;
330 r.in.values = r.out.values = talloc_array_p(mem_ctx, struct QueryMultipleValue, 1);
331 r.in.values[0].name = talloc_p(mem_ctx, struct winreg_String);
332 r.in.values[0].name->name = valuename;
333 r.in.values[0].offset = 0;
334 r.in.values[0].length = 0;
335 r.in.values[0].type = 0;
338 r.in.buffer_size = r.out.buffer_size = talloc_p(mem_ctx, uint32);
339 *r.in.buffer_size = 0x20;
340 r.in.buffer = r.out.buffer = talloc_zero_array_p(mem_ctx, uint8, *r.in.buffer_size);
342 status = dcerpc_winreg_QueryMultipleValues(p, mem_ctx, &r);
343 if(NT_STATUS_IS_ERR(status)) {
344 printf("QueryMultipleValues failed - %s\n", nt_errstr(status));
348 if (!W_ERROR_IS_OK(r.out.result)) {
349 printf("QueryMultipleValues failed - %s\n", win_errstr(r.out.result));
356 static BOOL test_QueryValue(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, struct policy_handle *handle, const char *valuename)
358 struct winreg_QueryValue r;
361 uint32 offered = 0xfff;
363 printf("Testing QueryValue\n");
365 r.in.handle = handle;
367 r.in.value_name.name = valuename;
369 r.in.size = &offered;
372 status = dcerpc_winreg_QueryValue(p, mem_ctx, &r);
373 if(NT_STATUS_IS_ERR(status)) {
374 printf("QueryValue failed - %s\n", nt_errstr(status));
378 if (!W_ERROR_IS_OK(r.out.result)) {
379 printf("QueryValue failed - %s\n", win_errstr(r.out.result));
386 static BOOL test_EnumValue(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
387 struct policy_handle *handle, int max_valnamelen, int max_valbufsize)
389 struct winreg_EnumValue r;
391 uint32 size = max_valbufsize, zero = 0;
396 printf("testing EnumValue\n");
398 r.in.handle = handle;
400 r.in.name_in.length = 0;
401 r.in.name_in.size = 0x200;
402 r.in.name_in.name = &buf16;
409 NTSTATUS status = dcerpc_winreg_EnumValue(p, mem_ctx, &r);
410 if(NT_STATUS_IS_ERR(status)) {
411 printf("EnumValue failed - %s\n", nt_errstr(status));
415 if (W_ERROR_IS_OK(r.out.result)) {
416 ret &= test_QueryValue(p, mem_ctx, handle, r.out.name_out.name);
417 ret &= test_QueryMultipleValues(p, mem_ctx, handle, r.out.name_out.name);
421 } while (W_ERROR_IS_OK(r.out.result));
423 if(!W_ERROR_EQUAL(r.out.result, WERR_NO_MORE_ITEMS)) {
424 printf("EnumValue failed - %s\n", win_errstr(r.out.result));
431 static BOOL test_OpenHKLM(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
432 struct policy_handle *handle)
435 struct winreg_OpenHKLM r;
436 struct winreg_OpenUnknown unknown;
439 printf("\ntesting OpenHKLM\n");
441 unknown.unknown0 = 0x84e0;
442 unknown.unknown1 = 0x0000;
443 r.in.unknown = &unknown;
444 r.in.access_required = SEC_FLAG_MAXIMUM_ALLOWED;
445 r.out.handle = handle;
447 status = dcerpc_winreg_OpenHKLM(p, mem_ctx, &r);
449 if (!NT_STATUS_IS_OK(status)) {
450 printf("OpenHKLM failed - %s\n", nt_errstr(status));
454 if (!W_ERROR_IS_OK(r.out.result)) {
455 printf("OpenHKLM failed - %s\n", win_errstr(r.out.result));
462 static BOOL test_OpenHKU(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
463 struct policy_handle *handle)
466 struct winreg_OpenHKU r;
467 struct winreg_OpenUnknown unknown;
470 printf("\ntesting OpenHKU\n");
472 unknown.unknown0 = 0x84e0;
473 unknown.unknown1 = 0x0000;
474 r.in.unknown = &unknown;
475 r.in.access_required = SEC_FLAG_MAXIMUM_ALLOWED;
476 r.out.handle = handle;
478 status = dcerpc_winreg_OpenHKU(p, mem_ctx, &r);
480 if (!NT_STATUS_IS_OK(status)) {
481 printf("OpenHKU failed - %s\n", nt_errstr(status));
485 if (!W_ERROR_IS_OK(r.out.result)) {
486 printf("OpenHKU failed - %s\n", win_errstr(r.out.result));
493 static BOOL test_OpenHKCR(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
494 struct policy_handle *handle)
497 struct winreg_OpenHKCR r;
498 struct winreg_OpenUnknown unknown;
501 printf("\ntesting OpenHKCR\n");
503 unknown.unknown0 = 0x84e0;
504 unknown.unknown1 = 0x0000;
505 r.in.unknown = &unknown;
506 r.in.access_required = SEC_FLAG_MAXIMUM_ALLOWED;
507 r.out.handle = handle;
509 status = dcerpc_winreg_OpenHKCR(p, mem_ctx, &r);
511 if (!NT_STATUS_IS_OK(status)) {
512 printf("OpenHKCR failed - %s\n", nt_errstr(status));
516 if (!W_ERROR_IS_OK(r.out.result)) {
517 printf("OpenHKU failed - %s\n", win_errstr(r.out.result));
523 static BOOL test_InitiateSystemShutdown(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
524 const char *msg, uint32_t timeout)
526 struct winreg_InitiateSystemShutdown r;
529 init_winreg_String(&r.in.hostname, NULL);
530 init_winreg_String(&r.in.message, msg);
532 r.in.timeout = timeout;
534 status = dcerpc_winreg_InitiateSystemShutdown(p, mem_ctx, &r);
536 if (!NT_STATUS_IS_OK(status)) {
537 printf("InitiateSystemShutdown failed - %s\n", nt_errstr(status));
541 if (!W_ERROR_IS_OK(r.out.result)) {
542 printf("InitiateSystemShutdown failed - %s\n", win_errstr(r.out.result));
549 static BOOL test_AbortSystemShutdown(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
551 struct winreg_AbortSystemShutdown r;
553 uint16_t server = 0x0;
555 r.in.server = &server;
557 status = dcerpc_winreg_AbortSystemShutdown(p, mem_ctx, &r);
559 if (!NT_STATUS_IS_OK(status)) {
560 printf("AbortSystemShutdown failed - %s\n", nt_errstr(status));
564 if (!W_ERROR_IS_OK(r.out.result)) {
565 printf("AbortSystemShutdown failed - %s\n", win_errstr(r.out.result));
572 static BOOL test_OpenHKCU(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
573 struct policy_handle *handle)
576 struct winreg_OpenHKCU r;
577 struct winreg_OpenUnknown unknown;
580 printf("\ntesting OpenHKCU\n");
582 unknown.unknown0 = 0x84e0;
583 unknown.unknown1 = 0x0000;
584 r.in.unknown = &unknown;
585 r.in.access_required = SEC_FLAG_MAXIMUM_ALLOWED;
586 r.out.handle = handle;
588 status = dcerpc_winreg_OpenHKCU(p, mem_ctx, &r);
590 if (!NT_STATUS_IS_OK(status)) {
591 printf("OpenHKCU failed - %s\n", nt_errstr(status));
598 #define MAX_DEPTH 2 /* Only go this far down the tree */
600 static BOOL test_key(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
601 struct policy_handle *handle, int depth)
603 if (depth == MAX_DEPTH)
606 if (!test_QueryInfoKey(p, mem_ctx, handle, NULL)) {
610 if (!test_GetKeySecurity(p, mem_ctx, handle)) {
613 if (!test_EnumKey(p, mem_ctx, handle, depth)) {
616 if (!test_EnumValue(p, mem_ctx, handle, 0xFF, 0xFFFF)) {
620 test_CloseKey(p, mem_ctx, handle);
625 typedef BOOL winreg_open_fn(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
626 struct policy_handle *handle);
628 static BOOL test_Open(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, void *fn)
630 struct policy_handle handle, newhandle;
632 winreg_open_fn *open_fn = (winreg_open_fn *)fn;
634 if (!open_fn(p, mem_ctx, &handle)) {
638 if (!test_CreateKey(p, mem_ctx, &handle, "spottyfoot", NULL)) {
639 printf("CreateKey failed\n");
643 if (!test_FlushKey(p, mem_ctx, &handle)) {
644 printf("FlushKey failed\n");
648 if (!test_OpenKey(p, mem_ctx, &handle, "spottyfoot", &newhandle)) {
649 printf("CreateKey failed (OpenKey after Create didn't work)\n");
653 if (!test_DeleteKey(p, mem_ctx, &handle, "spottyfoot")) {
654 printf("DeleteKey failed\n");
658 if (!test_FlushKey(p, mem_ctx, &handle)) {
659 printf("FlushKey failed\n");
663 if (test_OpenKey(p, mem_ctx, &handle, "spottyfoot", &newhandle)) {
664 printf("DeleteKey failed (OpenKey after Delete didn't work)\n");
668 if (!test_GetVersion(p, mem_ctx, &handle)) {
669 printf("GetVersion failed\n");
673 /* The HKCR hive has a very large fanout */
675 if (open_fn == test_OpenHKCR) {
676 if(!test_key(p, mem_ctx, &handle, MAX_DEPTH - 1)) {
681 if(!test_key(p, mem_ctx, &handle, 0)) {
688 BOOL torture_rpc_winreg(void)
691 struct dcerpc_pipe *p;
694 winreg_open_fn *open_fns[] = { test_OpenHKLM, test_OpenHKU,
695 test_OpenHKCR, test_OpenHKCU };
698 mem_ctx = talloc_init("torture_rpc_winreg");
700 status = torture_rpc_connection(&p,
703 DCERPC_WINREG_VERSION);
705 if (!NT_STATUS_IS_OK(status)) {
709 if(!test_InitiateSystemShutdown(p, mem_ctx, "spottyfood", 30))
712 if(!test_AbortSystemShutdown(p, mem_ctx))
715 for (i = 0; i < ARRAY_SIZE(open_fns); i++) {
716 if (!test_Open(p, mem_ctx, open_fns[i]))
720 talloc_destroy(mem_ctx);
722 torture_rpc_close(p);