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
},
41 { KEY_SHARES
, &shares_reg_ops
},
47 static struct generic_mapping reg_generic_map
=
48 { REG_KEY_READ
, REG_KEY_WRITE
, REG_KEY_EXECUTE
, REG_KEY_ALL
};
50 /********************************************************************
51 ********************************************************************/
53 static NTSTATUS
registry_access_check( SEC_DESC
*sec_desc
, NT_USER_TOKEN
*token
,
54 uint32 access_desired
, uint32
*access_granted
)
58 if ( geteuid() == sec_initial_uid() ) {
59 DEBUG(5,("registry_access_check: using root's token\n"));
60 token
= get_root_nt_token();
63 se_map_generic( &access_desired
, ®_generic_map
);
64 se_access_check( sec_desc
, token
, access_desired
, access_granted
, &result
);
69 /********************************************************************
70 ********************************************************************/
72 static SEC_DESC
* construct_registry_sd( TALLOC_CTX
*ctx
)
81 /* basic access for Everyone */
83 init_sec_access(&mask
, REG_KEY_READ
);
84 init_sec_ace(&ace
[i
++], &global_sid_World
, SEC_ACE_TYPE_ACCESS_ALLOWED
, mask
, 0);
86 /* Full Access 'BUILTIN\Administrators' */
88 init_sec_access(&mask
, REG_KEY_ALL
);
89 init_sec_ace(&ace
[i
++], &global_sid_Builtin_Administrators
, SEC_ACE_TYPE_ACCESS_ALLOWED
, mask
, 0);
92 /* create the security descriptor */
94 if ( !(acl
= make_sec_acl(ctx
, NT4_ACL_REVISION
, i
, ace
)) )
97 if ( !(sd
= make_sec_desc(ctx
, SEC_DESC_REVISION
, SEC_DESC_SELF_RELATIVE
, NULL
, NULL
, NULL
, acl
, &sd_size
)) )
104 /***********************************************************************
105 Open the registry database and initialize the REGISTRY_HOOK cache
106 ***********************************************************************/
108 BOOL
init_registry( void )
113 if ( !regdb_init() ) {
114 DEBUG(0,("init_registry: failed to initialize the registry tdb!\n"));
118 /* build the cache tree of registry hooks */
120 reghook_cache_init();
122 for ( i
=0; reg_hooks
[i
].keyname
; i
++ ) {
123 if ( !reghook_cache_add(®_hooks
[i
]) )
127 if ( DEBUGLEVEL
>= 20 )
128 reghook_dump_cache(20);
130 /* add any keys for other services */
133 eventlog_init_keys();
134 perfcount_init_keys();
136 /* close and let each smbd open up as necessary */
143 /***********************************************************************
144 High level wrapper function for storing registry subkeys
145 ***********************************************************************/
147 BOOL
store_reg_keys( REGISTRY_KEY
*key
, REGSUBKEY_CTR
*subkeys
)
149 if ( key
->hook
&& key
->hook
->ops
&& key
->hook
->ops
->store_subkeys
)
150 return key
->hook
->ops
->store_subkeys( key
->name
, subkeys
);
156 /***********************************************************************
157 High level wrapper function for storing registry values
158 ***********************************************************************/
160 BOOL
store_reg_values( REGISTRY_KEY
*key
, REGVAL_CTR
*val
)
162 if ( check_dynamic_reg_values( key
) )
165 if ( key
->hook
&& key
->hook
->ops
&& key
->hook
->ops
->store_values
)
166 return key
->hook
->ops
->store_values( key
->name
, val
);
172 /***********************************************************************
173 High level wrapper function for enumerating registry subkeys
174 Initialize the TALLOC_CTX if necessary
175 ***********************************************************************/
177 int fetch_reg_keys( REGISTRY_KEY
*key
, REGSUBKEY_CTR
*subkey_ctr
)
181 if ( key
->hook
&& key
->hook
->ops
&& key
->hook
->ops
->fetch_subkeys
)
182 result
= key
->hook
->ops
->fetch_subkeys( key
->name
, subkey_ctr
);
187 /***********************************************************************
188 retreive a specific subkey specified by index. Caller is
189 responsible for freeing memory
190 ***********************************************************************/
192 BOOL
fetch_reg_keys_specific( REGISTRY_KEY
*key
, char** subkey
, uint32 key_index
)
194 static REGSUBKEY_CTR
*ctr
= NULL
;
195 static pstring save_path
;
200 /* simple caching for performance; very basic heuristic */
202 DEBUG(8,("fetch_reg_keys_specific: Looking for key [%d] of [%s]\n", key_index
, key
->name
));
205 DEBUG(8,("fetch_reg_keys_specific: Initializing cache of subkeys for [%s]\n", key
->name
));
207 if ( !(ctr
= TALLOC_ZERO_P( NULL
, REGSUBKEY_CTR
)) ) {
208 DEBUG(0,("fetch_reg_keys_specific: talloc() failed!\n"));
212 pstrcpy( save_path
, key
->name
);
214 if ( fetch_reg_keys( key
, ctr
) == -1 )
218 /* clear the cache when key_index == 0 or the path has changed */
219 else if ( !key_index
|| StrCaseCmp( save_path
, key
->name
) ) {
221 DEBUG(8,("fetch_reg_keys_specific: Updating cache of subkeys for [%s]\n", key
->name
));
225 if ( !(ctr
= TALLOC_ZERO_P( NULL
, REGSUBKEY_CTR
)) ) {
226 DEBUG(0,("fetch_reg_keys_specific: talloc() failed!\n"));
230 pstrcpy( save_path
, key
->name
);
232 if ( fetch_reg_keys( key
, ctr
) == -1 )
236 if ( !(s
= regsubkey_ctr_specific_key( ctr
, key_index
)) )
239 *subkey
= SMB_STRDUP( s
);
244 /***********************************************************************
245 High level wrapper function for enumerating registry values
246 ***********************************************************************/
248 int fetch_reg_values( REGISTRY_KEY
*key
, REGVAL_CTR
*val
)
252 if ( key
->hook
&& key
->hook
->ops
&& key
->hook
->ops
->fetch_values
)
253 result
= key
->hook
->ops
->fetch_values( key
->name
, val
);
255 /* if the backend lookup returned no data, try the dynamic overlay */
258 result
= fetch_dynamic_reg_values( key
, val
);
260 return ( result
!= -1 ) ? result
: 0;
267 /***********************************************************************
268 retreive a specific subkey specified by index. Caller is
269 responsible for freeing memory
270 ***********************************************************************/
272 BOOL
fetch_reg_values_specific( REGISTRY_KEY
*key
, REGISTRY_VALUE
**val
, uint32 val_index
)
274 static REGVAL_CTR
*ctr
= NULL
;
275 static pstring save_path
;
280 /* simple caching for performance; very basic heuristic */
283 DEBUG(8,("fetch_reg_values_specific: Initializing cache of values for [%s]\n", key
->name
));
285 if ( !(ctr
= TALLOC_ZERO_P( NULL
, REGVAL_CTR
)) ) {
286 DEBUG(0,("fetch_reg_values_specific: talloc() failed!\n"));
290 pstrcpy( save_path
, key
->name
);
292 if ( fetch_reg_values( key
, ctr
) == -1 )
295 /* clear the cache when val_index == 0 or the path has changed */
296 else if ( !val_index
|| !strequal(save_path
, key
->name
) ) {
298 DEBUG(8,("fetch_reg_values_specific: Updating cache of values for [%s]\n", key
->name
));
302 if ( !(ctr
= TALLOC_ZERO_P( NULL
, REGVAL_CTR
)) ) {
303 DEBUG(0,("fetch_reg_values_specific: talloc() failed!\n"));
307 pstrcpy( save_path
, key
->name
);
309 if ( fetch_reg_values( key
, ctr
) == -1 )
313 if ( !(v
= regval_ctr_specific_value( ctr
, val_index
)) )
316 *val
= dup_registry_value( v
);
321 /***********************************************************************
322 High level access check for passing the required access mask to the
323 underlying registry backend
324 ***********************************************************************/
326 BOOL
regkey_access_check( REGISTRY_KEY
*key
, uint32 requested
, uint32
*granted
, NT_USER_TOKEN
*token
)
328 /* use the default security check if the backend has not defined its own */
330 if ( !(key
->hook
&& key
->hook
->ops
&& key
->hook
->ops
->reg_access_check
) ) {
334 if ( !(sec_desc
= construct_registry_sd( get_talloc_ctx() )) )
337 status
= registry_access_check( sec_desc
, token
, requested
, granted
);
339 return NT_STATUS_IS_OK(status
);
342 return key
->hook
->ops
->reg_access_check( key
->name
, requested
, granted
, token
);
345 /***********************************************************************
346 ***********************************************************************/
348 WERROR
regkey_open_internal( REGISTRY_KEY
**regkey
, const char *path
,
349 NT_USER_TOKEN
*token
, uint32 access_desired
)
351 WERROR result
= WERR_OK
;
352 REGISTRY_KEY
*keyinfo
;
353 REGSUBKEY_CTR
*subkeys
= NULL
;
354 uint32 access_granted
;
356 if ( !(W_ERROR_IS_OK(result
= regdb_open()) ) )
359 DEBUG(7,("regkey_open_internal: name = [%s]\n", path
));
361 if ( !(*regkey
= TALLOC_ZERO_P(NULL
, REGISTRY_KEY
)) ) {
370 keyinfo
->type
= REG_KEY_GENERIC
;
371 if (!(keyinfo
->name
= talloc_strdup(keyinfo
, path
))) {
376 /* Tag this as a Performance Counter Key */
378 if( StrnCaseCmp(path
, KEY_HKPD
, strlen(KEY_HKPD
)) == 0 )
379 keyinfo
->type
= REG_KEY_HKPD
;
381 /* Look up the table of registry I/O operations */
383 if ( !(keyinfo
->hook
= reghook_cache_find( keyinfo
->name
)) ) {
384 DEBUG(0,("open_registry_key: Failed to assigned a REGISTRY_HOOK to [%s]\n",
386 result
= WERR_BADFILE
;
390 /* check if the path really exists; failed is indicated by -1 */
391 /* if the subkey count failed, bail out */
393 if ( !(subkeys
= TALLOC_ZERO_P( keyinfo
, REGSUBKEY_CTR
)) ) {
398 if ( fetch_reg_keys( keyinfo
, subkeys
) == -1 ) {
399 result
= WERR_BADFILE
;
403 TALLOC_FREE( subkeys
);
405 if ( !regkey_access_check( keyinfo
, access_desired
, &access_granted
, token
) ) {
406 result
= WERR_ACCESS_DENIED
;
410 keyinfo
->access_granted
= access_granted
;
413 if ( !W_ERROR_IS_OK(result
) ) {
414 regkey_close_internal( *regkey
);
420 /*******************************************************************
421 *******************************************************************/
423 WERROR
regkey_close_internal( REGISTRY_KEY
*key
)