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. */
24 #include "reg_objects.h"
27 #define DBGC_CLASS DBGC_REGISTRY
29 /* low level structure to contain registry values */
34 /* this should be encapsulated in an RPC_DATA_BLOB */
35 uint32 size; /* in bytes */
39 /* container for registry values */
43 struct regval_blob **values;
47 struct regsubkey_ctr {
50 struct db_context *subkeys_hash;
54 /**********************************************************************
56 Note that the struct regsubkey_ctr and struct regval_ctr objects *must* be
57 talloc()'d since the methods use the object pointer as the talloc
58 context for internal private data.
60 There is no longer a regval_ctr_intit() and regval_ctr_destroy()
61 pair of functions. Simply TALLOC_ZERO_P() and TALLOC_FREE() the
64 **********************************************************************/
66 WERROR regsubkey_ctr_init(TALLOC_CTX *mem_ctx, struct regsubkey_ctr **ctr)
69 return WERR_INVALID_PARAM;
72 *ctr = talloc_zero(mem_ctx, struct regsubkey_ctr);
77 (*ctr)->subkeys_hash = db_open_rbt(*ctr);
78 if ((*ctr)->subkeys_hash == NULL) {
87 * re-initialize the list of subkeys (to the emtpy list)
88 * in an already allocated regsubkey_ctr
91 WERROR regsubkey_ctr_reinit(struct regsubkey_ctr *ctr)
94 return WERR_INVALID_PARAM;
97 talloc_free(ctr->subkeys_hash);
98 ctr->subkeys_hash = db_open_rbt(ctr);
99 W_ERROR_HAVE_NO_MEMORY(ctr->subkeys_hash);
101 TALLOC_FREE(ctr->subkeys);
103 ctr->num_subkeys = 0;
109 WERROR regsubkey_ctr_set_seqnum(struct regsubkey_ctr *ctr, int seqnum)
112 return WERR_INVALID_PARAM;
115 ctr->seqnum = seqnum;
120 int regsubkey_ctr_get_seqnum(struct regsubkey_ctr *ctr)
129 static WERROR regsubkey_ctr_hash_keyname(struct regsubkey_ctr *ctr,
135 werr = ntstatus_to_werror(dbwrap_store_bystring_upper(ctr->subkeys_hash,
137 make_tdb_data((uint8 *)&idx,
140 if (!W_ERROR_IS_OK(werr)) {
141 DEBUG(1, ("error hashing new key '%s' in container: %s\n",
142 keyname, win_errstr(werr)));
148 static WERROR regsubkey_ctr_unhash_keyname(struct regsubkey_ctr *ctr,
153 werr = ntstatus_to_werror(dbwrap_delete_bystring_upper(ctr->subkeys_hash,
155 if (!W_ERROR_IS_OK(werr)) {
156 DEBUG(1, ("error unhashing key '%s' in container: %s\n",
157 keyname, win_errstr(werr)));
163 static WERROR regsubkey_ctr_index_for_keyname(struct regsubkey_ctr *ctr,
169 if ((ctr == NULL) || (keyname == NULL)) {
170 return WERR_INVALID_PARAM;
173 data = dbwrap_fetch_bystring_upper(ctr->subkeys_hash, ctr, keyname);
174 if (data.dptr == NULL) {
175 return WERR_NOT_FOUND;
178 if (data.dsize != sizeof(*idx)) {
179 talloc_free(data.dptr);
180 return WERR_INVALID_DATATYPE;
184 *idx = *(uint32 *)data.dptr;
187 talloc_free(data.dptr);
191 /***********************************************************************
192 Add a new key to the array
193 **********************************************************************/
195 WERROR regsubkey_ctr_addkey( struct regsubkey_ctr *ctr, const char *keyname )
204 /* make sure the keyname is not already there */
206 if ( regsubkey_ctr_key_exists( ctr, keyname ) ) {
210 if (!(newkeys = TALLOC_REALLOC_ARRAY(ctr, ctr->subkeys, char *,
211 ctr->num_subkeys+1))) {
215 ctr->subkeys = newkeys;
217 if (!(ctr->subkeys[ctr->num_subkeys] = talloc_strdup(ctr->subkeys,
220 * Don't shrink the new array again, this wastes a pointer
225 werr = regsubkey_ctr_hash_keyname(ctr, keyname, ctr->num_subkeys);
226 W_ERROR_NOT_OK_RETURN(werr);
233 /***********************************************************************
234 Delete a key from the array
235 **********************************************************************/
237 WERROR regsubkey_ctr_delkey( struct regsubkey_ctr *ctr, const char *keyname )
242 if (keyname == NULL) {
243 return WERR_INVALID_PARAM;
246 /* make sure the keyname is actually already there */
248 werr = regsubkey_ctr_index_for_keyname(ctr, keyname, &idx);
249 W_ERROR_NOT_OK_RETURN(werr);
251 werr = regsubkey_ctr_unhash_keyname(ctr, keyname);
252 W_ERROR_NOT_OK_RETURN(werr);
254 /* update if we have any keys left */
256 if (idx < ctr->num_subkeys) {
257 memmove(&ctr->subkeys[idx], &ctr->subkeys[idx+1],
258 sizeof(char *) * (ctr->num_subkeys - idx));
260 /* we have to re-hash rest of the array... :-( */
261 for (j = idx; j < ctr->num_subkeys; j++) {
262 werr = regsubkey_ctr_hash_keyname(ctr, ctr->subkeys[j], j);
263 W_ERROR_NOT_OK_RETURN(werr);
270 /***********************************************************************
271 Check for the existance of a key
272 **********************************************************************/
274 bool regsubkey_ctr_key_exists( struct regsubkey_ctr *ctr, const char *keyname )
282 werr = regsubkey_ctr_index_for_keyname(ctr, keyname, NULL);
283 if (!W_ERROR_IS_OK(werr)) {
290 /***********************************************************************
291 How many keys does the container hold ?
292 **********************************************************************/
294 int regsubkey_ctr_numkeys( struct regsubkey_ctr *ctr )
296 return ctr->num_subkeys;
299 /***********************************************************************
300 Retreive a specific key string
301 **********************************************************************/
303 char* regsubkey_ctr_specific_key( struct regsubkey_ctr *ctr, uint32_t key_index )
305 if ( ! (key_index < ctr->num_subkeys) )
308 return ctr->subkeys[key_index];
312 * Utility functions for struct regval_ctr
316 * allocate a regval_ctr structure.
318 WERROR regval_ctr_init(TALLOC_CTX *mem_ctx, struct regval_ctr **ctr)
321 return WERR_INVALID_PARAM;
324 *ctr = talloc_zero(mem_ctx, struct regval_ctr);
332 /***********************************************************************
333 How many keys does the container hold ?
334 **********************************************************************/
336 int regval_ctr_numvals(struct regval_ctr *ctr)
338 return ctr->num_values;
341 /***********************************************************************
342 allocate memory for and duplicate a struct regval_blob.
343 This is malloc'd memory so the caller should free it when done
344 **********************************************************************/
346 struct regval_blob* dup_registry_value(struct regval_blob *val)
348 struct regval_blob *copy = NULL;
353 if ( !(copy = SMB_MALLOC_P( struct regval_blob)) ) {
354 DEBUG(0,("dup_registry_value: malloc() failed!\n"));
358 /* copy all the non-pointer initial data */
360 memcpy( copy, val, sizeof(struct regval_blob) );
365 if ( val->data_p && val->size )
367 if ( !(copy->data_p = (uint8 *)memdup( val->data_p,
369 DEBUG(0,("dup_registry_value: memdup() failed for [%d] "
370 "bytes!\n", val->size));
374 copy->size = val->size;
380 /**********************************************************************
381 free the memory allocated to a struct regval_blob
382 *********************************************************************/
384 void free_registry_value(struct regval_blob *val)
389 SAFE_FREE( val->data_p );
395 /**********************************************************************
396 *********************************************************************/
398 uint8* regval_data_p(struct regval_blob *val)
403 /**********************************************************************
404 *********************************************************************/
406 uint32 regval_size(struct regval_blob *val)
411 /**********************************************************************
412 *********************************************************************/
414 char* regval_name(struct regval_blob *val)
416 return val->valuename;
419 /**********************************************************************
420 *********************************************************************/
422 uint32 regval_type(struct regval_blob *val)
427 /***********************************************************************
428 Retreive a pointer to a specific value. Caller shoud dup the structure
429 since this memory will go away when the ctr is free()'d
430 **********************************************************************/
432 struct regval_blob *regval_ctr_specific_value(struct regval_ctr *ctr,
435 if ( !(idx < ctr->num_values) )
438 return ctr->values[idx];
441 /***********************************************************************
442 Check for the existance of a value
443 **********************************************************************/
445 bool regval_ctr_key_exists(struct regval_ctr *ctr, const char *value)
449 for ( i=0; i<ctr->num_values; i++ ) {
450 if ( strequal( ctr->values[i]->valuename, value) )
457 /***********************************************************************
458 * compose a struct regval_blob from input data
459 **********************************************************************/
461 struct regval_blob *regval_compose(TALLOC_CTX *ctx, const char *name,
463 const uint8 *data_p, size_t size)
465 struct regval_blob *regval = TALLOC_P(ctx, struct regval_blob);
467 if (regval == NULL) {
471 fstrcpy(regval->valuename, name);
474 regval->data_p = (uint8 *)TALLOC_MEMDUP(regval, data_p, size);
475 if (!regval->data_p) {
480 regval->data_p = NULL;
487 /***********************************************************************
488 Add a new registry value to the array
489 **********************************************************************/
491 int regval_ctr_addvalue(struct regval_ctr *ctr, const char *name, uint16 type,
492 const uint8 *data_p, size_t size)
495 return ctr->num_values;
497 /* Delete the current value (if it exists) and add the new one */
499 regval_ctr_delvalue( ctr, name );
501 /* allocate a slot in the array of pointers */
503 if ( ctr->num_values == 0 ) {
504 ctr->values = TALLOC_P( ctr, struct regval_blob *);
506 ctr->values = TALLOC_REALLOC_ARRAY(ctr, ctr->values,
507 struct regval_blob *,
516 /* allocate a new value and store the pointer in the arrya */
518 ctr->values[ctr->num_values] = regval_compose(ctr, name, type, data_p,
520 if (ctr->values[ctr->num_values] == NULL) {
526 return ctr->num_values;
529 /***********************************************************************
530 Add a new registry SZ value to the array
531 **********************************************************************/
533 int regval_ctr_addvalue_sz(struct regval_ctr *ctr, const char *name, const char *data)
537 if (!push_reg_sz(ctr, &blob, data)) {
541 return regval_ctr_addvalue(ctr, name, REG_SZ,
542 (const uint8 *)blob.data,
546 /***********************************************************************
547 Add a new registry MULTI_SZ value to the array
548 **********************************************************************/
550 int regval_ctr_addvalue_multi_sz(struct regval_ctr *ctr, const char *name, const char **data)
554 if (!push_reg_multi_sz(ctr, &blob, data)) {
558 return regval_ctr_addvalue(ctr, name, REG_MULTI_SZ,
559 (const uint8 *)blob.data,
563 /***********************************************************************
564 Add a new registry value to the array
565 **********************************************************************/
567 int regval_ctr_copyvalue(struct regval_ctr *ctr, struct regval_blob *val)
570 regval_ctr_addvalue(ctr, val->valuename, val->type,
571 (uint8 *)val->data_p, val->size);
574 return ctr->num_values;
577 /***********************************************************************
578 Delete a single value from the registry container.
579 No need to free memory since it is talloc'd.
580 **********************************************************************/
582 int regval_ctr_delvalue(struct regval_ctr *ctr, const char *name)
586 for ( i=0; i<ctr->num_values; i++ ) {
587 if ( strequal( ctr->values[i]->valuename, name ) )
591 /* just return if we don't find it */
593 if ( i == ctr->num_values )
594 return ctr->num_values;
596 /* If 'i' was not the last element, just shift everything down one */
598 if ( i < ctr->num_values )
599 memmove(&ctr->values[i], &ctr->values[i+1],
600 sizeof(struct regval_blob*)*(ctr->num_values-i));
602 return ctr->num_values;
605 /***********************************************************************
606 Retrieve single value from the registry container.
607 No need to free memory since it is talloc'd.
608 **********************************************************************/
610 struct regval_blob* regval_ctr_getvalue(struct regval_ctr *ctr,
615 /* search for the value */
617 for ( i=0; i<ctr->num_values; i++ ) {
618 if ( strequal( ctr->values[i]->valuename, name ) )
619 return ctr->values[i];
625 int regval_ctr_get_seqnum(struct regval_ctr *ctr)
634 WERROR regval_ctr_set_seqnum(struct regval_ctr *ctr, int seqnum)
637 return WERR_INVALID_PARAM;
640 ctr->seqnum = seqnum;
645 /***********************************************************************
646 return the data_p as a uint32
647 **********************************************************************/
649 uint32 regval_dword(struct regval_blob *val)
653 data = IVAL( regval_data_p(val), 0 );
658 /***********************************************************************
659 return the data_p as a character string
660 **********************************************************************/
662 const char *regval_sz(struct regval_blob *val)
664 const char *data = NULL;
665 DATA_BLOB blob = data_blob_const(regval_data_p(val), regval_size(val));
667 pull_reg_sz(talloc_tos(), &blob, &data);