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