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 bool regdb_key_is_base_key(const char *key);
43 static WERROR regdb_fetch_keys_internal(struct db_context *db, const char *key,
44 struct regsubkey_ctr *ctr);
45 static bool regdb_store_keys_internal(struct db_context *db, const char *key,
46 struct regsubkey_ctr *ctr);
47 static int regdb_fetch_values_internal(struct db_context *db, const char* key,
48 struct regval_ctr *values);
49 static bool regdb_store_values_internal(struct db_context *db, const char *key,
50 struct regval_ctr *values);
52 static NTSTATUS create_sorted_subkeys(const char *key);
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(uint32_t version)
415 const char *version_keyname = "INFO/version";
418 return WERR_CAN_NOT_COMPLETE;
421 status = dbwrap_trans_store_int32(regdb, 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(void)
439 mem_ctx = talloc_stackframe();
440 if (mem_ctx == NULL) {
444 rc = regdb->traverse(regdb, regdb_normalize_keynames_fn, mem_ctx);
446 talloc_destroy(mem_ctx);
449 return WERR_REG_IO_FAILURE;
452 werr = regdb_store_regdb_version(REGVER_V2);
456 /***********************************************************************
457 Open the registry database
458 ***********************************************************************/
460 WERROR regdb_init(void)
462 const char *vstring = "INFO/version";
463 uint32 vers_id, expected_version;
467 DEBUG(10, ("regdb_init: incrementing refcount (%d->%d)\n",
468 regdb_refcount, regdb_refcount+1));
473 regdb = db_open(NULL, state_path("registry.tdb"), 0,
474 REG_TDB_FLAGS, O_RDWR, 0600);
476 regdb = db_open(NULL, state_path("registry.tdb"), 0,
477 REG_TDB_FLAGS, O_RDWR|O_CREAT, 0600);
479 werr = ntstatus_to_werror(map_nt_error_from_unix(errno));
480 DEBUG(1,("regdb_init: Failed to open registry %s (%s)\n",
481 state_path("registry.tdb"), strerror(errno) ));
485 DEBUG(10,("regdb_init: Successfully created registry tdb\n"));
489 DEBUG(10, ("regdb_init: registry db openend. refcount reset (%d)\n",
492 expected_version = REGVER_V2;
494 vers_id = dbwrap_fetch_int32(regdb, vstring);
496 DEBUG(10, ("regdb_init: registry version uninitialized "
497 "(got %d), initializing to version %d\n",
498 vers_id, expected_version));
500 werr = regdb_store_regdb_version(expected_version);
504 if (vers_id > expected_version || vers_id == 0) {
505 DEBUG(1, ("regdb_init: unknown registry version %d "
506 "(code version = %d), refusing initialization\n",
507 vers_id, expected_version));
508 return WERR_CAN_NOT_COMPLETE;
511 if (vers_id == REGVER_V1) {
512 DEBUG(10, ("regdb_init: got registry db version %d, upgrading "
513 "to version %d\n", REGVER_V1, REGVER_V2));
515 if (regdb->transaction_start(regdb) != 0) {
516 return WERR_REG_IO_FAILURE;
519 werr = regdb_upgrade_v1_to_v2();
520 if (!W_ERROR_IS_OK(werr)) {
521 regdb->transaction_cancel(regdb);
525 if (regdb->transaction_commit(regdb) != 0) {
526 return WERR_REG_IO_FAILURE;
532 /* future upgrade code should go here */
537 /***********************************************************************
538 Open the registry. Must already have been initialized by regdb_init()
539 ***********************************************************************/
541 WERROR regdb_open( void )
543 WERROR result = WERR_OK;
546 DEBUG(10, ("regdb_open: incrementing refcount (%d->%d)\n",
547 regdb_refcount, regdb_refcount+1));
554 regdb = db_open(NULL, state_path("registry.tdb"), 0,
555 REG_TDB_FLAGS, O_RDWR, 0600);
557 result = ntstatus_to_werror( map_nt_error_from_unix( errno ) );
558 DEBUG(0,("regdb_open: Failed to open %s! (%s)\n",
559 state_path("registry.tdb"), strerror(errno) ));
565 DEBUG(10, ("regdb_open: registry db opened. refcount reset (%d)\n",
571 /***********************************************************************
572 ***********************************************************************/
574 int regdb_close( void )
576 if (regdb_refcount == 0) {
582 DEBUG(10, ("regdb_close: decrementing refcount (%d->%d)\n",
583 regdb_refcount+1, regdb_refcount));
585 if ( regdb_refcount > 0 )
588 SMB_ASSERT( regdb_refcount >= 0 );
594 WERROR regdb_transaction_start(void)
596 return (regdb->transaction_start(regdb) == 0) ?
597 WERR_OK : WERR_REG_IO_FAILURE;
600 WERROR regdb_transaction_commit(void)
602 return (regdb->transaction_commit(regdb) == 0) ?
603 WERR_OK : WERR_REG_IO_FAILURE;
606 WERROR regdb_transaction_cancel(void)
608 return (regdb->transaction_cancel(regdb) == 0) ?
609 WERR_OK : WERR_REG_IO_FAILURE;
612 /***********************************************************************
613 return the tdb sequence number of the registry tdb.
614 this is an indicator for the content of the registry
615 having changed. it will change upon regdb_init, too, though.
616 ***********************************************************************/
617 int regdb_get_seqnum(void)
619 return regdb->get_seqnum(regdb);
623 static WERROR regdb_delete_key_with_prefix(struct db_context *db,
628 WERROR werr = WERR_NOMEM;
629 TALLOC_CTX *mem_ctx = talloc_stackframe();
631 if (keyname == NULL) {
632 werr = WERR_INVALID_PARAM;
636 if (prefix == NULL) {
637 path = discard_const_p(char, keyname);
639 path = talloc_asprintf(mem_ctx, "%s\\%s", prefix, keyname);
645 path = normalize_reg_path(mem_ctx, path);
650 werr = ntstatus_to_werror(dbwrap_delete_bystring(db, path));
652 /* treat "not" found" as ok */
653 if (W_ERROR_EQUAL(werr, WERR_NOT_FOUND)) {
658 talloc_free(mem_ctx);
663 static WERROR regdb_delete_values(struct db_context *db, const char *keyname)
665 return regdb_delete_key_with_prefix(db, keyname, REG_VALUE_PREFIX);
668 static WERROR regdb_delete_secdesc(struct db_context *db, const char *keyname)
670 return regdb_delete_key_with_prefix(db, keyname, REG_SECDESC_PREFIX);
673 static WERROR regdb_delete_subkeylist(struct db_context *db, const char *keyname)
675 return regdb_delete_key_with_prefix(db, keyname, NULL);
678 static WERROR regdb_delete_sorted_subkeys(struct db_context *db,
681 return regdb_delete_key_with_prefix(db, keyname, REG_SORTED_SUBKEYS_PREFIX);
685 static WERROR regdb_delete_key_lists(struct db_context *db, const char *keyname)
689 werr = regdb_delete_values(db, keyname);
690 if (!W_ERROR_IS_OK(werr)) {
691 DEBUG(1, (__location__ " Deleting %s\\%s failed: %s\n",
692 REG_VALUE_PREFIX, keyname, win_errstr(werr)));
696 werr = regdb_delete_secdesc(db, keyname);
697 if (!W_ERROR_IS_OK(werr)) {
698 DEBUG(1, (__location__ " Deleting %s\\%s failed: %s\n",
699 REG_SECDESC_PREFIX, keyname, win_errstr(werr)));
703 werr = regdb_delete_sorted_subkeys(db, keyname);
704 if (!W_ERROR_IS_OK(werr)) {
705 DEBUG(1, (__location__ " Deleting %s\\%s failed: %s\n",
706 REG_SORTED_SUBKEYS_PREFIX, keyname,
711 werr = regdb_delete_subkeylist(db, keyname);
712 if (!W_ERROR_IS_OK(werr)) {
713 DEBUG(1, (__location__ " Deleting %s failed: %s\n",
714 keyname, win_errstr(werr)));
722 /***********************************************************************
723 Add subkey strings to the registry tdb under a defined key
724 fmt is the same format as tdb_pack except this function only supports
726 ***********************************************************************/
728 static WERROR regdb_store_keys_internal2(struct db_context *db,
730 struct regsubkey_ctr *ctr)
733 uint8 *buffer = NULL;
736 uint32 num_subkeys = regsubkey_ctr_numkeys(ctr);
737 char *keyname = NULL;
738 TALLOC_CTX *ctx = talloc_stackframe();
742 werr = WERR_INVALID_PARAM;
746 keyname = talloc_strdup(ctx, key);
752 keyname = normalize_reg_path(ctx, keyname);
758 /* allocate some initial memory */
760 buffer = (uint8 *)SMB_MALLOC(1024);
761 if (buffer == NULL) {
768 /* store the number of subkeys */
770 len += tdb_pack(buffer+len, buflen-len, "d", num_subkeys);
772 /* pack all the strings */
774 for (i=0; i<num_subkeys; i++) {
777 thistime = tdb_pack(buffer+len, buflen-len, "f",
778 regsubkey_ctr_specific_key(ctr, i));
779 if (len+thistime > buflen) {
782 * tdb_pack hasn't done anything because of the short
783 * buffer, allocate extra space.
785 buffer = SMB_REALLOC_ARRAY(buffer, uint8_t,
788 DEBUG(0, ("regdb_store_keys: Failed to realloc "
789 "memory of size [%u]\n",
790 (unsigned int)(len+thistime)*2));
794 buflen = (len+thistime)*2;
795 thistime2 = tdb_pack(
796 buffer+len, buflen-len, "f",
797 regsubkey_ctr_specific_key(ctr, i));
798 if (thistime2 != thistime) {
799 DEBUG(0, ("tdb_pack failed\n"));
800 werr = WERR_CAN_NOT_COMPLETE;
807 /* finally write out the data */
811 werr = ntstatus_to_werror(dbwrap_store_bystring(db, keyname, dbuf,
821 * Utility function to store a new empty list of
822 * subkeys of given key specified as parent and subkey name
823 * (thereby creating the key).
824 * If the parent keyname is NULL, then the "subkey" is
825 * interpreted as a base key.
826 * If the subkey list does already exist, it is not modified.
828 * Must be called from within a transaction.
830 static WERROR regdb_store_subkey_list(struct db_context *db, const char *parent,
835 struct regsubkey_ctr *subkeys = NULL;
836 TALLOC_CTX *frame = talloc_stackframe();
838 if (parent == NULL) {
839 path = talloc_strdup(frame, key);
841 path = talloc_asprintf(frame, "%s\\%s", parent, key);
848 werr = regsubkey_ctr_init(frame, &subkeys);
849 W_ERROR_NOT_OK_GOTO_DONE(werr);
851 werr = regdb_fetch_keys_internal(db, path, subkeys);
852 if (W_ERROR_IS_OK(werr)) {
853 /* subkey list exists already - don't modify */
857 werr = regsubkey_ctr_reinit(subkeys);
858 W_ERROR_NOT_OK_GOTO_DONE(werr);
860 /* create a record with 0 subkeys */
861 werr = regdb_store_keys_internal2(db, path, subkeys);
862 if (!W_ERROR_IS_OK(werr)) {
863 DEBUG(0, ("regdb_store_keys: Failed to store new record for "
864 "key [%s]: %s\n", path, win_errstr(werr)));
873 /***********************************************************************
874 Store the new subkey record and create any child key records that
875 do not currently exist
876 ***********************************************************************/
878 struct regdb_store_keys_context {
880 struct regsubkey_ctr *ctr;
883 static NTSTATUS regdb_store_keys_action(struct db_context *db,
886 struct regdb_store_keys_context *store_ctx;
890 struct regsubkey_ctr *old_subkeys = NULL;
891 char *oldkeyname = NULL;
892 TALLOC_CTX *mem_ctx = talloc_stackframe();
894 store_ctx = (struct regdb_store_keys_context *)private_data;
897 * Re-fetch the old keys inside the transaction
900 werr = regsubkey_ctr_init(mem_ctx, &old_subkeys);
901 W_ERROR_NOT_OK_GOTO_DONE(werr);
903 werr = regdb_fetch_keys_internal(db, store_ctx->key, old_subkeys);
904 if (!W_ERROR_IS_OK(werr) &&
905 !W_ERROR_EQUAL(werr, WERR_NOT_FOUND))
911 * Make the store operation as safe as possible without transactions:
913 * (1) For each subkey removed from ctr compared with old_subkeys:
915 * (a) First delete the value db entry.
917 * (b) Next delete the secdesc db record.
919 * (c) Then delete the subkey list entry.
921 * (2) Now write the list of subkeys of the parent key,
922 * deleting removed entries and adding new ones.
924 * (3) Finally create the subkey list entries for the added keys.
926 * This way if we crash half-way in between deleting the subkeys
927 * and storing the parent's list of subkeys, no old data can pop up
928 * out of the blue when re-adding keys later on.
931 /* (1) delete removed keys' lists (values/secdesc/subkeys) */
933 num_subkeys = regsubkey_ctr_numkeys(old_subkeys);
934 for (i=0; i<num_subkeys; i++) {
935 oldkeyname = regsubkey_ctr_specific_key(old_subkeys, i);
937 if (regsubkey_ctr_key_exists(store_ctx->ctr, oldkeyname)) {
939 * It's still around, don't delete
944 path = talloc_asprintf(mem_ctx, "%s\\%s", store_ctx->key,
951 werr = regdb_delete_key_lists(db, path);
952 W_ERROR_NOT_OK_GOTO_DONE(werr);
957 TALLOC_FREE(old_subkeys);
959 /* (2) store the subkey list for the parent */
961 werr = regdb_store_keys_internal2(db, store_ctx->key, store_ctx->ctr);
962 if (!W_ERROR_IS_OK(werr)) {
963 DEBUG(0,("regdb_store_keys: Failed to store new subkey list "
964 "for parent [%s]: %s\n", store_ctx->key,
969 /* (3) now create records for any subkeys that don't already exist */
971 num_subkeys = regsubkey_ctr_numkeys(store_ctx->ctr);
973 for (i=0; i<num_subkeys; i++) {
976 subkey = regsubkey_ctr_specific_key(store_ctx->ctr, i);
978 werr = regdb_store_subkey_list(db, store_ctx->key, subkey);
979 W_ERROR_NOT_OK_GOTO_DONE(werr);
985 talloc_free(mem_ctx);
986 return werror_to_ntstatus(werr);
989 static bool regdb_store_keys_internal(struct db_context *db, const char *key,
990 struct regsubkey_ctr *ctr)
992 int num_subkeys, old_num_subkeys, i;
993 struct regsubkey_ctr *old_subkeys = NULL;
994 TALLOC_CTX *ctx = talloc_stackframe();
997 struct regdb_store_keys_context store_ctx;
999 if (!regdb_key_exists(db, key)) {
1004 * fetch a list of the old subkeys so we can determine if anything has
1008 werr = regsubkey_ctr_init(ctx, &old_subkeys);
1009 if (!W_ERROR_IS_OK(werr)) {
1010 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
1014 werr = regdb_fetch_keys_internal(db, key, old_subkeys);
1015 if (!W_ERROR_IS_OK(werr) &&
1016 !W_ERROR_EQUAL(werr, WERR_NOT_FOUND))
1021 num_subkeys = regsubkey_ctr_numkeys(ctr);
1022 old_num_subkeys = regsubkey_ctr_numkeys(old_subkeys);
1023 if ((num_subkeys && old_num_subkeys) &&
1024 (num_subkeys == old_num_subkeys)) {
1026 for (i = 0; i < num_subkeys; i++) {
1027 if (strcmp(regsubkey_ctr_specific_key(ctr, i),
1028 regsubkey_ctr_specific_key(old_subkeys, i))
1034 if (i == num_subkeys) {
1036 * Nothing changed, no point to even start a tdb
1045 TALLOC_FREE(old_subkeys);
1047 store_ctx.key = key;
1048 store_ctx.ctr = ctr;
1050 werr = ntstatus_to_werror(dbwrap_trans_do(db,
1051 regdb_store_keys_action,
1054 ret = W_ERROR_IS_OK(werr);
1062 bool regdb_store_keys(const char *key, struct regsubkey_ctr *ctr)
1064 return regdb_store_keys_internal(regdb, key, ctr);
1068 * create a subkey of a given key
1071 struct regdb_create_subkey_context {
1076 static NTSTATUS regdb_create_subkey_action(struct db_context *db,
1080 struct regdb_create_subkey_context *create_ctx;
1081 struct regsubkey_ctr *subkeys;
1082 TALLOC_CTX *mem_ctx = talloc_stackframe();
1084 create_ctx = (struct regdb_create_subkey_context *)private_data;
1086 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
1087 W_ERROR_NOT_OK_GOTO_DONE(werr);
1089 werr = regdb_fetch_keys_internal(db, create_ctx->key, subkeys);
1090 W_ERROR_NOT_OK_GOTO_DONE(werr);
1092 werr = regsubkey_ctr_addkey(subkeys, create_ctx->subkey);
1093 W_ERROR_NOT_OK_GOTO_DONE(werr);
1095 werr = regdb_store_keys_internal2(db, create_ctx->key, subkeys);
1096 if (!W_ERROR_IS_OK(werr)) {
1097 DEBUG(0, (__location__ " failed to store new subkey list for "
1098 "parent key %s: %s\n", create_ctx->key,
1102 werr = regdb_store_subkey_list(db, create_ctx->key, create_ctx->subkey);
1105 talloc_free(mem_ctx);
1106 return werror_to_ntstatus(werr);
1109 static WERROR regdb_create_subkey_internal(struct db_context *db,
1114 struct regsubkey_ctr *subkeys;
1115 TALLOC_CTX *mem_ctx = talloc_stackframe();
1116 struct regdb_create_subkey_context create_ctx;
1118 if (!regdb_key_exists(db, key)) {
1119 werr = WERR_NOT_FOUND;
1123 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
1124 W_ERROR_NOT_OK_GOTO_DONE(werr);
1126 werr = regdb_fetch_keys_internal(db, key, subkeys);
1127 W_ERROR_NOT_OK_GOTO_DONE(werr);
1129 if (regsubkey_ctr_key_exists(subkeys, subkey)) {
1134 talloc_free(subkeys);
1136 create_ctx.key = key;
1137 create_ctx.subkey = subkey;
1139 werr = ntstatus_to_werror(dbwrap_trans_do(db,
1140 regdb_create_subkey_action,
1144 talloc_free(mem_ctx);
1148 static WERROR regdb_create_subkey(const char *key, const char *subkey)
1150 return regdb_create_subkey_internal(regdb, key, subkey);
1157 struct regdb_create_basekey_context {
1161 static NTSTATUS regdb_create_basekey_action(struct db_context *db,
1165 struct regdb_create_basekey_context *create_ctx;
1167 create_ctx = (struct regdb_create_basekey_context *)private_data;
1169 werr = regdb_store_subkey_list(db, NULL, create_ctx->key);
1171 return werror_to_ntstatus(werr);
1174 static WERROR regdb_create_basekey(struct db_context *db, const char *key)
1177 struct regdb_create_subkey_context create_ctx;
1179 create_ctx.key = key;
1181 werr = ntstatus_to_werror(dbwrap_trans_do(db,
1182 regdb_create_basekey_action,
1189 * create a subkey of a given key
1192 struct regdb_delete_subkey_context {
1198 static NTSTATUS regdb_delete_subkey_action(struct db_context *db,
1202 struct regdb_delete_subkey_context *delete_ctx;
1203 struct regsubkey_ctr *subkeys;
1204 TALLOC_CTX *mem_ctx = talloc_stackframe();
1206 delete_ctx = (struct regdb_delete_subkey_context *)private_data;
1208 werr = regdb_delete_key_lists(db, delete_ctx->path);
1209 W_ERROR_NOT_OK_GOTO_DONE(werr);
1211 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
1212 W_ERROR_NOT_OK_GOTO_DONE(werr);
1214 werr = regdb_fetch_keys_internal(db, delete_ctx->key, subkeys);
1215 W_ERROR_NOT_OK_GOTO_DONE(werr);
1217 werr = regsubkey_ctr_delkey(subkeys, delete_ctx->subkey);
1218 W_ERROR_NOT_OK_GOTO_DONE(werr);
1220 werr = regdb_store_keys_internal2(db, delete_ctx->key, subkeys);
1221 if (!W_ERROR_IS_OK(werr)) {
1222 DEBUG(0, (__location__ " failed to store new subkey_list for "
1223 "parent key %s: %s\n", delete_ctx->key,
1228 talloc_free(mem_ctx);
1229 return werror_to_ntstatus(werr);
1232 static WERROR regdb_delete_subkey(const char *key, const char *subkey)
1236 struct regdb_delete_subkey_context delete_ctx;
1237 TALLOC_CTX *mem_ctx = talloc_stackframe();
1239 if (!regdb_key_exists(regdb, key)) {
1240 werr = WERR_NOT_FOUND;
1244 path = talloc_asprintf(mem_ctx, "%s\\%s", key, subkey);
1250 if (!regdb_key_exists(regdb, path)) {
1255 delete_ctx.key = key;
1256 delete_ctx.subkey = subkey;
1257 delete_ctx.path = path;
1259 werr = ntstatus_to_werror(dbwrap_trans_do(regdb,
1260 regdb_delete_subkey_action,
1264 talloc_free(mem_ctx);
1268 static TDB_DATA regdb_fetch_key_internal(struct db_context *db,
1269 TALLOC_CTX *mem_ctx, const char *key)
1274 path = normalize_reg_path(mem_ctx, key);
1276 return make_tdb_data(NULL, 0);
1279 data = dbwrap_fetch_bystring(db, mem_ctx, path);
1287 * check whether a given key name represents a base key,
1288 * i.e one without a subkey separator ('\').
1290 static bool regdb_key_is_base_key(const char *key)
1292 TALLOC_CTX *mem_ctx = talloc_stackframe();
1300 path = normalize_reg_path(mem_ctx, key);
1302 DEBUG(0, ("out of memory! (talloc failed)\n"));
1306 if (*path == '\0') {
1310 ret = (strrchr(path, '\\') == NULL);
1313 TALLOC_FREE(mem_ctx);
1318 * regdb_key_exists() is a very frequent operation. It can be quite
1319 * time-consuming to fully fetch the parent's subkey list, talloc_strdup all
1320 * subkeys and then compare the keyname linearly to all the parent's subkeys.
1322 * The following code tries to make this operation as efficient as possible:
1323 * Per registry key we create a list of subkeys that is very efficient to
1324 * search for existence of a subkey. Its format is:
1326 * 4 bytes num_subkeys
1327 * 4*num_subkey bytes offset into the string array
1328 * then follows a sorted list of subkeys in uppercase
1330 * This record is created by create_sorted_subkeys() on demand if it does not
1331 * exist. scan_parent_subkeys() uses regdb->parse_record to search the sorted
1332 * list, the parsing code and the binary search can be found in
1333 * parent_subkey_scanner. The code uses parse_record() to avoid a memcpy of
1334 * the potentially large subkey record.
1336 * The sorted subkey record is deleted in regdb_store_keys_internal2 and
1337 * recreated on demand.
1340 static int cmp_keynames(char **p1, char **p2)
1342 return strcasecmp_m(*p1, *p2);
1345 struct create_sorted_subkeys_context {
1347 const char *sorted_keyname;
1350 static NTSTATUS create_sorted_subkeys_action(struct db_context *db,
1353 char **sorted_subkeys;
1354 struct regsubkey_ctr *ctr;
1361 struct create_sorted_subkeys_context *sorted_ctx;
1363 sorted_ctx = (struct create_sorted_subkeys_context *)private_data;
1366 * In this function, we only treat failing of the actual write to
1367 * the db as a real error. All preliminary errors, at a stage when
1368 * nothing has been written to the DB yet are treated as success
1369 * to be committed (as an empty transaction).
1371 * The reason is that this (disposable) call might be nested in other
1372 * transactions. Doing a cancel here would destroy the possibility of
1373 * a transaction_commit for transactions that we might be wrapped in.
1376 status = werror_to_ntstatus(regsubkey_ctr_init(talloc_tos(), &ctr));
1377 if (!NT_STATUS_IS_OK(status)) {
1378 /* don't treat this as an error */
1379 status = NT_STATUS_OK;
1383 status = werror_to_ntstatus(regdb_fetch_keys_internal(db,
1386 if (!NT_STATUS_IS_OK(status)) {
1387 /* don't treat this as an error */
1388 status = NT_STATUS_OK;
1392 num_subkeys = regsubkey_ctr_numkeys(ctr);
1393 sorted_subkeys = talloc_array(ctr, char *, num_subkeys);
1394 if (sorted_subkeys == NULL) {
1395 /* don't treat this as an error */
1399 len = 4 + 4*num_subkeys;
1401 for (i = 0; i < num_subkeys; i++) {
1402 sorted_subkeys[i] = talloc_strdup_upper(sorted_subkeys,
1403 regsubkey_ctr_specific_key(ctr, i));
1404 if (sorted_subkeys[i] == NULL) {
1405 /* don't treat this as an error */
1408 len += strlen(sorted_subkeys[i])+1;
1411 TYPESAFE_QSORT(sorted_subkeys, num_subkeys, cmp_keynames);
1413 buf = talloc_array(ctr, char, len);
1415 /* don't treat this as an error */
1418 p = buf + 4 + 4*num_subkeys;
1420 SIVAL(buf, 0, num_subkeys);
1422 for (i=0; i < num_subkeys; i++) {
1423 ptrdiff_t offset = p - buf;
1424 SIVAL(buf, 4 + 4*i, offset);
1425 strlcpy(p, sorted_subkeys[i], len-offset);
1426 p += strlen(sorted_subkeys[i]) + 1;
1429 status = dbwrap_store_bystring(
1430 db, sorted_ctx->sorted_keyname, make_tdb_data((uint8_t *)buf,
1439 static NTSTATUS create_sorted_subkeys_internal(const char *key,
1440 const char *sorted_keyname)
1443 struct create_sorted_subkeys_context sorted_ctx;
1445 sorted_ctx.key = key;
1446 sorted_ctx.sorted_keyname = sorted_keyname;
1448 status = dbwrap_trans_do(regdb,
1449 create_sorted_subkeys_action,
1455 static NTSTATUS create_sorted_subkeys(const char *key)
1457 char *sorted_subkeys_keyname;
1460 sorted_subkeys_keyname = talloc_asprintf(talloc_tos(), "%s\\%s",
1461 REG_SORTED_SUBKEYS_PREFIX,
1463 if (sorted_subkeys_keyname == NULL) {
1464 status = NT_STATUS_NO_MEMORY;
1468 status = create_sorted_subkeys_internal(key, sorted_subkeys_keyname);
1474 struct scan_subkey_state {
1480 static int parent_subkey_scanner(TDB_DATA key, TDB_DATA data,
1483 struct scan_subkey_state *state =
1484 (struct scan_subkey_state *)private_data;
1485 uint32_t num_subkeys;
1488 if (data.dsize < sizeof(uint32_t)) {
1492 state->scanned = true;
1493 state->found = false;
1495 tdb_unpack(data.dptr, data.dsize, "d", &num_subkeys);
1501 uint32_t idx = (l+u)/2;
1502 char *s = (char *)data.dptr + IVAL(data.dptr, 4 + 4*idx);
1503 int comparison = strcmp(state->name, s);
1505 if (comparison < 0) {
1507 } else if (comparison > 0) {
1510 state->found = true;
1517 static bool scan_parent_subkeys(struct db_context *db, const char *parent,
1522 struct scan_subkey_state state = { 0, };
1523 bool result = false;
1528 path = normalize_reg_path(talloc_tos(), parent);
1533 key = talloc_asprintf(talloc_tos(), "%s\\%s",
1534 REG_SORTED_SUBKEYS_PREFIX, path);
1539 state.name = talloc_strdup_upper(talloc_tos(), name);
1540 if (state.name == NULL) {
1543 state.scanned = false;
1545 res = db->parse_record(db, string_term_tdb_data(key),
1546 parent_subkey_scanner, &state);
1548 if (state.scanned) {
1549 result = state.found;
1553 res = db->transaction_start(db);
1555 DEBUG(0, ("error starting transaction\n"));
1559 DEBUG(2, (__location__ " WARNING: recreating the sorted "
1560 "subkeys cache for key '%s' from scan_parent_subkeys "
1561 "this should not happen (too frequently)...\n",
1564 status = create_sorted_subkeys_internal(path, key);
1565 if (!NT_STATUS_IS_OK(status)) {
1566 res = db->transaction_cancel(db);
1568 smb_panic("Failed to cancel transaction.");
1573 res = db->parse_record(db, string_term_tdb_data(key),
1574 parent_subkey_scanner, &state);
1575 if ((res == 0) && (state.scanned)) {
1576 result = state.found;
1579 res = db->transaction_commit(db);
1581 DEBUG(0, ("error committing transaction\n"));
1588 TALLOC_FREE(state.name);
1593 * Check for the existence of a key.
1595 * Existence of a key is authoritatively defined by
1596 * the existence of the record that contains the list
1599 static bool regdb_key_exists(struct db_context *db, const char *key)
1601 TALLOC_CTX *mem_ctx = talloc_stackframe();
1610 path = normalize_reg_path(mem_ctx, key);
1612 DEBUG(0, ("out of memory! (talloc failed)\n"));
1616 if (*path == '\0') {
1620 value = regdb_fetch_key_internal(db, mem_ctx, path);
1621 ret = (value.dptr != NULL);
1624 TALLOC_FREE(mem_ctx);
1629 /***********************************************************************
1630 Retrieve an array of strings containing subkeys. Memory should be
1631 released by the caller.
1632 ***********************************************************************/
1634 static WERROR regdb_fetch_keys_internal(struct db_context *db, const char *key,
1635 struct regsubkey_ctr *ctr)
1643 TALLOC_CTX *frame = talloc_stackframe();
1646 DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key ? key : "NULL"));
1648 if (!regdb_key_exists(db, key)) {
1649 DEBUG(10, ("key [%s] not found\n", key));
1650 werr = WERR_NOT_FOUND;
1654 werr = regsubkey_ctr_set_seqnum(ctr, db->get_seqnum(db));
1655 W_ERROR_NOT_OK_GOTO_DONE(werr);
1657 value = regdb_fetch_key_internal(db, frame, key);
1659 if (value.dsize == 0 || value.dptr == NULL) {
1660 DEBUG(10, ("regdb_fetch_keys: no subkeys found for key [%s]\n",
1666 buflen = value.dsize;
1667 len = tdb_unpack( buf, buflen, "d", &num_items);
1668 if (len == (uint32_t)-1) {
1669 werr = WERR_NOT_FOUND;
1673 werr = regsubkey_ctr_reinit(ctr);
1674 W_ERROR_NOT_OK_GOTO_DONE(werr);
1676 for (i=0; i<num_items; i++) {
1677 len += tdb_unpack(buf+len, buflen-len, "f", subkeyname);
1678 werr = regsubkey_ctr_addkey(ctr, subkeyname);
1679 if (!W_ERROR_IS_OK(werr)) {
1680 DEBUG(5, ("regdb_fetch_keys: regsubkey_ctr_addkey "
1681 "failed: %s\n", win_errstr(werr)));
1687 DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items));
1694 int regdb_fetch_keys(const char *key, struct regsubkey_ctr *ctr)
1698 werr = regdb_fetch_keys_internal(regdb, key, ctr);
1699 if (!W_ERROR_IS_OK(werr)) {
1703 return regsubkey_ctr_numkeys(ctr);
1706 /****************************************************************************
1707 Unpack a list of registry values frem the TDB
1708 ***************************************************************************/
1710 static int regdb_unpack_values(struct regval_ctr *values, uint8 *buf, int buflen)
1717 uint32 num_values = 0;
1720 /* loop and unpack the rest of the registry values */
1722 len += tdb_unpack(buf+len, buflen-len, "d", &num_values);
1724 for ( i=0; i<num_values; i++ ) {
1725 /* unpack the next regval */
1730 valuename[0] = '\0';
1731 len += tdb_unpack(buf+len, buflen-len, "fdB",
1737 regval_ctr_addvalue(values, valuename, type,
1738 (uint8_t *)data_p, size);
1739 SAFE_FREE(data_p); /* 'B' option to tdb_unpack does a malloc() */
1741 DEBUG(8,("specific: [%s], len: %d\n", valuename, size));
1747 /****************************************************************************
1748 Pack all values in all printer keys
1749 ***************************************************************************/
1751 static int regdb_pack_values(struct regval_ctr *values, uint8 *buf, int buflen)
1755 struct regval_blob *val;
1761 num_values = regval_ctr_numvals( values );
1763 /* pack the number of values first */
1765 len += tdb_pack( buf+len, buflen-len, "d", num_values );
1767 /* loop over all values */
1769 for ( i=0; i<num_values; i++ ) {
1770 val = regval_ctr_specific_value( values, i );
1771 len += tdb_pack(buf+len, buflen-len, "fdB",
1775 regval_data_p(val) );
1781 /***********************************************************************
1782 Retrieve an array of strings containing subkeys. Memory should be
1783 released by the caller.
1784 ***********************************************************************/
1786 static int regdb_fetch_values_internal(struct db_context *db, const char* key,
1787 struct regval_ctr *values)
1789 char *keystr = NULL;
1790 TALLOC_CTX *ctx = talloc_stackframe();
1795 DEBUG(10,("regdb_fetch_values: Looking for value of key [%s] \n", key));
1797 if (!regdb_key_exists(db, key)) {
1801 keystr = talloc_asprintf(ctx, "%s\\%s", REG_VALUE_PREFIX, key);
1806 werr = regval_ctr_set_seqnum(values, db->get_seqnum(db));
1807 W_ERROR_NOT_OK_GOTO_DONE(werr);
1809 value = regdb_fetch_key_internal(db, ctx, keystr);
1812 /* all keys have zero values by default */
1816 regdb_unpack_values(values, value.dptr, value.dsize);
1817 ret = regval_ctr_numvals(values);
1824 int regdb_fetch_values(const char* key, struct regval_ctr *values)
1826 return regdb_fetch_values_internal(regdb, key, values);
1829 static bool regdb_store_values_internal(struct db_context *db, const char *key,
1830 struct regval_ctr *values)
1832 TDB_DATA old_data, data;
1833 char *keystr = NULL;
1834 TALLOC_CTX *ctx = talloc_stackframe();
1837 bool result = false;
1839 DEBUG(10,("regdb_store_values: Looking for value of key [%s] \n", key));
1841 if (!regdb_key_exists(db, key)) {
1847 len = regdb_pack_values(values, data.dptr, data.dsize);
1849 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
1853 data.dptr = talloc_array(ctx, uint8, len);
1856 len = regdb_pack_values(values, data.dptr, data.dsize);
1858 SMB_ASSERT( len == data.dsize );
1860 keystr = talloc_asprintf(ctx, "%s\\%s", REG_VALUE_PREFIX, key );
1864 keystr = normalize_reg_path(ctx, keystr);
1869 old_data = dbwrap_fetch_bystring(db, ctx, keystr);
1871 if ((old_data.dptr != NULL)
1872 && (old_data.dsize == data.dsize)
1873 && (memcmp(old_data.dptr, data.dptr, data.dsize) == 0))
1879 status = dbwrap_trans_store_bystring(db, keystr, data, TDB_REPLACE);
1881 result = NT_STATUS_IS_OK(status);
1888 bool regdb_store_values(const char *key, struct regval_ctr *values)
1890 return regdb_store_values_internal(regdb, key, values);
1893 static WERROR regdb_get_secdesc(TALLOC_CTX *mem_ctx, const char *key,
1894 struct security_descriptor **psecdesc)
1899 TALLOC_CTX *tmp_ctx = talloc_stackframe();
1900 WERROR err = WERR_OK;
1902 DEBUG(10, ("regdb_get_secdesc: Getting secdesc of key [%s]\n", key));
1904 if (!regdb_key_exists(regdb, key)) {
1909 tdbkey = talloc_asprintf(tmp_ctx, "%s\\%s", REG_SECDESC_PREFIX, key);
1910 if (tdbkey == NULL) {
1915 tdbkey = normalize_reg_path(tmp_ctx, tdbkey);
1916 if (tdbkey == NULL) {
1921 data = dbwrap_fetch_bystring(regdb, tmp_ctx, tdbkey);
1922 if (data.dptr == NULL) {
1927 status = unmarshall_sec_desc(mem_ctx, (uint8 *)data.dptr, data.dsize,
1930 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MEMORY)) {
1932 } else if (!NT_STATUS_IS_OK(status)) {
1933 err = WERR_REG_CORRUPT;
1937 TALLOC_FREE(tmp_ctx);
1941 static WERROR regdb_set_secdesc(const char *key,
1942 struct security_descriptor *secdesc)
1944 TALLOC_CTX *mem_ctx = talloc_stackframe();
1946 WERROR err = WERR_NOMEM;
1949 if (!regdb_key_exists(regdb, key)) {
1954 tdbkey = talloc_asprintf(mem_ctx, "%s\\%s", REG_SECDESC_PREFIX, key);
1955 if (tdbkey == NULL) {
1959 tdbkey = normalize_reg_path(mem_ctx, tdbkey);
1960 if (tdbkey == NULL) {
1965 if (secdesc == NULL) {
1966 /* assuming a delete */
1967 err = ntstatus_to_werror(dbwrap_trans_delete_bystring(regdb,
1972 err = ntstatus_to_werror(marshall_sec_desc(mem_ctx, secdesc,
1975 W_ERROR_NOT_OK_GOTO_DONE(err);
1977 err = ntstatus_to_werror(dbwrap_trans_store_bystring(regdb, tdbkey,
1981 TALLOC_FREE(mem_ctx);
1985 bool regdb_subkeys_need_update(struct regsubkey_ctr *subkeys)
1987 return (regdb_get_seqnum() != regsubkey_ctr_get_seqnum(subkeys));
1990 bool regdb_values_need_update(struct regval_ctr *values)
1992 return (regdb_get_seqnum() != regval_ctr_get_seqnum(values));
1996 * Table of function pointers for default access
1999 struct registry_ops regdb_ops = {
2000 .fetch_subkeys = regdb_fetch_keys,
2001 .fetch_values = regdb_fetch_values,
2002 .store_subkeys = regdb_store_keys,
2003 .store_values = regdb_store_values,
2004 .create_subkey = regdb_create_subkey,
2005 .delete_subkey = regdb_delete_subkey,
2006 .get_secdesc = regdb_get_secdesc,
2007 .set_secdesc = regdb_set_secdesc,
2008 .subkeys_need_update = regdb_subkeys_need_update,
2009 .values_need_update = regdb_values_need_update