s3:registry: refactor adding of builtin reg values out
[Samba/gbeck.git] / source3 / registry / reg_backend_db.c
blobd04d34bac051642d1805369c1842a678f49af5b7
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 static bool regdb_key_exists(struct db_context *db, const char *key);
31 static bool regdb_key_is_base_key(const char *key);
32 static int regdb_fetch_keys_internal(struct db_context *db, const char *key,
33 struct regsubkey_ctr *ctr);
34 static bool regdb_store_keys_internal(struct db_context *db, const char *key,
35 struct regsubkey_ctr *ctr);
36 static int regdb_fetch_values_internal(struct db_context *db, const char* key,
37 struct regval_ctr *values);
38 static bool regdb_store_values_internal(struct db_context *db, const char *key,
39 struct regval_ctr *values);
41 /* List the deepest path into the registry. All part components will be created.*/
43 /* If you want to have a part of the path controlled by the tdb and part by
44 a virtual registry db (e.g. printing), then you have to list the deepest path.
45 For example,"HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion/Print"
46 allows the reg_db backend to handle everything up to
47 "HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion" and then we'll hook
48 the reg_printing backend onto the last component of the path (see
49 KEY_PRINTING_2K in include/rpc_reg.h) --jerry */
51 static const char *builtin_registry_paths[] = {
52 KEY_PRINTING_2K,
53 KEY_PRINTING_PORTS,
54 KEY_PRINTING,
55 KEY_SHARES,
56 KEY_EVENTLOG,
57 KEY_SMBCONF,
58 KEY_PERFLIB,
59 KEY_PERFLIB_009,
60 KEY_GROUP_POLICY,
61 KEY_SAMBA_GROUP_POLICY,
62 KEY_GP_MACHINE_POLICY,
63 KEY_GP_MACHINE_WIN_POLICY,
64 KEY_HKCU,
65 KEY_GP_USER_POLICY,
66 KEY_GP_USER_WIN_POLICY,
67 KEY_WINLOGON_GPEXT_PATH,
68 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Print\\Monitors",
69 KEY_PROD_OPTIONS,
70 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\DefaultUserConfiguration",
71 KEY_TCPIP_PARAMS,
72 KEY_NETLOGON_PARAMS,
73 KEY_HKU,
74 KEY_HKCR,
75 KEY_HKPD,
76 KEY_HKPT,
77 NULL };
79 struct builtin_regkey_value {
80 const char *path;
81 const char *valuename;
82 uint32 type;
83 union {
84 const char *string;
85 uint32 dw_value;
86 } data;
89 static struct builtin_regkey_value builtin_registry_values[] = {
90 { KEY_PRINTING_PORTS,
91 SAMBA_PRINTER_PORT_NAME, REG_SZ, { "" } },
92 { KEY_PRINTING_2K,
93 "DefaultSpoolDirectory", REG_SZ, { "C:\\Windows\\System32\\Spool\\Printers" } },
94 { KEY_EVENTLOG,
95 "DisplayName", REG_SZ, { "Event Log" } },
96 { KEY_EVENTLOG,
97 "ErrorControl", REG_DWORD, { (char*)0x00000001 } },
98 { NULL, NULL, 0, { NULL } }
102 * Initialize a key in the registry:
103 * create each component key of the specified path.
105 static WERROR init_registry_key_internal(struct db_context *db,
106 const char *add_path)
108 WERROR werr;
109 TALLOC_CTX *frame = talloc_stackframe();
110 char *path = NULL;
111 char *base = NULL;
112 char *remaining = NULL;
113 char *keyname;
114 char *subkeyname;
115 struct regsubkey_ctr *subkeys;
116 const char *p, *p2;
118 DEBUG(6, ("init_registry_key: Adding [%s]\n", add_path));
120 path = talloc_strdup(frame, add_path);
121 base = talloc_strdup(frame, "");
122 if (!path || !base) {
123 werr = WERR_NOMEM;
124 goto fail;
126 p = path;
128 while (next_token_talloc(frame, &p, &keyname, "\\")) {
130 /* build up the registry path from the components */
132 if (*base) {
133 base = talloc_asprintf(frame, "%s\\", base);
134 if (!base) {
135 werr = WERR_NOMEM;
136 goto fail;
139 base = talloc_asprintf_append(base, "%s", keyname);
140 if (!base) {
141 werr = WERR_NOMEM;
142 goto fail;
145 /* get the immediate subkeyname (if we have one ) */
147 subkeyname = talloc_strdup(frame, "");
148 if (!subkeyname) {
149 werr = WERR_NOMEM;
150 goto fail;
152 if (*p) {
153 remaining = talloc_strdup(frame, p);
154 if (!remaining) {
155 werr = WERR_NOMEM;
156 goto fail;
158 p2 = remaining;
160 if (!next_token_talloc(frame, &p2,
161 &subkeyname, "\\"))
163 subkeyname = talloc_strdup(frame,p2);
164 if (!subkeyname) {
165 werr = WERR_NOMEM;
166 goto fail;
171 DEBUG(10,("init_registry_key: Storing key [%s] with "
172 "subkey [%s]\n", base,
173 *subkeyname ? subkeyname : "NULL"));
175 /* we don't really care if the lookup succeeds or not
176 * since we are about to update the record.
177 * We just want any subkeys already present */
179 werr = regsubkey_ctr_init(frame, &subkeys);
180 if (!W_ERROR_IS_OK(werr)) {
181 DEBUG(0,("talloc() failure!\n"));
182 goto fail;
185 regdb_fetch_keys_internal(db, base, subkeys);
186 if (*subkeyname) {
187 werr = regsubkey_ctr_addkey(subkeys, subkeyname);
188 if (!W_ERROR_IS_OK(werr)) {
189 goto fail;
192 if (!regdb_store_keys_internal(db, base, subkeys)) {
193 werr = WERR_CAN_NOT_COMPLETE;
194 goto fail;
198 werr = WERR_OK;
200 fail:
201 TALLOC_FREE(frame);
202 return werr;
205 struct init_registry_key_context {
206 const char *add_path;
209 static NTSTATUS init_registry_key_action(struct db_context *db,
210 void *private_data)
212 struct init_registry_key_context *init_ctx =
213 (struct init_registry_key_context *)private_data;
215 return werror_to_ntstatus(init_registry_key_internal(
216 db, init_ctx->add_path));
220 * Initialize a key in the registry:
221 * create each component key of the specified path,
222 * wrapped in one db transaction.
224 WERROR init_registry_key(const char *add_path)
226 struct init_registry_key_context init_ctx;
228 if (regdb_key_exists(regdb, add_path)) {
229 return WERR_OK;
232 init_ctx.add_path = add_path;
234 return ntstatus_to_werror(dbwrap_trans_do(regdb,
235 init_registry_key_action,
236 &init_ctx));
239 /***********************************************************************
240 Open the registry data in the tdb
241 ***********************************************************************/
243 static void regdb_ctr_add_value(struct regval_ctr *ctr,
244 struct builtin_regkey_value *value)
246 UNISTR2 data;
248 switch(value->type) {
249 case REG_DWORD:
250 regval_ctr_addvalue(ctr, value->valuename, REG_DWORD,
251 (char*)&value->data.dw_value,
252 sizeof(uint32));
253 break;
255 case REG_SZ:
256 init_unistr2(&data, value->data.string, UNI_STR_TERMINATE);
257 regval_ctr_addvalue(ctr, value->valuename, REG_SZ,
258 (char*)data.buffer,
259 data.uni_str_len*sizeof(uint16));
260 break;
262 default:
263 DEBUG(0, ("regdb_ctr_add_value: invalid value type in "
264 "registry values [%d]\n", value->type));
268 static NTSTATUS init_registry_data_action(struct db_context *db,
269 void *private_data)
271 NTSTATUS status;
272 TALLOC_CTX *frame = talloc_stackframe();
273 struct regval_ctr *values;
274 int i;
276 /* loop over all of the predefined paths and add each component */
278 for (i=0; builtin_registry_paths[i] != NULL; i++) {
279 if (regdb_key_exists(db, builtin_registry_paths[i])) {
280 continue;
282 status = werror_to_ntstatus(init_registry_key_internal(db,
283 builtin_registry_paths[i]));
284 if (!NT_STATUS_IS_OK(status)) {
285 goto done;
289 /* loop over all of the predefined values and add each component */
291 for (i=0; builtin_registry_values[i].path != NULL; i++) {
293 values = TALLOC_ZERO_P(frame, struct regval_ctr);
294 if (values == NULL) {
295 status = NT_STATUS_NO_MEMORY;
296 goto done;
299 regdb_fetch_values_internal(db,
300 builtin_registry_values[i].path,
301 values);
303 /* preserve existing values across restarts. Only add new ones */
305 if (!regval_ctr_key_exists(values,
306 builtin_registry_values[i].valuename))
308 regdb_ctr_add_value(values,
309 &builtin_registry_values[i]);
310 regdb_store_values_internal(db,
311 builtin_registry_values[i].path,
312 values);
314 TALLOC_FREE(values);
317 status = NT_STATUS_OK;
319 done:
321 TALLOC_FREE(frame);
322 return status;
325 WERROR init_registry_data(void)
327 WERROR werr;
328 TALLOC_CTX *frame = talloc_stackframe();
329 struct regval_ctr *values;
330 int i;
333 * First, check for the existence of the needed keys and values.
334 * If all do already exist, we can save the writes.
336 for (i=0; builtin_registry_paths[i] != NULL; i++) {
337 if (!regdb_key_exists(regdb, builtin_registry_paths[i])) {
338 goto do_init;
342 for (i=0; builtin_registry_values[i].path != NULL; i++) {
343 values = TALLOC_ZERO_P(frame, struct regval_ctr);
344 if (values == NULL) {
345 werr = WERR_NOMEM;
346 goto done;
349 regdb_fetch_values_internal(regdb,
350 builtin_registry_values[i].path,
351 values);
352 if (!regval_ctr_key_exists(values,
353 builtin_registry_values[i].valuename))
355 TALLOC_FREE(values);
356 goto do_init;
359 TALLOC_FREE(values);
362 werr = WERR_OK;
363 goto done;
365 do_init:
368 * There are potentially quite a few store operations which are all
369 * indiviually wrapped in tdb transactions. Wrapping them in a single
370 * transaction gives just a single transaction_commit() to actually do
371 * its fsync()s. See tdb/common/transaction.c for info about nested
372 * transaction behaviour.
375 werr = ntstatus_to_werror(dbwrap_trans_do(regdb,
376 init_registry_data_action,
377 NULL));
379 done:
380 TALLOC_FREE(frame);
381 return werr;
384 /***********************************************************************
385 Open the registry database
386 ***********************************************************************/
388 WERROR regdb_init(void)
390 const char *vstring = "INFO/version";
391 uint32 vers_id;
392 WERROR werr;
394 if (regdb) {
395 DEBUG(10, ("regdb_init: incrementing refcount (%d)\n",
396 regdb_refcount));
397 regdb_refcount++;
398 return WERR_OK;
401 regdb = db_open(NULL, state_path("registry.tdb"), 0,
402 REG_TDB_FLAGS, O_RDWR, 0600);
403 if (!regdb) {
404 regdb = db_open(NULL, state_path("registry.tdb"), 0,
405 REG_TDB_FLAGS, O_RDWR|O_CREAT, 0600);
406 if (!regdb) {
407 werr = ntstatus_to_werror(map_nt_error_from_unix(errno));
408 DEBUG(1,("regdb_init: Failed to open registry %s (%s)\n",
409 state_path("registry.tdb"), strerror(errno) ));
410 return werr;
413 DEBUG(10,("regdb_init: Successfully created registry tdb\n"));
416 regdb_refcount = 1;
418 vers_id = dbwrap_fetch_int32(regdb, vstring);
420 if ( vers_id != REGVER_V1 ) {
421 NTSTATUS status;
422 /* any upgrade code here if needed */
423 DEBUG(10, ("regdb_init: got %s = %d != %d\n", vstring,
424 vers_id, REGVER_V1));
425 status = dbwrap_trans_store_int32(regdb, vstring, REGVER_V1);
426 if (!NT_STATUS_IS_OK(status)) {
427 DEBUG(1, ("regdb_init: error storing %s = %d: %s\n",
428 vstring, REGVER_V1, nt_errstr(status)));
429 return ntstatus_to_werror(status);
430 } else {
431 DEBUG(10, ("regdb_init: stored %s = %d\n",
432 vstring, REGVER_V1));
436 return WERR_OK;
439 /***********************************************************************
440 Open the registry. Must already have been initialized by regdb_init()
441 ***********************************************************************/
443 WERROR regdb_open( void )
445 WERROR result = WERR_OK;
447 if ( regdb ) {
448 DEBUG(10,("regdb_open: incrementing refcount (%d)\n", regdb_refcount));
449 regdb_refcount++;
450 return WERR_OK;
453 become_root();
455 regdb = db_open(NULL, state_path("registry.tdb"), 0,
456 REG_TDB_FLAGS, O_RDWR, 0600);
457 if ( !regdb ) {
458 result = ntstatus_to_werror( map_nt_error_from_unix( errno ) );
459 DEBUG(0,("regdb_open: Failed to open %s! (%s)\n",
460 state_path("registry.tdb"), strerror(errno) ));
463 unbecome_root();
465 regdb_refcount = 1;
466 DEBUG(10,("regdb_open: refcount reset (%d)\n", regdb_refcount));
468 return result;
471 /***********************************************************************
472 ***********************************************************************/
474 int regdb_close( void )
476 if (regdb_refcount == 0) {
477 return 0;
480 regdb_refcount--;
482 DEBUG(10,("regdb_close: decrementing refcount (%d)\n", regdb_refcount));
484 if ( regdb_refcount > 0 )
485 return 0;
487 SMB_ASSERT( regdb_refcount >= 0 );
489 TALLOC_FREE(regdb);
490 return 0;
493 WERROR regdb_transaction_start(void)
495 return (regdb->transaction_start(regdb) == 0) ?
496 WERR_OK : WERR_REG_IO_FAILURE;
499 WERROR regdb_transaction_commit(void)
501 return (regdb->transaction_commit(regdb) == 0) ?
502 WERR_OK : WERR_REG_IO_FAILURE;
505 WERROR regdb_transaction_cancel(void)
507 return (regdb->transaction_cancel(regdb) == 0) ?
508 WERR_OK : WERR_REG_IO_FAILURE;
511 /***********************************************************************
512 return the tdb sequence number of the registry tdb.
513 this is an indicator for the content of the registry
514 having changed. it will change upon regdb_init, too, though.
515 ***********************************************************************/
516 int regdb_get_seqnum(void)
518 return regdb->get_seqnum(regdb);
522 static WERROR regdb_delete_key_with_prefix(struct db_context *db,
523 const char *keyname,
524 const char *prefix)
526 char *path;
527 WERROR werr = WERR_NOMEM;
528 TALLOC_CTX *mem_ctx = talloc_stackframe();
530 if (keyname == NULL) {
531 werr = WERR_INVALID_PARAM;
532 goto done;
535 if (prefix == NULL) {
536 path = discard_const_p(char, keyname);
537 } else {
538 path = talloc_asprintf(mem_ctx, "%s/%s", prefix, keyname);
539 if (path == NULL) {
540 goto done;
544 path = normalize_reg_path(mem_ctx, path);
545 if (path == NULL) {
546 goto done;
549 werr = ntstatus_to_werror(dbwrap_delete_bystring(db, path));
551 /* treat "not" found" as ok */
552 if (W_ERROR_EQUAL(werr, WERR_NOT_FOUND)) {
553 werr = WERR_OK;
556 done:
557 talloc_free(mem_ctx);
558 return werr;
562 static WERROR regdb_delete_values(struct db_context *db, const char *keyname)
564 return regdb_delete_key_with_prefix(db, keyname, REG_VALUE_PREFIX);
567 static WERROR regdb_delete_secdesc(struct db_context *db, const char *keyname)
569 return regdb_delete_key_with_prefix(db, keyname, REG_SECDESC_PREFIX);
572 static WERROR regdb_delete_subkeylist(struct db_context *db, const char *keyname)
574 return regdb_delete_key_with_prefix(db, keyname, NULL);
577 static WERROR regdb_delete_key_lists(struct db_context *db, const char *keyname)
579 WERROR werr;
581 werr = regdb_delete_values(db, keyname);
582 if (!W_ERROR_IS_OK(werr)) {
583 DEBUG(1, (__location__ " Deleting %s/%s failed: %s\n",
584 REG_VALUE_PREFIX, keyname, win_errstr(werr)));
585 goto done;
588 werr = regdb_delete_secdesc(db, keyname);
589 if (!W_ERROR_IS_OK(werr)) {
590 DEBUG(1, (__location__ " Deleting %s/%s failed: %s\n",
591 REG_SECDESC_PREFIX, keyname, win_errstr(werr)));
592 goto done;
595 werr = regdb_delete_subkeylist(db, keyname);
596 if (!W_ERROR_IS_OK(werr)) {
597 DEBUG(1, (__location__ " Deleting %s failed: %s\n",
598 keyname, win_errstr(werr)));
599 goto done;
602 done:
603 return werr;
606 /***********************************************************************
607 Add subkey strings to the registry tdb under a defined key
608 fmt is the same format as tdb_pack except this function only supports
609 fstrings
610 ***********************************************************************/
612 static bool regdb_store_keys_internal2(struct db_context *db,
613 const char *key,
614 struct regsubkey_ctr *ctr)
616 TDB_DATA dbuf;
617 uint8 *buffer = NULL;
618 int i = 0;
619 uint32 len, buflen;
620 bool ret = true;
621 uint32 num_subkeys = regsubkey_ctr_numkeys(ctr);
622 char *keyname = NULL;
623 TALLOC_CTX *ctx = talloc_stackframe();
624 NTSTATUS status;
626 if (!key) {
627 return false;
630 keyname = talloc_strdup(ctx, key);
631 if (!keyname) {
632 return false;
634 keyname = normalize_reg_path(ctx, keyname);
636 /* allocate some initial memory */
638 buffer = (uint8 *)SMB_MALLOC(1024);
639 if (buffer == NULL) {
640 return false;
642 buflen = 1024;
643 len = 0;
645 /* store the number of subkeys */
647 len += tdb_pack(buffer+len, buflen-len, "d", num_subkeys);
649 /* pack all the strings */
651 for (i=0; i<num_subkeys; i++) {
652 size_t thistime;
654 thistime = tdb_pack(buffer+len, buflen-len, "f",
655 regsubkey_ctr_specific_key(ctr, i));
656 if (len+thistime > buflen) {
657 size_t thistime2;
659 * tdb_pack hasn't done anything because of the short
660 * buffer, allocate extra space.
662 buffer = SMB_REALLOC_ARRAY(buffer, uint8_t,
663 (len+thistime)*2);
664 if(buffer == NULL) {
665 DEBUG(0, ("regdb_store_keys: Failed to realloc "
666 "memory of size [%u]\n",
667 (unsigned int)(len+thistime)*2));
668 ret = false;
669 goto done;
671 buflen = (len+thistime)*2;
672 thistime2 = tdb_pack(
673 buffer+len, buflen-len, "f",
674 regsubkey_ctr_specific_key(ctr, i));
675 if (thistime2 != thistime) {
676 DEBUG(0, ("tdb_pack failed\n"));
677 ret = false;
678 goto done;
681 len += thistime;
684 /* finally write out the data */
686 dbuf.dptr = buffer;
687 dbuf.dsize = len;
688 status = dbwrap_store_bystring(db, keyname, dbuf, TDB_REPLACE);
689 if (!NT_STATUS_IS_OK(status)) {
690 ret = false;
691 goto done;
695 * Delete a sorted subkey cache for regdb_key_exists, will be
696 * recreated automatically
698 keyname = talloc_asprintf(ctx, "%s/%s", REG_SORTED_SUBKEYS_PREFIX,
699 keyname);
700 if (keyname != NULL) {
701 dbwrap_delete_bystring(db, keyname);
704 done:
705 TALLOC_FREE(ctx);
706 SAFE_FREE(buffer);
707 return ret;
710 /***********************************************************************
711 Store the new subkey record and create any child key records that
712 do not currently exist
713 ***********************************************************************/
715 static bool regdb_store_keys_internal(struct db_context *db, const char *key,
716 struct regsubkey_ctr *ctr)
718 int num_subkeys, old_num_subkeys, i;
719 char *path = NULL;
720 struct regsubkey_ctr *subkeys = NULL, *old_subkeys = NULL;
721 char *oldkeyname = NULL;
722 TALLOC_CTX *ctx = talloc_stackframe();
723 WERROR werr;
725 if (!regdb_key_is_base_key(key) && !regdb_key_exists(db, key)) {
726 goto fail;
730 * fetch a list of the old subkeys so we can determine if anything has
731 * changed
734 werr = regsubkey_ctr_init(ctx, &old_subkeys);
735 if (!W_ERROR_IS_OK(werr)) {
736 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
737 return false;
740 regdb_fetch_keys_internal(db, key, old_subkeys);
742 num_subkeys = regsubkey_ctr_numkeys(ctr);
743 old_num_subkeys = regsubkey_ctr_numkeys(old_subkeys);
744 if ((num_subkeys && old_num_subkeys) &&
745 (num_subkeys == old_num_subkeys)) {
747 for (i = 0; i < num_subkeys; i++) {
748 if (strcmp(regsubkey_ctr_specific_key(ctr, i),
749 regsubkey_ctr_specific_key(old_subkeys, i))
750 != 0)
752 break;
755 if (i == num_subkeys) {
757 * Nothing changed, no point to even start a tdb
758 * transaction
760 TALLOC_FREE(old_subkeys);
761 return true;
765 TALLOC_FREE(old_subkeys);
767 if (db->transaction_start(db) != 0) {
768 DEBUG(0, ("regdb_store_keys: transaction_start failed\n"));
769 goto fail;
773 * Re-fetch the old keys inside the transaction
776 werr = regsubkey_ctr_init(ctx, &old_subkeys);
777 if (!W_ERROR_IS_OK(werr)) {
778 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
779 goto cancel;
782 regdb_fetch_keys_internal(db, key, old_subkeys);
785 * Make the store operation as safe as possible without transactions:
787 * (1) For each subkey removed from ctr compared with old_subkeys:
789 * (a) First delete the value db entry.
791 * (b) Next delete the secdesc db record.
793 * (c) Then delete the subkey list entry.
795 * (2) Now write the list of subkeys of the parent key,
796 * deleting removed entries and adding new ones.
798 * (3) Finally create the subkey list entries for the added keys.
800 * This way if we crash half-way in between deleting the subkeys
801 * and storing the parent's list of subkeys, no old data can pop up
802 * out of the blue when re-adding keys later on.
805 /* (1) delete removed keys' lists (values/secdesc/subkeys) */
807 num_subkeys = regsubkey_ctr_numkeys(old_subkeys);
808 for (i=0; i<num_subkeys; i++) {
809 oldkeyname = regsubkey_ctr_specific_key(old_subkeys, i);
811 if (regsubkey_ctr_key_exists(ctr, oldkeyname)) {
813 * It's still around, don't delete
816 continue;
819 path = talloc_asprintf(ctx, "%s/%s", key, oldkeyname);
820 if (!path) {
821 goto cancel;
824 werr = regdb_delete_key_lists(db, path);
825 W_ERROR_NOT_OK_GOTO(werr, cancel);
827 TALLOC_FREE(path);
830 TALLOC_FREE(old_subkeys);
832 /* (2) store the subkey list for the parent */
834 if (!regdb_store_keys_internal2(db, key, ctr)) {
835 DEBUG(0,("regdb_store_keys: Failed to store new subkey list "
836 "for parent [%s]\n", key));
837 goto cancel;
840 /* (3) now create records for any subkeys that don't already exist */
842 num_subkeys = regsubkey_ctr_numkeys(ctr);
844 if (num_subkeys == 0) {
845 werr = regsubkey_ctr_init(ctx, &subkeys);
846 if (!W_ERROR_IS_OK(werr)) {
847 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
848 goto cancel;
851 if (!regdb_store_keys_internal2(db, key, subkeys)) {
852 DEBUG(0,("regdb_store_keys: Failed to store "
853 "new record for key [%s]\n", key));
854 goto cancel;
856 TALLOC_FREE(subkeys);
860 for (i=0; i<num_subkeys; i++) {
861 path = talloc_asprintf(ctx, "%s/%s",
862 key,
863 regsubkey_ctr_specific_key(ctr, i));
864 if (!path) {
865 goto cancel;
867 werr = regsubkey_ctr_init(ctx, &subkeys);
868 if (!W_ERROR_IS_OK(werr)) {
869 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
870 goto cancel;
873 if (regdb_fetch_keys_internal(db, path, subkeys) == -1) {
874 /* create a record with 0 subkeys */
875 if (!regdb_store_keys_internal2(db, path, subkeys)) {
876 DEBUG(0,("regdb_store_keys: Failed to store "
877 "new record for key [%s]\n", path));
878 goto cancel;
882 TALLOC_FREE(subkeys);
883 TALLOC_FREE(path);
886 if (db->transaction_commit(db) != 0) {
887 DEBUG(0, ("regdb_store_keys: Could not commit transaction\n"));
888 goto fail;
891 TALLOC_FREE(ctx);
892 return true;
894 cancel:
895 if (db->transaction_cancel(db) != 0) {
896 smb_panic("regdb_store_keys: transaction_cancel failed\n");
899 fail:
900 TALLOC_FREE(ctx);
902 return false;
905 bool regdb_store_keys(const char *key, struct regsubkey_ctr *ctr)
907 return regdb_store_keys_internal(regdb, key, ctr);
910 static WERROR regdb_create_subkey(const char *key, const char *subkey)
912 WERROR werr;
913 struct regsubkey_ctr *subkeys;
914 TALLOC_CTX *mem_ctx = talloc_stackframe();
916 if (!regdb_key_is_base_key(key) && !regdb_key_exists(regdb, key)) {
917 werr = WERR_NOT_FOUND;
918 goto done;
921 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
922 W_ERROR_NOT_OK_GOTO_DONE(werr);
924 if (regdb_fetch_keys_internal(regdb, key, subkeys) < 0) {
925 werr = WERR_REG_IO_FAILURE;
926 goto done;
929 if (regsubkey_ctr_key_exists(subkeys, subkey)) {
930 werr = WERR_OK;
931 goto done;
934 talloc_free(subkeys);
936 if (regdb->transaction_start(regdb) != 0) {
937 werr = WERR_REG_IO_FAILURE;
938 goto done;
941 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
942 W_ERROR_NOT_OK_GOTO(werr, cancel);
944 if (regdb_fetch_keys_internal(regdb, key, subkeys) < 0) {
945 werr = WERR_REG_IO_FAILURE;
946 goto cancel;
949 werr = regsubkey_ctr_addkey(subkeys, subkey);
950 W_ERROR_NOT_OK_GOTO(werr, cancel);
952 if (!regdb_store_keys_internal2(regdb, key, subkeys)) {
953 DEBUG(0, (__location__ " failed to store new subkey list for "
954 "parent key %s\n", key));
955 werr = WERR_REG_IO_FAILURE;
956 goto cancel;
959 if (regdb->transaction_commit(regdb) != 0) {
960 werr = WERR_REG_IO_FAILURE;
961 DEBUG(0, (__location__ " failed to commit transaction\n"));
964 goto done;
966 cancel:
967 if (regdb->transaction_cancel(regdb) != 0) {
968 smb_panic("regdb_create_subkey: transaction_cancel failed\n");
971 done:
972 talloc_free(mem_ctx);
973 return werr;
976 static WERROR regdb_delete_subkey(const char *key, const char *subkey)
978 WERROR werr;
979 struct regsubkey_ctr *subkeys;
980 char *path;
981 TALLOC_CTX *mem_ctx = talloc_stackframe();
983 if (!regdb_key_is_base_key(key) && !regdb_key_exists(regdb, key)) {
984 werr = WERR_NOT_FOUND;
985 goto done;
988 path = talloc_asprintf(mem_ctx, "%s/%s", key, subkey);
989 if (path == NULL) {
990 werr = WERR_NOMEM;
991 goto done;
994 if (!regdb_key_exists(regdb, path)) {
995 werr = WERR_OK;
996 goto done;
999 if (regdb->transaction_start(regdb) != 0) {
1000 werr = WERR_REG_IO_FAILURE;
1001 goto done;
1004 werr = regdb_delete_key_lists(regdb, path);
1005 W_ERROR_NOT_OK_GOTO(werr, cancel);
1007 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
1008 W_ERROR_NOT_OK_GOTO(werr, cancel);
1010 if (regdb_fetch_keys_internal(regdb, key, subkeys) < 0) {
1011 werr = WERR_REG_IO_FAILURE;
1012 goto cancel;
1015 werr = regsubkey_ctr_delkey(subkeys, subkey);
1016 W_ERROR_NOT_OK_GOTO(werr, cancel);
1018 if (!regdb_store_keys_internal2(regdb, key, subkeys)) {
1019 DEBUG(0, (__location__ " failed to store new subkey_list for "
1020 "parent key %s\n", key));
1021 werr = WERR_REG_IO_FAILURE;
1022 goto cancel;
1025 if (regdb->transaction_commit(regdb) != 0) {
1026 DEBUG(0, (__location__ " failed to commit transaction\n"));
1027 werr = WERR_REG_IO_FAILURE;
1030 goto done;
1032 cancel:
1033 if (regdb->transaction_cancel(regdb) != 0) {
1034 smb_panic("regdb_delete_subkey: transaction_cancel failed\n");
1037 done:
1038 talloc_free(mem_ctx);
1039 return werr;
1042 static TDB_DATA regdb_fetch_key_internal(struct db_context *db,
1043 TALLOC_CTX *mem_ctx, const char *key)
1045 char *path = NULL;
1046 TDB_DATA data;
1048 path = normalize_reg_path(mem_ctx, key);
1049 if (!path) {
1050 return make_tdb_data(NULL, 0);
1053 data = dbwrap_fetch_bystring(db, mem_ctx, path);
1055 TALLOC_FREE(path);
1056 return data;
1061 * check whether a given key name represents a base key,
1062 * i.e one without a subkey separator ('/' or '\').
1064 static bool regdb_key_is_base_key(const char *key)
1066 TALLOC_CTX *mem_ctx = talloc_stackframe();
1067 bool ret = false;
1068 char *path;
1070 if (key == NULL) {
1071 goto done;
1074 path = normalize_reg_path(mem_ctx, key);
1075 if (path == NULL) {
1076 DEBUG(0, ("out of memory! (talloc failed)\n"));
1077 goto done;
1080 if (*path == '\0') {
1081 goto done;
1084 ret = (strrchr(path, '/') == NULL);
1086 done:
1087 TALLOC_FREE(mem_ctx);
1088 return ret;
1092 * regdb_key_exists() is a very frequent operation. It can be quite
1093 * time-consuming to fully fetch the parent's subkey list, talloc_strdup all
1094 * subkeys and then compare the keyname linearly to all the parent's subkeys.
1096 * The following code tries to make this operation as efficient as possible:
1097 * Per registry key we create a list of subkeys that is very efficient to
1098 * search for existence of a subkey. Its format is:
1100 * 4 bytes num_subkeys
1101 * 4*num_subkey bytes offset into the string array
1102 * then follows a sorted list of subkeys in uppercase
1104 * This record is created by create_sorted_subkeys() on demand if it does not
1105 * exist. scan_parent_subkeys() uses regdb->parse_record to search the sorted
1106 * list, the parsing code and the binary search can be found in
1107 * parent_subkey_scanner. The code uses parse_record() to avoid a memcpy of
1108 * the potentially large subkey record.
1110 * The sorted subkey record is deleted in regdb_store_keys_internal2 and
1111 * recreated on demand.
1114 static int cmp_keynames(const void *p1, const void *p2)
1116 return StrCaseCmp(*((char **)p1), *((char **)p2));
1119 static bool create_sorted_subkeys(const char *key, const char *sorted_keyname)
1121 char **sorted_subkeys;
1122 struct regsubkey_ctr *ctr;
1123 bool result = false;
1124 NTSTATUS status;
1125 char *buf;
1126 char *p;
1127 int i, res;
1128 size_t len;
1129 int num_subkeys;
1130 WERROR werr;
1132 if (regdb->transaction_start(regdb) != 0) {
1133 DEBUG(0, ("create_sorted_subkeys: transaction_start "
1134 "failed\n"));
1135 return false;
1138 werr = regsubkey_ctr_init(talloc_tos(), &ctr);
1139 if (!W_ERROR_IS_OK(werr)) {
1140 goto fail;
1143 res = regdb_fetch_keys_internal(regdb, key, ctr);
1144 if (res == -1) {
1145 goto fail;
1148 num_subkeys = regsubkey_ctr_numkeys(ctr);
1149 sorted_subkeys = talloc_array(ctr, char *, num_subkeys);
1150 if (sorted_subkeys == NULL) {
1151 goto fail;
1154 len = 4 + 4*num_subkeys;
1156 for (i = 0; i < num_subkeys; i++) {
1157 sorted_subkeys[i] = talloc_strdup_upper(sorted_subkeys,
1158 regsubkey_ctr_specific_key(ctr, i));
1159 if (sorted_subkeys[i] == NULL) {
1160 goto fail;
1162 len += strlen(sorted_subkeys[i])+1;
1165 qsort(sorted_subkeys, num_subkeys, sizeof(char *), cmp_keynames);
1167 buf = talloc_array(ctr, char, len);
1168 if (buf == NULL) {
1169 goto fail;
1171 p = buf + 4 + 4*num_subkeys;
1173 SIVAL(buf, 0, num_subkeys);
1175 for (i=0; i < num_subkeys; i++) {
1176 ptrdiff_t offset = p - buf;
1177 SIVAL(buf, 4 + 4*i, offset);
1178 strlcpy(p, sorted_subkeys[i], len-offset);
1179 p += strlen(sorted_subkeys[i]) + 1;
1182 status = dbwrap_store_bystring(
1183 regdb, sorted_keyname, make_tdb_data((uint8_t *)buf, len),
1184 TDB_REPLACE);
1185 if (!NT_STATUS_IS_OK(status)) {
1187 * Don't use a "goto fail;" here, this would commit the broken
1188 * transaction. See below for an explanation.
1190 if (regdb->transaction_cancel(regdb) == -1) {
1191 smb_panic("create_sorted_subkeys: transaction_cancel "
1192 "failed\n");
1194 TALLOC_FREE(ctr);
1195 return false;
1198 result = true;
1199 fail:
1201 * We only get here via the "goto fail" when we did not write anything
1202 * yet. Using transaction_commit even in a failure case is necessary
1203 * because this (disposable) call might be nested in other
1204 * transactions. Doing a cancel here would destroy the possibility of
1205 * a transaction_commit for transactions that we might be wrapped in.
1207 if (regdb->transaction_commit(regdb) == -1) {
1208 DEBUG(0, ("create_sorted_subkeys: transaction_commit "
1209 "failed\n"));
1210 result = false;
1213 TALLOC_FREE(ctr);
1214 return result;
1217 struct scan_subkey_state {
1218 char *name;
1219 bool scanned;
1220 bool found;
1223 static int parent_subkey_scanner(TDB_DATA key, TDB_DATA data,
1224 void *private_data)
1226 struct scan_subkey_state *state =
1227 (struct scan_subkey_state *)private_data;
1228 uint32_t num_subkeys;
1229 uint32_t l, u;
1231 if (data.dsize < sizeof(uint32_t)) {
1232 return -1;
1235 state->scanned = true;
1236 state->found = false;
1238 tdb_unpack(data.dptr, data.dsize, "d", &num_subkeys);
1240 l = 0;
1241 u = num_subkeys;
1243 while (l < u) {
1244 uint32_t idx = (l+u)/2;
1245 char *s = (char *)data.dptr + IVAL(data.dptr, 4 + 4*idx);
1246 int comparison = strcmp(state->name, s);
1248 if (comparison < 0) {
1249 u = idx;
1250 } else if (comparison > 0) {
1251 l = idx + 1;
1252 } else {
1253 state->found = true;
1254 return 0;
1257 return 0;
1260 static bool scan_parent_subkeys(struct db_context *db, const char *parent,
1261 const char *name)
1263 char *path = NULL;
1264 char *key = NULL;
1265 struct scan_subkey_state state = { 0, };
1266 bool result = false;
1267 int res;
1269 state.name = NULL;
1271 path = normalize_reg_path(talloc_tos(), parent);
1272 if (path == NULL) {
1273 goto fail;
1276 key = talloc_asprintf(talloc_tos(), "%s/%s",
1277 REG_SORTED_SUBKEYS_PREFIX, path);
1278 if (key == NULL) {
1279 goto fail;
1282 state.name = talloc_strdup_upper(talloc_tos(), name);
1283 if (state.name == NULL) {
1284 goto fail;
1286 state.scanned = false;
1288 res = db->parse_record(db, string_term_tdb_data(key),
1289 parent_subkey_scanner, &state);
1291 if (state.scanned) {
1292 result = state.found;
1293 } else {
1294 if (!create_sorted_subkeys(path, key)) {
1295 goto fail;
1297 res = db->parse_record(db, string_term_tdb_data(key),
1298 parent_subkey_scanner, &state);
1299 if ((res == 0) && (state.scanned)) {
1300 result = state.found;
1304 fail:
1305 TALLOC_FREE(path);
1306 TALLOC_FREE(state.name);
1307 return result;
1311 * Check for the existence of a key.
1313 * Existence of a key is authoritatively defined by its
1314 * existence in the list of subkeys of its parent key.
1315 * The exeption of this are keys without a parent key,
1316 * i.e. the "base" keys (HKLM, HKCU, ...).
1318 static bool regdb_key_exists(struct db_context *db, const char *key)
1320 TALLOC_CTX *mem_ctx = talloc_stackframe();
1321 TDB_DATA value;
1322 bool ret = false;
1323 char *path, *p;
1325 if (key == NULL) {
1326 goto done;
1329 path = normalize_reg_path(mem_ctx, key);
1330 if (path == NULL) {
1331 DEBUG(0, ("out of memory! (talloc failed)\n"));
1332 goto done;
1335 if (*path == '\0') {
1336 goto done;
1339 p = strrchr(path, '/');
1340 if (p == NULL) {
1341 /* this is a base key */
1342 value = regdb_fetch_key_internal(db, mem_ctx, path);
1343 ret = (value.dptr != NULL);
1344 } else {
1345 *p = '\0';
1346 ret = scan_parent_subkeys(db, path, p+1);
1349 done:
1350 TALLOC_FREE(mem_ctx);
1351 return ret;
1355 /***********************************************************************
1356 Retrieve an array of strings containing subkeys. Memory should be
1357 released by the caller.
1358 ***********************************************************************/
1360 static int regdb_fetch_keys_internal(struct db_context *db, const char *key,
1361 struct regsubkey_ctr *ctr)
1363 WERROR werr;
1364 uint32 num_items;
1365 uint8 *buf;
1366 uint32 buflen, len;
1367 int i;
1368 fstring subkeyname;
1369 int ret = -1;
1370 TALLOC_CTX *frame = talloc_stackframe();
1371 TDB_DATA value;
1373 DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key ? key : "NULL"));
1375 if (!regdb_key_exists(db, key)) {
1376 goto done;
1379 werr = regsubkey_ctr_set_seqnum(ctr, db->get_seqnum(db));
1380 if (!W_ERROR_IS_OK(werr)) {
1381 goto done;
1384 value = regdb_fetch_key_internal(db, frame, key);
1386 if (value.dptr == NULL) {
1387 DEBUG(10, ("regdb_fetch_keys: no subkeys found for key [%s]\n",
1388 key));
1389 ret = 0;
1390 goto done;
1393 buf = value.dptr;
1394 buflen = value.dsize;
1395 len = tdb_unpack( buf, buflen, "d", &num_items);
1397 for (i=0; i<num_items; i++) {
1398 len += tdb_unpack(buf+len, buflen-len, "f", subkeyname);
1399 werr = regsubkey_ctr_addkey(ctr, subkeyname);
1400 if (!W_ERROR_IS_OK(werr)) {
1401 DEBUG(5, ("regdb_fetch_keys: regsubkey_ctr_addkey "
1402 "failed: %s\n", win_errstr(werr)));
1403 goto done;
1407 DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items));
1409 ret = num_items;
1410 done:
1411 TALLOC_FREE(frame);
1412 return ret;
1415 int regdb_fetch_keys(const char *key, struct regsubkey_ctr *ctr)
1417 return regdb_fetch_keys_internal(regdb, key, ctr);
1420 /****************************************************************************
1421 Unpack a list of registry values frem the TDB
1422 ***************************************************************************/
1424 static int regdb_unpack_values(struct regval_ctr *values, uint8 *buf, int buflen)
1426 int len = 0;
1427 uint32 type;
1428 fstring valuename;
1429 uint32 size;
1430 uint8 *data_p;
1431 uint32 num_values = 0;
1432 int i;
1434 /* loop and unpack the rest of the registry values */
1436 len += tdb_unpack(buf+len, buflen-len, "d", &num_values);
1438 for ( i=0; i<num_values; i++ ) {
1439 /* unpack the next regval */
1441 type = REG_NONE;
1442 size = 0;
1443 data_p = NULL;
1444 valuename[0] = '\0';
1445 len += tdb_unpack(buf+len, buflen-len, "fdB",
1446 valuename,
1447 &type,
1448 &size,
1449 &data_p);
1451 /* add the new value. Paranoid protective code -- make sure data_p is valid */
1453 if (*valuename && size && data_p) {
1454 regval_ctr_addvalue(values, valuename, type,
1455 (const char *)data_p, size);
1457 SAFE_FREE(data_p); /* 'B' option to tdb_unpack does a malloc() */
1459 DEBUG(8,("specific: [%s], len: %d\n", valuename, size));
1462 return len;
1465 /****************************************************************************
1466 Pack all values in all printer keys
1467 ***************************************************************************/
1469 static int regdb_pack_values(struct regval_ctr *values, uint8 *buf, int buflen)
1471 int len = 0;
1472 int i;
1473 struct regval_blob *val;
1474 int num_values;
1476 if ( !values )
1477 return 0;
1479 num_values = regval_ctr_numvals( values );
1481 /* pack the number of values first */
1483 len += tdb_pack( buf+len, buflen-len, "d", num_values );
1485 /* loop over all values */
1487 for ( i=0; i<num_values; i++ ) {
1488 val = regval_ctr_specific_value( values, i );
1489 len += tdb_pack(buf+len, buflen-len, "fdB",
1490 regval_name(val),
1491 regval_type(val),
1492 regval_size(val),
1493 regval_data_p(val) );
1496 return len;
1499 /***********************************************************************
1500 Retrieve an array of strings containing subkeys. Memory should be
1501 released by the caller.
1502 ***********************************************************************/
1504 static int regdb_fetch_values_internal(struct db_context *db, const char* key,
1505 struct regval_ctr *values)
1507 char *keystr = NULL;
1508 TALLOC_CTX *ctx = talloc_stackframe();
1509 int ret = 0;
1510 TDB_DATA value;
1512 DEBUG(10,("regdb_fetch_values: Looking for value of key [%s] \n", key));
1514 if (!regdb_key_exists(db, key)) {
1515 goto done;
1518 keystr = talloc_asprintf(ctx, "%s/%s", REG_VALUE_PREFIX, key);
1519 if (!keystr) {
1520 goto done;
1523 values->seqnum = db->get_seqnum(db);
1525 value = regdb_fetch_key_internal(db, ctx, keystr);
1527 if (!value.dptr) {
1528 /* all keys have zero values by default */
1529 goto done;
1532 regdb_unpack_values(values, value.dptr, value.dsize);
1533 ret = regval_ctr_numvals(values);
1535 done:
1536 TALLOC_FREE(ctx);
1537 return ret;
1540 int regdb_fetch_values(const char* key, struct regval_ctr *values)
1542 return regdb_fetch_values_internal(regdb, key, values);
1545 static bool regdb_store_values_internal(struct db_context *db, const char *key,
1546 struct regval_ctr *values)
1548 TDB_DATA old_data, data;
1549 char *keystr = NULL;
1550 TALLOC_CTX *ctx = talloc_stackframe();
1551 int len;
1552 NTSTATUS status;
1553 bool result = false;
1555 DEBUG(10,("regdb_store_values: Looking for value of key [%s] \n", key));
1557 if (!regdb_key_exists(db, key)) {
1558 goto done;
1561 ZERO_STRUCT(data);
1563 len = regdb_pack_values(values, data.dptr, data.dsize);
1564 if (len <= 0) {
1565 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
1566 goto done;
1569 data.dptr = TALLOC_ARRAY(ctx, uint8, len);
1570 data.dsize = len;
1572 len = regdb_pack_values(values, data.dptr, data.dsize);
1574 SMB_ASSERT( len == data.dsize );
1576 keystr = talloc_asprintf(ctx, "%s/%s", REG_VALUE_PREFIX, key );
1577 if (!keystr) {
1578 goto done;
1580 keystr = normalize_reg_path(ctx, keystr);
1581 if (!keystr) {
1582 goto done;
1585 old_data = dbwrap_fetch_bystring(db, ctx, keystr);
1587 if ((old_data.dptr != NULL)
1588 && (old_data.dsize == data.dsize)
1589 && (memcmp(old_data.dptr, data.dptr, data.dsize) == 0))
1591 result = true;
1592 goto done;
1595 status = dbwrap_trans_store_bystring(db, keystr, data, TDB_REPLACE);
1597 result = NT_STATUS_IS_OK(status);
1599 done:
1600 TALLOC_FREE(ctx);
1601 return result;
1604 bool regdb_store_values(const char *key, struct regval_ctr *values)
1606 return regdb_store_values_internal(regdb, key, values);
1609 static WERROR regdb_get_secdesc(TALLOC_CTX *mem_ctx, const char *key,
1610 struct security_descriptor **psecdesc)
1612 char *tdbkey;
1613 TDB_DATA data;
1614 NTSTATUS status;
1615 TALLOC_CTX *tmp_ctx = talloc_stackframe();
1616 WERROR err = WERR_OK;
1618 DEBUG(10, ("regdb_get_secdesc: Getting secdesc of key [%s]\n", key));
1620 if (!regdb_key_exists(regdb, key)) {
1621 err = WERR_BADFILE;
1622 goto done;
1625 tdbkey = talloc_asprintf(tmp_ctx, "%s/%s", REG_SECDESC_PREFIX, key);
1626 if (tdbkey == NULL) {
1627 err = WERR_NOMEM;
1628 goto done;
1630 normalize_dbkey(tdbkey);
1632 data = dbwrap_fetch_bystring(regdb, tmp_ctx, tdbkey);
1633 if (data.dptr == NULL) {
1634 err = WERR_BADFILE;
1635 goto done;
1638 status = unmarshall_sec_desc(mem_ctx, (uint8 *)data.dptr, data.dsize,
1639 psecdesc);
1641 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MEMORY)) {
1642 err = WERR_NOMEM;
1643 } else if (!NT_STATUS_IS_OK(status)) {
1644 err = WERR_REG_CORRUPT;
1647 done:
1648 TALLOC_FREE(tmp_ctx);
1649 return err;
1652 static WERROR regdb_set_secdesc(const char *key,
1653 struct security_descriptor *secdesc)
1655 TALLOC_CTX *mem_ctx = talloc_stackframe();
1656 char *tdbkey;
1657 WERROR err = WERR_NOMEM;
1658 TDB_DATA tdbdata;
1660 if (!regdb_key_exists(regdb, key)) {
1661 err = WERR_BADFILE;
1662 goto done;
1665 tdbkey = talloc_asprintf(mem_ctx, "%s/%s", REG_SECDESC_PREFIX, key);
1666 if (tdbkey == NULL) {
1667 goto done;
1669 normalize_dbkey(tdbkey);
1671 if (secdesc == NULL) {
1672 /* assuming a delete */
1673 err = ntstatus_to_werror(dbwrap_trans_delete_bystring(regdb,
1674 tdbkey));
1675 goto done;
1678 err = ntstatus_to_werror(marshall_sec_desc(mem_ctx, secdesc,
1679 &tdbdata.dptr,
1680 &tdbdata.dsize));
1681 W_ERROR_NOT_OK_GOTO_DONE(err);
1683 err = ntstatus_to_werror(dbwrap_trans_store_bystring(regdb, tdbkey,
1684 tdbdata, 0));
1686 done:
1687 TALLOC_FREE(mem_ctx);
1688 return err;
1691 bool regdb_subkeys_need_update(struct regsubkey_ctr *subkeys)
1693 return (regdb_get_seqnum() != regsubkey_ctr_get_seqnum(subkeys));
1696 bool regdb_values_need_update(struct regval_ctr *values)
1698 return (regdb_get_seqnum() != values->seqnum);
1702 * Table of function pointers for default access
1705 struct registry_ops regdb_ops = {
1706 .fetch_subkeys = regdb_fetch_keys,
1707 .fetch_values = regdb_fetch_values,
1708 .store_subkeys = regdb_store_keys,
1709 .store_values = regdb_store_values,
1710 .create_subkey = regdb_create_subkey,
1711 .delete_subkey = regdb_delete_subkey,
1712 .get_secdesc = regdb_get_secdesc,
1713 .set_secdesc = regdb_set_secdesc,
1714 .subkeys_need_update = regdb_subkeys_need_update,
1715 .values_need_update = regdb_values_need_update