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 TDB_CONTEXT
*tdb_reg
;
29 static int tdb_refcount
;
31 #define VALUE_PREFIX "SAMBA_REGVAL"
33 /* List the deepest path into the registry. All part components will be created.*/
35 /* If you want to have a part of the path controlled by the tdb and part by
36 a virtual registry db (e.g. printing), then you have to list the deepest path.
37 For example,"HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion/Print"
38 allows the reg_db backend to handle everything up to
39 "HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion" and then we'll hook
40 the reg_printing backend onto the last component of the path (see
41 KEY_PRINTING_2K in include/rpc_reg.h) --jerry */
43 static const char *builtin_registry_paths
[] = {
49 "HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Perflib",
50 "HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Perflib\\009",
51 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Print\\Monitors",
52 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\ProductOptions",
53 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\DefaultUserConfiguration",
54 "HKLM\\SYSTEM\\CurrentControlSet\\Services\\TcpIp\\Parameters",
55 "HKLM\\SYSTEM\\CurrentControlSet\\Services\\Netlogon\\Parameters",
62 struct builtin_regkey_value
{
64 const char *valuename
;
72 static struct builtin_regkey_value builtin_registry_values
[] = {
74 SAMBA_PRINTER_PORT_NAME
, REG_SZ
, { "" } },
76 "DefaultSpoolDirectory", REG_SZ
, { "C:\\Windows\\System32\\Spool\\Printers" } },
78 "DisplayName", REG_SZ
, { "Event Log" } },
80 "ErrorControl", REG_DWORD
, { (char*)0x00000001 } },
81 { NULL
, NULL
, 0, { NULL
} }
84 #define REGVER_V1 1 /* first db version with write support */
86 /***********************************************************************
87 Open the registry data in the tdb
88 ***********************************************************************/
90 static BOOL
init_registry_data( void )
92 pstring path
, base
, remaining
;
93 fstring keyname
, subkeyname
;
94 REGSUBKEY_CTR
*subkeys
;
100 /* loop over all of the predefined paths and add each component */
102 for ( i
=0; builtin_registry_paths
[i
] != NULL
; i
++ ) {
104 DEBUG(6,("init_registry_data: Adding [%s]\n", builtin_registry_paths
[i
]));
106 pstrcpy( path
, builtin_registry_paths
[i
] );
110 while ( next_token(&p
, keyname
, "\\", sizeof(keyname
)) ) {
112 /* build up the registry path from the components */
115 pstrcat( base
, "\\" );
116 pstrcat( base
, keyname
);
118 /* get the immediate subkeyname (if we have one ) */
122 pstrcpy( remaining
, p
);
125 if ( !next_token(&p2
, subkeyname
, "\\", sizeof(subkeyname
)) )
126 fstrcpy( subkeyname
, p2
);
129 DEBUG(10,("init_registry_data: Storing key [%s] with subkey [%s]\n",
130 base
, *subkeyname
? subkeyname
: "NULL"));
132 /* we don't really care if the lookup succeeds or not since
133 we are about to update the record. We just want any
134 subkeys already present */
136 if ( !(subkeys
= TALLOC_ZERO_P( NULL
, REGSUBKEY_CTR
)) ) {
137 DEBUG(0,("talloc() failure!\n"));
141 regdb_fetch_keys( base
, subkeys
);
143 regsubkey_ctr_addkey( subkeys
, subkeyname
);
144 if ( !regdb_store_keys( base
, subkeys
))
147 TALLOC_FREE( subkeys
);
151 /* loop over all of the predefined values and add each component */
153 for ( i
=0; builtin_registry_values
[i
].path
!= NULL
; i
++ ) {
154 if ( !(values
= TALLOC_ZERO_P( NULL
, REGVAL_CTR
)) ) {
155 DEBUG(0,("talloc() failure!\n"));
159 regdb_fetch_values( builtin_registry_values
[i
].path
, values
);
161 /* preserve existing values across restarts. Only add new ones */
163 if ( !regval_ctr_key_exists( values
, builtin_registry_values
[i
].valuename
) )
165 switch( builtin_registry_values
[i
].type
) {
167 regval_ctr_addvalue( values
,
168 builtin_registry_values
[i
].valuename
,
170 (char*)&builtin_registry_values
[i
].data
.dw_value
,
175 init_unistr2( &data
, builtin_registry_values
[i
].data
.string
, UNI_STR_TERMINATE
);
176 regval_ctr_addvalue( values
,
177 builtin_registry_values
[i
].valuename
,
180 data
.uni_str_len
*sizeof(uint16
) );
184 DEBUG(0,("init_registry_data: invalid value type in builtin_registry_values [%d]\n",
185 builtin_registry_values
[i
].type
));
187 regdb_store_values( builtin_registry_values
[i
].path
, values
);
190 TALLOC_FREE( values
);
196 /***********************************************************************
197 Open the registry database
198 ***********************************************************************/
200 BOOL
regdb_init( void )
202 const char *vstring
= "INFO/version";
208 if ( !(tdb_reg
= tdb_open_log(lock_path("registry.tdb"), 0, TDB_DEFAULT
, O_RDWR
, 0600)) )
210 tdb_reg
= tdb_open_log(lock_path("registry.tdb"), 0, TDB_DEFAULT
, O_RDWR
|O_CREAT
, 0600);
212 DEBUG(0,("regdb_init: Failed to open registry %s (%s)\n",
213 lock_path("registry.tdb"), strerror(errno
) ));
217 DEBUG(10,("regdb_init: Successfully created registry tdb\n"));
223 vers_id
= tdb_fetch_int32(tdb_reg
, vstring
);
225 if ( vers_id
!= REGVER_V1
) {
226 /* any upgrade code here if needed */
229 /* always setup the necessary keys and values */
231 if ( !init_registry_data() ) {
232 DEBUG(0,("init_registry: Failed to initialize data in registry!\n"));
239 /***********************************************************************
240 Open the registry. Must already have been initialized by regdb_init()
241 ***********************************************************************/
243 WERROR
regdb_open( void )
245 WERROR result
= WERR_OK
;
248 DEBUG(10,("regdb_open: incrementing refcount (%d)\n", tdb_refcount
));
255 tdb_reg
= tdb_open_log(lock_path("registry.tdb"), 0, TDB_DEFAULT
, O_RDWR
, 0600);
257 result
= ntstatus_to_werror( map_nt_error_from_unix( errno
) );
258 DEBUG(0,("regdb_open: Failed to open %s! (%s)\n",
259 lock_path("registry.tdb"), strerror(errno
) ));
265 DEBUG(10,("regdb_open: refcount reset (%d)\n", tdb_refcount
));
270 /***********************************************************************
271 ***********************************************************************/
273 int regdb_close( void )
279 DEBUG(10,("regdb_close: decrementing refcount (%d)\n", tdb_refcount
));
281 if ( tdb_refcount
> 0 )
284 SMB_ASSERT( tdb_refcount
>= 0 );
286 ret
= tdb_close( tdb_reg
);
292 /***********************************************************************
293 Add subkey strings to the registry tdb under a defined key
294 fmt is the same format as tdb_pack except this function only supports
296 ***********************************************************************/
298 static BOOL
regdb_store_keys_internal( const char *key
, REGSUBKEY_CTR
*ctr
)
305 uint32 num_subkeys
= regsubkey_ctr_numkeys( ctr
);
311 pstrcpy( keyname
, key
);
312 normalize_reg_path( keyname
);
314 /* allocate some initial memory */
316 if (!(buffer
= (char *)SMB_MALLOC(sizeof(pstring
)))) {
319 buflen
= sizeof(pstring
);
322 /* store the number of subkeys */
324 len
+= tdb_pack(buffer
+len
, buflen
-len
, "d", num_subkeys
);
326 /* pack all the strings */
328 for (i
=0; i
<num_subkeys
; i
++) {
329 len
+= tdb_pack( buffer
+len
, buflen
-len
, "f", regsubkey_ctr_specific_key(ctr
, i
) );
330 if ( len
> buflen
) {
331 /* allocate some extra space */
332 if ((buffer
= (char *)SMB_REALLOC( buffer
, len
*2 )) == NULL
) {
333 DEBUG(0,("regdb_store_keys: Failed to realloc memory of size [%d]\n", len
*2));
339 len
= tdb_pack( buffer
+len
, buflen
-len
, "f", regsubkey_ctr_specific_key(ctr
, i
) );
343 /* finally write out the data */
346 kbuf
.dsize
= strlen(keyname
)+1;
349 if ( tdb_store( tdb_reg
, kbuf
, dbuf
, TDB_REPLACE
) == -1) {
360 /***********************************************************************
361 Store the new subkey record and create any child key records that
362 do not currently exist
363 ***********************************************************************/
365 BOOL
regdb_store_keys( const char *key
, REGSUBKEY_CTR
*ctr
)
369 REGSUBKEY_CTR
*subkeys
, *old_subkeys
;
372 /* fetch a list of the old subkeys so we can determine if any were deleted */
374 if ( !(old_subkeys
= TALLOC_ZERO_P( ctr
, REGSUBKEY_CTR
)) ) {
375 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
379 regdb_fetch_keys( key
, old_subkeys
);
381 /* store the subkey list for the parent */
383 if ( !regdb_store_keys_internal( key
, ctr
) ) {
384 DEBUG(0,("regdb_store_keys: Failed to store new subkey list for parent [%s}\n", key
));
388 /* now delete removed keys */
390 num_subkeys
= regsubkey_ctr_numkeys( old_subkeys
);
391 for ( i
=0; i
<num_subkeys
; i
++ ) {
392 oldkeyname
= regsubkey_ctr_specific_key( old_subkeys
, i
);
393 if ( !regsubkey_ctr_key_exists( ctr
, oldkeyname
) ) {
394 pstr_sprintf( path
, "%s%c%s", key
, '/', oldkeyname
);
395 normalize_reg_path( path
);
396 tdb_delete_bystring( tdb_reg
, path
);
400 TALLOC_FREE( old_subkeys
);
402 /* now create records for any subkeys that don't already exist */
404 num_subkeys
= regsubkey_ctr_numkeys( ctr
);
405 for ( i
=0; i
<num_subkeys
; i
++ ) {
406 pstr_sprintf( path
, "%s%c%s", key
, '/', regsubkey_ctr_specific_key( ctr
, i
) );
408 if ( !(subkeys
= TALLOC_ZERO_P( ctr
, REGSUBKEY_CTR
)) ) {
409 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
413 if ( regdb_fetch_keys( path
, subkeys
) == -1 ) {
414 /* create a record with 0 subkeys */
415 if ( !regdb_store_keys_internal( path
, subkeys
) ) {
416 DEBUG(0,("regdb_store_keys: Failed to store new record for key [%s}\n", path
));
417 TALLOC_FREE( subkeys
);
422 TALLOC_FREE( subkeys
);
429 /***********************************************************************
430 Retrieve an array of strings containing subkeys. Memory should be
431 released by the caller.
432 ***********************************************************************/
434 int regdb_fetch_keys( const char* key
, REGSUBKEY_CTR
*ctr
)
444 DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key
? key
: "NULL"));
446 pstrcpy( path
, key
);
448 /* convert to key format */
449 pstring_sub( path
, "\\", "/" );
452 dbuf
= tdb_fetch_bystring( tdb_reg
, path
);
458 DEBUG(5,("regdb_fetch_keys: tdb lookup failed to locate key [%s]\n", key
));
462 len
= tdb_unpack( buf
, buflen
, "d", &num_items
);
464 for (i
=0; i
<num_items
; i
++) {
465 len
+= tdb_unpack( buf
+len
, buflen
-len
, "f", subkeyname
);
466 regsubkey_ctr_addkey( ctr
, subkeyname
);
469 SAFE_FREE( dbuf
.dptr
);
471 DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items
));
476 /****************************************************************************
477 Unpack a list of registry values frem the TDB
478 ***************************************************************************/
480 static int regdb_unpack_values(REGVAL_CTR
*values
, char *buf
, int buflen
)
487 uint32 num_values
= 0;
492 /* loop and unpack the rest of the registry values */
494 len
+= tdb_unpack(buf
+len
, buflen
-len
, "d", &num_values
);
496 for ( i
=0; i
<num_values
; i
++ ) {
497 /* unpack the next regval */
502 len
+= tdb_unpack(buf
+len
, buflen
-len
, "fdB",
508 /* add the new value. Paranoid protective code -- make sure data_p is valid */
510 if ( size
&& data_p
) {
511 regval_ctr_addvalue( values
, valuename
, type
, (const char *)data_p
, size
);
512 SAFE_FREE(data_p
); /* 'B' option to tdb_unpack does a malloc() */
515 DEBUG(8,("specific: [%s], len: %d\n", valuename
, size
));
521 /****************************************************************************
522 Pack all values in all printer keys
523 ***************************************************************************/
525 static int regdb_pack_values(REGVAL_CTR
*values
, char *buf
, int buflen
)
535 num_values
= regval_ctr_numvals( values
);
537 /* pack the number of values first */
539 len
+= tdb_pack( buf
+len
, buflen
-len
, "d", num_values
);
541 /* loop over all values */
543 for ( i
=0; i
<num_values
; i
++ ) {
544 val
= regval_ctr_specific_value( values
, i
);
545 len
+= tdb_pack(buf
+len
, buflen
-len
, "fdB",
549 regval_data_p(val
) );
555 /***********************************************************************
556 Retrieve an array of strings containing subkeys. Memory should be
557 released by the caller.
558 ***********************************************************************/
560 int regdb_fetch_values( const char* key
, REGVAL_CTR
*values
)
565 DEBUG(10,("regdb_fetch_values: Looking for value of key [%s] \n", key
));
567 pstr_sprintf( keystr
, "%s/%s", VALUE_PREFIX
, key
);
568 normalize_reg_path( keystr
);
570 data
= tdb_fetch_bystring( tdb_reg
, keystr
);
573 /* all keys have zero values by default */
577 regdb_unpack_values( values
, data
.dptr
, data
.dsize
);
579 SAFE_FREE( data
.dptr
);
581 return regval_ctr_numvals(values
);
584 /***********************************************************************
585 Stub function since we do not currently support storing registry
586 values in the registry.tdb
587 ***********************************************************************/
589 BOOL
regdb_store_values( const char *key
, REGVAL_CTR
*values
)
595 DEBUG(10,("regdb_store_values: Looking for value of key [%s] \n", key
));
599 len
= regdb_pack_values( values
, data
.dptr
, data
.dsize
);
601 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
605 data
.dptr
= SMB_MALLOC_ARRAY( char, len
);
608 len
= regdb_pack_values( values
, data
.dptr
, data
.dsize
);
610 SMB_ASSERT( len
== data
.dsize
);
612 pstr_sprintf( keystr
, "%s/%s", VALUE_PREFIX
, key
);
613 normalize_reg_path( keystr
);
615 ret
= tdb_store_bystring(tdb_reg
, keystr
, data
, TDB_REPLACE
);
617 SAFE_FREE( data
.dptr
);
624 * Table of function pointers for default access
627 REGISTRY_OPS regdb_ops
= {