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 struct regsubkey_ctr {
31 struct db_context *subkeys_hash;
35 /**********************************************************************
37 Note that the struct regsubkey_ctr and struct regval_ctr objects *must* be
38 talloc()'d since the methods use the object pointer as the talloc
39 context for internal private data.
41 There is no longer a regval_ctr_intit() and regval_ctr_destroy()
42 pair of functions. Simply TALLOC_ZERO_P() and TALLOC_FREE() the
45 **********************************************************************/
47 WERROR regsubkey_ctr_init(TALLOC_CTX *mem_ctx, struct regsubkey_ctr **ctr)
50 return WERR_INVALID_PARAM;
53 *ctr = talloc_zero(mem_ctx, struct regsubkey_ctr);
58 (*ctr)->subkeys_hash = db_open_rbt(*ctr);
59 if ((*ctr)->subkeys_hash == NULL) {
68 * re-initialize the list of subkeys (to the emtpy list)
69 * in an already allocated regsubkey_ctr
72 WERROR regsubkey_ctr_reinit(struct regsubkey_ctr *ctr)
75 return WERR_INVALID_PARAM;
78 talloc_free(ctr->subkeys_hash);
79 ctr->subkeys_hash = db_open_rbt(ctr);
80 W_ERROR_HAVE_NO_MEMORY(ctr->subkeys_hash);
82 TALLOC_FREE(ctr->subkeys);
90 WERROR regsubkey_ctr_set_seqnum(struct regsubkey_ctr *ctr, int seqnum)
93 return WERR_INVALID_PARAM;
101 int regsubkey_ctr_get_seqnum(struct regsubkey_ctr *ctr)
110 static WERROR regsubkey_ctr_hash_keyname(struct regsubkey_ctr *ctr,
116 werr = ntstatus_to_werror(dbwrap_store_bystring_upper(ctr->subkeys_hash,
118 make_tdb_data((uint8 *)&idx,
121 if (!W_ERROR_IS_OK(werr)) {
122 DEBUG(1, ("error hashing new key '%s' in container: %s\n",
123 keyname, win_errstr(werr)));
129 static WERROR regsubkey_ctr_unhash_keyname(struct regsubkey_ctr *ctr,
134 werr = ntstatus_to_werror(dbwrap_delete_bystring_upper(ctr->subkeys_hash,
136 if (!W_ERROR_IS_OK(werr)) {
137 DEBUG(1, ("error unhashing key '%s' in container: %s\n",
138 keyname, win_errstr(werr)));
144 static WERROR regsubkey_ctr_index_for_keyname(struct regsubkey_ctr *ctr,
150 if ((ctr == NULL) || (keyname == NULL)) {
151 return WERR_INVALID_PARAM;
154 data = dbwrap_fetch_bystring_upper(ctr->subkeys_hash, ctr, keyname);
155 if (data.dptr == NULL) {
156 return WERR_NOT_FOUND;
159 if (data.dsize != sizeof(*idx)) {
160 talloc_free(data.dptr);
161 return WERR_INVALID_DATATYPE;
165 *idx = *(uint32 *)data.dptr;
168 talloc_free(data.dptr);
172 /***********************************************************************
173 Add a new key to the array
174 **********************************************************************/
176 WERROR regsubkey_ctr_addkey( struct regsubkey_ctr *ctr, const char *keyname )
185 /* make sure the keyname is not already there */
187 if ( regsubkey_ctr_key_exists( ctr, keyname ) ) {
191 if (!(newkeys = TALLOC_REALLOC_ARRAY(ctr, ctr->subkeys, char *,
192 ctr->num_subkeys+1))) {
196 ctr->subkeys = newkeys;
198 if (!(ctr->subkeys[ctr->num_subkeys] = talloc_strdup(ctr->subkeys,
201 * Don't shrink the new array again, this wastes a pointer
206 werr = regsubkey_ctr_hash_keyname(ctr, keyname, ctr->num_subkeys);
207 W_ERROR_NOT_OK_RETURN(werr);
214 /***********************************************************************
215 Delete a key from the array
216 **********************************************************************/
218 WERROR regsubkey_ctr_delkey( struct regsubkey_ctr *ctr, const char *keyname )
223 if (keyname == NULL) {
224 return WERR_INVALID_PARAM;
227 /* make sure the keyname is actually already there */
229 werr = regsubkey_ctr_index_for_keyname(ctr, keyname, &idx);
230 W_ERROR_NOT_OK_RETURN(werr);
232 werr = regsubkey_ctr_unhash_keyname(ctr, keyname);
233 W_ERROR_NOT_OK_RETURN(werr);
235 /* update if we have any keys left */
237 if (idx < ctr->num_subkeys) {
238 memmove(&ctr->subkeys[idx], &ctr->subkeys[idx+1],
239 sizeof(char *) * (ctr->num_subkeys - idx));
241 /* we have to re-hash rest of the array... :-( */
242 for (j = idx; j < ctr->num_subkeys; j++) {
243 werr = regsubkey_ctr_hash_keyname(ctr, ctr->subkeys[j], j);
244 W_ERROR_NOT_OK_RETURN(werr);
251 /***********************************************************************
252 Check for the existance of a key
253 **********************************************************************/
255 bool regsubkey_ctr_key_exists( struct regsubkey_ctr *ctr, const char *keyname )
263 werr = regsubkey_ctr_index_for_keyname(ctr, keyname, NULL);
264 if (!W_ERROR_IS_OK(werr)) {
271 /***********************************************************************
272 How many keys does the container hold ?
273 **********************************************************************/
275 int regsubkey_ctr_numkeys( struct regsubkey_ctr *ctr )
277 return ctr->num_subkeys;
280 /***********************************************************************
281 Retreive a specific key string
282 **********************************************************************/
284 char* regsubkey_ctr_specific_key( struct regsubkey_ctr *ctr, uint32_t key_index )
286 if ( ! (key_index < ctr->num_subkeys) )
289 return ctr->subkeys[key_index];
293 * Utility functions for struct regval_ctr
297 * allocate a regval_ctr structure.
299 WERROR regval_ctr_init(TALLOC_CTX *mem_ctx, struct regval_ctr **ctr)
302 return WERR_INVALID_PARAM;
305 *ctr = talloc_zero(mem_ctx, struct regval_ctr);
313 /***********************************************************************
314 How many keys does the container hold ?
315 **********************************************************************/
317 int regval_ctr_numvals(struct regval_ctr *ctr)
319 return ctr->num_values;
322 /***********************************************************************
323 allocate memory for and duplicate a struct regval_blob.
324 This is malloc'd memory so the caller should free it when done
325 **********************************************************************/
327 struct regval_blob* dup_registry_value(struct regval_blob *val)
329 struct regval_blob *copy = NULL;
334 if ( !(copy = SMB_MALLOC_P( struct regval_blob)) ) {
335 DEBUG(0,("dup_registry_value: malloc() failed!\n"));
339 /* copy all the non-pointer initial data */
341 memcpy( copy, val, sizeof(struct regval_blob) );
346 if ( val->data_p && val->size )
348 if ( !(copy->data_p = (uint8 *)memdup( val->data_p,
350 DEBUG(0,("dup_registry_value: memdup() failed for [%d] "
351 "bytes!\n", val->size));
355 copy->size = val->size;
361 /**********************************************************************
362 free the memory allocated to a struct regval_blob
363 *********************************************************************/
365 void free_registry_value(struct regval_blob *val)
370 SAFE_FREE( val->data_p );
376 /**********************************************************************
377 *********************************************************************/
379 uint8* regval_data_p(struct regval_blob *val)
384 /**********************************************************************
385 *********************************************************************/
387 uint32 regval_size(struct regval_blob *val)
392 /**********************************************************************
393 *********************************************************************/
395 char* regval_name(struct regval_blob *val)
397 return val->valuename;
400 /**********************************************************************
401 *********************************************************************/
403 uint32 regval_type(struct regval_blob *val)
408 /***********************************************************************
409 Retreive a pointer to a specific value. Caller shoud dup the structure
410 since this memory will go away when the ctr is free()'d
411 **********************************************************************/
413 struct regval_blob *regval_ctr_specific_value(struct regval_ctr *ctr,
416 if ( !(idx < ctr->num_values) )
419 return ctr->values[idx];
422 /***********************************************************************
423 Check for the existance of a value
424 **********************************************************************/
426 bool regval_ctr_key_exists(struct regval_ctr *ctr, const char *value)
430 for ( i=0; i<ctr->num_values; i++ ) {
431 if ( strequal( ctr->values[i]->valuename, value) )
438 /***********************************************************************
439 * compose a struct regval_blob from input data
440 **********************************************************************/
442 struct regval_blob *regval_compose(TALLOC_CTX *ctx, const char *name,
444 const uint8 *data_p, size_t size)
446 struct regval_blob *regval = TALLOC_P(ctx, struct regval_blob);
448 if (regval == NULL) {
452 fstrcpy(regval->valuename, name);
455 regval->data_p = (uint8 *)TALLOC_MEMDUP(regval, data_p, size);
456 if (!regval->data_p) {
461 regval->data_p = NULL;
468 /***********************************************************************
469 Add a new registry value to the array
470 **********************************************************************/
472 int regval_ctr_addvalue(struct regval_ctr *ctr, const char *name, uint16 type,
473 const uint8 *data_p, size_t size)
476 return ctr->num_values;
478 /* Delete the current value (if it exists) and add the new one */
480 regval_ctr_delvalue( ctr, name );
482 /* allocate a slot in the array of pointers */
484 if ( ctr->num_values == 0 ) {
485 ctr->values = TALLOC_P( ctr, struct regval_blob *);
487 ctr->values = TALLOC_REALLOC_ARRAY(ctr, ctr->values,
488 struct regval_blob *,
497 /* allocate a new value and store the pointer in the arrya */
499 ctr->values[ctr->num_values] = regval_compose(ctr, name, type, data_p,
501 if (ctr->values[ctr->num_values] == NULL) {
507 return ctr->num_values;
510 /***********************************************************************
511 Add a new registry SZ value to the array
512 **********************************************************************/
514 int regval_ctr_addvalue_sz(struct regval_ctr *ctr, const char *name, const char *data)
518 if (!push_reg_sz(ctr, &blob, data)) {
522 return regval_ctr_addvalue(ctr, name, REG_SZ,
523 (const uint8 *)blob.data,
527 /***********************************************************************
528 Add a new registry MULTI_SZ value to the array
529 **********************************************************************/
531 int regval_ctr_addvalue_multi_sz(struct regval_ctr *ctr, const char *name, const char **data)
535 if (!push_reg_multi_sz(ctr, &blob, data)) {
539 return regval_ctr_addvalue(ctr, name, REG_MULTI_SZ,
540 (const uint8 *)blob.data,
544 /***********************************************************************
545 Add a new registry value to the array
546 **********************************************************************/
548 int regval_ctr_copyvalue(struct regval_ctr *ctr, struct regval_blob *val)
551 regval_ctr_addvalue(ctr, val->valuename, val->type,
552 (uint8 *)val->data_p, val->size);
555 return ctr->num_values;
558 /***********************************************************************
559 Delete a single value from the registry container.
560 No need to free memory since it is talloc'd.
561 **********************************************************************/
563 int regval_ctr_delvalue(struct regval_ctr *ctr, const char *name)
567 for ( i=0; i<ctr->num_values; i++ ) {
568 if ( strequal( ctr->values[i]->valuename, name ) )
572 /* just return if we don't find it */
574 if ( i == ctr->num_values )
575 return ctr->num_values;
577 /* If 'i' was not the last element, just shift everything down one */
579 if ( i < ctr->num_values )
580 memmove(&ctr->values[i], &ctr->values[i+1],
581 sizeof(struct regval_blob*)*(ctr->num_values-i));
583 return ctr->num_values;
586 /***********************************************************************
587 Retrieve single value from the registry container.
588 No need to free memory since it is talloc'd.
589 **********************************************************************/
591 struct regval_blob* regval_ctr_getvalue(struct regval_ctr *ctr,
596 /* search for the value */
598 for ( i=0; i<ctr->num_values; i++ ) {
599 if ( strequal( ctr->values[i]->valuename, name ) )
600 return ctr->values[i];
606 /***********************************************************************
607 return the data_p as a uint32
608 **********************************************************************/
610 uint32 regval_dword(struct regval_blob *val)
614 data = IVAL( regval_data_p(val), 0 );
619 /***********************************************************************
620 return the data_p as a character string
621 **********************************************************************/
623 const char *regval_sz(struct regval_blob *val)
625 const char *data = NULL;
626 DATA_BLOB blob = data_blob_const(regval_data_p(val), regval_size(val));
628 pull_reg_sz(talloc_tos(), &blob, &data);