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_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
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],
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
)
118 for ( i
=0; i
<ctr
->num_subkeys
; i
++ ) {
119 if ( strequal( ctr
->subkeys
[i
],keyname
) )
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
) )
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
;
172 if ( !(copy
= SMB_MALLOC_P( REGISTRY_VALUE
)) ) {
173 DEBUG(0,("dup_registry_value: malloc() failed!\n"));
177 /* copy all the non-pointer initial data */
179 memcpy( copy
, val
, sizeof(REGISTRY_VALUE
) );
184 if ( val
->data_p
&& val
->size
)
186 if ( !(copy
->data_p
= (uint8
*)memdup( val
->data_p
,
188 DEBUG(0,("dup_registry_value: memdup() failed for [%d] "
189 "bytes!\n", val
->size
));
193 copy
->size
= val
->size
;
199 /**********************************************************************
200 free the memory allocated to a REGISTRY_VALUE
201 *********************************************************************/
203 void free_registry_value( REGISTRY_VALUE
*val
)
208 SAFE_FREE( val
->data_p
);
214 /**********************************************************************
215 *********************************************************************/
217 uint8
* regval_data_p( REGISTRY_VALUE
*val
)
222 /**********************************************************************
223 *********************************************************************/
225 uint32
regval_size( REGISTRY_VALUE
*val
)
230 /**********************************************************************
231 *********************************************************************/
233 char* regval_name( REGISTRY_VALUE
*val
)
235 return val
->valuename
;
238 /**********************************************************************
239 *********************************************************************/
241 uint32
regval_type( REGISTRY_VALUE
*val
)
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
) )
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
)
267 for ( i
=0; i
<ctr
->num_values
; i
++ ) {
268 if ( strequal( ctr
->values
[i
]->valuename
, value
) )
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
) {
288 fstrcpy(regval
->valuename
, name
);
291 regval
->data_p
= (uint8
*)TALLOC_MEMDUP(regval
, data_p
, size
);
292 if (!regval
->data_p
) {
297 regval
->data_p
= NULL
;
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
)
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
*);
323 ctr
->values
= TALLOC_REALLOC_ARRAY(ctr
, ctr
->values
,
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
,
337 if (ctr
->values
[ctr
->num_values
] == NULL
) {
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
)
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
)
369 for ( i
=0; i
<ctr
->num_values
; i
++ ) {
370 if ( strequal( ctr
->values
[i
]->valuename
, name
) )
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 */
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
)
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
];
407 /***********************************************************************
408 return the data_p as a uint32
409 **********************************************************************/
411 uint32
regval_dword( REGISTRY_VALUE
*val
)
415 data
= IVAL( regval_data_p(val
), 0 );
420 /***********************************************************************
421 return the data_p as a character string
422 **********************************************************************/
424 char *regval_sz(REGISTRY_VALUE
*val
)
428 rpcstr_pull_talloc(talloc_tos(), &data
,
429 regval_data_p(val
), regval_size(val
),0);