summaryrefslogtreecommitdiffstats
path: root/g10/tdbio.c
blob: fdb887132df0a3e71abb06f14397c4f06d31e891 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
/* tdbio.c - trust database I/O operations
 * Copyright (C) 1998-2002, 2012 Free Software Foundation, Inc.
 * Copyright (C) 1998-2015 Werner Koch
 *
 * This file is part of GnuPG.
 *
 * GnuPG is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * GnuPG is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses/>.
 */

#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#include "gpg.h"
#include "../common/status.h"
#include "../common/iobuf.h"
#include "../common/util.h"
#include "options.h"
#include "main.h"
#include "../common/i18n.h"
#include "trustdb.h"
#include "tdbio.h"

#if defined(HAVE_DOSISH_SYSTEM) && !defined(ftruncate)
#define ftruncate chsize
#endif

#if defined(HAVE_DOSISH_SYSTEM) || defined(__CYGWIN__)
#define MY_O_BINARY  O_BINARY
#else
#define MY_O_BINARY  0
#endif

/* We use ERRNO despite that the cegcc provided open/read/write
   functions don't set ERRNO - at least show that ERRNO does not make
   sense.  */
#ifdef HAVE_W32CE_SYSTEM
#undef strerror
#define strerror(a) ("[errno not available]")
#endif

/*
 * Yes, this is a very simple implementation. We should really
 * use a page aligned buffer and read complete pages.
 * To implement a simple trannsaction system, this is sufficient.
 */
typedef struct cache_ctrl_struct *CACHE_CTRL;
struct cache_ctrl_struct
{
  CACHE_CTRL next;
  struct {
    unsigned used:1;
    unsigned dirty:1;
  } flags;
  ulong recno;
  char data[TRUST_RECORD_LEN];
};

/* Size of the cache.  The SOFT value is the general one.  While in a
   transaction this may not be sufficient and thus we may increase it
   then up to the HARD limit.  */
#define MAX_CACHE_ENTRIES_SOFT	200
#define MAX_CACHE_ENTRIES_HARD	10000


/* The cache is controlled by these variables.  */
static CACHE_CTRL cache_list;
static int cache_entries;
static int cache_is_dirty;


/* An object to pass information to cmp_krec_fpr. */
struct cmp_krec_fpr_struct
{
  int pubkey_algo;
  const char *fpr;
  int fprlen;
};

/* An object used to pass information to cmp_[s]dir. */
struct cmp_xdir_struct
{
  int pubkey_algo;
  u32 keyid[2];
};


/* The name of the trustdb file.  */
static char *db_name;

/* The handle for locking the trustdb file and a counter to record how
 * often this lock has been taken.  That counter is required becuase
 * dotlock does not implemen recursive locks.  */
static dotlock_t lockhandle;
static unsigned int is_locked;

/* The file descriptor of the trustdb.  */
static int  db_fd = -1;

/* A flag indicating that a transaction is active.  */
/* static int in_transaction;   Not yet used. */



static void open_db (void);
static void create_hashtable (ctrl_t ctrl, TRUSTREC *vr, int type);



/*
 * Take a lock on the trustdb file name.  I a lock file can't be
 * created the function terminates the process.  Except for a
 * different return code the function does nothing if the lock has
 * already been taken.
 *
 * Returns: True if lock already exists, False if the lock has
 *          actually been taken.
 */
static int
take_write_lock (void)
{
  int rc;

  if (!lockhandle)
    lockhandle = dotlock_create (db_name, 0);
  if (!lockhandle)
    log_fatal ( _("can't create lock for '%s'\n"), db_name );

  if (!is_locked)
    {
      if (dotlock_take (lockhandle, -1) )
        log_fatal ( _("can't lock '%s'\n"), db_name );
      rc = 0;
    }
  else
    rc = 1;

  if (opt.lock_once)
    is_locked = 1;
  else
    is_locked++;
  return rc;
}


/*
 * Release a lock from the trustdb file unless the global option
 * --lock-once has been used.
 */
static void
release_write_lock (void)
{
  if (opt.lock_once)
    return;  /* Don't care; here IS_LOCKED is fixed to 1.  */

  if (!is_locked)
    {
      log_error ("Ooops, tdbio:release_write_lock with no lock held\n");
      return;
    }
  if (--is_locked)
    return;

  if (dotlock_release (lockhandle))
    log_error ("Oops, tdbio:release_write_locked failed\n");
}



/*************************************
 ************* record cache **********
 *************************************/

/*
 * Get the data from the record cache and return a pointer into that
 * cache.  Caller should copy the returned data.  NULL is returned on
 * a cache miss.
 */
static const char *
get_record_from_cache (ulong recno)
{
  CACHE_CTRL r;

  for (r = cache_list; r; r = r->next)
    {
      if (r->flags.used && r->recno == recno)
        return r->data;
    }
  return NULL;
}


/*
 * Write a cached item back to the trustdb file.
 *
 * Returns: 0 on success or an error code.
 */
static int
write_cache_item (CACHE_CTRL r)
{
  gpg_error_t err;
  int n;

  if (lseek (db_fd, r->recno * TRUST_RECORD_LEN, SEEK_SET) == -1)
    {
      err = gpg_error_from_syserror ();
      log_error (_("trustdb rec %lu: lseek failed: %s\n"),
                 r->recno, strerror (errno));
      return err;
    }
  n = write (db_fd, r->data, TRUST_RECORD_LEN);
  if (n != TRUST_RECORD_LEN)
    {
      err = gpg_error_from_syserror ();
      log_error (_("trustdb rec %lu: write failed (n=%d): %s\n"),
                 r->recno, n, strerror (errno) );
      return err;
    }
  r->flags.dirty = 0;
  return 0;
}


/*
 * Put data into the cache.  This function may flush
 * some cache entries if the cache is filled up.
 *
 * Returns: 0 on success or an error code.
 */
