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