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 tdb_wrap
*tdb_reg
= NULL
;
28 static int tdb_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
[] = {
47 "HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Perflib",
48 "HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Perflib\\009",
49 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Print\\Monitors",
50 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\ProductOptions",
51 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\DefaultUserConfiguration",
52 "HKLM\\SYSTEM\\CurrentControlSet\\Services\\TcpIp\\Parameters",
53 "HKLM\\SYSTEM\\CurrentControlSet\\Services\\Netlogon\\Parameters",
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
} }
82 /***********************************************************************
83 Open the registry data in the tdb
84 ***********************************************************************/
86 static BOOL
init_registry_data( void )
88 pstring path
, base
, remaining
;
89 fstring keyname
, subkeyname
;
90 REGSUBKEY_CTR
*subkeys
;
97 * There are potentially quite a few store operations which are all
98 * indiviually wrapped in tdb transactions. Wrapping them in a single
99 * transaction gives just a single transaction_commit() to actually do
100 * its fsync()s. See tdb/common/transaction.c for info about nested
101 * transaction behaviour.
104 if ( tdb_transaction_start( tdb_reg
->tdb
) == -1 ) {
105 DEBUG(0, ("init_registry_data: tdb_transaction_start "
110 /* loop over all of the predefined paths and add each component */
112 for ( i
=0; builtin_registry_paths
[i
] != NULL
; i
++ ) {
114 DEBUG(6,("init_registry_data: Adding [%s]\n", builtin_registry_paths
[i
]));
116 pstrcpy( path
, builtin_registry_paths
[i
] );
120 while ( next_token(&p
, keyname
, "\\", sizeof(keyname
)) ) {
122 /* build up the registry path from the components */
125 pstrcat( base
, "\\" );
126 pstrcat( base
, keyname
);
128 /* get the immediate subkeyname (if we have one ) */
132 pstrcpy( remaining
, p
);
135 if ( !next_token(&p2
, subkeyname
, "\\", sizeof(subkeyname
)) )
136 fstrcpy( subkeyname
, p2
);
139 DEBUG(10,("init_registry_data: Storing key [%s] with subkey [%s]\n",
140 base
, *subkeyname
? subkeyname
: "NULL"));
142 /* we don't really care if the lookup succeeds or not since
143 we are about to update the record. We just want any
144 subkeys already present */
146 if ( !(subkeys
= TALLOC_ZERO_P( NULL
, REGSUBKEY_CTR
)) ) {
147 DEBUG(0,("talloc() failure!\n"));
151 regdb_fetch_keys( base
, subkeys
);
153 regsubkey_ctr_addkey( subkeys
, subkeyname
);
154 if ( !regdb_store_keys( base
, subkeys
))
157 TALLOC_FREE( subkeys
);
161 /* loop over all of the predefined values and add each component */
163 for ( i
=0; builtin_registry_values
[i
].path
!= NULL
; i
++ ) {
164 if ( !(values
= TALLOC_ZERO_P( NULL
, REGVAL_CTR
)) ) {
165 DEBUG(0,("talloc() failure!\n"));
169 regdb_fetch_values( builtin_registry_values
[i
].path
, values
);
171 /* preserve existing values across restarts. Only add new ones */
173 if ( !regval_ctr_key_exists( values
, builtin_registry_values
[i
].valuename
) )
175 switch( builtin_registry_values
[i
].type
) {
177 regval_ctr_addvalue( values
,
178 builtin_registry_values
[i
].valuename
,
180 (char*)&builtin_registry_values
[i
].data
.dw_value
,
185 init_unistr2( &data
, builtin_registry_values
[i
].data
.string
, UNI_STR_TERMINATE
);
186 regval_ctr_addvalue( values
,
187 builtin_registry_values
[i
].valuename
,
190 data
.uni_str_len
*sizeof(uint16
) );
194 DEBUG(0,("init_registry_data: invalid value type in builtin_registry_values [%d]\n",
195 builtin_registry_values
[i
].type
));
197 regdb_store_values( builtin_registry_values
[i
].path
, values
);
200 TALLOC_FREE( values
);
203 if (tdb_transaction_commit( tdb_reg
->tdb
) == -1) {
204 DEBUG(0, ("init_registry_data: Could not commit "
213 if (tdb_transaction_cancel( tdb_reg
->tdb
) == -1) {
214 smb_panic("init_registry_data: tdb_transaction_cancel "
221 /***********************************************************************
222 Open the registry database
223 ***********************************************************************/
225 BOOL
regdb_init( void )
227 const char *vstring
= "INFO/version";
233 if ( !(tdb_reg
= tdb_wrap_open(NULL
, lock_path("registry.tdb"), 0, REG_TDB_FLAGS
, O_RDWR
, 0600)) )
235 tdb_reg
= tdb_wrap_open(NULL
, lock_path("registry.tdb"), 0, REG_TDB_FLAGS
, O_RDWR
|O_CREAT
, 0600);
237 DEBUG(0,("regdb_init: Failed to open registry %s (%s)\n",
238 lock_path("registry.tdb"), strerror(errno
) ));
242 DEBUG(10,("regdb_init: Successfully created registry tdb\n"));
247 vers_id
= tdb_fetch_int32(tdb_reg
->tdb
, vstring
);
249 if ( vers_id
!= REGVER_V1
) {
250 /* any upgrade code here if needed */
251 DEBUG(10, ("regdb_init: got INFO/version = %d != %d\n",
252 vers_id
, REGVER_V1
));
255 /* always setup the necessary keys and values */
257 if ( !init_registry_data() ) {
258 DEBUG(0,("init_registry: Failed to initialize data in registry!\n"));
265 /***********************************************************************
266 Open the registry. Must already have been initialized by regdb_init()
267 ***********************************************************************/
269 WERROR
regdb_open( void )
271 WERROR result
= WERR_OK
;
274 DEBUG(10,("regdb_open: incrementing refcount (%d)\n", tdb_refcount
));
281 tdb_reg
= tdb_wrap_open(NULL
, lock_path("registry.tdb"), 0, REG_TDB_FLAGS
, O_RDWR
, 0600);
283 result
= ntstatus_to_werror( map_nt_error_from_unix( errno
) );
284 DEBUG(0,("regdb_open: Failed to open %s! (%s)\n",
285 lock_path("registry.tdb"), strerror(errno
) ));
291 DEBUG(10,("regdb_open: refcount reset (%d)\n", tdb_refcount
));
296 /***********************************************************************
297 ***********************************************************************/
299 int regdb_close( void )
303 DEBUG(10,("regdb_close: decrementing refcount (%d)\n", tdb_refcount
));
305 if ( tdb_refcount
> 0 )
308 SMB_ASSERT( tdb_refcount
>= 0 );
310 TALLOC_FREE(tdb_reg
);
314 /***********************************************************************
315 return the tdb sequence number of the registry tdb.
316 this is an indicator for the content of the registry
317 having changed. it will change upon regdb_init, too, though.
318 ***********************************************************************/
319 int regdb_get_seqnum(void)
321 return tdb_get_seqnum(tdb_reg
->tdb
);
324 /***********************************************************************
325 Add subkey strings to the registry tdb under a defined key
326 fmt is the same format as tdb_pack except this function only supports
328 ***********************************************************************/
330 static BOOL
regdb_store_keys_internal( const char *key
, REGSUBKEY_CTR
*ctr
)
337 uint32 num_subkeys
= regsubkey_ctr_numkeys( ctr
);
343 pstrcpy( keyname
, key
);
344 normalize_reg_path( keyname
);
346 /* allocate some initial memory */
348 if (!(buffer
= (uint8
*)SMB_MALLOC(sizeof(pstring
)))) {
351 buflen
= sizeof(pstring
);
354 /* store the number of subkeys */
356 len
+= tdb_pack(buffer
+len
, buflen
-len
, "d", num_subkeys
);
358 /* pack all the strings */
360 for (i
=0; i
<num_subkeys
; i
++) {
361 len
+= tdb_pack( buffer
+len
, buflen
-len
, "f", regsubkey_ctr_specific_key(ctr
, i
) );
362 if ( len
> buflen
) {
363 /* allocate some extra space */
364 if ((buffer
= (uint8
*)SMB_REALLOC( buffer
, len
*2 )) == NULL
) {
365 DEBUG(0,("regdb_store_keys: Failed to realloc memory of size [%d]\n", len
*2));
371 len
= tdb_pack( buffer
+len
, buflen
-len
, "f", regsubkey_ctr_specific_key(ctr
, i
) );
375 /* finally write out the data */
379 if ( tdb_store_bystring( tdb_reg
->tdb
, keyname
, dbuf
, TDB_REPLACE
) == -1) {
390 /***********************************************************************
391 Store the new subkey record and create any child key records that
392 do not currently exist
393 ***********************************************************************/
395 BOOL
regdb_store_keys( const char *key
, REGSUBKEY_CTR
*ctr
)
399 REGSUBKEY_CTR
*subkeys
= NULL
, *old_subkeys
= NULL
;
403 * fetch a list of the old subkeys so we can determine if anything has
407 if ( !(old_subkeys
= TALLOC_ZERO_P( ctr
, REGSUBKEY_CTR
)) ) {
408 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
412 regdb_fetch_keys( key
, old_subkeys
);
414 if (ctr
->num_subkeys
== old_subkeys
->num_subkeys
) {
416 for (i
= 0; i
<ctr
->num_subkeys
; i
++) {
417 if (strcmp(ctr
->subkeys
[i
],
418 old_subkeys
->subkeys
[i
]) != 0) {
422 if (i
== ctr
->num_subkeys
) {
424 * Nothing changed, no point to even start a tdb
427 TALLOC_FREE(old_subkeys
);
432 if ( tdb_transaction_start( tdb_reg
->tdb
) == -1 ) {
433 DEBUG(0, ("regdb_store_keys: tdb_transaction_start failed\n"));
438 * Re-fetch the old keys inside the transaction
441 TALLOC_FREE(old_subkeys
);
443 if ( !(old_subkeys
= TALLOC_ZERO_P( ctr
, REGSUBKEY_CTR
)) ) {
444 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
448 regdb_fetch_keys( key
, old_subkeys
);
450 /* store the subkey list for the parent */
452 if ( !regdb_store_keys_internal( key
, ctr
) ) {
453 DEBUG(0,("regdb_store_keys: Failed to store new subkey list "
454 "for parent [%s]\n", key
));
458 /* now delete removed keys */
460 num_subkeys
= regsubkey_ctr_numkeys( old_subkeys
);
461 for ( i
=0; i
<num_subkeys
; i
++ ) {
462 oldkeyname
= regsubkey_ctr_specific_key( old_subkeys
, i
);
464 if ( regsubkey_ctr_key_exists( ctr
, oldkeyname
) ) {
466 * It's still around, don't delete
472 pstr_sprintf( path
, "%s/%s", key
, oldkeyname
);
473 normalize_reg_path( path
);
474 if (tdb_delete_bystring( tdb_reg
->tdb
, path
) == -1) {
475 DEBUG(1, ("Deleting %s failed\n", path
));
479 pstr_sprintf( path
, "%s/%s/%s", REG_VALUE_PREFIX
, key
,
481 normalize_reg_path( path
);
484 * Ignore errors here, we might have no values around
486 tdb_delete_bystring( tdb_reg
->tdb
, path
);
489 TALLOC_FREE( old_subkeys
);
491 /* now create records for any subkeys that don't already exist */
493 num_subkeys
= regsubkey_ctr_numkeys( ctr
);
494 for ( i
=0; i
<num_subkeys
; i
++ ) {
495 pstr_sprintf( path
, "%s/%s", key
,
496 regsubkey_ctr_specific_key( ctr
, i
) );
498 if ( !(subkeys
= TALLOC_ZERO_P( ctr
, REGSUBKEY_CTR
)) ) {
499 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
503 if ( regdb_fetch_keys( path
, subkeys
) == -1 ) {
504 /* create a record with 0 subkeys */
505 if ( !regdb_store_keys_internal( path
, subkeys
) ) {
506 DEBUG(0,("regdb_store_keys: Failed to store "
507 "new record for key [%s]\n", path
));
512 TALLOC_FREE( subkeys
);
515 if (tdb_transaction_commit( tdb_reg
->tdb
) == -1) {
516 DEBUG(0, ("regdb_store_keys: Could not commit transaction\n"));
523 TALLOC_FREE( old_subkeys
);
524 TALLOC_FREE( subkeys
);
526 if (tdb_transaction_cancel( tdb_reg
->tdb
) == -1) {
527 smb_panic("regdb_store_keys: tdb_transaction_cancel failed\n");
534 /***********************************************************************
535 Retrieve an array of strings containing subkeys. Memory should be
536 released by the caller.
537 ***********************************************************************/
539 int regdb_fetch_keys( const char* key
, REGSUBKEY_CTR
*ctr
)
549 DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key
? key
: "NULL"));
551 pstrcpy( path
, key
);
553 /* convert to key format */
554 pstring_sub( path
, "\\", "/" );
557 dbuf
= tdb_fetch_bystring( tdb_reg
->tdb
, path
);
563 DEBUG(5,("regdb_fetch_keys: tdb lookup failed to locate key [%s]\n", key
));
567 len
= tdb_unpack( buf
, buflen
, "d", &num_items
);
569 for (i
=0; i
<num_items
; i
++) {
570 len
+= tdb_unpack( buf
+len
, buflen
-len
, "f", subkeyname
);
571 regsubkey_ctr_addkey( ctr
, subkeyname
);
574 SAFE_FREE( dbuf
.dptr
);
576 DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items
));
581 /****************************************************************************
582 Unpack a list of registry values frem the TDB
583 ***************************************************************************/
585 static int regdb_unpack_values(REGVAL_CTR
*values
, uint8
*buf
, int buflen
)
592 uint32 num_values
= 0;
597 /* loop and unpack the rest of the registry values */
599 len
+= tdb_unpack(buf
+len
, buflen
-len
, "d", &num_values
);
601 for ( i
=0; i
<num_values
; i
++ ) {
602 /* unpack the next regval */
607 len
+= tdb_unpack(buf
+len
, buflen
-len
, "fdB",
613 /* add the new value. Paranoid protective code -- make sure data_p is valid */
615 if ( size
&& data_p
) {
616 regval_ctr_addvalue( values
, valuename
, type
, (const char *)data_p
, size
);
617 SAFE_FREE(data_p
); /* 'B' option to tdb_unpack does a malloc() */
620 DEBUG(8,("specific: [%s], len: %d\n", valuename
, size
));
626 /****************************************************************************
627 Pack all values in all printer keys
628 ***************************************************************************/
630 static int regdb_pack_values(REGVAL_CTR
*values
, uint8
*buf
, int buflen
)
640 num_values
= regval_ctr_numvals( values
);
642 /* pack the number of values first */
644 len
+= tdb_pack( buf
+len
, buflen
-len
, "d", num_values
);
646 /* loop over all values */
648 for ( i
=0; i
<num_values
; i
++ ) {
649 val
= regval_ctr_specific_value( values
, i
);
650 len
+= tdb_pack(buf
+len
, buflen
-len
, "fdB",
654 regval_data_p(val
) );
660 /***********************************************************************
661 Retrieve an array of strings containing subkeys. Memory should be
662 released by the caller.
663 ***********************************************************************/
665 int regdb_fetch_values( const char* key
, REGVAL_CTR
*values
)
670 DEBUG(10,("regdb_fetch_values: Looking for value of key [%s] \n", key
));
672 pstr_sprintf( keystr
, "%s/%s", REG_VALUE_PREFIX
, key
);
673 normalize_reg_path( keystr
);
675 data
= tdb_fetch_bystring( tdb_reg
->tdb
, keystr
);
678 /* all keys have zero values by default */
682 regdb_unpack_values( values
, data
.dptr
, data
.dsize
);
684 SAFE_FREE( data
.dptr
);
686 return regval_ctr_numvals(values
);
689 /***********************************************************************
690 Stub function since we do not currently support storing registry
691 values in the registry.tdb
692 ***********************************************************************/
694 BOOL
regdb_store_values( const char *key
, REGVAL_CTR
*values
)
696 TDB_DATA old_data
, data
;
700 DEBUG(10,("regdb_store_values: Looking for value of key [%s] \n", key
));
704 len
= regdb_pack_values( values
, data
.dptr
, data
.dsize
);
706 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
710 data
.dptr
= SMB_MALLOC_ARRAY( uint8
, len
);
713 len
= regdb_pack_values( values
, data
.dptr
, data
.dsize
);
715 SMB_ASSERT( len
== data
.dsize
);
717 pstr_sprintf( keystr
, "%s/%s", REG_VALUE_PREFIX
, key
);
718 normalize_reg_path( keystr
);
720 old_data
= tdb_fetch_bystring(tdb_reg
->tdb
, keystr
);
722 if ((old_data
.dptr
!= NULL
)
723 && (old_data
.dsize
== data
.dsize
)
724 && (memcmp(old_data
.dptr
, data
.dptr
, data
.dsize
) == 0)) {
725 SAFE_FREE(old_data
.dptr
);
726 SAFE_FREE(data
.dptr
);
730 ret
= tdb_trans_store_bystring(tdb_reg
->tdb
, keystr
, data
, TDB_REPLACE
);
732 SAFE_FREE( old_data
.dptr
);
733 SAFE_FREE( data
.dptr
);
738 static WERROR
regdb_get_secdesc(TALLOC_CTX
*mem_ctx
, const char *key
,
739 struct security_descriptor
**psecdesc
)
745 DEBUG(10, ("regdb_get_secdesc: Getting secdesc of key [%s]\n", key
));
747 if (asprintf(&tdbkey
, "%s/%s", REG_SECDESC_PREFIX
, key
) == -1) {
750 normalize_dbkey(tdbkey
);
752 data
= tdb_fetch_bystring(tdb_reg
->tdb
, tdbkey
);
755 if (data
.dptr
== NULL
) {
759 status
= unmarshall_sec_desc(mem_ctx
, (uint8
*)data
.dptr
, data
.dsize
,
762 SAFE_FREE(data
.dptr
);
764 if (NT_STATUS_EQUAL(status
, NT_STATUS_NO_MEMORY
)) {
768 if (!NT_STATUS_IS_OK(status
)) {
769 return WERR_REG_CORRUPT
;
775 static WERROR
regdb_set_secdesc(const char *key
,
776 struct security_descriptor
*secdesc
)
781 WERROR err
= WERR_NOMEM
;
784 if (!(mem_ctx
= talloc_init("regdb_set_secdesc"))) {
790 if (!(tdbkey
= talloc_asprintf(mem_ctx
, "%s/%s", REG_SECDESC_PREFIX
,
794 normalize_dbkey(tdbkey
);
796 err
= ntstatus_to_werror(marshall_sec_desc(mem_ctx
, secdesc
,
799 if (!W_ERROR_IS_OK(err
)) {
803 if (tdb_trans_store_bystring(tdb_reg
->tdb
, tdbkey
, tdbdata
, 0) == -1) {
804 err
= ntstatus_to_werror(map_nt_error_from_unix(errno
));
810 TALLOC_FREE(mem_ctx
);
815 * Table of function pointers for default access
818 REGISTRY_OPS regdb_ops
= {