r9895: fix typo in registry path
[Samba.git] / source / registry / reg_db.c
blobafb5c613c802d2f224980b8e51c0ca8fa87faa54
1 /*
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. */
23 #include "includes.h"
25 #undef DBGC_CLASS
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[] = {
43 KEY_PRINTING_2K,
44 KEY_PRINTING_PORTS,
45 KEY_PRINTING,
46 KEY_SHARES,
47 KEY_EVENTLOG,
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",
55 "HKU",
56 "HKCR",
57 "HKPD",
58 "HKPT",
59 NULL };
61 struct builtin_regkey_value {
62 const char *path;
63 const char *valuename;
64 uint32 type;
65 union {
66 const char *string;
67 uint32 dw_value;
68 } data;
71 static struct builtin_regkey_value builtin_registry_values[] = {
72 { KEY_PRINTING_PORTS,
73 SAMBA_PRINTER_PORT_NAME, REG_SZ, { "" } },
74 { KEY_PRINTING_2K,
75 "DefaultSpoolDirectory", REG_SZ, { "C:\\Windows\\System32\\Spool\\Printers" } },
76 { KEY_EVENTLOG,
77 "DisplayName", REG_SZ, { "Event Log" } },
78 { KEY_EVENTLOG,
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;
94 REGVAL_CTR *values;
95 uint32 *ctx;
96 int i;
97 const char *p, *p2;
98 UNISTR2 data;
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"));
104 return False;
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] );
114 pstrcpy( base, "" );
115 p = path;
117 while ( next_token(&p, keyname, "\\", sizeof(keyname)) ) {
119 /* build up the registry path from the components */
121 if ( *base )
122 pstrcat( base, "\\" );
123 pstrcat( base, keyname );
125 /* get the immediate subkeyname (if we have one ) */
127 *subkeyname = '\0';
128 if ( *p ) {
129 pstrcpy( remaining, p );
130 p2 = remaining;
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"));
145 return False;
148 regdb_fetch_keys( base, subkeys );
149 if ( *subkeyname )
150 regsubkey_ctr_addkey( subkeys, subkeyname );
151 if ( !regdb_store_keys( base, subkeys ))
152 return False;
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"));
163 return False;
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 ) {
173 case REG_DWORD:
174 regval_ctr_addvalue( values,
175 builtin_registry_values[i].valuename,
176 REG_DWORD,
177 (char*)&builtin_registry_values[i].data.dw_value,
178 sizeof(uint32) );
179 break;
181 case REG_SZ:
182 init_unistr2( &data, builtin_registry_values[i].data.string, UNI_STR_TERMINATE);
183 regval_ctr_addvalue( values,
184 builtin_registry_values[i].valuename,
185 REG_SZ,
186 (char*)data.buffer,
187 data.uni_str_len*sizeof(uint16) );
188 break;
190 default:
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 );
200 return True;
203 /***********************************************************************
204 Open the registry database
205 ***********************************************************************/
207 BOOL init_registry_db( void )
209 const char *vstring = "INFO/version";
210 uint32 vers_id;
212 if ( tdb_reg )
213 return True;
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);
218 if ( !tdb_reg ) {
219 DEBUG(0,("init_registry: Failed to open registry %s (%s)\n",
220 lock_path("registry.tdb"), strerror(errno) ));
221 return False;
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"));
238 return False;
241 return True;
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
247 fstrings
248 ***********************************************************************/
250 static BOOL regdb_store_keys_internal( const char *key, REGSUBKEY_CTR *ctr )
252 TDB_DATA kbuf, dbuf;
253 char *buffer, *tmpbuf;
254 int i = 0;
255 uint32 len, buflen;
256 BOOL ret = True;
257 uint32 num_subkeys = regsubkey_ctr_numkeys( ctr );
258 pstring keyname;
260 if ( !key )
261 return False;
263 pstrcpy( keyname, key );
264 normalize_reg_path( keyname );
266 /* allocate some initial memory */
268 buffer = SMB_MALLOC(sizeof(pstring));
269 buflen = sizeof(pstring);
270 len = 0;
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));
284 ret = False;
285 goto done;
287 buffer = tmpbuf;
288 buflen = len*2;
290 len = tdb_pack( buffer+len, buflen-len, "f", regsubkey_ctr_specific_key(ctr, i) );
294 /* finally write out the data */
296 kbuf.dptr = keyname;
297 kbuf.dsize = strlen(keyname)+1;
298 dbuf.dptr = buffer;
299 dbuf.dsize = len;
300 if ( tdb_store( tdb_reg, kbuf, dbuf, TDB_REPLACE ) == -1) {
301 ret = False;
302 goto done;
305 done:
306 SAFE_FREE( buffer );
308 return ret;
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 )
318 int num_subkeys, i;
319 pstring path;
320 REGSUBKEY_CTR *subkeys, *old_subkeys;
321 char *oldkeyname;
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"));
327 return False;
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 ));
336 return False;
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"));
361 return False;
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 );
369 return False;
373 TALLOC_FREE( subkeys );
376 return True;
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 )
387 pstring path;
388 uint32 num_items;
389 TDB_DATA dbuf;
390 char *buf;
391 uint32 buflen, len;
392 int i;
393 fstring subkeyname;
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, "\\", "/" );
401 strupper_m( path );
403 dbuf = tdb_fetch_bystring( tdb_reg, path );
405 buf = dbuf.dptr;
406 buflen = dbuf.dsize;
408 if ( !buf ) {
409 DEBUG(5,("regdb_fetch_keys: tdb lookup failed to locate key [%s]\n", key));
410 return -1;
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));
424 return 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)
433 int len = 0;
434 uint32 type;
435 pstring valuename;
436 uint32 size;
437 uint8 *data_p;
438 uint32 num_values = 0;
439 int i;
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 */
450 type = REG_NONE;
451 size = 0;
452 data_p = NULL;
453 len += tdb_unpack(buf+len, buflen-len, "fdB",
454 valuename,
455 &type,
456 &size,
457 &data_p);
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));
469 return len;
472 /****************************************************************************
473 Pack all values in all printer keys
474 ***************************************************************************/
476 static int regdb_pack_values(REGVAL_CTR *values, char *buf, int buflen)
478 int len = 0;
479 int i;
480 REGISTRY_VALUE *val;
481 int num_values = regval_ctr_numvals( values );
483 if ( !values )
484 return 0;
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",
495 regval_name(val),
496 regval_type(val),
497 regval_size(val),
498 regval_data_p(val) );
501 return len;
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 )
511 TDB_DATA data;
512 pstring keystr;
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 );
521 if ( !data.dptr ) {
522 /* all keys have zero values by default */
523 return 0;
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 )
540 TDB_DATA data;
541 pstring keystr;
542 int len, ret;
544 DEBUG(10,("regdb_store_values: Looking for value of key [%s] \n", key));
546 ZERO_STRUCT( data );
548 len = regdb_pack_values( values, data.dptr, data.dsize );
549 if ( len <= 0 ) {
550 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
551 return False;
554 data.dptr = SMB_MALLOC_ARRAY( char, len );
555 data.dsize = 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 );
568 return ret != -1 ;
573 * Table of function pointers for default access
576 REGISTRY_OPS regdb_ops = {
577 regdb_fetch_keys,
578 regdb_fetch_values,
579 regdb_store_keys,
580 regdb_store_values,
581 NULL