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
29 /***********************************************************************
30 Init the talloc context held by a REGSUBKEY_CTR structure
31 This now zero's the structure
32 **********************************************************************/
34 void regsubkey_ctr_init( REGSUBKEY_CTR *ctr )
37 ctr->ctx = talloc_init("regsubkey_ctr_init for ctr %p", ctr);
40 /***********************************************************************
41 Add a new key to the array
42 **********************************************************************/
44 int regsubkey_ctr_addkey( REGSUBKEY_CTR *ctr, const char *keyname )
49 return ctr->num_subkeys;
51 /* make sure the keyname is not already there */
53 if ( regsubkey_ctr_key_exists( ctr, keyname ) )
54 return ctr->num_subkeys;
56 /* allocate a space for the char* in the array */
58 if ( ctr->subkeys == 0 )
59 ctr->subkeys = TALLOC_P( ctr->ctx, char *);
61 pp = TALLOC_REALLOC_ARRAY( ctr->ctx, ctr->subkeys, char *, ctr->num_subkeys+1);
66 /* allocate the string and save it in the array */
68 ctr->subkeys[ctr->num_subkeys] = talloc_strdup( ctr->ctx, keyname );
71 return ctr->num_subkeys;
74 /***********************************************************************
75 Add a new key to the array
76 **********************************************************************/
78 int regsubkey_ctr_delkey( REGSUBKEY_CTR *ctr, const char *keyname )
83 return ctr->num_subkeys;
85 /* make sure the keyname is actually already there */
87 for ( i=0; i<ctr->num_subkeys; i++ ) {
88 if ( strequal( ctr->subkeys[i], keyname ) )
92 if ( i == ctr->num_subkeys )
93 return ctr->num_subkeys;
95 /* update if we have any keys left */
97 if ( i < ctr->num_subkeys )
98 memmove( &ctr->subkeys[i], &ctr->subkeys[i+1], sizeof(char*) * (ctr->num_subkeys-i) );
100 return ctr->num_subkeys;
103 /***********************************************************************
104 Check for the existance of a key
105 **********************************************************************/
107 BOOL regsubkey_ctr_key_exists( REGSUBKEY_CTR *ctr, const char *keyname )
111 for ( i=0; i<ctr->num_subkeys; i++ ) {
112 if ( strequal( ctr->subkeys[i],keyname ) )
119 /***********************************************************************
120 How many keys does the container hold ?
121 **********************************************************************/
123 int regsubkey_ctr_numkeys( REGSUBKEY_CTR *ctr )
125 return ctr->num_subkeys;
128 /***********************************************************************
129 Retreive a specific key string
130 **********************************************************************/
132 char* regsubkey_ctr_specific_key( REGSUBKEY_CTR *ctr, uint32 key_index )
134 if ( ! (key_index < ctr->num_subkeys) )
137 return ctr->subkeys[key_index];
140 /***********************************************************************
141 free memory held by a REGSUBKEY_CTR structure
142 **********************************************************************/
144 void regsubkey_ctr_destroy( REGSUBKEY_CTR *ctr )
147 talloc_destroy( ctr->ctx );
154 * Utility functions for REGVAL_CTR
157 /***********************************************************************
158 Init the talloc context held by a REGSUBKEY_CTR structure
159 This now zero's the structure
160 **********************************************************************/
162 void regval_ctr_init( REGVAL_CTR *ctr )
165 ctr->ctx = talloc_init("regval_ctr_init for ctr %p", ctr);
168 /***********************************************************************
169 How many keys does the container hold ?
170 **********************************************************************/
172 int regval_ctr_numvals( REGVAL_CTR *ctr )
174 return ctr->num_values;
177 /***********************************************************************
178 allocate memory for and duplicate a REGISTRY_VALUE.
179 This is malloc'd memory so the caller should free it when done
180 **********************************************************************/
182 REGISTRY_VALUE* dup_registry_value( REGISTRY_VALUE *val )
184 REGISTRY_VALUE *copy = NULL;
189 if ( !(copy = SMB_MALLOC_P( REGISTRY_VALUE)) ) {
190 DEBUG(0,("dup_registry_value: malloc() failed!\n"));
194 /* copy all the non-pointer initial data */
196 memcpy( copy, val, sizeof(REGISTRY_VALUE) );
199 if ( !(copy->data_p = memdup( val->data_p, val->size )) ) {
200 DEBUG(0,("dup_registry_value: memdup() failed for [%d] bytes!\n",
209 /**********************************************************************
210 free the memory allocated to a REGISTRY_VALUE
211 *********************************************************************/
213 void free_registry_value( REGISTRY_VALUE *val )
218 SAFE_FREE( val->data_p );
224 /**********************************************************************
225 *********************************************************************/
227 uint8* regval_data_p( REGISTRY_VALUE *val )
232 /**********************************************************************
233 *********************************************************************/
235 uint32 regval_size( REGISTRY_VALUE *val )
240 /**********************************************************************
241 *********************************************************************/
243 char* regval_name( REGISTRY_VALUE *val )
245 return val->valuename;
248 /**********************************************************************
249 *********************************************************************/
251 uint32 regval_type( REGISTRY_VALUE *val )
256 /***********************************************************************
257 Retreive a pointer to a specific value. Caller shoud dup the structure
258 since this memory may go away with a regval_ctr_destroy()
259 **********************************************************************/
261 REGISTRY_VALUE* regval_ctr_specific_value( REGVAL_CTR *ctr, uint32 idx )
263 if ( !(idx < ctr->num_values) )
266 return ctr->values[idx];
269 /***********************************************************************
270 Retrive the TALLOC_CTX associated with a REGISTRY_VALUE
271 **********************************************************************/
273 TALLOC_CTX* regval_ctr_getctx( REGVAL_CTR *val )
280 /***********************************************************************
281 Check for the existance of a value
282 **********************************************************************/
284 BOOL regval_ctr_key_exists( REGVAL_CTR *ctr, const char *value )
288 for ( i=0; i<ctr->num_values; i++ ) {
289 if ( strequal( ctr->values[i]->valuename, value) )
295 /***********************************************************************
296 Add a new registry value to the array
297 **********************************************************************/
299 int regval_ctr_addvalue( REGVAL_CTR *ctr, const char *name, uint16 type,
300 const char *data_p, size_t size )
302 REGISTRY_VALUE **ppreg;
305 return ctr->num_values;
307 /* Delete the current value (if it exists) and add the new one */
309 regval_ctr_delvalue( ctr, name );
311 /* allocate a slot in the array of pointers */
313 if ( ctr->num_values == 0 )
314 ctr->values = TALLOC_P( ctr->ctx, REGISTRY_VALUE *);
316 ppreg = TALLOC_REALLOC_ARRAY( ctr->ctx, ctr->values, REGISTRY_VALUE *, ctr->num_values+1 );
321 /* allocate a new value and store the pointer in the arrya */
323 ctr->values[ctr->num_values] = TALLOC_P( ctr->ctx, REGISTRY_VALUE);
327 fstrcpy( ctr->values[ctr->num_values]->valuename, name );
328 ctr->values[ctr->num_values]->type = type;
329 ctr->values[ctr->num_values]->data_p = TALLOC_MEMDUP( ctr->ctx, data_p, size );
330 ctr->values[ctr->num_values]->size = size;
333 return ctr->num_values;
336 /***********************************************************************
337 Add a new registry value to the array
338 **********************************************************************/
340 int regval_ctr_copyvalue( REGVAL_CTR *ctr, REGISTRY_VALUE *val )
342 REGISTRY_VALUE **ppreg;
346 /* allocate a slot in the array of pointers */
348 if ( ctr->num_values == 0 )
349 ctr->values = TALLOC_P( ctr->ctx, REGISTRY_VALUE *);
351 ppreg = TALLOC_REALLOC_ARRAY( ctr->ctx, ctr->values, REGISTRY_VALUE *, ctr->num_values+1 );
356 /* allocate a new value and store the pointer in the arrya */
358 ctr->values[ctr->num_values] = TALLOC_P( ctr->ctx, REGISTRY_VALUE);
362 fstrcpy( ctr->values[ctr->num_values]->valuename, val->valuename );
363 ctr->values[ctr->num_values]->type = val->type;
364 ctr->values[ctr->num_values]->data_p = TALLOC_MEMDUP( ctr->ctx, val->data_p, val->size );
365 ctr->values[ctr->num_values]->size = val->size;
369 return ctr->num_values;
372 /***********************************************************************
373 Delete a single value from the registry container.
374 No need to free memory since it is talloc'd.
375 **********************************************************************/
377 int regval_ctr_delvalue( REGVAL_CTR *ctr, const char *name )
381 for ( i=0; i<ctr->num_values; i++ ) {
382 if ( strequal( ctr->values[i]->valuename, name ) )
386 /* just return if we don't find it */
388 if ( i == ctr->num_values )
389 return ctr->num_values;
391 /* If 'i' was not the last element, just shift everything down one */
393 if ( i < ctr->num_values )
394 memmove( &ctr->values[i], &ctr->values[i+1], sizeof(REGISTRY_VALUE*)*(ctr->num_values-i) );
396 return ctr->num_values;
399 /***********************************************************************
400 Retrieve single value from the registry container.
401 No need to free memory since it is talloc'd.
402 **********************************************************************/
404 REGISTRY_VALUE* regval_ctr_getvalue( REGVAL_CTR *ctr, const char *name )
408 /* search for the value */
410 for ( i=0; i<ctr->num_values; i++ ) {
411 if ( strequal( ctr->values[i]->valuename, name ) )
412 return ctr->values[i];
418 /***********************************************************************
419 free memory held by a REGVAL_CTR structure
420 **********************************************************************/
422 void regval_ctr_destroy( REGVAL_CTR *ctr )
425 talloc_destroy( ctr->ctx );