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