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 2 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, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 /* Implementation of internal registry database functions. */
26 #define DBGC_CLASS DBGC_RPC_SRV
28 static struct tdb_wrap *tdb_reg = NULL;
29 static int tdb_refcount;
31 #define VALUE_PREFIX "SAMBA_REGVAL"
32 #define SECDESC_PREFIX "SAMBA_SECDESC"
34 #define REG_TDB_FLAGS TDB_SEQNUM
36 /* List the deepest path into the registry. All part components will be created.*/
38 /* If you want to have a part of the path controlled by the tdb and part by
39 a virtual registry db (e.g. printing), then you have to list the deepest path.
40 For example,"HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion/Print"
41 allows the reg_db backend to handle everything up to
42 "HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion" and then we'll hook
43 the reg_printing backend onto the last component of the path (see
44 KEY_PRINTING_2K in include/rpc_reg.h) --jerry */
46 static const char *builtin_registry_paths[] = {
53 "HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Perflib",
54 "HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Perflib\\009",
55 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Print\\Monitors",
56 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\ProductOptions",
57 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\DefaultUserConfiguration",
58 "HKLM\\SYSTEM\\CurrentControlSet\\Services\\TcpIp\\Parameters",
59 "HKLM\\SYSTEM\\CurrentControlSet\\Services\\Netlogon\\Parameters",
66 struct builtin_regkey_value {
68 const char *valuename;
76 static struct builtin_regkey_value builtin_registry_values[] = {
78 SAMBA_PRINTER_PORT_NAME, REG_SZ, { "" } },
80 "DefaultSpoolDirectory", REG_SZ, { "C:\\Windows\\System32\\Spool\\Printers" } },
82 "DisplayName", REG_SZ, { "Event Log" } },
84 "ErrorControl", REG_DWORD, { (char*)0x00000001 } },
85 { NULL, NULL, 0, { NULL } }
88 #define REGVER_V1 1 /* first db version with write support */
90 /***********************************************************************
91 Open the registry data in the tdb
92 ***********************************************************************/
94 static BOOL init_registry_data( void )
96 pstring path, base, remaining;
97 fstring keyname, subkeyname;
98 REGSUBKEY_CTR *subkeys;
105 * There are potentially quite a few store operations which are all
106 * indiviually wrapped in tdb transactions. Wrapping them in a single
107 * transaction gives just a single transaction_commit() to actually do
108 * its fsync()s. See tdb/common/transaction.c for info about nested
109 * transaction behaviour.
112 if ( tdb_transaction_start( tdb_reg->tdb ) == -1 ) {
113 DEBUG(0, ("init_registry_data: tdb_transaction_start "
118 /* loop over all of the predefined paths and add each component */
120 for ( i=0; builtin_registry_paths[i] != NULL; i++ ) {
122 DEBUG(6,("init_registry_data: Adding [%s]\n", builtin_registry_paths[i]));
124 pstrcpy( path, builtin_registry_paths[i] );
128 while ( next_token(&p, keyname, "\\", sizeof(keyname)) ) {
130 /* build up the registry path from the components */
133 pstrcat( base, "\\" );
134 pstrcat( base, keyname );
136 /* get the immediate subkeyname (if we have one ) */
140 pstrcpy( remaining, p );
143 if ( !next_token(&p2, subkeyname, "\\", sizeof(subkeyname)) )
144 fstrcpy( subkeyname, p2 );
147 DEBUG(10,("init_registry_data: Storing key [%s] with subkey [%s]\n",
148 base, *subkeyname ? subkeyname : "NULL"));
150 /* we don't really care if the lookup succeeds or not since
151 we are about to update the record. We just want any
152 subkeys already present */
154 if ( !(subkeys = TALLOC_ZERO_P( NULL, REGSUBKEY_CTR )) ) {
155 DEBUG(0,("talloc() failure!\n"));
159 regdb_fetch_keys( base, subkeys );
161 regsubkey_ctr_addkey( subkeys, subkeyname );
162 if ( !regdb_store_keys( base, subkeys ))
165 TALLOC_FREE( subkeys );
169 /* loop over all of the predefined values and add each component */
171 for ( i=0; builtin_registry_values[i].path != NULL; i++ ) {
172 if ( !(values = TALLOC_ZERO_P( NULL, REGVAL_CTR )) ) {
173 DEBUG(0,("talloc() failure!\n"));
177 regdb_fetch_values( builtin_registry_values[i].path, values );
179 /* preserve existing values across restarts. Only add new ones */
181 if ( !regval_ctr_key_exists( values, builtin_registry_values[i].valuename ) )
183 switch( builtin_registry_values[i].type ) {
185 regval_ctr_addvalue( values,
186 builtin_registry_values[i].valuename,
188 (char*)&builtin_registry_values[i].data.dw_value,
193 init_unistr2( &data, builtin_registry_values[i].data.string, UNI_STR_TERMINATE);
194 regval_ctr_addvalue( values,
195 builtin_registry_values[i].valuename,
198 data.uni_str_len*sizeof(uint16) );
202 DEBUG(0,("init_registry_data: invalid value type in builtin_registry_values [%d]\n",
203 builtin_registry_values[i].type));
205 regdb_store_values( builtin_registry_values[i].path, values );
208 TALLOC_FREE( values );
211 if (tdb_transaction_commit( tdb_reg->tdb ) == -1) {
212 DEBUG(0, ("init_registry_data: Could not commit "
221 if (tdb_transaction_cancel( tdb_reg->tdb ) == -1) {
222 smb_panic("init_registry_data: tdb_transaction_cancel "
229 /***********************************************************************
230 Open the registry database
231 ***********************************************************************/
233 BOOL regdb_init( void )
235 const char *vstring = "INFO/version";
241 if ( !(tdb_reg = tdb_wrap_open(NULL, lock_path("registry.tdb"), 0, REG_TDB_FLAGS, O_RDWR, 0600)) )
243 tdb_reg = tdb_wrap_open(NULL, lock_path("registry.tdb"), 0, REG_TDB_FLAGS, O_RDWR|O_CREAT, 0600);
245 DEBUG(0,("regdb_init: Failed to open registry %s (%s)\n",
246 lock_path("registry.tdb"), strerror(errno) ));
250 DEBUG(10,("regdb_init: Successfully created registry tdb\n"));
255 vers_id = tdb_fetch_int32(tdb_reg->tdb, vstring);
257 if ( vers_id != REGVER_V1 ) {
258 /* any upgrade code here if needed */
261 /* always setup the necessary keys and values */
263 if ( !init_registry_data() ) {
264 DEBUG(0,("init_registry: Failed to initialize data in registry!\n"));
271 /***********************************************************************
272 Open the registry. Must already have been initialized by regdb_init()
273 ***********************************************************************/
275 WERROR regdb_open( void )
277 WERROR result = WERR_OK;
280 DEBUG(10,("regdb_open: incrementing refcount (%d)\n", tdb_refcount));
287 tdb_reg = tdb_wrap_open(NULL, lock_path("registry.tdb"), 0, REG_TDB_FLAGS, O_RDWR, 0600);
289 result = ntstatus_to_werror( map_nt_error_from_unix( errno ) );
290 DEBUG(0,("regdb_open: Failed to open %s! (%s)\n",
291 lock_path("registry.tdb"), strerror(errno) ));
297 DEBUG(10,("regdb_open: refcount reset (%d)\n", tdb_refcount));
302 /***********************************************************************
303 ***********************************************************************/
305 int regdb_close( void )
309 DEBUG(10,("regdb_close: decrementing refcount (%d)\n", tdb_refcount));
311 if ( tdb_refcount > 0 )
314 SMB_ASSERT( tdb_refcount >= 0 );
316 TALLOC_FREE(tdb_reg);
320 /***********************************************************************
321 return the tdb sequence number of the registry tdb.
322 this is an indicator for the content of the registry
323 having changed. it will change upon regdb_init, too, though.
324 ***********************************************************************/
325 int regdb_get_seqnum(void)
327 return tdb_get_seqnum(tdb_reg->tdb);
330 /***********************************************************************
331 Add subkey strings to the registry tdb under a defined key
332 fmt is the same format as tdb_pack except this function only supports
334 ***********************************************************************/
336 static BOOL regdb_store_keys_internal( const char *key, REGSUBKEY_CTR *ctr )
343 uint32 num_subkeys = regsubkey_ctr_numkeys( ctr );
349 pstrcpy( keyname, key );
350 normalize_reg_path( keyname );
352 /* allocate some initial memory */
354 if (!(buffer = (uint8 *)SMB_MALLOC(sizeof(pstring)))) {
357 buflen = sizeof(pstring);
360 /* store the number of subkeys */
362 len += tdb_pack(buffer+len, buflen-len, "d", num_subkeys );
364 /* pack all the strings */
366 for (i=0; i<num_subkeys; i++) {
367 len += tdb_pack( buffer+len, buflen-len, "f", regsubkey_ctr_specific_key(ctr, i) );
368 if ( len > buflen ) {
369 /* allocate some extra space */
370 if ((buffer = (uint8 *)SMB_REALLOC( buffer, len*2 )) == NULL) {
371 DEBUG(0,("regdb_store_keys: Failed to realloc memory of size [%d]\n", len*2));
377 len = tdb_pack( buffer+len, buflen-len, "f", regsubkey_ctr_specific_key(ctr, i) );
381 /* finally write out the data */
385 if ( tdb_store_bystring( tdb_reg->tdb, keyname, dbuf, TDB_REPLACE ) == -1) {
396 /***********************************************************************
397 Store the new subkey record and create any child key records that
398 do not currently exist
399 ***********************************************************************/
401 BOOL regdb_store_keys( const char *key, REGSUBKEY_CTR *ctr )
405 REGSUBKEY_CTR *subkeys = NULL, *old_subkeys = NULL;
408 if ( tdb_transaction_start( tdb_reg->tdb ) == -1 ) {
409 DEBUG(0, ("regdb_store_keys: tdb_transaction_start failed\n"));
413 /* fetch a list of the old subkeys so we can determine if any were
416 if ( !(old_subkeys = TALLOC_ZERO_P( ctr, REGSUBKEY_CTR )) ) {
417 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
421 regdb_fetch_keys( key, old_subkeys );
423 /* store the subkey list for the parent */
425 if ( !regdb_store_keys_internal( key, ctr ) ) {
426 DEBUG(0,("regdb_store_keys: Failed to store new subkey list "
427 "for parent [%s]\n", key ));
431 /* now delete removed keys */
433 num_subkeys = regsubkey_ctr_numkeys( old_subkeys );
434 for ( i=0; i<num_subkeys; i++ ) {
435 oldkeyname = regsubkey_ctr_specific_key( old_subkeys, i );
437 if ( regsubkey_ctr_key_exists( ctr, oldkeyname ) ) {
439 * It's still around, don't delete
445 pstr_sprintf( path, "%s/%s", key, oldkeyname );
446 normalize_reg_path( path );
447 if (tdb_delete_bystring( tdb_reg->tdb, path ) == -1) {
448 DEBUG(1, ("Deleting %s failed\n", path));
452 pstr_sprintf( path, "%s/%s/%s", VALUE_PREFIX, key,
454 normalize_reg_path( path );
457 * Ignore errors here, we might have no values around
459 tdb_delete_bystring( tdb_reg->tdb, path );
462 TALLOC_FREE( old_subkeys );
464 /* now create records for any subkeys that don't already exist */
466 num_subkeys = regsubkey_ctr_numkeys( ctr );
467 for ( i=0; i<num_subkeys; i++ ) {
468 pstr_sprintf( path, "%s/%s", key,
469 regsubkey_ctr_specific_key( ctr, i ) );
471 if ( !(subkeys = TALLOC_ZERO_P( ctr, REGSUBKEY_CTR )) ) {
472 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
476 if ( regdb_fetch_keys( path, subkeys ) == -1 ) {
477 /* create a record with 0 subkeys */
478 if ( !regdb_store_keys_internal( path, subkeys ) ) {
479 DEBUG(0,("regdb_store_keys: Failed to store "
480 "new record for key [%s]\n", path ));
485 TALLOC_FREE( subkeys );
488 if (tdb_transaction_commit( tdb_reg->tdb ) == -1) {
489 DEBUG(0, ("regdb_store_keys: Could not commit transaction\n"));
496 TALLOC_FREE( old_subkeys );
497 TALLOC_FREE( subkeys );
499 if (tdb_transaction_cancel( tdb_reg->tdb ) == -1) {
500 smb_panic("regdb_store_keys: tdb_transaction_cancel failed\n");
507 /***********************************************************************
508 Retrieve an array of strings containing subkeys. Memory should be
509 released by the caller.
510 ***********************************************************************/
512 int regdb_fetch_keys( const char* key, REGSUBKEY_CTR *ctr )
522 DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key ? key : "NULL"));
524 pstrcpy( path, key );
526 /* convert to key format */
527 pstring_sub( path, "\\", "/" );
530 dbuf = tdb_fetch_bystring( tdb_reg->tdb, path );
536 DEBUG(5,("regdb_fetch_keys: tdb lookup failed to locate key [%s]\n", key));
540 len = tdb_unpack( buf, buflen, "d", &num_items);
542 for (i=0; i<num_items; i++) {
543 len += tdb_unpack( buf+len, buflen-len, "f", subkeyname );
544 regsubkey_ctr_addkey( ctr, subkeyname );
547 SAFE_FREE( dbuf.dptr );
549 DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items));
554 /****************************************************************************
555 Unpack a list of registry values frem the TDB
556 ***************************************************************************/
558 static int regdb_unpack_values(REGVAL_CTR *values, uint8 *buf, int buflen)
565 uint32 num_values = 0;
570 /* loop and unpack the rest of the registry values */
572 len += tdb_unpack(buf+len, buflen-len, "d", &num_values);
574 for ( i=0; i<num_values; i++ ) {
575 /* unpack the next regval */
580 len += tdb_unpack(buf+len, buflen-len, "fdB",
586 /* add the new value. Paranoid protective code -- make sure data_p is valid */
588 if ( size && data_p ) {
589 regval_ctr_addvalue( values, valuename, type, (const char *)data_p, size );
590 SAFE_FREE(data_p); /* 'B' option to tdb_unpack does a malloc() */
593 DEBUG(8,("specific: [%s], len: %d\n", valuename, size));
599 /****************************************************************************
600 Pack all values in all printer keys
601 ***************************************************************************/
603 static int regdb_pack_values(REGVAL_CTR *values, uint8 *buf, int buflen)
613 num_values = regval_ctr_numvals( values );
615 /* pack the number of values first */
617 len += tdb_pack( buf+len, buflen-len, "d", num_values );
619 /* loop over all values */
621 for ( i=0; i<num_values; i++ ) {
622 val = regval_ctr_specific_value( values, i );
623 len += tdb_pack(buf+len, buflen-len, "fdB",
627 regval_data_p(val) );
633 /***********************************************************************
634 Retrieve an array of strings containing subkeys. Memory should be
635 released by the caller.
636 ***********************************************************************/
638 int regdb_fetch_values( const char* key, REGVAL_CTR *values )
643 DEBUG(10,("regdb_fetch_values: Looking for value of key [%s] \n", key));
645 pstr_sprintf( keystr, "%s/%s", VALUE_PREFIX, key );
646 normalize_reg_path( keystr );
648 data = tdb_fetch_bystring( tdb_reg->tdb, keystr );
651 /* all keys have zero values by default */
655 regdb_unpack_values( values, data.dptr, data.dsize );
657 SAFE_FREE( data.dptr );
659 return regval_ctr_numvals(values);
662 /***********************************************************************
663 Stub function since we do not currently support storing registry
664 values in the registry.tdb
665 ***********************************************************************/
667 BOOL regdb_store_values( const char *key, REGVAL_CTR *values )
673 DEBUG(10,("regdb_store_values: Looking for value of key [%s] \n", key));
677 len = regdb_pack_values( values, data.dptr, data.dsize );
679 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
683 data.dptr = SMB_MALLOC_ARRAY( uint8, len );
686 len = regdb_pack_values( values, data.dptr, data.dsize );
688 SMB_ASSERT( len == data.dsize );
690 pstr_sprintf( keystr, "%s/%s", VALUE_PREFIX, key );
691 normalize_reg_path( keystr );
693 ret = tdb_trans_store_bystring(tdb_reg->tdb, keystr, data, TDB_REPLACE);
695 SAFE_FREE( data.dptr );
700 static WERROR regdb_get_secdesc(TALLOC_CTX *mem_ctx, const char *key,
701 struct security_descriptor **psecdesc)
707 DEBUG(10, ("regdb_get_secdesc: Getting secdesc of key [%s]\n", key));
709 if (asprintf(&tdbkey, "%s/%s", SECDESC_PREFIX, key) == -1) {
712 normalize_dbkey(tdbkey);
714 data = tdb_fetch_bystring(tdb_reg->tdb, tdbkey);
717 if (data.dptr == NULL) {
721 status = unmarshall_sec_desc(mem_ctx, (uint8 *)data.dptr, data.dsize,
724 SAFE_FREE(data.dptr);
726 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MEMORY)) {
730 if (!NT_STATUS_IS_OK(status)) {
731 return WERR_REG_CORRUPT;
737 static WERROR regdb_set_secdesc(const char *key,
738 struct security_descriptor *secdesc)
743 WERROR err = WERR_NOMEM;
746 if (!(mem_ctx = talloc_init("regdb_set_secdesc"))) {
752 if (!(tdbkey = talloc_asprintf(mem_ctx, "%s/%s", SECDESC_PREFIX,
756 normalize_dbkey(tdbkey);
758 err = ntstatus_to_werror(marshall_sec_desc(mem_ctx, secdesc,
761 if (!W_ERROR_IS_OK(err)) {
765 if (tdb_trans_store_bystring(tdb_reg->tdb, tdbkey, tdbdata, 0) == -1) {
766 err = ntstatus_to_werror(map_nt_error_from_unix(errno));
772 TALLOC_FREE(mem_ctx);
777 * Table of function pointers for default access
780 REGISTRY_OPS regdb_ops = {