2 * Unix SMB/CIFS implementation.
3 * Virtual Windows Registry Layer
4 * Copyright (C) Gerald Carter 2002-2005
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 /* Implementation of internal registry database functions. */
25 #define DBGC_CLASS DBGC_REGISTRY
27 static struct db_context
*regdb
= NULL
;
28 static int regdb_refcount
;
30 static bool regdb_key_exists(struct db_context
*db
, const char *key
);
31 static bool regdb_key_is_base_key(const char *key
);
32 static int regdb_fetch_keys_internal(struct db_context
*db
, const char *key
,
33 struct regsubkey_ctr
*ctr
);
34 static bool regdb_store_keys_internal(struct db_context
*db
, const char *key
,
35 struct regsubkey_ctr
*ctr
);
36 static int regdb_fetch_values_internal(struct db_context
*db
, const char* key
,
37 struct regval_ctr
*values
);
38 static bool regdb_store_values_internal(struct db_context
*db
, const char *key
,
39 struct regval_ctr
*values
);
41 /* List the deepest path into the registry. All part components will be created.*/
43 /* If you want to have a part of the path controlled by the tdb and part by
44 a virtual registry db (e.g. printing), then you have to list the deepest path.
45 For example,"HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion/Print"
46 allows the reg_db backend to handle everything up to
47 "HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion" and then we'll hook
48 the reg_printing backend onto the last component of the path (see
49 KEY_PRINTING_2K in include/rpc_reg.h) --jerry */
51 static const char *builtin_registry_paths
[] = {
61 KEY_SAMBA_GROUP_POLICY
,
62 KEY_GP_MACHINE_POLICY
,
63 KEY_GP_MACHINE_WIN_POLICY
,
66 KEY_GP_USER_WIN_POLICY
,
67 KEY_WINLOGON_GPEXT_PATH
,
68 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Print\\Monitors",
70 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\DefaultUserConfiguration",
79 struct builtin_regkey_value
{
81 const char *valuename
;
89 static struct builtin_regkey_value builtin_registry_values
[] = {
91 SAMBA_PRINTER_PORT_NAME
, REG_SZ
, { "" } },
93 "DefaultSpoolDirectory", REG_SZ
, { "C:\\Windows\\System32\\Spool\\Printers" } },
95 "DisplayName", REG_SZ
, { "Event Log" } },
97 "ErrorControl", REG_DWORD
, { (char*)0x00000001 } },
98 { NULL
, NULL
, 0, { NULL
} }
102 * Initialize a key in the registry:
103 * create each component key of the specified path.
105 static WERROR
init_registry_key_internal(struct db_context
*db
,
106 const char *add_path
)
109 TALLOC_CTX
*frame
= talloc_stackframe();
112 char *remaining
= NULL
;
115 struct regsubkey_ctr
*subkeys
;
118 DEBUG(6, ("init_registry_key: Adding [%s]\n", add_path
));
120 path
= talloc_strdup(frame
, add_path
);
121 base
= talloc_strdup(frame
, "");
122 if (!path
|| !base
) {
128 while (next_token_talloc(frame
, &p
, &keyname
, "\\")) {
130 /* build up the registry path from the components */
133 base
= talloc_asprintf(frame
, "%s\\", base
);
139 base
= talloc_asprintf_append(base
, "%s", keyname
);
145 /* get the immediate subkeyname (if we have one ) */
147 subkeyname
= talloc_strdup(frame
, "");
153 remaining
= talloc_strdup(frame
, p
);
160 if (!next_token_talloc(frame
, &p2
,
163 subkeyname
= talloc_strdup(frame
,p2
);
171 DEBUG(10,("init_registry_key: Storing key [%s] with "
172 "subkey [%s]\n", base
,
173 *subkeyname
? subkeyname
: "NULL"));
175 /* we don't really care if the lookup succeeds or not
176 * since we are about to update the record.
177 * We just want any subkeys already present */
179 werr
= regsubkey_ctr_init(frame
, &subkeys
);
180 if (!W_ERROR_IS_OK(werr
)) {
181 DEBUG(0,("talloc() failure!\n"));
185 regdb_fetch_keys_internal(db
, base
, subkeys
);
187 werr
= regsubkey_ctr_addkey(subkeys
, subkeyname
);
188 if (!W_ERROR_IS_OK(werr
)) {
192 if (!regdb_store_keys_internal(db
, base
, subkeys
)) {
193 werr
= WERR_CAN_NOT_COMPLETE
;
205 struct init_registry_key_context
{
206 const char *add_path
;
209 static NTSTATUS
init_registry_key_action(struct db_context
*db
,
212 struct init_registry_key_context
*init_ctx
=
213 (struct init_registry_key_context
*)private_data
;
215 return werror_to_ntstatus(init_registry_key_internal(
216 db
, init_ctx
->add_path
));
220 * Initialize a key in the registry:
221 * create each component key of the specified path,
222 * wrapped in one db transaction.
224 WERROR
init_registry_key(const char *add_path
)
226 struct init_registry_key_context init_ctx
;
228 if (regdb_key_exists(regdb
, add_path
)) {
232 init_ctx
.add_path
= add_path
;
234 return ntstatus_to_werror(dbwrap_trans_do(regdb
,
235 init_registry_key_action
,
239 /***********************************************************************
240 Open the registry data in the tdb
241 ***********************************************************************/
243 static void regdb_ctr_add_value(struct regval_ctr
*ctr
,
244 struct builtin_regkey_value
*value
)
248 switch(value
->type
) {
250 regval_ctr_addvalue(ctr
, value
->valuename
, REG_DWORD
,
251 (char*)&value
->data
.dw_value
,
256 init_unistr2(&data
, value
->data
.string
, UNI_STR_TERMINATE
);
257 regval_ctr_addvalue(ctr
, value
->valuename
, REG_SZ
,
259 data
.uni_str_len
*sizeof(uint16
));
263 DEBUG(0, ("regdb_ctr_add_value: invalid value type in "
264 "registry values [%d]\n", value
->type
));
268 static NTSTATUS
init_registry_data_action(struct db_context
*db
,
272 TALLOC_CTX
*frame
= talloc_stackframe();
273 struct regval_ctr
*values
;
276 /* loop over all of the predefined paths and add each component */
278 for (i
=0; builtin_registry_paths
[i
] != NULL
; i
++) {
279 if (regdb_key_exists(db
, builtin_registry_paths
[i
])) {
282 status
= werror_to_ntstatus(init_registry_key_internal(db
,
283 builtin_registry_paths
[i
]));
284 if (!NT_STATUS_IS_OK(status
)) {
289 /* loop over all of the predefined values and add each component */
291 for (i
=0; builtin_registry_values
[i
].path
!= NULL
; i
++) {
293 values
= TALLOC_ZERO_P(frame
, struct regval_ctr
);
294 if (values
== NULL
) {
295 status
= NT_STATUS_NO_MEMORY
;
299 regdb_fetch_values_internal(db
,
300 builtin_registry_values
[i
].path
,
303 /* preserve existing values across restarts. Only add new ones */
305 if (!regval_ctr_key_exists(values
,
306 builtin_registry_values
[i
].valuename
))
308 regdb_ctr_add_value(values
,
309 &builtin_registry_values
[i
]);
310 regdb_store_values_internal(db
,
311 builtin_registry_values
[i
].path
,
317 status
= NT_STATUS_OK
;
325 WERROR
init_registry_data(void)
328 TALLOC_CTX
*frame
= talloc_stackframe();
329 struct regval_ctr
*values
;
333 * First, check for the existence of the needed keys and values.
334 * If all do already exist, we can save the writes.
336 for (i
=0; builtin_registry_paths
[i
] != NULL
; i
++) {
337 if (!regdb_key_exists(regdb
, builtin_registry_paths
[i
])) {
342 for (i
=0; builtin_registry_values
[i
].path
!= NULL
; i
++) {
343 values
= TALLOC_ZERO_P(frame
, struct regval_ctr
);
344 if (values
== NULL
) {
349 regdb_fetch_values_internal(regdb
,
350 builtin_registry_values
[i
].path
,
352 if (!regval_ctr_key_exists(values
,
353 builtin_registry_values
[i
].valuename
))
368 * There are potentially quite a few store operations which are all
369 * indiviually wrapped in tdb transactions. Wrapping them in a single
370 * transaction gives just a single transaction_commit() to actually do
371 * its fsync()s. See tdb/common/transaction.c for info about nested
372 * transaction behaviour.
375 werr
= ntstatus_to_werror(dbwrap_trans_do(regdb
,
376 init_registry_data_action
,
384 /***********************************************************************
385 Open the registry database
386 ***********************************************************************/
388 WERROR
regdb_init(void)
390 const char *vstring
= "INFO/version";
395 DEBUG(10, ("regdb_init: incrementing refcount (%d)\n",
401 regdb
= db_open(NULL
, state_path("registry.tdb"), 0,
402 REG_TDB_FLAGS
, O_RDWR
, 0600);
404 regdb
= db_open(NULL
, state_path("registry.tdb"), 0,
405 REG_TDB_FLAGS
, O_RDWR
|O_CREAT
, 0600);
407 werr
= ntstatus_to_werror(map_nt_error_from_unix(errno
));
408 DEBUG(1,("regdb_init: Failed to open registry %s (%s)\n",
409 state_path("registry.tdb"), strerror(errno
) ));
413 DEBUG(10,("regdb_init: Successfully created registry tdb\n"));
418 vers_id
= dbwrap_fetch_int32(regdb
, vstring
);
420 if ( vers_id
!= REGVER_V1
) {
422 /* any upgrade code here if needed */
423 DEBUG(10, ("regdb_init: got %s = %d != %d\n", vstring
,
424 vers_id
, REGVER_V1
));
425 status
= dbwrap_trans_store_int32(regdb
, vstring
, REGVER_V1
);
426 if (!NT_STATUS_IS_OK(status
)) {
427 DEBUG(1, ("regdb_init: error storing %s = %d: %s\n",
428 vstring
, REGVER_V1
, nt_errstr(status
)));
429 return ntstatus_to_werror(status
);
431 DEBUG(10, ("regdb_init: stored %s = %d\n",
432 vstring
, REGVER_V1
));
439 /***********************************************************************
440 Open the registry. Must already have been initialized by regdb_init()
441 ***********************************************************************/
443 WERROR
regdb_open( void )
445 WERROR result
= WERR_OK
;
448 DEBUG(10,("regdb_open: incrementing refcount (%d)\n", regdb_refcount
));
455 regdb
= db_open(NULL
, state_path("registry.tdb"), 0,
456 REG_TDB_FLAGS
, O_RDWR
, 0600);
458 result
= ntstatus_to_werror( map_nt_error_from_unix( errno
) );
459 DEBUG(0,("regdb_open: Failed to open %s! (%s)\n",
460 state_path("registry.tdb"), strerror(errno
) ));
466 DEBUG(10,("regdb_open: refcount reset (%d)\n", regdb_refcount
));
471 /***********************************************************************
472 ***********************************************************************/
474 int regdb_close( void )
476 if (regdb_refcount
== 0) {
482 DEBUG(10,("regdb_close: decrementing refcount (%d)\n", regdb_refcount
));
484 if ( regdb_refcount
> 0 )
487 SMB_ASSERT( regdb_refcount
>= 0 );
493 WERROR
regdb_transaction_start(void)
495 return (regdb
->transaction_start(regdb
) == 0) ?
496 WERR_OK
: WERR_REG_IO_FAILURE
;
499 WERROR
regdb_transaction_commit(void)
501 return (regdb
->transaction_commit(regdb
) == 0) ?
502 WERR_OK
: WERR_REG_IO_FAILURE
;
505 WERROR
regdb_transaction_cancel(void)
507 return (regdb
->transaction_cancel(regdb
) == 0) ?
508 WERR_OK
: WERR_REG_IO_FAILURE
;
511 /***********************************************************************
512 return the tdb sequence number of the registry tdb.
513 this is an indicator for the content of the registry
514 having changed. it will change upon regdb_init, too, though.
515 ***********************************************************************/
516 int regdb_get_seqnum(void)
518 return regdb
->get_seqnum(regdb
);
522 static WERROR
regdb_delete_key_with_prefix(struct db_context
*db
,
527 WERROR werr
= WERR_NOMEM
;
528 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
530 if (keyname
== NULL
) {
531 werr
= WERR_INVALID_PARAM
;
535 if (prefix
== NULL
) {
536 path
= discard_const_p(char, keyname
);
538 path
= talloc_asprintf(mem_ctx
, "%s/%s", prefix
, keyname
);
544 path
= normalize_reg_path(mem_ctx
, path
);
549 werr
= ntstatus_to_werror(dbwrap_delete_bystring(db
, path
));
551 /* treat "not" found" as ok */
552 if (W_ERROR_EQUAL(werr
, WERR_NOT_FOUND
)) {
557 talloc_free(mem_ctx
);
562 static WERROR
regdb_delete_values(struct db_context
*db
, const char *keyname
)
564 return regdb_delete_key_with_prefix(db
, keyname
, REG_VALUE_PREFIX
);
567 static WERROR
regdb_delete_secdesc(struct db_context
*db
, const char *keyname
)
569 return regdb_delete_key_with_prefix(db
, keyname
, REG_SECDESC_PREFIX
);
572 static WERROR
regdb_delete_subkeylist(struct db_context
*db
, const char *keyname
)
574 return regdb_delete_key_with_prefix(db
, keyname
, NULL
);
577 static WERROR
regdb_delete_key_lists(struct db_context
*db
, const char *keyname
)
581 werr
= regdb_delete_values(db
, keyname
);
582 if (!W_ERROR_IS_OK(werr
)) {
583 DEBUG(1, (__location__
" Deleting %s/%s failed: %s\n",
584 REG_VALUE_PREFIX
, keyname
, win_errstr(werr
)));
588 werr
= regdb_delete_secdesc(db
, keyname
);
589 if (!W_ERROR_IS_OK(werr
)) {
590 DEBUG(1, (__location__
" Deleting %s/%s failed: %s\n",
591 REG_SECDESC_PREFIX
, keyname
, win_errstr(werr
)));
595 werr
= regdb_delete_subkeylist(db
, keyname
);
596 if (!W_ERROR_IS_OK(werr
)) {
597 DEBUG(1, (__location__
" Deleting %s failed: %s\n",
598 keyname
, win_errstr(werr
)));
606 /***********************************************************************
607 Add subkey strings to the registry tdb under a defined key
608 fmt is the same format as tdb_pack except this function only supports
610 ***********************************************************************/
612 static WERROR
regdb_store_keys_internal2(struct db_context
*db
,
614 struct regsubkey_ctr
*ctr
)
617 uint8
*buffer
= NULL
;
620 uint32 num_subkeys
= regsubkey_ctr_numkeys(ctr
);
621 char *keyname
= NULL
;
622 TALLOC_CTX
*ctx
= talloc_stackframe();
626 werr
= WERR_INVALID_PARAM
;
630 keyname
= talloc_strdup(ctx
, key
);
636 keyname
= normalize_reg_path(ctx
, keyname
);
642 /* allocate some initial memory */
644 buffer
= (uint8
*)SMB_MALLOC(1024);
645 if (buffer
== NULL
) {
652 /* store the number of subkeys */
654 len
+= tdb_pack(buffer
+len
, buflen
-len
, "d", num_subkeys
);
656 /* pack all the strings */
658 for (i
=0; i
<num_subkeys
; i
++) {
661 thistime
= tdb_pack(buffer
+len
, buflen
-len
, "f",
662 regsubkey_ctr_specific_key(ctr
, i
));
663 if (len
+thistime
> buflen
) {
666 * tdb_pack hasn't done anything because of the short
667 * buffer, allocate extra space.
669 buffer
= SMB_REALLOC_ARRAY(buffer
, uint8_t,
672 DEBUG(0, ("regdb_store_keys: Failed to realloc "
673 "memory of size [%u]\n",
674 (unsigned int)(len
+thistime
)*2));
678 buflen
= (len
+thistime
)*2;
679 thistime2
= tdb_pack(
680 buffer
+len
, buflen
-len
, "f",
681 regsubkey_ctr_specific_key(ctr
, i
));
682 if (thistime2
!= thistime
) {
683 DEBUG(0, ("tdb_pack failed\n"));
684 werr
= WERR_CAN_NOT_COMPLETE
;
691 /* finally write out the data */
695 werr
= ntstatus_to_werror(dbwrap_store_bystring(db
, keyname
, dbuf
,
697 W_ERROR_NOT_OK_GOTO_DONE(werr
);
700 * Delete a sorted subkey cache for regdb_key_exists, will be
701 * recreated automatically
703 keyname
= talloc_asprintf(ctx
, "%s/%s", REG_SORTED_SUBKEYS_PREFIX
,
705 if (keyname
== NULL
) {
710 werr
= ntstatus_to_werror(dbwrap_delete_bystring(db
, keyname
));
712 /* don't treat WERR_NOT_FOUND as an error here */
713 if (W_ERROR_EQUAL(werr
, WERR_NOT_FOUND
)) {
723 /***********************************************************************
724 Store the new subkey record and create any child key records that
725 do not currently exist
726 ***********************************************************************/
728 struct regdb_store_keys_context
{
730 struct regsubkey_ctr
*ctr
;
733 static NTSTATUS
regdb_store_keys_action(struct db_context
*db
,
736 struct regdb_store_keys_context
*store_ctx
;
740 struct regsubkey_ctr
*subkeys
= NULL
, *old_subkeys
= NULL
;
741 char *oldkeyname
= NULL
;
742 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
744 store_ctx
= (struct regdb_store_keys_context
*)private_data
;
747 * Re-fetch the old keys inside the transaction
750 werr
= regsubkey_ctr_init(mem_ctx
, &old_subkeys
);
751 W_ERROR_NOT_OK_GOTO_DONE(werr
);
753 regdb_fetch_keys_internal(db
, store_ctx
->key
, old_subkeys
);
756 * Make the store operation as safe as possible without transactions:
758 * (1) For each subkey removed from ctr compared with old_subkeys:
760 * (a) First delete the value db entry.
762 * (b) Next delete the secdesc db record.
764 * (c) Then delete the subkey list entry.
766 * (2) Now write the list of subkeys of the parent key,
767 * deleting removed entries and adding new ones.
769 * (3) Finally create the subkey list entries for the added keys.
771 * This way if we crash half-way in between deleting the subkeys
772 * and storing the parent's list of subkeys, no old data can pop up
773 * out of the blue when re-adding keys later on.
776 /* (1) delete removed keys' lists (values/secdesc/subkeys) */
778 num_subkeys
= regsubkey_ctr_numkeys(old_subkeys
);
779 for (i
=0; i
<num_subkeys
; i
++) {
780 oldkeyname
= regsubkey_ctr_specific_key(old_subkeys
, i
);
782 if (regsubkey_ctr_key_exists(store_ctx
->ctr
, oldkeyname
)) {
784 * It's still around, don't delete
789 path
= talloc_asprintf(mem_ctx
, "%s/%s", store_ctx
->key
,
796 werr
= regdb_delete_key_lists(db
, path
);
797 W_ERROR_NOT_OK_GOTO_DONE(werr
);
802 TALLOC_FREE(old_subkeys
);
804 /* (2) store the subkey list for the parent */
806 werr
= regdb_store_keys_internal2(db
, store_ctx
->key
, store_ctx
->ctr
);
807 if (!W_ERROR_IS_OK(werr
)) {
808 DEBUG(0,("regdb_store_keys: Failed to store new subkey list "
809 "for parent [%s]: %s\n", store_ctx
->key
,
814 /* (3) now create records for any subkeys that don't already exist */
816 num_subkeys
= regsubkey_ctr_numkeys(store_ctx
->ctr
);
818 if (num_subkeys
== 0) {
819 werr
= regsubkey_ctr_init(mem_ctx
, &subkeys
);
820 W_ERROR_NOT_OK_GOTO_DONE(werr
);
822 werr
= regdb_store_keys_internal2(db
, store_ctx
->key
, subkeys
);
823 if (!W_ERROR_IS_OK(werr
)) {
824 DEBUG(0,("regdb_store_keys: Failed to store "
825 "new record for key [%s]: %s\n",
826 store_ctx
->key
, win_errstr(werr
)));
829 TALLOC_FREE(subkeys
);
832 for (i
=0; i
<num_subkeys
; i
++) {
833 path
= talloc_asprintf(mem_ctx
, "%s/%s", store_ctx
->key
,
834 regsubkey_ctr_specific_key(store_ctx
->ctr
, i
));
839 werr
= regsubkey_ctr_init(mem_ctx
, &subkeys
);
840 W_ERROR_NOT_OK_GOTO_DONE(werr
);
842 if (regdb_fetch_keys_internal(db
, path
, subkeys
) == -1) {
843 /* create a record with 0 subkeys */
844 werr
= regdb_store_keys_internal2(db
, path
, subkeys
);
845 if (!W_ERROR_IS_OK(werr
)) {
846 DEBUG(0,("regdb_store_keys: Failed to store "
847 "new record for key [%s]: %s\n", path
,
853 TALLOC_FREE(subkeys
);
860 talloc_free(mem_ctx
);
861 return werror_to_ntstatus(werr
);
864 static bool regdb_store_keys_internal(struct db_context
*db
, const char *key
,
865 struct regsubkey_ctr
*ctr
)
867 int num_subkeys
, old_num_subkeys
, i
;
868 struct regsubkey_ctr
*old_subkeys
= NULL
;
869 TALLOC_CTX
*ctx
= talloc_stackframe();
872 struct regdb_store_keys_context store_ctx
;
874 if (!regdb_key_is_base_key(key
) && !regdb_key_exists(db
, key
)) {
879 * fetch a list of the old subkeys so we can determine if anything has
883 werr
= regsubkey_ctr_init(ctx
, &old_subkeys
);
884 if (!W_ERROR_IS_OK(werr
)) {
885 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
889 regdb_fetch_keys_internal(db
, key
, old_subkeys
);
891 num_subkeys
= regsubkey_ctr_numkeys(ctr
);
892 old_num_subkeys
= regsubkey_ctr_numkeys(old_subkeys
);
893 if ((num_subkeys
&& old_num_subkeys
) &&
894 (num_subkeys
== old_num_subkeys
)) {
896 for (i
= 0; i
< num_subkeys
; i
++) {
897 if (strcmp(regsubkey_ctr_specific_key(ctr
, i
),
898 regsubkey_ctr_specific_key(old_subkeys
, i
))
904 if (i
== num_subkeys
) {
906 * Nothing changed, no point to even start a tdb
915 TALLOC_FREE(old_subkeys
);
920 werr
= ntstatus_to_werror(dbwrap_trans_do(db
,
921 regdb_store_keys_action
,
924 ret
= W_ERROR_IS_OK(werr
);
932 bool regdb_store_keys(const char *key
, struct regsubkey_ctr
*ctr
)
934 return regdb_store_keys_internal(regdb
, key
, ctr
);
937 static WERROR
regdb_create_subkey(const char *key
, const char *subkey
)
940 struct regsubkey_ctr
*subkeys
;
941 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
943 if (!regdb_key_is_base_key(key
) && !regdb_key_exists(regdb
, key
)) {
944 werr
= WERR_NOT_FOUND
;
948 werr
= regsubkey_ctr_init(mem_ctx
, &subkeys
);
949 W_ERROR_NOT_OK_GOTO_DONE(werr
);
951 if (regdb_fetch_keys_internal(regdb
, key
, subkeys
) < 0) {
952 werr
= WERR_REG_IO_FAILURE
;
956 if (regsubkey_ctr_key_exists(subkeys
, subkey
)) {
961 talloc_free(subkeys
);
963 if (regdb
->transaction_start(regdb
) != 0) {
964 werr
= WERR_REG_IO_FAILURE
;
968 werr
= regsubkey_ctr_init(mem_ctx
, &subkeys
);
969 W_ERROR_NOT_OK_GOTO(werr
, cancel
);
971 if (regdb_fetch_keys_internal(regdb
, key
, subkeys
) < 0) {
972 werr
= WERR_REG_IO_FAILURE
;
976 werr
= regsubkey_ctr_addkey(subkeys
, subkey
);
977 W_ERROR_NOT_OK_GOTO(werr
, cancel
);
979 werr
= regdb_store_keys_internal2(regdb
, key
, subkeys
);
980 if (!W_ERROR_IS_OK(werr
)) {
981 DEBUG(0, (__location__
" failed to store new subkey list for "
982 "parent key %s: %s\n", key
, win_errstr(werr
)));
986 if (regdb
->transaction_commit(regdb
) != 0) {
987 werr
= WERR_REG_IO_FAILURE
;
988 DEBUG(0, (__location__
" failed to commit transaction\n"));
994 if (regdb
->transaction_cancel(regdb
) != 0) {
995 smb_panic("regdb_create_subkey: transaction_cancel failed\n");
999 talloc_free(mem_ctx
);
1003 static WERROR
regdb_delete_subkey(const char *key
, const char *subkey
)
1006 struct regsubkey_ctr
*subkeys
;
1008 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
1010 if (!regdb_key_is_base_key(key
) && !regdb_key_exists(regdb
, key
)) {
1011 werr
= WERR_NOT_FOUND
;
1015 path
= talloc_asprintf(mem_ctx
, "%s/%s", key
, subkey
);
1021 if (!regdb_key_exists(regdb
, path
)) {
1026 if (regdb
->transaction_start(regdb
) != 0) {
1027 werr
= WERR_REG_IO_FAILURE
;
1031 werr
= regdb_delete_key_lists(regdb
, path
);
1032 W_ERROR_NOT_OK_GOTO(werr
, cancel
);
1034 werr
= regsubkey_ctr_init(mem_ctx
, &subkeys
);
1035 W_ERROR_NOT_OK_GOTO(werr
, cancel
);
1037 if (regdb_fetch_keys_internal(regdb
, key
, subkeys
) < 0) {
1038 werr
= WERR_REG_IO_FAILURE
;
1042 werr
= regsubkey_ctr_delkey(subkeys
, subkey
);
1043 W_ERROR_NOT_OK_GOTO(werr
, cancel
);
1045 werr
= regdb_store_keys_internal2(regdb
, key
, subkeys
);
1046 if (!W_ERROR_IS_OK(werr
)) {
1047 DEBUG(0, (__location__
" failed to store new subkey_list for "
1048 "parent key %s: %s\n", key
, win_errstr(werr
)));
1052 if (regdb
->transaction_commit(regdb
) != 0) {
1053 DEBUG(0, (__location__
" failed to commit transaction\n"));
1054 werr
= WERR_REG_IO_FAILURE
;
1060 if (regdb
->transaction_cancel(regdb
) != 0) {
1061 smb_panic("regdb_delete_subkey: transaction_cancel failed\n");
1065 talloc_free(mem_ctx
);
1069 static TDB_DATA
regdb_fetch_key_internal(struct db_context
*db
,
1070 TALLOC_CTX
*mem_ctx
, const char *key
)
1075 path
= normalize_reg_path(mem_ctx
, key
);
1077 return make_tdb_data(NULL
, 0);
1080 data
= dbwrap_fetch_bystring(db
, mem_ctx
, path
);
1088 * check whether a given key name represents a base key,
1089 * i.e one without a subkey separator ('/' or '\').
1091 static bool regdb_key_is_base_key(const char *key
)
1093 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
1101 path
= normalize_reg_path(mem_ctx
, key
);
1103 DEBUG(0, ("out of memory! (talloc failed)\n"));
1107 if (*path
== '\0') {
1111 ret
= (strrchr(path
, '/') == NULL
);
1114 TALLOC_FREE(mem_ctx
);
1119 * regdb_key_exists() is a very frequent operation. It can be quite
1120 * time-consuming to fully fetch the parent's subkey list, talloc_strdup all
1121 * subkeys and then compare the keyname linearly to all the parent's subkeys.
1123 * The following code tries to make this operation as efficient as possible:
1124 * Per registry key we create a list of subkeys that is very efficient to
1125 * search for existence of a subkey. Its format is:
1127 * 4 bytes num_subkeys
1128 * 4*num_subkey bytes offset into the string array
1129 * then follows a sorted list of subkeys in uppercase
1131 * This record is created by create_sorted_subkeys() on demand if it does not
1132 * exist. scan_parent_subkeys() uses regdb->parse_record to search the sorted
1133 * list, the parsing code and the binary search can be found in
1134 * parent_subkey_scanner. The code uses parse_record() to avoid a memcpy of
1135 * the potentially large subkey record.
1137 * The sorted subkey record is deleted in regdb_store_keys_internal2 and
1138 * recreated on demand.
1141 static int cmp_keynames(const void *p1
, const void *p2
)
1143 return StrCaseCmp(*((char **)p1
), *((char **)p2
));
1146 static bool create_sorted_subkeys(const char *key
, const char *sorted_keyname
)
1148 char **sorted_subkeys
;
1149 struct regsubkey_ctr
*ctr
;
1150 bool result
= false;
1159 if (regdb
->transaction_start(regdb
) != 0) {
1160 DEBUG(0, ("create_sorted_subkeys: transaction_start "
1165 werr
= regsubkey_ctr_init(talloc_tos(), &ctr
);
1166 if (!W_ERROR_IS_OK(werr
)) {
1170 res
= regdb_fetch_keys_internal(regdb
, key
, ctr
);
1175 num_subkeys
= regsubkey_ctr_numkeys(ctr
);
1176 sorted_subkeys
= talloc_array(ctr
, char *, num_subkeys
);
1177 if (sorted_subkeys
== NULL
) {
1181 len
= 4 + 4*num_subkeys
;
1183 for (i
= 0; i
< num_subkeys
; i
++) {
1184 sorted_subkeys
[i
] = talloc_strdup_upper(sorted_subkeys
,
1185 regsubkey_ctr_specific_key(ctr
, i
));
1186 if (sorted_subkeys
[i
] == NULL
) {
1189 len
+= strlen(sorted_subkeys
[i
])+1;
1192 qsort(sorted_subkeys
, num_subkeys
, sizeof(char *), cmp_keynames
);
1194 buf
= talloc_array(ctr
, char, len
);
1198 p
= buf
+ 4 + 4*num_subkeys
;
1200 SIVAL(buf
, 0, num_subkeys
);
1202 for (i
=0; i
< num_subkeys
; i
++) {
1203 ptrdiff_t offset
= p
- buf
;
1204 SIVAL(buf
, 4 + 4*i
, offset
);
1205 strlcpy(p
, sorted_subkeys
[i
], len
-offset
);
1206 p
+= strlen(sorted_subkeys
[i
]) + 1;
1209 status
= dbwrap_store_bystring(
1210 regdb
, sorted_keyname
, make_tdb_data((uint8_t *)buf
, len
),
1212 if (!NT_STATUS_IS_OK(status
)) {
1214 * Don't use a "goto fail;" here, this would commit the broken
1215 * transaction. See below for an explanation.
1217 if (regdb
->transaction_cancel(regdb
) == -1) {
1218 smb_panic("create_sorted_subkeys: transaction_cancel "
1228 * We only get here via the "goto fail" when we did not write anything
1229 * yet. Using transaction_commit even in a failure case is necessary
1230 * because this (disposable) call might be nested in other
1231 * transactions. Doing a cancel here would destroy the possibility of
1232 * a transaction_commit for transactions that we might be wrapped in.
1234 if (regdb
->transaction_commit(regdb
) == -1) {
1235 DEBUG(0, ("create_sorted_subkeys: transaction_commit "
1244 struct scan_subkey_state
{
1250 static int parent_subkey_scanner(TDB_DATA key
, TDB_DATA data
,
1253 struct scan_subkey_state
*state
=
1254 (struct scan_subkey_state
*)private_data
;
1255 uint32_t num_subkeys
;
1258 if (data
.dsize
< sizeof(uint32_t)) {
1262 state
->scanned
= true;
1263 state
->found
= false;
1265 tdb_unpack(data
.dptr
, data
.dsize
, "d", &num_subkeys
);
1271 uint32_t idx
= (l
+u
)/2;
1272 char *s
= (char *)data
.dptr
+ IVAL(data
.dptr
, 4 + 4*idx
);
1273 int comparison
= strcmp(state
->name
, s
);
1275 if (comparison
< 0) {
1277 } else if (comparison
> 0) {
1280 state
->found
= true;
1287 static bool scan_parent_subkeys(struct db_context
*db
, const char *parent
,
1292 struct scan_subkey_state state
= { 0, };
1293 bool result
= false;
1298 path
= normalize_reg_path(talloc_tos(), parent
);
1303 key
= talloc_asprintf(talloc_tos(), "%s/%s",
1304 REG_SORTED_SUBKEYS_PREFIX
, path
);
1309 state
.name
= talloc_strdup_upper(talloc_tos(), name
);
1310 if (state
.name
== NULL
) {
1313 state
.scanned
= false;
1315 res
= db
->parse_record(db
, string_term_tdb_data(key
),
1316 parent_subkey_scanner
, &state
);
1318 if (state
.scanned
) {
1319 result
= state
.found
;
1321 if (!create_sorted_subkeys(path
, key
)) {
1324 res
= db
->parse_record(db
, string_term_tdb_data(key
),
1325 parent_subkey_scanner
, &state
);
1326 if ((res
== 0) && (state
.scanned
)) {
1327 result
= state
.found
;
1333 TALLOC_FREE(state
.name
);
1338 * Check for the existence of a key.
1340 * Existence of a key is authoritatively defined by its
1341 * existence in the list of subkeys of its parent key.
1342 * The exeption of this are keys without a parent key,
1343 * i.e. the "base" keys (HKLM, HKCU, ...).
1345 static bool regdb_key_exists(struct db_context
*db
, const char *key
)
1347 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
1356 path
= normalize_reg_path(mem_ctx
, key
);
1358 DEBUG(0, ("out of memory! (talloc failed)\n"));
1362 if (*path
== '\0') {
1366 p
= strrchr(path
, '/');
1368 /* this is a base key */
1369 value
= regdb_fetch_key_internal(db
, mem_ctx
, path
);
1370 ret
= (value
.dptr
!= NULL
);
1373 ret
= scan_parent_subkeys(db
, path
, p
+1);
1377 TALLOC_FREE(mem_ctx
);
1382 /***********************************************************************
1383 Retrieve an array of strings containing subkeys. Memory should be
1384 released by the caller.
1385 ***********************************************************************/
1387 static int regdb_fetch_keys_internal(struct db_context
*db
, const char *key
,
1388 struct regsubkey_ctr
*ctr
)
1397 TALLOC_CTX
*frame
= talloc_stackframe();
1400 DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key
? key
: "NULL"));
1402 if (!regdb_key_exists(db
, key
)) {
1406 werr
= regsubkey_ctr_set_seqnum(ctr
, db
->get_seqnum(db
));
1407 if (!W_ERROR_IS_OK(werr
)) {
1411 value
= regdb_fetch_key_internal(db
, frame
, key
);
1413 if (value
.dptr
== NULL
) {
1414 DEBUG(10, ("regdb_fetch_keys: no subkeys found for key [%s]\n",
1421 buflen
= value
.dsize
;
1422 len
= tdb_unpack( buf
, buflen
, "d", &num_items
);
1424 for (i
=0; i
<num_items
; i
++) {
1425 len
+= tdb_unpack(buf
+len
, buflen
-len
, "f", subkeyname
);
1426 werr
= regsubkey_ctr_addkey(ctr
, subkeyname
);
1427 if (!W_ERROR_IS_OK(werr
)) {
1428 DEBUG(5, ("regdb_fetch_keys: regsubkey_ctr_addkey "
1429 "failed: %s\n", win_errstr(werr
)));
1434 DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items
));
1442 int regdb_fetch_keys(const char *key
, struct regsubkey_ctr
*ctr
)
1444 return regdb_fetch_keys_internal(regdb
, key
, ctr
);
1447 /****************************************************************************
1448 Unpack a list of registry values frem the TDB
1449 ***************************************************************************/
1451 static int regdb_unpack_values(struct regval_ctr
*values
, uint8
*buf
, int buflen
)
1458 uint32 num_values
= 0;
1461 /* loop and unpack the rest of the registry values */
1463 len
+= tdb_unpack(buf
+len
, buflen
-len
, "d", &num_values
);
1465 for ( i
=0; i
<num_values
; i
++ ) {
1466 /* unpack the next regval */
1471 valuename
[0] = '\0';
1472 len
+= tdb_unpack(buf
+len
, buflen
-len
, "fdB",
1478 /* add the new value. Paranoid protective code -- make sure data_p is valid */
1480 if (*valuename
&& size
&& data_p
) {
1481 regval_ctr_addvalue(values
, valuename
, type
,
1482 (const char *)data_p
, size
);
1484 SAFE_FREE(data_p
); /* 'B' option to tdb_unpack does a malloc() */
1486 DEBUG(8,("specific: [%s], len: %d\n", valuename
, size
));
1492 /****************************************************************************
1493 Pack all values in all printer keys
1494 ***************************************************************************/
1496 static int regdb_pack_values(struct regval_ctr
*values
, uint8
*buf
, int buflen
)
1500 struct regval_blob
*val
;
1506 num_values
= regval_ctr_numvals( values
);
1508 /* pack the number of values first */
1510 len
+= tdb_pack( buf
+len
, buflen
-len
, "d", num_values
);
1512 /* loop over all values */
1514 for ( i
=0; i
<num_values
; i
++ ) {
1515 val
= regval_ctr_specific_value( values
, i
);
1516 len
+= tdb_pack(buf
+len
, buflen
-len
, "fdB",
1520 regval_data_p(val
) );
1526 /***********************************************************************
1527 Retrieve an array of strings containing subkeys. Memory should be
1528 released by the caller.
1529 ***********************************************************************/
1531 static int regdb_fetch_values_internal(struct db_context
*db
, const char* key
,
1532 struct regval_ctr
*values
)
1534 char *keystr
= NULL
;
1535 TALLOC_CTX
*ctx
= talloc_stackframe();
1539 DEBUG(10,("regdb_fetch_values: Looking for value of key [%s] \n", key
));
1541 if (!regdb_key_exists(db
, key
)) {
1545 keystr
= talloc_asprintf(ctx
, "%s/%s", REG_VALUE_PREFIX
, key
);
1550 values
->seqnum
= db
->get_seqnum(db
);
1552 value
= regdb_fetch_key_internal(db
, ctx
, keystr
);
1555 /* all keys have zero values by default */
1559 regdb_unpack_values(values
, value
.dptr
, value
.dsize
);
1560 ret
= regval_ctr_numvals(values
);
1567 int regdb_fetch_values(const char* key
, struct regval_ctr
*values
)
1569 return regdb_fetch_values_internal(regdb
, key
, values
);
1572 static bool regdb_store_values_internal(struct db_context
*db
, const char *key
,
1573 struct regval_ctr
*values
)
1575 TDB_DATA old_data
, data
;
1576 char *keystr
= NULL
;
1577 TALLOC_CTX
*ctx
= talloc_stackframe();
1580 bool result
= false;
1582 DEBUG(10,("regdb_store_values: Looking for value of key [%s] \n", key
));
1584 if (!regdb_key_exists(db
, key
)) {
1590 len
= regdb_pack_values(values
, data
.dptr
, data
.dsize
);
1592 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
1596 data
.dptr
= TALLOC_ARRAY(ctx
, uint8
, len
);
1599 len
= regdb_pack_values(values
, data
.dptr
, data
.dsize
);
1601 SMB_ASSERT( len
== data
.dsize
);
1603 keystr
= talloc_asprintf(ctx
, "%s/%s", REG_VALUE_PREFIX
, key
);
1607 keystr
= normalize_reg_path(ctx
, keystr
);
1612 old_data
= dbwrap_fetch_bystring(db
, ctx
, keystr
);
1614 if ((old_data
.dptr
!= NULL
)
1615 && (old_data
.dsize
== data
.dsize
)
1616 && (memcmp(old_data
.dptr
, data
.dptr
, data
.dsize
) == 0))
1622 status
= dbwrap_trans_store_bystring(db
, keystr
, data
, TDB_REPLACE
);
1624 result
= NT_STATUS_IS_OK(status
);
1631 bool regdb_store_values(const char *key
, struct regval_ctr
*values
)
1633 return regdb_store_values_internal(regdb
, key
, values
);
1636 static WERROR
regdb_get_secdesc(TALLOC_CTX
*mem_ctx
, const char *key
,
1637 struct security_descriptor
**psecdesc
)
1642 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
1643 WERROR err
= WERR_OK
;
1645 DEBUG(10, ("regdb_get_secdesc: Getting secdesc of key [%s]\n", key
));
1647 if (!regdb_key_exists(regdb
, key
)) {
1652 tdbkey
= talloc_asprintf(tmp_ctx
, "%s/%s", REG_SECDESC_PREFIX
, key
);
1653 if (tdbkey
== NULL
) {
1657 normalize_dbkey(tdbkey
);
1659 data
= dbwrap_fetch_bystring(regdb
, tmp_ctx
, tdbkey
);
1660 if (data
.dptr
== NULL
) {
1665 status
= unmarshall_sec_desc(mem_ctx
, (uint8
*)data
.dptr
, data
.dsize
,
1668 if (NT_STATUS_EQUAL(status
, NT_STATUS_NO_MEMORY
)) {
1670 } else if (!NT_STATUS_IS_OK(status
)) {
1671 err
= WERR_REG_CORRUPT
;
1675 TALLOC_FREE(tmp_ctx
);
1679 static WERROR
regdb_set_secdesc(const char *key
,
1680 struct security_descriptor
*secdesc
)
1682 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
1684 WERROR err
= WERR_NOMEM
;
1687 if (!regdb_key_exists(regdb
, key
)) {
1692 tdbkey
= talloc_asprintf(mem_ctx
, "%s/%s", REG_SECDESC_PREFIX
, key
);
1693 if (tdbkey
== NULL
) {
1696 normalize_dbkey(tdbkey
);
1698 if (secdesc
== NULL
) {
1699 /* assuming a delete */
1700 err
= ntstatus_to_werror(dbwrap_trans_delete_bystring(regdb
,
1705 err
= ntstatus_to_werror(marshall_sec_desc(mem_ctx
, secdesc
,
1708 W_ERROR_NOT_OK_GOTO_DONE(err
);
1710 err
= ntstatus_to_werror(dbwrap_trans_store_bystring(regdb
, tdbkey
,
1714 TALLOC_FREE(mem_ctx
);
1718 bool regdb_subkeys_need_update(struct regsubkey_ctr
*subkeys
)
1720 return (regdb_get_seqnum() != regsubkey_ctr_get_seqnum(subkeys
));
1723 bool regdb_values_need_update(struct regval_ctr
*values
)
1725 return (regdb_get_seqnum() != values
->seqnum
);
1729 * Table of function pointers for default access
1732 struct registry_ops regdb_ops
= {
1733 .fetch_subkeys
= regdb_fetch_keys
,
1734 .fetch_values
= regdb_fetch_values
,
1735 .store_subkeys
= regdb_store_keys
,
1736 .store_values
= regdb_store_values
,
1737 .create_subkey
= regdb_create_subkey
,
1738 .delete_subkey
= regdb_delete_subkey
,
1739 .get_secdesc
= regdb_get_secdesc
,
1740 .set_secdesc
= regdb_set_secdesc
,
1741 .subkeys_need_update
= regdb_subkeys_need_update
,
1742 .values_need_update
= regdb_values_need_update