s3:registry: hide definition of regval_ctr and regval_blob in reg_objects.c
[Samba/gebeck_regimport.git] / source3 / registry / reg_objects.c
blob55559461280864875b28b9c2dcfbdc44d4c8a66a
1 /*
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. */
22 #include "includes.h"
23 #include "registry.h"
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_REGISTRY
28 /* low level structure to contain registry values */
30 struct regval_blob {
31 fstring valuename;
32 uint16 type;
33 /* this should be encapsulated in an RPC_DATA_BLOB */
34 uint32 size; /* in bytes */
35 uint8 *data_p;
38 /* container for registry values */
40 struct regval_ctr {
41 uint32 num_values;
42 struct regval_blob **values;
43 int seqnum;
46 struct regsubkey_ctr {
47 uint32_t num_subkeys;
48 char **subkeys;
49 struct db_context *subkeys_hash;
50 int seqnum;
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
61 object.
63 **********************************************************************/
65 WERROR regsubkey_ctr_init(TALLOC_CTX *mem_ctx, struct regsubkey_ctr **ctr)
67 if (ctr == NULL) {
68 return WERR_INVALID_PARAM;
71 *ctr = talloc_zero(mem_ctx, struct regsubkey_ctr);
72 if (*ctr == NULL) {
73 return WERR_NOMEM;
76 (*ctr)->subkeys_hash = db_open_rbt(*ctr);
77 if ((*ctr)->subkeys_hash == NULL) {
78 talloc_free(*ctr);
79 return WERR_NOMEM;
82 return WERR_OK;
85 /**
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)
92 if (ctr == NULL) {
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;
103 ctr->seqnum = 0;
105 return WERR_OK;
108 WERROR regsubkey_ctr_set_seqnum(struct regsubkey_ctr *ctr, int seqnum)
110 if (ctr == NULL) {
111 return WERR_INVALID_PARAM;
114 ctr->seqnum = seqnum;
116 return WERR_OK;
119 int regsubkey_ctr_get_seqnum(struct regsubkey_ctr *ctr)
121 if (ctr == NULL) {
122 return -1;
125 return ctr->seqnum;
128 static WERROR regsubkey_ctr_hash_keyname(struct regsubkey_ctr *ctr,
129 const char *keyname,
130 uint32 idx)
132 WERROR werr;
134 werr = ntstatus_to_werror(dbwrap_store_bystring_upper(ctr->subkeys_hash,
135 keyname,
136 make_tdb_data((uint8 *)&idx,
137 sizeof(idx)),
138 TDB_REPLACE));
139 if (!W_ERROR_IS_OK(werr)) {
140 DEBUG(1, ("error hashing new key '%s' in container: %s\n",
141 keyname, win_errstr(werr)));
144 return werr;
147 static WERROR regsubkey_ctr_unhash_keyname(struct regsubkey_ctr *ctr,
148 const char *keyname)
150 WERROR werr;
152 werr = ntstatus_to_werror(dbwrap_delete_bystring_upper(ctr->subkeys_hash,
153 keyname));
154 if (!W_ERROR_IS_OK(werr)) {
155 DEBUG(1, ("error unhashing key '%s' in container: %s\n",
156 keyname, win_errstr(werr)));
159 return werr;
162 static WERROR regsubkey_ctr_index_for_keyname(struct regsubkey_ctr *ctr,
163 const char *keyname,
164 uint32 *idx)
166 TDB_DATA data;
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;
182 if (idx != NULL) {
183 *idx = *(uint32 *)data.dptr;
186 talloc_free(data.dptr);
187 return WERR_OK;
190 /***********************************************************************
191 Add a new key to the array
192 **********************************************************************/
194 WERROR regsubkey_ctr_addkey( struct regsubkey_ctr *ctr, const char *keyname )
196 char **newkeys;
197 WERROR werr;
199 if ( !keyname ) {
200 return WERR_OK;
203 /* make sure the keyname is not already there */
205 if ( regsubkey_ctr_key_exists( ctr, keyname ) ) {
206 return WERR_OK;
209 if (!(newkeys = TALLOC_REALLOC_ARRAY(ctr, ctr->subkeys, char *,
210 ctr->num_subkeys+1))) {
211 return WERR_NOMEM;
214 ctr->subkeys = newkeys;
216 if (!(ctr->subkeys[ctr->num_subkeys] = talloc_strdup(ctr->subkeys,
217 keyname ))) {
219 * Don't shrink the new array again, this wastes a pointer
221 return WERR_NOMEM;
224 werr = regsubkey_ctr_hash_keyname(ctr, keyname, ctr->num_subkeys);
225 W_ERROR_NOT_OK_RETURN(werr);
227 ctr->num_subkeys++;
229 return WERR_OK;
232 /***********************************************************************
233 Delete a key from the array
234 **********************************************************************/
236 WERROR regsubkey_ctr_delkey( struct regsubkey_ctr *ctr, const char *keyname )
238 WERROR werr;
239 uint32 idx, j;
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 */
254 ctr->num_subkeys--;
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);
266 return WERR_OK;
269 /***********************************************************************
270 Check for the existance of a key
271 **********************************************************************/
273 bool regsubkey_ctr_key_exists( struct regsubkey_ctr *ctr, const char *keyname )
275 WERROR werr;
277 if (!ctr->subkeys) {
278 return False;
281 werr = regsubkey_ctr_index_for_keyname(ctr, keyname, NULL);
282 if (!W_ERROR_IS_OK(werr)) {
283 return false;
286 return true;
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) )
305 return NULL;
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)
319 if (ctr == NULL) {
320 return WERR_INVALID_PARAM;
323 *ctr = talloc_zero(mem_ctx, struct regval_ctr);
324 if (*ctr == NULL) {
325 return WERR_NOMEM;
328 return WERR_OK;
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;
349 if ( !val )
350 return NULL;
352 if ( !(copy = SMB_MALLOC_P( struct regval_blob)) ) {
353 DEBUG(0,("dup_registry_value: malloc() failed!\n"));
354 return NULL;
357 /* copy all the non-pointer initial data */
359 memcpy( copy, val, sizeof(struct regval_blob) );
361 copy->size = 0;
362 copy->data_p = NULL;
364 if ( val->data_p && val->size )
366 if ( !(copy->data_p = (uint8 *)memdup( val->data_p,
367 val->size )) ) {
368 DEBUG(0,("dup_registry_value: memdup() failed for [%d] "
369 "bytes!\n", val->size));
370 SAFE_FREE( copy );
371 return NULL;
373 copy->size = val->size;
376 return copy;
379 /**********************************************************************
380 free the memory allocated to a struct regval_blob
381 *********************************************************************/
383 void free_registry_value(struct regval_blob *val)
385 if ( !val )
386 return;
388 SAFE_FREE( val->data_p );
389 SAFE_FREE( val );
391 return;
394 /**********************************************************************
395 *********************************************************************/
397 uint8* regval_data_p(struct regval_blob *val)
399 return val->data_p;
402 /**********************************************************************
403 *********************************************************************/
405 uint32 regval_size(struct regval_blob *val)
407 return val->size;
410 /**********************************************************************
411 *********************************************************************/
413 char* regval_name(struct regval_blob *val)
415 return val->valuename;
418 /**********************************************************************
419 *********************************************************************/
421 uint32 regval_type(struct regval_blob *val)
423 return val->type;
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,
432 uint32 idx)
434 if ( !(idx < ctr->num_values) )
435 return NULL;
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)
446 int i;
448 for ( i=0; i<ctr->num_values; i++ ) {
449 if ( strequal( ctr->values[i]->valuename, value) )
450 return True;
453 return False;
456 /***********************************************************************
457 * compose a struct regval_blob from input data
458 **********************************************************************/
460 struct regval_blob *regval_compose(TALLOC_CTX *ctx, const char *name,
461 uint16 type,
462 const uint8 *data_p, size_t size)
464 struct regval_blob *regval = TALLOC_P(ctx, struct regval_blob);
466 if (regval == NULL) {
467 return NULL;
470 fstrcpy(regval->valuename, name);
471 regval->type = type;
472 if (size) {
473 regval->data_p = (uint8 *)TALLOC_MEMDUP(regval, data_p, size);
474 if (!regval->data_p) {
475 TALLOC_FREE(regval);
476 return NULL;
478 } else {
479 regval->data_p = NULL;
481 regval->size = size;
483 return regval;
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)
493 if ( !name )
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 *);
504 } else {
505 ctr->values = TALLOC_REALLOC_ARRAY(ctr, ctr->values,
506 struct regval_blob *,
507 ctr->num_values+1);
510 if (!ctr->values) {
511 ctr->num_values = 0;
512 return 0;
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,
518 size);
519 if (ctr->values[ctr->num_values] == NULL) {
520 ctr->num_values = 0;
521 return 0;
523 ctr->num_values++;
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)
534 DATA_BLOB blob;
536 if (!push_reg_sz(ctr, &blob, data)) {
537 return -1;
540 return regval_ctr_addvalue(ctr, name, REG_SZ,
541 (const uint8 *)blob.data,
542 blob.length);
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)
551 DATA_BLOB blob;
553 if (!push_reg_multi_sz(ctr, &blob, data)) {
554 return -1;
557 return regval_ctr_addvalue(ctr, name, REG_MULTI_SZ,
558 (const uint8 *)blob.data,
559 blob.length);
562 /***********************************************************************
563 Add a new registry value to the array
564 **********************************************************************/
566 int regval_ctr_copyvalue(struct regval_ctr *ctr, struct regval_blob *val)
568 if ( 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)
583 int i;
585 for ( i=0; i<ctr->num_values; i++ ) {
586 if ( strequal( ctr->values[i]->valuename, name ) )
587 break;
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 */
596 ctr->num_values--;
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,
610 const char *name)
612 int i;
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];
621 return NULL;
624 int regval_ctr_get_seqnum(struct regval_ctr *ctr)
626 if (ctr == NULL) {
627 return -1;
630 return ctr->seqnum;
633 WERROR regval_ctr_set_seqnum(struct regval_ctr *ctr, int seqnum)
635 if (ctr == NULL) {
636 return WERR_INVALID_PARAM;
639 ctr->seqnum = seqnum;
641 return WERR_OK;
644 /***********************************************************************
645 return the data_p as a uint32
646 **********************************************************************/
648 uint32 regval_dword(struct regval_blob *val)
650 uint32 data;
652 data = IVAL( regval_data_p(val), 0 );
654 return data;
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);
668 return data;