4a68469a54b2248a0d0cf55126caabd00c53e7dc
[sfrench/samba-autobuild/.git] / source3 / registry / reg_api.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  Virtual Windows Registry Layer
4  *  Copyright (C) Volker Lendecke 2006
5  *  Copyright (C) Michael Adam 2007-2010
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
19  */
20
21 /* Attempt to wrap the existing API in a more winreg.idl-like way */
22
23 /*
24  * Here is a list of winreg.idl functions and corresponding implementations
25  * provided here:
26  *
27  * 0x00         winreg_OpenHKCR
28  * 0x01         winreg_OpenHKCU
29  * 0x02         winreg_OpenHKLM
30  * 0x03         winreg_OpenHKPD
31  * 0x04         winreg_OpenHKU
32  * 0x05         winreg_CloseKey
33  * 0x06         winreg_CreateKey                        reg_createkey
34  * 0x07         winreg_DeleteKey                        reg_deletekey
35  * 0x08         winreg_DeleteValue                      reg_deletevalue
36  * 0x09         winreg_EnumKey                          reg_enumkey
37  * 0x0a         winreg_EnumValue                        reg_enumvalue
38  * 0x0b         winreg_FlushKey
39  * 0x0c         winreg_GetKeySecurity                   reg_getkeysecurity
40  * 0x0d         winreg_LoadKey
41  * 0x0e         winreg_NotifyChangeKeyValue
42  * 0x0f         winreg_OpenKey                          reg_openkey
43  * 0x10         winreg_QueryInfoKey                     reg_queryinfokey
44  * 0x11         winreg_QueryValue                       reg_queryvalue
45  * 0x12         winreg_ReplaceKey
46  * 0x13         winreg_RestoreKey                       reg_restorekey
47  * 0x14         winreg_SaveKey                          reg_savekey
48  * 0x15         winreg_SetKeySecurity                   reg_setkeysecurity
49  * 0x16         winreg_SetValue                         reg_setvalue
50  * 0x17         winreg_UnLoadKey
51  * 0x18         winreg_InitiateSystemShutdown
52  * 0x19         winreg_AbortSystemShutdown
53  * 0x1a         winreg_GetVersion                       reg_getversion
54  * 0x1b         winreg_OpenHKCC
55  * 0x1c         winreg_OpenHKDD
56  * 0x1d         winreg_QueryMultipleValues              reg_querymultiplevalues
57  * 0x1e         winreg_InitiateSystemShutdownEx
58  * 0x1f         winreg_SaveKeyEx
59  * 0x20         winreg_OpenHKPT
60  * 0x21         winreg_OpenHKPN
61  * 0x22         winreg_QueryMultipleValues2             reg_querymultiplevalues
62  *
63  */
64
65 #include "includes.h"
66 #include "registry.h"
67 #include "reg_api.h"
68 #include "reg_cachehook.h"
69 #include "reg_backend_db.h"
70 #include "reg_dispatcher.h"
71 #include "reg_objects.h"
72 #include "../librpc/gen_ndr/ndr_security.h"
73 #include "reg_parse_internal.h"
74
75 #undef DBGC_CLASS
76 #define DBGC_CLASS DBGC_REGISTRY
77
78
79 /**********************************************************************
80  * Helper functions
81  **********************************************************************/
82
83 static WERROR fill_value_cache(struct registry_key *key)
84 {
85         WERROR werr;
86
87         if (key->values != NULL) {
88                 if (!reg_values_need_update(key->key, key->values)) {
89                         return WERR_OK;
90                 }
91         }
92
93         TALLOC_FREE(key->values);
94         werr = regval_ctr_init(key, &(key->values));
95         W_ERROR_NOT_OK_RETURN(werr);
96
97         if (fetch_reg_values(key->key, key->values) == -1) {
98                 TALLOC_FREE(key->values);
99                 return WERR_FILE_NOT_FOUND;
100         }
101
102         return WERR_OK;
103 }
104
105 static WERROR fill_subkey_cache(struct registry_key *key)
106 {
107         WERROR werr;
108
109         if (key->subkeys != NULL) {
110                 if (!reg_subkeys_need_update(key->key, key->subkeys)) {
111                         return WERR_OK;
112                 }
113         }
114
115         TALLOC_FREE(key->subkeys);
116         werr = regsubkey_ctr_init(key, &(key->subkeys));
117         W_ERROR_NOT_OK_RETURN(werr);
118
119         if (fetch_reg_keys(key->key, key->subkeys) == -1) {
120                 TALLOC_FREE(key->subkeys);
121                 return WERR_FILE_NOT_FOUND;
122         }
123
124         return WERR_OK;
125 }
126
127 static int regkey_destructor(struct registry_key_handle *key)
128 {
129         return regdb_close();
130 }
131
132 static WERROR regkey_open_onelevel(TALLOC_CTX *mem_ctx, 
133                                    struct registry_key *parent,
134                                    const char *name,
135                                    const struct security_token *token,
136                                    uint32_t access_desired,
137                                    struct registry_key **pregkey)
138 {
139         WERROR          result = WERR_OK;
140         struct registry_key *regkey;
141         struct registry_key_handle *key;
142
143         DEBUG(7,("regkey_open_onelevel: name = [%s]\n", name));
144
145         SMB_ASSERT(strchr(name, '\\') == NULL);
146
147         if (!(regkey = talloc_zero(mem_ctx, struct registry_key)) ||
148             !(regkey->token = dup_nt_token(regkey, token)) ||
149             !(regkey->key = talloc_zero(regkey, struct registry_key_handle)))
150         {
151                 result = WERR_NOT_ENOUGH_MEMORY;
152                 goto done;
153         }
154
155         result = regdb_open();
156         if (!(W_ERROR_IS_OK(result))) {
157                 goto done;
158         }
159
160         key = regkey->key;
161         talloc_set_destructor(key, regkey_destructor);
162
163         /* initialization */
164
165         key->type = REG_KEY_GENERIC;
166
167         if (name[0] == '\0') {
168                 /*
169                  * Open a copy of the parent key
170                  */
171                 if (!parent) {
172                         result = WERR_FILE_NOT_FOUND;
173                         goto done;
174                 }
175                 key->name = talloc_strdup(key, parent->key->name);
176         }
177         else {
178                 /*
179                  * Normal subkey open
180                  */
181                 key->name = talloc_asprintf(key, "%s%s%s",
182                                             parent ? parent->key->name : "",
183                                             parent ? "\\": "",
184                                             name);
185         }
186
187         if (key->name == NULL) {
188                 result = WERR_NOT_ENOUGH_MEMORY;
189                 goto done;
190         }
191
192         /* Tag this as a Performance Counter Key */
193
194         if( strncasecmp_m(key->name, KEY_HKPD, strlen(KEY_HKPD)) == 0 )
195                 key->type = REG_KEY_HKPD;
196
197         /* Look up the table of registry I/O operations */
198
199         key->ops = reghook_cache_find( key->name );
200         if (key->ops == NULL) {
201                 DEBUG(0,("reg_open_onelevel: Failed to assign "
202                          "registry_ops to [%s]\n", key->name ));
203                 result = WERR_FILE_NOT_FOUND;
204                 goto done;
205         }
206
207         /* FIXME: Existence is currently checked by fetching the subkeys */
208
209         result = fill_subkey_cache(regkey);
210         if (!W_ERROR_IS_OK(result)) {
211                 goto done;
212         }
213
214         if ( !regkey_access_check( key, access_desired, &key->access_granted,
215                                    token ) ) {
216                 result = WERR_ACCESS_DENIED;
217                 goto done;
218         }
219
220         *pregkey = regkey;
221         result = WERR_OK;
222
223 done:
224         if ( !W_ERROR_IS_OK(result) ) {
225                 TALLOC_FREE(regkey);
226         }
227
228         return result;
229 }
230
231 WERROR reg_openhive(TALLOC_CTX *mem_ctx, const char *hive,
232                     uint32_t desired_access,
233                     const struct security_token *token,
234                     struct registry_key **pkey)
235 {
236         const struct hive_info *hi;
237         SMB_ASSERT(hive != NULL);
238         SMB_ASSERT(strchr(hive, '\\') == NULL);
239
240         hi = hive_info(hive);
241         if (hi == NULL) {
242                 return WERR_FILE_NOT_FOUND;
243         }
244
245         return regkey_open_onelevel(mem_ctx, NULL, hi->short_name, token,
246                                     desired_access, pkey);
247 }
248
249
250 /**********************************************************************
251  * The API functions
252  **********************************************************************/
253
254 WERROR reg_openkey(TALLOC_CTX *mem_ctx, struct registry_key *parent,
255                    const char *name, uint32_t desired_access,
256                    struct registry_key **pkey)
257 {
258         struct registry_key *direct_parent = parent;
259         WERROR err;
260         char *p, *path;
261         size_t len;
262         TALLOC_CTX *frame = talloc_stackframe();
263
264         path = talloc_strdup(frame, name);
265         if (path == NULL) {
266                 err = WERR_NOT_ENOUGH_MEMORY;
267                 goto error;
268         }
269
270         len = strlen(path);
271
272         if ((len > 0) && (path[len-1] == '\\')) {
273                 path[len-1] = '\0';
274         }
275
276         while ((p = strchr(path, '\\')) != NULL) {
277                 char *name_component;
278                 struct registry_key *tmp;
279
280                 name_component = talloc_strndup(frame, path, (p - path));
281                 if (name_component == NULL) {
282                         err = WERR_NOT_ENOUGH_MEMORY;
283                         goto error;
284                 }
285
286                 err = regkey_open_onelevel(frame, direct_parent,
287                                            name_component, parent->token,
288                                            KEY_ENUMERATE_SUB_KEYS, &tmp);
289
290                 if (!W_ERROR_IS_OK(err)) {
291                         goto error;
292                 }
293
294                 direct_parent = tmp;
295                 path = p+1;
296         }
297
298         err = regkey_open_onelevel(mem_ctx, direct_parent, path, parent->token,
299                                    desired_access, pkey);
300
301 error:
302         talloc_free(frame);
303         return err;
304 }
305
306 WERROR reg_enumkey(TALLOC_CTX *mem_ctx, struct registry_key *key,
307                    uint32_t idx, char **name, NTTIME *last_write_time)
308 {
309         WERROR err;
310
311         if (!(key->key->access_granted & KEY_ENUMERATE_SUB_KEYS)) {
312                 return WERR_ACCESS_DENIED;
313         }
314
315         err = fill_subkey_cache(key);
316         if (!W_ERROR_IS_OK(err)) {
317                 return err;
318         }
319
320         if (idx >= regsubkey_ctr_numkeys(key->subkeys)) {
321                 return WERR_NO_MORE_ITEMS;
322         }
323
324         if (!(*name = talloc_strdup(mem_ctx,
325                         regsubkey_ctr_specific_key(key->subkeys, idx))))
326         {
327                 return WERR_NOT_ENOUGH_MEMORY;
328         }
329
330         if (last_write_time) {
331                 *last_write_time = 0;
332         }
333
334         return WERR_OK;
335 }
336
337 WERROR reg_enumvalue(TALLOC_CTX *mem_ctx, struct registry_key *key,
338                      uint32_t idx, char **pname, struct registry_value **pval)
339 {
340         struct registry_value *val;
341         struct regval_blob *blob;
342         WERROR err;
343
344         if (!(key->key->access_granted & KEY_QUERY_VALUE)) {
345                 return WERR_ACCESS_DENIED;
346         }
347
348         err = fill_value_cache(key);
349         if (!(W_ERROR_IS_OK(err))) {
350                 return err;
351         }
352
353         if (idx >= regval_ctr_numvals(key->values)) {
354                 return WERR_NO_MORE_ITEMS;
355         }
356
357         blob = regval_ctr_specific_value(key->values, idx);
358
359         val = talloc_zero(mem_ctx, struct registry_value);
360         if (val == NULL) {
361                 return WERR_NOT_ENOUGH_MEMORY;
362         }
363
364         val->type = regval_type(blob);
365         val->data = data_blob_talloc(mem_ctx, regval_data_p(blob), regval_size(blob));
366
367         if (pname
368             && !(*pname = talloc_strdup(
369                          mem_ctx, regval_name(blob)))) {
370                 TALLOC_FREE(val);
371                 return WERR_NOT_ENOUGH_MEMORY;
372         }
373
374         *pval = val;
375         return WERR_OK;
376 }
377
378 static WERROR reg_enumvalue_nocachefill(TALLOC_CTX *mem_ctx,
379                                         struct registry_key *key,
380                                         uint32_t idx, char **pname,
381                                         struct registry_value **pval)
382 {
383         struct registry_value *val;
384         struct regval_blob *blob;
385
386         if (!(key->key->access_granted & KEY_QUERY_VALUE)) {
387                 return WERR_ACCESS_DENIED;
388         }
389
390         if (idx >= regval_ctr_numvals(key->values)) {
391                 return WERR_NO_MORE_ITEMS;
392         }
393
394         blob = regval_ctr_specific_value(key->values, idx);
395
396         val = talloc_zero(mem_ctx, struct registry_value);
397         if (val == NULL) {
398                 return WERR_NOT_ENOUGH_MEMORY;
399         }
400
401         val->type = regval_type(blob);
402         val->data = data_blob_talloc(mem_ctx, regval_data_p(blob), regval_size(blob));
403
404         if (pname
405             && !(*pname = talloc_strdup(
406                          mem_ctx, regval_name(blob)))) {
407                 TALLOC_FREE(val);
408                 return WERR_NOT_ENOUGH_MEMORY;
409         }
410
411         *pval = val;
412         return WERR_OK;
413 }
414
415 WERROR reg_queryvalue(TALLOC_CTX *mem_ctx, struct registry_key *key,
416                       const char *name, struct registry_value **pval)
417 {
418         WERROR err;
419         uint32_t i;
420
421         if (!(key->key->access_granted & KEY_QUERY_VALUE)) {
422                 return WERR_ACCESS_DENIED;
423         }
424
425         if (!(W_ERROR_IS_OK(err = fill_value_cache(key)))) {
426                 return err;
427         }
428
429         for (i=0; i < regval_ctr_numvals(key->values); i++) {
430                 struct regval_blob *blob;
431                 blob = regval_ctr_specific_value(key->values, i);
432                 if (strequal(regval_name(blob), name)) {
433                         /*
434                          * don't use reg_enumvalue here:
435                          * re-reading the values from the disk
436                          * would change the indexing and break
437                          * this function.
438                          */
439                         return reg_enumvalue_nocachefill(mem_ctx, key, i,
440                                                          NULL, pval);
441                 }
442         }
443
444         return WERR_FILE_NOT_FOUND;
445 }
446
447 WERROR reg_querymultiplevalues(TALLOC_CTX *mem_ctx,
448                                struct registry_key *key,
449                                uint32_t num_names,
450                                const char **names,
451                                uint32_t *pnum_vals,
452                                struct registry_value **pvals)
453 {
454         WERROR err;
455         uint32_t i, n, found = 0;
456         struct registry_value *vals;
457
458         if (num_names == 0) {
459                 return WERR_OK;
460         }
461
462         if (!(key->key->access_granted & KEY_QUERY_VALUE)) {
463                 return WERR_ACCESS_DENIED;
464         }
465
466         if (!(W_ERROR_IS_OK(err = fill_value_cache(key)))) {
467                 return err;
468         }
469
470         vals = talloc_zero_array(mem_ctx, struct registry_value, num_names);
471         if (vals == NULL) {
472                 return WERR_NOT_ENOUGH_MEMORY;
473         }
474
475         for (n=0; n < num_names; n++) {
476                 for (i=0; i < regval_ctr_numvals(key->values); i++) {
477                         struct regval_blob *blob;
478                         blob = regval_ctr_specific_value(key->values, i);
479                         if (strequal(regval_name(blob), names[n])) {
480                                 struct registry_value *v;
481                                 err = reg_enumvalue(mem_ctx, key, i, NULL, &v);
482                                 if (!W_ERROR_IS_OK(err)) {
483                                         return err;
484                                 }
485                                 vals[n] = *v;
486                                 found++;
487                         }
488                 }
489         }
490
491         *pvals = vals;
492         *pnum_vals = found;
493
494         return WERR_OK;
495 }
496
497 WERROR reg_queryinfokey(struct registry_key *key, uint32_t *num_subkeys,
498                         uint32_t *max_subkeylen, uint32_t *max_subkeysize, 
499                         uint32_t *num_values, uint32_t *max_valnamelen, 
500                         uint32_t *max_valbufsize, uint32_t *secdescsize,
501                         NTTIME *last_changed_time)
502 {
503         uint32_t i, max_size;
504         size_t max_len;
505         TALLOC_CTX *mem_ctx;
506         WERROR err;
507         struct security_descriptor *secdesc;
508
509         if (!(key->key->access_granted & KEY_QUERY_VALUE)) {
510                 return WERR_ACCESS_DENIED;
511         }
512
513         if (!W_ERROR_IS_OK(fill_subkey_cache(key)) ||
514             !W_ERROR_IS_OK(fill_value_cache(key))) {
515                 return WERR_FILE_NOT_FOUND;
516         }
517
518         max_len = 0;
519         for (i=0; i< regsubkey_ctr_numkeys(key->subkeys); i++) {
520                 max_len = MAX(max_len,
521                         strlen(regsubkey_ctr_specific_key(key->subkeys, i)));
522         }
523
524         *num_subkeys = regsubkey_ctr_numkeys(key->subkeys);
525         *max_subkeylen = max_len;
526         *max_subkeysize = 0;    /* Class length? */
527
528         max_len = 0;
529         max_size = 0;
530         for (i=0; i < regval_ctr_numvals(key->values); i++) {
531                 struct regval_blob *blob;
532                 blob = regval_ctr_specific_value(key->values, i);
533                 max_len = MAX(max_len, strlen(regval_name(blob)));
534                 max_size = MAX(max_size, regval_size(blob));
535         }
536
537         *num_values = regval_ctr_numvals(key->values);
538         *max_valnamelen = max_len;
539         *max_valbufsize = max_size;
540
541         if (!(mem_ctx = talloc_new(key))) {
542                 return WERR_NOT_ENOUGH_MEMORY;
543         }
544
545         err = regkey_get_secdesc(mem_ctx, key->key, &secdesc);
546         if (!W_ERROR_IS_OK(err)) {
547                 TALLOC_FREE(mem_ctx);
548                 return err;
549         }
550
551         *secdescsize = ndr_size_security_descriptor(secdesc, 0);
552         TALLOC_FREE(mem_ctx);
553
554         *last_changed_time = 0;
555
556         return WERR_OK;
557 }
558
559 WERROR reg_createkey(TALLOC_CTX *ctx, struct registry_key *parent,
560                      const char *subkeypath, uint32_t desired_access,
561                      struct registry_key **pkey,
562                      enum winreg_CreateAction *paction)
563 {
564         struct registry_key *key = parent;
565         TALLOC_CTX *mem_ctx;
566         char *path, *end;
567         WERROR err;
568         uint32_t access_granted;
569
570         mem_ctx = talloc_new(ctx);
571         if (mem_ctx == NULL) {
572                 return WERR_NOT_ENOUGH_MEMORY;
573         }
574
575         path = talloc_strdup(mem_ctx, subkeypath);
576         if (path == NULL) {
577                 err = WERR_NOT_ENOUGH_MEMORY;
578                 goto done;
579         }
580
581         err = regdb_transaction_start();
582         if (!W_ERROR_IS_OK(err)) {
583                 DEBUG(0, ("reg_createkey: failed to start transaction: %s\n",
584                           win_errstr(err)));
585                 goto done;
586         }
587
588         while ((end = strchr(path, '\\')) != NULL) {
589                 struct registry_key *tmp;
590                 enum winreg_CreateAction action;
591
592                 *end = '\0';
593
594                 err = reg_createkey(mem_ctx, key, path,
595                                     KEY_ENUMERATE_SUB_KEYS, &tmp, &action);
596                 if (!W_ERROR_IS_OK(err)) {
597                         goto trans_done;
598                 }
599
600                 if (key != parent) {
601                         TALLOC_FREE(key);
602                 }
603
604                 key = tmp;
605                 path = end+1;
606         }
607
608         /*
609          * At this point, "path" contains the one-element subkey of "key". We
610          * can try to open it.
611          */
612
613         err = reg_openkey(ctx, key, path, desired_access, pkey);
614         if (W_ERROR_IS_OK(err)) {
615                 if (paction != NULL) {
616                         *paction = REG_OPENED_EXISTING_KEY;
617                 }
618                 goto trans_done;
619         }
620
621         if (!W_ERROR_EQUAL(err, WERR_FILE_NOT_FOUND)) {
622                 /*
623                  * Something but "notfound" has happened, so bail out
624                  */
625                 goto trans_done;
626         }
627
628         /*
629          * We may (e.g. in the iteration) have opened the key with ENUM_SUBKEY.
630          * Instead of re-opening the key with CREATE_SUB_KEY, we simply
631          * duplicate the access check here and skip the expensive full open.
632          */
633         if (!regkey_access_check(key->key, KEY_CREATE_SUB_KEY, &access_granted,
634                                  key->token))
635         {
636                 err = WERR_ACCESS_DENIED;
637                 goto trans_done;
638         }
639
640         /*
641          * Actually create the subkey
642          */
643
644         err = create_reg_subkey(key->key, path);
645         if (!W_ERROR_IS_OK(err)) {
646                 goto trans_done;
647         }
648
649         /*
650          * Now open the newly created key
651          */
652
653         err = reg_openkey(ctx, key, path, desired_access, pkey);
654         if (W_ERROR_IS_OK(err) && (paction != NULL)) {
655                 *paction = REG_CREATED_NEW_KEY;
656         }
657
658 trans_done:
659         if (W_ERROR_IS_OK(err)) {
660                 err = regdb_transaction_commit();
661                 if (!W_ERROR_IS_OK(err)) {
662                         DEBUG(0, ("reg_createkey: Error committing transaction: %s\n", win_errstr(err)));
663                 }
664         } else {
665                 WERROR err1 = regdb_transaction_cancel();
666                 if (!W_ERROR_IS_OK(err1)) {
667                         DEBUG(0, ("reg_createkey: Error cancelling transaction: %s\n", win_errstr(err1)));
668                 }
669         }
670
671  done:
672         TALLOC_FREE(mem_ctx);
673         return err;
674 }
675
676 static WERROR reg_deletekey_internal(TALLOC_CTX *mem_ctx,
677                                      struct registry_key *parent,
678                                      const char *path, bool lazy)
679 {
680         WERROR err;
681         char *name, *end;
682         struct registry_key *key;
683         name = talloc_strdup(mem_ctx, path);
684         if (name == NULL) {
685                 err = WERR_NOT_ENOUGH_MEMORY;
686                 goto done;
687         }
688
689         /* no subkeys - proceed with delete */
690         end = strrchr(name, '\\');
691         if (end != NULL) {
692                 *end = '\0';
693
694                 err = reg_openkey(mem_ctx, parent, name,
695                                   KEY_CREATE_SUB_KEY, &key);
696                 W_ERROR_NOT_OK_GOTO_DONE(err);
697
698                 parent = key;
699                 name = end+1;
700         }
701
702         if (name[0] == '\0') {
703                 err = WERR_INVALID_PARAMETER;
704                 goto done;
705         }
706
707         err = delete_reg_subkey(parent->key, name, lazy);
708
709 done:
710         return err;
711 }
712
713 WERROR reg_deletekey(struct registry_key *parent, const char *path)
714 {
715         WERROR err;
716         struct registry_key *key;
717         TALLOC_CTX *mem_ctx = talloc_stackframe();
718
719         /* check if the key has subkeys */
720         err = reg_openkey(mem_ctx, parent, path, REG_KEY_READ, &key);
721         W_ERROR_NOT_OK_GOTO_DONE(err);
722
723         err = regdb_transaction_start();
724         if (!W_ERROR_IS_OK(err)) {
725                 DEBUG(0, ("reg_deletekey: Error starting transaction: %s\n",
726                           win_errstr(err)));
727                 goto done;
728         }
729
730         err = fill_subkey_cache(key);
731         if (!W_ERROR_IS_OK(err)) {
732                 goto trans_done;
733         }
734
735         if (regsubkey_ctr_numkeys(key->subkeys) > 0) {
736                 err = WERR_ACCESS_DENIED;
737                 goto trans_done;
738         }
739         err = reg_deletekey_internal(mem_ctx, parent, path, false);
740
741 trans_done:
742         if (W_ERROR_IS_OK(err)) {
743                 err = regdb_transaction_commit();
744                 if (!W_ERROR_IS_OK(err)) {
745                         DEBUG(0, ("reg_deletekey: Error committing transaction: %s\n", win_errstr(err)));
746                 }
747         } else {
748                 WERROR err1 = regdb_transaction_cancel();
749                 if (!W_ERROR_IS_OK(err1)) {
750                         DEBUG(0, ("reg_deletekey: Error cancelling transaction: %s\n", win_errstr(err1)));
751                 }
752         }
753
754 done:
755         TALLOC_FREE(mem_ctx);
756         return err;
757 }
758
759
760 WERROR reg_setvalue(struct registry_key *key, const char *name,
761                     const struct registry_value *val)
762 {
763         struct regval_blob *existing;
764         WERROR err;
765         int res;
766
767         if (!(key->key->access_granted & KEY_SET_VALUE)) {
768                 return WERR_ACCESS_DENIED;
769         }
770
771         err = regdb_transaction_start();
772         if (!W_ERROR_IS_OK(err)) {
773                 DEBUG(0, ("reg_setvalue: Failed to start transaction: %s\n",
774                           win_errstr(err)));
775                 return err;
776         }
777
778         err = fill_value_cache(key);
779         if (!W_ERROR_IS_OK(err)) {
780                 DEBUG(0, ("reg_setvalue: Error filling value cache: %s\n", win_errstr(err)));
781                 goto done;
782         }
783
784         existing = regval_ctr_getvalue(key->values, name);
785
786         if ((existing != NULL) &&
787             (regval_size(existing) == val->data.length) &&
788             (memcmp(regval_data_p(existing), val->data.data,
789                     val->data.length) == 0))
790         {
791                 err = WERR_OK;
792                 goto done;
793         }
794
795         res = regval_ctr_addvalue(key->values, name, val->type,
796                                   val->data.data, val->data.length);
797
798         if (res == 0) {
799                 TALLOC_FREE(key->values);
800                 err = WERR_NOT_ENOUGH_MEMORY;
801                 goto done;
802         }
803
804         if (!store_reg_values(key->key, key->values)) {
805                 TALLOC_FREE(key->values);
806                 DEBUG(0, ("reg_setvalue: store_reg_values failed\n"));
807                 err = WERR_REGISTRY_IO_FAILED;
808                 goto done;
809         }
810
811         err = WERR_OK;
812
813 done:
814         if (W_ERROR_IS_OK(err)) {
815                 err = regdb_transaction_commit();
816                 if (!W_ERROR_IS_OK(err)) {
817                         DEBUG(0, ("reg_setvalue: Error committing transaction: %s\n", win_errstr(err)));
818                 }
819         } else {
820                 WERROR err1 = regdb_transaction_cancel();
821                 if (!W_ERROR_IS_OK(err1)) {
822                         DEBUG(0, ("reg_setvalue: Error cancelling transaction: %s\n", win_errstr(err1)));
823                 }
824         }
825
826         return err;
827 }
828
829 static WERROR reg_value_exists(struct registry_key *key, const char *name)
830 {
831         struct regval_blob *blob;
832
833         blob = regval_ctr_getvalue(key->values, name);
834
835         if (blob == NULL) {
836                 return WERR_FILE_NOT_FOUND;
837         } else {
838                 return WERR_OK;
839         }
840 }
841
842 WERROR reg_deletevalue(struct registry_key *key, const char *name)
843 {
844         WERROR err;
845
846         if (!(key->key->access_granted & KEY_SET_VALUE)) {
847                 return WERR_ACCESS_DENIED;
848         }
849
850         err = regdb_transaction_start();
851         if (!W_ERROR_IS_OK(err)) {
852                 DEBUG(0, ("reg_deletevalue: Failed to start transaction: %s\n",
853                           win_errstr(err)));
854                 return err;
855         }
856
857         err = fill_value_cache(key);
858         if (!W_ERROR_IS_OK(err)) {
859                 DEBUG(0, ("reg_deletevalue; Error filling value cache: %s\n",
860                           win_errstr(err)));
861                 goto done;
862         }
863
864         err = reg_value_exists(key, name);
865         if (!W_ERROR_IS_OK(err)) {
866                 goto done;
867         }
868
869         regval_ctr_delvalue(key->values, name);
870
871         if (!store_reg_values(key->key, key->values)) {
872                 TALLOC_FREE(key->values);
873                 err = WERR_REGISTRY_IO_FAILED;
874                 DEBUG(0, ("reg_deletevalue: store_reg_values failed\n"));
875                 goto done;
876         }
877
878         err = WERR_OK;
879
880 done:
881         if (W_ERROR_IS_OK(err)) {
882                 err = regdb_transaction_commit();
883                 if (!W_ERROR_IS_OK(err)) {
884                         DEBUG(0, ("reg_deletevalue: Error committing transaction: %s\n", win_errstr(err)));
885                 }
886         } else {
887                 WERROR err1 = regdb_transaction_cancel();
888                 if (!W_ERROR_IS_OK(err1)) {
889                         DEBUG(0, ("reg_deletevalue: Error cancelling transaction: %s\n", win_errstr(err1)));
890                 }
891         }
892
893         return err;
894 }
895
896 WERROR reg_getkeysecurity(TALLOC_CTX *mem_ctx, struct registry_key *key,
897                           struct security_descriptor **psecdesc)
898 {
899         return regkey_get_secdesc(mem_ctx, key->key, psecdesc);
900 }
901
902 WERROR reg_setkeysecurity(struct registry_key *key,
903                           struct security_descriptor *psecdesc)
904 {
905         return regkey_set_secdesc(key->key, psecdesc);
906 }
907
908 WERROR reg_getversion(uint32_t *version)
909 {
910         if (version == NULL) {
911                 return WERR_INVALID_PARAMETER;
912         }
913
914         *version = 0x00000005; /* Windows 2000 registry API version */
915         return WERR_OK;
916 }
917
918 /**********************************************************************
919  * Higher level utility functions
920  **********************************************************************/
921
922 WERROR reg_deleteallvalues(struct registry_key *key)
923 {
924         WERROR err;
925         int i;
926
927         if (!(key->key->access_granted & KEY_SET_VALUE)) {
928                 return WERR_ACCESS_DENIED;
929         }
930
931         if (!W_ERROR_IS_OK(err = fill_value_cache(key))) {
932                 return err;
933         }
934
935         for (i=0; i < regval_ctr_numvals(key->values); i++) {
936                 struct regval_blob *blob;
937                 blob = regval_ctr_specific_value(key->values, i);
938                 regval_ctr_delvalue(key->values, regval_name(blob));
939         }
940
941         if (!store_reg_values(key->key, key->values)) {
942                 TALLOC_FREE(key->values);
943                 return WERR_REGISTRY_IO_FAILED;
944         }
945
946         return WERR_OK;
947 }
948
949 /*
950  * Utility function to delete a registry key with all its subkeys.
951  * Note that reg_deletekey returns ACCESS_DENIED when called on a
952  * key that has subkeys.
953  */
954 static WERROR reg_deletekey_recursive_internal(struct registry_key *parent,
955                                                const char *path,
956                                                bool del_key, bool lazy)
957 {
958         WERROR werr = WERR_OK;
959         struct registry_key *key;
960         char *subkey_name = NULL;
961         uint32_t i;
962         TALLOC_CTX *mem_ctx = talloc_stackframe();
963
964         DEBUG(5, ("reg_deletekey_recursive_internal: deleting '%s' from '%s'\n",
965                   path, parent->key->name));
966
967         /* recurse through subkeys first */
968         werr = reg_openkey(mem_ctx, parent, path, REG_KEY_ALL, &key);
969         if (!W_ERROR_IS_OK(werr)) {
970                 DEBUG(3, ("reg_deletekey_recursive_internal: error opening "
971                           "subkey '%s' of '%s': '%s'\n",
972                           path, parent->key->name, win_errstr(werr)));
973                 goto done;
974         }
975
976         werr = fill_subkey_cache(key);
977         W_ERROR_NOT_OK_GOTO_DONE(werr);
978
979         /*
980          * loop from top to bottom for perfomance:
981          * this way, we need to rehash the regsubkey containers less
982          */
983         for (i = regsubkey_ctr_numkeys(key->subkeys) ; i > 0; i--) {
984                 subkey_name = regsubkey_ctr_specific_key(key->subkeys, i-1);
985                 werr = reg_deletekey_recursive_internal(key, subkey_name, true, del_key);
986                 W_ERROR_NOT_OK_GOTO_DONE(werr);
987         }
988
989         if (del_key) {
990                 /* now delete the actual key */
991                 werr = reg_deletekey_internal(mem_ctx, parent, path, lazy);
992         }
993
994 done:
995
996         DEBUG(5, ("reg_deletekey_recursive_internal: done deleting '%s' from "
997                   "'%s': %s\n",
998                   path, parent->key->name, win_errstr(werr)));
999         TALLOC_FREE(mem_ctx);
1000         return werr;
1001 }
1002
1003 static WERROR reg_deletekey_recursive_trans(struct registry_key *parent,
1004                                             const char *path,
1005                                             bool del_key)
1006 {
1007         WERROR werr;
1008
1009         werr = regdb_transaction_start();
1010         if (!W_ERROR_IS_OK(werr)) {
1011                 DEBUG(0, ("reg_deletekey_recursive_trans: "
1012                           "error starting transaction: %s\n",
1013                           win_errstr(werr)));
1014                 return werr;
1015         }
1016
1017         werr = reg_deletekey_recursive_internal(parent, path, del_key, false);
1018
1019         if (!W_ERROR_IS_OK(werr)) {
1020                 WERROR werr2;
1021                 DEBUG(W_ERROR_EQUAL(werr, WERR_FILE_NOT_FOUND) ? 5 : 1,
1022                       (__location__ ": failed to delete key '%s' from key "
1023                        "'%s': %s\n", path, parent->key->name,
1024                        win_errstr(werr)));
1025
1026                 werr2 = regdb_transaction_cancel();
1027                 if (!W_ERROR_IS_OK(werr2)) {
1028                         DEBUG(0, ("reg_deletekey_recursive_trans: "
1029                                   "error cancelling transaction: %s\n",
1030                                   win_errstr(werr2)));
1031                         /*
1032                          * return the original werr or the
1033                          * error from cancelling the transaction?
1034                          */
1035                 }
1036         } else {
1037                 werr = regdb_transaction_commit();
1038                 if (!W_ERROR_IS_OK(werr)) {
1039                         DEBUG(0, ("reg_deletekey_recursive_trans: "
1040                                   "error committing transaction: %s\n",
1041                                   win_errstr(werr)));
1042                 } else {
1043                         DEBUG(5, ("reg_deletekey_recursive_trans: deleted key '%s' from '%s'\n",
1044                                   path, parent->key->name));
1045
1046                 }
1047         }
1048
1049         return werr;
1050 }
1051
1052 WERROR reg_deletekey_recursive(struct registry_key *parent,
1053                                const char *path)
1054 {
1055         return reg_deletekey_recursive_trans(parent, path, true);
1056 }
1057
1058 WERROR reg_deletesubkeys_recursive(struct registry_key *parent,
1059                                    const char *path)
1060 {
1061         return reg_deletekey_recursive_trans(parent, path, false);
1062 }
1063