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 registry database functions. */
26 #define DBGC_CLASS DBGC_RPC_SRV
30 static TDB_CONTEXT
*tdb_reg
;
33 /***********************************************************************
34 Open the registry database
35 ***********************************************************************/
37 static BOOL
init_registry_data( void )
42 /* HKEY_LOCAL_MACHINE */
44 pstrcpy( keyname
, KEY_HKLM
);
45 subkeys
[0] = "SYSTEM";
46 if ( !store_reg_keys( keyname
, subkeys
, 1 ))
49 pstrcpy( keyname
, KEY_HKLM
);
50 pstrcat( keyname
, "/SYSTEM" );
51 subkeys
[0] = "CurrentControlSet";
52 if ( !store_reg_keys( keyname
, subkeys
, 1 ))
55 pstrcpy( keyname
, KEY_HKLM
);
56 pstrcat( keyname
, "/SYSTEM/CurrentControlSet" );
57 subkeys
[0] = "Control";
58 subkeys
[1] = "services";
59 if ( !store_reg_keys( keyname
, subkeys
, 2 ))
62 pstrcpy( keyname
, KEY_HKLM
);
63 pstrcat( keyname
, "/SYSTEM/CurrentControlSet/Control" );
65 subkeys
[1] = "ProduceOptions";
66 if ( !store_reg_keys( keyname
, subkeys
, 2 ))
69 pstrcpy( keyname
, KEY_HKLM
);
70 pstrcat( keyname
, "/SYSTEM/CurrentControlSet/Control/Print" );
71 subkeys
[0] = "Environments";
73 subkeys
[2] = "Printers";
74 if ( !store_reg_keys( keyname
, subkeys
, 3 ))
77 pstrcpy( keyname
, KEY_HKLM
);
78 pstrcat( keyname
, "/SYSTEM/CurrentControlSet/Control/ProductOptions" );
79 if ( !store_reg_keys( keyname
, subkeys
, 0 ))
82 pstrcpy( keyname
, KEY_HKLM
);
83 pstrcat( keyname
, "/SYSTEM/CurrentControlSet/services" );
84 subkeys
[0] = "Netlogon";
85 if ( !store_reg_keys( keyname
, subkeys
, 1 ))
88 pstrcpy( keyname
, KEY_HKLM
);
89 pstrcat( keyname
, "/SYSTEM/CurrentControlSet/services/Netlogon" );
90 subkeys
[0] = "parameters";
91 if ( !store_reg_keys( keyname
, subkeys
, 1 ))
94 pstrcpy( keyname
, KEY_HKLM
);
95 pstrcat( keyname
, "/SYSTEM/CurrentControlSet/services/Netlogon/parameters" );
96 if ( !store_reg_keys( keyname
, subkeys
, 0 ))
102 pstrcpy( keyname
, KEY_HKU
);
103 if ( !store_reg_keys( keyname
, subkeys
, 0 ) )
110 /***********************************************************************
111 Open the registry database
112 ***********************************************************************/
114 BOOL
init_registry( void )
116 static pid_t local_pid
;
119 if (tdb_reg
&& local_pid
== sys_getpid())
123 * try to open first without creating so we can determine
124 * if we need to init the data in the registry
127 tdb_reg
= tdb_open_log(lock_path("registry.tdb"), 0, TDB_DEFAULT
, O_RDWR
, 0600);
130 tdb_reg
= tdb_open_log(lock_path("registry.tdb"), 0, TDB_DEFAULT
, O_RDWR
|O_CREAT
, 0600);
132 DEBUG(0,("init_registry: Failed to open registry %s (%s)\n",
133 lock_path("registry.tdb"), strerror(errno
) ));
137 DEBUG(10,("init_registry: Successfully created registry tdb\n"));
139 /* create the registry here */
140 if ( !init_registry_data() ) {
141 DEBUG(0,("init_registry: Failed to initiailize data in registry!\n"));
146 local_pid
= sys_getpid();
151 /***********************************************************************
152 Add subkey strings to the registry tdb under a defined key
153 fmt is the same format as tdb_pack except this function only supports
155 ***********************************************************************/
157 BOOL
store_reg_keys( char *keyname
, char **subkeys
, uint32 num_subkeys
)
160 char *buffer
, *tmpbuf
;
168 /* allocate some initial memory */
170 buffer
= malloc(sizeof(pstring
));
171 buflen
= sizeof(pstring
);
174 /* store the number of subkeys */
176 len
+= tdb_pack(buffer
+len
, buflen
-len
, "d", num_subkeys
);
178 /* pack all the strings */
180 for (i
=0; i
<num_subkeys
; i
++) {
181 len
+= tdb_pack(buffer
+len
, buflen
-len
, "f", subkeys
[i
]);
182 if ( len
> buflen
) {
183 /* allocate some extra space */
184 if ((tmpbuf
= Realloc( buffer
, len
*2 )) == NULL
) {
185 DEBUG(0,("store_reg_keys: Failed to realloc memory of size [%d]\n", len
*2));
192 len
= tdb_pack(buffer
+len
, buflen
-len
, "f", subkeys
[i
]);
196 /* finally write out the data */
199 kbuf
.dsize
= strlen(keyname
)+1;
202 if ( tdb_store( tdb_reg
, kbuf
, dbuf
, TDB_REPLACE
) == -1) {
212 /***********************************************************************
213 Retrieve an array of strings containing subkeys. Memory should be
214 released by the caller. The subkeys are stored in a catenated string
215 of null terminated character strings
216 ***********************************************************************/
218 int fetch_reg_keys( char* key
, char **subkeys
)
229 pstrcpy( path
, key
);
231 /* convert to key format */
232 pstring_sub( path
, "\\", "/" );
234 dbuf
= tdb_fetch_by_string( tdb_reg
, path
);
240 DEBUG(5,("fetch_reg_keys: Failed to fetch any subkeys for [%s]\n", key
));
244 len
= tdb_unpack( buf
, buflen
, "d", &num_items
);
246 if ( (*subkeys
= (char*)malloc(sizeof(fstring
)*num_items
)) == NULL
) {
247 DEBUG(0,("fetch_reg_keys: Failed to malloc memory for subkey array containing [%d] items!\n",
255 for (i
=0; i
<num_items
; i
++) {
256 len
+= tdb_unpack( buf
+len
, buflen
-len
, "f", s
);
261 SAFE_FREE(dbuf
.dptr
);
265 /***********************************************************************
266 count the number of subkeys dtored in the registry
267 ***********************************************************************/
269 int fetch_reg_keys_count( char* key
)
278 pstrcpy( path
, key
);
280 /* convert to key format */
281 pstring_sub( path
, "\\", "/" );
283 dbuf
= tdb_fetch_by_string( tdb_reg
, path
);
289 DEBUG(5,("fetch_reg_keys: Failed to fetch any subkeys for [%s]\n", key
));
293 len
= tdb_unpack( buf
, buflen
, "d", &num_items
);
300 /***********************************************************************
301 retreive a specific subkey specified by index. The subkey parameter
302 is assumed to be an fstring.
303 ***********************************************************************/
305 BOOL
fetch_reg_keys_specific( char* key
, char* subkey
, uint32 key_index
)
308 char *subkeys
= NULL
;
311 num_subkeys
= fetch_reg_keys( key
, &subkeys
);
312 if ( num_subkeys
== -1 )
316 for ( i
=0; i
<num_subkeys
; i
++ ) {
317 /* copy the key if the index matches */
318 if ( i
== key_index
) {
319 fstrcpy( subkey
, s
);
323 /* go onto the next string */