regdb: use db_open_trans()
[Samba/gbeck.git] / source / registry / reg_backend_db.c
blobbbd783b3874305eae95b277ba174462dfe589716
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 bool init_registry_key_internal(const char *add_path)
88 bool ret = false;
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 goto fail;
105 p = path;
107 while (next_token_talloc(frame, &p, &keyname, "\\")) {
109 /* build up the registry path from the components */
111 if (*base) {
112 base = talloc_asprintf(frame, "%s\\", base);
113 if (!base) {
114 goto fail;
117 base = talloc_asprintf_append(base, "%s", keyname);
118 if (!base) {
119 goto fail;
122 /* get the immediate subkeyname (if we have one ) */
124 subkeyname = talloc_strdup(frame, "");
125 if (!subkeyname) {
126 goto fail;
128 if (*p) {
129 remaining = talloc_strdup(frame, p);
130 if (!remaining) {
131 goto fail;
133 p2 = remaining;
135 if (!next_token_talloc(frame, &p2,
136 &subkeyname, "\\"))
138 subkeyname = talloc_strdup(frame,p2);
139 if (!subkeyname) {
140 goto fail;
145 DEBUG(10,("init_registry_key: Storing key [%s] with "
146 "subkey [%s]\n", base,
147 *subkeyname ? subkeyname : "NULL"));
149 /* we don't really care if the lookup succeeds or not
150 * since we are about to update the record.
151 * We just want any subkeys already present */
153 if (!(subkeys = TALLOC_ZERO_P(frame, REGSUBKEY_CTR))) {
154 DEBUG(0,("talloc() failure!\n"));
155 goto fail;
158 regdb_fetch_keys(base, subkeys);
159 if (*subkeyname) {
160 regsubkey_ctr_addkey( subkeys, subkeyname);
162 if (!regdb_store_keys( base, subkeys)) {
163 goto fail;
167 ret = true;
168 fail:
169 TALLOC_FREE(frame);
170 return ret;
174 * Initialize a key in the registry:
175 * create each component key of the specified path,
176 * wrapped in one db transaction.
178 bool init_registry_key(const char *add_path)
180 if (regdb->transaction_start(regdb) != 0) {
181 DEBUG(0, ("init_registry_key: transaction_start failed\n"));
182 return false;
185 if (!init_registry_key_internal(add_path)) {
186 goto fail;
189 if (regdb->transaction_commit(regdb) == -1) {
190 DEBUG(0, ("init_registry_key: Could not commit transaction\n"));
191 return false;
194 return true;
196 fail:
197 if (regdb->transaction_cancel(regdb) == -1) {
198 smb_panic("init_registry_key: transaction_cancel failed\n");
201 return false;
204 /***********************************************************************
205 Open the registry data in the tdb
206 ***********************************************************************/
208 bool init_registry_data(void)
210 TALLOC_CTX *frame = NULL;
211 REGVAL_CTR *values;
212 int i;
213 UNISTR2 data;
216 * There are potentially quite a few store operations which are all
217 * indiviually wrapped in tdb transactions. Wrapping them in a single
218 * transaction gives just a single transaction_commit() to actually do
219 * its fsync()s. See tdb/common/transaction.c for info about nested
220 * transaction behaviour.
223 if (regdb->transaction_start(regdb) != 0) {
224 DEBUG(0, ("init_registry_data: tdb_transaction_start "
225 "failed\n"));
226 return false;
229 /* loop over all of the predefined paths and add each component */
231 for (i=0; builtin_registry_paths[i] != NULL; i++) {
232 if (!init_registry_key_internal(builtin_registry_paths[i])) {
233 goto fail;
237 /* loop over all of the predefined values and add each component */
239 frame = talloc_stackframe();
241 for (i=0; builtin_registry_values[i].path != NULL; i++) {
243 values = TALLOC_ZERO_P(frame, REGVAL_CTR);
244 if (values == NULL) {
245 goto fail;
248 regdb_fetch_values(builtin_registry_values[i].path, values);
250 /* preserve existing values across restarts. Only add new ones */
252 if (!regval_ctr_key_exists(values,
253 builtin_registry_values[i].valuename))
255 switch(builtin_registry_values[i].type) {
256 case REG_DWORD:
257 regval_ctr_addvalue(values,
258 builtin_registry_values[i].valuename,
259 REG_DWORD,
260 (char*)&builtin_registry_values[i].data.dw_value,
261 sizeof(uint32));
262 break;
264 case REG_SZ:
265 init_unistr2(&data,
266 builtin_registry_values[i].data.string,
267 UNI_STR_TERMINATE);
268 regval_ctr_addvalue(values,
269 builtin_registry_values[i].valuename,
270 REG_SZ,
271 (char*)data.buffer,
272 data.uni_str_len*sizeof(uint16));
273 break;
275 default:
276 DEBUG(0, ("init_registry_data: invalid value "
277 "type in builtin_registry_values "
278 "[%d]\n",
279 builtin_registry_values[i].type));
281 regdb_store_values(builtin_registry_values[i].path,
282 values);
284 TALLOC_FREE(values);
287 TALLOC_FREE(frame);
289 if (regdb->transaction_commit(regdb) == -1) {
290 DEBUG(0, ("init_registry_data: Could not commit "
291 "transaction\n"));
292 return false;
295 return true;
297 fail:
299 TALLOC_FREE(frame);
301 if (regdb->transaction_cancel(regdb) == -1) {
302 smb_panic("init_registry_data: tdb_transaction_cancel "
303 "failed\n");
306 return false;
309 /***********************************************************************
310 Open the registry database
311 ***********************************************************************/
313 bool regdb_init(void)
315 const char *vstring = "INFO/version";
316 uint32 vers_id;
318 if (regdb) {
319 DEBUG(10, ("regdb_init: incrementing refcount (%d)\n",
320 regdb_refcount));
321 regdb_refcount++;
322 return true;
325 regdb = db_open_trans(NULL, state_path("registry.tdb"), 0,
326 REG_TDB_FLAGS, O_RDWR, 0600);
327 if (!regdb) {
328 regdb = db_open_trans(NULL, state_path("registry.tdb"), 0,
329 REG_TDB_FLAGS, O_RDWR|O_CREAT, 0600);
330 if (!regdb) {
331 DEBUG(0,("regdb_init: Failed to open registry %s (%s)\n",
332 state_path("registry.tdb"), strerror(errno) ));
333 return false;
336 DEBUG(10,("regdb_init: Successfully created registry tdb\n"));
339 regdb_refcount = 1;
341 vers_id = dbwrap_fetch_int32(regdb, vstring);
343 if ( vers_id != REGVER_V1 ) {
344 /* any upgrade code here if needed */
345 DEBUG(10, ("regdb_init: got %s = %d != %d\n", vstring,
346 vers_id, REGVER_V1));
347 if (dbwrap_trans_store_int32(regdb, vstring, REGVER_V1) != 0) {
348 DEBUG(0, ("regdb_init: error storing %s = %d\n",
349 vstring, REGVER_V1));
350 return false;
351 } else {
352 DEBUG(10, ("regdb_init: stored %s = %d\n",
353 vstring, REGVER_V1));
357 return true;
360 /***********************************************************************
361 Open the registry. Must already have been initialized by regdb_init()
362 ***********************************************************************/
364 WERROR regdb_open( void )
366 WERROR result = WERR_OK;
368 if ( regdb ) {
369 DEBUG(10,("regdb_open: incrementing refcount (%d)\n", regdb_refcount));
370 regdb_refcount++;
371 return WERR_OK;
374 become_root();
376 regdb = db_open_trans(NULL, state_path("registry.tdb"), 0,
377 REG_TDB_FLAGS, O_RDWR, 0600);
378 if ( !regdb ) {
379 result = ntstatus_to_werror( map_nt_error_from_unix( errno ) );
380 DEBUG(0,("regdb_open: Failed to open %s! (%s)\n",
381 state_path("registry.tdb"), strerror(errno) ));
384 unbecome_root();
386 regdb_refcount = 1;
387 DEBUG(10,("regdb_open: refcount reset (%d)\n", regdb_refcount));
389 return result;
392 /***********************************************************************
393 ***********************************************************************/
395 int regdb_close( void )
397 if (regdb_refcount == 0) {
398 return 0;
401 regdb_refcount--;
403 DEBUG(10,("regdb_close: decrementing refcount (%d)\n", regdb_refcount));
405 if ( regdb_refcount > 0 )
406 return 0;
408 SMB_ASSERT( regdb_refcount >= 0 );
410 TALLOC_FREE(regdb);
411 return 0;
414 /***********************************************************************
415 return the tdb sequence number of the registry tdb.
416 this is an indicator for the content of the registry
417 having changed. it will change upon regdb_init, too, though.
418 ***********************************************************************/
419 int regdb_get_seqnum(void)
421 return regdb->get_seqnum(regdb);
424 /***********************************************************************
425 Add subkey strings to the registry tdb under a defined key
426 fmt is the same format as tdb_pack except this function only supports
427 fstrings
428 ***********************************************************************/
430 static bool regdb_store_keys_internal(const char *key, REGSUBKEY_CTR *ctr)
432 TDB_DATA dbuf;
433 uint8 *buffer = NULL;
434 int i = 0;
435 uint32 len, buflen;
436 bool ret = true;
437 uint32 num_subkeys = regsubkey_ctr_numkeys(ctr);
438 char *keyname = NULL;
439 TALLOC_CTX *ctx = talloc_stackframe();
440 NTSTATUS status;
442 if (!key) {
443 return false;
446 keyname = talloc_strdup(ctx, key);
447 if (!keyname) {
448 return false;
450 keyname = normalize_reg_path(ctx, keyname);
452 /* allocate some initial memory */
454 buffer = (uint8 *)SMB_MALLOC(1024);
455 if (buffer == NULL) {
456 return false;
458 buflen = 1024;
459 len = 0;
461 /* store the number of subkeys */
463 len += tdb_pack(buffer+len, buflen-len, "d", num_subkeys);
465 /* pack all the strings */
467 for (i=0; i<num_subkeys; i++) {
468 len += tdb_pack(buffer+len, buflen-len, "f",
469 regsubkey_ctr_specific_key(ctr, i));
470 if (len > buflen) {
471 /* allocate some extra space */
472 buffer = (uint8 *)SMB_REALLOC(buffer, len*2);
473 if(buffer == NULL) {
474 DEBUG(0, ("regdb_store_keys: Failed to realloc "
475 "memory of size [%d]\n", len*2));
476 ret = false;
477 goto done;
479 buflen = len*2;
480 len = tdb_pack(buffer+len, buflen-len, "f",
481 regsubkey_ctr_specific_key(ctr, i));
485 /* finally write out the data */
487 dbuf.dptr = buffer;
488 dbuf.dsize = len;
489 status = dbwrap_store_bystring(regdb, keyname, dbuf, TDB_REPLACE);
490 if (!NT_STATUS_IS_OK(status)) {
491 ret = false;
492 goto done;
495 done:
496 TALLOC_FREE(ctx);
497 SAFE_FREE(buffer);
498 return ret;
501 /***********************************************************************
502 Store the new subkey record and create any child key records that
503 do not currently exist
504 ***********************************************************************/
506 bool regdb_store_keys(const char *key, REGSUBKEY_CTR *ctr)
508 int num_subkeys, i;
509 char *path = NULL;
510 REGSUBKEY_CTR *subkeys = NULL, *old_subkeys = NULL;
511 char *oldkeyname = NULL;
512 TALLOC_CTX *ctx = talloc_stackframe();
513 NTSTATUS status;
516 * fetch a list of the old subkeys so we can determine if anything has
517 * changed
520 if (!(old_subkeys = TALLOC_ZERO_P(ctx, REGSUBKEY_CTR))) {
521 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
522 return false;
525 regdb_fetch_keys(key, old_subkeys);
527 if ((ctr->num_subkeys && old_subkeys->num_subkeys) &&
528 (ctr->num_subkeys == old_subkeys->num_subkeys)) {
530 for (i = 0; i<ctr->num_subkeys; i++) {
531 if (strcmp(ctr->subkeys[i],
532 old_subkeys->subkeys[i]) != 0) {
533 break;
536 if (i == ctr->num_subkeys) {
538 * Nothing changed, no point to even start a tdb
539 * transaction
541 TALLOC_FREE(old_subkeys);
542 return true;
546 TALLOC_FREE(old_subkeys);
548 if (regdb->transaction_start(regdb) != 0) {
549 DEBUG(0, ("regdb_store_keys: transaction_start failed\n"));
550 goto fail;
554 * Re-fetch the old keys inside the transaction
557 if (!(old_subkeys = TALLOC_ZERO_P(ctx, REGSUBKEY_CTR))) {
558 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
559 goto cancel;
562 regdb_fetch_keys(key, old_subkeys);
564 /* store the subkey list for the parent */
566 if (!regdb_store_keys_internal(key, ctr) ) {
567 DEBUG(0,("regdb_store_keys: Failed to store new subkey list "
568 "for parent [%s]\n", key));
569 goto cancel;
572 /* now delete removed keys */
574 num_subkeys = regsubkey_ctr_numkeys(old_subkeys);
575 for (i=0; i<num_subkeys; i++) {
576 oldkeyname = regsubkey_ctr_specific_key(old_subkeys, i);
578 if (regsubkey_ctr_key_exists(ctr, oldkeyname)) {
580 * It's still around, don't delete
583 continue;
586 path = talloc_asprintf(ctx, "%s/%s", key, oldkeyname);
587 if (!path) {
588 goto cancel;
590 path = normalize_reg_path(ctx, path);
591 if (!path) {
592 goto cancel;
594 status = dbwrap_delete_bystring(regdb, path);
595 if (!NT_STATUS_IS_OK(status)) {
596 DEBUG(1, ("Deleting %s failed\n", path));
597 goto cancel;
600 TALLOC_FREE(path);
601 path = talloc_asprintf(ctx, "%s/%s/%s",
602 REG_VALUE_PREFIX,
603 key,
604 oldkeyname );
605 if (!path) {
606 goto cancel;
608 path = normalize_reg_path(ctx, path);
609 if (!path) {
610 goto cancel;
614 * Ignore errors here, we might have no values around
616 dbwrap_delete_bystring(regdb, path);
617 TALLOC_FREE(path);
620 TALLOC_FREE(old_subkeys);
622 /* now create records for any subkeys that don't already exist */
624 num_subkeys = regsubkey_ctr_numkeys(ctr);
626 if (num_subkeys == 0) {
627 if (!(subkeys = TALLOC_ZERO_P(ctx, REGSUBKEY_CTR)) ) {
628 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
629 goto cancel;
632 if (!regdb_store_keys_internal(key, subkeys)) {
633 DEBUG(0,("regdb_store_keys: Failed to store "
634 "new record for key [%s]\n", key));
635 goto cancel;
637 TALLOC_FREE(subkeys);
641 for (i=0; i<num_subkeys; i++) {
642 path = talloc_asprintf(ctx, "%s/%s",
643 key,
644 regsubkey_ctr_specific_key(ctr, i));
645 if (!path) {
646 goto cancel;
648 if (!(subkeys = TALLOC_ZERO_P(ctx, REGSUBKEY_CTR)) ) {
649 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
650 goto cancel;
653 if (regdb_fetch_keys( path, subkeys ) == -1) {
654 /* create a record with 0 subkeys */
655 if (!regdb_store_keys_internal(path, subkeys)) {
656 DEBUG(0,("regdb_store_keys: Failed to store "
657 "new record for key [%s]\n", path));
658 goto cancel;
662 TALLOC_FREE(subkeys);
663 TALLOC_FREE(path);
666 if (regdb->transaction_commit(regdb) == -1) {
667 DEBUG(0, ("regdb_store_keys: Could not commit transaction\n"));
668 goto fail;
671 TALLOC_FREE(ctx);
672 return true;
674 cancel:
675 if (regdb->transaction_cancel(regdb) == -1) {
676 smb_panic("regdb_store_keys: transaction_cancel failed\n");
679 fail:
680 TALLOC_FREE(ctx);
682 return false;
686 /***********************************************************************
687 Retrieve an array of strings containing subkeys. Memory should be
688 released by the caller.
689 ***********************************************************************/
691 int regdb_fetch_keys(const char *key, REGSUBKEY_CTR *ctr)
693 char *path = NULL;
694 uint32 num_items;
695 uint8 *buf;
696 uint32 buflen, len;
697 int i;
698 fstring subkeyname;
699 int ret = -1;
700 TALLOC_CTX *frame = talloc_stackframe();
701 TDB_DATA value;
703 DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key ? key : "NULL"));
705 path = talloc_strdup(frame, key);
706 if (!path) {
707 goto fail;
710 /* convert to key format */
711 path = talloc_string_sub(frame, path, "\\", "/");
712 if (!path) {
713 goto fail;
715 strupper_m(path);
717 ret = regdb->fetch(regdb, frame, string_term_tdb_data(path), &value);
718 if (ret != 0) {
719 ret = 0;
720 goto fail;
723 ctr->seqnum = regdb_get_seqnum();
725 buf = value.dptr;
726 buflen = value.dsize;
728 if ( !buf ) {
729 DEBUG(5,("regdb_fetch_keys: tdb lookup failed to locate key [%s]\n", key));
730 goto fail;
733 len = tdb_unpack( buf, buflen, "d", &num_items);
735 for (i=0; i<num_items; i++) {
736 len += tdb_unpack(buf+len, buflen-len, "f", subkeyname);
737 regsubkey_ctr_addkey(ctr, subkeyname);
740 DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items));
742 ret = num_items;
743 fail:
744 TALLOC_FREE(frame);
745 return ret;
748 /****************************************************************************
749 Unpack a list of registry values frem the TDB
750 ***************************************************************************/
752 static int regdb_unpack_values(REGVAL_CTR *values, uint8 *buf, int buflen)
754 int len = 0;
755 uint32 type;
756 fstring valuename;
757 uint32 size;
758 uint8 *data_p;
759 uint32 num_values = 0;
760 int i;
762 /* loop and unpack the rest of the registry values */
764 len += tdb_unpack(buf+len, buflen-len, "d", &num_values);
766 for ( i=0; i<num_values; i++ ) {
767 /* unpack the next regval */
769 type = REG_NONE;
770 size = 0;
771 data_p = NULL;
772 valuename[0] = '\0';
773 len += tdb_unpack(buf+len, buflen-len, "fdB",
774 valuename,
775 &type,
776 &size,
777 &data_p);
779 /* add the new value. Paranoid protective code -- make sure data_p is valid */
781 if (*valuename && size && data_p) {
782 regval_ctr_addvalue(values, valuename, type,
783 (const char *)data_p, size);
785 SAFE_FREE(data_p); /* 'B' option to tdb_unpack does a malloc() */
787 DEBUG(8,("specific: [%s], len: %d\n", valuename, size));
790 return len;
793 /****************************************************************************
794 Pack all values in all printer keys
795 ***************************************************************************/
797 static int regdb_pack_values(REGVAL_CTR *values, uint8 *buf, int buflen)
799 int len = 0;
800 int i;
801 REGISTRY_VALUE *val;
802 int num_values;
804 if ( !values )
805 return 0;
807 num_values = regval_ctr_numvals( values );
809 /* pack the number of values first */
811 len += tdb_pack( buf+len, buflen-len, "d", num_values );
813 /* loop over all values */
815 for ( i=0; i<num_values; i++ ) {
816 val = regval_ctr_specific_value( values, i );
817 len += tdb_pack(buf+len, buflen-len, "fdB",
818 regval_name(val),
819 regval_type(val),
820 regval_size(val),
821 regval_data_p(val) );
824 return len;
827 /***********************************************************************
828 Retrieve an array of strings containing subkeys. Memory should be
829 released by the caller.
830 ***********************************************************************/
832 int regdb_fetch_values( const char* key, REGVAL_CTR *values )
834 char *keystr = NULL;
835 TALLOC_CTX *ctx = talloc_stackframe();
836 int ret = 0;
837 TDB_DATA value;
839 DEBUG(10,("regdb_fetch_values: Looking for value of key [%s] \n", key));
841 keystr = talloc_asprintf(ctx, "%s/%s", REG_VALUE_PREFIX, key);
842 if (!keystr) {
843 return 0;
845 keystr = normalize_reg_path(ctx, keystr);
846 if (!keystr) {
847 goto done;
850 ret = regdb->fetch(regdb, ctx, string_term_tdb_data(keystr), &value);
851 if (ret != 0) {
852 ret = 0;
853 goto done;
856 values->seqnum = regdb_get_seqnum();
858 if (!value.dptr) {
859 /* all keys have zero values by default */
860 goto done;
863 regdb_unpack_values(values, value.dptr, value.dsize);
864 ret = regval_ctr_numvals(values);
866 done:
867 TALLOC_FREE(ctx);
868 return ret;
871 bool regdb_store_values( const char *key, REGVAL_CTR *values )
873 TDB_DATA old_data, data;
874 char *keystr = NULL;
875 TALLOC_CTX *ctx = talloc_stackframe();
876 int len, ret;
877 bool result = false;
879 DEBUG(10,("regdb_store_values: Looking for value of key [%s] \n", key));
881 ZERO_STRUCT(data);
883 len = regdb_pack_values(values, data.dptr, data.dsize);
884 if (len <= 0) {
885 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
886 goto done;
889 data.dptr = TALLOC_ARRAY(ctx, uint8, len);
890 data.dsize = len;
892 len = regdb_pack_values(values, data.dptr, data.dsize);
894 SMB_ASSERT( len == data.dsize );
896 keystr = talloc_asprintf(ctx, "%s/%s", REG_VALUE_PREFIX, key );
897 if (!keystr) {
898 goto done;
900 keystr = normalize_reg_path(ctx, keystr);
901 if (!keystr) {
902 goto done;
905 old_data = dbwrap_fetch_bystring(regdb, ctx, keystr);
907 if ((old_data.dptr != NULL)
908 && (old_data.dsize == data.dsize)
909 && (memcmp(old_data.dptr, data.dptr, data.dsize) == 0))
911 result = true;
912 goto done;
915 ret = dbwrap_trans_store(regdb, string_term_tdb_data(keystr), data,
916 TDB_REPLACE);
917 result = (ret != -1);
919 done:
920 TALLOC_FREE(ctx);
921 return result;
924 static WERROR regdb_get_secdesc(TALLOC_CTX *mem_ctx, const char *key,
925 struct security_descriptor **psecdesc)
927 char *tdbkey;
928 TDB_DATA data;
929 NTSTATUS status;
930 TALLOC_CTX *tmp_ctx = talloc_stackframe();
931 WERROR err = WERR_OK;
933 DEBUG(10, ("regdb_get_secdesc: Getting secdesc of key [%s]\n", key));
935 tdbkey = talloc_asprintf(tmp_ctx, "%s/%s", REG_SECDESC_PREFIX, key);
936 if (tdbkey == NULL) {
937 err = WERR_NOMEM;
938 goto done;
940 normalize_dbkey(tdbkey);
942 data = dbwrap_fetch_bystring(regdb, tmp_ctx, tdbkey);
943 if (data.dptr == NULL) {
944 err = WERR_BADFILE;
945 goto done;
948 status = unmarshall_sec_desc(mem_ctx, (uint8 *)data.dptr, data.dsize,
949 psecdesc);
951 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MEMORY)) {
952 err = WERR_NOMEM;
953 } else if (!NT_STATUS_IS_OK(status)) {
954 err = WERR_REG_CORRUPT;
957 done:
958 TALLOC_FREE(tmp_ctx);
959 return err;
962 static WERROR regdb_set_secdesc(const char *key,
963 struct security_descriptor *secdesc)
965 TALLOC_CTX *mem_ctx = talloc_stackframe();
966 char *tdbkey;
967 WERROR err = WERR_NOMEM;
968 TDB_DATA tdbdata;
969 int tdb_ret;
971 tdbkey = talloc_asprintf(mem_ctx, "%s/%s", REG_SECDESC_PREFIX, key);
972 if (tdbkey == NULL) {
973 goto done;
975 normalize_dbkey(tdbkey);
977 if (secdesc == NULL) {
978 /* assuming a delete */
979 tdb_ret = dbwrap_trans_delete(regdb,
980 string_term_tdb_data(tdbkey));
981 if (tdb_ret == -1) {
982 err = ntstatus_to_werror(map_nt_error_from_unix(errno));
983 } else {
984 err = WERR_OK;
986 goto done;
989 err = ntstatus_to_werror(marshall_sec_desc(mem_ctx, secdesc,
990 &tdbdata.dptr,
991 &tdbdata.dsize));
992 if (!W_ERROR_IS_OK(err)) {
993 goto done;
996 tdb_ret = dbwrap_trans_store(regdb, string_term_tdb_data(tdbkey),
997 tdbdata, 0);
998 if (tdb_ret == -1) {
999 err = ntstatus_to_werror(map_nt_error_from_unix(errno));
1000 goto done;
1003 done:
1004 TALLOC_FREE(mem_ctx);
1005 return err;
1008 bool regdb_subkeys_need_update(REGSUBKEY_CTR *subkeys)
1010 return (regdb_get_seqnum() != subkeys->seqnum);
1013 bool regdb_values_need_update(REGVAL_CTR *values)
1015 return (regdb_get_seqnum() != values->seqnum);
1019 * Table of function pointers for default access
1022 REGISTRY_OPS regdb_ops = {
1023 .fetch_subkeys = regdb_fetch_keys,
1024 .fetch_values = regdb_fetch_values,
1025 .store_subkeys = regdb_store_keys,
1026 .store_values = regdb_store_values,
1027 .get_secdesc = regdb_get_secdesc,
1028 .set_secdesc = regdb_set_secdesc,
1029 .subkeys_need_update = regdb_subkeys_need_update,
1030 .values_need_update = regdb_values_need_update