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_bystring(db
, REGDB_VERSION_KEYNAME
,
78 if (!NT_STATUS_IS_OK(status
)) {
79 DEBUG(0, ("ERROR: could not fetch registry db version: %s. "
80 "Denying access.\n", nt_errstr(status
)));
81 return NT_STATUS_ACCESS_DENIED
;
84 if (version_id
!= REGDB_CODE_VERSION
) {
85 DEBUG(0, ("ERROR: changed registry version %d found while "
86 "trying to write to the registry. Version %d "
87 "expected. Denying access.\n",
88 version_id
, REGDB_CODE_VERSION
));
89 return NT_STATUS_ACCESS_DENIED
;
92 status
= ctx
->action(db
, ctx
->private_data
);
96 static WERROR
regdb_trans_do(struct db_context
*db
,
97 NTSTATUS (*action
)(struct db_context
*, void *),
101 struct regdb_trans_ctx ctx
;
105 ctx
.private_data
= private_data
;
107 status
= dbwrap_trans_do(db
, regdb_trans_do_action
, &ctx
);
109 return ntstatus_to_werror(status
);
112 /* List the deepest path into the registry. All part components will be created.*/
114 /* If you want to have a part of the path controlled by the tdb and part by
115 a virtual registry db (e.g. printing), then you have to list the deepest path.
116 For example,"HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion/Print"
117 allows the reg_db backend to handle everything up to
118 "HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion" and then we'll hook
119 the reg_printing backend onto the last component of the path (see
120 KEY_PRINTING_2K in include/rpc_reg.h) --jerry */
122 static const char *builtin_registry_paths
[] = {
126 KEY_PRINTING
"\\Forms",
127 KEY_PRINTING
"\\Printers",
128 KEY_PRINTING
"\\Environments\\Windows NT x86\\Print Processors\\winprint",
135 KEY_SAMBA_GROUP_POLICY
,
136 KEY_GP_MACHINE_POLICY
,
137 KEY_GP_MACHINE_WIN_POLICY
,
140 KEY_GP_USER_WIN_POLICY
,
141 "HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon\\GPExtensions",
142 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Print\\Monitors",
144 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\DefaultUserConfiguration",
153 struct builtin_regkey_value
{
155 const char *valuename
;
163 static struct builtin_regkey_value builtin_registry_values
[] = {
164 { KEY_PRINTING_PORTS
,
165 SAMBA_PRINTER_PORT_NAME
, REG_SZ
, { "" } },
167 "DefaultSpoolDirectory", REG_SZ
, { "C:\\Windows\\System32\\Spool\\Printers" } },
169 "DisplayName", REG_SZ
, { "Event Log" } },
171 "ErrorControl", REG_DWORD
, { (char*)0x00000001 } },
172 { NULL
, NULL
, 0, { NULL
} }
175 static WERROR
create_key_recursive(struct db_context
*db
,
182 if (subkey
== NULL
) {
183 return WERR_INVALID_PARAM
;
187 return regdb_create_basekey(db
, subkey
);
190 p
= strrchr_m(path
, '\\');
193 werr
= create_key_recursive(db
, NULL
, path
);
196 werr
= create_key_recursive(db
, path
, p
+1);
200 if (!W_ERROR_IS_OK(werr
)) {
204 werr
= regdb_create_subkey_internal(db
, path
, subkey
);
211 * Initialize a key in the registry:
212 * create each component key of the specified path.
214 static WERROR
init_registry_key_internal(struct db_context
*db
,
215 const char *add_path
)
219 TALLOC_CTX
*frame
= talloc_stackframe();
221 if (add_path
== NULL
) {
222 werr
= WERR_INVALID_PARAM
;
226 key
= talloc_strdup(frame
, add_path
);
228 subkey
= strrchr_m(key
, '\\');
229 if (subkey
== NULL
) {
237 werr
= create_key_recursive(db
, key
, subkey
);
244 struct init_registry_key_context
{
245 const char *add_path
;
248 static NTSTATUS
init_registry_key_action(struct db_context
*db
,
251 struct init_registry_key_context
*init_ctx
=
252 (struct init_registry_key_context
*)private_data
;
254 return werror_to_ntstatus(init_registry_key_internal(
255 db
, init_ctx
->add_path
));
259 * Initialize a key in the registry:
260 * create each component key of the specified path,
261 * wrapped in one db transaction.
263 WERROR
init_registry_key(const char *add_path
)
265 struct init_registry_key_context init_ctx
;
267 if (regdb_key_exists(regdb
, add_path
)) {
271 init_ctx
.add_path
= add_path
;
273 return regdb_trans_do(regdb
,
274 init_registry_key_action
,
278 /***********************************************************************
279 Open the registry data in the tdb
280 ***********************************************************************/
282 static void regdb_ctr_add_value(struct regval_ctr
*ctr
,
283 struct builtin_regkey_value
*value
)
285 switch(value
->type
) {
287 regval_ctr_addvalue(ctr
, value
->valuename
, REG_DWORD
,
288 (uint8_t *)&value
->data
.dw_value
,
293 regval_ctr_addvalue_sz(ctr
, value
->valuename
,
298 DEBUG(0, ("regdb_ctr_add_value: invalid value type in "
299 "registry values [%d]\n", value
->type
));
303 static NTSTATUS
init_registry_data_action(struct db_context
*db
,
307 TALLOC_CTX
*frame
= talloc_stackframe();
308 struct regval_ctr
*values
;
311 /* loop over all of the predefined paths and add each component */
313 for (i
=0; builtin_registry_paths
[i
] != NULL
; i
++) {
314 if (regdb_key_exists(db
, builtin_registry_paths
[i
])) {
317 status
= werror_to_ntstatus(init_registry_key_internal(db
,
318 builtin_registry_paths
[i
]));
319 if (!NT_STATUS_IS_OK(status
)) {
324 /* loop over all of the predefined values and add each component */
326 for (i
=0; builtin_registry_values
[i
].path
!= NULL
; i
++) {
329 werr
= regval_ctr_init(frame
, &values
);
330 if (!W_ERROR_IS_OK(werr
)) {
331 status
= werror_to_ntstatus(werr
);
335 regdb_fetch_values_internal(db
,
336 builtin_registry_values
[i
].path
,
339 /* preserve existing values across restarts. Only add new ones */
341 if (!regval_ctr_value_exists(values
,
342 builtin_registry_values
[i
].valuename
))
344 regdb_ctr_add_value(values
,
345 &builtin_registry_values
[i
]);
346 status
= regdb_store_values_internal(db
,
347 builtin_registry_values
[i
].path
,
349 if (!NT_STATUS_IS_OK(status
)) {
356 status
= NT_STATUS_OK
;
364 WERROR
init_registry_data(void)
367 TALLOC_CTX
*frame
= talloc_stackframe();
368 struct regval_ctr
*values
;
372 * First, check for the existence of the needed keys and values.
373 * If all do already exist, we can save the writes.
375 for (i
=0; builtin_registry_paths
[i
] != NULL
; i
++) {
376 if (!regdb_key_exists(regdb
, builtin_registry_paths
[i
])) {
381 for (i
=0; builtin_registry_values
[i
].path
!= NULL
; i
++) {
382 werr
= regval_ctr_init(frame
, &values
);
383 W_ERROR_NOT_OK_GOTO_DONE(werr
);
385 regdb_fetch_values_internal(regdb
,
386 builtin_registry_values
[i
].path
,
388 if (!regval_ctr_value_exists(values
,
389 builtin_registry_values
[i
].valuename
))
404 * There are potentially quite a few store operations which are all
405 * indiviually wrapped in tdb transactions. Wrapping them in a single
406 * transaction gives just a single transaction_commit() to actually do
407 * its fsync()s. See tdb/common/transaction.c for info about nested
408 * transaction behaviour.
411 werr
= regdb_trans_do(regdb
,
412 init_registry_data_action
,
420 static int regdb_normalize_keynames_fn(struct db_record
*rec
,
423 TALLOC_CTX
*mem_ctx
= talloc_tos();
428 struct db_context
*db
= (struct db_context
*)private_data
;
430 key
= dbwrap_record_get_key(rec
);
431 if (key
.dptr
== NULL
|| key
.dsize
== 0) {
435 value
= dbwrap_record_get_value(rec
);
438 DEBUG(0, ("regdb_normalize_keynames_fn: ERROR: "
439 "NULL db context handed in via private_data\n"));
443 if (strncmp((const char *)key
.dptr
, REGDB_VERSION_KEYNAME
,
444 strlen(REGDB_VERSION_KEYNAME
)) == 0)
449 keyname
= strchr((const char *)key
.dptr
, '/');
451 keyname
= talloc_string_sub(mem_ctx
,
452 (const char *)key
.dptr
,
456 DEBUG(2, ("regdb_normalize_keynames_fn: Convert %s to %s\n",
457 (const char *)key
.dptr
,
460 /* Delete the original record and store the normalized key */
461 status
= dbwrap_record_delete(rec
);
462 if (!NT_STATUS_IS_OK(status
)) {
463 DEBUG(0,("regdb_normalize_keynames_fn: "
464 "tdb_delete for [%s] failed!\n",
465 (const char *)key
.dptr
));
469 status
= dbwrap_store_bystring(db
, keyname
, value
, TDB_REPLACE
);
470 if (!NT_STATUS_IS_OK(status
)) {
471 DEBUG(0,("regdb_normalize_keynames_fn: "
472 "failed to store new record for [%s]!\n",
481 static WERROR
regdb_store_regdb_version(struct db_context
*db
, uint32_t version
)
485 return WERR_CAN_NOT_COMPLETE
;
488 status
= dbwrap_trans_store_int32_bystring(db
, REGDB_VERSION_KEYNAME
,
490 if (!NT_STATUS_IS_OK(status
)) {
491 DEBUG(1, ("regdb_store_regdb_version: error storing %s = %d: %s\n",
492 REGDB_VERSION_KEYNAME
, version
, nt_errstr(status
)));
493 return ntstatus_to_werror(status
);
495 DEBUG(10, ("regdb_store_regdb_version: stored %s = %d\n",
496 REGDB_VERSION_KEYNAME
, version
));
501 static WERROR
regdb_upgrade_v1_to_v2(struct db_context
*db
)
507 mem_ctx
= talloc_stackframe();
509 status
= dbwrap_traverse(db
, regdb_normalize_keynames_fn
, db
, NULL
);
510 if (!NT_STATUS_IS_OK(status
)) {
511 werr
= WERR_REG_IO_FAILURE
;
515 werr
= regdb_store_regdb_version(db
, REGDB_VERSION_V2
);
518 talloc_free(mem_ctx
);
522 static bool tdb_data_read_uint32(TDB_DATA
*buf
, uint32_t *result
)
524 const size_t len
= sizeof(uint32_t);
525 if (buf
->dsize
>= len
) {
526 *result
= IVAL(buf
->dptr
, 0);
534 static bool tdb_data_read_cstr(TDB_DATA
*buf
, char **result
)
536 const size_t len
= strnlen((char*)buf
->dptr
, buf
->dsize
) + 1;
537 if (buf
->dsize
>= len
) {
538 *result
= (char*)buf
->dptr
;
546 static bool tdb_data_is_cstr(TDB_DATA d
) {
547 if (tdb_data_is_empty(d
) || (d
.dptr
[d
.dsize
-1] != '\0')) {
550 return strlen((char *)d
.dptr
) == (d
.dsize
-1);
553 static bool upgrade_v2_to_v3_check_subkeylist(struct db_context
*db
,
557 static uint32_t zero
= 0;
558 static TDB_DATA empty_subkey_list
= {
559 .dptr
= (unsigned char*)&zero
,
560 .dsize
= sizeof(uint32_t),
562 bool success
= false;
563 char *path
= talloc_asprintf(talloc_tos(), "%s\\%s", key
, subkey
);
564 if (!strupper_m(path
)) {
568 if (!dbwrap_exists(db
, string_term_tdb_data(path
))) {
571 DEBUG(10, ("regdb_upgrade_v2_to_v3: writing subkey list [%s]\n",
574 status
= dbwrap_store_bystring(db
, path
, empty_subkey_list
,
576 if (!NT_STATUS_IS_OK(status
)) {
577 DEBUG(0, ("regdb_upgrade_v2_to_v3: writing subkey list "
578 "[%s] failed\n", path
));
588 static bool upgrade_v2_to_v3_check_parent(struct db_context
*db
,
591 const char *sep
= strrchr_m(key
, '\\');
593 char *pkey
= talloc_strndup(talloc_tos(), key
, sep
-key
);
594 if (!dbwrap_exists(db
, string_term_tdb_data(pkey
))) {
595 DEBUG(0, ("regdb_upgrade_v2_to_v3: missing subkey list "
596 "[%s]\nrun \"net registry check\"\n", pkey
));
604 #define IS_EQUAL(d,s) (((d).dsize == strlen(s)+1) && \
605 (strcmp((char*)(d).dptr, (s)) == 0))
606 #define STARTS_WITH(d,s) (((d).dsize > strlen(s)) && \
607 (strncmp((char*)(d).dptr, (s), strlen(s)) == 0))
608 #define SSTR(d) (int)(d).dsize , (char*)(d).dptr
611 static int regdb_upgrade_v2_to_v3_fn(struct db_record
*rec
, void *private_data
)
613 struct db_context
*db
= (struct db_context
*)private_data
;
614 TDB_DATA key
= dbwrap_record_get_key(rec
);
615 TDB_DATA val
= dbwrap_record_get_value(rec
);
617 if (tdb_data_is_empty(key
)) {
622 DEBUG(0, ("regdb_upgrade_v2_to_v3_fn: ERROR: "
623 "NULL db context handed in via private_data\n"));
627 if (IS_EQUAL(key
, REGDB_VERSION_KEYNAME
) ||
628 STARTS_WITH(key
, REG_VALUE_PREFIX
) ||
629 STARTS_WITH(key
, REG_SECDESC_PREFIX
))
631 DEBUG(10, ("regdb_upgrade_v2_to_v3: skipping [%.*s]\n",
636 if (STARTS_WITH(key
, REG_SORTED_SUBKEYS_PREFIX
)) {
638 /* Delete the deprecated sorted subkeys cache. */
640 DEBUG(10, ("regdb_upgrade_v2_to_v3: deleting [%.*s]\n",
643 status
= dbwrap_record_delete(rec
);
644 if (!NT_STATUS_IS_OK(status
)) {
645 DEBUG(0, ("regdb_upgrade_v2_to_v3: deleting [%.*s] "
646 "failed!\n", SSTR(key
)));
653 if ( tdb_data_is_cstr(key
) &&
654 hive_info((char*)key
.dptr
) != NULL
)
657 * Found a regular subkey list record.
658 * Walk the list and create the list record for those
659 * subkeys that don't already have one.
662 char *subkey
, *path
= (char*)key
.dptr
;
663 uint32_t num_items
, found_items
= 0;
666 DEBUG(10, ("regdb_upgrade_v2_to_v3: scanning subkeylist of "
669 if (!tdb_data_read_uint32(&pos
, &num_items
)) {
670 /* invalid or empty - skip */
674 while (tdb_data_read_cstr(&pos
, &subkey
)) {
677 if (!upgrade_v2_to_v3_check_subkeylist(db
, path
, subkey
))
682 if (!upgrade_v2_to_v3_check_parent(db
, path
)) {
686 if (found_items
!= num_items
) {
687 DEBUG(0, ("regdb_upgrade_v2_to_v3: inconsistent subkey "
688 "list [%s]\nrun \"net registry check\"\n",
692 DEBUG(10, ("regdb_upgrade_v2_to_v3: skipping invalid [%.*s]\n"
693 "run \"net registry check\"\n", SSTR(key
)));
699 static WERROR
regdb_upgrade_v2_to_v3(struct db_context
*db
)
704 status
= dbwrap_traverse(db
, regdb_upgrade_v2_to_v3_fn
, db
, NULL
);
705 if (!NT_STATUS_IS_OK(status
)) {
706 werr
= WERR_REG_IO_FAILURE
;
710 werr
= regdb_store_regdb_version(db
, REGDB_VERSION_V3
);
716 /***********************************************************************
717 Open the registry database
718 ***********************************************************************/
720 WERROR
regdb_init(void)
728 DEBUG(10, ("regdb_init: incrementing refcount (%d->%d)\n",
729 regdb_refcount
, regdb_refcount
+1));
734 db_path
= state_path("registry.tdb");
735 if (db_path
== NULL
) {
739 regdb
= db_open(NULL
, db_path
, 0,
740 REG_TDB_FLAGS
, O_RDWR
, 0600,
741 DBWRAP_LOCK_ORDER_1
, REG_DBWRAP_FLAGS
);
743 regdb
= db_open(NULL
, db_path
, 0,
744 REG_TDB_FLAGS
, O_RDWR
|O_CREAT
, 0600,
745 DBWRAP_LOCK_ORDER_1
, REG_DBWRAP_FLAGS
);
747 werr
= ntstatus_to_werror(map_nt_error_from_unix(errno
));
748 DEBUG(1,("regdb_init: Failed to open registry %s (%s)\n",
749 db_path
, strerror(errno
) ));
750 TALLOC_FREE(db_path
);
754 werr
= regdb_store_regdb_version(regdb
, REGDB_CODE_VERSION
);
755 if (!W_ERROR_IS_OK(werr
)) {
756 DEBUG(1, ("regdb_init: Failed to store version: %s\n",
758 TALLOC_FREE(db_path
);
762 DEBUG(10,("regdb_init: Successfully created registry tdb\n"));
764 TALLOC_FREE(db_path
);
767 DEBUG(10, ("regdb_init: registry db openend. refcount reset (%d)\n",
770 status
= dbwrap_fetch_int32_bystring(regdb
, REGDB_VERSION_KEYNAME
,
772 if (!NT_STATUS_IS_OK(status
)) {
773 DEBUG(10, ("regdb_init: registry version uninitialized "
774 "(got %d), initializing to version %d\n",
775 vers_id
, REGDB_VERSION_V1
));
778 * There was a regdb format version prior to version 1
779 * which did not store a INFO/version key. The format
780 * of this version was identical to version 1 except for
781 * the lack of the sorted subkey cache records.
782 * Since these are disposable, we can safely assume version
783 * 1 if no INFO/version key is found and run the db through
784 * the whole chain of upgrade. If the database was not
785 * initialized, this does not harm. If it was the unversioned
786 * version ("0"), then it do the right thing with the records.
788 werr
= regdb_store_regdb_version(regdb
, REGDB_VERSION_V1
);
789 if (!W_ERROR_IS_OK(werr
)) {
792 vers_id
= REGDB_VERSION_V1
;
795 if (vers_id
== REGDB_CODE_VERSION
) {
799 if (vers_id
> REGDB_CODE_VERSION
|| vers_id
== 0) {
800 DEBUG(0, ("regdb_init: unknown registry version %d "
801 "(code version = %d), refusing initialization\n",
802 vers_id
, REGDB_CODE_VERSION
));
803 return WERR_CAN_NOT_COMPLETE
;
806 if (dbwrap_transaction_start(regdb
) != 0) {
807 return WERR_REG_IO_FAILURE
;
810 if (vers_id
== REGDB_VERSION_V1
) {
811 DEBUG(10, ("regdb_init: upgrading registry from version %d "
812 "to %d\n", REGDB_VERSION_V1
, REGDB_VERSION_V2
));
814 werr
= regdb_upgrade_v1_to_v2(regdb
);
815 if (!W_ERROR_IS_OK(werr
)) {
816 dbwrap_transaction_cancel(regdb
);
820 vers_id
= REGDB_VERSION_V2
;
823 if (vers_id
== REGDB_VERSION_V2
) {
824 DEBUG(10, ("regdb_init: upgrading registry from version %d "
825 "to %d\n", REGDB_VERSION_V2
, REGDB_VERSION_V3
));
827 werr
= regdb_upgrade_v2_to_v3(regdb
);
828 if (!W_ERROR_IS_OK(werr
)) {
829 dbwrap_transaction_cancel(regdb
);
833 vers_id
= REGDB_VERSION_V3
;
836 /* future upgrade code should go here */
838 if (dbwrap_transaction_commit(regdb
) != 0) {
839 return WERR_REG_IO_FAILURE
;
845 /***********************************************************************
846 Open the registry. Must already have been initialized by regdb_init()
847 ***********************************************************************/
849 WERROR
regdb_open( void )
851 WERROR result
= WERR_OK
;
856 DEBUG(10, ("regdb_open: incrementing refcount (%d->%d)\n",
857 regdb_refcount
, regdb_refcount
+1));
862 db_path
= state_path("registry.tdb");
863 if (db_path
== NULL
) {
869 regdb
= db_open(NULL
, db_path
, 0,
870 REG_TDB_FLAGS
, O_RDWR
, 0600,
871 DBWRAP_LOCK_ORDER_1
, REG_DBWRAP_FLAGS
);
875 result
= ntstatus_to_werror(map_nt_error_from_unix(saved_errno
));
876 DEBUG(0,("regdb_open: Failed to open %s! (%s)\n",
877 db_path
, strerror(saved_errno
)));
878 TALLOC_FREE(db_path
);
881 TALLOC_FREE(db_path
);
884 DEBUG(10, ("regdb_open: registry db opened. refcount reset (%d)\n",
890 /***********************************************************************
891 ***********************************************************************/
893 int regdb_close( void )
895 if (regdb_refcount
== 0) {
901 DEBUG(10, ("regdb_close: decrementing refcount (%d->%d)\n",
902 regdb_refcount
+1, regdb_refcount
));
904 if ( regdb_refcount
> 0 )
907 SMB_ASSERT( regdb_refcount
>= 0 );
913 WERROR
regdb_transaction_start(void)
915 return (dbwrap_transaction_start(regdb
) == 0) ?
916 WERR_OK
: WERR_REG_IO_FAILURE
;
919 WERROR
regdb_transaction_commit(void)
921 return (dbwrap_transaction_commit(regdb
) == 0) ?
922 WERR_OK
: WERR_REG_IO_FAILURE
;
925 WERROR
regdb_transaction_cancel(void)
927 return (dbwrap_transaction_cancel(regdb
) == 0) ?
928 WERR_OK
: WERR_REG_IO_FAILURE
;
931 /***********************************************************************
932 return the tdb sequence number of the registry tdb.
933 this is an indicator for the content of the registry
934 having changed. it will change upon regdb_init, too, though.
935 ***********************************************************************/
936 int regdb_get_seqnum(void)
938 return dbwrap_get_seqnum(regdb
);
942 static WERROR
regdb_delete_key_with_prefix(struct db_context
*db
,
947 WERROR werr
= WERR_NOMEM
;
948 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
950 if (keyname
== NULL
) {
951 werr
= WERR_INVALID_PARAM
;
955 if (prefix
== NULL
) {
956 path
= discard_const_p(char, keyname
);
958 path
= talloc_asprintf(mem_ctx
, "%s\\%s", prefix
, keyname
);
964 path
= normalize_reg_path(mem_ctx
, path
);
969 werr
= ntstatus_to_werror(dbwrap_purge_bystring(db
, path
));
972 talloc_free(mem_ctx
);
977 static WERROR
regdb_delete_values(struct db_context
*db
, const char *keyname
)
979 return regdb_delete_key_with_prefix(db
, keyname
, REG_VALUE_PREFIX
);
982 static WERROR
regdb_delete_secdesc(struct db_context
*db
, const char *keyname
)
984 return regdb_delete_key_with_prefix(db
, keyname
, REG_SECDESC_PREFIX
);
987 static WERROR
regdb_delete_subkeylist(struct db_context
*db
, const char *keyname
)
989 return regdb_delete_key_with_prefix(db
, keyname
, NULL
);
993 static WERROR
regdb_delete_key_lists(struct db_context
*db
, const char *keyname
)
997 werr
= regdb_delete_values(db
, keyname
);
998 if (!W_ERROR_IS_OK(werr
)) {
999 DEBUG(1, (__location__
" Deleting %s\\%s failed: %s\n",
1000 REG_VALUE_PREFIX
, keyname
, win_errstr(werr
)));
1004 werr
= regdb_delete_secdesc(db
, keyname
);
1005 if (!W_ERROR_IS_OK(werr
)) {
1006 DEBUG(1, (__location__
" Deleting %s\\%s failed: %s\n",
1007 REG_SECDESC_PREFIX
, keyname
, win_errstr(werr
)));
1011 werr
= regdb_delete_subkeylist(db
, keyname
);
1012 if (!W_ERROR_IS_OK(werr
)) {
1013 DEBUG(1, (__location__
" Deleting %s failed: %s\n",
1014 keyname
, win_errstr(werr
)));
1022 /***********************************************************************
1023 Add subkey strings to the registry tdb under a defined key
1024 fmt is the same format as tdb_pack except this function only supports
1026 ***********************************************************************/
1028 static WERROR
regdb_store_keys_internal2(struct db_context
*db
,
1030 struct regsubkey_ctr
*ctr
)
1033 uint8_t *buffer
= NULL
;
1035 uint32_t len
, buflen
;
1036 uint32_t num_subkeys
= regsubkey_ctr_numkeys(ctr
);
1037 char *keyname
= NULL
;
1038 TALLOC_CTX
*ctx
= talloc_stackframe();
1042 werr
= WERR_INVALID_PARAM
;
1046 keyname
= talloc_strdup(ctx
, key
);
1052 keyname
= normalize_reg_path(ctx
, keyname
);
1058 /* allocate some initial memory */
1060 buffer
= (uint8_t *)SMB_MALLOC(1024);
1061 if (buffer
== NULL
) {
1068 /* store the number of subkeys */
1070 len
+= tdb_pack(buffer
+len
, buflen
-len
, "d", num_subkeys
);
1072 /* pack all the strings */
1074 for (i
=0; i
<num_subkeys
; i
++) {
1077 thistime
= tdb_pack(buffer
+len
, buflen
-len
, "f",
1078 regsubkey_ctr_specific_key(ctr
, i
));
1079 if (len
+thistime
> buflen
) {
1082 * tdb_pack hasn't done anything because of the short
1083 * buffer, allocate extra space.
1085 buffer
= SMB_REALLOC_ARRAY(buffer
, uint8_t,
1087 if(buffer
== NULL
) {
1088 DEBUG(0, ("regdb_store_keys: Failed to realloc "
1089 "memory of size [%u]\n",
1090 (unsigned int)(len
+thistime
)*2));
1094 buflen
= (len
+thistime
)*2;
1095 thistime2
= tdb_pack(
1096 buffer
+len
, buflen
-len
, "f",
1097 regsubkey_ctr_specific_key(ctr
, i
));
1098 if (thistime2
!= thistime
) {
1099 DEBUG(0, ("tdb_pack failed\n"));
1100 werr
= WERR_CAN_NOT_COMPLETE
;
1107 /* finally write out the data */
1111 werr
= ntstatus_to_werror(dbwrap_store_bystring(db
, keyname
, dbuf
,
1121 * Utility function to store a new empty list of
1122 * subkeys of given key specified as parent and subkey name
1123 * (thereby creating the key).
1124 * If the parent keyname is NULL, then the "subkey" is
1125 * interpreted as a base key.
1126 * If the subkey list does already exist, it is not modified.
1128 * Must be called from within a transaction.
1130 static WERROR
regdb_store_subkey_list(struct db_context
*db
, const char *parent
,
1135 struct regsubkey_ctr
*subkeys
= NULL
;
1136 TALLOC_CTX
*frame
= talloc_stackframe();
1138 if (parent
== NULL
) {
1139 path
= talloc_strdup(frame
, key
);
1141 path
= talloc_asprintf(frame
, "%s\\%s", parent
, key
);
1148 werr
= regsubkey_ctr_init(frame
, &subkeys
);
1149 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1151 werr
= regdb_fetch_keys_internal(db
, path
, subkeys
);
1152 if (W_ERROR_IS_OK(werr
)) {
1153 /* subkey list exists already - don't modify */
1157 werr
= regsubkey_ctr_reinit(subkeys
);
1158 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1160 /* create a record with 0 subkeys */
1161 werr
= regdb_store_keys_internal2(db
, path
, subkeys
);
1162 if (!W_ERROR_IS_OK(werr
)) {
1163 DEBUG(0, ("regdb_store_keys: Failed to store new record for "
1164 "key [%s]: %s\n", path
, win_errstr(werr
)));
1173 /***********************************************************************
1174 Store the new subkey record and create any child key records that
1175 do not currently exist
1176 ***********************************************************************/
1178 struct regdb_store_keys_context
{
1180 struct regsubkey_ctr
*ctr
;
1183 static NTSTATUS
regdb_store_keys_action(struct db_context
*db
,
1186 struct regdb_store_keys_context
*store_ctx
;
1190 struct regsubkey_ctr
*old_subkeys
= NULL
;
1191 char *oldkeyname
= NULL
;
1192 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
1194 store_ctx
= (struct regdb_store_keys_context
*)private_data
;
1197 * Re-fetch the old keys inside the transaction
1200 werr
= regsubkey_ctr_init(mem_ctx
, &old_subkeys
);
1201 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1203 werr
= regdb_fetch_keys_internal(db
, store_ctx
->key
, old_subkeys
);
1204 if (!W_ERROR_IS_OK(werr
) &&
1205 !W_ERROR_EQUAL(werr
, WERR_NOT_FOUND
))
1211 * Make the store operation as safe as possible without transactions:
1213 * (1) For each subkey removed from ctr compared with old_subkeys:
1215 * (a) First delete the value db entry.
1217 * (b) Next delete the secdesc db record.
1219 * (c) Then delete the subkey list entry.
1221 * (2) Now write the list of subkeys of the parent key,
1222 * deleting removed entries and adding new ones.
1224 * (3) Finally create the subkey list entries for the added keys.
1226 * This way if we crash half-way in between deleting the subkeys
1227 * and storing the parent's list of subkeys, no old data can pop up
1228 * out of the blue when re-adding keys later on.
1231 /* (1) delete removed keys' lists (values/secdesc/subkeys) */
1233 num_subkeys
= regsubkey_ctr_numkeys(old_subkeys
);
1234 for (i
=0; i
<num_subkeys
; i
++) {
1235 oldkeyname
= regsubkey_ctr_specific_key(old_subkeys
, i
);
1237 if (regsubkey_ctr_key_exists(store_ctx
->ctr
, oldkeyname
)) {
1239 * It's still around, don't delete
1244 path
= talloc_asprintf(mem_ctx
, "%s\\%s", store_ctx
->key
,
1251 werr
= regdb_delete_key_lists(db
, path
);
1252 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1257 TALLOC_FREE(old_subkeys
);
1259 /* (2) store the subkey list for the parent */
1261 werr
= regdb_store_keys_internal2(db
, store_ctx
->key
, store_ctx
->ctr
);
1262 if (!W_ERROR_IS_OK(werr
)) {
1263 DEBUG(0,("regdb_store_keys: Failed to store new subkey list "
1264 "for parent [%s]: %s\n", store_ctx
->key
,
1269 /* (3) now create records for any subkeys that don't already exist */
1271 num_subkeys
= regsubkey_ctr_numkeys(store_ctx
->ctr
);
1273 for (i
=0; i
<num_subkeys
; i
++) {
1276 subkey
= regsubkey_ctr_specific_key(store_ctx
->ctr
, i
);
1278 werr
= regdb_store_subkey_list(db
, store_ctx
->key
, subkey
);
1279 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1283 * Update the seqnum in the container to possibly
1284 * prevent next read from going to disk
1286 werr
= regsubkey_ctr_set_seqnum(store_ctx
->ctr
, dbwrap_get_seqnum(db
));
1289 talloc_free(mem_ctx
);
1290 return werror_to_ntstatus(werr
);
1293 static bool regdb_store_keys_internal(struct db_context
*db
, const char *key
,
1294 struct regsubkey_ctr
*ctr
)
1296 int num_subkeys
, old_num_subkeys
, i
;
1297 struct regsubkey_ctr
*old_subkeys
= NULL
;
1298 TALLOC_CTX
*ctx
= talloc_stackframe();
1301 struct regdb_store_keys_context store_ctx
;
1303 if (!regdb_key_exists(db
, key
)) {
1308 * fetch a list of the old subkeys so we can determine if anything has
1312 werr
= regsubkey_ctr_init(ctx
, &old_subkeys
);
1313 if (!W_ERROR_IS_OK(werr
)) {
1314 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
1318 werr
= regdb_fetch_keys_internal(db
, key
, old_subkeys
);
1319 if (!W_ERROR_IS_OK(werr
) &&
1320 !W_ERROR_EQUAL(werr
, WERR_NOT_FOUND
))
1325 num_subkeys
= regsubkey_ctr_numkeys(ctr
);
1326 old_num_subkeys
= regsubkey_ctr_numkeys(old_subkeys
);
1327 if ((num_subkeys
&& old_num_subkeys
) &&
1328 (num_subkeys
== old_num_subkeys
)) {
1330 for (i
= 0; i
< num_subkeys
; i
++) {
1331 if (strcmp(regsubkey_ctr_specific_key(ctr
, i
),
1332 regsubkey_ctr_specific_key(old_subkeys
, i
))
1338 if (i
== num_subkeys
) {
1340 * Nothing changed, no point to even start a tdb
1349 TALLOC_FREE(old_subkeys
);
1351 store_ctx
.key
= key
;
1352 store_ctx
.ctr
= ctr
;
1354 werr
= regdb_trans_do(db
,
1355 regdb_store_keys_action
,
1358 ret
= W_ERROR_IS_OK(werr
);
1366 static bool regdb_store_keys(const char *key
, struct regsubkey_ctr
*ctr
)
1368 return regdb_store_keys_internal(regdb
, key
, ctr
);
1372 * create a subkey of a given key
1375 struct regdb_create_subkey_context
{
1380 static NTSTATUS
regdb_create_subkey_action(struct db_context
*db
,
1384 struct regdb_create_subkey_context
*create_ctx
;
1385 struct regsubkey_ctr
*subkeys
;
1386 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
1388 create_ctx
= (struct regdb_create_subkey_context
*)private_data
;
1390 werr
= regsubkey_ctr_init(mem_ctx
, &subkeys
);
1391 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1393 werr
= regdb_fetch_keys_internal(db
, create_ctx
->key
, subkeys
);
1394 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1396 werr
= regsubkey_ctr_addkey(subkeys
, create_ctx
->subkey
);
1397 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1399 werr
= regdb_store_keys_internal2(db
, create_ctx
->key
, subkeys
);
1400 if (!W_ERROR_IS_OK(werr
)) {
1401 DEBUG(0, (__location__
" failed to store new subkey list for "
1402 "parent key %s: %s\n", create_ctx
->key
,
1406 werr
= regdb_store_subkey_list(db
, create_ctx
->key
, create_ctx
->subkey
);
1409 talloc_free(mem_ctx
);
1410 return werror_to_ntstatus(werr
);
1413 static WERROR
regdb_create_subkey_internal(struct db_context
*db
,
1418 struct regsubkey_ctr
*subkeys
;
1419 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
1420 struct regdb_create_subkey_context create_ctx
;
1422 if (!regdb_key_exists(db
, key
)) {
1423 werr
= WERR_NOT_FOUND
;
1427 werr
= regsubkey_ctr_init(mem_ctx
, &subkeys
);
1428 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1430 werr
= regdb_fetch_keys_internal(db
, key
, subkeys
);
1431 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1433 if (regsubkey_ctr_key_exists(subkeys
, subkey
)) {
1436 newkey
= talloc_asprintf(mem_ctx
, "%s\\%s", key
, subkey
);
1437 if (newkey
== NULL
) {
1442 if (regdb_key_exists(db
, newkey
)) {
1448 talloc_free(subkeys
);
1450 create_ctx
.key
= key
;
1451 create_ctx
.subkey
= subkey
;
1453 werr
= regdb_trans_do(db
,
1454 regdb_create_subkey_action
,
1458 talloc_free(mem_ctx
);
1462 static WERROR
regdb_create_subkey(const char *key
, const char *subkey
)
1464 return regdb_create_subkey_internal(regdb
, key
, subkey
);
1471 struct regdb_create_basekey_context
{
1475 static NTSTATUS
regdb_create_basekey_action(struct db_context
*db
,
1479 struct regdb_create_basekey_context
*create_ctx
;
1481 create_ctx
= (struct regdb_create_basekey_context
*)private_data
;
1483 werr
= regdb_store_subkey_list(db
, NULL
, create_ctx
->key
);
1485 return werror_to_ntstatus(werr
);
1488 static WERROR
regdb_create_basekey(struct db_context
*db
, const char *key
)
1491 struct regdb_create_subkey_context create_ctx
;
1493 create_ctx
.key
= key
;
1495 werr
= regdb_trans_do(db
,
1496 regdb_create_basekey_action
,
1503 * create a subkey of a given key
1506 struct regdb_delete_subkey_context
{
1513 static NTSTATUS
regdb_delete_subkey_action(struct db_context
*db
,
1517 struct regdb_delete_subkey_context
*delete_ctx
;
1518 struct regsubkey_ctr
*subkeys
;
1519 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
1521 delete_ctx
= (struct regdb_delete_subkey_context
*)private_data
;
1523 werr
= regdb_delete_key_lists(db
, delete_ctx
->path
);
1524 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1526 if (delete_ctx
->lazy
) {
1530 werr
= regsubkey_ctr_init(mem_ctx
, &subkeys
);
1531 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1533 werr
= regdb_fetch_keys_internal(db
, delete_ctx
->key
, subkeys
);
1534 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1536 werr
= regsubkey_ctr_delkey(subkeys
, delete_ctx
->subkey
);
1537 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1539 werr
= regdb_store_keys_internal2(db
, delete_ctx
->key
, subkeys
);
1540 if (!W_ERROR_IS_OK(werr
)) {
1541 DEBUG(0, (__location__
" failed to store new subkey_list for "
1542 "parent key %s: %s\n", delete_ctx
->key
,
1547 talloc_free(mem_ctx
);
1548 return werror_to_ntstatus(werr
);
1551 static WERROR
regdb_delete_subkey(const char *key
, const char *subkey
, bool lazy
)
1555 struct regdb_delete_subkey_context delete_ctx
;
1556 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
1558 if (!regdb_key_exists(regdb
, key
)) {
1559 werr
= WERR_NOT_FOUND
;
1563 path
= talloc_asprintf(mem_ctx
, "%s\\%s", key
, subkey
);
1569 if (!regdb_key_exists(regdb
, path
)) {
1574 delete_ctx
.key
= key
;
1575 delete_ctx
.subkey
= subkey
;
1576 delete_ctx
.path
= path
;
1577 delete_ctx
.lazy
= lazy
;
1579 werr
= regdb_trans_do(regdb
,
1580 regdb_delete_subkey_action
,
1584 talloc_free(mem_ctx
);
1588 static TDB_DATA
regdb_fetch_key_internal(struct db_context
*db
,
1589 TALLOC_CTX
*mem_ctx
, const char *key
)
1595 path
= normalize_reg_path(mem_ctx
, key
);
1597 return make_tdb_data(NULL
, 0);
1600 status
= dbwrap_fetch_bystring(db
, mem_ctx
, path
, &data
);
1601 if (!NT_STATUS_IS_OK(status
)) {
1611 * Check for the existence of a key.
1613 * Existence of a key is authoritatively defined by
1614 * the existence of the record that contains the list
1617 * Return false, if the record does not match the correct
1618 * structure of an initial 4-byte counter and then a
1619 * list of the corresponding number of zero-terminated
1622 static bool regdb_key_exists(struct db_context
*db
, const char *key
)
1624 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
1630 uint32_t num_items
, i
;
1637 path
= normalize_reg_path(mem_ctx
, key
);
1639 DEBUG(0, ("out of memory! (talloc failed)\n"));
1643 if (*path
== '\0') {
1647 value
= regdb_fetch_key_internal(db
, mem_ctx
, path
);
1648 if (value
.dptr
== NULL
) {
1652 if (value
.dsize
== 0) {
1653 DEBUG(10, ("regdb_key_exists: subkeylist-record for key "
1654 "[%s] is empty: Could be a deleted record in a "
1655 "clustered (ctdb) environment?\n",
1660 len
= tdb_unpack(value
.dptr
, value
.dsize
, "d", &num_items
);
1661 if (len
== (int32_t)-1) {
1662 DEBUG(1, ("regdb_key_exists: ERROR: subkeylist-record for key "
1663 "[%s] is invalid: Could not parse initial 4-byte "
1664 "counter. record data length is %u.\n",
1665 path
, (unsigned int)value
.dsize
));
1670 * Note: the tdb_unpack check above implies that len <= value.dsize
1672 buflen
= value
.dsize
- len
;
1673 buf
= (const char *)value
.dptr
+ len
;
1677 for (i
= 0; i
< num_items
; i
++) {
1681 len
= strnlen(buf
, buflen
) + 1;
1683 DEBUG(1, ("regdb_key_exists: ERROR: subkeylist-record "
1684 "for key [%s] is corrupt: %u items expected, "
1685 "item number %u is not zero terminated.\n",
1686 path
, num_items
, i
+1));
1695 DEBUG(1, ("regdb_key_exists: ERROR: subkeylist-record for key "
1696 "[%s] is corrupt: %u items expected and found, but "
1697 "the record contains additional %u bytes\n",
1698 path
, num_items
, buflen
));
1702 if (i
< num_items
) {
1703 DEBUG(1, ("regdb_key_exists: ERROR: subkeylist-record for key "
1704 "[%s] is corrupt: %u items expected, but only %u "
1706 path
, num_items
, i
+1));
1713 TALLOC_FREE(mem_ctx
);
1718 /***********************************************************************
1719 Retrieve an array of strings containing subkeys. Memory should be
1720 released by the caller.
1721 ***********************************************************************/
1723 static WERROR
regdb_fetch_keys_internal(struct db_context
*db
, const char *key
,
1724 struct regsubkey_ctr
*ctr
)
1729 uint32_t buflen
, len
;
1732 TALLOC_CTX
*frame
= talloc_stackframe();
1734 int seqnum
[2], count
;
1736 DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key
? key
: "NULL"));
1738 if (!regdb_key_exists(db
, key
)) {
1739 DEBUG(10, ("key [%s] not found\n", key
));
1740 werr
= WERR_NOT_FOUND
;
1744 werr
= regsubkey_ctr_reinit(ctr
);
1745 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1749 seqnum
[0] = dbwrap_get_seqnum(db
);
1753 TALLOC_FREE(value
.dptr
);
1754 value
= regdb_fetch_key_internal(db
, frame
, key
);
1755 seqnum
[count
% 2] = dbwrap_get_seqnum(db
);
1757 } while (seqnum
[0] != seqnum
[1]);
1760 DEBUG(5, ("regdb_fetch_keys_internal: it took %d attempts to "
1761 "fetch key '%s' with constant seqnum\n",
1765 werr
= regsubkey_ctr_set_seqnum(ctr
, seqnum
[0]);
1766 if (!W_ERROR_IS_OK(werr
)) {
1770 if (value
.dsize
== 0 || value
.dptr
== NULL
) {
1771 DEBUG(10, ("regdb_fetch_keys: no subkeys found for key [%s]\n",
1777 buflen
= value
.dsize
;
1778 len
= tdb_unpack( buf
, buflen
, "d", &num_items
);
1779 if (len
== (uint32_t)-1) {
1780 werr
= WERR_NOT_FOUND
;
1784 for (i
=0; i
<num_items
; i
++) {
1785 len
+= tdb_unpack(buf
+len
, buflen
-len
, "f", subkeyname
);
1786 werr
= regsubkey_ctr_addkey(ctr
, subkeyname
);
1787 if (!W_ERROR_IS_OK(werr
)) {
1788 DEBUG(5, ("regdb_fetch_keys: regsubkey_ctr_addkey "
1789 "failed: %s\n", win_errstr(werr
)));
1795 DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items
));
1802 static int regdb_fetch_keys(const char *key
, struct regsubkey_ctr
*ctr
)
1806 werr
= regdb_fetch_keys_internal(regdb
, key
, ctr
);
1807 if (!W_ERROR_IS_OK(werr
)) {
1811 return regsubkey_ctr_numkeys(ctr
);
1814 /****************************************************************************
1815 Unpack a list of registry values frem the TDB
1816 ***************************************************************************/
1818 static int regdb_unpack_values(struct regval_ctr
*values
, uint8_t *buf
, int buflen
)
1825 uint32_t num_values
= 0;
1828 /* loop and unpack the rest of the registry values */
1830 len
+= tdb_unpack(buf
+len
, buflen
-len
, "d", &num_values
);
1832 for ( i
=0; i
<num_values
; i
++ ) {
1833 /* unpack the next regval */
1838 valuename
[0] = '\0';
1839 len
+= tdb_unpack(buf
+len
, buflen
-len
, "fdB",
1845 regval_ctr_addvalue(values
, valuename
, type
,
1846 (uint8_t *)data_p
, size
);
1847 SAFE_FREE(data_p
); /* 'B' option to tdb_unpack does a malloc() */
1849 DEBUG(10, ("regdb_unpack_values: value[%d]: name[%s] len[%d]\n",
1850 i
, valuename
, size
));
1856 /****************************************************************************
1857 Pack all values in all printer keys
1858 ***************************************************************************/
1860 static int regdb_pack_values(struct regval_ctr
*values
, uint8_t *buf
, int buflen
)
1864 struct regval_blob
*val
;
1870 num_values
= regval_ctr_numvals( values
);
1872 /* pack the number of values first */
1874 len
+= tdb_pack( buf
+len
, buflen
-len
, "d", num_values
);
1876 /* loop over all values */
1878 for ( i
=0; i
<num_values
; i
++ ) {
1879 val
= regval_ctr_specific_value( values
, i
);
1880 len
+= tdb_pack(buf
+len
, buflen
-len
, "fdB",
1884 regval_data_p(val
) );
1890 /***********************************************************************
1891 Retrieve an array of strings containing subkeys. Memory should be
1892 released by the caller.
1893 ***********************************************************************/
1895 static int regdb_fetch_values_internal(struct db_context
*db
, const char* key
,
1896 struct regval_ctr
*values
)
1898 char *keystr
= NULL
;
1899 TALLOC_CTX
*ctx
= talloc_stackframe();
1903 int seqnum
[2], count
;
1905 DEBUG(10,("regdb_fetch_values: Looking for values of key [%s]\n", key
));
1907 if (!regdb_key_exists(db
, key
)) {
1908 DEBUG(10, ("regb_fetch_values: key [%s] does not exist\n",
1914 keystr
= talloc_asprintf(ctx
, "%s\\%s", REG_VALUE_PREFIX
, key
);
1921 seqnum
[0] = dbwrap_get_seqnum(db
);
1925 TALLOC_FREE(value
.dptr
);
1926 value
= regdb_fetch_key_internal(db
, ctx
, keystr
);
1927 seqnum
[count
% 2] = dbwrap_get_seqnum(db
);
1928 } while (seqnum
[0] != seqnum
[1]);
1931 DEBUG(5, ("regdb_fetch_values_internal: it took %d attempts "
1932 "to fetch key '%s' with constant seqnum\n",
1936 werr
= regval_ctr_set_seqnum(values
, seqnum
[0]);
1937 if (!W_ERROR_IS_OK(werr
)) {
1942 /* all keys have zero values by default */
1946 regdb_unpack_values(values
, value
.dptr
, value
.dsize
);
1947 ret
= regval_ctr_numvals(values
);
1954 static int regdb_fetch_values(const char* key
, struct regval_ctr
*values
)
1956 return regdb_fetch_values_internal(regdb
, key
, values
);
1959 static NTSTATUS
regdb_store_values_internal(struct db_context
*db
,
1961 struct regval_ctr
*values
)
1963 TDB_DATA old_data
, data
;
1964 char *keystr
= NULL
;
1965 TALLOC_CTX
*ctx
= talloc_stackframe();
1970 DEBUG(10,("regdb_store_values: Looking for values of key [%s]\n", key
));
1972 if (!regdb_key_exists(db
, key
)) {
1973 status
= NT_STATUS_NOT_FOUND
;
1977 if (regval_ctr_numvals(values
) == 0) {
1978 werr
= regdb_delete_values(db
, key
);
1979 if (!W_ERROR_IS_OK(werr
)) {
1980 status
= werror_to_ntstatus(werr
);
1985 * update the seqnum in the cache to prevent the next read
1986 * from going to disk
1988 werr
= regval_ctr_set_seqnum(values
, dbwrap_get_seqnum(db
));
1989 status
= werror_to_ntstatus(werr
);
1995 len
= regdb_pack_values(values
, data
.dptr
, data
.dsize
);
1997 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
1998 status
= NT_STATUS_UNSUCCESSFUL
;
2002 data
.dptr
= talloc_array(ctx
, uint8_t, len
);
2005 len
= regdb_pack_values(values
, data
.dptr
, data
.dsize
);
2007 SMB_ASSERT( len
== data
.dsize
);
2009 keystr
= talloc_asprintf(ctx
, "%s\\%s", REG_VALUE_PREFIX
, key
);
2011 status
= NT_STATUS_NO_MEMORY
;
2014 keystr
= normalize_reg_path(ctx
, keystr
);
2016 status
= NT_STATUS_NO_MEMORY
;
2020 status
= dbwrap_fetch_bystring(db
, ctx
, keystr
, &old_data
);
2022 if (NT_STATUS_IS_OK(status
)
2023 && (old_data
.dptr
!= NULL
)
2024 && (old_data
.dsize
== data
.dsize
)
2025 && (memcmp(old_data
.dptr
, data
.dptr
, data
.dsize
) == 0))
2027 status
= NT_STATUS_OK
;
2031 status
= dbwrap_trans_store_bystring(db
, keystr
, data
, TDB_REPLACE
);
2032 if (!NT_STATUS_IS_OK(status
)) {
2033 DEBUG(0, ("regdb_store_values_internal: error storing: %s\n", nt_errstr(status
)));
2038 * update the seqnum in the cache to prevent the next read
2039 * from going to disk
2041 werr
= regval_ctr_set_seqnum(values
, dbwrap_get_seqnum(db
));
2042 status
= werror_to_ntstatus(werr
);
2049 struct regdb_store_values_ctx
{
2051 struct regval_ctr
*values
;
2054 static NTSTATUS
regdb_store_values_action(struct db_context
*db
,
2058 struct regdb_store_values_ctx
*ctx
=
2059 (struct regdb_store_values_ctx
*)private_data
;
2061 status
= regdb_store_values_internal(db
, ctx
->key
, ctx
->values
);
2066 static bool regdb_store_values(const char *key
, struct regval_ctr
*values
)
2069 struct regdb_store_values_ctx ctx
;
2072 ctx
.values
= values
;
2074 werr
= regdb_trans_do(regdb
, regdb_store_values_action
, &ctx
);
2076 return W_ERROR_IS_OK(werr
);
2079 static WERROR
regdb_get_secdesc(TALLOC_CTX
*mem_ctx
, const char *key
,
2080 struct security_descriptor
**psecdesc
)
2085 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
2086 WERROR err
= WERR_OK
;
2088 DEBUG(10, ("regdb_get_secdesc: Getting secdesc of key [%s]\n", key
));
2090 if (!regdb_key_exists(regdb
, key
)) {
2095 tdbkey
= talloc_asprintf(tmp_ctx
, "%s\\%s", REG_SECDESC_PREFIX
, key
);
2096 if (tdbkey
== NULL
) {
2101 tdbkey
= normalize_reg_path(tmp_ctx
, tdbkey
);
2102 if (tdbkey
== NULL
) {
2107 status
= dbwrap_fetch_bystring(regdb
, tmp_ctx
, tdbkey
, &data
);
2108 if (!NT_STATUS_IS_OK(status
)) {
2113 status
= unmarshall_sec_desc(mem_ctx
, (uint8_t *)data
.dptr
, data
.dsize
,
2116 if (NT_STATUS_EQUAL(status
, NT_STATUS_NO_MEMORY
)) {
2118 } else if (!NT_STATUS_IS_OK(status
)) {
2119 err
= WERR_REG_CORRUPT
;
2123 TALLOC_FREE(tmp_ctx
);
2127 struct regdb_set_secdesc_ctx
{
2129 struct security_descriptor
*secdesc
;
2132 static NTSTATUS
regdb_set_secdesc_action(struct db_context
*db
,
2138 struct regdb_set_secdesc_ctx
*ctx
=
2139 (struct regdb_set_secdesc_ctx
*)private_data
;
2140 TALLOC_CTX
*frame
= talloc_stackframe();
2142 tdbkey
= talloc_asprintf(frame
, "%s\\%s", REG_SECDESC_PREFIX
, ctx
->key
);
2143 if (tdbkey
== NULL
) {
2144 status
= NT_STATUS_NO_MEMORY
;
2148 tdbkey
= normalize_reg_path(frame
, tdbkey
);
2149 if (tdbkey
== NULL
) {
2150 status
= NT_STATUS_NO_MEMORY
;
2154 if (ctx
->secdesc
== NULL
) {
2155 /* assuming a delete */
2156 status
= dbwrap_delete_bystring(db
, tdbkey
);
2160 status
= marshall_sec_desc(frame
, ctx
->secdesc
, &tdbdata
.dptr
,
2162 if (!NT_STATUS_IS_OK(status
)) {
2166 status
= dbwrap_store_bystring(db
, tdbkey
, tdbdata
, 0);
2173 static WERROR
regdb_set_secdesc(const char *key
,
2174 struct security_descriptor
*secdesc
)
2177 struct regdb_set_secdesc_ctx ctx
;
2179 if (!regdb_key_exists(regdb
, key
)) {
2185 ctx
.secdesc
= secdesc
;
2187 err
= regdb_trans_do(regdb
, regdb_set_secdesc_action
, &ctx
);
2193 static bool regdb_subkeys_need_update(struct regsubkey_ctr
*subkeys
)
2195 return (regdb_get_seqnum() != regsubkey_ctr_get_seqnum(subkeys
));
2198 static bool regdb_values_need_update(struct regval_ctr
*values
)
2200 return (regdb_get_seqnum() != regval_ctr_get_seqnum(values
));
2204 * Table of function pointers for default access
2207 struct registry_ops regdb_ops
= {
2208 .fetch_subkeys
= regdb_fetch_keys
,
2209 .fetch_values
= regdb_fetch_values
,
2210 .store_subkeys
= regdb_store_keys
,
2211 .store_values
= regdb_store_values
,
2212 .create_subkey
= regdb_create_subkey
,
2213 .delete_subkey
= regdb_delete_subkey
,
2214 .get_secdesc
= regdb_get_secdesc
,
2215 .set_secdesc
= regdb_set_secdesc
,
2216 .subkeys_need_update
= regdb_subkeys_need_update
,
2217 .values_need_update
= regdb_values_need_update