Add comment about max value for os level
[Samba/gbeck.git] / source3 / registry / reg_objects.c
blob47122ccad2a20739e0c1483403eed89300b7e099
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 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. */
22 #include "includes.h"
24 #undef DBGC_CLASS
25 #define DBGC_CLASS DBGC_REGISTRY
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
35 object.
37 **********************************************************************/
39 /***********************************************************************
40 Add a new key to the array
41 **********************************************************************/
43 WERROR regsubkey_ctr_addkey( REGSUBKEY_CTR *ctr, const char *keyname )
45 char **newkeys;
47 if ( !keyname ) {
48 return WERR_OK;
51 /* make sure the keyname is not already there */
53 if ( regsubkey_ctr_key_exists( ctr, keyname ) ) {
54 return WERR_OK;
57 if (!(newkeys = TALLOC_REALLOC_ARRAY(ctr, ctr->subkeys, char *,
58 ctr->num_subkeys+1))) {
59 return WERR_NOMEM;
62 ctr->subkeys = newkeys;
64 if (!(ctr->subkeys[ctr->num_subkeys] = talloc_strdup(ctr->subkeys,
65 keyname ))) {
67 * Don't shrink the new array again, this wastes a pointer
69 return WERR_NOMEM;
71 ctr->num_subkeys++;
73 return WERR_OK;
76 /***********************************************************************
77 Delete a key from the array
78 **********************************************************************/
80 int regsubkey_ctr_delkey( REGSUBKEY_CTR *ctr, const char *keyname )
82 int i;
84 if ( !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 ) )
91 break;
94 if ( i == ctr->num_subkeys )
95 return ctr->num_subkeys;
97 /* update if we have any keys left */
98 ctr->num_subkeys--;
99 if ( i < ctr->num_subkeys )
100 memmove(&ctr->subkeys[i], &ctr->subkeys[i+1],
101 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 )
112 int i;
114 if (!ctr->subkeys) {
115 return False;
118 for ( i=0; i<ctr->num_subkeys; i++ ) {
119 if ( strequal( ctr->subkeys[i],keyname ) )
120 return True;
123 return False;
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) )
142 return NULL;
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;
169 if ( !val )
170 return NULL;
172 if ( !(copy = SMB_MALLOC_P( REGISTRY_VALUE)) ) {
173 DEBUG(0,("dup_registry_value: malloc() failed!\n"));
174 return NULL;
177 /* copy all the non-pointer initial data */
179 memcpy( copy, val, sizeof(REGISTRY_VALUE) );
181 copy->size = 0;
182 copy->data_p = NULL;
184 if ( val->data_p && val->size )
186 if ( !(copy->data_p = (uint8 *)memdup( val->data_p,
187 val->size )) ) {
188 DEBUG(0,("dup_registry_value: memdup() failed for [%d] "
189 "bytes!\n", val->size));
190 SAFE_FREE( copy );
191 return NULL;
193 copy->size = val->size;
196 return copy;
199 /**********************************************************************
200 free the memory allocated to a REGISTRY_VALUE
201 *********************************************************************/
203 void free_registry_value( REGISTRY_VALUE *val )
205 if ( !val )
206 return;
208 SAFE_FREE( val->data_p );
209 SAFE_FREE( val );
211 return;
214 /**********************************************************************
215 *********************************************************************/
217 uint8* regval_data_p( REGISTRY_VALUE *val )
219 return val->data_p;
222 /**********************************************************************
223 *********************************************************************/
225 uint32 regval_size( REGISTRY_VALUE *val )
227 return val->size;
230 /**********************************************************************
231 *********************************************************************/
233 char* regval_name( REGISTRY_VALUE *val )
235 return val->valuename;
238 /**********************************************************************
239 *********************************************************************/
241 uint32 regval_type( REGISTRY_VALUE *val )
243 return val->type;
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) )
254 return NULL;
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 )
265 int i;
267 for ( i=0; i<ctr->num_values; i++ ) {
268 if ( strequal( ctr->values[i]->valuename, value) )
269 return True;
272 return False;
275 /***********************************************************************
276 * compose a REGISTRY_VALUE from input data
277 **********************************************************************/
279 REGISTRY_VALUE *regval_compose(TALLOC_CTX *ctx, const char *name, uint16 type,
280 const char *data_p, size_t size)
282 REGISTRY_VALUE *regval = TALLOC_P(ctx, REGISTRY_VALUE);
284 if (regval == NULL) {
285 return NULL;
288 fstrcpy(regval->valuename, name);
289 regval->type = type;
290 if (size) {
291 regval->data_p = (uint8 *)TALLOC_MEMDUP(regval, data_p, size);
292 if (!regval->data_p) {
293 TALLOC_FREE(regval);
294 return NULL;
296 } else {
297 regval->data_p = NULL;
299 regval->size = size;
301 return regval;
304 /***********************************************************************
305 Add a new registry value to the array
306 **********************************************************************/
308 int regval_ctr_addvalue( REGVAL_CTR *ctr, const char *name, uint16 type,
309 const char *data_p, size_t size )
311 if ( !name )
312 return ctr->num_values;
314 /* Delete the current value (if it exists) and add the new one */
316 regval_ctr_delvalue( ctr, name );
318 /* allocate a slot in the array of pointers */
320 if ( ctr->num_values == 0 ) {
321 ctr->values = TALLOC_P( ctr, REGISTRY_VALUE *);
322 } else {
323 ctr->values = TALLOC_REALLOC_ARRAY(ctr, ctr->values,
324 REGISTRY_VALUE *,
325 ctr->num_values+1);
328 if (!ctr->values) {
329 ctr->num_values = 0;
330 return 0;
333 /* allocate a new value and store the pointer in the arrya */
335 ctr->values[ctr->num_values] = regval_compose(ctr, name, type, data_p,
336 size);
337 if (ctr->values[ctr->num_values] == NULL) {
338 ctr->num_values = 0;
339 return 0;
341 ctr->num_values++;
343 return ctr->num_values;
346 /***********************************************************************
347 Add a new registry value to the array
348 **********************************************************************/
350 int regval_ctr_copyvalue( REGVAL_CTR *ctr, REGISTRY_VALUE *val )
352 if ( val ) {
353 regval_ctr_addvalue(ctr, val->valuename, val->type,
354 (char *)val->data_p, val->size);
357 return ctr->num_values;
360 /***********************************************************************
361 Delete a single value from the registry container.
362 No need to free memory since it is talloc'd.
363 **********************************************************************/
365 int regval_ctr_delvalue( REGVAL_CTR *ctr, const char *name )
367 int i;
369 for ( i=0; i<ctr->num_values; i++ ) {
370 if ( strequal( ctr->values[i]->valuename, name ) )
371 break;
374 /* just return if we don't find it */
376 if ( i == ctr->num_values )
377 return ctr->num_values;
379 /* If 'i' was not the last element, just shift everything down one */
380 ctr->num_values--;
381 if ( i < ctr->num_values )
382 memmove(&ctr->values[i], &ctr->values[i+1],
383 sizeof(REGISTRY_VALUE*)*(ctr->num_values-i));
385 return ctr->num_values;
388 /***********************************************************************
389 Retrieve single value from the registry container.
390 No need to free memory since it is talloc'd.
391 **********************************************************************/
393 REGISTRY_VALUE* regval_ctr_getvalue( REGVAL_CTR *ctr, const char *name )
395 int i;
397 /* search for the value */
399 for ( i=0; i<ctr->num_values; i++ ) {
400 if ( strequal( ctr->values[i]->valuename, name ) )
401 return ctr->values[i];
404 return NULL;
407 /***********************************************************************
408 return the data_p as a uint32
409 **********************************************************************/
411 uint32 regval_dword( REGISTRY_VALUE *val )
413 uint32 data;
415 data = IVAL( regval_data_p(val), 0 );
417 return data;
420 /***********************************************************************
421 return the data_p as a character string
422 **********************************************************************/
424 char *regval_sz(REGISTRY_VALUE *val)
426 char *data = NULL;
428 rpcstr_pull_talloc(talloc_tos(), &data,
429 regval_data_p(val), regval_size(val),0);
430 return data;