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