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