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