3.2 samr bug 6301: fix samr_ConnectVersion enum which is 32bit not 16bit.
[Samba.git] / source / registry / reg_backend_db.c
blobfb8b64a05e79cb2117d7cfc76ac947498bd80739
1 /*
2 * Unix SMB/CIFS implementation.
3 * Virtual Windows Registry Layer
4 * Copyright (C) Gerald Carter 2002-2005
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.
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.
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/>.
20 /* Implementation of internal registry database functions. */
22 #include "includes.h"
24 #undef DBGC_CLASS
25 #define DBGC_CLASS DBGC_REGISTRY
27 static struct db_context *regdb = NULL;
28 static int regdb_refcount;
30 /* List the deepest path into the registry. All part components will be created.*/
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 */
40 static const char *builtin_registry_paths[] = {
41 KEY_PRINTING_2K,
42 KEY_PRINTING_PORTS,
43 KEY_PRINTING,
44 KEY_SHARES,
45 KEY_EVENTLOG,
46 KEY_SMBCONF,
47 KEY_PERFLIB,
48 KEY_PERFLIB_009,
49 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Print\\Monitors",
50 KEY_PROD_OPTIONS,
51 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\DefaultUserConfiguration",
52 KEY_TCPIP_PARAMS,
53 KEY_NETLOGON_PARAMS,
54 KEY_HKU,
55 KEY_HKCR,
56 KEY_HKPD,
57 KEY_HKPT,
58 NULL };
60 struct builtin_regkey_value {
61 const char *path;
62 const char *valuename;
63 uint32 type;
64 union {
65 const char *string;
66 uint32 dw_value;
67 } data;
70 static struct builtin_regkey_value builtin_registry_values[] = {
71 { KEY_PRINTING_PORTS,
72 SAMBA_PRINTER_PORT_NAME, REG_SZ, { "" } },
73 { KEY_PRINTING_2K,
74 "DefaultSpoolDirectory", REG_SZ, { "C:\\Windows\\System32\\Spool\\Printers" } },
75 { KEY_EVENTLOG,
76 "DisplayName", REG_SZ, { "Event Log" } },
77 { KEY_EVENTLOG,
78 "ErrorControl", REG_DWORD, { (char*)0x00000001 } },
79 { NULL, NULL, 0, { NULL } }
82 /**
83 * Initialize a key in the registry:
84 * create each component key of the specified path.
86 static WERROR init_registry_key_internal(const char *add_path)
88 WERROR werr;
89 TALLOC_CTX *frame = talloc_stackframe();
90 char *path = NULL;
91 char *base = NULL;
92 char *remaining = NULL;
93 char *keyname;
94 char *subkeyname;
95 REGSUBKEY_CTR *subkeys;
96 const char *p, *p2;
98 DEBUG(6, ("init_registry_key: Adding [%s]\n", add_path));
100 path = talloc_strdup(frame, add_path);
101 base = talloc_strdup(frame, "");
102 if (!path || !base) {
103 werr = WERR_NOMEM;
104 goto fail;
106 p = path;
108 while (next_token_talloc(frame, &p, &keyname, "\\")) {
110 /* build up the registry path from the components */
112 if (*base) {
113 base = talloc_asprintf(frame, "%s\\", base);
114 if (!base) {
115 werr = WERR_NOMEM;
116 goto fail;
119 base = talloc_asprintf_append(base, "%s", keyname);
120 if (!base) {
121 werr = WERR_NOMEM;
122 goto fail;
125 /* get the immediate subkeyname (if we have one ) */
127 subkeyname = talloc_strdup(frame, "");
128 if (!subkeyname) {
129 werr = WERR_NOMEM;
130 goto fail;
132 if (*p) {
133 remaining = talloc_strdup(frame, p);
134 if (!remaining) {
135 werr = WERR_NOMEM;
136 goto fail;
138 p2 = remaining;
140 if (!next_token_talloc(frame, &p2,
141 &subkeyname, "\\"))
143 subkeyname = talloc_strdup(frame,p2);
144 if (!subkeyname) {
145 werr = WERR_NOMEM;
146 goto fail;
151 DEBUG(10,("init_registry_key: Storing key [%s] with "
152 "subkey [%s]\n", base,
153 *subkeyname ? subkeyname : "NULL"));
155 /* we don't really care if the lookup succeeds or not
156 * since we are about to update the record.
157 * We just want any subkeys already present */
159 if (!(subkeys = TALLOC_ZERO_P(frame, REGSUBKEY_CTR))) {
160 DEBUG(0,("talloc() failure!\n"));
161 werr = WERR_NOMEM;
162 goto fail;
165 regdb_fetch_keys(base, subkeys);
166 if (*subkeyname) {
167 werr = regsubkey_ctr_addkey(subkeys, subkeyname);
168 if (!W_ERROR_IS_OK(werr)) {
169 goto fail;
172 if (!regdb_store_keys( base, subkeys)) {
173 werr = WERR_CAN_NOT_COMPLETE;
174 goto fail;
178 werr = WERR_OK;
180 fail:
181 TALLOC_FREE(frame);
182 return werr;
186 * Initialize a key in the registry:
187 * create each component key of the specified path,
188 * wrapped in one db transaction.
190 WERROR init_registry_key(const char *add_path)
192 WERROR werr;
194 if (regdb->transaction_start(regdb) != 0) {
195 DEBUG(0, ("init_registry_key: transaction_start failed\n"));
196 return WERR_REG_IO_FAILURE;
199 werr = init_registry_key_internal(add_path);
200 if (!W_ERROR_IS_OK(werr)) {
201 goto fail;
204 if (regdb->transaction_commit(regdb) != 0) {
205 DEBUG(0, ("init_registry_key: Could not commit transaction\n"));
206 return WERR_REG_IO_FAILURE;
209 return WERR_OK;
211 fail:
212 if (regdb->transaction_cancel(regdb) != 0) {
213 smb_panic("init_registry_key: transaction_cancel failed\n");
216 return werr;
219 /***********************************************************************
220 Open the registry data in the tdb
221 ***********************************************************************/
223 WERROR init_registry_data(void)
225 WERROR werr;
226 TALLOC_CTX *frame = NULL;
227 REGVAL_CTR *values;
228 int i;
229 UNISTR2 data;
232 * There are potentially quite a few store operations which are all
233 * indiviually wrapped in tdb transactions. Wrapping them in a single
234 * transaction gives just a single transaction_commit() to actually do
235 * its fsync()s. See tdb/common/transaction.c for info about nested
236 * transaction behaviour.
239 if (regdb->transaction_start(regdb) != 0) {
240 DEBUG(0, ("init_registry_data: tdb_transaction_start "
241 "failed\n"));
242 return WERR_REG_IO_FAILURE;
245 /* loop over all of the predefined paths and add each component */
247 for (i=0; builtin_registry_paths[i] != NULL; i++) {
248 werr = init_registry_key_internal(builtin_registry_paths[i]);
249 if (!W_ERROR_IS_OK(werr)) {
250 goto fail;
254 /* loop over all of the predefined values and add each component */
256 frame = talloc_stackframe();
258 for (i=0; builtin_registry_values[i].path != NULL; i++) {
260 values = TALLOC_ZERO_P(frame, REGVAL_CTR);
261 if (values == NULL) {
262 werr = WERR_NOMEM;
263 goto fail;
266 regdb_fetch_values(builtin_registry_values[i].path, values);
268 /* preserve existing values across restarts. Only add new ones */
270 if (!regval_ctr_key_exists(values,
271 builtin_registry_values[i].valuename))
273 switch(builtin_registry_values[i].type) {
274 case REG_DWORD:
275 regval_ctr_addvalue(values,
276 builtin_registry_values[i].valuename,
277 REG_DWORD,
278 (char*)&builtin_registry_values[i].data.dw_value,
279 sizeof(uint32));
280 break;
282 case REG_SZ:
283 init_unistr2(&data,
284 builtin_registry_values[i].data.string,
285 UNI_STR_TERMINATE);
286 regval_ctr_addvalue(values,
287 builtin_registry_values[i].valuename,
288 REG_SZ,
289 (char*)data.buffer,
290 data.uni_str_len*sizeof(uint16));
291 break;
293 default:
294 DEBUG(0, ("init_registry_data: invalid value "
295 "type in builtin_registry_values "
296 "[%d]\n",
297 builtin_registry_values[i].type));
299 regdb_store_values(builtin_registry_values[i].path,
300 values);
302 TALLOC_FREE(values);
305 TALLOC_FREE(frame);
307 if (regdb->transaction_commit(regdb) != 0) {
308 DEBUG(0, ("init_registry_data: Could not commit "
309 "transaction\n"));
310 return WERR_REG_IO_FAILURE;
313 return WERR_OK;
315 fail:
317 TALLOC_FREE(frame);
319 if (regdb->transaction_cancel(regdb) != 0) {
320 smb_panic("init_registry_data: tdb_transaction_cancel "
321 "failed\n");
324 return werr;
327 /***********************************************************************
328 Open the registry database
329 ***********************************************************************/
331 WERROR regdb_init(void)
333 const char *vstring = "INFO/version";
334 uint32 vers_id;
335 WERROR werr;
337 if (regdb) {
338 DEBUG(10, ("regdb_init: incrementing refcount (%d)\n",
339 regdb_refcount));
340 regdb_refcount++;
341 return WERR_OK;
344 regdb = db_open_trans(NULL, state_path("registry.tdb"), 0,
345 REG_TDB_FLAGS, O_RDWR, 0600);
346 if (!regdb) {
347 regdb = db_open_trans(NULL, state_path("registry.tdb"), 0,
348 REG_TDB_FLAGS, O_RDWR|O_CREAT, 0600);
349 if (!regdb) {
350 werr = ntstatus_to_werror(map_nt_error_from_unix(errno));
351 DEBUG(0,("regdb_init: Failed to open registry %s (%s)\n",
352 state_path("registry.tdb"), strerror(errno) ));
353 return werr;
356 DEBUG(10,("regdb_init: Successfully created registry tdb\n"));
359 regdb_refcount = 1;
361 vers_id = dbwrap_fetch_int32(regdb, vstring);
363 if ( vers_id != REGVER_V1 ) {
364 NTSTATUS status;
365 /* any upgrade code here if needed */
366 DEBUG(10, ("regdb_init: got %s = %d != %d\n", vstring,
367 vers_id, REGVER_V1));
368 status = dbwrap_trans_store_int32(regdb, vstring, REGVER_V1);
369 if (!NT_STATUS_IS_OK(status)) {
370 DEBUG(0, ("regdb_init: error storing %s = %d: %s\n",
371 vstring, REGVER_V1, nt_errstr(status)));
372 return ntstatus_to_werror(status);
373 } else {
374 DEBUG(10, ("regdb_init: stored %s = %d\n",
375 vstring, REGVER_V1));
379 return WERR_OK;
382 /***********************************************************************
383 Open the registry. Must already have been initialized by regdb_init()
384 ***********************************************************************/
386 WERROR regdb_open( void )
388 WERROR result = WERR_OK;
390 if ( regdb ) {
391 DEBUG(10,("regdb_open: incrementing refcount (%d)\n", regdb_refcount));
392 regdb_refcount++;
393 return WERR_OK;
396 become_root();
398 regdb = db_open_trans(NULL, state_path("registry.tdb"), 0,
399 REG_TDB_FLAGS, O_RDWR, 0600);
400 if ( !regdb ) {
401 result = ntstatus_to_werror( map_nt_error_from_unix( errno ) );
402 DEBUG(0,("regdb_open: Failed to open %s! (%s)\n",
403 state_path("registry.tdb"), strerror(errno) ));
406 unbecome_root();
408 regdb_refcount = 1;
409 DEBUG(10,("regdb_open: refcount reset (%d)\n", regdb_refcount));
411 return result;
414 /***********************************************************************
415 ***********************************************************************/
417 int regdb_close( void )
419 if (regdb_refcount == 0) {
420 return 0;
423 regdb_refcount--;
425 DEBUG(10,("regdb_close: decrementing refcount (%d)\n", regdb_refcount));
427 if ( regdb_refcount > 0 )
428 return 0;
430 SMB_ASSERT( regdb_refcount >= 0 );
432 TALLOC_FREE(regdb);
433 return 0;
436 /***********************************************************************
437 return the tdb sequence number of the registry tdb.
438 this is an indicator for the content of the registry
439 having changed. it will change upon regdb_init, too, though.
440 ***********************************************************************/
441 int regdb_get_seqnum(void)
443 return regdb->get_seqnum(regdb);
446 /***********************************************************************
447 Add subkey strings to the registry tdb under a defined key
448 fmt is the same format as tdb_pack except this function only supports
449 fstrings
450 ***********************************************************************/
452 static bool regdb_store_keys_internal(const char *key, REGSUBKEY_CTR *ctr)
454 TDB_DATA dbuf;
455 uint8 *buffer = NULL;
456 int i = 0;
457 uint32 len, buflen;
458 bool ret = true;
459 uint32 num_subkeys = regsubkey_ctr_numkeys(ctr);
460 char *keyname = NULL;
461 TALLOC_CTX *ctx = talloc_stackframe();
462 NTSTATUS status;
464 if (!key) {
465 return false;
468 keyname = talloc_strdup(ctx, key);
469 if (!keyname) {
470 return false;
472 keyname = normalize_reg_path(ctx, keyname);
474 /* allocate some initial memory */
476 buffer = (uint8 *)SMB_MALLOC(1024);
477 if (buffer == NULL) {
478 return false;
480 buflen = 1024;
481 len = 0;
483 /* store the number of subkeys */
485 len += tdb_pack(buffer+len, buflen-len, "d", num_subkeys);
487 /* pack all the strings */
489 for (i=0; i<num_subkeys; i++) {
490 size_t thistime;
492 thistime = tdb_pack(buffer+len, buflen-len, "f",
493 regsubkey_ctr_specific_key(ctr, i));
494 if (len+thistime > buflen) {
495 size_t thistime2;
497 * tdb_pack hasn't done anything because of the short
498 * buffer, allocate extra space.
500 buffer = SMB_REALLOC_ARRAY(buffer, uint8_t,
501 (len+thistime)*2);
502 if(buffer == NULL) {
503 DEBUG(0, ("regdb_store_keys: Failed to realloc "
504 "memory of size [%d]\n",
505 (len+thistime)*2));
506 ret = false;
507 goto done;
509 buflen = (len+thistime)*2;
510 thistime2 = tdb_pack(
511 buffer+len, buflen-len, "f",
512 regsubkey_ctr_specific_key(ctr, i));
513 if (thistime2 != thistime) {
514 DEBUG(0, ("tdb_pack failed\n"));
515 ret = false;
516 goto done;
519 len += thistime;
522 /* finally write out the data */
524 dbuf.dptr = buffer;
525 dbuf.dsize = len;
526 status = dbwrap_store_bystring(regdb, keyname, dbuf, TDB_REPLACE);
527 if (!NT_STATUS_IS_OK(status)) {
528 ret = false;
529 goto done;
532 done:
533 TALLOC_FREE(ctx);
534 SAFE_FREE(buffer);
535 return ret;
538 /***********************************************************************
539 Store the new subkey record and create any child key records that
540 do not currently exist
541 ***********************************************************************/
543 bool regdb_store_keys(const char *key, REGSUBKEY_CTR *ctr)
545 int num_subkeys, i;
546 char *path = NULL;
547 REGSUBKEY_CTR *subkeys = NULL, *old_subkeys = NULL;
548 char *oldkeyname = NULL;
549 TALLOC_CTX *ctx = talloc_stackframe();
550 NTSTATUS status;
553 * fetch a list of the old subkeys so we can determine if anything has
554 * changed
557 if (!(old_subkeys = TALLOC_ZERO_P(ctx, REGSUBKEY_CTR))) {
558 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
559 return false;
562 regdb_fetch_keys(key, old_subkeys);
564 if ((ctr->num_subkeys && old_subkeys->num_subkeys) &&
565 (ctr->num_subkeys == old_subkeys->num_subkeys)) {
567 for (i = 0; i<ctr->num_subkeys; i++) {
568 if (strcmp(ctr->subkeys[i],
569 old_subkeys->subkeys[i]) != 0) {
570 break;
573 if (i == ctr->num_subkeys) {
575 * Nothing changed, no point to even start a tdb
576 * transaction
578 TALLOC_FREE(old_subkeys);
579 return true;
583 TALLOC_FREE(old_subkeys);
585 if (regdb->transaction_start(regdb) != 0) {
586 DEBUG(0, ("regdb_store_keys: transaction_start failed\n"));
587 goto fail;
591 * Re-fetch the old keys inside the transaction
594 if (!(old_subkeys = TALLOC_ZERO_P(ctx, REGSUBKEY_CTR))) {
595 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
596 goto cancel;
599 regdb_fetch_keys(key, old_subkeys);
601 /* store the subkey list for the parent */
603 if (!regdb_store_keys_internal(key, ctr) ) {
604 DEBUG(0,("regdb_store_keys: Failed to store new subkey list "
605 "for parent [%s]\n", key));
606 goto cancel;
609 /* now delete removed keys */
611 num_subkeys = regsubkey_ctr_numkeys(old_subkeys);
612 for (i=0; i<num_subkeys; i++) {
613 oldkeyname = regsubkey_ctr_specific_key(old_subkeys, i);
615 if (regsubkey_ctr_key_exists(ctr, oldkeyname)) {
617 * It's still around, don't delete
620 continue;
623 path = talloc_asprintf(ctx, "%s/%s", key, oldkeyname);
624 if (!path) {
625 goto cancel;
627 path = normalize_reg_path(ctx, path);
628 if (!path) {
629 goto cancel;
631 status = dbwrap_delete_bystring(regdb, path);
632 if (!NT_STATUS_IS_OK(status)) {
633 DEBUG(1, ("Deleting %s failed\n", path));
634 goto cancel;
637 TALLOC_FREE(path);
638 path = talloc_asprintf(ctx, "%s/%s/%s",
639 REG_VALUE_PREFIX,
640 key,
641 oldkeyname );
642 if (!path) {
643 goto cancel;
645 path = normalize_reg_path(ctx, path);
646 if (!path) {
647 goto cancel;
651 * Ignore errors here, we might have no values around
653 dbwrap_delete_bystring(regdb, path);
654 TALLOC_FREE(path);
657 TALLOC_FREE(old_subkeys);
659 /* now create records for any subkeys that don't already exist */
661 num_subkeys = regsubkey_ctr_numkeys(ctr);
663 if (num_subkeys == 0) {
664 if (!(subkeys = TALLOC_ZERO_P(ctx, REGSUBKEY_CTR)) ) {
665 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
666 goto cancel;
669 if (!regdb_store_keys_internal(key, subkeys)) {
670 DEBUG(0,("regdb_store_keys: Failed to store "
671 "new record for key [%s]\n", key));
672 goto cancel;
674 TALLOC_FREE(subkeys);
678 for (i=0; i<num_subkeys; i++) {
679 path = talloc_asprintf(ctx, "%s/%s",
680 key,
681 regsubkey_ctr_specific_key(ctr, i));
682 if (!path) {
683 goto cancel;
685 if (!(subkeys = TALLOC_ZERO_P(ctx, REGSUBKEY_CTR)) ) {
686 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
687 goto cancel;
690 if (regdb_fetch_keys( path, subkeys ) == -1) {
691 /* create a record with 0 subkeys */
692 if (!regdb_store_keys_internal(path, subkeys)) {
693 DEBUG(0,("regdb_store_keys: Failed to store "
694 "new record for key [%s]\n", path));
695 goto cancel;
699 TALLOC_FREE(subkeys);
700 TALLOC_FREE(path);
703 if (regdb->transaction_commit(regdb) != 0) {
704 DEBUG(0, ("regdb_store_keys: Could not commit transaction\n"));
705 goto fail;
708 TALLOC_FREE(ctx);
709 return true;
711 cancel:
712 if (regdb->transaction_cancel(regdb) != 0) {
713 smb_panic("regdb_store_keys: transaction_cancel failed\n");
716 fail:
717 TALLOC_FREE(ctx);
719 return false;
723 /***********************************************************************
724 Retrieve an array of strings containing subkeys. Memory should be
725 released by the caller.
726 ***********************************************************************/
728 int regdb_fetch_keys(const char *key, REGSUBKEY_CTR *ctr)
730 char *path = NULL;
731 uint32 num_items;
732 uint8 *buf;
733 uint32 buflen, len;
734 int i;
735 fstring subkeyname;
736 int ret = -1;
737 int dbret = -1;
738 TALLOC_CTX *frame = talloc_stackframe();
739 TDB_DATA value;
741 DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key ? key : "NULL"));
743 path = talloc_strdup(frame, key);
744 if (!path) {
745 goto fail;
748 /* convert to key format */
749 path = talloc_string_sub(frame, path, "\\", "/");
750 if (!path) {
751 goto fail;
753 strupper_m(path);
755 ctr->seqnum = regdb_get_seqnum();
757 dbret = regdb->fetch(regdb, frame, string_term_tdb_data(path), &value);
758 if (dbret != 0) {
759 goto fail;
762 buf = value.dptr;
763 buflen = value.dsize;
765 if ( !buf ) {
766 DEBUG(5,("regdb_fetch_keys: tdb lookup failed to locate key [%s]\n", key));
767 goto fail;
770 len = tdb_unpack( buf, buflen, "d", &num_items);
773 * The following code breaks the abstraction that reg_objects.c sets
774 * up with regsubkey_ctr_addkey(). But if we use that with the current
775 * data structure of ctr->subkeys being an unsorted array, we end up
776 * with an O(n^2) algorithm for retrieving keys from the tdb
777 * file. This is pretty pointless, as we have to trust the data
778 * structure on disk not to have duplicates anyway. The alternative to
779 * breaking this abstraction would be to set up a more sophisticated
780 * data structure in REGSUBKEY_CTR.
782 * This makes "net conf list" for a registry with >1000 shares
783 * actually usable :-)
786 ctr->subkeys = talloc_array(ctr, char *, num_items);
787 if (ctr->subkeys == NULL) {
788 DEBUG(5, ("regdb_fetch_keys: could not allocate subkeys\n"));
789 goto fail;
791 ctr->num_subkeys = num_items;
793 for (i=0; i<num_items; i++) {
794 len += tdb_unpack(buf+len, buflen-len, "f", subkeyname);
795 ctr->subkeys[i] = talloc_strdup(ctr->subkeys, subkeyname);
796 if (ctr->subkeys[i] == NULL) {
797 DEBUG(5, ("regdb_fetch_keys: could not allocate "
798 "subkeyname\n"));
799 TALLOC_FREE(ctr->subkeys);
800 ctr->num_subkeys = 0;
801 goto fail;
805 DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items));
807 ret = num_items;
808 fail:
809 TALLOC_FREE(frame);
810 return ret;
813 /****************************************************************************
814 Unpack a list of registry values frem the TDB
815 ***************************************************************************/
817 static int regdb_unpack_values(REGVAL_CTR *values, uint8 *buf, int buflen)
819 int len = 0;
820 uint32 type;
821 fstring valuename;
822 uint32 size;
823 uint8 *data_p;
824 uint32 num_values = 0;
825 int i;
827 /* loop and unpack the rest of the registry values */
829 len += tdb_unpack(buf+len, buflen-len, "d", &num_values);
831 for ( i=0; i<num_values; i++ ) {
832 /* unpack the next regval */
834 type = REG_NONE;
835 size = 0;
836 data_p = NULL;
837 valuename[0] = '\0';
838 len += tdb_unpack(buf+len, buflen-len, "fdB",
839 valuename,
840 &type,
841 &size,
842 &data_p);
844 /* add the new value. Paranoid protective code -- make sure data_p is valid */
846 if (*valuename && size && data_p) {
847 regval_ctr_addvalue(values, valuename, type,
848 (const char *)data_p, size);
850 SAFE_FREE(data_p); /* 'B' option to tdb_unpack does a malloc() */
852 DEBUG(8,("specific: [%s], len: %d\n", valuename, size));
855 return len;
858 /****************************************************************************
859 Pack all values in all printer keys
860 ***************************************************************************/
862 static int regdb_pack_values(REGVAL_CTR *values, uint8 *buf, int buflen)
864 int len = 0;
865 int i;
866 REGISTRY_VALUE *val;
867 int num_values;
869 if ( !values )
870 return 0;
872 num_values = regval_ctr_numvals( values );
874 /* pack the number of values first */
876 len += tdb_pack( buf+len, buflen-len, "d", num_values );
878 /* loop over all values */
880 for ( i=0; i<num_values; i++ ) {
881 val = regval_ctr_specific_value( values, i );
882 len += tdb_pack(buf+len, buflen-len, "fdB",
883 regval_name(val),
884 regval_type(val),
885 regval_size(val),
886 regval_data_p(val) );
889 return len;
892 /***********************************************************************
893 Retrieve an array of strings containing subkeys. Memory should be
894 released by the caller.
895 ***********************************************************************/
897 int regdb_fetch_values( const char* key, REGVAL_CTR *values )
899 char *keystr = NULL;
900 TALLOC_CTX *ctx = talloc_stackframe();
901 int ret = 0;
902 int dbret = -1;
903 TDB_DATA value;
905 DEBUG(10,("regdb_fetch_values: Looking for value of key [%s] \n", key));
907 keystr = talloc_asprintf(ctx, "%s/%s", REG_VALUE_PREFIX, key);
908 if (!keystr) {
909 return 0;
911 keystr = normalize_reg_path(ctx, keystr);
912 if (!keystr) {
913 goto done;
916 values->seqnum = regdb_get_seqnum();
918 dbret = regdb->fetch(regdb, ctx, string_term_tdb_data(keystr), &value);
919 if (dbret != 0) {
920 goto done;
923 if (!value.dptr) {
924 /* all keys have zero values by default */
925 goto done;
928 regdb_unpack_values(values, value.dptr, value.dsize);
929 ret = regval_ctr_numvals(values);
931 done:
932 TALLOC_FREE(ctx);
933 return ret;
936 bool regdb_store_values( const char *key, REGVAL_CTR *values )
938 TDB_DATA old_data, data;
939 char *keystr = NULL;
940 TALLOC_CTX *ctx = talloc_stackframe();
941 int len;
942 NTSTATUS status;
943 bool result = false;
945 DEBUG(10,("regdb_store_values: Looking for value of key [%s] \n", key));
947 ZERO_STRUCT(data);
949 len = regdb_pack_values(values, data.dptr, data.dsize);
950 if (len <= 0) {
951 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
952 goto done;
955 data.dptr = TALLOC_ARRAY(ctx, uint8, len);
956 data.dsize = len;
958 len = regdb_pack_values(values, data.dptr, data.dsize);
960 SMB_ASSERT( len == data.dsize );
962 keystr = talloc_asprintf(ctx, "%s/%s", REG_VALUE_PREFIX, key );
963 if (!keystr) {
964 goto done;
966 keystr = normalize_reg_path(ctx, keystr);
967 if (!keystr) {
968 goto done;
971 old_data = dbwrap_fetch_bystring(regdb, ctx, keystr);
973 if ((old_data.dptr != NULL)
974 && (old_data.dsize == data.dsize)
975 && (memcmp(old_data.dptr, data.dptr, data.dsize) == 0))
977 result = true;
978 goto done;
981 status = dbwrap_trans_store(regdb, string_term_tdb_data(keystr), data,
982 TDB_REPLACE);
984 result = NT_STATUS_IS_OK(status);
986 done:
987 TALLOC_FREE(ctx);
988 return result;
991 static WERROR regdb_get_secdesc(TALLOC_CTX *mem_ctx, const char *key,
992 struct security_descriptor **psecdesc)
994 char *tdbkey;
995 TDB_DATA data;
996 NTSTATUS status;
997 TALLOC_CTX *tmp_ctx = talloc_stackframe();
998 WERROR err = WERR_OK;
1000 DEBUG(10, ("regdb_get_secdesc: Getting secdesc of key [%s]\n", key));
1002 tdbkey = talloc_asprintf(tmp_ctx, "%s/%s", REG_SECDESC_PREFIX, key);
1003 if (tdbkey == NULL) {
1004 err = WERR_NOMEM;
1005 goto done;
1007 normalize_dbkey(tdbkey);
1009 data = dbwrap_fetch_bystring(regdb, tmp_ctx, tdbkey);
1010 if (data.dptr == NULL) {
1011 err = WERR_BADFILE;
1012 goto done;
1015 status = unmarshall_sec_desc(mem_ctx, (uint8 *)data.dptr, data.dsize,
1016 psecdesc);
1018 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MEMORY)) {
1019 err = WERR_NOMEM;
1020 } else if (!NT_STATUS_IS_OK(status)) {
1021 err = WERR_REG_CORRUPT;
1024 done:
1025 TALLOC_FREE(tmp_ctx);
1026 return err;
1029 static WERROR regdb_set_secdesc(const char *key,
1030 struct security_descriptor *secdesc)
1032 TALLOC_CTX *mem_ctx = talloc_stackframe();
1033 char *tdbkey;
1034 NTSTATUS status;
1035 WERROR err = WERR_NOMEM;
1036 TDB_DATA tdbdata;
1038 tdbkey = talloc_asprintf(mem_ctx, "%s/%s", REG_SECDESC_PREFIX, key);
1039 if (tdbkey == NULL) {
1040 goto done;
1042 normalize_dbkey(tdbkey);
1044 if (secdesc == NULL) {
1045 /* assuming a delete */
1046 status = dbwrap_trans_delete(regdb,
1047 string_term_tdb_data(tdbkey));
1048 if (NT_STATUS_IS_OK(status)) {
1049 err = WERR_OK;
1050 } else {
1051 err = ntstatus_to_werror(status);
1053 goto done;
1056 err = ntstatus_to_werror(marshall_sec_desc(mem_ctx, secdesc,
1057 &tdbdata.dptr,
1058 &tdbdata.dsize));
1059 if (!W_ERROR_IS_OK(err)) {
1060 goto done;
1063 status = dbwrap_trans_store(regdb, string_term_tdb_data(tdbkey),
1064 tdbdata, 0);
1065 if (!NT_STATUS_IS_OK(status)) {
1066 err = ntstatus_to_werror(status);
1067 goto done;
1070 done:
1071 TALLOC_FREE(mem_ctx);
1072 return err;
1075 bool regdb_subkeys_need_update(REGSUBKEY_CTR *subkeys)
1077 return (regdb_get_seqnum() != subkeys->seqnum);
1080 bool regdb_values_need_update(REGVAL_CTR *values)
1082 return (regdb_get_seqnum() != values->seqnum);
1086 * Table of function pointers for default access
1089 REGISTRY_OPS regdb_ops = {
1090 .fetch_subkeys = regdb_fetch_keys,
1091 .fetch_values = regdb_fetch_values,
1092 .store_subkeys = regdb_store_keys,
1093 .store_values = regdb_store_values,
1094 .get_secdesc = regdb_get_secdesc,
1095 .set_secdesc = regdb_set_secdesc,
1096 .subkeys_need_update = regdb_subkeys_need_update,
1097 .values_need_update = regdb_values_need_update