2 * Unix SMB/CIFS implementation.
3 * RPC Pipe client / server routines
4 * Copyright (C) Gerald Carter 2002.
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. */
26 #define DBGC_CLASS DBGC_RPC_SRV
28 static TDB_CONTEXT *tdb_reg;
31 /***********************************************************************
32 Open the registry data in the tdb
33 ***********************************************************************/
35 static BOOL init_registry_data( void )
38 REGSUBKEY_CTR subkeys;
40 ZERO_STRUCTP( &subkeys );
42 /* HKEY_LOCAL_MACHINE */
44 regsubkey_ctr_init( &subkeys );
45 pstrcpy( keyname, KEY_HKLM );
46 regsubkey_ctr_addkey( &subkeys, "SYSTEM" );
47 if ( !regdb_store_reg_keys( keyname, &subkeys ))
49 regsubkey_ctr_destroy( &subkeys );
51 regsubkey_ctr_init( &subkeys );
52 pstrcpy( keyname, KEY_HKLM );
53 pstrcat( keyname, "/SYSTEM" );
54 regsubkey_ctr_addkey( &subkeys, "CurrentControlSet" );
55 if ( !regdb_store_reg_keys( keyname, &subkeys ))
57 regsubkey_ctr_destroy( &subkeys );
59 regsubkey_ctr_init( &subkeys );
60 pstrcpy( keyname, KEY_HKLM );
61 pstrcat( keyname, "/SYSTEM/CurrentControlSet" );
62 regsubkey_ctr_addkey( &subkeys, "Control" );
63 regsubkey_ctr_addkey( &subkeys, "Services" );
64 if ( !regdb_store_reg_keys( keyname, &subkeys ))
66 regsubkey_ctr_destroy( &subkeys );
68 regsubkey_ctr_init( &subkeys );
69 pstrcpy( keyname, KEY_HKLM );
70 pstrcat( keyname, "/SYSTEM/CurrentControlSet/Control" );
71 regsubkey_ctr_addkey( &subkeys, "Print" );
72 regsubkey_ctr_addkey( &subkeys, "ProductOptions" );
73 if ( !regdb_store_reg_keys( keyname, &subkeys ))
75 regsubkey_ctr_destroy( &subkeys );
77 pstrcpy( keyname, KEY_HKLM );
78 pstrcat( keyname, "/SYSTEM/CurrentControlSet/Control/ProductOptions" );
79 if ( !regdb_store_reg_keys( keyname, &subkeys ))
82 regsubkey_ctr_init( &subkeys );
83 pstrcpy( keyname, KEY_HKLM );
84 pstrcat( keyname, "/SYSTEM/CurrentControlSet/Services" );
85 regsubkey_ctr_addkey( &subkeys, "Netlogon" );
86 if ( !regdb_store_reg_keys( keyname, &subkeys ))
88 regsubkey_ctr_destroy( &subkeys );
90 regsubkey_ctr_init( &subkeys );
91 pstrcpy( keyname, KEY_HKLM );
92 pstrcat( keyname, "/SYSTEM/CurrentControlSet/Services/Netlogon" );
93 regsubkey_ctr_addkey( &subkeys, "Parameters" );
94 if ( !regdb_store_reg_keys( keyname, &subkeys ))
96 regsubkey_ctr_destroy( &subkeys );
98 pstrcpy( keyname, KEY_HKLM );
99 pstrcat( keyname, "/SYSTEM/CurrentControlSet/Services/Netlogon/Parameters" );
100 if ( !regdb_store_reg_keys( keyname, &subkeys ))
105 pstrcpy( keyname, KEY_HKU );
106 if ( !regdb_store_reg_keys( keyname, &subkeys ) )
109 /* HKEY_CLASSES_ROOT*/
111 pstrcpy( keyname, KEY_HKCR );
112 if ( !regdb_store_reg_keys( keyname, &subkeys ) )
118 /***********************************************************************
119 Open the registry database
120 ***********************************************************************/
122 BOOL init_registry_db( void )
124 static pid_t local_pid;
126 if (tdb_reg && local_pid == sys_getpid())
130 * try to open first without creating so we can determine
131 * if we need to init the data in the registry
134 tdb_reg = tdb_open_log(lock_path("registry.tdb"), 0, TDB_DEFAULT, O_RDWR, 0600);
137 tdb_reg = tdb_open_log(lock_path("registry.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
139 DEBUG(0,("init_registry: Failed to open registry %s (%s)\n",
140 lock_path("registry.tdb"), strerror(errno) ));
144 DEBUG(10,("init_registry: Successfully created registry tdb\n"));
146 /* create the registry here */
147 if ( !init_registry_data() ) {
148 DEBUG(0,("init_registry: Failed to initiailize data in registry!\n"));
153 local_pid = sys_getpid();
160 /***********************************************************************
161 Add subkey strings to the registry tdb under a defined key
162 fmt is the same format as tdb_pack except this function only supports
165 The full path to the registry key is used as database after the
166 \'s are converted to /'s. Key string is also normalized to UPPER
168 ***********************************************************************/
170 BOOL regdb_store_reg_keys( char *keyname, REGSUBKEY_CTR *ctr )
173 char *buffer, *tmpbuf;
177 uint32 num_subkeys = regsubkey_ctr_numkeys( ctr );
182 strupper_m( keyname );
184 /* allocate some initial memory */
186 buffer = SMB_MALLOC(sizeof(pstring));
187 buflen = sizeof(pstring);
190 /* store the number of subkeys */
192 len += tdb_pack(buffer+len, buflen-len, "d", num_subkeys );
194 /* pack all the strings */
196 for (i=0; i<num_subkeys; i++) {
197 len += tdb_pack( buffer+len, buflen-len, "f", regsubkey_ctr_specific_key(ctr, i) );
198 if ( len > buflen ) {
199 /* allocate some extra space */
200 if ((tmpbuf = SMB_REALLOC( buffer, len*2 )) == NULL) {
201 DEBUG(0,("regdb_store_reg_keys: Failed to realloc memory of size [%d]\n", len*2));
208 len = tdb_pack( buffer+len, buflen-len, "f", regsubkey_ctr_specific_key(ctr, i) );
212 /* finally write out the data */
215 kbuf.dsize = strlen(keyname)+1;
218 if ( tdb_store( tdb_reg, kbuf, dbuf, TDB_REPLACE ) == -1) {
229 /***********************************************************************
230 Retrieve an array of strings containing subkeys. Memory should be
231 released by the caller. The subkeys are stored in a catenated string
232 of null terminated character strings
233 ***********************************************************************/
235 int regdb_fetch_reg_keys( char* key, REGSUBKEY_CTR *ctr )
245 DEBUG(10,("regdb_fetch_reg_keys: Enter key => [%s]\n", key ? key : "NULL"));
247 pstrcpy( path, key );
249 /* convert to key format */
250 pstring_sub( path, "\\", "/" );
253 dbuf = tdb_fetch_bystring( tdb_reg, path );
259 DEBUG(5,("regdb_fetch_reg_keys: tdb lookup failed to locate key [%s]\n", key));
263 len = tdb_unpack( buf, buflen, "d", &num_items);
265 for (i=0; i<num_items; i++) {
266 len += tdb_unpack( buf+len, buflen-len, "f", subkeyname );
267 regsubkey_ctr_addkey( ctr, subkeyname );
270 SAFE_FREE( dbuf.dptr );
272 DEBUG(10,("regdb_fetch_reg_keys: Exit [%d] items\n", num_items));
278 /***********************************************************************
279 Retrieve an array of strings containing subkeys. Memory should be
280 released by the caller. The subkeys are stored in a catenated string
281 of null terminated character strings
282 ***********************************************************************/
284 int regdb_fetch_reg_values( char* key, REGVAL_CTR *val )
289 /***********************************************************************
290 Stub function since we do not currently support storing registry
291 values in the registry.tdb
292 ***********************************************************************/
294 BOOL regdb_store_reg_values( char *key, REGVAL_CTR *val )
301 * Table of function pointers for default access
304 REGISTRY_OPS regdb_ops = {
305 regdb_fetch_reg_keys,
306 regdb_fetch_reg_values,
307 regdb_store_reg_keys,
308 regdb_store_reg_values