f93b7e1ffba74b8a4f03ea2be46c8992dd0034fc
[ira/wip.git] / source3 / registry / reg_db.c
1 /* 
2  *  Unix SMB/CIFS implementation.
3  *  RPC Pipe client / server routines
4  *  Copyright (C) Gerald Carter                     2002.
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
31 /***********************************************************************
32  Open the registry data in the tdb
33  ***********************************************************************/
34  
35 static BOOL init_registry_data( void )
36 {
37         pstring         keyname;
38         REGSUBKEY_CTR   subkeys;
39
40         ZERO_STRUCTP( &subkeys );
41
42         /* HKEY_LOCAL_MACHINE */
43         
44         regsubkey_ctr_init( &subkeys );
45         pstrcpy( keyname, KEY_HKLM );
46         regsubkey_ctr_addkey( &subkeys, "SYSTEM" );
47         if ( !regdb_store_reg_keys( keyname, &subkeys ))
48                 return False;
49         regsubkey_ctr_destroy( &subkeys );
50                 
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 ))
56                 return False;
57         regsubkey_ctr_destroy( &subkeys );
58                 
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 ))
65                 return False;
66         regsubkey_ctr_destroy( &subkeys );
67
68 #ifdef REG_TEST_CODE
69         pstrcpy( keyname, KEY_HKLM );
70         pstrcat( keyname, "/SOFTWARE/Microsoft/Windows NT/CurrentVersion/Print" );
71         if ( !regdb_store_reg_keys( keyname, &subkeys ))
72                 return False;
73 #endif
74
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 ))
81                 return False;
82         regsubkey_ctr_destroy( &subkeys );
83
84         pstrcpy( keyname, KEY_HKLM );
85         pstrcat( keyname, "/SYSTEM/CurrentControlSet/Control/ProductOptions" );
86         if ( !regdb_store_reg_keys( keyname, &subkeys ))
87                 return False;
88
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 ))
94                 return False;
95         regsubkey_ctr_destroy( &subkeys );
96                 
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 ))
102                 return False;
103         regsubkey_ctr_destroy( &subkeys );
104                 
105         pstrcpy( keyname, KEY_HKLM );
106         pstrcat( keyname, "/SYSTEM/CurrentControlSet/Services/Netlogon/Parameters" );
107         if ( !regdb_store_reg_keys( keyname, &subkeys ))
108                 return False;
109         
110         /* HKEY_USER */
111                 
112         pstrcpy( keyname, KEY_HKU );
113         if ( !regdb_store_reg_keys( keyname, &subkeys ) )
114                 return False;
115                 
116         /* HKEY_CLASSES_ROOT*/
117                 
118         pstrcpy( keyname, KEY_HKCR );
119         if ( !regdb_store_reg_keys( keyname, &subkeys ) )
120                 return False;
121                 
122         return True;
123 }
124
125 /***********************************************************************
126  Open the registry database
127  ***********************************************************************/
128  
129 BOOL init_registry_db( void )
130 {
131         static pid_t local_pid;
132
133         if (tdb_reg && local_pid == sys_getpid())
134                 return True;
135
136         /* 
137          * try to open first without creating so we can determine
138          * if we need to init the data in the registry
139          */
140         
141         tdb_reg = tdb_open_log(lock_path("registry.tdb"), 0, TDB_DEFAULT, O_RDWR, 0600);
142         if ( !tdb_reg ) 
143         {
144                 tdb_reg = tdb_open_log(lock_path("registry.tdb"), 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
145                 if ( !tdb_reg ) {
146                         DEBUG(0,("init_registry: Failed to open registry %s (%s)\n",
147                                 lock_path("registry.tdb"), strerror(errno) ));
148                         return False;
149                 }
150                 
151                 DEBUG(10,("init_registry: Successfully created registry tdb\n"));
152                 
153                 /* create the registry here */
154                 if ( !init_registry_data() ) {
155                         DEBUG(0,("init_registry: Failed to initiailize data in registry!\n"));
156                         return False;
157                 }
158         }
159
160         local_pid = sys_getpid();
161                 
162         return True;
163 }
164
165
166
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
170  fstrings
171
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
174  case.
175  ***********************************************************************/
176  
177 BOOL regdb_store_reg_keys( char *keyname, REGSUBKEY_CTR *ctr )
178 {
179         TDB_DATA kbuf, dbuf;
180         char *buffer, *tmpbuf;
181         int i = 0;
182         uint32 len, buflen;
183         BOOL ret = True;
184         uint32 num_subkeys = regsubkey_ctr_numkeys( ctr );
185         
186         if ( !keyname )
187                 return False;
188         
189         strupper_m( keyname  );
190         
191         /* allocate some initial memory */
192                 
193         buffer = SMB_MALLOC(sizeof(pstring));
194         buflen = sizeof(pstring);
195         len = 0;
196         
197         /* store the number of subkeys */
198         
199         len += tdb_pack(buffer+len, buflen-len, "d", num_subkeys );
200         
201         /* pack all the strings */
202         
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));
209                                 ret = False;
210                                 goto done;
211                         }
212                         buffer = tmpbuf;
213                         buflen = len*2;
214                                         
215                         len = tdb_pack( buffer+len, buflen-len, "f", regsubkey_ctr_specific_key(ctr, i) );
216                 }               
217         }
218         
219         /* finally write out the data */
220         
221         kbuf.dptr = keyname;
222         kbuf.dsize = strlen(keyname)+1;
223         dbuf.dptr = buffer;
224         dbuf.dsize = len;
225         if ( tdb_store( tdb_reg, kbuf, dbuf, TDB_REPLACE ) == -1) {
226                 ret = False;
227                 goto done;
228         }
229
230 done:           
231         SAFE_FREE( buffer );
232         
233         return ret;
234 }
235
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  ***********************************************************************/
241
242 int regdb_fetch_reg_keys( char* key, REGSUBKEY_CTR *ctr )
243 {
244         pstring path;
245         uint32 num_items;
246         TDB_DATA dbuf;
247         char *buf;
248         uint32 buflen, len;
249         int i;
250         fstring subkeyname;
251
252         DEBUG(10,("regdb_fetch_reg_keys: Enter key => [%s]\n", key ? key : "NULL"));
253         
254         pstrcpy( path, key );
255         
256         /* convert to key format */
257         pstring_sub( path, "\\", "/" ); 
258         strupper_m( path );
259         
260         dbuf = tdb_fetch_bystring( tdb_reg, path );
261         
262         buf = dbuf.dptr;
263         buflen = dbuf.dsize;
264         
265         if ( !buf ) {
266                 DEBUG(5,("regdb_fetch_reg_keys: tdb lookup failed to locate key [%s]\n", key));
267                 return -1;
268         }
269         
270         len = tdb_unpack( buf, buflen, "d", &num_items);
271         
272         for (i=0; i<num_items; i++) {
273                 len += tdb_unpack( buf+len, buflen-len, "f", subkeyname );
274                 regsubkey_ctr_addkey( ctr, subkeyname );
275         }
276
277         SAFE_FREE( dbuf.dptr );
278         
279         DEBUG(10,("regdb_fetch_reg_keys: Exit [%d] items\n", num_items));
280         
281         return num_items;
282 }
283
284
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  ***********************************************************************/
290
291 int regdb_fetch_reg_values( char* key, REGVAL_CTR *val )
292 {
293         return 0;
294 }
295
296 /***********************************************************************
297  Stub function since we do not currently support storing registry 
298  values in the registry.tdb
299  ***********************************************************************/
300
301 BOOL regdb_store_reg_values( char *key, REGVAL_CTR *val )
302 {
303         return False;
304 }
305
306
307 /* 
308  * Table of function pointers for default access
309  */
310  
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
316 };
317
318