s4:torture/rpc/samr_accessmask.c: make use of dcerpc_binding_handle stubs
[Samba/nascimento.git] / source3 / registry / reg_backend_db.c
blobc8241755275c494d7c228a464db001d5891a5ae3
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 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_REGISTRY
28 static struct db_context *regdb = NULL;
29 static int regdb_refcount;
31 static bool regdb_key_exists(struct db_context *db, const char *key);
32 static bool regdb_key_is_base_key(const char *key);
33 static WERROR regdb_fetch_keys_internal(struct db_context *db, const char *key,
34 struct regsubkey_ctr *ctr);
35 static bool regdb_store_keys_internal(struct db_context *db, const char *key,
36 struct regsubkey_ctr *ctr);
37 static int regdb_fetch_values_internal(struct db_context *db, const char* key,
38 struct regval_ctr *values);
39 static bool regdb_store_values_internal(struct db_context *db, const char *key,
40 struct regval_ctr *values);
42 /* List the deepest path into the registry. All part components will be created.*/
44 /* If you want to have a part of the path controlled by the tdb and part by
45 a virtual registry db (e.g. printing), then you have to list the deepest path.
46 For example,"HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion/Print"
47 allows the reg_db backend to handle everything up to
48 "HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion" and then we'll hook
49 the reg_printing backend onto the last component of the path (see
50 KEY_PRINTING_2K in include/rpc_reg.h) --jerry */
52 static const char *builtin_registry_paths[] = {
53 KEY_PRINTING_2K,
54 KEY_PRINTING_PORTS,
55 KEY_PRINTING,
56 KEY_SHARES,
57 KEY_EVENTLOG,
58 KEY_SMBCONF,
59 KEY_PERFLIB,
60 KEY_PERFLIB_009,
61 KEY_GROUP_POLICY,
62 KEY_SAMBA_GROUP_POLICY,
63 KEY_GP_MACHINE_POLICY,
64 KEY_GP_MACHINE_WIN_POLICY,
65 KEY_HKCU,
66 KEY_GP_USER_POLICY,
67 KEY_GP_USER_WIN_POLICY,
68 KEY_WINLOGON_GPEXT_PATH,
69 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Print\\Monitors",
70 KEY_PROD_OPTIONS,
71 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\DefaultUserConfiguration",
72 KEY_TCPIP_PARAMS,
73 KEY_NETLOGON_PARAMS,
74 KEY_HKU,
75 KEY_HKCR,
76 KEY_HKPD,
77 KEY_HKPT,
78 NULL };
80 struct builtin_regkey_value {
81 const char *path;
82 const char *valuename;
83 uint32 type;
84 union {
85 const char *string;
86 uint32 dw_value;
87 } data;
90 static struct builtin_regkey_value builtin_registry_values[] = {
91 { KEY_PRINTING_PORTS,
92 SAMBA_PRINTER_PORT_NAME, REG_SZ, { "" } },
93 { KEY_PRINTING_2K,
94 "DefaultSpoolDirectory", REG_SZ, { "C:\\Windows\\System32\\Spool\\Printers" } },
95 { KEY_EVENTLOG,
96 "DisplayName", REG_SZ, { "Event Log" } },
97 { KEY_EVENTLOG,
98 "ErrorControl", REG_DWORD, { (char*)0x00000001 } },
99 { NULL, NULL, 0, { NULL } }
103 * Initialize a key in the registry:
104 * create each component key of the specified path.
106 static WERROR init_registry_key_internal(struct db_context *db,
107 const char *add_path)
109 WERROR werr;
110 TALLOC_CTX *frame = talloc_stackframe();
111 char *path = NULL;
112 char *base = NULL;
113 char *remaining = NULL;
114 char *keyname;
115 char *subkeyname;
116 struct regsubkey_ctr *subkeys;
117 const char *p, *p2;
119 DEBUG(6, ("init_registry_key: Adding [%s]\n", add_path));
121 path = talloc_strdup(frame, add_path);
122 base = talloc_strdup(frame, "");
123 if (!path || !base) {
124 werr = WERR_NOMEM;
125 goto fail;
127 p = path;
129 while (next_token_talloc(frame, &p, &keyname, "\\")) {
131 /* build up the registry path from the components */
133 if (*base) {
134 base = talloc_asprintf(frame, "%s\\", base);
135 if (!base) {
136 werr = WERR_NOMEM;
137 goto fail;
140 base = talloc_asprintf_append(base, "%s", keyname);
141 if (!base) {
142 werr = WERR_NOMEM;
143 goto fail;
146 /* get the immediate subkeyname (if we have one ) */
148 subkeyname = talloc_strdup(frame, "");
149 if (!subkeyname) {
150 werr = WERR_NOMEM;
151 goto fail;
153 if (*p) {
154 remaining = talloc_strdup(frame, p);
155 if (!remaining) {
156 werr = WERR_NOMEM;
157 goto fail;
159 p2 = remaining;
161 if (!next_token_talloc(frame, &p2,
162 &subkeyname, "\\"))
164 subkeyname = talloc_strdup(frame,p2);
165 if (!subkeyname) {
166 werr = WERR_NOMEM;
167 goto fail;
172 DEBUG(10,("init_registry_key: Storing key [%s] with "
173 "subkey [%s]\n", base,
174 *subkeyname ? subkeyname : "NULL"));
176 /* we don't really care if the lookup succeeds or not
177 * since we are about to update the record.
178 * We just want any subkeys already present */
180 werr = regsubkey_ctr_init(frame, &subkeys);
181 if (!W_ERROR_IS_OK(werr)) {
182 DEBUG(0,("talloc() failure!\n"));
183 goto fail;
186 werr = regdb_fetch_keys_internal(db, base, subkeys);
187 if (!W_ERROR_IS_OK(werr) &&
188 !W_ERROR_EQUAL(werr, WERR_NOT_FOUND))
190 goto fail;
193 if (*subkeyname) {
194 werr = regsubkey_ctr_addkey(subkeys, subkeyname);
195 if (!W_ERROR_IS_OK(werr)) {
196 goto fail;
199 if (!regdb_store_keys_internal(db, base, subkeys)) {
200 werr = WERR_CAN_NOT_COMPLETE;
201 goto fail;
205 werr = WERR_OK;
207 fail:
208 TALLOC_FREE(frame);
209 return werr;
212 struct init_registry_key_context {
213 const char *add_path;
216 static NTSTATUS init_registry_key_action(struct db_context *db,
217 void *private_data)
219 struct init_registry_key_context *init_ctx =
220 (struct init_registry_key_context *)private_data;
222 return werror_to_ntstatus(init_registry_key_internal(
223 db, init_ctx->add_path));
227 * Initialize a key in the registry:
228 * create each component key of the specified path,
229 * wrapped in one db transaction.
231 WERROR init_registry_key(const char *add_path)
233 struct init_registry_key_context init_ctx;
235 if (regdb_key_exists(regdb, add_path)) {
236 return WERR_OK;
239 init_ctx.add_path = add_path;
241 return ntstatus_to_werror(dbwrap_trans_do(regdb,
242 init_registry_key_action,
243 &init_ctx));
246 /***********************************************************************
247 Open the registry data in the tdb
248 ***********************************************************************/
250 static void regdb_ctr_add_value(struct regval_ctr *ctr,
251 struct builtin_regkey_value *value)
253 switch(value->type) {
254 case REG_DWORD:
255 regval_ctr_addvalue(ctr, value->valuename, REG_DWORD,
256 (char*)&value->data.dw_value,
257 sizeof(uint32));
258 break;
260 case REG_SZ:
261 regval_ctr_addvalue_sz(ctr, value->valuename,
262 value->data.string);
263 break;
265 default:
266 DEBUG(0, ("regdb_ctr_add_value: invalid value type in "
267 "registry values [%d]\n", value->type));
271 static NTSTATUS init_registry_data_action(struct db_context *db,
272 void *private_data)
274 NTSTATUS status;
275 TALLOC_CTX *frame = talloc_stackframe();
276 struct regval_ctr *values;
277 int i;
279 /* loop over all of the predefined paths and add each component */
281 for (i=0; builtin_registry_paths[i] != NULL; i++) {
282 if (regdb_key_exists(db, builtin_registry_paths[i])) {
283 continue;
285 status = werror_to_ntstatus(init_registry_key_internal(db,
286 builtin_registry_paths[i]));
287 if (!NT_STATUS_IS_OK(status)) {
288 goto done;
292 /* loop over all of the predefined values and add each component */
294 for (i=0; builtin_registry_values[i].path != NULL; i++) {
296 values = TALLOC_ZERO_P(frame, struct regval_ctr);
297 if (values == NULL) {
298 status = NT_STATUS_NO_MEMORY;
299 goto done;
302 regdb_fetch_values_internal(db,
303 builtin_registry_values[i].path,
304 values);
306 /* preserve existing values across restarts. Only add new ones */
308 if (!regval_ctr_key_exists(values,
309 builtin_registry_values[i].valuename))
311 regdb_ctr_add_value(values,
312 &builtin_registry_values[i]);
313 regdb_store_values_internal(db,
314 builtin_registry_values[i].path,
315 values);
317 TALLOC_FREE(values);
320 status = NT_STATUS_OK;
322 done:
324 TALLOC_FREE(frame);
325 return status;
328 WERROR init_registry_data(void)
330 WERROR werr;
331 TALLOC_CTX *frame = talloc_stackframe();
332 struct regval_ctr *values;
333 int i;
336 * First, check for the existence of the needed keys and values.
337 * If all do already exist, we can save the writes.
339 for (i=0; builtin_registry_paths[i] != NULL; i++) {
340 if (!regdb_key_exists(regdb, builtin_registry_paths[i])) {
341 goto do_init;
345 for (i=0; builtin_registry_values[i].path != NULL; i++) {
346 values = TALLOC_ZERO_P(frame, struct regval_ctr);
347 if (values == NULL) {
348 werr = WERR_NOMEM;
349 goto done;
352 regdb_fetch_values_internal(regdb,
353 builtin_registry_values[i].path,
354 values);
355 if (!regval_ctr_key_exists(values,
356 builtin_registry_values[i].valuename))
358 TALLOC_FREE(values);
359 goto do_init;
362 TALLOC_FREE(values);
365 werr = WERR_OK;
366 goto done;
368 do_init:
371 * There are potentially quite a few store operations which are all
372 * indiviually wrapped in tdb transactions. Wrapping them in a single
373 * transaction gives just a single transaction_commit() to actually do
374 * its fsync()s. See tdb/common/transaction.c for info about nested
375 * transaction behaviour.
378 werr = ntstatus_to_werror(dbwrap_trans_do(regdb,
379 init_registry_data_action,
380 NULL));
382 done:
383 TALLOC_FREE(frame);
384 return werr;
387 /***********************************************************************
388 Open the registry database
389 ***********************************************************************/
391 WERROR regdb_init(void)
393 const char *vstring = "INFO/version";
394 uint32 vers_id;
395 WERROR werr;
397 if (regdb) {
398 DEBUG(10, ("regdb_init: incrementing refcount (%d)\n",
399 regdb_refcount));
400 regdb_refcount++;
401 return WERR_OK;
404 regdb = db_open(NULL, state_path("registry.tdb"), 0,
405 REG_TDB_FLAGS, O_RDWR, 0600);
406 if (!regdb) {
407 regdb = db_open(NULL, state_path("registry.tdb"), 0,
408 REG_TDB_FLAGS, O_RDWR|O_CREAT, 0600);
409 if (!regdb) {
410 werr = ntstatus_to_werror(map_nt_error_from_unix(errno));
411 DEBUG(1,("regdb_init: Failed to open registry %s (%s)\n",
412 state_path("registry.tdb"), strerror(errno) ));
413 return werr;
416 DEBUG(10,("regdb_init: Successfully created registry tdb\n"));
419 regdb_refcount = 1;
421 vers_id = dbwrap_fetch_int32(regdb, vstring);
423 if ( vers_id != REGVER_V1 ) {
424 NTSTATUS status;
425 /* any upgrade code here if needed */
426 DEBUG(10, ("regdb_init: got %s = %d != %d\n", vstring,
427 vers_id, REGVER_V1));
428 status = dbwrap_trans_store_int32(regdb, vstring, REGVER_V1);
429 if (!NT_STATUS_IS_OK(status)) {
430 DEBUG(1, ("regdb_init: error storing %s = %d: %s\n",
431 vstring, REGVER_V1, nt_errstr(status)));
432 return ntstatus_to_werror(status);
433 } else {
434 DEBUG(10, ("regdb_init: stored %s = %d\n",
435 vstring, REGVER_V1));
439 return WERR_OK;
442 /***********************************************************************
443 Open the registry. Must already have been initialized by regdb_init()
444 ***********************************************************************/
446 WERROR regdb_open( void )
448 WERROR result = WERR_OK;
450 if ( regdb ) {
451 DEBUG(10,("regdb_open: incrementing refcount (%d)\n", regdb_refcount));
452 regdb_refcount++;
453 return WERR_OK;
456 become_root();
458 regdb = db_open(NULL, state_path("registry.tdb"), 0,
459 REG_TDB_FLAGS, O_RDWR, 0600);
460 if ( !regdb ) {
461 result = ntstatus_to_werror( map_nt_error_from_unix( errno ) );
462 DEBUG(0,("regdb_open: Failed to open %s! (%s)\n",
463 state_path("registry.tdb"), strerror(errno) ));
466 unbecome_root();
468 regdb_refcount = 1;
469 DEBUG(10,("regdb_open: refcount reset (%d)\n", regdb_refcount));
471 return result;
474 /***********************************************************************
475 ***********************************************************************/
477 int regdb_close( void )
479 if (regdb_refcount == 0) {
480 return 0;
483 regdb_refcount--;
485 DEBUG(10,("regdb_close: decrementing refcount (%d)\n", regdb_refcount));
487 if ( regdb_refcount > 0 )
488 return 0;
490 SMB_ASSERT( regdb_refcount >= 0 );
492 TALLOC_FREE(regdb);
493 return 0;
496 WERROR regdb_transaction_start(void)
498 return (regdb->transaction_start(regdb) == 0) ?
499 WERR_OK : WERR_REG_IO_FAILURE;
502 WERROR regdb_transaction_commit(void)
504 return (regdb->transaction_commit(regdb) == 0) ?
505 WERR_OK : WERR_REG_IO_FAILURE;
508 WERROR regdb_transaction_cancel(void)
510 return (regdb->transaction_cancel(regdb) == 0) ?
511 WERR_OK : WERR_REG_IO_FAILURE;
514 /***********************************************************************
515 return the tdb sequence number of the registry tdb.
516 this is an indicator for the content of the registry
517 having changed. it will change upon regdb_init, too, though.
518 ***********************************************************************/
519 int regdb_get_seqnum(void)
521 return regdb->get_seqnum(regdb);
525 static WERROR regdb_delete_key_with_prefix(struct db_context *db,
526 const char *keyname,
527 const char *prefix)
529 char *path;
530 WERROR werr = WERR_NOMEM;
531 TALLOC_CTX *mem_ctx = talloc_stackframe();
533 if (keyname == NULL) {
534 werr = WERR_INVALID_PARAM;
535 goto done;
538 if (prefix == NULL) {
539 path = discard_const_p(char, keyname);
540 } else {
541 path = talloc_asprintf(mem_ctx, "%s/%s", prefix, keyname);
542 if (path == NULL) {
543 goto done;
547 path = normalize_reg_path(mem_ctx, path);
548 if (path == NULL) {
549 goto done;
552 werr = ntstatus_to_werror(dbwrap_delete_bystring(db, path));
554 /* treat "not" found" as ok */
555 if (W_ERROR_EQUAL(werr, WERR_NOT_FOUND)) {
556 werr = WERR_OK;
559 done:
560 talloc_free(mem_ctx);
561 return werr;
565 static WERROR regdb_delete_values(struct db_context *db, const char *keyname)
567 return regdb_delete_key_with_prefix(db, keyname, REG_VALUE_PREFIX);
570 static WERROR regdb_delete_secdesc(struct db_context *db, const char *keyname)
572 return regdb_delete_key_with_prefix(db, keyname, REG_SECDESC_PREFIX);
575 static WERROR regdb_delete_subkeylist(struct db_context *db, const char *keyname)
577 return regdb_delete_key_with_prefix(db, keyname, NULL);
580 static WERROR regdb_delete_key_lists(struct db_context *db, const char *keyname)
582 WERROR werr;
584 werr = regdb_delete_values(db, keyname);
585 if (!W_ERROR_IS_OK(werr)) {
586 DEBUG(1, (__location__ " Deleting %s/%s failed: %s\n",
587 REG_VALUE_PREFIX, keyname, win_errstr(werr)));
588 goto done;
591 werr = regdb_delete_secdesc(db, keyname);
592 if (!W_ERROR_IS_OK(werr)) {
593 DEBUG(1, (__location__ " Deleting %s/%s failed: %s\n",
594 REG_SECDESC_PREFIX, keyname, win_errstr(werr)));
595 goto done;
598 werr = regdb_delete_subkeylist(db, keyname);
599 if (!W_ERROR_IS_OK(werr)) {
600 DEBUG(1, (__location__ " Deleting %s failed: %s\n",
601 keyname, win_errstr(werr)));
602 goto done;
605 done:
606 return werr;
609 /***********************************************************************
610 Add subkey strings to the registry tdb under a defined key
611 fmt is the same format as tdb_pack except this function only supports
612 fstrings
613 ***********************************************************************/
615 static WERROR regdb_store_keys_internal2(struct db_context *db,
616 const char *key,
617 struct regsubkey_ctr *ctr)
619 TDB_DATA dbuf;
620 uint8 *buffer = NULL;
621 int i = 0;
622 uint32 len, buflen;
623 uint32 num_subkeys = regsubkey_ctr_numkeys(ctr);
624 char *keyname = NULL;
625 TALLOC_CTX *ctx = talloc_stackframe();
626 WERROR werr;
628 if (!key) {
629 werr = WERR_INVALID_PARAM;
630 goto done;
633 keyname = talloc_strdup(ctx, key);
634 if (!keyname) {
635 werr = WERR_NOMEM;
636 goto done;
639 keyname = normalize_reg_path(ctx, keyname);
640 if (!keyname) {
641 werr = WERR_NOMEM;
642 goto done;
645 /* allocate some initial memory */
647 buffer = (uint8 *)SMB_MALLOC(1024);
648 if (buffer == NULL) {
649 werr = WERR_NOMEM;
650 goto done;
652 buflen = 1024;
653 len = 0;
655 /* store the number of subkeys */
657 len += tdb_pack(buffer+len, buflen-len, "d", num_subkeys);
659 /* pack all the strings */
661 for (i=0; i<num_subkeys; i++) {
662 size_t thistime;
664 thistime = tdb_pack(buffer+len, buflen-len, "f",
665 regsubkey_ctr_specific_key(ctr, i));
666 if (len+thistime > buflen) {
667 size_t thistime2;
669 * tdb_pack hasn't done anything because of the short
670 * buffer, allocate extra space.
672 buffer = SMB_REALLOC_ARRAY(buffer, uint8_t,
673 (len+thistime)*2);
674 if(buffer == NULL) {
675 DEBUG(0, ("regdb_store_keys: Failed to realloc "
676 "memory of size [%u]\n",
677 (unsigned int)(len+thistime)*2));
678 werr = WERR_NOMEM;
679 goto done;
681 buflen = (len+thistime)*2;
682 thistime2 = tdb_pack(
683 buffer+len, buflen-len, "f",
684 regsubkey_ctr_specific_key(ctr, i));
685 if (thistime2 != thistime) {
686 DEBUG(0, ("tdb_pack failed\n"));
687 werr = WERR_CAN_NOT_COMPLETE;
688 goto done;
691 len += thistime;
694 /* finally write out the data */
696 dbuf.dptr = buffer;
697 dbuf.dsize = len;
698 werr = ntstatus_to_werror(dbwrap_store_bystring(db, keyname, dbuf,
699 TDB_REPLACE));
700 W_ERROR_NOT_OK_GOTO_DONE(werr);
703 * Delete a sorted subkey cache for regdb_key_exists, will be
704 * recreated automatically
706 keyname = talloc_asprintf(ctx, "%s/%s", REG_SORTED_SUBKEYS_PREFIX,
707 keyname);
708 if (keyname == NULL) {
709 werr = WERR_NOMEM;
710 goto done;
713 werr = ntstatus_to_werror(dbwrap_delete_bystring(db, keyname));
715 /* don't treat WERR_NOT_FOUND as an error here */
716 if (W_ERROR_EQUAL(werr, WERR_NOT_FOUND)) {
717 werr = WERR_OK;
720 done:
721 TALLOC_FREE(ctx);
722 SAFE_FREE(buffer);
723 return werr;
726 /***********************************************************************
727 Store the new subkey record and create any child key records that
728 do not currently exist
729 ***********************************************************************/
731 struct regdb_store_keys_context {
732 const char *key;
733 struct regsubkey_ctr *ctr;
736 static NTSTATUS regdb_store_keys_action(struct db_context *db,
737 void *private_data)
739 struct regdb_store_keys_context *store_ctx;
740 WERROR werr;
741 int num_subkeys, i;
742 char *path = NULL;
743 struct regsubkey_ctr *subkeys = NULL, *old_subkeys = NULL;
744 char *oldkeyname = NULL;
745 TALLOC_CTX *mem_ctx = talloc_stackframe();
747 store_ctx = (struct regdb_store_keys_context *)private_data;
750 * Re-fetch the old keys inside the transaction
753 werr = regsubkey_ctr_init(mem_ctx, &old_subkeys);
754 W_ERROR_NOT_OK_GOTO_DONE(werr);
756 werr = regdb_fetch_keys_internal(db, store_ctx->key, old_subkeys);
757 if (!W_ERROR_IS_OK(werr) &&
758 !W_ERROR_EQUAL(werr, WERR_NOT_FOUND))
760 goto done;
764 * Make the store operation as safe as possible without transactions:
766 * (1) For each subkey removed from ctr compared with old_subkeys:
768 * (a) First delete the value db entry.
770 * (b) Next delete the secdesc db record.
772 * (c) Then delete the subkey list entry.
774 * (2) Now write the list of subkeys of the parent key,
775 * deleting removed entries and adding new ones.
777 * (3) Finally create the subkey list entries for the added keys.
779 * This way if we crash half-way in between deleting the subkeys
780 * and storing the parent's list of subkeys, no old data can pop up
781 * out of the blue when re-adding keys later on.
784 /* (1) delete removed keys' lists (values/secdesc/subkeys) */
786 num_subkeys = regsubkey_ctr_numkeys(old_subkeys);
787 for (i=0; i<num_subkeys; i++) {
788 oldkeyname = regsubkey_ctr_specific_key(old_subkeys, i);
790 if (regsubkey_ctr_key_exists(store_ctx->ctr, oldkeyname)) {
792 * It's still around, don't delete
794 continue;
797 path = talloc_asprintf(mem_ctx, "%s/%s", store_ctx->key,
798 oldkeyname);
799 if (!path) {
800 werr = WERR_NOMEM;
801 goto done;
804 werr = regdb_delete_key_lists(db, path);
805 W_ERROR_NOT_OK_GOTO_DONE(werr);
807 TALLOC_FREE(path);
810 TALLOC_FREE(old_subkeys);
812 /* (2) store the subkey list for the parent */
814 werr = regdb_store_keys_internal2(db, store_ctx->key, store_ctx->ctr);
815 if (!W_ERROR_IS_OK(werr)) {
816 DEBUG(0,("regdb_store_keys: Failed to store new subkey list "
817 "for parent [%s]: %s\n", store_ctx->key,
818 win_errstr(werr)));
819 goto done;
822 /* (3) now create records for any subkeys that don't already exist */
824 num_subkeys = regsubkey_ctr_numkeys(store_ctx->ctr);
826 if (num_subkeys == 0) {
827 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
828 W_ERROR_NOT_OK_GOTO_DONE(werr);
830 werr = regdb_store_keys_internal2(db, store_ctx->key, subkeys);
831 if (!W_ERROR_IS_OK(werr)) {
832 DEBUG(0,("regdb_store_keys: Failed to store "
833 "new record for key [%s]: %s\n",
834 store_ctx->key, win_errstr(werr)));
835 goto done;
837 TALLOC_FREE(subkeys);
840 for (i=0; i<num_subkeys; i++) {
841 path = talloc_asprintf(mem_ctx, "%s/%s", store_ctx->key,
842 regsubkey_ctr_specific_key(store_ctx->ctr, i));
843 if (!path) {
844 werr = WERR_NOMEM;
845 goto done;
847 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
848 W_ERROR_NOT_OK_GOTO_DONE(werr);
850 werr = regdb_fetch_keys_internal(db, path, subkeys);
851 if (!W_ERROR_IS_OK(werr)) {
852 /* create a record with 0 subkeys */
853 werr = regdb_store_keys_internal2(db, path, subkeys);
854 if (!W_ERROR_IS_OK(werr)) {
855 DEBUG(0,("regdb_store_keys: Failed to store "
856 "new record for key [%s]: %s\n", path,
857 win_errstr(werr)));
858 goto done;
862 TALLOC_FREE(subkeys);
863 TALLOC_FREE(path);
866 werr = WERR_OK;
868 done:
869 talloc_free(mem_ctx);
870 return werror_to_ntstatus(werr);
873 static bool regdb_store_keys_internal(struct db_context *db, const char *key,
874 struct regsubkey_ctr *ctr)
876 int num_subkeys, old_num_subkeys, i;
877 struct regsubkey_ctr *old_subkeys = NULL;
878 TALLOC_CTX *ctx = talloc_stackframe();
879 WERROR werr;
880 bool ret = false;
881 struct regdb_store_keys_context store_ctx;
883 if (!regdb_key_is_base_key(key) && !regdb_key_exists(db, key)) {
884 goto done;
888 * fetch a list of the old subkeys so we can determine if anything has
889 * changed
892 werr = regsubkey_ctr_init(ctx, &old_subkeys);
893 if (!W_ERROR_IS_OK(werr)) {
894 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
895 goto done;
898 werr = regdb_fetch_keys_internal(db, key, old_subkeys);
899 if (!W_ERROR_IS_OK(werr) &&
900 !W_ERROR_EQUAL(werr, WERR_NOT_FOUND))
902 goto done;
905 num_subkeys = regsubkey_ctr_numkeys(ctr);
906 old_num_subkeys = regsubkey_ctr_numkeys(old_subkeys);
907 if ((num_subkeys && old_num_subkeys) &&
908 (num_subkeys == old_num_subkeys)) {
910 for (i = 0; i < num_subkeys; i++) {
911 if (strcmp(regsubkey_ctr_specific_key(ctr, i),
912 regsubkey_ctr_specific_key(old_subkeys, i))
913 != 0)
915 break;
918 if (i == num_subkeys) {
920 * Nothing changed, no point to even start a tdb
921 * transaction
924 ret = true;
925 goto done;
929 TALLOC_FREE(old_subkeys);
931 store_ctx.key = key;
932 store_ctx.ctr = ctr;
934 werr = ntstatus_to_werror(dbwrap_trans_do(db,
935 regdb_store_keys_action,
936 &store_ctx));
938 ret = W_ERROR_IS_OK(werr);
940 done:
941 TALLOC_FREE(ctx);
943 return ret;
946 bool regdb_store_keys(const char *key, struct regsubkey_ctr *ctr)
948 return regdb_store_keys_internal(regdb, key, ctr);
952 * create a subkey of a given key
955 struct regdb_create_subkey_context {
956 const char *key;
957 const char *subkey;
960 static NTSTATUS regdb_create_subkey_action(struct db_context *db,
961 void *private_data)
963 WERROR werr;
964 struct regdb_create_subkey_context *create_ctx;
965 struct regsubkey_ctr *subkeys;
966 TALLOC_CTX *mem_ctx = talloc_stackframe();
968 create_ctx = (struct regdb_create_subkey_context *)private_data;
970 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
971 W_ERROR_NOT_OK_GOTO_DONE(werr);
973 werr = regdb_fetch_keys_internal(db, create_ctx->key, subkeys);
974 W_ERROR_NOT_OK_GOTO_DONE(werr);
976 werr = regsubkey_ctr_addkey(subkeys, create_ctx->subkey);
977 W_ERROR_NOT_OK_GOTO_DONE(werr);
979 werr = regdb_store_keys_internal2(db, create_ctx->key, subkeys);
980 if (!W_ERROR_IS_OK(werr)) {
981 DEBUG(0, (__location__ " failed to store new subkey list for "
982 "parent key %s: %s\n", create_ctx->key,
983 win_errstr(werr)));
986 done:
987 talloc_free(mem_ctx);
988 return werror_to_ntstatus(werr);
991 static WERROR regdb_create_subkey(const char *key, const char *subkey)
993 WERROR werr;
994 struct regsubkey_ctr *subkeys;
995 TALLOC_CTX *mem_ctx = talloc_stackframe();
996 struct regdb_create_subkey_context create_ctx;
998 if (!regdb_key_is_base_key(key) && !regdb_key_exists(regdb, key)) {
999 werr = WERR_NOT_FOUND;
1000 goto done;
1003 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
1004 W_ERROR_NOT_OK_GOTO_DONE(werr);
1006 werr = regdb_fetch_keys_internal(regdb, key, subkeys);
1007 W_ERROR_NOT_OK_GOTO_DONE(werr);
1009 if (regsubkey_ctr_key_exists(subkeys, subkey)) {
1010 werr = WERR_OK;
1011 goto done;
1014 talloc_free(subkeys);
1016 create_ctx.key = key;
1017 create_ctx.subkey = subkey;
1019 werr = ntstatus_to_werror(dbwrap_trans_do(regdb,
1020 regdb_create_subkey_action,
1021 &create_ctx));
1023 done:
1024 talloc_free(mem_ctx);
1025 return werr;
1029 * create a subkey of a given key
1032 struct regdb_delete_subkey_context {
1033 const char *key;
1034 const char *subkey;
1035 const char *path;
1038 static NTSTATUS regdb_delete_subkey_action(struct db_context *db,
1039 void *private_data)
1041 WERROR werr;
1042 struct regdb_delete_subkey_context *delete_ctx;
1043 struct regsubkey_ctr *subkeys;
1044 TALLOC_CTX *mem_ctx = talloc_stackframe();
1046 delete_ctx = (struct regdb_delete_subkey_context *)private_data;
1048 werr = regdb_delete_key_lists(db, delete_ctx->path);
1049 W_ERROR_NOT_OK_GOTO_DONE(werr);
1051 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
1052 W_ERROR_NOT_OK_GOTO_DONE(werr);
1054 werr = regdb_fetch_keys_internal(db, delete_ctx->key, subkeys);
1055 W_ERROR_NOT_OK_GOTO_DONE(werr);
1057 werr = regsubkey_ctr_delkey(subkeys, delete_ctx->subkey);
1058 W_ERROR_NOT_OK_GOTO_DONE(werr);
1060 werr = regdb_store_keys_internal2(db, delete_ctx->key, subkeys);
1061 if (!W_ERROR_IS_OK(werr)) {
1062 DEBUG(0, (__location__ " failed to store new subkey_list for "
1063 "parent key %s: %s\n", delete_ctx->key,
1064 win_errstr(werr)));
1067 done:
1068 talloc_free(mem_ctx);
1069 return werror_to_ntstatus(werr);
1072 static WERROR regdb_delete_subkey(const char *key, const char *subkey)
1074 WERROR werr;
1075 char *path;
1076 struct regdb_delete_subkey_context delete_ctx;
1077 TALLOC_CTX *mem_ctx = talloc_stackframe();
1079 if (!regdb_key_is_base_key(key) && !regdb_key_exists(regdb, key)) {
1080 werr = WERR_NOT_FOUND;
1081 goto done;
1084 path = talloc_asprintf(mem_ctx, "%s/%s", key, subkey);
1085 if (path == NULL) {
1086 werr = WERR_NOMEM;
1087 goto done;
1090 if (!regdb_key_exists(regdb, path)) {
1091 werr = WERR_OK;
1092 goto done;
1095 delete_ctx.key = key;
1096 delete_ctx.subkey = subkey;
1097 delete_ctx.path = path;
1099 werr = ntstatus_to_werror(dbwrap_trans_do(regdb,
1100 regdb_delete_subkey_action,
1101 &delete_ctx));
1103 done:
1104 talloc_free(mem_ctx);
1105 return werr;
1108 static TDB_DATA regdb_fetch_key_internal(struct db_context *db,
1109 TALLOC_CTX *mem_ctx, const char *key)
1111 char *path = NULL;
1112 TDB_DATA data;
1114 path = normalize_reg_path(mem_ctx, key);
1115 if (!path) {
1116 return make_tdb_data(NULL, 0);
1119 data = dbwrap_fetch_bystring(db, mem_ctx, path);
1121 TALLOC_FREE(path);
1122 return data;
1127 * check whether a given key name represents a base key,
1128 * i.e one without a subkey separator ('/' or '\').
1130 static bool regdb_key_is_base_key(const char *key)
1132 TALLOC_CTX *mem_ctx = talloc_stackframe();
1133 bool ret = false;
1134 char *path;
1136 if (key == NULL) {
1137 goto done;
1140 path = normalize_reg_path(mem_ctx, key);
1141 if (path == NULL) {
1142 DEBUG(0, ("out of memory! (talloc failed)\n"));
1143 goto done;
1146 if (*path == '\0') {
1147 goto done;
1150 ret = (strrchr(path, '/') == NULL);
1152 done:
1153 TALLOC_FREE(mem_ctx);
1154 return ret;
1158 * regdb_key_exists() is a very frequent operation. It can be quite
1159 * time-consuming to fully fetch the parent's subkey list, talloc_strdup all
1160 * subkeys and then compare the keyname linearly to all the parent's subkeys.
1162 * The following code tries to make this operation as efficient as possible:
1163 * Per registry key we create a list of subkeys that is very efficient to
1164 * search for existence of a subkey. Its format is:
1166 * 4 bytes num_subkeys
1167 * 4*num_subkey bytes offset into the string array
1168 * then follows a sorted list of subkeys in uppercase
1170 * This record is created by create_sorted_subkeys() on demand if it does not
1171 * exist. scan_parent_subkeys() uses regdb->parse_record to search the sorted
1172 * list, the parsing code and the binary search can be found in
1173 * parent_subkey_scanner. The code uses parse_record() to avoid a memcpy of
1174 * the potentially large subkey record.
1176 * The sorted subkey record is deleted in regdb_store_keys_internal2 and
1177 * recreated on demand.
1180 static int cmp_keynames(char **p1, char **p2)
1182 return StrCaseCmp(*p1, *p2);
1185 struct create_sorted_subkeys_context {
1186 const char *key;
1187 const char *sorted_keyname;
1190 static NTSTATUS create_sorted_subkeys_action(struct db_context *db,
1191 void *private_data)
1193 char **sorted_subkeys;
1194 struct regsubkey_ctr *ctr;
1195 NTSTATUS status;
1196 char *buf;
1197 char *p;
1198 int i;
1199 size_t len;
1200 int num_subkeys;
1201 struct create_sorted_subkeys_context *sorted_ctx;
1203 sorted_ctx = (struct create_sorted_subkeys_context *)private_data;
1206 * In this function, we only treat failing of the actual write to
1207 * the db as a real error. All preliminary errors, at a stage when
1208 * nothing has been written to the DB yet are treated as success
1209 * to be committed (as an empty transaction).
1211 * The reason is that this (disposable) call might be nested in other
1212 * transactions. Doing a cancel here would destroy the possibility of
1213 * a transaction_commit for transactions that we might be wrapped in.
1216 status = werror_to_ntstatus(regsubkey_ctr_init(talloc_tos(), &ctr));
1217 if (!NT_STATUS_IS_OK(status)) {
1218 /* don't treat this as an error */
1219 status = NT_STATUS_OK;
1220 goto done;
1223 status = werror_to_ntstatus(regdb_fetch_keys_internal(db,
1224 sorted_ctx->key,
1225 ctr));
1226 if (!NT_STATUS_IS_OK(status)) {
1227 /* don't treat this as an error */
1228 status = NT_STATUS_OK;
1229 goto done;
1232 num_subkeys = regsubkey_ctr_numkeys(ctr);
1233 sorted_subkeys = talloc_array(ctr, char *, num_subkeys);
1234 if (sorted_subkeys == NULL) {
1235 /* don't treat this as an error */
1236 goto done;
1239 len = 4 + 4*num_subkeys;
1241 for (i = 0; i < num_subkeys; i++) {
1242 sorted_subkeys[i] = talloc_strdup_upper(sorted_subkeys,
1243 regsubkey_ctr_specific_key(ctr, i));
1244 if (sorted_subkeys[i] == NULL) {
1245 /* don't treat this as an error */
1246 goto done;
1248 len += strlen(sorted_subkeys[i])+1;
1251 TYPESAFE_QSORT(sorted_subkeys, num_subkeys, cmp_keynames);
1253 buf = talloc_array(ctr, char, len);
1254 if (buf == NULL) {
1255 /* don't treat this as an error */
1256 goto done;
1258 p = buf + 4 + 4*num_subkeys;
1260 SIVAL(buf, 0, num_subkeys);
1262 for (i=0; i < num_subkeys; i++) {
1263 ptrdiff_t offset = p - buf;
1264 SIVAL(buf, 4 + 4*i, offset);
1265 strlcpy(p, sorted_subkeys[i], len-offset);
1266 p += strlen(sorted_subkeys[i]) + 1;
1269 status = dbwrap_store_bystring(
1270 db, sorted_ctx->sorted_keyname, make_tdb_data((uint8_t *)buf,
1271 len),
1272 TDB_REPLACE);
1274 done:
1275 talloc_free(ctr);
1276 return status;
1279 static bool create_sorted_subkeys(const char *key, const char *sorted_keyname)
1281 NTSTATUS status;
1282 struct create_sorted_subkeys_context sorted_ctx;
1284 sorted_ctx.key = key;
1285 sorted_ctx.sorted_keyname = sorted_keyname;
1287 status = dbwrap_trans_do(regdb,
1288 create_sorted_subkeys_action,
1289 &sorted_ctx);
1291 return NT_STATUS_IS_OK(status);
1294 struct scan_subkey_state {
1295 char *name;
1296 bool scanned;
1297 bool found;
1300 static int parent_subkey_scanner(TDB_DATA key, TDB_DATA data,
1301 void *private_data)
1303 struct scan_subkey_state *state =
1304 (struct scan_subkey_state *)private_data;
1305 uint32_t num_subkeys;
1306 uint32_t l, u;
1308 if (data.dsize < sizeof(uint32_t)) {
1309 return -1;
1312 state->scanned = true;
1313 state->found = false;
1315 tdb_unpack(data.dptr, data.dsize, "d", &num_subkeys);
1317 l = 0;
1318 u = num_subkeys;
1320 while (l < u) {
1321 uint32_t idx = (l+u)/2;
1322 char *s = (char *)data.dptr + IVAL(data.dptr, 4 + 4*idx);
1323 int comparison = strcmp(state->name, s);
1325 if (comparison < 0) {
1326 u = idx;
1327 } else if (comparison > 0) {
1328 l = idx + 1;
1329 } else {
1330 state->found = true;
1331 return 0;
1334 return 0;
1337 static bool scan_parent_subkeys(struct db_context *db, const char *parent,
1338 const char *name)
1340 char *path = NULL;
1341 char *key = NULL;
1342 struct scan_subkey_state state = { 0, };
1343 bool result = false;
1344 int res;
1346 state.name = NULL;
1348 path = normalize_reg_path(talloc_tos(), parent);
1349 if (path == NULL) {
1350 goto fail;
1353 key = talloc_asprintf(talloc_tos(), "%s/%s",
1354 REG_SORTED_SUBKEYS_PREFIX, path);
1355 if (key == NULL) {
1356 goto fail;
1359 state.name = talloc_strdup_upper(talloc_tos(), name);
1360 if (state.name == NULL) {
1361 goto fail;
1363 state.scanned = false;
1365 res = db->parse_record(db, string_term_tdb_data(key),
1366 parent_subkey_scanner, &state);
1368 if (state.scanned) {
1369 result = state.found;
1370 } else {
1371 res = db->transaction_start(db);
1372 if (res != 0) {
1373 DEBUG(0, ("error starting transacion\n"));
1374 goto fail;
1377 if (!create_sorted_subkeys(path, key)) {
1378 res = db->transaction_cancel(db);
1379 if (res != 0) {
1380 smb_panic("Failed to cancel transaction.");
1382 goto fail;
1385 res = db->parse_record(db, string_term_tdb_data(key),
1386 parent_subkey_scanner, &state);
1387 if ((res == 0) && (state.scanned)) {
1388 result = state.found;
1391 res = db->transaction_commit(db);
1392 if (res != 0) {
1393 DEBUG(0, ("error committing transaction\n"));
1394 result = false;
1398 fail:
1399 TALLOC_FREE(path);
1400 TALLOC_FREE(state.name);
1401 return result;
1405 * Check for the existence of a key.
1407 * Existence of a key is authoritatively defined by its
1408 * existence in the list of subkeys of its parent key.
1409 * The exeption of this are keys without a parent key,
1410 * i.e. the "base" keys (HKLM, HKCU, ...).
1412 static bool regdb_key_exists(struct db_context *db, const char *key)
1414 TALLOC_CTX *mem_ctx = talloc_stackframe();
1415 TDB_DATA value;
1416 bool ret = false;
1417 char *path, *p;
1419 if (key == NULL) {
1420 goto done;
1423 path = normalize_reg_path(mem_ctx, key);
1424 if (path == NULL) {
1425 DEBUG(0, ("out of memory! (talloc failed)\n"));
1426 goto done;
1429 if (*path == '\0') {
1430 goto done;
1433 p = strrchr(path, '/');
1434 if (p == NULL) {
1435 /* this is a base key */
1436 value = regdb_fetch_key_internal(db, mem_ctx, path);
1437 ret = (value.dptr != NULL);
1438 } else {
1439 *p = '\0';
1440 ret = scan_parent_subkeys(db, path, p+1);
1443 done:
1444 TALLOC_FREE(mem_ctx);
1445 return ret;
1449 /***********************************************************************
1450 Retrieve an array of strings containing subkeys. Memory should be
1451 released by the caller.
1452 ***********************************************************************/
1454 static WERROR regdb_fetch_keys_internal(struct db_context *db, const char *key,
1455 struct regsubkey_ctr *ctr)
1457 WERROR werr;
1458 uint32_t num_items;
1459 uint8 *buf;
1460 uint32 buflen, len;
1461 int i;
1462 fstring subkeyname;
1463 TALLOC_CTX *frame = talloc_stackframe();
1464 TDB_DATA value;
1466 DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key ? key : "NULL"));
1468 frame = talloc_stackframe();
1470 if (!regdb_key_exists(db, key)) {
1471 DEBUG(10, ("key [%s] not found\n", key));
1472 werr = WERR_NOT_FOUND;
1473 goto done;
1476 werr = regsubkey_ctr_set_seqnum(ctr, db->get_seqnum(db));
1477 W_ERROR_NOT_OK_GOTO_DONE(werr);
1479 value = regdb_fetch_key_internal(db, frame, key);
1481 if (value.dsize == 0 || value.dptr == NULL) {
1482 DEBUG(10, ("regdb_fetch_keys: no subkeys found for key [%s]\n",
1483 key));
1484 goto done;
1487 buf = value.dptr;
1488 buflen = value.dsize;
1489 len = tdb_unpack( buf, buflen, "d", &num_items);
1490 if (len == (uint32_t)-1) {
1491 werr = WERR_NOT_FOUND;
1492 goto done;
1495 werr = regsubkey_ctr_reinit(ctr);
1496 W_ERROR_NOT_OK_GOTO_DONE(werr);
1498 for (i=0; i<num_items; i++) {
1499 len += tdb_unpack(buf+len, buflen-len, "f", subkeyname);
1500 werr = regsubkey_ctr_addkey(ctr, subkeyname);
1501 if (!W_ERROR_IS_OK(werr)) {
1502 DEBUG(5, ("regdb_fetch_keys: regsubkey_ctr_addkey "
1503 "failed: %s\n", win_errstr(werr)));
1504 num_items = 0;
1505 goto done;
1509 DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items));
1511 done:
1512 TALLOC_FREE(frame);
1513 return werr;
1516 int regdb_fetch_keys(const char *key, struct regsubkey_ctr *ctr)
1518 WERROR werr;
1520 werr = regdb_fetch_keys_internal(regdb, key, ctr);
1521 if (!W_ERROR_IS_OK(werr)) {
1522 return -1;
1525 return regsubkey_ctr_numkeys(ctr);
1528 /****************************************************************************
1529 Unpack a list of registry values frem the TDB
1530 ***************************************************************************/
1532 static int regdb_unpack_values(struct regval_ctr *values, uint8 *buf, int buflen)
1534 int len = 0;
1535 uint32 type;
1536 fstring valuename;
1537 uint32 size;
1538 uint8 *data_p;
1539 uint32 num_values = 0;
1540 int i;
1542 /* loop and unpack the rest of the registry values */
1544 len += tdb_unpack(buf+len, buflen-len, "d", &num_values);
1546 for ( i=0; i<num_values; i++ ) {
1547 /* unpack the next regval */
1549 type = REG_NONE;
1550 size = 0;
1551 data_p = NULL;
1552 valuename[0] = '\0';
1553 len += tdb_unpack(buf+len, buflen-len, "fdB",
1554 valuename,
1555 &type,
1556 &size,
1557 &data_p);
1559 /* add the new value. Paranoid protective code -- make sure data_p is valid */
1561 if (*valuename && size && data_p) {
1562 regval_ctr_addvalue(values, valuename, type,
1563 (const char *)data_p, size);
1565 SAFE_FREE(data_p); /* 'B' option to tdb_unpack does a malloc() */
1567 DEBUG(8,("specific: [%s], len: %d\n", valuename, size));
1570 return len;
1573 /****************************************************************************
1574 Pack all values in all printer keys
1575 ***************************************************************************/
1577 static int regdb_pack_values(struct regval_ctr *values, uint8 *buf, int buflen)
1579 int len = 0;
1580 int i;
1581 struct regval_blob *val;
1582 int num_values;
1584 if ( !values )
1585 return 0;
1587 num_values = regval_ctr_numvals( values );
1589 /* pack the number of values first */
1591 len += tdb_pack( buf+len, buflen-len, "d", num_values );
1593 /* loop over all values */
1595 for ( i=0; i<num_values; i++ ) {
1596 val = regval_ctr_specific_value( values, i );
1597 len += tdb_pack(buf+len, buflen-len, "fdB",
1598 regval_name(val),
1599 regval_type(val),
1600 regval_size(val),
1601 regval_data_p(val) );
1604 return len;
1607 /***********************************************************************
1608 Retrieve an array of strings containing subkeys. Memory should be
1609 released by the caller.
1610 ***********************************************************************/
1612 static int regdb_fetch_values_internal(struct db_context *db, const char* key,
1613 struct regval_ctr *values)
1615 char *keystr = NULL;
1616 TALLOC_CTX *ctx = talloc_stackframe();
1617 int ret = 0;
1618 TDB_DATA value;
1620 DEBUG(10,("regdb_fetch_values: Looking for value of key [%s] \n", key));
1622 if (!regdb_key_exists(db, key)) {
1623 goto done;
1626 keystr = talloc_asprintf(ctx, "%s/%s", REG_VALUE_PREFIX, key);
1627 if (!keystr) {
1628 goto done;
1631 values->seqnum = db->get_seqnum(db);
1633 value = regdb_fetch_key_internal(db, ctx, keystr);
1635 if (!value.dptr) {
1636 /* all keys have zero values by default */
1637 goto done;
1640 regdb_unpack_values(values, value.dptr, value.dsize);
1641 ret = regval_ctr_numvals(values);
1643 done:
1644 TALLOC_FREE(ctx);
1645 return ret;
1648 int regdb_fetch_values(const char* key, struct regval_ctr *values)
1650 return regdb_fetch_values_internal(regdb, key, values);
1653 static bool regdb_store_values_internal(struct db_context *db, const char *key,
1654 struct regval_ctr *values)
1656 TDB_DATA old_data, data;
1657 char *keystr = NULL;
1658 TALLOC_CTX *ctx = talloc_stackframe();
1659 int len;
1660 NTSTATUS status;
1661 bool result = false;
1663 DEBUG(10,("regdb_store_values: Looking for value of key [%s] \n", key));
1665 if (!regdb_key_exists(db, key)) {
1666 goto done;
1669 ZERO_STRUCT(data);
1671 len = regdb_pack_values(values, data.dptr, data.dsize);
1672 if (len <= 0) {
1673 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
1674 goto done;
1677 data.dptr = TALLOC_ARRAY(ctx, uint8, len);
1678 data.dsize = len;
1680 len = regdb_pack_values(values, data.dptr, data.dsize);
1682 SMB_ASSERT( len == data.dsize );
1684 keystr = talloc_asprintf(ctx, "%s/%s", REG_VALUE_PREFIX, key );
1685 if (!keystr) {
1686 goto done;
1688 keystr = normalize_reg_path(ctx, keystr);
1689 if (!keystr) {
1690 goto done;
1693 old_data = dbwrap_fetch_bystring(db, ctx, keystr);
1695 if ((old_data.dptr != NULL)
1696 && (old_data.dsize == data.dsize)
1697 && (memcmp(old_data.dptr, data.dptr, data.dsize) == 0))
1699 result = true;
1700 goto done;
1703 status = dbwrap_trans_store_bystring(db, keystr, data, TDB_REPLACE);
1705 result = NT_STATUS_IS_OK(status);
1707 done:
1708 TALLOC_FREE(ctx);
1709 return result;
1712 bool regdb_store_values(const char *key, struct regval_ctr *values)
1714 return regdb_store_values_internal(regdb, key, values);
1717 static WERROR regdb_get_secdesc(TALLOC_CTX *mem_ctx, const char *key,
1718 struct security_descriptor **psecdesc)
1720 char *tdbkey;
1721 TDB_DATA data;
1722 NTSTATUS status;
1723 TALLOC_CTX *tmp_ctx = talloc_stackframe();
1724 WERROR err = WERR_OK;
1726 DEBUG(10, ("regdb_get_secdesc: Getting secdesc of key [%s]\n", key));
1728 if (!regdb_key_exists(regdb, key)) {
1729 err = WERR_BADFILE;
1730 goto done;
1733 tdbkey = talloc_asprintf(tmp_ctx, "%s/%s", REG_SECDESC_PREFIX, key);
1734 if (tdbkey == NULL) {
1735 err = WERR_NOMEM;
1736 goto done;
1738 normalize_dbkey(tdbkey);
1740 data = dbwrap_fetch_bystring(regdb, tmp_ctx, tdbkey);
1741 if (data.dptr == NULL) {
1742 err = WERR_BADFILE;
1743 goto done;
1746 status = unmarshall_sec_desc(mem_ctx, (uint8 *)data.dptr, data.dsize,
1747 psecdesc);
1749 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MEMORY)) {
1750 err = WERR_NOMEM;
1751 } else if (!NT_STATUS_IS_OK(status)) {
1752 err = WERR_REG_CORRUPT;
1755 done:
1756 TALLOC_FREE(tmp_ctx);
1757 return err;
1760 static WERROR regdb_set_secdesc(const char *key,
1761 struct security_descriptor *secdesc)
1763 TALLOC_CTX *mem_ctx = talloc_stackframe();
1764 char *tdbkey;
1765 WERROR err = WERR_NOMEM;
1766 TDB_DATA tdbdata;
1768 if (!regdb_key_exists(regdb, key)) {
1769 err = WERR_BADFILE;
1770 goto done;
1773 tdbkey = talloc_asprintf(mem_ctx, "%s/%s", REG_SECDESC_PREFIX, key);
1774 if (tdbkey == NULL) {
1775 goto done;
1777 normalize_dbkey(tdbkey);
1779 if (secdesc == NULL) {
1780 /* assuming a delete */
1781 err = ntstatus_to_werror(dbwrap_trans_delete_bystring(regdb,
1782 tdbkey));
1783 goto done;
1786 err = ntstatus_to_werror(marshall_sec_desc(mem_ctx, secdesc,
1787 &tdbdata.dptr,
1788 &tdbdata.dsize));
1789 W_ERROR_NOT_OK_GOTO_DONE(err);
1791 err = ntstatus_to_werror(dbwrap_trans_store_bystring(regdb, tdbkey,
1792 tdbdata, 0));
1794 done:
1795 TALLOC_FREE(mem_ctx);
1796 return err;
1799 bool regdb_subkeys_need_update(struct regsubkey_ctr *subkeys)
1801 return (regdb_get_seqnum() != regsubkey_ctr_get_seqnum(subkeys));
1804 bool regdb_values_need_update(struct regval_ctr *values)
1806 return (regdb_get_seqnum() != values->seqnum);
1810 * Table of function pointers for default access
1813 struct registry_ops regdb_ops = {
1814 .fetch_subkeys = regdb_fetch_keys,
1815 .fetch_values = regdb_fetch_values,
1816 .store_subkeys = regdb_store_keys,
1817 .store_values = regdb_store_values,
1818 .create_subkey = regdb_create_subkey,
1819 .delete_subkey = regdb_delete_subkey,
1820 .get_secdesc = regdb_get_secdesc,
1821 .set_secdesc = regdb_set_secdesc,
1822 .subkeys_need_update = regdb_subkeys_need_update,
1823 .values_need_update = regdb_values_need_update