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);
32 /* List the deepest path into the registry. All part components will be created.*/
34 /* If you want to have a part of the path controlled by the tdb and part by
35 a virtual registry db (e.g. printing), then you have to list the deepest path.
36 For example,"HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion/Print"
37 allows the reg_db backend to handle everything up to
38 "HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion" and then we'll hook
39 the reg_printing backend onto the last component of the path (see
40 KEY_PRINTING_2K in include/rpc_reg.h) --jerry */
42 static const char *builtin_registry_paths[] = {
52 KEY_SAMBA_GROUP_POLICY,
53 KEY_GP_MACHINE_POLICY,
54 KEY_GP_MACHINE_WIN_POLICY,
57 KEY_GP_USER_WIN_POLICY,
58 KEY_WINLOGON_GPEXT_PATH,
59 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Print\\Monitors",
61 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\DefaultUserConfiguration",
70 struct builtin_regkey_value {
72 const char *valuename;
80 static struct builtin_regkey_value builtin_registry_values[] = {
82 SAMBA_PRINTER_PORT_NAME, REG_SZ, { "" } },
84 "DefaultSpoolDirectory", REG_SZ, { "C:\\Windows\\System32\\Spool\\Printers" } },
86 "DisplayName", REG_SZ, { "Event Log" } },
88 "ErrorControl", REG_DWORD, { (char*)0x00000001 } },
89 { NULL, NULL, 0, { NULL } }
93 * Initialize a key in the registry:
94 * create each component key of the specified path.
96 static WERROR init_registry_key_internal(const char *add_path)
99 TALLOC_CTX *frame = talloc_stackframe();
102 char *remaining = NULL;
105 REGSUBKEY_CTR *subkeys;
108 DEBUG(6, ("init_registry_key: Adding [%s]\n", add_path));
110 path = talloc_strdup(frame, add_path);
111 base = talloc_strdup(frame, "");
112 if (!path || !base) {
118 while (next_token_talloc(frame, &p, &keyname, "\\")) {
120 /* build up the registry path from the components */
123 base = talloc_asprintf(frame, "%s\\", base);
129 base = talloc_asprintf_append(base, "%s", keyname);
135 /* get the immediate subkeyname (if we have one ) */
137 subkeyname = talloc_strdup(frame, "");
143 remaining = talloc_strdup(frame, p);
150 if (!next_token_talloc(frame, &p2,
153 subkeyname = talloc_strdup(frame,p2);
161 DEBUG(10,("init_registry_key: Storing key [%s] with "
162 "subkey [%s]\n", base,
163 *subkeyname ? subkeyname : "NULL"));
165 /* we don't really care if the lookup succeeds or not
166 * since we are about to update the record.
167 * We just want any subkeys already present */
169 if (!(subkeys = TALLOC_ZERO_P(frame, REGSUBKEY_CTR))) {
170 DEBUG(0,("talloc() failure!\n"));
175 regdb_fetch_keys(base, subkeys);
177 werr = regsubkey_ctr_addkey(subkeys, subkeyname);
178 if (!W_ERROR_IS_OK(werr)) {
182 if (!regdb_store_keys( base, subkeys)) {
183 werr = WERR_CAN_NOT_COMPLETE;
196 * Initialize a key in the registry:
197 * create each component key of the specified path,
198 * wrapped in one db transaction.
200 WERROR init_registry_key(const char *add_path)
204 if (regdb->transaction_start(regdb) != 0) {
205 DEBUG(0, ("init_registry_key: transaction_start failed\n"));
206 return WERR_REG_IO_FAILURE;
209 werr = init_registry_key_internal(add_path);
210 if (!W_ERROR_IS_OK(werr)) {
214 if (regdb->transaction_commit(regdb) != 0) {
215 DEBUG(0, ("init_registry_key: Could not commit transaction\n"));
216 return WERR_REG_IO_FAILURE;
222 if (regdb->transaction_cancel(regdb) != 0) {
223 smb_panic("init_registry_key: transaction_cancel failed\n");
229 /***********************************************************************
230 Open the registry data in the tdb
231 ***********************************************************************/
233 WERROR init_registry_data(void)
236 TALLOC_CTX *frame = NULL;
242 * There are potentially quite a few store operations which are all
243 * indiviually wrapped in tdb transactions. Wrapping them in a single
244 * transaction gives just a single transaction_commit() to actually do
245 * its fsync()s. See tdb/common/transaction.c for info about nested
246 * transaction behaviour.
249 if (regdb->transaction_start(regdb) != 0) {
250 DEBUG(0, ("init_registry_data: tdb_transaction_start "
252 return WERR_REG_IO_FAILURE;
255 /* loop over all of the predefined paths and add each component */
257 for (i=0; builtin_registry_paths[i] != NULL; i++) {
258 werr = init_registry_key_internal(builtin_registry_paths[i]);
259 if (!W_ERROR_IS_OK(werr)) {
264 /* loop over all of the predefined values and add each component */
266 frame = talloc_stackframe();
268 for (i=0; builtin_registry_values[i].path != NULL; i++) {
270 values = TALLOC_ZERO_P(frame, REGVAL_CTR);
271 if (values == NULL) {
276 regdb_fetch_values(builtin_registry_values[i].path, values);
278 /* preserve existing values across restarts. Only add new ones */
280 if (!regval_ctr_key_exists(values,
281 builtin_registry_values[i].valuename))
283 switch(builtin_registry_values[i].type) {
285 regval_ctr_addvalue(values,
286 builtin_registry_values[i].valuename,
288 (char*)&builtin_registry_values[i].data.dw_value,
294 builtin_registry_values[i].data.string,
296 regval_ctr_addvalue(values,
297 builtin_registry_values[i].valuename,
300 data.uni_str_len*sizeof(uint16));
304 DEBUG(0, ("init_registry_data: invalid value "
305 "type in builtin_registry_values "
307 builtin_registry_values[i].type));
309 regdb_store_values(builtin_registry_values[i].path,
317 if (regdb->transaction_commit(regdb) != 0) {
318 DEBUG(0, ("init_registry_data: Could not commit "
320 return WERR_REG_IO_FAILURE;
329 if (regdb->transaction_cancel(regdb) != 0) {
330 smb_panic("init_registry_data: tdb_transaction_cancel "
337 /***********************************************************************
338 Open the registry database
339 ***********************************************************************/
341 WERROR regdb_init(void)
343 const char *vstring = "INFO/version";
348 DEBUG(10, ("regdb_init: incrementing refcount (%d)\n",
354 regdb = db_open_trans(NULL, state_path("registry.tdb"), 0,
355 REG_TDB_FLAGS, O_RDWR, 0600);
357 regdb = db_open_trans(NULL, state_path("registry.tdb"), 0,
358 REG_TDB_FLAGS, O_RDWR|O_CREAT, 0600);
360 werr = ntstatus_to_werror(map_nt_error_from_unix(errno));
361 DEBUG(0,("regdb_init: Failed to open registry %s (%s)\n",
362 state_path("registry.tdb"), strerror(errno) ));
366 DEBUG(10,("regdb_init: Successfully created registry tdb\n"));
371 vers_id = dbwrap_fetch_int32(regdb, vstring);
373 if ( vers_id != REGVER_V1 ) {
375 /* any upgrade code here if needed */
376 DEBUG(10, ("regdb_init: got %s = %d != %d\n", vstring,
377 vers_id, REGVER_V1));
378 status = dbwrap_trans_store_int32(regdb, vstring, REGVER_V1);
379 if (!NT_STATUS_IS_OK(status)) {
380 DEBUG(0, ("regdb_init: error storing %s = %d: %s\n",
381 vstring, REGVER_V1, nt_errstr(status)));
382 return ntstatus_to_werror(status);
384 DEBUG(10, ("regdb_init: stored %s = %d\n",
385 vstring, REGVER_V1));
392 /***********************************************************************
393 Open the registry. Must already have been initialized by regdb_init()
394 ***********************************************************************/
396 WERROR regdb_open( void )
398 WERROR result = WERR_OK;
401 DEBUG(10,("regdb_open: incrementing refcount (%d)\n", regdb_refcount));
408 regdb = db_open_trans(NULL, state_path("registry.tdb"), 0,
409 REG_TDB_FLAGS, O_RDWR, 0600);
411 result = ntstatus_to_werror( map_nt_error_from_unix( errno ) );
412 DEBUG(0,("regdb_open: Failed to open %s! (%s)\n",
413 state_path("registry.tdb"), strerror(errno) ));
419 DEBUG(10,("regdb_open: refcount reset (%d)\n", regdb_refcount));
424 /***********************************************************************
425 ***********************************************************************/
427 int regdb_close( void )
429 if (regdb_refcount == 0) {
435 DEBUG(10,("regdb_close: decrementing refcount (%d)\n", regdb_refcount));
437 if ( regdb_refcount > 0 )
440 SMB_ASSERT( regdb_refcount >= 0 );
446 /***********************************************************************
447 return the tdb sequence number of the registry tdb.
448 this is an indicator for the content of the registry
449 having changed. it will change upon regdb_init, too, though.
450 ***********************************************************************/
451 int regdb_get_seqnum(void)
453 return regdb->get_seqnum(regdb);
456 /***********************************************************************
457 Add subkey strings to the registry tdb under a defined key
458 fmt is the same format as tdb_pack except this function only supports
460 ***********************************************************************/
462 static bool regdb_store_keys_internal(const char *key, REGSUBKEY_CTR *ctr)
465 uint8 *buffer = NULL;
469 uint32 num_subkeys = regsubkey_ctr_numkeys(ctr);
470 char *keyname = NULL;
471 TALLOC_CTX *ctx = talloc_stackframe();
478 keyname = talloc_strdup(ctx, key);
482 keyname = normalize_reg_path(ctx, keyname);
484 /* allocate some initial memory */
486 buffer = (uint8 *)SMB_MALLOC(1024);
487 if (buffer == NULL) {
493 /* store the number of subkeys */
495 len += tdb_pack(buffer+len, buflen-len, "d", num_subkeys);
497 /* pack all the strings */
499 for (i=0; i<num_subkeys; i++) {
500 len += tdb_pack(buffer+len, buflen-len, "f",
501 regsubkey_ctr_specific_key(ctr, i));
503 /* allocate some extra space */
504 buffer = (uint8 *)SMB_REALLOC(buffer, len*2);
506 DEBUG(0, ("regdb_store_keys: Failed to realloc "
507 "memory of size [%d]\n", len*2));
512 len = tdb_pack(buffer+len, buflen-len, "f",
513 regsubkey_ctr_specific_key(ctr, i));
517 /* finally write out the data */
521 status = dbwrap_store_bystring(regdb, keyname, dbuf, TDB_REPLACE);
522 if (!NT_STATUS_IS_OK(status)) {
533 /***********************************************************************
534 Store the new subkey record and create any child key records that
535 do not currently exist
536 ***********************************************************************/
538 bool regdb_store_keys(const char *key, REGSUBKEY_CTR *ctr)
542 REGSUBKEY_CTR *subkeys = NULL, *old_subkeys = NULL;
543 char *oldkeyname = NULL;
544 TALLOC_CTX *ctx = talloc_stackframe();
548 * fetch a list of the old subkeys so we can determine if anything has
552 if (!(old_subkeys = TALLOC_ZERO_P(ctx, REGSUBKEY_CTR))) {
553 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
557 regdb_fetch_keys(key, old_subkeys);
559 if ((ctr->num_subkeys && old_subkeys->num_subkeys) &&
560 (ctr->num_subkeys == old_subkeys->num_subkeys)) {
562 for (i = 0; i<ctr->num_subkeys; i++) {
563 if (strcmp(ctr->subkeys[i],
564 old_subkeys->subkeys[i]) != 0) {
568 if (i == ctr->num_subkeys) {
570 * Nothing changed, no point to even start a tdb
573 TALLOC_FREE(old_subkeys);
578 TALLOC_FREE(old_subkeys);
580 if (regdb->transaction_start(regdb) != 0) {
581 DEBUG(0, ("regdb_store_keys: transaction_start failed\n"));
586 * Re-fetch the old keys inside the transaction
589 if (!(old_subkeys = TALLOC_ZERO_P(ctx, REGSUBKEY_CTR))) {
590 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
594 regdb_fetch_keys(key, old_subkeys);
596 /* store the subkey list for the parent */
598 if (!regdb_store_keys_internal(key, ctr) ) {
599 DEBUG(0,("regdb_store_keys: Failed to store new subkey list "
600 "for parent [%s]\n", key));
604 /* now delete removed keys */
606 num_subkeys = regsubkey_ctr_numkeys(old_subkeys);
607 for (i=0; i<num_subkeys; i++) {
608 oldkeyname = regsubkey_ctr_specific_key(old_subkeys, i);
610 if (regsubkey_ctr_key_exists(ctr, oldkeyname)) {
612 * It's still around, don't delete
618 path = talloc_asprintf(ctx, "%s/%s", key, oldkeyname);
622 path = normalize_reg_path(ctx, path);
626 status = dbwrap_delete_bystring(regdb, path);
627 if (!NT_STATUS_IS_OK(status)) {
628 DEBUG(1, ("Deleting %s failed\n", path));
633 path = talloc_asprintf(ctx, "%s/%s/%s",
640 path = normalize_reg_path(ctx, path);
646 * Ignore errors here, we might have no values around
648 dbwrap_delete_bystring(regdb, path);
652 TALLOC_FREE(old_subkeys);
654 /* now create records for any subkeys that don't already exist */
656 num_subkeys = regsubkey_ctr_numkeys(ctr);
658 if (num_subkeys == 0) {
659 if (!(subkeys = TALLOC_ZERO_P(ctx, REGSUBKEY_CTR)) ) {
660 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
664 if (!regdb_store_keys_internal(key, subkeys)) {
665 DEBUG(0,("regdb_store_keys: Failed to store "
666 "new record for key [%s]\n", key));
669 TALLOC_FREE(subkeys);
673 for (i=0; i<num_subkeys; i++) {
674 path = talloc_asprintf(ctx, "%s/%s",
676 regsubkey_ctr_specific_key(ctr, i));
680 if (!(subkeys = TALLOC_ZERO_P(ctx, REGSUBKEY_CTR)) ) {
681 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
685 if (regdb_fetch_keys( path, subkeys ) == -1) {
686 /* create a record with 0 subkeys */
687 if (!regdb_store_keys_internal(path, subkeys)) {
688 DEBUG(0,("regdb_store_keys: Failed to store "
689 "new record for key [%s]\n", path));
694 TALLOC_FREE(subkeys);
698 if (regdb->transaction_commit(regdb) != 0) {
699 DEBUG(0, ("regdb_store_keys: Could not commit transaction\n"));
707 if (regdb->transaction_cancel(regdb) != 0) {
708 smb_panic("regdb_store_keys: transaction_cancel failed\n");
718 static TDB_DATA regdb_fetch_key_internal(const char *key, TALLOC_CTX *mem_ctx)
722 path = normalize_reg_path(mem_ctx, key);
724 return make_tdb_data(NULL, 0);
727 return dbwrap_fetch_bystring(regdb, mem_ctx, path);
731 static bool regdb_key_exists(const char *key)
733 TALLOC_CTX *mem_ctx = talloc_stackframe();
737 value = regdb_fetch_key_internal(key, mem_ctx);
738 ret = (value.dptr != NULL);
740 TALLOC_FREE(mem_ctx);
745 /***********************************************************************
746 Retrieve an array of strings containing subkeys. Memory should be
747 released by the caller.
748 ***********************************************************************/
750 int regdb_fetch_keys(const char *key, REGSUBKEY_CTR *ctr)
759 TALLOC_CTX *frame = talloc_stackframe();
762 DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key ? key : "NULL"));
764 ctr->seqnum = regdb_get_seqnum();
766 value = regdb_fetch_key_internal(key, frame);
769 buflen = value.dsize;
772 DEBUG(5,("regdb_fetch_keys: tdb lookup failed to locate key [%s]\n", key));
776 len = tdb_unpack( buf, buflen, "d", &num_items);
778 for (i=0; i<num_items; i++) {
779 len += tdb_unpack(buf+len, buflen-len, "f", subkeyname);
780 werr = regsubkey_ctr_addkey(ctr, subkeyname);
781 if (!W_ERROR_IS_OK(werr)) {
782 DEBUG(5, ("regdb_fetch_keys: regsubkey_ctr_addkey "
783 "failed: %s\n", dos_errstr(werr)));
788 DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items));
796 /****************************************************************************
797 Unpack a list of registry values frem the TDB
798 ***************************************************************************/
800 static int regdb_unpack_values(REGVAL_CTR *values, uint8 *buf, int buflen)
807 uint32 num_values = 0;
810 /* loop and unpack the rest of the registry values */
812 len += tdb_unpack(buf+len, buflen-len, "d", &num_values);
814 for ( i=0; i<num_values; i++ ) {
815 /* unpack the next regval */
821 len += tdb_unpack(buf+len, buflen-len, "fdB",
827 /* add the new value. Paranoid protective code -- make sure data_p is valid */
829 if (*valuename && size && data_p) {
830 regval_ctr_addvalue(values, valuename, type,
831 (const char *)data_p, size);
833 SAFE_FREE(data_p); /* 'B' option to tdb_unpack does a malloc() */
835 DEBUG(8,("specific: [%s], len: %d\n", valuename, size));
841 /****************************************************************************
842 Pack all values in all printer keys
843 ***************************************************************************/
845 static int regdb_pack_values(REGVAL_CTR *values, uint8 *buf, int buflen)
855 num_values = regval_ctr_numvals( values );
857 /* pack the number of values first */
859 len += tdb_pack( buf+len, buflen-len, "d", num_values );
861 /* loop over all values */
863 for ( i=0; i<num_values; i++ ) {
864 val = regval_ctr_specific_value( values, i );
865 len += tdb_pack(buf+len, buflen-len, "fdB",
869 regval_data_p(val) );
875 /***********************************************************************
876 Retrieve an array of strings containing subkeys. Memory should be
877 released by the caller.
878 ***********************************************************************/
880 int regdb_fetch_values( const char* key, REGVAL_CTR *values )
883 TALLOC_CTX *ctx = talloc_stackframe();
887 DEBUG(10,("regdb_fetch_values: Looking for value of key [%s] \n", key));
889 keystr = talloc_asprintf(ctx, "%s/%s", REG_VALUE_PREFIX, key);
894 values->seqnum = regdb_get_seqnum();
896 value = regdb_fetch_key_internal(keystr, ctx);
899 /* all keys have zero values by default */
903 regdb_unpack_values(values, value.dptr, value.dsize);
904 ret = regval_ctr_numvals(values);
911 bool regdb_store_values( const char *key, REGVAL_CTR *values )
913 TDB_DATA old_data, data;
915 TALLOC_CTX *ctx = talloc_stackframe();
920 DEBUG(10,("regdb_store_values: Looking for value of key [%s] \n", key));
924 len = regdb_pack_values(values, data.dptr, data.dsize);
926 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
930 data.dptr = TALLOC_ARRAY(ctx, uint8, len);
933 len = regdb_pack_values(values, data.dptr, data.dsize);
935 SMB_ASSERT( len == data.dsize );
937 keystr = talloc_asprintf(ctx, "%s/%s", REG_VALUE_PREFIX, key );
941 keystr = normalize_reg_path(ctx, keystr);
946 old_data = dbwrap_fetch_bystring(regdb, ctx, keystr);
948 if ((old_data.dptr != NULL)
949 && (old_data.dsize == data.dsize)
950 && (memcmp(old_data.dptr, data.dptr, data.dsize) == 0))
956 status = dbwrap_trans_store(regdb, string_term_tdb_data(keystr), data,
959 result = NT_STATUS_IS_OK(status);
966 static WERROR regdb_get_secdesc(TALLOC_CTX *mem_ctx, const char *key,
967 struct security_descriptor **psecdesc)
972 TALLOC_CTX *tmp_ctx = talloc_stackframe();
973 WERROR err = WERR_OK;
975 DEBUG(10, ("regdb_get_secdesc: Getting secdesc of key [%s]\n", key));
977 tdbkey = talloc_asprintf(tmp_ctx, "%s/%s", REG_SECDESC_PREFIX, key);
978 if (tdbkey == NULL) {
982 normalize_dbkey(tdbkey);
984 data = dbwrap_fetch_bystring(regdb, tmp_ctx, tdbkey);
985 if (data.dptr == NULL) {
990 status = unmarshall_sec_desc(mem_ctx, (uint8 *)data.dptr, data.dsize,
993 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MEMORY)) {
995 } else if (!NT_STATUS_IS_OK(status)) {
996 err = WERR_REG_CORRUPT;
1000 TALLOC_FREE(tmp_ctx);
1004 static WERROR regdb_set_secdesc(const char *key,
1005 struct security_descriptor *secdesc)
1007 TALLOC_CTX *mem_ctx = talloc_stackframe();
1010 WERROR err = WERR_NOMEM;
1013 tdbkey = talloc_asprintf(mem_ctx, "%s/%s", REG_SECDESC_PREFIX, key);
1014 if (tdbkey == NULL) {
1017 normalize_dbkey(tdbkey);
1019 if (secdesc == NULL) {
1020 /* assuming a delete */
1021 status = dbwrap_trans_delete(regdb,
1022 string_term_tdb_data(tdbkey));
1023 if (NT_STATUS_IS_OK(status)) {
1026 err = ntstatus_to_werror(status);
1031 err = ntstatus_to_werror(marshall_sec_desc(mem_ctx, secdesc,
1034 if (!W_ERROR_IS_OK(err)) {
1038 status = dbwrap_trans_store(regdb, string_term_tdb_data(tdbkey),
1040 if (!NT_STATUS_IS_OK(status)) {
1041 err = ntstatus_to_werror(status);
1046 TALLOC_FREE(mem_ctx);
1050 bool regdb_subkeys_need_update(REGSUBKEY_CTR *subkeys)
1052 return (regdb_get_seqnum() != subkeys->seqnum);
1055 bool regdb_values_need_update(REGVAL_CTR *values)
1057 return (regdb_get_seqnum() != values->seqnum);
1061 * Table of function pointers for default access
1064 REGISTRY_OPS regdb_ops = {
1065 .fetch_subkeys = regdb_fetch_keys,
1066 .fetch_values = regdb_fetch_values,
1067 .store_subkeys = regdb_store_keys,
1068 .store_values = regdb_store_values,
1069 .get_secdesc = regdb_get_secdesc,
1070 .set_secdesc = regdb_set_secdesc,
1071 .subkeys_need_update = regdb_subkeys_need_update,
1072 .values_need_update = regdb_values_need_update