docs:smbdotconf: fix type of "preferred master" parameter.
[Samba.git] / source3 / registry / reg_backend_db.c
blob7b3391deb1e96619c68bdc22cac6efcbc17e28a8
1 /*
2 * Unix SMB/CIFS implementation.
3 * Virtual Windows Registry Layer
4 * Copyright (C) Gerald Carter 2002-2005
5 * Copyright (C) Michael Adam 2007-2011
6 * Copyright (C) Gregor Beck 2011
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 /* Implementation of internal registry database functions. */
24 #include "includes.h"
25 #include "system/filesys.h"
26 #include "registry.h"
27 #include "reg_db.h"
28 #include "reg_util_internal.h"
29 #include "reg_parse_internal.h"
30 #include "reg_backend_db.h"
31 #include "reg_objects.h"
32 #include "nt_printing.h"
33 #include "util_tdb.h"
34 #include "dbwrap/dbwrap.h"
35 #include "dbwrap/dbwrap_open.h"
36 #include "../libcli/security/secdesc.h"
38 #undef DBGC_CLASS
39 #define DBGC_CLASS DBGC_REGISTRY
41 #define REGDB_VERSION_KEYNAME "INFO/version"
43 static struct db_context *regdb = NULL;
44 static int regdb_refcount;
46 static bool regdb_key_exists(struct db_context *db, const char *key);
47 static WERROR regdb_fetch_keys_internal(struct db_context *db, const char *key,
48 struct regsubkey_ctr *ctr);
49 static bool regdb_store_keys_internal(struct db_context *db, const char *key,
50 struct regsubkey_ctr *ctr);
51 static int regdb_fetch_values_internal(struct db_context *db, const char* key,
52 struct regval_ctr *values);
53 static NTSTATUS regdb_store_values_internal(struct db_context *db, const char *key,
54 struct regval_ctr *values);
55 static WERROR regdb_store_subkey_list(struct db_context *db, const char *parent,
56 const char *key);
58 static WERROR regdb_create_basekey(struct db_context *db, const char *key);
59 static WERROR regdb_create_subkey_internal(struct db_context *db,
60 const char *key,
61 const char *subkey);
64 struct regdb_trans_ctx {
65 NTSTATUS (*action)(struct db_context *, void *);
66 void *private_data;
69 static NTSTATUS regdb_trans_do_action(struct db_context *db, void *private_data)
71 NTSTATUS status;
72 int32_t version_id;
73 struct regdb_trans_ctx *ctx = (struct regdb_trans_ctx *)private_data;
75 status = dbwrap_fetch_int32_bystring(db, REGDB_VERSION_KEYNAME,
76 &version_id);
78 if (!NT_STATUS_IS_OK(status)) {
79 DEBUG(0, ("ERROR: could not fetch registry db version: %s. "
80 "Denying access.\n", nt_errstr(status)));
81 return NT_STATUS_ACCESS_DENIED;
84 if (version_id != REGDB_CODE_VERSION) {
85 DEBUG(0, ("ERROR: changed registry version %d found while "
86 "trying to write to the registry. Version %d "
87 "expected. Denying access.\n",
88 version_id, REGDB_CODE_VERSION));
89 return NT_STATUS_ACCESS_DENIED;
92 status = ctx->action(db, ctx->private_data);
93 return status;
96 static WERROR regdb_trans_do(struct db_context *db,
97 NTSTATUS (*action)(struct db_context *, void *),
98 void *private_data)
100 NTSTATUS status;
101 struct regdb_trans_ctx ctx;
104 ctx.action = action;
105 ctx.private_data = private_data;
107 status = dbwrap_trans_do(db, regdb_trans_do_action, &ctx);
109 return ntstatus_to_werror(status);
112 /* List the deepest path into the registry. All part components will be created.*/
114 /* If you want to have a part of the path controlled by the tdb and part by
115 a virtual registry db (e.g. printing), then you have to list the deepest path.
116 For example,"HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion/Print"
117 allows the reg_db backend to handle everything up to
118 "HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion" and then we'll hook
119 the reg_printing backend onto the last component of the path (see
120 KEY_PRINTING_2K in include/rpc_reg.h) --jerry */
122 static const char *builtin_registry_paths[] = {
123 KEY_PRINTING_2K,
124 KEY_PRINTING_PORTS,
125 KEY_PRINTING,
126 KEY_PRINTING "\\Forms",
127 KEY_PRINTING "\\Printers",
128 KEY_PRINTING "\\Environments\\Windows NT x86\\Print Processors\\winprint",
129 KEY_SHARES,
130 KEY_EVENTLOG,
131 KEY_SMBCONF,
132 KEY_PERFLIB,
133 KEY_PERFLIB_009,
134 KEY_GROUP_POLICY,
135 KEY_SAMBA_GROUP_POLICY,
136 KEY_GP_MACHINE_POLICY,
137 KEY_GP_MACHINE_WIN_POLICY,
138 KEY_HKCU,
139 KEY_GP_USER_POLICY,
140 KEY_GP_USER_WIN_POLICY,
141 "HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon\\GPExtensions",
142 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Print\\Monitors",
143 KEY_PROD_OPTIONS,
144 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\DefaultUserConfiguration",
145 KEY_TCPIP_PARAMS,
146 KEY_NETLOGON_PARAMS,
147 KEY_HKU,
148 KEY_HKCR,
149 KEY_HKPD,
150 KEY_HKPT,
151 NULL };
153 struct builtin_regkey_value {
154 const char *path;
155 const char *valuename;
156 uint32_t type;
157 union {
158 const char *string;
159 uint32_t dw_value;
160 } data;
163 static struct builtin_regkey_value builtin_registry_values[] = {
164 { KEY_PRINTING_PORTS,
165 SAMBA_PRINTER_PORT_NAME, REG_SZ, { "" } },
166 { KEY_PRINTING_2K,
167 "DefaultSpoolDirectory", REG_SZ, { "C:\\Windows\\System32\\Spool\\Printers" } },
168 { KEY_EVENTLOG,
169 "DisplayName", REG_SZ, { "Event Log" } },
170 { KEY_EVENTLOG,
171 "ErrorControl", REG_DWORD, { (char*)0x00000001 } },
172 { NULL, NULL, 0, { NULL } }
175 static WERROR create_key_recursive(struct db_context *db,
176 char *path,
177 const char *subkey)
179 WERROR werr;
180 char *p;
182 if (subkey == NULL) {
183 return WERR_INVALID_PARAM;
186 if (path == NULL) {
187 return regdb_create_basekey(db, subkey);
190 p = strrchr_m(path, '\\');
192 if (p == NULL) {
193 werr = create_key_recursive(db, NULL, path);
194 } else {
195 *p = '\0';
196 werr = create_key_recursive(db, path, p+1);
197 *p = '\\';
200 if (!W_ERROR_IS_OK(werr)) {
201 goto done;
204 werr = regdb_create_subkey_internal(db, path, subkey);
206 done:
207 return werr;
211 * Initialize a key in the registry:
212 * create each component key of the specified path.
214 static WERROR init_registry_key_internal(struct db_context *db,
215 const char *add_path)
217 char *subkey, *key;
218 WERROR werr;
219 TALLOC_CTX *frame = talloc_stackframe();
221 if (add_path == NULL) {
222 werr = WERR_INVALID_PARAM;
223 goto done;
226 key = talloc_strdup(frame, add_path);
228 subkey = strrchr_m(key, '\\');
229 if (subkey == NULL) {
230 subkey = key;
231 key = NULL;
232 } else {
233 *subkey = '\0';
234 subkey++;
237 werr = create_key_recursive(db, key, subkey);
239 done:
240 talloc_free(frame);
241 return werr;
244 struct init_registry_key_context {
245 const char *add_path;
248 static NTSTATUS init_registry_key_action(struct db_context *db,
249 void *private_data)
251 struct init_registry_key_context *init_ctx =
252 (struct init_registry_key_context *)private_data;
254 return werror_to_ntstatus(init_registry_key_internal(
255 db, init_ctx->add_path));
259 * Initialize a key in the registry:
260 * create each component key of the specified path,
261 * wrapped in one db transaction.
263 WERROR init_registry_key(const char *add_path)
265 struct init_registry_key_context init_ctx;
267 if (regdb_key_exists(regdb, add_path)) {
268 return WERR_OK;
271 init_ctx.add_path = add_path;
273 return regdb_trans_do(regdb,
274 init_registry_key_action,
275 &init_ctx);
278 /***********************************************************************
279 Open the registry data in the tdb
280 ***********************************************************************/
282 static void regdb_ctr_add_value(struct regval_ctr *ctr,
283 struct builtin_regkey_value *value)
285 switch(value->type) {
286 case REG_DWORD:
287 regval_ctr_addvalue(ctr, value->valuename, REG_DWORD,
288 (uint8_t *)&value->data.dw_value,
289 sizeof(uint32_t));
290 break;
292 case REG_SZ:
293 regval_ctr_addvalue_sz(ctr, value->valuename,
294 value->data.string);
295 break;
297 default:
298 DEBUG(0, ("regdb_ctr_add_value: invalid value type in "
299 "registry values [%d]\n", value->type));
303 static NTSTATUS init_registry_data_action(struct db_context *db,
304 void *private_data)
306 NTSTATUS status;
307 TALLOC_CTX *frame = talloc_stackframe();
308 struct regval_ctr *values;
309 int i;
311 /* loop over all of the predefined paths and add each component */
313 for (i=0; builtin_registry_paths[i] != NULL; i++) {
314 if (regdb_key_exists(db, builtin_registry_paths[i])) {
315 continue;
317 status = werror_to_ntstatus(init_registry_key_internal(db,
318 builtin_registry_paths[i]));
319 if (!NT_STATUS_IS_OK(status)) {
320 goto done;
324 /* loop over all of the predefined values and add each component */
326 for (i=0; builtin_registry_values[i].path != NULL; i++) {
327 WERROR werr;
329 werr = regval_ctr_init(frame, &values);
330 if (!W_ERROR_IS_OK(werr)) {
331 status = werror_to_ntstatus(werr);
332 goto done;
335 regdb_fetch_values_internal(db,
336 builtin_registry_values[i].path,
337 values);
339 /* preserve existing values across restarts. Only add new ones */
341 if (!regval_ctr_value_exists(values,
342 builtin_registry_values[i].valuename))
344 regdb_ctr_add_value(values,
345 &builtin_registry_values[i]);
346 status = regdb_store_values_internal(db,
347 builtin_registry_values[i].path,
348 values);
349 if (!NT_STATUS_IS_OK(status)) {
350 goto done;
353 TALLOC_FREE(values);
356 status = NT_STATUS_OK;
358 done:
360 TALLOC_FREE(frame);
361 return status;
364 WERROR init_registry_data(void)
366 WERROR werr;
367 TALLOC_CTX *frame = talloc_stackframe();
368 struct regval_ctr *values;
369 int i;
372 * First, check for the existence of the needed keys and values.
373 * If all do already exist, we can save the writes.
375 for (i=0; builtin_registry_paths[i] != NULL; i++) {
376 if (!regdb_key_exists(regdb, builtin_registry_paths[i])) {
377 goto do_init;
381 for (i=0; builtin_registry_values[i].path != NULL; i++) {
382 werr = regval_ctr_init(frame, &values);
383 W_ERROR_NOT_OK_GOTO_DONE(werr);
385 regdb_fetch_values_internal(regdb,
386 builtin_registry_values[i].path,
387 values);
388 if (!regval_ctr_value_exists(values,
389 builtin_registry_values[i].valuename))
391 TALLOC_FREE(values);
392 goto do_init;
395 TALLOC_FREE(values);
398 werr = WERR_OK;
399 goto done;
401 do_init:
404 * There are potentially quite a few store operations which are all
405 * indiviually wrapped in tdb transactions. Wrapping them in a single
406 * transaction gives just a single transaction_commit() to actually do
407 * its fsync()s. See tdb/common/transaction.c for info about nested
408 * transaction behaviour.
411 werr = regdb_trans_do(regdb,
412 init_registry_data_action,
413 NULL);
415 done:
416 TALLOC_FREE(frame);
417 return werr;
420 static int regdb_normalize_keynames_fn(struct db_record *rec,
421 void *private_data)
423 TALLOC_CTX *mem_ctx = talloc_tos();
424 const char *keyname;
425 NTSTATUS status;
426 TDB_DATA key;
427 TDB_DATA value;
428 struct db_context *db = (struct db_context *)private_data;
430 key = dbwrap_record_get_key(rec);
431 if (key.dptr == NULL || key.dsize == 0) {
432 return 0;
435 value = dbwrap_record_get_value(rec);
437 if (db == NULL) {
438 DEBUG(0, ("regdb_normalize_keynames_fn: ERROR: "
439 "NULL db context handed in via private_data\n"));
440 return 1;
443 if (strncmp((const char *)key.dptr, REGDB_VERSION_KEYNAME,
444 strlen(REGDB_VERSION_KEYNAME)) == 0)
446 return 0;
449 keyname = strchr((const char *)key.dptr, '/');
450 if (keyname) {
451 keyname = talloc_string_sub(mem_ctx,
452 (const char *)key.dptr,
453 "/",
454 "\\");
456 DEBUG(2, ("regdb_normalize_keynames_fn: Convert %s to %s\n",
457 (const char *)key.dptr,
458 keyname));
460 /* Delete the original record and store the normalized key */
461 status = dbwrap_record_delete(rec);
462 if (!NT_STATUS_IS_OK(status)) {
463 DEBUG(0,("regdb_normalize_keynames_fn: "
464 "tdb_delete for [%s] failed!\n",
465 (const char *)key.dptr));
466 return 1;
469 status = dbwrap_store_bystring(db, keyname, value, TDB_REPLACE);
470 if (!NT_STATUS_IS_OK(status)) {
471 DEBUG(0,("regdb_normalize_keynames_fn: "
472 "failed to store new record for [%s]!\n",
473 keyname));
474 return 1;
478 return 0;
481 static WERROR regdb_store_regdb_version(struct db_context *db, uint32_t version)
483 NTSTATUS status;
484 if (db == NULL) {
485 return WERR_CAN_NOT_COMPLETE;
488 status = dbwrap_trans_store_int32_bystring(db, REGDB_VERSION_KEYNAME,
489 version);
490 if (!NT_STATUS_IS_OK(status)) {
491 DEBUG(1, ("regdb_store_regdb_version: error storing %s = %d: %s\n",
492 REGDB_VERSION_KEYNAME, version, nt_errstr(status)));
493 return ntstatus_to_werror(status);
494 } else {
495 DEBUG(10, ("regdb_store_regdb_version: stored %s = %d\n",
496 REGDB_VERSION_KEYNAME, version));
497 return WERR_OK;
501 static WERROR regdb_upgrade_v1_to_v2(struct db_context *db)
503 TALLOC_CTX *mem_ctx;
504 NTSTATUS status;
505 WERROR werr;
507 mem_ctx = talloc_stackframe();
509 status = dbwrap_traverse(db, regdb_normalize_keynames_fn, db, NULL);
510 if (!NT_STATUS_IS_OK(status)) {
511 werr = WERR_REG_IO_FAILURE;
512 goto done;
515 werr = regdb_store_regdb_version(db, REGDB_VERSION_V2);
517 done:
518 talloc_free(mem_ctx);
519 return werr;
522 static bool tdb_data_read_uint32(TDB_DATA *buf, uint32_t *result)
524 const size_t len = sizeof(uint32_t);
525 if (buf->dsize >= len) {
526 *result = IVAL(buf->dptr, 0);
527 buf->dptr += len;
528 buf->dsize -= len;
529 return true;
531 return false;
534 static bool tdb_data_read_cstr(TDB_DATA *buf, char **result)
536 const size_t len = strnlen((char*)buf->dptr, buf->dsize) + 1;
537 if (buf->dsize >= len) {
538 *result = (char*)buf->dptr;
539 buf->dptr += len;
540 buf->dsize -= len;
541 return true;
543 return false;
546 static bool tdb_data_is_cstr(TDB_DATA d) {
547 if (tdb_data_is_empty(d) || (d.dptr[d.dsize-1] != '\0')) {
548 return false;
550 return strlen((char *)d.dptr) == (d.dsize-1);
553 static bool upgrade_v2_to_v3_check_subkeylist(struct db_context *db,
554 const char *key,
555 const char *subkey)
557 static uint32_t zero = 0;
558 static TDB_DATA empty_subkey_list = {
559 .dptr = (unsigned char*)&zero,
560 .dsize = sizeof(uint32_t),
562 bool success = false;
563 char *path = talloc_asprintf(talloc_tos(), "%s\\%s", key, subkey);
564 if (!strupper_m(path)) {
565 goto done;
568 if (!dbwrap_exists(db, string_term_tdb_data(path))) {
569 NTSTATUS status;
571 DEBUG(10, ("regdb_upgrade_v2_to_v3: writing subkey list [%s]\n",
572 path));
574 status = dbwrap_store_bystring(db, path, empty_subkey_list,
575 TDB_INSERT);
576 if (!NT_STATUS_IS_OK(status)) {
577 DEBUG(0, ("regdb_upgrade_v2_to_v3: writing subkey list "
578 "[%s] failed\n", path));
579 goto done;
582 success = true;
583 done:
584 talloc_free(path);
585 return success;
588 static bool upgrade_v2_to_v3_check_parent(struct db_context *db,
589 const char *key)
591 const char *sep = strrchr_m(key, '\\');
592 if (sep != NULL) {
593 char *pkey = talloc_strndup(talloc_tos(), key, sep-key);
594 if (!dbwrap_exists(db, string_term_tdb_data(pkey))) {
595 DEBUG(0, ("regdb_upgrade_v2_to_v3: missing subkey list "
596 "[%s]\nrun \"net registry check\"\n", pkey));
598 talloc_free(pkey);
600 return true;
604 #define IS_EQUAL(d,s) (((d).dsize == strlen(s)+1) && \
605 (strcmp((char*)(d).dptr, (s)) == 0))
606 #define STARTS_WITH(d,s) (((d).dsize > strlen(s)) && \
607 (strncmp((char*)(d).dptr, (s), strlen(s)) == 0))
608 #define SSTR(d) (int)(d).dsize , (char*)(d).dptr
611 static int regdb_upgrade_v2_to_v3_fn(struct db_record *rec, void *private_data)
613 struct db_context *db = (struct db_context *)private_data;
614 TDB_DATA key = dbwrap_record_get_key(rec);
615 TDB_DATA val = dbwrap_record_get_value(rec);
617 if (tdb_data_is_empty(key)) {
618 return 0;
621 if (db == NULL) {
622 DEBUG(0, ("regdb_upgrade_v2_to_v3_fn: ERROR: "
623 "NULL db context handed in via private_data\n"));
624 return 1;
627 if (IS_EQUAL(key, REGDB_VERSION_KEYNAME) ||
628 STARTS_WITH(key, REG_VALUE_PREFIX) ||
629 STARTS_WITH(key, REG_SECDESC_PREFIX))
631 DEBUG(10, ("regdb_upgrade_v2_to_v3: skipping [%.*s]\n",
632 SSTR(key)));
633 return 0;
636 if (STARTS_WITH(key, REG_SORTED_SUBKEYS_PREFIX)) {
637 NTSTATUS status;
638 /* Delete the deprecated sorted subkeys cache. */
640 DEBUG(10, ("regdb_upgrade_v2_to_v3: deleting [%.*s]\n",
641 SSTR(key)));
643 status = dbwrap_record_delete(rec);
644 if (!NT_STATUS_IS_OK(status)) {
645 DEBUG(0, ("regdb_upgrade_v2_to_v3: deleting [%.*s] "
646 "failed!\n", SSTR(key)));
647 return 1;
650 return 0;
653 if ( tdb_data_is_cstr(key) &&
654 hive_info((char*)key.dptr) != NULL )
657 * Found a regular subkey list record.
658 * Walk the list and create the list record for those
659 * subkeys that don't already have one.
661 TDB_DATA pos = val;
662 char *subkey, *path = (char*)key.dptr;
663 uint32_t num_items, found_items = 0;
666 DEBUG(10, ("regdb_upgrade_v2_to_v3: scanning subkeylist of "
667 "[%s]\n", path));
669 if (!tdb_data_read_uint32(&pos, &num_items)) {
670 /* invalid or empty - skip */
671 return 0;
674 while (tdb_data_read_cstr(&pos, &subkey)) {
675 found_items++;
677 if (!upgrade_v2_to_v3_check_subkeylist(db, path, subkey))
679 return 1;
682 if (!upgrade_v2_to_v3_check_parent(db, path)) {
683 return 1;
686 if (found_items != num_items) {
687 DEBUG(0, ("regdb_upgrade_v2_to_v3: inconsistent subkey "
688 "list [%s]\nrun \"net registry check\"\n",
689 path));
691 } else {
692 DEBUG(10, ("regdb_upgrade_v2_to_v3: skipping invalid [%.*s]\n"
693 "run \"net registry check\"\n", SSTR(key)));
696 return 0;
699 static WERROR regdb_upgrade_v2_to_v3(struct db_context *db)
701 NTSTATUS status;
702 WERROR werr;
704 status = dbwrap_traverse(db, regdb_upgrade_v2_to_v3_fn, db, NULL);
705 if (!NT_STATUS_IS_OK(status)) {
706 werr = WERR_REG_IO_FAILURE;
707 goto done;
710 werr = regdb_store_regdb_version(db, REGDB_VERSION_V3);
712 done:
713 return werr;
716 /***********************************************************************
717 Open the registry database
718 ***********************************************************************/
720 WERROR regdb_init(void)
722 int32_t vers_id;
723 WERROR werr;
724 NTSTATUS status;
725 char *db_path;
727 if (regdb) {
728 DEBUG(10, ("regdb_init: incrementing refcount (%d->%d)\n",
729 regdb_refcount, regdb_refcount+1));
730 regdb_refcount++;
731 return WERR_OK;
734 db_path = state_path("registry.tdb");
735 if (db_path == NULL) {
736 return WERR_NOMEM;
739 regdb = db_open(NULL, db_path, 0,
740 REG_TDB_FLAGS, O_RDWR, 0600,
741 DBWRAP_LOCK_ORDER_1, REG_DBWRAP_FLAGS);
742 if (!regdb) {
743 regdb = db_open(NULL, db_path, 0,
744 REG_TDB_FLAGS, O_RDWR|O_CREAT, 0600,
745 DBWRAP_LOCK_ORDER_1, REG_DBWRAP_FLAGS);
746 if (!regdb) {
747 werr = ntstatus_to_werror(map_nt_error_from_unix(errno));
748 DEBUG(1,("regdb_init: Failed to open registry %s (%s)\n",
749 db_path, strerror(errno) ));
750 TALLOC_FREE(db_path);
751 return werr;
754 werr = regdb_store_regdb_version(regdb, REGDB_CODE_VERSION);
755 if (!W_ERROR_IS_OK(werr)) {
756 DEBUG(1, ("regdb_init: Failed to store version: %s\n",
757 win_errstr(werr)));
758 TALLOC_FREE(db_path);
759 return werr;
762 DEBUG(10,("regdb_init: Successfully created registry tdb\n"));
764 TALLOC_FREE(db_path);
766 regdb_refcount = 1;
767 DEBUG(10, ("regdb_init: registry db openend. refcount reset (%d)\n",
768 regdb_refcount));
770 status = dbwrap_fetch_int32_bystring(regdb, REGDB_VERSION_KEYNAME,
771 &vers_id);
772 if (!NT_STATUS_IS_OK(status)) {
773 DEBUG(10, ("regdb_init: registry version uninitialized "
774 "(got %d), initializing to version %d\n",
775 vers_id, REGDB_VERSION_V1));
778 * There was a regdb format version prior to version 1
779 * which did not store a INFO/version key. The format
780 * of this version was identical to version 1 except for
781 * the lack of the sorted subkey cache records.
782 * Since these are disposable, we can safely assume version
783 * 1 if no INFO/version key is found and run the db through
784 * the whole chain of upgrade. If the database was not
785 * initialized, this does not harm. If it was the unversioned
786 * version ("0"), then it do the right thing with the records.
788 werr = regdb_store_regdb_version(regdb, REGDB_VERSION_V1);
789 if (!W_ERROR_IS_OK(werr)) {
790 return werr;
792 vers_id = REGDB_VERSION_V1;
795 if (vers_id == REGDB_CODE_VERSION) {
796 return WERR_OK;
799 if (vers_id > REGDB_CODE_VERSION || vers_id == 0) {
800 DEBUG(0, ("regdb_init: unknown registry version %d "
801 "(code version = %d), refusing initialization\n",
802 vers_id, REGDB_CODE_VERSION));
803 return WERR_CAN_NOT_COMPLETE;
806 if (dbwrap_transaction_start(regdb) != 0) {
807 return WERR_REG_IO_FAILURE;
810 if (vers_id == REGDB_VERSION_V1) {
811 DEBUG(10, ("regdb_init: upgrading registry from version %d "
812 "to %d\n", REGDB_VERSION_V1, REGDB_VERSION_V2));
814 werr = regdb_upgrade_v1_to_v2(regdb);
815 if (!W_ERROR_IS_OK(werr)) {
816 dbwrap_transaction_cancel(regdb);
817 return werr;
820 vers_id = REGDB_VERSION_V2;
823 if (vers_id == REGDB_VERSION_V2) {
824 DEBUG(10, ("regdb_init: upgrading registry from version %d "
825 "to %d\n", REGDB_VERSION_V2, REGDB_VERSION_V3));
827 werr = regdb_upgrade_v2_to_v3(regdb);
828 if (!W_ERROR_IS_OK(werr)) {
829 dbwrap_transaction_cancel(regdb);
830 return werr;
833 vers_id = REGDB_VERSION_V3;
836 /* future upgrade code should go here */
838 if (dbwrap_transaction_commit(regdb) != 0) {
839 return WERR_REG_IO_FAILURE;
842 return WERR_OK;
845 /***********************************************************************
846 Open the registry. Must already have been initialized by regdb_init()
847 ***********************************************************************/
849 WERROR regdb_open( void )
851 WERROR result = WERR_OK;
852 char *db_path;
853 int saved_errno;
855 if ( regdb ) {
856 DEBUG(10, ("regdb_open: incrementing refcount (%d->%d)\n",
857 regdb_refcount, regdb_refcount+1));
858 regdb_refcount++;
859 return WERR_OK;
862 db_path = state_path("registry.tdb");
863 if (db_path == NULL) {
864 return WERR_NOMEM;
867 become_root();
869 regdb = db_open(NULL, db_path, 0,
870 REG_TDB_FLAGS, O_RDWR, 0600,
871 DBWRAP_LOCK_ORDER_1, REG_DBWRAP_FLAGS);
872 saved_errno = errno;
873 unbecome_root();
874 if ( !regdb ) {
875 result = ntstatus_to_werror(map_nt_error_from_unix(saved_errno));
876 DEBUG(0,("regdb_open: Failed to open %s! (%s)\n",
877 db_path, strerror(saved_errno)));
878 TALLOC_FREE(db_path);
879 return result;
881 TALLOC_FREE(db_path);
883 regdb_refcount = 1;
884 DEBUG(10, ("regdb_open: registry db opened. refcount reset (%d)\n",
885 regdb_refcount));
887 return WERR_OK;
890 /***********************************************************************
891 ***********************************************************************/
893 int regdb_close( void )
895 if (regdb_refcount == 0) {
896 return 0;
899 regdb_refcount--;
901 DEBUG(10, ("regdb_close: decrementing refcount (%d->%d)\n",
902 regdb_refcount+1, regdb_refcount));
904 if ( regdb_refcount > 0 )
905 return 0;
907 SMB_ASSERT( regdb_refcount >= 0 );
909 TALLOC_FREE(regdb);
910 return 0;
913 WERROR regdb_transaction_start(void)
915 return (dbwrap_transaction_start(regdb) == 0) ?
916 WERR_OK : WERR_REG_IO_FAILURE;
919 WERROR regdb_transaction_commit(void)
921 return (dbwrap_transaction_commit(regdb) == 0) ?
922 WERR_OK : WERR_REG_IO_FAILURE;
925 WERROR regdb_transaction_cancel(void)
927 return (dbwrap_transaction_cancel(regdb) == 0) ?
928 WERR_OK : WERR_REG_IO_FAILURE;
931 /***********************************************************************
932 return the tdb sequence number of the registry tdb.
933 this is an indicator for the content of the registry
934 having changed. it will change upon regdb_init, too, though.
935 ***********************************************************************/
936 int regdb_get_seqnum(void)
938 return dbwrap_get_seqnum(regdb);
942 static WERROR regdb_delete_key_with_prefix(struct db_context *db,
943 const char *keyname,
944 const char *prefix)
946 char *path;
947 WERROR werr = WERR_NOMEM;
948 TALLOC_CTX *mem_ctx = talloc_stackframe();
950 if (keyname == NULL) {
951 werr = WERR_INVALID_PARAM;
952 goto done;
955 if (prefix == NULL) {
956 path = discard_const_p(char, keyname);
957 } else {
958 path = talloc_asprintf(mem_ctx, "%s\\%s", prefix, keyname);
959 if (path == NULL) {
960 goto done;
964 path = normalize_reg_path(mem_ctx, path);
965 if (path == NULL) {
966 goto done;
969 werr = ntstatus_to_werror(dbwrap_delete_bystring(db, path));
971 /* treat "not found" as ok */
972 if (W_ERROR_EQUAL(werr, WERR_NOT_FOUND)) {
973 werr = WERR_OK;
976 done:
977 talloc_free(mem_ctx);
978 return werr;
982 static WERROR regdb_delete_values(struct db_context *db, const char *keyname)
984 return regdb_delete_key_with_prefix(db, keyname, REG_VALUE_PREFIX);
987 static WERROR regdb_delete_secdesc(struct db_context *db, const char *keyname)
989 return regdb_delete_key_with_prefix(db, keyname, REG_SECDESC_PREFIX);
992 static WERROR regdb_delete_subkeylist(struct db_context *db, const char *keyname)
994 return regdb_delete_key_with_prefix(db, keyname, NULL);
998 static WERROR regdb_delete_key_lists(struct db_context *db, const char *keyname)
1000 WERROR werr;
1002 werr = regdb_delete_values(db, keyname);
1003 if (!W_ERROR_IS_OK(werr)) {
1004 DEBUG(1, (__location__ " Deleting %s\\%s failed: %s\n",
1005 REG_VALUE_PREFIX, keyname, win_errstr(werr)));
1006 goto done;
1009 werr = regdb_delete_secdesc(db, keyname);
1010 if (!W_ERROR_IS_OK(werr)) {
1011 DEBUG(1, (__location__ " Deleting %s\\%s failed: %s\n",
1012 REG_SECDESC_PREFIX, keyname, win_errstr(werr)));
1013 goto done;
1016 werr = regdb_delete_subkeylist(db, keyname);
1017 if (!W_ERROR_IS_OK(werr)) {
1018 DEBUG(1, (__location__ " Deleting %s failed: %s\n",
1019 keyname, win_errstr(werr)));
1020 goto done;
1023 done:
1024 return werr;
1027 /***********************************************************************
1028 Add subkey strings to the registry tdb under a defined key
1029 fmt is the same format as tdb_pack except this function only supports
1030 fstrings
1031 ***********************************************************************/
1033 static WERROR regdb_store_keys_internal2(struct db_context *db,
1034 const char *key,
1035 struct regsubkey_ctr *ctr)
1037 TDB_DATA dbuf;
1038 uint8_t *buffer = NULL;
1039 int i = 0;
1040 uint32_t len, buflen;
1041 uint32_t num_subkeys = regsubkey_ctr_numkeys(ctr);
1042 char *keyname = NULL;
1043 TALLOC_CTX *ctx = talloc_stackframe();
1044 WERROR werr;
1046 if (!key) {
1047 werr = WERR_INVALID_PARAM;
1048 goto done;
1051 keyname = talloc_strdup(ctx, key);
1052 if (!keyname) {
1053 werr = WERR_NOMEM;
1054 goto done;
1057 keyname = normalize_reg_path(ctx, keyname);
1058 if (!keyname) {
1059 werr = WERR_NOMEM;
1060 goto done;
1063 /* allocate some initial memory */
1065 buffer = (uint8_t *)SMB_MALLOC(1024);
1066 if (buffer == NULL) {
1067 werr = WERR_NOMEM;
1068 goto done;
1070 buflen = 1024;
1071 len = 0;
1073 /* store the number of subkeys */
1075 len += tdb_pack(buffer+len, buflen-len, "d", num_subkeys);
1077 /* pack all the strings */
1079 for (i=0; i<num_subkeys; i++) {
1080 size_t thistime;
1082 thistime = tdb_pack(buffer+len, buflen-len, "f",
1083 regsubkey_ctr_specific_key(ctr, i));
1084 if (len+thistime > buflen) {
1085 size_t thistime2;
1087 * tdb_pack hasn't done anything because of the short
1088 * buffer, allocate extra space.
1090 buffer = SMB_REALLOC_ARRAY(buffer, uint8_t,
1091 (len+thistime)*2);
1092 if(buffer == NULL) {
1093 DEBUG(0, ("regdb_store_keys: Failed to realloc "
1094 "memory of size [%u]\n",
1095 (unsigned int)(len+thistime)*2));
1096 werr = WERR_NOMEM;
1097 goto done;
1099 buflen = (len+thistime)*2;
1100 thistime2 = tdb_pack(
1101 buffer+len, buflen-len, "f",
1102 regsubkey_ctr_specific_key(ctr, i));
1103 if (thistime2 != thistime) {
1104 DEBUG(0, ("tdb_pack failed\n"));
1105 werr = WERR_CAN_NOT_COMPLETE;
1106 goto done;
1109 len += thistime;
1112 /* finally write out the data */
1114 dbuf.dptr = buffer;
1115 dbuf.dsize = len;
1116 werr = ntstatus_to_werror(dbwrap_store_bystring(db, keyname, dbuf,
1117 TDB_REPLACE));
1119 done:
1120 TALLOC_FREE(ctx);
1121 SAFE_FREE(buffer);
1122 return werr;
1126 * Utility function to store a new empty list of
1127 * subkeys of given key specified as parent and subkey name
1128 * (thereby creating the key).
1129 * If the parent keyname is NULL, then the "subkey" is
1130 * interpreted as a base key.
1131 * If the subkey list does already exist, it is not modified.
1133 * Must be called from within a transaction.
1135 static WERROR regdb_store_subkey_list(struct db_context *db, const char *parent,
1136 const char *key)
1138 WERROR werr;
1139 char *path = NULL;
1140 struct regsubkey_ctr *subkeys = NULL;
1141 TALLOC_CTX *frame = talloc_stackframe();
1143 if (parent == NULL) {
1144 path = talloc_strdup(frame, key);
1145 } else {
1146 path = talloc_asprintf(frame, "%s\\%s", parent, key);
1148 if (!path) {
1149 werr = WERR_NOMEM;
1150 goto done;
1153 werr = regsubkey_ctr_init(frame, &subkeys);
1154 W_ERROR_NOT_OK_GOTO_DONE(werr);
1156 werr = regdb_fetch_keys_internal(db, path, subkeys);
1157 if (W_ERROR_IS_OK(werr)) {
1158 /* subkey list exists already - don't modify */
1159 goto done;
1162 werr = regsubkey_ctr_reinit(subkeys);
1163 W_ERROR_NOT_OK_GOTO_DONE(werr);
1165 /* create a record with 0 subkeys */
1166 werr = regdb_store_keys_internal2(db, path, subkeys);
1167 if (!W_ERROR_IS_OK(werr)) {
1168 DEBUG(0, ("regdb_store_keys: Failed to store new record for "
1169 "key [%s]: %s\n", path, win_errstr(werr)));
1170 goto done;
1173 done:
1174 talloc_free(frame);
1175 return werr;
1178 /***********************************************************************
1179 Store the new subkey record and create any child key records that
1180 do not currently exist
1181 ***********************************************************************/
1183 struct regdb_store_keys_context {
1184 const char *key;
1185 struct regsubkey_ctr *ctr;
1188 static NTSTATUS regdb_store_keys_action(struct db_context *db,
1189 void *private_data)
1191 struct regdb_store_keys_context *store_ctx;
1192 WERROR werr;
1193 int num_subkeys, i;
1194 char *path = NULL;
1195 struct regsubkey_ctr *old_subkeys = NULL;
1196 char *oldkeyname = NULL;
1197 TALLOC_CTX *mem_ctx = talloc_stackframe();
1199 store_ctx = (struct regdb_store_keys_context *)private_data;
1202 * Re-fetch the old keys inside the transaction
1205 werr = regsubkey_ctr_init(mem_ctx, &old_subkeys);
1206 W_ERROR_NOT_OK_GOTO_DONE(werr);
1208 werr = regdb_fetch_keys_internal(db, store_ctx->key, old_subkeys);
1209 if (!W_ERROR_IS_OK(werr) &&
1210 !W_ERROR_EQUAL(werr, WERR_NOT_FOUND))
1212 goto done;
1216 * Make the store operation as safe as possible without transactions:
1218 * (1) For each subkey removed from ctr compared with old_subkeys:
1220 * (a) First delete the value db entry.
1222 * (b) Next delete the secdesc db record.
1224 * (c) Then delete the subkey list entry.
1226 * (2) Now write the list of subkeys of the parent key,
1227 * deleting removed entries and adding new ones.
1229 * (3) Finally create the subkey list entries for the added keys.
1231 * This way if we crash half-way in between deleting the subkeys
1232 * and storing the parent's list of subkeys, no old data can pop up
1233 * out of the blue when re-adding keys later on.
1236 /* (1) delete removed keys' lists (values/secdesc/subkeys) */
1238 num_subkeys = regsubkey_ctr_numkeys(old_subkeys);
1239 for (i=0; i<num_subkeys; i++) {
1240 oldkeyname = regsubkey_ctr_specific_key(old_subkeys, i);
1242 if (regsubkey_ctr_key_exists(store_ctx->ctr, oldkeyname)) {
1244 * It's still around, don't delete
1246 continue;
1249 path = talloc_asprintf(mem_ctx, "%s\\%s", store_ctx->key,
1250 oldkeyname);
1251 if (!path) {
1252 werr = WERR_NOMEM;
1253 goto done;
1256 werr = regdb_delete_key_lists(db, path);
1257 W_ERROR_NOT_OK_GOTO_DONE(werr);
1259 TALLOC_FREE(path);
1262 TALLOC_FREE(old_subkeys);
1264 /* (2) store the subkey list for the parent */
1266 werr = regdb_store_keys_internal2(db, store_ctx->key, store_ctx->ctr);
1267 if (!W_ERROR_IS_OK(werr)) {
1268 DEBUG(0,("regdb_store_keys: Failed to store new subkey list "
1269 "for parent [%s]: %s\n", store_ctx->key,
1270 win_errstr(werr)));
1271 goto done;
1274 /* (3) now create records for any subkeys that don't already exist */
1276 num_subkeys = regsubkey_ctr_numkeys(store_ctx->ctr);
1278 for (i=0; i<num_subkeys; i++) {
1279 const char *subkey;
1281 subkey = regsubkey_ctr_specific_key(store_ctx->ctr, i);
1283 werr = regdb_store_subkey_list(db, store_ctx->key, subkey);
1284 W_ERROR_NOT_OK_GOTO_DONE(werr);
1288 * Update the seqnum in the container to possibly
1289 * prevent next read from going to disk
1291 werr = regsubkey_ctr_set_seqnum(store_ctx->ctr, dbwrap_get_seqnum(db));
1293 done:
1294 talloc_free(mem_ctx);
1295 return werror_to_ntstatus(werr);
1298 static bool regdb_store_keys_internal(struct db_context *db, const char *key,
1299 struct regsubkey_ctr *ctr)
1301 int num_subkeys, old_num_subkeys, i;
1302 struct regsubkey_ctr *old_subkeys = NULL;
1303 TALLOC_CTX *ctx = talloc_stackframe();
1304 WERROR werr;
1305 bool ret = false;
1306 struct regdb_store_keys_context store_ctx;
1308 if (!regdb_key_exists(db, key)) {
1309 goto done;
1313 * fetch a list of the old subkeys so we can determine if anything has
1314 * changed
1317 werr = regsubkey_ctr_init(ctx, &old_subkeys);
1318 if (!W_ERROR_IS_OK(werr)) {
1319 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
1320 goto done;
1323 werr = regdb_fetch_keys_internal(db, key, old_subkeys);
1324 if (!W_ERROR_IS_OK(werr) &&
1325 !W_ERROR_EQUAL(werr, WERR_NOT_FOUND))
1327 goto done;
1330 num_subkeys = regsubkey_ctr_numkeys(ctr);
1331 old_num_subkeys = regsubkey_ctr_numkeys(old_subkeys);
1332 if ((num_subkeys && old_num_subkeys) &&
1333 (num_subkeys == old_num_subkeys)) {
1335 for (i = 0; i < num_subkeys; i++) {
1336 if (strcmp(regsubkey_ctr_specific_key(ctr, i),
1337 regsubkey_ctr_specific_key(old_subkeys, i))
1338 != 0)
1340 break;
1343 if (i == num_subkeys) {
1345 * Nothing changed, no point to even start a tdb
1346 * transaction
1349 ret = true;
1350 goto done;
1354 TALLOC_FREE(old_subkeys);
1356 store_ctx.key = key;
1357 store_ctx.ctr = ctr;
1359 werr = regdb_trans_do(db,
1360 regdb_store_keys_action,
1361 &store_ctx);
1363 ret = W_ERROR_IS_OK(werr);
1365 done:
1366 TALLOC_FREE(ctx);
1368 return ret;
1371 static bool regdb_store_keys(const char *key, struct regsubkey_ctr *ctr)
1373 return regdb_store_keys_internal(regdb, key, ctr);
1377 * create a subkey of a given key
1380 struct regdb_create_subkey_context {
1381 const char *key;
1382 const char *subkey;
1385 static NTSTATUS regdb_create_subkey_action(struct db_context *db,
1386 void *private_data)
1388 WERROR werr;
1389 struct regdb_create_subkey_context *create_ctx;
1390 struct regsubkey_ctr *subkeys;
1391 TALLOC_CTX *mem_ctx = talloc_stackframe();
1393 create_ctx = (struct regdb_create_subkey_context *)private_data;
1395 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
1396 W_ERROR_NOT_OK_GOTO_DONE(werr);
1398 werr = regdb_fetch_keys_internal(db, create_ctx->key, subkeys);
1399 W_ERROR_NOT_OK_GOTO_DONE(werr);
1401 werr = regsubkey_ctr_addkey(subkeys, create_ctx->subkey);
1402 W_ERROR_NOT_OK_GOTO_DONE(werr);
1404 werr = regdb_store_keys_internal2(db, create_ctx->key, subkeys);
1405 if (!W_ERROR_IS_OK(werr)) {
1406 DEBUG(0, (__location__ " failed to store new subkey list for "
1407 "parent key %s: %s\n", create_ctx->key,
1408 win_errstr(werr)));
1411 werr = regdb_store_subkey_list(db, create_ctx->key, create_ctx->subkey);
1413 done:
1414 talloc_free(mem_ctx);
1415 return werror_to_ntstatus(werr);
1418 static WERROR regdb_create_subkey_internal(struct db_context *db,
1419 const char *key,
1420 const char *subkey)
1422 WERROR werr;
1423 struct regsubkey_ctr *subkeys;
1424 TALLOC_CTX *mem_ctx = talloc_stackframe();
1425 struct regdb_create_subkey_context create_ctx;
1427 if (!regdb_key_exists(db, key)) {
1428 werr = WERR_NOT_FOUND;
1429 goto done;
1432 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
1433 W_ERROR_NOT_OK_GOTO_DONE(werr);
1435 werr = regdb_fetch_keys_internal(db, key, subkeys);
1436 W_ERROR_NOT_OK_GOTO_DONE(werr);
1438 if (regsubkey_ctr_key_exists(subkeys, subkey)) {
1439 char *newkey;
1441 newkey = talloc_asprintf(mem_ctx, "%s\\%s", key, subkey);
1442 if (newkey == NULL) {
1443 werr = WERR_NOMEM;
1444 goto done;
1447 if (regdb_key_exists(db, newkey)) {
1448 werr = WERR_OK;
1449 goto done;
1453 talloc_free(subkeys);
1455 create_ctx.key = key;
1456 create_ctx.subkey = subkey;
1458 werr = regdb_trans_do(db,
1459 regdb_create_subkey_action,
1460 &create_ctx);
1462 done:
1463 talloc_free(mem_ctx);
1464 return werr;
1467 static WERROR regdb_create_subkey(const char *key, const char *subkey)
1469 return regdb_create_subkey_internal(regdb, key, subkey);
1473 * create a base key
1476 struct regdb_create_basekey_context {
1477 const char *key;
1480 static NTSTATUS regdb_create_basekey_action(struct db_context *db,
1481 void *private_data)
1483 WERROR werr;
1484 struct regdb_create_basekey_context *create_ctx;
1486 create_ctx = (struct regdb_create_basekey_context *)private_data;
1488 werr = regdb_store_subkey_list(db, NULL, create_ctx->key);
1490 return werror_to_ntstatus(werr);
1493 static WERROR regdb_create_basekey(struct db_context *db, const char *key)
1495 WERROR werr;
1496 struct regdb_create_subkey_context create_ctx;
1498 create_ctx.key = key;
1500 werr = regdb_trans_do(db,
1501 regdb_create_basekey_action,
1502 &create_ctx);
1504 return werr;
1508 * create a subkey of a given key
1511 struct regdb_delete_subkey_context {
1512 const char *key;
1513 const char *subkey;
1514 const char *path;
1515 bool lazy;
1518 static NTSTATUS regdb_delete_subkey_action(struct db_context *db,
1519 void *private_data)
1521 WERROR werr;
1522 struct regdb_delete_subkey_context *delete_ctx;
1523 struct regsubkey_ctr *subkeys;
1524 TALLOC_CTX *mem_ctx = talloc_stackframe();
1526 delete_ctx = (struct regdb_delete_subkey_context *)private_data;
1528 werr = regdb_delete_key_lists(db, delete_ctx->path);
1529 W_ERROR_NOT_OK_GOTO_DONE(werr);
1531 if (delete_ctx->lazy) {
1532 goto done;
1535 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
1536 W_ERROR_NOT_OK_GOTO_DONE(werr);
1538 werr = regdb_fetch_keys_internal(db, delete_ctx->key, subkeys);
1539 W_ERROR_NOT_OK_GOTO_DONE(werr);
1541 werr = regsubkey_ctr_delkey(subkeys, delete_ctx->subkey);
1542 W_ERROR_NOT_OK_GOTO_DONE(werr);
1544 werr = regdb_store_keys_internal2(db, delete_ctx->key, subkeys);
1545 if (!W_ERROR_IS_OK(werr)) {
1546 DEBUG(0, (__location__ " failed to store new subkey_list for "
1547 "parent key %s: %s\n", delete_ctx->key,
1548 win_errstr(werr)));
1551 done:
1552 talloc_free(mem_ctx);
1553 return werror_to_ntstatus(werr);
1556 static WERROR regdb_delete_subkey(const char *key, const char *subkey, bool lazy)
1558 WERROR werr;
1559 char *path;
1560 struct regdb_delete_subkey_context delete_ctx;
1561 TALLOC_CTX *mem_ctx = talloc_stackframe();
1563 if (!regdb_key_exists(regdb, key)) {
1564 werr = WERR_NOT_FOUND;
1565 goto done;
1568 path = talloc_asprintf(mem_ctx, "%s\\%s", key, subkey);
1569 if (path == NULL) {
1570 werr = WERR_NOMEM;
1571 goto done;
1574 if (!regdb_key_exists(regdb, path)) {
1575 werr = WERR_OK;
1576 goto done;
1579 delete_ctx.key = key;
1580 delete_ctx.subkey = subkey;
1581 delete_ctx.path = path;
1582 delete_ctx.lazy = lazy;
1584 werr = regdb_trans_do(regdb,
1585 regdb_delete_subkey_action,
1586 &delete_ctx);
1588 done:
1589 talloc_free(mem_ctx);
1590 return werr;
1593 static TDB_DATA regdb_fetch_key_internal(struct db_context *db,
1594 TALLOC_CTX *mem_ctx, const char *key)
1596 char *path = NULL;
1597 TDB_DATA data;
1598 NTSTATUS status;
1600 path = normalize_reg_path(mem_ctx, key);
1601 if (!path) {
1602 return make_tdb_data(NULL, 0);
1605 status = dbwrap_fetch_bystring(db, mem_ctx, path, &data);
1606 if (!NT_STATUS_IS_OK(status)) {
1607 data = tdb_null;
1610 TALLOC_FREE(path);
1611 return data;
1616 * Check for the existence of a key.
1618 * Existence of a key is authoritatively defined by
1619 * the existence of the record that contains the list
1620 * of its subkeys.
1622 * Return false, if the record does not match the correct
1623 * structure of an initial 4-byte counter and then a
1624 * list of the corresponding number of zero-terminated
1625 * strings.
1627 static bool regdb_key_exists(struct db_context *db, const char *key)
1629 TALLOC_CTX *mem_ctx = talloc_stackframe();
1630 TDB_DATA value;
1631 bool ret = false;
1632 char *path;
1633 uint32_t buflen;
1634 const char *buf;
1635 uint32_t num_items, i;
1636 int32_t len;
1638 if (key == NULL) {
1639 goto done;
1642 path = normalize_reg_path(mem_ctx, key);
1643 if (path == NULL) {
1644 DEBUG(0, ("out of memory! (talloc failed)\n"));
1645 goto done;
1648 if (*path == '\0') {
1649 goto done;
1652 value = regdb_fetch_key_internal(db, mem_ctx, path);
1653 if (value.dptr == NULL) {
1654 goto done;
1657 if (value.dsize == 0) {
1658 DEBUG(10, ("regdb_key_exists: subkeylist-record for key "
1659 "[%s] is empty: Could be a deleted record in a "
1660 "clustered (ctdb) environment?\n",
1661 path));
1662 goto done;
1665 len = tdb_unpack(value.dptr, value.dsize, "d", &num_items);
1666 if (len == (int32_t)-1) {
1667 DEBUG(1, ("regdb_key_exists: ERROR: subkeylist-record for key "
1668 "[%s] is invalid: Could not parse initial 4-byte "
1669 "counter. record data length is %u.\n",
1670 path, (unsigned int)value.dsize));
1671 goto done;
1675 * Note: the tdb_unpack check above implies that len <= value.dsize
1677 buflen = value.dsize - len;
1678 buf = (const char *)value.dptr + len;
1680 len = 0;
1682 for (i = 0; i < num_items; i++) {
1683 if (buflen == 0) {
1684 break;
1686 len = strnlen(buf, buflen) + 1;
1687 if (buflen < len) {
1688 DEBUG(1, ("regdb_key_exists: ERROR: subkeylist-record "
1689 "for key [%s] is corrupt: %u items expected, "
1690 "item number %u is not zero terminated.\n",
1691 path, num_items, i+1));
1692 goto done;
1695 buf += len;
1696 buflen -= len;
1699 if (buflen > 0) {
1700 DEBUG(1, ("regdb_key_exists: ERROR: subkeylist-record for key "
1701 "[%s] is corrupt: %u items expected and found, but "
1702 "the record contains additional %u bytes\n",
1703 path, num_items, buflen));
1704 goto done;
1707 if (i < num_items) {
1708 DEBUG(1, ("regdb_key_exists: ERROR: subkeylist-record for key "
1709 "[%s] is corrupt: %u items expected, but only %u "
1710 "items found.\n",
1711 path, num_items, i+1));
1712 goto done;
1715 ret = true;
1717 done:
1718 TALLOC_FREE(mem_ctx);
1719 return ret;
1723 /***********************************************************************
1724 Retrieve an array of strings containing subkeys. Memory should be
1725 released by the caller.
1726 ***********************************************************************/
1728 static WERROR regdb_fetch_keys_internal(struct db_context *db, const char *key,
1729 struct regsubkey_ctr *ctr)
1731 WERROR werr;
1732 uint32_t num_items;
1733 uint8_t *buf;
1734 uint32_t buflen, len;
1735 int i;
1736 fstring subkeyname;
1737 TALLOC_CTX *frame = talloc_stackframe();
1738 TDB_DATA value;
1739 int seqnum[2], count;
1741 DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key ? key : "NULL"));
1743 if (!regdb_key_exists(db, key)) {
1744 DEBUG(10, ("key [%s] not found\n", key));
1745 werr = WERR_NOT_FOUND;
1746 goto done;
1749 werr = regsubkey_ctr_reinit(ctr);
1750 W_ERROR_NOT_OK_GOTO_DONE(werr);
1752 count = 0;
1753 ZERO_STRUCT(value);
1754 seqnum[0] = dbwrap_get_seqnum(db);
1756 do {
1757 count++;
1758 TALLOC_FREE(value.dptr);
1759 value = regdb_fetch_key_internal(db, frame, key);
1760 seqnum[count % 2] = dbwrap_get_seqnum(db);
1762 } while (seqnum[0] != seqnum[1]);
1764 if (count > 1) {
1765 DEBUG(5, ("regdb_fetch_keys_internal: it took %d attempts to "
1766 "fetch key '%s' with constant seqnum\n",
1767 count, key));
1770 werr = regsubkey_ctr_set_seqnum(ctr, seqnum[0]);
1771 if (!W_ERROR_IS_OK(werr)) {
1772 goto done;
1775 if (value.dsize == 0 || value.dptr == NULL) {
1776 DEBUG(10, ("regdb_fetch_keys: no subkeys found for key [%s]\n",
1777 key));
1778 goto done;
1781 buf = value.dptr;
1782 buflen = value.dsize;
1783 len = tdb_unpack( buf, buflen, "d", &num_items);
1784 if (len == (uint32_t)-1) {
1785 werr = WERR_NOT_FOUND;
1786 goto done;
1789 for (i=0; i<num_items; i++) {
1790 len += tdb_unpack(buf+len, buflen-len, "f", subkeyname);
1791 werr = regsubkey_ctr_addkey(ctr, subkeyname);
1792 if (!W_ERROR_IS_OK(werr)) {
1793 DEBUG(5, ("regdb_fetch_keys: regsubkey_ctr_addkey "
1794 "failed: %s\n", win_errstr(werr)));
1795 num_items = 0;
1796 goto done;
1800 DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items));
1802 done:
1803 TALLOC_FREE(frame);
1804 return werr;
1807 static int regdb_fetch_keys(const char *key, struct regsubkey_ctr *ctr)
1809 WERROR werr;
1811 werr = regdb_fetch_keys_internal(regdb, key, ctr);
1812 if (!W_ERROR_IS_OK(werr)) {
1813 return -1;
1816 return regsubkey_ctr_numkeys(ctr);
1819 /****************************************************************************
1820 Unpack a list of registry values frem the TDB
1821 ***************************************************************************/
1823 static int regdb_unpack_values(struct regval_ctr *values, uint8_t *buf, int buflen)
1825 int len = 0;
1826 uint32_t type;
1827 fstring valuename;
1828 uint32_t size;
1829 uint8_t *data_p;
1830 uint32_t num_values = 0;
1831 int i;
1833 /* loop and unpack the rest of the registry values */
1835 len += tdb_unpack(buf+len, buflen-len, "d", &num_values);
1837 for ( i=0; i<num_values; i++ ) {
1838 /* unpack the next regval */
1840 type = REG_NONE;
1841 size = 0;
1842 data_p = NULL;
1843 valuename[0] = '\0';
1844 len += tdb_unpack(buf+len, buflen-len, "fdB",
1845 valuename,
1846 &type,
1847 &size,
1848 &data_p);
1850 regval_ctr_addvalue(values, valuename, type,
1851 (uint8_t *)data_p, size);
1852 SAFE_FREE(data_p); /* 'B' option to tdb_unpack does a malloc() */
1854 DEBUG(10, ("regdb_unpack_values: value[%d]: name[%s] len[%d]\n",
1855 i, valuename, size));
1858 return len;
1861 /****************************************************************************
1862 Pack all values in all printer keys
1863 ***************************************************************************/
1865 static int regdb_pack_values(struct regval_ctr *values, uint8_t *buf, int buflen)
1867 int len = 0;
1868 int i;
1869 struct regval_blob *val;
1870 int num_values;
1872 if ( !values )
1873 return 0;
1875 num_values = regval_ctr_numvals( values );
1877 /* pack the number of values first */
1879 len += tdb_pack( buf+len, buflen-len, "d", num_values );
1881 /* loop over all values */
1883 for ( i=0; i<num_values; i++ ) {
1884 val = regval_ctr_specific_value( values, i );
1885 len += tdb_pack(buf+len, buflen-len, "fdB",
1886 regval_name(val),
1887 regval_type(val),
1888 regval_size(val),
1889 regval_data_p(val) );
1892 return len;
1895 /***********************************************************************
1896 Retrieve an array of strings containing subkeys. Memory should be
1897 released by the caller.
1898 ***********************************************************************/
1900 static int regdb_fetch_values_internal(struct db_context *db, const char* key,
1901 struct regval_ctr *values)
1903 char *keystr = NULL;
1904 TALLOC_CTX *ctx = talloc_stackframe();
1905 int ret = 0;
1906 TDB_DATA value;
1907 WERROR werr;
1908 int seqnum[2], count;
1910 DEBUG(10,("regdb_fetch_values: Looking for values of key [%s]\n", key));
1912 if (!regdb_key_exists(db, key)) {
1913 DEBUG(10, ("regb_fetch_values: key [%s] does not exist\n",
1914 key));
1915 ret = -1;
1916 goto done;
1919 keystr = talloc_asprintf(ctx, "%s\\%s", REG_VALUE_PREFIX, key);
1920 if (!keystr) {
1921 goto done;
1924 ZERO_STRUCT(value);
1925 count = 0;
1926 seqnum[0] = dbwrap_get_seqnum(db);
1928 do {
1929 count++;
1930 TALLOC_FREE(value.dptr);
1931 value = regdb_fetch_key_internal(db, ctx, keystr);
1932 seqnum[count % 2] = dbwrap_get_seqnum(db);
1933 } while (seqnum[0] != seqnum[1]);
1935 if (count > 1) {
1936 DEBUG(5, ("regdb_fetch_values_internal: it took %d attempts "
1937 "to fetch key '%s' with constant seqnum\n",
1938 count, key));
1941 werr = regval_ctr_set_seqnum(values, seqnum[0]);
1942 if (!W_ERROR_IS_OK(werr)) {
1943 goto done;
1946 if (!value.dptr) {
1947 /* all keys have zero values by default */
1948 goto done;
1951 regdb_unpack_values(values, value.dptr, value.dsize);
1952 ret = regval_ctr_numvals(values);
1954 done:
1955 TALLOC_FREE(ctx);
1956 return ret;
1959 static int regdb_fetch_values(const char* key, struct regval_ctr *values)
1961 return regdb_fetch_values_internal(regdb, key, values);
1964 static NTSTATUS regdb_store_values_internal(struct db_context *db,
1965 const char *key,
1966 struct regval_ctr *values)
1968 TDB_DATA old_data, data;
1969 char *keystr = NULL;
1970 TALLOC_CTX *ctx = talloc_stackframe();
1971 int len;
1972 NTSTATUS status;
1973 WERROR werr;
1975 DEBUG(10,("regdb_store_values: Looking for values of key [%s]\n", key));
1977 if (!regdb_key_exists(db, key)) {
1978 status = NT_STATUS_NOT_FOUND;
1979 goto done;
1982 if (regval_ctr_numvals(values) == 0) {
1983 werr = regdb_delete_values(db, key);
1984 if (!W_ERROR_IS_OK(werr)) {
1985 status = werror_to_ntstatus(werr);
1986 goto done;
1990 * update the seqnum in the cache to prevent the next read
1991 * from going to disk
1993 werr = regval_ctr_set_seqnum(values, dbwrap_get_seqnum(db));
1994 status = werror_to_ntstatus(werr);
1995 goto done;
1998 ZERO_STRUCT(data);
2000 len = regdb_pack_values(values, data.dptr, data.dsize);
2001 if (len <= 0) {
2002 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
2003 status = NT_STATUS_UNSUCCESSFUL;
2004 goto done;
2007 data.dptr = talloc_array(ctx, uint8_t, len);
2008 data.dsize = len;
2010 len = regdb_pack_values(values, data.dptr, data.dsize);
2012 SMB_ASSERT( len == data.dsize );
2014 keystr = talloc_asprintf(ctx, "%s\\%s", REG_VALUE_PREFIX, key );
2015 if (!keystr) {
2016 status = NT_STATUS_NO_MEMORY;
2017 goto done;
2019 keystr = normalize_reg_path(ctx, keystr);
2020 if (!keystr) {
2021 status = NT_STATUS_NO_MEMORY;
2022 goto done;
2025 status = dbwrap_fetch_bystring(db, ctx, keystr, &old_data);
2027 if (NT_STATUS_IS_OK(status)
2028 && (old_data.dptr != NULL)
2029 && (old_data.dsize == data.dsize)
2030 && (memcmp(old_data.dptr, data.dptr, data.dsize) == 0))
2032 status = NT_STATUS_OK;
2033 goto done;
2036 status = dbwrap_trans_store_bystring(db, keystr, data, TDB_REPLACE);
2037 if (!NT_STATUS_IS_OK(status)) {
2038 DEBUG(0, ("regdb_store_values_internal: error storing: %s\n", nt_errstr(status)));
2039 goto done;
2043 * update the seqnum in the cache to prevent the next read
2044 * from going to disk
2046 werr = regval_ctr_set_seqnum(values, dbwrap_get_seqnum(db));
2047 status = werror_to_ntstatus(werr);
2049 done:
2050 TALLOC_FREE(ctx);
2051 return status;
2054 struct regdb_store_values_ctx {
2055 const char *key;
2056 struct regval_ctr *values;
2059 static NTSTATUS regdb_store_values_action(struct db_context *db,
2060 void *private_data)
2062 NTSTATUS status;
2063 struct regdb_store_values_ctx *ctx =
2064 (struct regdb_store_values_ctx *)private_data;
2066 status = regdb_store_values_internal(db, ctx->key, ctx->values);
2068 return status;
2071 static bool regdb_store_values(const char *key, struct regval_ctr *values)
2073 WERROR werr;
2074 struct regdb_store_values_ctx ctx;
2076 ctx.key = key;
2077 ctx.values = values;
2079 werr = regdb_trans_do(regdb, regdb_store_values_action, &ctx);
2081 return W_ERROR_IS_OK(werr);
2084 static WERROR regdb_get_secdesc(TALLOC_CTX *mem_ctx, const char *key,
2085 struct security_descriptor **psecdesc)
2087 char *tdbkey;
2088 TDB_DATA data;
2089 NTSTATUS status;
2090 TALLOC_CTX *tmp_ctx = talloc_stackframe();
2091 WERROR err = WERR_OK;
2093 DEBUG(10, ("regdb_get_secdesc: Getting secdesc of key [%s]\n", key));
2095 if (!regdb_key_exists(regdb, key)) {
2096 err = WERR_BADFILE;
2097 goto done;
2100 tdbkey = talloc_asprintf(tmp_ctx, "%s\\%s", REG_SECDESC_PREFIX, key);
2101 if (tdbkey == NULL) {
2102 err = WERR_NOMEM;
2103 goto done;
2106 tdbkey = normalize_reg_path(tmp_ctx, tdbkey);
2107 if (tdbkey == NULL) {
2108 err = WERR_NOMEM;
2109 goto done;
2112 status = dbwrap_fetch_bystring(regdb, tmp_ctx, tdbkey, &data);
2113 if (!NT_STATUS_IS_OK(status)) {
2114 err = WERR_BADFILE;
2115 goto done;
2118 status = unmarshall_sec_desc(mem_ctx, (uint8_t *)data.dptr, data.dsize,
2119 psecdesc);
2121 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MEMORY)) {
2122 err = WERR_NOMEM;
2123 } else if (!NT_STATUS_IS_OK(status)) {
2124 err = WERR_REG_CORRUPT;
2127 done:
2128 TALLOC_FREE(tmp_ctx);
2129 return err;
2132 struct regdb_set_secdesc_ctx {
2133 const char *key;
2134 struct security_descriptor *secdesc;
2137 static NTSTATUS regdb_set_secdesc_action(struct db_context *db,
2138 void *private_data)
2140 char *tdbkey;
2141 NTSTATUS status;
2142 TDB_DATA tdbdata;
2143 struct regdb_set_secdesc_ctx *ctx =
2144 (struct regdb_set_secdesc_ctx *)private_data;
2145 TALLOC_CTX *frame = talloc_stackframe();
2147 tdbkey = talloc_asprintf(frame, "%s\\%s", REG_SECDESC_PREFIX, ctx->key);
2148 if (tdbkey == NULL) {
2149 status = NT_STATUS_NO_MEMORY;
2150 goto done;
2153 tdbkey = normalize_reg_path(frame, tdbkey);
2154 if (tdbkey == NULL) {
2155 status = NT_STATUS_NO_MEMORY;
2156 goto done;
2159 if (ctx->secdesc == NULL) {
2160 /* assuming a delete */
2161 status = dbwrap_delete_bystring(db, tdbkey);
2162 goto done;
2165 status = marshall_sec_desc(frame, ctx->secdesc, &tdbdata.dptr,
2166 &tdbdata.dsize);
2167 if (!NT_STATUS_IS_OK(status)) {
2168 goto done;
2171 status = dbwrap_store_bystring(db, tdbkey, tdbdata, 0);
2173 done:
2174 TALLOC_FREE(frame);
2175 return status;
2178 static WERROR regdb_set_secdesc(const char *key,
2179 struct security_descriptor *secdesc)
2181 WERROR err;
2182 struct regdb_set_secdesc_ctx ctx;
2184 if (!regdb_key_exists(regdb, key)) {
2185 err = WERR_BADFILE;
2186 goto done;
2189 ctx.key = key;
2190 ctx.secdesc = secdesc;
2192 err = regdb_trans_do(regdb, regdb_set_secdesc_action, &ctx);
2194 done:
2195 return err;
2198 static bool regdb_subkeys_need_update(struct regsubkey_ctr *subkeys)
2200 return (regdb_get_seqnum() != regsubkey_ctr_get_seqnum(subkeys));
2203 static bool regdb_values_need_update(struct regval_ctr *values)
2205 return (regdb_get_seqnum() != regval_ctr_get_seqnum(values));
2209 * Table of function pointers for default access
2212 struct registry_ops regdb_ops = {
2213 .fetch_subkeys = regdb_fetch_keys,
2214 .fetch_values = regdb_fetch_values,
2215 .store_subkeys = regdb_store_keys,
2216 .store_values = regdb_store_values,
2217 .create_subkey = regdb_create_subkey,
2218 .delete_subkey = regdb_delete_subkey,
2219 .get_secdesc = regdb_get_secdesc,
2220 .set_secdesc = regdb_set_secdesc,
2221 .subkeys_need_update = regdb_subkeys_need_update,
2222 .values_need_update = regdb_values_need_update