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 3 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, see <http://www.gnu.org/licenses/>.
23 #include "torture/torture.h"
24 #include "librpc/gen_ndr/ndr_winreg_c.h"
25 #include "librpc/gen_ndr/ndr_security.h"
26 #include "libcli/security/security.h"
27 #include "torture/rpc/rpc.h"
29 #define TEST_KEY_BASE "smbtorture test"
30 #define TEST_KEY1 TEST_KEY_BASE "\\spottyfoot"
31 #define TEST_KEY2 TEST_KEY_BASE "\\with a SD (#1)"
33 static void init_initshutdown_String(TALLOC_CTX *mem_ctx, struct initshutdown_String *name, const char *s)
35 name->name = talloc(mem_ctx, struct initshutdown_String_sub);
39 static void init_winreg_String(struct winreg_String *name, const char *s)
43 name->name_len = 2 * (strlen_m(s) + 1);
44 name->name_size = name->name_len;
51 static bool test_GetVersion(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
52 struct policy_handle *handle)
55 struct winreg_GetVersion r;
57 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 enum winreg_CreateAction 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.keyclass, 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 enum winreg_CreateAction 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.keyclass, 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.parent_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;
376 uint32_t num_subkeys, max_subkeylen, max_subkeysize,
377 num_values, max_valnamelen, max_valbufsize,
379 NTTIME last_changed_time;
381 printf("\ntesting QueryInfoKey\n");
384 r.in.handle = handle;
385 r.out.num_subkeys = &num_subkeys;
386 r.out.max_subkeylen = &max_subkeylen;
387 r.out.max_subkeysize = &max_subkeysize;
388 r.out.num_values = &num_values;
389 r.out.max_valnamelen = &max_valnamelen;
390 r.out.max_valbufsize = &max_valbufsize;
391 r.out.secdescsize = &secdescsize;
392 r.out.last_changed_time = &last_changed_time;
394 r.out.classname = talloc(mem_ctx, struct winreg_String);
396 r.in.classname = talloc(mem_ctx, struct winreg_String);
397 init_winreg_String(r.in.classname, class);
399 status = dcerpc_winreg_QueryInfoKey(p, mem_ctx, &r);
401 if (!NT_STATUS_IS_OK(status)) {
402 printf("QueryInfoKey failed - %s\n", nt_errstr(status));
406 if (!W_ERROR_IS_OK(r.out.result)) {
407 printf("QueryInfoKey failed - %s\n", win_errstr(r.out.result));
414 static bool test_key(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
415 struct policy_handle *handle, int depth);
417 static bool test_EnumKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
418 struct policy_handle *handle, int depth)
420 struct winreg_EnumKey r;
421 struct winreg_StringBuf class, name;
425 printf("Testing EnumKey\n\n");
430 r.in.handle = handle;
433 r.in.keyclass = &class;
435 r.in.last_changed_time = &t;
441 status = dcerpc_winreg_EnumKey(p, mem_ctx, &r);
443 if (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(r.out.result)) {
444 struct policy_handle key_handle;
446 printf("EnumKey: %d: %s\n", r.in.enum_index, r.out.name->name);
449 p, mem_ctx, handle, r.out.name->name,
452 test_key(p, mem_ctx, &key_handle, depth + 1);
458 } while (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(r.out.result));
460 if (!NT_STATUS_IS_OK(status)) {
461 printf("EnumKey failed - %s\n", nt_errstr(status));
465 if (!W_ERROR_IS_OK(r.out.result) && !W_ERROR_EQUAL(r.out.result, WERR_NO_MORE_ITEMS)) {
466 printf("EnumKey failed - %s\n", win_errstr(r.out.result));
475 static bool test_QueryMultipleValues(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, struct policy_handle *handle, const char *valuename)
477 struct winreg_QueryMultipleValues r;
481 printf("Testing QueryMultipleValues\n");
483 r.in.key_handle = handle;
484 r.in.values = r.out.values = talloc_array(mem_ctx, struct QueryMultipleValue, 1);
485 r.in.values[0].name = talloc(mem_ctx, struct winreg_String);
486 r.in.values[0].name->name = valuename;
487 r.in.values[0].offset = 0;
488 r.in.values[0].length = 0;
489 r.in.values[0].type = 0;
492 r.in.buffer_size = r.out.buffer_size = talloc(mem_ctx, uint32_t);
493 *r.in.buffer_size = bufsize;
495 *r.in.buffer_size = bufsize;
496 r.in.buffer = r.out.buffer = talloc_zero_array(mem_ctx, uint8_t,
499 status = dcerpc_winreg_QueryMultipleValues(p, mem_ctx, &r);
501 if(NT_STATUS_IS_ERR(status)) {
502 printf("QueryMultipleValues failed - %s\n", nt_errstr(status));
505 talloc_free(r.in.buffer);
507 } while (W_ERROR_EQUAL(r.out.result, WERR_MORE_DATA));
509 if (!W_ERROR_IS_OK(r.out.result)) {
510 printf("QueryMultipleValues failed - %s\n", win_errstr(r.out.result));
517 static bool test_QueryValue(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, struct policy_handle *handle, const char *valuename)
519 struct winreg_QueryValue r;
521 enum winreg_Type zero_type = 0;
522 uint32_t offered = 0xfff;
525 printf("Testing QueryValue\n");
527 r.in.handle = handle;
529 r.in.value_name.name = valuename;
530 r.in.type = &zero_type;
531 r.in.size = &offered;
534 status = dcerpc_winreg_QueryValue(p, mem_ctx, &r);
535 if(NT_STATUS_IS_ERR(status)) {
536 printf("QueryValue failed - %s\n", nt_errstr(status));
540 if (!W_ERROR_IS_OK(r.out.result)) {
541 printf("QueryValue failed - %s\n", win_errstr(r.out.result));
548 static bool test_EnumValue(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
549 struct policy_handle *handle, int max_valnamelen, int max_valbufsize)
551 struct winreg_EnumValue r;
552 enum winreg_Type type = 0;
553 uint32_t size = max_valbufsize, zero = 0;
556 struct winreg_StringBuf name;
558 printf("testing EnumValue\n");
563 r.in.handle = handle;
573 NTSTATUS status = dcerpc_winreg_EnumValue(p, mem_ctx, &r);
574 if(NT_STATUS_IS_ERR(status)) {
575 printf("EnumValue failed - %s\n", nt_errstr(status));
579 if (W_ERROR_IS_OK(r.out.result)) {
580 ret &= test_QueryValue(p, mem_ctx, handle, r.out.name->name);
581 ret &= test_QueryMultipleValues(p, mem_ctx, handle, r.out.name->name);
585 } while (W_ERROR_IS_OK(r.out.result));
587 if(!W_ERROR_EQUAL(r.out.result, WERR_NO_MORE_ITEMS)) {
588 printf("EnumValue failed - %s\n", win_errstr(r.out.result));
595 static bool test_InitiateSystemShutdown(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
596 const char *msg, uint32_t timeout)
598 struct winreg_InitiateSystemShutdown r;
600 uint16_t hostname = 0x0;
602 r.in.hostname = &hostname;
603 r.in.message = talloc(mem_ctx, struct initshutdown_String);
604 init_initshutdown_String(mem_ctx, r.in.message, msg);
606 r.in.timeout = timeout;
609 status = dcerpc_winreg_InitiateSystemShutdown(p, mem_ctx, &r);
611 if (!NT_STATUS_IS_OK(status)) {
612 printf("InitiateSystemShutdown failed - %s\n", nt_errstr(status));
616 if (!W_ERROR_IS_OK(r.out.result)) {
617 printf("InitiateSystemShutdown failed - %s\n", win_errstr(r.out.result));
624 static bool test_InitiateSystemShutdownEx(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
625 const char *msg, uint32_t timeout)
627 struct winreg_InitiateSystemShutdownEx r;
629 uint16_t hostname = 0x0;
631 r.in.hostname = &hostname;
632 r.in.message = talloc(mem_ctx, struct initshutdown_String);
633 init_initshutdown_String(mem_ctx, r.in.message, msg);
635 r.in.timeout = timeout;
639 status = dcerpc_winreg_InitiateSystemShutdownEx(p, mem_ctx, &r);
641 if (!NT_STATUS_IS_OK(status)) {
642 printf("InitiateSystemShutdownEx failed - %s\n", nt_errstr(status));
646 if (!W_ERROR_IS_OK(r.out.result)) {
647 printf("InitiateSystemShutdownEx failed - %s\n", win_errstr(r.out.result));
654 static bool test_AbortSystemShutdown(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
656 struct winreg_AbortSystemShutdown r;
658 uint16_t server = 0x0;
660 r.in.server = &server;
662 status = dcerpc_winreg_AbortSystemShutdown(p, mem_ctx, &r);
664 if (!NT_STATUS_IS_OK(status)) {
665 printf("AbortSystemShutdown failed - %s\n", nt_errstr(status));
669 if (!W_ERROR_IS_OK(r.out.result)) {
670 printf("AbortSystemShutdown failed - %s\n", win_errstr(r.out.result));
677 #define MAX_DEPTH 2 /* Only go this far down the tree */
679 static bool test_key(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
680 struct policy_handle *handle, int depth)
682 if (depth == MAX_DEPTH)
685 if (!test_QueryInfoKey(p, mem_ctx, handle, NULL)) {
688 if (!test_NotifyChangeKeyValue(p, mem_ctx, handle)) {
691 if (!test_GetKeySecurity(p, mem_ctx, handle)) {
694 if (!test_EnumKey(p, mem_ctx, handle, depth)) {
697 if (!test_EnumValue(p, mem_ctx, handle, 0xFF, 0xFFFF)) {
700 test_CloseKey(p, mem_ctx, handle);
705 typedef NTSTATUS (*winreg_open_fn)(struct dcerpc_pipe *, TALLOC_CTX *, void *);
707 static bool test_Open(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
708 const char *name, winreg_open_fn open_fn)
710 struct policy_handle handle, newhandle;
711 bool ret = true, created = false, created2 = false, deleted = false;
712 struct winreg_OpenHKLM r;
715 printf("Testing %s\n", name);
717 r.in.system_name = 0;
718 r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
719 r.out.handle = &handle;
721 status = open_fn(p, mem_ctx, &r);
722 if (!NT_STATUS_IS_OK(status)) {
726 test_Cleanup(p, mem_ctx, &handle, TEST_KEY1);
727 test_Cleanup(p, mem_ctx, &handle, TEST_KEY2);
728 test_Cleanup(p, mem_ctx, &handle, TEST_KEY_BASE);
730 if (!test_CreateKey(p, mem_ctx, &handle, TEST_KEY1, NULL)) {
731 printf("CreateKey failed - not considering a failure\n");
736 if (created && !test_FlushKey(p, mem_ctx, &handle)) {
737 printf("FlushKey failed\n");
741 if (created && !test_OpenKey(p, mem_ctx, &handle, TEST_KEY1, &newhandle)) {
742 printf("CreateKey failed (OpenKey after Create didn't work)\n");
746 if (created && !test_DeleteKey(p, mem_ctx, &handle, TEST_KEY1)) {
747 printf("DeleteKey failed\n");
753 if (created && !test_FlushKey(p, mem_ctx, &handle)) {
754 printf("FlushKey failed\n");
758 if (created && deleted &&
759 test_OpenKey(p, mem_ctx, &handle, TEST_KEY1, &newhandle)) {
760 printf("DeleteKey failed (OpenKey after Delete worked)\n");
764 if (!test_GetVersion(p, mem_ctx, &handle)) {
765 printf("GetVersion failed\n");
769 if (created && test_CreateKey_sd(p, mem_ctx, &handle, TEST_KEY2,
774 if (created2 && !test_GetKeySecurity(p, mem_ctx, &newhandle)) {
775 printf("GetKeySecurity failed\n");
779 if (created2 && !test_CloseKey(p, mem_ctx, &newhandle)) {
780 printf("CloseKey failed\n");
784 if (created && !test_DeleteKey(p, mem_ctx, &handle, TEST_KEY2)) {
785 printf("DeleteKey failed\n");
789 /* The HKCR hive has a very large fanout */
791 if (open_fn == (void *)dcerpc_winreg_OpenHKCR) {
792 if(!test_key(p, mem_ctx, &handle, MAX_DEPTH - 1)) {
797 if(!test_key(p, mem_ctx, &handle, 0)) {
801 test_Cleanup(p, mem_ctx, &handle, TEST_KEY_BASE);
806 bool torture_rpc_winreg(struct torture_context *torture)
809 struct dcerpc_pipe *p;
815 } open_fns[] = {{"OpenHKLM", (winreg_open_fn)dcerpc_winreg_OpenHKLM },
816 {"OpenHKU", (winreg_open_fn)dcerpc_winreg_OpenHKU },
817 {"OpenHKCR", (winreg_open_fn)dcerpc_winreg_OpenHKCR },
818 {"OpenHKCU", (winreg_open_fn)dcerpc_winreg_OpenHKCU }};
820 mem_ctx = talloc_init("torture_rpc_winreg");
822 status = torture_rpc_connection(mem_ctx, &p, &dcerpc_table_winreg);
824 if (!NT_STATUS_IS_OK(status)) {
825 talloc_free(mem_ctx);
829 if (!torture_setting_bool(torture, "dangerous", false)) {
830 printf("winreg_InitiateShutdown disabled - enable dangerous tests to use\n");
832 ret &= test_InitiateSystemShutdown(p, mem_ctx, "spottyfood", 30);
833 ret &= test_AbortSystemShutdown(p, mem_ctx);
834 ret &= test_InitiateSystemShutdownEx(p, mem_ctx, "spottyfood", 30);
835 ret &= test_AbortSystemShutdown(p, mem_ctx);
838 for (i = 0; i < ARRAY_SIZE(open_fns); i++) {
839 ret &= test_Open(p, mem_ctx, open_fns[i].name, open_fns[i].fn);
842 talloc_free(mem_ctx);