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