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 /**********************************************************************
30 Note that the REGSUB_CTR and REGVAL_CTR objects *must* be talloc()'d
31 since the methods use the object pointer as the talloc context for
32 internal private data.
34 There is no longer a regXXX_ctr_intit() and regXXX_ctr_destroy()
35 pair of functions. Simply TALLOC_ZERO_P() and TALLOC_FREE() the
38 **********************************************************************/
40 /***********************************************************************
41 Add a new key to the array
42 **********************************************************************/
44 WERROR regsubkey_ctr_addkey( REGSUBKEY_CTR *ctr, const char *keyname )
52 /* make sure the keyname is not already there */
54 if ( regsubkey_ctr_key_exists( ctr, keyname ) ) {
58 if (!(newkeys = TALLOC_REALLOC_ARRAY(ctr, ctr->subkeys, char *,
59 ctr->num_subkeys+1))) {
63 ctr->subkeys = newkeys;
65 if (!(ctr->subkeys[ctr->num_subkeys] = talloc_strdup(ctr->subkeys,
68 * Don't shrink the new array again, this wastes a pointer
77 /***********************************************************************
78 Add a new key to the array
79 **********************************************************************/
81 int regsubkey_ctr_delkey( REGSUBKEY_CTR *ctr, const char *keyname )
86 return ctr->num_subkeys;
88 /* make sure the keyname is actually already there */
90 for ( i=0; i<ctr->num_subkeys; i++ ) {
91 if ( strequal( ctr->subkeys[i], keyname ) )
95 if ( i == ctr->num_subkeys )
96 return ctr->num_subkeys;
98 /* update if we have any keys left */
100 if ( i < ctr->num_subkeys )
101 memmove( &ctr->subkeys[i], &ctr->subkeys[i+1], sizeof(char*) * (ctr->num_subkeys-i) );
103 return ctr->num_subkeys;
106 /***********************************************************************
107 Check for the existance of a key
108 **********************************************************************/
110 BOOL regsubkey_ctr_key_exists( REGSUBKEY_CTR *ctr, const char *keyname )
118 for ( i=0; i<ctr->num_subkeys; i++ ) {
119 if ( strequal( ctr->subkeys[i],keyname ) )
126 /***********************************************************************
127 How many keys does the container hold ?
128 **********************************************************************/
130 int regsubkey_ctr_numkeys( REGSUBKEY_CTR *ctr )
132 return ctr->num_subkeys;
135 /***********************************************************************
136 Retreive a specific key string
137 **********************************************************************/
139 char* regsubkey_ctr_specific_key( REGSUBKEY_CTR *ctr, uint32 key_index )
141 if ( ! (key_index < ctr->num_subkeys) )
144 return ctr->subkeys[key_index];
148 * Utility functions for REGVAL_CTR
151 /***********************************************************************
152 How many keys does the container hold ?
153 **********************************************************************/
155 int regval_ctr_numvals( REGVAL_CTR *ctr )
157 return ctr->num_values;
160 /***********************************************************************
161 allocate memory for and duplicate a REGISTRY_VALUE.
162 This is malloc'd memory so the caller should free it when done
163 **********************************************************************/
165 REGISTRY_VALUE* dup_registry_value( REGISTRY_VALUE *val )
167 REGISTRY_VALUE *copy = NULL;
172 if ( !(copy = SMB_MALLOC_P( REGISTRY_VALUE)) ) {
173 DEBUG(0,("dup_registry_value: malloc() failed!\n"));
177 /* copy all the non-pointer initial data */
179 memcpy( copy, val, sizeof(REGISTRY_VALUE) );
184 if ( val->data_p && val->size )
186 if ( !(copy->data_p = (uint8 *)memdup( val->data_p,
188 DEBUG(0,("dup_registry_value: memdup() failed for [%d] bytes!\n",
193 copy->size = val->size;
199 /**********************************************************************
200 free the memory allocated to a REGISTRY_VALUE
201 *********************************************************************/
203 void free_registry_value( REGISTRY_VALUE *val )
208 SAFE_FREE( val->data_p );
214 /**********************************************************************
215 *********************************************************************/
217 uint8* regval_data_p( REGISTRY_VALUE *val )
222 /**********************************************************************
223 *********************************************************************/
225 uint32 regval_size( REGISTRY_VALUE *val )
230 /**********************************************************************
231 *********************************************************************/
233 char* regval_name( REGISTRY_VALUE *val )
235 return val->valuename;
238 /**********************************************************************
239 *********************************************************************/
241 uint32 regval_type( REGISTRY_VALUE *val )
246 /***********************************************************************
247 Retreive a pointer to a specific value. Caller shoud dup the structure
248 since this memory will go away when the ctr is free()'d
249 **********************************************************************/
251 REGISTRY_VALUE* regval_ctr_specific_value( REGVAL_CTR *ctr, uint32 idx )
253 if ( !(idx < ctr->num_values) )
256 return ctr->values[idx];
259 /***********************************************************************
260 Check for the existance of a value
261 **********************************************************************/
263 BOOL regval_ctr_key_exists( REGVAL_CTR *ctr, const char *value )
267 for ( i=0; i<ctr->num_values; i++ ) {
268 if ( strequal( ctr->values[i]->valuename, value) )
274 /***********************************************************************
275 Add a new registry value to the array
276 **********************************************************************/
278 int regval_ctr_addvalue( REGVAL_CTR *ctr, const char *name, uint16 type,
279 const char *data_p, size_t size )
282 return ctr->num_values;
284 /* Delete the current value (if it exists) and add the new one */
286 regval_ctr_delvalue( ctr, name );
288 /* allocate a slot in the array of pointers */
290 if ( ctr->num_values == 0 ) {
291 ctr->values = TALLOC_P( ctr, REGISTRY_VALUE *);
293 ctr->values = TALLOC_REALLOC_ARRAY( ctr, ctr->values, REGISTRY_VALUE *, ctr->num_values+1 );
301 /* allocate a new value and store the pointer in the arrya */
303 ctr->values[ctr->num_values] = TALLOC_P( ctr, REGISTRY_VALUE);
304 if (!ctr->values[ctr->num_values]) {
311 fstrcpy( ctr->values[ctr->num_values]->valuename, name );
312 ctr->values[ctr->num_values]->type = type;
313 ctr->values[ctr->num_values]->data_p = (uint8 *)TALLOC_MEMDUP(
315 ctr->values[ctr->num_values]->size = size;
318 return ctr->num_values;
321 /***********************************************************************
322 Add a new registry value to the array
323 **********************************************************************/
325 int regval_ctr_copyvalue( REGVAL_CTR *ctr, REGISTRY_VALUE *val )
328 /* allocate a slot in the array of pointers */
330 if ( ctr->num_values == 0 ) {
331 ctr->values = TALLOC_P( ctr, REGISTRY_VALUE *);
333 ctr->values = TALLOC_REALLOC_ARRAY( ctr, ctr->values, REGISTRY_VALUE *, ctr->num_values+1 );
341 /* allocate a new value and store the pointer in the arrya */
343 ctr->values[ctr->num_values] = TALLOC_P( ctr, REGISTRY_VALUE);
344 if (!ctr->values[ctr->num_values]) {
351 fstrcpy( ctr->values[ctr->num_values]->valuename, val->valuename );
352 ctr->values[ctr->num_values]->type = val->type;
353 ctr->values[ctr->num_values]->data_p = (uint8 *)TALLOC_MEMDUP(
354 ctr, val->data_p, val->size );
355 ctr->values[ctr->num_values]->size = val->size;
359 return ctr->num_values;
362 /***********************************************************************
363 Delete a single value from the registry container.
364 No need to free memory since it is talloc'd.
365 **********************************************************************/
367 int regval_ctr_delvalue( REGVAL_CTR *ctr, const char *name )
371 for ( i=0; i<ctr->num_values; i++ ) {
372 if ( strequal( ctr->values[i]->valuename, name ) )
376 /* just return if we don't find it */
378 if ( i == ctr->num_values )
379 return ctr->num_values;
381 /* If 'i' was not the last element, just shift everything down one */
383 if ( i < ctr->num_values )
384 memmove( &ctr->values[i], &ctr->values[i+1], sizeof(REGISTRY_VALUE*)*(ctr->num_values-i) );
386 return ctr->num_values;
389 /***********************************************************************
390 Retrieve single value from the registry container.
391 No need to free memory since it is talloc'd.
392 **********************************************************************/
394 REGISTRY_VALUE* regval_ctr_getvalue( REGVAL_CTR *ctr, const char *name )
398 /* search for the value */
400 for ( i=0; i<ctr->num_values; i++ ) {
401 if ( strequal( ctr->values[i]->valuename, name ) )
402 return ctr->values[i];
408 /***********************************************************************
409 return the data_p as a uint32
410 **********************************************************************/
412 uint32 regval_dword( REGISTRY_VALUE *val )
416 data = IVAL( regval_data_p(val), 0 );
421 /***********************************************************************
422 return the data_p as a character string
423 **********************************************************************/
425 char* regval_sz( REGISTRY_VALUE *val )
429 rpcstr_pull( data, regval_data_p(val), sizeof(data), regval_size(val), 0 );