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"
27 #include "dbwrap/dbwrap.h"
28 #include "dbwrap/dbwrap_rbt.h"
29 #include "../libcli/registry/util_reg.h"
32 #define DBGC_CLASS DBGC_REGISTRY
34 /* low level structure to contain registry values */
39 /* this should be encapsulated in an RPC_DATA_BLOB */
40 uint32_t size
; /* in bytes */
44 /* container for registry values */
48 struct regval_blob
**values
;
52 struct regsubkey_ctr
{
55 struct db_context
*subkeys_hash
;
59 /**********************************************************************
61 Note that the struct regsubkey_ctr and struct regval_ctr objects *must* be
62 talloc()'d since the methods use the object pointer as the talloc
63 context for internal private data.
65 There is no longer a regval_ctr_intit() and regval_ctr_destroy()
66 pair of functions. Simply talloc_zero() and TALLOC_FREE() the
69 **********************************************************************/
71 WERROR
regsubkey_ctr_init(TALLOC_CTX
*mem_ctx
, struct regsubkey_ctr
**ctr
)
74 return WERR_INVALID_PARAM
;
77 *ctr
= talloc_zero(mem_ctx
, struct regsubkey_ctr
);
82 (*ctr
)->subkeys_hash
= db_open_rbt(*ctr
);
83 if ((*ctr
)->subkeys_hash
== NULL
) {
92 * re-initialize the list of subkeys (to the emtpy list)
93 * in an already allocated regsubkey_ctr
96 WERROR
regsubkey_ctr_reinit(struct regsubkey_ctr
*ctr
)
99 return WERR_INVALID_PARAM
;
102 talloc_free(ctr
->subkeys_hash
);
103 ctr
->subkeys_hash
= db_open_rbt(ctr
);
104 W_ERROR_HAVE_NO_MEMORY(ctr
->subkeys_hash
);
106 TALLOC_FREE(ctr
->subkeys
);
108 ctr
->num_subkeys
= 0;
114 WERROR
regsubkey_ctr_set_seqnum(struct regsubkey_ctr
*ctr
, int seqnum
)
117 return WERR_INVALID_PARAM
;
120 ctr
->seqnum
= seqnum
;
125 int regsubkey_ctr_get_seqnum(struct regsubkey_ctr
*ctr
)
134 static WERROR
regsubkey_ctr_hash_keyname(struct regsubkey_ctr
*ctr
,
140 werr
= ntstatus_to_werror(dbwrap_store_bystring_upper(ctr
->subkeys_hash
,
142 make_tdb_data((uint8_t *)&idx
,
145 if (!W_ERROR_IS_OK(werr
)) {
146 DEBUG(1, ("error hashing new key '%s' in container: %s\n",
147 keyname
, win_errstr(werr
)));
153 static WERROR
regsubkey_ctr_unhash_keyname(struct regsubkey_ctr
*ctr
,
158 werr
= ntstatus_to_werror(dbwrap_delete_bystring_upper(ctr
->subkeys_hash
,
160 if (!W_ERROR_IS_OK(werr
)) {
161 DEBUG(1, ("error unhashing key '%s' in container: %s\n",
162 keyname
, win_errstr(werr
)));
168 static WERROR
regsubkey_ctr_index_for_keyname(struct regsubkey_ctr
*ctr
,
175 if ((ctr
== NULL
) || (keyname
== NULL
)) {
176 return WERR_INVALID_PARAM
;
179 status
= dbwrap_fetch_bystring_upper(ctr
->subkeys_hash
, ctr
, keyname
,
181 if (!NT_STATUS_IS_OK(status
)) {
182 return WERR_NOT_FOUND
;
185 if (data
.dsize
!= sizeof(*idx
)) {
186 talloc_free(data
.dptr
);
187 return WERR_INVALID_DATATYPE
;
191 *idx
= *(uint32_t *)data
.dptr
;
194 talloc_free(data
.dptr
);
198 /***********************************************************************
199 Add a new key to the array
200 **********************************************************************/
202 WERROR
regsubkey_ctr_addkey( struct regsubkey_ctr
*ctr
, const char *keyname
)
211 /* make sure the keyname is not already there */
213 if ( regsubkey_ctr_key_exists( ctr
, keyname
) ) {
217 if (!(newkeys
= talloc_realloc(ctr
, ctr
->subkeys
, char *,
218 ctr
->num_subkeys
+1))) {
222 ctr
->subkeys
= newkeys
;
224 if (!(ctr
->subkeys
[ctr
->num_subkeys
] = talloc_strdup(ctr
->subkeys
,
227 * Don't shrink the new array again, this wastes a pointer
232 werr
= regsubkey_ctr_hash_keyname(ctr
, keyname
, ctr
->num_subkeys
);
233 W_ERROR_NOT_OK_RETURN(werr
);
240 /***********************************************************************
241 Delete a key from the array
242 **********************************************************************/
244 WERROR
regsubkey_ctr_delkey( struct regsubkey_ctr
*ctr
, const char *keyname
)
249 if (keyname
== NULL
) {
250 return WERR_INVALID_PARAM
;
253 /* make sure the keyname is actually already there */
255 werr
= regsubkey_ctr_index_for_keyname(ctr
, keyname
, &idx
);
256 W_ERROR_NOT_OK_RETURN(werr
);
258 werr
= regsubkey_ctr_unhash_keyname(ctr
, keyname
);
259 W_ERROR_NOT_OK_RETURN(werr
);
261 /* update if we have any keys left */
263 if (idx
< ctr
->num_subkeys
) {
264 memmove(&ctr
->subkeys
[idx
], &ctr
->subkeys
[idx
+1],
265 sizeof(char *) * (ctr
->num_subkeys
- idx
));
267 /* we have to re-hash rest of the array... :-( */
268 for (j
= idx
; j
< ctr
->num_subkeys
; j
++) {
269 werr
= regsubkey_ctr_hash_keyname(ctr
, ctr
->subkeys
[j
], j
);
270 W_ERROR_NOT_OK_RETURN(werr
);
277 /***********************************************************************
278 Check for the existance of a key
279 **********************************************************************/
281 bool regsubkey_ctr_key_exists( struct regsubkey_ctr
*ctr
, const char *keyname
)
289 werr
= regsubkey_ctr_index_for_keyname(ctr
, keyname
, NULL
);
290 if (!W_ERROR_IS_OK(werr
)) {
297 /***********************************************************************
298 How many keys does the container hold ?
299 **********************************************************************/
301 int regsubkey_ctr_numkeys( struct regsubkey_ctr
*ctr
)
303 return ctr
->num_subkeys
;
306 /***********************************************************************
307 Retreive a specific key string
308 **********************************************************************/
310 char* regsubkey_ctr_specific_key( struct regsubkey_ctr
*ctr
, uint32_t key_index
)
312 if ( ! (key_index
< ctr
->num_subkeys
) )
315 return ctr
->subkeys
[key_index
];
319 * Utility functions for struct regval_ctr
323 * allocate a regval_ctr structure.
325 WERROR
regval_ctr_init(TALLOC_CTX
*mem_ctx
, struct regval_ctr
**ctr
)
328 return WERR_INVALID_PARAM
;
331 *ctr
= talloc_zero(mem_ctx
, struct regval_ctr
);
339 /***********************************************************************
340 How many keys does the container hold ?
341 **********************************************************************/
343 int regval_ctr_numvals(struct regval_ctr
*ctr
)
345 return ctr
->num_values
;
348 /***********************************************************************
349 allocate memory for and duplicate a struct regval_blob.
350 This is malloc'd memory so the caller should free it when done
351 **********************************************************************/
353 struct regval_blob
* dup_registry_value(struct regval_blob
*val
)
355 struct regval_blob
*copy
= NULL
;
360 if ( !(copy
= SMB_MALLOC_P( struct regval_blob
)) ) {
361 DEBUG(0,("dup_registry_value: malloc() failed!\n"));
365 /* copy all the non-pointer initial data */
367 memcpy( copy
, val
, sizeof(struct regval_blob
) );
372 if ( val
->data_p
&& val
->size
)
374 if ( !(copy
->data_p
= (uint8_t *)memdup( val
->data_p
,
376 DEBUG(0,("dup_registry_value: memdup() failed for [%d] "
377 "bytes!\n", val
->size
));
381 copy
->size
= val
->size
;
387 /**********************************************************************
388 free the memory allocated to a struct regval_blob
389 *********************************************************************/
391 void free_registry_value(struct regval_blob
*val
)
396 SAFE_FREE( val
->data_p
);
402 /**********************************************************************
403 *********************************************************************/
405 uint8_t* regval_data_p(struct regval_blob
*val
)
410 /**********************************************************************
411 *********************************************************************/
413 uint32_t regval_size(struct regval_blob
*val
)
418 /**********************************************************************
419 *********************************************************************/
421 char* regval_name(struct regval_blob
*val
)
423 return val
->valuename
;
426 /**********************************************************************
427 *********************************************************************/
429 uint32_t regval_type(struct regval_blob
*val
)
434 /***********************************************************************
435 Retreive a pointer to a specific value. Caller shoud dup the structure
436 since this memory will go away when the ctr is free()'d
437 **********************************************************************/
439 struct regval_blob
*regval_ctr_specific_value(struct regval_ctr
*ctr
,
442 if ( !(idx
< ctr
->num_values
) )
445 return ctr
->values
[idx
];
448 /***********************************************************************
449 Check for the existance of a value
450 **********************************************************************/
452 bool regval_ctr_key_exists(struct regval_ctr
*ctr
, const char *value
)
456 for ( i
=0; i
<ctr
->num_values
; i
++ ) {
457 if ( strequal( ctr
->values
[i
]->valuename
, value
) )
464 /***********************************************************************
465 * compose a struct regval_blob from input data
466 **********************************************************************/
468 struct regval_blob
*regval_compose(TALLOC_CTX
*ctx
, const char *name
,
470 const uint8_t *data_p
, size_t size
)
472 struct regval_blob
*regval
= talloc(ctx
, struct regval_blob
);
474 if (regval
== NULL
) {
478 fstrcpy(regval
->valuename
, name
);
481 regval
->data_p
= (uint8_t *)talloc_memdup(regval
, data_p
, size
);
482 if (!regval
->data_p
) {
487 regval
->data_p
= NULL
;
494 /***********************************************************************
495 Add a new registry value to the array
496 **********************************************************************/
498 int regval_ctr_addvalue(struct regval_ctr
*ctr
, const char *name
, uint32_t type
,
499 const uint8_t *data_p
, size_t size
)
502 return ctr
->num_values
;
504 /* Delete the current value (if it exists) and add the new one */
506 regval_ctr_delvalue( ctr
, name
);
508 /* allocate a slot in the array of pointers */
510 if ( ctr
->num_values
== 0 ) {
511 ctr
->values
= talloc( ctr
, struct regval_blob
*);
513 ctr
->values
= talloc_realloc(ctr
, ctr
->values
,
514 struct regval_blob
*,
523 /* allocate a new value and store the pointer in the arrya */
525 ctr
->values
[ctr
->num_values
] = regval_compose(ctr
, name
, type
, data_p
,
527 if (ctr
->values
[ctr
->num_values
] == NULL
) {
533 return ctr
->num_values
;
536 /***********************************************************************
537 Add a new registry SZ value to the array
538 **********************************************************************/
540 int regval_ctr_addvalue_sz(struct regval_ctr
*ctr
, const char *name
, const char *data
)
544 if (!push_reg_sz(ctr
, &blob
, data
)) {
548 return regval_ctr_addvalue(ctr
, name
, REG_SZ
,
549 (const uint8_t *)blob
.data
,
553 /***********************************************************************
554 Add a new registry MULTI_SZ value to the array
555 **********************************************************************/
557 int regval_ctr_addvalue_multi_sz(struct regval_ctr
*ctr
, const char *name
, const char **data
)
561 if (!push_reg_multi_sz(ctr
, &blob
, data
)) {
565 return regval_ctr_addvalue(ctr
, name
, REG_MULTI_SZ
,
566 (const uint8_t *)blob
.data
,
570 /***********************************************************************
571 Add a new registry value to the array
572 **********************************************************************/
574 int regval_ctr_copyvalue(struct regval_ctr
*ctr
, struct regval_blob
*val
)
577 regval_ctr_addvalue(ctr
, val
->valuename
, val
->type
,
578 (uint8_t *)val
->data_p
, val
->size
);
581 return ctr
->num_values
;
584 /***********************************************************************
585 Delete a single value from the registry container.
586 No need to free memory since it is talloc'd.
587 **********************************************************************/
589 int regval_ctr_delvalue(struct regval_ctr
*ctr
, const char *name
)
593 for ( i
=0; i
<ctr
->num_values
; i
++ ) {
594 if ( strequal( ctr
->values
[i
]->valuename
, name
) )
598 /* just return if we don't find it */
600 if ( i
== ctr
->num_values
)
601 return ctr
->num_values
;
603 /* If 'i' was not the last element, just shift everything down one */
605 if ( i
< ctr
->num_values
)
606 memmove(&ctr
->values
[i
], &ctr
->values
[i
+1],
607 sizeof(struct regval_blob
*)*(ctr
->num_values
-i
));
609 return ctr
->num_values
;
612 /***********************************************************************
613 Retrieve single value from the registry container.
614 No need to free memory since it is talloc'd.
615 **********************************************************************/
617 struct regval_blob
* regval_ctr_getvalue(struct regval_ctr
*ctr
,
622 /* search for the value */
624 for ( i
=0; i
<ctr
->num_values
; i
++ ) {
625 if ( strequal( ctr
->values
[i
]->valuename
, name
) )
626 return ctr
->values
[i
];
632 int regval_ctr_get_seqnum(struct regval_ctr
*ctr
)
641 WERROR
regval_ctr_set_seqnum(struct regval_ctr
*ctr
, int seqnum
)
644 return WERR_INVALID_PARAM
;
647 ctr
->seqnum
= seqnum
;
652 /***********************************************************************
653 return the data_p as a uint32_t
654 **********************************************************************/
656 uint32_t regval_dword(struct regval_blob
*val
)
660 data
= IVAL( regval_data_p(val
), 0 );
665 /***********************************************************************
666 return the data_p as a character string
667 **********************************************************************/
669 const char *regval_sz(struct regval_blob
*val
)
671 const char *data
= NULL
;
672 DATA_BLOB blob
= data_blob_const(regval_data_p(val
), regval_size(val
));
674 pull_reg_sz(talloc_tos(), &blob
, &data
);