Samba 3: added Samba 3.0.24 sources
[tomato.git] / release / src / router / samba3 / source / registry / reg_objects.c
blobecad94f1d6cc59a92d506339814644174468a506
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 = memdup( val->data_p, val->size )) ) {
185 DEBUG(0,("dup_registry_value: memdup() failed for [%d] bytes!\n",
186 val->size));
187 SAFE_FREE( copy );
188 return NULL;
190 copy->size = val->size;
193 return copy;
196 /**********************************************************************
197 free the memory allocated to a REGISTRY_VALUE
198 *********************************************************************/
200 void free_registry_value( REGISTRY_VALUE *val )
202 if ( !val )
203 return;
205 SAFE_FREE( val->data_p );
206 SAFE_FREE( val );
208 return;
211 /**********************************************************************
212 *********************************************************************/
214 uint8* regval_data_p( REGISTRY_VALUE *val )
216 return val->data_p;
219 /**********************************************************************
220 *********************************************************************/
222 uint32 regval_size( REGISTRY_VALUE *val )
224 return val->size;
227 /**********************************************************************
228 *********************************************************************/
230 char* regval_name( REGISTRY_VALUE *val )
232 return val->valuename;
235 /**********************************************************************
236 *********************************************************************/
238 uint32 regval_type( REGISTRY_VALUE *val )
240 return val->type;
243 /***********************************************************************
244 Retreive a pointer to a specific value. Caller shoud dup the structure
245 since this memory will go away when the ctr is free()'d
246 **********************************************************************/
248 REGISTRY_VALUE* regval_ctr_specific_value( REGVAL_CTR *ctr, uint32 idx )
250 if ( !(idx < ctr->num_values) )
251 return NULL;
253 return ctr->values[idx];
256 /***********************************************************************
257 Check for the existance of a value
258 **********************************************************************/
260 BOOL regval_ctr_key_exists( REGVAL_CTR *ctr, const char *value )
262 int i;
264 for ( i=0; i<ctr->num_values; i++ ) {
265 if ( strequal( ctr->values[i]->valuename, value) )
266 return True;
269 return False;
271 /***********************************************************************
272 Add a new registry value to the array
273 **********************************************************************/
275 int regval_ctr_addvalue( REGVAL_CTR *ctr, const char *name, uint16 type,
276 const char *data_p, size_t size )
278 if ( !name )
279 return ctr->num_values;
281 /* Delete the current value (if it exists) and add the new one */
283 regval_ctr_delvalue( ctr, name );
285 /* allocate a slot in the array of pointers */
287 if ( ctr->num_values == 0 ) {
288 ctr->values = TALLOC_P( ctr, REGISTRY_VALUE *);
289 } else {
290 ctr->values = TALLOC_REALLOC_ARRAY( ctr, ctr->values, REGISTRY_VALUE *, ctr->num_values+1 );
293 if (!ctr->values) {
294 ctr->num_values = 0;
295 return 0;
298 /* allocate a new value and store the pointer in the arrya */
300 ctr->values[ctr->num_values] = TALLOC_P( ctr, REGISTRY_VALUE);
301 if (!ctr->values[ctr->num_values]) {
302 ctr->num_values = 0;
303 return 0;
306 /* init the value */
308 fstrcpy( ctr->values[ctr->num_values]->valuename, name );
309 ctr->values[ctr->num_values]->type = type;
310 ctr->values[ctr->num_values]->data_p = TALLOC_MEMDUP( ctr, data_p, size );
311 ctr->values[ctr->num_values]->size = size;
312 ctr->num_values++;
314 return ctr->num_values;
317 /***********************************************************************
318 Add a new registry value to the array
319 **********************************************************************/
321 int regval_ctr_copyvalue( REGVAL_CTR *ctr, REGISTRY_VALUE *val )
323 if ( val ) {
324 /* allocate a slot in the array of pointers */
326 if ( ctr->num_values == 0 ) {
327 ctr->values = TALLOC_P( ctr, REGISTRY_VALUE *);
328 } else {
329 ctr->values = TALLOC_REALLOC_ARRAY( ctr, ctr->values, REGISTRY_VALUE *, ctr->num_values+1 );
332 if (!ctr->values) {
333 ctr->num_values = 0;
334 return 0;
337 /* allocate a new value and store the pointer in the arrya */
339 ctr->values[ctr->num_values] = TALLOC_P( ctr, REGISTRY_VALUE);
340 if (!ctr->values[ctr->num_values]) {
341 ctr->num_values = 0;
342 return 0;
345 /* init the value */
347 fstrcpy( ctr->values[ctr->num_values]->valuename, val->valuename );
348 ctr->values[ctr->num_values]->type = val->type;
349 ctr->values[ctr->num_values]->data_p = TALLOC_MEMDUP( ctr, val->data_p, val->size );
350 ctr->values[ctr->num_values]->size = val->size;
351 ctr->num_values++;
354 return ctr->num_values;
357 /***********************************************************************
358 Delete a single value from the registry container.
359 No need to free memory since it is talloc'd.
360 **********************************************************************/
362 int regval_ctr_delvalue( REGVAL_CTR *ctr, const char *name )
364 int i;
366 for ( i=0; i<ctr->num_values; i++ ) {
367 if ( strequal( ctr->values[i]->valuename, name ) )
368 break;
371 /* just return if we don't find it */
373 if ( i == ctr->num_values )
374 return ctr->num_values;
376 /* If 'i' was not the last element, just shift everything down one */
377 ctr->num_values--;
378 if ( i < ctr->num_values )
379 memmove( &ctr->values[i], &ctr->values[i+1], sizeof(REGISTRY_VALUE*)*(ctr->num_values-i) );
381 return ctr->num_values;
384 /***********************************************************************
385 Retrieve single value from the registry container.
386 No need to free memory since it is talloc'd.
387 **********************************************************************/
389 REGISTRY_VALUE* regval_ctr_getvalue( REGVAL_CTR *ctr, const char *name )
391 int i;
393 /* search for the value */
395 for ( i=0; i<ctr->num_values; i++ ) {
396 if ( strequal( ctr->values[i]->valuename, name ) )
397 return ctr->values[i];
400 return NULL;
403 /***********************************************************************
404 return the data_p as a uint32
405 **********************************************************************/
407 uint32 regval_dword( REGISTRY_VALUE *val )
409 uint32 data;
411 data = IVAL( regval_data_p(val), 0 );
413 return data;
416 /***********************************************************************
417 return the data_p as a character string
418 **********************************************************************/
420 char* regval_sz( REGISTRY_VALUE *val )
422 static pstring data;
424 rpcstr_pull( data, regval_data_p(val), sizeof(data), regval_size(val), 0 );
426 return data;