s3:registry: turn create_sorted_subkeys_internal to NTSTATUS return type
[Samba.git] / source3 / registry / reg_backend_db.c
blobb99ef916a53600ae7f98692a9416606dde20848e
1 /*
2 * Unix SMB/CIFS implementation.
3 * Virtual Windows Registry Layer
4 * Copyright (C) Gerald Carter 2002-2005
5 * Copyright (C) Michael Adam 2007-2009
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 /* Implementation of internal registry database functions. */
23 #include "includes.h"
24 #include "system/filesys.h"
25 #include "registry.h"
26 #include "reg_db.h"
27 #include "reg_util_internal.h"
28 #include "reg_backend_db.h"
29 #include "reg_objects.h"
30 #include "nt_printing.h"
31 #include "util_tdb.h"
32 #include "dbwrap.h"
34 #undef DBGC_CLASS
35 #define DBGC_CLASS DBGC_REGISTRY
37 static struct db_context *regdb = NULL;
38 static int regdb_refcount;
40 static bool regdb_key_exists(struct db_context *db, const char *key);
41 static bool regdb_key_is_base_key(const char *key);
42 static WERROR regdb_fetch_keys_internal(struct db_context *db, const char *key,
43 struct regsubkey_ctr *ctr);
44 static bool regdb_store_keys_internal(struct db_context *db, const char *key,
45 struct regsubkey_ctr *ctr);
46 static int regdb_fetch_values_internal(struct db_context *db, const char* key,
47 struct regval_ctr *values);
48 static bool regdb_store_values_internal(struct db_context *db, const char *key,
49 struct regval_ctr *values);
51 /* List the deepest path into the registry. All part components will be created.*/
53 /* If you want to have a part of the path controlled by the tdb and part by
54 a virtual registry db (e.g. printing), then you have to list the deepest path.
55 For example,"HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion/Print"
56 allows the reg_db backend to handle everything up to
57 "HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion" and then we'll hook
58 the reg_printing backend onto the last component of the path (see
59 KEY_PRINTING_2K in include/rpc_reg.h) --jerry */
61 static const char *builtin_registry_paths[] = {
62 KEY_PRINTING_2K,
63 KEY_PRINTING_PORTS,
64 KEY_PRINTING,
65 KEY_PRINTING "\\Forms",
66 KEY_PRINTING "\\Printers",
67 KEY_PRINTING "\\Environments\\Windows NT x86\\Print Processors\\winprint",
68 KEY_SHARES,
69 KEY_EVENTLOG,
70 KEY_SMBCONF,
71 KEY_PERFLIB,
72 KEY_PERFLIB_009,
73 KEY_GROUP_POLICY,
74 KEY_SAMBA_GROUP_POLICY,
75 KEY_GP_MACHINE_POLICY,
76 KEY_GP_MACHINE_WIN_POLICY,
77 KEY_HKCU,
78 KEY_GP_USER_POLICY,
79 KEY_GP_USER_WIN_POLICY,
80 "HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon\\GPExtensions",
81 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Print\\Monitors",
82 KEY_PROD_OPTIONS,
83 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\DefaultUserConfiguration",
84 KEY_TCPIP_PARAMS,
85 KEY_NETLOGON_PARAMS,
86 KEY_HKU,
87 KEY_HKCR,
88 KEY_HKPD,
89 KEY_HKPT,
90 NULL };
92 struct builtin_regkey_value {
93 const char *path;
94 const char *valuename;
95 uint32 type;
96 union {
97 const char *string;
98 uint32 dw_value;
99 } data;
102 static struct builtin_regkey_value builtin_registry_values[] = {
103 { KEY_PRINTING_PORTS,
104 SAMBA_PRINTER_PORT_NAME, REG_SZ, { "" } },
105 { KEY_PRINTING_2K,
106 "DefaultSpoolDirectory", REG_SZ, { "C:\\Windows\\System32\\Spool\\Printers" } },
107 { KEY_EVENTLOG,
108 "DisplayName", REG_SZ, { "Event Log" } },
109 { KEY_EVENTLOG,
110 "ErrorControl", REG_DWORD, { (char*)0x00000001 } },
111 { NULL, NULL, 0, { NULL } }
115 * Initialize a key in the registry:
116 * create each component key of the specified path.
118 static WERROR init_registry_key_internal(struct db_context *db,
119 const char *add_path)
121 WERROR werr;
122 TALLOC_CTX *frame = talloc_stackframe();
123 char *path = NULL;
124 char *base = NULL;
125 char *remaining = NULL;
126 char *keyname;
127 char *subkeyname;
128 struct regsubkey_ctr *subkeys;
129 const char *p, *p2;
131 DEBUG(6, ("init_registry_key: Adding [%s]\n", add_path));
133 path = talloc_strdup(frame, add_path);
134 base = talloc_strdup(frame, "");
135 if (!path || !base) {
136 werr = WERR_NOMEM;
137 goto fail;
139 p = path;
141 while (next_token_talloc(frame, &p, &keyname, "\\")) {
143 /* build up the registry path from the components */
145 if (*base) {
146 base = talloc_asprintf(frame, "%s\\", base);
147 if (!base) {
148 werr = WERR_NOMEM;
149 goto fail;
152 base = talloc_asprintf_append(base, "%s", keyname);
153 if (!base) {
154 werr = WERR_NOMEM;
155 goto fail;
158 /* get the immediate subkeyname (if we have one ) */
160 subkeyname = talloc_strdup(frame, "");
161 if (!subkeyname) {
162 werr = WERR_NOMEM;
163 goto fail;
165 if (*p) {
166 remaining = talloc_strdup(frame, p);
167 if (!remaining) {
168 werr = WERR_NOMEM;
169 goto fail;
171 p2 = remaining;
173 if (!next_token_talloc(frame, &p2,
174 &subkeyname, "\\"))
176 subkeyname = talloc_strdup(frame,p2);
177 if (!subkeyname) {
178 werr = WERR_NOMEM;
179 goto fail;
184 DEBUG(10,("init_registry_key: Storing key [%s] with "
185 "subkey [%s]\n", base,
186 *subkeyname ? subkeyname : "NULL"));
188 /* we don't really care if the lookup succeeds or not
189 * since we are about to update the record.
190 * We just want any subkeys already present */
192 werr = regsubkey_ctr_init(frame, &subkeys);
193 if (!W_ERROR_IS_OK(werr)) {
194 DEBUG(0,("talloc() failure!\n"));
195 goto fail;
198 werr = regdb_fetch_keys_internal(db, base, subkeys);
199 if (!W_ERROR_IS_OK(werr) &&
200 !W_ERROR_EQUAL(werr, WERR_NOT_FOUND))
202 goto fail;
205 if (*subkeyname) {
206 werr = regsubkey_ctr_addkey(subkeys, subkeyname);
207 if (!W_ERROR_IS_OK(werr)) {
208 goto fail;
211 if (!regdb_store_keys_internal(db, base, subkeys)) {
212 werr = WERR_CAN_NOT_COMPLETE;
213 goto fail;
217 werr = WERR_OK;
219 fail:
220 TALLOC_FREE(frame);
221 return werr;
224 struct init_registry_key_context {
225 const char *add_path;
228 static NTSTATUS init_registry_key_action(struct db_context *db,
229 void *private_data)
231 struct init_registry_key_context *init_ctx =
232 (struct init_registry_key_context *)private_data;
234 return werror_to_ntstatus(init_registry_key_internal(
235 db, init_ctx->add_path));
239 * Initialize a key in the registry:
240 * create each component key of the specified path,
241 * wrapped in one db transaction.
243 WERROR init_registry_key(const char *add_path)
245 struct init_registry_key_context init_ctx;
247 if (regdb_key_exists(regdb, add_path)) {
248 return WERR_OK;
251 init_ctx.add_path = add_path;
253 return ntstatus_to_werror(dbwrap_trans_do(regdb,
254 init_registry_key_action,
255 &init_ctx));
258 /***********************************************************************
259 Open the registry data in the tdb
260 ***********************************************************************/
262 static void regdb_ctr_add_value(struct regval_ctr *ctr,
263 struct builtin_regkey_value *value)
265 switch(value->type) {
266 case REG_DWORD:
267 regval_ctr_addvalue(ctr, value->valuename, REG_DWORD,
268 (uint8_t *)&value->data.dw_value,
269 sizeof(uint32));
270 break;
272 case REG_SZ:
273 regval_ctr_addvalue_sz(ctr, value->valuename,
274 value->data.string);
275 break;
277 default:
278 DEBUG(0, ("regdb_ctr_add_value: invalid value type in "
279 "registry values [%d]\n", value->type));
283 static NTSTATUS init_registry_data_action(struct db_context *db,
284 void *private_data)
286 NTSTATUS status;
287 TALLOC_CTX *frame = talloc_stackframe();
288 struct regval_ctr *values;
289 int i;
291 /* loop over all of the predefined paths and add each component */
293 for (i=0; builtin_registry_paths[i] != NULL; i++) {
294 if (regdb_key_exists(db, builtin_registry_paths[i])) {
295 continue;
297 status = werror_to_ntstatus(init_registry_key_internal(db,
298 builtin_registry_paths[i]));
299 if (!NT_STATUS_IS_OK(status)) {
300 goto done;
304 /* loop over all of the predefined values and add each component */
306 for (i=0; builtin_registry_values[i].path != NULL; i++) {
307 WERROR werr;
309 werr = regval_ctr_init(frame, &values);
310 if (!W_ERROR_IS_OK(werr)) {
311 status = werror_to_ntstatus(werr);
312 goto done;
315 regdb_fetch_values_internal(db,
316 builtin_registry_values[i].path,
317 values);
319 /* preserve existing values across restarts. Only add new ones */
321 if (!regval_ctr_key_exists(values,
322 builtin_registry_values[i].valuename))
324 regdb_ctr_add_value(values,
325 &builtin_registry_values[i]);
326 regdb_store_values_internal(db,
327 builtin_registry_values[i].path,
328 values);
330 TALLOC_FREE(values);
333 status = NT_STATUS_OK;
335 done:
337 TALLOC_FREE(frame);
338 return status;
341 WERROR init_registry_data(void)
343 WERROR werr;
344 TALLOC_CTX *frame = talloc_stackframe();
345 struct regval_ctr *values;
346 int i;
349 * First, check for the existence of the needed keys and values.
350 * If all do already exist, we can save the writes.
352 for (i=0; builtin_registry_paths[i] != NULL; i++) {
353 if (!regdb_key_exists(regdb, builtin_registry_paths[i])) {
354 goto do_init;
358 for (i=0; builtin_registry_values[i].path != NULL; i++) {
359 werr = regval_ctr_init(frame, &values);
360 W_ERROR_NOT_OK_GOTO_DONE(werr);
362 regdb_fetch_values_internal(regdb,
363 builtin_registry_values[i].path,
364 values);
365 if (!regval_ctr_key_exists(values,
366 builtin_registry_values[i].valuename))
368 TALLOC_FREE(values);
369 goto do_init;
372 TALLOC_FREE(values);
375 werr = WERR_OK;
376 goto done;
378 do_init:
381 * There are potentially quite a few store operations which are all
382 * indiviually wrapped in tdb transactions. Wrapping them in a single
383 * transaction gives just a single transaction_commit() to actually do
384 * its fsync()s. See tdb/common/transaction.c for info about nested
385 * transaction behaviour.
388 werr = ntstatus_to_werror(dbwrap_trans_do(regdb,
389 init_registry_data_action,
390 NULL));
392 done:
393 TALLOC_FREE(frame);
394 return werr;
397 static int regdb_normalize_keynames_fn(struct db_record *rec,
398 void *private_data)
400 TALLOC_CTX *mem_ctx = talloc_tos();
401 const char *keyname;
402 NTSTATUS status;
404 if (rec->key.dptr == NULL || rec->key.dsize == 0) {
405 return 0;
408 keyname = strchr((const char *) rec->key.dptr, '/');
409 if (keyname) {
410 struct db_record new_rec;
412 keyname = talloc_string_sub(mem_ctx,
413 (const char *) rec->key.dptr,
414 "/",
415 "\\");
417 DEBUG(2, ("regdb_normalize_keynames_fn: Convert %s to %s\n",
418 (const char *) rec->key.dptr,
419 keyname));
421 new_rec.value = rec->value;
422 new_rec.key = string_term_tdb_data(keyname);
423 new_rec.private_data = rec->private_data;
425 /* Delete the original record and store the normalized key */
426 status = rec->delete_rec(rec);
427 if (!NT_STATUS_IS_OK(status)) {
428 DEBUG(0,("regdb_normalize_keynames_fn: "
429 "tdb_delete for [%s] failed!\n",
430 rec->key.dptr));
431 return 1;
434 status = rec->store(&new_rec, new_rec.value, TDB_REPLACE);
435 if (!NT_STATUS_IS_OK(status)) {
436 DEBUG(0,("regdb_normalize_keynames_fn: "
437 "failed to store new record for [%s]!\n",
438 keyname));
439 return 1;
443 return 0;
446 static WERROR regdb_store_regdb_version(uint32_t version)
448 NTSTATUS status;
449 const char *version_keyname = "INFO/version";
451 if (!regdb) {
452 return WERR_CAN_NOT_COMPLETE;
455 status = dbwrap_trans_store_int32(regdb, version_keyname, version);
456 if (!NT_STATUS_IS_OK(status)) {
457 DEBUG(1, ("regdb_store_regdb_version: error storing %s = %d: %s\n",
458 version_keyname, version, nt_errstr(status)));
459 return ntstatus_to_werror(status);
460 } else {
461 DEBUG(10, ("regdb_store_regdb_version: stored %s = %d\n",
462 version_keyname, version));
463 return WERR_OK;
467 static WERROR regdb_upgrade_v1_to_v2(void)
469 TALLOC_CTX *mem_ctx;
470 int rc;
471 WERROR werr;
473 mem_ctx = talloc_stackframe();
474 if (mem_ctx == NULL) {
475 return WERR_NOMEM;
478 rc = regdb->traverse(regdb, regdb_normalize_keynames_fn, mem_ctx);
480 talloc_destroy(mem_ctx);
482 if (rc == -1) {
483 return WERR_REG_IO_FAILURE;
486 werr = regdb_store_regdb_version(REGVER_V2);
487 return werr;
490 /***********************************************************************
491 Open the registry database
492 ***********************************************************************/
494 WERROR regdb_init(void)
496 const char *vstring = "INFO/version";
497 uint32 vers_id, expected_version;
498 WERROR werr;
500 if (regdb) {
501 DEBUG(10, ("regdb_init: incrementing refcount (%d->%d)\n",
502 regdb_refcount, regdb_refcount+1));
503 regdb_refcount++;
504 return WERR_OK;
507 regdb = db_open(NULL, state_path("registry.tdb"), 0,
508 REG_TDB_FLAGS, O_RDWR, 0600);
509 if (!regdb) {
510 regdb = db_open(NULL, state_path("registry.tdb"), 0,
511 REG_TDB_FLAGS, O_RDWR|O_CREAT, 0600);
512 if (!regdb) {
513 werr = ntstatus_to_werror(map_nt_error_from_unix(errno));
514 DEBUG(1,("regdb_init: Failed to open registry %s (%s)\n",
515 state_path("registry.tdb"), strerror(errno) ));
516 return werr;
519 DEBUG(10,("regdb_init: Successfully created registry tdb\n"));
522 regdb_refcount = 1;
523 DEBUG(10, ("regdb_init: registry db openend. refcount reset (%d)\n",
524 regdb_refcount));
526 expected_version = REGVER_V2;
528 vers_id = dbwrap_fetch_int32(regdb, vstring);
529 if (vers_id == -1) {
530 DEBUG(10, ("regdb_init: registry version uninitialized "
531 "(got %d), initializing to version %d\n",
532 vers_id, expected_version));
534 werr = regdb_store_regdb_version(expected_version);
535 return werr;
538 if (vers_id > expected_version || vers_id == 0) {
539 DEBUG(1, ("regdb_init: unknown registry version %d "
540 "(code version = %d), refusing initialization\n",
541 vers_id, expected_version));
542 return WERR_CAN_NOT_COMPLETE;
545 if (vers_id == REGVER_V1) {
546 DEBUG(10, ("regdb_init: got registry db version %d, upgrading "
547 "to version %d\n", REGVER_V1, REGVER_V2));
549 if (regdb->transaction_start(regdb) != 0) {
550 return WERR_REG_IO_FAILURE;
553 werr = regdb_upgrade_v1_to_v2();
554 if (!W_ERROR_IS_OK(werr)) {
555 regdb->transaction_cancel(regdb);
556 return werr;
559 if (regdb->transaction_commit(regdb) != 0) {
560 return WERR_REG_IO_FAILURE;
563 vers_id = REGVER_V2;
566 /* future upgrade code should go here */
568 return WERR_OK;
571 /***********************************************************************
572 Open the registry. Must already have been initialized by regdb_init()
573 ***********************************************************************/
575 WERROR regdb_open( void )
577 WERROR result = WERR_OK;
579 if ( regdb ) {
580 DEBUG(10, ("regdb_open: incrementing refcount (%d->%d)\n",
581 regdb_refcount, regdb_refcount+1));
582 regdb_refcount++;
583 return WERR_OK;
586 become_root();
588 regdb = db_open(NULL, state_path("registry.tdb"), 0,
589 REG_TDB_FLAGS, O_RDWR, 0600);
590 if ( !regdb ) {
591 result = ntstatus_to_werror( map_nt_error_from_unix( errno ) );
592 DEBUG(0,("regdb_open: Failed to open %s! (%s)\n",
593 state_path("registry.tdb"), strerror(errno) ));
596 unbecome_root();
598 regdb_refcount = 1;
599 DEBUG(10, ("regdb_open: registry db opened. refcount reset (%d)\n",
600 regdb_refcount));
602 return result;
605 /***********************************************************************
606 ***********************************************************************/
608 int regdb_close( void )
610 if (regdb_refcount == 0) {
611 return 0;
614 regdb_refcount--;
616 DEBUG(10, ("regdb_close: decrementing refcount (%d->%d)\n",
617 regdb_refcount+1, regdb_refcount));
619 if ( regdb_refcount > 0 )
620 return 0;
622 SMB_ASSERT( regdb_refcount >= 0 );
624 TALLOC_FREE(regdb);
625 return 0;
628 WERROR regdb_transaction_start(void)
630 return (regdb->transaction_start(regdb) == 0) ?
631 WERR_OK : WERR_REG_IO_FAILURE;
634 WERROR regdb_transaction_commit(void)
636 return (regdb->transaction_commit(regdb) == 0) ?
637 WERR_OK : WERR_REG_IO_FAILURE;
640 WERROR regdb_transaction_cancel(void)
642 return (regdb->transaction_cancel(regdb) == 0) ?
643 WERR_OK : WERR_REG_IO_FAILURE;
646 /***********************************************************************
647 return the tdb sequence number of the registry tdb.
648 this is an indicator for the content of the registry
649 having changed. it will change upon regdb_init, too, though.
650 ***********************************************************************/
651 int regdb_get_seqnum(void)
653 return regdb->get_seqnum(regdb);
657 static WERROR regdb_delete_key_with_prefix(struct db_context *db,
658 const char *keyname,
659 const char *prefix)
661 char *path;
662 WERROR werr = WERR_NOMEM;
663 TALLOC_CTX *mem_ctx = talloc_stackframe();
665 if (keyname == NULL) {
666 werr = WERR_INVALID_PARAM;
667 goto done;
670 if (prefix == NULL) {
671 path = discard_const_p(char, keyname);
672 } else {
673 path = talloc_asprintf(mem_ctx, "%s\\%s", prefix, keyname);
674 if (path == NULL) {
675 goto done;
679 path = normalize_reg_path(mem_ctx, path);
680 if (path == NULL) {
681 goto done;
684 werr = ntstatus_to_werror(dbwrap_delete_bystring(db, path));
686 /* treat "not" found" as ok */
687 if (W_ERROR_EQUAL(werr, WERR_NOT_FOUND)) {
688 werr = WERR_OK;
691 done:
692 talloc_free(mem_ctx);
693 return werr;
697 static WERROR regdb_delete_values(struct db_context *db, const char *keyname)
699 return regdb_delete_key_with_prefix(db, keyname, REG_VALUE_PREFIX);
702 static WERROR regdb_delete_secdesc(struct db_context *db, const char *keyname)
704 return regdb_delete_key_with_prefix(db, keyname, REG_SECDESC_PREFIX);
707 static WERROR regdb_delete_subkeylist(struct db_context *db, const char *keyname)
709 return regdb_delete_key_with_prefix(db, keyname, NULL);
712 static WERROR regdb_delete_key_lists(struct db_context *db, const char *keyname)
714 WERROR werr;
716 werr = regdb_delete_values(db, keyname);
717 if (!W_ERROR_IS_OK(werr)) {
718 DEBUG(1, (__location__ " Deleting %s\\%s failed: %s\n",
719 REG_VALUE_PREFIX, keyname, win_errstr(werr)));
720 goto done;
723 werr = regdb_delete_secdesc(db, keyname);
724 if (!W_ERROR_IS_OK(werr)) {
725 DEBUG(1, (__location__ " Deleting %s\\%s failed: %s\n",
726 REG_SECDESC_PREFIX, keyname, win_errstr(werr)));
727 goto done;
730 werr = regdb_delete_subkeylist(db, keyname);
731 if (!W_ERROR_IS_OK(werr)) {
732 DEBUG(1, (__location__ " Deleting %s failed: %s\n",
733 keyname, win_errstr(werr)));
734 goto done;
737 done:
738 return werr;
741 /***********************************************************************
742 Add subkey strings to the registry tdb under a defined key
743 fmt is the same format as tdb_pack except this function only supports
744 fstrings
745 ***********************************************************************/
747 static WERROR regdb_store_keys_internal2(struct db_context *db,
748 const char *key,
749 struct regsubkey_ctr *ctr)
751 TDB_DATA dbuf;
752 uint8 *buffer = NULL;
753 int i = 0;
754 uint32 len, buflen;
755 uint32 num_subkeys = regsubkey_ctr_numkeys(ctr);
756 char *keyname = NULL;
757 TALLOC_CTX *ctx = talloc_stackframe();
758 WERROR werr;
760 if (!key) {
761 werr = WERR_INVALID_PARAM;
762 goto done;
765 keyname = talloc_strdup(ctx, key);
766 if (!keyname) {
767 werr = WERR_NOMEM;
768 goto done;
771 keyname = normalize_reg_path(ctx, keyname);
772 if (!keyname) {
773 werr = WERR_NOMEM;
774 goto done;
777 /* allocate some initial memory */
779 buffer = (uint8 *)SMB_MALLOC(1024);
780 if (buffer == NULL) {
781 werr = WERR_NOMEM;
782 goto done;
784 buflen = 1024;
785 len = 0;
787 /* store the number of subkeys */
789 len += tdb_pack(buffer+len, buflen-len, "d", num_subkeys);
791 /* pack all the strings */
793 for (i=0; i<num_subkeys; i++) {
794 size_t thistime;
796 thistime = tdb_pack(buffer+len, buflen-len, "f",
797 regsubkey_ctr_specific_key(ctr, i));
798 if (len+thistime > buflen) {
799 size_t thistime2;
801 * tdb_pack hasn't done anything because of the short
802 * buffer, allocate extra space.
804 buffer = SMB_REALLOC_ARRAY(buffer, uint8_t,
805 (len+thistime)*2);
806 if(buffer == NULL) {
807 DEBUG(0, ("regdb_store_keys: Failed to realloc "
808 "memory of size [%u]\n",
809 (unsigned int)(len+thistime)*2));
810 werr = WERR_NOMEM;
811 goto done;
813 buflen = (len+thistime)*2;
814 thistime2 = tdb_pack(
815 buffer+len, buflen-len, "f",
816 regsubkey_ctr_specific_key(ctr, i));
817 if (thistime2 != thistime) {
818 DEBUG(0, ("tdb_pack failed\n"));
819 werr = WERR_CAN_NOT_COMPLETE;
820 goto done;
823 len += thistime;
826 /* finally write out the data */
828 dbuf.dptr = buffer;
829 dbuf.dsize = len;
830 werr = ntstatus_to_werror(dbwrap_store_bystring(db, keyname, dbuf,
831 TDB_REPLACE));
832 W_ERROR_NOT_OK_GOTO_DONE(werr);
835 * Delete a sorted subkey cache for regdb_key_exists, will be
836 * recreated automatically
838 keyname = talloc_asprintf(ctx, "%s\\%s", REG_SORTED_SUBKEYS_PREFIX,
839 keyname);
840 if (keyname == NULL) {
841 werr = WERR_NOMEM;
842 goto done;
845 werr = ntstatus_to_werror(dbwrap_delete_bystring(db, keyname));
847 /* don't treat WERR_NOT_FOUND as an error here */
848 if (W_ERROR_EQUAL(werr, WERR_NOT_FOUND)) {
849 werr = WERR_OK;
852 done:
853 TALLOC_FREE(ctx);
854 SAFE_FREE(buffer);
855 return werr;
858 /***********************************************************************
859 Store the new subkey record and create any child key records that
860 do not currently exist
861 ***********************************************************************/
863 struct regdb_store_keys_context {
864 const char *key;
865 struct regsubkey_ctr *ctr;
868 static NTSTATUS regdb_store_keys_action(struct db_context *db,
869 void *private_data)
871 struct regdb_store_keys_context *store_ctx;
872 WERROR werr;
873 int num_subkeys, i;
874 char *path = NULL;
875 struct regsubkey_ctr *subkeys = NULL, *old_subkeys = NULL;
876 char *oldkeyname = NULL;
877 TALLOC_CTX *mem_ctx = talloc_stackframe();
879 store_ctx = (struct regdb_store_keys_context *)private_data;
882 * Re-fetch the old keys inside the transaction
885 werr = regsubkey_ctr_init(mem_ctx, &old_subkeys);
886 W_ERROR_NOT_OK_GOTO_DONE(werr);
888 werr = regdb_fetch_keys_internal(db, store_ctx->key, old_subkeys);
889 if (!W_ERROR_IS_OK(werr) &&
890 !W_ERROR_EQUAL(werr, WERR_NOT_FOUND))
892 goto done;
896 * Make the store operation as safe as possible without transactions:
898 * (1) For each subkey removed from ctr compared with old_subkeys:
900 * (a) First delete the value db entry.
902 * (b) Next delete the secdesc db record.
904 * (c) Then delete the subkey list entry.
906 * (2) Now write the list of subkeys of the parent key,
907 * deleting removed entries and adding new ones.
909 * (3) Finally create the subkey list entries for the added keys.
911 * This way if we crash half-way in between deleting the subkeys
912 * and storing the parent's list of subkeys, no old data can pop up
913 * out of the blue when re-adding keys later on.
916 /* (1) delete removed keys' lists (values/secdesc/subkeys) */
918 num_subkeys = regsubkey_ctr_numkeys(old_subkeys);
919 for (i=0; i<num_subkeys; i++) {
920 oldkeyname = regsubkey_ctr_specific_key(old_subkeys, i);
922 if (regsubkey_ctr_key_exists(store_ctx->ctr, oldkeyname)) {
924 * It's still around, don't delete
926 continue;
929 path = talloc_asprintf(mem_ctx, "%s\\%s", store_ctx->key,
930 oldkeyname);
931 if (!path) {
932 werr = WERR_NOMEM;
933 goto done;
936 werr = regdb_delete_key_lists(db, path);
937 W_ERROR_NOT_OK_GOTO_DONE(werr);
939 TALLOC_FREE(path);
942 TALLOC_FREE(old_subkeys);
944 /* (2) store the subkey list for the parent */
946 werr = regdb_store_keys_internal2(db, store_ctx->key, store_ctx->ctr);
947 if (!W_ERROR_IS_OK(werr)) {
948 DEBUG(0,("regdb_store_keys: Failed to store new subkey list "
949 "for parent [%s]: %s\n", store_ctx->key,
950 win_errstr(werr)));
951 goto done;
954 /* (3) now create records for any subkeys that don't already exist */
956 num_subkeys = regsubkey_ctr_numkeys(store_ctx->ctr);
958 if (num_subkeys == 0) {
959 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
960 W_ERROR_NOT_OK_GOTO_DONE(werr);
962 werr = regdb_store_keys_internal2(db, store_ctx->key, subkeys);
963 if (!W_ERROR_IS_OK(werr)) {
964 DEBUG(0,("regdb_store_keys: Failed to store "
965 "new record for key [%s]: %s\n",
966 store_ctx->key, win_errstr(werr)));
967 goto done;
969 TALLOC_FREE(subkeys);
972 for (i=0; i<num_subkeys; i++) {
973 path = talloc_asprintf(mem_ctx, "%s\\%s", store_ctx->key,
974 regsubkey_ctr_specific_key(store_ctx->ctr, i));
975 if (!path) {
976 werr = WERR_NOMEM;
977 goto done;
979 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
980 W_ERROR_NOT_OK_GOTO_DONE(werr);
982 werr = regdb_fetch_keys_internal(db, path, subkeys);
983 if (!W_ERROR_IS_OK(werr)) {
984 /* create a record with 0 subkeys */
985 werr = regdb_store_keys_internal2(db, path, subkeys);
986 if (!W_ERROR_IS_OK(werr)) {
987 DEBUG(0,("regdb_store_keys: Failed to store "
988 "new record for key [%s]: %s\n", path,
989 win_errstr(werr)));
990 goto done;
994 TALLOC_FREE(subkeys);
995 TALLOC_FREE(path);
998 werr = WERR_OK;
1000 done:
1001 talloc_free(mem_ctx);
1002 return werror_to_ntstatus(werr);
1005 static bool regdb_store_keys_internal(struct db_context *db, const char *key,
1006 struct regsubkey_ctr *ctr)
1008 int num_subkeys, old_num_subkeys, i;
1009 struct regsubkey_ctr *old_subkeys = NULL;
1010 TALLOC_CTX *ctx = talloc_stackframe();
1011 WERROR werr;
1012 bool ret = false;
1013 struct regdb_store_keys_context store_ctx;
1015 if (!regdb_key_is_base_key(key) && !regdb_key_exists(db, key)) {
1016 goto done;
1020 * fetch a list of the old subkeys so we can determine if anything has
1021 * changed
1024 werr = regsubkey_ctr_init(ctx, &old_subkeys);
1025 if (!W_ERROR_IS_OK(werr)) {
1026 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
1027 goto done;
1030 werr = regdb_fetch_keys_internal(db, key, old_subkeys);
1031 if (!W_ERROR_IS_OK(werr) &&
1032 !W_ERROR_EQUAL(werr, WERR_NOT_FOUND))
1034 goto done;
1037 num_subkeys = regsubkey_ctr_numkeys(ctr);
1038 old_num_subkeys = regsubkey_ctr_numkeys(old_subkeys);
1039 if ((num_subkeys && old_num_subkeys) &&
1040 (num_subkeys == old_num_subkeys)) {
1042 for (i = 0; i < num_subkeys; i++) {
1043 if (strcmp(regsubkey_ctr_specific_key(ctr, i),
1044 regsubkey_ctr_specific_key(old_subkeys, i))
1045 != 0)
1047 break;
1050 if (i == num_subkeys) {
1052 * Nothing changed, no point to even start a tdb
1053 * transaction
1056 ret = true;
1057 goto done;
1061 TALLOC_FREE(old_subkeys);
1063 store_ctx.key = key;
1064 store_ctx.ctr = ctr;
1066 werr = ntstatus_to_werror(dbwrap_trans_do(db,
1067 regdb_store_keys_action,
1068 &store_ctx));
1070 ret = W_ERROR_IS_OK(werr);
1072 done:
1073 TALLOC_FREE(ctx);
1075 return ret;
1078 bool regdb_store_keys(const char *key, struct regsubkey_ctr *ctr)
1080 return regdb_store_keys_internal(regdb, key, ctr);
1084 * create a subkey of a given key
1087 struct regdb_create_subkey_context {
1088 const char *key;
1089 const char *subkey;
1092 static NTSTATUS regdb_create_subkey_action(struct db_context *db,
1093 void *private_data)
1095 WERROR werr;
1096 struct regdb_create_subkey_context *create_ctx;
1097 struct regsubkey_ctr *subkeys;
1098 TALLOC_CTX *mem_ctx = talloc_stackframe();
1100 create_ctx = (struct regdb_create_subkey_context *)private_data;
1102 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
1103 W_ERROR_NOT_OK_GOTO_DONE(werr);
1105 werr = regdb_fetch_keys_internal(db, create_ctx->key, subkeys);
1106 W_ERROR_NOT_OK_GOTO_DONE(werr);
1108 werr = regsubkey_ctr_addkey(subkeys, create_ctx->subkey);
1109 W_ERROR_NOT_OK_GOTO_DONE(werr);
1111 werr = regdb_store_keys_internal2(db, create_ctx->key, subkeys);
1112 if (!W_ERROR_IS_OK(werr)) {
1113 DEBUG(0, (__location__ " failed to store new subkey list for "
1114 "parent key %s: %s\n", create_ctx->key,
1115 win_errstr(werr)));
1118 done:
1119 talloc_free(mem_ctx);
1120 return werror_to_ntstatus(werr);
1123 static WERROR regdb_create_subkey(const char *key, const char *subkey)
1125 WERROR werr;
1126 struct regsubkey_ctr *subkeys;
1127 TALLOC_CTX *mem_ctx = talloc_stackframe();
1128 struct regdb_create_subkey_context create_ctx;
1130 if (!regdb_key_is_base_key(key) && !regdb_key_exists(regdb, key)) {
1131 werr = WERR_NOT_FOUND;
1132 goto done;
1135 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
1136 W_ERROR_NOT_OK_GOTO_DONE(werr);
1138 werr = regdb_fetch_keys_internal(regdb, key, subkeys);
1139 W_ERROR_NOT_OK_GOTO_DONE(werr);
1141 if (regsubkey_ctr_key_exists(subkeys, subkey)) {
1142 werr = WERR_OK;
1143 goto done;
1146 talloc_free(subkeys);
1148 create_ctx.key = key;
1149 create_ctx.subkey = subkey;
1151 werr = ntstatus_to_werror(dbwrap_trans_do(regdb,
1152 regdb_create_subkey_action,
1153 &create_ctx));
1155 done:
1156 talloc_free(mem_ctx);
1157 return werr;
1161 * create a subkey of a given key
1164 struct regdb_delete_subkey_context {
1165 const char *key;
1166 const char *subkey;
1167 const char *path;
1170 static NTSTATUS regdb_delete_subkey_action(struct db_context *db,
1171 void *private_data)
1173 WERROR werr;
1174 struct regdb_delete_subkey_context *delete_ctx;
1175 struct regsubkey_ctr *subkeys;
1176 TALLOC_CTX *mem_ctx = talloc_stackframe();
1178 delete_ctx = (struct regdb_delete_subkey_context *)private_data;
1180 werr = regdb_delete_key_lists(db, delete_ctx->path);
1181 W_ERROR_NOT_OK_GOTO_DONE(werr);
1183 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
1184 W_ERROR_NOT_OK_GOTO_DONE(werr);
1186 werr = regdb_fetch_keys_internal(db, delete_ctx->key, subkeys);
1187 W_ERROR_NOT_OK_GOTO_DONE(werr);
1189 werr = regsubkey_ctr_delkey(subkeys, delete_ctx->subkey);
1190 W_ERROR_NOT_OK_GOTO_DONE(werr);
1192 werr = regdb_store_keys_internal2(db, delete_ctx->key, subkeys);
1193 if (!W_ERROR_IS_OK(werr)) {
1194 DEBUG(0, (__location__ " failed to store new subkey_list for "
1195 "parent key %s: %s\n", delete_ctx->key,
1196 win_errstr(werr)));
1199 done:
1200 talloc_free(mem_ctx);
1201 return werror_to_ntstatus(werr);
1204 static WERROR regdb_delete_subkey(const char *key, const char *subkey)
1206 WERROR werr;
1207 char *path;
1208 struct regdb_delete_subkey_context delete_ctx;
1209 TALLOC_CTX *mem_ctx = talloc_stackframe();
1211 if (!regdb_key_is_base_key(key) && !regdb_key_exists(regdb, key)) {
1212 werr = WERR_NOT_FOUND;
1213 goto done;
1216 path = talloc_asprintf(mem_ctx, "%s\\%s", key, subkey);
1217 if (path == NULL) {
1218 werr = WERR_NOMEM;
1219 goto done;
1222 if (!regdb_key_exists(regdb, path)) {
1223 werr = WERR_OK;
1224 goto done;
1227 delete_ctx.key = key;
1228 delete_ctx.subkey = subkey;
1229 delete_ctx.path = path;
1231 werr = ntstatus_to_werror(dbwrap_trans_do(regdb,
1232 regdb_delete_subkey_action,
1233 &delete_ctx));
1235 done:
1236 talloc_free(mem_ctx);
1237 return werr;
1240 static TDB_DATA regdb_fetch_key_internal(struct db_context *db,
1241 TALLOC_CTX *mem_ctx, const char *key)
1243 char *path = NULL;
1244 TDB_DATA data;
1246 path = normalize_reg_path(mem_ctx, key);
1247 if (!path) {
1248 return make_tdb_data(NULL, 0);
1251 data = dbwrap_fetch_bystring(db, mem_ctx, path);
1253 TALLOC_FREE(path);
1254 return data;
1259 * check whether a given key name represents a base key,
1260 * i.e one without a subkey separator ('\').
1262 static bool regdb_key_is_base_key(const char *key)
1264 TALLOC_CTX *mem_ctx = talloc_stackframe();
1265 bool ret = false;
1266 char *path;
1268 if (key == NULL) {
1269 goto done;
1272 path = normalize_reg_path(mem_ctx, key);
1273 if (path == NULL) {
1274 DEBUG(0, ("out of memory! (talloc failed)\n"));
1275 goto done;
1278 if (*path == '\0') {
1279 goto done;
1282 ret = (strrchr(path, '\\') == NULL);
1284 done:
1285 TALLOC_FREE(mem_ctx);
1286 return ret;
1290 * regdb_key_exists() is a very frequent operation. It can be quite
1291 * time-consuming to fully fetch the parent's subkey list, talloc_strdup all
1292 * subkeys and then compare the keyname linearly to all the parent's subkeys.
1294 * The following code tries to make this operation as efficient as possible:
1295 * Per registry key we create a list of subkeys that is very efficient to
1296 * search for existence of a subkey. Its format is:
1298 * 4 bytes num_subkeys
1299 * 4*num_subkey bytes offset into the string array
1300 * then follows a sorted list of subkeys in uppercase
1302 * This record is created by create_sorted_subkeys() on demand if it does not
1303 * exist. scan_parent_subkeys() uses regdb->parse_record to search the sorted
1304 * list, the parsing code and the binary search can be found in
1305 * parent_subkey_scanner. The code uses parse_record() to avoid a memcpy of
1306 * the potentially large subkey record.
1308 * The sorted subkey record is deleted in regdb_store_keys_internal2 and
1309 * recreated on demand.
1312 static int cmp_keynames(char **p1, char **p2)
1314 return StrCaseCmp(*p1, *p2);
1317 struct create_sorted_subkeys_context {
1318 const char *key;
1319 const char *sorted_keyname;
1322 static NTSTATUS create_sorted_subkeys_action(struct db_context *db,
1323 void *private_data)
1325 char **sorted_subkeys;
1326 struct regsubkey_ctr *ctr;
1327 NTSTATUS status;
1328 char *buf;
1329 char *p;
1330 int i;
1331 size_t len;
1332 int num_subkeys;
1333 struct create_sorted_subkeys_context *sorted_ctx;
1335 sorted_ctx = (struct create_sorted_subkeys_context *)private_data;
1338 * In this function, we only treat failing of the actual write to
1339 * the db as a real error. All preliminary errors, at a stage when
1340 * nothing has been written to the DB yet are treated as success
1341 * to be committed (as an empty transaction).
1343 * The reason is that this (disposable) call might be nested in other
1344 * transactions. Doing a cancel here would destroy the possibility of
1345 * a transaction_commit for transactions that we might be wrapped in.
1348 status = werror_to_ntstatus(regsubkey_ctr_init(talloc_tos(), &ctr));
1349 if (!NT_STATUS_IS_OK(status)) {
1350 /* don't treat this as an error */
1351 status = NT_STATUS_OK;
1352 goto done;
1355 status = werror_to_ntstatus(regdb_fetch_keys_internal(db,
1356 sorted_ctx->key,
1357 ctr));
1358 if (!NT_STATUS_IS_OK(status)) {
1359 /* don't treat this as an error */
1360 status = NT_STATUS_OK;
1361 goto done;
1364 num_subkeys = regsubkey_ctr_numkeys(ctr);
1365 sorted_subkeys = talloc_array(ctr, char *, num_subkeys);
1366 if (sorted_subkeys == NULL) {
1367 /* don't treat this as an error */
1368 goto done;
1371 len = 4 + 4*num_subkeys;
1373 for (i = 0; i < num_subkeys; i++) {
1374 sorted_subkeys[i] = talloc_strdup_upper(sorted_subkeys,
1375 regsubkey_ctr_specific_key(ctr, i));
1376 if (sorted_subkeys[i] == NULL) {
1377 /* don't treat this as an error */
1378 goto done;
1380 len += strlen(sorted_subkeys[i])+1;
1383 TYPESAFE_QSORT(sorted_subkeys, num_subkeys, cmp_keynames);
1385 buf = talloc_array(ctr, char, len);
1386 if (buf == NULL) {
1387 /* don't treat this as an error */
1388 goto done;
1390 p = buf + 4 + 4*num_subkeys;
1392 SIVAL(buf, 0, num_subkeys);
1394 for (i=0; i < num_subkeys; i++) {
1395 ptrdiff_t offset = p - buf;
1396 SIVAL(buf, 4 + 4*i, offset);
1397 strlcpy(p, sorted_subkeys[i], len-offset);
1398 p += strlen(sorted_subkeys[i]) + 1;
1401 status = dbwrap_store_bystring(
1402 db, sorted_ctx->sorted_keyname, make_tdb_data((uint8_t *)buf,
1403 len),
1404 TDB_REPLACE);
1406 done:
1407 talloc_free(ctr);
1408 return status;
1411 static NTSTATUS create_sorted_subkeys_internal(const char *key,
1412 const char *sorted_keyname)
1414 NTSTATUS status;
1415 struct create_sorted_subkeys_context sorted_ctx;
1417 sorted_ctx.key = key;
1418 sorted_ctx.sorted_keyname = sorted_keyname;
1420 status = dbwrap_trans_do(regdb,
1421 create_sorted_subkeys_action,
1422 &sorted_ctx);
1424 return status;
1427 struct scan_subkey_state {
1428 char *name;
1429 bool scanned;
1430 bool found;
1433 static int parent_subkey_scanner(TDB_DATA key, TDB_DATA data,
1434 void *private_data)
1436 struct scan_subkey_state *state =
1437 (struct scan_subkey_state *)private_data;
1438 uint32_t num_subkeys;
1439 uint32_t l, u;
1441 if (data.dsize < sizeof(uint32_t)) {
1442 return -1;
1445 state->scanned = true;
1446 state->found = false;
1448 tdb_unpack(data.dptr, data.dsize, "d", &num_subkeys);
1450 l = 0;
1451 u = num_subkeys;
1453 while (l < u) {
1454 uint32_t idx = (l+u)/2;
1455 char *s = (char *)data.dptr + IVAL(data.dptr, 4 + 4*idx);
1456 int comparison = strcmp(state->name, s);
1458 if (comparison < 0) {
1459 u = idx;
1460 } else if (comparison > 0) {
1461 l = idx + 1;
1462 } else {
1463 state->found = true;
1464 return 0;
1467 return 0;
1470 static bool scan_parent_subkeys(struct db_context *db, const char *parent,
1471 const char *name)
1473 char *path = NULL;
1474 char *key = NULL;
1475 struct scan_subkey_state state = { 0, };
1476 bool result = false;
1477 int res;
1479 state.name = NULL;
1481 path = normalize_reg_path(talloc_tos(), parent);
1482 if (path == NULL) {
1483 goto fail;
1486 key = talloc_asprintf(talloc_tos(), "%s\\%s",
1487 REG_SORTED_SUBKEYS_PREFIX, path);
1488 if (key == NULL) {
1489 goto fail;
1492 state.name = talloc_strdup_upper(talloc_tos(), name);
1493 if (state.name == NULL) {
1494 goto fail;
1496 state.scanned = false;
1498 res = db->parse_record(db, string_term_tdb_data(key),
1499 parent_subkey_scanner, &state);
1501 if (state.scanned) {
1502 result = state.found;
1503 } else {
1504 NTSTATUS status;
1506 res = db->transaction_start(db);
1507 if (res != 0) {
1508 DEBUG(0, ("error starting transaction\n"));
1509 goto fail;
1512 status = create_sorted_subkeys_internal(path, key);
1513 if (!NT_STATUS_IS_OK(status)) {
1514 res = db->transaction_cancel(db);
1515 if (res != 0) {
1516 smb_panic("Failed to cancel transaction.");
1518 goto fail;
1521 res = db->parse_record(db, string_term_tdb_data(key),
1522 parent_subkey_scanner, &state);
1523 if ((res == 0) && (state.scanned)) {
1524 result = state.found;
1527 res = db->transaction_commit(db);
1528 if (res != 0) {
1529 DEBUG(0, ("error committing transaction\n"));
1530 result = false;
1534 fail:
1535 TALLOC_FREE(path);
1536 TALLOC_FREE(state.name);
1537 return result;
1541 * Check for the existence of a key.
1543 * Existence of a key is authoritatively defined by its
1544 * existence in the list of subkeys of its parent key.
1545 * The exeption of this are keys without a parent key,
1546 * i.e. the "base" keys (HKLM, HKCU, ...).
1548 static bool regdb_key_exists(struct db_context *db, const char *key)
1550 TALLOC_CTX *mem_ctx = talloc_stackframe();
1551 TDB_DATA value;
1552 bool ret = false;
1553 char *path, *p;
1555 if (key == NULL) {
1556 goto done;
1559 path = normalize_reg_path(mem_ctx, key);
1560 if (path == NULL) {
1561 DEBUG(0, ("out of memory! (talloc failed)\n"));
1562 goto done;
1565 if (*path == '\0') {
1566 goto done;
1569 p = strrchr(path, '\\');
1570 if (p == NULL) {
1571 /* this is a base key */
1572 value = regdb_fetch_key_internal(db, mem_ctx, path);
1573 ret = (value.dptr != NULL);
1574 } else {
1575 *p = '\0';
1576 ret = scan_parent_subkeys(db, path, p+1);
1579 done:
1580 TALLOC_FREE(mem_ctx);
1581 return ret;
1585 /***********************************************************************
1586 Retrieve an array of strings containing subkeys. Memory should be
1587 released by the caller.
1588 ***********************************************************************/
1590 static WERROR regdb_fetch_keys_internal(struct db_context *db, const char *key,
1591 struct regsubkey_ctr *ctr)
1593 WERROR werr;
1594 uint32_t num_items;
1595 uint8 *buf;
1596 uint32 buflen, len;
1597 int i;
1598 fstring subkeyname;
1599 TALLOC_CTX *frame = talloc_stackframe();
1600 TDB_DATA value;
1602 DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key ? key : "NULL"));
1604 if (!regdb_key_exists(db, key)) {
1605 DEBUG(10, ("key [%s] not found\n", key));
1606 werr = WERR_NOT_FOUND;
1607 goto done;
1610 werr = regsubkey_ctr_set_seqnum(ctr, db->get_seqnum(db));
1611 W_ERROR_NOT_OK_GOTO_DONE(werr);
1613 value = regdb_fetch_key_internal(db, frame, key);
1615 if (value.dsize == 0 || value.dptr == NULL) {
1616 DEBUG(10, ("regdb_fetch_keys: no subkeys found for key [%s]\n",
1617 key));
1618 goto done;
1621 buf = value.dptr;
1622 buflen = value.dsize;
1623 len = tdb_unpack( buf, buflen, "d", &num_items);
1624 if (len == (uint32_t)-1) {
1625 werr = WERR_NOT_FOUND;
1626 goto done;
1629 werr = regsubkey_ctr_reinit(ctr);
1630 W_ERROR_NOT_OK_GOTO_DONE(werr);
1632 for (i=0; i<num_items; i++) {
1633 len += tdb_unpack(buf+len, buflen-len, "f", subkeyname);
1634 werr = regsubkey_ctr_addkey(ctr, subkeyname);
1635 if (!W_ERROR_IS_OK(werr)) {
1636 DEBUG(5, ("regdb_fetch_keys: regsubkey_ctr_addkey "
1637 "failed: %s\n", win_errstr(werr)));
1638 num_items = 0;
1639 goto done;
1643 DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items));
1645 done:
1646 TALLOC_FREE(frame);
1647 return werr;
1650 int regdb_fetch_keys(const char *key, struct regsubkey_ctr *ctr)
1652 WERROR werr;
1654 werr = regdb_fetch_keys_internal(regdb, key, ctr);
1655 if (!W_ERROR_IS_OK(werr)) {
1656 return -1;
1659 return regsubkey_ctr_numkeys(ctr);
1662 /****************************************************************************
1663 Unpack a list of registry values frem the TDB
1664 ***************************************************************************/
1666 static int regdb_unpack_values(struct regval_ctr *values, uint8 *buf, int buflen)
1668 int len = 0;
1669 uint32 type;
1670 fstring valuename;
1671 uint32 size;
1672 uint8 *data_p;
1673 uint32 num_values = 0;
1674 int i;
1676 /* loop and unpack the rest of the registry values */
1678 len += tdb_unpack(buf+len, buflen-len, "d", &num_values);
1680 for ( i=0; i<num_values; i++ ) {
1681 /* unpack the next regval */
1683 type = REG_NONE;
1684 size = 0;
1685 data_p = NULL;
1686 valuename[0] = '\0';
1687 len += tdb_unpack(buf+len, buflen-len, "fdB",
1688 valuename,
1689 &type,
1690 &size,
1691 &data_p);
1693 regval_ctr_addvalue(values, valuename, type,
1694 (uint8_t *)data_p, size);
1695 SAFE_FREE(data_p); /* 'B' option to tdb_unpack does a malloc() */
1697 DEBUG(8,("specific: [%s], len: %d\n", valuename, size));
1700 return len;
1703 /****************************************************************************
1704 Pack all values in all printer keys
1705 ***************************************************************************/
1707 static int regdb_pack_values(struct regval_ctr *values, uint8 *buf, int buflen)
1709 int len = 0;
1710 int i;
1711 struct regval_blob *val;
1712 int num_values;
1714 if ( !values )
1715 return 0;
1717 num_values = regval_ctr_numvals( values );
1719 /* pack the number of values first */
1721 len += tdb_pack( buf+len, buflen-len, "d", num_values );
1723 /* loop over all values */
1725 for ( i=0; i<num_values; i++ ) {
1726 val = regval_ctr_specific_value( values, i );
1727 len += tdb_pack(buf+len, buflen-len, "fdB",
1728 regval_name(val),
1729 regval_type(val),
1730 regval_size(val),
1731 regval_data_p(val) );
1734 return len;
1737 /***********************************************************************
1738 Retrieve an array of strings containing subkeys. Memory should be
1739 released by the caller.
1740 ***********************************************************************/
1742 static int regdb_fetch_values_internal(struct db_context *db, const char* key,
1743 struct regval_ctr *values)
1745 char *keystr = NULL;
1746 TALLOC_CTX *ctx = talloc_stackframe();
1747 int ret = 0;
1748 TDB_DATA value;
1749 WERROR werr;
1751 DEBUG(10,("regdb_fetch_values: Looking for value of key [%s] \n", key));
1753 if (!regdb_key_exists(db, key)) {
1754 goto done;
1757 keystr = talloc_asprintf(ctx, "%s\\%s", REG_VALUE_PREFIX, key);
1758 if (!keystr) {
1759 goto done;
1762 werr = regval_ctr_set_seqnum(values, db->get_seqnum(db));
1763 W_ERROR_NOT_OK_GOTO_DONE(werr);
1765 value = regdb_fetch_key_internal(db, ctx, keystr);
1767 if (!value.dptr) {
1768 /* all keys have zero values by default */
1769 goto done;
1772 regdb_unpack_values(values, value.dptr, value.dsize);
1773 ret = regval_ctr_numvals(values);
1775 done:
1776 TALLOC_FREE(ctx);
1777 return ret;
1780 int regdb_fetch_values(const char* key, struct regval_ctr *values)
1782 return regdb_fetch_values_internal(regdb, key, values);
1785 static bool regdb_store_values_internal(struct db_context *db, const char *key,
1786 struct regval_ctr *values)
1788 TDB_DATA old_data, data;
1789 char *keystr = NULL;
1790 TALLOC_CTX *ctx = talloc_stackframe();
1791 int len;
1792 NTSTATUS status;
1793 bool result = false;
1795 DEBUG(10,("regdb_store_values: Looking for value of key [%s] \n", key));
1797 if (!regdb_key_exists(db, key)) {
1798 goto done;
1801 ZERO_STRUCT(data);
1803 len = regdb_pack_values(values, data.dptr, data.dsize);
1804 if (len <= 0) {
1805 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
1806 goto done;
1809 data.dptr = TALLOC_ARRAY(ctx, uint8, len);
1810 data.dsize = len;
1812 len = regdb_pack_values(values, data.dptr, data.dsize);
1814 SMB_ASSERT( len == data.dsize );
1816 keystr = talloc_asprintf(ctx, "%s\\%s", REG_VALUE_PREFIX, key );
1817 if (!keystr) {
1818 goto done;
1820 keystr = normalize_reg_path(ctx, keystr);
1821 if (!keystr) {
1822 goto done;
1825 old_data = dbwrap_fetch_bystring(db, ctx, keystr);
1827 if ((old_data.dptr != NULL)
1828 && (old_data.dsize == data.dsize)
1829 && (memcmp(old_data.dptr, data.dptr, data.dsize) == 0))
1831 result = true;
1832 goto done;
1835 status = dbwrap_trans_store_bystring(db, keystr, data, TDB_REPLACE);
1837 result = NT_STATUS_IS_OK(status);
1839 done:
1840 TALLOC_FREE(ctx);
1841 return result;
1844 bool regdb_store_values(const char *key, struct regval_ctr *values)
1846 return regdb_store_values_internal(regdb, key, values);
1849 static WERROR regdb_get_secdesc(TALLOC_CTX *mem_ctx, const char *key,
1850 struct security_descriptor **psecdesc)
1852 char *tdbkey;
1853 TDB_DATA data;
1854 NTSTATUS status;
1855 TALLOC_CTX *tmp_ctx = talloc_stackframe();
1856 WERROR err = WERR_OK;
1858 DEBUG(10, ("regdb_get_secdesc: Getting secdesc of key [%s]\n", key));
1860 if (!regdb_key_exists(regdb, key)) {
1861 err = WERR_BADFILE;
1862 goto done;
1865 tdbkey = talloc_asprintf(tmp_ctx, "%s\\%s", REG_SECDESC_PREFIX, key);
1866 if (tdbkey == NULL) {
1867 err = WERR_NOMEM;
1868 goto done;
1871 tdbkey = normalize_reg_path(tmp_ctx, tdbkey);
1872 if (tdbkey == NULL) {
1873 err = WERR_NOMEM;
1874 goto done;
1877 data = dbwrap_fetch_bystring(regdb, tmp_ctx, tdbkey);
1878 if (data.dptr == NULL) {
1879 err = WERR_BADFILE;
1880 goto done;
1883 status = unmarshall_sec_desc(mem_ctx, (uint8 *)data.dptr, data.dsize,
1884 psecdesc);
1886 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MEMORY)) {
1887 err = WERR_NOMEM;
1888 } else if (!NT_STATUS_IS_OK(status)) {
1889 err = WERR_REG_CORRUPT;
1892 done:
1893 TALLOC_FREE(tmp_ctx);
1894 return err;
1897 static WERROR regdb_set_secdesc(const char *key,
1898 struct security_descriptor *secdesc)
1900 TALLOC_CTX *mem_ctx = talloc_stackframe();
1901 char *tdbkey;
1902 WERROR err = WERR_NOMEM;
1903 TDB_DATA tdbdata;
1905 if (!regdb_key_exists(regdb, key)) {
1906 err = WERR_BADFILE;
1907 goto done;
1910 tdbkey = talloc_asprintf(mem_ctx, "%s\\%s", REG_SECDESC_PREFIX, key);
1911 if (tdbkey == NULL) {
1912 goto done;
1915 tdbkey = normalize_reg_path(mem_ctx, tdbkey);
1916 if (tdbkey == NULL) {
1917 err = WERR_NOMEM;
1918 goto done;
1921 if (secdesc == NULL) {
1922 /* assuming a delete */
1923 err = ntstatus_to_werror(dbwrap_trans_delete_bystring(regdb,
1924 tdbkey));
1925 goto done;
1928 err = ntstatus_to_werror(marshall_sec_desc(mem_ctx, secdesc,
1929 &tdbdata.dptr,
1930 &tdbdata.dsize));
1931 W_ERROR_NOT_OK_GOTO_DONE(err);
1933 err = ntstatus_to_werror(dbwrap_trans_store_bystring(regdb, tdbkey,
1934 tdbdata, 0));
1936 done:
1937 TALLOC_FREE(mem_ctx);
1938 return err;
1941 bool regdb_subkeys_need_update(struct regsubkey_ctr *subkeys)
1943 return (regdb_get_seqnum() != regsubkey_ctr_get_seqnum(subkeys));
1946 bool regdb_values_need_update(struct regval_ctr *values)
1948 return (regdb_get_seqnum() != regval_ctr_get_seqnum(values));
1952 * Table of function pointers for default access
1955 struct registry_ops regdb_ops = {
1956 .fetch_subkeys = regdb_fetch_keys,
1957 .fetch_values = regdb_fetch_values,
1958 .store_subkeys = regdb_store_keys,
1959 .store_values = regdb_store_values,
1960 .create_subkey = regdb_create_subkey,
1961 .delete_subkey = regdb_delete_subkey,
1962 .get_secdesc = regdb_get_secdesc,
1963 .set_secdesc = regdb_set_secdesc,
1964 .subkeys_need_update = regdb_subkeys_need_update,
1965 .values_need_update = regdb_values_need_update