r7938: * move the hardcoded registry value names from _reg_query_value()
[Samba/gbeck.git] / source / registry / reg_frontend.c
blob440f108cc97861ca4eeb5ff3c907b651ac82330b
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 */
35 REGISTRY_HOOK reg_hooks[] = {
36 { KEY_PRINTING, &printing_ops },
37 { KEY_PRINTING_2K, &printing_ops },
38 { KEY_PRINTING_PORTS, &printing_ops },
39 { KEY_EVENTLOG, &eventlog_ops },
40 { KEY_SHARES, &shares_reg_ops },
41 { NULL, NULL }
45 /***********************************************************************
46 Open the registry database and initialize the REGISTRY_HOOK cache
47 ***********************************************************************/
49 BOOL init_registry( void )
51 int i;
53 if ( !init_registry_db() ) {
54 DEBUG(0,("init_registry: failed to initialize the registry tdb!\n"));
55 return False;
58 /* build the cache tree of registry hooks */
60 reghook_cache_init();
62 for ( i=0; reg_hooks[i].keyname; i++ ) {
63 if ( !reghook_cache_add(&reg_hooks[i]) )
64 return False;
67 if ( DEBUGLEVEL >= 20 )
68 reghook_dump_cache(20);
70 return True;
73 /***********************************************************************
74 High level wrapper function for storing registry subkeys
75 ***********************************************************************/
77 BOOL store_reg_keys( REGISTRY_KEY *key, REGSUBKEY_CTR *subkeys )
79 if ( key->hook && key->hook->ops && key->hook->ops->store_subkeys )
80 return key->hook->ops->store_subkeys( key->name, subkeys );
82 return False;
86 /***********************************************************************
87 High level wrapper function for storing registry values
88 ***********************************************************************/
90 BOOL store_reg_values( REGISTRY_KEY *key, REGVAL_CTR *val )
92 if ( check_dynamic_reg_values( key ) )
93 return False;
95 if ( key->hook && key->hook->ops && key->hook->ops->store_values )
96 return key->hook->ops->store_values( key->name, val );
98 return False;
102 /***********************************************************************
103 High level wrapper function for enumerating registry subkeys
104 Initialize the TALLOC_CTX if necessary
105 ***********************************************************************/
107 int fetch_reg_keys( REGISTRY_KEY *key, REGSUBKEY_CTR *subkey_ctr )
109 int result = -1;
111 if ( key->hook && key->hook->ops && key->hook->ops->fetch_subkeys )
112 result = key->hook->ops->fetch_subkeys( key->name, subkey_ctr );
114 return result;
117 /***********************************************************************
118 retreive a specific subkey specified by index. Caller is
119 responsible for freeing memory
120 ***********************************************************************/
122 BOOL fetch_reg_keys_specific( REGISTRY_KEY *key, char** subkey, uint32 key_index )
124 static REGSUBKEY_CTR ctr;
125 static pstring save_path;
126 static BOOL ctr_init = False;
127 char *s;
129 *subkey = NULL;
131 /* simple caching for performance; very basic heuristic */
133 DEBUG(8,("fetch_reg_keys_specific: Looking for key [%d] of [%s]\n", key_index, key->name));
135 if ( !ctr_init ) {
136 DEBUG(8,("fetch_reg_keys_specific: Initializing cache of subkeys for [%s]\n", key->name));
137 regsubkey_ctr_init( &ctr );
139 pstrcpy( save_path, key->name );
141 if ( fetch_reg_keys( key, &ctr) == -1 )
142 return False;
144 ctr_init = True;
146 /* clear the cache when key_index == 0 or the path has changed */
147 else if ( !key_index || StrCaseCmp( save_path, key->name) ) {
149 DEBUG(8,("fetch_reg_keys_specific: Updating cache of subkeys for [%s]\n", key->name));
151 regsubkey_ctr_destroy( &ctr );
152 regsubkey_ctr_init( &ctr );
154 pstrcpy( save_path, key->name );
156 if ( fetch_reg_keys( key, &ctr) == -1 )
157 return False;
160 if ( !(s = regsubkey_ctr_specific_key( &ctr, key_index )) )
161 return False;
163 *subkey = SMB_STRDUP( s );
165 return True;
168 /***********************************************************************
169 High level wrapper function for enumerating registry values
170 ***********************************************************************/
172 int fetch_reg_values( REGISTRY_KEY *key, REGVAL_CTR *val )
174 int result = -1;
176 if ( key->hook && key->hook->ops && key->hook->ops->fetch_values )
177 result = key->hook->ops->fetch_values( key->name, val );
179 /* if the backend lookup returned no data, try the dynamic overlay */
181 if ( result == 0 ) {
182 result = fetch_dynamic_reg_values( key, val );
184 return ( result != -1 ) ? result : 0;
187 return result;
191 /***********************************************************************
192 retreive a specific subkey specified by index. Caller is
193 responsible for freeing memory
194 ***********************************************************************/
196 BOOL fetch_reg_values_specific( REGISTRY_KEY *key, REGISTRY_VALUE **val, uint32 val_index )
198 static REGVAL_CTR ctr;
199 static pstring save_path;
200 static BOOL ctr_init = False;
201 REGISTRY_VALUE *v;
203 *val = NULL;
205 /* simple caching for performance; very basic heuristic */
207 if ( !ctr_init ) {
208 DEBUG(8,("fetch_reg_values_specific: Initializing cache of values for [%s]\n", key->name));
210 regval_ctr_init( &ctr );
212 pstrcpy( save_path, key->name );
214 if ( fetch_reg_values( key, &ctr) == -1 )
215 return False;
217 ctr_init = True;
219 /* clear the cache when val_index == 0 or the path has changed */
220 else if ( !val_index || !strequal(save_path, key->name) ) {
222 DEBUG(8,("fetch_reg_values_specific: Updating cache of values for [%s]\n", key->name));
224 regval_ctr_destroy( &ctr );
225 regval_ctr_init( &ctr );
227 pstrcpy( save_path, key->name );
229 if ( fetch_reg_values( key, &ctr) == -1 )
230 return False;
233 if ( !(v = regval_ctr_specific_value( &ctr, val_index )) )
234 return False;
236 *val = dup_registry_value( v );
238 return True;
241 /***********************************************************************
242 High level access check for passing the required access mask to the
243 underlying registry backend
244 ***********************************************************************/
246 BOOL regkey_access_check( REGISTRY_KEY *key, uint32 requested, uint32 *granted, NT_USER_TOKEN *token )
248 /* use the default security check if the backend has not defined its own */
250 if ( !(key->hook && key->hook->ops && key->hook->ops->reg_access_check) ) {
251 SEC_DESC *sec_desc;
252 NTSTATUS status;
254 if ( !(sec_desc = construct_registry_sd( get_talloc_ctx() )) )
255 return False;
257 status = registry_access_check( sec_desc, token, requested, granted );
259 return NT_STATUS_IS_OK(status);
262 return key->hook->ops->reg_access_check( key->name, requested, granted, token );