summaryrefslogtreecommitdiffstats
path: root/g10/trustdb.c
blob: 88e06c3154e9aab454b972bec841c0e53fe0895e (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
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
/* trustdb.c
 *	Copyright (C) 1998, 1999 Free Software Foundation, Inc.
 *
 * 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 2 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 */

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

#include "errors.h"
#include "iobuf.h"
#include "keydb.h"
#include "memory.h"
#include "util.h"
#include "trustdb.h"
#include "options.h"
#include "packet.h"
#include "main.h"
#include "i18n.h"
#include "tdbio.h"
#include "ttyio.h"

#if MAX_FINGERPRINT_LEN > 20
  #error Must change structure of trustdb
#endif

struct keyid_list {
    struct keyid_list *next;
    u32 keyid[2];
};

struct local_id_item {
    struct local_id_item *next;
    ulong lid;
    unsigned flag;
};

struct local_id_table {
    struct local_id_table *next; /* only used to keep a list of unused tables */
    struct local_id_item *items[16];
};


typedef struct local_id_table *LOCAL_ID_TABLE;


struct enum_cert_paths_ctx {
   int init;
   int idx;
};


struct recno_list_struct {
    struct recno_list_struct *next;
    ulong recno;
    int type;
};
typedef struct recno_list_struct *RECNO_LIST;



typedef struct trust_node *TN;
struct trust_node {
    TN	  back;  /* parent */
    TN	  list;  /* list of other node (should all be of the same type)*/
    TN	  next;  /* used to build the list */
    int   is_uid; /* set if this is an uid node */
    ulong lid;	 /* key or uid recordnumber */
    union {
	struct {
	    int ownertrust;
	    int validity;
	    /* helper */
	    int buckstop;
	} k;
	struct {
	    int marginal_count;
	    int fully_count;
	    int validity;
	} u;
    } n;
};


static TN used_tns;
static int alloced_tns;
static int max_alloced_tns;



static LOCAL_ID_TABLE new_lid_table(void);
static int ins_lid_table_item( LOCAL_ID_TABLE tbl, ulong lid, unsigned flag );
static int qry_lid_table_flag( LOCAL_ID_TABLE tbl, ulong lid, unsigned *flag );


static int propagate_validity( TN root, TN node,
			       int (*add_fnc)(ulong), unsigned *retflgs );

static void print_user_id( FILE *fp, const char *text, u32 *keyid );
static int do_check( TRUSTREC *drec, unsigned *trustlevel,
		     const char *nhash, int (*add_fnc)(ulong),
						unsigned *retflgs);
static int get_dir_record( PKT_public_key *pk, TRUSTREC *rec );

static void upd_pref_record( TRUSTREC *urec, u32 *keyid, PKT_signature *sig );
static void upd_cert_record( KBNODE keyblock, KBNODE signode, u32 *keyid,
		 TRUSTREC *drec, RECNO_LIST *recno_list, int recheck,
		 TRUSTREC *urec, const byte *uidhash, int revoked,
					int *mod_up, int *mod_down );

/* a table used to keep track of ultimately trusted keys
 * which are the ones from our secrings and the trusted keys */
static LOCAL_ID_TABLE ultikey_table;

/* list of unused lid items and tables */
static LOCAL_ID_TABLE unused_lid_tables;
static struct local_id_item *unused_lid_items;

static struct {
    int init;
    int level;
    char *dbname;
} trustdb_args;


/**********************************************
 ***********  record read write  **************
 **********************************************/


/****************
 * Read a record but die if it does not exist
 */
static void
read_record( ulong recno, TRUSTREC *rec, int rectype )
{
    int rc = tdbio_read_record( recno, rec, rectype );
    if( !rc )
	return;
    log_error(_("trust record %lu, req type %d: read failed: %s\n"),
				    recno, rectype,  g10_errstr(rc) );
    tdbio_invalid();
}


/****************
 * Wirte a record but die on error
 */
static void
write_record( TRUSTREC *rec )
{
    int rc = tdbio_write_record( rec );
    if( !rc )
	return;
    log_error(_("trust record %lu, type %d: write failed: %s\n"),
			    rec->recnum, rec->rectype, g10_errstr(rc) );
    tdbio_invalid();
}

/****************
 * Delete a record but die on error
 */
static void
delete_record( ulong recno )
{
    int rc = tdbio_delete_record( recno );
    if( !rc )
	return;
    log_error(_("trust record %lu: delete failed: %s\n"),
					      recno, g10_errstr(rc) );
    tdbio_invalid();
}

/****************
 * sync the db
 */
static void
do_sync(void)
{
    int rc = tdbio_sync();
    if( !rc )
	return;
    log_error(_("trustdb: sync failed: %s\n"), g10_errstr(rc) );
    g10_exit(2);
}



/**********************************************
 *****************  helpers  ******************
 **********************************************/

/****************
 * Insert a new item into a recno list
 */
static void
ins_recno_list( RECNO_LIST *head, ulong recno, int type )
{
    RECNO_LIST item = m_alloc( sizeof *item );

    item->recno = recno;
    item->type = type;
    item->next = *head;
    *head = item;
}

static RECNO_LIST
qry_recno_list( RECNO_LIST list, ulong recno, int type	)
{
    for( ; list; list = list->next ) {
	if( list->recno == recno && (!type || list->type == type) )
	    return list;
    }
    return NULL;
}


static void
rel_recno_list( RECNO_LIST *head )
{
    RECNO_LIST r, r2;

    for(r = *head; r; r = r2 ) {
	r2 = r->next;
	m_free(r);
    }
    *head = NULL;
}

static LOCAL_ID_TABLE
new_lid_table(void)
{
    LOCAL_ID_TABLE a;

    a = unused_lid_tables;
    if( a ) {
	unused_lid_tables = a->next;
	memset( a, 0, sizeof *a );
    }
    else
	a = m_alloc_clear( sizeof *a );
    return a;
}

#if 0
static void
release_lid_table( LOCAL_ID_TABLE tbl )
{
    struct local_id_item *a, *a2;
    int i;

    for(i=0; i < 16; i++ ) {
	for(a=tbl->items[i]; a; a = a2 ) {
	    a2 = a->next;
	    a->next = unused_lid_items;
	    unused_lid_items = a;
	}
    }
    tbl->next = unused_lid_tables;
    unused_lid_tables = tbl;
}
#endif

/****************
 * Add a new item to the table or return 1 if we already have this item
 */
static int
ins_lid_table_item( LOCAL_ID_TABLE tbl, ulong lid, unsigned flag )
{
    struct local_id_item *a;

    for( a = tbl->items[lid & 0x0f]; a; a = a->next )
	if( a->lid == lid )
	    return 1;
    a = unused_lid_items;
    if( a )
	unused_lid_items = a->next;
    else
	a = m_alloc( sizeof *a );
    a->lid = lid;
    a->flag = flag;
    a->next = tbl->items[lid & 0x0f];
    tbl->items[lid & 0x0f] = a;
    return 0;
}

static int
qry_lid_table_flag( LOCAL_ID_TABLE tbl, ulong lid, unsigned *flag )
{
    struct local_id_item *a;

    for( a = tbl->items[lid & 0x0f]; a; a = a->next )
	if( a->lid == lid ) {
	    if( flag )
		*flag = a->flag;
	    return 0;
	}
    return -1;
}


static TN
new_tn(void)
{
    TN t;

    if( used_tns ) {
	t = used_tns;
	used_tns = t->next;
	memset( t, 0, sizeof *t );
    }
    else
	t = m_alloc_clear( sizeof *t );
    if( ++alloced_tns > max_alloced_tns )
	max_alloced_tns = alloced_tns;
    return t;
}


static void
release_tn( TN t )
{
    if( t ) {
	t->next = used_tns;
	used_tns = t;
	alloced_tns--;
    }
}


static void
release_tn_tree( TN kr )
{
    TN	kr2;

    for( ; kr; kr = kr2 ) {
	release_tn_tree( kr->list );
	kr2 = kr->next;
	release_tn( kr );
    }
}




/**********************************************
 ****** access by LID and other helpers *******
 **********************************************/

/****************
 * Return the keyid from the primary key identified by LID.
 */
int
keyid_from_lid( ulong lid, u32 *keyid )
{
    TRUSTREC rec;
    int rc;

    init_trustdb();
    keyid[0] = keyid[1] = 0;
    rc = tdbio_read_record( lid, &rec, 0 );
    if( rc ) {
	log_error(_("error reading dir record for LID %lu: %s\n"),
						    lid, g10_errstr(rc));
	return G10ERR_TRUSTDB;
    }
    if( rec.rectype == RECTYPE_SDIR )
	return 0;
    if( rec.rectype != RECTYPE_DIR ) {
	log_error(_("lid %lu: expected dir record, got type %d\n"),
						    lid, rec.rectype );
	return G10ERR_TRUSTDB;
    }
    if( !rec.r.dir.keylist ) {
	log_error(_("no primary key for LID %lu\n"), lid );
	return G10ERR_TRUSTDB;
    }
    rc = tdbio_read_record( rec.r.dir.keylist, &rec, RECTYPE_KEY );
    if( rc ) {
	log_error(_("error reading primary key for LID %lu: %s\n"),
						    lid, g10_errstr(rc));
	return G10ERR_TRUSTDB;
    }
    keyid_from_fingerprint( rec.r.key.fingerprint, rec.r.key.fingerprint_len,
			    keyid );

    return 0;
}


ulong
lid_from_keyblock( KBNODE keyblock )
{
    KBNODE node = find_kbnode( keyblock, PKT_PUBLIC_KEY );
    PKT_public_key *pk;
    if( !node )
	BUG();
    pk = node->pkt->pkt.public_key;
    if( !pk->local_id ) {
	TRUSTREC rec;
	init_trustdb();

	get_dir_record( pk, &rec );
    }
    return pk->local_id;
}


static int
get_dir_record( PKT_public_key *pk, TRUSTREC *rec )
{
    int rc=0;

    if( pk->local_id ) {
	read_record( pk->local_id, rec, RECTYPE_DIR );
    }
    else { /* no local_id: scan the trustdb */
	if( (rc=tdbio_search_dir_bypk( pk, rec )) && rc != -1 )
	    log_error(_("get_dir_record: search_record failed: %s\n"),
							    g10_errstr(rc));
    }
    return rc;
}

/****************
 * Get the LID of a public key.
 * Returns: The LID of the key (note, that this may be a shadow dir)
 *	    or 0 if not available.
 */
static ulong
lid_from_keyid( u32 *keyid )
{
    PKT_public_key *pk = m_alloc_clear( sizeof *pk );
    TRUSTREC rec;
    ulong lid = 0;
    int rc;

    rc = get_pubkey( pk, keyid );
    if( !rc ) {
	if( pk->local_id )
	    lid = pk->local_id;
	else {
	    rc = tdbio_search_dir_bypk( pk, &rec );
	    if( !rc )
		lid = rec.recnum;
	    else if( rc == -1 ) { /* see whether there is a sdir instead */
		u32 akid[2];

		keyid_from_pk( pk, akid );
		rc = tdbio_search_sdir( akid, pk->pubkey_algo, &rec );
		if( !rc )
		    lid = rec.recnum;
	    }
	}
    }
    free_public_key( pk );
    return lid;
}



/***********************************************
 *************	Initialization	****************
 ***********************************************/

/****************
 * Verify that all our public keys are in the trustdb.
 */
static int
verify_own_keys(void)
{
    int rc;
    void *enum_context = NULL;
    PKT_secret_key *sk = m_alloc_clear( sizeof *sk );
    PKT_public_key *pk = m_alloc_clear( sizeof *pk );
    u32 keyid[2];

    while( !(rc=enum_secret_keys( &enum_context, sk, 0 ) ) ) {
	int have_pk = 0;

	keyid_from_sk( sk, keyid );

	if( DBG_TRUST )
	    log_debug("key %08lX: checking secret key\n", (ulong)keyid[1] );

	if( is_secret_key_protected( sk ) < 1 )
	    log_info(_("NOTE: secret key %08lX is NOT protected.\n"),
							    (ulong)keyid[1] );


	/* see whether we can access the public key of this secret key */
	memset( pk, 0, sizeof *pk );
	rc = get_pubkey( pk, keyid );
	if( rc ) {
	    log_info(_("key %08lX: secret key without public key - skipped\n"),
							    (ulong)keyid[1] );
	    goto skip;
	}
	have_pk=1;

	if( cmp_public_secret_key( pk, sk ) ) {
	    log_info(_("key %08lX: secret and public key don't match\n"),
							    (ulong)keyid[1] );
	    goto skip;
	}

	/* make sure that the pubkey is in the trustdb */
	rc = query_trust_record( pk );
	if( rc == -1 ) { /* put it into the trustdb */
	    rc = insert_trust_record( pk );
	    if( rc ) {
		log_error(_("key %08lX: can't put it into the trustdb\n"),
							    (ulong)keyid[1] );
		goto skip;
	    }
	}
	else if( rc ) {
	    log_error(_("key %08lX: query record failed\n"), (ulong)keyid[1] );
	    goto skip;

	}

	if( DBG_TRUST )
	    log_debug("key %08lX.%lu: stored into ultikey_table\n",
				    (ulong)keyid[1], pk->local_id );
	if( ins_lid_table_item( ultikey_table, pk->local_id, 0 ) )
	    log_error(_("key %08lX: already in trusted key table\n"),
							(ulong)keyid[1]);
	else if( opt.verbose > 1 )
	    log_info(_("key %08lX: accepted as trusted key.\n"),
							(ulong)keyid[1]);
      skip:
	release_secret_key_parts( sk );
	if( have_pk )
	    release_public_key_parts( pk );
    }
    if( rc != -1 )
	log_error(_("enumerate secret keys failed: %s\n"), g10_errstr(rc) );
    else
	rc = 0;

    enum_secret_keys( &enum_context, NULL, 0 ); /* free context */
    free_secret_key( sk );
    free_public_key( pk );
    return rc;
}


/****************
 * Perform some checks over the trustdb
 *  level 0: only open the db
 *	  1: used for initial program startup
 */
int
setup_trustdb( int level, const char *dbname )
{
    /* just store the args */
    if( trustdb_args.init )
	return 0;
    trustdb_args.level = level;
    trustdb_args.dbname = dbname? m_strdup(dbname): NULL;
    return 0;
}

void
init_trustdb()
{
    int rc=0;
    int level = trustdb_args.level;
    const char* dbname = trustdb_args.dbname;

    if( trustdb_args.init )
	return;

    trustdb_args.init = 1;

    if( !ultikey_table )
	ultikey_table = new_lid_table();

    if( !level || level==1 ) {
	rc = tdbio_set_dbname( dbname, !!level );
	if( !rc ) {
	    if( !level )
		return;

	    /* verify that our own keys are in the trustDB
	     * or move them to the trustdb. */
	    rc = verify_own_keys();

	    /* should we check whether there is no other ultimately trusted
	     * key in the database? */
	}
    }
    else
	BUG();
    if( rc )
	log_fatal("can't init trustdb: %s\n", g10_errstr(rc) );
}





/***********************************************
 *************	Print helpers	****************
 ***********************************************/
static void
print_user_id( FILE *fp, const char *text, u32 *keyid )
{
    char *p;
    size_t n;

    p = get_user_id( keyid, &n );
    if( fp ) {
	fprintf( fp, "%s \"", text );
	print_string( fp, p, n, 0 );
	putc('\"', fp);
	putc('\n', fp);
    }
    else {
	tty_printf( "%s \"", text );
	tty_print_string( p, n );
	tty_printf( "\"\n" );
    }
    m_free(p);
}



int
trust_letter( unsigned value )
{
    switch( value ) {
      case TRUST_UNKNOWN:   return '-';
      case TRUST_EXPIRED:   return 'e';
      case TRUST_UNDEFINED: return 'q';
      case TRUST_NEVER:     return 'n';
      case TRUST_MARGINAL:  return 'm';
      case TRUST_FULLY:     return 'f';
      case TRUST_ULTIMATE:  return 'u';
      default:		    return  0 ;
    }
}


#if 0
static void
print_path( int pathlen, TN ME .........., FILE *fp, ulong highlight )
{
    int rc, c, i;
    u32 keyid[2];
    char *p;
    size_t n;

    for( i = 0; i < pathlen; i++ )  {
	if( highlight )
	    fputs(highlight == path[i].lid? "* ":"  ", fp );
	rc = keyid_from_lid( path[i].lid, keyid );
	if( rc )
	    fprintf(fp, "????????.%lu:", path[i].lid );
	else
	    fprintf(fp,"%08lX.%lu:", (ulong)keyid[1], path[i].lid );
	c = trust_letter(path[i].otrust);
	if( c )
	    putc( c, fp );
	else
	    fprintf( fp, "%02x", path[i].otrust );
	putc('/', fp);
	c = trust_letter(path[i].trust);
	if( c )
	    putc( c, fp );
	else
	    fprintf( fp, "%02x", path[i].trust );
	putc(' ', fp);
	p = get_user_id( keyid, &n );
	putc(' ', fp);
	putc('\"', fp);
	print_string( fp, p, n > 40? 40:n, 0 );
	putc('\"', fp);
	m_free(p);
	putc('\n', fp );
    }
}
#endif


static void
print_default_uid( FILE *fp, ulong lid )
{
    u32 keyid[2];

    if( !keyid_from_lid( lid, keyid ) )
	print_user_id( fp, "", keyid );
}


static void
print_uid_from_keyblock( FILE *fp, KBNODE keyblock, ulong urecno )
{
    TRUSTREC urec;
    KBNODE node;
    byte uhash[20];

    read_record( urecno, &urec, RECTYPE_UID );
    for( node=keyblock; node; node = node->next ) {
	if( node->pkt->pkttype == PKT_USER_ID ) {
	    PKT_user_id *uidpkt = node->pkt->pkt.user_id;

	    rmd160_hash_buffer( uhash, uidpkt->name, uidpkt->len );
	    if( !memcmp( uhash, urec.r.uid.namehash, 20 ) ) {
		print_string( fp,  uidpkt->name, uidpkt->len, ':' );
		return;
	    }
	}
    }

    fputs("[?]", fp );
}



static void
dump_tn_tree( FILE *fp, int level, TN tree )
{
    TN kr, ur;

    for( kr=tree; kr; kr = kr->next ) {
	if( fp ) {
	    fprintf( fp, "%*s", level*4, "" );
	    fprintf( fp, "K%lu(ot=%d,val=%d)  ", kr->lid,
					     kr->n.k.ownertrust,
					     kr->n.k.validity  );
	}
	else {
	    tty_printf("%*s", level*4, "" );
	    tty_printf("K%lu(ot=%d,val=%d)  ", kr->lid,
					     kr->n.k.ownertrust,
					     kr->n.k.validity  );
	}
	print_default_uid( fp, kr->lid );
	for( ur=kr->list; ur; ur = ur->next ) {
	    if( fp ) {
		fprintf(fp, "%*s  ", level*4, "" );
		fprintf(fp, "U%lu(mc=%d,fc=%d,val=%d)\n", ur->lid,
						     ur->n.u.marginal_count,
						     ur->n.u.fully_count,
						     ur->n.u.validity
						);
	    }
	    else {
		tty_printf("%*s  ", level*4, "" );
		tty_printf("U%lu(mc=%d,fc=%d,val=%d)\n", ur->lid,
						     ur->n.u.marginal_count,
						     ur->n.u.fully_count,
						     ur->n.u.validity
						);
	    }
	    dump_tn_tree( fp, level+1, ur->list );
	}
    }
}

/****************
 * Special version of dump_tn_tree, which prints it colon delimited.
 * Format:
 *   level:keyid:type:recno:ot:val:mc:cc:name:
 * With TYPE = U for a user ID
 *	       K for a key
 * The RECNO is either the one of the dir record or the one of the uid record.
 * OT is the the usual trust letter and only availabel on K lines.
 * VAL is the calcualted validity
 * MC is the marginal trust counter and only available on U lines
 * CC is the same for the complete count
 * NAME ist the username and only printed on U lines
 */
static void
dump_tn_tree_with_colons( int level, TN tree )
{
    TN kr, ur;

    for( kr=tree; kr; kr = kr->next ) {
	KBNODE kb = NULL;
	u32 kid[2];

	keyid_from_lid( kr->lid, kid );
	get_keyblock_bylid( &kb, kr->lid );

	printf( "%d:%08lX%08lX:K:%lu:%c:%c::::\n",
			level, (ulong)kid[0], (ulong)kid[1], kr->lid,
			trust_letter( kr->n.k.ownertrust ),
			trust_letter( kr->n.k.validity ) );
	for( ur=kr->list; ur; ur = ur->next ) {
	    printf( "%d:%08lX%08lX:U:%lu::%c:%d:%d:",
			level, (ulong)kid[0], (ulong)kid[1], ur->lid,
			trust_letter( kr->n.u.validity ),
			ur->n.u.marginal_count,
			ur->n.u.fully_count );
	    print_uid_from_keyblock( stdout, kb, ur->lid );
	    putchar(':');
	    putchar('\n');
	    dump_tn_tree_with_colons( level+1, ur->list );
	}
	release_kbnode( kb );
    }
}



/***********************************************
 *************	trustdb maintenance  ***********
 ***********************************************/


static void
check_hint_sig( ulong lid, KBNODE keyblock, u32 *keyid, byte *uidrec_hash,
		TRUSTREC *sigrec, int sigidx, ulong hint_owner )
{
    KBNODE node;
    int rc, state;
    byte uhash[20];
    int is_selfsig;
    PKT_signature *sigpkt = NULL;
    TRUSTREC tmp;
    u32 sigkid[2];
    int revoked = 0;

    if( sigrec->r.sig.sig[sigidx].flag & SIGF_CHECKED )
	log_info(_("NOTE: sig rec %lu[%d] in hintlist "
		   "of %lu but marked as checked\n"),
		    sigrec->recnum, sigidx, hint_owner );
    if( !(sigrec->r.sig.sig[sigidx].flag & SIGF_NOPUBKEY) )
	log_info(_("NOTE: sig rec %lu[%d] in hintlist "
		   "of %lu but not marked\n"),
		    sigrec->recnum, sigidx, hint_owner );

    read_record( sigrec->r.sig.sig[sigidx].lid, &tmp, 0 );
    if( tmp.rectype != RECTYPE_DIR ) {
	/* we need the dir record */
	log_error(_("sig rec %lu[%d] in hintlist "
		    "of %lu does not point to a dir record\n"),
		    sigrec->recnum, sigidx, hint_owner );
	return;
    }
    if( !tmp.r.dir.keylist ) {
	log_error(_("lid %lu: no primary key\n"), tmp.r.dir.lid );
	return;
    }
    read_record(tmp.r.dir.keylist, &tmp, RECTYPE_KEY );
    keyid_from_fingerprint( tmp.r.key.fingerprint,
			    tmp.r.key.fingerprint_len, sigkid );


    /* find the correct signature packet */
    state = 0;
    for( node=keyblock; node; node = node->next ) {
	if( node->pkt->pkttype == PKT_USER_ID ) {
	    PKT_user_id *uidpkt = node->pkt->pkt.user_id;

	    if( state )
		break;
	    rmd160_hash_buffer( uhash, uidpkt->name, uidpkt->len );
	    if( !memcmp( uhash, uidrec_hash, 20 ) )
		state = 1;
	}
	else if( state && node->pkt->pkttype == PKT_SIGNATURE ) {
	    sigpkt = node->pkt->pkt.signature;
	    if( sigpkt->keyid[0] == sigkid[0]
		&& sigpkt->keyid[1] == sigkid[1]
		&& ( (sigpkt->sig_class&~3) == 0x10
		     || ( revoked = (sigpkt->sig_class == 0x30)) ) ) {
		state = 2;
		break; /* found */
	    }
	}
    }

    if( !node ) {
	log_info(_("lid %lu: user id not found in keyblock\n"), lid );
	return ;
    }
    if( state != 2 ) {
	log_info(_("lid %lu: user id without signature\n"), lid );
	return ;
    }

    /* and check the sig */
    rc = check_key_signature( keyblock, node, &is_selfsig );
    if( is_selfsig ) {
	log_error(_("lid %lu: self-signature in hintlist\n"), lid );
	return;
    }

    /* FiXME: handling fo SIGF_REVOKED is not correct! */

    if( !rc ) { /* valid signature */
	if( opt.verbose )
	    log_info("sig %08lX.%lu/%02X%02X/%08lX: %s\n",
		    (ulong)keyid[1], lid, uhash[18], uhash[19],
		    (ulong)sigpkt->keyid[1],
		    revoked? _("Valid certificate revocation")
			   : _("Good certificate") );
	sigrec->r.sig.sig[sigidx].flag = SIGF_CHECKED | SIGF_VALID;
	if( revoked )
	    sigrec->r.sig.sig[sigidx].flag |= SIGF_REVOKED;
    }
    else if( rc == G10ERR_NO_PUBKEY ) {
	log_info("sig %08lX.%lu/%02X%02X/%08lX: %s\n",
	      (ulong)keyid[1], lid, uhash[18], uhash[19],
	       (ulong)sigpkt->keyid[1],
		 _("very strange: no public key\n") );
	sigrec->r.sig.sig[sigidx].flag = SIGF_NOPUBKEY;
    }
    else {
	log_info("sig %08lX.%lu/%02X%02X/%08lX: %s\n",
		    (ulong)keyid[1], lid, uhash[18], uhash[19],
		    (ulong)sigpkt->keyid[1], g10_errstr(rc) );
	sigrec->r.sig.sig[sigidx].flag = SIGF_CHECKED;
    }
    sigrec->dirty = 1;
}


/****************
 * Process a hintlist.
 * Fixme: this list is not anymore anchored to another
 *	  record, so it should be put elsewehere in case of an error
 * FIXME: add mod_up/down handling
 */
static void
process_hintlist( ulong hintlist, ulong hint_owner )
{
    ulong hlst_rn;
    int rc;

    for( hlst_rn = hintlist; hlst_rn; ) {
	TRUSTREC hlstrec;
	int hlst_idx;

	read_record( hlst_rn, &hlstrec, RECTYPE_HLST );

	for( hlst_idx=0; hlst_idx < ITEMS_PER_HLST_RECORD; hlst_idx++ ) {
	    TRUSTREC dirrec;
	    TRUSTREC uidrec;
	    TRUSTREC tmprec;
	    KBNODE keyblock = NULL;
	    u32 keyid[2];
	    ulong lid;
	    ulong r1, r2;

	    lid = hlstrec.r.hlst.rnum[hlst_idx];
	    if( !lid )
		continue;

	    read_record( lid, &dirrec, 0 );
	    /* make sure it points to a dir record:
	     * this should be true because it only makes sense to
	     * call this function if the dir record is available */
	    if( dirrec.rectype != RECTYPE_DIR )  {
		log_error(_("hintlist %lu[%d] of %lu "
			    "does not point to a dir record\n"),
			    hlst_rn, hlst_idx, hint_owner );
		continue;
	    }
	    if( !dirrec.r.dir.keylist ) {
		log_error(_("lid %lu does not have a key\n"), lid );
		continue;
	    }

	    /* get the keyblock */
	    read_record( dirrec.r.dir.keylist, &tmprec, RECTYPE_KEY );
	    rc = get_keyblock_byfprint( &keyblock,
					tmprec.r.key.fingerprint,
					tmprec.r.key.fingerprint_len );
	    if( rc ) {
		log_error(_("lid %lu: can't get keyblock: %s\n"),
						    lid, g10_errstr(rc) );
		continue;
	    }
	    keyid_from_fingerprint( tmprec.r.key.fingerprint,
				    tmprec.r.key.fingerprint_len, keyid );

	    /* Walk over all user ids and their signatures and check all
	     * the signature which are created by hint_owner */
	    for( r1 = dirrec.r.dir.uidlist; r1; r1 = uidrec.r.uid.next ) {
		TRUSTREC sigrec;

		read_record( r1, &uidrec, RECTYPE_UID );
		for( r2 = uidrec.r.uid.siglist; r2; r2 = sigrec.r.sig.next ) {
		    int i;

		    read_record( r2, &sigrec, RECTYPE_SIG );
		    sigrec.dirty = 0;
		    for(i=0; i < SIGS_PER_RECORD; i++ ) {
			if( !sigrec.r.sig.sig[i].lid )
			    continue; /* skip deleted sigs */
			if( sigrec.r.sig.sig[i].lid != hint_owner )
			    continue; /* not for us */
			/* some diagnostic messages */
			/* and do the signature check */
			check_hint_sig( lid, keyblock, keyid,
					uidrec.r.uid.namehash,
					&sigrec, i, hint_owner );
		    }
		    if( sigrec.dirty )
			write_record( &sigrec );
		}
	    }
	    release_kbnode( keyblock );
	} /* loop over hlst entries */

	/* delete this hlst record */
	hlst_rn = hlstrec.r.hlst.next;
	delete_record( hlstrec.recnum );
    } /* loop over hintlist */
}


/****************
 * Create or update shadow dir record and return the LID of the record
 */
static ulong
create_shadow_dir( PKT_signature *sig, ulong lid  )
{
    TRUSTREC sdir, hlst, tmphlst;
    ulong recno, newlid;
    int tmpidx=0; /* avoids gcc warnign - this is controlled by tmphlst */
    int rc;

    /* first see whether we already have such a record */
    rc = tdbio_search_sdir( sig->keyid, sig->pubkey_algo, &sdir );
    if( rc && rc != -1 ) {
	log_error(_("tdbio_search_dir failed: %s\n"), g10_errstr(rc));
	tdbio_invalid();
    }
    if( rc == -1 ) { /* not found: create */
	memset( &sdir, 0, sizeof sdir );
	sdir.recnum = tdbio_new_recnum();
	sdir.rectype= RECTYPE_SDIR;
	sdir.r.sdir.lid = sdir.recnum;
	sdir.r.sdir.keyid[0] = sig->keyid[0];
	sdir.r.sdir.keyid[1] = sig->keyid[1];
	sdir.r.sdir.pubkey_algo = sig->pubkey_algo;
	sdir.r.sdir.hintlist = 0;
	write_record( &sdir );
    }
    newlid = sdir.recnum;
    /* Put the record number into the hintlist.
     * (It is easier to use the lid and not the record number of the
     *	key to save some space (assuming that a signator has
     *	signed more than one user id - and it is easier to implement.)
     */
    tmphlst.recnum = 0;
    for( recno=sdir.r.sdir.hintlist; recno; recno = hlst.r.hlst.next) {
	int i;
	read_record( recno, &hlst, RECTYPE_HLST );
	for( i=0; i < ITEMS_PER_HLST_RECORD; i++ ) {
	    if( !hlst.r.hlst.rnum[i] ) {
		if( !tmphlst.recnum ) {
		    tmphlst = hlst;
		    tmpidx = i;
		}
	    }
	    else if( hlst.r.hlst.rnum[i] == lid )
		return newlid; /* the signature is already in the hintlist */
	}
    }
    /* not yet in the hint list, write it */
    if( tmphlst.recnum ) { /* we have an empty slot */
	tmphlst.r.hlst.rnum[tmpidx] = lid;
	write_record( &tmphlst );
    }
    else { /* must append a new hlst record */
	memset( &hlst, 0, sizeof hlst );
	hlst.recnum = tdbio_new_recnum();
	hlst.rectype = RECTYPE_HLST;
	hlst.r.hlst.next = sdir.r.sdir.hintlist;
	hlst.r.hlst.rnum[0] = lid;
	write_record( &hlst );
	sdir.r.sdir.hintlist = hlst.recnum;
	write_record( &sdir );
    }

    return newlid;
}


/****************
 * This function checks the given public key and inserts or updates
 * the keyrecord from the trustdb.  Revocation certificates
 * are handled here and the keybinding of subkeys is checked.
 * Hmmm: Should we check here, that the key has at least one valid
 * user ID or do we allow keys w/o user ID?
 *
 * keyblock points to the first node in the keyblock,
 * keynode is the node with the public key to check
 * (either primary or secondary), keyid is the keyid of
 * the primary key, drec is the directory record and recno_list
 * is a list used to keep track of visited records.
 * Existing keyflags are recalculated if recheck is true.
 */
static void
upd_key_record( KBNODE keyblock, KBNODE keynode, u32 *keyid,
		TRUSTREC *drec, RECNO_LIST *recno_list, int recheck )
{
    TRUSTREC krec;
    KBNODE  node;
    PKT_public_key *pk = keynode->pkt->pkt.public_key;
    ulong lid = drec->recnum;
    byte fpr[MAX_FINGERPRINT_LEN];
    size_t fprlen;
    ulong recno, newrecno;
    int keybind_seen = 0;
    int revoke_seen = 0;
    int rc;

    fingerprint_from_pk( pk, fpr, &fprlen );
    /* do we already have this key? */
    for( recno=drec->r.dir.keylist; recno; recno = krec.r.key.next ) {
	read_record( recno, &krec, RECTYPE_KEY );
	if( krec.r.key.fingerprint_len == fprlen
	    && !memcmp( krec.r.key.fingerprint, fpr, fprlen ) )
	    break;
    }
    if( recno ) { /* yes */
	ins_recno_list( recno_list, recno, RECTYPE_KEY );
    }
    else { /* no: insert this new key */
	recheck = 1; /* same as recheck */
	memset( &krec, 0, sizeof(krec) );
	krec.rectype = RECTYPE_KEY;
	krec.r.key.lid = lid;
	krec.r.key.pubkey_algo = pk->pubkey_algo;
	krec.r.key.fingerprint_len = fprlen;
	memcpy(krec.r.key.fingerprint, fpr, fprlen );
	krec.recnum = newrecno = tdbio_new_recnum();
	write_record( &krec );
	ins_recno_list( recno_list, newrecno, RECTYPE_KEY );
	/* and put this new record at the end of the keylist */
	if( !(recno=drec->r.dir.keylist) ) {
	    /* this is the first key */
	    drec->r.dir.keylist = newrecno;
	    drec->dirty = 1;
	}
	else { /* we already have a key, append the new one */
	    TRUSTREC save = krec;
	    for( ; recno; recno = krec.r.key.next )
		read_record( recno, &krec, RECTYPE_KEY );
	    krec.r.key.next = newrecno;
	    write_record( &krec );
	    krec = save;
	}
    }

    if( !recheck && (krec.r.key.keyflags & KEYF_CHECKED) )
	return;

    /* check keybindings and revocations */
    krec.r.key.keyflags = 0;
    if( keynode->pkt->pkttype == PKT_PUBLIC_KEY ) {
	/* we assume that a primary key is always valid
	 * and check later whether we have a revocation */
	krec.r.key.keyflags |= KEYF_CHECKED | KEYF_VALID;
    }

    for( node=keynode->next; node; node = node->next ) {
	PKT_signature *sig;

	if( node->pkt->pkttype == PKT_PUBLIC_SUBKEY )
	    break; /* ready (we check only one key at a time) */
	else if( node->pkt->pkttype != PKT_SIGNATURE )
	    continue;

	sig = node->pkt->pkt.signature;

	if( keyid[0] != sig->keyid[0] || keyid[1] != sig->keyid[1] )
	    continue; /* here we only care about a self-signatures */

	if( sig->sig_class == 0x18 && !keybind_seen ) { /* a keybinding */
	    if( keynode->pkt->pkttype == PKT_PUBLIC_KEY )
		continue; /* oops, ignore subkey binding on main key */

	    /* we check until we find a valid keybinding */
	    rc = check_key_signature( keyblock, node, NULL );
	    if( !rc ) {
		if( opt.verbose )
		    log_info(_(
			"key %08lX.%lu: Good subkey binding\n"),
			 (ulong)keyid_from_pk(pk,NULL), lid );
		krec.r.key.keyflags |= KEYF_CHECKED | KEYF_VALID;
	    }
	    else {
		log_info(_(
		  "key %08lX.%lu: Invalid subkey binding: %s\n"),
		    (ulong)keyid_from_pk(pk,NULL), lid, g10_errstr(rc) );
		krec.r.key.keyflags |= KEYF_CHECKED;
		krec.r.key.keyflags &= ~KEYF_VALID;
	    }
	    keybind_seen = 1;
	}
	else if( sig->sig_class == 0x20 && !revoke_seen ) {
	    if( keynode->pkt->pkttype == PKT_PUBLIC_SUBKEY )
		continue; /* a subkey is not expected here */

	    /* This is a key revocation certificate: check it */
	    rc = check_key_signature( keyblock, node, NULL );
	    if( !rc ) {
		if( opt.verbose )
		    log_info(_(
			"key %08lX.%lu: Valid key revocation\n"),
			 (ulong)keyid_from_pk(pk,NULL), lid );
		krec.r.key.keyflags |= KEYF_REVOKED;
	    }
	    else {
		log_info(_(
		  "key %08lX.%lu: Invalid key revocation: %s\n"),
		  (ulong)keyid_from_pk(pk,NULL), lid, g10_errstr(rc) );
	    }
	    revoke_seen = 1;
	}
	else if( sig->sig_class == 0x28 && !revoke_seen ) {
	    if( keynode->pkt->pkttype == PKT_PUBLIC_KEY )
		continue; /* a mainkey is not expected here */
	    /* This is a subkey revocation certificate: check it */
	    /* fixme: we should also check the revocation
	     * is newer than the key (OpenPGP) */
	    rc = check_key_signature( keyblock, node, NULL );
	    if( !rc ) {
		if( opt.verbose )
		    log_info(_(
			"key %08lX.%lu: Valid subkey revocation\n"),
			 (ulong)keyid_from_pk(pk,NULL), lid );
		krec.r.key.keyflags |= KEYF_REVOKED;
	    }
	    else {
		log_info(_(
		  "key %08lX.%lu: Invalid subkey binding: %s\n"),
		  (ulong)keyid_from_pk(pk,NULL), lid, g10_errstr(rc) );
	    }
	    revoke_seen = 1;
	}
    }

    write_record( &krec );
}


/****************
 * This function checks the given user ID and inserts or updates
 * the uid record of the trustdb.  Revocation certificates
 * are handled here.
 *
 * keyblock points to the first node in the keyblock,
 * uidnode is the node with the user id to check
 * keyid is the keyid of
 * the primary key, drec is the directory record and recno_list
 * is a list used to keep track of visited records.
 * Existing uidflags are recalculated if recheck is true.
 */
static void
upd_uid_record( KBNODE keyblock, KBNODE uidnode, u32 *keyid,
		TRUSTREC *drec, RECNO_LIST *recno_list,
		int recheck, int *mod_up, int *mod_down )
{
    ulong lid = drec->recnum;
    PKT_user_id *uid = uidnode->pkt->pkt.user_id;
    TRUSTREC urec;
    PKT_signature *selfsig = NULL;
    byte uidhash[20];
    KBNODE node;
    ulong recno, newrecno;
    int rc;

    if( DBG_TRUST )
	log_debug("upd_uid_record for %08lX/%02X%02X\n",
			       (ulong)keyid[1], uidhash[18], uidhash[19]);

    /* see whether we already have an uid record */
    rmd160_hash_buffer( uidhash, uid->name, uid->len );
    for( recno=drec->r.dir.uidlist; recno; recno = urec.r.uid.next ) {
	read_record( recno, &urec, RECTYPE_UID );
	if( !memcmp( uidhash, urec.r.uid.namehash, 20 ) )
	    break;
    }
    if( recno ) { /* we already have this record */
	ins_recno_list( recno_list, recno, RECTYPE_UID );
    }
    else { /* new user id */
	recheck = 1; /* insert is the same as a recheck */
	memset( &urec, 0 , sizeof(urec) );
	urec.rectype = RECTYPE_UID;
	urec.r.uid.lid = drec->recnum;
	memcpy(urec.r.uid.namehash, uidhash, 20 );
	urec.recnum = newrecno = tdbio_new_recnum();
	write_record( &urec );
	ins_recno_list( recno_list, newrecno, RECTYPE_UID );
	/* and put this new record at the end of the uidlist */
	if( !(recno=drec->r.dir.uidlist) ) { /* this is the first uid */
	    drec->r.dir.uidlist = newrecno;
	    drec->dirty = 1;
	}
	else { /* we already have an uid, append it to the list */
	    TRUSTREC save = urec;
	    for( ; recno; recno = urec.r.key.next )
		read_record( recno, &urec, RECTYPE_UID );
	    urec.r.uid.next = newrecno;
	    write_record( &urec );
	    urec = save;
	}
    }

    if( recheck || !(urec.r.uid.uidflags & UIDF_CHECKED) ) {
	unsigned orig_uidflags = urec.r.uid.uidflags;

	urec.r.uid.uidflags = 0;
	/* first check regular self signatures */
	for( node=uidnode->next; node; node = node->next ) {
	    PKT_signature *sig;

	    if( node->pkt->pkttype == PKT_USER_ID )
		break; /* ready */
	    if( node->pkt->pkttype == PKT_PUBLIC_SUBKEY )
		break; /* ready */
	    if( node->pkt->pkttype != PKT_SIGNATURE )
		continue;

	    sig = node->pkt->pkt.signature;

	    if( keyid[0] != sig->keyid[0] || keyid[1] != sig->keyid[1] )
		continue; /* not a self signature */

	    if( (sig->sig_class&~3) == 0x10 ) { /* regular self signature */
		rc = check_key_signature( keyblock, node, NULL );
		if( !rc ) {
		    if( opt.verbose )
			log_info( "uid %08lX.%lu/%02X%02X: %s\n",
			   (ulong)keyid[1], lid, uidhash[18], uidhash[19],
				  _("Good self-signature") );
		    urec.r.uid.uidflags |= UIDF_CHECKED | UIDF_VALID;
		    if( !selfsig )
			selfsig = sig; /* use the first valid sig */
		    else if( sig->timestamp > selfsig->timestamp
			     && sig->sig_class >= selfsig->sig_class )
			selfsig = sig; /* but this one is newer */
		}
		else {
		    log_info( "uid %08lX/%02X%02X: %s: %s\n",
			       (ulong)keyid[1], uidhash[18], uidhash[19],
			      _("Invalid self-signature"),
			       g10_errstr(rc) );
		    urec.r.uid.uidflags |= UIDF_CHECKED;
		}
	    }
	}

	/* and now check for revocations- we must do this after the
	 * self signature check because a selfsignature which is newer
	 * than a revocation makes the revocation invalid.
	 * Fixme: Is this correct - check with rfc2440
	 */
	for( node=uidnode->next; node; node = node->next ) {
	    PKT_signature *sig;

	    if( node->pkt->pkttype == PKT_USER_ID )
		break; /* ready */
	    if( node->pkt->pkttype == PKT_PUBLIC_SUBKEY )
		break; /* ready */
	    if( node->pkt->pkttype != PKT_SIGNATURE )
		continue;

	    sig = node->pkt->pkt.signature;

	    if( keyid[0] != sig->keyid[0] || keyid[1] != sig->keyid[1] )
		continue; /* not a self signature */

	    if( sig->sig_class == 0x30 ) { /* cert revocation */
		rc = check_key_signature( keyblock, node, NULL );
		if( !rc && selfsig && selfsig->timestamp > sig->timestamp ) {
		    log_info( "uid %08lX.%lu/%02X%02X: %s\n",
			   (ulong)keyid[1], lid, uidhash[18], uidhash[19],
			   _("Valid user ID revocation skipped "
			     "due to a newer self signature\n") );
		}
		else if( !rc ) {
		    if( opt.verbose )
			log_info( "uid %08lX.%lu/%02X%02X: %s\n",
			   (ulong)keyid[1], lid, uidhash[18], uidhash[19],
				 _("Valid user ID revocation\n") );
		    urec.r.uid.uidflags |= UIDF_CHECKED | UIDF_VALID;
		    urec.r.uid.uidflags |= UIDF_REVOKED;
		}
		else {
		    log_info("uid %08lX/%02X%02X: %s: %s\n",
				(ulong)keyid[1], uidhash[18], uidhash[19],
			       _("Invalid user ID revocation"),
							g10_errstr(rc) );
		}
	    }

	}

	if( orig_uidflags != urec.r.uid.uidflags ) {
	    write_record( &urec );
	    if(   !( urec.r.uid.uidflags & UIDF_VALID )
		|| ( urec.r.uid.uidflags & UIDF_REVOKED ) )
		*mod_down=1;
	    else
		*mod_up=1; /*(maybe a new user id)*/
	    /* Hmmm, did we catch changed expiration dates? */
	}

    } /* end check self-signatures */


    if( (urec.r.uid.uidflags & (UIDF_CHECKED|UIDF_VALID))
	!= (UIDF_CHECKED|UIDF_VALID) )
	return; /* user ID is not valid, so no need to check more things */

    /* check the preferences */
    if( selfsig )
	upd_pref_record( &urec, keyid, selfsig );

    /* Now we va check the certication signatures */
    for( node=uidnode->next; node; node = node->next ) {
	PKT_signature *sig;

	if( node->pkt->pkttype == PKT_USER_ID )
	    break; /* ready */
	if( node->pkt->pkttype == PKT_PUBLIC_SUBKEY )
	    break; /* ready */
	if( node->pkt->pkttype != PKT_SIGNATURE )
	    continue;

	sig = node->pkt->pkt.signature;

	if( keyid[0] == sig->keyid[0] || keyid[1] == sig->keyid[1] )
	    continue; /* here we skip the self-signatures */

	if( (sig->sig_class&~3) == 0x10 ) { /* regular certification */
	    upd_cert_record( keyblock, node, keyid, drec, recno_list,
			     recheck, &urec, uidhash, 0, mod_up, mod_down );
	}
	else if( sig->sig_class == 0x30 ) { /* cert revocation */
	    upd_cert_record( keyblock, node, keyid, drec, recno_list,
			     recheck, &urec, uidhash, 1, mod_up, mod_down );
	}
    } /* end check certificates */

    write_record( &urec );
}


static void
upd_pref_record( TRUSTREC *urec, u32 *keyid, PKT_signature *sig )
{
    static struct {
	sigsubpkttype_t subpkttype;
	int preftype;
    } ptable[] = {
	{ SIGSUBPKT_PREF_SYM,	PREFTYPE_SYM	},
	{ SIGSUBPKT_PREF_HASH,	PREFTYPE_HASH	},
	{ SIGSUBPKT_PREF_COMPR, PREFTYPE_COMPR	},
	{ 0, 0 }
    };
    TRUSTREC prec;
    ulong lid = urec->r.uid.lid ;
    const byte *uidhash = urec->r.uid.namehash;
    const byte *s;
    size_t n;
    int k, i;
    ulong recno;
    byte prefs_sig[200];
    int n_prefs_sig = 0;
    byte prefs_rec[200];
    int n_prefs_rec = 0;

    if( DBG_TRUST )
	log_debug("upd_pref_record for %08lX.%lu/%02X%02X\n",
			  (ulong)keyid[1], lid, uidhash[18], uidhash[19] );


    /* check for changed preferences */
    for(k=0; ptable[k].subpkttype; k++ ) {
	s = parse_sig_subpkt2( sig, ptable[k].subpkttype, &n );
	if( s ) {
	    for( ; n; n--, s++ ) {
		if( n_prefs_sig >= DIM(prefs_sig)-1 ) {
		    log_info("uid %08lX.%lu/%02X%02X: %s\n",
			      (ulong)keyid[1], lid, uidhash[18], uidhash[19],
			      _("Too many preferences") );
		    break;
		}
		prefs_sig[n_prefs_sig++] = ptable[k].preftype;
		prefs_sig[n_prefs_sig++] = *s;
	    }
	}
    }
    for( recno=urec->r.uid.prefrec; recno; recno = prec.r.pref.next ) {
	read_record( recno, &prec, RECTYPE_PREF );
	for(i = 0; i < ITEMS_PER_PREF_RECORD; i +=2 )  {
	    if( n_prefs_rec >= DIM(prefs_rec)-1 ) {
		log_info("uid %08lX.%lu/%02X%02X: %s\n",
			  (ulong)keyid[1], lid, uidhash[18], uidhash[19],
			  _("Too many preference items") );
		break;
	    }
	    if( prec.r.pref.data[i] ) {
		prefs_rec[n_prefs_rec++] = prec.r.pref.data[i];
		prefs_rec[n_prefs_rec++] = prec.r.pref.data[i+1];
	    }
	}
    }
    if( n_prefs_sig == n_prefs_rec
	&& !memcmp( prefs_sig, prefs_rec, n_prefs_sig ) )
	return;  /* not changed */

    /* Preferences have changed:  Delete all pref records
     * This is much simpler than checking whether we have to
     * do update the record at all - the record cache may care about it
     */
    for( recno=urec->r.uid.prefrec; recno; recno = prec.r.pref.next ) {
	read_record( recno, &prec, RECTYPE_PREF );
	delete_record( recno );
    }

    if( n_prefs_sig > ITEMS_PER_PREF_RECORD )
	 log_info(_("WARNING: can't yet handle long pref records\n"));

    memset( &prec, 0, sizeof prec );
    prec.recnum = tdbio_new_recnum();
    prec.rectype = RECTYPE_PREF;
    prec.r.pref.lid = lid;
    if( n_prefs_sig <= ITEMS_PER_PREF_RECORD )
	memcpy( prec.r.pref.data, prefs_sig, n_prefs_sig );
    else { /* need more than one pref record */
	TRUSTREC tmp;
	ulong nextrn;
	byte *pp = prefs_sig;

	n = n_prefs_sig;
	memcpy( prec.r.pref.data, pp, ITEMS_PER_PREF_RECORD );
	n -= ITEMS_PER_PREF_RECORD;
	pp += ITEMS_PER_PREF_RECORD;
	nextrn = prec.r.pref.next = tdbio_new_recnum();
	do {
	    memset( &tmp, 0, sizeof tmp );
	    tmp.recnum = nextrn;
	    tmp.rectype = RECTYPE_PREF;
	    tmp.r.pref.lid = lid;
	    if( n <= ITEMS_PER_PREF_RECORD ) {
		memcpy( tmp.r.pref.data, pp, n );
		n = 0;
	    }
	    else {
		memcpy( tmp.r.pref.data, pp, ITEMS_PER_PREF_RECORD );
		n -= ITEMS_PER_PREF_RECORD;
		pp += ITEMS_PER_PREF_RECORD;
		nextrn = tmp.r.pref.next = tdbio_new_recnum();
	    }
	    write_record( &tmp );
	} while( n );
    }
    write_record( &prec );
    urec->r.uid.prefrec = prec.recnum;
    urec->dirty = 1;
}


static void
upd_cert_record( KBNODE keyblock, KBNODE signode, u32 *keyid,
		 TRUSTREC *drec, RECNO_LIST *recno_list, int recheck,
		 TRUSTREC *urec, const byte *uidhash, int revoked,
		 int *mod_up, int *mod_down )
{
    /* We simply insert the signature into the sig records but
     * avoid duplicate ones.  We do not check them here because
     * there is a big chance, that we import required public keys
     * later.  The problem with this is that we must somewhere store
     * the information about this signature (we need a record id).
     * We do this by using the record type shadow dir, which will
     * be converted to a dir record as when the missing public key
     * gets inserted into the trustdb.
     */
    ulong lid = drec->recnum;
    PKT_signature *sig = signode->pkt->pkt.signature;
    TRUSTREC rec;
    ulong recno;
    TRUSTREC delrec;
    int delrecidx=0;
    int newflag = 0;
    ulong newlid = 0;
    ulong pk_lid = 0;
    int found_sig = 0;
    int found_delrec = 0;
    int rc;


    if( DBG_TRUST )
	log_debug("upd_cert_record for %08lX.?/%02X%02X/%08lX\n",
			      (ulong)keyid[1], uidhash[18],
			       uidhash[19], (ulong)sig->keyid[1] );

    delrec.recnum = 0;

    /* get the LID of the pubkey of the signature under verification */
    pk_lid = lid_from_keyid( sig->keyid );

    /* Loop over all signatures just in case one is not correctly
     * marked.	If we see the correct signature, set a flag.
     * delete duplicate signatures (should not happen but...) */
    for( recno = urec->r.uid.siglist; recno; recno = rec.r.sig.next ) {
	int i;

	read_record( recno, &rec, RECTYPE_SIG );
	for(i=0; i < SIGS_PER_RECORD; i++ ) {
	    TRUSTREC tmp;
	    if( !rec.r.sig.sig[i].lid ) {
		/* (remember this unused slot) */
		if( !found_delrec && !delrec.recnum ) {
		    delrec = rec;
		    delrecidx = i;
		    found_delrec=1;
		}
		continue; /* skip unused slots */
	    }

	    if( rec.r.sig.sig[i].lid == pk_lid ) {
		if( found_sig ) {
		    log_info( "sig %08lX.%lu/%02X%02X/%08lX: %s\n",
			      (ulong)keyid[1], lid, uidhash[18],
			       uidhash[19], (ulong)sig->keyid[1],
			     _("duplicated certificate - deleted") );
		    rec.r.sig.sig[i].lid = 0;
		    rec.dirty = 1;
		    continue;
		}
		found_sig = 1;
	    }
	    if( !recheck && !revoked && (rec.r.sig.sig[i].flag & SIGF_CHECKED))
		continue; /* we already checked this signature */
	    if( !recheck && (rec.r.sig.sig[i].flag & SIGF_NOPUBKEY) )
		continue; /* we do not have the public key */

	    read_record( rec.r.sig.sig[i].lid, &tmp, 0 );
	    if( tmp.rectype == RECTYPE_DIR ) {
		/* the public key is in the trustdb: check sig */
		rc = check_key_signature( keyblock, signode, NULL );
		if( !rc ) { /* valid signature */
		    if( opt.verbose )
			log_info("sig %08lX.%lu/%02X%02X/%08lX: %s\n",
				(ulong)keyid[1], lid, uidhash[18],
				uidhash[19], (ulong)sig->keyid[1],
				revoked? _("Valid certificate revocation")
				       : _("Good certificate") );
		    rec.r.sig.sig[i].flag = SIGF_CHECKED | SIGF_VALID;
		    if( revoked ) { /* we are investigating revocations */
			rec.r.sig.sig[i].flag |= SIGF_REVOKED;
			*mod_down = 1;
		    }
		    else
			*mod_up = 1;
		}
		else if( rc == G10ERR_NO_PUBKEY ) {
		    /* This may happen if the key is still in the trustdb
		     * but not available in the keystorage */
		    if( (rec.r.sig.sig[i].flag & SIGF_CHECKED) )
			log_info("sig %08lX.%lu/%02X%02X/%08lX: %s\n",
				  (ulong)keyid[1], lid, uidhash[18],
				 uidhash[19], (ulong)sig->keyid[1],
				 _("public key not anymore available") );
		    rec.r.sig.sig[i].flag = SIGF_NOPUBKEY;
		    *mod_down = 1;
		    if( revoked )
			rec.r.sig.sig[i].flag |= SIGF_REVOKED;
		}
		else {
		    log_info("sig %08lX.%lu/%02X%02X/%08lX: %s: %s\n",
				(ulong)keyid[1], lid, uidhash[18],
				uidhash[19], (ulong)sig->keyid[1],
				revoked? _("Invalid certificate revocation")
				       : _("Invalid certificate"),
						    g10_errstr(rc));
		    rec.r.sig.sig[i].flag = SIGF_CHECKED;
		    if( revoked ) {
			rec.r.sig.sig[i].flag |= SIGF_REVOKED;
			*mod_down = 1;
		    }
		}
		rec.dirty = 1;
	    }
	    else if( tmp.rectype == RECTYPE_SDIR ) {
		/* must check that it is the right one */
		if( tmp.r.sdir.keyid[0] == sig->keyid[0]
		    && tmp.r.sdir.keyid[1] == sig->keyid[1]
		    && (!tmp.r.sdir.pubkey_algo
			 || tmp.r.sdir.pubkey_algo == sig->pubkey_algo )) {
		    if( !(rec.r.sig.sig[i].flag & SIGF_NOPUBKEY) )
			log_info(_("uid %08lX.%lu/%02X%02X: "
				"has shadow dir %lu but is not yet marked.\n"),
				(ulong)keyid[1], lid,
				uidhash[18], uidhash[19], tmp.recnum );
		    rec.r.sig.sig[i].flag = SIGF_NOPUBKEY;
		    if( revoked )
			rec.r.sig.sig[i].flag |= SIGF_REVOKED;
		    rec.dirty = 1;
		    /* fixme: should we verify that the record is
		     * in the hintlist? - This case here should anyway
		     * never occur */
		}
	    }
	    else {
		log_error(_("sig record %lu[%d] points to wrong record.\n"),
			    rec.r.sig.sig[i].lid, i );
		tdbio_invalid();
	    }
	}
	if( found_delrec && delrec.recnum ) {
	    delrec = rec;
	    found_delrec = 0; /* we only want the first one */
	}
	if( rec.dirty ) {
	    write_record( &rec );
	    rec.dirty = 0;
	}
    }

    if( found_sig )
	return;

    /* at this point, we have verified, that the signature is not in
     * our list of signatures.	Add a new record with that signature
     * and if the public key is there, check the signature. */

    if( !pk_lid ) /* we have already seen that there is no pubkey */
	rc = G10ERR_NO_PUBKEY;
    else
	rc = check_key_signature( keyblock, signode, NULL );

    if( !rc ) { /* valid signature */
	if( opt.verbose )
	    log_info("sig %08lX.%lu/%02X%02X/%08lX: %s\n",
			  (ulong)keyid[1], lid, uidhash[18],
			   uidhash[19], (ulong)sig->keyid[1],
				revoked? _("Valid certificate revocation")
				       : _("Good certificate") );
	newlid = pk_lid;  /* this is the pk of the signature */
	newflag = SIGF_CHECKED | SIGF_VALID;
	if( revoked ) {
	    newflag |= SIGF_REVOKED;
	    *mod_down = 1;
	}
	else
	    *mod_up = 1;
    }
    else if( rc == G10ERR_NO_PUBKEY ) {
	if( opt.verbose > 1 || DBG_TRUST )
	    log_info("sig %08lX.%lu/%02X%02X/%08lX: %s\n",
		     (ulong)keyid[1], lid, uidhash[18],
		      uidhash[19], (ulong)sig->keyid[1], g10_errstr(rc) );
	newlid = create_shadow_dir( sig, lid );
	newflag = SIGF_NOPUBKEY;
	if( revoked )
	    newflag |= SIGF_REVOKED;
    }
    else {
	log_info( "sig %08lX.%lu/%02X%02X/%08lX: %s: %s\n",
		    (ulong)keyid[1], lid, uidhash[18], uidhash[19],
			      (ulong)sig->keyid[1],
		revoked? _("Invalid certificate revocation")
		       : _("Invalid certificate"),
					    g10_errstr(rc));
	newlid = create_shadow_dir( sig, lid );
	newflag = SIGF_CHECKED;
	if( revoked )
	    newflag |= SIGF_REVOKED;
	*mod_down = 1;
    }

    if( delrec.recnum ) { /* we can reuse an unused slot */
	delrec.r.sig.sig[delrecidx].lid = newlid;
	delrec.r.sig.sig[delrecidx].flag= newflag;
	write_record( &delrec );
    }
    else { /* we must insert a new sig record */
	TRUSTREC tmp;

	memset( &tmp, 0, sizeof tmp );
	tmp.recnum = tdbio_new_recnum();
	tmp.rectype = RECTYPE_SIG;
	tmp.r.sig.lid = lid;
	tmp.r.sig.next = urec->r.uid.siglist;
	tmp.r.sig.sig[0].lid = newlid;
	tmp.r.sig.sig[0].flag= newflag;
	write_record( &tmp );
	urec->r.uid.siglist = tmp.recnum;
	urec->dirty = 1;
    }
}


/****************
 * Update all the info from the public keyblock.
 * The key must already exist in the keydb.
 * This function is responsible for checking the signatures in cases
 * where the public key is already available.  If we do not have the public
 * key, the check is done by some special code in insert_trust_record().
 */
int
update_trust_record( KBNODE keyblock, int recheck, int *modified )
{
    PKT_public_key *primary_pk;
    KBNODE node;
    TRUSTREC drec;
    TRUSTREC krec;
    TRUSTREC urec;
    TRUSTREC prec;
    TRUSTREC helprec;
    int rc = 0;
    u32 keyid[2]; /* keyid of primary key */
    ulong recno, lastrecno;
    int mod_up = 0;
    int mod_down = 0;
    RECNO_LIST recno_list = NULL; /* list of verified records */
    /* fixme: replace recno_list by a lookup on node->recno */

    if( opt.dry_run )
	return 0;

    init_trustdb();
    if( modified )
	*modified = 0;

    node = find_kbnode( keyblock, PKT_PUBLIC_KEY );
    primary_pk = node->pkt->pkt.public_key;
    rc = get_dir_record( primary_pk, &drec );
    if( rc )
	return rc;
    if( !primary_pk->local_id )
	primary_pk->local_id = drec.recnum;

    keyid_from_pk( primary_pk, keyid );

    /* fixme: check that the keyblock has a valid structure */

    rc = tdbio_begin_transaction();
    if( rc )
	return rc;

    /* update the keys */
    for( node=keyblock; node; node = node->next ) {
	if( node->pkt->pkttype == PKT_PUBLIC_KEY
	    || node->pkt->pkttype == PKT_PUBLIC_SUBKEY )
	    upd_key_record( keyblock, node, keyid,
			    &drec, &recno_list, recheck );
    }
    /* update the user IDs */
    for( node=keyblock; node; node = node->next ) {
	if( node->pkt->pkttype == PKT_USER_ID )
	    upd_uid_record( keyblock, node, keyid,
			    &drec, &recno_list, recheck, &mod_up, &mod_down );
    }

    /* delete keyrecords from the trustdb which are not anymore used */
    /* should we really do this, or is it better to keep them and */
    /* mark as unused? */
    /* And set the revocation flag into the dir record */
    drec.r.dir.dirflags &= ~DIRF_REVOKED;
    lastrecno = 0;
    for( recno=drec.r.dir.keylist; recno; recno = krec.r.key.next ) {
	read_record( recno, &krec, RECTYPE_KEY );
	if( recno == drec.r.dir.keylist ) { /* this is the primary key */
	    if( (krec.r.key.keyflags & KEYF_REVOKED) ) {
		drec.r.dir.dirflags |= DIRF_REVOKED;
		drec.dirty = 1;
	    }
	}

	if( !qry_recno_list( recno_list, recno, RECTYPE_KEY ) ) {
	    /* delete this one */
	    if( !lastrecno ) {
		drec.r.dir.keylist = krec.r.key.next;
		drec.dirty = 1;
	    }
	    else {
		read_record( lastrecno, &helprec, RECTYPE_KEY );
		helprec.r.key.next = krec.r.key.next;
		write_record( &helprec );
	    }
	    delete_record( recno );
	}
	else
	    lastrecno = recno;
    }
    /* delete uid records and sig and their pref records from the
     * trustdb which are not anymore used */
    lastrecno = 0;
    for( recno=drec.r.dir.uidlist; recno; recno = urec.r.uid.next ) {
	read_record( recno, &urec, RECTYPE_UID );
	if( !qry_recno_list( recno_list, recno, RECTYPE_UID ) ) {
	    ulong r2;
	    /* delete this one */
	    if( !lastrecno ) {
		drec.r.dir.uidlist = urec.r.uid.next;
		drec.dirty = 1;
	    }
	    else {
		read_record( lastrecno, &helprec, RECTYPE_UID );
		helprec.r.uid.next = urec.r.uid.next;
		write_record( &helprec );
	    }
	    for(r2=urec.r.uid.prefrec ; r2; r2 = prec.r.pref.next ) {
		read_record( r2, &prec, RECTYPE_PREF );
		delete_record( r2 );
	    }
	    for(r2=urec.r.uid.siglist ; r2; r2 = helprec.r.sig.next ) {
		read_record( r2, &helprec, RECTYPE_SIG );
		delete_record( r2 );
	    }
	    delete_record( recno );
	}
	else
	    lastrecno = recno;
    }



    if( rc )
	rc = tdbio_cancel_transaction();
    else {
	if( modified && tdbio_is_dirty() )
	    *modified = 1;
	drec.r.dir.dirflags |= DIRF_CHECKED;
	drec.r.dir.valcheck = 0;
	write_record( &drec );
	tdbio_write_modify_stamp( mod_up, mod_down );
	rc = tdbio_end_transaction();
    }
    rel_recno_list( &recno_list );
    return rc;
}


/****************
 * Insert a trust record into the TrustDB
 * This function assumes that the record does not yet exist.
 */
int
insert_trust_record( PKT_public_key *orig_pk )
{
    TRUSTREC dirrec;
    TRUSTREC shadow;
    KBNODE keyblock = NULL;
    KBNODE node;
    byte fingerprint[MAX_FINGERPRINT_LEN];
    size_t fingerlen;
    int rc = 0;
    ulong hintlist = 0;
    PKT_public_key *pk;


    if( opt.dry_run )
	return 0;

    init_trustdb();

    fingerprint_from_pk( orig_pk, fingerprint, &fingerlen );

    /* fixme: assert that we do not have this record.
     * we can do this by searching for the primary keyid
     *
     * fixme: If there is no such key we should look whether one
     * of the subkeys has been used to sign another key and in this case
     * we got the key anyway - this is because a secondary key can't be used
     * without a primary key (it is needed to bind the secondary one
     * to the primary one which has the user ids etc.)
     */

    if( orig_pk->local_id )
	log_debug("insert_trust_record with pk->local_id=%lu (1)\n",
						   orig_pk->local_id );

    /* get the keyblock which has the key */
    rc = get_keyblock_byfprint( &keyblock, fingerprint, fingerlen );
    if( rc ) { /* that should never happen */
	log_error( _("insert_trust_record: keyblock not found: %s\n"),
							  g10_errstr(rc) );
	goto leave;
    }

    /* make sure that we use the primary key */
    pk = find_kbnode( keyblock, PKT_PUBLIC_KEY )->pkt->pkt.public_key;

    if( pk->local_id ) {
	orig_pk->local_id = pk->local_id;
	log_debug("insert_trust_record with pk->local_id=%lu (2)\n",
							pk->local_id );
	rc = update_trust_record( keyblock, 1, NULL );
	release_kbnode( keyblock );
	return rc;
    }

    /* We have to look for a shadow dir record which must be reused
     * as the dir record. And: check all signatures which are listed
     * in the hintlist of the shadow dir record.
     */
    rc = tdbio_search_sdir( pk->keyid, pk->pubkey_algo, &shadow );
    if( rc && rc != -1 ) {
	log_error(_("tdbio_search_dir failed: %s\n"), g10_errstr(rc));
	tdbio_invalid();
    }
    memset( &dirrec, 0, sizeof dirrec );
    dirrec.rectype = RECTYPE_DIR;
    if( !rc ) {
	/* hey, great: this key has already signed other keys
	 * convert this to a real directory entry */
	hintlist = shadow.r.sdir.hintlist;
	dirrec.recnum = shadow.recnum;
    }
    else {
	dirrec.recnum = tdbio_new_recnum();
    }
    dirrec.r.dir.lid = dirrec.recnum;
    write_record( &dirrec );

    /* out the LID into the keyblock */
    pk->local_id = dirrec.r.dir.lid;
    orig_pk->local_id = dirrec.r.dir.lid;
    for( node=keyblock; node; node = node->next ) {
	if( node->pkt->pkttype == PKT_PUBLIC_KEY
	    || node->pkt->pkttype == PKT_PUBLIC_SUBKEY ) {
	    PKT_public_key *a_pk = node->pkt->pkt.public_key;
	    a_pk->local_id = dirrec.r.dir.lid;
	}
	else if( node->pkt->pkttype == PKT_SIGNATURE ) {
	    PKT_signature *a_sig = node->pkt->pkt.signature;
	    a_sig->local_id = dirrec.r.dir.lid;
	}
    }

    /* mark tdb as modified upwards */
    tdbio_write_modify_stamp( 1, 0 );

    /* and put all the other stuff into the keydb */
    rc = update_trust_record( keyblock, 1, NULL );
    if( !rc )
	process_hintlist( hintlist, dirrec.r.dir.lid );

  leave:
    if( rc && hintlist )
	; /* fixme: the hintlist is not anymore anchored */
    release_kbnode( keyblock );
    do_sync();
    return rc;
}




/***********************************************
 *********  Trust calculation  *****************
 ***********************************************/

/****************
 * Find all certification paths of a given LID.
 * Limit the search to MAX_DEPTH.  stack is a helper variable which
 * should have been allocated with size max_depth, stack[0] should
 * be setup to the key we are investigating, so the minimal depth
 * we should ever see in this function is 1.
 * Returns: a new tree
 * certchain_set must be a valid set or point to NULL; this function
 * may modifiy it.
 *
 * Fixme: add a fastscan mode which stops ad valid validity nodes.
 */
static TN
build_cert_tree( ulong lid, int depth, int max_depth, TN helproot )
{
    TRUSTREC dirrec;
    TRUSTREC uidrec;
    ulong uidrno;
    TN keynode;

    if( depth >= max_depth )
	return NULL;

    keynode = new_tn();
    if( !helproot )
	helproot = keynode;
    keynode->lid = lid;
    if( !qry_lid_table_flag( ultikey_table, lid, NULL ) ) {
	/* this is an ultimately trusted key;
	 * which means that we have found the end of the chain:
	 * We do this here prior to reading the dir record
	 * because we don't really need the info from that record */
	keynode->n.k.ownertrust = TRUST_ULTIMATE;
	keynode->n.k.buckstop	= 1;
	return keynode;
    }
    read_record( lid, &dirrec, 0 );
    if( dirrec.rectype != RECTYPE_DIR ) {
	if( dirrec.rectype != RECTYPE_SDIR )
	    log_debug("lid %lu, has rectype %d"
		      " - skipped\n", lid, dirrec.rectype );
	m_free(keynode);
	return NULL;
    }
    keynode->n.k.ownertrust = dirrec.r.dir.ownertrust;

    /* loop over all user ids */
    for( uidrno = dirrec.r.dir.uidlist; uidrno; uidrno = uidrec.r.uid.next ) {
	TRUSTREC sigrec;
	ulong sigrno;
	TN uidnode = NULL;

	read_record( uidrno, &uidrec, RECTYPE_UID );

	if( !(uidrec.r.uid.uidflags & UIDF_CHECKED) )
	    continue; /* user id has not been checked */
	if( !(uidrec.r.uid.uidflags & UIDF_VALID) )
	    continue; /* user id is not valid */
	if( (uidrec.r.uid.uidflags & UIDF_REVOKED) )
	    continue; /* user id has been revoked */

	/* loop over all signature records */
	for(sigrno=uidrec.r.uid.siglist; sigrno; sigrno = sigrec.r.sig.next ) {
	    int i;
	    TN tn;

	    read_record( sigrno, &sigrec, RECTYPE_SIG );

	    for(i=0; i < SIGS_PER_RECORD; i++ ) {
		if( !sigrec.r.sig.sig[i].lid )
		    continue; /* skip deleted sigs */
		if( !(sigrec.r.sig.sig[i].flag & SIGF_CHECKED) )
		    continue; /* skip unchecked signatures */
		if( !(sigrec.r.sig.sig[i].flag & SIGF_VALID) )
		    continue; /* skip invalid signatures */
		if( (sigrec.r.sig.sig[i].flag & SIGF_EXPIRED) )
		    continue; /* skip expired signatures */
		if( (sigrec.r.sig.sig[i].flag & SIGF_REVOKED) )
		    continue; /* skip revoked signatures */
		/* check for cycles */
		for( tn=keynode; tn && tn->lid != sigrec.r.sig.sig[i].lid;
							  tn = tn->back )
		    ;
		if( tn )
		    continue; /* cycle found */

		tn = build_cert_tree( sigrec.r.sig.sig[i].lid,
				      depth+1, max_depth, helproot );
		if( !tn )
		    continue; /* cert chain too deep or error */

		if( !uidnode ) {
		    uidnode = new_tn();
		    uidnode->back = keynode;
		    uidnode->lid = uidrno;
		    uidnode->is_uid = 1;
		    uidnode->next = keynode->list;
		    keynode->list = uidnode;
		}

		tn->back = uidnode;
		tn->next = uidnode->list;
		uidnode->list = tn;
	      #if 0 /* optimazation - fixme: reenable this later */
		if( tn->n.k.buckstop ) {
		    /* ultimately trusted key found:
		     * no need to check more signatures of this uid */
		    sigrec.r.sig.next = 0;
		    break;
		}
	      #endif
	    }
	} /* end loop over sig recs */
    } /* end loop over user ids */

    if( !keynode->list ) {
	release_tn_tree( keynode );
	keynode = NULL;
    }

    return keynode;
}


static void
upd_one_ownertrust( ulong lid, unsigned new_trust, unsigned *retflgs )
{
    TRUSTREC rec;

    read_record( lid, &rec, RECTYPE_DIR );
    if( DBG_TRUST )
	log_debug("upd_one_ownertrust of %lu from %u to %u\n",
			   lid, (unsigned)rec.r.dir.ownertrust, new_trust );
    if( retflgs ) {
	if( new_trust > rec.r.dir.ownertrust )
	    *retflgs |= 16; /* modified up */
	else
	    *retflgs |= 32; /* modified down */
    }
    rec.r.dir.ownertrust = new_trust;
    write_record( &rec );
}

/****************
 * Update the ownertrust in the complete tree.
 */
static void
propagate_ownertrust( TN kr, ulong lid, unsigned trust )
{
    TN ur;

    for( ; kr; kr = kr->next ) {
	if( kr->lid == lid )
	    kr->n.k.ownertrust = trust;
	for( ur=kr->list; ur; ur = ur->next )
	    propagate_ownertrust( ur->list, lid, trust );
    }
}

/****************
 * Calculate the validity of all keys in the tree and especially
 * the one of the top key.  If add_fnc is not NULL, it is used to
 * ask for missing ownertrust values (but only if this will help
 * us to increase the validity.
 * add_fnc is expected to take the LID of the key under question
 * and return a ownertrust value or an error:  positive values
 * are assumed to be the new ownertrust value; a 0 does mean no change,
 * a -1 is a request to cancel this validation procedure, a -2 requests
 * a listing of the sub-tree using the tty functions.
 *
 *
 * Returns: 0 = okay
 */
static int
propagate_validity( TN root, TN node, int (*add_fnc)(ulong), unsigned *retflgs )
{
    TN kr, ur;
    int max_validity = 0;

    assert( !node->is_uid );
    if( node->n.k.ownertrust == TRUST_ULTIMATE ) {
	/* this is one of our keys */
	assert( !node->list ); /* it should be a leaf */
	node->n.k.validity = TRUST_ULTIMATE;
	if( retflgs )
	    *retflgs |= 1;  /* found a path to an ultimately trusted key */
	return 0;
    }

    /* loop over all user ids */
    for( ur=node->list; ur; ur = ur->next ) {
	assert( ur->is_uid );
	/* loop over all signators */
	for(kr=ur->list; kr; kr = kr->next ) {
	    if( propagate_validity( root, kr, add_fnc, retflgs ) )
		return -1; /* quit */
	    if( kr->n.k.validity == TRUST_ULTIMATE ) {
		ur->n.u.fully_count = opt.completes_needed;
	    }
	    else if( kr->n.k.validity == TRUST_FULLY ) {
		if( add_fnc && !kr->n.k.ownertrust ) {
		    int rc;

		    if( retflgs )
			*retflgs |= 2; /* found key with undefined ownertrust*/
		    do {
			rc = add_fnc( kr->lid );
			switch( rc ) {
			  case TRUST_NEVER:
			  case TRUST_MARGINAL:
			  case TRUST_FULLY:
			    propagate_ownertrust( root, kr->lid, rc );
			    upd_one_ownertrust( kr->lid, rc, retflgs );
			    if( retflgs )
				*retflgs |= 4; /* changed */
			    break;
			  case -1:
			    return -1; /* cancel */
			  case -2:
			    dump_tn_tree( NULL, 0, kr );
			    tty_printf("\n");
			    break;
			  default:
			    break;
			}
		    } while( rc == -2 );
		}
		if( kr->n.k.ownertrust == TRUST_FULLY )
		    ur->n.u.fully_count++;
		else if( kr->n.k.ownertrust == TRUST_MARGINAL )
		    ur->n.u.marginal_count++;
	    }
	}
	/* fixme: We can move this test into the loop to stop as soon as
	 * we have a level of FULLY and return from this function
	 * We dont do this now to get better debug output */
	if( ur->n.u.fully_count >= opt.completes_needed
	    || ur->n.u.marginal_count >= opt.marginals_needed )
	    ur->n.u.validity = TRUST_FULLY;
	else if( ur->n.u.fully_count || ur->n.u.marginal_count )
	    ur->n.u.validity = TRUST_MARGINAL;

	if( ur->n.u.validity >= max_validity )
	    max_validity = ur->n.u.validity;
    }

    node->n.k.validity = max_validity;
    return 0;
}



/****************
 * Given the directory record of a key, check whether we can
 * find a path to an ultimately trusted key.  We do this by
 * checking all key signatures up to a some depth.
 */
static int
verify_key( int max_depth, TRUSTREC *drec, const char *namehash,
			    int (*add_fnc)(ulong), unsigned *retflgs )
{
    TN tree;
    int keytrust;
    int pv_result;

    tree = build_cert_tree( drec->r.dir.lid, 0, opt.max_cert_depth, NULL );
    if( !tree )
	return TRUST_UNDEFINED;
    pv_result = propagate_validity( tree, tree, add_fnc, retflgs );
    if( namehash ) {
	/* find the matching user id.
	 * fixme: the way we handle this is too inefficient */
	TN ur;
	TRUSTREC rec;

	keytrust = 0;
	for( ur=tree->list; ur; ur = ur->next ) {
	    read_record( ur->lid, &rec, RECTYPE_UID );
	    if( !memcmp( namehash, rec.r.uid.namehash, 20 ) ) {
		keytrust = ur->n.u.validity;
		break;
	    }
	}
    }
    else
	keytrust = tree->n.k.validity;

    /* update the cached validity values */
    if( !pv_result
	&& keytrust >= TRUST_UNDEFINED
	&& tdbio_db_matches_options()
	&& ( !drec->r.dir.valcheck || drec->r.dir.validity != keytrust ) ) {
	TN ur;
	TRUSTREC rec;

	for( ur=tree->list; ur; ur = ur->next ) {
	    read_record( ur->lid, &rec, RECTYPE_UID );
	    if( rec.r.uid.validity != ur->n.u.validity ) {
		rec.r.uid.validity = ur->n.u.validity;
		write_record( &rec );
	    }
	}

	drec->r.dir.validity = tree->n.k.validity;
	drec->r.dir.valcheck = make_timestamp();
	write_record( drec );
	do_sync();
    }

    release_tn_tree( tree );
    return keytrust;
}


/****************
 * we have the pubkey record and all needed informations are in the trustdb
 * but nothing more is known.
 */
static int
do_check( TRUSTREC *dr, unsigned *validity,
	  const char *namehash, int (*add_fnc)(ulong), unsigned *retflgs )
{
    if( !dr->r.dir.keylist ) {
	log_error(_("Ooops, no keys\n"));
	return G10ERR_TRUSTDB;
    }
    if( !dr->r.dir.uidlist ) {
	log_error(_("Ooops, no user ids\n"));
	return G10ERR_TRUSTDB;
    }

    if( retflgs )
	*retflgs &= ~(16|32);  /* reset the 2 special flags */


    if( namehash ) {
	/* Fixme: use the cache */
	*validity = verify_key( opt.max_cert_depth, dr, namehash,
							add_fnc, retflgs );
    }
    else if( !add_fnc
	&& tdbio_db_matches_options()
	&& dr->r.dir.valcheck
	    > tdbio_read_modify_stamp( (dr->r.dir.validity < TRUST_FULLY) )
	&& dr->r.dir.validity )
	*validity = dr->r.dir.validity;
    else
	*validity = verify_key( opt.max_cert_depth, dr, NULL,
							add_fnc, retflgs );

    if( !(*validity & TRUST_MASK) )
	*validity = TRUST_UNDEFINED;

    if( dr->r.dir.dirflags & DIRF_REVOKED )
	*validity |= TRUST_FLAG_REVOKED;

    /* If we have changed some ownertrusts, set the trustdb timestamps
     * and do a sync */
    if( retflgs && (*retflgs & (16|32)) ) {
	tdbio_write_modify_stamp( (*retflgs & 16), (*retflgs & 32) );
	do_sync();
    }


    return 0;
}



/***********************************************
 *********  Change trustdb values **************
 ***********************************************/

int
update_ownertrust( ulong lid, unsigned new_trust )
{
    TRUSTREC rec;

    init_trustdb();
    read_record( lid, &rec, RECTYPE_DIR );
    if( DBG_TRUST )
	log_debug("update_ownertrust of %lu from %u to %u\n",
			   lid, (unsigned)rec.r.dir.ownertrust, new_trust );
    rec.r.dir.ownertrust = new_trust;
    write_record( &rec );
    do_sync();
    return 0;
}


int
clear_trust_checked_flag( PKT_public_key *pk )
{
    TRUSTREC rec;
    int rc;

    if( opt.dry_run )
	return 0;

    init_trustdb();
    rc = get_dir_record( pk, &rec );
    if( rc )
	return rc;

    /* check whether they are already reset */
    if( !(rec.r.dir.dirflags & DIRF_CHECKED) && !rec.r.dir.valcheck )
	return 0;

    /* reset the flag */
    rec.r.dir.dirflags &= ~DIRF_CHECKED;
    rec.r.dir.valcheck = 0;
    write_record( &rec );
    do_sync();
    return 0;
}


/****************
 * Put new entries  from the pubrings into the trustdb.
 * This function honors the sig flags to speed up the check.
 */
void
update_trustdb( )
{
    KBNODE keyblock = NULL;
    KBPOS kbpos;
    int rc;

    if( opt.dry_run )
	return;

    init_trustdb();
    rc = enum_keyblocks( 0, &kbpos, &keyblock );
    if( !rc ) {
	ulong count=0, upd_count=0, err_count=0, new_count=0;

	while( !(rc = enum_keyblocks( 1, &kbpos, &keyblock )) ) {
	    int modified;

	    rc = update_trust_record( keyblock, 1, &modified );
	    if( rc == -1 ) { /* not yet in trustdb: insert */
		PKT_public_key *pk = keyblock->pkt->pkt.public_key;
		rc = insert_trust_record( pk );
		if( rc && !pk->local_id ) {
		    log_error(_("lid ?: insert failed: %s\n"),
						     g10_errstr(rc) );
		    err_count++;
		}
		else if( rc ) {
		    log_error(_("lid %lu: insert failed: %s\n"),
				       pk->local_id, g10_errstr(rc) );
		    err_count++;
		}
		else {
		    if( opt.verbose )
			log_info(_("lid %lu: inserted\n"), pk->local_id );
		    new_count++;
		}
	    }
	    else if( rc ) {
		log_error(_("lid %lu: update failed: %s\n"),
			 lid_from_keyblock(keyblock), g10_errstr(rc) );
		err_count++;
	    }
	    else if( modified ) {
		if( opt.verbose )
		    log_info(_("lid %lu: updated\n"),
					lid_from_keyblock(keyblock));
		upd_count++;
	    }
	    else if( opt.verbose > 1 )
		log_info(_("lid %lu: okay\n"), lid_from_keyblock(keyblock) );

	    release_kbnode( keyblock ); keyblock = NULL;
	    if( !(++count % 100) )
		log_info(_("%lu keys so far processed\n"), count);
	}
	log_info(_("%lu keys processed\n"), count);
	if( err_count )
	    log_info(_("\t%lu keys with errors\n"), err_count);
	if( upd_count )
	    log_info(_("\t%lu keys updated\n"), upd_count);
	if( new_count )
	    log_info(_("\t%lu keys inserted\n"), new_count);
    }
    if( rc && rc != -1 )
	log_error(_("enumerate keyblocks failed: %s\n"), g10_errstr(rc));

    enum_keyblocks( 2, &kbpos, &keyblock ); /* close */
    release_kbnode( keyblock );
}

/****************
 * Check the complete trustdb or only the entries for the given username.
 * We check the complete database. If a username is given or the special
 * username "*" is used, a complete recheck is done.  With no user ID
 * only the records which are not yet checkd are now checked.
 */
void
check_trustdb( const char *username )
{
    TRUSTREC rec;
    KBNODE keyblock = NULL;
    KBPOS kbpos;
    int rc;
    int recheck = username && *username == '*' && !username[1];

    init_trustdb();
    if( username && !recheck ) {
	rc = find_keyblock_byname( &kbpos, username );
	if( !rc )
	    rc = read_keyblock( &kbpos, &keyblock );
	if( rc ) {
	    log_error(_("%s: keyblock read problem: %s\n"),
				    username, g10_errstr(rc));
	}
	else {
	    int modified;

	    rc = update_trust_record( keyblock, 1, &modified );
	    if( rc == -1 ) { /* not yet in trustdb: insert */
		rc = insert_trust_record(
			    find_kbnode( keyblock, PKT_PUBLIC_KEY
				       ) ->pkt->pkt.public_key );

	    }
	    if( rc )
		log_error(_("%s: update failed: %s\n"),
					   username, g10_errstr(rc) );
	    else if( modified )
		log_info(_("%s: updated\n"), username );
	    else
		log_info(_("%s: okay\n"), username );

	}
	release_kbnode( keyblock ); keyblock = NULL;
    }
    else {
	ulong recnum;
	ulong count=0, upd_count=0, err_count=0, skip_count=0;

	for(recnum=0; !tdbio_read_record( recnum, &rec, 0); recnum++ ) {
	    if( rec.rectype == RECTYPE_DIR ) {
		TRUSTREC tmp;
		int modified;

		if( !rec.r.dir.keylist ) {
		    log_info(_("lid %lu: dir record w/o key - skipped\n"),
								  recnum);
		    count++;
		    skip_count++;
		    continue;
		}

		read_record( rec.r.dir.keylist, &tmp, RECTYPE_KEY );

		rc = get_keyblock_byfprint( &keyblock,
					    tmp.r.key.fingerprint,
					    tmp.r.key.fingerprint_len );
		if( rc ) {
		    log_error(_("lid %lu: keyblock not found: %s\n"),
						 recnum, g10_errstr(rc) );
		    count++;
		    skip_count++;
		    continue;
		}

		rc = update_trust_record( keyblock, recheck, &modified );
		if( rc ) {
		    log_error(_("lid %lu: update failed: %s\n"),
						 recnum, g10_errstr(rc) );
		    err_count++;
		}
		else if( modified ) {
		    if( opt.verbose )
			log_info(_("lid %lu: updated\n"), recnum );
		    upd_count++;
		}
		else if( opt.verbose > 1 )
		    log_info(_("lid %lu: okay\n"), recnum );

		release_kbnode( keyblock ); keyblock = NULL;
		if( !(++count % 100) )
		    log_info(_("%lu keys so far processed\n"), count);
	    }
	}
	log_info(_("%lu keys processed\n"), count);
	if( skip_count )
	    log_info(_("\t%lu keys skipped\n"), skip_count);
	if( err_count )
	    log_info(_("\t%lu keys with errors\n"), err_count);
	if( upd_count )
	    log_info(_("\t%lu keys updated\n"), upd_count);
    }
}



/***********************************************
 *********  Query trustdb values  **************
 ***********************************************/


/****************
 * This function simply looks for the key in the trustdb
 * and makes sure that pk->local_id is set to the correct value.
 * Return: 0 = found
 *	   -1 = not found
 *	  other = error
 */
int
query_trust_record( PKT_public_key *pk )
{
    TRUSTREC rec;
    init_trustdb();
    return get_dir_record( pk, &rec );
}


/****************
 * Get the trustlevel for this PK.
 * Note: This does not ask any questions
 * Returns: 0 okay of an errorcode
 *
 * It operates this way:
 *  locate the pk in the trustdb
 *	found:
 *	    Do we have a valid cache record for it?
 *		yes: return trustlevel from cache
 *		no:  make a cache record and all the other stuff
 *	not found:
 *	    try to insert the pubkey into the trustdb and check again
 *
 * Problems: How do we get the complete keyblock to check that the
 *	     cache record is actually valid?  Think we need a clever
 *	     cache in getkey.c	to keep track of this stuff. Maybe it
 *	     is not necessary to check this if we use a local pubring. Hmmmm.
 */
int
check_trust( PKT_public_key *pk, unsigned *r_trustlevel,
	     const byte *namehash, int (*add_fnc)(ulong), unsigned *retflgs )
{
    TRUSTREC rec;
    unsigned trustlevel = TRUST_UNKNOWN;
    int rc=0;
    u32 cur_time;
    u32 keyid[2];


    init_trustdb();
    keyid_from_pk( pk, keyid );

    /* get the pubkey record */
    if( pk->local_id ) {
	read_record( pk->local_id, &rec, RECTYPE_DIR );
    }
    else { /* no local_id: scan the trustdb */
	if( (rc=tdbio_search_dir_bypk( pk, &rec )) && rc != -1 ) {
	    log_error(_("check_trust: search dir record failed: %s\n"),
							    g10_errstr(rc));
	    return rc;
	}
	else if( rc == -1 ) { /* not found - insert */
	    rc = insert_trust_record( pk );
	    if( rc ) {
		log_error(_("key %08lX: insert trust record failed: %s\n"),
					  (ulong)keyid[1], g10_errstr(rc));
		goto leave;
	    }
	    log_info(_("key %08lX.%lu: inserted into trustdb\n"),
					  (ulong)keyid[1], pk->local_id );
	    /* and re-read the dir record */
	    read_record( pk->local_id, &rec, RECTYPE_DIR );
	}
    }
    cur_time = make_timestamp();
    if( pk->timestamp > cur_time ) {
	log_info(_("key %08lX.%lu: created in future "
		   "(time warp or clock problem)\n"),
					  (ulong)keyid[1], pk->local_id );
	return G10ERR_TIME_CONFLICT;
    }

    if( pk->expiredate && pk->expiredate <= cur_time ) {
	log_info(_("key %08lX.%lu: expired at %s\n"),
			(ulong)keyid[1], pk->local_id,
			     asctimestamp( pk->expiredate) );
	 trustlevel = TRUST_EXPIRED;
    }
    else {
	rc = do_check( &rec, &trustlevel, namehash, add_fnc, retflgs );
	if( rc ) {
	    log_error(_("key %08lX.%lu: trust check failed: %s\n"),
			    (ulong)keyid[1], pk->local_id, g10_errstr(rc));
	    return rc;
	}
    }


  leave:
    if( DBG_TRUST )
	log_debug("check_trust() returns trustlevel %04x.\n", trustlevel);
    *r_trustlevel = trustlevel;
    return 0;
}


int
query_trust_info( PKT_public_key *pk, const byte *namehash )
{
    unsigned trustlevel;
    int c;

    init_trustdb();
    if( check_trust( pk, &trustlevel, namehash, NULL, NULL ) )
	return '?';
    if( trustlevel & TRUST_FLAG_REVOKED )
	return 'r';
    c = trust_letter( (trustlevel & TRUST_MASK) );
    if( !c )
	c = '?';
    return c;
}



/****************
 * Return the assigned ownertrust value for the given LID
 */
unsigned
get_ownertrust( ulong lid )
{
    TRUSTREC rec;

    init_trustdb();
    read_record( lid, &rec, RECTYPE_DIR );
    return rec.r.dir.ownertrust;
}

int
get_ownertrust_info( ulong lid )
{
    unsigned otrust;
    int c;

    init_trustdb();
    otrust = get_ownertrust( lid );
    c = trust_letter( (otrust & TRUST_MASK) );
    if( !c )
	c = '?';
    return c;
}



void
list_trust_path( const char *username )
{
    int rc;
    ulong lid;
    TRUSTREC rec;
    TN tree;
    PKT_public_key *pk = m_alloc_clear( sizeof *pk );

    init_trustdb();
    if( (rc = get_pubkey_byname(NULL, pk, username, NULL )) )
	log_error(_("user '%s' not found: %s\n"), username, g10_errstr(rc) );
    else if( (rc=tdbio_search_dir_bypk( pk, &rec )) && rc != -1 )
	log_error(_("problem finding '%s' in trustdb: %s\n"),
					    username, g10_errstr(rc));
    else if( rc == -1 ) {
	log_info(_("user '%s' not in trustdb - inserting\n"), username);
	rc = insert_trust_record( pk );
	if( rc )
	    log_error(_("failed to put '%s' into trustdb: %s\n"),
						    username, g10_errstr(rc));
	else {
	    assert( pk->local_id );
	}
    }
    lid = pk->local_id;

    tree = build_cert_tree( lid, 0, opt.max_cert_depth, NULL );
    if( tree )
	propagate_validity( tree, tree, NULL, NULL );
    if( opt.with_colons )
	dump_tn_tree_with_colons( 0, tree );
    else
	dump_tn_tree( stdout, 0, tree );
    /*printf("(alloced tns=%d  max=%d)\n", alloced_tns, max_alloced_tns );*/
    release_tn_tree( tree );
    /*printf("Ownertrust=%c Validity=%c\n", get_ownertrust_info( lid ),
					  query_trust_info( pk, NULL ) ); */

    free_public_key( pk );

}




/****************
 * Enumerate all keys, which are needed to build all trust paths for
 * the given key.  This function does not return the key itself or
 * the ultimate key (the last point in cerificate chain).  Only
 * certificate chains which ends up at an ultimately trusted key
 * are listed.	If ownertrust or validity is not NULL, the corresponding
 * value for the returned LID is also returned in these variable(s).
 *
 *  1) create a void pointer and initialize it to NULL
 *  2) pass this void pointer by reference to this function.
 *     Set lid to the key you want to enumerate and pass it by reference.
 *  3) call this function as long as it does not return -1
 *     to indicate EOF. LID does contain the next key used to build the web
 *  4) Always call this function a last time with LID set to NULL,
 *     so that it can free its context.
 *
 * Returns: -1 on EOF or the level of the returned LID
 */
int
enum_cert_paths( void **context, ulong *lid,
		 unsigned *ownertrust, unsigned *validity )
{
    return -1;
  #if 0
    struct enum_cert_paths_ctx *ctx;
    fixme: .....   tsl;

    init_trustdb();
    if( !lid ) {  /* release the context */
	if( *context ) {
	    FIXME: ........tsl2;

	    ctx = *context;
	    for(tsl = ctx->tsl_head; tsl; tsl = tsl2 ) {
		tsl2 = tsl->next;
		m_free( tsl );
	    }
	    *context = NULL;
	}
	return -1;
    }

    if( !*context ) {
	FIXME .... *tmppath;
	TRUSTREC rec;

	if( !*lid )
	    return -1;

	ctx = m_alloc_clear( sizeof *ctx );
	*context = ctx;
	/* collect the paths */
      #if 0
	read_record( *lid, &rec, RECTYPE_DIR );
	tmppath = m_alloc_clear( (opt.max_cert_depth+1)* sizeof *tmppath );
	tsl = NULL;
	collect_paths( 0, opt.max_cert_depth, 1, &rec, tmppath, &tsl );
	m_free( tmppath );
	sort_tsl_list( &tsl );
      #endif
	/* setup the context */
	ctx->tsl_head = tsl;
	ctx->tsl = ctx->tsl_head;
	ctx->idx = 0;
    }
    else
	ctx = *context;

    while( ctx->tsl && ctx->idx >= ctx->tsl->pathlen )	{
	ctx->tsl = ctx->tsl->next;
	ctx->idx = 0;
    }
    tsl = ctx->tsl;
    if( !tsl )
	return -1; /* eof */

    if( ownertrust )
	*ownertrust = tsl->path[ctx->idx].otrust;
    if( validity )
	*validity = tsl->path[ctx->idx].trust;
    *lid = tsl->path[ctx->idx].lid;
    ctx->idx++;
    return ctx->idx-1;
  #endif
}


/****************
 * Print the current path
 */
void
enum_cert_paths_print( void **context, FILE *fp,
				       int refresh, ulong selected_lid )
{
    return;
  #if 0
    struct enum_cert_paths_ctx *ctx;
    FIXME......... tsl;

    if( !*context )
	return;
    init_trustdb();
    ctx = *context;
    if( !ctx->tsl )
	return;
    tsl = ctx->tsl;

    if( !fp )
	fp = stderr;

    if( refresh ) { /* update the ownertrust and if possible the validity */
	int i;
	int match = tdbio_db_matches_options();

	for( i = 0; i < tsl->pathlen; i++ )  {
	    TRUSTREC rec;

	    read_record( tsl->path[i].lid, &rec, RECTYPE_DIR );
	    tsl->path[i].otrust = rec.r.dir.ownertrust;
	    /* update validity only if we have it in the cache
	     * calculation is too time consuming */
	    if( match && rec.r.dir.valcheck && rec.r.dir.validity ) {
		tsl->path[i].trust = rec.r.dir.validity;
		if( rec.r.dir.dirflags & DIRF_REVOKED )
		    tsl->path[i].trust = TRUST_FLAG_REVOKED;
	    }
	}
    }

    print_path( tsl->pathlen, tsl->path, fp, selected_lid );
  #endif
}


/*
 * Return an allocated buffer with the preference values for
 * the key with LID and the userid which is identified by the
 * HAMEHASH or the firstone if namehash is NULL.  ret_n receives
 * the length of the allocated buffer.	Structure of the buffer is
 * a repeated sequences of 2 bytes; where the first byte describes the
 * type of the preference and the second one the value.  The constants
 * PREFTYPE_xxxx should be used to reference a type.
 */
byte *
get_pref_data( ulong lid, const byte *namehash, size_t *ret_n )
{
    TRUSTREC rec;
    ulong recno;

    init_trustdb();
    read_record( lid, &rec, RECTYPE_DIR );
    for( recno=rec.r.dir.uidlist; recno; recno = rec.r.uid.next ) {
	read_record( recno, &rec, RECTYPE_UID );
	if( rec.r.uid.prefrec
	    && ( !namehash || !memcmp(namehash, rec.r.uid.namehash, 20) ))  {
	    byte *buf;
	    /* found the correct one or the first one */
	    read_record( rec.r.uid.prefrec, &rec, RECTYPE_PREF );
	    if( rec.r.pref.next )
		log_info(_("WARNING: can't yet handle long pref records\n"));
	    buf = m_alloc( ITEMS_PER_PREF_RECORD );
	    memcpy( buf, rec.r.pref.data, ITEMS_PER_PREF_RECORD );
	    *ret_n = ITEMS_PER_PREF_RECORD;
	    return buf;
	}
    }
    return NULL;
}



/****************
 * Check whether the algorithm is in one of the pref records
 */
int
is_algo_in_prefs( ulong lid, int preftype, int algo )
{
    TRUSTREC rec;
    ulong recno;
    int i;
    byte *pref;

    init_trustdb();
    read_record( lid, &rec, RECTYPE_DIR );
    for( recno=rec.r.dir.uidlist; recno; recno = rec.r.uid.next ) {
	read_record( recno, &rec, RECTYPE_UID );
	if( rec.r.uid.prefrec ) {
	    read_record( rec.r.uid.prefrec, &rec, RECTYPE_PREF );
	    if( rec.r.pref.next )
		log_info(_("WARNING: can't yet handle long pref records\n"));
	    pref = rec.r.pref.data;
	    for(i=0; i+1 < ITEMS_PER_PREF_RECORD; i+=2 ) {
		if( pref[i] == preftype && pref[i+1] == algo )
		    return 1;
	    }
	}
    }
    return 0;
}