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. */
26 #define DBGC_CLASS DBGC_REGISTRY
28 static struct db_context
*regdb
= NULL
;
29 static int regdb_refcount
;
31 static bool regdb_key_exists(struct db_context
*db
, const char *key
);
32 static bool regdb_key_is_base_key(const char *key
);
33 static WERROR
regdb_fetch_keys_internal(struct db_context
*db
, const char *key
,
34 struct regsubkey_ctr
*ctr
);
35 static bool regdb_store_keys_internal(struct db_context
*db
, const char *key
,
36 struct regsubkey_ctr
*ctr
);
37 static int regdb_fetch_values_internal(struct db_context
*db
, const char* key
,
38 struct regval_ctr
*values
);
39 static bool regdb_store_values_internal(struct db_context
*db
, const char *key
,
40 struct regval_ctr
*values
);
42 /* List the deepest path into the registry. All part components will be created.*/
44 /* If you want to have a part of the path controlled by the tdb and part by
45 a virtual registry db (e.g. printing), then you have to list the deepest path.
46 For example,"HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion/Print"
47 allows the reg_db backend to handle everything up to
48 "HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion" and then we'll hook
49 the reg_printing backend onto the last component of the path (see
50 KEY_PRINTING_2K in include/rpc_reg.h) --jerry */
52 static const char *builtin_registry_paths
[] = {
62 KEY_SAMBA_GROUP_POLICY
,
63 KEY_GP_MACHINE_POLICY
,
64 KEY_GP_MACHINE_WIN_POLICY
,
67 KEY_GP_USER_WIN_POLICY
,
68 KEY_WINLOGON_GPEXT_PATH
,
69 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Print\\Monitors",
71 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\DefaultUserConfiguration",
80 struct builtin_regkey_value
{
82 const char *valuename
;
90 static struct builtin_regkey_value builtin_registry_values
[] = {
92 SAMBA_PRINTER_PORT_NAME
, REG_SZ
, { "" } },
94 "DefaultSpoolDirectory", REG_SZ
, { "C:\\Windows\\System32\\Spool\\Printers" } },
96 "DisplayName", REG_SZ
, { "Event Log" } },
98 "ErrorControl", REG_DWORD
, { (char*)0x00000001 } },
99 { NULL
, NULL
, 0, { NULL
} }
103 * Initialize a key in the registry:
104 * create each component key of the specified path.
106 static WERROR
init_registry_key_internal(struct db_context
*db
,
107 const char *add_path
)
110 TALLOC_CTX
*frame
= talloc_stackframe();
113 char *remaining
= NULL
;
116 struct regsubkey_ctr
*subkeys
;
119 DEBUG(6, ("init_registry_key: Adding [%s]\n", add_path
));
121 path
= talloc_strdup(frame
, add_path
);
122 base
= talloc_strdup(frame
, "");
123 if (!path
|| !base
) {
129 while (next_token_talloc(frame
, &p
, &keyname
, "\\")) {
131 /* build up the registry path from the components */
134 base
= talloc_asprintf(frame
, "%s\\", base
);
140 base
= talloc_asprintf_append(base
, "%s", keyname
);
146 /* get the immediate subkeyname (if we have one ) */
148 subkeyname
= talloc_strdup(frame
, "");
154 remaining
= talloc_strdup(frame
, p
);
161 if (!next_token_talloc(frame
, &p2
,
164 subkeyname
= talloc_strdup(frame
,p2
);
172 DEBUG(10,("init_registry_key: Storing key [%s] with "
173 "subkey [%s]\n", base
,
174 *subkeyname
? subkeyname
: "NULL"));
176 /* we don't really care if the lookup succeeds or not
177 * since we are about to update the record.
178 * We just want any subkeys already present */
180 werr
= regsubkey_ctr_init(frame
, &subkeys
);
181 if (!W_ERROR_IS_OK(werr
)) {
182 DEBUG(0,("talloc() failure!\n"));
186 werr
= regdb_fetch_keys_internal(db
, base
, subkeys
);
187 if (!W_ERROR_IS_OK(werr
) &&
188 !W_ERROR_EQUAL(werr
, WERR_NOT_FOUND
))
194 werr
= regsubkey_ctr_addkey(subkeys
, subkeyname
);
195 if (!W_ERROR_IS_OK(werr
)) {
199 if (!regdb_store_keys_internal(db
, base
, subkeys
)) {
200 werr
= WERR_CAN_NOT_COMPLETE
;
212 struct init_registry_key_context
{
213 const char *add_path
;
216 static NTSTATUS
init_registry_key_action(struct db_context
*db
,
219 struct init_registry_key_context
*init_ctx
=
220 (struct init_registry_key_context
*)private_data
;
222 return werror_to_ntstatus(init_registry_key_internal(
223 db
, init_ctx
->add_path
));
227 * Initialize a key in the registry:
228 * create each component key of the specified path,
229 * wrapped in one db transaction.
231 WERROR
init_registry_key(const char *add_path
)
233 struct init_registry_key_context init_ctx
;
235 if (regdb_key_exists(regdb
, add_path
)) {
239 init_ctx
.add_path
= add_path
;
241 return ntstatus_to_werror(dbwrap_trans_do(regdb
,
242 init_registry_key_action
,
246 /***********************************************************************
247 Open the registry data in the tdb
248 ***********************************************************************/
250 static void regdb_ctr_add_value(struct regval_ctr
*ctr
,
251 struct builtin_regkey_value
*value
)
255 switch(value
->type
) {
257 regval_ctr_addvalue(ctr
, value
->valuename
, REG_DWORD
,
258 (char*)&value
->data
.dw_value
,
263 init_unistr2(&data
, value
->data
.string
, UNI_STR_TERMINATE
);
264 regval_ctr_addvalue(ctr
, value
->valuename
, REG_SZ
,
266 data
.uni_str_len
*sizeof(uint16
));
270 DEBUG(0, ("regdb_ctr_add_value: invalid value type in "
271 "registry values [%d]\n", value
->type
));
275 static NTSTATUS
init_registry_data_action(struct db_context
*db
,
279 TALLOC_CTX
*frame
= talloc_stackframe();
280 struct regval_ctr
*values
;
283 /* loop over all of the predefined paths and add each component */
285 for (i
=0; builtin_registry_paths
[i
] != NULL
; i
++) {
286 if (regdb_key_exists(db
, builtin_registry_paths
[i
])) {
289 status
= werror_to_ntstatus(init_registry_key_internal(db
,
290 builtin_registry_paths
[i
]));
291 if (!NT_STATUS_IS_OK(status
)) {
296 /* loop over all of the predefined values and add each component */
298 for (i
=0; builtin_registry_values
[i
].path
!= NULL
; i
++) {
300 values
= TALLOC_ZERO_P(frame
, struct regval_ctr
);
301 if (values
== NULL
) {
302 status
= NT_STATUS_NO_MEMORY
;
306 regdb_fetch_values_internal(db
,
307 builtin_registry_values
[i
].path
,
310 /* preserve existing values across restarts. Only add new ones */
312 if (!regval_ctr_key_exists(values
,
313 builtin_registry_values
[i
].valuename
))
315 regdb_ctr_add_value(values
,
316 &builtin_registry_values
[i
]);
317 regdb_store_values_internal(db
,
318 builtin_registry_values
[i
].path
,
324 status
= NT_STATUS_OK
;
332 WERROR
init_registry_data(void)
335 TALLOC_CTX
*frame
= talloc_stackframe();
336 struct regval_ctr
*values
;
340 * First, check for the existence of the needed keys and values.
341 * If all do already exist, we can save the writes.
343 for (i
=0; builtin_registry_paths
[i
] != NULL
; i
++) {
344 if (!regdb_key_exists(regdb
, builtin_registry_paths
[i
])) {
349 for (i
=0; builtin_registry_values
[i
].path
!= NULL
; i
++) {
350 values
= TALLOC_ZERO_P(frame
, struct regval_ctr
);
351 if (values
== NULL
) {
356 regdb_fetch_values_internal(regdb
,
357 builtin_registry_values
[i
].path
,
359 if (!regval_ctr_key_exists(values
,
360 builtin_registry_values
[i
].valuename
))
375 * There are potentially quite a few store operations which are all
376 * indiviually wrapped in tdb transactions. Wrapping them in a single
377 * transaction gives just a single transaction_commit() to actually do
378 * its fsync()s. See tdb/common/transaction.c for info about nested
379 * transaction behaviour.
382 werr
= ntstatus_to_werror(dbwrap_trans_do(regdb
,
383 init_registry_data_action
,
391 /***********************************************************************
392 Open the registry database
393 ***********************************************************************/
395 WERROR
regdb_init(void)
397 const char *vstring
= "INFO/version";
402 DEBUG(10, ("regdb_init: incrementing refcount (%d)\n",
408 regdb
= db_open(NULL
, state_path("registry.tdb"), 0,
409 REG_TDB_FLAGS
, O_RDWR
, 0600);
411 regdb
= db_open(NULL
, state_path("registry.tdb"), 0,
412 REG_TDB_FLAGS
, O_RDWR
|O_CREAT
, 0600);
414 werr
= ntstatus_to_werror(map_nt_error_from_unix(errno
));
415 DEBUG(1,("regdb_init: Failed to open registry %s (%s)\n",
416 state_path("registry.tdb"), strerror(errno
) ));
420 DEBUG(10,("regdb_init: Successfully created registry tdb\n"));
425 vers_id
= dbwrap_fetch_int32(regdb
, vstring
);
427 if ( vers_id
!= REGVER_V1
) {
429 /* any upgrade code here if needed */
430 DEBUG(10, ("regdb_init: got %s = %d != %d\n", vstring
,
431 vers_id
, REGVER_V1
));
432 status
= dbwrap_trans_store_int32(regdb
, vstring
, REGVER_V1
);
433 if (!NT_STATUS_IS_OK(status
)) {
434 DEBUG(1, ("regdb_init: error storing %s = %d: %s\n",
435 vstring
, REGVER_V1
, nt_errstr(status
)));
436 return ntstatus_to_werror(status
);
438 DEBUG(10, ("regdb_init: stored %s = %d\n",
439 vstring
, REGVER_V1
));
446 /***********************************************************************
447 Open the registry. Must already have been initialized by regdb_init()
448 ***********************************************************************/
450 WERROR
regdb_open( void )
452 WERROR result
= WERR_OK
;
455 DEBUG(10,("regdb_open: incrementing refcount (%d)\n", regdb_refcount
));
462 regdb
= db_open(NULL
, state_path("registry.tdb"), 0,
463 REG_TDB_FLAGS
, O_RDWR
, 0600);
465 result
= ntstatus_to_werror( map_nt_error_from_unix( errno
) );
466 DEBUG(0,("regdb_open: Failed to open %s! (%s)\n",
467 state_path("registry.tdb"), strerror(errno
) ));
473 DEBUG(10,("regdb_open: refcount reset (%d)\n", regdb_refcount
));
478 /***********************************************************************
479 ***********************************************************************/
481 int regdb_close( void )
483 if (regdb_refcount
== 0) {
489 DEBUG(10,("regdb_close: decrementing refcount (%d)\n", regdb_refcount
));
491 if ( regdb_refcount
> 0 )
494 SMB_ASSERT( regdb_refcount
>= 0 );
500 WERROR
regdb_transaction_start(void)
502 return (regdb
->transaction_start(regdb
) == 0) ?
503 WERR_OK
: WERR_REG_IO_FAILURE
;
506 WERROR
regdb_transaction_commit(void)
508 return (regdb
->transaction_commit(regdb
) == 0) ?
509 WERR_OK
: WERR_REG_IO_FAILURE
;
512 WERROR
regdb_transaction_cancel(void)
514 return (regdb
->transaction_cancel(regdb
) == 0) ?
515 WERR_OK
: WERR_REG_IO_FAILURE
;
518 /***********************************************************************
519 return the tdb sequence number of the registry tdb.
520 this is an indicator for the content of the registry
521 having changed. it will change upon regdb_init, too, though.
522 ***********************************************************************/
523 int regdb_get_seqnum(void)
525 return regdb
->get_seqnum(regdb
);
529 static WERROR
regdb_delete_key_with_prefix(struct db_context
*db
,
534 WERROR werr
= WERR_NOMEM
;
535 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
537 if (keyname
== NULL
) {
538 werr
= WERR_INVALID_PARAM
;
542 if (prefix
== NULL
) {
543 path
= discard_const_p(char, keyname
);
545 path
= talloc_asprintf(mem_ctx
, "%s/%s", prefix
, keyname
);
551 path
= normalize_reg_path(mem_ctx
, path
);
556 werr
= ntstatus_to_werror(dbwrap_delete_bystring(db
, path
));
558 /* treat "not" found" as ok */
559 if (W_ERROR_EQUAL(werr
, WERR_NOT_FOUND
)) {
564 talloc_free(mem_ctx
);
569 static WERROR
regdb_delete_values(struct db_context
*db
, const char *keyname
)
571 return regdb_delete_key_with_prefix(db
, keyname
, REG_VALUE_PREFIX
);
574 static WERROR
regdb_delete_secdesc(struct db_context
*db
, const char *keyname
)
576 return regdb_delete_key_with_prefix(db
, keyname
, REG_SECDESC_PREFIX
);
579 static WERROR
regdb_delete_subkeylist(struct db_context
*db
, const char *keyname
)
581 return regdb_delete_key_with_prefix(db
, keyname
, NULL
);
584 static WERROR
regdb_delete_key_lists(struct db_context
*db
, const char *keyname
)
588 werr
= regdb_delete_values(db
, keyname
);
589 if (!W_ERROR_IS_OK(werr
)) {
590 DEBUG(1, (__location__
" Deleting %s/%s failed: %s\n",
591 REG_VALUE_PREFIX
, keyname
, win_errstr(werr
)));
595 werr
= regdb_delete_secdesc(db
, keyname
);
596 if (!W_ERROR_IS_OK(werr
)) {
597 DEBUG(1, (__location__
" Deleting %s/%s failed: %s\n",
598 REG_SECDESC_PREFIX
, keyname
, win_errstr(werr
)));
602 werr
= regdb_delete_subkeylist(db
, keyname
);
603 if (!W_ERROR_IS_OK(werr
)) {
604 DEBUG(1, (__location__
" Deleting %s failed: %s\n",
605 keyname
, win_errstr(werr
)));
613 /***********************************************************************
614 Add subkey strings to the registry tdb under a defined key
615 fmt is the same format as tdb_pack except this function only supports
617 ***********************************************************************/
619 static WERROR
regdb_store_keys_internal2(struct db_context
*db
,
621 struct regsubkey_ctr
*ctr
)
624 uint8
*buffer
= NULL
;
627 uint32 num_subkeys
= regsubkey_ctr_numkeys(ctr
);
628 char *keyname
= NULL
;
629 TALLOC_CTX
*ctx
= talloc_stackframe();
633 werr
= WERR_INVALID_PARAM
;
637 keyname
= talloc_strdup(ctx
, key
);
643 keyname
= normalize_reg_path(ctx
, keyname
);
649 /* allocate some initial memory */
651 buffer
= (uint8
*)SMB_MALLOC(1024);
652 if (buffer
== NULL
) {
659 /* store the number of subkeys */
661 len
+= tdb_pack(buffer
+len
, buflen
-len
, "d", num_subkeys
);
663 /* pack all the strings */
665 for (i
=0; i
<num_subkeys
; i
++) {
668 thistime
= tdb_pack(buffer
+len
, buflen
-len
, "f",
669 regsubkey_ctr_specific_key(ctr
, i
));
670 if (len
+thistime
> buflen
) {
673 * tdb_pack hasn't done anything because of the short
674 * buffer, allocate extra space.
676 buffer
= SMB_REALLOC_ARRAY(buffer
, uint8_t,
679 DEBUG(0, ("regdb_store_keys: Failed to realloc "
680 "memory of size [%u]\n",
681 (unsigned int)(len
+thistime
)*2));
685 buflen
= (len
+thistime
)*2;
686 thistime2
= tdb_pack(
687 buffer
+len
, buflen
-len
, "f",
688 regsubkey_ctr_specific_key(ctr
, i
));
689 if (thistime2
!= thistime
) {
690 DEBUG(0, ("tdb_pack failed\n"));
691 werr
= WERR_CAN_NOT_COMPLETE
;
698 /* finally write out the data */
702 werr
= ntstatus_to_werror(dbwrap_store_bystring(db
, keyname
, dbuf
,
704 W_ERROR_NOT_OK_GOTO_DONE(werr
);
707 * Delete a sorted subkey cache for regdb_key_exists, will be
708 * recreated automatically
710 keyname
= talloc_asprintf(ctx
, "%s/%s", REG_SORTED_SUBKEYS_PREFIX
,
712 if (keyname
== NULL
) {
717 werr
= ntstatus_to_werror(dbwrap_delete_bystring(db
, keyname
));
719 /* don't treat WERR_NOT_FOUND as an error here */
720 if (W_ERROR_EQUAL(werr
, WERR_NOT_FOUND
)) {
730 /***********************************************************************
731 Store the new subkey record and create any child key records that
732 do not currently exist
733 ***********************************************************************/
735 struct regdb_store_keys_context
{
737 struct regsubkey_ctr
*ctr
;
740 static NTSTATUS
regdb_store_keys_action(struct db_context
*db
,
743 struct regdb_store_keys_context
*store_ctx
;
747 struct regsubkey_ctr
*subkeys
= NULL
, *old_subkeys
= NULL
;
748 char *oldkeyname
= NULL
;
749 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
751 store_ctx
= (struct regdb_store_keys_context
*)private_data
;
754 * Re-fetch the old keys inside the transaction
757 werr
= regsubkey_ctr_init(mem_ctx
, &old_subkeys
);
758 W_ERROR_NOT_OK_GOTO_DONE(werr
);
760 werr
= regdb_fetch_keys_internal(db
, store_ctx
->key
, old_subkeys
);
761 if (!W_ERROR_IS_OK(werr
) &&
762 !W_ERROR_EQUAL(werr
, WERR_NOT_FOUND
))
768 * Make the store operation as safe as possible without transactions:
770 * (1) For each subkey removed from ctr compared with old_subkeys:
772 * (a) First delete the value db entry.
774 * (b) Next delete the secdesc db record.
776 * (c) Then delete the subkey list entry.
778 * (2) Now write the list of subkeys of the parent key,
779 * deleting removed entries and adding new ones.
781 * (3) Finally create the subkey list entries for the added keys.
783 * This way if we crash half-way in between deleting the subkeys
784 * and storing the parent's list of subkeys, no old data can pop up
785 * out of the blue when re-adding keys later on.
788 /* (1) delete removed keys' lists (values/secdesc/subkeys) */
790 num_subkeys
= regsubkey_ctr_numkeys(old_subkeys
);
791 for (i
=0; i
<num_subkeys
; i
++) {
792 oldkeyname
= regsubkey_ctr_specific_key(old_subkeys
, i
);
794 if (regsubkey_ctr_key_exists(store_ctx
->ctr
, oldkeyname
)) {
796 * It's still around, don't delete
801 path
= talloc_asprintf(mem_ctx
, "%s/%s", store_ctx
->key
,
808 werr
= regdb_delete_key_lists(db
, path
);
809 W_ERROR_NOT_OK_GOTO_DONE(werr
);
814 TALLOC_FREE(old_subkeys
);
816 /* (2) store the subkey list for the parent */
818 werr
= regdb_store_keys_internal2(db
, store_ctx
->key
, store_ctx
->ctr
);
819 if (!W_ERROR_IS_OK(werr
)) {
820 DEBUG(0,("regdb_store_keys: Failed to store new subkey list "
821 "for parent [%s]: %s\n", store_ctx
->key
,
826 /* (3) now create records for any subkeys that don't already exist */
828 num_subkeys
= regsubkey_ctr_numkeys(store_ctx
->ctr
);
830 if (num_subkeys
== 0) {
831 werr
= regsubkey_ctr_init(mem_ctx
, &subkeys
);
832 W_ERROR_NOT_OK_GOTO_DONE(werr
);
834 werr
= regdb_store_keys_internal2(db
, store_ctx
->key
, subkeys
);
835 if (!W_ERROR_IS_OK(werr
)) {
836 DEBUG(0,("regdb_store_keys: Failed to store "
837 "new record for key [%s]: %s\n",
838 store_ctx
->key
, win_errstr(werr
)));
841 TALLOC_FREE(subkeys
);
844 for (i
=0; i
<num_subkeys
; i
++) {
845 path
= talloc_asprintf(mem_ctx
, "%s/%s", store_ctx
->key
,
846 regsubkey_ctr_specific_key(store_ctx
->ctr
, i
));
851 werr
= regsubkey_ctr_init(mem_ctx
, &subkeys
);
852 W_ERROR_NOT_OK_GOTO_DONE(werr
);
854 werr
= regdb_fetch_keys_internal(db
, path
, subkeys
);
855 if (!W_ERROR_IS_OK(werr
)) {
856 /* create a record with 0 subkeys */
857 werr
= regdb_store_keys_internal2(db
, path
, subkeys
);
858 if (!W_ERROR_IS_OK(werr
)) {
859 DEBUG(0,("regdb_store_keys: Failed to store "
860 "new record for key [%s]: %s\n", path
,
866 TALLOC_FREE(subkeys
);
873 talloc_free(mem_ctx
);
874 return werror_to_ntstatus(werr
);
877 static bool regdb_store_keys_internal(struct db_context
*db
, const char *key
,
878 struct regsubkey_ctr
*ctr
)
880 int num_subkeys
, old_num_subkeys
, i
;
881 struct regsubkey_ctr
*old_subkeys
= NULL
;
882 TALLOC_CTX
*ctx
= talloc_stackframe();
885 struct regdb_store_keys_context store_ctx
;
887 if (!regdb_key_is_base_key(key
) && !regdb_key_exists(db
, key
)) {
892 * fetch a list of the old subkeys so we can determine if anything has
896 werr
= regsubkey_ctr_init(ctx
, &old_subkeys
);
897 if (!W_ERROR_IS_OK(werr
)) {
898 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
902 werr
= regdb_fetch_keys_internal(db
, key
, old_subkeys
);
903 if (!W_ERROR_IS_OK(werr
) &&
904 !W_ERROR_EQUAL(werr
, WERR_NOT_FOUND
))
909 num_subkeys
= regsubkey_ctr_numkeys(ctr
);
910 old_num_subkeys
= regsubkey_ctr_numkeys(old_subkeys
);
911 if ((num_subkeys
&& old_num_subkeys
) &&
912 (num_subkeys
== old_num_subkeys
)) {
914 for (i
= 0; i
< num_subkeys
; i
++) {
915 if (strcmp(regsubkey_ctr_specific_key(ctr
, i
),
916 regsubkey_ctr_specific_key(old_subkeys
, i
))
922 if (i
== num_subkeys
) {
924 * Nothing changed, no point to even start a tdb
933 TALLOC_FREE(old_subkeys
);
938 werr
= ntstatus_to_werror(dbwrap_trans_do(db
,
939 regdb_store_keys_action
,
942 ret
= W_ERROR_IS_OK(werr
);
950 bool regdb_store_keys(const char *key
, struct regsubkey_ctr
*ctr
)
952 return regdb_store_keys_internal(regdb
, key
, ctr
);
956 * create a subkey of a given key
959 struct regdb_create_subkey_context
{
964 static NTSTATUS
regdb_create_subkey_action(struct db_context
*db
,
968 struct regdb_create_subkey_context
*create_ctx
;
969 struct regsubkey_ctr
*subkeys
;
970 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
972 create_ctx
= (struct regdb_create_subkey_context
*)private_data
;
974 werr
= regsubkey_ctr_init(mem_ctx
, &subkeys
);
975 W_ERROR_NOT_OK_GOTO_DONE(werr
);
977 werr
= regdb_fetch_keys_internal(db
, create_ctx
->key
, subkeys
);
978 W_ERROR_NOT_OK_GOTO_DONE(werr
);
980 werr
= regsubkey_ctr_addkey(subkeys
, create_ctx
->subkey
);
981 W_ERROR_NOT_OK_GOTO_DONE(werr
);
983 werr
= regdb_store_keys_internal2(db
, create_ctx
->key
, subkeys
);
984 if (!W_ERROR_IS_OK(werr
)) {
985 DEBUG(0, (__location__
" failed to store new subkey list for "
986 "parent key %s: %s\n", create_ctx
->key
,
991 talloc_free(mem_ctx
);
992 return werror_to_ntstatus(werr
);
995 static WERROR
regdb_create_subkey(const char *key
, const char *subkey
)
998 struct regsubkey_ctr
*subkeys
;
999 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
1000 struct regdb_create_subkey_context create_ctx
;
1002 if (!regdb_key_is_base_key(key
) && !regdb_key_exists(regdb
, key
)) {
1003 werr
= WERR_NOT_FOUND
;
1007 werr
= regsubkey_ctr_init(mem_ctx
, &subkeys
);
1008 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1010 werr
= regdb_fetch_keys_internal(regdb
, key
, subkeys
);
1011 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1013 if (regsubkey_ctr_key_exists(subkeys
, subkey
)) {
1018 talloc_free(subkeys
);
1020 create_ctx
.key
= key
;
1021 create_ctx
.subkey
= subkey
;
1023 werr
= ntstatus_to_werror(dbwrap_trans_do(regdb
,
1024 regdb_create_subkey_action
,
1028 talloc_free(mem_ctx
);
1033 * create a subkey of a given key
1036 struct regdb_delete_subkey_context
{
1042 static NTSTATUS
regdb_delete_subkey_action(struct db_context
*db
,
1046 struct regdb_delete_subkey_context
*delete_ctx
;
1047 struct regsubkey_ctr
*subkeys
;
1048 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
1050 delete_ctx
= (struct regdb_delete_subkey_context
*)private_data
;
1052 werr
= regdb_delete_key_lists(db
, delete_ctx
->path
);
1053 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1055 werr
= regsubkey_ctr_init(mem_ctx
, &subkeys
);
1056 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1058 werr
= regdb_fetch_keys_internal(db
, delete_ctx
->key
, subkeys
);
1059 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1061 werr
= regsubkey_ctr_delkey(subkeys
, delete_ctx
->subkey
);
1062 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1064 werr
= regdb_store_keys_internal2(db
, delete_ctx
->key
, subkeys
);
1065 if (!W_ERROR_IS_OK(werr
)) {
1066 DEBUG(0, (__location__
" failed to store new subkey_list for "
1067 "parent key %s: %s\n", delete_ctx
->key
,
1072 talloc_free(mem_ctx
);
1073 return werror_to_ntstatus(werr
);
1076 static WERROR
regdb_delete_subkey(const char *key
, const char *subkey
)
1080 struct regdb_delete_subkey_context delete_ctx
;
1081 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
1083 if (!regdb_key_is_base_key(key
) && !regdb_key_exists(regdb
, key
)) {
1084 werr
= WERR_NOT_FOUND
;
1088 path
= talloc_asprintf(mem_ctx
, "%s/%s", key
, subkey
);
1094 if (!regdb_key_exists(regdb
, path
)) {
1099 delete_ctx
.key
= key
;
1100 delete_ctx
.subkey
= subkey
;
1101 delete_ctx
.path
= path
;
1103 werr
= ntstatus_to_werror(dbwrap_trans_do(regdb
,
1104 regdb_delete_subkey_action
,
1108 talloc_free(mem_ctx
);
1112 static TDB_DATA
regdb_fetch_key_internal(struct db_context
*db
,
1113 TALLOC_CTX
*mem_ctx
, const char *key
)
1118 path
= normalize_reg_path(mem_ctx
, key
);
1120 return make_tdb_data(NULL
, 0);
1123 data
= dbwrap_fetch_bystring(db
, mem_ctx
, path
);
1131 * check whether a given key name represents a base key,
1132 * i.e one without a subkey separator ('/' or '\').
1134 static bool regdb_key_is_base_key(const char *key
)
1136 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
1144 path
= normalize_reg_path(mem_ctx
, key
);
1146 DEBUG(0, ("out of memory! (talloc failed)\n"));
1150 if (*path
== '\0') {
1154 ret
= (strrchr(path
, '/') == NULL
);
1157 TALLOC_FREE(mem_ctx
);
1162 * regdb_key_exists() is a very frequent operation. It can be quite
1163 * time-consuming to fully fetch the parent's subkey list, talloc_strdup all
1164 * subkeys and then compare the keyname linearly to all the parent's subkeys.
1166 * The following code tries to make this operation as efficient as possible:
1167 * Per registry key we create a list of subkeys that is very efficient to
1168 * search for existence of a subkey. Its format is:
1170 * 4 bytes num_subkeys
1171 * 4*num_subkey bytes offset into the string array
1172 * then follows a sorted list of subkeys in uppercase
1174 * This record is created by create_sorted_subkeys() on demand if it does not
1175 * exist. scan_parent_subkeys() uses regdb->parse_record to search the sorted
1176 * list, the parsing code and the binary search can be found in
1177 * parent_subkey_scanner. The code uses parse_record() to avoid a memcpy of
1178 * the potentially large subkey record.
1180 * The sorted subkey record is deleted in regdb_store_keys_internal2 and
1181 * recreated on demand.
1184 static int cmp_keynames(const void *p1
, const void *p2
)
1186 return StrCaseCmp(*((char **)p1
), *((char **)p2
));
1189 struct create_sorted_subkeys_context
{
1191 const char *sorted_keyname
;
1194 static NTSTATUS
create_sorted_subkeys_action(struct db_context
*db
,
1197 char **sorted_subkeys
;
1198 struct regsubkey_ctr
*ctr
;
1205 struct create_sorted_subkeys_context
*sorted_ctx
;
1207 sorted_ctx
= (struct create_sorted_subkeys_context
*)private_data
;
1210 * In this function, we only treat failing of the actual write to
1211 * the db as a real error. All preliminary errors, at a stage when
1212 * nothing has been written to the DB yet are treated as success
1213 * to be committed (as an empty transaction).
1215 * The reason is that this (disposable) call might be nested in other
1216 * transactions. Doing a cancel here would destroy the possibility of
1217 * a transaction_commit for transactions that we might be wrapped in.
1220 status
= werror_to_ntstatus(regsubkey_ctr_init(talloc_tos(), &ctr
));
1221 if (!NT_STATUS_IS_OK(status
)) {
1222 /* don't treat this as an error */
1223 status
= NT_STATUS_OK
;
1227 status
= werror_to_ntstatus(regdb_fetch_keys_internal(db
,
1230 if (!NT_STATUS_IS_OK(status
)) {
1231 /* don't treat this as an error */
1232 status
= NT_STATUS_OK
;
1236 num_subkeys
= regsubkey_ctr_numkeys(ctr
);
1237 sorted_subkeys
= talloc_array(ctr
, char *, num_subkeys
);
1238 if (sorted_subkeys
== NULL
) {
1239 /* don't treat this as an error */
1243 len
= 4 + 4*num_subkeys
;
1245 for (i
= 0; i
< num_subkeys
; i
++) {
1246 sorted_subkeys
[i
] = talloc_strdup_upper(sorted_subkeys
,
1247 regsubkey_ctr_specific_key(ctr
, i
));
1248 if (sorted_subkeys
[i
] == NULL
) {
1249 /* don't treat this as an error */
1252 len
+= strlen(sorted_subkeys
[i
])+1;
1255 qsort(sorted_subkeys
, num_subkeys
, sizeof(char *), cmp_keynames
);
1257 buf
= talloc_array(ctr
, char, len
);
1259 /* don't treat this as an error */
1262 p
= buf
+ 4 + 4*num_subkeys
;
1264 SIVAL(buf
, 0, num_subkeys
);
1266 for (i
=0; i
< num_subkeys
; i
++) {
1267 ptrdiff_t offset
= p
- buf
;
1268 SIVAL(buf
, 4 + 4*i
, offset
);
1269 strlcpy(p
, sorted_subkeys
[i
], len
-offset
);
1270 p
+= strlen(sorted_subkeys
[i
]) + 1;
1273 status
= dbwrap_store_bystring(
1274 db
, sorted_ctx
->sorted_keyname
, make_tdb_data((uint8_t *)buf
,
1283 static bool create_sorted_subkeys(const char *key
, const char *sorted_keyname
)
1286 struct create_sorted_subkeys_context sorted_ctx
;
1288 sorted_ctx
.key
= key
;
1289 sorted_ctx
.sorted_keyname
= sorted_keyname
;
1291 status
= dbwrap_trans_do(regdb
,
1292 create_sorted_subkeys_action
,
1295 return NT_STATUS_IS_OK(status
);
1298 struct scan_subkey_state
{
1304 static int parent_subkey_scanner(TDB_DATA key
, TDB_DATA data
,
1307 struct scan_subkey_state
*state
=
1308 (struct scan_subkey_state
*)private_data
;
1309 uint32_t num_subkeys
;
1312 if (data
.dsize
< sizeof(uint32_t)) {
1316 state
->scanned
= true;
1317 state
->found
= false;
1319 tdb_unpack(data
.dptr
, data
.dsize
, "d", &num_subkeys
);
1325 uint32_t idx
= (l
+u
)/2;
1326 char *s
= (char *)data
.dptr
+ IVAL(data
.dptr
, 4 + 4*idx
);
1327 int comparison
= strcmp(state
->name
, s
);
1329 if (comparison
< 0) {
1331 } else if (comparison
> 0) {
1334 state
->found
= true;
1341 static bool scan_parent_subkeys(struct db_context
*db
, const char *parent
,
1346 struct scan_subkey_state state
= { 0, };
1347 bool result
= false;
1352 path
= normalize_reg_path(talloc_tos(), parent
);
1357 key
= talloc_asprintf(talloc_tos(), "%s/%s",
1358 REG_SORTED_SUBKEYS_PREFIX
, path
);
1363 state
.name
= talloc_strdup_upper(talloc_tos(), name
);
1364 if (state
.name
== NULL
) {
1367 state
.scanned
= false;
1369 res
= db
->parse_record(db
, string_term_tdb_data(key
),
1370 parent_subkey_scanner
, &state
);
1372 if (state
.scanned
) {
1373 result
= state
.found
;
1375 if (!create_sorted_subkeys(path
, key
)) {
1378 res
= db
->parse_record(db
, string_term_tdb_data(key
),
1379 parent_subkey_scanner
, &state
);
1380 if ((res
== 0) && (state
.scanned
)) {
1381 result
= state
.found
;
1387 TALLOC_FREE(state
.name
);
1392 * Check for the existence of a key.
1394 * Existence of a key is authoritatively defined by its
1395 * existence in the list of subkeys of its parent key.
1396 * The exeption of this are keys without a parent key,
1397 * i.e. the "base" keys (HKLM, HKCU, ...).
1399 static bool regdb_key_exists(struct db_context
*db
, const char *key
)
1401 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
1410 path
= normalize_reg_path(mem_ctx
, key
);
1412 DEBUG(0, ("out of memory! (talloc failed)\n"));
1416 if (*path
== '\0') {
1420 p
= strrchr(path
, '/');
1422 /* this is a base key */
1423 value
= regdb_fetch_key_internal(db
, mem_ctx
, path
);
1424 ret
= (value
.dptr
!= NULL
);
1427 ret
= scan_parent_subkeys(db
, path
, p
+1);
1431 TALLOC_FREE(mem_ctx
);
1436 /***********************************************************************
1437 Retrieve an array of strings containing subkeys. Memory should be
1438 released by the caller.
1439 ***********************************************************************/
1441 static WERROR
regdb_fetch_keys_internal(struct db_context
*db
, const char *key
,
1442 struct regsubkey_ctr
*ctr
)
1450 TALLOC_CTX
*frame
= talloc_stackframe();
1453 DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key
? key
: "NULL"));
1455 frame
= talloc_stackframe();
1457 if (!regdb_key_exists(db
, key
)) {
1458 DEBUG(10, ("key [%s] not found\n", key
));
1459 werr
= WERR_NOT_FOUND
;
1463 werr
= regsubkey_ctr_set_seqnum(ctr
, db
->get_seqnum(db
));
1464 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1466 value
= regdb_fetch_key_internal(db
, frame
, key
);
1468 if (value
.dptr
== NULL
) {
1469 DEBUG(10, ("regdb_fetch_keys: no subkeys found for key [%s]\n",
1475 buflen
= value
.dsize
;
1476 len
= tdb_unpack( buf
, buflen
, "d", &num_items
);
1478 werr
= regsubkey_ctr_reinit(ctr
);
1479 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1481 for (i
=0; i
<num_items
; i
++) {
1482 len
+= tdb_unpack(buf
+len
, buflen
-len
, "f", subkeyname
);
1483 werr
= regsubkey_ctr_addkey(ctr
, subkeyname
);
1484 if (!W_ERROR_IS_OK(werr
)) {
1485 DEBUG(5, ("regdb_fetch_keys: regsubkey_ctr_addkey "
1486 "failed: %s\n", win_errstr(werr
)));
1492 DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items
));
1499 int regdb_fetch_keys(const char *key
, struct regsubkey_ctr
*ctr
)
1503 werr
= regdb_fetch_keys_internal(regdb
, key
, ctr
);
1504 if (!W_ERROR_IS_OK(werr
)) {
1508 return regsubkey_ctr_numkeys(ctr
);
1511 /****************************************************************************
1512 Unpack a list of registry values frem the TDB
1513 ***************************************************************************/
1515 static int regdb_unpack_values(struct regval_ctr
*values
, uint8
*buf
, int buflen
)
1522 uint32 num_values
= 0;
1525 /* loop and unpack the rest of the registry values */
1527 len
+= tdb_unpack(buf
+len
, buflen
-len
, "d", &num_values
);
1529 for ( i
=0; i
<num_values
; i
++ ) {
1530 /* unpack the next regval */
1535 valuename
[0] = '\0';
1536 len
+= tdb_unpack(buf
+len
, buflen
-len
, "fdB",
1542 /* add the new value. Paranoid protective code -- make sure data_p is valid */
1544 if (*valuename
&& size
&& data_p
) {
1545 regval_ctr_addvalue(values
, valuename
, type
,
1546 (const char *)data_p
, size
);
1548 SAFE_FREE(data_p
); /* 'B' option to tdb_unpack does a malloc() */
1550 DEBUG(8,("specific: [%s], len: %d\n", valuename
, size
));
1556 /****************************************************************************
1557 Pack all values in all printer keys
1558 ***************************************************************************/
1560 static int regdb_pack_values(struct regval_ctr
*values
, uint8
*buf
, int buflen
)
1564 struct regval_blob
*val
;
1570 num_values
= regval_ctr_numvals( values
);
1572 /* pack the number of values first */
1574 len
+= tdb_pack( buf
+len
, buflen
-len
, "d", num_values
);
1576 /* loop over all values */
1578 for ( i
=0; i
<num_values
; i
++ ) {
1579 val
= regval_ctr_specific_value( values
, i
);
1580 len
+= tdb_pack(buf
+len
, buflen
-len
, "fdB",
1584 regval_data_p(val
) );
1590 /***********************************************************************
1591 Retrieve an array of strings containing subkeys. Memory should be
1592 released by the caller.
1593 ***********************************************************************/
1595 static int regdb_fetch_values_internal(struct db_context
*db
, const char* key
,
1596 struct regval_ctr
*values
)
1598 char *keystr
= NULL
;
1599 TALLOC_CTX
*ctx
= talloc_stackframe();
1603 DEBUG(10,("regdb_fetch_values: Looking for value of key [%s] \n", key
));
1605 if (!regdb_key_exists(db
, key
)) {
1609 keystr
= talloc_asprintf(ctx
, "%s/%s", REG_VALUE_PREFIX
, key
);
1614 values
->seqnum
= db
->get_seqnum(db
);
1616 value
= regdb_fetch_key_internal(db
, ctx
, keystr
);
1619 /* all keys have zero values by default */
1623 regdb_unpack_values(values
, value
.dptr
, value
.dsize
);
1624 ret
= regval_ctr_numvals(values
);
1631 int regdb_fetch_values(const char* key
, struct regval_ctr
*values
)
1633 return regdb_fetch_values_internal(regdb
, key
, values
);
1636 static bool regdb_store_values_internal(struct db_context
*db
, const char *key
,
1637 struct regval_ctr
*values
)
1639 TDB_DATA old_data
, data
;
1640 char *keystr
= NULL
;
1641 TALLOC_CTX
*ctx
= talloc_stackframe();
1644 bool result
= false;
1646 DEBUG(10,("regdb_store_values: Looking for value of key [%s] \n", key
));
1648 if (!regdb_key_exists(db
, key
)) {
1654 len
= regdb_pack_values(values
, data
.dptr
, data
.dsize
);
1656 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
1660 data
.dptr
= TALLOC_ARRAY(ctx
, uint8
, len
);
1663 len
= regdb_pack_values(values
, data
.dptr
, data
.dsize
);
1665 SMB_ASSERT( len
== data
.dsize
);
1667 keystr
= talloc_asprintf(ctx
, "%s/%s", REG_VALUE_PREFIX
, key
);
1671 keystr
= normalize_reg_path(ctx
, keystr
);
1676 old_data
= dbwrap_fetch_bystring(db
, ctx
, keystr
);
1678 if ((old_data
.dptr
!= NULL
)
1679 && (old_data
.dsize
== data
.dsize
)
1680 && (memcmp(old_data
.dptr
, data
.dptr
, data
.dsize
) == 0))
1686 status
= dbwrap_trans_store_bystring(db
, keystr
, data
, TDB_REPLACE
);
1688 result
= NT_STATUS_IS_OK(status
);
1695 bool regdb_store_values(const char *key
, struct regval_ctr
*values
)
1697 return regdb_store_values_internal(regdb
, key
, values
);
1700 static WERROR
regdb_get_secdesc(TALLOC_CTX
*mem_ctx
, const char *key
,
1701 struct security_descriptor
**psecdesc
)
1706 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
1707 WERROR err
= WERR_OK
;
1709 DEBUG(10, ("regdb_get_secdesc: Getting secdesc of key [%s]\n", key
));
1711 if (!regdb_key_exists(regdb
, key
)) {
1716 tdbkey
= talloc_asprintf(tmp_ctx
, "%s/%s", REG_SECDESC_PREFIX
, key
);
1717 if (tdbkey
== NULL
) {
1721 normalize_dbkey(tdbkey
);
1723 data
= dbwrap_fetch_bystring(regdb
, tmp_ctx
, tdbkey
);
1724 if (data
.dptr
== NULL
) {
1729 status
= unmarshall_sec_desc(mem_ctx
, (uint8
*)data
.dptr
, data
.dsize
,
1732 if (NT_STATUS_EQUAL(status
, NT_STATUS_NO_MEMORY
)) {
1734 } else if (!NT_STATUS_IS_OK(status
)) {
1735 err
= WERR_REG_CORRUPT
;
1739 TALLOC_FREE(tmp_ctx
);
1743 static WERROR
regdb_set_secdesc(const char *key
,
1744 struct security_descriptor
*secdesc
)
1746 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
1748 WERROR err
= WERR_NOMEM
;
1751 if (!regdb_key_exists(regdb
, key
)) {
1756 tdbkey
= talloc_asprintf(mem_ctx
, "%s/%s", REG_SECDESC_PREFIX
, key
);
1757 if (tdbkey
== NULL
) {
1760 normalize_dbkey(tdbkey
);
1762 if (secdesc
== NULL
) {
1763 /* assuming a delete */
1764 err
= ntstatus_to_werror(dbwrap_trans_delete_bystring(regdb
,
1769 err
= ntstatus_to_werror(marshall_sec_desc(mem_ctx
, secdesc
,
1772 W_ERROR_NOT_OK_GOTO_DONE(err
);
1774 err
= ntstatus_to_werror(dbwrap_trans_store_bystring(regdb
, tdbkey
,
1778 TALLOC_FREE(mem_ctx
);
1782 bool regdb_subkeys_need_update(struct regsubkey_ctr
*subkeys
)
1784 return (regdb_get_seqnum() != regsubkey_ctr_get_seqnum(subkeys
));
1787 bool regdb_values_need_update(struct regval_ctr
*values
)
1789 return (regdb_get_seqnum() != values
->seqnum
);
1793 * Table of function pointers for default access
1796 struct registry_ops regdb_ops
= {
1797 .fetch_subkeys
= regdb_fetch_keys
,
1798 .fetch_values
= regdb_fetch_values
,
1799 .store_subkeys
= regdb_store_keys
,
1800 .store_values
= regdb_store_values
,
1801 .create_subkey
= regdb_create_subkey
,
1802 .delete_subkey
= regdb_delete_subkey
,
1803 .get_secdesc
= regdb_get_secdesc
,
1804 .set_secdesc
= regdb_set_secdesc
,
1805 .subkeys_need_update
= regdb_subkeys_need_update
,
1806 .values_need_update
= regdb_values_need_update