s3:registry: convert reg_openkey() to use talloc instead of SMB_STRDUP etc (cherry...
[ddiss/samba.git] / source3 / registry / reg_api.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  Virtual Windows Registry Layer
4  *  Copyright (C) Volker Lendecke 2006
5  *  Copyright (C) Michael Adam 2007-2010
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
19  */
20
21 /* Attempt to wrap the existing API in a more winreg.idl-like way */
22
23 /*
24  * Here is a list of winreg.idl functions and corresponding implementations
25  * provided here:
26  *
27  * 0x00         winreg_OpenHKCR
28  * 0x01         winreg_OpenHKCU
29  * 0x02         winreg_OpenHKLM
30  * 0x03         winreg_OpenHKPD
31  * 0x04         winreg_OpenHKU
32  * 0x05         winreg_CloseKey
33  * 0x06         winreg_CreateKey                        reg_createkey
34  * 0x07         winreg_DeleteKey                        reg_deletekey
35  * 0x08         winreg_DeleteValue                      reg_deletevalue
36  * 0x09         winreg_EnumKey                          reg_enumkey
37  * 0x0a         winreg_EnumValue                        reg_enumvalue
38  * 0x0b         winreg_FlushKey
39  * 0x0c         winreg_GetKeySecurity                   reg_getkeysecurity
40  * 0x0d         winreg_LoadKey
41  * 0x0e         winreg_NotifyChangeKeyValue
42  * 0x0f         winreg_OpenKey                          reg_openkey
43  * 0x10         winreg_QueryInfoKey                     reg_queryinfokey
44  * 0x11         winreg_QueryValue                       reg_queryvalue
45  * 0x12         winreg_ReplaceKey
46  * 0x13         winreg_RestoreKey                       reg_restorekey
47  * 0x14         winreg_SaveKey                          reg_savekey
48  * 0x15         winreg_SetKeySecurity                   reg_setkeysecurity
49  * 0x16         winreg_SetValue                         reg_setvalue
50  * 0x17         winreg_UnLoadKey
51  * 0x18         winreg_InitiateSystemShutdown
52  * 0x19         winreg_AbortSystemShutdown
53  * 0x1a         winreg_GetVersion                       reg_getversion
54  * 0x1b         winreg_OpenHKCC
55  * 0x1c         winreg_OpenHKDD
56  * 0x1d         winreg_QueryMultipleValues              reg_querymultiplevalues
57  * 0x1e         winreg_InitiateSystemShutdownEx
58  * 0x1f         winreg_SaveKeyEx
59  * 0x20         winreg_OpenHKPT
60  * 0x21         winreg_OpenHKPN
61  * 0x22         winreg_QueryMultipleValues2             reg_querymultiplevalues
62  *
63  */
64
65 #include "includes.h"
66 #include "registry.h"
67 #include "reg_api.h"
68 #include "reg_cachehook.h"
69 #include "reg_backend_db.h"
70 #include "reg_dispatcher.h"
71 #include "reg_objects.h"
72 #include "../librpc/gen_ndr/ndr_security.h"
73
74 #undef DBGC_CLASS
75 #define DBGC_CLASS DBGC_REGISTRY
76
77
78 /**********************************************************************
79  * Helper functions
80  **********************************************************************/
81
82 static WERROR fill_value_cache(struct registry_key *key)
83 {
84         WERROR werr;
85
86         if (key->values != NULL) {
87                 if (!reg_values_need_update(key->key, key->values)) {
88                         return WERR_OK;
89                 }
90         }
91
92         TALLOC_FREE(key->values);
93         werr = regval_ctr_init(key, &(key->values));
94         W_ERROR_NOT_OK_RETURN(werr);
95
96         if (fetch_reg_values(key->key, key->values) == -1) {
97                 TALLOC_FREE(key->values);
98                 return WERR_BADFILE;
99         }
100
101         return WERR_OK;
102 }
103
104 static WERROR fill_subkey_cache(struct registry_key *key)
105 {
106         WERROR werr;
107
108         if (key->subkeys != NULL) {
109                 if (!reg_subkeys_need_update(key->key, key->subkeys)) {
110                         return WERR_OK;
111                 }
112         }
113
114         werr = regsubkey_ctr_init(key, &(key->subkeys));
115         W_ERROR_NOT_OK_RETURN(werr);
116
117         if (fetch_reg_keys(key->key, key->subkeys) == -1) {
118                 TALLOC_FREE(key->subkeys);
119                 return WERR_NO_MORE_ITEMS;
120         }
121
122         return WERR_OK;
123 }
124
125 static int regkey_destructor(struct registry_key_handle *key)
126 {
127         return regdb_close();
128 }
129
130 static WERROR regkey_open_onelevel(TALLOC_CTX *mem_ctx, 
131                                    struct registry_key *parent,
132                                    const char *name,
133                                    const struct security_token *token,
134                                    uint32 access_desired,
135                                    struct registry_key **pregkey)
136 {
137         WERROR          result = WERR_OK;
138         struct registry_key *regkey;
139         struct registry_key_handle *key;
140         struct regsubkey_ctr    *subkeys = NULL;
141
142         DEBUG(7,("regkey_open_onelevel: name = [%s]\n", name));
143
144         SMB_ASSERT(strchr(name, '\\') == NULL);
145
146         if (!(regkey = TALLOC_ZERO_P(mem_ctx, struct registry_key)) ||
147             !(regkey->token = dup_nt_token(regkey, token)) ||
148             !(regkey->key = TALLOC_ZERO_P(regkey, struct registry_key_handle)))
149         {
150                 result = WERR_NOMEM;
151                 goto done;
152         }
153
154         result = regdb_open();
155         if (!(W_ERROR_IS_OK(result))) {
156                 goto done;
157         }
158
159         key = regkey->key;
160         talloc_set_destructor(key, regkey_destructor);
161
162         /* initialization */
163
164         key->type = REG_KEY_GENERIC;
165
166         if (name[0] == '\0') {
167                 /*
168                  * Open a copy of the parent key
169                  */
170                 if (!parent) {
171                         result = WERR_BADFILE;
172                         goto done;
173                 }
174                 key->name = talloc_strdup(key, parent->key->name);
175         }
176         else {
177                 /*
178                  * Normal subkey open
179                  */
180                 key->name = talloc_asprintf(key, "%s%s%s",
181                                             parent ? parent->key->name : "",
182                                             parent ? "\\": "",
183                                             name);
184         }
185
186         if (key->name == NULL) {
187                 result = WERR_NOMEM;
188                 goto done;
189         }
190
191         /* Tag this as a Performance Counter Key */
192
193         if( StrnCaseCmp(key->name, KEY_HKPD, strlen(KEY_HKPD)) == 0 )
194                 key->type = REG_KEY_HKPD;
195
196         /* Look up the table of registry I/O operations */
197
198         key->ops = reghook_cache_find( key->name );
199         if (key->ops == NULL) {
200                 DEBUG(0,("reg_open_onelevel: Failed to assign "
201                          "registry_ops to [%s]\n", key->name ));
202                 result = WERR_BADFILE;
203                 goto done;
204         }
205
206         /* check if the path really exists; failed is indicated by -1 */
207         /* if the subkey count failed, bail out */
208
209         result = regsubkey_ctr_init(key, &subkeys);
210         if (!W_ERROR_IS_OK(result)) {
211                 goto done;
212         }
213
214         if ( fetch_reg_keys( key, subkeys ) == -1 )  {
215                 result = WERR_BADFILE;
216                 goto done;
217         }
218
219         TALLOC_FREE( subkeys );
220
221         if ( !regkey_access_check( key, access_desired, &key->access_granted,
222                                    token ) ) {
223                 result = WERR_ACCESS_DENIED;
224                 goto done;
225         }
226
227         *pregkey = regkey;
228         result = WERR_OK;
229
230 done:
231         if ( !W_ERROR_IS_OK(result) ) {
232                 TALLOC_FREE(regkey);
233         }
234
235         return result;
236 }
237
238 WERROR reg_openhive(TALLOC_CTX *mem_ctx, const char *hive,
239                     uint32 desired_access,
240                     const struct security_token *token,
241                     struct registry_key **pkey)
242 {
243         SMB_ASSERT(hive != NULL);
244         SMB_ASSERT(hive[0] != '\0');
245         SMB_ASSERT(strchr(hive, '\\') == NULL);
246
247         return regkey_open_onelevel(mem_ctx, NULL, hive, token, desired_access,
248                                     pkey);
249 }
250
251
252 /**********************************************************************
253  * The API functions
254  **********************************************************************/
255
256 WERROR reg_openkey(TALLOC_CTX *mem_ctx, struct registry_key *parent,
257                    const char *name, uint32 desired_access,
258                    struct registry_key **pkey)
259 {
260         struct registry_key *direct_parent = parent;
261         WERROR err;
262         char *p, *path;
263         size_t len;
264         TALLOC_CTX *frame = talloc_stackframe();
265
266         path = talloc_strdup(frame, name);
267         if (path == NULL) {
268                 err = WERR_NOMEM;
269                 goto error;
270         }
271
272         len = strlen(path);
273
274         if ((len > 0) && (path[len-1] == '\\')) {
275                 path[len-1] = '\0';
276         }
277
278         while ((p = strchr(path, '\\')) != NULL) {
279                 char *name_component;
280                 struct registry_key *tmp;
281
282                 name_component = talloc_strndup(frame, path, (p - path));
283                 if (name_component == NULL) {
284                         err = WERR_NOMEM;
285                         goto error;
286                 }
287
288                 err = regkey_open_onelevel(frame, direct_parent,
289                                            name_component, parent->token,
290                                            KEY_ENUMERATE_SUB_KEYS, &tmp);
291
292                 if (!W_ERROR_IS_OK(err)) {
293                         goto error;
294                 }
295
296                 direct_parent = tmp;
297                 path = p+1;
298         }
299
300         err = regkey_open_onelevel(mem_ctx, direct_parent, path, parent->token,
301                                    desired_access, pkey);
302
303 error:
304         talloc_free(frame);
305         return err;
306 }
307
308 WERROR reg_enumkey(TALLOC_CTX *mem_ctx, struct registry_key *key,
309                    uint32 idx, char **name, NTTIME *last_write_time)
310 {
311         WERROR err;
312
313         if (!(key->key->access_granted & KEY_ENUMERATE_SUB_KEYS)) {
314                 return WERR_ACCESS_DENIED;
315         }
316
317         if (!W_ERROR_IS_OK(err = fill_subkey_cache(key))) {
318                 return err;
319         }
320
321         if (idx >= regsubkey_ctr_numkeys(key->subkeys)) {
322                 return WERR_NO_MORE_ITEMS;
323         }
324
325         if (!(*name = talloc_strdup(mem_ctx,
326                         regsubkey_ctr_specific_key(key->subkeys, idx))))
327         {
328                 return WERR_NOMEM;
329         }
330
331         if (last_write_time) {
332                 *last_write_time = 0;
333         }
334
335         return WERR_OK;
336 }
337
338 WERROR reg_enumvalue(TALLOC_CTX *mem_ctx, struct registry_key *key,
339                      uint32 idx, char **pname, struct registry_value **pval)
340 {
341         struct registry_value *val;
342         struct regval_blob *blob;
343         WERROR err;
344
345         if (!(key->key->access_granted & KEY_QUERY_VALUE)) {
346                 return WERR_ACCESS_DENIED;
347         }
348
349         if (!(W_ERROR_IS_OK(err = fill_value_cache(key)))) {
350                 return err;
351         }
352
353         if (idx >= regval_ctr_numvals(key->values)) {
354                 return WERR_NO_MORE_ITEMS;
355         }
356
357         blob = regval_ctr_specific_value(key->values, idx);
358
359         val = talloc_zero(mem_ctx, struct registry_value);
360         if (val == NULL) {
361                 return WERR_NOMEM;
362         }
363
364         val->type = regval_type(blob);
365         val->data = data_blob_talloc(mem_ctx, regval_data_p(blob), regval_size(blob));
366
367         if (pname
368             && !(*pname = talloc_strdup(
369                          mem_ctx, regval_name(blob)))) {
370                 TALLOC_FREE(val);
371                 return WERR_NOMEM;
372         }
373
374         *pval = val;
375         return WERR_OK;
376 }
377
378 static WERROR reg_enumvalue_nocachefill(TALLOC_CTX *mem_ctx,
379                                         struct registry_key *key,
380                                         uint32 idx, char **pname,
381                                         struct registry_value **pval)
382 {
383         struct registry_value *val;
384         struct regval_blob *blob;
385
386         if (!(key->key->access_granted & KEY_QUERY_VALUE)) {
387                 return WERR_ACCESS_DENIED;
388         }
389
390         if (idx >= regval_ctr_numvals(key->values)) {
391                 return WERR_NO_MORE_ITEMS;
392         }
393
394         blob = regval_ctr_specific_value(key->values, idx);
395
396         val = talloc_zero(mem_ctx, struct registry_value);
397         if (val == NULL) {
398                 return WERR_NOMEM;
399         }
400
401         val->type = regval_type(blob);
402         val->data = data_blob_talloc(mem_ctx, regval_data_p(blob), regval_size(blob));
403
404         if (pname
405             && !(*pname = talloc_strdup(
406                          mem_ctx, regval_name(blob)))) {
407                 TALLOC_FREE(val);
408                 return WERR_NOMEM;
409         }
410
411         *pval = val;
412         return WERR_OK;
413 }
414
415 WERROR reg_queryvalue(TALLOC_CTX *mem_ctx, struct registry_key *key,
416                       const char *name, struct registry_value **pval)
417 {
418         WERROR err;
419         uint32 i;
420
421         if (!(key->key->access_granted & KEY_QUERY_VALUE)) {
422                 return WERR_ACCESS_DENIED;
423         }
424
425         if (!(W_ERROR_IS_OK(err = fill_value_cache(key)))) {
426                 return err;
427         }
428
429         for (i=0; i < regval_ctr_numvals(key->values); i++) {
430                 struct regval_blob *blob;
431                 blob = regval_ctr_specific_value(key->values, i);
432                 if (strequal(regval_name(blob), name)) {
433                         /*
434                          * don't use reg_enumvalue here:
435                          * re-reading the values from the disk
436                          * would change the indexing and break
437                          * this function.
438                          */
439                         return reg_enumvalue_nocachefill(mem_ctx, key, i,
440                                                          NULL, pval);
441                 }
442         }
443
444         return WERR_BADFILE;
445 }
446
447 WERROR reg_querymultiplevalues(TALLOC_CTX *mem_ctx,
448                                struct registry_key *key,
449                                uint32_t num_names,
450                                const char **names,
451                                uint32_t *pnum_vals,
452                                struct registry_value **pvals)
453 {
454         WERROR err;
455         uint32_t i, n, found = 0;
456         struct registry_value *vals;
457
458         if (num_names == 0) {
459                 return WERR_OK;
460         }
461
462         if (!(key->key->access_granted & KEY_QUERY_VALUE)) {
463                 return WERR_ACCESS_DENIED;
464         }
465
466         if (!(W_ERROR_IS_OK(err = fill_value_cache(key)))) {
467                 return err;
468         }
469
470         vals = talloc_zero_array(mem_ctx, struct registry_value, num_names);
471         if (vals == NULL) {
472                 return WERR_NOMEM;
473         }
474
475         for (n=0; n < num_names; n++) {
476                 for (i=0; i < regval_ctr_numvals(key->values); i++) {
477                         struct regval_blob *blob;
478                         blob = regval_ctr_specific_value(key->values, i);
479                         if (strequal(regval_name(blob), names[n])) {
480                                 struct registry_value *v;
481                                 err = reg_enumvalue(mem_ctx, key, i, NULL, &v);
482                                 if (!W_ERROR_IS_OK(err)) {
483                                         return err;
484                                 }
485                                 vals[n] = *v;
486                                 found++;
487                         }
488                 }
489         }
490
491         *pvals = vals;
492         *pnum_vals = found;
493
494         return WERR_OK;
495 }
496
497 WERROR reg_queryinfokey(struct registry_key *key, uint32_t *num_subkeys,
498                         uint32_t *max_subkeylen, uint32_t *max_subkeysize, 
499                         uint32_t *num_values, uint32_t *max_valnamelen, 
500                         uint32_t *max_valbufsize, uint32_t *secdescsize,
501                         NTTIME *last_changed_time)
502 {
503         uint32 i, max_size;
504         size_t max_len;
505         TALLOC_CTX *mem_ctx;
506         WERROR err;
507         struct security_descriptor *secdesc;
508
509         if (!(key->key->access_granted & KEY_QUERY_VALUE)) {
510                 return WERR_ACCESS_DENIED;
511         }
512
513         if (!W_ERROR_IS_OK(fill_subkey_cache(key)) ||
514             !W_ERROR_IS_OK(fill_value_cache(key))) {
515                 return WERR_BADFILE;
516         }
517
518         max_len = 0;
519         for (i=0; i< regsubkey_ctr_numkeys(key->subkeys); i++) {
520                 max_len = MAX(max_len,
521                         strlen(regsubkey_ctr_specific_key(key->subkeys, i)));
522         }
523
524         *num_subkeys = regsubkey_ctr_numkeys(key->subkeys);
525         *max_subkeylen = max_len;
526         *max_subkeysize = 0;    /* Class length? */
527
528         max_len = 0;
529         max_size = 0;
530         for (i=0; i < regval_ctr_numvals(key->values); i++) {
531                 struct regval_blob *blob;
532                 blob = regval_ctr_specific_value(key->values, i);
533                 max_len = MAX(max_len, strlen(regval_name(blob)));
534                 max_size = MAX(max_size, regval_size(blob));
535         }
536
537         *num_values = regval_ctr_numvals(key->values);
538         *max_valnamelen = max_len;
539         *max_valbufsize = max_size;
540
541         if (!(mem_ctx = talloc_new(key))) {
542                 return WERR_NOMEM;
543         }
544
545         err = regkey_get_secdesc(mem_ctx, key->key, &secdesc);
546         if (!W_ERROR_IS_OK(err)) {
547                 TALLOC_FREE(mem_ctx);
548                 return err;
549         }
550
551         *secdescsize = ndr_size_security_descriptor(secdesc, 0);
552         TALLOC_FREE(mem_ctx);
553
554         *last_changed_time = 0;
555
556         return WERR_OK;
557 }
558
559 WERROR reg_createkey(TALLOC_CTX *ctx, struct registry_key *parent,
560                      const char *subkeypath, uint32 desired_access,
561                      struct registry_key **pkey,
562                      enum winreg_CreateAction *paction)
563 {
564         struct registry_key *key = parent;
565         struct registry_key *create_parent;
566         TALLOC_CTX *mem_ctx;
567         char *path, *end;
568         WERROR err;
569
570         mem_ctx = talloc_new(ctx);
571         if (mem_ctx == NULL) {
572                 return WERR_NOMEM;
573         }
574
575         path = talloc_strdup(mem_ctx, subkeypath);
576         if (path == NULL) {
577                 err = WERR_NOMEM;
578                 goto done;
579         }
580
581         err = regdb_transaction_start();
582         if (!W_ERROR_IS_OK(err)) {
583                 DEBUG(0, ("reg_createkey: failed to start transaction: %s\n",
584                           win_errstr(err)));
585                 goto done;
586         }
587
588         while ((end = strchr(path, '\\')) != NULL) {
589                 struct registry_key *tmp;
590                 enum winreg_CreateAction action;
591
592                 *end = '\0';
593
594                 err = reg_createkey(mem_ctx, key, path,
595                                     KEY_ENUMERATE_SUB_KEYS, &tmp, &action);
596                 if (!W_ERROR_IS_OK(err)) {
597                         goto trans_done;
598                 }
599
600                 if (key != parent) {
601                         TALLOC_FREE(key);
602                 }
603
604                 key = tmp;
605                 path = end+1;
606         }
607
608         /*
609          * At this point, "path" contains the one-element subkey of "key". We
610          * can try to open it.
611          */
612
613         err = reg_openkey(ctx, key, path, desired_access, pkey);
614         if (W_ERROR_IS_OK(err)) {
615                 if (paction != NULL) {
616                         *paction = REG_OPENED_EXISTING_KEY;
617                 }
618                 goto trans_done;
619         }
620
621         if (!W_ERROR_EQUAL(err, WERR_BADFILE)) {
622                 /*
623                  * Something but "notfound" has happened, so bail out
624                  */
625                 goto trans_done;
626         }
627
628         /*
629          * We have to make a copy of the current key, as we opened it only
630          * with ENUM_SUBKEY access.
631          */
632
633         err = reg_openkey(mem_ctx, key, "", KEY_CREATE_SUB_KEY,
634                           &create_parent);
635         if (!W_ERROR_IS_OK(err)) {
636                 goto trans_done;
637         }
638
639         /*
640          * Actually create the subkey
641          */
642
643         err = fill_subkey_cache(create_parent);
644         if (!W_ERROR_IS_OK(err)) {
645                 goto trans_done;
646         }
647
648         err = create_reg_subkey(key->key, path);
649         if (!W_ERROR_IS_OK(err)) {
650                 goto trans_done;
651         }
652
653         /*
654          * Now open the newly created key
655          */
656
657         err = reg_openkey(ctx, create_parent, path, desired_access, pkey);
658         if (W_ERROR_IS_OK(err) && (paction != NULL)) {
659                 *paction = REG_CREATED_NEW_KEY;
660         }
661
662 trans_done:
663         if (W_ERROR_IS_OK(err)) {
664                 err = regdb_transaction_commit();
665                 if (!W_ERROR_IS_OK(err)) {
666                         DEBUG(0, ("reg_createkey: Error committing transaction: %s\n", win_errstr(err)));
667                 }
668         } else {
669                 WERROR err1 = regdb_transaction_cancel();
670                 if (!W_ERROR_IS_OK(err1)) {
671                         DEBUG(0, ("reg_createkey: Error cancelling transaction: %s\n", win_errstr(err1)));
672                 }
673         }
674
675  done:
676         TALLOC_FREE(mem_ctx);
677         return err;
678 }
679
680 WERROR reg_deletekey(struct registry_key *parent, const char *path)
681 {
682         WERROR err;
683         char *name, *end;
684         struct registry_key *tmp_key, *key;
685         TALLOC_CTX *mem_ctx = talloc_stackframe();
686
687         name = talloc_strdup(mem_ctx, path);
688         if (name == NULL) {
689                 err = WERR_NOMEM;
690                 goto done;
691         }
692
693         /* check if the key has subkeys */
694         err = reg_openkey(mem_ctx, parent, name, REG_KEY_READ, &key);
695         W_ERROR_NOT_OK_GOTO_DONE(err);
696
697         err = regdb_transaction_start();
698         if (!W_ERROR_IS_OK(err)) {
699                 DEBUG(0, ("reg_deletekey: Error starting transaction: %s\n",
700                           win_errstr(err)));
701                 goto done;
702         }
703
704         err = fill_subkey_cache(key);
705         W_ERROR_NOT_OK_GOTO(err, trans_done);
706
707         if (regsubkey_ctr_numkeys(key->subkeys) > 0) {
708                 err = WERR_ACCESS_DENIED;
709                 goto trans_done;
710         }
711
712         /* no subkeys - proceed with delete */
713         end = strrchr(name, '\\');
714         if (end != NULL) {
715                 *end = '\0';
716
717                 err = reg_openkey(mem_ctx, parent, name,
718                                   KEY_CREATE_SUB_KEY, &tmp_key);
719                 W_ERROR_NOT_OK_GOTO(err, trans_done);
720
721                 parent = tmp_key;
722                 name = end+1;
723         }
724
725         if (name[0] == '\0') {
726                 err = WERR_INVALID_PARAM;
727                 goto trans_done;
728         }
729
730         err = delete_reg_subkey(parent->key, name);
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 WERROR reg_setvalue(struct registry_key *key, const char *name,
751                     const struct registry_value *val)
752 {
753         struct regval_blob *existing;
754         WERROR err;
755         int res;
756
757         if (!(key->key->access_granted & KEY_SET_VALUE)) {
758                 return WERR_ACCESS_DENIED;
759         }
760
761         err = regdb_transaction_start();
762         if (!W_ERROR_IS_OK(err)) {
763                 DEBUG(0, ("reg_setvalue: Failed to start transaction: %s\n",
764                           win_errstr(err)));
765                 return err;
766         }
767
768         err = fill_value_cache(key);
769         if (!W_ERROR_IS_OK(err)) {
770                 DEBUG(0, ("reg_setvalue: Error filling value cache: %s\n", win_errstr(err)));
771                 goto done;
772         }
773
774         existing = regval_ctr_getvalue(key->values, name);
775
776         if ((existing != NULL) &&
777             (regval_size(existing) == val->data.length) &&
778             (memcmp(regval_data_p(existing), val->data.data,
779                     val->data.length) == 0))
780         {
781                 err = WERR_OK;
782                 goto done;
783         }
784
785         res = regval_ctr_addvalue(key->values, name, val->type,
786                                   val->data.data, val->data.length);
787
788         if (res == 0) {
789                 TALLOC_FREE(key->values);
790                 err = WERR_NOMEM;
791                 goto done;
792         }
793
794         if (!store_reg_values(key->key, key->values)) {
795                 TALLOC_FREE(key->values);
796                 DEBUG(0, ("reg_setvalue: store_reg_values failed\n"));
797                 err = WERR_REG_IO_FAILURE;
798                 goto done;
799         }
800
801         err = WERR_OK;
802
803 done:
804         if (W_ERROR_IS_OK(err)) {
805                 err = regdb_transaction_commit();
806                 if (!W_ERROR_IS_OK(err)) {
807                         DEBUG(0, ("reg_setvalue: Error committing transaction: %s\n", win_errstr(err)));
808                 }
809         } else {
810                 WERROR err1 = regdb_transaction_cancel();
811                 if (!W_ERROR_IS_OK(err1)) {
812                         DEBUG(0, ("reg_setvalue: Error cancelling transaction: %s\n", win_errstr(err1)));
813                 }
814         }
815
816         return err;
817 }
818
819 static WERROR reg_value_exists(struct registry_key *key, const char *name)
820 {
821         struct regval_blob *blob;
822
823         blob = regval_ctr_getvalue(key->values, name);
824
825         if (blob == NULL) {
826                 return WERR_BADFILE;
827         } else {
828                 return WERR_OK;
829         }
830 }
831
832 WERROR reg_deletevalue(struct registry_key *key, const char *name)
833 {
834         WERROR err;
835
836         if (!(key->key->access_granted & KEY_SET_VALUE)) {
837                 return WERR_ACCESS_DENIED;
838         }
839
840         err = regdb_transaction_start();
841         if (!W_ERROR_IS_OK(err)) {
842                 DEBUG(0, ("reg_deletevalue: Failed to start transaction: %s\n",
843                           win_errstr(err)));
844                 return err;
845         }
846
847         err = fill_value_cache(key);
848         if (!W_ERROR_IS_OK(err)) {
849                 DEBUG(0, ("reg_deletevalue; Error filling value cache: %s\n",
850                           win_errstr(err)));
851                 goto done;
852         }
853
854         err = reg_value_exists(key, name);
855         if (!W_ERROR_IS_OK(err)) {
856                 goto done;
857         }
858
859         regval_ctr_delvalue(key->values, name);
860
861         if (!store_reg_values(key->key, key->values)) {
862                 TALLOC_FREE(key->values);
863                 err = WERR_REG_IO_FAILURE;
864                 DEBUG(0, ("reg_deletevalue: store_reg_values failed\n"));
865                 goto done;
866         }
867
868         err = WERR_OK;
869
870 done:
871         if (W_ERROR_IS_OK(err)) {
872                 err = regdb_transaction_commit();
873                 if (!W_ERROR_IS_OK(err)) {
874                         DEBUG(0, ("reg_deletevalue: Error committing transaction: %s\n", win_errstr(err)));
875                 }
876         } else {
877                 WERROR err1 = regdb_transaction_cancel();
878                 if (!W_ERROR_IS_OK(err1)) {
879                         DEBUG(0, ("reg_deletevalue: Error cancelling transaction: %s\n", win_errstr(err1)));
880                 }
881         }
882
883         return err;
884 }
885
886 WERROR reg_getkeysecurity(TALLOC_CTX *mem_ctx, struct registry_key *key,
887                           struct security_descriptor **psecdesc)
888 {
889         return regkey_get_secdesc(mem_ctx, key->key, psecdesc);
890 }
891
892 WERROR reg_setkeysecurity(struct registry_key *key,
893                           struct security_descriptor *psecdesc)
894 {
895         return regkey_set_secdesc(key->key, psecdesc);
896 }
897
898 WERROR reg_getversion(uint32_t *version)
899 {
900         if (version == NULL) {
901                 return WERR_INVALID_PARAM;
902         }
903
904         *version = 0x00000005; /* Windows 2000 registry API version */
905         return WERR_OK;
906 }
907
908 /**********************************************************************
909  * Higher level utility functions
910  **********************************************************************/
911
912 WERROR reg_deleteallvalues(struct registry_key *key)
913 {
914         WERROR err;
915         int i;
916
917         if (!(key->key->access_granted & KEY_SET_VALUE)) {
918                 return WERR_ACCESS_DENIED;
919         }
920
921         if (!W_ERROR_IS_OK(err = fill_value_cache(key))) {
922                 return err;
923         }
924
925         for (i=0; i < regval_ctr_numvals(key->values); i++) {
926                 struct regval_blob *blob;
927                 blob = regval_ctr_specific_value(key->values, i);
928                 regval_ctr_delvalue(key->values, regval_name(blob));
929         }
930
931         if (!store_reg_values(key->key, key->values)) {
932                 TALLOC_FREE(key->values);
933                 return WERR_REG_IO_FAILURE;
934         }
935
936         return WERR_OK;
937 }
938
939 /*
940  * Utility function to delete a registry key with all its subkeys.
941  * Note that reg_deletekey returns ACCESS_DENIED when called on a
942  * key that has subkeys.
943  */
944 static WERROR reg_deletekey_recursive_internal(struct registry_key *parent,
945                                                const char *path,
946                                                bool del_key)
947 {
948         WERROR werr = WERR_OK;
949         struct registry_key *key;
950         char *subkey_name = NULL;
951         uint32 i;
952         TALLOC_CTX *mem_ctx = talloc_stackframe();
953
954         /* recurse through subkeys first */
955         werr = reg_openkey(mem_ctx, parent, path, REG_KEY_ALL, &key);
956         if (!W_ERROR_IS_OK(werr)) {
957                 goto done;
958         }
959
960         werr = fill_subkey_cache(key);
961         W_ERROR_NOT_OK_GOTO_DONE(werr);
962
963         /*
964          * loop from top to bottom for perfomance:
965          * this way, we need to rehash the regsubkey containers less
966          */
967         for (i = regsubkey_ctr_numkeys(key->subkeys) ; i > 0; i--) {
968                 subkey_name = regsubkey_ctr_specific_key(key->subkeys, i-1);
969                 werr = reg_deletekey_recursive_internal(key, subkey_name, true);
970                 W_ERROR_NOT_OK_GOTO_DONE(werr);
971         }
972
973         if (del_key) {
974                 /* now delete the actual key */
975                 werr = reg_deletekey(parent, path);
976         }
977
978 done:
979         TALLOC_FREE(mem_ctx);
980         return werr;
981 }
982
983 static WERROR reg_deletekey_recursive_trans(struct registry_key *parent,
984                                             const char *path,
985                                             bool del_key)
986 {
987         WERROR werr;
988
989         werr = regdb_transaction_start();
990         if (!W_ERROR_IS_OK(werr)) {
991                 DEBUG(0, ("reg_deletekey_recursive_trans: "
992                           "error starting transaction: %s\n",
993                           win_errstr(werr)));
994                 return werr;
995         }
996
997         werr = reg_deletekey_recursive_internal(parent, path, del_key);
998
999         if (!W_ERROR_IS_OK(werr)) {
1000                 WERROR werr2;
1001
1002                 DEBUG(1, (__location__ " failed to delete key '%s' from key "
1003                           "'%s': %s\n", path, parent->key->name,
1004                           win_errstr(werr)));
1005
1006                 werr2 = regdb_transaction_cancel();
1007                 if (!W_ERROR_IS_OK(werr2)) {
1008                         DEBUG(0, ("reg_deletekey_recursive_trans: "
1009                                   "error cancelling transaction: %s\n",
1010                                   win_errstr(werr2)));
1011                         /*
1012                          * return the original werr or the
1013                          * error from cancelling the transaction?
1014                          */
1015                 }
1016         } else {
1017                 werr = regdb_transaction_commit();
1018                 if (!W_ERROR_IS_OK(werr)) {
1019                         DEBUG(0, ("reg_deletekey_recursive_trans: "
1020                                   "error committing transaction: %s\n",
1021                                   win_errstr(werr)));
1022                 }
1023         }
1024
1025         return werr;
1026 }
1027
1028 WERROR reg_deletekey_recursive(struct registry_key *parent,
1029                                const char *path)
1030 {
1031         return reg_deletekey_recursive_trans(parent, path, true);
1032 }
1033
1034 WERROR reg_deletesubkeys_recursive(struct registry_key *parent,
1035                                    const char *path)
1036 {
1037         return reg_deletekey_recursive_trans(parent, path, false);
1038 }
1039