2 * Unix SMB/CIFS implementation.
3 * Virtual Windows Registry Layer
4 * Copyright (C) Gerald Carter 2002-2005
5 * Copyright (C) Michael Adam 2007-2009
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.
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.
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/>.
21 /* Implementation of internal registry database functions. */
24 #include "system/filesys.h"
27 #include "reg_util_internal.h"
28 #include "reg_backend_db.h"
29 #include "reg_objects.h"
30 #include "nt_printing.h"
35 #define DBGC_CLASS DBGC_REGISTRY
37 static struct db_context
*regdb
= NULL
;
38 static int regdb_refcount
;
40 static bool regdb_key_exists(struct db_context
*db
, const char *key
);
41 static bool regdb_key_is_base_key(const char *key
);
42 static WERROR
regdb_fetch_keys_internal(struct db_context
*db
, const char *key
,
43 struct regsubkey_ctr
*ctr
);
44 static bool regdb_store_keys_internal(struct db_context
*db
, const char *key
,
45 struct regsubkey_ctr
*ctr
);
46 static int regdb_fetch_values_internal(struct db_context
*db
, const char* key
,
47 struct regval_ctr
*values
);
48 static bool regdb_store_values_internal(struct db_context
*db
, const char *key
,
49 struct regval_ctr
*values
);
51 static NTSTATUS
create_sorted_subkeys(const char *key
);
53 /* List the deepest path into the registry. All part components will be created.*/
55 /* If you want to have a part of the path controlled by the tdb and part by
56 a virtual registry db (e.g. printing), then you have to list the deepest path.
57 For example,"HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion/Print"
58 allows the reg_db backend to handle everything up to
59 "HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion" and then we'll hook
60 the reg_printing backend onto the last component of the path (see
61 KEY_PRINTING_2K in include/rpc_reg.h) --jerry */
63 static const char *builtin_registry_paths
[] = {
67 KEY_PRINTING
"\\Forms",
68 KEY_PRINTING
"\\Printers",
69 KEY_PRINTING
"\\Environments\\Windows NT x86\\Print Processors\\winprint",
76 KEY_SAMBA_GROUP_POLICY
,
77 KEY_GP_MACHINE_POLICY
,
78 KEY_GP_MACHINE_WIN_POLICY
,
81 KEY_GP_USER_WIN_POLICY
,
82 "HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon\\GPExtensions",
83 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Print\\Monitors",
85 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\DefaultUserConfiguration",
94 struct builtin_regkey_value
{
96 const char *valuename
;
104 static struct builtin_regkey_value builtin_registry_values
[] = {
105 { KEY_PRINTING_PORTS
,
106 SAMBA_PRINTER_PORT_NAME
, REG_SZ
, { "" } },
108 "DefaultSpoolDirectory", REG_SZ
, { "C:\\Windows\\System32\\Spool\\Printers" } },
110 "DisplayName", REG_SZ
, { "Event Log" } },
112 "ErrorControl", REG_DWORD
, { (char*)0x00000001 } },
113 { NULL
, NULL
, 0, { NULL
} }
117 * Initialize a key in the registry:
118 * create each component key of the specified path.
120 static WERROR
init_registry_key_internal(struct db_context
*db
,
121 const char *add_path
)
124 TALLOC_CTX
*frame
= talloc_stackframe();
127 char *remaining
= NULL
;
130 struct regsubkey_ctr
*subkeys
;
133 DEBUG(6, ("init_registry_key: Adding [%s]\n", add_path
));
135 path
= talloc_strdup(frame
, add_path
);
136 base
= talloc_strdup(frame
, "");
137 if (!path
|| !base
) {
143 while (next_token_talloc(frame
, &p
, &keyname
, "\\")) {
145 /* build up the registry path from the components */
148 base
= talloc_asprintf(frame
, "%s\\", base
);
154 base
= talloc_asprintf_append(base
, "%s", keyname
);
160 /* get the immediate subkeyname (if we have one ) */
162 subkeyname
= talloc_strdup(frame
, "");
168 remaining
= talloc_strdup(frame
, p
);
175 if (!next_token_talloc(frame
, &p2
,
178 subkeyname
= talloc_strdup(frame
,p2
);
186 DEBUG(10,("init_registry_key: Storing key [%s] with "
187 "subkey [%s]\n", base
,
188 *subkeyname
? subkeyname
: "NULL"));
190 /* we don't really care if the lookup succeeds or not
191 * since we are about to update the record.
192 * We just want any subkeys already present */
194 werr
= regsubkey_ctr_init(frame
, &subkeys
);
195 if (!W_ERROR_IS_OK(werr
)) {
196 DEBUG(0,("talloc() failure!\n"));
200 werr
= regdb_fetch_keys_internal(db
, base
, subkeys
);
201 if (!W_ERROR_IS_OK(werr
) &&
202 !W_ERROR_EQUAL(werr
, WERR_NOT_FOUND
))
208 werr
= regsubkey_ctr_addkey(subkeys
, subkeyname
);
209 if (!W_ERROR_IS_OK(werr
)) {
213 if (!regdb_store_keys_internal(db
, base
, subkeys
)) {
214 werr
= WERR_CAN_NOT_COMPLETE
;
226 struct init_registry_key_context
{
227 const char *add_path
;
230 static NTSTATUS
init_registry_key_action(struct db_context
*db
,
233 struct init_registry_key_context
*init_ctx
=
234 (struct init_registry_key_context
*)private_data
;
236 return werror_to_ntstatus(init_registry_key_internal(
237 db
, init_ctx
->add_path
));
241 * Initialize a key in the registry:
242 * create each component key of the specified path,
243 * wrapped in one db transaction.
245 WERROR
init_registry_key(const char *add_path
)
247 struct init_registry_key_context init_ctx
;
249 if (regdb_key_exists(regdb
, add_path
)) {
253 init_ctx
.add_path
= add_path
;
255 return ntstatus_to_werror(dbwrap_trans_do(regdb
,
256 init_registry_key_action
,
260 /***********************************************************************
261 Open the registry data in the tdb
262 ***********************************************************************/
264 static void regdb_ctr_add_value(struct regval_ctr
*ctr
,
265 struct builtin_regkey_value
*value
)
267 switch(value
->type
) {
269 regval_ctr_addvalue(ctr
, value
->valuename
, REG_DWORD
,
270 (uint8_t *)&value
->data
.dw_value
,
275 regval_ctr_addvalue_sz(ctr
, value
->valuename
,
280 DEBUG(0, ("regdb_ctr_add_value: invalid value type in "
281 "registry values [%d]\n", value
->type
));
285 static NTSTATUS
init_registry_data_action(struct db_context
*db
,
289 TALLOC_CTX
*frame
= talloc_stackframe();
290 struct regval_ctr
*values
;
293 /* loop over all of the predefined paths and add each component */
295 for (i
=0; builtin_registry_paths
[i
] != NULL
; i
++) {
296 if (regdb_key_exists(db
, builtin_registry_paths
[i
])) {
299 status
= werror_to_ntstatus(init_registry_key_internal(db
,
300 builtin_registry_paths
[i
]));
301 if (!NT_STATUS_IS_OK(status
)) {
306 /* loop over all of the predefined values and add each component */
308 for (i
=0; builtin_registry_values
[i
].path
!= NULL
; i
++) {
311 werr
= regval_ctr_init(frame
, &values
);
312 if (!W_ERROR_IS_OK(werr
)) {
313 status
= werror_to_ntstatus(werr
);
317 regdb_fetch_values_internal(db
,
318 builtin_registry_values
[i
].path
,
321 /* preserve existing values across restarts. Only add new ones */
323 if (!regval_ctr_value_exists(values
,
324 builtin_registry_values
[i
].valuename
))
326 regdb_ctr_add_value(values
,
327 &builtin_registry_values
[i
]);
328 regdb_store_values_internal(db
,
329 builtin_registry_values
[i
].path
,
335 status
= NT_STATUS_OK
;
343 WERROR
init_registry_data(void)
346 TALLOC_CTX
*frame
= talloc_stackframe();
347 struct regval_ctr
*values
;
351 * First, check for the existence of the needed keys and values.
352 * If all do already exist, we can save the writes.
354 for (i
=0; builtin_registry_paths
[i
] != NULL
; i
++) {
355 if (!regdb_key_exists(regdb
, builtin_registry_paths
[i
])) {
360 for (i
=0; builtin_registry_values
[i
].path
!= NULL
; i
++) {
361 werr
= regval_ctr_init(frame
, &values
);
362 W_ERROR_NOT_OK_GOTO_DONE(werr
);
364 regdb_fetch_values_internal(regdb
,
365 builtin_registry_values
[i
].path
,
367 if (!regval_ctr_value_exists(values
,
368 builtin_registry_values
[i
].valuename
))
383 * There are potentially quite a few store operations which are all
384 * indiviually wrapped in tdb transactions. Wrapping them in a single
385 * transaction gives just a single transaction_commit() to actually do
386 * its fsync()s. See tdb/common/transaction.c for info about nested
387 * transaction behaviour.
390 werr
= ntstatus_to_werror(dbwrap_trans_do(regdb
,
391 init_registry_data_action
,
399 static int regdb_normalize_keynames_fn(struct db_record
*rec
,
402 TALLOC_CTX
*mem_ctx
= talloc_tos();
406 if (rec
->key
.dptr
== NULL
|| rec
->key
.dsize
== 0) {
410 keyname
= strchr((const char *) rec
->key
.dptr
, '/');
412 struct db_record new_rec
;
414 keyname
= talloc_string_sub(mem_ctx
,
415 (const char *) rec
->key
.dptr
,
419 DEBUG(2, ("regdb_normalize_keynames_fn: Convert %s to %s\n",
420 (const char *) rec
->key
.dptr
,
423 new_rec
.value
= rec
->value
;
424 new_rec
.key
= string_term_tdb_data(keyname
);
425 new_rec
.private_data
= rec
->private_data
;
427 /* Delete the original record and store the normalized key */
428 status
= rec
->delete_rec(rec
);
429 if (!NT_STATUS_IS_OK(status
)) {
430 DEBUG(0,("regdb_normalize_keynames_fn: "
431 "tdb_delete for [%s] failed!\n",
436 status
= rec
->store(&new_rec
, new_rec
.value
, TDB_REPLACE
);
437 if (!NT_STATUS_IS_OK(status
)) {
438 DEBUG(0,("regdb_normalize_keynames_fn: "
439 "failed to store new record for [%s]!\n",
448 static WERROR
regdb_store_regdb_version(uint32_t version
)
451 const char *version_keyname
= "INFO/version";
454 return WERR_CAN_NOT_COMPLETE
;
457 status
= dbwrap_trans_store_int32(regdb
, version_keyname
, version
);
458 if (!NT_STATUS_IS_OK(status
)) {
459 DEBUG(1, ("regdb_store_regdb_version: error storing %s = %d: %s\n",
460 version_keyname
, version
, nt_errstr(status
)));
461 return ntstatus_to_werror(status
);
463 DEBUG(10, ("regdb_store_regdb_version: stored %s = %d\n",
464 version_keyname
, version
));
469 static WERROR
regdb_upgrade_v1_to_v2(void)
475 mem_ctx
= talloc_stackframe();
476 if (mem_ctx
== NULL
) {
480 rc
= regdb
->traverse(regdb
, regdb_normalize_keynames_fn
, mem_ctx
);
482 talloc_destroy(mem_ctx
);
485 return WERR_REG_IO_FAILURE
;
488 werr
= regdb_store_regdb_version(REGVER_V2
);
492 /***********************************************************************
493 Open the registry database
494 ***********************************************************************/
496 WERROR
regdb_init(void)
498 const char *vstring
= "INFO/version";
499 uint32 vers_id
, expected_version
;
503 DEBUG(10, ("regdb_init: incrementing refcount (%d->%d)\n",
504 regdb_refcount
, regdb_refcount
+1));
509 regdb
= db_open(NULL
, state_path("registry.tdb"), 0,
510 REG_TDB_FLAGS
, O_RDWR
, 0600);
512 regdb
= db_open(NULL
, state_path("registry.tdb"), 0,
513 REG_TDB_FLAGS
, O_RDWR
|O_CREAT
, 0600);
515 werr
= ntstatus_to_werror(map_nt_error_from_unix(errno
));
516 DEBUG(1,("regdb_init: Failed to open registry %s (%s)\n",
517 state_path("registry.tdb"), strerror(errno
) ));
521 DEBUG(10,("regdb_init: Successfully created registry tdb\n"));
525 DEBUG(10, ("regdb_init: registry db openend. refcount reset (%d)\n",
528 expected_version
= REGVER_V2
;
530 vers_id
= dbwrap_fetch_int32(regdb
, vstring
);
532 DEBUG(10, ("regdb_init: registry version uninitialized "
533 "(got %d), initializing to version %d\n",
534 vers_id
, expected_version
));
536 werr
= regdb_store_regdb_version(expected_version
);
540 if (vers_id
> expected_version
|| vers_id
== 0) {
541 DEBUG(1, ("regdb_init: unknown registry version %d "
542 "(code version = %d), refusing initialization\n",
543 vers_id
, expected_version
));
544 return WERR_CAN_NOT_COMPLETE
;
547 if (vers_id
== REGVER_V1
) {
548 DEBUG(10, ("regdb_init: got registry db version %d, upgrading "
549 "to version %d\n", REGVER_V1
, REGVER_V2
));
551 if (regdb
->transaction_start(regdb
) != 0) {
552 return WERR_REG_IO_FAILURE
;
555 werr
= regdb_upgrade_v1_to_v2();
556 if (!W_ERROR_IS_OK(werr
)) {
557 regdb
->transaction_cancel(regdb
);
561 if (regdb
->transaction_commit(regdb
) != 0) {
562 return WERR_REG_IO_FAILURE
;
568 /* future upgrade code should go here */
573 /***********************************************************************
574 Open the registry. Must already have been initialized by regdb_init()
575 ***********************************************************************/
577 WERROR
regdb_open( void )
579 WERROR result
= WERR_OK
;
582 DEBUG(10, ("regdb_open: incrementing refcount (%d->%d)\n",
583 regdb_refcount
, regdb_refcount
+1));
590 regdb
= db_open(NULL
, state_path("registry.tdb"), 0,
591 REG_TDB_FLAGS
, O_RDWR
, 0600);
593 result
= ntstatus_to_werror( map_nt_error_from_unix( errno
) );
594 DEBUG(0,("regdb_open: Failed to open %s! (%s)\n",
595 state_path("registry.tdb"), strerror(errno
) ));
601 DEBUG(10, ("regdb_open: registry db opened. refcount reset (%d)\n",
607 /***********************************************************************
608 ***********************************************************************/
610 int regdb_close( void )
612 if (regdb_refcount
== 0) {
618 DEBUG(10, ("regdb_close: decrementing refcount (%d->%d)\n",
619 regdb_refcount
+1, regdb_refcount
));
621 if ( regdb_refcount
> 0 )
624 SMB_ASSERT( regdb_refcount
>= 0 );
630 WERROR
regdb_transaction_start(void)
632 return (regdb
->transaction_start(regdb
) == 0) ?
633 WERR_OK
: WERR_REG_IO_FAILURE
;
636 WERROR
regdb_transaction_commit(void)
638 return (regdb
->transaction_commit(regdb
) == 0) ?
639 WERR_OK
: WERR_REG_IO_FAILURE
;
642 WERROR
regdb_transaction_cancel(void)
644 return (regdb
->transaction_cancel(regdb
) == 0) ?
645 WERR_OK
: WERR_REG_IO_FAILURE
;
648 /***********************************************************************
649 return the tdb sequence number of the registry tdb.
650 this is an indicator for the content of the registry
651 having changed. it will change upon regdb_init, too, though.
652 ***********************************************************************/
653 int regdb_get_seqnum(void)
655 return regdb
->get_seqnum(regdb
);
659 static WERROR
regdb_delete_key_with_prefix(struct db_context
*db
,
664 WERROR werr
= WERR_NOMEM
;
665 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
667 if (keyname
== NULL
) {
668 werr
= WERR_INVALID_PARAM
;
672 if (prefix
== NULL
) {
673 path
= discard_const_p(char, keyname
);
675 path
= talloc_asprintf(mem_ctx
, "%s\\%s", prefix
, keyname
);
681 path
= normalize_reg_path(mem_ctx
, path
);
686 werr
= ntstatus_to_werror(dbwrap_delete_bystring(db
, path
));
688 /* treat "not" found" as ok */
689 if (W_ERROR_EQUAL(werr
, WERR_NOT_FOUND
)) {
694 talloc_free(mem_ctx
);
699 static WERROR
regdb_delete_values(struct db_context
*db
, const char *keyname
)
701 return regdb_delete_key_with_prefix(db
, keyname
, REG_VALUE_PREFIX
);
704 static WERROR
regdb_delete_secdesc(struct db_context
*db
, const char *keyname
)
706 return regdb_delete_key_with_prefix(db
, keyname
, REG_SECDESC_PREFIX
);
709 static WERROR
regdb_delete_subkeylist(struct db_context
*db
, const char *keyname
)
711 return regdb_delete_key_with_prefix(db
, keyname
, NULL
);
714 static WERROR
regdb_delete_key_lists(struct db_context
*db
, const char *keyname
)
718 werr
= regdb_delete_values(db
, keyname
);
719 if (!W_ERROR_IS_OK(werr
)) {
720 DEBUG(1, (__location__
" Deleting %s\\%s failed: %s\n",
721 REG_VALUE_PREFIX
, keyname
, win_errstr(werr
)));
725 werr
= regdb_delete_secdesc(db
, keyname
);
726 if (!W_ERROR_IS_OK(werr
)) {
727 DEBUG(1, (__location__
" Deleting %s\\%s failed: %s\n",
728 REG_SECDESC_PREFIX
, keyname
, win_errstr(werr
)));
732 werr
= regdb_delete_subkeylist(db
, keyname
);
733 if (!W_ERROR_IS_OK(werr
)) {
734 DEBUG(1, (__location__
" Deleting %s failed: %s\n",
735 keyname
, win_errstr(werr
)));
743 /***********************************************************************
744 Add subkey strings to the registry tdb under a defined key
745 fmt is the same format as tdb_pack except this function only supports
747 ***********************************************************************/
749 static WERROR
regdb_store_keys_internal2(struct db_context
*db
,
751 struct regsubkey_ctr
*ctr
)
754 uint8
*buffer
= NULL
;
757 uint32 num_subkeys
= regsubkey_ctr_numkeys(ctr
);
758 char *keyname
= NULL
;
759 TALLOC_CTX
*ctx
= talloc_stackframe();
763 werr
= WERR_INVALID_PARAM
;
767 keyname
= talloc_strdup(ctx
, key
);
773 keyname
= normalize_reg_path(ctx
, keyname
);
779 /* allocate some initial memory */
781 buffer
= (uint8
*)SMB_MALLOC(1024);
782 if (buffer
== NULL
) {
789 /* store the number of subkeys */
791 len
+= tdb_pack(buffer
+len
, buflen
-len
, "d", num_subkeys
);
793 /* pack all the strings */
795 for (i
=0; i
<num_subkeys
; i
++) {
798 thistime
= tdb_pack(buffer
+len
, buflen
-len
, "f",
799 regsubkey_ctr_specific_key(ctr
, i
));
800 if (len
+thistime
> buflen
) {
803 * tdb_pack hasn't done anything because of the short
804 * buffer, allocate extra space.
806 buffer
= SMB_REALLOC_ARRAY(buffer
, uint8_t,
809 DEBUG(0, ("regdb_store_keys: Failed to realloc "
810 "memory of size [%u]\n",
811 (unsigned int)(len
+thistime
)*2));
815 buflen
= (len
+thistime
)*2;
816 thistime2
= tdb_pack(
817 buffer
+len
, buflen
-len
, "f",
818 regsubkey_ctr_specific_key(ctr
, i
));
819 if (thistime2
!= thistime
) {
820 DEBUG(0, ("tdb_pack failed\n"));
821 werr
= WERR_CAN_NOT_COMPLETE
;
828 /* finally write out the data */
832 werr
= ntstatus_to_werror(dbwrap_store_bystring(db
, keyname
, dbuf
,
834 W_ERROR_NOT_OK_GOTO_DONE(werr
);
837 * recreate the sorted subkey cache for regdb_key_exists()
839 werr
= ntstatus_to_werror(create_sorted_subkeys(keyname
));
847 /***********************************************************************
848 Store the new subkey record and create any child key records that
849 do not currently exist
850 ***********************************************************************/
852 struct regdb_store_keys_context
{
854 struct regsubkey_ctr
*ctr
;
857 static NTSTATUS
regdb_store_keys_action(struct db_context
*db
,
860 struct regdb_store_keys_context
*store_ctx
;
864 struct regsubkey_ctr
*subkeys
= NULL
, *old_subkeys
= NULL
;
865 char *oldkeyname
= NULL
;
866 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
868 store_ctx
= (struct regdb_store_keys_context
*)private_data
;
871 * Re-fetch the old keys inside the transaction
874 werr
= regsubkey_ctr_init(mem_ctx
, &old_subkeys
);
875 W_ERROR_NOT_OK_GOTO_DONE(werr
);
877 werr
= regdb_fetch_keys_internal(db
, store_ctx
->key
, old_subkeys
);
878 if (!W_ERROR_IS_OK(werr
) &&
879 !W_ERROR_EQUAL(werr
, WERR_NOT_FOUND
))
885 * Make the store operation as safe as possible without transactions:
887 * (1) For each subkey removed from ctr compared with old_subkeys:
889 * (a) First delete the value db entry.
891 * (b) Next delete the secdesc db record.
893 * (c) Then delete the subkey list entry.
895 * (2) Now write the list of subkeys of the parent key,
896 * deleting removed entries and adding new ones.
898 * (3) Finally create the subkey list entries for the added keys.
900 * This way if we crash half-way in between deleting the subkeys
901 * and storing the parent's list of subkeys, no old data can pop up
902 * out of the blue when re-adding keys later on.
905 /* (1) delete removed keys' lists (values/secdesc/subkeys) */
907 num_subkeys
= regsubkey_ctr_numkeys(old_subkeys
);
908 for (i
=0; i
<num_subkeys
; i
++) {
909 oldkeyname
= regsubkey_ctr_specific_key(old_subkeys
, i
);
911 if (regsubkey_ctr_key_exists(store_ctx
->ctr
, oldkeyname
)) {
913 * It's still around, don't delete
918 path
= talloc_asprintf(mem_ctx
, "%s\\%s", store_ctx
->key
,
925 werr
= regdb_delete_key_lists(db
, path
);
926 W_ERROR_NOT_OK_GOTO_DONE(werr
);
931 TALLOC_FREE(old_subkeys
);
933 /* (2) store the subkey list for the parent */
935 werr
= regdb_store_keys_internal2(db
, store_ctx
->key
, store_ctx
->ctr
);
936 if (!W_ERROR_IS_OK(werr
)) {
937 DEBUG(0,("regdb_store_keys: Failed to store new subkey list "
938 "for parent [%s]: %s\n", store_ctx
->key
,
943 /* (3) now create records for any subkeys that don't already exist */
945 num_subkeys
= regsubkey_ctr_numkeys(store_ctx
->ctr
);
947 if (num_subkeys
== 0) {
948 werr
= regsubkey_ctr_init(mem_ctx
, &subkeys
);
949 W_ERROR_NOT_OK_GOTO_DONE(werr
);
951 werr
= regdb_store_keys_internal2(db
, store_ctx
->key
, subkeys
);
952 if (!W_ERROR_IS_OK(werr
)) {
953 DEBUG(0,("regdb_store_keys: Failed to store "
954 "new record for key [%s]: %s\n",
955 store_ctx
->key
, win_errstr(werr
)));
958 TALLOC_FREE(subkeys
);
961 for (i
=0; i
<num_subkeys
; i
++) {
962 path
= talloc_asprintf(mem_ctx
, "%s\\%s", store_ctx
->key
,
963 regsubkey_ctr_specific_key(store_ctx
->ctr
, i
));
968 werr
= regsubkey_ctr_init(mem_ctx
, &subkeys
);
969 W_ERROR_NOT_OK_GOTO_DONE(werr
);
971 werr
= regdb_fetch_keys_internal(db
, path
, subkeys
);
972 if (!W_ERROR_IS_OK(werr
)) {
973 /* create a record with 0 subkeys */
974 werr
= regdb_store_keys_internal2(db
, path
, subkeys
);
975 if (!W_ERROR_IS_OK(werr
)) {
976 DEBUG(0,("regdb_store_keys: Failed to store "
977 "new record for key [%s]: %s\n", path
,
983 TALLOC_FREE(subkeys
);
988 * Update the seqnum in the container to possibly
989 * prevent next read from going to disk
991 werr
= regsubkey_ctr_set_seqnum(store_ctx
->ctr
, db
->get_seqnum(db
));
994 talloc_free(mem_ctx
);
995 return werror_to_ntstatus(werr
);
998 static bool regdb_store_keys_internal(struct db_context
*db
, const char *key
,
999 struct regsubkey_ctr
*ctr
)
1001 int num_subkeys
, old_num_subkeys
, i
;
1002 struct regsubkey_ctr
*old_subkeys
= NULL
;
1003 TALLOC_CTX
*ctx
= talloc_stackframe();
1006 struct regdb_store_keys_context store_ctx
;
1008 if (!regdb_key_is_base_key(key
) && !regdb_key_exists(db
, key
)) {
1013 * fetch a list of the old subkeys so we can determine if anything has
1017 werr
= regsubkey_ctr_init(ctx
, &old_subkeys
);
1018 if (!W_ERROR_IS_OK(werr
)) {
1019 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
1023 werr
= regdb_fetch_keys_internal(db
, key
, old_subkeys
);
1024 if (!W_ERROR_IS_OK(werr
) &&
1025 !W_ERROR_EQUAL(werr
, WERR_NOT_FOUND
))
1030 num_subkeys
= regsubkey_ctr_numkeys(ctr
);
1031 old_num_subkeys
= regsubkey_ctr_numkeys(old_subkeys
);
1032 if ((num_subkeys
&& old_num_subkeys
) &&
1033 (num_subkeys
== old_num_subkeys
)) {
1035 for (i
= 0; i
< num_subkeys
; i
++) {
1036 if (strcmp(regsubkey_ctr_specific_key(ctr
, i
),
1037 regsubkey_ctr_specific_key(old_subkeys
, i
))
1043 if (i
== num_subkeys
) {
1045 * Nothing changed, no point to even start a tdb
1054 TALLOC_FREE(old_subkeys
);
1056 store_ctx
.key
= key
;
1057 store_ctx
.ctr
= ctr
;
1059 werr
= ntstatus_to_werror(dbwrap_trans_do(db
,
1060 regdb_store_keys_action
,
1063 ret
= W_ERROR_IS_OK(werr
);
1071 bool regdb_store_keys(const char *key
, struct regsubkey_ctr
*ctr
)
1073 return regdb_store_keys_internal(regdb
, key
, ctr
);
1077 * create a subkey of a given key
1080 struct regdb_create_subkey_context
{
1085 static NTSTATUS
regdb_create_subkey_action(struct db_context
*db
,
1089 struct regdb_create_subkey_context
*create_ctx
;
1090 struct regsubkey_ctr
*subkeys
;
1091 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
1093 create_ctx
= (struct regdb_create_subkey_context
*)private_data
;
1095 werr
= regsubkey_ctr_init(mem_ctx
, &subkeys
);
1096 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1098 werr
= regdb_fetch_keys_internal(db
, create_ctx
->key
, subkeys
);
1099 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1101 werr
= regsubkey_ctr_addkey(subkeys
, create_ctx
->subkey
);
1102 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1104 werr
= regdb_store_keys_internal2(db
, create_ctx
->key
, subkeys
);
1105 if (!W_ERROR_IS_OK(werr
)) {
1106 DEBUG(0, (__location__
" failed to store new subkey list for "
1107 "parent key %s: %s\n", create_ctx
->key
,
1112 talloc_free(mem_ctx
);
1113 return werror_to_ntstatus(werr
);
1116 static WERROR
regdb_create_subkey(const char *key
, const char *subkey
)
1119 struct regsubkey_ctr
*subkeys
;
1120 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
1121 struct regdb_create_subkey_context create_ctx
;
1123 if (!regdb_key_is_base_key(key
) && !regdb_key_exists(regdb
, key
)) {
1124 werr
= WERR_NOT_FOUND
;
1128 werr
= regsubkey_ctr_init(mem_ctx
, &subkeys
);
1129 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1131 werr
= regdb_fetch_keys_internal(regdb
, key
, subkeys
);
1132 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1134 if (regsubkey_ctr_key_exists(subkeys
, subkey
)) {
1139 talloc_free(subkeys
);
1141 create_ctx
.key
= key
;
1142 create_ctx
.subkey
= subkey
;
1144 werr
= ntstatus_to_werror(dbwrap_trans_do(regdb
,
1145 regdb_create_subkey_action
,
1149 talloc_free(mem_ctx
);
1154 * create a subkey of a given key
1157 struct regdb_delete_subkey_context
{
1163 static NTSTATUS
regdb_delete_subkey_action(struct db_context
*db
,
1167 struct regdb_delete_subkey_context
*delete_ctx
;
1168 struct regsubkey_ctr
*subkeys
;
1169 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
1171 delete_ctx
= (struct regdb_delete_subkey_context
*)private_data
;
1173 werr
= regdb_delete_key_lists(db
, delete_ctx
->path
);
1174 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1176 werr
= regsubkey_ctr_init(mem_ctx
, &subkeys
);
1177 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1179 werr
= regdb_fetch_keys_internal(db
, delete_ctx
->key
, subkeys
);
1180 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1182 werr
= regsubkey_ctr_delkey(subkeys
, delete_ctx
->subkey
);
1183 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1185 werr
= regdb_store_keys_internal2(db
, delete_ctx
->key
, subkeys
);
1186 if (!W_ERROR_IS_OK(werr
)) {
1187 DEBUG(0, (__location__
" failed to store new subkey_list for "
1188 "parent key %s: %s\n", delete_ctx
->key
,
1193 talloc_free(mem_ctx
);
1194 return werror_to_ntstatus(werr
);
1197 static WERROR
regdb_delete_subkey(const char *key
, const char *subkey
)
1201 struct regdb_delete_subkey_context delete_ctx
;
1202 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
1204 if (!regdb_key_is_base_key(key
) && !regdb_key_exists(regdb
, key
)) {
1205 werr
= WERR_NOT_FOUND
;
1209 path
= talloc_asprintf(mem_ctx
, "%s\\%s", key
, subkey
);
1215 if (!regdb_key_exists(regdb
, path
)) {
1220 delete_ctx
.key
= key
;
1221 delete_ctx
.subkey
= subkey
;
1222 delete_ctx
.path
= path
;
1224 werr
= ntstatus_to_werror(dbwrap_trans_do(regdb
,
1225 regdb_delete_subkey_action
,
1229 talloc_free(mem_ctx
);
1233 static TDB_DATA
regdb_fetch_key_internal(struct db_context
*db
,
1234 TALLOC_CTX
*mem_ctx
, const char *key
)
1239 path
= normalize_reg_path(mem_ctx
, key
);
1241 return make_tdb_data(NULL
, 0);
1244 data
= dbwrap_fetch_bystring(db
, mem_ctx
, path
);
1252 * check whether a given key name represents a base key,
1253 * i.e one without a subkey separator ('\').
1255 static bool regdb_key_is_base_key(const char *key
)
1257 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
1265 path
= normalize_reg_path(mem_ctx
, key
);
1267 DEBUG(0, ("out of memory! (talloc failed)\n"));
1271 if (*path
== '\0') {
1275 ret
= (strrchr(path
, '\\') == NULL
);
1278 TALLOC_FREE(mem_ctx
);
1283 * regdb_key_exists() is a very frequent operation. It can be quite
1284 * time-consuming to fully fetch the parent's subkey list, talloc_strdup all
1285 * subkeys and then compare the keyname linearly to all the parent's subkeys.
1287 * The following code tries to make this operation as efficient as possible:
1288 * Per registry key we create a list of subkeys that is very efficient to
1289 * search for existence of a subkey. Its format is:
1291 * 4 bytes num_subkeys
1292 * 4*num_subkey bytes offset into the string array
1293 * then follows a sorted list of subkeys in uppercase
1295 * This record is created by create_sorted_subkeys() on demand if it does not
1296 * exist. scan_parent_subkeys() uses regdb->parse_record to search the sorted
1297 * list, the parsing code and the binary search can be found in
1298 * parent_subkey_scanner. The code uses parse_record() to avoid a memcpy of
1299 * the potentially large subkey record.
1301 * The sorted subkey record is deleted in regdb_store_keys_internal2 and
1302 * recreated on demand.
1305 static int cmp_keynames(char **p1
, char **p2
)
1307 return StrCaseCmp(*p1
, *p2
);
1310 struct create_sorted_subkeys_context
{
1312 const char *sorted_keyname
;
1315 static NTSTATUS
create_sorted_subkeys_action(struct db_context
*db
,
1318 char **sorted_subkeys
;
1319 struct regsubkey_ctr
*ctr
;
1326 struct create_sorted_subkeys_context
*sorted_ctx
;
1328 sorted_ctx
= (struct create_sorted_subkeys_context
*)private_data
;
1331 * In this function, we only treat failing of the actual write to
1332 * the db as a real error. All preliminary errors, at a stage when
1333 * nothing has been written to the DB yet are treated as success
1334 * to be committed (as an empty transaction).
1336 * The reason is that this (disposable) call might be nested in other
1337 * transactions. Doing a cancel here would destroy the possibility of
1338 * a transaction_commit for transactions that we might be wrapped in.
1341 status
= werror_to_ntstatus(regsubkey_ctr_init(talloc_tos(), &ctr
));
1342 if (!NT_STATUS_IS_OK(status
)) {
1343 /* don't treat this as an error */
1344 status
= NT_STATUS_OK
;
1348 status
= werror_to_ntstatus(regdb_fetch_keys_internal(db
,
1351 if (!NT_STATUS_IS_OK(status
)) {
1352 /* don't treat this as an error */
1353 status
= NT_STATUS_OK
;
1357 num_subkeys
= regsubkey_ctr_numkeys(ctr
);
1358 sorted_subkeys
= talloc_array(ctr
, char *, num_subkeys
);
1359 if (sorted_subkeys
== NULL
) {
1360 /* don't treat this as an error */
1364 len
= 4 + 4*num_subkeys
;
1366 for (i
= 0; i
< num_subkeys
; i
++) {
1367 sorted_subkeys
[i
] = talloc_strdup_upper(sorted_subkeys
,
1368 regsubkey_ctr_specific_key(ctr
, i
));
1369 if (sorted_subkeys
[i
] == NULL
) {
1370 /* don't treat this as an error */
1373 len
+= strlen(sorted_subkeys
[i
])+1;
1376 TYPESAFE_QSORT(sorted_subkeys
, num_subkeys
, cmp_keynames
);
1378 buf
= talloc_array(ctr
, char, len
);
1380 /* don't treat this as an error */
1383 p
= buf
+ 4 + 4*num_subkeys
;
1385 SIVAL(buf
, 0, num_subkeys
);
1387 for (i
=0; i
< num_subkeys
; i
++) {
1388 ptrdiff_t offset
= p
- buf
;
1389 SIVAL(buf
, 4 + 4*i
, offset
);
1390 strlcpy(p
, sorted_subkeys
[i
], len
-offset
);
1391 p
+= strlen(sorted_subkeys
[i
]) + 1;
1394 status
= dbwrap_store_bystring(
1395 db
, sorted_ctx
->sorted_keyname
, make_tdb_data((uint8_t *)buf
,
1404 static NTSTATUS
create_sorted_subkeys_internal(const char *key
,
1405 const char *sorted_keyname
)
1408 struct create_sorted_subkeys_context sorted_ctx
;
1410 sorted_ctx
.key
= key
;
1411 sorted_ctx
.sorted_keyname
= sorted_keyname
;
1413 status
= dbwrap_trans_do(regdb
,
1414 create_sorted_subkeys_action
,
1420 static NTSTATUS
create_sorted_subkeys(const char *key
)
1422 char *sorted_subkeys_keyname
;
1425 sorted_subkeys_keyname
= talloc_asprintf(talloc_tos(), "%s\\%s",
1426 REG_SORTED_SUBKEYS_PREFIX
,
1428 if (sorted_subkeys_keyname
== NULL
) {
1429 status
= NT_STATUS_NO_MEMORY
;
1433 status
= create_sorted_subkeys_internal(key
, sorted_subkeys_keyname
);
1439 struct scan_subkey_state
{
1445 static int parent_subkey_scanner(TDB_DATA key
, TDB_DATA data
,
1448 struct scan_subkey_state
*state
=
1449 (struct scan_subkey_state
*)private_data
;
1450 uint32_t num_subkeys
;
1453 if (data
.dsize
< sizeof(uint32_t)) {
1457 state
->scanned
= true;
1458 state
->found
= false;
1460 tdb_unpack(data
.dptr
, data
.dsize
, "d", &num_subkeys
);
1466 uint32_t idx
= (l
+u
)/2;
1467 char *s
= (char *)data
.dptr
+ IVAL(data
.dptr
, 4 + 4*idx
);
1468 int comparison
= strcmp(state
->name
, s
);
1470 if (comparison
< 0) {
1472 } else if (comparison
> 0) {
1475 state
->found
= true;
1482 static bool scan_parent_subkeys(struct db_context
*db
, const char *parent
,
1487 struct scan_subkey_state state
= { 0, };
1488 bool result
= false;
1493 path
= normalize_reg_path(talloc_tos(), parent
);
1498 key
= talloc_asprintf(talloc_tos(), "%s\\%s",
1499 REG_SORTED_SUBKEYS_PREFIX
, path
);
1504 state
.name
= talloc_strdup_upper(talloc_tos(), name
);
1505 if (state
.name
== NULL
) {
1508 state
.scanned
= false;
1510 res
= db
->parse_record(db
, string_term_tdb_data(key
),
1511 parent_subkey_scanner
, &state
);
1513 if (state
.scanned
) {
1514 result
= state
.found
;
1518 res
= db
->transaction_start(db
);
1520 DEBUG(0, ("error starting transaction\n"));
1524 DEBUG(2, (__location__
" WARNING: recreating the sorted "
1525 "subkeys cache for key '%s' from scan_parent_subkeys "
1526 "this should not happen (too frequently)...\n",
1529 status
= create_sorted_subkeys_internal(path
, key
);
1530 if (!NT_STATUS_IS_OK(status
)) {
1531 res
= db
->transaction_cancel(db
);
1533 smb_panic("Failed to cancel transaction.");
1538 res
= db
->parse_record(db
, string_term_tdb_data(key
),
1539 parent_subkey_scanner
, &state
);
1540 if ((res
== 0) && (state
.scanned
)) {
1541 result
= state
.found
;
1544 res
= db
->transaction_commit(db
);
1546 DEBUG(0, ("error committing transaction\n"));
1553 TALLOC_FREE(state
.name
);
1558 * Check for the existence of a key.
1560 * Existence of a key is authoritatively defined by its
1561 * existence in the list of subkeys of its parent key.
1562 * The exeption of this are keys without a parent key,
1563 * i.e. the "base" keys (HKLM, HKCU, ...).
1565 static bool regdb_key_exists(struct db_context
*db
, const char *key
)
1567 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
1576 path
= normalize_reg_path(mem_ctx
, key
);
1578 DEBUG(0, ("out of memory! (talloc failed)\n"));
1582 if (*path
== '\0') {
1586 p
= strrchr(path
, '\\');
1588 /* this is a base key */
1589 value
= regdb_fetch_key_internal(db
, mem_ctx
, path
);
1590 ret
= (value
.dptr
!= NULL
);
1593 ret
= scan_parent_subkeys(db
, path
, p
+1);
1597 TALLOC_FREE(mem_ctx
);
1602 /***********************************************************************
1603 Retrieve an array of strings containing subkeys. Memory should be
1604 released by the caller.
1605 ***********************************************************************/
1607 static WERROR
regdb_fetch_keys_internal(struct db_context
*db
, const char *key
,
1608 struct regsubkey_ctr
*ctr
)
1616 TALLOC_CTX
*frame
= talloc_stackframe();
1618 int seqnum
[2], count
;
1620 DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key
? key
: "NULL"));
1622 if (!regdb_key_exists(db
, key
)) {
1623 DEBUG(10, ("key [%s] not found\n", key
));
1624 werr
= WERR_NOT_FOUND
;
1628 werr
= regsubkey_ctr_reinit(ctr
);
1629 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1633 seqnum
[0] = db
->get_seqnum(db
);
1637 TALLOC_FREE(value
.dptr
);
1638 value
= regdb_fetch_key_internal(db
, frame
, key
);
1639 seqnum
[count
% 2] = db
->get_seqnum(db
);
1641 } while (seqnum
[0] != seqnum
[1]);
1644 DEBUG(5, ("regdb_fetch_keys_internal: it took %d attempts to "
1645 "fetch key '%s' with constant seqnum\n",
1649 werr
= regsubkey_ctr_set_seqnum(ctr
, seqnum
[0]);
1650 if (!W_ERROR_IS_OK(werr
)) {
1654 if (value
.dsize
== 0 || value
.dptr
== NULL
) {
1655 DEBUG(10, ("regdb_fetch_keys: no subkeys found for key [%s]\n",
1661 buflen
= value
.dsize
;
1662 len
= tdb_unpack( buf
, buflen
, "d", &num_items
);
1663 if (len
== (uint32_t)-1) {
1664 werr
= WERR_NOT_FOUND
;
1668 for (i
=0; i
<num_items
; i
++) {
1669 len
+= tdb_unpack(buf
+len
, buflen
-len
, "f", subkeyname
);
1670 werr
= regsubkey_ctr_addkey(ctr
, subkeyname
);
1671 if (!W_ERROR_IS_OK(werr
)) {
1672 DEBUG(5, ("regdb_fetch_keys: regsubkey_ctr_addkey "
1673 "failed: %s\n", win_errstr(werr
)));
1679 DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items
));
1686 int regdb_fetch_keys(const char *key
, struct regsubkey_ctr
*ctr
)
1690 werr
= regdb_fetch_keys_internal(regdb
, key
, ctr
);
1691 if (!W_ERROR_IS_OK(werr
)) {
1695 return regsubkey_ctr_numkeys(ctr
);
1698 /****************************************************************************
1699 Unpack a list of registry values frem the TDB
1700 ***************************************************************************/
1702 static int regdb_unpack_values(struct regval_ctr
*values
, uint8
*buf
, int buflen
)
1709 uint32 num_values
= 0;
1712 /* loop and unpack the rest of the registry values */
1714 len
+= tdb_unpack(buf
+len
, buflen
-len
, "d", &num_values
);
1716 for ( i
=0; i
<num_values
; i
++ ) {
1717 /* unpack the next regval */
1722 valuename
[0] = '\0';
1723 len
+= tdb_unpack(buf
+len
, buflen
-len
, "fdB",
1729 regval_ctr_addvalue(values
, valuename
, type
,
1730 (uint8_t *)data_p
, size
);
1731 SAFE_FREE(data_p
); /* 'B' option to tdb_unpack does a malloc() */
1733 DEBUG(10, ("regdb_unpack_values: value[%d]: name[%s] len[%d]\n",
1734 i
, valuename
, size
));
1740 /****************************************************************************
1741 Pack all values in all printer keys
1742 ***************************************************************************/
1744 static int regdb_pack_values(struct regval_ctr
*values
, uint8
*buf
, int buflen
)
1748 struct regval_blob
*val
;
1754 num_values
= regval_ctr_numvals( values
);
1756 /* pack the number of values first */
1758 len
+= tdb_pack( buf
+len
, buflen
-len
, "d", num_values
);
1760 /* loop over all values */
1762 for ( i
=0; i
<num_values
; i
++ ) {
1763 val
= regval_ctr_specific_value( values
, i
);
1764 len
+= tdb_pack(buf
+len
, buflen
-len
, "fdB",
1768 regval_data_p(val
) );
1774 /***********************************************************************
1775 Retrieve an array of strings containing subkeys. Memory should be
1776 released by the caller.
1777 ***********************************************************************/
1779 static int regdb_fetch_values_internal(struct db_context
*db
, const char* key
,
1780 struct regval_ctr
*values
)
1782 char *keystr
= NULL
;
1783 TALLOC_CTX
*ctx
= talloc_stackframe();
1787 int seqnum
[2], count
;
1789 DEBUG(10,("regdb_fetch_values: Looking for values of key [%s]\n", key
));
1791 if (!regdb_key_exists(db
, key
)) {
1792 DEBUG(10, ("regb_fetch_values: key [%s] does not exist\n",
1798 keystr
= talloc_asprintf(ctx
, "%s\\%s", REG_VALUE_PREFIX
, key
);
1805 seqnum
[0] = db
->get_seqnum(db
);
1809 TALLOC_FREE(value
.dptr
);
1810 value
= regdb_fetch_key_internal(db
, ctx
, keystr
);
1811 seqnum
[count
% 2] = db
->get_seqnum(db
);
1812 } while (seqnum
[0] != seqnum
[1]);
1815 DEBUG(5, ("regdb_fetch_values_internal: it took %d attempts "
1816 "to fetch key '%s' with constant seqnum\n",
1820 werr
= regval_ctr_set_seqnum(values
, seqnum
[0]);
1821 if (!W_ERROR_IS_OK(werr
)) {
1826 /* all keys have zero values by default */
1830 regdb_unpack_values(values
, value
.dptr
, value
.dsize
);
1831 ret
= regval_ctr_numvals(values
);
1838 int regdb_fetch_values(const char* key
, struct regval_ctr
*values
)
1840 return regdb_fetch_values_internal(regdb
, key
, values
);
1843 static bool regdb_store_values_internal(struct db_context
*db
, const char *key
,
1844 struct regval_ctr
*values
)
1846 TDB_DATA old_data
, data
;
1847 char *keystr
= NULL
;
1848 TALLOC_CTX
*ctx
= talloc_stackframe();
1851 bool result
= false;
1854 DEBUG(10,("regdb_store_values: Looking for values of key [%s]\n", key
));
1856 if (!regdb_key_exists(db
, key
)) {
1862 len
= regdb_pack_values(values
, data
.dptr
, data
.dsize
);
1864 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
1868 data
.dptr
= TALLOC_ARRAY(ctx
, uint8
, len
);
1871 len
= regdb_pack_values(values
, data
.dptr
, data
.dsize
);
1873 SMB_ASSERT( len
== data
.dsize
);
1875 keystr
= talloc_asprintf(ctx
, "%s\\%s", REG_VALUE_PREFIX
, key
);
1879 keystr
= normalize_reg_path(ctx
, keystr
);
1884 old_data
= dbwrap_fetch_bystring(db
, ctx
, keystr
);
1886 if ((old_data
.dptr
!= NULL
)
1887 && (old_data
.dsize
== data
.dsize
)
1888 && (memcmp(old_data
.dptr
, data
.dptr
, data
.dsize
) == 0))
1894 status
= dbwrap_trans_store_bystring(db
, keystr
, data
, TDB_REPLACE
);
1895 if (!NT_STATUS_IS_OK(status
)) {
1896 DEBUG(0, ("regdb_store_values_internal: error storing: %s\n", nt_errstr(status
)));
1901 * update the seqnum in the cache to prevent the next read
1902 * from going to disk
1904 werr
= regval_ctr_set_seqnum(values
, db
->get_seqnum(db
));
1905 result
= W_ERROR_IS_OK(status
);
1912 bool regdb_store_values(const char *key
, struct regval_ctr
*values
)
1914 return regdb_store_values_internal(regdb
, key
, values
);
1917 static WERROR
regdb_get_secdesc(TALLOC_CTX
*mem_ctx
, const char *key
,
1918 struct security_descriptor
**psecdesc
)
1923 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
1924 WERROR err
= WERR_OK
;
1926 DEBUG(10, ("regdb_get_secdesc: Getting secdesc of key [%s]\n", key
));
1928 if (!regdb_key_exists(regdb
, key
)) {
1933 tdbkey
= talloc_asprintf(tmp_ctx
, "%s\\%s", REG_SECDESC_PREFIX
, key
);
1934 if (tdbkey
== NULL
) {
1939 tdbkey
= normalize_reg_path(tmp_ctx
, tdbkey
);
1940 if (tdbkey
== NULL
) {
1945 data
= dbwrap_fetch_bystring(regdb
, tmp_ctx
, tdbkey
);
1946 if (data
.dptr
== NULL
) {
1951 status
= unmarshall_sec_desc(mem_ctx
, (uint8
*)data
.dptr
, data
.dsize
,
1954 if (NT_STATUS_EQUAL(status
, NT_STATUS_NO_MEMORY
)) {
1956 } else if (!NT_STATUS_IS_OK(status
)) {
1957 err
= WERR_REG_CORRUPT
;
1961 TALLOC_FREE(tmp_ctx
);
1965 static WERROR
regdb_set_secdesc(const char *key
,
1966 struct security_descriptor
*secdesc
)
1968 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
1970 WERROR err
= WERR_NOMEM
;
1973 if (!regdb_key_exists(regdb
, key
)) {
1978 tdbkey
= talloc_asprintf(mem_ctx
, "%s\\%s", REG_SECDESC_PREFIX
, key
);
1979 if (tdbkey
== NULL
) {
1983 tdbkey
= normalize_reg_path(mem_ctx
, tdbkey
);
1984 if (tdbkey
== NULL
) {
1989 if (secdesc
== NULL
) {
1990 /* assuming a delete */
1991 err
= ntstatus_to_werror(dbwrap_trans_delete_bystring(regdb
,
1996 err
= ntstatus_to_werror(marshall_sec_desc(mem_ctx
, secdesc
,
1999 W_ERROR_NOT_OK_GOTO_DONE(err
);
2001 err
= ntstatus_to_werror(dbwrap_trans_store_bystring(regdb
, tdbkey
,
2005 TALLOC_FREE(mem_ctx
);
2009 bool regdb_subkeys_need_update(struct regsubkey_ctr
*subkeys
)
2011 return (regdb_get_seqnum() != regsubkey_ctr_get_seqnum(subkeys
));
2014 bool regdb_values_need_update(struct regval_ctr
*values
)
2016 return (regdb_get_seqnum() != regval_ctr_get_seqnum(values
));
2020 * Table of function pointers for default access
2023 struct registry_ops regdb_ops
= {
2024 .fetch_subkeys
= regdb_fetch_keys
,
2025 .fetch_values
= regdb_fetch_values
,
2026 .store_subkeys
= regdb_store_keys
,
2027 .store_values
= regdb_store_values
,
2028 .create_subkey
= regdb_create_subkey
,
2029 .delete_subkey
= regdb_delete_subkey
,
2030 .get_secdesc
= regdb_get_secdesc
,
2031 .set_secdesc
= regdb_set_secdesc
,
2032 .subkeys_need_update
= regdb_subkeys_need_update
,
2033 .values_need_update
= regdb_values_need_update