s3: re-run make samba3-idl.
[Samba/cd1.git] / source3 / registry / reg_objects.c
blobb4b8ff26307d79124a6bc0e55af3630bf672f27a
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"
24 #undef DBGC_CLASS
25 #define DBGC_CLASS DBGC_REGISTRY
27 struct regsubkey_ctr {
28 uint32_t num_subkeys;
29 char **subkeys;
30 struct db_context *subkeys_hash;
31 int seqnum;
34 /**********************************************************************
36 Note that the struct regsubkey_ctr and struct regval_ctr objects *must* be
37 talloc()'d since the methods use the object pointer as the talloc
38 context for internal private data.
40 There is no longer a regval_ctr_intit() and regval_ctr_destroy()
41 pair of functions. Simply TALLOC_ZERO_P() and TALLOC_FREE() the
42 object.
44 **********************************************************************/
46 WERROR regsubkey_ctr_init(TALLOC_CTX *mem_ctx, struct regsubkey_ctr **ctr)
48 if (ctr == NULL) {
49 return WERR_INVALID_PARAM;
52 *ctr = talloc_zero(mem_ctx, struct regsubkey_ctr);
53 if (*ctr == NULL) {
54 return WERR_NOMEM;
57 (*ctr)->subkeys_hash = db_open_rbt(*ctr);
58 if ((*ctr)->subkeys_hash == NULL) {
59 talloc_free(*ctr);
60 return WERR_NOMEM;
63 return WERR_OK;
66 /**
67 * re-initialize the list of subkeys (to the emtpy list)
68 * in an already allocated regsubkey_ctr
71 WERROR regsubkey_ctr_reinit(struct regsubkey_ctr *ctr)
73 if (ctr == NULL) {
74 return WERR_INVALID_PARAM;
77 talloc_free(ctr->subkeys_hash);
78 ctr->subkeys_hash = db_open_rbt(ctr);
79 W_ERROR_HAVE_NO_MEMORY(ctr->subkeys_hash);
81 TALLOC_FREE(ctr->subkeys);
83 ctr->num_subkeys = 0;
84 ctr->seqnum = 0;
86 return WERR_OK;
89 WERROR regsubkey_ctr_set_seqnum(struct regsubkey_ctr *ctr, int seqnum)
91 if (ctr == NULL) {
92 return WERR_INVALID_PARAM;
95 ctr->seqnum = seqnum;
97 return WERR_OK;
100 int regsubkey_ctr_get_seqnum(struct regsubkey_ctr *ctr)
102 if (ctr == NULL) {
103 return -1;
106 return ctr->seqnum;
109 static WERROR regsubkey_ctr_hash_keyname(struct regsubkey_ctr *ctr,
110 const char *keyname,
111 uint32 idx)
113 WERROR werr;
115 werr = ntstatus_to_werror(dbwrap_store_bystring_upper(ctr->subkeys_hash,
116 keyname,
117 make_tdb_data((uint8 *)&idx,
118 sizeof(idx)),
119 TDB_REPLACE));
120 if (!W_ERROR_IS_OK(werr)) {
121 DEBUG(1, ("error hashing new key '%s' in container: %s\n",
122 keyname, win_errstr(werr)));
125 return werr;
128 static WERROR regsubkey_ctr_unhash_keyname(struct regsubkey_ctr *ctr,
129 const char *keyname)
131 WERROR werr;
133 werr = ntstatus_to_werror(dbwrap_delete_bystring_upper(ctr->subkeys_hash,
134 keyname));
135 if (!W_ERROR_IS_OK(werr)) {
136 DEBUG(1, ("error unhashing key '%s' in container: %s\n",
137 keyname, win_errstr(werr)));
140 return werr;
143 static WERROR regsubkey_ctr_index_for_keyname(struct regsubkey_ctr *ctr,
144 const char *keyname,
145 uint32 *idx)
147 TDB_DATA data;
149 if ((ctr == NULL) || (keyname == NULL)) {
150 return WERR_INVALID_PARAM;
153 data = dbwrap_fetch_bystring_upper(ctr->subkeys_hash, ctr, keyname);
154 if (data.dptr == NULL) {
155 return WERR_NOT_FOUND;
158 if (data.dsize != sizeof(*idx)) {
159 talloc_free(data.dptr);
160 return WERR_INVALID_DATATYPE;
163 if (idx != NULL) {
164 *idx = *(uint32 *)data.dptr;
167 talloc_free(data.dptr);
168 return WERR_OK;
171 /***********************************************************************
172 Add a new key to the array
173 **********************************************************************/
175 WERROR regsubkey_ctr_addkey( struct regsubkey_ctr *ctr, const char *keyname )
177 char **newkeys;
178 WERROR werr;
180 if ( !keyname ) {
181 return WERR_OK;
184 /* make sure the keyname is not already there */
186 if ( regsubkey_ctr_key_exists( ctr, keyname ) ) {
187 return WERR_OK;
190 if (!(newkeys = TALLOC_REALLOC_ARRAY(ctr, ctr->subkeys, char *,
191 ctr->num_subkeys+1))) {
192 return WERR_NOMEM;
195 ctr->subkeys = newkeys;
197 if (!(ctr->subkeys[ctr->num_subkeys] = talloc_strdup(ctr->subkeys,
198 keyname ))) {
200 * Don't shrink the new array again, this wastes a pointer
202 return WERR_NOMEM;
205 werr = regsubkey_ctr_hash_keyname(ctr, keyname, ctr->num_subkeys);
206 W_ERROR_NOT_OK_RETURN(werr);
208 ctr->num_subkeys++;
210 return WERR_OK;
213 /***********************************************************************
214 Delete a key from the array
215 **********************************************************************/
217 WERROR regsubkey_ctr_delkey( struct regsubkey_ctr *ctr, const char *keyname )
219 WERROR werr;
220 uint32 idx, j;
222 if (keyname == NULL) {
223 return WERR_INVALID_PARAM;
226 /* make sure the keyname is actually already there */
228 werr = regsubkey_ctr_index_for_keyname(ctr, keyname, &idx);
229 W_ERROR_NOT_OK_RETURN(werr);
231 werr = regsubkey_ctr_unhash_keyname(ctr, keyname);
232 W_ERROR_NOT_OK_RETURN(werr);
234 /* update if we have any keys left */
235 ctr->num_subkeys--;
236 if (idx < ctr->num_subkeys) {
237 memmove(&ctr->subkeys[idx], &ctr->subkeys[idx+1],
238 sizeof(char *) * (ctr->num_subkeys - idx));
240 /* we have to re-hash rest of the array... :-( */
241 for (j = idx; j < ctr->num_subkeys; j++) {
242 werr = regsubkey_ctr_hash_keyname(ctr, ctr->subkeys[j], j);
243 W_ERROR_NOT_OK_RETURN(werr);
247 return WERR_OK;
250 /***********************************************************************
251 Check for the existance of a key
252 **********************************************************************/
254 bool regsubkey_ctr_key_exists( struct regsubkey_ctr *ctr, const char *keyname )
256 WERROR werr;
258 if (!ctr->subkeys) {
259 return False;
262 werr = regsubkey_ctr_index_for_keyname(ctr, keyname, NULL);
263 if (!W_ERROR_IS_OK(werr)) {
264 return false;
267 return true;
270 /***********************************************************************
271 How many keys does the container hold ?
272 **********************************************************************/
274 int regsubkey_ctr_numkeys( struct regsubkey_ctr *ctr )
276 return ctr->num_subkeys;
279 /***********************************************************************
280 Retreive a specific key string
281 **********************************************************************/
283 char* regsubkey_ctr_specific_key( struct regsubkey_ctr *ctr, uint32_t key_index )
285 if ( ! (key_index < ctr->num_subkeys) )
286 return NULL;
288 return ctr->subkeys[key_index];
292 * Utility functions for struct regval_ctr
295 /***********************************************************************
296 How many keys does the container hold ?
297 **********************************************************************/
299 int regval_ctr_numvals(struct regval_ctr *ctr)
301 return ctr->num_values;
304 /***********************************************************************
305 allocate memory for and duplicate a struct regval_blob.
306 This is malloc'd memory so the caller should free it when done
307 **********************************************************************/
309 struct regval_blob* dup_registry_value(struct regval_blob *val)
311 struct regval_blob *copy = NULL;
313 if ( !val )
314 return NULL;
316 if ( !(copy = SMB_MALLOC_P( struct regval_blob)) ) {
317 DEBUG(0,("dup_registry_value: malloc() failed!\n"));
318 return NULL;
321 /* copy all the non-pointer initial data */
323 memcpy( copy, val, sizeof(struct regval_blob) );
325 copy->size = 0;
326 copy->data_p = NULL;
328 if ( val->data_p && val->size )
330 if ( !(copy->data_p = (uint8 *)memdup( val->data_p,
331 val->size )) ) {
332 DEBUG(0,("dup_registry_value: memdup() failed for [%d] "
333 "bytes!\n", val->size));
334 SAFE_FREE( copy );
335 return NULL;
337 copy->size = val->size;
340 return copy;
343 /**********************************************************************
344 free the memory allocated to a struct regval_blob
345 *********************************************************************/
347 void free_registry_value(struct regval_blob *val)
349 if ( !val )
350 return;
352 SAFE_FREE( val->data_p );
353 SAFE_FREE( val );
355 return;
358 /**********************************************************************
359 *********************************************************************/
361 uint8* regval_data_p(struct regval_blob *val)
363 return val->data_p;
366 /**********************************************************************
367 *********************************************************************/
369 uint32 regval_size(struct regval_blob *val)
371 return val->size;
374 /**********************************************************************
375 *********************************************************************/
377 char* regval_name(struct regval_blob *val)
379 return val->valuename;
382 /**********************************************************************
383 *********************************************************************/
385 uint32 regval_type(struct regval_blob *val)
387 return val->type;
390 /***********************************************************************
391 Retreive a pointer to a specific value. Caller shoud dup the structure
392 since this memory will go away when the ctr is free()'d
393 **********************************************************************/
395 struct regval_blob *regval_ctr_specific_value(struct regval_ctr *ctr,
396 uint32 idx)
398 if ( !(idx < ctr->num_values) )
399 return NULL;
401 return ctr->values[idx];
404 /***********************************************************************
405 Check for the existance of a value
406 **********************************************************************/
408 bool regval_ctr_key_exists(struct regval_ctr *ctr, const char *value)
410 int i;
412 for ( i=0; i<ctr->num_values; i++ ) {
413 if ( strequal( ctr->values[i]->valuename, value) )
414 return True;
417 return False;
420 /***********************************************************************
421 * compose a struct regval_blob from input data
422 **********************************************************************/
424 struct regval_blob *regval_compose(TALLOC_CTX *ctx, const char *name,
425 uint16 type,
426 const char *data_p, size_t size)
428 struct regval_blob *regval = TALLOC_P(ctx, struct regval_blob);
430 if (regval == NULL) {
431 return NULL;
434 fstrcpy(regval->valuename, name);
435 regval->type = type;
436 if (size) {
437 regval->data_p = (uint8 *)TALLOC_MEMDUP(regval, data_p, size);
438 if (!regval->data_p) {
439 TALLOC_FREE(regval);
440 return NULL;
442 } else {
443 regval->data_p = NULL;
445 regval->size = size;
447 return regval;
450 /***********************************************************************
451 Add a new registry value to the array
452 **********************************************************************/
454 int regval_ctr_addvalue(struct regval_ctr *ctr, const char *name, uint16 type,
455 const char *data_p, size_t size)
457 if ( !name )
458 return ctr->num_values;
460 /* Delete the current value (if it exists) and add the new one */
462 regval_ctr_delvalue( ctr, name );
464 /* allocate a slot in the array of pointers */
466 if ( ctr->num_values == 0 ) {
467 ctr->values = TALLOC_P( ctr, struct regval_blob *);
468 } else {
469 ctr->values = TALLOC_REALLOC_ARRAY(ctr, ctr->values,
470 struct regval_blob *,
471 ctr->num_values+1);
474 if (!ctr->values) {
475 ctr->num_values = 0;
476 return 0;
479 /* allocate a new value and store the pointer in the arrya */
481 ctr->values[ctr->num_values] = regval_compose(ctr, name, type, data_p,
482 size);
483 if (ctr->values[ctr->num_values] == NULL) {
484 ctr->num_values = 0;
485 return 0;
487 ctr->num_values++;
489 return ctr->num_values;
492 /***********************************************************************
493 Add a new registry SZ value to the array
494 **********************************************************************/
496 int regval_ctr_addvalue_sz(struct regval_ctr *ctr, const char *name, const char *data)
498 DATA_BLOB blob;
500 if (!push_reg_sz(ctr, &blob, data)) {
501 return -1;
504 return regval_ctr_addvalue(ctr, name, REG_SZ,
505 (const char *)blob.data,
506 blob.length);
509 /***********************************************************************
510 Add a new registry MULTI_SZ value to the array
511 **********************************************************************/
513 int regval_ctr_addvalue_multi_sz(struct regval_ctr *ctr, const char *name, const char **data)
515 DATA_BLOB blob;
517 if (!push_reg_multi_sz(ctr, &blob, data)) {
518 return -1;
521 return regval_ctr_addvalue(ctr, name, REG_MULTI_SZ,
522 (const char *)blob.data,
523 blob.length);
526 /***********************************************************************
527 Add a new registry value to the array
528 **********************************************************************/
530 int regval_ctr_copyvalue(struct regval_ctr *ctr, struct regval_blob *val)
532 if ( val ) {
533 regval_ctr_addvalue(ctr, val->valuename, val->type,
534 (char *)val->data_p, val->size);
537 return ctr->num_values;
540 /***********************************************************************
541 Delete a single value from the registry container.
542 No need to free memory since it is talloc'd.
543 **********************************************************************/
545 int regval_ctr_delvalue(struct regval_ctr *ctr, const char *name)
547 int i;
549 for ( i=0; i<ctr->num_values; i++ ) {
550 if ( strequal( ctr->values[i]->valuename, name ) )
551 break;
554 /* just return if we don't find it */
556 if ( i == ctr->num_values )
557 return ctr->num_values;
559 /* If 'i' was not the last element, just shift everything down one */
560 ctr->num_values--;
561 if ( i < ctr->num_values )
562 memmove(&ctr->values[i], &ctr->values[i+1],
563 sizeof(struct regval_blob*)*(ctr->num_values-i));
565 return ctr->num_values;
568 /***********************************************************************
569 Retrieve single value from the registry container.
570 No need to free memory since it is talloc'd.
571 **********************************************************************/
573 struct regval_blob* regval_ctr_getvalue(struct regval_ctr *ctr,
574 const char *name)
576 int i;
578 /* search for the value */
580 for ( i=0; i<ctr->num_values; i++ ) {
581 if ( strequal( ctr->values[i]->valuename, name ) )
582 return ctr->values[i];
585 return NULL;
588 /***********************************************************************
589 return the data_p as a uint32
590 **********************************************************************/
592 uint32 regval_dword(struct regval_blob *val)
594 uint32 data;
596 data = IVAL( regval_data_p(val), 0 );
598 return data;
601 /***********************************************************************
602 return the data_p as a character string
603 **********************************************************************/
605 const char *regval_sz(struct regval_blob *val)
607 const char *data = NULL;
608 DATA_BLOB blob = data_blob_const(regval_data_p(val), regval_size(val));
610 pull_reg_sz(talloc_tos(), &blob, &data);
612 return data;