2 * Unix SMB/CIFS implementation.
3 * Virtual Windows Registry Layer
4 * Copyright (C) Gerald Carter 2002-2005
5 * Copyright (C) Michael Adam 2007-2009
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 /* Implementation of internal registry database functions. */
24 #include "system/filesys.h"
27 #include "reg_util_internal.h"
28 #include "reg_backend_db.h"
29 #include "reg_objects.h"
30 #include "nt_printing.h"
33 #include "../libcli/security/secdesc.h"
36 #define DBGC_CLASS DBGC_REGISTRY
38 static struct db_context *regdb = NULL;
39 static int regdb_refcount;
41 static bool regdb_key_exists(struct db_context *db, const char *key);
42 static WERROR regdb_fetch_keys_internal(struct db_context *db, const char *key,
43 struct regsubkey_ctr *ctr);
44 static bool regdb_store_keys_internal(struct db_context *db, const char *key,
45 struct regsubkey_ctr *ctr);
46 static int regdb_fetch_values_internal(struct db_context *db, const char* key,
47 struct regval_ctr *values);
48 static bool regdb_store_values_internal(struct db_context *db, const char *key,
49 struct regval_ctr *values);
50 static WERROR regdb_store_subkey_list(struct db_context *db, const char *parent,
53 static WERROR regdb_create_basekey(struct db_context *db, const char *key);
54 static WERROR regdb_create_subkey_internal(struct db_context *db,
58 /* List the deepest path into the registry. All part components will be created.*/
60 /* If you want to have a part of the path controlled by the tdb and part by
61 a virtual registry db (e.g. printing), then you have to list the deepest path.
62 For example,"HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion/Print"
63 allows the reg_db backend to handle everything up to
64 "HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion" and then we'll hook
65 the reg_printing backend onto the last component of the path (see
66 KEY_PRINTING_2K in include/rpc_reg.h) --jerry */
68 static const char *builtin_registry_paths[] = {
72 KEY_PRINTING "\\Forms",
73 KEY_PRINTING "\\Printers",
74 KEY_PRINTING "\\Environments\\Windows NT x86\\Print Processors\\winprint",
81 KEY_SAMBA_GROUP_POLICY,
82 KEY_GP_MACHINE_POLICY,
83 KEY_GP_MACHINE_WIN_POLICY,
86 KEY_GP_USER_WIN_POLICY,
87 "HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon\\GPExtensions",
88 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Print\\Monitors",
90 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\DefaultUserConfiguration",
99 struct builtin_regkey_value {
101 const char *valuename;
109 static struct builtin_regkey_value builtin_registry_values[] = {
110 { KEY_PRINTING_PORTS,
111 SAMBA_PRINTER_PORT_NAME, REG_SZ, { "" } },
113 "DefaultSpoolDirectory", REG_SZ, { "C:\\Windows\\System32\\Spool\\Printers" } },
115 "DisplayName", REG_SZ, { "Event Log" } },
117 "ErrorControl", REG_DWORD, { (char*)0x00000001 } },
118 { NULL, NULL, 0, { NULL } }
121 static WERROR create_key_recursive(struct db_context *db,
128 if (subkey == NULL) {
129 return WERR_INVALID_PARAM;
133 return regdb_create_basekey(db, subkey);
136 p = strrchr_m(path, '\\');
139 werr = create_key_recursive(db, NULL, path);
142 werr = create_key_recursive(db, path, p+1);
146 if (!W_ERROR_IS_OK(werr)) {
150 werr = regdb_create_subkey_internal(db, path, subkey);
157 * Initialize a key in the registry:
158 * create each component key of the specified path.
160 static WERROR init_registry_key_internal(struct db_context *db,
161 const char *add_path)
165 TALLOC_CTX *frame = talloc_stackframe();
167 if (add_path == NULL) {
168 werr = WERR_INVALID_PARAM;
172 key = talloc_strdup(frame, add_path);
174 subkey = strrchr_m(key, '\\');
175 if (subkey == NULL) {
183 werr = create_key_recursive(db, key, subkey);
190 struct init_registry_key_context {
191 const char *add_path;
194 static NTSTATUS init_registry_key_action(struct db_context *db,
197 struct init_registry_key_context *init_ctx =
198 (struct init_registry_key_context *)private_data;
200 return werror_to_ntstatus(init_registry_key_internal(
201 db, init_ctx->add_path));
205 * Initialize a key in the registry:
206 * create each component key of the specified path,
207 * wrapped in one db transaction.
209 WERROR init_registry_key(const char *add_path)
211 struct init_registry_key_context init_ctx;
213 if (regdb_key_exists(regdb, add_path)) {
217 init_ctx.add_path = add_path;
219 return ntstatus_to_werror(dbwrap_trans_do(regdb,
220 init_registry_key_action,
224 /***********************************************************************
225 Open the registry data in the tdb
226 ***********************************************************************/
228 static void regdb_ctr_add_value(struct regval_ctr *ctr,
229 struct builtin_regkey_value *value)
231 switch(value->type) {
233 regval_ctr_addvalue(ctr, value->valuename, REG_DWORD,
234 (uint8_t *)&value->data.dw_value,
239 regval_ctr_addvalue_sz(ctr, value->valuename,
244 DEBUG(0, ("regdb_ctr_add_value: invalid value type in "
245 "registry values [%d]\n", value->type));
249 static NTSTATUS init_registry_data_action(struct db_context *db,
253 TALLOC_CTX *frame = talloc_stackframe();
254 struct regval_ctr *values;
257 /* loop over all of the predefined paths and add each component */
259 for (i=0; builtin_registry_paths[i] != NULL; i++) {
260 if (regdb_key_exists(db, builtin_registry_paths[i])) {
263 status = werror_to_ntstatus(init_registry_key_internal(db,
264 builtin_registry_paths[i]));
265 if (!NT_STATUS_IS_OK(status)) {
270 /* loop over all of the predefined values and add each component */
272 for (i=0; builtin_registry_values[i].path != NULL; i++) {
275 werr = regval_ctr_init(frame, &values);
276 if (!W_ERROR_IS_OK(werr)) {
277 status = werror_to_ntstatus(werr);
281 regdb_fetch_values_internal(db,
282 builtin_registry_values[i].path,
285 /* preserve existing values across restarts. Only add new ones */
287 if (!regval_ctr_key_exists(values,
288 builtin_registry_values[i].valuename))
290 regdb_ctr_add_value(values,
291 &builtin_registry_values[i]);
292 regdb_store_values_internal(db,
293 builtin_registry_values[i].path,
299 status = NT_STATUS_OK;
307 WERROR init_registry_data(void)
310 TALLOC_CTX *frame = talloc_stackframe();
311 struct regval_ctr *values;
315 * First, check for the existence of the needed keys and values.
316 * If all do already exist, we can save the writes.
318 for (i=0; builtin_registry_paths[i] != NULL; i++) {
319 if (!regdb_key_exists(regdb, builtin_registry_paths[i])) {
324 for (i=0; builtin_registry_values[i].path != NULL; i++) {
325 werr = regval_ctr_init(frame, &values);
326 W_ERROR_NOT_OK_GOTO_DONE(werr);
328 regdb_fetch_values_internal(regdb,
329 builtin_registry_values[i].path,
331 if (!regval_ctr_key_exists(values,
332 builtin_registry_values[i].valuename))
347 * There are potentially quite a few store operations which are all
348 * indiviually wrapped in tdb transactions. Wrapping them in a single
349 * transaction gives just a single transaction_commit() to actually do
350 * its fsync()s. See tdb/common/transaction.c for info about nested
351 * transaction behaviour.
354 werr = ntstatus_to_werror(dbwrap_trans_do(regdb,
355 init_registry_data_action,
363 static int regdb_normalize_keynames_fn(struct db_record *rec,
366 TALLOC_CTX *mem_ctx = talloc_tos();
370 if (rec->key.dptr == NULL || rec->key.dsize == 0) {
374 keyname = strchr((const char *) rec->key.dptr, '/');
376 struct db_record new_rec;
378 keyname = talloc_string_sub(mem_ctx,
379 (const char *) rec->key.dptr,
383 DEBUG(2, ("regdb_normalize_keynames_fn: Convert %s to %s\n",
384 (const char *) rec->key.dptr,
387 new_rec.value = rec->value;
388 new_rec.key = string_term_tdb_data(keyname);
389 new_rec.private_data = rec->private_data;
391 /* Delete the original record and store the normalized key */
392 status = rec->delete_rec(rec);
393 if (!NT_STATUS_IS_OK(status)) {
394 DEBUG(0,("regdb_normalize_keynames_fn: "
395 "tdb_delete for [%s] failed!\n",
400 status = rec->store(&new_rec, new_rec.value, TDB_REPLACE);
401 if (!NT_STATUS_IS_OK(status)) {
402 DEBUG(0,("regdb_normalize_keynames_fn: "
403 "failed to store new record for [%s]!\n",
412 static WERROR regdb_store_regdb_version(struct db_context *db, uint32_t version)
415 const char *version_keyname = "INFO/version";
418 return WERR_CAN_NOT_COMPLETE;
421 status = dbwrap_trans_store_int32(db, version_keyname, version);
422 if (!NT_STATUS_IS_OK(status)) {
423 DEBUG(1, ("regdb_store_regdb_version: error storing %s = %d: %s\n",
424 version_keyname, version, nt_errstr(status)));
425 return ntstatus_to_werror(status);
427 DEBUG(10, ("regdb_store_regdb_version: stored %s = %d\n",
428 version_keyname, version));
433 static WERROR regdb_upgrade_v1_to_v2(struct db_context *db)
439 mem_ctx = talloc_stackframe();
441 rc = regdb->traverse(db, regdb_normalize_keynames_fn, mem_ctx);
443 talloc_free(mem_ctx);
446 return WERR_REG_IO_FAILURE;
449 werr = regdb_store_regdb_version(db, REGVER_V2);
453 static int regdb_upgrade_v2_to_v3_fn(struct db_record *rec, void *private_data)
460 uint32_t buflen, len;
464 if (rec->key.dptr == NULL || rec->key.dsize == 0) {
468 keyname = (const char *)rec->key.dptr;
470 if (strncmp(keyname, REG_SORTED_SUBKEYS_PREFIX,
471 strlen(REG_SORTED_SUBKEYS_PREFIX)) == 0)
473 /* Delete the deprecated sorted subkeys cache. */
475 DEBUG(10, ("regdb_upgrade_v2_to_v3: deleting [%s]\n", keyname));
477 status = rec->delete_rec(rec);
478 if (!NT_STATUS_IS_OK(status)) {
479 DEBUG(0, ("regdb_upgrade_v2_to_v3: tdb_delete for [%s] "
480 "failed!\n", keyname));
487 if (strncmp(keyname, REG_VALUE_PREFIX, strlen(REG_VALUE_PREFIX)) == 0) {
488 DEBUG(10, ("regdb_upgrade_v2_to_v3: skipping [%s]\n", keyname));
492 if (strncmp(keyname, REG_SECDESC_PREFIX,
493 strlen(REG_SECDESC_PREFIX)) == 0)
495 DEBUG(10, ("regdb_upgrade_v2_to_v3: skipping [%s]\n", keyname));
500 * Found a regular subkey list record.
501 * Walk the list and create the list record for those
502 * subkeys that don't already have one.
504 DEBUG(10, ("regdb_upgrade_v2_to_v3: scanning subkey list of [%s]\n",
507 buf = rec->value.dptr;
508 buflen = rec->value.dsize;
510 len = tdb_unpack(buf, buflen, "d", &num_items);
511 if (len == (uint32_t)-1) {
512 /* invalid or empty - skip */
516 for (i=0; i<num_items; i++) {
517 len += tdb_unpack(buf+len, buflen-len, "f", subkeyname);
518 DEBUG(10, ("regdb_upgrade_v2_to_v3: "
519 "writing subkey list for [%s\\%s]\n",
520 keyname, subkeyname));
521 werr = regdb_store_subkey_list(regdb, keyname, subkeyname);
522 if (!W_ERROR_IS_OK(werr)) {
530 static WERROR regdb_upgrade_v2_to_v3(struct db_context *db)
534 TALLOC_CTX *frame = talloc_stackframe();
536 rc = regdb->traverse(db, regdb_upgrade_v2_to_v3_fn, frame);
538 werr = WERR_REG_IO_FAILURE;
542 werr = regdb_store_regdb_version(db, REGVER_V3);
549 /***********************************************************************
550 Open the registry database
551 ***********************************************************************/
553 WERROR regdb_init(void)
555 const char *vstring = "INFO/version";
556 uint32 vers_id, expected_version;
560 DEBUG(10, ("regdb_init: incrementing refcount (%d->%d)\n",
561 regdb_refcount, regdb_refcount+1));
566 regdb = db_open(NULL, state_path("registry.tdb"), 0,
567 REG_TDB_FLAGS, O_RDWR, 0600);
569 regdb = db_open(NULL, state_path("registry.tdb"), 0,
570 REG_TDB_FLAGS, O_RDWR|O_CREAT, 0600);
572 werr = ntstatus_to_werror(map_nt_error_from_unix(errno));
573 DEBUG(1,("regdb_init: Failed to open registry %s (%s)\n",
574 state_path("registry.tdb"), strerror(errno) ));
578 DEBUG(10,("regdb_init: Successfully created registry tdb\n"));
582 DEBUG(10, ("regdb_init: registry db openend. refcount reset (%d)\n",
585 expected_version = REGVER_V3;
587 vers_id = dbwrap_fetch_int32(regdb, vstring);
589 DEBUG(10, ("regdb_init: registry version uninitialized "
590 "(got %d), initializing to version %d\n",
591 vers_id, expected_version));
593 werr = regdb_store_regdb_version(regdb, expected_version);
597 if (vers_id > expected_version || vers_id == 0) {
598 DEBUG(1, ("regdb_init: unknown registry version %d "
599 "(code version = %d), refusing initialization\n",
600 vers_id, expected_version));
601 return WERR_CAN_NOT_COMPLETE;
604 if (regdb->transaction_start(regdb) != 0) {
605 return WERR_REG_IO_FAILURE;
608 if (vers_id == REGVER_V1) {
609 DEBUG(10, ("regdb_init: upgrading registry fromversion %d "
610 "to %d\n", REGVER_V1, REGVER_V2));
612 werr = regdb_upgrade_v1_to_v2(regdb);
613 if (!W_ERROR_IS_OK(werr)) {
614 regdb->transaction_cancel(regdb);
621 if (vers_id == REGVER_V2) {
622 DEBUG(10, ("regdb_init: upgrading registry from version %d "
623 "to %d\n", REGVER_V2, REGVER_V3));
625 werr = regdb_upgrade_v2_to_v3(regdb);
626 if (!W_ERROR_IS_OK(werr)) {
627 regdb->transaction_cancel(regdb);
634 /* future upgrade code should go here */
636 if (regdb->transaction_commit(regdb) != 0) {
637 return WERR_REG_IO_FAILURE;
643 /***********************************************************************
644 Open the registry. Must already have been initialized by regdb_init()
645 ***********************************************************************/
647 WERROR regdb_open( void )
649 WERROR result = WERR_OK;
652 DEBUG(10, ("regdb_open: incrementing refcount (%d->%d)\n",
653 regdb_refcount, regdb_refcount+1));
660 regdb = db_open(NULL, state_path("registry.tdb"), 0,
661 REG_TDB_FLAGS, O_RDWR, 0600);
663 result = ntstatus_to_werror( map_nt_error_from_unix( errno ) );
664 DEBUG(0,("regdb_open: Failed to open %s! (%s)\n",
665 state_path("registry.tdb"), strerror(errno) ));
671 DEBUG(10, ("regdb_open: registry db opened. refcount reset (%d)\n",
677 /***********************************************************************
678 ***********************************************************************/
680 int regdb_close( void )
682 if (regdb_refcount == 0) {
688 DEBUG(10, ("regdb_close: decrementing refcount (%d->%d)\n",
689 regdb_refcount+1, regdb_refcount));
691 if ( regdb_refcount > 0 )
694 SMB_ASSERT( regdb_refcount >= 0 );
700 WERROR regdb_transaction_start(void)
702 return (regdb->transaction_start(regdb) == 0) ?
703 WERR_OK : WERR_REG_IO_FAILURE;
706 WERROR regdb_transaction_commit(void)
708 return (regdb->transaction_commit(regdb) == 0) ?
709 WERR_OK : WERR_REG_IO_FAILURE;
712 WERROR regdb_transaction_cancel(void)
714 return (regdb->transaction_cancel(regdb) == 0) ?
715 WERR_OK : WERR_REG_IO_FAILURE;
718 /***********************************************************************
719 return the tdb sequence number of the registry tdb.
720 this is an indicator for the content of the registry
721 having changed. it will change upon regdb_init, too, though.
722 ***********************************************************************/
723 int regdb_get_seqnum(void)
725 return regdb->get_seqnum(regdb);
729 static WERROR regdb_delete_key_with_prefix(struct db_context *db,
734 WERROR werr = WERR_NOMEM;
735 TALLOC_CTX *mem_ctx = talloc_stackframe();
737 if (keyname == NULL) {
738 werr = WERR_INVALID_PARAM;
742 if (prefix == NULL) {
743 path = discard_const_p(char, keyname);
745 path = talloc_asprintf(mem_ctx, "%s\\%s", prefix, keyname);
751 path = normalize_reg_path(mem_ctx, path);
756 werr = ntstatus_to_werror(dbwrap_delete_bystring(db, path));
758 /* treat "not found" as ok */
759 if (W_ERROR_EQUAL(werr, WERR_NOT_FOUND)) {
764 talloc_free(mem_ctx);
769 static WERROR regdb_delete_values(struct db_context *db, const char *keyname)
771 return regdb_delete_key_with_prefix(db, keyname, REG_VALUE_PREFIX);
774 static WERROR regdb_delete_secdesc(struct db_context *db, const char *keyname)
776 return regdb_delete_key_with_prefix(db, keyname, REG_SECDESC_PREFIX);
779 static WERROR regdb_delete_subkeylist(struct db_context *db, const char *keyname)
781 return regdb_delete_key_with_prefix(db, keyname, NULL);
785 static WERROR regdb_delete_key_lists(struct db_context *db, const char *keyname)
789 werr = regdb_delete_values(db, keyname);
790 if (!W_ERROR_IS_OK(werr)) {
791 DEBUG(1, (__location__ " Deleting %s\\%s failed: %s\n",
792 REG_VALUE_PREFIX, keyname, win_errstr(werr)));
796 werr = regdb_delete_secdesc(db, keyname);
797 if (!W_ERROR_IS_OK(werr)) {
798 DEBUG(1, (__location__ " Deleting %s\\%s failed: %s\n",
799 REG_SECDESC_PREFIX, keyname, win_errstr(werr)));
803 werr = regdb_delete_subkeylist(db, keyname);
804 if (!W_ERROR_IS_OK(werr)) {
805 DEBUG(1, (__location__ " Deleting %s failed: %s\n",
806 keyname, win_errstr(werr)));
814 /***********************************************************************
815 Add subkey strings to the registry tdb under a defined key
816 fmt is the same format as tdb_pack except this function only supports
818 ***********************************************************************/
820 static WERROR regdb_store_keys_internal2(struct db_context *db,
822 struct regsubkey_ctr *ctr)
825 uint8 *buffer = NULL;
828 uint32 num_subkeys = regsubkey_ctr_numkeys(ctr);
829 char *keyname = NULL;
830 TALLOC_CTX *ctx = talloc_stackframe();
834 werr = WERR_INVALID_PARAM;
838 keyname = talloc_strdup(ctx, key);
844 keyname = normalize_reg_path(ctx, keyname);
850 /* allocate some initial memory */
852 buffer = (uint8 *)SMB_MALLOC(1024);
853 if (buffer == NULL) {
860 /* store the number of subkeys */
862 len += tdb_pack(buffer+len, buflen-len, "d", num_subkeys);
864 /* pack all the strings */
866 for (i=0; i<num_subkeys; i++) {
869 thistime = tdb_pack(buffer+len, buflen-len, "f",
870 regsubkey_ctr_specific_key(ctr, i));
871 if (len+thistime > buflen) {
874 * tdb_pack hasn't done anything because of the short
875 * buffer, allocate extra space.
877 buffer = SMB_REALLOC_ARRAY(buffer, uint8_t,
880 DEBUG(0, ("regdb_store_keys: Failed to realloc "
881 "memory of size [%u]\n",
882 (unsigned int)(len+thistime)*2));
886 buflen = (len+thistime)*2;
887 thistime2 = tdb_pack(
888 buffer+len, buflen-len, "f",
889 regsubkey_ctr_specific_key(ctr, i));
890 if (thistime2 != thistime) {
891 DEBUG(0, ("tdb_pack failed\n"));
892 werr = WERR_CAN_NOT_COMPLETE;
899 /* finally write out the data */
903 werr = ntstatus_to_werror(dbwrap_store_bystring(db, keyname, dbuf,
913 * Utility function to store a new empty list of
914 * subkeys of given key specified as parent and subkey name
915 * (thereby creating the key).
916 * If the parent keyname is NULL, then the "subkey" is
917 * interpreted as a base key.
918 * If the subkey list does already exist, it is not modified.
920 * Must be called from within a transaction.
922 static WERROR regdb_store_subkey_list(struct db_context *db, const char *parent,
927 struct regsubkey_ctr *subkeys = NULL;
928 TALLOC_CTX *frame = talloc_stackframe();
930 if (parent == NULL) {
931 path = talloc_strdup(frame, key);
933 path = talloc_asprintf(frame, "%s\\%s", parent, key);
940 werr = regsubkey_ctr_init(frame, &subkeys);
941 W_ERROR_NOT_OK_GOTO_DONE(werr);
943 werr = regdb_fetch_keys_internal(db, path, subkeys);
944 if (W_ERROR_IS_OK(werr)) {
945 /* subkey list exists already - don't modify */
949 werr = regsubkey_ctr_reinit(subkeys);
950 W_ERROR_NOT_OK_GOTO_DONE(werr);
952 /* create a record with 0 subkeys */
953 werr = regdb_store_keys_internal2(db, path, subkeys);
954 if (!W_ERROR_IS_OK(werr)) {
955 DEBUG(0, ("regdb_store_keys: Failed to store new record for "
956 "key [%s]: %s\n", path, win_errstr(werr)));
965 /***********************************************************************
966 Store the new subkey record and create any child key records that
967 do not currently exist
968 ***********************************************************************/
970 struct regdb_store_keys_context {
972 struct regsubkey_ctr *ctr;
975 static NTSTATUS regdb_store_keys_action(struct db_context *db,
978 struct regdb_store_keys_context *store_ctx;
982 struct regsubkey_ctr *old_subkeys = NULL;
983 char *oldkeyname = NULL;
984 TALLOC_CTX *mem_ctx = talloc_stackframe();
986 store_ctx = (struct regdb_store_keys_context *)private_data;
989 * Re-fetch the old keys inside the transaction
992 werr = regsubkey_ctr_init(mem_ctx, &old_subkeys);
993 W_ERROR_NOT_OK_GOTO_DONE(werr);
995 werr = regdb_fetch_keys_internal(db, store_ctx->key, old_subkeys);
996 if (!W_ERROR_IS_OK(werr) &&
997 !W_ERROR_EQUAL(werr, WERR_NOT_FOUND))
1003 * Make the store operation as safe as possible without transactions:
1005 * (1) For each subkey removed from ctr compared with old_subkeys:
1007 * (a) First delete the value db entry.
1009 * (b) Next delete the secdesc db record.
1011 * (c) Then delete the subkey list entry.
1013 * (2) Now write the list of subkeys of the parent key,
1014 * deleting removed entries and adding new ones.
1016 * (3) Finally create the subkey list entries for the added keys.
1018 * This way if we crash half-way in between deleting the subkeys
1019 * and storing the parent's list of subkeys, no old data can pop up
1020 * out of the blue when re-adding keys later on.
1023 /* (1) delete removed keys' lists (values/secdesc/subkeys) */
1025 num_subkeys = regsubkey_ctr_numkeys(old_subkeys);
1026 for (i=0; i<num_subkeys; i++) {
1027 oldkeyname = regsubkey_ctr_specific_key(old_subkeys, i);
1029 if (regsubkey_ctr_key_exists(store_ctx->ctr, oldkeyname)) {
1031 * It's still around, don't delete
1036 path = talloc_asprintf(mem_ctx, "%s\\%s", store_ctx->key,
1043 werr = regdb_delete_key_lists(db, path);
1044 W_ERROR_NOT_OK_GOTO_DONE(werr);
1049 TALLOC_FREE(old_subkeys);
1051 /* (2) store the subkey list for the parent */
1053 werr = regdb_store_keys_internal2(db, store_ctx->key, store_ctx->ctr);
1054 if (!W_ERROR_IS_OK(werr)) {
1055 DEBUG(0,("regdb_store_keys: Failed to store new subkey list "
1056 "for parent [%s]: %s\n", store_ctx->key,
1061 /* (3) now create records for any subkeys that don't already exist */
1063 num_subkeys = regsubkey_ctr_numkeys(store_ctx->ctr);
1065 for (i=0; i<num_subkeys; i++) {
1068 subkey = regsubkey_ctr_specific_key(store_ctx->ctr, i);
1070 werr = regdb_store_subkey_list(db, store_ctx->key, subkey);
1071 W_ERROR_NOT_OK_GOTO_DONE(werr);
1077 talloc_free(mem_ctx);
1078 return werror_to_ntstatus(werr);
1081 static bool regdb_store_keys_internal(struct db_context *db, const char *key,
1082 struct regsubkey_ctr *ctr)
1084 int num_subkeys, old_num_subkeys, i;
1085 struct regsubkey_ctr *old_subkeys = NULL;
1086 TALLOC_CTX *ctx = talloc_stackframe();
1089 struct regdb_store_keys_context store_ctx;
1091 if (!regdb_key_exists(db, key)) {
1096 * fetch a list of the old subkeys so we can determine if anything has
1100 werr = regsubkey_ctr_init(ctx, &old_subkeys);
1101 if (!W_ERROR_IS_OK(werr)) {
1102 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
1106 werr = regdb_fetch_keys_internal(db, key, old_subkeys);
1107 if (!W_ERROR_IS_OK(werr) &&
1108 !W_ERROR_EQUAL(werr, WERR_NOT_FOUND))
1113 num_subkeys = regsubkey_ctr_numkeys(ctr);
1114 old_num_subkeys = regsubkey_ctr_numkeys(old_subkeys);
1115 if ((num_subkeys && old_num_subkeys) &&
1116 (num_subkeys == old_num_subkeys)) {
1118 for (i = 0; i < num_subkeys; i++) {
1119 if (strcmp(regsubkey_ctr_specific_key(ctr, i),
1120 regsubkey_ctr_specific_key(old_subkeys, i))
1126 if (i == num_subkeys) {
1128 * Nothing changed, no point to even start a tdb
1137 TALLOC_FREE(old_subkeys);
1139 store_ctx.key = key;
1140 store_ctx.ctr = ctr;
1142 werr = ntstatus_to_werror(dbwrap_trans_do(db,
1143 regdb_store_keys_action,
1146 ret = W_ERROR_IS_OK(werr);
1154 bool regdb_store_keys(const char *key, struct regsubkey_ctr *ctr)
1156 return regdb_store_keys_internal(regdb, key, ctr);
1160 * create a subkey of a given key
1163 struct regdb_create_subkey_context {
1168 static NTSTATUS regdb_create_subkey_action(struct db_context *db,
1172 struct regdb_create_subkey_context *create_ctx;
1173 struct regsubkey_ctr *subkeys;
1174 TALLOC_CTX *mem_ctx = talloc_stackframe();
1176 create_ctx = (struct regdb_create_subkey_context *)private_data;
1178 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
1179 W_ERROR_NOT_OK_GOTO_DONE(werr);
1181 werr = regdb_fetch_keys_internal(db, create_ctx->key, subkeys);
1182 W_ERROR_NOT_OK_GOTO_DONE(werr);
1184 werr = regsubkey_ctr_addkey(subkeys, create_ctx->subkey);
1185 W_ERROR_NOT_OK_GOTO_DONE(werr);
1187 werr = regdb_store_keys_internal2(db, create_ctx->key, subkeys);
1188 if (!W_ERROR_IS_OK(werr)) {
1189 DEBUG(0, (__location__ " failed to store new subkey list for "
1190 "parent key %s: %s\n", create_ctx->key,
1194 werr = regdb_store_subkey_list(db, create_ctx->key, create_ctx->subkey);
1197 talloc_free(mem_ctx);
1198 return werror_to_ntstatus(werr);
1201 static WERROR regdb_create_subkey_internal(struct db_context *db,
1206 struct regsubkey_ctr *subkeys;
1207 TALLOC_CTX *mem_ctx = talloc_stackframe();
1208 struct regdb_create_subkey_context create_ctx;
1210 if (!regdb_key_exists(db, key)) {
1211 werr = WERR_NOT_FOUND;
1215 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
1216 W_ERROR_NOT_OK_GOTO_DONE(werr);
1218 werr = regdb_fetch_keys_internal(db, key, subkeys);
1219 W_ERROR_NOT_OK_GOTO_DONE(werr);
1221 if (regsubkey_ctr_key_exists(subkeys, subkey)) {
1226 talloc_free(subkeys);
1228 create_ctx.key = key;
1229 create_ctx.subkey = subkey;
1231 werr = ntstatus_to_werror(dbwrap_trans_do(db,
1232 regdb_create_subkey_action,
1236 talloc_free(mem_ctx);
1240 static WERROR regdb_create_subkey(const char *key, const char *subkey)
1242 return regdb_create_subkey_internal(regdb, key, subkey);
1249 struct regdb_create_basekey_context {
1253 static NTSTATUS regdb_create_basekey_action(struct db_context *db,
1257 struct regdb_create_basekey_context *create_ctx;
1259 create_ctx = (struct regdb_create_basekey_context *)private_data;
1261 werr = regdb_store_subkey_list(db, NULL, create_ctx->key);
1263 return werror_to_ntstatus(werr);
1266 static WERROR regdb_create_basekey(struct db_context *db, const char *key)
1269 struct regdb_create_subkey_context create_ctx;
1271 create_ctx.key = key;
1273 werr = ntstatus_to_werror(dbwrap_trans_do(db,
1274 regdb_create_basekey_action,
1281 * create a subkey of a given key
1284 struct regdb_delete_subkey_context {
1290 static NTSTATUS regdb_delete_subkey_action(struct db_context *db,
1294 struct regdb_delete_subkey_context *delete_ctx;
1295 struct regsubkey_ctr *subkeys;
1296 TALLOC_CTX *mem_ctx = talloc_stackframe();
1298 delete_ctx = (struct regdb_delete_subkey_context *)private_data;
1300 werr = regdb_delete_key_lists(db, delete_ctx->path);
1301 W_ERROR_NOT_OK_GOTO_DONE(werr);
1303 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
1304 W_ERROR_NOT_OK_GOTO_DONE(werr);
1306 werr = regdb_fetch_keys_internal(db, delete_ctx->key, subkeys);
1307 W_ERROR_NOT_OK_GOTO_DONE(werr);
1309 werr = regsubkey_ctr_delkey(subkeys, delete_ctx->subkey);
1310 W_ERROR_NOT_OK_GOTO_DONE(werr);
1312 werr = regdb_store_keys_internal2(db, delete_ctx->key, subkeys);
1313 if (!W_ERROR_IS_OK(werr)) {
1314 DEBUG(0, (__location__ " failed to store new subkey_list for "
1315 "parent key %s: %s\n", delete_ctx->key,
1320 talloc_free(mem_ctx);
1321 return werror_to_ntstatus(werr);
1324 static WERROR regdb_delete_subkey(const char *key, const char *subkey)
1328 struct regdb_delete_subkey_context delete_ctx;
1329 TALLOC_CTX *mem_ctx = talloc_stackframe();
1331 if (!regdb_key_exists(regdb, key)) {
1332 werr = WERR_NOT_FOUND;
1336 path = talloc_asprintf(mem_ctx, "%s\\%s", key, subkey);
1342 if (!regdb_key_exists(regdb, path)) {
1347 delete_ctx.key = key;
1348 delete_ctx.subkey = subkey;
1349 delete_ctx.path = path;
1351 werr = ntstatus_to_werror(dbwrap_trans_do(regdb,
1352 regdb_delete_subkey_action,
1356 talloc_free(mem_ctx);
1360 static TDB_DATA regdb_fetch_key_internal(struct db_context *db,
1361 TALLOC_CTX *mem_ctx, const char *key)
1366 path = normalize_reg_path(mem_ctx, key);
1368 return make_tdb_data(NULL, 0);
1371 data = dbwrap_fetch_bystring(db, mem_ctx, path);
1379 * Check for the existence of a key.
1381 * Existence of a key is authoritatively defined by
1382 * the existence of the record that contains the list
1385 static bool regdb_key_exists(struct db_context *db, const char *key)
1387 TALLOC_CTX *mem_ctx = talloc_stackframe();
1396 path = normalize_reg_path(mem_ctx, key);
1398 DEBUG(0, ("out of memory! (talloc failed)\n"));
1402 if (*path == '\0') {
1406 value = regdb_fetch_key_internal(db, mem_ctx, path);
1407 ret = (value.dptr != NULL);
1410 TALLOC_FREE(mem_ctx);
1415 /***********************************************************************
1416 Retrieve an array of strings containing subkeys. Memory should be
1417 released by the caller.
1418 ***********************************************************************/
1420 static WERROR regdb_fetch_keys_internal(struct db_context *db, const char *key,
1421 struct regsubkey_ctr *ctr)
1429 TALLOC_CTX *frame = talloc_stackframe();
1432 DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key ? key : "NULL"));
1434 if (!regdb_key_exists(db, key)) {
1435 DEBUG(10, ("key [%s] not found\n", key));
1436 werr = WERR_NOT_FOUND;
1440 werr = regsubkey_ctr_set_seqnum(ctr, db->get_seqnum(db));
1441 W_ERROR_NOT_OK_GOTO_DONE(werr);
1443 value = regdb_fetch_key_internal(db, frame, key);
1445 if (value.dsize == 0 || value.dptr == NULL) {
1446 DEBUG(10, ("regdb_fetch_keys: no subkeys found for key [%s]\n",
1452 buflen = value.dsize;
1453 len = tdb_unpack( buf, buflen, "d", &num_items);
1454 if (len == (uint32_t)-1) {
1455 werr = WERR_NOT_FOUND;
1459 werr = regsubkey_ctr_reinit(ctr);
1460 W_ERROR_NOT_OK_GOTO_DONE(werr);
1462 for (i=0; i<num_items; i++) {
1463 len += tdb_unpack(buf+len, buflen-len, "f", subkeyname);
1464 werr = regsubkey_ctr_addkey(ctr, subkeyname);
1465 if (!W_ERROR_IS_OK(werr)) {
1466 DEBUG(5, ("regdb_fetch_keys: regsubkey_ctr_addkey "
1467 "failed: %s\n", win_errstr(werr)));
1473 DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items));
1480 int regdb_fetch_keys(const char *key, struct regsubkey_ctr *ctr)
1484 werr = regdb_fetch_keys_internal(regdb, key, ctr);
1485 if (!W_ERROR_IS_OK(werr)) {
1489 return regsubkey_ctr_numkeys(ctr);
1492 /****************************************************************************
1493 Unpack a list of registry values frem the TDB
1494 ***************************************************************************/
1496 static int regdb_unpack_values(struct regval_ctr *values, uint8 *buf, int buflen)
1503 uint32 num_values = 0;
1506 /* loop and unpack the rest of the registry values */
1508 len += tdb_unpack(buf+len, buflen-len, "d", &num_values);
1510 for ( i=0; i<num_values; i++ ) {
1511 /* unpack the next regval */
1516 valuename[0] = '\0';
1517 len += tdb_unpack(buf+len, buflen-len, "fdB",
1523 regval_ctr_addvalue(values, valuename, type,
1524 (uint8_t *)data_p, size);
1525 SAFE_FREE(data_p); /* 'B' option to tdb_unpack does a malloc() */
1527 DEBUG(8,("specific: [%s], len: %d\n", valuename, size));
1533 /****************************************************************************
1534 Pack all values in all printer keys
1535 ***************************************************************************/
1537 static int regdb_pack_values(struct regval_ctr *values, uint8 *buf, int buflen)
1541 struct regval_blob *val;
1547 num_values = regval_ctr_numvals( values );
1549 /* pack the number of values first */
1551 len += tdb_pack( buf+len, buflen-len, "d", num_values );
1553 /* loop over all values */
1555 for ( i=0; i<num_values; i++ ) {
1556 val = regval_ctr_specific_value( values, i );
1557 len += tdb_pack(buf+len, buflen-len, "fdB",
1561 regval_data_p(val) );
1567 /***********************************************************************
1568 Retrieve an array of strings containing subkeys. Memory should be
1569 released by the caller.
1570 ***********************************************************************/
1572 static int regdb_fetch_values_internal(struct db_context *db, const char* key,
1573 struct regval_ctr *values)
1575 char *keystr = NULL;
1576 TALLOC_CTX *ctx = talloc_stackframe();
1581 DEBUG(10,("regdb_fetch_values: Looking for value of key [%s] \n", key));
1583 if (!regdb_key_exists(db, key)) {
1587 keystr = talloc_asprintf(ctx, "%s\\%s", REG_VALUE_PREFIX, key);
1592 werr = regval_ctr_set_seqnum(values, db->get_seqnum(db));
1593 W_ERROR_NOT_OK_GOTO_DONE(werr);
1595 value = regdb_fetch_key_internal(db, ctx, keystr);
1598 /* all keys have zero values by default */
1602 regdb_unpack_values(values, value.dptr, value.dsize);
1603 ret = regval_ctr_numvals(values);
1610 int regdb_fetch_values(const char* key, struct regval_ctr *values)
1612 return regdb_fetch_values_internal(regdb, key, values);
1615 static bool regdb_store_values_internal(struct db_context *db, const char *key,
1616 struct regval_ctr *values)
1618 TDB_DATA old_data, data;
1619 char *keystr = NULL;
1620 TALLOC_CTX *ctx = talloc_stackframe();
1623 bool result = false;
1625 DEBUG(10,("regdb_store_values: Looking for value of key [%s] \n", key));
1627 if (!regdb_key_exists(db, key)) {
1633 len = regdb_pack_values(values, data.dptr, data.dsize);
1635 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
1639 data.dptr = talloc_array(ctx, uint8, len);
1642 len = regdb_pack_values(values, data.dptr, data.dsize);
1644 SMB_ASSERT( len == data.dsize );
1646 keystr = talloc_asprintf(ctx, "%s\\%s", REG_VALUE_PREFIX, key );
1650 keystr = normalize_reg_path(ctx, keystr);
1655 old_data = dbwrap_fetch_bystring(db, ctx, keystr);
1657 if ((old_data.dptr != NULL)
1658 && (old_data.dsize == data.dsize)
1659 && (memcmp(old_data.dptr, data.dptr, data.dsize) == 0))
1665 status = dbwrap_trans_store_bystring(db, keystr, data, TDB_REPLACE);
1667 result = NT_STATUS_IS_OK(status);
1674 bool regdb_store_values(const char *key, struct regval_ctr *values)
1676 return regdb_store_values_internal(regdb, key, values);
1679 static WERROR regdb_get_secdesc(TALLOC_CTX *mem_ctx, const char *key,
1680 struct security_descriptor **psecdesc)
1685 TALLOC_CTX *tmp_ctx = talloc_stackframe();
1686 WERROR err = WERR_OK;
1688 DEBUG(10, ("regdb_get_secdesc: Getting secdesc of key [%s]\n", key));
1690 if (!regdb_key_exists(regdb, key)) {
1695 tdbkey = talloc_asprintf(tmp_ctx, "%s\\%s", REG_SECDESC_PREFIX, key);
1696 if (tdbkey == NULL) {
1701 tdbkey = normalize_reg_path(tmp_ctx, tdbkey);
1702 if (tdbkey == NULL) {
1707 data = dbwrap_fetch_bystring(regdb, tmp_ctx, tdbkey);
1708 if (data.dptr == NULL) {
1713 status = unmarshall_sec_desc(mem_ctx, (uint8 *)data.dptr, data.dsize,
1716 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MEMORY)) {
1718 } else if (!NT_STATUS_IS_OK(status)) {
1719 err = WERR_REG_CORRUPT;
1723 TALLOC_FREE(tmp_ctx);
1727 static WERROR regdb_set_secdesc(const char *key,
1728 struct security_descriptor *secdesc)
1730 TALLOC_CTX *mem_ctx = talloc_stackframe();
1732 WERROR err = WERR_NOMEM;
1735 if (!regdb_key_exists(regdb, key)) {
1740 tdbkey = talloc_asprintf(mem_ctx, "%s\\%s", REG_SECDESC_PREFIX, key);
1741 if (tdbkey == NULL) {
1745 tdbkey = normalize_reg_path(mem_ctx, tdbkey);
1746 if (tdbkey == NULL) {
1751 if (secdesc == NULL) {
1752 /* assuming a delete */
1753 err = ntstatus_to_werror(dbwrap_trans_delete_bystring(regdb,
1758 err = ntstatus_to_werror(marshall_sec_desc(mem_ctx, secdesc,
1761 W_ERROR_NOT_OK_GOTO_DONE(err);
1763 err = ntstatus_to_werror(dbwrap_trans_store_bystring(regdb, tdbkey,
1767 TALLOC_FREE(mem_ctx);
1771 bool regdb_subkeys_need_update(struct regsubkey_ctr *subkeys)
1773 return (regdb_get_seqnum() != regsubkey_ctr_get_seqnum(subkeys));
1776 bool regdb_values_need_update(struct regval_ctr *values)
1778 return (regdb_get_seqnum() != regval_ctr_get_seqnum(values));
1782 * Table of function pointers for default access
1785 struct registry_ops regdb_ops = {
1786 .fetch_subkeys = regdb_fetch_keys,
1787 .fetch_values = regdb_fetch_values,
1788 .store_subkeys = regdb_store_keys,
1789 .store_values = regdb_store_values,
1790 .create_subkey = regdb_create_subkey,
1791 .delete_subkey = regdb_delete_subkey,
1792 .get_secdesc = regdb_get_secdesc,
1793 .set_secdesc = regdb_set_secdesc,
1794 .subkeys_need_update = regdb_subkeys_need_update,
1795 .values_need_update = regdb_values_need_update