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