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 /* 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 (regdb->transaction_start(regdb) == -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",
121 builtin_registry_paths[i]));
123 path = talloc_strdup(frame, builtin_registry_paths[i]);
124 base = talloc_strdup(frame, "");
125 if (!path || !base) {
130 while (next_token_talloc(frame, &p, &keyname, "\\")) {
132 /* build up the registry path from the components */
135 base = talloc_asprintf(frame, "%s\\", base);
140 base = talloc_asprintf_append(base, "%s", keyname);
145 /* get the immediate subkeyname (if we have one ) */
147 subkeyname = talloc_strdup(frame, "");
152 remaining = talloc_strdup(frame, p);
158 if (!next_token_talloc(frame, &p2,
161 subkeyname = talloc_strdup(frame,p2);
168 DEBUG(10,("init_registry_data: Storing key [%s] with "
169 "subkey [%s]\n", base,
170 *subkeyname ? subkeyname : "NULL"));
172 /* we don't really care if the lookup succeeds or not
173 * since we are about to update the record.
174 * We just want any subkeys already present */
176 if (!(subkeys = TALLOC_ZERO_P(frame, REGSUBKEY_CTR))) {
177 DEBUG(0,("talloc() failure!\n"));
181 regdb_fetch_keys(base, subkeys);
183 regsubkey_ctr_addkey( subkeys, subkeyname);
185 if (!regdb_store_keys( base, subkeys)) {
193 /* loop over all of the predefined values and add each component */
195 frame = talloc_stackframe();
197 for (i=0; builtin_registry_values[i].path != NULL; i++) {
199 if (!(values = TALLOC_ZERO_P(frame, REGVAL_CTR))) {
203 regdb_fetch_values(builtin_registry_values[i].path, values);
205 /* preserve existing values across restarts. Only add new ones */
207 if (!regval_ctr_key_exists(values,
208 builtin_registry_values[i].valuename))
210 switch(builtin_registry_values[i].type) {
212 regval_ctr_addvalue(values,
213 builtin_registry_values[i].valuename,
215 (char*)&builtin_registry_values[i].data.dw_value,
221 builtin_registry_values[i].data.string,
223 regval_ctr_addvalue(values,
224 builtin_registry_values[i].valuename,
227 data.uni_str_len*sizeof(uint16));
231 DEBUG(0, ("init_registry_data: invalid value "
232 "type in builtin_registry_values "
234 builtin_registry_values[i].type));
236 regdb_store_values(builtin_registry_values[i].path,
244 if (regdb->transaction_commit(regdb) == -1) {
245 DEBUG(0, ("init_registry_data: Could not commit "
256 if (regdb->transaction_cancel(regdb) == -1) {
257 smb_panic("init_registry_data: tdb_transaction_cancel "
264 /***********************************************************************
265 Open the registry database
266 ***********************************************************************/
268 bool regdb_init( void )
270 const char *vstring = "INFO/version";
274 DEBUG(10,("regdb_init: incrementing refcount (%d)\n", regdb_refcount));
279 if ( !(regdb = db_open(NULL, state_path("registry.tdb"), 0, REG_TDB_FLAGS, O_RDWR, 0600)) )
281 regdb = db_open(NULL, state_path("registry.tdb"), 0, REG_TDB_FLAGS, O_RDWR|O_CREAT, 0600);
283 DEBUG(0,("regdb_init: Failed to open registry %s (%s)\n",
284 state_path("registry.tdb"), strerror(errno) ));
288 DEBUG(10,("regdb_init: Successfully created registry tdb\n"));
293 vers_id = dbwrap_fetch_int32(regdb, vstring);
295 if ( vers_id != REGVER_V1 ) {
296 /* any upgrade code here if needed */
297 DEBUG(10, ("regdb_init: got INFO/version = %d != %d\n",
298 vers_id, REGVER_V1));
301 /* always setup the necessary keys and values */
303 if ( !init_registry_data() ) {
304 DEBUG(0,("regdb_init: Failed to initialize data in registry!\n"));
311 /***********************************************************************
312 Open the registry. Must already have been initialized by regdb_init()
313 ***********************************************************************/
315 WERROR regdb_open( void )
317 WERROR result = WERR_OK;
320 DEBUG(10,("regdb_open: incrementing refcount (%d)\n", regdb_refcount));
327 regdb = db_open(NULL, state_path("registry.tdb"), 0, REG_TDB_FLAGS, O_RDWR, 0600);
329 result = ntstatus_to_werror( map_nt_error_from_unix( errno ) );
330 DEBUG(0,("regdb_open: Failed to open %s! (%s)\n",
331 state_path("registry.tdb"), strerror(errno) ));
337 DEBUG(10,("regdb_open: refcount reset (%d)\n", regdb_refcount));
342 /***********************************************************************
343 ***********************************************************************/
345 int regdb_close( void )
347 if (regdb_refcount == 0) {
353 DEBUG(10,("regdb_close: decrementing refcount (%d)\n", regdb_refcount));
355 if ( regdb_refcount > 0 )
358 SMB_ASSERT( regdb_refcount >= 0 );
364 /***********************************************************************
365 return the tdb sequence number of the registry tdb.
366 this is an indicator for the content of the registry
367 having changed. it will change upon regdb_init, too, though.
368 ***********************************************************************/
369 int regdb_get_seqnum(void)
371 return regdb->get_seqnum(regdb);
374 /***********************************************************************
375 Add subkey strings to the registry tdb under a defined key
376 fmt is the same format as tdb_pack except this function only supports
378 ***********************************************************************/
380 static bool regdb_store_keys_internal(const char *key, REGSUBKEY_CTR *ctr)
383 uint8 *buffer = NULL;
387 uint32 num_subkeys = regsubkey_ctr_numkeys(ctr);
388 char *keyname = NULL;
389 TALLOC_CTX *ctx = talloc_stackframe();
396 keyname = talloc_strdup(ctx, key);
400 keyname = normalize_reg_path(ctx, keyname);
402 /* allocate some initial memory */
404 buffer = (uint8 *)SMB_MALLOC(1024);
405 if (buffer == NULL) {
411 /* store the number of subkeys */
413 len += tdb_pack(buffer+len, buflen-len, "d", num_subkeys);
415 /* pack all the strings */
417 for (i=0; i<num_subkeys; i++) {
418 len += tdb_pack(buffer+len, buflen-len, "f",
419 regsubkey_ctr_specific_key(ctr, i));
421 /* allocate some extra space */
422 buffer = (uint8 *)SMB_REALLOC(buffer, len*2);
424 DEBUG(0, ("regdb_store_keys: Failed to realloc "
425 "memory of size [%d]\n", len*2));
430 len = tdb_pack(buffer+len, buflen-len, "f",
431 regsubkey_ctr_specific_key(ctr, i));
435 /* finally write out the data */
439 status = dbwrap_store_bystring(regdb, keyname, dbuf, TDB_REPLACE);
440 if (!NT_STATUS_IS_OK(status)) {
451 /***********************************************************************
452 Store the new subkey record and create any child key records that
453 do not currently exist
454 ***********************************************************************/
456 bool regdb_store_keys(const char *key, REGSUBKEY_CTR *ctr)
460 REGSUBKEY_CTR *subkeys = NULL, *old_subkeys = NULL;
461 char *oldkeyname = NULL;
462 TALLOC_CTX *ctx = talloc_stackframe();
466 * fetch a list of the old subkeys so we can determine if anything has
470 if (!(old_subkeys = TALLOC_ZERO_P(ctr, REGSUBKEY_CTR))) {
471 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
475 regdb_fetch_keys(key, old_subkeys);
477 if ((ctr->num_subkeys && old_subkeys->num_subkeys) &&
478 (ctr->num_subkeys == old_subkeys->num_subkeys)) {
480 for (i = 0; i<ctr->num_subkeys; i++) {
481 if (strcmp(ctr->subkeys[i],
482 old_subkeys->subkeys[i]) != 0) {
486 if (i == ctr->num_subkeys) {
488 * Nothing changed, no point to even start a tdb
491 TALLOC_FREE(old_subkeys);
496 if (regdb->transaction_start(regdb) == -1) {
497 DEBUG(0, ("regdb_store_keys: transaction_start failed\n"));
502 * Re-fetch the old keys inside the transaction
505 TALLOC_FREE(old_subkeys);
507 if (!(old_subkeys = TALLOC_ZERO_P(ctr, REGSUBKEY_CTR))) {
508 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
512 regdb_fetch_keys(key, old_subkeys);
514 /* store the subkey list for the parent */
516 if (!regdb_store_keys_internal(key, ctr) ) {
517 DEBUG(0,("regdb_store_keys: Failed to store new subkey list "
518 "for parent [%s]\n", key));
522 /* now delete removed keys */
524 num_subkeys = regsubkey_ctr_numkeys(old_subkeys);
525 for (i=0; i<num_subkeys; i++) {
526 oldkeyname = regsubkey_ctr_specific_key(old_subkeys, i);
528 if (regsubkey_ctr_key_exists(ctr, oldkeyname)) {
530 * It's still around, don't delete
536 path = talloc_asprintf(ctx, "%s/%s", key, oldkeyname);
540 path = normalize_reg_path(ctx, path);
544 status = dbwrap_delete_bystring(regdb, path);
545 if (!NT_STATUS_IS_OK(status)) {
546 DEBUG(1, ("Deleting %s failed\n", path));
551 path = talloc_asprintf(ctx, "%s/%s/%s",
558 path = normalize_reg_path(ctx, path);
564 * Ignore errors here, we might have no values around
566 dbwrap_delete_bystring(regdb, path);
570 TALLOC_FREE(old_subkeys);
572 /* now create records for any subkeys that don't already exist */
574 num_subkeys = regsubkey_ctr_numkeys(ctr);
576 if (num_subkeys == 0) {
577 if (!(subkeys = TALLOC_ZERO_P(ctr, REGSUBKEY_CTR)) ) {
578 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
582 if (!regdb_store_keys_internal(key, subkeys)) {
583 DEBUG(0,("regdb_store_keys: Failed to store "
584 "new record for key [%s]\n", key));
587 TALLOC_FREE(subkeys);
591 for (i=0; i<num_subkeys; i++) {
592 path = talloc_asprintf(ctx, "%s/%s",
594 regsubkey_ctr_specific_key(ctr, i));
598 if (!(subkeys = TALLOC_ZERO_P(ctr, REGSUBKEY_CTR)) ) {
599 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
603 if (regdb_fetch_keys( path, subkeys ) == -1) {
604 /* create a record with 0 subkeys */
605 if (!regdb_store_keys_internal(path, subkeys)) {
606 DEBUG(0,("regdb_store_keys: Failed to store "
607 "new record for key [%s]\n", path));
612 TALLOC_FREE(subkeys);
616 if (regdb->transaction_commit(regdb) == -1) {
617 DEBUG(0, ("regdb_store_keys: Could not commit transaction\n"));
624 TALLOC_FREE(old_subkeys);
625 TALLOC_FREE(subkeys);
628 if (regdb->transaction_cancel(regdb) == -1) {
629 smb_panic("regdb_store_keys: transaction_cancel failed\n");
636 /***********************************************************************
637 Retrieve an array of strings containing subkeys. Memory should be
638 released by the caller.
639 ***********************************************************************/
641 int regdb_fetch_keys(const char *key, REGSUBKEY_CTR *ctr)
650 TALLOC_CTX *frame = talloc_stackframe();
651 struct db_record *rec;
653 DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key ? key : "NULL"));
655 path = talloc_strdup(talloc_tos(), key);
660 /* convert to key format */
661 path = talloc_string_sub(talloc_tos(), path, "\\", "/");
667 rec = regdb->fetch_locked(regdb, frame, string_term_tdb_data(path));
672 ctr->seqnum = regdb_get_seqnum();
674 buf = rec->value.dptr;
675 buflen = rec->value.dsize;
678 DEBUG(5,("regdb_fetch_keys: tdb lookup failed to locate key [%s]\n", key));
682 len = tdb_unpack( buf, buflen, "d", &num_items);
684 for (i=0; i<num_items; i++) {
685 len += tdb_unpack(buf+len, buflen-len, "f", subkeyname);
686 regsubkey_ctr_addkey(ctr, subkeyname);
689 DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items));
697 /****************************************************************************
698 Unpack a list of registry values frem the TDB
699 ***************************************************************************/
701 static int regdb_unpack_values(REGVAL_CTR *values, uint8 *buf, int buflen)
708 uint32 num_values = 0;
711 /* loop and unpack the rest of the registry values */
713 len += tdb_unpack(buf+len, buflen-len, "d", &num_values);
715 for ( i=0; i<num_values; i++ ) {
716 /* unpack the next regval */
722 len += tdb_unpack(buf+len, buflen-len, "fdB",
728 /* add the new value. Paranoid protective code -- make sure data_p is valid */
730 if (*valuename && size && data_p) {
731 regval_ctr_addvalue(values, valuename, type,
732 (const char *)data_p, size);
734 SAFE_FREE(data_p); /* 'B' option to tdb_unpack does a malloc() */
736 DEBUG(8,("specific: [%s], len: %d\n", valuename, size));
742 /****************************************************************************
743 Pack all values in all printer keys
744 ***************************************************************************/
746 static int regdb_pack_values(REGVAL_CTR *values, uint8 *buf, int buflen)
756 num_values = regval_ctr_numvals( values );
758 /* pack the number of values first */
760 len += tdb_pack( buf+len, buflen-len, "d", num_values );
762 /* loop over all values */
764 for ( i=0; i<num_values; i++ ) {
765 val = regval_ctr_specific_value( values, i );
766 len += tdb_pack(buf+len, buflen-len, "fdB",
770 regval_data_p(val) );
776 /***********************************************************************
777 Retrieve an array of strings containing subkeys. Memory should be
778 released by the caller.
779 ***********************************************************************/
781 int regdb_fetch_values( const char* key, REGVAL_CTR *values )
784 TALLOC_CTX *ctx = talloc_stackframe();
785 struct db_record *rec;
788 DEBUG(10,("regdb_fetch_values: Looking for value of key [%s] \n", key));
790 keystr = talloc_asprintf(ctx, "%s/%s", REG_VALUE_PREFIX, key);
794 keystr = normalize_reg_path(ctx, keystr);
799 rec = regdb->fetch_locked(regdb, ctx, string_term_tdb_data(keystr));
804 values->seqnum = regdb_get_seqnum();
806 if (!rec->value.dptr) {
807 /* all keys have zero values by default */
811 regdb_unpack_values(values, rec->value.dptr, rec->value.dsize);
812 ret = regval_ctr_numvals(values);
819 bool regdb_store_values( const char *key, REGVAL_CTR *values )
821 TDB_DATA old_data, data;
823 TALLOC_CTX *ctx = talloc_tos();
826 DEBUG(10,("regdb_store_values: Looking for value of key [%s] \n", key));
830 len = regdb_pack_values(values, data.dptr, data.dsize);
832 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
836 data.dptr = SMB_MALLOC_ARRAY( uint8, len );
839 len = regdb_pack_values(values, data.dptr, data.dsize);
841 SMB_ASSERT( len == data.dsize );
843 keystr = talloc_asprintf(ctx, "%s/%s", REG_VALUE_PREFIX, key );
845 SAFE_FREE(data.dptr);
848 keystr = normalize_reg_path(ctx, keystr);
850 SAFE_FREE(data.dptr);
854 old_data = dbwrap_fetch_bystring(regdb, ctx, keystr);
856 if ((old_data.dptr != NULL)
857 && (old_data.dsize == data.dsize)
858 && (memcmp(old_data.dptr, data.dptr, data.dsize) == 0)) {
859 TALLOC_FREE(old_data.dptr);
860 SAFE_FREE(data.dptr);
864 ret = dbwrap_trans_store(regdb, string_term_tdb_data(keystr), data,
867 TALLOC_FREE( old_data.dptr );
868 SAFE_FREE( data.dptr );
873 static WERROR regdb_get_secdesc(TALLOC_CTX *mem_ctx, const char *key,
874 struct security_descriptor **psecdesc)
879 TALLOC_CTX *tmp_ctx = talloc_stackframe();
881 DEBUG(10, ("regdb_get_secdesc: Getting secdesc of key [%s]\n", key));
883 if (asprintf(&tdbkey, "%s/%s", REG_SECDESC_PREFIX, key) == -1) {
886 normalize_dbkey(tdbkey);
888 data = dbwrap_fetch_bystring(regdb, tmp_ctx, tdbkey);
891 if (data.dptr == NULL) {
895 status = unmarshall_sec_desc(mem_ctx, (uint8 *)data.dptr, data.dsize,
898 TALLOC_FREE(tmp_ctx);
900 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MEMORY)) {
904 if (!NT_STATUS_IS_OK(status)) {
905 return WERR_REG_CORRUPT;
911 static WERROR regdb_set_secdesc(const char *key,
912 struct security_descriptor *secdesc)
917 WERROR err = WERR_NOMEM;
920 if (!(mem_ctx = talloc_init("regdb_set_secdesc"))) {
926 if (!(tdbkey = talloc_asprintf(mem_ctx, "%s/%s", REG_SECDESC_PREFIX,
930 normalize_dbkey(tdbkey);
932 if (secdesc == NULL) {
933 /* assuming a delete */
936 tdb_ret = dbwrap_trans_delete(regdb,
937 string_term_tdb_data(tdbkey));
939 err = ntstatus_to_werror(map_nt_error_from_unix(errno));
947 err = ntstatus_to_werror(marshall_sec_desc(mem_ctx, secdesc,
950 if (!W_ERROR_IS_OK(err)) {
954 if (dbwrap_trans_store(regdb, string_term_tdb_data(tdbkey),
956 err = ntstatus_to_werror(map_nt_error_from_unix(errno));
962 TALLOC_FREE(mem_ctx);
966 bool regdb_subkeys_need_update(REGSUBKEY_CTR *subkeys)
968 return (regdb_get_seqnum() != subkeys->seqnum);
971 bool regdb_values_need_update(REGVAL_CTR *values)
973 return (regdb_get_seqnum() != values->seqnum);
977 * Table of function pointers for default access
980 REGISTRY_OPS regdb_ops = {
988 regdb_subkeys_need_update,
989 regdb_values_need_update