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
;
30 #define VALUE_PREFIX "SAMBA_REGVAL"
32 /* List the deepest path into the registry. All part components will be created.*/
34 /* If you want to have a part of the path controlled by the tdb and part by
35 a virtual registry db (e.g. printing), then you have to list the deepest path.
36 For example,"HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion/Print"
37 allows the reg_db backend to handle everything up to
38 "HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion" and then we'll hook
39 the reg_printing backend onto the last component of the path (see
40 KEY_PRINTING_2K in include/rpc_reg.h) --jerry */
42 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 #define REGVER_V1 1 /* first db version with write support */
85 /***********************************************************************
86 Open the registry data in the tdb
87 ***********************************************************************/
89 static BOOL
init_registry_data( void )
91 pstring path
, base
, remaining
;
92 fstring keyname
, subkeyname
;
93 REGSUBKEY_CTR
*subkeys
;
100 /* create a new top level talloc ctx */
102 if ( !(ctx
= TALLOC_P( NULL
, uint32
)) ) {
103 DEBUG(0,("init_registry_data: top level talloc() failure!\n"));
107 /* loop over all of the predefined paths and add each component */
109 for ( i
=0; builtin_registry_paths
[i
] != NULL
; i
++ ) {
111 DEBUG(6,("init_registry_data: Adding [%s]\n", builtin_registry_paths
[i
]));
113 pstrcpy( path
, builtin_registry_paths
[i
] );
117 while ( next_token(&p
, keyname
, "\\", sizeof(keyname
)) ) {
119 /* build up the registry path from the components */
122 pstrcat( base
, "\\" );
123 pstrcat( base
, keyname
);
125 /* get the immediate subkeyname (if we have one ) */
129 pstrcpy( remaining
, p
);
132 if ( !next_token(&p2
, subkeyname
, "\\", sizeof(subkeyname
)) )
133 fstrcpy( subkeyname
, p2
);
136 DEBUG(10,("init_registry_data: Storing key [%s] with subkey [%s]\n",
137 base
, *subkeyname
? subkeyname
: "NULL"));
139 /* we don't really care if the lookup succeeds or not since
140 we are about to update the record. We just want any
141 subkeys already present */
143 if ( !(subkeys
= TALLOC_ZERO_P( ctx
, REGSUBKEY_CTR
)) ) {
144 DEBUG(0,("talloc() failure!\n"));
148 regdb_fetch_keys( base
, subkeys
);
150 regsubkey_ctr_addkey( subkeys
, subkeyname
);
151 if ( !regdb_store_keys( base
, subkeys
))
154 TALLOC_FREE( subkeys
);
158 /* loop over all of the predefined values and add each component */
160 for ( i
=0; builtin_registry_values
[i
].path
!= NULL
; i
++ ) {
161 if ( !(values
= TALLOC_ZERO_P( ctx
, REGVAL_CTR
)) ) {
162 DEBUG(0,("talloc() failure!\n"));
166 regdb_fetch_values( builtin_registry_values
[i
].path
, values
);
168 /* preserve existing values across restarts. Only add new ones */
170 if ( !regval_ctr_key_exists( values
, builtin_registry_values
[i
].valuename
) )
172 switch( builtin_registry_values
[i
].type
) {
174 regval_ctr_addvalue( values
,
175 builtin_registry_values
[i
].valuename
,
177 (char*)&builtin_registry_values
[i
].data
.dw_value
,
182 init_unistr2( &data
, builtin_registry_values
[i
].data
.string
, UNI_STR_TERMINATE
);
183 regval_ctr_addvalue( values
,
184 builtin_registry_values
[i
].valuename
,
187 data
.uni_str_len
*sizeof(uint16
) );
191 DEBUG(0,("init_registry_data: invalid value type in builtin_registry_values [%d]\n",
192 builtin_registry_values
[i
].type
));
194 regdb_store_values( builtin_registry_values
[i
].path
, values
);
197 TALLOC_FREE( values
);
203 /***********************************************************************
204 Open the registry database
205 ***********************************************************************/
207 BOOL
init_registry_db( void )
209 const char *vstring
= "INFO/version";
215 if ( !(tdb_reg
= tdb_open_log(lock_path("registry.tdb"), 0, TDB_DEFAULT
, O_RDWR
, 0600)) )
217 tdb_reg
= tdb_open_log(lock_path("registry.tdb"), 0, TDB_DEFAULT
, O_RDWR
|O_CREAT
, 0600);
219 DEBUG(0,("init_registry: Failed to open registry %s (%s)\n",
220 lock_path("registry.tdb"), strerror(errno
) ));
224 DEBUG(10,("init_registry: Successfully created registry tdb\n"));
228 vers_id
= tdb_fetch_int32(tdb_reg
, vstring
);
230 if ( vers_id
!= REGVER_V1
) {
231 /* any upgrade code here if needed */
234 /* always setup the necessary keys and values */
236 if ( !init_registry_data() ) {
237 DEBUG(0,("init_registry: Failed to initiailize data in registry!\n"));
244 /***********************************************************************
245 Add subkey strings to the registry tdb under a defined key
246 fmt is the same format as tdb_pack except this function only supports
248 ***********************************************************************/
250 static BOOL
regdb_store_keys_internal( const char *key
, REGSUBKEY_CTR
*ctr
)
253 char *buffer
, *tmpbuf
;
257 uint32 num_subkeys
= regsubkey_ctr_numkeys( ctr
);
263 pstrcpy( keyname
, key
);
264 normalize_reg_path( keyname
);
266 /* allocate some initial memory */
268 buffer
= SMB_MALLOC(sizeof(pstring
));
269 buflen
= sizeof(pstring
);
272 /* store the number of subkeys */
274 len
+= tdb_pack(buffer
+len
, buflen
-len
, "d", num_subkeys
);
276 /* pack all the strings */
278 for (i
=0; i
<num_subkeys
; i
++) {
279 len
+= tdb_pack( buffer
+len
, buflen
-len
, "f", regsubkey_ctr_specific_key(ctr
, i
) );
280 if ( len
> buflen
) {
281 /* allocate some extra space */
282 if ((tmpbuf
= SMB_REALLOC( buffer
, len
*2 )) == NULL
) {
283 DEBUG(0,("regdb_store_keys: Failed to realloc memory of size [%d]\n", len
*2));
290 len
= tdb_pack( buffer
+len
, buflen
-len
, "f", regsubkey_ctr_specific_key(ctr
, i
) );
294 /* finally write out the data */
297 kbuf
.dsize
= strlen(keyname
)+1;
300 if ( tdb_store( tdb_reg
, kbuf
, dbuf
, TDB_REPLACE
) == -1) {
311 /***********************************************************************
312 Store the new subkey record and create any child key records that
313 do not currently exist
314 ***********************************************************************/
316 BOOL
regdb_store_keys( const char *key
, REGSUBKEY_CTR
*ctr
)
320 REGSUBKEY_CTR
*subkeys
, *old_subkeys
;
323 /* fetch a list of the old subkeys so we can determine if any were deleted */
325 if ( !(old_subkeys
= TALLOC_ZERO_P( ctr
, REGSUBKEY_CTR
)) ) {
326 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
330 regdb_fetch_keys( key
, old_subkeys
);
332 /* store the subkey list for the parent */
334 if ( !regdb_store_keys_internal( key
, ctr
) ) {
335 DEBUG(0,("regdb_store_keys: Failed to store new subkey list for parent [%s}\n", key
));
339 /* now delete removed keys */
341 num_subkeys
= regsubkey_ctr_numkeys( old_subkeys
);
342 for ( i
=0; i
<num_subkeys
; i
++ ) {
343 oldkeyname
= regsubkey_ctr_specific_key( old_subkeys
, i
);
344 if ( !regsubkey_ctr_key_exists( ctr
, oldkeyname
) ) {
345 pstr_sprintf( path
, "%s%c%s", key
, '/', oldkeyname
);
346 normalize_reg_path( path
);
347 tdb_delete_bystring( tdb_reg
, path
);
351 TALLOC_FREE( old_subkeys
);
353 /* now create records for any subkeys that don't already exist */
355 num_subkeys
= regsubkey_ctr_numkeys( ctr
);
356 for ( i
=0; i
<num_subkeys
; i
++ ) {
357 pstr_sprintf( path
, "%s%c%s", key
, '/', regsubkey_ctr_specific_key( ctr
, i
) );
359 if ( !(subkeys
= TALLOC_ZERO_P( ctr
, REGSUBKEY_CTR
)) ) {
360 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
364 if ( regdb_fetch_keys( path
, subkeys
) == -1 ) {
365 /* create a record with 0 subkeys */
366 if ( !regdb_store_keys_internal( path
, subkeys
) ) {
367 DEBUG(0,("regdb_store_keys: Failed to store new record for key [%s}\n", path
));
368 TALLOC_FREE( subkeys
);
373 TALLOC_FREE( subkeys
);
380 /***********************************************************************
381 Retrieve an array of strings containing subkeys. Memory should be
382 released by the caller.
383 ***********************************************************************/
385 int regdb_fetch_keys( const char* key
, REGSUBKEY_CTR
*ctr
)
395 DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key
? key
: "NULL"));
397 pstrcpy( path
, key
);
399 /* convert to key format */
400 pstring_sub( path
, "\\", "/" );
403 dbuf
= tdb_fetch_bystring( tdb_reg
, path
);
409 DEBUG(5,("regdb_fetch_keys: tdb lookup failed to locate key [%s]\n", key
));
413 len
= tdb_unpack( buf
, buflen
, "d", &num_items
);
415 for (i
=0; i
<num_items
; i
++) {
416 len
+= tdb_unpack( buf
+len
, buflen
-len
, "f", subkeyname
);
417 regsubkey_ctr_addkey( ctr
, subkeyname
);
420 SAFE_FREE( dbuf
.dptr
);
422 DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items
));
427 /****************************************************************************
428 Unpack a list of registry values frem the TDB
429 ***************************************************************************/
431 static int regdb_unpack_values(REGVAL_CTR
*values
, char *buf
, int buflen
)
438 uint32 num_values
= 0;
443 /* loop and unpack the rest of the registry values */
445 len
+= tdb_unpack(buf
+len
, buflen
-len
, "d", &num_values
);
447 for ( i
=0; i
<num_values
; i
++ ) {
448 /* unpack the next regval */
453 len
+= tdb_unpack(buf
+len
, buflen
-len
, "fdB",
459 /* add the new value. Paranoid protective code -- make sure data_p is valid */
461 if ( size
&& data_p
) {
462 regval_ctr_addvalue( values
, valuename
, type
, (const char *)data_p
, size
);
463 SAFE_FREE(data_p
); /* 'B' option to tdb_unpack does a malloc() */
466 DEBUG(8,("specific: [%s], len: %d\n", valuename
, size
));
472 /****************************************************************************
473 Pack all values in all printer keys
474 ***************************************************************************/
476 static int regdb_pack_values(REGVAL_CTR
*values
, char *buf
, int buflen
)
481 int num_values
= regval_ctr_numvals( values
);
486 /* pack the number of values first */
488 len
+= tdb_pack( buf
+len
, buflen
-len
, "d", num_values
);
490 /* loop over all values */
492 for ( i
=0; i
<num_values
; i
++ ) {
493 val
= regval_ctr_specific_value( values
, i
);
494 len
+= tdb_pack(buf
+len
, buflen
-len
, "fdB",
498 regval_data_p(val
) );
504 /***********************************************************************
505 Retrieve an array of strings containing subkeys. Memory should be
506 released by the caller.
507 ***********************************************************************/
509 int regdb_fetch_values( const char* key
, REGVAL_CTR
*values
)
514 DEBUG(10,("regdb_fetch_values: Looking for value of key [%s] \n", key
));
516 pstr_sprintf( keystr
, "%s/%s", VALUE_PREFIX
, key
);
517 normalize_reg_path( keystr
);
519 data
= tdb_fetch_bystring( tdb_reg
, keystr
);
522 /* all keys have zero values by default */
526 regdb_unpack_values( values
, data
.dptr
, data
.dsize
);
528 SAFE_FREE( data
.dptr
);
530 return regval_ctr_numvals(values
);
533 /***********************************************************************
534 Stub function since we do not currently support storing registry
535 values in the registry.tdb
536 ***********************************************************************/
538 BOOL
regdb_store_values( const char *key
, REGVAL_CTR
*values
)
544 DEBUG(10,("regdb_store_values: Looking for value of key [%s] \n", key
));
548 len
= regdb_pack_values( values
, data
.dptr
, data
.dsize
);
550 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
554 data
.dptr
= SMB_MALLOC_ARRAY( char, len
);
557 len
= regdb_pack_values( values
, data
.dptr
, data
.dsize
);
559 SMB_ASSERT( len
== data
.dsize
);
561 pstr_sprintf( keystr
, "%s/%s", VALUE_PREFIX
, key
);
562 normalize_reg_path( keystr
);
564 ret
= tdb_store_bystring(tdb_reg
, keystr
, data
, TDB_REPLACE
);
566 SAFE_FREE( data
.dptr
);
573 * Table of function pointers for default access
576 REGISTRY_OPS regdb_ops
= {