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 int regdb_fetch_keys_internal(struct db_context *db, const char *key,
33 struct regsubkey_ctr *ctr);
35 /* List the deepest path into the registry. All part components will be created.*/
37 /* If you want to have a part of the path controlled by the tdb and part by
38 a virtual registry db (e.g. printing), then you have to list the deepest path.
39 For example,"HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion/Print"
40 allows the reg_db backend to handle everything up to
41 "HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion" and then we'll hook
42 the reg_printing backend onto the last component of the path (see
43 KEY_PRINTING_2K in include/rpc_reg.h) --jerry */
45 static const char *builtin_registry_paths[] = {
55 KEY_SAMBA_GROUP_POLICY,
56 KEY_GP_MACHINE_POLICY,
57 KEY_GP_MACHINE_WIN_POLICY,
60 KEY_GP_USER_WIN_POLICY,
61 KEY_WINLOGON_GPEXT_PATH,
62 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Print\\Monitors",
64 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\DefaultUserConfiguration",
73 struct builtin_regkey_value {
75 const char *valuename;
83 static struct builtin_regkey_value builtin_registry_values[] = {
85 SAMBA_PRINTER_PORT_NAME, REG_SZ, { "" } },
87 "DefaultSpoolDirectory", REG_SZ, { "C:\\Windows\\System32\\Spool\\Printers" } },
89 "DisplayName", REG_SZ, { "Event Log" } },
91 "ErrorControl", REG_DWORD, { (char*)0x00000001 } },
92 { NULL, NULL, 0, { NULL } }
96 * Initialize a key in the registry:
97 * create each component key of the specified path.
99 static WERROR init_registry_key_internal(const char *add_path)
102 TALLOC_CTX *frame = talloc_stackframe();
105 char *remaining = NULL;
108 struct regsubkey_ctr *subkeys;
111 DEBUG(6, ("init_registry_key: Adding [%s]\n", add_path));
113 path = talloc_strdup(frame, add_path);
114 base = talloc_strdup(frame, "");
115 if (!path || !base) {
121 while (next_token_talloc(frame, &p, &keyname, "\\")) {
123 /* build up the registry path from the components */
126 base = talloc_asprintf(frame, "%s\\", base);
132 base = talloc_asprintf_append(base, "%s", keyname);
138 /* get the immediate subkeyname (if we have one ) */
140 subkeyname = talloc_strdup(frame, "");
146 remaining = talloc_strdup(frame, p);
153 if (!next_token_talloc(frame, &p2,
156 subkeyname = talloc_strdup(frame,p2);
164 DEBUG(10,("init_registry_key: Storing key [%s] with "
165 "subkey [%s]\n", base,
166 *subkeyname ? subkeyname : "NULL"));
168 /* we don't really care if the lookup succeeds or not
169 * since we are about to update the record.
170 * We just want any subkeys already present */
172 werr = regsubkey_ctr_init(frame, &subkeys);
173 if (!W_ERROR_IS_OK(werr)) {
174 DEBUG(0,("talloc() failure!\n"));
178 regdb_fetch_keys_internal(regdb, base, subkeys);
180 werr = regsubkey_ctr_addkey(subkeys, subkeyname);
181 if (!W_ERROR_IS_OK(werr)) {
185 if (!regdb_store_keys( base, subkeys)) {
186 werr = WERR_CAN_NOT_COMPLETE;
199 * Initialize a key in the registry:
200 * create each component key of the specified path,
201 * wrapped in one db transaction.
203 WERROR init_registry_key(const char *add_path)
207 if (regdb_key_exists(regdb, add_path)) {
211 if (regdb->transaction_start(regdb) != 0) {
212 DEBUG(0, ("init_registry_key: transaction_start failed\n"));
213 return WERR_REG_IO_FAILURE;
216 werr = init_registry_key_internal(add_path);
217 if (!W_ERROR_IS_OK(werr)) {
221 if (regdb->transaction_commit(regdb) != 0) {
222 DEBUG(0, ("init_registry_key: Could not commit transaction\n"));
223 return WERR_REG_IO_FAILURE;
229 if (regdb->transaction_cancel(regdb) != 0) {
230 smb_panic("init_registry_key: transaction_cancel failed\n");
236 /***********************************************************************
237 Open the registry data in the tdb
238 ***********************************************************************/
240 WERROR init_registry_data(void)
243 TALLOC_CTX *frame = talloc_stackframe();
244 struct regval_ctr *values;
249 * First, check for the existence of the needed keys and values.
250 * If all do already exist, we can save the writes.
252 for (i=0; builtin_registry_paths[i] != NULL; i++) {
253 if (!regdb_key_exists(regdb, builtin_registry_paths[i])) {
258 for (i=0; builtin_registry_values[i].path != NULL; i++) {
259 values = TALLOC_ZERO_P(frame, struct regval_ctr);
260 if (values == NULL) {
265 regdb_fetch_values(builtin_registry_values[i].path, values);
266 if (!regval_ctr_key_exists(values,
267 builtin_registry_values[i].valuename))
282 * There are potentially quite a few store operations which are all
283 * indiviually wrapped in tdb transactions. Wrapping them in a single
284 * transaction gives just a single transaction_commit() to actually do
285 * its fsync()s. See tdb/common/transaction.c for info about nested
286 * transaction behaviour.
289 if (regdb->transaction_start(regdb) != 0) {
290 DEBUG(0, ("init_registry_data: tdb_transaction_start "
292 werr = WERR_REG_IO_FAILURE;
296 /* loop over all of the predefined paths and add each component */
298 for (i=0; builtin_registry_paths[i] != NULL; i++) {
299 if (regdb_key_exists(regdb, builtin_registry_paths[i])) {
302 werr = init_registry_key_internal(builtin_registry_paths[i]);
303 if (!W_ERROR_IS_OK(werr)) {
308 /* loop over all of the predefined values and add each component */
310 for (i=0; builtin_registry_values[i].path != NULL; i++) {
312 values = TALLOC_ZERO_P(frame, struct regval_ctr);
313 if (values == NULL) {
318 regdb_fetch_values(builtin_registry_values[i].path, values);
320 /* preserve existing values across restarts. Only add new ones */
322 if (!regval_ctr_key_exists(values,
323 builtin_registry_values[i].valuename))
325 switch(builtin_registry_values[i].type) {
327 regval_ctr_addvalue(values,
328 builtin_registry_values[i].valuename,
330 (char*)&builtin_registry_values[i].data.dw_value,
336 builtin_registry_values[i].data.string,
338 regval_ctr_addvalue(values,
339 builtin_registry_values[i].valuename,
342 data.uni_str_len*sizeof(uint16));
346 DEBUG(0, ("init_registry_data: invalid value "
347 "type in builtin_registry_values "
349 builtin_registry_values[i].type));
351 regdb_store_values(builtin_registry_values[i].path,
357 if (regdb->transaction_commit(regdb) != 0) {
358 DEBUG(0, ("init_registry_data: Could not commit "
360 werr = WERR_REG_IO_FAILURE;
368 if (regdb->transaction_cancel(regdb) != 0) {
369 smb_panic("init_registry_data: tdb_transaction_cancel "
378 /***********************************************************************
379 Open the registry database
380 ***********************************************************************/
382 WERROR regdb_init(void)
384 const char *vstring = "INFO/version";
389 DEBUG(10, ("regdb_init: incrementing refcount (%d)\n",
395 regdb = db_open(NULL, state_path("registry.tdb"), 0,
396 REG_TDB_FLAGS, O_RDWR, 0600);
398 regdb = db_open(NULL, state_path("registry.tdb"), 0,
399 REG_TDB_FLAGS, O_RDWR|O_CREAT, 0600);
401 werr = ntstatus_to_werror(map_nt_error_from_unix(errno));
402 DEBUG(1,("regdb_init: Failed to open registry %s (%s)\n",
403 state_path("registry.tdb"), strerror(errno) ));
407 DEBUG(10,("regdb_init: Successfully created registry tdb\n"));
412 vers_id = dbwrap_fetch_int32(regdb, vstring);
414 if ( vers_id != REGVER_V1 ) {
416 /* any upgrade code here if needed */
417 DEBUG(10, ("regdb_init: got %s = %d != %d\n", vstring,
418 vers_id, REGVER_V1));
419 status = dbwrap_trans_store_int32(regdb, vstring, REGVER_V1);
420 if (!NT_STATUS_IS_OK(status)) {
421 DEBUG(1, ("regdb_init: error storing %s = %d: %s\n",
422 vstring, REGVER_V1, nt_errstr(status)));
423 return ntstatus_to_werror(status);
425 DEBUG(10, ("regdb_init: stored %s = %d\n",
426 vstring, REGVER_V1));
433 /***********************************************************************
434 Open the registry. Must already have been initialized by regdb_init()
435 ***********************************************************************/
437 WERROR regdb_open( void )
439 WERROR result = WERR_OK;
442 DEBUG(10,("regdb_open: incrementing refcount (%d)\n", regdb_refcount));
449 regdb = db_open(NULL, state_path("registry.tdb"), 0,
450 REG_TDB_FLAGS, O_RDWR, 0600);
452 result = ntstatus_to_werror( map_nt_error_from_unix( errno ) );
453 DEBUG(0,("regdb_open: Failed to open %s! (%s)\n",
454 state_path("registry.tdb"), strerror(errno) ));
460 DEBUG(10,("regdb_open: refcount reset (%d)\n", regdb_refcount));
465 /***********************************************************************
466 ***********************************************************************/
468 int regdb_close( void )
470 if (regdb_refcount == 0) {
476 DEBUG(10,("regdb_close: decrementing refcount (%d)\n", regdb_refcount));
478 if ( regdb_refcount > 0 )
481 SMB_ASSERT( regdb_refcount >= 0 );
487 WERROR regdb_transaction_start(void)
489 return (regdb->transaction_start(regdb) == 0) ?
490 WERR_OK : WERR_REG_IO_FAILURE;
493 WERROR regdb_transaction_commit(void)
495 return (regdb->transaction_commit(regdb) == 0) ?
496 WERR_OK : WERR_REG_IO_FAILURE;
499 WERROR regdb_transaction_cancel(void)
501 return (regdb->transaction_cancel(regdb) == 0) ?
502 WERR_OK : WERR_REG_IO_FAILURE;
505 /***********************************************************************
506 return the tdb sequence number of the registry tdb.
507 this is an indicator for the content of the registry
508 having changed. it will change upon regdb_init, too, though.
509 ***********************************************************************/
510 int regdb_get_seqnum(void)
512 return regdb->get_seqnum(regdb);
516 static WERROR regdb_delete_key_with_prefix(struct db_context *db,
521 WERROR werr = WERR_NOMEM;
522 TALLOC_CTX *mem_ctx = talloc_stackframe();
524 if (keyname == NULL) {
525 werr = WERR_INVALID_PARAM;
529 if (prefix == NULL) {
530 path = discard_const_p(char, keyname);
532 path = talloc_asprintf(mem_ctx, "%s/%s", prefix, keyname);
538 path = normalize_reg_path(mem_ctx, path);
543 werr = ntstatus_to_werror(dbwrap_delete_bystring(db, path));
545 /* treat "not" found" as ok */
546 if (W_ERROR_EQUAL(werr, WERR_NOT_FOUND)) {
551 talloc_free(mem_ctx);
556 static WERROR regdb_delete_values(struct db_context *db, const char *keyname)
558 return regdb_delete_key_with_prefix(db, keyname, REG_VALUE_PREFIX);
561 static WERROR regdb_delete_secdesc(struct db_context *db, const char *keyname)
563 return regdb_delete_key_with_prefix(db, keyname, REG_SECDESC_PREFIX);
566 static WERROR regdb_delete_subkeylist(struct db_context *db, const char *keyname)
568 return regdb_delete_key_with_prefix(db, keyname, NULL);
571 static WERROR regdb_delete_key_lists(struct db_context *db, const char *keyname)
575 werr = regdb_delete_values(db, keyname);
576 if (!W_ERROR_IS_OK(werr)) {
577 DEBUG(1, (__location__ " Deleting %s/%s failed: %s\n",
578 REG_VALUE_PREFIX, keyname, win_errstr(werr)));
582 werr = regdb_delete_secdesc(db, keyname);
583 if (!W_ERROR_IS_OK(werr)) {
584 DEBUG(1, (__location__ " Deleting %s/%s failed: %s\n",
585 REG_SECDESC_PREFIX, keyname, win_errstr(werr)));
589 werr = regdb_delete_subkeylist(db, keyname);
590 if (!W_ERROR_IS_OK(werr)) {
591 DEBUG(1, (__location__ " Deleting %s failed: %s\n",
592 keyname, win_errstr(werr)));
600 /***********************************************************************
601 Add subkey strings to the registry tdb under a defined key
602 fmt is the same format as tdb_pack except this function only supports
604 ***********************************************************************/
606 static bool regdb_store_keys_internal2(struct db_context *db,
608 struct regsubkey_ctr *ctr)
611 uint8 *buffer = NULL;
615 uint32 num_subkeys = regsubkey_ctr_numkeys(ctr);
616 char *keyname = NULL;
617 TALLOC_CTX *ctx = talloc_stackframe();
624 keyname = talloc_strdup(ctx, key);
628 keyname = normalize_reg_path(ctx, keyname);
630 /* allocate some initial memory */
632 buffer = (uint8 *)SMB_MALLOC(1024);
633 if (buffer == NULL) {
639 /* store the number of subkeys */
641 len += tdb_pack(buffer+len, buflen-len, "d", num_subkeys);
643 /* pack all the strings */
645 for (i=0; i<num_subkeys; i++) {
648 thistime = tdb_pack(buffer+len, buflen-len, "f",
649 regsubkey_ctr_specific_key(ctr, i));
650 if (len+thistime > buflen) {
653 * tdb_pack hasn't done anything because of the short
654 * buffer, allocate extra space.
656 buffer = SMB_REALLOC_ARRAY(buffer, uint8_t,
659 DEBUG(0, ("regdb_store_keys: Failed to realloc "
660 "memory of size [%u]\n",
661 (unsigned int)(len+thistime)*2));
665 buflen = (len+thistime)*2;
666 thistime2 = tdb_pack(
667 buffer+len, buflen-len, "f",
668 regsubkey_ctr_specific_key(ctr, i));
669 if (thistime2 != thistime) {
670 DEBUG(0, ("tdb_pack failed\n"));
678 /* finally write out the data */
682 status = dbwrap_store_bystring(db, keyname, dbuf, TDB_REPLACE);
683 if (!NT_STATUS_IS_OK(status)) {
689 * Delete a sorted subkey cache for regdb_key_exists, will be
690 * recreated automatically
692 keyname = talloc_asprintf(ctx, "%s/%s", REG_SORTED_SUBKEYS_PREFIX,
694 if (keyname != NULL) {
695 dbwrap_delete_bystring(db, keyname);
704 /***********************************************************************
705 Store the new subkey record and create any child key records that
706 do not currently exist
707 ***********************************************************************/
709 bool regdb_store_keys(const char *key, struct regsubkey_ctr *ctr)
711 int num_subkeys, old_num_subkeys, i;
713 struct regsubkey_ctr *subkeys = NULL, *old_subkeys = NULL;
714 char *oldkeyname = NULL;
715 TALLOC_CTX *ctx = talloc_stackframe();
718 if (!regdb_key_is_base_key(key) && !regdb_key_exists(regdb, key)) {
723 * fetch a list of the old subkeys so we can determine if anything has
727 werr = regsubkey_ctr_init(ctx, &old_subkeys);
728 if (!W_ERROR_IS_OK(werr)) {
729 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
733 regdb_fetch_keys_internal(regdb, key, old_subkeys);
735 num_subkeys = regsubkey_ctr_numkeys(ctr);
736 old_num_subkeys = regsubkey_ctr_numkeys(old_subkeys);
737 if ((num_subkeys && old_num_subkeys) &&
738 (num_subkeys == old_num_subkeys)) {
740 for (i = 0; i < num_subkeys; i++) {
741 if (strcmp(regsubkey_ctr_specific_key(ctr, i),
742 regsubkey_ctr_specific_key(old_subkeys, i))
748 if (i == num_subkeys) {
750 * Nothing changed, no point to even start a tdb
753 TALLOC_FREE(old_subkeys);
758 TALLOC_FREE(old_subkeys);
760 if (regdb->transaction_start(regdb) != 0) {
761 DEBUG(0, ("regdb_store_keys: transaction_start failed\n"));
766 * Re-fetch the old keys inside the transaction
769 werr = regsubkey_ctr_init(ctx, &old_subkeys);
770 if (!W_ERROR_IS_OK(werr)) {
771 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
775 regdb_fetch_keys_internal(regdb, key, old_subkeys);
778 * Make the store operation as safe as possible without transactions:
780 * (1) For each subkey removed from ctr compared with old_subkeys:
782 * (a) First delete the value db entry.
784 * (b) Next delete the secdesc db record.
786 * (c) Then delete the subkey list entry.
788 * (2) Now write the list of subkeys of the parent key,
789 * deleting removed entries and adding new ones.
791 * (3) Finally create the subkey list entries for the added keys.
793 * This way if we crash half-way in between deleting the subkeys
794 * and storing the parent's list of subkeys, no old data can pop up
795 * out of the blue when re-adding keys later on.
798 /* (1) delete removed keys' lists (values/secdesc/subkeys) */
800 num_subkeys = regsubkey_ctr_numkeys(old_subkeys);
801 for (i=0; i<num_subkeys; i++) {
802 oldkeyname = regsubkey_ctr_specific_key(old_subkeys, i);
804 if (regsubkey_ctr_key_exists(ctr, oldkeyname)) {
806 * It's still around, don't delete
812 path = talloc_asprintf(ctx, "%s/%s", key, oldkeyname);
817 werr = regdb_delete_key_lists(regdb, path);
818 W_ERROR_NOT_OK_GOTO(werr, cancel);
823 TALLOC_FREE(old_subkeys);
825 /* (2) store the subkey list for the parent */
827 if (!regdb_store_keys_internal2(regdb, key, ctr)) {
828 DEBUG(0,("regdb_store_keys: Failed to store new subkey list "
829 "for parent [%s]\n", key));
833 /* (3) now create records for any subkeys that don't already exist */
835 num_subkeys = regsubkey_ctr_numkeys(ctr);
837 if (num_subkeys == 0) {
838 werr = regsubkey_ctr_init(ctx, &subkeys);
839 if (!W_ERROR_IS_OK(werr)) {
840 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
844 if (!regdb_store_keys_internal2(regdb, key, subkeys)) {
845 DEBUG(0,("regdb_store_keys: Failed to store "
846 "new record for key [%s]\n", key));
849 TALLOC_FREE(subkeys);
853 for (i=0; i<num_subkeys; i++) {
854 path = talloc_asprintf(ctx, "%s/%s",
856 regsubkey_ctr_specific_key(ctr, i));
860 werr = regsubkey_ctr_init(ctx, &subkeys);
861 if (!W_ERROR_IS_OK(werr)) {
862 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
866 if (regdb_fetch_keys_internal(regdb, path, subkeys) == -1) {
867 /* create a record with 0 subkeys */
868 if (!regdb_store_keys_internal2(regdb, path, subkeys)) {
869 DEBUG(0,("regdb_store_keys: Failed to store "
870 "new record for key [%s]\n", path));
875 TALLOC_FREE(subkeys);
879 if (regdb->transaction_commit(regdb) != 0) {
880 DEBUG(0, ("regdb_store_keys: Could not commit transaction\n"));
888 if (regdb->transaction_cancel(regdb) != 0) {
889 smb_panic("regdb_store_keys: transaction_cancel failed\n");
898 static WERROR regdb_create_subkey(const char *key, const char *subkey)
901 struct regsubkey_ctr *subkeys;
902 TALLOC_CTX *mem_ctx = talloc_stackframe();
904 if (!regdb_key_is_base_key(key) && !regdb_key_exists(regdb, key)) {
905 werr = WERR_NOT_FOUND;
909 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
910 W_ERROR_NOT_OK_GOTO_DONE(werr);
912 if (regdb_fetch_keys_internal(regdb, key, subkeys) < 0) {
913 werr = WERR_REG_IO_FAILURE;
917 if (regsubkey_ctr_key_exists(subkeys, subkey)) {
922 talloc_free(subkeys);
924 werr = regdb_transaction_start();
925 W_ERROR_NOT_OK_GOTO_DONE(werr);
927 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
928 W_ERROR_NOT_OK_GOTO(werr, cancel);
930 if (regdb_fetch_keys_internal(regdb, key, subkeys) < 0) {
931 werr = WERR_REG_IO_FAILURE;
935 werr = regsubkey_ctr_addkey(subkeys, subkey);
936 W_ERROR_NOT_OK_GOTO(werr, cancel);
938 if (!regdb_store_keys_internal2(regdb, key, subkeys)) {
939 DEBUG(0, (__location__ " failed to store new subkey list for "
940 "parent key %s\n", key));
941 werr = WERR_REG_IO_FAILURE;
945 werr = regdb_transaction_commit();
946 if (!W_ERROR_IS_OK(werr)) {
947 DEBUG(0, (__location__ " failed to commit transaction: %s\n",
954 werr = regdb_transaction_cancel();
955 if (!W_ERROR_IS_OK(werr)) {
956 DEBUG(0, (__location__ " failed to cancel transaction: %s\n",
961 talloc_free(mem_ctx);
965 static WERROR regdb_delete_subkey(const char *key, const char *subkey)
968 struct regsubkey_ctr *subkeys;
970 TALLOC_CTX *mem_ctx = talloc_stackframe();
972 if (!regdb_key_is_base_key(key) && !regdb_key_exists(regdb, key)) {
973 werr = WERR_NOT_FOUND;
977 path = talloc_asprintf(mem_ctx, "%s/%s", key, subkey);
983 if (!regdb_key_exists(regdb, path)) {
988 werr = regdb_transaction_start();
989 W_ERROR_NOT_OK_GOTO_DONE(werr);
991 werr = regdb_delete_key_lists(regdb, path);
992 W_ERROR_NOT_OK_GOTO(werr, cancel);
994 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
995 W_ERROR_NOT_OK_GOTO(werr, cancel);
997 if (regdb_fetch_keys_internal(regdb, key, subkeys) < 0) {
998 werr = WERR_REG_IO_FAILURE;
1002 werr = regsubkey_ctr_delkey(subkeys, subkey);
1003 W_ERROR_NOT_OK_GOTO(werr, cancel);
1005 if (!regdb_store_keys_internal2(regdb, key, subkeys)) {
1006 DEBUG(0, (__location__ " failed to store new subkey_list for "
1007 "parent key %s\n", key));
1008 werr = WERR_REG_IO_FAILURE;
1012 werr = regdb_transaction_commit();
1013 if (!W_ERROR_IS_OK(werr)) {
1014 DEBUG(0, (__location__ " failed to commit transaction: %s\n",
1021 werr2 = regdb_transaction_cancel();
1022 if (!W_ERROR_IS_OK(werr2)) {
1023 DEBUG(0, (__location__ " failed to cancel transaction: %s\n",
1024 win_errstr(werr2)));
1028 talloc_free(mem_ctx);
1032 static TDB_DATA regdb_fetch_key_internal(struct db_context *db,
1033 TALLOC_CTX *mem_ctx, const char *key)
1038 path = normalize_reg_path(mem_ctx, key);
1040 return make_tdb_data(NULL, 0);
1043 data = dbwrap_fetch_bystring(db, mem_ctx, path);
1051 * check whether a given key name represents a base key,
1052 * i.e one without a subkey separator ('/' or '\').
1054 static bool regdb_key_is_base_key(const char *key)
1056 TALLOC_CTX *mem_ctx = talloc_stackframe();
1064 path = normalize_reg_path(mem_ctx, key);
1066 DEBUG(0, ("out of memory! (talloc failed)\n"));
1070 if (*path == '\0') {
1074 ret = (strrchr(path, '/') == NULL);
1077 TALLOC_FREE(mem_ctx);
1082 * regdb_key_exists() is a very frequent operation. It can be quite
1083 * time-consuming to fully fetch the parent's subkey list, talloc_strdup all
1084 * subkeys and then compare the keyname linearly to all the parent's subkeys.
1086 * The following code tries to make this operation as efficient as possible:
1087 * Per registry key we create a list of subkeys that is very efficient to
1088 * search for existence of a subkey. Its format is:
1090 * 4 bytes num_subkeys
1091 * 4*num_subkey bytes offset into the string array
1092 * then follows a sorted list of subkeys in uppercase
1094 * This record is created by create_sorted_subkeys() on demand if it does not
1095 * exist. scan_parent_subkeys() uses regdb->parse_record to search the sorted
1096 * list, the parsing code and the binary search can be found in
1097 * parent_subkey_scanner. The code uses parse_record() to avoid a memcpy of
1098 * the potentially large subkey record.
1100 * The sorted subkey record is deleted in regdb_store_keys_internal2 and
1101 * recreated on demand.
1104 static int cmp_keynames(const void *p1, const void *p2)
1106 return StrCaseCmp(*((char **)p1), *((char **)p2));
1109 static bool create_sorted_subkeys(const char *key, const char *sorted_keyname)
1111 char **sorted_subkeys;
1112 struct regsubkey_ctr *ctr;
1113 bool result = false;
1122 if (regdb->transaction_start(regdb) != 0) {
1123 DEBUG(0, ("create_sorted_subkeys: transaction_start "
1128 werr = regsubkey_ctr_init(talloc_tos(), &ctr);
1129 if (!W_ERROR_IS_OK(werr)) {
1133 res = regdb_fetch_keys_internal(regdb, key, ctr);
1138 num_subkeys = regsubkey_ctr_numkeys(ctr);
1139 sorted_subkeys = talloc_array(ctr, char *, num_subkeys);
1140 if (sorted_subkeys == NULL) {
1144 len = 4 + 4*num_subkeys;
1146 for (i = 0; i < num_subkeys; i++) {
1147 sorted_subkeys[i] = talloc_strdup_upper(sorted_subkeys,
1148 regsubkey_ctr_specific_key(ctr, i));
1149 if (sorted_subkeys[i] == NULL) {
1152 len += strlen(sorted_subkeys[i])+1;
1155 qsort(sorted_subkeys, num_subkeys, sizeof(char *), cmp_keynames);
1157 buf = talloc_array(ctr, char, len);
1161 p = buf + 4 + 4*num_subkeys;
1163 SIVAL(buf, 0, num_subkeys);
1165 for (i=0; i < num_subkeys; i++) {
1166 ptrdiff_t offset = p - buf;
1167 SIVAL(buf, 4 + 4*i, offset);
1168 strlcpy(p, sorted_subkeys[i], len-offset);
1169 p += strlen(sorted_subkeys[i]) + 1;
1172 status = dbwrap_store_bystring(
1173 regdb, sorted_keyname, make_tdb_data((uint8_t *)buf, len),
1175 if (!NT_STATUS_IS_OK(status)) {
1177 * Don't use a "goto fail;" here, this would commit the broken
1178 * transaction. See below for an explanation.
1180 if (regdb->transaction_cancel(regdb) == -1) {
1181 DEBUG(0, ("create_sorted_subkeys: transaction_cancel "
1191 * We only get here via the "goto fail" when we did not write anything
1192 * yet. Using transaction_commit even in a failure case is necessary
1193 * because this (disposable) call might be nested in other
1194 * transactions. Doing a cancel here would destroy the possibility of
1195 * a transaction_commit for transactions that we might be wrapped in.
1197 if (regdb->transaction_commit(regdb) == -1) {
1198 DEBUG(0, ("create_sorted_subkeys: transaction_start "
1207 struct scan_subkey_state {
1213 static int parent_subkey_scanner(TDB_DATA key, TDB_DATA data,
1216 struct scan_subkey_state *state =
1217 (struct scan_subkey_state *)private_data;
1218 uint32_t num_subkeys;
1221 if (data.dsize < sizeof(uint32_t)) {
1225 state->scanned = true;
1226 state->found = false;
1228 tdb_unpack(data.dptr, data.dsize, "d", &num_subkeys);
1234 uint32_t idx = (l+u)/2;
1235 char *s = (char *)data.dptr + IVAL(data.dptr, 4 + 4*idx);
1236 int comparison = strcmp(state->name, s);
1238 if (comparison < 0) {
1240 } else if (comparison > 0) {
1243 state->found = true;
1250 static bool scan_parent_subkeys(struct db_context *db, const char *parent,
1255 struct scan_subkey_state state = { 0, };
1256 bool result = false;
1261 path = normalize_reg_path(talloc_tos(), parent);
1266 key = talloc_asprintf(talloc_tos(), "%s/%s",
1267 REG_SORTED_SUBKEYS_PREFIX, path);
1272 state.name = talloc_strdup_upper(talloc_tos(), name);
1273 if (state.name == NULL) {
1276 state.scanned = false;
1278 res = db->parse_record(db, string_term_tdb_data(key),
1279 parent_subkey_scanner, &state);
1281 if (state.scanned) {
1282 result = state.found;
1284 if (!create_sorted_subkeys(path, key)) {
1287 res = db->parse_record(db, string_term_tdb_data(key),
1288 parent_subkey_scanner, &state);
1289 if ((res == 0) && (state.scanned)) {
1290 result = state.found;
1296 TALLOC_FREE(state.name);
1301 * Check for the existence of a key.
1303 * Existence of a key is authoritatively defined by its
1304 * existence in the list of subkeys of its parent key.
1305 * The exeption of this are keys without a parent key,
1306 * i.e. the "base" keys (HKLM, HKCU, ...).
1308 static bool regdb_key_exists(struct db_context *db, const char *key)
1310 TALLOC_CTX *mem_ctx = talloc_stackframe();
1319 path = normalize_reg_path(mem_ctx, key);
1321 DEBUG(0, ("out of memory! (talloc failed)\n"));
1325 if (*path == '\0') {
1329 p = strrchr(path, '/');
1331 /* this is a base key */
1332 value = regdb_fetch_key_internal(db, mem_ctx, path);
1333 ret = (value.dptr != NULL);
1336 ret = scan_parent_subkeys(db, path, p+1);
1340 TALLOC_FREE(mem_ctx);
1345 /***********************************************************************
1346 Retrieve an array of strings containing subkeys. Memory should be
1347 released by the caller.
1348 ***********************************************************************/
1350 static int regdb_fetch_keys_internal(struct db_context *db, const char *key,
1351 struct regsubkey_ctr *ctr)
1360 TALLOC_CTX *frame = talloc_stackframe();
1363 DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key ? key : "NULL"));
1365 if (!regdb_key_exists(db, key)) {
1369 werr = regsubkey_ctr_set_seqnum(ctr, db->get_seqnum(db));
1370 if (!W_ERROR_IS_OK(werr)) {
1374 value = regdb_fetch_key_internal(db, frame, key);
1376 if (value.dptr == NULL) {
1377 DEBUG(10, ("regdb_fetch_keys: no subkeys found for key [%s]\n",
1384 buflen = value.dsize;
1385 len = tdb_unpack( buf, buflen, "d", &num_items);
1387 for (i=0; i<num_items; i++) {
1388 len += tdb_unpack(buf+len, buflen-len, "f", subkeyname);
1389 werr = regsubkey_ctr_addkey(ctr, subkeyname);
1390 if (!W_ERROR_IS_OK(werr)) {
1391 DEBUG(5, ("regdb_fetch_keys: regsubkey_ctr_addkey "
1392 "failed: %s\n", win_errstr(werr)));
1397 DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items));
1405 int regdb_fetch_keys(const char *key, struct regsubkey_ctr *ctr)
1407 return regdb_fetch_keys_internal(regdb, key, ctr);
1410 /****************************************************************************
1411 Unpack a list of registry values frem the TDB
1412 ***************************************************************************/
1414 static int regdb_unpack_values(struct regval_ctr *values, uint8 *buf, int buflen)
1421 uint32 num_values = 0;
1424 /* loop and unpack the rest of the registry values */
1426 len += tdb_unpack(buf+len, buflen-len, "d", &num_values);
1428 for ( i=0; i<num_values; i++ ) {
1429 /* unpack the next regval */
1434 valuename[0] = '\0';
1435 len += tdb_unpack(buf+len, buflen-len, "fdB",
1441 /* add the new value. Paranoid protective code -- make sure data_p is valid */
1443 if (*valuename && size && data_p) {
1444 regval_ctr_addvalue(values, valuename, type,
1445 (const char *)data_p, size);
1447 SAFE_FREE(data_p); /* 'B' option to tdb_unpack does a malloc() */
1449 DEBUG(8,("specific: [%s], len: %d\n", valuename, size));
1455 /****************************************************************************
1456 Pack all values in all printer keys
1457 ***************************************************************************/
1459 static int regdb_pack_values(struct regval_ctr *values, uint8 *buf, int buflen)
1463 struct regval_blob *val;
1469 num_values = regval_ctr_numvals( values );
1471 /* pack the number of values first */
1473 len += tdb_pack( buf+len, buflen-len, "d", num_values );
1475 /* loop over all values */
1477 for ( i=0; i<num_values; i++ ) {
1478 val = regval_ctr_specific_value( values, i );
1479 len += tdb_pack(buf+len, buflen-len, "fdB",
1483 regval_data_p(val) );
1489 /***********************************************************************
1490 Retrieve an array of strings containing subkeys. Memory should be
1491 released by the caller.
1492 ***********************************************************************/
1494 int regdb_fetch_values(const char* key, struct regval_ctr *values)
1496 char *keystr = NULL;
1497 TALLOC_CTX *ctx = talloc_stackframe();
1501 DEBUG(10,("regdb_fetch_values: Looking for value of key [%s] \n", key));
1503 if (!regdb_key_exists(regdb, key)) {
1507 keystr = talloc_asprintf(ctx, "%s/%s", REG_VALUE_PREFIX, key);
1512 values->seqnum = regdb_get_seqnum();
1514 value = regdb_fetch_key_internal(regdb, ctx, keystr);
1517 /* all keys have zero values by default */
1521 regdb_unpack_values(values, value.dptr, value.dsize);
1522 ret = regval_ctr_numvals(values);
1529 bool regdb_store_values(const char *key, struct regval_ctr *values)
1531 TDB_DATA old_data, data;
1532 char *keystr = NULL;
1533 TALLOC_CTX *ctx = talloc_stackframe();
1536 bool result = false;
1538 DEBUG(10,("regdb_store_values: Looking for value of key [%s] \n", key));
1540 if (!regdb_key_exists(regdb, key)) {
1546 len = regdb_pack_values(values, data.dptr, data.dsize);
1548 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
1552 data.dptr = TALLOC_ARRAY(ctx, uint8, len);
1555 len = regdb_pack_values(values, data.dptr, data.dsize);
1557 SMB_ASSERT( len == data.dsize );
1559 keystr = talloc_asprintf(ctx, "%s/%s", REG_VALUE_PREFIX, key );
1563 keystr = normalize_reg_path(ctx, keystr);
1568 old_data = dbwrap_fetch_bystring(regdb, ctx, keystr);
1570 if ((old_data.dptr != NULL)
1571 && (old_data.dsize == data.dsize)
1572 && (memcmp(old_data.dptr, data.dptr, data.dsize) == 0))
1578 status = dbwrap_trans_store_bystring(regdb, keystr, data, TDB_REPLACE);
1580 result = NT_STATUS_IS_OK(status);
1587 static WERROR regdb_get_secdesc(TALLOC_CTX *mem_ctx, const char *key,
1588 struct security_descriptor **psecdesc)
1593 TALLOC_CTX *tmp_ctx = talloc_stackframe();
1594 WERROR err = WERR_OK;
1596 DEBUG(10, ("regdb_get_secdesc: Getting secdesc of key [%s]\n", key));
1598 if (!regdb_key_exists(regdb, key)) {
1603 tdbkey = talloc_asprintf(tmp_ctx, "%s/%s", REG_SECDESC_PREFIX, key);
1604 if (tdbkey == NULL) {
1608 normalize_dbkey(tdbkey);
1610 data = dbwrap_fetch_bystring(regdb, tmp_ctx, tdbkey);
1611 if (data.dptr == NULL) {
1616 status = unmarshall_sec_desc(mem_ctx, (uint8 *)data.dptr, data.dsize,
1619 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MEMORY)) {
1621 } else if (!NT_STATUS_IS_OK(status)) {
1622 err = WERR_REG_CORRUPT;
1626 TALLOC_FREE(tmp_ctx);
1630 static WERROR regdb_set_secdesc(const char *key,
1631 struct security_descriptor *secdesc)
1633 TALLOC_CTX *mem_ctx = talloc_stackframe();
1635 WERROR err = WERR_NOMEM;
1638 if (!regdb_key_exists(regdb, key)) {
1643 tdbkey = talloc_asprintf(mem_ctx, "%s/%s", REG_SECDESC_PREFIX, key);
1644 if (tdbkey == NULL) {
1647 normalize_dbkey(tdbkey);
1649 if (secdesc == NULL) {
1650 /* assuming a delete */
1651 err = ntstatus_to_werror(dbwrap_trans_delete_bystring(regdb,
1656 err = ntstatus_to_werror(marshall_sec_desc(mem_ctx, secdesc,
1659 W_ERROR_NOT_OK_GOTO_DONE(err);
1661 err = ntstatus_to_werror(dbwrap_trans_store_bystring(regdb, tdbkey,
1665 TALLOC_FREE(mem_ctx);
1669 bool regdb_subkeys_need_update(struct regsubkey_ctr *subkeys)
1671 return (regdb_get_seqnum() != regsubkey_ctr_get_seqnum(subkeys));
1674 bool regdb_values_need_update(struct regval_ctr *values)
1676 return (regdb_get_seqnum() != values->seqnum);
1680 * Table of function pointers for default access
1683 struct registry_ops regdb_ops = {
1684 .fetch_subkeys = regdb_fetch_keys,
1685 .fetch_values = regdb_fetch_values,
1686 .store_subkeys = regdb_store_keys,
1687 .store_values = regdb_store_values,
1688 .create_subkey = regdb_create_subkey,
1689 .delete_subkey = regdb_delete_subkey,
1690 .get_secdesc = regdb_get_secdesc,
1691 .set_secdesc = regdb_set_secdesc,
1692 .subkeys_need_update = regdb_subkeys_need_update,
1693 .values_need_update = regdb_values_need_update