s3:registry: don't directly access key->subkeys->subkeys[] in reg_backend_db.c
[ira/wip.git] / source3 / registry / reg_backend_db.c
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
27 static struct db_context *regdb = NULL;
28 static int regdb_refcount;
29
30 static bool regdb_key_exists(const char *key);
31 static bool regdb_key_is_base_key(const char *key);
32
33 /* List the deepest path into the registry.  All part components will be created.*/
34
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 */
42
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 };
70
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;
79 };
80
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 } }
91 };
92
93 /**
94  * Initialize a key in the registry:
95  * create each component key of the specified path.
96  */
97 static WERROR init_registry_key_internal(const char *add_path)
98 {
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         struct regsubkey_ctr *subkeys;
107         const char *p, *p2;
108
109         DEBUG(6, ("init_registry_key: Adding [%s]\n", add_path));
110
111         path = talloc_strdup(frame, add_path);
112         base = talloc_strdup(frame, "");
113         if (!path || !base) {
114                 werr = WERR_NOMEM;
115                 goto fail;
116         }
117         p = path;
118
119         while (next_token_talloc(frame, &p, &keyname, "\\")) {
120
121                 /* build up the registry path from the components */
122
123                 if (*base) {
124                         base = talloc_asprintf(frame, "%s\\", base);
125                         if (!base) {
126                                 werr = WERR_NOMEM;
127                                 goto fail;
128                         }
129                 }
130                 base = talloc_asprintf_append(base, "%s", keyname);
131                 if (!base) {
132                         werr = WERR_NOMEM;
133                         goto fail;
134                 }
135
136                 /* get the immediate subkeyname (if we have one ) */
137
138                 subkeyname = talloc_strdup(frame, "");
139                 if (!subkeyname) {
140                         werr = WERR_NOMEM;
141                         goto fail;
142                 }
143                 if (*p) {
144                         remaining = talloc_strdup(frame, p);
145                         if (!remaining) {
146                                 werr = WERR_NOMEM;
147                                 goto fail;
148                         }
149                         p2 = remaining;
150
151                         if (!next_token_talloc(frame, &p2,
152                                                 &subkeyname, "\\"))
153                         {
154                                 subkeyname = talloc_strdup(frame,p2);
155                                 if (!subkeyname) {
156                                         werr = WERR_NOMEM;
157                                         goto fail;
158                                 }
159                         }
160                 }
161
162                 DEBUG(10,("init_registry_key: Storing key [%s] with "
163                           "subkey [%s]\n", base,
164                           *subkeyname ? subkeyname : "NULL"));
165
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 */
169
170                 if (!(subkeys = TALLOC_ZERO_P(frame, struct regsubkey_ctr))) {
171                         DEBUG(0,("talloc() failure!\n"));
172                         werr = WERR_NOMEM;
173                         goto fail;
174                 }
175
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;
181                         }
182                 }
183                 if (!regdb_store_keys( base, subkeys)) {
184                         werr = WERR_CAN_NOT_COMPLETE;
185                         goto fail;
186                 }
187         }
188
189         werr = WERR_OK;
190
191 fail:
192         TALLOC_FREE(frame);
193         return werr;
194 }
195
196 /**
197  * Initialize a key in the registry:
198  * create each component key of the specified path,
199  * wrapped in one db transaction.
200  */
201 WERROR init_registry_key(const char *add_path)
202 {
203         WERROR werr;
204
205         if (regdb_key_exists(add_path)) {
206                 return WERR_OK;
207         }
208
209         if (regdb->transaction_start(regdb) != 0) {
210                 DEBUG(0, ("init_registry_key: transaction_start failed\n"));
211                 return WERR_REG_IO_FAILURE;
212         }
213
214         werr = init_registry_key_internal(add_path);
215         if (!W_ERROR_IS_OK(werr)) {
216                 goto fail;
217         }
218
219         if (regdb->transaction_commit(regdb) != 0) {
220                 DEBUG(0, ("init_registry_key: Could not commit transaction\n"));
221                 return WERR_REG_IO_FAILURE;
222         }
223
224         return WERR_OK;
225
226 fail:
227         if (regdb->transaction_cancel(regdb) != 0) {
228                 smb_panic("init_registry_key: transaction_cancel failed\n");
229         }
230
231         return werr;
232 }
233
234 /***********************************************************************
235  Open the registry data in the tdb
236  ***********************************************************************/
237
238 WERROR init_registry_data(void)
239 {
240         WERROR werr;
241         TALLOC_CTX *frame = talloc_stackframe();
242         REGVAL_CTR *values;
243         int i;
244         UNISTR2 data;
245
246         /*
247          * First, check for the existence of the needed keys and values.
248          * If all do already exist, we can save the writes.
249          */
250         for (i=0; builtin_registry_paths[i] != NULL; i++) {
251                 if (!regdb_key_exists(builtin_registry_paths[i])) {
252                         goto do_init;
253                 }
254         }
255
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;
261                 }
262
263                 regdb_fetch_values(builtin_registry_values[i].path, values);
264                 if (!regval_ctr_key_exists(values,
265                                         builtin_registry_values[i].valuename))
266                 {
267                         TALLOC_FREE(values);
268                         goto do_init;
269                 }
270
271                 TALLOC_FREE(values);
272         }
273
274         werr = WERR_OK;
275         goto done;
276
277 do_init:
278
279         /*
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.
285          */
286
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;
292         }
293
294         /* loop over all of the predefined paths and add each component */
295
296         for (i=0; builtin_registry_paths[i] != NULL; i++) {
297                 if (regdb_key_exists(builtin_registry_paths[i])) {
298                         continue;
299                 }
300                 werr = init_registry_key_internal(builtin_registry_paths[i]);
301                 if (!W_ERROR_IS_OK(werr)) {
302                         goto fail;
303                 }
304         }
305
306         /* loop over all of the predefined values and add each component */
307
308         for (i=0; builtin_registry_values[i].path != NULL; i++) {
309
310                 values = TALLOC_ZERO_P(frame, REGVAL_CTR);
311                 if (values == NULL) {
312                         werr = WERR_NOMEM;
313                         goto fail;
314                 }
315
316                 regdb_fetch_values(builtin_registry_values[i].path, values);
317
318                 /* preserve existing values across restarts. Only add new ones */
319
320                 if (!regval_ctr_key_exists(values,
321                                         builtin_registry_values[i].valuename))
322                 {
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;
331
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;
342
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));
348                         }
349                         regdb_store_values(builtin_registry_values[i].path,
350                                            values);
351                 }
352                 TALLOC_FREE(values);
353         }
354
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;
361         }
362
363         goto done;
364
365 fail:
366         if (regdb->transaction_cancel(regdb) != 0) {
367                 smb_panic("init_registry_data: tdb_transaction_cancel "
368                           "failed\n");
369         }
370
371 done:
372         TALLOC_FREE(frame);
373         return werr;
374 }
375
376 /***********************************************************************
377  Open the registry database
378  ***********************************************************************/
379  
380 WERROR regdb_init(void)
381 {
382         const char *vstring = "INFO/version";
383         uint32 vers_id;
384         WERROR werr;
385
386         if (regdb) {
387                 DEBUG(10, ("regdb_init: incrementing refcount (%d)\n",
388                           regdb_refcount));
389                 regdb_refcount++;
390                 return WERR_OK;
391         }
392
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;
403                 }
404                 
405                 DEBUG(10,("regdb_init: Successfully created registry tdb\n"));
406         }
407
408         regdb_refcount = 1;
409
410         vers_id = dbwrap_fetch_int32(regdb, vstring);
411
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));
425                 }
426         }
427
428         return WERR_OK;
429 }
430
431 /***********************************************************************
432  Open the registry.  Must already have been initialized by regdb_init()
433  ***********************************************************************/
434
435 WERROR regdb_open( void )
436 {
437         WERROR result = WERR_OK;
438
439         if ( regdb ) {
440                 DEBUG(10,("regdb_open: incrementing refcount (%d)\n", regdb_refcount));
441                 regdb_refcount++;
442                 return WERR_OK;
443         }
444         
445         become_root();
446
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) ));
453         }
454
455         unbecome_root();
456
457         regdb_refcount = 1;
458         DEBUG(10,("regdb_open: refcount reset (%d)\n", regdb_refcount));
459
460         return result;
461 }
462
463 /***********************************************************************
464  ***********************************************************************/
465
466 int regdb_close( void )
467 {
468         if (regdb_refcount == 0) {
469                 return 0;
470         }
471
472         regdb_refcount--;
473
474         DEBUG(10,("regdb_close: decrementing refcount (%d)\n", regdb_refcount));
475
476         if ( regdb_refcount > 0 )
477                 return 0;
478
479         SMB_ASSERT( regdb_refcount >= 0 );
480
481         TALLOC_FREE(regdb);
482         return 0;
483 }
484
485 WERROR regdb_transaction_start(void)
486 {
487         return (regdb->transaction_start(regdb) == 0) ?
488                 WERR_OK : WERR_REG_IO_FAILURE;
489 }
490
491 WERROR regdb_transaction_commit(void)
492 {
493         return (regdb->transaction_commit(regdb) == 0) ?
494                 WERR_OK : WERR_REG_IO_FAILURE;
495 }
496
497 WERROR regdb_transaction_cancel(void)
498 {
499         return (regdb->transaction_cancel(regdb) == 0) ?
500                 WERR_OK : WERR_REG_IO_FAILURE;
501 }
502
503 /***********************************************************************
504  return the tdb sequence number of the registry tdb.
505  this is an indicator for the content of the registry
506  having changed. it will change upon regdb_init, too, though.
507  ***********************************************************************/
508 int regdb_get_seqnum(void)
509 {
510         return regdb->get_seqnum(regdb);
511 }
512
513 /***********************************************************************
514  Add subkey strings to the registry tdb under a defined key
515  fmt is the same format as tdb_pack except this function only supports
516  fstrings
517  ***********************************************************************/
518
519 static bool regdb_store_keys_internal(const char *key, struct regsubkey_ctr *ctr)
520 {
521         TDB_DATA dbuf;
522         uint8 *buffer = NULL;
523         int i = 0;
524         uint32 len, buflen;
525         bool ret = true;
526         uint32 num_subkeys = regsubkey_ctr_numkeys(ctr);
527         char *keyname = NULL;
528         TALLOC_CTX *ctx = talloc_stackframe();
529         NTSTATUS status;
530
531         if (!key) {
532                 return false;
533         }
534
535         keyname = talloc_strdup(ctx, key);
536         if (!keyname) {
537                 return false;
538         }
539         keyname = normalize_reg_path(ctx, keyname);
540
541         /* allocate some initial memory */
542
543         buffer = (uint8 *)SMB_MALLOC(1024);
544         if (buffer == NULL) {
545                 return false;
546         }
547         buflen = 1024;
548         len = 0;
549
550         /* store the number of subkeys */
551
552         len += tdb_pack(buffer+len, buflen-len, "d", num_subkeys);
553
554         /* pack all the strings */
555
556         for (i=0; i<num_subkeys; i++) {
557                 size_t thistime;
558
559                 thistime = tdb_pack(buffer+len, buflen-len, "f",
560                                     regsubkey_ctr_specific_key(ctr, i));
561                 if (len+thistime > buflen) {
562                         size_t thistime2;
563                         /*
564                          * tdb_pack hasn't done anything because of the short
565                          * buffer, allocate extra space.
566                          */
567                         buffer = SMB_REALLOC_ARRAY(buffer, uint8_t,
568                                                    (len+thistime)*2);
569                         if(buffer == NULL) {
570                                 DEBUG(0, ("regdb_store_keys: Failed to realloc "
571                                           "memory of size [%u]\n",
572                                           (unsigned int)(len+thistime)*2));
573                                 ret = false;
574                                 goto done;
575                         }
576                         buflen = (len+thistime)*2;
577                         thistime2 = tdb_pack(
578                                 buffer+len, buflen-len, "f",
579                                 regsubkey_ctr_specific_key(ctr, i));
580                         if (thistime2 != thistime) {
581                                 DEBUG(0, ("tdb_pack failed\n"));
582                                 ret = false;
583                                 goto done;
584                         }
585                 }
586                 len += thistime;
587         }
588
589         /* finally write out the data */
590
591         dbuf.dptr = buffer;
592         dbuf.dsize = len;
593         status = dbwrap_store_bystring(regdb, keyname, dbuf, TDB_REPLACE);
594         if (!NT_STATUS_IS_OK(status)) {
595                 ret = false;
596                 goto done;
597         }
598
599         /*
600          * Delete a sorted subkey cache for regdb_key_exists, will be
601          * recreated automatically
602          */
603         keyname = talloc_asprintf(ctx, "%s/%s", REG_SORTED_SUBKEYS_PREFIX,
604                                   keyname);
605         if (keyname != NULL) {
606                 dbwrap_delete_bystring(regdb, keyname);
607         }
608
609 done:
610         TALLOC_FREE(ctx);
611         SAFE_FREE(buffer);
612         return ret;
613 }
614
615 /***********************************************************************
616  Store the new subkey record and create any child key records that
617  do not currently exist
618  ***********************************************************************/
619
620 bool regdb_store_keys(const char *key, struct regsubkey_ctr *ctr)
621 {
622         int num_subkeys, old_num_subkeys, i;
623         char *path = NULL;
624         struct regsubkey_ctr *subkeys = NULL, *old_subkeys = NULL;
625         char *oldkeyname = NULL;
626         TALLOC_CTX *ctx = talloc_stackframe();
627         NTSTATUS status;
628
629         if (!regdb_key_is_base_key(key) && !regdb_key_exists(key)) {
630                 goto fail;
631         }
632
633         /*
634          * fetch a list of the old subkeys so we can determine if anything has
635          * changed
636          */
637
638         if (!(old_subkeys = TALLOC_ZERO_P(ctx, struct regsubkey_ctr))) {
639                 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
640                 return false;
641         }
642
643         regdb_fetch_keys(key, old_subkeys);
644
645         num_subkeys = regsubkey_ctr_numkeys(ctr);
646         old_num_subkeys = regsubkey_ctr_numkeys(old_subkeys);
647         if ((num_subkeys && old_num_subkeys) &&
648             (num_subkeys == old_num_subkeys)) {
649
650                 for (i = 0; i < num_subkeys; i++) {
651                         if (strcmp(regsubkey_ctr_specific_key(ctr, i),
652                                    regsubkey_ctr_specific_key(old_subkeys, i))
653                             != 0)
654                         {
655                                 break;
656                         }
657                 }
658                 if (i == num_subkeys) {
659                         /*
660                          * Nothing changed, no point to even start a tdb
661                          * transaction
662                          */
663                         TALLOC_FREE(old_subkeys);
664                         return true;
665                 }
666         }
667
668         TALLOC_FREE(old_subkeys);
669
670         if (regdb->transaction_start(regdb) != 0) {
671                 DEBUG(0, ("regdb_store_keys: transaction_start failed\n"));
672                 goto fail;
673         }
674
675         /*
676          * Re-fetch the old keys inside the transaction
677          */
678
679         if (!(old_subkeys = TALLOC_ZERO_P(ctx, struct regsubkey_ctr))) {
680                 DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
681                 goto cancel;
682         }
683
684         regdb_fetch_keys(key, old_subkeys);
685
686         /*
687          * Make the store operation as safe as possible without transactions:
688          *
689          * (1) For each subkey removed from ctr compared with old_subkeys:
690          *
691          *     (a) First delete the value db entry.
692          *
693          *     (b) Next delete the secdesc db record.
694          *
695          *     (c) Then delete the subkey list entry.
696          *
697          * (2) Now write the list of subkeys of the parent key,
698          *     deleting removed entries and adding new ones.
699          *
700          * (3) Finally create the subkey list entries for the added keys.
701          *
702          * This way if we crash half-way in between deleting the subkeys
703          * and storing the parent's list of subkeys, no old data can pop up
704          * out of the blue when re-adding keys later on.
705          */
706
707         /* (1) delete removed keys' lists (values/secdesc/subkeys) */
708
709         num_subkeys = regsubkey_ctr_numkeys(old_subkeys);
710         for (i=0; i<num_subkeys; i++) {
711                 oldkeyname = regsubkey_ctr_specific_key(old_subkeys, i);
712
713                 if (regsubkey_ctr_key_exists(ctr, oldkeyname)) {
714                         /*
715                          * It's still around, don't delete
716                          */
717
718                         continue;
719                 }
720
721                 /* (a) Delete the value list for this key */
722
723                 path = talloc_asprintf(ctx, "%s/%s/%s",
724                                 REG_VALUE_PREFIX,
725                                 key,
726                                 oldkeyname );
727                 if (!path) {
728                         goto cancel;
729                 }
730                 path = normalize_reg_path(ctx, path);
731                 if (!path) {
732                         goto cancel;
733                 }
734                 /* Ignore errors here, we might have no values around */
735                 dbwrap_delete_bystring(regdb, path);
736                 TALLOC_FREE(path);
737
738                 /* (b) Delete the secdesc for this key */
739
740                 path = talloc_asprintf(ctx, "%s/%s/%s",
741                                 REG_SECDESC_PREFIX,
742                                 key,
743                                 oldkeyname );
744                 if (!path) {
745                         goto cancel;
746                 }
747                 path = normalize_reg_path(ctx, path);
748                 if (!path) {
749                         goto cancel;
750                 }
751                 status = dbwrap_delete_bystring(regdb, path);
752                 /* Don't fail if there are no values around. */
753                 if (!NT_STATUS_IS_OK(status) &&
754                     !NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND))
755                 {
756                         DEBUG(1, ("Deleting %s failed: %s\n", path,
757                                   nt_errstr(status)));
758                         goto cancel;
759                 }
760                 TALLOC_FREE(path);
761
762                 /* (c) Delete the list of subkeys of this key */
763
764                 path = talloc_asprintf(ctx, "%s/%s", key, oldkeyname);
765                 if (!path) {
766                         goto cancel;
767                 }
768                 path = normalize_reg_path(ctx, path);
769                 if (!path) {
770                         goto cancel;
771                 }
772                 status = dbwrap_delete_bystring(regdb, path);
773                 /* Don't fail if the subkey record was not found. */
774                 if (!NT_STATUS_IS_OK(status) &&
775                     !NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND))
776                 {
777                         DEBUG(1, ("Deleting %s failed: %s\n", path,
778                                   nt_errstr(status)));
779                         goto cancel;
780                 }
781                 TALLOC_FREE(path);
782         }
783
784         TALLOC_FREE(old_subkeys);
785
786         /* (2) store the subkey list for the parent */
787
788         if (!regdb_store_keys_internal(key, ctr) ) {
789                 DEBUG(0,("regdb_store_keys: Failed to store new subkey list "
790                          "for parent [%s]\n", key));
791                 goto cancel;
792         }
793
794         /* (3) now create records for any subkeys that don't already exist */
795
796         num_subkeys = regsubkey_ctr_numkeys(ctr);
797
798         if (num_subkeys == 0) {
799                 if (!(subkeys = TALLOC_ZERO_P(ctx, struct regsubkey_ctr)) ) {
800                         DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
801                         goto cancel;
802                 }
803
804                 if (!regdb_store_keys_internal(key, subkeys)) {
805                         DEBUG(0,("regdb_store_keys: Failed to store "
806                                  "new record for key [%s]\n", key));
807                         goto cancel;
808                 }
809                 TALLOC_FREE(subkeys);
810
811         }
812
813         for (i=0; i<num_subkeys; i++) {
814                 path = talloc_asprintf(ctx, "%s/%s",
815                                         key,
816                                         regsubkey_ctr_specific_key(ctr, i));
817                 if (!path) {
818                         goto cancel;
819                 }
820                 if (!(subkeys = TALLOC_ZERO_P(ctx, struct regsubkey_ctr)) ) {
821                         DEBUG(0,("regdb_store_keys: talloc() failure!\n"));
822                         goto cancel;
823                 }
824
825                 if (regdb_fetch_keys( path, subkeys ) == -1) {
826                         /* create a record with 0 subkeys */
827                         if (!regdb_store_keys_internal(path, subkeys)) {
828                                 DEBUG(0,("regdb_store_keys: Failed to store "
829                                          "new record for key [%s]\n", path));
830                                 goto cancel;
831                         }
832                 }
833
834                 TALLOC_FREE(subkeys);
835                 TALLOC_FREE(path);
836         }
837
838         if (regdb->transaction_commit(regdb) != 0) {
839                 DEBUG(0, ("regdb_store_keys: Could not commit transaction\n"));
840                 goto fail;
841         }
842
843         TALLOC_FREE(ctx);
844         return true;
845
846 cancel:
847         if (regdb->transaction_cancel(regdb) != 0) {
848                 smb_panic("regdb_store_keys: transaction_cancel failed\n");
849         }
850
851 fail:
852         TALLOC_FREE(ctx);
853
854         return false;
855 }
856
857
858 static TDB_DATA regdb_fetch_key_internal(TALLOC_CTX *mem_ctx, const char *key)
859 {
860         char *path = NULL;
861         TDB_DATA data;
862
863         path = normalize_reg_path(mem_ctx, key);
864         if (!path) {
865                 return make_tdb_data(NULL, 0);
866         }
867
868         data = dbwrap_fetch_bystring(regdb, mem_ctx, path);
869
870         TALLOC_FREE(path);
871         return data;
872 }
873
874
875 /**
876  * check whether a given key name represents a base key,
877  * i.e one without a subkey separator ('/' or '\').
878  */
879 static bool regdb_key_is_base_key(const char *key)
880 {
881         TALLOC_CTX *mem_ctx = talloc_stackframe();
882         bool ret = false;
883         char *path;
884
885         if (key == NULL) {
886                 goto done;
887         }
888
889         path = normalize_reg_path(mem_ctx, key);
890         if (path == NULL) {
891                 DEBUG(0, ("out of memory! (talloc failed)\n"));
892                 goto done;
893         }
894
895         if (*path == '\0') {
896                 goto done;
897         }
898
899         ret = (strrchr(path, '/') == NULL);
900
901 done:
902         TALLOC_FREE(mem_ctx);
903         return ret;
904 }
905
906 /*
907  * regdb_key_exists() is a very frequent operation. It can be quite
908  * time-consuming to fully fetch the parent's subkey list, talloc_strdup all
909  * subkeys and then compare the keyname linearly to all the parent's subkeys.
910  *
911  * The following code tries to make this operation as efficient as possible:
912  * Per registry key we create a list of subkeys that is very efficient to
913  * search for existence of a subkey. Its format is:
914  *
915  * 4 bytes num_subkeys
916  * 4*num_subkey bytes offset into the string array
917  * then follows a sorted list of subkeys in uppercase
918  *
919  * This record is created by create_sorted_subkeys() on demand if it does not
920  * exist. scan_parent_subkeys() uses regdb->parse_record to search the sorted
921  * list, the parsing code and the binary search can be found in
922  * parent_subkey_scanner. The code uses parse_record() to avoid a memcpy of
923  * the potentially large subkey record.
924  *
925  * The sorted subkey record is deleted in regdb_store_keys_internal and
926  * recreated on demand.
927  */
928
929 static int cmp_keynames(const void *p1, const void *p2)
930 {
931         return StrCaseCmp(*((char **)p1), *((char **)p2));
932 }
933
934 static bool create_sorted_subkeys(const char *key, const char *sorted_keyname)
935 {
936         char **sorted_subkeys;
937         struct regsubkey_ctr *ctr;
938         bool result = false;
939         NTSTATUS status;
940         char *buf;
941         char *p;
942         int i, res;
943         size_t len;
944         int num_subkeys;
945
946         if (regdb->transaction_start(regdb) != 0) {
947                 DEBUG(0, ("create_sorted_subkeys: transaction_start "
948                           "failed\n"));
949                 return false;
950         }
951
952         ctr = talloc(talloc_tos(), struct regsubkey_ctr);
953         if (ctr == NULL) {
954                 goto fail;
955         }
956
957         res = regdb_fetch_keys(key, ctr);
958         if (res == -1) {
959                 goto fail;
960         }
961
962         num_subkeys = regsubkey_ctr_numkeys(ctr);
963         sorted_subkeys = talloc_array(ctr, char *, num_subkeys);
964         if (sorted_subkeys == NULL) {
965                 goto fail;
966         }
967
968         len = 4 + 4*num_subkeys;
969
970         for (i = 0; i < num_subkeys; i++) {
971                 sorted_subkeys[i] = talloc_strdup_upper(sorted_subkeys,
972                                         regsubkey_ctr_specific_key(ctr, i));
973                 if (sorted_subkeys[i] == NULL) {
974                         goto fail;
975                 }
976                 len += strlen(sorted_subkeys[i])+1;
977         }
978
979         qsort(sorted_subkeys, num_subkeys, sizeof(char *), cmp_keynames);
980
981         buf = talloc_array(ctr, char, len);
982         if (buf == NULL) {
983                 goto fail;
984         }
985         p = buf + 4 + 4*num_subkeys;
986
987         SIVAL(buf, 0, num_subkeys);
988
989         for (i=0; i < num_subkeys; i++) {
990                 ptrdiff_t offset = p - buf;
991                 SIVAL(buf, 4 + 4*i, offset);
992                 strlcpy(p, sorted_subkeys[i], len-offset);
993                 p += strlen(sorted_subkeys[i]) + 1;
994         }
995
996         status = dbwrap_store_bystring(
997                 regdb, sorted_keyname, make_tdb_data((uint8_t *)buf, len),
998                 TDB_REPLACE);
999         if (!NT_STATUS_IS_OK(status)) {
1000                 /*
1001                  * Don't use a "goto fail;" here, this would commit the broken
1002                  * transaction. See below for an explanation.
1003                  */
1004                 if (regdb->transaction_cancel(regdb) == -1) {
1005                         DEBUG(0, ("create_sorted_subkeys: transaction_cancel "
1006                                   "failed\n"));
1007                 }
1008                 TALLOC_FREE(ctr);
1009                 return false;
1010         }
1011
1012         result = true;
1013  fail:
1014         /*
1015          * We only get here via the "goto fail" when we did not write anything
1016          * yet. Using transaction_commit even in a failure case is necessary
1017          * because this (disposable) call might be nested in other
1018          * transactions. Doing a cancel here would destroy the possibility of
1019          * a transaction_commit for transactions that we might be wrapped in.
1020          */
1021         if (regdb->transaction_commit(regdb) == -1) {
1022                 DEBUG(0, ("create_sorted_subkeys: transaction_start "
1023                           "failed\n"));
1024                 goto fail;
1025         }
1026
1027         TALLOC_FREE(ctr);
1028         return result;
1029 }
1030
1031 struct scan_subkey_state {
1032         char *name;
1033         bool scanned;
1034         bool found;
1035 };
1036
1037 static int parent_subkey_scanner(TDB_DATA key, TDB_DATA data,
1038                                  void *private_data)
1039 {
1040         struct scan_subkey_state *state =
1041                 (struct scan_subkey_state *)private_data;
1042         uint32_t num_subkeys;
1043         uint32_t l, u;
1044
1045         if (data.dsize < sizeof(uint32_t)) {
1046                 return -1;
1047         }
1048
1049         state->scanned = true;
1050         state->found = false;
1051
1052         tdb_unpack(data.dptr, data.dsize, "d", &num_subkeys);
1053
1054         l = 0;
1055         u = num_subkeys;
1056
1057         while (l < u) {
1058                 uint32_t idx = (l+u)/2;
1059                 char *s = (char *)data.dptr + IVAL(data.dptr, 4 + 4*idx);
1060                 int comparison = strcmp(state->name, s);
1061
1062                 if (comparison < 0) {
1063                         u = idx;
1064                 } else if (comparison > 0) {
1065                         l = idx + 1;
1066                 } else {
1067                         state->found = true;
1068                         return 0;
1069                 }
1070         }
1071         return 0;
1072 }
1073
1074 static bool scan_parent_subkeys(const char *parent, const char *name)
1075 {
1076         char *path = NULL;
1077         char *key = NULL;
1078         struct scan_subkey_state state = { 0, };
1079         bool result = false;
1080         int res;
1081
1082         state.name = NULL;
1083
1084         path = normalize_reg_path(talloc_tos(), parent);
1085         if (path == NULL) {
1086                 goto fail;
1087         }
1088
1089         key = talloc_asprintf(talloc_tos(), "%s/%s",
1090                               REG_SORTED_SUBKEYS_PREFIX, path);
1091         if (key == NULL) {
1092                 goto fail;
1093         }
1094
1095         state.name = talloc_strdup_upper(talloc_tos(), name);
1096         if (state.name == NULL) {
1097                 goto fail;
1098         }
1099         state.scanned = false;
1100
1101         res = regdb->parse_record(regdb, string_term_tdb_data(key),
1102                                   parent_subkey_scanner, &state);
1103
1104         if (state.scanned) {
1105                 result = state.found;
1106         } else {
1107                 if (!create_sorted_subkeys(path, key)) {
1108                         goto fail;
1109                 }
1110                 res = regdb->parse_record(regdb, string_term_tdb_data(key),
1111                                           parent_subkey_scanner, &state);
1112                 if ((res == 0) && (state.scanned)) {
1113                         result = state.found;
1114                 }
1115         }
1116
1117  fail:
1118         TALLOC_FREE(path);
1119         TALLOC_FREE(state.name);
1120         return result;
1121 }
1122
1123 /**
1124  * Check for the existence of a key.
1125  *
1126  * Existence of a key is authoritatively defined by its
1127  * existence in the list of subkeys of its parent key.
1128  * The exeption of this are keys without a parent key,
1129  * i.e. the "base" keys (HKLM, HKCU, ...).
1130  */
1131 static bool regdb_key_exists(const char *key)
1132 {
1133         TALLOC_CTX *mem_ctx = talloc_stackframe();
1134         TDB_DATA value;
1135         bool ret = false;
1136         char *path, *p;
1137
1138         if (key == NULL) {
1139                 goto done;
1140         }
1141
1142         path = normalize_reg_path(mem_ctx, key);
1143         if (path == NULL) {
1144                 DEBUG(0, ("out of memory! (talloc failed)\n"));
1145                 goto done;
1146         }
1147
1148         if (*path == '\0') {
1149                 goto done;
1150         }
1151
1152         p = strrchr(path, '/');
1153         if (p == NULL) {
1154                 /* this is a base key */
1155                 value = regdb_fetch_key_internal(mem_ctx, path);
1156                 ret = (value.dptr != NULL);
1157         } else {
1158                 *p = '\0';
1159                 ret = scan_parent_subkeys(path, p+1);
1160         }
1161
1162 done:
1163         TALLOC_FREE(mem_ctx);
1164         return ret;
1165 }
1166
1167
1168 /***********************************************************************
1169  Retrieve an array of strings containing subkeys.  Memory should be
1170  released by the caller.
1171  ***********************************************************************/
1172
1173 int regdb_fetch_keys(const char *key, struct regsubkey_ctr *ctr)
1174 {
1175         WERROR werr;
1176         uint32 num_items;
1177         uint8 *buf;
1178         uint32 buflen, len;
1179         int i;
1180         fstring subkeyname;
1181         int ret = -1;
1182         TALLOC_CTX *frame = talloc_stackframe();
1183         TDB_DATA value;
1184
1185         DEBUG(11,("regdb_fetch_keys: Enter key => [%s]\n", key ? key : "NULL"));
1186
1187         if (!regdb_key_exists(key)) {
1188                 goto done;
1189         }
1190
1191         ctr->seqnum = regdb_get_seqnum();
1192
1193         value = regdb_fetch_key_internal(frame, key);
1194
1195         if (value.dptr == NULL) {
1196                 DEBUG(10, ("regdb_fetch_keys: no subkeys found for key [%s]\n",
1197                            key));
1198                 ret = 0;
1199                 goto done;
1200         }
1201
1202         buf = value.dptr;
1203         buflen = value.dsize;
1204         len = tdb_unpack( buf, buflen, "d", &num_items);
1205
1206         for (i=0; i<num_items; i++) {
1207                 len += tdb_unpack(buf+len, buflen-len, "f", subkeyname);
1208                 werr = regsubkey_ctr_addkey(ctr, subkeyname);
1209                 if (!W_ERROR_IS_OK(werr)) {
1210                         DEBUG(5, ("regdb_fetch_keys: regsubkey_ctr_addkey "
1211                                   "failed: %s\n", win_errstr(werr)));
1212                         goto done;
1213                 }
1214         }
1215
1216         DEBUG(11,("regdb_fetch_keys: Exit [%d] items\n", num_items));
1217
1218         ret = num_items;
1219 done:
1220         TALLOC_FREE(frame);
1221         return ret;
1222 }
1223
1224 /****************************************************************************
1225  Unpack a list of registry values frem the TDB
1226  ***************************************************************************/
1227
1228 static int regdb_unpack_values(REGVAL_CTR *values, uint8 *buf, int buflen)
1229 {
1230         int             len = 0;
1231         uint32          type;
1232         fstring valuename;
1233         uint32          size;
1234         uint8           *data_p;
1235         uint32          num_values = 0;
1236         int             i;
1237
1238         /* loop and unpack the rest of the registry values */
1239
1240         len += tdb_unpack(buf+len, buflen-len, "d", &num_values);
1241
1242         for ( i=0; i<num_values; i++ ) {
1243                 /* unpack the next regval */
1244
1245                 type = REG_NONE;
1246                 size = 0;
1247                 data_p = NULL;
1248                 valuename[0] = '\0';
1249                 len += tdb_unpack(buf+len, buflen-len, "fdB",
1250                                   valuename,
1251                                   &type,
1252                                   &size,
1253                                   &data_p);
1254
1255                 /* add the new value. Paranoid protective code -- make sure data_p is valid */
1256
1257                 if (*valuename && size && data_p) {
1258                         regval_ctr_addvalue(values, valuename, type,
1259                                         (const char *)data_p, size);
1260                 }
1261                 SAFE_FREE(data_p); /* 'B' option to tdb_unpack does a malloc() */
1262
1263                 DEBUG(8,("specific: [%s], len: %d\n", valuename, size));
1264         }
1265
1266         return len;
1267 }
1268
1269 /****************************************************************************
1270  Pack all values in all printer keys
1271  ***************************************************************************/
1272
1273 static int regdb_pack_values(REGVAL_CTR *values, uint8 *buf, int buflen)
1274 {
1275         int             len = 0;
1276         int             i;
1277         REGISTRY_VALUE  *val;
1278         int             num_values;
1279
1280         if ( !values )
1281                 return 0;
1282
1283         num_values = regval_ctr_numvals( values );
1284
1285         /* pack the number of values first */
1286
1287         len += tdb_pack( buf+len, buflen-len, "d", num_values );
1288
1289         /* loop over all values */
1290
1291         for ( i=0; i<num_values; i++ ) {
1292                 val = regval_ctr_specific_value( values, i );
1293                 len += tdb_pack(buf+len, buflen-len, "fdB",
1294                                 regval_name(val),
1295                                 regval_type(val),
1296                                 regval_size(val),
1297                                 regval_data_p(val) );
1298         }
1299
1300         return len;
1301 }
1302
1303 /***********************************************************************
1304  Retrieve an array of strings containing subkeys.  Memory should be
1305  released by the caller.
1306  ***********************************************************************/
1307
1308 int regdb_fetch_values( const char* key, REGVAL_CTR *values )
1309 {
1310         char *keystr = NULL;
1311         TALLOC_CTX *ctx = talloc_stackframe();
1312         int ret = 0;
1313         TDB_DATA value;
1314
1315         DEBUG(10,("regdb_fetch_values: Looking for value of key [%s] \n", key));
1316
1317         if (!regdb_key_exists(key)) {
1318                 goto done;
1319         }
1320
1321         keystr = talloc_asprintf(ctx, "%s/%s", REG_VALUE_PREFIX, key);
1322         if (!keystr) {
1323                 goto done;
1324         }
1325
1326         values->seqnum = regdb_get_seqnum();
1327
1328         value = regdb_fetch_key_internal(ctx, keystr);
1329
1330         if (!value.dptr) {
1331                 /* all keys have zero values by default */
1332                 goto done;
1333         }
1334
1335         regdb_unpack_values(values, value.dptr, value.dsize);
1336         ret = regval_ctr_numvals(values);
1337
1338 done:
1339         TALLOC_FREE(ctx);
1340         return ret;
1341 }
1342
1343 bool regdb_store_values( const char *key, REGVAL_CTR *values )
1344 {
1345         TDB_DATA old_data, data;
1346         char *keystr = NULL;
1347         TALLOC_CTX *ctx = talloc_stackframe();
1348         int len;
1349         NTSTATUS status;
1350         bool result = false;
1351
1352         DEBUG(10,("regdb_store_values: Looking for value of key [%s] \n", key));
1353
1354         if (!regdb_key_exists(key)) {
1355                 goto done;
1356         }
1357
1358         ZERO_STRUCT(data);
1359
1360         len = regdb_pack_values(values, data.dptr, data.dsize);
1361         if (len <= 0) {
1362                 DEBUG(0,("regdb_store_values: unable to pack values. len <= 0\n"));
1363                 goto done;
1364         }
1365
1366         data.dptr = TALLOC_ARRAY(ctx, uint8, len);
1367         data.dsize = len;
1368
1369         len = regdb_pack_values(values, data.dptr, data.dsize);
1370
1371         SMB_ASSERT( len == data.dsize );
1372
1373         keystr = talloc_asprintf(ctx, "%s/%s", REG_VALUE_PREFIX, key );
1374         if (!keystr) {
1375                 goto done;
1376         }
1377         keystr = normalize_reg_path(ctx, keystr);
1378         if (!keystr) {
1379                 goto done;
1380         }
1381
1382         old_data = dbwrap_fetch_bystring(regdb, ctx, keystr);
1383
1384         if ((old_data.dptr != NULL)
1385             && (old_data.dsize == data.dsize)
1386             && (memcmp(old_data.dptr, data.dptr, data.dsize) == 0))
1387         {
1388                 result = true;
1389                 goto done;
1390         }
1391
1392         status = dbwrap_trans_store_bystring(regdb, keystr, data, TDB_REPLACE);
1393
1394         result = NT_STATUS_IS_OK(status);
1395
1396 done:
1397         TALLOC_FREE(ctx);
1398         return result;
1399 }
1400
1401 static WERROR regdb_get_secdesc(TALLOC_CTX *mem_ctx, const char *key,
1402                                 struct security_descriptor **psecdesc)
1403 {
1404         char *tdbkey;
1405         TDB_DATA data;
1406         NTSTATUS status;
1407         TALLOC_CTX *tmp_ctx = talloc_stackframe();
1408         WERROR err = WERR_OK;
1409
1410         DEBUG(10, ("regdb_get_secdesc: Getting secdesc of key [%s]\n", key));
1411
1412         if (!regdb_key_exists(key)) {
1413                 err = WERR_BADFILE;
1414                 goto done;
1415         }
1416
1417         tdbkey = talloc_asprintf(tmp_ctx, "%s/%s", REG_SECDESC_PREFIX, key);
1418         if (tdbkey == NULL) {
1419                 err = WERR_NOMEM;
1420                 goto done;
1421         }
1422         normalize_dbkey(tdbkey);
1423
1424         data = dbwrap_fetch_bystring(regdb, tmp_ctx, tdbkey);
1425         if (data.dptr == NULL) {
1426                 err = WERR_BADFILE;
1427                 goto done;
1428         }
1429
1430         status = unmarshall_sec_desc(mem_ctx, (uint8 *)data.dptr, data.dsize,
1431                                      psecdesc);
1432
1433         if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MEMORY)) {
1434                 err = WERR_NOMEM;
1435         } else if (!NT_STATUS_IS_OK(status)) {
1436                 err = WERR_REG_CORRUPT;
1437         }
1438
1439 done:
1440         TALLOC_FREE(tmp_ctx);
1441         return err;
1442 }
1443
1444 static WERROR regdb_set_secdesc(const char *key,
1445                                 struct security_descriptor *secdesc)
1446 {
1447         TALLOC_CTX *mem_ctx = talloc_stackframe();
1448         char *tdbkey;
1449         NTSTATUS status;
1450         WERROR err = WERR_NOMEM;
1451         TDB_DATA tdbdata;
1452
1453         if (!regdb_key_exists(key)) {
1454                 err = WERR_BADFILE;
1455                 goto done;
1456         }
1457
1458         tdbkey = talloc_asprintf(mem_ctx, "%s/%s", REG_SECDESC_PREFIX, key);
1459         if (tdbkey == NULL) {
1460                 goto done;
1461         }
1462         normalize_dbkey(tdbkey);
1463
1464         if (secdesc == NULL) {
1465                 /* assuming a delete */
1466                 status = dbwrap_trans_delete_bystring(regdb, tdbkey);
1467                 if (NT_STATUS_IS_OK(status)) {
1468                         err = WERR_OK;
1469                 } else {
1470                         err = ntstatus_to_werror(status);
1471                 }
1472                 goto done;
1473         }
1474
1475         err = ntstatus_to_werror(marshall_sec_desc(mem_ctx, secdesc,
1476                                                    &tdbdata.dptr,
1477                                                    &tdbdata.dsize));
1478         if (!W_ERROR_IS_OK(err)) {
1479                 goto done;
1480         }
1481
1482         status = dbwrap_trans_store_bystring(regdb, tdbkey, tdbdata, 0);
1483         if (!NT_STATUS_IS_OK(status)) {
1484                 err = ntstatus_to_werror(status);
1485                 goto done;
1486         }
1487
1488  done:
1489         TALLOC_FREE(mem_ctx);
1490         return err;
1491 }
1492
1493 bool regdb_subkeys_need_update(struct regsubkey_ctr *subkeys)
1494 {
1495         return (regdb_get_seqnum() != subkeys->seqnum);
1496 }
1497
1498 bool regdb_values_need_update(REGVAL_CTR *values)
1499 {
1500         return (regdb_get_seqnum() != values->seqnum);
1501 }
1502
1503 /* 
1504  * Table of function pointers for default access
1505  */
1506  
1507 REGISTRY_OPS regdb_ops = {
1508         .fetch_subkeys = regdb_fetch_keys,
1509         .fetch_values = regdb_fetch_values,
1510         .store_subkeys = regdb_store_keys,
1511         .store_values = regdb_store_values,
1512         .get_secdesc = regdb_get_secdesc,
1513         .set_secdesc = regdb_set_secdesc,
1514         .subkeys_need_update = regdb_subkeys_need_update,
1515         .values_need_update = regdb_values_need_update
1516 };