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)) {
177 static BOOL test_DeleteKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
178 struct policy_handle *handle, const char *key)
181 struct winreg_DeleteKey r;
183 printf("\ntesting DeleteKey\n");
185 r.in.handle = handle;
186 init_winreg_String(&r.in.key, key);
188 status = dcerpc_winreg_DeleteKey(p, mem_ctx, &r);
190 if (!NT_STATUS_IS_OK(status)) {
191 printf("DeleteKey failed - %s\n", nt_errstr(status));
195 if (!W_ERROR_IS_OK(r.out.result)) {
196 printf("DeleteKey failed - %s\n", win_errstr(r.out.result));
203 static BOOL test_QueryInfoKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
204 struct policy_handle *handle, char *class)
207 struct winreg_QueryInfoKey r;
209 printf("\ntesting QueryInfoKey\n");
211 r.in.handle = handle;
212 init_winreg_String(&r.in.class, class);
214 status = dcerpc_winreg_QueryInfoKey(p, mem_ctx, &r);
216 if (!NT_STATUS_IS_OK(status)) {
217 printf("QueryInfoKey failed - %s\n", nt_errstr(status));
221 if (!W_ERROR_IS_OK(r.out.result)) {
222 printf("QueryInfoKey failed - %s\n", win_errstr(r.out.result));
229 static BOOL test_key(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
230 struct policy_handle *handle, int depth);
232 static BOOL test_EnumKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
233 struct policy_handle *handle, int depth)
235 struct winreg_EnumKey r;
236 struct winreg_EnumKeyNameRequest keyname;
237 struct winreg_String classname;
238 struct winreg_Time tm;
241 printf("Testing EnumKey\n\n");
243 r.in.handle = handle;
245 r.in.key_name_len = r.out.key_name_len = 0;
246 r.in.unknown = r.out.unknown = 0x0414;
247 keyname.unknown = 0x0000020a;
248 init_winreg_String(&keyname.key_name, NULL);
249 init_winreg_String(&classname, NULL);
250 r.in.in_name = &keyname;
251 r.in.class = &classname;
252 tm.low = tm.high = 0x7fffffff;
253 r.in.last_changed_time = &tm;
256 status = dcerpc_winreg_EnumKey(p, mem_ctx, &r);
258 if (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(r.out.result)) {
259 struct policy_handle key_handle;
261 printf("EnumKey: %d: %s\n", r.in.enum_index, r.out.out_name->name);
264 p, mem_ctx, handle, r.out.out_name->name,
266 printf("OpenKey(%s) failed - %s\n",
267 r.out.out_name->name,
268 win_errstr(r.out.result));
270 test_key(p, mem_ctx, &key_handle, depth + 1);
276 } while (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(r.out.result));
278 if (!NT_STATUS_IS_OK(status)) {
279 printf("EnumKey failed - %s\n", nt_errstr(status));
283 if (!W_ERROR_IS_OK(r.out.result) && !W_ERROR_EQUAL(r.out.result, WERR_NO_MORE_ITEMS)) {
284 printf("EnumKey failed - %s\n", win_errstr(r.out.result));
293 static BOOL test_EnumValue(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
294 struct policy_handle *handle, int max_valnamelen, int max_valbufsize)
296 struct winreg_EnumValue r;
297 struct EnumValueIn buf_name;
298 struct EnumValueIn buf_val;
300 uint32 len1 = max_valbufsize, len2 = 0;
302 printf("testing EnumValue\n");
304 r.in.handle = handle;
306 r.in.name_in.len = 0;
307 r.in.name_in.max_len = max_valnamelen * 2;
308 buf_name.max_len = max_valnamelen;
311 r.in.name_in.buffer = &buf_name;
313 buf_val.max_len = max_valbufsize;
316 r.in.value_in = &buf_val;
317 r.in.value_len1 = &len1;
318 r.in.value_len2 = &len2;
321 NTSTATUS status = dcerpc_winreg_EnumValue(p, mem_ctx, &r);
322 if(NT_STATUS_IS_ERR(status)) {
323 printf("EnumValue failed - %s\n", nt_errstr(status));
328 } while (W_ERROR_IS_OK(r.out.result));
330 if(!W_ERROR_EQUAL(r.out.result, WERR_NO_MORE_ITEMS)) {
331 printf("EnumValue failed - %s\n", win_errstr(r.out.result));
338 static BOOL test_OpenHKLM(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
339 struct policy_handle *handle)
342 struct winreg_OpenHKLM r;
343 struct winreg_OpenUnknown unknown;
346 printf("\ntesting OpenHKLM\n");
348 unknown.unknown0 = 0x84e0;
349 unknown.unknown1 = 0x0000;
350 r.in.unknown = &unknown;
351 r.in.access_required = SEC_FLAG_MAXIMUM_ALLOWED;
352 r.out.handle = handle;
354 status = dcerpc_winreg_OpenHKLM(p, mem_ctx, &r);
356 if (!NT_STATUS_IS_OK(status)) {
357 printf("OpenHKLM failed - %s\n", nt_errstr(status));
361 if (!W_ERROR_IS_OK(r.out.result)) {
362 printf("OpenHKLM failed - %s\n", win_errstr(r.out.result));
369 static BOOL test_OpenHKU(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
370 struct policy_handle *handle)
373 struct winreg_OpenHKU r;
374 struct winreg_OpenUnknown unknown;
377 printf("\ntesting OpenHKU\n");
379 unknown.unknown0 = 0x84e0;
380 unknown.unknown1 = 0x0000;
381 r.in.unknown = &unknown;
382 r.in.access_required = SEC_FLAG_MAXIMUM_ALLOWED;
383 r.out.handle = handle;
385 status = dcerpc_winreg_OpenHKU(p, mem_ctx, &r);
387 if (!NT_STATUS_IS_OK(status)) {
388 printf("OpenHKU failed - %s\n", nt_errstr(status));
395 static BOOL test_OpenHKCR(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
396 struct policy_handle *handle)
399 struct winreg_OpenHKCR r;
400 struct winreg_OpenUnknown unknown;
403 printf("\ntesting OpenHKCR\n");
405 unknown.unknown0 = 0x84e0;
406 unknown.unknown1 = 0x0000;
407 r.in.unknown = &unknown;
408 r.in.access_required = SEC_FLAG_MAXIMUM_ALLOWED;
409 r.out.handle = handle;
411 status = dcerpc_winreg_OpenHKCR(p, mem_ctx, &r);
413 if (!NT_STATUS_IS_OK(status)) {
414 printf("OpenHKCR failed - %s\n", nt_errstr(status));
421 static BOOL test_InitiateSystemShutdown(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
422 const char *msg, uint32_t timeout)
424 struct winreg_InitiateSystemShutdown r;
427 init_winreg_String(&r.in.message, msg);
429 r.in.timeout = timeout;
431 status = dcerpc_winreg_InitiateSystemShutdown(p, mem_ctx, &r);
433 if (!NT_STATUS_IS_OK(status)) {
434 printf("InitiateSystemShutdown failed - %s\n", nt_errstr(status));
438 if (!W_ERROR_IS_OK(r.out.result)) {
439 printf("InitiateSystemShutdown failed - %s\n", win_errstr(r.out.result));
446 static BOOL test_AbortSystemShutdown(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
448 struct winreg_AbortSystemShutdown r;
450 uint16_t server = 0x0;
452 r.in.server = &server;
454 status = dcerpc_winreg_AbortSystemShutdown(p, mem_ctx, &r);
456 if (!NT_STATUS_IS_OK(status)) {
457 printf("AbortSystemShutdown failed - %s\n", nt_errstr(status));
461 if (!W_ERROR_IS_OK(r.out.result)) {
462 printf("AbortSystemShutdown failed - %s\n", win_errstr(r.out.result));
469 static BOOL test_OpenHKCU(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
470 struct policy_handle *handle)
473 struct winreg_OpenHKCU r;
474 struct winreg_OpenUnknown unknown;
477 printf("\ntesting OpenHKCU\n");
479 unknown.unknown0 = 0x84e0;
480 unknown.unknown1 = 0x0000;
481 r.in.unknown = &unknown;
482 r.in.access_required = SEC_FLAG_MAXIMUM_ALLOWED;
483 r.out.handle = handle;
485 status = dcerpc_winreg_OpenHKCU(p, mem_ctx, &r);
487 if (!NT_STATUS_IS_OK(status)) {
488 printf("OpenHKCU failed - %s\n", nt_errstr(status));
495 #define MAX_DEPTH 2 /* Only go this far down the tree */
497 static BOOL test_key(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
498 struct policy_handle *handle, int depth)
500 if (depth == MAX_DEPTH)
503 if (!test_QueryInfoKey(p, mem_ctx, handle, NULL)) {
506 if (!test_EnumKey(p, mem_ctx, handle, depth)) {
509 if (!test_EnumValue(p, mem_ctx, handle, 0xFF, 0xFFFF)) {
512 test_CloseKey(p, mem_ctx, handle);
517 typedef BOOL winreg_open_fn(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
518 struct policy_handle *handle);
520 static BOOL test_Open(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, void *fn)
522 struct policy_handle handle, newhandle;
524 winreg_open_fn *open_fn = (winreg_open_fn *)fn;
526 if (!open_fn(p, mem_ctx, &handle)) {
530 if (!test_CreateKey(p, mem_ctx, &handle, "spottyfoot", NULL)) {
531 printf("CreateKey failed\n");
535 if (!test_FlushKey(p, mem_ctx, &handle)) {
536 printf("FlushKey failed\n");
540 if (!test_OpenKey(p, mem_ctx, &handle, "spottyfoot", &newhandle)) {
541 printf("CreateKey failed (OpenKey after Create didn't work)\n");
545 if (!test_DeleteKey(p, mem_ctx, &handle, "spottyfoot")) {
546 printf("DeleteKey failed\n");
550 if (!test_FlushKey(p, mem_ctx, &handle)) {
551 printf("FlushKey failed\n");
555 if (test_OpenKey(p, mem_ctx, &handle, "spottyfoot", &newhandle)) {
556 printf("DeleteKey failed (OpenKey after Delete didn't work)\n");
560 if (!test_GetVersion(p, mem_ctx, &handle)) {
561 printf("GetVersion failed\n");
565 /* The HKCR hive has a very large fanout */
567 if (open_fn == test_OpenHKCR) {
568 if(!test_key(p, mem_ctx, &handle, MAX_DEPTH - 1)) {
573 if(!test_key(p, mem_ctx, &handle, 0)) {
580 BOOL torture_rpc_winreg(void)
583 struct dcerpc_pipe *p;
586 winreg_open_fn *open_fns[] = { test_OpenHKLM, test_OpenHKU,
587 test_OpenHKCR, test_OpenHKCU };
590 mem_ctx = talloc_init("torture_rpc_winreg");
592 status = torture_rpc_connection(&p,
595 DCERPC_WINREG_VERSION);
597 if (!NT_STATUS_IS_OK(status)) {
601 if(!test_InitiateSystemShutdown(p, mem_ctx, "spottyfood", 30))
604 if(!test_AbortSystemShutdown(p, mem_ctx))
607 for (i = 0; i < ARRAY_SIZE(open_fns); i++) {
608 if (!test_Open(p, mem_ctx, open_fns[i]))
612 talloc_destroy(mem_ctx);
614 torture_rpc_close(p);