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();
371 struct db_context *db = (struct db_context *)private_data;
373 if (rec->key.dptr == NULL || rec->key.dsize == 0) {
378 DEBUG(0, ("regdb_normalize_keynames_fn: ERROR: "
379 "NULL db context handed in via private_data\n"));
383 keyname = strchr((const char *) rec->key.dptr, '/');
385 keyname = talloc_string_sub(mem_ctx,
386 (const char *) rec->key.dptr,
390 DEBUG(2, ("regdb_normalize_keynames_fn: Convert %s to %s\n",
391 (const char *) rec->key.dptr,
394 /* Delete the original record and store the normalized key */
395 status = rec->delete_rec(rec);
396 if (!NT_STATUS_IS_OK(status)) {
397 DEBUG(0,("regdb_normalize_keynames_fn: "
398 "tdb_delete for [%s] failed!\n",
403 status = dbwrap_store_bystring(db, keyname, rec->value,
405 if (!NT_STATUS_IS_OK(status)) {
406 DEBUG(0,("regdb_normalize_keynames_fn: "
407 "failed to store new record for [%s]!\n",
416 static WERROR regdb_store_regdb_version(struct db_context *db, uint32_t version)
419 const char *version_keyname = "INFO/version";
422 return WERR_CAN_NOT_COMPLETE;
425 status = dbwrap_trans_store_int32(db, version_keyname, version);
426 if (!NT_STATUS_IS_OK(status)) {
427 DEBUG(1, ("regdb_store_regdb_version: error storing %s = %d: %s\n",
428 version_keyname, version, nt_errstr(status)));
429 return ntstatus_to_werror(status);
431 DEBUG(10, ("regdb_store_regdb_version: stored %s = %d\n",
432 version_keyname, version));
437 static WERROR regdb_upgrade_v1_to_v2(struct db_context *db)
443 mem_ctx = talloc_stackframe();
445 rc = db->traverse(db, regdb_normalize_keynames_fn, db);
447 talloc_free(mem_ctx);
450 return WERR_REG_IO_FAILURE;
453 werr = regdb_store_regdb_version(db, REGVER_V2);
457 static int regdb_upgrade_v2_to_v3_fn(struct db_record *rec, void *private_data)
464 uint32_t buflen, len;
468 if (rec->key.dptr == NULL || rec->key.dsize == 0) {
472 keyname = (const char *)rec->key.dptr;
474 if (strncmp(keyname, REG_SORTED_SUBKEYS_PREFIX,
475 strlen(REG_SORTED_SUBKEYS_PREFIX)) == 0)
477 /* Delete the deprecated sorted subkeys cache. */
479 DEBUG(10, ("regdb_upgrade_v2_to_v3: deleting [%s]\n", keyname));
481 status = rec->delete_rec(rec);
482 if (!NT_STATUS_IS_OK(status)) {
483 DEBUG(0, ("regdb_upgrade_v2_to_v3: tdb_delete for [%s] "
484 "failed!\n", keyname));
491 if (strncmp(keyname, REG_VALUE_PREFIX, strlen(REG_VALUE_PREFIX)) == 0) {
492 DEBUG(10, ("regdb_upgrade_v2_to_v3: skipping [%s]\n", keyname));
496 if (strncmp(keyname, REG_SECDESC_PREFIX,
497 strlen(REG_SECDESC_PREFIX)) == 0)
499 DEBUG(10, ("regdb_upgrade_v2_to_v3: skipping [%s]\n", keyname));
504 * Found a regular subkey list record.
505 * Walk the list and create the list record for those
506 * subkeys that don't already have one.
508 DEBUG(10, ("regdb_upgrade_v2_to_v3: scanning subkey list of [%s]\n",
511 buf = rec->value.dptr;
512 buflen = rec->value.dsize;
514 len = tdb_unpack(buf, buflen, "d", &num_items);
515 if (len == (uint32_t)-1) {
516 /* invalid or empty - skip */
520 for (i=0; i<num_items; i++) {
521 len += tdb_unpack(buf+len, buflen-len, "f", subkeyname);
522 DEBUG(10, ("regdb_upgrade_v2_to_v3: "
523 "writing subkey list for [%s\\%s]\n",
524 keyname, subkeyname));
525 werr = regdb_store_subkey_list(regdb, keyname, subkeyname);
526 if (!W_ERROR_IS_OK(werr)) {
534 static WERROR regdb_upgrade_v2_to_v3(struct db_context *db)
538 TALLOC_CTX *frame = talloc_stackframe();
540 rc = regdb->traverse(db, regdb_upgrade_v2_to_v3_fn, frame);
542 werr = WERR_REG_IO_FAILURE;
546 werr = regdb_store_regdb_version(db, REGVER_V3);
553 /***********************************************************************
554 Open the registry database
555 ***********************************************************************/
557 WERROR regdb_init(void)
559 const char *vstring = "INFO/version";
560 uint32 vers_id, expected_version;
564 DEBUG(10, ("regdb_init: incrementing refcount (%d->%d)\n",
565 regdb_refcount, regdb_refcount+1));
570 regdb = db_open(NULL, state_path("registry.tdb"), 0,
571 REG_TDB_FLAGS, O_RDWR, 0600);
573 regdb = db_open(NULL, state_path("registry.tdb"), 0,
574 REG_TDB_FLAGS, O_RDWR|O_CREAT, 0600);
576 werr = ntstatus_to_werror(map_nt_error_from_unix(errno));
577 DEBUG(1,("regdb_init: Failed to open registry %s (%s)\n",
578 state_path("registry.tdb"), strerror(errno) ));
582 DEBUG(10,("regdb_init: Successfully created registry tdb\n"));
586 DEBUG(10, ("regdb_init: registry db openend. refcount reset (%d)\n",
589 expected_version = REGVER_V3;
591 vers_id = dbwrap_fetch_int32(regdb, vstring);
593 DEBUG(10, ("regdb_init: registry version uninitialized "
594 "(got %d), initializing to version %d\n",
595 vers_id, expected_version));
597 werr = regdb_store_regdb_version(regdb, expected_version);
601 if (vers_id > expected_version || vers_id == 0) {
602 DEBUG(1, ("regdb_init: unknown registry version %d "
603 "(code version = %d), refusing initialization\n",
604 vers_id, expected_version));
605 return WERR_CAN_NOT_COMPLETE;
608 if (regdb->transaction_start(regdb) != 0) {
609 return WERR_REG_IO_FAILURE;
612 if (vers_id == REGVER_V1) {
613 DEBUG(10, ("regdb_init: upgrading registry fromversion %d "
614 "to %d\n", REGVER_V1, REGVER_V2));
616 werr = regdb_upgrade_v1_to_v2(regdb);
617 if (!W_ERROR_IS_OK(werr)) {
618 regdb->transaction_cancel(regdb);
625 if (vers_id == REGVER_V2) {
626 DEBUG(10, ("regdb_init: upgrading registry from version %d "
627 "to %d\n", REGVER_V2, REGVER_V3));
629 werr = regdb_upgrade_v2_to_v3(regdb);
630 if (!W_ERROR_IS_OK(werr)) {
631 regdb->transaction_cancel(regdb);
638 /* future upgrade code should go here */
640 if (regdb->transaction_commit(regdb) != 0) {
641 return WERR_REG_IO_FAILURE;
647 /***********************************************************************
648 Open the registry. Must already have been initialized by regdb_init()
649 ***********************************************************************/
651 WERROR regdb_open( void )
653 WERROR result = WERR_OK;
656 DEBUG(10, ("regdb_open: incrementing refcount (%d->%d)\n",
657 regdb_refcount, regdb_refcount+1));
664 regdb = db_open(NULL, state_path("registry.tdb"), 0,
665 REG_TDB_FLAGS, O_RDWR, 0600);
667 result = ntstatus_to_werror( map_nt_error_from_unix( errno ) );
668 DEBUG(0,("regdb_open: Failed to open %s! (%s)\n",
669 state_path("registry.tdb"), strerror(errno) ));
675 DEBUG(10, ("regdb_open: registry db opened. refcount reset (%d)\n",
681 /***********************************************************************
682 ***********************************************************************/
684 int regdb_close( void )
686 if (regdb_refcount == 0) {
692 DEBUG(10, ("regdb_close: decrementing refcount (%d->%d)\n",
693 regdb_refcount+1, regdb_refcount));
695 if ( regdb_refcount > 0 )
698 SMB_ASSERT( regdb_refcount >= 0 );
704 WERROR regdb_transaction_start(void)
706 return (regdb->transaction_start(regdb) == 0) ?
707 WERR_OK : WERR_REG_IO_FAILURE;
710 WERROR regdb_transaction_commit(void)
712 return (regdb->transaction_commit(regdb) == 0) ?
713 WERR_OK : WERR_REG_IO_FAILURE;
716 WERROR regdb_transaction_cancel(void)
718 return (regdb->transaction_cancel(regdb) == 0) ?
719 WERR_OK : WERR_REG_IO_FAILURE;
722 /***********************************************************************
723 return the tdb sequence number of the registry tdb.
724 this is an indicator for the content of the registry
725 having changed. it will change upon regdb_init, too, though.
726 ***********************************************************************/
727 int regdb_get_seqnum(void)
729 return regdb->get_seqnum(regdb);
733 static WERROR regdb_delete_key_with_prefix(struct db_context *db,
738 WERROR werr = WERR_NOMEM;
739 TALLOC_CTX *mem_ctx = talloc_stackframe();
741 if (keyname == NULL) {
742 werr = WERR_INVALID_PARAM;
746 if (prefix == NULL) {
747 path = discard_const_p(char, keyname);
749 path = talloc_asprintf(mem_ctx, "%s\\%s", prefix, keyname);
755 path = normalize_reg_path(mem_ctx, path);
760 werr = ntstatus_to_werror(dbwrap_delete_bystring(db, path));
762 /* treat "not found" as ok */
763 if (W_ERROR_EQUAL(werr, WERR_NOT_FOUND)) {
768 talloc_free(mem_ctx);
773 static WERROR regdb_delete_values(struct db_context *db, const char *keyname)
775 return regdb_delete_key_with_prefix(db, keyname, REG_VALUE_PREFIX);
778 static WERROR regdb_delete_secdesc(struct db_context *db, const char *keyname)
780 return regdb_delete_key_with_prefix(db, keyname, REG_SECDESC_PREFIX);
783 static WERROR regdb_delete_subkeylist(struct db_context *db, const char *keyname)
785 return regdb_delete_key_with_prefix(db, keyname, NULL);
789 static WERROR regdb_delete_key_lists(struct db_context *db, const char *keyname)
793 werr = regdb_delete_values(db, keyname);
794 if (!W_ERROR_IS_OK(werr)) {
795 DEBUG(1, (__location__ " Deleting %s\\%s failed: %s\n",
796 REG_VALUE_PREFIX, keyname, win_errstr(werr)));
800 werr = regdb_delete_secdesc(db, keyname);
801 if (!W_ERROR_IS_OK(werr)) {
802 DEBUG(1, (__location__ " Deleting %s\\%s failed: %s\n",
803 REG_SECDESC_PREFIX, keyname, win_errstr(werr)));
807 werr = regdb_delete_subkeylist(db, keyname);
808 if (!W_ERROR_IS_OK(werr)) {
809 DEBUG(1, (__location__ " Deleting %s failed: %s\n",
810 keyname, win_errstr(werr)));
818 /***********************************************************************
819 Add subkey strings to the registry tdb under a defined key
820 fmt is the same format as tdb_pack except this function only supports
822 ***********************************************************************/
824 static WERROR regdb_store_keys_internal2(struct db_context *db,
826 struct regsubkey_ctr *ctr)
829 uint8 *buffer = NULL;
832 uint32 num_subkeys = regsubkey_ctr_numkeys(ctr);
833 char *keyname = NULL;
834 TALLOC_CTX *ctx = talloc_stackframe();
838 werr = WERR_INVALID_PARAM;
842 keyname = talloc_strdup(ctx, key);
848 keyname = normalize_reg_path(ctx, keyname);
854 /* allocate some initial memory */
856 buffer = (uint8 *)SMB_MALLOC(1024);
857 if (buffer == NULL) {
864 /* store the number of subkeys */
866 len += tdb_pack(buffer+len, buflen-len, "d", num_subkeys);
868 /* pack all the strings */
870 for (i=0; i<num_subkeys; i++) {
873 thistime = tdb_pack(buffer+len, buflen-len, "f",
874 regsubkey_ctr_specific_key(ctr, i));
875 if (len+thistime > buflen) {
878 * tdb_pack hasn't done anything because of the short
879 * buffer, allocate extra space.
881 buffer = SMB_REALLOC_ARRAY(buffer, uint8_t,
884 DEBUG(0, ("regdb_store_keys: Failed to realloc "
885 "memory of size [%u]\n",
886 (unsigned int)(len+thistime)*2));
890 buflen = (len+thistime)*2;
891 thistime2 = tdb_pack(
892 buffer+len, buflen-len, "f",
893 regsubkey_ctr_specific_key(ctr, i));
894 if (thistime2 != thistime) {
895 DEBUG(0, ("tdb_pack failed\n"));
896 werr = WERR_CAN_NOT_COMPLETE;
903 /* finally write out the data */
907 werr = ntstatus_to_werror(dbwrap_store_bystring(db, keyname, dbuf,
917 * Utility function to store a new empty list of
918 * subkeys of given key specified as parent and subkey name
919 * (thereby creating the key).
920 * If the parent keyname is NULL, then the "subkey" is
921 * interpreted as a base key.
922 * If the subkey list does already exist, it is not modified.
924 * Must be called from within a transaction.
926 static WERROR regdb_store_subkey_list(struct db_context *db, const char *parent,
931 struct regsubkey_ctr *subkeys = NULL;
932 TALLOC_CTX *frame = talloc_stackframe();
934 if (parent == NULL) {
935 path = talloc_strdup(frame, key);
937 path = talloc_asprintf(frame, "%s\\%s", parent, key);
944 werr = regsubkey_ctr_init(frame, &subkeys);
945 W_ERROR_NOT_OK_GOTO_DONE(werr);
947 werr = regdb_fetch_keys_internal(db, path, subkeys);
948 if (W_ERROR_IS_OK(werr)) {
949 /* subkey list exists already - don't modify */
953 werr = regsubkey_ctr_reinit(subkeys);
954 W_ERROR_NOT_OK_GOTO_DONE(werr);
956 /* create a record with 0 subkeys */
957 werr = regdb_store_keys_internal2(db, path, subkeys);
958 if (!W_ERROR_IS_OK(werr)) {
959 DEBUG(0, ("regdb_store_keys: Failed to store new record for "
960 "key [%s]: %s\n", path, win_errstr(werr)));
969 /***********************************************************************
970 Store the new subkey record and create any child key records that
971 do not currently exist
972 ***********************************************************************/
974 struct regdb_store_keys_context {
976 struct regsubkey_ctr *ctr;
979 static NTSTATUS regdb_store_keys_action(struct db_context *db,
982 struct regdb_store_keys_context *store_ctx;
986 struct regsubkey_ctr *old_subkeys = NULL;
987 char *oldkeyname = NULL;
988 TALLOC_CTX *mem_ctx = talloc_stackframe();
990 store_ctx = (struct regdb_store_keys_context *)private_data;
993 * Re-fetch the old keys inside the transaction
996 werr = regsubkey_ctr_init(mem_ctx, &old_subkeys);
997 W_ERROR_NOT_OK_GOTO_DONE(werr);
999 werr = regdb_fetch_keys_internal(db, store_ctx->key, old_subkeys);
1000 if (!W_ERROR_IS_OK(werr) &&
1001 !W_ERROR_EQUAL(werr, WERR_NOT_FOUND))
1007 * Make the store operation as safe as possible without transactions:
1009 * (1) For each subkey removed from ctr compared with old_subkeys:
1011 * (a) First delete the value db entry.
1013 * (b) Next delete the secdesc db record.
1015 * (c) Then delete the subkey list entry.
1017 * (2) Now write the list of subkeys of the parent key,
1018 * deleting removed entries and adding new ones.
1020 * (3) Finally create the subkey list entries for the added keys.
1022 * This way if we crash half-way in between deleting the subkeys
1023 * and storing the parent's list of subkeys, no old data can pop up
1024 * out of the blue when re-adding keys later on.
1027 /* (1) delete removed keys' lists (values/secdesc/subkeys) */
1029 num_subkeys = regsubkey_ctr_numkeys(old_subkeys);
1030 for (i=0; i<num_subkeys; i++) {
1031 oldkeyname = regsubkey_ctr_specific_key(old_subkeys, i);
1033 if (regsubkey_ctr_key_exists(store_ctx->ctr, oldkeyname)) {
1035 * It's still around, don't delete
1040 path = talloc_asprintf(mem_ctx, "%s\\%s", store_ctx->key,
1047 werr = regdb_delete_key_lists(db, path);
1048 W_ERROR_NOT_OK_GOTO_DONE(werr);
1053 TALLOC_FREE(old_subkeys);
1055 /* (2) store the subkey list for the parent */
1057 werr = regdb_store_keys_internal2(db, store_ctx->key, store_ctx->ctr);
1058 if (!W_ERROR_IS_OK(werr)) {
1059 DEBUG(0,("regdb_store_keys: Failed to store new subkey list "
1060 "for parent [%s]: %s\n", store_ctx->key,
1065 /* (3) now create records for any subkeys that don't already exist */
1067 num_subkeys = regsubkey_ctr_numkeys(store_ctx->ctr);
1069 for (i=0; i<num_subkeys; i++) {
1072 subkey = regsubkey_ctr_specific_key(store_ctx->ctr, i);
1074 werr = regdb_store_subkey_list(db, store_ctx->key, subkey);
1075 W_ERROR_NOT_OK_GOTO_DONE(werr);
1081 talloc_free(mem_ctx);
1082 return werror_to_ntstatus(werr);
1085 static bool regdb_store_keys_internal(struct db_context *db, const char *key,
1086 struct regsubkey_ctr *ctr)
1088 int num_subkeys, old_num_subkeys, i;
1089 struct regsubkey_ctr *old_subkeys = NULL;
1090 TALLOC_CTX *ctx = talloc_stackframe();
1093 struct regdb_store_keys_context store_ctx;
1095 if (!regdb_key_exists(db, key)) {
1100 * fetch a list of the old subkeys so we can determine if anything has
1104 werr = regsubkey_ctr_init(ctx, &old_subkeys);
1105 if (!W_ERROR_IS_OK(werr)) {
1106 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
1110 werr = regdb_fetch_keys_internal(db, key, old_subkeys);
1111 if (!W_ERROR_IS_OK(werr) &&
1112 !W_ERROR_EQUAL(werr, WERR_NOT_FOUND))
1117 num_subkeys = regsubkey_ctr_numkeys(ctr);
1118 old_num_subkeys = regsubkey_ctr_numkeys(old_subkeys);
1119 if ((num_subkeys && old_num_subkeys) &&
1120 (num_subkeys == old_num_subkeys)) {
1122 for (i = 0; i < num_subkeys; i++) {
1123 if (strcmp(regsubkey_ctr_specific_key(ctr, i),
1124 regsubkey_ctr_specific_key(old_subkeys, i))
1130 if (i == num_subkeys) {
1132 * Nothing changed, no point to even start a tdb
1141 TALLOC_FREE(old_subkeys);
1143 store_ctx.key = key;
1144 store_ctx.ctr = ctr;
1146 werr = ntstatus_to_werror(dbwrap_trans_do(db,
1147 regdb_store_keys_action,
1150 ret = W_ERROR_IS_OK(werr);
1158 bool regdb_store_keys(const char *key, struct regsubkey_ctr *ctr)
1160 return regdb_store_keys_internal(regdb, key, ctr);
1164 * create a subkey of a given key
1167 struct regdb_create_subkey_context {
1172 static NTSTATUS regdb_create_subkey_action(struct db_context *db,
1176 struct regdb_create_subkey_context *create_ctx;
1177 struct regsubkey_ctr *subkeys;
1178 TALLOC_CTX *mem_ctx = talloc_stackframe();
1180 create_ctx = (struct regdb_create_subkey_context *)private_data;
1182 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
1183 W_ERROR_NOT_OK_GOTO_DONE(werr);
1185 werr = regdb_fetch_keys_internal(db, create_ctx->key, subkeys);
1186 W_ERROR_NOT_OK_GOTO_DONE(werr);
1188 werr = regsubkey_ctr_addkey(subkeys, create_ctx->subkey);
1189 W_ERROR_NOT_OK_GOTO_DONE(werr);
1191 werr = regdb_store_keys_internal2(db, create_ctx->key, subkeys);
1192 if (!W_ERROR_IS_OK(werr)) {
1193 DEBUG(0, (__location__ " failed to store new subkey list for "
1194 "parent key %s: %s\n", create_ctx->key,
1198 werr = regdb_store_subkey_list(db, create_ctx->key, create_ctx->subkey);
1201 talloc_free(mem_ctx);
1202 return werror_to_ntstatus(werr);
1205 static WERROR regdb_create_subkey_internal(struct db_context *db,
1210 struct regsubkey_ctr *subkeys;
1211 TALLOC_CTX *mem_ctx = talloc_stackframe();
1212 struct regdb_create_subkey_context create_ctx;
1214 if (!regdb_key_exists(db, key)) {
1215 werr = WERR_NOT_FOUND;
1219 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
1220 W_ERROR_NOT_OK_GOTO_DONE(werr);
1222 werr = regdb_fetch_keys_internal(db, key, subkeys);
1223 W_ERROR_NOT_OK_GOTO_DONE(werr);
1225 if (regsubkey_ctr_key_exists(subkeys, subkey)) {
1230 talloc_free(subkeys);
1232 create_ctx.key = key;
1233 create_ctx.subkey = subkey;
1235 werr = ntstatus_to_werror(dbwrap_trans_do(db,
1236 regdb_create_subkey_action,
1240 talloc_free(mem_ctx);
1244 static WERROR regdb_create_subkey(const char *key, const char *subkey)
1246 return regdb_create_subkey_internal(regdb, key, subkey);
1253 struct regdb_create_basekey_context {
1257 static NTSTATUS regdb_create_basekey_action(struct db_context *db,
1261 struct regdb_create_basekey_context *create_ctx;
1263 create_ctx = (struct regdb_create_basekey_context *)private_data;
1265 werr = regdb_store_subkey_list(db, NULL, create_ctx->key);
1267 return werror_to_ntstatus(werr);
1270 static WERROR regdb_create_basekey(struct db_context *db, const char *key)
1273 struct regdb_create_subkey_context create_ctx;
1275 create_ctx.key = key;
1277 werr = ntstatus_to_werror(dbwrap_trans_do(db,
1278 regdb_create_basekey_action,
1285 * create a subkey of a given key
1288 struct regdb_delete_subkey_context {
1295 static NTSTATUS regdb_delete_subkey_action(struct db_context *db,
1299 struct regdb_delete_subkey_context *delete_ctx;
1300 struct regsubkey_ctr *subkeys;
1301 TALLOC_CTX *mem_ctx = talloc_stackframe();
1303 delete_ctx = (struct regdb_delete_subkey_context *)private_data;
1305 werr = regdb_delete_key_lists(db, delete_ctx->path);
1306 W_ERROR_NOT_OK_GOTO_DONE(werr);
1308 if (delete_ctx->lazy) {
1312 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
1313 W_ERROR_NOT_OK_GOTO_DONE(werr);
1315 werr = regdb_fetch_keys_internal(db, delete_ctx->key, subkeys);
1316 W_ERROR_NOT_OK_GOTO_DONE(werr);
1318 werr = regsubkey_ctr_delkey(subkeys, delete_ctx->subkey);
1319 W_ERROR_NOT_OK_GOTO_DONE(werr);
1321 werr = regdb_store_keys_internal2(db, delete_ctx->key, subkeys);
1322 if (!W_ERROR_IS_OK(werr)) {
1323 DEBUG(0, (__location__ " failed to store new subkey_list for "
1324 "parent key %s: %s\n", delete_ctx->key,
1329 talloc_free(mem_ctx);
1330 return werror_to_ntstatus(werr);
1333 static WERROR regdb_delete_subkey(const char *key, const char *subkey, bool lazy)
1337 struct regdb_delete_subkey_context delete_ctx;
1338 TALLOC_CTX *mem_ctx = talloc_stackframe();
1340 if (!regdb_key_exists(regdb, key)) {
1341 werr = WERR_NOT_FOUND;
1345 path = talloc_asprintf(mem_ctx, "%s\\%s", key, subkey);
1351 if (!regdb_key_exists(regdb, path)) {
1356 delete_ctx.key = key;
1357 delete_ctx.subkey = subkey;
1358 delete_ctx.path = path;
1359 delete_ctx.lazy = lazy;
1361 werr = ntstatus_to_werror(dbwrap_trans_do(regdb,
1362 regdb_delete_subkey_action,
1366 talloc_free(mem_ctx);
1370 static TDB_DATA regdb_fetch_key_internal(struct db_context *db,
1371 TALLOC_CTX *mem_ctx, const char *key)
1376 path = normalize_reg_path(mem_ctx, key);
1378 return make_tdb_data(NULL, 0);
1381 data = dbwrap_fetch_bystring(db, mem_ctx, path);
1389 * Check for the existence of a key.
1391 * Existence of a key is authoritatively defined by
1392 * the existence of the record that contains the list
1395 * Return false, if the record does not match the correct
1396 * structure of an initial 4-byte counter and then a
1397 * list of the corresponding number of zero-terminated
1400 static bool regdb_key_exists(struct db_context *db, const char *key)
1402 TALLOC_CTX *mem_ctx = talloc_stackframe();
1408 uint32_t num_items, i;
1415 path = normalize_reg_path(mem_ctx, key);
1417 DEBUG(0, ("out of memory! (talloc failed)\n"));
1421 if (*path == '\0') {
1425 value = regdb_fetch_key_internal(db, mem_ctx, path);
1426 if (value.dptr == NULL) {
1430 if (value.dsize == 0) {
1431 DEBUG(10, ("regdb_key_exists: subkeylist-record for key "
1432 "[%s] is empty: Could be a deleted record in a "
1433 "clustered (ctdb) environment?\n",
1438 len = tdb_unpack(value.dptr, value.dsize, "d", &num_items);
1439 if (len == (int32_t)-1) {
1440 DEBUG(1, ("regdb_key_exists: ERROR: subkeylist-record for key "
1441 "[%s] is invalid: Could not parse initial 4-byte "
1442 "counter. record data length is %u.\n",
1443 path, (unsigned int)value.dsize));
1448 * Note: the tdb_unpack check above implies that len <= value.dsize
1450 buflen = value.dsize - len;
1451 buf = (const char *)value.dptr + len;
1455 for (i = 0; i < num_items; i++) {
1459 len = strnlen(buf, buflen) + 1;
1461 DEBUG(1, ("regdb_key_exists: ERROR: subkeylist-record "
1462 "for key [%s] is corrupt: %u items expected, "
1463 "item number %u is not zero terminated.\n",
1464 path, num_items, i+1));
1473 DEBUG(1, ("regdb_key_exists: ERROR: subkeylist-record for key "
1474 "[%s] is corrupt: %u items expected and found, but "
1475 "the record contains additional %u bytes\n",
1476 path, num_items, buflen));
1480 if (i < num_items) {
1481 DEBUG(1, ("regdb_key_exists: ERROR: subkeylist-record for key "
1482 "[%s] is corrupt: %u items expected, but only %u "
1484 path, num_items, i+1));
1491 TALLOC_FREE(mem_ctx);
1496 /***********************************************************************
1497 Retrieve an array of strings containing subkeys. Memory should be
1498 released by the caller.
1499 ***********************************************************************/
1501 static WERROR regdb_fetch_keys_internal(struct db_context *db, const char *key,
1502 struct regsubkey_ctr *ctr)
1510 TALLOC_CTX *frame = talloc_stackframe();
1513 DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key ? key : "NULL"));
1515 if (!regdb_key_exists(db, key)) {
1516 DEBUG(10, ("key [%s] not found\n", key));
1517 werr = WERR_NOT_FOUND;
1521 werr = regsubkey_ctr_reinit(ctr);
1522 W_ERROR_NOT_OK_GOTO_DONE(werr);
1524 werr = regsubkey_ctr_set_seqnum(ctr, db->get_seqnum(db));
1525 W_ERROR_NOT_OK_GOTO_DONE(werr);
1527 value = regdb_fetch_key_internal(db, frame, key);
1529 if (value.dsize == 0 || value.dptr == NULL) {
1530 DEBUG(10, ("regdb_fetch_keys: no subkeys found for key [%s]\n",
1536 buflen = value.dsize;
1537 len = tdb_unpack( buf, buflen, "d", &num_items);
1538 if (len == (uint32_t)-1) {
1539 werr = WERR_NOT_FOUND;
1543 for (i=0; i<num_items; i++) {
1544 len += tdb_unpack(buf+len, buflen-len, "f", subkeyname);
1545 werr = regsubkey_ctr_addkey(ctr, subkeyname);
1546 if (!W_ERROR_IS_OK(werr)) {
1547 DEBUG(5, ("regdb_fetch_keys: regsubkey_ctr_addkey "
1548 "failed: %s\n", win_errstr(werr)));
1554 DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items));
1561 int regdb_fetch_keys(const char *key, struct regsubkey_ctr *ctr)
1565 werr = regdb_fetch_keys_internal(regdb, key, ctr);
1566 if (!W_ERROR_IS_OK(werr)) {
1570 return regsubkey_ctr_numkeys(ctr);
1573 /****************************************************************************
1574 Unpack a list of registry values frem the TDB
1575 ***************************************************************************/
1577 static int regdb_unpack_values(struct regval_ctr *values, uint8 *buf, int buflen)
1584 uint32 num_values = 0;
1587 /* loop and unpack the rest of the registry values */
1589 len += tdb_unpack(buf+len, buflen-len, "d", &num_values);
1591 for ( i=0; i<num_values; i++ ) {
1592 /* unpack the next regval */
1597 valuename[0] = '\0';
1598 len += tdb_unpack(buf+len, buflen-len, "fdB",
1604 regval_ctr_addvalue(values, valuename, type,
1605 (uint8_t *)data_p, size);
1606 SAFE_FREE(data_p); /* 'B' option to tdb_unpack does a malloc() */
1608 DEBUG(8,("specific: [%s], len: %d\n", valuename, size));
1614 /****************************************************************************
1615 Pack all values in all printer keys
1616 ***************************************************************************/
1618 static int regdb_pack_values(struct regval_ctr *values, uint8 *buf, int buflen)
1622 struct regval_blob *val;
1628 num_values = regval_ctr_numvals( values );
1630 /* pack the number of values first */
1632 len += tdb_pack( buf+len, buflen-len, "d", num_values );
1634 /* loop over all values */
1636 for ( i=0; i<num_values; i++ ) {
1637 val = regval_ctr_specific_value( values, i );
1638 len += tdb_pack(buf+len, buflen-len, "fdB",
1642 regval_data_p(val) );
1648 /***********************************************************************
1649 Retrieve an array of strings containing subkeys. Memory should be
1650 released by the caller.
1651 ***********************************************************************/
1653 static int regdb_fetch_values_internal(struct db_context *db, const char* key,
1654 struct regval_ctr *values)
1656 char *keystr = NULL;
1657 TALLOC_CTX *ctx = talloc_stackframe();
1662 DEBUG(10,("regdb_fetch_values: Looking for value of key [%s] \n", key));
1664 if (!regdb_key_exists(db, key)) {
1668 keystr = talloc_asprintf(ctx, "%s\\%s", REG_VALUE_PREFIX, key);
1673 werr = regval_ctr_set_seqnum(values, db->get_seqnum(db));
1674 W_ERROR_NOT_OK_GOTO_DONE(werr);
1676 value = regdb_fetch_key_internal(db, ctx, keystr);
1679 /* all keys have zero values by default */
1683 regdb_unpack_values(values, value.dptr, value.dsize);
1684 ret = regval_ctr_numvals(values);
1691 int regdb_fetch_values(const char* key, struct regval_ctr *values)
1693 return regdb_fetch_values_internal(regdb, key, values);
1696 static bool regdb_store_values_internal(struct db_context *db, const char *key,
1697 struct regval_ctr *values)
1699 TDB_DATA old_data, data;
1700 char *keystr = NULL;
1701 TALLOC_CTX *ctx = talloc_stackframe();
1704 bool result = false;
1706 DEBUG(10,("regdb_store_values: Looking for value of key [%s] \n", key));
1708 if (!regdb_key_exists(db, key)) {
1714 len = regdb_pack_values(values, data.dptr, data.dsize);
1716 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
1720 data.dptr = talloc_array(ctx, uint8, len);
1723 len = regdb_pack_values(values, data.dptr, data.dsize);
1725 SMB_ASSERT( len == data.dsize );
1727 keystr = talloc_asprintf(ctx, "%s\\%s", REG_VALUE_PREFIX, key );
1731 keystr = normalize_reg_path(ctx, keystr);
1736 old_data = dbwrap_fetch_bystring(db, ctx, keystr);
1738 if ((old_data.dptr != NULL)
1739 && (old_data.dsize == data.dsize)
1740 && (memcmp(old_data.dptr, data.dptr, data.dsize) == 0))
1746 status = dbwrap_trans_store_bystring(db, keystr, data, TDB_REPLACE);
1748 result = NT_STATUS_IS_OK(status);
1755 bool regdb_store_values(const char *key, struct regval_ctr *values)
1757 return regdb_store_values_internal(regdb, key, values);
1760 static WERROR regdb_get_secdesc(TALLOC_CTX *mem_ctx, const char *key,
1761 struct security_descriptor **psecdesc)
1766 TALLOC_CTX *tmp_ctx = talloc_stackframe();
1767 WERROR err = WERR_OK;
1769 DEBUG(10, ("regdb_get_secdesc: Getting secdesc of key [%s]\n", key));
1771 if (!regdb_key_exists(regdb, key)) {
1776 tdbkey = talloc_asprintf(tmp_ctx, "%s\\%s", REG_SECDESC_PREFIX, key);
1777 if (tdbkey == NULL) {
1782 tdbkey = normalize_reg_path(tmp_ctx, tdbkey);
1783 if (tdbkey == NULL) {
1788 data = dbwrap_fetch_bystring(regdb, tmp_ctx, tdbkey);
1789 if (data.dptr == NULL) {
1794 status = unmarshall_sec_desc(mem_ctx, (uint8 *)data.dptr, data.dsize,
1797 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MEMORY)) {
1799 } else if (!NT_STATUS_IS_OK(status)) {
1800 err = WERR_REG_CORRUPT;
1804 TALLOC_FREE(tmp_ctx);
1808 static WERROR regdb_set_secdesc(const char *key,
1809 struct security_descriptor *secdesc)
1811 TALLOC_CTX *mem_ctx = talloc_stackframe();
1813 WERROR err = WERR_NOMEM;
1816 if (!regdb_key_exists(regdb, key)) {
1821 tdbkey = talloc_asprintf(mem_ctx, "%s\\%s", REG_SECDESC_PREFIX, key);
1822 if (tdbkey == NULL) {
1826 tdbkey = normalize_reg_path(mem_ctx, tdbkey);
1827 if (tdbkey == NULL) {
1832 if (secdesc == NULL) {
1833 /* assuming a delete */
1834 err = ntstatus_to_werror(dbwrap_trans_delete_bystring(regdb,
1839 err = ntstatus_to_werror(marshall_sec_desc(mem_ctx, secdesc,
1842 W_ERROR_NOT_OK_GOTO_DONE(err);
1844 err = ntstatus_to_werror(dbwrap_trans_store_bystring(regdb, tdbkey,
1848 TALLOC_FREE(mem_ctx);
1852 bool regdb_subkeys_need_update(struct regsubkey_ctr *subkeys)
1854 return (regdb_get_seqnum() != regsubkey_ctr_get_seqnum(subkeys));
1857 bool regdb_values_need_update(struct regval_ctr *values)
1859 return (regdb_get_seqnum() != regval_ctr_get_seqnum(values));
1863 * Table of function pointers for default access
1866 struct registry_ops regdb_ops = {
1867 .fetch_subkeys = regdb_fetch_keys,
1868 .fetch_values = regdb_fetch_values,
1869 .store_subkeys = regdb_store_keys,
1870 .store_values = regdb_store_values,
1871 .create_subkey = regdb_create_subkey,
1872 .delete_subkey = regdb_delete_subkey,
1873 .get_secdesc = regdb_get_secdesc,
1874 .set_secdesc = regdb_set_secdesc,
1875 .subkeys_need_update = regdb_subkeys_need_update,
1876 .values_need_update = regdb_values_need_update