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 struct regsubkey_ctr
{
33 /**********************************************************************
35 Note that the struct regsubkey_ctr and REGVAL_CTR objects *must* be
36 talloc()'d since the methods use the object pointer as the talloc
37 context for internal private data.
39 There is no longer a regval_ctr_intit() and regval_ctr_destroy()
40 pair of functions. Simply TALLOC_ZERO_P() and TALLOC_FREE() the
43 **********************************************************************/
45 WERROR
regsubkey_ctr_init(TALLOC_CTX
*mem_ctx
, struct regsubkey_ctr
**ctr
)
48 return WERR_INVALID_PARAM
;
51 *ctr
= talloc_zero(mem_ctx
, struct regsubkey_ctr
);
59 WERROR
regsubkey_ctr_set_seqnum(struct regsubkey_ctr
*ctr
, int seqnum
)
62 return WERR_INVALID_PARAM
;
70 int regsubkey_ctr_get_seqnum(struct regsubkey_ctr
*ctr
)
79 /***********************************************************************
80 Add a new key to the array
81 **********************************************************************/
83 WERROR
regsubkey_ctr_addkey( struct regsubkey_ctr
*ctr
, const char *keyname
)
91 /* make sure the keyname is not already there */
93 if ( regsubkey_ctr_key_exists( ctr
, keyname
) ) {
97 if (!(newkeys
= TALLOC_REALLOC_ARRAY(ctr
, ctr
->subkeys
, char *,
98 ctr
->num_subkeys
+1))) {
102 ctr
->subkeys
= newkeys
;
104 if (!(ctr
->subkeys
[ctr
->num_subkeys
] = talloc_strdup(ctr
->subkeys
,
107 * Don't shrink the new array again, this wastes a pointer
116 /***********************************************************************
117 Delete a key from the array
118 **********************************************************************/
120 int regsubkey_ctr_delkey( struct regsubkey_ctr
*ctr
, const char *keyname
)
125 return ctr
->num_subkeys
;
127 /* make sure the keyname is actually already there */
129 for ( i
=0; i
<ctr
->num_subkeys
; i
++ ) {
130 if ( strequal( ctr
->subkeys
[i
], keyname
) )
134 if ( i
== ctr
->num_subkeys
)
135 return ctr
->num_subkeys
;
137 /* update if we have any keys left */
139 if ( i
< ctr
->num_subkeys
)
140 memmove(&ctr
->subkeys
[i
], &ctr
->subkeys
[i
+1],
141 sizeof(char*) * (ctr
->num_subkeys
-i
));
143 return ctr
->num_subkeys
;
146 /***********************************************************************
147 Check for the existance of a key
148 **********************************************************************/
150 bool regsubkey_ctr_key_exists( struct regsubkey_ctr
*ctr
, const char *keyname
)
158 for ( i
=0; i
<ctr
->num_subkeys
; i
++ ) {
159 if ( strequal( ctr
->subkeys
[i
],keyname
) )
166 /***********************************************************************
167 How many keys does the container hold ?
168 **********************************************************************/
170 int regsubkey_ctr_numkeys( struct regsubkey_ctr
*ctr
)
172 return ctr
->num_subkeys
;
175 /***********************************************************************
176 Retreive a specific key string
177 **********************************************************************/
179 char* regsubkey_ctr_specific_key( struct regsubkey_ctr
*ctr
, uint32 key_index
)
181 if ( ! (key_index
< ctr
->num_subkeys
) )
184 return ctr
->subkeys
[key_index
];
188 * Utility functions for REGVAL_CTR
191 /***********************************************************************
192 How many keys does the container hold ?
193 **********************************************************************/
195 int regval_ctr_numvals( REGVAL_CTR
*ctr
)
197 return ctr
->num_values
;
200 /***********************************************************************
201 allocate memory for and duplicate a REGISTRY_VALUE.
202 This is malloc'd memory so the caller should free it when done
203 **********************************************************************/
205 REGISTRY_VALUE
* dup_registry_value( REGISTRY_VALUE
*val
)
207 REGISTRY_VALUE
*copy
= NULL
;
212 if ( !(copy
= SMB_MALLOC_P( REGISTRY_VALUE
)) ) {
213 DEBUG(0,("dup_registry_value: malloc() failed!\n"));
217 /* copy all the non-pointer initial data */
219 memcpy( copy
, val
, sizeof(REGISTRY_VALUE
) );
224 if ( val
->data_p
&& val
->size
)
226 if ( !(copy
->data_p
= (uint8
*)memdup( val
->data_p
,
228 DEBUG(0,("dup_registry_value: memdup() failed for [%d] "
229 "bytes!\n", val
->size
));
233 copy
->size
= val
->size
;
239 /**********************************************************************
240 free the memory allocated to a REGISTRY_VALUE
241 *********************************************************************/
243 void free_registry_value( REGISTRY_VALUE
*val
)
248 SAFE_FREE( val
->data_p
);
254 /**********************************************************************
255 *********************************************************************/
257 uint8
* regval_data_p( REGISTRY_VALUE
*val
)
262 /**********************************************************************
263 *********************************************************************/
265 uint32
regval_size( REGISTRY_VALUE
*val
)
270 /**********************************************************************
271 *********************************************************************/
273 char* regval_name( REGISTRY_VALUE
*val
)
275 return val
->valuename
;
278 /**********************************************************************
279 *********************************************************************/
281 uint32
regval_type( REGISTRY_VALUE
*val
)
286 /***********************************************************************
287 Retreive a pointer to a specific value. Caller shoud dup the structure
288 since this memory will go away when the ctr is free()'d
289 **********************************************************************/
291 REGISTRY_VALUE
* regval_ctr_specific_value( REGVAL_CTR
*ctr
, uint32 idx
)
293 if ( !(idx
< ctr
->num_values
) )
296 return ctr
->values
[idx
];
299 /***********************************************************************
300 Check for the existance of a value
301 **********************************************************************/
303 bool regval_ctr_key_exists( REGVAL_CTR
*ctr
, const char *value
)
307 for ( i
=0; i
<ctr
->num_values
; i
++ ) {
308 if ( strequal( ctr
->values
[i
]->valuename
, value
) )
315 /***********************************************************************
316 * compose a REGISTRY_VALUE from input data
317 **********************************************************************/
319 REGISTRY_VALUE
*regval_compose(TALLOC_CTX
*ctx
, const char *name
, uint16 type
,
320 const char *data_p
, size_t size
)
322 REGISTRY_VALUE
*regval
= TALLOC_P(ctx
, REGISTRY_VALUE
);
324 if (regval
== NULL
) {
328 fstrcpy(regval
->valuename
, name
);
331 regval
->data_p
= (uint8
*)TALLOC_MEMDUP(regval
, data_p
, size
);
332 if (!regval
->data_p
) {
337 regval
->data_p
= NULL
;
344 /***********************************************************************
345 Add a new registry value to the array
346 **********************************************************************/
348 int regval_ctr_addvalue( REGVAL_CTR
*ctr
, const char *name
, uint16 type
,
349 const char *data_p
, size_t size
)
352 return ctr
->num_values
;
354 /* Delete the current value (if it exists) and add the new one */
356 regval_ctr_delvalue( ctr
, name
);
358 /* allocate a slot in the array of pointers */
360 if ( ctr
->num_values
== 0 ) {
361 ctr
->values
= TALLOC_P( ctr
, REGISTRY_VALUE
*);
363 ctr
->values
= TALLOC_REALLOC_ARRAY(ctr
, ctr
->values
,
373 /* allocate a new value and store the pointer in the arrya */
375 ctr
->values
[ctr
->num_values
] = regval_compose(ctr
, name
, type
, data_p
,
377 if (ctr
->values
[ctr
->num_values
] == NULL
) {
383 return ctr
->num_values
;
386 /***********************************************************************
387 Add a new registry value to the array
388 **********************************************************************/
390 int regval_ctr_copyvalue( REGVAL_CTR
*ctr
, REGISTRY_VALUE
*val
)
393 regval_ctr_addvalue(ctr
, val
->valuename
, val
->type
,
394 (char *)val
->data_p
, val
->size
);
397 return ctr
->num_values
;
400 /***********************************************************************
401 Delete a single value from the registry container.
402 No need to free memory since it is talloc'd.
403 **********************************************************************/
405 int regval_ctr_delvalue( REGVAL_CTR
*ctr
, const char *name
)
409 for ( i
=0; i
<ctr
->num_values
; i
++ ) {
410 if ( strequal( ctr
->values
[i
]->valuename
, name
) )
414 /* just return if we don't find it */
416 if ( i
== ctr
->num_values
)
417 return ctr
->num_values
;
419 /* If 'i' was not the last element, just shift everything down one */
421 if ( i
< ctr
->num_values
)
422 memmove(&ctr
->values
[i
], &ctr
->values
[i
+1],
423 sizeof(REGISTRY_VALUE
*)*(ctr
->num_values
-i
));
425 return ctr
->num_values
;
428 /***********************************************************************
429 Retrieve single value from the registry container.
430 No need to free memory since it is talloc'd.
431 **********************************************************************/
433 REGISTRY_VALUE
* regval_ctr_getvalue( REGVAL_CTR
*ctr
, const char *name
)
437 /* search for the value */
439 for ( i
=0; i
<ctr
->num_values
; i
++ ) {
440 if ( strequal( ctr
->values
[i
]->valuename
, name
) )
441 return ctr
->values
[i
];
447 /***********************************************************************
448 return the data_p as a uint32
449 **********************************************************************/
451 uint32
regval_dword( REGISTRY_VALUE
*val
)
455 data
= IVAL( regval_data_p(val
), 0 );
460 /***********************************************************************
461 return the data_p as a character string
462 **********************************************************************/
464 char *regval_sz(REGISTRY_VALUE
*val
)
468 rpcstr_pull_talloc(talloc_tos(), &data
,
469 regval_data_p(val
), regval_size(val
),0);