Fixed a compiler warning.
[vlendec/samba-autobuild/.git] / source3 / registry / reg_frontend.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 registry frontend view functions. */
22
23 #include "includes.h"
24
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_RPC_SRV
27
28 extern REGISTRY_OPS printing_ops;
29 extern REGISTRY_OPS regdb_ops;          /* these are the default */
30
31 /* array of REGISTRY_HOOK's which are read into a tree for easy access */
32
33
34 REGISTRY_HOOK reg_hooks[] = {
35   { KEY_TREE_ROOT,  &regdb_ops    },
36   { KEY_PRINTING,   &printing_ops },
37   { NULL, NULL }
38 };
39
40
41 /***********************************************************************
42  Open the registry database and initialize the REGISTRY_HOOK cache
43  ***********************************************************************/
44  
45 BOOL init_registry( void )
46 {
47         int i;
48         
49         if ( !init_registry_db() ) {
50                 DEBUG(0,("init_registry: failed to initialize the registry tdb!\n"));
51                 return False;
52         }
53                 
54         /* build the cache tree of registry hooks */
55         
56         reghook_cache_init();
57         
58         for ( i=0; reg_hooks[i].keyname; i++ ) {
59                 if ( !reghook_cache_add(&reg_hooks[i]) )
60                         return False;
61         }
62
63         reghook_dump_cache(20);
64
65         return True;
66 }
67
68
69
70
71 /***********************************************************************
72  High level wrapper function for storing registry subkeys
73  ***********************************************************************/
74  
75 BOOL store_reg_keys( REGISTRY_KEY *key, REGSUBKEY_CTR *subkeys )
76 {
77         if ( key->hook && key->hook->ops && key->hook->ops->store_subkeys_fn )
78                 return key->hook->ops->store_subkeys_fn( key->name, subkeys );
79         else
80                 return False;
81
82 }
83
84 /***********************************************************************
85  High level wrapper function for storing registry values
86  ***********************************************************************/
87  
88 BOOL store_reg_values( REGISTRY_KEY *key, REGVAL_CTR *val )
89 {
90         if ( key->hook && key->hook->ops && key->hook->ops->store_values_fn )
91                 return key->hook->ops->store_values_fn( key->name, val );
92         else
93                 return False;
94 }
95
96
97 /***********************************************************************
98  High level wrapper function for enumerating registry subkeys
99  Initialize the TALLOC_CTX if necessary
100  ***********************************************************************/
101
102 int fetch_reg_keys( REGISTRY_KEY *key, REGSUBKEY_CTR *subkey_ctr )
103 {
104         int result = -1;
105         
106         if ( key->hook && key->hook->ops && key->hook->ops->subkey_fn )
107                 result = key->hook->ops->subkey_fn( key->name, subkey_ctr );
108
109         return result;
110 }
111
112 /***********************************************************************
113  retreive a specific subkey specified by index.  Caller is 
114  responsible for freeing memory
115  ***********************************************************************/
116
117 BOOL fetch_reg_keys_specific( REGISTRY_KEY *key, char** subkey, uint32 key_index )
118 {
119         char *s;
120         REGSUBKEY_CTR ctr;
121         
122         ZERO_STRUCTP( &ctr );
123         
124         regsubkey_ctr_init( &ctr );
125         
126         if ( fetch_reg_keys( key, &ctr) == -1 )
127                 return False;
128
129         if ( !(s = regsubkey_ctr_specific_key( &ctr, key_index )) )
130                 return False;
131
132         *subkey = strdup( s );
133
134         regsubkey_ctr_destroy( &ctr ); 
135         
136         return True;
137 }
138
139
140 /***********************************************************************
141  High level wrapper function for enumerating registry values
142  Initialize the TALLOC_CTX if necessary
143  ***********************************************************************/
144
145 int fetch_reg_values( REGISTRY_KEY *key, REGVAL_CTR *val )
146 {
147         int result = -1;
148         
149         if ( key->hook && key->hook->ops && key->hook->ops->value_fn )
150                 result = key->hook->ops->value_fn( key->name, val );
151
152         return result;
153 }
154
155 /***********************************************************************
156  Utility function for splitting the base path of a registry path off
157  by setting base and new_path to the apprapriate offsets withing the
158  path.
159  
160  WARNING!!  Does modify the original string!
161  ***********************************************************************/
162
163 BOOL reg_split_path( char *path, char **base, char **new_path )
164 {
165         char *p;
166         
167         *new_path = *base = NULL;
168         
169         if ( !path)
170                 return False;
171         
172         *base = path;
173         
174         p = strchr( path, '\\' );
175         
176         if ( p ) {
177                 *p = '\0';
178                 *new_path = p+1;
179         }
180         
181         return True;
182 }
183
184
185 /*
186  * Utility functions for REGSUBKEY_CTR
187  */
188
189 /***********************************************************************
190  Init the talloc context held by a REGSUBKEY_CTR structure
191  **********************************************************************/
192
193 void regsubkey_ctr_init( REGSUBKEY_CTR *ctr )
194 {
195         if ( !ctr->ctx )
196                 ctr->ctx = talloc_init();
197 }
198
199 /***********************************************************************
200  Add a new key to the array
201  **********************************************************************/
202
203 int regsubkey_ctr_addkey( REGSUBKEY_CTR *ctr, char *keyname )
204 {
205         uint32 len;
206         
207         if ( keyname )
208         {
209                 len = strlen( keyname );
210
211                 if (  ctr->subkeys == 0 )
212                         ctr->subkeys = talloc( ctr->ctx, 1 );
213                 else
214                         talloc_realloc( ctr->ctx, ctr->subkeys, ctr->num_subkeys+1 );
215
216                 ctr->subkeys[ctr->num_subkeys] = talloc( ctr->ctx, len+1 );
217                 strncpy( ctr->subkeys[ctr->num_subkeys], keyname, len+1 );
218                 ctr->num_subkeys++;
219         }
220         
221         return ctr->num_subkeys;
222 }
223  
224 /***********************************************************************
225  How many keys does the container hold ?
226  **********************************************************************/
227
228 int regsubkey_ctr_numkeys( REGSUBKEY_CTR *ctr )
229 {
230         return ctr->num_subkeys;
231 }
232
233 /***********************************************************************
234  Retreive a specific key string
235  **********************************************************************/
236
237 char* regsubkey_ctr_specific_key( REGSUBKEY_CTR *ctr, uint32 key_index )
238 {
239         if ( ! (key_index < ctr->num_subkeys) )
240                 return NULL;
241                 
242         return ctr->subkeys[key_index];
243 }
244
245 /***********************************************************************
246  free memory held by a REGSUBKEY_CTR structure
247  **********************************************************************/
248
249 void regsubkey_ctr_destroy( REGSUBKEY_CTR *ctr )
250 {
251         if ( ctr )
252                 talloc_destroy( ctr->ctx );
253                 
254         ctr->num_subkeys  = 0;
255         ctr->subkeys      = NULL;
256 }
257
258
259 /*
260  * Utility functions for REGVAL_CTR
261  */
262
263 /***********************************************************************
264  Init the talloc context held by a REGSUBKEY_CTR structure
265  **********************************************************************/
266
267 void regval_ctr_init( REGVAL_CTR *ctr )
268 {
269         if ( !ctr->ctx )
270                 ctr->ctx = talloc_init();
271 }
272
273 /***********************************************************************
274  How many keys does the container hold ?
275  **********************************************************************/
276
277 int regval_ctr_numvals( REGVAL_CTR *ctr )
278 {
279         return ctr->num_values;
280 }
281
282 /***********************************************************************
283  free memory held by a REGVAL_CTR structure
284  **********************************************************************/
285
286 void regval_ctr_destroy( REGVAL_CTR *ctr )
287 {
288         if ( ctr )
289                 talloc_destroy( ctr->ctx );
290                 
291         ctr->num_values  = 0;
292         ctr->values      = NULL;
293 }
294