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 WERROR 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) {
116 while (next_token_talloc(frame, &p, &keyname, "\\")) {
118 /* build up the registry path from the components */
121 base = talloc_asprintf(frame, "%s\\", base);
127 base = talloc_asprintf_append(base, "%s", keyname);
133 /* get the immediate subkeyname (if we have one ) */
135 subkeyname = talloc_strdup(frame, "");
141 remaining = talloc_strdup(frame, p);
148 if (!next_token_talloc(frame, &p2,
151 subkeyname = talloc_strdup(frame,p2);
159 DEBUG(10,("init_registry_key: Storing key [%s] with "
160 "subkey [%s]\n", base,
161 *subkeyname ? subkeyname : "NULL"));
163 /* we don't really care if the lookup succeeds or not
164 * since we are about to update the record.
165 * We just want any subkeys already present */
167 if (!(subkeys = TALLOC_ZERO_P(frame, REGSUBKEY_CTR))) {
168 DEBUG(0,("talloc() failure!\n"));
173 regdb_fetch_keys(base, subkeys);
175 werr = regsubkey_ctr_addkey(subkeys, subkeyname);
176 if (!W_ERROR_IS_OK(werr)) {
180 if (!regdb_store_keys( base, subkeys)) {
181 werr = WERR_CAN_NOT_COMPLETE;
194 * Initialize a key in the registry:
195 * create each component key of the specified path,
196 * wrapped in one db transaction.
198 WERROR init_registry_key(const char *add_path)
202 if (regdb->transaction_start(regdb) != 0) {
203 DEBUG(0, ("init_registry_key: transaction_start failed\n"));
204 return WERR_REG_IO_FAILURE;
207 werr = init_registry_key_internal(add_path);
208 if (!W_ERROR_IS_OK(werr)) {
212 if (regdb->transaction_commit(regdb) != 0) {
213 DEBUG(0, ("init_registry_key: Could not commit transaction\n"));
214 return WERR_REG_IO_FAILURE;
220 if (regdb->transaction_cancel(regdb) != 0) {
221 smb_panic("init_registry_key: transaction_cancel failed\n");
227 /***********************************************************************
228 Open the registry data in the tdb
229 ***********************************************************************/
231 WERROR init_registry_data(void)
234 TALLOC_CTX *frame = NULL;
240 * There are potentially quite a few store operations which are all
241 * indiviually wrapped in tdb transactions. Wrapping them in a single
242 * transaction gives just a single transaction_commit() to actually do
243 * its fsync()s. See tdb/common/transaction.c for info about nested
244 * transaction behaviour.
247 if (regdb->transaction_start(regdb) != 0) {
248 DEBUG(0, ("init_registry_data: tdb_transaction_start "
250 return WERR_REG_IO_FAILURE;
253 /* loop over all of the predefined paths and add each component */
255 for (i=0; builtin_registry_paths[i] != NULL; i++) {
256 werr = init_registry_key_internal(builtin_registry_paths[i]);
257 if (!W_ERROR_IS_OK(werr)) {
262 /* loop over all of the predefined values and add each component */
264 frame = talloc_stackframe();
266 for (i=0; builtin_registry_values[i].path != NULL; i++) {
268 values = TALLOC_ZERO_P(frame, REGVAL_CTR);
269 if (values == NULL) {
274 regdb_fetch_values(builtin_registry_values[i].path, values);
276 /* preserve existing values across restarts. Only add new ones */
278 if (!regval_ctr_key_exists(values,
279 builtin_registry_values[i].valuename))
281 switch(builtin_registry_values[i].type) {
283 regval_ctr_addvalue(values,
284 builtin_registry_values[i].valuename,
286 (char*)&builtin_registry_values[i].data.dw_value,
292 builtin_registry_values[i].data.string,
294 regval_ctr_addvalue(values,
295 builtin_registry_values[i].valuename,
298 data.uni_str_len*sizeof(uint16));
302 DEBUG(0, ("init_registry_data: invalid value "
303 "type in builtin_registry_values "
305 builtin_registry_values[i].type));
307 regdb_store_values(builtin_registry_values[i].path,
315 if (regdb->transaction_commit(regdb) != 0) {
316 DEBUG(0, ("init_registry_data: Could not commit "
318 return WERR_REG_IO_FAILURE;
327 if (regdb->transaction_cancel(regdb) != 0) {
328 smb_panic("init_registry_data: tdb_transaction_cancel "
335 /***********************************************************************
336 Open the registry database
337 ***********************************************************************/
339 WERROR regdb_init(void)
341 const char *vstring = "INFO/version";
346 DEBUG(10, ("regdb_init: incrementing refcount (%d)\n",
352 regdb = db_open_trans(NULL, state_path("registry.tdb"), 0,
353 REG_TDB_FLAGS, O_RDWR, 0600);
355 regdb = db_open_trans(NULL, state_path("registry.tdb"), 0,
356 REG_TDB_FLAGS, O_RDWR|O_CREAT, 0600);
358 werr = ntstatus_to_werror(map_nt_error_from_unix(errno));
359 DEBUG(0,("regdb_init: Failed to open registry %s (%s)\n",
360 state_path("registry.tdb"), strerror(errno) ));
364 DEBUG(10,("regdb_init: Successfully created registry tdb\n"));
369 vers_id = dbwrap_fetch_int32(regdb, vstring);
371 if ( vers_id != REGVER_V1 ) {
373 /* any upgrade code here if needed */
374 DEBUG(10, ("regdb_init: got %s = %d != %d\n", vstring,
375 vers_id, REGVER_V1));
376 status = dbwrap_trans_store_int32(regdb, vstring, REGVER_V1);
377 if (!NT_STATUS_IS_OK(status)) {
378 DEBUG(0, ("regdb_init: error storing %s = %d: %s\n",
379 vstring, REGVER_V1, nt_errstr(status)));
380 return ntstatus_to_werror(status);
382 DEBUG(10, ("regdb_init: stored %s = %d\n",
383 vstring, REGVER_V1));
390 /***********************************************************************
391 Open the registry. Must already have been initialized by regdb_init()
392 ***********************************************************************/
394 WERROR regdb_open( void )
396 WERROR result = WERR_OK;
399 DEBUG(10,("regdb_open: incrementing refcount (%d)\n", regdb_refcount));
406 regdb = db_open_trans(NULL, state_path("registry.tdb"), 0,
407 REG_TDB_FLAGS, O_RDWR, 0600);
409 result = ntstatus_to_werror( map_nt_error_from_unix( errno ) );
410 DEBUG(0,("regdb_open: Failed to open %s! (%s)\n",
411 state_path("registry.tdb"), strerror(errno) ));
417 DEBUG(10,("regdb_open: refcount reset (%d)\n", regdb_refcount));
422 /***********************************************************************
423 ***********************************************************************/
425 int regdb_close( void )
427 if (regdb_refcount == 0) {
433 DEBUG(10,("regdb_close: decrementing refcount (%d)\n", regdb_refcount));
435 if ( regdb_refcount > 0 )
438 SMB_ASSERT( regdb_refcount >= 0 );
444 /***********************************************************************
445 return the tdb sequence number of the registry tdb.
446 this is an indicator for the content of the registry
447 having changed. it will change upon regdb_init, too, though.
448 ***********************************************************************/
449 int regdb_get_seqnum(void)
451 return regdb->get_seqnum(regdb);
454 /***********************************************************************
455 Add subkey strings to the registry tdb under a defined key
456 fmt is the same format as tdb_pack except this function only supports
458 ***********************************************************************/
460 static bool regdb_store_keys_internal(const char *key, REGSUBKEY_CTR *ctr)
463 uint8 *buffer = NULL;
467 uint32 num_subkeys = regsubkey_ctr_numkeys(ctr);
468 char *keyname = NULL;
469 TALLOC_CTX *ctx = talloc_stackframe();
476 keyname = talloc_strdup(ctx, key);
480 keyname = normalize_reg_path(ctx, keyname);
482 /* allocate some initial memory */
484 buffer = (uint8 *)SMB_MALLOC(1024);
485 if (buffer == NULL) {
491 /* store the number of subkeys */
493 len += tdb_pack(buffer+len, buflen-len, "d", num_subkeys);
495 /* pack all the strings */
497 for (i=0; i<num_subkeys; i++) {
498 len += tdb_pack(buffer+len, buflen-len, "f",
499 regsubkey_ctr_specific_key(ctr, i));
501 /* allocate some extra space */
502 buffer = (uint8 *)SMB_REALLOC(buffer, len*2);
504 DEBUG(0, ("regdb_store_keys: Failed to realloc "
505 "memory of size [%d]\n", len*2));
510 len = tdb_pack(buffer+len, buflen-len, "f",
511 regsubkey_ctr_specific_key(ctr, i));
515 /* finally write out the data */
519 status = dbwrap_store_bystring(regdb, keyname, dbuf, TDB_REPLACE);
520 if (!NT_STATUS_IS_OK(status)) {
531 /***********************************************************************
532 Store the new subkey record and create any child key records that
533 do not currently exist
534 ***********************************************************************/
536 bool regdb_store_keys(const char *key, REGSUBKEY_CTR *ctr)
540 REGSUBKEY_CTR *subkeys = NULL, *old_subkeys = NULL;
541 char *oldkeyname = NULL;
542 TALLOC_CTX *ctx = talloc_stackframe();
546 * fetch a list of the old subkeys so we can determine if anything has
550 if (!(old_subkeys = TALLOC_ZERO_P(ctx, REGSUBKEY_CTR))) {
551 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
555 regdb_fetch_keys(key, old_subkeys);
557 if ((ctr->num_subkeys && old_subkeys->num_subkeys) &&
558 (ctr->num_subkeys == old_subkeys->num_subkeys)) {
560 for (i = 0; i<ctr->num_subkeys; i++) {
561 if (strcmp(ctr->subkeys[i],
562 old_subkeys->subkeys[i]) != 0) {
566 if (i == ctr->num_subkeys) {
568 * Nothing changed, no point to even start a tdb
571 TALLOC_FREE(old_subkeys);
576 TALLOC_FREE(old_subkeys);
578 if (regdb->transaction_start(regdb) != 0) {
579 DEBUG(0, ("regdb_store_keys: transaction_start failed\n"));
584 * Re-fetch the old keys inside the transaction
587 if (!(old_subkeys = TALLOC_ZERO_P(ctx, REGSUBKEY_CTR))) {
588 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
592 regdb_fetch_keys(key, old_subkeys);
594 /* store the subkey list for the parent */
596 if (!regdb_store_keys_internal(key, ctr) ) {
597 DEBUG(0,("regdb_store_keys: Failed to store new subkey list "
598 "for parent [%s]\n", key));
602 /* now delete removed keys */
604 num_subkeys = regsubkey_ctr_numkeys(old_subkeys);
605 for (i=0; i<num_subkeys; i++) {
606 oldkeyname = regsubkey_ctr_specific_key(old_subkeys, i);
608 if (regsubkey_ctr_key_exists(ctr, oldkeyname)) {
610 * It's still around, don't delete
616 path = talloc_asprintf(ctx, "%s/%s", key, oldkeyname);
620 path = normalize_reg_path(ctx, path);
624 status = dbwrap_delete_bystring(regdb, path);
625 if (!NT_STATUS_IS_OK(status)) {
626 DEBUG(1, ("Deleting %s failed\n", path));
631 path = talloc_asprintf(ctx, "%s/%s/%s",
638 path = normalize_reg_path(ctx, path);
644 * Ignore errors here, we might have no values around
646 dbwrap_delete_bystring(regdb, path);
650 TALLOC_FREE(old_subkeys);
652 /* now create records for any subkeys that don't already exist */
654 num_subkeys = regsubkey_ctr_numkeys(ctr);
656 if (num_subkeys == 0) {
657 if (!(subkeys = TALLOC_ZERO_P(ctx, REGSUBKEY_CTR)) ) {
658 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
662 if (!regdb_store_keys_internal(key, subkeys)) {
663 DEBUG(0,("regdb_store_keys: Failed to store "
664 "new record for key [%s]\n", key));
667 TALLOC_FREE(subkeys);
671 for (i=0; i<num_subkeys; i++) {
672 path = talloc_asprintf(ctx, "%s/%s",
674 regsubkey_ctr_specific_key(ctr, i));
678 if (!(subkeys = TALLOC_ZERO_P(ctx, REGSUBKEY_CTR)) ) {
679 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
683 if (regdb_fetch_keys( path, subkeys ) == -1) {
684 /* create a record with 0 subkeys */
685 if (!regdb_store_keys_internal(path, subkeys)) {
686 DEBUG(0,("regdb_store_keys: Failed to store "
687 "new record for key [%s]\n", path));
692 TALLOC_FREE(subkeys);
696 if (regdb->transaction_commit(regdb) != 0) {
697 DEBUG(0, ("regdb_store_keys: Could not commit transaction\n"));
705 if (regdb->transaction_cancel(regdb) != 0) {
706 smb_panic("regdb_store_keys: transaction_cancel failed\n");
716 /***********************************************************************
717 Retrieve an array of strings containing subkeys. Memory should be
718 released by the caller.
719 ***********************************************************************/
721 int regdb_fetch_keys(const char *key, REGSUBKEY_CTR *ctr)
731 TALLOC_CTX *frame = talloc_stackframe();
734 DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key ? key : "NULL"));
736 path = talloc_strdup(frame, key);
741 /* convert to key format */
742 path = normalize_reg_path(frame, path);
747 ctr->seqnum = regdb_get_seqnum();
749 value = dbwrap_fetch_bystring(regdb, frame, path);
752 buflen = value.dsize;
755 DEBUG(5,("regdb_fetch_keys: tdb lookup failed to locate key [%s]\n", key));
759 len = tdb_unpack( buf, buflen, "d", &num_items);
761 for (i=0; i<num_items; i++) {
762 len += tdb_unpack(buf+len, buflen-len, "f", subkeyname);
763 werr = regsubkey_ctr_addkey(ctr, subkeyname);
764 if (!W_ERROR_IS_OK(werr)) {
765 DEBUG(5, ("regdb_fetch_keys: regsubkey_ctr_addkey "
766 "failed: %s\n", dos_errstr(werr)));
771 DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items));
779 /****************************************************************************
780 Unpack a list of registry values frem the TDB
781 ***************************************************************************/
783 static int regdb_unpack_values(REGVAL_CTR *values, uint8 *buf, int buflen)
790 uint32 num_values = 0;
793 /* loop and unpack the rest of the registry values */
795 len += tdb_unpack(buf+len, buflen-len, "d", &num_values);
797 for ( i=0; i<num_values; i++ ) {
798 /* unpack the next regval */
804 len += tdb_unpack(buf+len, buflen-len, "fdB",
810 /* add the new value. Paranoid protective code -- make sure data_p is valid */
812 if (*valuename && size && data_p) {
813 regval_ctr_addvalue(values, valuename, type,
814 (const char *)data_p, size);
816 SAFE_FREE(data_p); /* 'B' option to tdb_unpack does a malloc() */
818 DEBUG(8,("specific: [%s], len: %d\n", valuename, size));
824 /****************************************************************************
825 Pack all values in all printer keys
826 ***************************************************************************/
828 static int regdb_pack_values(REGVAL_CTR *values, uint8 *buf, int buflen)
838 num_values = regval_ctr_numvals( values );
840 /* pack the number of values first */
842 len += tdb_pack( buf+len, buflen-len, "d", num_values );
844 /* loop over all values */
846 for ( i=0; i<num_values; i++ ) {
847 val = regval_ctr_specific_value( values, i );
848 len += tdb_pack(buf+len, buflen-len, "fdB",
852 regval_data_p(val) );
858 /***********************************************************************
859 Retrieve an array of strings containing subkeys. Memory should be
860 released by the caller.
861 ***********************************************************************/
863 int regdb_fetch_values( const char* key, REGVAL_CTR *values )
866 TALLOC_CTX *ctx = talloc_stackframe();
870 DEBUG(10,("regdb_fetch_values: Looking for value of key [%s] \n", key));
872 keystr = talloc_asprintf(ctx, "%s/%s", REG_VALUE_PREFIX, key);
876 keystr = normalize_reg_path(ctx, keystr);
881 values->seqnum = regdb_get_seqnum();
883 value = dbwrap_fetch_bystring(regdb, ctx, keystr);
886 /* all keys have zero values by default */
890 regdb_unpack_values(values, value.dptr, value.dsize);
891 ret = regval_ctr_numvals(values);
898 bool regdb_store_values( const char *key, REGVAL_CTR *values )
900 TDB_DATA old_data, data;
902 TALLOC_CTX *ctx = talloc_stackframe();
907 DEBUG(10,("regdb_store_values: Looking for value of key [%s] \n", key));
911 len = regdb_pack_values(values, data.dptr, data.dsize);
913 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
917 data.dptr = TALLOC_ARRAY(ctx, uint8, len);
920 len = regdb_pack_values(values, data.dptr, data.dsize);
922 SMB_ASSERT( len == data.dsize );
924 keystr = talloc_asprintf(ctx, "%s/%s", REG_VALUE_PREFIX, key );
928 keystr = normalize_reg_path(ctx, keystr);
933 old_data = dbwrap_fetch_bystring(regdb, ctx, keystr);
935 if ((old_data.dptr != NULL)
936 && (old_data.dsize == data.dsize)
937 && (memcmp(old_data.dptr, data.dptr, data.dsize) == 0))
943 status = dbwrap_trans_store(regdb, string_term_tdb_data(keystr), data,
946 result = NT_STATUS_IS_OK(status);
953 static WERROR regdb_get_secdesc(TALLOC_CTX *mem_ctx, const char *key,
954 struct security_descriptor **psecdesc)
959 TALLOC_CTX *tmp_ctx = talloc_stackframe();
960 WERROR err = WERR_OK;
962 DEBUG(10, ("regdb_get_secdesc: Getting secdesc of key [%s]\n", key));
964 tdbkey = talloc_asprintf(tmp_ctx, "%s/%s", REG_SECDESC_PREFIX, key);
965 if (tdbkey == NULL) {
969 normalize_dbkey(tdbkey);
971 data = dbwrap_fetch_bystring(regdb, tmp_ctx, tdbkey);
972 if (data.dptr == NULL) {
977 status = unmarshall_sec_desc(mem_ctx, (uint8 *)data.dptr, data.dsize,
980 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MEMORY)) {
982 } else if (!NT_STATUS_IS_OK(status)) {
983 err = WERR_REG_CORRUPT;
987 TALLOC_FREE(tmp_ctx);
991 static WERROR regdb_set_secdesc(const char *key,
992 struct security_descriptor *secdesc)
994 TALLOC_CTX *mem_ctx = talloc_stackframe();
997 WERROR err = WERR_NOMEM;
1000 tdbkey = talloc_asprintf(mem_ctx, "%s/%s", REG_SECDESC_PREFIX, key);
1001 if (tdbkey == NULL) {
1004 normalize_dbkey(tdbkey);
1006 if (secdesc == NULL) {
1007 /* assuming a delete */
1008 status = dbwrap_trans_delete(regdb,
1009 string_term_tdb_data(tdbkey));
1010 if (NT_STATUS_IS_OK(status)) {
1013 err = ntstatus_to_werror(status);
1018 err = ntstatus_to_werror(marshall_sec_desc(mem_ctx, secdesc,
1021 if (!W_ERROR_IS_OK(err)) {
1025 status = dbwrap_trans_store(regdb, string_term_tdb_data(tdbkey),
1027 if (!NT_STATUS_IS_OK(status)) {
1028 err = ntstatus_to_werror(status);
1033 TALLOC_FREE(mem_ctx);
1037 bool regdb_subkeys_need_update(REGSUBKEY_CTR *subkeys)
1039 return (regdb_get_seqnum() != subkeys->seqnum);
1042 bool regdb_values_need_update(REGVAL_CTR *values)
1044 return (regdb_get_seqnum() != values->seqnum);
1048 * Table of function pointers for default access
1051 REGISTRY_OPS regdb_ops = {
1052 .fetch_subkeys = regdb_fetch_keys,
1053 .fetch_values = regdb_fetch_values,
1054 .store_subkeys = regdb_store_keys,
1055 .store_values = regdb_store_values,
1056 .get_secdesc = regdb_get_secdesc,
1057 .set_secdesc = regdb_set_secdesc,
1058 .subkeys_need_update = regdb_subkeys_need_update,
1059 .values_need_update = regdb_values_need_update