2e40f2c5046044ac1892db6b94eb04fb4533fa04
[amitay/samba.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_cachehook.h"
68 #include "reg_util_internal.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
74 #undef DBGC_CLASS
75 #define DBGC_CLASS DBGC_REGISTRY
76
77
78 /**********************************************************************
79  * Helper functions
80  **********************************************************************/
81
82 static WERROR fill_value_cache(struct registry_key *key)
83 {
84         WERROR werr;
85
86         if (key->values != NULL) {
87                 if (!reg_values_need_update(key->key, key->values)) {
88                         return WERR_OK;
89                 }
90         }
91
92         werr = regval_ctr_init(key, &(key->values));
93         W_ERROR_NOT_OK_RETURN(werr);
94
95         if (fetch_reg_values(key->key, key->values) == -1) {
96                 TALLOC_FREE(key->values);
97                 return WERR_BADFILE;
98         }
99
100         return WERR_OK;
101 }
102
103 static WERROR fill_subkey_cache(struct registry_key *key)
104 {
105         WERROR werr;
106
107         if (key->subkeys != NULL) {
108                 if (!reg_subkeys_need_update(key->key, key->subkeys)) {
109                         return WERR_OK;
110                 }
111         }
112
113         werr = regsubkey_ctr_init(key, &(key->subkeys));
114         W_ERROR_NOT_OK_RETURN(werr);
115
116         if (fetch_reg_keys(key->key, key->subkeys) == -1) {
117                 TALLOC_FREE(key->subkeys);
118                 return WERR_NO_MORE_ITEMS;
119         }
120
121         return WERR_OK;
122 }
123
124 static int regkey_destructor(struct registry_key_handle *key)
125 {
126         return regdb_close();
127 }
128
129 static WERROR regkey_open_onelevel(TALLOC_CTX *mem_ctx, 
130                                    struct registry_key *parent,
131                                    const char *name,
132                                    const struct security_token *token,
133                                    uint32 access_desired,
134                                    struct registry_key **pregkey)
135 {
136         WERROR          result = WERR_OK;
137         struct registry_key *regkey;
138         struct registry_key_handle *key;
139         struct regsubkey_ctr    *subkeys = NULL;
140
141         DEBUG(7,("regkey_open_onelevel: name = [%s]\n", name));
142
143         SMB_ASSERT(strchr(name, '\\') == NULL);
144
145         if (!(regkey = TALLOC_ZERO_P(mem_ctx, struct registry_key)) ||
146             !(regkey->token = dup_nt_token(regkey, token)) ||
147             !(regkey->key = TALLOC_ZERO_P(regkey, struct registry_key_handle)))
148         {
149                 result = WERR_NOMEM;
150                 goto done;
151         }
152
153         if ( !(W_ERROR_IS_OK(result = regdb_open())) ) {
154                 goto done;
155         }
156
157         key = regkey->key;
158         talloc_set_destructor(key, regkey_destructor);
159
160         /* initialization */
161
162         key->type = REG_KEY_GENERIC;
163
164         if (name[0] == '\0') {
165                 /*
166                  * Open a copy of the parent key
167                  */
168                 if (!parent) {
169                         result = WERR_BADFILE;
170                         goto done;
171                 }
172                 key->name = talloc_strdup(key, parent->key->name);
173         }
174         else {
175                 /*
176                  * Normal subkey open
177                  */
178                 key->name = talloc_asprintf(key, "%s%s%s",
179                                             parent ? parent->key->name : "",
180                                             parent ? "\\": "",
181                                             name);
182         }
183
184         if (key->name == NULL) {
185                 result = WERR_NOMEM;
186                 goto done;
187         }
188
189         /* Tag this as a Performance Counter Key */
190
191         if( StrnCaseCmp(key->name, KEY_HKPD, strlen(KEY_HKPD)) == 0 )
192                 key->type = REG_KEY_HKPD;
193
194         /* Look up the table of registry I/O operations */
195
196         if ( !(key->ops = reghook_cache_find( key->name )) ) {
197                 DEBUG(0,("reg_open_onelevel: Failed to assign "
198                          "registry_ops to [%s]\n", key->name ));
199                 result = WERR_BADFILE;
200                 goto done;
201         }
202
203         /* check if the path really exists; failed is indicated by -1 */
204         /* if the subkey count failed, bail out */
205
206         result = regsubkey_ctr_init(key, &subkeys);
207         if (!W_ERROR_IS_OK(result)) {
208                 goto done;
209         }
210
211         if ( fetch_reg_keys( key, subkeys ) == -1 )  {
212                 result = WERR_BADFILE;
213                 goto done;
214         }
215
216         TALLOC_FREE( subkeys );
217
218         if ( !regkey_access_check( key, access_desired, &key->access_granted,
219                                    token ) ) {
220                 result = WERR_ACCESS_DENIED;
221                 goto done;
222         }
223
224         *pregkey = regkey;
225         result = WERR_OK;
226
227 done:
228         if ( !W_ERROR_IS_OK(result) ) {
229                 TALLOC_FREE(regkey);
230         }
231
232         return result;
233 }
234
235 WERROR reg_openhive(TALLOC_CTX *mem_ctx, const char *hive,
236                     uint32 desired_access,
237                     const struct security_token *token,
238                     struct registry_key **pkey)
239 {
240         SMB_ASSERT(hive != NULL);
241         SMB_ASSERT(hive[0] != '\0');
242         SMB_ASSERT(strchr(hive, '\\') == NULL);
243
244         return regkey_open_onelevel(mem_ctx, NULL, hive, token, desired_access,
245                                     pkey);
246 }
247
248
249 /**********************************************************************
250  * The API functions
251  **********************************************************************/
252
253 WERROR reg_openkey(TALLOC_CTX *mem_ctx, struct registry_key *parent,
254                    const char *name, uint32 desired_access,
255                    struct registry_key **pkey)
256 {
257         struct registry_key *direct_parent = parent;
258         WERROR err;
259         char *p, *path, *to_free;
260         size_t len;
261
262         if (!(path = SMB_STRDUP(name))) {
263                 return WERR_NOMEM;
264         }
265         to_free = path;
266
267         len = strlen(path);
268
269         if ((len > 0) && (path[len-1] == '\\')) {
270                 path[len-1] = '\0';
271         }
272
273         while ((p = strchr(path, '\\')) != NULL) {
274                 char *name_component;
275                 struct registry_key *tmp;
276
277                 if (!(name_component = SMB_STRNDUP(path, (p - path)))) {
278                         err = WERR_NOMEM;
279                         goto error;
280                 }
281
282                 err = regkey_open_onelevel(mem_ctx, direct_parent,
283                                            name_component, parent->token,
284                                            KEY_ENUMERATE_SUB_KEYS, &tmp);
285                 SAFE_FREE(name_component);
286
287                 if (!W_ERROR_IS_OK(err)) {
288                         goto error;
289                 }
290                 if (direct_parent != parent) {
291                         TALLOC_FREE(direct_parent);
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  error:
301         if (direct_parent != parent) {
302                 TALLOC_FREE(direct_parent);
303         }
304         SAFE_FREE(to_free);
305         return err;
306 }
307
308 WERROR reg_enumkey(TALLOC_CTX *mem_ctx, struct registry_key *key,
309                    uint32 idx, char **name, NTTIME *last_write_time)
310 {
311         WERROR err;
312
313         if (!(key->key->access_granted & KEY_ENUMERATE_SUB_KEYS)) {
314                 return WERR_ACCESS_DENIED;
315         }
316
317         if (!W_ERROR_IS_OK(err = fill_subkey_cache(key))) {
318                 return err;
319         }
320
321         if (idx >= regsubkey_ctr_numkeys(key->subkeys)) {
322                 return WERR_NO_MORE_ITEMS;
323         }
324
325         if (!(*name = talloc_strdup(mem_ctx,
326                         regsubkey_ctr_specific_key(key->subkeys, idx))))
327         {
328                 return WERR_NOMEM;
329         }
330
331         if (last_write_time) {
332                 *last_write_time = 0;
333         }
334
335         return WERR_OK;
336 }
337
338 WERROR reg_enumvalue(TALLOC_CTX *mem_ctx, struct registry_key *key,
339                      uint32 idx, char **pname, struct registry_value **pval)
340 {
341         struct registry_value *val;
342         struct regval_blob *blob;
343         WERROR err;
344
345         if (!(key->key->access_granted & KEY_QUERY_VALUE)) {
346                 return WERR_ACCESS_DENIED;
347         }
348
349         if (!(W_ERROR_IS_OK(err = fill_value_cache(key)))) {
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_NOMEM;
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_NOMEM;
372         }
373
374         *pval = val;
375         return WERR_OK;
376 }
377
378 WERROR reg_queryvalue(TALLOC_CTX *mem_ctx, struct registry_key *key,
379                       const char *name, struct registry_value **pval)
380 {
381         WERROR err;
382         uint32 i;
383
384         if (!(key->key->access_granted & KEY_QUERY_VALUE)) {
385                 return WERR_ACCESS_DENIED;
386         }
387
388         if (!(W_ERROR_IS_OK(err = fill_value_cache(key)))) {
389                 return err;
390         }
391
392         for (i=0; i < regval_ctr_numvals(key->values); i++) {
393                 struct regval_blob *blob;
394                 blob = regval_ctr_specific_value(key->values, i);
395                 if (strequal(regval_name(blob), name)) {
396                         return reg_enumvalue(mem_ctx, key, i, NULL, pval);
397                 }
398         }
399
400         return WERR_BADFILE;
401 }
402
403 WERROR reg_querymultiplevalues(TALLOC_CTX *mem_ctx,
404                                struct registry_key *key,
405                                uint32_t num_names,
406                                const char **names,
407                                uint32_t *pnum_vals,
408                                struct registry_value **pvals)
409 {
410         WERROR err;
411         uint32_t i, n, found = 0;
412         struct registry_value *vals;
413
414         if (num_names == 0) {
415                 return WERR_OK;
416         }
417
418         if (!(key->key->access_granted & KEY_QUERY_VALUE)) {
419                 return WERR_ACCESS_DENIED;
420         }
421
422         if (!(W_ERROR_IS_OK(err = fill_value_cache(key)))) {
423                 return err;
424         }
425
426         vals = talloc_zero_array(mem_ctx, struct registry_value, num_names);
427         if (vals == NULL) {
428                 return WERR_NOMEM;
429         }
430
431         for (n=0; n < num_names; n++) {
432                 for (i=0; i < regval_ctr_numvals(key->values); i++) {
433                         struct regval_blob *blob;
434                         blob = regval_ctr_specific_value(key->values, i);
435                         if (strequal(regval_name(blob), names[n])) {
436                                 struct registry_value *v;
437                                 err = reg_enumvalue(mem_ctx, key, i, NULL, &v);
438                                 if (!W_ERROR_IS_OK(err)) {
439                                         return err;
440                                 }
441                                 vals[n] = *v;
442                                 found++;
443                         }
444                 }
445         }
446
447         *pvals = vals;
448         *pnum_vals = found;
449
450         return WERR_OK;
451 }
452
453 WERROR reg_queryinfokey(struct registry_key *key, uint32_t *num_subkeys,
454                         uint32_t *max_subkeylen, uint32_t *max_subkeysize, 
455                         uint32_t *num_values, uint32_t *max_valnamelen, 
456                         uint32_t *max_valbufsize, uint32_t *secdescsize,
457                         NTTIME *last_changed_time)
458 {
459         uint32 i, max_size;
460         size_t max_len;
461         TALLOC_CTX *mem_ctx;
462         WERROR err;
463         struct security_descriptor *secdesc;
464
465         if (!(key->key->access_granted & KEY_QUERY_VALUE)) {
466                 return WERR_ACCESS_DENIED;
467         }
468
469         if (!W_ERROR_IS_OK(fill_subkey_cache(key)) ||
470             !W_ERROR_IS_OK(fill_value_cache(key))) {
471                 return WERR_BADFILE;
472         }
473
474         max_len = 0;
475         for (i=0; i< regsubkey_ctr_numkeys(key->subkeys); i++) {
476                 max_len = MAX(max_len,
477                         strlen(regsubkey_ctr_specific_key(key->subkeys, i)));
478         }
479
480         *num_subkeys = regsubkey_ctr_numkeys(key->subkeys);
481         *max_subkeylen = max_len;
482         *max_subkeysize = 0;    /* Class length? */
483
484         max_len = 0;
485         max_size = 0;
486         for (i=0; i < regval_ctr_numvals(key->values); i++) {
487                 struct regval_blob *blob;
488                 blob = regval_ctr_specific_value(key->values, i);
489                 max_len = MAX(max_len, strlen(regval_name(blob)));
490                 max_size = MAX(max_size, regval_size(blob));
491         }
492
493         *num_values = regval_ctr_numvals(key->values);
494         *max_valnamelen = max_len;
495         *max_valbufsize = max_size;
496
497         if (!(mem_ctx = talloc_new(key))) {
498                 return WERR_NOMEM;
499         }
500
501         err = regkey_get_secdesc(mem_ctx, key->key, &secdesc);
502         if (!W_ERROR_IS_OK(err)) {
503                 TALLOC_FREE(mem_ctx);
504                 return err;
505         }
506
507         *secdescsize = ndr_size_security_descriptor(secdesc, 0);
508         TALLOC_FREE(mem_ctx);
509
510         *last_changed_time = 0;
511
512         return WERR_OK;
513 }
514
515 WERROR reg_createkey(TALLOC_CTX *ctx, struct registry_key *parent,
516                      const char *subkeypath, uint32 desired_access,
517                      struct registry_key **pkey,
518                      enum winreg_CreateAction *paction)
519 {
520         struct registry_key *key = parent;
521         struct registry_key *create_parent;
522         TALLOC_CTX *mem_ctx;
523         char *path, *end;
524         WERROR err;
525
526         if (!(mem_ctx = talloc_new(ctx))) return WERR_NOMEM;
527
528         if (!(path = talloc_strdup(mem_ctx, subkeypath))) {
529                 err = WERR_NOMEM;
530                 goto done;
531         }
532
533         while ((end = strchr(path, '\\')) != NULL) {
534                 struct registry_key *tmp;
535                 enum winreg_CreateAction action;
536
537                 *end = '\0';
538
539                 err = reg_createkey(mem_ctx, key, path,
540                                     KEY_ENUMERATE_SUB_KEYS, &tmp, &action);
541                 if (!W_ERROR_IS_OK(err)) {
542                         goto done;
543                 }
544
545                 if (key != parent) {
546                         TALLOC_FREE(key);
547                 }
548
549                 key = tmp;
550                 path = end+1;
551         }
552
553         /*
554          * At this point, "path" contains the one-element subkey of "key". We
555          * can try to open it.
556          */
557
558         err = reg_openkey(ctx, key, path, desired_access, pkey);
559         if (W_ERROR_IS_OK(err)) {
560                 if (paction != NULL) {
561                         *paction = REG_OPENED_EXISTING_KEY;
562                 }
563                 goto done;
564         }
565
566         if (!W_ERROR_EQUAL(err, WERR_BADFILE)) {
567                 /*
568                  * Something but "notfound" has happened, so bail out
569                  */
570                 goto done;
571         }
572
573         /*
574          * We have to make a copy of the current key, as we opened it only
575          * with ENUM_SUBKEY access.
576          */
577
578         err = reg_openkey(mem_ctx, key, "", KEY_CREATE_SUB_KEY,
579                           &create_parent);
580         if (!W_ERROR_IS_OK(err)) {
581                 goto done;
582         }
583
584         /*
585          * Actually create the subkey
586          */
587
588         err = fill_subkey_cache(create_parent);
589         if (!W_ERROR_IS_OK(err)) goto done;
590
591         err = create_reg_subkey(key->key, path);
592         W_ERROR_NOT_OK_GOTO_DONE(err);
593
594         /*
595          * Now open the newly created key
596          */
597
598         err = reg_openkey(ctx, create_parent, path, desired_access, pkey);
599         if (W_ERROR_IS_OK(err) && (paction != NULL)) {
600                 *paction = REG_CREATED_NEW_KEY;
601         }
602
603  done:
604         TALLOC_FREE(mem_ctx);
605         return err;
606 }
607
608 WERROR reg_deletekey(struct registry_key *parent, const char *path)
609 {
610         WERROR err;
611         char *name, *end;
612         struct registry_key *tmp_key, *key;
613         TALLOC_CTX *mem_ctx = talloc_stackframe();
614
615         name = talloc_strdup(mem_ctx, path);
616         if (name == NULL) {
617                 err = WERR_NOMEM;
618                 goto done;
619         }
620
621         /* check if the key has subkeys */
622         err = reg_openkey(mem_ctx, parent, name, REG_KEY_READ, &key);
623         W_ERROR_NOT_OK_GOTO_DONE(err);
624
625         err = fill_subkey_cache(key);
626         W_ERROR_NOT_OK_GOTO_DONE(err);
627
628         if (regsubkey_ctr_numkeys(key->subkeys) > 0) {
629                 err = WERR_ACCESS_DENIED;
630                 goto done;
631         }
632
633         /* no subkeys - proceed with delete */
634         end = strrchr(name, '\\');
635         if (end != NULL) {
636                 *end = '\0';
637
638                 err = reg_openkey(mem_ctx, parent, name,
639                                   KEY_CREATE_SUB_KEY, &tmp_key);
640                 W_ERROR_NOT_OK_GOTO_DONE(err);
641
642                 parent = tmp_key;
643                 name = end+1;
644         }
645
646         if (name[0] == '\0') {
647                 err = WERR_INVALID_PARAM;
648                 goto done;
649         }
650
651         err = delete_reg_subkey(parent->key, name);
652
653 done:
654         TALLOC_FREE(mem_ctx);
655         return err;
656 }
657
658 WERROR reg_setvalue(struct registry_key *key, const char *name,
659                     const struct registry_value *val)
660 {
661         WERROR err;
662         int res;
663
664         if (!(key->key->access_granted & KEY_SET_VALUE)) {
665                 return WERR_ACCESS_DENIED;
666         }
667
668         if (!W_ERROR_IS_OK(err = fill_value_cache(key))) {
669                 return err;
670         }
671
672         res = regval_ctr_addvalue(key->values, name, val->type,
673                                   val->data.data, val->data.length);
674
675         if (res == 0) {
676                 TALLOC_FREE(key->values);
677                 return WERR_NOMEM;
678         }
679
680         if (!store_reg_values(key->key, key->values)) {
681                 TALLOC_FREE(key->values);
682                 return WERR_REG_IO_FAILURE;
683         }
684
685         return WERR_OK;
686 }
687
688 static WERROR reg_value_exists(struct registry_key *key, const char *name)
689 {
690         struct regval_blob *blob;
691
692         blob = regval_ctr_getvalue(key->values, name);
693
694         if (blob == NULL) {
695                 return WERR_BADFILE;
696         } else {
697                 return WERR_OK;
698         }
699 }
700
701 WERROR reg_deletevalue(struct registry_key *key, const char *name)
702 {
703         WERROR err;
704
705         if (!(key->key->access_granted & KEY_SET_VALUE)) {
706                 return WERR_ACCESS_DENIED;
707         }
708
709         if (!W_ERROR_IS_OK(err = fill_value_cache(key))) {
710                 return err;
711         }
712
713         err = reg_value_exists(key, name);
714         if (!W_ERROR_IS_OK(err)) {
715                 return err;
716         }
717
718         regval_ctr_delvalue(key->values, name);
719
720         if (!store_reg_values(key->key, key->values)) {
721                 TALLOC_FREE(key->values);
722                 return WERR_REG_IO_FAILURE;
723         }
724
725         return WERR_OK;
726 }
727
728 WERROR reg_getkeysecurity(TALLOC_CTX *mem_ctx, struct registry_key *key,
729                           struct security_descriptor **psecdesc)
730 {
731         return regkey_get_secdesc(mem_ctx, key->key, psecdesc);
732 }
733
734 WERROR reg_setkeysecurity(struct registry_key *key,
735                           struct security_descriptor *psecdesc)
736 {
737         return regkey_set_secdesc(key->key, psecdesc);
738 }
739
740 WERROR reg_getversion(uint32_t *version)
741 {
742         if (version == NULL) {
743                 return WERR_INVALID_PARAM;
744         }
745
746         *version = 0x00000005; /* Windows 2000 registry API version */
747         return WERR_OK;
748 }
749
750 /**********************************************************************
751  * Higher level utility functions
752  **********************************************************************/
753
754 WERROR reg_deleteallvalues(struct registry_key *key)
755 {
756         WERROR err;
757         int i;
758
759         if (!(key->key->access_granted & KEY_SET_VALUE)) {
760                 return WERR_ACCESS_DENIED;
761         }
762
763         if (!W_ERROR_IS_OK(err = fill_value_cache(key))) {
764                 return err;
765         }
766
767         for (i=0; i < regval_ctr_numvals(key->values); i++) {
768                 struct regval_blob *blob;
769                 blob = regval_ctr_specific_value(key->values, i);
770                 regval_ctr_delvalue(key->values, regval_name(blob));
771         }
772
773         if (!store_reg_values(key->key, key->values)) {
774                 TALLOC_FREE(key->values);
775                 return WERR_REG_IO_FAILURE;
776         }
777
778         return WERR_OK;
779 }
780
781 /*
782  * Utility function to delete a registry key with all its subkeys.
783  * Note that reg_deletekey returns ACCESS_DENIED when called on a
784  * key that has subkeys.
785  */
786 static WERROR reg_deletekey_recursive_internal(TALLOC_CTX *ctx,
787                                                struct registry_key *parent,
788                                                const char *path,
789                                                bool del_key)
790 {
791         TALLOC_CTX *mem_ctx = NULL;
792         WERROR werr = WERR_OK;
793         struct registry_key *key;
794         char *subkey_name = NULL;
795         uint32 i;
796
797         mem_ctx = talloc_new(ctx);
798         if (mem_ctx == NULL) {
799                 werr = WERR_NOMEM;
800                 goto done;
801         }
802
803         /* recurse through subkeys first */
804         werr = reg_openkey(mem_ctx, parent, path, REG_KEY_ALL, &key);
805         if (!W_ERROR_IS_OK(werr)) {
806                 goto done;
807         }
808
809         werr = fill_subkey_cache(key);
810         W_ERROR_NOT_OK_GOTO_DONE(werr);
811
812         /*
813          * loop from top to bottom for perfomance:
814          * this way, we need to rehash the regsubkey containers less
815          */
816         for (i = regsubkey_ctr_numkeys(key->subkeys) ; i > 0; i--) {
817                 subkey_name = regsubkey_ctr_specific_key(key->subkeys, i-1);
818                 werr = reg_deletekey_recursive_internal(mem_ctx, key,
819                                         subkey_name,
820                                         true);
821                 W_ERROR_NOT_OK_GOTO_DONE(werr);
822         }
823
824         if (del_key) {
825                 /* now delete the actual key */
826                 werr = reg_deletekey(parent, path);
827         }
828
829 done:
830         TALLOC_FREE(mem_ctx);
831         return werr;
832 }
833
834 static WERROR reg_deletekey_recursive_trans(TALLOC_CTX *ctx,
835                                             struct registry_key *parent,
836                                             const char *path,
837                                             bool del_key)
838 {
839         WERROR werr;
840
841         werr = regdb_transaction_start();
842         if (!W_ERROR_IS_OK(werr)) {
843                 DEBUG(0, ("reg_deletekey_recursive_trans: "
844                           "error starting transaction: %s\n",
845                           win_errstr(werr)));
846                 return werr;
847         }
848
849         werr = reg_deletekey_recursive_internal(ctx, parent, path, del_key);
850
851         if (!W_ERROR_IS_OK(werr)) {
852                 DEBUG(1, (__location__ " failed to delete key '%s' from key "
853                           "'%s': %s\n", path, parent->key->name,
854                           win_errstr(werr)));
855                 werr = regdb_transaction_cancel();
856                 if (!W_ERROR_IS_OK(werr)) {
857                         DEBUG(0, ("reg_deletekey_recursive_trans: "
858                                   "error cancelling transaction: %s\n",
859                                   win_errstr(werr)));
860                 }
861         } else {
862                 werr = regdb_transaction_commit();
863                 if (!W_ERROR_IS_OK(werr)) {
864                         DEBUG(0, ("reg_deletekey_recursive_trans: "
865                                   "error committing transaction: %s\n",
866                                   win_errstr(werr)));
867                 }
868         }
869
870         return werr;
871 }
872
873 WERROR reg_deletekey_recursive(TALLOC_CTX *ctx,
874                                struct registry_key *parent,
875                                const char *path)
876 {
877         return reg_deletekey_recursive_trans(ctx, parent, path, true);
878 }
879
880 WERROR reg_deletesubkeys_recursive(TALLOC_CTX *ctx,
881                                    struct registry_key *parent,
882                                    const char *path)
883 {
884         return reg_deletekey_recursive_trans(ctx, parent, path, false);
885 }
886