static int
put_record_into_cache (ulong recno, const char *data)
{
  CACHE_CTRL r, unused;
  int dirty_count = 0;
  int clean_count = 0;

  /* See whether we already cached this one.  */
  for (unused = NULL, r = cache_list; r; r = r->next)
    {
      if (!r->flags.used)
        {
          if (!unused)
            unused = r;
	}
      else if (r->recno == recno)
        {
          if (!r->flags.dirty)
            {
              /* Hmmm: should we use a copy and compare? */
              if (memcmp (r->data, data, TRUST_RECORD_LEN))
                {
                  r->flags.dirty = 1;
                  cache_is_dirty = 1;
		}
	    }
          memcpy (r->data, data, TRUST_RECORD_LEN);
          return 0;
	}
      if (r->flags.used)
        {
          if (r->flags.dirty)
            dirty_count++;
          else
            clean_count++;
	}
    }

  /* Not in the cache: add a new entry. */
  if (unused)
    {
      /* Reuse this entry. */
      r = unused;
      r->flags.used = 1;
      r->recno = recno;
      memcpy (r->data, data, TRUST_RECORD_LEN);
      r->flags.dirty = 1;
      cache_is_dirty = 1;
      cache_entries++;
      return 0;
    }

  /* See whether we reached the limit. */
  if (cache_entries < MAX_CACHE_ENTRIES_SOFT)
    {
      /* No: Put into cache.  */
      r = xmalloc (sizeof *r);
      r->flags.used = 1;
      r->recno = recno;
      memcpy (r->data, data, TRUST_RECORD_LEN);
      r->flags.dirty = 1;
      r->next = cache_list;
      cache_list = r;
      cache_is_dirty = 1;
      cache_entries++;
      return 0;
    }

  /* Cache is full: discard some clean entries.  */
  if (clean_count)
    {
      int n;

      /* We discard a third of the clean entries.  */
      n = clean_count / 3;
      if (!n)
        n = 1;

      for (unused = NULL, r = cache_list; r; r = r->next)
        {
          if (r->flags.used && !r->flags.dirty)
            {
              if (!unused)
                unused = r;
              r->flags.used = 0;
              cache_entries--;
              if (!--n)
                break;
	    }
	}

      /* Now put into the cache.  */
      log_assert (unused);
      r = unused;
      r->flags.used = 1;
      r->recno = recno;
      memcpy (r->data, data, TRUST_RECORD_LEN);
      r->flags.dirty = 1;
      cache_is_dirty = 1;
      cache_entries++;
      return 0;
    }

  /* No clean entries: We have to flush some dirty entries.  */
#if 0 /* Transactions are not yet used.  */
  if (in_transaction)
    {
      /* But we can't do this while in a transaction.  Thus we
       * increase the cache size instead.  */
      if (cache_entries < MAX_CACHE_ENTRIES_HARD)
        {
          if (opt.debug && !(cache_entries % 100))
            log_debug ("increasing tdbio cache size\n");
          r = xmalloc (sizeof *r);
          r->flags.used = 1;
          r->recno = recno;
          memcpy (r->data, data, TRUST_RECORD_LEN);
          r->flags.dirty = 1;
          r->next = cache_list;
          cache_list = r;
          cache_is_dirty = 1;
          cache_entries++;
          return 0;
	}
      /* Hard limit for the cache size reached.  */
      log_info (_("trustdb transaction too large\n"));
      return GPG_ERR_RESOURCE_LIMIT;
    }
#endif

  if (dirty_count)
    {
      int n;

      /* Discard some dirty entries. */
      n = dirty_count / 5;
      if (!n)
        n = 1;

      take_write_lock ();
      for (unused = NULL, r = cache_list; r; r = r->next)
        {
          if (r->flags.used && r->flags.dirty)
            {
              int rc;

              rc = write_cache_item (r);
              if (rc)
                return rc;
              if (!unused)
                unused = r;
              r->flags.used = 0;
              cache_entries--;
              if (!--n)
                break;
	    }
	}
      release_write_lock ();

      /* Now put into the cache.  */
      log_assert (unused);
      r = unused;
      r->flags.used = 1;
      r->recno = recno;
      memcpy (r->data, data, TRUST_RECORD_LEN);
      r->flags.dirty = 1;
      cache_is_dirty = 1;
      cache_entries++;
      return 0;
    }

  /* We should never reach this.  */
  BUG();
}


/* Return true if the cache is dirty.  */
int
tdbio_is_dirty()
{
  return cache_is_dirty;
}


/*
 * Flush the cache.  This cannot be used while in a transaction.
 */
int
tdbio_sync()
{
    CACHE_CTRL r;
    int did_lock = 0;

    if( db_fd == -1 )
	open_db();
#if 0 /* Transactions are not yet used.  */
    if( in_transaction )
	log_bug("tdbio: syncing while in transaction\n");
#endif

    if( !cache_is_dirty )
	return 0;

    if (!take_write_lock ())
        did_lock = 1;

    for( r = cache_list; r; r = r->next ) {
	if( r->flags.used && r->flags.dirty ) {
	    int rc = write_cache_item( r );
	    if( rc )
		return rc;
	}
    }
    cache_is_dirty = 0;
    if (did_lock)
        release_write_lock ();

    return 0;
}


#if 0  /* Not yet used.  */
/*
 * Simple transactions system:
 * Everything between begin_transaction and end/cancel_transaction
 * is not immediately written but at the time of end_transaction.
 *
 * NOTE: The transaction code is disabled in the 1.2 branch, as it is
 * not yet used.
 */
int
tdbio_begin_transaction ()  /* Not yet used.  */
{
  int rc;

  if (in_transaction)
    log_bug ("tdbio: nested transactions\n");
  /* Flush everything out. */
  rc = tdbio_sync();
  if (rc)
    return rc;
  in_transaction = 1;
  return 0;
}

int
tdbio_end_transaction ()  /* Not yet used.  */
{
  int rc;

  if (!in_transaction)
    log_bug ("tdbio: no active transaction\n");
  take_write_lock ();
  gnupg_block_all_signals ();
  in_transaction = 0;
  rc = tdbio_sync();
  gnupg_unblock_all_signals();
  release_write_lock ();
  return rc;
}

int
tdbio_cancel_transaction () /* Not yet used.  */
{
  CACHE_CTRL r;

  if (!in_transaction)
    log_bug ("tdbio: no active transaction\n");

  /* Remove all dirty marked entries, so that the original ones are
   * read back the next time.  */
  if (cache_is_dirty)
    {
      for (r = cache_list; r; r = r->next)
        {
          if (r->flags.used && r->flags.dirty)
            {
              r->flags.used = 0;
              cache_entries--;
	    }
	}
      cache_is_dirty = 0;
    }

  in_transaction = 0;
  return 0;
}
#endif  /* Not yet used.  */



/********************************************************
 **************** cached I/O functions ******************
 ********************************************************/

/* The cleanup handler for this module.  */
static void
cleanup (void)
{
  if (is_locked)
    {
      if (!dotlock_release (lockhandle))
        is_locked = 0;
    }
}


/*
 * Update an existing trustdb record.  The caller must call
 * tdbio_sync.
 *
 * Returns: 0 on success or an error code.
 */
int
tdbio_update_version_record (ctrl_t ctrl)
{
  TRUSTREC rec;
  int rc;
  int opt_tm;

  /* Never store a TOFU trust model in the trustdb.  Use PGP instead.  */
  opt_tm = opt.trust_model;
  if (opt_tm == TM_TOFU || opt_tm == TM_TOFU_PGP)
    opt_tm = TM_PGP;

  memset (&rec, 0, sizeof rec);

  rc = tdbio_read_record (0, &rec, RECTYPE_VER);
  if (!rc)
    {
      rec.r.ver.created     = make_timestamp();
      rec.r.ver.marginals   = opt.marginals_needed;
      rec.r.ver.completes   = opt.completes_needed;
      rec.r.ver.cert_depth  = opt.max_cert_depth;
      rec.r.ver.trust_model = opt_tm;
      rec.r.ver.min_cert_level = opt.min_cert_level;
      rc = tdbio_write_record (ctrl, &rec);
    }

  return rc;
}


