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