r7997: Pointers don't kill people, people with pointers kill people...
[Samba/gbeck.git] / source / registry / reg_frontend.c
blob51ad23b49847a3a36de3daf4e2619b72a8bb4164
1 /*
2 * Unix SMB/CIFS implementation.
3 * Virtual Windows Registry Layer
4 * Copyright (C) Gerald Carter 2002-2005
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 frontend view functions. */
23 #include "includes.h"
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_RPC_SRV
28 extern REGISTRY_OPS printing_ops;
29 extern REGISTRY_OPS eventlog_ops;
30 extern REGISTRY_OPS shares_reg_ops;
31 extern REGISTRY_OPS regdb_ops; /* these are the default */
33 /* array of REGISTRY_HOOK's which are read into a tree for easy access */
34 /* #define REG_TDB_ONLY 1 */
36 REGISTRY_HOOK reg_hooks[] = {
37 #ifndef REG_TDB_ONLY
38 { KEY_PRINTING, &printing_ops },
39 { KEY_PRINTING_2K, &printing_ops },
40 { KEY_PRINTING_PORTS, &printing_ops },
41 { KEY_EVENTLOG, &eventlog_ops },
42 { KEY_SHARES, &shares_reg_ops },
43 #endif
44 { NULL, NULL }
48 /***********************************************************************
49 Open the registry database and initialize the REGISTRY_HOOK cache
50 ***********************************************************************/
52 BOOL init_registry( void )
54 int i;
56 if ( !init_registry_db() ) {
57 DEBUG(0,("init_registry: failed to initialize the registry tdb!\n"));
58 return False;
61 /* build the cache tree of registry hooks */
63 reghook_cache_init();
65 for ( i=0; reg_hooks[i].keyname; i++ ) {
66 if ( !reghook_cache_add(&reg_hooks[i]) )
67 return False;
70 if ( DEBUGLEVEL >= 20 )
71 reghook_dump_cache(20);
73 return True;
76 /***********************************************************************
77 High level wrapper function for storing registry subkeys
78 ***********************************************************************/
80 BOOL store_reg_keys( REGISTRY_KEY *key, REGSUBKEY_CTR *subkeys )
82 if ( key->hook && key->hook->ops && key->hook->ops->store_subkeys )
83 return key->hook->ops->store_subkeys( key->name, subkeys );
85 return False;
89 /***********************************************************************
90 High level wrapper function for storing registry values
91 ***********************************************************************/
93 BOOL store_reg_values( REGISTRY_KEY *key, REGVAL_CTR *val )
95 if ( check_dynamic_reg_values( key ) )
96 return False;
98 if ( key->hook && key->hook->ops && key->hook->ops->store_values )
99 return key->hook->ops->store_values( key->name, val );
101 return False;
105 /***********************************************************************
106 High level wrapper function for enumerating registry subkeys
107 Initialize the TALLOC_CTX if necessary
108 ***********************************************************************/
110 int fetch_reg_keys( REGISTRY_KEY *key, REGSUBKEY_CTR *subkey_ctr )
112 int result = -1;
114 if ( key->hook && key->hook->ops && key->hook->ops->fetch_subkeys )
115 result = key->hook->ops->fetch_subkeys( key->name, subkey_ctr );
117 return result;
120 /***********************************************************************
121 retreive a specific subkey specified by index. Caller is
122 responsible for freeing memory
123 ***********************************************************************/
125 BOOL fetch_reg_keys_specific( REGISTRY_KEY *key, char** subkey, uint32 key_index )
127 static REGSUBKEY_CTR ctr;
128 static pstring save_path;
129 static BOOL ctr_init = False;
130 char *s;
132 *subkey = NULL;
134 /* simple caching for performance; very basic heuristic */
136 DEBUG(8,("fetch_reg_keys_specific: Looking for key [%d] of [%s]\n", key_index, key->name));
138 if ( !ctr_init ) {
139 DEBUG(8,("fetch_reg_keys_specific: Initializing cache of subkeys for [%s]\n", key->name));
140 regsubkey_ctr_init( &ctr );
142 pstrcpy( save_path, key->name );
144 if ( fetch_reg_keys( key, &ctr) == -1 )
145 return False;
147 ctr_init = True;
149 /* clear the cache when key_index == 0 or the path has changed */
150 else if ( !key_index || StrCaseCmp( save_path, key->name) ) {
152 DEBUG(8,("fetch_reg_keys_specific: Updating cache of subkeys for [%s]\n", key->name));
154 regsubkey_ctr_destroy( &ctr );
155 regsubkey_ctr_init( &ctr );
157 pstrcpy( save_path, key->name );
159 if ( fetch_reg_keys( key, &ctr) == -1 )
160 return False;
163 if ( !(s = regsubkey_ctr_specific_key( &ctr, key_index )) )
164 return False;
166 *subkey = SMB_STRDUP( s );
168 return True;
171 /***********************************************************************
172 High level wrapper function for enumerating registry values
173 ***********************************************************************/
175 int fetch_reg_values( REGISTRY_KEY *key, REGVAL_CTR *val )
177 int result = -1;
179 if ( key->hook && key->hook->ops && key->hook->ops->fetch_values )
180 result = key->hook->ops->fetch_values( key->name, val );
182 /* if the backend lookup returned no data, try the dynamic overlay */
184 if ( result == 0 ) {
185 result = fetch_dynamic_reg_values( key, val );
187 return ( result != -1 ) ? result : 0;
190 return result;
194 /***********************************************************************
195 retreive a specific subkey specified by index. Caller is
196 responsible for freeing memory
197 ***********************************************************************/
199 BOOL fetch_reg_values_specific( REGISTRY_KEY *key, REGISTRY_VALUE **val, uint32 val_index )
201 static REGVAL_CTR ctr;
202 static pstring save_path;
203 static BOOL ctr_init = False;
204 REGISTRY_VALUE *v;
206 *val = NULL;
208 /* simple caching for performance; very basic heuristic */
210 if ( !ctr_init ) {
211 DEBUG(8,("fetch_reg_values_specific: Initializing cache of values for [%s]\n", key->name));
213 regval_ctr_init( &ctr );
215 pstrcpy( save_path, key->name );
217 if ( fetch_reg_values( key, &ctr) == -1 )
218 return False;
220 ctr_init = True;
222 /* clear the cache when val_index == 0 or the path has changed */
223 else if ( !val_index || !strequal(save_path, key->name) ) {
225 DEBUG(8,("fetch_reg_values_specific: Updating cache of values for [%s]\n", key->name));
227 regval_ctr_destroy( &ctr );
228 regval_ctr_init( &ctr );
230 pstrcpy( save_path, key->name );
232 if ( fetch_reg_values( key, &ctr) == -1 )
233 return False;
236 if ( !(v = regval_ctr_specific_value( &ctr, val_index )) )
237 return False;
239 *val = dup_registry_value( v );
241 return True;
244 /***********************************************************************
245 High level access check for passing the required access mask to the
246 underlying registry backend
247 ***********************************************************************/
249 BOOL regkey_access_check( REGISTRY_KEY *key, uint32 requested, uint32 *granted, NT_USER_TOKEN *token )
251 /* use the default security check if the backend has not defined its own */
253 if ( !(key->hook && key->hook->ops && key->hook->ops->reg_access_check) ) {
254 SEC_DESC *sec_desc;
255 NTSTATUS status;
257 if ( !(sec_desc = construct_registry_sd( get_talloc_ctx() )) )
258 return False;
260 status = registry_access_check( sec_desc, token, requested, granted );
262 return NT_STATUS_IS_OK(status);
265 return key->hook->ops->reg_access_check( key->name, requested, granted, token );