/*
 * Create and write the trustdb version record.
 * This is called with the writelock activ.
 * Returns: 0 on success or an error code.
 */
static int
create_version_record (ctrl_t ctrl)
{
  TRUSTREC rec;
  int rc;
  int opt_tm;

  /* Never store a TOFU trust model in the trustdb.  Use PGP instead.  */
  opt_tm = opt.trust_model;
  if (opt_tm == TM_TOFU || opt_tm == TM_TOFU_PGP)
    opt_tm = TM_PGP;

  memset (&rec, 0, sizeof rec);
  rec.r.ver.version     = 3;
  rec.r.ver.created     = make_timestamp ();
  rec.r.ver.marginals   = opt.marginals_needed;
  rec.r.ver.completes   = opt.completes_needed;
  rec.r.ver.cert_depth  = opt.max_cert_depth;
  if (opt_tm == TM_PGP || opt_tm == TM_CLASSIC)
    rec.r.ver.trust_model = opt_tm;
  else
    rec.r.ver.trust_model = TM_PGP;
  rec.r.ver.min_cert_level = opt.min_cert_level;
  rec.rectype = RECTYPE_VER;
  rec.recnum = 0;
  rc = tdbio_write_record (ctrl, &rec);

  if (!rc)
    tdbio_sync ();

  if (!rc)
    create_hashtable (ctrl, &rec, 0);

  return rc;
}


/*
 * Set the file name for the trustdb to NEW_DBNAME and if CREATE is
 * true create that file.  If NEW_DBNAME is NULL a default name is
 * used, if the it does not contain a path component separator ('/')
 * the global GnuPG home directory is used.
 *
 * Returns: 0 on success or an error code.
 *
 * On the first call this function registers an atexit handler.
 *
 */
int
tdbio_set_dbname (ctrl_t ctrl, const char *new_dbname,
                  int create, int *r_nofile)
{
  char *fname, *p;
  struct stat statbuf;
  static int initialized = 0;
  int save_slash;

  if (!initialized)
    {
      atexit (cleanup);
      initialized = 1;
    }

  *r_nofile = 0;

  if (!new_dbname)
    {
      fname = make_filename (gnupg_homedir (),
                             "trustdb" EXTSEP_S GPGEXT_GPG, NULL);
    }
  else if (*new_dbname != DIRSEP_C )
    {
      if (strchr (new_dbname, DIRSEP_C))
        fname = make_filename (new_dbname, NULL);
      else
        fname = make_filename (gnupg_homedir (), new_dbname, NULL);
    }
  else
    {
      fname = xstrdup (new_dbname);
    }

  xfree (db_name);
  db_name = fname;

  /* Quick check for (likely) case where there already is a
   * trustdb.gpg.  This check is not required in theory, but it helps
   * in practice avoiding costly operations of preparing and taking
   * the lock.  */
  if (!gnupg_stat (fname, &statbuf) && statbuf.st_size > 0)
    {
      /* OK, we have the valid trustdb.gpg already.  */
      return 0;
    }
  else if (!create)
    {
      *r_nofile = 1;
      return 0;
    }

  /* Here comes: No valid trustdb.gpg AND CREATE==1 */

  /*
   * Make sure the directory exists.  This should be done before
   * acquiring the lock, which assumes the existence of the directory.
   */
  p = strrchr (fname, DIRSEP_C);
#if HAVE_W32_SYSTEM
  {
    /* Windows may either have a slash or a backslash.  Take
       care of it.  */
    char *pp = strrchr (fname, '/');
    if (!p || pp > p)
      p = pp;
  }
#endif /*HAVE_W32_SYSTEM*/
  log_assert (p);
  save_slash = *p;
  *p = 0;
  if (gnupg_access (fname, F_OK))
    {
      try_make_homedir (fname);
      if (gnupg_access (fname, F_OK))
        log_fatal (_("%s: directory does not exist!\n"), fname);
    }
  *p = save_slash;

  take_write_lock ();

  if (gnupg_access (fname, R_OK)
      || gnupg_stat (fname, &statbuf)
      || statbuf.st_size == 0)
    {
      estream_t fp;
      TRUSTREC rec;
      int rc;
      mode_t oldmask;

#ifdef HAVE_W32CE_SYSTEM
      /* We know how the cegcc implementation of access works ;-). */
      if (GetLastError () == ERROR_FILE_NOT_FOUND)
        gpg_err_set_errno (ENOENT);
      else
        gpg_err_set_errno (EIO);
#endif /*HAVE_W32CE_SYSTEM*/
      if (errno && errno != ENOENT)
        log_fatal ( _("can't access '%s': %s\n"), fname, strerror (errno));

      oldmask = umask (077);
      if (is_secured_filename (fname))
        {
          fp = NULL;
          gpg_err_set_errno (EPERM);
        }
      else
        fp = es_fopen (fname, "wb");
      umask(oldmask);
      if (!fp)
        log_fatal (_("can't create '%s': %s\n"), fname, strerror (errno));
      es_fclose (fp);

      db_fd = gnupg_open (db_name, O_RDWR | MY_O_BINARY, 0);
      if (db_fd == -1)
        log_fatal (_("can't open '%s': %s\n"), db_name, strerror (errno));

      rc = create_version_record (ctrl);
      if (rc)
        log_fatal (_("%s: failed to create version record: %s"),
                   fname, gpg_strerror (rc));

      /* Read again to check that we are okay. */
      if (tdbio_read_record (0, &rec, RECTYPE_VER))
        log_fatal (_("%s: invalid trustdb created\n"), db_name);

      if (!opt.quiet)
        log_info (_("%s: trustdb created\n"), db_name);
    }

  release_write_lock ();
  return 0;
}


/*
 * Return the full name of the trustdb.
 */
const char *
tdbio_get_dbname ()
{
  return db_name;
}


/*
 * Open the trustdb.  This may only be called if it has not yet been
 * opened and after a successful call to tdbio_set_dbname.  On return
 * the trustdb handle (DB_FD) is guaranteed to be open.
 */
static void
open_db ()
{
  TRUSTREC rec;

  log_assert( db_fd == -1 );

#ifdef HAVE_W32CE_SYSTEM
  {
    DWORD prevrc = 0;
    wchar_t *wname = utf8_to_wchar (db_name);
    if (wname)
      {
        db_fd = (int)CreateFile (wname, GENERIC_READ|GENERIC_WRITE,
                                 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
                                 OPEN_EXISTING, 0, NULL);
        xfree (wname);
      }
    if (db_fd == -1)
      log_fatal ("can't open '%s': %d, %d\n", db_name,
                 (int)prevrc, (int)GetLastError ());
  }
#else /*!HAVE_W32CE_SYSTEM*/
  db_fd = gnupg_open (db_name, O_RDWR | MY_O_BINARY, 0);
  if (db_fd == -1 && (errno == EACCES
#ifdef EROFS
                      || errno == EROFS
#endif
                      )
      ) {
      /* Take care of read-only trustdbs.  */
      db_fd = gnupg_open (db_name, O_RDONLY | MY_O_BINARY, 0);
      if (db_fd != -1 && !opt.quiet)
          log_info (_("Note: trustdb not writable\n"));
  }
  if ( db_fd == -1 )
    log_fatal( _("can't open '%s': %s\n"), db_name, strerror(errno) );
#endif /*!HAVE_W32CE_SYSTEM*/
  register_secured_file (db_name);

  /* Read the version record. */
  if (tdbio_read_record (0, &rec, RECTYPE_VER ) )
    log_fatal( _("%s: invalid trustdb\n"), db_name );
}


