Convert dbwrap_trans_store to NTSTATUS
[ira/wip.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 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) != 0) {
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) != 0) {
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) != 0) {
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) != 0) {
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) != 0) {
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) != 0) {
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",
328                           regdb_refcount));
329                 regdb_refcount++;
330                 return true;
331         }
332
333         regdb = db_open_trans(NULL, state_path("registry.tdb"), 0,
334                               REG_TDB_FLAGS, O_RDWR, 0600);
335         if (!regdb) {
336                 regdb = db_open_trans(NULL, state_path("registry.tdb"), 0,
337                                       REG_TDB_FLAGS, O_RDWR|O_CREAT, 0600);
338                 if (!regdb) {
339                         DEBUG(0,("regdb_init: Failed to open registry %s (%s)\n",
340                                 state_path("registry.tdb"), strerror(errno) ));
341                         return false;
342                 }
343                 
344                 DEBUG(10,("regdb_init: Successfully created registry tdb\n"));
345         }
346
347         regdb_refcount = 1;
348
349         vers_id = dbwrap_fetch_int32(regdb, vstring);
350
351         if ( vers_id != REGVER_V1 ) {
352                 NTSTATUS status;
353                 /* any upgrade code here if needed */
354                 DEBUG(10, ("regdb_init: got %s = %d != %d\n", vstring,
355                            vers_id, REGVER_V1));
356                 status = dbwrap_trans_store_int32(regdb, vstring, REGVER_V1);
357                 if (!NT_STATUS_IS_OK(status)) {
358                         DEBUG(0, ("regdb_init: error storing %s = %d: %s\n",
359                                   vstring, REGVER_V1, nt_errstr(status)));
360                         return false;
361                 } else {
362                         DEBUG(10, ("regdb_init: stored %s = %d\n",
363                                   vstring, REGVER_V1));
364                 }
365         }
366
367         return true;
368 }
369
370 /***********************************************************************
371  Open the registry.  Must already have been initialized by regdb_init()
372  ***********************************************************************/
373
374 WERROR regdb_open( void )
375 {
376         WERROR result = WERR_OK;
377
378         if ( regdb ) {
379                 DEBUG(10,("regdb_open: incrementing refcount (%d)\n", regdb_refcount));
380                 regdb_refcount++;
381                 return WERR_OK;
382         }
383         
384         become_root();
385
386         regdb = db_open_trans(NULL, state_path("registry.tdb"), 0,
387                               REG_TDB_FLAGS, O_RDWR, 0600);
388         if ( !regdb ) {
389                 result = ntstatus_to_werror( map_nt_error_from_unix( errno ) );
390                 DEBUG(0,("regdb_open: Failed to open %s! (%s)\n", 
391                         state_path("registry.tdb"), strerror(errno) ));
392         }
393
394         unbecome_root();
395
396         regdb_refcount = 1;
397         DEBUG(10,("regdb_open: refcount reset (%d)\n", regdb_refcount));
398
399         return result;
400 }
401
402 /***********************************************************************
403  ***********************************************************************/
404
405 int regdb_close( void )
406 {
407         if (regdb_refcount == 0) {
408                 return 0;
409         }
410
411         regdb_refcount--;
412
413         DEBUG(10,("regdb_close: decrementing refcount (%d)\n", regdb_refcount));
414
415         if ( regdb_refcount > 0 )
416                 return 0;
417
418         SMB_ASSERT( regdb_refcount >= 0 );
419
420         TALLOC_FREE(regdb);
421         return 0;
422 }
423
424 /***********************************************************************
425  return the tdb sequence number of the registry tdb.
426  this is an indicator for the content of the registry
427  having changed. it will change upon regdb_init, too, though.
428  ***********************************************************************/
429 int regdb_get_seqnum(void)
430 {
431         return regdb->get_seqnum(regdb);
432 }
433
434 /***********************************************************************
435  Add subkey strings to the registry tdb under a defined key
436  fmt is the same format as tdb_pack except this function only supports
437  fstrings
438  ***********************************************************************/
439
440 static bool regdb_store_keys_internal(const char *key, REGSUBKEY_CTR *ctr)
441 {
442         TDB_DATA dbuf;
443         uint8 *buffer = NULL;
444         int i = 0;
445         uint32 len, buflen;
446         bool ret = true;
447         uint32 num_subkeys = regsubkey_ctr_numkeys(ctr);
448         char *keyname = NULL;
449         TALLOC_CTX *ctx = talloc_stackframe();
450         NTSTATUS status;
451
452         if (!key) {
453                 return false;
454         }
455
456         keyname = talloc_strdup(ctx, key);
457         if (!keyname) {
458                 return false;
459         }
460         keyname = normalize_reg_path(ctx, keyname);
461
462         /* allocate some initial memory */
463
464         buffer = (uint8 *)SMB_MALLOC(1024);
465         if (buffer == NULL) {
466                 return false;
467         }
468         buflen = 1024;
469         len = 0;
470
471         /* store the number of subkeys */
472
473         len += tdb_pack(buffer+len, buflen-len, "d", num_subkeys);
474
475         /* pack all the strings */
476
477         for (i=0; i<num_subkeys; i++) {
478                 len += tdb_pack(buffer+len, buflen-len, "f",
479                                 regsubkey_ctr_specific_key(ctr, i));
480                 if (len > buflen) {
481                         /* allocate some extra space */
482                         buffer = (uint8 *)SMB_REALLOC(buffer, len*2);
483                         if(buffer == NULL) {
484                                 DEBUG(0, ("regdb_store_keys: Failed to realloc "
485                                           "memory of size [%d]\n", len*2));
486                                 ret = false;
487                                 goto done;
488                         }
489                         buflen = len*2;
490                         len = tdb_pack(buffer+len, buflen-len, "f",
491                                        regsubkey_ctr_specific_key(ctr, i));
492                 }
493         }
494
495         /* finally write out the data */
496
497         dbuf.dptr = buffer;
498         dbuf.dsize = len;
499         status = dbwrap_store_bystring(regdb, keyname, dbuf, TDB_REPLACE);
500         if (!NT_STATUS_IS_OK(status)) {
501                 ret = false;
502                 goto done;
503         }
504
505 done:
506         TALLOC_FREE(ctx);
507         SAFE_FREE(buffer);
508         return ret;
509 }
510
511 /***********************************************************************
512  Store the new subkey record and create any child key records that
513  do not currently exist
514  ***********************************************************************/
515
516 bool regdb_store_keys(const char *key, REGSUBKEY_CTR *ctr)
517 {
518         int num_subkeys, i;
519         char *path = NULL;
520         REGSUBKEY_CTR *subkeys = NULL, *old_subkeys = NULL;
521         char *oldkeyname = NULL;
522         TALLOC_CTX *ctx = talloc_stackframe();
523         NTSTATUS status;
524
525         /*
526          * fetch a list of the old subkeys so we can determine if anything has
527          * changed
528          */
529
530         if (!(old_subkeys = TALLOC_ZERO_P(ctx, REGSUBKEY_CTR))) {
531                 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
532                 return false;
533         }
534
535         regdb_fetch_keys(key, old_subkeys);
536
537         if ((ctr->num_subkeys && old_subkeys->num_subkeys) &&
538             (ctr->num_subkeys == old_subkeys->num_subkeys)) {
539
540                 for (i = 0; i<ctr->num_subkeys; i++) {
541                         if (strcmp(ctr->subkeys[i],
542                                    old_subkeys->subkeys[i]) != 0) {
543                                 break;
544                         }
545                 }
546                 if (i == ctr->num_subkeys) {
547                         /*
548                          * Nothing changed, no point to even start a tdb
549                          * transaction
550                          */
551                         TALLOC_FREE(old_subkeys);
552                         return true;
553                 }
554         }
555
556         TALLOC_FREE(old_subkeys);
557
558         if (regdb->transaction_start(regdb) != 0) {
559                 DEBUG(0, ("regdb_store_keys: transaction_start failed\n"));
560                 goto fail;
561         }
562
563         /*
564          * Re-fetch the old keys inside the transaction
565          */
566
567         if (!(old_subkeys = TALLOC_ZERO_P(ctx, REGSUBKEY_CTR))) {
568                 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
569                 goto cancel;
570         }
571
572         regdb_fetch_keys(key, old_subkeys);
573
574         /* store the subkey list for the parent */
575
576         if (!regdb_store_keys_internal(key, ctr) ) {
577                 DEBUG(0,("regdb_store_keys: Failed to store new subkey list "
578                          "for parent [%s]\n", key));
579                 goto cancel;
580         }
581
582         /* now delete removed keys */
583
584         num_subkeys = regsubkey_ctr_numkeys(old_subkeys);
585         for (i=0; i<num_subkeys; i++) {
586                 oldkeyname = regsubkey_ctr_specific_key(old_subkeys, i);
587
588                 if (regsubkey_ctr_key_exists(ctr, oldkeyname)) {
589                         /*
590                          * It's still around, don't delete
591                          */
592
593                         continue;
594                 }
595
596                 path = talloc_asprintf(ctx, "%s/%s", key, oldkeyname);
597                 if (!path) {
598                         goto cancel;
599                 }
600                 path = normalize_reg_path(ctx, path);
601                 if (!path) {
602                         goto cancel;
603                 }
604                 status = dbwrap_delete_bystring(regdb, path);
605                 if (!NT_STATUS_IS_OK(status)) {
606                         DEBUG(1, ("Deleting %s failed\n", path));
607                         goto cancel;
608                 }
609
610                 TALLOC_FREE(path);
611                 path = talloc_asprintf(ctx, "%s/%s/%s",
612                                 REG_VALUE_PREFIX,
613                                 key,
614                                 oldkeyname );
615                 if (!path) {
616                         goto cancel;
617                 }
618                 path = normalize_reg_path(ctx, path);
619                 if (!path) {
620                         goto cancel;
621                 }
622
623                 /*
624                  * Ignore errors here, we might have no values around
625                  */
626                 dbwrap_delete_bystring(regdb, path);
627                 TALLOC_FREE(path);
628         }
629
630         TALLOC_FREE(old_subkeys);
631
632         /* now create records for any subkeys that don't already exist */
633
634         num_subkeys = regsubkey_ctr_numkeys(ctr);
635
636         if (num_subkeys == 0) {
637                 if (!(subkeys = TALLOC_ZERO_P(ctx, REGSUBKEY_CTR)) ) {
638                         DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
639                         goto cancel;
640                 }
641
642                 if (!regdb_store_keys_internal(key, subkeys)) {
643                         DEBUG(0,("regdb_store_keys: Failed to store "
644                                  "new record for key [%s]\n", key));
645                         goto cancel;
646                 }
647                 TALLOC_FREE(subkeys);
648
649         }
650
651         for (i=0; i<num_subkeys; i++) {
652                 path = talloc_asprintf(ctx, "%s/%s",
653                                         key,
654                                         regsubkey_ctr_specific_key(ctr, i));
655                 if (!path) {
656                         goto cancel;
657                 }
658                 if (!(subkeys = TALLOC_ZERO_P(ctx, REGSUBKEY_CTR)) ) {
659                         DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
660                         goto cancel;
661                 }
662
663                 if (regdb_fetch_keys( path, subkeys ) == -1) {
664                         /* create a record with 0 subkeys */
665                         if (!regdb_store_keys_internal(path, subkeys)) {
666                                 DEBUG(0,("regdb_store_keys: Failed to store "
667                                          "new record for key [%s]\n", path));
668                                 goto cancel;
669                         }
670                 }
671
672                 TALLOC_FREE(subkeys);
673                 TALLOC_FREE(path);
674         }
675
676         if (regdb->transaction_commit(regdb) != 0) {
677                 DEBUG(0, ("regdb_store_keys: Could not commit transaction\n"));
678                 goto fail;
679         }
680
681         TALLOC_FREE(ctx);
682         return true;
683
684 cancel:
685         if (regdb->transaction_cancel(regdb) != 0) {
686                 smb_panic("regdb_store_keys: transaction_cancel failed\n");
687         }
688
689 fail:
690         TALLOC_FREE(ctx);
691
692         return false;
693 }
694
695
696 /***********************************************************************
697  Retrieve an array of strings containing subkeys.  Memory should be
698  released by the caller.
699  ***********************************************************************/
700
701 int regdb_fetch_keys(const char *key, REGSUBKEY_CTR *ctr)
702 {
703         char *path = NULL;
704         uint32 num_items;
705         uint8 *buf;
706         uint32 buflen, len;
707         int i;
708         fstring subkeyname;
709         int ret = -1;
710         int dbret = -1;
711         TALLOC_CTX *frame = talloc_stackframe();
712         TDB_DATA value;
713
714         DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key ? key : "NULL"));
715
716         path = talloc_strdup(frame, key);
717         if (!path) {
718                 goto fail;
719         }
720
721         /* convert to key format */
722         path = talloc_string_sub(frame, path, "\\", "/");
723         if (!path) {
724                 goto fail;
725         }
726         strupper_m(path);
727
728         ctr->seqnum = regdb_get_seqnum();
729
730         dbret = regdb->fetch(regdb, frame, string_term_tdb_data(path), &value);
731         if (dbret != 0) {
732                 goto fail;
733         }
734
735         buf = value.dptr;
736         buflen = value.dsize;
737
738         if ( !buf ) {
739                 DEBUG(5,("regdb_fetch_keys: tdb lookup failed to locate key [%s]\n", key));
740                 goto fail;
741         }
742
743         len = tdb_unpack( buf, buflen, "d", &num_items);
744
745         for (i=0; i<num_items; i++) {
746                 len += tdb_unpack(buf+len, buflen-len, "f", subkeyname);
747                 regsubkey_ctr_addkey(ctr, subkeyname);
748         }
749
750         DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items));
751
752         ret = num_items;
753  fail:
754         TALLOC_FREE(frame);
755         return ret;
756 }
757
758 /****************************************************************************
759  Unpack a list of registry values frem the TDB
760  ***************************************************************************/
761
762 static int regdb_unpack_values(REGVAL_CTR *values, uint8 *buf, int buflen)
763 {
764         int             len = 0;
765         uint32          type;
766         fstring valuename;
767         uint32          size;
768         uint8           *data_p;
769         uint32          num_values = 0;
770         int             i;
771
772         /* loop and unpack the rest of the registry values */
773
774         len += tdb_unpack(buf+len, buflen-len, "d", &num_values);
775
776         for ( i=0; i<num_values; i++ ) {
777                 /* unpack the next regval */
778
779                 type = REG_NONE;
780                 size = 0;
781                 data_p = NULL;
782                 valuename[0] = '\0';
783                 len += tdb_unpack(buf+len, buflen-len, "fdB",
784                                   valuename,
785                                   &type,
786                                   &size,
787                                   &data_p);
788
789                 /* add the new value. Paranoid protective code -- make sure data_p is valid */
790
791                 if (*valuename && size && data_p) {
792                         regval_ctr_addvalue(values, valuename, type,
793                                         (const char *)data_p, size);
794                 }
795                 SAFE_FREE(data_p); /* 'B' option to tdb_unpack does a malloc() */
796
797                 DEBUG(8,("specific: [%s], len: %d\n", valuename, size));
798         }
799
800         return len;
801 }
802
803 /****************************************************************************
804  Pack all values in all printer keys
805  ***************************************************************************/
806
807 static int regdb_pack_values(REGVAL_CTR *values, uint8 *buf, int buflen)
808 {
809         int             len = 0;
810         int             i;
811         REGISTRY_VALUE  *val;
812         int             num_values;
813
814         if ( !values )
815                 return 0;
816
817         num_values = regval_ctr_numvals( values );
818
819         /* pack the number of values first */
820
821         len += tdb_pack( buf+len, buflen-len, "d", num_values );
822
823         /* loop over all values */
824
825         for ( i=0; i<num_values; i++ ) {
826                 val = regval_ctr_specific_value( values, i );
827                 len += tdb_pack(buf+len, buflen-len, "fdB",
828                                 regval_name(val),
829                                 regval_type(val),
830                                 regval_size(val),
831                                 regval_data_p(val) );
832         }
833
834         return len;
835 }
836
837 /***********************************************************************
838  Retrieve an array of strings containing subkeys.  Memory should be
839  released by the caller.
840  ***********************************************************************/
841
842 int regdb_fetch_values( const char* key, REGVAL_CTR *values )
843 {
844         char *keystr = NULL;
845         TALLOC_CTX *ctx = talloc_stackframe();
846         int ret = 0;
847         int dbret = -1;
848         TDB_DATA value;
849
850         DEBUG(10,("regdb_fetch_values: Looking for value of key [%s] \n", key));
851
852         keystr = talloc_asprintf(ctx, "%s/%s", REG_VALUE_PREFIX, key);
853         if (!keystr) {
854                 return 0;
855         }
856         keystr = normalize_reg_path(ctx, keystr);
857         if (!keystr) {
858                 goto done;
859         }
860
861         values->seqnum = regdb_get_seqnum();
862
863         dbret = regdb->fetch(regdb, ctx, string_term_tdb_data(keystr), &value);
864         if (dbret != 0) {
865                 goto done;
866         }
867
868         if (!value.dptr) {
869                 /* all keys have zero values by default */
870                 goto done;
871         }
872
873         regdb_unpack_values(values, value.dptr, value.dsize);
874         ret = regval_ctr_numvals(values);
875
876 done:
877         TALLOC_FREE(ctx);
878         return ret;
879 }
880
881 bool regdb_store_values( const char *key, REGVAL_CTR *values )
882 {
883         TDB_DATA old_data, data;
884         char *keystr = NULL;
885         TALLOC_CTX *ctx = talloc_stackframe();
886         int len;
887         NTSTATUS status;
888         bool result = false;
889
890         DEBUG(10,("regdb_store_values: Looking for value of key [%s] \n", key));
891
892         ZERO_STRUCT(data);
893
894         len = regdb_pack_values(values, data.dptr, data.dsize);
895         if (len <= 0) {
896                 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
897                 goto done;
898         }
899
900         data.dptr = TALLOC_ARRAY(ctx, uint8, len);
901         data.dsize = len;
902
903         len = regdb_pack_values(values, data.dptr, data.dsize);
904
905         SMB_ASSERT( len == data.dsize );
906
907         keystr = talloc_asprintf(ctx, "%s/%s", REG_VALUE_PREFIX, key );
908         if (!keystr) {
909                 goto done;
910         }
911         keystr = normalize_reg_path(ctx, keystr);
912         if (!keystr) {
913                 goto done;
914         }
915
916         old_data = dbwrap_fetch_bystring(regdb, ctx, keystr);
917
918         if ((old_data.dptr != NULL)
919             && (old_data.dsize == data.dsize)
920             && (memcmp(old_data.dptr, data.dptr, data.dsize) == 0))
921         {
922                 result = true;
923                 goto done;
924         }
925
926         status = dbwrap_trans_store(regdb, string_term_tdb_data(keystr), data,
927                                     TDB_REPLACE);
928
929         result = NT_STATUS_IS_OK(status);
930
931 done:
932         TALLOC_FREE(ctx);
933         return result;
934 }
935
936 static WERROR regdb_get_secdesc(TALLOC_CTX *mem_ctx, const char *key,
937                                 struct security_descriptor **psecdesc)
938 {
939         char *tdbkey;
940         TDB_DATA data;
941         NTSTATUS status;
942         TALLOC_CTX *tmp_ctx = talloc_stackframe();
943         WERROR err = WERR_OK;
944
945         DEBUG(10, ("regdb_get_secdesc: Getting secdesc of key [%s]\n", key));
946
947         tdbkey = talloc_asprintf(tmp_ctx, "%s/%s", REG_SECDESC_PREFIX, key);
948         if (tdbkey == NULL) {
949                 err = WERR_NOMEM;
950                 goto done;
951         }
952         normalize_dbkey(tdbkey);
953
954         data = dbwrap_fetch_bystring(regdb, tmp_ctx, tdbkey);
955         if (data.dptr == NULL) {
956                 err = WERR_BADFILE;
957                 goto done;
958         }
959
960         status = unmarshall_sec_desc(mem_ctx, (uint8 *)data.dptr, data.dsize,
961                                      psecdesc);
962
963         if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MEMORY)) {
964                 err = WERR_NOMEM;
965         } else if (!NT_STATUS_IS_OK(status)) {
966                 err = WERR_REG_CORRUPT;
967         }
968
969 done:
970         TALLOC_FREE(tmp_ctx);
971         return err;
972 }
973
974 static WERROR regdb_set_secdesc(const char *key,
975                                 struct security_descriptor *secdesc)
976 {
977         TALLOC_CTX *mem_ctx = talloc_stackframe();
978         char *tdbkey;
979         NTSTATUS status;
980         WERROR err = WERR_NOMEM;
981         TDB_DATA tdbdata;
982         int tdb_ret;
983
984         tdbkey = talloc_asprintf(mem_ctx, "%s/%s", REG_SECDESC_PREFIX, key);
985         if (tdbkey == NULL) {
986                 goto done;
987         }
988         normalize_dbkey(tdbkey);
989
990         if (secdesc == NULL) {
991                 /* assuming a delete */
992                 tdb_ret = dbwrap_trans_delete(regdb,
993                                               string_term_tdb_data(tdbkey));
994                 if (tdb_ret == 0) {
995                         err = WERR_OK;
996                 } else {
997                         err = ntstatus_to_werror(map_nt_error_from_unix(errno));
998                 }
999                 goto done;
1000         }
1001
1002         err = ntstatus_to_werror(marshall_sec_desc(mem_ctx, secdesc,
1003                                                    &tdbdata.dptr,
1004                                                    &tdbdata.dsize));
1005         if (!W_ERROR_IS_OK(err)) {
1006                 goto done;
1007         }
1008
1009         status = dbwrap_trans_store(regdb, string_term_tdb_data(tdbkey),
1010                                     tdbdata, 0);
1011         if (!NT_STATUS_IS_OK(status)) {
1012                 err = ntstatus_to_werror(status);
1013                 goto done;
1014         }
1015
1016  done:
1017         TALLOC_FREE(mem_ctx);
1018         return err;
1019 }
1020
1021 bool regdb_subkeys_need_update(REGSUBKEY_CTR *subkeys)
1022 {
1023         return (regdb_get_seqnum() != subkeys->seqnum);
1024 }
1025
1026 bool regdb_values_need_update(REGVAL_CTR *values)
1027 {
1028         return (regdb_get_seqnum() != values->seqnum);
1029 }
1030
1031 /* 
1032  * Table of function pointers for default access
1033  */
1034  
1035 REGISTRY_OPS regdb_ops = {
1036         .fetch_subkeys = regdb_fetch_keys,
1037         .fetch_values = regdb_fetch_values,
1038         .store_subkeys = regdb_store_keys,
1039         .store_values = regdb_store_values,
1040         .get_secdesc = regdb_get_secdesc,
1041         .set_secdesc = regdb_set_secdesc,
1042         .subkeys_need_update = regdb_subkeys_need_update,
1043         .values_need_update = regdb_values_need_update
1044 };