libsmb: Remove unused setup_stat_from_stat_ex()
[Samba.git] / source3 / registry / reg_objects.c
blob20556b1a174d9eb55c900448e9f633212c4d9b41
1 /*
2 * Unix SMB/CIFS implementation.
3 * Virtual Windows Registry Layer
4 * Copyright (C) Gerald Carter 2002-2005
5 * Copyright (C) Michael Adam 2007-2010
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 /* Implementation of registry frontend view functions. */
23 #include "includes.h"
24 #include "registry.h"
25 #include "reg_objects.h"
26 #include "util_tdb.h"
27 #include "dbwrap/dbwrap.h"
28 #include "dbwrap/dbwrap_rbt.h"
29 #include "../libcli/registry/util_reg.h"
30 #include "lib/util/string_wrappers.h"
32 #undef DBGC_CLASS
33 #define DBGC_CLASS DBGC_REGISTRY
35 /* low level structure to contain registry values */
37 struct regval_blob {
38 fstring valuename;
39 uint32_t type;
40 /* this should be encapsulated in an RPC_DATA_BLOB */
41 uint32_t size; /* in bytes */
42 uint8_t *data_p;
45 /* container for registry values */
47 struct regval_ctr {
48 uint32_t num_values;
49 struct regval_blob **values;
50 int seqnum;
53 struct regsubkey_ctr {
54 uint32_t num_subkeys;
55 char **subkeys;
56 struct db_context *subkeys_hash;
57 int seqnum;
60 /**********************************************************************
62 Note that the struct regsubkey_ctr and struct regval_ctr objects *must* be
63 talloc()'d since the methods use the object pointer as the talloc
64 context for internal private data.
66 There is no longer a regval_ctr_init() and regval_ctr_destroy()
67 pair of functions. Simply talloc_zero() and TALLOC_FREE() the
68 object.
70 **********************************************************************/
72 WERROR regsubkey_ctr_init(TALLOC_CTX *mem_ctx, struct regsubkey_ctr **ctr)
74 if (ctr == NULL) {
75 return WERR_INVALID_PARAMETER;
78 *ctr = talloc_zero(mem_ctx, struct regsubkey_ctr);
79 if (*ctr == NULL) {
80 return WERR_NOT_ENOUGH_MEMORY;
83 (*ctr)->subkeys_hash = db_open_rbt(*ctr);
84 if ((*ctr)->subkeys_hash == NULL) {
85 talloc_free(*ctr);
86 return WERR_NOT_ENOUGH_MEMORY;
89 return WERR_OK;
92 /**
93 * re-initialize the list of subkeys (to the empty list)
94 * in an already allocated regsubkey_ctr
97 WERROR regsubkey_ctr_reinit(struct regsubkey_ctr *ctr)
99 if (ctr == NULL) {
100 return WERR_INVALID_PARAMETER;
103 talloc_free(ctr->subkeys_hash);
104 ctr->subkeys_hash = db_open_rbt(ctr);
105 W_ERROR_HAVE_NO_MEMORY(ctr->subkeys_hash);
107 TALLOC_FREE(ctr->subkeys);
109 ctr->num_subkeys = 0;
110 ctr->seqnum = 0;
112 return WERR_OK;
115 WERROR regsubkey_ctr_set_seqnum(struct regsubkey_ctr *ctr, int seqnum)
117 if (ctr == NULL) {
118 return WERR_INVALID_PARAMETER;
121 ctr->seqnum = seqnum;
123 return WERR_OK;
126 int regsubkey_ctr_get_seqnum(struct regsubkey_ctr *ctr)
128 if (ctr == NULL) {
129 return -1;
132 return ctr->seqnum;
135 static WERROR regsubkey_ctr_hash_keyname(struct regsubkey_ctr *ctr,
136 const char *keyname,
137 uint32_t idx)
139 WERROR werr;
141 werr = ntstatus_to_werror(dbwrap_store_bystring_upper(ctr->subkeys_hash,
142 keyname,
143 make_tdb_data((uint8_t *)&idx,
144 sizeof(idx)),
145 TDB_REPLACE));
146 if (!W_ERROR_IS_OK(werr)) {
147 DEBUG(1, ("error hashing new key '%s' in container: %s\n",
148 keyname, win_errstr(werr)));
151 return werr;
154 static WERROR regsubkey_ctr_unhash_keyname(struct regsubkey_ctr *ctr,
155 const char *keyname)
157 WERROR werr;
159 werr = ntstatus_to_werror(dbwrap_delete_bystring_upper(ctr->subkeys_hash,
160 keyname));
161 if (!W_ERROR_IS_OK(werr)) {
162 DEBUG(1, ("error unhashing key '%s' in container: %s\n",
163 keyname, win_errstr(werr)));
166 return werr;
169 static WERROR regsubkey_ctr_index_for_keyname(struct regsubkey_ctr *ctr,
170 const char *keyname,
171 uint32_t *idx)
173 TDB_DATA data;
174 NTSTATUS status;
176 if ((ctr == NULL) || (keyname == NULL)) {
177 return WERR_INVALID_PARAMETER;
180 status = dbwrap_fetch_bystring_upper(ctr->subkeys_hash, ctr, keyname,
181 &data);
182 if (!NT_STATUS_IS_OK(status)) {
183 return WERR_NOT_FOUND;
186 if (data.dsize != sizeof(*idx)) {
187 talloc_free(data.dptr);
188 return WERR_INVALID_DATATYPE;
191 if (idx != NULL) {
192 memcpy(idx, data.dptr, sizeof(*idx));
195 talloc_free(data.dptr);
196 return WERR_OK;
199 /***********************************************************************
200 Add a new key to the array
201 **********************************************************************/
203 WERROR regsubkey_ctr_addkey( struct regsubkey_ctr *ctr, const char *keyname )
205 char **newkeys;
206 WERROR werr;
208 if ( !keyname ) {
209 return WERR_OK;
212 /* make sure the keyname is not already there */
214 if ( regsubkey_ctr_key_exists( ctr, keyname ) ) {
215 return WERR_OK;
218 if (!(newkeys = talloc_realloc(ctr, ctr->subkeys, char *,
219 ctr->num_subkeys+1))) {
220 return WERR_NOT_ENOUGH_MEMORY;
223 ctr->subkeys = newkeys;
225 if (!(ctr->subkeys[ctr->num_subkeys] = talloc_strdup(ctr->subkeys,
226 keyname ))) {
228 * Don't shrink the new array again, this wastes a pointer
230 return WERR_NOT_ENOUGH_MEMORY;
233 werr = regsubkey_ctr_hash_keyname(ctr, keyname, ctr->num_subkeys);
234 W_ERROR_NOT_OK_RETURN(werr);
236 ctr->num_subkeys++;
238 return WERR_OK;
241 /***********************************************************************
242 Delete a key from the array
243 **********************************************************************/
245 WERROR regsubkey_ctr_delkey( struct regsubkey_ctr *ctr, const char *keyname )
247 WERROR werr;
248 uint32_t idx, j;
250 if (keyname == NULL) {
251 return WERR_INVALID_PARAMETER;
254 /* make sure the keyname is actually already there */
256 werr = regsubkey_ctr_index_for_keyname(ctr, keyname, &idx);
257 W_ERROR_NOT_OK_RETURN(werr);
259 werr = regsubkey_ctr_unhash_keyname(ctr, keyname);
260 W_ERROR_NOT_OK_RETURN(werr);
262 /* update if we have any keys left */
263 ctr->num_subkeys--;
264 if (idx < ctr->num_subkeys) {
265 memmove(&ctr->subkeys[idx], &ctr->subkeys[idx+1],
266 sizeof(char *) * (ctr->num_subkeys - idx));
268 /* we have to re-hash rest of the array... :-( */
269 for (j = idx; j < ctr->num_subkeys; j++) {
270 werr = regsubkey_ctr_hash_keyname(ctr, ctr->subkeys[j], j);
271 W_ERROR_NOT_OK_RETURN(werr);
275 return WERR_OK;
278 /***********************************************************************
279 Check for the existence of a key
280 **********************************************************************/
282 bool regsubkey_ctr_key_exists( struct regsubkey_ctr *ctr, const char *keyname )
284 WERROR werr;
286 if (!ctr->subkeys) {
287 return False;
290 werr = regsubkey_ctr_index_for_keyname(ctr, keyname, NULL);
291 if (!W_ERROR_IS_OK(werr)) {
292 return false;
295 return true;
298 /***********************************************************************
299 How many keys does the container hold ?
300 **********************************************************************/
302 uint32_t regsubkey_ctr_numkeys( struct regsubkey_ctr *ctr )
304 return ctr->num_subkeys;
307 /***********************************************************************
308 Retrieve a specific key string
309 **********************************************************************/
311 char* regsubkey_ctr_specific_key( struct regsubkey_ctr *ctr, uint32_t key_index )
313 if ( ! (key_index < ctr->num_subkeys) )
314 return NULL;
316 return ctr->subkeys[key_index];
320 * Utility functions for struct regval_ctr
324 * allocate a regval_ctr structure.
326 WERROR regval_ctr_init(TALLOC_CTX *mem_ctx, struct regval_ctr **ctr)
328 if (ctr == NULL) {
329 return WERR_INVALID_PARAMETER;
332 *ctr = talloc_zero(mem_ctx, struct regval_ctr);
333 if (*ctr == NULL) {
334 return WERR_NOT_ENOUGH_MEMORY;
337 return WERR_OK;
340 /***********************************************************************
341 How many keys does the container hold ?
342 **********************************************************************/
344 uint32_t regval_ctr_numvals(struct regval_ctr *ctr)
346 return ctr->num_values;
349 /**********************************************************************
350 *********************************************************************/
352 uint8_t* regval_data_p(struct regval_blob *val)
354 return val->data_p;
357 /**********************************************************************
358 *********************************************************************/
360 uint32_t regval_size(struct regval_blob *val)
362 return val->size;
365 /**********************************************************************
366 *********************************************************************/
368 char* regval_name(struct regval_blob *val)
370 return val->valuename;
373 /**********************************************************************
374 *********************************************************************/
376 uint32_t regval_type(struct regval_blob *val)
378 return val->type;
381 /***********************************************************************
382 Retrieve a pointer to a specific value. Caller should dup the structure
383 since this memory will go away when the ctr is free()'d
384 **********************************************************************/
386 struct regval_blob *regval_ctr_specific_value(struct regval_ctr *ctr,
387 uint32_t idx)
389 if ( !(idx < ctr->num_values) )
390 return NULL;
392 return ctr->values[idx];
395 /***********************************************************************
396 Check for the existence of a value
397 **********************************************************************/
399 bool regval_ctr_value_exists(struct regval_ctr *ctr, const char *value)
401 uint32_t i;
403 for ( i=0; i<ctr->num_values; i++ ) {
404 if ( strequal( ctr->values[i]->valuename, value) )
405 return True;
408 return False;
412 * Get a value by its name
414 struct regval_blob *regval_ctr_value_byname(struct regval_ctr *ctr,
415 const char *value)
417 uint32_t i;
419 for (i = 0; i < ctr->num_values; i++) {
420 if (strequal(ctr->values[i]->valuename, value)) {
421 return ctr->values[i];
425 return NULL;
429 /***********************************************************************
430 * compose a struct regval_blob from input data
431 **********************************************************************/
433 struct regval_blob *regval_compose(TALLOC_CTX *ctx, const char *name,
434 uint32_t type,
435 const uint8_t *data_p, size_t size)
437 struct regval_blob *regval = talloc(ctx, struct regval_blob);
439 if (regval == NULL) {
440 return NULL;
443 fstrcpy(regval->valuename, name);
444 regval->type = type;
445 if (size) {
446 regval->data_p = (uint8_t *)talloc_memdup(regval, data_p, size);
447 if (!regval->data_p) {
448 TALLOC_FREE(regval);
449 return NULL;
451 } else {
452 regval->data_p = NULL;
454 regval->size = size;
456 return regval;
459 /***********************************************************************
460 Add a new registry value to the array
461 **********************************************************************/
463 int regval_ctr_addvalue(struct regval_ctr *ctr, const char *name, uint32_t type,
464 const uint8_t *data_p, size_t size)
466 if ( !name )
467 return ctr->num_values;
469 /* Delete the current value (if it exists) and add the new one */
471 regval_ctr_delvalue( ctr, name );
473 /* allocate a slot in the array of pointers */
475 if ( ctr->num_values == 0 ) {
476 ctr->values = talloc( ctr, struct regval_blob *);
477 } else {
478 ctr->values = talloc_realloc(ctr, ctr->values,
479 struct regval_blob *,
480 ctr->num_values+1);
483 if (!ctr->values) {
484 ctr->num_values = 0;
485 return 0;
488 /* allocate a new value and store the pointer in the array */
490 ctr->values[ctr->num_values] = regval_compose(ctr, name, type, data_p,
491 size);
492 if (ctr->values[ctr->num_values] == NULL) {
493 ctr->num_values = 0;
494 return 0;
496 ctr->num_values++;
498 return ctr->num_values;
501 /***********************************************************************
502 Add a new registry SZ value to the array
503 **********************************************************************/
505 int regval_ctr_addvalue_sz(struct regval_ctr *ctr, const char *name, const char *data)
507 DATA_BLOB blob;
509 if (!push_reg_sz(ctr, &blob, data)) {
510 return -1;
513 return regval_ctr_addvalue(ctr, name, REG_SZ,
514 (const uint8_t *)blob.data,
515 blob.length);
518 /***********************************************************************
519 Add a new registry MULTI_SZ value to the array
520 **********************************************************************/
522 int regval_ctr_addvalue_multi_sz(struct regval_ctr *ctr, const char *name, const char **data)
524 DATA_BLOB blob;
526 if (!push_reg_multi_sz(ctr, &blob, data)) {
527 return -1;
530 return regval_ctr_addvalue(ctr, name, REG_MULTI_SZ,
531 (const uint8_t *)blob.data,
532 blob.length);
535 /***********************************************************************
536 Add a new registry value to the array
537 **********************************************************************/
539 int regval_ctr_copyvalue(struct regval_ctr *ctr, struct regval_blob *val)
541 if ( val ) {
542 regval_ctr_addvalue(ctr, val->valuename, val->type,
543 (uint8_t *)val->data_p, val->size);
546 return ctr->num_values;
549 /***********************************************************************
550 Delete a single value from the registry container.
551 No need to free memory since it is talloc'd.
552 **********************************************************************/
554 int regval_ctr_delvalue(struct regval_ctr *ctr, const char *name)
556 uint32_t i;
558 for ( i=0; i<ctr->num_values; i++ ) {
559 if ( strequal( ctr->values[i]->valuename, name ) )
560 break;
563 /* just return if we don't find it */
565 if ( i == ctr->num_values )
566 return ctr->num_values;
568 /* If 'i' was not the last element, just shift everything down one */
569 ctr->num_values--;
570 if ( i < ctr->num_values )
571 memmove(&ctr->values[i], &ctr->values[i+1],
572 sizeof(struct regval_blob*)*(ctr->num_values-i));
574 return ctr->num_values;
577 /***********************************************************************
578 Retrieve single value from the registry container.
579 No need to free memory since it is talloc'd.
580 **********************************************************************/
582 struct regval_blob* regval_ctr_getvalue(struct regval_ctr *ctr,
583 const char *name)
585 uint32_t i;
587 /* search for the value */
589 for ( i=0; i<ctr->num_values; i++ ) {
590 if ( strequal( ctr->values[i]->valuename, name ) )
591 return ctr->values[i];
594 return NULL;
597 int regval_ctr_get_seqnum(struct regval_ctr *ctr)
599 if (ctr == NULL) {
600 return -1;
603 return ctr->seqnum;
606 WERROR regval_ctr_set_seqnum(struct regval_ctr *ctr, int seqnum)
608 if (ctr == NULL) {
609 return WERR_INVALID_PARAMETER;
612 ctr->seqnum = seqnum;
614 return WERR_OK;