r23792: convert Samba4 to GPLv3
[kai/samba.git] / source4 / torture / rpc / winreg.c
1 /* 
2    Unix SMB/CIFS implementation.
3    test suite for winreg rpc operations
4
5    Copyright (C) Tim Potter 2003
6    Copyright (C) Jelmer Vernooij 2004
7    
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.
12    
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.
17    
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/>.
20 */
21
22 #include "includes.h"
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"
28
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)"
32
33 static void init_initshutdown_String(TALLOC_CTX *mem_ctx, struct initshutdown_String *name, const char *s)
34 {
35         name->name = talloc(mem_ctx, struct initshutdown_String_sub);
36         name->name->name = s;
37 }
38
39 static void init_winreg_String(struct winreg_String *name, const char *s)
40 {
41         name->name = s;
42         if (s) {
43                 name->name_len = 2 * (strlen_m(s) + 1);
44                 name->name_size = name->name_len;
45         } else {
46                 name->name_len = 0;
47                 name->name_size = 0;
48         }
49 }
50
51 static bool test_GetVersion(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
52                             struct policy_handle *handle)
53 {
54         NTSTATUS status;
55         struct winreg_GetVersion r;
56         uint32_t v;
57         printf("\ntesting GetVersion\n");
58
59         ZERO_STRUCT(r);
60         r.in.handle = handle;
61         r.out.version = &v;
62
63         status = dcerpc_winreg_GetVersion(p, mem_ctx, &r);
64
65         if (!NT_STATUS_IS_OK(status)) {
66                 printf("GetVersion failed - %s\n", nt_errstr(status));
67                 return false;
68         }
69
70         if (!W_ERROR_IS_OK(r.out.result)) {
71                 printf("GetVersion failed - %s\n", win_errstr(r.out.result));
72                 return false;
73         }
74
75         return true;
76 }
77
78 static bool test_NotifyChangeKeyValue(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
79                                                                           struct policy_handle *handle)
80 {
81         struct winreg_NotifyChangeKeyValue r;
82         NTSTATUS status;
83
84         printf("\ntesting NotifyChangeKeyValue\n");
85
86         r.in.handle = handle;
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);
92
93         status = dcerpc_winreg_NotifyChangeKeyValue(p, mem_ctx, &r);
94         
95         if (!NT_STATUS_IS_OK(status)) {
96                 printf("NotifyChangeKeyValue failed - %s\n", nt_errstr(status));
97                 return false;
98         }
99
100         if (!W_ERROR_IS_OK(r.out.result)) {
101                 printf("NotifyChangeKeyValue failed - %s - not considering\n", win_errstr(r.out.result));
102                 return true;
103         }
104
105         return true;
106 }
107
108 static bool test_CreateKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
109                           struct policy_handle *handle, const char *name, 
110                            const char *class)
111 {
112         struct winreg_CreateKey r;
113         struct policy_handle newhandle;
114         NTSTATUS status;
115         enum winreg_CreateAction action_taken = 0;
116
117         printf("\ntesting CreateKey\n");
118
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);
123         r.in.options = 0x0;
124         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
125         r.in.action_taken = r.out.action_taken = &action_taken;
126         r.in.secdesc = NULL;
127
128         status = dcerpc_winreg_CreateKey(p, mem_ctx, &r);
129
130         if (!NT_STATUS_IS_OK(status)) {
131                 printf("CreateKey failed - %s\n", nt_errstr(status));
132                 return false;
133         }
134
135         if (!W_ERROR_IS_OK(r.out.result)) {
136                 printf("CreateKey failed - %s\n", win_errstr(r.out.result));
137                 return false;
138         }
139
140         return true;
141 }
142
143
144 /*
145   createkey testing with a SD
146 */
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)
150 {
151         struct winreg_CreateKey r;
152         NTSTATUS status;
153         enum winreg_CreateAction action_taken = 0;
154         struct security_descriptor *sd;
155         DATA_BLOB sdblob;
156         struct winreg_SecBuf secbuf;
157
158         sd = security_descriptor_create(mem_ctx,
159                                         NULL, NULL,
160                                         SID_NT_AUTHENTICATED_USERS,
161                                         SEC_ACE_TYPE_ACCESS_ALLOWED,
162                                         SEC_GENERIC_ALL,
163                                         SEC_ACE_FLAG_OBJECT_INHERIT,
164                                         NULL);
165
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");
170                 return false;
171         }
172
173         secbuf.sd.data = sdblob.data;
174         secbuf.sd.len = sdblob.length;
175         secbuf.sd.size = sdblob.length;
176         secbuf.length = sdblob.length-10;
177         secbuf.inherit = 0;
178
179         printf("\ntesting CreateKey with sd\n");
180
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);
185         r.in.options = 0x0;
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;
189
190         status = dcerpc_winreg_CreateKey(p, mem_ctx, &r);
191
192         if (!NT_STATUS_IS_OK(status)) {
193                 printf("CreateKey with sd failed - %s\n", nt_errstr(status));
194                 return false;
195         }
196
197         if (!W_ERROR_IS_OK(r.out.result)) {
198                 printf("CreateKey with sd failed - %s\n", win_errstr(r.out.result));
199                 return false;
200         }
201
202         return true;
203 }
204
205 static bool test_GetKeySecurity(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
206                           struct policy_handle *handle)
207 {
208         NTSTATUS status;
209         struct winreg_GetKeySecurity r;
210         struct security_descriptor sd;
211         DATA_BLOB sdblob;
212
213         printf("\ntesting GetKeySecurity\n");
214
215         ZERO_STRUCT(r);
216
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;
221
222         status = dcerpc_winreg_GetKeySecurity(p, mem_ctx, &r);
223
224         if (!NT_STATUS_IS_OK(status)) {
225                 printf("GetKeySecurity failed - %s\n", nt_errstr(status));
226                 return false;
227         }
228
229         if (!W_ERROR_IS_OK(r.out.result)) {
230                 printf("GetKeySecurity failed - %s\n", win_errstr(r.out.result));
231                 return false;
232         }
233
234         sdblob.data = r.out.sd->data;
235         sdblob.length = r.out.sd->len;
236
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));
241                 return false;
242         }
243         if (p->conn->flags & DCERPC_DEBUG_PRINT_OUT) {
244                 NDR_PRINT_DEBUG(security_descriptor, &sd);
245         }
246
247         return true;
248 }
249
250 static bool test_CloseKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
251                           struct policy_handle *handle)
252 {
253         NTSTATUS status;
254         struct winreg_CloseKey r;
255
256         printf("\ntesting CloseKey\n");
257
258         r.in.handle = r.out.handle = handle;
259
260         status = dcerpc_winreg_CloseKey(p, mem_ctx, &r);
261
262         if (!NT_STATUS_IS_OK(status)) {
263                 printf("CloseKey failed - %s\n", nt_errstr(status));
264                 return false;
265         }
266
267         if (!W_ERROR_IS_OK(r.out.result)) {
268                 printf("CloseKey failed - %s\n", win_errstr(r.out.result));
269                 return false;
270         }
271
272         return true;
273 }
274
275 static bool test_FlushKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
276                           struct policy_handle *handle)
277 {
278         NTSTATUS status;
279         struct winreg_FlushKey r;
280
281         printf("\ntesting FlushKey\n");
282
283         r.in.handle = handle;
284
285         status = dcerpc_winreg_FlushKey(p, mem_ctx, &r);
286
287         if (!NT_STATUS_IS_OK(status)) {
288                 printf("FlushKey failed - %s\n", nt_errstr(status));
289                 return false;
290         }
291
292         if (!W_ERROR_IS_OK(r.out.result)) {
293                 printf("FlushKey failed - %s\n", win_errstr(r.out.result));
294                 return false;
295         }
296
297         return true;
298 }
299
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)
303 {
304         NTSTATUS status;
305         struct winreg_OpenKey r;
306
307         printf("\ntesting OpenKey\n");
308
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;
314
315         status = dcerpc_winreg_OpenKey(p, mem_ctx, &r);
316
317         if (!NT_STATUS_IS_OK(status)) {
318                 printf("OpenKey failed - %s\n", nt_errstr(status));
319                 return false;
320         }
321
322         if (!W_ERROR_IS_OK(r.out.result)) {
323                 printf("OpenKey failed - %s\n", win_errstr(r.out.result));
324
325                 return false;
326         }
327
328         return true;
329 }
330
331 static bool test_Cleanup(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
332                          struct policy_handle *handle, const char *key)
333 {
334         struct winreg_DeleteKey r;
335
336         r.in.handle = handle;
337
338         init_winreg_String(&r.in.key, key);
339         dcerpc_winreg_DeleteKey(p, mem_ctx, &r);
340
341         return true;
342 }
343
344
345 static bool test_DeleteKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
346                            struct policy_handle *handle, const char *key)
347 {
348         NTSTATUS status;
349         struct winreg_DeleteKey r;
350
351         printf("\ntesting DeleteKey\n");
352
353         r.in.handle = handle;
354         init_winreg_String(&r.in.key, key);     
355
356         status = dcerpc_winreg_DeleteKey(p, mem_ctx, &r);
357
358         if (!NT_STATUS_IS_OK(status)) {
359                 printf("DeleteKey failed - %s\n", nt_errstr(status));
360                 return false;
361         }
362
363         if (!W_ERROR_IS_OK(r.out.result)) {
364                 printf("DeleteKey failed - %s\n", win_errstr(r.out.result));
365                 return false;
366         }
367
368         return true;
369 }
370
371 static bool test_QueryInfoKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
372                               struct policy_handle *handle, char *class)
373 {
374         NTSTATUS status;
375         struct winreg_QueryInfoKey r;
376         uint32_t num_subkeys, max_subkeylen, max_subkeysize,
377                 num_values, max_valnamelen, max_valbufsize,
378                 secdescsize;
379         NTTIME last_changed_time;
380
381         printf("\ntesting QueryInfoKey\n");
382
383         ZERO_STRUCT(r);
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;
393
394         r.out.classname = talloc(mem_ctx, struct winreg_String);
395         
396         r.in.classname = talloc(mem_ctx, struct winreg_String);
397         init_winreg_String(r.in.classname, class);
398         
399         status = dcerpc_winreg_QueryInfoKey(p, mem_ctx, &r);
400
401         if (!NT_STATUS_IS_OK(status)) {
402                 printf("QueryInfoKey failed - %s\n", nt_errstr(status));
403                 return false;
404         }
405
406         if (!W_ERROR_IS_OK(r.out.result)) {
407                 printf("QueryInfoKey failed - %s\n", win_errstr(r.out.result));
408                 return false;
409         }
410
411         return true;
412 }
413
414 static bool test_key(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
415                      struct policy_handle *handle, int depth);
416
417 static bool test_EnumKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
418                          struct policy_handle *handle, int depth)
419 {
420         struct winreg_EnumKey r;
421         struct winreg_StringBuf class, name;
422         NTSTATUS status;
423         NTTIME t = 0;
424
425         printf("Testing EnumKey\n\n");
426
427         class.name   = "";
428         class.size   = 1024;
429
430         r.in.handle = handle;
431         r.in.enum_index = 0;
432         r.in.name = &name;
433         r.in.keyclass = &class;
434         r.out.name = &name;
435         r.in.last_changed_time = &t;
436
437         do {
438                 name.name   = NULL;
439                 name.size   = 1024;
440
441                 status = dcerpc_winreg_EnumKey(p, mem_ctx, &r);
442
443                 if (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(r.out.result)) {
444                         struct policy_handle key_handle;
445
446                         printf("EnumKey: %d: %s\n", r.in.enum_index, r.out.name->name);
447
448                         if (!test_OpenKey(
449                                     p, mem_ctx, handle, r.out.name->name,
450                                     &key_handle)) {
451                         } else {
452                                 test_key(p, mem_ctx, &key_handle, depth + 1);
453                         }
454                 }
455
456                 r.in.enum_index++;
457
458         } while (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(r.out.result));
459
460         if (!NT_STATUS_IS_OK(status)) {
461                 printf("EnumKey failed - %s\n", nt_errstr(status));
462                 return false;
463         }
464
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));
467                 return false;
468         }
469
470
471
472         return true;
473 }
474
475 static bool test_QueryMultipleValues(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, struct policy_handle *handle, const char *valuename)
476 {
477         struct winreg_QueryMultipleValues r;
478         NTSTATUS status;
479         uint32_t bufsize=0;
480
481         printf("Testing QueryMultipleValues\n");
482
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;
490
491         r.in.num_values = 1;
492         r.in.buffer_size = r.out.buffer_size = talloc(mem_ctx, uint32_t);
493         *r.in.buffer_size = bufsize;
494         do { 
495                 *r.in.buffer_size = bufsize;
496                 r.in.buffer = r.out.buffer = talloc_zero_array(mem_ctx, uint8_t, 
497                                                                *r.in.buffer_size);
498
499                 status = dcerpc_winreg_QueryMultipleValues(p, mem_ctx, &r);
500         
501                 if(NT_STATUS_IS_ERR(status)) {
502                         printf("QueryMultipleValues failed - %s\n", nt_errstr(status));
503                         return false;
504                 }
505                 talloc_free(r.in.buffer);
506                 bufsize += 0x20;
507         } while (W_ERROR_EQUAL(r.out.result, WERR_MORE_DATA));
508
509         if (!W_ERROR_IS_OK(r.out.result)) {
510                 printf("QueryMultipleValues failed - %s\n", win_errstr(r.out.result));
511                 return false;
512         }
513
514         return true;
515 }
516
517 static bool test_QueryValue(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, struct policy_handle *handle, const char *valuename)
518 {
519         struct winreg_QueryValue r;
520         NTSTATUS status;
521         enum winreg_Type zero_type = 0;
522         uint32_t offered = 0xfff;
523         uint32_t zero = 0;
524
525         printf("Testing QueryValue\n");
526
527         r.in.handle = handle;
528         r.in.data = NULL;
529         r.in.value_name.name = valuename;
530         r.in.type = &zero_type;
531         r.in.size = &offered;
532         r.in.length = &zero;
533
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));
537                 return false;
538         }
539
540         if (!W_ERROR_IS_OK(r.out.result)) {
541                 printf("QueryValue failed - %s\n", win_errstr(r.out.result));
542                 return false;
543         }
544
545         return true;
546 }
547
548 static bool test_EnumValue(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
549                            struct policy_handle *handle, int max_valnamelen, int max_valbufsize)
550 {
551         struct winreg_EnumValue r;
552         enum winreg_Type type = 0;
553         uint32_t size = max_valbufsize, zero = 0;
554         bool ret = true;
555         uint8_t buf8;
556         struct winreg_StringBuf name;
557
558         printf("testing EnumValue\n");
559
560         name.name   = "";
561         name.size   = 1024;
562
563         r.in.handle = handle;
564         r.in.enum_index = 0;
565         r.in.name = &name;
566         r.out.name = &name;
567         r.in.type = &type;
568         r.in.value = &buf8;
569         r.in.length = &zero;
570         r.in.size = &size;
571         
572         do {
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));
576                         return false;
577                 }
578
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);
582                 }
583
584                 r.in.enum_index++;
585         } while (W_ERROR_IS_OK(r.out.result));
586
587         if(!W_ERROR_EQUAL(r.out.result, WERR_NO_MORE_ITEMS)) {
588                 printf("EnumValue failed - %s\n", win_errstr(r.out.result));
589                 return false;
590         }
591
592         return ret;
593 }
594
595 static bool test_InitiateSystemShutdown(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
596                         const char *msg, uint32_t timeout)
597 {
598         struct winreg_InitiateSystemShutdown r;
599         NTSTATUS status;
600         uint16_t hostname = 0x0;
601         
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);
605         r.in.force_apps = 1;
606         r.in.timeout = timeout;
607         r.in.reboot = 1;
608
609         status = dcerpc_winreg_InitiateSystemShutdown(p, mem_ctx, &r);
610
611         if (!NT_STATUS_IS_OK(status)) {
612                 printf("InitiateSystemShutdown failed - %s\n", nt_errstr(status));
613                 return false;
614         }
615
616         if (!W_ERROR_IS_OK(r.out.result)) {
617                 printf("InitiateSystemShutdown failed - %s\n", win_errstr(r.out.result));
618                 return false;
619         }
620
621         return true;
622 }
623
624 static bool test_InitiateSystemShutdownEx(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
625                         const char *msg, uint32_t timeout)
626 {
627         struct winreg_InitiateSystemShutdownEx r;
628         NTSTATUS status;
629         uint16_t hostname = 0x0;
630         
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);
634         r.in.force_apps = 1;
635         r.in.timeout = timeout;
636         r.in.reboot = 1;
637         r.in.reason = 0;
638
639         status = dcerpc_winreg_InitiateSystemShutdownEx(p, mem_ctx, &r);
640
641         if (!NT_STATUS_IS_OK(status)) {
642                 printf("InitiateSystemShutdownEx failed - %s\n", nt_errstr(status));
643                 return false;
644         }
645
646         if (!W_ERROR_IS_OK(r.out.result)) {
647                 printf("InitiateSystemShutdownEx failed - %s\n", win_errstr(r.out.result));
648                 return false;
649         }
650
651         return true;
652 }
653
654 static bool test_AbortSystemShutdown(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
655 {
656         struct winreg_AbortSystemShutdown r;
657         NTSTATUS status;
658         uint16_t server = 0x0;
659
660         r.in.server = &server;
661         
662         status = dcerpc_winreg_AbortSystemShutdown(p, mem_ctx, &r);
663
664         if (!NT_STATUS_IS_OK(status)) {
665                 printf("AbortSystemShutdown failed - %s\n", nt_errstr(status));
666                 return false;
667         }
668
669         if (!W_ERROR_IS_OK(r.out.result)) {
670                 printf("AbortSystemShutdown failed - %s\n", win_errstr(r.out.result));
671                 return false;
672         }
673
674         return true;
675 }
676
677 #define MAX_DEPTH 2             /* Only go this far down the tree */
678
679 static bool test_key(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
680                      struct policy_handle *handle, int depth)
681 {
682         if (depth == MAX_DEPTH)
683                 return true;
684
685         if (!test_QueryInfoKey(p, mem_ctx, handle, NULL)) {
686         }
687
688         if (!test_NotifyChangeKeyValue(p, mem_ctx, handle)) {
689         }
690         
691         if (!test_GetKeySecurity(p, mem_ctx, handle)) {
692         }
693
694         if (!test_EnumKey(p, mem_ctx, handle, depth)) {
695         }
696
697         if (!test_EnumValue(p, mem_ctx, handle, 0xFF, 0xFFFF)) {
698         }
699
700         test_CloseKey(p, mem_ctx, handle);
701
702         return true;
703 }
704
705 typedef NTSTATUS (*winreg_open_fn)(struct dcerpc_pipe *, TALLOC_CTX *, void *);
706
707 static bool test_Open(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
708                       const char *name, winreg_open_fn open_fn)
709 {
710         struct policy_handle handle, newhandle;
711         bool ret = true, created = false, created2 = false, deleted = false;
712         struct winreg_OpenHKLM r;
713         NTSTATUS status;
714
715         printf("Testing %s\n", name);
716
717         r.in.system_name = 0;
718         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
719         r.out.handle = &handle;
720         
721         status = open_fn(p, mem_ctx, &r);
722         if (!NT_STATUS_IS_OK(status)) {
723                 return false;
724         }
725
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);
729
730         if (!test_CreateKey(p, mem_ctx, &handle, TEST_KEY1, NULL)) {
731                 printf("CreateKey failed - not considering a failure\n");
732         } else {
733                 created = true;
734         }
735
736         if (created && !test_FlushKey(p, mem_ctx, &handle)) {
737                 printf("FlushKey failed\n");
738                 ret = false;
739         }
740
741         if (created && !test_OpenKey(p, mem_ctx, &handle, TEST_KEY1, &newhandle)) {
742                 printf("CreateKey failed (OpenKey after Create didn't work)\n");
743                 ret = false;
744         }
745
746         if (created && !test_DeleteKey(p, mem_ctx, &handle, TEST_KEY1)) {
747                 printf("DeleteKey failed\n");
748                 ret = false;
749         } else {
750                 deleted = true;
751         }
752
753         if (created && !test_FlushKey(p, mem_ctx, &handle)) {
754                 printf("FlushKey failed\n");
755                 ret = false;
756         }
757
758         if (created && deleted && 
759             test_OpenKey(p, mem_ctx, &handle, TEST_KEY1, &newhandle)) {
760                 printf("DeleteKey failed (OpenKey after Delete worked)\n");
761                 ret = false;
762         }
763
764         if (!test_GetVersion(p, mem_ctx, &handle)) {
765                 printf("GetVersion failed\n");
766                 ret = false;
767         }
768
769         if (created && test_CreateKey_sd(p, mem_ctx, &handle, TEST_KEY2, 
770                                           NULL, &newhandle)) {
771                 created2 = true;
772         }
773
774         if (created2 && !test_GetKeySecurity(p, mem_ctx, &newhandle)) {
775                 printf("GetKeySecurity failed\n");
776                 ret = false;
777         }
778
779         if (created2 && !test_CloseKey(p, mem_ctx, &newhandle)) {
780                 printf("CloseKey failed\n");
781                 ret = false;
782         }
783
784         if (created && !test_DeleteKey(p, mem_ctx, &handle, TEST_KEY2)) {
785                 printf("DeleteKey failed\n");
786                 ret = false;
787         }
788
789         /* The HKCR hive has a very large fanout */
790
791         if (open_fn == (void *)dcerpc_winreg_OpenHKCR) {
792                 if(!test_key(p, mem_ctx, &handle, MAX_DEPTH - 1)) {
793                         ret = false;
794                 }
795         }
796
797         if(!test_key(p, mem_ctx, &handle, 0)) {
798                 ret = false;
799         }
800
801         test_Cleanup(p, mem_ctx, &handle, TEST_KEY_BASE);
802
803         return ret;
804 }
805
806 bool torture_rpc_winreg(struct torture_context *torture)
807 {
808         NTSTATUS status;
809         struct dcerpc_pipe *p;
810         TALLOC_CTX *mem_ctx;
811         bool ret = true;
812         struct {
813                 const char *name;
814                 winreg_open_fn fn;
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 }};
819         int i;
820         mem_ctx = talloc_init("torture_rpc_winreg");
821
822         status = torture_rpc_connection(mem_ctx, &p, &dcerpc_table_winreg);
823
824         if (!NT_STATUS_IS_OK(status)) {
825                 talloc_free(mem_ctx);
826                 return false;
827         }
828
829         if (!torture_setting_bool(torture, "dangerous", false)) {
830                 printf("winreg_InitiateShutdown disabled - enable dangerous tests to use\n");
831         } else {
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);
836         }
837
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);
840         }
841
842         talloc_free(mem_ctx);
843
844         return ret;
845 }