s3-rpc_server: Created a per connection spoolss pipe.
[Samba/ekacnet.git] / source3 / registry / reg_objects.c
blob459f7fca7022e01282defa8bebe3c45bdfa20df7
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"
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_REGISTRY
30 /* low level structure to contain registry values */
32 struct regval_blob {
33 fstring valuename;
34 uint16_t type;
35 /* this should be encapsulated in an RPC_DATA_BLOB */
36 uint32_t size; /* in bytes */
37 uint8_t *data_p;
40 /* container for registry values */
42 struct regval_ctr {
43 uint32_t num_values;
44 struct regval_blob **values;
45 int seqnum;
48 struct regsubkey_ctr {
49 uint32_t num_subkeys;
50 char **subkeys;
51 struct db_context *subkeys_hash;
52 int seqnum;
55 /**********************************************************************
57 Note that the struct regsubkey_ctr and struct regval_ctr objects *must* be
58 talloc()'d since the methods use the object pointer as the talloc
59 context for internal private data.
61 There is no longer a regval_ctr_intit() and regval_ctr_destroy()
62 pair of functions. Simply TALLOC_ZERO_P() and TALLOC_FREE() the
63 object.
65 **********************************************************************/
67 WERROR regsubkey_ctr_init(TALLOC_CTX *mem_ctx, struct regsubkey_ctr **ctr)
69 if (ctr == NULL) {
70 return WERR_INVALID_PARAM;
73 *ctr = talloc_zero(mem_ctx, struct regsubkey_ctr);
74 if (*ctr == NULL) {
75 return WERR_NOMEM;
78 (*ctr)->subkeys_hash = db_open_rbt(*ctr);
79 if ((*ctr)->subkeys_hash == NULL) {
80 talloc_free(*ctr);
81 return WERR_NOMEM;
84 return WERR_OK;
87 /**
88 * re-initialize the list of subkeys (to the emtpy list)
89 * in an already allocated regsubkey_ctr
92 WERROR regsubkey_ctr_reinit(struct regsubkey_ctr *ctr)
94 if (ctr == NULL) {
95 return WERR_INVALID_PARAM;
98 talloc_free(ctr->subkeys_hash);
99 ctr->subkeys_hash = db_open_rbt(ctr);
100 W_ERROR_HAVE_NO_MEMORY(ctr->subkeys_hash);
102 TALLOC_FREE(ctr->subkeys);
104 ctr->num_subkeys = 0;
105 ctr->seqnum = 0;
107 return WERR_OK;
110 WERROR regsubkey_ctr_set_seqnum(struct regsubkey_ctr *ctr, int seqnum)
112 if (ctr == NULL) {
113 return WERR_INVALID_PARAM;
116 ctr->seqnum = seqnum;
118 return WERR_OK;
121 int regsubkey_ctr_get_seqnum(struct regsubkey_ctr *ctr)
123 if (ctr == NULL) {
124 return -1;
127 return ctr->seqnum;
130 static WERROR regsubkey_ctr_hash_keyname(struct regsubkey_ctr *ctr,
131 const char *keyname,
132 uint32_t idx)
134 WERROR werr;
136 werr = ntstatus_to_werror(dbwrap_store_bystring_upper(ctr->subkeys_hash,
137 keyname,
138 make_tdb_data((uint8_t *)&idx,
139 sizeof(idx)),
140 TDB_REPLACE));
141 if (!W_ERROR_IS_OK(werr)) {
142 DEBUG(1, ("error hashing new key '%s' in container: %s\n",
143 keyname, win_errstr(werr)));
146 return werr;
149 static WERROR regsubkey_ctr_unhash_keyname(struct regsubkey_ctr *ctr,
150 const char *keyname)
152 WERROR werr;
154 werr = ntstatus_to_werror(dbwrap_delete_bystring_upper(ctr->subkeys_hash,
155 keyname));
156 if (!W_ERROR_IS_OK(werr)) {
157 DEBUG(1, ("error unhashing key '%s' in container: %s\n",
158 keyname, win_errstr(werr)));
161 return werr;
164 static WERROR regsubkey_ctr_index_for_keyname(struct regsubkey_ctr *ctr,
165 const char *keyname,
166 uint32_t *idx)
168 TDB_DATA data;
170 if ((ctr == NULL) || (keyname == NULL)) {
171 return WERR_INVALID_PARAM;
174 data = dbwrap_fetch_bystring_upper(ctr->subkeys_hash, ctr, keyname);
175 if (data.dptr == NULL) {
176 return WERR_NOT_FOUND;
179 if (data.dsize != sizeof(*idx)) {
180 talloc_free(data.dptr);
181 return WERR_INVALID_DATATYPE;
184 if (idx != NULL) {
185 *idx = *(uint32_t *)data.dptr;
188 talloc_free(data.dptr);
189 return WERR_OK;
192 /***********************************************************************
193 Add a new key to the array
194 **********************************************************************/
196 WERROR regsubkey_ctr_addkey( struct regsubkey_ctr *ctr, const char *keyname )
198 char **newkeys;
199 WERROR werr;
201 if ( !keyname ) {
202 return WERR_OK;
205 /* make sure the keyname is not already there */
207 if ( regsubkey_ctr_key_exists( ctr, keyname ) ) {
208 return WERR_OK;
211 if (!(newkeys = TALLOC_REALLOC_ARRAY(ctr, ctr->subkeys, char *,
212 ctr->num_subkeys+1))) {
213 return WERR_NOMEM;
216 ctr->subkeys = newkeys;
218 if (!(ctr->subkeys[ctr->num_subkeys] = talloc_strdup(ctr->subkeys,
219 keyname ))) {
221 * Don't shrink the new array again, this wastes a pointer
223 return WERR_NOMEM;
226 werr = regsubkey_ctr_hash_keyname(ctr, keyname, ctr->num_subkeys);
227 W_ERROR_NOT_OK_RETURN(werr);
229 ctr->num_subkeys++;
231 return WERR_OK;
234 /***********************************************************************
235 Delete a key from the array
236 **********************************************************************/
238 WERROR regsubkey_ctr_delkey( struct regsubkey_ctr *ctr, const char *keyname )
240 WERROR werr;
241 uint32_t idx, j;
243 if (keyname == NULL) {
244 return WERR_INVALID_PARAM;
247 /* make sure the keyname is actually already there */
249 werr = regsubkey_ctr_index_for_keyname(ctr, keyname, &idx);
250 W_ERROR_NOT_OK_RETURN(werr);
252 werr = regsubkey_ctr_unhash_keyname(ctr, keyname);
253 W_ERROR_NOT_OK_RETURN(werr);
255 /* update if we have any keys left */
256 ctr->num_subkeys--;
257 if (idx < ctr->num_subkeys) {
258 memmove(&ctr->subkeys[idx], &ctr->subkeys[idx+1],
259 sizeof(char *) * (ctr->num_subkeys - idx));
261 /* we have to re-hash rest of the array... :-( */
262 for (j = idx; j < ctr->num_subkeys; j++) {
263 werr = regsubkey_ctr_hash_keyname(ctr, ctr->subkeys[j], j);
264 W_ERROR_NOT_OK_RETURN(werr);
268 return WERR_OK;
271 /***********************************************************************
272 Check for the existance of a key
273 **********************************************************************/
275 bool regsubkey_ctr_key_exists( struct regsubkey_ctr *ctr, const char *keyname )
277 WERROR werr;
279 if (!ctr->subkeys) {
280 return False;
283 werr = regsubkey_ctr_index_for_keyname(ctr, keyname, NULL);
284 if (!W_ERROR_IS_OK(werr)) {
285 return false;
288 return true;
291 /***********************************************************************
292 How many keys does the container hold ?
293 **********************************************************************/
295 int regsubkey_ctr_numkeys( struct regsubkey_ctr *ctr )
297 return ctr->num_subkeys;
300 /***********************************************************************
301 Retreive a specific key string
302 **********************************************************************/
304 char* regsubkey_ctr_specific_key( struct regsubkey_ctr *ctr, uint32_t key_index )
306 if ( ! (key_index < ctr->num_subkeys) )
307 return NULL;
309 return ctr->subkeys[key_index];
313 * Utility functions for struct regval_ctr
317 * allocate a regval_ctr structure.
319 WERROR regval_ctr_init(TALLOC_CTX *mem_ctx, struct regval_ctr **ctr)
321 if (ctr == NULL) {
322 return WERR_INVALID_PARAM;
325 *ctr = talloc_zero(mem_ctx, struct regval_ctr);
326 if (*ctr == NULL) {
327 return WERR_NOMEM;
330 return WERR_OK;
333 /***********************************************************************
334 How many keys does the container hold ?
335 **********************************************************************/
337 int regval_ctr_numvals(struct regval_ctr *ctr)
339 return ctr->num_values;
342 /***********************************************************************
343 allocate memory for and duplicate a struct regval_blob.
344 This is malloc'd memory so the caller should free it when done
345 **********************************************************************/
347 struct regval_blob* dup_registry_value(struct regval_blob *val)
349 struct regval_blob *copy = NULL;
351 if ( !val )
352 return NULL;
354 if ( !(copy = SMB_MALLOC_P( struct regval_blob)) ) {
355 DEBUG(0,("dup_registry_value: malloc() failed!\n"));
356 return NULL;
359 /* copy all the non-pointer initial data */
361 memcpy( copy, val, sizeof(struct regval_blob) );
363 copy->size = 0;
364 copy->data_p = NULL;
366 if ( val->data_p && val->size )
368 if ( !(copy->data_p = (uint8_t *)memdup( val->data_p,
369 val->size )) ) {
370 DEBUG(0,("dup_registry_value: memdup() failed for [%d] "
371 "bytes!\n", val->size));
372 SAFE_FREE( copy );
373 return NULL;
375 copy->size = val->size;
378 return copy;
381 /**********************************************************************
382 free the memory allocated to a struct regval_blob
383 *********************************************************************/
385 void free_registry_value(struct regval_blob *val)
387 if ( !val )
388 return;
390 SAFE_FREE( val->data_p );
391 SAFE_FREE( val );
393 return;
396 /**********************************************************************
397 *********************************************************************/
399 uint8_t* regval_data_p(struct regval_blob *val)
401 return val->data_p;
404 /**********************************************************************
405 *********************************************************************/
407 uint32_t regval_size(struct regval_blob *val)
409 return val->size;
412 /**********************************************************************
413 *********************************************************************/
415 char* regval_name(struct regval_blob *val)
417 return val->valuename;
420 /**********************************************************************
421 *********************************************************************/
423 uint32_t regval_type(struct regval_blob *val)
425 return val->type;
428 /***********************************************************************
429 Retreive a pointer to a specific value. Caller shoud dup the structure
430 since this memory will go away when the ctr is free()'d
431 **********************************************************************/
433 struct regval_blob *regval_ctr_specific_value(struct regval_ctr *ctr,
434 uint32_t idx)
436 if ( !(idx < ctr->num_values) )
437 return NULL;
439 return ctr->values[idx];
442 /***********************************************************************
443 Check for the existance of a value
444 **********************************************************************/
446 bool regval_ctr_key_exists(struct regval_ctr *ctr, const char *value)
448 int i;
450 for ( i=0; i<ctr->num_values; i++ ) {
451 if ( strequal( ctr->values[i]->valuename, value) )
452 return True;
455 return False;
458 /***********************************************************************
459 * compose a struct regval_blob from input data
460 **********************************************************************/
462 struct regval_blob *regval_compose(TALLOC_CTX *ctx, const char *name,
463 uint16_t type,
464 const uint8_t *data_p, size_t size)
466 struct regval_blob *regval = TALLOC_P(ctx, struct regval_blob);
468 if (regval == NULL) {
469 return NULL;
472 fstrcpy(regval->valuename, name);
473 regval->type = type;
474 if (size) {
475 regval->data_p = (uint8_t *)TALLOC_MEMDUP(regval, data_p, size);
476 if (!regval->data_p) {
477 TALLOC_FREE(regval);
478 return NULL;
480 } else {
481 regval->data_p = NULL;
483 regval->size = size;
485 return regval;
488 /***********************************************************************
489 Add a new registry value to the array
490 **********************************************************************/
492 int regval_ctr_addvalue(struct regval_ctr *ctr, const char *name, uint16_t type,
493 const uint8_t *data_p, size_t size)
495 if ( !name )
496 return ctr->num_values;
498 /* Delete the current value (if it exists) and add the new one */
500 regval_ctr_delvalue( ctr, name );
502 /* allocate a slot in the array of pointers */
504 if ( ctr->num_values == 0 ) {
505 ctr->values = TALLOC_P( ctr, struct regval_blob *);
506 } else {
507 ctr->values = TALLOC_REALLOC_ARRAY(ctr, ctr->values,
508 struct regval_blob *,
509 ctr->num_values+1);
512 if (!ctr->values) {
513 ctr->num_values = 0;
514 return 0;
517 /* allocate a new value and store the pointer in the arrya */
519 ctr->values[ctr->num_values] = regval_compose(ctr, name, type, data_p,
520 size);
521 if (ctr->values[ctr->num_values] == NULL) {
522 ctr->num_values = 0;
523 return 0;
525 ctr->num_values++;
527 return ctr->num_values;
530 /***********************************************************************
531 Add a new registry SZ value to the array
532 **********************************************************************/
534 int regval_ctr_addvalue_sz(struct regval_ctr *ctr, const char *name, const char *data)
536 DATA_BLOB blob;
538 if (!push_reg_sz(ctr, &blob, data)) {
539 return -1;
542 return regval_ctr_addvalue(ctr, name, REG_SZ,
543 (const uint8_t *)blob.data,
544 blob.length);
547 /***********************************************************************
548 Add a new registry MULTI_SZ value to the array
549 **********************************************************************/
551 int regval_ctr_addvalue_multi_sz(struct regval_ctr *ctr, const char *name, const char **data)
553 DATA_BLOB blob;
555 if (!push_reg_multi_sz(ctr, &blob, data)) {
556 return -1;
559 return regval_ctr_addvalue(ctr, name, REG_MULTI_SZ,
560 (const uint8_t *)blob.data,
561 blob.length);
564 /***********************************************************************
565 Add a new registry value to the array
566 **********************************************************************/
568 int regval_ctr_copyvalue(struct regval_ctr *ctr, struct regval_blob *val)
570 if ( val ) {
571 regval_ctr_addvalue(ctr, val->valuename, val->type,
572 (uint8_t *)val->data_p, val->size);
575 return ctr->num_values;
578 /***********************************************************************
579 Delete a single value from the registry container.
580 No need to free memory since it is talloc'd.
581 **********************************************************************/
583 int regval_ctr_delvalue(struct regval_ctr *ctr, const char *name)
585 int i;
587 for ( i=0; i<ctr->num_values; i++ ) {
588 if ( strequal( ctr->values[i]->valuename, name ) )
589 break;
592 /* just return if we don't find it */
594 if ( i == ctr->num_values )
595 return ctr->num_values;
597 /* If 'i' was not the last element, just shift everything down one */
598 ctr->num_values--;
599 if ( i < ctr->num_values )
600 memmove(&ctr->values[i], &ctr->values[i+1],
601 sizeof(struct regval_blob*)*(ctr->num_values-i));
603 return ctr->num_values;
606 /***********************************************************************
607 Retrieve single value from the registry container.
608 No need to free memory since it is talloc'd.
609 **********************************************************************/
611 struct regval_blob* regval_ctr_getvalue(struct regval_ctr *ctr,
612 const char *name)
614 int i;
616 /* search for the value */
618 for ( i=0; i<ctr->num_values; i++ ) {
619 if ( strequal( ctr->values[i]->valuename, name ) )
620 return ctr->values[i];
623 return NULL;
626 int regval_ctr_get_seqnum(struct regval_ctr *ctr)
628 if (ctr == NULL) {
629 return -1;
632 return ctr->seqnum;
635 WERROR regval_ctr_set_seqnum(struct regval_ctr *ctr, int seqnum)
637 if (ctr == NULL) {
638 return WERR_INVALID_PARAM;
641 ctr->seqnum = seqnum;
643 return WERR_OK;
646 /***********************************************************************
647 return the data_p as a uint32_t
648 **********************************************************************/
650 uint32_t regval_dword(struct regval_blob *val)
652 uint32_t data;
654 data = IVAL( regval_data_p(val), 0 );
656 return data;
659 /***********************************************************************
660 return the data_p as a character string
661 **********************************************************************/
663 const char *regval_sz(struct regval_blob *val)
665 const char *data = NULL;
666 DATA_BLOB blob = data_blob_const(regval_data_p(val), regval_size(val));
668 pull_reg_sz(talloc_tos(), &blob, &data);
670 return data;