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[] = {
50 KEY_SAMBA_GROUP_POLICY,
51 KEY_GP_MACHINE_POLICY,
52 KEY_GP_MACHINE_WIN_POLICY,
55 KEY_GP_USER_WIN_POLICY,
56 KEY_WINLOGON_GPEXT_PATH,
57 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Print\\Monitors",
59 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\DefaultUserConfiguration",
68 struct builtin_regkey_value {
70 const char *valuename;
78 static struct builtin_regkey_value builtin_registry_values[] = {
80 SAMBA_PRINTER_PORT_NAME, REG_SZ, { "" } },
82 "DefaultSpoolDirectory", REG_SZ, { "C:\\Windows\\System32\\Spool\\Printers" } },
84 "DisplayName", REG_SZ, { "Event Log" } },
86 "ErrorControl", REG_DWORD, { (char*)0x00000001 } },
87 { NULL, NULL, 0, { NULL } }
91 * Initialize a key in the registry:
92 * create each component key of the specified path.
94 static bool init_registry_key_internal(const char *add_path)
97 TALLOC_CTX *frame = talloc_stackframe();
100 char *remaining = NULL;
103 REGSUBKEY_CTR *subkeys;
106 DEBUG(6, ("init_registry_key: Adding [%s]\n", add_path));
108 path = talloc_strdup(frame, add_path);
109 base = talloc_strdup(frame, "");
110 if (!path || !base) {
115 while (next_token_talloc(frame, &p, &keyname, "\\")) {
117 /* build up the registry path from the components */
120 base = talloc_asprintf(frame, "%s\\", base);
125 base = talloc_asprintf_append(base, "%s", keyname);
130 /* get the immediate subkeyname (if we have one ) */
132 subkeyname = talloc_strdup(frame, "");
137 remaining = talloc_strdup(frame, p);
143 if (!next_token_talloc(frame, &p2,
146 subkeyname = talloc_strdup(frame,p2);
153 DEBUG(10,("init_registry_key: Storing key [%s] with "
154 "subkey [%s]\n", base,
155 *subkeyname ? subkeyname : "NULL"));
157 /* we don't really care if the lookup succeeds or not
158 * since we are about to update the record.
159 * We just want any subkeys already present */
161 if (!(subkeys = TALLOC_ZERO_P(frame, REGSUBKEY_CTR))) {
162 DEBUG(0,("talloc() failure!\n"));
166 regdb_fetch_keys(base, subkeys);
168 regsubkey_ctr_addkey( subkeys, subkeyname);
170 if (!regdb_store_keys( base, subkeys)) {
182 * Initialize a key in the registry:
183 * create each component key of the specified path,
184 * wrapped in one db transaction.
186 bool init_registry_key(const char *add_path)
188 if (regdb->transaction_start(regdb) != 0) {
189 DEBUG(0, ("init_registry_key: transaction_start failed\n"));
193 if (!init_registry_key_internal(add_path)) {
197 if (regdb->transaction_commit(regdb) != 0) {
198 DEBUG(0, ("init_registry_key: Could not commit transaction\n"));
205 if (regdb->transaction_cancel(regdb) != 0) {
206 smb_panic("init_registry_key: transaction_cancel failed\n");
212 /***********************************************************************
213 Open the registry data in the tdb
214 ***********************************************************************/
216 bool init_registry_data(void)
218 TALLOC_CTX *frame = NULL;
224 * There are potentially quite a few store operations which are all
225 * indiviually wrapped in tdb transactions. Wrapping them in a single
226 * transaction gives just a single transaction_commit() to actually do
227 * its fsync()s. See tdb/common/transaction.c for info about nested
228 * transaction behaviour.
231 if (regdb->transaction_start(regdb) != 0) {
232 DEBUG(0, ("init_registry_data: tdb_transaction_start "
237 /* loop over all of the predefined paths and add each component */
239 for (i=0; builtin_registry_paths[i] != NULL; i++) {
240 if (!init_registry_key_internal(builtin_registry_paths[i])) {
245 /* loop over all of the predefined values and add each component */
247 frame = talloc_stackframe();
249 for (i=0; builtin_registry_values[i].path != NULL; i++) {
251 values = TALLOC_ZERO_P(frame, REGVAL_CTR);
252 if (values == NULL) {
256 regdb_fetch_values(builtin_registry_values[i].path, values);
258 /* preserve existing values across restarts. Only add new ones */
260 if (!regval_ctr_key_exists(values,
261 builtin_registry_values[i].valuename))
263 switch(builtin_registry_values[i].type) {
265 regval_ctr_addvalue(values,
266 builtin_registry_values[i].valuename,
268 (char*)&builtin_registry_values[i].data.dw_value,
274 builtin_registry_values[i].data.string,
276 regval_ctr_addvalue(values,
277 builtin_registry_values[i].valuename,
280 data.uni_str_len*sizeof(uint16));
284 DEBUG(0, ("init_registry_data: invalid value "
285 "type in builtin_registry_values "
287 builtin_registry_values[i].type));
289 regdb_store_values(builtin_registry_values[i].path,
297 if (regdb->transaction_commit(regdb) != 0) {
298 DEBUG(0, ("init_registry_data: Could not commit "
309 if (regdb->transaction_cancel(regdb) != 0) {
310 smb_panic("init_registry_data: tdb_transaction_cancel "
317 /***********************************************************************
318 Open the registry database
319 ***********************************************************************/
321 bool regdb_init(void)
323 const char *vstring = "INFO/version";
327 DEBUG(10, ("regdb_init: incrementing refcount (%d)\n",
333 regdb = db_open_trans(NULL, state_path("registry.tdb"), 0,
334 REG_TDB_FLAGS, O_RDWR, 0600);
336 regdb = db_open_trans(NULL, state_path("registry.tdb"), 0,
337 REG_TDB_FLAGS, O_RDWR|O_CREAT, 0600);
339 DEBUG(0,("regdb_init: Failed to open registry %s (%s)\n",
340 state_path("registry.tdb"), strerror(errno) ));
344 DEBUG(10,("regdb_init: Successfully created registry tdb\n"));
349 vers_id = dbwrap_fetch_int32(regdb, vstring);
351 if ( vers_id != REGVER_V1 ) {
352 /* any upgrade code here if needed */
353 DEBUG(10, ("regdb_init: got %s = %d != %d\n", vstring,
354 vers_id, REGVER_V1));
355 if (dbwrap_trans_store_int32(regdb, vstring, REGVER_V1) != 0) {
356 DEBUG(0, ("regdb_init: error storing %s = %d\n",
357 vstring, REGVER_V1));
360 DEBUG(10, ("regdb_init: stored %s = %d\n",
361 vstring, REGVER_V1));
368 /***********************************************************************
369 Open the registry. Must already have been initialized by regdb_init()
370 ***********************************************************************/
372 WERROR regdb_open( void )
374 WERROR result = WERR_OK;
377 DEBUG(10,("regdb_open: incrementing refcount (%d)\n", regdb_refcount));
384 regdb = db_open_trans(NULL, state_path("registry.tdb"), 0,
385 REG_TDB_FLAGS, O_RDWR, 0600);
387 result = ntstatus_to_werror( map_nt_error_from_unix( errno ) );
388 DEBUG(0,("regdb_open: Failed to open %s! (%s)\n",
389 state_path("registry.tdb"), strerror(errno) ));
395 DEBUG(10,("regdb_open: refcount reset (%d)\n", regdb_refcount));
400 /***********************************************************************
401 ***********************************************************************/
403 int regdb_close( void )
405 if (regdb_refcount == 0) {
411 DEBUG(10,("regdb_close: decrementing refcount (%d)\n", regdb_refcount));
413 if ( regdb_refcount > 0 )
416 SMB_ASSERT( regdb_refcount >= 0 );
422 /***********************************************************************
423 return the tdb sequence number of the registry tdb.
424 this is an indicator for the content of the registry
425 having changed. it will change upon regdb_init, too, though.
426 ***********************************************************************/
427 int regdb_get_seqnum(void)
429 return regdb->get_seqnum(regdb);
432 /***********************************************************************
433 Add subkey strings to the registry tdb under a defined key
434 fmt is the same format as tdb_pack except this function only supports
436 ***********************************************************************/
438 static bool regdb_store_keys_internal(const char *key, REGSUBKEY_CTR *ctr)
441 uint8 *buffer = NULL;
445 uint32 num_subkeys = regsubkey_ctr_numkeys(ctr);
446 char *keyname = NULL;
447 TALLOC_CTX *ctx = talloc_stackframe();
454 keyname = talloc_strdup(ctx, key);
458 keyname = normalize_reg_path(ctx, keyname);
460 /* allocate some initial memory */
462 buffer = (uint8 *)SMB_MALLOC(1024);
463 if (buffer == NULL) {
469 /* store the number of subkeys */
471 len += tdb_pack(buffer+len, buflen-len, "d", num_subkeys);
473 /* pack all the strings */
475 for (i=0; i<num_subkeys; i++) {
476 len += tdb_pack(buffer+len, buflen-len, "f",
477 regsubkey_ctr_specific_key(ctr, i));
479 /* allocate some extra space */
480 buffer = (uint8 *)SMB_REALLOC(buffer, len*2);
482 DEBUG(0, ("regdb_store_keys: Failed to realloc "
483 "memory of size [%d]\n", len*2));
488 len = tdb_pack(buffer+len, buflen-len, "f",
489 regsubkey_ctr_specific_key(ctr, i));
493 /* finally write out the data */
497 status = dbwrap_store_bystring(regdb, keyname, dbuf, TDB_REPLACE);
498 if (!NT_STATUS_IS_OK(status)) {
509 /***********************************************************************
510 Store the new subkey record and create any child key records that
511 do not currently exist
512 ***********************************************************************/
514 bool regdb_store_keys(const char *key, REGSUBKEY_CTR *ctr)
518 REGSUBKEY_CTR *subkeys = NULL, *old_subkeys = NULL;
519 char *oldkeyname = NULL;
520 TALLOC_CTX *ctx = talloc_stackframe();
524 * fetch a list of the old subkeys so we can determine if anything has
528 if (!(old_subkeys = TALLOC_ZERO_P(ctx, REGSUBKEY_CTR))) {
529 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
533 regdb_fetch_keys(key, old_subkeys);
535 if ((ctr->num_subkeys && old_subkeys->num_subkeys) &&
536 (ctr->num_subkeys == old_subkeys->num_subkeys)) {
538 for (i = 0; i<ctr->num_subkeys; i++) {
539 if (strcmp(ctr->subkeys[i],
540 old_subkeys->subkeys[i]) != 0) {
544 if (i == ctr->num_subkeys) {
546 * Nothing changed, no point to even start a tdb
549 TALLOC_FREE(old_subkeys);
554 TALLOC_FREE(old_subkeys);
556 if (regdb->transaction_start(regdb) != 0) {
557 DEBUG(0, ("regdb_store_keys: transaction_start failed\n"));
562 * Re-fetch the old keys inside the transaction
565 if (!(old_subkeys = TALLOC_ZERO_P(ctx, REGSUBKEY_CTR))) {
566 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
570 regdb_fetch_keys(key, old_subkeys);
572 /* store the subkey list for the parent */
574 if (!regdb_store_keys_internal(key, ctr) ) {
575 DEBUG(0,("regdb_store_keys: Failed to store new subkey list "
576 "for parent [%s]\n", key));
580 /* now delete removed keys */
582 num_subkeys = regsubkey_ctr_numkeys(old_subkeys);
583 for (i=0; i<num_subkeys; i++) {
584 oldkeyname = regsubkey_ctr_specific_key(old_subkeys, i);
586 if (regsubkey_ctr_key_exists(ctr, oldkeyname)) {
588 * It's still around, don't delete
594 path = talloc_asprintf(ctx, "%s/%s", key, oldkeyname);
598 path = normalize_reg_path(ctx, path);
602 status = dbwrap_delete_bystring(regdb, path);
603 if (!NT_STATUS_IS_OK(status)) {
604 DEBUG(1, ("Deleting %s failed\n", path));
609 path = talloc_asprintf(ctx, "%s/%s/%s",
616 path = normalize_reg_path(ctx, path);
622 * Ignore errors here, we might have no values around
624 dbwrap_delete_bystring(regdb, path);
628 TALLOC_FREE(old_subkeys);
630 /* now create records for any subkeys that don't already exist */
632 num_subkeys = regsubkey_ctr_numkeys(ctr);
634 if (num_subkeys == 0) {
635 if (!(subkeys = TALLOC_ZERO_P(ctx, REGSUBKEY_CTR)) ) {
636 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
640 if (!regdb_store_keys_internal(key, subkeys)) {
641 DEBUG(0,("regdb_store_keys: Failed to store "
642 "new record for key [%s]\n", key));
645 TALLOC_FREE(subkeys);
649 for (i=0; i<num_subkeys; i++) {
650 path = talloc_asprintf(ctx, "%s/%s",
652 regsubkey_ctr_specific_key(ctr, i));
656 if (!(subkeys = TALLOC_ZERO_P(ctx, REGSUBKEY_CTR)) ) {
657 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
661 if (regdb_fetch_keys( path, subkeys ) == -1) {
662 /* create a record with 0 subkeys */
663 if (!regdb_store_keys_internal(path, subkeys)) {
664 DEBUG(0,("regdb_store_keys: Failed to store "
665 "new record for key [%s]\n", path));
670 TALLOC_FREE(subkeys);
674 if (regdb->transaction_commit(regdb) != 0) {
675 DEBUG(0, ("regdb_store_keys: Could not commit transaction\n"));
683 if (regdb->transaction_cancel(regdb) != 0) {
684 smb_panic("regdb_store_keys: transaction_cancel failed\n");
694 /***********************************************************************
695 Retrieve an array of strings containing subkeys. Memory should be
696 released by the caller.
697 ***********************************************************************/
699 int regdb_fetch_keys(const char *key, REGSUBKEY_CTR *ctr)
709 TALLOC_CTX *frame = talloc_stackframe();
712 DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key ? key : "NULL"));
714 path = talloc_strdup(frame, key);
719 /* convert to key format */
720 path = talloc_string_sub(frame, path, "\\", "/");
726 dbret = regdb->fetch(regdb, frame, string_term_tdb_data(path), &value);
732 ctr->seqnum = regdb_get_seqnum();
735 buflen = value.dsize;
738 DEBUG(5,("regdb_fetch_keys: tdb lookup failed to locate key [%s]\n", key));
742 len = tdb_unpack( buf, buflen, "d", &num_items);
744 for (i=0; i<num_items; i++) {
745 len += tdb_unpack(buf+len, buflen-len, "f", subkeyname);
746 regsubkey_ctr_addkey(ctr, subkeyname);
749 DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items));
757 /****************************************************************************
758 Unpack a list of registry values frem the TDB
759 ***************************************************************************/
761 static int regdb_unpack_values(REGVAL_CTR *values, uint8 *buf, int buflen)
768 uint32 num_values = 0;
771 /* loop and unpack the rest of the registry values */
773 len += tdb_unpack(buf+len, buflen-len, "d", &num_values);
775 for ( i=0; i<num_values; i++ ) {
776 /* unpack the next regval */
782 len += tdb_unpack(buf+len, buflen-len, "fdB",
788 /* add the new value. Paranoid protective code -- make sure data_p is valid */
790 if (*valuename && size && data_p) {
791 regval_ctr_addvalue(values, valuename, type,
792 (const char *)data_p, size);
794 SAFE_FREE(data_p); /* 'B' option to tdb_unpack does a malloc() */
796 DEBUG(8,("specific: [%s], len: %d\n", valuename, size));
802 /****************************************************************************
803 Pack all values in all printer keys
804 ***************************************************************************/
806 static int regdb_pack_values(REGVAL_CTR *values, uint8 *buf, int buflen)
816 num_values = regval_ctr_numvals( values );
818 /* pack the number of values first */
820 len += tdb_pack( buf+len, buflen-len, "d", num_values );
822 /* loop over all values */
824 for ( i=0; i<num_values; i++ ) {
825 val = regval_ctr_specific_value( values, i );
826 len += tdb_pack(buf+len, buflen-len, "fdB",
830 regval_data_p(val) );
836 /***********************************************************************
837 Retrieve an array of strings containing subkeys. Memory should be
838 released by the caller.
839 ***********************************************************************/
841 int regdb_fetch_values( const char* key, REGVAL_CTR *values )
844 TALLOC_CTX *ctx = talloc_stackframe();
849 DEBUG(10,("regdb_fetch_values: Looking for value of key [%s] \n", key));
851 keystr = talloc_asprintf(ctx, "%s/%s", REG_VALUE_PREFIX, key);
855 keystr = normalize_reg_path(ctx, keystr);
860 dbret = regdb->fetch(regdb, ctx, string_term_tdb_data(keystr), &value);
865 values->seqnum = regdb_get_seqnum();
868 /* all keys have zero values by default */
872 regdb_unpack_values(values, value.dptr, value.dsize);
873 ret = regval_ctr_numvals(values);
880 bool regdb_store_values( const char *key, REGVAL_CTR *values )
882 TDB_DATA old_data, data;
884 TALLOC_CTX *ctx = talloc_stackframe();
888 DEBUG(10,("regdb_store_values: Looking for value of key [%s] \n", key));
892 len = regdb_pack_values(values, data.dptr, data.dsize);
894 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
898 data.dptr = TALLOC_ARRAY(ctx, uint8, len);
901 len = regdb_pack_values(values, data.dptr, data.dsize);
903 SMB_ASSERT( len == data.dsize );
905 keystr = talloc_asprintf(ctx, "%s/%s", REG_VALUE_PREFIX, key );
909 keystr = normalize_reg_path(ctx, keystr);
914 old_data = dbwrap_fetch_bystring(regdb, ctx, keystr);
916 if ((old_data.dptr != NULL)
917 && (old_data.dsize == data.dsize)
918 && (memcmp(old_data.dptr, data.dptr, data.dsize) == 0))
924 ret = dbwrap_trans_store(regdb, string_term_tdb_data(keystr), data,
926 result = (ret != -1);
933 static WERROR regdb_get_secdesc(TALLOC_CTX *mem_ctx, const char *key,
934 struct security_descriptor **psecdesc)
939 TALLOC_CTX *tmp_ctx = talloc_stackframe();
940 WERROR err = WERR_OK;
942 DEBUG(10, ("regdb_get_secdesc: Getting secdesc of key [%s]\n", key));
944 tdbkey = talloc_asprintf(tmp_ctx, "%s/%s", REG_SECDESC_PREFIX, key);
945 if (tdbkey == NULL) {
949 normalize_dbkey(tdbkey);
951 data = dbwrap_fetch_bystring(regdb, tmp_ctx, tdbkey);
952 if (data.dptr == NULL) {
957 status = unmarshall_sec_desc(mem_ctx, (uint8 *)data.dptr, data.dsize,
960 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MEMORY)) {
962 } else if (!NT_STATUS_IS_OK(status)) {
963 err = WERR_REG_CORRUPT;
967 TALLOC_FREE(tmp_ctx);
971 static WERROR regdb_set_secdesc(const char *key,
972 struct security_descriptor *secdesc)
974 TALLOC_CTX *mem_ctx = talloc_stackframe();
976 WERROR err = WERR_NOMEM;
980 tdbkey = talloc_asprintf(mem_ctx, "%s/%s", REG_SECDESC_PREFIX, key);
981 if (tdbkey == NULL) {
984 normalize_dbkey(tdbkey);
986 if (secdesc == NULL) {
987 /* assuming a delete */
988 tdb_ret = dbwrap_trans_delete(regdb,
989 string_term_tdb_data(tdbkey));
993 err = ntstatus_to_werror(map_nt_error_from_unix(errno));
998 err = ntstatus_to_werror(marshall_sec_desc(mem_ctx, secdesc,
1001 if (!W_ERROR_IS_OK(err)) {
1005 tdb_ret = dbwrap_trans_store(regdb, string_term_tdb_data(tdbkey),
1008 err = ntstatus_to_werror(map_nt_error_from_unix(errno));
1013 TALLOC_FREE(mem_ctx);
1017 bool regdb_subkeys_need_update(REGSUBKEY_CTR *subkeys)
1019 return (regdb_get_seqnum() != subkeys->seqnum);
1022 bool regdb_values_need_update(REGVAL_CTR *values)
1024 return (regdb_get_seqnum() != values->seqnum);
1028 * Table of function pointers for default access
1031 REGISTRY_OPS regdb_ops = {
1032 .fetch_subkeys = regdb_fetch_keys,
1033 .fetch_values = regdb_fetch_values,
1034 .store_subkeys = regdb_store_keys,
1035 .store_values = regdb_store_values,
1036 .get_secdesc = regdb_get_secdesc,
1037 .set_secdesc = regdb_set_secdesc,
1038 .subkeys_need_update = regdb_subkeys_need_update,
1039 .values_need_update = regdb_values_need_update