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_RPC_SRV
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;
402 if ( tdb_transaction_start( tdb_reg->tdb ) == -1 ) {
403 DEBUG(0, ("regdb_store_keys: tdb_transaction_start failed\n"));
407 /* fetch a list of the old subkeys so we can determine if any were
410 if ( !(old_subkeys = TALLOC_ZERO_P( ctr, REGSUBKEY_CTR )) ) {
411 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
415 regdb_fetch_keys( key, old_subkeys );
417 /* store the subkey list for the parent */
419 if ( !regdb_store_keys_internal( key, ctr ) ) {
420 DEBUG(0,("regdb_store_keys: Failed to store new subkey list "
421 "for parent [%s]\n", key ));
425 /* now delete removed keys */
427 num_subkeys = regsubkey_ctr_numkeys( old_subkeys );
428 for ( i=0; i<num_subkeys; i++ ) {
429 oldkeyname = regsubkey_ctr_specific_key( old_subkeys, i );
431 if ( regsubkey_ctr_key_exists( ctr, oldkeyname ) ) {
433 * It's still around, don't delete
439 pstr_sprintf( path, "%s/%s", key, oldkeyname );
440 normalize_reg_path( path );
441 if (tdb_delete_bystring( tdb_reg->tdb, path ) == -1) {
442 DEBUG(1, ("Deleting %s failed\n", path));
446 pstr_sprintf( path, "%s/%s/%s", REG_VALUE_PREFIX, key,
448 normalize_reg_path( path );
451 * Ignore errors here, we might have no values around
453 tdb_delete_bystring( tdb_reg->tdb, path );
456 TALLOC_FREE( old_subkeys );
458 /* now create records for any subkeys that don't already exist */
460 num_subkeys = regsubkey_ctr_numkeys( ctr );
461 for ( i=0; i<num_subkeys; i++ ) {
462 pstr_sprintf( path, "%s/%s", key,
463 regsubkey_ctr_specific_key( ctr, i ) );
465 if ( !(subkeys = TALLOC_ZERO_P( ctr, REGSUBKEY_CTR )) ) {
466 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
470 if ( regdb_fetch_keys( path, subkeys ) == -1 ) {
471 /* create a record with 0 subkeys */
472 if ( !regdb_store_keys_internal( path, subkeys ) ) {
473 DEBUG(0,("regdb_store_keys: Failed to store "
474 "new record for key [%s]\n", path ));
479 TALLOC_FREE( subkeys );
482 if (tdb_transaction_commit( tdb_reg->tdb ) == -1) {
483 DEBUG(0, ("regdb_store_keys: Could not commit transaction\n"));
490 TALLOC_FREE( old_subkeys );
491 TALLOC_FREE( subkeys );
493 if (tdb_transaction_cancel( tdb_reg->tdb ) == -1) {
494 smb_panic("regdb_store_keys: tdb_transaction_cancel failed\n");
501 /***********************************************************************
502 Retrieve an array of strings containing subkeys. Memory should be
503 released by the caller.
504 ***********************************************************************/
506 int regdb_fetch_keys( const char* key, REGSUBKEY_CTR *ctr )
516 DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key ? key : "NULL"));
518 pstrcpy( path, key );
520 /* convert to key format */
521 pstring_sub( path, "\\", "/" );
524 dbuf = tdb_fetch_bystring( tdb_reg->tdb, path );
530 DEBUG(5,("regdb_fetch_keys: tdb lookup failed to locate key [%s]\n", key));
534 len = tdb_unpack( buf, buflen, "d", &num_items);
536 for (i=0; i<num_items; i++) {
537 len += tdb_unpack( buf+len, buflen-len, "f", subkeyname );
538 regsubkey_ctr_addkey( ctr, subkeyname );
541 SAFE_FREE( dbuf.dptr );
543 DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items));
548 /****************************************************************************
549 Unpack a list of registry values frem the TDB
550 ***************************************************************************/
552 static int regdb_unpack_values(REGVAL_CTR *values, uint8 *buf, int buflen)
559 uint32 num_values = 0;
564 /* loop and unpack the rest of the registry values */
566 len += tdb_unpack(buf+len, buflen-len, "d", &num_values);
568 for ( i=0; i<num_values; i++ ) {
569 /* unpack the next regval */
574 len += tdb_unpack(buf+len, buflen-len, "fdB",
580 /* add the new value. Paranoid protective code -- make sure data_p is valid */
582 if ( size && data_p ) {
583 regval_ctr_addvalue( values, valuename, type, (const char *)data_p, size );
584 SAFE_FREE(data_p); /* 'B' option to tdb_unpack does a malloc() */
587 DEBUG(8,("specific: [%s], len: %d\n", valuename, size));
593 /****************************************************************************
594 Pack all values in all printer keys
595 ***************************************************************************/
597 static int regdb_pack_values(REGVAL_CTR *values, uint8 *buf, int buflen)
607 num_values = regval_ctr_numvals( values );
609 /* pack the number of values first */
611 len += tdb_pack( buf+len, buflen-len, "d", num_values );
613 /* loop over all values */
615 for ( i=0; i<num_values; i++ ) {
616 val = regval_ctr_specific_value( values, i );
617 len += tdb_pack(buf+len, buflen-len, "fdB",
621 regval_data_p(val) );
627 /***********************************************************************
628 Retrieve an array of strings containing subkeys. Memory should be
629 released by the caller.
630 ***********************************************************************/
632 int regdb_fetch_values( const char* key, REGVAL_CTR *values )
637 DEBUG(10,("regdb_fetch_values: Looking for value of key [%s] \n", key));
639 pstr_sprintf( keystr, "%s/%s", REG_VALUE_PREFIX, key );
640 normalize_reg_path( keystr );
642 data = tdb_fetch_bystring( tdb_reg->tdb, keystr );
645 /* all keys have zero values by default */
649 regdb_unpack_values( values, data.dptr, data.dsize );
651 SAFE_FREE( data.dptr );
653 return regval_ctr_numvals(values);
656 /***********************************************************************
657 Stub function since we do not currently support storing registry
658 values in the registry.tdb
659 ***********************************************************************/
661 BOOL regdb_store_values( const char *key, REGVAL_CTR *values )
667 DEBUG(10,("regdb_store_values: Looking for value of key [%s] \n", key));
671 len = regdb_pack_values( values, data.dptr, data.dsize );
673 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
677 data.dptr = SMB_MALLOC_ARRAY( uint8, len );
680 len = regdb_pack_values( values, data.dptr, data.dsize );
682 SMB_ASSERT( len == data.dsize );
684 pstr_sprintf( keystr, "%s/%s", REG_VALUE_PREFIX, key );
685 normalize_reg_path( keystr );
687 ret = tdb_trans_store_bystring(tdb_reg->tdb, keystr, data, TDB_REPLACE);
689 SAFE_FREE( data.dptr );
694 static WERROR regdb_get_secdesc(TALLOC_CTX *mem_ctx, const char *key,
695 struct security_descriptor **psecdesc)
701 DEBUG(10, ("regdb_get_secdesc: Getting secdesc of key [%s]\n", key));
703 if (asprintf(&tdbkey, "%s/%s", REG_SECDESC_PREFIX, key) == -1) {
706 normalize_dbkey(tdbkey);
708 data = tdb_fetch_bystring(tdb_reg->tdb, tdbkey);
711 if (data.dptr == NULL) {
715 status = unmarshall_sec_desc(mem_ctx, (uint8 *)data.dptr, data.dsize,
718 SAFE_FREE(data.dptr);
720 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MEMORY)) {
724 if (!NT_STATUS_IS_OK(status)) {
725 return WERR_REG_CORRUPT;
731 static WERROR regdb_set_secdesc(const char *key,
732 struct security_descriptor *secdesc)
737 WERROR err = WERR_NOMEM;
740 if (!(mem_ctx = talloc_init("regdb_set_secdesc"))) {
746 if (!(tdbkey = talloc_asprintf(mem_ctx, "%s/%s", REG_SECDESC_PREFIX,
750 normalize_dbkey(tdbkey);
752 err = ntstatus_to_werror(marshall_sec_desc(mem_ctx, secdesc,
755 if (!W_ERROR_IS_OK(err)) {
759 if (tdb_trans_store_bystring(tdb_reg->tdb, tdbkey, tdbdata, 0) == -1) {
760 err = ntstatus_to_werror(map_nt_error_from_unix(errno));
766 TALLOC_FREE(mem_ctx);
771 * Table of function pointers for default access
774 REGISTRY_OPS regdb_ops = {