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. */
26 #define DBGC_CLASS DBGC_REGISTRY
28 struct regsubkey_ctr
{
31 struct db_context
*subkeys_hash
;
35 /**********************************************************************
37 Note that the struct regsubkey_ctr and struct regval_ctr objects *must* be
38 talloc()'d since the methods use the object pointer as the talloc
39 context for internal private data.
41 There is no longer a regval_ctr_intit() and regval_ctr_destroy()
42 pair of functions. Simply TALLOC_ZERO_P() and TALLOC_FREE() the
45 **********************************************************************/
47 WERROR
regsubkey_ctr_init(TALLOC_CTX
*mem_ctx
, struct regsubkey_ctr
**ctr
)
50 return WERR_INVALID_PARAM
;
53 *ctr
= talloc_zero(mem_ctx
, struct regsubkey_ctr
);
58 (*ctr
)->subkeys_hash
= db_open_rbt(*ctr
);
59 if ((*ctr
)->subkeys_hash
== NULL
) {
68 * re-initialize the list of subkeys (to the emtpy list)
69 * in an already allocated regsubkey_ctr
72 WERROR
regsubkey_ctr_reinit(struct regsubkey_ctr
*ctr
)
75 return WERR_INVALID_PARAM
;
78 talloc_free(ctr
->subkeys_hash
);
79 ctr
->subkeys_hash
= db_open_rbt(ctr
);
80 W_ERROR_HAVE_NO_MEMORY(ctr
->subkeys_hash
);
82 TALLOC_FREE(ctr
->subkeys
);
90 WERROR
regsubkey_ctr_set_seqnum(struct regsubkey_ctr
*ctr
, int seqnum
)
93 return WERR_INVALID_PARAM
;
101 int regsubkey_ctr_get_seqnum(struct regsubkey_ctr
*ctr
)
110 static WERROR
regsubkey_ctr_hash_keyname(struct regsubkey_ctr
*ctr
,
116 werr
= ntstatus_to_werror(dbwrap_store_bystring_upper(ctr
->subkeys_hash
,
118 make_tdb_data((uint8
*)&idx
,
121 if (!W_ERROR_IS_OK(werr
)) {
122 DEBUG(1, ("error hashing new key '%s' in container: %s\n",
123 keyname
, win_errstr(werr
)));
129 static WERROR
regsubkey_ctr_unhash_keyname(struct regsubkey_ctr
*ctr
,
134 werr
= ntstatus_to_werror(dbwrap_delete_bystring_upper(ctr
->subkeys_hash
,
136 if (!W_ERROR_IS_OK(werr
)) {
137 DEBUG(1, ("error unhashing key '%s' in container: %s\n",
138 keyname
, win_errstr(werr
)));
144 static WERROR
regsubkey_ctr_index_for_keyname(struct regsubkey_ctr
*ctr
,
150 if ((ctr
== NULL
) || (keyname
== NULL
)) {
151 return WERR_INVALID_PARAM
;
154 data
= dbwrap_fetch_bystring_upper(ctr
->subkeys_hash
, ctr
, keyname
);
155 if (data
.dptr
== NULL
) {
156 return WERR_NOT_FOUND
;
159 if (data
.dsize
!= sizeof(*idx
)) {
160 talloc_free(data
.dptr
);
161 return WERR_INVALID_DATATYPE
;
165 *idx
= *(uint32
*)data
.dptr
;
168 talloc_free(data
.dptr
);
172 /***********************************************************************
173 Add a new key to the array
174 **********************************************************************/
176 WERROR
regsubkey_ctr_addkey( struct regsubkey_ctr
*ctr
, const char *keyname
)
185 /* make sure the keyname is not already there */
187 if ( regsubkey_ctr_key_exists( ctr
, keyname
) ) {
191 if (!(newkeys
= TALLOC_REALLOC_ARRAY(ctr
, ctr
->subkeys
, char *,
192 ctr
->num_subkeys
+1))) {
196 ctr
->subkeys
= newkeys
;
198 if (!(ctr
->subkeys
[ctr
->num_subkeys
] = talloc_strdup(ctr
->subkeys
,
201 * Don't shrink the new array again, this wastes a pointer
206 werr
= regsubkey_ctr_hash_keyname(ctr
, keyname
, ctr
->num_subkeys
);
207 W_ERROR_NOT_OK_RETURN(werr
);
214 /***********************************************************************
215 Delete a key from the array
216 **********************************************************************/
218 WERROR
regsubkey_ctr_delkey( struct regsubkey_ctr
*ctr
, const char *keyname
)
223 if (keyname
== NULL
) {
224 return WERR_INVALID_PARAM
;
227 /* make sure the keyname is actually already there */
229 werr
= regsubkey_ctr_index_for_keyname(ctr
, keyname
, &idx
);
230 W_ERROR_NOT_OK_RETURN(werr
);
232 werr
= regsubkey_ctr_unhash_keyname(ctr
, keyname
);
233 W_ERROR_NOT_OK_RETURN(werr
);
235 /* update if we have any keys left */
237 if (idx
< ctr
->num_subkeys
) {
238 memmove(&ctr
->subkeys
[idx
], &ctr
->subkeys
[idx
+1],
239 sizeof(char *) * (ctr
->num_subkeys
- idx
));
241 /* we have to re-hash rest of the array... :-( */
242 for (j
= idx
; j
< ctr
->num_subkeys
; j
++) {
243 werr
= regsubkey_ctr_hash_keyname(ctr
, ctr
->subkeys
[j
], j
);
244 W_ERROR_NOT_OK_RETURN(werr
);
251 /***********************************************************************
252 Check for the existance of a key
253 **********************************************************************/
255 bool regsubkey_ctr_key_exists( struct regsubkey_ctr
*ctr
, const char *keyname
)
263 werr
= regsubkey_ctr_index_for_keyname(ctr
, keyname
, NULL
);
264 if (!W_ERROR_IS_OK(werr
)) {
271 /***********************************************************************
272 How many keys does the container hold ?
273 **********************************************************************/
275 int regsubkey_ctr_numkeys( struct regsubkey_ctr
*ctr
)
277 return ctr
->num_subkeys
;
280 /***********************************************************************
281 Retreive a specific key string
282 **********************************************************************/
284 char* regsubkey_ctr_specific_key( struct regsubkey_ctr
*ctr
, uint32_t key_index
)
286 if ( ! (key_index
< ctr
->num_subkeys
) )
289 return ctr
->subkeys
[key_index
];
293 * Utility functions for struct regval_ctr
296 /***********************************************************************
297 How many keys does the container hold ?
298 **********************************************************************/
300 int regval_ctr_numvals(struct regval_ctr
*ctr
)
302 return ctr
->num_values
;
305 /***********************************************************************
306 allocate memory for and duplicate a struct regval_blob.
307 This is malloc'd memory so the caller should free it when done
308 **********************************************************************/
310 struct regval_blob
* dup_registry_value(struct regval_blob
*val
)
312 struct regval_blob
*copy
= NULL
;
317 if ( !(copy
= SMB_MALLOC_P( struct regval_blob
)) ) {
318 DEBUG(0,("dup_registry_value: malloc() failed!\n"));
322 /* copy all the non-pointer initial data */
324 memcpy( copy
, val
, sizeof(struct regval_blob
) );
329 if ( val
->data_p
&& val
->size
)
331 if ( !(copy
->data_p
= (uint8
*)memdup( val
->data_p
,
333 DEBUG(0,("dup_registry_value: memdup() failed for [%d] "
334 "bytes!\n", val
->size
));
338 copy
->size
= val
->size
;
344 /**********************************************************************
345 free the memory allocated to a struct regval_blob
346 *********************************************************************/
348 void free_registry_value(struct regval_blob
*val
)
353 SAFE_FREE( val
->data_p
);
359 /**********************************************************************
360 *********************************************************************/
362 uint8
* regval_data_p(struct regval_blob
*val
)
367 /**********************************************************************
368 *********************************************************************/
370 uint32
regval_size(struct regval_blob
*val
)
375 /**********************************************************************
376 *********************************************************************/
378 char* regval_name(struct regval_blob
*val
)
380 return val
->valuename
;
383 /**********************************************************************
384 *********************************************************************/
386 uint32
regval_type(struct regval_blob
*val
)
391 /***********************************************************************
392 Retreive a pointer to a specific value. Caller shoud dup the structure
393 since this memory will go away when the ctr is free()'d
394 **********************************************************************/
396 struct regval_blob
*regval_ctr_specific_value(struct regval_ctr
*ctr
,
399 if ( !(idx
< ctr
->num_values
) )
402 return ctr
->values
[idx
];
405 /***********************************************************************
406 Check for the existance of a value
407 **********************************************************************/
409 bool regval_ctr_key_exists(struct regval_ctr
*ctr
, const char *value
)
413 for ( i
=0; i
<ctr
->num_values
; i
++ ) {
414 if ( strequal( ctr
->values
[i
]->valuename
, value
) )
421 /***********************************************************************
422 * compose a struct regval_blob from input data
423 **********************************************************************/
425 struct regval_blob
*regval_compose(TALLOC_CTX
*ctx
, const char *name
,
427 const char *data_p
, size_t size
)
429 struct regval_blob
*regval
= TALLOC_P(ctx
, struct regval_blob
);
431 if (regval
== NULL
) {
435 fstrcpy(regval
->valuename
, name
);
438 regval
->data_p
= (uint8
*)TALLOC_MEMDUP(regval
, data_p
, size
);
439 if (!regval
->data_p
) {
444 regval
->data_p
= NULL
;
451 /***********************************************************************
452 Add a new registry value to the array
453 **********************************************************************/
455 int regval_ctr_addvalue(struct regval_ctr
*ctr
, const char *name
, uint16 type
,
456 const char *data_p
, size_t size
)
459 return ctr
->num_values
;
461 /* Delete the current value (if it exists) and add the new one */
463 regval_ctr_delvalue( ctr
, name
);
465 /* allocate a slot in the array of pointers */
467 if ( ctr
->num_values
== 0 ) {
468 ctr
->values
= TALLOC_P( ctr
, struct regval_blob
*);
470 ctr
->values
= TALLOC_REALLOC_ARRAY(ctr
, ctr
->values
,
471 struct regval_blob
*,
480 /* allocate a new value and store the pointer in the arrya */
482 ctr
->values
[ctr
->num_values
] = regval_compose(ctr
, name
, type
, data_p
,
484 if (ctr
->values
[ctr
->num_values
] == NULL
) {
490 return ctr
->num_values
;
493 /***********************************************************************
494 Add a new registry SZ value to the array
495 **********************************************************************/
497 int regval_ctr_addvalue_sz(struct regval_ctr
*ctr
, const char *name
, const char *data
)
501 if (!push_reg_sz(ctr
, &blob
, data
)) {
505 return regval_ctr_addvalue(ctr
, name
, REG_SZ
,
506 (const char *)blob
.data
,
510 /***********************************************************************
511 Add a new registry MULTI_SZ value to the array
512 **********************************************************************/
514 int regval_ctr_addvalue_multi_sz(struct regval_ctr
*ctr
, const char *name
, const char **data
)
518 if (!push_reg_multi_sz(ctr
, &blob
, data
)) {
522 return regval_ctr_addvalue(ctr
, name
, REG_MULTI_SZ
,
523 (const char *)blob
.data
,
527 /***********************************************************************
528 Add a new registry value to the array
529 **********************************************************************/
531 int regval_ctr_copyvalue(struct regval_ctr
*ctr
, struct regval_blob
*val
)
534 regval_ctr_addvalue(ctr
, val
->valuename
, val
->type
,
535 (char *)val
->data_p
, val
->size
);
538 return ctr
->num_values
;
541 /***********************************************************************
542 Delete a single value from the registry container.
543 No need to free memory since it is talloc'd.
544 **********************************************************************/
546 int regval_ctr_delvalue(struct regval_ctr
*ctr
, const char *name
)
550 for ( i
=0; i
<ctr
->num_values
; i
++ ) {
551 if ( strequal( ctr
->values
[i
]->valuename
, name
) )
555 /* just return if we don't find it */
557 if ( i
== ctr
->num_values
)
558 return ctr
->num_values
;
560 /* If 'i' was not the last element, just shift everything down one */
562 if ( i
< ctr
->num_values
)
563 memmove(&ctr
->values
[i
], &ctr
->values
[i
+1],
564 sizeof(struct regval_blob
*)*(ctr
->num_values
-i
));
566 return ctr
->num_values
;
569 /***********************************************************************
570 Retrieve single value from the registry container.
571 No need to free memory since it is talloc'd.
572 **********************************************************************/
574 struct regval_blob
* regval_ctr_getvalue(struct regval_ctr
*ctr
,
579 /* search for the value */
581 for ( i
=0; i
<ctr
->num_values
; i
++ ) {
582 if ( strequal( ctr
->values
[i
]->valuename
, name
) )
583 return ctr
->values
[i
];
589 /***********************************************************************
590 return the data_p as a uint32
591 **********************************************************************/
593 uint32
regval_dword(struct regval_blob
*val
)
597 data
= IVAL( regval_data_p(val
), 0 );
602 /***********************************************************************
603 return the data_p as a character string
604 **********************************************************************/
606 const char *regval_sz(struct regval_blob
*val
)
608 const char *data
= NULL
;
609 DATA_BLOB blob
= data_blob_const(regval_data_p(val
), regval_size(val
));
611 pull_reg_sz(talloc_tos(), &blob
, &data
);