r24817: Don't REG_NONE for all value types. Patch from
[kai/samba.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 3 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, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "rpc_server/dcerpc_server.h"
24 #include "lib/registry/registry.h"
25 #include "librpc/gen_ndr/ndr_winreg.h"
26 #include "rpc_server/common/common.h"
27 #include "librpc/gen_ndr/ndr_security.h"
28
29 enum handle_types { HTYPE_REGVAL, HTYPE_REGKEY };
30
31 static NTSTATUS dcerpc_winreg_bind(struct dcesrv_call_state *dce_call, 
32                                                                    const struct dcesrv_interface *iface)
33 {
34         struct registry_context *ctx;
35         WERROR err;
36
37         err = reg_open_samba(dce_call->context,
38                              &ctx, dce_call->conn->auth_state.session_info, NULL);
39
40         if (!W_ERROR_IS_OK(err)) {
41                 DEBUG(0, ("Error opening registry: %s\n", win_errstr(err)));
42                 return NT_STATUS_UNSUCCESSFUL;
43         }
44
45         dce_call->context->private = ctx;
46
47         return NT_STATUS_OK;
48 }
49
50 #define DCESRV_INTERFACE_WINREG_BIND dcerpc_winreg_bind
51
52 static WERROR dcesrv_winreg_openhive(struct dcesrv_call_state *dce_call, 
53                                                                          TALLOC_CTX *mem_ctx, uint32_t hkey, 
54                                                                          struct policy_handle **outh)
55 {
56         struct registry_context *ctx = dce_call->context->private;
57         struct dcesrv_handle *h; 
58         WERROR error;
59
60         h = dcesrv_handle_new(dce_call->context, HTYPE_REGKEY); 
61
62         error = reg_get_predefined_key(ctx, hkey, (struct registry_key **)&h->data);
63         if (!W_ERROR_IS_OK(error)) {
64                 return error;
65         }
66         
67         *outh = &h->wire_handle; 
68
69         return error; 
70 }
71
72 #define func_winreg_OpenHive(k,n) static WERROR dcesrv_winreg_Open ## k (struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct winreg_Open ## k *r) \
73 { \
74         return dcesrv_winreg_openhive (dce_call, mem_ctx, n, &r->out.handle);\
75 }
76
77 func_winreg_OpenHive(HKCR,HKEY_CLASSES_ROOT)
78 func_winreg_OpenHive(HKCU,HKEY_CURRENT_USER)
79 func_winreg_OpenHive(HKLM,HKEY_LOCAL_MACHINE)
80 func_winreg_OpenHive(HKPD,HKEY_PERFORMANCE_DATA)
81 func_winreg_OpenHive(HKU,HKEY_USERS)
82 func_winreg_OpenHive(HKCC,HKEY_CURRENT_CONFIG)
83 func_winreg_OpenHive(HKDD,HKEY_DYN_DATA)
84 func_winreg_OpenHive(HKPT,HKEY_PERFORMANCE_TEXT)
85 func_winreg_OpenHive(HKPN,HKEY_PERFORMANCE_NLSTEXT)
86
87 /* 
88   winreg_CloseKey 
89 */
90 static WERROR dcesrv_winreg_CloseKey(struct dcesrv_call_state *dce_call, 
91                                                                          TALLOC_CTX *mem_ctx,
92                                                                          struct winreg_CloseKey *r)
93 {
94         struct dcesrv_handle *h; 
95
96         DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
97
98         talloc_free(h);
99
100         ZERO_STRUCTP(r->out.handle);
101
102         return WERR_OK;
103 }
104
105 /* 
106   winreg_CreateKey 
107 */
108 static WERROR dcesrv_winreg_CreateKey(struct dcesrv_call_state *dce_call, 
109                                                                           TALLOC_CTX *mem_ctx, 
110                                                                           struct winreg_CreateKey *r)
111 {
112         struct dcesrv_handle *h, *newh;
113         WERROR error;
114         struct security_descriptor sd;
115
116         DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
117         
118         newh = dcesrv_handle_new(dce_call->context, HTYPE_REGKEY);
119
120         /* the security descriptor is optional */
121         if (r->in.secdesc != NULL) {
122                 DATA_BLOB sdblob;
123                 NTSTATUS status;
124                 sdblob.data = r->in.secdesc->sd.data;
125                 sdblob.length = r->in.secdesc->sd.len;
126                 if (sdblob.data == NULL) {
127                         return WERR_INVALID_PARAM;
128                 }
129                 status = ndr_pull_struct_blob_all(&sdblob, mem_ctx, &sd, 
130                                                   (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
131                 if (!NT_STATUS_IS_OK(status)) {
132                         return WERR_INVALID_PARAM;
133                 }
134         }
135
136         error = reg_key_add_name(newh, (struct registry_key *)h->data, 
137                                                          r->in.name.name, NULL, r->in.secdesc?&sd:NULL, 
138                                                          (struct registry_key **)&newh->data);
139         if (W_ERROR_IS_OK(error)) {
140                 r->out.new_handle = &newh->wire_handle;
141         } else {
142                 talloc_free(newh);
143         }
144
145         return error;
146 }
147
148
149 /* 
150   winreg_DeleteKey 
151 */
152 static WERROR dcesrv_winreg_DeleteKey(struct dcesrv_call_state *dce_call, 
153                                                                           TALLOC_CTX *mem_ctx,
154                                                                           struct winreg_DeleteKey *r)
155 {
156         struct dcesrv_handle *h;
157
158         DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
159         
160         return reg_key_del((struct registry_key *)h->data, r->in.key.name);
161 }
162
163
164 /* 
165   winreg_DeleteValue 
166 */
167 static WERROR dcesrv_winreg_DeleteValue(struct dcesrv_call_state *dce_call, 
168                                                                                 TALLOC_CTX *mem_ctx,
169                                                                                 struct winreg_DeleteValue *r)
170 {
171         struct dcesrv_handle *h;
172         struct registry_key *key;
173
174         DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
175
176         key = h->data;
177         
178         return reg_del_value(key, r->in.value.name);
179 }
180
181
182 /* 
183   winreg_EnumKey 
184 */
185 static WERROR dcesrv_winreg_EnumKey(struct dcesrv_call_state *dce_call, 
186                                                                         TALLOC_CTX *mem_ctx,
187                                                                         struct winreg_EnumKey *r)
188 {
189         struct dcesrv_handle *h;
190         const char *name;
191         NTTIME last_mod;
192
193         DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
194
195         r->out.result = reg_key_get_subkey_by_index(mem_ctx, 
196                                                 (struct registry_key *)h->data, r->in.enum_index, 
197                                                 &name, NULL, &last_mod);
198
199         if (W_ERROR_IS_OK(r->out.result)) {
200                 if (2*strlen_m_term(name) > r->in.name->size) {
201                         return WERR_MORE_DATA;
202                 }
203                 r->out.name->length = 2*strlen_m_term(name);
204                 r->out.name->name = name;
205                 r->out.keyclass = talloc_zero(mem_ctx, struct winreg_StringBuf);
206                 if (r->in.last_changed_time) {
207                         r->out.last_changed_time = &last_mod;
208                 }
209         }
210         
211         return r->out.result;
212 }
213
214
215 /* 
216   winreg_EnumValue 
217 */
218 static WERROR dcesrv_winreg_EnumValue(struct dcesrv_call_state *dce_call, 
219                                                                           TALLOC_CTX *mem_ctx,
220                                                                           struct winreg_EnumValue *r)
221 {
222         struct dcesrv_handle *h;
223         struct registry_key *key;
224         WERROR result;
225         const char *data_name;
226         uint32_t data_type;
227         DATA_BLOB data;
228
229         DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
230
231         key = h->data;
232
233         result = reg_key_get_value_by_index(mem_ctx, key, r->in.enum_index, 
234                                                                                 &data_name,
235                                                                                 &data_type, &data);
236         if (!W_ERROR_IS_OK(result)) {
237                 return result;
238         }
239
240         /* the client can optionally pass a NULL for type, meaning they don't
241            want that back */
242         if (r->in.type != NULL) {
243                 r->out.type = talloc(mem_ctx, enum winreg_Type);
244                 *r->out.type = data_type;
245         }
246
247         /* check the client has enough room for the value */
248         if (r->in.value != NULL &&
249             r->in.size != NULL && 
250             data.length > *r->in.size) {
251                 return WERR_MORE_DATA;
252         }
253         
254         /* and enough room for the name */
255         if (r->in.name->size < 2*strlen_m_term(data_name)) {
256                 return WERR_MORE_DATA;          
257         }
258
259         r->out.name->name = data_name;
260         r->out.name->length = 2*strlen_m_term(data_name);
261         r->out.name->size = 2*strlen_m_term(data_name);
262
263         if (r->in.value) {
264                 r->out.value = data.data;
265         }
266
267         if (r->in.size) {
268                 r->out.size = talloc(mem_ctx, uint32_t);
269                 *r->out.size = data.length;
270                 r->out.length = r->out.size;
271         }
272         
273         return WERR_OK;
274 }
275
276
277 /* 
278   winreg_FlushKey 
279 */
280 static WERROR dcesrv_winreg_FlushKey(struct dcesrv_call_state *dce_call, 
281                                                                          TALLOC_CTX *mem_ctx, 
282                                                                          struct winreg_FlushKey *r)
283 {
284         struct dcesrv_handle *h;
285
286         DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
287
288         return reg_key_flush(h->data);
289 }
290
291
292 /* 
293   winreg_GetKeySecurity 
294 */
295 static WERROR dcesrv_winreg_GetKeySecurity(struct dcesrv_call_state *dce_call, 
296                                                                                    TALLOC_CTX *mem_ctx, 
297                                                                                    struct winreg_GetKeySecurity *r)
298 {
299         struct dcesrv_handle *h;
300
301         DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
302
303         return WERR_NOT_SUPPORTED;
304 }
305
306
307 /* 
308   winreg_LoadKey 
309 */
310 static WERROR dcesrv_winreg_LoadKey(struct dcesrv_call_state *dce_call, 
311                                                                         TALLOC_CTX *mem_ctx, 
312                                                                         struct winreg_LoadKey *r)
313 {
314         return WERR_NOT_SUPPORTED;
315 }
316
317
318 /* 
319   winreg_NotifyChangeKeyValue 
320 */
321 static WERROR dcesrv_winreg_NotifyChangeKeyValue(
322                                         struct dcesrv_call_state *dce_call, 
323                                         TALLOC_CTX *mem_ctx, struct winreg_NotifyChangeKeyValue *r)
324 {
325         return WERR_NOT_SUPPORTED;
326 }
327
328
329 /* 
330   winreg_OpenKey 
331 */
332 static WERROR dcesrv_winreg_OpenKey(struct dcesrv_call_state *dce_call, 
333                                                                         TALLOC_CTX *mem_ctx,
334                                                                         struct winreg_OpenKey *r)
335 {
336         struct dcesrv_handle *h, *newh;
337         WERROR result;
338
339         DCESRV_PULL_HANDLE_FAULT(h, r->in.parent_handle, HTYPE_REGKEY);
340
341         if (r->in.keyname.name && strcmp(r->in.keyname.name, "") == 0) {
342                 newh = talloc_reference(dce_call->context, h);
343                 result = WERR_OK;
344         } else {
345                 newh = dcesrv_handle_new(dce_call->context, HTYPE_REGKEY);
346                 result = reg_open_key(newh, (struct registry_key *)h->data, 
347                                       r->in.keyname.name, (struct registry_key **)&newh->data);
348         }
349         
350         if (W_ERROR_IS_OK(result)) {
351                 r->out.handle = &newh->wire_handle; 
352         } else {
353                 talloc_free(newh);
354         }
355         
356         return result;
357 }
358
359
360 /* 
361   winreg_QueryInfoKey 
362 */
363 static WERROR dcesrv_winreg_QueryInfoKey(struct dcesrv_call_state *dce_call, 
364                                                                                  TALLOC_CTX *mem_ctx,
365                                                                                  struct winreg_QueryInfoKey *r)
366 {
367         struct dcesrv_handle *h;
368         struct registry_key *k;
369         WERROR ret;
370         const char *classname = NULL;
371
372         DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
373
374         k = h->data;
375
376         ret = reg_key_get_info(mem_ctx, k, &classname, r->out.num_subkeys, 
377                                                    r->out.num_values, r->out.last_changed_time);
378
379         if (r->out.classname != NULL)
380                 r->out.classname->name = classname;
381
382         return ret;
383 }
384
385
386 /* 
387   winreg_QueryValue 
388 */
389 static WERROR dcesrv_winreg_QueryValue(struct dcesrv_call_state *dce_call, 
390                                                                            TALLOC_CTX *mem_ctx,
391                                                                            struct winreg_QueryValue *r)
392 {
393         struct dcesrv_handle *h;
394         struct registry_key *key;
395         uint32_t value_type;
396         DATA_BLOB value_data;
397         WERROR result;
398
399         DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
400
401         key = h->data;
402         
403         result = reg_key_get_value_by_name(mem_ctx, key, r->in.value_name.name, 
404                                                                            &value_type, &value_data);
405
406         if (!W_ERROR_IS_OK(result)) { 
407                 return result;
408         }
409
410         /* Just asking for the size of the buffer */
411         r->out.type = talloc(mem_ctx, uint32_t);
412         if (!r->out.type) {
413                 return WERR_NOMEM;
414         }
415         *r->out.type = value_type;
416         r->out.length = talloc(mem_ctx, uint32_t);
417         if (!r->out.length) {
418                 return WERR_NOMEM;
419         }
420         *r->out.length = value_data.length;
421         if (r->in.data == NULL) {
422                 r->out.size = talloc(mem_ctx, uint32_t);
423                 *r->out.size = value_data.length;
424         } else {
425                 r->out.size = r->in.size;
426                 r->out.data = value_data.data;
427         }
428
429         return WERR_OK;
430 }
431
432
433 /* 
434   winreg_ReplaceKey 
435 */
436 static WERROR dcesrv_winreg_ReplaceKey(struct dcesrv_call_state *dce_call, 
437                                                                            TALLOC_CTX *mem_ctx,
438                                                                            struct winreg_ReplaceKey *r)
439 {
440         return WERR_NOT_SUPPORTED;
441 }
442
443
444 /* 
445   winreg_RestoreKey 
446 */
447 static WERROR dcesrv_winreg_RestoreKey(struct dcesrv_call_state *dce_call, 
448                                                                            TALLOC_CTX *mem_ctx, 
449                                                                            struct winreg_RestoreKey *r)
450 {
451         return WERR_NOT_SUPPORTED;
452 }
453
454
455 /* 
456   winreg_SaveKey 
457 */
458 static WERROR dcesrv_winreg_SaveKey(struct dcesrv_call_state *dce_call, 
459                                                                         TALLOC_CTX *mem_ctx, 
460                                                                         struct winreg_SaveKey *r)
461 {
462         return WERR_NOT_SUPPORTED;
463 }
464
465
466 /* 
467   winreg_SetKeySecurity 
468 */
469 static WERROR dcesrv_winreg_SetKeySecurity(struct dcesrv_call_state *dce_call, 
470                                                                                    TALLOC_CTX *mem_ctx, 
471                                                                                    struct winreg_SetKeySecurity *r)
472 {
473         return WERR_NOT_SUPPORTED;
474 }
475
476
477 /* 
478   winreg_SetValue 
479 */
480 static WERROR dcesrv_winreg_SetValue(struct dcesrv_call_state *dce_call, 
481                                                                          TALLOC_CTX *mem_ctx, 
482                                                                          struct winreg_SetValue *r)
483 {
484         struct dcesrv_handle *h;
485         struct registry_key *key;
486         WERROR result;
487         DATA_BLOB data;
488
489         DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
490
491         key = h->data;
492         
493         data.data = r->in.data;
494         data.length = r->in.size;
495         result = reg_val_set(key, r->in.name.name, r->in.type, data);
496
497         return result;
498 }
499
500
501 /* 
502   winreg_UnLoadKey 
503 */
504 static WERROR dcesrv_winreg_UnLoadKey(struct dcesrv_call_state *dce_call, 
505                                                                           TALLOC_CTX *mem_ctx, 
506                                                                           struct winreg_UnLoadKey *r)
507 {
508         return WERR_NOT_SUPPORTED;
509 }
510
511
512 /* 
513   winreg_InitiateSystemShutdown 
514 */
515 static WERROR dcesrv_winreg_InitiateSystemShutdown(
516                                                 struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
517                                                 struct winreg_InitiateSystemShutdown *r)
518 {
519         return WERR_NOT_SUPPORTED;
520 }
521
522
523 /* 
524   winreg_AbortSystemShutdown 
525 */
526 static WERROR dcesrv_winreg_AbortSystemShutdown(
527                 struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
528                 struct winreg_AbortSystemShutdown *r)
529 {
530         return WERR_NOT_SUPPORTED;
531 }
532
533
534 /* 
535   winreg_GetVersion 
536 */
537 static WERROR dcesrv_winreg_GetVersion(struct dcesrv_call_state *dce_call, 
538                                                                            TALLOC_CTX *mem_ctx, 
539                                                                            struct winreg_GetVersion *r)
540 {
541         struct dcesrv_handle *h;
542
543         DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
544
545         r->out.version = talloc(mem_ctx, uint32_t);
546         W_ERROR_HAVE_NO_MEMORY(r->out.version);
547
548         *r->out.version = 5;
549
550         return WERR_OK;
551 }
552
553
554 /* 
555   winreg_QueryMultipleValues 
556 */
557 static WERROR dcesrv_winreg_QueryMultipleValues(
558         struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
559         struct winreg_QueryMultipleValues *r)
560 {
561         return WERR_NOT_SUPPORTED;
562 }
563
564
565 /* 
566   winreg_InitiateSystemShutdownEx 
567 */
568 static WERROR dcesrv_winreg_InitiateSystemShutdownEx(
569                 struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
570                 struct winreg_InitiateSystemShutdownEx *r)
571 {
572         return WERR_NOT_SUPPORTED;
573 }
574
575
576 /* 
577   winreg_SaveKeyEx 
578 */
579 static WERROR dcesrv_winreg_SaveKeyEx(struct dcesrv_call_state *dce_call, 
580                                                                           TALLOC_CTX *mem_ctx,
581                                                                           struct winreg_SaveKeyEx *r)
582 {
583         return WERR_NOT_SUPPORTED;
584 }
585
586
587 /* 
588   winreg_QueryMultipleValues2 
589 */
590 static WERROR dcesrv_winreg_QueryMultipleValues2(
591                 struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
592                 struct winreg_QueryMultipleValues2 *r)
593 {
594         return WERR_NOT_SUPPORTED;
595 }
596
597
598 /* include the generated boilerplate */
599 #include "librpc/gen_ndr/ndr_winreg_s.c"