r5037: got rid of all of the TALLOC_DEPRECATED stuff. My apologies for the
[bbaumbach/samba-autobuild/.git] / source4 / rpc_server / winreg / rpc_winreg.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    endpoint server for the winreg pipe
5
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 "rpc_server/dcerpc_server.h"
25 #include "registry.h"
26 #include "librpc/gen_ndr/ndr_winreg.h"
27 #include "rpc_server/common/common.h"
28
29 enum handle_types { HTYPE_REGVAL, HTYPE_REGKEY };
30
31 static NTSTATUS dcerpc_winreg_bind(struct dcesrv_call_state *dce_call, const struct dcesrv_interface *iface)
32 {
33         struct registry_context *ctx;
34         reg_open_local(&ctx);
35
36         dce_call->context->private = ctx;
37
38         return NT_STATUS_OK;
39 }
40
41 #define DCESRV_INTERFACE_WINREG_BIND dcerpc_winreg_bind
42
43 static WERROR winreg_openhive (struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, uint32_t hkey, struct policy_handle **outh)
44 {
45         struct registry_context *ctx = dce_call->context->private;
46         struct dcesrv_handle *h; 
47         WERROR error;
48
49         h = dcesrv_handle_new(dce_call->context, HTYPE_REGKEY); 
50
51         error = reg_get_predefined_key(ctx, hkey, (struct registry_key **)&h->data);
52         if (!W_ERROR_IS_OK(error)) {
53                 return error;
54         }
55         
56         *outh = &h->wire_handle; 
57
58         return error; 
59 }
60
61 #define func_winreg_OpenHive(k,n) static WERROR winreg_Open ## k (struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct winreg_Open ## k *r) \
62 { \
63         return winreg_openhive (dce_call, mem_ctx, n, &r->out.handle);\
64 }
65
66 func_winreg_OpenHive(HKCR,HKEY_CLASSES_ROOT)
67 func_winreg_OpenHive(HKCU,HKEY_CURRENT_USER)
68 func_winreg_OpenHive(HKLM,HKEY_LOCAL_MACHINE)
69 func_winreg_OpenHive(HKPD,HKEY_PERFORMANCE_DATA)
70 func_winreg_OpenHive(HKU,HKEY_USERS)
71 func_winreg_OpenHive(HKCC,HKEY_CURRENT_CONFIG)
72 func_winreg_OpenHive(HKDD,HKEY_DYN_DATA)
73 func_winreg_OpenHive(HKPT,HKEY_PERFORMANCE_TEXT)
74 func_winreg_OpenHive(HKPN,HKEY_PERFORMANCE_NLSTEXT)
75
76 /* 
77   winreg_CloseKey 
78 */
79 static WERROR winreg_CloseKey(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
80                               struct winreg_CloseKey *r)
81 {
82         struct dcesrv_handle *h; 
83
84         DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
85
86         talloc_free(h);
87
88         return WERR_OK;
89 }
90
91
92 /* 
93   winreg_CreateKey 
94 */
95 static WERROR winreg_CreateKey(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
96                                struct winreg_CreateKey *r)
97 {
98         struct dcesrv_handle *h, *newh;
99         WERROR error;
100
101         DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
102         
103         newh = dcesrv_handle_new(dce_call->context, HTYPE_REGKEY);
104
105         error = reg_key_add_name(newh, (struct registry_key *)h->data, r->in.key.name, 
106                                  r->in.access_mask, 
107                                  r->in.sec_desc?r->in.sec_desc->sd:NULL, 
108                                  (struct registry_key **)&newh->data);
109         if (W_ERROR_IS_OK(error)) {
110                 r->out.handle = &newh->wire_handle;
111         } else {
112                 talloc_free(newh);
113         }
114
115         return error;
116 }
117
118
119 /* 
120   winreg_DeleteKey 
121 */
122 static WERROR winreg_DeleteKey(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
123                        struct winreg_DeleteKey *r)
124 {
125         struct dcesrv_handle *h;
126         WERROR result;
127
128         DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
129
130         if (W_ERROR_IS_OK(result)) {
131                 return reg_key_del((struct registry_key *)h->data, r->in.key.name);
132         }
133
134         return result;
135 }
136
137
138 /* 
139   winreg_DeleteValue 
140 */
141 static WERROR winreg_DeleteValue(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
142                        struct winreg_DeleteValue *r)
143 {
144         struct dcesrv_handle *h;
145         struct registry_key *key;
146
147         DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
148
149         key = h->data;
150         
151         return reg_del_value(key, r->in.value.name);
152 }
153
154
155 /* 
156   winreg_EnumKey 
157 */
158 static WERROR winreg_EnumKey(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
159                        struct winreg_EnumKey *r)
160 {
161         struct dcesrv_handle *h;
162         struct registry_key *key;
163
164         DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
165
166         r->out.result = reg_key_get_subkey_by_index(mem_ctx, (struct registry_key *)h->data, r->in.enum_index, &key);
167
168         if (W_ERROR_IS_OK(r->out.result)) {
169                 r->out.key_name_len = strlen(key->name);
170                 r->out.out_name = talloc_zero(mem_ctx, struct winreg_EnumKeyNameResponse);
171                 r->out.out_name->name = key->name;
172                 r->out.class = talloc_zero(mem_ctx, struct winreg_String);
173                 r->out.last_changed_time = talloc_zero(mem_ctx, struct winreg_Time);
174         }
175         
176         return r->out.result;
177 }
178
179
180 /* 
181   winreg_EnumValue 
182 */
183 static WERROR winreg_EnumValue(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
184                                struct winreg_EnumValue *r)
185 {
186         struct dcesrv_handle *h;
187         struct registry_key *key;
188         struct registry_value *value;
189         WERROR result;
190
191         DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
192
193         key = h->data;
194
195         result = reg_key_get_value_by_index(mem_ctx, key, r->in.enum_index, &value);
196         if (!W_ERROR_IS_OK(result)) {
197                 return result;
198         }
199         
200         r->out.type = talloc(mem_ctx, uint32_t);
201         *r->out.type = value->data_type;
202         r->out.name_out.name = value->name;
203         r->out.value = value->data_blk;
204         r->out.size = talloc(mem_ctx, uint32_t);
205         r->out.length = r->out.size;
206         *r->out.size = value->data_len;
207         
208         return WERR_OK;
209 }
210
211
212 /* 
213   winreg_FlushKey 
214 */
215 static WERROR winreg_FlushKey(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
216                        struct winreg_FlushKey *r)
217 {
218         struct dcesrv_handle *h;
219
220         DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
221
222         return reg_key_flush(h->data);
223 }
224
225
226 /* 
227   winreg_GetKeySecurity 
228 */
229 static WERROR winreg_GetKeySecurity(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
230                        struct winreg_GetKeySecurity *r)
231 {
232         struct dcesrv_handle *h;
233
234         DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
235
236         return WERR_NOT_SUPPORTED;
237 }
238
239
240 /* 
241   winreg_LoadKey 
242 */
243 static WERROR winreg_LoadKey(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
244                        struct winreg_LoadKey *r)
245 {
246         return WERR_NOT_SUPPORTED;
247 }
248
249
250 /* 
251   winreg_NotifyChangeKeyValue 
252 */
253 static WERROR winreg_NotifyChangeKeyValue(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
254                        struct winreg_NotifyChangeKeyValue *r)
255 {
256         return WERR_NOT_SUPPORTED;
257 }
258
259
260 /* 
261   winreg_OpenKey 
262 */
263 static WERROR winreg_OpenKey(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
264                        struct winreg_OpenKey *r)
265 {
266         struct dcesrv_handle *h, *newh;
267         WERROR result;
268
269         DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
270
271         newh = dcesrv_handle_new(dce_call->context, HTYPE_REGKEY);
272
273         result = reg_open_key(newh, (struct registry_key *)h->data, 
274                               r->in.keyname.name, (struct registry_key **)&newh->data);
275
276         if (W_ERROR_IS_OK(result)) {
277                 r->out.handle = &newh->wire_handle; 
278         } else {
279                 talloc_free(newh);
280         }
281         
282         return result;
283 }
284
285
286 /* 
287   winreg_QueryInfoKey 
288 */
289 static WERROR winreg_QueryInfoKey(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
290                        struct winreg_QueryInfoKey *r)
291 {
292         struct dcesrv_handle *h;
293         struct registry_key *k;
294         WERROR ret;
295
296         DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
297
298         k = h->data;
299
300         ret = reg_key_num_subkeys(k, &r->out.num_subkeys);
301         if (!W_ERROR_IS_OK(ret)) { 
302                 return ret;
303         }
304
305         ret = reg_key_num_values(k, &r->out.num_values);
306         if (!W_ERROR_IS_OK(ret)) { 
307                 return ret;
308         }
309
310         ret = reg_key_subkeysizes(k, &r->out.max_subkeysize, &r->out.max_subkeylen);
311         if (!W_ERROR_IS_OK(ret)) { 
312                 return ret;
313         }
314
315         ret = reg_key_valuesizes(k, &r->out.max_valnamelen, &r->out.max_valbufsize);
316         if (!W_ERROR_IS_OK(ret)) { 
317                 return ret;
318         }
319
320         r->out.secdescsize = 0; /* FIXME */
321         ZERO_STRUCT(r->out.last_changed_time); /* FIXME */
322         if (!W_ERROR_IS_OK(ret)) { 
323                 return ret;
324         }
325
326
327         return WERR_OK;
328 }
329
330
331 /* 
332   winreg_QueryValue 
333 */
334 static WERROR winreg_QueryValue(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
335                        struct winreg_QueryValue *r)
336 {
337         struct dcesrv_handle *h;
338         struct registry_key *key;
339         struct registry_value *val;
340         WERROR result;
341
342         DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
343
344         key = h->data;
345         
346         result = reg_key_get_value_by_name(mem_ctx, key, r->in.value_name.name, &val);
347
348         if (!W_ERROR_IS_OK(result)) { 
349                 return result;
350         }
351
352         /* Just asking for the size of the buffer */
353         r->out.type = &val->data_type;
354         r->out.length = &val->data_len;
355         if (!r->in.data) {
356                 r->out.size = talloc(mem_ctx, uint32);
357                 *r->out.size = val->data_len;
358         } else {
359                 r->out.size = r->in.size;
360                 r->out.data = val->data_blk;
361         }
362
363         return WERR_OK;
364 }
365
366
367 /* 
368   winreg_ReplaceKey 
369 */
370 static WERROR winreg_ReplaceKey(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
371                        struct winreg_ReplaceKey *r)
372 {
373         return WERR_NOT_SUPPORTED;
374 }
375
376
377 /* 
378   winreg_RestoreKey 
379 */
380 static WERROR winreg_RestoreKey(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
381                        struct winreg_RestoreKey *r)
382 {
383         return WERR_NOT_SUPPORTED;
384 }
385
386
387 /* 
388   winreg_SaveKey 
389 */
390 static WERROR winreg_SaveKey(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
391                        struct winreg_SaveKey *r)
392 {
393         return WERR_NOT_SUPPORTED;
394 }
395
396
397 /* 
398   winreg_SetKeySecurity 
399 */
400 static WERROR winreg_SetKeySecurity(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
401                        struct winreg_SetKeySecurity *r)
402 {
403         return WERR_NOT_SUPPORTED;
404 }
405
406
407 /* 
408   winreg_SetValue 
409 */
410 static WERROR winreg_SetValue(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
411                        struct winreg_SetValue *r)
412 {
413         struct dcesrv_handle *h;
414         struct registry_key *key;
415         WERROR result;
416
417         DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
418
419         key = h->data;
420         
421         result = reg_val_set(key, r->in.name.name, r->in.type, r->in.data, r->in.size);
422
423         if (!W_ERROR_IS_OK(result)) { 
424                 return result;
425         }
426
427         return WERR_OK;
428 }
429
430
431 /* 
432   winreg_UnLoadKey 
433 */
434 static WERROR winreg_UnLoadKey(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
435                        struct winreg_UnLoadKey *r)
436 {
437         return WERR_NOT_SUPPORTED;
438 }
439
440
441 /* 
442   winreg_InitiateSystemShutdown 
443 */
444 static WERROR winreg_InitiateSystemShutdown(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
445                        struct winreg_InitiateSystemShutdown *r)
446 {
447         return WERR_NOT_SUPPORTED;
448 }
449
450
451 /* 
452   winreg_AbortSystemShutdown 
453 */
454 static WERROR winreg_AbortSystemShutdown(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
455                        struct winreg_AbortSystemShutdown *r)
456 {
457         return WERR_NOT_SUPPORTED;
458 }
459
460
461 /* 
462   winreg_GetVersion 
463 */
464 static WERROR winreg_GetVersion(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
465                        struct winreg_GetVersion *r)
466 {
467         r->out.version = 5;
468         return WERR_OK;
469 }
470
471
472 /* 
473   winreg_QueryMultipleValues 
474 */
475 static WERROR winreg_QueryMultipleValues(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
476                        struct winreg_QueryMultipleValues *r)
477 {
478         return WERR_NOT_SUPPORTED;
479 }
480
481
482 /* 
483   winreg_InitiateSystemShutdownEx 
484 */
485 static WERROR winreg_InitiateSystemShutdownEx(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
486                        struct winreg_InitiateSystemShutdownEx *r)
487 {
488         return WERR_NOT_SUPPORTED;
489 }
490
491
492 /* 
493   winreg_SaveKeyEx 
494 */
495 static WERROR winreg_SaveKeyEx(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
496                        struct winreg_SaveKeyEx *r)
497 {
498         return WERR_NOT_SUPPORTED;
499 }
500
501
502 /* 
503   winreg_QueryMultipleValues2 
504 */
505 static WERROR winreg_QueryMultipleValues2(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
506                        struct winreg_QueryMultipleValues2 *r)
507 {
508         return WERR_NOT_SUPPORTED;
509 }
510
511
512 /* include the generated boilerplate */
513 #include "librpc/gen_ndr/ndr_winreg_s.c"