Modify WINREG test program to recursively enumerate keys in all known
[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    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23
24 static void init_winreg_String(struct winreg_String *name, const char *s)
25 {
26         name->name = s;
27         if (s) {
28                 name->name_len = 2 * (strlen_m(s) + 1);
29                 name->name_size = name->name_len;
30         } else {
31                 name->name_len = 0;
32                 name->name_size = 0;
33         }
34 }
35
36 static BOOL test_GetVersion(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
37                             struct policy_handle *handle)
38 {
39         NTSTATUS status;
40         struct winreg_GetVersion r;
41
42         printf("\ntesting GetVersion\n");
43
44         r.in.handle = handle;
45
46         status = dcerpc_winreg_GetVersion(p, mem_ctx, &r);
47
48         if (!NT_STATUS_IS_OK(status)) {
49                 printf("GetVersion failed - %s\n", nt_errstr(status));
50                 return False;
51         }
52
53         return True;
54 }
55
56 static BOOL test_CloseKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
57                           struct policy_handle *handle)
58 {
59         NTSTATUS status;
60         struct winreg_CloseKey r;
61
62         printf("\ntesting CloseKey\n");
63
64         r.in.handle = r.out.handle = handle;
65
66         status = dcerpc_winreg_CloseKey(p, mem_ctx, &r);
67
68         if (!NT_STATUS_IS_OK(status)) {
69                 printf("CloseKey failed - %s\n", nt_errstr(status));
70                 return False;
71         }
72
73         return True;
74 }
75
76 static BOOL test_FlushKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
77                           struct policy_handle *handle)
78 {
79         NTSTATUS status;
80         struct winreg_FlushKey r;
81
82         printf("\ntesting FlushKey\n");
83
84         r.in.handle = handle;
85
86         status = dcerpc_winreg_FlushKey(p, mem_ctx, &r);
87
88         if (!NT_STATUS_IS_OK(status)) {
89                 printf("FlushKey failed - %s\n", nt_errstr(status));
90                 return False;
91         }
92
93         return True;
94 }
95
96 static BOOL test_OpenKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
97                          struct policy_handle *hive_handle,
98                          char *keyname, struct policy_handle *key_handle)
99 {
100         NTSTATUS status;
101         struct winreg_OpenKey r;
102
103         printf("\ntesting OpenKey\n");
104
105         r.in.handle = hive_handle;
106         init_winreg_String(&r.in.keyname, keyname);
107         r.in.unknown = 0x00000000;
108         r.in.access_mask = 0x02000000;
109         r.out.handle = key_handle;
110
111         status = dcerpc_winreg_OpenKey(p, mem_ctx, &r);
112
113         if (!W_ERROR_IS_OK(r.out.result)) {
114                 printf("OpenKey failed - %s\n", win_errstr(r.out.result));
115                 return False;
116         }
117
118         return True;
119 }
120
121 static BOOL test_DeleteKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
122                            struct policy_handle *handle, char *key)
123 {
124         NTSTATUS status;
125         struct winreg_DeleteKey r;
126
127         printf("\ntesting DeleteKey\n");
128
129         r.in.handle = handle;
130         init_winreg_String(&r.in.key, key);     
131
132         status = dcerpc_winreg_DeleteKey(p, mem_ctx, &r);
133
134         if (!NT_STATUS_IS_OK(status)) {
135                 printf("DeleteKey failed - %s\n", nt_errstr(status));
136                 return False;
137         }
138
139         return True;
140 }
141
142 static BOOL test_QueryInfoKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
143                               struct policy_handle *handle, char *class)
144 {
145         NTSTATUS status;
146         struct winreg_QueryInfoKey r;
147
148         printf("\ntesting QueryInfoKey\n");
149
150         r.in.handle = handle;
151         init_winreg_String(&r.in.class, class);
152         
153         status = dcerpc_winreg_QueryInfoKey(p, mem_ctx, &r);
154
155         if (!W_ERROR_IS_OK(r.out.result)) {
156                 printf("QueryInfoKey failed - %s\n", win_errstr(r.out.result));
157                 return False;
158         }
159
160         return True;
161 }
162
163 static BOOL test_EnumValue(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
164                            struct policy_handle *handle)
165 {
166         NTSTATUS status;
167         struct winreg_QueryInfoKey qik;
168         struct winreg_EnumValue r;
169         struct winreg_String name;
170         uint32 type;
171         uint32 value1, value2;
172
173         printf("\ntesting EnumValue\n");
174
175         qik.in.handle = handle;
176         init_winreg_String(&qik.in.class, NULL);
177
178         status = dcerpc_winreg_QueryInfoKey(p, mem_ctx, &qik);
179
180         if (!W_ERROR_IS_OK(r.out.result)) {
181                 printf("QueryInfoKey failed - %s\n", win_errstr(r.out.result));
182                 return False;
183         }
184
185         r.in.handle = handle;
186         r.in.val_index = 0;
187         init_winreg_String(&name, "");
188         r.in.name = &name;
189         type = 0;
190         r.in.type = r.out.type = &type;
191         r.in.value = NULL;
192         value1 = 0;
193         value2 = 0;
194         r.in.value1 = &value1;
195         r.in.value2 = &value2;
196
197         do {
198                 status = dcerpc_winreg_EnumValue(p, mem_ctx, &r);
199                 r.in.val_index++;
200         } while (W_ERROR_IS_OK(r.out.result));
201
202         return True;
203 }
204
205 static BOOL test_OpenHKLM(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
206                           struct policy_handle *handle)
207 {
208         NTSTATUS status;
209         struct winreg_OpenHKLM r;
210         struct winreg_OpenUnknown unknown;
211         BOOL ret = True;
212
213         printf("\ntesting OpenHKLM\n");
214
215         unknown.unknown0 = 0x84e0;
216         unknown.unknown1 = 0x0000;
217         r.in.unknown = &unknown;
218         r.in.access_required = SEC_RIGHTS_MAXIMUM_ALLOWED;
219         r.out.handle = handle;
220
221         status = dcerpc_winreg_OpenHKLM(p, mem_ctx, &r);
222
223         if (!NT_STATUS_IS_OK(status)) {
224                 printf("OpenHKLM failed - %s\n", nt_errstr(status));
225                 return False;
226         }
227
228         return ret;
229 }
230
231 static BOOL test_OpenHKU(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
232                          struct policy_handle *handle)
233 {
234         NTSTATUS status;
235         struct winreg_OpenHKU r;
236         struct winreg_OpenUnknown unknown;
237         BOOL ret = True;
238
239         printf("\ntesting OpenHKU\n");
240
241         unknown.unknown0 = 0x84e0;
242         unknown.unknown1 = 0x0000;
243         r.in.unknown = &unknown;
244         r.in.access_required = SEC_RIGHTS_MAXIMUM_ALLOWED;
245         r.out.handle = handle;
246
247         status = dcerpc_winreg_OpenHKU(p, mem_ctx, &r);
248
249         if (!NT_STATUS_IS_OK(status)) {
250                 printf("OpenHKU failed - %s\n", nt_errstr(status));
251                 return False;
252         }
253
254         return ret;
255 }
256
257 static BOOL test_OpenHKCR(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
258                           struct policy_handle *handle)
259 {
260         NTSTATUS status;
261         struct winreg_OpenHKCR r;
262         struct winreg_OpenUnknown unknown;
263         BOOL ret = True;
264
265         printf("\ntesting OpenHKCR\n");
266
267         unknown.unknown0 = 0x84e0;
268         unknown.unknown1 = 0x0000;
269         r.in.unknown = &unknown;
270         r.in.access_required = SEC_RIGHTS_MAXIMUM_ALLOWED;
271         r.out.handle = handle;
272
273         status = dcerpc_winreg_OpenHKCR(p, mem_ctx, &r);
274
275         if (!NT_STATUS_IS_OK(status)) {
276                 printf("OpenHKCR failed - %s\n", nt_errstr(status));
277                 return False;
278         }
279
280         return ret;
281 }
282
283 static BOOL test_OpenHKCU(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
284                           struct policy_handle *handle)
285 {
286         NTSTATUS status;
287         struct winreg_OpenHKCU r;
288         struct winreg_OpenUnknown unknown;
289         BOOL ret = True;
290
291         printf("\ntesting OpenHKCU\n");
292
293         unknown.unknown0 = 0x84e0;
294         unknown.unknown1 = 0x0000;
295         r.in.unknown = &unknown;
296         r.in.access_required = SEC_RIGHTS_MAXIMUM_ALLOWED;
297         r.out.handle = handle;
298
299         status = dcerpc_winreg_OpenHKCU(p, mem_ctx, &r);
300
301         if (!NT_STATUS_IS_OK(status)) {
302                 printf("OpenHKCU failed - %s\n", nt_errstr(status));
303                 return False;
304         }
305
306         return ret;
307 }
308
309 #define MAX_DEPTH 2             /* Only go this far down the tree */
310
311 static BOOL test_key(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
312                      struct policy_handle *handle, int depth)
313 {
314         struct winreg_EnumKey r;
315         struct winreg_EnumKeyNameRequest keyname;
316         struct winreg_String classname;
317         struct winreg_Time tm;
318         NTSTATUS status;
319
320         if (depth == MAX_DEPTH)
321                 return True;
322
323         if (!test_QueryInfoKey(p, mem_ctx, handle, NULL)) {
324                 ;
325         }
326
327         /* Enumerate keys */
328
329         r.in.handle = handle;
330         r.in.key_index = 0;
331         r.in.key_name_len = r.out.key_name_len = 0;
332         r.in.unknown = r.out.unknown = 0x0414;
333         keyname.unknown = 0x0000020a;
334         init_winreg_String(&keyname.key_name, NULL);
335         init_winreg_String(&classname, NULL);
336         r.in.in_name = &keyname;
337         r.in.class = &classname;
338         tm.low = tm.high = 0x7fffffff;
339         r.in.last_changed_time = &tm;
340
341         do {
342                 status = dcerpc_winreg_EnumKey(p, mem_ctx, &r);
343
344                 if (NT_STATUS_IS_OK(status) && W_ERROR_IS_OK(r.out.result)) {
345                         struct policy_handle key_handle;
346
347                         if (!test_OpenKey(
348                                     p, mem_ctx, handle, r.out.out_name->name,
349                                     &key_handle)) {
350                                 printf("OpenKey(%s) failed - %s\n",
351                                        r.out.out_name->name, 
352                                        win_errstr(r.out.result));
353                                 goto next_key;
354                         }
355
356                         test_key(p, mem_ctx, &key_handle, depth + 1);
357                 }
358
359         next_key:
360
361                 r.in.key_index++;
362
363         } while (W_ERROR_IS_OK(r.out.result));
364
365         test_CloseKey(p, mem_ctx, handle);
366
367         return True;
368 }
369
370 typedef BOOL winreg_open_fn(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
371                             struct policy_handle *handle);
372
373 static BOOL test_Open(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, void *fn)
374 {
375         struct policy_handle handle;
376         winreg_open_fn *open_fn = (winreg_open_fn *)fn;
377
378         if (!open_fn(p, mem_ctx, &handle))
379                 return False;
380
381         if (!test_GetVersion(p, mem_ctx, &handle)) {
382                 printf("GetVersion failed\n");
383                 return False;
384         }
385
386         if (!test_FlushKey(p, mem_ctx, &handle)) {
387                 printf("FlushKey failed\n");
388                 return False;
389         }
390
391         if (!test_DeleteKey(p, mem_ctx, &handle, "spottyfoot")) {
392                 printf("FlushKey failed\n");
393                 return False;
394         }
395
396         /* The HKCR hive has a very large fanout */
397
398         if (open_fn == test_OpenHKCR) 
399                 return test_key(p, mem_ctx, &handle, MAX_DEPTH - 1);
400
401         return test_key(p, mem_ctx, &handle, 0);
402 }
403
404 BOOL torture_rpc_winreg(int dummy)
405 {
406         NTSTATUS status;
407         struct dcerpc_pipe *p;
408         TALLOC_CTX *mem_ctx;
409         BOOL ret = True;
410         winreg_open_fn *open_fns[] = { test_OpenHKLM, test_OpenHKU,
411                                        test_OpenHKCR, test_OpenHKCU };
412         int i;
413
414         mem_ctx = talloc_init("torture_rpc_winreg");
415
416         status = torture_rpc_connection(&p, 
417                                         DCERPC_WINREG_NAME, 
418                                         DCERPC_WINREG_UUID, 
419                                         DCERPC_WINREG_VERSION);
420
421         if (!NT_STATUS_IS_OK(status)) {
422                 return False;
423         }
424         
425         p->flags |= DCERPC_DEBUG_PRINT_BOTH;
426
427         for (i = 0; i < ARRAY_SIZE(open_fns); i++) {
428                 if (!test_Open(p, mem_ctx, open_fns[i]))
429                         ret = False;
430         }
431
432         talloc_destroy(mem_ctx);
433
434         torture_rpc_close(p);
435
436         return ret;
437 }