s3-registry: fix upgrade code
[Samba/gebeck_regimport.git] / source3 / registry / reg_backend_db.c
blob7fa6249cecd070cb524549744171966eb2e4bb84
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_backend_db.h"
30 #include "reg_objects.h"
31 #include "nt_printing.h"
32 #include "util_tdb.h"
33 #include "dbwrap/dbwrap.h"
34 #include "dbwrap/dbwrap_open.h"
35 #include "../libcli/security/secdesc.h"
37 #undef DBGC_CLASS
38 #define DBGC_CLASS DBGC_REGISTRY
40 #define REGDB_VERSION_KEYNAME "INFO/version"
42 static struct db_context *regdb = NULL;
43 static int regdb_refcount;
45 static bool regdb_key_exists(struct db_context *db, const char *key);
46 static WERROR regdb_fetch_keys_internal(struct db_context *db, const char *key,
47 struct regsubkey_ctr *ctr);
48 static bool regdb_store_keys_internal(struct db_context *db, const char *key,
49 struct regsubkey_ctr *ctr);
50 static int regdb_fetch_values_internal(struct db_context *db, const char* key,
51 struct regval_ctr *values);
52 static NTSTATUS regdb_store_values_internal(struct db_context *db, const char *key,
53 struct regval_ctr *values);
54 static WERROR regdb_store_subkey_list(struct db_context *db, const char *parent,
55 const char *key);
57 static WERROR regdb_create_basekey(struct db_context *db, const char *key);
58 static WERROR regdb_create_subkey_internal(struct db_context *db,
59 const char *key,
60 const char *subkey);
63 struct regdb_trans_ctx {
64 NTSTATUS (*action)(struct db_context *, void *);
65 void *private_data;
68 static NTSTATUS regdb_trans_do_action(struct db_context *db, void *private_data)
70 NTSTATUS status;
71 int32_t version_id;
72 struct regdb_trans_ctx *ctx = (struct regdb_trans_ctx *)private_data;
74 status = dbwrap_fetch_int32(db, REGDB_VERSION_KEYNAME, &version_id);
76 if (!NT_STATUS_IS_OK(status)) {
77 DEBUG(0, ("ERROR: could not fetch registry db version: %s. "
78 "Denying access.\n", nt_errstr(status)));
79 return NT_STATUS_ACCESS_DENIED;
82 if (version_id != REGDB_CODE_VERSION) {
83 DEBUG(0, ("ERROR: changed registry version %d found while "
84 "trying to write to the registry. Version %d "
85 "expected. Denying access.\n",
86 version_id, REGDB_CODE_VERSION));
87 return NT_STATUS_ACCESS_DENIED;
90 status = ctx->action(db, ctx->private_data);
91 return status;
94 static WERROR regdb_trans_do(struct db_context *db,
95 NTSTATUS (*action)(struct db_context *, void *),
96 void *private_data)
98 NTSTATUS status;
99 struct regdb_trans_ctx ctx;
102 ctx.action = action;
103 ctx.private_data = private_data;
105 status = dbwrap_trans_do(db, regdb_trans_do_action, &ctx);
107 return ntstatus_to_werror(status);
110 /* List the deepest path into the registry. All part components will be created.*/
112 /* If you want to have a part of the path controlled by the tdb and part by
113 a virtual registry db (e.g. printing), then you have to list the deepest path.
114 For example,"HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion/Print"
115 allows the reg_db backend to handle everything up to
116 "HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion" and then we'll hook
117 the reg_printing backend onto the last component of the path (see
118 KEY_PRINTING_2K in include/rpc_reg.h) --jerry */
120 static const char *builtin_registry_paths[] = {
121 KEY_PRINTING_2K,
122 KEY_PRINTING_PORTS,
123 KEY_PRINTING,
124 KEY_PRINTING "\\Forms",
125 KEY_PRINTING "\\Printers",
126 KEY_PRINTING "\\Environments\\Windows NT x86\\Print Processors\\winprint",
127 KEY_SHARES,
128 KEY_EVENTLOG,
129 KEY_SMBCONF,
130 KEY_PERFLIB,
131 KEY_PERFLIB_009,
132 KEY_GROUP_POLICY,
133 KEY_SAMBA_GROUP_POLICY,
134 KEY_GP_MACHINE_POLICY,
135 KEY_GP_MACHINE_WIN_POLICY,
136 KEY_HKCU,
137 KEY_GP_USER_POLICY,
138 KEY_GP_USER_WIN_POLICY,
139 "HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon\\GPExtensions",
140 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Print\\Monitors",
141 KEY_PROD_OPTIONS,
142 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\DefaultUserConfiguration",
143 KEY_TCPIP_PARAMS,
144 KEY_NETLOGON_PARAMS,
145 KEY_HKU,
146 KEY_HKCR,
147 KEY_HKPD,
148 KEY_HKPT,
149 NULL };
151 struct builtin_regkey_value {
152 const char *path;
153 const char *valuename;
154 uint32 type;
155 union {
156 const char *string;
157 uint32 dw_value;
158 } data;
161 static struct builtin_regkey_value builtin_registry_values[] = {
162 { KEY_PRINTING_PORTS,
163 SAMBA_PRINTER_PORT_NAME, REG_SZ, { "" } },
164 { KEY_PRINTING_2K,
165 "DefaultSpoolDirectory", REG_SZ, { "C:\\Windows\\System32\\Spool\\Printers" } },
166 { KEY_EVENTLOG,
167 "DisplayName", REG_SZ, { "Event Log" } },
168 { KEY_EVENTLOG,
169 "ErrorControl", REG_DWORD, { (char*)0x00000001 } },
170 { NULL, NULL, 0, { NULL } }
173 static WERROR create_key_recursive(struct db_context *db,
174 char *path,
175 const char *subkey)
177 WERROR werr;
178 char *p;
180 if (subkey == NULL) {
181 return WERR_INVALID_PARAM;
184 if (path == NULL) {
185 return regdb_create_basekey(db, subkey);
188 p = strrchr_m(path, '\\');
190 if (p == NULL) {
191 werr = create_key_recursive(db, NULL, path);
192 } else {
193 *p = '\0';
194 werr = create_key_recursive(db, path, p+1);
195 *p = '\\';
198 if (!W_ERROR_IS_OK(werr)) {
199 goto done;
202 werr = regdb_create_subkey_internal(db, path, subkey);
204 done:
205 return werr;
209 * Initialize a key in the registry:
210 * create each component key of the specified path.
212 static WERROR init_registry_key_internal(struct db_context *db,
213 const char *add_path)
215 char *subkey, *key;
216 WERROR werr;
217 TALLOC_CTX *frame = talloc_stackframe();
219 if (add_path == NULL) {
220 werr = WERR_INVALID_PARAM;
221 goto done;
224 key = talloc_strdup(frame, add_path);
226 subkey = strrchr_m(key, '\\');
227 if (subkey == NULL) {
228 subkey = key;
229 key = NULL;
230 } else {
231 *subkey = '\0';
232 subkey++;
235 werr = create_key_recursive(db, key, subkey);
237 done:
238 talloc_free(frame);
239 return werr;
242 struct init_registry_key_context {
243 const char *add_path;
246 static NTSTATUS init_registry_key_action(struct db_context *db,
247 void *private_data)
249 struct init_registry_key_context *init_ctx =
250 (struct init_registry_key_context *)private_data;
252 return werror_to_ntstatus(init_registry_key_internal(
253 db, init_ctx->add_path));
257 * Initialize a key in the registry:
258 * create each component key of the specified path,
259 * wrapped in one db transaction.
261 WERROR init_registry_key(const char *add_path)
263 struct init_registry_key_context init_ctx;
265 if (regdb_key_exists(regdb, add_path)) {
266 return WERR_OK;
269 init_ctx.add_path = add_path;
271 return regdb_trans_do(regdb,
272 init_registry_key_action,
273 &init_ctx);
276 /***********************************************************************
277 Open the registry data in the tdb
278 ***********************************************************************/
280 static void regdb_ctr_add_value(struct regval_ctr *ctr,
281 struct builtin_regkey_value *value)
283 switch(value->type) {
284 case REG_DWORD:
285 regval_ctr_addvalue(ctr, value->valuename, REG_DWORD,
286 (uint8_t *)&value->data.dw_value,
287 sizeof(uint32));
288 break;
290 case REG_SZ:
291 regval_ctr_addvalue_sz(ctr, value->valuename,
292 value->data.string);
293 break;
295 default:
296 DEBUG(0, ("regdb_ctr_add_value: invalid value type in "
297 "registry values [%d]\n", value->type));
301 static NTSTATUS init_registry_data_action(struct db_context *db,
302 void *private_data)
304 NTSTATUS status;
305 TALLOC_CTX *frame = talloc_stackframe();
306 struct regval_ctr *values;
307 int i;
309 /* loop over all of the predefined paths and add each component */
311 for (i=0; builtin_registry_paths[i] != NULL; i++) {
312 if (regdb_key_exists(db, builtin_registry_paths[i])) {
313 continue;
315 status = werror_to_ntstatus(init_registry_key_internal(db,
316 builtin_registry_paths[i]));
317 if (!NT_STATUS_IS_OK(status)) {
318 goto done;
322 /* loop over all of the predefined values and add each component */
324 for (i=0; builtin_registry_values[i].path != NULL; i++) {
325 WERROR werr;
327 werr = regval_ctr_init(frame, &values);
328 if (!W_ERROR_IS_OK(werr)) {
329 status = werror_to_ntstatus(werr);
330 goto done;
333 regdb_fetch_values_internal(db,
334 builtin_registry_values[i].path,
335 values);
337 /* preserve existing values across restarts. Only add new ones */
339 if (!regval_ctr_key_exists(values,
340 builtin_registry_values[i].valuename))
342 regdb_ctr_add_value(values,
343 &builtin_registry_values[i]);
344 status = regdb_store_values_internal(db,
345 builtin_registry_values[i].path,
346 values);
347 if (!NT_STATUS_IS_OK(status)) {
348 goto done;
351 TALLOC_FREE(values);
354 status = NT_STATUS_OK;
356 done:
358 TALLOC_FREE(frame);
359 return status;
362 WERROR init_registry_data(void)
364 WERROR werr;
365 TALLOC_CTX *frame = talloc_stackframe();
366 struct regval_ctr *values;
367 int i;
370 * First, check for the existence of the needed keys and values.
371 * If all do already exist, we can save the writes.
373 for (i=0; builtin_registry_paths[i] != NULL; i++) {
374 if (!regdb_key_exists(regdb, builtin_registry_paths[i])) {
375 goto do_init;
379 for (i=0; builtin_registry_values[i].path != NULL; i++) {
380 werr = regval_ctr_init(frame, &values);
381 W_ERROR_NOT_OK_GOTO_DONE(werr);
383 regdb_fetch_values_internal(regdb,
384 builtin_registry_values[i].path,
385 values);
386 if (!regval_ctr_key_exists(values,
387 builtin_registry_values[i].valuename))
389 TALLOC_FREE(values);
390 goto do_init;
393 TALLOC_FREE(values);
396 werr = WERR_OK;
397 goto done;
399 do_init:
402 * There are potentially quite a few store operations which are all
403 * indiviually wrapped in tdb transactions. Wrapping them in a single
404 * transaction gives just a single transaction_commit() to actually do
405 * its fsync()s. See tdb/common/transaction.c for info about nested
406 * transaction behaviour.
409 werr = regdb_trans_do(regdb,
410 init_registry_data_action,
411 NULL);
413 done:
414 TALLOC_FREE(frame);
415 return werr;
418 static int regdb_normalize_keynames_fn(struct db_record *rec,
419 void *private_data)
421 TALLOC_CTX *mem_ctx = talloc_tos();
422 const char *keyname;
423 NTSTATUS status;
424 TDB_DATA key;
425 TDB_DATA value;
426 struct db_context *db = (struct db_context *)private_data;
428 key = dbwrap_record_get_key(rec);
429 if (key.dptr == NULL || key.dsize == 0) {
430 return 0;
433 value = dbwrap_record_get_value(rec);
435 if (db == NULL) {
436 DEBUG(0, ("regdb_normalize_keynames_fn: ERROR: "
437 "NULL db context handed in via private_data\n"));
438 return 1;
441 if (strncmp((const char *)key.dptr, REGDB_VERSION_KEYNAME,
442 strlen(REGDB_VERSION_KEYNAME)) == 0)
444 return 0;
447 keyname = strchr((const char *)key.dptr, '/');
448 if (keyname) {
449 keyname = talloc_string_sub(mem_ctx,
450 (const char *)key.dptr,
451 "/",
452 "\\");
454 DEBUG(2, ("regdb_normalize_keynames_fn: Convert %s to %s\n",
455 (const char *)key.dptr,
456 keyname));
458 /* Delete the original record and store the normalized key */
459 status = dbwrap_record_delete(rec);
460 if (!NT_STATUS_IS_OK(status)) {
461 DEBUG(0,("regdb_normalize_keynames_fn: "
462 "tdb_delete for [%s] failed!\n",
463 (const char *)key.dptr));
464 return 1;
467 status = dbwrap_store_bystring(db, keyname, value, TDB_REPLACE);
468 if (!NT_STATUS_IS_OK(status)) {
469 DEBUG(0,("regdb_normalize_keynames_fn: "
470 "failed to store new record for [%s]!\n",
471 keyname));
472 return 1;
476 return 0;
479 static WERROR regdb_store_regdb_version(struct db_context *db, uint32_t version)
481 NTSTATUS status;
482 if (db == NULL) {
483 return WERR_CAN_NOT_COMPLETE;
486 status = dbwrap_trans_store_int32(db, REGDB_VERSION_KEYNAME, version);
487 if (!NT_STATUS_IS_OK(status)) {
488 DEBUG(1, ("regdb_store_regdb_version: error storing %s = %d: %s\n",
489 REGDB_VERSION_KEYNAME, version, nt_errstr(status)));
490 return ntstatus_to_werror(status);
491 } else {
492 DEBUG(10, ("regdb_store_regdb_version: stored %s = %d\n",
493 REGDB_VERSION_KEYNAME, version));
494 return WERR_OK;
498 static WERROR regdb_upgrade_v1_to_v2(struct db_context *db)
500 TALLOC_CTX *mem_ctx;
501 NTSTATUS status;
502 WERROR werr;
504 mem_ctx = talloc_stackframe();
506 status = dbwrap_traverse(db, regdb_normalize_keynames_fn, db, NULL);
507 if (!NT_STATUS_IS_OK(status)) {
508 werr = WERR_REG_IO_FAILURE;
509 goto done;
512 werr = regdb_store_regdb_version(db, REGDB_VERSION_V2);
514 done:
515 talloc_free(mem_ctx);
516 return werr;
519 static int regdb_upgrade_v2_to_v3_fn(struct db_record *rec, void *private_data)
521 const char *keyname;
522 fstring subkeyname;
523 NTSTATUS status;
524 WERROR werr;
525 uint8_t *buf;
526 uint32_t buflen, len;
527 uint32_t num_items;
528 uint32_t i;
529 TDB_DATA key;
530 TDB_DATA value;
531 struct db_context *db = (struct db_context *)private_data;
533 key = dbwrap_record_get_key(rec);
534 if (key.dptr == NULL || key.dsize == 0) {
535 return 0;
538 if (db == NULL) {
539 DEBUG(0, ("regdb_upgrade_v2_to_v3_fn: ERROR: "
540 "NULL db context handed in via private_data\n"));
541 return 1;
544 keyname = (const char *)key.dptr;
546 if (strncmp(keyname, REGDB_VERSION_KEYNAME,
547 strlen(REGDB_VERSION_KEYNAME)) == 0)
549 return 0;
552 if (strncmp(keyname, REG_SORTED_SUBKEYS_PREFIX,
553 strlen(REG_SORTED_SUBKEYS_PREFIX)) == 0)
555 /* Delete the deprecated sorted subkeys cache. */
557 DEBUG(10, ("regdb_upgrade_v2_to_v3: deleting [%s]\n", keyname));
559 status = dbwrap_record_delete(rec);
560 if (!NT_STATUS_IS_OK(status)) {
561 DEBUG(0, ("regdb_upgrade_v2_to_v3: tdb_delete for [%s] "
562 "failed!\n", keyname));
563 return 1;
566 return 0;
569 if (strncmp(keyname, REG_VALUE_PREFIX, strlen(REG_VALUE_PREFIX)) == 0) {
570 DEBUG(10, ("regdb_upgrade_v2_to_v3: skipping [%s]\n", keyname));
571 return 0;
574 if (strncmp(keyname, REG_SECDESC_PREFIX,
575 strlen(REG_SECDESC_PREFIX)) == 0)
577 DEBUG(10, ("regdb_upgrade_v2_to_v3: skipping [%s]\n", keyname));
578 return 0;
582 * Found a regular subkey list record.
583 * Walk the list and create the list record for those
584 * subkeys that don't already have one.
586 DEBUG(10, ("regdb_upgrade_v2_to_v3: scanning subkey list of [%s]\n",
587 keyname));
589 value = dbwrap_record_get_value(rec);
590 buf = value.dptr;
591 buflen = value.dsize;
593 len = tdb_unpack(buf, buflen, "d", &num_items);
594 if (len == (uint32_t)-1) {
595 /* invalid or empty - skip */
596 return 0;
599 for (i=0; i<num_items; i++) {
600 len += tdb_unpack(buf+len, buflen-len, "f", subkeyname);
601 DEBUG(10, ("regdb_upgrade_v2_to_v3: "
602 "writing subkey list for [%s\\%s]\n",
603 keyname, subkeyname));
604 werr = regdb_store_subkey_list(db, keyname, subkeyname);
605 if (!W_ERROR_IS_OK(werr)) {
606 return 1;
610 return 0;
613 static WERROR regdb_upgrade_v2_to_v3(struct db_context *db)
615 NTSTATUS status;
616 WERROR werr;
618 status = dbwrap_traverse(db, regdb_upgrade_v2_to_v3_fn, db, NULL);
619 if (!NT_STATUS_IS_OK(status)) {
620 werr = WERR_REG_IO_FAILURE;
621 goto done;
624 werr = regdb_store_regdb_version(db, REGDB_VERSION_V3);
626 done:
627 return werr;
630 /***********************************************************************
631 Open the registry database
632 ***********************************************************************/
634 WERROR regdb_init(void)
636 int32_t vers_id;
637 WERROR werr;
638 NTSTATUS status;
640 if (regdb) {
641 DEBUG(10, ("regdb_init: incrementing refcount (%d->%d)\n",
642 regdb_refcount, regdb_refcount+1));
643 regdb_refcount++;
644 return WERR_OK;
647 regdb = db_open(NULL, state_path("registry.tdb"), 0,
648 REG_TDB_FLAGS, O_RDWR, 0600);
649 if (!regdb) {
650 regdb = db_open(NULL, state_path("registry.tdb"), 0,
651 REG_TDB_FLAGS, O_RDWR|O_CREAT, 0600);
652 if (!regdb) {
653 werr = ntstatus_to_werror(map_nt_error_from_unix(errno));
654 DEBUG(1,("regdb_init: Failed to open registry %s (%s)\n",
655 state_path("registry.tdb"), strerror(errno) ));
656 return werr;
659 DEBUG(10,("regdb_init: Successfully created registry tdb\n"));
662 regdb_refcount = 1;
663 DEBUG(10, ("regdb_init: registry db openend. refcount reset (%d)\n",
664 regdb_refcount));
666 status = dbwrap_fetch_int32(regdb, REGDB_VERSION_KEYNAME, &vers_id);
667 if (!NT_STATUS_IS_OK(status)) {
668 DEBUG(10, ("regdb_init: registry version uninitialized "
669 "(got %d), initializing to version %d\n",
670 vers_id, REGDB_VERSION_V1));
673 * There was a regdb format version prior to version 1
674 * which did not store a INFO/version key. The format
675 * of this version was identical to version 1 except for
676 * the lack of the sorted subkey cache records.
677 * Since these are disposable, we can safely assume version
678 * 1 if no INFO/version key is found and run the db through
679 * the whole chain of upgrade. If the database was not
680 * initialized, this does not harm. If it was the unversioned
681 * version ("0"), then it do the right thing with the records.
683 werr = regdb_store_regdb_version(regdb, REGDB_VERSION_V1);
684 if (!W_ERROR_IS_OK(werr)) {
685 return werr;
687 vers_id = REGDB_VERSION_V1;
690 if (vers_id > REGDB_CODE_VERSION || vers_id == 0) {
691 DEBUG(0, ("regdb_init: unknown registry version %d "
692 "(code version = %d), refusing initialization\n",
693 vers_id, REGDB_CODE_VERSION));
694 return WERR_CAN_NOT_COMPLETE;
697 if (dbwrap_transaction_start(regdb) != 0) {
698 return WERR_REG_IO_FAILURE;
701 if (vers_id == REGDB_VERSION_V1) {
702 DEBUG(10, ("regdb_init: upgrading registry from version %d "
703 "to %d\n", REGDB_VERSION_V1, REGDB_VERSION_V2));
705 werr = regdb_upgrade_v1_to_v2(regdb);
706 if (!W_ERROR_IS_OK(werr)) {
707 dbwrap_transaction_cancel(regdb);
708 return werr;
711 vers_id = REGDB_VERSION_V2;
714 if (vers_id == REGDB_VERSION_V2) {
715 DEBUG(10, ("regdb_init: upgrading registry from version %d "
716 "to %d\n", REGDB_VERSION_V2, REGDB_VERSION_V3));
718 werr = regdb_upgrade_v2_to_v3(regdb);
719 if (!W_ERROR_IS_OK(werr)) {
720 dbwrap_transaction_cancel(regdb);
721 return werr;
724 vers_id = REGDB_VERSION_V3;
727 /* future upgrade code should go here */
729 if (dbwrap_transaction_commit(regdb) != 0) {
730 return WERR_REG_IO_FAILURE;
733 return WERR_OK;
736 /***********************************************************************
737 Open the registry. Must already have been initialized by regdb_init()
738 ***********************************************************************/
740 WERROR regdb_open( void )
742 WERROR result = WERR_OK;
744 if ( regdb ) {
745 DEBUG(10, ("regdb_open: incrementing refcount (%d->%d)\n",
746 regdb_refcount, regdb_refcount+1));
747 regdb_refcount++;
748 return WERR_OK;
751 become_root();
753 regdb = db_open(NULL, state_path("registry.tdb"), 0,
754 REG_TDB_FLAGS, O_RDWR, 0600);
755 if ( !regdb ) {
756 result = ntstatus_to_werror( map_nt_error_from_unix( errno ) );
757 DEBUG(0,("regdb_open: Failed to open %s! (%s)\n",
758 state_path("registry.tdb"), strerror(errno) ));
761 unbecome_root();
763 regdb_refcount = 1;
764 DEBUG(10, ("regdb_open: registry db opened. refcount reset (%d)\n",
765 regdb_refcount));
767 return result;
770 /***********************************************************************
771 ***********************************************************************/
773 int regdb_close( void )
775 if (regdb_refcount == 0) {
776 return 0;
779 regdb_refcount--;
781 DEBUG(10, ("regdb_close: decrementing refcount (%d->%d)\n",
782 regdb_refcount+1, regdb_refcount));
784 if ( regdb_refcount > 0 )
785 return 0;
787 SMB_ASSERT( regdb_refcount >= 0 );
789 TALLOC_FREE(regdb);
790 return 0;
793 WERROR regdb_transaction_start(void)
795 return (dbwrap_transaction_start(regdb) == 0) ?
796 WERR_OK : WERR_REG_IO_FAILURE;
799 WERROR regdb_transaction_commit(void)
801 return (dbwrap_transaction_commit(regdb) == 0) ?
802 WERR_OK : WERR_REG_IO_FAILURE;
805 WERROR regdb_transaction_cancel(void)
807 return (dbwrap_transaction_cancel(regdb) == 0) ?
808 WERR_OK : WERR_REG_IO_FAILURE;
811 /***********************************************************************
812 return the tdb sequence number of the registry tdb.
813 this is an indicator for the content of the registry
814 having changed. it will change upon regdb_init, too, though.
815 ***********************************************************************/
816 int regdb_get_seqnum(void)
818 return dbwrap_get_seqnum(regdb);
822 static WERROR regdb_delete_key_with_prefix(struct db_context *db,
823 const char *keyname,
824 const char *prefix)
826 char *path;
827 WERROR werr = WERR_NOMEM;
828 TALLOC_CTX *mem_ctx = talloc_stackframe();
830 if (keyname == NULL) {
831 werr = WERR_INVALID_PARAM;
832 goto done;
835 if (prefix == NULL) {
836 path = discard_const_p(char, keyname);
837 } else {
838 path = talloc_asprintf(mem_ctx, "%s\\%s", prefix, keyname);
839 if (path == NULL) {
840 goto done;
844 path = normalize_reg_path(mem_ctx, path);
845 if (path == NULL) {
846 goto done;
849 werr = ntstatus_to_werror(dbwrap_delete_bystring(db, path));
851 /* treat "not found" as ok */
852 if (W_ERROR_EQUAL(werr, WERR_NOT_FOUND)) {
853 werr = WERR_OK;
856 done:
857 talloc_free(mem_ctx);
858 return werr;
862 static WERROR regdb_delete_values(struct db_context *db, const char *keyname)
864 return regdb_delete_key_with_prefix(db, keyname, REG_VALUE_PREFIX);
867 static WERROR regdb_delete_secdesc(struct db_context *db, const char *keyname)
869 return regdb_delete_key_with_prefix(db, keyname, REG_SECDESC_PREFIX);
872 static WERROR regdb_delete_subkeylist(struct db_context *db, const char *keyname)
874 return regdb_delete_key_with_prefix(db, keyname, NULL);
878 static WERROR regdb_delete_key_lists(struct db_context *db, const char *keyname)
880 WERROR werr;
882 werr = regdb_delete_values(db, keyname);
883 if (!W_ERROR_IS_OK(werr)) {
884 DEBUG(1, (__location__ " Deleting %s\\%s failed: %s\n",
885 REG_VALUE_PREFIX, keyname, win_errstr(werr)));
886 goto done;
889 werr = regdb_delete_secdesc(db, keyname);
890 if (!W_ERROR_IS_OK(werr)) {
891 DEBUG(1, (__location__ " Deleting %s\\%s failed: %s\n",
892 REG_SECDESC_PREFIX, keyname, win_errstr(werr)));
893 goto done;
896 werr = regdb_delete_subkeylist(db, keyname);
897 if (!W_ERROR_IS_OK(werr)) {
898 DEBUG(1, (__location__ " Deleting %s failed: %s\n",
899 keyname, win_errstr(werr)));
900 goto done;
903 done:
904 return werr;
907 /***********************************************************************
908 Add subkey strings to the registry tdb under a defined key
909 fmt is the same format as tdb_pack except this function only supports
910 fstrings
911 ***********************************************************************/
913 static WERROR regdb_store_keys_internal2(struct db_context *db,
914 const char *key,
915 struct regsubkey_ctr *ctr)
917 TDB_DATA dbuf;
918 uint8 *buffer = NULL;
919 int i = 0;
920 uint32 len, buflen;
921 uint32 num_subkeys = regsubkey_ctr_numkeys(ctr);
922 char *keyname = NULL;
923 TALLOC_CTX *ctx = talloc_stackframe();
924 WERROR werr;
926 if (!key) {
927 werr = WERR_INVALID_PARAM;
928 goto done;
931 keyname = talloc_strdup(ctx, key);
932 if (!keyname) {
933 werr = WERR_NOMEM;
934 goto done;
937 keyname = normalize_reg_path(ctx, keyname);
938 if (!keyname) {
939 werr = WERR_NOMEM;
940 goto done;
943 /* allocate some initial memory */
945 buffer = (uint8 *)SMB_MALLOC(1024);
946 if (buffer == NULL) {
947 werr = WERR_NOMEM;
948 goto done;
950 buflen = 1024;
951 len = 0;
953 /* store the number of subkeys */
955 len += tdb_pack(buffer+len, buflen-len, "d", num_subkeys);
957 /* pack all the strings */
959 for (i=0; i<num_subkeys; i++) {
960 size_t thistime;
962 thistime = tdb_pack(buffer+len, buflen-len, "f",
963 regsubkey_ctr_specific_key(ctr, i));
964 if (len+thistime > buflen) {
965 size_t thistime2;
967 * tdb_pack hasn't done anything because of the short
968 * buffer, allocate extra space.
970 buffer = SMB_REALLOC_ARRAY(buffer, uint8_t,
971 (len+thistime)*2);
972 if(buffer == NULL) {
973 DEBUG(0, ("regdb_store_keys: Failed to realloc "
974 "memory of size [%u]\n",
975 (unsigned int)(len+thistime)*2));
976 werr = WERR_NOMEM;
977 goto done;
979 buflen = (len+thistime)*2;
980 thistime2 = tdb_pack(
981 buffer+len, buflen-len, "f",
982 regsubkey_ctr_specific_key(ctr, i));
983 if (thistime2 != thistime) {
984 DEBUG(0, ("tdb_pack failed\n"));
985 werr = WERR_CAN_NOT_COMPLETE;
986 goto done;
989 len += thistime;
992 /* finally write out the data */
994 dbuf.dptr = buffer;
995 dbuf.dsize = len;
996 werr = ntstatus_to_werror(dbwrap_store_bystring(db, keyname, dbuf,
997 TDB_REPLACE));
999 done:
1000 TALLOC_FREE(ctx);
1001 SAFE_FREE(buffer);
1002 return werr;
1006 * Utility function to store a new empty list of
1007 * subkeys of given key specified as parent and subkey name
1008 * (thereby creating the key).
1009 * If the parent keyname is NULL, then the "subkey" is
1010 * interpreted as a base key.
1011 * If the subkey list does already exist, it is not modified.
1013 * Must be called from within a transaction.
1015 static WERROR regdb_store_subkey_list(struct db_context *db, const char *parent,
1016 const char *key)
1018 WERROR werr;
1019 char *path = NULL;
1020 struct regsubkey_ctr *subkeys = NULL;
1021 TALLOC_CTX *frame = talloc_stackframe();
1023 if (parent == NULL) {
1024 path = talloc_strdup(frame, key);
1025 } else {
1026 path = talloc_asprintf(frame, "%s\\%s", parent, key);
1028 if (!path) {
1029 werr = WERR_NOMEM;
1030 goto done;
1033 werr = regsubkey_ctr_init(frame, &subkeys);
1034 W_ERROR_NOT_OK_GOTO_DONE(werr);
1036 werr = regdb_fetch_keys_internal(db, path, subkeys);
1037 if (W_ERROR_IS_OK(werr)) {
1038 /* subkey list exists already - don't modify */
1039 goto done;
1042 werr = regsubkey_ctr_reinit(subkeys);
1043 W_ERROR_NOT_OK_GOTO_DONE(werr);
1045 /* create a record with 0 subkeys */
1046 werr = regdb_store_keys_internal2(db, path, subkeys);
1047 if (!W_ERROR_IS_OK(werr)) {
1048 DEBUG(0, ("regdb_store_keys: Failed to store new record for "
1049 "key [%s]: %s\n", path, win_errstr(werr)));
1050 goto done;
1053 done:
1054 talloc_free(frame);
1055 return werr;
1058 /***********************************************************************
1059 Store the new subkey record and create any child key records that
1060 do not currently exist
1061 ***********************************************************************/
1063 struct regdb_store_keys_context {
1064 const char *key;
1065 struct regsubkey_ctr *ctr;
1068 static NTSTATUS regdb_store_keys_action(struct db_context *db,
1069 void *private_data)
1071 struct regdb_store_keys_context *store_ctx;
1072 WERROR werr;
1073 int num_subkeys, i;
1074 char *path = NULL;
1075 struct regsubkey_ctr *old_subkeys = NULL;
1076 char *oldkeyname = NULL;
1077 TALLOC_CTX *mem_ctx = talloc_stackframe();
1079 store_ctx = (struct regdb_store_keys_context *)private_data;
1082 * Re-fetch the old keys inside the transaction
1085 werr = regsubkey_ctr_init(mem_ctx, &old_subkeys);
1086 W_ERROR_NOT_OK_GOTO_DONE(werr);
1088 werr = regdb_fetch_keys_internal(db, store_ctx->key, old_subkeys);
1089 if (!W_ERROR_IS_OK(werr) &&
1090 !W_ERROR_EQUAL(werr, WERR_NOT_FOUND))
1092 goto done;
1096 * Make the store operation as safe as possible without transactions:
1098 * (1) For each subkey removed from ctr compared with old_subkeys:
1100 * (a) First delete the value db entry.
1102 * (b) Next delete the secdesc db record.
1104 * (c) Then delete the subkey list entry.
1106 * (2) Now write the list of subkeys of the parent key,
1107 * deleting removed entries and adding new ones.
1109 * (3) Finally create the subkey list entries for the added keys.
1111 * This way if we crash half-way in between deleting the subkeys
1112 * and storing the parent's list of subkeys, no old data can pop up
1113 * out of the blue when re-adding keys later on.
1116 /* (1) delete removed keys' lists (values/secdesc/subkeys) */
1118 num_subkeys = regsubkey_ctr_numkeys(old_subkeys);
1119 for (i=0; i<num_subkeys; i++) {
1120 oldkeyname = regsubkey_ctr_specific_key(old_subkeys, i);
1122 if (regsubkey_ctr_key_exists(store_ctx->ctr, oldkeyname)) {
1124 * It's still around, don't delete
1126 continue;
1129 path = talloc_asprintf(mem_ctx, "%s\\%s", store_ctx->key,
1130 oldkeyname);
1131 if (!path) {
1132 werr = WERR_NOMEM;
1133 goto done;
1136 werr = regdb_delete_key_lists(db, path);
1137 W_ERROR_NOT_OK_GOTO_DONE(werr);
1139 TALLOC_FREE(path);
1142 TALLOC_FREE(old_subkeys);
1144 /* (2) store the subkey list for the parent */
1146 werr = regdb_store_keys_internal2(db, store_ctx->key, store_ctx->ctr);
1147 if (!W_ERROR_IS_OK(werr)) {
1148 DEBUG(0,("regdb_store_keys: Failed to store new subkey list "
1149 "for parent [%s]: %s\n", store_ctx->key,
1150 win_errstr(werr)));
1151 goto done;
1154 /* (3) now create records for any subkeys that don't already exist */
1156 num_subkeys = regsubkey_ctr_numkeys(store_ctx->ctr);
1158 for (i=0; i<num_subkeys; i++) {
1159 const char *subkey;
1161 subkey = regsubkey_ctr_specific_key(store_ctx->ctr, i);
1163 werr = regdb_store_subkey_list(db, store_ctx->key, subkey);
1164 W_ERROR_NOT_OK_GOTO_DONE(werr);
1167 werr = WERR_OK;
1169 done:
1170 talloc_free(mem_ctx);
1171 return werror_to_ntstatus(werr);
1174 static bool regdb_store_keys_internal(struct db_context *db, const char *key,
1175 struct regsubkey_ctr *ctr)
1177 int num_subkeys, old_num_subkeys, i;
1178 struct regsubkey_ctr *old_subkeys = NULL;
1179 TALLOC_CTX *ctx = talloc_stackframe();
1180 WERROR werr;
1181 bool ret = false;
1182 struct regdb_store_keys_context store_ctx;
1184 if (!regdb_key_exists(db, key)) {
1185 goto done;
1189 * fetch a list of the old subkeys so we can determine if anything has
1190 * changed
1193 werr = regsubkey_ctr_init(ctx, &old_subkeys);
1194 if (!W_ERROR_IS_OK(werr)) {
1195 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
1196 goto done;
1199 werr = regdb_fetch_keys_internal(db, key, old_subkeys);
1200 if (!W_ERROR_IS_OK(werr) &&
1201 !W_ERROR_EQUAL(werr, WERR_NOT_FOUND))
1203 goto done;
1206 num_subkeys = regsubkey_ctr_numkeys(ctr);
1207 old_num_subkeys = regsubkey_ctr_numkeys(old_subkeys);
1208 if ((num_subkeys && old_num_subkeys) &&
1209 (num_subkeys == old_num_subkeys)) {
1211 for (i = 0; i < num_subkeys; i++) {
1212 if (strcmp(regsubkey_ctr_specific_key(ctr, i),
1213 regsubkey_ctr_specific_key(old_subkeys, i))
1214 != 0)
1216 break;
1219 if (i == num_subkeys) {
1221 * Nothing changed, no point to even start a tdb
1222 * transaction
1225 ret = true;
1226 goto done;
1230 TALLOC_FREE(old_subkeys);
1232 store_ctx.key = key;
1233 store_ctx.ctr = ctr;
1235 werr = regdb_trans_do(db,
1236 regdb_store_keys_action,
1237 &store_ctx);
1239 ret = W_ERROR_IS_OK(werr);
1241 done:
1242 TALLOC_FREE(ctx);
1244 return ret;
1247 bool regdb_store_keys(const char *key, struct regsubkey_ctr *ctr)
1249 return regdb_store_keys_internal(regdb, key, ctr);
1253 * create a subkey of a given key
1256 struct regdb_create_subkey_context {
1257 const char *key;
1258 const char *subkey;
1261 static NTSTATUS regdb_create_subkey_action(struct db_context *db,
1262 void *private_data)
1264 WERROR werr;
1265 struct regdb_create_subkey_context *create_ctx;
1266 struct regsubkey_ctr *subkeys;
1267 TALLOC_CTX *mem_ctx = talloc_stackframe();
1269 create_ctx = (struct regdb_create_subkey_context *)private_data;
1271 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
1272 W_ERROR_NOT_OK_GOTO_DONE(werr);
1274 werr = regdb_fetch_keys_internal(db, create_ctx->key, subkeys);
1275 W_ERROR_NOT_OK_GOTO_DONE(werr);
1277 werr = regsubkey_ctr_addkey(subkeys, create_ctx->subkey);
1278 W_ERROR_NOT_OK_GOTO_DONE(werr);
1280 werr = regdb_store_keys_internal2(db, create_ctx->key, subkeys);
1281 if (!W_ERROR_IS_OK(werr)) {
1282 DEBUG(0, (__location__ " failed to store new subkey list for "
1283 "parent key %s: %s\n", create_ctx->key,
1284 win_errstr(werr)));
1287 werr = regdb_store_subkey_list(db, create_ctx->key, create_ctx->subkey);
1289 done:
1290 talloc_free(mem_ctx);
1291 return werror_to_ntstatus(werr);
1294 static WERROR regdb_create_subkey_internal(struct db_context *db,
1295 const char *key,
1296 const char *subkey)
1298 WERROR werr;
1299 struct regsubkey_ctr *subkeys;
1300 TALLOC_CTX *mem_ctx = talloc_stackframe();
1301 struct regdb_create_subkey_context create_ctx;
1303 if (!regdb_key_exists(db, key)) {
1304 werr = WERR_NOT_FOUND;
1305 goto done;
1308 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
1309 W_ERROR_NOT_OK_GOTO_DONE(werr);
1311 werr = regdb_fetch_keys_internal(db, key, subkeys);
1312 W_ERROR_NOT_OK_GOTO_DONE(werr);
1314 if (regsubkey_ctr_key_exists(subkeys, subkey)) {
1315 char *newkey;
1317 newkey = talloc_asprintf(mem_ctx, "%s\\%s", key, subkey);
1318 if (newkey == NULL) {
1319 werr = WERR_NOMEM;
1320 goto done;
1323 if (regdb_key_exists(db, newkey)) {
1324 werr = WERR_OK;
1325 goto done;
1329 talloc_free(subkeys);
1331 create_ctx.key = key;
1332 create_ctx.subkey = subkey;
1334 werr = regdb_trans_do(db,
1335 regdb_create_subkey_action,
1336 &create_ctx);
1338 done:
1339 talloc_free(mem_ctx);
1340 return werr;
1343 static WERROR regdb_create_subkey(const char *key, const char *subkey)
1345 return regdb_create_subkey_internal(regdb, key, subkey);
1349 * create a base key
1352 struct regdb_create_basekey_context {
1353 const char *key;
1356 static NTSTATUS regdb_create_basekey_action(struct db_context *db,
1357 void *private_data)
1359 WERROR werr;
1360 struct regdb_create_basekey_context *create_ctx;
1362 create_ctx = (struct regdb_create_basekey_context *)private_data;
1364 werr = regdb_store_subkey_list(db, NULL, create_ctx->key);
1366 return werror_to_ntstatus(werr);
1369 static WERROR regdb_create_basekey(struct db_context *db, const char *key)
1371 WERROR werr;
1372 struct regdb_create_subkey_context create_ctx;
1374 create_ctx.key = key;
1376 werr = regdb_trans_do(db,
1377 regdb_create_basekey_action,
1378 &create_ctx);
1380 return werr;
1384 * create a subkey of a given key
1387 struct regdb_delete_subkey_context {
1388 const char *key;
1389 const char *subkey;
1390 const char *path;
1391 bool lazy;
1394 static NTSTATUS regdb_delete_subkey_action(struct db_context *db,
1395 void *private_data)
1397 WERROR werr;
1398 struct regdb_delete_subkey_context *delete_ctx;
1399 struct regsubkey_ctr *subkeys;
1400 TALLOC_CTX *mem_ctx = talloc_stackframe();
1402 delete_ctx = (struct regdb_delete_subkey_context *)private_data;
1404 werr = regdb_delete_key_lists(db, delete_ctx->path);
1405 W_ERROR_NOT_OK_GOTO_DONE(werr);
1407 if (delete_ctx->lazy) {
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, delete_ctx->key, subkeys);
1415 W_ERROR_NOT_OK_GOTO_DONE(werr);
1417 werr = regsubkey_ctr_delkey(subkeys, delete_ctx->subkey);
1418 W_ERROR_NOT_OK_GOTO_DONE(werr);
1420 werr = regdb_store_keys_internal2(db, delete_ctx->key, subkeys);
1421 if (!W_ERROR_IS_OK(werr)) {
1422 DEBUG(0, (__location__ " failed to store new subkey_list for "
1423 "parent key %s: %s\n", delete_ctx->key,
1424 win_errstr(werr)));
1427 done:
1428 talloc_free(mem_ctx);
1429 return werror_to_ntstatus(werr);
1432 static WERROR regdb_delete_subkey(const char *key, const char *subkey, bool lazy)
1434 WERROR werr;
1435 char *path;
1436 struct regdb_delete_subkey_context delete_ctx;
1437 TALLOC_CTX *mem_ctx = talloc_stackframe();
1439 if (!regdb_key_exists(regdb, key)) {
1440 werr = WERR_NOT_FOUND;
1441 goto done;
1444 path = talloc_asprintf(mem_ctx, "%s\\%s", key, subkey);
1445 if (path == NULL) {
1446 werr = WERR_NOMEM;
1447 goto done;
1450 if (!regdb_key_exists(regdb, path)) {
1451 werr = WERR_OK;
1452 goto done;
1455 delete_ctx.key = key;
1456 delete_ctx.subkey = subkey;
1457 delete_ctx.path = path;
1458 delete_ctx.lazy = lazy;
1460 werr = regdb_trans_do(regdb,
1461 regdb_delete_subkey_action,
1462 &delete_ctx);
1464 done:
1465 talloc_free(mem_ctx);
1466 return werr;
1469 static TDB_DATA regdb_fetch_key_internal(struct db_context *db,
1470 TALLOC_CTX *mem_ctx, const char *key)
1472 char *path = NULL;
1473 TDB_DATA data;
1474 NTSTATUS status;
1476 path = normalize_reg_path(mem_ctx, key);
1477 if (!path) {
1478 return make_tdb_data(NULL, 0);
1481 status = dbwrap_fetch_bystring(db, mem_ctx, path, &data);
1482 if (!NT_STATUS_IS_OK(status)) {
1483 data = tdb_null;
1486 TALLOC_FREE(path);
1487 return data;
1492 * Check for the existence of a key.
1494 * Existence of a key is authoritatively defined by
1495 * the existence of the record that contains the list
1496 * of its subkeys.
1498 * Return false, if the record does not match the correct
1499 * structure of an initial 4-byte counter and then a
1500 * list of the corresponding number of zero-terminated
1501 * strings.
1503 static bool regdb_key_exists(struct db_context *db, const char *key)
1505 TALLOC_CTX *mem_ctx = talloc_stackframe();
1506 TDB_DATA value;
1507 bool ret = false;
1508 char *path;
1509 uint32_t buflen;
1510 const char *buf;
1511 uint32_t num_items, i;
1512 int32_t len;
1514 if (key == NULL) {
1515 goto done;
1518 path = normalize_reg_path(mem_ctx, key);
1519 if (path == NULL) {
1520 DEBUG(0, ("out of memory! (talloc failed)\n"));
1521 goto done;
1524 if (*path == '\0') {
1525 goto done;
1528 value = regdb_fetch_key_internal(db, mem_ctx, path);
1529 if (value.dptr == NULL) {
1530 goto done;
1533 if (value.dsize == 0) {
1534 DEBUG(10, ("regdb_key_exists: subkeylist-record for key "
1535 "[%s] is empty: Could be a deleted record in a "
1536 "clustered (ctdb) environment?\n",
1537 path));
1538 goto done;
1541 len = tdb_unpack(value.dptr, value.dsize, "d", &num_items);
1542 if (len == (int32_t)-1) {
1543 DEBUG(1, ("regdb_key_exists: ERROR: subkeylist-record for key "
1544 "[%s] is invalid: Could not parse initial 4-byte "
1545 "counter. record data length is %u.\n",
1546 path, (unsigned int)value.dsize));
1547 goto done;
1551 * Note: the tdb_unpack check above implies that len <= value.dsize
1553 buflen = value.dsize - len;
1554 buf = (const char *)value.dptr + len;
1556 len = 0;
1558 for (i = 0; i < num_items; i++) {
1559 if (buflen == 0) {
1560 break;
1562 len = strnlen(buf, buflen) + 1;
1563 if (buflen < len) {
1564 DEBUG(1, ("regdb_key_exists: ERROR: subkeylist-record "
1565 "for key [%s] is corrupt: %u items expected, "
1566 "item number %u is not zero terminated.\n",
1567 path, num_items, i+1));
1568 goto done;
1571 buf += len;
1572 buflen -= len;
1575 if (buflen > 0) {
1576 DEBUG(1, ("regdb_key_exists: ERROR: subkeylist-record for key "
1577 "[%s] is corrupt: %u items expected and found, but "
1578 "the record contains additional %u bytes\n",
1579 path, num_items, buflen));
1580 goto done;
1583 if (i < num_items) {
1584 DEBUG(1, ("regdb_key_exists: ERROR: subkeylist-record for key "
1585 "[%s] is corrupt: %u items expected, but only %u "
1586 "items found.\n",
1587 path, num_items, i+1));
1588 goto done;
1591 ret = true;
1593 done:
1594 TALLOC_FREE(mem_ctx);
1595 return ret;
1599 /***********************************************************************
1600 Retrieve an array of strings containing subkeys. Memory should be
1601 released by the caller.
1602 ***********************************************************************/
1604 static WERROR regdb_fetch_keys_internal(struct db_context *db, const char *key,
1605 struct regsubkey_ctr *ctr)
1607 WERROR werr;
1608 uint32_t num_items;
1609 uint8 *buf;
1610 uint32 buflen, len;
1611 int i;
1612 fstring subkeyname;
1613 TALLOC_CTX *frame = talloc_stackframe();
1614 TDB_DATA value;
1616 DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key ? key : "NULL"));
1618 if (!regdb_key_exists(db, key)) {
1619 DEBUG(10, ("key [%s] not found\n", key));
1620 werr = WERR_NOT_FOUND;
1621 goto done;
1624 werr = regsubkey_ctr_reinit(ctr);
1625 W_ERROR_NOT_OK_GOTO_DONE(werr);
1627 werr = regsubkey_ctr_set_seqnum(ctr, dbwrap_get_seqnum(db));
1628 W_ERROR_NOT_OK_GOTO_DONE(werr);
1630 value = regdb_fetch_key_internal(db, frame, key);
1632 if (value.dsize == 0 || value.dptr == NULL) {
1633 DEBUG(10, ("regdb_fetch_keys: no subkeys found for key [%s]\n",
1634 key));
1635 goto done;
1638 buf = value.dptr;
1639 buflen = value.dsize;
1640 len = tdb_unpack( buf, buflen, "d", &num_items);
1641 if (len == (uint32_t)-1) {
1642 werr = WERR_NOT_FOUND;
1643 goto done;
1646 for (i=0; i<num_items; i++) {
1647 len += tdb_unpack(buf+len, buflen-len, "f", subkeyname);
1648 werr = regsubkey_ctr_addkey(ctr, subkeyname);
1649 if (!W_ERROR_IS_OK(werr)) {
1650 DEBUG(5, ("regdb_fetch_keys: regsubkey_ctr_addkey "
1651 "failed: %s\n", win_errstr(werr)));
1652 num_items = 0;
1653 goto done;
1657 DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items));
1659 done:
1660 TALLOC_FREE(frame);
1661 return werr;
1664 int regdb_fetch_keys(const char *key, struct regsubkey_ctr *ctr)
1666 WERROR werr;
1668 werr = regdb_fetch_keys_internal(regdb, key, ctr);
1669 if (!W_ERROR_IS_OK(werr)) {
1670 return -1;
1673 return regsubkey_ctr_numkeys(ctr);
1676 /****************************************************************************
1677 Unpack a list of registry values frem the TDB
1678 ***************************************************************************/
1680 static int regdb_unpack_values(struct regval_ctr *values, uint8 *buf, int buflen)
1682 int len = 0;
1683 uint32 type;
1684 fstring valuename;
1685 uint32 size;
1686 uint8 *data_p;
1687 uint32 num_values = 0;
1688 int i;
1690 /* loop and unpack the rest of the registry values */
1692 len += tdb_unpack(buf+len, buflen-len, "d", &num_values);
1694 for ( i=0; i<num_values; i++ ) {
1695 /* unpack the next regval */
1697 type = REG_NONE;
1698 size = 0;
1699 data_p = NULL;
1700 valuename[0] = '\0';
1701 len += tdb_unpack(buf+len, buflen-len, "fdB",
1702 valuename,
1703 &type,
1704 &size,
1705 &data_p);
1707 regval_ctr_addvalue(values, valuename, type,
1708 (uint8_t *)data_p, size);
1709 SAFE_FREE(data_p); /* 'B' option to tdb_unpack does a malloc() */
1711 DEBUG(8,("specific: [%s], len: %d\n", valuename, size));
1714 return len;
1717 /****************************************************************************
1718 Pack all values in all printer keys
1719 ***************************************************************************/
1721 static int regdb_pack_values(struct regval_ctr *values, uint8 *buf, int buflen)
1723 int len = 0;
1724 int i;
1725 struct regval_blob *val;
1726 int num_values;
1728 if ( !values )
1729 return 0;
1731 num_values = regval_ctr_numvals( values );
1733 /* pack the number of values first */
1735 len += tdb_pack( buf+len, buflen-len, "d", num_values );
1737 /* loop over all values */
1739 for ( i=0; i<num_values; i++ ) {
1740 val = regval_ctr_specific_value( values, i );
1741 len += tdb_pack(buf+len, buflen-len, "fdB",
1742 regval_name(val),
1743 regval_type(val),
1744 regval_size(val),
1745 regval_data_p(val) );
1748 return len;
1751 /***********************************************************************
1752 Retrieve an array of strings containing subkeys. Memory should be
1753 released by the caller.
1754 ***********************************************************************/
1756 static int regdb_fetch_values_internal(struct db_context *db, const char* key,
1757 struct regval_ctr *values)
1759 char *keystr = NULL;
1760 TALLOC_CTX *ctx = talloc_stackframe();
1761 int ret = 0;
1762 TDB_DATA value;
1763 WERROR werr;
1765 DEBUG(10,("regdb_fetch_values: Looking for value of key [%s] \n", key));
1767 if (!regdb_key_exists(db, key)) {
1768 goto done;
1771 keystr = talloc_asprintf(ctx, "%s\\%s", REG_VALUE_PREFIX, key);
1772 if (!keystr) {
1773 goto done;
1776 werr = regval_ctr_set_seqnum(values, dbwrap_get_seqnum(db));
1777 W_ERROR_NOT_OK_GOTO_DONE(werr);
1779 value = regdb_fetch_key_internal(db, ctx, keystr);
1781 if (!value.dptr) {
1782 /* all keys have zero values by default */
1783 goto done;
1786 regdb_unpack_values(values, value.dptr, value.dsize);
1787 ret = regval_ctr_numvals(values);
1789 done:
1790 TALLOC_FREE(ctx);
1791 return ret;
1794 int regdb_fetch_values(const char* key, struct regval_ctr *values)
1796 return regdb_fetch_values_internal(regdb, key, values);
1799 static NTSTATUS regdb_store_values_internal(struct db_context *db,
1800 const char *key,
1801 struct regval_ctr *values)
1803 TDB_DATA old_data, data;
1804 char *keystr = NULL;
1805 TALLOC_CTX *ctx = talloc_stackframe();
1806 int len;
1807 NTSTATUS status;
1809 DEBUG(10,("regdb_store_values: Looking for value of key [%s] \n", key));
1811 if (!regdb_key_exists(db, key)) {
1812 status = NT_STATUS_NOT_FOUND;
1813 goto done;
1816 ZERO_STRUCT(data);
1818 len = regdb_pack_values(values, data.dptr, data.dsize);
1819 if (len <= 0) {
1820 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
1821 status = NT_STATUS_UNSUCCESSFUL;
1822 goto done;
1825 data.dptr = talloc_array(ctx, uint8, len);
1826 data.dsize = len;
1828 len = regdb_pack_values(values, data.dptr, data.dsize);
1830 SMB_ASSERT( len == data.dsize );
1832 keystr = talloc_asprintf(ctx, "%s\\%s", REG_VALUE_PREFIX, key );
1833 if (!keystr) {
1834 status = NT_STATUS_NO_MEMORY;
1835 goto done;
1837 keystr = normalize_reg_path(ctx, keystr);
1838 if (!keystr) {
1839 status = NT_STATUS_NO_MEMORY;
1840 goto done;
1843 status = dbwrap_fetch_bystring(db, ctx, keystr, &old_data);
1845 if (NT_STATUS_IS_OK(status)
1846 && (old_data.dptr != NULL)
1847 && (old_data.dsize == data.dsize)
1848 && (memcmp(old_data.dptr, data.dptr, data.dsize) == 0))
1850 status = NT_STATUS_OK;
1851 goto done;
1854 status = dbwrap_trans_store_bystring(db, keystr, data, TDB_REPLACE);
1856 done:
1857 TALLOC_FREE(ctx);
1858 return status;
1861 struct regdb_store_values_ctx {
1862 const char *key;
1863 struct regval_ctr *values;
1866 static NTSTATUS regdb_store_values_action(struct db_context *db,
1867 void *private_data)
1869 NTSTATUS status;
1870 struct regdb_store_values_ctx *ctx =
1871 (struct regdb_store_values_ctx *)private_data;
1873 status = regdb_store_values_internal(db, ctx->key, ctx->values);
1875 return status;
1878 bool regdb_store_values(const char *key, struct regval_ctr *values)
1880 WERROR werr;
1881 struct regdb_store_values_ctx ctx;
1883 ctx.key = key;
1884 ctx.values = values;
1886 werr = regdb_trans_do(regdb, regdb_store_values_action, &ctx);
1888 return W_ERROR_IS_OK(werr);
1891 static WERROR regdb_get_secdesc(TALLOC_CTX *mem_ctx, const char *key,
1892 struct security_descriptor **psecdesc)
1894 char *tdbkey;
1895 TDB_DATA data;
1896 NTSTATUS status;
1897 TALLOC_CTX *tmp_ctx = talloc_stackframe();
1898 WERROR err = WERR_OK;
1900 DEBUG(10, ("regdb_get_secdesc: Getting secdesc of key [%s]\n", key));
1902 if (!regdb_key_exists(regdb, key)) {
1903 err = WERR_BADFILE;
1904 goto done;
1907 tdbkey = talloc_asprintf(tmp_ctx, "%s\\%s", REG_SECDESC_PREFIX, key);
1908 if (tdbkey == NULL) {
1909 err = WERR_NOMEM;
1910 goto done;
1913 tdbkey = normalize_reg_path(tmp_ctx, tdbkey);
1914 if (tdbkey == NULL) {
1915 err = WERR_NOMEM;
1916 goto done;
1919 status = dbwrap_fetch_bystring(regdb, tmp_ctx, tdbkey, &data);
1920 if (!NT_STATUS_IS_OK(status)) {
1921 err = WERR_BADFILE;
1922 goto done;
1925 status = unmarshall_sec_desc(mem_ctx, (uint8 *)data.dptr, data.dsize,
1926 psecdesc);
1928 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MEMORY)) {
1929 err = WERR_NOMEM;
1930 } else if (!NT_STATUS_IS_OK(status)) {
1931 err = WERR_REG_CORRUPT;
1934 done:
1935 TALLOC_FREE(tmp_ctx);
1936 return err;
1939 struct regdb_set_secdesc_ctx {
1940 const char *key;
1941 struct security_descriptor *secdesc;
1944 static NTSTATUS regdb_set_secdesc_action(struct db_context *db,
1945 void *private_data)
1947 char *tdbkey;
1948 NTSTATUS status;
1949 TDB_DATA tdbdata;
1950 struct regdb_set_secdesc_ctx *ctx =
1951 (struct regdb_set_secdesc_ctx *)private_data;
1952 TALLOC_CTX *frame = talloc_stackframe();
1954 tdbkey = talloc_asprintf(frame, "%s\\%s", REG_SECDESC_PREFIX, ctx->key);
1955 if (tdbkey == NULL) {
1956 status = NT_STATUS_NO_MEMORY;
1957 goto done;
1960 tdbkey = normalize_reg_path(frame, tdbkey);
1961 if (tdbkey == NULL) {
1962 status = NT_STATUS_NO_MEMORY;
1963 goto done;
1966 if (ctx->secdesc == NULL) {
1967 /* assuming a delete */
1968 status = dbwrap_delete_bystring(db, tdbkey);
1969 goto done;
1972 status = marshall_sec_desc(frame, ctx->secdesc, &tdbdata.dptr,
1973 &tdbdata.dsize);
1974 if (!NT_STATUS_IS_OK(status)) {
1975 goto done;
1978 status = dbwrap_store_bystring(db, tdbkey, tdbdata, 0);
1980 done:
1981 TALLOC_FREE(frame);
1982 return status;
1985 static WERROR regdb_set_secdesc(const char *key,
1986 struct security_descriptor *secdesc)
1988 WERROR err;
1989 struct regdb_set_secdesc_ctx ctx;
1991 if (!regdb_key_exists(regdb, key)) {
1992 err = WERR_BADFILE;
1993 goto done;
1996 ctx.key = key;
1997 ctx.secdesc = secdesc;
1999 err = regdb_trans_do(regdb, regdb_set_secdesc_action, &ctx);
2001 done:
2002 return err;
2005 bool regdb_subkeys_need_update(struct regsubkey_ctr *subkeys)
2007 return (regdb_get_seqnum() != regsubkey_ctr_get_seqnum(subkeys));
2010 bool regdb_values_need_update(struct regval_ctr *values)
2012 return (regdb_get_seqnum() != regval_ctr_get_seqnum(values));
2016 * Table of function pointers for default access
2019 struct registry_ops regdb_ops = {
2020 .fetch_subkeys = regdb_fetch_keys,
2021 .fetch_values = regdb_fetch_values,
2022 .store_subkeys = regdb_store_keys,
2023 .store_values = regdb_store_values,
2024 .create_subkey = regdb_create_subkey,
2025 .delete_subkey = regdb_delete_subkey,
2026 .get_secdesc = regdb_get_secdesc,
2027 .set_secdesc = regdb_set_secdesc,
2028 .subkeys_need_update = regdb_subkeys_need_update,
2029 .values_need_update = regdb_values_need_update