r24727: Initialize variable.
[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 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 = &value_type;
412         r->out.length = talloc(mem_ctx, uint32_t);
413         if (!r->out.length) {
414                 return WERR_NOMEM;
415         }
416         *r->out.length = value_data.length;
417         if (r->in.data == NULL) {
418                 r->out.size = talloc(mem_ctx, uint32_t);
419                 *r->out.size = value_data.length;
420         } else {
421                 r->out.size = r->in.size;
422                 r->out.data = value_data.data;
423         }
424
425         return WERR_OK;
426 }
427
428
429 /* 
430   winreg_ReplaceKey 
431 */
432 static WERROR dcesrv_winreg_ReplaceKey(struct dcesrv_call_state *dce_call, 
433                                                                            TALLOC_CTX *mem_ctx,
434                                                                            struct winreg_ReplaceKey *r)
435 {
436         return WERR_NOT_SUPPORTED;
437 }
438
439
440 /* 
441   winreg_RestoreKey 
442 */
443 static WERROR dcesrv_winreg_RestoreKey(struct dcesrv_call_state *dce_call, 
444                                                                            TALLOC_CTX *mem_ctx, 
445                                                                            struct winreg_RestoreKey *r)
446 {
447         return WERR_NOT_SUPPORTED;
448 }
449
450
451 /* 
452   winreg_SaveKey 
453 */
454 static WERROR dcesrv_winreg_SaveKey(struct dcesrv_call_state *dce_call, 
455                                                                         TALLOC_CTX *mem_ctx, 
456                                                                         struct winreg_SaveKey *r)
457 {
458         return WERR_NOT_SUPPORTED;
459 }
460
461
462 /* 
463   winreg_SetKeySecurity 
464 */
465 static WERROR dcesrv_winreg_SetKeySecurity(struct dcesrv_call_state *dce_call, 
466                                                                                    TALLOC_CTX *mem_ctx, 
467                                                                                    struct winreg_SetKeySecurity *r)
468 {
469         return WERR_NOT_SUPPORTED;
470 }
471
472
473 /* 
474   winreg_SetValue 
475 */
476 static WERROR dcesrv_winreg_SetValue(struct dcesrv_call_state *dce_call, 
477                                                                          TALLOC_CTX *mem_ctx, 
478                                                                          struct winreg_SetValue *r)
479 {
480         struct dcesrv_handle *h;
481         struct registry_key *key;
482         WERROR result;
483         DATA_BLOB data;
484
485         DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
486
487         key = h->data;
488         
489         data.data = r->in.data;
490         data.length = r->in.size;
491         result = reg_val_set(key, r->in.name.name, r->in.type, data);
492
493         return result;
494 }
495
496
497 /* 
498   winreg_UnLoadKey 
499 */
500 static WERROR dcesrv_winreg_UnLoadKey(struct dcesrv_call_state *dce_call, 
501                                                                           TALLOC_CTX *mem_ctx, 
502                                                                           struct winreg_UnLoadKey *r)
503 {
504         return WERR_NOT_SUPPORTED;
505 }
506
507
508 /* 
509   winreg_InitiateSystemShutdown 
510 */
511 static WERROR dcesrv_winreg_InitiateSystemShutdown(
512                                                 struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
513                                                 struct winreg_InitiateSystemShutdown *r)
514 {
515         return WERR_NOT_SUPPORTED;
516 }
517
518
519 /* 
520   winreg_AbortSystemShutdown 
521 */
522 static WERROR dcesrv_winreg_AbortSystemShutdown(
523                 struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
524                 struct winreg_AbortSystemShutdown *r)
525 {
526         return WERR_NOT_SUPPORTED;
527 }
528
529
530 /* 
531   winreg_GetVersion 
532 */
533 static WERROR dcesrv_winreg_GetVersion(struct dcesrv_call_state *dce_call, 
534                                                                            TALLOC_CTX *mem_ctx, 
535                                                                            struct winreg_GetVersion *r)
536 {
537         struct dcesrv_handle *h;
538
539         DCESRV_PULL_HANDLE_FAULT(h, r->in.handle, HTYPE_REGKEY);
540
541         r->out.version = talloc(mem_ctx, uint32_t);
542         W_ERROR_HAVE_NO_MEMORY(r->out.version);
543
544         *r->out.version = 5;
545
546         return WERR_OK;
547 }
548
549
550 /* 
551   winreg_QueryMultipleValues 
552 */
553 static WERROR dcesrv_winreg_QueryMultipleValues(
554         struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
555         struct winreg_QueryMultipleValues *r)
556 {
557         return WERR_NOT_SUPPORTED;
558 }
559
560
561 /* 
562   winreg_InitiateSystemShutdownEx 
563 */
564 static WERROR dcesrv_winreg_InitiateSystemShutdownEx(
565                 struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
566                 struct winreg_InitiateSystemShutdownEx *r)
567 {
568         return WERR_NOT_SUPPORTED;
569 }
570
571
572 /* 
573   winreg_SaveKeyEx 
574 */
575 static WERROR dcesrv_winreg_SaveKeyEx(struct dcesrv_call_state *dce_call, 
576                                                                           TALLOC_CTX *mem_ctx,
577                                                                           struct winreg_SaveKeyEx *r)
578 {
579         return WERR_NOT_SUPPORTED;
580 }
581
582
583 /* 
584   winreg_QueryMultipleValues2 
585 */
586 static WERROR dcesrv_winreg_QueryMultipleValues2(
587                 struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx,
588                 struct winreg_QueryMultipleValues2 *r)
589 {
590         return WERR_NOT_SUPPORTED;
591 }
592
593
594 /* include the generated boilerplate */
595 #include "librpc/gen_ndr/ndr_winreg_s.c"