s3:registry: do not use regdb functions during db upgrade
[Samba/gebeck_regimport.git] / source3 / registry / reg_backend_db.c
blob06251e01f91459bef3a166ef969f6f881e929695
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(db, REGDB_VERSION_KEYNAME, &version_id);
77 if (!NT_STATUS_IS_OK(status)) {
78 DEBUG(0, ("ERROR: could not fetch registry db version: %s. "
79 "Denying access.\n", nt_errstr(status)));
80 return NT_STATUS_ACCESS_DENIED;
83 if (version_id != REGDB_CODE_VERSION) {
84 DEBUG(0, ("ERROR: changed registry version %d found while "
85 "trying to write to the registry. Version %d "
86 "expected. Denying access.\n",
87 version_id, REGDB_CODE_VERSION));
88 return NT_STATUS_ACCESS_DENIED;
91 status = ctx->action(db, ctx->private_data);
92 return status;
95 static WERROR regdb_trans_do(struct db_context *db,
96 NTSTATUS (*action)(struct db_context *, void *),
97 void *private_data)
99 NTSTATUS status;
100 struct regdb_trans_ctx ctx;
103 ctx.action = action;
104 ctx.private_data = private_data;
106 status = dbwrap_trans_do(db, regdb_trans_do_action, &ctx);
108 return ntstatus_to_werror(status);
111 /* List the deepest path into the registry. All part components will be created.*/
113 /* If you want to have a part of the path controlled by the tdb and part by
114 a virtual registry db (e.g. printing), then you have to list the deepest path.
115 For example,"HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion/Print"
116 allows the reg_db backend to handle everything up to
117 "HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion" and then we'll hook
118 the reg_printing backend onto the last component of the path (see
119 KEY_PRINTING_2K in include/rpc_reg.h) --jerry */
121 static const char *builtin_registry_paths[] = {
122 KEY_PRINTING_2K,
123 KEY_PRINTING_PORTS,
124 KEY_PRINTING,
125 KEY_PRINTING "\\Forms",
126 KEY_PRINTING "\\Printers",
127 KEY_PRINTING "\\Environments\\Windows NT x86\\Print Processors\\winprint",
128 KEY_SHARES,
129 KEY_EVENTLOG,
130 KEY_SMBCONF,
131 KEY_PERFLIB,
132 KEY_PERFLIB_009,
133 KEY_GROUP_POLICY,
134 KEY_SAMBA_GROUP_POLICY,
135 KEY_GP_MACHINE_POLICY,
136 KEY_GP_MACHINE_WIN_POLICY,
137 KEY_HKCU,
138 KEY_GP_USER_POLICY,
139 KEY_GP_USER_WIN_POLICY,
140 "HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon\\GPExtensions",
141 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Print\\Monitors",
142 KEY_PROD_OPTIONS,
143 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\DefaultUserConfiguration",
144 KEY_TCPIP_PARAMS,
145 KEY_NETLOGON_PARAMS,
146 KEY_HKU,
147 KEY_HKCR,
148 KEY_HKPD,
149 KEY_HKPT,
150 NULL };
152 struct builtin_regkey_value {
153 const char *path;
154 const char *valuename;
155 uint32 type;
156 union {
157 const char *string;
158 uint32 dw_value;
159 } data;
162 static struct builtin_regkey_value builtin_registry_values[] = {
163 { KEY_PRINTING_PORTS,
164 SAMBA_PRINTER_PORT_NAME, REG_SZ, { "" } },
165 { KEY_PRINTING_2K,
166 "DefaultSpoolDirectory", REG_SZ, { "C:\\Windows\\System32\\Spool\\Printers" } },
167 { KEY_EVENTLOG,
168 "DisplayName", REG_SZ, { "Event Log" } },
169 { KEY_EVENTLOG,
170 "ErrorControl", REG_DWORD, { (char*)0x00000001 } },
171 { NULL, NULL, 0, { NULL } }
174 static WERROR create_key_recursive(struct db_context *db,
175 char *path,
176 const char *subkey)
178 WERROR werr;
179 char *p;
181 if (subkey == NULL) {
182 return WERR_INVALID_PARAM;
185 if (path == NULL) {
186 return regdb_create_basekey(db, subkey);
189 p = strrchr_m(path, '\\');
191 if (p == NULL) {
192 werr = create_key_recursive(db, NULL, path);
193 } else {
194 *p = '\0';
195 werr = create_key_recursive(db, path, p+1);
196 *p = '\\';
199 if (!W_ERROR_IS_OK(werr)) {
200 goto done;
203 werr = regdb_create_subkey_internal(db, path, subkey);
205 done:
206 return werr;
210 * Initialize a key in the registry:
211 * create each component key of the specified path.
213 static WERROR init_registry_key_internal(struct db_context *db,
214 const char *add_path)
216 char *subkey, *key;
217 WERROR werr;
218 TALLOC_CTX *frame = talloc_stackframe();
220 if (add_path == NULL) {
221 werr = WERR_INVALID_PARAM;
222 goto done;
225 key = talloc_strdup(frame, add_path);
227 subkey = strrchr_m(key, '\\');
228 if (subkey == NULL) {
229 subkey = key;
230 key = NULL;
231 } else {
232 *subkey = '\0';
233 subkey++;
236 werr = create_key_recursive(db, key, subkey);
238 done:
239 talloc_free(frame);
240 return werr;
243 struct init_registry_key_context {
244 const char *add_path;
247 static NTSTATUS init_registry_key_action(struct db_context *db,
248 void *private_data)
250 struct init_registry_key_context *init_ctx =
251 (struct init_registry_key_context *)private_data;
253 return werror_to_ntstatus(init_registry_key_internal(
254 db, init_ctx->add_path));
258 * Initialize a key in the registry:
259 * create each component key of the specified path,
260 * wrapped in one db transaction.
262 WERROR init_registry_key(const char *add_path)
264 struct init_registry_key_context init_ctx;
266 if (regdb_key_exists(regdb, add_path)) {
267 return WERR_OK;
270 init_ctx.add_path = add_path;
272 return regdb_trans_do(regdb,
273 init_registry_key_action,
274 &init_ctx);
277 /***********************************************************************
278 Open the registry data in the tdb
279 ***********************************************************************/
281 static void regdb_ctr_add_value(struct regval_ctr *ctr,
282 struct builtin_regkey_value *value)
284 switch(value->type) {
285 case REG_DWORD:
286 regval_ctr_addvalue(ctr, value->valuename, REG_DWORD,
287 (uint8_t *)&value->data.dw_value,
288 sizeof(uint32));
289 break;
291 case REG_SZ:
292 regval_ctr_addvalue_sz(ctr, value->valuename,
293 value->data.string);
294 break;
296 default:
297 DEBUG(0, ("regdb_ctr_add_value: invalid value type in "
298 "registry values [%d]\n", value->type));
302 static NTSTATUS init_registry_data_action(struct db_context *db,
303 void *private_data)
305 NTSTATUS status;
306 TALLOC_CTX *frame = talloc_stackframe();
307 struct regval_ctr *values;
308 int i;
310 /* loop over all of the predefined paths and add each component */
312 for (i=0; builtin_registry_paths[i] != NULL; i++) {
313 if (regdb_key_exists(db, builtin_registry_paths[i])) {
314 continue;
316 status = werror_to_ntstatus(init_registry_key_internal(db,
317 builtin_registry_paths[i]));
318 if (!NT_STATUS_IS_OK(status)) {
319 goto done;
323 /* loop over all of the predefined values and add each component */
325 for (i=0; builtin_registry_values[i].path != NULL; i++) {
326 WERROR werr;
328 werr = regval_ctr_init(frame, &values);
329 if (!W_ERROR_IS_OK(werr)) {
330 status = werror_to_ntstatus(werr);
331 goto done;
334 regdb_fetch_values_internal(db,
335 builtin_registry_values[i].path,
336 values);
338 /* preserve existing values across restarts. Only add new ones */
340 if (!regval_ctr_key_exists(values,
341 builtin_registry_values[i].valuename))
343 regdb_ctr_add_value(values,
344 &builtin_registry_values[i]);
345 status = regdb_store_values_internal(db,
346 builtin_registry_values[i].path,
347 values);
348 if (!NT_STATUS_IS_OK(status)) {
349 goto done;
352 TALLOC_FREE(values);
355 status = NT_STATUS_OK;
357 done:
359 TALLOC_FREE(frame);
360 return status;
363 WERROR init_registry_data(void)
365 WERROR werr;
366 TALLOC_CTX *frame = talloc_stackframe();
367 struct regval_ctr *values;
368 int i;
371 * First, check for the existence of the needed keys and values.
372 * If all do already exist, we can save the writes.
374 for (i=0; builtin_registry_paths[i] != NULL; i++) {
375 if (!regdb_key_exists(regdb, builtin_registry_paths[i])) {
376 goto do_init;
380 for (i=0; builtin_registry_values[i].path != NULL; i++) {
381 werr = regval_ctr_init(frame, &values);
382 W_ERROR_NOT_OK_GOTO_DONE(werr);
384 regdb_fetch_values_internal(regdb,
385 builtin_registry_values[i].path,
386 values);
387 if (!regval_ctr_key_exists(values,
388 builtin_registry_values[i].valuename))
390 TALLOC_FREE(values);
391 goto do_init;
394 TALLOC_FREE(values);
397 werr = WERR_OK;
398 goto done;
400 do_init:
403 * There are potentially quite a few store operations which are all
404 * indiviually wrapped in tdb transactions. Wrapping them in a single
405 * transaction gives just a single transaction_commit() to actually do
406 * its fsync()s. See tdb/common/transaction.c for info about nested
407 * transaction behaviour.
410 werr = regdb_trans_do(regdb,
411 init_registry_data_action,
412 NULL);
414 done:
415 TALLOC_FREE(frame);
416 return werr;
419 static int regdb_normalize_keynames_fn(struct db_record *rec,
420 void *private_data)
422 TALLOC_CTX *mem_ctx = talloc_tos();
423 const char *keyname;
424 NTSTATUS status;
425 TDB_DATA key;
426 TDB_DATA value;
427 struct db_context *db = (struct db_context *)private_data;
429 key = dbwrap_record_get_key(rec);
430 if (key.dptr == NULL || key.dsize == 0) {
431 return 0;
434 value = dbwrap_record_get_value(rec);
436 if (db == NULL) {
437 DEBUG(0, ("regdb_normalize_keynames_fn: ERROR: "
438 "NULL db context handed in via private_data\n"));
439 return 1;
442 if (strncmp((const char *)key.dptr, REGDB_VERSION_KEYNAME,
443 strlen(REGDB_VERSION_KEYNAME)) == 0)
445 return 0;
448 keyname = strchr((const char *)key.dptr, '/');
449 if (keyname) {
450 keyname = talloc_string_sub(mem_ctx,
451 (const char *)key.dptr,
452 "/",
453 "\\");
455 DEBUG(2, ("regdb_normalize_keynames_fn: Convert %s to %s\n",
456 (const char *)key.dptr,
457 keyname));
459 /* Delete the original record and store the normalized key */
460 status = dbwrap_record_delete(rec);
461 if (!NT_STATUS_IS_OK(status)) {
462 DEBUG(0,("regdb_normalize_keynames_fn: "
463 "tdb_delete for [%s] failed!\n",
464 (const char *)key.dptr));
465 return 1;
468 status = dbwrap_store_bystring(db, keyname, value, TDB_REPLACE);
469 if (!NT_STATUS_IS_OK(status)) {
470 DEBUG(0,("regdb_normalize_keynames_fn: "
471 "failed to store new record for [%s]!\n",
472 keyname));
473 return 1;
477 return 0;
480 static WERROR regdb_store_regdb_version(struct db_context *db, uint32_t version)
482 NTSTATUS status;
483 if (db == NULL) {
484 return WERR_CAN_NOT_COMPLETE;
487 status = dbwrap_trans_store_int32(db, REGDB_VERSION_KEYNAME, version);
488 if (!NT_STATUS_IS_OK(status)) {
489 DEBUG(1, ("regdb_store_regdb_version: error storing %s = %d: %s\n",
490 REGDB_VERSION_KEYNAME, version, nt_errstr(status)));
491 return ntstatus_to_werror(status);
492 } else {
493 DEBUG(10, ("regdb_store_regdb_version: stored %s = %d\n",
494 REGDB_VERSION_KEYNAME, version));
495 return WERR_OK;
499 static WERROR regdb_upgrade_v1_to_v2(struct db_context *db)
501 TALLOC_CTX *mem_ctx;
502 NTSTATUS status;
503 WERROR werr;
505 mem_ctx = talloc_stackframe();
507 status = dbwrap_traverse(db, regdb_normalize_keynames_fn, db, NULL);
508 if (!NT_STATUS_IS_OK(status)) {
509 werr = WERR_REG_IO_FAILURE;
510 goto done;
513 werr = regdb_store_regdb_version(db, REGDB_VERSION_V2);
515 done:
516 talloc_free(mem_ctx);
517 return werr;
520 static bool tdb_data_read_uint32(TDB_DATA *buf, uint32_t *result)
522 const size_t len = sizeof(uint32_t);
523 if (buf->dsize >= len) {
524 *result = IVAL(buf->dptr, 0);
525 buf->dptr += len;
526 buf->dsize -= len;
527 return true;
529 return false;
532 static bool tdb_data_read_cstr(TDB_DATA *buf, char **result)
534 const size_t len = strnlen((char*)buf->dptr, buf->dsize) + 1;
535 if (buf->dsize >= len) {
536 *result = (char*)buf->dptr;
537 buf->dptr += len;
538 buf->dsize -= len;
539 return true;
541 return false;
544 static bool tdb_data_is_empty(TDB_DATA d) {
545 return (d.dptr == NULL) || (d.dsize == 0);
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 rawmemchr(d.dptr, '\0') == &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 strupper_m(path);
568 if (!dbwrap_exists(db, string_term_tdb_data(path))) {
569 NTSTATUS status;
571 DEBUG(10, ("regdb_upgrade_v2_to_v3: writing subkey list [%s]\n",
572 path));
574 status = dbwrap_store_bystring(db, path, empty_subkey_list,
575 TDB_INSERT);
576 if (!NT_STATUS_IS_OK(status)) {
577 DEBUG(0, ("regdb_upgrade_v2_to_v3: writing subkey list "
578 "[%s] failed\n", path));
579 goto done;
582 success = true;
583 done:
584 talloc_free(path);
585 return success;
588 static bool upgrade_v2_to_v3_check_parent(struct db_context *db,
589 const char *key)
591 const char *sep = strrchr_m(key, '\\');
592 if (sep != NULL) {
593 char *pkey = talloc_strndup(talloc_tos(), key, sep-key);
594 if (!dbwrap_exists(db, string_term_tdb_data(pkey))) {
595 DEBUG(0, ("regdb_upgrade_v2_to_v3: missing subkey list "
596 "[%s]\nrun \"net registry check\"\n", pkey));
598 talloc_free(pkey);
600 return true;
604 #define IS_EQUAL(d,s) (((d).dsize == strlen(s)+1) && \
605 (strcmp((char*)(d).dptr, (s)) == 0))
606 #define STARTS_WITH(d,s) (((d).dsize > strlen(s)) && \
607 (strncmp((char*)(d).dptr, (s), strlen(s)) == 0))
608 #define SSTR(d) (int)(d).dsize , (char*)(d).dptr
611 static int regdb_upgrade_v2_to_v3_fn(struct db_record *rec, void *private_data)
613 struct db_context *db = (struct db_context *)private_data;
614 TDB_DATA key = dbwrap_record_get_key(rec);
615 TDB_DATA val = dbwrap_record_get_value(rec);
617 if (tdb_data_is_empty(key)) {
618 return 0;
621 if (db == NULL) {
622 DEBUG(0, ("regdb_upgrade_v2_to_v3_fn: ERROR: "
623 "NULL db context handed in via private_data\n"));
624 return 1;
627 if (IS_EQUAL(key, REGDB_VERSION_KEYNAME) ||
628 STARTS_WITH(key, REG_VALUE_PREFIX) ||
629 STARTS_WITH(key, REG_SECDESC_PREFIX))
631 DEBUG(10, ("regdb_upgrade_v2_to_v3: skipping [%.*s]\n",
632 SSTR(key)));
633 return 0;
636 if (STARTS_WITH(key, REG_SORTED_SUBKEYS_PREFIX)) {
637 NTSTATUS status;
638 /* Delete the deprecated sorted subkeys cache. */
640 DEBUG(10, ("regdb_upgrade_v2_to_v3: deleting [%.*s]\n",
641 SSTR(key)));
643 status = dbwrap_record_delete(rec);
644 if (!NT_STATUS_IS_OK(status)) {
645 DEBUG(0, ("regdb_upgrade_v2_to_v3: deleting [%.*s] "
646 "failed!\n", SSTR(key)));
647 return 1;
650 return 0;
653 if ( tdb_data_is_cstr(key) &&
654 hive_info((char*)key.dptr) != NULL )
657 * Found a regular subkey list record.
658 * Walk the list and create the list record for those
659 * subkeys that don't already have one.
661 TDB_DATA pos = val;
662 char *subkey, *path = (char*)key.dptr;
663 uint32_t num_items, found_items = 0;
666 DEBUG(10, ("regdb_upgrade_v2_to_v3: scanning subkeylist of "
667 "[%s]\n", path));
669 if (!tdb_data_read_uint32(&pos, &num_items)) {
670 /* invalid or empty - skip */
671 return 0;
674 while (tdb_data_read_cstr(&pos, &subkey)) {
675 found_items++;
677 if (!upgrade_v2_to_v3_check_subkeylist(db, path, subkey))
679 return 1;
682 if (!upgrade_v2_to_v3_check_parent(db, path)) {
683 return 1;
686 if (found_items != num_items) {
687 DEBUG(0, ("regdb_upgrade_v2_to_v3: inconsistent subkey "
688 "list [%s]\nrun \"net registry check\"\n",
689 path));
691 } else {
692 DEBUG(10, ("regdb_upgrade_v2_to_v3: skipping invalid [%.*s]\n"
693 "run \"net registry check\"\n", SSTR(key)));
696 return 0;
699 static WERROR regdb_upgrade_v2_to_v3(struct db_context *db)
701 NTSTATUS status;
702 WERROR werr;
704 status = dbwrap_traverse(db, regdb_upgrade_v2_to_v3_fn, db, NULL);
705 if (!NT_STATUS_IS_OK(status)) {
706 werr = WERR_REG_IO_FAILURE;
707 goto done;
710 werr = regdb_store_regdb_version(db, REGDB_VERSION_V3);
712 done:
713 return werr;
716 /***********************************************************************
717 Open the registry database
718 ***********************************************************************/
720 WERROR regdb_init(void)
722 int32_t vers_id;
723 WERROR werr;
724 NTSTATUS status;
726 if (regdb) {
727 DEBUG(10, ("regdb_init: incrementing refcount (%d->%d)\n",
728 regdb_refcount, regdb_refcount+1));
729 regdb_refcount++;
730 return WERR_OK;
733 regdb = db_open(NULL, state_path("registry.tdb"), 0,
734 REG_TDB_FLAGS, O_RDWR, 0600);
735 if (!regdb) {
736 regdb = db_open(NULL, state_path("registry.tdb"), 0,
737 REG_TDB_FLAGS, O_RDWR|O_CREAT, 0600);
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(regdb, REGDB_VERSION_KEYNAME, &vers_id);
760 if (!NT_STATUS_IS_OK(status)) {
761 DEBUG(10, ("regdb_init: registry version uninitialized "
762 "(got %d), initializing to version %d\n",
763 vers_id, REGDB_VERSION_V1));
766 * There was a regdb format version prior to version 1
767 * which did not store a INFO/version key. The format
768 * of this version was identical to version 1 except for
769 * the lack of the sorted subkey cache records.
770 * Since these are disposable, we can safely assume version
771 * 1 if no INFO/version key is found and run the db through
772 * the whole chain of upgrade. If the database was not
773 * initialized, this does not harm. If it was the unversioned
774 * version ("0"), then it do the right thing with the records.
776 werr = regdb_store_regdb_version(regdb, REGDB_VERSION_V1);
777 if (!W_ERROR_IS_OK(werr)) {
778 return werr;
780 vers_id = REGDB_VERSION_V1;
783 if (vers_id == REGDB_CODE_VERSION) {
784 return WERR_OK;
787 if (vers_id > REGDB_CODE_VERSION || vers_id == 0) {
788 DEBUG(0, ("regdb_init: unknown registry version %d "
789 "(code version = %d), refusing initialization\n",
790 vers_id, REGDB_CODE_VERSION));
791 return WERR_CAN_NOT_COMPLETE;
794 if (dbwrap_transaction_start(regdb) != 0) {
795 return WERR_REG_IO_FAILURE;
798 if (vers_id == REGDB_VERSION_V1) {
799 DEBUG(10, ("regdb_init: upgrading registry from version %d "
800 "to %d\n", REGDB_VERSION_V1, REGDB_VERSION_V2));
802 werr = regdb_upgrade_v1_to_v2(regdb);
803 if (!W_ERROR_IS_OK(werr)) {
804 dbwrap_transaction_cancel(regdb);
805 return werr;
808 vers_id = REGDB_VERSION_V2;
811 if (vers_id == REGDB_VERSION_V2) {
812 DEBUG(10, ("regdb_init: upgrading registry from version %d "
813 "to %d\n", REGDB_VERSION_V2, REGDB_VERSION_V3));
815 werr = regdb_upgrade_v2_to_v3(regdb);
816 if (!W_ERROR_IS_OK(werr)) {
817 dbwrap_transaction_cancel(regdb);
818 return werr;
821 vers_id = REGDB_VERSION_V3;
824 /* future upgrade code should go here */
826 if (dbwrap_transaction_commit(regdb) != 0) {
827 return WERR_REG_IO_FAILURE;
830 return WERR_OK;
833 /***********************************************************************
834 Open the registry. Must already have been initialized by regdb_init()
835 ***********************************************************************/
837 WERROR regdb_open( void )
839 WERROR result = WERR_OK;
841 if ( regdb ) {
842 DEBUG(10, ("regdb_open: incrementing refcount (%d->%d)\n",
843 regdb_refcount, regdb_refcount+1));
844 regdb_refcount++;
845 return WERR_OK;
848 become_root();
850 regdb = db_open(NULL, state_path("registry.tdb"), 0,
851 REG_TDB_FLAGS, O_RDWR, 0600);
852 if ( !regdb ) {
853 result = ntstatus_to_werror( map_nt_error_from_unix( errno ) );
854 DEBUG(0,("regdb_open: Failed to open %s! (%s)\n",
855 state_path("registry.tdb"), strerror(errno) ));
858 unbecome_root();
860 regdb_refcount = 1;
861 DEBUG(10, ("regdb_open: registry db opened. refcount reset (%d)\n",
862 regdb_refcount));
864 return result;
867 /***********************************************************************
868 ***********************************************************************/
870 int regdb_close( void )
872 if (regdb_refcount == 0) {
873 return 0;
876 regdb_refcount--;
878 DEBUG(10, ("regdb_close: decrementing refcount (%d->%d)\n",
879 regdb_refcount+1, regdb_refcount));
881 if ( regdb_refcount > 0 )
882 return 0;
884 SMB_ASSERT( regdb_refcount >= 0 );
886 TALLOC_FREE(regdb);
887 return 0;
890 WERROR regdb_transaction_start(void)
892 return (dbwrap_transaction_start(regdb) == 0) ?
893 WERR_OK : WERR_REG_IO_FAILURE;
896 WERROR regdb_transaction_commit(void)
898 return (dbwrap_transaction_commit(regdb) == 0) ?
899 WERR_OK : WERR_REG_IO_FAILURE;
902 WERROR regdb_transaction_cancel(void)
904 return (dbwrap_transaction_cancel(regdb) == 0) ?
905 WERR_OK : WERR_REG_IO_FAILURE;
908 /***********************************************************************
909 return the tdb sequence number of the registry tdb.
910 this is an indicator for the content of the registry
911 having changed. it will change upon regdb_init, too, though.
912 ***********************************************************************/
913 int regdb_get_seqnum(void)
915 return dbwrap_get_seqnum(regdb);
919 static WERROR regdb_delete_key_with_prefix(struct db_context *db,
920 const char *keyname,
921 const char *prefix)
923 char *path;
924 WERROR werr = WERR_NOMEM;
925 TALLOC_CTX *mem_ctx = talloc_stackframe();
927 if (keyname == NULL) {
928 werr = WERR_INVALID_PARAM;
929 goto done;
932 if (prefix == NULL) {
933 path = discard_const_p(char, keyname);
934 } else {
935 path = talloc_asprintf(mem_ctx, "%s\\%s", prefix, keyname);
936 if (path == NULL) {
937 goto done;
941 path = normalize_reg_path(mem_ctx, path);
942 if (path == NULL) {
943 goto done;
946 werr = ntstatus_to_werror(dbwrap_delete_bystring(db, path));
948 /* treat "not found" as ok */
949 if (W_ERROR_EQUAL(werr, WERR_NOT_FOUND)) {
950 werr = WERR_OK;
953 done:
954 talloc_free(mem_ctx);
955 return werr;
959 static WERROR regdb_delete_values(struct db_context *db, const char *keyname)
961 return regdb_delete_key_with_prefix(db, keyname, REG_VALUE_PREFIX);
964 static WERROR regdb_delete_secdesc(struct db_context *db, const char *keyname)
966 return regdb_delete_key_with_prefix(db, keyname, REG_SECDESC_PREFIX);
969 static WERROR regdb_delete_subkeylist(struct db_context *db, const char *keyname)
971 return regdb_delete_key_with_prefix(db, keyname, NULL);
975 static WERROR regdb_delete_key_lists(struct db_context *db, const char *keyname)
977 WERROR werr;
979 werr = regdb_delete_values(db, keyname);
980 if (!W_ERROR_IS_OK(werr)) {
981 DEBUG(1, (__location__ " Deleting %s\\%s failed: %s\n",
982 REG_VALUE_PREFIX, keyname, win_errstr(werr)));
983 goto done;
986 werr = regdb_delete_secdesc(db, keyname);
987 if (!W_ERROR_IS_OK(werr)) {
988 DEBUG(1, (__location__ " Deleting %s\\%s failed: %s\n",
989 REG_SECDESC_PREFIX, keyname, win_errstr(werr)));
990 goto done;
993 werr = regdb_delete_subkeylist(db, keyname);
994 if (!W_ERROR_IS_OK(werr)) {
995 DEBUG(1, (__location__ " Deleting %s failed: %s\n",
996 keyname, win_errstr(werr)));
997 goto done;
1000 done:
1001 return werr;
1004 /***********************************************************************
1005 Add subkey strings to the registry tdb under a defined key
1006 fmt is the same format as tdb_pack except this function only supports
1007 fstrings
1008 ***********************************************************************/
1010 static WERROR regdb_store_keys_internal2(struct db_context *db,
1011 const char *key,
1012 struct regsubkey_ctr *ctr)
1014 TDB_DATA dbuf;
1015 uint8 *buffer = NULL;
1016 int i = 0;
1017 uint32 len, buflen;
1018 uint32 num_subkeys = regsubkey_ctr_numkeys(ctr);
1019 char *keyname = NULL;
1020 TALLOC_CTX *ctx = talloc_stackframe();
1021 WERROR werr;
1023 if (!key) {
1024 werr = WERR_INVALID_PARAM;
1025 goto done;
1028 keyname = talloc_strdup(ctx, key);
1029 if (!keyname) {
1030 werr = WERR_NOMEM;
1031 goto done;
1034 keyname = normalize_reg_path(ctx, keyname);
1035 if (!keyname) {
1036 werr = WERR_NOMEM;
1037 goto done;
1040 /* allocate some initial memory */
1042 buffer = (uint8 *)SMB_MALLOC(1024);
1043 if (buffer == NULL) {
1044 werr = WERR_NOMEM;
1045 goto done;
1047 buflen = 1024;
1048 len = 0;
1050 /* store the number of subkeys */
1052 len += tdb_pack(buffer+len, buflen-len, "d", num_subkeys);
1054 /* pack all the strings */
1056 for (i=0; i<num_subkeys; i++) {
1057 size_t thistime;
1059 thistime = tdb_pack(buffer+len, buflen-len, "f",
1060 regsubkey_ctr_specific_key(ctr, i));
1061 if (len+thistime > buflen) {
1062 size_t thistime2;
1064 * tdb_pack hasn't done anything because of the short
1065 * buffer, allocate extra space.
1067 buffer = SMB_REALLOC_ARRAY(buffer, uint8_t,
1068 (len+thistime)*2);
1069 if(buffer == NULL) {
1070 DEBUG(0, ("regdb_store_keys: Failed to realloc "
1071 "memory of size [%u]\n",
1072 (unsigned int)(len+thistime)*2));
1073 werr = WERR_NOMEM;
1074 goto done;
1076 buflen = (len+thistime)*2;
1077 thistime2 = tdb_pack(
1078 buffer+len, buflen-len, "f",
1079 regsubkey_ctr_specific_key(ctr, i));
1080 if (thistime2 != thistime) {
1081 DEBUG(0, ("tdb_pack failed\n"));
1082 werr = WERR_CAN_NOT_COMPLETE;
1083 goto done;
1086 len += thistime;
1089 /* finally write out the data */
1091 dbuf.dptr = buffer;
1092 dbuf.dsize = len;
1093 werr = ntstatus_to_werror(dbwrap_store_bystring(db, keyname, dbuf,
1094 TDB_REPLACE));
1096 done:
1097 TALLOC_FREE(ctx);
1098 SAFE_FREE(buffer);
1099 return werr;
1103 * Utility function to store a new empty list of
1104 * subkeys of given key specified as parent and subkey name
1105 * (thereby creating the key).
1106 * If the parent keyname is NULL, then the "subkey" is
1107 * interpreted as a base key.
1108 * If the subkey list does already exist, it is not modified.
1110 * Must be called from within a transaction.
1112 static WERROR regdb_store_subkey_list(struct db_context *db, const char *parent,
1113 const char *key)
1115 WERROR werr;
1116 char *path = NULL;
1117 struct regsubkey_ctr *subkeys = NULL;
1118 TALLOC_CTX *frame = talloc_stackframe();
1120 if (parent == NULL) {
1121 path = talloc_strdup(frame, key);
1122 } else {
1123 path = talloc_asprintf(frame, "%s\\%s", parent, key);
1125 if (!path) {
1126 werr = WERR_NOMEM;
1127 goto done;
1130 werr = regsubkey_ctr_init(frame, &subkeys);
1131 W_ERROR_NOT_OK_GOTO_DONE(werr);
1133 werr = regdb_fetch_keys_internal(db, path, subkeys);
1134 if (W_ERROR_IS_OK(werr)) {
1135 /* subkey list exists already - don't modify */
1136 goto done;
1139 werr = regsubkey_ctr_reinit(subkeys);
1140 W_ERROR_NOT_OK_GOTO_DONE(werr);
1142 /* create a record with 0 subkeys */
1143 werr = regdb_store_keys_internal2(db, path, subkeys);
1144 if (!W_ERROR_IS_OK(werr)) {
1145 DEBUG(0, ("regdb_store_keys: Failed to store new record for "
1146 "key [%s]: %s\n", path, win_errstr(werr)));
1147 goto done;
1150 done:
1151 talloc_free(frame);
1152 return werr;
1155 /***********************************************************************
1156 Store the new subkey record and create any child key records that
1157 do not currently exist
1158 ***********************************************************************/
1160 struct regdb_store_keys_context {
1161 const char *key;
1162 struct regsubkey_ctr *ctr;
1165 static NTSTATUS regdb_store_keys_action(struct db_context *db,
1166 void *private_data)
1168 struct regdb_store_keys_context *store_ctx;
1169 WERROR werr;
1170 int num_subkeys, i;
1171 char *path = NULL;
1172 struct regsubkey_ctr *old_subkeys = NULL;
1173 char *oldkeyname = NULL;
1174 TALLOC_CTX *mem_ctx = talloc_stackframe();
1176 store_ctx = (struct regdb_store_keys_context *)private_data;
1179 * Re-fetch the old keys inside the transaction
1182 werr = regsubkey_ctr_init(mem_ctx, &old_subkeys);
1183 W_ERROR_NOT_OK_GOTO_DONE(werr);
1185 werr = regdb_fetch_keys_internal(db, store_ctx->key, old_subkeys);
1186 if (!W_ERROR_IS_OK(werr) &&
1187 !W_ERROR_EQUAL(werr, WERR_NOT_FOUND))
1189 goto done;
1193 * Make the store operation as safe as possible without transactions:
1195 * (1) For each subkey removed from ctr compared with old_subkeys:
1197 * (a) First delete the value db entry.
1199 * (b) Next delete the secdesc db record.
1201 * (c) Then delete the subkey list entry.
1203 * (2) Now write the list of subkeys of the parent key,
1204 * deleting removed entries and adding new ones.
1206 * (3) Finally create the subkey list entries for the added keys.
1208 * This way if we crash half-way in between deleting the subkeys
1209 * and storing the parent's list of subkeys, no old data can pop up
1210 * out of the blue when re-adding keys later on.
1213 /* (1) delete removed keys' lists (values/secdesc/subkeys) */
1215 num_subkeys = regsubkey_ctr_numkeys(old_subkeys);
1216 for (i=0; i<num_subkeys; i++) {
1217 oldkeyname = regsubkey_ctr_specific_key(old_subkeys, i);
1219 if (regsubkey_ctr_key_exists(store_ctx->ctr, oldkeyname)) {
1221 * It's still around, don't delete
1223 continue;
1226 path = talloc_asprintf(mem_ctx, "%s\\%s", store_ctx->key,
1227 oldkeyname);
1228 if (!path) {
1229 werr = WERR_NOMEM;
1230 goto done;
1233 werr = regdb_delete_key_lists(db, path);
1234 W_ERROR_NOT_OK_GOTO_DONE(werr);
1236 TALLOC_FREE(path);
1239 TALLOC_FREE(old_subkeys);
1241 /* (2) store the subkey list for the parent */
1243 werr = regdb_store_keys_internal2(db, store_ctx->key, store_ctx->ctr);
1244 if (!W_ERROR_IS_OK(werr)) {
1245 DEBUG(0,("regdb_store_keys: Failed to store new subkey list "
1246 "for parent [%s]: %s\n", store_ctx->key,
1247 win_errstr(werr)));
1248 goto done;
1251 /* (3) now create records for any subkeys that don't already exist */
1253 num_subkeys = regsubkey_ctr_numkeys(store_ctx->ctr);
1255 for (i=0; i<num_subkeys; i++) {
1256 const char *subkey;
1258 subkey = regsubkey_ctr_specific_key(store_ctx->ctr, i);
1260 werr = regdb_store_subkey_list(db, store_ctx->key, subkey);
1261 W_ERROR_NOT_OK_GOTO_DONE(werr);
1264 werr = WERR_OK;
1266 done:
1267 talloc_free(mem_ctx);
1268 return werror_to_ntstatus(werr);
1271 static bool regdb_store_keys_internal(struct db_context *db, const char *key,
1272 struct regsubkey_ctr *ctr)
1274 int num_subkeys, old_num_subkeys, i;
1275 struct regsubkey_ctr *old_subkeys = NULL;
1276 TALLOC_CTX *ctx = talloc_stackframe();
1277 WERROR werr;
1278 bool ret = false;
1279 struct regdb_store_keys_context store_ctx;
1281 if (!regdb_key_exists(db, key)) {
1282 goto done;
1286 * fetch a list of the old subkeys so we can determine if anything has
1287 * changed
1290 werr = regsubkey_ctr_init(ctx, &old_subkeys);
1291 if (!W_ERROR_IS_OK(werr)) {
1292 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
1293 goto done;
1296 werr = regdb_fetch_keys_internal(db, key, old_subkeys);
1297 if (!W_ERROR_IS_OK(werr) &&
1298 !W_ERROR_EQUAL(werr, WERR_NOT_FOUND))
1300 goto done;
1303 num_subkeys = regsubkey_ctr_numkeys(ctr);
1304 old_num_subkeys = regsubkey_ctr_numkeys(old_subkeys);
1305 if ((num_subkeys && old_num_subkeys) &&
1306 (num_subkeys == old_num_subkeys)) {
1308 for (i = 0; i < num_subkeys; i++) {
1309 if (strcmp(regsubkey_ctr_specific_key(ctr, i),
1310 regsubkey_ctr_specific_key(old_subkeys, i))
1311 != 0)
1313 break;
1316 if (i == num_subkeys) {
1318 * Nothing changed, no point to even start a tdb
1319 * transaction
1322 ret = true;
1323 goto done;
1327 TALLOC_FREE(old_subkeys);
1329 store_ctx.key = key;
1330 store_ctx.ctr = ctr;
1332 werr = regdb_trans_do(db,
1333 regdb_store_keys_action,
1334 &store_ctx);
1336 ret = W_ERROR_IS_OK(werr);
1338 done:
1339 TALLOC_FREE(ctx);
1341 return ret;
1344 bool regdb_store_keys(const char *key, struct regsubkey_ctr *ctr)
1346 return regdb_store_keys_internal(regdb, key, ctr);
1350 * create a subkey of a given key
1353 struct regdb_create_subkey_context {
1354 const char *key;
1355 const char *subkey;
1358 static NTSTATUS regdb_create_subkey_action(struct db_context *db,
1359 void *private_data)
1361 WERROR werr;
1362 struct regdb_create_subkey_context *create_ctx;
1363 struct regsubkey_ctr *subkeys;
1364 TALLOC_CTX *mem_ctx = talloc_stackframe();
1366 create_ctx = (struct regdb_create_subkey_context *)private_data;
1368 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
1369 W_ERROR_NOT_OK_GOTO_DONE(werr);
1371 werr = regdb_fetch_keys_internal(db, create_ctx->key, subkeys);
1372 W_ERROR_NOT_OK_GOTO_DONE(werr);
1374 werr = regsubkey_ctr_addkey(subkeys, create_ctx->subkey);
1375 W_ERROR_NOT_OK_GOTO_DONE(werr);
1377 werr = regdb_store_keys_internal2(db, create_ctx->key, subkeys);
1378 if (!W_ERROR_IS_OK(werr)) {
1379 DEBUG(0, (__location__ " failed to store new subkey list for "
1380 "parent key %s: %s\n", create_ctx->key,
1381 win_errstr(werr)));
1384 werr = regdb_store_subkey_list(db, create_ctx->key, create_ctx->subkey);
1386 done:
1387 talloc_free(mem_ctx);
1388 return werror_to_ntstatus(werr);
1391 static WERROR regdb_create_subkey_internal(struct db_context *db,
1392 const char *key,
1393 const char *subkey)
1395 WERROR werr;
1396 struct regsubkey_ctr *subkeys;
1397 TALLOC_CTX *mem_ctx = talloc_stackframe();
1398 struct regdb_create_subkey_context create_ctx;
1400 if (!regdb_key_exists(db, key)) {
1401 werr = WERR_NOT_FOUND;
1402 goto done;
1405 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
1406 W_ERROR_NOT_OK_GOTO_DONE(werr);
1408 werr = regdb_fetch_keys_internal(db, key, subkeys);
1409 W_ERROR_NOT_OK_GOTO_DONE(werr);
1411 if (regsubkey_ctr_key_exists(subkeys, subkey)) {
1412 char *newkey;
1414 newkey = talloc_asprintf(mem_ctx, "%s\\%s", key, subkey);
1415 if (newkey == NULL) {
1416 werr = WERR_NOMEM;
1417 goto done;
1420 if (regdb_key_exists(db, newkey)) {
1421 werr = WERR_OK;
1422 goto done;
1426 talloc_free(subkeys);
1428 create_ctx.key = key;
1429 create_ctx.subkey = subkey;
1431 werr = regdb_trans_do(db,
1432 regdb_create_subkey_action,
1433 &create_ctx);
1435 done:
1436 talloc_free(mem_ctx);
1437 return werr;
1440 static WERROR regdb_create_subkey(const char *key, const char *subkey)
1442 return regdb_create_subkey_internal(regdb, key, subkey);
1446 * create a base key
1449 struct regdb_create_basekey_context {
1450 const char *key;
1453 static NTSTATUS regdb_create_basekey_action(struct db_context *db,
1454 void *private_data)
1456 WERROR werr;
1457 struct regdb_create_basekey_context *create_ctx;
1459 create_ctx = (struct regdb_create_basekey_context *)private_data;
1461 werr = regdb_store_subkey_list(db, NULL, create_ctx->key);
1463 return werror_to_ntstatus(werr);
1466 static WERROR regdb_create_basekey(struct db_context *db, const char *key)
1468 WERROR werr;
1469 struct regdb_create_subkey_context create_ctx;
1471 create_ctx.key = key;
1473 werr = regdb_trans_do(db,
1474 regdb_create_basekey_action,
1475 &create_ctx);
1477 return werr;
1481 * create a subkey of a given key
1484 struct regdb_delete_subkey_context {
1485 const char *key;
1486 const char *subkey;
1487 const char *path;
1488 bool lazy;
1491 static NTSTATUS regdb_delete_subkey_action(struct db_context *db,
1492 void *private_data)
1494 WERROR werr;
1495 struct regdb_delete_subkey_context *delete_ctx;
1496 struct regsubkey_ctr *subkeys;
1497 TALLOC_CTX *mem_ctx = talloc_stackframe();
1499 delete_ctx = (struct regdb_delete_subkey_context *)private_data;
1501 werr = regdb_delete_key_lists(db, delete_ctx->path);
1502 W_ERROR_NOT_OK_GOTO_DONE(werr);
1504 if (delete_ctx->lazy) {
1505 goto done;
1508 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
1509 W_ERROR_NOT_OK_GOTO_DONE(werr);
1511 werr = regdb_fetch_keys_internal(db, delete_ctx->key, subkeys);
1512 W_ERROR_NOT_OK_GOTO_DONE(werr);
1514 werr = regsubkey_ctr_delkey(subkeys, delete_ctx->subkey);
1515 W_ERROR_NOT_OK_GOTO_DONE(werr);
1517 werr = regdb_store_keys_internal2(db, delete_ctx->key, subkeys);
1518 if (!W_ERROR_IS_OK(werr)) {
1519 DEBUG(0, (__location__ " failed to store new subkey_list for "
1520 "parent key %s: %s\n", delete_ctx->key,
1521 win_errstr(werr)));
1524 done:
1525 talloc_free(mem_ctx);
1526 return werror_to_ntstatus(werr);
1529 static WERROR regdb_delete_subkey(const char *key, const char *subkey, bool lazy)
1531 WERROR werr;
1532 char *path;
1533 struct regdb_delete_subkey_context delete_ctx;
1534 TALLOC_CTX *mem_ctx = talloc_stackframe();
1536 if (!regdb_key_exists(regdb, key)) {
1537 werr = WERR_NOT_FOUND;
1538 goto done;
1541 path = talloc_asprintf(mem_ctx, "%s\\%s", key, subkey);
1542 if (path == NULL) {
1543 werr = WERR_NOMEM;
1544 goto done;
1547 if (!regdb_key_exists(regdb, path)) {
1548 werr = WERR_OK;
1549 goto done;
1552 delete_ctx.key = key;
1553 delete_ctx.subkey = subkey;
1554 delete_ctx.path = path;
1555 delete_ctx.lazy = lazy;
1557 werr = regdb_trans_do(regdb,
1558 regdb_delete_subkey_action,
1559 &delete_ctx);
1561 done:
1562 talloc_free(mem_ctx);
1563 return werr;
1566 static TDB_DATA regdb_fetch_key_internal(struct db_context *db,
1567 TALLOC_CTX *mem_ctx, const char *key)
1569 char *path = NULL;
1570 TDB_DATA data;
1571 NTSTATUS status;
1573 path = normalize_reg_path(mem_ctx, key);
1574 if (!path) {
1575 return make_tdb_data(NULL, 0);
1578 status = dbwrap_fetch_bystring(db, mem_ctx, path, &data);
1579 if (!NT_STATUS_IS_OK(status)) {
1580 data = tdb_null;
1583 TALLOC_FREE(path);
1584 return data;
1589 * Check for the existence of a key.
1591 * Existence of a key is authoritatively defined by
1592 * the existence of the record that contains the list
1593 * of its subkeys.
1595 * Return false, if the record does not match the correct
1596 * structure of an initial 4-byte counter and then a
1597 * list of the corresponding number of zero-terminated
1598 * strings.
1600 static bool regdb_key_exists(struct db_context *db, const char *key)
1602 TALLOC_CTX *mem_ctx = talloc_stackframe();
1603 TDB_DATA value;
1604 bool ret = false;
1605 char *path;
1606 uint32_t buflen;
1607 const char *buf;
1608 uint32_t num_items, i;
1609 int32_t len;
1611 if (key == NULL) {
1612 goto done;
1615 path = normalize_reg_path(mem_ctx, key);
1616 if (path == NULL) {
1617 DEBUG(0, ("out of memory! (talloc failed)\n"));
1618 goto done;
1621 if (*path == '\0') {
1622 goto done;
1625 value = regdb_fetch_key_internal(db, mem_ctx, path);
1626 if (value.dptr == NULL) {
1627 goto done;
1630 if (value.dsize == 0) {
1631 DEBUG(10, ("regdb_key_exists: subkeylist-record for key "
1632 "[%s] is empty: Could be a deleted record in a "
1633 "clustered (ctdb) environment?\n",
1634 path));
1635 goto done;
1638 len = tdb_unpack(value.dptr, value.dsize, "d", &num_items);
1639 if (len == (int32_t)-1) {
1640 DEBUG(1, ("regdb_key_exists: ERROR: subkeylist-record for key "
1641 "[%s] is invalid: Could not parse initial 4-byte "
1642 "counter. record data length is %u.\n",
1643 path, (unsigned int)value.dsize));
1644 goto done;
1648 * Note: the tdb_unpack check above implies that len <= value.dsize
1650 buflen = value.dsize - len;
1651 buf = (const char *)value.dptr + len;
1653 len = 0;
1655 for (i = 0; i < num_items; i++) {
1656 if (buflen == 0) {
1657 break;
1659 len = strnlen(buf, buflen) + 1;
1660 if (buflen < len) {
1661 DEBUG(1, ("regdb_key_exists: ERROR: subkeylist-record "
1662 "for key [%s] is corrupt: %u items expected, "
1663 "item number %u is not zero terminated.\n",
1664 path, num_items, i+1));
1665 goto done;
1668 buf += len;
1669 buflen -= len;
1672 if (buflen > 0) {
1673 DEBUG(1, ("regdb_key_exists: ERROR: subkeylist-record for key "
1674 "[%s] is corrupt: %u items expected and found, but "
1675 "the record contains additional %u bytes\n",
1676 path, num_items, buflen));
1677 goto done;
1680 if (i < num_items) {
1681 DEBUG(1, ("regdb_key_exists: ERROR: subkeylist-record for key "
1682 "[%s] is corrupt: %u items expected, but only %u "
1683 "items found.\n",
1684 path, num_items, i+1));
1685 goto done;
1688 ret = true;
1690 done:
1691 TALLOC_FREE(mem_ctx);
1692 return ret;
1696 /***********************************************************************
1697 Retrieve an array of strings containing subkeys. Memory should be
1698 released by the caller.
1699 ***********************************************************************/
1701 static WERROR regdb_fetch_keys_internal(struct db_context *db, const char *key,
1702 struct regsubkey_ctr *ctr)
1704 WERROR werr;
1705 uint32_t num_items;
1706 uint8 *buf;
1707 uint32 buflen, len;
1708 int i;
1709 fstring subkeyname;
1710 TALLOC_CTX *frame = talloc_stackframe();
1711 TDB_DATA value;
1713 DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key ? key : "NULL"));
1715 if (!regdb_key_exists(db, key)) {
1716 DEBUG(10, ("key [%s] not found\n", key));
1717 werr = WERR_NOT_FOUND;
1718 goto done;
1721 werr = regsubkey_ctr_reinit(ctr);
1722 W_ERROR_NOT_OK_GOTO_DONE(werr);
1724 werr = regsubkey_ctr_set_seqnum(ctr, dbwrap_get_seqnum(db));
1725 W_ERROR_NOT_OK_GOTO_DONE(werr);
1727 value = regdb_fetch_key_internal(db, frame, key);
1729 if (value.dsize == 0 || value.dptr == NULL) {
1730 DEBUG(10, ("regdb_fetch_keys: no subkeys found for key [%s]\n",
1731 key));
1732 goto done;
1735 buf = value.dptr;
1736 buflen = value.dsize;
1737 len = tdb_unpack( buf, buflen, "d", &num_items);
1738 if (len == (uint32_t)-1) {
1739 werr = WERR_NOT_FOUND;
1740 goto done;
1743 for (i=0; i<num_items; i++) {
1744 len += tdb_unpack(buf+len, buflen-len, "f", subkeyname);
1745 werr = regsubkey_ctr_addkey(ctr, subkeyname);
1746 if (!W_ERROR_IS_OK(werr)) {
1747 DEBUG(5, ("regdb_fetch_keys: regsubkey_ctr_addkey "
1748 "failed: %s\n", win_errstr(werr)));
1749 num_items = 0;
1750 goto done;
1754 DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items));
1756 done:
1757 TALLOC_FREE(frame);
1758 return werr;
1761 int regdb_fetch_keys(const char *key, struct regsubkey_ctr *ctr)
1763 WERROR werr;
1765 werr = regdb_fetch_keys_internal(regdb, key, ctr);
1766 if (!W_ERROR_IS_OK(werr)) {
1767 return -1;
1770 return regsubkey_ctr_numkeys(ctr);
1773 /****************************************************************************
1774 Unpack a list of registry values frem the TDB
1775 ***************************************************************************/
1777 static int regdb_unpack_values(struct regval_ctr *values, uint8 *buf, int buflen)
1779 int len = 0;
1780 uint32 type;
1781 fstring valuename;
1782 uint32 size;
1783 uint8 *data_p;
1784 uint32 num_values = 0;
1785 int i;
1787 /* loop and unpack the rest of the registry values */
1789 len += tdb_unpack(buf+len, buflen-len, "d", &num_values);
1791 for ( i=0; i<num_values; i++ ) {
1792 /* unpack the next regval */
1794 type = REG_NONE;
1795 size = 0;
1796 data_p = NULL;
1797 valuename[0] = '\0';
1798 len += tdb_unpack(buf+len, buflen-len, "fdB",
1799 valuename,
1800 &type,
1801 &size,
1802 &data_p);
1804 regval_ctr_addvalue(values, valuename, type,
1805 (uint8_t *)data_p, size);
1806 SAFE_FREE(data_p); /* 'B' option to tdb_unpack does a malloc() */
1808 DEBUG(8,("specific: [%s], len: %d\n", valuename, size));
1811 return len;
1814 /****************************************************************************
1815 Pack all values in all printer keys
1816 ***************************************************************************/
1818 static int regdb_pack_values(struct regval_ctr *values, uint8 *buf, int buflen)
1820 int len = 0;
1821 int i;
1822 struct regval_blob *val;
1823 int num_values;
1825 if ( !values )
1826 return 0;
1828 num_values = regval_ctr_numvals( values );
1830 /* pack the number of values first */
1832 len += tdb_pack( buf+len, buflen-len, "d", num_values );
1834 /* loop over all values */
1836 for ( i=0; i<num_values; i++ ) {
1837 val = regval_ctr_specific_value( values, i );
1838 len += tdb_pack(buf+len, buflen-len, "fdB",
1839 regval_name(val),
1840 regval_type(val),
1841 regval_size(val),
1842 regval_data_p(val) );
1845 return len;
1848 /***********************************************************************
1849 Retrieve an array of strings containing subkeys. Memory should be
1850 released by the caller.
1851 ***********************************************************************/
1853 static int regdb_fetch_values_internal(struct db_context *db, const char* key,
1854 struct regval_ctr *values)
1856 char *keystr = NULL;
1857 TALLOC_CTX *ctx = talloc_stackframe();
1858 int ret = 0;
1859 TDB_DATA value;
1860 WERROR werr;
1862 DEBUG(10,("regdb_fetch_values: Looking for value of key [%s] \n", key));
1864 if (!regdb_key_exists(db, key)) {
1865 goto done;
1868 keystr = talloc_asprintf(ctx, "%s\\%s", REG_VALUE_PREFIX, key);
1869 if (!keystr) {
1870 goto done;
1873 werr = regval_ctr_set_seqnum(values, dbwrap_get_seqnum(db));
1874 W_ERROR_NOT_OK_GOTO_DONE(werr);
1876 value = regdb_fetch_key_internal(db, ctx, keystr);
1878 if (!value.dptr) {
1879 /* all keys have zero values by default */
1880 goto done;
1883 regdb_unpack_values(values, value.dptr, value.dsize);
1884 ret = regval_ctr_numvals(values);
1886 done:
1887 TALLOC_FREE(ctx);
1888 return ret;
1891 int regdb_fetch_values(const char* key, struct regval_ctr *values)
1893 return regdb_fetch_values_internal(regdb, key, values);
1896 static NTSTATUS regdb_store_values_internal(struct db_context *db,
1897 const char *key,
1898 struct regval_ctr *values)
1900 TDB_DATA old_data, data;
1901 char *keystr = NULL;
1902 TALLOC_CTX *ctx = talloc_stackframe();
1903 int len;
1904 NTSTATUS status;
1906 DEBUG(10,("regdb_store_values: Looking for value of key [%s] \n", key));
1908 if (!regdb_key_exists(db, key)) {
1909 status = NT_STATUS_NOT_FOUND;
1910 goto done;
1913 ZERO_STRUCT(data);
1915 len = regdb_pack_values(values, data.dptr, data.dsize);
1916 if (len <= 0) {
1917 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
1918 status = NT_STATUS_UNSUCCESSFUL;
1919 goto done;
1922 data.dptr = talloc_array(ctx, uint8, len);
1923 data.dsize = len;
1925 len = regdb_pack_values(values, data.dptr, data.dsize);
1927 SMB_ASSERT( len == data.dsize );
1929 keystr = talloc_asprintf(ctx, "%s\\%s", REG_VALUE_PREFIX, key );
1930 if (!keystr) {
1931 status = NT_STATUS_NO_MEMORY;
1932 goto done;
1934 keystr = normalize_reg_path(ctx, keystr);
1935 if (!keystr) {
1936 status = NT_STATUS_NO_MEMORY;
1937 goto done;
1940 status = dbwrap_fetch_bystring(db, ctx, keystr, &old_data);
1942 if (NT_STATUS_IS_OK(status)
1943 && (old_data.dptr != NULL)
1944 && (old_data.dsize == data.dsize)
1945 && (memcmp(old_data.dptr, data.dptr, data.dsize) == 0))
1947 status = NT_STATUS_OK;
1948 goto done;
1951 status = dbwrap_trans_store_bystring(db, keystr, data, TDB_REPLACE);
1953 done:
1954 TALLOC_FREE(ctx);
1955 return status;
1958 struct regdb_store_values_ctx {
1959 const char *key;
1960 struct regval_ctr *values;
1963 static NTSTATUS regdb_store_values_action(struct db_context *db,
1964 void *private_data)
1966 NTSTATUS status;
1967 struct regdb_store_values_ctx *ctx =
1968 (struct regdb_store_values_ctx *)private_data;
1970 status = regdb_store_values_internal(db, ctx->key, ctx->values);
1972 return status;
1975 bool regdb_store_values(const char *key, struct regval_ctr *values)
1977 WERROR werr;
1978 struct regdb_store_values_ctx ctx;
1980 ctx.key = key;
1981 ctx.values = values;
1983 werr = regdb_trans_do(regdb, regdb_store_values_action, &ctx);
1985 return W_ERROR_IS_OK(werr);
1988 static WERROR regdb_get_secdesc(TALLOC_CTX *mem_ctx, const char *key,
1989 struct security_descriptor **psecdesc)
1991 char *tdbkey;
1992 TDB_DATA data;
1993 NTSTATUS status;
1994 TALLOC_CTX *tmp_ctx = talloc_stackframe();
1995 WERROR err = WERR_OK;
1997 DEBUG(10, ("regdb_get_secdesc: Getting secdesc of key [%s]\n", key));
1999 if (!regdb_key_exists(regdb, key)) {
2000 err = WERR_BADFILE;
2001 goto done;
2004 tdbkey = talloc_asprintf(tmp_ctx, "%s\\%s", REG_SECDESC_PREFIX, key);
2005 if (tdbkey == NULL) {
2006 err = WERR_NOMEM;
2007 goto done;
2010 tdbkey = normalize_reg_path(tmp_ctx, tdbkey);
2011 if (tdbkey == NULL) {
2012 err = WERR_NOMEM;
2013 goto done;
2016 status = dbwrap_fetch_bystring(regdb, tmp_ctx, tdbkey, &data);
2017 if (!NT_STATUS_IS_OK(status)) {
2018 err = WERR_BADFILE;
2019 goto done;
2022 status = unmarshall_sec_desc(mem_ctx, (uint8 *)data.dptr, data.dsize,
2023 psecdesc);
2025 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MEMORY)) {
2026 err = WERR_NOMEM;
2027 } else if (!NT_STATUS_IS_OK(status)) {
2028 err = WERR_REG_CORRUPT;
2031 done:
2032 TALLOC_FREE(tmp_ctx);
2033 return err;
2036 struct regdb_set_secdesc_ctx {
2037 const char *key;
2038 struct security_descriptor *secdesc;
2041 static NTSTATUS regdb_set_secdesc_action(struct db_context *db,
2042 void *private_data)
2044 char *tdbkey;
2045 NTSTATUS status;
2046 TDB_DATA tdbdata;
2047 struct regdb_set_secdesc_ctx *ctx =
2048 (struct regdb_set_secdesc_ctx *)private_data;
2049 TALLOC_CTX *frame = talloc_stackframe();
2051 tdbkey = talloc_asprintf(frame, "%s\\%s", REG_SECDESC_PREFIX, ctx->key);
2052 if (tdbkey == NULL) {
2053 status = NT_STATUS_NO_MEMORY;
2054 goto done;
2057 tdbkey = normalize_reg_path(frame, tdbkey);
2058 if (tdbkey == NULL) {
2059 status = NT_STATUS_NO_MEMORY;
2060 goto done;
2063 if (ctx->secdesc == NULL) {
2064 /* assuming a delete */
2065 status = dbwrap_delete_bystring(db, tdbkey);
2066 goto done;
2069 status = marshall_sec_desc(frame, ctx->secdesc, &tdbdata.dptr,
2070 &tdbdata.dsize);
2071 if (!NT_STATUS_IS_OK(status)) {
2072 goto done;
2075 status = dbwrap_store_bystring(db, tdbkey, tdbdata, 0);
2077 done:
2078 TALLOC_FREE(frame);
2079 return status;
2082 static WERROR regdb_set_secdesc(const char *key,
2083 struct security_descriptor *secdesc)
2085 WERROR err;
2086 struct regdb_set_secdesc_ctx ctx;
2088 if (!regdb_key_exists(regdb, key)) {
2089 err = WERR_BADFILE;
2090 goto done;
2093 ctx.key = key;
2094 ctx.secdesc = secdesc;
2096 err = regdb_trans_do(regdb, regdb_set_secdesc_action, &ctx);
2098 done:
2099 return err;
2102 bool regdb_subkeys_need_update(struct regsubkey_ctr *subkeys)
2104 return (regdb_get_seqnum() != regsubkey_ctr_get_seqnum(subkeys));
2107 bool regdb_values_need_update(struct regval_ctr *values)
2109 return (regdb_get_seqnum() != regval_ctr_get_seqnum(values));
2113 * Table of function pointers for default access
2116 struct registry_ops regdb_ops = {
2117 .fetch_subkeys = regdb_fetch_keys,
2118 .fetch_values = regdb_fetch_values,
2119 .store_subkeys = regdb_store_keys,
2120 .store_values = regdb_store_values,
2121 .create_subkey = regdb_create_subkey,
2122 .delete_subkey = regdb_delete_subkey,
2123 .get_secdesc = regdb_get_secdesc,
2124 .set_secdesc = regdb_set_secdesc,
2125 .subkeys_need_update = regdb_subkeys_need_update,
2126 .values_need_update = regdb_values_need_update