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 "torture/torture.h"
25 #include "librpc/gen_ndr/ndr_winreg.h"
26 #include "librpc/gen_ndr/ndr_winreg_c.h"
27 #include "librpc/gen_ndr/ndr_security.h"
28 #include "libcli/security/proto.h"
29 #include "torture/rpc/rpc.h"
31 #define TEST_KEY_BASE "smbtorture test"
32 #define TEST_KEY1 TEST_KEY_BASE "\\spottyfoot"
33 #define TEST_KEY2 TEST_KEY_BASE "\\with a SD (#1)"
35 static void init_initshutdown_String(TALLOC_CTX *mem_ctx, struct initshutdown_String *name, const char *s)
37 name->name = talloc(mem_ctx, struct initshutdown_String_sub);
41 static void init_winreg_String(struct winreg_String *name, const char *s)
45 name->name_len = 2 * (strlen_m(s) + 1);
46 name->name_size = name->name_len;
53 static BOOL test_GetVersion(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
54 struct policy_handle *handle)
57 struct winreg_GetVersion r;
59 printf("\ntesting GetVersion\n");
63 status = dcerpc_winreg_GetVersion(p, mem_ctx, &r);
65 if (!NT_STATUS_IS_OK(status)) {
66 printf("GetVersion failed - %s\n", nt_errstr(status));
70 if (!W_ERROR_IS_OK(r.out.result)) {
71 printf("GetVersion failed - %s\n", win_errstr(r.out.result));
78 static BOOL test_NotifyChangeKeyValue(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
79 struct policy_handle *handle)
81 struct winreg_NotifyChangeKeyValue r;
84 printf("\ntesting NotifyChangeKeyValue\n");
87 r.in.watch_subtree = 1;
88 r.in.notify_filter = 0;
89 r.in.unknown = r.in.unknown2 = 0;
90 init_winreg_String(&r.in.string1, NULL);
91 init_winreg_String(&r.in.string2, NULL);
93 status = dcerpc_winreg_NotifyChangeKeyValue(p, mem_ctx, &r);
95 if (!NT_STATUS_IS_OK(status)) {
96 printf("NotifyChangeKeyValue failed - %s\n", nt_errstr(status));
100 if (!W_ERROR_IS_OK(r.out.result)) {
101 printf("NotifyChangeKeyValue failed - %s - not considering\n", win_errstr(r.out.result));
108 static BOOL test_CreateKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
109 struct policy_handle *handle, const char *name,
112 struct winreg_CreateKey r;
113 struct policy_handle newhandle;
115 uint32_t action_taken = 0;
117 printf("\ntesting CreateKey\n");
119 r.in.handle = handle;
120 r.out.new_handle = &newhandle;
121 init_winreg_String(&r.in.name, name);
122 init_winreg_String(&r.in.class, class);
124 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
125 r.in.action_taken = r.out.action_taken = &action_taken;
128 status = dcerpc_winreg_CreateKey(p, mem_ctx, &r);
130 if (!NT_STATUS_IS_OK(status)) {
131 printf("CreateKey failed - %s\n", nt_errstr(status));
135 if (!W_ERROR_IS_OK(r.out.result)) {
136 printf("CreateKey failed - %s\n", win_errstr(r.out.result));
145 createkey testing with a SD
147 static BOOL test_CreateKey_sd(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
148 struct policy_handle *handle, const char *name,
149 const char *class, struct policy_handle *newhandle)
151 struct winreg_CreateKey r;
153 uint32_t action_taken = 0;
154 struct security_descriptor *sd;
156 struct winreg_SecBuf secbuf;
158 sd = security_descriptor_create(mem_ctx,
160 SID_NT_AUTHENTICATED_USERS,
161 SEC_ACE_TYPE_ACCESS_ALLOWED,
163 SEC_ACE_FLAG_OBJECT_INHERIT,
166 status = ndr_push_struct_blob(&sdblob, mem_ctx, sd,
167 (ndr_push_flags_fn_t)ndr_push_security_descriptor);
168 if (!NT_STATUS_IS_OK(status)) {
169 printf("Failed to push security_descriptor ?!\n");
173 secbuf.sd.data = sdblob.data;
174 secbuf.sd.len = sdblob.length;
175 secbuf.sd.size = sdblob.length;
176 secbuf.length = sdblob.length-10;
179 printf("\ntesting CreateKey with sd\n");
181 r.in.handle = handle;
182 r.out.new_handle = newhandle;
183 init_winreg_String(&r.in.name, name);
184 init_winreg_String(&r.in.class, class);
186 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
187 r.in.action_taken = r.out.action_taken = &action_taken;
188 r.in.secdesc = &secbuf;
190 status = dcerpc_winreg_CreateKey(p, mem_ctx, &r);
192 if (!NT_STATUS_IS_OK(status)) {
193 printf("CreateKey with sd failed - %s\n", nt_errstr(status));
197 if (!W_ERROR_IS_OK(r.out.result)) {
198 printf("CreateKey with sd failed - %s\n", win_errstr(r.out.result));
205 static BOOL test_GetKeySecurity(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
206 struct policy_handle *handle)
209 struct winreg_GetKeySecurity r;
210 struct security_descriptor sd;
213 printf("\ntesting GetKeySecurity\n");
217 r.in.handle = handle;
218 r.in.sd = r.out.sd = talloc_zero(mem_ctx, struct KeySecurityData);
219 r.in.sd->size = 0x1000;
220 r.in.sec_info = SECINFO_OWNER | SECINFO_GROUP | SECINFO_DACL;
222 status = dcerpc_winreg_GetKeySecurity(p, mem_ctx, &r);
224 if (!NT_STATUS_IS_OK(status)) {
225 printf("GetKeySecurity failed - %s\n", nt_errstr(status));
229 if (!W_ERROR_IS_OK(r.out.result)) {
230 printf("GetKeySecurity failed - %s\n", win_errstr(r.out.result));
234 sdblob.data = r.out.sd->data;
235 sdblob.length = r.out.sd->len;
237 status = ndr_pull_struct_blob(&sdblob, mem_ctx, &sd,
238 (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
239 if (!NT_STATUS_IS_OK(status)) {
240 printf("pull_security_descriptor failed - %s\n", nt_errstr(status));
243 if (p->conn->flags & DCERPC_DEBUG_PRINT_OUT) {
244 NDR_PRINT_DEBUG(security_descriptor, &sd);
250 static BOOL test_CloseKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
251 struct policy_handle *handle)
254 struct winreg_CloseKey r;
256 printf("\ntesting CloseKey\n");
258 r.in.handle = r.out.handle = handle;
260 status = dcerpc_winreg_CloseKey(p, mem_ctx, &r);
262 if (!NT_STATUS_IS_OK(status)) {
263 printf("CloseKey failed - %s\n", nt_errstr(status));
267 if (!W_ERROR_IS_OK(r.out.result)) {
268 printf("CloseKey failed - %s\n", win_errstr(r.out.result));
275 static BOOL test_FlushKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
276 struct policy_handle *handle)
279 struct winreg_FlushKey r;
281 printf("\ntesting FlushKey\n");
283 r.in.handle = handle;
285 status = dcerpc_winreg_FlushKey(p, mem_ctx, &r);
287 if (!NT_STATUS_IS_OK(status)) {
288 printf("FlushKey failed - %s\n", nt_errstr(status));
292 if (!W_ERROR_IS_OK(r.out.result)) {
293 printf("FlushKey failed - %s\n", win_errstr(r.out.result));
300 static BOOL test_OpenKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
301 struct policy_handle *hive_handle,
302 const char *keyname, struct policy_handle *key_handle)
305 struct winreg_OpenKey r;
307 printf("\ntesting OpenKey\n");
309 r.in.handle = hive_handle;
310 init_winreg_String(&r.in.keyname, keyname);
311 r.in.unknown = 0x00000000;
312 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
313 r.out.handle = key_handle;
315 status = dcerpc_winreg_OpenKey(p, mem_ctx, &r);
317 if (!NT_STATUS_IS_OK(status)) {
318 printf("OpenKey failed - %s\n", nt_errstr(status));
322 if (!W_ERROR_IS_OK(r.out.result)) {
323 printf("OpenKey failed - %s\n", win_errstr(r.out.result));
331 static BOOL test_Cleanup(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
332 struct policy_handle *handle, const char *key)
334 struct winreg_DeleteKey r;
336 r.in.handle = handle;
338 init_winreg_String(&r.in.key, key);
339 dcerpc_winreg_DeleteKey(p, mem_ctx, &r);
345 static BOOL test_DeleteKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
346 struct policy_handle *handle, const char *key)
349 struct winreg_DeleteKey r;
351 printf("\ntesting DeleteKey\n");
353 r.in.handle = handle;
354 init_winreg_String(&r.in.key, key);
356 status = dcerpc_winreg_DeleteKey(p, mem_ctx, &r);
358 if (!NT_STATUS_IS_OK(status)) {
359 printf("DeleteKey failed - %s\n", nt_errstr(status));
363 if (!W_ERROR_IS_OK(r.out.result)) {
364 printf("DeleteKey failed - %s\n", win_errstr(r.out.result));
371 static BOOL test_QueryInfoKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
372 struct policy_handle *handle, char *class)
375 struct winreg_QueryInfoKey r;
377 printf("\ntesting QueryInfoKey\n");
379 r.in.handle = handle;
380 init_winreg_String(&r.in.class, class);
382 status = dcerpc_winreg_QueryInfoKey(p, mem_ctx, &r);
384 if (!NT_STATUS_IS_OK(status)) {
385 printf("QueryInfoKey failed - %s\n", nt_errstr(status));
389 if (!W_ERROR_IS_OK(r.out.result)) {
390 printf("QueryInfoKey failed - %s\n", win_errstr(r.out.result));
397 static BOOL test_key(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
398 struct policy_handle *handle, int depth);
400 static BOOL test_EnumKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
401 struct policy_handle *handle, int depth)
403 struct winreg_EnumKey r;
404 struct winreg_StringBuf class, name;
408 printf("Testing EnumKey\n\n");
413 r.in.handle = handle;
418 r.in.last_changed_time = &t;
424 status = dcerpc_winreg_EnumKey(p, mem_ctx, &r);
426 if (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(r.out.result)) {
427 struct policy_handle key_handle;
429 printf("EnumKey: %d: %s\n", r.in.enum_index, r.out.name->name);
432 p, mem_ctx, handle, r.out.name->name,
435 test_key(p, mem_ctx, &key_handle, depth + 1);
441 } while (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(r.out.result));
443 if (!NT_STATUS_IS_OK(status)) {
444 printf("EnumKey failed - %s\n", nt_errstr(status));
448 if (!W_ERROR_IS_OK(r.out.result) && !W_ERROR_EQUAL(r.out.result, WERR_NO_MORE_ITEMS)) {
449 printf("EnumKey failed - %s\n", win_errstr(r.out.result));
458 static BOOL test_QueryMultipleValues(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, struct policy_handle *handle, const char *valuename)
460 struct winreg_QueryMultipleValues r;
464 printf("Testing QueryMultipleValues\n");
466 r.in.key_handle = handle;
467 r.in.values = r.out.values = talloc_array(mem_ctx, struct QueryMultipleValue, 1);
468 r.in.values[0].name = talloc(mem_ctx, struct winreg_String);
469 r.in.values[0].name->name = valuename;
470 r.in.values[0].offset = 0;
471 r.in.values[0].length = 0;
472 r.in.values[0].type = 0;
475 r.in.buffer_size = r.out.buffer_size = talloc(mem_ctx, uint32_t);
476 *r.in.buffer_size = bufsize;
478 *r.in.buffer_size = bufsize;
479 r.in.buffer = r.out.buffer = talloc_zero_array(mem_ctx, uint8_t,
482 status = dcerpc_winreg_QueryMultipleValues(p, mem_ctx, &r);
484 if(NT_STATUS_IS_ERR(status)) {
485 printf("QueryMultipleValues failed - %s\n", nt_errstr(status));
488 talloc_free(r.in.buffer);
490 } while (W_ERROR_EQUAL(r.out.result, WERR_MORE_DATA));
492 if (!W_ERROR_IS_OK(r.out.result)) {
493 printf("QueryMultipleValues failed - %s\n", win_errstr(r.out.result));
500 static BOOL test_QueryValue(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, struct policy_handle *handle, const char *valuename)
502 struct winreg_QueryValue r;
505 uint32_t offered = 0xfff;
507 printf("Testing QueryValue\n");
509 r.in.handle = handle;
511 r.in.value_name.name = valuename;
513 r.in.size = &offered;
516 status = dcerpc_winreg_QueryValue(p, mem_ctx, &r);
517 if(NT_STATUS_IS_ERR(status)) {
518 printf("QueryValue failed - %s\n", nt_errstr(status));
522 if (!W_ERROR_IS_OK(r.out.result)) {
523 printf("QueryValue failed - %s\n", win_errstr(r.out.result));
530 static BOOL test_EnumValue(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
531 struct policy_handle *handle, int max_valnamelen, int max_valbufsize)
533 struct winreg_EnumValue r;
535 uint32_t size = max_valbufsize, zero = 0;
538 struct winreg_StringBuf name;
540 printf("testing EnumValue\n");
545 r.in.handle = handle;
555 NTSTATUS status = dcerpc_winreg_EnumValue(p, mem_ctx, &r);
556 if(NT_STATUS_IS_ERR(status)) {
557 printf("EnumValue failed - %s\n", nt_errstr(status));
561 if (W_ERROR_IS_OK(r.out.result)) {
562 ret &= test_QueryValue(p, mem_ctx, handle, r.out.name->name);
563 ret &= test_QueryMultipleValues(p, mem_ctx, handle, r.out.name->name);
567 } while (W_ERROR_IS_OK(r.out.result));
569 if(!W_ERROR_EQUAL(r.out.result, WERR_NO_MORE_ITEMS)) {
570 printf("EnumValue failed - %s\n", win_errstr(r.out.result));
577 static BOOL test_InitiateSystemShutdown(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
578 const char *msg, uint32_t timeout)
580 struct winreg_InitiateSystemShutdown r;
582 uint16_t hostname = 0x0;
584 r.in.hostname = &hostname;
585 r.in.message = talloc(mem_ctx, struct initshutdown_String);
586 init_initshutdown_String(mem_ctx, r.in.message, msg);
588 r.in.timeout = timeout;
591 status = dcerpc_winreg_InitiateSystemShutdown(p, mem_ctx, &r);
593 if (!NT_STATUS_IS_OK(status)) {
594 printf("InitiateSystemShutdown failed - %s\n", nt_errstr(status));
598 if (!W_ERROR_IS_OK(r.out.result)) {
599 printf("InitiateSystemShutdown failed - %s\n", win_errstr(r.out.result));
606 static BOOL test_InitiateSystemShutdownEx(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
607 const char *msg, uint32_t timeout)
609 struct winreg_InitiateSystemShutdownEx r;
611 uint16_t hostname = 0x0;
613 r.in.hostname = &hostname;
614 r.in.message = talloc(mem_ctx, struct initshutdown_String);
615 init_initshutdown_String(mem_ctx, r.in.message, msg);
617 r.in.timeout = timeout;
621 status = dcerpc_winreg_InitiateSystemShutdownEx(p, mem_ctx, &r);
623 if (!NT_STATUS_IS_OK(status)) {
624 printf("InitiateSystemShutdownEx failed - %s\n", nt_errstr(status));
628 if (!W_ERROR_IS_OK(r.out.result)) {
629 printf("InitiateSystemShutdownEx failed - %s\n", win_errstr(r.out.result));
636 static BOOL test_AbortSystemShutdown(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
638 struct winreg_AbortSystemShutdown r;
640 uint16_t server = 0x0;
642 r.in.server = &server;
644 status = dcerpc_winreg_AbortSystemShutdown(p, mem_ctx, &r);
646 if (!NT_STATUS_IS_OK(status)) {
647 printf("AbortSystemShutdown failed - %s\n", nt_errstr(status));
651 if (!W_ERROR_IS_OK(r.out.result)) {
652 printf("AbortSystemShutdown failed - %s\n", win_errstr(r.out.result));
659 #define MAX_DEPTH 2 /* Only go this far down the tree */
661 static BOOL test_key(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
662 struct policy_handle *handle, int depth)
664 if (depth == MAX_DEPTH)
667 if (!test_QueryInfoKey(p, mem_ctx, handle, NULL)) {
671 if (!test_NotifyChangeKeyValue(p, mem_ctx, handle)) {
674 if (!test_GetKeySecurity(p, mem_ctx, handle)) {
677 if (!test_EnumKey(p, mem_ctx, handle, depth)) {
680 if (!test_EnumValue(p, mem_ctx, handle, 0xFF, 0xFFFF)) {
684 test_CloseKey(p, mem_ctx, handle);
689 typedef NTSTATUS (*winreg_open_fn)(struct dcerpc_pipe *, TALLOC_CTX *, void *);
691 static BOOL test_Open(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
692 const char *name, winreg_open_fn open_fn)
694 struct policy_handle handle, newhandle;
695 BOOL ret = True, created = False, created2 = False, deleted = False;
696 struct winreg_OpenHKLM r;
699 printf("Testing %s\n", name);
701 r.in.system_name = 0;
702 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
703 r.out.handle = &handle;
705 status = open_fn(p, mem_ctx, &r);
706 if (!NT_STATUS_IS_OK(status)) {
710 test_Cleanup(p, mem_ctx, &handle, TEST_KEY1);
711 test_Cleanup(p, mem_ctx, &handle, TEST_KEY2);
712 test_Cleanup(p, mem_ctx, &handle, TEST_KEY_BASE);
714 if (!test_CreateKey(p, mem_ctx, &handle, TEST_KEY1, NULL)) {
715 printf("CreateKey failed - not considering a failure\n");
720 if (created && !test_FlushKey(p, mem_ctx, &handle)) {
721 printf("FlushKey failed\n");
725 if (created && !test_OpenKey(p, mem_ctx, &handle, TEST_KEY1, &newhandle)) {
726 printf("CreateKey failed (OpenKey after Create didn't work)\n");
730 if (created && !test_DeleteKey(p, mem_ctx, &handle, TEST_KEY1)) {
731 printf("DeleteKey failed\n");
737 if (created && !test_FlushKey(p, mem_ctx, &handle)) {
738 printf("FlushKey failed\n");
742 if (created && deleted &&
743 test_OpenKey(p, mem_ctx, &handle, TEST_KEY1, &newhandle)) {
744 printf("DeleteKey failed (OpenKey after Delete didn't work)\n");
748 if (!test_GetVersion(p, mem_ctx, &handle)) {
749 printf("GetVersion failed\n");
753 if (created && test_CreateKey_sd(p, mem_ctx, &handle, TEST_KEY2,
758 if (created2 && !test_GetKeySecurity(p, mem_ctx, &newhandle)) {
759 printf("GetKeySecurity failed\n");
763 if (created2 && !test_CloseKey(p, mem_ctx, &newhandle)) {
764 printf("CloseKey failed\n");
768 if (created && !test_DeleteKey(p, mem_ctx, &handle, TEST_KEY2)) {
769 printf("DeleteKey failed\n");
773 /* The HKCR hive has a very large fanout */
775 if (open_fn == (void *)dcerpc_winreg_OpenHKCR) {
776 if(!test_key(p, mem_ctx, &handle, MAX_DEPTH - 1)) {
781 if(!test_key(p, mem_ctx, &handle, 0)) {
785 test_Cleanup(p, mem_ctx, &handle, TEST_KEY_BASE);
790 BOOL torture_rpc_winreg(void)
793 struct dcerpc_pipe *p;
799 } open_fns[] = {{"OpenHKLM", (winreg_open_fn)dcerpc_winreg_OpenHKLM },
800 {"OpenHKU", (winreg_open_fn)dcerpc_winreg_OpenHKU },
801 {"OpenHKCR", (winreg_open_fn)dcerpc_winreg_OpenHKCR },
802 {"OpenHKCU", (winreg_open_fn)dcerpc_winreg_OpenHKCU }};
804 mem_ctx = talloc_init("torture_rpc_winreg");
806 status = torture_rpc_connection(mem_ctx, &p, &dcerpc_table_winreg);
808 if (!NT_STATUS_IS_OK(status)) {
809 talloc_free(mem_ctx);
813 if (!lp_parm_bool(-1, "torture", "dangerous", False)) {
814 printf("winreg_InitiateShutdown disabled - enable dangerous tests to use\n");
816 ret &= test_InitiateSystemShutdown(p, mem_ctx, "spottyfood", 30);
817 ret &= test_AbortSystemShutdown(p, mem_ctx);
818 ret &= test_InitiateSystemShutdownEx(p, mem_ctx, "spottyfood", 30);
819 ret &= test_AbortSystemShutdown(p, mem_ctx);
822 for (i = 0; i < ARRAY_SIZE(open_fns); i++) {
823 if (!test_Open(p, mem_ctx, open_fns[i].name, open_fns[i].fn))
827 talloc_free(mem_ctx);