2 * Unix SMB/CIFS implementation.
3 * Virtual Windows Registry Layer
4 * Copyright (C) Gerald Carter 2002-2005
5 * Copyright (C) Michael Adam 2007-2009
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 /* Implementation of internal registry database functions. */
24 #include "system/filesys.h"
27 #include "reg_util_internal.h"
28 #include "reg_backend_db.h"
29 #include "reg_objects.h"
30 #include "nt_printing.h"
33 #include "../libcli/security/secdesc.h"
36 #define DBGC_CLASS DBGC_REGISTRY
38 static struct db_context *regdb = NULL;
39 static int regdb_refcount;
41 static bool regdb_key_exists(struct db_context *db, const char *key);
42 static bool regdb_key_is_base_key(const char *key);
43 static WERROR regdb_fetch_keys_internal(struct db_context *db, const char *key,
44 struct regsubkey_ctr *ctr);
45 static bool regdb_store_keys_internal(struct db_context *db, const char *key,
46 struct regsubkey_ctr *ctr);
47 static int regdb_fetch_values_internal(struct db_context *db, const char* key,
48 struct regval_ctr *values);
49 static bool regdb_store_values_internal(struct db_context *db, const char *key,
50 struct regval_ctr *values);
52 static NTSTATUS create_sorted_subkeys(const char *key);
54 /* List the deepest path into the registry. All part components will be created.*/
56 /* If you want to have a part of the path controlled by the tdb and part by
57 a virtual registry db (e.g. printing), then you have to list the deepest path.
58 For example,"HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion/Print"
59 allows the reg_db backend to handle everything up to
60 "HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion" and then we'll hook
61 the reg_printing backend onto the last component of the path (see
62 KEY_PRINTING_2K in include/rpc_reg.h) --jerry */
64 static const char *builtin_registry_paths[] = {
68 KEY_PRINTING "\\Forms",
69 KEY_PRINTING "\\Printers",
70 KEY_PRINTING "\\Environments\\Windows NT x86\\Print Processors\\winprint",
77 KEY_SAMBA_GROUP_POLICY,
78 KEY_GP_MACHINE_POLICY,
79 KEY_GP_MACHINE_WIN_POLICY,
82 KEY_GP_USER_WIN_POLICY,
83 "HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon\\GPExtensions",
84 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Print\\Monitors",
86 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\DefaultUserConfiguration",
95 struct builtin_regkey_value {
97 const char *valuename;
105 static struct builtin_regkey_value builtin_registry_values[] = {
106 { KEY_PRINTING_PORTS,
107 SAMBA_PRINTER_PORT_NAME, REG_SZ, { "" } },
109 "DefaultSpoolDirectory", REG_SZ, { "C:\\Windows\\System32\\Spool\\Printers" } },
111 "DisplayName", REG_SZ, { "Event Log" } },
113 "ErrorControl", REG_DWORD, { (char*)0x00000001 } },
114 { NULL, NULL, 0, { NULL } }
118 * Initialize a key in the registry:
119 * create each component key of the specified path.
121 static WERROR init_registry_key_internal(struct db_context *db,
122 const char *add_path)
125 TALLOC_CTX *frame = talloc_stackframe();
128 char *remaining = NULL;
131 struct regsubkey_ctr *subkeys;
134 DEBUG(6, ("init_registry_key: Adding [%s]\n", add_path));
136 path = talloc_strdup(frame, add_path);
137 base = talloc_strdup(frame, "");
138 if (!path || !base) {
144 while (next_token_talloc(frame, &p, &keyname, "\\")) {
146 /* build up the registry path from the components */
149 base = talloc_asprintf(frame, "%s\\", base);
155 base = talloc_asprintf_append(base, "%s", keyname);
161 /* get the immediate subkeyname (if we have one ) */
163 subkeyname = talloc_strdup(frame, "");
169 remaining = talloc_strdup(frame, p);
176 if (!next_token_talloc(frame, &p2,
179 subkeyname = talloc_strdup(frame,p2);
187 DEBUG(10,("init_registry_key: Storing key [%s] with "
188 "subkey [%s]\n", base,
189 *subkeyname ? subkeyname : "NULL"));
191 /* we don't really care if the lookup succeeds or not
192 * since we are about to update the record.
193 * We just want any subkeys already present */
195 werr = regsubkey_ctr_init(frame, &subkeys);
196 if (!W_ERROR_IS_OK(werr)) {
197 DEBUG(0,("talloc() failure!\n"));
201 werr = regdb_fetch_keys_internal(db, base, subkeys);
202 if (!W_ERROR_IS_OK(werr) &&
203 !W_ERROR_EQUAL(werr, WERR_NOT_FOUND))
209 werr = regsubkey_ctr_addkey(subkeys, subkeyname);
210 if (!W_ERROR_IS_OK(werr)) {
214 if (!regdb_store_keys_internal(db, base, subkeys)) {
215 werr = WERR_CAN_NOT_COMPLETE;
227 struct init_registry_key_context {
228 const char *add_path;
231 static NTSTATUS init_registry_key_action(struct db_context *db,
234 struct init_registry_key_context *init_ctx =
235 (struct init_registry_key_context *)private_data;
237 return werror_to_ntstatus(init_registry_key_internal(
238 db, init_ctx->add_path));
242 * Initialize a key in the registry:
243 * create each component key of the specified path,
244 * wrapped in one db transaction.
246 WERROR init_registry_key(const char *add_path)
248 struct init_registry_key_context init_ctx;
250 if (regdb_key_exists(regdb, add_path)) {
254 init_ctx.add_path = add_path;
256 return ntstatus_to_werror(dbwrap_trans_do(regdb,
257 init_registry_key_action,
261 /***********************************************************************
262 Open the registry data in the tdb
263 ***********************************************************************/
265 static void regdb_ctr_add_value(struct regval_ctr *ctr,
266 struct builtin_regkey_value *value)
268 switch(value->type) {
270 regval_ctr_addvalue(ctr, value->valuename, REG_DWORD,
271 (uint8_t *)&value->data.dw_value,
276 regval_ctr_addvalue_sz(ctr, value->valuename,
281 DEBUG(0, ("regdb_ctr_add_value: invalid value type in "
282 "registry values [%d]\n", value->type));
286 static NTSTATUS init_registry_data_action(struct db_context *db,
290 TALLOC_CTX *frame = talloc_stackframe();
291 struct regval_ctr *values;
294 /* loop over all of the predefined paths and add each component */
296 for (i=0; builtin_registry_paths[i] != NULL; i++) {
297 if (regdb_key_exists(db, builtin_registry_paths[i])) {
300 status = werror_to_ntstatus(init_registry_key_internal(db,
301 builtin_registry_paths[i]));
302 if (!NT_STATUS_IS_OK(status)) {
307 /* loop over all of the predefined values and add each component */
309 for (i=0; builtin_registry_values[i].path != NULL; i++) {
312 werr = regval_ctr_init(frame, &values);
313 if (!W_ERROR_IS_OK(werr)) {
314 status = werror_to_ntstatus(werr);
318 regdb_fetch_values_internal(db,
319 builtin_registry_values[i].path,
322 /* preserve existing values across restarts. Only add new ones */
324 if (!regval_ctr_key_exists(values,
325 builtin_registry_values[i].valuename))
327 regdb_ctr_add_value(values,
328 &builtin_registry_values[i]);
329 regdb_store_values_internal(db,
330 builtin_registry_values[i].path,
336 status = NT_STATUS_OK;
344 WERROR init_registry_data(void)
347 TALLOC_CTX *frame = talloc_stackframe();
348 struct regval_ctr *values;
352 * First, check for the existence of the needed keys and values.
353 * If all do already exist, we can save the writes.
355 for (i=0; builtin_registry_paths[i] != NULL; i++) {
356 if (!regdb_key_exists(regdb, builtin_registry_paths[i])) {
361 for (i=0; builtin_registry_values[i].path != NULL; i++) {
362 werr = regval_ctr_init(frame, &values);
363 W_ERROR_NOT_OK_GOTO_DONE(werr);
365 regdb_fetch_values_internal(regdb,
366 builtin_registry_values[i].path,
368 if (!regval_ctr_key_exists(values,
369 builtin_registry_values[i].valuename))
384 * There are potentially quite a few store operations which are all
385 * indiviually wrapped in tdb transactions. Wrapping them in a single
386 * transaction gives just a single transaction_commit() to actually do
387 * its fsync()s. See tdb/common/transaction.c for info about nested
388 * transaction behaviour.
391 werr = ntstatus_to_werror(dbwrap_trans_do(regdb,
392 init_registry_data_action,
400 static int regdb_normalize_keynames_fn(struct db_record *rec,
403 TALLOC_CTX *mem_ctx = talloc_tos();
407 if (rec->key.dptr == NULL || rec->key.dsize == 0) {
411 keyname = strchr((const char *) rec->key.dptr, '/');
413 struct db_record new_rec;
415 keyname = talloc_string_sub(mem_ctx,
416 (const char *) rec->key.dptr,
420 DEBUG(2, ("regdb_normalize_keynames_fn: Convert %s to %s\n",
421 (const char *) rec->key.dptr,
424 new_rec.value = rec->value;
425 new_rec.key = string_term_tdb_data(keyname);
426 new_rec.private_data = rec->private_data;
428 /* Delete the original record and store the normalized key */
429 status = rec->delete_rec(rec);
430 if (!NT_STATUS_IS_OK(status)) {
431 DEBUG(0,("regdb_normalize_keynames_fn: "
432 "tdb_delete for [%s] failed!\n",
437 status = rec->store(&new_rec, new_rec.value, TDB_REPLACE);
438 if (!NT_STATUS_IS_OK(status)) {
439 DEBUG(0,("regdb_normalize_keynames_fn: "
440 "failed to store new record for [%s]!\n",
449 static WERROR regdb_store_regdb_version(uint32_t version)
452 const char *version_keyname = "INFO/version";
455 return WERR_CAN_NOT_COMPLETE;
458 status = dbwrap_trans_store_int32(regdb, version_keyname, version);
459 if (!NT_STATUS_IS_OK(status)) {
460 DEBUG(1, ("regdb_store_regdb_version: error storing %s = %d: %s\n",
461 version_keyname, version, nt_errstr(status)));
462 return ntstatus_to_werror(status);
464 DEBUG(10, ("regdb_store_regdb_version: stored %s = %d\n",
465 version_keyname, version));
470 static WERROR regdb_upgrade_v1_to_v2(void)
476 mem_ctx = talloc_stackframe();
477 if (mem_ctx == NULL) {
481 rc = regdb->traverse(regdb, regdb_normalize_keynames_fn, mem_ctx);
483 talloc_destroy(mem_ctx);
486 return WERR_REG_IO_FAILURE;
489 werr = regdb_store_regdb_version(REGVER_V2);
493 /***********************************************************************
494 Open the registry database
495 ***********************************************************************/
497 WERROR regdb_init(void)
499 const char *vstring = "INFO/version";
500 uint32 vers_id, expected_version;
504 DEBUG(10, ("regdb_init: incrementing refcount (%d->%d)\n",
505 regdb_refcount, regdb_refcount+1));
510 regdb = db_open(NULL, state_path("registry.tdb"), 0,
511 REG_TDB_FLAGS, O_RDWR, 0600);
513 regdb = db_open(NULL, state_path("registry.tdb"), 0,
514 REG_TDB_FLAGS, O_RDWR|O_CREAT, 0600);
516 werr = ntstatus_to_werror(map_nt_error_from_unix(errno));
517 DEBUG(1,("regdb_init: Failed to open registry %s (%s)\n",
518 state_path("registry.tdb"), strerror(errno) ));
522 DEBUG(10,("regdb_init: Successfully created registry tdb\n"));
526 DEBUG(10, ("regdb_init: registry db openend. refcount reset (%d)\n",
529 expected_version = REGVER_V2;
531 vers_id = dbwrap_fetch_int32(regdb, vstring);
533 DEBUG(10, ("regdb_init: registry version uninitialized "
534 "(got %d), initializing to version %d\n",
535 vers_id, expected_version));
537 werr = regdb_store_regdb_version(expected_version);
541 if (vers_id > expected_version || vers_id == 0) {
542 DEBUG(1, ("regdb_init: unknown registry version %d "
543 "(code version = %d), refusing initialization\n",
544 vers_id, expected_version));
545 return WERR_CAN_NOT_COMPLETE;
548 if (vers_id == REGVER_V1) {
549 DEBUG(10, ("regdb_init: got registry db version %d, upgrading "
550 "to version %d\n", REGVER_V1, REGVER_V2));
552 if (regdb->transaction_start(regdb) != 0) {
553 return WERR_REG_IO_FAILURE;
556 werr = regdb_upgrade_v1_to_v2();
557 if (!W_ERROR_IS_OK(werr)) {
558 regdb->transaction_cancel(regdb);
562 if (regdb->transaction_commit(regdb) != 0) {
563 return WERR_REG_IO_FAILURE;
569 /* future upgrade code should go here */
574 /***********************************************************************
575 Open the registry. Must already have been initialized by regdb_init()
576 ***********************************************************************/
578 WERROR regdb_open( void )
580 WERROR result = WERR_OK;
583 DEBUG(10, ("regdb_open: incrementing refcount (%d->%d)\n",
584 regdb_refcount, regdb_refcount+1));
591 regdb = db_open(NULL, state_path("registry.tdb"), 0,
592 REG_TDB_FLAGS, O_RDWR, 0600);
594 result = ntstatus_to_werror( map_nt_error_from_unix( errno ) );
595 DEBUG(0,("regdb_open: Failed to open %s! (%s)\n",
596 state_path("registry.tdb"), strerror(errno) ));
602 DEBUG(10, ("regdb_open: registry db opened. refcount reset (%d)\n",
608 /***********************************************************************
609 ***********************************************************************/
611 int regdb_close( void )
613 if (regdb_refcount == 0) {
619 DEBUG(10, ("regdb_close: decrementing refcount (%d->%d)\n",
620 regdb_refcount+1, regdb_refcount));
622 if ( regdb_refcount > 0 )
625 SMB_ASSERT( regdb_refcount >= 0 );
631 WERROR regdb_transaction_start(void)
633 return (regdb->transaction_start(regdb) == 0) ?
634 WERR_OK : WERR_REG_IO_FAILURE;
637 WERROR regdb_transaction_commit(void)
639 return (regdb->transaction_commit(regdb) == 0) ?
640 WERR_OK : WERR_REG_IO_FAILURE;
643 WERROR regdb_transaction_cancel(void)
645 return (regdb->transaction_cancel(regdb) == 0) ?
646 WERR_OK : WERR_REG_IO_FAILURE;
649 /***********************************************************************
650 return the tdb sequence number of the registry tdb.
651 this is an indicator for the content of the registry
652 having changed. it will change upon regdb_init, too, though.
653 ***********************************************************************/
654 int regdb_get_seqnum(void)
656 return regdb->get_seqnum(regdb);
660 static WERROR regdb_delete_key_with_prefix(struct db_context *db,
665 WERROR werr = WERR_NOMEM;
666 TALLOC_CTX *mem_ctx = talloc_stackframe();
668 if (keyname == NULL) {
669 werr = WERR_INVALID_PARAM;
673 if (prefix == NULL) {
674 path = discard_const_p(char, keyname);
676 path = talloc_asprintf(mem_ctx, "%s\\%s", prefix, keyname);
682 path = normalize_reg_path(mem_ctx, path);
687 werr = ntstatus_to_werror(dbwrap_delete_bystring(db, path));
689 /* treat "not" found" as ok */
690 if (W_ERROR_EQUAL(werr, WERR_NOT_FOUND)) {
695 talloc_free(mem_ctx);
700 static WERROR regdb_delete_values(struct db_context *db, const char *keyname)
702 return regdb_delete_key_with_prefix(db, keyname, REG_VALUE_PREFIX);
705 static WERROR regdb_delete_secdesc(struct db_context *db, const char *keyname)
707 return regdb_delete_key_with_prefix(db, keyname, REG_SECDESC_PREFIX);
710 static WERROR regdb_delete_subkeylist(struct db_context *db, const char *keyname)
712 return regdb_delete_key_with_prefix(db, keyname, NULL);
715 static WERROR regdb_delete_sorted_subkeys(struct db_context *db,
718 return regdb_delete_key_with_prefix(db, keyname, REG_SORTED_SUBKEYS_PREFIX);
722 static WERROR regdb_delete_key_lists(struct db_context *db, const char *keyname)
726 werr = regdb_delete_values(db, keyname);
727 if (!W_ERROR_IS_OK(werr)) {
728 DEBUG(1, (__location__ " Deleting %s\\%s failed: %s\n",
729 REG_VALUE_PREFIX, keyname, win_errstr(werr)));
733 werr = regdb_delete_secdesc(db, keyname);
734 if (!W_ERROR_IS_OK(werr)) {
735 DEBUG(1, (__location__ " Deleting %s\\%s failed: %s\n",
736 REG_SECDESC_PREFIX, keyname, win_errstr(werr)));
740 werr = regdb_delete_subkeylist(db, keyname);
741 if (!W_ERROR_IS_OK(werr)) {
742 DEBUG(1, (__location__ " Deleting %s failed: %s\n",
743 keyname, win_errstr(werr)));
751 /***********************************************************************
752 Add subkey strings to the registry tdb under a defined key
753 fmt is the same format as tdb_pack except this function only supports
755 ***********************************************************************/
757 static WERROR regdb_store_keys_internal2(struct db_context *db,
759 struct regsubkey_ctr *ctr)
762 uint8 *buffer = NULL;
765 uint32 num_subkeys = regsubkey_ctr_numkeys(ctr);
766 char *keyname = NULL;
767 TALLOC_CTX *ctx = talloc_stackframe();
771 werr = WERR_INVALID_PARAM;
775 keyname = talloc_strdup(ctx, key);
781 keyname = normalize_reg_path(ctx, keyname);
787 /* allocate some initial memory */
789 buffer = (uint8 *)SMB_MALLOC(1024);
790 if (buffer == NULL) {
797 /* store the number of subkeys */
799 len += tdb_pack(buffer+len, buflen-len, "d", num_subkeys);
801 /* pack all the strings */
803 for (i=0; i<num_subkeys; i++) {
806 thistime = tdb_pack(buffer+len, buflen-len, "f",
807 regsubkey_ctr_specific_key(ctr, i));
808 if (len+thistime > buflen) {
811 * tdb_pack hasn't done anything because of the short
812 * buffer, allocate extra space.
814 buffer = SMB_REALLOC_ARRAY(buffer, uint8_t,
817 DEBUG(0, ("regdb_store_keys: Failed to realloc "
818 "memory of size [%u]\n",
819 (unsigned int)(len+thistime)*2));
823 buflen = (len+thistime)*2;
824 thistime2 = tdb_pack(
825 buffer+len, buflen-len, "f",
826 regsubkey_ctr_specific_key(ctr, i));
827 if (thistime2 != thistime) {
828 DEBUG(0, ("tdb_pack failed\n"));
829 werr = WERR_CAN_NOT_COMPLETE;
836 /* finally write out the data */
840 werr = ntstatus_to_werror(dbwrap_store_bystring(db, keyname, dbuf,
842 W_ERROR_NOT_OK_GOTO_DONE(werr);
845 * recreate the sorted subkey cache for regdb_key_exists()
847 werr = ntstatus_to_werror(create_sorted_subkeys(keyname));
855 /***********************************************************************
856 Store the new subkey record and create any child key records that
857 do not currently exist
858 ***********************************************************************/
860 struct regdb_store_keys_context {
862 struct regsubkey_ctr *ctr;
865 static NTSTATUS regdb_store_keys_action(struct db_context *db,
868 struct regdb_store_keys_context *store_ctx;
872 struct regsubkey_ctr *subkeys = NULL, *old_subkeys = NULL;
873 char *oldkeyname = NULL;
874 TALLOC_CTX *mem_ctx = talloc_stackframe();
876 store_ctx = (struct regdb_store_keys_context *)private_data;
879 * Re-fetch the old keys inside the transaction
882 werr = regsubkey_ctr_init(mem_ctx, &old_subkeys);
883 W_ERROR_NOT_OK_GOTO_DONE(werr);
885 werr = regdb_fetch_keys_internal(db, store_ctx->key, old_subkeys);
886 if (!W_ERROR_IS_OK(werr) &&
887 !W_ERROR_EQUAL(werr, WERR_NOT_FOUND))
893 * Make the store operation as safe as possible without transactions:
895 * (1) For each subkey removed from ctr compared with old_subkeys:
897 * (a) First delete the value db entry.
899 * (b) Next delete the secdesc db record.
901 * (c) Then delete the subkey list entry.
903 * (2) Now write the list of subkeys of the parent key,
904 * deleting removed entries and adding new ones.
906 * (3) Finally create the subkey list entries for the added keys.
908 * This way if we crash half-way in between deleting the subkeys
909 * and storing the parent's list of subkeys, no old data can pop up
910 * out of the blue when re-adding keys later on.
913 /* (1) delete removed keys' lists (values/secdesc/subkeys) */
915 num_subkeys = regsubkey_ctr_numkeys(old_subkeys);
916 for (i=0; i<num_subkeys; i++) {
917 oldkeyname = regsubkey_ctr_specific_key(old_subkeys, i);
919 if (regsubkey_ctr_key_exists(store_ctx->ctr, oldkeyname)) {
921 * It's still around, don't delete
926 path = talloc_asprintf(mem_ctx, "%s\\%s", store_ctx->key,
933 werr = regdb_delete_key_lists(db, path);
934 W_ERROR_NOT_OK_GOTO_DONE(werr);
939 TALLOC_FREE(old_subkeys);
941 /* (2) store the subkey list for the parent */
943 werr = regdb_store_keys_internal2(db, store_ctx->key, store_ctx->ctr);
944 if (!W_ERROR_IS_OK(werr)) {
945 DEBUG(0,("regdb_store_keys: Failed to store new subkey list "
946 "for parent [%s]: %s\n", store_ctx->key,
951 /* (3) now create records for any subkeys that don't already exist */
953 num_subkeys = regsubkey_ctr_numkeys(store_ctx->ctr);
955 if (num_subkeys == 0) {
956 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
957 W_ERROR_NOT_OK_GOTO_DONE(werr);
959 werr = regdb_store_keys_internal2(db, store_ctx->key, subkeys);
960 if (!W_ERROR_IS_OK(werr)) {
961 DEBUG(0,("regdb_store_keys: Failed to store "
962 "new record for key [%s]: %s\n",
963 store_ctx->key, win_errstr(werr)));
966 TALLOC_FREE(subkeys);
969 for (i=0; i<num_subkeys; i++) {
970 path = talloc_asprintf(mem_ctx, "%s\\%s", store_ctx->key,
971 regsubkey_ctr_specific_key(store_ctx->ctr, i));
976 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
977 W_ERROR_NOT_OK_GOTO_DONE(werr);
979 werr = regdb_fetch_keys_internal(db, path, subkeys);
980 if (!W_ERROR_IS_OK(werr)) {
981 /* create a record with 0 subkeys */
982 werr = regdb_store_keys_internal2(db, path, subkeys);
983 if (!W_ERROR_IS_OK(werr)) {
984 DEBUG(0,("regdb_store_keys: Failed to store "
985 "new record for key [%s]: %s\n", path,
991 TALLOC_FREE(subkeys);
998 talloc_free(mem_ctx);
999 return werror_to_ntstatus(werr);
1002 static bool regdb_store_keys_internal(struct db_context *db, const char *key,
1003 struct regsubkey_ctr *ctr)
1005 int num_subkeys, old_num_subkeys, i;
1006 struct regsubkey_ctr *old_subkeys = NULL;
1007 TALLOC_CTX *ctx = talloc_stackframe();
1010 struct regdb_store_keys_context store_ctx;
1012 if (!regdb_key_is_base_key(key) && !regdb_key_exists(db, key)) {
1017 * fetch a list of the old subkeys so we can determine if anything has
1021 werr = regsubkey_ctr_init(ctx, &old_subkeys);
1022 if (!W_ERROR_IS_OK(werr)) {
1023 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
1027 werr = regdb_fetch_keys_internal(db, key, old_subkeys);
1028 if (!W_ERROR_IS_OK(werr) &&
1029 !W_ERROR_EQUAL(werr, WERR_NOT_FOUND))
1034 num_subkeys = regsubkey_ctr_numkeys(ctr);
1035 old_num_subkeys = regsubkey_ctr_numkeys(old_subkeys);
1036 if ((num_subkeys && old_num_subkeys) &&
1037 (num_subkeys == old_num_subkeys)) {
1039 for (i = 0; i < num_subkeys; i++) {
1040 if (strcmp(regsubkey_ctr_specific_key(ctr, i),
1041 regsubkey_ctr_specific_key(old_subkeys, i))
1047 if (i == num_subkeys) {
1049 * Nothing changed, no point to even start a tdb
1058 TALLOC_FREE(old_subkeys);
1060 store_ctx.key = key;
1061 store_ctx.ctr = ctr;
1063 werr = ntstatus_to_werror(dbwrap_trans_do(db,
1064 regdb_store_keys_action,
1067 ret = W_ERROR_IS_OK(werr);
1075 bool regdb_store_keys(const char *key, struct regsubkey_ctr *ctr)
1077 return regdb_store_keys_internal(regdb, key, ctr);
1081 * create a subkey of a given key
1084 struct regdb_create_subkey_context {
1089 static NTSTATUS regdb_create_subkey_action(struct db_context *db,
1093 struct regdb_create_subkey_context *create_ctx;
1094 struct regsubkey_ctr *subkeys;
1095 TALLOC_CTX *mem_ctx = talloc_stackframe();
1097 create_ctx = (struct regdb_create_subkey_context *)private_data;
1099 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
1100 W_ERROR_NOT_OK_GOTO_DONE(werr);
1102 werr = regdb_fetch_keys_internal(db, create_ctx->key, subkeys);
1103 W_ERROR_NOT_OK_GOTO_DONE(werr);
1105 werr = regsubkey_ctr_addkey(subkeys, create_ctx->subkey);
1106 W_ERROR_NOT_OK_GOTO_DONE(werr);
1108 werr = regdb_store_keys_internal2(db, create_ctx->key, subkeys);
1109 if (!W_ERROR_IS_OK(werr)) {
1110 DEBUG(0, (__location__ " failed to store new subkey list for "
1111 "parent key %s: %s\n", create_ctx->key,
1116 talloc_free(mem_ctx);
1117 return werror_to_ntstatus(werr);
1120 static WERROR regdb_create_subkey(const char *key, const char *subkey)
1123 struct regsubkey_ctr *subkeys;
1124 TALLOC_CTX *mem_ctx = talloc_stackframe();
1125 struct regdb_create_subkey_context create_ctx;
1127 if (!regdb_key_is_base_key(key) && !regdb_key_exists(regdb, key)) {
1128 werr = WERR_NOT_FOUND;
1132 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
1133 W_ERROR_NOT_OK_GOTO_DONE(werr);
1135 werr = regdb_fetch_keys_internal(regdb, key, subkeys);
1136 W_ERROR_NOT_OK_GOTO_DONE(werr);
1138 if (regsubkey_ctr_key_exists(subkeys, subkey)) {
1143 talloc_free(subkeys);
1145 create_ctx.key = key;
1146 create_ctx.subkey = subkey;
1148 werr = ntstatus_to_werror(dbwrap_trans_do(regdb,
1149 regdb_create_subkey_action,
1153 talloc_free(mem_ctx);
1158 * create a subkey of a given key
1161 struct regdb_delete_subkey_context {
1167 static NTSTATUS regdb_delete_subkey_action(struct db_context *db,
1171 struct regdb_delete_subkey_context *delete_ctx;
1172 struct regsubkey_ctr *subkeys;
1173 TALLOC_CTX *mem_ctx = talloc_stackframe();
1175 delete_ctx = (struct regdb_delete_subkey_context *)private_data;
1177 werr = regdb_delete_key_lists(db, delete_ctx->path);
1178 W_ERROR_NOT_OK_GOTO_DONE(werr);
1180 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
1181 W_ERROR_NOT_OK_GOTO_DONE(werr);
1183 werr = regdb_fetch_keys_internal(db, delete_ctx->key, subkeys);
1184 W_ERROR_NOT_OK_GOTO_DONE(werr);
1186 werr = regsubkey_ctr_delkey(subkeys, delete_ctx->subkey);
1187 W_ERROR_NOT_OK_GOTO_DONE(werr);
1189 werr = regdb_store_keys_internal2(db, delete_ctx->key, subkeys);
1190 if (!W_ERROR_IS_OK(werr)) {
1191 DEBUG(0, (__location__ " failed to store new subkey_list for "
1192 "parent key %s: %s\n", delete_ctx->key,
1197 talloc_free(mem_ctx);
1198 return werror_to_ntstatus(werr);
1201 static WERROR regdb_delete_subkey(const char *key, const char *subkey)
1205 struct regdb_delete_subkey_context delete_ctx;
1206 TALLOC_CTX *mem_ctx = talloc_stackframe();
1208 if (!regdb_key_is_base_key(key) && !regdb_key_exists(regdb, key)) {
1209 werr = WERR_NOT_FOUND;
1213 path = talloc_asprintf(mem_ctx, "%s\\%s", key, subkey);
1219 if (!regdb_key_exists(regdb, path)) {
1224 delete_ctx.key = key;
1225 delete_ctx.subkey = subkey;
1226 delete_ctx.path = path;
1228 werr = ntstatus_to_werror(dbwrap_trans_do(regdb,
1229 regdb_delete_subkey_action,
1233 talloc_free(mem_ctx);
1237 static TDB_DATA regdb_fetch_key_internal(struct db_context *db,
1238 TALLOC_CTX *mem_ctx, const char *key)
1243 path = normalize_reg_path(mem_ctx, key);
1245 return make_tdb_data(NULL, 0);
1248 data = dbwrap_fetch_bystring(db, mem_ctx, path);
1256 * check whether a given key name represents a base key,
1257 * i.e one without a subkey separator ('\').
1259 static bool regdb_key_is_base_key(const char *key)
1261 TALLOC_CTX *mem_ctx = talloc_stackframe();
1269 path = normalize_reg_path(mem_ctx, key);
1271 DEBUG(0, ("out of memory! (talloc failed)\n"));
1275 if (*path == '\0') {
1279 ret = (strrchr(path, '\\') == NULL);
1282 TALLOC_FREE(mem_ctx);
1287 * regdb_key_exists() is a very frequent operation. It can be quite
1288 * time-consuming to fully fetch the parent's subkey list, talloc_strdup all
1289 * subkeys and then compare the keyname linearly to all the parent's subkeys.
1291 * The following code tries to make this operation as efficient as possible:
1292 * Per registry key we create a list of subkeys that is very efficient to
1293 * search for existence of a subkey. Its format is:
1295 * 4 bytes num_subkeys
1296 * 4*num_subkey bytes offset into the string array
1297 * then follows a sorted list of subkeys in uppercase
1299 * This record is created by create_sorted_subkeys() on demand if it does not
1300 * exist. scan_parent_subkeys() uses regdb->parse_record to search the sorted
1301 * list, the parsing code and the binary search can be found in
1302 * parent_subkey_scanner. The code uses parse_record() to avoid a memcpy of
1303 * the potentially large subkey record.
1305 * The sorted subkey record is deleted in regdb_store_keys_internal2 and
1306 * recreated on demand.
1309 static int cmp_keynames(char **p1, char **p2)
1311 return strcasecmp_m(*p1, *p2);
1314 struct create_sorted_subkeys_context {
1316 const char *sorted_keyname;
1319 static NTSTATUS create_sorted_subkeys_action(struct db_context *db,
1322 char **sorted_subkeys;
1323 struct regsubkey_ctr *ctr;
1330 struct create_sorted_subkeys_context *sorted_ctx;
1332 sorted_ctx = (struct create_sorted_subkeys_context *)private_data;
1335 * In this function, we only treat failing of the actual write to
1336 * the db as a real error. All preliminary errors, at a stage when
1337 * nothing has been written to the DB yet are treated as success
1338 * to be committed (as an empty transaction).
1340 * The reason is that this (disposable) call might be nested in other
1341 * transactions. Doing a cancel here would destroy the possibility of
1342 * a transaction_commit for transactions that we might be wrapped in.
1345 status = werror_to_ntstatus(regsubkey_ctr_init(talloc_tos(), &ctr));
1346 if (!NT_STATUS_IS_OK(status)) {
1347 /* don't treat this as an error */
1348 status = NT_STATUS_OK;
1352 status = werror_to_ntstatus(regdb_fetch_keys_internal(db,
1355 if (!NT_STATUS_IS_OK(status)) {
1356 /* don't treat this as an error */
1357 status = NT_STATUS_OK;
1361 num_subkeys = regsubkey_ctr_numkeys(ctr);
1362 sorted_subkeys = talloc_array(ctr, char *, num_subkeys);
1363 if (sorted_subkeys == NULL) {
1364 /* don't treat this as an error */
1368 len = 4 + 4*num_subkeys;
1370 for (i = 0; i < num_subkeys; i++) {
1371 sorted_subkeys[i] = talloc_strdup_upper(sorted_subkeys,
1372 regsubkey_ctr_specific_key(ctr, i));
1373 if (sorted_subkeys[i] == NULL) {
1374 /* don't treat this as an error */
1377 len += strlen(sorted_subkeys[i])+1;
1380 TYPESAFE_QSORT(sorted_subkeys, num_subkeys, cmp_keynames);
1382 buf = talloc_array(ctr, char, len);
1384 /* don't treat this as an error */
1387 p = buf + 4 + 4*num_subkeys;
1389 SIVAL(buf, 0, num_subkeys);
1391 for (i=0; i < num_subkeys; i++) {
1392 ptrdiff_t offset = p - buf;
1393 SIVAL(buf, 4 + 4*i, offset);
1394 strlcpy(p, sorted_subkeys[i], len-offset);
1395 p += strlen(sorted_subkeys[i]) + 1;
1398 status = dbwrap_store_bystring(
1399 db, sorted_ctx->sorted_keyname, make_tdb_data((uint8_t *)buf,
1408 static NTSTATUS create_sorted_subkeys_internal(const char *key,
1409 const char *sorted_keyname)
1412 struct create_sorted_subkeys_context sorted_ctx;
1414 sorted_ctx.key = key;
1415 sorted_ctx.sorted_keyname = sorted_keyname;
1417 status = dbwrap_trans_do(regdb,
1418 create_sorted_subkeys_action,
1424 static NTSTATUS create_sorted_subkeys(const char *key)
1426 char *sorted_subkeys_keyname;
1429 sorted_subkeys_keyname = talloc_asprintf(talloc_tos(), "%s\\%s",
1430 REG_SORTED_SUBKEYS_PREFIX,
1432 if (sorted_subkeys_keyname == NULL) {
1433 status = NT_STATUS_NO_MEMORY;
1437 status = create_sorted_subkeys_internal(key, sorted_subkeys_keyname);
1443 struct scan_subkey_state {
1449 static int parent_subkey_scanner(TDB_DATA key, TDB_DATA data,
1452 struct scan_subkey_state *state =
1453 (struct scan_subkey_state *)private_data;
1454 uint32_t num_subkeys;
1457 if (data.dsize < sizeof(uint32_t)) {
1461 state->scanned = true;
1462 state->found = false;
1464 tdb_unpack(data.dptr, data.dsize, "d", &num_subkeys);
1470 uint32_t idx = (l+u)/2;
1471 char *s = (char *)data.dptr + IVAL(data.dptr, 4 + 4*idx);
1472 int comparison = strcmp(state->name, s);
1474 if (comparison < 0) {
1476 } else if (comparison > 0) {
1479 state->found = true;
1486 static bool scan_parent_subkeys(struct db_context *db, const char *parent,
1491 struct scan_subkey_state state = { 0, };
1492 bool result = false;
1497 path = normalize_reg_path(talloc_tos(), parent);
1502 key = talloc_asprintf(talloc_tos(), "%s\\%s",
1503 REG_SORTED_SUBKEYS_PREFIX, path);
1508 state.name = talloc_strdup_upper(talloc_tos(), name);
1509 if (state.name == NULL) {
1512 state.scanned = false;
1514 res = db->parse_record(db, string_term_tdb_data(key),
1515 parent_subkey_scanner, &state);
1517 if (state.scanned) {
1518 result = state.found;
1522 res = db->transaction_start(db);
1524 DEBUG(0, ("error starting transaction\n"));
1528 DEBUG(2, (__location__ " WARNING: recreating the sorted "
1529 "subkeys cache for key '%s' from scan_parent_subkeys "
1530 "this should not happen (too frequently)...\n",
1533 status = create_sorted_subkeys_internal(path, key);
1534 if (!NT_STATUS_IS_OK(status)) {
1535 res = db->transaction_cancel(db);
1537 smb_panic("Failed to cancel transaction.");
1542 res = db->parse_record(db, string_term_tdb_data(key),
1543 parent_subkey_scanner, &state);
1544 if ((res == 0) && (state.scanned)) {
1545 result = state.found;
1548 res = db->transaction_commit(db);
1550 DEBUG(0, ("error committing transaction\n"));
1557 TALLOC_FREE(state.name);
1562 * Check for the existence of a key.
1564 * Existence of a key is authoritatively defined by its
1565 * existence in the list of subkeys of its parent key.
1566 * The exeption of this are keys without a parent key,
1567 * i.e. the "base" keys (HKLM, HKCU, ...).
1569 static bool regdb_key_exists(struct db_context *db, const char *key)
1571 TALLOC_CTX *mem_ctx = talloc_stackframe();
1580 path = normalize_reg_path(mem_ctx, key);
1582 DEBUG(0, ("out of memory! (talloc failed)\n"));
1586 if (*path == '\0') {
1590 p = strrchr(path, '\\');
1592 /* this is a base key */
1593 value = regdb_fetch_key_internal(db, mem_ctx, path);
1594 ret = (value.dptr != NULL);
1597 ret = scan_parent_subkeys(db, path, p+1);
1601 TALLOC_FREE(mem_ctx);
1606 /***********************************************************************
1607 Retrieve an array of strings containing subkeys. Memory should be
1608 released by the caller.
1609 ***********************************************************************/
1611 static WERROR regdb_fetch_keys_internal(struct db_context *db, const char *key,
1612 struct regsubkey_ctr *ctr)
1620 TALLOC_CTX *frame = talloc_stackframe();
1623 DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key ? key : "NULL"));
1625 if (!regdb_key_exists(db, key)) {
1626 DEBUG(10, ("key [%s] not found\n", key));
1627 werr = WERR_NOT_FOUND;
1631 werr = regsubkey_ctr_set_seqnum(ctr, db->get_seqnum(db));
1632 W_ERROR_NOT_OK_GOTO_DONE(werr);
1634 value = regdb_fetch_key_internal(db, frame, key);
1636 if (value.dsize == 0 || value.dptr == NULL) {
1637 DEBUG(10, ("regdb_fetch_keys: no subkeys found for key [%s]\n",
1643 buflen = value.dsize;
1644 len = tdb_unpack( buf, buflen, "d", &num_items);
1645 if (len == (uint32_t)-1) {
1646 werr = WERR_NOT_FOUND;
1650 werr = regsubkey_ctr_reinit(ctr);
1651 W_ERROR_NOT_OK_GOTO_DONE(werr);
1653 for (i=0; i<num_items; i++) {
1654 len += tdb_unpack(buf+len, buflen-len, "f", subkeyname);
1655 werr = regsubkey_ctr_addkey(ctr, subkeyname);
1656 if (!W_ERROR_IS_OK(werr)) {
1657 DEBUG(5, ("regdb_fetch_keys: regsubkey_ctr_addkey "
1658 "failed: %s\n", win_errstr(werr)));
1664 DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items));
1671 int regdb_fetch_keys(const char *key, struct regsubkey_ctr *ctr)
1675 werr = regdb_fetch_keys_internal(regdb, key, ctr);
1676 if (!W_ERROR_IS_OK(werr)) {
1680 return regsubkey_ctr_numkeys(ctr);
1683 /****************************************************************************
1684 Unpack a list of registry values frem the TDB
1685 ***************************************************************************/
1687 static int regdb_unpack_values(struct regval_ctr *values, uint8 *buf, int buflen)
1694 uint32 num_values = 0;
1697 /* loop and unpack the rest of the registry values */
1699 len += tdb_unpack(buf+len, buflen-len, "d", &num_values);
1701 for ( i=0; i<num_values; i++ ) {
1702 /* unpack the next regval */
1707 valuename[0] = '\0';
1708 len += tdb_unpack(buf+len, buflen-len, "fdB",
1714 regval_ctr_addvalue(values, valuename, type,
1715 (uint8_t *)data_p, size);
1716 SAFE_FREE(data_p); /* 'B' option to tdb_unpack does a malloc() */
1718 DEBUG(8,("specific: [%s], len: %d\n", valuename, size));
1724 /****************************************************************************
1725 Pack all values in all printer keys
1726 ***************************************************************************/
1728 static int regdb_pack_values(struct regval_ctr *values, uint8 *buf, int buflen)
1732 struct regval_blob *val;
1738 num_values = regval_ctr_numvals( values );
1740 /* pack the number of values first */
1742 len += tdb_pack( buf+len, buflen-len, "d", num_values );
1744 /* loop over all values */
1746 for ( i=0; i<num_values; i++ ) {
1747 val = regval_ctr_specific_value( values, i );
1748 len += tdb_pack(buf+len, buflen-len, "fdB",
1752 regval_data_p(val) );
1758 /***********************************************************************
1759 Retrieve an array of strings containing subkeys. Memory should be
1760 released by the caller.
1761 ***********************************************************************/
1763 static int regdb_fetch_values_internal(struct db_context *db, const char* key,
1764 struct regval_ctr *values)
1766 char *keystr = NULL;
1767 TALLOC_CTX *ctx = talloc_stackframe();
1772 DEBUG(10,("regdb_fetch_values: Looking for value of key [%s] \n", key));
1774 if (!regdb_key_exists(db, key)) {
1778 keystr = talloc_asprintf(ctx, "%s\\%s", REG_VALUE_PREFIX, key);
1783 werr = regval_ctr_set_seqnum(values, db->get_seqnum(db));
1784 W_ERROR_NOT_OK_GOTO_DONE(werr);
1786 value = regdb_fetch_key_internal(db, ctx, keystr);
1789 /* all keys have zero values by default */
1793 regdb_unpack_values(values, value.dptr, value.dsize);
1794 ret = regval_ctr_numvals(values);
1801 int regdb_fetch_values(const char* key, struct regval_ctr *values)
1803 return regdb_fetch_values_internal(regdb, key, values);
1806 static bool regdb_store_values_internal(struct db_context *db, const char *key,
1807 struct regval_ctr *values)
1809 TDB_DATA old_data, data;
1810 char *keystr = NULL;
1811 TALLOC_CTX *ctx = talloc_stackframe();
1814 bool result = false;
1816 DEBUG(10,("regdb_store_values: Looking for value of key [%s] \n", key));
1818 if (!regdb_key_exists(db, key)) {
1824 len = regdb_pack_values(values, data.dptr, data.dsize);
1826 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
1830 data.dptr = talloc_array(ctx, uint8, len);
1833 len = regdb_pack_values(values, data.dptr, data.dsize);
1835 SMB_ASSERT( len == data.dsize );
1837 keystr = talloc_asprintf(ctx, "%s\\%s", REG_VALUE_PREFIX, key );
1841 keystr = normalize_reg_path(ctx, keystr);
1846 old_data = dbwrap_fetch_bystring(db, ctx, keystr);
1848 if ((old_data.dptr != NULL)
1849 && (old_data.dsize == data.dsize)
1850 && (memcmp(old_data.dptr, data.dptr, data.dsize) == 0))
1856 status = dbwrap_trans_store_bystring(db, keystr, data, TDB_REPLACE);
1858 result = NT_STATUS_IS_OK(status);
1865 bool regdb_store_values(const char *key, struct regval_ctr *values)
1867 return regdb_store_values_internal(regdb, key, values);
1870 static WERROR regdb_get_secdesc(TALLOC_CTX *mem_ctx, const char *key,
1871 struct security_descriptor **psecdesc)
1876 TALLOC_CTX *tmp_ctx = talloc_stackframe();
1877 WERROR err = WERR_OK;
1879 DEBUG(10, ("regdb_get_secdesc: Getting secdesc of key [%s]\n", key));
1881 if (!regdb_key_exists(regdb, key)) {
1886 tdbkey = talloc_asprintf(tmp_ctx, "%s\\%s", REG_SECDESC_PREFIX, key);
1887 if (tdbkey == NULL) {
1892 tdbkey = normalize_reg_path(tmp_ctx, tdbkey);
1893 if (tdbkey == NULL) {
1898 data = dbwrap_fetch_bystring(regdb, tmp_ctx, tdbkey);
1899 if (data.dptr == NULL) {
1904 status = unmarshall_sec_desc(mem_ctx, (uint8 *)data.dptr, data.dsize,
1907 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MEMORY)) {
1909 } else if (!NT_STATUS_IS_OK(status)) {
1910 err = WERR_REG_CORRUPT;
1914 TALLOC_FREE(tmp_ctx);
1918 static WERROR regdb_set_secdesc(const char *key,
1919 struct security_descriptor *secdesc)
1921 TALLOC_CTX *mem_ctx = talloc_stackframe();
1923 WERROR err = WERR_NOMEM;
1926 if (!regdb_key_exists(regdb, key)) {
1931 tdbkey = talloc_asprintf(mem_ctx, "%s\\%s", REG_SECDESC_PREFIX, key);
1932 if (tdbkey == NULL) {
1936 tdbkey = normalize_reg_path(mem_ctx, tdbkey);
1937 if (tdbkey == NULL) {
1942 if (secdesc == NULL) {
1943 /* assuming a delete */
1944 err = ntstatus_to_werror(dbwrap_trans_delete_bystring(regdb,
1949 err = ntstatus_to_werror(marshall_sec_desc(mem_ctx, secdesc,
1952 W_ERROR_NOT_OK_GOTO_DONE(err);
1954 err = ntstatus_to_werror(dbwrap_trans_store_bystring(regdb, tdbkey,
1958 TALLOC_FREE(mem_ctx);
1962 bool regdb_subkeys_need_update(struct regsubkey_ctr *subkeys)
1964 return (regdb_get_seqnum() != regsubkey_ctr_get_seqnum(subkeys));
1967 bool regdb_values_need_update(struct regval_ctr *values)
1969 return (regdb_get_seqnum() != regval_ctr_get_seqnum(values));
1973 * Table of function pointers for default access
1976 struct registry_ops regdb_ops = {
1977 .fetch_subkeys = regdb_fetch_keys,
1978 .fetch_values = regdb_fetch_values,
1979 .store_subkeys = regdb_store_keys,
1980 .store_values = regdb_store_values,
1981 .create_subkey = regdb_create_subkey,
1982 .delete_subkey = regdb_delete_subkey,
1983 .get_secdesc = regdb_get_secdesc,
1984 .set_secdesc = regdb_set_secdesc,
1985 .subkeys_need_update = regdb_subkeys_need_update,
1986 .values_need_update = regdb_values_need_update