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"
35 #define DBGC_CLASS DBGC_REGISTRY
37 static struct db_context *regdb = NULL;
38 static int regdb_refcount;
40 static bool regdb_key_exists(struct db_context *db, const char *key);
41 static bool regdb_key_is_base_key(const char *key);
42 static WERROR regdb_fetch_keys_internal(struct db_context *db, const char *key,
43 struct regsubkey_ctr *ctr);
44 static bool regdb_store_keys_internal(struct db_context *db, const char *key,
45 struct regsubkey_ctr *ctr);
46 static int regdb_fetch_values_internal(struct db_context *db, const char* key,
47 struct regval_ctr *values);
48 static bool regdb_store_values_internal(struct db_context *db, const char *key,
49 struct regval_ctr *values);
51 /* List the deepest path into the registry. All part components will be created.*/
53 /* If you want to have a part of the path controlled by the tdb and part by
54 a virtual registry db (e.g. printing), then you have to list the deepest path.
55 For example,"HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion/Print"
56 allows the reg_db backend to handle everything up to
57 "HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion" and then we'll hook
58 the reg_printing backend onto the last component of the path (see
59 KEY_PRINTING_2K in include/rpc_reg.h) --jerry */
61 static const char *builtin_registry_paths[] = {
65 KEY_PRINTING "\\Forms",
66 KEY_PRINTING "\\Printers",
67 KEY_PRINTING "\\Environments\\Windows NT x86\\Print Processors\\winprint",
74 KEY_SAMBA_GROUP_POLICY,
75 KEY_GP_MACHINE_POLICY,
76 KEY_GP_MACHINE_WIN_POLICY,
79 KEY_GP_USER_WIN_POLICY,
80 "HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon\\GPExtensions",
81 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Print\\Monitors",
83 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\DefaultUserConfiguration",
92 struct builtin_regkey_value {
94 const char *valuename;
102 static struct builtin_regkey_value builtin_registry_values[] = {
103 { KEY_PRINTING_PORTS,
104 SAMBA_PRINTER_PORT_NAME, REG_SZ, { "" } },
106 "DefaultSpoolDirectory", REG_SZ, { "C:\\Windows\\System32\\Spool\\Printers" } },
108 "DisplayName", REG_SZ, { "Event Log" } },
110 "ErrorControl", REG_DWORD, { (char*)0x00000001 } },
111 { NULL, NULL, 0, { NULL } }
115 * Initialize a key in the registry:
116 * create each component key of the specified path.
118 static WERROR init_registry_key_internal(struct db_context *db,
119 const char *add_path)
122 TALLOC_CTX *frame = talloc_stackframe();
125 char *remaining = NULL;
128 struct regsubkey_ctr *subkeys;
131 DEBUG(6, ("init_registry_key: Adding [%s]\n", add_path));
133 path = talloc_strdup(frame, add_path);
134 base = talloc_strdup(frame, "");
135 if (!path || !base) {
141 while (next_token_talloc(frame, &p, &keyname, "\\")) {
143 /* build up the registry path from the components */
146 base = talloc_asprintf(frame, "%s\\", base);
152 base = talloc_asprintf_append(base, "%s", keyname);
158 /* get the immediate subkeyname (if we have one ) */
160 subkeyname = talloc_strdup(frame, "");
166 remaining = talloc_strdup(frame, p);
173 if (!next_token_talloc(frame, &p2,
176 subkeyname = talloc_strdup(frame,p2);
184 DEBUG(10,("init_registry_key: Storing key [%s] with "
185 "subkey [%s]\n", base,
186 *subkeyname ? subkeyname : "NULL"));
188 /* we don't really care if the lookup succeeds or not
189 * since we are about to update the record.
190 * We just want any subkeys already present */
192 werr = regsubkey_ctr_init(frame, &subkeys);
193 if (!W_ERROR_IS_OK(werr)) {
194 DEBUG(0,("talloc() failure!\n"));
198 werr = regdb_fetch_keys_internal(db, base, subkeys);
199 if (!W_ERROR_IS_OK(werr) &&
200 !W_ERROR_EQUAL(werr, WERR_NOT_FOUND))
206 werr = regsubkey_ctr_addkey(subkeys, subkeyname);
207 if (!W_ERROR_IS_OK(werr)) {
211 if (!regdb_store_keys_internal(db, base, subkeys)) {
212 werr = WERR_CAN_NOT_COMPLETE;
224 struct init_registry_key_context {
225 const char *add_path;
228 static NTSTATUS init_registry_key_action(struct db_context *db,
231 struct init_registry_key_context *init_ctx =
232 (struct init_registry_key_context *)private_data;
234 return werror_to_ntstatus(init_registry_key_internal(
235 db, init_ctx->add_path));
239 * Initialize a key in the registry:
240 * create each component key of the specified path,
241 * wrapped in one db transaction.
243 WERROR init_registry_key(const char *add_path)
245 struct init_registry_key_context init_ctx;
247 if (regdb_key_exists(regdb, add_path)) {
251 init_ctx.add_path = add_path;
253 return ntstatus_to_werror(dbwrap_trans_do(regdb,
254 init_registry_key_action,
258 /***********************************************************************
259 Open the registry data in the tdb
260 ***********************************************************************/
262 static void regdb_ctr_add_value(struct regval_ctr *ctr,
263 struct builtin_regkey_value *value)
265 switch(value->type) {
267 regval_ctr_addvalue(ctr, value->valuename, REG_DWORD,
268 (uint8_t *)&value->data.dw_value,
273 regval_ctr_addvalue_sz(ctr, value->valuename,
278 DEBUG(0, ("regdb_ctr_add_value: invalid value type in "
279 "registry values [%d]\n", value->type));
283 static NTSTATUS init_registry_data_action(struct db_context *db,
287 TALLOC_CTX *frame = talloc_stackframe();
288 struct regval_ctr *values;
291 /* loop over all of the predefined paths and add each component */
293 for (i=0; builtin_registry_paths[i] != NULL; i++) {
294 if (regdb_key_exists(db, builtin_registry_paths[i])) {
297 status = werror_to_ntstatus(init_registry_key_internal(db,
298 builtin_registry_paths[i]));
299 if (!NT_STATUS_IS_OK(status)) {
304 /* loop over all of the predefined values and add each component */
306 for (i=0; builtin_registry_values[i].path != NULL; i++) {
309 werr = regval_ctr_init(frame, &values);
310 if (!W_ERROR_IS_OK(werr)) {
311 status = werror_to_ntstatus(werr);
315 regdb_fetch_values_internal(db,
316 builtin_registry_values[i].path,
319 /* preserve existing values across restarts. Only add new ones */
321 if (!regval_ctr_key_exists(values,
322 builtin_registry_values[i].valuename))
324 regdb_ctr_add_value(values,
325 &builtin_registry_values[i]);
326 regdb_store_values_internal(db,
327 builtin_registry_values[i].path,
333 status = NT_STATUS_OK;
341 WERROR init_registry_data(void)
344 TALLOC_CTX *frame = talloc_stackframe();
345 struct regval_ctr *values;
349 * First, check for the existence of the needed keys and values.
350 * If all do already exist, we can save the writes.
352 for (i=0; builtin_registry_paths[i] != NULL; i++) {
353 if (!regdb_key_exists(regdb, builtin_registry_paths[i])) {
358 for (i=0; builtin_registry_values[i].path != NULL; i++) {
359 werr = regval_ctr_init(frame, &values);
360 W_ERROR_NOT_OK_GOTO_DONE(werr);
362 regdb_fetch_values_internal(regdb,
363 builtin_registry_values[i].path,
365 if (!regval_ctr_key_exists(values,
366 builtin_registry_values[i].valuename))
381 * There are potentially quite a few store operations which are all
382 * indiviually wrapped in tdb transactions. Wrapping them in a single
383 * transaction gives just a single transaction_commit() to actually do
384 * its fsync()s. See tdb/common/transaction.c for info about nested
385 * transaction behaviour.
388 werr = ntstatus_to_werror(dbwrap_trans_do(regdb,
389 init_registry_data_action,
397 static int regdb_normalize_keynames_fn(struct db_record *rec,
400 TALLOC_CTX *mem_ctx = talloc_tos();
404 if (rec->key.dptr == NULL || rec->key.dsize == 0) {
408 keyname = strchr((const char *) rec->key.dptr, '/');
410 struct db_record new_rec;
412 keyname = talloc_string_sub(mem_ctx,
413 (const char *) rec->key.dptr,
417 DEBUG(2, ("regdb_normalize_keynames_fn: Convert %s to %s\n",
418 (const char *) rec->key.dptr,
421 new_rec.value = rec->value;
422 new_rec.key = string_term_tdb_data(keyname);
423 new_rec.private_data = rec->private_data;
425 /* Delete the original record and store the normalized key */
426 status = rec->delete_rec(rec);
427 if (!NT_STATUS_IS_OK(status)) {
428 DEBUG(0,("regdb_normalize_keynames_fn: "
429 "tdb_delete for [%s] failed!\n",
434 status = rec->store(&new_rec, new_rec.value, TDB_REPLACE);
435 if (!NT_STATUS_IS_OK(status)) {
436 DEBUG(0,("regdb_normalize_keynames_fn: "
437 "failed to store new record for [%s]!\n",
446 static WERROR regdb_store_regdb_version(uint32_t version)
449 const char *version_keyname = "INFO/version";
452 return WERR_CAN_NOT_COMPLETE;
455 status = dbwrap_trans_store_int32(regdb, version_keyname, version);
456 if (!NT_STATUS_IS_OK(status)) {
457 DEBUG(1, ("regdb_store_regdb_version: error storing %s = %d: %s\n",
458 version_keyname, version, nt_errstr(status)));
459 return ntstatus_to_werror(status);
461 DEBUG(10, ("regdb_store_regdb_version: stored %s = %d\n",
462 version_keyname, version));
467 static WERROR regdb_upgrade_v1_to_v2(void)
473 mem_ctx = talloc_stackframe();
474 if (mem_ctx == NULL) {
478 rc = regdb->traverse(regdb, regdb_normalize_keynames_fn, mem_ctx);
480 talloc_destroy(mem_ctx);
483 return WERR_REG_IO_FAILURE;
486 werr = regdb_store_regdb_version(REGVER_V2);
490 /***********************************************************************
491 Open the registry database
492 ***********************************************************************/
494 WERROR regdb_init(void)
496 const char *vstring = "INFO/version";
497 uint32 vers_id, expected_version;
501 DEBUG(10, ("regdb_init: incrementing refcount (%d->%d)\n",
502 regdb_refcount, regdb_refcount+1));
507 regdb = db_open(NULL, state_path("registry.tdb"), 0,
508 REG_TDB_FLAGS, O_RDWR, 0600);
510 regdb = db_open(NULL, state_path("registry.tdb"), 0,
511 REG_TDB_FLAGS, O_RDWR|O_CREAT, 0600);
513 werr = ntstatus_to_werror(map_nt_error_from_unix(errno));
514 DEBUG(1,("regdb_init: Failed to open registry %s (%s)\n",
515 state_path("registry.tdb"), strerror(errno) ));
519 DEBUG(10,("regdb_init: Successfully created registry tdb\n"));
523 DEBUG(10, ("regdb_init: registry db openend. refcount reset (%d)\n",
526 expected_version = REGVER_V2;
528 vers_id = dbwrap_fetch_int32(regdb, vstring);
530 DEBUG(10, ("regdb_init: registry version uninitialized "
531 "(got %d), initializing to version %d\n",
532 vers_id, expected_version));
534 werr = regdb_store_regdb_version(expected_version);
538 if (vers_id > expected_version || vers_id == 0) {
539 DEBUG(1, ("regdb_init: unknown registry version %d "
540 "(code version = %d), refusing initialization\n",
541 vers_id, expected_version));
542 return WERR_CAN_NOT_COMPLETE;
545 if (vers_id == REGVER_V1) {
546 DEBUG(10, ("regdb_init: got registry db version %d, upgrading "
547 "to version %d\n", REGVER_V1, REGVER_V2));
549 if (regdb->transaction_start(regdb) != 0) {
550 return WERR_REG_IO_FAILURE;
553 werr = regdb_upgrade_v1_to_v2();
554 if (!W_ERROR_IS_OK(werr)) {
555 regdb->transaction_cancel(regdb);
559 if (regdb->transaction_commit(regdb) != 0) {
560 return WERR_REG_IO_FAILURE;
566 /* future upgrade code should go here */
571 /***********************************************************************
572 Open the registry. Must already have been initialized by regdb_init()
573 ***********************************************************************/
575 WERROR regdb_open( void )
577 WERROR result = WERR_OK;
580 DEBUG(10, ("regdb_open: incrementing refcount (%d->%d)\n",
581 regdb_refcount, regdb_refcount+1));
588 regdb = db_open(NULL, state_path("registry.tdb"), 0,
589 REG_TDB_FLAGS, O_RDWR, 0600);
591 result = ntstatus_to_werror( map_nt_error_from_unix( errno ) );
592 DEBUG(0,("regdb_open: Failed to open %s! (%s)\n",
593 state_path("registry.tdb"), strerror(errno) ));
599 DEBUG(10, ("regdb_open: registry db opened. refcount reset (%d)\n",
605 /***********************************************************************
606 ***********************************************************************/
608 int regdb_close( void )
610 if (regdb_refcount == 0) {
616 DEBUG(10, ("regdb_close: decrementing refcount (%d->%d)\n",
617 regdb_refcount+1, regdb_refcount));
619 if ( regdb_refcount > 0 )
622 SMB_ASSERT( regdb_refcount >= 0 );
628 WERROR regdb_transaction_start(void)
630 return (regdb->transaction_start(regdb) == 0) ?
631 WERR_OK : WERR_REG_IO_FAILURE;
634 WERROR regdb_transaction_commit(void)
636 return (regdb->transaction_commit(regdb) == 0) ?
637 WERR_OK : WERR_REG_IO_FAILURE;
640 WERROR regdb_transaction_cancel(void)
642 return (regdb->transaction_cancel(regdb) == 0) ?
643 WERR_OK : WERR_REG_IO_FAILURE;
646 /***********************************************************************
647 return the tdb sequence number of the registry tdb.
648 this is an indicator for the content of the registry
649 having changed. it will change upon regdb_init, too, though.
650 ***********************************************************************/
651 int regdb_get_seqnum(void)
653 return regdb->get_seqnum(regdb);
657 static WERROR regdb_delete_key_with_prefix(struct db_context *db,
662 WERROR werr = WERR_NOMEM;
663 TALLOC_CTX *mem_ctx = talloc_stackframe();
665 if (keyname == NULL) {
666 werr = WERR_INVALID_PARAM;
670 if (prefix == NULL) {
671 path = discard_const_p(char, keyname);
673 path = talloc_asprintf(mem_ctx, "%s\\%s", prefix, keyname);
679 path = normalize_reg_path(mem_ctx, path);
684 werr = ntstatus_to_werror(dbwrap_delete_bystring(db, path));
686 /* treat "not" found" as ok */
687 if (W_ERROR_EQUAL(werr, WERR_NOT_FOUND)) {
692 talloc_free(mem_ctx);
697 static WERROR regdb_delete_values(struct db_context *db, const char *keyname)
699 return regdb_delete_key_with_prefix(db, keyname, REG_VALUE_PREFIX);
702 static WERROR regdb_delete_secdesc(struct db_context *db, const char *keyname)
704 return regdb_delete_key_with_prefix(db, keyname, REG_SECDESC_PREFIX);
707 static WERROR regdb_delete_subkeylist(struct db_context *db, const char *keyname)
709 return regdb_delete_key_with_prefix(db, keyname, NULL);
712 static WERROR regdb_delete_key_lists(struct db_context *db, const char *keyname)
716 werr = regdb_delete_values(db, keyname);
717 if (!W_ERROR_IS_OK(werr)) {
718 DEBUG(1, (__location__ " Deleting %s\\%s failed: %s\n",
719 REG_VALUE_PREFIX, keyname, win_errstr(werr)));
723 werr = regdb_delete_secdesc(db, keyname);
724 if (!W_ERROR_IS_OK(werr)) {
725 DEBUG(1, (__location__ " Deleting %s\\%s failed: %s\n",
726 REG_SECDESC_PREFIX, keyname, win_errstr(werr)));
730 werr = regdb_delete_subkeylist(db, keyname);
731 if (!W_ERROR_IS_OK(werr)) {
732 DEBUG(1, (__location__ " Deleting %s failed: %s\n",
733 keyname, win_errstr(werr)));
741 /***********************************************************************
742 Add subkey strings to the registry tdb under a defined key
743 fmt is the same format as tdb_pack except this function only supports
745 ***********************************************************************/
747 static WERROR regdb_store_keys_internal2(struct db_context *db,
749 struct regsubkey_ctr *ctr)
752 uint8 *buffer = NULL;
755 uint32 num_subkeys = regsubkey_ctr_numkeys(ctr);
756 char *keyname = NULL;
757 TALLOC_CTX *ctx = talloc_stackframe();
761 werr = WERR_INVALID_PARAM;
765 keyname = talloc_strdup(ctx, key);
771 keyname = normalize_reg_path(ctx, keyname);
777 /* allocate some initial memory */
779 buffer = (uint8 *)SMB_MALLOC(1024);
780 if (buffer == NULL) {
787 /* store the number of subkeys */
789 len += tdb_pack(buffer+len, buflen-len, "d", num_subkeys);
791 /* pack all the strings */
793 for (i=0; i<num_subkeys; i++) {
796 thistime = tdb_pack(buffer+len, buflen-len, "f",
797 regsubkey_ctr_specific_key(ctr, i));
798 if (len+thistime > buflen) {
801 * tdb_pack hasn't done anything because of the short
802 * buffer, allocate extra space.
804 buffer = SMB_REALLOC_ARRAY(buffer, uint8_t,
807 DEBUG(0, ("regdb_store_keys: Failed to realloc "
808 "memory of size [%u]\n",
809 (unsigned int)(len+thistime)*2));
813 buflen = (len+thistime)*2;
814 thistime2 = tdb_pack(
815 buffer+len, buflen-len, "f",
816 regsubkey_ctr_specific_key(ctr, i));
817 if (thistime2 != thistime) {
818 DEBUG(0, ("tdb_pack failed\n"));
819 werr = WERR_CAN_NOT_COMPLETE;
826 /* finally write out the data */
830 werr = ntstatus_to_werror(dbwrap_store_bystring(db, keyname, dbuf,
832 W_ERROR_NOT_OK_GOTO_DONE(werr);
835 * Delete a sorted subkey cache for regdb_key_exists, will be
836 * recreated automatically
838 keyname = talloc_asprintf(ctx, "%s\\%s", REG_SORTED_SUBKEYS_PREFIX,
840 if (keyname == NULL) {
845 werr = ntstatus_to_werror(dbwrap_delete_bystring(db, keyname));
847 /* don't treat WERR_NOT_FOUND as an error here */
848 if (W_ERROR_EQUAL(werr, WERR_NOT_FOUND)) {
858 /***********************************************************************
859 Store the new subkey record and create any child key records that
860 do not currently exist
861 ***********************************************************************/
863 struct regdb_store_keys_context {
865 struct regsubkey_ctr *ctr;
868 static NTSTATUS regdb_store_keys_action(struct db_context *db,
871 struct regdb_store_keys_context *store_ctx;
875 struct regsubkey_ctr *subkeys = NULL, *old_subkeys = NULL;
876 char *oldkeyname = NULL;
877 TALLOC_CTX *mem_ctx = talloc_stackframe();
879 store_ctx = (struct regdb_store_keys_context *)private_data;
882 * Re-fetch the old keys inside the transaction
885 werr = regsubkey_ctr_init(mem_ctx, &old_subkeys);
886 W_ERROR_NOT_OK_GOTO_DONE(werr);
888 werr = regdb_fetch_keys_internal(db, store_ctx->key, old_subkeys);
889 if (!W_ERROR_IS_OK(werr) &&
890 !W_ERROR_EQUAL(werr, WERR_NOT_FOUND))
896 * Make the store operation as safe as possible without transactions:
898 * (1) For each subkey removed from ctr compared with old_subkeys:
900 * (a) First delete the value db entry.
902 * (b) Next delete the secdesc db record.
904 * (c) Then delete the subkey list entry.
906 * (2) Now write the list of subkeys of the parent key,
907 * deleting removed entries and adding new ones.
909 * (3) Finally create the subkey list entries for the added keys.
911 * This way if we crash half-way in between deleting the subkeys
912 * and storing the parent's list of subkeys, no old data can pop up
913 * out of the blue when re-adding keys later on.
916 /* (1) delete removed keys' lists (values/secdesc/subkeys) */
918 num_subkeys = regsubkey_ctr_numkeys(old_subkeys);
919 for (i=0; i<num_subkeys; i++) {
920 oldkeyname = regsubkey_ctr_specific_key(old_subkeys, i);
922 if (regsubkey_ctr_key_exists(store_ctx->ctr, oldkeyname)) {
924 * It's still around, don't delete
929 path = talloc_asprintf(mem_ctx, "%s\\%s", store_ctx->key,
936 werr = regdb_delete_key_lists(db, path);
937 W_ERROR_NOT_OK_GOTO_DONE(werr);
942 TALLOC_FREE(old_subkeys);
944 /* (2) store the subkey list for the parent */
946 werr = regdb_store_keys_internal2(db, store_ctx->key, store_ctx->ctr);
947 if (!W_ERROR_IS_OK(werr)) {
948 DEBUG(0,("regdb_store_keys: Failed to store new subkey list "
949 "for parent [%s]: %s\n", store_ctx->key,
954 /* (3) now create records for any subkeys that don't already exist */
956 num_subkeys = regsubkey_ctr_numkeys(store_ctx->ctr);
958 if (num_subkeys == 0) {
959 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
960 W_ERROR_NOT_OK_GOTO_DONE(werr);
962 werr = regdb_store_keys_internal2(db, store_ctx->key, subkeys);
963 if (!W_ERROR_IS_OK(werr)) {
964 DEBUG(0,("regdb_store_keys: Failed to store "
965 "new record for key [%s]: %s\n",
966 store_ctx->key, win_errstr(werr)));
969 TALLOC_FREE(subkeys);
972 for (i=0; i<num_subkeys; i++) {
973 path = talloc_asprintf(mem_ctx, "%s\\%s", store_ctx->key,
974 regsubkey_ctr_specific_key(store_ctx->ctr, i));
979 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
980 W_ERROR_NOT_OK_GOTO_DONE(werr);
982 werr = regdb_fetch_keys_internal(db, path, subkeys);
983 if (!W_ERROR_IS_OK(werr)) {
984 /* create a record with 0 subkeys */
985 werr = regdb_store_keys_internal2(db, path, subkeys);
986 if (!W_ERROR_IS_OK(werr)) {
987 DEBUG(0,("regdb_store_keys: Failed to store "
988 "new record for key [%s]: %s\n", path,
994 TALLOC_FREE(subkeys);
1001 talloc_free(mem_ctx);
1002 return werror_to_ntstatus(werr);
1005 static bool regdb_store_keys_internal(struct db_context *db, const char *key,
1006 struct regsubkey_ctr *ctr)
1008 int num_subkeys, old_num_subkeys, i;
1009 struct regsubkey_ctr *old_subkeys = NULL;
1010 TALLOC_CTX *ctx = talloc_stackframe();
1013 struct regdb_store_keys_context store_ctx;
1015 if (!regdb_key_is_base_key(key) && !regdb_key_exists(db, key)) {
1020 * fetch a list of the old subkeys so we can determine if anything has
1024 werr = regsubkey_ctr_init(ctx, &old_subkeys);
1025 if (!W_ERROR_IS_OK(werr)) {
1026 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
1030 werr = regdb_fetch_keys_internal(db, key, old_subkeys);
1031 if (!W_ERROR_IS_OK(werr) &&
1032 !W_ERROR_EQUAL(werr, WERR_NOT_FOUND))
1037 num_subkeys = regsubkey_ctr_numkeys(ctr);
1038 old_num_subkeys = regsubkey_ctr_numkeys(old_subkeys);
1039 if ((num_subkeys && old_num_subkeys) &&
1040 (num_subkeys == old_num_subkeys)) {
1042 for (i = 0; i < num_subkeys; i++) {
1043 if (strcmp(regsubkey_ctr_specific_key(ctr, i),
1044 regsubkey_ctr_specific_key(old_subkeys, i))
1050 if (i == num_subkeys) {
1052 * Nothing changed, no point to even start a tdb
1061 TALLOC_FREE(old_subkeys);
1063 store_ctx.key = key;
1064 store_ctx.ctr = ctr;
1066 werr = ntstatus_to_werror(dbwrap_trans_do(db,
1067 regdb_store_keys_action,
1070 ret = W_ERROR_IS_OK(werr);
1078 bool regdb_store_keys(const char *key, struct regsubkey_ctr *ctr)
1080 return regdb_store_keys_internal(regdb, key, ctr);
1084 * create a subkey of a given key
1087 struct regdb_create_subkey_context {
1092 static NTSTATUS regdb_create_subkey_action(struct db_context *db,
1096 struct regdb_create_subkey_context *create_ctx;
1097 struct regsubkey_ctr *subkeys;
1098 TALLOC_CTX *mem_ctx = talloc_stackframe();
1100 create_ctx = (struct regdb_create_subkey_context *)private_data;
1102 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
1103 W_ERROR_NOT_OK_GOTO_DONE(werr);
1105 werr = regdb_fetch_keys_internal(db, create_ctx->key, subkeys);
1106 W_ERROR_NOT_OK_GOTO_DONE(werr);
1108 werr = regsubkey_ctr_addkey(subkeys, create_ctx->subkey);
1109 W_ERROR_NOT_OK_GOTO_DONE(werr);
1111 werr = regdb_store_keys_internal2(db, create_ctx->key, subkeys);
1112 if (!W_ERROR_IS_OK(werr)) {
1113 DEBUG(0, (__location__ " failed to store new subkey list for "
1114 "parent key %s: %s\n", create_ctx->key,
1119 talloc_free(mem_ctx);
1120 return werror_to_ntstatus(werr);
1123 static WERROR regdb_create_subkey(const char *key, const char *subkey)
1126 struct regsubkey_ctr *subkeys;
1127 TALLOC_CTX *mem_ctx = talloc_stackframe();
1128 struct regdb_create_subkey_context create_ctx;
1130 if (!regdb_key_is_base_key(key) && !regdb_key_exists(regdb, key)) {
1131 werr = WERR_NOT_FOUND;
1135 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
1136 W_ERROR_NOT_OK_GOTO_DONE(werr);
1138 werr = regdb_fetch_keys_internal(regdb, key, subkeys);
1139 W_ERROR_NOT_OK_GOTO_DONE(werr);
1141 if (regsubkey_ctr_key_exists(subkeys, subkey)) {
1146 talloc_free(subkeys);
1148 create_ctx.key = key;
1149 create_ctx.subkey = subkey;
1151 werr = ntstatus_to_werror(dbwrap_trans_do(regdb,
1152 regdb_create_subkey_action,
1156 talloc_free(mem_ctx);
1161 * create a subkey of a given key
1164 struct regdb_delete_subkey_context {
1170 static NTSTATUS regdb_delete_subkey_action(struct db_context *db,
1174 struct regdb_delete_subkey_context *delete_ctx;
1175 struct regsubkey_ctr *subkeys;
1176 TALLOC_CTX *mem_ctx = talloc_stackframe();
1178 delete_ctx = (struct regdb_delete_subkey_context *)private_data;
1180 werr = regdb_delete_key_lists(db, delete_ctx->path);
1181 W_ERROR_NOT_OK_GOTO_DONE(werr);
1183 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
1184 W_ERROR_NOT_OK_GOTO_DONE(werr);
1186 werr = regdb_fetch_keys_internal(db, delete_ctx->key, subkeys);
1187 W_ERROR_NOT_OK_GOTO_DONE(werr);
1189 werr = regsubkey_ctr_delkey(subkeys, delete_ctx->subkey);
1190 W_ERROR_NOT_OK_GOTO_DONE(werr);
1192 werr = regdb_store_keys_internal2(db, delete_ctx->key, subkeys);
1193 if (!W_ERROR_IS_OK(werr)) {
1194 DEBUG(0, (__location__ " failed to store new subkey_list for "
1195 "parent key %s: %s\n", delete_ctx->key,
1200 talloc_free(mem_ctx);
1201 return werror_to_ntstatus(werr);
1204 static WERROR regdb_delete_subkey(const char *key, const char *subkey)
1208 struct regdb_delete_subkey_context delete_ctx;
1209 TALLOC_CTX *mem_ctx = talloc_stackframe();
1211 if (!regdb_key_is_base_key(key) && !regdb_key_exists(regdb, key)) {
1212 werr = WERR_NOT_FOUND;
1216 path = talloc_asprintf(mem_ctx, "%s\\%s", key, subkey);
1222 if (!regdb_key_exists(regdb, path)) {
1227 delete_ctx.key = key;
1228 delete_ctx.subkey = subkey;
1229 delete_ctx.path = path;
1231 werr = ntstatus_to_werror(dbwrap_trans_do(regdb,
1232 regdb_delete_subkey_action,
1236 talloc_free(mem_ctx);
1240 static TDB_DATA regdb_fetch_key_internal(struct db_context *db,
1241 TALLOC_CTX *mem_ctx, const char *key)
1246 path = normalize_reg_path(mem_ctx, key);
1248 return make_tdb_data(NULL, 0);
1251 data = dbwrap_fetch_bystring(db, mem_ctx, path);
1259 * check whether a given key name represents a base key,
1260 * i.e one without a subkey separator ('\').
1262 static bool regdb_key_is_base_key(const char *key)
1264 TALLOC_CTX *mem_ctx = talloc_stackframe();
1272 path = normalize_reg_path(mem_ctx, key);
1274 DEBUG(0, ("out of memory! (talloc failed)\n"));
1278 if (*path == '\0') {
1282 ret = (strrchr(path, '\\') == NULL);
1285 TALLOC_FREE(mem_ctx);
1290 * regdb_key_exists() is a very frequent operation. It can be quite
1291 * time-consuming to fully fetch the parent's subkey list, talloc_strdup all
1292 * subkeys and then compare the keyname linearly to all the parent's subkeys.
1294 * The following code tries to make this operation as efficient as possible:
1295 * Per registry key we create a list of subkeys that is very efficient to
1296 * search for existence of a subkey. Its format is:
1298 * 4 bytes num_subkeys
1299 * 4*num_subkey bytes offset into the string array
1300 * then follows a sorted list of subkeys in uppercase
1302 * This record is created by create_sorted_subkeys() on demand if it does not
1303 * exist. scan_parent_subkeys() uses regdb->parse_record to search the sorted
1304 * list, the parsing code and the binary search can be found in
1305 * parent_subkey_scanner. The code uses parse_record() to avoid a memcpy of
1306 * the potentially large subkey record.
1308 * The sorted subkey record is deleted in regdb_store_keys_internal2 and
1309 * recreated on demand.
1312 static int cmp_keynames(char **p1, char **p2)
1314 return StrCaseCmp(*p1, *p2);
1317 struct create_sorted_subkeys_context {
1319 const char *sorted_keyname;
1322 static NTSTATUS create_sorted_subkeys_action(struct db_context *db,
1325 char **sorted_subkeys;
1326 struct regsubkey_ctr *ctr;
1333 struct create_sorted_subkeys_context *sorted_ctx;
1335 sorted_ctx = (struct create_sorted_subkeys_context *)private_data;
1338 * In this function, we only treat failing of the actual write to
1339 * the db as a real error. All preliminary errors, at a stage when
1340 * nothing has been written to the DB yet are treated as success
1341 * to be committed (as an empty transaction).
1343 * The reason is that this (disposable) call might be nested in other
1344 * transactions. Doing a cancel here would destroy the possibility of
1345 * a transaction_commit for transactions that we might be wrapped in.
1348 status = werror_to_ntstatus(regsubkey_ctr_init(talloc_tos(), &ctr));
1349 if (!NT_STATUS_IS_OK(status)) {
1350 /* don't treat this as an error */
1351 status = NT_STATUS_OK;
1355 status = werror_to_ntstatus(regdb_fetch_keys_internal(db,
1358 if (!NT_STATUS_IS_OK(status)) {
1359 /* don't treat this as an error */
1360 status = NT_STATUS_OK;
1364 num_subkeys = regsubkey_ctr_numkeys(ctr);
1365 sorted_subkeys = talloc_array(ctr, char *, num_subkeys);
1366 if (sorted_subkeys == NULL) {
1367 /* don't treat this as an error */
1371 len = 4 + 4*num_subkeys;
1373 for (i = 0; i < num_subkeys; i++) {
1374 sorted_subkeys[i] = talloc_strdup_upper(sorted_subkeys,
1375 regsubkey_ctr_specific_key(ctr, i));
1376 if (sorted_subkeys[i] == NULL) {
1377 /* don't treat this as an error */
1380 len += strlen(sorted_subkeys[i])+1;
1383 TYPESAFE_QSORT(sorted_subkeys, num_subkeys, cmp_keynames);
1385 buf = talloc_array(ctr, char, len);
1387 /* don't treat this as an error */
1390 p = buf + 4 + 4*num_subkeys;
1392 SIVAL(buf, 0, num_subkeys);
1394 for (i=0; i < num_subkeys; i++) {
1395 ptrdiff_t offset = p - buf;
1396 SIVAL(buf, 4 + 4*i, offset);
1397 strlcpy(p, sorted_subkeys[i], len-offset);
1398 p += strlen(sorted_subkeys[i]) + 1;
1401 status = dbwrap_store_bystring(
1402 db, sorted_ctx->sorted_keyname, make_tdb_data((uint8_t *)buf,
1411 static bool create_sorted_subkeys(const char *key, const char *sorted_keyname)
1414 struct create_sorted_subkeys_context sorted_ctx;
1416 sorted_ctx.key = key;
1417 sorted_ctx.sorted_keyname = sorted_keyname;
1419 status = dbwrap_trans_do(regdb,
1420 create_sorted_subkeys_action,
1423 return NT_STATUS_IS_OK(status);
1426 struct scan_subkey_state {
1432 static int parent_subkey_scanner(TDB_DATA key, TDB_DATA data,
1435 struct scan_subkey_state *state =
1436 (struct scan_subkey_state *)private_data;
1437 uint32_t num_subkeys;
1440 if (data.dsize < sizeof(uint32_t)) {
1444 state->scanned = true;
1445 state->found = false;
1447 tdb_unpack(data.dptr, data.dsize, "d", &num_subkeys);
1453 uint32_t idx = (l+u)/2;
1454 char *s = (char *)data.dptr + IVAL(data.dptr, 4 + 4*idx);
1455 int comparison = strcmp(state->name, s);
1457 if (comparison < 0) {
1459 } else if (comparison > 0) {
1462 state->found = true;
1469 static bool scan_parent_subkeys(struct db_context *db, const char *parent,
1474 struct scan_subkey_state state = { 0, };
1475 bool result = false;
1480 path = normalize_reg_path(talloc_tos(), parent);
1485 key = talloc_asprintf(talloc_tos(), "%s\\%s",
1486 REG_SORTED_SUBKEYS_PREFIX, path);
1491 state.name = talloc_strdup_upper(talloc_tos(), name);
1492 if (state.name == NULL) {
1495 state.scanned = false;
1497 res = db->parse_record(db, string_term_tdb_data(key),
1498 parent_subkey_scanner, &state);
1500 if (state.scanned) {
1501 result = state.found;
1503 res = db->transaction_start(db);
1505 DEBUG(0, ("error starting transacion\n"));
1509 if (!create_sorted_subkeys(path, key)) {
1510 res = db->transaction_cancel(db);
1512 smb_panic("Failed to cancel transaction.");
1517 res = db->parse_record(db, string_term_tdb_data(key),
1518 parent_subkey_scanner, &state);
1519 if ((res == 0) && (state.scanned)) {
1520 result = state.found;
1523 res = db->transaction_commit(db);
1525 DEBUG(0, ("error committing transaction\n"));
1532 TALLOC_FREE(state.name);
1537 * Check for the existence of a key.
1539 * Existence of a key is authoritatively defined by its
1540 * existence in the list of subkeys of its parent key.
1541 * The exeption of this are keys without a parent key,
1542 * i.e. the "base" keys (HKLM, HKCU, ...).
1544 static bool regdb_key_exists(struct db_context *db, const char *key)
1546 TALLOC_CTX *mem_ctx = talloc_stackframe();
1555 path = normalize_reg_path(mem_ctx, key);
1557 DEBUG(0, ("out of memory! (talloc failed)\n"));
1561 if (*path == '\0') {
1565 p = strrchr(path, '\\');
1567 /* this is a base key */
1568 value = regdb_fetch_key_internal(db, mem_ctx, path);
1569 ret = (value.dptr != NULL);
1572 ret = scan_parent_subkeys(db, path, p+1);
1576 TALLOC_FREE(mem_ctx);
1581 /***********************************************************************
1582 Retrieve an array of strings containing subkeys. Memory should be
1583 released by the caller.
1584 ***********************************************************************/
1586 static WERROR regdb_fetch_keys_internal(struct db_context *db, const char *key,
1587 struct regsubkey_ctr *ctr)
1595 TALLOC_CTX *frame = talloc_stackframe();
1598 DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key ? key : "NULL"));
1600 if (!regdb_key_exists(db, key)) {
1601 DEBUG(10, ("key [%s] not found\n", key));
1602 werr = WERR_NOT_FOUND;
1606 werr = regsubkey_ctr_set_seqnum(ctr, db->get_seqnum(db));
1607 W_ERROR_NOT_OK_GOTO_DONE(werr);
1609 value = regdb_fetch_key_internal(db, frame, key);
1611 if (value.dsize == 0 || value.dptr == NULL) {
1612 DEBUG(10, ("regdb_fetch_keys: no subkeys found for key [%s]\n",
1618 buflen = value.dsize;
1619 len = tdb_unpack( buf, buflen, "d", &num_items);
1620 if (len == (uint32_t)-1) {
1621 werr = WERR_NOT_FOUND;
1625 werr = regsubkey_ctr_reinit(ctr);
1626 W_ERROR_NOT_OK_GOTO_DONE(werr);
1628 for (i=0; i<num_items; i++) {
1629 len += tdb_unpack(buf+len, buflen-len, "f", subkeyname);
1630 werr = regsubkey_ctr_addkey(ctr, subkeyname);
1631 if (!W_ERROR_IS_OK(werr)) {
1632 DEBUG(5, ("regdb_fetch_keys: regsubkey_ctr_addkey "
1633 "failed: %s\n", win_errstr(werr)));
1639 DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items));
1646 int regdb_fetch_keys(const char *key, struct regsubkey_ctr *ctr)
1650 werr = regdb_fetch_keys_internal(regdb, key, ctr);
1651 if (!W_ERROR_IS_OK(werr)) {
1655 return regsubkey_ctr_numkeys(ctr);
1658 /****************************************************************************
1659 Unpack a list of registry values frem the TDB
1660 ***************************************************************************/
1662 static int regdb_unpack_values(struct regval_ctr *values, uint8 *buf, int buflen)
1669 uint32 num_values = 0;
1672 /* loop and unpack the rest of the registry values */
1674 len += tdb_unpack(buf+len, buflen-len, "d", &num_values);
1676 for ( i=0; i<num_values; i++ ) {
1677 /* unpack the next regval */
1682 valuename[0] = '\0';
1683 len += tdb_unpack(buf+len, buflen-len, "fdB",
1689 regval_ctr_addvalue(values, valuename, type,
1690 (uint8_t *)data_p, size);
1691 SAFE_FREE(data_p); /* 'B' option to tdb_unpack does a malloc() */
1693 DEBUG(8,("specific: [%s], len: %d\n", valuename, size));
1699 /****************************************************************************
1700 Pack all values in all printer keys
1701 ***************************************************************************/
1703 static int regdb_pack_values(struct regval_ctr *values, uint8 *buf, int buflen)
1707 struct regval_blob *val;
1713 num_values = regval_ctr_numvals( values );
1715 /* pack the number of values first */
1717 len += tdb_pack( buf+len, buflen-len, "d", num_values );
1719 /* loop over all values */
1721 for ( i=0; i<num_values; i++ ) {
1722 val = regval_ctr_specific_value( values, i );
1723 len += tdb_pack(buf+len, buflen-len, "fdB",
1727 regval_data_p(val) );
1733 /***********************************************************************
1734 Retrieve an array of strings containing subkeys. Memory should be
1735 released by the caller.
1736 ***********************************************************************/
1738 static int regdb_fetch_values_internal(struct db_context *db, const char* key,
1739 struct regval_ctr *values)
1741 char *keystr = NULL;
1742 TALLOC_CTX *ctx = talloc_stackframe();
1747 DEBUG(10,("regdb_fetch_values: Looking for value of key [%s] \n", key));
1749 if (!regdb_key_exists(db, key)) {
1753 keystr = talloc_asprintf(ctx, "%s\\%s", REG_VALUE_PREFIX, key);
1758 werr = regval_ctr_set_seqnum(values, db->get_seqnum(db));
1759 W_ERROR_NOT_OK_GOTO_DONE(werr);
1761 value = regdb_fetch_key_internal(db, ctx, keystr);
1764 /* all keys have zero values by default */
1768 regdb_unpack_values(values, value.dptr, value.dsize);
1769 ret = regval_ctr_numvals(values);
1776 int regdb_fetch_values(const char* key, struct regval_ctr *values)
1778 return regdb_fetch_values_internal(regdb, key, values);
1781 static bool regdb_store_values_internal(struct db_context *db, const char *key,
1782 struct regval_ctr *values)
1784 TDB_DATA old_data, data;
1785 char *keystr = NULL;
1786 TALLOC_CTX *ctx = talloc_stackframe();
1789 bool result = false;
1791 DEBUG(10,("regdb_store_values: Looking for value of key [%s] \n", key));
1793 if (!regdb_key_exists(db, key)) {
1799 len = regdb_pack_values(values, data.dptr, data.dsize);
1801 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
1805 data.dptr = TALLOC_ARRAY(ctx, uint8, len);
1808 len = regdb_pack_values(values, data.dptr, data.dsize);
1810 SMB_ASSERT( len == data.dsize );
1812 keystr = talloc_asprintf(ctx, "%s\\%s", REG_VALUE_PREFIX, key );
1816 keystr = normalize_reg_path(ctx, keystr);
1821 old_data = dbwrap_fetch_bystring(db, ctx, keystr);
1823 if ((old_data.dptr != NULL)
1824 && (old_data.dsize == data.dsize)
1825 && (memcmp(old_data.dptr, data.dptr, data.dsize) == 0))
1831 status = dbwrap_trans_store_bystring(db, keystr, data, TDB_REPLACE);
1833 result = NT_STATUS_IS_OK(status);
1840 bool regdb_store_values(const char *key, struct regval_ctr *values)
1842 return regdb_store_values_internal(regdb, key, values);
1845 static WERROR regdb_get_secdesc(TALLOC_CTX *mem_ctx, const char *key,
1846 struct security_descriptor **psecdesc)
1851 TALLOC_CTX *tmp_ctx = talloc_stackframe();
1852 WERROR err = WERR_OK;
1854 DEBUG(10, ("regdb_get_secdesc: Getting secdesc of key [%s]\n", key));
1856 if (!regdb_key_exists(regdb, key)) {
1861 tdbkey = talloc_asprintf(tmp_ctx, "%s\\%s", REG_SECDESC_PREFIX, key);
1862 if (tdbkey == NULL) {
1867 tdbkey = normalize_reg_path(tmp_ctx, tdbkey);
1868 if (tdbkey == NULL) {
1873 data = dbwrap_fetch_bystring(regdb, tmp_ctx, tdbkey);
1874 if (data.dptr == NULL) {
1879 status = unmarshall_sec_desc(mem_ctx, (uint8 *)data.dptr, data.dsize,
1882 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MEMORY)) {
1884 } else if (!NT_STATUS_IS_OK(status)) {
1885 err = WERR_REG_CORRUPT;
1889 TALLOC_FREE(tmp_ctx);
1893 static WERROR regdb_set_secdesc(const char *key,
1894 struct security_descriptor *secdesc)
1896 TALLOC_CTX *mem_ctx = talloc_stackframe();
1898 WERROR err = WERR_NOMEM;
1901 if (!regdb_key_exists(regdb, key)) {
1906 tdbkey = talloc_asprintf(mem_ctx, "%s\\%s", REG_SECDESC_PREFIX, key);
1907 if (tdbkey == NULL) {
1911 tdbkey = normalize_reg_path(mem_ctx, tdbkey);
1912 if (tdbkey == NULL) {
1917 if (secdesc == NULL) {
1918 /* assuming a delete */
1919 err = ntstatus_to_werror(dbwrap_trans_delete_bystring(regdb,
1924 err = ntstatus_to_werror(marshall_sec_desc(mem_ctx, secdesc,
1927 W_ERROR_NOT_OK_GOTO_DONE(err);
1929 err = ntstatus_to_werror(dbwrap_trans_store_bystring(regdb, tdbkey,
1933 TALLOC_FREE(mem_ctx);
1937 bool regdb_subkeys_need_update(struct regsubkey_ctr *subkeys)
1939 return (regdb_get_seqnum() != regsubkey_ctr_get_seqnum(subkeys));
1942 bool regdb_values_need_update(struct regval_ctr *values)
1944 return (regdb_get_seqnum() != regval_ctr_get_seqnum(values));
1948 * Table of function pointers for default access
1951 struct registry_ops regdb_ops = {
1952 .fetch_subkeys = regdb_fetch_keys,
1953 .fetch_values = regdb_fetch_values,
1954 .store_subkeys = regdb_store_keys,
1955 .store_values = regdb_store_values,
1956 .create_subkey = regdb_create_subkey,
1957 .delete_subkey = regdb_delete_subkey,
1958 .get_secdesc = regdb_get_secdesc,
1959 .set_secdesc = regdb_set_secdesc,
1960 .subkeys_need_update = regdb_subkeys_need_update,
1961 .values_need_update = regdb_values_need_update