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