r9742: merging reg_objects and NT_PRINTER_DATA changes from SAMBA_3_0
[Samba.git] / source / registry / reg_frontend.c
blob80ef3e355885107dea3c93c8116afd888ce6a658
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 #if 0
42 { KEY_EVENTLOG, &eventlog_ops },
43 #endif
44 { KEY_SHARES, &shares_reg_ops },
45 #endif
46 { NULL, NULL }
50 /***********************************************************************
51 Open the registry database and initialize the REGISTRY_HOOK cache
52 ***********************************************************************/
54 BOOL init_registry( void )
56 int i;
58 if ( !init_registry_db() ) {
59 DEBUG(0,("init_registry: failed to initialize the registry tdb!\n"));
60 return False;
62 /* initialize eventlog related "registry entries" */
63 init_eventlog_parameters();
64 /* inform the external eventlog machinery of the change */
65 eventlog_refresh_external_parameters();
67 /* build the cache tree of registry hooks */
69 reghook_cache_init();
71 for ( i=0; reg_hooks[i].keyname; i++ ) {
72 if ( !reghook_cache_add(&reg_hooks[i]) )
73 return False;
76 if ( DEBUGLEVEL >= 20 )
77 reghook_dump_cache(20);
79 return True;
82 /***********************************************************************
83 High level wrapper function for storing registry subkeys
84 ***********************************************************************/
86 BOOL store_reg_keys( REGISTRY_KEY *key, REGSUBKEY_CTR *subkeys )
88 if ( key->hook && key->hook->ops && key->hook->ops->store_subkeys )
89 return key->hook->ops->store_subkeys( key->name, subkeys );
91 return False;
95 /***********************************************************************
96 High level wrapper function for storing registry values
97 ***********************************************************************/
99 BOOL store_reg_values( REGISTRY_KEY *key, REGVAL_CTR *val )
101 if ( check_dynamic_reg_values( key ) )
102 return False;
104 if ( key->hook && key->hook->ops && key->hook->ops->store_values )
105 return key->hook->ops->store_values( key->name, val );
107 return False;
111 /***********************************************************************
112 High level wrapper function for enumerating registry subkeys
113 Initialize the TALLOC_CTX if necessary
114 ***********************************************************************/
116 int fetch_reg_keys( REGISTRY_KEY *key, REGSUBKEY_CTR *subkey_ctr )
118 int result = -1;
120 if ( key->hook && key->hook->ops && key->hook->ops->fetch_subkeys )
121 result = key->hook->ops->fetch_subkeys( key->name, subkey_ctr );
123 return result;
126 /***********************************************************************
127 retreive a specific subkey specified by index. Caller is
128 responsible for freeing memory
129 ***********************************************************************/
131 BOOL fetch_reg_keys_specific( REGISTRY_KEY *key, char** subkey, uint32 key_index )
133 static REGSUBKEY_CTR *ctr = NULL;
134 static pstring save_path;
135 char *s;
137 *subkey = NULL;
139 /* simple caching for performance; very basic heuristic */
141 DEBUG(8,("fetch_reg_keys_specific: Looking for key [%d] of [%s]\n", key_index, key->name));
143 if ( !ctr ) {
144 DEBUG(8,("fetch_reg_keys_specific: Initializing cache of subkeys for [%s]\n", key->name));
146 if ( !(ctr = TALLOC_ZERO_P( NULL, REGSUBKEY_CTR )) ) {
147 DEBUG(0,("fetch_reg_keys_specific: talloc() failed!\n"));
148 return False;
151 pstrcpy( save_path, key->name );
153 if ( fetch_reg_keys( key, ctr) == -1 )
154 return False;
157 /* clear the cache when key_index == 0 or the path has changed */
158 else if ( !key_index || StrCaseCmp( save_path, key->name) ) {
160 DEBUG(8,("fetch_reg_keys_specific: Updating cache of subkeys for [%s]\n", key->name));
162 TALLOC_FREE( ctr );
164 if ( !(ctr = TALLOC_ZERO_P( NULL, REGSUBKEY_CTR )) ) {
165 DEBUG(0,("fetch_reg_keys_specific: talloc() failed!\n"));
166 return False;
169 pstrcpy( save_path, key->name );
171 if ( fetch_reg_keys( key, ctr) == -1 )
172 return False;
175 if ( !(s = regsubkey_ctr_specific_key( ctr, key_index )) )
176 return False;
178 *subkey = SMB_STRDUP( s );
180 return True;
183 /***********************************************************************
184 High level wrapper function for enumerating registry values
185 ***********************************************************************/
187 int fetch_reg_values( REGISTRY_KEY *key, REGVAL_CTR *val )
189 int result = -1;
191 if ( key->hook && key->hook->ops && key->hook->ops->fetch_values )
192 result = key->hook->ops->fetch_values( key->name, val );
194 /* if the backend lookup returned no data, try the dynamic overlay */
196 if ( result == 0 ) {
197 result = fetch_dynamic_reg_values( key, val );
199 return ( result != -1 ) ? result : 0;
202 return result;
206 /***********************************************************************
207 retreive a specific subkey specified by index. Caller is
208 responsible for freeing memory
209 ***********************************************************************/
211 BOOL fetch_reg_values_specific( REGISTRY_KEY *key, REGISTRY_VALUE **val, uint32 val_index )
213 static REGVAL_CTR *ctr = NULL;
214 static pstring save_path;
215 REGISTRY_VALUE *v;
217 *val = NULL;
219 /* simple caching for performance; very basic heuristic */
221 if ( !ctr ) {
222 DEBUG(8,("fetch_reg_values_specific: Initializing cache of values for [%s]\n", key->name));
224 if ( !(ctr = TALLOC_ZERO_P( NULL, REGVAL_CTR )) ) {
225 DEBUG(0,("fetch_reg_values_specific: talloc() failed!\n"));
226 return False;
229 pstrcpy( save_path, key->name );
231 if ( fetch_reg_values( key, ctr) == -1 )
232 return False;
234 /* clear the cache when val_index == 0 or the path has changed */
235 else if ( !val_index || !strequal(save_path, key->name) ) {
237 DEBUG(8,("fetch_reg_values_specific: Updating cache of values for [%s]\n", key->name));
239 TALLOC_FREE( ctr );
241 if ( !(ctr = TALLOC_ZERO_P( NULL, REGVAL_CTR )) ) {
242 DEBUG(0,("fetch_reg_values_specific: talloc() failed!\n"));
243 return False;
246 pstrcpy( save_path, key->name );
248 if ( fetch_reg_values( key, ctr) == -1 )
249 return False;
252 if ( !(v = regval_ctr_specific_value( ctr, val_index )) )
253 return False;
255 *val = dup_registry_value( v );
257 return True;
260 /***********************************************************************
261 High level access check for passing the required access mask to the
262 underlying registry backend
263 ***********************************************************************/
265 BOOL regkey_access_check( REGISTRY_KEY *key, uint32 requested, uint32 *granted, NT_USER_TOKEN *token )
267 /* use the default security check if the backend has not defined its own */
269 if ( !(key->hook && key->hook->ops && key->hook->ops->reg_access_check) ) {
270 SEC_DESC *sec_desc;
271 NTSTATUS status;
273 if ( !(sec_desc = construct_registry_sd( get_talloc_ctx() )) )
274 return False;
276 status = registry_access_check( sec_desc, token, requested, granted );
278 return NT_STATUS_IS_OK(status);
281 return key->hook->ops->reg_access_check( key->name, requested, granted, token );