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