2 * Unix SMB/CIFS implementation.
3 * Virtual Windows Registry Layer
4 * Copyright (C) Gerald Carter 2002-2005
5 * Copyright (C) Michael Adam 2007-2011
6 * Copyright (C) Gregor Beck 2011
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 /* Implementation of internal registry database functions. */
25 #include "system/filesys.h"
28 #include "reg_util_internal.h"
29 #include "reg_backend_db.h"
30 #include "reg_objects.h"
31 #include "nt_printing.h"
33 #include "dbwrap/dbwrap.h"
34 #include "dbwrap/dbwrap_open.h"
35 #include "../libcli/security/secdesc.h"
38 #define DBGC_CLASS DBGC_REGISTRY
40 static struct db_context *regdb = NULL;
41 static int regdb_refcount;
43 static bool regdb_key_exists(struct db_context *db, const char *key);
44 static WERROR regdb_fetch_keys_internal(struct db_context *db, const char *key,
45 struct regsubkey_ctr *ctr);
46 static bool regdb_store_keys_internal(struct db_context *db, const char *key,
47 struct regsubkey_ctr *ctr);
48 static int regdb_fetch_values_internal(struct db_context *db, const char* key,
49 struct regval_ctr *values);
50 static bool regdb_store_values_internal(struct db_context *db, const char *key,
51 struct regval_ctr *values);
52 static WERROR regdb_store_subkey_list(struct db_context *db, const char *parent,
55 static WERROR regdb_create_basekey(struct db_context *db, const char *key);
56 static WERROR regdb_create_subkey_internal(struct db_context *db,
60 /* List the deepest path into the registry. All part components will be created.*/
62 /* If you want to have a part of the path controlled by the tdb and part by
63 a virtual registry db (e.g. printing), then you have to list the deepest path.
64 For example,"HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion/Print"
65 allows the reg_db backend to handle everything up to
66 "HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion" and then we'll hook
67 the reg_printing backend onto the last component of the path (see
68 KEY_PRINTING_2K in include/rpc_reg.h) --jerry */
70 static const char *builtin_registry_paths[] = {
74 KEY_PRINTING "\\Forms",
75 KEY_PRINTING "\\Printers",
76 KEY_PRINTING "\\Environments\\Windows NT x86\\Print Processors\\winprint",
83 KEY_SAMBA_GROUP_POLICY,
84 KEY_GP_MACHINE_POLICY,
85 KEY_GP_MACHINE_WIN_POLICY,
88 KEY_GP_USER_WIN_POLICY,
89 "HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon\\GPExtensions",
90 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Print\\Monitors",
92 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\DefaultUserConfiguration",
101 struct builtin_regkey_value {
103 const char *valuename;
111 static struct builtin_regkey_value builtin_registry_values[] = {
112 { KEY_PRINTING_PORTS,
113 SAMBA_PRINTER_PORT_NAME, REG_SZ, { "" } },
115 "DefaultSpoolDirectory", REG_SZ, { "C:\\Windows\\System32\\Spool\\Printers" } },
117 "DisplayName", REG_SZ, { "Event Log" } },
119 "ErrorControl", REG_DWORD, { (char*)0x00000001 } },
120 { NULL, NULL, 0, { NULL } }
123 static WERROR create_key_recursive(struct db_context *db,
130 if (subkey == NULL) {
131 return WERR_INVALID_PARAM;
135 return regdb_create_basekey(db, subkey);
138 p = strrchr_m(path, '\\');
141 werr = create_key_recursive(db, NULL, path);
144 werr = create_key_recursive(db, path, p+1);
148 if (!W_ERROR_IS_OK(werr)) {
152 werr = regdb_create_subkey_internal(db, path, subkey);
159 * Initialize a key in the registry:
160 * create each component key of the specified path.
162 static WERROR init_registry_key_internal(struct db_context *db,
163 const char *add_path)
167 TALLOC_CTX *frame = talloc_stackframe();
169 if (add_path == NULL) {
170 werr = WERR_INVALID_PARAM;
174 key = talloc_strdup(frame, add_path);
176 subkey = strrchr_m(key, '\\');
177 if (subkey == NULL) {
185 werr = create_key_recursive(db, key, subkey);
192 struct init_registry_key_context {
193 const char *add_path;
196 static NTSTATUS init_registry_key_action(struct db_context *db,
199 struct init_registry_key_context *init_ctx =
200 (struct init_registry_key_context *)private_data;
202 return werror_to_ntstatus(init_registry_key_internal(
203 db, init_ctx->add_path));
207 * Initialize a key in the registry:
208 * create each component key of the specified path,
209 * wrapped in one db transaction.
211 WERROR init_registry_key(const char *add_path)
213 struct init_registry_key_context init_ctx;
215 if (regdb_key_exists(regdb, add_path)) {
219 init_ctx.add_path = add_path;
221 return ntstatus_to_werror(dbwrap_trans_do(regdb,
222 init_registry_key_action,
226 /***********************************************************************
227 Open the registry data in the tdb
228 ***********************************************************************/
230 static void regdb_ctr_add_value(struct regval_ctr *ctr,
231 struct builtin_regkey_value *value)
233 switch(value->type) {
235 regval_ctr_addvalue(ctr, value->valuename, REG_DWORD,
236 (uint8_t *)&value->data.dw_value,
241 regval_ctr_addvalue_sz(ctr, value->valuename,
246 DEBUG(0, ("regdb_ctr_add_value: invalid value type in "
247 "registry values [%d]\n", value->type));
251 static NTSTATUS init_registry_data_action(struct db_context *db,
255 TALLOC_CTX *frame = talloc_stackframe();
256 struct regval_ctr *values;
259 /* loop over all of the predefined paths and add each component */
261 for (i=0; builtin_registry_paths[i] != NULL; i++) {
262 if (regdb_key_exists(db, builtin_registry_paths[i])) {
265 status = werror_to_ntstatus(init_registry_key_internal(db,
266 builtin_registry_paths[i]));
267 if (!NT_STATUS_IS_OK(status)) {
272 /* loop over all of the predefined values and add each component */
274 for (i=0; builtin_registry_values[i].path != NULL; i++) {
277 werr = regval_ctr_init(frame, &values);
278 if (!W_ERROR_IS_OK(werr)) {
279 status = werror_to_ntstatus(werr);
283 regdb_fetch_values_internal(db,
284 builtin_registry_values[i].path,
287 /* preserve existing values across restarts. Only add new ones */
289 if (!regval_ctr_key_exists(values,
290 builtin_registry_values[i].valuename))
292 regdb_ctr_add_value(values,
293 &builtin_registry_values[i]);
294 regdb_store_values_internal(db,
295 builtin_registry_values[i].path,
301 status = NT_STATUS_OK;
309 WERROR init_registry_data(void)
312 TALLOC_CTX *frame = talloc_stackframe();
313 struct regval_ctr *values;
317 * First, check for the existence of the needed keys and values.
318 * If all do already exist, we can save the writes.
320 for (i=0; builtin_registry_paths[i] != NULL; i++) {
321 if (!regdb_key_exists(regdb, builtin_registry_paths[i])) {
326 for (i=0; builtin_registry_values[i].path != NULL; i++) {
327 werr = regval_ctr_init(frame, &values);
328 W_ERROR_NOT_OK_GOTO_DONE(werr);
330 regdb_fetch_values_internal(regdb,
331 builtin_registry_values[i].path,
333 if (!regval_ctr_key_exists(values,
334 builtin_registry_values[i].valuename))
349 * There are potentially quite a few store operations which are all
350 * indiviually wrapped in tdb transactions. Wrapping them in a single
351 * transaction gives just a single transaction_commit() to actually do
352 * its fsync()s. See tdb/common/transaction.c for info about nested
353 * transaction behaviour.
356 werr = ntstatus_to_werror(dbwrap_trans_do(regdb,
357 init_registry_data_action,
365 static int regdb_normalize_keynames_fn(struct db_record *rec,
368 TALLOC_CTX *mem_ctx = talloc_tos();
372 if (rec->key.dptr == NULL || rec->key.dsize == 0) {
376 keyname = strchr((const char *) rec->key.dptr, '/');
378 struct db_record new_rec;
380 keyname = talloc_string_sub(mem_ctx,
381 (const char *) rec->key.dptr,
385 DEBUG(2, ("regdb_normalize_keynames_fn: Convert %s to %s\n",
386 (const char *) rec->key.dptr,
389 new_rec.value = rec->value;
390 new_rec.key = string_term_tdb_data(keyname);
391 new_rec.private_data = rec->private_data;
393 /* Delete the original record and store the normalized key */
394 status = rec->delete_rec(rec);
395 if (!NT_STATUS_IS_OK(status)) {
396 DEBUG(0,("regdb_normalize_keynames_fn: "
397 "tdb_delete for [%s] failed!\n",
402 status = rec->store(&new_rec, new_rec.value, TDB_REPLACE);
403 if (!NT_STATUS_IS_OK(status)) {
404 DEBUG(0,("regdb_normalize_keynames_fn: "
405 "failed to store new record for [%s]!\n",
414 static WERROR regdb_store_regdb_version(struct db_context *db, uint32_t version)
417 const char *version_keyname = "INFO/version";
420 return WERR_CAN_NOT_COMPLETE;
423 status = dbwrap_trans_store_int32(db, version_keyname, version);
424 if (!NT_STATUS_IS_OK(status)) {
425 DEBUG(1, ("regdb_store_regdb_version: error storing %s = %d: %s\n",
426 version_keyname, version, nt_errstr(status)));
427 return ntstatus_to_werror(status);
429 DEBUG(10, ("regdb_store_regdb_version: stored %s = %d\n",
430 version_keyname, version));
435 static WERROR regdb_upgrade_v1_to_v2(struct db_context *db)
441 mem_ctx = talloc_stackframe();
443 rc = regdb->traverse(db, regdb_normalize_keynames_fn, mem_ctx);
445 talloc_free(mem_ctx);
448 return WERR_REG_IO_FAILURE;
451 werr = regdb_store_regdb_version(db, REGVER_V2);
455 static int regdb_upgrade_v2_to_v3_fn(struct db_record *rec, void *private_data)
462 uint32_t buflen, len;
466 if (rec->key.dptr == NULL || rec->key.dsize == 0) {
470 keyname = (const char *)rec->key.dptr;
472 if (strncmp(keyname, REG_SORTED_SUBKEYS_PREFIX,
473 strlen(REG_SORTED_SUBKEYS_PREFIX)) == 0)
475 /* Delete the deprecated sorted subkeys cache. */
477 DEBUG(10, ("regdb_upgrade_v2_to_v3: deleting [%s]\n", keyname));
479 status = rec->delete_rec(rec);
480 if (!NT_STATUS_IS_OK(status)) {
481 DEBUG(0, ("regdb_upgrade_v2_to_v3: tdb_delete for [%s] "
482 "failed!\n", keyname));
489 if (strncmp(keyname, REG_VALUE_PREFIX, strlen(REG_VALUE_PREFIX)) == 0) {
490 DEBUG(10, ("regdb_upgrade_v2_to_v3: skipping [%s]\n", keyname));
494 if (strncmp(keyname, REG_SECDESC_PREFIX,
495 strlen(REG_SECDESC_PREFIX)) == 0)
497 DEBUG(10, ("regdb_upgrade_v2_to_v3: skipping [%s]\n", keyname));
502 * Found a regular subkey list record.
503 * Walk the list and create the list record for those
504 * subkeys that don't already have one.
506 DEBUG(10, ("regdb_upgrade_v2_to_v3: scanning subkey list of [%s]\n",
509 buf = rec->value.dptr;
510 buflen = rec->value.dsize;
512 len = tdb_unpack(buf, buflen, "d", &num_items);
513 if (len == (uint32_t)-1) {
514 /* invalid or empty - skip */
518 for (i=0; i<num_items; i++) {
519 len += tdb_unpack(buf+len, buflen-len, "f", subkeyname);
520 DEBUG(10, ("regdb_upgrade_v2_to_v3: "
521 "writing subkey list for [%s\\%s]\n",
522 keyname, subkeyname));
523 werr = regdb_store_subkey_list(regdb, keyname, subkeyname);
524 if (!W_ERROR_IS_OK(werr)) {
532 static WERROR regdb_upgrade_v2_to_v3(struct db_context *db)
536 TALLOC_CTX *frame = talloc_stackframe();
538 rc = regdb->traverse(db, regdb_upgrade_v2_to_v3_fn, frame);
540 werr = WERR_REG_IO_FAILURE;
544 werr = regdb_store_regdb_version(db, REGVER_V3);
551 /***********************************************************************
552 Open the registry database
553 ***********************************************************************/
555 WERROR regdb_init(void)
557 const char *vstring = "INFO/version";
558 uint32 vers_id, expected_version;
562 DEBUG(10, ("regdb_init: incrementing refcount (%d->%d)\n",
563 regdb_refcount, regdb_refcount+1));
568 regdb = db_open(NULL, state_path("registry.tdb"), 0,
569 REG_TDB_FLAGS, O_RDWR, 0600);
571 regdb = db_open(NULL, state_path("registry.tdb"), 0,
572 REG_TDB_FLAGS, O_RDWR|O_CREAT, 0600);
574 werr = ntstatus_to_werror(map_nt_error_from_unix(errno));
575 DEBUG(1,("regdb_init: Failed to open registry %s (%s)\n",
576 state_path("registry.tdb"), strerror(errno) ));
580 DEBUG(10,("regdb_init: Successfully created registry tdb\n"));
584 DEBUG(10, ("regdb_init: registry db openend. refcount reset (%d)\n",
587 expected_version = REGVER_V3;
589 vers_id = dbwrap_fetch_int32(regdb, vstring);
591 DEBUG(10, ("regdb_init: registry version uninitialized "
592 "(got %d), initializing to version %d\n",
593 vers_id, expected_version));
595 werr = regdb_store_regdb_version(regdb, expected_version);
599 if (vers_id > expected_version || vers_id == 0) {
600 DEBUG(1, ("regdb_init: unknown registry version %d "
601 "(code version = %d), refusing initialization\n",
602 vers_id, expected_version));
603 return WERR_CAN_NOT_COMPLETE;
606 if (regdb->transaction_start(regdb) != 0) {
607 return WERR_REG_IO_FAILURE;
610 if (vers_id == REGVER_V1) {
611 DEBUG(10, ("regdb_init: upgrading registry fromversion %d "
612 "to %d\n", REGVER_V1, REGVER_V2));
614 werr = regdb_upgrade_v1_to_v2(regdb);
615 if (!W_ERROR_IS_OK(werr)) {
616 regdb->transaction_cancel(regdb);
623 if (vers_id == REGVER_V2) {
624 DEBUG(10, ("regdb_init: upgrading registry from version %d "
625 "to %d\n", REGVER_V2, REGVER_V3));
627 werr = regdb_upgrade_v2_to_v3(regdb);
628 if (!W_ERROR_IS_OK(werr)) {
629 regdb->transaction_cancel(regdb);
636 /* future upgrade code should go here */
638 if (regdb->transaction_commit(regdb) != 0) {
639 return WERR_REG_IO_FAILURE;
645 /***********************************************************************
646 Open the registry. Must already have been initialized by regdb_init()
647 ***********************************************************************/
649 WERROR regdb_open( void )
651 WERROR result = WERR_OK;
654 DEBUG(10, ("regdb_open: incrementing refcount (%d->%d)\n",
655 regdb_refcount, regdb_refcount+1));
662 regdb = db_open(NULL, state_path("registry.tdb"), 0,
663 REG_TDB_FLAGS, O_RDWR, 0600);
665 result = ntstatus_to_werror( map_nt_error_from_unix( errno ) );
666 DEBUG(0,("regdb_open: Failed to open %s! (%s)\n",
667 state_path("registry.tdb"), strerror(errno) ));
673 DEBUG(10, ("regdb_open: registry db opened. refcount reset (%d)\n",
679 /***********************************************************************
680 ***********************************************************************/
682 int regdb_close( void )
684 if (regdb_refcount == 0) {
690 DEBUG(10, ("regdb_close: decrementing refcount (%d->%d)\n",
691 regdb_refcount+1, regdb_refcount));
693 if ( regdb_refcount > 0 )
696 SMB_ASSERT( regdb_refcount >= 0 );
702 WERROR regdb_transaction_start(void)
704 return (regdb->transaction_start(regdb) == 0) ?
705 WERR_OK : WERR_REG_IO_FAILURE;
708 WERROR regdb_transaction_commit(void)
710 return (regdb->transaction_commit(regdb) == 0) ?
711 WERR_OK : WERR_REG_IO_FAILURE;
714 WERROR regdb_transaction_cancel(void)
716 return (regdb->transaction_cancel(regdb) == 0) ?
717 WERR_OK : WERR_REG_IO_FAILURE;
720 /***********************************************************************
721 return the tdb sequence number of the registry tdb.
722 this is an indicator for the content of the registry
723 having changed. it will change upon regdb_init, too, though.
724 ***********************************************************************/
725 int regdb_get_seqnum(void)
727 return regdb->get_seqnum(regdb);
731 static WERROR regdb_delete_key_with_prefix(struct db_context *db,
736 WERROR werr = WERR_NOMEM;
737 TALLOC_CTX *mem_ctx = talloc_stackframe();
739 if (keyname == NULL) {
740 werr = WERR_INVALID_PARAM;
744 if (prefix == NULL) {
745 path = discard_const_p(char, keyname);
747 path = talloc_asprintf(mem_ctx, "%s\\%s", prefix, keyname);
753 path = normalize_reg_path(mem_ctx, path);
758 werr = ntstatus_to_werror(dbwrap_delete_bystring(db, path));
760 /* treat "not found" as ok */
761 if (W_ERROR_EQUAL(werr, WERR_NOT_FOUND)) {
766 talloc_free(mem_ctx);
771 static WERROR regdb_delete_values(struct db_context *db, const char *keyname)
773 return regdb_delete_key_with_prefix(db, keyname, REG_VALUE_PREFIX);
776 static WERROR regdb_delete_secdesc(struct db_context *db, const char *keyname)
778 return regdb_delete_key_with_prefix(db, keyname, REG_SECDESC_PREFIX);
781 static WERROR regdb_delete_subkeylist(struct db_context *db, const char *keyname)
783 return regdb_delete_key_with_prefix(db, keyname, NULL);
787 static WERROR regdb_delete_key_lists(struct db_context *db, const char *keyname)
791 werr = regdb_delete_values(db, keyname);
792 if (!W_ERROR_IS_OK(werr)) {
793 DEBUG(1, (__location__ " Deleting %s\\%s failed: %s\n",
794 REG_VALUE_PREFIX, keyname, win_errstr(werr)));
798 werr = regdb_delete_secdesc(db, keyname);
799 if (!W_ERROR_IS_OK(werr)) {
800 DEBUG(1, (__location__ " Deleting %s\\%s failed: %s\n",
801 REG_SECDESC_PREFIX, keyname, win_errstr(werr)));
805 werr = regdb_delete_subkeylist(db, keyname);
806 if (!W_ERROR_IS_OK(werr)) {
807 DEBUG(1, (__location__ " Deleting %s failed: %s\n",
808 keyname, win_errstr(werr)));
816 /***********************************************************************
817 Add subkey strings to the registry tdb under a defined key
818 fmt is the same format as tdb_pack except this function only supports
820 ***********************************************************************/
822 static WERROR regdb_store_keys_internal2(struct db_context *db,
824 struct regsubkey_ctr *ctr)
827 uint8 *buffer = NULL;
830 uint32 num_subkeys = regsubkey_ctr_numkeys(ctr);
831 char *keyname = NULL;
832 TALLOC_CTX *ctx = talloc_stackframe();
836 werr = WERR_INVALID_PARAM;
840 keyname = talloc_strdup(ctx, key);
846 keyname = normalize_reg_path(ctx, keyname);
852 /* allocate some initial memory */
854 buffer = (uint8 *)SMB_MALLOC(1024);
855 if (buffer == NULL) {
862 /* store the number of subkeys */
864 len += tdb_pack(buffer+len, buflen-len, "d", num_subkeys);
866 /* pack all the strings */
868 for (i=0; i<num_subkeys; i++) {
871 thistime = tdb_pack(buffer+len, buflen-len, "f",
872 regsubkey_ctr_specific_key(ctr, i));
873 if (len+thistime > buflen) {
876 * tdb_pack hasn't done anything because of the short
877 * buffer, allocate extra space.
879 buffer = SMB_REALLOC_ARRAY(buffer, uint8_t,
882 DEBUG(0, ("regdb_store_keys: Failed to realloc "
883 "memory of size [%u]\n",
884 (unsigned int)(len+thistime)*2));
888 buflen = (len+thistime)*2;
889 thistime2 = tdb_pack(
890 buffer+len, buflen-len, "f",
891 regsubkey_ctr_specific_key(ctr, i));
892 if (thistime2 != thistime) {
893 DEBUG(0, ("tdb_pack failed\n"));
894 werr = WERR_CAN_NOT_COMPLETE;
901 /* finally write out the data */
905 werr = ntstatus_to_werror(dbwrap_store_bystring(db, keyname, dbuf,
915 * Utility function to store a new empty list of
916 * subkeys of given key specified as parent and subkey name
917 * (thereby creating the key).
918 * If the parent keyname is NULL, then the "subkey" is
919 * interpreted as a base key.
920 * If the subkey list does already exist, it is not modified.
922 * Must be called from within a transaction.
924 static WERROR regdb_store_subkey_list(struct db_context *db, const char *parent,
929 struct regsubkey_ctr *subkeys = NULL;
930 TALLOC_CTX *frame = talloc_stackframe();
932 if (parent == NULL) {
933 path = talloc_strdup(frame, key);
935 path = talloc_asprintf(frame, "%s\\%s", parent, key);
942 werr = regsubkey_ctr_init(frame, &subkeys);
943 W_ERROR_NOT_OK_GOTO_DONE(werr);
945 werr = regdb_fetch_keys_internal(db, path, subkeys);
946 if (W_ERROR_IS_OK(werr)) {
947 /* subkey list exists already - don't modify */
951 werr = regsubkey_ctr_reinit(subkeys);
952 W_ERROR_NOT_OK_GOTO_DONE(werr);
954 /* create a record with 0 subkeys */
955 werr = regdb_store_keys_internal2(db, path, subkeys);
956 if (!W_ERROR_IS_OK(werr)) {
957 DEBUG(0, ("regdb_store_keys: Failed to store new record for "
958 "key [%s]: %s\n", path, win_errstr(werr)));
967 /***********************************************************************
968 Store the new subkey record and create any child key records that
969 do not currently exist
970 ***********************************************************************/
972 struct regdb_store_keys_context {
974 struct regsubkey_ctr *ctr;
977 static NTSTATUS regdb_store_keys_action(struct db_context *db,
980 struct regdb_store_keys_context *store_ctx;
984 struct regsubkey_ctr *old_subkeys = NULL;
985 char *oldkeyname = NULL;
986 TALLOC_CTX *mem_ctx = talloc_stackframe();
988 store_ctx = (struct regdb_store_keys_context *)private_data;
991 * Re-fetch the old keys inside the transaction
994 werr = regsubkey_ctr_init(mem_ctx, &old_subkeys);
995 W_ERROR_NOT_OK_GOTO_DONE(werr);
997 werr = regdb_fetch_keys_internal(db, store_ctx->key, old_subkeys);
998 if (!W_ERROR_IS_OK(werr) &&
999 !W_ERROR_EQUAL(werr, WERR_NOT_FOUND))
1005 * Make the store operation as safe as possible without transactions:
1007 * (1) For each subkey removed from ctr compared with old_subkeys:
1009 * (a) First delete the value db entry.
1011 * (b) Next delete the secdesc db record.
1013 * (c) Then delete the subkey list entry.
1015 * (2) Now write the list of subkeys of the parent key,
1016 * deleting removed entries and adding new ones.
1018 * (3) Finally create the subkey list entries for the added keys.
1020 * This way if we crash half-way in between deleting the subkeys
1021 * and storing the parent's list of subkeys, no old data can pop up
1022 * out of the blue when re-adding keys later on.
1025 /* (1) delete removed keys' lists (values/secdesc/subkeys) */
1027 num_subkeys = regsubkey_ctr_numkeys(old_subkeys);
1028 for (i=0; i<num_subkeys; i++) {
1029 oldkeyname = regsubkey_ctr_specific_key(old_subkeys, i);
1031 if (regsubkey_ctr_key_exists(store_ctx->ctr, oldkeyname)) {
1033 * It's still around, don't delete
1038 path = talloc_asprintf(mem_ctx, "%s\\%s", store_ctx->key,
1045 werr = regdb_delete_key_lists(db, path);
1046 W_ERROR_NOT_OK_GOTO_DONE(werr);
1051 TALLOC_FREE(old_subkeys);
1053 /* (2) store the subkey list for the parent */
1055 werr = regdb_store_keys_internal2(db, store_ctx->key, store_ctx->ctr);
1056 if (!W_ERROR_IS_OK(werr)) {
1057 DEBUG(0,("regdb_store_keys: Failed to store new subkey list "
1058 "for parent [%s]: %s\n", store_ctx->key,
1063 /* (3) now create records for any subkeys that don't already exist */
1065 num_subkeys = regsubkey_ctr_numkeys(store_ctx->ctr);
1067 for (i=0; i<num_subkeys; i++) {
1070 subkey = regsubkey_ctr_specific_key(store_ctx->ctr, i);
1072 werr = regdb_store_subkey_list(db, store_ctx->key, subkey);
1073 W_ERROR_NOT_OK_GOTO_DONE(werr);
1079 talloc_free(mem_ctx);
1080 return werror_to_ntstatus(werr);
1083 static bool regdb_store_keys_internal(struct db_context *db, const char *key,
1084 struct regsubkey_ctr *ctr)
1086 int num_subkeys, old_num_subkeys, i;
1087 struct regsubkey_ctr *old_subkeys = NULL;
1088 TALLOC_CTX *ctx = talloc_stackframe();
1091 struct regdb_store_keys_context store_ctx;
1093 if (!regdb_key_exists(db, key)) {
1098 * fetch a list of the old subkeys so we can determine if anything has
1102 werr = regsubkey_ctr_init(ctx, &old_subkeys);
1103 if (!W_ERROR_IS_OK(werr)) {
1104 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
1108 werr = regdb_fetch_keys_internal(db, key, old_subkeys);
1109 if (!W_ERROR_IS_OK(werr) &&
1110 !W_ERROR_EQUAL(werr, WERR_NOT_FOUND))
1115 num_subkeys = regsubkey_ctr_numkeys(ctr);
1116 old_num_subkeys = regsubkey_ctr_numkeys(old_subkeys);
1117 if ((num_subkeys && old_num_subkeys) &&
1118 (num_subkeys == old_num_subkeys)) {
1120 for (i = 0; i < num_subkeys; i++) {
1121 if (strcmp(regsubkey_ctr_specific_key(ctr, i),
1122 regsubkey_ctr_specific_key(old_subkeys, i))
1128 if (i == num_subkeys) {
1130 * Nothing changed, no point to even start a tdb
1139 TALLOC_FREE(old_subkeys);
1141 store_ctx.key = key;
1142 store_ctx.ctr = ctr;
1144 werr = ntstatus_to_werror(dbwrap_trans_do(db,
1145 regdb_store_keys_action,
1148 ret = W_ERROR_IS_OK(werr);
1156 bool regdb_store_keys(const char *key, struct regsubkey_ctr *ctr)
1158 return regdb_store_keys_internal(regdb, key, ctr);
1162 * create a subkey of a given key
1165 struct regdb_create_subkey_context {
1170 static NTSTATUS regdb_create_subkey_action(struct db_context *db,
1174 struct regdb_create_subkey_context *create_ctx;
1175 struct regsubkey_ctr *subkeys;
1176 TALLOC_CTX *mem_ctx = talloc_stackframe();
1178 create_ctx = (struct regdb_create_subkey_context *)private_data;
1180 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
1181 W_ERROR_NOT_OK_GOTO_DONE(werr);
1183 werr = regdb_fetch_keys_internal(db, create_ctx->key, subkeys);
1184 W_ERROR_NOT_OK_GOTO_DONE(werr);
1186 werr = regsubkey_ctr_addkey(subkeys, create_ctx->subkey);
1187 W_ERROR_NOT_OK_GOTO_DONE(werr);
1189 werr = regdb_store_keys_internal2(db, create_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", create_ctx->key,
1196 werr = regdb_store_subkey_list(db, create_ctx->key, create_ctx->subkey);
1199 talloc_free(mem_ctx);
1200 return werror_to_ntstatus(werr);
1203 static WERROR regdb_create_subkey_internal(struct db_context *db,
1208 struct regsubkey_ctr *subkeys;
1209 TALLOC_CTX *mem_ctx = talloc_stackframe();
1210 struct regdb_create_subkey_context create_ctx;
1212 if (!regdb_key_exists(db, key)) {
1213 werr = WERR_NOT_FOUND;
1217 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
1218 W_ERROR_NOT_OK_GOTO_DONE(werr);
1220 werr = regdb_fetch_keys_internal(db, key, subkeys);
1221 W_ERROR_NOT_OK_GOTO_DONE(werr);
1223 if (regsubkey_ctr_key_exists(subkeys, subkey)) {
1228 talloc_free(subkeys);
1230 create_ctx.key = key;
1231 create_ctx.subkey = subkey;
1233 werr = ntstatus_to_werror(dbwrap_trans_do(db,
1234 regdb_create_subkey_action,
1238 talloc_free(mem_ctx);
1242 static WERROR regdb_create_subkey(const char *key, const char *subkey)
1244 return regdb_create_subkey_internal(regdb, key, subkey);
1251 struct regdb_create_basekey_context {
1255 static NTSTATUS regdb_create_basekey_action(struct db_context *db,
1259 struct regdb_create_basekey_context *create_ctx;
1261 create_ctx = (struct regdb_create_basekey_context *)private_data;
1263 werr = regdb_store_subkey_list(db, NULL, create_ctx->key);
1265 return werror_to_ntstatus(werr);
1268 static WERROR regdb_create_basekey(struct db_context *db, const char *key)
1271 struct regdb_create_subkey_context create_ctx;
1273 create_ctx.key = key;
1275 werr = ntstatus_to_werror(dbwrap_trans_do(db,
1276 regdb_create_basekey_action,
1283 * create a subkey of a given key
1286 struct regdb_delete_subkey_context {
1292 static NTSTATUS regdb_delete_subkey_action(struct db_context *db,
1296 struct regdb_delete_subkey_context *delete_ctx;
1297 struct regsubkey_ctr *subkeys;
1298 TALLOC_CTX *mem_ctx = talloc_stackframe();
1300 delete_ctx = (struct regdb_delete_subkey_context *)private_data;
1302 werr = regdb_delete_key_lists(db, delete_ctx->path);
1303 W_ERROR_NOT_OK_GOTO_DONE(werr);
1305 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
1306 W_ERROR_NOT_OK_GOTO_DONE(werr);
1308 werr = regdb_fetch_keys_internal(db, delete_ctx->key, subkeys);
1309 W_ERROR_NOT_OK_GOTO_DONE(werr);
1311 werr = regsubkey_ctr_delkey(subkeys, delete_ctx->subkey);
1312 W_ERROR_NOT_OK_GOTO_DONE(werr);
1314 werr = regdb_store_keys_internal2(db, delete_ctx->key, subkeys);
1315 if (!W_ERROR_IS_OK(werr)) {
1316 DEBUG(0, (__location__ " failed to store new subkey_list for "
1317 "parent key %s: %s\n", delete_ctx->key,
1322 talloc_free(mem_ctx);
1323 return werror_to_ntstatus(werr);
1326 static WERROR regdb_delete_subkey(const char *key, const char *subkey)
1330 struct regdb_delete_subkey_context delete_ctx;
1331 TALLOC_CTX *mem_ctx = talloc_stackframe();
1333 if (!regdb_key_exists(regdb, key)) {
1334 werr = WERR_NOT_FOUND;
1338 path = talloc_asprintf(mem_ctx, "%s\\%s", key, subkey);
1344 if (!regdb_key_exists(regdb, path)) {
1349 delete_ctx.key = key;
1350 delete_ctx.subkey = subkey;
1351 delete_ctx.path = path;
1353 werr = ntstatus_to_werror(dbwrap_trans_do(regdb,
1354 regdb_delete_subkey_action,
1358 talloc_free(mem_ctx);
1362 static TDB_DATA regdb_fetch_key_internal(struct db_context *db,
1363 TALLOC_CTX *mem_ctx, const char *key)
1368 path = normalize_reg_path(mem_ctx, key);
1370 return make_tdb_data(NULL, 0);
1373 data = dbwrap_fetch_bystring(db, mem_ctx, path);
1381 * Check for the existence of a key.
1383 * Existence of a key is authoritatively defined by
1384 * the existence of the record that contains the list
1387 static bool regdb_key_exists(struct db_context *db, const char *key)
1389 TALLOC_CTX *mem_ctx = talloc_stackframe();
1398 path = normalize_reg_path(mem_ctx, key);
1400 DEBUG(0, ("out of memory! (talloc failed)\n"));
1404 if (*path == '\0') {
1408 value = regdb_fetch_key_internal(db, mem_ctx, path);
1409 ret = (value.dptr != NULL);
1412 TALLOC_FREE(mem_ctx);
1417 /***********************************************************************
1418 Retrieve an array of strings containing subkeys. Memory should be
1419 released by the caller.
1420 ***********************************************************************/
1422 static WERROR regdb_fetch_keys_internal(struct db_context *db, const char *key,
1423 struct regsubkey_ctr *ctr)
1431 TALLOC_CTX *frame = talloc_stackframe();
1434 DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key ? key : "NULL"));
1436 if (!regdb_key_exists(db, key)) {
1437 DEBUG(10, ("key [%s] not found\n", key));
1438 werr = WERR_NOT_FOUND;
1442 werr = regsubkey_ctr_reinit(ctr);
1443 W_ERROR_NOT_OK_GOTO_DONE(werr);
1445 werr = regsubkey_ctr_set_seqnum(ctr, db->get_seqnum(db));
1446 W_ERROR_NOT_OK_GOTO_DONE(werr);
1448 value = regdb_fetch_key_internal(db, frame, key);
1450 if (value.dsize == 0 || value.dptr == NULL) {
1451 DEBUG(10, ("regdb_fetch_keys: no subkeys found for key [%s]\n",
1457 buflen = value.dsize;
1458 len = tdb_unpack( buf, buflen, "d", &num_items);
1459 if (len == (uint32_t)-1) {
1460 werr = WERR_NOT_FOUND;
1464 for (i=0; i<num_items; i++) {
1465 len += tdb_unpack(buf+len, buflen-len, "f", subkeyname);
1466 werr = regsubkey_ctr_addkey(ctr, subkeyname);
1467 if (!W_ERROR_IS_OK(werr)) {
1468 DEBUG(5, ("regdb_fetch_keys: regsubkey_ctr_addkey "
1469 "failed: %s\n", win_errstr(werr)));
1475 DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items));
1482 int regdb_fetch_keys(const char *key, struct regsubkey_ctr *ctr)
1486 werr = regdb_fetch_keys_internal(regdb, key, ctr);
1487 if (!W_ERROR_IS_OK(werr)) {
1491 return regsubkey_ctr_numkeys(ctr);
1494 /****************************************************************************
1495 Unpack a list of registry values frem the TDB
1496 ***************************************************************************/
1498 static int regdb_unpack_values(struct regval_ctr *values, uint8 *buf, int buflen)
1505 uint32 num_values = 0;
1508 /* loop and unpack the rest of the registry values */
1510 len += tdb_unpack(buf+len, buflen-len, "d", &num_values);
1512 for ( i=0; i<num_values; i++ ) {
1513 /* unpack the next regval */
1518 valuename[0] = '\0';
1519 len += tdb_unpack(buf+len, buflen-len, "fdB",
1525 regval_ctr_addvalue(values, valuename, type,
1526 (uint8_t *)data_p, size);
1527 SAFE_FREE(data_p); /* 'B' option to tdb_unpack does a malloc() */
1529 DEBUG(8,("specific: [%s], len: %d\n", valuename, size));
1535 /****************************************************************************
1536 Pack all values in all printer keys
1537 ***************************************************************************/
1539 static int regdb_pack_values(struct regval_ctr *values, uint8 *buf, int buflen)
1543 struct regval_blob *val;
1549 num_values = regval_ctr_numvals( values );
1551 /* pack the number of values first */
1553 len += tdb_pack( buf+len, buflen-len, "d", num_values );
1555 /* loop over all values */
1557 for ( i=0; i<num_values; i++ ) {
1558 val = regval_ctr_specific_value( values, i );
1559 len += tdb_pack(buf+len, buflen-len, "fdB",
1563 regval_data_p(val) );
1569 /***********************************************************************
1570 Retrieve an array of strings containing subkeys. Memory should be
1571 released by the caller.
1572 ***********************************************************************/
1574 static int regdb_fetch_values_internal(struct db_context *db, const char* key,
1575 struct regval_ctr *values)
1577 char *keystr = NULL;
1578 TALLOC_CTX *ctx = talloc_stackframe();
1583 DEBUG(10,("regdb_fetch_values: Looking for value of key [%s] \n", key));
1585 if (!regdb_key_exists(db, key)) {
1589 keystr = talloc_asprintf(ctx, "%s\\%s", REG_VALUE_PREFIX, key);
1594 werr = regval_ctr_set_seqnum(values, db->get_seqnum(db));
1595 W_ERROR_NOT_OK_GOTO_DONE(werr);
1597 value = regdb_fetch_key_internal(db, ctx, keystr);
1600 /* all keys have zero values by default */
1604 regdb_unpack_values(values, value.dptr, value.dsize);
1605 ret = regval_ctr_numvals(values);
1612 int regdb_fetch_values(const char* key, struct regval_ctr *values)
1614 return regdb_fetch_values_internal(regdb, key, values);
1617 static bool regdb_store_values_internal(struct db_context *db, const char *key,
1618 struct regval_ctr *values)
1620 TDB_DATA old_data, data;
1621 char *keystr = NULL;
1622 TALLOC_CTX *ctx = talloc_stackframe();
1625 bool result = false;
1627 DEBUG(10,("regdb_store_values: Looking for value of key [%s] \n", key));
1629 if (!regdb_key_exists(db, key)) {
1635 len = regdb_pack_values(values, data.dptr, data.dsize);
1637 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
1641 data.dptr = talloc_array(ctx, uint8, len);
1644 len = regdb_pack_values(values, data.dptr, data.dsize);
1646 SMB_ASSERT( len == data.dsize );
1648 keystr = talloc_asprintf(ctx, "%s\\%s", REG_VALUE_PREFIX, key );
1652 keystr = normalize_reg_path(ctx, keystr);
1657 old_data = dbwrap_fetch_bystring(db, ctx, keystr);
1659 if ((old_data.dptr != NULL)
1660 && (old_data.dsize == data.dsize)
1661 && (memcmp(old_data.dptr, data.dptr, data.dsize) == 0))
1667 status = dbwrap_trans_store_bystring(db, keystr, data, TDB_REPLACE);
1669 result = NT_STATUS_IS_OK(status);
1676 bool regdb_store_values(const char *key, struct regval_ctr *values)
1678 return regdb_store_values_internal(regdb, key, values);
1681 static WERROR regdb_get_secdesc(TALLOC_CTX *mem_ctx, const char *key,
1682 struct security_descriptor **psecdesc)
1687 TALLOC_CTX *tmp_ctx = talloc_stackframe();
1688 WERROR err = WERR_OK;
1690 DEBUG(10, ("regdb_get_secdesc: Getting secdesc of key [%s]\n", key));
1692 if (!regdb_key_exists(regdb, key)) {
1697 tdbkey = talloc_asprintf(tmp_ctx, "%s\\%s", REG_SECDESC_PREFIX, key);
1698 if (tdbkey == NULL) {
1703 tdbkey = normalize_reg_path(tmp_ctx, tdbkey);
1704 if (tdbkey == NULL) {
1709 data = dbwrap_fetch_bystring(regdb, tmp_ctx, tdbkey);
1710 if (data.dptr == NULL) {
1715 status = unmarshall_sec_desc(mem_ctx, (uint8 *)data.dptr, data.dsize,
1718 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MEMORY)) {
1720 } else if (!NT_STATUS_IS_OK(status)) {
1721 err = WERR_REG_CORRUPT;
1725 TALLOC_FREE(tmp_ctx);
1729 static WERROR regdb_set_secdesc(const char *key,
1730 struct security_descriptor *secdesc)
1732 TALLOC_CTX *mem_ctx = talloc_stackframe();
1734 WERROR err = WERR_NOMEM;
1737 if (!regdb_key_exists(regdb, key)) {
1742 tdbkey = talloc_asprintf(mem_ctx, "%s\\%s", REG_SECDESC_PREFIX, key);
1743 if (tdbkey == NULL) {
1747 tdbkey = normalize_reg_path(mem_ctx, tdbkey);
1748 if (tdbkey == NULL) {
1753 if (secdesc == NULL) {
1754 /* assuming a delete */
1755 err = ntstatus_to_werror(dbwrap_trans_delete_bystring(regdb,
1760 err = ntstatus_to_werror(marshall_sec_desc(mem_ctx, secdesc,
1763 W_ERROR_NOT_OK_GOTO_DONE(err);
1765 err = ntstatus_to_werror(dbwrap_trans_store_bystring(regdb, tdbkey,
1769 TALLOC_FREE(mem_ctx);
1773 bool regdb_subkeys_need_update(struct regsubkey_ctr *subkeys)
1775 return (regdb_get_seqnum() != regsubkey_ctr_get_seqnum(subkeys));
1778 bool regdb_values_need_update(struct regval_ctr *values)
1780 return (regdb_get_seqnum() != regval_ctr_get_seqnum(values));
1784 * Table of function pointers for default access
1787 struct registry_ops regdb_ops = {
1788 .fetch_subkeys = regdb_fetch_keys,
1789 .fetch_values = regdb_fetch_values,
1790 .store_subkeys = regdb_store_keys,
1791 .store_values = regdb_store_values,
1792 .create_subkey = regdb_create_subkey,
1793 .delete_subkey = regdb_delete_subkey,
1794 .get_secdesc = regdb_get_secdesc,
1795 .set_secdesc = regdb_set_secdesc,
1796 .subkeys_need_update = regdb_subkeys_need_update,
1797 .values_need_update = regdb_values_need_update