Fix build for pam_smbpass
[Samba.git] / source / registry / reg_db.c
blobc4bfc2b6c9c9c7c51b19ba629cab3f199d67a436
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 tdb_wrap *tdb_reg = NULL;
28 static int tdb_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 "HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Perflib",
48 "HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Perflib\\009",
49 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Print\\Monitors",
50 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\ProductOptions",
51 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\DefaultUserConfiguration",
52 "HKLM\\SYSTEM\\CurrentControlSet\\Services\\TcpIp\\Parameters",
53 "HKLM\\SYSTEM\\CurrentControlSet\\Services\\Netlogon\\Parameters",
54 "HKU",
55 "HKCR",
56 "HKPD",
57 "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 Open the registry data in the tdb
84 ***********************************************************************/
86 static bool init_registry_data( void )
88 char *path = NULL;
89 char *base = NULL;
90 char *remaining = NULL;
91 TALLOC_CTX *frame = NULL;
92 char *keyname;
93 char *subkeyname;
94 REGSUBKEY_CTR *subkeys;
95 REGVAL_CTR *values;
96 int i;
97 const char *p, *p2;
98 UNISTR2 data;
101 * There are potentially quite a few store operations which are all
102 * indiviually wrapped in tdb transactions. Wrapping them in a single
103 * transaction gives just a single transaction_commit() to actually do
104 * its fsync()s. See tdb/common/transaction.c for info about nested
105 * transaction behaviour.
108 if ( tdb_transaction_start( tdb_reg->tdb ) == -1 ) {
109 DEBUG(0, ("init_registry_data: tdb_transaction_start "
110 "failed\n"));
111 return false;
114 /* loop over all of the predefined paths and add each component */
116 for ( i=0; builtin_registry_paths[i] != NULL; i++ ) {
118 frame = talloc_stackframe();
120 DEBUG(6,("init_registry_data: Adding [%s]\n", builtin_registry_paths[i]));
122 path = talloc_strdup(talloc_tos(), builtin_registry_paths[i]);
123 base = talloc_strdup(talloc_tos(), "");
124 if (!path || !base) {
125 goto fail;
127 p = path;
129 while (next_token_talloc(talloc_tos(), &p, &keyname, "\\")) {
131 /* build up the registry path from the components */
133 if (*base) {
134 base = talloc_asprintf(talloc_tos(), "%s\\", base);
135 if (!base) {
136 goto fail;
139 base = talloc_asprintf_append(base, "%s", keyname);
140 if (!base) {
141 goto fail;
144 /* get the immediate subkeyname (if we have one ) */
146 subkeyname = talloc_strdup(talloc_tos(), "");
147 if (!subkeyname) {
148 goto fail;
150 if (*p) {
151 remaining = talloc_strdup(talloc_tos(), p);
152 if (!remaining) {
153 goto fail;
155 p2 = remaining;
157 if (!next_token_talloc(talloc_tos(), &p2,
158 &subkeyname, "\\")) {
159 subkeyname = talloc_strdup(talloc_tos(),p2);
160 if (!subkeyname) {
161 goto fail;
166 DEBUG(10,("init_registry_data: Storing key [%s] with subkey [%s]\n",
167 base, *subkeyname ? subkeyname : "NULL"));
169 /* we don't really care if the lookup succeeds or not since
170 we are about to update the record. We just want any
171 subkeys already present */
173 if ( !(subkeys = TALLOC_ZERO_P(talloc_tos(), REGSUBKEY_CTR )) ) {
174 DEBUG(0,("talloc() failure!\n"));
175 goto fail;
178 regdb_fetch_keys(base, subkeys);
179 if (*subkeyname) {
180 regsubkey_ctr_addkey( subkeys, subkeyname);
182 if (!regdb_store_keys( base, subkeys)) {
183 goto fail;
187 TALLOC_FREE(frame);
190 /* loop over all of the predefined values and add each component */
192 for (i=0; builtin_registry_values[i].path != NULL; i++) {
194 if (!(values = TALLOC_ZERO_P(talloc_tos(), REGVAL_CTR))) {
195 goto fail;
198 regdb_fetch_values( builtin_registry_values[i].path, values);
200 /* preserve existing values across restarts. Only add new ones */
202 if (!regval_ctr_key_exists(values, builtin_registry_values[i].valuename)) {
203 switch(builtin_registry_values[i].type) {
204 case REG_DWORD:
205 regval_ctr_addvalue( values,
206 builtin_registry_values[i].valuename,
207 REG_DWORD,
208 (char*)&builtin_registry_values[i].data.dw_value,
209 sizeof(uint32) );
210 break;
212 case REG_SZ:
213 init_unistr2( &data, builtin_registry_values[i].data.string, UNI_STR_TERMINATE);
214 regval_ctr_addvalue( values,
215 builtin_registry_values[i].valuename,
216 REG_SZ,
217 (char*)data.buffer,
218 data.uni_str_len*sizeof(uint16) );
219 break;
221 default:
222 DEBUG(0,("init_registry_data: invalid value type in builtin_registry_values [%d]\n",
223 builtin_registry_values[i].type));
225 regdb_store_values( builtin_registry_values[i].path, values );
227 TALLOC_FREE( values );
230 TALLOC_FREE(frame);
232 if (tdb_transaction_commit( tdb_reg->tdb ) == -1) {
233 DEBUG(0, ("init_registry_data: Could not commit "
234 "transaction\n"));
235 return false;
238 return true;
240 fail:
242 TALLOC_FREE(frame);
244 if (tdb_transaction_cancel( tdb_reg->tdb ) == -1) {
245 smb_panic("init_registry_data: tdb_transaction_cancel "
246 "failed\n");
249 return false;
252 /***********************************************************************
253 Open the registry database
254 ***********************************************************************/
256 bool regdb_init( void )
258 const char *vstring = "INFO/version";
259 uint32 vers_id;
261 if ( tdb_reg )
262 return true;
264 if ( !(tdb_reg = tdb_wrap_open(NULL, state_path("registry.tdb"), 0, REG_TDB_FLAGS, O_RDWR, 0600)) )
266 tdb_reg = tdb_wrap_open(NULL, state_path("registry.tdb"), 0, REG_TDB_FLAGS, O_RDWR|O_CREAT, 0600);
267 if ( !tdb_reg ) {
268 DEBUG(0,("regdb_init: Failed to open registry %s (%s)\n",
269 state_path("registry.tdb"), strerror(errno) ));
270 return false;
273 DEBUG(10,("regdb_init: Successfully created registry tdb\n"));
276 tdb_refcount = 1;
278 vers_id = tdb_fetch_int32(tdb_reg->tdb, vstring);
280 if ( vers_id != REGVER_V1 ) {
281 /* any upgrade code here if needed */
282 DEBUG(10, ("regdb_init: got INFO/version = %d != %d\n",
283 vers_id, REGVER_V1));
286 /* always setup the necessary keys and values */
288 if ( !init_registry_data() ) {
289 DEBUG(0,("regdb_init: Failed to initialize data in registry!\n"));
290 return false;
293 return true;
296 /***********************************************************************
297 Open the registry. Must already have been initialized by regdb_init()
298 ***********************************************************************/
300 WERROR regdb_open( void )
302 WERROR result = WERR_OK;
304 if ( tdb_reg ) {
305 DEBUG(10,("regdb_open: incrementing refcount (%d)\n", tdb_refcount));
306 tdb_refcount++;
307 return WERR_OK;
310 become_root();
312 tdb_reg = tdb_wrap_open(NULL, state_path("registry.tdb"), 0, REG_TDB_FLAGS, O_RDWR, 0600);
313 if ( !tdb_reg ) {
314 result = ntstatus_to_werror( map_nt_error_from_unix( errno ) );
315 DEBUG(0,("regdb_open: Failed to open %s! (%s)\n",
316 state_path("registry.tdb"), strerror(errno) ));
319 unbecome_root();
321 tdb_refcount = 1;
322 DEBUG(10,("regdb_open: refcount reset (%d)\n", tdb_refcount));
324 return result;
327 /***********************************************************************
328 ***********************************************************************/
330 int regdb_close( void )
332 if (tdb_refcount == 0) {
333 return 0;
336 tdb_refcount--;
338 DEBUG(10,("regdb_close: decrementing refcount (%d)\n", tdb_refcount));
340 if ( tdb_refcount > 0 )
341 return 0;
343 SMB_ASSERT( tdb_refcount >= 0 );
345 TALLOC_FREE(tdb_reg);
346 return 0;
349 /***********************************************************************
350 return the tdb sequence number of the registry tdb.
351 this is an indicator for the content of the registry
352 having changed. it will change upon regdb_init, too, though.
353 ***********************************************************************/
354 int regdb_get_seqnum(void)
356 return tdb_get_seqnum(tdb_reg->tdb);
359 /***********************************************************************
360 Add subkey strings to the registry tdb under a defined key
361 fmt is the same format as tdb_pack except this function only supports
362 fstrings
363 ***********************************************************************/
365 static bool regdb_store_keys_internal(const char *key, REGSUBKEY_CTR *ctr)
367 TDB_DATA dbuf;
368 uint8 *buffer = NULL;
369 int i = 0;
370 uint32 len, buflen;
371 bool ret = true;
372 uint32 num_subkeys = regsubkey_ctr_numkeys(ctr);
373 char *keyname = NULL;
374 TALLOC_CTX *ctx = talloc_tos();
376 if (!key) {
377 return false;
380 keyname = talloc_strdup(ctx, key);
381 if (!keyname) {
382 return false;
384 keyname = normalize_reg_path(ctx, keyname);
386 /* allocate some initial memory */
388 if (!(buffer = (uint8 *)SMB_MALLOC(1024))) {
389 return false;
391 buflen = 1024;
392 len = 0;
394 /* store the number of subkeys */
396 len += tdb_pack(buffer+len, buflen-len, "d", num_subkeys );
398 /* pack all the strings */
400 for (i=0; i<num_subkeys; i++) {
401 len += tdb_pack( buffer+len, buflen-len, "f", regsubkey_ctr_specific_key(ctr, i) );
402 if ( len > buflen ) {
403 /* allocate some extra space */
404 if ((buffer = (uint8 *)SMB_REALLOC( buffer, len*2 )) == NULL) {
405 DEBUG(0,("regdb_store_keys: Failed to realloc memory of size [%d]\n", len*2));
406 ret = false;
407 goto done;
409 buflen = len*2;
411 len = tdb_pack( buffer+len, buflen-len, "f", regsubkey_ctr_specific_key(ctr, i) );
415 /* finally write out the data */
417 dbuf.dptr = buffer;
418 dbuf.dsize = len;
419 if ( tdb_store_bystring( tdb_reg->tdb, keyname, dbuf, TDB_REPLACE ) == -1) {
420 ret = false;
421 goto done;
424 done:
425 SAFE_FREE( buffer );
426 return ret;
429 /***********************************************************************
430 Store the new subkey record and create any child key records that
431 do not currently exist
432 ***********************************************************************/
434 bool regdb_store_keys(const char *key, REGSUBKEY_CTR *ctr)
436 int num_subkeys, i;
437 char *path = NULL;
438 REGSUBKEY_CTR *subkeys = NULL, *old_subkeys = NULL;
439 char *oldkeyname = NULL;
440 TALLOC_CTX *ctx = talloc_tos();
443 * fetch a list of the old subkeys so we can determine if anything has
444 * changed
447 if (!(old_subkeys = TALLOC_ZERO_P(ctr, REGSUBKEY_CTR))) {
448 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
449 return false;
452 regdb_fetch_keys(key, old_subkeys);
454 if (ctr->num_subkeys == old_subkeys->num_subkeys) {
456 for (i = 0; i<ctr->num_subkeys; i++) {
457 if (strcmp(ctr->subkeys[i],
458 old_subkeys->subkeys[i]) != 0) {
459 break;
462 if (i == ctr->num_subkeys) {
464 * Nothing changed, no point to even start a tdb
465 * transaction
467 TALLOC_FREE(old_subkeys);
468 return true;
472 if (tdb_transaction_start( tdb_reg->tdb ) == -1) {
473 DEBUG(0, ("regdb_store_keys: tdb_transaction_start failed\n"));
474 return false;
478 * Re-fetch the old keys inside the transaction
481 TALLOC_FREE(old_subkeys);
483 if (!(old_subkeys = TALLOC_ZERO_P(ctr, REGSUBKEY_CTR))) {
484 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
485 goto fail;
488 regdb_fetch_keys(key, old_subkeys);
490 /* store the subkey list for the parent */
492 if (!regdb_store_keys_internal(key, ctr) ) {
493 DEBUG(0,("regdb_store_keys: Failed to store new subkey list "
494 "for parent [%s]\n", key));
495 goto fail;
498 /* now delete removed keys */
500 num_subkeys = regsubkey_ctr_numkeys(old_subkeys);
501 for (i=0; i<num_subkeys; i++) {
502 oldkeyname = regsubkey_ctr_specific_key(old_subkeys, i);
504 if (regsubkey_ctr_key_exists(ctr, oldkeyname)) {
506 * It's still around, don't delete
509 continue;
512 path = talloc_asprintf(ctx, "%s/%s", key, oldkeyname);
513 if (!path) {
514 goto fail;
516 path = normalize_reg_path(ctx, path);
517 if (!path) {
518 goto fail;
520 if (tdb_delete_bystring(tdb_reg->tdb, path) == -1) {
521 DEBUG(1, ("Deleting %s failed\n", path));
522 goto fail;
525 TALLOC_FREE(path);
526 path = talloc_asprintf(ctx, "%s/%s/%s",
527 REG_VALUE_PREFIX,
528 key,
529 oldkeyname );
530 if (!path) {
531 goto fail;
533 path = normalize_reg_path(ctx, path);
534 if (!path) {
535 goto fail;
539 * Ignore errors here, we might have no values around
541 tdb_delete_bystring( tdb_reg->tdb, path );
542 TALLOC_FREE(path);
545 TALLOC_FREE(old_subkeys);
547 /* now create records for any subkeys that don't already exist */
549 num_subkeys = regsubkey_ctr_numkeys(ctr);
550 for (i=0; i<num_subkeys; i++) {
551 path = talloc_asprintf(ctx, "%s/%s",
552 key,
553 regsubkey_ctr_specific_key(ctr, i));
554 if (!path) {
555 goto fail;
557 if (!(subkeys = TALLOC_ZERO_P(ctr, REGSUBKEY_CTR)) ) {
558 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
559 goto fail;
562 if (regdb_fetch_keys( path, subkeys ) == -1) {
563 /* create a record with 0 subkeys */
564 if (!regdb_store_keys_internal(path, subkeys)) {
565 DEBUG(0,("regdb_store_keys: Failed to store "
566 "new record for key [%s]\n", path));
567 goto fail;
571 TALLOC_FREE(subkeys);
572 TALLOC_FREE(path);
575 if (tdb_transaction_commit( tdb_reg->tdb ) == -1) {
576 DEBUG(0, ("regdb_store_keys: Could not commit transaction\n"));
577 return false;
580 return true;
582 fail:
583 TALLOC_FREE(old_subkeys);
584 TALLOC_FREE(subkeys);
586 if (tdb_transaction_cancel(tdb_reg->tdb) == -1) {
587 smb_panic("regdb_store_keys: tdb_transaction_cancel failed\n");
590 return false;
594 /***********************************************************************
595 Retrieve an array of strings containing subkeys. Memory should be
596 released by the caller.
597 ***********************************************************************/
599 int regdb_fetch_keys(const char *key, REGSUBKEY_CTR *ctr)
601 char *path = NULL;
602 uint32 num_items;
603 TDB_DATA dbuf;
604 uint8 *buf;
605 uint32 buflen, len;
606 int i;
607 fstring subkeyname;
608 int ret = -1;
609 TALLOC_CTX *frame = talloc_stackframe();
611 DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key ? key : "NULL"));
613 path = talloc_strdup(talloc_tos(), key);
614 if (!path) {
615 goto fail;
618 /* convert to key format */
619 path = talloc_string_sub(talloc_tos(), path, "\\", "/");
620 if (!path) {
621 goto fail;
623 strupper_m(path);
625 if (tdb_read_lock_bystring_with_timeout(tdb_reg->tdb, path, 10) == -1) {
626 return 0;
629 dbuf = tdb_fetch_bystring(tdb_reg->tdb, path);
630 ctr->seqnum = regdb_get_seqnum();
632 tdb_read_unlock_bystring(tdb_reg->tdb, path);
635 buf = dbuf.dptr;
636 buflen = dbuf.dsize;
638 if ( !buf ) {
639 DEBUG(5,("regdb_fetch_keys: tdb lookup failed to locate key [%s]\n", key));
640 goto fail;
643 len = tdb_unpack( buf, buflen, "d", &num_items);
645 for (i=0; i<num_items; i++) {
646 len += tdb_unpack(buf+len, buflen-len, "f", subkeyname);
647 regsubkey_ctr_addkey(ctr, subkeyname);
650 SAFE_FREE(dbuf.dptr);
652 DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items));
654 ret = num_items;
655 fail:
656 TALLOC_FREE(frame);
657 return ret;
660 /****************************************************************************
661 Unpack a list of registry values frem the TDB
662 ***************************************************************************/
664 static int regdb_unpack_values(REGVAL_CTR *values, uint8 *buf, int buflen)
666 int len = 0;
667 uint32 type;
668 fstring valuename;
669 uint32 size;
670 uint8 *data_p;
671 uint32 num_values = 0;
672 int i;
674 /* loop and unpack the rest of the registry values */
676 len += tdb_unpack(buf+len, buflen-len, "d", &num_values);
678 for ( i=0; i<num_values; i++ ) {
679 /* unpack the next regval */
681 type = REG_NONE;
682 size = 0;
683 data_p = NULL;
684 valuename[0] = '\0';
685 len += tdb_unpack(buf+len, buflen-len, "fdB",
686 valuename,
687 &type,
688 &size,
689 &data_p);
691 /* add the new value. Paranoid protective code -- make sure data_p is valid */
693 if (*valuename && size && data_p) {
694 regval_ctr_addvalue(values, valuename, type,
695 (const char *)data_p, size);
697 SAFE_FREE(data_p); /* 'B' option to tdb_unpack does a malloc() */
699 DEBUG(8,("specific: [%s], len: %d\n", valuename, size));
702 return len;
705 /****************************************************************************
706 Pack all values in all printer keys
707 ***************************************************************************/
709 static int regdb_pack_values(REGVAL_CTR *values, uint8 *buf, int buflen)
711 int len = 0;
712 int i;
713 REGISTRY_VALUE *val;
714 int num_values;
716 if ( !values )
717 return 0;
719 num_values = regval_ctr_numvals( values );
721 /* pack the number of values first */
723 len += tdb_pack( buf+len, buflen-len, "d", num_values );
725 /* loop over all values */
727 for ( i=0; i<num_values; i++ ) {
728 val = regval_ctr_specific_value( values, i );
729 len += tdb_pack(buf+len, buflen-len, "fdB",
730 regval_name(val),
731 regval_type(val),
732 regval_size(val),
733 regval_data_p(val) );
736 return len;
739 /***********************************************************************
740 Retrieve an array of strings containing subkeys. Memory should be
741 released by the caller.
742 ***********************************************************************/
744 int regdb_fetch_values( const char* key, REGVAL_CTR *values )
746 TDB_DATA data;
747 char *keystr = NULL;
748 TALLOC_CTX *ctx = talloc_tos();
750 DEBUG(10,("regdb_fetch_values: Looking for value of key [%s] \n", key));
752 keystr = talloc_asprintf(ctx, "%s/%s", REG_VALUE_PREFIX, key);
753 if (!keystr) {
754 return 0;
756 keystr = normalize_reg_path(ctx, keystr);
757 if (!keystr) {
758 return 0;
761 if (tdb_read_lock_bystring_with_timeout(tdb_reg->tdb, keystr, 10) == -1) {
762 return 0;
765 data = tdb_fetch_bystring(tdb_reg->tdb, keystr);
766 values->seqnum = regdb_get_seqnum();
768 tdb_read_unlock_bystring(tdb_reg->tdb, keystr);
770 if (!data.dptr) {
771 /* all keys have zero values by default */
772 return 0;
775 regdb_unpack_values(values, data.dptr, data.dsize);
777 SAFE_FREE(data.dptr);
778 return regval_ctr_numvals(values);
781 bool regdb_store_values( const char *key, REGVAL_CTR *values )
783 TDB_DATA old_data, data;
784 char *keystr = NULL;
785 TALLOC_CTX *ctx = talloc_tos();
786 int len, ret;
788 DEBUG(10,("regdb_store_values: Looking for value of key [%s] \n", key));
790 ZERO_STRUCT(data);
792 len = regdb_pack_values(values, data.dptr, data.dsize);
793 if (len <= 0) {
794 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
795 return false;
798 data.dptr = SMB_MALLOC_ARRAY( uint8, len );
799 data.dsize = len;
801 len = regdb_pack_values(values, data.dptr, data.dsize);
803 SMB_ASSERT( len == data.dsize );
805 keystr = talloc_asprintf(ctx, "%s/%s", REG_VALUE_PREFIX, key );
806 if (!keystr) {
807 SAFE_FREE(data.dptr);
808 return false;
810 keystr = normalize_reg_path(ctx, keystr);
811 if (!keystr) {
812 SAFE_FREE(data.dptr);
813 return false;
816 old_data = tdb_fetch_bystring(tdb_reg->tdb, keystr);
818 if ((old_data.dptr != NULL)
819 && (old_data.dsize == data.dsize)
820 && (memcmp(old_data.dptr, data.dptr, data.dsize) == 0)) {
821 SAFE_FREE(old_data.dptr);
822 SAFE_FREE(data.dptr);
823 return true;
826 ret = tdb_trans_store_bystring(tdb_reg->tdb, keystr, data, TDB_REPLACE);
828 SAFE_FREE( old_data.dptr );
829 SAFE_FREE( data.dptr );
831 return ret != -1 ;
834 static WERROR regdb_get_secdesc(TALLOC_CTX *mem_ctx, const char *key,
835 struct security_descriptor **psecdesc)
837 char *tdbkey;
838 TDB_DATA data;
839 NTSTATUS status;
841 DEBUG(10, ("regdb_get_secdesc: Getting secdesc of key [%s]\n", key));
843 if (asprintf(&tdbkey, "%s/%s", REG_SECDESC_PREFIX, key) == -1) {
844 return WERR_NOMEM;
846 normalize_dbkey(tdbkey);
848 data = tdb_fetch_bystring(tdb_reg->tdb, tdbkey);
849 SAFE_FREE(tdbkey);
851 if (data.dptr == NULL) {
852 return WERR_BADFILE;
855 status = unmarshall_sec_desc(mem_ctx, (uint8 *)data.dptr, data.dsize,
856 psecdesc);
858 SAFE_FREE(data.dptr);
860 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MEMORY)) {
861 return WERR_NOMEM;
864 if (!NT_STATUS_IS_OK(status)) {
865 return WERR_REG_CORRUPT;
868 return WERR_OK;
871 static WERROR regdb_set_secdesc(const char *key,
872 struct security_descriptor *secdesc)
874 prs_struct ps;
875 TALLOC_CTX *mem_ctx;
876 char *tdbkey;
877 WERROR err = WERR_NOMEM;
878 TDB_DATA tdbdata;
880 if (!(mem_ctx = talloc_init("regdb_set_secdesc"))) {
881 return WERR_NOMEM;
884 ZERO_STRUCT(ps);
886 if (!(tdbkey = talloc_asprintf(mem_ctx, "%s/%s", REG_SECDESC_PREFIX,
887 key))) {
888 goto done;
890 normalize_dbkey(tdbkey);
892 if (secdesc == NULL) {
893 /* assuming a delete */
894 int tdb_ret;
896 tdb_ret = tdb_trans_delete(tdb_reg->tdb,
897 string_term_tdb_data(tdbkey));
898 if (tdb_ret == -1) {
899 err = ntstatus_to_werror(map_nt_error_from_unix(errno));
900 } else {
901 err = WERR_OK;
904 goto done;
907 err = ntstatus_to_werror(marshall_sec_desc(mem_ctx, secdesc,
908 &tdbdata.dptr,
909 &tdbdata.dsize));
910 if (!W_ERROR_IS_OK(err)) {
911 goto done;
914 if (tdb_trans_store_bystring(tdb_reg->tdb, tdbkey, tdbdata, 0) == -1) {
915 err = ntstatus_to_werror(map_nt_error_from_unix(errno));
916 goto done;
919 done:
920 prs_mem_free(&ps);
921 TALLOC_FREE(mem_ctx);
922 return err;
925 bool regdb_subkeys_need_update(REGSUBKEY_CTR *subkeys)
927 return (regdb_get_seqnum() != subkeys->seqnum);
930 bool regdb_values_need_update(REGVAL_CTR *values)
932 return (regdb_get_seqnum() != values->seqnum);
936 * Table of function pointers for default access
939 REGISTRY_OPS regdb_ops = {
940 regdb_fetch_keys,
941 regdb_fetch_values,
942 regdb_store_keys,
943 regdb_store_values,
944 NULL,
945 regdb_get_secdesc,
946 regdb_set_secdesc,
947 regdb_subkeys_need_update,
948 regdb_values_need_update