r10656: BIG merge from trunk. Features not copied over
[abartlet/samba.git/.git] / source3 / registry / reg_db.c
1 /* 
2  *  Unix SMB/CIFS implementation.
3  *  Virtual Windows Registry Layer
4  *  Copyright (C) Gerald Carter                     2002-2005
5  *
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.
10  *  
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.
15  *  
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.
19  */
20
21 /* Implementation of internal registry database functions. */
22
23 #include "includes.h"
24
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_RPC_SRV
27
28 static TDB_CONTEXT *tdb_reg;
29
30 #define VALUE_PREFIX    "SAMBA_REGVAL"
31
32 /* List the deepest path into the registry.  All part components will be created.*/
33
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 */
41
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 };
60
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;
69 };
70
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 } }
81 };
82
83 #define REGVER_V1       1       /* first db version with write support */
84         
85 /***********************************************************************
86  Open the registry data in the tdb
87  ***********************************************************************/
88  
89 static BOOL init_registry_data( void )
90 {
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;
98         
99         /* loop over all of the predefined paths and add each component */
100         
101         for ( i=0; builtin_registry_paths[i] != NULL; i++ ) {
102
103                 DEBUG(6,("init_registry_data: Adding [%s]\n", builtin_registry_paths[i]));
104
105                 pstrcpy( path, builtin_registry_paths[i] );
106                 pstrcpy( base, "" );
107                 p = path;
108                 
109                 while ( next_token(&p, keyname, "\\", sizeof(keyname)) ) {
110                 
111                         /* build up the registry path from the components */
112                         
113                         if ( *base )
114                                 pstrcat( base, "\\" );
115                         pstrcat( base, keyname );
116                         
117                         /* get the immediate subkeyname (if we have one ) */
118                         
119                         *subkeyname = '\0';
120                         if ( *p ) {
121                                 pstrcpy( remaining, p );
122                                 p2 = remaining;
123                                 
124                                 if ( !next_token(&p2, subkeyname, "\\", sizeof(subkeyname)) )
125                                         fstrcpy( subkeyname, p2 );
126                         }
127
128                         DEBUG(10,("init_registry_data: Storing key [%s] with subkey [%s]\n",
129                                 base, *subkeyname ? subkeyname : "NULL"));
130                         
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 */
134                         
135                         if ( !(subkeys = TALLOC_ZERO_P( NULL, REGSUBKEY_CTR )) ) {
136                                 DEBUG(0,("talloc() failure!\n"));
137                                 return False;
138                         }
139
140                         regdb_fetch_keys( base, subkeys );
141                         if ( *subkeyname ) 
142                                 regsubkey_ctr_addkey( subkeys, subkeyname );
143                         if ( !regdb_store_keys( base, subkeys ))
144                                 return False;
145                         
146                         TALLOC_FREE( subkeys );
147                 }
148         }
149
150         /* loop over all of the predefined values and add each component */
151         
152         for ( i=0; builtin_registry_values[i].path != NULL; i++ ) {
153                 if ( !(values = TALLOC_ZERO_P( NULL, REGVAL_CTR )) ) {
154                         DEBUG(0,("talloc() failure!\n"));
155                         return False;
156                 }
157
158                 regdb_fetch_values( builtin_registry_values[i].path, values );
159
160                 /* preserve existing values across restarts.  Only add new ones */
161
162                 if ( !regval_ctr_key_exists( values, builtin_registry_values[i].valuename ) ) 
163                 {
164                         switch( builtin_registry_values[i].type ) {
165                         case REG_DWORD:
166                                 regval_ctr_addvalue( values, 
167                                                      builtin_registry_values[i].valuename,
168                                                      REG_DWORD,
169                                                      (char*)&builtin_registry_values[i].data.dw_value,
170                                                      sizeof(uint32) );
171                                 break;
172                                 
173                         case REG_SZ:
174                                 init_unistr2( &data, builtin_registry_values[i].data.string, UNI_STR_TERMINATE);
175                                 regval_ctr_addvalue( values, 
176                                                      builtin_registry_values[i].valuename,
177                                                      REG_SZ,
178                                                      (char*)data.buffer,
179                                                      data.uni_str_len*sizeof(uint16) );
180                                 break;
181                         
182                         default:
183                                 DEBUG(0,("init_registry_data: invalid value type in builtin_registry_values [%d]\n",
184                                         builtin_registry_values[i].type));
185                         }
186                         regdb_store_values( builtin_registry_values[i].path, values );
187                 }
188                 
189                 TALLOC_FREE( values );
190         }
191         
192         return True;
193 }
194
195 /***********************************************************************
196  Open the registry database
197  ***********************************************************************/
198  
199 BOOL init_registry_db( void )
200 {
201         const char *vstring = "INFO/version";
202         uint32 vers_id;
203
204         if ( tdb_reg )
205                 return True;
206
207         if ( !(tdb_reg = tdb_open_log(lock_path("registry.tdb"), 0, TDB_DEFAULT, O_RDWR, 0600)) )
208         {
209                 tdb_reg = tdb_open_log(lock_path("registry.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
210                 if ( !tdb_reg ) {
211                         DEBUG(0,("init_registry: Failed to open registry %s (%s)\n",
212                                 lock_path("registry.tdb"), strerror(errno) ));
213                         return False;
214                 }
215                 
216                 DEBUG(10,("init_registry: Successfully created registry tdb\n"));
217         }
218                 
219
220         vers_id = tdb_fetch_int32(tdb_reg, vstring);
221
222         if ( vers_id != REGVER_V1 ) {
223                 /* any upgrade code here if needed */
224         }
225
226         /* always setup the necessary keys and values */
227
228         if ( !init_registry_data() ) {
229                 DEBUG(0,("init_registry: Failed to initiailize data in registry!\n"));
230                 return False;
231         }
232
233         return True;
234 }
235
236 /***********************************************************************
237  Add subkey strings to the registry tdb under a defined key
238  fmt is the same format as tdb_pack except this function only supports
239  fstrings
240  ***********************************************************************/
241  
242 static BOOL regdb_store_keys_internal( const char *key, REGSUBKEY_CTR *ctr )
243 {
244         TDB_DATA kbuf, dbuf;
245         char *buffer, *tmpbuf;
246         int i = 0;
247         uint32 len, buflen;
248         BOOL ret = True;
249         uint32 num_subkeys = regsubkey_ctr_numkeys( ctr );
250         pstring keyname;
251         
252         if ( !key )
253                 return False;
254
255         pstrcpy( keyname, key );
256         normalize_reg_path( keyname );
257
258         /* allocate some initial memory */
259                 
260         buffer = SMB_MALLOC(sizeof(pstring));
261         buflen = sizeof(pstring);
262         len = 0;
263         
264         /* store the number of subkeys */
265         
266         len += tdb_pack(buffer+len, buflen-len, "d", num_subkeys );
267         
268         /* pack all the strings */
269         
270         for (i=0; i<num_subkeys; i++) {
271                 len += tdb_pack( buffer+len, buflen-len, "f", regsubkey_ctr_specific_key(ctr, i) );
272                 if ( len > buflen ) {
273                         /* allocate some extra space */
274                         if ((tmpbuf = SMB_REALLOC( buffer, len*2 )) == NULL) {
275                                 DEBUG(0,("regdb_store_keys: Failed to realloc memory of size [%d]\n", len*2));
276                                 ret = False;
277                                 goto done;
278                         }
279                         buffer = tmpbuf;
280                         buflen = len*2;
281                                         
282                         len = tdb_pack( buffer+len, buflen-len, "f", regsubkey_ctr_specific_key(ctr, i) );
283                 }               
284         }
285         
286         /* finally write out the data */
287         
288         kbuf.dptr = keyname;
289         kbuf.dsize = strlen(keyname)+1;
290         dbuf.dptr = buffer;
291         dbuf.dsize = len;
292         if ( tdb_store( tdb_reg, kbuf, dbuf, TDB_REPLACE ) == -1) {
293                 ret = False;
294                 goto done;
295         }
296
297 done:           
298         SAFE_FREE( buffer );
299         
300         return ret;
301 }
302
303 /***********************************************************************
304  Store the new subkey record and create any child key records that 
305  do not currently exist
306  ***********************************************************************/
307
308 BOOL regdb_store_keys( const char *key, REGSUBKEY_CTR *ctr )
309 {
310         int num_subkeys, i;
311         pstring path;
312         REGSUBKEY_CTR *subkeys, *old_subkeys;
313         char *oldkeyname;
314         
315         /* fetch a list of the old subkeys so we can determine if any were deleted */
316         
317         if ( !(old_subkeys = TALLOC_ZERO_P( ctr, REGSUBKEY_CTR )) ) {
318                 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
319                 return False;
320         }
321
322         regdb_fetch_keys( key, old_subkeys );
323         
324         /* store the subkey list for the parent */
325         
326         if ( !regdb_store_keys_internal( key, ctr ) ) {
327                 DEBUG(0,("regdb_store_keys: Failed to store new subkey list for parent [%s}\n", key ));
328                 return False;
329         }
330         
331         /* now delete removed keys */
332         
333         num_subkeys = regsubkey_ctr_numkeys( old_subkeys );
334         for ( i=0; i<num_subkeys; i++ ) {
335                 oldkeyname = regsubkey_ctr_specific_key( old_subkeys, i );
336                 if ( !regsubkey_ctr_key_exists( ctr, oldkeyname ) ) {
337                         pstr_sprintf( path, "%s%c%s", key, '/', oldkeyname );
338                         normalize_reg_path( path );
339                         tdb_delete_bystring( tdb_reg, path );
340                 }
341         }
342
343         TALLOC_FREE( old_subkeys );
344         
345         /* now create records for any subkeys that don't already exist */
346         
347         num_subkeys = regsubkey_ctr_numkeys( ctr );
348         for ( i=0; i<num_subkeys; i++ ) {
349                 pstr_sprintf( path, "%s%c%s", key, '/', regsubkey_ctr_specific_key( ctr, i ) );
350
351                 if ( !(subkeys = TALLOC_ZERO_P( ctr, REGSUBKEY_CTR )) ) {
352                         DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
353                         return False;
354                 }
355
356                 if ( regdb_fetch_keys( path, subkeys ) == -1 ) {
357                         /* create a record with 0 subkeys */
358                         if ( !regdb_store_keys_internal( path, subkeys ) ) {
359                                 DEBUG(0,("regdb_store_keys: Failed to store new record for key [%s}\n", path ));
360                                 TALLOC_FREE( subkeys );
361                                 return False;
362                         }
363                 }
364
365                 TALLOC_FREE( subkeys );
366         }
367         
368         return True;
369 }
370
371
372 /***********************************************************************
373  Retrieve an array of strings containing subkeys.  Memory should be 
374  released by the caller.  
375  ***********************************************************************/
376
377 int regdb_fetch_keys( const char* key, REGSUBKEY_CTR *ctr )
378 {
379         pstring path;
380         uint32 num_items;
381         TDB_DATA dbuf;
382         char *buf;
383         uint32 buflen, len;
384         int i;
385         fstring subkeyname;
386
387         DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key ? key : "NULL"));
388         
389         pstrcpy( path, key );
390         
391         /* convert to key format */
392         pstring_sub( path, "\\", "/" ); 
393         strupper_m( path );
394         
395         dbuf = tdb_fetch_bystring( tdb_reg, path );
396         
397         buf = dbuf.dptr;
398         buflen = dbuf.dsize;
399         
400         if ( !buf ) {
401                 DEBUG(5,("regdb_fetch_keys: tdb lookup failed to locate key [%s]\n", key));
402                 return -1;
403         }
404         
405         len = tdb_unpack( buf, buflen, "d", &num_items);
406         
407         for (i=0; i<num_items; i++) {
408                 len += tdb_unpack( buf+len, buflen-len, "f", subkeyname );
409                 regsubkey_ctr_addkey( ctr, subkeyname );
410         }
411
412         SAFE_FREE( dbuf.dptr );
413         
414         DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items));
415         
416         return num_items;
417 }
418
419 /****************************************************************************
420  Unpack a list of registry values frem the TDB
421  ***************************************************************************/
422  
423 static int regdb_unpack_values(REGVAL_CTR *values, char *buf, int buflen)
424 {
425         int             len = 0;
426         uint32          type;
427         pstring         valuename;
428         uint32          size;
429         uint8           *data_p;
430         uint32          num_values = 0;
431         int             i;
432         
433         
434         
435         /* loop and unpack the rest of the registry values */
436         
437         len += tdb_unpack(buf+len, buflen-len, "d", &num_values);
438         
439         for ( i=0; i<num_values; i++ ) {
440                 /* unpack the next regval */
441                 
442                 type = REG_NONE;
443                 size = 0;
444                 data_p = NULL;
445                 len += tdb_unpack(buf+len, buflen-len, "fdB",
446                                   valuename,
447                                   &type,
448                                   &size,
449                                   &data_p);
450                                 
451                 /* add the new value. Paranoid protective code -- make sure data_p is valid */
452
453                 if ( size && data_p ) {
454                         regval_ctr_addvalue( values, valuename, type, (const char *)data_p, size );
455                         SAFE_FREE(data_p); /* 'B' option to tdb_unpack does a malloc() */
456                 }
457
458                 DEBUG(8,("specific: [%s], len: %d\n", valuename, size));
459         }
460
461         return len;
462 }
463
464 /****************************************************************************
465  Pack all values in all printer keys
466  ***************************************************************************/
467  
468 static int regdb_pack_values(REGVAL_CTR *values, char *buf, int buflen)
469 {
470         int             len = 0;
471         int             i;
472         REGISTRY_VALUE  *val;
473         int             num_values = regval_ctr_numvals( values );
474
475         if ( !values )
476                 return 0;
477
478         /* pack the number of values first */
479         
480         len += tdb_pack( buf+len, buflen-len, "d", num_values );
481         
482         /* loop over all values */
483                 
484         for ( i=0; i<num_values; i++ ) {                        
485                 val = regval_ctr_specific_value( values, i );
486                 len += tdb_pack(buf+len, buflen-len, "fdB",
487                                 regval_name(val),
488                                 regval_type(val),
489                                 regval_size(val),
490                                 regval_data_p(val) );
491         }
492
493         return len;
494 }
495
496 /***********************************************************************
497  Retrieve an array of strings containing subkeys.  Memory should be 
498  released by the caller.
499  ***********************************************************************/
500
501 int regdb_fetch_values( const char* key, REGVAL_CTR *values )
502 {
503         TDB_DATA data;
504         pstring keystr;
505
506         DEBUG(10,("regdb_fetch_values: Looking for value of key [%s] \n", key));
507         
508         pstr_sprintf( keystr, "%s/%s", VALUE_PREFIX, key );
509         normalize_reg_path( keystr );
510         
511         data = tdb_fetch_bystring( tdb_reg, keystr );
512         
513         if ( !data.dptr ) {
514                 /* all keys have zero values by default */
515                 return 0;
516         }
517         
518         regdb_unpack_values( values, data.dptr, data.dsize );
519         
520         SAFE_FREE( data.dptr );
521         
522         return regval_ctr_numvals(values);
523 }
524
525 /***********************************************************************
526  Stub function since we do not currently support storing registry 
527  values in the registry.tdb
528  ***********************************************************************/
529
530 BOOL regdb_store_values( const char *key, REGVAL_CTR *values )
531 {
532         TDB_DATA data;
533         pstring keystr;
534         int len, ret;
535         
536         DEBUG(10,("regdb_store_values: Looking for value of key [%s] \n", key));
537         
538         ZERO_STRUCT( data );
539         
540         len = regdb_pack_values( values, data.dptr, data.dsize );
541         if ( len <= 0 ) {
542                 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
543                 return False;
544         }
545         
546         data.dptr = SMB_MALLOC_ARRAY( char, len );
547         data.dsize = len;
548         
549         len = regdb_pack_values( values, data.dptr, data.dsize );
550         
551         SMB_ASSERT( len == data.dsize );
552         
553         pstr_sprintf( keystr, "%s/%s", VALUE_PREFIX, key );
554         normalize_reg_path( keystr );
555         
556         ret = tdb_store_bystring(tdb_reg, keystr, data, TDB_REPLACE);
557         
558         SAFE_FREE( data.dptr );
559         
560         return ret != -1 ;
561 }
562
563
564 /* 
565  * Table of function pointers for default access
566  */
567  
568 REGISTRY_OPS regdb_ops = {
569         regdb_fetch_keys,
570         regdb_fetch_values,
571         regdb_store_keys,
572         regdb_store_values,
573         NULL
574 };
575
576