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