r9895: fix typo in registry path
[tprouty/samba.git] / source / 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         uint32 *ctx;
96         int i;
97         const char *p, *p2;
98         UNISTR2 data;
99         
100         /* create a new top level talloc ctx */
101
102         if ( !(ctx = TALLOC_P( NULL, uint32 )) ) {
103                 DEBUG(0,("init_registry_data: top level talloc() failure!\n"));
104                 return False;
105         }
106         
107         /* loop over all of the predefined paths and add each component */
108         
109         for ( i=0; builtin_registry_paths[i] != NULL; i++ ) {
110
111                 DEBUG(6,("init_registry_data: Adding [%s]\n", builtin_registry_paths[i]));
112
113                 pstrcpy( path, builtin_registry_paths[i] );
114                 pstrcpy( base, "" );
115                 p = path;
116                 
117                 while ( next_token(&p, keyname, "\\", sizeof(keyname)) ) {
118                 
119                         /* build up the registry path from the components */
120                         
121                         if ( *base )
122                                 pstrcat( base, "\\" );
123                         pstrcat( base, keyname );
124                         
125                         /* get the immediate subkeyname (if we have one ) */
126                         
127                         *subkeyname = '\0';
128                         if ( *p ) {
129                                 pstrcpy( remaining, p );
130                                 p2 = remaining;
131                                 
132                                 if ( !next_token(&p2, subkeyname, "\\", sizeof(subkeyname)) )
133                                         fstrcpy( subkeyname, p2 );
134                         }
135
136                         DEBUG(10,("init_registry_data: Storing key [%s] with subkey [%s]\n",
137                                 base, *subkeyname ? subkeyname : "NULL"));
138                         
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 */
142                         
143                         if ( !(subkeys = TALLOC_ZERO_P( ctx, REGSUBKEY_CTR )) ) {
144                                 DEBUG(0,("talloc() failure!\n"));
145                                 return False;
146                         }
147
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;
153                         
154                         TALLOC_FREE( subkeys );
155                 }
156         }
157
158         /* loop over all of the predefined values and add each component */
159         
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;
164                 }
165
166                 regdb_fetch_values( builtin_registry_values[i].path, values );
167
168                 /* preserve existing values across restarts.  Only add new ones */
169
170                 if ( !regval_ctr_key_exists( values, builtin_registry_values[i].valuename ) ) 
171                 {
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;
180                                 
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;
189                         
190                         default:
191                                 DEBUG(0,("init_registry_data: invalid value type in builtin_registry_values [%d]\n",
192                                         builtin_registry_values[i].type));
193                         }
194                         regdb_store_values( builtin_registry_values[i].path, values );
195                 }
196                 
197                 TALLOC_FREE( values );
198         }
199         
200         return True;
201 }
202
203 /***********************************************************************
204  Open the registry database
205  ***********************************************************************/
206  
207 BOOL init_registry_db( void )
208 {
209         const char *vstring = "INFO/version";
210         uint32 vers_id;
211
212         if ( tdb_reg )
213                 return True;
214
215         if ( !(tdb_reg = tdb_open_log(lock_path("registry.tdb"), 0, TDB_DEFAULT, O_RDWR, 0600)) )
216         {
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;
222                 }
223                 
224                 DEBUG(10,("init_registry: Successfully created registry tdb\n"));
225         }
226                 
227
228         vers_id = tdb_fetch_int32(tdb_reg, vstring);
229
230         if ( vers_id != REGVER_V1 ) {
231                 /* any upgrade code here if needed */
232         }
233
234         /* always setup the necessary keys and values */
235
236         if ( !init_registry_data() ) {
237                 DEBUG(0,("init_registry: Failed to initiailize data in registry!\n"));
238                 return False;
239         }
240
241         return True;
242 }
243
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  ***********************************************************************/
249  
250 static BOOL regdb_store_keys_internal( const char *key, REGSUBKEY_CTR *ctr )
251 {
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;
259         
260         if ( !key )
261                 return False;
262
263         pstrcpy( keyname, key );
264         normalize_reg_path( keyname );
265
266         /* allocate some initial memory */
267                 
268         buffer = SMB_MALLOC(sizeof(pstring));
269         buflen = sizeof(pstring);
270         len = 0;
271         
272         /* store the number of subkeys */
273         
274         len += tdb_pack(buffer+len, buflen-len, "d", num_subkeys );
275         
276         /* pack all the strings */
277         
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;
286                         }
287                         buffer = tmpbuf;
288                         buflen = len*2;
289                                         
290                         len = tdb_pack( buffer+len, buflen-len, "f", regsubkey_ctr_specific_key(ctr, i) );
291                 }               
292         }
293         
294         /* finally write out the data */
295         
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;
303         }
304
305 done:           
306         SAFE_FREE( buffer );
307         
308         return ret;
309 }
310
311 /***********************************************************************
312  Store the new subkey record and create any child key records that 
313  do not currently exist
314  ***********************************************************************/
315
316 BOOL regdb_store_keys( const char *key, REGSUBKEY_CTR *ctr )
317 {
318         int num_subkeys, i;
319         pstring path;
320         REGSUBKEY_CTR *subkeys, *old_subkeys;
321         char *oldkeyname;
322         
323         /* fetch a list of the old subkeys so we can determine if any were deleted */
324         
325         if ( !(old_subkeys = TALLOC_ZERO_P( ctr, REGSUBKEY_CTR )) ) {
326                 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
327                 return False;
328         }
329
330         regdb_fetch_keys( key, old_subkeys );
331         
332         /* store the subkey list for the parent */
333         
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;
337         }
338         
339         /* now delete removed keys */
340         
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 );
348                 }
349         }
350
351         TALLOC_FREE( old_subkeys );
352         
353         /* now create records for any subkeys that don't already exist */
354         
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 ) );
358
359                 if ( !(subkeys = TALLOC_ZERO_P( ctr, REGSUBKEY_CTR )) ) {
360                         DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
361                         return False;
362                 }
363
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;
370                         }
371                 }
372
373                 TALLOC_FREE( subkeys );
374         }
375         
376         return True;
377 }
378
379
380 /***********************************************************************
381  Retrieve an array of strings containing subkeys.  Memory should be 
382  released by the caller.  
383  ***********************************************************************/
384
385 int regdb_fetch_keys( const char* key, REGSUBKEY_CTR *ctr )
386 {
387         pstring path;
388         uint32 num_items;
389         TDB_DATA dbuf;
390         char *buf;
391         uint32 buflen, len;
392         int i;
393         fstring subkeyname;
394
395         DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key ? key : "NULL"));
396         
397         pstrcpy( path, key );
398         
399         /* convert to key format */
400         pstring_sub( path, "\\", "/" ); 
401         strupper_m( path );
402         
403         dbuf = tdb_fetch_bystring( tdb_reg, path );
404         
405         buf = dbuf.dptr;
406         buflen = dbuf.dsize;
407         
408         if ( !buf ) {
409                 DEBUG(5,("regdb_fetch_keys: tdb lookup failed to locate key [%s]\n", key));
410                 return -1;
411         }
412         
413         len = tdb_unpack( buf, buflen, "d", &num_items);
414         
415         for (i=0; i<num_items; i++) {
416                 len += tdb_unpack( buf+len, buflen-len, "f", subkeyname );
417                 regsubkey_ctr_addkey( ctr, subkeyname );
418         }
419
420         SAFE_FREE( dbuf.dptr );
421         
422         DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items));
423         
424         return num_items;
425 }
426
427 /****************************************************************************
428  Unpack a list of registry values frem the TDB
429  ***************************************************************************/
430  
431 static int regdb_unpack_values(REGVAL_CTR *values, char *buf, int buflen)
432 {
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;
440         
441         
442         
443         /* loop and unpack the rest of the registry values */
444         
445         len += tdb_unpack(buf+len, buflen-len, "d", &num_values);
446         
447         for ( i=0; i<num_values; i++ ) {
448                 /* unpack the next regval */
449                 
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);
458                                 
459                 /* add the new value. Paranoid protective code -- make sure data_p is valid */
460
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() */
464                 }
465
466                 DEBUG(8,("specific: [%s], len: %d\n", valuename, size));
467         }
468
469         return len;
470 }
471
472 /****************************************************************************
473  Pack all values in all printer keys
474  ***************************************************************************/
475  
476 static int regdb_pack_values(REGVAL_CTR *values, char *buf, int buflen)
477 {
478         int             len = 0;
479         int             i;
480         REGISTRY_VALUE  *val;
481         int             num_values = regval_ctr_numvals( values );
482
483         if ( !values )
484                 return 0;
485
486         /* pack the number of values first */
487         
488         len += tdb_pack( buf+len, buflen-len, "d", num_values );
489         
490         /* loop over all values */
491                 
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) );
499         }
500
501         return len;
502 }
503
504 /***********************************************************************
505  Retrieve an array of strings containing subkeys.  Memory should be 
506  released by the caller.
507  ***********************************************************************/
508
509 int regdb_fetch_values( const char* key, REGVAL_CTR *values )
510 {
511         TDB_DATA data;
512         pstring keystr;
513
514         DEBUG(10,("regdb_fetch_values: Looking for value of key [%s] \n", key));
515         
516         pstr_sprintf( keystr, "%s/%s", VALUE_PREFIX, key );
517         normalize_reg_path( keystr );
518         
519         data = tdb_fetch_bystring( tdb_reg, keystr );
520         
521         if ( !data.dptr ) {
522                 /* all keys have zero values by default */
523                 return 0;
524         }
525         
526         regdb_unpack_values( values, data.dptr, data.dsize );
527         
528         SAFE_FREE( data.dptr );
529         
530         return regval_ctr_numvals(values);
531 }
532
533 /***********************************************************************
534  Stub function since we do not currently support storing registry 
535  values in the registry.tdb
536  ***********************************************************************/
537
538 BOOL regdb_store_values( const char *key, REGVAL_CTR *values )
539 {
540         TDB_DATA data;
541         pstring keystr;
542         int len, ret;
543         
544         DEBUG(10,("regdb_store_values: Looking for value of key [%s] \n", key));
545         
546         ZERO_STRUCT( data );
547         
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;
552         }
553         
554         data.dptr = SMB_MALLOC_ARRAY( char, len );
555         data.dsize = len;
556         
557         len = regdb_pack_values( values, data.dptr, data.dsize );
558         
559         SMB_ASSERT( len == data.dsize );
560         
561         pstr_sprintf( keystr, "%s/%s", VALUE_PREFIX, key );
562         normalize_reg_path( keystr );
563         
564         ret = tdb_store_bystring(tdb_reg, keystr, data, TDB_REPLACE);
565         
566         SAFE_FREE( data.dptr );
567         
568         return ret != -1 ;
569 }
570
571
572 /* 
573  * Table of function pointers for default access
574  */
575  
576 REGISTRY_OPS regdb_ops = {
577         regdb_fetch_keys,
578         regdb_fetch_values,
579         regdb_store_keys,
580         regdb_store_values,
581         NULL
582 };
583
584