Fix a O(n^2) algorithm in regdb_fetch_keys()
[Samba.git] / source / registry / reg_backend_db.c
blobfe5f19271395c5158da2e71e1be68551cf5dad56
1 /*
2 * Unix SMB/CIFS implementation.
3 * Virtual Windows Registry Layer
4 * Copyright (C) Gerald Carter 2002-2005
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 /* Implementation of internal registry database functions. */
22 #include "includes.h"
24 #undef DBGC_CLASS
25 #define DBGC_CLASS DBGC_REGISTRY
27 static struct db_context *regdb = NULL;
28 static int regdb_refcount;
30 static bool regdb_key_exists(const char *key);
31 static bool regdb_key_is_base_key(const char *key);
33 /* List the deepest path into the registry. All part components will be created.*/
35 /* If you want to have a part of the path controlled by the tdb and part by
36 a virtual registry db (e.g. printing), then you have to list the deepest path.
37 For example,"HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion/Print"
38 allows the reg_db backend to handle everything up to
39 "HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion" and then we'll hook
40 the reg_printing backend onto the last component of the path (see
41 KEY_PRINTING_2K in include/rpc_reg.h) --jerry */
43 static const char *builtin_registry_paths[] = {
44 KEY_PRINTING_2K,
45 KEY_PRINTING_PORTS,
46 KEY_PRINTING,
47 KEY_SHARES,
48 KEY_EVENTLOG,
49 KEY_SMBCONF,
50 KEY_PERFLIB,
51 KEY_PERFLIB_009,
52 KEY_GROUP_POLICY,
53 KEY_SAMBA_GROUP_POLICY,
54 KEY_GP_MACHINE_POLICY,
55 KEY_GP_MACHINE_WIN_POLICY,
56 KEY_HKCU,
57 KEY_GP_USER_POLICY,
58 KEY_GP_USER_WIN_POLICY,
59 KEY_WINLOGON_GPEXT_PATH,
60 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Print\\Monitors",
61 KEY_PROD_OPTIONS,
62 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\DefaultUserConfiguration",
63 KEY_TCPIP_PARAMS,
64 KEY_NETLOGON_PARAMS,
65 KEY_HKU,
66 KEY_HKCR,
67 KEY_HKPD,
68 KEY_HKPT,
69 NULL };
71 struct builtin_regkey_value {
72 const char *path;
73 const char *valuename;
74 uint32 type;
75 union {
76 const char *string;
77 uint32 dw_value;
78 } data;
81 static struct builtin_regkey_value builtin_registry_values[] = {
82 { KEY_PRINTING_PORTS,
83 SAMBA_PRINTER_PORT_NAME, REG_SZ, { "" } },
84 { KEY_PRINTING_2K,
85 "DefaultSpoolDirectory", REG_SZ, { "C:\\Windows\\System32\\Spool\\Printers" } },
86 { KEY_EVENTLOG,
87 "DisplayName", REG_SZ, { "Event Log" } },
88 { KEY_EVENTLOG,
89 "ErrorControl", REG_DWORD, { (char*)0x00000001 } },
90 { NULL, NULL, 0, { NULL } }
93 /**
94 * Initialize a key in the registry:
95 * create each component key of the specified path.
97 static WERROR init_registry_key_internal(const char *add_path)
99 WERROR werr;
100 TALLOC_CTX *frame = talloc_stackframe();
101 char *path = NULL;
102 char *base = NULL;
103 char *remaining = NULL;
104 char *keyname;
105 char *subkeyname;
106 REGSUBKEY_CTR *subkeys;
107 const char *p, *p2;
109 DEBUG(6, ("init_registry_key: Adding [%s]\n", add_path));
111 path = talloc_strdup(frame, add_path);
112 base = talloc_strdup(frame, "");
113 if (!path || !base) {
114 werr = WERR_NOMEM;
115 goto fail;
117 p = path;
119 while (next_token_talloc(frame, &p, &keyname, "\\")) {
121 /* build up the registry path from the components */
123 if (*base) {
124 base = talloc_asprintf(frame, "%s\\", base);
125 if (!base) {
126 werr = WERR_NOMEM;
127 goto fail;
130 base = talloc_asprintf_append(base, "%s", keyname);
131 if (!base) {
132 werr = WERR_NOMEM;
133 goto fail;
136 /* get the immediate subkeyname (if we have one ) */
138 subkeyname = talloc_strdup(frame, "");
139 if (!subkeyname) {
140 werr = WERR_NOMEM;
141 goto fail;
143 if (*p) {
144 remaining = talloc_strdup(frame, p);
145 if (!remaining) {
146 werr = WERR_NOMEM;
147 goto fail;
149 p2 = remaining;
151 if (!next_token_talloc(frame, &p2,
152 &subkeyname, "\\"))
154 subkeyname = talloc_strdup(frame,p2);
155 if (!subkeyname) {
156 werr = WERR_NOMEM;
157 goto fail;
162 DEBUG(10,("init_registry_key: Storing key [%s] with "
163 "subkey [%s]\n", base,
164 *subkeyname ? subkeyname : "NULL"));
166 /* we don't really care if the lookup succeeds or not
167 * since we are about to update the record.
168 * We just want any subkeys already present */
170 if (!(subkeys = TALLOC_ZERO_P(frame, REGSUBKEY_CTR))) {
171 DEBUG(0,("talloc() failure!\n"));
172 werr = WERR_NOMEM;
173 goto fail;
176 regdb_fetch_keys(base, subkeys);
177 if (*subkeyname) {
178 werr = regsubkey_ctr_addkey(subkeys, subkeyname);
179 if (!W_ERROR_IS_OK(werr)) {
180 goto fail;
183 if (!regdb_store_keys( base, subkeys)) {
184 werr = WERR_CAN_NOT_COMPLETE;
185 goto fail;
189 werr = WERR_OK;
191 fail:
192 TALLOC_FREE(frame);
193 return werr;
197 * Initialize a key in the registry:
198 * create each component key of the specified path,
199 * wrapped in one db transaction.
201 WERROR init_registry_key(const char *add_path)
203 WERROR werr;
205 if (regdb_key_exists(add_path)) {
206 return WERR_OK;
209 if (regdb->transaction_start(regdb) != 0) {
210 DEBUG(0, ("init_registry_key: transaction_start failed\n"));
211 return WERR_REG_IO_FAILURE;
214 werr = init_registry_key_internal(add_path);
215 if (!W_ERROR_IS_OK(werr)) {
216 goto fail;
219 if (regdb->transaction_commit(regdb) != 0) {
220 DEBUG(0, ("init_registry_key: Could not commit transaction\n"));
221 return WERR_REG_IO_FAILURE;
224 return WERR_OK;
226 fail:
227 if (regdb->transaction_cancel(regdb) != 0) {
228 smb_panic("init_registry_key: transaction_cancel failed\n");
231 return werr;
234 /***********************************************************************
235 Open the registry data in the tdb
236 ***********************************************************************/
238 WERROR init_registry_data(void)
240 WERROR werr;
241 TALLOC_CTX *frame = talloc_stackframe();
242 REGVAL_CTR *values;
243 int i;
244 UNISTR2 data;
247 * First, check for the existence of the needed keys and values.
248 * If all do already exist, we can save the writes.
250 for (i=0; builtin_registry_paths[i] != NULL; i++) {
251 if (!regdb_key_exists(builtin_registry_paths[i])) {
252 goto do_init;
256 for (i=0; builtin_registry_values[i].path != NULL; i++) {
257 values = TALLOC_ZERO_P(frame, REGVAL_CTR);
258 if (values == NULL) {
259 werr = WERR_NOMEM;
260 goto done;
263 regdb_fetch_values(builtin_registry_values[i].path, values);
264 if (!regval_ctr_key_exists(values,
265 builtin_registry_values[i].valuename))
267 TALLOC_FREE(values);
268 goto do_init;
271 TALLOC_FREE(values);
274 werr = WERR_OK;
275 goto done;
277 do_init:
280 * There are potentially quite a few store operations which are all
281 * indiviually wrapped in tdb transactions. Wrapping them in a single
282 * transaction gives just a single transaction_commit() to actually do
283 * its fsync()s. See tdb/common/transaction.c for info about nested
284 * transaction behaviour.
287 if (regdb->transaction_start(regdb) != 0) {
288 DEBUG(0, ("init_registry_data: tdb_transaction_start "
289 "failed\n"));
290 werr = WERR_REG_IO_FAILURE;
291 goto done;
294 /* loop over all of the predefined paths and add each component */
296 for (i=0; builtin_registry_paths[i] != NULL; i++) {
297 if (regdb_key_exists(builtin_registry_paths[i])) {
298 continue;
300 werr = init_registry_key_internal(builtin_registry_paths[i]);
301 if (!W_ERROR_IS_OK(werr)) {
302 goto fail;
306 /* loop over all of the predefined values and add each component */
308 for (i=0; builtin_registry_values[i].path != NULL; i++) {
310 values = TALLOC_ZERO_P(frame, REGVAL_CTR);
311 if (values == NULL) {
312 werr = WERR_NOMEM;
313 goto fail;
316 regdb_fetch_values(builtin_registry_values[i].path, 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 switch(builtin_registry_values[i].type) {
324 case REG_DWORD:
325 regval_ctr_addvalue(values,
326 builtin_registry_values[i].valuename,
327 REG_DWORD,
328 (char*)&builtin_registry_values[i].data.dw_value,
329 sizeof(uint32));
330 break;
332 case REG_SZ:
333 init_unistr2(&data,
334 builtin_registry_values[i].data.string,
335 UNI_STR_TERMINATE);
336 regval_ctr_addvalue(values,
337 builtin_registry_values[i].valuename,
338 REG_SZ,
339 (char*)data.buffer,
340 data.uni_str_len*sizeof(uint16));
341 break;
343 default:
344 DEBUG(0, ("init_registry_data: invalid value "
345 "type in builtin_registry_values "
346 "[%d]\n",
347 builtin_registry_values[i].type));
349 regdb_store_values(builtin_registry_values[i].path,
350 values);
352 TALLOC_FREE(values);
355 if (regdb->transaction_commit(regdb) != 0) {
356 DEBUG(0, ("init_registry_data: Could not commit "
357 "transaction\n"));
358 werr = WERR_REG_IO_FAILURE;
359 } else {
360 werr = WERR_OK;
363 goto done;
365 fail:
366 if (regdb->transaction_cancel(regdb) != 0) {
367 smb_panic("init_registry_data: tdb_transaction_cancel "
368 "failed\n");
371 done:
372 TALLOC_FREE(frame);
373 return werr;
376 /***********************************************************************
377 Open the registry database
378 ***********************************************************************/
380 WERROR regdb_init(void)
382 const char *vstring = "INFO/version";
383 uint32 vers_id;
384 WERROR werr;
386 if (regdb) {
387 DEBUG(10, ("regdb_init: incrementing refcount (%d)\n",
388 regdb_refcount));
389 regdb_refcount++;
390 return WERR_OK;
393 regdb = db_open(NULL, state_path("registry.tdb"), 0,
394 REG_TDB_FLAGS, O_RDWR, 0600);
395 if (!regdb) {
396 regdb = db_open(NULL, state_path("registry.tdb"), 0,
397 REG_TDB_FLAGS, O_RDWR|O_CREAT, 0600);
398 if (!regdb) {
399 werr = ntstatus_to_werror(map_nt_error_from_unix(errno));
400 DEBUG(1,("regdb_init: Failed to open registry %s (%s)\n",
401 state_path("registry.tdb"), strerror(errno) ));
402 return werr;
405 DEBUG(10,("regdb_init: Successfully created registry tdb\n"));
408 regdb_refcount = 1;
410 vers_id = dbwrap_fetch_int32(regdb, vstring);
412 if ( vers_id != REGVER_V1 ) {
413 NTSTATUS status;
414 /* any upgrade code here if needed */
415 DEBUG(10, ("regdb_init: got %s = %d != %d\n", vstring,
416 vers_id, REGVER_V1));
417 status = dbwrap_trans_store_int32(regdb, vstring, REGVER_V1);
418 if (!NT_STATUS_IS_OK(status)) {
419 DEBUG(1, ("regdb_init: error storing %s = %d: %s\n",
420 vstring, REGVER_V1, nt_errstr(status)));
421 return ntstatus_to_werror(status);
422 } else {
423 DEBUG(10, ("regdb_init: stored %s = %d\n",
424 vstring, REGVER_V1));
428 return WERR_OK;
431 /***********************************************************************
432 Open the registry. Must already have been initialized by regdb_init()
433 ***********************************************************************/
435 WERROR regdb_open( void )
437 WERROR result = WERR_OK;
439 if ( regdb ) {
440 DEBUG(10,("regdb_open: incrementing refcount (%d)\n", regdb_refcount));
441 regdb_refcount++;
442 return WERR_OK;
445 become_root();
447 regdb = db_open(NULL, state_path("registry.tdb"), 0,
448 REG_TDB_FLAGS, O_RDWR, 0600);
449 if ( !regdb ) {
450 result = ntstatus_to_werror( map_nt_error_from_unix( errno ) );
451 DEBUG(0,("regdb_open: Failed to open %s! (%s)\n",
452 state_path("registry.tdb"), strerror(errno) ));
455 unbecome_root();
457 regdb_refcount = 1;
458 DEBUG(10,("regdb_open: refcount reset (%d)\n", regdb_refcount));
460 return result;
463 /***********************************************************************
464 ***********************************************************************/
466 int regdb_close( void )
468 if (regdb_refcount == 0) {
469 return 0;
472 regdb_refcount--;
474 DEBUG(10,("regdb_close: decrementing refcount (%d)\n", regdb_refcount));
476 if ( regdb_refcount > 0 )
477 return 0;
479 SMB_ASSERT( regdb_refcount >= 0 );
481 TALLOC_FREE(regdb);
482 return 0;
485 /***********************************************************************
486 return the tdb sequence number of the registry tdb.
487 this is an indicator for the content of the registry
488 having changed. it will change upon regdb_init, too, though.
489 ***********************************************************************/
490 int regdb_get_seqnum(void)
492 return regdb->get_seqnum(regdb);
495 /***********************************************************************
496 Add subkey strings to the registry tdb under a defined key
497 fmt is the same format as tdb_pack except this function only supports
498 fstrings
499 ***********************************************************************/
501 static bool regdb_store_keys_internal(const char *key, REGSUBKEY_CTR *ctr)
503 TDB_DATA dbuf;
504 uint8 *buffer = NULL;
505 int i = 0;
506 uint32 len, buflen;
507 bool ret = true;
508 uint32 num_subkeys = regsubkey_ctr_numkeys(ctr);
509 char *keyname = NULL;
510 TALLOC_CTX *ctx = talloc_stackframe();
511 NTSTATUS status;
513 if (!key) {
514 return false;
517 keyname = talloc_strdup(ctx, key);
518 if (!keyname) {
519 return false;
521 keyname = normalize_reg_path(ctx, keyname);
523 /* allocate some initial memory */
525 buffer = (uint8 *)SMB_MALLOC(1024);
526 if (buffer == NULL) {
527 return false;
529 buflen = 1024;
530 len = 0;
532 /* store the number of subkeys */
534 len += tdb_pack(buffer+len, buflen-len, "d", num_subkeys);
536 /* pack all the strings */
538 for (i=0; i<num_subkeys; i++) {
539 size_t thistime;
541 thistime = tdb_pack(buffer+len, buflen-len, "f",
542 regsubkey_ctr_specific_key(ctr, i));
543 if (len+thistime > buflen) {
544 size_t thistime2;
546 * tdb_pack hasn't done anything because of the short
547 * buffer, allocate extra space.
549 buffer = SMB_REALLOC_ARRAY(buffer, uint8_t,
550 (len+thistime)*2);
551 if(buffer == NULL) {
552 DEBUG(0, ("regdb_store_keys: Failed to realloc "
553 "memory of size [%d]\n",
554 (len+thistime)*2));
555 ret = false;
556 goto done;
558 buflen = (len+thistime)*2;
559 thistime2 = tdb_pack(
560 buffer+len, buflen-len, "f",
561 regsubkey_ctr_specific_key(ctr, i));
562 if (thistime2 != thistime) {
563 DEBUG(0, ("tdb_pack failed\n"));
564 ret = false;
565 goto done;
568 len += thistime;
571 /* finally write out the data */
573 dbuf.dptr = buffer;
574 dbuf.dsize = len;
575 status = dbwrap_store_bystring(regdb, keyname, dbuf, TDB_REPLACE);
576 if (!NT_STATUS_IS_OK(status)) {
577 ret = false;
578 goto done;
581 done:
582 TALLOC_FREE(ctx);
583 SAFE_FREE(buffer);
584 return ret;
587 /***********************************************************************
588 Store the new subkey record and create any child key records that
589 do not currently exist
590 ***********************************************************************/
592 bool regdb_store_keys(const char *key, REGSUBKEY_CTR *ctr)
594 int num_subkeys, i;
595 char *path = NULL;
596 REGSUBKEY_CTR *subkeys = NULL, *old_subkeys = NULL;
597 char *oldkeyname = NULL;
598 TALLOC_CTX *ctx = talloc_stackframe();
599 NTSTATUS status;
601 if (!regdb_key_is_base_key(key) && !regdb_key_exists(key)) {
602 goto fail;
606 * fetch a list of the old subkeys so we can determine if anything has
607 * changed
610 if (!(old_subkeys = TALLOC_ZERO_P(ctx, REGSUBKEY_CTR))) {
611 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
612 return false;
615 regdb_fetch_keys(key, old_subkeys);
617 if ((ctr->num_subkeys && old_subkeys->num_subkeys) &&
618 (ctr->num_subkeys == old_subkeys->num_subkeys)) {
620 for (i = 0; i<ctr->num_subkeys; i++) {
621 if (strcmp(ctr->subkeys[i],
622 old_subkeys->subkeys[i]) != 0) {
623 break;
626 if (i == ctr->num_subkeys) {
628 * Nothing changed, no point to even start a tdb
629 * transaction
631 TALLOC_FREE(old_subkeys);
632 return true;
636 TALLOC_FREE(old_subkeys);
638 if (regdb->transaction_start(regdb) != 0) {
639 DEBUG(0, ("regdb_store_keys: transaction_start failed\n"));
640 goto fail;
644 * Re-fetch the old keys inside the transaction
647 if (!(old_subkeys = TALLOC_ZERO_P(ctx, REGSUBKEY_CTR))) {
648 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
649 goto cancel;
652 regdb_fetch_keys(key, old_subkeys);
655 * Make the store operation as safe as possible without transactions:
657 * (1) For each subkey removed from ctr compared with old_subkeys:
659 * (a) First delete the value db entry.
661 * (b) Next delete the secdesc db record.
663 * (c) Then delete the subkey list entry.
665 * (2) Now write the list of subkeys of the parent key,
666 * deleting removed entries and adding new ones.
668 * (3) Finally create the subkey list entries for the added keys.
670 * This way if we crash half-way in between deleting the subkeys
671 * and storing the parent's list of subkeys, no old data can pop up
672 * out of the blue when re-adding keys later on.
675 /* (1) delete removed keys' lists (values/secdesc/subkeys) */
677 num_subkeys = regsubkey_ctr_numkeys(old_subkeys);
678 for (i=0; i<num_subkeys; i++) {
679 oldkeyname = regsubkey_ctr_specific_key(old_subkeys, i);
681 if (regsubkey_ctr_key_exists(ctr, oldkeyname)) {
683 * It's still around, don't delete
686 continue;
689 /* (a) Delete the value list for this key */
691 path = talloc_asprintf(ctx, "%s/%s/%s",
692 REG_VALUE_PREFIX,
693 key,
694 oldkeyname );
695 if (!path) {
696 goto cancel;
698 path = normalize_reg_path(ctx, path);
699 if (!path) {
700 goto cancel;
702 /* Ignore errors here, we might have no values around */
703 dbwrap_delete_bystring(regdb, path);
704 TALLOC_FREE(path);
706 /* (b) Delete the secdesc for this key */
708 path = talloc_asprintf(ctx, "%s/%s/%s",
709 REG_SECDESC_PREFIX,
710 key,
711 oldkeyname );
712 if (!path) {
713 goto cancel;
715 path = normalize_reg_path(ctx, path);
716 if (!path) {
717 goto cancel;
719 status = dbwrap_delete_bystring(regdb, path);
720 /* Don't fail if there are no values around. */
721 if (!NT_STATUS_IS_OK(status) &&
722 !NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND))
724 DEBUG(1, ("Deleting %s failed: %s\n", path,
725 nt_errstr(status)));
726 goto cancel;
728 TALLOC_FREE(path);
730 /* (c) Delete the list of subkeys of this key */
732 path = talloc_asprintf(ctx, "%s/%s", key, oldkeyname);
733 if (!path) {
734 goto cancel;
736 path = normalize_reg_path(ctx, path);
737 if (!path) {
738 goto cancel;
740 status = dbwrap_delete_bystring(regdb, path);
741 /* Don't fail if the subkey record was not found. */
742 if (!NT_STATUS_IS_OK(status) &&
743 !NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND))
745 DEBUG(1, ("Deleting %s failed: %s\n", path,
746 nt_errstr(status)));
747 goto cancel;
749 TALLOC_FREE(path);
752 TALLOC_FREE(old_subkeys);
754 /* (2) store the subkey list for the parent */
756 if (!regdb_store_keys_internal(key, ctr) ) {
757 DEBUG(0,("regdb_store_keys: Failed to store new subkey list "
758 "for parent [%s]\n", key));
759 goto cancel;
762 /* (3) now create records for any subkeys that don't already exist */
764 num_subkeys = regsubkey_ctr_numkeys(ctr);
766 if (num_subkeys == 0) {
767 if (!(subkeys = TALLOC_ZERO_P(ctx, REGSUBKEY_CTR)) ) {
768 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
769 goto cancel;
772 if (!regdb_store_keys_internal(key, subkeys)) {
773 DEBUG(0,("regdb_store_keys: Failed to store "
774 "new record for key [%s]\n", key));
775 goto cancel;
777 TALLOC_FREE(subkeys);
781 for (i=0; i<num_subkeys; i++) {
782 path = talloc_asprintf(ctx, "%s/%s",
783 key,
784 regsubkey_ctr_specific_key(ctr, i));
785 if (!path) {
786 goto cancel;
788 if (!(subkeys = TALLOC_ZERO_P(ctx, REGSUBKEY_CTR)) ) {
789 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
790 goto cancel;
793 if (regdb_fetch_keys( path, subkeys ) == -1) {
794 /* create a record with 0 subkeys */
795 if (!regdb_store_keys_internal(path, subkeys)) {
796 DEBUG(0,("regdb_store_keys: Failed to store "
797 "new record for key [%s]\n", path));
798 goto cancel;
802 TALLOC_FREE(subkeys);
803 TALLOC_FREE(path);
806 if (regdb->transaction_commit(regdb) != 0) {
807 DEBUG(0, ("regdb_store_keys: Could not commit transaction\n"));
808 goto fail;
811 TALLOC_FREE(ctx);
812 return true;
814 cancel:
815 if (regdb->transaction_cancel(regdb) != 0) {
816 smb_panic("regdb_store_keys: transaction_cancel failed\n");
819 fail:
820 TALLOC_FREE(ctx);
822 return false;
826 static TDB_DATA regdb_fetch_key_internal(TALLOC_CTX *mem_ctx, const char *key)
828 char *path = NULL;
829 TDB_DATA data;
831 path = normalize_reg_path(mem_ctx, key);
832 if (!path) {
833 return make_tdb_data(NULL, 0);
836 data = dbwrap_fetch_bystring(regdb, mem_ctx, path);
838 TALLOC_FREE(path);
839 return data;
844 * check whether a given key name represents a base key,
845 * i.e one without a subkey separator ('/' or '\').
847 static bool regdb_key_is_base_key(const char *key)
849 TALLOC_CTX *mem_ctx = talloc_stackframe();
850 bool ret = false;
851 char *path;
853 if (key == NULL) {
854 goto done;
857 path = normalize_reg_path(mem_ctx, key);
858 if (path == NULL) {
859 DEBUG(0, ("out of memory! (talloc failed)\n"));
860 goto done;
863 if (*path == '\0') {
864 goto done;
867 ret = (strrchr(path, '/') == NULL);
869 done:
870 TALLOC_FREE(mem_ctx);
871 return ret;
876 * Check for the existence of a key.
878 * Existence of a key is authoritatively defined by its
879 * existence in the list of subkeys of its parent key.
880 * The exeption of this are keys without a parent key,
881 * i.e. the "base" keys (HKLM, HKCU, ...).
883 static bool regdb_key_exists(const char *key)
885 TALLOC_CTX *mem_ctx = talloc_stackframe();
886 TDB_DATA value;
887 bool ret = false;
888 char *path, *p;
890 if (key == NULL) {
891 goto done;
894 path = normalize_reg_path(mem_ctx, key);
895 if (path == NULL) {
896 DEBUG(0, ("out of memory! (talloc failed)\n"));
897 goto done;
900 if (*path == '\0') {
901 goto done;
904 p = strrchr(path, '/');
905 if (p == NULL) {
906 /* this is a base key */
907 value = regdb_fetch_key_internal(mem_ctx, path);
908 ret = (value.dptr != NULL);
909 } else {
910 /* get the list of subkeys of the parent key */
911 uint32 num_items, len, i;
912 fstring subkeyname;
914 *p = '\0';
915 p++;
916 value = regdb_fetch_key_internal(mem_ctx, path);
917 if (value.dptr == NULL) {
918 goto done;
921 len = tdb_unpack(value.dptr, value.dsize, "d", &num_items);
922 for (i = 0; i < num_items; i++) {
923 len += tdb_unpack(value.dptr +len, value.dsize -len,
924 "f", &subkeyname);
925 if (strequal(subkeyname, p)) {
926 ret = true;
927 goto done;
932 done:
933 TALLOC_FREE(mem_ctx);
934 return ret;
938 /***********************************************************************
939 Retrieve an array of strings containing subkeys. Memory should be
940 released by the caller.
941 ***********************************************************************/
943 int regdb_fetch_keys(const char *key, REGSUBKEY_CTR *ctr)
945 uint32 num_items;
946 uint8 *buf;
947 uint32 buflen, len;
948 int i;
949 fstring subkeyname;
950 int ret = -1;
951 TALLOC_CTX *frame = talloc_stackframe();
952 TDB_DATA value;
954 DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key ? key : "NULL"));
956 if (!regdb_key_exists(key)) {
957 goto done;
960 ctr->seqnum = regdb_get_seqnum();
962 value = regdb_fetch_key_internal(frame, key);
964 if (value.dptr == NULL) {
965 DEBUG(10, ("regdb_fetch_keys: no subkeys found for key [%s]\n",
966 key));
967 ret = 0;
968 goto done;
971 buf = value.dptr;
972 buflen = value.dsize;
973 len = tdb_unpack( buf, buflen, "d", &num_items);
976 * The following code breaks the abstraction that reg_objects.c sets
977 * up with regsubkey_ctr_addkey(). But if we use that with the current
978 * data structure of ctr->subkeys being an unsorted array, we end up
979 * with an O(n^2) algorithm for retrieving keys from the tdb
980 * file. This is pretty pointless, as we have to trust the data
981 * structure on disk not to have duplicates anyway. The alternative to
982 * breaking this abstraction would be to set up a more sophisticated
983 * data structure in REGSUBKEY_CTR.
985 * This makes "net conf list" for a registry with >1000 shares
986 * actually usable :-)
989 ctr->subkeys = talloc_array(ctr, char *, num_items);
990 if (ctr->subkeys == NULL) {
991 DEBUG(5, ("regdb_fetch_keys: could not allocate subkeys\n"));
992 goto done;
994 ctr->num_subkeys = num_items;
996 for (i=0; i<num_items; i++) {
997 len += tdb_unpack(buf+len, buflen-len, "f", subkeyname);
998 ctr->subkeys[i] = talloc_strdup(ctr->subkeys, subkeyname);
999 if (ctr->subkeys[i] == NULL) {
1000 DEBUG(5, ("regdb_fetch_keys: could not allocate "
1001 "subkeyname\n"));
1002 TALLOC_FREE(ctr->subkeys);
1003 ctr->num_subkeys = 0;
1004 goto done;
1008 DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items));
1010 ret = num_items;
1011 done:
1012 TALLOC_FREE(frame);
1013 return ret;
1016 /****************************************************************************
1017 Unpack a list of registry values frem the TDB
1018 ***************************************************************************/
1020 static int regdb_unpack_values(REGVAL_CTR *values, uint8 *buf, int buflen)
1022 int len = 0;
1023 uint32 type;
1024 fstring valuename;
1025 uint32 size;
1026 uint8 *data_p;
1027 uint32 num_values = 0;
1028 int i;
1030 /* loop and unpack the rest of the registry values */
1032 len += tdb_unpack(buf+len, buflen-len, "d", &num_values);
1034 for ( i=0; i<num_values; i++ ) {
1035 /* unpack the next regval */
1037 type = REG_NONE;
1038 size = 0;
1039 data_p = NULL;
1040 valuename[0] = '\0';
1041 len += tdb_unpack(buf+len, buflen-len, "fdB",
1042 valuename,
1043 &type,
1044 &size,
1045 &data_p);
1047 /* add the new value. Paranoid protective code -- make sure data_p is valid */
1049 if (*valuename && size && data_p) {
1050 regval_ctr_addvalue(values, valuename, type,
1051 (const char *)data_p, size);
1053 SAFE_FREE(data_p); /* 'B' option to tdb_unpack does a malloc() */
1055 DEBUG(8,("specific: [%s], len: %d\n", valuename, size));
1058 return len;
1061 /****************************************************************************
1062 Pack all values in all printer keys
1063 ***************************************************************************/
1065 static int regdb_pack_values(REGVAL_CTR *values, uint8 *buf, int buflen)
1067 int len = 0;
1068 int i;
1069 REGISTRY_VALUE *val;
1070 int num_values;
1072 if ( !values )
1073 return 0;
1075 num_values = regval_ctr_numvals( values );
1077 /* pack the number of values first */
1079 len += tdb_pack( buf+len, buflen-len, "d", num_values );
1081 /* loop over all values */
1083 for ( i=0; i<num_values; i++ ) {
1084 val = regval_ctr_specific_value( values, i );
1085 len += tdb_pack(buf+len, buflen-len, "fdB",
1086 regval_name(val),
1087 regval_type(val),
1088 regval_size(val),
1089 regval_data_p(val) );
1092 return len;
1095 /***********************************************************************
1096 Retrieve an array of strings containing subkeys. Memory should be
1097 released by the caller.
1098 ***********************************************************************/
1100 int regdb_fetch_values( const char* key, REGVAL_CTR *values )
1102 char *keystr = NULL;
1103 TALLOC_CTX *ctx = talloc_stackframe();
1104 int ret = 0;
1105 TDB_DATA value;
1107 DEBUG(10,("regdb_fetch_values: Looking for value of key [%s] \n", key));
1109 if (!regdb_key_exists(key)) {
1110 goto done;
1113 keystr = talloc_asprintf(ctx, "%s/%s", REG_VALUE_PREFIX, key);
1114 if (!keystr) {
1115 goto done;
1118 values->seqnum = regdb_get_seqnum();
1120 value = regdb_fetch_key_internal(ctx, keystr);
1122 if (!value.dptr) {
1123 /* all keys have zero values by default */
1124 goto done;
1127 regdb_unpack_values(values, value.dptr, value.dsize);
1128 ret = regval_ctr_numvals(values);
1130 done:
1131 TALLOC_FREE(ctx);
1132 return ret;
1135 bool regdb_store_values( const char *key, REGVAL_CTR *values )
1137 TDB_DATA old_data, data;
1138 char *keystr = NULL;
1139 TALLOC_CTX *ctx = talloc_stackframe();
1140 int len;
1141 NTSTATUS status;
1142 bool result = false;
1144 DEBUG(10,("regdb_store_values: Looking for value of key [%s] \n", key));
1146 if (!regdb_key_exists(key)) {
1147 goto done;
1150 ZERO_STRUCT(data);
1152 len = regdb_pack_values(values, data.dptr, data.dsize);
1153 if (len <= 0) {
1154 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
1155 goto done;
1158 data.dptr = TALLOC_ARRAY(ctx, uint8, len);
1159 data.dsize = len;
1161 len = regdb_pack_values(values, data.dptr, data.dsize);
1163 SMB_ASSERT( len == data.dsize );
1165 keystr = talloc_asprintf(ctx, "%s/%s", REG_VALUE_PREFIX, key );
1166 if (!keystr) {
1167 goto done;
1169 keystr = normalize_reg_path(ctx, keystr);
1170 if (!keystr) {
1171 goto done;
1174 old_data = dbwrap_fetch_bystring(regdb, ctx, keystr);
1176 if ((old_data.dptr != NULL)
1177 && (old_data.dsize == data.dsize)
1178 && (memcmp(old_data.dptr, data.dptr, data.dsize) == 0))
1180 result = true;
1181 goto done;
1184 status = dbwrap_trans_store_bystring(regdb, keystr, data, TDB_REPLACE);
1186 result = NT_STATUS_IS_OK(status);
1188 done:
1189 TALLOC_FREE(ctx);
1190 return result;
1193 static WERROR regdb_get_secdesc(TALLOC_CTX *mem_ctx, const char *key,
1194 struct security_descriptor **psecdesc)
1196 char *tdbkey;
1197 TDB_DATA data;
1198 NTSTATUS status;
1199 TALLOC_CTX *tmp_ctx = talloc_stackframe();
1200 WERROR err = WERR_OK;
1202 DEBUG(10, ("regdb_get_secdesc: Getting secdesc of key [%s]\n", key));
1204 if (!regdb_key_exists(key)) {
1205 err = WERR_BADFILE;
1206 goto done;
1209 tdbkey = talloc_asprintf(tmp_ctx, "%s/%s", REG_SECDESC_PREFIX, key);
1210 if (tdbkey == NULL) {
1211 err = WERR_NOMEM;
1212 goto done;
1214 normalize_dbkey(tdbkey);
1216 data = dbwrap_fetch_bystring(regdb, tmp_ctx, tdbkey);
1217 if (data.dptr == NULL) {
1218 err = WERR_BADFILE;
1219 goto done;
1222 status = unmarshall_sec_desc(mem_ctx, (uint8 *)data.dptr, data.dsize,
1223 psecdesc);
1225 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MEMORY)) {
1226 err = WERR_NOMEM;
1227 } else if (!NT_STATUS_IS_OK(status)) {
1228 err = WERR_REG_CORRUPT;
1231 done:
1232 TALLOC_FREE(tmp_ctx);
1233 return err;
1236 static WERROR regdb_set_secdesc(const char *key,
1237 struct security_descriptor *secdesc)
1239 TALLOC_CTX *mem_ctx = talloc_stackframe();
1240 char *tdbkey;
1241 NTSTATUS status;
1242 WERROR err = WERR_NOMEM;
1243 TDB_DATA tdbdata;
1245 if (!regdb_key_exists(key)) {
1246 err = WERR_BADFILE;
1247 goto done;
1250 tdbkey = talloc_asprintf(mem_ctx, "%s/%s", REG_SECDESC_PREFIX, key);
1251 if (tdbkey == NULL) {
1252 goto done;
1254 normalize_dbkey(tdbkey);
1256 if (secdesc == NULL) {
1257 /* assuming a delete */
1258 status = dbwrap_trans_delete_bystring(regdb, tdbkey);
1259 if (NT_STATUS_IS_OK(status)) {
1260 err = WERR_OK;
1261 } else {
1262 err = ntstatus_to_werror(status);
1264 goto done;
1267 err = ntstatus_to_werror(marshall_sec_desc(mem_ctx, secdesc,
1268 &tdbdata.dptr,
1269 &tdbdata.dsize));
1270 if (!W_ERROR_IS_OK(err)) {
1271 goto done;
1274 status = dbwrap_trans_store_bystring(regdb, tdbkey, tdbdata, 0);
1275 if (!NT_STATUS_IS_OK(status)) {
1276 err = ntstatus_to_werror(status);
1277 goto done;
1280 done:
1281 TALLOC_FREE(mem_ctx);
1282 return err;
1285 bool regdb_subkeys_need_update(REGSUBKEY_CTR *subkeys)
1287 return (regdb_get_seqnum() != subkeys->seqnum);
1290 bool regdb_values_need_update(REGVAL_CTR *values)
1292 return (regdb_get_seqnum() != values->seqnum);
1296 * Table of function pointers for default access
1299 REGISTRY_OPS regdb_ops = {
1300 .fetch_subkeys = regdb_fetch_keys,
1301 .fetch_values = regdb_fetch_values,
1302 .store_subkeys = regdb_store_keys,
1303 .store_values = regdb_store_values,
1304 .get_secdesc = regdb_get_secdesc,
1305 .set_secdesc = regdb_set_secdesc,
1306 .subkeys_need_update = regdb_subkeys_need_update,
1307 .values_need_update = regdb_values_need_update