Add STREAMINFO op to vfs_full_audit
[Samba.git] / source / registry / reg_backend_db.c
blob52a01507c09b3b4987008d2187ffbd102ded2cdc
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 len += tdb_pack(buffer+len, buflen-len, "f",
491 regsubkey_ctr_specific_key(ctr, i));
492 if (len > buflen) {
493 /* allocate some extra space */
494 buffer = (uint8 *)SMB_REALLOC(buffer, len*2);
495 if(buffer == NULL) {
496 DEBUG(0, ("regdb_store_keys: Failed to realloc "
497 "memory of size [%d]\n", len*2));
498 ret = false;
499 goto done;
501 buflen = len*2;
502 len = tdb_pack(buffer+len, buflen-len, "f",
503 regsubkey_ctr_specific_key(ctr, i));
507 /* finally write out the data */
509 dbuf.dptr = buffer;
510 dbuf.dsize = len;
511 status = dbwrap_store_bystring(regdb, keyname, dbuf, TDB_REPLACE);
512 if (!NT_STATUS_IS_OK(status)) {
513 ret = false;
514 goto done;
517 done:
518 TALLOC_FREE(ctx);
519 SAFE_FREE(buffer);
520 return ret;
523 /***********************************************************************
524 Store the new subkey record and create any child key records that
525 do not currently exist
526 ***********************************************************************/
528 bool regdb_store_keys(const char *key, REGSUBKEY_CTR *ctr)
530 int num_subkeys, i;
531 char *path = NULL;
532 REGSUBKEY_CTR *subkeys = NULL, *old_subkeys = NULL;
533 char *oldkeyname = NULL;
534 TALLOC_CTX *ctx = talloc_stackframe();
535 NTSTATUS status;
538 * fetch a list of the old subkeys so we can determine if anything has
539 * changed
542 if (!(old_subkeys = TALLOC_ZERO_P(ctx, REGSUBKEY_CTR))) {
543 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
544 return false;
547 regdb_fetch_keys(key, old_subkeys);
549 if ((ctr->num_subkeys && old_subkeys->num_subkeys) &&
550 (ctr->num_subkeys == old_subkeys->num_subkeys)) {
552 for (i = 0; i<ctr->num_subkeys; i++) {
553 if (strcmp(ctr->subkeys[i],
554 old_subkeys->subkeys[i]) != 0) {
555 break;
558 if (i == ctr->num_subkeys) {
560 * Nothing changed, no point to even start a tdb
561 * transaction
563 TALLOC_FREE(old_subkeys);
564 return true;
568 TALLOC_FREE(old_subkeys);
570 if (regdb->transaction_start(regdb) != 0) {
571 DEBUG(0, ("regdb_store_keys: transaction_start failed\n"));
572 goto fail;
576 * Re-fetch the old keys inside the transaction
579 if (!(old_subkeys = TALLOC_ZERO_P(ctx, REGSUBKEY_CTR))) {
580 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
581 goto cancel;
584 regdb_fetch_keys(key, old_subkeys);
586 /* store the subkey list for the parent */
588 if (!regdb_store_keys_internal(key, ctr) ) {
589 DEBUG(0,("regdb_store_keys: Failed to store new subkey list "
590 "for parent [%s]\n", key));
591 goto cancel;
594 /* now delete removed keys */
596 num_subkeys = regsubkey_ctr_numkeys(old_subkeys);
597 for (i=0; i<num_subkeys; i++) {
598 oldkeyname = regsubkey_ctr_specific_key(old_subkeys, i);
600 if (regsubkey_ctr_key_exists(ctr, oldkeyname)) {
602 * It's still around, don't delete
605 continue;
608 path = talloc_asprintf(ctx, "%s/%s", key, oldkeyname);
609 if (!path) {
610 goto cancel;
612 path = normalize_reg_path(ctx, path);
613 if (!path) {
614 goto cancel;
616 status = dbwrap_delete_bystring(regdb, path);
617 if (!NT_STATUS_IS_OK(status)) {
618 DEBUG(1, ("Deleting %s failed\n", path));
619 goto cancel;
622 TALLOC_FREE(path);
623 path = talloc_asprintf(ctx, "%s/%s/%s",
624 REG_VALUE_PREFIX,
625 key,
626 oldkeyname );
627 if (!path) {
628 goto cancel;
630 path = normalize_reg_path(ctx, path);
631 if (!path) {
632 goto cancel;
636 * Ignore errors here, we might have no values around
638 dbwrap_delete_bystring(regdb, path);
639 TALLOC_FREE(path);
642 TALLOC_FREE(old_subkeys);
644 /* now create records for any subkeys that don't already exist */
646 num_subkeys = regsubkey_ctr_numkeys(ctr);
648 if (num_subkeys == 0) {
649 if (!(subkeys = TALLOC_ZERO_P(ctx, REGSUBKEY_CTR)) ) {
650 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
651 goto cancel;
654 if (!regdb_store_keys_internal(key, subkeys)) {
655 DEBUG(0,("regdb_store_keys: Failed to store "
656 "new record for key [%s]\n", key));
657 goto cancel;
659 TALLOC_FREE(subkeys);
663 for (i=0; i<num_subkeys; i++) {
664 path = talloc_asprintf(ctx, "%s/%s",
665 key,
666 regsubkey_ctr_specific_key(ctr, i));
667 if (!path) {
668 goto cancel;
670 if (!(subkeys = TALLOC_ZERO_P(ctx, REGSUBKEY_CTR)) ) {
671 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
672 goto cancel;
675 if (regdb_fetch_keys( path, subkeys ) == -1) {
676 /* create a record with 0 subkeys */
677 if (!regdb_store_keys_internal(path, subkeys)) {
678 DEBUG(0,("regdb_store_keys: Failed to store "
679 "new record for key [%s]\n", path));
680 goto cancel;
684 TALLOC_FREE(subkeys);
685 TALLOC_FREE(path);
688 if (regdb->transaction_commit(regdb) != 0) {
689 DEBUG(0, ("regdb_store_keys: Could not commit transaction\n"));
690 goto fail;
693 TALLOC_FREE(ctx);
694 return true;
696 cancel:
697 if (regdb->transaction_cancel(regdb) != 0) {
698 smb_panic("regdb_store_keys: transaction_cancel failed\n");
701 fail:
702 TALLOC_FREE(ctx);
704 return false;
708 /***********************************************************************
709 Retrieve an array of strings containing subkeys. Memory should be
710 released by the caller.
711 ***********************************************************************/
713 int regdb_fetch_keys(const char *key, REGSUBKEY_CTR *ctr)
715 WERROR werr;
716 char *path = NULL;
717 uint32 num_items;
718 uint8 *buf;
719 uint32 buflen, len;
720 int i;
721 fstring subkeyname;
722 int ret = -1;
723 int dbret = -1;
724 TALLOC_CTX *frame = talloc_stackframe();
725 TDB_DATA value;
727 DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key ? key : "NULL"));
729 path = talloc_strdup(frame, key);
730 if (!path) {
731 goto fail;
734 /* convert to key format */
735 path = talloc_string_sub(frame, path, "\\", "/");
736 if (!path) {
737 goto fail;
739 strupper_m(path);
741 ctr->seqnum = regdb_get_seqnum();
743 dbret = regdb->fetch(regdb, frame, string_term_tdb_data(path), &value);
744 if (dbret != 0) {
745 goto fail;
748 buf = value.dptr;
749 buflen = value.dsize;
751 if ( !buf ) {
752 DEBUG(5,("regdb_fetch_keys: tdb lookup failed to locate key [%s]\n", key));
753 goto fail;
756 len = tdb_unpack( buf, buflen, "d", &num_items);
758 for (i=0; i<num_items; i++) {
759 len += tdb_unpack(buf+len, buflen-len, "f", subkeyname);
760 werr = regsubkey_ctr_addkey(ctr, subkeyname);
761 if (!W_ERROR_IS_OK(werr)) {
762 DEBUG(5, ("regdb_fetch_keys: regsubkey_ctr_addkey "
763 "failed: %s\n", dos_errstr(werr)));
764 goto fail;
768 DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items));
770 ret = num_items;
771 fail:
772 TALLOC_FREE(frame);
773 return ret;
776 /****************************************************************************
777 Unpack a list of registry values frem the TDB
778 ***************************************************************************/
780 static int regdb_unpack_values(REGVAL_CTR *values, uint8 *buf, int buflen)
782 int len = 0;
783 uint32 type;
784 fstring valuename;
785 uint32 size;
786 uint8 *data_p;
787 uint32 num_values = 0;
788 int i;
790 /* loop and unpack the rest of the registry values */
792 len += tdb_unpack(buf+len, buflen-len, "d", &num_values);
794 for ( i=0; i<num_values; i++ ) {
795 /* unpack the next regval */
797 type = REG_NONE;
798 size = 0;
799 data_p = NULL;
800 valuename[0] = '\0';
801 len += tdb_unpack(buf+len, buflen-len, "fdB",
802 valuename,
803 &type,
804 &size,
805 &data_p);
807 /* add the new value. Paranoid protective code -- make sure data_p is valid */
809 if (*valuename && size && data_p) {
810 regval_ctr_addvalue(values, valuename, type,
811 (const char *)data_p, size);
813 SAFE_FREE(data_p); /* 'B' option to tdb_unpack does a malloc() */
815 DEBUG(8,("specific: [%s], len: %d\n", valuename, size));
818 return len;
821 /****************************************************************************
822 Pack all values in all printer keys
823 ***************************************************************************/
825 static int regdb_pack_values(REGVAL_CTR *values, uint8 *buf, int buflen)
827 int len = 0;
828 int i;
829 REGISTRY_VALUE *val;
830 int num_values;
832 if ( !values )
833 return 0;
835 num_values = regval_ctr_numvals( values );
837 /* pack the number of values first */
839 len += tdb_pack( buf+len, buflen-len, "d", num_values );
841 /* loop over all values */
843 for ( i=0; i<num_values; i++ ) {
844 val = regval_ctr_specific_value( values, i );
845 len += tdb_pack(buf+len, buflen-len, "fdB",
846 regval_name(val),
847 regval_type(val),
848 regval_size(val),
849 regval_data_p(val) );
852 return len;
855 /***********************************************************************
856 Retrieve an array of strings containing subkeys. Memory should be
857 released by the caller.
858 ***********************************************************************/
860 int regdb_fetch_values( const char* key, REGVAL_CTR *values )
862 char *keystr = NULL;
863 TALLOC_CTX *ctx = talloc_stackframe();
864 int ret = 0;
865 int dbret = -1;
866 TDB_DATA value;
868 DEBUG(10,("regdb_fetch_values: Looking for value of key [%s] \n", key));
870 keystr = talloc_asprintf(ctx, "%s/%s", REG_VALUE_PREFIX, key);
871 if (!keystr) {
872 return 0;
874 keystr = normalize_reg_path(ctx, keystr);
875 if (!keystr) {
876 goto done;
879 values->seqnum = regdb_get_seqnum();
881 dbret = regdb->fetch(regdb, ctx, string_term_tdb_data(keystr), &value);
882 if (dbret != 0) {
883 goto done;
886 if (!value.dptr) {
887 /* all keys have zero values by default */
888 goto done;
891 regdb_unpack_values(values, value.dptr, value.dsize);
892 ret = regval_ctr_numvals(values);
894 done:
895 TALLOC_FREE(ctx);
896 return ret;
899 bool regdb_store_values( const char *key, REGVAL_CTR *values )
901 TDB_DATA old_data, data;
902 char *keystr = NULL;
903 TALLOC_CTX *ctx = talloc_stackframe();
904 int len;
905 NTSTATUS status;
906 bool result = false;
908 DEBUG(10,("regdb_store_values: Looking for value of key [%s] \n", key));
910 ZERO_STRUCT(data);
912 len = regdb_pack_values(values, data.dptr, data.dsize);
913 if (len <= 0) {
914 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
915 goto done;
918 data.dptr = TALLOC_ARRAY(ctx, uint8, len);
919 data.dsize = len;
921 len = regdb_pack_values(values, data.dptr, data.dsize);
923 SMB_ASSERT( len == data.dsize );
925 keystr = talloc_asprintf(ctx, "%s/%s", REG_VALUE_PREFIX, key );
926 if (!keystr) {
927 goto done;
929 keystr = normalize_reg_path(ctx, keystr);
930 if (!keystr) {
931 goto done;
934 old_data = dbwrap_fetch_bystring(regdb, ctx, keystr);
936 if ((old_data.dptr != NULL)
937 && (old_data.dsize == data.dsize)
938 && (memcmp(old_data.dptr, data.dptr, data.dsize) == 0))
940 result = true;
941 goto done;
944 status = dbwrap_trans_store(regdb, string_term_tdb_data(keystr), data,
945 TDB_REPLACE);
947 result = NT_STATUS_IS_OK(status);
949 done:
950 TALLOC_FREE(ctx);
951 return result;
954 static WERROR regdb_get_secdesc(TALLOC_CTX *mem_ctx, const char *key,
955 struct security_descriptor **psecdesc)
957 char *tdbkey;
958 TDB_DATA data;
959 NTSTATUS status;
960 TALLOC_CTX *tmp_ctx = talloc_stackframe();
961 WERROR err = WERR_OK;
963 DEBUG(10, ("regdb_get_secdesc: Getting secdesc of key [%s]\n", key));
965 tdbkey = talloc_asprintf(tmp_ctx, "%s/%s", REG_SECDESC_PREFIX, key);
966 if (tdbkey == NULL) {
967 err = WERR_NOMEM;
968 goto done;
970 normalize_dbkey(tdbkey);
972 data = dbwrap_fetch_bystring(regdb, tmp_ctx, tdbkey);
973 if (data.dptr == NULL) {
974 err = WERR_BADFILE;
975 goto done;
978 status = unmarshall_sec_desc(mem_ctx, (uint8 *)data.dptr, data.dsize,
979 psecdesc);
981 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MEMORY)) {
982 err = WERR_NOMEM;
983 } else if (!NT_STATUS_IS_OK(status)) {
984 err = WERR_REG_CORRUPT;
987 done:
988 TALLOC_FREE(tmp_ctx);
989 return err;
992 static WERROR regdb_set_secdesc(const char *key,
993 struct security_descriptor *secdesc)
995 TALLOC_CTX *mem_ctx = talloc_stackframe();
996 char *tdbkey;
997 NTSTATUS status;
998 WERROR err = WERR_NOMEM;
999 TDB_DATA tdbdata;
1001 tdbkey = talloc_asprintf(mem_ctx, "%s/%s", REG_SECDESC_PREFIX, key);
1002 if (tdbkey == NULL) {
1003 goto done;
1005 normalize_dbkey(tdbkey);
1007 if (secdesc == NULL) {
1008 /* assuming a delete */
1009 status = dbwrap_trans_delete(regdb,
1010 string_term_tdb_data(tdbkey));
1011 if (NT_STATUS_IS_OK(status)) {
1012 err = WERR_OK;
1013 } else {
1014 err = ntstatus_to_werror(status);
1016 goto done;
1019 err = ntstatus_to_werror(marshall_sec_desc(mem_ctx, secdesc,
1020 &tdbdata.dptr,
1021 &tdbdata.dsize));
1022 if (!W_ERROR_IS_OK(err)) {
1023 goto done;
1026 status = dbwrap_trans_store(regdb, string_term_tdb_data(tdbkey),
1027 tdbdata, 0);
1028 if (!NT_STATUS_IS_OK(status)) {
1029 err = ntstatus_to_werror(status);
1030 goto done;
1033 done:
1034 TALLOC_FREE(mem_ctx);
1035 return err;
1038 bool regdb_subkeys_need_update(REGSUBKEY_CTR *subkeys)
1040 return (regdb_get_seqnum() != subkeys->seqnum);
1043 bool regdb_values_need_update(REGVAL_CTR *values)
1045 return (regdb_get_seqnum() != values->seqnum);
1049 * Table of function pointers for default access
1052 REGISTRY_OPS regdb_ops = {
1053 .fetch_subkeys = regdb_fetch_keys,
1054 .fetch_values = regdb_fetch_values,
1055 .store_subkeys = regdb_store_keys,
1056 .store_values = regdb_store_values,
1057 .get_secdesc = regdb_get_secdesc,
1058 .set_secdesc = regdb_set_secdesc,
1059 .subkeys_need_update = regdb_subkeys_need_update,
1060 .values_need_update = regdb_values_need_update