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. */
27 #include "reg_util_internal.h"
28 #include "reg_backend_db.h"
29 #include "reg_objects.h"
30 #include "nt_printing.h"
34 #define DBGC_CLASS DBGC_REGISTRY
36 static struct db_context *regdb = NULL;
37 static int regdb_refcount;
39 static bool regdb_key_exists(struct db_context *db, const char *key);
40 static bool regdb_key_is_base_key(const char *key);
41 static WERROR regdb_fetch_keys_internal(struct db_context *db, const char *key,
42 struct regsubkey_ctr *ctr);
43 static bool regdb_store_keys_internal(struct db_context *db, const char *key,
44 struct regsubkey_ctr *ctr);
45 static int regdb_fetch_values_internal(struct db_context *db, const char* key,
46 struct regval_ctr *values);
47 static bool regdb_store_values_internal(struct db_context *db, const char *key,
48 struct regval_ctr *values);
50 /* List the deepest path into the registry. All part components will be created.*/
52 /* If you want to have a part of the path controlled by the tdb and part by
53 a virtual registry db (e.g. printing), then you have to list the deepest path.
54 For example,"HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion/Print"
55 allows the reg_db backend to handle everything up to
56 "HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion" and then we'll hook
57 the reg_printing backend onto the last component of the path (see
58 KEY_PRINTING_2K in include/rpc_reg.h) --jerry */
60 static const char *builtin_registry_paths[] = {
64 KEY_PRINTING "\\Forms",
65 KEY_PRINTING "\\Printers",
66 KEY_PRINTING "\\Environments\\Windows NT x86\\Print Processors\\winprint",
73 KEY_SAMBA_GROUP_POLICY,
74 KEY_GP_MACHINE_POLICY,
75 KEY_GP_MACHINE_WIN_POLICY,
78 KEY_GP_USER_WIN_POLICY,
79 "HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon\\GPExtensions",
80 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Print\\Monitors",
82 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\DefaultUserConfiguration",
91 struct builtin_regkey_value {
93 const char *valuename;
101 static struct builtin_regkey_value builtin_registry_values[] = {
102 { KEY_PRINTING_PORTS,
103 SAMBA_PRINTER_PORT_NAME, REG_SZ, { "" } },
105 "DefaultSpoolDirectory", REG_SZ, { "C:\\Windows\\System32\\Spool\\Printers" } },
107 "DisplayName", REG_SZ, { "Event Log" } },
109 "ErrorControl", REG_DWORD, { (char*)0x00000001 } },
110 { NULL, NULL, 0, { NULL } }
114 * Initialize a key in the registry:
115 * create each component key of the specified path.
117 static WERROR init_registry_key_internal(struct db_context *db,
118 const char *add_path)
121 TALLOC_CTX *frame = talloc_stackframe();
124 char *remaining = NULL;
127 struct regsubkey_ctr *subkeys;
130 DEBUG(6, ("init_registry_key: Adding [%s]\n", add_path));
132 path = talloc_strdup(frame, add_path);
133 base = talloc_strdup(frame, "");
134 if (!path || !base) {
140 while (next_token_talloc(frame, &p, &keyname, "\\")) {
142 /* build up the registry path from the components */
145 base = talloc_asprintf(frame, "%s\\", base);
151 base = talloc_asprintf_append(base, "%s", keyname);
157 /* get the immediate subkeyname (if we have one ) */
159 subkeyname = talloc_strdup(frame, "");
165 remaining = talloc_strdup(frame, p);
172 if (!next_token_talloc(frame, &p2,
175 subkeyname = talloc_strdup(frame,p2);
183 DEBUG(10,("init_registry_key: Storing key [%s] with "
184 "subkey [%s]\n", base,
185 *subkeyname ? subkeyname : "NULL"));
187 /* we don't really care if the lookup succeeds or not
188 * since we are about to update the record.
189 * We just want any subkeys already present */
191 werr = regsubkey_ctr_init(frame, &subkeys);
192 if (!W_ERROR_IS_OK(werr)) {
193 DEBUG(0,("talloc() failure!\n"));
197 werr = regdb_fetch_keys_internal(db, base, subkeys);
198 if (!W_ERROR_IS_OK(werr) &&
199 !W_ERROR_EQUAL(werr, WERR_NOT_FOUND))
205 werr = regsubkey_ctr_addkey(subkeys, subkeyname);
206 if (!W_ERROR_IS_OK(werr)) {
210 if (!regdb_store_keys_internal(db, base, subkeys)) {
211 werr = WERR_CAN_NOT_COMPLETE;
223 struct init_registry_key_context {
224 const char *add_path;
227 static NTSTATUS init_registry_key_action(struct db_context *db,
230 struct init_registry_key_context *init_ctx =
231 (struct init_registry_key_context *)private_data;
233 return werror_to_ntstatus(init_registry_key_internal(
234 db, init_ctx->add_path));
238 * Initialize a key in the registry:
239 * create each component key of the specified path,
240 * wrapped in one db transaction.
242 WERROR init_registry_key(const char *add_path)
244 struct init_registry_key_context init_ctx;
246 if (regdb_key_exists(regdb, add_path)) {
250 init_ctx.add_path = add_path;
252 return ntstatus_to_werror(dbwrap_trans_do(regdb,
253 init_registry_key_action,
257 /***********************************************************************
258 Open the registry data in the tdb
259 ***********************************************************************/
261 static void regdb_ctr_add_value(struct regval_ctr *ctr,
262 struct builtin_regkey_value *value)
264 switch(value->type) {
266 regval_ctr_addvalue(ctr, value->valuename, REG_DWORD,
267 (uint8_t *)&value->data.dw_value,
272 regval_ctr_addvalue_sz(ctr, value->valuename,
277 DEBUG(0, ("regdb_ctr_add_value: invalid value type in "
278 "registry values [%d]\n", value->type));
282 static NTSTATUS init_registry_data_action(struct db_context *db,
286 TALLOC_CTX *frame = talloc_stackframe();
287 struct regval_ctr *values;
290 /* loop over all of the predefined paths and add each component */
292 for (i=0; builtin_registry_paths[i] != NULL; i++) {
293 if (regdb_key_exists(db, builtin_registry_paths[i])) {
296 status = werror_to_ntstatus(init_registry_key_internal(db,
297 builtin_registry_paths[i]));
298 if (!NT_STATUS_IS_OK(status)) {
303 /* loop over all of the predefined values and add each component */
305 for (i=0; builtin_registry_values[i].path != NULL; i++) {
308 werr = regval_ctr_init(frame, &values);
309 if (!W_ERROR_IS_OK(werr)) {
310 status = werror_to_ntstatus(werr);
314 regdb_fetch_values_internal(db,
315 builtin_registry_values[i].path,
318 /* preserve existing values across restarts. Only add new ones */
320 if (!regval_ctr_key_exists(values,
321 builtin_registry_values[i].valuename))
323 regdb_ctr_add_value(values,
324 &builtin_registry_values[i]);
325 regdb_store_values_internal(db,
326 builtin_registry_values[i].path,
332 status = NT_STATUS_OK;
340 WERROR init_registry_data(void)
343 TALLOC_CTX *frame = talloc_stackframe();
344 struct regval_ctr *values;
348 * First, check for the existence of the needed keys and values.
349 * If all do already exist, we can save the writes.
351 for (i=0; builtin_registry_paths[i] != NULL; i++) {
352 if (!regdb_key_exists(regdb, builtin_registry_paths[i])) {
357 for (i=0; builtin_registry_values[i].path != NULL; i++) {
358 werr = regval_ctr_init(frame, &values);
359 W_ERROR_NOT_OK_GOTO_DONE(werr);
361 regdb_fetch_values_internal(regdb,
362 builtin_registry_values[i].path,
364 if (!regval_ctr_key_exists(values,
365 builtin_registry_values[i].valuename))
380 * There are potentially quite a few store operations which are all
381 * indiviually wrapped in tdb transactions. Wrapping them in a single
382 * transaction gives just a single transaction_commit() to actually do
383 * its fsync()s. See tdb/common/transaction.c for info about nested
384 * transaction behaviour.
387 werr = ntstatus_to_werror(dbwrap_trans_do(regdb,
388 init_registry_data_action,
396 static int regdb_normalize_keynames_fn(struct db_record *rec,
399 TALLOC_CTX *mem_ctx = talloc_tos();
403 if (rec->key.dptr == NULL || rec->key.dsize == 0) {
407 keyname = strchr((const char *) rec->key.dptr, '/');
409 struct db_record new_rec;
411 keyname = talloc_string_sub(mem_ctx,
412 (const char *) rec->key.dptr,
416 DEBUG(2, ("regdb_normalize_keynames_fn: Convert %s to %s\n",
417 (const char *) rec->key.dptr,
420 new_rec.value.dptr = rec->value.dptr;
421 new_rec.value.dsize = rec->value.dsize;
422 new_rec.key.dptr = (unsigned char *) keyname;
423 new_rec.key.dsize = strlen(keyname);
424 new_rec.private_data = rec->private_data;
426 /* Delete the original record and store the normalized key */
427 status = rec->delete_rec(rec);
428 if (!NT_STATUS_IS_OK(status)) {
429 DEBUG(0,("regdb_normalize_keynames_fn: "
430 "tdb_delete for [%s] failed!\n",
435 status = rec->store(&new_rec, new_rec.value, TDB_REPLACE);
436 if (!NT_STATUS_IS_OK(status)) {
437 DEBUG(0,("regdb_normalize_keynames_fn: "
438 "failed to store new record for [%s]!\n",
447 static WERROR regdb_store_regdb_version(uint32_t version)
450 const char *version_keyname = "INFO/version";
453 return WERR_CAN_NOT_COMPLETE;
456 status = dbwrap_trans_store_int32(regdb, version_keyname, version);
457 if (!NT_STATUS_IS_OK(status)) {
458 DEBUG(1, ("regdb_init: error storing %s = %d: %s\n",
459 version_keyname, version, nt_errstr(status)));
460 return ntstatus_to_werror(status);
462 DEBUG(10, ("regdb_init: stored %s = %d\n",
463 version_keyname, version));
468 static WERROR regdb_upgrade_v1_to_v2(void)
474 mem_ctx = talloc_stackframe();
475 if (mem_ctx == NULL) {
479 rc = regdb->traverse(regdb, regdb_normalize_keynames_fn, mem_ctx);
481 talloc_destroy(mem_ctx);
484 return WERR_REG_IO_FAILURE;
487 werr = regdb_store_regdb_version(REGVER_V2);
491 /***********************************************************************
492 Open the registry database
493 ***********************************************************************/
495 WERROR regdb_init(void)
497 const char *vstring = "INFO/version";
498 uint32 vers_id, expected_version;
502 DEBUG(10, ("regdb_init: incrementing refcount (%d)\n",
508 regdb = db_open(NULL, state_path("registry.tdb"), 0,
509 REG_TDB_FLAGS, O_RDWR, 0600);
511 regdb = db_open(NULL, state_path("registry.tdb"), 0,
512 REG_TDB_FLAGS, O_RDWR|O_CREAT, 0600);
514 werr = ntstatus_to_werror(map_nt_error_from_unix(errno));
515 DEBUG(1,("regdb_init: Failed to open registry %s (%s)\n",
516 state_path("registry.tdb"), strerror(errno) ));
520 DEBUG(10,("regdb_init: Successfully created registry tdb\n"));
525 expected_version = REGVER_V2;
527 vers_id = dbwrap_fetch_int32(regdb, vstring);
529 DEBUG(10, ("regdb_init: registry version uninitialized "
530 "(got %d), initializing to version %d\n",
531 vers_id, expected_version));
533 werr = regdb_store_regdb_version(expected_version);
537 if (vers_id > expected_version || vers_id == 0) {
538 DEBUG(1, ("regdb_init: unknown registry version %d "
539 "(code version = %d), refusing initialization\n",
540 vers_id, expected_version));
541 return WERR_CAN_NOT_COMPLETE;
544 if (vers_id == REGVER_V1) {
545 DEBUG(10, ("regdb_init: got registry db version %d, upgrading "
546 "to version %d\n", REGVER_V1, REGVER_V2));
548 if (regdb->transaction_start(regdb) != 0) {
549 return WERR_REG_IO_FAILURE;
552 werr = regdb_upgrade_v1_to_v2();
553 if (!W_ERROR_IS_OK(werr)) {
554 regdb->transaction_cancel(regdb);
558 if (regdb->transaction_commit(regdb) != 0) {
559 return WERR_REG_IO_FAILURE;
565 /* future upgrade code should go here */
570 /***********************************************************************
571 Open the registry. Must already have been initialized by regdb_init()
572 ***********************************************************************/
574 WERROR regdb_open( void )
576 WERROR result = WERR_OK;
579 DEBUG(10,("regdb_open: incrementing refcount (%d)\n", regdb_refcount));
586 regdb = db_open(NULL, state_path("registry.tdb"), 0,
587 REG_TDB_FLAGS, O_RDWR, 0600);
589 result = ntstatus_to_werror( map_nt_error_from_unix( errno ) );
590 DEBUG(0,("regdb_open: Failed to open %s! (%s)\n",
591 state_path("registry.tdb"), strerror(errno) ));
597 DEBUG(10,("regdb_open: refcount reset (%d)\n", regdb_refcount));
602 /***********************************************************************
603 ***********************************************************************/
605 int regdb_close( void )
607 if (regdb_refcount == 0) {
613 DEBUG(10,("regdb_close: decrementing refcount (%d)\n", regdb_refcount));
615 if ( regdb_refcount > 0 )
618 SMB_ASSERT( regdb_refcount >= 0 );
624 WERROR regdb_transaction_start(void)
626 return (regdb->transaction_start(regdb) == 0) ?
627 WERR_OK : WERR_REG_IO_FAILURE;
630 WERROR regdb_transaction_commit(void)
632 return (regdb->transaction_commit(regdb) == 0) ?
633 WERR_OK : WERR_REG_IO_FAILURE;
636 WERROR regdb_transaction_cancel(void)
638 return (regdb->transaction_cancel(regdb) == 0) ?
639 WERR_OK : WERR_REG_IO_FAILURE;
642 /***********************************************************************
643 return the tdb sequence number of the registry tdb.
644 this is an indicator for the content of the registry
645 having changed. it will change upon regdb_init, too, though.
646 ***********************************************************************/
647 int regdb_get_seqnum(void)
649 return regdb->get_seqnum(regdb);
653 static WERROR regdb_delete_key_with_prefix(struct db_context *db,
658 WERROR werr = WERR_NOMEM;
659 TALLOC_CTX *mem_ctx = talloc_stackframe();
661 if (keyname == NULL) {
662 werr = WERR_INVALID_PARAM;
666 if (prefix == NULL) {
667 path = discard_const_p(char, keyname);
669 path = talloc_asprintf(mem_ctx, "%s\\%s", prefix, keyname);
675 path = normalize_reg_path(mem_ctx, path);
680 werr = ntstatus_to_werror(dbwrap_delete_bystring(db, path));
682 /* treat "not" found" as ok */
683 if (W_ERROR_EQUAL(werr, WERR_NOT_FOUND)) {
688 talloc_free(mem_ctx);
693 static WERROR regdb_delete_values(struct db_context *db, const char *keyname)
695 return regdb_delete_key_with_prefix(db, keyname, REG_VALUE_PREFIX);
698 static WERROR regdb_delete_secdesc(struct db_context *db, const char *keyname)
700 return regdb_delete_key_with_prefix(db, keyname, REG_SECDESC_PREFIX);
703 static WERROR regdb_delete_subkeylist(struct db_context *db, const char *keyname)
705 return regdb_delete_key_with_prefix(db, keyname, NULL);
708 static WERROR regdb_delete_key_lists(struct db_context *db, const char *keyname)
712 werr = regdb_delete_values(db, keyname);
713 if (!W_ERROR_IS_OK(werr)) {
714 DEBUG(1, (__location__ " Deleting %s\\%s failed: %s\n",
715 REG_VALUE_PREFIX, keyname, win_errstr(werr)));
719 werr = regdb_delete_secdesc(db, keyname);
720 if (!W_ERROR_IS_OK(werr)) {
721 DEBUG(1, (__location__ " Deleting %s\\%s failed: %s\n",
722 REG_SECDESC_PREFIX, keyname, win_errstr(werr)));
726 werr = regdb_delete_subkeylist(db, keyname);
727 if (!W_ERROR_IS_OK(werr)) {
728 DEBUG(1, (__location__ " Deleting %s failed: %s\n",
729 keyname, win_errstr(werr)));
737 /***********************************************************************
738 Add subkey strings to the registry tdb under a defined key
739 fmt is the same format as tdb_pack except this function only supports
741 ***********************************************************************/
743 static WERROR regdb_store_keys_internal2(struct db_context *db,
745 struct regsubkey_ctr *ctr)
748 uint8 *buffer = NULL;
751 uint32 num_subkeys = regsubkey_ctr_numkeys(ctr);
752 char *keyname = NULL;
753 TALLOC_CTX *ctx = talloc_stackframe();
757 werr = WERR_INVALID_PARAM;
761 keyname = talloc_strdup(ctx, key);
767 keyname = normalize_reg_path(ctx, keyname);
773 /* allocate some initial memory */
775 buffer = (uint8 *)SMB_MALLOC(1024);
776 if (buffer == NULL) {
783 /* store the number of subkeys */
785 len += tdb_pack(buffer+len, buflen-len, "d", num_subkeys);
787 /* pack all the strings */
789 for (i=0; i<num_subkeys; i++) {
792 thistime = tdb_pack(buffer+len, buflen-len, "f",
793 regsubkey_ctr_specific_key(ctr, i));
794 if (len+thistime > buflen) {
797 * tdb_pack hasn't done anything because of the short
798 * buffer, allocate extra space.
800 buffer = SMB_REALLOC_ARRAY(buffer, uint8_t,
803 DEBUG(0, ("regdb_store_keys: Failed to realloc "
804 "memory of size [%u]\n",
805 (unsigned int)(len+thistime)*2));
809 buflen = (len+thistime)*2;
810 thistime2 = tdb_pack(
811 buffer+len, buflen-len, "f",
812 regsubkey_ctr_specific_key(ctr, i));
813 if (thistime2 != thistime) {
814 DEBUG(0, ("tdb_pack failed\n"));
815 werr = WERR_CAN_NOT_COMPLETE;
822 /* finally write out the data */
826 werr = ntstatus_to_werror(dbwrap_store_bystring(db, keyname, dbuf,
828 W_ERROR_NOT_OK_GOTO_DONE(werr);
831 * Delete a sorted subkey cache for regdb_key_exists, will be
832 * recreated automatically
834 keyname = talloc_asprintf(ctx, "%s\\%s", REG_SORTED_SUBKEYS_PREFIX,
836 if (keyname == NULL) {
841 werr = ntstatus_to_werror(dbwrap_delete_bystring(db, keyname));
843 /* don't treat WERR_NOT_FOUND as an error here */
844 if (W_ERROR_EQUAL(werr, WERR_NOT_FOUND)) {
854 /***********************************************************************
855 Store the new subkey record and create any child key records that
856 do not currently exist
857 ***********************************************************************/
859 struct regdb_store_keys_context {
861 struct regsubkey_ctr *ctr;
864 static NTSTATUS regdb_store_keys_action(struct db_context *db,
867 struct regdb_store_keys_context *store_ctx;
871 struct regsubkey_ctr *subkeys = NULL, *old_subkeys = NULL;
872 char *oldkeyname = NULL;
873 TALLOC_CTX *mem_ctx = talloc_stackframe();
875 store_ctx = (struct regdb_store_keys_context *)private_data;
878 * Re-fetch the old keys inside the transaction
881 werr = regsubkey_ctr_init(mem_ctx, &old_subkeys);
882 W_ERROR_NOT_OK_GOTO_DONE(werr);
884 werr = regdb_fetch_keys_internal(db, store_ctx->key, old_subkeys);
885 if (!W_ERROR_IS_OK(werr) &&
886 !W_ERROR_EQUAL(werr, WERR_NOT_FOUND))
892 * Make the store operation as safe as possible without transactions:
894 * (1) For each subkey removed from ctr compared with old_subkeys:
896 * (a) First delete the value db entry.
898 * (b) Next delete the secdesc db record.
900 * (c) Then delete the subkey list entry.
902 * (2) Now write the list of subkeys of the parent key,
903 * deleting removed entries and adding new ones.
905 * (3) Finally create the subkey list entries for the added keys.
907 * This way if we crash half-way in between deleting the subkeys
908 * and storing the parent's list of subkeys, no old data can pop up
909 * out of the blue when re-adding keys later on.
912 /* (1) delete removed keys' lists (values/secdesc/subkeys) */
914 num_subkeys = regsubkey_ctr_numkeys(old_subkeys);
915 for (i=0; i<num_subkeys; i++) {
916 oldkeyname = regsubkey_ctr_specific_key(old_subkeys, i);
918 if (regsubkey_ctr_key_exists(store_ctx->ctr, oldkeyname)) {
920 * It's still around, don't delete
925 path = talloc_asprintf(mem_ctx, "%s\\%s", store_ctx->key,
932 werr = regdb_delete_key_lists(db, path);
933 W_ERROR_NOT_OK_GOTO_DONE(werr);
938 TALLOC_FREE(old_subkeys);
940 /* (2) store the subkey list for the parent */
942 werr = regdb_store_keys_internal2(db, store_ctx->key, store_ctx->ctr);
943 if (!W_ERROR_IS_OK(werr)) {
944 DEBUG(0,("regdb_store_keys: Failed to store new subkey list "
945 "for parent [%s]: %s\n", store_ctx->key,
950 /* (3) now create records for any subkeys that don't already exist */
952 num_subkeys = regsubkey_ctr_numkeys(store_ctx->ctr);
954 if (num_subkeys == 0) {
955 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
956 W_ERROR_NOT_OK_GOTO_DONE(werr);
958 werr = regdb_store_keys_internal2(db, store_ctx->key, subkeys);
959 if (!W_ERROR_IS_OK(werr)) {
960 DEBUG(0,("regdb_store_keys: Failed to store "
961 "new record for key [%s]: %s\n",
962 store_ctx->key, win_errstr(werr)));
965 TALLOC_FREE(subkeys);
968 for (i=0; i<num_subkeys; i++) {
969 path = talloc_asprintf(mem_ctx, "%s\\%s", store_ctx->key,
970 regsubkey_ctr_specific_key(store_ctx->ctr, i));
975 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
976 W_ERROR_NOT_OK_GOTO_DONE(werr);
978 werr = regdb_fetch_keys_internal(db, path, subkeys);
979 if (!W_ERROR_IS_OK(werr)) {
980 /* create a record with 0 subkeys */
981 werr = regdb_store_keys_internal2(db, path, subkeys);
982 if (!W_ERROR_IS_OK(werr)) {
983 DEBUG(0,("regdb_store_keys: Failed to store "
984 "new record for key [%s]: %s\n", path,
990 TALLOC_FREE(subkeys);
997 talloc_free(mem_ctx);
998 return werror_to_ntstatus(werr);
1001 static bool regdb_store_keys_internal(struct db_context *db, const char *key,
1002 struct regsubkey_ctr *ctr)
1004 int num_subkeys, old_num_subkeys, i;
1005 struct regsubkey_ctr *old_subkeys = NULL;
1006 TALLOC_CTX *ctx = talloc_stackframe();
1009 struct regdb_store_keys_context store_ctx;
1011 if (!regdb_key_is_base_key(key) && !regdb_key_exists(db, key)) {
1016 * fetch a list of the old subkeys so we can determine if anything has
1020 werr = regsubkey_ctr_init(ctx, &old_subkeys);
1021 if (!W_ERROR_IS_OK(werr)) {
1022 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
1026 werr = regdb_fetch_keys_internal(db, key, old_subkeys);
1027 if (!W_ERROR_IS_OK(werr) &&
1028 !W_ERROR_EQUAL(werr, WERR_NOT_FOUND))
1033 num_subkeys = regsubkey_ctr_numkeys(ctr);
1034 old_num_subkeys = regsubkey_ctr_numkeys(old_subkeys);
1035 if ((num_subkeys && old_num_subkeys) &&
1036 (num_subkeys == old_num_subkeys)) {
1038 for (i = 0; i < num_subkeys; i++) {
1039 if (strcmp(regsubkey_ctr_specific_key(ctr, i),
1040 regsubkey_ctr_specific_key(old_subkeys, i))
1046 if (i == num_subkeys) {
1048 * Nothing changed, no point to even start a tdb
1057 TALLOC_FREE(old_subkeys);
1059 store_ctx.key = key;
1060 store_ctx.ctr = ctr;
1062 werr = ntstatus_to_werror(dbwrap_trans_do(db,
1063 regdb_store_keys_action,
1066 ret = W_ERROR_IS_OK(werr);
1074 bool regdb_store_keys(const char *key, struct regsubkey_ctr *ctr)
1076 return regdb_store_keys_internal(regdb, key, ctr);
1080 * create a subkey of a given key
1083 struct regdb_create_subkey_context {
1088 static NTSTATUS regdb_create_subkey_action(struct db_context *db,
1092 struct regdb_create_subkey_context *create_ctx;
1093 struct regsubkey_ctr *subkeys;
1094 TALLOC_CTX *mem_ctx = talloc_stackframe();
1096 create_ctx = (struct regdb_create_subkey_context *)private_data;
1098 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
1099 W_ERROR_NOT_OK_GOTO_DONE(werr);
1101 werr = regdb_fetch_keys_internal(db, create_ctx->key, subkeys);
1102 W_ERROR_NOT_OK_GOTO_DONE(werr);
1104 werr = regsubkey_ctr_addkey(subkeys, create_ctx->subkey);
1105 W_ERROR_NOT_OK_GOTO_DONE(werr);
1107 werr = regdb_store_keys_internal2(db, create_ctx->key, subkeys);
1108 if (!W_ERROR_IS_OK(werr)) {
1109 DEBUG(0, (__location__ " failed to store new subkey list for "
1110 "parent key %s: %s\n", create_ctx->key,
1115 talloc_free(mem_ctx);
1116 return werror_to_ntstatus(werr);
1119 static WERROR regdb_create_subkey(const char *key, const char *subkey)
1122 struct regsubkey_ctr *subkeys;
1123 TALLOC_CTX *mem_ctx = talloc_stackframe();
1124 struct regdb_create_subkey_context create_ctx;
1126 if (!regdb_key_is_base_key(key) && !regdb_key_exists(regdb, key)) {
1127 werr = WERR_NOT_FOUND;
1131 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
1132 W_ERROR_NOT_OK_GOTO_DONE(werr);
1134 werr = regdb_fetch_keys_internal(regdb, key, subkeys);
1135 W_ERROR_NOT_OK_GOTO_DONE(werr);
1137 if (regsubkey_ctr_key_exists(subkeys, subkey)) {
1142 talloc_free(subkeys);
1144 create_ctx.key = key;
1145 create_ctx.subkey = subkey;
1147 werr = ntstatus_to_werror(dbwrap_trans_do(regdb,
1148 regdb_create_subkey_action,
1152 talloc_free(mem_ctx);
1157 * create a subkey of a given key
1160 struct regdb_delete_subkey_context {
1166 static NTSTATUS regdb_delete_subkey_action(struct db_context *db,
1170 struct regdb_delete_subkey_context *delete_ctx;
1171 struct regsubkey_ctr *subkeys;
1172 TALLOC_CTX *mem_ctx = talloc_stackframe();
1174 delete_ctx = (struct regdb_delete_subkey_context *)private_data;
1176 werr = regdb_delete_key_lists(db, delete_ctx->path);
1177 W_ERROR_NOT_OK_GOTO_DONE(werr);
1179 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
1180 W_ERROR_NOT_OK_GOTO_DONE(werr);
1182 werr = regdb_fetch_keys_internal(db, delete_ctx->key, subkeys);
1183 W_ERROR_NOT_OK_GOTO_DONE(werr);
1185 werr = regsubkey_ctr_delkey(subkeys, delete_ctx->subkey);
1186 W_ERROR_NOT_OK_GOTO_DONE(werr);
1188 werr = regdb_store_keys_internal2(db, delete_ctx->key, subkeys);
1189 if (!W_ERROR_IS_OK(werr)) {
1190 DEBUG(0, (__location__ " failed to store new subkey_list for "
1191 "parent key %s: %s\n", delete_ctx->key,
1196 talloc_free(mem_ctx);
1197 return werror_to_ntstatus(werr);
1200 static WERROR regdb_delete_subkey(const char *key, const char *subkey)
1204 struct regdb_delete_subkey_context delete_ctx;
1205 TALLOC_CTX *mem_ctx = talloc_stackframe();
1207 if (!regdb_key_is_base_key(key) && !regdb_key_exists(regdb, key)) {
1208 werr = WERR_NOT_FOUND;
1212 path = talloc_asprintf(mem_ctx, "%s\\%s", key, subkey);
1218 if (!regdb_key_exists(regdb, path)) {
1223 delete_ctx.key = key;
1224 delete_ctx.subkey = subkey;
1225 delete_ctx.path = path;
1227 werr = ntstatus_to_werror(dbwrap_trans_do(regdb,
1228 regdb_delete_subkey_action,
1232 talloc_free(mem_ctx);
1236 static TDB_DATA regdb_fetch_key_internal(struct db_context *db,
1237 TALLOC_CTX *mem_ctx, const char *key)
1242 path = normalize_reg_path(mem_ctx, key);
1244 return make_tdb_data(NULL, 0);
1247 data = dbwrap_fetch_bystring(db, mem_ctx, path);
1255 * check whether a given key name represents a base key,
1256 * i.e one without a subkey separator ('\').
1258 static bool regdb_key_is_base_key(const char *key)
1260 TALLOC_CTX *mem_ctx = talloc_stackframe();
1268 path = normalize_reg_path(mem_ctx, key);
1270 DEBUG(0, ("out of memory! (talloc failed)\n"));
1274 if (*path == '\0') {
1278 ret = (strrchr(path, '\\') == NULL);
1281 TALLOC_FREE(mem_ctx);
1286 * regdb_key_exists() is a very frequent operation. It can be quite
1287 * time-consuming to fully fetch the parent's subkey list, talloc_strdup all
1288 * subkeys and then compare the keyname linearly to all the parent's subkeys.
1290 * The following code tries to make this operation as efficient as possible:
1291 * Per registry key we create a list of subkeys that is very efficient to
1292 * search for existence of a subkey. Its format is:
1294 * 4 bytes num_subkeys
1295 * 4*num_subkey bytes offset into the string array
1296 * then follows a sorted list of subkeys in uppercase
1298 * This record is created by create_sorted_subkeys() on demand if it does not
1299 * exist. scan_parent_subkeys() uses regdb->parse_record to search the sorted
1300 * list, the parsing code and the binary search can be found in
1301 * parent_subkey_scanner. The code uses parse_record() to avoid a memcpy of
1302 * the potentially large subkey record.
1304 * The sorted subkey record is deleted in regdb_store_keys_internal2 and
1305 * recreated on demand.
1308 static int cmp_keynames(char **p1, char **p2)
1310 return StrCaseCmp(*p1, *p2);
1313 struct create_sorted_subkeys_context {
1315 const char *sorted_keyname;
1318 static NTSTATUS create_sorted_subkeys_action(struct db_context *db,
1321 char **sorted_subkeys;
1322 struct regsubkey_ctr *ctr;
1329 struct create_sorted_subkeys_context *sorted_ctx;
1331 sorted_ctx = (struct create_sorted_subkeys_context *)private_data;
1334 * In this function, we only treat failing of the actual write to
1335 * the db as a real error. All preliminary errors, at a stage when
1336 * nothing has been written to the DB yet are treated as success
1337 * to be committed (as an empty transaction).
1339 * The reason is that this (disposable) call might be nested in other
1340 * transactions. Doing a cancel here would destroy the possibility of
1341 * a transaction_commit for transactions that we might be wrapped in.
1344 status = werror_to_ntstatus(regsubkey_ctr_init(talloc_tos(), &ctr));
1345 if (!NT_STATUS_IS_OK(status)) {
1346 /* don't treat this as an error */
1347 status = NT_STATUS_OK;
1351 status = werror_to_ntstatus(regdb_fetch_keys_internal(db,
1354 if (!NT_STATUS_IS_OK(status)) {
1355 /* don't treat this as an error */
1356 status = NT_STATUS_OK;
1360 num_subkeys = regsubkey_ctr_numkeys(ctr);
1361 sorted_subkeys = talloc_array(ctr, char *, num_subkeys);
1362 if (sorted_subkeys == NULL) {
1363 /* don't treat this as an error */
1367 len = 4 + 4*num_subkeys;
1369 for (i = 0; i < num_subkeys; i++) {
1370 sorted_subkeys[i] = talloc_strdup_upper(sorted_subkeys,
1371 regsubkey_ctr_specific_key(ctr, i));
1372 if (sorted_subkeys[i] == NULL) {
1373 /* don't treat this as an error */
1376 len += strlen(sorted_subkeys[i])+1;
1379 TYPESAFE_QSORT(sorted_subkeys, num_subkeys, cmp_keynames);
1381 buf = talloc_array(ctr, char, len);
1383 /* don't treat this as an error */
1386 p = buf + 4 + 4*num_subkeys;
1388 SIVAL(buf, 0, num_subkeys);
1390 for (i=0; i < num_subkeys; i++) {
1391 ptrdiff_t offset = p - buf;
1392 SIVAL(buf, 4 + 4*i, offset);
1393 strlcpy(p, sorted_subkeys[i], len-offset);
1394 p += strlen(sorted_subkeys[i]) + 1;
1397 status = dbwrap_store_bystring(
1398 db, sorted_ctx->sorted_keyname, make_tdb_data((uint8_t *)buf,
1407 static bool create_sorted_subkeys(const char *key, const char *sorted_keyname)
1410 struct create_sorted_subkeys_context sorted_ctx;
1412 sorted_ctx.key = key;
1413 sorted_ctx.sorted_keyname = sorted_keyname;
1415 status = dbwrap_trans_do(regdb,
1416 create_sorted_subkeys_action,
1419 return NT_STATUS_IS_OK(status);
1422 struct scan_subkey_state {
1428 static int parent_subkey_scanner(TDB_DATA key, TDB_DATA data,
1431 struct scan_subkey_state *state =
1432 (struct scan_subkey_state *)private_data;
1433 uint32_t num_subkeys;
1436 if (data.dsize < sizeof(uint32_t)) {
1440 state->scanned = true;
1441 state->found = false;
1443 tdb_unpack(data.dptr, data.dsize, "d", &num_subkeys);
1449 uint32_t idx = (l+u)/2;
1450 char *s = (char *)data.dptr + IVAL(data.dptr, 4 + 4*idx);
1451 int comparison = strcmp(state->name, s);
1453 if (comparison < 0) {
1455 } else if (comparison > 0) {
1458 state->found = true;
1465 static bool scan_parent_subkeys(struct db_context *db, const char *parent,
1470 struct scan_subkey_state state = { 0, };
1471 bool result = false;
1476 path = normalize_reg_path(talloc_tos(), parent);
1481 key = talloc_asprintf(talloc_tos(), "%s\\%s",
1482 REG_SORTED_SUBKEYS_PREFIX, path);
1487 state.name = talloc_strdup_upper(talloc_tos(), name);
1488 if (state.name == NULL) {
1491 state.scanned = false;
1493 res = db->parse_record(db, string_term_tdb_data(key),
1494 parent_subkey_scanner, &state);
1496 if (state.scanned) {
1497 result = state.found;
1499 res = db->transaction_start(db);
1501 DEBUG(0, ("error starting transacion\n"));
1505 if (!create_sorted_subkeys(path, key)) {
1506 res = db->transaction_cancel(db);
1508 smb_panic("Failed to cancel transaction.");
1513 res = db->parse_record(db, string_term_tdb_data(key),
1514 parent_subkey_scanner, &state);
1515 if ((res == 0) && (state.scanned)) {
1516 result = state.found;
1519 res = db->transaction_commit(db);
1521 DEBUG(0, ("error committing transaction\n"));
1528 TALLOC_FREE(state.name);
1533 * Check for the existence of a key.
1535 * Existence of a key is authoritatively defined by its
1536 * existence in the list of subkeys of its parent key.
1537 * The exeption of this are keys without a parent key,
1538 * i.e. the "base" keys (HKLM, HKCU, ...).
1540 static bool regdb_key_exists(struct db_context *db, const char *key)
1542 TALLOC_CTX *mem_ctx = talloc_stackframe();
1551 path = normalize_reg_path(mem_ctx, key);
1553 DEBUG(0, ("out of memory! (talloc failed)\n"));
1557 if (*path == '\0') {
1561 p = strrchr(path, '\\');
1563 /* this is a base key */
1564 value = regdb_fetch_key_internal(db, mem_ctx, path);
1565 ret = (value.dptr != NULL);
1568 ret = scan_parent_subkeys(db, path, p+1);
1572 TALLOC_FREE(mem_ctx);
1577 /***********************************************************************
1578 Retrieve an array of strings containing subkeys. Memory should be
1579 released by the caller.
1580 ***********************************************************************/
1582 static WERROR regdb_fetch_keys_internal(struct db_context *db, const char *key,
1583 struct regsubkey_ctr *ctr)
1591 TALLOC_CTX *frame = talloc_stackframe();
1594 DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key ? key : "NULL"));
1596 frame = talloc_stackframe();
1598 if (!regdb_key_exists(db, key)) {
1599 DEBUG(10, ("key [%s] not found\n", key));
1600 werr = WERR_NOT_FOUND;
1604 werr = regsubkey_ctr_set_seqnum(ctr, db->get_seqnum(db));
1605 W_ERROR_NOT_OK_GOTO_DONE(werr);
1607 value = regdb_fetch_key_internal(db, frame, key);
1609 if (value.dsize == 0 || value.dptr == NULL) {
1610 DEBUG(10, ("regdb_fetch_keys: no subkeys found for key [%s]\n",
1616 buflen = value.dsize;
1617 len = tdb_unpack( buf, buflen, "d", &num_items);
1618 if (len == (uint32_t)-1) {
1619 werr = WERR_NOT_FOUND;
1623 werr = regsubkey_ctr_reinit(ctr);
1624 W_ERROR_NOT_OK_GOTO_DONE(werr);
1626 for (i=0; i<num_items; i++) {
1627 len += tdb_unpack(buf+len, buflen-len, "f", subkeyname);
1628 werr = regsubkey_ctr_addkey(ctr, subkeyname);
1629 if (!W_ERROR_IS_OK(werr)) {
1630 DEBUG(5, ("regdb_fetch_keys: regsubkey_ctr_addkey "
1631 "failed: %s\n", win_errstr(werr)));
1637 DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items));
1644 int regdb_fetch_keys(const char *key, struct regsubkey_ctr *ctr)
1648 werr = regdb_fetch_keys_internal(regdb, key, ctr);
1649 if (!W_ERROR_IS_OK(werr)) {
1653 return regsubkey_ctr_numkeys(ctr);
1656 /****************************************************************************
1657 Unpack a list of registry values frem the TDB
1658 ***************************************************************************/
1660 static int regdb_unpack_values(struct regval_ctr *values, uint8 *buf, int buflen)
1667 uint32 num_values = 0;
1670 /* loop and unpack the rest of the registry values */
1672 len += tdb_unpack(buf+len, buflen-len, "d", &num_values);
1674 for ( i=0; i<num_values; i++ ) {
1675 /* unpack the next regval */
1680 valuename[0] = '\0';
1681 len += tdb_unpack(buf+len, buflen-len, "fdB",
1687 regval_ctr_addvalue(values, valuename, type,
1688 (uint8_t *)data_p, size);
1689 SAFE_FREE(data_p); /* 'B' option to tdb_unpack does a malloc() */
1691 DEBUG(8,("specific: [%s], len: %d\n", valuename, size));
1697 /****************************************************************************
1698 Pack all values in all printer keys
1699 ***************************************************************************/
1701 static int regdb_pack_values(struct regval_ctr *values, uint8 *buf, int buflen)
1705 struct regval_blob *val;
1711 num_values = regval_ctr_numvals( values );
1713 /* pack the number of values first */
1715 len += tdb_pack( buf+len, buflen-len, "d", num_values );
1717 /* loop over all values */
1719 for ( i=0; i<num_values; i++ ) {
1720 val = regval_ctr_specific_value( values, i );
1721 len += tdb_pack(buf+len, buflen-len, "fdB",
1725 regval_data_p(val) );
1731 /***********************************************************************
1732 Retrieve an array of strings containing subkeys. Memory should be
1733 released by the caller.
1734 ***********************************************************************/
1736 static int regdb_fetch_values_internal(struct db_context *db, const char* key,
1737 struct regval_ctr *values)
1739 char *keystr = NULL;
1740 TALLOC_CTX *ctx = talloc_stackframe();
1745 DEBUG(10,("regdb_fetch_values: Looking for value of key [%s] \n", key));
1747 if (!regdb_key_exists(db, key)) {
1751 keystr = talloc_asprintf(ctx, "%s\\%s", REG_VALUE_PREFIX, key);
1756 werr = regval_ctr_set_seqnum(values, db->get_seqnum(db));
1757 W_ERROR_NOT_OK_GOTO_DONE(werr);
1759 value = regdb_fetch_key_internal(db, ctx, keystr);
1762 /* all keys have zero values by default */
1766 regdb_unpack_values(values, value.dptr, value.dsize);
1767 ret = regval_ctr_numvals(values);
1774 int regdb_fetch_values(const char* key, struct regval_ctr *values)
1776 return regdb_fetch_values_internal(regdb, key, values);
1779 static bool regdb_store_values_internal(struct db_context *db, const char *key,
1780 struct regval_ctr *values)
1782 TDB_DATA old_data, data;
1783 char *keystr = NULL;
1784 TALLOC_CTX *ctx = talloc_stackframe();
1787 bool result = false;
1789 DEBUG(10,("regdb_store_values: Looking for value of key [%s] \n", key));
1791 if (!regdb_key_exists(db, key)) {
1797 len = regdb_pack_values(values, data.dptr, data.dsize);
1799 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
1803 data.dptr = TALLOC_ARRAY(ctx, uint8, len);
1806 len = regdb_pack_values(values, data.dptr, data.dsize);
1808 SMB_ASSERT( len == data.dsize );
1810 keystr = talloc_asprintf(ctx, "%s\\%s", REG_VALUE_PREFIX, key );
1814 keystr = normalize_reg_path(ctx, keystr);
1819 old_data = dbwrap_fetch_bystring(db, ctx, keystr);
1821 if ((old_data.dptr != NULL)
1822 && (old_data.dsize == data.dsize)
1823 && (memcmp(old_data.dptr, data.dptr, data.dsize) == 0))
1829 status = dbwrap_trans_store_bystring(db, keystr, data, TDB_REPLACE);
1831 result = NT_STATUS_IS_OK(status);
1838 bool regdb_store_values(const char *key, struct regval_ctr *values)
1840 return regdb_store_values_internal(regdb, key, values);
1843 static WERROR regdb_get_secdesc(TALLOC_CTX *mem_ctx, const char *key,
1844 struct security_descriptor **psecdesc)
1849 TALLOC_CTX *tmp_ctx = talloc_stackframe();
1850 WERROR err = WERR_OK;
1852 DEBUG(10, ("regdb_get_secdesc: Getting secdesc of key [%s]\n", key));
1854 if (!regdb_key_exists(regdb, key)) {
1859 tdbkey = talloc_asprintf(tmp_ctx, "%s\\%s", REG_SECDESC_PREFIX, key);
1860 if (tdbkey == NULL) {
1865 tdbkey = normalize_reg_path(tmp_ctx, tdbkey);
1866 if (tdbkey == NULL) {
1871 data = dbwrap_fetch_bystring(regdb, tmp_ctx, tdbkey);
1872 if (data.dptr == NULL) {
1877 status = unmarshall_sec_desc(mem_ctx, (uint8 *)data.dptr, data.dsize,
1880 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MEMORY)) {
1882 } else if (!NT_STATUS_IS_OK(status)) {
1883 err = WERR_REG_CORRUPT;
1887 TALLOC_FREE(tmp_ctx);
1891 static WERROR regdb_set_secdesc(const char *key,
1892 struct security_descriptor *secdesc)
1894 TALLOC_CTX *mem_ctx = talloc_stackframe();
1896 WERROR err = WERR_NOMEM;
1899 if (!regdb_key_exists(regdb, key)) {
1904 tdbkey = talloc_asprintf(mem_ctx, "%s\\%s", REG_SECDESC_PREFIX, key);
1905 if (tdbkey == NULL) {
1909 tdbkey = normalize_reg_path(mem_ctx, tdbkey);
1910 if (tdbkey == NULL) {
1915 if (secdesc == NULL) {
1916 /* assuming a delete */
1917 err = ntstatus_to_werror(dbwrap_trans_delete_bystring(regdb,
1922 err = ntstatus_to_werror(marshall_sec_desc(mem_ctx, secdesc,
1925 W_ERROR_NOT_OK_GOTO_DONE(err);
1927 err = ntstatus_to_werror(dbwrap_trans_store_bystring(regdb, tdbkey,
1931 TALLOC_FREE(mem_ctx);
1935 bool regdb_subkeys_need_update(struct regsubkey_ctr *subkeys)
1937 return (regdb_get_seqnum() != regsubkey_ctr_get_seqnum(subkeys));
1940 bool regdb_values_need_update(struct regval_ctr *values)
1942 return (regdb_get_seqnum() != regval_ctr_get_seqnum(values));
1946 * Table of function pointers for default access
1949 struct registry_ops regdb_ops = {
1950 .fetch_subkeys = regdb_fetch_keys,
1951 .fetch_values = regdb_fetch_values,
1952 .store_subkeys = regdb_store_keys,
1953 .store_values = regdb_store_values,
1954 .create_subkey = regdb_create_subkey,
1955 .delete_subkey = regdb_delete_subkey,
1956 .get_secdesc = regdb_get_secdesc,
1957 .set_secdesc = regdb_set_secdesc,
1958 .subkeys_need_update = regdb_subkeys_need_update,
1959 .values_need_update = regdb_values_need_update