source: branches/samba-3.2.x/source/registry/reg_backend_db.c

Last change on this file was 233, checked in by Herwig Bauernfeind, 16 years ago

Update 3.2 branch to 3.2.9

File size: 26.9 KB
Line 
1/*
2 * Unix SMB/CIFS implementation.
3 * Virtual Windows Registry Layer
4 * Copyright (C) Gerald Carter 2002-2005
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
20/* Implementation of internal registry database functions. */
21
22#include "includes.h"
23
24#undef DBGC_CLASS
25#define DBGC_CLASS DBGC_REGISTRY
26
27static struct db_context *regdb = NULL;
28static int regdb_refcount;
29
30/* List the deepest path into the registry. All part components will be created.*/
31
32/* If you want to have a part of the path controlled by the tdb and part by
33 a virtual registry db (e.g. printing), then you have to list the deepest path.
34 For example,"HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion/Print"
35 allows the reg_db backend to handle everything up to
36 "HKLM/SOFTWARE/Microsoft/Windows NT/CurrentVersion" and then we'll hook
37 the reg_printing backend onto the last component of the path (see
38 KEY_PRINTING_2K in include/rpc_reg.h) --jerry */
39
40static const char *builtin_registry_paths[] = {
41 KEY_PRINTING_2K,
42 KEY_PRINTING_PORTS,
43 KEY_PRINTING,
44 KEY_SHARES,
45 KEY_EVENTLOG,
46 KEY_SMBCONF,
47 KEY_PERFLIB,
48 KEY_PERFLIB_009,
49 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Print\\Monitors",
50 KEY_PROD_OPTIONS,
51 "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\DefaultUserConfiguration",
52 KEY_TCPIP_PARAMS,
53 KEY_NETLOGON_PARAMS,
54 KEY_HKU,
55 KEY_HKCR,
56 KEY_HKPD,
57 KEY_HKPT,
58 NULL };
59
60struct builtin_regkey_value {
61 const char *path;
62 const char *valuename;
63 uint32 type;
64 union {
65 const char *string;
66 uint32 dw_value;
67 } data;
68};
69
70static struct builtin_regkey_value builtin_registry_values[] = {
71 { KEY_PRINTING_PORTS,
72 SAMBA_PRINTER_PORT_NAME, REG_SZ, { "" } },
73 { KEY_PRINTING_2K,
74 "DefaultSpoolDirectory", REG_SZ, { "C:\\Windows\\System32\\Spool\\Printers" } },
75 { KEY_EVENTLOG,
76 "DisplayName", REG_SZ, { "Event Log" } },
77 { KEY_EVENTLOG,
78 "ErrorControl", REG_DWORD, { (char*)0x00000001 } },
79 { NULL, NULL, 0, { NULL } }
80};
81
82/**
83 * Initialize a key in the registry:
84 * create each component key of the specified path.
85 */
86static WERROR init_registry_key_internal(const char *add_path)
87{
88 WERROR werr;
89 TALLOC_CTX *frame = talloc_stackframe();
90 char *path = NULL;
91 char *base = NULL;
92 char *remaining = NULL;
93 char *keyname;
94 char *subkeyname;
95 REGSUBKEY_CTR *subkeys;
96 const char *p, *p2;
97
98 DEBUG(6, ("init_registry_key: Adding [%s]\n", add_path));
99
100 path = talloc_strdup(frame, add_path);
101 base = talloc_strdup(frame, "");
102 if (!path || !base) {
103 werr = WERR_NOMEM;
104 goto fail;
105 }
106 p = path;
107
108 while (next_token_talloc(frame, &p, &keyname, "\\")) {
109
110 /* build up the registry path from the components */
111
112 if (*base) {
113 base = talloc_asprintf(frame, "%s\\", base);
114 if (!base) {
115 werr = WERR_NOMEM;
116 goto fail;
117 }
118 }
119 base = talloc_asprintf_append(base, "%s", keyname);
120 if (!base) {
121 werr = WERR_NOMEM;
122 goto fail;
123 }
124
125 /* get the immediate subkeyname (if we have one ) */
126
127 subkeyname = talloc_strdup(frame, "");
128 if (!subkeyname) {
129 werr = WERR_NOMEM;
130 goto fail;
131 }
132 if (*p) {
133 remaining = talloc_strdup(frame, p);
134 if (!remaining) {
135 werr = WERR_NOMEM;
136 goto fail;
137 }
138 p2 = remaining;
139
140 if (!next_token_talloc(frame, &p2,
141 &subkeyname, "\\"))
142 {
143 subkeyname = talloc_strdup(frame,p2);
144 if (!subkeyname) {
145 werr = WERR_NOMEM;
146 goto fail;
147 }
148 }
149 }
150
151 DEBUG(10,("init_registry_key: Storing key [%s] with "
152 "subkey [%s]\n", base,
153 *subkeyname ? subkeyname : "NULL"));
154
155 /* we don't really care if the lookup succeeds or not
156 * since we are about to update the record.
157 * We just want any subkeys already present */
158
159 if (!(subkeys = TALLOC_ZERO_P(frame, REGSUBKEY_CTR))) {
160 DEBUG(0,("talloc() failure!\n"));
161 werr = WERR_NOMEM;
162 goto fail;
163 }
164
165 regdb_fetch_keys(base, subkeys);
166 if (*subkeyname) {
167 werr = regsubkey_ctr_addkey(subkeys, subkeyname);
168 if (!W_ERROR_IS_OK(werr)) {
169 goto fail;
170 }
171 }
172 if (!regdb_store_keys( base, subkeys)) {
173 werr = WERR_CAN_NOT_COMPLETE;
174 goto fail;
175 }
176 }
177
178 werr = WERR_OK;
179
180fail:
181 TALLOC_FREE(frame);
182 return werr;
183}
184
185/**
186 * Initialize a key in the registry:
187 * create each component key of the specified path,
188 * wrapped in one db transaction.
189 */
190WERROR init_registry_key(const char *add_path)
191{
192 WERROR werr;
193
194 if (regdb->transaction_start(regdb) != 0) {
195 DEBUG(0, ("init_registry_key: transaction_start failed\n"));
196 return WERR_REG_IO_FAILURE;
197 }
198
199 werr = init_registry_key_internal(add_path);
200 if (!W_ERROR_IS_OK(werr)) {
201 goto fail;
202 }
203
204 if (regdb->transaction_commit(regdb) != 0) {
205 DEBUG(0, ("init_registry_key: Could not commit transaction\n"));
206 return WERR_REG_IO_FAILURE;
207 }
208
209 return WERR_OK;
210
211fail:
212 if (regdb->transaction_cancel(regdb) != 0) {
213 smb_panic("init_registry_key: transaction_cancel failed\n");
214 }
215
216 return werr;
217}
218
219/***********************************************************************
220 Open the registry data in the tdb
221 ***********************************************************************/
222
223WERROR init_registry_data(void)
224{
225 WERROR werr;
226 TALLOC_CTX *frame = NULL;
227 REGVAL_CTR *values;
228 int i;
229 UNISTR2 data;
230
231 /*
232 * There are potentially quite a few store operations which are all
233 * indiviually wrapped in tdb transactions. Wrapping them in a single
234 * transaction gives just a single transaction_commit() to actually do
235 * its fsync()s. See tdb/common/transaction.c for info about nested
236 * transaction behaviour.
237 */
238
239 if (regdb->transaction_start(regdb) != 0) {
240 DEBUG(0, ("init_registry_data: tdb_transaction_start "
241 "failed\n"));
242 return WERR_REG_IO_FAILURE;
243 }
244
245 /* loop over all of the predefined paths and add each component */
246
247 for (i=0; builtin_registry_paths[i] != NULL; i++) {
248 werr = init_registry_key_internal(builtin_registry_paths[i]);
249 if (!W_ERROR_IS_OK(werr)) {
250 goto fail;
251 }
252 }
253
254 /* loop over all of the predefined values and add each component */
255
256 frame = talloc_stackframe();
257
258 for (i=0; builtin_registry_values[i].path != NULL; i++) {
259
260 values = TALLOC_ZERO_P(frame, REGVAL_CTR);
261 if (values == NULL) {
262 werr = WERR_NOMEM;
263 goto fail;
264 }
265
266 regdb_fetch_values(builtin_registry_values[i].path, values);
267
268 /* preserve existing values across restarts. Only add new ones */
269
270 if (!regval_ctr_key_exists(values,
271 builtin_registry_values[i].valuename))
272 {
273 switch(builtin_registry_values[i].type) {
274 case REG_DWORD:
275 regval_ctr_addvalue(values,
276 builtin_registry_values[i].valuename,
277 REG_DWORD,
278 (char*)&builtin_registry_values[i].data.dw_value,
279 sizeof(uint32));
280 break;
281
282 case REG_SZ:
283 init_unistr2(&data,
284 builtin_registry_values[i].data.string,
285 UNI_STR_TERMINATE);
286 regval_ctr_addvalue(values,
287 builtin_registry_values[i].valuename,
288 REG_SZ,
289 (char*)data.buffer,
290 data.uni_str_len*sizeof(uint16));
291 break;
292
293 default:
294 DEBUG(0, ("init_registry_data: invalid value "
295 "type in builtin_registry_values "
296 "[%d]\n",
297 builtin_registry_values[i].type));
298 }
299 regdb_store_values(builtin_registry_values[i].path,
300 values);
301 }
302 TALLOC_FREE(values);
303 }
304
305 TALLOC_FREE(frame);
306
307 if (regdb->transaction_commit(regdb) != 0) {
308 DEBUG(0, ("init_registry_data: Could not commit "
309 "transaction\n"));
310 return WERR_REG_IO_FAILURE;
311 }
312
313 return WERR_OK;
314
315 fail:
316
317 TALLOC_FREE(frame);
318
319 if (regdb->transaction_cancel(regdb) != 0) {
320 smb_panic("init_registry_data: tdb_transaction_cancel "
321 "failed\n");
322 }
323
324 return werr;
325}
326
327/***********************************************************************
328 Open the registry database
329 ***********************************************************************/
330
331WERROR regdb_init(void)
332{
333 const char *vstring = "INFO/version";
334 uint32 vers_id;
335 WERROR werr;
336
337 if (regdb) {
338 DEBUG(10, ("regdb_init: incrementing refcount (%d)\n",
339 regdb_refcount));
340 regdb_refcount++;
341 return WERR_OK;
342 }
343
344 regdb = db_open_trans(NULL, state_path("registry.tdb"), 0,
345 REG_TDB_FLAGS, O_RDWR, 0600);
346 if (!regdb) {
347 regdb = db_open_trans(NULL, state_path("registry.tdb"), 0,
348 REG_TDB_FLAGS, O_RDWR|O_CREAT, 0600);
349 if (!regdb) {
350 werr = ntstatus_to_werror(map_nt_error_from_unix(errno));
351 DEBUG(0,("regdb_init: Failed to open registry %s (%s)\n",
352 state_path("registry.tdb"), strerror(errno) ));
353 return werr;
354 }
355
356 DEBUG(10,("regdb_init: Successfully created registry tdb\n"));
357 }
358
359 regdb_refcount = 1;
360
361 vers_id = dbwrap_fetch_int32(regdb, vstring);
362
363 if ( vers_id != REGVER_V1 ) {
364 NTSTATUS status;
365 /* any upgrade code here if needed */
366 DEBUG(10, ("regdb_init: got %s = %d != %d\n", vstring,
367 vers_id, REGVER_V1));
368 status = dbwrap_trans_store_int32(regdb, vstring, REGVER_V1);
369 if (!NT_STATUS_IS_OK(status)) {
370 DEBUG(0, ("regdb_init: error storing %s = %d: %s\n",
371 vstring, REGVER_V1, nt_errstr(status)));
372 return ntstatus_to_werror(status);
373 } else {
374 DEBUG(10, ("regdb_init: stored %s = %d\n",
375 vstring, REGVER_V1));
376 }
377 }
378
379 return WERR_OK;
380}
381
382/***********************************************************************
383 Open the registry. Must already have been initialized by regdb_init()
384 ***********************************************************************/
385
386WERROR regdb_open( void )
387{
388 WERROR result = WERR_OK;
389
390 if ( regdb ) {
391 DEBUG(10,("regdb_open: incrementing refcount (%d)\n", regdb_refcount));
392 regdb_refcount++;
393 return WERR_OK;
394 }
395
396 become_root();
397
398 regdb = db_open_trans(NULL, state_path("registry.tdb"), 0,
399 REG_TDB_FLAGS, O_RDWR, 0600);
400 if ( !regdb ) {
401 result = ntstatus_to_werror( map_nt_error_from_unix( errno ) );
402 DEBUG(0,("regdb_open: Failed to open %s! (%s)\n",
403 state_path("registry.tdb"), strerror(errno) ));
404 }
405
406 unbecome_root();
407
408 regdb_refcount = 1;
409 DEBUG(10,("regdb_open: refcount reset (%d)\n", regdb_refcount));
410
411 return result;
412}
413
414/***********************************************************************
415 ***********************************************************************/
416
417int regdb_close( void )
418{
419 if (regdb_refcount == 0) {
420 return 0;
421 }
422
423 regdb_refcount--;
424
425 DEBUG(10,("regdb_close: decrementing refcount (%d)\n", regdb_refcount));
426
427 if ( regdb_refcount > 0 )
428 return 0;
429
430 SMB_ASSERT( regdb_refcount >= 0 );
431
432 TALLOC_FREE(regdb);
433 return 0;
434}
435
436/***********************************************************************
437 return the tdb sequence number of the registry tdb.
438 this is an indicator for the content of the registry
439 having changed. it will change upon regdb_init, too, though.
440 ***********************************************************************/
441int regdb_get_seqnum(void)
442{
443 return regdb->get_seqnum(regdb);
444}
445
446/***********************************************************************
447 Add subkey strings to the registry tdb under a defined key
448 fmt is the same format as tdb_pack except this function only supports
449 fstrings
450 ***********************************************************************/
451
452static bool regdb_store_keys_internal(const char *key, REGSUBKEY_CTR *ctr)
453{
454 TDB_DATA dbuf;
455 uint8 *buffer = NULL;
456 int i = 0;
457 uint32 len, buflen;
458 bool ret = true;
459 uint32 num_subkeys = regsubkey_ctr_numkeys(ctr);
460 char *keyname = NULL;
461 TALLOC_CTX *ctx = talloc_stackframe();
462 NTSTATUS status;
463
464 if (!key) {
465 return false;
466 }
467
468 keyname = talloc_strdup(ctx, key);
469 if (!keyname) {
470 return false;
471 }
472 keyname = normalize_reg_path(ctx, keyname);
473
474 /* allocate some initial memory */
475
476 buffer = (uint8 *)SMB_MALLOC(1024);
477 if (buffer == NULL) {
478 return false;
479 }
480 buflen = 1024;
481 len = 0;
482
483 /* store the number of subkeys */
484
485 len += tdb_pack(buffer+len, buflen-len, "d", num_subkeys);
486
487 /* pack all the strings */
488
489 for (i=0; i<num_subkeys; i++) {
490 size_t thistime;
491
492 thistime = tdb_pack(buffer+len, buflen-len, "f",
493 regsubkey_ctr_specific_key(ctr, i));
494 if (len+thistime > buflen) {
495 size_t thistime2;
496 /*
497 * tdb_pack hasn't done anything because of the short
498 * buffer, allocate extra space.
499 */
500 buffer = SMB_REALLOC_ARRAY(buffer, uint8_t,
501 (len+thistime)*2);
502 if(buffer == NULL) {
503 DEBUG(0, ("regdb_store_keys: Failed to realloc "
504 "memory of size [%d]\n",
505 (len+thistime)*2));
506 ret = false;
507 goto done;
508 }
509 buflen = (len+thistime)*2;
510 thistime2 = tdb_pack(
511 buffer+len, buflen-len, "f",
512 regsubkey_ctr_specific_key(ctr, i));
513 if (thistime2 != thistime) {
514 DEBUG(0, ("tdb_pack failed\n"));
515 ret = false;
516 goto done;
517 }
518 }
519 len += thistime;
520 }
521
522 /* finally write out the data */
523
524 dbuf.dptr = buffer;
525 dbuf.dsize = len;
526 status = dbwrap_store_bystring(regdb, keyname, dbuf, TDB_REPLACE);
527 if (!NT_STATUS_IS_OK(status)) {
528 ret = false;
529 goto done;
530 }
531
532done:
533 TALLOC_FREE(ctx);
534 SAFE_FREE(buffer);
535 return ret;
536}
537
538/***********************************************************************
539 Store the new subkey record and create any child key records that
540 do not currently exist
541 ***********************************************************************/
542
543bool regdb_store_keys(const char *key, REGSUBKEY_CTR *ctr)
544{
545 int num_subkeys, i;
546 char *path = NULL;
547 REGSUBKEY_CTR *subkeys = NULL, *old_subkeys = NULL;
548 char *oldkeyname = NULL;
549 TALLOC_CTX *ctx = talloc_stackframe();
550 NTSTATUS status;
551
552 /*
553 * fetch a list of the old subkeys so we can determine if anything has
554 * changed
555 */
556
557 if (!(old_subkeys = TALLOC_ZERO_P(ctx, REGSUBKEY_CTR))) {
558 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
559 return false;
560 }
561
562 regdb_fetch_keys(key, old_subkeys);
563
564 if ((ctr->num_subkeys && old_subkeys->num_subkeys) &&
565 (ctr->num_subkeys == old_subkeys->num_subkeys)) {
566
567 for (i = 0; i<ctr->num_subkeys; i++) {
568 if (strcmp(ctr->subkeys[i],
569 old_subkeys->subkeys[i]) != 0) {
570 break;
571 }
572 }
573 if (i == ctr->num_subkeys) {
574 /*
575 * Nothing changed, no point to even start a tdb
576 * transaction
577 */
578 TALLOC_FREE(old_subkeys);
579 return true;
580 }
581 }
582
583 TALLOC_FREE(old_subkeys);
584
585 if (regdb->transaction_start(regdb) != 0) {
586 DEBUG(0, ("regdb_store_keys: transaction_start failed\n"));
587 goto fail;
588 }
589
590 /*
591 * Re-fetch the old keys inside the transaction
592 */
593
594 if (!(old_subkeys = TALLOC_ZERO_P(ctx, REGSUBKEY_CTR))) {
595 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
596 goto cancel;
597 }
598
599 regdb_fetch_keys(key, old_subkeys);
600
601 /* store the subkey list for the parent */
602
603 if (!regdb_store_keys_internal(key, ctr) ) {
604 DEBUG(0,("regdb_store_keys: Failed to store new subkey list "
605 "for parent [%s]\n", key));
606 goto cancel;
607 }
608
609 /* now delete removed keys */
610
611 num_subkeys = regsubkey_ctr_numkeys(old_subkeys);
612 for (i=0; i<num_subkeys; i++) {
613 oldkeyname = regsubkey_ctr_specific_key(old_subkeys, i);
614
615 if (regsubkey_ctr_key_exists(ctr, oldkeyname)) {
616 /*
617 * It's still around, don't delete
618 */
619
620 continue;
621 }
622
623 path = talloc_asprintf(ctx, "%s/%s", key, oldkeyname);
624 if (!path) {
625 goto cancel;
626 }
627 path = normalize_reg_path(ctx, path);
628 if (!path) {
629 goto cancel;
630 }
631 status = dbwrap_delete_bystring(regdb, path);
632 if (!NT_STATUS_IS_OK(status)) {
633 DEBUG(1, ("Deleting %s failed\n", path));
634 goto cancel;
635 }
636
637 TALLOC_FREE(path);
638 path = talloc_asprintf(ctx, "%s/%s/%s",
639 REG_VALUE_PREFIX,
640 key,
641 oldkeyname );
642 if (!path) {
643 goto cancel;
644 }
645 path = normalize_reg_path(ctx, path);
646 if (!path) {
647 goto cancel;
648 }
649
650 /*
651 * Ignore errors here, we might have no values around
652 */
653 dbwrap_delete_bystring(regdb, path);
654 TALLOC_FREE(path);
655 }
656
657 TALLOC_FREE(old_subkeys);
658
659 /* now create records for any subkeys that don't already exist */
660
661 num_subkeys = regsubkey_ctr_numkeys(ctr);
662
663 if (num_subkeys == 0) {
664 if (!(subkeys = TALLOC_ZERO_P(ctx, REGSUBKEY_CTR)) ) {
665 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
666 goto cancel;
667 }
668
669 if (!regdb_store_keys_internal(key, subkeys)) {
670 DEBUG(0,("regdb_store_keys: Failed to store "
671 "new record for key [%s]\n", key));
672 goto cancel;
673 }
674 TALLOC_FREE(subkeys);
675
676 }
677
678 for (i=0; i<num_subkeys; i++) {
679 path = talloc_asprintf(ctx, "%s/%s",
680 key,
681 regsubkey_ctr_specific_key(ctr, i));
682 if (!path) {
683 goto cancel;
684 }
685 if (!(subkeys = TALLOC_ZERO_P(ctx, REGSUBKEY_CTR)) ) {
686 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
687 goto cancel;
688 }
689
690 if (regdb_fetch_keys( path, subkeys ) == -1) {
691 /* create a record with 0 subkeys */
692 if (!regdb_store_keys_internal(path, subkeys)) {
693 DEBUG(0,("regdb_store_keys: Failed to store "
694 "new record for key [%s]\n", path));
695 goto cancel;
696 }
697 }
698
699 TALLOC_FREE(subkeys);
700 TALLOC_FREE(path);
701 }
702
703 if (regdb->transaction_commit(regdb) != 0) {
704 DEBUG(0, ("regdb_store_keys: Could not commit transaction\n"));
705 goto fail;
706 }
707
708 TALLOC_FREE(ctx);
709 return true;
710
711cancel:
712 if (regdb->transaction_cancel(regdb) != 0) {
713 smb_panic("regdb_store_keys: transaction_cancel failed\n");
714 }
715
716fail:
717 TALLOC_FREE(ctx);
718
719 return false;
720}
721
722
723/***********************************************************************
724 Retrieve an array of strings containing subkeys. Memory should be
725 released by the caller.
726 ***********************************************************************/
727
728int regdb_fetch_keys(const char *key, REGSUBKEY_CTR *ctr)
729{
730 char *path = NULL;
731 uint32 num_items;
732 uint8 *buf;
733 uint32 buflen, len;
734 int i;
735 fstring subkeyname;
736 int ret = -1;
737 int dbret = -1;
738 TALLOC_CTX *frame = talloc_stackframe();
739 TDB_DATA value;
740
741 DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key ? key : "NULL"));
742
743 path = talloc_strdup(frame, key);
744 if (!path) {
745 goto fail;
746 }
747
748 /* convert to key format */
749 path = talloc_string_sub(frame, path, "\\", "/");
750 if (!path) {
751 goto fail;
752 }
753 strupper_m(path);
754
755 ctr->seqnum = regdb_get_seqnum();
756
757 dbret = regdb->fetch(regdb, frame, string_term_tdb_data(path), &value);
758 if (dbret != 0) {
759 goto fail;
760 }
761
762 buf = value.dptr;
763 buflen = value.dsize;
764
765 if ( !buf ) {
766 DEBUG(5,("regdb_fetch_keys: tdb lookup failed to locate key [%s]\n", key));
767 goto fail;
768 }
769
770 len = tdb_unpack( buf, buflen, "d", &num_items);
771
772 /*
773 * The following code breaks the abstraction that reg_objects.c sets
774 * up with regsubkey_ctr_addkey(). But if we use that with the current
775 * data structure of ctr->subkeys being an unsorted array, we end up
776 * with an O(n^2) algorithm for retrieving keys from the tdb
777 * file. This is pretty pointless, as we have to trust the data
778 * structure on disk not to have duplicates anyway. The alternative to
779 * breaking this abstraction would be to set up a more sophisticated
780 * data structure in REGSUBKEY_CTR.
781 *
782 * This makes "net conf list" for a registry with >1000 shares
783 * actually usable :-)
784 */
785
786 ctr->subkeys = talloc_array(ctr, char *, num_items);
787 if (ctr->subkeys == NULL) {
788 DEBUG(5, ("regdb_fetch_keys: could not allocate subkeys\n"));
789 goto fail;
790 }
791 ctr->num_subkeys = num_items;
792
793 for (i=0; i<num_items; i++) {
794 len += tdb_unpack(buf+len, buflen-len, "f", subkeyname);
795 ctr->subkeys[i] = talloc_strdup(ctr->subkeys, subkeyname);
796 if (ctr->subkeys[i] == NULL) {
797 DEBUG(5, ("regdb_fetch_keys: could not allocate "
798 "subkeyname\n"));
799 TALLOC_FREE(ctr->subkeys);
800 ctr->num_subkeys = 0;
801 goto fail;
802 }
803 }
804
805 DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items));
806
807 ret = num_items;
808 fail:
809 TALLOC_FREE(frame);
810 return ret;
811}
812
813/****************************************************************************
814 Unpack a list of registry values frem the TDB
815 ***************************************************************************/
816
817static int regdb_unpack_values(REGVAL_CTR *values, uint8 *buf, int buflen)
818{
819 int len = 0;
820 uint32 type;
821 fstring valuename;
822 uint32 size;
823 uint8 *data_p;
824 uint32 num_values = 0;
825 int i;
826
827 /* loop and unpack the rest of the registry values */
828
829 len += tdb_unpack(buf+len, buflen-len, "d", &num_values);
830
831 for ( i=0; i<num_values; i++ ) {
832 /* unpack the next regval */
833
834 type = REG_NONE;
835 size = 0;
836 data_p = NULL;
837 valuename[0] = '\0';
838 len += tdb_unpack(buf+len, buflen-len, "fdB",
839 valuename,
840 &type,
841 &size,
842 &data_p);
843
844 /* add the new value. Paranoid protective code -- make sure data_p is valid */
845
846 if (*valuename && size && data_p) {
847 regval_ctr_addvalue(values, valuename, type,
848 (const char *)data_p, size);
849 }
850 SAFE_FREE(data_p); /* 'B' option to tdb_unpack does a malloc() */
851
852 DEBUG(8,("specific: [%s], len: %d\n", valuename, size));
853 }
854
855 return len;
856}
857
858/****************************************************************************
859 Pack all values in all printer keys
860 ***************************************************************************/
861
862static int regdb_pack_values(REGVAL_CTR *values, uint8 *buf, int buflen)
863{
864 int len = 0;
865 int i;
866 REGISTRY_VALUE *val;
867 int num_values;
868
869 if ( !values )
870 return 0;
871
872 num_values = regval_ctr_numvals( values );
873
874 /* pack the number of values first */
875
876 len += tdb_pack( buf+len, buflen-len, "d", num_values );
877
878 /* loop over all values */
879
880 for ( i=0; i<num_values; i++ ) {
881 val = regval_ctr_specific_value( values, i );
882 len += tdb_pack(buf+len, buflen-len, "fdB",
883 regval_name(val),
884 regval_type(val),
885 regval_size(val),
886 regval_data_p(val) );
887 }
888
889 return len;
890}
891
892/***********************************************************************
893 Retrieve an array of strings containing subkeys. Memory should be
894 released by the caller.
895 ***********************************************************************/
896
897int regdb_fetch_values( const char* key, REGVAL_CTR *values )
898{
899 char *keystr = NULL;
900 TALLOC_CTX *ctx = talloc_stackframe();
901 int ret = 0;
902 int dbret = -1;
903 TDB_DATA value;
904
905 DEBUG(10,("regdb_fetch_values: Looking for value of key [%s] \n", key));
906
907 keystr = talloc_asprintf(ctx, "%s/%s", REG_VALUE_PREFIX, key);
908 if (!keystr) {
909 return 0;
910 }
911 keystr = normalize_reg_path(ctx, keystr);
912 if (!keystr) {
913 goto done;
914 }
915
916 values->seqnum = regdb_get_seqnum();
917
918 dbret = regdb->fetch(regdb, ctx, string_term_tdb_data(keystr), &value);
919 if (dbret != 0) {
920 goto done;
921 }
922
923 if (!value.dptr) {
924 /* all keys have zero values by default */
925 goto done;
926 }
927
928 regdb_unpack_values(values, value.dptr, value.dsize);
929 ret = regval_ctr_numvals(values);
930
931done:
932 TALLOC_FREE(ctx);
933 return ret;
934}
935
936bool regdb_store_values( const char *key, REGVAL_CTR *values )
937{
938 TDB_DATA old_data, data;
939 char *keystr = NULL;
940 TALLOC_CTX *ctx = talloc_stackframe();
941 int len;
942 NTSTATUS status;
943 bool result = false;
944
945 DEBUG(10,("regdb_store_values: Looking for value of key [%s] \n", key));
946
947 ZERO_STRUCT(data);
948
949 len = regdb_pack_values(values, data.dptr, data.dsize);
950 if (len <= 0) {
951 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
952 goto done;
953 }
954
955 data.dptr = TALLOC_ARRAY(ctx, uint8, len);
956 data.dsize = len;
957
958 len = regdb_pack_values(values, data.dptr, data.dsize);
959
960 SMB_ASSERT( len == data.dsize );
961
962 keystr = talloc_asprintf(ctx, "%s/%s", REG_VALUE_PREFIX, key );
963 if (!keystr) {
964 goto done;
965 }
966 keystr = normalize_reg_path(ctx, keystr);
967 if (!keystr) {
968 goto done;
969 }
970
971 old_data = dbwrap_fetch_bystring(regdb, ctx, keystr);
972
973 if ((old_data.dptr != NULL)
974 && (old_data.dsize == data.dsize)
975 && (memcmp(old_data.dptr, data.dptr, data.dsize) == 0))
976 {
977 result = true;
978 goto done;
979 }
980
981 status = dbwrap_trans_store(regdb, string_term_tdb_data(keystr), data,
982 TDB_REPLACE);
983
984 result = NT_STATUS_IS_OK(status);
985
986done:
987 TALLOC_FREE(ctx);
988 return result;
989}
990
991static WERROR regdb_get_secdesc(TALLOC_CTX *mem_ctx, const char *key,
992 struct security_descriptor **psecdesc)
993{
994 char *tdbkey;
995 TDB_DATA data;
996 NTSTATUS status;
997 TALLOC_CTX *tmp_ctx = talloc_stackframe();
998 WERROR err = WERR_OK;
999
1000 DEBUG(10, ("regdb_get_secdesc: Getting secdesc of key [%s]\n", key));
1001
1002 tdbkey = talloc_asprintf(tmp_ctx, "%s/%s", REG_SECDESC_PREFIX, key);
1003 if (tdbkey == NULL) {
1004 err = WERR_NOMEM;
1005 goto done;
1006 }
1007 normalize_dbkey(tdbkey);
1008
1009 data = dbwrap_fetch_bystring(regdb, tmp_ctx, tdbkey);
1010 if (data.dptr == NULL) {
1011 err = WERR_BADFILE;
1012 goto done;
1013 }
1014
1015 status = unmarshall_sec_desc(mem_ctx, (uint8 *)data.dptr, data.dsize,
1016 psecdesc);
1017
1018 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MEMORY)) {
1019 err = WERR_NOMEM;
1020 } else if (!NT_STATUS_IS_OK(status)) {
1021 err = WERR_REG_CORRUPT;
1022 }
1023
1024done:
1025 TALLOC_FREE(tmp_ctx);
1026 return err;
1027}
1028
1029static WERROR regdb_set_secdesc(const char *key,
1030 struct security_descriptor *secdesc)
1031{
1032 TALLOC_CTX *mem_ctx = talloc_stackframe();
1033 char *tdbkey;
1034 NTSTATUS status;
1035 WERROR err = WERR_NOMEM;
1036 TDB_DATA tdbdata;
1037
1038 tdbkey = talloc_asprintf(mem_ctx, "%s/%s", REG_SECDESC_PREFIX, key);
1039 if (tdbkey == NULL) {
1040 goto done;
1041 }
1042 normalize_dbkey(tdbkey);
1043
1044 if (secdesc == NULL) {
1045 /* assuming a delete */
1046 status = dbwrap_trans_delete(regdb,
1047 string_term_tdb_data(tdbkey));
1048 if (NT_STATUS_IS_OK(status)) {
1049 err = WERR_OK;
1050 } else {
1051 err = ntstatus_to_werror(status);
1052 }
1053 goto done;
1054 }
1055
1056 err = ntstatus_to_werror(marshall_sec_desc(mem_ctx, secdesc,
1057 &tdbdata.dptr,
1058 &tdbdata.dsize));
1059 if (!W_ERROR_IS_OK(err)) {
1060 goto done;
1061 }
1062
1063 status = dbwrap_trans_store(regdb, string_term_tdb_data(tdbkey),
1064 tdbdata, 0);
1065 if (!NT_STATUS_IS_OK(status)) {
1066 err = ntstatus_to_werror(status);
1067 goto done;
1068 }
1069
1070 done:
1071 TALLOC_FREE(mem_ctx);
1072 return err;
1073}
1074
1075bool regdb_subkeys_need_update(REGSUBKEY_CTR *subkeys)
1076{
1077 return (regdb_get_seqnum() != subkeys->seqnum);
1078}
1079
1080bool regdb_values_need_update(REGVAL_CTR *values)
1081{
1082 return (regdb_get_seqnum() != values->seqnum);
1083}
1084
1085/*
1086 * Table of function pointers for default access
1087 */
1088
1089REGISTRY_OPS regdb_ops = {
1090 .fetch_subkeys = regdb_fetch_keys,
1091 .fetch_values = regdb_fetch_values,
1092 .store_subkeys = regdb_store_keys,
1093 .store_values = regdb_store_values,
1094 .get_secdesc = regdb_get_secdesc,
1095 .set_secdesc = regdb_set_secdesc,
1096 .subkeys_need_update = regdb_subkeys_need_update,
1097 .values_need_update = regdb_values_need_update
1098};
Note: See TracBrowser for help on using the repository browser.