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_key_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_key_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
);
990 talloc_free(mem_ctx
);
991 return werror_to_ntstatus(werr
);
994 static bool regdb_store_keys_internal(struct db_context
*db
, const char *key
,
995 struct regsubkey_ctr
*ctr
)
997 int num_subkeys
, old_num_subkeys
, i
;
998 struct regsubkey_ctr
*old_subkeys
= NULL
;
999 TALLOC_CTX
*ctx
= talloc_stackframe();
1002 struct regdb_store_keys_context store_ctx
;
1004 if (!regdb_key_is_base_key(key
) && !regdb_key_exists(db
, key
)) {
1009 * fetch a list of the old subkeys so we can determine if anything has
1013 werr
= regsubkey_ctr_init(ctx
, &old_subkeys
);
1014 if (!W_ERROR_IS_OK(werr
)) {
1015 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
1019 werr
= regdb_fetch_keys_internal(db
, key
, old_subkeys
);
1020 if (!W_ERROR_IS_OK(werr
) &&
1021 !W_ERROR_EQUAL(werr
, WERR_NOT_FOUND
))
1026 num_subkeys
= regsubkey_ctr_numkeys(ctr
);
1027 old_num_subkeys
= regsubkey_ctr_numkeys(old_subkeys
);
1028 if ((num_subkeys
&& old_num_subkeys
) &&
1029 (num_subkeys
== old_num_subkeys
)) {
1031 for (i
= 0; i
< num_subkeys
; i
++) {
1032 if (strcmp(regsubkey_ctr_specific_key(ctr
, i
),
1033 regsubkey_ctr_specific_key(old_subkeys
, i
))
1039 if (i
== num_subkeys
) {
1041 * Nothing changed, no point to even start a tdb
1050 TALLOC_FREE(old_subkeys
);
1052 store_ctx
.key
= key
;
1053 store_ctx
.ctr
= ctr
;
1055 werr
= ntstatus_to_werror(dbwrap_trans_do(db
,
1056 regdb_store_keys_action
,
1059 ret
= W_ERROR_IS_OK(werr
);
1067 bool regdb_store_keys(const char *key
, struct regsubkey_ctr
*ctr
)
1069 return regdb_store_keys_internal(regdb
, key
, ctr
);
1073 * create a subkey of a given key
1076 struct regdb_create_subkey_context
{
1081 static NTSTATUS
regdb_create_subkey_action(struct db_context
*db
,
1085 struct regdb_create_subkey_context
*create_ctx
;
1086 struct regsubkey_ctr
*subkeys
;
1087 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
1089 create_ctx
= (struct regdb_create_subkey_context
*)private_data
;
1091 werr
= regsubkey_ctr_init(mem_ctx
, &subkeys
);
1092 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1094 werr
= regdb_fetch_keys_internal(db
, create_ctx
->key
, subkeys
);
1095 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1097 werr
= regsubkey_ctr_addkey(subkeys
, create_ctx
->subkey
);
1098 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1100 werr
= regdb_store_keys_internal2(db
, create_ctx
->key
, subkeys
);
1101 if (!W_ERROR_IS_OK(werr
)) {
1102 DEBUG(0, (__location__
" failed to store new subkey list for "
1103 "parent key %s: %s\n", create_ctx
->key
,
1108 talloc_free(mem_ctx
);
1109 return werror_to_ntstatus(werr
);
1112 static WERROR
regdb_create_subkey(const char *key
, const char *subkey
)
1115 struct regsubkey_ctr
*subkeys
;
1116 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
1117 struct regdb_create_subkey_context create_ctx
;
1119 if (!regdb_key_is_base_key(key
) && !regdb_key_exists(regdb
, key
)) {
1120 werr
= WERR_NOT_FOUND
;
1124 werr
= regsubkey_ctr_init(mem_ctx
, &subkeys
);
1125 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1127 werr
= regdb_fetch_keys_internal(regdb
, key
, subkeys
);
1128 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1130 if (regsubkey_ctr_key_exists(subkeys
, subkey
)) {
1135 talloc_free(subkeys
);
1137 create_ctx
.key
= key
;
1138 create_ctx
.subkey
= subkey
;
1140 werr
= ntstatus_to_werror(dbwrap_trans_do(regdb
,
1141 regdb_create_subkey_action
,
1145 talloc_free(mem_ctx
);
1150 * create a subkey of a given key
1153 struct regdb_delete_subkey_context
{
1159 static NTSTATUS
regdb_delete_subkey_action(struct db_context
*db
,
1163 struct regdb_delete_subkey_context
*delete_ctx
;
1164 struct regsubkey_ctr
*subkeys
;
1165 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
1167 delete_ctx
= (struct regdb_delete_subkey_context
*)private_data
;
1169 werr
= regdb_delete_key_lists(db
, delete_ctx
->path
);
1170 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1172 werr
= regsubkey_ctr_init(mem_ctx
, &subkeys
);
1173 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1175 werr
= regdb_fetch_keys_internal(db
, delete_ctx
->key
, subkeys
);
1176 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1178 werr
= regsubkey_ctr_delkey(subkeys
, delete_ctx
->subkey
);
1179 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1181 werr
= regdb_store_keys_internal2(db
, delete_ctx
->key
, subkeys
);
1182 if (!W_ERROR_IS_OK(werr
)) {
1183 DEBUG(0, (__location__
" failed to store new subkey_list for "
1184 "parent key %s: %s\n", delete_ctx
->key
,
1189 talloc_free(mem_ctx
);
1190 return werror_to_ntstatus(werr
);
1193 static WERROR
regdb_delete_subkey(const char *key
, const char *subkey
)
1197 struct regdb_delete_subkey_context delete_ctx
;
1198 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
1200 if (!regdb_key_is_base_key(key
) && !regdb_key_exists(regdb
, key
)) {
1201 werr
= WERR_NOT_FOUND
;
1205 path
= talloc_asprintf(mem_ctx
, "%s\\%s", key
, subkey
);
1211 if (!regdb_key_exists(regdb
, path
)) {
1216 delete_ctx
.key
= key
;
1217 delete_ctx
.subkey
= subkey
;
1218 delete_ctx
.path
= path
;
1220 werr
= ntstatus_to_werror(dbwrap_trans_do(regdb
,
1221 regdb_delete_subkey_action
,
1225 talloc_free(mem_ctx
);
1229 static TDB_DATA
regdb_fetch_key_internal(struct db_context
*db
,
1230 TALLOC_CTX
*mem_ctx
, const char *key
)
1235 path
= normalize_reg_path(mem_ctx
, key
);
1237 return make_tdb_data(NULL
, 0);
1240 data
= dbwrap_fetch_bystring(db
, mem_ctx
, path
);
1248 * check whether a given key name represents a base key,
1249 * i.e one without a subkey separator ('\').
1251 static bool regdb_key_is_base_key(const char *key
)
1253 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
1261 path
= normalize_reg_path(mem_ctx
, key
);
1263 DEBUG(0, ("out of memory! (talloc failed)\n"));
1267 if (*path
== '\0') {
1271 ret
= (strrchr(path
, '\\') == NULL
);
1274 TALLOC_FREE(mem_ctx
);
1279 * regdb_key_exists() is a very frequent operation. It can be quite
1280 * time-consuming to fully fetch the parent's subkey list, talloc_strdup all
1281 * subkeys and then compare the keyname linearly to all the parent's subkeys.
1283 * The following code tries to make this operation as efficient as possible:
1284 * Per registry key we create a list of subkeys that is very efficient to
1285 * search for existence of a subkey. Its format is:
1287 * 4 bytes num_subkeys
1288 * 4*num_subkey bytes offset into the string array
1289 * then follows a sorted list of subkeys in uppercase
1291 * This record is created by create_sorted_subkeys() on demand if it does not
1292 * exist. scan_parent_subkeys() uses regdb->parse_record to search the sorted
1293 * list, the parsing code and the binary search can be found in
1294 * parent_subkey_scanner. The code uses parse_record() to avoid a memcpy of
1295 * the potentially large subkey record.
1297 * The sorted subkey record is deleted in regdb_store_keys_internal2 and
1298 * recreated on demand.
1301 static int cmp_keynames(char **p1
, char **p2
)
1303 return StrCaseCmp(*p1
, *p2
);
1306 struct create_sorted_subkeys_context
{
1308 const char *sorted_keyname
;
1311 static NTSTATUS
create_sorted_subkeys_action(struct db_context
*db
,
1314 char **sorted_subkeys
;
1315 struct regsubkey_ctr
*ctr
;
1322 struct create_sorted_subkeys_context
*sorted_ctx
;
1324 sorted_ctx
= (struct create_sorted_subkeys_context
*)private_data
;
1327 * In this function, we only treat failing of the actual write to
1328 * the db as a real error. All preliminary errors, at a stage when
1329 * nothing has been written to the DB yet are treated as success
1330 * to be committed (as an empty transaction).
1332 * The reason is that this (disposable) call might be nested in other
1333 * transactions. Doing a cancel here would destroy the possibility of
1334 * a transaction_commit for transactions that we might be wrapped in.
1337 status
= werror_to_ntstatus(regsubkey_ctr_init(talloc_tos(), &ctr
));
1338 if (!NT_STATUS_IS_OK(status
)) {
1339 /* don't treat this as an error */
1340 status
= NT_STATUS_OK
;
1344 status
= werror_to_ntstatus(regdb_fetch_keys_internal(db
,
1347 if (!NT_STATUS_IS_OK(status
)) {
1348 /* don't treat this as an error */
1349 status
= NT_STATUS_OK
;
1353 num_subkeys
= regsubkey_ctr_numkeys(ctr
);
1354 sorted_subkeys
= talloc_array(ctr
, char *, num_subkeys
);
1355 if (sorted_subkeys
== NULL
) {
1356 /* don't treat this as an error */
1360 len
= 4 + 4*num_subkeys
;
1362 for (i
= 0; i
< num_subkeys
; i
++) {
1363 sorted_subkeys
[i
] = talloc_strdup_upper(sorted_subkeys
,
1364 regsubkey_ctr_specific_key(ctr
, i
));
1365 if (sorted_subkeys
[i
] == NULL
) {
1366 /* don't treat this as an error */
1369 len
+= strlen(sorted_subkeys
[i
])+1;
1372 TYPESAFE_QSORT(sorted_subkeys
, num_subkeys
, cmp_keynames
);
1374 buf
= talloc_array(ctr
, char, len
);
1376 /* don't treat this as an error */
1379 p
= buf
+ 4 + 4*num_subkeys
;
1381 SIVAL(buf
, 0, num_subkeys
);
1383 for (i
=0; i
< num_subkeys
; i
++) {
1384 ptrdiff_t offset
= p
- buf
;
1385 SIVAL(buf
, 4 + 4*i
, offset
);
1386 strlcpy(p
, sorted_subkeys
[i
], len
-offset
);
1387 p
+= strlen(sorted_subkeys
[i
]) + 1;
1390 status
= dbwrap_store_bystring(
1391 db
, sorted_ctx
->sorted_keyname
, make_tdb_data((uint8_t *)buf
,
1400 static NTSTATUS
create_sorted_subkeys_internal(const char *key
,
1401 const char *sorted_keyname
)
1404 struct create_sorted_subkeys_context sorted_ctx
;
1406 sorted_ctx
.key
= key
;
1407 sorted_ctx
.sorted_keyname
= sorted_keyname
;
1409 status
= dbwrap_trans_do(regdb
,
1410 create_sorted_subkeys_action
,
1416 static NTSTATUS
create_sorted_subkeys(const char *key
)
1418 char *sorted_subkeys_keyname
;
1421 sorted_subkeys_keyname
= talloc_asprintf(talloc_tos(), "%s\\%s",
1422 REG_SORTED_SUBKEYS_PREFIX
,
1424 if (sorted_subkeys_keyname
== NULL
) {
1425 status
= NT_STATUS_NO_MEMORY
;
1429 status
= create_sorted_subkeys_internal(key
, sorted_subkeys_keyname
);
1435 struct scan_subkey_state
{
1441 static int parent_subkey_scanner(TDB_DATA key
, TDB_DATA data
,
1444 struct scan_subkey_state
*state
=
1445 (struct scan_subkey_state
*)private_data
;
1446 uint32_t num_subkeys
;
1449 if (data
.dsize
< sizeof(uint32_t)) {
1453 state
->scanned
= true;
1454 state
->found
= false;
1456 tdb_unpack(data
.dptr
, data
.dsize
, "d", &num_subkeys
);
1462 uint32_t idx
= (l
+u
)/2;
1463 char *s
= (char *)data
.dptr
+ IVAL(data
.dptr
, 4 + 4*idx
);
1464 int comparison
= strcmp(state
->name
, s
);
1466 if (comparison
< 0) {
1468 } else if (comparison
> 0) {
1471 state
->found
= true;
1478 static bool scan_parent_subkeys(struct db_context
*db
, const char *parent
,
1483 struct scan_subkey_state state
= { 0, };
1484 bool result
= false;
1489 path
= normalize_reg_path(talloc_tos(), parent
);
1494 key
= talloc_asprintf(talloc_tos(), "%s\\%s",
1495 REG_SORTED_SUBKEYS_PREFIX
, path
);
1500 state
.name
= talloc_strdup_upper(talloc_tos(), name
);
1501 if (state
.name
== NULL
) {
1504 state
.scanned
= false;
1506 res
= db
->parse_record(db
, string_term_tdb_data(key
),
1507 parent_subkey_scanner
, &state
);
1509 if (state
.scanned
) {
1510 result
= state
.found
;
1514 res
= db
->transaction_start(db
);
1516 DEBUG(0, ("error starting transaction\n"));
1520 DEBUG(2, (__location__
" WARNING: recreating the sorted "
1521 "subkeys cache for key '%s' from scan_parent_subkeys "
1522 "this should not happen (too frequently)...\n",
1525 status
= create_sorted_subkeys_internal(path
, key
);
1526 if (!NT_STATUS_IS_OK(status
)) {
1527 res
= db
->transaction_cancel(db
);
1529 smb_panic("Failed to cancel transaction.");
1534 res
= db
->parse_record(db
, string_term_tdb_data(key
),
1535 parent_subkey_scanner
, &state
);
1536 if ((res
== 0) && (state
.scanned
)) {
1537 result
= state
.found
;
1540 res
= db
->transaction_commit(db
);
1542 DEBUG(0, ("error committing transaction\n"));
1549 TALLOC_FREE(state
.name
);
1554 * Check for the existence of a key.
1556 * Existence of a key is authoritatively defined by its
1557 * existence in the list of subkeys of its parent key.
1558 * The exeption of this are keys without a parent key,
1559 * i.e. the "base" keys (HKLM, HKCU, ...).
1561 static bool regdb_key_exists(struct db_context
*db
, const char *key
)
1563 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
1572 path
= normalize_reg_path(mem_ctx
, key
);
1574 DEBUG(0, ("out of memory! (talloc failed)\n"));
1578 if (*path
== '\0') {
1582 p
= strrchr(path
, '\\');
1584 /* this is a base key */
1585 value
= regdb_fetch_key_internal(db
, mem_ctx
, path
);
1586 ret
= (value
.dptr
!= NULL
);
1589 ret
= scan_parent_subkeys(db
, path
, p
+1);
1593 TALLOC_FREE(mem_ctx
);
1598 /***********************************************************************
1599 Retrieve an array of strings containing subkeys. Memory should be
1600 released by the caller.
1601 ***********************************************************************/
1603 static WERROR
regdb_fetch_keys_internal(struct db_context
*db
, const char *key
,
1604 struct regsubkey_ctr
*ctr
)
1612 TALLOC_CTX
*frame
= talloc_stackframe();
1615 DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key
? key
: "NULL"));
1617 if (!regdb_key_exists(db
, key
)) {
1618 DEBUG(10, ("key [%s] not found\n", key
));
1619 werr
= WERR_NOT_FOUND
;
1623 werr
= regsubkey_ctr_set_seqnum(ctr
, db
->get_seqnum(db
));
1624 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1626 value
= regdb_fetch_key_internal(db
, frame
, key
);
1628 if (value
.dsize
== 0 || value
.dptr
== NULL
) {
1629 DEBUG(10, ("regdb_fetch_keys: no subkeys found for key [%s]\n",
1635 buflen
= value
.dsize
;
1636 len
= tdb_unpack( buf
, buflen
, "d", &num_items
);
1637 if (len
== (uint32_t)-1) {
1638 werr
= WERR_NOT_FOUND
;
1642 werr
= regsubkey_ctr_reinit(ctr
);
1643 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1645 for (i
=0; i
<num_items
; i
++) {
1646 len
+= tdb_unpack(buf
+len
, buflen
-len
, "f", subkeyname
);
1647 werr
= regsubkey_ctr_addkey(ctr
, subkeyname
);
1648 if (!W_ERROR_IS_OK(werr
)) {
1649 DEBUG(5, ("regdb_fetch_keys: regsubkey_ctr_addkey "
1650 "failed: %s\n", win_errstr(werr
)));
1656 DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items
));
1663 int regdb_fetch_keys(const char *key
, struct regsubkey_ctr
*ctr
)
1667 werr
= regdb_fetch_keys_internal(regdb
, key
, ctr
);
1668 if (!W_ERROR_IS_OK(werr
)) {
1672 return regsubkey_ctr_numkeys(ctr
);
1675 /****************************************************************************
1676 Unpack a list of registry values frem the TDB
1677 ***************************************************************************/
1679 static int regdb_unpack_values(struct regval_ctr
*values
, uint8
*buf
, int buflen
)
1686 uint32 num_values
= 0;
1689 /* loop and unpack the rest of the registry values */
1691 len
+= tdb_unpack(buf
+len
, buflen
-len
, "d", &num_values
);
1693 for ( i
=0; i
<num_values
; i
++ ) {
1694 /* unpack the next regval */
1699 valuename
[0] = '\0';
1700 len
+= tdb_unpack(buf
+len
, buflen
-len
, "fdB",
1706 regval_ctr_addvalue(values
, valuename
, type
,
1707 (uint8_t *)data_p
, size
);
1708 SAFE_FREE(data_p
); /* 'B' option to tdb_unpack does a malloc() */
1710 DEBUG(8,("specific: [%s], len: %d\n", valuename
, size
));
1716 /****************************************************************************
1717 Pack all values in all printer keys
1718 ***************************************************************************/
1720 static int regdb_pack_values(struct regval_ctr
*values
, uint8
*buf
, int buflen
)
1724 struct regval_blob
*val
;
1730 num_values
= regval_ctr_numvals( values
);
1732 /* pack the number of values first */
1734 len
+= tdb_pack( buf
+len
, buflen
-len
, "d", num_values
);
1736 /* loop over all values */
1738 for ( i
=0; i
<num_values
; i
++ ) {
1739 val
= regval_ctr_specific_value( values
, i
);
1740 len
+= tdb_pack(buf
+len
, buflen
-len
, "fdB",
1744 regval_data_p(val
) );
1750 /***********************************************************************
1751 Retrieve an array of strings containing subkeys. Memory should be
1752 released by the caller.
1753 ***********************************************************************/
1755 static int regdb_fetch_values_internal(struct db_context
*db
, const char* key
,
1756 struct regval_ctr
*values
)
1758 char *keystr
= NULL
;
1759 TALLOC_CTX
*ctx
= talloc_stackframe();
1764 DEBUG(10,("regdb_fetch_values: Looking for value of key [%s] \n", key
));
1766 if (!regdb_key_exists(db
, key
)) {
1770 keystr
= talloc_asprintf(ctx
, "%s\\%s", REG_VALUE_PREFIX
, key
);
1775 werr
= regval_ctr_set_seqnum(values
, db
->get_seqnum(db
));
1776 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1778 value
= regdb_fetch_key_internal(db
, ctx
, keystr
);
1781 /* all keys have zero values by default */
1785 regdb_unpack_values(values
, value
.dptr
, value
.dsize
);
1786 ret
= regval_ctr_numvals(values
);
1793 int regdb_fetch_values(const char* key
, struct regval_ctr
*values
)
1795 return regdb_fetch_values_internal(regdb
, key
, values
);
1798 static bool regdb_store_values_internal(struct db_context
*db
, const char *key
,
1799 struct regval_ctr
*values
)
1801 TDB_DATA old_data
, data
;
1802 char *keystr
= NULL
;
1803 TALLOC_CTX
*ctx
= talloc_stackframe();
1806 bool result
= false;
1808 DEBUG(10,("regdb_store_values: Looking for value of key [%s] \n", key
));
1810 if (!regdb_key_exists(db
, key
)) {
1816 len
= regdb_pack_values(values
, data
.dptr
, data
.dsize
);
1818 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
1822 data
.dptr
= TALLOC_ARRAY(ctx
, uint8
, len
);
1825 len
= regdb_pack_values(values
, data
.dptr
, data
.dsize
);
1827 SMB_ASSERT( len
== data
.dsize
);
1829 keystr
= talloc_asprintf(ctx
, "%s\\%s", REG_VALUE_PREFIX
, key
);
1833 keystr
= normalize_reg_path(ctx
, keystr
);
1838 old_data
= dbwrap_fetch_bystring(db
, ctx
, keystr
);
1840 if ((old_data
.dptr
!= NULL
)
1841 && (old_data
.dsize
== data
.dsize
)
1842 && (memcmp(old_data
.dptr
, data
.dptr
, data
.dsize
) == 0))
1848 status
= dbwrap_trans_store_bystring(db
, keystr
, data
, TDB_REPLACE
);
1850 result
= NT_STATUS_IS_OK(status
);
1857 bool regdb_store_values(const char *key
, struct regval_ctr
*values
)
1859 return regdb_store_values_internal(regdb
, key
, values
);
1862 static WERROR
regdb_get_secdesc(TALLOC_CTX
*mem_ctx
, const char *key
,
1863 struct security_descriptor
**psecdesc
)
1868 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
1869 WERROR err
= WERR_OK
;
1871 DEBUG(10, ("regdb_get_secdesc: Getting secdesc of key [%s]\n", key
));
1873 if (!regdb_key_exists(regdb
, key
)) {
1878 tdbkey
= talloc_asprintf(tmp_ctx
, "%s\\%s", REG_SECDESC_PREFIX
, key
);
1879 if (tdbkey
== NULL
) {
1884 tdbkey
= normalize_reg_path(tmp_ctx
, tdbkey
);
1885 if (tdbkey
== NULL
) {
1890 data
= dbwrap_fetch_bystring(regdb
, tmp_ctx
, tdbkey
);
1891 if (data
.dptr
== NULL
) {
1896 status
= unmarshall_sec_desc(mem_ctx
, (uint8
*)data
.dptr
, data
.dsize
,
1899 if (NT_STATUS_EQUAL(status
, NT_STATUS_NO_MEMORY
)) {
1901 } else if (!NT_STATUS_IS_OK(status
)) {
1902 err
= WERR_REG_CORRUPT
;
1906 TALLOC_FREE(tmp_ctx
);
1910 static WERROR
regdb_set_secdesc(const char *key
,
1911 struct security_descriptor
*secdesc
)
1913 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
1915 WERROR err
= WERR_NOMEM
;
1918 if (!regdb_key_exists(regdb
, key
)) {
1923 tdbkey
= talloc_asprintf(mem_ctx
, "%s\\%s", REG_SECDESC_PREFIX
, key
);
1924 if (tdbkey
== NULL
) {
1928 tdbkey
= normalize_reg_path(mem_ctx
, tdbkey
);
1929 if (tdbkey
== NULL
) {
1934 if (secdesc
== NULL
) {
1935 /* assuming a delete */
1936 err
= ntstatus_to_werror(dbwrap_trans_delete_bystring(regdb
,
1941 err
= ntstatus_to_werror(marshall_sec_desc(mem_ctx
, secdesc
,
1944 W_ERROR_NOT_OK_GOTO_DONE(err
);
1946 err
= ntstatus_to_werror(dbwrap_trans_store_bystring(regdb
, tdbkey
,
1950 TALLOC_FREE(mem_ctx
);
1954 bool regdb_subkeys_need_update(struct regsubkey_ctr
*subkeys
)
1956 return (regdb_get_seqnum() != regsubkey_ctr_get_seqnum(subkeys
));
1959 bool regdb_values_need_update(struct regval_ctr
*values
)
1961 return (regdb_get_seqnum() != regval_ctr_get_seqnum(values
));
1965 * Table of function pointers for default access
1968 struct registry_ops regdb_ops
= {
1969 .fetch_subkeys
= regdb_fetch_keys
,
1970 .fetch_values
= regdb_fetch_values
,
1971 .store_subkeys
= regdb_store_keys
,
1972 .store_values
= regdb_store_values
,
1973 .create_subkey
= regdb_create_subkey
,
1974 .delete_subkey
= regdb_delete_subkey
,
1975 .get_secdesc
= regdb_get_secdesc
,
1976 .set_secdesc
= regdb_set_secdesc
,
1977 .subkeys_need_update
= regdb_subkeys_need_update
,
1978 .values_need_update
= regdb_values_need_update