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