s3/docs: Fix typo.
[Samba/gebeck_regimport.git] / source3 / registry / reg_objects.c
blob5ae1cd8aa7dec7f6b9585fb6cf295cef2e32b838
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 WERROR regsubkey_ctr_set_seqnum(struct regsubkey_ctr *ctr, int seqnum)
68 if (ctr == NULL) {
69 return WERR_INVALID_PARAM;
72 ctr->seqnum = seqnum;
74 return WERR_OK;
77 int regsubkey_ctr_get_seqnum(struct regsubkey_ctr *ctr)
79 if (ctr == NULL) {
80 return -1;
83 return ctr->seqnum;
86 static WERROR regsubkey_ctr_hash_keyname(struct regsubkey_ctr *ctr,
87 const char *keyname,
88 uint32 idx)
90 WERROR werr;
92 werr = ntstatus_to_werror(dbwrap_store_bystring(ctr->subkeys_hash,
93 keyname,
94 make_tdb_data((uint8 *)&idx,
95 sizeof(idx)),
96 TDB_REPLACE));
97 if (!W_ERROR_IS_OK(werr)) {
98 DEBUG(1, ("error hashing new key '%s' in container: %s\n",
99 keyname, win_errstr(werr)));
102 return werr;
105 static WERROR regsubkey_ctr_unhash_keyname(struct regsubkey_ctr *ctr,
106 const char *keyname)
108 WERROR werr;
110 werr = ntstatus_to_werror(dbwrap_delete_bystring(ctr->subkeys_hash,
111 keyname));
112 if (!W_ERROR_IS_OK(werr)) {
113 DEBUG(1, ("error unhashing key '%s' in container: %s\n",
114 keyname, win_errstr(werr)));
117 return werr;
120 static WERROR regsubkey_ctr_index_for_keyname(struct regsubkey_ctr *ctr,
121 const char *keyname,
122 uint32 *idx)
124 TDB_DATA data;
126 if ((ctr == NULL) || (keyname == NULL)) {
127 return WERR_INVALID_PARAM;
130 data = dbwrap_fetch_bystring(ctr->subkeys_hash, ctr, keyname);
131 if (data.dptr == NULL) {
132 return WERR_NOT_FOUND;
135 if (data.dsize != sizeof(*idx)) {
136 talloc_free(data.dptr);
137 return WERR_INVALID_DATATYPE;
140 if (idx != NULL) {
141 *idx = *(uint32 *)data.dptr;
144 talloc_free(data.dptr);
145 return WERR_OK;
148 /***********************************************************************
149 Add a new key to the array
150 **********************************************************************/
152 WERROR regsubkey_ctr_addkey( struct regsubkey_ctr *ctr, const char *keyname )
154 char **newkeys;
155 WERROR werr;
157 if ( !keyname ) {
158 return WERR_OK;
161 /* make sure the keyname is not already there */
163 if ( regsubkey_ctr_key_exists( ctr, keyname ) ) {
164 return WERR_OK;
167 if (!(newkeys = TALLOC_REALLOC_ARRAY(ctr, ctr->subkeys, char *,
168 ctr->num_subkeys+1))) {
169 return WERR_NOMEM;
172 ctr->subkeys = newkeys;
174 if (!(ctr->subkeys[ctr->num_subkeys] = talloc_strdup(ctr->subkeys,
175 keyname ))) {
177 * Don't shrink the new array again, this wastes a pointer
179 return WERR_NOMEM;
182 werr = regsubkey_ctr_hash_keyname(ctr, keyname, ctr->num_subkeys);
183 W_ERROR_NOT_OK_RETURN(werr);
185 ctr->num_subkeys++;
187 return WERR_OK;
190 /***********************************************************************
191 Delete a key from the array
192 **********************************************************************/
194 WERROR regsubkey_ctr_delkey( struct regsubkey_ctr *ctr, const char *keyname )
196 WERROR werr;
197 uint32 idx, j;
199 if (keyname == NULL) {
200 return WERR_INVALID_PARAM;
203 /* make sure the keyname is actually already there */
205 werr = regsubkey_ctr_index_for_keyname(ctr, keyname, &idx);
206 W_ERROR_NOT_OK_RETURN(werr);
208 werr = regsubkey_ctr_unhash_keyname(ctr, keyname);
209 W_ERROR_NOT_OK_RETURN(werr);
211 /* update if we have any keys left */
212 ctr->num_subkeys--;
213 if (idx < ctr->num_subkeys) {
214 memmove(&ctr->subkeys[idx], &ctr->subkeys[idx+1],
215 sizeof(char *) * (ctr->num_subkeys - idx));
217 /* we have to re-hash rest of the array... :-( */
218 for (j = idx; j < ctr->num_subkeys; j++) {
219 werr = regsubkey_ctr_hash_keyname(ctr, ctr->subkeys[j], j);
220 W_ERROR_NOT_OK_RETURN(werr);
224 return WERR_OK;
227 /***********************************************************************
228 Check for the existance of a key
229 **********************************************************************/
231 bool regsubkey_ctr_key_exists( struct regsubkey_ctr *ctr, const char *keyname )
233 WERROR werr;
235 if (!ctr->subkeys) {
236 return False;
239 werr = regsubkey_ctr_index_for_keyname(ctr, keyname, NULL);
240 if (!W_ERROR_IS_OK(werr)) {
241 return false;
244 return true;
247 /***********************************************************************
248 How many keys does the container hold ?
249 **********************************************************************/
251 int regsubkey_ctr_numkeys( struct regsubkey_ctr *ctr )
253 return ctr->num_subkeys;
256 /***********************************************************************
257 Retreive a specific key string
258 **********************************************************************/
260 char* regsubkey_ctr_specific_key( struct regsubkey_ctr *ctr, uint32_t key_index )
262 if ( ! (key_index < ctr->num_subkeys) )
263 return NULL;
265 return ctr->subkeys[key_index];
269 * Utility functions for struct regval_ctr
272 /***********************************************************************
273 How many keys does the container hold ?
274 **********************************************************************/
276 int regval_ctr_numvals(struct regval_ctr *ctr)
278 return ctr->num_values;
281 /***********************************************************************
282 allocate memory for and duplicate a struct regval_blob.
283 This is malloc'd memory so the caller should free it when done
284 **********************************************************************/
286 struct regval_blob* dup_registry_value(struct regval_blob *val)
288 struct regval_blob *copy = NULL;
290 if ( !val )
291 return NULL;
293 if ( !(copy = SMB_MALLOC_P( struct regval_blob)) ) {
294 DEBUG(0,("dup_registry_value: malloc() failed!\n"));
295 return NULL;
298 /* copy all the non-pointer initial data */
300 memcpy( copy, val, sizeof(struct regval_blob) );
302 copy->size = 0;
303 copy->data_p = NULL;
305 if ( val->data_p && val->size )
307 if ( !(copy->data_p = (uint8 *)memdup( val->data_p,
308 val->size )) ) {
309 DEBUG(0,("dup_registry_value: memdup() failed for [%d] "
310 "bytes!\n", val->size));
311 SAFE_FREE( copy );
312 return NULL;
314 copy->size = val->size;
317 return copy;
320 /**********************************************************************
321 free the memory allocated to a struct regval_blob
322 *********************************************************************/
324 void free_registry_value(struct regval_blob *val)
326 if ( !val )
327 return;
329 SAFE_FREE( val->data_p );
330 SAFE_FREE( val );
332 return;
335 /**********************************************************************
336 *********************************************************************/
338 uint8* regval_data_p(struct regval_blob *val)
340 return val->data_p;
343 /**********************************************************************
344 *********************************************************************/
346 uint32 regval_size(struct regval_blob *val)
348 return val->size;
351 /**********************************************************************
352 *********************************************************************/
354 char* regval_name(struct regval_blob *val)
356 return val->valuename;
359 /**********************************************************************
360 *********************************************************************/
362 uint32 regval_type(struct regval_blob *val)
364 return val->type;
367 /***********************************************************************
368 Retreive a pointer to a specific value. Caller shoud dup the structure
369 since this memory will go away when the ctr is free()'d
370 **********************************************************************/
372 struct regval_blob *regval_ctr_specific_value(struct regval_ctr *ctr,
373 uint32 idx)
375 if ( !(idx < ctr->num_values) )
376 return NULL;
378 return ctr->values[idx];
381 /***********************************************************************
382 Check for the existance of a value
383 **********************************************************************/
385 bool regval_ctr_key_exists(struct regval_ctr *ctr, const char *value)
387 int i;
389 for ( i=0; i<ctr->num_values; i++ ) {
390 if ( strequal( ctr->values[i]->valuename, value) )
391 return True;
394 return False;
397 /***********************************************************************
398 * compose a struct regval_blob from input data
399 **********************************************************************/
401 struct regval_blob *regval_compose(TALLOC_CTX *ctx, const char *name,
402 uint16 type,
403 const char *data_p, size_t size)
405 struct regval_blob *regval = TALLOC_P(ctx, struct regval_blob);
407 if (regval == NULL) {
408 return NULL;
411 fstrcpy(regval->valuename, name);
412 regval->type = type;
413 if (size) {
414 regval->data_p = (uint8 *)TALLOC_MEMDUP(regval, data_p, size);
415 if (!regval->data_p) {
416 TALLOC_FREE(regval);
417 return NULL;
419 } else {
420 regval->data_p = NULL;
422 regval->size = size;
424 return regval;
427 /***********************************************************************
428 Add a new registry value to the array
429 **********************************************************************/
431 int regval_ctr_addvalue(struct regval_ctr *ctr, const char *name, uint16 type,
432 const char *data_p, size_t size)
434 if ( !name )
435 return ctr->num_values;
437 /* Delete the current value (if it exists) and add the new one */
439 regval_ctr_delvalue( ctr, name );
441 /* allocate a slot in the array of pointers */
443 if ( ctr->num_values == 0 ) {
444 ctr->values = TALLOC_P( ctr, struct regval_blob *);
445 } else {
446 ctr->values = TALLOC_REALLOC_ARRAY(ctr, ctr->values,
447 struct regval_blob *,
448 ctr->num_values+1);
451 if (!ctr->values) {
452 ctr->num_values = 0;
453 return 0;
456 /* allocate a new value and store the pointer in the arrya */
458 ctr->values[ctr->num_values] = regval_compose(ctr, name, type, data_p,
459 size);
460 if (ctr->values[ctr->num_values] == NULL) {
461 ctr->num_values = 0;
462 return 0;
464 ctr->num_values++;
466 return ctr->num_values;
469 /***********************************************************************
470 Add a new registry value to the array
471 **********************************************************************/
473 int regval_ctr_copyvalue(struct regval_ctr *ctr, struct regval_blob *val)
475 if ( val ) {
476 regval_ctr_addvalue(ctr, val->valuename, val->type,
477 (char *)val->data_p, val->size);
480 return ctr->num_values;
483 /***********************************************************************
484 Delete a single value from the registry container.
485 No need to free memory since it is talloc'd.
486 **********************************************************************/
488 int regval_ctr_delvalue(struct regval_ctr *ctr, const char *name)
490 int i;
492 for ( i=0; i<ctr->num_values; i++ ) {
493 if ( strequal( ctr->values[i]->valuename, name ) )
494 break;
497 /* just return if we don't find it */
499 if ( i == ctr->num_values )
500 return ctr->num_values;
502 /* If 'i' was not the last element, just shift everything down one */
503 ctr->num_values--;
504 if ( i < ctr->num_values )
505 memmove(&ctr->values[i], &ctr->values[i+1],
506 sizeof(struct regval_blob*)*(ctr->num_values-i));
508 return ctr->num_values;
511 /***********************************************************************
512 Retrieve single value from the registry container.
513 No need to free memory since it is talloc'd.
514 **********************************************************************/
516 struct regval_blob* regval_ctr_getvalue(struct regval_ctr *ctr,
517 const char *name)
519 int i;
521 /* search for the value */
523 for ( i=0; i<ctr->num_values; i++ ) {
524 if ( strequal( ctr->values[i]->valuename, name ) )
525 return ctr->values[i];
528 return NULL;
531 /***********************************************************************
532 return the data_p as a uint32
533 **********************************************************************/
535 uint32 regval_dword(struct regval_blob *val)
537 uint32 data;
539 data = IVAL( regval_data_p(val), 0 );
541 return data;
544 /***********************************************************************
545 return the data_p as a character string
546 **********************************************************************/
548 char *regval_sz(struct regval_blob *val)
550 char *data = NULL;
552 rpcstr_pull_talloc(talloc_tos(), &data,
553 regval_data_p(val), regval_size(val),0);
554 return data;