2 * Unix SMB/CIFS implementation.
3 * Virtual Windows Registry Layer
4 * Copyright (C) Gerald Carter 2002-2005
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 /* Implementation of internal registry database functions. */
25 #define DBGC_CLASS DBGC_REGISTRY
27 static struct db_context *regdb = NULL;
28 static int regdb_refcount;
30 static bool regdb_key_exists(struct db_context *db, const char *key);
31 static bool regdb_key_is_base_key(const char *key);
32 static WERROR regdb_fetch_keys_internal(struct db_context *db, const char *key,
33 struct regsubkey_ctr *ctr);
34 static bool regdb_store_keys_internal(struct db_context *db, const char *key,
35 struct regsubkey_ctr *ctr);
36 static int regdb_fetch_values_internal(struct db_context *db, const char* key,
37 struct regval_ctr *values);
38 static bool regdb_store_values_internal(struct db_context *db, const char *key,
39 struct regval_ctr *values);
41 /* List the deepest path into the registry. All part components will be created.*/
43 /* If you want to have a part of the path controlled by the tdb and part by
44 a virtual registry db (e.g. printing), then you have to list the deepest path.
45 For example,"HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion/Print"
46 allows the reg_db backend to handle everything up to
47 "HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion" and then we'll hook
48 the reg_printing backend onto the last component of the path (see
49 KEY_PRINTING_2K in include/rpc_reg.h) --jerry */
51 static const char *builtin_registry_paths[] = {
61 KEY_SAMBA_GROUP_POLICY,
62 KEY_GP_MACHINE_POLICY,
63 KEY_GP_MACHINE_WIN_POLICY,
66 KEY_GP_USER_WIN_POLICY,
67 KEY_WINLOGON_GPEXT_PATH,
68 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Print\\Monitors",
70 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\DefaultUserConfiguration",
79 struct builtin_regkey_value {
81 const char *valuename;
89 static struct builtin_regkey_value builtin_registry_values[] = {
91 SAMBA_PRINTER_PORT_NAME, REG_SZ, { "" } },
93 "DefaultSpoolDirectory", REG_SZ, { "C:\\Windows\\System32\\Spool\\Printers" } },
95 "DisplayName", REG_SZ, { "Event Log" } },
97 "ErrorControl", REG_DWORD, { (char*)0x00000001 } },
98 { NULL, NULL, 0, { NULL } }
102 * Initialize a key in the registry:
103 * create each component key of the specified path.
105 static WERROR init_registry_key_internal(struct db_context *db,
106 const char *add_path)
109 TALLOC_CTX *frame = talloc_stackframe();
112 char *remaining = NULL;
115 struct regsubkey_ctr *subkeys;
118 DEBUG(6, ("init_registry_key: Adding [%s]\n", add_path));
120 path = talloc_strdup(frame, add_path);
121 base = talloc_strdup(frame, "");
122 if (!path || !base) {
128 while (next_token_talloc(frame, &p, &keyname, "\\")) {
130 /* build up the registry path from the components */
133 base = talloc_asprintf(frame, "%s\\", base);
139 base = talloc_asprintf_append(base, "%s", keyname);
145 /* get the immediate subkeyname (if we have one ) */
147 subkeyname = talloc_strdup(frame, "");
153 remaining = talloc_strdup(frame, p);
160 if (!next_token_talloc(frame, &p2,
163 subkeyname = talloc_strdup(frame,p2);
171 DEBUG(10,("init_registry_key: Storing key [%s] with "
172 "subkey [%s]\n", base,
173 *subkeyname ? subkeyname : "NULL"));
175 /* we don't really care if the lookup succeeds or not
176 * since we are about to update the record.
177 * We just want any subkeys already present */
179 werr = regsubkey_ctr_init(frame, &subkeys);
180 if (!W_ERROR_IS_OK(werr)) {
181 DEBUG(0,("talloc() failure!\n"));
185 werr = regdb_fetch_keys_internal(db, base, subkeys);
186 if (!W_ERROR_IS_OK(werr) &&
187 !W_ERROR_EQUAL(werr, WERR_NOT_FOUND))
193 werr = regsubkey_ctr_addkey(subkeys, subkeyname);
194 if (!W_ERROR_IS_OK(werr)) {
198 if (!regdb_store_keys_internal(db, base, subkeys)) {
199 werr = WERR_CAN_NOT_COMPLETE;
211 struct init_registry_key_context {
212 const char *add_path;
215 static NTSTATUS init_registry_key_action(struct db_context *db,
218 struct init_registry_key_context *init_ctx =
219 (struct init_registry_key_context *)private_data;
221 return werror_to_ntstatus(init_registry_key_internal(
222 db, init_ctx->add_path));
226 * Initialize a key in the registry:
227 * create each component key of the specified path,
228 * wrapped in one db transaction.
230 WERROR init_registry_key(const char *add_path)
232 struct init_registry_key_context init_ctx;
234 if (regdb_key_exists(regdb, add_path)) {
238 init_ctx.add_path = add_path;
240 return ntstatus_to_werror(dbwrap_trans_do(regdb,
241 init_registry_key_action,
245 /***********************************************************************
246 Open the registry data in the tdb
247 ***********************************************************************/
249 static void regdb_ctr_add_value(struct regval_ctr *ctr,
250 struct builtin_regkey_value *value)
254 switch(value->type) {
256 regval_ctr_addvalue(ctr, value->valuename, REG_DWORD,
257 (char*)&value->data.dw_value,
262 init_unistr2(&data, value->data.string, UNI_STR_TERMINATE);
263 regval_ctr_addvalue(ctr, value->valuename, REG_SZ,
265 data.uni_str_len*sizeof(uint16));
269 DEBUG(0, ("regdb_ctr_add_value: invalid value type in "
270 "registry values [%d]\n", value->type));
274 static NTSTATUS init_registry_data_action(struct db_context *db,
278 TALLOC_CTX *frame = talloc_stackframe();
279 struct regval_ctr *values;
282 /* loop over all of the predefined paths and add each component */
284 for (i=0; builtin_registry_paths[i] != NULL; i++) {
285 if (regdb_key_exists(db, builtin_registry_paths[i])) {
288 status = werror_to_ntstatus(init_registry_key_internal(db,
289 builtin_registry_paths[i]));
290 if (!NT_STATUS_IS_OK(status)) {
295 /* loop over all of the predefined values and add each component */
297 for (i=0; builtin_registry_values[i].path != NULL; i++) {
299 values = TALLOC_ZERO_P(frame, struct regval_ctr);
300 if (values == NULL) {
301 status = NT_STATUS_NO_MEMORY;
305 regdb_fetch_values_internal(db,
306 builtin_registry_values[i].path,
309 /* preserve existing values across restarts. Only add new ones */
311 if (!regval_ctr_key_exists(values,
312 builtin_registry_values[i].valuename))
314 regdb_ctr_add_value(values,
315 &builtin_registry_values[i]);
316 regdb_store_values_internal(db,
317 builtin_registry_values[i].path,
323 status = NT_STATUS_OK;
331 WERROR init_registry_data(void)
334 TALLOC_CTX *frame = talloc_stackframe();
335 struct regval_ctr *values;
339 * First, check for the existence of the needed keys and values.
340 * If all do already exist, we can save the writes.
342 for (i=0; builtin_registry_paths[i] != NULL; i++) {
343 if (!regdb_key_exists(regdb, builtin_registry_paths[i])) {
348 for (i=0; builtin_registry_values[i].path != NULL; i++) {
349 values = TALLOC_ZERO_P(frame, struct regval_ctr);
350 if (values == NULL) {
355 regdb_fetch_values_internal(regdb,
356 builtin_registry_values[i].path,
358 if (!regval_ctr_key_exists(values,
359 builtin_registry_values[i].valuename))
374 * There are potentially quite a few store operations which are all
375 * indiviually wrapped in tdb transactions. Wrapping them in a single
376 * transaction gives just a single transaction_commit() to actually do
377 * its fsync()s. See tdb/common/transaction.c for info about nested
378 * transaction behaviour.
381 werr = ntstatus_to_werror(dbwrap_trans_do(regdb,
382 init_registry_data_action,
390 /***********************************************************************
391 Open the registry database
392 ***********************************************************************/
394 WERROR regdb_init(void)
396 const char *vstring = "INFO/version";
401 DEBUG(10, ("regdb_init: incrementing refcount (%d)\n",
407 regdb = db_open(NULL, state_path("registry.tdb"), 0,
408 REG_TDB_FLAGS, O_RDWR, 0600);
410 regdb = db_open(NULL, state_path("registry.tdb"), 0,
411 REG_TDB_FLAGS, O_RDWR|O_CREAT, 0600);
413 werr = ntstatus_to_werror(map_nt_error_from_unix(errno));
414 DEBUG(1,("regdb_init: Failed to open registry %s (%s)\n",
415 state_path("registry.tdb"), strerror(errno) ));
419 DEBUG(10,("regdb_init: Successfully created registry tdb\n"));
424 vers_id = dbwrap_fetch_int32(regdb, vstring);
426 if ( vers_id != REGVER_V1 ) {
428 /* any upgrade code here if needed */
429 DEBUG(10, ("regdb_init: got %s = %d != %d\n", vstring,
430 vers_id, REGVER_V1));
431 status = dbwrap_trans_store_int32(regdb, vstring, REGVER_V1);
432 if (!NT_STATUS_IS_OK(status)) {
433 DEBUG(1, ("regdb_init: error storing %s = %d: %s\n",
434 vstring, REGVER_V1, nt_errstr(status)));
435 return ntstatus_to_werror(status);
437 DEBUG(10, ("regdb_init: stored %s = %d\n",
438 vstring, REGVER_V1));
445 /***********************************************************************
446 Open the registry. Must already have been initialized by regdb_init()
447 ***********************************************************************/
449 WERROR regdb_open( void )
451 WERROR result = WERR_OK;
454 DEBUG(10,("regdb_open: incrementing refcount (%d)\n", regdb_refcount));
461 regdb = db_open(NULL, state_path("registry.tdb"), 0,
462 REG_TDB_FLAGS, O_RDWR, 0600);
464 result = ntstatus_to_werror( map_nt_error_from_unix( errno ) );
465 DEBUG(0,("regdb_open: Failed to open %s! (%s)\n",
466 state_path("registry.tdb"), strerror(errno) ));
472 DEBUG(10,("regdb_open: refcount reset (%d)\n", regdb_refcount));
477 /***********************************************************************
478 ***********************************************************************/
480 int regdb_close( void )
482 if (regdb_refcount == 0) {
488 DEBUG(10,("regdb_close: decrementing refcount (%d)\n", regdb_refcount));
490 if ( regdb_refcount > 0 )
493 SMB_ASSERT( regdb_refcount >= 0 );
499 WERROR regdb_transaction_start(void)
501 return (regdb->transaction_start(regdb) == 0) ?
502 WERR_OK : WERR_REG_IO_FAILURE;
505 WERROR regdb_transaction_commit(void)
507 return (regdb->transaction_commit(regdb) == 0) ?
508 WERR_OK : WERR_REG_IO_FAILURE;
511 WERROR regdb_transaction_cancel(void)
513 return (regdb->transaction_cancel(regdb) == 0) ?
514 WERR_OK : WERR_REG_IO_FAILURE;
517 /***********************************************************************
518 return the tdb sequence number of the registry tdb.
519 this is an indicator for the content of the registry
520 having changed. it will change upon regdb_init, too, though.
521 ***********************************************************************/
522 int regdb_get_seqnum(void)
524 return regdb->get_seqnum(regdb);
528 static WERROR regdb_delete_key_with_prefix(struct db_context *db,
533 WERROR werr = WERR_NOMEM;
534 TALLOC_CTX *mem_ctx = talloc_stackframe();
536 if (keyname == NULL) {
537 werr = WERR_INVALID_PARAM;
541 if (prefix == NULL) {
542 path = discard_const_p(char, keyname);
544 path = talloc_asprintf(mem_ctx, "%s/%s", prefix, keyname);
550 path = normalize_reg_path(mem_ctx, path);
555 werr = ntstatus_to_werror(dbwrap_delete_bystring(db, path));
557 /* treat "not" found" as ok */
558 if (W_ERROR_EQUAL(werr, WERR_NOT_FOUND)) {
563 talloc_free(mem_ctx);
568 static WERROR regdb_delete_values(struct db_context *db, const char *keyname)
570 return regdb_delete_key_with_prefix(db, keyname, REG_VALUE_PREFIX);
573 static WERROR regdb_delete_secdesc(struct db_context *db, const char *keyname)
575 return regdb_delete_key_with_prefix(db, keyname, REG_SECDESC_PREFIX);
578 static WERROR regdb_delete_subkeylist(struct db_context *db, const char *keyname)
580 return regdb_delete_key_with_prefix(db, keyname, NULL);
583 static WERROR regdb_delete_key_lists(struct db_context *db, const char *keyname)
587 werr = regdb_delete_values(db, keyname);
588 if (!W_ERROR_IS_OK(werr)) {
589 DEBUG(1, (__location__ " Deleting %s/%s failed: %s\n",
590 REG_VALUE_PREFIX, keyname, win_errstr(werr)));
594 werr = regdb_delete_secdesc(db, keyname);
595 if (!W_ERROR_IS_OK(werr)) {
596 DEBUG(1, (__location__ " Deleting %s/%s failed: %s\n",
597 REG_SECDESC_PREFIX, keyname, win_errstr(werr)));
601 werr = regdb_delete_subkeylist(db, keyname);
602 if (!W_ERROR_IS_OK(werr)) {
603 DEBUG(1, (__location__ " Deleting %s failed: %s\n",
604 keyname, win_errstr(werr)));
612 /***********************************************************************
613 Add subkey strings to the registry tdb under a defined key
614 fmt is the same format as tdb_pack except this function only supports
616 ***********************************************************************/
618 static WERROR regdb_store_keys_internal2(struct db_context *db,
620 struct regsubkey_ctr *ctr)
623 uint8 *buffer = NULL;
626 uint32 num_subkeys = regsubkey_ctr_numkeys(ctr);
627 char *keyname = NULL;
628 TALLOC_CTX *ctx = talloc_stackframe();
632 werr = WERR_INVALID_PARAM;
636 keyname = talloc_strdup(ctx, key);
642 keyname = normalize_reg_path(ctx, keyname);
648 /* allocate some initial memory */
650 buffer = (uint8 *)SMB_MALLOC(1024);
651 if (buffer == NULL) {
658 /* store the number of subkeys */
660 len += tdb_pack(buffer+len, buflen-len, "d", num_subkeys);
662 /* pack all the strings */
664 for (i=0; i<num_subkeys; i++) {
667 thistime = tdb_pack(buffer+len, buflen-len, "f",
668 regsubkey_ctr_specific_key(ctr, i));
669 if (len+thistime > buflen) {
672 * tdb_pack hasn't done anything because of the short
673 * buffer, allocate extra space.
675 buffer = SMB_REALLOC_ARRAY(buffer, uint8_t,
678 DEBUG(0, ("regdb_store_keys: Failed to realloc "
679 "memory of size [%u]\n",
680 (unsigned int)(len+thistime)*2));
684 buflen = (len+thistime)*2;
685 thistime2 = tdb_pack(
686 buffer+len, buflen-len, "f",
687 regsubkey_ctr_specific_key(ctr, i));
688 if (thistime2 != thistime) {
689 DEBUG(0, ("tdb_pack failed\n"));
690 werr = WERR_CAN_NOT_COMPLETE;
697 /* finally write out the data */
701 werr = ntstatus_to_werror(dbwrap_store_bystring(db, keyname, dbuf,
703 W_ERROR_NOT_OK_GOTO_DONE(werr);
706 * Delete a sorted subkey cache for regdb_key_exists, will be
707 * recreated automatically
709 keyname = talloc_asprintf(ctx, "%s/%s", REG_SORTED_SUBKEYS_PREFIX,
711 if (keyname == NULL) {
716 werr = ntstatus_to_werror(dbwrap_delete_bystring(db, keyname));
718 /* don't treat WERR_NOT_FOUND as an error here */
719 if (W_ERROR_EQUAL(werr, WERR_NOT_FOUND)) {
729 /***********************************************************************
730 Store the new subkey record and create any child key records that
731 do not currently exist
732 ***********************************************************************/
734 struct regdb_store_keys_context {
736 struct regsubkey_ctr *ctr;
739 static NTSTATUS regdb_store_keys_action(struct db_context *db,
742 struct regdb_store_keys_context *store_ctx;
746 struct regsubkey_ctr *subkeys = NULL, *old_subkeys = NULL;
747 char *oldkeyname = NULL;
748 TALLOC_CTX *mem_ctx = talloc_stackframe();
750 store_ctx = (struct regdb_store_keys_context *)private_data;
753 * Re-fetch the old keys inside the transaction
756 werr = regsubkey_ctr_init(mem_ctx, &old_subkeys);
757 W_ERROR_NOT_OK_GOTO_DONE(werr);
759 werr = regdb_fetch_keys_internal(db, store_ctx->key, old_subkeys);
760 if (!W_ERROR_IS_OK(werr) &&
761 !W_ERROR_EQUAL(werr, WERR_NOT_FOUND))
767 * Make the store operation as safe as possible without transactions:
769 * (1) For each subkey removed from ctr compared with old_subkeys:
771 * (a) First delete the value db entry.
773 * (b) Next delete the secdesc db record.
775 * (c) Then delete the subkey list entry.
777 * (2) Now write the list of subkeys of the parent key,
778 * deleting removed entries and adding new ones.
780 * (3) Finally create the subkey list entries for the added keys.
782 * This way if we crash half-way in between deleting the subkeys
783 * and storing the parent's list of subkeys, no old data can pop up
784 * out of the blue when re-adding keys later on.
787 /* (1) delete removed keys' lists (values/secdesc/subkeys) */
789 num_subkeys = regsubkey_ctr_numkeys(old_subkeys);
790 for (i=0; i<num_subkeys; i++) {
791 oldkeyname = regsubkey_ctr_specific_key(old_subkeys, i);
793 if (regsubkey_ctr_key_exists(store_ctx->ctr, oldkeyname)) {
795 * It's still around, don't delete
800 path = talloc_asprintf(mem_ctx, "%s/%s", store_ctx->key,
807 werr = regdb_delete_key_lists(db, path);
808 W_ERROR_NOT_OK_GOTO_DONE(werr);
813 TALLOC_FREE(old_subkeys);
815 /* (2) store the subkey list for the parent */
817 werr = regdb_store_keys_internal2(db, store_ctx->key, store_ctx->ctr);
818 if (!W_ERROR_IS_OK(werr)) {
819 DEBUG(0,("regdb_store_keys: Failed to store new subkey list "
820 "for parent [%s]: %s\n", store_ctx->key,
825 /* (3) now create records for any subkeys that don't already exist */
827 num_subkeys = regsubkey_ctr_numkeys(store_ctx->ctr);
829 if (num_subkeys == 0) {
830 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
831 W_ERROR_NOT_OK_GOTO_DONE(werr);
833 werr = regdb_store_keys_internal2(db, store_ctx->key, subkeys);
834 if (!W_ERROR_IS_OK(werr)) {
835 DEBUG(0,("regdb_store_keys: Failed to store "
836 "new record for key [%s]: %s\n",
837 store_ctx->key, win_errstr(werr)));
840 TALLOC_FREE(subkeys);
843 for (i=0; i<num_subkeys; i++) {
844 path = talloc_asprintf(mem_ctx, "%s/%s", store_ctx->key,
845 regsubkey_ctr_specific_key(store_ctx->ctr, i));
850 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
851 W_ERROR_NOT_OK_GOTO_DONE(werr);
853 werr = regdb_fetch_keys_internal(db, path, subkeys);
854 if (!W_ERROR_IS_OK(werr)) {
855 /* create a record with 0 subkeys */
856 werr = regdb_store_keys_internal2(db, path, subkeys);
857 if (!W_ERROR_IS_OK(werr)) {
858 DEBUG(0,("regdb_store_keys: Failed to store "
859 "new record for key [%s]: %s\n", path,
865 TALLOC_FREE(subkeys);
872 talloc_free(mem_ctx);
873 return werror_to_ntstatus(werr);
876 static bool regdb_store_keys_internal(struct db_context *db, const char *key,
877 struct regsubkey_ctr *ctr)
879 int num_subkeys, old_num_subkeys, i;
880 struct regsubkey_ctr *old_subkeys = NULL;
881 TALLOC_CTX *ctx = talloc_stackframe();
884 struct regdb_store_keys_context store_ctx;
886 if (!regdb_key_is_base_key(key) && !regdb_key_exists(db, key)) {
891 * fetch a list of the old subkeys so we can determine if anything has
895 werr = regsubkey_ctr_init(ctx, &old_subkeys);
896 if (!W_ERROR_IS_OK(werr)) {
897 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
901 werr = regdb_fetch_keys_internal(db, key, old_subkeys);
902 if (!W_ERROR_IS_OK(werr) &&
903 !W_ERROR_EQUAL(werr, WERR_NOT_FOUND))
908 num_subkeys = regsubkey_ctr_numkeys(ctr);
909 old_num_subkeys = regsubkey_ctr_numkeys(old_subkeys);
910 if ((num_subkeys && old_num_subkeys) &&
911 (num_subkeys == old_num_subkeys)) {
913 for (i = 0; i < num_subkeys; i++) {
914 if (strcmp(regsubkey_ctr_specific_key(ctr, i),
915 regsubkey_ctr_specific_key(old_subkeys, i))
921 if (i == num_subkeys) {
923 * Nothing changed, no point to even start a tdb
932 TALLOC_FREE(old_subkeys);
937 werr = ntstatus_to_werror(dbwrap_trans_do(db,
938 regdb_store_keys_action,
941 ret = W_ERROR_IS_OK(werr);
949 bool regdb_store_keys(const char *key, struct regsubkey_ctr *ctr)
951 return regdb_store_keys_internal(regdb, key, ctr);
955 * create a subkey of a given key
958 struct regdb_create_subkey_context {
963 static NTSTATUS regdb_create_subkey_action(struct db_context *db,
967 struct regdb_create_subkey_context *create_ctx;
968 struct regsubkey_ctr *subkeys;
969 TALLOC_CTX *mem_ctx = talloc_stackframe();
971 create_ctx = (struct regdb_create_subkey_context *)private_data;
973 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
974 W_ERROR_NOT_OK_GOTO_DONE(werr);
976 werr = regdb_fetch_keys_internal(db, create_ctx->key, subkeys);
977 W_ERROR_NOT_OK_GOTO_DONE(werr);
979 werr = regsubkey_ctr_addkey(subkeys, create_ctx->subkey);
980 W_ERROR_NOT_OK_GOTO_DONE(werr);
982 werr = regdb_store_keys_internal2(db, create_ctx->key, subkeys);
983 if (!W_ERROR_IS_OK(werr)) {
984 DEBUG(0, (__location__ " failed to store new subkey list for "
985 "parent key %s: %s\n", create_ctx->key,
990 talloc_free(mem_ctx);
991 return werror_to_ntstatus(werr);
994 static WERROR regdb_create_subkey(const char *key, const char *subkey)
997 struct regsubkey_ctr *subkeys;
998 TALLOC_CTX *mem_ctx = talloc_stackframe();
999 struct regdb_create_subkey_context create_ctx;
1001 if (!regdb_key_is_base_key(key) && !regdb_key_exists(regdb, key)) {
1002 werr = WERR_NOT_FOUND;
1006 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
1007 W_ERROR_NOT_OK_GOTO_DONE(werr);
1009 werr = regdb_fetch_keys_internal(regdb, key, subkeys);
1010 W_ERROR_NOT_OK_GOTO_DONE(werr);
1012 if (regsubkey_ctr_key_exists(subkeys, subkey)) {
1017 talloc_free(subkeys);
1019 create_ctx.key = key;
1020 create_ctx.subkey = subkey;
1022 werr = ntstatus_to_werror(dbwrap_trans_do(regdb,
1023 regdb_create_subkey_action,
1027 talloc_free(mem_ctx);
1032 * create a subkey of a given key
1035 struct regdb_delete_subkey_context {
1041 static NTSTATUS regdb_delete_subkey_action(struct db_context *db,
1045 struct regdb_delete_subkey_context *delete_ctx;
1046 struct regsubkey_ctr *subkeys;
1047 TALLOC_CTX *mem_ctx = talloc_stackframe();
1049 delete_ctx = (struct regdb_delete_subkey_context *)private_data;
1051 werr = regdb_delete_key_lists(db, delete_ctx->path);
1052 W_ERROR_NOT_OK_GOTO_DONE(werr);
1054 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
1055 W_ERROR_NOT_OK_GOTO_DONE(werr);
1057 werr = regdb_fetch_keys_internal(db, delete_ctx->key, subkeys);
1058 W_ERROR_NOT_OK_GOTO_DONE(werr);
1060 werr = regsubkey_ctr_delkey(subkeys, delete_ctx->subkey);
1061 W_ERROR_NOT_OK_GOTO_DONE(werr);
1063 werr = regdb_store_keys_internal2(db, delete_ctx->key, subkeys);
1064 if (!W_ERROR_IS_OK(werr)) {
1065 DEBUG(0, (__location__ " failed to store new subkey_list for "
1066 "parent key %s: %s\n", delete_ctx->key,
1071 talloc_free(mem_ctx);
1072 return werror_to_ntstatus(werr);
1075 static WERROR regdb_delete_subkey(const char *key, const char *subkey)
1079 struct regdb_delete_subkey_context delete_ctx;
1080 TALLOC_CTX *mem_ctx = talloc_stackframe();
1082 if (!regdb_key_is_base_key(key) && !regdb_key_exists(regdb, key)) {
1083 werr = WERR_NOT_FOUND;
1087 path = talloc_asprintf(mem_ctx, "%s/%s", key, subkey);
1093 if (!regdb_key_exists(regdb, path)) {
1098 delete_ctx.key = key;
1099 delete_ctx.subkey = subkey;
1100 delete_ctx.path = path;
1102 werr = ntstatus_to_werror(dbwrap_trans_do(regdb,
1103 regdb_delete_subkey_action,
1107 talloc_free(mem_ctx);
1111 static TDB_DATA regdb_fetch_key_internal(struct db_context *db,
1112 TALLOC_CTX *mem_ctx, const char *key)
1117 path = normalize_reg_path(mem_ctx, key);
1119 return make_tdb_data(NULL, 0);
1122 data = dbwrap_fetch_bystring(db, mem_ctx, path);
1130 * check whether a given key name represents a base key,
1131 * i.e one without a subkey separator ('/' or '\').
1133 static bool regdb_key_is_base_key(const char *key)
1135 TALLOC_CTX *mem_ctx = talloc_stackframe();
1143 path = normalize_reg_path(mem_ctx, key);
1145 DEBUG(0, ("out of memory! (talloc failed)\n"));
1149 if (*path == '\0') {
1153 ret = (strrchr(path, '/') == NULL);
1156 TALLOC_FREE(mem_ctx);
1161 * regdb_key_exists() is a very frequent operation. It can be quite
1162 * time-consuming to fully fetch the parent's subkey list, talloc_strdup all
1163 * subkeys and then compare the keyname linearly to all the parent's subkeys.
1165 * The following code tries to make this operation as efficient as possible:
1166 * Per registry key we create a list of subkeys that is very efficient to
1167 * search for existence of a subkey. Its format is:
1169 * 4 bytes num_subkeys
1170 * 4*num_subkey bytes offset into the string array
1171 * then follows a sorted list of subkeys in uppercase
1173 * This record is created by create_sorted_subkeys() on demand if it does not
1174 * exist. scan_parent_subkeys() uses regdb->parse_record to search the sorted
1175 * list, the parsing code and the binary search can be found in
1176 * parent_subkey_scanner. The code uses parse_record() to avoid a memcpy of
1177 * the potentially large subkey record.
1179 * The sorted subkey record is deleted in regdb_store_keys_internal2 and
1180 * recreated on demand.
1183 static int cmp_keynames(const void *p1, const void *p2)
1185 return StrCaseCmp(*((char **)p1), *((char **)p2));
1188 struct create_sorted_subkeys_context {
1190 const char *sorted_keyname;
1193 static NTSTATUS create_sorted_subkeys_action(struct db_context *db,
1196 char **sorted_subkeys;
1197 struct regsubkey_ctr *ctr;
1204 struct create_sorted_subkeys_context *sorted_ctx;
1206 sorted_ctx = (struct create_sorted_subkeys_context *)private_data;
1209 * In this function, we only treat failing of the actual write to
1210 * the db as a real error. All preliminary errors, at a stage when
1211 * nothing has been written to the DB yet are treated as success
1212 * to be committed (as an empty transaction).
1214 * The reason is that this (disposable) call might be nested in other
1215 * transactions. Doing a cancel here would destroy the possibility of
1216 * a transaction_commit for transactions that we might be wrapped in.
1219 status = werror_to_ntstatus(regsubkey_ctr_init(talloc_tos(), &ctr));
1220 if (!NT_STATUS_IS_OK(status)) {
1221 /* don't treat this as an error */
1222 status = NT_STATUS_OK;
1226 status = werror_to_ntstatus(regdb_fetch_keys_internal(db,
1229 if (!NT_STATUS_IS_OK(status)) {
1230 /* don't treat this as an error */
1231 status = NT_STATUS_OK;
1235 num_subkeys = regsubkey_ctr_numkeys(ctr);
1236 sorted_subkeys = talloc_array(ctr, char *, num_subkeys);
1237 if (sorted_subkeys == NULL) {
1238 /* don't treat this as an error */
1242 len = 4 + 4*num_subkeys;
1244 for (i = 0; i < num_subkeys; i++) {
1245 sorted_subkeys[i] = talloc_strdup_upper(sorted_subkeys,
1246 regsubkey_ctr_specific_key(ctr, i));
1247 if (sorted_subkeys[i] == NULL) {
1248 /* don't treat this as an error */
1251 len += strlen(sorted_subkeys[i])+1;
1254 qsort(sorted_subkeys, num_subkeys, sizeof(char *), cmp_keynames);
1256 buf = talloc_array(ctr, char, len);
1258 /* don't treat this as an error */
1261 p = buf + 4 + 4*num_subkeys;
1263 SIVAL(buf, 0, num_subkeys);
1265 for (i=0; i < num_subkeys; i++) {
1266 ptrdiff_t offset = p - buf;
1267 SIVAL(buf, 4 + 4*i, offset);
1268 strlcpy(p, sorted_subkeys[i], len-offset);
1269 p += strlen(sorted_subkeys[i]) + 1;
1272 status = dbwrap_store_bystring(
1273 db, sorted_ctx->sorted_keyname, make_tdb_data((uint8_t *)buf,
1282 static bool create_sorted_subkeys(const char *key, const char *sorted_keyname)
1285 struct create_sorted_subkeys_context sorted_ctx;
1287 sorted_ctx.key = key;
1288 sorted_ctx.sorted_keyname = sorted_keyname;
1290 status = dbwrap_trans_do(regdb,
1291 create_sorted_subkeys_action,
1294 return NT_STATUS_IS_OK(status);
1297 struct scan_subkey_state {
1303 static int parent_subkey_scanner(TDB_DATA key, TDB_DATA data,
1306 struct scan_subkey_state *state =
1307 (struct scan_subkey_state *)private_data;
1308 uint32_t num_subkeys;
1311 if (data.dsize < sizeof(uint32_t)) {
1315 state->scanned = true;
1316 state->found = false;
1318 tdb_unpack(data.dptr, data.dsize, "d", &num_subkeys);
1324 uint32_t idx = (l+u)/2;
1325 char *s = (char *)data.dptr + IVAL(data.dptr, 4 + 4*idx);
1326 int comparison = strcmp(state->name, s);
1328 if (comparison < 0) {
1330 } else if (comparison > 0) {
1333 state->found = true;
1340 static bool scan_parent_subkeys(struct db_context *db, const char *parent,
1345 struct scan_subkey_state state = { 0, };
1346 bool result = false;
1351 path = normalize_reg_path(talloc_tos(), parent);
1356 key = talloc_asprintf(talloc_tos(), "%s/%s",
1357 REG_SORTED_SUBKEYS_PREFIX, path);
1362 state.name = talloc_strdup_upper(talloc_tos(), name);
1363 if (state.name == NULL) {
1366 state.scanned = false;
1368 res = db->parse_record(db, string_term_tdb_data(key),
1369 parent_subkey_scanner, &state);
1371 if (state.scanned) {
1372 result = state.found;
1374 if (!create_sorted_subkeys(path, key)) {
1377 res = db->parse_record(db, string_term_tdb_data(key),
1378 parent_subkey_scanner, &state);
1379 if ((res == 0) && (state.scanned)) {
1380 result = state.found;
1386 TALLOC_FREE(state.name);
1391 * Check for the existence of a key.
1393 * Existence of a key is authoritatively defined by its
1394 * existence in the list of subkeys of its parent key.
1395 * The exeption of this are keys without a parent key,
1396 * i.e. the "base" keys (HKLM, HKCU, ...).
1398 static bool regdb_key_exists(struct db_context *db, const char *key)
1400 TALLOC_CTX *mem_ctx = talloc_stackframe();
1409 path = normalize_reg_path(mem_ctx, key);
1411 DEBUG(0, ("out of memory! (talloc failed)\n"));
1415 if (*path == '\0') {
1419 p = strrchr(path, '/');
1421 /* this is a base key */
1422 value = regdb_fetch_key_internal(db, mem_ctx, path);
1423 ret = (value.dptr != NULL);
1426 ret = scan_parent_subkeys(db, path, p+1);
1430 TALLOC_FREE(mem_ctx);
1435 /***********************************************************************
1436 Retrieve an array of strings containing subkeys. Memory should be
1437 released by the caller.
1438 ***********************************************************************/
1440 static WERROR regdb_fetch_keys_internal(struct db_context *db, const char *key,
1441 struct regsubkey_ctr *ctr)
1449 TALLOC_CTX *frame = talloc_stackframe();
1452 DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key ? key : "NULL"));
1454 frame = talloc_stackframe();
1456 if (!regdb_key_exists(db, key)) {
1457 DEBUG(10, ("key [%s] not found\n", key));
1458 werr = WERR_NOT_FOUND;
1462 werr = regsubkey_ctr_set_seqnum(ctr, db->get_seqnum(db));
1463 W_ERROR_NOT_OK_GOTO_DONE(werr);
1465 value = regdb_fetch_key_internal(db, frame, key);
1467 if (value.dptr == NULL) {
1468 DEBUG(10, ("regdb_fetch_keys: no subkeys found for key [%s]\n",
1474 buflen = value.dsize;
1475 len = tdb_unpack( buf, buflen, "d", &num_items);
1477 for (i=0; i<num_items; i++) {
1478 len += tdb_unpack(buf+len, buflen-len, "f", subkeyname);
1479 werr = regsubkey_ctr_addkey(ctr, subkeyname);
1480 if (!W_ERROR_IS_OK(werr)) {
1481 DEBUG(5, ("regdb_fetch_keys: regsubkey_ctr_addkey "
1482 "failed: %s\n", win_errstr(werr)));
1488 DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items));
1495 int regdb_fetch_keys(const char *key, struct regsubkey_ctr *ctr)
1499 werr = regdb_fetch_keys_internal(regdb, key, ctr);
1500 if (!W_ERROR_IS_OK(werr)) {
1504 return regsubkey_ctr_numkeys(ctr);
1507 /****************************************************************************
1508 Unpack a list of registry values frem the TDB
1509 ***************************************************************************/
1511 static int regdb_unpack_values(struct regval_ctr *values, uint8 *buf, int buflen)
1518 uint32 num_values = 0;
1521 /* loop and unpack the rest of the registry values */
1523 len += tdb_unpack(buf+len, buflen-len, "d", &num_values);
1525 for ( i=0; i<num_values; i++ ) {
1526 /* unpack the next regval */
1531 valuename[0] = '\0';
1532 len += tdb_unpack(buf+len, buflen-len, "fdB",
1538 /* add the new value. Paranoid protective code -- make sure data_p is valid */
1540 if (*valuename && size && data_p) {
1541 regval_ctr_addvalue(values, valuename, type,
1542 (const char *)data_p, size);
1544 SAFE_FREE(data_p); /* 'B' option to tdb_unpack does a malloc() */
1546 DEBUG(8,("specific: [%s], len: %d\n", valuename, size));
1552 /****************************************************************************
1553 Pack all values in all printer keys
1554 ***************************************************************************/
1556 static int regdb_pack_values(struct regval_ctr *values, uint8 *buf, int buflen)
1560 struct regval_blob *val;
1566 num_values = regval_ctr_numvals( values );
1568 /* pack the number of values first */
1570 len += tdb_pack( buf+len, buflen-len, "d", num_values );
1572 /* loop over all values */
1574 for ( i=0; i<num_values; i++ ) {
1575 val = regval_ctr_specific_value( values, i );
1576 len += tdb_pack(buf+len, buflen-len, "fdB",
1580 regval_data_p(val) );
1586 /***********************************************************************
1587 Retrieve an array of strings containing subkeys. Memory should be
1588 released by the caller.
1589 ***********************************************************************/
1591 static int regdb_fetch_values_internal(struct db_context *db, const char* key,
1592 struct regval_ctr *values)
1594 char *keystr = NULL;
1595 TALLOC_CTX *ctx = talloc_stackframe();
1599 DEBUG(10,("regdb_fetch_values: Looking for value of key [%s] \n", key));
1601 if (!regdb_key_exists(db, key)) {
1605 keystr = talloc_asprintf(ctx, "%s/%s", REG_VALUE_PREFIX, key);
1610 values->seqnum = db->get_seqnum(db);
1612 value = regdb_fetch_key_internal(db, ctx, keystr);
1615 /* all keys have zero values by default */
1619 regdb_unpack_values(values, value.dptr, value.dsize);
1620 ret = regval_ctr_numvals(values);
1627 int regdb_fetch_values(const char* key, struct regval_ctr *values)
1629 return regdb_fetch_values_internal(regdb, key, values);
1632 static bool regdb_store_values_internal(struct db_context *db, const char *key,
1633 struct regval_ctr *values)
1635 TDB_DATA old_data, data;
1636 char *keystr = NULL;
1637 TALLOC_CTX *ctx = talloc_stackframe();
1640 bool result = false;
1642 DEBUG(10,("regdb_store_values: Looking for value of key [%s] \n", key));
1644 if (!regdb_key_exists(db, key)) {
1650 len = regdb_pack_values(values, data.dptr, data.dsize);
1652 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
1656 data.dptr = TALLOC_ARRAY(ctx, uint8, len);
1659 len = regdb_pack_values(values, data.dptr, data.dsize);
1661 SMB_ASSERT( len == data.dsize );
1663 keystr = talloc_asprintf(ctx, "%s/%s", REG_VALUE_PREFIX, key );
1667 keystr = normalize_reg_path(ctx, keystr);
1672 old_data = dbwrap_fetch_bystring(db, ctx, keystr);
1674 if ((old_data.dptr != NULL)
1675 && (old_data.dsize == data.dsize)
1676 && (memcmp(old_data.dptr, data.dptr, data.dsize) == 0))
1682 status = dbwrap_trans_store_bystring(db, keystr, data, TDB_REPLACE);
1684 result = NT_STATUS_IS_OK(status);
1691 bool regdb_store_values(const char *key, struct regval_ctr *values)
1693 return regdb_store_values_internal(regdb, key, values);
1696 static WERROR regdb_get_secdesc(TALLOC_CTX *mem_ctx, const char *key,
1697 struct security_descriptor **psecdesc)
1702 TALLOC_CTX *tmp_ctx = talloc_stackframe();
1703 WERROR err = WERR_OK;
1705 DEBUG(10, ("regdb_get_secdesc: Getting secdesc of key [%s]\n", key));
1707 if (!regdb_key_exists(regdb, key)) {
1712 tdbkey = talloc_asprintf(tmp_ctx, "%s/%s", REG_SECDESC_PREFIX, key);
1713 if (tdbkey == NULL) {
1717 normalize_dbkey(tdbkey);
1719 data = dbwrap_fetch_bystring(regdb, tmp_ctx, tdbkey);
1720 if (data.dptr == NULL) {
1725 status = unmarshall_sec_desc(mem_ctx, (uint8 *)data.dptr, data.dsize,
1728 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MEMORY)) {
1730 } else if (!NT_STATUS_IS_OK(status)) {
1731 err = WERR_REG_CORRUPT;
1735 TALLOC_FREE(tmp_ctx);
1739 static WERROR regdb_set_secdesc(const char *key,
1740 struct security_descriptor *secdesc)
1742 TALLOC_CTX *mem_ctx = talloc_stackframe();
1744 WERROR err = WERR_NOMEM;
1747 if (!regdb_key_exists(regdb, key)) {
1752 tdbkey = talloc_asprintf(mem_ctx, "%s/%s", REG_SECDESC_PREFIX, key);
1753 if (tdbkey == NULL) {
1756 normalize_dbkey(tdbkey);
1758 if (secdesc == NULL) {
1759 /* assuming a delete */
1760 err = ntstatus_to_werror(dbwrap_trans_delete_bystring(regdb,
1765 err = ntstatus_to_werror(marshall_sec_desc(mem_ctx, secdesc,
1768 W_ERROR_NOT_OK_GOTO_DONE(err);
1770 err = ntstatus_to_werror(dbwrap_trans_store_bystring(regdb, tdbkey,
1774 TALLOC_FREE(mem_ctx);
1778 bool regdb_subkeys_need_update(struct regsubkey_ctr *subkeys)
1780 return (regdb_get_seqnum() != regsubkey_ctr_get_seqnum(subkeys));
1783 bool regdb_values_need_update(struct regval_ctr *values)
1785 return (regdb_get_seqnum() != values->seqnum);
1789 * Table of function pointers for default access
1792 struct registry_ops regdb_ops = {
1793 .fetch_subkeys = regdb_fetch_keys,
1794 .fetch_values = regdb_fetch_values,
1795 .store_subkeys = regdb_store_keys,
1796 .store_values = regdb_store_values,
1797 .create_subkey = regdb_create_subkey,
1798 .delete_subkey = regdb_delete_subkey,
1799 .get_secdesc = regdb_get_secdesc,
1800 .set_secdesc = regdb_set_secdesc,
1801 .subkeys_need_update = regdb_subkeys_need_update,
1802 .values_need_update = regdb_values_need_update