/*
 * Append a new empty hashtable to the trustdb.  TYPE gives the type
 * of the hash table.  The only defined type is 0 for a trust hash.
 * On return the hashtable has been created, written, the version
 * record update, and the data flushed to the disk.  On a fatal error
 * the function terminates the process.
 */
static void
create_hashtable (ctrl_t ctrl, TRUSTREC *vr, int type)
{
  TRUSTREC rec;
  off_t offset;
  ulong recnum;
  int i, n, rc;

  offset = lseek (db_fd, 0, SEEK_END);
  if (offset == -1)
    log_fatal ("trustdb: lseek to end failed: %s\n", strerror(errno));
  recnum = offset / TRUST_RECORD_LEN;
  log_assert (recnum); /* This is will never be the first record. */

  if (!type)
    vr->r.ver.trusthashtbl = recnum;

  /* Now write the records making up the hash table. */
  n = (256+ITEMS_PER_HTBL_RECORD-1) / ITEMS_PER_HTBL_RECORD;
  for (i=0; i < n; i++, recnum++)
    {
      memset (&rec, 0, sizeof rec);
      rec.rectype = RECTYPE_HTBL;
      rec.recnum = recnum;
      rc = tdbio_write_record (ctrl, &rec);
      if (rc)
        log_fatal (_("%s: failed to create hashtable: %s\n"),
                   db_name, gpg_strerror (rc));
    }
  /* Update the version record and flush. */
  rc = tdbio_write_record (ctrl, vr);
  if (!rc)
    rc = tdbio_sync ();
  if (rc)
    log_fatal (_("%s: error updating version record: %s\n"),
               db_name, gpg_strerror (rc));
}


/*
 * Check whether open trustdb matches the global trust options given
 * for this process.  On a read problem the process is terminated.
 *
 * Return: 1 for yes, 0 for no.
 */
int
tdbio_db_matches_options()
{
  static int yes_no = -1;

  if (yes_no == -1)
    {
      TRUSTREC vr;
      int rc;
      int opt_tm, tm;

      rc = tdbio_read_record (0, &vr, RECTYPE_VER);
      if( rc )
	log_fatal( _("%s: error reading version record: %s\n"),
		   db_name, gpg_strerror (rc) );

      /* Consider tofu and pgp the same.  */
      tm = vr.r.ver.trust_model;
      if (tm == TM_TOFU || tm == TM_TOFU_PGP)
        tm = TM_PGP;
      opt_tm  = opt.trust_model;
      if (opt_tm == TM_TOFU || opt_tm == TM_TOFU_PGP)
        opt_tm = TM_PGP;

      yes_no = vr.r.ver.marginals == opt.marginals_needed
	&& vr.r.ver.completes == opt.completes_needed
	&& vr.r.ver.cert_depth == opt.max_cert_depth
	&& tm == opt_tm
	&& vr.r.ver.min_cert_level == opt.min_cert_level;
    }

  return yes_no;
}


/*
 * Read and return the trust model identifier from the trustdb.  On a
 * read problem the process is terminated.
 */
byte
tdbio_read_model (void)
{
  TRUSTREC vr;
  int rc;

  rc = tdbio_read_record (0, &vr, RECTYPE_VER );
  if (rc)
    log_fatal (_("%s: error reading version record: %s\n"),
	       db_name, gpg_strerror (rc) );
  return vr.r.ver.trust_model;
}


/*
 * Read and return the nextstamp value from the trustdb.  On a read
 * problem the process is terminated.
 */
ulong
tdbio_read_nextcheck ()
{
  TRUSTREC vr;
  int rc;

  rc = tdbio_read_record (0, &vr, RECTYPE_VER);
  if (rc)
    log_fatal (_("%s: error reading version record: %s\n"),
               db_name, gpg_strerror (rc));
  return vr.r.ver.nextcheck;
}


/*
 * Write the STAMP nextstamp timestamp to the trustdb.  On a read or
 * write problem the process is terminated.
 *
 * Return: True if the stamp actually changed.
 */
int
tdbio_write_nextcheck (ctrl_t ctrl, ulong stamp)
{
  TRUSTREC vr;
  int rc;

  rc = tdbio_read_record (0, &vr, RECTYPE_VER);
  if (rc)
    log_fatal (_("%s: error reading version record: %s\n"),
               db_name, gpg_strerror (rc));

  if (vr.r.ver.nextcheck == stamp)
    return 0;

  vr.r.ver.nextcheck = stamp;
  rc = tdbio_write_record (ctrl, &vr);
  if (rc)
    log_fatal (_("%s: error writing version record: %s\n"),
               db_name, gpg_strerror (rc));
  return 1;
}



/*
 * Return the record number of the trusthash table or create one if it
 * does not yet exist.  On a read or write problem the process is
 * terminated.
 *
 * Return: record number
 */
static ulong
get_trusthashrec (ctrl_t ctrl)
{
  static ulong trusthashtbl; /* Record number of the trust hashtable.  */

  (void)ctrl;

  if (!trusthashtbl)
    {
      TRUSTREC vr;
      int rc;

      rc = tdbio_read_record (0, &vr, RECTYPE_VER );
      if (rc)
        log_fatal (_("%s: error reading version record: %s\n"),
                   db_name, gpg_strerror (rc) );

      if (!vr.r.ver.trusthashtbl)
        {
          /* Oops: the trustdb is corrupt because the hashtable is
           * always created along with the version record.  However,
           * if something went initially wrong it may happen that
           * there is just the version record.  We try to fix it here.
           * If we can't do that we return 0 - this is the version
           * record and thus the actual read will detect the mismatch
           * and bail out.  Note that create_hashtable updates VR.  */
          take_write_lock ();
          if (lseek (db_fd, 0, SEEK_END) == TRUST_RECORD_LEN)
            create_hashtable (ctrl, &vr, 0);
          release_write_lock ();
        }
      trusthashtbl = vr.r.ver.trusthashtbl;
    }

  return trusthashtbl;
}



/*
 * Update a hashtable in the trustdb.  TABLE gives the start of the
 * table, KEY and KEYLEN are the key, NEWRECNUM is the record number
 * to insert into the table.
 *
 * Return: 0 on success or an error code.
 */
