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, const char *name, const char *class)
59 struct winreg_CreateKey r;
60 struct policy_handle newhandle;
63 printf("\ntesting CreateKey\n");
66 r.out.handle = &newhandle;
67 init_winreg_String(&r.in.key, name);
68 init_winreg_String(&r.in.class, class);
71 r.in.access_mask = 0x02000000;
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 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 (!W_ERROR_IS_OK(r.out.result)) {
149 printf("OpenKey failed - %s\n", win_errstr(r.out.result));
156 static BOOL test_DeleteKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
157 struct policy_handle *handle, char *key)
160 struct winreg_DeleteKey r;
162 printf("\ntesting DeleteKey\n");
164 r.in.handle = handle;
165 init_winreg_String(&r.in.key, key);
167 status = dcerpc_winreg_DeleteKey(p, mem_ctx, &r);
169 if (!NT_STATUS_IS_OK(status)) {
170 printf("DeleteKey failed - %s\n", nt_errstr(status));
174 if (!W_ERROR_IS_OK(r.out.result)) {
175 printf("DeleteKey failed - %s\n", win_errstr(r.out.result));
182 static BOOL test_QueryInfoKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
183 struct policy_handle *handle, char *class)
186 struct winreg_QueryInfoKey r;
188 printf("\ntesting QueryInfoKey\n");
190 r.in.handle = handle;
191 init_winreg_String(&r.in.class, class);
193 status = dcerpc_winreg_QueryInfoKey(p, mem_ctx, &r);
195 if (!W_ERROR_IS_OK(r.out.result)) {
196 printf("QueryInfoKey failed - %s\n", win_errstr(r.out.result));
203 static BOOL test_key(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
204 struct policy_handle *handle, int depth);
206 static BOOL test_EnumKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
207 struct policy_handle *handle, int depth)
209 struct winreg_EnumKey r;
210 struct winreg_EnumKeyNameRequest keyname;
211 struct winreg_String classname;
212 struct winreg_Time tm;
215 r.in.handle = handle;
217 r.in.key_name_len = r.out.key_name_len = 0;
218 r.in.unknown = r.out.unknown = 0x0414;
219 keyname.unknown = 0x0000020a;
220 init_winreg_String(&keyname.key_name, NULL);
221 init_winreg_String(&classname, NULL);
222 r.in.in_name = &keyname;
223 r.in.class = &classname;
224 tm.low = tm.high = 0x7fffffff;
225 r.in.last_changed_time = &tm;
228 status = dcerpc_winreg_EnumKey(p, mem_ctx, &r);
230 if (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(r.out.result)) {
231 struct policy_handle key_handle;
234 p, mem_ctx, handle, r.out.out_name->name,
236 printf("OpenKey(%s) failed - %s\n",
237 r.out.out_name->name,
238 win_errstr(r.out.result));
242 test_key(p, mem_ctx, &key_handle, depth + 1);
249 } while (W_ERROR_IS_OK(r.out.result));
254 static BOOL test_EnumValue(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
255 struct policy_handle *handle)
257 struct winreg_EnumValue r;
258 struct winreg_Uint8buf value;
259 struct winreg_String name;
260 uint32 type, requested_len, returned_len;
263 r.in.handle = handle;
266 init_winreg_String(&name, NULL);
267 r.in.name = r.out.name = &name;
272 value.max_len = 0xffff;
279 requested_len = value.max_len;
280 r.in.requested_len = &requested_len;
282 r.in.returned_len = &returned_len;
286 status = dcerpc_winreg_EnumValue(p, mem_ctx, &r);
288 } while (W_ERROR_IS_OK(r.out.result));
293 static BOOL test_OpenHKLM(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
294 struct policy_handle *handle)
297 struct winreg_OpenHKLM r;
298 struct winreg_OpenUnknown unknown;
301 printf("\ntesting OpenHKLM\n");
303 unknown.unknown0 = 0x84e0;
304 unknown.unknown1 = 0x0000;
305 r.in.unknown = &unknown;
306 r.in.access_required = SEC_RIGHTS_MAXIMUM_ALLOWED;
307 r.out.handle = handle;
309 status = dcerpc_winreg_OpenHKLM(p, mem_ctx, &r);
311 if (!NT_STATUS_IS_OK(status)) {
312 printf("OpenHKLM failed - %s\n", nt_errstr(status));
319 static BOOL test_OpenHKU(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
320 struct policy_handle *handle)
323 struct winreg_OpenHKU r;
324 struct winreg_OpenUnknown unknown;
327 printf("\ntesting OpenHKU\n");
329 unknown.unknown0 = 0x84e0;
330 unknown.unknown1 = 0x0000;
331 r.in.unknown = &unknown;
332 r.in.access_required = SEC_RIGHTS_MAXIMUM_ALLOWED;
333 r.out.handle = handle;
335 status = dcerpc_winreg_OpenHKU(p, mem_ctx, &r);
337 if (!NT_STATUS_IS_OK(status)) {
338 printf("OpenHKU failed - %s\n", nt_errstr(status));
345 static BOOL test_OpenHKCR(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
346 struct policy_handle *handle)
349 struct winreg_OpenHKCR r;
350 struct winreg_OpenUnknown unknown;
353 printf("\ntesting OpenHKCR\n");
355 unknown.unknown0 = 0x84e0;
356 unknown.unknown1 = 0x0000;
357 r.in.unknown = &unknown;
358 r.in.access_required = SEC_RIGHTS_MAXIMUM_ALLOWED;
359 r.out.handle = handle;
361 status = dcerpc_winreg_OpenHKCR(p, mem_ctx, &r);
363 if (!NT_STATUS_IS_OK(status)) {
364 printf("OpenHKCR failed - %s\n", nt_errstr(status));
371 static BOOL test_OpenHKCU(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
372 struct policy_handle *handle)
375 struct winreg_OpenHKCU r;
376 struct winreg_OpenUnknown unknown;
379 printf("\ntesting OpenHKCU\n");
381 unknown.unknown0 = 0x84e0;
382 unknown.unknown1 = 0x0000;
383 r.in.unknown = &unknown;
384 r.in.access_required = SEC_RIGHTS_MAXIMUM_ALLOWED;
385 r.out.handle = handle;
387 status = dcerpc_winreg_OpenHKCU(p, mem_ctx, &r);
389 if (!NT_STATUS_IS_OK(status)) {
390 printf("OpenHKCU failed - %s\n", nt_errstr(status));
397 #define MAX_DEPTH 2 /* Only go this far down the tree */
399 static BOOL test_key(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
400 struct policy_handle *handle, int depth)
402 if (depth == MAX_DEPTH)
405 if (!test_QueryInfoKey(p, mem_ctx, handle, NULL)) {
408 if (!test_EnumKey(p, mem_ctx, handle, depth)) {
411 if (!test_EnumValue(p, mem_ctx, handle)) {
414 /* Enumerate values */
416 test_CloseKey(p, mem_ctx, handle);
421 typedef BOOL winreg_open_fn(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
422 struct policy_handle *handle);
424 static BOOL test_Open(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, void *fn)
426 struct policy_handle handle, newhandle;
428 winreg_open_fn *open_fn = (winreg_open_fn *)fn;
430 if (!open_fn(p, mem_ctx, &handle))
433 if (!test_GetVersion(p, mem_ctx, &handle)) {
434 printf("GetVersion failed\n");
438 if (!test_FlushKey(p, mem_ctx, &handle)) {
439 printf("FlushKey failed\n");
443 if (!test_CreateKey(p, mem_ctx, &handle, "spottyfoot", "foo")) {
444 printf("CreateKey failed\n");
448 if (!test_OpenKey(p, mem_ctx, &handle, "spottyfoot", &newhandle)) {
449 printf("CreateKey failed (OpenKey after Create didn't work)\n");
453 if (!test_DeleteKey(p, mem_ctx, &handle, "spottyfoot")) {
454 printf("DeleteKey failed\n");
458 if (test_OpenKey(p, mem_ctx, &handle, "spottyfoot", &newhandle)) {
459 printf("DeleteKey failed (OpenKey after Delete didn't work)\n");
464 /* The HKCR hive has a very large fanout */
466 if (open_fn == test_OpenHKCR) {
467 if(!test_key(p, mem_ctx, &handle, MAX_DEPTH - 1)) {
472 if(!test_key(p, mem_ctx, &handle, 0)) {
479 BOOL torture_rpc_winreg(int dummy)
482 struct dcerpc_pipe *p;
485 winreg_open_fn *open_fns[] = { test_OpenHKLM, test_OpenHKU,
486 test_OpenHKCR, test_OpenHKCU };
489 mem_ctx = talloc_init("torture_rpc_winreg");
491 status = torture_rpc_connection(&p,
494 DCERPC_WINREG_VERSION);
496 if (!NT_STATUS_IS_OK(status)) {
500 for (i = 0; i < ARRAY_SIZE(open_fns); i++) {
501 if (!test_Open(p, mem_ctx, open_fns[i]))
505 talloc_destroy(mem_ctx);
507 torture_rpc_close(p);