2 * Unix SMB/CIFS implementation.
3 * Virtual Windows Registry Layer
4 * Copyright (C) Gerald Carter 2002-2005
5 * Copyright (C) Michael Adam 2007-2011
6 * Copyright (C) Gregor Beck 2011
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 /* Implementation of internal registry database functions. */
25 #include "system/filesys.h"
28 #include "reg_util_internal.h"
29 #include "reg_backend_db.h"
30 #include "reg_objects.h"
31 #include "nt_printing.h"
34 #include "../libcli/security/secdesc.h"
37 #define DBGC_CLASS DBGC_REGISTRY
39 static struct db_context
*regdb
= NULL
;
40 static int regdb_refcount
;
42 static bool regdb_key_exists(struct db_context
*db
, const char *key
);
43 static WERROR
regdb_fetch_keys_internal(struct db_context
*db
, const char *key
,
44 struct regsubkey_ctr
*ctr
);
45 static bool regdb_store_keys_internal(struct db_context
*db
, const char *key
,
46 struct regsubkey_ctr
*ctr
);
47 static int regdb_fetch_values_internal(struct db_context
*db
, const char* key
,
48 struct regval_ctr
*values
);
49 static bool regdb_store_values_internal(struct db_context
*db
, const char *key
,
50 struct regval_ctr
*values
);
51 static WERROR
regdb_store_subkey_list(struct db_context
*db
, const char *parent
,
54 static WERROR
regdb_create_basekey(struct db_context
*db
, const char *key
);
55 static WERROR
regdb_create_subkey_internal(struct db_context
*db
,
59 /* List the deepest path into the registry. All part components will be created.*/
61 /* If you want to have a part of the path controlled by the tdb and part by
62 a virtual registry db (e.g. printing), then you have to list the deepest path.
63 For example,"HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion/Print"
64 allows the reg_db backend to handle everything up to
65 "HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion" and then we'll hook
66 the reg_printing backend onto the last component of the path (see
67 KEY_PRINTING_2K in include/rpc_reg.h) --jerry */
69 static const char *builtin_registry_paths
[] = {
73 KEY_PRINTING
"\\Forms",
74 KEY_PRINTING
"\\Printers",
75 KEY_PRINTING
"\\Environments\\Windows NT x86\\Print Processors\\winprint",
82 KEY_SAMBA_GROUP_POLICY
,
83 KEY_GP_MACHINE_POLICY
,
84 KEY_GP_MACHINE_WIN_POLICY
,
87 KEY_GP_USER_WIN_POLICY
,
88 "HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon\\GPExtensions",
89 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Print\\Monitors",
91 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\DefaultUserConfiguration",
100 struct builtin_regkey_value
{
102 const char *valuename
;
110 static struct builtin_regkey_value builtin_registry_values
[] = {
111 { KEY_PRINTING_PORTS
,
112 SAMBA_PRINTER_PORT_NAME
, REG_SZ
, { "" } },
114 "DefaultSpoolDirectory", REG_SZ
, { "C:\\Windows\\System32\\Spool\\Printers" } },
116 "DisplayName", REG_SZ
, { "Event Log" } },
118 "ErrorControl", REG_DWORD
, { (char*)0x00000001 } },
119 { NULL
, NULL
, 0, { NULL
} }
122 static WERROR
create_key_recursive(struct db_context
*db
,
129 if (subkey
== NULL
) {
130 return WERR_INVALID_PARAM
;
134 return regdb_create_basekey(db
, subkey
);
137 p
= strrchr_m(path
, '\\');
140 werr
= create_key_recursive(db
, NULL
, path
);
143 werr
= create_key_recursive(db
, path
, p
+1);
147 if (!W_ERROR_IS_OK(werr
)) {
151 werr
= regdb_create_subkey_internal(db
, path
, subkey
);
158 * Initialize a key in the registry:
159 * create each component key of the specified path.
161 static WERROR
init_registry_key_internal(struct db_context
*db
,
162 const char *add_path
)
166 TALLOC_CTX
*frame
= talloc_stackframe();
168 if (add_path
== NULL
) {
169 werr
= WERR_INVALID_PARAM
;
173 key
= talloc_strdup(frame
, add_path
);
175 subkey
= strrchr_m(key
, '\\');
176 if (subkey
== NULL
) {
184 werr
= create_key_recursive(db
, key
, subkey
);
191 struct init_registry_key_context
{
192 const char *add_path
;
195 static NTSTATUS
init_registry_key_action(struct db_context
*db
,
198 struct init_registry_key_context
*init_ctx
=
199 (struct init_registry_key_context
*)private_data
;
201 return werror_to_ntstatus(init_registry_key_internal(
202 db
, init_ctx
->add_path
));
206 * Initialize a key in the registry:
207 * create each component key of the specified path,
208 * wrapped in one db transaction.
210 WERROR
init_registry_key(const char *add_path
)
212 struct init_registry_key_context init_ctx
;
214 if (regdb_key_exists(regdb
, add_path
)) {
218 init_ctx
.add_path
= add_path
;
220 return ntstatus_to_werror(dbwrap_trans_do(regdb
,
221 init_registry_key_action
,
225 /***********************************************************************
226 Open the registry data in the tdb
227 ***********************************************************************/
229 static void regdb_ctr_add_value(struct regval_ctr
*ctr
,
230 struct builtin_regkey_value
*value
)
232 switch(value
->type
) {
234 regval_ctr_addvalue(ctr
, value
->valuename
, REG_DWORD
,
235 (uint8_t *)&value
->data
.dw_value
,
240 regval_ctr_addvalue_sz(ctr
, value
->valuename
,
245 DEBUG(0, ("regdb_ctr_add_value: invalid value type in "
246 "registry values [%d]\n", value
->type
));
250 static NTSTATUS
init_registry_data_action(struct db_context
*db
,
254 TALLOC_CTX
*frame
= talloc_stackframe();
255 struct regval_ctr
*values
;
258 /* loop over all of the predefined paths and add each component */
260 for (i
=0; builtin_registry_paths
[i
] != NULL
; i
++) {
261 if (regdb_key_exists(db
, builtin_registry_paths
[i
])) {
264 status
= werror_to_ntstatus(init_registry_key_internal(db
,
265 builtin_registry_paths
[i
]));
266 if (!NT_STATUS_IS_OK(status
)) {
271 /* loop over all of the predefined values and add each component */
273 for (i
=0; builtin_registry_values
[i
].path
!= NULL
; i
++) {
276 werr
= regval_ctr_init(frame
, &values
);
277 if (!W_ERROR_IS_OK(werr
)) {
278 status
= werror_to_ntstatus(werr
);
282 regdb_fetch_values_internal(db
,
283 builtin_registry_values
[i
].path
,
286 /* preserve existing values across restarts. Only add new ones */
288 if (!regval_ctr_key_exists(values
,
289 builtin_registry_values
[i
].valuename
))
291 regdb_ctr_add_value(values
,
292 &builtin_registry_values
[i
]);
293 regdb_store_values_internal(db
,
294 builtin_registry_values
[i
].path
,
300 status
= NT_STATUS_OK
;
308 WERROR
init_registry_data(void)
311 TALLOC_CTX
*frame
= talloc_stackframe();
312 struct regval_ctr
*values
;
316 * First, check for the existence of the needed keys and values.
317 * If all do already exist, we can save the writes.
319 for (i
=0; builtin_registry_paths
[i
] != NULL
; i
++) {
320 if (!regdb_key_exists(regdb
, builtin_registry_paths
[i
])) {
325 for (i
=0; builtin_registry_values
[i
].path
!= NULL
; i
++) {
326 werr
= regval_ctr_init(frame
, &values
);
327 W_ERROR_NOT_OK_GOTO_DONE(werr
);
329 regdb_fetch_values_internal(regdb
,
330 builtin_registry_values
[i
].path
,
332 if (!regval_ctr_key_exists(values
,
333 builtin_registry_values
[i
].valuename
))
348 * There are potentially quite a few store operations which are all
349 * indiviually wrapped in tdb transactions. Wrapping them in a single
350 * transaction gives just a single transaction_commit() to actually do
351 * its fsync()s. See tdb/common/transaction.c for info about nested
352 * transaction behaviour.
355 werr
= ntstatus_to_werror(dbwrap_trans_do(regdb
,
356 init_registry_data_action
,
364 static int regdb_normalize_keynames_fn(struct db_record
*rec
,
367 TALLOC_CTX
*mem_ctx
= talloc_tos();
371 if (rec
->key
.dptr
== NULL
|| rec
->key
.dsize
== 0) {
375 keyname
= strchr((const char *) rec
->key
.dptr
, '/');
377 struct db_record new_rec
;
379 keyname
= talloc_string_sub(mem_ctx
,
380 (const char *) rec
->key
.dptr
,
384 DEBUG(2, ("regdb_normalize_keynames_fn: Convert %s to %s\n",
385 (const char *) rec
->key
.dptr
,
388 new_rec
.value
= rec
->value
;
389 new_rec
.key
= string_term_tdb_data(keyname
);
390 new_rec
.private_data
= rec
->private_data
;
392 /* Delete the original record and store the normalized key */
393 status
= rec
->delete_rec(rec
);
394 if (!NT_STATUS_IS_OK(status
)) {
395 DEBUG(0,("regdb_normalize_keynames_fn: "
396 "tdb_delete for [%s] failed!\n",
401 status
= rec
->store(&new_rec
, new_rec
.value
, TDB_REPLACE
);
402 if (!NT_STATUS_IS_OK(status
)) {
403 DEBUG(0,("regdb_normalize_keynames_fn: "
404 "failed to store new record for [%s]!\n",
413 static WERROR
regdb_store_regdb_version(struct db_context
*db
, uint32_t version
)
416 const char *version_keyname
= "INFO/version";
419 return WERR_CAN_NOT_COMPLETE
;
422 status
= dbwrap_trans_store_int32(db
, version_keyname
, version
);
423 if (!NT_STATUS_IS_OK(status
)) {
424 DEBUG(1, ("regdb_store_regdb_version: error storing %s = %d: %s\n",
425 version_keyname
, version
, nt_errstr(status
)));
426 return ntstatus_to_werror(status
);
428 DEBUG(10, ("regdb_store_regdb_version: stored %s = %d\n",
429 version_keyname
, version
));
434 static WERROR
regdb_upgrade_v1_to_v2(struct db_context
*db
)
440 mem_ctx
= talloc_stackframe();
442 rc
= regdb
->traverse(db
, regdb_normalize_keynames_fn
, mem_ctx
);
444 talloc_free(mem_ctx
);
447 return WERR_REG_IO_FAILURE
;
450 werr
= regdb_store_regdb_version(db
, REGVER_V2
);
454 static int regdb_upgrade_v2_to_v3_fn(struct db_record
*rec
, void *private_data
)
461 uint32_t buflen
, len
;
465 if (rec
->key
.dptr
== NULL
|| rec
->key
.dsize
== 0) {
469 keyname
= (const char *)rec
->key
.dptr
;
471 if (strncmp(keyname
, REG_SORTED_SUBKEYS_PREFIX
,
472 strlen(REG_SORTED_SUBKEYS_PREFIX
)) == 0)
474 /* Delete the deprecated sorted subkeys cache. */
476 DEBUG(10, ("regdb_upgrade_v2_to_v3: deleting [%s]\n", keyname
));
478 status
= rec
->delete_rec(rec
);
479 if (!NT_STATUS_IS_OK(status
)) {
480 DEBUG(0, ("regdb_upgrade_v2_to_v3: tdb_delete for [%s] "
481 "failed!\n", keyname
));
488 if (strncmp(keyname
, REG_VALUE_PREFIX
, strlen(REG_VALUE_PREFIX
)) == 0) {
489 DEBUG(10, ("regdb_upgrade_v2_to_v3: skipping [%s]\n", keyname
));
493 if (strncmp(keyname
, REG_SECDESC_PREFIX
,
494 strlen(REG_SECDESC_PREFIX
)) == 0)
496 DEBUG(10, ("regdb_upgrade_v2_to_v3: skipping [%s]\n", keyname
));
501 * Found a regular subkey list record.
502 * Walk the list and create the list record for those
503 * subkeys that don't already have one.
505 DEBUG(10, ("regdb_upgrade_v2_to_v3: scanning subkey list of [%s]\n",
508 buf
= rec
->value
.dptr
;
509 buflen
= rec
->value
.dsize
;
511 len
= tdb_unpack(buf
, buflen
, "d", &num_items
);
512 if (len
== (uint32_t)-1) {
513 /* invalid or empty - skip */
517 for (i
=0; i
<num_items
; i
++) {
518 len
+= tdb_unpack(buf
+len
, buflen
-len
, "f", subkeyname
);
519 DEBUG(10, ("regdb_upgrade_v2_to_v3: "
520 "writing subkey list for [%s\\%s]\n",
521 keyname
, subkeyname
));
522 werr
= regdb_store_subkey_list(regdb
, keyname
, subkeyname
);
523 if (!W_ERROR_IS_OK(werr
)) {
531 static WERROR
regdb_upgrade_v2_to_v3(struct db_context
*db
)
535 TALLOC_CTX
*frame
= talloc_stackframe();
537 rc
= regdb
->traverse(db
, regdb_upgrade_v2_to_v3_fn
, frame
);
539 werr
= WERR_REG_IO_FAILURE
;
543 werr
= regdb_store_regdb_version(db
, REGVER_V3
);
550 /***********************************************************************
551 Open the registry database
552 ***********************************************************************/
554 WERROR
regdb_init(void)
556 const char *vstring
= "INFO/version";
557 uint32 vers_id
, expected_version
;
561 DEBUG(10, ("regdb_init: incrementing refcount (%d->%d)\n",
562 regdb_refcount
, regdb_refcount
+1));
567 regdb
= db_open(NULL
, state_path("registry.tdb"), 0,
568 REG_TDB_FLAGS
, O_RDWR
, 0600);
570 regdb
= db_open(NULL
, state_path("registry.tdb"), 0,
571 REG_TDB_FLAGS
, O_RDWR
|O_CREAT
, 0600);
573 werr
= ntstatus_to_werror(map_nt_error_from_unix(errno
));
574 DEBUG(1,("regdb_init: Failed to open registry %s (%s)\n",
575 state_path("registry.tdb"), strerror(errno
) ));
579 DEBUG(10,("regdb_init: Successfully created registry tdb\n"));
583 DEBUG(10, ("regdb_init: registry db openend. refcount reset (%d)\n",
586 expected_version
= REGVER_V3
;
588 vers_id
= dbwrap_fetch_int32(regdb
, vstring
);
590 DEBUG(10, ("regdb_init: registry version uninitialized "
591 "(got %d), initializing to version %d\n",
592 vers_id
, expected_version
));
594 werr
= regdb_store_regdb_version(regdb
, expected_version
);
598 if (vers_id
> expected_version
|| vers_id
== 0) {
599 DEBUG(1, ("regdb_init: unknown registry version %d "
600 "(code version = %d), refusing initialization\n",
601 vers_id
, expected_version
));
602 return WERR_CAN_NOT_COMPLETE
;
605 if (regdb
->transaction_start(regdb
) != 0) {
606 return WERR_REG_IO_FAILURE
;
609 if (vers_id
== REGVER_V1
) {
610 DEBUG(10, ("regdb_init: upgrading registry fromversion %d "
611 "to %d\n", REGVER_V1
, REGVER_V2
));
613 werr
= regdb_upgrade_v1_to_v2(regdb
);
614 if (!W_ERROR_IS_OK(werr
)) {
615 regdb
->transaction_cancel(regdb
);
622 if (vers_id
== REGVER_V2
) {
623 DEBUG(10, ("regdb_init: upgrading registry from version %d "
624 "to %d\n", REGVER_V2
, REGVER_V3
));
626 werr
= regdb_upgrade_v2_to_v3(regdb
);
627 if (!W_ERROR_IS_OK(werr
)) {
628 regdb
->transaction_cancel(regdb
);
635 /* future upgrade code should go here */
637 if (regdb
->transaction_commit(regdb
) != 0) {
638 return WERR_REG_IO_FAILURE
;
644 /***********************************************************************
645 Open the registry. Must already have been initialized by regdb_init()
646 ***********************************************************************/
648 WERROR
regdb_open( void )
650 WERROR result
= WERR_OK
;
653 DEBUG(10, ("regdb_open: incrementing refcount (%d->%d)\n",
654 regdb_refcount
, regdb_refcount
+1));
661 regdb
= db_open(NULL
, state_path("registry.tdb"), 0,
662 REG_TDB_FLAGS
, O_RDWR
, 0600);
664 result
= ntstatus_to_werror( map_nt_error_from_unix( errno
) );
665 DEBUG(0,("regdb_open: Failed to open %s! (%s)\n",
666 state_path("registry.tdb"), strerror(errno
) ));
672 DEBUG(10, ("regdb_open: registry db opened. refcount reset (%d)\n",
678 /***********************************************************************
679 ***********************************************************************/
681 int regdb_close( void )
683 if (regdb_refcount
== 0) {
689 DEBUG(10, ("regdb_close: decrementing refcount (%d->%d)\n",
690 regdb_refcount
+1, regdb_refcount
));
692 if ( regdb_refcount
> 0 )
695 SMB_ASSERT( regdb_refcount
>= 0 );
701 WERROR
regdb_transaction_start(void)
703 return (regdb
->transaction_start(regdb
) == 0) ?
704 WERR_OK
: WERR_REG_IO_FAILURE
;
707 WERROR
regdb_transaction_commit(void)
709 return (regdb
->transaction_commit(regdb
) == 0) ?
710 WERR_OK
: WERR_REG_IO_FAILURE
;
713 WERROR
regdb_transaction_cancel(void)
715 return (regdb
->transaction_cancel(regdb
) == 0) ?
716 WERR_OK
: WERR_REG_IO_FAILURE
;
719 /***********************************************************************
720 return the tdb sequence number of the registry tdb.
721 this is an indicator for the content of the registry
722 having changed. it will change upon regdb_init, too, though.
723 ***********************************************************************/
724 int regdb_get_seqnum(void)
726 return regdb
->get_seqnum(regdb
);
730 static WERROR
regdb_delete_key_with_prefix(struct db_context
*db
,
735 WERROR werr
= WERR_NOMEM
;
736 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
738 if (keyname
== NULL
) {
739 werr
= WERR_INVALID_PARAM
;
743 if (prefix
== NULL
) {
744 path
= discard_const_p(char, keyname
);
746 path
= talloc_asprintf(mem_ctx
, "%s\\%s", prefix
, keyname
);
752 path
= normalize_reg_path(mem_ctx
, path
);
757 werr
= ntstatus_to_werror(dbwrap_delete_bystring(db
, path
));
759 /* treat "not found" as ok */
760 if (W_ERROR_EQUAL(werr
, WERR_NOT_FOUND
)) {
765 talloc_free(mem_ctx
);
770 static WERROR
regdb_delete_values(struct db_context
*db
, const char *keyname
)
772 return regdb_delete_key_with_prefix(db
, keyname
, REG_VALUE_PREFIX
);
775 static WERROR
regdb_delete_secdesc(struct db_context
*db
, const char *keyname
)
777 return regdb_delete_key_with_prefix(db
, keyname
, REG_SECDESC_PREFIX
);
780 static WERROR
regdb_delete_subkeylist(struct db_context
*db
, const char *keyname
)
782 return regdb_delete_key_with_prefix(db
, keyname
, NULL
);
786 static WERROR
regdb_delete_key_lists(struct db_context
*db
, const char *keyname
)
790 werr
= regdb_delete_values(db
, keyname
);
791 if (!W_ERROR_IS_OK(werr
)) {
792 DEBUG(1, (__location__
" Deleting %s\\%s failed: %s\n",
793 REG_VALUE_PREFIX
, keyname
, win_errstr(werr
)));
797 werr
= regdb_delete_secdesc(db
, keyname
);
798 if (!W_ERROR_IS_OK(werr
)) {
799 DEBUG(1, (__location__
" Deleting %s\\%s failed: %s\n",
800 REG_SECDESC_PREFIX
, keyname
, win_errstr(werr
)));
804 werr
= regdb_delete_subkeylist(db
, keyname
);
805 if (!W_ERROR_IS_OK(werr
)) {
806 DEBUG(1, (__location__
" Deleting %s failed: %s\n",
807 keyname
, win_errstr(werr
)));
815 /***********************************************************************
816 Add subkey strings to the registry tdb under a defined key
817 fmt is the same format as tdb_pack except this function only supports
819 ***********************************************************************/
821 static WERROR
regdb_store_keys_internal2(struct db_context
*db
,
823 struct regsubkey_ctr
*ctr
)
826 uint8
*buffer
= NULL
;
829 uint32 num_subkeys
= regsubkey_ctr_numkeys(ctr
);
830 char *keyname
= NULL
;
831 TALLOC_CTX
*ctx
= talloc_stackframe();
835 werr
= WERR_INVALID_PARAM
;
839 keyname
= talloc_strdup(ctx
, key
);
845 keyname
= normalize_reg_path(ctx
, keyname
);
851 /* allocate some initial memory */
853 buffer
= (uint8
*)SMB_MALLOC(1024);
854 if (buffer
== NULL
) {
861 /* store the number of subkeys */
863 len
+= tdb_pack(buffer
+len
, buflen
-len
, "d", num_subkeys
);
865 /* pack all the strings */
867 for (i
=0; i
<num_subkeys
; i
++) {
870 thistime
= tdb_pack(buffer
+len
, buflen
-len
, "f",
871 regsubkey_ctr_specific_key(ctr
, i
));
872 if (len
+thistime
> buflen
) {
875 * tdb_pack hasn't done anything because of the short
876 * buffer, allocate extra space.
878 buffer
= SMB_REALLOC_ARRAY(buffer
, uint8_t,
881 DEBUG(0, ("regdb_store_keys: Failed to realloc "
882 "memory of size [%u]\n",
883 (unsigned int)(len
+thistime
)*2));
887 buflen
= (len
+thistime
)*2;
888 thistime2
= tdb_pack(
889 buffer
+len
, buflen
-len
, "f",
890 regsubkey_ctr_specific_key(ctr
, i
));
891 if (thistime2
!= thistime
) {
892 DEBUG(0, ("tdb_pack failed\n"));
893 werr
= WERR_CAN_NOT_COMPLETE
;
900 /* finally write out the data */
904 werr
= ntstatus_to_werror(dbwrap_store_bystring(db
, keyname
, dbuf
,
914 * Utility function to store a new empty list of
915 * subkeys of given key specified as parent and subkey name
916 * (thereby creating the key).
917 * If the parent keyname is NULL, then the "subkey" is
918 * interpreted as a base key.
919 * If the subkey list does already exist, it is not modified.
921 * Must be called from within a transaction.
923 static WERROR
regdb_store_subkey_list(struct db_context
*db
, const char *parent
,
928 struct regsubkey_ctr
*subkeys
= NULL
;
929 TALLOC_CTX
*frame
= talloc_stackframe();
931 if (parent
== NULL
) {
932 path
= talloc_strdup(frame
, key
);
934 path
= talloc_asprintf(frame
, "%s\\%s", parent
, key
);
941 werr
= regsubkey_ctr_init(frame
, &subkeys
);
942 W_ERROR_NOT_OK_GOTO_DONE(werr
);
944 werr
= regdb_fetch_keys_internal(db
, path
, subkeys
);
945 if (W_ERROR_IS_OK(werr
)) {
946 /* subkey list exists already - don't modify */
950 werr
= regsubkey_ctr_reinit(subkeys
);
951 W_ERROR_NOT_OK_GOTO_DONE(werr
);
953 /* create a record with 0 subkeys */
954 werr
= regdb_store_keys_internal2(db
, path
, subkeys
);
955 if (!W_ERROR_IS_OK(werr
)) {
956 DEBUG(0, ("regdb_store_keys: Failed to store new record for "
957 "key [%s]: %s\n", path
, win_errstr(werr
)));
966 /***********************************************************************
967 Store the new subkey record and create any child key records that
968 do not currently exist
969 ***********************************************************************/
971 struct regdb_store_keys_context
{
973 struct regsubkey_ctr
*ctr
;
976 static NTSTATUS
regdb_store_keys_action(struct db_context
*db
,
979 struct regdb_store_keys_context
*store_ctx
;
983 struct regsubkey_ctr
*old_subkeys
= NULL
;
984 char *oldkeyname
= NULL
;
985 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
987 store_ctx
= (struct regdb_store_keys_context
*)private_data
;
990 * Re-fetch the old keys inside the transaction
993 werr
= regsubkey_ctr_init(mem_ctx
, &old_subkeys
);
994 W_ERROR_NOT_OK_GOTO_DONE(werr
);
996 werr
= regdb_fetch_keys_internal(db
, store_ctx
->key
, old_subkeys
);
997 if (!W_ERROR_IS_OK(werr
) &&
998 !W_ERROR_EQUAL(werr
, WERR_NOT_FOUND
))
1004 * Make the store operation as safe as possible without transactions:
1006 * (1) For each subkey removed from ctr compared with old_subkeys:
1008 * (a) First delete the value db entry.
1010 * (b) Next delete the secdesc db record.
1012 * (c) Then delete the subkey list entry.
1014 * (2) Now write the list of subkeys of the parent key,
1015 * deleting removed entries and adding new ones.
1017 * (3) Finally create the subkey list entries for the added keys.
1019 * This way if we crash half-way in between deleting the subkeys
1020 * and storing the parent's list of subkeys, no old data can pop up
1021 * out of the blue when re-adding keys later on.
1024 /* (1) delete removed keys' lists (values/secdesc/subkeys) */
1026 num_subkeys
= regsubkey_ctr_numkeys(old_subkeys
);
1027 for (i
=0; i
<num_subkeys
; i
++) {
1028 oldkeyname
= regsubkey_ctr_specific_key(old_subkeys
, i
);
1030 if (regsubkey_ctr_key_exists(store_ctx
->ctr
, oldkeyname
)) {
1032 * It's still around, don't delete
1037 path
= talloc_asprintf(mem_ctx
, "%s\\%s", store_ctx
->key
,
1044 werr
= regdb_delete_key_lists(db
, path
);
1045 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1050 TALLOC_FREE(old_subkeys
);
1052 /* (2) store the subkey list for the parent */
1054 werr
= regdb_store_keys_internal2(db
, store_ctx
->key
, store_ctx
->ctr
);
1055 if (!W_ERROR_IS_OK(werr
)) {
1056 DEBUG(0,("regdb_store_keys: Failed to store new subkey list "
1057 "for parent [%s]: %s\n", store_ctx
->key
,
1062 /* (3) now create records for any subkeys that don't already exist */
1064 num_subkeys
= regsubkey_ctr_numkeys(store_ctx
->ctr
);
1066 for (i
=0; i
<num_subkeys
; i
++) {
1069 subkey
= regsubkey_ctr_specific_key(store_ctx
->ctr
, i
);
1071 werr
= regdb_store_subkey_list(db
, store_ctx
->key
, subkey
);
1072 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1078 talloc_free(mem_ctx
);
1079 return werror_to_ntstatus(werr
);
1082 static bool regdb_store_keys_internal(struct db_context
*db
, const char *key
,
1083 struct regsubkey_ctr
*ctr
)
1085 int num_subkeys
, old_num_subkeys
, i
;
1086 struct regsubkey_ctr
*old_subkeys
= NULL
;
1087 TALLOC_CTX
*ctx
= talloc_stackframe();
1090 struct regdb_store_keys_context store_ctx
;
1092 if (!regdb_key_exists(db
, key
)) {
1097 * fetch a list of the old subkeys so we can determine if anything has
1101 werr
= regsubkey_ctr_init(ctx
, &old_subkeys
);
1102 if (!W_ERROR_IS_OK(werr
)) {
1103 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
1107 werr
= regdb_fetch_keys_internal(db
, key
, old_subkeys
);
1108 if (!W_ERROR_IS_OK(werr
) &&
1109 !W_ERROR_EQUAL(werr
, WERR_NOT_FOUND
))
1114 num_subkeys
= regsubkey_ctr_numkeys(ctr
);
1115 old_num_subkeys
= regsubkey_ctr_numkeys(old_subkeys
);
1116 if ((num_subkeys
&& old_num_subkeys
) &&
1117 (num_subkeys
== old_num_subkeys
)) {
1119 for (i
= 0; i
< num_subkeys
; i
++) {
1120 if (strcmp(regsubkey_ctr_specific_key(ctr
, i
),
1121 regsubkey_ctr_specific_key(old_subkeys
, i
))
1127 if (i
== num_subkeys
) {
1129 * Nothing changed, no point to even start a tdb
1138 TALLOC_FREE(old_subkeys
);
1140 store_ctx
.key
= key
;
1141 store_ctx
.ctr
= ctr
;
1143 werr
= ntstatus_to_werror(dbwrap_trans_do(db
,
1144 regdb_store_keys_action
,
1147 ret
= W_ERROR_IS_OK(werr
);
1155 bool regdb_store_keys(const char *key
, struct regsubkey_ctr
*ctr
)
1157 return regdb_store_keys_internal(regdb
, key
, ctr
);
1161 * create a subkey of a given key
1164 struct regdb_create_subkey_context
{
1169 static NTSTATUS
regdb_create_subkey_action(struct db_context
*db
,
1173 struct regdb_create_subkey_context
*create_ctx
;
1174 struct regsubkey_ctr
*subkeys
;
1175 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
1177 create_ctx
= (struct regdb_create_subkey_context
*)private_data
;
1179 werr
= regsubkey_ctr_init(mem_ctx
, &subkeys
);
1180 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1182 werr
= regdb_fetch_keys_internal(db
, create_ctx
->key
, subkeys
);
1183 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1185 werr
= regsubkey_ctr_addkey(subkeys
, create_ctx
->subkey
);
1186 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1188 werr
= regdb_store_keys_internal2(db
, create_ctx
->key
, subkeys
);
1189 if (!W_ERROR_IS_OK(werr
)) {
1190 DEBUG(0, (__location__
" failed to store new subkey list for "
1191 "parent key %s: %s\n", create_ctx
->key
,
1195 werr
= regdb_store_subkey_list(db
, create_ctx
->key
, create_ctx
->subkey
);
1198 talloc_free(mem_ctx
);
1199 return werror_to_ntstatus(werr
);
1202 static WERROR
regdb_create_subkey_internal(struct db_context
*db
,
1207 struct regsubkey_ctr
*subkeys
;
1208 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
1209 struct regdb_create_subkey_context create_ctx
;
1211 if (!regdb_key_exists(db
, key
)) {
1212 werr
= WERR_NOT_FOUND
;
1216 werr
= regsubkey_ctr_init(mem_ctx
, &subkeys
);
1217 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1219 werr
= regdb_fetch_keys_internal(db
, key
, subkeys
);
1220 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1222 if (regsubkey_ctr_key_exists(subkeys
, subkey
)) {
1227 talloc_free(subkeys
);
1229 create_ctx
.key
= key
;
1230 create_ctx
.subkey
= subkey
;
1232 werr
= ntstatus_to_werror(dbwrap_trans_do(db
,
1233 regdb_create_subkey_action
,
1237 talloc_free(mem_ctx
);
1241 static WERROR
regdb_create_subkey(const char *key
, const char *subkey
)
1243 return regdb_create_subkey_internal(regdb
, key
, subkey
);
1250 struct regdb_create_basekey_context
{
1254 static NTSTATUS
regdb_create_basekey_action(struct db_context
*db
,
1258 struct regdb_create_basekey_context
*create_ctx
;
1260 create_ctx
= (struct regdb_create_basekey_context
*)private_data
;
1262 werr
= regdb_store_subkey_list(db
, NULL
, create_ctx
->key
);
1264 return werror_to_ntstatus(werr
);
1267 static WERROR
regdb_create_basekey(struct db_context
*db
, const char *key
)
1270 struct regdb_create_subkey_context create_ctx
;
1272 create_ctx
.key
= key
;
1274 werr
= ntstatus_to_werror(dbwrap_trans_do(db
,
1275 regdb_create_basekey_action
,
1282 * create a subkey of a given key
1285 struct regdb_delete_subkey_context
{
1291 static NTSTATUS
regdb_delete_subkey_action(struct db_context
*db
,
1295 struct regdb_delete_subkey_context
*delete_ctx
;
1296 struct regsubkey_ctr
*subkeys
;
1297 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
1299 delete_ctx
= (struct regdb_delete_subkey_context
*)private_data
;
1301 werr
= regdb_delete_key_lists(db
, delete_ctx
->path
);
1302 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1304 werr
= regsubkey_ctr_init(mem_ctx
, &subkeys
);
1305 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1307 werr
= regdb_fetch_keys_internal(db
, delete_ctx
->key
, subkeys
);
1308 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1310 werr
= regsubkey_ctr_delkey(subkeys
, delete_ctx
->subkey
);
1311 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1313 werr
= regdb_store_keys_internal2(db
, delete_ctx
->key
, subkeys
);
1314 if (!W_ERROR_IS_OK(werr
)) {
1315 DEBUG(0, (__location__
" failed to store new subkey_list for "
1316 "parent key %s: %s\n", delete_ctx
->key
,
1321 talloc_free(mem_ctx
);
1322 return werror_to_ntstatus(werr
);
1325 static WERROR
regdb_delete_subkey(const char *key
, const char *subkey
)
1329 struct regdb_delete_subkey_context delete_ctx
;
1330 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
1332 if (!regdb_key_exists(regdb
, key
)) {
1333 werr
= WERR_NOT_FOUND
;
1337 path
= talloc_asprintf(mem_ctx
, "%s\\%s", key
, subkey
);
1343 if (!regdb_key_exists(regdb
, path
)) {
1348 delete_ctx
.key
= key
;
1349 delete_ctx
.subkey
= subkey
;
1350 delete_ctx
.path
= path
;
1352 werr
= ntstatus_to_werror(dbwrap_trans_do(regdb
,
1353 regdb_delete_subkey_action
,
1357 talloc_free(mem_ctx
);
1361 static TDB_DATA
regdb_fetch_key_internal(struct db_context
*db
,
1362 TALLOC_CTX
*mem_ctx
, const char *key
)
1367 path
= normalize_reg_path(mem_ctx
, key
);
1369 return make_tdb_data(NULL
, 0);
1372 data
= dbwrap_fetch_bystring(db
, mem_ctx
, path
);
1380 * Check for the existence of a key.
1382 * Existence of a key is authoritatively defined by
1383 * the existence of the record that contains the list
1386 static bool regdb_key_exists(struct db_context
*db
, const char *key
)
1388 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
1397 path
= normalize_reg_path(mem_ctx
, key
);
1399 DEBUG(0, ("out of memory! (talloc failed)\n"));
1403 if (*path
== '\0') {
1407 value
= regdb_fetch_key_internal(db
, mem_ctx
, path
);
1408 ret
= (value
.dptr
!= NULL
);
1411 TALLOC_FREE(mem_ctx
);
1416 /***********************************************************************
1417 Retrieve an array of strings containing subkeys. Memory should be
1418 released by the caller.
1419 ***********************************************************************/
1421 static WERROR
regdb_fetch_keys_internal(struct db_context
*db
, const char *key
,
1422 struct regsubkey_ctr
*ctr
)
1430 TALLOC_CTX
*frame
= talloc_stackframe();
1433 DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key
? key
: "NULL"));
1435 if (!regdb_key_exists(db
, key
)) {
1436 DEBUG(10, ("key [%s] not found\n", key
));
1437 werr
= WERR_NOT_FOUND
;
1441 werr
= regsubkey_ctr_reinit(ctr
);
1442 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1444 werr
= regsubkey_ctr_set_seqnum(ctr
, db
->get_seqnum(db
));
1445 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1447 value
= regdb_fetch_key_internal(db
, frame
, key
);
1449 if (value
.dsize
== 0 || value
.dptr
== NULL
) {
1450 DEBUG(10, ("regdb_fetch_keys: no subkeys found for key [%s]\n",
1456 buflen
= value
.dsize
;
1457 len
= tdb_unpack( buf
, buflen
, "d", &num_items
);
1458 if (len
== (uint32_t)-1) {
1459 werr
= WERR_NOT_FOUND
;
1463 for (i
=0; i
<num_items
; i
++) {
1464 len
+= tdb_unpack(buf
+len
, buflen
-len
, "f", subkeyname
);
1465 werr
= regsubkey_ctr_addkey(ctr
, subkeyname
);
1466 if (!W_ERROR_IS_OK(werr
)) {
1467 DEBUG(5, ("regdb_fetch_keys: regsubkey_ctr_addkey "
1468 "failed: %s\n", win_errstr(werr
)));
1474 DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items
));
1481 int regdb_fetch_keys(const char *key
, struct regsubkey_ctr
*ctr
)
1485 werr
= regdb_fetch_keys_internal(regdb
, key
, ctr
);
1486 if (!W_ERROR_IS_OK(werr
)) {
1490 return regsubkey_ctr_numkeys(ctr
);
1493 /****************************************************************************
1494 Unpack a list of registry values frem the TDB
1495 ***************************************************************************/
1497 static int regdb_unpack_values(struct regval_ctr
*values
, uint8
*buf
, int buflen
)
1504 uint32 num_values
= 0;
1507 /* loop and unpack the rest of the registry values */
1509 len
+= tdb_unpack(buf
+len
, buflen
-len
, "d", &num_values
);
1511 for ( i
=0; i
<num_values
; i
++ ) {
1512 /* unpack the next regval */
1517 valuename
[0] = '\0';
1518 len
+= tdb_unpack(buf
+len
, buflen
-len
, "fdB",
1524 regval_ctr_addvalue(values
, valuename
, type
,
1525 (uint8_t *)data_p
, size
);
1526 SAFE_FREE(data_p
); /* 'B' option to tdb_unpack does a malloc() */
1528 DEBUG(8,("specific: [%s], len: %d\n", valuename
, size
));
1534 /****************************************************************************
1535 Pack all values in all printer keys
1536 ***************************************************************************/
1538 static int regdb_pack_values(struct regval_ctr
*values
, uint8
*buf
, int buflen
)
1542 struct regval_blob
*val
;
1548 num_values
= regval_ctr_numvals( values
);
1550 /* pack the number of values first */
1552 len
+= tdb_pack( buf
+len
, buflen
-len
, "d", num_values
);
1554 /* loop over all values */
1556 for ( i
=0; i
<num_values
; i
++ ) {
1557 val
= regval_ctr_specific_value( values
, i
);
1558 len
+= tdb_pack(buf
+len
, buflen
-len
, "fdB",
1562 regval_data_p(val
) );
1568 /***********************************************************************
1569 Retrieve an array of strings containing subkeys. Memory should be
1570 released by the caller.
1571 ***********************************************************************/
1573 static int regdb_fetch_values_internal(struct db_context
*db
, const char* key
,
1574 struct regval_ctr
*values
)
1576 char *keystr
= NULL
;
1577 TALLOC_CTX
*ctx
= talloc_stackframe();
1582 DEBUG(10,("regdb_fetch_values: Looking for value of key [%s] \n", key
));
1584 if (!regdb_key_exists(db
, key
)) {
1588 keystr
= talloc_asprintf(ctx
, "%s\\%s", REG_VALUE_PREFIX
, key
);
1593 werr
= regval_ctr_set_seqnum(values
, db
->get_seqnum(db
));
1594 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1596 value
= regdb_fetch_key_internal(db
, ctx
, keystr
);
1599 /* all keys have zero values by default */
1603 regdb_unpack_values(values
, value
.dptr
, value
.dsize
);
1604 ret
= regval_ctr_numvals(values
);
1611 int regdb_fetch_values(const char* key
, struct regval_ctr
*values
)
1613 return regdb_fetch_values_internal(regdb
, key
, values
);
1616 static bool regdb_store_values_internal(struct db_context
*db
, const char *key
,
1617 struct regval_ctr
*values
)
1619 TDB_DATA old_data
, data
;
1620 char *keystr
= NULL
;
1621 TALLOC_CTX
*ctx
= talloc_stackframe();
1624 bool result
= false;
1626 DEBUG(10,("regdb_store_values: Looking for value of key [%s] \n", key
));
1628 if (!regdb_key_exists(db
, key
)) {
1634 len
= regdb_pack_values(values
, data
.dptr
, data
.dsize
);
1636 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
1640 data
.dptr
= talloc_array(ctx
, uint8
, len
);
1643 len
= regdb_pack_values(values
, data
.dptr
, data
.dsize
);
1645 SMB_ASSERT( len
== data
.dsize
);
1647 keystr
= talloc_asprintf(ctx
, "%s\\%s", REG_VALUE_PREFIX
, key
);
1651 keystr
= normalize_reg_path(ctx
, keystr
);
1656 old_data
= dbwrap_fetch_bystring(db
, ctx
, keystr
);
1658 if ((old_data
.dptr
!= NULL
)
1659 && (old_data
.dsize
== data
.dsize
)
1660 && (memcmp(old_data
.dptr
, data
.dptr
, data
.dsize
) == 0))
1666 status
= dbwrap_trans_store_bystring(db
, keystr
, data
, TDB_REPLACE
);
1668 result
= NT_STATUS_IS_OK(status
);
1675 bool regdb_store_values(const char *key
, struct regval_ctr
*values
)
1677 return regdb_store_values_internal(regdb
, key
, values
);
1680 static WERROR
regdb_get_secdesc(TALLOC_CTX
*mem_ctx
, const char *key
,
1681 struct security_descriptor
**psecdesc
)
1686 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
1687 WERROR err
= WERR_OK
;
1689 DEBUG(10, ("regdb_get_secdesc: Getting secdesc of key [%s]\n", key
));
1691 if (!regdb_key_exists(regdb
, key
)) {
1696 tdbkey
= talloc_asprintf(tmp_ctx
, "%s\\%s", REG_SECDESC_PREFIX
, key
);
1697 if (tdbkey
== NULL
) {
1702 tdbkey
= normalize_reg_path(tmp_ctx
, tdbkey
);
1703 if (tdbkey
== NULL
) {
1708 data
= dbwrap_fetch_bystring(regdb
, tmp_ctx
, tdbkey
);
1709 if (data
.dptr
== NULL
) {
1714 status
= unmarshall_sec_desc(mem_ctx
, (uint8
*)data
.dptr
, data
.dsize
,
1717 if (NT_STATUS_EQUAL(status
, NT_STATUS_NO_MEMORY
)) {
1719 } else if (!NT_STATUS_IS_OK(status
)) {
1720 err
= WERR_REG_CORRUPT
;
1724 TALLOC_FREE(tmp_ctx
);
1728 static WERROR
regdb_set_secdesc(const char *key
,
1729 struct security_descriptor
*secdesc
)
1731 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
1733 WERROR err
= WERR_NOMEM
;
1736 if (!regdb_key_exists(regdb
, key
)) {
1741 tdbkey
= talloc_asprintf(mem_ctx
, "%s\\%s", REG_SECDESC_PREFIX
, key
);
1742 if (tdbkey
== NULL
) {
1746 tdbkey
= normalize_reg_path(mem_ctx
, tdbkey
);
1747 if (tdbkey
== NULL
) {
1752 if (secdesc
== NULL
) {
1753 /* assuming a delete */
1754 err
= ntstatus_to_werror(dbwrap_trans_delete_bystring(regdb
,
1759 err
= ntstatus_to_werror(marshall_sec_desc(mem_ctx
, secdesc
,
1762 W_ERROR_NOT_OK_GOTO_DONE(err
);
1764 err
= ntstatus_to_werror(dbwrap_trans_store_bystring(regdb
, tdbkey
,
1768 TALLOC_FREE(mem_ctx
);
1772 bool regdb_subkeys_need_update(struct regsubkey_ctr
*subkeys
)
1774 return (regdb_get_seqnum() != regsubkey_ctr_get_seqnum(subkeys
));
1777 bool regdb_values_need_update(struct regval_ctr
*values
)
1779 return (regdb_get_seqnum() != regval_ctr_get_seqnum(values
));
1783 * Table of function pointers for default access
1786 struct registry_ops regdb_ops
= {
1787 .fetch_subkeys
= regdb_fetch_keys
,
1788 .fetch_values
= regdb_fetch_values
,
1789 .store_subkeys
= regdb_store_keys
,
1790 .store_values
= regdb_store_values
,
1791 .create_subkey
= regdb_create_subkey
,
1792 .delete_subkey
= regdb_delete_subkey
,
1793 .get_secdesc
= regdb_get_secdesc
,
1794 .set_secdesc
= regdb_set_secdesc
,
1795 .subkeys_need_update
= regdb_subkeys_need_update
,
1796 .values_need_update
= regdb_values_need_update