static int
upd_hashtable (ctrl_t ctrl, ulong table, byte *key, int keylen, ulong newrecnum)
{
  TRUSTREC lastrec, rec;
  ulong hashrec, item;
  int msb;
  int level = 0;
  int rc, i;

  hashrec = table;
 next_level:
  msb = key[level];
  hashrec += msb / ITEMS_PER_HTBL_RECORD;
  rc = tdbio_read_record (hashrec, &rec, RECTYPE_HTBL);
  if (rc)
    {
      log_error ("upd_hashtable: read failed: %s\n", gpg_strerror (rc));
      return rc;
    }

  item = rec.r.htbl.item[msb % ITEMS_PER_HTBL_RECORD];
  if (!item)  /* Insert a new item into the hash table.  */
    {
      rec.r.htbl.item[msb % ITEMS_PER_HTBL_RECORD] = newrecnum;
      rc = tdbio_write_record (ctrl, &rec);
      if (rc)
        {
          log_error ("upd_hashtable: write htbl failed: %s\n",
                     gpg_strerror (rc));
          return rc;
	}
    }
  else if (item != newrecnum) /* Must do an update.  */
    {
      lastrec = rec;
      rc = tdbio_read_record (item, &rec, 0);
      if (rc)
        {
          log_error ("upd_hashtable: read item failed: %s\n",
                     gpg_strerror (rc));
          return rc;
	}

      if (rec.rectype == RECTYPE_HTBL)
        {
          hashrec = item;
          level++;
          if (level >= keylen)
            {
              log_error ("hashtable has invalid indirections.\n");
              return GPG_ERR_TRUSTDB;
	    }
          goto next_level;
	}
      else if (rec.rectype == RECTYPE_HLST) /* Extend the list.  */
        {
          /* Check whether the key is already in this list. */
          for (;;)
            {
              for (i=0; i < ITEMS_PER_HLST_RECORD; i++)
                {
                  if (rec.r.hlst.rnum[i] == newrecnum)
                    {
                      return 0; /* Okay, already in the list.  */
		    }
		}
              if (rec.r.hlst.next)
                {
                  rc = tdbio_read_record (rec.r.hlst.next, &rec, RECTYPE_HLST);
                  if (rc)
                    {
                      log_error ("upd_hashtable: read hlst failed: %s\n",
                                 gpg_strerror (rc) );
                      return rc;
		    }
		}
              else
                break; /* key is not in the list */
	    }

          /* Find the next free entry and put it in.  */
          for (;;)
            {
              for (i=0; i < ITEMS_PER_HLST_RECORD; i++)
                {
                  if (!rec.r.hlst.rnum[i])
                    {
                      /* Empty slot found.  */
                      rec.r.hlst.rnum[i] = newrecnum;
                      rc = tdbio_write_record (ctrl, &rec);
                      if (rc)
                        log_error ("upd_hashtable: write hlst failed: %s\n",
                                   gpg_strerror (rc));
                      return rc; /* Done.  */
		    }
		}

              if (rec.r.hlst.next)
                {
                  /* read the next reord of the list.  */
                  rc = tdbio_read_record (rec.r.hlst.next, &rec, RECTYPE_HLST);
                  if (rc)
                    {
                      log_error ("upd_hashtable: read hlst failed: %s\n",
                                 gpg_strerror (rc));
                      return rc;
		    }
		}
              else
                {
                  /* Append a new record to the list.  */
                  rec.r.hlst.next = item = tdbio_new_recnum (ctrl);
                  rc = tdbio_write_record (ctrl, &rec);
                  if (rc)
                    {
                      log_error ("upd_hashtable: write hlst failed: %s\n",
                                 gpg_strerror (rc));
                      return rc;
		    }
                  memset (&rec, 0, sizeof rec);
                  rec.rectype = RECTYPE_HLST;
                  rec.recnum = item;
                  rec.r.hlst.rnum[0] = newrecnum;
                  rc = tdbio_write_record (ctrl, &rec);
                  if (rc)
                    log_error ("upd_hashtable: write ext hlst failed: %s\n",
                               gpg_strerror (rc));
                  return rc; /* Done.  */
		}
	    } /* end loop over list slots */

	}
      else if (rec.rectype == RECTYPE_TRUST) /* Insert a list record.  */
        {
          if (rec.recnum == newrecnum)
            {
              return 0;
            }
          item = rec.recnum; /* Save number of key record.  */
          memset (&rec, 0, sizeof rec);
          rec.rectype = RECTYPE_HLST;
          rec.recnum = tdbio_new_recnum (ctrl);
          rec.r.hlst.rnum[0] = item;	    /* Old key record */
          rec.r.hlst.rnum[1] = newrecnum; /* and new key record */
          rc = tdbio_write_record (ctrl, &rec);
          if (rc)
            {
              log_error( "upd_hashtable: write new hlst failed: %s\n",
                           gpg_strerror (rc) );
              return rc;
            }
          /* Update the hashtable record.  */
          lastrec.r.htbl.item[msb % ITEMS_PER_HTBL_RECORD] = rec.recnum;
          rc = tdbio_write_record (ctrl, &lastrec);
          if (rc)
            log_error ("upd_hashtable: update htbl failed: %s\n",
                       gpg_strerror (rc));
          return rc; /* Ready.  */
        }
      else
        {
          log_error ("hashtbl %lu: %lu/%d points to an invalid record %lu\n",
                     table, hashrec, (msb % ITEMS_PER_HTBL_RECORD), item);
          if (opt.verbose > 1)
            list_trustdb (ctrl, es_stderr, NULL);
          return GPG_ERR_TRUSTDB;
	}
    }

  return 0;
}


/*
 * Drop an entry from a hashtable.  TABLE gives the start of the
 * table, KEY and KEYLEN are the key.
 *
 * Return: 0 on success or an error code.
 */
static int
drop_from_hashtable (ctrl_t ctrl, ulong table,
                     byte *key, int keylen, ulong recnum)
{
  TRUSTREC rec;
  ulong hashrec, item;
  int msb;
  int level = 0;
  int rc, i;

  hashrec = table;
 next_level:
  msb = key[level];
  hashrec += msb / ITEMS_PER_HTBL_RECORD;
  rc = tdbio_read_record (hashrec, &rec, RECTYPE_HTBL );
  if (rc)
    {
      log_error ("drop_from_hashtable: read failed: %s\n", gpg_strerror (rc));
      return rc;
    }

  item = rec.r.htbl.item[msb % ITEMS_PER_HTBL_RECORD];
  if (!item)
    return 0;   /* Not found - forget about it.  */

  if (item == recnum) /* Table points direct to the record.  */
    {
      rec.r.htbl.item[msb % ITEMS_PER_HTBL_RECORD] = 0;
      rc = tdbio_write_record (ctrl, &rec);
      if (rc)
        log_error ("drop_from_hashtable: write htbl failed: %s\n",
                   gpg_strerror (rc));
      return rc;
    }

  rc = tdbio_read_record (item, &rec, 0);
  if (rc)
    {
      log_error ("drop_from_hashtable: read item failed: %s\n",
                 gpg_strerror (rc));
      return rc;
    }

  if (rec.rectype == RECTYPE_HTBL)
    {
      hashrec = item;
      level++;
      if (level >= keylen)
        {
          log_error ("hashtable has invalid indirections.\n");
          return GPG_ERR_TRUSTDB;
	}
      goto next_level;
    }

  if (rec.rectype == RECTYPE_HLST)
    {
      for (;;)
        {
          for (i=0; i < ITEMS_PER_HLST_RECORD; i++)
            {
              if (rec.r.hlst.rnum[i] == recnum)
                {
                  rec.r.hlst.rnum[i] = 0; /* Mark as free.  */
                  rc = tdbio_write_record (ctrl, &rec);
                  if (rc)
                    log_error("drop_from_hashtable: write htbl failed: %s\n",
                              gpg_strerror (rc));
                  return rc;
		}
	    }
          if (rec.r.hlst.next)
            {
              rc = tdbio_read_record (rec.r.hlst.next, &rec, RECTYPE_HLST);
              if (rc)
                {
                  log_error ("drop_from_hashtable: read hlst failed: %s\n",
                             gpg_strerror (rc));
                  return rc;
		}
	    }
          else
            return 0; /* Key not in table.  */
	}
    }

  log_error ("hashtbl %lu: %lu/%d points to wrong record %lu\n",
             table, hashrec, (msb % ITEMS_PER_HTBL_RECORD), item);
  return GPG_ERR_TRUSTDB;
}



