[GLUE] Rsync SAMBA_3_0 SVN r25598 in order to create the v3-0-test branch.
[Samba.git] / source / registry / reg_objects.c
blob4103033c751e0294272b138896e5b1688bd46f24
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 if ( !keyname )
47 return ctr->num_subkeys;
49 /* make sure the keyname is not already there */
51 if ( regsubkey_ctr_key_exists( ctr, keyname ) )
52 return ctr->num_subkeys;
54 /* allocate a space for the char* in the array */
56 if (ctr->subkeys == NULL) {
57 ctr->subkeys = TALLOC_P(ctr, char *);
58 } else {
59 ctr->subkeys = TALLOC_REALLOC_ARRAY(ctr, ctr->subkeys, char *, ctr->num_subkeys+1);
62 if (!ctr->subkeys) {
63 ctr->num_subkeys = 0;
64 return 0;
67 /* allocate the string and save it in the array */
69 ctr->subkeys[ctr->num_subkeys] = talloc_strdup( ctr, keyname );
70 ctr->num_subkeys++;
72 return ctr->num_subkeys;
75 /***********************************************************************
76 Add a new key to the array
77 **********************************************************************/
79 int regsubkey_ctr_delkey( REGSUBKEY_CTR *ctr, const char *keyname )
81 int i;
83 if ( !keyname )
84 return ctr->num_subkeys;
86 /* make sure the keyname is actually already there */
88 for ( i=0; i<ctr->num_subkeys; i++ ) {
89 if ( strequal( ctr->subkeys[i], keyname ) )
90 break;
93 if ( i == ctr->num_subkeys )
94 return ctr->num_subkeys;
96 /* update if we have any keys left */
97 ctr->num_subkeys--;
98 if ( i < ctr->num_subkeys )
99 memmove( &ctr->subkeys[i], &ctr->subkeys[i+1], sizeof(char*) * (ctr->num_subkeys-i) );
101 return ctr->num_subkeys;
104 /***********************************************************************
105 Check for the existance of a key
106 **********************************************************************/
108 BOOL regsubkey_ctr_key_exists( REGSUBKEY_CTR *ctr, const char *keyname )
110 int i;
112 if (!ctr->subkeys) {
113 return False;
116 for ( i=0; i<ctr->num_subkeys; i++ ) {
117 if ( strequal( ctr->subkeys[i],keyname ) )
118 return True;
121 return False;
124 /***********************************************************************
125 How many keys does the container hold ?
126 **********************************************************************/
128 int regsubkey_ctr_numkeys( REGSUBKEY_CTR *ctr )
130 return ctr->num_subkeys;
133 /***********************************************************************
134 Retreive a specific key string
135 **********************************************************************/
137 char* regsubkey_ctr_specific_key( REGSUBKEY_CTR *ctr, uint32 key_index )
139 if ( ! (key_index < ctr->num_subkeys) )
140 return NULL;
142 return ctr->subkeys[key_index];
146 * Utility functions for REGVAL_CTR
149 /***********************************************************************
150 How many keys does the container hold ?
151 **********************************************************************/
153 int regval_ctr_numvals( REGVAL_CTR *ctr )
155 return ctr->num_values;
158 /***********************************************************************
159 allocate memory for and duplicate a REGISTRY_VALUE.
160 This is malloc'd memory so the caller should free it when done
161 **********************************************************************/
163 REGISTRY_VALUE* dup_registry_value( REGISTRY_VALUE *val )
165 REGISTRY_VALUE *copy = NULL;
167 if ( !val )
168 return NULL;
170 if ( !(copy = SMB_MALLOC_P( REGISTRY_VALUE)) ) {
171 DEBUG(0,("dup_registry_value: malloc() failed!\n"));
172 return NULL;
175 /* copy all the non-pointer initial data */
177 memcpy( copy, val, sizeof(REGISTRY_VALUE) );
179 copy->size = 0;
180 copy->data_p = NULL;
182 if ( val->data_p && val->size )
184 if ( !(copy->data_p = (uint8 *)memdup( val->data_p,
185 val->size )) ) {
186 DEBUG(0,("dup_registry_value: memdup() failed for [%d] bytes!\n",
187 val->size));
188 SAFE_FREE( copy );
189 return NULL;
191 copy->size = val->size;
194 return copy;
197 /**********************************************************************
198 free the memory allocated to a REGISTRY_VALUE
199 *********************************************************************/
201 void free_registry_value( REGISTRY_VALUE *val )
203 if ( !val )
204 return;
206 SAFE_FREE( val->data_p );
207 SAFE_FREE( val );
209 return;
212 /**********************************************************************
213 *********************************************************************/
215 uint8* regval_data_p( REGISTRY_VALUE *val )
217 return val->data_p;
220 /**********************************************************************
221 *********************************************************************/
223 uint32 regval_size( REGISTRY_VALUE *val )
225 return val->size;
228 /**********************************************************************
229 *********************************************************************/
231 char* regval_name( REGISTRY_VALUE *val )
233 return val->valuename;
236 /**********************************************************************
237 *********************************************************************/
239 uint32 regval_type( REGISTRY_VALUE *val )
241 return val->type;
244 /***********************************************************************
245 Retreive a pointer to a specific value. Caller shoud dup the structure
246 since this memory will go away when the ctr is free()'d
247 **********************************************************************/
249 REGISTRY_VALUE* regval_ctr_specific_value( REGVAL_CTR *ctr, uint32 idx )
251 if ( !(idx < ctr->num_values) )
252 return NULL;
254 return ctr->values[idx];
257 /***********************************************************************
258 Check for the existance of a value
259 **********************************************************************/
261 BOOL regval_ctr_key_exists( REGVAL_CTR *ctr, const char *value )
263 int i;
265 for ( i=0; i<ctr->num_values; i++ ) {
266 if ( strequal( ctr->values[i]->valuename, value) )
267 return True;
270 return False;
272 /***********************************************************************
273 Add a new registry value to the array
274 **********************************************************************/
276 int regval_ctr_addvalue( REGVAL_CTR *ctr, const char *name, uint16 type,
277 const char *data_p, size_t size )
279 if ( !name )
280 return ctr->num_values;
282 /* Delete the current value (if it exists) and add the new one */
284 regval_ctr_delvalue( ctr, name );
286 /* allocate a slot in the array of pointers */
288 if ( ctr->num_values == 0 ) {
289 ctr->values = TALLOC_P( ctr, REGISTRY_VALUE *);
290 } else {
291 ctr->values = TALLOC_REALLOC_ARRAY( ctr, ctr->values, REGISTRY_VALUE *, ctr->num_values+1 );
294 if (!ctr->values) {
295 ctr->num_values = 0;
296 return 0;
299 /* allocate a new value and store the pointer in the arrya */
301 ctr->values[ctr->num_values] = TALLOC_P( ctr, REGISTRY_VALUE);
302 if (!ctr->values[ctr->num_values]) {
303 ctr->num_values = 0;
304 return 0;
307 /* init the value */
309 fstrcpy( ctr->values[ctr->num_values]->valuename, name );
310 ctr->values[ctr->num_values]->type = type;
311 if (size) {
312 ctr->values[ctr->num_values]->data_p = (uint8 *)TALLOC_MEMDUP(
313 ctr, data_p, size );
314 if (!ctr->values[ctr->num_values]->data_p) {
315 ctr->num_values = 0;
316 return 0;
318 } else {
319 ctr->values[ctr->num_values]->data_p = NULL;
321 ctr->values[ctr->num_values]->size = size;
322 ctr->num_values++;
324 return ctr->num_values;
327 /***********************************************************************
328 Add a new registry value to the array
329 **********************************************************************/
331 int regval_ctr_copyvalue( REGVAL_CTR *ctr, REGISTRY_VALUE *val )
333 if ( val ) {
334 /* allocate a slot in the array of pointers */
336 if ( ctr->num_values == 0 ) {
337 ctr->values = TALLOC_P( ctr, REGISTRY_VALUE *);
338 } else {
339 ctr->values = TALLOC_REALLOC_ARRAY( ctr, ctr->values, REGISTRY_VALUE *, ctr->num_values+1 );
342 if (!ctr->values) {
343 ctr->num_values = 0;
344 return 0;
347 /* allocate a new value and store the pointer in the arrya */
349 ctr->values[ctr->num_values] = TALLOC_P( ctr, REGISTRY_VALUE);
350 if (!ctr->values[ctr->num_values]) {
351 ctr->num_values = 0;
352 return 0;
355 /* init the value */
357 fstrcpy( ctr->values[ctr->num_values]->valuename, val->valuename );
358 ctr->values[ctr->num_values]->type = val->type;
359 if (val->size) {
360 ctr->values[ctr->num_values]->data_p = (uint8 *)TALLOC_MEMDUP(
361 ctr, val->data_p, val->size );
362 if (!ctr->values[ctr->num_values]->data_p) {
363 ctr->num_values = 0;
364 return 0;
366 } else {
367 ctr->values[ctr->num_values]->data_p = NULL;
369 ctr->values[ctr->num_values]->size = val->size;
370 ctr->num_values++;
373 return ctr->num_values;
376 /***********************************************************************
377 Delete a single value from the registry container.
378 No need to free memory since it is talloc'd.
379 **********************************************************************/
381 int regval_ctr_delvalue( REGVAL_CTR *ctr, const char *name )
383 int i;
385 for ( i=0; i<ctr->num_values; i++ ) {
386 if ( strequal( ctr->values[i]->valuename, name ) )
387 break;
390 /* just return if we don't find it */
392 if ( i == ctr->num_values )
393 return ctr->num_values;
395 /* If 'i' was not the last element, just shift everything down one */
396 ctr->num_values--;
397 if ( i < ctr->num_values )
398 memmove( &ctr->values[i], &ctr->values[i+1], sizeof(REGISTRY_VALUE*)*(ctr->num_values-i) );
400 return ctr->num_values;
403 /***********************************************************************
404 Retrieve single value from the registry container.
405 No need to free memory since it is talloc'd.
406 **********************************************************************/
408 REGISTRY_VALUE* regval_ctr_getvalue( REGVAL_CTR *ctr, const char *name )
410 int i;
412 /* search for the value */
414 for ( i=0; i<ctr->num_values; i++ ) {
415 if ( strequal( ctr->values[i]->valuename, name ) )
416 return ctr->values[i];
419 return NULL;
422 /***********************************************************************
423 return the data_p as a uint32
424 **********************************************************************/
426 uint32 regval_dword( REGISTRY_VALUE *val )
428 uint32 data;
430 data = IVAL( regval_data_p(val), 0 );
432 return data;
435 /***********************************************************************
436 return the data_p as a character string
437 **********************************************************************/
439 char* regval_sz( REGISTRY_VALUE *val )
441 static pstring data;
443 rpcstr_pull( data, regval_data_p(val), sizeof(data), regval_size(val), 0 );
445 return data;