2 * Unix SMB/CIFS implementation.
3 * Virtual Windows Registry Layer
4 * Copyright (C) Gerald Carter 2002-2005
5 * Copyright (C) Michael Adam 2007-2010
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 /* Implementation of registry frontend view functions. */
25 #include "reg_objects.h"
28 #include "../libcli/registry/util_reg.h"
31 #define DBGC_CLASS DBGC_REGISTRY
33 /* low level structure to contain registry values */
38 /* this should be encapsulated in an RPC_DATA_BLOB */
39 uint32_t size; /* in bytes */
43 /* container for registry values */
47 struct regval_blob **values;
51 struct regsubkey_ctr {
54 struct db_context *subkeys_hash;
58 /**********************************************************************
60 Note that the struct regsubkey_ctr and struct regval_ctr objects *must* be
61 talloc()'d since the methods use the object pointer as the talloc
62 context for internal private data.
64 There is no longer a regval_ctr_intit() and regval_ctr_destroy()
65 pair of functions. Simply talloc_zero() and TALLOC_FREE() the
68 **********************************************************************/
70 WERROR regsubkey_ctr_init(TALLOC_CTX *mem_ctx, struct regsubkey_ctr **ctr)
73 return WERR_INVALID_PARAM;
76 *ctr = talloc_zero(mem_ctx, struct regsubkey_ctr);
81 (*ctr)->subkeys_hash = db_open_rbt(*ctr);
82 if ((*ctr)->subkeys_hash == NULL) {
91 * re-initialize the list of subkeys (to the emtpy list)
92 * in an already allocated regsubkey_ctr
95 WERROR regsubkey_ctr_reinit(struct regsubkey_ctr *ctr)
98 return WERR_INVALID_PARAM;
101 talloc_free(ctr->subkeys_hash);
102 ctr->subkeys_hash = db_open_rbt(ctr);
103 W_ERROR_HAVE_NO_MEMORY(ctr->subkeys_hash);
105 TALLOC_FREE(ctr->subkeys);
107 ctr->num_subkeys = 0;
113 WERROR regsubkey_ctr_set_seqnum(struct regsubkey_ctr *ctr, int seqnum)
116 return WERR_INVALID_PARAM;
119 ctr->seqnum = seqnum;
124 int regsubkey_ctr_get_seqnum(struct regsubkey_ctr *ctr)
133 static WERROR regsubkey_ctr_hash_keyname(struct regsubkey_ctr *ctr,
139 werr = ntstatus_to_werror(dbwrap_store_bystring_upper(ctr->subkeys_hash,
141 make_tdb_data((uint8_t *)&idx,
144 if (!W_ERROR_IS_OK(werr)) {
145 DEBUG(1, ("error hashing new key '%s' in container: %s\n",
146 keyname, win_errstr(werr)));
152 static WERROR regsubkey_ctr_unhash_keyname(struct regsubkey_ctr *ctr,
157 werr = ntstatus_to_werror(dbwrap_delete_bystring_upper(ctr->subkeys_hash,
159 if (!W_ERROR_IS_OK(werr)) {
160 DEBUG(1, ("error unhashing key '%s' in container: %s\n",
161 keyname, win_errstr(werr)));
167 static WERROR regsubkey_ctr_index_for_keyname(struct regsubkey_ctr *ctr,
173 if ((ctr == NULL) || (keyname == NULL)) {
174 return WERR_INVALID_PARAM;
177 data = dbwrap_fetch_bystring_upper(ctr->subkeys_hash, ctr, keyname);
178 if (data.dptr == NULL) {
179 return WERR_NOT_FOUND;
182 if (data.dsize != sizeof(*idx)) {
183 talloc_free(data.dptr);
184 return WERR_INVALID_DATATYPE;
188 *idx = *(uint32_t *)data.dptr;
191 talloc_free(data.dptr);
195 /***********************************************************************
196 Add a new key to the array
197 **********************************************************************/
199 WERROR regsubkey_ctr_addkey( struct regsubkey_ctr *ctr, const char *keyname )
208 /* make sure the keyname is not already there */
210 if ( regsubkey_ctr_key_exists( ctr, keyname ) ) {
214 if (!(newkeys = talloc_realloc(ctr, ctr->subkeys, char *,
215 ctr->num_subkeys+1))) {
219 ctr->subkeys = newkeys;
221 if (!(ctr->subkeys[ctr->num_subkeys] = talloc_strdup(ctr->subkeys,
224 * Don't shrink the new array again, this wastes a pointer
229 werr = regsubkey_ctr_hash_keyname(ctr, keyname, ctr->num_subkeys);
230 W_ERROR_NOT_OK_RETURN(werr);
237 /***********************************************************************
238 Delete a key from the array
239 **********************************************************************/
241 WERROR regsubkey_ctr_delkey( struct regsubkey_ctr *ctr, const char *keyname )
246 if (keyname == NULL) {
247 return WERR_INVALID_PARAM;
250 /* make sure the keyname is actually already there */
252 werr = regsubkey_ctr_index_for_keyname(ctr, keyname, &idx);
253 W_ERROR_NOT_OK_RETURN(werr);
255 werr = regsubkey_ctr_unhash_keyname(ctr, keyname);
256 W_ERROR_NOT_OK_RETURN(werr);
258 /* update if we have any keys left */
260 if (idx < ctr->num_subkeys) {
261 memmove(&ctr->subkeys[idx], &ctr->subkeys[idx+1],
262 sizeof(char *) * (ctr->num_subkeys - idx));
264 /* we have to re-hash rest of the array... :-( */
265 for (j = idx; j < ctr->num_subkeys; j++) {
266 werr = regsubkey_ctr_hash_keyname(ctr, ctr->subkeys[j], j);
267 W_ERROR_NOT_OK_RETURN(werr);
274 /***********************************************************************
275 Check for the existance of a key
276 **********************************************************************/
278 bool regsubkey_ctr_key_exists( struct regsubkey_ctr *ctr, const char *keyname )
286 werr = regsubkey_ctr_index_for_keyname(ctr, keyname, NULL);
287 if (!W_ERROR_IS_OK(werr)) {
294 /***********************************************************************
295 How many keys does the container hold ?
296 **********************************************************************/
298 int regsubkey_ctr_numkeys( struct regsubkey_ctr *ctr )
300 return ctr->num_subkeys;
303 /***********************************************************************
304 Retreive a specific key string
305 **********************************************************************/
307 char* regsubkey_ctr_specific_key( struct regsubkey_ctr *ctr, uint32_t key_index )
309 if ( ! (key_index < ctr->num_subkeys) )
312 return ctr->subkeys[key_index];
316 * Utility functions for struct regval_ctr
320 * allocate a regval_ctr structure.
322 WERROR regval_ctr_init(TALLOC_CTX *mem_ctx, struct regval_ctr **ctr)
325 return WERR_INVALID_PARAM;
328 *ctr = talloc_zero(mem_ctx, struct regval_ctr);
336 /***********************************************************************
337 How many keys does the container hold ?
338 **********************************************************************/
340 int regval_ctr_numvals(struct regval_ctr *ctr)
342 return ctr->num_values;
345 /***********************************************************************
346 allocate memory for and duplicate a struct regval_blob.
347 This is malloc'd memory so the caller should free it when done
348 **********************************************************************/
350 struct regval_blob* dup_registry_value(struct regval_blob *val)
352 struct regval_blob *copy = NULL;
357 if ( !(copy = SMB_MALLOC_P( struct regval_blob)) ) {
358 DEBUG(0,("dup_registry_value: malloc() failed!\n"));
362 /* copy all the non-pointer initial data */
364 memcpy( copy, val, sizeof(struct regval_blob) );
369 if ( val->data_p && val->size )
371 if ( !(copy->data_p = (uint8_t *)memdup( val->data_p,
373 DEBUG(0,("dup_registry_value: memdup() failed for [%d] "
374 "bytes!\n", val->size));
378 copy->size = val->size;
384 /**********************************************************************
385 free the memory allocated to a struct regval_blob
386 *********************************************************************/
388 void free_registry_value(struct regval_blob *val)
393 SAFE_FREE( val->data_p );
399 /**********************************************************************
400 *********************************************************************/
402 uint8_t* regval_data_p(struct regval_blob *val)
407 /**********************************************************************
408 *********************************************************************/
410 uint32_t regval_size(struct regval_blob *val)
415 /**********************************************************************
416 *********************************************************************/
418 char* regval_name(struct regval_blob *val)
420 return val->valuename;
423 /**********************************************************************
424 *********************************************************************/
426 uint32_t regval_type(struct regval_blob *val)
431 /***********************************************************************
432 Retreive a pointer to a specific value. Caller shoud dup the structure
433 since this memory will go away when the ctr is free()'d
434 **********************************************************************/
436 struct regval_blob *regval_ctr_specific_value(struct regval_ctr *ctr,
439 if ( !(idx < ctr->num_values) )
442 return ctr->values[idx];
445 /***********************************************************************
446 Check for the existance of a value
447 **********************************************************************/
449 bool regval_ctr_key_exists(struct regval_ctr *ctr, const char *value)
453 for ( i=0; i<ctr->num_values; i++ ) {
454 if ( strequal( ctr->values[i]->valuename, value) )
461 /***********************************************************************
462 * compose a struct regval_blob from input data
463 **********************************************************************/
465 struct regval_blob *regval_compose(TALLOC_CTX *ctx, const char *name,
467 const uint8_t *data_p, size_t size)
469 struct regval_blob *regval = talloc(ctx, struct regval_blob);
471 if (regval == NULL) {
475 fstrcpy(regval->valuename, name);
478 regval->data_p = (uint8_t *)TALLOC_MEMDUP(regval, data_p, size);
479 if (!regval->data_p) {
484 regval->data_p = NULL;
491 /***********************************************************************
492 Add a new registry value to the array
493 **********************************************************************/
495 int regval_ctr_addvalue(struct regval_ctr *ctr, const char *name, uint32_t type,
496 const uint8_t *data_p, size_t size)
499 return ctr->num_values;
501 /* Delete the current value (if it exists) and add the new one */
503 regval_ctr_delvalue( ctr, name );
505 /* allocate a slot in the array of pointers */
507 if ( ctr->num_values == 0 ) {
508 ctr->values = talloc( ctr, struct regval_blob *);
510 ctr->values = talloc_realloc(ctr, ctr->values,
511 struct regval_blob *,
520 /* allocate a new value and store the pointer in the arrya */
522 ctr->values[ctr->num_values] = regval_compose(ctr, name, type, data_p,
524 if (ctr->values[ctr->num_values] == NULL) {
530 return ctr->num_values;
533 /***********************************************************************
534 Add a new registry SZ value to the array
535 **********************************************************************/
537 int regval_ctr_addvalue_sz(struct regval_ctr *ctr, const char *name, const char *data)
541 if (!push_reg_sz(ctr, &blob, data)) {
545 return regval_ctr_addvalue(ctr, name, REG_SZ,
546 (const uint8_t *)blob.data,
550 /***********************************************************************
551 Add a new registry MULTI_SZ value to the array
552 **********************************************************************/
554 int regval_ctr_addvalue_multi_sz(struct regval_ctr *ctr, const char *name, const char **data)
558 if (!push_reg_multi_sz(ctr, &blob, data)) {
562 return regval_ctr_addvalue(ctr, name, REG_MULTI_SZ,
563 (const uint8_t *)blob.data,
567 /***********************************************************************
568 Add a new registry value to the array
569 **********************************************************************/
571 int regval_ctr_copyvalue(struct regval_ctr *ctr, struct regval_blob *val)
574 regval_ctr_addvalue(ctr, val->valuename, val->type,
575 (uint8_t *)val->data_p, val->size);
578 return ctr->num_values;
581 /***********************************************************************
582 Delete a single value from the registry container.
583 No need to free memory since it is talloc'd.
584 **********************************************************************/
586 int regval_ctr_delvalue(struct regval_ctr *ctr, const char *name)
590 for ( i=0; i<ctr->num_values; i++ ) {
591 if ( strequal( ctr->values[i]->valuename, name ) )
595 /* just return if we don't find it */
597 if ( i == ctr->num_values )
598 return ctr->num_values;
600 /* If 'i' was not the last element, just shift everything down one */
602 if ( i < ctr->num_values )
603 memmove(&ctr->values[i], &ctr->values[i+1],
604 sizeof(struct regval_blob*)*(ctr->num_values-i));
606 return ctr->num_values;
609 /***********************************************************************
610 Retrieve single value from the registry container.
611 No need to free memory since it is talloc'd.
612 **********************************************************************/
614 struct regval_blob* regval_ctr_getvalue(struct regval_ctr *ctr,
619 /* search for the value */
621 for ( i=0; i<ctr->num_values; i++ ) {
622 if ( strequal( ctr->values[i]->valuename, name ) )
623 return ctr->values[i];
629 int regval_ctr_get_seqnum(struct regval_ctr *ctr)
638 WERROR regval_ctr_set_seqnum(struct regval_ctr *ctr, int seqnum)
641 return WERR_INVALID_PARAM;
644 ctr->seqnum = seqnum;
649 /***********************************************************************
650 return the data_p as a uint32_t
651 **********************************************************************/
653 uint32_t regval_dword(struct regval_blob *val)
657 data = IVAL( regval_data_p(val), 0 );
662 /***********************************************************************
663 return the data_p as a character string
664 **********************************************************************/
666 const char *regval_sz(struct regval_blob *val)
668 const char *data = NULL;
669 DATA_BLOB blob = data_blob_const(regval_data_p(val), regval_size(val));
671 pull_reg_sz(talloc_tos(), &blob, &data);