r10391: * setting version to 3.0.20a
[Samba.git] / source / registry / reg_db.c
blob888f7ca73574a612f62b17bcfce9c858f5d16aa9
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 abd 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 int i;
96 const char *p, *p2;
97 UNISTR2 data;
99 /* loop over all of the predefined paths and add each component */
101 for ( i=0; builtin_registry_paths[i] != NULL; i++ ) {
103 DEBUG(6,("init_registry_data: Adding [%s]\n", builtin_registry_paths[i]));
105 pstrcpy( path, builtin_registry_paths[i] );
106 pstrcpy( base, "" );
107 p = path;
109 while ( next_token(&p, keyname, "\\", sizeof(keyname)) ) {
111 /* build up the registry path from the components */
113 if ( *base )
114 pstrcat( base, "\\" );
115 pstrcat( base, keyname );
117 /* get the immediate subkeyname (if we have one ) */
119 *subkeyname = '\0';
120 if ( *p ) {
121 pstrcpy( remaining, p );
122 p2 = remaining;
124 if ( !next_token(&p2, subkeyname, "\\", sizeof(subkeyname)) )
125 fstrcpy( subkeyname, p2 );
128 DEBUG(10,("init_registry_data: Storing key [%s] with subkey [%s]\n",
129 base, *subkeyname ? subkeyname : "NULL"));
131 /* we don't really care if the lookup succeeds or not since
132 we are about to update the record. We just want any
133 subkeys already present */
135 regsubkey_ctr_init( &subkeys );
137 regdb_fetch_keys( base, &subkeys );
138 if ( *subkeyname )
139 regsubkey_ctr_addkey( &subkeys, subkeyname );
140 if ( !regdb_store_keys( base, &subkeys ))
141 return False;
143 regsubkey_ctr_destroy( &subkeys );
147 /* loop over all of the predefined values and add each component */
149 for ( i=0; builtin_registry_values[i].path != NULL; i++ ) {
150 regval_ctr_init( &values );
152 regdb_fetch_values( builtin_registry_values[i].path, &values );
154 /* preserve existing values across restarts. Only add new ones */
156 if ( !regval_ctr_key_exists( &values, builtin_registry_values[i].valuename ) )
158 switch( builtin_registry_values[i].type ) {
159 case REG_DWORD:
160 regval_ctr_addvalue( &values,
161 builtin_registry_values[i].valuename,
162 REG_DWORD,
163 (char*)&builtin_registry_values[i].data.dw_value,
164 sizeof(uint32) );
165 break;
167 case REG_SZ:
168 init_unistr2( &data, builtin_registry_values[i].data.string, UNI_STR_TERMINATE);
169 regval_ctr_addvalue( &values,
170 builtin_registry_values[i].valuename,
171 REG_SZ,
172 (char*)data.buffer,
173 data.uni_str_len*sizeof(uint16) );
174 break;
176 default:
177 DEBUG(0,("init_registry_data: invalid value type in builtin_registry_values [%d]\n",
178 builtin_registry_values[i].type));
180 regdb_store_values( builtin_registry_values[i].path, &values );
183 regval_ctr_destroy( &values );
186 return True;
189 /***********************************************************************
190 Open the registry database
191 ***********************************************************************/
193 BOOL init_registry_db( void )
195 const char *vstring = "INFO/version";
196 uint32 vers_id;
198 if ( tdb_reg )
199 return True;
201 if ( !(tdb_reg = tdb_open_log(lock_path("registry.tdb"), 0, TDB_DEFAULT, O_RDWR, 0600)) )
203 tdb_reg = tdb_open_log(lock_path("registry.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
204 if ( !tdb_reg ) {
205 DEBUG(0,("init_registry: Failed to open registry %s (%s)\n",
206 lock_path("registry.tdb"), strerror(errno) ));
207 return False;
210 DEBUG(10,("init_registry: Successfully created registry tdb\n"));
214 vers_id = tdb_fetch_int32(tdb_reg, vstring);
216 if ( vers_id != REGVER_V1 ) {
217 /* any upgrade code here if needed */
220 /* always setup the necessary keys and values */
222 if ( !init_registry_data() ) {
223 DEBUG(0,("init_registry: Failed to initiailize data in registry!\n"));
224 return False;
227 return True;
230 /***********************************************************************
231 Add subkey strings to the registry tdb under a defined key
232 fmt is the same format as tdb_pack except this function only supports
233 fstrings
234 ***********************************************************************/
236 static BOOL regdb_store_keys_internal( const char *key, REGSUBKEY_CTR *ctr )
238 TDB_DATA kbuf, dbuf;
239 char *buffer, *tmpbuf;
240 int i = 0;
241 uint32 len, buflen;
242 BOOL ret = True;
243 uint32 num_subkeys = regsubkey_ctr_numkeys( ctr );
244 pstring keyname;
246 if ( !key )
247 return False;
249 pstrcpy( keyname, key );
250 normalize_reg_path( keyname );
252 /* allocate some initial memory */
254 buffer = SMB_MALLOC(sizeof(pstring));
255 buflen = sizeof(pstring);
256 len = 0;
258 /* store the number of subkeys */
260 len += tdb_pack(buffer+len, buflen-len, "d", num_subkeys );
262 /* pack all the strings */
264 for (i=0; i<num_subkeys; i++) {
265 len += tdb_pack( buffer+len, buflen-len, "f", regsubkey_ctr_specific_key(ctr, i) );
266 if ( len > buflen ) {
267 /* allocate some extra space */
268 if ((tmpbuf = SMB_REALLOC( buffer, len*2 )) == NULL) {
269 DEBUG(0,("regdb_store_keys: Failed to realloc memory of size [%d]\n", len*2));
270 ret = False;
271 goto done;
273 buffer = tmpbuf;
274 buflen = len*2;
276 len = tdb_pack( buffer+len, buflen-len, "f", regsubkey_ctr_specific_key(ctr, i) );
280 /* finally write out the data */
282 kbuf.dptr = keyname;
283 kbuf.dsize = strlen(keyname)+1;
284 dbuf.dptr = buffer;
285 dbuf.dsize = len;
286 if ( tdb_store( tdb_reg, kbuf, dbuf, TDB_REPLACE ) == -1) {
287 ret = False;
288 goto done;
291 done:
292 SAFE_FREE( buffer );
294 return ret;
297 /***********************************************************************
298 Store the new subkey record and create any child key records that
299 do not currently exist
300 ***********************************************************************/
302 BOOL regdb_store_keys( const char *key, REGSUBKEY_CTR *ctr )
304 int num_subkeys, i;
305 pstring path;
306 REGSUBKEY_CTR subkeys, old_subkeys;
307 char *oldkeyname;
309 /* fetch a list of the old subkeys so we can determine if any were deleted */
311 regsubkey_ctr_init( &old_subkeys );
312 regdb_fetch_keys( key, &old_subkeys );
314 /* store the subkey list for the parent */
316 if ( !regdb_store_keys_internal( key, ctr ) ) {
317 DEBUG(0,("regdb_store_keys: Failed to store new subkey list for parent [%s}\n", key ));
318 return False;
321 /* now delete removed keys */
323 num_subkeys = regsubkey_ctr_numkeys( &old_subkeys );
324 for ( i=0; i<num_subkeys; i++ ) {
325 oldkeyname = regsubkey_ctr_specific_key( &old_subkeys, i );
326 if ( !regsubkey_ctr_key_exists( ctr, oldkeyname ) ) {
327 pstr_sprintf( path, "%s%c%s", key, '/', oldkeyname );
328 normalize_reg_path( path );
329 tdb_delete_bystring( tdb_reg, path );
333 regsubkey_ctr_destroy( &old_subkeys );
335 /* now create records for any subkeys that don't already exist */
337 num_subkeys = regsubkey_ctr_numkeys( ctr );
338 for ( i=0; i<num_subkeys; i++ ) {
339 pstr_sprintf( path, "%s%c%s", key, '/', regsubkey_ctr_specific_key( ctr, i ) );
340 regsubkey_ctr_init( &subkeys );
341 if ( regdb_fetch_keys( path, &subkeys ) == -1 ) {
342 /* create a record with 0 subkeys */
343 if ( !regdb_store_keys_internal( path, &subkeys ) ) {
344 DEBUG(0,("regdb_store_keys: Failed to store new record for key [%s}\n", path ));
345 regsubkey_ctr_destroy( &subkeys );
346 return False;
349 regsubkey_ctr_destroy( &subkeys );
352 return True;
356 /***********************************************************************
357 Retrieve an array of strings containing subkeys. Memory should be
358 released by the caller.
359 ***********************************************************************/
361 int regdb_fetch_keys( const char* key, REGSUBKEY_CTR *ctr )
363 pstring path;
364 uint32 num_items;
365 TDB_DATA dbuf;
366 char *buf;
367 uint32 buflen, len;
368 int i;
369 fstring subkeyname;
371 DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key ? key : "NULL"));
373 pstrcpy( path, key );
375 /* convert to key format */
376 pstring_sub( path, "\\", "/" );
377 strupper_m( path );
379 dbuf = tdb_fetch_bystring( tdb_reg, path );
381 buf = dbuf.dptr;
382 buflen = dbuf.dsize;
384 if ( !buf ) {
385 DEBUG(5,("regdb_fetch_keys: tdb lookup failed to locate key [%s]\n", key));
386 return -1;
389 len = tdb_unpack( buf, buflen, "d", &num_items);
391 for (i=0; i<num_items; i++) {
392 len += tdb_unpack( buf+len, buflen-len, "f", subkeyname );
393 regsubkey_ctr_addkey( ctr, subkeyname );
396 SAFE_FREE( dbuf.dptr );
398 DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items));
400 return num_items;
403 /****************************************************************************
404 Unpack a list of registry values frem the TDB
405 ***************************************************************************/
407 static int regdb_unpack_values(REGVAL_CTR *values, char *buf, int buflen)
409 int len = 0;
410 uint32 type;
411 pstring valuename;
412 uint32 size;
413 uint8 *data_p;
414 uint32 num_values = 0;
415 int i;
419 /* loop and unpack the rest of the registry values */
421 len += tdb_unpack(buf+len, buflen-len, "d", &num_values);
423 for ( i=0; i<num_values; i++ ) {
424 /* unpack the next regval */
426 type = REG_NONE;
427 size = 0;
428 data_p = NULL;
429 len += tdb_unpack(buf+len, buflen-len, "fdB",
430 valuename,
431 &type,
432 &size,
433 &data_p);
435 /* add the new value. Paranoid protective code -- make sure data_p is valid */
437 if ( size && data_p ) {
438 regval_ctr_addvalue( values, valuename, type, (const char *)data_p, size );
439 SAFE_FREE(data_p); /* 'B' option to tdb_unpack does a malloc() */
442 DEBUG(8,("specific: [%s], len: %d\n", valuename, size));
445 return len;
448 /****************************************************************************
449 Pack all values in all printer keys
450 ***************************************************************************/
452 static int regdb_pack_values(REGVAL_CTR *values, char *buf, int buflen)
454 int len = 0;
455 int i;
456 REGISTRY_VALUE *val;
457 int num_values = regval_ctr_numvals( values );
459 if ( !values )
460 return 0;
462 /* pack the number of values first */
464 len += tdb_pack( buf+len, buflen-len, "d", num_values );
466 /* loop over all values */
468 for ( i=0; i<num_values; i++ ) {
469 val = regval_ctr_specific_value( values, i );
470 len += tdb_pack(buf+len, buflen-len, "fdB",
471 regval_name(val),
472 regval_type(val),
473 regval_size(val),
474 regval_data_p(val) );
477 return len;
480 /***********************************************************************
481 Retrieve an array of strings containing subkeys. Memory should be
482 released by the caller.
483 ***********************************************************************/
485 int regdb_fetch_values( const char* key, REGVAL_CTR *values )
487 TDB_DATA data;
488 pstring keystr;
489 int len;
491 DEBUG(10,("regdb_fetch_values: Looking for value of key [%s] \n", key));
493 pstr_sprintf( keystr, "%s/%s", VALUE_PREFIX, key );
494 normalize_reg_path( keystr );
496 data = tdb_fetch_bystring( tdb_reg, keystr );
498 if ( !data.dptr ) {
499 /* all keys have zero values by default */
500 return 0;
503 len = regdb_unpack_values( values, data.dptr, data.dsize );
505 SAFE_FREE( data.dptr );
507 return regval_ctr_numvals(values);
510 /***********************************************************************
511 Stub function since we do not currently support storing registry
512 values in the registry.tdb
513 ***********************************************************************/
515 BOOL regdb_store_values( const char *key, REGVAL_CTR *values )
517 TDB_DATA data;
518 pstring keystr;
519 int len, ret;
521 DEBUG(10,("regdb_store_values: Looking for value of key [%s] \n", key));
523 ZERO_STRUCT( data );
525 len = regdb_pack_values( values, data.dptr, data.dsize );
526 if ( len <= 0 ) {
527 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
528 return False;
531 data.dptr = SMB_MALLOC_ARRAY( char, len );
532 data.dsize = len;
534 len = regdb_pack_values( values, data.dptr, data.dsize );
536 SMB_ASSERT( len == data.dsize );
538 pstr_sprintf( keystr, "%s/%s", VALUE_PREFIX, key );
539 normalize_reg_path( keystr );
541 ret = tdb_store_bystring(tdb_reg, keystr, data, TDB_REPLACE);
543 SAFE_FREE( data.dptr );
545 return ret != -1 ;
550 * Table of function pointers for default access
553 REGISTRY_OPS regdb_ops = {
554 regdb_fetch_keys,
555 regdb_fetch_values,
556 regdb_store_keys,
557 regdb_store_values,
558 NULL