r4035: more effort on consistent naming of the access mask bits.
[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 static void init_winreg_String(struct winreg_String *name, const char *s)
27 {
28         name->name = s;
29         if (s) {
30                 name->name_len = 2 * (strlen_m(s) + 1);
31                 name->name_size = name->name_len;
32         } else {
33                 name->name_len = 0;
34                 name->name_size = 0;
35         }
36 }
37
38 static BOOL test_GetVersion(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
39                             struct policy_handle *handle)
40 {
41         NTSTATUS status;
42         struct winreg_GetVersion r;
43
44         printf("\ntesting GetVersion\n");
45
46         r.in.handle = handle;
47
48         status = dcerpc_winreg_GetVersion(p, mem_ctx, &r);
49
50         if (!NT_STATUS_IS_OK(status)) {
51                 printf("GetVersion failed - %s\n", nt_errstr(status));
52                 return False;
53         }
54
55         if (!W_ERROR_IS_OK(r.out.result)) {
56                 printf("GetVersion failed - %s\n", win_errstr(r.out.result));
57                 return False;
58         }
59
60         return True;
61 }
62
63 static BOOL test_CreateKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
64                           struct policy_handle *handle, const char *name, 
65                            const char *class)
66 {
67         struct winreg_CreateKey r;
68         struct policy_handle newhandle;
69         NTSTATUS status;
70         uint32_t sec_info = 0;
71
72         printf("\ntesting CreateKey\n");
73
74         r.in.handle = handle;
75         r.out.handle = &newhandle;
76         init_winreg_String(&r.in.key, name);    
77         init_winreg_String(&r.in.class, class);
78         r.in.reserved = 0x0;
79         r.in.access_mask = 0x02000000;
80         r.in.sec_info = &sec_info;
81         r.in.sec_desc = NULL;
82
83         status = dcerpc_winreg_CreateKey(p, mem_ctx, &r);
84
85         if (!NT_STATUS_IS_OK(status)) {
86                 printf("CreateKey failed - %s\n", nt_errstr(status));
87                 return False;
88         }
89
90         if (!W_ERROR_IS_OK(r.out.result)) {
91                 printf("CreateKey failed - %s\n", win_errstr(r.out.result));
92                 return False;
93         }
94
95         return True;
96 }
97
98 static BOOL test_CloseKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
99                           struct policy_handle *handle)
100 {
101         NTSTATUS status;
102         struct winreg_CloseKey r;
103
104         printf("\ntesting CloseKey\n");
105
106         r.in.handle = r.out.handle = handle;
107
108         status = dcerpc_winreg_CloseKey(p, mem_ctx, &r);
109
110         if (!NT_STATUS_IS_OK(status)) {
111                 printf("CloseKey failed - %s\n", nt_errstr(status));
112                 return False;
113         }
114
115         if (!W_ERROR_IS_OK(r.out.result)) {
116                 printf("CloseKey failed - %s\n", win_errstr(r.out.result));
117                 return False;
118         }
119
120         return True;
121 }
122
123 static BOOL test_FlushKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
124                           struct policy_handle *handle)
125 {
126         NTSTATUS status;
127         struct winreg_FlushKey r;
128
129         printf("\ntesting FlushKey\n");
130
131         r.in.handle = handle;
132
133         status = dcerpc_winreg_FlushKey(p, mem_ctx, &r);
134
135         if (!NT_STATUS_IS_OK(status)) {
136                 printf("FlushKey failed - %s\n", nt_errstr(status));
137                 return False;
138         }
139
140         if (!W_ERROR_IS_OK(r.out.result)) {
141                 printf("FlushKey failed - %s\n", win_errstr(r.out.result));
142                 return False;
143         }
144
145         return True;
146 }
147
148 static BOOL test_OpenKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
149                          struct policy_handle *hive_handle,
150                          const char *keyname, struct policy_handle *key_handle)
151 {
152         NTSTATUS status;
153         struct winreg_OpenKey r;
154
155         printf("\ntesting OpenKey\n");
156
157         r.in.handle = hive_handle;
158         init_winreg_String(&r.in.keyname, keyname);
159         r.in.unknown = 0x00000000;
160         r.in.access_mask = 0x02000000;
161         r.out.handle = key_handle;
162
163         status = dcerpc_winreg_OpenKey(p, mem_ctx, &r);
164
165         if (!NT_STATUS_IS_OK(status)) {
166                 printf("OpenKey failed - %s\n", nt_errstr(status));
167                 return False;
168         }
169
170         if (!W_ERROR_IS_OK(r.out.result)) {
171                 return False;
172         }
173
174         return True;
175 }
176
177 static BOOL test_DeleteKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
178                            struct policy_handle *handle, const char *key)
179 {
180         NTSTATUS status;
181         struct winreg_DeleteKey r;
182
183         printf("\ntesting DeleteKey\n");
184
185         r.in.handle = handle;
186         init_winreg_String(&r.in.key, key);     
187
188         status = dcerpc_winreg_DeleteKey(p, mem_ctx, &r);
189
190         if (!NT_STATUS_IS_OK(status)) {
191                 printf("DeleteKey failed - %s\n", nt_errstr(status));
192                 return False;
193         }
194
195         if (!W_ERROR_IS_OK(r.out.result)) {
196                 printf("DeleteKey failed - %s\n", win_errstr(r.out.result));
197                 return False;
198         }
199
200         return True;
201 }
202
203 static BOOL test_QueryInfoKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
204                               struct policy_handle *handle, char *class)
205 {
206         NTSTATUS status;
207         struct winreg_QueryInfoKey r;
208
209         printf("\ntesting QueryInfoKey\n");
210
211         r.in.handle = handle;
212         init_winreg_String(&r.in.class, class);
213         
214         status = dcerpc_winreg_QueryInfoKey(p, mem_ctx, &r);
215
216         if (!NT_STATUS_IS_OK(status)) {
217                 printf("QueryInfoKey failed - %s\n", nt_errstr(status));
218                 return False;
219         }
220
221         if (!W_ERROR_IS_OK(r.out.result)) {
222                 printf("QueryInfoKey failed - %s\n", win_errstr(r.out.result));
223                 return False;
224         }
225
226         return True;
227 }
228
229 static BOOL test_key(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
230                      struct policy_handle *handle, int depth);
231
232 static BOOL test_EnumKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
233                          struct policy_handle *handle, int depth)
234 {
235         struct winreg_EnumKey r;
236         struct winreg_EnumKeyNameRequest keyname;
237         struct winreg_String classname;
238         struct winreg_Time tm;
239         NTSTATUS status;
240
241         printf("Testing EnumKey\n\n");
242
243         r.in.handle = handle;
244         r.in.enum_index = 0;
245         r.in.key_name_len = r.out.key_name_len = 0;
246         r.in.unknown = r.out.unknown = 0x0414;
247         keyname.unknown = 0x0000020a;
248         init_winreg_String(&keyname.key_name, NULL);
249         init_winreg_String(&classname, NULL);
250         r.in.in_name = &keyname;
251         r.in.class = &classname;
252         tm.low = tm.high = 0x7fffffff;
253         r.in.last_changed_time = &tm;
254
255         do {
256                 status = dcerpc_winreg_EnumKey(p, mem_ctx, &r);
257
258                 if (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(r.out.result)) {
259                         struct policy_handle key_handle;
260
261                         printf("EnumKey: %d: %s\n", r.in.enum_index, r.out.out_name->name);
262
263                         if (!test_OpenKey(
264                                     p, mem_ctx, handle, r.out.out_name->name,
265                                     &key_handle)) {
266                                 printf("OpenKey(%s) failed - %s\n",
267                                        r.out.out_name->name, 
268                                        win_errstr(r.out.result));
269                         } else {
270                                 test_key(p, mem_ctx, &key_handle, depth + 1);
271                         }
272                 }
273
274                 r.in.enum_index++;
275
276         } while (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(r.out.result));
277
278         if (!NT_STATUS_IS_OK(status)) {
279                 printf("EnumKey failed - %s\n", nt_errstr(status));
280                 return False;
281         }
282
283         if (!W_ERROR_IS_OK(r.out.result) && !W_ERROR_EQUAL(r.out.result, WERR_NO_MORE_ITEMS)) {
284                 printf("EnumKey failed - %s\n", win_errstr(r.out.result));
285                 return False;
286         }
287
288
289
290         return True;
291 }
292
293 static BOOL test_EnumValue(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
294                            struct policy_handle *handle, int max_valnamelen, int max_valbufsize)
295 {
296         struct winreg_EnumValue r;
297         struct EnumValueIn buf_name;
298         struct EnumValueIn buf_val;
299         uint32 type;
300         uint32 len1 = max_valbufsize, len2 = 0;
301
302         printf("testing EnumValue\n");
303
304         r.in.handle = handle;
305         r.in.enum_index = 0;
306         r.in.name_in.len = 0;
307         r.in.name_in.max_len = max_valnamelen * 2;
308         buf_name.max_len = max_valnamelen;
309         buf_name.offset = 0;
310         buf_name.len = 0;
311         r.in.name_in.buffer = &buf_name;
312         r.in.type = &type;
313         buf_val.max_len = max_valbufsize;
314         buf_val.offset = 0;
315         buf_val.len = 0;
316         r.in.value_in = &buf_val;
317         r.in.value_len1 = &len1;
318         r.in.value_len2 = &len2;
319         
320         do {
321                 NTSTATUS status = dcerpc_winreg_EnumValue(p, mem_ctx, &r);
322                 if(NT_STATUS_IS_ERR(status)) {
323                         printf("EnumValue failed - %s\n", nt_errstr(status));
324                         return False;
325                 }
326
327                 r.in.enum_index++;
328         } while (W_ERROR_IS_OK(r.out.result));
329
330         if(!W_ERROR_EQUAL(r.out.result, WERR_NO_MORE_ITEMS)) {
331                 printf("EnumValue failed - %s\n", win_errstr(r.out.result));
332                 return False;
333         }
334
335         return True;
336 }
337
338 static BOOL test_OpenHKLM(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
339                           struct policy_handle *handle)
340 {
341         NTSTATUS status;
342         struct winreg_OpenHKLM r;
343         struct winreg_OpenUnknown unknown;
344         BOOL ret = True;
345
346         printf("\ntesting OpenHKLM\n");
347
348         unknown.unknown0 = 0x84e0;
349         unknown.unknown1 = 0x0000;
350         r.in.unknown = &unknown;
351         r.in.access_required = SEC_FLAG_MAXIMUM_ALLOWED;
352         r.out.handle = handle;
353
354         status = dcerpc_winreg_OpenHKLM(p, mem_ctx, &r);
355
356         if (!NT_STATUS_IS_OK(status)) {
357                 printf("OpenHKLM failed - %s\n", nt_errstr(status));
358                 return False;
359         }
360
361         if (!W_ERROR_IS_OK(r.out.result)) {
362                 printf("OpenHKLM failed - %s\n", win_errstr(r.out.result));
363                 return False;
364         }
365
366         return ret;
367 }
368
369 static BOOL test_OpenHKU(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
370                          struct policy_handle *handle)
371 {
372         NTSTATUS status;
373         struct winreg_OpenHKU r;
374         struct winreg_OpenUnknown unknown;
375         BOOL ret = True;
376
377         printf("\ntesting OpenHKU\n");
378
379         unknown.unknown0 = 0x84e0;
380         unknown.unknown1 = 0x0000;
381         r.in.unknown = &unknown;
382         r.in.access_required = SEC_FLAG_MAXIMUM_ALLOWED;
383         r.out.handle = handle;
384
385         status = dcerpc_winreg_OpenHKU(p, mem_ctx, &r);
386
387         if (!NT_STATUS_IS_OK(status)) {
388                 printf("OpenHKU failed - %s\n", nt_errstr(status));
389                 return False;
390         }
391
392         return ret;
393 }
394
395 static BOOL test_OpenHKCR(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
396                           struct policy_handle *handle)
397 {
398         NTSTATUS status;
399         struct winreg_OpenHKCR r;
400         struct winreg_OpenUnknown unknown;
401         BOOL ret = True;
402
403         printf("\ntesting OpenHKCR\n");
404
405         unknown.unknown0 = 0x84e0;
406         unknown.unknown1 = 0x0000;
407         r.in.unknown = &unknown;
408         r.in.access_required = SEC_FLAG_MAXIMUM_ALLOWED;
409         r.out.handle = handle;
410
411         status = dcerpc_winreg_OpenHKCR(p, mem_ctx, &r);
412
413         if (!NT_STATUS_IS_OK(status)) {
414                 printf("OpenHKCR failed - %s\n", nt_errstr(status));
415                 return False;
416         }
417
418         return ret;
419 }
420
421 static BOOL test_InitiateSystemShutdown(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
422                         const char *msg, uint32_t timeout)
423 {
424         struct winreg_InitiateSystemShutdown r;
425         NTSTATUS status;
426         
427         init_winreg_String(&r.in.message, msg);
428         r.in.flags = 0;
429         r.in.timeout = timeout;
430
431         status = dcerpc_winreg_InitiateSystemShutdown(p, mem_ctx, &r);
432
433         if (!NT_STATUS_IS_OK(status)) {
434                 printf("InitiateSystemShutdown failed - %s\n", nt_errstr(status));
435                 return False;
436         }
437
438         if (!W_ERROR_IS_OK(r.out.result)) {
439                 printf("InitiateSystemShutdown failed - %s\n", win_errstr(r.out.result));
440                 return False;
441         }
442
443         return True;
444 }
445
446 static BOOL test_AbortSystemShutdown(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx)
447 {
448         struct winreg_AbortSystemShutdown r;
449         NTSTATUS status;
450         uint16_t server = 0x0;
451
452         r.in.server = &server;
453         
454         status = dcerpc_winreg_AbortSystemShutdown(p, mem_ctx, &r);
455
456         if (!NT_STATUS_IS_OK(status)) {
457                 printf("AbortSystemShutdown failed - %s\n", nt_errstr(status));
458                 return False;
459         }
460
461         if (!W_ERROR_IS_OK(r.out.result)) {
462                 printf("AbortSystemShutdown failed - %s\n", win_errstr(r.out.result));
463                 return False;
464         }
465
466         return True;
467 }
468
469 static BOOL test_OpenHKCU(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
470                           struct policy_handle *handle)
471 {
472         NTSTATUS status;
473         struct winreg_OpenHKCU r;
474         struct winreg_OpenUnknown unknown;
475         BOOL ret = True;
476
477         printf("\ntesting OpenHKCU\n");
478
479         unknown.unknown0 = 0x84e0;
480         unknown.unknown1 = 0x0000;
481         r.in.unknown = &unknown;
482         r.in.access_required = SEC_FLAG_MAXIMUM_ALLOWED;
483         r.out.handle = handle;
484
485         status = dcerpc_winreg_OpenHKCU(p, mem_ctx, &r);
486
487         if (!NT_STATUS_IS_OK(status)) {
488                 printf("OpenHKCU failed - %s\n", nt_errstr(status));
489                 return False;
490         }
491
492         return ret;
493 }
494
495 #define MAX_DEPTH 2             /* Only go this far down the tree */
496
497 static BOOL test_key(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
498                      struct policy_handle *handle, int depth)
499 {
500         if (depth == MAX_DEPTH)
501                 return True;
502
503         if (!test_QueryInfoKey(p, mem_ctx, handle, NULL)) {
504         }
505
506         if (!test_EnumKey(p, mem_ctx, handle, depth)) {
507         }
508
509         if (!test_EnumValue(p, mem_ctx, handle, 0xFF, 0xFFFF)) {
510         }
511
512         test_CloseKey(p, mem_ctx, handle);
513
514         return True;
515 }
516
517 typedef BOOL winreg_open_fn(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
518                             struct policy_handle *handle);
519
520 static BOOL test_Open(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, void *fn)
521 {
522         struct policy_handle handle, newhandle;
523         BOOL ret = True;
524         winreg_open_fn *open_fn = (winreg_open_fn *)fn;
525
526         if (!open_fn(p, mem_ctx, &handle)) {
527                 return False;
528         }
529
530         if (!test_CreateKey(p, mem_ctx, &handle, "spottyfoot", NULL)) {
531                 printf("CreateKey failed\n");
532                 ret = False;
533         }
534
535         if (!test_FlushKey(p, mem_ctx, &handle)) {
536                 printf("FlushKey failed\n");
537                 ret = False;
538         }
539
540         if (!test_OpenKey(p, mem_ctx, &handle, "spottyfoot", &newhandle)) {
541                 printf("CreateKey failed (OpenKey after Create didn't work)\n");
542                 ret = False;
543         }
544
545         if (!test_DeleteKey(p, mem_ctx, &handle, "spottyfoot")) {
546                 printf("DeleteKey failed\n");
547                 ret = False;
548         }
549
550         if (!test_FlushKey(p, mem_ctx, &handle)) {
551                 printf("FlushKey failed\n");
552                 ret = False;
553         }
554
555         if (test_OpenKey(p, mem_ctx, &handle, "spottyfoot", &newhandle)) {
556                 printf("DeleteKey failed (OpenKey after Delete didn't work)\n");
557                 ret = False;
558         }
559
560         if (!test_GetVersion(p, mem_ctx, &handle)) {
561                 printf("GetVersion failed\n");
562                 ret = False;
563         }
564
565         /* The HKCR hive has a very large fanout */
566
567         if (open_fn == test_OpenHKCR) {
568                 if(!test_key(p, mem_ctx, &handle, MAX_DEPTH - 1)) {
569                         ret = False;
570                 }
571         }
572
573         if(!test_key(p, mem_ctx, &handle, 0)) {
574                 ret = False;
575         }
576
577         return ret;
578 }
579
580 BOOL torture_rpc_winreg(void)
581 {
582         NTSTATUS status;
583        struct dcerpc_pipe *p;
584         TALLOC_CTX *mem_ctx;
585         BOOL ret = True;
586         winreg_open_fn *open_fns[] = { test_OpenHKLM, test_OpenHKU,
587                                        test_OpenHKCR, test_OpenHKCU };
588         int i;
589
590         mem_ctx = talloc_init("torture_rpc_winreg");
591
592         status = torture_rpc_connection(&p, 
593                                         DCERPC_WINREG_NAME, 
594                                         DCERPC_WINREG_UUID, 
595                                         DCERPC_WINREG_VERSION);
596
597         if (!NT_STATUS_IS_OK(status)) {
598                 return False;
599         }
600
601         if(!test_InitiateSystemShutdown(p, mem_ctx, "spottyfood", 30))
602                 ret = False;
603
604         if(!test_AbortSystemShutdown(p, mem_ctx))
605                 ret = False;
606
607         for (i = 0; i < ARRAY_SIZE(open_fns); i++) {
608                 if (!test_Open(p, mem_ctx, open_fns[i]))
609                         ret = False;
610         }
611
612         talloc_destroy(mem_ctx);
613
614         torture_rpc_close(p);
615
616         return ret;
617 }