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"
30 #include "lib/util/string_wrappers.h"
33 #define DBGC_CLASS DBGC_REGISTRY
35 /* low level structure to contain registry values */
40 /* this should be encapsulated in an RPC_DATA_BLOB */
41 uint32_t size
; /* in bytes */
45 /* container for registry values */
49 struct regval_blob
**values
;
53 struct regsubkey_ctr
{
56 struct db_context
*subkeys_hash
;
60 /**********************************************************************
62 Note that the struct regsubkey_ctr and struct regval_ctr objects *must* be
63 talloc()'d since the methods use the object pointer as the talloc
64 context for internal private data.
66 There is no longer a regval_ctr_init() and regval_ctr_destroy()
67 pair of functions. Simply talloc_zero() and TALLOC_FREE() the
70 **********************************************************************/
72 WERROR
regsubkey_ctr_init(TALLOC_CTX
*mem_ctx
, struct regsubkey_ctr
**ctr
)
75 return WERR_INVALID_PARAMETER
;
78 *ctr
= talloc_zero(mem_ctx
, struct regsubkey_ctr
);
80 return WERR_NOT_ENOUGH_MEMORY
;
83 (*ctr
)->subkeys_hash
= db_open_rbt(*ctr
);
84 if ((*ctr
)->subkeys_hash
== NULL
) {
86 return WERR_NOT_ENOUGH_MEMORY
;
93 * re-initialize the list of subkeys (to the empty list)
94 * in an already allocated regsubkey_ctr
97 WERROR
regsubkey_ctr_reinit(struct regsubkey_ctr
*ctr
)
100 return WERR_INVALID_PARAMETER
;
103 talloc_free(ctr
->subkeys_hash
);
104 ctr
->subkeys_hash
= db_open_rbt(ctr
);
105 W_ERROR_HAVE_NO_MEMORY(ctr
->subkeys_hash
);
107 TALLOC_FREE(ctr
->subkeys
);
109 ctr
->num_subkeys
= 0;
115 WERROR
regsubkey_ctr_set_seqnum(struct regsubkey_ctr
*ctr
, int seqnum
)
118 return WERR_INVALID_PARAMETER
;
121 ctr
->seqnum
= seqnum
;
126 int regsubkey_ctr_get_seqnum(struct regsubkey_ctr
*ctr
)
135 static WERROR
regsubkey_ctr_hash_keyname(struct regsubkey_ctr
*ctr
,
141 werr
= ntstatus_to_werror(dbwrap_store_bystring_upper(ctr
->subkeys_hash
,
143 make_tdb_data((uint8_t *)&idx
,
146 if (!W_ERROR_IS_OK(werr
)) {
147 DEBUG(1, ("error hashing new key '%s' in container: %s\n",
148 keyname
, win_errstr(werr
)));
154 static WERROR
regsubkey_ctr_unhash_keyname(struct regsubkey_ctr
*ctr
,
159 werr
= ntstatus_to_werror(dbwrap_delete_bystring_upper(ctr
->subkeys_hash
,
161 if (!W_ERROR_IS_OK(werr
)) {
162 DEBUG(1, ("error unhashing key '%s' in container: %s\n",
163 keyname
, win_errstr(werr
)));
169 static WERROR
regsubkey_ctr_index_for_keyname(struct regsubkey_ctr
*ctr
,
176 if ((ctr
== NULL
) || (keyname
== NULL
)) {
177 return WERR_INVALID_PARAMETER
;
180 status
= dbwrap_fetch_bystring_upper(ctr
->subkeys_hash
, ctr
, keyname
,
182 if (!NT_STATUS_IS_OK(status
)) {
183 return WERR_NOT_FOUND
;
186 if (data
.dsize
!= sizeof(*idx
)) {
187 talloc_free(data
.dptr
);
188 return WERR_INVALID_DATATYPE
;
192 memcpy(idx
, data
.dptr
, sizeof(*idx
));
195 talloc_free(data
.dptr
);
199 /***********************************************************************
200 Add a new key to the array
201 **********************************************************************/
203 WERROR
regsubkey_ctr_addkey( struct regsubkey_ctr
*ctr
, const char *keyname
)
212 /* make sure the keyname is not already there */
214 if ( regsubkey_ctr_key_exists( ctr
, keyname
) ) {
218 if (!(newkeys
= talloc_realloc(ctr
, ctr
->subkeys
, char *,
219 ctr
->num_subkeys
+1))) {
220 return WERR_NOT_ENOUGH_MEMORY
;
223 ctr
->subkeys
= newkeys
;
225 if (!(ctr
->subkeys
[ctr
->num_subkeys
] = talloc_strdup(ctr
->subkeys
,
228 * Don't shrink the new array again, this wastes a pointer
230 return WERR_NOT_ENOUGH_MEMORY
;
233 werr
= regsubkey_ctr_hash_keyname(ctr
, keyname
, ctr
->num_subkeys
);
234 W_ERROR_NOT_OK_RETURN(werr
);
241 /***********************************************************************
242 Delete a key from the array
243 **********************************************************************/
245 WERROR
regsubkey_ctr_delkey( struct regsubkey_ctr
*ctr
, const char *keyname
)
250 if (keyname
== NULL
) {
251 return WERR_INVALID_PARAMETER
;
254 /* make sure the keyname is actually already there */
256 werr
= regsubkey_ctr_index_for_keyname(ctr
, keyname
, &idx
);
257 W_ERROR_NOT_OK_RETURN(werr
);
259 werr
= regsubkey_ctr_unhash_keyname(ctr
, keyname
);
260 W_ERROR_NOT_OK_RETURN(werr
);
262 /* update if we have any keys left */
264 if (idx
< ctr
->num_subkeys
) {
265 memmove(&ctr
->subkeys
[idx
], &ctr
->subkeys
[idx
+1],
266 sizeof(char *) * (ctr
->num_subkeys
- idx
));
268 /* we have to re-hash rest of the array... :-( */
269 for (j
= idx
; j
< ctr
->num_subkeys
; j
++) {
270 werr
= regsubkey_ctr_hash_keyname(ctr
, ctr
->subkeys
[j
], j
);
271 W_ERROR_NOT_OK_RETURN(werr
);
278 /***********************************************************************
279 Check for the existence of a key
280 **********************************************************************/
282 bool regsubkey_ctr_key_exists( struct regsubkey_ctr
*ctr
, const char *keyname
)
290 werr
= regsubkey_ctr_index_for_keyname(ctr
, keyname
, NULL
);
291 if (!W_ERROR_IS_OK(werr
)) {
298 /***********************************************************************
299 How many keys does the container hold ?
300 **********************************************************************/
302 uint32_t regsubkey_ctr_numkeys( struct regsubkey_ctr
*ctr
)
304 return ctr
->num_subkeys
;
307 /***********************************************************************
308 Retrieve a specific key string
309 **********************************************************************/
311 char* regsubkey_ctr_specific_key( struct regsubkey_ctr
*ctr
, uint32_t key_index
)
313 if ( ! (key_index
< ctr
->num_subkeys
) )
316 return ctr
->subkeys
[key_index
];
320 * Utility functions for struct regval_ctr
324 * allocate a regval_ctr structure.
326 WERROR
regval_ctr_init(TALLOC_CTX
*mem_ctx
, struct regval_ctr
**ctr
)
329 return WERR_INVALID_PARAMETER
;
332 *ctr
= talloc_zero(mem_ctx
, struct regval_ctr
);
334 return WERR_NOT_ENOUGH_MEMORY
;
340 /***********************************************************************
341 How many keys does the container hold ?
342 **********************************************************************/
344 uint32_t regval_ctr_numvals(struct regval_ctr
*ctr
)
346 return ctr
->num_values
;
349 /**********************************************************************
350 *********************************************************************/
352 uint8_t* regval_data_p(struct regval_blob
*val
)
357 /**********************************************************************
358 *********************************************************************/
360 uint32_t regval_size(struct regval_blob
*val
)
365 /**********************************************************************
366 *********************************************************************/
368 char* regval_name(struct regval_blob
*val
)
370 return val
->valuename
;
373 /**********************************************************************
374 *********************************************************************/
376 uint32_t regval_type(struct regval_blob
*val
)
381 /***********************************************************************
382 Retrieve a pointer to a specific value. Caller should dup the structure
383 since this memory will go away when the ctr is free()'d
384 **********************************************************************/
386 struct regval_blob
*regval_ctr_specific_value(struct regval_ctr
*ctr
,
389 if ( !(idx
< ctr
->num_values
) )
392 return ctr
->values
[idx
];
395 /***********************************************************************
396 Check for the existence of a value
397 **********************************************************************/
399 bool regval_ctr_value_exists(struct regval_ctr
*ctr
, const char *value
)
403 for ( i
=0; i
<ctr
->num_values
; i
++ ) {
404 if ( strequal( ctr
->values
[i
]->valuename
, value
) )
412 * Get a value by its name
414 struct regval_blob
*regval_ctr_value_byname(struct regval_ctr
*ctr
,
419 for (i
= 0; i
< ctr
->num_values
; i
++) {
420 if (strequal(ctr
->values
[i
]->valuename
, value
)) {
421 return ctr
->values
[i
];
429 /***********************************************************************
430 * compose a struct regval_blob from input data
431 **********************************************************************/
433 struct regval_blob
*regval_compose(TALLOC_CTX
*ctx
, const char *name
,
435 const uint8_t *data_p
, size_t size
)
437 struct regval_blob
*regval
= talloc(ctx
, struct regval_blob
);
439 if (regval
== NULL
) {
443 fstrcpy(regval
->valuename
, name
);
446 regval
->data_p
= (uint8_t *)talloc_memdup(regval
, data_p
, size
);
447 if (!regval
->data_p
) {
452 regval
->data_p
= NULL
;
459 /***********************************************************************
460 Add a new registry value to the array
461 **********************************************************************/
463 int regval_ctr_addvalue(struct regval_ctr
*ctr
, const char *name
, uint32_t type
,
464 const uint8_t *data_p
, size_t size
)
467 return ctr
->num_values
;
469 /* Delete the current value (if it exists) and add the new one */
471 regval_ctr_delvalue( ctr
, name
);
473 /* allocate a slot in the array of pointers */
475 if ( ctr
->num_values
== 0 ) {
476 ctr
->values
= talloc( ctr
, struct regval_blob
*);
478 ctr
->values
= talloc_realloc(ctr
, ctr
->values
,
479 struct regval_blob
*,
488 /* allocate a new value and store the pointer in the array */
490 ctr
->values
[ctr
->num_values
] = regval_compose(ctr
, name
, type
, data_p
,
492 if (ctr
->values
[ctr
->num_values
] == NULL
) {
498 return ctr
->num_values
;
501 /***********************************************************************
502 Add a new registry SZ value to the array
503 **********************************************************************/
505 int regval_ctr_addvalue_sz(struct regval_ctr
*ctr
, const char *name
, const char *data
)
509 if (!push_reg_sz(ctr
, &blob
, data
)) {
513 return regval_ctr_addvalue(ctr
, name
, REG_SZ
,
514 (const uint8_t *)blob
.data
,
518 /***********************************************************************
519 Add a new registry MULTI_SZ value to the array
520 **********************************************************************/
522 int regval_ctr_addvalue_multi_sz(struct regval_ctr
*ctr
, const char *name
, const char **data
)
526 if (!push_reg_multi_sz(ctr
, &blob
, data
)) {
530 return regval_ctr_addvalue(ctr
, name
, REG_MULTI_SZ
,
531 (const uint8_t *)blob
.data
,
535 /***********************************************************************
536 Add a new registry value to the array
537 **********************************************************************/
539 int regval_ctr_copyvalue(struct regval_ctr
*ctr
, struct regval_blob
*val
)
542 regval_ctr_addvalue(ctr
, val
->valuename
, val
->type
,
543 (uint8_t *)val
->data_p
, val
->size
);
546 return ctr
->num_values
;
549 /***********************************************************************
550 Delete a single value from the registry container.
551 No need to free memory since it is talloc'd.
552 **********************************************************************/
554 int regval_ctr_delvalue(struct regval_ctr
*ctr
, const char *name
)
558 for ( i
=0; i
<ctr
->num_values
; i
++ ) {
559 if ( strequal( ctr
->values
[i
]->valuename
, name
) )
563 /* just return if we don't find it */
565 if ( i
== ctr
->num_values
)
566 return ctr
->num_values
;
568 /* If 'i' was not the last element, just shift everything down one */
570 if ( i
< ctr
->num_values
)
571 memmove(&ctr
->values
[i
], &ctr
->values
[i
+1],
572 sizeof(struct regval_blob
*)*(ctr
->num_values
-i
));
574 return ctr
->num_values
;
577 /***********************************************************************
578 Retrieve single value from the registry container.
579 No need to free memory since it is talloc'd.
580 **********************************************************************/
582 struct regval_blob
* regval_ctr_getvalue(struct regval_ctr
*ctr
,
587 /* search for the value */
589 for ( i
=0; i
<ctr
->num_values
; i
++ ) {
590 if ( strequal( ctr
->values
[i
]->valuename
, name
) )
591 return ctr
->values
[i
];
597 int regval_ctr_get_seqnum(struct regval_ctr
*ctr
)
606 WERROR
regval_ctr_set_seqnum(struct regval_ctr
*ctr
, int seqnum
)
609 return WERR_INVALID_PARAMETER
;
612 ctr
->seqnum
= seqnum
;