source: trunk/server/source3/registry/reg_objects.c

Last change on this file was 751, checked in by Silvan Scherrer, 13 years ago

Samba Server: updated trunk to 3.6.9

File size: 17.1 KB
Line 
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
6 *
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.
11 *
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.
16 *
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/>.
19 */
20
21/* Implementation of registry frontend view functions. */
22
23#include "includes.h"
24#include "registry.h"
25#include "reg_objects.h"
26#include "util_tdb.h"
27#include "dbwrap.h"
28#include "../libcli/registry/util_reg.h"
29
30#undef DBGC_CLASS
31#define DBGC_CLASS DBGC_REGISTRY
32
33/* low level structure to contain registry values */
34
35struct regval_blob {
36 fstring valuename;
37 uint32_t type;
38 /* this should be encapsulated in an RPC_DATA_BLOB */
39 uint32_t size; /* in bytes */
40 uint8_t *data_p;
41};
42
43/* container for registry values */
44
45struct regval_ctr {
46 uint32_t num_values;
47 struct regval_blob **values;
48 int seqnum;
49};
50
51struct regsubkey_ctr {
52 uint32_t num_subkeys;
53 char **subkeys;
54 struct db_context *subkeys_hash;
55 int seqnum;
56};
57
58/**********************************************************************
59
60 Note that the struct regsubkey_ctr and struct regval_ctr objects *must* be
61 talloc()'d since the methods use the object pointer as the talloc
62 context for internal private data.
63
64 There is no longer a regval_ctr_intit() and regval_ctr_destroy()
65 pair of functions. Simply TALLOC_ZERO_P() and TALLOC_FREE() the
66 object.
67
68 **********************************************************************/
69
70WERROR regsubkey_ctr_init(TALLOC_CTX *mem_ctx, struct regsubkey_ctr **ctr)
71{
72 if (ctr == NULL) {
73 return WERR_INVALID_PARAM;
74 }
75
76 *ctr = talloc_zero(mem_ctx, struct regsubkey_ctr);
77 if (*ctr == NULL) {
78 return WERR_NOMEM;
79 }
80
81 (*ctr)->subkeys_hash = db_open_rbt(*ctr);
82 if ((*ctr)->subkeys_hash == NULL) {
83 talloc_free(*ctr);
84 return WERR_NOMEM;
85 }
86
87 return WERR_OK;
88}
89
90/**
91 * re-initialize the list of subkeys (to the emtpy list)
92 * in an already allocated regsubkey_ctr
93 */
94
95WERROR regsubkey_ctr_reinit(struct regsubkey_ctr *ctr)
96{
97 if (ctr == NULL) {
98 return WERR_INVALID_PARAM;
99 }
100
101 talloc_free(ctr->subkeys_hash);
102 ctr->subkeys_hash = db_open_rbt(ctr);
103 W_ERROR_HAVE_NO_MEMORY(ctr->subkeys_hash);
104
105 TALLOC_FREE(ctr->subkeys);
106
107 ctr->num_subkeys = 0;
108 ctr->seqnum = 0;
109
110 return WERR_OK;
111}
112
113WERROR regsubkey_ctr_set_seqnum(struct regsubkey_ctr *ctr, int seqnum)
114{
115 if (ctr == NULL) {
116 return WERR_INVALID_PARAM;
117 }
118
119 ctr->seqnum = seqnum;
120
121 return WERR_OK;
122}
123
124int regsubkey_ctr_get_seqnum(struct regsubkey_ctr *ctr)
125{
126 if (ctr == NULL) {
127 return -1;
128 }
129
130 return ctr->seqnum;
131}
132
133static WERROR regsubkey_ctr_hash_keyname(struct regsubkey_ctr *ctr,
134 const char *keyname,
135 uint32_t idx)
136{
137 WERROR werr;
138
139 werr = ntstatus_to_werror(dbwrap_store_bystring_upper(ctr->subkeys_hash,
140 keyname,
141 make_tdb_data((uint8_t *)&idx,
142 sizeof(idx)),
143 TDB_REPLACE));
144 if (!W_ERROR_IS_OK(werr)) {
145 DEBUG(1, ("error hashing new key '%s' in container: %s\n",
146 keyname, win_errstr(werr)));
147 }
148
149 return werr;
150}
151
152static WERROR regsubkey_ctr_unhash_keyname(struct regsubkey_ctr *ctr,
153 const char *keyname)
154{
155 WERROR werr;
156
157 werr = ntstatus_to_werror(dbwrap_delete_bystring_upper(ctr->subkeys_hash,
158 keyname));
159 if (!W_ERROR_IS_OK(werr)) {
160 DEBUG(1, ("error unhashing key '%s' in container: %s\n",
161 keyname, win_errstr(werr)));
162 }
163
164 return werr;
165}
166
167static WERROR regsubkey_ctr_index_for_keyname(struct regsubkey_ctr *ctr,
168 const char *keyname,
169 uint32_t *idx)
170{
171 TDB_DATA data;
172
173 if ((ctr == NULL) || (keyname == NULL)) {
174 return WERR_INVALID_PARAM;
175 }
176
177 data = dbwrap_fetch_bystring_upper(ctr->subkeys_hash, ctr, keyname);
178 if (data.dptr == NULL) {
179 return WERR_NOT_FOUND;
180 }
181
182 if (data.dsize != sizeof(*idx)) {
183 talloc_free(data.dptr);
184 return WERR_INVALID_DATATYPE;
185 }
186
187 if (idx != NULL) {
188 *idx = *(uint32_t *)data.dptr;
189 }
190
191 talloc_free(data.dptr);
192 return WERR_OK;
193}
194
195/***********************************************************************
196 Add a new key to the array
197 **********************************************************************/
198
199WERROR regsubkey_ctr_addkey( struct regsubkey_ctr *ctr, const char *keyname )
200{
201 char **newkeys;
202 WERROR werr;
203
204 if ( !keyname ) {
205 return WERR_OK;
206 }
207
208 /* make sure the keyname is not already there */
209
210 if ( regsubkey_ctr_key_exists( ctr, keyname ) ) {
211 return WERR_OK;
212 }
213
214 if (!(newkeys = TALLOC_REALLOC_ARRAY(ctr, ctr->subkeys, char *,
215 ctr->num_subkeys+1))) {
216 return WERR_NOMEM;
217 }
218
219 ctr->subkeys = newkeys;
220
221 if (!(ctr->subkeys[ctr->num_subkeys] = talloc_strdup(ctr->subkeys,
222 keyname ))) {
223 /*
224 * Don't shrink the new array again, this wastes a pointer
225 */
226 return WERR_NOMEM;
227 }
228
229 werr = regsubkey_ctr_hash_keyname(ctr, keyname, ctr->num_subkeys);
230 W_ERROR_NOT_OK_RETURN(werr);
231
232 ctr->num_subkeys++;
233
234 return WERR_OK;
235}
236
237 /***********************************************************************
238 Delete a key from the array
239 **********************************************************************/
240
241WERROR regsubkey_ctr_delkey( struct regsubkey_ctr *ctr, const char *keyname )
242{
243 WERROR werr;
244 uint32_t idx, j;
245
246 if (keyname == NULL) {
247 return WERR_INVALID_PARAM;
248 }
249
250 /* make sure the keyname is actually already there */
251
252 werr = regsubkey_ctr_index_for_keyname(ctr, keyname, &idx);
253 W_ERROR_NOT_OK_RETURN(werr);
254
255 werr = regsubkey_ctr_unhash_keyname(ctr, keyname);
256 W_ERROR_NOT_OK_RETURN(werr);
257
258 /* update if we have any keys left */
259 ctr->num_subkeys--;
260 if (idx < ctr->num_subkeys) {
261 memmove(&ctr->subkeys[idx], &ctr->subkeys[idx+1],
262 sizeof(char *) * (ctr->num_subkeys - idx));
263
264 /* we have to re-hash rest of the array... :-( */
265 for (j = idx; j < ctr->num_subkeys; j++) {
266 werr = regsubkey_ctr_hash_keyname(ctr, ctr->subkeys[j], j);
267 W_ERROR_NOT_OK_RETURN(werr);
268 }
269 }
270
271 return WERR_OK;
272}
273
274/***********************************************************************
275 Check for the existance of a key
276 **********************************************************************/
277
278bool regsubkey_ctr_key_exists( struct regsubkey_ctr *ctr, const char *keyname )
279{
280 WERROR werr;
281
282 if (!ctr->subkeys) {
283 return False;
284 }
285
286 werr = regsubkey_ctr_index_for_keyname(ctr, keyname, NULL);
287 if (!W_ERROR_IS_OK(werr)) {
288 return false;
289 }
290
291 return true;
292}
293
294/***********************************************************************
295 How many keys does the container hold ?
296 **********************************************************************/
297
298int regsubkey_ctr_numkeys( struct regsubkey_ctr *ctr )
299{
300 return ctr->num_subkeys;
301}
302
303/***********************************************************************
304 Retreive a specific key string
305 **********************************************************************/
306
307char* regsubkey_ctr_specific_key( struct regsubkey_ctr *ctr, uint32_t key_index )
308{
309 if ( ! (key_index < ctr->num_subkeys) )
310 return NULL;
311
312 return ctr->subkeys[key_index];
313}
314
315/*
316 * Utility functions for struct regval_ctr
317 */
318
319/**
320 * allocate a regval_ctr structure.
321 */
322WERROR regval_ctr_init(TALLOC_CTX *mem_ctx, struct regval_ctr **ctr)
323{
324 if (ctr == NULL) {
325 return WERR_INVALID_PARAM;
326 }
327
328 *ctr = talloc_zero(mem_ctx, struct regval_ctr);
329 if (*ctr == NULL) {
330 return WERR_NOMEM;
331 }
332
333 return WERR_OK;
334}
335
336/***********************************************************************
337 How many keys does the container hold ?
338 **********************************************************************/
339
340int regval_ctr_numvals(struct regval_ctr *ctr)
341{
342 return ctr->num_values;
343}
344
345/***********************************************************************
346 allocate memory for and duplicate a struct regval_blob.
347 This is malloc'd memory so the caller should free it when done
348 **********************************************************************/
349
350struct regval_blob* dup_registry_value(struct regval_blob *val)
351{
352 struct regval_blob *copy = NULL;
353
354 if ( !val )
355 return NULL;
356
357 if ( !(copy = SMB_MALLOC_P( struct regval_blob)) ) {
358 DEBUG(0,("dup_registry_value: malloc() failed!\n"));
359 return NULL;
360 }
361
362 /* copy all the non-pointer initial data */
363
364 memcpy( copy, val, sizeof(struct regval_blob) );
365
366 copy->size = 0;
367 copy->data_p = NULL;
368
369 if ( val->data_p && val->size )
370 {
371 if ( !(copy->data_p = (uint8_t *)memdup( val->data_p,
372 val->size )) ) {
373 DEBUG(0,("dup_registry_value: memdup() failed for [%d] "
374 "bytes!\n", val->size));
375 SAFE_FREE( copy );
376 return NULL;
377 }
378 copy->size = val->size;
379 }
380
381 return copy;
382}
383
384/**********************************************************************
385 free the memory allocated to a struct regval_blob
386 *********************************************************************/
387
388void free_registry_value(struct regval_blob *val)
389{
390 if ( !val )
391 return;
392
393 SAFE_FREE( val->data_p );
394 SAFE_FREE( val );
395
396 return;
397}
398
399/**********************************************************************
400 *********************************************************************/
401
402uint8_t* regval_data_p(struct regval_blob *val)
403{
404 return val->data_p;
405}
406
407/**********************************************************************
408 *********************************************************************/
409
410uint32_t regval_size(struct regval_blob *val)
411{
412 return val->size;
413}
414
415/**********************************************************************
416 *********************************************************************/
417
418char* regval_name(struct regval_blob *val)
419{
420 return val->valuename;
421}
422
423/**********************************************************************
424 *********************************************************************/
425
426uint32_t regval_type(struct regval_blob *val)
427{
428 return val->type;
429}
430
431/***********************************************************************
432 Retreive a pointer to a specific value. Caller shoud dup the structure
433 since this memory will go away when the ctr is free()'d
434 **********************************************************************/
435
436struct regval_blob *regval_ctr_specific_value(struct regval_ctr *ctr,
437 uint32_t idx)
438{
439 if ( !(idx < ctr->num_values) )
440 return NULL;
441
442 return ctr->values[idx];
443}
444
445/***********************************************************************
446 Check for the existance of a value
447 **********************************************************************/
448
449bool regval_ctr_value_exists(struct regval_ctr *ctr, const char *value)
450{
451 int i;
452
453 for ( i=0; i<ctr->num_values; i++ ) {
454 if ( strequal( ctr->values[i]->valuename, value) )
455 return True;
456 }
457
458 return False;
459}
460
461/**
462 * Get a value by its name
463 */
464struct regval_blob *regval_ctr_value_byname(struct regval_ctr *ctr,
465 const char *value)
466{
467 int i;
468
469 for (i=0; i<ctr->num_values; i++) {
470 if (strequal(ctr->values[i]->valuename,value)) {
471 return ctr->values[i];
472 }
473 }
474
475 return NULL;
476}
477
478
479/***********************************************************************
480 * compose a struct regval_blob from input data
481 **********************************************************************/
482
483struct regval_blob *regval_compose(TALLOC_CTX *ctx, const char *name,
484 uint32_t type,
485 const uint8_t *data_p, size_t size)
486{
487 struct regval_blob *regval = TALLOC_P(ctx, struct regval_blob);
488
489 if (regval == NULL) {
490 return NULL;
491 }
492
493 fstrcpy(regval->valuename, name);
494 regval->type = type;
495 if (size) {
496 regval->data_p = (uint8_t *)TALLOC_MEMDUP(regval, data_p, size);
497 if (!regval->data_p) {
498 TALLOC_FREE(regval);
499 return NULL;
500 }
501 } else {
502 regval->data_p = NULL;
503 }
504 regval->size = size;
505
506 return regval;
507}
508
509/***********************************************************************
510 Add a new registry value to the array
511 **********************************************************************/
512
513int regval_ctr_addvalue(struct regval_ctr *ctr, const char *name, uint32_t type,
514 const uint8_t *data_p, size_t size)
515{
516 if ( !name )
517 return ctr->num_values;
518
519 /* Delete the current value (if it exists) and add the new one */
520
521 regval_ctr_delvalue( ctr, name );
522
523 /* allocate a slot in the array of pointers */
524
525 if ( ctr->num_values == 0 ) {
526 ctr->values = TALLOC_P( ctr, struct regval_blob *);
527 } else {
528 ctr->values = TALLOC_REALLOC_ARRAY(ctr, ctr->values,
529 struct regval_blob *,
530 ctr->num_values+1);
531 }
532
533 if (!ctr->values) {
534 ctr->num_values = 0;
535 return 0;
536 }
537
538 /* allocate a new value and store the pointer in the arrya */
539
540 ctr->values[ctr->num_values] = regval_compose(ctr, name, type, data_p,
541 size);
542 if (ctr->values[ctr->num_values] == NULL) {
543 ctr->num_values = 0;
544 return 0;
545 }
546 ctr->num_values++;
547
548 return ctr->num_values;
549}
550
551/***********************************************************************
552 Add a new registry SZ value to the array
553 **********************************************************************/
554
555int regval_ctr_addvalue_sz(struct regval_ctr *ctr, const char *name, const char *data)
556{
557 DATA_BLOB blob;
558
559 if (!push_reg_sz(ctr, &blob, data)) {
560 return -1;
561 }
562
563 return regval_ctr_addvalue(ctr, name, REG_SZ,
564 (const uint8_t *)blob.data,
565 blob.length);
566}
567
568/***********************************************************************
569 Add a new registry MULTI_SZ value to the array
570 **********************************************************************/
571
572int regval_ctr_addvalue_multi_sz(struct regval_ctr *ctr, const char *name, const char **data)
573{
574 DATA_BLOB blob;
575
576 if (!push_reg_multi_sz(ctr, &blob, data)) {
577 return -1;
578 }
579
580 return regval_ctr_addvalue(ctr, name, REG_MULTI_SZ,
581 (const uint8_t *)blob.data,
582 blob.length);
583}
584
585/***********************************************************************
586 Add a new registry value to the array
587 **********************************************************************/
588
589int regval_ctr_copyvalue(struct regval_ctr *ctr, struct regval_blob *val)
590{
591 if ( val ) {
592 regval_ctr_addvalue(ctr, val->valuename, val->type,
593 (uint8_t *)val->data_p, val->size);
594 }
595
596 return ctr->num_values;
597}
598
599/***********************************************************************
600 Delete a single value from the registry container.
601 No need to free memory since it is talloc'd.
602 **********************************************************************/
603
604int regval_ctr_delvalue(struct regval_ctr *ctr, const char *name)
605{
606 int i;
607
608 for ( i=0; i<ctr->num_values; i++ ) {
609 if ( strequal( ctr->values[i]->valuename, name ) )
610 break;
611 }
612
613 /* just return if we don't find it */
614
615 if ( i == ctr->num_values )
616 return ctr->num_values;
617
618 /* If 'i' was not the last element, just shift everything down one */
619 ctr->num_values--;
620 if ( i < ctr->num_values )
621 memmove(&ctr->values[i], &ctr->values[i+1],
622 sizeof(struct regval_blob*)*(ctr->num_values-i));
623
624 return ctr->num_values;
625}
626
627/***********************************************************************
628 Retrieve single value from the registry container.
629 No need to free memory since it is talloc'd.
630 **********************************************************************/
631
632struct regval_blob* regval_ctr_getvalue(struct regval_ctr *ctr,
633 const char *name)
634{
635 int i;
636
637 /* search for the value */
638
639 for ( i=0; i<ctr->num_values; i++ ) {
640 if ( strequal( ctr->values[i]->valuename, name ) )
641 return ctr->values[i];
642 }
643
644 return NULL;
645}
646
647int regval_ctr_get_seqnum(struct regval_ctr *ctr)
648{
649 if (ctr == NULL) {
650 return -1;
651 }
652
653 return ctr->seqnum;
654}
655
656WERROR regval_ctr_set_seqnum(struct regval_ctr *ctr, int seqnum)
657{
658 if (ctr == NULL) {
659 return WERR_INVALID_PARAM;
660 }
661
662 ctr->seqnum = seqnum;
663
664 return WERR_OK;
665}
666
667/***********************************************************************
668 return the data_p as a uint32_t
669 **********************************************************************/
670
671uint32_t regval_dword(struct regval_blob *val)
672{
673 uint32_t data;
674
675 data = IVAL( regval_data_p(val), 0 );
676
677 return data;
678}
679
680/***********************************************************************
681 return the data_p as a character string
682 **********************************************************************/
683
684const char *regval_sz(struct regval_blob *val)
685{
686 const char *data = NULL;
687 DATA_BLOB blob = data_blob_const(regval_data_p(val), regval_size(val));
688
689 pull_reg_sz(talloc_tos(), &blob, &data);
690
691 return data;
692}
Note: See TracBrowser for help on using the repository browser.