r4213: Store REG_SZ in UTF16, not the unix charset..
[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->conn->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->conn->private;
46         struct dcesrv_handle *h; 
47         WERROR error;
48
49         h = dcesrv_handle_new(dce_call->conn, 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         h = dcesrv_handle_fetch(dce_call->conn, r->in.handle, HTYPE_REGKEY);
85         DCESRV_CHECK_HANDLE(h);
86
87         dcesrv_handle_destroy(dce_call->conn, h);
88
89         return WERR_OK;
90 }
91
92
93 /* 
94   winreg_CreateKey 
95 */
96 static WERROR winreg_CreateKey(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
97                        struct winreg_CreateKey *r)
98 {
99         struct dcesrv_handle *h, *newh;
100         WERROR error;
101
102         h = dcesrv_handle_fetch(dce_call->conn, r->in.handle, HTYPE_REGKEY);
103         DCESRV_CHECK_HANDLE(h);
104         
105         newh = dcesrv_handle_new(dce_call->conn, HTYPE_REGKEY);
106
107         error = reg_key_add_name(newh, (struct registry_key *)h->data, r->in.key.name, 
108                                                          r->in.access_mask, 
109                                                          r->in.sec_desc?r->in.sec_desc->sd:NULL, 
110                                                          (struct registry_key **)&newh->data);
111
112         if(W_ERROR_IS_OK(error)) {
113                 r->out.handle = &newh->wire_handle;
114         } else {
115                 dcesrv_handle_destroy(dce_call->conn, newh);
116         }
117
118         return error;
119 }
120
121
122 /* 
123   winreg_DeleteKey 
124 */
125 static WERROR winreg_DeleteKey(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
126                        struct winreg_DeleteKey *r)
127 {
128         struct dcesrv_handle *h;
129         WERROR result;
130
131         h = dcesrv_handle_fetch(dce_call->conn, r->in.handle, HTYPE_REGKEY);
132         DCESRV_CHECK_HANDLE(h);
133
134         if (W_ERROR_IS_OK(result)) {
135                 return reg_key_del((struct registry_key *)h->data, r->in.key.name);
136         }
137
138         return result;
139 }
140
141
142 /* 
143   winreg_DeleteValue 
144 */
145 static WERROR winreg_DeleteValue(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
146                        struct winreg_DeleteValue *r)
147 {
148         struct dcesrv_handle *h;
149         struct registry_key *key;
150
151         h = dcesrv_handle_fetch(dce_call->conn, r->in.handle, HTYPE_REGKEY);
152         DCESRV_CHECK_HANDLE(h);
153
154         key = h->data;
155         
156         return reg_del_value(key, r->in.value.name);
157 }
158
159
160 /* 
161   winreg_EnumKey 
162 */
163 static WERROR winreg_EnumKey(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
164                        struct winreg_EnumKey *r)
165 {
166         struct dcesrv_handle *h;
167         struct registry_key *key;
168
169         h = dcesrv_handle_fetch(dce_call->conn, r->in.handle, HTYPE_REGKEY);
170         DCESRV_CHECK_HANDLE(h);
171
172         r->out.result = reg_key_get_subkey_by_index(mem_ctx, (struct registry_key *)h->data, r->in.enum_index, &key);
173
174         if (W_ERROR_IS_OK(r->out.result)) {
175                 r->out.key_name_len = strlen(key->name);
176                 r->out.out_name = talloc_zero_p(mem_ctx, struct winreg_EnumKeyNameResponse);
177                 r->out.out_name->name = key->name;
178                 r->out.class = talloc_zero_p(mem_ctx, struct winreg_String);
179                 r->out.last_changed_time = talloc_zero_p(mem_ctx, struct winreg_Time);
180         }
181         
182         return r->out.result;
183 }
184
185
186 /* 
187   winreg_EnumValue 
188 */
189 static WERROR winreg_EnumValue(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
190                                struct winreg_EnumValue *r)
191 {
192         struct dcesrv_handle *h;
193         struct registry_key *key;
194         struct registry_value *value;
195         WERROR result;
196
197         h = dcesrv_handle_fetch(dce_call->conn, r->in.handle, HTYPE_REGKEY);
198         DCESRV_CHECK_HANDLE(h);
199
200         key = h->data;
201
202         result = reg_key_get_value_by_index(mem_ctx, key, r->in.enum_index, &value);
203         if (!W_ERROR_IS_OK(result)) {
204                 return result;
205         }
206         
207         r->out.type = talloc_p(mem_ctx, uint32_t);
208         *r->out.type = value->data_type;
209         r->out.name_out.name = value->name;
210         r->out.value = value->data_blk;
211         r->out.size = talloc_p(mem_ctx, uint32_t);
212         r->out.length = r->out.size;
213         *r->out.size = value->data_len;
214         
215         return WERR_OK;
216 }
217
218
219 /* 
220   winreg_FlushKey 
221 */
222 static WERROR winreg_FlushKey(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
223                        struct winreg_FlushKey *r)
224 {
225         struct dcesrv_handle *h;
226
227         h = dcesrv_handle_fetch(dce_call->conn, r->in.handle, HTYPE_REGKEY);
228         DCESRV_CHECK_HANDLE(h);
229
230         return reg_key_flush(h->data);
231 }
232
233
234 /* 
235   winreg_GetKeySecurity 
236 */
237 static WERROR winreg_GetKeySecurity(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
238                        struct winreg_GetKeySecurity *r)
239 {
240         struct dcesrv_handle *h;
241
242         h = dcesrv_handle_fetch(dce_call->conn, r->in.handle, HTYPE_REGKEY);
243         DCESRV_CHECK_HANDLE(h);
244
245         return WERR_NOT_SUPPORTED;
246 }
247
248
249 /* 
250   winreg_LoadKey 
251 */
252 static WERROR winreg_LoadKey(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
253                        struct winreg_LoadKey *r)
254 {
255         return WERR_NOT_SUPPORTED;
256 }
257
258
259 /* 
260   winreg_NotifyChangeKeyValue 
261 */
262 static WERROR winreg_NotifyChangeKeyValue(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
263                        struct winreg_NotifyChangeKeyValue *r)
264 {
265         return WERR_NOT_SUPPORTED;
266 }
267
268
269 /* 
270   winreg_OpenKey 
271 */
272 static WERROR winreg_OpenKey(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
273                        struct winreg_OpenKey *r)
274 {
275         struct dcesrv_handle *h, *newh;
276         WERROR result;
277
278         h = dcesrv_handle_fetch(dce_call->conn, r->in.handle, HTYPE_REGKEY);
279         DCESRV_CHECK_HANDLE(h);
280
281         newh = dcesrv_handle_new(dce_call->conn, HTYPE_REGKEY);
282
283         result = reg_open_key(newh, (struct registry_key *)h->data, 
284                                 r->in.keyname.name, (struct registry_key **)&newh->data);
285
286         if (W_ERROR_IS_OK(result)) {
287                 r->out.handle = &newh->wire_handle; 
288         } else {
289                 dcesrv_handle_destroy(dce_call->conn, newh);
290         }
291         
292         return result;
293 }
294
295
296 /* 
297   winreg_QueryInfoKey 
298 */
299 static WERROR winreg_QueryInfoKey(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
300                        struct winreg_QueryInfoKey *r)
301 {
302         struct dcesrv_handle *h;
303         struct registry_key *k;
304         WERROR ret;
305
306         h = dcesrv_handle_fetch(dce_call->conn, r->in.handle, HTYPE_REGKEY);
307         DCESRV_CHECK_HANDLE(h);
308         k = h->data;
309
310         ret = reg_key_num_subkeys(k, &r->out.num_subkeys);
311         if (!W_ERROR_IS_OK(ret)) { 
312                 return ret;
313         }
314
315         ret = reg_key_num_values(k, &r->out.num_values);
316         if (!W_ERROR_IS_OK(ret)) { 
317                 return ret;
318         }
319
320         ret = reg_key_subkeysizes(k, &r->out.max_subkeysize, &r->out.max_subkeylen);
321         if (!W_ERROR_IS_OK(ret)) { 
322                 return ret;
323         }
324
325         ret = reg_key_valuesizes(k, &r->out.max_valnamelen, &r->out.max_valbufsize);
326         if (!W_ERROR_IS_OK(ret)) { 
327                 return ret;
328         }
329
330         r->out.secdescsize = 0; /* FIXME */
331         ZERO_STRUCT(r->out.last_changed_time); /* FIXME */
332         if (!W_ERROR_IS_OK(ret)) { 
333                 return ret;
334         }
335
336
337         return WERR_OK;
338 }
339
340
341 /* 
342   winreg_QueryValue 
343 */
344 static WERROR winreg_QueryValue(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
345                        struct winreg_QueryValue *r)
346 {
347         struct dcesrv_handle *h;
348         struct registry_key *key;
349         struct registry_value *val;
350         WERROR result;
351
352         h = dcesrv_handle_fetch(dce_call->conn, r->in.handle, HTYPE_REGKEY);
353         DCESRV_CHECK_HANDLE(h);
354
355         key = h->data;
356         
357         result = reg_key_get_value_by_name(mem_ctx, key, r->in.value_name.name, &val);
358
359         if (!W_ERROR_IS_OK(result)) { 
360                 return result;
361         }
362
363         /* Just asking for the size of the buffer */
364         r->out.type = &val->data_type;
365         r->out.length = &val->data_len;
366         if (!r->in.data) {
367                 r->out.size = talloc_p(mem_ctx, uint32);
368                 *r->out.size = val->data_len;
369         } else {
370                 r->out.size = r->in.size;
371                 r->out.data = val->data_blk;
372         }
373
374         return WERR_OK;
375 }
376
377
378 /* 
379   winreg_ReplaceKey 
380 */
381 static WERROR winreg_ReplaceKey(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
382                        struct winreg_ReplaceKey *r)
383 {
384         return WERR_NOT_SUPPORTED;
385 }
386
387
388 /* 
389   winreg_RestoreKey 
390 */
391 static WERROR winreg_RestoreKey(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
392                        struct winreg_RestoreKey *r)
393 {
394         return WERR_NOT_SUPPORTED;
395 }
396
397
398 /* 
399   winreg_SaveKey 
400 */
401 static WERROR winreg_SaveKey(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
402                        struct winreg_SaveKey *r)
403 {
404         return WERR_NOT_SUPPORTED;
405 }
406
407
408 /* 
409   winreg_SetKeySecurity 
410 */
411 static WERROR winreg_SetKeySecurity(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
412                        struct winreg_SetKeySecurity *r)
413 {
414         return WERR_NOT_SUPPORTED;
415 }
416
417
418 /* 
419   winreg_SetValue 
420 */
421 static WERROR winreg_SetValue(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
422                        struct winreg_SetValue *r)
423 {
424         struct dcesrv_handle *h;
425         struct registry_key *key;
426         WERROR result;
427
428         h = dcesrv_handle_fetch(dce_call->conn, r->in.handle, HTYPE_REGKEY);
429         DCESRV_CHECK_HANDLE(h);
430
431         key = h->data;
432         
433         result = reg_val_set(key, r->in.name.name, r->in.type, r->in.data, r->in.size);
434
435         if (!W_ERROR_IS_OK(result)) { 
436                 return result;
437         }
438
439         return WERR_OK;
440 }
441
442
443 /* 
444   winreg_UnLoadKey 
445 */
446 static WERROR winreg_UnLoadKey(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
447                        struct winreg_UnLoadKey *r)
448 {
449         return WERR_NOT_SUPPORTED;
450 }
451
452
453 /* 
454   winreg_InitiateSystemShutdown 
455 */
456 static WERROR winreg_InitiateSystemShutdown(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
457                        struct winreg_InitiateSystemShutdown *r)
458 {
459         return WERR_NOT_SUPPORTED;
460 }
461
462
463 /* 
464   winreg_AbortSystemShutdown 
465 */
466 static WERROR winreg_AbortSystemShutdown(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
467                        struct winreg_AbortSystemShutdown *r)
468 {
469         return WERR_NOT_SUPPORTED;
470 }
471
472
473 /* 
474   winreg_GetVersion 
475 */
476 static WERROR winreg_GetVersion(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
477                        struct winreg_GetVersion *r)
478 {
479         r->out.version = 5;
480         return WERR_OK;
481 }
482
483
484 /* 
485   winreg_QueryMultipleValues 
486 */
487 static WERROR winreg_QueryMultipleValues(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
488                        struct winreg_QueryMultipleValues *r)
489 {
490         return WERR_NOT_SUPPORTED;
491 }
492
493
494 /* 
495   winreg_InitiateSystemShutdownEx 
496 */
497 static WERROR winreg_InitiateSystemShutdownEx(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
498                        struct winreg_InitiateSystemShutdownEx *r)
499 {
500         return WERR_NOT_SUPPORTED;
501 }
502
503
504 /* 
505   winreg_SaveKeyEx 
506 */
507 static WERROR winreg_SaveKeyEx(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
508                        struct winreg_SaveKeyEx *r)
509 {
510         return WERR_NOT_SUPPORTED;
511 }
512
513
514 /* 
515   winreg_QueryMultipleValues2 
516 */
517 static WERROR winreg_QueryMultipleValues2(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
518                        struct winreg_QueryMultipleValues2 *r)
519 {
520         return WERR_NOT_SUPPORTED;
521 }
522
523
524 /* include the generated boilerplate */
525 #include "librpc/gen_ndr/ndr_winreg_s.c"