s3:registry: fix regdb_key_exists: the record has to contain at least the 4-byte...
[Samba.git] / source3 / registry / reg_backend_db.c
blob4e10bf652a5adf7e77950cfac74b4019770fffd1
1 /*
2 * Unix SMB/CIFS implementation.
3 * Virtual Windows Registry Layer
4 * Copyright (C) Gerald Carter 2002-2005
5 * Copyright (C) Michael Adam 2007-2011
6 * Copyright (C) Gregor Beck 2011
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 /* Implementation of internal registry database functions. */
24 #include "includes.h"
25 #include "system/filesys.h"
26 #include "registry.h"
27 #include "reg_db.h"
28 #include "reg_util_internal.h"
29 #include "reg_backend_db.h"
30 #include "reg_objects.h"
31 #include "nt_printing.h"
32 #include "util_tdb.h"
33 #include "dbwrap/dbwrap.h"
34 #include "dbwrap/dbwrap_open.h"
35 #include "../libcli/security/secdesc.h"
37 #undef DBGC_CLASS
38 #define DBGC_CLASS DBGC_REGISTRY
40 static struct db_context *regdb = NULL;
41 static int regdb_refcount;
43 static bool regdb_key_exists(struct db_context *db, const char *key);
44 static WERROR regdb_fetch_keys_internal(struct db_context *db, const char *key,
45 struct regsubkey_ctr *ctr);
46 static bool regdb_store_keys_internal(struct db_context *db, const char *key,
47 struct regsubkey_ctr *ctr);
48 static int regdb_fetch_values_internal(struct db_context *db, const char* key,
49 struct regval_ctr *values);
50 static bool regdb_store_values_internal(struct db_context *db, const char *key,
51 struct regval_ctr *values);
52 static WERROR regdb_store_subkey_list(struct db_context *db, const char *parent,
53 const char *key);
55 static WERROR regdb_create_basekey(struct db_context *db, const char *key);
56 static WERROR regdb_create_subkey_internal(struct db_context *db,
57 const char *key,
58 const char *subkey);
60 /* List the deepest path into the registry. All part components will be created.*/
62 /* If you want to have a part of the path controlled by the tdb and part by
63 a virtual registry db (e.g. printing), then you have to list the deepest path.
64 For example,"HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion/Print"
65 allows the reg_db backend to handle everything up to
66 "HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion" and then we'll hook
67 the reg_printing backend onto the last component of the path (see
68 KEY_PRINTING_2K in include/rpc_reg.h) --jerry */
70 static const char *builtin_registry_paths[] = {
71 KEY_PRINTING_2K,
72 KEY_PRINTING_PORTS,
73 KEY_PRINTING,
74 KEY_PRINTING "\\Forms",
75 KEY_PRINTING "\\Printers",
76 KEY_PRINTING "\\Environments\\Windows NT x86\\Print Processors\\winprint",
77 KEY_SHARES,
78 KEY_EVENTLOG,
79 KEY_SMBCONF,
80 KEY_PERFLIB,
81 KEY_PERFLIB_009,
82 KEY_GROUP_POLICY,
83 KEY_SAMBA_GROUP_POLICY,
84 KEY_GP_MACHINE_POLICY,
85 KEY_GP_MACHINE_WIN_POLICY,
86 KEY_HKCU,
87 KEY_GP_USER_POLICY,
88 KEY_GP_USER_WIN_POLICY,
89 "HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon\\GPExtensions",
90 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Print\\Monitors",
91 KEY_PROD_OPTIONS,
92 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\DefaultUserConfiguration",
93 KEY_TCPIP_PARAMS,
94 KEY_NETLOGON_PARAMS,
95 KEY_HKU,
96 KEY_HKCR,
97 KEY_HKPD,
98 KEY_HKPT,
99 NULL };
101 struct builtin_regkey_value {
102 const char *path;
103 const char *valuename;
104 uint32 type;
105 union {
106 const char *string;
107 uint32 dw_value;
108 } data;
111 static struct builtin_regkey_value builtin_registry_values[] = {
112 { KEY_PRINTING_PORTS,
113 SAMBA_PRINTER_PORT_NAME, REG_SZ, { "" } },
114 { KEY_PRINTING_2K,
115 "DefaultSpoolDirectory", REG_SZ, { "C:\\Windows\\System32\\Spool\\Printers" } },
116 { KEY_EVENTLOG,
117 "DisplayName", REG_SZ, { "Event Log" } },
118 { KEY_EVENTLOG,
119 "ErrorControl", REG_DWORD, { (char*)0x00000001 } },
120 { NULL, NULL, 0, { NULL } }
123 static WERROR create_key_recursive(struct db_context *db,
124 char *path,
125 const char *subkey)
127 WERROR werr;
128 char *p;
130 if (subkey == NULL) {
131 return WERR_INVALID_PARAM;
134 if (path == NULL) {
135 return regdb_create_basekey(db, subkey);
138 p = strrchr_m(path, '\\');
140 if (p == NULL) {
141 werr = create_key_recursive(db, NULL, path);
142 } else {
143 *p = '\0';
144 werr = create_key_recursive(db, path, p+1);
145 *p = '\\';
148 if (!W_ERROR_IS_OK(werr)) {
149 goto done;
152 werr = regdb_create_subkey_internal(db, path, subkey);
154 done:
155 return werr;
159 * Initialize a key in the registry:
160 * create each component key of the specified path.
162 static WERROR init_registry_key_internal(struct db_context *db,
163 const char *add_path)
165 char *subkey, *key;
166 WERROR werr;
167 TALLOC_CTX *frame = talloc_stackframe();
169 if (add_path == NULL) {
170 werr = WERR_INVALID_PARAM;
171 goto done;
174 key = talloc_strdup(frame, add_path);
176 subkey = strrchr_m(key, '\\');
177 if (subkey == NULL) {
178 subkey = key;
179 key = NULL;
180 } else {
181 *subkey = '\0';
182 subkey++;
185 werr = create_key_recursive(db, key, subkey);
187 done:
188 talloc_free(frame);
189 return werr;
192 struct init_registry_key_context {
193 const char *add_path;
196 static NTSTATUS init_registry_key_action(struct db_context *db,
197 void *private_data)
199 struct init_registry_key_context *init_ctx =
200 (struct init_registry_key_context *)private_data;
202 return werror_to_ntstatus(init_registry_key_internal(
203 db, init_ctx->add_path));
207 * Initialize a key in the registry:
208 * create each component key of the specified path,
209 * wrapped in one db transaction.
211 WERROR init_registry_key(const char *add_path)
213 struct init_registry_key_context init_ctx;
215 if (regdb_key_exists(regdb, add_path)) {
216 return WERR_OK;
219 init_ctx.add_path = add_path;
221 return ntstatus_to_werror(dbwrap_trans_do(regdb,
222 init_registry_key_action,
223 &init_ctx));
226 /***********************************************************************
227 Open the registry data in the tdb
228 ***********************************************************************/
230 static void regdb_ctr_add_value(struct regval_ctr *ctr,
231 struct builtin_regkey_value *value)
233 switch(value->type) {
234 case REG_DWORD:
235 regval_ctr_addvalue(ctr, value->valuename, REG_DWORD,
236 (uint8_t *)&value->data.dw_value,
237 sizeof(uint32));
238 break;
240 case REG_SZ:
241 regval_ctr_addvalue_sz(ctr, value->valuename,
242 value->data.string);
243 break;
245 default:
246 DEBUG(0, ("regdb_ctr_add_value: invalid value type in "
247 "registry values [%d]\n", value->type));
251 static NTSTATUS init_registry_data_action(struct db_context *db,
252 void *private_data)
254 NTSTATUS status;
255 TALLOC_CTX *frame = talloc_stackframe();
256 struct regval_ctr *values;
257 int i;
259 /* loop over all of the predefined paths and add each component */
261 for (i=0; builtin_registry_paths[i] != NULL; i++) {
262 if (regdb_key_exists(db, builtin_registry_paths[i])) {
263 continue;
265 status = werror_to_ntstatus(init_registry_key_internal(db,
266 builtin_registry_paths[i]));
267 if (!NT_STATUS_IS_OK(status)) {
268 goto done;
272 /* loop over all of the predefined values and add each component */
274 for (i=0; builtin_registry_values[i].path != NULL; i++) {
275 WERROR werr;
277 werr = regval_ctr_init(frame, &values);
278 if (!W_ERROR_IS_OK(werr)) {
279 status = werror_to_ntstatus(werr);
280 goto done;
283 regdb_fetch_values_internal(db,
284 builtin_registry_values[i].path,
285 values);
287 /* preserve existing values across restarts. Only add new ones */
289 if (!regval_ctr_key_exists(values,
290 builtin_registry_values[i].valuename))
292 regdb_ctr_add_value(values,
293 &builtin_registry_values[i]);
294 regdb_store_values_internal(db,
295 builtin_registry_values[i].path,
296 values);
298 TALLOC_FREE(values);
301 status = NT_STATUS_OK;
303 done:
305 TALLOC_FREE(frame);
306 return status;
309 WERROR init_registry_data(void)
311 WERROR werr;
312 TALLOC_CTX *frame = talloc_stackframe();
313 struct regval_ctr *values;
314 int i;
317 * First, check for the existence of the needed keys and values.
318 * If all do already exist, we can save the writes.
320 for (i=0; builtin_registry_paths[i] != NULL; i++) {
321 if (!regdb_key_exists(regdb, builtin_registry_paths[i])) {
322 goto do_init;
326 for (i=0; builtin_registry_values[i].path != NULL; i++) {
327 werr = regval_ctr_init(frame, &values);
328 W_ERROR_NOT_OK_GOTO_DONE(werr);
330 regdb_fetch_values_internal(regdb,
331 builtin_registry_values[i].path,
332 values);
333 if (!regval_ctr_key_exists(values,
334 builtin_registry_values[i].valuename))
336 TALLOC_FREE(values);
337 goto do_init;
340 TALLOC_FREE(values);
343 werr = WERR_OK;
344 goto done;
346 do_init:
349 * There are potentially quite a few store operations which are all
350 * indiviually wrapped in tdb transactions. Wrapping them in a single
351 * transaction gives just a single transaction_commit() to actually do
352 * its fsync()s. See tdb/common/transaction.c for info about nested
353 * transaction behaviour.
356 werr = ntstatus_to_werror(dbwrap_trans_do(regdb,
357 init_registry_data_action,
358 NULL));
360 done:
361 TALLOC_FREE(frame);
362 return werr;
365 static int regdb_normalize_keynames_fn(struct db_record *rec,
366 void *private_data)
368 TALLOC_CTX *mem_ctx = talloc_tos();
369 const char *keyname;
370 NTSTATUS status;
372 if (rec->key.dptr == NULL || rec->key.dsize == 0) {
373 return 0;
376 keyname = strchr((const char *) rec->key.dptr, '/');
377 if (keyname) {
378 struct db_record new_rec;
380 keyname = talloc_string_sub(mem_ctx,
381 (const char *) rec->key.dptr,
382 "/",
383 "\\");
385 DEBUG(2, ("regdb_normalize_keynames_fn: Convert %s to %s\n",
386 (const char *) rec->key.dptr,
387 keyname));
389 new_rec.value = rec->value;
390 new_rec.key = string_term_tdb_data(keyname);
391 new_rec.private_data = rec->private_data;
393 /* Delete the original record and store the normalized key */
394 status = rec->delete_rec(rec);
395 if (!NT_STATUS_IS_OK(status)) {
396 DEBUG(0,("regdb_normalize_keynames_fn: "
397 "tdb_delete for [%s] failed!\n",
398 rec->key.dptr));
399 return 1;
402 status = rec->store(&new_rec, new_rec.value, TDB_REPLACE);
403 if (!NT_STATUS_IS_OK(status)) {
404 DEBUG(0,("regdb_normalize_keynames_fn: "
405 "failed to store new record for [%s]!\n",
406 keyname));
407 return 1;
411 return 0;
414 static WERROR regdb_store_regdb_version(struct db_context *db, uint32_t version)
416 NTSTATUS status;
417 const char *version_keyname = "INFO/version";
419 if (db == NULL) {
420 return WERR_CAN_NOT_COMPLETE;
423 status = dbwrap_trans_store_int32(db, version_keyname, version);
424 if (!NT_STATUS_IS_OK(status)) {
425 DEBUG(1, ("regdb_store_regdb_version: error storing %s = %d: %s\n",
426 version_keyname, version, nt_errstr(status)));
427 return ntstatus_to_werror(status);
428 } else {
429 DEBUG(10, ("regdb_store_regdb_version: stored %s = %d\n",
430 version_keyname, version));
431 return WERR_OK;
435 static WERROR regdb_upgrade_v1_to_v2(struct db_context *db)
437 TALLOC_CTX *mem_ctx;
438 int rc;
439 WERROR werr;
441 mem_ctx = talloc_stackframe();
443 rc = regdb->traverse(db, regdb_normalize_keynames_fn, mem_ctx);
445 talloc_free(mem_ctx);
447 if (rc < 0) {
448 return WERR_REG_IO_FAILURE;
451 werr = regdb_store_regdb_version(db, REGVER_V2);
452 return werr;
455 static int regdb_upgrade_v2_to_v3_fn(struct db_record *rec, void *private_data)
457 const char *keyname;
458 fstring subkeyname;
459 NTSTATUS status;
460 WERROR werr;
461 uint8_t *buf;
462 uint32_t buflen, len;
463 uint32_t num_items;
464 uint32_t i;
466 if (rec->key.dptr == NULL || rec->key.dsize == 0) {
467 return 0;
470 keyname = (const char *)rec->key.dptr;
472 if (strncmp(keyname, REG_SORTED_SUBKEYS_PREFIX,
473 strlen(REG_SORTED_SUBKEYS_PREFIX)) == 0)
475 /* Delete the deprecated sorted subkeys cache. */
477 DEBUG(10, ("regdb_upgrade_v2_to_v3: deleting [%s]\n", keyname));
479 status = rec->delete_rec(rec);
480 if (!NT_STATUS_IS_OK(status)) {
481 DEBUG(0, ("regdb_upgrade_v2_to_v3: tdb_delete for [%s] "
482 "failed!\n", keyname));
483 return 1;
486 return 0;
489 if (strncmp(keyname, REG_VALUE_PREFIX, strlen(REG_VALUE_PREFIX)) == 0) {
490 DEBUG(10, ("regdb_upgrade_v2_to_v3: skipping [%s]\n", keyname));
491 return 0;
494 if (strncmp(keyname, REG_SECDESC_PREFIX,
495 strlen(REG_SECDESC_PREFIX)) == 0)
497 DEBUG(10, ("regdb_upgrade_v2_to_v3: skipping [%s]\n", keyname));
498 return 0;
502 * Found a regular subkey list record.
503 * Walk the list and create the list record for those
504 * subkeys that don't already have one.
506 DEBUG(10, ("regdb_upgrade_v2_to_v3: scanning subkey list of [%s]\n",
507 keyname));
509 buf = rec->value.dptr;
510 buflen = rec->value.dsize;
512 len = tdb_unpack(buf, buflen, "d", &num_items);
513 if (len == (uint32_t)-1) {
514 /* invalid or empty - skip */
515 return 0;
518 for (i=0; i<num_items; i++) {
519 len += tdb_unpack(buf+len, buflen-len, "f", subkeyname);
520 DEBUG(10, ("regdb_upgrade_v2_to_v3: "
521 "writing subkey list for [%s\\%s]\n",
522 keyname, subkeyname));
523 werr = regdb_store_subkey_list(regdb, keyname, subkeyname);
524 if (!W_ERROR_IS_OK(werr)) {
525 return 1;
529 return 0;
532 static WERROR regdb_upgrade_v2_to_v3(struct db_context *db)
534 int rc;
535 WERROR werr;
536 TALLOC_CTX *frame = talloc_stackframe();
538 rc = regdb->traverse(db, regdb_upgrade_v2_to_v3_fn, frame);
539 if (rc < 0) {
540 werr = WERR_REG_IO_FAILURE;
541 goto done;
544 werr = regdb_store_regdb_version(db, REGVER_V3);
546 done:
547 talloc_free(frame);
548 return werr;
551 /***********************************************************************
552 Open the registry database
553 ***********************************************************************/
555 WERROR regdb_init(void)
557 const char *vstring = "INFO/version";
558 uint32 vers_id, expected_version;
559 WERROR werr;
561 if (regdb) {
562 DEBUG(10, ("regdb_init: incrementing refcount (%d->%d)\n",
563 regdb_refcount, regdb_refcount+1));
564 regdb_refcount++;
565 return WERR_OK;
568 regdb = db_open(NULL, state_path("registry.tdb"), 0,
569 REG_TDB_FLAGS, O_RDWR, 0600);
570 if (!regdb) {
571 regdb = db_open(NULL, state_path("registry.tdb"), 0,
572 REG_TDB_FLAGS, O_RDWR|O_CREAT, 0600);
573 if (!regdb) {
574 werr = ntstatus_to_werror(map_nt_error_from_unix(errno));
575 DEBUG(1,("regdb_init: Failed to open registry %s (%s)\n",
576 state_path("registry.tdb"), strerror(errno) ));
577 return werr;
580 DEBUG(10,("regdb_init: Successfully created registry tdb\n"));
583 regdb_refcount = 1;
584 DEBUG(10, ("regdb_init: registry db openend. refcount reset (%d)\n",
585 regdb_refcount));
587 expected_version = REGVER_V3;
589 vers_id = dbwrap_fetch_int32(regdb, vstring);
590 if (vers_id == -1) {
591 DEBUG(10, ("regdb_init: registry version uninitialized "
592 "(got %d), initializing to version %d\n",
593 vers_id, expected_version));
595 werr = regdb_store_regdb_version(regdb, expected_version);
596 return werr;
599 if (vers_id > expected_version || vers_id == 0) {
600 DEBUG(1, ("regdb_init: unknown registry version %d "
601 "(code version = %d), refusing initialization\n",
602 vers_id, expected_version));
603 return WERR_CAN_NOT_COMPLETE;
606 if (regdb->transaction_start(regdb) != 0) {
607 return WERR_REG_IO_FAILURE;
610 if (vers_id == REGVER_V1) {
611 DEBUG(10, ("regdb_init: upgrading registry fromversion %d "
612 "to %d\n", REGVER_V1, REGVER_V2));
614 werr = regdb_upgrade_v1_to_v2(regdb);
615 if (!W_ERROR_IS_OK(werr)) {
616 regdb->transaction_cancel(regdb);
617 return werr;
620 vers_id = REGVER_V2;
623 if (vers_id == REGVER_V2) {
624 DEBUG(10, ("regdb_init: upgrading registry from version %d "
625 "to %d\n", REGVER_V2, REGVER_V3));
627 werr = regdb_upgrade_v2_to_v3(regdb);
628 if (!W_ERROR_IS_OK(werr)) {
629 regdb->transaction_cancel(regdb);
630 return werr;
633 vers_id = REGVER_V3;
636 /* future upgrade code should go here */
638 if (regdb->transaction_commit(regdb) != 0) {
639 return WERR_REG_IO_FAILURE;
642 return WERR_OK;
645 /***********************************************************************
646 Open the registry. Must already have been initialized by regdb_init()
647 ***********************************************************************/
649 WERROR regdb_open( void )
651 WERROR result = WERR_OK;
653 if ( regdb ) {
654 DEBUG(10, ("regdb_open: incrementing refcount (%d->%d)\n",
655 regdb_refcount, regdb_refcount+1));
656 regdb_refcount++;
657 return WERR_OK;
660 become_root();
662 regdb = db_open(NULL, state_path("registry.tdb"), 0,
663 REG_TDB_FLAGS, O_RDWR, 0600);
664 if ( !regdb ) {
665 result = ntstatus_to_werror( map_nt_error_from_unix( errno ) );
666 DEBUG(0,("regdb_open: Failed to open %s! (%s)\n",
667 state_path("registry.tdb"), strerror(errno) ));
670 unbecome_root();
672 regdb_refcount = 1;
673 DEBUG(10, ("regdb_open: registry db opened. refcount reset (%d)\n",
674 regdb_refcount));
676 return result;
679 /***********************************************************************
680 ***********************************************************************/
682 int regdb_close( void )
684 if (regdb_refcount == 0) {
685 return 0;
688 regdb_refcount--;
690 DEBUG(10, ("regdb_close: decrementing refcount (%d->%d)\n",
691 regdb_refcount+1, regdb_refcount));
693 if ( regdb_refcount > 0 )
694 return 0;
696 SMB_ASSERT( regdb_refcount >= 0 );
698 TALLOC_FREE(regdb);
699 return 0;
702 WERROR regdb_transaction_start(void)
704 return (regdb->transaction_start(regdb) == 0) ?
705 WERR_OK : WERR_REG_IO_FAILURE;
708 WERROR regdb_transaction_commit(void)
710 return (regdb->transaction_commit(regdb) == 0) ?
711 WERR_OK : WERR_REG_IO_FAILURE;
714 WERROR regdb_transaction_cancel(void)
716 return (regdb->transaction_cancel(regdb) == 0) ?
717 WERR_OK : WERR_REG_IO_FAILURE;
720 /***********************************************************************
721 return the tdb sequence number of the registry tdb.
722 this is an indicator for the content of the registry
723 having changed. it will change upon regdb_init, too, though.
724 ***********************************************************************/
725 int regdb_get_seqnum(void)
727 return regdb->get_seqnum(regdb);
731 static WERROR regdb_delete_key_with_prefix(struct db_context *db,
732 const char *keyname,
733 const char *prefix)
735 char *path;
736 WERROR werr = WERR_NOMEM;
737 TALLOC_CTX *mem_ctx = talloc_stackframe();
739 if (keyname == NULL) {
740 werr = WERR_INVALID_PARAM;
741 goto done;
744 if (prefix == NULL) {
745 path = discard_const_p(char, keyname);
746 } else {
747 path = talloc_asprintf(mem_ctx, "%s\\%s", prefix, keyname);
748 if (path == NULL) {
749 goto done;
753 path = normalize_reg_path(mem_ctx, path);
754 if (path == NULL) {
755 goto done;
758 werr = ntstatus_to_werror(dbwrap_delete_bystring(db, path));
760 /* treat "not found" as ok */
761 if (W_ERROR_EQUAL(werr, WERR_NOT_FOUND)) {
762 werr = WERR_OK;
765 done:
766 talloc_free(mem_ctx);
767 return werr;
771 static WERROR regdb_delete_values(struct db_context *db, const char *keyname)
773 return regdb_delete_key_with_prefix(db, keyname, REG_VALUE_PREFIX);
776 static WERROR regdb_delete_secdesc(struct db_context *db, const char *keyname)
778 return regdb_delete_key_with_prefix(db, keyname, REG_SECDESC_PREFIX);
781 static WERROR regdb_delete_subkeylist(struct db_context *db, const char *keyname)
783 return regdb_delete_key_with_prefix(db, keyname, NULL);
787 static WERROR regdb_delete_key_lists(struct db_context *db, const char *keyname)
789 WERROR werr;
791 werr = regdb_delete_values(db, keyname);
792 if (!W_ERROR_IS_OK(werr)) {
793 DEBUG(1, (__location__ " Deleting %s\\%s failed: %s\n",
794 REG_VALUE_PREFIX, keyname, win_errstr(werr)));
795 goto done;
798 werr = regdb_delete_secdesc(db, keyname);
799 if (!W_ERROR_IS_OK(werr)) {
800 DEBUG(1, (__location__ " Deleting %s\\%s failed: %s\n",
801 REG_SECDESC_PREFIX, keyname, win_errstr(werr)));
802 goto done;
805 werr = regdb_delete_subkeylist(db, keyname);
806 if (!W_ERROR_IS_OK(werr)) {
807 DEBUG(1, (__location__ " Deleting %s failed: %s\n",
808 keyname, win_errstr(werr)));
809 goto done;
812 done:
813 return werr;
816 /***********************************************************************
817 Add subkey strings to the registry tdb under a defined key
818 fmt is the same format as tdb_pack except this function only supports
819 fstrings
820 ***********************************************************************/
822 static WERROR regdb_store_keys_internal2(struct db_context *db,
823 const char *key,
824 struct regsubkey_ctr *ctr)
826 TDB_DATA dbuf;
827 uint8 *buffer = NULL;
828 int i = 0;
829 uint32 len, buflen;
830 uint32 num_subkeys = regsubkey_ctr_numkeys(ctr);
831 char *keyname = NULL;
832 TALLOC_CTX *ctx = talloc_stackframe();
833 WERROR werr;
835 if (!key) {
836 werr = WERR_INVALID_PARAM;
837 goto done;
840 keyname = talloc_strdup(ctx, key);
841 if (!keyname) {
842 werr = WERR_NOMEM;
843 goto done;
846 keyname = normalize_reg_path(ctx, keyname);
847 if (!keyname) {
848 werr = WERR_NOMEM;
849 goto done;
852 /* allocate some initial memory */
854 buffer = (uint8 *)SMB_MALLOC(1024);
855 if (buffer == NULL) {
856 werr = WERR_NOMEM;
857 goto done;
859 buflen = 1024;
860 len = 0;
862 /* store the number of subkeys */
864 len += tdb_pack(buffer+len, buflen-len, "d", num_subkeys);
866 /* pack all the strings */
868 for (i=0; i<num_subkeys; i++) {
869 size_t thistime;
871 thistime = tdb_pack(buffer+len, buflen-len, "f",
872 regsubkey_ctr_specific_key(ctr, i));
873 if (len+thistime > buflen) {
874 size_t thistime2;
876 * tdb_pack hasn't done anything because of the short
877 * buffer, allocate extra space.
879 buffer = SMB_REALLOC_ARRAY(buffer, uint8_t,
880 (len+thistime)*2);
881 if(buffer == NULL) {
882 DEBUG(0, ("regdb_store_keys: Failed to realloc "
883 "memory of size [%u]\n",
884 (unsigned int)(len+thistime)*2));
885 werr = WERR_NOMEM;
886 goto done;
888 buflen = (len+thistime)*2;
889 thistime2 = tdb_pack(
890 buffer+len, buflen-len, "f",
891 regsubkey_ctr_specific_key(ctr, i));
892 if (thistime2 != thistime) {
893 DEBUG(0, ("tdb_pack failed\n"));
894 werr = WERR_CAN_NOT_COMPLETE;
895 goto done;
898 len += thistime;
901 /* finally write out the data */
903 dbuf.dptr = buffer;
904 dbuf.dsize = len;
905 werr = ntstatus_to_werror(dbwrap_store_bystring(db, keyname, dbuf,
906 TDB_REPLACE));
908 done:
909 TALLOC_FREE(ctx);
910 SAFE_FREE(buffer);
911 return werr;
915 * Utility function to store a new empty list of
916 * subkeys of given key specified as parent and subkey name
917 * (thereby creating the key).
918 * If the parent keyname is NULL, then the "subkey" is
919 * interpreted as a base key.
920 * If the subkey list does already exist, it is not modified.
922 * Must be called from within a transaction.
924 static WERROR regdb_store_subkey_list(struct db_context *db, const char *parent,
925 const char *key)
927 WERROR werr;
928 char *path = NULL;
929 struct regsubkey_ctr *subkeys = NULL;
930 TALLOC_CTX *frame = talloc_stackframe();
932 if (parent == NULL) {
933 path = talloc_strdup(frame, key);
934 } else {
935 path = talloc_asprintf(frame, "%s\\%s", parent, key);
937 if (!path) {
938 werr = WERR_NOMEM;
939 goto done;
942 werr = regsubkey_ctr_init(frame, &subkeys);
943 W_ERROR_NOT_OK_GOTO_DONE(werr);
945 werr = regdb_fetch_keys_internal(db, path, subkeys);
946 if (W_ERROR_IS_OK(werr)) {
947 /* subkey list exists already - don't modify */
948 goto done;
951 werr = regsubkey_ctr_reinit(subkeys);
952 W_ERROR_NOT_OK_GOTO_DONE(werr);
954 /* create a record with 0 subkeys */
955 werr = regdb_store_keys_internal2(db, path, subkeys);
956 if (!W_ERROR_IS_OK(werr)) {
957 DEBUG(0, ("regdb_store_keys: Failed to store new record for "
958 "key [%s]: %s\n", path, win_errstr(werr)));
959 goto done;
962 done:
963 talloc_free(frame);
964 return werr;
967 /***********************************************************************
968 Store the new subkey record and create any child key records that
969 do not currently exist
970 ***********************************************************************/
972 struct regdb_store_keys_context {
973 const char *key;
974 struct regsubkey_ctr *ctr;
977 static NTSTATUS regdb_store_keys_action(struct db_context *db,
978 void *private_data)
980 struct regdb_store_keys_context *store_ctx;
981 WERROR werr;
982 int num_subkeys, i;
983 char *path = NULL;
984 struct regsubkey_ctr *old_subkeys = NULL;
985 char *oldkeyname = NULL;
986 TALLOC_CTX *mem_ctx = talloc_stackframe();
988 store_ctx = (struct regdb_store_keys_context *)private_data;
991 * Re-fetch the old keys inside the transaction
994 werr = regsubkey_ctr_init(mem_ctx, &old_subkeys);
995 W_ERROR_NOT_OK_GOTO_DONE(werr);
997 werr = regdb_fetch_keys_internal(db, store_ctx->key, old_subkeys);
998 if (!W_ERROR_IS_OK(werr) &&
999 !W_ERROR_EQUAL(werr, WERR_NOT_FOUND))
1001 goto done;
1005 * Make the store operation as safe as possible without transactions:
1007 * (1) For each subkey removed from ctr compared with old_subkeys:
1009 * (a) First delete the value db entry.
1011 * (b) Next delete the secdesc db record.
1013 * (c) Then delete the subkey list entry.
1015 * (2) Now write the list of subkeys of the parent key,
1016 * deleting removed entries and adding new ones.
1018 * (3) Finally create the subkey list entries for the added keys.
1020 * This way if we crash half-way in between deleting the subkeys
1021 * and storing the parent's list of subkeys, no old data can pop up
1022 * out of the blue when re-adding keys later on.
1025 /* (1) delete removed keys' lists (values/secdesc/subkeys) */
1027 num_subkeys = regsubkey_ctr_numkeys(old_subkeys);
1028 for (i=0; i<num_subkeys; i++) {
1029 oldkeyname = regsubkey_ctr_specific_key(old_subkeys, i);
1031 if (regsubkey_ctr_key_exists(store_ctx->ctr, oldkeyname)) {
1033 * It's still around, don't delete
1035 continue;
1038 path = talloc_asprintf(mem_ctx, "%s\\%s", store_ctx->key,
1039 oldkeyname);
1040 if (!path) {
1041 werr = WERR_NOMEM;
1042 goto done;
1045 werr = regdb_delete_key_lists(db, path);
1046 W_ERROR_NOT_OK_GOTO_DONE(werr);
1048 TALLOC_FREE(path);
1051 TALLOC_FREE(old_subkeys);
1053 /* (2) store the subkey list for the parent */
1055 werr = regdb_store_keys_internal2(db, store_ctx->key, store_ctx->ctr);
1056 if (!W_ERROR_IS_OK(werr)) {
1057 DEBUG(0,("regdb_store_keys: Failed to store new subkey list "
1058 "for parent [%s]: %s\n", store_ctx->key,
1059 win_errstr(werr)));
1060 goto done;
1063 /* (3) now create records for any subkeys that don't already exist */
1065 num_subkeys = regsubkey_ctr_numkeys(store_ctx->ctr);
1067 for (i=0; i<num_subkeys; i++) {
1068 const char *subkey;
1070 subkey = regsubkey_ctr_specific_key(store_ctx->ctr, i);
1072 werr = regdb_store_subkey_list(db, store_ctx->key, subkey);
1073 W_ERROR_NOT_OK_GOTO_DONE(werr);
1076 werr = WERR_OK;
1078 done:
1079 talloc_free(mem_ctx);
1080 return werror_to_ntstatus(werr);
1083 static bool regdb_store_keys_internal(struct db_context *db, const char *key,
1084 struct regsubkey_ctr *ctr)
1086 int num_subkeys, old_num_subkeys, i;
1087 struct regsubkey_ctr *old_subkeys = NULL;
1088 TALLOC_CTX *ctx = talloc_stackframe();
1089 WERROR werr;
1090 bool ret = false;
1091 struct regdb_store_keys_context store_ctx;
1093 if (!regdb_key_exists(db, key)) {
1094 goto done;
1098 * fetch a list of the old subkeys so we can determine if anything has
1099 * changed
1102 werr = regsubkey_ctr_init(ctx, &old_subkeys);
1103 if (!W_ERROR_IS_OK(werr)) {
1104 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
1105 goto done;
1108 werr = regdb_fetch_keys_internal(db, key, old_subkeys);
1109 if (!W_ERROR_IS_OK(werr) &&
1110 !W_ERROR_EQUAL(werr, WERR_NOT_FOUND))
1112 goto done;
1115 num_subkeys = regsubkey_ctr_numkeys(ctr);
1116 old_num_subkeys = regsubkey_ctr_numkeys(old_subkeys);
1117 if ((num_subkeys && old_num_subkeys) &&
1118 (num_subkeys == old_num_subkeys)) {
1120 for (i = 0; i < num_subkeys; i++) {
1121 if (strcmp(regsubkey_ctr_specific_key(ctr, i),
1122 regsubkey_ctr_specific_key(old_subkeys, i))
1123 != 0)
1125 break;
1128 if (i == num_subkeys) {
1130 * Nothing changed, no point to even start a tdb
1131 * transaction
1134 ret = true;
1135 goto done;
1139 TALLOC_FREE(old_subkeys);
1141 store_ctx.key = key;
1142 store_ctx.ctr = ctr;
1144 werr = ntstatus_to_werror(dbwrap_trans_do(db,
1145 regdb_store_keys_action,
1146 &store_ctx));
1148 ret = W_ERROR_IS_OK(werr);
1150 done:
1151 TALLOC_FREE(ctx);
1153 return ret;
1156 bool regdb_store_keys(const char *key, struct regsubkey_ctr *ctr)
1158 return regdb_store_keys_internal(regdb, key, ctr);
1162 * create a subkey of a given key
1165 struct regdb_create_subkey_context {
1166 const char *key;
1167 const char *subkey;
1170 static NTSTATUS regdb_create_subkey_action(struct db_context *db,
1171 void *private_data)
1173 WERROR werr;
1174 struct regdb_create_subkey_context *create_ctx;
1175 struct regsubkey_ctr *subkeys;
1176 TALLOC_CTX *mem_ctx = talloc_stackframe();
1178 create_ctx = (struct regdb_create_subkey_context *)private_data;
1180 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
1181 W_ERROR_NOT_OK_GOTO_DONE(werr);
1183 werr = regdb_fetch_keys_internal(db, create_ctx->key, subkeys);
1184 W_ERROR_NOT_OK_GOTO_DONE(werr);
1186 werr = regsubkey_ctr_addkey(subkeys, create_ctx->subkey);
1187 W_ERROR_NOT_OK_GOTO_DONE(werr);
1189 werr = regdb_store_keys_internal2(db, create_ctx->key, subkeys);
1190 if (!W_ERROR_IS_OK(werr)) {
1191 DEBUG(0, (__location__ " failed to store new subkey list for "
1192 "parent key %s: %s\n", create_ctx->key,
1193 win_errstr(werr)));
1196 werr = regdb_store_subkey_list(db, create_ctx->key, create_ctx->subkey);
1198 done:
1199 talloc_free(mem_ctx);
1200 return werror_to_ntstatus(werr);
1203 static WERROR regdb_create_subkey_internal(struct db_context *db,
1204 const char *key,
1205 const char *subkey)
1207 WERROR werr;
1208 struct regsubkey_ctr *subkeys;
1209 TALLOC_CTX *mem_ctx = talloc_stackframe();
1210 struct regdb_create_subkey_context create_ctx;
1212 if (!regdb_key_exists(db, key)) {
1213 werr = WERR_NOT_FOUND;
1214 goto done;
1217 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
1218 W_ERROR_NOT_OK_GOTO_DONE(werr);
1220 werr = regdb_fetch_keys_internal(db, key, subkeys);
1221 W_ERROR_NOT_OK_GOTO_DONE(werr);
1223 if (regsubkey_ctr_key_exists(subkeys, subkey)) {
1224 werr = WERR_OK;
1225 goto done;
1228 talloc_free(subkeys);
1230 create_ctx.key = key;
1231 create_ctx.subkey = subkey;
1233 werr = ntstatus_to_werror(dbwrap_trans_do(db,
1234 regdb_create_subkey_action,
1235 &create_ctx));
1237 done:
1238 talloc_free(mem_ctx);
1239 return werr;
1242 static WERROR regdb_create_subkey(const char *key, const char *subkey)
1244 return regdb_create_subkey_internal(regdb, key, subkey);
1248 * create a base key
1251 struct regdb_create_basekey_context {
1252 const char *key;
1255 static NTSTATUS regdb_create_basekey_action(struct db_context *db,
1256 void *private_data)
1258 WERROR werr;
1259 struct regdb_create_basekey_context *create_ctx;
1261 create_ctx = (struct regdb_create_basekey_context *)private_data;
1263 werr = regdb_store_subkey_list(db, NULL, create_ctx->key);
1265 return werror_to_ntstatus(werr);
1268 static WERROR regdb_create_basekey(struct db_context *db, const char *key)
1270 WERROR werr;
1271 struct regdb_create_subkey_context create_ctx;
1273 create_ctx.key = key;
1275 werr = ntstatus_to_werror(dbwrap_trans_do(db,
1276 regdb_create_basekey_action,
1277 &create_ctx));
1279 return werr;
1283 * create a subkey of a given key
1286 struct regdb_delete_subkey_context {
1287 const char *key;
1288 const char *subkey;
1289 const char *path;
1290 bool lazy;
1293 static NTSTATUS regdb_delete_subkey_action(struct db_context *db,
1294 void *private_data)
1296 WERROR werr;
1297 struct regdb_delete_subkey_context *delete_ctx;
1298 struct regsubkey_ctr *subkeys;
1299 TALLOC_CTX *mem_ctx = talloc_stackframe();
1301 delete_ctx = (struct regdb_delete_subkey_context *)private_data;
1303 werr = regdb_delete_key_lists(db, delete_ctx->path);
1304 W_ERROR_NOT_OK_GOTO_DONE(werr);
1306 if (delete_ctx->lazy) {
1307 goto done;
1310 werr = regsubkey_ctr_init(mem_ctx, &subkeys);
1311 W_ERROR_NOT_OK_GOTO_DONE(werr);
1313 werr = regdb_fetch_keys_internal(db, delete_ctx->key, subkeys);
1314 W_ERROR_NOT_OK_GOTO_DONE(werr);
1316 werr = regsubkey_ctr_delkey(subkeys, delete_ctx->subkey);
1317 W_ERROR_NOT_OK_GOTO_DONE(werr);
1319 werr = regdb_store_keys_internal2(db, delete_ctx->key, subkeys);
1320 if (!W_ERROR_IS_OK(werr)) {
1321 DEBUG(0, (__location__ " failed to store new subkey_list for "
1322 "parent key %s: %s\n", delete_ctx->key,
1323 win_errstr(werr)));
1326 done:
1327 talloc_free(mem_ctx);
1328 return werror_to_ntstatus(werr);
1331 static WERROR regdb_delete_subkey(const char *key, const char *subkey, bool lazy)
1333 WERROR werr;
1334 char *path;
1335 struct regdb_delete_subkey_context delete_ctx;
1336 TALLOC_CTX *mem_ctx = talloc_stackframe();
1338 if (!regdb_key_exists(regdb, key)) {
1339 werr = WERR_NOT_FOUND;
1340 goto done;
1343 path = talloc_asprintf(mem_ctx, "%s\\%s", key, subkey);
1344 if (path == NULL) {
1345 werr = WERR_NOMEM;
1346 goto done;
1349 if (!regdb_key_exists(regdb, path)) {
1350 werr = WERR_OK;
1351 goto done;
1354 delete_ctx.key = key;
1355 delete_ctx.subkey = subkey;
1356 delete_ctx.path = path;
1357 delete_ctx.lazy = lazy;
1359 werr = ntstatus_to_werror(dbwrap_trans_do(regdb,
1360 regdb_delete_subkey_action,
1361 &delete_ctx));
1363 done:
1364 talloc_free(mem_ctx);
1365 return werr;
1368 static TDB_DATA regdb_fetch_key_internal(struct db_context *db,
1369 TALLOC_CTX *mem_ctx, const char *key)
1371 char *path = NULL;
1372 TDB_DATA data;
1374 path = normalize_reg_path(mem_ctx, key);
1375 if (!path) {
1376 return make_tdb_data(NULL, 0);
1379 data = dbwrap_fetch_bystring(db, mem_ctx, path);
1381 TALLOC_FREE(path);
1382 return data;
1387 * Check for the existence of a key.
1389 * Existence of a key is authoritatively defined by
1390 * the existence of the record that contains the list
1391 * of its subkeys.
1393 * Return false, if the record does not match the correct
1394 * structure of an initial 4-byte counter and then a
1395 * list of the corresponding number of zero-terminated
1396 * strings.
1398 static bool regdb_key_exists(struct db_context *db, const char *key)
1400 TALLOC_CTX *mem_ctx = talloc_stackframe();
1401 TDB_DATA value;
1402 bool ret = false;
1403 char *path;
1404 uint32_t buflen;
1405 const char *buf;
1406 uint32_t num_items, i;
1407 int32_t len;
1409 if (key == NULL) {
1410 goto done;
1413 path = normalize_reg_path(mem_ctx, key);
1414 if (path == NULL) {
1415 DEBUG(0, ("out of memory! (talloc failed)\n"));
1416 goto done;
1419 if (*path == '\0') {
1420 goto done;
1423 value = regdb_fetch_key_internal(db, mem_ctx, path);
1424 if (value.dptr == NULL) {
1425 goto done;
1428 if (value.dsize == 0) {
1429 DEBUG(10, ("regdb_key_exists: subkeylist-record for key "
1430 "[%s] is empty: Could be a deleted record in a "
1431 "clustered (ctdb) environment?\n",
1432 path));
1433 goto done;
1436 len = tdb_unpack(value.dptr, value.dsize, "d", &num_items);
1437 if (len == (int32_t)-1) {
1438 DEBUG(1, ("regdb_key_exists: ERROR: subkeylist-record for key "
1439 "[%s] is invalid: Could not parse initial 4-byte "
1440 "counter. record data length is %u.\n",
1441 path, (unsigned int)value.dsize));
1442 goto done;
1446 * Note: the tdb_unpack check above implies that len <= value.dsize
1448 buflen = value.dsize - len;
1449 buf = (const char *)value.dptr + len;
1451 len = 0;
1453 for (i = 0; i < num_items; i++) {
1454 if (buflen == 0) {
1455 break;
1457 len = strnlen(buf, buflen) + 1;
1458 if (buflen < len) {
1459 DEBUG(1, ("regdb_key_exists: ERROR: subkeylist-record "
1460 "for key [%s] is corrupt: %u items expected, "
1461 "item number %u is not zero terminated.\n",
1462 path, num_items, i+1));
1463 goto done;
1466 buf += len;
1467 buflen -= len;
1470 if (buflen > 0) {
1471 DEBUG(1, ("regdb_key_exists: ERROR: subkeylist-record for key "
1472 "[%s] is corrupt: %u items expected and found, but "
1473 "the record contains additional %u bytes\n",
1474 path, num_items, buflen));
1475 goto done;
1478 if (i < num_items) {
1479 DEBUG(1, ("regdb_key_exists: ERROR: subkeylist-record for key "
1480 "[%s] is corrupt: %u items expected, but only %u "
1481 "items found.\n",
1482 path, num_items, i+1));
1483 goto done;
1486 ret = true;
1488 done:
1489 TALLOC_FREE(mem_ctx);
1490 return ret;
1494 /***********************************************************************
1495 Retrieve an array of strings containing subkeys. Memory should be
1496 released by the caller.
1497 ***********************************************************************/
1499 static WERROR regdb_fetch_keys_internal(struct db_context *db, const char *key,
1500 struct regsubkey_ctr *ctr)
1502 WERROR werr;
1503 uint32_t num_items;
1504 uint8 *buf;
1505 uint32 buflen, len;
1506 int i;
1507 fstring subkeyname;
1508 TALLOC_CTX *frame = talloc_stackframe();
1509 TDB_DATA value;
1511 DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key ? key : "NULL"));
1513 if (!regdb_key_exists(db, key)) {
1514 DEBUG(10, ("key [%s] not found\n", key));
1515 werr = WERR_NOT_FOUND;
1516 goto done;
1519 werr = regsubkey_ctr_reinit(ctr);
1520 W_ERROR_NOT_OK_GOTO_DONE(werr);
1522 werr = regsubkey_ctr_set_seqnum(ctr, db->get_seqnum(db));
1523 W_ERROR_NOT_OK_GOTO_DONE(werr);
1525 value = regdb_fetch_key_internal(db, frame, key);
1527 if (value.dsize == 0 || value.dptr == NULL) {
1528 DEBUG(10, ("regdb_fetch_keys: no subkeys found for key [%s]\n",
1529 key));
1530 goto done;
1533 buf = value.dptr;
1534 buflen = value.dsize;
1535 len = tdb_unpack( buf, buflen, "d", &num_items);
1536 if (len == (uint32_t)-1) {
1537 werr = WERR_NOT_FOUND;
1538 goto done;
1541 for (i=0; i<num_items; i++) {
1542 len += tdb_unpack(buf+len, buflen-len, "f", subkeyname);
1543 werr = regsubkey_ctr_addkey(ctr, subkeyname);
1544 if (!W_ERROR_IS_OK(werr)) {
1545 DEBUG(5, ("regdb_fetch_keys: regsubkey_ctr_addkey "
1546 "failed: %s\n", win_errstr(werr)));
1547 num_items = 0;
1548 goto done;
1552 DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items));
1554 done:
1555 TALLOC_FREE(frame);
1556 return werr;
1559 int regdb_fetch_keys(const char *key, struct regsubkey_ctr *ctr)
1561 WERROR werr;
1563 werr = regdb_fetch_keys_internal(regdb, key, ctr);
1564 if (!W_ERROR_IS_OK(werr)) {
1565 return -1;
1568 return regsubkey_ctr_numkeys(ctr);
1571 /****************************************************************************
1572 Unpack a list of registry values frem the TDB
1573 ***************************************************************************/
1575 static int regdb_unpack_values(struct regval_ctr *values, uint8 *buf, int buflen)
1577 int len = 0;
1578 uint32 type;
1579 fstring valuename;
1580 uint32 size;
1581 uint8 *data_p;
1582 uint32 num_values = 0;
1583 int i;
1585 /* loop and unpack the rest of the registry values */
1587 len += tdb_unpack(buf+len, buflen-len, "d", &num_values);
1589 for ( i=0; i<num_values; i++ ) {
1590 /* unpack the next regval */
1592 type = REG_NONE;
1593 size = 0;
1594 data_p = NULL;
1595 valuename[0] = '\0';
1596 len += tdb_unpack(buf+len, buflen-len, "fdB",
1597 valuename,
1598 &type,
1599 &size,
1600 &data_p);
1602 regval_ctr_addvalue(values, valuename, type,
1603 (uint8_t *)data_p, size);
1604 SAFE_FREE(data_p); /* 'B' option to tdb_unpack does a malloc() */
1606 DEBUG(8,("specific: [%s], len: %d\n", valuename, size));
1609 return len;
1612 /****************************************************************************
1613 Pack all values in all printer keys
1614 ***************************************************************************/
1616 static int regdb_pack_values(struct regval_ctr *values, uint8 *buf, int buflen)
1618 int len = 0;
1619 int i;
1620 struct regval_blob *val;
1621 int num_values;
1623 if ( !values )
1624 return 0;
1626 num_values = regval_ctr_numvals( values );
1628 /* pack the number of values first */
1630 len += tdb_pack( buf+len, buflen-len, "d", num_values );
1632 /* loop over all values */
1634 for ( i=0; i<num_values; i++ ) {
1635 val = regval_ctr_specific_value( values, i );
1636 len += tdb_pack(buf+len, buflen-len, "fdB",
1637 regval_name(val),
1638 regval_type(val),
1639 regval_size(val),
1640 regval_data_p(val) );
1643 return len;
1646 /***********************************************************************
1647 Retrieve an array of strings containing subkeys. Memory should be
1648 released by the caller.
1649 ***********************************************************************/
1651 static int regdb_fetch_values_internal(struct db_context *db, const char* key,
1652 struct regval_ctr *values)
1654 char *keystr = NULL;
1655 TALLOC_CTX *ctx = talloc_stackframe();
1656 int ret = 0;
1657 TDB_DATA value;
1658 WERROR werr;
1660 DEBUG(10,("regdb_fetch_values: Looking for value of key [%s] \n", key));
1662 if (!regdb_key_exists(db, key)) {
1663 goto done;
1666 keystr = talloc_asprintf(ctx, "%s\\%s", REG_VALUE_PREFIX, key);
1667 if (!keystr) {
1668 goto done;
1671 werr = regval_ctr_set_seqnum(values, db->get_seqnum(db));
1672 W_ERROR_NOT_OK_GOTO_DONE(werr);
1674 value = regdb_fetch_key_internal(db, ctx, keystr);
1676 if (!value.dptr) {
1677 /* all keys have zero values by default */
1678 goto done;
1681 regdb_unpack_values(values, value.dptr, value.dsize);
1682 ret = regval_ctr_numvals(values);
1684 done:
1685 TALLOC_FREE(ctx);
1686 return ret;
1689 int regdb_fetch_values(const char* key, struct regval_ctr *values)
1691 return regdb_fetch_values_internal(regdb, key, values);
1694 static bool regdb_store_values_internal(struct db_context *db, const char *key,
1695 struct regval_ctr *values)
1697 TDB_DATA old_data, data;
1698 char *keystr = NULL;
1699 TALLOC_CTX *ctx = talloc_stackframe();
1700 int len;
1701 NTSTATUS status;
1702 bool result = false;
1704 DEBUG(10,("regdb_store_values: Looking for value of key [%s] \n", key));
1706 if (!regdb_key_exists(db, key)) {
1707 goto done;
1710 ZERO_STRUCT(data);
1712 len = regdb_pack_values(values, data.dptr, data.dsize);
1713 if (len <= 0) {
1714 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
1715 goto done;
1718 data.dptr = talloc_array(ctx, uint8, len);
1719 data.dsize = len;
1721 len = regdb_pack_values(values, data.dptr, data.dsize);
1723 SMB_ASSERT( len == data.dsize );
1725 keystr = talloc_asprintf(ctx, "%s\\%s", REG_VALUE_PREFIX, key );
1726 if (!keystr) {
1727 goto done;
1729 keystr = normalize_reg_path(ctx, keystr);
1730 if (!keystr) {
1731 goto done;
1734 old_data = dbwrap_fetch_bystring(db, ctx, keystr);
1736 if ((old_data.dptr != NULL)
1737 && (old_data.dsize == data.dsize)
1738 && (memcmp(old_data.dptr, data.dptr, data.dsize) == 0))
1740 result = true;
1741 goto done;
1744 status = dbwrap_trans_store_bystring(db, keystr, data, TDB_REPLACE);
1746 result = NT_STATUS_IS_OK(status);
1748 done:
1749 TALLOC_FREE(ctx);
1750 return result;
1753 bool regdb_store_values(const char *key, struct regval_ctr *values)
1755 return regdb_store_values_internal(regdb, key, values);
1758 static WERROR regdb_get_secdesc(TALLOC_CTX *mem_ctx, const char *key,
1759 struct security_descriptor **psecdesc)
1761 char *tdbkey;
1762 TDB_DATA data;
1763 NTSTATUS status;
1764 TALLOC_CTX *tmp_ctx = talloc_stackframe();
1765 WERROR err = WERR_OK;
1767 DEBUG(10, ("regdb_get_secdesc: Getting secdesc of key [%s]\n", key));
1769 if (!regdb_key_exists(regdb, key)) {
1770 err = WERR_BADFILE;
1771 goto done;
1774 tdbkey = talloc_asprintf(tmp_ctx, "%s\\%s", REG_SECDESC_PREFIX, key);
1775 if (tdbkey == NULL) {
1776 err = WERR_NOMEM;
1777 goto done;
1780 tdbkey = normalize_reg_path(tmp_ctx, tdbkey);
1781 if (tdbkey == NULL) {
1782 err = WERR_NOMEM;
1783 goto done;
1786 data = dbwrap_fetch_bystring(regdb, tmp_ctx, tdbkey);
1787 if (data.dptr == NULL) {
1788 err = WERR_BADFILE;
1789 goto done;
1792 status = unmarshall_sec_desc(mem_ctx, (uint8 *)data.dptr, data.dsize,
1793 psecdesc);
1795 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MEMORY)) {
1796 err = WERR_NOMEM;
1797 } else if (!NT_STATUS_IS_OK(status)) {
1798 err = WERR_REG_CORRUPT;
1801 done:
1802 TALLOC_FREE(tmp_ctx);
1803 return err;
1806 static WERROR regdb_set_secdesc(const char *key,
1807 struct security_descriptor *secdesc)
1809 TALLOC_CTX *mem_ctx = talloc_stackframe();
1810 char *tdbkey;
1811 WERROR err = WERR_NOMEM;
1812 TDB_DATA tdbdata;
1814 if (!regdb_key_exists(regdb, key)) {
1815 err = WERR_BADFILE;
1816 goto done;
1819 tdbkey = talloc_asprintf(mem_ctx, "%s\\%s", REG_SECDESC_PREFIX, key);
1820 if (tdbkey == NULL) {
1821 goto done;
1824 tdbkey = normalize_reg_path(mem_ctx, tdbkey);
1825 if (tdbkey == NULL) {
1826 err = WERR_NOMEM;
1827 goto done;
1830 if (secdesc == NULL) {
1831 /* assuming a delete */
1832 err = ntstatus_to_werror(dbwrap_trans_delete_bystring(regdb,
1833 tdbkey));
1834 goto done;
1837 err = ntstatus_to_werror(marshall_sec_desc(mem_ctx, secdesc,
1838 &tdbdata.dptr,
1839 &tdbdata.dsize));
1840 W_ERROR_NOT_OK_GOTO_DONE(err);
1842 err = ntstatus_to_werror(dbwrap_trans_store_bystring(regdb, tdbkey,
1843 tdbdata, 0));
1845 done:
1846 TALLOC_FREE(mem_ctx);
1847 return err;
1850 bool regdb_subkeys_need_update(struct regsubkey_ctr *subkeys)
1852 return (regdb_get_seqnum() != regsubkey_ctr_get_seqnum(subkeys));
1855 bool regdb_values_need_update(struct regval_ctr *values)
1857 return (regdb_get_seqnum() != regval_ctr_get_seqnum(values));
1861 * Table of function pointers for default access
1864 struct registry_ops regdb_ops = {
1865 .fetch_subkeys = regdb_fetch_keys,
1866 .fetch_values = regdb_fetch_values,
1867 .store_subkeys = regdb_store_keys,
1868 .store_values = regdb_store_values,
1869 .create_subkey = regdb_create_subkey,
1870 .delete_subkey = regdb_delete_subkey,
1871 .get_secdesc = regdb_get_secdesc,
1872 .set_secdesc = regdb_set_secdesc,
1873 .subkeys_need_update = regdb_subkeys_need_update,
1874 .values_need_update = regdb_values_need_update