s3:dbwrap: move the db_open_rbt() prototype to a new header dbwrap_rbt.h
[Samba.git] / source3 / registry / reg_objects.c
blob3e6ca67e804f4a394d4570ce9053264e594b9930
1 /*
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. */
23 #include "includes.h"
24 #include "registry.h"
25 #include "reg_objects.h"
26 #include "util_tdb.h"
27 #include "dbwrap/dbwrap.h"
28 #include "dbwrap/dbwrap_rbt.h"
29 #include "../libcli/registry/util_reg.h"
31 #undef DBGC_CLASS
32 #define DBGC_CLASS DBGC_REGISTRY
34 /* low level structure to contain registry values */
36 struct regval_blob {
37 fstring valuename;
38 uint32_t type;
39 /* this should be encapsulated in an RPC_DATA_BLOB */
40 uint32_t size; /* in bytes */
41 uint8_t *data_p;
44 /* container for registry values */
46 struct regval_ctr {
47 uint32_t num_values;
48 struct regval_blob **values;
49 int seqnum;
52 struct regsubkey_ctr {
53 uint32_t num_subkeys;
54 char **subkeys;
55 struct db_context *subkeys_hash;
56 int seqnum;
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
67 object.
69 **********************************************************************/
71 WERROR regsubkey_ctr_init(TALLOC_CTX *mem_ctx, struct regsubkey_ctr **ctr)
73 if (ctr == NULL) {
74 return WERR_INVALID_PARAM;
77 *ctr = talloc_zero(mem_ctx, struct regsubkey_ctr);
78 if (*ctr == NULL) {
79 return WERR_NOMEM;
82 (*ctr)->subkeys_hash = db_open_rbt(*ctr);
83 if ((*ctr)->subkeys_hash == NULL) {
84 talloc_free(*ctr);
85 return WERR_NOMEM;
88 return WERR_OK;
91 /**
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)
98 if (ctr == NULL) {
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;
109 ctr->seqnum = 0;
111 return WERR_OK;
114 WERROR regsubkey_ctr_set_seqnum(struct regsubkey_ctr *ctr, int seqnum)
116 if (ctr == NULL) {
117 return WERR_INVALID_PARAM;
120 ctr->seqnum = seqnum;
122 return WERR_OK;
125 int regsubkey_ctr_get_seqnum(struct regsubkey_ctr *ctr)
127 if (ctr == NULL) {
128 return -1;
131 return ctr->seqnum;
134 static WERROR regsubkey_ctr_hash_keyname(struct regsubkey_ctr *ctr,
135 const char *keyname,
136 uint32_t idx)
138 WERROR werr;
140 werr = ntstatus_to_werror(dbwrap_store_bystring_upper(ctr->subkeys_hash,
141 keyname,
142 make_tdb_data((uint8_t *)&idx,
143 sizeof(idx)),
144 TDB_REPLACE));
145 if (!W_ERROR_IS_OK(werr)) {
146 DEBUG(1, ("error hashing new key '%s' in container: %s\n",
147 keyname, win_errstr(werr)));
150 return werr;
153 static WERROR regsubkey_ctr_unhash_keyname(struct regsubkey_ctr *ctr,
154 const char *keyname)
156 WERROR werr;
158 werr = ntstatus_to_werror(dbwrap_delete_bystring_upper(ctr->subkeys_hash,
159 keyname));
160 if (!W_ERROR_IS_OK(werr)) {
161 DEBUG(1, ("error unhashing key '%s' in container: %s\n",
162 keyname, win_errstr(werr)));
165 return werr;
168 static WERROR regsubkey_ctr_index_for_keyname(struct regsubkey_ctr *ctr,
169 const char *keyname,
170 uint32_t *idx)
172 TDB_DATA data;
174 if ((ctr == NULL) || (keyname == NULL)) {
175 return WERR_INVALID_PARAM;
178 data = dbwrap_fetch_bystring_upper(ctr->subkeys_hash, ctr, keyname);
179 if (data.dptr == NULL) {
180 return WERR_NOT_FOUND;
183 if (data.dsize != sizeof(*idx)) {
184 talloc_free(data.dptr);
185 return WERR_INVALID_DATATYPE;
188 if (idx != NULL) {
189 *idx = *(uint32_t *)data.dptr;
192 talloc_free(data.dptr);
193 return WERR_OK;
196 /***********************************************************************
197 Add a new key to the array
198 **********************************************************************/
200 WERROR regsubkey_ctr_addkey( struct regsubkey_ctr *ctr, const char *keyname )
202 char **newkeys;
203 WERROR werr;
205 if ( !keyname ) {
206 return WERR_OK;
209 /* make sure the keyname is not already there */
211 if ( regsubkey_ctr_key_exists( ctr, keyname ) ) {
212 return WERR_OK;
215 if (!(newkeys = talloc_realloc(ctr, ctr->subkeys, char *,
216 ctr->num_subkeys+1))) {
217 return WERR_NOMEM;
220 ctr->subkeys = newkeys;
222 if (!(ctr->subkeys[ctr->num_subkeys] = talloc_strdup(ctr->subkeys,
223 keyname ))) {
225 * Don't shrink the new array again, this wastes a pointer
227 return WERR_NOMEM;
230 werr = regsubkey_ctr_hash_keyname(ctr, keyname, ctr->num_subkeys);
231 W_ERROR_NOT_OK_RETURN(werr);
233 ctr->num_subkeys++;
235 return WERR_OK;
238 /***********************************************************************
239 Delete a key from the array
240 **********************************************************************/
242 WERROR regsubkey_ctr_delkey( struct regsubkey_ctr *ctr, const char *keyname )
244 WERROR werr;
245 uint32_t idx, j;
247 if (keyname == NULL) {
248 return WERR_INVALID_PARAM;
251 /* make sure the keyname is actually already there */
253 werr = regsubkey_ctr_index_for_keyname(ctr, keyname, &idx);
254 W_ERROR_NOT_OK_RETURN(werr);
256 werr = regsubkey_ctr_unhash_keyname(ctr, keyname);
257 W_ERROR_NOT_OK_RETURN(werr);
259 /* update if we have any keys left */
260 ctr->num_subkeys--;
261 if (idx < ctr->num_subkeys) {
262 memmove(&ctr->subkeys[idx], &ctr->subkeys[idx+1],
263 sizeof(char *) * (ctr->num_subkeys - idx));
265 /* we have to re-hash rest of the array... :-( */
266 for (j = idx; j < ctr->num_subkeys; j++) {
267 werr = regsubkey_ctr_hash_keyname(ctr, ctr->subkeys[j], j);
268 W_ERROR_NOT_OK_RETURN(werr);
272 return WERR_OK;
275 /***********************************************************************
276 Check for the existance of a key
277 **********************************************************************/
279 bool regsubkey_ctr_key_exists( struct regsubkey_ctr *ctr, const char *keyname )
281 WERROR werr;
283 if (!ctr->subkeys) {
284 return False;
287 werr = regsubkey_ctr_index_for_keyname(ctr, keyname, NULL);
288 if (!W_ERROR_IS_OK(werr)) {
289 return false;
292 return true;
295 /***********************************************************************
296 How many keys does the container hold ?
297 **********************************************************************/
299 int regsubkey_ctr_numkeys( struct regsubkey_ctr *ctr )
301 return ctr->num_subkeys;
304 /***********************************************************************
305 Retreive a specific key string
306 **********************************************************************/
308 char* regsubkey_ctr_specific_key( struct regsubkey_ctr *ctr, uint32_t key_index )
310 if ( ! (key_index < ctr->num_subkeys) )
311 return NULL;
313 return ctr->subkeys[key_index];
317 * Utility functions for struct regval_ctr
321 * allocate a regval_ctr structure.
323 WERROR regval_ctr_init(TALLOC_CTX *mem_ctx, struct regval_ctr **ctr)
325 if (ctr == NULL) {
326 return WERR_INVALID_PARAM;
329 *ctr = talloc_zero(mem_ctx, struct regval_ctr);
330 if (*ctr == NULL) {
331 return WERR_NOMEM;
334 return WERR_OK;
337 /***********************************************************************
338 How many keys does the container hold ?
339 **********************************************************************/
341 int regval_ctr_numvals(struct regval_ctr *ctr)
343 return ctr->num_values;
346 /***********************************************************************
347 allocate memory for and duplicate a struct regval_blob.
348 This is malloc'd memory so the caller should free it when done
349 **********************************************************************/
351 struct regval_blob* dup_registry_value(struct regval_blob *val)
353 struct regval_blob *copy = NULL;
355 if ( !val )
356 return NULL;
358 if ( !(copy = SMB_MALLOC_P( struct regval_blob)) ) {
359 DEBUG(0,("dup_registry_value: malloc() failed!\n"));
360 return NULL;
363 /* copy all the non-pointer initial data */
365 memcpy( copy, val, sizeof(struct regval_blob) );
367 copy->size = 0;
368 copy->data_p = NULL;
370 if ( val->data_p && val->size )
372 if ( !(copy->data_p = (uint8_t *)memdup( val->data_p,
373 val->size )) ) {
374 DEBUG(0,("dup_registry_value: memdup() failed for [%d] "
375 "bytes!\n", val->size));
376 SAFE_FREE( copy );
377 return NULL;
379 copy->size = val->size;
382 return copy;
385 /**********************************************************************
386 free the memory allocated to a struct regval_blob
387 *********************************************************************/
389 void free_registry_value(struct regval_blob *val)
391 if ( !val )
392 return;
394 SAFE_FREE( val->data_p );
395 SAFE_FREE( val );
397 return;
400 /**********************************************************************
401 *********************************************************************/
403 uint8_t* regval_data_p(struct regval_blob *val)
405 return val->data_p;
408 /**********************************************************************
409 *********************************************************************/
411 uint32_t regval_size(struct regval_blob *val)
413 return val->size;
416 /**********************************************************************
417 *********************************************************************/
419 char* regval_name(struct regval_blob *val)
421 return val->valuename;
424 /**********************************************************************
425 *********************************************************************/
427 uint32_t regval_type(struct regval_blob *val)
429 return val->type;
432 /***********************************************************************
433 Retreive a pointer to a specific value. Caller shoud dup the structure
434 since this memory will go away when the ctr is free()'d
435 **********************************************************************/
437 struct regval_blob *regval_ctr_specific_value(struct regval_ctr *ctr,
438 uint32_t idx)
440 if ( !(idx < ctr->num_values) )
441 return NULL;
443 return ctr->values[idx];
446 /***********************************************************************
447 Check for the existance of a value
448 **********************************************************************/
450 bool regval_ctr_key_exists(struct regval_ctr *ctr, const char *value)
452 int i;
454 for ( i=0; i<ctr->num_values; i++ ) {
455 if ( strequal( ctr->values[i]->valuename, value) )
456 return True;
459 return False;
462 /***********************************************************************
463 * compose a struct regval_blob from input data
464 **********************************************************************/
466 struct regval_blob *regval_compose(TALLOC_CTX *ctx, const char *name,
467 uint32_t type,
468 const uint8_t *data_p, size_t size)
470 struct regval_blob *regval = talloc(ctx, struct regval_blob);
472 if (regval == NULL) {
473 return NULL;
476 fstrcpy(regval->valuename, name);
477 regval->type = type;
478 if (size) {
479 regval->data_p = (uint8_t *)talloc_memdup(regval, data_p, size);
480 if (!regval->data_p) {
481 TALLOC_FREE(regval);
482 return NULL;
484 } else {
485 regval->data_p = NULL;
487 regval->size = size;
489 return regval;
492 /***********************************************************************
493 Add a new registry value to the array
494 **********************************************************************/
496 int regval_ctr_addvalue(struct regval_ctr *ctr, const char *name, uint32_t type,
497 const uint8_t *data_p, size_t size)
499 if ( !name )
500 return ctr->num_values;
502 /* Delete the current value (if it exists) and add the new one */
504 regval_ctr_delvalue( ctr, name );
506 /* allocate a slot in the array of pointers */
508 if ( ctr->num_values == 0 ) {
509 ctr->values = talloc( ctr, struct regval_blob *);
510 } else {
511 ctr->values = talloc_realloc(ctr, ctr->values,
512 struct regval_blob *,
513 ctr->num_values+1);
516 if (!ctr->values) {
517 ctr->num_values = 0;
518 return 0;
521 /* allocate a new value and store the pointer in the arrya */
523 ctr->values[ctr->num_values] = regval_compose(ctr, name, type, data_p,
524 size);
525 if (ctr->values[ctr->num_values] == NULL) {
526 ctr->num_values = 0;
527 return 0;
529 ctr->num_values++;
531 return ctr->num_values;
534 /***********************************************************************
535 Add a new registry SZ value to the array
536 **********************************************************************/
538 int regval_ctr_addvalue_sz(struct regval_ctr *ctr, const char *name, const char *data)
540 DATA_BLOB blob;
542 if (!push_reg_sz(ctr, &blob, data)) {
543 return -1;
546 return regval_ctr_addvalue(ctr, name, REG_SZ,
547 (const uint8_t *)blob.data,
548 blob.length);
551 /***********************************************************************
552 Add a new registry MULTI_SZ value to the array
553 **********************************************************************/
555 int regval_ctr_addvalue_multi_sz(struct regval_ctr *ctr, const char *name, const char **data)
557 DATA_BLOB blob;
559 if (!push_reg_multi_sz(ctr, &blob, data)) {
560 return -1;
563 return regval_ctr_addvalue(ctr, name, REG_MULTI_SZ,
564 (const uint8_t *)blob.data,
565 blob.length);
568 /***********************************************************************
569 Add a new registry value to the array
570 **********************************************************************/
572 int regval_ctr_copyvalue(struct regval_ctr *ctr, struct regval_blob *val)
574 if ( val ) {
575 regval_ctr_addvalue(ctr, val->valuename, val->type,
576 (uint8_t *)val->data_p, val->size);
579 return ctr->num_values;
582 /***********************************************************************
583 Delete a single value from the registry container.
584 No need to free memory since it is talloc'd.
585 **********************************************************************/
587 int regval_ctr_delvalue(struct regval_ctr *ctr, const char *name)
589 int i;
591 for ( i=0; i<ctr->num_values; i++ ) {
592 if ( strequal( ctr->values[i]->valuename, name ) )
593 break;
596 /* just return if we don't find it */
598 if ( i == ctr->num_values )
599 return ctr->num_values;
601 /* If 'i' was not the last element, just shift everything down one */
602 ctr->num_values--;
603 if ( i < ctr->num_values )
604 memmove(&ctr->values[i], &ctr->values[i+1],
605 sizeof(struct regval_blob*)*(ctr->num_values-i));
607 return ctr->num_values;
610 /***********************************************************************
611 Retrieve single value from the registry container.
612 No need to free memory since it is talloc'd.
613 **********************************************************************/
615 struct regval_blob* regval_ctr_getvalue(struct regval_ctr *ctr,
616 const char *name)
618 int i;
620 /* search for the value */
622 for ( i=0; i<ctr->num_values; i++ ) {
623 if ( strequal( ctr->values[i]->valuename, name ) )
624 return ctr->values[i];
627 return NULL;
630 int regval_ctr_get_seqnum(struct regval_ctr *ctr)
632 if (ctr == NULL) {
633 return -1;
636 return ctr->seqnum;
639 WERROR regval_ctr_set_seqnum(struct regval_ctr *ctr, int seqnum)
641 if (ctr == NULL) {
642 return WERR_INVALID_PARAM;
645 ctr->seqnum = seqnum;
647 return WERR_OK;
650 /***********************************************************************
651 return the data_p as a uint32_t
652 **********************************************************************/
654 uint32_t regval_dword(struct regval_blob *val)
656 uint32_t data;
658 data = IVAL( regval_data_p(val), 0 );
660 return data;
663 /***********************************************************************
664 return the data_p as a character string
665 **********************************************************************/
667 const char *regval_sz(struct regval_blob *val)
669 const char *data = NULL;
670 DATA_BLOB blob = data_blob_const(regval_data_p(val), regval_size(val));
672 pull_reg_sz(talloc_tos(), &blob, &data);
674 return data;