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