ctdb-common: replace talloc / memcpy by talloc_memdup
[Samba.git] / source3 / registry / reg_backend_db.c
blob928572e8a5a1644e8e25581365058700b8bd05de
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_PCC,
125 KEY_PRINTING_PORTS,
126 KEY_PRINTING,
127 KEY_PRINTING "\\Forms",
128 KEY_PRINTING "\\Printers",
129 KEY_PRINTING "\\Environments\\Windows NT x86\\Print Processors\\winprint",
130 KEY_PRINTING "\\Environments\\Windows x64\\Print Processors\\winprint",
131 KEY_SHARES,
132 KEY_EVENTLOG,
133 KEY_SMBCONF,
134 KEY_PERFLIB,
135 KEY_PERFLIB_009,
136 KEY_GROUP_POLICY,
137 KEY_SAMBA_GROUP_POLICY,
138 KEY_GP_MACHINE_POLICY,
139 KEY_GP_MACHINE_WIN_POLICY,
140 KEY_HKCU,
141 KEY_GP_USER_POLICY,
142 KEY_GP_USER_WIN_POLICY,
143 "HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon\\GPExtensions",
144 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Print\\Monitors",
145 KEY_PROD_OPTIONS,
146 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\DefaultUserConfiguration",
147 KEY_TCPIP_PARAMS,
148 KEY_NETLOGON_PARAMS,
149 KEY_HKU,
150 KEY_HKCR,
151 KEY_HKPD,
152 KEY_HKPT,
153 NULL };
155 struct builtin_regkey_value {
156 const char *path;
157 const char *valuename;
158 uint32_t type;
159 union {
160 const char *string;
161 uint32_t dw_value;
162 } data;
165 static struct builtin_regkey_value builtin_registry_values[] = {
166 { KEY_PRINTING_PORTS,
167 SAMBA_PRINTER_PORT_NAME, REG_SZ, { "" } },
168 { KEY_PRINTING_2K,
169 "DefaultSpoolDirectory", REG_SZ, { "C:\\Windows\\System32\\Spool\\Printers" } },
170 { KEY_EVENTLOG,
171 "DisplayName", REG_SZ, { "Event Log" } },
172 { KEY_EVENTLOG,
173 "ErrorControl", REG_DWORD, { (char*)0x00000001 } },
174 { NULL, NULL, 0, { NULL } }
177 static WERROR create_key_recursive(struct db_context *db,
178 char *path,
179 const char *subkey)
181 WERROR werr;
182 char *p;
184 if (subkey == NULL) {
185 return WERR_INVALID_PARAMETER;
188 if (path == NULL) {
189 return regdb_create_basekey(db, subkey);
192 p = strrchr_m(path, '\\');
194 if (p == NULL) {
195 werr = create_key_recursive(db, NULL, path);
196 } else {
197 *p = '\0';
198 werr = create_key_recursive(db, path, p+1);
199 *p = '\\';
202 if (!W_ERROR_IS_OK(werr)) {
203 goto done;
206 werr = regdb_create_subkey_internal(db, path, subkey);
208 done:
209 return werr;
213 * Initialize a key in the registry:
214 * create each component key of the specified path.
216 static WERROR init_registry_key_internal(struct db_context *db,
217 const char *add_path)
219 char *subkey, *key;
220 WERROR werr;
221 TALLOC_CTX *frame = talloc_stackframe();
223 if (add_path == NULL) {
224 werr = WERR_INVALID_PARAMETER;
225 goto done;
228 key = talloc_strdup(frame, add_path);
230 subkey = strrchr_m(key, '\\');
231 if (subkey == NULL) {
232 subkey = key;
233 key = NULL;
234 } else {
235 *subkey = '\0';
236 subkey++;
239 werr = create_key_recursive(db, key, subkey);
241 done:
242 talloc_free(frame);
243 return werr;
246 struct init_registry_key_context {
247 const char *add_path;
250 static NTSTATUS init_registry_key_action(struct db_context *db,
251 void *private_data)
253 struct init_registry_key_context *init_ctx =
254 (struct init_registry_key_context *)private_data;
256 return werror_to_ntstatus(init_registry_key_internal(
257 db, init_ctx->add_path));
261 * Initialize a key in the registry:
262 * create each component key of the specified path,
263 * wrapped in one db transaction.
265 WERROR init_registry_key(const char *add_path)
267 struct init_registry_key_context init_ctx;
269 if (regdb_key_exists(regdb, add_path)) {
270 return WERR_OK;
273 init_ctx.add_path = add_path;
275 return regdb_trans_do(regdb,
276 init_registry_key_action,
277 &init_ctx);
280 /***********************************************************************
281 Open the registry data in the tdb
282 ***********************************************************************/
284 static void regdb_ctr_add_value(struct regval_ctr *ctr,
285 struct builtin_regkey_value *value)
287 switch(value->type) {
288 case REG_DWORD:
289 regval_ctr_addvalue(ctr, value->valuename, REG_DWORD,
290 (uint8_t *)&value->data.dw_value,
291 sizeof(uint32_t));
292 break;
294 case REG_SZ:
295 regval_ctr_addvalue_sz(ctr, value->valuename,
296 value->data.string);
297 break;
299 default:
300 DEBUG(0, ("regdb_ctr_add_value: invalid value type in "
301 "registry values [%d]\n", value->type));
305 static NTSTATUS init_registry_data_action(struct db_context *db,
306 void *private_data)
308 NTSTATUS status;
309 TALLOC_CTX *frame = talloc_stackframe();
310 struct regval_ctr *values;
311 int i;
313 /* loop over all of the predefined paths and add each component */
315 for (i=0; builtin_registry_paths[i] != NULL; i++) {
316 if (regdb_key_exists(db, builtin_registry_paths[i])) {
317 continue;
319 status = werror_to_ntstatus(init_registry_key_internal(db,
320 builtin_registry_paths[i]));
321 if (!NT_STATUS_IS_OK(status)) {
322 goto done;
326 /* loop over all of the predefined values and add each component */
328 for (i=0; builtin_registry_values[i].path != NULL; i++) {
329 WERROR werr;
331 werr = regval_ctr_init(frame, &values);
332 if (!W_ERROR_IS_OK(werr)) {
333 status = werror_to_ntstatus(werr);
334 goto done;
337 regdb_fetch_values_internal(db,
338 builtin_registry_values[i].path,
339 values);
341 /* preserve existing values across restarts. Only add new ones */
343 if (!regval_ctr_value_exists(values,
344 builtin_registry_values[i].valuename))
346 regdb_ctr_add_value(values,
347 &builtin_registry_values[i]);
348 status = regdb_store_values_internal(db,
349 builtin_registry_values[i].path,
350 values);
351 if (!NT_STATUS_IS_OK(status)) {
352 goto done;
355 TALLOC_FREE(values);
358 status = NT_STATUS_OK;
360 done:
362 TALLOC_FREE(frame);
363 return status;
366 WERROR init_registry_data(void)
368 WERROR werr;
369 TALLOC_CTX *frame = talloc_stackframe();
370 struct regval_ctr *values;
371 int i;
374 * First, check for the existence of the needed keys and values.
375 * If all do already exist, we can save the writes.
377 for (i=0; builtin_registry_paths[i] != NULL; i++) {
378 if (!regdb_key_exists(regdb, builtin_registry_paths[i])) {
379 goto do_init;
383 for (i=0; builtin_registry_values[i].path != NULL; i++) {
384 werr = regval_ctr_init(frame, &values);
385 W_ERROR_NOT_OK_GOTO_DONE(werr);
387 regdb_fetch_values_internal(regdb,
388 builtin_registry_values[i].path,
389 values);
390 if (!regval_ctr_value_exists(values,
391 builtin_registry_values[i].valuename))
393 TALLOC_FREE(values);
394 goto do_init;
397 TALLOC_FREE(values);
400 werr = WERR_OK;
401 goto done;
403 do_init:
406 * There are potentially quite a few store operations which are all
407 * indiviually wrapped in tdb transactions. Wrapping them in a single
408 * transaction gives just a single transaction_commit() to actually do
409 * its fsync()s. See tdb/common/transaction.c for info about nested
410 * transaction behaviour.
413 werr = regdb_trans_do(regdb,
414 init_registry_data_action,
415 NULL);
417 done:
418 TALLOC_FREE(frame);
419 return werr;
422 static int regdb_normalize_keynames_fn(struct db_record *rec,
423 void *private_data)
425 TALLOC_CTX *mem_ctx = talloc_tos();
426 const char *keyname;
427 NTSTATUS status;
428 TDB_DATA key;
429 TDB_DATA value;
430 struct db_context *db = (struct db_context *)private_data;
432 key = dbwrap_record_get_key(rec);
433 if (key.dptr == NULL || key.dsize == 0) {
434 return 0;
437 value = dbwrap_record_get_value(rec);
439 if (db == NULL) {
440 DEBUG(0, ("regdb_normalize_keynames_fn: ERROR: "
441 "NULL db context handed in via private_data\n"));
442 return 1;
445 if (strncmp((const char *)key.dptr, REGDB_VERSION_KEYNAME,
446 strlen(REGDB_VERSION_KEYNAME)) == 0)
448 return 0;
451 keyname = strchr((const char *)key.dptr, '/');
452 if (keyname) {
453 keyname = talloc_string_sub(mem_ctx,
454 (const char *)key.dptr,
455 "/",
456 "\\");
458 DEBUG(2, ("regdb_normalize_keynames_fn: Convert %s to %s\n",
459 (const char *)key.dptr,
460 keyname));
462 /* Delete the original record and store the normalized key */
463 status = dbwrap_record_delete(rec);
464 if (!NT_STATUS_IS_OK(status)) {
465 DEBUG(0,("regdb_normalize_keynames_fn: "
466 "tdb_delete for [%s] failed!\n",
467 (const char *)key.dptr));
468 return 1;
471 status = dbwrap_store_bystring(db, keyname, value, TDB_REPLACE);
472 if (!NT_STATUS_IS_OK(status)) {
473 DEBUG(0,("regdb_normalize_keynames_fn: "
474 "failed to store new record for [%s]!\n",
475 keyname));
476 return 1;
480 return 0;
483 static WERROR regdb_store_regdb_version(struct db_context *db, uint32_t version)
485 NTSTATUS status;
486 if (db == NULL) {
487 return WERR_CAN_NOT_COMPLETE;
490 status = dbwrap_trans_store_int32_bystring(db, REGDB_VERSION_KEYNAME,
491 version);
492 if (!NT_STATUS_IS_OK(status)) {
493 DEBUG(1, ("regdb_store_regdb_version: error storing %s = %d: %s\n",
494 REGDB_VERSION_KEYNAME, version, nt_errstr(status)));
495 return ntstatus_to_werror(status);
496 } else {
497 DEBUG(10, ("regdb_store_regdb_version: stored %s = %d\n",
498 REGDB_VERSION_KEYNAME, version));
499 return WERR_OK;
503 static WERROR regdb_upgrade_v1_to_v2(struct db_context *db)
505 TALLOC_CTX *mem_ctx;
506 NTSTATUS status;
507 WERROR werr;
509 mem_ctx = talloc_stackframe();
511 status = dbwrap_traverse(db, regdb_normalize_keynames_fn, db, NULL);
512 if (!NT_STATUS_IS_OK(status)) {
513 werr = WERR_REGISTRY_IO_FAILED;
514 goto done;
517 werr = regdb_store_regdb_version(db, REGDB_VERSION_V2);
519 done:
520 talloc_free(mem_ctx);
521 return werr;
524 static bool tdb_data_read_uint32(TDB_DATA *buf, uint32_t *result)
526 const size_t len = sizeof(uint32_t);
527 if (buf->dsize >= len) {
528 *result = IVAL(buf->dptr, 0);
529 buf->dptr += len;
530 buf->dsize -= len;
531 return true;
533 return false;
536 static bool tdb_data_read_cstr(TDB_DATA *buf, char **result)
538 const size_t len = strnlen((char*)buf->dptr, buf->dsize) + 1;
539 if (buf->dsize >= len) {
540 *result = (char*)buf->dptr;
541 buf->dptr += len;
542 buf->dsize -= len;
543 return true;
545 return false;
548 static bool tdb_data_is_cstr(TDB_DATA d) {
549 if (tdb_data_is_empty(d) || (d.dptr[d.dsize-1] != '\0')) {
550 return false;
552 return strlen((char *)d.dptr) == (d.dsize-1);
555 static bool upgrade_v2_to_v3_check_subkeylist(struct db_context *db,
556 const char *key,
557 const char *subkey)
559 static uint32_t zero = 0;
560 static TDB_DATA empty_subkey_list = {
561 .dptr = (unsigned char*)&zero,
562 .dsize = sizeof(uint32_t),
564 bool success = false;
565 char *path = talloc_asprintf(talloc_tos(), "%s\\%s", key, subkey);
566 if (!strupper_m(path)) {
567 goto done;
570 if (!dbwrap_exists(db, string_term_tdb_data(path))) {
571 NTSTATUS status;
573 DEBUG(10, ("regdb_upgrade_v2_to_v3: writing subkey list [%s]\n",
574 path));
576 status = dbwrap_store_bystring(db, path, empty_subkey_list,
577 TDB_INSERT);
578 if (!NT_STATUS_IS_OK(status)) {
579 DEBUG(0, ("regdb_upgrade_v2_to_v3: writing subkey list "
580 "[%s] failed\n", path));
581 goto done;
584 success = true;
585 done:
586 talloc_free(path);
587 return success;
590 static bool upgrade_v2_to_v3_check_parent(struct db_context *db,
591 const char *key)
593 const char *sep = strrchr_m(key, '\\');
594 if (sep != NULL) {
595 char *pkey = talloc_strndup(talloc_tos(), key, sep-key);
596 if (!dbwrap_exists(db, string_term_tdb_data(pkey))) {
597 DEBUG(0, ("regdb_upgrade_v2_to_v3: missing subkey list "
598 "[%s]\nrun \"net registry check\"\n", pkey));
600 talloc_free(pkey);
602 return true;
606 #define IS_EQUAL(d,s) (((d).dsize == strlen(s)+1) && \
607 (strcmp((char*)(d).dptr, (s)) == 0))
608 #define STARTS_WITH(d,s) (((d).dsize > strlen(s)) && \
609 (strncmp((char*)(d).dptr, (s), strlen(s)) == 0))
610 #define SSTR(d) (int)(d).dsize , (char*)(d).dptr
613 static int regdb_upgrade_v2_to_v3_fn(struct db_record *rec, void *private_data)
615 struct db_context *db = (struct db_context *)private_data;
616 TDB_DATA key = dbwrap_record_get_key(rec);
617 TDB_DATA val = dbwrap_record_get_value(rec);
619 if (tdb_data_is_empty(key)) {
620 return 0;
623 if (db == NULL) {
624 DEBUG(0, ("regdb_upgrade_v2_to_v3_fn: ERROR: "
625 "NULL db context handed in via private_data\n"));
626 return 1;
629 if (IS_EQUAL(key, REGDB_VERSION_KEYNAME) ||
630 STARTS_WITH(key, REG_VALUE_PREFIX) ||
631 STARTS_WITH(key, REG_SECDESC_PREFIX))
633 DEBUG(10, ("regdb_upgrade_v2_to_v3: skipping [%.*s]\n",
634 SSTR(key)));
635 return 0;
638 if (STARTS_WITH(key, REG_SORTED_SUBKEYS_PREFIX)) {
639 NTSTATUS status;
640 /* Delete the deprecated sorted subkeys cache. */
642 DEBUG(10, ("regdb_upgrade_v2_to_v3: deleting [%.*s]\n",
643 SSTR(key)));
645 status = dbwrap_record_delete(rec);
646 if (!NT_STATUS_IS_OK(status)) {
647 DEBUG(0, ("regdb_upgrade_v2_to_v3: deleting [%.*s] "
648 "failed!\n", SSTR(key)));
649 return 1;
652 return 0;
655 if ( tdb_data_is_cstr(key) &&
656 hive_info((char*)key.dptr) != NULL )
659 * Found a regular subkey list record.
660 * Walk the list and create the list record for those
661 * subkeys that don't already have one.
663 TDB_DATA pos = val;
664 char *subkey, *path = (char*)key.dptr;
665 uint32_t num_items, found_items = 0;
668 DEBUG(10, ("regdb_upgrade_v2_to_v3: scanning subkeylist of "
669 "[%s]\n", path));
671 if (!tdb_data_read_uint32(&pos, &num_items)) {
672 /* invalid or empty - skip */
673 return 0;
676 while (tdb_data_read_cstr(&pos, &subkey)) {
677 found_items++;
679 if (!upgrade_v2_to_v3_check_subkeylist(db, path, subkey))
681 return 1;
684 if (!upgrade_v2_to_v3_check_parent(db, path)) {
685 return 1;
688 if (found_items != num_items) {
689 DEBUG(0, ("regdb_upgrade_v2_to_v3: inconsistent subkey "
690 "list [%s]\nrun \"net registry check\"\n",
691 path));
693 } else {
694 DEBUG(10, ("regdb_upgrade_v2_to_v3: skipping invalid [%.*s]\n"
695 "run \"net registry check\"\n", SSTR(key)));
698 return 0;
701 static WERROR regdb_upgrade_v2_to_v3(struct db_context *db)
703 NTSTATUS status;
704 WERROR werr;
706 status = dbwrap_traverse(db, regdb_upgrade_v2_to_v3_fn, db, NULL);
707 if (!NT_STATUS_IS_OK(status)) {
708 werr = WERR_REGISTRY_IO_FAILED;
709 goto done;
712 werr = regdb_store_regdb_version(db, REGDB_VERSION_V3);
714 done:
715 return werr;
718 /***********************************************************************
719 Open the registry database
720 ***********************************************************************/
722 WERROR regdb_init(void)
724 int32_t vers_id;
725 WERROR werr;
726 NTSTATUS status;
727 char *db_path;
729 if (regdb) {
730 DEBUG(10, ("regdb_init: incrementing refcount (%d->%d)\n",
731 regdb_refcount, regdb_refcount+1));
732 regdb_refcount++;
733 return WERR_OK;
736 db_path = state_path("registry.tdb");
737 if (db_path == NULL) {
738 return WERR_NOT_ENOUGH_MEMORY;
741 regdb = db_open(NULL, db_path, 0,
742 REG_TDB_FLAGS, O_RDWR, 0600,
743 DBWRAP_LOCK_ORDER_1, REG_DBWRAP_FLAGS);
744 if (!regdb) {
745 regdb = db_open(NULL, db_path, 0,
746 REG_TDB_FLAGS, O_RDWR|O_CREAT, 0600,
747 DBWRAP_LOCK_ORDER_1, REG_DBWRAP_FLAGS);
748 if (!regdb) {
749 werr = ntstatus_to_werror(map_nt_error_from_unix(errno));
750 DEBUG(1,("regdb_init: Failed to open registry %s (%s)\n",
751 db_path, strerror(errno) ));
752 TALLOC_FREE(db_path);
753 return werr;
756 werr = regdb_store_regdb_version(regdb, REGDB_CODE_VERSION);
757 if (!W_ERROR_IS_OK(werr)) {
758 DEBUG(1, ("regdb_init: Failed to store version: %s\n",
759 win_errstr(werr)));
760 TALLOC_FREE(db_path);
761 return werr;
764 DEBUG(10,("regdb_init: Successfully created registry tdb\n"));
766 TALLOC_FREE(db_path);
768 regdb_refcount = 1;
769 DEBUG(10, ("regdb_init: registry db openend. refcount reset (%d)\n",
770 regdb_refcount));
772 status = dbwrap_fetch_int32_bystring(regdb, REGDB_VERSION_KEYNAME,
773 &vers_id);
774 if (!NT_STATUS_IS_OK(status)) {
775 DEBUG(10, ("regdb_init: registry version uninitialized "
776 "(got %d), initializing to version %d\n",
777 vers_id, REGDB_VERSION_V1));
780 * There was a regdb format version prior to version 1
781 * which did not store a INFO/version key. The format
782 * of this version was identical to version 1 except for
783 * the lack of the sorted subkey cache records.
784 * Since these are disposable, we can safely assume version
785 * 1 if no INFO/version key is found and run the db through
786 * the whole chain of upgrade. If the database was not
787 * initialized, this does not harm. If it was the unversioned
788 * version ("0"), then it do the right thing with the records.
790 werr = regdb_store_regdb_version(regdb, REGDB_VERSION_V1);
791 if (!W_ERROR_IS_OK(werr)) {
792 return werr;
794 vers_id = REGDB_VERSION_V1;
797 if (vers_id == REGDB_CODE_VERSION) {
798 return WERR_OK;
801 if (vers_id > REGDB_CODE_VERSION || vers_id == 0) {
802 DEBUG(0, ("regdb_init: unknown registry version %d "
803 "(code version = %d), refusing initialization\n",
804 vers_id, REGDB_CODE_VERSION));
805 return WERR_CAN_NOT_COMPLETE;
808 if (dbwrap_transaction_start(regdb) != 0) {
809 return WERR_REGISTRY_IO_FAILED;
812 if (vers_id == REGDB_VERSION_V1) {
813 DEBUG(10, ("regdb_init: upgrading registry from version %d "
814 "to %d\n", REGDB_VERSION_V1, REGDB_VERSION_V2));
816 werr = regdb_upgrade_v1_to_v2(regdb);
817 if (!W_ERROR_IS_OK(werr)) {
818 dbwrap_transaction_cancel(regdb);
819 return werr;
822 vers_id = REGDB_VERSION_V2;
825 if (vers_id == REGDB_VERSION_V2) {
826 DEBUG(10, ("regdb_init: upgrading registry from version %d "
827 "to %d\n", REGDB_VERSION_V2, REGDB_VERSION_V3));
829 werr = regdb_upgrade_v2_to_v3(regdb);
830 if (!W_ERROR_IS_OK(werr)) {
831 dbwrap_transaction_cancel(regdb);
832 return werr;
835 vers_id = REGDB_VERSION_V3;
838 /* future upgrade code should go here */
840 if (dbwrap_transaction_commit(regdb) != 0) {
841 return WERR_REGISTRY_IO_FAILED;
844 return WERR_OK;
847 /***********************************************************************
848 Open the registry. Must already have been initialized by regdb_init()
849 ***********************************************************************/
851 WERROR regdb_open( void )
853 WERROR result = WERR_OK;
854 char *db_path;
855 int saved_errno;
857 if ( regdb ) {
858 DEBUG(10, ("regdb_open: incrementing refcount (%d->%d)\n",
859 regdb_refcount, regdb_refcount+1));
860 regdb_refcount++;
861 return WERR_OK;
864 db_path = state_path("registry.tdb");
865 if (db_path == NULL) {
866 return WERR_NOT_ENOUGH_MEMORY;
869 become_root();
871 regdb = db_open(NULL, db_path, 0,
872 REG_TDB_FLAGS, O_RDWR, 0600,
873 DBWRAP_LOCK_ORDER_1, REG_DBWRAP_FLAGS);
874 saved_errno = errno;
875 unbecome_root();
876 if ( !regdb ) {
877 result = ntstatus_to_werror(map_nt_error_from_unix(saved_errno));
878 DEBUG(0,("regdb_open: Failed to open %s! (%s)\n",
879 db_path, strerror(saved_errno)));
880 TALLOC_FREE(db_path);
881 return result;
883 TALLOC_FREE(db_path);
885 regdb_refcount = 1;
886 DEBUG(10, ("regdb_open: registry db opened. refcount reset (%d)\n",
887 regdb_refcount));
889 return WERR_OK;
892 /***********************************************************************
893 ***********************************************************************/
895 int regdb_close( void )
897 if (regdb_refcount == 0) {
898 return 0;
901 regdb_refcount--;
903 DEBUG(10, ("regdb_close: decrementing refcount (%d->%d)\n",
904 regdb_refcount+1, regdb_refcount));
906 if ( regdb_refcount > 0 )
907 return 0;
909 SMB_ASSERT( regdb_refcount >= 0 );
911 TALLOC_FREE(regdb);
912 return 0;
915 WERROR regdb_transaction_start(void)
917 return (dbwrap_transaction_start(regdb) == 0) ?
918 WERR_OK : WERR_REGISTRY_IO_FAILED;
921 WERROR regdb_transaction_commit(void)
923 return (dbwrap_transaction_commit(regdb) == 0) ?
924 WERR_OK : WERR_REGISTRY_IO_FAILED;
927 WERROR regdb_transaction_cancel(void)
929 return (dbwrap_transaction_cancel(regdb) == 0) ?
930 WERR_OK : WERR_REGISTRY_IO_FAILED;
933 /***********************************************************************
934 return the tdb sequence number of the registry tdb.
935 this is an indicator for the content of the registry
936 having changed. it will change upon regdb_init, too, though.
937 ***********************************************************************/
938 int regdb_get_seqnum(void)
940 return dbwrap_get_seqnum(regdb);
944 static WERROR regdb_delete_key_with_prefix(struct db_context *db,
945 const char *keyname,
946 const char *prefix)
948 char *path;
949 WERROR werr = WERR_NOT_ENOUGH_MEMORY;
950 TALLOC_CTX *mem_ctx = talloc_stackframe();
952 if (keyname == NULL) {
953 werr = WERR_INVALID_PARAMETER;
954 goto done;
957 if (prefix == NULL) {
958 path = discard_const_p(char, keyname);
959 } else {
960 path = talloc_asprintf(mem_ctx, "%s\\%s", prefix, keyname);
961 if (path == NULL) {
962 goto done;
966 path = normalize_reg_path(mem_ctx, path);
967 if (path == NULL) {
968 goto done;
971 werr = ntstatus_to_werror(dbwrap_purge_bystring(db, path));
973 done:
974 talloc_free(mem_ctx);
975 return werr;
979 static WERROR regdb_delete_values(struct db_context *db, const char *keyname)
981 return regdb_delete_key_with_prefix(db, keyname, REG_VALUE_PREFIX);
984 static WERROR regdb_delete_secdesc(struct db_context *db, const char *keyname)
986 return regdb_delete_key_with_prefix(db, keyname, REG_SECDESC_PREFIX);
989 static WERROR regdb_delete_subkeylist(struct db_context *db, const char *keyname)
991 return regdb_delete_key_with_prefix(db, keyname, NULL);
995 static WERROR regdb_delete_key_lists(struct db_context *db, const char *keyname)
997 WERROR werr;
999 werr = regdb_delete_values(db, keyname);
1000 if (!W_ERROR_IS_OK(werr)) {
1001 DEBUG(1, (__location__ " Deleting %s\\%s failed: %s\n",
1002 REG_VALUE_PREFIX, keyname, win_errstr(werr)));
1003 goto done;
1006 werr = regdb_delete_secdesc(db, keyname);
1007 if (!W_ERROR_IS_OK(werr)) {
1008 DEBUG(1, (__location__ " Deleting %s\\%s failed: %s\n",
1009 REG_SECDESC_PREFIX, keyname, win_errstr(werr)));
1010 goto done;
1013 werr = regdb_delete_subkeylist(db, keyname);
1014 if (!W_ERROR_IS_OK(werr)) {
1015 DEBUG(1, (__location__ " Deleting %s failed: %s\n",
1016 keyname, win_errstr(werr)));
1017 goto done;
1020 done:
1021 return werr;
1024 /***********************************************************************
1025 Add subkey strings to the registry tdb under a defined key
1026 fmt is the same format as tdb_pack except this function only supports
1027 fstrings
1028 ***********************************************************************/
1030 static WERROR regdb_store_keys_internal2(struct db_context *db,
1031 const char *key,
1032 struct regsubkey_ctr *ctr)
1034 TDB_DATA dbuf;
1035 uint8_t *buffer = NULL;
1036 int i = 0;
1037 uint32_t len, buflen;
1038 uint32_t num_subkeys = regsubkey_ctr_numkeys(ctr);
1039 char *keyname = NULL;
1040 TALLOC_CTX *ctx = talloc_stackframe();
1041 WERROR werr;
1043 if (!key) {
1044 werr = WERR_INVALID_PARAMETER;
1045 goto done;
1048 keyname = talloc_strdup(ctx, key);
1049 if (!keyname) {
1050 werr = WERR_NOT_ENOUGH_MEMORY;
1051 goto done;
1054 keyname = normalize_reg_path(ctx, keyname);
1055 if (!keyname) {
1056 werr = WERR_NOT_ENOUGH_MEMORY;
1057 goto done;
1060 /* allocate some initial memory */
1062 buffer = (uint8_t *)SMB_MALLOC(1024);
1063 if (buffer == NULL) {
1064 werr = WERR_NOT_ENOUGH_MEMORY;
1065 goto done;
1067 buflen = 1024;
1068 len = 0;
1070 /* store the number of subkeys */
1072 len += tdb_pack(buffer+len, buflen-len, "d", num_subkeys);
1074 /* pack all the strings */
1076 for (i=0; i<num_subkeys; i++) {
1077 size_t thistime;
1079 thistime = tdb_pack(buffer+len, buflen-len, "f",
1080 regsubkey_ctr_specific_key(ctr, i));
1081 if (len+thistime > buflen) {
1082 size_t thistime2;
1084 * tdb_pack hasn't done anything because of the short
1085 * buffer, allocate extra space.
1087 buffer = SMB_REALLOC_ARRAY(buffer, uint8_t,
1088 (len+thistime)*2);
1089 if(buffer == NULL) {
1090 DEBUG(0, ("regdb_store_keys: Failed to realloc "
1091 "memory of size [%u]\n",
1092 (unsigned int)(len+thistime)*2));
1093 werr = WERR_NOT_ENOUGH_MEMORY;
1094 goto done;
1096 buflen = (len+thistime)*2;
1097 thistime2 = tdb_pack(
1098 buffer+len, buflen-len, "f",
1099 regsubkey_ctr_specific_key(ctr, i));
1100 if (thistime2 != thistime) {
1101 DEBUG(0, ("tdb_pack failed\n"));
1102 werr = WERR_CAN_NOT_COMPLETE;
1103 goto done;
1106 len += thistime;
1109 /* finally write out the data */
1111 dbuf.dptr = buffer;
1112 dbuf.dsize = len;
1113 werr = ntstatus_to_werror(dbwrap_store_bystring(db, keyname, dbuf,
1114 TDB_REPLACE));
1116 done:
1117 TALLOC_FREE(ctx);
1118 SAFE_FREE(buffer);
1119 return werr;
1123 * Utility function to store a new empty list of
1124 * subkeys of given key specified as parent and subkey name
1125 * (thereby creating the key).
1126 * If the parent keyname is NULL, then the "subkey" is
1127 * interpreted as a base key.
1128 * If the subkey list does already exist, it is not modified.
1130 * Must be called from within a transaction.
1132 static WERROR regdb_store_subkey_list(struct db_context *db, const char *parent,
1133 const char *key)
1135 WERROR werr;
1136 char *path = NULL;
1137 struct regsubkey_ctr *subkeys = NULL;
1138 TALLOC_CTX *frame = talloc_stackframe();
1140 if (parent == NULL) {
1141 path = talloc_strdup(frame, key);
1142 } else {
1143 path = talloc_asprintf(frame, "%s\\%s", parent, key);
1145 if (!path) {
1146 werr = WERR_NOT_ENOUGH_MEMORY;
1147 goto done;
1150 werr = regsubkey_ctr_init(frame, &subkeys);
1151 W_ERROR_NOT_OK_GOTO_DONE(werr);
1153 werr = regdb_fetch_keys_internal(db, path, subkeys);
1154 if (W_ERROR_IS_OK(werr)) {
1155 /* subkey list exists already - don't modify */
1156 goto done;
1159 werr = regsubkey_ctr_reinit(subkeys);
1160 W_ERROR_NOT_OK_GOTO_DONE(werr);
1162 /* create a record with 0 subkeys */
1163 werr = regdb_store_keys_internal2(db, path, subkeys);
1164 if (!W_ERROR_IS_OK(werr)) {
1165 DEBUG(0, ("regdb_store_keys: Failed to store new record for "
1166 "key [%s]: %s\n", path, win_errstr(werr)));
1167 goto done;
1170 done:
1171 talloc_free(frame);
1172 return werr;
1175 /***********************************************************************
1176 Store the new subkey record and create any child key records that
1177 do not currently exist
1178 ***********************************************************************/
1180 struct regdb_store_keys_context {
1181 const char *key;
1182 struct regsubkey_ctr *ctr;
1185 static NTSTATUS regdb_store_keys_action(struct db_context *db,
1186 void *private_data)
1188 struct regdb_store_keys_context *store_ctx;
1189 WERROR werr;
1190 int num_subkeys, i;
1191 char *path = NULL;
1192 struct regsubkey_ctr *old_subkeys = NULL;
1193 char *oldkeyname = NULL;
1194 TALLOC_CTX *mem_ctx = talloc_stackframe();
1196 store_ctx = (struct regdb_store_keys_context *)private_data;
1199 * Re-fetch the old keys inside the transaction
1202 werr = regsubkey_ctr_init(mem_ctx, &old_subkeys);
1203 W_ERROR_NOT_OK_GOTO_DONE(werr);
1205 werr = regdb_fetch_keys_internal(db, store_ctx->key, old_subkeys);
1206 if (!W_ERROR_IS_OK(werr) &&
1207 !W_ERROR_EQUAL(werr, WERR_NOT_FOUND))
1209 goto done;
1213 * Make the store operation as safe as possible without transactions:
1215 * (1) For each subkey removed from ctr compared with old_subkeys:
1217 * (a) First delete the value db entry.
1219 * (b) Next delete the secdesc db record.
1221 * (c) Then delete the subkey list entry.
1223 * (2) Now write the list of subkeys of the parent key,
1224 * deleting removed entries and adding new ones.
1226 * (3) Finally create the subkey list entries for the added keys.
1228 * This way if we crash half-way in between deleting the subkeys
1229 * and storing the parent's list of subkeys, no old data can pop up
1230 * out of the blue when re-adding keys later on.
1233 /* (1) delete removed keys' lists (values/secdesc/subkeys) */
1235 num_subkeys = regsubkey_ctr_numkeys(old_subkeys);
1236 for (i=0; i<num_subkeys; i++) {
1237 oldkeyname = regsubkey_ctr_specific_key(old_subkeys, i);
1239 if (regsubkey_ctr_key_exists(store_ctx->ctr, oldkeyname)) {
1241 * It's still around, don't delete
1243 continue;
1246 path = talloc_asprintf(mem_ctx, "%s\\%s", store_ctx->key,
1247 oldkeyname);
1248 if (!path) {
1249 werr = WERR_NOT_ENOUGH_MEMORY;
1250 goto done;
1253 werr = regdb_delete_key_lists(db, path);
1254 W_ERROR_NOT_OK_GOTO_DONE(werr);
1256 TALLOC_FREE(path);
1259 TALLOC_FREE(old_subkeys);
1261 /* (2) store the subkey list for the parent */
1263 werr = regdb_store_keys_internal2(db, store_ctx->key, store_ctx->ctr);
1264 if (!W_ERROR_IS_OK(werr)) {
1265 DEBUG(0,("regdb_store_keys: Failed to store new subkey list "
1266 "for parent [%s]: %s\n", store_ctx->key,
1267 win_errstr(werr)));
1268 goto done;
1271 /* (3) now create records for any subkeys that don't already exist */
1273 num_subkeys = regsubkey_ctr_numkeys(store_ctx->ctr);
1275 for (i=0; i<num_subkeys; i++) {
1276 const char *subkey;
1278 subkey = regsubkey_ctr_specific_key(store_ctx->ctr, i);
1280 werr = regdb_store_subkey_list(db, store_ctx->key, subkey);
1281 W_ERROR_NOT_OK_GOTO_DONE(werr);
1285 * Update the seqnum in the container to possibly
1286 * prevent next read from going to disk
1288 werr = regsubkey_ctr_set_seqnum(store_ctx->ctr, dbwrap_get_seqnum(db));
1290 done:
1291 talloc_free(mem_ctx);
1292 return werror_to_ntstatus(werr);
1295 static bool regdb_store_keys_internal(struct db_context *db, const char *key,
1296 struct regsubkey_ctr *ctr)
1298 int num_subkeys, old_num_subkeys, i;
1299 struct regsubkey_ctr *old_subkeys = NULL;
1300 TALLOC_CTX *ctx = talloc_stackframe();
1301 WERROR werr;
1302 bool ret = false;
1303 struct regdb_store_keys_context store_ctx;
1305 if (!regdb_key_exists(db, key)) {
1306 goto done;
1310 * fetch a list of the old subkeys so we can determine if anything has
1311 * changed
1314 werr = regsubkey_ctr_init(ctx, &old_subkeys);
1315 if (!W_ERROR_IS_OK(werr)) {
1316 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
1317 goto done;
1320 werr = regdb_fetch_keys_internal(db, key, old_subkeys);
1321 if (!W_ERROR_IS_OK(werr) &&
1322 !W_ERROR_EQUAL(werr, WERR_NOT_FOUND))
1324 goto done;
1327 num_subkeys = regsubkey_ctr_numkeys(ctr);
1328 old_num_subkeys = regsubkey_ctr_numkeys(old_subkeys);
1329 if ((num_subkeys && old_num_subkeys) &&
1330 (num_subkeys == old_num_subkeys)) {
1332 for (i = 0; i < num_subkeys; i++) {
1333 if (strcmp(regsubkey_ctr_specific_key(ctr, i),
1334 regsubkey_ctr_specific_key(old_subkeys, i))
1335 != 0)
1337 break;
1340 if (i == num_subkeys) {
1342 * Nothing changed, no point to even start a tdb
1343 * transaction
1346 ret = true;
1347 goto done;
1351 TALLOC_FREE(old_subkeys);
1353 store_ctx.key = key;
1354 store_ctx.ctr = ctr;
1356 werr = regdb_trans_do(db,
1357 regdb_store_keys_action,
1358 &store_ctx);
1360 ret = W_ERROR_IS_OK(werr);
1362 done:
1363 TALLOC_FREE(ctx);
1365 return ret;
1368 static bool regdb_store_keys(const char *key, struct regsubkey_ctr *ctr)
1370 return regdb_store_keys_internal(regdb, key, ctr);
1374 * create a subkey of a given key
1377 struct regdb_create_subkey_context {
1378 const char *key;
1379 const char *subkey;
1382 static NTSTATUS regdb_create_subkey_action(struct db_context *db,
1383 void *private_data)
1385 WERROR werr;
1386 struct regdb_create_subkey_context *create_ctx;
1387 struct regsubkey_ctr *subkeys;
1388 TALLOC_CTX *mem_ctx = talloc_stackframe();
1390 create_ctx = (struct regdb_create_subkey_context *)private_data;
1392 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
1393 W_ERROR_NOT_OK_GOTO_DONE(werr);
1395 werr = regdb_fetch_keys_internal(db, create_ctx->key, subkeys);
1396 W_ERROR_NOT_OK_GOTO_DONE(werr);
1398 werr = regsubkey_ctr_addkey(subkeys, create_ctx->subkey);
1399 W_ERROR_NOT_OK_GOTO_DONE(werr);
1401 werr = regdb_store_keys_internal2(db, create_ctx->key, subkeys);
1402 if (!W_ERROR_IS_OK(werr)) {
1403 DEBUG(0, (__location__ " failed to store new subkey list for "
1404 "parent key %s: %s\n", create_ctx->key,
1405 win_errstr(werr)));
1408 werr = regdb_store_subkey_list(db, create_ctx->key, create_ctx->subkey);
1410 done:
1411 talloc_free(mem_ctx);
1412 return werror_to_ntstatus(werr);
1415 static WERROR regdb_create_subkey_internal(struct db_context *db,
1416 const char *key,
1417 const char *subkey)
1419 WERROR werr;
1420 struct regsubkey_ctr *subkeys;
1421 TALLOC_CTX *mem_ctx = talloc_stackframe();
1422 struct regdb_create_subkey_context create_ctx;
1424 if (!regdb_key_exists(db, key)) {
1425 werr = WERR_NOT_FOUND;
1426 goto done;
1429 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
1430 W_ERROR_NOT_OK_GOTO_DONE(werr);
1432 werr = regdb_fetch_keys_internal(db, key, subkeys);
1433 W_ERROR_NOT_OK_GOTO_DONE(werr);
1435 if (regsubkey_ctr_key_exists(subkeys, subkey)) {
1436 char *newkey;
1438 newkey = talloc_asprintf(mem_ctx, "%s\\%s", key, subkey);
1439 if (newkey == NULL) {
1440 werr = WERR_NOT_ENOUGH_MEMORY;
1441 goto done;
1444 if (regdb_key_exists(db, newkey)) {
1445 werr = WERR_OK;
1446 goto done;
1450 talloc_free(subkeys);
1452 create_ctx.key = key;
1453 create_ctx.subkey = subkey;
1455 werr = regdb_trans_do(db,
1456 regdb_create_subkey_action,
1457 &create_ctx);
1459 done:
1460 talloc_free(mem_ctx);
1461 return werr;
1464 static WERROR regdb_create_subkey(const char *key, const char *subkey)
1466 return regdb_create_subkey_internal(regdb, key, subkey);
1470 * create a base key
1473 struct regdb_create_basekey_context {
1474 const char *key;
1477 static NTSTATUS regdb_create_basekey_action(struct db_context *db,
1478 void *private_data)
1480 WERROR werr;
1481 struct regdb_create_basekey_context *create_ctx;
1483 create_ctx = (struct regdb_create_basekey_context *)private_data;
1485 werr = regdb_store_subkey_list(db, NULL, create_ctx->key);
1487 return werror_to_ntstatus(werr);
1490 static WERROR regdb_create_basekey(struct db_context *db, const char *key)
1492 WERROR werr;
1493 struct regdb_create_subkey_context create_ctx;
1495 create_ctx.key = key;
1497 werr = regdb_trans_do(db,
1498 regdb_create_basekey_action,
1499 &create_ctx);
1501 return werr;
1505 * create a subkey of a given key
1508 struct regdb_delete_subkey_context {
1509 const char *key;
1510 const char *subkey;
1511 const char *path;
1512 bool lazy;
1515 static NTSTATUS regdb_delete_subkey_action(struct db_context *db,
1516 void *private_data)
1518 WERROR werr;
1519 struct regdb_delete_subkey_context *delete_ctx;
1520 struct regsubkey_ctr *subkeys;
1521 TALLOC_CTX *mem_ctx = talloc_stackframe();
1523 delete_ctx = (struct regdb_delete_subkey_context *)private_data;
1525 werr = regdb_delete_key_lists(db, delete_ctx->path);
1526 W_ERROR_NOT_OK_GOTO_DONE(werr);
1528 if (delete_ctx->lazy) {
1529 goto done;
1532 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
1533 W_ERROR_NOT_OK_GOTO_DONE(werr);
1535 werr = regdb_fetch_keys_internal(db, delete_ctx->key, subkeys);
1536 W_ERROR_NOT_OK_GOTO_DONE(werr);
1538 werr = regsubkey_ctr_delkey(subkeys, delete_ctx->subkey);
1539 W_ERROR_NOT_OK_GOTO_DONE(werr);
1541 werr = regdb_store_keys_internal2(db, delete_ctx->key, subkeys);
1542 if (!W_ERROR_IS_OK(werr)) {
1543 DEBUG(0, (__location__ " failed to store new subkey_list for "
1544 "parent key %s: %s\n", delete_ctx->key,
1545 win_errstr(werr)));
1548 done:
1549 talloc_free(mem_ctx);
1550 return werror_to_ntstatus(werr);
1553 static WERROR regdb_delete_subkey(const char *key, const char *subkey, bool lazy)
1555 WERROR werr;
1556 char *path;
1557 struct regdb_delete_subkey_context delete_ctx;
1558 TALLOC_CTX *mem_ctx = talloc_stackframe();
1560 if (!regdb_key_exists(regdb, key)) {
1561 werr = WERR_NOT_FOUND;
1562 goto done;
1565 path = talloc_asprintf(mem_ctx, "%s\\%s", key, subkey);
1566 if (path == NULL) {
1567 werr = WERR_NOT_ENOUGH_MEMORY;
1568 goto done;
1571 if (!regdb_key_exists(regdb, path)) {
1572 werr = WERR_OK;
1573 goto done;
1576 delete_ctx.key = key;
1577 delete_ctx.subkey = subkey;
1578 delete_ctx.path = path;
1579 delete_ctx.lazy = lazy;
1581 werr = regdb_trans_do(regdb,
1582 regdb_delete_subkey_action,
1583 &delete_ctx);
1585 done:
1586 talloc_free(mem_ctx);
1587 return werr;
1590 static TDB_DATA regdb_fetch_key_internal(struct db_context *db,
1591 TALLOC_CTX *mem_ctx, const char *key)
1593 char *path = NULL;
1594 TDB_DATA data;
1595 NTSTATUS status;
1597 path = normalize_reg_path(mem_ctx, key);
1598 if (!path) {
1599 return make_tdb_data(NULL, 0);
1602 status = dbwrap_fetch_bystring(db, mem_ctx, path, &data);
1603 if (!NT_STATUS_IS_OK(status)) {
1604 data = tdb_null;
1607 TALLOC_FREE(path);
1608 return data;
1613 * Check for the existence of a key.
1615 * Existence of a key is authoritatively defined by
1616 * the existence of the record that contains the list
1617 * of its subkeys.
1619 * Return false, if the record does not match the correct
1620 * structure of an initial 4-byte counter and then a
1621 * list of the corresponding number of zero-terminated
1622 * strings.
1624 static bool regdb_key_exists(struct db_context *db, const char *key)
1626 TALLOC_CTX *mem_ctx = talloc_stackframe();
1627 TDB_DATA value;
1628 bool ret = false;
1629 char *path;
1630 uint32_t buflen;
1631 const char *buf;
1632 uint32_t num_items, i;
1633 int32_t len;
1635 if (key == NULL) {
1636 goto done;
1639 path = normalize_reg_path(mem_ctx, key);
1640 if (path == NULL) {
1641 DEBUG(0, ("out of memory! (talloc failed)\n"));
1642 goto done;
1645 if (*path == '\0') {
1646 goto done;
1649 value = regdb_fetch_key_internal(db, mem_ctx, path);
1650 if (value.dptr == NULL) {
1651 goto done;
1654 if (value.dsize == 0) {
1655 DEBUG(10, ("regdb_key_exists: subkeylist-record for key "
1656 "[%s] is empty: Could be a deleted record in a "
1657 "clustered (ctdb) environment?\n",
1658 path));
1659 goto done;
1662 len = tdb_unpack(value.dptr, value.dsize, "d", &num_items);
1663 if (len == (int32_t)-1) {
1664 DEBUG(1, ("regdb_key_exists: ERROR: subkeylist-record for key "
1665 "[%s] is invalid: Could not parse initial 4-byte "
1666 "counter. record data length is %u.\n",
1667 path, (unsigned int)value.dsize));
1668 goto done;
1672 * Note: the tdb_unpack check above implies that len <= value.dsize
1674 buflen = value.dsize - len;
1675 buf = (const char *)value.dptr + len;
1677 len = 0;
1679 for (i = 0; i < num_items; i++) {
1680 if (buflen == 0) {
1681 break;
1683 len = strnlen(buf, buflen) + 1;
1684 if (buflen < len) {
1685 DEBUG(1, ("regdb_key_exists: ERROR: subkeylist-record "
1686 "for key [%s] is corrupt: %u items expected, "
1687 "item number %u is not zero terminated.\n",
1688 path, num_items, i+1));
1689 goto done;
1692 buf += len;
1693 buflen -= len;
1696 if (buflen > 0) {
1697 DEBUG(1, ("regdb_key_exists: ERROR: subkeylist-record for key "
1698 "[%s] is corrupt: %u items expected and found, but "
1699 "the record contains additional %u bytes\n",
1700 path, num_items, buflen));
1701 goto done;
1704 if (i < num_items) {
1705 DEBUG(1, ("regdb_key_exists: ERROR: subkeylist-record for key "
1706 "[%s] is corrupt: %u items expected, but only %u "
1707 "items found.\n",
1708 path, num_items, i+1));
1709 goto done;
1712 ret = true;
1714 done:
1715 TALLOC_FREE(mem_ctx);
1716 return ret;
1720 /***********************************************************************
1721 Retrieve an array of strings containing subkeys. Memory should be
1722 released by the caller.
1723 ***********************************************************************/
1725 static WERROR regdb_fetch_keys_internal(struct db_context *db, const char *key,
1726 struct regsubkey_ctr *ctr)
1728 WERROR werr;
1729 uint32_t num_items;
1730 uint8_t *buf;
1731 uint32_t buflen, len;
1732 int i;
1733 fstring subkeyname;
1734 TALLOC_CTX *frame = talloc_stackframe();
1735 TDB_DATA value;
1736 int seqnum[2], count;
1738 DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key ? key : "NULL"));
1740 if (!regdb_key_exists(db, key)) {
1741 DEBUG(10, ("key [%s] not found\n", key));
1742 werr = WERR_NOT_FOUND;
1743 goto done;
1746 werr = regsubkey_ctr_reinit(ctr);
1747 W_ERROR_NOT_OK_GOTO_DONE(werr);
1749 count = 0;
1750 ZERO_STRUCT(value);
1751 seqnum[0] = dbwrap_get_seqnum(db);
1753 do {
1754 count++;
1755 TALLOC_FREE(value.dptr);
1756 value = regdb_fetch_key_internal(db, frame, key);
1757 seqnum[count % 2] = dbwrap_get_seqnum(db);
1759 } while (seqnum[0] != seqnum[1]);
1761 if (count > 1) {
1762 DEBUG(5, ("regdb_fetch_keys_internal: it took %d attempts to "
1763 "fetch key '%s' with constant seqnum\n",
1764 count, key));
1767 werr = regsubkey_ctr_set_seqnum(ctr, seqnum[0]);
1768 if (!W_ERROR_IS_OK(werr)) {
1769 goto done;
1772 if (value.dsize == 0 || value.dptr == NULL) {
1773 DEBUG(10, ("regdb_fetch_keys: no subkeys found for key [%s]\n",
1774 key));
1775 goto done;
1778 buf = value.dptr;
1779 buflen = value.dsize;
1780 len = tdb_unpack( buf, buflen, "d", &num_items);
1781 if (len == (uint32_t)-1) {
1782 werr = WERR_NOT_FOUND;
1783 goto done;
1786 for (i=0; i<num_items; i++) {
1787 len += tdb_unpack(buf+len, buflen-len, "f", subkeyname);
1788 werr = regsubkey_ctr_addkey(ctr, subkeyname);
1789 if (!W_ERROR_IS_OK(werr)) {
1790 DEBUG(5, ("regdb_fetch_keys: regsubkey_ctr_addkey "
1791 "failed: %s\n", win_errstr(werr)));
1792 num_items = 0;
1793 goto done;
1797 DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items));
1799 done:
1800 TALLOC_FREE(frame);
1801 return werr;
1804 static int regdb_fetch_keys(const char *key, struct regsubkey_ctr *ctr)
1806 WERROR werr;
1808 werr = regdb_fetch_keys_internal(regdb, key, ctr);
1809 if (!W_ERROR_IS_OK(werr)) {
1810 return -1;
1813 return regsubkey_ctr_numkeys(ctr);
1816 /****************************************************************************
1817 Unpack a list of registry values frem the TDB
1818 ***************************************************************************/
1820 static int regdb_unpack_values(struct regval_ctr *values, uint8_t *buf, int buflen)
1822 int len = 0;
1823 uint32_t type;
1824 fstring valuename;
1825 uint32_t size;
1826 uint8_t *data_p;
1827 uint32_t num_values = 0;
1828 int i;
1830 /* loop and unpack the rest of the registry values */
1832 len += tdb_unpack(buf+len, buflen-len, "d", &num_values);
1834 for ( i=0; i<num_values; i++ ) {
1835 /* unpack the next regval */
1837 type = REG_NONE;
1838 size = 0;
1839 data_p = NULL;
1840 valuename[0] = '\0';
1841 len += tdb_unpack(buf+len, buflen-len, "fdB",
1842 valuename,
1843 &type,
1844 &size,
1845 &data_p);
1847 regval_ctr_addvalue(values, valuename, type,
1848 (uint8_t *)data_p, size);
1849 SAFE_FREE(data_p); /* 'B' option to tdb_unpack does a malloc() */
1851 DEBUG(10, ("regdb_unpack_values: value[%d]: name[%s] len[%d]\n",
1852 i, valuename, size));
1855 return len;
1858 /****************************************************************************
1859 Pack all values in all printer keys
1860 ***************************************************************************/
1862 static int regdb_pack_values(struct regval_ctr *values, uint8_t *buf, int buflen)
1864 int len = 0;
1865 int i;
1866 struct regval_blob *val;
1867 int num_values;
1869 if ( !values )
1870 return 0;
1872 num_values = regval_ctr_numvals( values );
1874 /* pack the number of values first */
1876 len += tdb_pack( buf+len, buflen-len, "d", num_values );
1878 /* loop over all values */
1880 for ( i=0; i<num_values; i++ ) {
1881 val = regval_ctr_specific_value( values, i );
1882 len += tdb_pack(buf+len, buflen-len, "fdB",
1883 regval_name(val),
1884 regval_type(val),
1885 regval_size(val),
1886 regval_data_p(val) );
1889 return len;
1892 /***********************************************************************
1893 Retrieve an array of strings containing subkeys. Memory should be
1894 released by the caller.
1895 ***********************************************************************/
1897 static int regdb_fetch_values_internal(struct db_context *db, const char* key,
1898 struct regval_ctr *values)
1900 char *keystr = NULL;
1901 TALLOC_CTX *ctx = talloc_stackframe();
1902 int ret = 0;
1903 TDB_DATA value;
1904 WERROR werr;
1905 int seqnum[2], count;
1907 DEBUG(10,("regdb_fetch_values: Looking for values of key [%s]\n", key));
1909 if (!regdb_key_exists(db, key)) {
1910 DEBUG(10, ("regb_fetch_values: key [%s] does not exist\n",
1911 key));
1912 ret = -1;
1913 goto done;
1916 keystr = talloc_asprintf(ctx, "%s\\%s", REG_VALUE_PREFIX, key);
1917 if (!keystr) {
1918 goto done;
1921 ZERO_STRUCT(value);
1922 count = 0;
1923 seqnum[0] = dbwrap_get_seqnum(db);
1925 do {
1926 count++;
1927 TALLOC_FREE(value.dptr);
1928 value = regdb_fetch_key_internal(db, ctx, keystr);
1929 seqnum[count % 2] = dbwrap_get_seqnum(db);
1930 } while (seqnum[0] != seqnum[1]);
1932 if (count > 1) {
1933 DEBUG(5, ("regdb_fetch_values_internal: it took %d attempts "
1934 "to fetch key '%s' with constant seqnum\n",
1935 count, key));
1938 werr = regval_ctr_set_seqnum(values, seqnum[0]);
1939 if (!W_ERROR_IS_OK(werr)) {
1940 goto done;
1943 if (!value.dptr) {
1944 /* all keys have zero values by default */
1945 goto done;
1948 regdb_unpack_values(values, value.dptr, value.dsize);
1949 ret = regval_ctr_numvals(values);
1951 done:
1952 TALLOC_FREE(ctx);
1953 return ret;
1956 static int regdb_fetch_values(const char* key, struct regval_ctr *values)
1958 return regdb_fetch_values_internal(regdb, key, values);
1961 static NTSTATUS regdb_store_values_internal(struct db_context *db,
1962 const char *key,
1963 struct regval_ctr *values)
1965 TDB_DATA old_data, data;
1966 char *keystr = NULL;
1967 TALLOC_CTX *ctx = talloc_stackframe();
1968 int len;
1969 NTSTATUS status;
1970 WERROR werr;
1972 DEBUG(10,("regdb_store_values: Looking for values of key [%s]\n", key));
1974 if (!regdb_key_exists(db, key)) {
1975 status = NT_STATUS_NOT_FOUND;
1976 goto done;
1979 if (regval_ctr_numvals(values) == 0) {
1980 werr = regdb_delete_values(db, key);
1981 if (!W_ERROR_IS_OK(werr)) {
1982 status = werror_to_ntstatus(werr);
1983 goto done;
1987 * update the seqnum in the cache to prevent the next read
1988 * from going to disk
1990 werr = regval_ctr_set_seqnum(values, dbwrap_get_seqnum(db));
1991 status = werror_to_ntstatus(werr);
1992 goto done;
1995 ZERO_STRUCT(data);
1997 len = regdb_pack_values(values, data.dptr, data.dsize);
1998 if (len <= 0) {
1999 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
2000 status = NT_STATUS_UNSUCCESSFUL;
2001 goto done;
2004 data.dptr = talloc_array(ctx, uint8_t, len);
2005 data.dsize = len;
2007 len = regdb_pack_values(values, data.dptr, data.dsize);
2009 SMB_ASSERT( len == data.dsize );
2011 keystr = talloc_asprintf(ctx, "%s\\%s", REG_VALUE_PREFIX, key );
2012 if (!keystr) {
2013 status = NT_STATUS_NO_MEMORY;
2014 goto done;
2016 keystr = normalize_reg_path(ctx, keystr);
2017 if (!keystr) {
2018 status = NT_STATUS_NO_MEMORY;
2019 goto done;
2022 status = dbwrap_fetch_bystring(db, ctx, keystr, &old_data);
2024 if (NT_STATUS_IS_OK(status)
2025 && (old_data.dptr != NULL)
2026 && (old_data.dsize == data.dsize)
2027 && (memcmp(old_data.dptr, data.dptr, data.dsize) == 0))
2029 status = NT_STATUS_OK;
2030 goto done;
2033 status = dbwrap_trans_store_bystring(db, keystr, data, TDB_REPLACE);
2034 if (!NT_STATUS_IS_OK(status)) {
2035 DEBUG(0, ("regdb_store_values_internal: error storing: %s\n", nt_errstr(status)));
2036 goto done;
2040 * update the seqnum in the cache to prevent the next read
2041 * from going to disk
2043 werr = regval_ctr_set_seqnum(values, dbwrap_get_seqnum(db));
2044 status = werror_to_ntstatus(werr);
2046 done:
2047 TALLOC_FREE(ctx);
2048 return status;
2051 struct regdb_store_values_ctx {
2052 const char *key;
2053 struct regval_ctr *values;
2056 static NTSTATUS regdb_store_values_action(struct db_context *db,
2057 void *private_data)
2059 NTSTATUS status;
2060 struct regdb_store_values_ctx *ctx =
2061 (struct regdb_store_values_ctx *)private_data;
2063 status = regdb_store_values_internal(db, ctx->key, ctx->values);
2065 return status;
2068 static bool regdb_store_values(const char *key, struct regval_ctr *values)
2070 WERROR werr;
2071 struct regdb_store_values_ctx ctx;
2073 ctx.key = key;
2074 ctx.values = values;
2076 werr = regdb_trans_do(regdb, regdb_store_values_action, &ctx);
2078 return W_ERROR_IS_OK(werr);
2081 static WERROR regdb_get_secdesc(TALLOC_CTX *mem_ctx, const char *key,
2082 struct security_descriptor **psecdesc)
2084 char *tdbkey;
2085 TDB_DATA data;
2086 NTSTATUS status;
2087 TALLOC_CTX *tmp_ctx = talloc_stackframe();
2088 WERROR err = WERR_OK;
2090 DEBUG(10, ("regdb_get_secdesc: Getting secdesc of key [%s]\n", key));
2092 if (!regdb_key_exists(regdb, key)) {
2093 err = WERR_FILE_NOT_FOUND;
2094 goto done;
2097 tdbkey = talloc_asprintf(tmp_ctx, "%s\\%s", REG_SECDESC_PREFIX, key);
2098 if (tdbkey == NULL) {
2099 err = WERR_NOT_ENOUGH_MEMORY;
2100 goto done;
2103 tdbkey = normalize_reg_path(tmp_ctx, tdbkey);
2104 if (tdbkey == NULL) {
2105 err = WERR_NOT_ENOUGH_MEMORY;
2106 goto done;
2109 status = dbwrap_fetch_bystring(regdb, tmp_ctx, tdbkey, &data);
2110 if (!NT_STATUS_IS_OK(status)) {
2111 err = WERR_FILE_NOT_FOUND;
2112 goto done;
2115 status = unmarshall_sec_desc(mem_ctx, (uint8_t *)data.dptr, data.dsize,
2116 psecdesc);
2118 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MEMORY)) {
2119 err = WERR_NOT_ENOUGH_MEMORY;
2120 } else if (!NT_STATUS_IS_OK(status)) {
2121 err = WERR_REGISTRY_CORRUPT;
2124 done:
2125 TALLOC_FREE(tmp_ctx);
2126 return err;
2129 struct regdb_set_secdesc_ctx {
2130 const char *key;
2131 struct security_descriptor *secdesc;
2134 static NTSTATUS regdb_set_secdesc_action(struct db_context *db,
2135 void *private_data)
2137 char *tdbkey;
2138 NTSTATUS status;
2139 TDB_DATA tdbdata;
2140 struct regdb_set_secdesc_ctx *ctx =
2141 (struct regdb_set_secdesc_ctx *)private_data;
2142 TALLOC_CTX *frame = talloc_stackframe();
2144 tdbkey = talloc_asprintf(frame, "%s\\%s", REG_SECDESC_PREFIX, ctx->key);
2145 if (tdbkey == NULL) {
2146 status = NT_STATUS_NO_MEMORY;
2147 goto done;
2150 tdbkey = normalize_reg_path(frame, tdbkey);
2151 if (tdbkey == NULL) {
2152 status = NT_STATUS_NO_MEMORY;
2153 goto done;
2156 if (ctx->secdesc == NULL) {
2157 /* assuming a delete */
2158 status = dbwrap_delete_bystring(db, tdbkey);
2159 goto done;
2162 status = marshall_sec_desc(frame, ctx->secdesc, &tdbdata.dptr,
2163 &tdbdata.dsize);
2164 if (!NT_STATUS_IS_OK(status)) {
2165 goto done;
2168 status = dbwrap_store_bystring(db, tdbkey, tdbdata, 0);
2170 done:
2171 TALLOC_FREE(frame);
2172 return status;
2175 static WERROR regdb_set_secdesc(const char *key,
2176 struct security_descriptor *secdesc)
2178 WERROR err;
2179 struct regdb_set_secdesc_ctx ctx;
2181 if (!regdb_key_exists(regdb, key)) {
2182 err = WERR_FILE_NOT_FOUND;
2183 goto done;
2186 ctx.key = key;
2187 ctx.secdesc = secdesc;
2189 err = regdb_trans_do(regdb, regdb_set_secdesc_action, &ctx);
2191 done:
2192 return err;
2195 static bool regdb_subkeys_need_update(struct regsubkey_ctr *subkeys)
2197 return (regdb_get_seqnum() != regsubkey_ctr_get_seqnum(subkeys));
2200 static bool regdb_values_need_update(struct regval_ctr *values)
2202 return (regdb_get_seqnum() != regval_ctr_get_seqnum(values));
2206 * Table of function pointers for default access
2209 struct registry_ops regdb_ops = {
2210 .fetch_subkeys = regdb_fetch_keys,
2211 .fetch_values = regdb_fetch_values,
2212 .store_subkeys = regdb_store_keys,
2213 .store_values = regdb_store_values,
2214 .create_subkey = regdb_create_subkey,
2215 .delete_subkey = regdb_delete_subkey,
2216 .get_secdesc = regdb_get_secdesc,
2217 .set_secdesc = regdb_set_secdesc,
2218 .subkeys_need_update = regdb_subkeys_need_update,
2219 .values_need_update = regdb_values_need_update