2 Unix SMB/CIFS implementation.
3 test suite for winreg rpc operations
5 Copyright (C) Tim Potter 2003
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 static void init_winreg_String(struct winreg_String *name, const char *s)
28 name->name_len = 2 * (strlen_m(s) + 1);
29 name->name_size = name->name_len;
36 static BOOL test_GetVersion(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
37 struct policy_handle *handle)
40 struct winreg_GetVersion r;
42 printf("\ntesting GetVersion\n");
46 status = dcerpc_winreg_GetVersion(p, mem_ctx, &r);
48 if (!NT_STATUS_IS_OK(status)) {
49 printf("GetVersion failed - %s\n", nt_errstr(status));
56 static BOOL test_CreateKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
57 struct policy_handle *handle, char *name, const char *class)
59 struct winreg_CreateKey r;
60 struct policy_handle newhandle;
62 struct sec_desc_buf sec_desc;
65 printf("\ntesting CreateKey\n");
68 r.out.handle = &newhandle;
69 init_winreg_String(&r.in.key, name);
70 init_winreg_String(&r.in.class, class);
72 r.in.access_mask = 0x02000000;
73 r.in.sec_info = &sec_info;
76 status = dcerpc_winreg_CreateKey(p, mem_ctx, &r);
78 if (!NT_STATUS_IS_OK(status)) {
79 printf("CreateKey failed - %s\n", nt_errstr(status));
83 if (!W_ERROR_IS_OK(r.out.result)) {
84 printf("CreateKey failed - %s\n", win_errstr(r.out.result));
91 static BOOL test_CloseKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
92 struct policy_handle *handle)
95 struct winreg_CloseKey r;
97 printf("\ntesting CloseKey\n");
99 r.in.handle = r.out.handle = handle;
101 status = dcerpc_winreg_CloseKey(p, mem_ctx, &r);
103 if (!NT_STATUS_IS_OK(status)) {
104 printf("CloseKey failed - %s\n", nt_errstr(status));
111 static BOOL test_FlushKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
112 struct policy_handle *handle)
115 struct winreg_FlushKey r;
117 printf("\ntesting FlushKey\n");
119 r.in.handle = handle;
121 status = dcerpc_winreg_FlushKey(p, mem_ctx, &r);
123 if (!NT_STATUS_IS_OK(status)) {
124 printf("FlushKey failed - %s\n", nt_errstr(status));
131 static BOOL test_OpenKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
132 struct policy_handle *hive_handle,
133 const char *keyname, struct policy_handle *key_handle)
136 struct winreg_OpenKey r;
138 printf("\ntesting OpenKey\n");
140 r.in.handle = hive_handle;
141 init_winreg_String(&r.in.keyname, keyname);
142 r.in.unknown = 0x00000000;
143 r.in.access_mask = 0x02000000;
144 r.out.handle = key_handle;
146 status = dcerpc_winreg_OpenKey(p, mem_ctx, &r);
148 if (!NT_STATUS_IS_OK(status)) {
149 printf("OpenKey failed - %s\n", nt_errstr(status));
153 if (!W_ERROR_IS_OK(r.out.result)) {
154 printf("OpenKey failed - %s\n", win_errstr(r.out.result));
161 static BOOL test_DeleteKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
162 struct policy_handle *handle, const char *key)
165 struct winreg_DeleteKey r;
167 printf("\ntesting DeleteKey\n");
169 r.in.handle = handle;
170 init_winreg_String(&r.in.key, key);
172 status = dcerpc_winreg_DeleteKey(p, mem_ctx, &r);
174 if (!NT_STATUS_IS_OK(status)) {
175 printf("DeleteKey failed - %s\n", nt_errstr(status));
179 if (!W_ERROR_IS_OK(r.out.result)) {
180 printf("DeleteKey failed - %s\n", win_errstr(r.out.result));
187 static BOOL test_QueryInfoKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
188 struct policy_handle *handle, char *class)
191 struct winreg_QueryInfoKey r;
193 printf("\ntesting QueryInfoKey\n");
195 r.in.handle = handle;
196 init_winreg_String(&r.in.class, class);
198 status = dcerpc_winreg_QueryInfoKey(p, mem_ctx, &r);
200 if (!NT_STATUS_IS_OK(status)) {
201 printf("QueryInfoKey failed - %s\n", nt_errstr(status));
205 if (!W_ERROR_IS_OK(r.out.result)) {
206 printf("QueryInfoKey failed - %s\n", win_errstr(r.out.result));
213 static BOOL test_key(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
214 struct policy_handle *handle, int depth);
216 static BOOL test_EnumKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
217 struct policy_handle *handle, int depth)
219 struct winreg_EnumKey r;
220 struct winreg_EnumKeyNameRequest keyname;
221 struct winreg_String classname;
222 struct winreg_Time tm;
225 r.in.handle = handle;
227 r.in.key_name_len = r.out.key_name_len = 0;
228 r.in.unknown = r.out.unknown = 0x0414;
229 keyname.unknown = 0x0000020a;
230 init_winreg_String(&keyname.key_name, NULL);
231 init_winreg_String(&classname, NULL);
232 r.in.in_name = &keyname;
233 r.in.class = &classname;
234 tm.low = tm.high = 0x7fffffff;
235 r.in.last_changed_time = &tm;
238 status = dcerpc_winreg_EnumKey(p, mem_ctx, &r);
240 if (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(r.out.result)) {
241 struct policy_handle key_handle;
244 p, mem_ctx, handle, r.out.out_name->name,
246 printf("OpenKey(%s) failed - %s\n",
247 r.out.out_name->name,
248 win_errstr(r.out.result));
252 test_key(p, mem_ctx, &key_handle, depth + 1);
259 } while (W_ERROR_IS_OK(r.out.result));
264 static BOOL test_EnumValue(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
265 struct policy_handle *handle)
267 struct winreg_EnumValue r;
268 struct winreg_Uint8buf value;
269 struct winreg_String name;
270 uint32 type, requested_len, returned_len;
273 r.in.handle = handle;
276 init_winreg_String(&name, NULL);
277 r.in.name = r.out.name = &name;
282 value.max_len = 0xffff;
289 requested_len = value.max_len;
290 r.in.requested_len = &requested_len;
292 r.in.returned_len = &returned_len;
296 status = dcerpc_winreg_EnumValue(p, mem_ctx, &r);
298 } while (W_ERROR_IS_OK(r.out.result));
303 static BOOL test_OpenHKLM(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
304 struct policy_handle *handle)
307 struct winreg_OpenHKLM r;
308 struct winreg_OpenUnknown unknown;
311 printf("\ntesting OpenHKLM\n");
313 unknown.unknown0 = 0x84e0;
314 unknown.unknown1 = 0x0000;
315 r.in.unknown = &unknown;
316 r.in.access_required = SEC_RIGHTS_MAXIMUM_ALLOWED;
317 r.out.handle = handle;
319 status = dcerpc_winreg_OpenHKLM(p, mem_ctx, &r);
321 if (!NT_STATUS_IS_OK(status)) {
322 printf("OpenHKLM failed - %s\n", nt_errstr(status));
329 static BOOL test_OpenHKU(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
330 struct policy_handle *handle)
333 struct winreg_OpenHKU r;
334 struct winreg_OpenUnknown unknown;
337 printf("\ntesting OpenHKU\n");
339 unknown.unknown0 = 0x84e0;
340 unknown.unknown1 = 0x0000;
341 r.in.unknown = &unknown;
342 r.in.access_required = SEC_RIGHTS_MAXIMUM_ALLOWED;
343 r.out.handle = handle;
345 status = dcerpc_winreg_OpenHKU(p, mem_ctx, &r);
347 if (!NT_STATUS_IS_OK(status)) {
348 printf("OpenHKU failed - %s\n", nt_errstr(status));
355 static BOOL test_OpenHKCR(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
356 struct policy_handle *handle)
359 struct winreg_OpenHKCR r;
360 struct winreg_OpenUnknown unknown;
363 printf("\ntesting OpenHKCR\n");
365 unknown.unknown0 = 0x84e0;
366 unknown.unknown1 = 0x0000;
367 r.in.unknown = &unknown;
368 r.in.access_required = SEC_RIGHTS_MAXIMUM_ALLOWED;
369 r.out.handle = handle;
371 status = dcerpc_winreg_OpenHKCR(p, mem_ctx, &r);
373 if (!NT_STATUS_IS_OK(status)) {
374 printf("OpenHKCR failed - %s\n", nt_errstr(status));
381 static BOOL test_InitiateSystemShutdown(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
382 const char *msg, uint32 timeout)
384 struct winreg_InitiateSystemShutdown r;
387 init_winreg_String(&r.in.message, msg);
389 r.in.timeout = timeout;
391 status = dcerpc_winreg_InitiateSystemShutdown(p, mem_ctx, &r);
393 if (!NT_STATUS_IS_OK(status)) {
394 printf("InitiateSystemShutdown failed - %s\n", nt_errstr(status));
398 if (!W_ERROR_IS_OK(r.out.result)) {
399 printf("InitiateSystemShutdown failed - %s\n", win_errstr(r.out.result));
406 static BOOL test_AbortSystemShutdown(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
408 struct winreg_AbortSystemShutdown r;
413 status = dcerpc_winreg_AbortSystemShutdown(p, mem_ctx, &r);
415 if (!NT_STATUS_IS_OK(status)) {
416 printf("AbortSystemShutdown failed - %s\n", nt_errstr(status));
420 if (!W_ERROR_IS_OK(r.out.result)) {
421 printf("AbortSystemShutdown failed - %s\n", win_errstr(r.out.result));
428 static BOOL test_OpenHKCU(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
429 struct policy_handle *handle)
432 struct winreg_OpenHKCU r;
433 struct winreg_OpenUnknown unknown;
436 printf("\ntesting OpenHKCU\n");
438 unknown.unknown0 = 0x84e0;
439 unknown.unknown1 = 0x0000;
440 r.in.unknown = &unknown;
441 r.in.access_required = SEC_RIGHTS_MAXIMUM_ALLOWED;
442 r.out.handle = handle;
444 status = dcerpc_winreg_OpenHKCU(p, mem_ctx, &r);
446 if (!NT_STATUS_IS_OK(status)) {
447 printf("OpenHKCU failed - %s\n", nt_errstr(status));
454 #define MAX_DEPTH 2 /* Only go this far down the tree */
456 static BOOL test_key(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
457 struct policy_handle *handle, int depth)
459 if (depth == MAX_DEPTH)
462 if (!test_QueryInfoKey(p, mem_ctx, handle, NULL)) {
465 if (!test_EnumKey(p, mem_ctx, handle, depth)) {
468 if (!test_EnumValue(p, mem_ctx, handle)) {
471 /* Enumerate values */
473 test_CloseKey(p, mem_ctx, handle);
478 typedef BOOL winreg_open_fn(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
479 struct policy_handle *handle);
481 static BOOL test_Open(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, void *fn)
483 struct policy_handle handle, newhandle;
485 winreg_open_fn *open_fn = (winreg_open_fn *)fn;
487 if (!open_fn(p, mem_ctx, &handle))
490 if (!test_CreateKey(p, mem_ctx, &handle, "spottyfoot", NULL)) {
491 printf("CreateKey failed\n");
495 if (!test_OpenKey(p, mem_ctx, &handle, "spottyfoot", &newhandle)) {
496 printf("CreateKey failed (OpenKey after Create didn't work)\n");
500 if (!test_DeleteKey(p, mem_ctx, &handle, "spottyfoot")) {
501 printf("DeleteKey failed\n");
505 if (test_OpenKey(p, mem_ctx, &handle, "spottyfoot", &newhandle)) {
506 printf("DeleteKey failed (OpenKey after Delete didn't work)\n");
510 if (!test_GetVersion(p, mem_ctx, &handle)) {
511 printf("GetVersion failed\n");
515 if (!test_FlushKey(p, mem_ctx, &handle)) {
516 printf("FlushKey failed\n");
521 /* The HKCR hive has a very large fanout */
523 if (open_fn == test_OpenHKCR) {
524 if(!test_key(p, mem_ctx, &handle, MAX_DEPTH - 1)) {
529 if(!test_key(p, mem_ctx, &handle, 0)) {
536 BOOL torture_rpc_winreg(int dummy)
539 struct dcerpc_pipe *p;
542 winreg_open_fn *open_fns[] = { test_OpenHKLM, test_OpenHKU,
543 test_OpenHKCR, test_OpenHKCU };
546 mem_ctx = talloc_init("torture_rpc_winreg");
548 status = torture_rpc_connection(&p,
551 DCERPC_WINREG_VERSION);
553 if (!NT_STATUS_IS_OK(status)) {
557 if(!test_InitiateSystemShutdown(p, mem_ctx, "spottyfood", 30))
560 for (i = 0; i < ARRAY_SIZE(open_fns); i++) {
561 if (!test_Open(p, mem_ctx, open_fns[i]))
565 talloc_destroy(mem_ctx);
567 torture_rpc_close(p);