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 3 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, see <http://www.gnu.org/licenses/>.
20 /* Implementation of registry frontend view functions. */
25 #define DBGC_CLASS DBGC_RPC_SRV
27 /**********************************************************************
29 Note that the REGSUB_CTR and REGVAL_CTR objects *must* be talloc()'d
30 since the methods use the object pointer as the talloc context for
31 internal private data.
33 There is no longer a regXXX_ctr_intit() and regXXX_ctr_destroy()
34 pair of functions. Simply TALLOC_ZERO_P() and TALLOC_FREE() the
37 **********************************************************************/
39 /***********************************************************************
40 Add a new key to the array
41 **********************************************************************/
43 WERROR regsubkey_ctr_addkey( REGSUBKEY_CTR *ctr, const char *keyname )
51 /* make sure the keyname is not already there */
53 if ( regsubkey_ctr_key_exists( ctr, keyname ) ) {
57 if (!(newkeys = TALLOC_REALLOC_ARRAY(ctr, ctr->subkeys, char *,
58 ctr->num_subkeys+1))) {
62 ctr->subkeys = newkeys;
64 if (!(ctr->subkeys[ctr->num_subkeys] = talloc_strdup(ctr->subkeys,
67 * Don't shrink the new array again, this wastes a pointer
76 /***********************************************************************
77 Delete a key from the array
78 **********************************************************************/
80 int regsubkey_ctr_delkey( REGSUBKEY_CTR *ctr, const char *keyname )
85 return ctr->num_subkeys;
87 /* make sure the keyname is actually already there */
89 for ( i=0; i<ctr->num_subkeys; i++ ) {
90 if ( strequal( ctr->subkeys[i], keyname ) )
94 if ( i == ctr->num_subkeys )
95 return ctr->num_subkeys;
97 /* update if we have any keys left */
99 if ( i < ctr->num_subkeys )
100 memmove( &ctr->subkeys[i], &ctr->subkeys[i+1], sizeof(char*) * (ctr->num_subkeys-i) );
102 return ctr->num_subkeys;
105 /***********************************************************************
106 Check for the existance of a key
107 **********************************************************************/
109 BOOL regsubkey_ctr_key_exists( REGSUBKEY_CTR *ctr, const char *keyname )
117 for ( i=0; i<ctr->num_subkeys; i++ ) {
118 if ( strequal( ctr->subkeys[i],keyname ) )
125 /***********************************************************************
126 How many keys does the container hold ?
127 **********************************************************************/
129 int regsubkey_ctr_numkeys( REGSUBKEY_CTR *ctr )
131 return ctr->num_subkeys;
134 /***********************************************************************
135 Retreive a specific key string
136 **********************************************************************/
138 char* regsubkey_ctr_specific_key( REGSUBKEY_CTR *ctr, uint32 key_index )
140 if ( ! (key_index < ctr->num_subkeys) )
143 return ctr->subkeys[key_index];
147 * Utility functions for REGVAL_CTR
150 /***********************************************************************
151 How many keys does the container hold ?
152 **********************************************************************/
154 int regval_ctr_numvals( REGVAL_CTR *ctr )
156 return ctr->num_values;
159 /***********************************************************************
160 allocate memory for and duplicate a REGISTRY_VALUE.
161 This is malloc'd memory so the caller should free it when done
162 **********************************************************************/
164 REGISTRY_VALUE* dup_registry_value( REGISTRY_VALUE *val )
166 REGISTRY_VALUE *copy = NULL;
171 if ( !(copy = SMB_MALLOC_P( REGISTRY_VALUE)) ) {
172 DEBUG(0,("dup_registry_value: malloc() failed!\n"));
176 /* copy all the non-pointer initial data */
178 memcpy( copy, val, sizeof(REGISTRY_VALUE) );
183 if ( val->data_p && val->size )
185 if ( !(copy->data_p = (uint8 *)memdup( val->data_p,
187 DEBUG(0,("dup_registry_value: memdup() failed for [%d] bytes!\n",
192 copy->size = val->size;
198 /**********************************************************************
199 free the memory allocated to a REGISTRY_VALUE
200 *********************************************************************/
202 void free_registry_value( REGISTRY_VALUE *val )
207 SAFE_FREE( val->data_p );
213 /**********************************************************************
214 *********************************************************************/
216 uint8* regval_data_p( REGISTRY_VALUE *val )
221 /**********************************************************************
222 *********************************************************************/
224 uint32 regval_size( REGISTRY_VALUE *val )
229 /**********************************************************************
230 *********************************************************************/
232 char* regval_name( REGISTRY_VALUE *val )
234 return val->valuename;
237 /**********************************************************************
238 *********************************************************************/
240 uint32 regval_type( REGISTRY_VALUE *val )
245 /***********************************************************************
246 Retreive a pointer to a specific value. Caller shoud dup the structure
247 since this memory will go away when the ctr is free()'d
248 **********************************************************************/
250 REGISTRY_VALUE* regval_ctr_specific_value( REGVAL_CTR *ctr, uint32 idx )
252 if ( !(idx < ctr->num_values) )
255 return ctr->values[idx];
258 /***********************************************************************
259 Check for the existance of a value
260 **********************************************************************/
262 BOOL regval_ctr_key_exists( REGVAL_CTR *ctr, const char *value )
266 for ( i=0; i<ctr->num_values; i++ ) {
267 if ( strequal( ctr->values[i]->valuename, value) )
274 /***********************************************************************
275 * compose a REGISTRY_VALUE from input data
276 **********************************************************************/
278 REGISTRY_VALUE *regval_compose(TALLOC_CTX *ctx, const char *name, uint16 type,
279 const char *data_p, size_t size)
281 REGISTRY_VALUE *regval = TALLOC_P(ctx, REGISTRY_VALUE);
283 if (regval == NULL) {
287 fstrcpy(regval->valuename, name);
290 regval->data_p = (uint8 *)TALLOC_MEMDUP(regval, data_p, size);
291 if (!regval->data_p) {
296 regval->data_p = NULL;
303 /***********************************************************************
304 Add a new registry value to the array
305 **********************************************************************/
307 int regval_ctr_addvalue( REGVAL_CTR *ctr, const char *name, uint16 type,
308 const char *data_p, size_t size )
311 return ctr->num_values;
313 /* Delete the current value (if it exists) and add the new one */
315 regval_ctr_delvalue( ctr, name );
317 /* allocate a slot in the array of pointers */
319 if ( ctr->num_values == 0 ) {
320 ctr->values = TALLOC_P( ctr, REGISTRY_VALUE *);
322 ctr->values = TALLOC_REALLOC_ARRAY( ctr, ctr->values, REGISTRY_VALUE *, ctr->num_values+1 );
330 /* allocate a new value and store the pointer in the arrya */
332 ctr->values[ctr->num_values] = TALLOC_P( ctr, REGISTRY_VALUE);
333 if (!ctr->values[ctr->num_values]) {
340 fstrcpy( ctr->values[ctr->num_values]->valuename, name );
341 ctr->values[ctr->num_values]->type = type;
343 ctr->values[ctr->num_values]->data_p = (uint8 *)TALLOC_MEMDUP(
345 if (!ctr->values[ctr->num_values]->data_p) {
350 ctr->values[ctr->num_values]->data_p = NULL;
352 ctr->values[ctr->num_values]->size = size;
355 return ctr->num_values;
358 /***********************************************************************
359 Add a new registry value to the array
360 **********************************************************************/
362 int regval_ctr_copyvalue( REGVAL_CTR *ctr, REGISTRY_VALUE *val )
365 /* allocate a slot in the array of pointers */
367 if ( ctr->num_values == 0 ) {
368 ctr->values = TALLOC_P( ctr, REGISTRY_VALUE *);
370 ctr->values = TALLOC_REALLOC_ARRAY( ctr, ctr->values, REGISTRY_VALUE *, ctr->num_values+1 );
378 /* allocate a new value and store the pointer in the arrya */
380 ctr->values[ctr->num_values] = TALLOC_P( ctr, REGISTRY_VALUE);
381 if (!ctr->values[ctr->num_values]) {
388 fstrcpy( ctr->values[ctr->num_values]->valuename, val->valuename );
389 ctr->values[ctr->num_values]->type = val->type;
391 ctr->values[ctr->num_values]->data_p = (uint8 *)TALLOC_MEMDUP(
392 ctr, val->data_p, val->size );
393 if (!ctr->values[ctr->num_values]->data_p) {
398 ctr->values[ctr->num_values]->data_p = NULL;
400 ctr->values[ctr->num_values]->size = val->size;
404 return ctr->num_values;
407 /***********************************************************************
408 Delete a single value from the registry container.
409 No need to free memory since it is talloc'd.
410 **********************************************************************/
412 int regval_ctr_delvalue( REGVAL_CTR *ctr, const char *name )
416 for ( i=0; i<ctr->num_values; i++ ) {
417 if ( strequal( ctr->values[i]->valuename, name ) )
421 /* just return if we don't find it */
423 if ( i == ctr->num_values )
424 return ctr->num_values;
426 /* If 'i' was not the last element, just shift everything down one */
428 if ( i < ctr->num_values )
429 memmove( &ctr->values[i], &ctr->values[i+1], sizeof(REGISTRY_VALUE*)*(ctr->num_values-i) );
431 return ctr->num_values;
434 /***********************************************************************
435 Retrieve single value from the registry container.
436 No need to free memory since it is talloc'd.
437 **********************************************************************/
439 REGISTRY_VALUE* regval_ctr_getvalue( REGVAL_CTR *ctr, const char *name )
443 /* search for the value */
445 for ( i=0; i<ctr->num_values; i++ ) {
446 if ( strequal( ctr->values[i]->valuename, name ) )
447 return ctr->values[i];
453 /***********************************************************************
454 return the data_p as a uint32
455 **********************************************************************/
457 uint32 regval_dword( REGISTRY_VALUE *val )
461 data = IVAL( regval_data_p(val), 0 );
466 /***********************************************************************
467 return the data_p as a character string
468 **********************************************************************/
470 char* regval_sz( REGISTRY_VALUE *val )
474 rpcstr_pull( data, regval_data_p(val), sizeof(data), regval_size(val), 0 );
476 return talloc_strdup(talloc_tos(), data);