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 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
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
) {
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
)
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
);
89 WERROR
regsubkey_ctr_set_seqnum(struct regsubkey_ctr
*ctr
, int seqnum
)
92 return WERR_INVALID_PARAM
;
100 int regsubkey_ctr_get_seqnum(struct regsubkey_ctr
*ctr
)
109 static WERROR
regsubkey_ctr_hash_keyname(struct regsubkey_ctr
*ctr
,
115 werr
= ntstatus_to_werror(dbwrap_store_bystring_upper(ctr
->subkeys_hash
,
117 make_tdb_data((uint8
*)&idx
,
120 if (!W_ERROR_IS_OK(werr
)) {
121 DEBUG(1, ("error hashing new key '%s' in container: %s\n",
122 keyname
, win_errstr(werr
)));
128 static WERROR
regsubkey_ctr_unhash_keyname(struct regsubkey_ctr
*ctr
,
133 werr
= ntstatus_to_werror(dbwrap_delete_bystring_upper(ctr
->subkeys_hash
,
135 if (!W_ERROR_IS_OK(werr
)) {
136 DEBUG(1, ("error unhashing key '%s' in container: %s\n",
137 keyname
, win_errstr(werr
)));
143 static WERROR
regsubkey_ctr_index_for_keyname(struct regsubkey_ctr
*ctr
,
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
;
164 *idx
= *(uint32
*)data
.dptr
;
167 talloc_free(data
.dptr
);
171 /***********************************************************************
172 Add a new key to the array
173 **********************************************************************/
175 WERROR
regsubkey_ctr_addkey( struct regsubkey_ctr
*ctr
, const char *keyname
)
184 /* make sure the keyname is not already there */
186 if ( regsubkey_ctr_key_exists( ctr
, keyname
) ) {
190 if (!(newkeys
= TALLOC_REALLOC_ARRAY(ctr
, ctr
->subkeys
, char *,
191 ctr
->num_subkeys
+1))) {
195 ctr
->subkeys
= newkeys
;
197 if (!(ctr
->subkeys
[ctr
->num_subkeys
] = talloc_strdup(ctr
->subkeys
,
200 * Don't shrink the new array again, this wastes a pointer
205 werr
= regsubkey_ctr_hash_keyname(ctr
, keyname
, ctr
->num_subkeys
);
206 W_ERROR_NOT_OK_RETURN(werr
);
213 /***********************************************************************
214 Delete a key from the array
215 **********************************************************************/
217 WERROR
regsubkey_ctr_delkey( struct regsubkey_ctr
*ctr
, const char *keyname
)
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 */
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
);
250 /***********************************************************************
251 Check for the existance of a key
252 **********************************************************************/
254 bool regsubkey_ctr_key_exists( struct regsubkey_ctr
*ctr
, const char *keyname
)
262 werr
= regsubkey_ctr_index_for_keyname(ctr
, keyname
, NULL
);
263 if (!W_ERROR_IS_OK(werr
)) {
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
) )
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
;
316 if ( !(copy
= SMB_MALLOC_P( struct regval_blob
)) ) {
317 DEBUG(0,("dup_registry_value: malloc() failed!\n"));
321 /* copy all the non-pointer initial data */
323 memcpy( copy
, val
, sizeof(struct regval_blob
) );
328 if ( val
->data_p
&& val
->size
)
330 if ( !(copy
->data_p
= (uint8
*)memdup( val
->data_p
,
332 DEBUG(0,("dup_registry_value: memdup() failed for [%d] "
333 "bytes!\n", val
->size
));
337 copy
->size
= val
->size
;
343 /**********************************************************************
344 free the memory allocated to a struct regval_blob
345 *********************************************************************/
347 void free_registry_value(struct regval_blob
*val
)
352 SAFE_FREE( val
->data_p
);
358 /**********************************************************************
359 *********************************************************************/
361 uint8
* regval_data_p(struct regval_blob
*val
)
366 /**********************************************************************
367 *********************************************************************/
369 uint32
regval_size(struct regval_blob
*val
)
374 /**********************************************************************
375 *********************************************************************/
377 char* regval_name(struct regval_blob
*val
)
379 return val
->valuename
;
382 /**********************************************************************
383 *********************************************************************/
385 uint32
regval_type(struct regval_blob
*val
)
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
,
398 if ( !(idx
< ctr
->num_values
) )
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
)
412 for ( i
=0; i
<ctr
->num_values
; i
++ ) {
413 if ( strequal( ctr
->values
[i
]->valuename
, value
) )
420 /***********************************************************************
421 * compose a struct regval_blob from input data
422 **********************************************************************/
424 struct regval_blob
*regval_compose(TALLOC_CTX
*ctx
, const char *name
,
426 const char *data_p
, size_t size
)
428 struct regval_blob
*regval
= TALLOC_P(ctx
, struct regval_blob
);
430 if (regval
== NULL
) {
434 fstrcpy(regval
->valuename
, name
);
437 regval
->data_p
= (uint8
*)TALLOC_MEMDUP(regval
, data_p
, size
);
438 if (!regval
->data_p
) {
443 regval
->data_p
= NULL
;
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
)
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
*);
469 ctr
->values
= TALLOC_REALLOC_ARRAY(ctr
, ctr
->values
,
470 struct regval_blob
*,
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
,
483 if (ctr
->values
[ctr
->num_values
] == NULL
) {
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
)
500 if (!push_reg_sz(ctr
, &blob
, data
)) {
504 return regval_ctr_addvalue(ctr
, name
, REG_SZ
,
505 (const char *)blob
.data
,
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
)
517 if (!push_reg_multi_sz(ctr
, &blob
, data
)) {
521 return regval_ctr_addvalue(ctr
, name
, REG_MULTI_SZ
,
522 (const char *)blob
.data
,
526 /***********************************************************************
527 Add a new registry value to the array
528 **********************************************************************/
530 int regval_ctr_copyvalue(struct regval_ctr
*ctr
, struct regval_blob
*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
)
549 for ( i
=0; i
<ctr
->num_values
; i
++ ) {
550 if ( strequal( ctr
->values
[i
]->valuename
, name
) )
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 */
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
,
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
];
588 /***********************************************************************
589 return the data_p as a uint32
590 **********************************************************************/
592 uint32
regval_dword(struct regval_blob
*val
)
596 data
= IVAL( regval_data_p(val
), 0 );
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
);