String termination fix.
[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_EnumKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
164                          struct policy_handle *handle)
165 {
166         NTSTATUS status;
167         struct winreg_EnumKey r;
168         struct winreg_EnumKeyNameRequest keyname;
169         struct winreg_String classname;
170         struct winreg_Time tm;
171
172         printf("\ntesting EnumKey\n");
173
174         r.in.handle = handle;
175         r.in.key_index = 0;
176         r.in.key_name_len = r.out.key_name_len = 0;
177         r.in.unknown = r.out.unknown = 0x0414;
178         keyname.unknown = 0x0000020a;
179         init_winreg_String(&keyname.key_name, NULL);
180         init_winreg_String(&classname, NULL);
181         r.in.in_name = &keyname;
182         r.in.class = &classname;
183         tm.low = tm.high = 0x7fffffff;
184         r.in.last_changed_time = &tm;
185
186         do {
187                 status = dcerpc_winreg_EnumKey(p, mem_ctx, &r);
188                 r.in.key_index++;
189         } while (W_ERROR_IS_OK(r.out.result));
190
191         return True;
192 }
193
194 static BOOL test_EnumValue(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
195                            struct policy_handle *handle)
196 {
197         NTSTATUS status;
198         struct winreg_QueryInfoKey qik;
199         struct winreg_EnumValue r;
200         struct winreg_String name;
201         uint32 type;
202         uint32 value1, value2;
203
204         printf("\ntesting EnumValue\n");
205
206         qik.in.handle = handle;
207         init_winreg_String(&qik.in.class, NULL);
208
209         status = dcerpc_winreg_QueryInfoKey(p, mem_ctx, &qik);
210
211         if (!W_ERROR_IS_OK(r.out.result)) {
212                 printf("QueryInfoKey failed - %s\n", win_errstr(r.out.result));
213                 return False;
214         }
215
216         r.in.handle = handle;
217         r.in.val_index = 0;
218         init_winreg_String(&name, "");
219         r.in.name = &name;
220         type = 0;
221         r.in.type = r.out.type = &type;
222         r.in.value = NULL;
223         value1 = 0;
224         value2 = 0;
225         r.in.value1 = &value1;
226         r.in.value2 = &value2;
227
228         do {
229                 status = dcerpc_winreg_EnumValue(p, mem_ctx, &r);
230                 r.in.val_index++;
231         } while (W_ERROR_IS_OK(r.out.result));
232
233         return True;
234 }
235
236 static BOOL test_OpenHKLM(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
237                           struct policy_handle *handle)
238 {
239         NTSTATUS status;
240         struct winreg_OpenHKLM r;
241         struct winreg_OpenUnknown unknown;
242         BOOL ret = True;
243
244         printf("\ntesting OpenHKLM\n");
245
246         unknown.unknown0 = 0x84e0;
247         unknown.unknown1 = 0x0000;
248         r.in.unknown = &unknown;
249         r.in.access_required = SEC_RIGHTS_MAXIMUM_ALLOWED;
250         r.out.handle = handle;
251
252         status = dcerpc_winreg_OpenHKLM(p, mem_ctx, &r);
253
254         if (!NT_STATUS_IS_OK(status)) {
255                 printf("OpenHKLM failed - %s\n", nt_errstr(status));
256                 return False;
257         }
258
259         return ret;
260 }
261
262 static BOOL test_OpenHKU(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
263                          struct policy_handle *handle)
264 {
265         NTSTATUS status;
266         struct winreg_OpenHKU r;
267         struct winreg_OpenUnknown unknown;
268         BOOL ret = True;
269
270         printf("\ntesting OpenHKU\n");
271
272         unknown.unknown0 = 0x84e0;
273         unknown.unknown1 = 0x0000;
274         r.in.unknown = &unknown;
275         r.in.access_required = SEC_RIGHTS_MAXIMUM_ALLOWED;
276         r.out.handle = handle;
277
278         status = dcerpc_winreg_OpenHKU(p, mem_ctx, &r);
279
280         if (!NT_STATUS_IS_OK(status)) {
281                 printf("OpenHKU failed - %s\n", nt_errstr(status));
282                 return False;
283         }
284
285         return ret;
286 }
287
288 static BOOL test_OpenHKCR(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
289                           struct policy_handle *handle)
290 {
291         NTSTATUS status;
292         struct winreg_OpenHKCR r;
293         struct winreg_OpenUnknown unknown;
294         BOOL ret = True;
295
296         printf("\ntesting OpenHKCR\n");
297
298         unknown.unknown0 = 0x84e0;
299         unknown.unknown1 = 0x0000;
300         r.in.unknown = &unknown;
301         r.in.access_required = SEC_RIGHTS_MAXIMUM_ALLOWED;
302         r.out.handle = handle;
303
304         status = dcerpc_winreg_OpenHKCR(p, mem_ctx, &r);
305
306         if (!NT_STATUS_IS_OK(status)) {
307                 printf("OpenHKCR failed - %s\n", nt_errstr(status));
308                 return False;
309         }
310
311         return ret;
312 }
313
314 static BOOL test_OpenHKCU(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
315                           struct policy_handle *handle)
316 {
317         NTSTATUS status;
318         struct winreg_OpenHKCU r;
319         struct winreg_OpenUnknown unknown;
320         BOOL ret = True;
321
322         printf("\ntesting OpenHKCU\n");
323
324         unknown.unknown0 = 0x84e0;
325         unknown.unknown1 = 0x0000;
326         r.in.unknown = &unknown;
327         r.in.access_required = SEC_RIGHTS_MAXIMUM_ALLOWED;
328         r.out.handle = handle;
329
330         status = dcerpc_winreg_OpenHKCU(p, mem_ctx, &r);
331
332         if (!NT_STATUS_IS_OK(status)) {
333                 printf("OpenHKCU failed - %s\n", nt_errstr(status));
334                 return False;
335         }
336
337         return ret;
338 }
339
340 typedef BOOL winreg_open_fn(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
341                             struct policy_handle *handle);
342
343 BOOL test_Open(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, void *fn)
344 {
345         struct policy_handle handle;
346         winreg_open_fn *open_fn = (winreg_open_fn *)fn;
347         BOOL ret = True;
348         struct winreg_EnumKey r;
349         struct winreg_EnumKeyNameRequest keyname;
350         struct winreg_String classname;
351         struct winreg_Time tm;
352         NTSTATUS status;
353
354         if (!open_fn(p, mem_ctx, &handle))
355                 return False;
356
357 #if 0
358         if (!test_GetVersion(p, mem_ctx, &handle)) {
359                 ret = False;
360         }
361                     
362         if (!test_DeleteKey(p, mem_ctx, &handle, "spottyfoot")) {
363                 ret = False;
364         }
365 #endif
366
367         /* Enumerate keys */
368
369         r.in.handle = &handle;
370         r.in.key_index = 0;
371         r.in.key_name_len = r.out.key_name_len = 0;
372         r.in.unknown = r.out.unknown = 0x0414;
373         keyname.unknown = 0x0000020a;
374         init_winreg_String(&keyname.key_name, NULL);
375         init_winreg_String(&classname, NULL);
376         r.in.in_name = &keyname;
377         r.in.class = &classname;
378         tm.low = tm.high = 0x7fffffff;
379         r.in.last_changed_time = &tm;
380
381         do {
382                 status = dcerpc_winreg_EnumKey(p, mem_ctx, &r);
383
384                 if (W_ERROR_IS_OK(r.out.result)) {
385                         struct policy_handle key_handle;
386
387                         if (!test_OpenKey(
388                                     p, mem_ctx, &handle, r.out.out_name->name,
389                                     &key_handle)) {
390                                 printf("OpenKey(%s) failed - %s\n",
391                                        r.out.out_name->name, 
392                                        win_errstr(r.out.result));
393                                 goto next_key;
394                         }
395
396                         if (!test_QueryInfoKey(p, mem_ctx, &handle, NULL)) {
397
398                         }
399                 }
400         next_key:
401                 r.in.key_index++;
402
403         } while (W_ERROR_IS_OK(r.out.result));
404
405 #if 0               
406         if (!test_EnumValue(p, mem_ctx, &handle)) {
407                 ret = False;
408         }
409 #endif
410
411         if (!test_CloseKey(p, mem_ctx, &handle)) {
412                 ret = False;
413         }
414
415         return ret;
416 }
417
418 BOOL torture_rpc_winreg(int dummy)
419 {
420         NTSTATUS status;
421         struct dcerpc_pipe *p;
422         TALLOC_CTX *mem_ctx;
423         BOOL ret = True;
424         winreg_open_fn *open_fns[] = { test_OpenHKLM };
425         int i;
426
427         mem_ctx = talloc_init("torture_rpc_winreg");
428
429         status = torture_rpc_connection(&p, 
430                                         DCERPC_WINREG_NAME, 
431                                         DCERPC_WINREG_UUID, 
432                                         DCERPC_WINREG_VERSION);
433
434         if (!NT_STATUS_IS_OK(status)) {
435                 return False;
436         }
437         
438         p->flags |= DCERPC_DEBUG_PRINT_BOTH;
439
440         for (i = 0; i < ARRAY_SIZE(open_fns); i++) {
441                 if (!test_Open(p, mem_ctx, open_fns[i]))
442                         ret = False;
443         }
444
445         talloc_destroy(mem_ctx);
446
447         torture_rpc_close(p);
448
449         return ret;
450 }