a fairly major upgrade to the dcerpc system
[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         name->name_len = 2*strlen_m(s);
28         name->name_size = name->name_len;
29 }
30
31 static BOOL test_GetVersion(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
32                             struct policy_handle *handle)
33 {
34         NTSTATUS status;
35         struct winreg_GetVersion r;
36
37         printf("\ntesting GetVersion\n");
38
39         r.in.handle = handle;
40
41         status = dcerpc_winreg_GetVersion(p, mem_ctx, &r);
42
43         if (!NT_STATUS_IS_OK(status)) {
44                 printf("GetVersion failed - %s\n", nt_errstr(status));
45                 return False;
46         }
47
48         return True;
49 }
50
51 static BOOL test_CloseKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
52                           struct policy_handle *handle)
53 {
54         NTSTATUS status;
55         struct winreg_CloseKey r;
56
57         printf("\ntesting CloseKey\n");
58
59         r.in.handle = r.out.handle = handle;
60
61         status = dcerpc_winreg_CloseKey(p, mem_ctx, &r);
62
63         if (!NT_STATUS_IS_OK(status)) {
64                 printf("CloseKey failed - %s\n", nt_errstr(status));
65                 return False;
66         }
67
68         return True;
69 }
70
71 static BOOL test_FlushKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
72                           struct policy_handle *handle)
73 {
74         NTSTATUS status;
75         struct winreg_FlushKey r;
76
77         printf("\ntesting FlushKey\n");
78
79         r.in.handle = handle;
80
81         status = dcerpc_winreg_FlushKey(p, mem_ctx, &r);
82
83         if (!NT_STATUS_IS_OK(status)) {
84                 printf("FlushKey failed - %s\n", nt_errstr(status));
85                 return False;
86         }
87
88         return True;
89 }
90
91 static BOOL test_OpenKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
92                          struct policy_handle *hive_handle,
93                          char *keyname, struct policy_handle *key_handle)
94 {
95         NTSTATUS status;
96         struct winreg_OpenKey r;
97
98         printf("\ntesting OpenKey\n");
99
100         r.in.handle = hive_handle;
101         init_winreg_String(&r.in.keyname, keyname);
102         r.in.unknown = 0x00000000;
103         r.in.access_mask = 0x02000000;
104         r.out.handle = key_handle;
105
106         status = dcerpc_winreg_OpenKey(p, mem_ctx, &r);
107
108         if (!W_ERROR_IS_OK(r.out.result)) {
109                 printf("OpenKey failed - %s\n", win_errstr(r.out.result));
110                 return False;
111         }
112
113         return True;
114 }
115
116 static BOOL test_DeleteKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
117                            struct policy_handle *handle, char *key)
118 {
119         NTSTATUS status;
120         struct winreg_DeleteKey r;
121
122         printf("\ntesting DeleteKey\n");
123
124         r.in.handle = handle;
125         init_winreg_String(&r.in.key, key);     
126
127         status = dcerpc_winreg_DeleteKey(p, mem_ctx, &r);
128
129         if (!NT_STATUS_IS_OK(status)) {
130                 printf("DeleteKey failed - %s\n", nt_errstr(status));
131                 return False;
132         }
133
134         return True;
135 }
136
137 static BOOL test_QueryInfoKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
138                               struct policy_handle *handle, char *class)
139 {
140         NTSTATUS status;
141         struct winreg_QueryInfoKey r;
142
143         printf("\ntesting QueryInfoKey\n");
144
145         r.in.handle = handle;
146         init_winreg_String(&r.in.class, class);
147         
148         status = dcerpc_winreg_QueryInfoKey(p, mem_ctx, &r);
149
150         if (!W_ERROR_IS_OK(r.out.result)) {
151                 printf("QueryInfoKey failed - %s\n", win_errstr(r.out.result));
152                 return False;
153         }
154
155         return True;
156 }
157
158 static BOOL test_EnumKey(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
159                          struct policy_handle *handle)
160 {
161         NTSTATUS status;
162         struct winreg_EnumKey r;
163         struct winreg_EnumKeyNameRequest keyname;
164         struct winreg_String classname;
165         struct winreg_Time tm;
166
167         printf("\ntesting EnumKey\n");
168
169         r.in.handle = handle;
170         r.in.key_index = 0;
171         r.in.key_name_len = r.out.key_name_len = 0;
172         r.in.unknown = r.out.unknown = 0x0414;
173         keyname.unknown = 0x0000020a;
174         init_winreg_String(&keyname.key_name, NULL);
175         init_winreg_String(&classname, NULL);
176         r.in.name = &keyname;
177         r.in.class = &classname;
178         tm.low = tm.high = 0x7fffffff;
179         r.in.last_changed_time = &tm;
180
181         do {
182                 status = dcerpc_winreg_EnumKey(p, mem_ctx, &r);
183                 r.in.key_index++;
184         } while (W_ERROR_IS_OK(r.out.result));
185
186         return True;
187 }
188
189 static BOOL test_EnumValue(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
190                            struct policy_handle *handle)
191 {
192         NTSTATUS status;
193         struct winreg_QueryInfoKey qik;
194         struct winreg_EnumValue r;
195         struct winreg_String name;
196         uint32 type;
197         uint32 value1, value2;
198
199
200         printf("\ntesting EnumValue\n");
201
202         qik.in.handle = handle;
203         init_winreg_String(&qik.in.class, NULL);
204
205         status = dcerpc_winreg_QueryInfoKey(p, mem_ctx, &qik);
206
207         if (!W_ERROR_IS_OK(r.out.result)) {
208                 printf("QueryInfoKey failed - %s\n", win_errstr(r.out.result));
209                 return False;
210         }
211
212         r.in.handle = handle;
213         r.in.val_index = 0;
214         init_winreg_String(&name, "");
215         r.in.name = &name;
216         type = 0;
217         r.in.type = r.out.type = &type;
218         r.in.value = NULL;
219         value1 = 0;
220         value2 = 0;
221         r.in.value1 = &value1;
222         r.in.value2 = &value2;
223
224         do {
225                 status = dcerpc_winreg_EnumValue(p, mem_ctx, &r);
226                 r.in.val_index++;
227         } while (W_ERROR_IS_OK(r.out.result));
228
229         return True;
230 }
231
232 static BOOL test_OpenHKLM(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
233                           struct policy_handle *handle)
234 {
235         NTSTATUS status;
236         struct winreg_OpenHKLM r;
237         struct winreg_OpenUnknown unknown;
238         BOOL ret = True;
239
240         printf("\ntesting OpenHKLM\n");
241
242         unknown.unknown0 = 0x84e0;
243         unknown.unknown1 = 0x0000;
244         r.in.unknown = &unknown;
245         r.in.access_required = SEC_RIGHTS_MAXIMUM_ALLOWED;
246         r.out.handle = handle;
247
248         status = dcerpc_winreg_OpenHKLM(p, mem_ctx, &r);
249
250         if (!NT_STATUS_IS_OK(status)) {
251                 printf("OpenHKLM failed - %s\n", nt_errstr(status));
252                 return False;
253         }
254
255         return ret;
256 }
257
258 static BOOL test_OpenHKU(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
259                          struct policy_handle *handle)
260 {
261         NTSTATUS status;
262         struct winreg_OpenHKU r;
263         struct winreg_OpenUnknown unknown;
264         BOOL ret = True;
265
266         printf("\ntesting OpenHKU\n");
267
268         unknown.unknown0 = 0x84e0;
269         unknown.unknown1 = 0x0000;
270         r.in.unknown = &unknown;
271         r.in.access_required = SEC_RIGHTS_MAXIMUM_ALLOWED;
272         r.out.handle = handle;
273
274         status = dcerpc_winreg_OpenHKU(p, mem_ctx, &r);
275
276         if (!NT_STATUS_IS_OK(status)) {
277                 printf("OpenHKU failed - %s\n", nt_errstr(status));
278                 return False;
279         }
280
281         return ret;
282 }
283
284 static BOOL test_OpenHKCR(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
285                           struct policy_handle *handle)
286 {
287         NTSTATUS status;
288         struct winreg_OpenHKCR r;
289         struct winreg_OpenUnknown unknown;
290         BOOL ret = True;
291
292         printf("\ntesting OpenHKCR\n");
293
294         unknown.unknown0 = 0x84e0;
295         unknown.unknown1 = 0x0000;
296         r.in.unknown = &unknown;
297         r.in.access_required = SEC_RIGHTS_MAXIMUM_ALLOWED;
298         r.out.handle = handle;
299
300         status = dcerpc_winreg_OpenHKCR(p, mem_ctx, &r);
301
302         if (!NT_STATUS_IS_OK(status)) {
303                 printf("OpenHKCR failed - %s\n", nt_errstr(status));
304                 return False;
305         }
306
307         return ret;
308 }
309
310 static BOOL test_OpenHKCU(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
311                           struct policy_handle *handle)
312 {
313         NTSTATUS status;
314         struct winreg_OpenHKCU r;
315         struct winreg_OpenUnknown unknown;
316         BOOL ret = True;
317
318         printf("\ntesting OpenHKCU\n");
319
320         unknown.unknown0 = 0x84e0;
321         unknown.unknown1 = 0x0000;
322         r.in.unknown = &unknown;
323         r.in.access_required = SEC_RIGHTS_MAXIMUM_ALLOWED;
324         r.out.handle = handle;
325
326         status = dcerpc_winreg_OpenHKCU(p, mem_ctx, &r);
327
328         if (!NT_STATUS_IS_OK(status)) {
329                 printf("OpenHKCU failed - %s\n", nt_errstr(status));
330                 return False;
331         }
332
333         return ret;
334 }
335
336 typedef BOOL (*winreg_open_fn)(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
337                                struct policy_handle *handle);
338
339 BOOL torture_rpc_winreg(int dummy)
340 {
341         NTSTATUS status;
342         struct dcerpc_pipe *p;
343         TALLOC_CTX *mem_ctx;
344         BOOL ret = True;
345         winreg_open_fn open_fns[] = { test_OpenHKLM };
346         int i;
347
348         mem_ctx = talloc_init("torture_rpc_winreg");
349
350         status = torture_rpc_connection(&p, 
351                                         DCERPC_WINREG_NAME, 
352                                         DCERPC_WINREG_UUID, 
353                                         DCERPC_WINREG_VERSION);
354         if (!NT_STATUS_IS_OK(status)) {
355                 return False;
356         }
357         
358         p->flags |= DCERPC_DEBUG_PRINT_BOTH;
359
360         for (i = 0; i < ARRAY_SIZE(open_fns); i++) {
361                 struct policy_handle handle;
362
363                 if (!open_fns[i](p, mem_ctx, &handle))
364                         ret = False;
365
366 #if 0
367                     if (!test_GetVersion(p, mem_ctx, &handle)) {
368                             ret = False;
369                     }
370                     
371                     if (!test_DeleteKey(p, mem_ctx, &handle, "spottyfoot")) {
372                             ret = False;
373                     }
374 #endif      
375                     if (!test_EnumKey(p, mem_ctx, &handle)) {
376                             ret = False;
377                     }
378                     
379                     if (!test_EnumValue(p, mem_ctx, &handle)) {
380                             ret = False;
381                     }
382
383                     if (!test_CloseKey(p, mem_ctx, &handle)) {
384                             ret = False;
385                     }
386         }
387
388         talloc_destroy(mem_ctx);
389
390         torture_rpc_close(p);
391
392         return ret;
393 }