r8026: * more fixes to the printing registry interface
[Samba.git] / source / registry / reg_db.c
blob4bb605ade015734758ce5c5c6152a55a9762b59a
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\\SYSTEM\\CurrentControlSet\\Control\\ProductOptions",
49 "HKLM\\SYSTEM\\CurrentControlSet\\Services\\TcpIp\\Parameters",
50 "HKLM\\SYSTEM\\CurrentControlSet\\Services\\Netlogon\\Parameters",
51 "HKU",
52 "HKCR",
53 NULL };
55 struct builtin_regkey_value {
56 const char *path;
57 const char *valuename;
58 uint32 type;
59 union {
60 const char *string;
61 uint32 dw_value;
62 } data;
65 static struct builtin_regkey_value builtin_registry_values[] = {
66 { "HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", "SystemRoot", REG_SZ, { "c:\\Windows" } },
67 { NULL, NULL, 0, { NULL } }
70 #define REGVER_V1 1 /* first db version with write support */
72 /***********************************************************************
73 Open the registry data in the tdb
74 ***********************************************************************/
76 static BOOL init_registry_data( void )
78 pstring path, base, remaining;
79 fstring keyname, subkeyname;
80 REGSUBKEY_CTR subkeys;
81 REGVAL_CTR values;
82 int i;
83 const char *p, *p2;
84 UNISTR2 data;
86 /* loop over all of the predefined paths and add each component */
88 for ( i=0; builtin_registry_paths[i] != NULL; i++ ) {
90 DEBUG(6,("init_registry_data: Adding [%s]\n", builtin_registry_paths[i]));
92 pstrcpy( path, builtin_registry_paths[i] );
93 pstrcpy( base, "" );
94 p = path;
96 while ( next_token(&p, keyname, "\\", sizeof(keyname)) ) {
98 /* build up the registry path from the components */
100 if ( *base )
101 pstrcat( base, "\\" );
102 pstrcat( base, keyname );
104 /* get the immediate subkeyname (if we have one ) */
106 *subkeyname = '\0';
107 if ( *p ) {
108 pstrcpy( remaining, p );
109 p2 = remaining;
111 if ( !next_token(&p2, subkeyname, "\\", sizeof(subkeyname)) )
112 fstrcpy( subkeyname, p2 );
115 DEBUG(10,("init_registry_data: Storing key [%s] with subkey [%s]\n",
116 base, *subkeyname ? subkeyname : "NULL"));
118 /* we don't really care if the lookup succeeds or not since
119 we are about to update the record. We just want any
120 subkeys already present */
122 regsubkey_ctr_init( &subkeys );
124 regdb_fetch_keys( base, &subkeys );
125 if ( *subkeyname )
126 regsubkey_ctr_addkey( &subkeys, subkeyname );
127 if ( !regdb_store_keys( base, &subkeys ))
128 return False;
130 regsubkey_ctr_destroy( &subkeys );
134 /* loop over all of the predefined values and add each component */
136 for ( i=0; builtin_registry_values[i].path != NULL; i++ ) {
137 regval_ctr_init( &values );
139 regdb_fetch_values( builtin_registry_values[i].path, &values );
140 switch( builtin_registry_values[i].type ) {
141 case REG_DWORD:
142 regval_ctr_addvalue( &values,
143 builtin_registry_values[i].valuename,
144 REG_DWORD,
145 (char*)&builtin_registry_values[i].data.dw_value,
146 sizeof(uint32) );
147 break;
149 case REG_SZ:
150 init_unistr2( &data, builtin_registry_values[i].data.string, UNI_STR_TERMINATE);
151 regval_ctr_addvalue( &values,
152 builtin_registry_values[i].valuename,
153 REG_SZ,
154 (char*)data.buffer,
155 data.uni_str_len*sizeof(uint16) );
156 break;
158 default:
159 DEBUG(0,("init_registry_data: invalid value type in builtin_registry_values [%d]\n",
160 builtin_registry_values[i].type));
162 regdb_store_values( builtin_registry_values[i].path, &values );
164 regval_ctr_destroy( &values );
167 return True;
170 /***********************************************************************
171 Open the registry database
172 ***********************************************************************/
174 BOOL init_registry_db( void )
176 const char *vstring = "INFO/version";
177 uint32 vers_id;
179 if ( tdb_reg )
180 return True;
182 /* placeholder tdb; reinit upon startup */
184 if ( !(tdb_reg = tdb_open_log(lock_path("registry.tdb"), 0, TDB_DEFAULT, O_RDWR, 0600)) )
186 tdb_reg = tdb_open_log(lock_path("registry.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
187 if ( !tdb_reg ) {
188 DEBUG(0,("init_registry: Failed to open registry %s (%s)\n",
189 lock_path("registry.tdb"), strerror(errno) ));
190 return False;
193 DEBUG(10,("init_registry: Successfully created registry tdb\n"));
197 vers_id = tdb_fetch_int32(tdb_reg, vstring);
199 if ( vers_id != REGVER_V1 ) {
201 /* create the registry here */
203 if ( !init_registry_data() ) {
204 DEBUG(0,("init_registry: Failed to initiailize data in registry!\n"));
205 return False;
209 return True;
212 /***********************************************************************
213 Add subkey strings to the registry tdb under a defined key
214 fmt is the same format as tdb_pack except this function only supports
215 fstrings
216 ***********************************************************************/
218 static BOOL regdb_store_keys_internal( const char *key, REGSUBKEY_CTR *ctr )
220 TDB_DATA kbuf, dbuf;
221 char *buffer, *tmpbuf;
222 int i = 0;
223 uint32 len, buflen;
224 BOOL ret = True;
225 uint32 num_subkeys = regsubkey_ctr_numkeys( ctr );
226 pstring keyname;
228 if ( !key )
229 return False;
231 pstrcpy( keyname, key );
232 normalize_reg_path( keyname );
234 /* allocate some initial memory */
236 buffer = SMB_MALLOC(sizeof(pstring));
237 buflen = sizeof(pstring);
238 len = 0;
240 /* store the number of subkeys */
242 len += tdb_pack(buffer+len, buflen-len, "d", num_subkeys );
244 /* pack all the strings */
246 for (i=0; i<num_subkeys; i++) {
247 len += tdb_pack( buffer+len, buflen-len, "f", regsubkey_ctr_specific_key(ctr, i) );
248 if ( len > buflen ) {
249 /* allocate some extra space */
250 if ((tmpbuf = SMB_REALLOC( buffer, len*2 )) == NULL) {
251 DEBUG(0,("regdb_store_keys: Failed to realloc memory of size [%d]\n", len*2));
252 ret = False;
253 goto done;
255 buffer = tmpbuf;
256 buflen = len*2;
258 len = tdb_pack( buffer+len, buflen-len, "f", regsubkey_ctr_specific_key(ctr, i) );
262 /* finally write out the data */
264 kbuf.dptr = keyname;
265 kbuf.dsize = strlen(keyname)+1;
266 dbuf.dptr = buffer;
267 dbuf.dsize = len;
268 if ( tdb_store( tdb_reg, kbuf, dbuf, TDB_REPLACE ) == -1) {
269 ret = False;
270 goto done;
273 done:
274 SAFE_FREE( buffer );
276 return ret;
279 /***********************************************************************
280 Store the new subkey record and create any child key records that
281 do not currently exist
282 ***********************************************************************/
284 BOOL regdb_store_keys( const char *key, REGSUBKEY_CTR *ctr )
286 int num_subkeys, i;
287 pstring path;
288 REGSUBKEY_CTR subkeys, old_subkeys;
289 char *oldkeyname;
291 /* fetch a list of the old subkeys so we can determine if any were deleted */
293 regsubkey_ctr_init( &old_subkeys );
294 regdb_fetch_keys( key, &old_subkeys );
296 /* store the subkey list for the parent */
298 if ( !regdb_store_keys_internal( key, ctr ) ) {
299 DEBUG(0,("regdb_store_keys: Failed to store new subkey list for parent [%s}\n", key ));
300 return False;
303 /* now delete removed keys */
305 num_subkeys = regsubkey_ctr_numkeys( &old_subkeys );
306 for ( i=0; i<num_subkeys; i++ ) {
307 oldkeyname = regsubkey_ctr_specific_key( &old_subkeys, i );
308 if ( !regsubkey_ctr_key_exists( ctr, oldkeyname ) ) {
309 pstr_sprintf( path, "%s%c%s", key, '/', oldkeyname );
310 normalize_reg_path( path );
311 tdb_delete_bystring( tdb_reg, path );
315 regsubkey_ctr_destroy( &old_subkeys );
317 /* now create records for any subkeys that don't already exist */
319 num_subkeys = regsubkey_ctr_numkeys( ctr );
320 for ( i=0; i<num_subkeys; i++ ) {
321 pstr_sprintf( path, "%s%c%s", key, '/', regsubkey_ctr_specific_key( ctr, i ) );
322 regsubkey_ctr_init( &subkeys );
323 if ( regdb_fetch_keys( path, &subkeys ) == -1 ) {
324 /* create a record with 0 subkeys */
325 if ( !regdb_store_keys_internal( path, &subkeys ) ) {
326 DEBUG(0,("regdb_store_keys: Failed to store new record for key [%s}\n", path ));
327 regsubkey_ctr_destroy( &subkeys );
328 return False;
331 regsubkey_ctr_destroy( &subkeys );
334 return True;
338 /***********************************************************************
339 Retrieve an array of strings containing subkeys. Memory should be
340 released by the caller.
341 ***********************************************************************/
343 int regdb_fetch_keys( const char* key, REGSUBKEY_CTR *ctr )
345 pstring path;
346 uint32 num_items;
347 TDB_DATA dbuf;
348 char *buf;
349 uint32 buflen, len;
350 int i;
351 fstring subkeyname;
353 DEBUG(10,("regdb_fetch_keys: Enter key => [%s]\n", key ? key : "NULL"));
355 pstrcpy( path, key );
357 /* convert to key format */
358 pstring_sub( path, "\\", "/" );
359 strupper_m( path );
361 dbuf = tdb_fetch_bystring( tdb_reg, path );
363 buf = dbuf.dptr;
364 buflen = dbuf.dsize;
366 if ( !buf ) {
367 DEBUG(5,("regdb_fetch_keys: tdb lookup failed to locate key [%s]\n", key));
368 return -1;
371 len = tdb_unpack( buf, buflen, "d", &num_items);
373 for (i=0; i<num_items; i++) {
374 len += tdb_unpack( buf+len, buflen-len, "f", subkeyname );
375 regsubkey_ctr_addkey( ctr, subkeyname );
378 SAFE_FREE( dbuf.dptr );
380 DEBUG(10,("regdb_fetch_keys: Exit [%d] items\n", num_items));
382 return num_items;
385 /****************************************************************************
386 Unpack a list of registry values frem the TDB
387 ***************************************************************************/
389 static int regdb_unpack_values(REGVAL_CTR *values, char *buf, int buflen)
391 int len = 0;
392 uint32 type;
393 pstring valuename;
394 uint32 size;
395 uint8 *data_p;
396 uint32 num_values = 0;
397 int i;
401 /* loop and unpack the rest of the registry values */
403 len += tdb_unpack(buf+len, buflen-len, "d", &num_values);
405 for ( i=0; i<num_values; i++ ) {
406 /* unpack the next regval */
408 type = REG_NONE;
409 size = 0;
410 data_p = NULL;
411 len += tdb_unpack(buf+len, buflen-len, "fdB",
412 valuename,
413 &type,
414 &size,
415 &data_p);
417 /* add the new value. Paranoid protective code -- make sure data_p is valid */
419 if ( size && data_p ) {
420 regval_ctr_addvalue( values, valuename, type, (const char *)data_p, size );
421 SAFE_FREE(data_p); /* 'B' option to tdb_unpack does a malloc() */
424 DEBUG(8,("specific: [%s], len: %d\n", valuename, size));
427 return len;
430 /****************************************************************************
431 Pack all values in all printer keys
432 ***************************************************************************/
434 static int regdb_pack_values(REGVAL_CTR *values, char *buf, int buflen)
436 int len = 0;
437 int i;
438 REGISTRY_VALUE *val;
439 int num_values = regval_ctr_numvals( values );
441 if ( !values )
442 return 0;
444 /* pack the number of values first */
446 len += tdb_pack( buf+len, buflen-len, "d", num_values );
448 /* loop over all values */
450 for ( i=0; i<num_values; i++ ) {
451 val = regval_ctr_specific_value( values, i );
452 len += tdb_pack(buf+len, buflen-len, "fdB",
453 regval_name(val),
454 regval_type(val),
455 regval_size(val),
456 regval_data_p(val) );
459 return len;
462 /***********************************************************************
463 Retrieve an array of strings containing subkeys. Memory should be
464 released by the caller.
465 ***********************************************************************/
467 int regdb_fetch_values( const char* key, REGVAL_CTR *values )
469 TDB_DATA data;
470 pstring keystr;
471 int len;
473 DEBUG(10,("regdb_fetch_values: Looking for value of key [%s] \n", key));
475 pstr_sprintf( keystr, "%s/%s", VALUE_PREFIX, key );
476 normalize_reg_path( keystr );
478 data = tdb_fetch_bystring( tdb_reg, keystr );
480 if ( !data.dptr ) {
481 /* all keys have zero values by default */
482 return 0;
485 len = regdb_unpack_values( values, data.dptr, data.dsize );
487 SAFE_FREE( data.dptr );
489 return regval_ctr_numvals(values);
492 /***********************************************************************
493 Stub function since we do not currently support storing registry
494 values in the registry.tdb
495 ***********************************************************************/
497 BOOL regdb_store_values( const char *key, REGVAL_CTR *values )
499 TDB_DATA data;
500 pstring keystr;
501 int len, ret;
503 DEBUG(10,("regdb_store_values: Looking for value of key [%s] \n", key));
505 ZERO_STRUCT( data );
507 len = regdb_pack_values( values, data.dptr, data.dsize );
508 if ( len <= 0 ) {
509 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
510 return False;
513 data.dptr = SMB_MALLOC_ARRAY( char, len );
514 data.dsize = len;
516 len = regdb_pack_values( values, data.dptr, data.dsize );
518 SMB_ASSERT( len == data.dsize );
520 pstr_sprintf( keystr, "%s/%s", VALUE_PREFIX, key );
521 normalize_reg_path( keystr );
523 ret = tdb_store_bystring(tdb_reg, keystr, data, TDB_REPLACE);
525 SAFE_FREE( data.dptr );
527 return ret != -1 ;
532 * Table of function pointers for default access
535 REGISTRY_OPS regdb_ops = {
536 regdb_fetch_keys,
537 regdb_fetch_values,
538 regdb_store_keys,
539 regdb_store_values,
540 NULL