r12028: fedora specfile fixes
[Samba.git] / source / registry / reg_objects.c
blob05567d561c62589a48b7f8e240dc31beaf81fd1a
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 /**********************************************************************
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
36 object.
38 **********************************************************************/
40 /***********************************************************************
41 Add a new key to the array
42 **********************************************************************/
44 int regsubkey_ctr_addkey( REGSUBKEY_CTR *ctr, const char *keyname )
46 char **pp;
48 if ( !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, char *);
60 else {
61 pp = TALLOC_REALLOC_ARRAY( ctr, ctr->subkeys, char *, ctr->num_subkeys+1);
62 if ( pp )
63 ctr->subkeys = pp;
66 /* allocate the string and save it in the array */
68 ctr->subkeys[ctr->num_subkeys] = talloc_strdup( ctr, keyname );
69 ctr->num_subkeys++;
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 )
80 int i;
82 if ( !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 ) )
89 break;
92 if ( i == ctr->num_subkeys )
93 return ctr->num_subkeys;
95 /* update if we have any keys left */
96 ctr->num_subkeys--;
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 )
109 int i;
111 for ( i=0; i<ctr->num_subkeys; i++ ) {
112 if ( strequal( ctr->subkeys[i],keyname ) )
113 return True;
116 return False;
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) )
135 return NULL;
137 return ctr->subkeys[key_index];
141 * Utility functions for REGVAL_CTR
144 /***********************************************************************
145 How many keys does the container hold ?
146 **********************************************************************/
148 int regval_ctr_numvals( REGVAL_CTR *ctr )
150 return ctr->num_values;
153 /***********************************************************************
154 allocate memory for and duplicate a REGISTRY_VALUE.
155 This is malloc'd memory so the caller should free it when done
156 **********************************************************************/
158 REGISTRY_VALUE* dup_registry_value( REGISTRY_VALUE *val )
160 REGISTRY_VALUE *copy = NULL;
162 if ( !val )
163 return NULL;
165 if ( !(copy = SMB_MALLOC_P( REGISTRY_VALUE)) ) {
166 DEBUG(0,("dup_registry_value: malloc() failed!\n"));
167 return NULL;
170 /* copy all the non-pointer initial data */
172 memcpy( copy, val, sizeof(REGISTRY_VALUE) );
174 copy->size = 0;
175 copy->data_p = NULL;
177 if ( val->data_p && val->size )
179 if ( !(copy->data_p = memdup( val->data_p, val->size )) ) {
180 DEBUG(0,("dup_registry_value: memdup() failed for [%d] bytes!\n",
181 val->size));
182 SAFE_FREE( copy );
184 copy->size = val->size;
187 return copy;
190 /**********************************************************************
191 free the memory allocated to a REGISTRY_VALUE
192 *********************************************************************/
194 void free_registry_value( REGISTRY_VALUE *val )
196 if ( !val )
197 return;
199 SAFE_FREE( val->data_p );
200 SAFE_FREE( val );
202 return;
205 /**********************************************************************
206 *********************************************************************/
208 uint8* regval_data_p( REGISTRY_VALUE *val )
210 return val->data_p;
213 /**********************************************************************
214 *********************************************************************/
216 uint32 regval_size( REGISTRY_VALUE *val )
218 return val->size;
221 /**********************************************************************
222 *********************************************************************/
224 char* regval_name( REGISTRY_VALUE *val )
226 return val->valuename;
229 /**********************************************************************
230 *********************************************************************/
232 uint32 regval_type( REGISTRY_VALUE *val )
234 return val->type;
237 /***********************************************************************
238 Retreive a pointer to a specific value. Caller shoud dup the structure
239 since this memory will go away when the ctr is free()'d
240 **********************************************************************/
242 REGISTRY_VALUE* regval_ctr_specific_value( REGVAL_CTR *ctr, uint32 idx )
244 if ( !(idx < ctr->num_values) )
245 return NULL;
247 return ctr->values[idx];
250 /***********************************************************************
251 Check for the existance of a value
252 **********************************************************************/
254 BOOL regval_ctr_key_exists( REGVAL_CTR *ctr, const char *value )
256 int i;
258 for ( i=0; i<ctr->num_values; i++ ) {
259 if ( strequal( ctr->values[i]->valuename, value) )
260 return True;
263 return False;
265 /***********************************************************************
266 Add a new registry value to the array
267 **********************************************************************/
269 int regval_ctr_addvalue( REGVAL_CTR *ctr, const char *name, uint16 type,
270 const char *data_p, size_t size )
272 REGISTRY_VALUE **ppreg;
274 if ( !name )
275 return ctr->num_values;
277 /* Delete the current value (if it exists) and add the new one */
279 regval_ctr_delvalue( ctr, name );
281 /* allocate a slot in the array of pointers */
283 if ( ctr->num_values == 0 )
284 ctr->values = TALLOC_P( ctr, REGISTRY_VALUE *);
285 else {
286 ppreg = TALLOC_REALLOC_ARRAY( ctr, ctr->values, REGISTRY_VALUE *, ctr->num_values+1 );
287 if ( ppreg )
288 ctr->values = ppreg;
291 /* allocate a new value and store the pointer in the arrya */
293 ctr->values[ctr->num_values] = TALLOC_P( ctr, REGISTRY_VALUE);
295 /* init the value */
297 fstrcpy( ctr->values[ctr->num_values]->valuename, name );
298 ctr->values[ctr->num_values]->type = type;
299 ctr->values[ctr->num_values]->data_p = TALLOC_MEMDUP( ctr, data_p, size );
300 ctr->values[ctr->num_values]->size = size;
301 ctr->num_values++;
303 return ctr->num_values;
306 /***********************************************************************
307 Add a new registry value to the array
308 **********************************************************************/
310 int regval_ctr_copyvalue( REGVAL_CTR *ctr, REGISTRY_VALUE *val )
312 REGISTRY_VALUE **ppreg;
314 if ( val )
316 /* allocate a slot in the array of pointers */
318 if ( ctr->num_values == 0 )
319 ctr->values = TALLOC_P( ctr, REGISTRY_VALUE *);
320 else {
321 ppreg = TALLOC_REALLOC_ARRAY( ctr, ctr->values, REGISTRY_VALUE *, ctr->num_values+1 );
322 if ( ppreg )
323 ctr->values = ppreg;
326 /* allocate a new value and store the pointer in the arrya */
328 ctr->values[ctr->num_values] = TALLOC_P( ctr, REGISTRY_VALUE);
330 /* init the value */
332 fstrcpy( ctr->values[ctr->num_values]->valuename, val->valuename );
333 ctr->values[ctr->num_values]->type = val->type;
334 ctr->values[ctr->num_values]->data_p = TALLOC_MEMDUP( ctr, val->data_p, val->size );
335 ctr->values[ctr->num_values]->size = val->size;
336 ctr->num_values++;
339 return ctr->num_values;
342 /***********************************************************************
343 Delete a single value from the registry container.
344 No need to free memory since it is talloc'd.
345 **********************************************************************/
347 int regval_ctr_delvalue( REGVAL_CTR *ctr, const char *name )
349 int i;
351 for ( i=0; i<ctr->num_values; i++ ) {
352 if ( strequal( ctr->values[i]->valuename, name ) )
353 break;
356 /* just return if we don't find it */
358 if ( i == ctr->num_values )
359 return ctr->num_values;
361 /* If 'i' was not the last element, just shift everything down one */
362 ctr->num_values--;
363 if ( i < ctr->num_values )
364 memmove( &ctr->values[i], &ctr->values[i+1], sizeof(REGISTRY_VALUE*)*(ctr->num_values-i) );
366 return ctr->num_values;
369 /***********************************************************************
370 Retrieve single value from the registry container.
371 No need to free memory since it is talloc'd.
372 **********************************************************************/
374 REGISTRY_VALUE* regval_ctr_getvalue( REGVAL_CTR *ctr, const char *name )
376 int i;
378 /* search for the value */
380 for ( i=0; i<ctr->num_values; i++ ) {
381 if ( strequal( ctr->values[i]->valuename, name ) )
382 return ctr->values[i];
385 return NULL;
388 /***********************************************************************
389 return the data_p as a uint32
390 **********************************************************************/
392 uint32 regval_dword( REGISTRY_VALUE *val )
394 uint32 data;
396 data = IVAL( regval_data_p(val), 0 );
398 return data;
401 /***********************************************************************
402 return the data_p as a character string
403 **********************************************************************/
405 char* regval_sz( REGISTRY_VALUE *val )
407 static pstring data;
409 rpcstr_pull( data, regval_data_p(val), sizeof(data), regval_size(val), 0 );
411 return data;