The previous code would not allow things like string_sub(str, "\\", "/", 0).
[Samba.git] / source / registry / reg_frontend.c
blobc4e332ed6b8c0b5eea1227d200ee1fad5e50bb19
1 /*
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. */
23 #include "includes.h"
25 #undef DBGC_CLASS
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 )
39 pstring keyname;
40 char *subkeys[3];
42 /* HKEY_LOCAL_MACHINE */
44 pstrcpy( keyname, KEY_HKLM );
45 subkeys[0] = "SYSTEM";
46 if ( !store_reg_keys( keyname, subkeys, 1 ))
47 return False;
49 pstrcpy( keyname, KEY_HKLM );
50 pstrcat( keyname, "/SYSTEM" );
51 subkeys[0] = "CurrentControlSet";
52 if ( !store_reg_keys( keyname, subkeys, 1 ))
53 return False;
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 ))
60 return False;
62 pstrcpy( keyname, KEY_HKLM );
63 pstrcat( keyname, "/SYSTEM/CurrentControlSet/Control" );
64 subkeys[0] = "Print";
65 subkeys[1] = "ProduceOptions";
66 if ( !store_reg_keys( keyname, subkeys, 2 ))
67 return False;
69 pstrcpy( keyname, KEY_HKLM );
70 pstrcat( keyname, "/SYSTEM/CurrentControlSet/Control/Print" );
71 subkeys[0] = "Environments";
72 subkeys[1] = "Forms";
73 subkeys[2] = "Printers";
74 if ( !store_reg_keys( keyname, subkeys, 3 ))
75 return False;
77 pstrcpy( keyname, KEY_HKLM );
78 pstrcat( keyname, "/SYSTEM/CurrentControlSet/Control/ProductOptions" );
79 if ( !store_reg_keys( keyname, subkeys, 0 ))
80 return False;
82 pstrcpy( keyname, KEY_HKLM );
83 pstrcat( keyname, "/SYSTEM/CurrentControlSet/services" );
84 subkeys[0] = "Netlogon";
85 if ( !store_reg_keys( keyname, subkeys, 1 ))
86 return False;
88 pstrcpy( keyname, KEY_HKLM );
89 pstrcat( keyname, "/SYSTEM/CurrentControlSet/services/Netlogon" );
90 subkeys[0] = "parameters";
91 if ( !store_reg_keys( keyname, subkeys, 1 ))
92 return False;
94 pstrcpy( keyname, KEY_HKLM );
95 pstrcat( keyname, "/SYSTEM/CurrentControlSet/services/Netlogon/parameters" );
96 if ( !store_reg_keys( keyname, subkeys, 0 ))
97 return False;
100 /* HKEY_USER */
102 pstrcpy( keyname, KEY_HKU );
103 if ( !store_reg_keys( keyname, subkeys, 0 ) )
104 return False;
106 return True;
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())
120 return True;
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);
128 if ( !tdb_reg )
130 tdb_reg = tdb_open_log(lock_path("registry.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
131 if ( !tdb_reg ) {
132 DEBUG(0,("init_registry: Failed to open registry %s (%s)\n",
133 lock_path("registry.tdb"), strerror(errno) ));
134 return False;
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"));
142 return False;
146 local_pid = sys_getpid();
148 return True;
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
154 fstrings
155 ***********************************************************************/
157 BOOL store_reg_keys( char *keyname, char **subkeys, uint32 num_subkeys )
159 TDB_DATA kbuf, dbuf;
160 char *buffer, *tmpbuf;
161 int i = 0;
162 uint32 len, buflen;
163 BOOL ret = True;
165 if ( !keyname )
166 return False;
168 /* allocate some initial memory */
170 buffer = malloc(sizeof(pstring));
171 buflen = sizeof(pstring);
172 len = 0;
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));
186 ret = False;
187 goto done;
189 buffer = tmpbuf;
190 buflen = len*2;
192 len = tdb_pack(buffer+len, buflen-len, "f", subkeys[i]);
196 /* finally write out the data */
198 kbuf.dptr = keyname;
199 kbuf.dsize = strlen(keyname)+1;
200 dbuf.dptr = buffer;
201 dbuf.dsize = len;
202 if ( tdb_store( tdb_reg, kbuf, dbuf, TDB_REPLACE ) == -1) {
203 ret = False;
204 goto done;
207 done:
208 SAFE_FREE( buffer );
209 return ret;
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 )
220 pstring path;
221 uint32 num_items;
222 TDB_DATA dbuf;
223 char *buf;
224 uint32 buflen, len;
225 int i;
226 char *s;
229 pstrcpy( path, key );
231 /* convert to key format */
232 pstring_sub( path, "\\", "/" );
234 dbuf = tdb_fetch_by_string( tdb_reg, path );
236 buf = dbuf.dptr;
237 buflen = dbuf.dsize;
239 if ( !buf ) {
240 DEBUG(5,("fetch_reg_keys: Failed to fetch any subkeys for [%s]\n", key));
241 return 0;
244 len = tdb_unpack( buf, buflen, "d", &num_items);
245 if (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",
248 num_items));
249 num_items = -1;
250 goto done;
254 s = *subkeys;
255 for (i=0; i<num_items; i++) {
256 len += tdb_unpack( buf+len, buflen-len, "f", s );
257 s += strlen(s) + 1;
260 done:
261 SAFE_FREE(dbuf.dptr);
262 return num_items;
265 /***********************************************************************
266 count the number of subkeys dtored in the registry
267 ***********************************************************************/
269 int fetch_reg_keys_count( char* key )
271 pstring path;
272 uint32 num_items;
273 TDB_DATA dbuf;
274 char *buf;
275 uint32 buflen, len;
278 pstrcpy( path, key );
280 /* convert to key format */
281 pstring_sub( path, "\\", "/" );
283 dbuf = tdb_fetch_by_string( tdb_reg, path );
285 buf = dbuf.dptr;
286 buflen = dbuf.dsize;
288 if ( !buf ) {
289 DEBUG(5,("fetch_reg_keys: Failed to fetch any subkeys for [%s]\n", key));
290 return 0;
293 len = tdb_unpack( buf, buflen, "d", &num_items);
295 SAFE_FREE( buf );
297 return 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 )
307 int num_subkeys, i;
308 char *subkeys = NULL;
309 char *s;
311 num_subkeys = fetch_reg_keys( key, &subkeys );
312 if ( num_subkeys == -1 )
313 return False;
315 s = subkeys;
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 );
320 break;
323 /* go onto the next string */
324 s += strlen(s) + 1;
327 SAFE_FREE(subkeys);
329 return True;