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 bool regdb_store_keys_internal2(struct db_context
*db
,
614 struct regsubkey_ctr
*ctr
)
617 uint8
*buffer
= NULL
;
621 uint32 num_subkeys
= regsubkey_ctr_numkeys(ctr
);
622 char *keyname
= NULL
;
623 TALLOC_CTX
*ctx
= talloc_stackframe();
630 keyname
= talloc_strdup(ctx
, key
);
634 keyname
= normalize_reg_path(ctx
, keyname
);
636 /* allocate some initial memory */
638 buffer
= (uint8
*)SMB_MALLOC(1024);
639 if (buffer
== NULL
) {
645 /* store the number of subkeys */
647 len
+= tdb_pack(buffer
+len
, buflen
-len
, "d", num_subkeys
);
649 /* pack all the strings */
651 for (i
=0; i
<num_subkeys
; i
++) {
654 thistime
= tdb_pack(buffer
+len
, buflen
-len
, "f",
655 regsubkey_ctr_specific_key(ctr
, i
));
656 if (len
+thistime
> buflen
) {
659 * tdb_pack hasn't done anything because of the short
660 * buffer, allocate extra space.
662 buffer
= SMB_REALLOC_ARRAY(buffer
, uint8_t,
665 DEBUG(0, ("regdb_store_keys: Failed to realloc "
666 "memory of size [%u]\n",
667 (unsigned int)(len
+thistime
)*2));
671 buflen
= (len
+thistime
)*2;
672 thistime2
= tdb_pack(
673 buffer
+len
, buflen
-len
, "f",
674 regsubkey_ctr_specific_key(ctr
, i
));
675 if (thistime2
!= thistime
) {
676 DEBUG(0, ("tdb_pack failed\n"));
684 /* finally write out the data */
688 status
= dbwrap_store_bystring(db
, keyname
, dbuf
, TDB_REPLACE
);
689 if (!NT_STATUS_IS_OK(status
)) {
695 * Delete a sorted subkey cache for regdb_key_exists, will be
696 * recreated automatically
698 keyname
= talloc_asprintf(ctx
, "%s/%s", REG_SORTED_SUBKEYS_PREFIX
,
700 if (keyname
!= NULL
) {
701 dbwrap_delete_bystring(db
, keyname
);
710 /***********************************************************************
711 Store the new subkey record and create any child key records that
712 do not currently exist
713 ***********************************************************************/
715 static bool regdb_store_keys_internal(struct db_context
*db
, const char *key
,
716 struct regsubkey_ctr
*ctr
)
718 int num_subkeys
, old_num_subkeys
, i
;
720 struct regsubkey_ctr
*subkeys
= NULL
, *old_subkeys
= NULL
;
721 char *oldkeyname
= NULL
;
722 TALLOC_CTX
*ctx
= talloc_stackframe();
725 if (!regdb_key_is_base_key(key
) && !regdb_key_exists(db
, key
)) {
730 * fetch a list of the old subkeys so we can determine if anything has
734 werr
= regsubkey_ctr_init(ctx
, &old_subkeys
);
735 if (!W_ERROR_IS_OK(werr
)) {
736 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
740 regdb_fetch_keys_internal(db
, key
, old_subkeys
);
742 num_subkeys
= regsubkey_ctr_numkeys(ctr
);
743 old_num_subkeys
= regsubkey_ctr_numkeys(old_subkeys
);
744 if ((num_subkeys
&& old_num_subkeys
) &&
745 (num_subkeys
== old_num_subkeys
)) {
747 for (i
= 0; i
< num_subkeys
; i
++) {
748 if (strcmp(regsubkey_ctr_specific_key(ctr
, i
),
749 regsubkey_ctr_specific_key(old_subkeys
, i
))
755 if (i
== num_subkeys
) {
757 * Nothing changed, no point to even start a tdb
760 TALLOC_FREE(old_subkeys
);
765 TALLOC_FREE(old_subkeys
);
767 if (db
->transaction_start(db
) != 0) {
768 DEBUG(0, ("regdb_store_keys: transaction_start failed\n"));
773 * Re-fetch the old keys inside the transaction
776 werr
= regsubkey_ctr_init(ctx
, &old_subkeys
);
777 if (!W_ERROR_IS_OK(werr
)) {
778 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
782 regdb_fetch_keys_internal(db
, key
, old_subkeys
);
785 * Make the store operation as safe as possible without transactions:
787 * (1) For each subkey removed from ctr compared with old_subkeys:
789 * (a) First delete the value db entry.
791 * (b) Next delete the secdesc db record.
793 * (c) Then delete the subkey list entry.
795 * (2) Now write the list of subkeys of the parent key,
796 * deleting removed entries and adding new ones.
798 * (3) Finally create the subkey list entries for the added keys.
800 * This way if we crash half-way in between deleting the subkeys
801 * and storing the parent's list of subkeys, no old data can pop up
802 * out of the blue when re-adding keys later on.
805 /* (1) delete removed keys' lists (values/secdesc/subkeys) */
807 num_subkeys
= regsubkey_ctr_numkeys(old_subkeys
);
808 for (i
=0; i
<num_subkeys
; i
++) {
809 oldkeyname
= regsubkey_ctr_specific_key(old_subkeys
, i
);
811 if (regsubkey_ctr_key_exists(ctr
, oldkeyname
)) {
813 * It's still around, don't delete
819 path
= talloc_asprintf(ctx
, "%s/%s", key
, oldkeyname
);
824 werr
= regdb_delete_key_lists(db
, path
);
825 W_ERROR_NOT_OK_GOTO(werr
, cancel
);
830 TALLOC_FREE(old_subkeys
);
832 /* (2) store the subkey list for the parent */
834 if (!regdb_store_keys_internal2(db
, key
, ctr
)) {
835 DEBUG(0,("regdb_store_keys: Failed to store new subkey list "
836 "for parent [%s]\n", key
));
840 /* (3) now create records for any subkeys that don't already exist */
842 num_subkeys
= regsubkey_ctr_numkeys(ctr
);
844 if (num_subkeys
== 0) {
845 werr
= regsubkey_ctr_init(ctx
, &subkeys
);
846 if (!W_ERROR_IS_OK(werr
)) {
847 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
851 if (!regdb_store_keys_internal2(db
, key
, subkeys
)) {
852 DEBUG(0,("regdb_store_keys: Failed to store "
853 "new record for key [%s]\n", key
));
856 TALLOC_FREE(subkeys
);
860 for (i
=0; i
<num_subkeys
; i
++) {
861 path
= talloc_asprintf(ctx
, "%s/%s",
863 regsubkey_ctr_specific_key(ctr
, i
));
867 werr
= regsubkey_ctr_init(ctx
, &subkeys
);
868 if (!W_ERROR_IS_OK(werr
)) {
869 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
873 if (regdb_fetch_keys_internal(db
, path
, subkeys
) == -1) {
874 /* create a record with 0 subkeys */
875 if (!regdb_store_keys_internal2(db
, path
, subkeys
)) {
876 DEBUG(0,("regdb_store_keys: Failed to store "
877 "new record for key [%s]\n", path
));
882 TALLOC_FREE(subkeys
);
886 if (db
->transaction_commit(db
) != 0) {
887 DEBUG(0, ("regdb_store_keys: Could not commit transaction\n"));
895 if (db
->transaction_cancel(db
) != 0) {
896 smb_panic("regdb_store_keys: transaction_cancel failed\n");
905 bool regdb_store_keys(const char *key
, struct regsubkey_ctr
*ctr
)
907 return regdb_store_keys_internal(regdb
, key
, ctr
);
910 static WERROR
regdb_create_subkey(const char *key
, const char *subkey
)
913 struct regsubkey_ctr
*subkeys
;
914 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
916 if (!regdb_key_is_base_key(key
) && !regdb_key_exists(regdb
, key
)) {
917 werr
= WERR_NOT_FOUND
;
921 werr
= regsubkey_ctr_init(mem_ctx
, &subkeys
);
922 W_ERROR_NOT_OK_GOTO_DONE(werr
);
924 if (regdb_fetch_keys_internal(regdb
, key
, subkeys
) < 0) {
925 werr
= WERR_REG_IO_FAILURE
;
929 if (regsubkey_ctr_key_exists(subkeys
, subkey
)) {
934 talloc_free(subkeys
);
936 if (regdb
->transaction_start(regdb
) != 0) {
937 werr
= WERR_REG_IO_FAILURE
;
941 werr
= regsubkey_ctr_init(mem_ctx
, &subkeys
);
942 W_ERROR_NOT_OK_GOTO(werr
, cancel
);
944 if (regdb_fetch_keys_internal(regdb
, key
, subkeys
) < 0) {
945 werr
= WERR_REG_IO_FAILURE
;
949 werr
= regsubkey_ctr_addkey(subkeys
, subkey
);
950 W_ERROR_NOT_OK_GOTO(werr
, cancel
);
952 if (!regdb_store_keys_internal2(regdb
, key
, subkeys
)) {
953 DEBUG(0, (__location__
" failed to store new subkey list for "
954 "parent key %s\n", key
));
955 werr
= WERR_REG_IO_FAILURE
;
959 if (regdb
->transaction_commit(regdb
) != 0) {
960 werr
= WERR_REG_IO_FAILURE
;
961 DEBUG(0, (__location__
" failed to commit transaction\n"));
967 if (regdb
->transaction_cancel(regdb
) != 0) {
968 smb_panic("regdb_create_subkey: transaction_cancel failed\n");
972 talloc_free(mem_ctx
);
976 static WERROR
regdb_delete_subkey(const char *key
, const char *subkey
)
979 struct regsubkey_ctr
*subkeys
;
981 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
983 if (!regdb_key_is_base_key(key
) && !regdb_key_exists(regdb
, key
)) {
984 werr
= WERR_NOT_FOUND
;
988 path
= talloc_asprintf(mem_ctx
, "%s/%s", key
, subkey
);
994 if (!regdb_key_exists(regdb
, path
)) {
999 if (regdb
->transaction_start(regdb
) != 0) {
1000 werr
= WERR_REG_IO_FAILURE
;
1004 werr
= regdb_delete_key_lists(regdb
, path
);
1005 W_ERROR_NOT_OK_GOTO(werr
, cancel
);
1007 werr
= regsubkey_ctr_init(mem_ctx
, &subkeys
);
1008 W_ERROR_NOT_OK_GOTO(werr
, cancel
);
1010 if (regdb_fetch_keys_internal(regdb
, key
, subkeys
) < 0) {
1011 werr
= WERR_REG_IO_FAILURE
;
1015 werr
= regsubkey_ctr_delkey(subkeys
, subkey
);
1016 W_ERROR_NOT_OK_GOTO(werr
, cancel
);
1018 if (!regdb_store_keys_internal2(regdb
, key
, subkeys
)) {
1019 DEBUG(0, (__location__
" failed to store new subkey_list for "
1020 "parent key %s\n", key
));
1021 werr
= WERR_REG_IO_FAILURE
;
1025 if (regdb
->transaction_commit(regdb
) != 0) {
1026 DEBUG(0, (__location__
" failed to commit transaction\n"));
1027 werr
= WERR_REG_IO_FAILURE
;
1033 if (regdb
->transaction_cancel(regdb
) != 0) {
1034 smb_panic("regdb_delete_subkey: transaction_cancel failed\n");
1038 talloc_free(mem_ctx
);
1042 static TDB_DATA
regdb_fetch_key_internal(struct db_context
*db
,
1043 TALLOC_CTX
*mem_ctx
, const char *key
)
1048 path
= normalize_reg_path(mem_ctx
, key
);
1050 return make_tdb_data(NULL
, 0);
1053 data
= dbwrap_fetch_bystring(db
, mem_ctx
, path
);
1061 * check whether a given key name represents a base key,
1062 * i.e one without a subkey separator ('/' or '\').
1064 static bool regdb_key_is_base_key(const char *key
)
1066 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
1074 path
= normalize_reg_path(mem_ctx
, key
);
1076 DEBUG(0, ("out of memory! (talloc failed)\n"));
1080 if (*path
== '\0') {
1084 ret
= (strrchr(path
, '/') == NULL
);
1087 TALLOC_FREE(mem_ctx
);
1092 * regdb_key_exists() is a very frequent operation. It can be quite
1093 * time-consuming to fully fetch the parent's subkey list, talloc_strdup all
1094 * subkeys and then compare the keyname linearly to all the parent's subkeys.
1096 * The following code tries to make this operation as efficient as possible:
1097 * Per registry key we create a list of subkeys that is very efficient to
1098 * search for existence of a subkey. Its format is:
1100 * 4 bytes num_subkeys
1101 * 4*num_subkey bytes offset into the string array
1102 * then follows a sorted list of subkeys in uppercase
1104 * This record is created by create_sorted_subkeys() on demand if it does not
1105 * exist. scan_parent_subkeys() uses regdb->parse_record to search the sorted
1106 * list, the parsing code and the binary search can be found in
1107 * parent_subkey_scanner. The code uses parse_record() to avoid a memcpy of
1108 * the potentially large subkey record.
1110 * The sorted subkey record is deleted in regdb_store_keys_internal2 and
1111 * recreated on demand.
1114 static int cmp_keynames(const void *p1
, const void *p2
)
1116 return StrCaseCmp(*((char **)p1
), *((char **)p2
));
1119 static bool create_sorted_subkeys(const char *key
, const char *sorted_keyname
)
1121 char **sorted_subkeys
;
1122 struct regsubkey_ctr
*ctr
;
1123 bool result
= false;
1132 if (regdb
->transaction_start(regdb
) != 0) {
1133 DEBUG(0, ("create_sorted_subkeys: transaction_start "
1138 werr
= regsubkey_ctr_init(talloc_tos(), &ctr
);
1139 if (!W_ERROR_IS_OK(werr
)) {
1143 res
= regdb_fetch_keys_internal(regdb
, key
, ctr
);
1148 num_subkeys
= regsubkey_ctr_numkeys(ctr
);
1149 sorted_subkeys
= talloc_array(ctr
, char *, num_subkeys
);
1150 if (sorted_subkeys
== NULL
) {
1154 len
= 4 + 4*num_subkeys
;
1156 for (i
= 0; i
< num_subkeys
; i
++) {
1157 sorted_subkeys
[i
] = talloc_strdup_upper(sorted_subkeys
,
1158 regsubkey_ctr_specific_key(ctr
, i
));
1159 if (sorted_subkeys
[i
] == NULL
) {
1162 len
+= strlen(sorted_subkeys
[i
])+1;
1165 qsort(sorted_subkeys
, num_subkeys
, sizeof(char *), cmp_keynames
);
1167 buf
= talloc_array(ctr
, char, len
);
1171 p
= buf
+ 4 + 4*num_subkeys
;
1173 SIVAL(buf
, 0, num_subkeys
);
1175 for (i
=0; i
< num_subkeys
; i
++) {
1176 ptrdiff_t offset
= p
- buf
;
1177 SIVAL(buf
, 4 + 4*i
, offset
);
1178 strlcpy(p
, sorted_subkeys
[i
], len
-offset
);
1179 p
+= strlen(sorted_subkeys
[i
]) + 1;
1182 status
= dbwrap_store_bystring(
1183 regdb
, sorted_keyname
, make_tdb_data((uint8_t *)buf
, len
),
1185 if (!NT_STATUS_IS_OK(status
)) {
1187 * Don't use a "goto fail;" here, this would commit the broken
1188 * transaction. See below for an explanation.
1190 if (regdb
->transaction_cancel(regdb
) == -1) {
1191 smb_panic("create_sorted_subkeys: transaction_cancel "
1201 * We only get here via the "goto fail" when we did not write anything
1202 * yet. Using transaction_commit even in a failure case is necessary
1203 * because this (disposable) call might be nested in other
1204 * transactions. Doing a cancel here would destroy the possibility of
1205 * a transaction_commit for transactions that we might be wrapped in.
1207 if (regdb
->transaction_commit(regdb
) == -1) {
1208 DEBUG(0, ("create_sorted_subkeys: transaction_commit "
1217 struct scan_subkey_state
{
1223 static int parent_subkey_scanner(TDB_DATA key
, TDB_DATA data
,
1226 struct scan_subkey_state
*state
=
1227 (struct scan_subkey_state
*)private_data
;
1228 uint32_t num_subkeys
;
1231 if (data
.dsize
< sizeof(uint32_t)) {
1235 state
->scanned
= true;
1236 state
->found
= false;
1238 tdb_unpack(data
.dptr
, data
.dsize
, "d", &num_subkeys
);
1244 uint32_t idx
= (l
+u
)/2;
1245 char *s
= (char *)data
.dptr
+ IVAL(data
.dptr
, 4 + 4*idx
);
1246 int comparison
= strcmp(state
->name
, s
);
1248 if (comparison
< 0) {
1250 } else if (comparison
> 0) {
1253 state
->found
= true;
1260 static bool scan_parent_subkeys(struct db_context
*db
, const char *parent
,
1265 struct scan_subkey_state state
= { 0, };
1266 bool result
= false;
1271 path
= normalize_reg_path(talloc_tos(), parent
);
1276 key
= talloc_asprintf(talloc_tos(), "%s/%s",
1277 REG_SORTED_SUBKEYS_PREFIX
, path
);
1282 state
.name
= talloc_strdup_upper(talloc_tos(), name
);
1283 if (state
.name
== NULL
) {
1286 state
.scanned
= false;
1288 res
= db
->parse_record(db
, string_term_tdb_data(key
),
1289 parent_subkey_scanner
, &state
);
1291 if (state
.scanned
) {
1292 result
= state
.found
;
1294 if (!create_sorted_subkeys(path
, key
)) {
1297 res
= db
->parse_record(db
, string_term_tdb_data(key
),
1298 parent_subkey_scanner
, &state
);
1299 if ((res
== 0) && (state
.scanned
)) {
1300 result
= state
.found
;
1306 TALLOC_FREE(state
.name
);
1311 * Check for the existence of a key.
1313 * Existence of a key is authoritatively defined by its
1314 * existence in the list of subkeys of its parent key.
1315 * The exeption of this are keys without a parent key,
1316 * i.e. the "base" keys (HKLM, HKCU, ...).
1318 static bool regdb_key_exists(struct db_context
*db
, const char *key
)
1320 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
1329 path
= normalize_reg_path(mem_ctx
, key
);
1331 DEBUG(0, ("out of memory! (talloc failed)\n"));
1335 if (*path
== '\0') {
1339 p
= strrchr(path
, '/');
1341 /* this is a base key */
1342 value
= regdb_fetch_key_internal(db
, mem_ctx
, path
);
1343 ret
= (value
.dptr
!= NULL
);
1346 ret
= scan_parent_subkeys(db
, path
, p
+1);
1350 TALLOC_FREE(mem_ctx
);
1355 /***********************************************************************
1356 Retrieve an array of strings containing subkeys. Memory should be
1357 released by the caller.
1358 ***********************************************************************/
1360 static int regdb_fetch_keys_internal(struct db_context
*db
, const char *key
,
1361 struct regsubkey_ctr
*ctr
)
1370 TALLOC_CTX
*frame
= talloc_stackframe();
1373 DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key
? key
: "NULL"));
1375 if (!regdb_key_exists(db
, key
)) {
1379 werr
= regsubkey_ctr_set_seqnum(ctr
, db
->get_seqnum(db
));
1380 if (!W_ERROR_IS_OK(werr
)) {
1384 value
= regdb_fetch_key_internal(db
, frame
, key
);
1386 if (value
.dptr
== NULL
) {
1387 DEBUG(10, ("regdb_fetch_keys: no subkeys found for key [%s]\n",
1394 buflen
= value
.dsize
;
1395 len
= tdb_unpack( buf
, buflen
, "d", &num_items
);
1397 for (i
=0; i
<num_items
; i
++) {
1398 len
+= tdb_unpack(buf
+len
, buflen
-len
, "f", subkeyname
);
1399 werr
= regsubkey_ctr_addkey(ctr
, subkeyname
);
1400 if (!W_ERROR_IS_OK(werr
)) {
1401 DEBUG(5, ("regdb_fetch_keys: regsubkey_ctr_addkey "
1402 "failed: %s\n", win_errstr(werr
)));
1407 DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items
));
1415 int regdb_fetch_keys(const char *key
, struct regsubkey_ctr
*ctr
)
1417 return regdb_fetch_keys_internal(regdb
, key
, ctr
);
1420 /****************************************************************************
1421 Unpack a list of registry values frem the TDB
1422 ***************************************************************************/
1424 static int regdb_unpack_values(struct regval_ctr
*values
, uint8
*buf
, int buflen
)
1431 uint32 num_values
= 0;
1434 /* loop and unpack the rest of the registry values */
1436 len
+= tdb_unpack(buf
+len
, buflen
-len
, "d", &num_values
);
1438 for ( i
=0; i
<num_values
; i
++ ) {
1439 /* unpack the next regval */
1444 valuename
[0] = '\0';
1445 len
+= tdb_unpack(buf
+len
, buflen
-len
, "fdB",
1451 /* add the new value. Paranoid protective code -- make sure data_p is valid */
1453 if (*valuename
&& size
&& data_p
) {
1454 regval_ctr_addvalue(values
, valuename
, type
,
1455 (const char *)data_p
, size
);
1457 SAFE_FREE(data_p
); /* 'B' option to tdb_unpack does a malloc() */
1459 DEBUG(8,("specific: [%s], len: %d\n", valuename
, size
));
1465 /****************************************************************************
1466 Pack all values in all printer keys
1467 ***************************************************************************/
1469 static int regdb_pack_values(struct regval_ctr
*values
, uint8
*buf
, int buflen
)
1473 struct regval_blob
*val
;
1479 num_values
= regval_ctr_numvals( values
);
1481 /* pack the number of values first */
1483 len
+= tdb_pack( buf
+len
, buflen
-len
, "d", num_values
);
1485 /* loop over all values */
1487 for ( i
=0; i
<num_values
; i
++ ) {
1488 val
= regval_ctr_specific_value( values
, i
);
1489 len
+= tdb_pack(buf
+len
, buflen
-len
, "fdB",
1493 regval_data_p(val
) );
1499 /***********************************************************************
1500 Retrieve an array of strings containing subkeys. Memory should be
1501 released by the caller.
1502 ***********************************************************************/
1504 static int regdb_fetch_values_internal(struct db_context
*db
, const char* key
,
1505 struct regval_ctr
*values
)
1507 char *keystr
= NULL
;
1508 TALLOC_CTX
*ctx
= talloc_stackframe();
1512 DEBUG(10,("regdb_fetch_values: Looking for value of key [%s] \n", key
));
1514 if (!regdb_key_exists(db
, key
)) {
1518 keystr
= talloc_asprintf(ctx
, "%s/%s", REG_VALUE_PREFIX
, key
);
1523 values
->seqnum
= db
->get_seqnum(db
);
1525 value
= regdb_fetch_key_internal(db
, ctx
, keystr
);
1528 /* all keys have zero values by default */
1532 regdb_unpack_values(values
, value
.dptr
, value
.dsize
);
1533 ret
= regval_ctr_numvals(values
);
1540 int regdb_fetch_values(const char* key
, struct regval_ctr
*values
)
1542 return regdb_fetch_values_internal(regdb
, key
, values
);
1545 static bool regdb_store_values_internal(struct db_context
*db
, const char *key
,
1546 struct regval_ctr
*values
)
1548 TDB_DATA old_data
, data
;
1549 char *keystr
= NULL
;
1550 TALLOC_CTX
*ctx
= talloc_stackframe();
1553 bool result
= false;
1555 DEBUG(10,("regdb_store_values: Looking for value of key [%s] \n", key
));
1557 if (!regdb_key_exists(db
, key
)) {
1563 len
= regdb_pack_values(values
, data
.dptr
, data
.dsize
);
1565 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
1569 data
.dptr
= TALLOC_ARRAY(ctx
, uint8
, len
);
1572 len
= regdb_pack_values(values
, data
.dptr
, data
.dsize
);
1574 SMB_ASSERT( len
== data
.dsize
);
1576 keystr
= talloc_asprintf(ctx
, "%s/%s", REG_VALUE_PREFIX
, key
);
1580 keystr
= normalize_reg_path(ctx
, keystr
);
1585 old_data
= dbwrap_fetch_bystring(db
, ctx
, keystr
);
1587 if ((old_data
.dptr
!= NULL
)
1588 && (old_data
.dsize
== data
.dsize
)
1589 && (memcmp(old_data
.dptr
, data
.dptr
, data
.dsize
) == 0))
1595 status
= dbwrap_trans_store_bystring(db
, keystr
, data
, TDB_REPLACE
);
1597 result
= NT_STATUS_IS_OK(status
);
1604 bool regdb_store_values(const char *key
, struct regval_ctr
*values
)
1606 return regdb_store_values_internal(regdb
, key
, values
);
1609 static WERROR
regdb_get_secdesc(TALLOC_CTX
*mem_ctx
, const char *key
,
1610 struct security_descriptor
**psecdesc
)
1615 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
1616 WERROR err
= WERR_OK
;
1618 DEBUG(10, ("regdb_get_secdesc: Getting secdesc of key [%s]\n", key
));
1620 if (!regdb_key_exists(regdb
, key
)) {
1625 tdbkey
= talloc_asprintf(tmp_ctx
, "%s/%s", REG_SECDESC_PREFIX
, key
);
1626 if (tdbkey
== NULL
) {
1630 normalize_dbkey(tdbkey
);
1632 data
= dbwrap_fetch_bystring(regdb
, tmp_ctx
, tdbkey
);
1633 if (data
.dptr
== NULL
) {
1638 status
= unmarshall_sec_desc(mem_ctx
, (uint8
*)data
.dptr
, data
.dsize
,
1641 if (NT_STATUS_EQUAL(status
, NT_STATUS_NO_MEMORY
)) {
1643 } else if (!NT_STATUS_IS_OK(status
)) {
1644 err
= WERR_REG_CORRUPT
;
1648 TALLOC_FREE(tmp_ctx
);
1652 static WERROR
regdb_set_secdesc(const char *key
,
1653 struct security_descriptor
*secdesc
)
1655 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
1657 WERROR err
= WERR_NOMEM
;
1660 if (!regdb_key_exists(regdb
, key
)) {
1665 tdbkey
= talloc_asprintf(mem_ctx
, "%s/%s", REG_SECDESC_PREFIX
, key
);
1666 if (tdbkey
== NULL
) {
1669 normalize_dbkey(tdbkey
);
1671 if (secdesc
== NULL
) {
1672 /* assuming a delete */
1673 err
= ntstatus_to_werror(dbwrap_trans_delete_bystring(regdb
,
1678 err
= ntstatus_to_werror(marshall_sec_desc(mem_ctx
, secdesc
,
1681 W_ERROR_NOT_OK_GOTO_DONE(err
);
1683 err
= ntstatus_to_werror(dbwrap_trans_store_bystring(regdb
, tdbkey
,
1687 TALLOC_FREE(mem_ctx
);
1691 bool regdb_subkeys_need_update(struct regsubkey_ctr
*subkeys
)
1693 return (regdb_get_seqnum() != regsubkey_ctr_get_seqnum(subkeys
));
1696 bool regdb_values_need_update(struct regval_ctr
*values
)
1698 return (regdb_get_seqnum() != values
->seqnum
);
1702 * Table of function pointers for default access
1705 struct registry_ops regdb_ops
= {
1706 .fetch_subkeys
= regdb_fetch_keys
,
1707 .fetch_values
= regdb_fetch_values
,
1708 .store_subkeys
= regdb_store_keys
,
1709 .store_values
= regdb_store_values
,
1710 .create_subkey
= regdb_create_subkey
,
1711 .delete_subkey
= regdb_delete_subkey
,
1712 .get_secdesc
= regdb_get_secdesc
,
1713 .set_secdesc
= regdb_set_secdesc
,
1714 .subkeys_need_update
= regdb_subkeys_need_update
,
1715 .values_need_update
= regdb_values_need_update