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, 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 /* List the deepest path into the registry. All part components will be created.*/
33 /* If you want to have a part of the path controlled by the tdb and part by
34 a virtual registry db (e.g. printing), then you have to list the deepest path.
35 For example,"HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion/Print"
36 allows the reg_db backend to handle everything up to
37 "HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion" and then we'll hook
38 the reg_printing backend onto the last component of the path (see
39 KEY_PRINTING_2K in include/rpc_reg.h) --jerry */
41 static const char *builtin_registry_paths[] = {
48 "HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Perflib",
49 "HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Perflib\\009",
50 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Print\\Monitors",
51 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\ProductOptions",
52 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\DefaultUserConfiguration",
53 "HKLM\\SYSTEM\\CurrentControlSet\\Services\\TcpIp\\Parameters",
54 "HKLM\\SYSTEM\\CurrentControlSet\\Services\\Netlogon\\Parameters",
61 struct builtin_regkey_value {
63 const char *valuename;
71 static struct builtin_regkey_value builtin_registry_values[] = {
73 SAMBA_PRINTER_PORT_NAME, REG_SZ, { "" } },
75 "DefaultSpoolDirectory", REG_SZ, { "C:\\Windows\\System32\\Spool\\Printers" } },
77 "DisplayName", REG_SZ, { "Event Log" } },
79 "ErrorControl", REG_DWORD, { (char*)0x00000001 } },
80 { NULL, NULL, 0, { NULL } }
83 /***********************************************************************
84 Open the registry data in the tdb
85 ***********************************************************************/
87 static BOOL init_registry_data( void )
89 pstring path, base, remaining;
90 fstring keyname, subkeyname;
91 REGSUBKEY_CTR *subkeys;
98 * There are potentially quite a few store operations which are all
99 * indiviually wrapped in tdb transactions. Wrapping them in a single
100 * transaction gives just a single transaction_commit() to actually do
101 * its fsync()s. See tdb/common/transaction.c for info about nested
102 * transaction behaviour.
105 if ( tdb_transaction_start( tdb_reg->tdb ) == -1 ) {
106 DEBUG(0, ("init_registry_data: tdb_transaction_start "
111 /* loop over all of the predefined paths and add each component */
113 for ( i=0; builtin_registry_paths[i] != NULL; i++ ) {
115 DEBUG(6,("init_registry_data: Adding [%s]\n", builtin_registry_paths[i]));
117 pstrcpy( path, builtin_registry_paths[i] );
121 while ( next_token(&p, keyname, "\\", sizeof(keyname)) ) {
123 /* build up the registry path from the components */
126 pstrcat( base, "\\" );
127 pstrcat( base, keyname );
129 /* get the immediate subkeyname (if we have one ) */
133 pstrcpy( remaining, p );
136 if ( !next_token(&p2, subkeyname, "\\", sizeof(subkeyname)) )
137 fstrcpy( subkeyname, p2 );
140 DEBUG(10,("init_registry_data: Storing key [%s] with subkey [%s]\n",
141 base, *subkeyname ? subkeyname : "NULL"));
143 /* we don't really care if the lookup succeeds or not since
144 we are about to update the record. We just want any
145 subkeys already present */
147 if ( !(subkeys = TALLOC_ZERO_P( NULL, REGSUBKEY_CTR )) ) {
148 DEBUG(0,("talloc() failure!\n"));
152 regdb_fetch_keys( base, subkeys );
154 regsubkey_ctr_addkey( subkeys, subkeyname );
155 if ( !regdb_store_keys( base, subkeys ))
158 TALLOC_FREE( subkeys );
162 /* loop over all of the predefined values and add each component */
164 for ( i=0; builtin_registry_values[i].path != NULL; i++ ) {
165 if ( !(values = TALLOC_ZERO_P( NULL, REGVAL_CTR )) ) {
166 DEBUG(0,("talloc() failure!\n"));
170 regdb_fetch_values( builtin_registry_values[i].path, values );
172 /* preserve existing values across restarts. Only add new ones */
174 if ( !regval_ctr_key_exists( values, builtin_registry_values[i].valuename ) )
176 switch( builtin_registry_values[i].type ) {
178 regval_ctr_addvalue( values,
179 builtin_registry_values[i].valuename,
181 (char*)&builtin_registry_values[i].data.dw_value,
186 init_unistr2( &data, builtin_registry_values[i].data.string, UNI_STR_TERMINATE);
187 regval_ctr_addvalue( values,
188 builtin_registry_values[i].valuename,
191 data.uni_str_len*sizeof(uint16) );
195 DEBUG(0,("init_registry_data: invalid value type in builtin_registry_values [%d]\n",
196 builtin_registry_values[i].type));
198 regdb_store_values( builtin_registry_values[i].path, values );
201 TALLOC_FREE( values );
204 if (tdb_transaction_commit( tdb_reg->tdb ) == -1) {
205 DEBUG(0, ("init_registry_data: Could not commit "
214 if (tdb_transaction_cancel( tdb_reg->tdb ) == -1) {
215 smb_panic("init_registry_data: tdb_transaction_cancel "
222 /***********************************************************************
223 Open the registry database
224 ***********************************************************************/
226 BOOL regdb_init( void )
228 const char *vstring = "INFO/version";
234 if ( !(tdb_reg = tdb_wrap_open(NULL, lock_path("registry.tdb"), 0, REG_TDB_FLAGS, O_RDWR, 0600)) )
236 tdb_reg = tdb_wrap_open(NULL, lock_path("registry.tdb"), 0, REG_TDB_FLAGS, O_RDWR|O_CREAT, 0600);
238 DEBUG(0,("regdb_init: Failed to open registry %s (%s)\n",
239 lock_path("registry.tdb"), strerror(errno) ));
243 DEBUG(10,("regdb_init: Successfully created registry tdb\n"));
248 vers_id = tdb_fetch_int32(tdb_reg->tdb, vstring);
250 if ( vers_id != REGVER_V1 ) {
251 /* any upgrade code here if needed */
252 DEBUG(10, ("regdb_init: got INFO/version = %d != %d\n",
253 vers_id, REGVER_V1));
256 /* always setup the necessary keys and values */
258 if ( !init_registry_data() ) {
259 DEBUG(0,("init_registry: Failed to initialize data in registry!\n"));
266 /***********************************************************************
267 Open the registry. Must already have been initialized by regdb_init()
268 ***********************************************************************/
270 WERROR regdb_open( void )
272 WERROR result = WERR_OK;
275 DEBUG(10,("regdb_open: incrementing refcount (%d)\n", tdb_refcount));
282 tdb_reg = tdb_wrap_open(NULL, lock_path("registry.tdb"), 0, REG_TDB_FLAGS, O_RDWR, 0600);
284 result = ntstatus_to_werror( map_nt_error_from_unix( errno ) );
285 DEBUG(0,("regdb_open: Failed to open %s! (%s)\n",
286 lock_path("registry.tdb"), strerror(errno) ));
292 DEBUG(10,("regdb_open: refcount reset (%d)\n", tdb_refcount));
297 /***********************************************************************
298 ***********************************************************************/
300 int regdb_close( void )
304 DEBUG(10,("regdb_close: decrementing refcount (%d)\n", tdb_refcount));
306 if ( tdb_refcount > 0 )
309 SMB_ASSERT( tdb_refcount >= 0 );
311 TALLOC_FREE(tdb_reg);
315 /***********************************************************************
316 return the tdb sequence number of the registry tdb.
317 this is an indicator for the content of the registry
318 having changed. it will change upon regdb_init, too, though.
319 ***********************************************************************/
320 int regdb_get_seqnum(void)
322 return tdb_get_seqnum(tdb_reg->tdb);
325 /***********************************************************************
326 Add subkey strings to the registry tdb under a defined key
327 fmt is the same format as tdb_pack except this function only supports
329 ***********************************************************************/
331 static BOOL regdb_store_keys_internal( const char *key, REGSUBKEY_CTR *ctr )
338 uint32 num_subkeys = regsubkey_ctr_numkeys( ctr );
344 pstrcpy( keyname, key );
345 normalize_reg_path( keyname );
347 /* allocate some initial memory */
349 if (!(buffer = (uint8 *)SMB_MALLOC(sizeof(pstring)))) {
352 buflen = sizeof(pstring);
355 /* store the number of subkeys */
357 len += tdb_pack(buffer+len, buflen-len, "d", num_subkeys );
359 /* pack all the strings */
361 for (i=0; i<num_subkeys; i++) {
362 len += tdb_pack( buffer+len, buflen-len, "f", regsubkey_ctr_specific_key(ctr, i) );
363 if ( len > buflen ) {
364 /* allocate some extra space */
365 if ((buffer = (uint8 *)SMB_REALLOC( buffer, len*2 )) == NULL) {
366 DEBUG(0,("regdb_store_keys: Failed to realloc memory of size [%d]\n", len*2));
372 len = tdb_pack( buffer+len, buflen-len, "f", regsubkey_ctr_specific_key(ctr, i) );
376 /* finally write out the data */
380 if ( tdb_store_bystring( tdb_reg->tdb, keyname, dbuf, TDB_REPLACE ) == -1) {
391 /***********************************************************************
392 Store the new subkey record and create any child key records that
393 do not currently exist
394 ***********************************************************************/
396 BOOL regdb_store_keys( const char *key, REGSUBKEY_CTR *ctr )
400 REGSUBKEY_CTR *subkeys = NULL, *old_subkeys = NULL;
403 if ( tdb_transaction_start( tdb_reg->tdb ) == -1 ) {
404 DEBUG(0, ("regdb_store_keys: tdb_transaction_start failed\n"));
408 /* fetch a list of the old subkeys so we can determine if any were
411 if ( !(old_subkeys = TALLOC_ZERO_P( ctr, REGSUBKEY_CTR )) ) {
412 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
416 regdb_fetch_keys( key, old_subkeys );
418 /* store the subkey list for the parent */
420 if ( !regdb_store_keys_internal( key, ctr ) ) {
421 DEBUG(0,("regdb_store_keys: Failed to store new subkey list "
422 "for parent [%s]\n", key ));
426 /* now delete removed keys */
428 num_subkeys = regsubkey_ctr_numkeys( old_subkeys );
429 for ( i=0; i<num_subkeys; i++ ) {
430 oldkeyname = regsubkey_ctr_specific_key( old_subkeys, i );
432 if ( regsubkey_ctr_key_exists( ctr, oldkeyname ) ) {
434 * It's still around, don't delete
440 pstr_sprintf( path, "%s/%s", key, oldkeyname );
441 normalize_reg_path( path );
442 if (tdb_delete_bystring( tdb_reg->tdb, path ) == -1) {
443 DEBUG(1, ("Deleting %s failed\n", path));
447 pstr_sprintf( path, "%s/%s/%s", REG_VALUE_PREFIX, key,
449 normalize_reg_path( path );
452 * Ignore errors here, we might have no values around
454 tdb_delete_bystring( tdb_reg->tdb, path );
457 TALLOC_FREE( old_subkeys );
459 /* now create records for any subkeys that don't already exist */
461 num_subkeys = regsubkey_ctr_numkeys( ctr );
462 for ( i=0; i<num_subkeys; i++ ) {
463 pstr_sprintf( path, "%s/%s", key,
464 regsubkey_ctr_specific_key( ctr, i ) );
466 if ( !(subkeys = TALLOC_ZERO_P( ctr, REGSUBKEY_CTR )) ) {
467 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
471 if ( regdb_fetch_keys( path, subkeys ) == -1 ) {
472 /* create a record with 0 subkeys */
473 if ( !regdb_store_keys_internal( path, subkeys ) ) {
474 DEBUG(0,("regdb_store_keys: Failed to store "
475 "new record for key [%s]\n", path ));
480 TALLOC_FREE( subkeys );
483 if (tdb_transaction_commit( tdb_reg->tdb ) == -1) {
484 DEBUG(0, ("regdb_store_keys: Could not commit transaction\n"));
491 TALLOC_FREE( old_subkeys );
492 TALLOC_FREE( subkeys );
494 if (tdb_transaction_cancel( tdb_reg->tdb ) == -1) {
495 smb_panic("regdb_store_keys: tdb_transaction_cancel failed\n");
502 /***********************************************************************
503 Retrieve an array of strings containing subkeys. Memory should be
504 released by the caller.
505 ***********************************************************************/
507 int regdb_fetch_keys( const char* key, REGSUBKEY_CTR *ctr )
517 DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key ? key : "NULL"));
519 pstrcpy( path, key );
521 /* convert to key format */
522 pstring_sub( path, "\\", "/" );
525 dbuf = tdb_fetch_bystring( tdb_reg->tdb, path );
531 DEBUG(5,("regdb_fetch_keys: tdb lookup failed to locate key [%s]\n", key));
535 len = tdb_unpack( buf, buflen, "d", &num_items);
537 for (i=0; i<num_items; i++) {
538 len += tdb_unpack( buf+len, buflen-len, "f", subkeyname );
539 regsubkey_ctr_addkey( ctr, subkeyname );
542 SAFE_FREE( dbuf.dptr );
544 DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items));
549 /****************************************************************************
550 Unpack a list of registry values frem the TDB
551 ***************************************************************************/
553 static int regdb_unpack_values(REGVAL_CTR *values, uint8 *buf, int buflen)
560 uint32 num_values = 0;
565 /* loop and unpack the rest of the registry values */
567 len += tdb_unpack(buf+len, buflen-len, "d", &num_values);
569 for ( i=0; i<num_values; i++ ) {
570 /* unpack the next regval */
575 len += tdb_unpack(buf+len, buflen-len, "fdB",
581 /* add the new value. Paranoid protective code -- make sure data_p is valid */
583 if ( size && data_p ) {
584 regval_ctr_addvalue( values, valuename, type, (const char *)data_p, size );
585 SAFE_FREE(data_p); /* 'B' option to tdb_unpack does a malloc() */
588 DEBUG(8,("specific: [%s], len: %d\n", valuename, size));
594 /****************************************************************************
595 Pack all values in all printer keys
596 ***************************************************************************/
598 static int regdb_pack_values(REGVAL_CTR *values, uint8 *buf, int buflen)
608 num_values = regval_ctr_numvals( values );
610 /* pack the number of values first */
612 len += tdb_pack( buf+len, buflen-len, "d", num_values );
614 /* loop over all values */
616 for ( i=0; i<num_values; i++ ) {
617 val = regval_ctr_specific_value( values, i );
618 len += tdb_pack(buf+len, buflen-len, "fdB",
622 regval_data_p(val) );
628 /***********************************************************************
629 Retrieve an array of strings containing subkeys. Memory should be
630 released by the caller.
631 ***********************************************************************/
633 int regdb_fetch_values( const char* key, REGVAL_CTR *values )
638 DEBUG(10,("regdb_fetch_values: Looking for value of key [%s] \n", key));
640 pstr_sprintf( keystr, "%s/%s", REG_VALUE_PREFIX, key );
641 normalize_reg_path( keystr );
643 data = tdb_fetch_bystring( tdb_reg->tdb, keystr );
646 /* all keys have zero values by default */
650 regdb_unpack_values( values, data.dptr, data.dsize );
652 SAFE_FREE( data.dptr );
654 return regval_ctr_numvals(values);
657 /***********************************************************************
658 Stub function since we do not currently support storing registry
659 values in the registry.tdb
660 ***********************************************************************/
662 BOOL regdb_store_values( const char *key, REGVAL_CTR *values )
668 DEBUG(10,("regdb_store_values: Looking for value of key [%s] \n", key));
672 len = regdb_pack_values( values, data.dptr, data.dsize );
674 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
678 data.dptr = SMB_MALLOC_ARRAY( uint8, len );
681 len = regdb_pack_values( values, data.dptr, data.dsize );
683 SMB_ASSERT( len == data.dsize );
685 pstr_sprintf( keystr, "%s/%s", REG_VALUE_PREFIX, key );
686 normalize_reg_path( keystr );
688 ret = tdb_trans_store_bystring(tdb_reg->tdb, keystr, data, TDB_REPLACE);
690 SAFE_FREE( data.dptr );
695 static WERROR regdb_get_secdesc(TALLOC_CTX *mem_ctx, const char *key,
696 struct security_descriptor **psecdesc)
702 DEBUG(10, ("regdb_get_secdesc: Getting secdesc of key [%s]\n", key));
704 if (asprintf(&tdbkey, "%s/%s", REG_SECDESC_PREFIX, key) == -1) {
707 normalize_dbkey(tdbkey);
709 data = tdb_fetch_bystring(tdb_reg->tdb, tdbkey);
712 if (data.dptr == NULL) {
716 status = unmarshall_sec_desc(mem_ctx, (uint8 *)data.dptr, data.dsize,
719 SAFE_FREE(data.dptr);
721 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MEMORY)) {
725 if (!NT_STATUS_IS_OK(status)) {
726 return WERR_REG_CORRUPT;
732 static WERROR regdb_set_secdesc(const char *key,
733 struct security_descriptor *secdesc)
738 WERROR err = WERR_NOMEM;
741 if (!(mem_ctx = talloc_init("regdb_set_secdesc"))) {
747 if (!(tdbkey = talloc_asprintf(mem_ctx, "%s/%s", REG_SECDESC_PREFIX,
751 normalize_dbkey(tdbkey);
753 err = ntstatus_to_werror(marshall_sec_desc(mem_ctx, secdesc,
756 if (!W_ERROR_IS_OK(err)) {
760 if (tdb_trans_store_bystring(tdb_reg->tdb, tdbkey, tdbdata, 0) == -1) {
761 err = ntstatus_to_werror(map_nt_error_from_unix(errno));
767 TALLOC_FREE(mem_ctx);
772 * Table of function pointers for default access
775 REGISTRY_OPS regdb_ops = {