s4:password_hash LDB module - return "ERR_CONSTRAINT_VIOLATION" on password conversio...
[Samba/wip.git] / source3 / registry / reg_backend_db.c
blob322cabf564c6e94d3f506687c589f51d242f2f18
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"
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 "dbwrap.h"
33 #undef DBGC_CLASS
34 #define DBGC_CLASS DBGC_REGISTRY
36 static struct db_context *regdb = NULL;
37 static int regdb_refcount;
39 static bool regdb_key_exists(struct db_context *db, const char *key);
40 static bool regdb_key_is_base_key(const char *key);
41 static WERROR regdb_fetch_keys_internal(struct db_context *db, const char *key,
42 struct regsubkey_ctr *ctr);
43 static bool regdb_store_keys_internal(struct db_context *db, const char *key,
44 struct regsubkey_ctr *ctr);
45 static int regdb_fetch_values_internal(struct db_context *db, const char* key,
46 struct regval_ctr *values);
47 static bool regdb_store_values_internal(struct db_context *db, const char *key,
48 struct regval_ctr *values);
50 /* List the deepest path into the registry. All part components will be created.*/
52 /* If you want to have a part of the path controlled by the tdb and part by
53 a virtual registry db (e.g. printing), then you have to list the deepest path.
54 For example,"HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion/Print"
55 allows the reg_db backend to handle everything up to
56 "HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion" and then we'll hook
57 the reg_printing backend onto the last component of the path (see
58 KEY_PRINTING_2K in include/rpc_reg.h) --jerry */
60 static const char *builtin_registry_paths[] = {
61 KEY_PRINTING_2K,
62 KEY_PRINTING_PORTS,
63 KEY_PRINTING,
64 KEY_PRINTING "\\Forms",
65 KEY_PRINTING "\\Printers",
66 KEY_PRINTING "\\Environments\\Windows NT x86\\Print Processors\\winprint",
67 KEY_SHARES,
68 KEY_EVENTLOG,
69 KEY_SMBCONF,
70 KEY_PERFLIB,
71 KEY_PERFLIB_009,
72 KEY_GROUP_POLICY,
73 KEY_SAMBA_GROUP_POLICY,
74 KEY_GP_MACHINE_POLICY,
75 KEY_GP_MACHINE_WIN_POLICY,
76 KEY_HKCU,
77 KEY_GP_USER_POLICY,
78 KEY_GP_USER_WIN_POLICY,
79 "HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon\\GPExtensions",
80 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Print\\Monitors",
81 KEY_PROD_OPTIONS,
82 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\DefaultUserConfiguration",
83 KEY_TCPIP_PARAMS,
84 KEY_NETLOGON_PARAMS,
85 KEY_HKU,
86 KEY_HKCR,
87 KEY_HKPD,
88 KEY_HKPT,
89 NULL };
91 struct builtin_regkey_value {
92 const char *path;
93 const char *valuename;
94 uint32 type;
95 union {
96 const char *string;
97 uint32 dw_value;
98 } data;
101 static struct builtin_regkey_value builtin_registry_values[] = {
102 { KEY_PRINTING_PORTS,
103 SAMBA_PRINTER_PORT_NAME, REG_SZ, { "" } },
104 { KEY_PRINTING_2K,
105 "DefaultSpoolDirectory", REG_SZ, { "C:\\Windows\\System32\\Spool\\Printers" } },
106 { KEY_EVENTLOG,
107 "DisplayName", REG_SZ, { "Event Log" } },
108 { KEY_EVENTLOG,
109 "ErrorControl", REG_DWORD, { (char*)0x00000001 } },
110 { NULL, NULL, 0, { NULL } }
114 * Initialize a key in the registry:
115 * create each component key of the specified path.
117 static WERROR init_registry_key_internal(struct db_context *db,
118 const char *add_path)
120 WERROR werr;
121 TALLOC_CTX *frame = talloc_stackframe();
122 char *path = NULL;
123 char *base = NULL;
124 char *remaining = NULL;
125 char *keyname;
126 char *subkeyname;
127 struct regsubkey_ctr *subkeys;
128 const char *p, *p2;
130 DEBUG(6, ("init_registry_key: Adding [%s]\n", add_path));
132 path = talloc_strdup(frame, add_path);
133 base = talloc_strdup(frame, "");
134 if (!path || !base) {
135 werr = WERR_NOMEM;
136 goto fail;
138 p = path;
140 while (next_token_talloc(frame, &p, &keyname, "\\")) {
142 /* build up the registry path from the components */
144 if (*base) {
145 base = talloc_asprintf(frame, "%s\\", base);
146 if (!base) {
147 werr = WERR_NOMEM;
148 goto fail;
151 base = talloc_asprintf_append(base, "%s", keyname);
152 if (!base) {
153 werr = WERR_NOMEM;
154 goto fail;
157 /* get the immediate subkeyname (if we have one ) */
159 subkeyname = talloc_strdup(frame, "");
160 if (!subkeyname) {
161 werr = WERR_NOMEM;
162 goto fail;
164 if (*p) {
165 remaining = talloc_strdup(frame, p);
166 if (!remaining) {
167 werr = WERR_NOMEM;
168 goto fail;
170 p2 = remaining;
172 if (!next_token_talloc(frame, &p2,
173 &subkeyname, "\\"))
175 subkeyname = talloc_strdup(frame,p2);
176 if (!subkeyname) {
177 werr = WERR_NOMEM;
178 goto fail;
183 DEBUG(10,("init_registry_key: Storing key [%s] with "
184 "subkey [%s]\n", base,
185 *subkeyname ? subkeyname : "NULL"));
187 /* we don't really care if the lookup succeeds or not
188 * since we are about to update the record.
189 * We just want any subkeys already present */
191 werr = regsubkey_ctr_init(frame, &subkeys);
192 if (!W_ERROR_IS_OK(werr)) {
193 DEBUG(0,("talloc() failure!\n"));
194 goto fail;
197 werr = regdb_fetch_keys_internal(db, base, subkeys);
198 if (!W_ERROR_IS_OK(werr) &&
199 !W_ERROR_EQUAL(werr, WERR_NOT_FOUND))
201 goto fail;
204 if (*subkeyname) {
205 werr = regsubkey_ctr_addkey(subkeys, subkeyname);
206 if (!W_ERROR_IS_OK(werr)) {
207 goto fail;
210 if (!regdb_store_keys_internal(db, base, subkeys)) {
211 werr = WERR_CAN_NOT_COMPLETE;
212 goto fail;
216 werr = WERR_OK;
218 fail:
219 TALLOC_FREE(frame);
220 return werr;
223 struct init_registry_key_context {
224 const char *add_path;
227 static NTSTATUS init_registry_key_action(struct db_context *db,
228 void *private_data)
230 struct init_registry_key_context *init_ctx =
231 (struct init_registry_key_context *)private_data;
233 return werror_to_ntstatus(init_registry_key_internal(
234 db, init_ctx->add_path));
238 * Initialize a key in the registry:
239 * create each component key of the specified path,
240 * wrapped in one db transaction.
242 WERROR init_registry_key(const char *add_path)
244 struct init_registry_key_context init_ctx;
246 if (regdb_key_exists(regdb, add_path)) {
247 return WERR_OK;
250 init_ctx.add_path = add_path;
252 return ntstatus_to_werror(dbwrap_trans_do(regdb,
253 init_registry_key_action,
254 &init_ctx));
257 /***********************************************************************
258 Open the registry data in the tdb
259 ***********************************************************************/
261 static void regdb_ctr_add_value(struct regval_ctr *ctr,
262 struct builtin_regkey_value *value)
264 switch(value->type) {
265 case REG_DWORD:
266 regval_ctr_addvalue(ctr, value->valuename, REG_DWORD,
267 (uint8_t *)&value->data.dw_value,
268 sizeof(uint32));
269 break;
271 case REG_SZ:
272 regval_ctr_addvalue_sz(ctr, value->valuename,
273 value->data.string);
274 break;
276 default:
277 DEBUG(0, ("regdb_ctr_add_value: invalid value type in "
278 "registry values [%d]\n", value->type));
282 static NTSTATUS init_registry_data_action(struct db_context *db,
283 void *private_data)
285 NTSTATUS status;
286 TALLOC_CTX *frame = talloc_stackframe();
287 struct regval_ctr *values;
288 int i;
290 /* loop over all of the predefined paths and add each component */
292 for (i=0; builtin_registry_paths[i] != NULL; i++) {
293 if (regdb_key_exists(db, builtin_registry_paths[i])) {
294 continue;
296 status = werror_to_ntstatus(init_registry_key_internal(db,
297 builtin_registry_paths[i]));
298 if (!NT_STATUS_IS_OK(status)) {
299 goto done;
303 /* loop over all of the predefined values and add each component */
305 for (i=0; builtin_registry_values[i].path != NULL; i++) {
306 WERROR werr;
308 werr = regval_ctr_init(frame, &values);
309 if (!W_ERROR_IS_OK(werr)) {
310 status = werror_to_ntstatus(werr);
311 goto done;
314 regdb_fetch_values_internal(db,
315 builtin_registry_values[i].path,
316 values);
318 /* preserve existing values across restarts. Only add new ones */
320 if (!regval_ctr_key_exists(values,
321 builtin_registry_values[i].valuename))
323 regdb_ctr_add_value(values,
324 &builtin_registry_values[i]);
325 regdb_store_values_internal(db,
326 builtin_registry_values[i].path,
327 values);
329 TALLOC_FREE(values);
332 status = NT_STATUS_OK;
334 done:
336 TALLOC_FREE(frame);
337 return status;
340 WERROR init_registry_data(void)
342 WERROR werr;
343 TALLOC_CTX *frame = talloc_stackframe();
344 struct regval_ctr *values;
345 int i;
348 * First, check for the existence of the needed keys and values.
349 * If all do already exist, we can save the writes.
351 for (i=0; builtin_registry_paths[i] != NULL; i++) {
352 if (!regdb_key_exists(regdb, builtin_registry_paths[i])) {
353 goto do_init;
357 for (i=0; builtin_registry_values[i].path != NULL; i++) {
358 werr = regval_ctr_init(frame, &values);
359 W_ERROR_NOT_OK_GOTO_DONE(werr);
361 regdb_fetch_values_internal(regdb,
362 builtin_registry_values[i].path,
363 values);
364 if (!regval_ctr_key_exists(values,
365 builtin_registry_values[i].valuename))
367 TALLOC_FREE(values);
368 goto do_init;
371 TALLOC_FREE(values);
374 werr = WERR_OK;
375 goto done;
377 do_init:
380 * There are potentially quite a few store operations which are all
381 * indiviually wrapped in tdb transactions. Wrapping them in a single
382 * transaction gives just a single transaction_commit() to actually do
383 * its fsync()s. See tdb/common/transaction.c for info about nested
384 * transaction behaviour.
387 werr = ntstatus_to_werror(dbwrap_trans_do(regdb,
388 init_registry_data_action,
389 NULL));
391 done:
392 TALLOC_FREE(frame);
393 return werr;
396 static int regdb_normalize_keynames_fn(struct db_record *rec,
397 void *private_data)
399 TALLOC_CTX *mem_ctx = talloc_tos();
400 const char *keyname;
401 NTSTATUS status;
403 if (rec->key.dptr == NULL || rec->key.dsize == 0) {
404 return 0;
407 keyname = strchr((const char *) rec->key.dptr, '/');
408 if (keyname) {
409 struct db_record new_rec;
411 keyname = talloc_string_sub(mem_ctx,
412 (const char *) rec->key.dptr,
413 "/",
414 "\\");
416 DEBUG(2, ("regdb_normalize_keynames_fn: Convert %s to %s\n",
417 (const char *) rec->key.dptr,
418 keyname));
420 new_rec.value = rec->value;
421 new_rec.key = string_term_tdb_data(keyname);
422 new_rec.private_data = rec->private_data;
424 /* Delete the original record and store the normalized key */
425 status = rec->delete_rec(rec);
426 if (!NT_STATUS_IS_OK(status)) {
427 DEBUG(0,("regdb_normalize_keynames_fn: "
428 "tdb_delete for [%s] failed!\n",
429 rec->key.dptr));
430 return 1;
433 status = rec->store(&new_rec, new_rec.value, TDB_REPLACE);
434 if (!NT_STATUS_IS_OK(status)) {
435 DEBUG(0,("regdb_normalize_keynames_fn: "
436 "failed to store new record for [%s]!\n",
437 keyname));
438 return 1;
442 return 0;
445 static WERROR regdb_store_regdb_version(uint32_t version)
447 NTSTATUS status;
448 const char *version_keyname = "INFO/version";
450 if (!regdb) {
451 return WERR_CAN_NOT_COMPLETE;
454 status = dbwrap_trans_store_int32(regdb, version_keyname, version);
455 if (!NT_STATUS_IS_OK(status)) {
456 DEBUG(1, ("regdb_init: error storing %s = %d: %s\n",
457 version_keyname, version, nt_errstr(status)));
458 return ntstatus_to_werror(status);
459 } else {
460 DEBUG(10, ("regdb_init: stored %s = %d\n",
461 version_keyname, version));
462 return WERR_OK;
466 static WERROR regdb_upgrade_v1_to_v2(void)
468 TALLOC_CTX *mem_ctx;
469 int rc;
470 WERROR werr;
472 mem_ctx = talloc_stackframe();
473 if (mem_ctx == NULL) {
474 return WERR_NOMEM;
477 rc = regdb->traverse(regdb, regdb_normalize_keynames_fn, mem_ctx);
479 talloc_destroy(mem_ctx);
481 if (rc == -1) {
482 return WERR_REG_IO_FAILURE;
485 werr = regdb_store_regdb_version(REGVER_V2);
486 return werr;
489 /***********************************************************************
490 Open the registry database
491 ***********************************************************************/
493 WERROR regdb_init(void)
495 const char *vstring = "INFO/version";
496 uint32 vers_id, expected_version;
497 WERROR werr;
499 if (regdb) {
500 DEBUG(10, ("regdb_init: incrementing refcount (%d)\n",
501 regdb_refcount));
502 regdb_refcount++;
503 return WERR_OK;
506 regdb = db_open(NULL, state_path("registry.tdb"), 0,
507 REG_TDB_FLAGS, O_RDWR, 0600);
508 if (!regdb) {
509 regdb = db_open(NULL, state_path("registry.tdb"), 0,
510 REG_TDB_FLAGS, O_RDWR|O_CREAT, 0600);
511 if (!regdb) {
512 werr = ntstatus_to_werror(map_nt_error_from_unix(errno));
513 DEBUG(1,("regdb_init: Failed to open registry %s (%s)\n",
514 state_path("registry.tdb"), strerror(errno) ));
515 return werr;
518 DEBUG(10,("regdb_init: Successfully created registry tdb\n"));
521 regdb_refcount = 1;
523 expected_version = REGVER_V2;
525 vers_id = dbwrap_fetch_int32(regdb, vstring);
526 if (vers_id == -1) {
527 DEBUG(10, ("regdb_init: registry version uninitialized "
528 "(got %d), initializing to version %d\n",
529 vers_id, expected_version));
531 werr = regdb_store_regdb_version(expected_version);
532 return werr;
535 if (vers_id > expected_version || vers_id == 0) {
536 DEBUG(1, ("regdb_init: unknown registry version %d "
537 "(code version = %d), refusing initialization\n",
538 vers_id, expected_version));
539 return WERR_CAN_NOT_COMPLETE;
542 if (vers_id == REGVER_V1) {
543 DEBUG(10, ("regdb_init: got registry db version %d, upgrading "
544 "to version %d\n", REGVER_V1, REGVER_V2));
546 if (regdb->transaction_start(regdb) != 0) {
547 return WERR_REG_IO_FAILURE;
550 werr = regdb_upgrade_v1_to_v2();
551 if (!W_ERROR_IS_OK(werr)) {
552 regdb->transaction_cancel(regdb);
553 return werr;
556 if (regdb->transaction_commit(regdb) != 0) {
557 return WERR_REG_IO_FAILURE;
560 vers_id = REGVER_V2;
563 /* future upgrade code should go here */
565 return WERR_OK;
568 /***********************************************************************
569 Open the registry. Must already have been initialized by regdb_init()
570 ***********************************************************************/
572 WERROR regdb_open( void )
574 WERROR result = WERR_OK;
576 if ( regdb ) {
577 DEBUG(10,("regdb_open: incrementing refcount (%d)\n", regdb_refcount));
578 regdb_refcount++;
579 return WERR_OK;
582 become_root();
584 regdb = db_open(NULL, state_path("registry.tdb"), 0,
585 REG_TDB_FLAGS, O_RDWR, 0600);
586 if ( !regdb ) {
587 result = ntstatus_to_werror( map_nt_error_from_unix( errno ) );
588 DEBUG(0,("regdb_open: Failed to open %s! (%s)\n",
589 state_path("registry.tdb"), strerror(errno) ));
592 unbecome_root();
594 regdb_refcount = 1;
595 DEBUG(10,("regdb_open: refcount reset (%d)\n", regdb_refcount));
597 return result;
600 /***********************************************************************
601 ***********************************************************************/
603 int regdb_close( void )
605 if (regdb_refcount == 0) {
606 return 0;
609 regdb_refcount--;
611 DEBUG(10,("regdb_close: decrementing refcount (%d)\n", regdb_refcount));
613 if ( regdb_refcount > 0 )
614 return 0;
616 SMB_ASSERT( regdb_refcount >= 0 );
618 TALLOC_FREE(regdb);
619 return 0;
622 WERROR regdb_transaction_start(void)
624 return (regdb->transaction_start(regdb) == 0) ?
625 WERR_OK : WERR_REG_IO_FAILURE;
628 WERROR regdb_transaction_commit(void)
630 return (regdb->transaction_commit(regdb) == 0) ?
631 WERR_OK : WERR_REG_IO_FAILURE;
634 WERROR regdb_transaction_cancel(void)
636 return (regdb->transaction_cancel(regdb) == 0) ?
637 WERR_OK : WERR_REG_IO_FAILURE;
640 /***********************************************************************
641 return the tdb sequence number of the registry tdb.
642 this is an indicator for the content of the registry
643 having changed. it will change upon regdb_init, too, though.
644 ***********************************************************************/
645 int regdb_get_seqnum(void)
647 return regdb->get_seqnum(regdb);
651 static WERROR regdb_delete_key_with_prefix(struct db_context *db,
652 const char *keyname,
653 const char *prefix)
655 char *path;
656 WERROR werr = WERR_NOMEM;
657 TALLOC_CTX *mem_ctx = talloc_stackframe();
659 if (keyname == NULL) {
660 werr = WERR_INVALID_PARAM;
661 goto done;
664 if (prefix == NULL) {
665 path = discard_const_p(char, keyname);
666 } else {
667 path = talloc_asprintf(mem_ctx, "%s\\%s", prefix, keyname);
668 if (path == NULL) {
669 goto done;
673 path = normalize_reg_path(mem_ctx, path);
674 if (path == NULL) {
675 goto done;
678 werr = ntstatus_to_werror(dbwrap_delete_bystring(db, path));
680 /* treat "not" found" as ok */
681 if (W_ERROR_EQUAL(werr, WERR_NOT_FOUND)) {
682 werr = WERR_OK;
685 done:
686 talloc_free(mem_ctx);
687 return werr;
691 static WERROR regdb_delete_values(struct db_context *db, const char *keyname)
693 return regdb_delete_key_with_prefix(db, keyname, REG_VALUE_PREFIX);
696 static WERROR regdb_delete_secdesc(struct db_context *db, const char *keyname)
698 return regdb_delete_key_with_prefix(db, keyname, REG_SECDESC_PREFIX);
701 static WERROR regdb_delete_subkeylist(struct db_context *db, const char *keyname)
703 return regdb_delete_key_with_prefix(db, keyname, NULL);
706 static WERROR regdb_delete_key_lists(struct db_context *db, const char *keyname)
708 WERROR werr;
710 werr = regdb_delete_values(db, keyname);
711 if (!W_ERROR_IS_OK(werr)) {
712 DEBUG(1, (__location__ " Deleting %s\\%s failed: %s\n",
713 REG_VALUE_PREFIX, keyname, win_errstr(werr)));
714 goto done;
717 werr = regdb_delete_secdesc(db, keyname);
718 if (!W_ERROR_IS_OK(werr)) {
719 DEBUG(1, (__location__ " Deleting %s\\%s failed: %s\n",
720 REG_SECDESC_PREFIX, keyname, win_errstr(werr)));
721 goto done;
724 werr = regdb_delete_subkeylist(db, keyname);
725 if (!W_ERROR_IS_OK(werr)) {
726 DEBUG(1, (__location__ " Deleting %s failed: %s\n",
727 keyname, win_errstr(werr)));
728 goto done;
731 done:
732 return werr;
735 /***********************************************************************
736 Add subkey strings to the registry tdb under a defined key
737 fmt is the same format as tdb_pack except this function only supports
738 fstrings
739 ***********************************************************************/
741 static WERROR regdb_store_keys_internal2(struct db_context *db,
742 const char *key,
743 struct regsubkey_ctr *ctr)
745 TDB_DATA dbuf;
746 uint8 *buffer = NULL;
747 int i = 0;
748 uint32 len, buflen;
749 uint32 num_subkeys = regsubkey_ctr_numkeys(ctr);
750 char *keyname = NULL;
751 TALLOC_CTX *ctx = talloc_stackframe();
752 WERROR werr;
754 if (!key) {
755 werr = WERR_INVALID_PARAM;
756 goto done;
759 keyname = talloc_strdup(ctx, key);
760 if (!keyname) {
761 werr = WERR_NOMEM;
762 goto done;
765 keyname = normalize_reg_path(ctx, keyname);
766 if (!keyname) {
767 werr = WERR_NOMEM;
768 goto done;
771 /* allocate some initial memory */
773 buffer = (uint8 *)SMB_MALLOC(1024);
774 if (buffer == NULL) {
775 werr = WERR_NOMEM;
776 goto done;
778 buflen = 1024;
779 len = 0;
781 /* store the number of subkeys */
783 len += tdb_pack(buffer+len, buflen-len, "d", num_subkeys);
785 /* pack all the strings */
787 for (i=0; i<num_subkeys; i++) {
788 size_t thistime;
790 thistime = tdb_pack(buffer+len, buflen-len, "f",
791 regsubkey_ctr_specific_key(ctr, i));
792 if (len+thistime > buflen) {
793 size_t thistime2;
795 * tdb_pack hasn't done anything because of the short
796 * buffer, allocate extra space.
798 buffer = SMB_REALLOC_ARRAY(buffer, uint8_t,
799 (len+thistime)*2);
800 if(buffer == NULL) {
801 DEBUG(0, ("regdb_store_keys: Failed to realloc "
802 "memory of size [%u]\n",
803 (unsigned int)(len+thistime)*2));
804 werr = WERR_NOMEM;
805 goto done;
807 buflen = (len+thistime)*2;
808 thistime2 = tdb_pack(
809 buffer+len, buflen-len, "f",
810 regsubkey_ctr_specific_key(ctr, i));
811 if (thistime2 != thistime) {
812 DEBUG(0, ("tdb_pack failed\n"));
813 werr = WERR_CAN_NOT_COMPLETE;
814 goto done;
817 len += thistime;
820 /* finally write out the data */
822 dbuf.dptr = buffer;
823 dbuf.dsize = len;
824 werr = ntstatus_to_werror(dbwrap_store_bystring(db, keyname, dbuf,
825 TDB_REPLACE));
826 W_ERROR_NOT_OK_GOTO_DONE(werr);
829 * Delete a sorted subkey cache for regdb_key_exists, will be
830 * recreated automatically
832 keyname = talloc_asprintf(ctx, "%s\\%s", REG_SORTED_SUBKEYS_PREFIX,
833 keyname);
834 if (keyname == NULL) {
835 werr = WERR_NOMEM;
836 goto done;
839 werr = ntstatus_to_werror(dbwrap_delete_bystring(db, keyname));
841 /* don't treat WERR_NOT_FOUND as an error here */
842 if (W_ERROR_EQUAL(werr, WERR_NOT_FOUND)) {
843 werr = WERR_OK;
846 done:
847 TALLOC_FREE(ctx);
848 SAFE_FREE(buffer);
849 return werr;
852 /***********************************************************************
853 Store the new subkey record and create any child key records that
854 do not currently exist
855 ***********************************************************************/
857 struct regdb_store_keys_context {
858 const char *key;
859 struct regsubkey_ctr *ctr;
862 static NTSTATUS regdb_store_keys_action(struct db_context *db,
863 void *private_data)
865 struct regdb_store_keys_context *store_ctx;
866 WERROR werr;
867 int num_subkeys, i;
868 char *path = NULL;
869 struct regsubkey_ctr *subkeys = NULL, *old_subkeys = NULL;
870 char *oldkeyname = NULL;
871 TALLOC_CTX *mem_ctx = talloc_stackframe();
873 store_ctx = (struct regdb_store_keys_context *)private_data;
876 * Re-fetch the old keys inside the transaction
879 werr = regsubkey_ctr_init(mem_ctx, &old_subkeys);
880 W_ERROR_NOT_OK_GOTO_DONE(werr);
882 werr = regdb_fetch_keys_internal(db, store_ctx->key, old_subkeys);
883 if (!W_ERROR_IS_OK(werr) &&
884 !W_ERROR_EQUAL(werr, WERR_NOT_FOUND))
886 goto done;
890 * Make the store operation as safe as possible without transactions:
892 * (1) For each subkey removed from ctr compared with old_subkeys:
894 * (a) First delete the value db entry.
896 * (b) Next delete the secdesc db record.
898 * (c) Then delete the subkey list entry.
900 * (2) Now write the list of subkeys of the parent key,
901 * deleting removed entries and adding new ones.
903 * (3) Finally create the subkey list entries for the added keys.
905 * This way if we crash half-way in between deleting the subkeys
906 * and storing the parent's list of subkeys, no old data can pop up
907 * out of the blue when re-adding keys later on.
910 /* (1) delete removed keys' lists (values/secdesc/subkeys) */
912 num_subkeys = regsubkey_ctr_numkeys(old_subkeys);
913 for (i=0; i<num_subkeys; i++) {
914 oldkeyname = regsubkey_ctr_specific_key(old_subkeys, i);
916 if (regsubkey_ctr_key_exists(store_ctx->ctr, oldkeyname)) {
918 * It's still around, don't delete
920 continue;
923 path = talloc_asprintf(mem_ctx, "%s\\%s", store_ctx->key,
924 oldkeyname);
925 if (!path) {
926 werr = WERR_NOMEM;
927 goto done;
930 werr = regdb_delete_key_lists(db, path);
931 W_ERROR_NOT_OK_GOTO_DONE(werr);
933 TALLOC_FREE(path);
936 TALLOC_FREE(old_subkeys);
938 /* (2) store the subkey list for the parent */
940 werr = regdb_store_keys_internal2(db, store_ctx->key, store_ctx->ctr);
941 if (!W_ERROR_IS_OK(werr)) {
942 DEBUG(0,("regdb_store_keys: Failed to store new subkey list "
943 "for parent [%s]: %s\n", store_ctx->key,
944 win_errstr(werr)));
945 goto done;
948 /* (3) now create records for any subkeys that don't already exist */
950 num_subkeys = regsubkey_ctr_numkeys(store_ctx->ctr);
952 if (num_subkeys == 0) {
953 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
954 W_ERROR_NOT_OK_GOTO_DONE(werr);
956 werr = regdb_store_keys_internal2(db, store_ctx->key, subkeys);
957 if (!W_ERROR_IS_OK(werr)) {
958 DEBUG(0,("regdb_store_keys: Failed to store "
959 "new record for key [%s]: %s\n",
960 store_ctx->key, win_errstr(werr)));
961 goto done;
963 TALLOC_FREE(subkeys);
966 for (i=0; i<num_subkeys; i++) {
967 path = talloc_asprintf(mem_ctx, "%s\\%s", store_ctx->key,
968 regsubkey_ctr_specific_key(store_ctx->ctr, i));
969 if (!path) {
970 werr = WERR_NOMEM;
971 goto done;
973 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
974 W_ERROR_NOT_OK_GOTO_DONE(werr);
976 werr = regdb_fetch_keys_internal(db, path, subkeys);
977 if (!W_ERROR_IS_OK(werr)) {
978 /* create a record with 0 subkeys */
979 werr = regdb_store_keys_internal2(db, path, subkeys);
980 if (!W_ERROR_IS_OK(werr)) {
981 DEBUG(0,("regdb_store_keys: Failed to store "
982 "new record for key [%s]: %s\n", path,
983 win_errstr(werr)));
984 goto done;
988 TALLOC_FREE(subkeys);
989 TALLOC_FREE(path);
992 werr = WERR_OK;
994 done:
995 talloc_free(mem_ctx);
996 return werror_to_ntstatus(werr);
999 static bool regdb_store_keys_internal(struct db_context *db, const char *key,
1000 struct regsubkey_ctr *ctr)
1002 int num_subkeys, old_num_subkeys, i;
1003 struct regsubkey_ctr *old_subkeys = NULL;
1004 TALLOC_CTX *ctx = talloc_stackframe();
1005 WERROR werr;
1006 bool ret = false;
1007 struct regdb_store_keys_context store_ctx;
1009 if (!regdb_key_is_base_key(key) && !regdb_key_exists(db, key)) {
1010 goto done;
1014 * fetch a list of the old subkeys so we can determine if anything has
1015 * changed
1018 werr = regsubkey_ctr_init(ctx, &old_subkeys);
1019 if (!W_ERROR_IS_OK(werr)) {
1020 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
1021 goto done;
1024 werr = regdb_fetch_keys_internal(db, key, old_subkeys);
1025 if (!W_ERROR_IS_OK(werr) &&
1026 !W_ERROR_EQUAL(werr, WERR_NOT_FOUND))
1028 goto done;
1031 num_subkeys = regsubkey_ctr_numkeys(ctr);
1032 old_num_subkeys = regsubkey_ctr_numkeys(old_subkeys);
1033 if ((num_subkeys && old_num_subkeys) &&
1034 (num_subkeys == old_num_subkeys)) {
1036 for (i = 0; i < num_subkeys; i++) {
1037 if (strcmp(regsubkey_ctr_specific_key(ctr, i),
1038 regsubkey_ctr_specific_key(old_subkeys, i))
1039 != 0)
1041 break;
1044 if (i == num_subkeys) {
1046 * Nothing changed, no point to even start a tdb
1047 * transaction
1050 ret = true;
1051 goto done;
1055 TALLOC_FREE(old_subkeys);
1057 store_ctx.key = key;
1058 store_ctx.ctr = ctr;
1060 werr = ntstatus_to_werror(dbwrap_trans_do(db,
1061 regdb_store_keys_action,
1062 &store_ctx));
1064 ret = W_ERROR_IS_OK(werr);
1066 done:
1067 TALLOC_FREE(ctx);
1069 return ret;
1072 bool regdb_store_keys(const char *key, struct regsubkey_ctr *ctr)
1074 return regdb_store_keys_internal(regdb, key, ctr);
1078 * create a subkey of a given key
1081 struct regdb_create_subkey_context {
1082 const char *key;
1083 const char *subkey;
1086 static NTSTATUS regdb_create_subkey_action(struct db_context *db,
1087 void *private_data)
1089 WERROR werr;
1090 struct regdb_create_subkey_context *create_ctx;
1091 struct regsubkey_ctr *subkeys;
1092 TALLOC_CTX *mem_ctx = talloc_stackframe();
1094 create_ctx = (struct regdb_create_subkey_context *)private_data;
1096 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
1097 W_ERROR_NOT_OK_GOTO_DONE(werr);
1099 werr = regdb_fetch_keys_internal(db, create_ctx->key, subkeys);
1100 W_ERROR_NOT_OK_GOTO_DONE(werr);
1102 werr = regsubkey_ctr_addkey(subkeys, create_ctx->subkey);
1103 W_ERROR_NOT_OK_GOTO_DONE(werr);
1105 werr = regdb_store_keys_internal2(db, create_ctx->key, subkeys);
1106 if (!W_ERROR_IS_OK(werr)) {
1107 DEBUG(0, (__location__ " failed to store new subkey list for "
1108 "parent key %s: %s\n", create_ctx->key,
1109 win_errstr(werr)));
1112 done:
1113 talloc_free(mem_ctx);
1114 return werror_to_ntstatus(werr);
1117 static WERROR regdb_create_subkey(const char *key, const char *subkey)
1119 WERROR werr;
1120 struct regsubkey_ctr *subkeys;
1121 TALLOC_CTX *mem_ctx = talloc_stackframe();
1122 struct regdb_create_subkey_context create_ctx;
1124 if (!regdb_key_is_base_key(key) && !regdb_key_exists(regdb, key)) {
1125 werr = WERR_NOT_FOUND;
1126 goto done;
1129 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
1130 W_ERROR_NOT_OK_GOTO_DONE(werr);
1132 werr = regdb_fetch_keys_internal(regdb, key, subkeys);
1133 W_ERROR_NOT_OK_GOTO_DONE(werr);
1135 if (regsubkey_ctr_key_exists(subkeys, subkey)) {
1136 werr = WERR_OK;
1137 goto done;
1140 talloc_free(subkeys);
1142 create_ctx.key = key;
1143 create_ctx.subkey = subkey;
1145 werr = ntstatus_to_werror(dbwrap_trans_do(regdb,
1146 regdb_create_subkey_action,
1147 &create_ctx));
1149 done:
1150 talloc_free(mem_ctx);
1151 return werr;
1155 * create a subkey of a given key
1158 struct regdb_delete_subkey_context {
1159 const char *key;
1160 const char *subkey;
1161 const char *path;
1164 static NTSTATUS regdb_delete_subkey_action(struct db_context *db,
1165 void *private_data)
1167 WERROR werr;
1168 struct regdb_delete_subkey_context *delete_ctx;
1169 struct regsubkey_ctr *subkeys;
1170 TALLOC_CTX *mem_ctx = talloc_stackframe();
1172 delete_ctx = (struct regdb_delete_subkey_context *)private_data;
1174 werr = regdb_delete_key_lists(db, delete_ctx->path);
1175 W_ERROR_NOT_OK_GOTO_DONE(werr);
1177 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
1178 W_ERROR_NOT_OK_GOTO_DONE(werr);
1180 werr = regdb_fetch_keys_internal(db, delete_ctx->key, subkeys);
1181 W_ERROR_NOT_OK_GOTO_DONE(werr);
1183 werr = regsubkey_ctr_delkey(subkeys, delete_ctx->subkey);
1184 W_ERROR_NOT_OK_GOTO_DONE(werr);
1186 werr = regdb_store_keys_internal2(db, delete_ctx->key, subkeys);
1187 if (!W_ERROR_IS_OK(werr)) {
1188 DEBUG(0, (__location__ " failed to store new subkey_list for "
1189 "parent key %s: %s\n", delete_ctx->key,
1190 win_errstr(werr)));
1193 done:
1194 talloc_free(mem_ctx);
1195 return werror_to_ntstatus(werr);
1198 static WERROR regdb_delete_subkey(const char *key, const char *subkey)
1200 WERROR werr;
1201 char *path;
1202 struct regdb_delete_subkey_context delete_ctx;
1203 TALLOC_CTX *mem_ctx = talloc_stackframe();
1205 if (!regdb_key_is_base_key(key) && !regdb_key_exists(regdb, key)) {
1206 werr = WERR_NOT_FOUND;
1207 goto done;
1210 path = talloc_asprintf(mem_ctx, "%s\\%s", key, subkey);
1211 if (path == NULL) {
1212 werr = WERR_NOMEM;
1213 goto done;
1216 if (!regdb_key_exists(regdb, path)) {
1217 werr = WERR_OK;
1218 goto done;
1221 delete_ctx.key = key;
1222 delete_ctx.subkey = subkey;
1223 delete_ctx.path = path;
1225 werr = ntstatus_to_werror(dbwrap_trans_do(regdb,
1226 regdb_delete_subkey_action,
1227 &delete_ctx));
1229 done:
1230 talloc_free(mem_ctx);
1231 return werr;
1234 static TDB_DATA regdb_fetch_key_internal(struct db_context *db,
1235 TALLOC_CTX *mem_ctx, const char *key)
1237 char *path = NULL;
1238 TDB_DATA data;
1240 path = normalize_reg_path(mem_ctx, key);
1241 if (!path) {
1242 return make_tdb_data(NULL, 0);
1245 data = dbwrap_fetch_bystring(db, mem_ctx, path);
1247 TALLOC_FREE(path);
1248 return data;
1253 * check whether a given key name represents a base key,
1254 * i.e one without a subkey separator ('\').
1256 static bool regdb_key_is_base_key(const char *key)
1258 TALLOC_CTX *mem_ctx = talloc_stackframe();
1259 bool ret = false;
1260 char *path;
1262 if (key == NULL) {
1263 goto done;
1266 path = normalize_reg_path(mem_ctx, key);
1267 if (path == NULL) {
1268 DEBUG(0, ("out of memory! (talloc failed)\n"));
1269 goto done;
1272 if (*path == '\0') {
1273 goto done;
1276 ret = (strrchr(path, '\\') == NULL);
1278 done:
1279 TALLOC_FREE(mem_ctx);
1280 return ret;
1284 * regdb_key_exists() is a very frequent operation. It can be quite
1285 * time-consuming to fully fetch the parent's subkey list, talloc_strdup all
1286 * subkeys and then compare the keyname linearly to all the parent's subkeys.
1288 * The following code tries to make this operation as efficient as possible:
1289 * Per registry key we create a list of subkeys that is very efficient to
1290 * search for existence of a subkey. Its format is:
1292 * 4 bytes num_subkeys
1293 * 4*num_subkey bytes offset into the string array
1294 * then follows a sorted list of subkeys in uppercase
1296 * This record is created by create_sorted_subkeys() on demand if it does not
1297 * exist. scan_parent_subkeys() uses regdb->parse_record to search the sorted
1298 * list, the parsing code and the binary search can be found in
1299 * parent_subkey_scanner. The code uses parse_record() to avoid a memcpy of
1300 * the potentially large subkey record.
1302 * The sorted subkey record is deleted in regdb_store_keys_internal2 and
1303 * recreated on demand.
1306 static int cmp_keynames(char **p1, char **p2)
1308 return StrCaseCmp(*p1, *p2);
1311 struct create_sorted_subkeys_context {
1312 const char *key;
1313 const char *sorted_keyname;
1316 static NTSTATUS create_sorted_subkeys_action(struct db_context *db,
1317 void *private_data)
1319 char **sorted_subkeys;
1320 struct regsubkey_ctr *ctr;
1321 NTSTATUS status;
1322 char *buf;
1323 char *p;
1324 int i;
1325 size_t len;
1326 int num_subkeys;
1327 struct create_sorted_subkeys_context *sorted_ctx;
1329 sorted_ctx = (struct create_sorted_subkeys_context *)private_data;
1332 * In this function, we only treat failing of the actual write to
1333 * the db as a real error. All preliminary errors, at a stage when
1334 * nothing has been written to the DB yet are treated as success
1335 * to be committed (as an empty transaction).
1337 * The reason is that this (disposable) call might be nested in other
1338 * transactions. Doing a cancel here would destroy the possibility of
1339 * a transaction_commit for transactions that we might be wrapped in.
1342 status = werror_to_ntstatus(regsubkey_ctr_init(talloc_tos(), &ctr));
1343 if (!NT_STATUS_IS_OK(status)) {
1344 /* don't treat this as an error */
1345 status = NT_STATUS_OK;
1346 goto done;
1349 status = werror_to_ntstatus(regdb_fetch_keys_internal(db,
1350 sorted_ctx->key,
1351 ctr));
1352 if (!NT_STATUS_IS_OK(status)) {
1353 /* don't treat this as an error */
1354 status = NT_STATUS_OK;
1355 goto done;
1358 num_subkeys = regsubkey_ctr_numkeys(ctr);
1359 sorted_subkeys = talloc_array(ctr, char *, num_subkeys);
1360 if (sorted_subkeys == NULL) {
1361 /* don't treat this as an error */
1362 goto done;
1365 len = 4 + 4*num_subkeys;
1367 for (i = 0; i < num_subkeys; i++) {
1368 sorted_subkeys[i] = talloc_strdup_upper(sorted_subkeys,
1369 regsubkey_ctr_specific_key(ctr, i));
1370 if (sorted_subkeys[i] == NULL) {
1371 /* don't treat this as an error */
1372 goto done;
1374 len += strlen(sorted_subkeys[i])+1;
1377 TYPESAFE_QSORT(sorted_subkeys, num_subkeys, cmp_keynames);
1379 buf = talloc_array(ctr, char, len);
1380 if (buf == NULL) {
1381 /* don't treat this as an error */
1382 goto done;
1384 p = buf + 4 + 4*num_subkeys;
1386 SIVAL(buf, 0, num_subkeys);
1388 for (i=0; i < num_subkeys; i++) {
1389 ptrdiff_t offset = p - buf;
1390 SIVAL(buf, 4 + 4*i, offset);
1391 strlcpy(p, sorted_subkeys[i], len-offset);
1392 p += strlen(sorted_subkeys[i]) + 1;
1395 status = dbwrap_store_bystring(
1396 db, sorted_ctx->sorted_keyname, make_tdb_data((uint8_t *)buf,
1397 len),
1398 TDB_REPLACE);
1400 done:
1401 talloc_free(ctr);
1402 return status;
1405 static bool create_sorted_subkeys(const char *key, const char *sorted_keyname)
1407 NTSTATUS status;
1408 struct create_sorted_subkeys_context sorted_ctx;
1410 sorted_ctx.key = key;
1411 sorted_ctx.sorted_keyname = sorted_keyname;
1413 status = dbwrap_trans_do(regdb,
1414 create_sorted_subkeys_action,
1415 &sorted_ctx);
1417 return NT_STATUS_IS_OK(status);
1420 struct scan_subkey_state {
1421 char *name;
1422 bool scanned;
1423 bool found;
1426 static int parent_subkey_scanner(TDB_DATA key, TDB_DATA data,
1427 void *private_data)
1429 struct scan_subkey_state *state =
1430 (struct scan_subkey_state *)private_data;
1431 uint32_t num_subkeys;
1432 uint32_t l, u;
1434 if (data.dsize < sizeof(uint32_t)) {
1435 return -1;
1438 state->scanned = true;
1439 state->found = false;
1441 tdb_unpack(data.dptr, data.dsize, "d", &num_subkeys);
1443 l = 0;
1444 u = num_subkeys;
1446 while (l < u) {
1447 uint32_t idx = (l+u)/2;
1448 char *s = (char *)data.dptr + IVAL(data.dptr, 4 + 4*idx);
1449 int comparison = strcmp(state->name, s);
1451 if (comparison < 0) {
1452 u = idx;
1453 } else if (comparison > 0) {
1454 l = idx + 1;
1455 } else {
1456 state->found = true;
1457 return 0;
1460 return 0;
1463 static bool scan_parent_subkeys(struct db_context *db, const char *parent,
1464 const char *name)
1466 char *path = NULL;
1467 char *key = NULL;
1468 struct scan_subkey_state state = { 0, };
1469 bool result = false;
1470 int res;
1472 state.name = NULL;
1474 path = normalize_reg_path(talloc_tos(), parent);
1475 if (path == NULL) {
1476 goto fail;
1479 key = talloc_asprintf(talloc_tos(), "%s\\%s",
1480 REG_SORTED_SUBKEYS_PREFIX, path);
1481 if (key == NULL) {
1482 goto fail;
1485 state.name = talloc_strdup_upper(talloc_tos(), name);
1486 if (state.name == NULL) {
1487 goto fail;
1489 state.scanned = false;
1491 res = db->parse_record(db, string_term_tdb_data(key),
1492 parent_subkey_scanner, &state);
1494 if (state.scanned) {
1495 result = state.found;
1496 } else {
1497 res = db->transaction_start(db);
1498 if (res != 0) {
1499 DEBUG(0, ("error starting transacion\n"));
1500 goto fail;
1503 if (!create_sorted_subkeys(path, key)) {
1504 res = db->transaction_cancel(db);
1505 if (res != 0) {
1506 smb_panic("Failed to cancel transaction.");
1508 goto fail;
1511 res = db->parse_record(db, string_term_tdb_data(key),
1512 parent_subkey_scanner, &state);
1513 if ((res == 0) && (state.scanned)) {
1514 result = state.found;
1517 res = db->transaction_commit(db);
1518 if (res != 0) {
1519 DEBUG(0, ("error committing transaction\n"));
1520 result = false;
1524 fail:
1525 TALLOC_FREE(path);
1526 TALLOC_FREE(state.name);
1527 return result;
1531 * Check for the existence of a key.
1533 * Existence of a key is authoritatively defined by its
1534 * existence in the list of subkeys of its parent key.
1535 * The exeption of this are keys without a parent key,
1536 * i.e. the "base" keys (HKLM, HKCU, ...).
1538 static bool regdb_key_exists(struct db_context *db, const char *key)
1540 TALLOC_CTX *mem_ctx = talloc_stackframe();
1541 TDB_DATA value;
1542 bool ret = false;
1543 char *path, *p;
1545 if (key == NULL) {
1546 goto done;
1549 path = normalize_reg_path(mem_ctx, key);
1550 if (path == NULL) {
1551 DEBUG(0, ("out of memory! (talloc failed)\n"));
1552 goto done;
1555 if (*path == '\0') {
1556 goto done;
1559 p = strrchr(path, '\\');
1560 if (p == NULL) {
1561 /* this is a base key */
1562 value = regdb_fetch_key_internal(db, mem_ctx, path);
1563 ret = (value.dptr != NULL);
1564 } else {
1565 *p = '\0';
1566 ret = scan_parent_subkeys(db, path, p+1);
1569 done:
1570 TALLOC_FREE(mem_ctx);
1571 return ret;
1575 /***********************************************************************
1576 Retrieve an array of strings containing subkeys. Memory should be
1577 released by the caller.
1578 ***********************************************************************/
1580 static WERROR regdb_fetch_keys_internal(struct db_context *db, const char *key,
1581 struct regsubkey_ctr *ctr)
1583 WERROR werr;
1584 uint32_t num_items;
1585 uint8 *buf;
1586 uint32 buflen, len;
1587 int i;
1588 fstring subkeyname;
1589 TALLOC_CTX *frame = talloc_stackframe();
1590 TDB_DATA value;
1592 DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key ? key : "NULL"));
1594 frame = talloc_stackframe();
1596 if (!regdb_key_exists(db, key)) {
1597 DEBUG(10, ("key [%s] not found\n", key));
1598 werr = WERR_NOT_FOUND;
1599 goto done;
1602 werr = regsubkey_ctr_set_seqnum(ctr, db->get_seqnum(db));
1603 W_ERROR_NOT_OK_GOTO_DONE(werr);
1605 value = regdb_fetch_key_internal(db, frame, key);
1607 if (value.dsize == 0 || value.dptr == NULL) {
1608 DEBUG(10, ("regdb_fetch_keys: no subkeys found for key [%s]\n",
1609 key));
1610 goto done;
1613 buf = value.dptr;
1614 buflen = value.dsize;
1615 len = tdb_unpack( buf, buflen, "d", &num_items);
1616 if (len == (uint32_t)-1) {
1617 werr = WERR_NOT_FOUND;
1618 goto done;
1621 werr = regsubkey_ctr_reinit(ctr);
1622 W_ERROR_NOT_OK_GOTO_DONE(werr);
1624 for (i=0; i<num_items; i++) {
1625 len += tdb_unpack(buf+len, buflen-len, "f", subkeyname);
1626 werr = regsubkey_ctr_addkey(ctr, subkeyname);
1627 if (!W_ERROR_IS_OK(werr)) {
1628 DEBUG(5, ("regdb_fetch_keys: regsubkey_ctr_addkey "
1629 "failed: %s\n", win_errstr(werr)));
1630 num_items = 0;
1631 goto done;
1635 DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items));
1637 done:
1638 TALLOC_FREE(frame);
1639 return werr;
1642 int regdb_fetch_keys(const char *key, struct regsubkey_ctr *ctr)
1644 WERROR werr;
1646 werr = regdb_fetch_keys_internal(regdb, key, ctr);
1647 if (!W_ERROR_IS_OK(werr)) {
1648 return -1;
1651 return regsubkey_ctr_numkeys(ctr);
1654 /****************************************************************************
1655 Unpack a list of registry values frem the TDB
1656 ***************************************************************************/
1658 static int regdb_unpack_values(struct regval_ctr *values, uint8 *buf, int buflen)
1660 int len = 0;
1661 uint32 type;
1662 fstring valuename;
1663 uint32 size;
1664 uint8 *data_p;
1665 uint32 num_values = 0;
1666 int i;
1668 /* loop and unpack the rest of the registry values */
1670 len += tdb_unpack(buf+len, buflen-len, "d", &num_values);
1672 for ( i=0; i<num_values; i++ ) {
1673 /* unpack the next regval */
1675 type = REG_NONE;
1676 size = 0;
1677 data_p = NULL;
1678 valuename[0] = '\0';
1679 len += tdb_unpack(buf+len, buflen-len, "fdB",
1680 valuename,
1681 &type,
1682 &size,
1683 &data_p);
1685 regval_ctr_addvalue(values, valuename, type,
1686 (uint8_t *)data_p, size);
1687 SAFE_FREE(data_p); /* 'B' option to tdb_unpack does a malloc() */
1689 DEBUG(8,("specific: [%s], len: %d\n", valuename, size));
1692 return len;
1695 /****************************************************************************
1696 Pack all values in all printer keys
1697 ***************************************************************************/
1699 static int regdb_pack_values(struct regval_ctr *values, uint8 *buf, int buflen)
1701 int len = 0;
1702 int i;
1703 struct regval_blob *val;
1704 int num_values;
1706 if ( !values )
1707 return 0;
1709 num_values = regval_ctr_numvals( values );
1711 /* pack the number of values first */
1713 len += tdb_pack( buf+len, buflen-len, "d", num_values );
1715 /* loop over all values */
1717 for ( i=0; i<num_values; i++ ) {
1718 val = regval_ctr_specific_value( values, i );
1719 len += tdb_pack(buf+len, buflen-len, "fdB",
1720 regval_name(val),
1721 regval_type(val),
1722 regval_size(val),
1723 regval_data_p(val) );
1726 return len;
1729 /***********************************************************************
1730 Retrieve an array of strings containing subkeys. Memory should be
1731 released by the caller.
1732 ***********************************************************************/
1734 static int regdb_fetch_values_internal(struct db_context *db, const char* key,
1735 struct regval_ctr *values)
1737 char *keystr = NULL;
1738 TALLOC_CTX *ctx = talloc_stackframe();
1739 int ret = 0;
1740 TDB_DATA value;
1741 WERROR werr;
1743 DEBUG(10,("regdb_fetch_values: Looking for value of key [%s] \n", key));
1745 if (!regdb_key_exists(db, key)) {
1746 goto done;
1749 keystr = talloc_asprintf(ctx, "%s\\%s", REG_VALUE_PREFIX, key);
1750 if (!keystr) {
1751 goto done;
1754 werr = regval_ctr_set_seqnum(values, db->get_seqnum(db));
1755 W_ERROR_NOT_OK_GOTO_DONE(werr);
1757 value = regdb_fetch_key_internal(db, ctx, keystr);
1759 if (!value.dptr) {
1760 /* all keys have zero values by default */
1761 goto done;
1764 regdb_unpack_values(values, value.dptr, value.dsize);
1765 ret = regval_ctr_numvals(values);
1767 done:
1768 TALLOC_FREE(ctx);
1769 return ret;
1772 int regdb_fetch_values(const char* key, struct regval_ctr *values)
1774 return regdb_fetch_values_internal(regdb, key, values);
1777 static bool regdb_store_values_internal(struct db_context *db, const char *key,
1778 struct regval_ctr *values)
1780 TDB_DATA old_data, data;
1781 char *keystr = NULL;
1782 TALLOC_CTX *ctx = talloc_stackframe();
1783 int len;
1784 NTSTATUS status;
1785 bool result = false;
1787 DEBUG(10,("regdb_store_values: Looking for value of key [%s] \n", key));
1789 if (!regdb_key_exists(db, key)) {
1790 goto done;
1793 ZERO_STRUCT(data);
1795 len = regdb_pack_values(values, data.dptr, data.dsize);
1796 if (len <= 0) {
1797 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
1798 goto done;
1801 data.dptr = TALLOC_ARRAY(ctx, uint8, len);
1802 data.dsize = len;
1804 len = regdb_pack_values(values, data.dptr, data.dsize);
1806 SMB_ASSERT( len == data.dsize );
1808 keystr = talloc_asprintf(ctx, "%s\\%s", REG_VALUE_PREFIX, key );
1809 if (!keystr) {
1810 goto done;
1812 keystr = normalize_reg_path(ctx, keystr);
1813 if (!keystr) {
1814 goto done;
1817 old_data = dbwrap_fetch_bystring(db, ctx, keystr);
1819 if ((old_data.dptr != NULL)
1820 && (old_data.dsize == data.dsize)
1821 && (memcmp(old_data.dptr, data.dptr, data.dsize) == 0))
1823 result = true;
1824 goto done;
1827 status = dbwrap_trans_store_bystring(db, keystr, data, TDB_REPLACE);
1829 result = NT_STATUS_IS_OK(status);
1831 done:
1832 TALLOC_FREE(ctx);
1833 return result;
1836 bool regdb_store_values(const char *key, struct regval_ctr *values)
1838 return regdb_store_values_internal(regdb, key, values);
1841 static WERROR regdb_get_secdesc(TALLOC_CTX *mem_ctx, const char *key,
1842 struct security_descriptor **psecdesc)
1844 char *tdbkey;
1845 TDB_DATA data;
1846 NTSTATUS status;
1847 TALLOC_CTX *tmp_ctx = talloc_stackframe();
1848 WERROR err = WERR_OK;
1850 DEBUG(10, ("regdb_get_secdesc: Getting secdesc of key [%s]\n", key));
1852 if (!regdb_key_exists(regdb, key)) {
1853 err = WERR_BADFILE;
1854 goto done;
1857 tdbkey = talloc_asprintf(tmp_ctx, "%s\\%s", REG_SECDESC_PREFIX, key);
1858 if (tdbkey == NULL) {
1859 err = WERR_NOMEM;
1860 goto done;
1863 tdbkey = normalize_reg_path(tmp_ctx, tdbkey);
1864 if (tdbkey == NULL) {
1865 err = WERR_NOMEM;
1866 goto done;
1869 data = dbwrap_fetch_bystring(regdb, tmp_ctx, tdbkey);
1870 if (data.dptr == NULL) {
1871 err = WERR_BADFILE;
1872 goto done;
1875 status = unmarshall_sec_desc(mem_ctx, (uint8 *)data.dptr, data.dsize,
1876 psecdesc);
1878 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MEMORY)) {
1879 err = WERR_NOMEM;
1880 } else if (!NT_STATUS_IS_OK(status)) {
1881 err = WERR_REG_CORRUPT;
1884 done:
1885 TALLOC_FREE(tmp_ctx);
1886 return err;
1889 static WERROR regdb_set_secdesc(const char *key,
1890 struct security_descriptor *secdesc)
1892 TALLOC_CTX *mem_ctx = talloc_stackframe();
1893 char *tdbkey;
1894 WERROR err = WERR_NOMEM;
1895 TDB_DATA tdbdata;
1897 if (!regdb_key_exists(regdb, key)) {
1898 err = WERR_BADFILE;
1899 goto done;
1902 tdbkey = talloc_asprintf(mem_ctx, "%s\\%s", REG_SECDESC_PREFIX, key);
1903 if (tdbkey == NULL) {
1904 goto done;
1907 tdbkey = normalize_reg_path(mem_ctx, tdbkey);
1908 if (tdbkey == NULL) {
1909 err = WERR_NOMEM;
1910 goto done;
1913 if (secdesc == NULL) {
1914 /* assuming a delete */
1915 err = ntstatus_to_werror(dbwrap_trans_delete_bystring(regdb,
1916 tdbkey));
1917 goto done;
1920 err = ntstatus_to_werror(marshall_sec_desc(mem_ctx, secdesc,
1921 &tdbdata.dptr,
1922 &tdbdata.dsize));
1923 W_ERROR_NOT_OK_GOTO_DONE(err);
1925 err = ntstatus_to_werror(dbwrap_trans_store_bystring(regdb, tdbkey,
1926 tdbdata, 0));
1928 done:
1929 TALLOC_FREE(mem_ctx);
1930 return err;
1933 bool regdb_subkeys_need_update(struct regsubkey_ctr *subkeys)
1935 return (regdb_get_seqnum() != regsubkey_ctr_get_seqnum(subkeys));
1938 bool regdb_values_need_update(struct regval_ctr *values)
1940 return (regdb_get_seqnum() != regval_ctr_get_seqnum(values));
1944 * Table of function pointers for default access
1947 struct registry_ops regdb_ops = {
1948 .fetch_subkeys = regdb_fetch_keys,
1949 .fetch_values = regdb_fetch_values,
1950 .store_subkeys = regdb_store_keys,
1951 .store_values = regdb_store_values,
1952 .create_subkey = regdb_create_subkey,
1953 .delete_subkey = regdb_delete_subkey,
1954 .get_secdesc = regdb_get_secdesc,
1955 .set_secdesc = regdb_set_secdesc,
1956 .subkeys_need_update = regdb_subkeys_need_update,
1957 .values_need_update = regdb_values_need_update