VERSION: Bump version up to 3.6.0rc1.
[Samba.git] / source3 / registry / reg_objects.c
blob0fab3f7d8bead7dcaa95da3c3550a8224c596342
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 "dbwrap.h"
27 #include "../libcli/registry/util_reg.h"
29 #undef DBGC_CLASS
30 #define DBGC_CLASS DBGC_REGISTRY
32 /* low level structure to contain registry values */
34 struct regval_blob {
35 fstring valuename;
36 uint32_t type;
37 /* this should be encapsulated in an RPC_DATA_BLOB */
38 uint32_t size; /* in bytes */
39 uint8_t *data_p;
42 /* container for registry values */
44 struct regval_ctr {
45 uint32_t num_values;
46 struct regval_blob **values;
47 int seqnum;
50 struct regsubkey_ctr {
51 uint32_t num_subkeys;
52 char **subkeys;
53 struct db_context *subkeys_hash;
54 int seqnum;
57 /**********************************************************************
59 Note that the struct regsubkey_ctr and struct regval_ctr objects *must* be
60 talloc()'d since the methods use the object pointer as the talloc
61 context for internal private data.
63 There is no longer a regval_ctr_intit() and regval_ctr_destroy()
64 pair of functions. Simply TALLOC_ZERO_P() and TALLOC_FREE() the
65 object.
67 **********************************************************************/
69 WERROR regsubkey_ctr_init(TALLOC_CTX *mem_ctx, struct regsubkey_ctr **ctr)
71 if (ctr == NULL) {
72 return WERR_INVALID_PARAM;
75 *ctr = talloc_zero(mem_ctx, struct regsubkey_ctr);
76 if (*ctr == NULL) {
77 return WERR_NOMEM;
80 (*ctr)->subkeys_hash = db_open_rbt(*ctr);
81 if ((*ctr)->subkeys_hash == NULL) {
82 talloc_free(*ctr);
83 return WERR_NOMEM;
86 return WERR_OK;
89 /**
90 * re-initialize the list of subkeys (to the emtpy list)
91 * in an already allocated regsubkey_ctr
94 WERROR regsubkey_ctr_reinit(struct regsubkey_ctr *ctr)
96 if (ctr == NULL) {
97 return WERR_INVALID_PARAM;
100 talloc_free(ctr->subkeys_hash);
101 ctr->subkeys_hash = db_open_rbt(ctr);
102 W_ERROR_HAVE_NO_MEMORY(ctr->subkeys_hash);
104 TALLOC_FREE(ctr->subkeys);
106 ctr->num_subkeys = 0;
107 ctr->seqnum = 0;
109 return WERR_OK;
112 WERROR regsubkey_ctr_set_seqnum(struct regsubkey_ctr *ctr, int seqnum)
114 if (ctr == NULL) {
115 return WERR_INVALID_PARAM;
118 ctr->seqnum = seqnum;
120 return WERR_OK;
123 int regsubkey_ctr_get_seqnum(struct regsubkey_ctr *ctr)
125 if (ctr == NULL) {
126 return -1;
129 return ctr->seqnum;
132 static WERROR regsubkey_ctr_hash_keyname(struct regsubkey_ctr *ctr,
133 const char *keyname,
134 uint32_t idx)
136 WERROR werr;
138 werr = ntstatus_to_werror(dbwrap_store_bystring_upper(ctr->subkeys_hash,
139 keyname,
140 make_tdb_data((uint8_t *)&idx,
141 sizeof(idx)),
142 TDB_REPLACE));
143 if (!W_ERROR_IS_OK(werr)) {
144 DEBUG(1, ("error hashing new key '%s' in container: %s\n",
145 keyname, win_errstr(werr)));
148 return werr;
151 static WERROR regsubkey_ctr_unhash_keyname(struct regsubkey_ctr *ctr,
152 const char *keyname)
154 WERROR werr;
156 werr = ntstatus_to_werror(dbwrap_delete_bystring_upper(ctr->subkeys_hash,
157 keyname));
158 if (!W_ERROR_IS_OK(werr)) {
159 DEBUG(1, ("error unhashing key '%s' in container: %s\n",
160 keyname, win_errstr(werr)));
163 return werr;
166 static WERROR regsubkey_ctr_index_for_keyname(struct regsubkey_ctr *ctr,
167 const char *keyname,
168 uint32_t *idx)
170 TDB_DATA data;
172 if ((ctr == NULL) || (keyname == NULL)) {
173 return WERR_INVALID_PARAM;
176 data = dbwrap_fetch_bystring_upper(ctr->subkeys_hash, ctr, keyname);
177 if (data.dptr == NULL) {
178 return WERR_NOT_FOUND;
181 if (data.dsize != sizeof(*idx)) {
182 talloc_free(data.dptr);
183 return WERR_INVALID_DATATYPE;
186 if (idx != NULL) {
187 *idx = *(uint32_t *)data.dptr;
190 talloc_free(data.dptr);
191 return WERR_OK;
194 /***********************************************************************
195 Add a new key to the array
196 **********************************************************************/
198 WERROR regsubkey_ctr_addkey( struct regsubkey_ctr *ctr, const char *keyname )
200 char **newkeys;
201 WERROR werr;
203 if ( !keyname ) {
204 return WERR_OK;
207 /* make sure the keyname is not already there */
209 if ( regsubkey_ctr_key_exists( ctr, keyname ) ) {
210 return WERR_OK;
213 if (!(newkeys = TALLOC_REALLOC_ARRAY(ctr, ctr->subkeys, char *,
214 ctr->num_subkeys+1))) {
215 return WERR_NOMEM;
218 ctr->subkeys = newkeys;
220 if (!(ctr->subkeys[ctr->num_subkeys] = talloc_strdup(ctr->subkeys,
221 keyname ))) {
223 * Don't shrink the new array again, this wastes a pointer
225 return WERR_NOMEM;
228 werr = regsubkey_ctr_hash_keyname(ctr, keyname, ctr->num_subkeys);
229 W_ERROR_NOT_OK_RETURN(werr);
231 ctr->num_subkeys++;
233 return WERR_OK;
236 /***********************************************************************
237 Delete a key from the array
238 **********************************************************************/
240 WERROR regsubkey_ctr_delkey( struct regsubkey_ctr *ctr, const char *keyname )
242 WERROR werr;
243 uint32_t idx, j;
245 if (keyname == NULL) {
246 return WERR_INVALID_PARAM;
249 /* make sure the keyname is actually already there */
251 werr = regsubkey_ctr_index_for_keyname(ctr, keyname, &idx);
252 W_ERROR_NOT_OK_RETURN(werr);
254 werr = regsubkey_ctr_unhash_keyname(ctr, keyname);
255 W_ERROR_NOT_OK_RETURN(werr);
257 /* update if we have any keys left */
258 ctr->num_subkeys--;
259 if (idx < ctr->num_subkeys) {
260 memmove(&ctr->subkeys[idx], &ctr->subkeys[idx+1],
261 sizeof(char *) * (ctr->num_subkeys - idx));
263 /* we have to re-hash rest of the array... :-( */
264 for (j = idx; j < ctr->num_subkeys; j++) {
265 werr = regsubkey_ctr_hash_keyname(ctr, ctr->subkeys[j], j);
266 W_ERROR_NOT_OK_RETURN(werr);
270 return WERR_OK;
273 /***********************************************************************
274 Check for the existance of a key
275 **********************************************************************/
277 bool regsubkey_ctr_key_exists( struct regsubkey_ctr *ctr, const char *keyname )
279 WERROR werr;
281 if (!ctr->subkeys) {
282 return False;
285 werr = regsubkey_ctr_index_for_keyname(ctr, keyname, NULL);
286 if (!W_ERROR_IS_OK(werr)) {
287 return false;
290 return true;
293 /***********************************************************************
294 How many keys does the container hold ?
295 **********************************************************************/
297 int regsubkey_ctr_numkeys( struct regsubkey_ctr *ctr )
299 return ctr->num_subkeys;
302 /***********************************************************************
303 Retreive a specific key string
304 **********************************************************************/
306 char* regsubkey_ctr_specific_key( struct regsubkey_ctr *ctr, uint32_t key_index )
308 if ( ! (key_index < ctr->num_subkeys) )
309 return NULL;
311 return ctr->subkeys[key_index];
315 * Utility functions for struct regval_ctr
319 * allocate a regval_ctr structure.
321 WERROR regval_ctr_init(TALLOC_CTX *mem_ctx, struct regval_ctr **ctr)
323 if (ctr == NULL) {
324 return WERR_INVALID_PARAM;
327 *ctr = talloc_zero(mem_ctx, struct regval_ctr);
328 if (*ctr == NULL) {
329 return WERR_NOMEM;
332 return WERR_OK;
335 /***********************************************************************
336 How many keys does the container hold ?
337 **********************************************************************/
339 int regval_ctr_numvals(struct regval_ctr *ctr)
341 return ctr->num_values;
344 /***********************************************************************
345 allocate memory for and duplicate a struct regval_blob.
346 This is malloc'd memory so the caller should free it when done
347 **********************************************************************/
349 struct regval_blob* dup_registry_value(struct regval_blob *val)
351 struct regval_blob *copy = NULL;
353 if ( !val )
354 return NULL;
356 if ( !(copy = SMB_MALLOC_P( struct regval_blob)) ) {
357 DEBUG(0,("dup_registry_value: malloc() failed!\n"));
358 return NULL;
361 /* copy all the non-pointer initial data */
363 memcpy( copy, val, sizeof(struct regval_blob) );
365 copy->size = 0;
366 copy->data_p = NULL;
368 if ( val->data_p && val->size )
370 if ( !(copy->data_p = (uint8_t *)memdup( val->data_p,
371 val->size )) ) {
372 DEBUG(0,("dup_registry_value: memdup() failed for [%d] "
373 "bytes!\n", val->size));
374 SAFE_FREE( copy );
375 return NULL;
377 copy->size = val->size;
380 return copy;
383 /**********************************************************************
384 free the memory allocated to a struct regval_blob
385 *********************************************************************/
387 void free_registry_value(struct regval_blob *val)
389 if ( !val )
390 return;
392 SAFE_FREE( val->data_p );
393 SAFE_FREE( val );
395 return;
398 /**********************************************************************
399 *********************************************************************/
401 uint8_t* regval_data_p(struct regval_blob *val)
403 return val->data_p;
406 /**********************************************************************
407 *********************************************************************/
409 uint32_t regval_size(struct regval_blob *val)
411 return val->size;
414 /**********************************************************************
415 *********************************************************************/
417 char* regval_name(struct regval_blob *val)
419 return val->valuename;
422 /**********************************************************************
423 *********************************************************************/
425 uint32_t regval_type(struct regval_blob *val)
427 return val->type;
430 /***********************************************************************
431 Retreive a pointer to a specific value. Caller shoud dup the structure
432 since this memory will go away when the ctr is free()'d
433 **********************************************************************/
435 struct regval_blob *regval_ctr_specific_value(struct regval_ctr *ctr,
436 uint32_t idx)
438 if ( !(idx < ctr->num_values) )
439 return NULL;
441 return ctr->values[idx];
444 /***********************************************************************
445 Check for the existance of a value
446 **********************************************************************/
448 bool regval_ctr_key_exists(struct regval_ctr *ctr, const char *value)
450 int i;
452 for ( i=0; i<ctr->num_values; i++ ) {
453 if ( strequal( ctr->values[i]->valuename, value) )
454 return True;
457 return False;
460 /***********************************************************************
461 * compose a struct regval_blob from input data
462 **********************************************************************/
464 struct regval_blob *regval_compose(TALLOC_CTX *ctx, const char *name,
465 uint32_t type,
466 const uint8_t *data_p, size_t size)
468 struct regval_blob *regval = TALLOC_P(ctx, struct regval_blob);
470 if (regval == NULL) {
471 return NULL;
474 fstrcpy(regval->valuename, name);
475 regval->type = type;
476 if (size) {
477 regval->data_p = (uint8_t *)TALLOC_MEMDUP(regval, data_p, size);
478 if (!regval->data_p) {
479 TALLOC_FREE(regval);
480 return NULL;
482 } else {
483 regval->data_p = NULL;
485 regval->size = size;
487 return regval;
490 /***********************************************************************
491 Add a new registry value to the array
492 **********************************************************************/
494 int regval_ctr_addvalue(struct regval_ctr *ctr, const char *name, uint32_t type,
495 const uint8_t *data_p, size_t size)
497 if ( !name )
498 return ctr->num_values;
500 /* Delete the current value (if it exists) and add the new one */
502 regval_ctr_delvalue( ctr, name );
504 /* allocate a slot in the array of pointers */
506 if ( ctr->num_values == 0 ) {
507 ctr->values = TALLOC_P( ctr, struct regval_blob *);
508 } else {
509 ctr->values = TALLOC_REALLOC_ARRAY(ctr, ctr->values,
510 struct regval_blob *,
511 ctr->num_values+1);
514 if (!ctr->values) {
515 ctr->num_values = 0;
516 return 0;
519 /* allocate a new value and store the pointer in the arrya */
521 ctr->values[ctr->num_values] = regval_compose(ctr, name, type, data_p,
522 size);
523 if (ctr->values[ctr->num_values] == NULL) {
524 ctr->num_values = 0;
525 return 0;
527 ctr->num_values++;
529 return ctr->num_values;
532 /***********************************************************************
533 Add a new registry SZ value to the array
534 **********************************************************************/
536 int regval_ctr_addvalue_sz(struct regval_ctr *ctr, const char *name, const char *data)
538 DATA_BLOB blob;
540 if (!push_reg_sz(ctr, &blob, data)) {
541 return -1;
544 return regval_ctr_addvalue(ctr, name, REG_SZ,
545 (const uint8_t *)blob.data,
546 blob.length);
549 /***********************************************************************
550 Add a new registry MULTI_SZ value to the array
551 **********************************************************************/
553 int regval_ctr_addvalue_multi_sz(struct regval_ctr *ctr, const char *name, const char **data)
555 DATA_BLOB blob;
557 if (!push_reg_multi_sz(ctr, &blob, data)) {
558 return -1;
561 return regval_ctr_addvalue(ctr, name, REG_MULTI_SZ,
562 (const uint8_t *)blob.data,
563 blob.length);
566 /***********************************************************************
567 Add a new registry value to the array
568 **********************************************************************/
570 int regval_ctr_copyvalue(struct regval_ctr *ctr, struct regval_blob *val)
572 if ( val ) {
573 regval_ctr_addvalue(ctr, val->valuename, val->type,
574 (uint8_t *)val->data_p, val->size);
577 return ctr->num_values;
580 /***********************************************************************
581 Delete a single value from the registry container.
582 No need to free memory since it is talloc'd.
583 **********************************************************************/
585 int regval_ctr_delvalue(struct regval_ctr *ctr, const char *name)
587 int i;
589 for ( i=0; i<ctr->num_values; i++ ) {
590 if ( strequal( ctr->values[i]->valuename, name ) )
591 break;
594 /* just return if we don't find it */
596 if ( i == ctr->num_values )
597 return ctr->num_values;
599 /* If 'i' was not the last element, just shift everything down one */
600 ctr->num_values--;
601 if ( i < ctr->num_values )
602 memmove(&ctr->values[i], &ctr->values[i+1],
603 sizeof(struct regval_blob*)*(ctr->num_values-i));
605 return ctr->num_values;
608 /***********************************************************************
609 Retrieve single value from the registry container.
610 No need to free memory since it is talloc'd.
611 **********************************************************************/
613 struct regval_blob* regval_ctr_getvalue(struct regval_ctr *ctr,
614 const char *name)
616 int i;
618 /* search for the value */
620 for ( i=0; i<ctr->num_values; i++ ) {
621 if ( strequal( ctr->values[i]->valuename, name ) )
622 return ctr->values[i];
625 return NULL;
628 int regval_ctr_get_seqnum(struct regval_ctr *ctr)
630 if (ctr == NULL) {
631 return -1;
634 return ctr->seqnum;
637 WERROR regval_ctr_set_seqnum(struct regval_ctr *ctr, int seqnum)
639 if (ctr == NULL) {
640 return WERR_INVALID_PARAM;
643 ctr->seqnum = seqnum;
645 return WERR_OK;
648 /***********************************************************************
649 return the data_p as a uint32_t
650 **********************************************************************/
652 uint32_t regval_dword(struct regval_blob *val)
654 uint32_t data;
656 data = IVAL( regval_data_p(val), 0 );
658 return data;
661 /***********************************************************************
662 return the data_p as a character string
663 **********************************************************************/
665 const char *regval_sz(struct regval_blob *val)
667 const char *data = NULL;
668 DATA_BLOB blob = data_blob_const(regval_data_p(val), regval_size(val));
670 pull_reg_sz(talloc_tos(), &blob, &data);
672 return data;