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