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