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 /* List the deepest path into the registry. All part components will be created.*/
32 /* If you want to have a part of the path controlled by the tdb and part by
33 a virtual registry db (e.g. printing), then you have to list the deepest path.
34 For example,"HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion/Print"
35 allows the reg_db backend to handle everything up to
36 "HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion" and then we'll hook
37 the reg_printing backend onto the last component of the path (see
38 KEY_PRINTING_2K in include/rpc_reg.h) --jerry */
40 static const char *builtin_registry_paths
[] = {
49 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Print\\Monitors",
51 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\DefaultUserConfiguration",
60 struct builtin_regkey_value
{
62 const char *valuename
;
70 static struct builtin_regkey_value builtin_registry_values
[] = {
72 SAMBA_PRINTER_PORT_NAME
, REG_SZ
, { "" } },
74 "DefaultSpoolDirectory", REG_SZ
, { "C:\\Windows\\System32\\Spool\\Printers" } },
76 "DisplayName", REG_SZ
, { "Event Log" } },
78 "ErrorControl", REG_DWORD
, { (char*)0x00000001 } },
79 { NULL
, NULL
, 0, { NULL
} }
83 * Initialize a key in the registry:
84 * create each component key of the specified path.
86 static bool init_registry_key_internal(const char *add_path
)
89 TALLOC_CTX
*frame
= talloc_stackframe();
92 char *remaining
= NULL
;
95 REGSUBKEY_CTR
*subkeys
;
98 DEBUG(6, ("init_registry_key: Adding [%s]\n", add_path
));
100 path
= talloc_strdup(frame
, add_path
);
101 base
= talloc_strdup(frame
, "");
102 if (!path
|| !base
) {
107 while (next_token_talloc(frame
, &p
, &keyname
, "\\")) {
109 /* build up the registry path from the components */
112 base
= talloc_asprintf(frame
, "%s\\", base
);
117 base
= talloc_asprintf_append(base
, "%s", keyname
);
122 /* get the immediate subkeyname (if we have one ) */
124 subkeyname
= talloc_strdup(frame
, "");
129 remaining
= talloc_strdup(frame
, p
);
135 if (!next_token_talloc(frame
, &p2
,
138 subkeyname
= talloc_strdup(frame
,p2
);
145 DEBUG(10,("init_registry_key: Storing key [%s] with "
146 "subkey [%s]\n", base
,
147 *subkeyname
? subkeyname
: "NULL"));
149 /* we don't really care if the lookup succeeds or not
150 * since we are about to update the record.
151 * We just want any subkeys already present */
153 if (!(subkeys
= TALLOC_ZERO_P(frame
, REGSUBKEY_CTR
))) {
154 DEBUG(0,("talloc() failure!\n"));
158 regdb_fetch_keys(base
, subkeys
);
160 regsubkey_ctr_addkey( subkeys
, subkeyname
);
162 if (!regdb_store_keys( base
, subkeys
)) {
174 * Initialize a key in the registry:
175 * create each component key of the specified path,
176 * wrapped in one db transaction.
178 bool init_registry_key(const char *add_path
)
180 if (regdb
->transaction_start(regdb
) != 0) {
181 DEBUG(0, ("init_registry_key: transaction_start failed\n"));
185 if (!init_registry_key_internal(add_path
)) {
189 if (regdb
->transaction_commit(regdb
) == -1) {
190 DEBUG(0, ("init_registry_key: Could not commit transaction\n"));
197 if (regdb
->transaction_cancel(regdb
) == -1) {
198 smb_panic("init_registry_key: transaction_cancel failed\n");
204 /***********************************************************************
205 Open the registry data in the tdb
206 ***********************************************************************/
208 bool init_registry_data(void)
210 TALLOC_CTX
*frame
= NULL
;
216 * There are potentially quite a few store operations which are all
217 * indiviually wrapped in tdb transactions. Wrapping them in a single
218 * transaction gives just a single transaction_commit() to actually do
219 * its fsync()s. See tdb/common/transaction.c for info about nested
220 * transaction behaviour.
223 if (regdb
->transaction_start(regdb
) != 0) {
224 DEBUG(0, ("init_registry_data: tdb_transaction_start "
229 /* loop over all of the predefined paths and add each component */
231 for (i
=0; builtin_registry_paths
[i
] != NULL
; i
++) {
232 if (!init_registry_key_internal(builtin_registry_paths
[i
])) {
237 /* loop over all of the predefined values and add each component */
239 frame
= talloc_stackframe();
241 for (i
=0; builtin_registry_values
[i
].path
!= NULL
; i
++) {
243 values
= TALLOC_ZERO_P(frame
, REGVAL_CTR
);
244 if (values
== NULL
) {
248 regdb_fetch_values(builtin_registry_values
[i
].path
, values
);
250 /* preserve existing values across restarts. Only add new ones */
252 if (!regval_ctr_key_exists(values
,
253 builtin_registry_values
[i
].valuename
))
255 switch(builtin_registry_values
[i
].type
) {
257 regval_ctr_addvalue(values
,
258 builtin_registry_values
[i
].valuename
,
260 (char*)&builtin_registry_values
[i
].data
.dw_value
,
266 builtin_registry_values
[i
].data
.string
,
268 regval_ctr_addvalue(values
,
269 builtin_registry_values
[i
].valuename
,
272 data
.uni_str_len
*sizeof(uint16
));
276 DEBUG(0, ("init_registry_data: invalid value "
277 "type in builtin_registry_values "
279 builtin_registry_values
[i
].type
));
281 regdb_store_values(builtin_registry_values
[i
].path
,
289 if (regdb
->transaction_commit(regdb
) == -1) {
290 DEBUG(0, ("init_registry_data: Could not commit "
301 if (regdb
->transaction_cancel(regdb
) == -1) {
302 smb_panic("init_registry_data: tdb_transaction_cancel "
309 /***********************************************************************
310 Open the registry database
311 ***********************************************************************/
313 bool regdb_init(void)
315 const char *vstring
= "INFO/version";
319 DEBUG(10, ("regdb_init: incrementing refcount (%d)\n",
325 regdb
= db_open_trans(NULL
, state_path("registry.tdb"), 0,
326 REG_TDB_FLAGS
, O_RDWR
, 0600);
328 regdb
= db_open_trans(NULL
, state_path("registry.tdb"), 0,
329 REG_TDB_FLAGS
, O_RDWR
|O_CREAT
, 0600);
331 DEBUG(0,("regdb_init: Failed to open registry %s (%s)\n",
332 state_path("registry.tdb"), strerror(errno
) ));
336 DEBUG(10,("regdb_init: Successfully created registry tdb\n"));
341 vers_id
= dbwrap_fetch_int32(regdb
, vstring
);
343 if ( vers_id
!= REGVER_V1
) {
344 /* any upgrade code here if needed */
345 DEBUG(10, ("regdb_init: got %s = %d != %d\n", vstring
,
346 vers_id
, REGVER_V1
));
347 if (dbwrap_trans_store_int32(regdb
, vstring
, REGVER_V1
) != 0) {
348 DEBUG(0, ("regdb_init: error storing %s = %d\n",
349 vstring
, REGVER_V1
));
352 DEBUG(10, ("regdb_init: stored %s = %d\n",
353 vstring
, REGVER_V1
));
360 /***********************************************************************
361 Open the registry. Must already have been initialized by regdb_init()
362 ***********************************************************************/
364 WERROR
regdb_open( void )
366 WERROR result
= WERR_OK
;
369 DEBUG(10,("regdb_open: incrementing refcount (%d)\n", regdb_refcount
));
376 regdb
= db_open_trans(NULL
, state_path("registry.tdb"), 0,
377 REG_TDB_FLAGS
, O_RDWR
, 0600);
379 result
= ntstatus_to_werror( map_nt_error_from_unix( errno
) );
380 DEBUG(0,("regdb_open: Failed to open %s! (%s)\n",
381 state_path("registry.tdb"), strerror(errno
) ));
387 DEBUG(10,("regdb_open: refcount reset (%d)\n", regdb_refcount
));
392 /***********************************************************************
393 ***********************************************************************/
395 int regdb_close( void )
397 if (regdb_refcount
== 0) {
403 DEBUG(10,("regdb_close: decrementing refcount (%d)\n", regdb_refcount
));
405 if ( regdb_refcount
> 0 )
408 SMB_ASSERT( regdb_refcount
>= 0 );
414 /***********************************************************************
415 return the tdb sequence number of the registry tdb.
416 this is an indicator for the content of the registry
417 having changed. it will change upon regdb_init, too, though.
418 ***********************************************************************/
419 int regdb_get_seqnum(void)
421 return regdb
->get_seqnum(regdb
);
424 /***********************************************************************
425 Add subkey strings to the registry tdb under a defined key
426 fmt is the same format as tdb_pack except this function only supports
428 ***********************************************************************/
430 static bool regdb_store_keys_internal(const char *key
, REGSUBKEY_CTR
*ctr
)
433 uint8
*buffer
= NULL
;
437 uint32 num_subkeys
= regsubkey_ctr_numkeys(ctr
);
438 char *keyname
= NULL
;
439 TALLOC_CTX
*ctx
= talloc_stackframe();
446 keyname
= talloc_strdup(ctx
, key
);
450 keyname
= normalize_reg_path(ctx
, keyname
);
452 /* allocate some initial memory */
454 buffer
= (uint8
*)SMB_MALLOC(1024);
455 if (buffer
== NULL
) {
461 /* store the number of subkeys */
463 len
+= tdb_pack(buffer
+len
, buflen
-len
, "d", num_subkeys
);
465 /* pack all the strings */
467 for (i
=0; i
<num_subkeys
; i
++) {
468 len
+= tdb_pack(buffer
+len
, buflen
-len
, "f",
469 regsubkey_ctr_specific_key(ctr
, i
));
471 /* allocate some extra space */
472 buffer
= (uint8
*)SMB_REALLOC(buffer
, len
*2);
474 DEBUG(0, ("regdb_store_keys: Failed to realloc "
475 "memory of size [%d]\n", len
*2));
480 len
= tdb_pack(buffer
+len
, buflen
-len
, "f",
481 regsubkey_ctr_specific_key(ctr
, i
));
485 /* finally write out the data */
489 status
= dbwrap_store_bystring(regdb
, keyname
, dbuf
, TDB_REPLACE
);
490 if (!NT_STATUS_IS_OK(status
)) {
501 /***********************************************************************
502 Store the new subkey record and create any child key records that
503 do not currently exist
504 ***********************************************************************/
506 bool regdb_store_keys(const char *key
, REGSUBKEY_CTR
*ctr
)
510 REGSUBKEY_CTR
*subkeys
= NULL
, *old_subkeys
= NULL
;
511 char *oldkeyname
= NULL
;
512 TALLOC_CTX
*ctx
= talloc_stackframe();
516 * fetch a list of the old subkeys so we can determine if anything has
520 if (!(old_subkeys
= TALLOC_ZERO_P(ctx
, REGSUBKEY_CTR
))) {
521 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
525 regdb_fetch_keys(key
, old_subkeys
);
527 if ((ctr
->num_subkeys
&& old_subkeys
->num_subkeys
) &&
528 (ctr
->num_subkeys
== old_subkeys
->num_subkeys
)) {
530 for (i
= 0; i
<ctr
->num_subkeys
; i
++) {
531 if (strcmp(ctr
->subkeys
[i
],
532 old_subkeys
->subkeys
[i
]) != 0) {
536 if (i
== ctr
->num_subkeys
) {
538 * Nothing changed, no point to even start a tdb
541 TALLOC_FREE(old_subkeys
);
546 TALLOC_FREE(old_subkeys
);
548 if (regdb
->transaction_start(regdb
) != 0) {
549 DEBUG(0, ("regdb_store_keys: transaction_start failed\n"));
554 * Re-fetch the old keys inside the transaction
557 if (!(old_subkeys
= TALLOC_ZERO_P(ctx
, REGSUBKEY_CTR
))) {
558 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
562 regdb_fetch_keys(key
, old_subkeys
);
564 /* store the subkey list for the parent */
566 if (!regdb_store_keys_internal(key
, ctr
) ) {
567 DEBUG(0,("regdb_store_keys: Failed to store new subkey list "
568 "for parent [%s]\n", key
));
572 /* now delete removed keys */
574 num_subkeys
= regsubkey_ctr_numkeys(old_subkeys
);
575 for (i
=0; i
<num_subkeys
; i
++) {
576 oldkeyname
= regsubkey_ctr_specific_key(old_subkeys
, i
);
578 if (regsubkey_ctr_key_exists(ctr
, oldkeyname
)) {
580 * It's still around, don't delete
586 path
= talloc_asprintf(ctx
, "%s/%s", key
, oldkeyname
);
590 path
= normalize_reg_path(ctx
, path
);
594 status
= dbwrap_delete_bystring(regdb
, path
);
595 if (!NT_STATUS_IS_OK(status
)) {
596 DEBUG(1, ("Deleting %s failed\n", path
));
601 path
= talloc_asprintf(ctx
, "%s/%s/%s",
608 path
= normalize_reg_path(ctx
, path
);
614 * Ignore errors here, we might have no values around
616 dbwrap_delete_bystring(regdb
, path
);
620 TALLOC_FREE(old_subkeys
);
622 /* now create records for any subkeys that don't already exist */
624 num_subkeys
= regsubkey_ctr_numkeys(ctr
);
626 if (num_subkeys
== 0) {
627 if (!(subkeys
= TALLOC_ZERO_P(ctx
, REGSUBKEY_CTR
)) ) {
628 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
632 if (!regdb_store_keys_internal(key
, subkeys
)) {
633 DEBUG(0,("regdb_store_keys: Failed to store "
634 "new record for key [%s]\n", key
));
637 TALLOC_FREE(subkeys
);
641 for (i
=0; i
<num_subkeys
; i
++) {
642 path
= talloc_asprintf(ctx
, "%s/%s",
644 regsubkey_ctr_specific_key(ctr
, i
));
648 if (!(subkeys
= TALLOC_ZERO_P(ctx
, REGSUBKEY_CTR
)) ) {
649 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
653 if (regdb_fetch_keys( path
, subkeys
) == -1) {
654 /* create a record with 0 subkeys */
655 if (!regdb_store_keys_internal(path
, subkeys
)) {
656 DEBUG(0,("regdb_store_keys: Failed to store "
657 "new record for key [%s]\n", path
));
662 TALLOC_FREE(subkeys
);
666 if (regdb
->transaction_commit(regdb
) == -1) {
667 DEBUG(0, ("regdb_store_keys: Could not commit transaction\n"));
675 if (regdb
->transaction_cancel(regdb
) == -1) {
676 smb_panic("regdb_store_keys: transaction_cancel failed\n");
686 /***********************************************************************
687 Retrieve an array of strings containing subkeys. Memory should be
688 released by the caller.
689 ***********************************************************************/
691 int regdb_fetch_keys(const char *key
, REGSUBKEY_CTR
*ctr
)
700 TALLOC_CTX
*frame
= talloc_stackframe();
703 DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key
? key
: "NULL"));
705 path
= talloc_strdup(frame
, key
);
710 /* convert to key format */
711 path
= talloc_string_sub(frame
, path
, "\\", "/");
717 ret
= regdb
->fetch(regdb
, frame
, string_term_tdb_data(path
), &value
);
723 ctr
->seqnum
= regdb_get_seqnum();
726 buflen
= value
.dsize
;
729 DEBUG(5,("regdb_fetch_keys: tdb lookup failed to locate key [%s]\n", key
));
733 len
= tdb_unpack( buf
, buflen
, "d", &num_items
);
735 for (i
=0; i
<num_items
; i
++) {
736 len
+= tdb_unpack(buf
+len
, buflen
-len
, "f", subkeyname
);
737 regsubkey_ctr_addkey(ctr
, subkeyname
);
740 DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items
));
748 /****************************************************************************
749 Unpack a list of registry values frem the TDB
750 ***************************************************************************/
752 static int regdb_unpack_values(REGVAL_CTR
*values
, uint8
*buf
, int buflen
)
759 uint32 num_values
= 0;
762 /* loop and unpack the rest of the registry values */
764 len
+= tdb_unpack(buf
+len
, buflen
-len
, "d", &num_values
);
766 for ( i
=0; i
<num_values
; i
++ ) {
767 /* unpack the next regval */
773 len
+= tdb_unpack(buf
+len
, buflen
-len
, "fdB",
779 /* add the new value. Paranoid protective code -- make sure data_p is valid */
781 if (*valuename
&& size
&& data_p
) {
782 regval_ctr_addvalue(values
, valuename
, type
,
783 (const char *)data_p
, size
);
785 SAFE_FREE(data_p
); /* 'B' option to tdb_unpack does a malloc() */
787 DEBUG(8,("specific: [%s], len: %d\n", valuename
, size
));
793 /****************************************************************************
794 Pack all values in all printer keys
795 ***************************************************************************/
797 static int regdb_pack_values(REGVAL_CTR
*values
, uint8
*buf
, int buflen
)
807 num_values
= regval_ctr_numvals( values
);
809 /* pack the number of values first */
811 len
+= tdb_pack( buf
+len
, buflen
-len
, "d", num_values
);
813 /* loop over all values */
815 for ( i
=0; i
<num_values
; i
++ ) {
816 val
= regval_ctr_specific_value( values
, i
);
817 len
+= tdb_pack(buf
+len
, buflen
-len
, "fdB",
821 regval_data_p(val
) );
827 /***********************************************************************
828 Retrieve an array of strings containing subkeys. Memory should be
829 released by the caller.
830 ***********************************************************************/
832 int regdb_fetch_values( const char* key
, REGVAL_CTR
*values
)
835 TALLOC_CTX
*ctx
= talloc_stackframe();
839 DEBUG(10,("regdb_fetch_values: Looking for value of key [%s] \n", key
));
841 keystr
= talloc_asprintf(ctx
, "%s/%s", REG_VALUE_PREFIX
, key
);
845 keystr
= normalize_reg_path(ctx
, keystr
);
850 ret
= regdb
->fetch(regdb
, ctx
, string_term_tdb_data(keystr
), &value
);
856 values
->seqnum
= regdb_get_seqnum();
859 /* all keys have zero values by default */
863 regdb_unpack_values(values
, value
.dptr
, value
.dsize
);
864 ret
= regval_ctr_numvals(values
);
871 bool regdb_store_values( const char *key
, REGVAL_CTR
*values
)
873 TDB_DATA old_data
, data
;
875 TALLOC_CTX
*ctx
= talloc_stackframe();
879 DEBUG(10,("regdb_store_values: Looking for value of key [%s] \n", key
));
883 len
= regdb_pack_values(values
, data
.dptr
, data
.dsize
);
885 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
889 data
.dptr
= TALLOC_ARRAY(ctx
, uint8
, len
);
892 len
= regdb_pack_values(values
, data
.dptr
, data
.dsize
);
894 SMB_ASSERT( len
== data
.dsize
);
896 keystr
= talloc_asprintf(ctx
, "%s/%s", REG_VALUE_PREFIX
, key
);
900 keystr
= normalize_reg_path(ctx
, keystr
);
905 old_data
= dbwrap_fetch_bystring(regdb
, ctx
, keystr
);
907 if ((old_data
.dptr
!= NULL
)
908 && (old_data
.dsize
== data
.dsize
)
909 && (memcmp(old_data
.dptr
, data
.dptr
, data
.dsize
) == 0))
915 ret
= dbwrap_trans_store(regdb
, string_term_tdb_data(keystr
), data
,
917 result
= (ret
!= -1);
924 static WERROR
regdb_get_secdesc(TALLOC_CTX
*mem_ctx
, const char *key
,
925 struct security_descriptor
**psecdesc
)
930 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
931 WERROR err
= WERR_OK
;
933 DEBUG(10, ("regdb_get_secdesc: Getting secdesc of key [%s]\n", key
));
935 tdbkey
= talloc_asprintf(tmp_ctx
, "%s/%s", REG_SECDESC_PREFIX
, key
);
936 if (tdbkey
== NULL
) {
940 normalize_dbkey(tdbkey
);
942 data
= dbwrap_fetch_bystring(regdb
, tmp_ctx
, tdbkey
);
943 if (data
.dptr
== NULL
) {
948 status
= unmarshall_sec_desc(mem_ctx
, (uint8
*)data
.dptr
, data
.dsize
,
951 if (NT_STATUS_EQUAL(status
, NT_STATUS_NO_MEMORY
)) {
953 } else if (!NT_STATUS_IS_OK(status
)) {
954 err
= WERR_REG_CORRUPT
;
958 TALLOC_FREE(tmp_ctx
);
962 static WERROR
regdb_set_secdesc(const char *key
,
963 struct security_descriptor
*secdesc
)
965 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
967 WERROR err
= WERR_NOMEM
;
971 tdbkey
= talloc_asprintf(mem_ctx
, "%s/%s", REG_SECDESC_PREFIX
, key
);
972 if (tdbkey
== NULL
) {
975 normalize_dbkey(tdbkey
);
977 if (secdesc
== NULL
) {
978 /* assuming a delete */
979 tdb_ret
= dbwrap_trans_delete(regdb
,
980 string_term_tdb_data(tdbkey
));
982 err
= ntstatus_to_werror(map_nt_error_from_unix(errno
));
989 err
= ntstatus_to_werror(marshall_sec_desc(mem_ctx
, secdesc
,
992 if (!W_ERROR_IS_OK(err
)) {
996 tdb_ret
= dbwrap_trans_store(regdb
, string_term_tdb_data(tdbkey
),
999 err
= ntstatus_to_werror(map_nt_error_from_unix(errno
));
1004 TALLOC_FREE(mem_ctx
);
1008 bool regdb_subkeys_need_update(REGSUBKEY_CTR
*subkeys
)
1010 return (regdb_get_seqnum() != subkeys
->seqnum
);
1013 bool regdb_values_need_update(REGVAL_CTR
*values
)
1015 return (regdb_get_seqnum() != values
->seqnum
);
1019 * Table of function pointers for default access
1022 REGISTRY_OPS regdb_ops
= {
1023 .fetch_subkeys
= regdb_fetch_keys
,
1024 .fetch_values
= regdb_fetch_values
,
1025 .store_subkeys
= regdb_store_keys
,
1026 .store_values
= regdb_store_values
,
1027 .get_secdesc
= regdb_get_secdesc
,
1028 .set_secdesc
= regdb_set_secdesc
,
1029 .subkeys_need_update
= regdb_subkeys_need_update
,
1030 .values_need_update
= regdb_values_need_update