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