/*
 * Lookup a record via the hashtable TABLE by (KEY,KEYLEN) and return
 * the result in REC.  The return value of CMP() should be True if the
 * record is the desired one.
 *
 * Return: 0 if found, GPG_ERR_NOT_FOUND, or another error code.
 */
static gpg_error_t
lookup_hashtable (ulong table, const byte *key, size_t keylen,
		  int (*cmpfnc)(const void*, const TRUSTREC *),
                  const void *cmpdata, TRUSTREC *rec )
{
  int rc;
  ulong hashrec, item;
  int msb;
  int level = 0;

  if (!table)
    {
      rc = gpg_error (GPG_ERR_INV_RECORD);
      log_error("lookup_hashtable failed: %s\n", "request for record 0");
      return rc;
    }

  hashrec = table;
 next_level:
  msb = key[level];
  hashrec += msb / ITEMS_PER_HTBL_RECORD;
  rc = tdbio_read_record (hashrec, rec, RECTYPE_HTBL);
  if (rc)
    {
      log_error("lookup_hashtable failed: %s\n", gpg_strerror (rc) );
      return rc;
    }

  item = rec->r.htbl.item[msb % ITEMS_PER_HTBL_RECORD];
  if (!item)
    return gpg_error (GPG_ERR_NOT_FOUND);

  rc = tdbio_read_record (item, rec, 0);
  if (rc)
    {
      log_error( "hashtable read failed: %s\n", gpg_strerror (rc) );
      return rc;
    }
  if (rec->rectype == RECTYPE_HTBL)
    {
      hashrec = item;
      level++;
      if (level >= keylen)
        {
          log_error ("hashtable has invalid indirections\n");
          return GPG_ERR_TRUSTDB;
	}
      goto next_level;
    }
  else if (rec->rectype == RECTYPE_HLST)
    {
      for (;;)
        {
          int i;

          for (i=0; i < ITEMS_PER_HLST_RECORD; i++)
            {
              if (rec->r.hlst.rnum[i])
                {
                  TRUSTREC tmp;

                  rc = tdbio_read_record (rec->r.hlst.rnum[i], &tmp, 0);
                  if (rc)
                    {
                      log_error ("lookup_hashtable: read item failed: %s\n",
                                 gpg_strerror (rc));
                      return rc;
		    }
                  if ((*cmpfnc)(cmpdata, &tmp))
                    {
                      *rec = tmp;
                      return 0;
		    }
		}
	    }
          if (rec->r.hlst.next)
            {
              rc = tdbio_read_record (rec->r.hlst.next, rec, RECTYPE_HLST);
              if (rc)
                {
                  log_error ("lookup_hashtable: read hlst failed: %s\n",
                             gpg_strerror (rc) );
                  return rc;
		}
	    }
          else
            return gpg_error (GPG_ERR_NOT_FOUND);
	}
    }

  if ((*cmpfnc)(cmpdata, rec))
    return 0; /* really found */

  return gpg_error (GPG_ERR_NOT_FOUND); /* no: not found */
}


/*
 * Update the trust hash table TR or create the table if it does not
 * exist.
 *
 * Return: 0 on success or an error code.
 */
static int
update_trusthashtbl (ctrl_t ctrl, TRUSTREC *tr)
{
  return upd_hashtable (ctrl, get_trusthashrec (ctrl),
                        tr->r.trust.fingerprint, 20, tr->recnum);
}


/*
 * Dump the trustdb record REC to stream FP.
 */
void
tdbio_dump_record (TRUSTREC *rec, estream_t fp)
{
  int i;
  ulong rnum = rec->recnum;

  es_fprintf (fp, "rec %5lu, ", rnum);

  switch (rec->rectype)
    {
    case 0:
      es_fprintf (fp, "blank\n");
      break;

    case RECTYPE_VER:
      es_fprintf (fp,
         "version, td=%lu, f=%lu, m/c/d=%d/%d/%d tm=%d mcl=%d nc=%lu (%s)\n",
                  rec->r.ver.trusthashtbl,
                  rec->r.ver.firstfree,
                  rec->r.ver.marginals,
                  rec->r.ver.completes,
                  rec->r.ver.cert_depth,
                  rec->r.ver.trust_model,
                  rec->r.ver.min_cert_level,
                  rec->r.ver.nextcheck,
                  strtimestamp(rec->r.ver.nextcheck)
                  );
      break;

    case RECTYPE_FREE:
      es_fprintf (fp, "free, next=%lu\n", rec->r.free.next);
      break;

    case RECTYPE_HTBL:
      es_fprintf (fp, "htbl,");
      for (i=0; i < ITEMS_PER_HTBL_RECORD; i++)
        es_fprintf (fp, " %lu", rec->r.htbl.item[i]);
      es_putc ('\n', fp);
      break;

    case RECTYPE_HLST:
      es_fprintf (fp, "hlst, next=%lu,", rec->r.hlst.next);
      for (i=0; i < ITEMS_PER_HLST_RECORD; i++)
        es_fprintf (fp, " %lu", rec->r.hlst.rnum[i]);
      es_putc ('\n', fp);
      break;

    case RECTYPE_TRUST:
      es_fprintf (fp, "trust ");
      for (i=0; i < 20; i++)
        es_fprintf (fp, "%02X", rec->r.trust.fingerprint[i]);
      es_fprintf (fp, ", ot=%d, d=%d, vl=%lu, mo=%d, f=%02x\n",
                  rec->r.trust.ownertrust,
                  rec->r.trust.depth, rec->r.trust.validlist,
                  rec->r.trust.min_ownertrust, rec->r.trust.flags);
      break;

    case RECTYPE_VALID:
      es_fprintf (fp, "valid ");
      for (i=0; i < 20; i++)
        es_fprintf(fp, "%02X", rec->r.valid.namehash[i]);
      es_fprintf (fp, ", v=%d, next=%lu, f=%d, m=%d\n",
                  rec->r.valid.validity, rec->r.valid.next,
                  rec->r.valid.full_count, rec->r.valid.marginal_count);
      break;

    default:
      es_fprintf (fp, "unknown type %d\n", rec->rectype );
      break;
    }
}


