2 * Unix SMB/CIFS implementation.
3 * Virtual Windows Registry Layer
4 * Copyright (C) Gerald Carter 2002-2005
5 * Copyright (C) Michael Adam 2007-2011
6 * Copyright (C) Gregor Beck 2011
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 /* Implementation of internal registry database functions. */
25 #include "system/filesys.h"
28 #include "reg_util_internal.h"
29 #include "reg_backend_db.h"
30 #include "reg_objects.h"
31 #include "nt_printing.h"
33 #include "dbwrap/dbwrap.h"
34 #include "dbwrap/dbwrap_open.h"
35 #include "../libcli/security/secdesc.h"
38 #define DBGC_CLASS DBGC_REGISTRY
40 #define REGDB_VERSION_KEYNAME "INFO/version"
42 static struct db_context
*regdb
= NULL
;
43 static int regdb_refcount
;
45 static bool regdb_key_exists(struct db_context
*db
, const char *key
);
46 static WERROR
regdb_fetch_keys_internal(struct db_context
*db
, const char *key
,
47 struct regsubkey_ctr
*ctr
);
48 static bool regdb_store_keys_internal(struct db_context
*db
, const char *key
,
49 struct regsubkey_ctr
*ctr
);
50 static int regdb_fetch_values_internal(struct db_context
*db
, const char* key
,
51 struct regval_ctr
*values
);
52 static NTSTATUS
regdb_store_values_internal(struct db_context
*db
, const char *key
,
53 struct regval_ctr
*values
);
54 static WERROR
regdb_store_subkey_list(struct db_context
*db
, const char *parent
,
57 static WERROR
regdb_create_basekey(struct db_context
*db
, const char *key
);
58 static WERROR
regdb_create_subkey_internal(struct db_context
*db
,
63 struct regdb_trans_ctx
{
64 NTSTATUS (*action
)(struct db_context
*, void *);
68 static NTSTATUS
regdb_trans_do_action(struct db_context
*db
, void *private_data
)
72 struct regdb_trans_ctx
*ctx
= (struct regdb_trans_ctx
*)private_data
;
74 status
= dbwrap_fetch_int32(db
, REGDB_VERSION_KEYNAME
, &version_id
);
76 if (!NT_STATUS_IS_OK(status
)) {
77 DEBUG(0, ("ERROR: could not fetch registry db version: %s. "
78 "Denying access.\n", nt_errstr(status
)));
79 return NT_STATUS_ACCESS_DENIED
;
82 if (version_id
!= REGDB_CODE_VERSION
) {
83 DEBUG(0, ("ERROR: changed registry version %d found while "
84 "trying to write to the registry. Version %d "
85 "expected. Denying access.\n",
86 version_id
, REGDB_CODE_VERSION
));
87 return NT_STATUS_ACCESS_DENIED
;
90 status
= ctx
->action(db
, ctx
->private_data
);
94 static WERROR
regdb_trans_do(struct db_context
*db
,
95 NTSTATUS (*action
)(struct db_context
*, void *),
99 struct regdb_trans_ctx ctx
;
103 ctx
.private_data
= private_data
;
105 status
= dbwrap_trans_do(db
, regdb_trans_do_action
, &ctx
);
107 return ntstatus_to_werror(status
);
110 /* List the deepest path into the registry. All part components will be created.*/
112 /* If you want to have a part of the path controlled by the tdb and part by
113 a virtual registry db (e.g. printing), then you have to list the deepest path.
114 For example,"HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion/Print"
115 allows the reg_db backend to handle everything up to
116 "HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion" and then we'll hook
117 the reg_printing backend onto the last component of the path (see
118 KEY_PRINTING_2K in include/rpc_reg.h) --jerry */
120 static const char *builtin_registry_paths
[] = {
124 KEY_PRINTING
"\\Forms",
125 KEY_PRINTING
"\\Printers",
126 KEY_PRINTING
"\\Environments\\Windows NT x86\\Print Processors\\winprint",
133 KEY_SAMBA_GROUP_POLICY
,
134 KEY_GP_MACHINE_POLICY
,
135 KEY_GP_MACHINE_WIN_POLICY
,
138 KEY_GP_USER_WIN_POLICY
,
139 "HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon\\GPExtensions",
140 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Print\\Monitors",
142 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\DefaultUserConfiguration",
151 struct builtin_regkey_value
{
153 const char *valuename
;
161 static struct builtin_regkey_value builtin_registry_values
[] = {
162 { KEY_PRINTING_PORTS
,
163 SAMBA_PRINTER_PORT_NAME
, REG_SZ
, { "" } },
165 "DefaultSpoolDirectory", REG_SZ
, { "C:\\Windows\\System32\\Spool\\Printers" } },
167 "DisplayName", REG_SZ
, { "Event Log" } },
169 "ErrorControl", REG_DWORD
, { (char*)0x00000001 } },
170 { NULL
, NULL
, 0, { NULL
} }
173 static WERROR
create_key_recursive(struct db_context
*db
,
180 if (subkey
== NULL
) {
181 return WERR_INVALID_PARAM
;
185 return regdb_create_basekey(db
, subkey
);
188 p
= strrchr_m(path
, '\\');
191 werr
= create_key_recursive(db
, NULL
, path
);
194 werr
= create_key_recursive(db
, path
, p
+1);
198 if (!W_ERROR_IS_OK(werr
)) {
202 werr
= regdb_create_subkey_internal(db
, path
, subkey
);
209 * Initialize a key in the registry:
210 * create each component key of the specified path.
212 static WERROR
init_registry_key_internal(struct db_context
*db
,
213 const char *add_path
)
217 TALLOC_CTX
*frame
= talloc_stackframe();
219 if (add_path
== NULL
) {
220 werr
= WERR_INVALID_PARAM
;
224 key
= talloc_strdup(frame
, add_path
);
226 subkey
= strrchr_m(key
, '\\');
227 if (subkey
== NULL
) {
235 werr
= create_key_recursive(db
, key
, subkey
);
242 struct init_registry_key_context
{
243 const char *add_path
;
246 static NTSTATUS
init_registry_key_action(struct db_context
*db
,
249 struct init_registry_key_context
*init_ctx
=
250 (struct init_registry_key_context
*)private_data
;
252 return werror_to_ntstatus(init_registry_key_internal(
253 db
, init_ctx
->add_path
));
257 * Initialize a key in the registry:
258 * create each component key of the specified path,
259 * wrapped in one db transaction.
261 WERROR
init_registry_key(const char *add_path
)
263 struct init_registry_key_context init_ctx
;
265 if (regdb_key_exists(regdb
, add_path
)) {
269 init_ctx
.add_path
= add_path
;
271 return regdb_trans_do(regdb
,
272 init_registry_key_action
,
276 /***********************************************************************
277 Open the registry data in the tdb
278 ***********************************************************************/
280 static void regdb_ctr_add_value(struct regval_ctr
*ctr
,
281 struct builtin_regkey_value
*value
)
283 switch(value
->type
) {
285 regval_ctr_addvalue(ctr
, value
->valuename
, REG_DWORD
,
286 (uint8_t *)&value
->data
.dw_value
,
291 regval_ctr_addvalue_sz(ctr
, value
->valuename
,
296 DEBUG(0, ("regdb_ctr_add_value: invalid value type in "
297 "registry values [%d]\n", value
->type
));
301 static NTSTATUS
init_registry_data_action(struct db_context
*db
,
305 TALLOC_CTX
*frame
= talloc_stackframe();
306 struct regval_ctr
*values
;
309 /* loop over all of the predefined paths and add each component */
311 for (i
=0; builtin_registry_paths
[i
] != NULL
; i
++) {
312 if (regdb_key_exists(db
, builtin_registry_paths
[i
])) {
315 status
= werror_to_ntstatus(init_registry_key_internal(db
,
316 builtin_registry_paths
[i
]));
317 if (!NT_STATUS_IS_OK(status
)) {
322 /* loop over all of the predefined values and add each component */
324 for (i
=0; builtin_registry_values
[i
].path
!= NULL
; i
++) {
327 werr
= regval_ctr_init(frame
, &values
);
328 if (!W_ERROR_IS_OK(werr
)) {
329 status
= werror_to_ntstatus(werr
);
333 regdb_fetch_values_internal(db
,
334 builtin_registry_values
[i
].path
,
337 /* preserve existing values across restarts. Only add new ones */
339 if (!regval_ctr_key_exists(values
,
340 builtin_registry_values
[i
].valuename
))
342 regdb_ctr_add_value(values
,
343 &builtin_registry_values
[i
]);
344 status
= regdb_store_values_internal(db
,
345 builtin_registry_values
[i
].path
,
347 if (!NT_STATUS_IS_OK(status
)) {
354 status
= NT_STATUS_OK
;
362 WERROR
init_registry_data(void)
365 TALLOC_CTX
*frame
= talloc_stackframe();
366 struct regval_ctr
*values
;
370 * First, check for the existence of the needed keys and values.
371 * If all do already exist, we can save the writes.
373 for (i
=0; builtin_registry_paths
[i
] != NULL
; i
++) {
374 if (!regdb_key_exists(regdb
, builtin_registry_paths
[i
])) {
379 for (i
=0; builtin_registry_values
[i
].path
!= NULL
; i
++) {
380 werr
= regval_ctr_init(frame
, &values
);
381 W_ERROR_NOT_OK_GOTO_DONE(werr
);
383 regdb_fetch_values_internal(regdb
,
384 builtin_registry_values
[i
].path
,
386 if (!regval_ctr_key_exists(values
,
387 builtin_registry_values
[i
].valuename
))
402 * There are potentially quite a few store operations which are all
403 * indiviually wrapped in tdb transactions. Wrapping them in a single
404 * transaction gives just a single transaction_commit() to actually do
405 * its fsync()s. See tdb/common/transaction.c for info about nested
406 * transaction behaviour.
409 werr
= regdb_trans_do(regdb
,
410 init_registry_data_action
,
418 static int regdb_normalize_keynames_fn(struct db_record
*rec
,
421 TALLOC_CTX
*mem_ctx
= talloc_tos();
426 struct db_context
*db
= (struct db_context
*)private_data
;
428 key
= dbwrap_record_get_key(rec
);
429 if (key
.dptr
== NULL
|| key
.dsize
== 0) {
433 value
= dbwrap_record_get_value(rec
);
436 DEBUG(0, ("regdb_normalize_keynames_fn: ERROR: "
437 "NULL db context handed in via private_data\n"));
441 if (strncmp((const char *)key
.dptr
, REGDB_VERSION_KEYNAME
,
442 strlen(REGDB_VERSION_KEYNAME
)) == 0)
447 keyname
= strchr((const char *)key
.dptr
, '/');
449 keyname
= talloc_string_sub(mem_ctx
,
450 (const char *)key
.dptr
,
454 DEBUG(2, ("regdb_normalize_keynames_fn: Convert %s to %s\n",
455 (const char *)key
.dptr
,
458 /* Delete the original record and store the normalized key */
459 status
= dbwrap_record_delete(rec
);
460 if (!NT_STATUS_IS_OK(status
)) {
461 DEBUG(0,("regdb_normalize_keynames_fn: "
462 "tdb_delete for [%s] failed!\n",
463 (const char *)key
.dptr
));
467 status
= dbwrap_store_bystring(db
, keyname
, value
, TDB_REPLACE
);
468 if (!NT_STATUS_IS_OK(status
)) {
469 DEBUG(0,("regdb_normalize_keynames_fn: "
470 "failed to store new record for [%s]!\n",
479 static WERROR
regdb_store_regdb_version(struct db_context
*db
, uint32_t version
)
483 return WERR_CAN_NOT_COMPLETE
;
486 status
= dbwrap_trans_store_int32(db
, REGDB_VERSION_KEYNAME
, version
);
487 if (!NT_STATUS_IS_OK(status
)) {
488 DEBUG(1, ("regdb_store_regdb_version: error storing %s = %d: %s\n",
489 REGDB_VERSION_KEYNAME
, version
, nt_errstr(status
)));
490 return ntstatus_to_werror(status
);
492 DEBUG(10, ("regdb_store_regdb_version: stored %s = %d\n",
493 REGDB_VERSION_KEYNAME
, version
));
498 static WERROR
regdb_upgrade_v1_to_v2(struct db_context
*db
)
504 mem_ctx
= talloc_stackframe();
506 status
= dbwrap_traverse(db
, regdb_normalize_keynames_fn
, db
, NULL
);
507 if (!NT_STATUS_IS_OK(status
)) {
508 werr
= WERR_REG_IO_FAILURE
;
512 werr
= regdb_store_regdb_version(db
, REGDB_VERSION_V2
);
515 talloc_free(mem_ctx
);
519 static int regdb_upgrade_v2_to_v3_fn(struct db_record
*rec
, void *private_data
)
526 uint32_t buflen
, len
;
531 struct db_context
*db
= (struct db_context
*)private_data
;
533 key
= dbwrap_record_get_key(rec
);
534 if (key
.dptr
== NULL
|| key
.dsize
== 0) {
539 DEBUG(0, ("regdb_upgrade_v2_to_v3_fn: ERROR: "
540 "NULL db context handed in via private_data\n"));
544 keyname
= (const char *)key
.dptr
;
546 if (strncmp(keyname
, REGDB_VERSION_KEYNAME
,
547 strlen(REGDB_VERSION_KEYNAME
)) == 0)
552 if (strncmp(keyname
, REG_SORTED_SUBKEYS_PREFIX
,
553 strlen(REG_SORTED_SUBKEYS_PREFIX
)) == 0)
555 /* Delete the deprecated sorted subkeys cache. */
557 DEBUG(10, ("regdb_upgrade_v2_to_v3: deleting [%s]\n", keyname
));
559 status
= dbwrap_record_delete(rec
);
560 if (!NT_STATUS_IS_OK(status
)) {
561 DEBUG(0, ("regdb_upgrade_v2_to_v3: tdb_delete for [%s] "
562 "failed!\n", keyname
));
569 if (strncmp(keyname
, REG_VALUE_PREFIX
, strlen(REG_VALUE_PREFIX
)) == 0) {
570 DEBUG(10, ("regdb_upgrade_v2_to_v3: skipping [%s]\n", keyname
));
574 if (strncmp(keyname
, REG_SECDESC_PREFIX
,
575 strlen(REG_SECDESC_PREFIX
)) == 0)
577 DEBUG(10, ("regdb_upgrade_v2_to_v3: skipping [%s]\n", keyname
));
582 * Found a regular subkey list record.
583 * Walk the list and create the list record for those
584 * subkeys that don't already have one.
586 DEBUG(10, ("regdb_upgrade_v2_to_v3: scanning subkey list of [%s]\n",
589 value
= dbwrap_record_get_value(rec
);
591 buflen
= value
.dsize
;
593 len
= tdb_unpack(buf
, buflen
, "d", &num_items
);
594 if (len
== (uint32_t)-1) {
595 /* invalid or empty - skip */
599 for (i
=0; i
<num_items
; i
++) {
600 len
+= tdb_unpack(buf
+len
, buflen
-len
, "f", subkeyname
);
601 DEBUG(10, ("regdb_upgrade_v2_to_v3: "
602 "writing subkey list for [%s\\%s]\n",
603 keyname
, subkeyname
));
604 werr
= regdb_store_subkey_list(db
, keyname
, subkeyname
);
605 if (!W_ERROR_IS_OK(werr
)) {
613 static WERROR
regdb_upgrade_v2_to_v3(struct db_context
*db
)
618 status
= dbwrap_traverse(db
, regdb_upgrade_v2_to_v3_fn
, db
, NULL
);
619 if (!NT_STATUS_IS_OK(status
)) {
620 werr
= WERR_REG_IO_FAILURE
;
624 werr
= regdb_store_regdb_version(db
, REGDB_VERSION_V3
);
630 /***********************************************************************
631 Open the registry database
632 ***********************************************************************/
634 WERROR
regdb_init(void)
641 DEBUG(10, ("regdb_init: incrementing refcount (%d->%d)\n",
642 regdb_refcount
, regdb_refcount
+1));
647 regdb
= db_open(NULL
, state_path("registry.tdb"), 0,
648 REG_TDB_FLAGS
, O_RDWR
, 0600);
650 regdb
= db_open(NULL
, state_path("registry.tdb"), 0,
651 REG_TDB_FLAGS
, O_RDWR
|O_CREAT
, 0600);
653 werr
= ntstatus_to_werror(map_nt_error_from_unix(errno
));
654 DEBUG(1,("regdb_init: Failed to open registry %s (%s)\n",
655 state_path("registry.tdb"), strerror(errno
) ));
659 DEBUG(10,("regdb_init: Successfully created registry tdb\n"));
663 DEBUG(10, ("regdb_init: registry db openend. refcount reset (%d)\n",
666 status
= dbwrap_fetch_int32(regdb
, REGDB_VERSION_KEYNAME
, &vers_id
);
667 if (!NT_STATUS_IS_OK(status
)) {
668 DEBUG(10, ("regdb_init: registry version uninitialized "
669 "(got %d), initializing to version %d\n",
670 vers_id
, REGDB_VERSION_V1
));
673 * There was a regdb format version prior to version 1
674 * which did not store a INFO/version key. The format
675 * of this version was identical to version 1 except for
676 * the lack of the sorted subkey cache records.
677 * Since these are disposable, we can safely assume version
678 * 1 if no INFO/version key is found and run the db through
679 * the whole chain of upgrade. If the database was not
680 * initialized, this does not harm. If it was the unversioned
681 * version ("0"), then it do the right thing with the records.
683 werr
= regdb_store_regdb_version(regdb
, REGDB_VERSION_V1
);
684 if (!W_ERROR_IS_OK(werr
)) {
687 vers_id
= REGDB_VERSION_V1
;
690 if (vers_id
== REGDB_CODE_VERSION
) {
694 if (vers_id
> REGDB_CODE_VERSION
|| vers_id
== 0) {
695 DEBUG(0, ("regdb_init: unknown registry version %d "
696 "(code version = %d), refusing initialization\n",
697 vers_id
, REGDB_CODE_VERSION
));
698 return WERR_CAN_NOT_COMPLETE
;
701 if (dbwrap_transaction_start(regdb
) != 0) {
702 return WERR_REG_IO_FAILURE
;
705 if (vers_id
== REGDB_VERSION_V1
) {
706 DEBUG(10, ("regdb_init: upgrading registry from version %d "
707 "to %d\n", REGDB_VERSION_V1
, REGDB_VERSION_V2
));
709 werr
= regdb_upgrade_v1_to_v2(regdb
);
710 if (!W_ERROR_IS_OK(werr
)) {
711 dbwrap_transaction_cancel(regdb
);
715 vers_id
= REGDB_VERSION_V2
;
718 if (vers_id
== REGDB_VERSION_V2
) {
719 DEBUG(10, ("regdb_init: upgrading registry from version %d "
720 "to %d\n", REGDB_VERSION_V2
, REGDB_VERSION_V3
));
722 werr
= regdb_upgrade_v2_to_v3(regdb
);
723 if (!W_ERROR_IS_OK(werr
)) {
724 dbwrap_transaction_cancel(regdb
);
728 vers_id
= REGDB_VERSION_V3
;
731 /* future upgrade code should go here */
733 if (dbwrap_transaction_commit(regdb
) != 0) {
734 return WERR_REG_IO_FAILURE
;
740 /***********************************************************************
741 Open the registry. Must already have been initialized by regdb_init()
742 ***********************************************************************/
744 WERROR
regdb_open( void )
746 WERROR result
= WERR_OK
;
749 DEBUG(10, ("regdb_open: incrementing refcount (%d->%d)\n",
750 regdb_refcount
, regdb_refcount
+1));
757 regdb
= db_open(NULL
, state_path("registry.tdb"), 0,
758 REG_TDB_FLAGS
, O_RDWR
, 0600);
760 result
= ntstatus_to_werror( map_nt_error_from_unix( errno
) );
761 DEBUG(0,("regdb_open: Failed to open %s! (%s)\n",
762 state_path("registry.tdb"), strerror(errno
) ));
768 DEBUG(10, ("regdb_open: registry db opened. refcount reset (%d)\n",
774 /***********************************************************************
775 ***********************************************************************/
777 int regdb_close( void )
779 if (regdb_refcount
== 0) {
785 DEBUG(10, ("regdb_close: decrementing refcount (%d->%d)\n",
786 regdb_refcount
+1, regdb_refcount
));
788 if ( regdb_refcount
> 0 )
791 SMB_ASSERT( regdb_refcount
>= 0 );
797 WERROR
regdb_transaction_start(void)
799 return (dbwrap_transaction_start(regdb
) == 0) ?
800 WERR_OK
: WERR_REG_IO_FAILURE
;
803 WERROR
regdb_transaction_commit(void)
805 return (dbwrap_transaction_commit(regdb
) == 0) ?
806 WERR_OK
: WERR_REG_IO_FAILURE
;
809 WERROR
regdb_transaction_cancel(void)
811 return (dbwrap_transaction_cancel(regdb
) == 0) ?
812 WERR_OK
: WERR_REG_IO_FAILURE
;
815 /***********************************************************************
816 return the tdb sequence number of the registry tdb.
817 this is an indicator for the content of the registry
818 having changed. it will change upon regdb_init, too, though.
819 ***********************************************************************/
820 int regdb_get_seqnum(void)
822 return dbwrap_get_seqnum(regdb
);
826 static WERROR
regdb_delete_key_with_prefix(struct db_context
*db
,
831 WERROR werr
= WERR_NOMEM
;
832 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
834 if (keyname
== NULL
) {
835 werr
= WERR_INVALID_PARAM
;
839 if (prefix
== NULL
) {
840 path
= discard_const_p(char, keyname
);
842 path
= talloc_asprintf(mem_ctx
, "%s\\%s", prefix
, keyname
);
848 path
= normalize_reg_path(mem_ctx
, path
);
853 werr
= ntstatus_to_werror(dbwrap_delete_bystring(db
, path
));
855 /* treat "not found" as ok */
856 if (W_ERROR_EQUAL(werr
, WERR_NOT_FOUND
)) {
861 talloc_free(mem_ctx
);
866 static WERROR
regdb_delete_values(struct db_context
*db
, const char *keyname
)
868 return regdb_delete_key_with_prefix(db
, keyname
, REG_VALUE_PREFIX
);
871 static WERROR
regdb_delete_secdesc(struct db_context
*db
, const char *keyname
)
873 return regdb_delete_key_with_prefix(db
, keyname
, REG_SECDESC_PREFIX
);
876 static WERROR
regdb_delete_subkeylist(struct db_context
*db
, const char *keyname
)
878 return regdb_delete_key_with_prefix(db
, keyname
, NULL
);
882 static WERROR
regdb_delete_key_lists(struct db_context
*db
, const char *keyname
)
886 werr
= regdb_delete_values(db
, keyname
);
887 if (!W_ERROR_IS_OK(werr
)) {
888 DEBUG(1, (__location__
" Deleting %s\\%s failed: %s\n",
889 REG_VALUE_PREFIX
, keyname
, win_errstr(werr
)));
893 werr
= regdb_delete_secdesc(db
, keyname
);
894 if (!W_ERROR_IS_OK(werr
)) {
895 DEBUG(1, (__location__
" Deleting %s\\%s failed: %s\n",
896 REG_SECDESC_PREFIX
, keyname
, win_errstr(werr
)));
900 werr
= regdb_delete_subkeylist(db
, keyname
);
901 if (!W_ERROR_IS_OK(werr
)) {
902 DEBUG(1, (__location__
" Deleting %s failed: %s\n",
903 keyname
, win_errstr(werr
)));
911 /***********************************************************************
912 Add subkey strings to the registry tdb under a defined key
913 fmt is the same format as tdb_pack except this function only supports
915 ***********************************************************************/
917 static WERROR
regdb_store_keys_internal2(struct db_context
*db
,
919 struct regsubkey_ctr
*ctr
)
922 uint8
*buffer
= NULL
;
925 uint32 num_subkeys
= regsubkey_ctr_numkeys(ctr
);
926 char *keyname
= NULL
;
927 TALLOC_CTX
*ctx
= talloc_stackframe();
931 werr
= WERR_INVALID_PARAM
;
935 keyname
= talloc_strdup(ctx
, key
);
941 keyname
= normalize_reg_path(ctx
, keyname
);
947 /* allocate some initial memory */
949 buffer
= (uint8
*)SMB_MALLOC(1024);
950 if (buffer
== NULL
) {
957 /* store the number of subkeys */
959 len
+= tdb_pack(buffer
+len
, buflen
-len
, "d", num_subkeys
);
961 /* pack all the strings */
963 for (i
=0; i
<num_subkeys
; i
++) {
966 thistime
= tdb_pack(buffer
+len
, buflen
-len
, "f",
967 regsubkey_ctr_specific_key(ctr
, i
));
968 if (len
+thistime
> buflen
) {
971 * tdb_pack hasn't done anything because of the short
972 * buffer, allocate extra space.
974 buffer
= SMB_REALLOC_ARRAY(buffer
, uint8_t,
977 DEBUG(0, ("regdb_store_keys: Failed to realloc "
978 "memory of size [%u]\n",
979 (unsigned int)(len
+thistime
)*2));
983 buflen
= (len
+thistime
)*2;
984 thistime2
= tdb_pack(
985 buffer
+len
, buflen
-len
, "f",
986 regsubkey_ctr_specific_key(ctr
, i
));
987 if (thistime2
!= thistime
) {
988 DEBUG(0, ("tdb_pack failed\n"));
989 werr
= WERR_CAN_NOT_COMPLETE
;
996 /* finally write out the data */
1000 werr
= ntstatus_to_werror(dbwrap_store_bystring(db
, keyname
, dbuf
,
1010 * Utility function to store a new empty list of
1011 * subkeys of given key specified as parent and subkey name
1012 * (thereby creating the key).
1013 * If the parent keyname is NULL, then the "subkey" is
1014 * interpreted as a base key.
1015 * If the subkey list does already exist, it is not modified.
1017 * Must be called from within a transaction.
1019 static WERROR
regdb_store_subkey_list(struct db_context
*db
, const char *parent
,
1024 struct regsubkey_ctr
*subkeys
= NULL
;
1025 TALLOC_CTX
*frame
= talloc_stackframe();
1027 if (parent
== NULL
) {
1028 path
= talloc_strdup(frame
, key
);
1030 path
= talloc_asprintf(frame
, "%s\\%s", parent
, key
);
1037 werr
= regsubkey_ctr_init(frame
, &subkeys
);
1038 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1040 werr
= regdb_fetch_keys_internal(db
, path
, subkeys
);
1041 if (W_ERROR_IS_OK(werr
)) {
1042 /* subkey list exists already - don't modify */
1046 werr
= regsubkey_ctr_reinit(subkeys
);
1047 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1049 /* create a record with 0 subkeys */
1050 werr
= regdb_store_keys_internal2(db
, path
, subkeys
);
1051 if (!W_ERROR_IS_OK(werr
)) {
1052 DEBUG(0, ("regdb_store_keys: Failed to store new record for "
1053 "key [%s]: %s\n", path
, win_errstr(werr
)));
1062 /***********************************************************************
1063 Store the new subkey record and create any child key records that
1064 do not currently exist
1065 ***********************************************************************/
1067 struct regdb_store_keys_context
{
1069 struct regsubkey_ctr
*ctr
;
1072 static NTSTATUS
regdb_store_keys_action(struct db_context
*db
,
1075 struct regdb_store_keys_context
*store_ctx
;
1079 struct regsubkey_ctr
*old_subkeys
= NULL
;
1080 char *oldkeyname
= NULL
;
1081 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
1083 store_ctx
= (struct regdb_store_keys_context
*)private_data
;
1086 * Re-fetch the old keys inside the transaction
1089 werr
= regsubkey_ctr_init(mem_ctx
, &old_subkeys
);
1090 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1092 werr
= regdb_fetch_keys_internal(db
, store_ctx
->key
, old_subkeys
);
1093 if (!W_ERROR_IS_OK(werr
) &&
1094 !W_ERROR_EQUAL(werr
, WERR_NOT_FOUND
))
1100 * Make the store operation as safe as possible without transactions:
1102 * (1) For each subkey removed from ctr compared with old_subkeys:
1104 * (a) First delete the value db entry.
1106 * (b) Next delete the secdesc db record.
1108 * (c) Then delete the subkey list entry.
1110 * (2) Now write the list of subkeys of the parent key,
1111 * deleting removed entries and adding new ones.
1113 * (3) Finally create the subkey list entries for the added keys.
1115 * This way if we crash half-way in between deleting the subkeys
1116 * and storing the parent's list of subkeys, no old data can pop up
1117 * out of the blue when re-adding keys later on.
1120 /* (1) delete removed keys' lists (values/secdesc/subkeys) */
1122 num_subkeys
= regsubkey_ctr_numkeys(old_subkeys
);
1123 for (i
=0; i
<num_subkeys
; i
++) {
1124 oldkeyname
= regsubkey_ctr_specific_key(old_subkeys
, i
);
1126 if (regsubkey_ctr_key_exists(store_ctx
->ctr
, oldkeyname
)) {
1128 * It's still around, don't delete
1133 path
= talloc_asprintf(mem_ctx
, "%s\\%s", store_ctx
->key
,
1140 werr
= regdb_delete_key_lists(db
, path
);
1141 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1146 TALLOC_FREE(old_subkeys
);
1148 /* (2) store the subkey list for the parent */
1150 werr
= regdb_store_keys_internal2(db
, store_ctx
->key
, store_ctx
->ctr
);
1151 if (!W_ERROR_IS_OK(werr
)) {
1152 DEBUG(0,("regdb_store_keys: Failed to store new subkey list "
1153 "for parent [%s]: %s\n", store_ctx
->key
,
1158 /* (3) now create records for any subkeys that don't already exist */
1160 num_subkeys
= regsubkey_ctr_numkeys(store_ctx
->ctr
);
1162 for (i
=0; i
<num_subkeys
; i
++) {
1165 subkey
= regsubkey_ctr_specific_key(store_ctx
->ctr
, i
);
1167 werr
= regdb_store_subkey_list(db
, store_ctx
->key
, subkey
);
1168 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1174 talloc_free(mem_ctx
);
1175 return werror_to_ntstatus(werr
);
1178 static bool regdb_store_keys_internal(struct db_context
*db
, const char *key
,
1179 struct regsubkey_ctr
*ctr
)
1181 int num_subkeys
, old_num_subkeys
, i
;
1182 struct regsubkey_ctr
*old_subkeys
= NULL
;
1183 TALLOC_CTX
*ctx
= talloc_stackframe();
1186 struct regdb_store_keys_context store_ctx
;
1188 if (!regdb_key_exists(db
, key
)) {
1193 * fetch a list of the old subkeys so we can determine if anything has
1197 werr
= regsubkey_ctr_init(ctx
, &old_subkeys
);
1198 if (!W_ERROR_IS_OK(werr
)) {
1199 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
1203 werr
= regdb_fetch_keys_internal(db
, key
, old_subkeys
);
1204 if (!W_ERROR_IS_OK(werr
) &&
1205 !W_ERROR_EQUAL(werr
, WERR_NOT_FOUND
))
1210 num_subkeys
= regsubkey_ctr_numkeys(ctr
);
1211 old_num_subkeys
= regsubkey_ctr_numkeys(old_subkeys
);
1212 if ((num_subkeys
&& old_num_subkeys
) &&
1213 (num_subkeys
== old_num_subkeys
)) {
1215 for (i
= 0; i
< num_subkeys
; i
++) {
1216 if (strcmp(regsubkey_ctr_specific_key(ctr
, i
),
1217 regsubkey_ctr_specific_key(old_subkeys
, i
))
1223 if (i
== num_subkeys
) {
1225 * Nothing changed, no point to even start a tdb
1234 TALLOC_FREE(old_subkeys
);
1236 store_ctx
.key
= key
;
1237 store_ctx
.ctr
= ctr
;
1239 werr
= regdb_trans_do(db
,
1240 regdb_store_keys_action
,
1243 ret
= W_ERROR_IS_OK(werr
);
1251 bool regdb_store_keys(const char *key
, struct regsubkey_ctr
*ctr
)
1253 return regdb_store_keys_internal(regdb
, key
, ctr
);
1257 * create a subkey of a given key
1260 struct regdb_create_subkey_context
{
1265 static NTSTATUS
regdb_create_subkey_action(struct db_context
*db
,
1269 struct regdb_create_subkey_context
*create_ctx
;
1270 struct regsubkey_ctr
*subkeys
;
1271 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
1273 create_ctx
= (struct regdb_create_subkey_context
*)private_data
;
1275 werr
= regsubkey_ctr_init(mem_ctx
, &subkeys
);
1276 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1278 werr
= regdb_fetch_keys_internal(db
, create_ctx
->key
, subkeys
);
1279 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1281 werr
= regsubkey_ctr_addkey(subkeys
, create_ctx
->subkey
);
1282 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1284 werr
= regdb_store_keys_internal2(db
, create_ctx
->key
, subkeys
);
1285 if (!W_ERROR_IS_OK(werr
)) {
1286 DEBUG(0, (__location__
" failed to store new subkey list for "
1287 "parent key %s: %s\n", create_ctx
->key
,
1291 werr
= regdb_store_subkey_list(db
, create_ctx
->key
, create_ctx
->subkey
);
1294 talloc_free(mem_ctx
);
1295 return werror_to_ntstatus(werr
);
1298 static WERROR
regdb_create_subkey_internal(struct db_context
*db
,
1303 struct regsubkey_ctr
*subkeys
;
1304 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
1305 struct regdb_create_subkey_context create_ctx
;
1307 if (!regdb_key_exists(db
, key
)) {
1308 werr
= WERR_NOT_FOUND
;
1312 werr
= regsubkey_ctr_init(mem_ctx
, &subkeys
);
1313 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1315 werr
= regdb_fetch_keys_internal(db
, key
, subkeys
);
1316 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1318 if (regsubkey_ctr_key_exists(subkeys
, subkey
)) {
1321 newkey
= talloc_asprintf(mem_ctx
, "%s\\%s", key
, subkey
);
1322 if (newkey
== NULL
) {
1327 if (regdb_key_exists(db
, newkey
)) {
1333 talloc_free(subkeys
);
1335 create_ctx
.key
= key
;
1336 create_ctx
.subkey
= subkey
;
1338 werr
= regdb_trans_do(db
,
1339 regdb_create_subkey_action
,
1343 talloc_free(mem_ctx
);
1347 static WERROR
regdb_create_subkey(const char *key
, const char *subkey
)
1349 return regdb_create_subkey_internal(regdb
, key
, subkey
);
1356 struct regdb_create_basekey_context
{
1360 static NTSTATUS
regdb_create_basekey_action(struct db_context
*db
,
1364 struct regdb_create_basekey_context
*create_ctx
;
1366 create_ctx
= (struct regdb_create_basekey_context
*)private_data
;
1368 werr
= regdb_store_subkey_list(db
, NULL
, create_ctx
->key
);
1370 return werror_to_ntstatus(werr
);
1373 static WERROR
regdb_create_basekey(struct db_context
*db
, const char *key
)
1376 struct regdb_create_subkey_context create_ctx
;
1378 create_ctx
.key
= key
;
1380 werr
= regdb_trans_do(db
,
1381 regdb_create_basekey_action
,
1388 * create a subkey of a given key
1391 struct regdb_delete_subkey_context
{
1398 static NTSTATUS
regdb_delete_subkey_action(struct db_context
*db
,
1402 struct regdb_delete_subkey_context
*delete_ctx
;
1403 struct regsubkey_ctr
*subkeys
;
1404 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
1406 delete_ctx
= (struct regdb_delete_subkey_context
*)private_data
;
1408 werr
= regdb_delete_key_lists(db
, delete_ctx
->path
);
1409 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1411 if (delete_ctx
->lazy
) {
1415 werr
= regsubkey_ctr_init(mem_ctx
, &subkeys
);
1416 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1418 werr
= regdb_fetch_keys_internal(db
, delete_ctx
->key
, subkeys
);
1419 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1421 werr
= regsubkey_ctr_delkey(subkeys
, delete_ctx
->subkey
);
1422 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1424 werr
= regdb_store_keys_internal2(db
, delete_ctx
->key
, subkeys
);
1425 if (!W_ERROR_IS_OK(werr
)) {
1426 DEBUG(0, (__location__
" failed to store new subkey_list for "
1427 "parent key %s: %s\n", delete_ctx
->key
,
1432 talloc_free(mem_ctx
);
1433 return werror_to_ntstatus(werr
);
1436 static WERROR
regdb_delete_subkey(const char *key
, const char *subkey
, bool lazy
)
1440 struct regdb_delete_subkey_context delete_ctx
;
1441 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
1443 if (!regdb_key_exists(regdb
, key
)) {
1444 werr
= WERR_NOT_FOUND
;
1448 path
= talloc_asprintf(mem_ctx
, "%s\\%s", key
, subkey
);
1454 if (!regdb_key_exists(regdb
, path
)) {
1459 delete_ctx
.key
= key
;
1460 delete_ctx
.subkey
= subkey
;
1461 delete_ctx
.path
= path
;
1462 delete_ctx
.lazy
= lazy
;
1464 werr
= regdb_trans_do(regdb
,
1465 regdb_delete_subkey_action
,
1469 talloc_free(mem_ctx
);
1473 static TDB_DATA
regdb_fetch_key_internal(struct db_context
*db
,
1474 TALLOC_CTX
*mem_ctx
, const char *key
)
1480 path
= normalize_reg_path(mem_ctx
, key
);
1482 return make_tdb_data(NULL
, 0);
1485 status
= dbwrap_fetch_bystring(db
, mem_ctx
, path
, &data
);
1486 if (!NT_STATUS_IS_OK(status
)) {
1496 * Check for the existence of a key.
1498 * Existence of a key is authoritatively defined by
1499 * the existence of the record that contains the list
1502 * Return false, if the record does not match the correct
1503 * structure of an initial 4-byte counter and then a
1504 * list of the corresponding number of zero-terminated
1507 static bool regdb_key_exists(struct db_context
*db
, const char *key
)
1509 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
1515 uint32_t num_items
, i
;
1522 path
= normalize_reg_path(mem_ctx
, key
);
1524 DEBUG(0, ("out of memory! (talloc failed)\n"));
1528 if (*path
== '\0') {
1532 value
= regdb_fetch_key_internal(db
, mem_ctx
, path
);
1533 if (value
.dptr
== NULL
) {
1537 if (value
.dsize
== 0) {
1538 DEBUG(10, ("regdb_key_exists: subkeylist-record for key "
1539 "[%s] is empty: Could be a deleted record in a "
1540 "clustered (ctdb) environment?\n",
1545 len
= tdb_unpack(value
.dptr
, value
.dsize
, "d", &num_items
);
1546 if (len
== (int32_t)-1) {
1547 DEBUG(1, ("regdb_key_exists: ERROR: subkeylist-record for key "
1548 "[%s] is invalid: Could not parse initial 4-byte "
1549 "counter. record data length is %u.\n",
1550 path
, (unsigned int)value
.dsize
));
1555 * Note: the tdb_unpack check above implies that len <= value.dsize
1557 buflen
= value
.dsize
- len
;
1558 buf
= (const char *)value
.dptr
+ len
;
1562 for (i
= 0; i
< num_items
; i
++) {
1566 len
= strnlen(buf
, buflen
) + 1;
1568 DEBUG(1, ("regdb_key_exists: ERROR: subkeylist-record "
1569 "for key [%s] is corrupt: %u items expected, "
1570 "item number %u is not zero terminated.\n",
1571 path
, num_items
, i
+1));
1580 DEBUG(1, ("regdb_key_exists: ERROR: subkeylist-record for key "
1581 "[%s] is corrupt: %u items expected and found, but "
1582 "the record contains additional %u bytes\n",
1583 path
, num_items
, buflen
));
1587 if (i
< num_items
) {
1588 DEBUG(1, ("regdb_key_exists: ERROR: subkeylist-record for key "
1589 "[%s] is corrupt: %u items expected, but only %u "
1591 path
, num_items
, i
+1));
1598 TALLOC_FREE(mem_ctx
);
1603 /***********************************************************************
1604 Retrieve an array of strings containing subkeys. Memory should be
1605 released by the caller.
1606 ***********************************************************************/
1608 static WERROR
regdb_fetch_keys_internal(struct db_context
*db
, const char *key
,
1609 struct regsubkey_ctr
*ctr
)
1617 TALLOC_CTX
*frame
= talloc_stackframe();
1620 DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key
? key
: "NULL"));
1622 if (!regdb_key_exists(db
, key
)) {
1623 DEBUG(10, ("key [%s] not found\n", key
));
1624 werr
= WERR_NOT_FOUND
;
1628 werr
= regsubkey_ctr_reinit(ctr
);
1629 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1631 werr
= regsubkey_ctr_set_seqnum(ctr
, dbwrap_get_seqnum(db
));
1632 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1634 value
= regdb_fetch_key_internal(db
, frame
, key
);
1636 if (value
.dsize
== 0 || value
.dptr
== NULL
) {
1637 DEBUG(10, ("regdb_fetch_keys: no subkeys found for key [%s]\n",
1643 buflen
= value
.dsize
;
1644 len
= tdb_unpack( buf
, buflen
, "d", &num_items
);
1645 if (len
== (uint32_t)-1) {
1646 werr
= WERR_NOT_FOUND
;
1650 for (i
=0; i
<num_items
; i
++) {
1651 len
+= tdb_unpack(buf
+len
, buflen
-len
, "f", subkeyname
);
1652 werr
= regsubkey_ctr_addkey(ctr
, subkeyname
);
1653 if (!W_ERROR_IS_OK(werr
)) {
1654 DEBUG(5, ("regdb_fetch_keys: regsubkey_ctr_addkey "
1655 "failed: %s\n", win_errstr(werr
)));
1661 DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items
));
1668 int regdb_fetch_keys(const char *key
, struct regsubkey_ctr
*ctr
)
1672 werr
= regdb_fetch_keys_internal(regdb
, key
, ctr
);
1673 if (!W_ERROR_IS_OK(werr
)) {
1677 return regsubkey_ctr_numkeys(ctr
);
1680 /****************************************************************************
1681 Unpack a list of registry values frem the TDB
1682 ***************************************************************************/
1684 static int regdb_unpack_values(struct regval_ctr
*values
, uint8
*buf
, int buflen
)
1691 uint32 num_values
= 0;
1694 /* loop and unpack the rest of the registry values */
1696 len
+= tdb_unpack(buf
+len
, buflen
-len
, "d", &num_values
);
1698 for ( i
=0; i
<num_values
; i
++ ) {
1699 /* unpack the next regval */
1704 valuename
[0] = '\0';
1705 len
+= tdb_unpack(buf
+len
, buflen
-len
, "fdB",
1711 regval_ctr_addvalue(values
, valuename
, type
,
1712 (uint8_t *)data_p
, size
);
1713 SAFE_FREE(data_p
); /* 'B' option to tdb_unpack does a malloc() */
1715 DEBUG(8,("specific: [%s], len: %d\n", valuename
, size
));
1721 /****************************************************************************
1722 Pack all values in all printer keys
1723 ***************************************************************************/
1725 static int regdb_pack_values(struct regval_ctr
*values
, uint8
*buf
, int buflen
)
1729 struct regval_blob
*val
;
1735 num_values
= regval_ctr_numvals( values
);
1737 /* pack the number of values first */
1739 len
+= tdb_pack( buf
+len
, buflen
-len
, "d", num_values
);
1741 /* loop over all values */
1743 for ( i
=0; i
<num_values
; i
++ ) {
1744 val
= regval_ctr_specific_value( values
, i
);
1745 len
+= tdb_pack(buf
+len
, buflen
-len
, "fdB",
1749 regval_data_p(val
) );
1755 /***********************************************************************
1756 Retrieve an array of strings containing subkeys. Memory should be
1757 released by the caller.
1758 ***********************************************************************/
1760 static int regdb_fetch_values_internal(struct db_context
*db
, const char* key
,
1761 struct regval_ctr
*values
)
1763 char *keystr
= NULL
;
1764 TALLOC_CTX
*ctx
= talloc_stackframe();
1769 DEBUG(10,("regdb_fetch_values: Looking for value of key [%s] \n", key
));
1771 if (!regdb_key_exists(db
, key
)) {
1775 keystr
= talloc_asprintf(ctx
, "%s\\%s", REG_VALUE_PREFIX
, key
);
1780 werr
= regval_ctr_set_seqnum(values
, dbwrap_get_seqnum(db
));
1781 W_ERROR_NOT_OK_GOTO_DONE(werr
);
1783 value
= regdb_fetch_key_internal(db
, ctx
, keystr
);
1786 /* all keys have zero values by default */
1790 regdb_unpack_values(values
, value
.dptr
, value
.dsize
);
1791 ret
= regval_ctr_numvals(values
);
1798 int regdb_fetch_values(const char* key
, struct regval_ctr
*values
)
1800 return regdb_fetch_values_internal(regdb
, key
, values
);
1803 static NTSTATUS
regdb_store_values_internal(struct db_context
*db
,
1805 struct regval_ctr
*values
)
1807 TDB_DATA old_data
, data
;
1808 char *keystr
= NULL
;
1809 TALLOC_CTX
*ctx
= talloc_stackframe();
1813 DEBUG(10,("regdb_store_values: Looking for value of key [%s] \n", key
));
1815 if (!regdb_key_exists(db
, key
)) {
1816 status
= NT_STATUS_NOT_FOUND
;
1822 len
= regdb_pack_values(values
, data
.dptr
, data
.dsize
);
1824 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
1825 status
= NT_STATUS_UNSUCCESSFUL
;
1829 data
.dptr
= talloc_array(ctx
, uint8
, len
);
1832 len
= regdb_pack_values(values
, data
.dptr
, data
.dsize
);
1834 SMB_ASSERT( len
== data
.dsize
);
1836 keystr
= talloc_asprintf(ctx
, "%s\\%s", REG_VALUE_PREFIX
, key
);
1838 status
= NT_STATUS_NO_MEMORY
;
1841 keystr
= normalize_reg_path(ctx
, keystr
);
1843 status
= NT_STATUS_NO_MEMORY
;
1847 status
= dbwrap_fetch_bystring(db
, ctx
, keystr
, &old_data
);
1849 if (NT_STATUS_IS_OK(status
)
1850 && (old_data
.dptr
!= NULL
)
1851 && (old_data
.dsize
== data
.dsize
)
1852 && (memcmp(old_data
.dptr
, data
.dptr
, data
.dsize
) == 0))
1854 status
= NT_STATUS_OK
;
1858 status
= dbwrap_trans_store_bystring(db
, keystr
, data
, TDB_REPLACE
);
1865 struct regdb_store_values_ctx
{
1867 struct regval_ctr
*values
;
1870 static NTSTATUS
regdb_store_values_action(struct db_context
*db
,
1874 struct regdb_store_values_ctx
*ctx
=
1875 (struct regdb_store_values_ctx
*)private_data
;
1877 status
= regdb_store_values_internal(db
, ctx
->key
, ctx
->values
);
1882 bool regdb_store_values(const char *key
, struct regval_ctr
*values
)
1885 struct regdb_store_values_ctx ctx
;
1888 ctx
.values
= values
;
1890 werr
= regdb_trans_do(regdb
, regdb_store_values_action
, &ctx
);
1892 return W_ERROR_IS_OK(werr
);
1895 static WERROR
regdb_get_secdesc(TALLOC_CTX
*mem_ctx
, const char *key
,
1896 struct security_descriptor
**psecdesc
)
1901 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
1902 WERROR err
= WERR_OK
;
1904 DEBUG(10, ("regdb_get_secdesc: Getting secdesc of key [%s]\n", key
));
1906 if (!regdb_key_exists(regdb
, key
)) {
1911 tdbkey
= talloc_asprintf(tmp_ctx
, "%s\\%s", REG_SECDESC_PREFIX
, key
);
1912 if (tdbkey
== NULL
) {
1917 tdbkey
= normalize_reg_path(tmp_ctx
, tdbkey
);
1918 if (tdbkey
== NULL
) {
1923 status
= dbwrap_fetch_bystring(regdb
, tmp_ctx
, tdbkey
, &data
);
1924 if (!NT_STATUS_IS_OK(status
)) {
1929 status
= unmarshall_sec_desc(mem_ctx
, (uint8
*)data
.dptr
, data
.dsize
,
1932 if (NT_STATUS_EQUAL(status
, NT_STATUS_NO_MEMORY
)) {
1934 } else if (!NT_STATUS_IS_OK(status
)) {
1935 err
= WERR_REG_CORRUPT
;
1939 TALLOC_FREE(tmp_ctx
);
1943 struct regdb_set_secdesc_ctx
{
1945 struct security_descriptor
*secdesc
;
1948 static NTSTATUS
regdb_set_secdesc_action(struct db_context
*db
,
1954 struct regdb_set_secdesc_ctx
*ctx
=
1955 (struct regdb_set_secdesc_ctx
*)private_data
;
1956 TALLOC_CTX
*frame
= talloc_stackframe();
1958 tdbkey
= talloc_asprintf(frame
, "%s\\%s", REG_SECDESC_PREFIX
, ctx
->key
);
1959 if (tdbkey
== NULL
) {
1960 status
= NT_STATUS_NO_MEMORY
;
1964 tdbkey
= normalize_reg_path(frame
, tdbkey
);
1965 if (tdbkey
== NULL
) {
1966 status
= NT_STATUS_NO_MEMORY
;
1970 if (ctx
->secdesc
== NULL
) {
1971 /* assuming a delete */
1972 status
= dbwrap_delete_bystring(db
, tdbkey
);
1976 status
= marshall_sec_desc(frame
, ctx
->secdesc
, &tdbdata
.dptr
,
1978 if (!NT_STATUS_IS_OK(status
)) {
1982 status
= dbwrap_store_bystring(db
, tdbkey
, tdbdata
, 0);
1989 static WERROR
regdb_set_secdesc(const char *key
,
1990 struct security_descriptor
*secdesc
)
1993 struct regdb_set_secdesc_ctx ctx
;
1995 if (!regdb_key_exists(regdb
, key
)) {
2001 ctx
.secdesc
= secdesc
;
2003 err
= regdb_trans_do(regdb
, regdb_set_secdesc_action
, &ctx
);
2009 bool regdb_subkeys_need_update(struct regsubkey_ctr
*subkeys
)
2011 return (regdb_get_seqnum() != regsubkey_ctr_get_seqnum(subkeys
));
2014 bool regdb_values_need_update(struct regval_ctr
*values
)
2016 return (regdb_get_seqnum() != regval_ctr_get_seqnum(values
));
2020 * Table of function pointers for default access
2023 struct registry_ops regdb_ops
= {
2024 .fetch_subkeys
= regdb_fetch_keys
,
2025 .fetch_values
= regdb_fetch_values
,
2026 .store_subkeys
= regdb_store_keys
,
2027 .store_values
= regdb_store_values
,
2028 .create_subkey
= regdb_create_subkey
,
2029 .delete_subkey
= regdb_delete_subkey
,
2030 .get_secdesc
= regdb_get_secdesc
,
2031 .set_secdesc
= regdb_set_secdesc
,
2032 .subkeys_need_update
= regdb_subkeys_need_update
,
2033 .values_need_update
= regdb_values_need_update