s3:torture:delete: simplify return code handling, fixing a couple of return codes...
[Samba.git] / source3 / registry / reg_backend_db.c
blob75ad507e2b05b54a0f63215b963b92b4c0cc560f
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 type;
157 union {
158 const char *string;
159 uint32 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));
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 strchr((char *)d.dptr, '\0') == (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 strupper_m(path);
566 if (!dbwrap_exists(db, string_term_tdb_data(path))) {
567 NTSTATUS status;
569 DEBUG(10, ("regdb_upgrade_v2_to_v3: writing subkey list [%s]\n",
570 path));
572 status = dbwrap_store_bystring(db, path, empty_subkey_list,
573 TDB_INSERT);
574 if (!NT_STATUS_IS_OK(status)) {
575 DEBUG(0, ("regdb_upgrade_v2_to_v3: writing subkey list "
576 "[%s] failed\n", path));
577 goto done;
580 success = true;
581 done:
582 talloc_free(path);
583 return success;
586 static bool upgrade_v2_to_v3_check_parent(struct db_context *db,
587 const char *key)
589 const char *sep = strrchr_m(key, '\\');
590 if (sep != NULL) {
591 char *pkey = talloc_strndup(talloc_tos(), key, sep-key);
592 if (!dbwrap_exists(db, string_term_tdb_data(pkey))) {
593 DEBUG(0, ("regdb_upgrade_v2_to_v3: missing subkey list "
594 "[%s]\nrun \"net registry check\"\n", pkey));
596 talloc_free(pkey);
598 return true;
602 #define IS_EQUAL(d,s) (((d).dsize == strlen(s)+1) && \
603 (strcmp((char*)(d).dptr, (s)) == 0))
604 #define STARTS_WITH(d,s) (((d).dsize > strlen(s)) && \
605 (strncmp((char*)(d).dptr, (s), strlen(s)) == 0))
606 #define SSTR(d) (int)(d).dsize , (char*)(d).dptr
609 static int regdb_upgrade_v2_to_v3_fn(struct db_record *rec, void *private_data)
611 struct db_context *db = (struct db_context *)private_data;
612 TDB_DATA key = dbwrap_record_get_key(rec);
613 TDB_DATA val = dbwrap_record_get_value(rec);
615 if (tdb_data_is_empty(key)) {
616 return 0;
619 if (db == NULL) {
620 DEBUG(0, ("regdb_upgrade_v2_to_v3_fn: ERROR: "
621 "NULL db context handed in via private_data\n"));
622 return 1;
625 if (IS_EQUAL(key, REGDB_VERSION_KEYNAME) ||
626 STARTS_WITH(key, REG_VALUE_PREFIX) ||
627 STARTS_WITH(key, REG_SECDESC_PREFIX))
629 DEBUG(10, ("regdb_upgrade_v2_to_v3: skipping [%.*s]\n",
630 SSTR(key)));
631 return 0;
634 if (STARTS_WITH(key, REG_SORTED_SUBKEYS_PREFIX)) {
635 NTSTATUS status;
636 /* Delete the deprecated sorted subkeys cache. */
638 DEBUG(10, ("regdb_upgrade_v2_to_v3: deleting [%.*s]\n",
639 SSTR(key)));
641 status = dbwrap_record_delete(rec);
642 if (!NT_STATUS_IS_OK(status)) {
643 DEBUG(0, ("regdb_upgrade_v2_to_v3: deleting [%.*s] "
644 "failed!\n", SSTR(key)));
645 return 1;
648 return 0;
651 if ( tdb_data_is_cstr(key) &&
652 hive_info((char*)key.dptr) != NULL )
655 * Found a regular subkey list record.
656 * Walk the list and create the list record for those
657 * subkeys that don't already have one.
659 TDB_DATA pos = val;
660 char *subkey, *path = (char*)key.dptr;
661 uint32_t num_items, found_items = 0;
664 DEBUG(10, ("regdb_upgrade_v2_to_v3: scanning subkeylist of "
665 "[%s]\n", path));
667 if (!tdb_data_read_uint32(&pos, &num_items)) {
668 /* invalid or empty - skip */
669 return 0;
672 while (tdb_data_read_cstr(&pos, &subkey)) {
673 found_items++;
675 if (!upgrade_v2_to_v3_check_subkeylist(db, path, subkey))
677 return 1;
680 if (!upgrade_v2_to_v3_check_parent(db, path)) {
681 return 1;
684 if (found_items != num_items) {
685 DEBUG(0, ("regdb_upgrade_v2_to_v3: inconsistent subkey "
686 "list [%s]\nrun \"net registry check\"\n",
687 path));
689 } else {
690 DEBUG(10, ("regdb_upgrade_v2_to_v3: skipping invalid [%.*s]\n"
691 "run \"net registry check\"\n", SSTR(key)));
694 return 0;
697 static WERROR regdb_upgrade_v2_to_v3(struct db_context *db)
699 NTSTATUS status;
700 WERROR werr;
702 status = dbwrap_traverse(db, regdb_upgrade_v2_to_v3_fn, db, NULL);
703 if (!NT_STATUS_IS_OK(status)) {
704 werr = WERR_REG_IO_FAILURE;
705 goto done;
708 werr = regdb_store_regdb_version(db, REGDB_VERSION_V3);
710 done:
711 return werr;
714 /***********************************************************************
715 Open the registry database
716 ***********************************************************************/
718 WERROR regdb_init(void)
720 int32_t vers_id;
721 WERROR werr;
722 NTSTATUS status;
724 if (regdb) {
725 DEBUG(10, ("regdb_init: incrementing refcount (%d->%d)\n",
726 regdb_refcount, regdb_refcount+1));
727 regdb_refcount++;
728 return WERR_OK;
731 regdb = db_open(NULL, state_path("registry.tdb"), 0,
732 REG_TDB_FLAGS, O_RDWR, 0600,
733 DBWRAP_LOCK_ORDER_1);
734 if (!regdb) {
735 regdb = db_open(NULL, state_path("registry.tdb"), 0,
736 REG_TDB_FLAGS, O_RDWR|O_CREAT, 0600,
737 DBWRAP_LOCK_ORDER_1);
738 if (!regdb) {
739 werr = ntstatus_to_werror(map_nt_error_from_unix(errno));
740 DEBUG(1,("regdb_init: Failed to open registry %s (%s)\n",
741 state_path("registry.tdb"), strerror(errno) ));
742 return werr;
745 werr = regdb_store_regdb_version(regdb, REGDB_CODE_VERSION);
746 if (!W_ERROR_IS_OK(werr)) {
747 DEBUG(1, ("regdb_init: Failed to store version: %s\n",
748 win_errstr(werr)));
749 return werr;
752 DEBUG(10,("regdb_init: Successfully created registry tdb\n"));
755 regdb_refcount = 1;
756 DEBUG(10, ("regdb_init: registry db openend. refcount reset (%d)\n",
757 regdb_refcount));
759 status = dbwrap_fetch_int32_bystring(regdb, REGDB_VERSION_KEYNAME,
760 &vers_id);
761 if (!NT_STATUS_IS_OK(status)) {
762 DEBUG(10, ("regdb_init: registry version uninitialized "
763 "(got %d), initializing to version %d\n",
764 vers_id, REGDB_VERSION_V1));
767 * There was a regdb format version prior to version 1
768 * which did not store a INFO/version key. The format
769 * of this version was identical to version 1 except for
770 * the lack of the sorted subkey cache records.
771 * Since these are disposable, we can safely assume version
772 * 1 if no INFO/version key is found and run the db through
773 * the whole chain of upgrade. If the database was not
774 * initialized, this does not harm. If it was the unversioned
775 * version ("0"), then it do the right thing with the records.
777 werr = regdb_store_regdb_version(regdb, REGDB_VERSION_V1);
778 if (!W_ERROR_IS_OK(werr)) {
779 return werr;
781 vers_id = REGDB_VERSION_V1;
784 if (vers_id == REGDB_CODE_VERSION) {
785 return WERR_OK;
788 if (vers_id > REGDB_CODE_VERSION || vers_id == 0) {
789 DEBUG(0, ("regdb_init: unknown registry version %d "
790 "(code version = %d), refusing initialization\n",
791 vers_id, REGDB_CODE_VERSION));
792 return WERR_CAN_NOT_COMPLETE;
795 if (dbwrap_transaction_start(regdb) != 0) {
796 return WERR_REG_IO_FAILURE;
799 if (vers_id == REGDB_VERSION_V1) {
800 DEBUG(10, ("regdb_init: upgrading registry from version %d "
801 "to %d\n", REGDB_VERSION_V1, REGDB_VERSION_V2));
803 werr = regdb_upgrade_v1_to_v2(regdb);
804 if (!W_ERROR_IS_OK(werr)) {
805 dbwrap_transaction_cancel(regdb);
806 return werr;
809 vers_id = REGDB_VERSION_V2;
812 if (vers_id == REGDB_VERSION_V2) {
813 DEBUG(10, ("regdb_init: upgrading registry from version %d "
814 "to %d\n", REGDB_VERSION_V2, REGDB_VERSION_V3));
816 werr = regdb_upgrade_v2_to_v3(regdb);
817 if (!W_ERROR_IS_OK(werr)) {
818 dbwrap_transaction_cancel(regdb);
819 return werr;
822 vers_id = REGDB_VERSION_V3;
825 /* future upgrade code should go here */
827 if (dbwrap_transaction_commit(regdb) != 0) {
828 return WERR_REG_IO_FAILURE;
831 return WERR_OK;
834 /***********************************************************************
835 Open the registry. Must already have been initialized by regdb_init()
836 ***********************************************************************/
838 WERROR regdb_open( void )
840 WERROR result = WERR_OK;
842 if ( regdb ) {
843 DEBUG(10, ("regdb_open: incrementing refcount (%d->%d)\n",
844 regdb_refcount, regdb_refcount+1));
845 regdb_refcount++;
846 return WERR_OK;
849 become_root();
851 regdb = db_open(NULL, state_path("registry.tdb"), 0,
852 REG_TDB_FLAGS, O_RDWR, 0600,
853 DBWRAP_LOCK_ORDER_1);
854 if ( !regdb ) {
855 result = ntstatus_to_werror( map_nt_error_from_unix( errno ) );
856 DEBUG(0,("regdb_open: Failed to open %s! (%s)\n",
857 state_path("registry.tdb"), strerror(errno) ));
860 unbecome_root();
862 regdb_refcount = 1;
863 DEBUG(10, ("regdb_open: registry db opened. refcount reset (%d)\n",
864 regdb_refcount));
866 return result;
869 /***********************************************************************
870 ***********************************************************************/
872 int regdb_close( void )
874 if (regdb_refcount == 0) {
875 return 0;
878 regdb_refcount--;
880 DEBUG(10, ("regdb_close: decrementing refcount (%d->%d)\n",
881 regdb_refcount+1, regdb_refcount));
883 if ( regdb_refcount > 0 )
884 return 0;
886 SMB_ASSERT( regdb_refcount >= 0 );
888 TALLOC_FREE(regdb);
889 return 0;
892 WERROR regdb_transaction_start(void)
894 return (dbwrap_transaction_start(regdb) == 0) ?
895 WERR_OK : WERR_REG_IO_FAILURE;
898 WERROR regdb_transaction_commit(void)
900 return (dbwrap_transaction_commit(regdb) == 0) ?
901 WERR_OK : WERR_REG_IO_FAILURE;
904 WERROR regdb_transaction_cancel(void)
906 return (dbwrap_transaction_cancel(regdb) == 0) ?
907 WERR_OK : WERR_REG_IO_FAILURE;
910 /***********************************************************************
911 return the tdb sequence number of the registry tdb.
912 this is an indicator for the content of the registry
913 having changed. it will change upon regdb_init, too, though.
914 ***********************************************************************/
915 int regdb_get_seqnum(void)
917 return dbwrap_get_seqnum(regdb);
921 static WERROR regdb_delete_key_with_prefix(struct db_context *db,
922 const char *keyname,
923 const char *prefix)
925 char *path;
926 WERROR werr = WERR_NOMEM;
927 TALLOC_CTX *mem_ctx = talloc_stackframe();
929 if (keyname == NULL) {
930 werr = WERR_INVALID_PARAM;
931 goto done;
934 if (prefix == NULL) {
935 path = discard_const_p(char, keyname);
936 } else {
937 path = talloc_asprintf(mem_ctx, "%s\\%s", prefix, keyname);
938 if (path == NULL) {
939 goto done;
943 path = normalize_reg_path(mem_ctx, path);
944 if (path == NULL) {
945 goto done;
948 werr = ntstatus_to_werror(dbwrap_delete_bystring(db, path));
950 /* treat "not found" as ok */
951 if (W_ERROR_EQUAL(werr, WERR_NOT_FOUND)) {
952 werr = WERR_OK;
955 done:
956 talloc_free(mem_ctx);
957 return werr;
961 static WERROR regdb_delete_values(struct db_context *db, const char *keyname)
963 return regdb_delete_key_with_prefix(db, keyname, REG_VALUE_PREFIX);
966 static WERROR regdb_delete_secdesc(struct db_context *db, const char *keyname)
968 return regdb_delete_key_with_prefix(db, keyname, REG_SECDESC_PREFIX);
971 static WERROR regdb_delete_subkeylist(struct db_context *db, const char *keyname)
973 return regdb_delete_key_with_prefix(db, keyname, NULL);
977 static WERROR regdb_delete_key_lists(struct db_context *db, const char *keyname)
979 WERROR werr;
981 werr = regdb_delete_values(db, keyname);
982 if (!W_ERROR_IS_OK(werr)) {
983 DEBUG(1, (__location__ " Deleting %s\\%s failed: %s\n",
984 REG_VALUE_PREFIX, keyname, win_errstr(werr)));
985 goto done;
988 werr = regdb_delete_secdesc(db, keyname);
989 if (!W_ERROR_IS_OK(werr)) {
990 DEBUG(1, (__location__ " Deleting %s\\%s failed: %s\n",
991 REG_SECDESC_PREFIX, keyname, win_errstr(werr)));
992 goto done;
995 werr = regdb_delete_subkeylist(db, keyname);
996 if (!W_ERROR_IS_OK(werr)) {
997 DEBUG(1, (__location__ " Deleting %s failed: %s\n",
998 keyname, win_errstr(werr)));
999 goto done;
1002 done:
1003 return werr;
1006 /***********************************************************************
1007 Add subkey strings to the registry tdb under a defined key
1008 fmt is the same format as tdb_pack except this function only supports
1009 fstrings
1010 ***********************************************************************/
1012 static WERROR regdb_store_keys_internal2(struct db_context *db,
1013 const char *key,
1014 struct regsubkey_ctr *ctr)
1016 TDB_DATA dbuf;
1017 uint8 *buffer = NULL;
1018 int i = 0;
1019 uint32 len, buflen;
1020 uint32 num_subkeys = regsubkey_ctr_numkeys(ctr);
1021 char *keyname = NULL;
1022 TALLOC_CTX *ctx = talloc_stackframe();
1023 WERROR werr;
1025 if (!key) {
1026 werr = WERR_INVALID_PARAM;
1027 goto done;
1030 keyname = talloc_strdup(ctx, key);
1031 if (!keyname) {
1032 werr = WERR_NOMEM;
1033 goto done;
1036 keyname = normalize_reg_path(ctx, keyname);
1037 if (!keyname) {
1038 werr = WERR_NOMEM;
1039 goto done;
1042 /* allocate some initial memory */
1044 buffer = (uint8 *)SMB_MALLOC(1024);
1045 if (buffer == NULL) {
1046 werr = WERR_NOMEM;
1047 goto done;
1049 buflen = 1024;
1050 len = 0;
1052 /* store the number of subkeys */
1054 len += tdb_pack(buffer+len, buflen-len, "d", num_subkeys);
1056 /* pack all the strings */
1058 for (i=0; i<num_subkeys; i++) {
1059 size_t thistime;
1061 thistime = tdb_pack(buffer+len, buflen-len, "f",
1062 regsubkey_ctr_specific_key(ctr, i));
1063 if (len+thistime > buflen) {
1064 size_t thistime2;
1066 * tdb_pack hasn't done anything because of the short
1067 * buffer, allocate extra space.
1069 buffer = SMB_REALLOC_ARRAY(buffer, uint8_t,
1070 (len+thistime)*2);
1071 if(buffer == NULL) {
1072 DEBUG(0, ("regdb_store_keys: Failed to realloc "
1073 "memory of size [%u]\n",
1074 (unsigned int)(len+thistime)*2));
1075 werr = WERR_NOMEM;
1076 goto done;
1078 buflen = (len+thistime)*2;
1079 thistime2 = tdb_pack(
1080 buffer+len, buflen-len, "f",
1081 regsubkey_ctr_specific_key(ctr, i));
1082 if (thistime2 != thistime) {
1083 DEBUG(0, ("tdb_pack failed\n"));
1084 werr = WERR_CAN_NOT_COMPLETE;
1085 goto done;
1088 len += thistime;
1091 /* finally write out the data */
1093 dbuf.dptr = buffer;
1094 dbuf.dsize = len;
1095 werr = ntstatus_to_werror(dbwrap_store_bystring(db, keyname, dbuf,
1096 TDB_REPLACE));
1098 done:
1099 TALLOC_FREE(ctx);
1100 SAFE_FREE(buffer);
1101 return werr;
1105 * Utility function to store a new empty list of
1106 * subkeys of given key specified as parent and subkey name
1107 * (thereby creating the key).
1108 * If the parent keyname is NULL, then the "subkey" is
1109 * interpreted as a base key.
1110 * If the subkey list does already exist, it is not modified.
1112 * Must be called from within a transaction.
1114 static WERROR regdb_store_subkey_list(struct db_context *db, const char *parent,
1115 const char *key)
1117 WERROR werr;
1118 char *path = NULL;
1119 struct regsubkey_ctr *subkeys = NULL;
1120 TALLOC_CTX *frame = talloc_stackframe();
1122 if (parent == NULL) {
1123 path = talloc_strdup(frame, key);
1124 } else {
1125 path = talloc_asprintf(frame, "%s\\%s", parent, key);
1127 if (!path) {
1128 werr = WERR_NOMEM;
1129 goto done;
1132 werr = regsubkey_ctr_init(frame, &subkeys);
1133 W_ERROR_NOT_OK_GOTO_DONE(werr);
1135 werr = regdb_fetch_keys_internal(db, path, subkeys);
1136 if (W_ERROR_IS_OK(werr)) {
1137 /* subkey list exists already - don't modify */
1138 goto done;
1141 werr = regsubkey_ctr_reinit(subkeys);
1142 W_ERROR_NOT_OK_GOTO_DONE(werr);
1144 /* create a record with 0 subkeys */
1145 werr = regdb_store_keys_internal2(db, path, subkeys);
1146 if (!W_ERROR_IS_OK(werr)) {
1147 DEBUG(0, ("regdb_store_keys: Failed to store new record for "
1148 "key [%s]: %s\n", path, win_errstr(werr)));
1149 goto done;
1152 done:
1153 talloc_free(frame);
1154 return werr;
1157 /***********************************************************************
1158 Store the new subkey record and create any child key records that
1159 do not currently exist
1160 ***********************************************************************/
1162 struct regdb_store_keys_context {
1163 const char *key;
1164 struct regsubkey_ctr *ctr;
1167 static NTSTATUS regdb_store_keys_action(struct db_context *db,
1168 void *private_data)
1170 struct regdb_store_keys_context *store_ctx;
1171 WERROR werr;
1172 int num_subkeys, i;
1173 char *path = NULL;
1174 struct regsubkey_ctr *old_subkeys = NULL;
1175 char *oldkeyname = NULL;
1176 TALLOC_CTX *mem_ctx = talloc_stackframe();
1178 store_ctx = (struct regdb_store_keys_context *)private_data;
1181 * Re-fetch the old keys inside the transaction
1184 werr = regsubkey_ctr_init(mem_ctx, &old_subkeys);
1185 W_ERROR_NOT_OK_GOTO_DONE(werr);
1187 werr = regdb_fetch_keys_internal(db, store_ctx->key, old_subkeys);
1188 if (!W_ERROR_IS_OK(werr) &&
1189 !W_ERROR_EQUAL(werr, WERR_NOT_FOUND))
1191 goto done;
1195 * Make the store operation as safe as possible without transactions:
1197 * (1) For each subkey removed from ctr compared with old_subkeys:
1199 * (a) First delete the value db entry.
1201 * (b) Next delete the secdesc db record.
1203 * (c) Then delete the subkey list entry.
1205 * (2) Now write the list of subkeys of the parent key,
1206 * deleting removed entries and adding new ones.
1208 * (3) Finally create the subkey list entries for the added keys.
1210 * This way if we crash half-way in between deleting the subkeys
1211 * and storing the parent's list of subkeys, no old data can pop up
1212 * out of the blue when re-adding keys later on.
1215 /* (1) delete removed keys' lists (values/secdesc/subkeys) */
1217 num_subkeys = regsubkey_ctr_numkeys(old_subkeys);
1218 for (i=0; i<num_subkeys; i++) {
1219 oldkeyname = regsubkey_ctr_specific_key(old_subkeys, i);
1221 if (regsubkey_ctr_key_exists(store_ctx->ctr, oldkeyname)) {
1223 * It's still around, don't delete
1225 continue;
1228 path = talloc_asprintf(mem_ctx, "%s\\%s", store_ctx->key,
1229 oldkeyname);
1230 if (!path) {
1231 werr = WERR_NOMEM;
1232 goto done;
1235 werr = regdb_delete_key_lists(db, path);
1236 W_ERROR_NOT_OK_GOTO_DONE(werr);
1238 TALLOC_FREE(path);
1241 TALLOC_FREE(old_subkeys);
1243 /* (2) store the subkey list for the parent */
1245 werr = regdb_store_keys_internal2(db, store_ctx->key, store_ctx->ctr);
1246 if (!W_ERROR_IS_OK(werr)) {
1247 DEBUG(0,("regdb_store_keys: Failed to store new subkey list "
1248 "for parent [%s]: %s\n", store_ctx->key,
1249 win_errstr(werr)));
1250 goto done;
1253 /* (3) now create records for any subkeys that don't already exist */
1255 num_subkeys = regsubkey_ctr_numkeys(store_ctx->ctr);
1257 for (i=0; i<num_subkeys; i++) {
1258 const char *subkey;
1260 subkey = regsubkey_ctr_specific_key(store_ctx->ctr, i);
1262 werr = regdb_store_subkey_list(db, store_ctx->key, subkey);
1263 W_ERROR_NOT_OK_GOTO_DONE(werr);
1267 * Update the seqnum in the container to possibly
1268 * prevent next read from going to disk
1270 werr = regsubkey_ctr_set_seqnum(store_ctx->ctr, dbwrap_get_seqnum(db));
1272 done:
1273 talloc_free(mem_ctx);
1274 return werror_to_ntstatus(werr);
1277 static bool regdb_store_keys_internal(struct db_context *db, const char *key,
1278 struct regsubkey_ctr *ctr)
1280 int num_subkeys, old_num_subkeys, i;
1281 struct regsubkey_ctr *old_subkeys = NULL;
1282 TALLOC_CTX *ctx = talloc_stackframe();
1283 WERROR werr;
1284 bool ret = false;
1285 struct regdb_store_keys_context store_ctx;
1287 if (!regdb_key_exists(db, key)) {
1288 goto done;
1292 * fetch a list of the old subkeys so we can determine if anything has
1293 * changed
1296 werr = regsubkey_ctr_init(ctx, &old_subkeys);
1297 if (!W_ERROR_IS_OK(werr)) {
1298 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
1299 goto done;
1302 werr = regdb_fetch_keys_internal(db, key, old_subkeys);
1303 if (!W_ERROR_IS_OK(werr) &&
1304 !W_ERROR_EQUAL(werr, WERR_NOT_FOUND))
1306 goto done;
1309 num_subkeys = regsubkey_ctr_numkeys(ctr);
1310 old_num_subkeys = regsubkey_ctr_numkeys(old_subkeys);
1311 if ((num_subkeys && old_num_subkeys) &&
1312 (num_subkeys == old_num_subkeys)) {
1314 for (i = 0; i < num_subkeys; i++) {
1315 if (strcmp(regsubkey_ctr_specific_key(ctr, i),
1316 regsubkey_ctr_specific_key(old_subkeys, i))
1317 != 0)
1319 break;
1322 if (i == num_subkeys) {
1324 * Nothing changed, no point to even start a tdb
1325 * transaction
1328 ret = true;
1329 goto done;
1333 TALLOC_FREE(old_subkeys);
1335 store_ctx.key = key;
1336 store_ctx.ctr = ctr;
1338 werr = regdb_trans_do(db,
1339 regdb_store_keys_action,
1340 &store_ctx);
1342 ret = W_ERROR_IS_OK(werr);
1344 done:
1345 TALLOC_FREE(ctx);
1347 return ret;
1350 static bool regdb_store_keys(const char *key, struct regsubkey_ctr *ctr)
1352 return regdb_store_keys_internal(regdb, key, ctr);
1356 * create a subkey of a given key
1359 struct regdb_create_subkey_context {
1360 const char *key;
1361 const char *subkey;
1364 static NTSTATUS regdb_create_subkey_action(struct db_context *db,
1365 void *private_data)
1367 WERROR werr;
1368 struct regdb_create_subkey_context *create_ctx;
1369 struct regsubkey_ctr *subkeys;
1370 TALLOC_CTX *mem_ctx = talloc_stackframe();
1372 create_ctx = (struct regdb_create_subkey_context *)private_data;
1374 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
1375 W_ERROR_NOT_OK_GOTO_DONE(werr);
1377 werr = regdb_fetch_keys_internal(db, create_ctx->key, subkeys);
1378 W_ERROR_NOT_OK_GOTO_DONE(werr);
1380 werr = regsubkey_ctr_addkey(subkeys, create_ctx->subkey);
1381 W_ERROR_NOT_OK_GOTO_DONE(werr);
1383 werr = regdb_store_keys_internal2(db, create_ctx->key, subkeys);
1384 if (!W_ERROR_IS_OK(werr)) {
1385 DEBUG(0, (__location__ " failed to store new subkey list for "
1386 "parent key %s: %s\n", create_ctx->key,
1387 win_errstr(werr)));
1390 werr = regdb_store_subkey_list(db, create_ctx->key, create_ctx->subkey);
1392 done:
1393 talloc_free(mem_ctx);
1394 return werror_to_ntstatus(werr);
1397 static WERROR regdb_create_subkey_internal(struct db_context *db,
1398 const char *key,
1399 const char *subkey)
1401 WERROR werr;
1402 struct regsubkey_ctr *subkeys;
1403 TALLOC_CTX *mem_ctx = talloc_stackframe();
1404 struct regdb_create_subkey_context create_ctx;
1406 if (!regdb_key_exists(db, key)) {
1407 werr = WERR_NOT_FOUND;
1408 goto done;
1411 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
1412 W_ERROR_NOT_OK_GOTO_DONE(werr);
1414 werr = regdb_fetch_keys_internal(db, key, subkeys);
1415 W_ERROR_NOT_OK_GOTO_DONE(werr);
1417 if (regsubkey_ctr_key_exists(subkeys, subkey)) {
1418 char *newkey;
1420 newkey = talloc_asprintf(mem_ctx, "%s\\%s", key, subkey);
1421 if (newkey == NULL) {
1422 werr = WERR_NOMEM;
1423 goto done;
1426 if (regdb_key_exists(db, newkey)) {
1427 werr = WERR_OK;
1428 goto done;
1432 talloc_free(subkeys);
1434 create_ctx.key = key;
1435 create_ctx.subkey = subkey;
1437 werr = regdb_trans_do(db,
1438 regdb_create_subkey_action,
1439 &create_ctx);
1441 done:
1442 talloc_free(mem_ctx);
1443 return werr;
1446 static WERROR regdb_create_subkey(const char *key, const char *subkey)
1448 return regdb_create_subkey_internal(regdb, key, subkey);
1452 * create a base key
1455 struct regdb_create_basekey_context {
1456 const char *key;
1459 static NTSTATUS regdb_create_basekey_action(struct db_context *db,
1460 void *private_data)
1462 WERROR werr;
1463 struct regdb_create_basekey_context *create_ctx;
1465 create_ctx = (struct regdb_create_basekey_context *)private_data;
1467 werr = regdb_store_subkey_list(db, NULL, create_ctx->key);
1469 return werror_to_ntstatus(werr);
1472 static WERROR regdb_create_basekey(struct db_context *db, const char *key)
1474 WERROR werr;
1475 struct regdb_create_subkey_context create_ctx;
1477 create_ctx.key = key;
1479 werr = regdb_trans_do(db,
1480 regdb_create_basekey_action,
1481 &create_ctx);
1483 return werr;
1487 * create a subkey of a given key
1490 struct regdb_delete_subkey_context {
1491 const char *key;
1492 const char *subkey;
1493 const char *path;
1494 bool lazy;
1497 static NTSTATUS regdb_delete_subkey_action(struct db_context *db,
1498 void *private_data)
1500 WERROR werr;
1501 struct regdb_delete_subkey_context *delete_ctx;
1502 struct regsubkey_ctr *subkeys;
1503 TALLOC_CTX *mem_ctx = talloc_stackframe();
1505 delete_ctx = (struct regdb_delete_subkey_context *)private_data;
1507 werr = regdb_delete_key_lists(db, delete_ctx->path);
1508 W_ERROR_NOT_OK_GOTO_DONE(werr);
1510 if (delete_ctx->lazy) {
1511 goto done;
1514 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
1515 W_ERROR_NOT_OK_GOTO_DONE(werr);
1517 werr = regdb_fetch_keys_internal(db, delete_ctx->key, subkeys);
1518 W_ERROR_NOT_OK_GOTO_DONE(werr);
1520 werr = regsubkey_ctr_delkey(subkeys, delete_ctx->subkey);
1521 W_ERROR_NOT_OK_GOTO_DONE(werr);
1523 werr = regdb_store_keys_internal2(db, delete_ctx->key, subkeys);
1524 if (!W_ERROR_IS_OK(werr)) {
1525 DEBUG(0, (__location__ " failed to store new subkey_list for "
1526 "parent key %s: %s\n", delete_ctx->key,
1527 win_errstr(werr)));
1530 done:
1531 talloc_free(mem_ctx);
1532 return werror_to_ntstatus(werr);
1535 static WERROR regdb_delete_subkey(const char *key, const char *subkey, bool lazy)
1537 WERROR werr;
1538 char *path;
1539 struct regdb_delete_subkey_context delete_ctx;
1540 TALLOC_CTX *mem_ctx = talloc_stackframe();
1542 if (!regdb_key_exists(regdb, key)) {
1543 werr = WERR_NOT_FOUND;
1544 goto done;
1547 path = talloc_asprintf(mem_ctx, "%s\\%s", key, subkey);
1548 if (path == NULL) {
1549 werr = WERR_NOMEM;
1550 goto done;
1553 if (!regdb_key_exists(regdb, path)) {
1554 werr = WERR_OK;
1555 goto done;
1558 delete_ctx.key = key;
1559 delete_ctx.subkey = subkey;
1560 delete_ctx.path = path;
1561 delete_ctx.lazy = lazy;
1563 werr = regdb_trans_do(regdb,
1564 regdb_delete_subkey_action,
1565 &delete_ctx);
1567 done:
1568 talloc_free(mem_ctx);
1569 return werr;
1572 static TDB_DATA regdb_fetch_key_internal(struct db_context *db,
1573 TALLOC_CTX *mem_ctx, const char *key)
1575 char *path = NULL;
1576 TDB_DATA data;
1577 NTSTATUS status;
1579 path = normalize_reg_path(mem_ctx, key);
1580 if (!path) {
1581 return make_tdb_data(NULL, 0);
1584 status = dbwrap_fetch_bystring(db, mem_ctx, path, &data);
1585 if (!NT_STATUS_IS_OK(status)) {
1586 data = tdb_null;
1589 TALLOC_FREE(path);
1590 return data;
1595 * Check for the existence of a key.
1597 * Existence of a key is authoritatively defined by
1598 * the existence of the record that contains the list
1599 * of its subkeys.
1601 * Return false, if the record does not match the correct
1602 * structure of an initial 4-byte counter and then a
1603 * list of the corresponding number of zero-terminated
1604 * strings.
1606 static bool regdb_key_exists(struct db_context *db, const char *key)
1608 TALLOC_CTX *mem_ctx = talloc_stackframe();
1609 TDB_DATA value;
1610 bool ret = false;
1611 char *path;
1612 uint32_t buflen;
1613 const char *buf;
1614 uint32_t num_items, i;
1615 int32_t len;
1617 if (key == NULL) {
1618 goto done;
1621 path = normalize_reg_path(mem_ctx, key);
1622 if (path == NULL) {
1623 DEBUG(0, ("out of memory! (talloc failed)\n"));
1624 goto done;
1627 if (*path == '\0') {
1628 goto done;
1631 value = regdb_fetch_key_internal(db, mem_ctx, path);
1632 if (value.dptr == NULL) {
1633 goto done;
1636 if (value.dsize == 0) {
1637 DEBUG(10, ("regdb_key_exists: subkeylist-record for key "
1638 "[%s] is empty: Could be a deleted record in a "
1639 "clustered (ctdb) environment?\n",
1640 path));
1641 goto done;
1644 len = tdb_unpack(value.dptr, value.dsize, "d", &num_items);
1645 if (len == (int32_t)-1) {
1646 DEBUG(1, ("regdb_key_exists: ERROR: subkeylist-record for key "
1647 "[%s] is invalid: Could not parse initial 4-byte "
1648 "counter. record data length is %u.\n",
1649 path, (unsigned int)value.dsize));
1650 goto done;
1654 * Note: the tdb_unpack check above implies that len <= value.dsize
1656 buflen = value.dsize - len;
1657 buf = (const char *)value.dptr + len;
1659 len = 0;
1661 for (i = 0; i < num_items; i++) {
1662 if (buflen == 0) {
1663 break;
1665 len = strnlen(buf, buflen) + 1;
1666 if (buflen < len) {
1667 DEBUG(1, ("regdb_key_exists: ERROR: subkeylist-record "
1668 "for key [%s] is corrupt: %u items expected, "
1669 "item number %u is not zero terminated.\n",
1670 path, num_items, i+1));
1671 goto done;
1674 buf += len;
1675 buflen -= len;
1678 if (buflen > 0) {
1679 DEBUG(1, ("regdb_key_exists: ERROR: subkeylist-record for key "
1680 "[%s] is corrupt: %u items expected and found, but "
1681 "the record contains additional %u bytes\n",
1682 path, num_items, buflen));
1683 goto done;
1686 if (i < num_items) {
1687 DEBUG(1, ("regdb_key_exists: ERROR: subkeylist-record for key "
1688 "[%s] is corrupt: %u items expected, but only %u "
1689 "items found.\n",
1690 path, num_items, i+1));
1691 goto done;
1694 ret = true;
1696 done:
1697 TALLOC_FREE(mem_ctx);
1698 return ret;
1702 /***********************************************************************
1703 Retrieve an array of strings containing subkeys. Memory should be
1704 released by the caller.
1705 ***********************************************************************/
1707 static WERROR regdb_fetch_keys_internal(struct db_context *db, const char *key,
1708 struct regsubkey_ctr *ctr)
1710 WERROR werr;
1711 uint32_t num_items;
1712 uint8 *buf;
1713 uint32 buflen, len;
1714 int i;
1715 fstring subkeyname;
1716 TALLOC_CTX *frame = talloc_stackframe();
1717 TDB_DATA value;
1718 int seqnum[2], count;
1720 DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key ? key : "NULL"));
1722 if (!regdb_key_exists(db, key)) {
1723 DEBUG(10, ("key [%s] not found\n", key));
1724 werr = WERR_NOT_FOUND;
1725 goto done;
1728 werr = regsubkey_ctr_reinit(ctr);
1729 W_ERROR_NOT_OK_GOTO_DONE(werr);
1731 count = 0;
1732 ZERO_STRUCT(value);
1733 seqnum[0] = dbwrap_get_seqnum(db);
1735 do {
1736 count++;
1737 TALLOC_FREE(value.dptr);
1738 value = regdb_fetch_key_internal(db, frame, key);
1739 seqnum[count % 2] = dbwrap_get_seqnum(db);
1741 } while (seqnum[0] != seqnum[1]);
1743 if (count > 1) {
1744 DEBUG(5, ("regdb_fetch_keys_internal: it took %d attempts to "
1745 "fetch key '%s' with constant seqnum\n",
1746 count, key));
1749 werr = regsubkey_ctr_set_seqnum(ctr, seqnum[0]);
1750 if (!W_ERROR_IS_OK(werr)) {
1751 goto done;
1754 if (value.dsize == 0 || value.dptr == NULL) {
1755 DEBUG(10, ("regdb_fetch_keys: no subkeys found for key [%s]\n",
1756 key));
1757 goto done;
1760 buf = value.dptr;
1761 buflen = value.dsize;
1762 len = tdb_unpack( buf, buflen, "d", &num_items);
1763 if (len == (uint32_t)-1) {
1764 werr = WERR_NOT_FOUND;
1765 goto done;
1768 for (i=0; i<num_items; i++) {
1769 len += tdb_unpack(buf+len, buflen-len, "f", subkeyname);
1770 werr = regsubkey_ctr_addkey(ctr, subkeyname);
1771 if (!W_ERROR_IS_OK(werr)) {
1772 DEBUG(5, ("regdb_fetch_keys: regsubkey_ctr_addkey "
1773 "failed: %s\n", win_errstr(werr)));
1774 num_items = 0;
1775 goto done;
1779 DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items));
1781 done:
1782 TALLOC_FREE(frame);
1783 return werr;
1786 static int regdb_fetch_keys(const char *key, struct regsubkey_ctr *ctr)
1788 WERROR werr;
1790 werr = regdb_fetch_keys_internal(regdb, key, ctr);
1791 if (!W_ERROR_IS_OK(werr)) {
1792 return -1;
1795 return regsubkey_ctr_numkeys(ctr);
1798 /****************************************************************************
1799 Unpack a list of registry values frem the TDB
1800 ***************************************************************************/
1802 static int regdb_unpack_values(struct regval_ctr *values, uint8 *buf, int buflen)
1804 int len = 0;
1805 uint32 type;
1806 fstring valuename;
1807 uint32 size;
1808 uint8 *data_p;
1809 uint32 num_values = 0;
1810 int i;
1812 /* loop and unpack the rest of the registry values */
1814 len += tdb_unpack(buf+len, buflen-len, "d", &num_values);
1816 for ( i=0; i<num_values; i++ ) {
1817 /* unpack the next regval */
1819 type = REG_NONE;
1820 size = 0;
1821 data_p = NULL;
1822 valuename[0] = '\0';
1823 len += tdb_unpack(buf+len, buflen-len, "fdB",
1824 valuename,
1825 &type,
1826 &size,
1827 &data_p);
1829 regval_ctr_addvalue(values, valuename, type,
1830 (uint8_t *)data_p, size);
1831 SAFE_FREE(data_p); /* 'B' option to tdb_unpack does a malloc() */
1833 DEBUG(10, ("regdb_unpack_values: value[%d]: name[%s] len[%d]\n",
1834 i, valuename, size));
1837 return len;
1840 /****************************************************************************
1841 Pack all values in all printer keys
1842 ***************************************************************************/
1844 static int regdb_pack_values(struct regval_ctr *values, uint8 *buf, int buflen)
1846 int len = 0;
1847 int i;
1848 struct regval_blob *val;
1849 int num_values;
1851 if ( !values )
1852 return 0;
1854 num_values = regval_ctr_numvals( values );
1856 /* pack the number of values first */
1858 len += tdb_pack( buf+len, buflen-len, "d", num_values );
1860 /* loop over all values */
1862 for ( i=0; i<num_values; i++ ) {
1863 val = regval_ctr_specific_value( values, i );
1864 len += tdb_pack(buf+len, buflen-len, "fdB",
1865 regval_name(val),
1866 regval_type(val),
1867 regval_size(val),
1868 regval_data_p(val) );
1871 return len;
1874 /***********************************************************************
1875 Retrieve an array of strings containing subkeys. Memory should be
1876 released by the caller.
1877 ***********************************************************************/
1879 static int regdb_fetch_values_internal(struct db_context *db, const char* key,
1880 struct regval_ctr *values)
1882 char *keystr = NULL;
1883 TALLOC_CTX *ctx = talloc_stackframe();
1884 int ret = 0;
1885 TDB_DATA value;
1886 WERROR werr;
1887 int seqnum[2], count;
1889 DEBUG(10,("regdb_fetch_values: Looking for values of key [%s]\n", key));
1891 if (!regdb_key_exists(db, key)) {
1892 DEBUG(10, ("regb_fetch_values: key [%s] does not exist\n",
1893 key));
1894 ret = -1;
1895 goto done;
1898 keystr = talloc_asprintf(ctx, "%s\\%s", REG_VALUE_PREFIX, key);
1899 if (!keystr) {
1900 goto done;
1903 ZERO_STRUCT(value);
1904 count = 0;
1905 seqnum[0] = dbwrap_get_seqnum(db);
1907 do {
1908 count++;
1909 TALLOC_FREE(value.dptr);
1910 value = regdb_fetch_key_internal(db, ctx, keystr);
1911 seqnum[count % 2] = dbwrap_get_seqnum(db);
1912 } while (seqnum[0] != seqnum[1]);
1914 if (count > 1) {
1915 DEBUG(5, ("regdb_fetch_values_internal: it took %d attempts "
1916 "to fetch key '%s' with constant seqnum\n",
1917 count, key));
1920 werr = regval_ctr_set_seqnum(values, seqnum[0]);
1921 if (!W_ERROR_IS_OK(werr)) {
1922 goto done;
1925 if (!value.dptr) {
1926 /* all keys have zero values by default */
1927 goto done;
1930 regdb_unpack_values(values, value.dptr, value.dsize);
1931 ret = regval_ctr_numvals(values);
1933 done:
1934 TALLOC_FREE(ctx);
1935 return ret;
1938 static int regdb_fetch_values(const char* key, struct regval_ctr *values)
1940 return regdb_fetch_values_internal(regdb, key, values);
1943 static NTSTATUS regdb_store_values_internal(struct db_context *db,
1944 const char *key,
1945 struct regval_ctr *values)
1947 TDB_DATA old_data, data;
1948 char *keystr = NULL;
1949 TALLOC_CTX *ctx = talloc_stackframe();
1950 int len;
1951 NTSTATUS status;
1952 WERROR werr;
1954 DEBUG(10,("regdb_store_values: Looking for values of key [%s]\n", key));
1956 if (!regdb_key_exists(db, key)) {
1957 status = NT_STATUS_NOT_FOUND;
1958 goto done;
1961 if (regval_ctr_numvals(values) == 0) {
1962 werr = regdb_delete_values(db, key);
1963 if (!W_ERROR_IS_OK(werr)) {
1964 status = werror_to_ntstatus(werr);
1965 goto done;
1969 * update the seqnum in the cache to prevent the next read
1970 * from going to disk
1972 werr = regval_ctr_set_seqnum(values, dbwrap_get_seqnum(db));
1973 status = werror_to_ntstatus(werr);
1974 goto done;
1977 ZERO_STRUCT(data);
1979 len = regdb_pack_values(values, data.dptr, data.dsize);
1980 if (len <= 0) {
1981 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
1982 status = NT_STATUS_UNSUCCESSFUL;
1983 goto done;
1986 data.dptr = talloc_array(ctx, uint8, len);
1987 data.dsize = len;
1989 len = regdb_pack_values(values, data.dptr, data.dsize);
1991 SMB_ASSERT( len == data.dsize );
1993 keystr = talloc_asprintf(ctx, "%s\\%s", REG_VALUE_PREFIX, key );
1994 if (!keystr) {
1995 status = NT_STATUS_NO_MEMORY;
1996 goto done;
1998 keystr = normalize_reg_path(ctx, keystr);
1999 if (!keystr) {
2000 status = NT_STATUS_NO_MEMORY;
2001 goto done;
2004 status = dbwrap_fetch_bystring(db, ctx, keystr, &old_data);
2006 if (NT_STATUS_IS_OK(status)
2007 && (old_data.dptr != NULL)
2008 && (old_data.dsize == data.dsize)
2009 && (memcmp(old_data.dptr, data.dptr, data.dsize) == 0))
2011 status = NT_STATUS_OK;
2012 goto done;
2015 status = dbwrap_trans_store_bystring(db, keystr, data, TDB_REPLACE);
2016 if (!NT_STATUS_IS_OK(status)) {
2017 DEBUG(0, ("regdb_store_values_internal: error storing: %s\n", nt_errstr(status)));
2018 goto done;
2022 * update the seqnum in the cache to prevent the next read
2023 * from going to disk
2025 werr = regval_ctr_set_seqnum(values, dbwrap_get_seqnum(db));
2026 status = werror_to_ntstatus(werr);
2028 done:
2029 TALLOC_FREE(ctx);
2030 return status;
2033 struct regdb_store_values_ctx {
2034 const char *key;
2035 struct regval_ctr *values;
2038 static NTSTATUS regdb_store_values_action(struct db_context *db,
2039 void *private_data)
2041 NTSTATUS status;
2042 struct regdb_store_values_ctx *ctx =
2043 (struct regdb_store_values_ctx *)private_data;
2045 status = regdb_store_values_internal(db, ctx->key, ctx->values);
2047 return status;
2050 static bool regdb_store_values(const char *key, struct regval_ctr *values)
2052 WERROR werr;
2053 struct regdb_store_values_ctx ctx;
2055 ctx.key = key;
2056 ctx.values = values;
2058 werr = regdb_trans_do(regdb, regdb_store_values_action, &ctx);
2060 return W_ERROR_IS_OK(werr);
2063 static WERROR regdb_get_secdesc(TALLOC_CTX *mem_ctx, const char *key,
2064 struct security_descriptor **psecdesc)
2066 char *tdbkey;
2067 TDB_DATA data;
2068 NTSTATUS status;
2069 TALLOC_CTX *tmp_ctx = talloc_stackframe();
2070 WERROR err = WERR_OK;
2072 DEBUG(10, ("regdb_get_secdesc: Getting secdesc of key [%s]\n", key));
2074 if (!regdb_key_exists(regdb, key)) {
2075 err = WERR_BADFILE;
2076 goto done;
2079 tdbkey = talloc_asprintf(tmp_ctx, "%s\\%s", REG_SECDESC_PREFIX, key);
2080 if (tdbkey == NULL) {
2081 err = WERR_NOMEM;
2082 goto done;
2085 tdbkey = normalize_reg_path(tmp_ctx, tdbkey);
2086 if (tdbkey == NULL) {
2087 err = WERR_NOMEM;
2088 goto done;
2091 status = dbwrap_fetch_bystring(regdb, tmp_ctx, tdbkey, &data);
2092 if (!NT_STATUS_IS_OK(status)) {
2093 err = WERR_BADFILE;
2094 goto done;
2097 status = unmarshall_sec_desc(mem_ctx, (uint8 *)data.dptr, data.dsize,
2098 psecdesc);
2100 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MEMORY)) {
2101 err = WERR_NOMEM;
2102 } else if (!NT_STATUS_IS_OK(status)) {
2103 err = WERR_REG_CORRUPT;
2106 done:
2107 TALLOC_FREE(tmp_ctx);
2108 return err;
2111 struct regdb_set_secdesc_ctx {
2112 const char *key;
2113 struct security_descriptor *secdesc;
2116 static NTSTATUS regdb_set_secdesc_action(struct db_context *db,
2117 void *private_data)
2119 char *tdbkey;
2120 NTSTATUS status;
2121 TDB_DATA tdbdata;
2122 struct regdb_set_secdesc_ctx *ctx =
2123 (struct regdb_set_secdesc_ctx *)private_data;
2124 TALLOC_CTX *frame = talloc_stackframe();
2126 tdbkey = talloc_asprintf(frame, "%s\\%s", REG_SECDESC_PREFIX, ctx->key);
2127 if (tdbkey == NULL) {
2128 status = NT_STATUS_NO_MEMORY;
2129 goto done;
2132 tdbkey = normalize_reg_path(frame, tdbkey);
2133 if (tdbkey == NULL) {
2134 status = NT_STATUS_NO_MEMORY;
2135 goto done;
2138 if (ctx->secdesc == NULL) {
2139 /* assuming a delete */
2140 status = dbwrap_delete_bystring(db, tdbkey);
2141 goto done;
2144 status = marshall_sec_desc(frame, ctx->secdesc, &tdbdata.dptr,
2145 &tdbdata.dsize);
2146 if (!NT_STATUS_IS_OK(status)) {
2147 goto done;
2150 status = dbwrap_store_bystring(db, tdbkey, tdbdata, 0);
2152 done:
2153 TALLOC_FREE(frame);
2154 return status;
2157 static WERROR regdb_set_secdesc(const char *key,
2158 struct security_descriptor *secdesc)
2160 WERROR err;
2161 struct regdb_set_secdesc_ctx ctx;
2163 if (!regdb_key_exists(regdb, key)) {
2164 err = WERR_BADFILE;
2165 goto done;
2168 ctx.key = key;
2169 ctx.secdesc = secdesc;
2171 err = regdb_trans_do(regdb, regdb_set_secdesc_action, &ctx);
2173 done:
2174 return err;
2177 static bool regdb_subkeys_need_update(struct regsubkey_ctr *subkeys)
2179 return (regdb_get_seqnum() != regsubkey_ctr_get_seqnum(subkeys));
2182 static bool regdb_values_need_update(struct regval_ctr *values)
2184 return (regdb_get_seqnum() != regval_ctr_get_seqnum(values));
2188 * Table of function pointers for default access
2191 struct registry_ops regdb_ops = {
2192 .fetch_subkeys = regdb_fetch_keys,
2193 .fetch_values = regdb_fetch_values,
2194 .store_subkeys = regdb_store_keys,
2195 .store_values = regdb_store_values,
2196 .create_subkey = regdb_create_subkey,
2197 .delete_subkey = regdb_delete_subkey,
2198 .get_secdesc = regdb_get_secdesc,
2199 .set_secdesc = regdb_set_secdesc,
2200 .subkeys_need_update = regdb_subkeys_need_update,
2201 .values_need_update = regdb_values_need_update