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 /* low level structure to contain registry values */
33 /* this should be encapsulated in an RPC_DATA_BLOB */
34 uint32 size
; /* in bytes */
38 /* container for registry values */
42 struct regval_blob
**values
;
46 struct regsubkey_ctr
{
49 struct db_context
*subkeys_hash
;
53 /**********************************************************************
55 Note that the struct regsubkey_ctr and struct regval_ctr objects *must* be
56 talloc()'d since the methods use the object pointer as the talloc
57 context for internal private data.
59 There is no longer a regval_ctr_intit() and regval_ctr_destroy()
60 pair of functions. Simply TALLOC_ZERO_P() and TALLOC_FREE() the
63 **********************************************************************/
65 WERROR
regsubkey_ctr_init(TALLOC_CTX
*mem_ctx
, struct regsubkey_ctr
**ctr
)
68 return WERR_INVALID_PARAM
;
71 *ctr
= talloc_zero(mem_ctx
, struct regsubkey_ctr
);
76 (*ctr
)->subkeys_hash
= db_open_rbt(*ctr
);
77 if ((*ctr
)->subkeys_hash
== NULL
) {
86 * re-initialize the list of subkeys (to the emtpy list)
87 * in an already allocated regsubkey_ctr
90 WERROR
regsubkey_ctr_reinit(struct regsubkey_ctr
*ctr
)
93 return WERR_INVALID_PARAM
;
96 talloc_free(ctr
->subkeys_hash
);
97 ctr
->subkeys_hash
= db_open_rbt(ctr
);
98 W_ERROR_HAVE_NO_MEMORY(ctr
->subkeys_hash
);
100 TALLOC_FREE(ctr
->subkeys
);
102 ctr
->num_subkeys
= 0;
108 WERROR
regsubkey_ctr_set_seqnum(struct regsubkey_ctr
*ctr
, int seqnum
)
111 return WERR_INVALID_PARAM
;
114 ctr
->seqnum
= seqnum
;
119 int regsubkey_ctr_get_seqnum(struct regsubkey_ctr
*ctr
)
128 static WERROR
regsubkey_ctr_hash_keyname(struct regsubkey_ctr
*ctr
,
134 werr
= ntstatus_to_werror(dbwrap_store_bystring_upper(ctr
->subkeys_hash
,
136 make_tdb_data((uint8
*)&idx
,
139 if (!W_ERROR_IS_OK(werr
)) {
140 DEBUG(1, ("error hashing new key '%s' in container: %s\n",
141 keyname
, win_errstr(werr
)));
147 static WERROR
regsubkey_ctr_unhash_keyname(struct regsubkey_ctr
*ctr
,
152 werr
= ntstatus_to_werror(dbwrap_delete_bystring_upper(ctr
->subkeys_hash
,
154 if (!W_ERROR_IS_OK(werr
)) {
155 DEBUG(1, ("error unhashing key '%s' in container: %s\n",
156 keyname
, win_errstr(werr
)));
162 static WERROR
regsubkey_ctr_index_for_keyname(struct regsubkey_ctr
*ctr
,
168 if ((ctr
== NULL
) || (keyname
== NULL
)) {
169 return WERR_INVALID_PARAM
;
172 data
= dbwrap_fetch_bystring_upper(ctr
->subkeys_hash
, ctr
, keyname
);
173 if (data
.dptr
== NULL
) {
174 return WERR_NOT_FOUND
;
177 if (data
.dsize
!= sizeof(*idx
)) {
178 talloc_free(data
.dptr
);
179 return WERR_INVALID_DATATYPE
;
183 *idx
= *(uint32
*)data
.dptr
;
186 talloc_free(data
.dptr
);
190 /***********************************************************************
191 Add a new key to the array
192 **********************************************************************/
194 WERROR
regsubkey_ctr_addkey( struct regsubkey_ctr
*ctr
, const char *keyname
)
203 /* make sure the keyname is not already there */
205 if ( regsubkey_ctr_key_exists( ctr
, keyname
) ) {
209 if (!(newkeys
= TALLOC_REALLOC_ARRAY(ctr
, ctr
->subkeys
, char *,
210 ctr
->num_subkeys
+1))) {
214 ctr
->subkeys
= newkeys
;
216 if (!(ctr
->subkeys
[ctr
->num_subkeys
] = talloc_strdup(ctr
->subkeys
,
219 * Don't shrink the new array again, this wastes a pointer
224 werr
= regsubkey_ctr_hash_keyname(ctr
, keyname
, ctr
->num_subkeys
);
225 W_ERROR_NOT_OK_RETURN(werr
);
232 /***********************************************************************
233 Delete a key from the array
234 **********************************************************************/
236 WERROR
regsubkey_ctr_delkey( struct regsubkey_ctr
*ctr
, const char *keyname
)
241 if (keyname
== NULL
) {
242 return WERR_INVALID_PARAM
;
245 /* make sure the keyname is actually already there */
247 werr
= regsubkey_ctr_index_for_keyname(ctr
, keyname
, &idx
);
248 W_ERROR_NOT_OK_RETURN(werr
);
250 werr
= regsubkey_ctr_unhash_keyname(ctr
, keyname
);
251 W_ERROR_NOT_OK_RETURN(werr
);
253 /* update if we have any keys left */
255 if (idx
< ctr
->num_subkeys
) {
256 memmove(&ctr
->subkeys
[idx
], &ctr
->subkeys
[idx
+1],
257 sizeof(char *) * (ctr
->num_subkeys
- idx
));
259 /* we have to re-hash rest of the array... :-( */
260 for (j
= idx
; j
< ctr
->num_subkeys
; j
++) {
261 werr
= regsubkey_ctr_hash_keyname(ctr
, ctr
->subkeys
[j
], j
);
262 W_ERROR_NOT_OK_RETURN(werr
);
269 /***********************************************************************
270 Check for the existance of a key
271 **********************************************************************/
273 bool regsubkey_ctr_key_exists( struct regsubkey_ctr
*ctr
, const char *keyname
)
281 werr
= regsubkey_ctr_index_for_keyname(ctr
, keyname
, NULL
);
282 if (!W_ERROR_IS_OK(werr
)) {
289 /***********************************************************************
290 How many keys does the container hold ?
291 **********************************************************************/
293 int regsubkey_ctr_numkeys( struct regsubkey_ctr
*ctr
)
295 return ctr
->num_subkeys
;
298 /***********************************************************************
299 Retreive a specific key string
300 **********************************************************************/
302 char* regsubkey_ctr_specific_key( struct regsubkey_ctr
*ctr
, uint32_t key_index
)
304 if ( ! (key_index
< ctr
->num_subkeys
) )
307 return ctr
->subkeys
[key_index
];
311 * Utility functions for struct regval_ctr
315 * allocate a regval_ctr structure.
317 WERROR
regval_ctr_init(TALLOC_CTX
*mem_ctx
, struct regval_ctr
**ctr
)
320 return WERR_INVALID_PARAM
;
323 *ctr
= talloc_zero(mem_ctx
, struct regval_ctr
);
331 /***********************************************************************
332 How many keys does the container hold ?
333 **********************************************************************/
335 int regval_ctr_numvals(struct regval_ctr
*ctr
)
337 return ctr
->num_values
;
340 /***********************************************************************
341 allocate memory for and duplicate a struct regval_blob.
342 This is malloc'd memory so the caller should free it when done
343 **********************************************************************/
345 struct regval_blob
* dup_registry_value(struct regval_blob
*val
)
347 struct regval_blob
*copy
= NULL
;
352 if ( !(copy
= SMB_MALLOC_P( struct regval_blob
)) ) {
353 DEBUG(0,("dup_registry_value: malloc() failed!\n"));
357 /* copy all the non-pointer initial data */
359 memcpy( copy
, val
, sizeof(struct regval_blob
) );
364 if ( val
->data_p
&& val
->size
)
366 if ( !(copy
->data_p
= (uint8
*)memdup( val
->data_p
,
368 DEBUG(0,("dup_registry_value: memdup() failed for [%d] "
369 "bytes!\n", val
->size
));
373 copy
->size
= val
->size
;
379 /**********************************************************************
380 free the memory allocated to a struct regval_blob
381 *********************************************************************/
383 void free_registry_value(struct regval_blob
*val
)
388 SAFE_FREE( val
->data_p
);
394 /**********************************************************************
395 *********************************************************************/
397 uint8
* regval_data_p(struct regval_blob
*val
)
402 /**********************************************************************
403 *********************************************************************/
405 uint32
regval_size(struct regval_blob
*val
)
410 /**********************************************************************
411 *********************************************************************/
413 char* regval_name(struct regval_blob
*val
)
415 return val
->valuename
;
418 /**********************************************************************
419 *********************************************************************/
421 uint32
regval_type(struct regval_blob
*val
)
426 /***********************************************************************
427 Retreive a pointer to a specific value. Caller shoud dup the structure
428 since this memory will go away when the ctr is free()'d
429 **********************************************************************/
431 struct regval_blob
*regval_ctr_specific_value(struct regval_ctr
*ctr
,
434 if ( !(idx
< ctr
->num_values
) )
437 return ctr
->values
[idx
];
440 /***********************************************************************
441 Check for the existance of a value
442 **********************************************************************/
444 bool regval_ctr_key_exists(struct regval_ctr
*ctr
, const char *value
)
448 for ( i
=0; i
<ctr
->num_values
; i
++ ) {
449 if ( strequal( ctr
->values
[i
]->valuename
, value
) )
456 /***********************************************************************
457 * compose a struct regval_blob from input data
458 **********************************************************************/
460 struct regval_blob
*regval_compose(TALLOC_CTX
*ctx
, const char *name
,
462 const uint8
*data_p
, size_t size
)
464 struct regval_blob
*regval
= TALLOC_P(ctx
, struct regval_blob
);
466 if (regval
== NULL
) {
470 fstrcpy(regval
->valuename
, name
);
473 regval
->data_p
= (uint8
*)TALLOC_MEMDUP(regval
, data_p
, size
);
474 if (!regval
->data_p
) {
479 regval
->data_p
= NULL
;
486 /***********************************************************************
487 Add a new registry value to the array
488 **********************************************************************/
490 int regval_ctr_addvalue(struct regval_ctr
*ctr
, const char *name
, uint16 type
,
491 const uint8
*data_p
, size_t size
)
494 return ctr
->num_values
;
496 /* Delete the current value (if it exists) and add the new one */
498 regval_ctr_delvalue( ctr
, name
);
500 /* allocate a slot in the array of pointers */
502 if ( ctr
->num_values
== 0 ) {
503 ctr
->values
= TALLOC_P( ctr
, struct regval_blob
*);
505 ctr
->values
= TALLOC_REALLOC_ARRAY(ctr
, ctr
->values
,
506 struct regval_blob
*,
515 /* allocate a new value and store the pointer in the arrya */
517 ctr
->values
[ctr
->num_values
] = regval_compose(ctr
, name
, type
, data_p
,
519 if (ctr
->values
[ctr
->num_values
] == NULL
) {
525 return ctr
->num_values
;
528 /***********************************************************************
529 Add a new registry SZ value to the array
530 **********************************************************************/
532 int regval_ctr_addvalue_sz(struct regval_ctr
*ctr
, const char *name
, const char *data
)
536 if (!push_reg_sz(ctr
, &blob
, data
)) {
540 return regval_ctr_addvalue(ctr
, name
, REG_SZ
,
541 (const uint8
*)blob
.data
,
545 /***********************************************************************
546 Add a new registry MULTI_SZ value to the array
547 **********************************************************************/
549 int regval_ctr_addvalue_multi_sz(struct regval_ctr
*ctr
, const char *name
, const char **data
)
553 if (!push_reg_multi_sz(ctr
, &blob
, data
)) {
557 return regval_ctr_addvalue(ctr
, name
, REG_MULTI_SZ
,
558 (const uint8
*)blob
.data
,
562 /***********************************************************************
563 Add a new registry value to the array
564 **********************************************************************/
566 int regval_ctr_copyvalue(struct regval_ctr
*ctr
, struct regval_blob
*val
)
569 regval_ctr_addvalue(ctr
, val
->valuename
, val
->type
,
570 (uint8
*)val
->data_p
, val
->size
);
573 return ctr
->num_values
;
576 /***********************************************************************
577 Delete a single value from the registry container.
578 No need to free memory since it is talloc'd.
579 **********************************************************************/
581 int regval_ctr_delvalue(struct regval_ctr
*ctr
, const char *name
)
585 for ( i
=0; i
<ctr
->num_values
; i
++ ) {
586 if ( strequal( ctr
->values
[i
]->valuename
, name
) )
590 /* just return if we don't find it */
592 if ( i
== ctr
->num_values
)
593 return ctr
->num_values
;
595 /* If 'i' was not the last element, just shift everything down one */
597 if ( i
< ctr
->num_values
)
598 memmove(&ctr
->values
[i
], &ctr
->values
[i
+1],
599 sizeof(struct regval_blob
*)*(ctr
->num_values
-i
));
601 return ctr
->num_values
;
604 /***********************************************************************
605 Retrieve single value from the registry container.
606 No need to free memory since it is talloc'd.
607 **********************************************************************/
609 struct regval_blob
* regval_ctr_getvalue(struct regval_ctr
*ctr
,
614 /* search for the value */
616 for ( i
=0; i
<ctr
->num_values
; i
++ ) {
617 if ( strequal( ctr
->values
[i
]->valuename
, name
) )
618 return ctr
->values
[i
];
624 int regval_ctr_get_seqnum(struct regval_ctr
*ctr
)
633 WERROR
regval_ctr_set_seqnum(struct regval_ctr
*ctr
, int seqnum
)
636 return WERR_INVALID_PARAM
;
639 ctr
->seqnum
= seqnum
;
644 /***********************************************************************
645 return the data_p as a uint32
646 **********************************************************************/
648 uint32
regval_dword(struct regval_blob
*val
)
652 data
= IVAL( regval_data_p(val
), 0 );
657 /***********************************************************************
658 return the data_p as a character string
659 **********************************************************************/
661 const char *regval_sz(struct regval_blob
*val
)
663 const char *data
= NULL
;
664 DATA_BLOB blob
= data_blob_const(regval_data_p(val
), regval_size(val
));
666 pull_reg_sz(talloc_tos(), &blob
, &data
);