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_CloseKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
99 struct policy_handle *handle)
102 struct winreg_CloseKey r;
104 printf("\ntesting CloseKey\n");
106 r.in.handle = r.out.handle = handle;
108 status = dcerpc_winreg_CloseKey(p, mem_ctx, &r);
110 if (!NT_STATUS_IS_OK(status)) {
111 printf("CloseKey failed - %s\n", nt_errstr(status));
115 if (!W_ERROR_IS_OK(r.out.result)) {
116 printf("CloseKey failed - %s\n", win_errstr(r.out.result));
123 static BOOL test_FlushKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
124 struct policy_handle *handle)
127 struct winreg_FlushKey r;
129 printf("\ntesting FlushKey\n");
131 r.in.handle = handle;
133 status = dcerpc_winreg_FlushKey(p, mem_ctx, &r);
135 if (!NT_STATUS_IS_OK(status)) {
136 printf("FlushKey failed - %s\n", nt_errstr(status));
140 if (!W_ERROR_IS_OK(r.out.result)) {
141 printf("FlushKey failed - %s\n", win_errstr(r.out.result));
148 static BOOL test_OpenKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
149 struct policy_handle *hive_handle,
150 const char *keyname, struct policy_handle *key_handle)
153 struct winreg_OpenKey r;
155 printf("\ntesting OpenKey\n");
157 r.in.handle = hive_handle;
158 init_winreg_String(&r.in.keyname, keyname);
159 r.in.unknown = 0x00000000;
160 r.in.access_mask = 0x02000000;
161 r.out.handle = key_handle;
163 status = dcerpc_winreg_OpenKey(p, mem_ctx, &r);
165 if (!NT_STATUS_IS_OK(status)) {
166 printf("OpenKey failed - %s\n", nt_errstr(status));
170 if (!W_ERROR_IS_OK(r.out.result)) {
171 printf("OpenKey failed - %s\n", win_errstr(r.out.result));
179 static BOOL test_DeleteKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
180 struct policy_handle *handle, const char *key)
183 struct winreg_DeleteKey r;
185 printf("\ntesting DeleteKey\n");
187 r.in.handle = handle;
188 init_winreg_String(&r.in.key, key);
190 status = dcerpc_winreg_DeleteKey(p, mem_ctx, &r);
192 if (!NT_STATUS_IS_OK(status)) {
193 printf("DeleteKey failed - %s\n", nt_errstr(status));
197 if (!W_ERROR_IS_OK(r.out.result)) {
198 printf("DeleteKey failed - %s\n", win_errstr(r.out.result));
205 static BOOL test_QueryInfoKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
206 struct policy_handle *handle, char *class)
209 struct winreg_QueryInfoKey r;
211 printf("\ntesting QueryInfoKey\n");
213 r.in.handle = handle;
214 init_winreg_String(&r.in.class, class);
216 status = dcerpc_winreg_QueryInfoKey(p, mem_ctx, &r);
218 if (!NT_STATUS_IS_OK(status)) {
219 printf("QueryInfoKey failed - %s\n", nt_errstr(status));
223 if (!W_ERROR_IS_OK(r.out.result)) {
224 printf("QueryInfoKey failed - %s\n", win_errstr(r.out.result));
231 static BOOL test_key(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
232 struct policy_handle *handle, int depth);
234 static BOOL test_EnumKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
235 struct policy_handle *handle, int depth)
237 struct winreg_EnumKey r;
238 struct winreg_EnumKeyNameRequest keyname;
239 struct winreg_String classname;
240 struct winreg_Time tm;
243 printf("Testing EnumKey\n\n");
245 r.in.handle = handle;
247 r.in.key_name_len = r.out.key_name_len = 0;
248 r.in.unknown = r.out.unknown = 0x0414;
249 keyname.unknown = 0x0000020a;
250 init_winreg_String(&keyname.key_name, NULL);
251 init_winreg_String(&classname, NULL);
252 r.in.in_name = &keyname;
253 r.in.class = &classname;
254 tm.low = tm.high = 0x7fffffff;
255 r.in.last_changed_time = &tm;
258 status = dcerpc_winreg_EnumKey(p, mem_ctx, &r);
260 if (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(r.out.result)) {
261 struct policy_handle key_handle;
263 printf("EnumKey: %d: %s\n", r.in.enum_index, r.out.out_name->name);
266 p, mem_ctx, handle, r.out.out_name->name,
269 test_key(p, mem_ctx, &key_handle, depth + 1);
275 } while (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(r.out.result));
277 if (!NT_STATUS_IS_OK(status)) {
278 printf("EnumKey failed - %s\n", nt_errstr(status));
282 if (!W_ERROR_IS_OK(r.out.result) && !W_ERROR_EQUAL(r.out.result, WERR_NO_MORE_ITEMS)) {
283 printf("EnumKey failed - %s\n", win_errstr(r.out.result));
292 static BOOL test_QueryValue(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, struct policy_handle *handle, const char *valuename)
294 struct winreg_QueryValue r;
297 uint32 offered = 0xfff;
299 printf("Testing QueryValue\n");
301 r.in.handle = handle;
302 r.in.value_name.name = valuename;
304 r.in.size = &offered;
307 status = dcerpc_winreg_QueryValue(p, mem_ctx, &r);
308 if(NT_STATUS_IS_ERR(status)) {
309 printf("QueryValue failed - %s\n", nt_errstr(status));
313 if (!W_ERROR_IS_OK(r.out.result)) {
314 printf("QueryValue failed - %s\n", win_errstr(r.out.result));
321 static BOOL test_EnumValue(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
322 struct policy_handle *handle, int max_valnamelen, int max_valbufsize)
324 struct winreg_EnumValue r;
326 uint32 size = max_valbufsize, zero = 0;
331 printf("testing EnumValue\n");
333 r.in.handle = handle;
335 r.in.name_in.length = 0;
336 r.in.name_in.size = 0x200;
337 r.in.name_in.name = &buf16;
344 NTSTATUS status = dcerpc_winreg_EnumValue(p, mem_ctx, &r);
345 if(NT_STATUS_IS_ERR(status)) {
346 printf("EnumValue failed - %s\n", nt_errstr(status));
350 if (W_ERROR_IS_OK(r.out.result)) {
351 ret &= test_QueryValue(p, mem_ctx, handle, r.out.name_out.name);
355 } while (W_ERROR_IS_OK(r.out.result));
357 if(!W_ERROR_EQUAL(r.out.result, WERR_NO_MORE_ITEMS)) {
358 printf("EnumValue failed - %s\n", win_errstr(r.out.result));
365 static BOOL test_OpenHKLM(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
366 struct policy_handle *handle)
369 struct winreg_OpenHKLM r;
370 struct winreg_OpenUnknown unknown;
373 printf("\ntesting OpenHKLM\n");
375 unknown.unknown0 = 0x84e0;
376 unknown.unknown1 = 0x0000;
377 r.in.unknown = &unknown;
378 r.in.access_required = SEC_FLAG_MAXIMUM_ALLOWED;
379 r.out.handle = handle;
381 status = dcerpc_winreg_OpenHKLM(p, mem_ctx, &r);
383 if (!NT_STATUS_IS_OK(status)) {
384 printf("OpenHKLM failed - %s\n", nt_errstr(status));
388 if (!W_ERROR_IS_OK(r.out.result)) {
389 printf("OpenHKLM failed - %s\n", win_errstr(r.out.result));
396 static BOOL test_OpenHKU(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
397 struct policy_handle *handle)
400 struct winreg_OpenHKU r;
401 struct winreg_OpenUnknown unknown;
404 printf("\ntesting OpenHKU\n");
406 unknown.unknown0 = 0x84e0;
407 unknown.unknown1 = 0x0000;
408 r.in.unknown = &unknown;
409 r.in.access_required = SEC_FLAG_MAXIMUM_ALLOWED;
410 r.out.handle = handle;
412 status = dcerpc_winreg_OpenHKU(p, mem_ctx, &r);
414 if (!NT_STATUS_IS_OK(status)) {
415 printf("OpenHKU failed - %s\n", nt_errstr(status));
422 static BOOL test_OpenHKCR(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
423 struct policy_handle *handle)
426 struct winreg_OpenHKCR r;
427 struct winreg_OpenUnknown unknown;
430 printf("\ntesting OpenHKCR\n");
432 unknown.unknown0 = 0x84e0;
433 unknown.unknown1 = 0x0000;
434 r.in.unknown = &unknown;
435 r.in.access_required = SEC_FLAG_MAXIMUM_ALLOWED;
436 r.out.handle = handle;
438 status = dcerpc_winreg_OpenHKCR(p, mem_ctx, &r);
440 if (!NT_STATUS_IS_OK(status)) {
441 printf("OpenHKCR failed - %s\n", nt_errstr(status));
448 static BOOL test_InitiateSystemShutdown(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
449 const char *msg, uint32_t timeout)
451 struct winreg_InitiateSystemShutdown r;
454 init_winreg_String(&r.in.hostname, NULL);
455 init_winreg_String(&r.in.message, msg);
457 r.in.timeout = timeout;
459 status = dcerpc_winreg_InitiateSystemShutdown(p, mem_ctx, &r);
461 if (!NT_STATUS_IS_OK(status)) {
462 printf("InitiateSystemShutdown failed - %s\n", nt_errstr(status));
466 if (!W_ERROR_IS_OK(r.out.result)) {
467 printf("InitiateSystemShutdown failed - %s\n", win_errstr(r.out.result));
474 static BOOL test_AbortSystemShutdown(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
476 struct winreg_AbortSystemShutdown r;
478 uint16_t server = 0x0;
480 r.in.server = &server;
482 status = dcerpc_winreg_AbortSystemShutdown(p, mem_ctx, &r);
484 if (!NT_STATUS_IS_OK(status)) {
485 printf("AbortSystemShutdown failed - %s\n", nt_errstr(status));
489 if (!W_ERROR_IS_OK(r.out.result)) {
490 printf("AbortSystemShutdown failed - %s\n", win_errstr(r.out.result));
497 static BOOL test_OpenHKCU(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
498 struct policy_handle *handle)
501 struct winreg_OpenHKCU r;
502 struct winreg_OpenUnknown unknown;
505 printf("\ntesting OpenHKCU\n");
507 unknown.unknown0 = 0x84e0;
508 unknown.unknown1 = 0x0000;
509 r.in.unknown = &unknown;
510 r.in.access_required = SEC_FLAG_MAXIMUM_ALLOWED;
511 r.out.handle = handle;
513 status = dcerpc_winreg_OpenHKCU(p, mem_ctx, &r);
515 if (!NT_STATUS_IS_OK(status)) {
516 printf("OpenHKCU failed - %s\n", nt_errstr(status));
523 #define MAX_DEPTH 2 /* Only go this far down the tree */
525 static BOOL test_key(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
526 struct policy_handle *handle, int depth)
528 if (depth == MAX_DEPTH)
531 if (!test_QueryInfoKey(p, mem_ctx, handle, NULL)) {
534 if (!test_EnumKey(p, mem_ctx, handle, depth)) {
537 if (!test_EnumValue(p, mem_ctx, handle, 0xFF, 0xFFFF)) {
540 test_CloseKey(p, mem_ctx, handle);
545 typedef BOOL winreg_open_fn(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
546 struct policy_handle *handle);
548 static BOOL test_Open(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, void *fn)
550 struct policy_handle handle, newhandle;
552 winreg_open_fn *open_fn = (winreg_open_fn *)fn;
554 if (!open_fn(p, mem_ctx, &handle)) {
558 if (!test_CreateKey(p, mem_ctx, &handle, "spottyfoot", NULL)) {
559 printf("CreateKey failed\n");
563 if (!test_FlushKey(p, mem_ctx, &handle)) {
564 printf("FlushKey failed\n");
568 if (!test_OpenKey(p, mem_ctx, &handle, "spottyfoot", &newhandle)) {
569 printf("CreateKey failed (OpenKey after Create didn't work)\n");
573 if (!test_DeleteKey(p, mem_ctx, &handle, "spottyfoot")) {
574 printf("DeleteKey failed\n");
578 if (!test_FlushKey(p, mem_ctx, &handle)) {
579 printf("FlushKey failed\n");
583 if (test_OpenKey(p, mem_ctx, &handle, "spottyfoot", &newhandle)) {
584 printf("DeleteKey failed (OpenKey after Delete didn't work)\n");
588 if (!test_GetVersion(p, mem_ctx, &handle)) {
589 printf("GetVersion failed\n");
593 /* The HKCR hive has a very large fanout */
595 if (open_fn == test_OpenHKCR) {
596 if(!test_key(p, mem_ctx, &handle, MAX_DEPTH - 1)) {
601 if(!test_key(p, mem_ctx, &handle, 0)) {
608 BOOL torture_rpc_winreg(void)
611 struct dcerpc_pipe *p;
614 winreg_open_fn *open_fns[] = { test_OpenHKLM, test_OpenHKU,
615 test_OpenHKCR, test_OpenHKCU };
618 mem_ctx = talloc_init("torture_rpc_winreg");
620 status = torture_rpc_connection(&p,
623 DCERPC_WINREG_VERSION);
625 if (!NT_STATUS_IS_OK(status)) {
629 if(!test_InitiateSystemShutdown(p, mem_ctx, "spottyfood", 30))
632 if(!test_AbortSystemShutdown(p, mem_ctx))
635 for (i = 0; i < ARRAY_SIZE(open_fns); i++) {
636 if (!test_Open(p, mem_ctx, open_fns[i]))
640 talloc_destroy(mem_ctx);
642 torture_rpc_close(p);