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