/*
 * Read the record with number RECNUM into the structure REC.  If
 * EXPECTED is not 0 reading any other record type will return an
 * error.
 *
 * Return: 0 on success or an error code.
 */
int
tdbio_read_record (ulong recnum, TRUSTREC *rec, int expected)
{
  byte readbuf[TRUST_RECORD_LEN];
  const byte *buf, *p;
  gpg_error_t err = 0;
  int n, i;

  if (db_fd == -1)
    open_db ();

  buf = get_record_from_cache( recnum );
  if (!buf)
    {
      if (lseek (db_fd, recnum * TRUST_RECORD_LEN, SEEK_SET) == -1)
        {
          err = gpg_error_from_syserror ();
          log_error (_("trustdb: lseek failed: %s\n"), strerror (errno));
          return err;
	}
      n = read (db_fd, readbuf, TRUST_RECORD_LEN);
      if (!n)
        {
          return gpg_error (GPG_ERR_EOF);
	}
      else if (n != TRUST_RECORD_LEN)
        {
          err = gpg_error_from_syserror ();
          log_error (_("trustdb: read failed (n=%d): %s\n"),
                     n, strerror(errno));
          return err;
	}
      buf = readbuf;
    }
  rec->recnum = recnum;
  rec->dirty = 0;
  p = buf;
  rec->rectype = *p++;
  if (expected && rec->rectype != expected)
    {
      log_error ("%lu: read expected rec type %d, got %d\n",
                 recnum, expected, rec->rectype);
      return gpg_error (GPG_ERR_TRUSTDB);
    }
  p++;    /* Skip reserved byte.  */
  switch (rec->rectype)
    {
    case 0:  /* unused (free) record */
      break;

    case RECTYPE_VER: /* version record */
      if (memcmp(buf+1, GPGEXT_GPG, 3))
        {
          log_error (_("%s: not a trustdb file\n"), db_name );
          err = gpg_error (GPG_ERR_TRUSTDB);
        }
      else
        {
          p += 2; /* skip "gpg" */
          rec->r.ver.version  = *p++;
          rec->r.ver.marginals = *p++;
          rec->r.ver.completes = *p++;
          rec->r.ver.cert_depth = *p++;
          rec->r.ver.trust_model = *p++;
          rec->r.ver.min_cert_level = *p++;
          p += 2;
          rec->r.ver.created  = buf32_to_ulong(p);
          p += 4;
          rec->r.ver.nextcheck = buf32_to_ulong(p);
          p += 4;
          p += 4;
          p += 4;
          rec->r.ver.firstfree = buf32_to_ulong(p);
          p += 4;
          p += 4;
          rec->r.ver.trusthashtbl = buf32_to_ulong(p);
          if (recnum)
            {
              log_error( _("%s: version record with recnum %lu\n"), db_name,
                         (ulong)recnum );
              err = gpg_error (GPG_ERR_TRUSTDB);
            }
          else if (rec->r.ver.version != 3)
            {
              log_error( _("%s: invalid file version %d\n"), db_name,
                         rec->r.ver.version );
              err = gpg_error (GPG_ERR_TRUSTDB);
            }
        }
      break;

    case RECTYPE_FREE:
      rec->r.free.next  = buf32_to_ulong(p);
      break;

    case RECTYPE_HTBL:
      for (i=0; i < ITEMS_PER_HTBL_RECORD; i++)
        {
          rec->r.htbl.item[i] = buf32_to_ulong(p);
          p += 4;
	}
      break;

    case RECTYPE_HLST:
      rec->r.hlst.next = buf32_to_ulong(p);
      p += 4;
      for (i=0; i < ITEMS_PER_HLST_RECORD; i++)
        {
          rec->r.hlst.rnum[i] = buf32_to_ulong(p);
          p += 4;
	}
      break;

    case RECTYPE_TRUST:
      memcpy (rec->r.trust.fingerprint, p, 20);
      p+=20;
      rec->r.trust.ownertrust = *p++;
      rec->r.trust.depth = *p++;
      rec->r.trust.min_ownertrust = *p++;
      rec->r.trust.flags = *p++;
      rec->r.trust.validlist = buf32_to_ulong(p);
      break;

    case RECTYPE_VALID:
      memcpy (rec->r.valid.namehash, p, 20);
      p+=20;
      rec->r.valid.validity = *p++;
      rec->r.valid.next = buf32_to_ulong(p);
      p += 4;
      rec->r.valid.full_count = *p++;
      rec->r.valid.marginal_count = *p++;
      break;

    default:
      log_error ("%s: invalid record type %d at recnum %lu\n",
                 db_name, rec->rectype, (ulong)recnum);
      err = gpg_error (GPG_ERR_TRUSTDB);
      break;
    }

  return err;
}


/*
 * Write the record from the struct REC.
 *
 * Return: 0 on success or an error code.
 */
int
tdbio_write_record (ctrl_t ctrl, TRUSTREC *rec)
{
  byte buf[TRUST_RECORD_LEN];
  byte *p;
  int rc = 0;
  int i;
  ulong recnum = rec->recnum;

  if (db_fd == -1)
    open_db ();

  memset (buf, 0, TRUST_RECORD_LEN);
  p = buf;
  *p++ = rec->rectype; p++;

  switch (rec->rectype)
    {
    case 0:  /* unused record */
      break;

    case RECTYPE_VER: /* version record */
      if (recnum)
        BUG ();
      memcpy(p-1, GPGEXT_GPG, 3 ); p += 2;
      *p++ = rec->r.ver.version;
      *p++ = rec->r.ver.marginals;
      *p++ = rec->r.ver.completes;
      *p++ = rec->r.ver.cert_depth;
      *p++ = rec->r.ver.trust_model;
      *p++ = rec->r.ver.min_cert_level;
      p += 2;
      ulongtobuf(p, rec->r.ver.created); p += 4;
      ulongtobuf(p, rec->r.ver.nextcheck); p += 4;
      p += 4;
      p += 4;
      ulongtobuf(p, rec->r.ver.firstfree ); p += 4;
      p += 4;
      ulongtobuf(p, rec->r.ver.trusthashtbl ); p += 4;
      break;

    case RECTYPE_FREE:
      ulongtobuf(p, rec->r.free.next); p += 4;
      break;

    case RECTYPE_HTBL:
      for (i=0; i < ITEMS_PER_HTBL_RECORD; i++)
        {
          ulongtobuf( p, rec->r.htbl.item[i]); p += 4;
        }
      break;

    case RECTYPE_HLST:
      ulongtobuf( p, rec->r.hlst.next); p += 4;
      for (i=0; i < ITEMS_PER_HLST_RECORD; i++ )
        {
          ulongtobuf( p, rec->r.hlst.rnum[i]); p += 4;
	}
      break;

    case RECTYPE_TRUST:
      memcpy (p, rec->r.trust.fingerprint, 20); p += 20;
      *p++ = rec->r.trust.ownertrust;
      *p++ = rec->r.trust.depth;
      *p++ = rec->r.trust.min_ownertrust;
      *p++ = rec->r.trust.flags;
      ulongtobuf( p, rec->r.trust.validlist); p += 4;
      break;

    case RECTYPE_VALID:
      memcpy (p, rec->r.valid.namehash, 20); p += 20;
      *p++ = rec->r.valid.validity;
      ulongtobuf( p, rec->r.valid.next); p += 4;
      *p++ = rec->r.valid.full_count;
      *p++ = rec->r.valid.marginal_count;
      break;

    default:
      BUG();
    }

  rc = put_record_into_cache (recnum, buf);
  if (rc)
    ;
  else if (rec->rectype == RECTYPE_TRUST)
    rc = update_trusthashtbl (ctrl, rec);

  return rc;
}


