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(const char *key);
31 static bool regdb_key_is_base_key(const char *key);
33 /* List the deepest path into the registry. All part components will be created.*/
35 /* If you want to have a part of the path controlled by the tdb and part by
36 a virtual registry db (e.g. printing), then you have to list the deepest path.
37 For example,"HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion/Print"
38 allows the reg_db backend to handle everything up to
39 "HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion" and then we'll hook
40 the reg_printing backend onto the last component of the path (see
41 KEY_PRINTING_2K in include/rpc_reg.h) --jerry */
43 static const char *builtin_registry_paths[] = {
53 KEY_SAMBA_GROUP_POLICY,
54 KEY_GP_MACHINE_POLICY,
55 KEY_GP_MACHINE_WIN_POLICY,
58 KEY_GP_USER_WIN_POLICY,
59 KEY_WINLOGON_GPEXT_PATH,
60 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Print\\Monitors",
62 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\DefaultUserConfiguration",
71 struct builtin_regkey_value {
73 const char *valuename;
81 static struct builtin_regkey_value builtin_registry_values[] = {
83 SAMBA_PRINTER_PORT_NAME, REG_SZ, { "" } },
85 "DefaultSpoolDirectory", REG_SZ, { "C:\\Windows\\System32\\Spool\\Printers" } },
87 "DisplayName", REG_SZ, { "Event Log" } },
89 "ErrorControl", REG_DWORD, { (char*)0x00000001 } },
90 { NULL, NULL, 0, { NULL } }
94 * Initialize a key in the registry:
95 * create each component key of the specified path.
97 static WERROR init_registry_key_internal(const char *add_path)
100 TALLOC_CTX *frame = talloc_stackframe();
103 char *remaining = NULL;
106 struct regsubkey_ctr *subkeys;
109 DEBUG(6, ("init_registry_key: Adding [%s]\n", add_path));
111 path = talloc_strdup(frame, add_path);
112 base = talloc_strdup(frame, "");
113 if (!path || !base) {
119 while (next_token_talloc(frame, &p, &keyname, "\\")) {
121 /* build up the registry path from the components */
124 base = talloc_asprintf(frame, "%s\\", base);
130 base = talloc_asprintf_append(base, "%s", keyname);
136 /* get the immediate subkeyname (if we have one ) */
138 subkeyname = talloc_strdup(frame, "");
144 remaining = talloc_strdup(frame, p);
151 if (!next_token_talloc(frame, &p2,
154 subkeyname = talloc_strdup(frame,p2);
162 DEBUG(10,("init_registry_key: Storing key [%s] with "
163 "subkey [%s]\n", base,
164 *subkeyname ? subkeyname : "NULL"));
166 /* we don't really care if the lookup succeeds or not
167 * since we are about to update the record.
168 * We just want any subkeys already present */
170 werr = regsubkey_ctr_init(frame, &subkeys);
171 if (!W_ERROR_IS_OK(werr)) {
172 DEBUG(0,("talloc() failure!\n"));
176 regdb_fetch_keys(base, subkeys);
178 werr = regsubkey_ctr_addkey(subkeys, subkeyname);
179 if (!W_ERROR_IS_OK(werr)) {
183 if (!regdb_store_keys( base, subkeys)) {
184 werr = WERR_CAN_NOT_COMPLETE;
197 * Initialize a key in the registry:
198 * create each component key of the specified path,
199 * wrapped in one db transaction.
201 WERROR init_registry_key(const char *add_path)
205 if (regdb_key_exists(add_path)) {
209 if (regdb->transaction_start(regdb) != 0) {
210 DEBUG(0, ("init_registry_key: transaction_start failed\n"));
211 return WERR_REG_IO_FAILURE;
214 werr = init_registry_key_internal(add_path);
215 if (!W_ERROR_IS_OK(werr)) {
219 if (regdb->transaction_commit(regdb) != 0) {
220 DEBUG(0, ("init_registry_key: Could not commit transaction\n"));
221 return WERR_REG_IO_FAILURE;
227 if (regdb->transaction_cancel(regdb) != 0) {
228 smb_panic("init_registry_key: transaction_cancel failed\n");
234 /***********************************************************************
235 Open the registry data in the tdb
236 ***********************************************************************/
238 WERROR init_registry_data(void)
241 TALLOC_CTX *frame = talloc_stackframe();
242 struct regval_ctr *values;
247 * First, check for the existence of the needed keys and values.
248 * If all do already exist, we can save the writes.
250 for (i=0; builtin_registry_paths[i] != NULL; i++) {
251 if (!regdb_key_exists(builtin_registry_paths[i])) {
256 for (i=0; builtin_registry_values[i].path != NULL; i++) {
257 values = TALLOC_ZERO_P(frame, struct regval_ctr);
258 if (values == NULL) {
263 regdb_fetch_values(builtin_registry_values[i].path, values);
264 if (!regval_ctr_key_exists(values,
265 builtin_registry_values[i].valuename))
280 * There are potentially quite a few store operations which are all
281 * indiviually wrapped in tdb transactions. Wrapping them in a single
282 * transaction gives just a single transaction_commit() to actually do
283 * its fsync()s. See tdb/common/transaction.c for info about nested
284 * transaction behaviour.
287 if (regdb->transaction_start(regdb) != 0) {
288 DEBUG(0, ("init_registry_data: tdb_transaction_start "
290 werr = WERR_REG_IO_FAILURE;
294 /* loop over all of the predefined paths and add each component */
296 for (i=0; builtin_registry_paths[i] != NULL; i++) {
297 if (regdb_key_exists(builtin_registry_paths[i])) {
300 werr = init_registry_key_internal(builtin_registry_paths[i]);
301 if (!W_ERROR_IS_OK(werr)) {
306 /* loop over all of the predefined values and add each component */
308 for (i=0; builtin_registry_values[i].path != NULL; i++) {
310 values = TALLOC_ZERO_P(frame, struct regval_ctr);
311 if (values == NULL) {
316 regdb_fetch_values(builtin_registry_values[i].path, values);
318 /* preserve existing values across restarts. Only add new ones */
320 if (!regval_ctr_key_exists(values,
321 builtin_registry_values[i].valuename))
323 switch(builtin_registry_values[i].type) {
325 regval_ctr_addvalue(values,
326 builtin_registry_values[i].valuename,
328 (char*)&builtin_registry_values[i].data.dw_value,
334 builtin_registry_values[i].data.string,
336 regval_ctr_addvalue(values,
337 builtin_registry_values[i].valuename,
340 data.uni_str_len*sizeof(uint16));
344 DEBUG(0, ("init_registry_data: invalid value "
345 "type in builtin_registry_values "
347 builtin_registry_values[i].type));
349 regdb_store_values(builtin_registry_values[i].path,
355 if (regdb->transaction_commit(regdb) != 0) {
356 DEBUG(0, ("init_registry_data: Could not commit "
358 werr = WERR_REG_IO_FAILURE;
366 if (regdb->transaction_cancel(regdb) != 0) {
367 smb_panic("init_registry_data: tdb_transaction_cancel "
376 /***********************************************************************
377 Open the registry database
378 ***********************************************************************/
380 WERROR regdb_init(void)
382 const char *vstring = "INFO/version";
387 DEBUG(10, ("regdb_init: incrementing refcount (%d)\n",
393 regdb = db_open(NULL, state_path("registry.tdb"), 0,
394 REG_TDB_FLAGS, O_RDWR, 0600);
396 regdb = db_open(NULL, state_path("registry.tdb"), 0,
397 REG_TDB_FLAGS, O_RDWR|O_CREAT, 0600);
399 werr = ntstatus_to_werror(map_nt_error_from_unix(errno));
400 DEBUG(1,("regdb_init: Failed to open registry %s (%s)\n",
401 state_path("registry.tdb"), strerror(errno) ));
405 DEBUG(10,("regdb_init: Successfully created registry tdb\n"));
410 vers_id = dbwrap_fetch_int32(regdb, vstring);
412 if ( vers_id != REGVER_V1 ) {
414 /* any upgrade code here if needed */
415 DEBUG(10, ("regdb_init: got %s = %d != %d\n", vstring,
416 vers_id, REGVER_V1));
417 status = dbwrap_trans_store_int32(regdb, vstring, REGVER_V1);
418 if (!NT_STATUS_IS_OK(status)) {
419 DEBUG(1, ("regdb_init: error storing %s = %d: %s\n",
420 vstring, REGVER_V1, nt_errstr(status)));
421 return ntstatus_to_werror(status);
423 DEBUG(10, ("regdb_init: stored %s = %d\n",
424 vstring, REGVER_V1));
431 /***********************************************************************
432 Open the registry. Must already have been initialized by regdb_init()
433 ***********************************************************************/
435 WERROR regdb_open( void )
437 WERROR result = WERR_OK;
440 DEBUG(10,("regdb_open: incrementing refcount (%d)\n", regdb_refcount));
447 regdb = db_open(NULL, state_path("registry.tdb"), 0,
448 REG_TDB_FLAGS, O_RDWR, 0600);
450 result = ntstatus_to_werror( map_nt_error_from_unix( errno ) );
451 DEBUG(0,("regdb_open: Failed to open %s! (%s)\n",
452 state_path("registry.tdb"), strerror(errno) ));
458 DEBUG(10,("regdb_open: refcount reset (%d)\n", regdb_refcount));
463 /***********************************************************************
464 ***********************************************************************/
466 int regdb_close( void )
468 if (regdb_refcount == 0) {
474 DEBUG(10,("regdb_close: decrementing refcount (%d)\n", regdb_refcount));
476 if ( regdb_refcount > 0 )
479 SMB_ASSERT( regdb_refcount >= 0 );
485 WERROR regdb_transaction_start(void)
487 return (regdb->transaction_start(regdb) == 0) ?
488 WERR_OK : WERR_REG_IO_FAILURE;
491 WERROR regdb_transaction_commit(void)
493 return (regdb->transaction_commit(regdb) == 0) ?
494 WERR_OK : WERR_REG_IO_FAILURE;
497 WERROR regdb_transaction_cancel(void)
499 return (regdb->transaction_cancel(regdb) == 0) ?
500 WERR_OK : WERR_REG_IO_FAILURE;
503 /***********************************************************************
504 return the tdb sequence number of the registry tdb.
505 this is an indicator for the content of the registry
506 having changed. it will change upon regdb_init, too, though.
507 ***********************************************************************/
508 int regdb_get_seqnum(void)
510 return regdb->get_seqnum(regdb);
514 static WERROR regdb_delete_key_with_prefix(struct db_context *db,
519 WERROR werr = WERR_NOMEM;
520 TALLOC_CTX *mem_ctx = talloc_stackframe();
522 if (keyname == NULL) {
523 werr = WERR_INVALID_PARAM;
527 if (prefix == NULL) {
528 path = discard_const_p(char, keyname);
530 path = talloc_asprintf(mem_ctx, "%s/%s", prefix, keyname);
536 path = normalize_reg_path(mem_ctx, path);
541 werr = ntstatus_to_werror(dbwrap_delete_bystring(db, path));
543 /* treat "not" found" as ok */
544 if (W_ERROR_EQUAL(werr, WERR_NOT_FOUND)) {
549 talloc_free(mem_ctx);
554 static WERROR regdb_delete_values(struct db_context *db, const char *keyname)
556 return regdb_delete_key_with_prefix(db, keyname, REG_VALUE_PREFIX);
559 static WERROR regdb_delete_secdesc(struct db_context *db, const char *keyname)
561 return regdb_delete_key_with_prefix(db, keyname, REG_SECDESC_PREFIX);
564 static WERROR regdb_delete_subkeylist(struct db_context *db, const char *keyname)
566 return regdb_delete_key_with_prefix(db, keyname, NULL);
569 static WERROR regdb_delete_key_lists(struct db_context *db, const char *keyname)
573 werr = regdb_delete_values(db, keyname);
574 if (!W_ERROR_IS_OK(werr)) {
575 DEBUG(1, (__location__ " Deleting %s/%s failed: %s\n",
576 REG_VALUE_PREFIX, keyname, win_errstr(werr)));
580 werr = regdb_delete_secdesc(db, keyname);
581 if (!W_ERROR_IS_OK(werr)) {
582 DEBUG(1, (__location__ " Deleting %s/%s failed: %s\n",
583 REG_SECDESC_PREFIX, keyname, win_errstr(werr)));
587 werr = regdb_delete_subkeylist(db, keyname);
588 if (!W_ERROR_IS_OK(werr)) {
589 DEBUG(1, (__location__ " Deleting %s failed: %s\n",
590 keyname, win_errstr(werr)));
598 /***********************************************************************
599 Add subkey strings to the registry tdb under a defined key
600 fmt is the same format as tdb_pack except this function only supports
602 ***********************************************************************/
604 static bool regdb_store_keys_internal(struct db_context *db,
606 struct regsubkey_ctr *ctr)
609 uint8 *buffer = NULL;
613 uint32 num_subkeys = regsubkey_ctr_numkeys(ctr);
614 char *keyname = NULL;
615 TALLOC_CTX *ctx = talloc_stackframe();
622 keyname = talloc_strdup(ctx, key);
626 keyname = normalize_reg_path(ctx, keyname);
628 /* allocate some initial memory */
630 buffer = (uint8 *)SMB_MALLOC(1024);
631 if (buffer == NULL) {
637 /* store the number of subkeys */
639 len += tdb_pack(buffer+len, buflen-len, "d", num_subkeys);
641 /* pack all the strings */
643 for (i=0; i<num_subkeys; i++) {
646 thistime = tdb_pack(buffer+len, buflen-len, "f",
647 regsubkey_ctr_specific_key(ctr, i));
648 if (len+thistime > buflen) {
651 * tdb_pack hasn't done anything because of the short
652 * buffer, allocate extra space.
654 buffer = SMB_REALLOC_ARRAY(buffer, uint8_t,
657 DEBUG(0, ("regdb_store_keys: Failed to realloc "
658 "memory of size [%u]\n",
659 (unsigned int)(len+thistime)*2));
663 buflen = (len+thistime)*2;
664 thistime2 = tdb_pack(
665 buffer+len, buflen-len, "f",
666 regsubkey_ctr_specific_key(ctr, i));
667 if (thistime2 != thistime) {
668 DEBUG(0, ("tdb_pack failed\n"));
676 /* finally write out the data */
680 status = dbwrap_store_bystring(db, keyname, dbuf, TDB_REPLACE);
681 if (!NT_STATUS_IS_OK(status)) {
687 * Delete a sorted subkey cache for regdb_key_exists, will be
688 * recreated automatically
690 keyname = talloc_asprintf(ctx, "%s/%s", REG_SORTED_SUBKEYS_PREFIX,
692 if (keyname != NULL) {
693 dbwrap_delete_bystring(db, keyname);
702 /***********************************************************************
703 Store the new subkey record and create any child key records that
704 do not currently exist
705 ***********************************************************************/
707 bool regdb_store_keys(const char *key, struct regsubkey_ctr *ctr)
709 int num_subkeys, old_num_subkeys, i;
711 struct regsubkey_ctr *subkeys = NULL, *old_subkeys = NULL;
712 char *oldkeyname = NULL;
713 TALLOC_CTX *ctx = talloc_stackframe();
716 if (!regdb_key_is_base_key(key) && !regdb_key_exists(key)) {
721 * fetch a list of the old subkeys so we can determine if anything has
725 werr = regsubkey_ctr_init(ctx, &old_subkeys);
726 if (!W_ERROR_IS_OK(werr)) {
727 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
731 regdb_fetch_keys(key, old_subkeys);
733 num_subkeys = regsubkey_ctr_numkeys(ctr);
734 old_num_subkeys = regsubkey_ctr_numkeys(old_subkeys);
735 if ((num_subkeys && old_num_subkeys) &&
736 (num_subkeys == old_num_subkeys)) {
738 for (i = 0; i < num_subkeys; i++) {
739 if (strcmp(regsubkey_ctr_specific_key(ctr, i),
740 regsubkey_ctr_specific_key(old_subkeys, i))
746 if (i == num_subkeys) {
748 * Nothing changed, no point to even start a tdb
751 TALLOC_FREE(old_subkeys);
756 TALLOC_FREE(old_subkeys);
758 if (regdb->transaction_start(regdb) != 0) {
759 DEBUG(0, ("regdb_store_keys: transaction_start failed\n"));
764 * Re-fetch the old keys inside the transaction
767 werr = regsubkey_ctr_init(ctx, &old_subkeys);
768 if (!W_ERROR_IS_OK(werr)) {
769 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
773 regdb_fetch_keys(key, old_subkeys);
776 * Make the store operation as safe as possible without transactions:
778 * (1) For each subkey removed from ctr compared with old_subkeys:
780 * (a) First delete the value db entry.
782 * (b) Next delete the secdesc db record.
784 * (c) Then delete the subkey list entry.
786 * (2) Now write the list of subkeys of the parent key,
787 * deleting removed entries and adding new ones.
789 * (3) Finally create the subkey list entries for the added keys.
791 * This way if we crash half-way in between deleting the subkeys
792 * and storing the parent's list of subkeys, no old data can pop up
793 * out of the blue when re-adding keys later on.
796 /* (1) delete removed keys' lists (values/secdesc/subkeys) */
798 num_subkeys = regsubkey_ctr_numkeys(old_subkeys);
799 for (i=0; i<num_subkeys; i++) {
800 oldkeyname = regsubkey_ctr_specific_key(old_subkeys, i);
802 if (regsubkey_ctr_key_exists(ctr, oldkeyname)) {
804 * It's still around, don't delete
810 path = talloc_asprintf(ctx, "%s/%s", key, oldkeyname);
815 werr = regdb_delete_key_lists(regdb, path);
816 W_ERROR_NOT_OK_GOTO(werr, cancel);
821 TALLOC_FREE(old_subkeys);
823 /* (2) store the subkey list for the parent */
825 if (!regdb_store_keys_internal(regdb, key, ctr)) {
826 DEBUG(0,("regdb_store_keys: Failed to store new subkey list "
827 "for parent [%s]\n", key));
831 /* (3) now create records for any subkeys that don't already exist */
833 num_subkeys = regsubkey_ctr_numkeys(ctr);
835 if (num_subkeys == 0) {
836 werr = regsubkey_ctr_init(ctx, &subkeys);
837 if (!W_ERROR_IS_OK(werr)) {
838 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
842 if (!regdb_store_keys_internal(regdb, key, subkeys)) {
843 DEBUG(0,("regdb_store_keys: Failed to store "
844 "new record for key [%s]\n", key));
847 TALLOC_FREE(subkeys);
851 for (i=0; i<num_subkeys; i++) {
852 path = talloc_asprintf(ctx, "%s/%s",
854 regsubkey_ctr_specific_key(ctr, i));
858 werr = regsubkey_ctr_init(ctx, &subkeys);
859 if (!W_ERROR_IS_OK(werr)) {
860 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
864 if (regdb_fetch_keys( path, subkeys ) == -1) {
865 /* create a record with 0 subkeys */
866 if (!regdb_store_keys_internal(regdb, path, subkeys)) {
867 DEBUG(0,("regdb_store_keys: Failed to store "
868 "new record for key [%s]\n", path));
873 TALLOC_FREE(subkeys);
877 if (regdb->transaction_commit(regdb) != 0) {
878 DEBUG(0, ("regdb_store_keys: Could not commit transaction\n"));
886 if (regdb->transaction_cancel(regdb) != 0) {
887 smb_panic("regdb_store_keys: transaction_cancel failed\n");
896 static WERROR regdb_create_subkey(const char *key, const char *subkey)
899 struct regsubkey_ctr *subkeys;
900 TALLOC_CTX *mem_ctx = talloc_stackframe();
902 if (!regdb_key_is_base_key(key) && !regdb_key_exists(key)) {
903 werr = WERR_NOT_FOUND;
907 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
908 W_ERROR_NOT_OK_GOTO_DONE(werr);
910 if (regdb_fetch_keys(key, subkeys) < 0) {
911 werr = WERR_REG_IO_FAILURE;
915 if (regsubkey_ctr_key_exists(subkeys, subkey)) {
920 talloc_free(subkeys);
922 werr = regdb_transaction_start();
923 W_ERROR_NOT_OK_GOTO_DONE(werr);
925 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
926 W_ERROR_NOT_OK_GOTO(werr, cancel);
928 if (regdb_fetch_keys(key, subkeys) < 0) {
929 werr = WERR_REG_IO_FAILURE;
933 werr = regsubkey_ctr_addkey(subkeys, subkey);
934 W_ERROR_NOT_OK_GOTO(werr, cancel);
936 if (!regdb_store_keys_internal(regdb, key, subkeys)) {
937 DEBUG(0, (__location__ " failed to store new subkey list for "
938 "parent key %s\n", key));
939 werr = WERR_REG_IO_FAILURE;
943 werr = regdb_transaction_commit();
944 if (!W_ERROR_IS_OK(werr)) {
945 DEBUG(0, (__location__ " failed to commit transaction: %s\n",
952 werr = regdb_transaction_cancel();
953 if (!W_ERROR_IS_OK(werr)) {
954 DEBUG(0, (__location__ " failed to cancel transaction: %s\n",
959 talloc_free(mem_ctx);
963 static WERROR regdb_delete_subkey(const char *key, const char *subkey)
966 struct regsubkey_ctr *subkeys;
968 TALLOC_CTX *mem_ctx = talloc_stackframe();
970 if (!regdb_key_is_base_key(key) && !regdb_key_exists(key)) {
971 werr = WERR_NOT_FOUND;
975 path = talloc_asprintf(mem_ctx, "%s/%s", key, subkey);
981 if (!regdb_key_exists(path)) {
986 werr = regdb_transaction_start();
987 W_ERROR_NOT_OK_GOTO_DONE(werr);
989 werr = regdb_delete_key_lists(regdb, path);
990 W_ERROR_NOT_OK_GOTO(werr, cancel);
992 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
993 W_ERROR_NOT_OK_GOTO(werr, cancel);
995 if (regdb_fetch_keys(key, subkeys) < 0) {
996 werr = WERR_REG_IO_FAILURE;
1000 werr = regsubkey_ctr_delkey(subkeys, subkey);
1001 W_ERROR_NOT_OK_GOTO(werr, cancel);
1003 if (!regdb_store_keys_internal(regdb, key, subkeys)) {
1004 DEBUG(0, (__location__ " failed to store new subkey_list for "
1005 "parent key %s\n", key));
1006 werr = WERR_REG_IO_FAILURE;
1010 werr = regdb_transaction_commit();
1011 if (!W_ERROR_IS_OK(werr)) {
1012 DEBUG(0, (__location__ " failed to commit transaction: %s\n",
1019 werr2 = regdb_transaction_cancel();
1020 if (!W_ERROR_IS_OK(werr2)) {
1021 DEBUG(0, (__location__ " failed to cancel transaction: %s\n",
1022 win_errstr(werr2)));
1026 talloc_free(mem_ctx);
1030 static TDB_DATA regdb_fetch_key_internal(TALLOC_CTX *mem_ctx, const char *key)
1035 path = normalize_reg_path(mem_ctx, key);
1037 return make_tdb_data(NULL, 0);
1040 data = dbwrap_fetch_bystring(regdb, mem_ctx, path);
1048 * check whether a given key name represents a base key,
1049 * i.e one without a subkey separator ('/' or '\').
1051 static bool regdb_key_is_base_key(const char *key)
1053 TALLOC_CTX *mem_ctx = talloc_stackframe();
1061 path = normalize_reg_path(mem_ctx, key);
1063 DEBUG(0, ("out of memory! (talloc failed)\n"));
1067 if (*path == '\0') {
1071 ret = (strrchr(path, '/') == NULL);
1074 TALLOC_FREE(mem_ctx);
1079 * regdb_key_exists() is a very frequent operation. It can be quite
1080 * time-consuming to fully fetch the parent's subkey list, talloc_strdup all
1081 * subkeys and then compare the keyname linearly to all the parent's subkeys.
1083 * The following code tries to make this operation as efficient as possible:
1084 * Per registry key we create a list of subkeys that is very efficient to
1085 * search for existence of a subkey. Its format is:
1087 * 4 bytes num_subkeys
1088 * 4*num_subkey bytes offset into the string array
1089 * then follows a sorted list of subkeys in uppercase
1091 * This record is created by create_sorted_subkeys() on demand if it does not
1092 * exist. scan_parent_subkeys() uses regdb->parse_record to search the sorted
1093 * list, the parsing code and the binary search can be found in
1094 * parent_subkey_scanner. The code uses parse_record() to avoid a memcpy of
1095 * the potentially large subkey record.
1097 * The sorted subkey record is deleted in regdb_store_keys_internal and
1098 * recreated on demand.
1101 static int cmp_keynames(const void *p1, const void *p2)
1103 return StrCaseCmp(*((char **)p1), *((char **)p2));
1106 static bool create_sorted_subkeys(const char *key, const char *sorted_keyname)
1108 char **sorted_subkeys;
1109 struct regsubkey_ctr *ctr;
1110 bool result = false;
1119 if (regdb->transaction_start(regdb) != 0) {
1120 DEBUG(0, ("create_sorted_subkeys: transaction_start "
1125 werr = regsubkey_ctr_init(talloc_tos(), &ctr);
1126 if (!W_ERROR_IS_OK(werr)) {
1130 res = regdb_fetch_keys(key, ctr);
1135 num_subkeys = regsubkey_ctr_numkeys(ctr);
1136 sorted_subkeys = talloc_array(ctr, char *, num_subkeys);
1137 if (sorted_subkeys == NULL) {
1141 len = 4 + 4*num_subkeys;
1143 for (i = 0; i < num_subkeys; i++) {
1144 sorted_subkeys[i] = talloc_strdup_upper(sorted_subkeys,
1145 regsubkey_ctr_specific_key(ctr, i));
1146 if (sorted_subkeys[i] == NULL) {
1149 len += strlen(sorted_subkeys[i])+1;
1152 qsort(sorted_subkeys, num_subkeys, sizeof(char *), cmp_keynames);
1154 buf = talloc_array(ctr, char, len);
1158 p = buf + 4 + 4*num_subkeys;
1160 SIVAL(buf, 0, num_subkeys);
1162 for (i=0; i < num_subkeys; i++) {
1163 ptrdiff_t offset = p - buf;
1164 SIVAL(buf, 4 + 4*i, offset);
1165 strlcpy(p, sorted_subkeys[i], len-offset);
1166 p += strlen(sorted_subkeys[i]) + 1;
1169 status = dbwrap_store_bystring(
1170 regdb, sorted_keyname, make_tdb_data((uint8_t *)buf, len),
1172 if (!NT_STATUS_IS_OK(status)) {
1174 * Don't use a "goto fail;" here, this would commit the broken
1175 * transaction. See below for an explanation.
1177 if (regdb->transaction_cancel(regdb) == -1) {
1178 DEBUG(0, ("create_sorted_subkeys: transaction_cancel "
1188 * We only get here via the "goto fail" when we did not write anything
1189 * yet. Using transaction_commit even in a failure case is necessary
1190 * because this (disposable) call might be nested in other
1191 * transactions. Doing a cancel here would destroy the possibility of
1192 * a transaction_commit for transactions that we might be wrapped in.
1194 if (regdb->transaction_commit(regdb) == -1) {
1195 DEBUG(0, ("create_sorted_subkeys: transaction_start "
1204 struct scan_subkey_state {
1210 static int parent_subkey_scanner(TDB_DATA key, TDB_DATA data,
1213 struct scan_subkey_state *state =
1214 (struct scan_subkey_state *)private_data;
1215 uint32_t num_subkeys;
1218 if (data.dsize < sizeof(uint32_t)) {
1222 state->scanned = true;
1223 state->found = false;
1225 tdb_unpack(data.dptr, data.dsize, "d", &num_subkeys);
1231 uint32_t idx = (l+u)/2;
1232 char *s = (char *)data.dptr + IVAL(data.dptr, 4 + 4*idx);
1233 int comparison = strcmp(state->name, s);
1235 if (comparison < 0) {
1237 } else if (comparison > 0) {
1240 state->found = true;
1247 static bool scan_parent_subkeys(const char *parent, const char *name)
1251 struct scan_subkey_state state = { 0, };
1252 bool result = false;
1257 path = normalize_reg_path(talloc_tos(), parent);
1262 key = talloc_asprintf(talloc_tos(), "%s/%s",
1263 REG_SORTED_SUBKEYS_PREFIX, path);
1268 state.name = talloc_strdup_upper(talloc_tos(), name);
1269 if (state.name == NULL) {
1272 state.scanned = false;
1274 res = regdb->parse_record(regdb, string_term_tdb_data(key),
1275 parent_subkey_scanner, &state);
1277 if (state.scanned) {
1278 result = state.found;
1280 if (!create_sorted_subkeys(path, key)) {
1283 res = regdb->parse_record(regdb, string_term_tdb_data(key),
1284 parent_subkey_scanner, &state);
1285 if ((res == 0) && (state.scanned)) {
1286 result = state.found;
1292 TALLOC_FREE(state.name);
1297 * Check for the existence of a key.
1299 * Existence of a key is authoritatively defined by its
1300 * existence in the list of subkeys of its parent key.
1301 * The exeption of this are keys without a parent key,
1302 * i.e. the "base" keys (HKLM, HKCU, ...).
1304 static bool regdb_key_exists(const char *key)
1306 TALLOC_CTX *mem_ctx = talloc_stackframe();
1315 path = normalize_reg_path(mem_ctx, key);
1317 DEBUG(0, ("out of memory! (talloc failed)\n"));
1321 if (*path == '\0') {
1325 p = strrchr(path, '/');
1327 /* this is a base key */
1328 value = regdb_fetch_key_internal(mem_ctx, path);
1329 ret = (value.dptr != NULL);
1332 ret = scan_parent_subkeys(path, p+1);
1336 TALLOC_FREE(mem_ctx);
1341 /***********************************************************************
1342 Retrieve an array of strings containing subkeys. Memory should be
1343 released by the caller.
1344 ***********************************************************************/
1346 int regdb_fetch_keys(const char *key, struct regsubkey_ctr *ctr)
1355 TALLOC_CTX *frame = talloc_stackframe();
1358 DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key ? key : "NULL"));
1360 if (!regdb_key_exists(key)) {
1364 werr = regsubkey_ctr_set_seqnum(ctr, regdb_get_seqnum());
1365 if (!W_ERROR_IS_OK(werr)) {
1369 value = regdb_fetch_key_internal(frame, key);
1371 if (value.dptr == NULL) {
1372 DEBUG(10, ("regdb_fetch_keys: no subkeys found for key [%s]\n",
1379 buflen = value.dsize;
1380 len = tdb_unpack( buf, buflen, "d", &num_items);
1382 for (i=0; i<num_items; i++) {
1383 len += tdb_unpack(buf+len, buflen-len, "f", subkeyname);
1384 werr = regsubkey_ctr_addkey(ctr, subkeyname);
1385 if (!W_ERROR_IS_OK(werr)) {
1386 DEBUG(5, ("regdb_fetch_keys: regsubkey_ctr_addkey "
1387 "failed: %s\n", win_errstr(werr)));
1392 DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items));
1400 /****************************************************************************
1401 Unpack a list of registry values frem the TDB
1402 ***************************************************************************/
1404 static int regdb_unpack_values(struct regval_ctr *values, uint8 *buf, int buflen)
1411 uint32 num_values = 0;
1414 /* loop and unpack the rest of the registry values */
1416 len += tdb_unpack(buf+len, buflen-len, "d", &num_values);
1418 for ( i=0; i<num_values; i++ ) {
1419 /* unpack the next regval */
1424 valuename[0] = '\0';
1425 len += tdb_unpack(buf+len, buflen-len, "fdB",
1431 /* add the new value. Paranoid protective code -- make sure data_p is valid */
1433 if (*valuename && size && data_p) {
1434 regval_ctr_addvalue(values, valuename, type,
1435 (const char *)data_p, size);
1437 SAFE_FREE(data_p); /* 'B' option to tdb_unpack does a malloc() */
1439 DEBUG(8,("specific: [%s], len: %d\n", valuename, size));
1445 /****************************************************************************
1446 Pack all values in all printer keys
1447 ***************************************************************************/
1449 static int regdb_pack_values(struct regval_ctr *values, uint8 *buf, int buflen)
1453 struct regval_blob *val;
1459 num_values = regval_ctr_numvals( values );
1461 /* pack the number of values first */
1463 len += tdb_pack( buf+len, buflen-len, "d", num_values );
1465 /* loop over all values */
1467 for ( i=0; i<num_values; i++ ) {
1468 val = regval_ctr_specific_value( values, i );
1469 len += tdb_pack(buf+len, buflen-len, "fdB",
1473 regval_data_p(val) );
1479 /***********************************************************************
1480 Retrieve an array of strings containing subkeys. Memory should be
1481 released by the caller.
1482 ***********************************************************************/
1484 int regdb_fetch_values(const char* key, struct regval_ctr *values)
1486 char *keystr = NULL;
1487 TALLOC_CTX *ctx = talloc_stackframe();
1491 DEBUG(10,("regdb_fetch_values: Looking for value of key [%s] \n", key));
1493 if (!regdb_key_exists(key)) {
1497 keystr = talloc_asprintf(ctx, "%s/%s", REG_VALUE_PREFIX, key);
1502 values->seqnum = regdb_get_seqnum();
1504 value = regdb_fetch_key_internal(ctx, keystr);
1507 /* all keys have zero values by default */
1511 regdb_unpack_values(values, value.dptr, value.dsize);
1512 ret = regval_ctr_numvals(values);
1519 bool regdb_store_values(const char *key, struct regval_ctr *values)
1521 TDB_DATA old_data, data;
1522 char *keystr = NULL;
1523 TALLOC_CTX *ctx = talloc_stackframe();
1526 bool result = false;
1528 DEBUG(10,("regdb_store_values: Looking for value of key [%s] \n", key));
1530 if (!regdb_key_exists(key)) {
1536 len = regdb_pack_values(values, data.dptr, data.dsize);
1538 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
1542 data.dptr = TALLOC_ARRAY(ctx, uint8, len);
1545 len = regdb_pack_values(values, data.dptr, data.dsize);
1547 SMB_ASSERT( len == data.dsize );
1549 keystr = talloc_asprintf(ctx, "%s/%s", REG_VALUE_PREFIX, key );
1553 keystr = normalize_reg_path(ctx, keystr);
1558 old_data = dbwrap_fetch_bystring(regdb, ctx, keystr);
1560 if ((old_data.dptr != NULL)
1561 && (old_data.dsize == data.dsize)
1562 && (memcmp(old_data.dptr, data.dptr, data.dsize) == 0))
1568 status = dbwrap_trans_store_bystring(regdb, keystr, data, TDB_REPLACE);
1570 result = NT_STATUS_IS_OK(status);
1577 static WERROR regdb_get_secdesc(TALLOC_CTX *mem_ctx, const char *key,
1578 struct security_descriptor **psecdesc)
1583 TALLOC_CTX *tmp_ctx = talloc_stackframe();
1584 WERROR err = WERR_OK;
1586 DEBUG(10, ("regdb_get_secdesc: Getting secdesc of key [%s]\n", key));
1588 if (!regdb_key_exists(key)) {
1593 tdbkey = talloc_asprintf(tmp_ctx, "%s/%s", REG_SECDESC_PREFIX, key);
1594 if (tdbkey == NULL) {
1598 normalize_dbkey(tdbkey);
1600 data = dbwrap_fetch_bystring(regdb, tmp_ctx, tdbkey);
1601 if (data.dptr == NULL) {
1606 status = unmarshall_sec_desc(mem_ctx, (uint8 *)data.dptr, data.dsize,
1609 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MEMORY)) {
1611 } else if (!NT_STATUS_IS_OK(status)) {
1612 err = WERR_REG_CORRUPT;
1616 TALLOC_FREE(tmp_ctx);
1620 static WERROR regdb_set_secdesc(const char *key,
1621 struct security_descriptor *secdesc)
1623 TALLOC_CTX *mem_ctx = talloc_stackframe();
1625 WERROR err = WERR_NOMEM;
1628 if (!regdb_key_exists(key)) {
1633 tdbkey = talloc_asprintf(mem_ctx, "%s/%s", REG_SECDESC_PREFIX, key);
1634 if (tdbkey == NULL) {
1637 normalize_dbkey(tdbkey);
1639 if (secdesc == NULL) {
1640 /* assuming a delete */
1641 err = ntstatus_to_werror(dbwrap_trans_delete_bystring(regdb,
1646 err = ntstatus_to_werror(marshall_sec_desc(mem_ctx, secdesc,
1649 W_ERROR_NOT_OK_GOTO_DONE(err);
1651 err = ntstatus_to_werror(dbwrap_trans_store_bystring(regdb, tdbkey,
1655 TALLOC_FREE(mem_ctx);
1659 bool regdb_subkeys_need_update(struct regsubkey_ctr *subkeys)
1661 return (regdb_get_seqnum() != regsubkey_ctr_get_seqnum(subkeys));
1664 bool regdb_values_need_update(struct regval_ctr *values)
1666 return (regdb_get_seqnum() != values->seqnum);
1670 * Table of function pointers for default access
1673 struct registry_ops regdb_ops = {
1674 .fetch_subkeys = regdb_fetch_keys,
1675 .fetch_values = regdb_fetch_values,
1676 .store_subkeys = regdb_store_keys,
1677 .store_values = regdb_store_values,
1678 .create_subkey = regdb_create_subkey,
1679 .delete_subkey = regdb_delete_subkey,
1680 .get_secdesc = regdb_get_secdesc,
1681 .set_secdesc = regdb_set_secdesc,
1682 .subkeys_need_update = regdb_subkeys_need_update,
1683 .values_need_update = regdb_values_need_update