Merge branch 'v3-2-test' of ssh://jra@git.samba.org/data/git/samba into v3-2-test
[jra/samba/.git] / source3 / registry / reg_backend_db.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 internal registry database functions. */
21
22 #include "includes.h"
23
24 #undef DBGC_CLASS
25 #define DBGC_CLASS DBGC_REGISTRY
26
27 static struct tdb_wrap *tdb_reg = NULL;
28 static int tdb_refcount;
29
30 /* List the deepest path into the registry.  All part components will be created.*/
31
32 /* If you want to have a part of the path controlled by the tdb and part by
33    a virtual registry db (e.g. printing), then you have to list the deepest path.
34    For example,"HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion/Print" 
35    allows the reg_db backend to handle everything up to 
36    "HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion" and then we'll hook 
37    the reg_printing backend onto the last component of the path (see 
38    KEY_PRINTING_2K in include/rpc_reg.h)   --jerry */
39
40 static const char *builtin_registry_paths[] = {
41         KEY_PRINTING_2K,
42         KEY_PRINTING_PORTS,
43         KEY_PRINTING,
44         KEY_SHARES,
45         KEY_EVENTLOG,
46         KEY_SMBCONF,
47         KEY_PERFLIB,
48         KEY_PERFLIB_009,
49         "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Print\\Monitors",
50         KEY_PROD_OPTIONS,
51         "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\DefaultUserConfiguration",
52         KEY_TCPIP_PARAMS,
53         KEY_NETLOGON_PARAMS,
54         KEY_HKU,
55         KEY_HKCR,
56         KEY_HKPD,
57         KEY_HKPT,
58          NULL };
59
60 struct builtin_regkey_value {
61         const char *path;
62         const char *valuename;
63         uint32 type;
64         union {
65                 const char *string;
66                 uint32 dw_value;
67         } data;
68 };
69
70 static struct builtin_regkey_value builtin_registry_values[] = {
71         { KEY_PRINTING_PORTS,
72                 SAMBA_PRINTER_PORT_NAME, REG_SZ, { "" } },
73         { KEY_PRINTING_2K,
74                 "DefaultSpoolDirectory", REG_SZ, { "C:\\Windows\\System32\\Spool\\Printers" } },
75         { KEY_EVENTLOG,
76                 "DisplayName", REG_SZ, { "Event Log" } }, 
77         { KEY_EVENTLOG,
78                 "ErrorControl", REG_DWORD, { (char*)0x00000001 } },
79         { NULL, NULL, 0, { NULL } }
80 };
81
82 /***********************************************************************
83  Open the registry data in the tdb
84  ***********************************************************************/
85
86 static bool init_registry_data( void )
87 {
88         char *path = NULL;
89         char *base = NULL;
90         char *remaining = NULL;
91         TALLOC_CTX *frame = NULL;
92         char *keyname;
93         char *subkeyname;
94         REGSUBKEY_CTR *subkeys;
95         REGVAL_CTR *values;
96         int i;
97         const char *p, *p2;
98         UNISTR2 data;
99
100         /*
101          * There are potentially quite a few store operations which are all
102          * indiviually wrapped in tdb transactions. Wrapping them in a single
103          * transaction gives just a single transaction_commit() to actually do
104          * its fsync()s. See tdb/common/transaction.c for info about nested
105          * transaction behaviour.
106          */
107
108         if ( tdb_transaction_start( tdb_reg->tdb ) == -1 ) {
109                 DEBUG(0, ("init_registry_data: tdb_transaction_start "
110                           "failed\n"));
111                 return false;
112         }
113
114         /* loop over all of the predefined paths and add each component */
115
116         for ( i=0; builtin_registry_paths[i] != NULL; i++ ) {
117
118                 frame = talloc_stackframe();
119
120                 DEBUG(6,("init_registry_data: Adding [%s]\n", builtin_registry_paths[i]));
121
122                 path = talloc_strdup(talloc_tos(), builtin_registry_paths[i]);
123                 base = talloc_strdup(talloc_tos(), "");
124                 if (!path || !base) {
125                         goto fail;
126                 }
127                 p = path;
128
129                 while (next_token_talloc(talloc_tos(), &p, &keyname, "\\")) {
130
131                         /* build up the registry path from the components */
132
133                         if (*base) {
134                                 base = talloc_asprintf(talloc_tos(), "%s\\", base);
135                                 if (!base) {
136                                         goto fail;
137                                 }
138                         }
139                         base = talloc_asprintf_append(base, "%s", keyname);
140                         if (!base) {
141                                 goto fail;
142                         }
143
144                         /* get the immediate subkeyname (if we have one ) */
145
146                         subkeyname = talloc_strdup(talloc_tos(), "");
147                         if (!subkeyname) {
148                                 goto fail;
149                         }
150                         if (*p) {
151                                 remaining = talloc_strdup(talloc_tos(), p);
152                                 if (!remaining) {
153                                         goto fail;
154                                 }
155                                 p2 = remaining;
156
157                                 if (!next_token_talloc(talloc_tos(), &p2,
158                                                         &subkeyname, "\\")) {
159                                         subkeyname = talloc_strdup(talloc_tos(),p2);
160                                         if (!subkeyname) {
161                                                 goto fail;
162                                         }
163                                 }
164                         }
165
166                         DEBUG(10,("init_registry_data: Storing key [%s] with subkey [%s]\n",
167                                 base, *subkeyname ? subkeyname : "NULL"));
168
169                         /* we don't really care if the lookup succeeds or not since
170                            we are about to update the record.  We just want any
171                            subkeys already present */
172
173                         if ( !(subkeys = TALLOC_ZERO_P(talloc_tos(), REGSUBKEY_CTR )) ) {
174                                 DEBUG(0,("talloc() failure!\n"));
175                                 goto fail;
176                         }
177
178                         regdb_fetch_keys(base, subkeys);
179                         if (*subkeyname) {
180                                 regsubkey_ctr_addkey( subkeys, subkeyname);
181                         }
182                         if (!regdb_store_keys( base, subkeys)) {
183                                 goto fail;
184                         }
185                 }
186
187                 TALLOC_FREE(frame);
188         }
189
190         /* loop over all of the predefined values and add each component */
191
192         for (i=0; builtin_registry_values[i].path != NULL; i++) {
193
194                 if (!(values = TALLOC_ZERO_P(talloc_tos(), REGVAL_CTR))) {
195                         goto fail;
196                 }
197
198                 regdb_fetch_values( builtin_registry_values[i].path, values);
199
200                 /* preserve existing values across restarts.  Only add new ones */
201
202                 if (!regval_ctr_key_exists(values, builtin_registry_values[i].valuename)) {
203                         switch(builtin_registry_values[i].type) {
204                         case REG_DWORD:
205                                 regval_ctr_addvalue( values,
206                                                      builtin_registry_values[i].valuename,
207                                                      REG_DWORD,
208                                                      (char*)&builtin_registry_values[i].data.dw_value,
209                                                      sizeof(uint32) );
210                                 break;
211
212                         case REG_SZ:
213                                 init_unistr2( &data, builtin_registry_values[i].data.string, UNI_STR_TERMINATE);
214                                 regval_ctr_addvalue( values,
215                                                      builtin_registry_values[i].valuename,
216                                                      REG_SZ,
217                                                      (char*)data.buffer,
218                                                      data.uni_str_len*sizeof(uint16) );
219                                 break;
220
221                         default:
222                                 DEBUG(0,("init_registry_data: invalid value type in builtin_registry_values [%d]\n",
223                                         builtin_registry_values[i].type));
224                         }
225                         regdb_store_values( builtin_registry_values[i].path, values );
226                 }
227                 TALLOC_FREE( values );
228         }
229
230         TALLOC_FREE(frame);
231
232         if (tdb_transaction_commit( tdb_reg->tdb ) == -1) {
233                 DEBUG(0, ("init_registry_data: Could not commit "
234                           "transaction\n"));
235                 return false;
236         }
237
238         return true;
239
240  fail:
241
242         TALLOC_FREE(frame);
243
244         if (tdb_transaction_cancel( tdb_reg->tdb ) == -1) {
245                 smb_panic("init_registry_data: tdb_transaction_cancel "
246                           "failed\n");
247         }
248
249         return false;
250 }
251
252 /***********************************************************************
253  Open the registry database
254  ***********************************************************************/
255  
256 bool regdb_init( void )
257 {
258         const char *vstring = "INFO/version";
259         uint32 vers_id;
260
261         if ( tdb_reg ) {
262                 DEBUG(10,("regdb_init: incrementing refcount (%d)\n", tdb_refcount));
263                 tdb_refcount++;
264                 return true;
265         }
266
267         if ( !(tdb_reg = tdb_wrap_open(NULL, state_path("registry.tdb"), 0, REG_TDB_FLAGS, O_RDWR, 0600)) )
268         {
269                 tdb_reg = tdb_wrap_open(NULL, state_path("registry.tdb"), 0, REG_TDB_FLAGS, O_RDWR|O_CREAT, 0600);
270                 if ( !tdb_reg ) {
271                         DEBUG(0,("regdb_init: Failed to open registry %s (%s)\n",
272                                 state_path("registry.tdb"), strerror(errno) ));
273                         return false;
274                 }
275                 
276                 DEBUG(10,("regdb_init: Successfully created registry tdb\n"));
277         }
278
279         tdb_refcount = 1;
280
281         vers_id = tdb_fetch_int32(tdb_reg->tdb, vstring);
282
283         if ( vers_id != REGVER_V1 ) {
284                 /* any upgrade code here if needed */
285                 DEBUG(10, ("regdb_init: got INFO/version = %d != %d\n",
286                            vers_id, REGVER_V1));
287         }
288
289         /* always setup the necessary keys and values */
290
291         if ( !init_registry_data() ) {
292                 DEBUG(0,("regdb_init: Failed to initialize data in registry!\n"));
293                 return false;
294         }
295
296         return true;
297 }
298
299 /***********************************************************************
300  Open the registry.  Must already have been initialized by regdb_init()
301  ***********************************************************************/
302
303 WERROR regdb_open( void )
304 {
305         WERROR result = WERR_OK;
306
307         if ( tdb_reg ) {
308                 DEBUG(10,("regdb_open: incrementing refcount (%d)\n", tdb_refcount));
309                 tdb_refcount++;
310                 return WERR_OK;
311         }
312         
313         become_root();
314
315         tdb_reg = tdb_wrap_open(NULL, state_path("registry.tdb"), 0, REG_TDB_FLAGS, O_RDWR, 0600);
316         if ( !tdb_reg ) {
317                 result = ntstatus_to_werror( map_nt_error_from_unix( errno ) );
318                 DEBUG(0,("regdb_open: Failed to open %s! (%s)\n", 
319                         state_path("registry.tdb"), strerror(errno) ));
320         }
321
322         unbecome_root();
323
324         tdb_refcount = 1;
325         DEBUG(10,("regdb_open: refcount reset (%d)\n", tdb_refcount));
326
327         return result;
328 }
329
330 /***********************************************************************
331  ***********************************************************************/
332
333 int regdb_close( void )
334 {
335         if (tdb_refcount == 0) {
336                 return 0;
337         }
338
339         tdb_refcount--;
340
341         DEBUG(10,("regdb_close: decrementing refcount (%d)\n", tdb_refcount));
342
343         if ( tdb_refcount > 0 )
344                 return 0;
345
346         SMB_ASSERT( tdb_refcount >= 0 );
347
348         TALLOC_FREE(tdb_reg);
349         return 0;
350 }
351
352 /***********************************************************************
353  return the tdb sequence number of the registry tdb.
354  this is an indicator for the content of the registry
355  having changed. it will change upon regdb_init, too, though.
356  ***********************************************************************/
357 int regdb_get_seqnum(void)
358 {
359         return tdb_get_seqnum(tdb_reg->tdb);
360 }
361
362 /***********************************************************************
363  Add subkey strings to the registry tdb under a defined key
364  fmt is the same format as tdb_pack except this function only supports
365  fstrings
366  ***********************************************************************/
367
368 static bool regdb_store_keys_internal(const char *key, REGSUBKEY_CTR *ctr)
369 {
370         TDB_DATA dbuf;
371         uint8 *buffer = NULL;
372         int i = 0;
373         uint32 len, buflen;
374         bool ret = true;
375         uint32 num_subkeys = regsubkey_ctr_numkeys(ctr);
376         char *keyname = NULL;
377         TALLOC_CTX *ctx = talloc_tos();
378
379         if (!key) {
380                 return false;
381         }
382
383         keyname = talloc_strdup(ctx, key);
384         if (!keyname) {
385                 return false;
386         }
387         keyname = normalize_reg_path(ctx, keyname);
388
389         /* allocate some initial memory */
390
391         if (!(buffer = (uint8 *)SMB_MALLOC(1024))) {
392                 return false;
393         }
394         buflen = 1024;
395         len = 0;
396
397         /* store the number of subkeys */
398
399         len += tdb_pack(buffer+len, buflen-len, "d", num_subkeys );
400
401         /* pack all the strings */
402
403         for (i=0; i<num_subkeys; i++) {
404                 len += tdb_pack( buffer+len, buflen-len, "f", regsubkey_ctr_specific_key(ctr, i) );
405                 if ( len > buflen ) {
406                         /* allocate some extra space */
407                         if ((buffer = (uint8 *)SMB_REALLOC( buffer, len*2 )) == NULL) {
408                                 DEBUG(0,("regdb_store_keys: Failed to realloc memory of size [%d]\n", len*2));
409                                 ret = false;
410                                 goto done;
411                         }
412                         buflen = len*2;
413
414                         len = tdb_pack( buffer+len, buflen-len, "f", regsubkey_ctr_specific_key(ctr, i) );
415                 }
416         }
417
418         /* finally write out the data */
419
420         dbuf.dptr = buffer;
421         dbuf.dsize = len;
422         if ( tdb_store_bystring( tdb_reg->tdb, keyname, dbuf, TDB_REPLACE ) == -1) {
423                 ret = false;
424                 goto done;
425         }
426
427 done:
428         SAFE_FREE( buffer );
429         return ret;
430 }
431
432 /***********************************************************************
433  Store the new subkey record and create any child key records that
434  do not currently exist
435  ***********************************************************************/
436
437 bool regdb_store_keys(const char *key, REGSUBKEY_CTR *ctr)
438 {
439         int num_subkeys, i;
440         char *path = NULL;
441         REGSUBKEY_CTR *subkeys = NULL, *old_subkeys = NULL;
442         char *oldkeyname = NULL;
443         TALLOC_CTX *ctx = talloc_tos();
444
445         /*
446          * fetch a list of the old subkeys so we can determine if anything has
447          * changed
448          */
449
450         if (!(old_subkeys = TALLOC_ZERO_P(ctr, REGSUBKEY_CTR))) {
451                 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
452                 return false;
453         }
454
455         regdb_fetch_keys(key, old_subkeys);
456
457         if (ctr->num_subkeys == old_subkeys->num_subkeys) {
458
459                 for (i = 0; i<ctr->num_subkeys; i++) {
460                         if (strcmp(ctr->subkeys[i],
461                                    old_subkeys->subkeys[i]) != 0) {
462                                 break;
463                         }
464                 }
465                 if (i == ctr->num_subkeys) {
466                         /*
467                          * Nothing changed, no point to even start a tdb
468                          * transaction
469                          */
470                         TALLOC_FREE(old_subkeys);
471                         return true;
472                 }
473         }
474
475         if (tdb_transaction_start( tdb_reg->tdb ) == -1) {
476                 DEBUG(0, ("regdb_store_keys: tdb_transaction_start failed\n"));
477                 return false;
478         }
479
480         /*
481          * Re-fetch the old keys inside the transaction
482          */
483
484         TALLOC_FREE(old_subkeys);
485
486         if (!(old_subkeys = TALLOC_ZERO_P(ctr, REGSUBKEY_CTR))) {
487                 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
488                 goto fail;
489         }
490
491         regdb_fetch_keys(key, old_subkeys);
492
493         /* store the subkey list for the parent */
494
495         if (!regdb_store_keys_internal(key, ctr) ) {
496                 DEBUG(0,("regdb_store_keys: Failed to store new subkey list "
497                          "for parent [%s]\n", key));
498                 goto fail;
499         }
500
501         /* now delete removed keys */
502
503         num_subkeys = regsubkey_ctr_numkeys(old_subkeys);
504         for (i=0; i<num_subkeys; i++) {
505                 oldkeyname = regsubkey_ctr_specific_key(old_subkeys, i);
506
507                 if (regsubkey_ctr_key_exists(ctr, oldkeyname)) {
508                         /*
509                          * It's still around, don't delete
510                          */
511
512                         continue;
513                 }
514
515                 path = talloc_asprintf(ctx, "%s/%s", key, oldkeyname);
516                 if (!path) {
517                         goto fail;
518                 }
519                 path = normalize_reg_path(ctx, path);
520                 if (!path) {
521                         goto fail;
522                 }
523                 if (tdb_delete_bystring(tdb_reg->tdb, path) == -1) {
524                         DEBUG(1, ("Deleting %s failed\n", path));
525                         goto fail;
526                 }
527
528                 TALLOC_FREE(path);
529                 path = talloc_asprintf(ctx, "%s/%s/%s",
530                                 REG_VALUE_PREFIX,
531                                 key,
532                                 oldkeyname );
533                 if (!path) {
534                         goto fail;
535                 }
536                 path = normalize_reg_path(ctx, path);
537                 if (!path) {
538                         goto fail;
539                 }
540
541                 /*
542                  * Ignore errors here, we might have no values around
543                  */
544                 tdb_delete_bystring( tdb_reg->tdb, path );
545                 TALLOC_FREE(path);
546         }
547
548         TALLOC_FREE(old_subkeys);
549
550         /* now create records for any subkeys that don't already exist */
551
552         num_subkeys = regsubkey_ctr_numkeys(ctr);
553         for (i=0; i<num_subkeys; i++) {
554                 path = talloc_asprintf(ctx, "%s/%s",
555                                         key,
556                                         regsubkey_ctr_specific_key(ctr, i));
557                 if (!path) {
558                         goto fail;
559                 }
560                 if (!(subkeys = TALLOC_ZERO_P(ctr, REGSUBKEY_CTR)) ) {
561                         DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
562                         goto fail;
563                 }
564
565                 if (regdb_fetch_keys( path, subkeys ) == -1) {
566                         /* create a record with 0 subkeys */
567                         if (!regdb_store_keys_internal(path, subkeys)) {
568                                 DEBUG(0,("regdb_store_keys: Failed to store "
569                                          "new record for key [%s]\n", path));
570                                 goto fail;
571                         }
572                 }
573
574                 TALLOC_FREE(subkeys);
575                 TALLOC_FREE(path);
576         }
577
578         if (tdb_transaction_commit( tdb_reg->tdb ) == -1) {
579                 DEBUG(0, ("regdb_store_keys: Could not commit transaction\n"));
580                 return false;
581         }
582
583         return true;
584
585  fail:
586         TALLOC_FREE(old_subkeys);
587         TALLOC_FREE(subkeys);
588
589         if (tdb_transaction_cancel(tdb_reg->tdb) == -1) {
590                 smb_panic("regdb_store_keys: tdb_transaction_cancel failed\n");
591         }
592
593         return false;
594 }
595
596
597 /***********************************************************************
598  Retrieve an array of strings containing subkeys.  Memory should be
599  released by the caller.
600  ***********************************************************************/
601
602 int regdb_fetch_keys(const char *key, REGSUBKEY_CTR *ctr)
603 {
604         char *path = NULL;
605         uint32 num_items;
606         TDB_DATA dbuf;
607         uint8 *buf;
608         uint32 buflen, len;
609         int i;
610         fstring subkeyname;
611         int ret = -1;
612         TALLOC_CTX *frame = talloc_stackframe();
613
614         DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key ? key : "NULL"));
615
616         path = talloc_strdup(talloc_tos(), key);
617         if (!path) {
618                 goto fail;
619         }
620
621         /* convert to key format */
622         path = talloc_string_sub(talloc_tos(), path, "\\", "/");
623         if (!path) {
624                 goto fail;
625         }
626         strupper_m(path);
627
628         if (tdb_read_lock_bystring_with_timeout(tdb_reg->tdb, path, 10) == -1) {
629                 return 0;
630         }
631
632         dbuf = tdb_fetch_bystring(tdb_reg->tdb, path);
633         ctr->seqnum = regdb_get_seqnum();
634
635         tdb_read_unlock_bystring(tdb_reg->tdb, path);
636
637
638         buf = dbuf.dptr;
639         buflen = dbuf.dsize;
640
641         if ( !buf ) {
642                 DEBUG(5,("regdb_fetch_keys: tdb lookup failed to locate key [%s]\n", key));
643                 goto fail;
644         }
645
646         len = tdb_unpack( buf, buflen, "d", &num_items);
647
648         for (i=0; i<num_items; i++) {
649                 len += tdb_unpack(buf+len, buflen-len, "f", subkeyname);
650                 regsubkey_ctr_addkey(ctr, subkeyname);
651         }
652
653         SAFE_FREE(dbuf.dptr);
654
655         DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items));
656
657         ret = num_items;
658  fail:
659         TALLOC_FREE(frame);
660         return ret;
661 }
662
663 /****************************************************************************
664  Unpack a list of registry values frem the TDB
665  ***************************************************************************/
666
667 static int regdb_unpack_values(REGVAL_CTR *values, uint8 *buf, int buflen)
668 {
669         int             len = 0;
670         uint32          type;
671         fstring valuename;
672         uint32          size;
673         uint8           *data_p;
674         uint32          num_values = 0;
675         int             i;
676
677         /* loop and unpack the rest of the registry values */
678
679         len += tdb_unpack(buf+len, buflen-len, "d", &num_values);
680
681         for ( i=0; i<num_values; i++ ) {
682                 /* unpack the next regval */
683
684                 type = REG_NONE;
685                 size = 0;
686                 data_p = NULL;
687                 valuename[0] = '\0';
688                 len += tdb_unpack(buf+len, buflen-len, "fdB",
689                                   valuename,
690                                   &type,
691                                   &size,
692                                   &data_p);
693
694                 /* add the new value. Paranoid protective code -- make sure data_p is valid */
695
696                 if (*valuename && size && data_p) {
697                         regval_ctr_addvalue(values, valuename, type,
698                                         (const char *)data_p, size);
699                 }
700                 SAFE_FREE(data_p); /* 'B' option to tdb_unpack does a malloc() */
701
702                 DEBUG(8,("specific: [%s], len: %d\n", valuename, size));
703         }
704
705         return len;
706 }
707
708 /****************************************************************************
709  Pack all values in all printer keys
710  ***************************************************************************/
711
712 static int regdb_pack_values(REGVAL_CTR *values, uint8 *buf, int buflen)
713 {
714         int             len = 0;
715         int             i;
716         REGISTRY_VALUE  *val;
717         int             num_values;
718
719         if ( !values )
720                 return 0;
721
722         num_values = regval_ctr_numvals( values );
723
724         /* pack the number of values first */
725
726         len += tdb_pack( buf+len, buflen-len, "d", num_values );
727
728         /* loop over all values */
729
730         for ( i=0; i<num_values; i++ ) {
731                 val = regval_ctr_specific_value( values, i );
732                 len += tdb_pack(buf+len, buflen-len, "fdB",
733                                 regval_name(val),
734                                 regval_type(val),
735                                 regval_size(val),
736                                 regval_data_p(val) );
737         }
738
739         return len;
740 }
741
742 /***********************************************************************
743  Retrieve an array of strings containing subkeys.  Memory should be
744  released by the caller.
745  ***********************************************************************/
746
747 int regdb_fetch_values( const char* key, REGVAL_CTR *values )
748 {
749         TDB_DATA data;
750         char *keystr = NULL;
751         TALLOC_CTX *ctx = talloc_tos();
752
753         DEBUG(10,("regdb_fetch_values: Looking for value of key [%s] \n", key));
754
755         keystr = talloc_asprintf(ctx, "%s/%s", REG_VALUE_PREFIX, key);
756         if (!keystr) {
757                 return 0;
758         }
759         keystr = normalize_reg_path(ctx, keystr);
760         if (!keystr) {
761                 return 0;
762         }
763
764         if (tdb_read_lock_bystring_with_timeout(tdb_reg->tdb, keystr, 10) == -1) {
765                 return 0;
766         }
767
768         data = tdb_fetch_bystring(tdb_reg->tdb, keystr);
769         values->seqnum = regdb_get_seqnum();
770
771         tdb_read_unlock_bystring(tdb_reg->tdb, keystr);
772
773         if (!data.dptr) {
774                 /* all keys have zero values by default */
775                 return 0;
776         }
777
778         regdb_unpack_values(values, data.dptr, data.dsize);
779
780         SAFE_FREE(data.dptr);
781         return regval_ctr_numvals(values);
782 }
783
784 bool regdb_store_values( const char *key, REGVAL_CTR *values )
785 {
786         TDB_DATA old_data, data;
787         char *keystr = NULL;
788         TALLOC_CTX *ctx = talloc_tos();
789         int len, ret;
790
791         DEBUG(10,("regdb_store_values: Looking for value of key [%s] \n", key));
792
793         ZERO_STRUCT(data);
794
795         len = regdb_pack_values(values, data.dptr, data.dsize);
796         if (len <= 0) {
797                 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
798                 return false;
799         }
800
801         data.dptr = SMB_MALLOC_ARRAY( uint8, len );
802         data.dsize = len;
803
804         len = regdb_pack_values(values, data.dptr, data.dsize);
805
806         SMB_ASSERT( len == data.dsize );
807
808         keystr = talloc_asprintf(ctx, "%s/%s", REG_VALUE_PREFIX, key );
809         if (!keystr) {
810                 SAFE_FREE(data.dptr);
811                 return false;
812         }
813         keystr = normalize_reg_path(ctx, keystr);
814         if (!keystr) {
815                 SAFE_FREE(data.dptr);
816                 return false;
817         }
818
819         old_data = tdb_fetch_bystring(tdb_reg->tdb, keystr);
820
821         if ((old_data.dptr != NULL)
822             && (old_data.dsize == data.dsize)
823             && (memcmp(old_data.dptr, data.dptr, data.dsize) == 0)) {
824                 SAFE_FREE(old_data.dptr);
825                 SAFE_FREE(data.dptr);
826                 return true;
827         }
828
829         ret = tdb_trans_store_bystring(tdb_reg->tdb, keystr, data, TDB_REPLACE);
830
831         SAFE_FREE( old_data.dptr );
832         SAFE_FREE( data.dptr );
833
834         return ret != -1 ;
835 }
836
837 static WERROR regdb_get_secdesc(TALLOC_CTX *mem_ctx, const char *key,
838                                 struct security_descriptor **psecdesc)
839 {
840         char *tdbkey;
841         TDB_DATA data;
842         NTSTATUS status;
843
844         DEBUG(10, ("regdb_get_secdesc: Getting secdesc of key [%s]\n", key));
845
846         if (asprintf(&tdbkey, "%s/%s", REG_SECDESC_PREFIX, key) == -1) {
847                 return WERR_NOMEM;
848         }
849         normalize_dbkey(tdbkey);
850
851         data = tdb_fetch_bystring(tdb_reg->tdb, tdbkey);
852         SAFE_FREE(tdbkey);
853
854         if (data.dptr == NULL) {
855                 return WERR_BADFILE;
856         }
857
858         status = unmarshall_sec_desc(mem_ctx, (uint8 *)data.dptr, data.dsize,
859                                      psecdesc);
860
861         SAFE_FREE(data.dptr);
862
863         if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MEMORY)) {
864                 return WERR_NOMEM;
865         }
866
867         if (!NT_STATUS_IS_OK(status)) {
868                 return WERR_REG_CORRUPT;
869         }
870
871         return WERR_OK;
872 }
873
874 static WERROR regdb_set_secdesc(const char *key,
875                                 struct security_descriptor *secdesc)
876 {
877         prs_struct ps;
878         TALLOC_CTX *mem_ctx;
879         char *tdbkey;
880         WERROR err = WERR_NOMEM;
881         TDB_DATA tdbdata;
882
883         if (!(mem_ctx = talloc_init("regdb_set_secdesc"))) {
884                 return WERR_NOMEM;
885         }
886
887         ZERO_STRUCT(ps);
888
889         if (!(tdbkey = talloc_asprintf(mem_ctx, "%s/%s", REG_SECDESC_PREFIX,
890                                        key))) {
891                 goto done;
892         }
893         normalize_dbkey(tdbkey);
894
895         if (secdesc == NULL) {
896                 /* assuming a delete */
897                 int tdb_ret;
898
899                 tdb_ret = tdb_trans_delete(tdb_reg->tdb,
900                                            string_term_tdb_data(tdbkey));
901                 if (tdb_ret == -1) {
902                         err = ntstatus_to_werror(map_nt_error_from_unix(errno));
903                 } else {
904                         err = WERR_OK;
905                 }
906
907                 goto done;
908         }
909
910         err = ntstatus_to_werror(marshall_sec_desc(mem_ctx, secdesc,
911                                                    &tdbdata.dptr,
912                                                    &tdbdata.dsize));
913         if (!W_ERROR_IS_OK(err)) {
914                 goto done;
915         }
916
917         if (tdb_trans_store_bystring(tdb_reg->tdb, tdbkey, tdbdata, 0) == -1) {
918                 err = ntstatus_to_werror(map_nt_error_from_unix(errno));
919                 goto done;
920         }
921
922  done:
923         prs_mem_free(&ps);
924         TALLOC_FREE(mem_ctx);
925         return err;
926 }
927
928 bool regdb_subkeys_need_update(REGSUBKEY_CTR *subkeys)
929 {
930         return (regdb_get_seqnum() != subkeys->seqnum);
931 }
932
933 bool regdb_values_need_update(REGVAL_CTR *values)
934 {
935         return (regdb_get_seqnum() != values->seqnum);
936 }
937
938 /* 
939  * Table of function pointers for default access
940  */
941  
942 REGISTRY_OPS regdb_ops = {
943         regdb_fetch_keys,
944         regdb_fetch_values,
945         regdb_store_keys,
946         regdb_store_values,
947         NULL,
948         regdb_get_secdesc,
949         regdb_set_secdesc,
950         regdb_subkeys_need_update,
951         regdb_values_need_update
952 };