2 * Unix SMB/CIFS implementation.
3 * Virtual Windows Registry Layer
4 * Copyright (C) Gerald Carter 2002-2005
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 /* Implementation of internal registry database functions. */
25 #define DBGC_CLASS DBGC_REGISTRY
27 static struct db_context
*regdb
= NULL
;
28 static int regdb_refcount
;
30 static bool regdb_key_exists(const char *key
);
31 static bool regdb_key_is_base_key(const char *key
);
33 /* List the deepest path into the registry. All part components will be created.*/
35 /* If you want to have a part of the path controlled by the tdb and part by
36 a virtual registry db (e.g. printing), then you have to list the deepest path.
37 For example,"HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion/Print"
38 allows the reg_db backend to handle everything up to
39 "HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion" and then we'll hook
40 the reg_printing backend onto the last component of the path (see
41 KEY_PRINTING_2K in include/rpc_reg.h) --jerry */
43 static const char *builtin_registry_paths
[] = {
53 KEY_SAMBA_GROUP_POLICY
,
54 KEY_GP_MACHINE_POLICY
,
55 KEY_GP_MACHINE_WIN_POLICY
,
58 KEY_GP_USER_WIN_POLICY
,
59 KEY_WINLOGON_GPEXT_PATH
,
60 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Print\\Monitors",
62 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\DefaultUserConfiguration",
71 struct builtin_regkey_value
{
73 const char *valuename
;
81 static struct builtin_regkey_value builtin_registry_values
[] = {
83 SAMBA_PRINTER_PORT_NAME
, REG_SZ
, { "" } },
85 "DefaultSpoolDirectory", REG_SZ
, { "C:\\Windows\\System32\\Spool\\Printers" } },
87 "DisplayName", REG_SZ
, { "Event Log" } },
89 "ErrorControl", REG_DWORD
, { (char*)0x00000001 } },
90 { NULL
, NULL
, 0, { NULL
} }
94 * Initialize a key in the registry:
95 * create each component key of the specified path.
97 static WERROR
init_registry_key_internal(const char *add_path
)
100 TALLOC_CTX
*frame
= talloc_stackframe();
103 char *remaining
= NULL
;
106 REGSUBKEY_CTR
*subkeys
;
109 DEBUG(6, ("init_registry_key: Adding [%s]\n", add_path
));
111 path
= talloc_strdup(frame
, add_path
);
112 base
= talloc_strdup(frame
, "");
113 if (!path
|| !base
) {
119 while (next_token_talloc(frame
, &p
, &keyname
, "\\")) {
121 /* build up the registry path from the components */
124 base
= talloc_asprintf(frame
, "%s\\", base
);
130 base
= talloc_asprintf_append(base
, "%s", keyname
);
136 /* get the immediate subkeyname (if we have one ) */
138 subkeyname
= talloc_strdup(frame
, "");
144 remaining
= talloc_strdup(frame
, p
);
151 if (!next_token_talloc(frame
, &p2
,
154 subkeyname
= talloc_strdup(frame
,p2
);
162 DEBUG(10,("init_registry_key: Storing key [%s] with "
163 "subkey [%s]\n", base
,
164 *subkeyname
? subkeyname
: "NULL"));
166 /* we don't really care if the lookup succeeds or not
167 * since we are about to update the record.
168 * We just want any subkeys already present */
170 if (!(subkeys
= TALLOC_ZERO_P(frame
, REGSUBKEY_CTR
))) {
171 DEBUG(0,("talloc() failure!\n"));
176 regdb_fetch_keys(base
, subkeys
);
178 werr
= regsubkey_ctr_addkey(subkeys
, subkeyname
);
179 if (!W_ERROR_IS_OK(werr
)) {
183 if (!regdb_store_keys( base
, subkeys
)) {
184 werr
= WERR_CAN_NOT_COMPLETE
;
197 * Initialize a key in the registry:
198 * create each component key of the specified path,
199 * wrapped in one db transaction.
201 WERROR
init_registry_key(const char *add_path
)
205 if (regdb_key_exists(add_path
)) {
209 if (regdb
->transaction_start(regdb
) != 0) {
210 DEBUG(0, ("init_registry_key: transaction_start failed\n"));
211 return WERR_REG_IO_FAILURE
;
214 werr
= init_registry_key_internal(add_path
);
215 if (!W_ERROR_IS_OK(werr
)) {
219 if (regdb
->transaction_commit(regdb
) != 0) {
220 DEBUG(0, ("init_registry_key: Could not commit transaction\n"));
221 return WERR_REG_IO_FAILURE
;
227 if (regdb
->transaction_cancel(regdb
) != 0) {
228 smb_panic("init_registry_key: transaction_cancel failed\n");
234 /***********************************************************************
235 Open the registry data in the tdb
236 ***********************************************************************/
238 WERROR
init_registry_data(void)
241 TALLOC_CTX
*frame
= talloc_stackframe();
247 * First, check for the existence of the needed keys and values.
248 * If all do already exist, we can save the writes.
250 for (i
=0; builtin_registry_paths
[i
] != NULL
; i
++) {
251 if (!regdb_key_exists(builtin_registry_paths
[i
])) {
256 for (i
=0; builtin_registry_values
[i
].path
!= NULL
; i
++) {
257 values
= TALLOC_ZERO_P(frame
, REGVAL_CTR
);
258 if (values
== NULL
) {
263 regdb_fetch_values(builtin_registry_values
[i
].path
, values
);
264 if (!regval_ctr_key_exists(values
,
265 builtin_registry_values
[i
].valuename
))
280 * There are potentially quite a few store operations which are all
281 * indiviually wrapped in tdb transactions. Wrapping them in a single
282 * transaction gives just a single transaction_commit() to actually do
283 * its fsync()s. See tdb/common/transaction.c for info about nested
284 * transaction behaviour.
287 if (regdb
->transaction_start(regdb
) != 0) {
288 DEBUG(0, ("init_registry_data: tdb_transaction_start "
290 werr
= WERR_REG_IO_FAILURE
;
294 /* loop over all of the predefined paths and add each component */
296 for (i
=0; builtin_registry_paths
[i
] != NULL
; i
++) {
297 if (regdb_key_exists(builtin_registry_paths
[i
])) {
300 werr
= init_registry_key_internal(builtin_registry_paths
[i
]);
301 if (!W_ERROR_IS_OK(werr
)) {
306 /* loop over all of the predefined values and add each component */
308 for (i
=0; builtin_registry_values
[i
].path
!= NULL
; i
++) {
310 values
= TALLOC_ZERO_P(frame
, REGVAL_CTR
);
311 if (values
== NULL
) {
316 regdb_fetch_values(builtin_registry_values
[i
].path
, values
);
318 /* preserve existing values across restarts. Only add new ones */
320 if (!regval_ctr_key_exists(values
,
321 builtin_registry_values
[i
].valuename
))
323 switch(builtin_registry_values
[i
].type
) {
325 regval_ctr_addvalue(values
,
326 builtin_registry_values
[i
].valuename
,
328 (char*)&builtin_registry_values
[i
].data
.dw_value
,
334 builtin_registry_values
[i
].data
.string
,
336 regval_ctr_addvalue(values
,
337 builtin_registry_values
[i
].valuename
,
340 data
.uni_str_len
*sizeof(uint16
));
344 DEBUG(0, ("init_registry_data: invalid value "
345 "type in builtin_registry_values "
347 builtin_registry_values
[i
].type
));
349 regdb_store_values(builtin_registry_values
[i
].path
,
355 if (regdb
->transaction_commit(regdb
) != 0) {
356 DEBUG(0, ("init_registry_data: Could not commit "
358 werr
= WERR_REG_IO_FAILURE
;
366 if (regdb
->transaction_cancel(regdb
) != 0) {
367 smb_panic("init_registry_data: tdb_transaction_cancel "
376 /***********************************************************************
377 Open the registry database
378 ***********************************************************************/
380 WERROR
regdb_init(void)
382 const char *vstring
= "INFO/version";
387 DEBUG(10, ("regdb_init: incrementing refcount (%d)\n",
393 regdb
= db_open_trans(NULL
, state_path("registry.tdb"), 0,
394 REG_TDB_FLAGS
, O_RDWR
, 0600);
396 regdb
= db_open_trans(NULL
, state_path("registry.tdb"), 0,
397 REG_TDB_FLAGS
, O_RDWR
|O_CREAT
, 0600);
399 werr
= ntstatus_to_werror(map_nt_error_from_unix(errno
));
400 DEBUG(1,("regdb_init: Failed to open registry %s (%s)\n",
401 state_path("registry.tdb"), strerror(errno
) ));
405 DEBUG(10,("regdb_init: Successfully created registry tdb\n"));
410 vers_id
= dbwrap_fetch_int32(regdb
, vstring
);
412 if ( vers_id
!= REGVER_V1
) {
414 /* any upgrade code here if needed */
415 DEBUG(10, ("regdb_init: got %s = %d != %d\n", vstring
,
416 vers_id
, REGVER_V1
));
417 status
= dbwrap_trans_store_int32(regdb
, vstring
, REGVER_V1
);
418 if (!NT_STATUS_IS_OK(status
)) {
419 DEBUG(1, ("regdb_init: error storing %s = %d: %s\n",
420 vstring
, REGVER_V1
, nt_errstr(status
)));
421 return ntstatus_to_werror(status
);
423 DEBUG(10, ("regdb_init: stored %s = %d\n",
424 vstring
, REGVER_V1
));
431 /***********************************************************************
432 Open the registry. Must already have been initialized by regdb_init()
433 ***********************************************************************/
435 WERROR
regdb_open( void )
437 WERROR result
= WERR_OK
;
440 DEBUG(10,("regdb_open: incrementing refcount (%d)\n", regdb_refcount
));
447 regdb
= db_open_trans(NULL
, state_path("registry.tdb"), 0,
448 REG_TDB_FLAGS
, O_RDWR
, 0600);
450 result
= ntstatus_to_werror( map_nt_error_from_unix( errno
) );
451 DEBUG(0,("regdb_open: Failed to open %s! (%s)\n",
452 state_path("registry.tdb"), strerror(errno
) ));
458 DEBUG(10,("regdb_open: refcount reset (%d)\n", regdb_refcount
));
463 /***********************************************************************
464 ***********************************************************************/
466 int regdb_close( void )
468 if (regdb_refcount
== 0) {
474 DEBUG(10,("regdb_close: decrementing refcount (%d)\n", regdb_refcount
));
476 if ( regdb_refcount
> 0 )
479 SMB_ASSERT( regdb_refcount
>= 0 );
485 /***********************************************************************
486 return the tdb sequence number of the registry tdb.
487 this is an indicator for the content of the registry
488 having changed. it will change upon regdb_init, too, though.
489 ***********************************************************************/
490 int regdb_get_seqnum(void)
492 return regdb
->get_seqnum(regdb
);
495 /***********************************************************************
496 Add subkey strings to the registry tdb under a defined key
497 fmt is the same format as tdb_pack except this function only supports
499 ***********************************************************************/
501 static bool regdb_store_keys_internal(const char *key
, REGSUBKEY_CTR
*ctr
)
504 uint8
*buffer
= NULL
;
508 uint32 num_subkeys
= regsubkey_ctr_numkeys(ctr
);
509 char *keyname
= NULL
;
510 TALLOC_CTX
*ctx
= talloc_stackframe();
517 keyname
= talloc_strdup(ctx
, key
);
521 keyname
= normalize_reg_path(ctx
, keyname
);
523 /* allocate some initial memory */
525 buffer
= (uint8
*)SMB_MALLOC(1024);
526 if (buffer
== NULL
) {
532 /* store the number of subkeys */
534 len
+= tdb_pack(buffer
+len
, buflen
-len
, "d", num_subkeys
);
536 /* pack all the strings */
538 for (i
=0; i
<num_subkeys
; i
++) {
539 len
+= tdb_pack(buffer
+len
, buflen
-len
, "f",
540 regsubkey_ctr_specific_key(ctr
, i
));
542 /* allocate some extra space */
543 buffer
= (uint8
*)SMB_REALLOC(buffer
, len
*2);
545 DEBUG(0, ("regdb_store_keys: Failed to realloc "
546 "memory of size [%d]\n", len
*2));
551 len
= tdb_pack(buffer
+len
, buflen
-len
, "f",
552 regsubkey_ctr_specific_key(ctr
, i
));
556 /* finally write out the data */
560 status
= dbwrap_store_bystring(regdb
, keyname
, dbuf
, TDB_REPLACE
);
561 if (!NT_STATUS_IS_OK(status
)) {
572 /***********************************************************************
573 Store the new subkey record and create any child key records that
574 do not currently exist
575 ***********************************************************************/
577 bool regdb_store_keys(const char *key
, REGSUBKEY_CTR
*ctr
)
581 REGSUBKEY_CTR
*subkeys
= NULL
, *old_subkeys
= NULL
;
582 char *oldkeyname
= NULL
;
583 TALLOC_CTX
*ctx
= talloc_stackframe();
586 if (!regdb_key_is_base_key(key
) && !regdb_key_exists(key
)) {
591 * fetch a list of the old subkeys so we can determine if anything has
595 if (!(old_subkeys
= TALLOC_ZERO_P(ctx
, REGSUBKEY_CTR
))) {
596 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
600 regdb_fetch_keys(key
, old_subkeys
);
602 if ((ctr
->num_subkeys
&& old_subkeys
->num_subkeys
) &&
603 (ctr
->num_subkeys
== old_subkeys
->num_subkeys
)) {
605 for (i
= 0; i
<ctr
->num_subkeys
; i
++) {
606 if (strcmp(ctr
->subkeys
[i
],
607 old_subkeys
->subkeys
[i
]) != 0) {
611 if (i
== ctr
->num_subkeys
) {
613 * Nothing changed, no point to even start a tdb
616 TALLOC_FREE(old_subkeys
);
621 TALLOC_FREE(old_subkeys
);
623 if (regdb
->transaction_start(regdb
) != 0) {
624 DEBUG(0, ("regdb_store_keys: transaction_start failed\n"));
629 * Re-fetch the old keys inside the transaction
632 if (!(old_subkeys
= TALLOC_ZERO_P(ctx
, REGSUBKEY_CTR
))) {
633 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
637 regdb_fetch_keys(key
, old_subkeys
);
640 * Make the store operation as safe as possible without transactions:
642 * (1) For each subkey removed from ctr compared with old_subkeys:
644 * (a) First delete the value db entry.
646 * (b) Next delete the secdesc db record.
648 * (c) Then delete the subkey list entry.
650 * (2) Now write the list of subkeys of the parent key,
651 * deleting removed entries and adding new ones.
653 * (3) Finally create the subkey list entries for the added keys.
655 * This way if we crash half-way in between deleting the subkeys
656 * and storing the parent's list of subkeys, no old data can pop up
657 * out of the blue when re-adding keys later on.
660 /* (1) delete removed keys' lists (values/secdesc/subkeys) */
662 num_subkeys
= regsubkey_ctr_numkeys(old_subkeys
);
663 for (i
=0; i
<num_subkeys
; i
++) {
664 oldkeyname
= regsubkey_ctr_specific_key(old_subkeys
, i
);
666 if (regsubkey_ctr_key_exists(ctr
, oldkeyname
)) {
668 * It's still around, don't delete
674 /* (a) Delete the value list for this key */
676 path
= talloc_asprintf(ctx
, "%s/%s/%s",
683 path
= normalize_reg_path(ctx
, path
);
687 /* Ignore errors here, we might have no values around */
688 dbwrap_delete_bystring(regdb
, path
);
691 /* (b) Delete the secdesc for this key */
693 path
= talloc_asprintf(ctx
, "%s/%s/%s",
700 path
= normalize_reg_path(ctx
, path
);
704 status
= dbwrap_delete_bystring(regdb
, path
);
705 /* Don't fail if there are no values around. */
706 if (!NT_STATUS_IS_OK(status
) &&
707 !NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
))
709 DEBUG(1, ("Deleting %s failed: %s\n", path
,
715 /* (c) Delete the list of subkeys of this key */
717 path
= talloc_asprintf(ctx
, "%s/%s", key
, oldkeyname
);
721 path
= normalize_reg_path(ctx
, path
);
725 status
= dbwrap_delete_bystring(regdb
, path
);
726 /* Don't fail if the subkey record was not found. */
727 if (!NT_STATUS_IS_OK(status
) &&
728 !NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
))
730 DEBUG(1, ("Deleting %s failed: %s\n", path
,
737 TALLOC_FREE(old_subkeys
);
739 /* (2) store the subkey list for the parent */
741 if (!regdb_store_keys_internal(key
, ctr
) ) {
742 DEBUG(0,("regdb_store_keys: Failed to store new subkey list "
743 "for parent [%s]\n", key
));
747 /* (3) now create records for any subkeys that don't already exist */
749 num_subkeys
= regsubkey_ctr_numkeys(ctr
);
751 if (num_subkeys
== 0) {
752 if (!(subkeys
= TALLOC_ZERO_P(ctx
, REGSUBKEY_CTR
)) ) {
753 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
757 if (!regdb_store_keys_internal(key
, subkeys
)) {
758 DEBUG(0,("regdb_store_keys: Failed to store "
759 "new record for key [%s]\n", key
));
762 TALLOC_FREE(subkeys
);
766 for (i
=0; i
<num_subkeys
; i
++) {
767 path
= talloc_asprintf(ctx
, "%s/%s",
769 regsubkey_ctr_specific_key(ctr
, i
));
773 if (!(subkeys
= TALLOC_ZERO_P(ctx
, REGSUBKEY_CTR
)) ) {
774 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
778 if (regdb_fetch_keys( path
, subkeys
) == -1) {
779 /* create a record with 0 subkeys */
780 if (!regdb_store_keys_internal(path
, subkeys
)) {
781 DEBUG(0,("regdb_store_keys: Failed to store "
782 "new record for key [%s]\n", path
));
787 TALLOC_FREE(subkeys
);
791 if (regdb
->transaction_commit(regdb
) != 0) {
792 DEBUG(0, ("regdb_store_keys: Could not commit transaction\n"));
800 if (regdb
->transaction_cancel(regdb
) != 0) {
801 smb_panic("regdb_store_keys: transaction_cancel failed\n");
811 static TDB_DATA
regdb_fetch_key_internal(TALLOC_CTX
*mem_ctx
, const char *key
)
816 path
= normalize_reg_path(mem_ctx
, key
);
818 return make_tdb_data(NULL
, 0);
821 data
= dbwrap_fetch_bystring(regdb
, mem_ctx
, path
);
829 * check whether a given key name represents a base key,
830 * i.e one without a subkey separator ('/' or '\').
832 static bool regdb_key_is_base_key(const char *key
)
834 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
842 path
= normalize_reg_path(mem_ctx
, key
);
844 DEBUG(0, ("out of memory! (talloc failed)\n"));
852 ret
= (strrchr(path
, '/') == NULL
);
855 TALLOC_FREE(mem_ctx
);
861 * Check for the existence of a key.
863 * Existence of a key is authoritatively defined by its
864 * existence in the list of subkeys of its parent key.
865 * The exeption of this are keys without a parent key,
866 * i.e. the "base" keys (HKLM, HKCU, ...).
868 static bool regdb_key_exists(const char *key
)
870 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
879 path
= normalize_reg_path(mem_ctx
, key
);
881 DEBUG(0, ("out of memory! (talloc failed)\n"));
889 p
= strrchr(path
, '/');
891 /* this is a base key */
892 value
= regdb_fetch_key_internal(mem_ctx
, path
);
893 ret
= (value
.dptr
!= NULL
);
895 /* get the list of subkeys of the parent key */
896 uint32 num_items
, len
, i
;
901 value
= regdb_fetch_key_internal(mem_ctx
, path
);
902 if (value
.dptr
== NULL
) {
906 len
= tdb_unpack(value
.dptr
, value
.dsize
, "d", &num_items
);
907 for (i
= 0; i
< num_items
; i
++) {
908 len
+= tdb_unpack(value
.dptr
+len
, value
.dsize
-len
,
910 if (strequal(subkeyname
, p
)) {
918 TALLOC_FREE(mem_ctx
);
923 /***********************************************************************
924 Retrieve an array of strings containing subkeys. Memory should be
925 released by the caller.
926 ***********************************************************************/
928 int regdb_fetch_keys(const char *key
, REGSUBKEY_CTR
*ctr
)
937 TALLOC_CTX
*frame
= talloc_stackframe();
940 DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key
? key
: "NULL"));
942 if (!regdb_key_exists(key
)) {
946 ctr
->seqnum
= regdb_get_seqnum();
948 value
= regdb_fetch_key_internal(frame
, key
);
950 if (value
.dptr
== NULL
) {
951 DEBUG(10, ("regdb_fetch_keys: no subkeys found for key [%s]\n",
958 buflen
= value
.dsize
;
959 len
= tdb_unpack( buf
, buflen
, "d", &num_items
);
961 for (i
=0; i
<num_items
; i
++) {
962 len
+= tdb_unpack(buf
+len
, buflen
-len
, "f", subkeyname
);
963 werr
= regsubkey_ctr_addkey(ctr
, subkeyname
);
964 if (!W_ERROR_IS_OK(werr
)) {
965 DEBUG(5, ("regdb_fetch_keys: regsubkey_ctr_addkey "
966 "failed: %s\n", dos_errstr(werr
)));
971 DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items
));
979 /****************************************************************************
980 Unpack a list of registry values frem the TDB
981 ***************************************************************************/
983 static int regdb_unpack_values(REGVAL_CTR
*values
, uint8
*buf
, int buflen
)
990 uint32 num_values
= 0;
993 /* loop and unpack the rest of the registry values */
995 len
+= tdb_unpack(buf
+len
, buflen
-len
, "d", &num_values
);
997 for ( i
=0; i
<num_values
; i
++ ) {
998 /* unpack the next regval */
1003 valuename
[0] = '\0';
1004 len
+= tdb_unpack(buf
+len
, buflen
-len
, "fdB",
1010 /* add the new value. Paranoid protective code -- make sure data_p is valid */
1012 if (*valuename
&& size
&& data_p
) {
1013 regval_ctr_addvalue(values
, valuename
, type
,
1014 (const char *)data_p
, size
);
1016 SAFE_FREE(data_p
); /* 'B' option to tdb_unpack does a malloc() */
1018 DEBUG(8,("specific: [%s], len: %d\n", valuename
, size
));
1024 /****************************************************************************
1025 Pack all values in all printer keys
1026 ***************************************************************************/
1028 static int regdb_pack_values(REGVAL_CTR
*values
, uint8
*buf
, int buflen
)
1032 REGISTRY_VALUE
*val
;
1038 num_values
= regval_ctr_numvals( values
);
1040 /* pack the number of values first */
1042 len
+= tdb_pack( buf
+len
, buflen
-len
, "d", num_values
);
1044 /* loop over all values */
1046 for ( i
=0; i
<num_values
; i
++ ) {
1047 val
= regval_ctr_specific_value( values
, i
);
1048 len
+= tdb_pack(buf
+len
, buflen
-len
, "fdB",
1052 regval_data_p(val
) );
1058 /***********************************************************************
1059 Retrieve an array of strings containing subkeys. Memory should be
1060 released by the caller.
1061 ***********************************************************************/
1063 int regdb_fetch_values( const char* key
, REGVAL_CTR
*values
)
1065 char *keystr
= NULL
;
1066 TALLOC_CTX
*ctx
= talloc_stackframe();
1070 DEBUG(10,("regdb_fetch_values: Looking for value of key [%s] \n", key
));
1072 if (!regdb_key_exists(key
)) {
1076 keystr
= talloc_asprintf(ctx
, "%s/%s", REG_VALUE_PREFIX
, key
);
1081 values
->seqnum
= regdb_get_seqnum();
1083 value
= regdb_fetch_key_internal(ctx
, keystr
);
1086 /* all keys have zero values by default */
1090 regdb_unpack_values(values
, value
.dptr
, value
.dsize
);
1091 ret
= regval_ctr_numvals(values
);
1098 bool regdb_store_values( const char *key
, REGVAL_CTR
*values
)
1100 TDB_DATA old_data
, data
;
1101 char *keystr
= NULL
;
1102 TALLOC_CTX
*ctx
= talloc_stackframe();
1105 bool result
= false;
1107 DEBUG(10,("regdb_store_values: Looking for value of key [%s] \n", key
));
1109 if (!regdb_key_exists(key
)) {
1115 len
= regdb_pack_values(values
, data
.dptr
, data
.dsize
);
1117 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
1121 data
.dptr
= TALLOC_ARRAY(ctx
, uint8
, len
);
1124 len
= regdb_pack_values(values
, data
.dptr
, data
.dsize
);
1126 SMB_ASSERT( len
== data
.dsize
);
1128 keystr
= talloc_asprintf(ctx
, "%s/%s", REG_VALUE_PREFIX
, key
);
1132 keystr
= normalize_reg_path(ctx
, keystr
);
1137 old_data
= dbwrap_fetch_bystring(regdb
, ctx
, keystr
);
1139 if ((old_data
.dptr
!= NULL
)
1140 && (old_data
.dsize
== data
.dsize
)
1141 && (memcmp(old_data
.dptr
, data
.dptr
, data
.dsize
) == 0))
1147 status
= dbwrap_trans_store(regdb
, string_term_tdb_data(keystr
), data
,
1150 result
= NT_STATUS_IS_OK(status
);
1157 static WERROR
regdb_get_secdesc(TALLOC_CTX
*mem_ctx
, const char *key
,
1158 struct security_descriptor
**psecdesc
)
1163 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
1164 WERROR err
= WERR_OK
;
1166 DEBUG(10, ("regdb_get_secdesc: Getting secdesc of key [%s]\n", key
));
1168 if (!regdb_key_exists(key
)) {
1173 tdbkey
= talloc_asprintf(tmp_ctx
, "%s/%s", REG_SECDESC_PREFIX
, key
);
1174 if (tdbkey
== NULL
) {
1178 normalize_dbkey(tdbkey
);
1180 data
= dbwrap_fetch_bystring(regdb
, tmp_ctx
, tdbkey
);
1181 if (data
.dptr
== NULL
) {
1186 status
= unmarshall_sec_desc(mem_ctx
, (uint8
*)data
.dptr
, data
.dsize
,
1189 if (NT_STATUS_EQUAL(status
, NT_STATUS_NO_MEMORY
)) {
1191 } else if (!NT_STATUS_IS_OK(status
)) {
1192 err
= WERR_REG_CORRUPT
;
1196 TALLOC_FREE(tmp_ctx
);
1200 static WERROR
regdb_set_secdesc(const char *key
,
1201 struct security_descriptor
*secdesc
)
1203 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
1206 WERROR err
= WERR_NOMEM
;
1209 if (!regdb_key_exists(key
)) {
1214 tdbkey
= talloc_asprintf(mem_ctx
, "%s/%s", REG_SECDESC_PREFIX
, key
);
1215 if (tdbkey
== NULL
) {
1218 normalize_dbkey(tdbkey
);
1220 if (secdesc
== NULL
) {
1221 /* assuming a delete */
1222 status
= dbwrap_trans_delete(regdb
,
1223 string_term_tdb_data(tdbkey
));
1224 if (NT_STATUS_IS_OK(status
)) {
1227 err
= ntstatus_to_werror(status
);
1232 err
= ntstatus_to_werror(marshall_sec_desc(mem_ctx
, secdesc
,
1235 if (!W_ERROR_IS_OK(err
)) {
1239 status
= dbwrap_trans_store(regdb
, string_term_tdb_data(tdbkey
),
1241 if (!NT_STATUS_IS_OK(status
)) {
1242 err
= ntstatus_to_werror(status
);
1247 TALLOC_FREE(mem_ctx
);
1251 bool regdb_subkeys_need_update(REGSUBKEY_CTR
*subkeys
)
1253 return (regdb_get_seqnum() != subkeys
->seqnum
);
1256 bool regdb_values_need_update(REGVAL_CTR
*values
)
1258 return (regdb_get_seqnum() != values
->seqnum
);
1262 * Table of function pointers for default access
1265 REGISTRY_OPS regdb_ops
= {
1266 .fetch_subkeys
= regdb_fetch_keys
,
1267 .fetch_values
= regdb_fetch_values
,
1268 .store_subkeys
= regdb_store_keys
,
1269 .store_values
= regdb_store_values
,
1270 .get_secdesc
= regdb_get_secdesc
,
1271 .set_secdesc
= regdb_set_secdesc
,
1272 .subkeys_need_update
= regdb_subkeys_need_update
,
1273 .values_need_update
= regdb_values_need_update