s3-waf: ndr string functions moved to top level
[Samba/ekacnet.git] / source3 / registry / reg_objects.c
bloba1d085b5865aedea9dc44d54a21bb237e6ebde42
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 struct regsubkey_ctr {
29 uint32_t num_subkeys;
30 char **subkeys;
31 struct db_context *subkeys_hash;
32 int seqnum;
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
43 object.
45 **********************************************************************/
47 WERROR regsubkey_ctr_init(TALLOC_CTX *mem_ctx, struct regsubkey_ctr **ctr)
49 if (ctr == NULL) {
50 return WERR_INVALID_PARAM;
53 *ctr = talloc_zero(mem_ctx, struct regsubkey_ctr);
54 if (*ctr == NULL) {
55 return WERR_NOMEM;
58 (*ctr)->subkeys_hash = db_open_rbt(*ctr);
59 if ((*ctr)->subkeys_hash == NULL) {
60 talloc_free(*ctr);
61 return WERR_NOMEM;
64 return WERR_OK;
67 /**
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)
74 if (ctr == NULL) {
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);
84 ctr->num_subkeys = 0;
85 ctr->seqnum = 0;
87 return WERR_OK;
90 WERROR regsubkey_ctr_set_seqnum(struct regsubkey_ctr *ctr, int seqnum)
92 if (ctr == NULL) {
93 return WERR_INVALID_PARAM;
96 ctr->seqnum = seqnum;
98 return WERR_OK;
101 int regsubkey_ctr_get_seqnum(struct regsubkey_ctr *ctr)
103 if (ctr == NULL) {
104 return -1;
107 return ctr->seqnum;
110 static WERROR regsubkey_ctr_hash_keyname(struct regsubkey_ctr *ctr,
111 const char *keyname,
112 uint32 idx)
114 WERROR werr;
116 werr = ntstatus_to_werror(dbwrap_store_bystring_upper(ctr->subkeys_hash,
117 keyname,
118 make_tdb_data((uint8 *)&idx,
119 sizeof(idx)),
120 TDB_REPLACE));
121 if (!W_ERROR_IS_OK(werr)) {
122 DEBUG(1, ("error hashing new key '%s' in container: %s\n",
123 keyname, win_errstr(werr)));
126 return werr;
129 static WERROR regsubkey_ctr_unhash_keyname(struct regsubkey_ctr *ctr,
130 const char *keyname)
132 WERROR werr;
134 werr = ntstatus_to_werror(dbwrap_delete_bystring_upper(ctr->subkeys_hash,
135 keyname));
136 if (!W_ERROR_IS_OK(werr)) {
137 DEBUG(1, ("error unhashing key '%s' in container: %s\n",
138 keyname, win_errstr(werr)));
141 return werr;
144 static WERROR regsubkey_ctr_index_for_keyname(struct regsubkey_ctr *ctr,
145 const char *keyname,
146 uint32 *idx)
148 TDB_DATA data;
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;
164 if (idx != NULL) {
165 *idx = *(uint32 *)data.dptr;
168 talloc_free(data.dptr);
169 return WERR_OK;
172 /***********************************************************************
173 Add a new key to the array
174 **********************************************************************/
176 WERROR regsubkey_ctr_addkey( struct regsubkey_ctr *ctr, const char *keyname )
178 char **newkeys;
179 WERROR werr;
181 if ( !keyname ) {
182 return WERR_OK;
185 /* make sure the keyname is not already there */
187 if ( regsubkey_ctr_key_exists( ctr, keyname ) ) {
188 return WERR_OK;
191 if (!(newkeys = TALLOC_REALLOC_ARRAY(ctr, ctr->subkeys, char *,
192 ctr->num_subkeys+1))) {
193 return WERR_NOMEM;
196 ctr->subkeys = newkeys;
198 if (!(ctr->subkeys[ctr->num_subkeys] = talloc_strdup(ctr->subkeys,
199 keyname ))) {
201 * Don't shrink the new array again, this wastes a pointer
203 return WERR_NOMEM;
206 werr = regsubkey_ctr_hash_keyname(ctr, keyname, ctr->num_subkeys);
207 W_ERROR_NOT_OK_RETURN(werr);
209 ctr->num_subkeys++;
211 return WERR_OK;
214 /***********************************************************************
215 Delete a key from the array
216 **********************************************************************/
218 WERROR regsubkey_ctr_delkey( struct regsubkey_ctr *ctr, const char *keyname )
220 WERROR werr;
221 uint32 idx, j;
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 */
236 ctr->num_subkeys--;
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);
248 return WERR_OK;
251 /***********************************************************************
252 Check for the existance of a key
253 **********************************************************************/
255 bool regsubkey_ctr_key_exists( struct regsubkey_ctr *ctr, const char *keyname )
257 WERROR werr;
259 if (!ctr->subkeys) {
260 return False;
263 werr = regsubkey_ctr_index_for_keyname(ctr, keyname, NULL);
264 if (!W_ERROR_IS_OK(werr)) {
265 return false;
268 return true;
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) )
287 return NULL;
289 return ctr->subkeys[key_index];
293 * Utility functions for struct regval_ctr
296 /***********************************************************************
297 How many keys does the container hold ?
298 **********************************************************************/
300 int regval_ctr_numvals(struct regval_ctr *ctr)
302 return ctr->num_values;
305 /***********************************************************************
306 allocate memory for and duplicate a struct regval_blob.
307 This is malloc'd memory so the caller should free it when done
308 **********************************************************************/
310 struct regval_blob* dup_registry_value(struct regval_blob *val)
312 struct regval_blob *copy = NULL;
314 if ( !val )
315 return NULL;
317 if ( !(copy = SMB_MALLOC_P( struct regval_blob)) ) {
318 DEBUG(0,("dup_registry_value: malloc() failed!\n"));
319 return NULL;
322 /* copy all the non-pointer initial data */
324 memcpy( copy, val, sizeof(struct regval_blob) );
326 copy->size = 0;
327 copy->data_p = NULL;
329 if ( val->data_p && val->size )
331 if ( !(copy->data_p = (uint8 *)memdup( val->data_p,
332 val->size )) ) {
333 DEBUG(0,("dup_registry_value: memdup() failed for [%d] "
334 "bytes!\n", val->size));
335 SAFE_FREE( copy );
336 return NULL;
338 copy->size = val->size;
341 return copy;
344 /**********************************************************************
345 free the memory allocated to a struct regval_blob
346 *********************************************************************/
348 void free_registry_value(struct regval_blob *val)
350 if ( !val )
351 return;
353 SAFE_FREE( val->data_p );
354 SAFE_FREE( val );
356 return;
359 /**********************************************************************
360 *********************************************************************/
362 uint8* regval_data_p(struct regval_blob *val)
364 return val->data_p;
367 /**********************************************************************
368 *********************************************************************/
370 uint32 regval_size(struct regval_blob *val)
372 return val->size;
375 /**********************************************************************
376 *********************************************************************/
378 char* regval_name(struct regval_blob *val)
380 return val->valuename;
383 /**********************************************************************
384 *********************************************************************/
386 uint32 regval_type(struct regval_blob *val)
388 return val->type;
391 /***********************************************************************
392 Retreive a pointer to a specific value. Caller shoud dup the structure
393 since this memory will go away when the ctr is free()'d
394 **********************************************************************/
396 struct regval_blob *regval_ctr_specific_value(struct regval_ctr *ctr,
397 uint32 idx)
399 if ( !(idx < ctr->num_values) )
400 return NULL;
402 return ctr->values[idx];
405 /***********************************************************************
406 Check for the existance of a value
407 **********************************************************************/
409 bool regval_ctr_key_exists(struct regval_ctr *ctr, const char *value)
411 int i;
413 for ( i=0; i<ctr->num_values; i++ ) {
414 if ( strequal( ctr->values[i]->valuename, value) )
415 return True;
418 return False;
421 /***********************************************************************
422 * compose a struct regval_blob from input data
423 **********************************************************************/
425 struct regval_blob *regval_compose(TALLOC_CTX *ctx, const char *name,
426 uint16 type,
427 const char *data_p, size_t size)
429 struct regval_blob *regval = TALLOC_P(ctx, struct regval_blob);
431 if (regval == NULL) {
432 return NULL;
435 fstrcpy(regval->valuename, name);
436 regval->type = type;
437 if (size) {
438 regval->data_p = (uint8 *)TALLOC_MEMDUP(regval, data_p, size);
439 if (!regval->data_p) {
440 TALLOC_FREE(regval);
441 return NULL;
443 } else {
444 regval->data_p = NULL;
446 regval->size = size;
448 return regval;
451 /***********************************************************************
452 Add a new registry value to the array
453 **********************************************************************/
455 int regval_ctr_addvalue(struct regval_ctr *ctr, const char *name, uint16 type,
456 const char *data_p, size_t size)
458 if ( !name )
459 return ctr->num_values;
461 /* Delete the current value (if it exists) and add the new one */
463 regval_ctr_delvalue( ctr, name );
465 /* allocate a slot in the array of pointers */
467 if ( ctr->num_values == 0 ) {
468 ctr->values = TALLOC_P( ctr, struct regval_blob *);
469 } else {
470 ctr->values = TALLOC_REALLOC_ARRAY(ctr, ctr->values,
471 struct regval_blob *,
472 ctr->num_values+1);
475 if (!ctr->values) {
476 ctr->num_values = 0;
477 return 0;
480 /* allocate a new value and store the pointer in the arrya */
482 ctr->values[ctr->num_values] = regval_compose(ctr, name, type, data_p,
483 size);
484 if (ctr->values[ctr->num_values] == NULL) {
485 ctr->num_values = 0;
486 return 0;
488 ctr->num_values++;
490 return ctr->num_values;
493 /***********************************************************************
494 Add a new registry SZ value to the array
495 **********************************************************************/
497 int regval_ctr_addvalue_sz(struct regval_ctr *ctr, const char *name, const char *data)
499 DATA_BLOB blob;
501 if (!push_reg_sz(ctr, &blob, data)) {
502 return -1;
505 return regval_ctr_addvalue(ctr, name, REG_SZ,
506 (const char *)blob.data,
507 blob.length);
510 /***********************************************************************
511 Add a new registry MULTI_SZ value to the array
512 **********************************************************************/
514 int regval_ctr_addvalue_multi_sz(struct regval_ctr *ctr, const char *name, const char **data)
516 DATA_BLOB blob;
518 if (!push_reg_multi_sz(ctr, &blob, data)) {
519 return -1;
522 return regval_ctr_addvalue(ctr, name, REG_MULTI_SZ,
523 (const char *)blob.data,
524 blob.length);
527 /***********************************************************************
528 Add a new registry value to the array
529 **********************************************************************/
531 int regval_ctr_copyvalue(struct regval_ctr *ctr, struct regval_blob *val)
533 if ( val ) {
534 regval_ctr_addvalue(ctr, val->valuename, val->type,
535 (char *)val->data_p, val->size);
538 return ctr->num_values;
541 /***********************************************************************
542 Delete a single value from the registry container.
543 No need to free memory since it is talloc'd.
544 **********************************************************************/
546 int regval_ctr_delvalue(struct regval_ctr *ctr, const char *name)
548 int i;
550 for ( i=0; i<ctr->num_values; i++ ) {
551 if ( strequal( ctr->values[i]->valuename, name ) )
552 break;
555 /* just return if we don't find it */
557 if ( i == ctr->num_values )
558 return ctr->num_values;
560 /* If 'i' was not the last element, just shift everything down one */
561 ctr->num_values--;
562 if ( i < ctr->num_values )
563 memmove(&ctr->values[i], &ctr->values[i+1],
564 sizeof(struct regval_blob*)*(ctr->num_values-i));
566 return ctr->num_values;
569 /***********************************************************************
570 Retrieve single value from the registry container.
571 No need to free memory since it is talloc'd.
572 **********************************************************************/
574 struct regval_blob* regval_ctr_getvalue(struct regval_ctr *ctr,
575 const char *name)
577 int i;
579 /* search for the value */
581 for ( i=0; i<ctr->num_values; i++ ) {
582 if ( strequal( ctr->values[i]->valuename, name ) )
583 return ctr->values[i];
586 return NULL;
589 /***********************************************************************
590 return the data_p as a uint32
591 **********************************************************************/
593 uint32 regval_dword(struct regval_blob *val)
595 uint32 data;
597 data = IVAL( regval_data_p(val), 0 );
599 return data;
602 /***********************************************************************
603 return the data_p as a character string
604 **********************************************************************/
606 const char *regval_sz(struct regval_blob *val)
608 const char *data = NULL;
609 DATA_BLOB blob = data_blob_const(regval_data_p(val), regval_size(val));
611 pull_reg_sz(talloc_tos(), &blob, &data);
613 return data;