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_parse_internal.h"
30 #include "reg_backend_db.h"
31 #include "reg_objects.h"
32 #include "nt_printing.h"
34 #include "dbwrap/dbwrap.h"
35 #include "dbwrap/dbwrap_open.h"
36 #include "../libcli/security/secdesc.h"
39 #define DBGC_CLASS DBGC_REGISTRY
41 #define REGDB_VERSION_KEYNAME "INFO/version"
43 static struct db_context
*regdb
= NULL
;
44 static int regdb_refcount
;
46 static bool regdb_key_exists(struct db_context
*db
, const char *key
);
47 static WERROR
regdb_fetch_keys_internal(struct db_context
*db
, const char *key
,
48 struct regsubkey_ctr
*ctr
);
49 static bool regdb_store_keys_internal(struct db_context
*db
, const char *key
,
50 struct regsubkey_ctr
*ctr
);
51 static int regdb_fetch_values_internal(struct db_context
*db
, const char* key
,
52 struct regval_ctr
*values
);
53 static NTSTATUS
regdb_store_values_internal(struct db_context
*db
, const char *key
,
54 struct regval_ctr
*values
);
55 static WERROR
regdb_store_subkey_list(struct db_context
*db
, const char *parent
,
58 static WERROR
regdb_create_basekey(struct db_context
*db
, const char *key
);
59 static WERROR
regdb_create_subkey_internal(struct db_context
*db
,
64 struct regdb_trans_ctx
{
65 NTSTATUS (*action
)(struct db_context
*, void *);
69 static NTSTATUS
regdb_trans_do_action(struct db_context
*db
, void *private_data
)
73 struct regdb_trans_ctx
*ctx
= (struct regdb_trans_ctx
*)private_data
;
75 status
= dbwrap_fetch_int32(db
, REGDB_VERSION_KEYNAME
, &version_id
);
77 if (!NT_STATUS_IS_OK(status
)) {
78 DEBUG(0, ("ERROR: could not fetch registry db version: %s. "
79 "Denying access.\n", nt_errstr(status
)));
80 return NT_STATUS_ACCESS_DENIED
;
83 if (version_id
!= REGDB_CODE_VERSION
) {
84 DEBUG(0, ("ERROR: changed registry version %d found while "
85 "trying to write to the registry. Version %d "
86 "expected. Denying access.\n",
87 version_id
, REGDB_CODE_VERSION
));
88 return NT_STATUS_ACCESS_DENIED
;
91 status
= ctx
->action(db
, ctx
->private_data
);
95 static WERROR
regdb_trans_do(struct db_context
*db
,
96 NTSTATUS (*action
)(struct db_context
*, void *),
100 struct regdb_trans_ctx ctx
;
104 ctx
.private_data
= private_data
;
106 status
= dbwrap_trans_do(db
, regdb_trans_do_action
, &ctx
);
108 return ntstatus_to_werror(status
);
111 /* List the deepest path into the registry. All part components will be created.*/
113 /* If you want to have a part of the path controlled by the tdb and part by
114 a virtual registry db (e.g. printing), then you have to list the deepest path.
115 For example,"HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion/Print"
116 allows the reg_db backend to handle everything up to
117 "HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion" and then we'll hook
118 the reg_printing backend onto the last component of the path (see
119 KEY_PRINTING_2K in include/rpc_reg.h) --jerry */
121 static const char *builtin_registry_paths
[] = {
125 KEY_PRINTING
"\\Forms",
126 KEY_PRINTING
"\\Printers",
127 KEY_PRINTING
"\\Environments\\Windows NT x86\\Print Processors\\winprint",
134 KEY_SAMBA_GROUP_POLICY
,
135 KEY_GP_MACHINE_POLICY
,
136 KEY_GP_MACHINE_WIN_POLICY
,
139 KEY_GP_USER_WIN_POLICY
,
140 "HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon\\GPExtensions",
141 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Print\\Monitors",
143 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\DefaultUserConfiguration",
152 struct builtin_regkey_value
{
154 const char *valuename
;
162 static struct builtin_regkey_value builtin_registry_values
[] = {
163 { KEY_PRINTING_PORTS
,
164 SAMBA_PRINTER_PORT_NAME
, REG_SZ
, { "" } },
166 "DefaultSpoolDirectory", REG_SZ
, { "C:\\Windows\\System32\\Spool\\Printers" } },
168 "DisplayName", REG_SZ
, { "Event Log" } },
170 "ErrorControl", REG_DWORD
, { (char*)0x00000001 } },
171 { NULL
, NULL
, 0, { NULL
} }
174 static WERROR
create_key_recursive(struct db_context
*db
,
181 if (subkey
== NULL
) {
182 return WERR_INVALID_PARAM
;
186 return regdb_create_basekey(db
, subkey
);
189 p
= strrchr_m(path
, '\\');
192 werr
= create_key_recursive(db
, NULL
, path
);
195 werr
= create_key_recursive(db
, path
, p
+1);
199 if (!W_ERROR_IS_OK(werr
)) {
203 werr
= regdb_create_subkey_internal(db
, path
, subkey
);
210 * Initialize a key in the registry:
211 * create each component key of the specified path.
213 static WERROR
init_registry_key_internal(struct db_context
*db
,
214 const char *add_path
)
218 TALLOC_CTX
*frame
= talloc_stackframe();
220 if (add_path
== NULL
) {
221 werr
= WERR_INVALID_PARAM
;
225 key
= talloc_strdup(frame
, add_path
);
227 subkey
= strrchr_m(key
, '\\');
228 if (subkey
== NULL
) {
236 werr
= create_key_recursive(db
, key
, subkey
);
243 struct init_registry_key_context
{
244 const char *add_path
;
247 static NTSTATUS
init_registry_key_action(struct db_context
*db
,
250 struct init_registry_key_context
*init_ctx
=
251 (struct init_registry_key_context
*)private_data
;
253 return werror_to_ntstatus(init_registry_key_internal(
254 db
, init_ctx
->add_path
));
258 * Initialize a key in the registry:
259 * create each component key of the specified path,
260 * wrapped in one db transaction.
262 WERROR
init_registry_key(const char *add_path
)
264 struct init_registry_key_context init_ctx
;
266 if (regdb_key_exists(regdb
, add_path
)) {
270 init_ctx
.add_path
= add_path
;
272 return regdb_trans_do(regdb
,
273 init_registry_key_action
,
277 /***********************************************************************
278 Open the registry data in the tdb
279 ***********************************************************************/
281 static void regdb_ctr_add_value(struct regval_ctr
*ctr
,
282 struct builtin_regkey_value
*value
)
284 switch(value
->type
) {
286 regval_ctr_addvalue(ctr
, value
->valuename
, REG_DWORD
,
287 (uint8_t *)&value
->data
.dw_value
,
292 regval_ctr_addvalue_sz(ctr
, value
->valuename
,
297 DEBUG(0, ("regdb_ctr_add_value: invalid value type in "
298 "registry values [%d]\n", value
->type
));
302 static NTSTATUS
init_registry_data_action(struct db_context
*db
,
306 TALLOC_CTX
*frame
= talloc_stackframe();
307 struct regval_ctr
*values
;
310 /* loop over all of the predefined paths and add each component */
312 for (i
=0; builtin_registry_paths
[i
] != NULL
; i
++) {
313 if (regdb_key_exists(db
, builtin_registry_paths
[i
])) {
316 status
= werror_to_ntstatus(init_registry_key_internal(db
,
317 builtin_registry_paths
[i
]));
318 if (!NT_STATUS_IS_OK(status
)) {
323 /* loop over all of the predefined values and add each component */
325 for (i
=0; builtin_registry_values
[i
].path
!= NULL
; i
++) {
328 werr
= regval_ctr_init(frame
, &values
);
329 if (!W_ERROR_IS_OK(werr
)) {
330 status
= werror_to_ntstatus(werr
);
334 regdb_fetch_values_internal(db
,
335 builtin_registry_values
[i
].path
,
338 /* preserve existing values across restarts. Only add new ones */
340 if (!regval_ctr_value_exists(values
,
341 builtin_registry_values
[i
].valuename
))
343 regdb_ctr_add_value(values
,
344 &builtin_registry_values
[i
]);
345 status
= regdb_store_values_internal(db
,
346 builtin_registry_values
[i
].path
,
348 if (!NT_STATUS_IS_OK(status
)) {
355 status
= NT_STATUS_OK
;
363 WERROR
init_registry_data(void)
366 TALLOC_CTX
*frame
= talloc_stackframe();
367 struct regval_ctr
*values
;
371 * First, check for the existence of the needed keys and values.
372 * If all do already exist, we can save the writes.
374 for (i
=0; builtin_registry_paths
[i
] != NULL
; i
++) {
375 if (!regdb_key_exists(regdb
, builtin_registry_paths
[i
])) {
380 for (i
=0; builtin_registry_values
[i
].path
!= NULL
; i
++) {
381 werr
= regval_ctr_init(frame
, &values
);
382 W_ERROR_NOT_OK_GOTO_DONE(werr
);
384 regdb_fetch_values_internal(regdb
,
385 builtin_registry_values
[i
].path
,
387 if (!regval_ctr_value_exists(values
,
388 builtin_registry_values
[i
].valuename
))
403 * There are potentially quite a few store operations which are all
404 * indiviually wrapped in tdb transactions. Wrapping them in a single
405 * transaction gives just a single transaction_commit() to actually do
406 * its fsync()s. See tdb/common/transaction.c for info about nested
407 * transaction behaviour.
410 werr
= regdb_trans_do(regdb
,
411 init_registry_data_action
,
419 static int regdb_normalize_keynames_fn(struct db_record
*rec
,
422 TALLOC_CTX
*mem_ctx
= talloc_tos();
427 struct db_context
*db
= (struct db_context
*)private_data
;
429 key
= dbwrap_record_get_key(rec
);
430 if (key
.dptr
== NULL
|| key
.dsize
== 0) {
434 value
= dbwrap_record_get_value(rec
);
437 DEBUG(0, ("regdb_normalize_keynames_fn: ERROR: "
438 "NULL db context handed in via private_data\n"));
442 if (strncmp((const char *)key
.dptr
, REGDB_VERSION_KEYNAME
,
443 strlen(REGDB_VERSION_KEYNAME
)) == 0)
448 keyname
= strchr((const char *)key
.dptr
, '/');
450 keyname
= talloc_string_sub(mem_ctx
,
451 (const char *)key
.dptr
,
455 DEBUG(2, ("regdb_normalize_keynames_fn: Convert %s to %s\n",
456 (const char *)key
.dptr
,
459 /* Delete the original record and store the normalized key */
460 status
= dbwrap_record_delete(rec
);
461 if (!NT_STATUS_IS_OK(status
)) {
462 DEBUG(0,("regdb_normalize_keynames_fn: "
463 "tdb_delete for [%s] failed!\n",
464 (const char *)key
.dptr
));
468 status
= dbwrap_store_bystring(db
, keyname
, value
, TDB_REPLACE
);
469 if (!NT_STATUS_IS_OK(status
)) {
470 DEBUG(0,("regdb_normalize_keynames_fn: "
471 "failed to store new record for [%s]!\n",
480 static WERROR
regdb_store_regdb_version(struct db_context
*db
, uint32_t version
)
484 return WERR_CAN_NOT_COMPLETE
;
487 status
= dbwrap_trans_store_int32(db
, REGDB_VERSION_KEYNAME
, version
);
488 if (!NT_STATUS_IS_OK(status
)) {
489 DEBUG(1, ("regdb_store_regdb_version: error storing %s = %d: %s\n",
490 REGDB_VERSION_KEYNAME
, version
, nt_errstr(status
)));
491 return ntstatus_to_werror(status
);
493 DEBUG(10, ("regdb_store_regdb_version: stored %s = %d\n",
494 REGDB_VERSION_KEYNAME
, version
));
499 static WERROR
regdb_upgrade_v1_to_v2(struct db_context
*db
)
505 mem_ctx
= talloc_stackframe();
507 status
= dbwrap_traverse(db
, regdb_normalize_keynames_fn
, db
, NULL
);
508 if (!NT_STATUS_IS_OK(status
)) {
509 werr
= WERR_REG_IO_FAILURE
;
513 werr
= regdb_store_regdb_version(db
, REGDB_VERSION_V2
);
516 talloc_free(mem_ctx
);
520 static bool tdb_data_read_uint32(TDB_DATA
*buf
, uint32_t *result
)
522 const size_t len
= sizeof(uint32_t);
523 if (buf
->dsize
>= len
) {
524 *result
= IVAL(buf
->dptr
, 0);
532 static bool tdb_data_read_cstr(TDB_DATA
*buf
, char **result
)
534 const size_t len
= strnlen((char*)buf
->dptr
, buf
->dsize
) + 1;
535 if (buf
->dsize
>= len
) {
536 *result
= (char*)buf
->dptr
;
544 static bool tdb_data_is_cstr(TDB_DATA d
) {
545 if (tdb_data_is_empty(d
) || (d
.dptr
[d
.dsize
-1] != '\0')) {
548 return strchr((char *)d
.dptr
, '\0') == (char *)&d
.dptr
[d
.dsize
-1];
551 static bool upgrade_v2_to_v3_check_subkeylist(struct db_context
*db
,
555 static uint32_t zero
= 0;
556 static TDB_DATA empty_subkey_list
= {
557 .dptr
= (unsigned char*)&zero
,
558 .dsize
= sizeof(uint32_t),
560 bool success
= false;
561 char *path
= talloc_asprintf(talloc_tos(), "%s\\%s", key
, subkey
);
564 if (!dbwrap_exists(db
, string_term_tdb_data(path
))) {
567 DEBUG(10, ("regdb_upgrade_v2_to_v3: writing subkey list [%s]\n",
570 status
= dbwrap_store_bystring(db
, path
, empty_subkey_list
,
572 if (!NT_STATUS_IS_OK(status
)) {
573 DEBUG(0, ("regdb_upgrade_v2_to_v3: writing subkey list "
574 "[%s] failed\n", path
));
584 static bool upgrade_v2_to_v3_check_parent(struct db_context
*db
,
587 const char *sep
= strrchr_m(key
, '\\');
589 char *pkey
= talloc_strndup(talloc_tos(), key
, sep
-key
);
590 if (!dbwrap_exists(db
, string_term_tdb_data(pkey
))) {
591 DEBUG(0, ("regdb_upgrade_v2_to_v3: missing subkey list "
592 "[%s]\nrun \"net registry check\"\n", pkey
));
600 #define IS_EQUAL(d,s) (((d).dsize == strlen(s)+1) && \
601 (strcmp((char*)(d).dptr, (s)) == 0))
602 #define STARTS_WITH(d,s) (((d).dsize > strlen(s)) && \
603 (strncmp((char*)(d).dptr, (s), strlen(s)) == 0))
604 #define SSTR(d) (int)(d).dsize , (char*)(d).dptr
607 static int regdb_upgrade_v2_to_v3_fn(struct db_record
*rec
, void *private_data
)
609 struct db_context
*db
= (struct db_context
*)private_data
;
610 TDB_DATA key
= dbwrap_record_get_key(rec
);
611 TDB_DATA val
= dbwrap_record_get_value(rec
);
613 if (tdb_data_is_empty(key
)) {
618 DEBUG(0, ("regdb_upgrade_v2_to_v3_fn: ERROR: "
619 "NULL db context handed in via private_data\n"));
623 if (IS_EQUAL(key
, REGDB_VERSION_KEYNAME
) ||
624 STARTS_WITH(key
, REG_VALUE_PREFIX
) ||
625 STARTS_WITH(key
, REG_SECDESC_PREFIX
))
627 DEBUG(10, ("regdb_upgrade_v2_to_v3: skipping [%.*s]\n",
632 if (STARTS_WITH(key
, REG_SORTED_SUBKEYS_PREFIX
)) {
634 /* Delete the deprecated sorted subkeys cache. */
636 DEBUG(10, ("regdb_upgrade_v2_to_v3: deleting [%.*s]\n",
639 status
= dbwrap_record_delete(rec
);
640 if (!NT_STATUS_IS_OK(status
)) {
641 DEBUG(0, ("regdb_upgrade_v2_to_v3: deleting [%.*s] "
642 "failed!\n", SSTR(key
)));
649 if ( tdb_data_is_cstr(key
) &&
650 hive_info((char*)key
.dptr
) != NULL
)
653 * Found a regular subkey list record.
654 * Walk the list and create the list record for those
655 * subkeys that don't already have one.
658 char *subkey
, *path
= (char*)key
.dptr
;
659 uint32_t num_items
, found_items
= 0;
662 DEBUG(10, ("regdb_upgrade_v2_to_v3: scanning subkeylist of "
665 if (!tdb_data_read_uint32(&pos
, &num_items
)) {
666 /* invalid or empty - skip */
670 while (tdb_data_read_cstr(&pos
, &subkey
)) {
673 if (!upgrade_v2_to_v3_check_subkeylist(db
, path
, subkey
))
678 if (!upgrade_v2_to_v3_check_parent(db
, path
)) {
682 if (found_items
!= num_items
) {
683 DEBUG(0, ("regdb_upgrade_v2_to_v3: inconsistent subkey "
684 "list [%s]\nrun \"net registry check\"\n",
688 DEBUG(10, ("regdb_upgrade_v2_to_v3: skipping invalid [%.*s]\n"
689 "run \"net registry check\"\n", SSTR(key
)));
695 static WERROR
regdb_upgrade_v2_to_v3(struct db_context
*db
)
700 status
= dbwrap_traverse(db
, regdb_upgrade_v2_to_v3_fn
, db
, NULL
);
701 if (!NT_STATUS_IS_OK(status
)) {
702 werr
= WERR_REG_IO_FAILURE
;
706 werr
= regdb_store_regdb_version(db
, REGDB_VERSION_V3
);
712 /***********************************************************************
713 Open the registry database
714 ***********************************************************************/
716 WERROR
regdb_init(void)
723 DEBUG(10, ("regdb_init: incrementing refcount (%d->%d)\n",
724 regdb_refcount
, regdb_refcount
+1));
729 regdb
= db_open(NULL
, state_path("registry.tdb"), 0,
730 REG_TDB_FLAGS
, O_RDWR
, 0600,
731 DBWRAP_LOCK_ORDER_1
);
733 regdb
= db_open(NULL
, state_path("registry.tdb"), 0,
734 REG_TDB_FLAGS
, O_RDWR
|O_CREAT
, 0600,
735 DBWRAP_LOCK_ORDER_1
);
737 werr
= ntstatus_to_werror(map_nt_error_from_unix(errno
));
738 DEBUG(1,("regdb_init: Failed to open registry %s (%s)\n",
739 state_path("registry.tdb"), strerror(errno
) ));
743 werr
= regdb_store_regdb_version(regdb
, REGDB_CODE_VERSION
);
744 if (!W_ERROR_IS_OK(werr
)) {
745 DEBUG(1, ("regdb_init: Failed to store version: %s\n",
750 DEBUG(10,("regdb_init: Successfully created registry tdb\n"));
754 DEBUG(10, ("regdb_init: registry db openend. refcount reset (%d)\n",
757 status
= dbwrap_fetch_int32(regdb
, REGDB_VERSION_KEYNAME
, &vers_id
);
758 if (!NT_STATUS_IS_OK(status
)) {
759 DEBUG(10, ("regdb_init: registry version uninitialized "
760 "(got %d), initializing to version %d\n",
761 vers_id
, REGDB_VERSION_V1
));
764 * There was a regdb format version prior to version 1
765 * which did not store a INFO/version key. The format
766 * of this version was identical to version 1 except for
767 * the lack of the sorted subkey cache records.
768 * Since these are disposable, we can safely assume version
769 * 1 if no INFO/version key is found and run the db through
770 * the whole chain of upgrade. If the database was not
771 * initialized, this does not harm. If it was the unversioned
772 * version ("0"), then it do the right thing with the records.
774 werr
= regdb_store_regdb_version(regdb
, REGDB_VERSION_V1
);
775 if (!W_ERROR_IS_OK(werr
)) {
778 vers_id
= REGDB_VERSION_V1
;
781 if (vers_id
== REGDB_CODE_VERSION
) {
785 if (vers_id
> REGDB_CODE_VERSION
|| vers_id
== 0) {
786 DEBUG(0, ("regdb_init: unknown registry version %d "
787 "(code version = %d), refusing initialization\n",
788 vers_id
, REGDB_CODE_VERSION
));
789 return WERR_CAN_NOT_COMPLETE
;
792 if (dbwrap_transaction_start(regdb
) != 0) {
793 return WERR_REG_IO_FAILURE
;
796 if (vers_id
== REGDB_VERSION_V1
) {
797 DEBUG(10, ("regdb_init: upgrading registry from version %d "
798 "to %d\n", REGDB_VERSION_V1
, REGDB_VERSION_V2
));
800 werr
= regdb_upgrade_v1_to_v2(regdb
);
801 if (!W_ERROR_IS_OK(werr
)) {
802 dbwrap_transaction_cancel(regdb
);
806 vers_id
= REGDB_VERSION_V2
;
809 if (vers_id
== REGDB_VERSION_V2
) {
810 DEBUG(10, ("regdb_init: upgrading registry from version %d "
811 "to %d\n", REGDB_VERSION_V2
, REGDB_VERSION_V3
));
813 werr
= regdb_upgrade_v2_to_v3(regdb
);
814 if (!W_ERROR_IS_OK(werr
)) {
815 dbwrap_transaction_cancel(regdb
);
819 vers_id
= REGDB_VERSION_V3
;
822 /* future upgrade code should go here */
824 if (dbwrap_transaction_commit(regdb
) != 0) {
825 return WERR_REG_IO_FAILURE
;
831 /***********************************************************************
832 Open the registry. Must already have been initialized by regdb_init()
833 ***********************************************************************/
835 WERROR
regdb_open( void )
837 WERROR result
= WERR_OK
;
840 DEBUG(10, ("regdb_open: incrementing refcount (%d->%d)\n",
841 regdb_refcount
, regdb_refcount
+1));
848 regdb
= db_open(NULL
, state_path("registry.tdb"), 0,
849 REG_TDB_FLAGS
, O_RDWR
, 0600,
850 DBWRAP_LOCK_ORDER_1
);
852 result
= ntstatus_to_werror( map_nt_error_from_unix( errno
) );
853 DEBUG(0,("regdb_open: Failed to open %s! (%s)\n",
854 state_path("registry.tdb"), strerror(errno
) ));
860 DEBUG(10, ("regdb_open: registry db opened. refcount reset (%d)\n",
866 /***********************************************************************
867 ***********************************************************************/
869 int regdb_close( void )
871 if (regdb_refcount
== 0) {
877 DEBUG(10, ("regdb_close: decrementing refcount (%d->%d)\n",
878 regdb_refcount
+1, regdb_refcount
));
880 if ( regdb_refcount
> 0 )
883 SMB_ASSERT( regdb_refcount
>= 0 );
889 WERROR
regdb_transaction_start(void)
891 return (dbwrap_transaction_start(regdb
) == 0) ?
892 WERR_OK
: WERR_REG_IO_FAILURE
;
895 WERROR
regdb_transaction_commit(void)
897 return (dbwrap_transaction_commit(regdb
) == 0) ?
898 WERR_OK
: WERR_REG_IO_FAILURE
;
901 WERROR
regdb_transaction_cancel(void)
903 return (dbwrap_transaction_cancel(regdb
) == 0) ?
904 WERR_OK
: WERR_REG_IO_FAILURE
;
907 /***********************************************************************
908 return the tdb sequence number of the registry tdb.
909 this is an indicator for the content of the registry
910 having changed. it will change upon regdb_init, too, though.
911 ***********************************************************************/
912 int regdb_get_seqnum(void)
914 return dbwrap_get_seqnum(regdb
);
918 static WERROR
regdb_delete_key_with_prefix(struct db_context
*db
,
923 WERROR werr
= WERR_NOMEM
;
924 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
926 if (keyname
== NULL
) {
927 werr
= WERR_INVALID_PARAM
;
931 if (prefix
== NULL
) {
932 path
= discard_const_p(char, keyname
);
934 path
= talloc_asprintf(mem_ctx
, "%s\\%s", prefix
, keyname
);
940 path
= normalize_reg_path(mem_ctx
, path
);
945 werr
= ntstatus_to_werror(dbwrap_delete_bystring(db
, path
));
947 /* treat "not found" as ok */
948 if (W_ERROR_EQUAL(werr
, WERR_NOT_FOUND
)) {
953 talloc_free(mem_ctx
);
958 static WERROR
regdb_delete_values(struct db_context
*db
, const char *keyname
)
960 return regdb_delete_key_with_prefix(db
, keyname
, REG_VALUE_PREFIX
);
963 static WERROR
regdb_delete_secdesc(struct db_context
*db
, const char *keyname
)
965 return regdb_delete_key_with_prefix(db
, keyname
, REG_SECDESC_PREFIX
);
968 static WERROR
regdb_delete_subkeylist(struct db_context
*db
, const char *keyname
)
970 return regdb_delete_key_with_prefix(db
, keyname
, NULL
);
974 static WERROR
regdb_delete_key_lists(struct db_context
*db
, const char *keyname
)
978 werr
= regdb_delete_values(db
, keyname
);
979 if (!W_ERROR_IS_OK(werr
)) {
980 DEBUG(1, (__location__
" Deleting %s\\%s failed: %s\n",
981 REG_VALUE_PREFIX
, keyname
, win_errstr(werr
)));
985 werr
= regdb_delete_secdesc(db
, keyname
);
986 if (!W_ERROR_IS_OK(werr
)) {
987 DEBUG(1, (__location__
" Deleting %s\\%s failed: %s\n",
988 REG_SECDESC_PREFIX
, keyname
, win_errstr(werr
)));
992 werr
= regdb_delete_subkeylist(db
, keyname
);
993 if (!W_ERROR_IS_OK(werr
)) {
994 DEBUG(1, (__location__
" Deleting %s failed: %s\n",
995 keyname
, win_errstr(werr
)));
1003 /***********************************************************************
1004 Add subkey strings to the registry tdb under a defined key
1005 fmt is the same format as tdb_pack except this function only supports
1007 ***********************************************************************/
1009 static WERROR
regdb_store_keys_internal2(struct db_context
*db
,
1011 struct regsubkey_ctr
*ctr
)
1014 uint8
*buffer
= NULL
;
1017 uint32 num_subkeys
= regsubkey_ctr_numkeys(ctr
);
1018 char *keyname
= NULL
;
1019 TALLOC_CTX
*ctx
= talloc_stackframe();
1023 werr
= WERR_INVALID_PARAM
;
1027 keyname
= talloc_strdup(ctx
, key
);
1033 keyname
= normalize_reg_path(ctx
, keyname
);
1039 /* allocate some initial memory */
1041 buffer
= (uint8
*)SMB_MALLOC(1024);
1042 if (buffer
== NULL
) {
1049 /* store the number of subkeys */
1051 len
+= tdb_pack(buffer
+len
, buflen
-len
, "d", num_subkeys
);
1053 /* pack all the strings */
1055 for (i
=0; i
<num_subkeys
; i
++) {
1058 thistime
= tdb_pack(buffer
+len
, buflen
-len
, "f",
1059 regsubkey_ctr_specific_key(ctr
, i
));
1060 if (len
+thistime
> buflen
) {
1063 * tdb_pack hasn't done anything because of the short
1064 * buffer, allocate extra space.
1066 buffer
= SMB_REALLOC_ARRAY(buffer
, uint8_t,
1068 if(buffer
== NULL
) {
1069 DEBUG(0, ("regdb_store_keys: Failed to realloc "
1070 "memory of size [%u]\n",
1071 (unsigned int)(len
+thistime
)*2));
1075 buflen
= (len
+thistime
)*2;
1076 thistime2
= tdb_pack(
1077 buffer
+len
, buflen
-len
, "f",
1078 regsubkey_ctr_specific_key(ctr
, i
));
1079 if (thistime2
!= thistime
) {
1080 DEBUG(0, ("tdb_pack failed\n"));
1081 werr
= WERR_CAN_NOT_COMPLETE
;
1088 /* finally write out the data */
1092 werr
= ntstatus_to_werror(dbwrap_store_bystring(db
, keyname
, dbuf
,
1102 * Utility function to store a new empty list of
1103 * subkeys of given key specified as parent and subkey name
1104 * (thereby creating the key).
1105 * If the parent keyname is NULL, then the "subkey" is
1106 * interpreted as a base key.
1107 * If the subkey list does already exist, it is not modified.
1109 * Must be called from within a transaction.
1111 static WERROR
regdb_store_subkey_list(struct db_context
*db
, const char *parent
,
1116 struct regsubkey_ctr
*subkeys
= NULL
;
1117 TALLOC_CTX
*frame
= talloc_stackframe();
1119 if (parent
== NULL
) {
1120 path
= talloc_strdup(frame
, key
);
1122 path
= talloc_asprintf(frame
, "%s\\%s", parent
, key
);
1129 werr
= regsubkey_ctr_init(frame
, &subkeys
);
1130 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1132 werr
= regdb_fetch_keys_internal(db
, path
, subkeys
);
1133 if (W_ERROR_IS_OK(werr
)) {
1134 /* subkey list exists already - don't modify */
1138 werr
= regsubkey_ctr_reinit(subkeys
);
1139 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1141 /* create a record with 0 subkeys */
1142 werr
= regdb_store_keys_internal2(db
, path
, subkeys
);
1143 if (!W_ERROR_IS_OK(werr
)) {
1144 DEBUG(0, ("regdb_store_keys: Failed to store new record for "
1145 "key [%s]: %s\n", path
, win_errstr(werr
)));
1154 /***********************************************************************
1155 Store the new subkey record and create any child key records that
1156 do not currently exist
1157 ***********************************************************************/
1159 struct regdb_store_keys_context
{
1161 struct regsubkey_ctr
*ctr
;
1164 static NTSTATUS
regdb_store_keys_action(struct db_context
*db
,
1167 struct regdb_store_keys_context
*store_ctx
;
1171 struct regsubkey_ctr
*old_subkeys
= NULL
;
1172 char *oldkeyname
= NULL
;
1173 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
1175 store_ctx
= (struct regdb_store_keys_context
*)private_data
;
1178 * Re-fetch the old keys inside the transaction
1181 werr
= regsubkey_ctr_init(mem_ctx
, &old_subkeys
);
1182 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1184 werr
= regdb_fetch_keys_internal(db
, store_ctx
->key
, old_subkeys
);
1185 if (!W_ERROR_IS_OK(werr
) &&
1186 !W_ERROR_EQUAL(werr
, WERR_NOT_FOUND
))
1192 * Make the store operation as safe as possible without transactions:
1194 * (1) For each subkey removed from ctr compared with old_subkeys:
1196 * (a) First delete the value db entry.
1198 * (b) Next delete the secdesc db record.
1200 * (c) Then delete the subkey list entry.
1202 * (2) Now write the list of subkeys of the parent key,
1203 * deleting removed entries and adding new ones.
1205 * (3) Finally create the subkey list entries for the added keys.
1207 * This way if we crash half-way in between deleting the subkeys
1208 * and storing the parent's list of subkeys, no old data can pop up
1209 * out of the blue when re-adding keys later on.
1212 /* (1) delete removed keys' lists (values/secdesc/subkeys) */
1214 num_subkeys
= regsubkey_ctr_numkeys(old_subkeys
);
1215 for (i
=0; i
<num_subkeys
; i
++) {
1216 oldkeyname
= regsubkey_ctr_specific_key(old_subkeys
, i
);
1218 if (regsubkey_ctr_key_exists(store_ctx
->ctr
, oldkeyname
)) {
1220 * It's still around, don't delete
1225 path
= talloc_asprintf(mem_ctx
, "%s\\%s", store_ctx
->key
,
1232 werr
= regdb_delete_key_lists(db
, path
);
1233 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1238 TALLOC_FREE(old_subkeys
);
1240 /* (2) store the subkey list for the parent */
1242 werr
= regdb_store_keys_internal2(db
, store_ctx
->key
, store_ctx
->ctr
);
1243 if (!W_ERROR_IS_OK(werr
)) {
1244 DEBUG(0,("regdb_store_keys: Failed to store new subkey list "
1245 "for parent [%s]: %s\n", store_ctx
->key
,
1250 /* (3) now create records for any subkeys that don't already exist */
1252 num_subkeys
= regsubkey_ctr_numkeys(store_ctx
->ctr
);
1254 for (i
=0; i
<num_subkeys
; i
++) {
1257 subkey
= regsubkey_ctr_specific_key(store_ctx
->ctr
, i
);
1259 werr
= regdb_store_subkey_list(db
, store_ctx
->key
, subkey
);
1260 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1264 * Update the seqnum in the container to possibly
1265 * prevent next read from going to disk
1267 werr
= regsubkey_ctr_set_seqnum(store_ctx
->ctr
, dbwrap_get_seqnum(db
));
1270 talloc_free(mem_ctx
);
1271 return werror_to_ntstatus(werr
);
1274 static bool regdb_store_keys_internal(struct db_context
*db
, const char *key
,
1275 struct regsubkey_ctr
*ctr
)
1277 int num_subkeys
, old_num_subkeys
, i
;
1278 struct regsubkey_ctr
*old_subkeys
= NULL
;
1279 TALLOC_CTX
*ctx
= talloc_stackframe();
1282 struct regdb_store_keys_context store_ctx
;
1284 if (!regdb_key_exists(db
, key
)) {
1289 * fetch a list of the old subkeys so we can determine if anything has
1293 werr
= regsubkey_ctr_init(ctx
, &old_subkeys
);
1294 if (!W_ERROR_IS_OK(werr
)) {
1295 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
1299 werr
= regdb_fetch_keys_internal(db
, key
, old_subkeys
);
1300 if (!W_ERROR_IS_OK(werr
) &&
1301 !W_ERROR_EQUAL(werr
, WERR_NOT_FOUND
))
1306 num_subkeys
= regsubkey_ctr_numkeys(ctr
);
1307 old_num_subkeys
= regsubkey_ctr_numkeys(old_subkeys
);
1308 if ((num_subkeys
&& old_num_subkeys
) &&
1309 (num_subkeys
== old_num_subkeys
)) {
1311 for (i
= 0; i
< num_subkeys
; i
++) {
1312 if (strcmp(regsubkey_ctr_specific_key(ctr
, i
),
1313 regsubkey_ctr_specific_key(old_subkeys
, i
))
1319 if (i
== num_subkeys
) {
1321 * Nothing changed, no point to even start a tdb
1330 TALLOC_FREE(old_subkeys
);
1332 store_ctx
.key
= key
;
1333 store_ctx
.ctr
= ctr
;
1335 werr
= regdb_trans_do(db
,
1336 regdb_store_keys_action
,
1339 ret
= W_ERROR_IS_OK(werr
);
1347 static bool regdb_store_keys(const char *key
, struct regsubkey_ctr
*ctr
)
1349 return regdb_store_keys_internal(regdb
, key
, ctr
);
1353 * create a subkey of a given key
1356 struct regdb_create_subkey_context
{
1361 static NTSTATUS
regdb_create_subkey_action(struct db_context
*db
,
1365 struct regdb_create_subkey_context
*create_ctx
;
1366 struct regsubkey_ctr
*subkeys
;
1367 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
1369 create_ctx
= (struct regdb_create_subkey_context
*)private_data
;
1371 werr
= regsubkey_ctr_init(mem_ctx
, &subkeys
);
1372 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1374 werr
= regdb_fetch_keys_internal(db
, create_ctx
->key
, subkeys
);
1375 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1377 werr
= regsubkey_ctr_addkey(subkeys
, create_ctx
->subkey
);
1378 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1380 werr
= regdb_store_keys_internal2(db
, create_ctx
->key
, subkeys
);
1381 if (!W_ERROR_IS_OK(werr
)) {
1382 DEBUG(0, (__location__
" failed to store new subkey list for "
1383 "parent key %s: %s\n", create_ctx
->key
,
1387 werr
= regdb_store_subkey_list(db
, create_ctx
->key
, create_ctx
->subkey
);
1390 talloc_free(mem_ctx
);
1391 return werror_to_ntstatus(werr
);
1394 static WERROR
regdb_create_subkey_internal(struct db_context
*db
,
1399 struct regsubkey_ctr
*subkeys
;
1400 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
1401 struct regdb_create_subkey_context create_ctx
;
1403 if (!regdb_key_exists(db
, key
)) {
1404 werr
= WERR_NOT_FOUND
;
1408 werr
= regsubkey_ctr_init(mem_ctx
, &subkeys
);
1409 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1411 werr
= regdb_fetch_keys_internal(db
, key
, subkeys
);
1412 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1414 if (regsubkey_ctr_key_exists(subkeys
, subkey
)) {
1417 newkey
= talloc_asprintf(mem_ctx
, "%s\\%s", key
, subkey
);
1418 if (newkey
== NULL
) {
1423 if (regdb_key_exists(db
, newkey
)) {
1429 talloc_free(subkeys
);
1431 create_ctx
.key
= key
;
1432 create_ctx
.subkey
= subkey
;
1434 werr
= regdb_trans_do(db
,
1435 regdb_create_subkey_action
,
1439 talloc_free(mem_ctx
);
1443 static WERROR
regdb_create_subkey(const char *key
, const char *subkey
)
1445 return regdb_create_subkey_internal(regdb
, key
, subkey
);
1452 struct regdb_create_basekey_context
{
1456 static NTSTATUS
regdb_create_basekey_action(struct db_context
*db
,
1460 struct regdb_create_basekey_context
*create_ctx
;
1462 create_ctx
= (struct regdb_create_basekey_context
*)private_data
;
1464 werr
= regdb_store_subkey_list(db
, NULL
, create_ctx
->key
);
1466 return werror_to_ntstatus(werr
);
1469 static WERROR
regdb_create_basekey(struct db_context
*db
, const char *key
)
1472 struct regdb_create_subkey_context create_ctx
;
1474 create_ctx
.key
= key
;
1476 werr
= regdb_trans_do(db
,
1477 regdb_create_basekey_action
,
1484 * create a subkey of a given key
1487 struct regdb_delete_subkey_context
{
1494 static NTSTATUS
regdb_delete_subkey_action(struct db_context
*db
,
1498 struct regdb_delete_subkey_context
*delete_ctx
;
1499 struct regsubkey_ctr
*subkeys
;
1500 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
1502 delete_ctx
= (struct regdb_delete_subkey_context
*)private_data
;
1504 werr
= regdb_delete_key_lists(db
, delete_ctx
->path
);
1505 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1507 if (delete_ctx
->lazy
) {
1511 werr
= regsubkey_ctr_init(mem_ctx
, &subkeys
);
1512 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1514 werr
= regdb_fetch_keys_internal(db
, delete_ctx
->key
, subkeys
);
1515 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1517 werr
= regsubkey_ctr_delkey(subkeys
, delete_ctx
->subkey
);
1518 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1520 werr
= regdb_store_keys_internal2(db
, delete_ctx
->key
, subkeys
);
1521 if (!W_ERROR_IS_OK(werr
)) {
1522 DEBUG(0, (__location__
" failed to store new subkey_list for "
1523 "parent key %s: %s\n", delete_ctx
->key
,
1528 talloc_free(mem_ctx
);
1529 return werror_to_ntstatus(werr
);
1532 static WERROR
regdb_delete_subkey(const char *key
, const char *subkey
, bool lazy
)
1536 struct regdb_delete_subkey_context delete_ctx
;
1537 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
1539 if (!regdb_key_exists(regdb
, key
)) {
1540 werr
= WERR_NOT_FOUND
;
1544 path
= talloc_asprintf(mem_ctx
, "%s\\%s", key
, subkey
);
1550 if (!regdb_key_exists(regdb
, path
)) {
1555 delete_ctx
.key
= key
;
1556 delete_ctx
.subkey
= subkey
;
1557 delete_ctx
.path
= path
;
1558 delete_ctx
.lazy
= lazy
;
1560 werr
= regdb_trans_do(regdb
,
1561 regdb_delete_subkey_action
,
1565 talloc_free(mem_ctx
);
1569 static TDB_DATA
regdb_fetch_key_internal(struct db_context
*db
,
1570 TALLOC_CTX
*mem_ctx
, const char *key
)
1576 path
= normalize_reg_path(mem_ctx
, key
);
1578 return make_tdb_data(NULL
, 0);
1581 status
= dbwrap_fetch_bystring(db
, mem_ctx
, path
, &data
);
1582 if (!NT_STATUS_IS_OK(status
)) {
1592 * Check for the existence of a key.
1594 * Existence of a key is authoritatively defined by
1595 * the existence of the record that contains the list
1598 * Return false, if the record does not match the correct
1599 * structure of an initial 4-byte counter and then a
1600 * list of the corresponding number of zero-terminated
1603 static bool regdb_key_exists(struct db_context
*db
, const char *key
)
1605 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
1611 uint32_t num_items
, i
;
1618 path
= normalize_reg_path(mem_ctx
, key
);
1620 DEBUG(0, ("out of memory! (talloc failed)\n"));
1624 if (*path
== '\0') {
1628 value
= regdb_fetch_key_internal(db
, mem_ctx
, path
);
1629 if (value
.dptr
== NULL
) {
1633 if (value
.dsize
== 0) {
1634 DEBUG(10, ("regdb_key_exists: subkeylist-record for key "
1635 "[%s] is empty: Could be a deleted record in a "
1636 "clustered (ctdb) environment?\n",
1641 len
= tdb_unpack(value
.dptr
, value
.dsize
, "d", &num_items
);
1642 if (len
== (int32_t)-1) {
1643 DEBUG(1, ("regdb_key_exists: ERROR: subkeylist-record for key "
1644 "[%s] is invalid: Could not parse initial 4-byte "
1645 "counter. record data length is %u.\n",
1646 path
, (unsigned int)value
.dsize
));
1651 * Note: the tdb_unpack check above implies that len <= value.dsize
1653 buflen
= value
.dsize
- len
;
1654 buf
= (const char *)value
.dptr
+ len
;
1658 for (i
= 0; i
< num_items
; i
++) {
1662 len
= strnlen(buf
, buflen
) + 1;
1664 DEBUG(1, ("regdb_key_exists: ERROR: subkeylist-record "
1665 "for key [%s] is corrupt: %u items expected, "
1666 "item number %u is not zero terminated.\n",
1667 path
, num_items
, i
+1));
1676 DEBUG(1, ("regdb_key_exists: ERROR: subkeylist-record for key "
1677 "[%s] is corrupt: %u items expected and found, but "
1678 "the record contains additional %u bytes\n",
1679 path
, num_items
, buflen
));
1683 if (i
< num_items
) {
1684 DEBUG(1, ("regdb_key_exists: ERROR: subkeylist-record for key "
1685 "[%s] is corrupt: %u items expected, but only %u "
1687 path
, num_items
, i
+1));
1694 TALLOC_FREE(mem_ctx
);
1699 /***********************************************************************
1700 Retrieve an array of strings containing subkeys. Memory should be
1701 released by the caller.
1702 ***********************************************************************/
1704 static WERROR
regdb_fetch_keys_internal(struct db_context
*db
, const char *key
,
1705 struct regsubkey_ctr
*ctr
)
1713 TALLOC_CTX
*frame
= talloc_stackframe();
1715 int seqnum
[2], count
;
1717 DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key
? key
: "NULL"));
1719 if (!regdb_key_exists(db
, key
)) {
1720 DEBUG(10, ("key [%s] not found\n", key
));
1721 werr
= WERR_NOT_FOUND
;
1725 werr
= regsubkey_ctr_reinit(ctr
);
1726 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1730 seqnum
[0] = dbwrap_get_seqnum(db
);
1734 TALLOC_FREE(value
.dptr
);
1735 value
= regdb_fetch_key_internal(db
, frame
, key
);
1736 seqnum
[count
% 2] = dbwrap_get_seqnum(db
);
1738 } while (seqnum
[0] != seqnum
[1]);
1741 DEBUG(5, ("regdb_fetch_keys_internal: it took %d attempts to "
1742 "fetch key '%s' with constant seqnum\n",
1746 werr
= regsubkey_ctr_set_seqnum(ctr
, seqnum
[0]);
1747 if (!W_ERROR_IS_OK(werr
)) {
1751 if (value
.dsize
== 0 || value
.dptr
== NULL
) {
1752 DEBUG(10, ("regdb_fetch_keys: no subkeys found for key [%s]\n",
1758 buflen
= value
.dsize
;
1759 len
= tdb_unpack( buf
, buflen
, "d", &num_items
);
1760 if (len
== (uint32_t)-1) {
1761 werr
= WERR_NOT_FOUND
;
1765 for (i
=0; i
<num_items
; i
++) {
1766 len
+= tdb_unpack(buf
+len
, buflen
-len
, "f", subkeyname
);
1767 werr
= regsubkey_ctr_addkey(ctr
, subkeyname
);
1768 if (!W_ERROR_IS_OK(werr
)) {
1769 DEBUG(5, ("regdb_fetch_keys: regsubkey_ctr_addkey "
1770 "failed: %s\n", win_errstr(werr
)));
1776 DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items
));
1783 static int regdb_fetch_keys(const char *key
, struct regsubkey_ctr
*ctr
)
1787 werr
= regdb_fetch_keys_internal(regdb
, key
, ctr
);
1788 if (!W_ERROR_IS_OK(werr
)) {
1792 return regsubkey_ctr_numkeys(ctr
);
1795 /****************************************************************************
1796 Unpack a list of registry values frem the TDB
1797 ***************************************************************************/
1799 static int regdb_unpack_values(struct regval_ctr
*values
, uint8
*buf
, int buflen
)
1806 uint32 num_values
= 0;
1809 /* loop and unpack the rest of the registry values */
1811 len
+= tdb_unpack(buf
+len
, buflen
-len
, "d", &num_values
);
1813 for ( i
=0; i
<num_values
; i
++ ) {
1814 /* unpack the next regval */
1819 valuename
[0] = '\0';
1820 len
+= tdb_unpack(buf
+len
, buflen
-len
, "fdB",
1826 regval_ctr_addvalue(values
, valuename
, type
,
1827 (uint8_t *)data_p
, size
);
1828 SAFE_FREE(data_p
); /* 'B' option to tdb_unpack does a malloc() */
1830 DEBUG(10, ("regdb_unpack_values: value[%d]: name[%s] len[%d]\n",
1831 i
, valuename
, size
));
1837 /****************************************************************************
1838 Pack all values in all printer keys
1839 ***************************************************************************/
1841 static int regdb_pack_values(struct regval_ctr
*values
, uint8
*buf
, int buflen
)
1845 struct regval_blob
*val
;
1851 num_values
= regval_ctr_numvals( values
);
1853 /* pack the number of values first */
1855 len
+= tdb_pack( buf
+len
, buflen
-len
, "d", num_values
);
1857 /* loop over all values */
1859 for ( i
=0; i
<num_values
; i
++ ) {
1860 val
= regval_ctr_specific_value( values
, i
);
1861 len
+= tdb_pack(buf
+len
, buflen
-len
, "fdB",
1865 regval_data_p(val
) );
1871 /***********************************************************************
1872 Retrieve an array of strings containing subkeys. Memory should be
1873 released by the caller.
1874 ***********************************************************************/
1876 static int regdb_fetch_values_internal(struct db_context
*db
, const char* key
,
1877 struct regval_ctr
*values
)
1879 char *keystr
= NULL
;
1880 TALLOC_CTX
*ctx
= talloc_stackframe();
1884 int seqnum
[2], count
;
1886 DEBUG(10,("regdb_fetch_values: Looking for values of key [%s]\n", key
));
1888 if (!regdb_key_exists(db
, key
)) {
1892 keystr
= talloc_asprintf(ctx
, "%s\\%s", REG_VALUE_PREFIX
, key
);
1899 seqnum
[0] = dbwrap_get_seqnum(db
);
1903 TALLOC_FREE(value
.dptr
);
1904 value
= regdb_fetch_key_internal(db
, ctx
, keystr
);
1905 seqnum
[count
% 2] = dbwrap_get_seqnum(db
);
1906 } while (seqnum
[0] != seqnum
[1]);
1909 DEBUG(5, ("regdb_fetch_values_internal: it took %d attempts "
1910 "to fetch key '%s' with constant seqnum\n",
1914 werr
= regval_ctr_set_seqnum(values
, seqnum
[0]);
1915 if (!W_ERROR_IS_OK(werr
)) {
1920 /* all keys have zero values by default */
1924 regdb_unpack_values(values
, value
.dptr
, value
.dsize
);
1925 ret
= regval_ctr_numvals(values
);
1932 static int regdb_fetch_values(const char* key
, struct regval_ctr
*values
)
1934 return regdb_fetch_values_internal(regdb
, key
, values
);
1937 static NTSTATUS
regdb_store_values_internal(struct db_context
*db
,
1939 struct regval_ctr
*values
)
1941 TDB_DATA old_data
, data
;
1942 char *keystr
= NULL
;
1943 TALLOC_CTX
*ctx
= talloc_stackframe();
1948 DEBUG(10,("regdb_store_values: Looking for values of key [%s]\n", key
));
1950 if (!regdb_key_exists(db
, key
)) {
1951 status
= NT_STATUS_NOT_FOUND
;
1955 if (regval_ctr_numvals(values
) == 0) {
1956 werr
= regdb_delete_values(db
, key
);
1957 if (!W_ERROR_IS_OK(werr
)) {
1958 return werror_to_ntstatus(werr
);
1962 * update the seqnum in the cache to prevent the next read
1963 * from going to disk
1965 werr
= regval_ctr_set_seqnum(values
, dbwrap_get_seqnum(db
));
1966 return werror_to_ntstatus(werr
);
1971 len
= regdb_pack_values(values
, data
.dptr
, data
.dsize
);
1973 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
1974 status
= NT_STATUS_UNSUCCESSFUL
;
1978 data
.dptr
= talloc_array(ctx
, uint8
, len
);
1981 len
= regdb_pack_values(values
, data
.dptr
, data
.dsize
);
1983 SMB_ASSERT( len
== data
.dsize
);
1985 keystr
= talloc_asprintf(ctx
, "%s\\%s", REG_VALUE_PREFIX
, key
);
1987 status
= NT_STATUS_NO_MEMORY
;
1990 keystr
= normalize_reg_path(ctx
, keystr
);
1992 status
= NT_STATUS_NO_MEMORY
;
1996 status
= dbwrap_fetch_bystring(db
, ctx
, keystr
, &old_data
);
1998 if (NT_STATUS_IS_OK(status
)
1999 && (old_data
.dptr
!= NULL
)
2000 && (old_data
.dsize
== data
.dsize
)
2001 && (memcmp(old_data
.dptr
, data
.dptr
, data
.dsize
) == 0))
2003 status
= NT_STATUS_OK
;
2007 status
= dbwrap_trans_store_bystring(db
, keystr
, data
, TDB_REPLACE
);
2008 if (!NT_STATUS_IS_OK(status
)) {
2009 DEBUG(0, ("regdb_store_values_internal: error storing: %s\n", nt_errstr(status
)));
2014 * update the seqnum in the cache to prevent the next read
2015 * from going to disk
2017 werr
= regval_ctr_set_seqnum(values
, dbwrap_get_seqnum(db
));
2018 status
= werror_to_ntstatus(werr
);
2025 struct regdb_store_values_ctx
{
2027 struct regval_ctr
*values
;
2030 static NTSTATUS
regdb_store_values_action(struct db_context
*db
,
2034 struct regdb_store_values_ctx
*ctx
=
2035 (struct regdb_store_values_ctx
*)private_data
;
2037 status
= regdb_store_values_internal(db
, ctx
->key
, ctx
->values
);
2042 static bool regdb_store_values(const char *key
, struct regval_ctr
*values
)
2045 struct regdb_store_values_ctx ctx
;
2048 ctx
.values
= values
;
2050 werr
= regdb_trans_do(regdb
, regdb_store_values_action
, &ctx
);
2052 return W_ERROR_IS_OK(werr
);
2055 static WERROR
regdb_get_secdesc(TALLOC_CTX
*mem_ctx
, const char *key
,
2056 struct security_descriptor
**psecdesc
)
2061 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
2062 WERROR err
= WERR_OK
;
2064 DEBUG(10, ("regdb_get_secdesc: Getting secdesc of key [%s]\n", key
));
2066 if (!regdb_key_exists(regdb
, key
)) {
2071 tdbkey
= talloc_asprintf(tmp_ctx
, "%s\\%s", REG_SECDESC_PREFIX
, key
);
2072 if (tdbkey
== NULL
) {
2077 tdbkey
= normalize_reg_path(tmp_ctx
, tdbkey
);
2078 if (tdbkey
== NULL
) {
2083 status
= dbwrap_fetch_bystring(regdb
, tmp_ctx
, tdbkey
, &data
);
2084 if (!NT_STATUS_IS_OK(status
)) {
2089 status
= unmarshall_sec_desc(mem_ctx
, (uint8
*)data
.dptr
, data
.dsize
,
2092 if (NT_STATUS_EQUAL(status
, NT_STATUS_NO_MEMORY
)) {
2094 } else if (!NT_STATUS_IS_OK(status
)) {
2095 err
= WERR_REG_CORRUPT
;
2099 TALLOC_FREE(tmp_ctx
);
2103 struct regdb_set_secdesc_ctx
{
2105 struct security_descriptor
*secdesc
;
2108 static NTSTATUS
regdb_set_secdesc_action(struct db_context
*db
,
2114 struct regdb_set_secdesc_ctx
*ctx
=
2115 (struct regdb_set_secdesc_ctx
*)private_data
;
2116 TALLOC_CTX
*frame
= talloc_stackframe();
2118 tdbkey
= talloc_asprintf(frame
, "%s\\%s", REG_SECDESC_PREFIX
, ctx
->key
);
2119 if (tdbkey
== NULL
) {
2120 status
= NT_STATUS_NO_MEMORY
;
2124 tdbkey
= normalize_reg_path(frame
, tdbkey
);
2125 if (tdbkey
== NULL
) {
2126 status
= NT_STATUS_NO_MEMORY
;
2130 if (ctx
->secdesc
== NULL
) {
2131 /* assuming a delete */
2132 status
= dbwrap_delete_bystring(db
, tdbkey
);
2136 status
= marshall_sec_desc(frame
, ctx
->secdesc
, &tdbdata
.dptr
,
2138 if (!NT_STATUS_IS_OK(status
)) {
2142 status
= dbwrap_store_bystring(db
, tdbkey
, tdbdata
, 0);
2149 static WERROR
regdb_set_secdesc(const char *key
,
2150 struct security_descriptor
*secdesc
)
2153 struct regdb_set_secdesc_ctx ctx
;
2155 if (!regdb_key_exists(regdb
, key
)) {
2161 ctx
.secdesc
= secdesc
;
2163 err
= regdb_trans_do(regdb
, regdb_set_secdesc_action
, &ctx
);
2169 static bool regdb_subkeys_need_update(struct regsubkey_ctr
*subkeys
)
2171 return (regdb_get_seqnum() != regsubkey_ctr_get_seqnum(subkeys
));
2174 static bool regdb_values_need_update(struct regval_ctr
*values
)
2176 return (regdb_get_seqnum() != regval_ctr_get_seqnum(values
));
2180 * Table of function pointers for default access
2183 struct registry_ops regdb_ops
= {
2184 .fetch_subkeys
= regdb_fetch_keys
,
2185 .fetch_values
= regdb_fetch_values
,
2186 .store_subkeys
= regdb_store_keys
,
2187 .store_values
= regdb_store_values
,
2188 .create_subkey
= regdb_create_subkey
,
2189 .delete_subkey
= regdb_delete_subkey
,
2190 .get_secdesc
= regdb_get_secdesc
,
2191 .set_secdesc
= regdb_set_secdesc
,
2192 .subkeys_need_update
= regdb_subkeys_need_update
,
2193 .values_need_update
= regdb_values_need_update