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. */
25
#define DBGC_CLASS DBGC_REGISTRY
27
struct regsubkey_ctr {
30
struct db_context *subkeys_hash;
34
/**********************************************************************
36
Note that the struct regsubkey_ctr and 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
44
**********************************************************************/
46
WERROR regsubkey_ctr_init(TALLOC_CTX *mem_ctx, struct regsubkey_ctr **ctr)
49
return WERR_INVALID_PARAM;
52
*ctr = talloc_zero(mem_ctx, struct regsubkey_ctr);
57
(*ctr)->subkeys_hash = db_open_rbt(*ctr);
58
if ((*ctr)->subkeys_hash == NULL) {
66
WERROR regsubkey_ctr_set_seqnum(struct regsubkey_ctr *ctr, int seqnum)
69
return WERR_INVALID_PARAM;
77
int regsubkey_ctr_get_seqnum(struct regsubkey_ctr *ctr)
86
static WERROR regsubkey_ctr_hash_keyname(struct regsubkey_ctr *ctr,
92
werr = ntstatus_to_werror(dbwrap_store_bystring(ctr->subkeys_hash,
94
make_tdb_data((uint8 *)&idx,
97
if (!W_ERROR_IS_OK(werr)) {
98
DEBUG(1, ("error hashing new key '%s' in container: %s\n",
99
keyname, win_errstr(werr)));
105
static WERROR regsubkey_ctr_unhash_keyname(struct regsubkey_ctr *ctr,
110
werr = ntstatus_to_werror(dbwrap_delete_bystring(ctr->subkeys_hash,
112
if (!W_ERROR_IS_OK(werr)) {
113
DEBUG(1, ("error unhashing key '%s' in container: %s\n",
114
keyname, win_errstr(werr)));
120
static WERROR regsubkey_ctr_index_for_keyname(struct regsubkey_ctr *ctr,
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;
141
*idx = *(uint32 *)data.dptr;
144
talloc_free(data.dptr);
148
/***********************************************************************
149
Add a new key to the array
150
**********************************************************************/
152
WERROR regsubkey_ctr_addkey( struct regsubkey_ctr *ctr, const char *keyname )
161
/* make sure the keyname is not already there */
163
if ( regsubkey_ctr_key_exists( ctr, keyname ) ) {
167
if (!(newkeys = TALLOC_REALLOC_ARRAY(ctr, ctr->subkeys, char *,
168
ctr->num_subkeys+1))) {
172
ctr->subkeys = newkeys;
174
if (!(ctr->subkeys[ctr->num_subkeys] = talloc_strdup(ctr->subkeys,
177
* Don't shrink the new array again, this wastes a pointer
182
werr = regsubkey_ctr_hash_keyname(ctr, keyname, ctr->num_subkeys);
183
W_ERROR_NOT_OK_RETURN(werr);
190
/***********************************************************************
191
Delete a key from the array
192
**********************************************************************/
194
WERROR regsubkey_ctr_delkey( struct regsubkey_ctr *ctr, const char *keyname )
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 */
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);
227
/***********************************************************************
228
Check for the existance of a key
229
**********************************************************************/
231
bool regsubkey_ctr_key_exists( struct regsubkey_ctr *ctr, const char *keyname )
239
werr = regsubkey_ctr_index_for_keyname(ctr, keyname, NULL);
240
if (!W_ERROR_IS_OK(werr)) {
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) )
265
return ctr->subkeys[key_index];
269
* Utility functions for REGVAL_CTR
272
/***********************************************************************
273
How many keys does the container hold ?
274
**********************************************************************/
276
int regval_ctr_numvals( REGVAL_CTR *ctr )
278
return ctr->num_values;
281
/***********************************************************************
282
allocate memory for and duplicate a REGISTRY_VALUE.
283
This is malloc'd memory so the caller should free it when done
284
**********************************************************************/
286
REGISTRY_VALUE* dup_registry_value( REGISTRY_VALUE *val )
288
REGISTRY_VALUE *copy = NULL;
293
if ( !(copy = SMB_MALLOC_P( REGISTRY_VALUE)) ) {
294
DEBUG(0,("dup_registry_value: malloc() failed!\n"));
298
/* copy all the non-pointer initial data */
300
memcpy( copy, val, sizeof(REGISTRY_VALUE) );
305
if ( val->data_p && val->size )
307
if ( !(copy->data_p = (uint8 *)memdup( val->data_p,
309
DEBUG(0,("dup_registry_value: memdup() failed for [%d] "
310
"bytes!\n", val->size));
314
copy->size = val->size;
320
/**********************************************************************
321
free the memory allocated to a REGISTRY_VALUE
322
*********************************************************************/
324
void free_registry_value( REGISTRY_VALUE *val )
329
SAFE_FREE( val->data_p );
335
/**********************************************************************
336
*********************************************************************/
338
uint8* regval_data_p( REGISTRY_VALUE *val )
343
/**********************************************************************
344
*********************************************************************/
346
uint32 regval_size( REGISTRY_VALUE *val )
351
/**********************************************************************
352
*********************************************************************/
354
char* regval_name( REGISTRY_VALUE *val )
356
return val->valuename;
359
/**********************************************************************
360
*********************************************************************/
362
uint32 regval_type( REGISTRY_VALUE *val )
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
REGISTRY_VALUE* regval_ctr_specific_value( REGVAL_CTR *ctr, uint32 idx )
374
if ( !(idx < ctr->num_values) )
377
return ctr->values[idx];
380
/***********************************************************************
381
Check for the existance of a value
382
**********************************************************************/
384
bool regval_ctr_key_exists( REGVAL_CTR *ctr, const char *value )
388
for ( i=0; i<ctr->num_values; i++ ) {
389
if ( strequal( ctr->values[i]->valuename, value) )
396
/***********************************************************************
397
* compose a REGISTRY_VALUE from input data
398
**********************************************************************/
400
REGISTRY_VALUE *regval_compose(TALLOC_CTX *ctx, const char *name, uint16 type,
401
const char *data_p, size_t size)
403
REGISTRY_VALUE *regval = TALLOC_P(ctx, REGISTRY_VALUE);
405
if (regval == NULL) {
409
fstrcpy(regval->valuename, name);
412
regval->data_p = (uint8 *)TALLOC_MEMDUP(regval, data_p, size);
413
if (!regval->data_p) {
418
regval->data_p = NULL;
425
/***********************************************************************
426
Add a new registry value to the array
427
**********************************************************************/
429
int regval_ctr_addvalue( REGVAL_CTR *ctr, const char *name, uint16 type,
430
const char *data_p, size_t size )
433
return ctr->num_values;
435
/* Delete the current value (if it exists) and add the new one */
437
regval_ctr_delvalue( ctr, name );
439
/* allocate a slot in the array of pointers */
441
if ( ctr->num_values == 0 ) {
442
ctr->values = TALLOC_P( ctr, REGISTRY_VALUE *);
444
ctr->values = TALLOC_REALLOC_ARRAY(ctr, ctr->values,
454
/* allocate a new value and store the pointer in the arrya */
456
ctr->values[ctr->num_values] = regval_compose(ctr, name, type, data_p,
458
if (ctr->values[ctr->num_values] == NULL) {
464
return ctr->num_values;
467
/***********************************************************************
468
Add a new registry value to the array
469
**********************************************************************/
471
int regval_ctr_copyvalue( REGVAL_CTR *ctr, REGISTRY_VALUE *val )
474
regval_ctr_addvalue(ctr, val->valuename, val->type,
475
(char *)val->data_p, val->size);
478
return ctr->num_values;
481
/***********************************************************************
482
Delete a single value from the registry container.
483
No need to free memory since it is talloc'd.
484
**********************************************************************/
486
int regval_ctr_delvalue( REGVAL_CTR *ctr, const char *name )
490
for ( i=0; i<ctr->num_values; i++ ) {
491
if ( strequal( ctr->values[i]->valuename, name ) )
495
/* just return if we don't find it */
497
if ( i == ctr->num_values )
498
return ctr->num_values;
500
/* If 'i' was not the last element, just shift everything down one */
502
if ( i < ctr->num_values )
503
memmove(&ctr->values[i], &ctr->values[i+1],
504
sizeof(REGISTRY_VALUE*)*(ctr->num_values-i));
506
return ctr->num_values;
509
/***********************************************************************
510
Retrieve single value from the registry container.
511
No need to free memory since it is talloc'd.
512
**********************************************************************/
514
REGISTRY_VALUE* regval_ctr_getvalue( REGVAL_CTR *ctr, const char *name )
518
/* search for the value */
520
for ( i=0; i<ctr->num_values; i++ ) {
521
if ( strequal( ctr->values[i]->valuename, name ) )
522
return ctr->values[i];
528
/***********************************************************************
529
return the data_p as a uint32
530
**********************************************************************/
532
uint32 regval_dword( REGISTRY_VALUE *val )
536
data = IVAL( regval_data_p(val), 0 );
541
/***********************************************************************
542
return the data_p as a character string
543
**********************************************************************/
545
char *regval_sz(REGISTRY_VALUE *val)
549
rpcstr_pull_talloc(talloc_tos(), &data,
550
regval_data_p(val), regval_size(val),0);