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