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. */
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[] = {
38 { KEY_PRINTING, &printing_ops },
39 { KEY_PRINTING_2K, &printing_ops },
40 { KEY_PRINTING_PORTS, &printing_ops },
42 { KEY_EVENTLOG, &eventlog_ops },
44 { KEY_SHARES, &shares_reg_ops },
50 /***********************************************************************
51 Open the registry database and initialize the REGISTRY_HOOK cache
52 ***********************************************************************/
54 BOOL init_registry( void )
58 if ( !init_registry_db() ) {
59 DEBUG(0,("init_registry: failed to initialize the registry tdb!\n"));
63 /* build the cache tree of registry hooks */
67 for ( i=0; reg_hooks[i].keyname; i++ ) {
68 if ( !reghook_cache_add(®_hooks[i]) )
72 if ( DEBUGLEVEL >= 20 )
73 reghook_dump_cache(20);
78 /***********************************************************************
79 High level wrapper function for storing registry subkeys
80 ***********************************************************************/
82 BOOL store_reg_keys( REGISTRY_KEY *key, REGSUBKEY_CTR *subkeys )
84 if ( key->hook && key->hook->ops && key->hook->ops->store_subkeys )
85 return key->hook->ops->store_subkeys( key->name, subkeys );
91 /***********************************************************************
92 High level wrapper function for storing registry values
93 ***********************************************************************/
95 BOOL store_reg_values( REGISTRY_KEY *key, REGVAL_CTR *val )
97 if ( check_dynamic_reg_values( key ) )
100 if ( key->hook && key->hook->ops && key->hook->ops->store_values )
101 return key->hook->ops->store_values( key->name, val );
107 /***********************************************************************
108 High level wrapper function for enumerating registry subkeys
109 Initialize the TALLOC_CTX if necessary
110 ***********************************************************************/
112 int fetch_reg_keys( REGISTRY_KEY *key, REGSUBKEY_CTR *subkey_ctr )
116 if ( key->hook && key->hook->ops && key->hook->ops->fetch_subkeys )
117 result = key->hook->ops->fetch_subkeys( key->name, subkey_ctr );
122 /***********************************************************************
123 retreive a specific subkey specified by index. Caller is
124 responsible for freeing memory
125 ***********************************************************************/
127 BOOL fetch_reg_keys_specific( REGISTRY_KEY *key, char** subkey, uint32 key_index )
129 static REGSUBKEY_CTR *ctr = NULL;
130 static pstring save_path;
135 /* simple caching for performance; very basic heuristic */
137 DEBUG(8,("fetch_reg_keys_specific: Looking for key [%d] of [%s]\n", key_index, key->name));
140 DEBUG(8,("fetch_reg_keys_specific: Initializing cache of subkeys for [%s]\n", key->name));
142 if ( !(ctr = TALLOC_ZERO_P( NULL, REGSUBKEY_CTR )) ) {
143 DEBUG(0,("fetch_reg_keys_specific: talloc() failed!\n"));
147 pstrcpy( save_path, key->name );
149 if ( fetch_reg_keys( key, ctr) == -1 )
153 /* clear the cache when key_index == 0 or the path has changed */
154 else if ( !key_index || StrCaseCmp( save_path, key->name) ) {
156 DEBUG(8,("fetch_reg_keys_specific: Updating cache of subkeys for [%s]\n", key->name));
160 if ( !(ctr = TALLOC_ZERO_P( NULL, REGSUBKEY_CTR )) ) {
161 DEBUG(0,("fetch_reg_keys_specific: talloc() failed!\n"));
165 pstrcpy( save_path, key->name );
167 if ( fetch_reg_keys( key, ctr) == -1 )
171 if ( !(s = regsubkey_ctr_specific_key( ctr, key_index )) )
174 *subkey = SMB_STRDUP( s );
179 /***********************************************************************
180 High level wrapper function for enumerating registry values
181 ***********************************************************************/
183 int fetch_reg_values( REGISTRY_KEY *key, REGVAL_CTR *val )
187 if ( key->hook && key->hook->ops && key->hook->ops->fetch_values )
188 result = key->hook->ops->fetch_values( key->name, val );
190 /* if the backend lookup returned no data, try the dynamic overlay */
193 result = fetch_dynamic_reg_values( key, val );
195 return ( result != -1 ) ? result : 0;
202 /***********************************************************************
203 retreive a specific subkey specified by index. Caller is
204 responsible for freeing memory
205 ***********************************************************************/
207 BOOL fetch_reg_values_specific( REGISTRY_KEY *key, REGISTRY_VALUE **val, uint32 val_index )
209 static REGVAL_CTR *ctr = NULL;
210 static pstring save_path;
215 /* simple caching for performance; very basic heuristic */
218 DEBUG(8,("fetch_reg_values_specific: Initializing cache of values for [%s]\n", key->name));
220 if ( !(ctr = TALLOC_ZERO_P( NULL, REGVAL_CTR )) ) {
221 DEBUG(0,("fetch_reg_values_specific: talloc() failed!\n"));
225 pstrcpy( save_path, key->name );
227 if ( fetch_reg_values( key, ctr) == -1 )
230 /* clear the cache when val_index == 0 or the path has changed */
231 else if ( !val_index || !strequal(save_path, key->name) ) {
233 DEBUG(8,("fetch_reg_values_specific: Updating cache of values for [%s]\n", key->name));
237 if ( !(ctr = TALLOC_ZERO_P( NULL, REGVAL_CTR )) ) {
238 DEBUG(0,("fetch_reg_values_specific: talloc() failed!\n"));
242 pstrcpy( save_path, key->name );
244 if ( fetch_reg_values( key, ctr) == -1 )
248 if ( !(v = regval_ctr_specific_value( ctr, val_index )) )
251 *val = dup_registry_value( v );
256 /***********************************************************************
257 High level access check for passing the required access mask to the
258 underlying registry backend
259 ***********************************************************************/
261 BOOL regkey_access_check( REGISTRY_KEY *key, uint32 requested, uint32 *granted, NT_USER_TOKEN *token )
263 /* use the default security check if the backend has not defined its own */
265 if ( !(key->hook && key->hook->ops && key->hook->ops->reg_access_check) ) {
269 if ( !(sec_desc = construct_registry_sd( get_talloc_ctx() )) )
272 status = registry_access_check( sec_desc, token, requested, granted );
274 return NT_STATUS_IS_OK(status);
277 return key->hook->ops->reg_access_check( key->name, requested, granted, token );