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