/*
 * Delete the record at record number RECNUm from the trustdb.
 *
 * Return: 0 on success or an error code.
 */
int
tdbio_delete_record (ctrl_t ctrl, ulong recnum)
{
  TRUSTREC vr, rec;
  int rc;

  /* Must read the record fist, so we can drop it from the hash tables */
  rc = tdbio_read_record (recnum, &rec, 0);
  if (rc)
    ;
  else if (rec.rectype == RECTYPE_TRUST)
    {
      rc = drop_from_hashtable (ctrl, get_trusthashrec (ctrl),
                                rec.r.trust.fingerprint, 20, rec.recnum);
    }

  if (rc)
    return rc;

  /* Now we can change it to a free record.  */
  rc = tdbio_read_record (0, &vr, RECTYPE_VER);
  if (rc)
    log_fatal (_("%s: error reading version record: %s\n"),
               db_name, gpg_strerror (rc));

  rec.recnum = recnum;
  rec.rectype = RECTYPE_FREE;
  rec.r.free.next = vr.r.ver.firstfree;
  vr.r.ver.firstfree = recnum;
  rc = tdbio_write_record (ctrl, &rec);
  if (!rc)
    rc = tdbio_write_record (ctrl, &vr);

  return rc;
}


/*
 * Create a new record and return its record number.
 */
ulong
tdbio_new_recnum (ctrl_t ctrl)
{
  off_t offset;
  ulong recnum;
  TRUSTREC vr, rec;
  int rc;

  /* Look for unused records.  */
  rc = tdbio_read_record (0, &vr, RECTYPE_VER);
  if (rc)
    log_fatal( _("%s: error reading version record: %s\n"),
               db_name, gpg_strerror (rc));
  if (vr.r.ver.firstfree)
    {
      recnum = vr.r.ver.firstfree;
      rc = tdbio_read_record (recnum, &rec, RECTYPE_FREE);
      if (rc)
        log_fatal (_("%s: error reading free record: %s\n"),
                   db_name, gpg_strerror (rc));
      /* Update dir record.  */
      vr.r.ver.firstfree = rec.r.free.next;
      rc = tdbio_write_record (ctrl, &vr);
      if (rc)
        log_fatal (_("%s: error writing dir record: %s\n"),
                   db_name, gpg_strerror (rc));
      /* Zero out the new record.  */
      memset (&rec, 0, sizeof rec);
      rec.rectype = 0; /* Mark as unused record (actually already done
                          my the memset).  */
      rec.recnum = recnum;
      rc = tdbio_write_record (ctrl, &rec);
      if (rc)
        log_fatal (_("%s: failed to zero a record: %s\n"),
                   db_name, gpg_strerror (rc));
    }
  else /* Not found - append a new record.  */
    {
      offset = lseek (db_fd, 0, SEEK_END);
      if (offset == (off_t)(-1))
        log_fatal ("trustdb: lseek to end failed: %s\n", strerror (errno));
      recnum = offset / TRUST_RECORD_LEN;
      log_assert (recnum); /* This will never be the first record */
      /* We must write a record, so that the next call to this
       * function returns another recnum.  */
      memset (&rec, 0, sizeof rec);
      rec.rectype = 0; /* unused record */
      rec.recnum = recnum;
      rc = 0;
      if (lseek( db_fd, recnum * TRUST_RECORD_LEN, SEEK_SET) == -1)
        {
          rc = gpg_error_from_syserror ();
          log_error (_("trustdb rec %lu: lseek failed: %s\n"),
                     recnum, strerror (errno));
	}
      else
        {
          int n;

          n = write (db_fd, &rec, TRUST_RECORD_LEN);
          if (n != TRUST_RECORD_LEN)
            {
              rc = gpg_error_from_syserror ();
              log_error (_("trustdb rec %lu: write failed (n=%d): %s\n"),
                         recnum, n, gpg_strerror (rc));
	    }
	}

      if (rc)
        log_fatal (_("%s: failed to append a record: %s\n"),
                   db_name, gpg_strerror (rc));
    }

  return recnum ;
}



/* Helper function for tdbio_search_trust_byfpr.  */
static int
cmp_trec_fpr ( const void *fpr, const TRUSTREC *rec )
{
  return (rec->rectype == RECTYPE_TRUST
          && !memcmp (rec->r.trust.fingerprint, fpr, 20));
}


/*
 * Given a 20 byte FINGERPRINT search its trust record and return
 * that at REC.
 *
 * Return: 0 if found, GPG_ERR_NOT_FOUND, or another error code.
 */
gpg_error_t
tdbio_search_trust_byfpr (ctrl_t ctrl, const byte *fingerprint, TRUSTREC *rec)
{
  int rc;

  /* Locate the trust record using the hash table */
  rc = lookup_hashtable (get_trusthashrec (ctrl), fingerprint, 20,
                         cmp_trec_fpr, fingerprint, rec );
  return rc;
}


/*
 * Given a primary public key object PK search its trust record and
 * return that at REC.
 *
 * Return: 0 if found, GPG_ERR_NOT_FOUND, or another error code.
 */
gpg_error_t
tdbio_search_trust_bypk (ctrl_t ctrl, PKT_public_key *pk, TRUSTREC *rec)
{
  byte fingerprint[MAX_FINGERPRINT_LEN];
  size_t fingerlen;

  fingerprint_from_pk( pk, fingerprint, &fingerlen );
  for (; fingerlen < 20; fingerlen++)
    fingerprint[fingerlen] = 0;
  return tdbio_search_trust_byfpr (ctrl, fingerprint, rec);
}


/*
 * Terminate the process with a message about a corrupted trustdb.
 */
void
tdbio_invalid (void)
{
  log_error (_("Error: The trustdb is corrupted.\n"));
  how_to_fix_the_trustdb ();
  g10_exit (2);
}