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