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 tdb_wrap *tdb_reg = NULL;
28 static int tdb_refcount;
30 /* List the deepest path into the registry. All part components will be created.*/
32 /* If you want to have a part of the path controlled by the tdb and part by
33 a virtual registry db (e.g. printing), then you have to list the deepest path.
34 For example,"HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion/Print"
35 allows the reg_db backend to handle everything up to
36 "HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion" and then we'll hook
37 the reg_printing backend onto the last component of the path (see
38 KEY_PRINTING_2K in include/rpc_reg.h) --jerry */
40 static const char *builtin_registry_paths[] = {
49 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Print\\Monitors",
51 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\DefaultUserConfiguration",
60 struct builtin_regkey_value {
62 const char *valuename;
70 static struct builtin_regkey_value builtin_registry_values[] = {
72 SAMBA_PRINTER_PORT_NAME, REG_SZ, { "" } },
74 "DefaultSpoolDirectory", REG_SZ, { "C:\\Windows\\System32\\Spool\\Printers" } },
76 "DisplayName", REG_SZ, { "Event Log" } },
78 "ErrorControl", REG_DWORD, { (char*)0x00000001 } },
79 { NULL, NULL, 0, { NULL } }
82 /***********************************************************************
83 Open the registry data in the tdb
84 ***********************************************************************/
86 static bool init_registry_data( void )
90 char *remaining = NULL;
91 TALLOC_CTX *frame = NULL;
94 REGSUBKEY_CTR *subkeys;
101 * There are potentially quite a few store operations which are all
102 * indiviually wrapped in tdb transactions. Wrapping them in a single
103 * transaction gives just a single transaction_commit() to actually do
104 * its fsync()s. See tdb/common/transaction.c for info about nested
105 * transaction behaviour.
108 if ( tdb_transaction_start( tdb_reg->tdb ) == -1 ) {
109 DEBUG(0, ("init_registry_data: tdb_transaction_start "
114 /* loop over all of the predefined paths and add each component */
116 for ( i=0; builtin_registry_paths[i] != NULL; i++ ) {
118 frame = talloc_stackframe();
120 DEBUG(6,("init_registry_data: Adding [%s]\n", builtin_registry_paths[i]));
122 path = talloc_strdup(talloc_tos(), builtin_registry_paths[i]);
123 base = talloc_strdup(talloc_tos(), "");
124 if (!path || !base) {
129 while (next_token_talloc(talloc_tos(), &p, &keyname, "\\")) {
131 /* build up the registry path from the components */
134 base = talloc_asprintf(talloc_tos(), "%s\\", base);
139 base = talloc_asprintf_append(base, "%s", keyname);
144 /* get the immediate subkeyname (if we have one ) */
146 subkeyname = talloc_strdup(talloc_tos(), "");
151 remaining = talloc_strdup(talloc_tos(), p);
157 if (!next_token_talloc(talloc_tos(), &p2,
158 &subkeyname, "\\")) {
159 subkeyname = talloc_strdup(talloc_tos(),p2);
166 DEBUG(10,("init_registry_data: Storing key [%s] with subkey [%s]\n",
167 base, *subkeyname ? subkeyname : "NULL"));
169 /* we don't really care if the lookup succeeds or not since
170 we are about to update the record. We just want any
171 subkeys already present */
173 if ( !(subkeys = TALLOC_ZERO_P(talloc_tos(), REGSUBKEY_CTR )) ) {
174 DEBUG(0,("talloc() failure!\n"));
178 regdb_fetch_keys(base, subkeys);
180 regsubkey_ctr_addkey( subkeys, subkeyname);
182 if (!regdb_store_keys( base, subkeys)) {
190 /* loop over all of the predefined values and add each component */
192 for (i=0; builtin_registry_values[i].path != NULL; i++) {
194 if (!(values = TALLOC_ZERO_P(talloc_tos(), REGVAL_CTR))) {
198 regdb_fetch_values( builtin_registry_values[i].path, values);
200 /* preserve existing values across restarts. Only add new ones */
202 if (!regval_ctr_key_exists(values, builtin_registry_values[i].valuename)) {
203 switch(builtin_registry_values[i].type) {
205 regval_ctr_addvalue( values,
206 builtin_registry_values[i].valuename,
208 (char*)&builtin_registry_values[i].data.dw_value,
213 init_unistr2( &data, builtin_registry_values[i].data.string, UNI_STR_TERMINATE);
214 regval_ctr_addvalue( values,
215 builtin_registry_values[i].valuename,
218 data.uni_str_len*sizeof(uint16) );
222 DEBUG(0,("init_registry_data: invalid value type in builtin_registry_values [%d]\n",
223 builtin_registry_values[i].type));
225 regdb_store_values( builtin_registry_values[i].path, values );
227 TALLOC_FREE( values );
232 if (tdb_transaction_commit( tdb_reg->tdb ) == -1) {
233 DEBUG(0, ("init_registry_data: Could not commit "
244 if (tdb_transaction_cancel( tdb_reg->tdb ) == -1) {
245 smb_panic("init_registry_data: tdb_transaction_cancel "
252 /***********************************************************************
253 Open the registry database
254 ***********************************************************************/
256 bool regdb_init( void )
258 const char *vstring = "INFO/version";
262 DEBUG(10,("regdb_init: incrementing refcount (%d)\n", tdb_refcount));
267 if ( !(tdb_reg = tdb_wrap_open(NULL, state_path("registry.tdb"), 0, REG_TDB_FLAGS, O_RDWR, 0600)) )
269 tdb_reg = tdb_wrap_open(NULL, state_path("registry.tdb"), 0, REG_TDB_FLAGS, O_RDWR|O_CREAT, 0600);
271 DEBUG(0,("regdb_init: Failed to open registry %s (%s)\n",
272 state_path("registry.tdb"), strerror(errno) ));
276 DEBUG(10,("regdb_init: Successfully created registry tdb\n"));
281 vers_id = tdb_fetch_int32(tdb_reg->tdb, vstring);
283 if ( vers_id != REGVER_V1 ) {
284 /* any upgrade code here if needed */
285 DEBUG(10, ("regdb_init: got INFO/version = %d != %d\n",
286 vers_id, REGVER_V1));
289 /* always setup the necessary keys and values */
291 if ( !init_registry_data() ) {
292 DEBUG(0,("regdb_init: Failed to initialize data in registry!\n"));
299 /***********************************************************************
300 Open the registry. Must already have been initialized by regdb_init()
301 ***********************************************************************/
303 WERROR regdb_open( void )
305 WERROR result = WERR_OK;
308 DEBUG(10,("regdb_open: incrementing refcount (%d)\n", tdb_refcount));
315 tdb_reg = tdb_wrap_open(NULL, state_path("registry.tdb"), 0, REG_TDB_FLAGS, O_RDWR, 0600);
317 result = ntstatus_to_werror( map_nt_error_from_unix( errno ) );
318 DEBUG(0,("regdb_open: Failed to open %s! (%s)\n",
319 state_path("registry.tdb"), strerror(errno) ));
325 DEBUG(10,("regdb_open: refcount reset (%d)\n", tdb_refcount));
330 /***********************************************************************
331 ***********************************************************************/
333 int regdb_close( void )
335 if (tdb_refcount == 0) {
341 DEBUG(10,("regdb_close: decrementing refcount (%d)\n", tdb_refcount));
343 if ( tdb_refcount > 0 )
346 SMB_ASSERT( tdb_refcount >= 0 );
348 TALLOC_FREE(tdb_reg);
352 /***********************************************************************
353 return the tdb sequence number of the registry tdb.
354 this is an indicator for the content of the registry
355 having changed. it will change upon regdb_init, too, though.
356 ***********************************************************************/
357 int regdb_get_seqnum(void)
359 return tdb_get_seqnum(tdb_reg->tdb);
362 /***********************************************************************
363 Add subkey strings to the registry tdb under a defined key
364 fmt is the same format as tdb_pack except this function only supports
366 ***********************************************************************/
368 static bool regdb_store_keys_internal(const char *key, REGSUBKEY_CTR *ctr)
371 uint8 *buffer = NULL;
375 uint32 num_subkeys = regsubkey_ctr_numkeys(ctr);
376 char *keyname = NULL;
377 TALLOC_CTX *ctx = talloc_tos();
383 keyname = talloc_strdup(ctx, key);
387 keyname = normalize_reg_path(ctx, keyname);
389 /* allocate some initial memory */
391 if (!(buffer = (uint8 *)SMB_MALLOC(1024))) {
397 /* store the number of subkeys */
399 len += tdb_pack(buffer+len, buflen-len, "d", num_subkeys );
401 /* pack all the strings */
403 for (i=0; i<num_subkeys; i++) {
404 len += tdb_pack( buffer+len, buflen-len, "f", regsubkey_ctr_specific_key(ctr, i) );
405 if ( len > buflen ) {
406 /* allocate some extra space */
407 if ((buffer = (uint8 *)SMB_REALLOC( buffer, len*2 )) == NULL) {
408 DEBUG(0,("regdb_store_keys: Failed to realloc memory of size [%d]\n", len*2));
414 len = tdb_pack( buffer+len, buflen-len, "f", regsubkey_ctr_specific_key(ctr, i) );
418 /* finally write out the data */
422 if ( tdb_store_bystring( tdb_reg->tdb, keyname, dbuf, TDB_REPLACE ) == -1) {
432 /***********************************************************************
433 Store the new subkey record and create any child key records that
434 do not currently exist
435 ***********************************************************************/
437 bool regdb_store_keys(const char *key, REGSUBKEY_CTR *ctr)
441 REGSUBKEY_CTR *subkeys = NULL, *old_subkeys = NULL;
442 char *oldkeyname = NULL;
443 TALLOC_CTX *ctx = talloc_tos();
446 * fetch a list of the old subkeys so we can determine if anything has
450 if (!(old_subkeys = TALLOC_ZERO_P(ctr, REGSUBKEY_CTR))) {
451 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
455 regdb_fetch_keys(key, old_subkeys);
457 if ((ctr->num_subkeys && old_subkeys->num_subkeys) &&
458 (ctr->num_subkeys == old_subkeys->num_subkeys)) {
460 for (i = 0; i<ctr->num_subkeys; i++) {
461 if (strcmp(ctr->subkeys[i],
462 old_subkeys->subkeys[i]) != 0) {
466 if (i == ctr->num_subkeys) {
468 * Nothing changed, no point to even start a tdb
471 TALLOC_FREE(old_subkeys);
476 if (tdb_transaction_start( tdb_reg->tdb ) == -1) {
477 DEBUG(0, ("regdb_store_keys: tdb_transaction_start failed\n"));
482 * Re-fetch the old keys inside the transaction
485 TALLOC_FREE(old_subkeys);
487 if (!(old_subkeys = TALLOC_ZERO_P(ctr, REGSUBKEY_CTR))) {
488 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
492 regdb_fetch_keys(key, old_subkeys);
494 /* store the subkey list for the parent */
496 if (!regdb_store_keys_internal(key, ctr) ) {
497 DEBUG(0,("regdb_store_keys: Failed to store new subkey list "
498 "for parent [%s]\n", key));
502 /* now delete removed keys */
504 num_subkeys = regsubkey_ctr_numkeys(old_subkeys);
505 for (i=0; i<num_subkeys; i++) {
506 oldkeyname = regsubkey_ctr_specific_key(old_subkeys, i);
508 if (regsubkey_ctr_key_exists(ctr, oldkeyname)) {
510 * It's still around, don't delete
516 path = talloc_asprintf(ctx, "%s/%s", key, oldkeyname);
520 path = normalize_reg_path(ctx, path);
524 if (tdb_delete_bystring(tdb_reg->tdb, path) == -1) {
525 DEBUG(1, ("Deleting %s failed\n", path));
530 path = talloc_asprintf(ctx, "%s/%s/%s",
537 path = normalize_reg_path(ctx, path);
543 * Ignore errors here, we might have no values around
545 tdb_delete_bystring( tdb_reg->tdb, path );
549 TALLOC_FREE(old_subkeys);
551 /* now create records for any subkeys that don't already exist */
553 num_subkeys = regsubkey_ctr_numkeys(ctr);
555 if (num_subkeys == 0) {
556 if (!(subkeys = TALLOC_ZERO_P(ctr, REGSUBKEY_CTR)) ) {
557 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
561 if (!regdb_store_keys_internal(key, subkeys)) {
562 DEBUG(0,("regdb_store_keys: Failed to store "
563 "new record for key [%s]\n", key));
566 TALLOC_FREE(subkeys);
570 for (i=0; i<num_subkeys; i++) {
571 path = talloc_asprintf(ctx, "%s/%s",
573 regsubkey_ctr_specific_key(ctr, i));
577 if (!(subkeys = TALLOC_ZERO_P(ctr, REGSUBKEY_CTR)) ) {
578 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
582 if (regdb_fetch_keys( path, subkeys ) == -1) {
583 /* create a record with 0 subkeys */
584 if (!regdb_store_keys_internal(path, subkeys)) {
585 DEBUG(0,("regdb_store_keys: Failed to store "
586 "new record for key [%s]\n", path));
591 TALLOC_FREE(subkeys);
595 if (tdb_transaction_commit( tdb_reg->tdb ) == -1) {
596 DEBUG(0, ("regdb_store_keys: Could not commit transaction\n"));
603 TALLOC_FREE(old_subkeys);
604 TALLOC_FREE(subkeys);
606 if (tdb_transaction_cancel(tdb_reg->tdb) == -1) {
607 smb_panic("regdb_store_keys: tdb_transaction_cancel failed\n");
614 /***********************************************************************
615 Retrieve an array of strings containing subkeys. Memory should be
616 released by the caller.
617 ***********************************************************************/
619 int regdb_fetch_keys(const char *key, REGSUBKEY_CTR *ctr)
629 TALLOC_CTX *frame = talloc_stackframe();
631 DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key ? key : "NULL"));
633 path = talloc_strdup(talloc_tos(), key);
638 /* convert to key format */
639 path = talloc_string_sub(talloc_tos(), path, "\\", "/");
645 if (tdb_read_lock_bystring_with_timeout(tdb_reg->tdb, path, 10) == -1) {
649 dbuf = tdb_fetch_bystring(tdb_reg->tdb, path);
650 ctr->seqnum = regdb_get_seqnum();
652 tdb_read_unlock_bystring(tdb_reg->tdb, path);
659 DEBUG(5,("regdb_fetch_keys: tdb lookup failed to locate key [%s]\n", key));
663 len = tdb_unpack( buf, buflen, "d", &num_items);
665 for (i=0; i<num_items; i++) {
666 len += tdb_unpack(buf+len, buflen-len, "f", subkeyname);
667 regsubkey_ctr_addkey(ctr, subkeyname);
670 SAFE_FREE(dbuf.dptr);
672 DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items));
680 /****************************************************************************
681 Unpack a list of registry values frem the TDB
682 ***************************************************************************/
684 static int regdb_unpack_values(REGVAL_CTR *values, uint8 *buf, int buflen)
691 uint32 num_values = 0;
694 /* loop and unpack the rest of the registry values */
696 len += tdb_unpack(buf+len, buflen-len, "d", &num_values);
698 for ( i=0; i<num_values; i++ ) {
699 /* unpack the next regval */
705 len += tdb_unpack(buf+len, buflen-len, "fdB",
711 /* add the new value. Paranoid protective code -- make sure data_p is valid */
713 if (*valuename && size && data_p) {
714 regval_ctr_addvalue(values, valuename, type,
715 (const char *)data_p, size);
717 SAFE_FREE(data_p); /* 'B' option to tdb_unpack does a malloc() */
719 DEBUG(8,("specific: [%s], len: %d\n", valuename, size));
725 /****************************************************************************
726 Pack all values in all printer keys
727 ***************************************************************************/
729 static int regdb_pack_values(REGVAL_CTR *values, uint8 *buf, int buflen)
739 num_values = regval_ctr_numvals( values );
741 /* pack the number of values first */
743 len += tdb_pack( buf+len, buflen-len, "d", num_values );
745 /* loop over all values */
747 for ( i=0; i<num_values; i++ ) {
748 val = regval_ctr_specific_value( values, i );
749 len += tdb_pack(buf+len, buflen-len, "fdB",
753 regval_data_p(val) );
759 /***********************************************************************
760 Retrieve an array of strings containing subkeys. Memory should be
761 released by the caller.
762 ***********************************************************************/
764 int regdb_fetch_values( const char* key, REGVAL_CTR *values )
768 TALLOC_CTX *ctx = talloc_tos();
770 DEBUG(10,("regdb_fetch_values: Looking for value of key [%s] \n", key));
772 keystr = talloc_asprintf(ctx, "%s/%s", REG_VALUE_PREFIX, key);
776 keystr = normalize_reg_path(ctx, keystr);
781 if (tdb_read_lock_bystring_with_timeout(tdb_reg->tdb, keystr, 10) == -1) {
785 data = tdb_fetch_bystring(tdb_reg->tdb, keystr);
786 values->seqnum = regdb_get_seqnum();
788 tdb_read_unlock_bystring(tdb_reg->tdb, keystr);
791 /* all keys have zero values by default */
795 regdb_unpack_values(values, data.dptr, data.dsize);
797 SAFE_FREE(data.dptr);
798 return regval_ctr_numvals(values);
801 bool regdb_store_values( const char *key, REGVAL_CTR *values )
803 TDB_DATA old_data, data;
805 TALLOC_CTX *ctx = talloc_tos();
808 DEBUG(10,("regdb_store_values: Looking for value of key [%s] \n", key));
812 len = regdb_pack_values(values, data.dptr, data.dsize);
814 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
818 data.dptr = SMB_MALLOC_ARRAY( uint8, len );
821 len = regdb_pack_values(values, data.dptr, data.dsize);
823 SMB_ASSERT( len == data.dsize );
825 keystr = talloc_asprintf(ctx, "%s/%s", REG_VALUE_PREFIX, key );
827 SAFE_FREE(data.dptr);
830 keystr = normalize_reg_path(ctx, keystr);
832 SAFE_FREE(data.dptr);
836 old_data = tdb_fetch_bystring(tdb_reg->tdb, keystr);
838 if ((old_data.dptr != NULL)
839 && (old_data.dsize == data.dsize)
840 && (memcmp(old_data.dptr, data.dptr, data.dsize) == 0)) {
841 SAFE_FREE(old_data.dptr);
842 SAFE_FREE(data.dptr);
846 ret = tdb_trans_store_bystring(tdb_reg->tdb, keystr, data, TDB_REPLACE);
848 SAFE_FREE( old_data.dptr );
849 SAFE_FREE( data.dptr );
854 static WERROR regdb_get_secdesc(TALLOC_CTX *mem_ctx, const char *key,
855 struct security_descriptor **psecdesc)
861 DEBUG(10, ("regdb_get_secdesc: Getting secdesc of key [%s]\n", key));
863 if (asprintf(&tdbkey, "%s/%s", REG_SECDESC_PREFIX, key) == -1) {
866 normalize_dbkey(tdbkey);
868 data = tdb_fetch_bystring(tdb_reg->tdb, tdbkey);
871 if (data.dptr == NULL) {
875 status = unmarshall_sec_desc(mem_ctx, (uint8 *)data.dptr, data.dsize,
878 SAFE_FREE(data.dptr);
880 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MEMORY)) {
884 if (!NT_STATUS_IS_OK(status)) {
885 return WERR_REG_CORRUPT;
891 static WERROR regdb_set_secdesc(const char *key,
892 struct security_descriptor *secdesc)
897 WERROR err = WERR_NOMEM;
900 if (!(mem_ctx = talloc_init("regdb_set_secdesc"))) {
906 if (!(tdbkey = talloc_asprintf(mem_ctx, "%s/%s", REG_SECDESC_PREFIX,
910 normalize_dbkey(tdbkey);
912 if (secdesc == NULL) {
913 /* assuming a delete */
916 tdb_ret = tdb_trans_delete(tdb_reg->tdb,
917 string_term_tdb_data(tdbkey));
919 err = ntstatus_to_werror(map_nt_error_from_unix(errno));
927 err = ntstatus_to_werror(marshall_sec_desc(mem_ctx, secdesc,
930 if (!W_ERROR_IS_OK(err)) {
934 if (tdb_trans_store_bystring(tdb_reg->tdb, tdbkey, tdbdata, 0) == -1) {
935 err = ntstatus_to_werror(map_nt_error_from_unix(errno));
941 TALLOC_FREE(mem_ctx);
945 bool regdb_subkeys_need_update(REGSUBKEY_CTR *subkeys)
947 return (regdb_get_seqnum() != subkeys->seqnum);
950 bool regdb_values_need_update(REGVAL_CTR *values)
952 return (regdb_get_seqnum() != values->seqnum);
956 * Table of function pointers for default access
959 REGISTRY_OPS regdb_ops = {
967 regdb_subkeys_need_update,
968 regdb_values_need_update