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
);
69 pstrcpy( keyname
, KEY_HKLM
);
70 pstrcat( keyname
, "/SOFTWARE/Microsoft/Windows NT/CurrentVersion/Print" );
71 if ( !regdb_store_reg_keys( keyname
, &subkeys
))
75 regsubkey_ctr_init( &subkeys
);
76 pstrcpy( keyname
, KEY_HKLM
);
77 pstrcat( keyname
, "/SYSTEM/CurrentControlSet/Control" );
78 regsubkey_ctr_addkey( &subkeys
, "Print" );
79 regsubkey_ctr_addkey( &subkeys
, "ProductOptions" );
80 if ( !regdb_store_reg_keys( keyname
, &subkeys
))
82 regsubkey_ctr_destroy( &subkeys
);
84 pstrcpy( keyname
, KEY_HKLM
);
85 pstrcat( keyname
, "/SYSTEM/CurrentControlSet/Control/ProductOptions" );
86 if ( !regdb_store_reg_keys( keyname
, &subkeys
))
89 regsubkey_ctr_init( &subkeys
);
90 pstrcpy( keyname
, KEY_HKLM
);
91 pstrcat( keyname
, "/SYSTEM/CurrentControlSet/Services" );
92 regsubkey_ctr_addkey( &subkeys
, "Netlogon" );
93 if ( !regdb_store_reg_keys( keyname
, &subkeys
))
95 regsubkey_ctr_destroy( &subkeys
);
97 regsubkey_ctr_init( &subkeys
);
98 pstrcpy( keyname
, KEY_HKLM
);
99 pstrcat( keyname
, "/SYSTEM/CurrentControlSet/Services/Netlogon" );
100 regsubkey_ctr_addkey( &subkeys
, "Parameters" );
101 if ( !regdb_store_reg_keys( keyname
, &subkeys
))
103 regsubkey_ctr_destroy( &subkeys
);
105 pstrcpy( keyname
, KEY_HKLM
);
106 pstrcat( keyname
, "/SYSTEM/CurrentControlSet/Services/Netlogon/Parameters" );
107 if ( !regdb_store_reg_keys( keyname
, &subkeys
))
112 pstrcpy( keyname
, KEY_HKU
);
113 if ( !regdb_store_reg_keys( keyname
, &subkeys
) )
116 /* HKEY_CLASSES_ROOT*/
118 pstrcpy( keyname
, KEY_HKCR
);
119 if ( !regdb_store_reg_keys( keyname
, &subkeys
) )
125 /***********************************************************************
126 Open the registry database
127 ***********************************************************************/
129 BOOL
init_registry_db( void )
131 static pid_t local_pid
;
133 if (tdb_reg
&& local_pid
== sys_getpid())
137 * try to open first without creating so we can determine
138 * if we need to init the data in the registry
141 tdb_reg
= tdb_open_log(lock_path("registry.tdb"), 0, TDB_DEFAULT
, O_RDWR
, 0600);
144 tdb_reg
= tdb_open_log(lock_path("registry.tdb"), 0, TDB_DEFAULT
, O_RDWR
|O_CREAT
, 0600);
146 DEBUG(0,("init_registry: Failed to open registry %s (%s)\n",
147 lock_path("registry.tdb"), strerror(errno
) ));
151 DEBUG(10,("init_registry: Successfully created registry tdb\n"));
153 /* create the registry here */
154 if ( !init_registry_data() ) {
155 DEBUG(0,("init_registry: Failed to initiailize data in registry!\n"));
160 local_pid
= sys_getpid();
167 /***********************************************************************
168 Add subkey strings to the registry tdb under a defined key
169 fmt is the same format as tdb_pack except this function only supports
172 The full path to the registry key is used as database after the
173 \'s are converted to /'s. Key string is also normalized to UPPER
175 ***********************************************************************/
177 BOOL
regdb_store_reg_keys( char *keyname
, REGSUBKEY_CTR
*ctr
)
180 char *buffer
, *tmpbuf
;
184 uint32 num_subkeys
= regsubkey_ctr_numkeys( ctr
);
189 strupper_m( keyname
);
191 /* allocate some initial memory */
193 buffer
= SMB_MALLOC(sizeof(pstring
));
194 buflen
= sizeof(pstring
);
197 /* store the number of subkeys */
199 len
+= tdb_pack(buffer
+len
, buflen
-len
, "d", num_subkeys
);
201 /* pack all the strings */
203 for (i
=0; i
<num_subkeys
; i
++) {
204 len
+= tdb_pack( buffer
+len
, buflen
-len
, "f", regsubkey_ctr_specific_key(ctr
, i
) );
205 if ( len
> buflen
) {
206 /* allocate some extra space */
207 if ((tmpbuf
= SMB_REALLOC( buffer
, len
*2 )) == NULL
) {
208 DEBUG(0,("regdb_store_reg_keys: Failed to realloc memory of size [%d]\n", len
*2));
215 len
= tdb_pack( buffer
+len
, buflen
-len
, "f", regsubkey_ctr_specific_key(ctr
, i
) );
219 /* finally write out the data */
222 kbuf
.dsize
= strlen(keyname
)+1;
225 if ( tdb_store( tdb_reg
, kbuf
, dbuf
, TDB_REPLACE
) == -1) {
236 /***********************************************************************
237 Retrieve an array of strings containing subkeys. Memory should be
238 released by the caller. The subkeys are stored in a catenated string
239 of null terminated character strings
240 ***********************************************************************/
242 int regdb_fetch_reg_keys( char* key
, REGSUBKEY_CTR
*ctr
)
252 DEBUG(10,("regdb_fetch_reg_keys: Enter key => [%s]\n", key
? key
: "NULL"));
254 pstrcpy( path
, key
);
256 /* convert to key format */
257 pstring_sub( path
, "\\", "/" );
260 dbuf
= tdb_fetch_bystring( tdb_reg
, path
);
266 DEBUG(5,("regdb_fetch_reg_keys: tdb lookup failed to locate key [%s]\n", key
));
270 len
= tdb_unpack( buf
, buflen
, "d", &num_items
);
272 for (i
=0; i
<num_items
; i
++) {
273 len
+= tdb_unpack( buf
+len
, buflen
-len
, "f", subkeyname
);
274 regsubkey_ctr_addkey( ctr
, subkeyname
);
277 SAFE_FREE( dbuf
.dptr
);
279 DEBUG(10,("regdb_fetch_reg_keys: Exit [%d] items\n", num_items
));
285 /***********************************************************************
286 Retrieve an array of strings containing subkeys. Memory should be
287 released by the caller. The subkeys are stored in a catenated string
288 of null terminated character strings
289 ***********************************************************************/
291 int regdb_fetch_reg_values( char* key
, REGVAL_CTR
*val
)
296 /***********************************************************************
297 Stub function since we do not currently support storing registry
298 values in the registry.tdb
299 ***********************************************************************/
301 BOOL
regdb_store_reg_values( char *key
, REGVAL_CTR
*val
)
308 * Table of function pointers for default access
311 REGISTRY_OPS regdb_ops
= {
312 regdb_fetch_reg_keys
,
313 regdb_fetch_reg_values
,
314 regdb_store_reg_keys
,
315 regdb_store_reg_values