summaryrefslogtreecommitdiffstats
path: root/dirmngr/server.c
blob: 651f67cf80e91b2084b849cab1ef2ad4d6c63ccc (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
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
/* server.c - LDAP and Keyserver access server
 * Copyright (C) 2002 Klarälvdalens Datakonsult AB
 * Copyright (C) 2003, 2004, 2005, 2007, 2008, 2009, 2011, 2015 g10 Code GmbH
 * Copyright (C) 2014, 2015, 2016 Werner Koch
 * Copyright (C) 2016 Bundesamt für Sicherheit in der Informationstechnik
 *
 * This file is part of GnuPG.
 *
 * GnuPG is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * GnuPG is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: GPL-3.0+
 */

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

#include "dirmngr.h"
#include <assuan.h>

#include "crlcache.h"
#include "crlfetch.h"
#if USE_LDAP
# include "ldapserver.h"
#endif
#include "ocsp.h"
#include "certcache.h"
#include "validate.h"
#include "misc.h"
#if USE_LDAP
# include "ldap-wrapper.h"
#endif
#include "ks-action.h"
#include "ks-engine.h"
#if USE_LDAP
# include "ldap-parse-uri.h"
#endif
#include "dns-stuff.h"
#include "../common/mbox-util.h"
#include "../common/zb32.h"
#include "../common/server-help.h"

/* To avoid DoS attacks we limit the size of a certificate to
   something reasonable.  The DoS was actually only an issue back when
   Dirmngr was a system service and not a user service. */
#define MAX_CERT_LENGTH (16*1024)

/* The limit for the CERTLIST inquiry.  We allow for up to 20
 * certificates but also take PEM encoding into account.  */
#define MAX_CERTLIST_LENGTH ((MAX_CERT_LENGTH * 20 * 4)/3)

/* The same goes for OpenPGP keyblocks, but here we need to allow for
   much longer blocks; a 200k keyblock is not too unusual for keys
   with a lot of signatures (e.g. 0x5b0358a2).  9C31503C6D866396 even
   has 770 KiB as of 2015-08-23.  To avoid adding a runtime option we
   now use 20MiB which should really be enough.  Well, a key with
   several pictures could be larger (the parser as a 18MiB limit for
   attribute packets) but it won't be nice to the keyservers to send
   them such large blobs.  */
#define MAX_KEYBLOCK_LENGTH (20*1024*1024)


#define PARM_ERROR(t) assuan_set_error (ctx, \
                                        gpg_error (GPG_ERR_ASS_PARAMETER), (t))
#define set_error(e,t) (ctx ? assuan_set_error (ctx, gpg_error (e), (t)) \
                        /**/: gpg_error (e))



/* Control structure per connection. */
struct server_local_s
{
  /* Data used to associate an Assuan context with local server data */
  assuan_context_t assuan_ctx;

  /* The session id (a counter).  */
  unsigned int session_id;

  /* Per-session LDAP servers.  */
  ldap_server_t ldapservers;

  /* Per-session list of keyservers.  */
  uri_item_t keyservers;

  /* If this flag is set to true this dirmngr process will be
     terminated after the end of this session.  */
  int stopme;

  /* State variable private to is_tor_running.  */
  int tor_state;

  /* If the first both flags are set the assuan logging of data lines
   * is suppressed.  The count variable is used to show the number of
   * non-logged bytes.  */
  size_t inhibit_data_logging_count;
  unsigned int inhibit_data_logging : 1;
  unsigned int inhibit_data_logging_now : 1;
};


/* Cookie definition for assuan data line output.  */
static gpgrt_ssize_t data_line_cookie_write (void *cookie,
                                             const void *buffer, size_t size);
static int data_line_cookie_close (void *cookie);
static es_cookie_io_functions_t data_line_cookie_functions =
  {
    NULL,
    data_line_cookie_write,
    NULL,
    data_line_cookie_close
  };


/* Local prototypes */
static const char *task_check_wkd_support (ctrl_t ctrl, const char *domain);




/* Accessor for the local ldapservers variable. */
ldap_server_t
get_ldapservers_from_ctrl (ctrl_t ctrl)
{
  if (ctrl && ctrl->server_local)
    return ctrl->server_local->ldapservers;
  else
    return NULL;
}

/* Release an uri_item_t list.  */
static void
release_uri_item_list (uri_item_t list)
{
  while (list)
    {
      uri_item_t tmp = list->next;
      http_release_parsed_uri (list->parsed_uri);
      xfree (list);
      list = tmp;
    }
}

/* Release all configured keyserver info from CTRL.  */
void
release_ctrl_keyservers (ctrl_t ctrl)
{
  if (! ctrl->server_local)
    return;

  release_uri_item_list (ctrl->server_local->keyservers);
  ctrl->server_local->keyservers = NULL;
}



/* Helper to print a message while leaving a command.  */
static gpg_error_t
leave_cmd (assuan_context_t ctx, gpg_error_t err)
{
  if (err)
    {
      const char *name = assuan_get_command_name (ctx);
      if (!name)
        name = "?";
      if (gpg_err_source (err) == GPG_ERR_SOURCE_DEFAULT)
        log_error ("command '%s' failed: %s\n", name,
                   gpg_strerror (err));
      else
        log_error ("command '%s' failed: %s <%s>\n", name,
                   gpg_strerror (err), gpg_strsource (err));
    }
  return err;
}


/* This is a wrapper around assuan_send_data which makes debugging the
   output in verbose mode easier.  */
static gpg_error_t
data_line_write (assuan_context_t ctx, const void *buffer_arg, size_t size)
{
  ctrl_t ctrl = assuan_get_pointer (ctx);
  const char *buffer = buffer_arg;
  gpg_error_t err;

  /* If we do not want logging, enable it here.  */
  if (ctrl && ctrl->server_local && ctrl->server_local->inhibit_data_logging)
    ctrl->server_local->inhibit_data_logging_now = 1;

  if (opt.verbose && buffer && size)
    {
      /* Ease reading of output by sending a physical line at each LF.  */
      const char *p;
      size_t n, nbytes;

      nbytes = size;
      do
        {
          p = memchr (buffer, '\n', nbytes);
          n = p ? (p - buffer) + 1 : nbytes;
          err = assuan_send_data (ctx, buffer, n);
          if (err)
            {
              gpg_err_set_errno (EIO);
              goto leave;
            }
          buffer += n;
          nbytes -= n;
          if (nbytes && (err=assuan_send_data (ctx, NULL, 0))) /* Flush line. */
            {
              gpg_err_set_errno (EIO);
              goto leave;
            }
        }
      while (nbytes);
    }
  else
    {
      err = assuan_send_data (ctx, buffer, size);
      if (err)
        {
          gpg_err_set_errno (EIO);  /* For use by data_line_cookie_write.  */
          goto leave;
        }
    }

 leave:
  if (ctrl && ctrl->server_local && ctrl->server_local->inhibit_data_logging)
    {
      ctrl->server_local->inhibit_data_logging_now = 0;
      ctrl->server_local->inhibit_data_logging_count += size;
    }

  return err;
}


/* A write handler used by es_fopencookie to write assuan data
   lines.  */
static gpgrt_ssize_t
data_line_cookie_write (void *cookie, const void *buffer, size_t size)
{
  assuan_context_t ctx = cookie;

  if (data_line_write (ctx, buffer, size))
    return -1;
  return (gpgrt_ssize_t)size;
}


static int
data_line_cookie_close (void *cookie)
{
  assuan_context_t ctx = cookie;

  if (DBG_IPC)
    {
      ctrl_t ctrl = assuan_get_pointer (ctx);

      if (ctrl && ctrl->server_local
          && ctrl->server_local->inhibit_data_logging
          && ctrl->server_local->inhibit_data_logging_count)
        log_debug ("(%zu bytes sent via D lines not shown)\n",
                   ctrl->server_local->inhibit_data_logging_count);
    }
  if (assuan_send_data (ctx, NULL, 0))
    {
      gpg_err_set_errno (EIO);
      return -1;
    }

  return 0;
}


/* Copy the % and + escaped string S into the buffer D and replace the
   escape sequences.  Note, that it is sufficient to allocate the
   target string D as long as the source string S, i.e.: strlen(s)+1.
   Note further that if S contains an escaped binary Nul the resulting
   string D will contain the 0 as well as all other characters but it
   will be impossible to know whether this is the original EOS or a
   copied Nul. */
static void
strcpy_escaped_plus (char *d, const unsigned char *s)
{
  while (*s)
    {
      if (*s == '%' && s[1] && s[2])
        {
          s++;
          *d++ = xtoi_2 ( s);
          s += 2;
        }
      else if (*s == '+')
        *d++ = ' ', s++;
      else
        *d++ = *s++;
    }
  *d = 0;
}


/* This function returns true if a Tor server is running.  The status
 * is cached for the current connection.  */
static int
is_tor_running (ctrl_t ctrl)
{
  /* Check whether we can connect to the proxy.  */

  if (!ctrl || !ctrl->server_local)
    return 0; /* Ooops.  */

  if (!ctrl->server_local->tor_state)
    {
      assuan_fd_t sock;

      sock = assuan_sock_connect_byname (NULL, 0, 0, NULL, ASSUAN_SOCK_TOR);
      if (sock == ASSUAN_INVALID_FD)
        ctrl->server_local->tor_state = -1; /* Not running.  */
      else
        {
          assuan_sock_close (sock);
          ctrl->server_local->tor_state = 1; /* Running.  */
        }
    }
  return (ctrl->server_local->tor_state > 0);
}


/* Return an error if the assuan context does not belong to the owner
   of the process or to root.  On error FAILTEXT is set as Assuan
   error string.  */
static gpg_error_t
check_owner_permission (assuan_context_t ctx, const char *failtext)
{
#ifdef HAVE_W32_SYSTEM
  /* Under Windows the dirmngr is always run under the control of the
     user.  */
  (void)ctx;
  (void)failtext;
#else
  gpg_err_code_t ec;
  assuan_peercred_t cred;

  ec = gpg_err_code (assuan_get_peercred (ctx, &cred));
  if (!ec && cred->uid && cred->uid != getuid ())
    ec = GPG_ERR_EPERM;
  if (ec)
    return set_error (ec, failtext);
#endif
  return 0;
}



/* Common code for get_cert_local and get_issuer_cert_local. */
static ksba_cert_t
do_get_cert_local (ctrl_t ctrl, const char *name, const char *command)
{
  unsigned char *value;
  size_t valuelen;
  int rc;
  char *buf;
  ksba_cert_t cert;

  buf = name? strconcat (command, " ", name, NULL) : xtrystrdup (command);
  if (!buf)
    rc = gpg_error_from_syserror ();
  else
    {
      rc = assuan_inquire (ctrl->server_local->assuan_ctx, buf,
                           &value, &valuelen, MAX_CERT_LENGTH);
      xfree (buf);
    }
  if (rc)
    {
      log_error (_("assuan_inquire(%s) failed: %s\n"),
                 command, gpg_strerror (rc));
      return NULL;
    }

  if (!valuelen)
    {
      xfree (value);
      return NULL;
    }

  rc = ksba_cert_new (&cert);
  if (!rc)
    {
      rc = ksba_cert_init_from_mem (cert, value, valuelen);
      if (rc)
        {
          ksba_cert_release (cert);
          cert = NULL;
        }
    }
  xfree (value);
  return cert;
}



/* Ask back to return a certificate for NAME, given as a regular gpgsm
 * certificate identifier (e.g. fingerprint or one of the other
 * methods).  Alternatively, NULL may be used for NAME to return the
 * current target certificate.  Either return the certificate in a
 * KSBA object or NULL if it is not available.  */
ksba_cert_t
get_cert_local (ctrl_t ctrl, const char *name)
{
  if (!ctrl || !ctrl->server_local || !ctrl->server_local->assuan_ctx)
    {
      if (opt.debug)
        log_debug ("get_cert_local called w/o context\n");
      return NULL;
    }
  return do_get_cert_local (ctrl, name, "SENDCERT");

}


/* Ask back to return the issuing certificate for NAME, given as a
 * regular gpgsm certificate identifier (e.g. fingerprint or one
 * of the other methods).  Alternatively, NULL may be used for NAME to
 * return the current target certificate. Either return the certificate
 * in a KSBA object or NULL if it is not available.  */
ksba_cert_t
get_issuing_cert_local (ctrl_t ctrl, const char *name)
{
  if (!ctrl || !ctrl->server_local || !ctrl->server_local->assuan_ctx)
    {
      if (opt.debug)
        log_debug ("get_issuing_cert_local called w/o context\n");
      return NULL;
    }
  return do_get_cert_local (ctrl, name, "SENDISSUERCERT");
}


/* Ask back to return a certificate with subject NAME and a
 * subjectKeyIdentifier of KEYID. */
ksba_cert_t
get_cert_local_ski (ctrl_t ctrl, const char *name, ksba_sexp_t keyid)
{
  unsigned char *value;
  size_t valuelen;
  int rc;
  char *buf;
  ksba_cert_t cert;
  char *hexkeyid;

  if (!ctrl || !ctrl->server_local || !ctrl->server_local->assuan_ctx)
    {
      if (opt.debug)
        log_debug ("get_cert_local_ski called w/o context\n");
      return NULL;
    }
  if (!name || !keyid)
    {
      log_debug ("get_cert_local_ski called with insufficient arguments\n");
      return NULL;
    }

  hexkeyid = serial_hex (keyid);
  if (!hexkeyid)
    {
      log_debug ("serial_hex() failed\n");
      return NULL;
    }

  buf = strconcat ("SENDCERT_SKI ", hexkeyid, " /", name, NULL);
  if (!buf)
    {
      log_error ("can't allocate enough memory: %s\n", strerror (errno));
      xfree (hexkeyid);
      return NULL;
    }
  xfree (hexkeyid);

  rc = assuan_inquire (ctrl->server_local->assuan_ctx, buf,
                       &value, &valuelen, MAX_CERT_LENGTH);
  xfree (buf);
  if (rc)
    {
      log_error (_("assuan_inquire(%s) failed: %s\n"), "SENDCERT_SKI",
                 gpg_strerror (rc));
      return NULL;
    }

  if (!valuelen)
    {
      xfree (value);
      return NULL;
    }

  rc = ksba_cert_new (&cert);
  if (!rc)
    {
      rc = ksba_cert_init_from_mem (cert, value, valuelen);
      if (rc)
        {
          ksba_cert_release (cert);
          cert = NULL;
        }
    }
  xfree (value);
  return cert;
}


/* Ask the client via an inquiry to check the istrusted status of the
   certificate specified by the hexified fingerprint HEXFPR.  Returns
   0 if the certificate is trusted by the client or an error code.  */
gpg_error_t
get_istrusted_from_client (ctrl_t ctrl, const char *hexfpr)
{
  unsigned char *value;
  size_t valuelen;
  int rc;
  char request[100];

  if (!ctrl || !ctrl->server_local || !ctrl->server_local->assuan_ctx
      || !hexfpr)
    return gpg_error (GPG_ERR_INV_ARG);

  snprintf (request, sizeof request, "ISTRUSTED %s", hexfpr);
  rc = assuan_inquire (ctrl->server_local->assuan_ctx, request,
                       &value, &valuelen, 100);
  if (rc)
    {
      log_error (_("assuan_inquire(%s) failed: %s\n"),
                 request, gpg_strerror (rc));
      return rc;
    }
  /* The expected data is: "1" or "1 cruft" (not a C-string).  */
  if (valuelen && *value == '1' && (valuelen == 1 || spacep (value+1)))
    rc = 0;
  else
    rc = gpg_error (GPG_ERR_NOT_TRUSTED);
  xfree (value);
  return rc;
}




/* Ask the client to return the certificate associated with the
   current command. This is sometimes needed because the client usually
   sends us just the cert ID, assuming that the request can be
   satisfied from the cache, where the cert ID is used as key. */
static int
inquire_cert_and_load_crl (assuan_context_t ctx)
{
  ctrl_t ctrl = assuan_get_pointer (ctx);
  gpg_error_t err;
  unsigned char *value = NULL;
  size_t valuelen;
  ksba_cert_t cert = NULL;

  err = assuan_inquire( ctx, "SENDCERT", &value, &valuelen, 0);
  if (err)
    return err;

/*   { */
/*     FILE *fp = fopen ("foo.der", "r"); */
/*     value = xmalloc (2000); */
/*     valuelen = fread (value, 1, 2000, fp); */
/*     fclose (fp); */
/*   } */

  if (!valuelen) /* No data returned; return a comprehensible error. */
    return gpg_error (GPG_ERR_MISSING_CERT);

  err = ksba_cert_new (&cert);
  if (err)
    goto leave;
  err = ksba_cert_init_from_mem (cert, value, valuelen);
  if(err)
    goto leave;
  xfree (value); value = NULL;

  err = crl_cache_reload_crl (ctrl, cert);

 leave:
  ksba_cert_release (cert);
  xfree (value);
  return err;
}


/* Handle OPTION commands. */
static gpg_error_t
option_handler (assuan_context_t ctx, const char *key, const char *value)
{
  ctrl_t ctrl = assuan_get_pointer (ctx);
  gpg_error_t err = 0;

  if (!strcmp (key, "force-crl-refresh"))
    {
      int i = *value? atoi (value) : 0;
      ctrl->force_crl_refresh = i;
    }
  else if (!strcmp (key, "audit-events"))
    {
      int i = *value? atoi (value) : 0;
      ctrl->audit_events = i;
    }
  else if (!strcmp (key, "http-proxy"))
    {
      xfree (ctrl->http_proxy);
      if (!*value || !strcmp (value, "none"))
        ctrl->http_proxy = NULL;
      else if (!(ctrl->http_proxy = xtrystrdup (value)))
        err = gpg_error_from_syserror ();
    }
  else if (!strcmp (key, "honor-keyserver-url-used"))
    {
      /* Return an error if we are running in Tor mode.  */
      if (dirmngr_use_tor ())
        err = gpg_error (GPG_ERR_FORBIDDEN);
    }
  else if (!strcmp (key, "http-crl"))
    {
      int i = *value? atoi (value) : 0;
      ctrl->http_no_crl = !i;
    }
  else
    err = gpg_error (GPG_ERR_UNKNOWN_OPTION);

  return err;
}



static const char hlp_dns_cert[] =
  "DNS_CERT <subtype> <name>\n"
  "DNS_CERT --pka <user_id>\n"
  "DNS_CERT --dane <user_id>\n"
  "\n"
  "Return the CERT record for <name>.  <subtype> is one of\n"
  "  *     Return the first record of any supported subtype\n"
  "  PGP   Return the first record of subtype PGP (3)\n"
  "  IPGP  Return the first record of subtype IPGP (6)\n"
  "If the content of a certificate is available (PGP) it is returned\n"
  "by data lines.  Fingerprints and URLs are returned via status lines.\n"
  "In --pka mode the fingerprint and if available an URL is returned.\n"
  "In --dane mode the key is returned from RR type 61";
static gpg_error_t
cmd_dns_cert (assuan_context_t ctx, char *line)
{
  /* ctrl_t ctrl = assuan_get_pointer (ctx); */
  gpg_error_t err = 0;
  int pka_mode, dane_mode;
  char *mbox = NULL;
  char *namebuf = NULL;
  char *encodedhash = NULL;
  const char *name;
  int certtype;
  char *p;
  void *key = NULL;
  size_t keylen;
  unsigned char *fpr = NULL;
  size_t fprlen;
  char *url = NULL;

  pka_mode = has_option (line, "--pka");
  dane_mode = has_option (line, "--dane");
  line = skip_options (line);

  if (pka_mode && dane_mode)
    {
      err = PARM_ERROR ("either --pka or --dane may be given");
      goto leave;
    }

  if (pka_mode || dane_mode)
    ; /* No need to parse here - we do this later.  */
  else
    {
      p = strchr (line, ' ');
      if (!p)
        {
          err = PARM_ERROR ("missing arguments");
          goto leave;
        }
      *p++ = 0;
      if (!strcmp (line, "*"))
        certtype = DNS_CERTTYPE_ANY;
      else if (!strcmp (line, "IPGP"))
        certtype = DNS_CERTTYPE_IPGP;
      else if (!strcmp (line, "PGP"))
        certtype = DNS_CERTTYPE_PGP;
      else
        {
          err = PARM_ERROR ("unknown subtype");
          goto leave;
        }
      while (spacep (p))
        p++;
      line = p;
      if (!*line)
        {
          err = PARM_ERROR ("name missing");
          goto leave;
        }
    }

  if (pka_mode || dane_mode)
    {
      char *domain;     /* Points to mbox.  */
      char hashbuf[32]; /* For SHA-1 and SHA-256. */

      /* We lowercase ascii characters but the DANE I-D does not allow
         this.  FIXME: Check after the release of the RFC whether to
         change this.  */
      mbox = mailbox_from_userid (line);
      if (!mbox || !(domain = strchr (mbox, '@')))
        {
          err = set_error (GPG_ERR_INV_USER_ID, "no mailbox in user id");
          goto leave;
        }
      *domain++ = 0;

      if (pka_mode)
        {
          gcry_md_hash_buffer (GCRY_MD_SHA1, hashbuf, mbox, strlen (mbox));
          encodedhash = zb32_encode (hashbuf, 8*20);
          if (!encodedhash)
            {
              err = gpg_error_from_syserror ();
              goto leave;
            }
          namebuf = strconcat (encodedhash, "._pka.", domain, NULL);
          if (!namebuf)
            {
              err = gpg_error_from_syserror ();
              goto leave;
            }
          name = namebuf;
          certtype = DNS_CERTTYPE_IPGP;
        }
      else
        {
          /* Note: The hash is truncated to 28 bytes and we lowercase
             the result only for aesthetic reasons.  */
          gcry_md_hash_buffer (GCRY_MD_SHA256, hashbuf, mbox, strlen (mbox));
          encodedhash = bin2hex (hashbuf, 28, NULL);
          if (!encodedhash)
            {
              err = gpg_error_from_syserror ();
              goto leave;
            }
          ascii_strlwr (encodedhash);
          namebuf = strconcat (encodedhash, "._openpgpkey.", domain, NULL);
          if (!namebuf)
            {
              err = gpg_error_from_syserror ();
              goto leave;
            }
          name = namebuf;
          certtype = DNS_CERTTYPE_RR61;
        }
    }
  else
    name = line;

  err = get_dns_cert (name, certtype, &key, &keylen, &fpr, &fprlen, &url);
  if (err)
    goto leave;

  if (key)
    {
      err = data_line_write (ctx, key, keylen);
      if (err)
        goto leave;
    }

  if (fpr)
    {
      char *tmpstr;

      tmpstr = bin2hex (fpr, fprlen, NULL);
      if (!tmpstr)
        err = gpg_error_from_syserror ();
      else
        {
          err = assuan_write_status (ctx, "FPR", tmpstr);
          xfree (tmpstr);
        }
      if (err)
        goto leave;
    }

  if (url)
    {
      err = assuan_write_status (ctx, "URL", url);
      if (err)
        goto leave;
    }


 leave:
  xfree (key);
  xfree (fpr);
  xfree (url);
  xfree (mbox);
  xfree (namebuf);
  xfree (encodedhash);
  return leave_cmd (ctx, err);
}



/* Core of cmd_wkd_get and task_check_wkd_support.  If CTX is NULL
 * this function will not write anything to the assuan output.  */
static gpg_error_t
proc_wkd_get (ctrl_t ctrl, assuan_context_t ctx, char *line)
{
  gpg_error_t err = 0;
  char *mbox = NULL;
  char *domainbuf = NULL;
  char *domain;     /* Points to mbox or domainbuf.  This is used to
                     * connect to the host.  */
  char *domain_orig;/* Points to mbox.  This is the used for the
                     * query; i.e. the domain part of the
                     * addrspec.  */
  char sha1buf[20];
  char *uri = NULL;
  char *encodedhash = NULL;
  int opt_submission_addr;
  int opt_policy_flags;
  int is_wkd_query;   /* True if this is a real WKD query.  */
  int no_log = 0;
  char portstr[20] = { 0 };
  int subdomain_mode = 0;

  opt_submission_addr = has_option (line, "--submission-address");
  opt_policy_flags = has_option (line, "--policy-flags");
  if (has_option (line, "--quick"))
    ctrl->timeout = opt.connect_quick_timeout;
  line = skip_options (line);
  is_wkd_query = !(opt_policy_flags || opt_submission_addr);

  mbox = mailbox_from_userid (line);
  if (!mbox || !(domain = strchr (mbox, '@')))
    {
      err = set_error (GPG_ERR_INV_USER_ID, "no mailbox in user id");
      goto leave;
    }
  *domain++ = 0;
  domain_orig = domain;


  /* Let's check whether we already know that the domain does not
   * support WKD.  */
  if (is_wkd_query)
    {
      if (domaininfo_is_wkd_not_supported (domain_orig))
        {
          err = gpg_error (GPG_ERR_NO_DATA);
          dirmngr_status_printf (ctrl, "NOTE", "wkd_cached_result %u", err);
          goto leave;
        }
    }


  /* First try the new "openpgp" subdomain.  We check that the domain
   * is valid because it is later used as an unescaped filename part
   * of the URI.  */
  if (is_valid_domain_name (domain_orig))
    {
      dns_addrinfo_t aibuf;

      domainbuf = strconcat ( "openpgpkey.", domain_orig, NULL);
      if (!domainbuf)
        {
          err = gpg_error_from_syserror ();
          goto leave;
        }

      /* FIXME: We should put a cache into dns-stuff because the same
       * query (with a different port and socket type, though) will be
       * done later by http function.  */
      err = resolve_dns_name (domainbuf, 0, 0, 0, &aibuf, NULL);
      if (err)
        {
          err = 0;
          xfree (domainbuf);
          domainbuf = NULL;
        }
      else /* Got a subdomain. */
        {
          free_dns_addrinfo (aibuf);
          subdomain_mode = 1;
          domain = domainbuf;
        }
    }

  /* Check for SRV records unless we have a subdomain. */
  if (!subdomain_mode)
    {
      struct srventry *srvs;
      unsigned int srvscount;
      size_t domainlen, targetlen;
      int i;

      err = get_dns_srv (domain, "openpgpkey", NULL, &srvs, &srvscount);
      if (err)
        {
          /* Ignore server failed becuase there are too many resolvers
           * which do not work as expected.  */
          if (gpg_err_code (err) == GPG_ERR_SERVER_FAILED)
            err = 0; /*(srvcount is guaranteed to be 0)*/
          else
            goto leave;
        }

      /* Check for rogue DNS names.  */
      for (i = 0; i < srvscount; i++)
        {
          if (!is_valid_domain_name (srvs[i].target))
            {
              err = gpg_error (GPG_ERR_DNS_ADDRESS);
              log_error ("rogue openpgpkey SRV record for '%s'\n", domain);
              xfree (srvs);
              goto leave;
            }
        }

      /* Find the first target which also ends in DOMAIN or is equal
       * to DOMAIN.  */
      domainlen = strlen (domain);
      for (i = 0; i < srvscount; i++)
        {
          if (DBG_DNS)
            log_debug ("srv: trying '%s:%hu'\n", srvs[i].target, srvs[i].port);
          targetlen = strlen (srvs[i].target);
          if ((targetlen > domainlen + 1
               && srvs[i].target[targetlen - domainlen - 1] == '.'
               && !ascii_strcasecmp (srvs[i].target + targetlen - domainlen,
                                     domain))
              || (targetlen == domainlen
                  && !ascii_strcasecmp (srvs[i].target, domain)))
            {
              /* found.  */
              domainbuf = xtrystrdup (srvs[i].target);
              if (!domainbuf)
                {
                  err = gpg_error_from_syserror ();
                  xfree (srvs);
                  goto leave;
                }
              domain = domainbuf;
              if (srvs[i].port)
                snprintf (portstr, sizeof portstr, ":%hu", srvs[i].port);
              break;
            }
        }
      xfree (srvs);
    }

  /* Prepare the hash of the local part.  */
  gcry_md_hash_buffer (GCRY_MD_SHA1, sha1buf, mbox, strlen (mbox));
  encodedhash = zb32_encode (sha1buf, 8*20);
  if (!encodedhash)
    {
      err = gpg_error_from_syserror ();
      goto leave;
    }

  if (opt_submission_addr)
    {
      uri = strconcat ("https://",
                       domain,
                       portstr,
                       "/.well-known/openpgpkey/",
                       subdomain_mode? domain_orig : "",
                       subdomain_mode? "/" : "",
                       "submission-address",
                       NULL);
    }
  else if (opt_policy_flags)
    {
      uri = strconcat ("https://",
                       domain,
                       portstr,
                       "/.well-known/openpgpkey/",
                       subdomain_mode? domain_orig : "",
                       subdomain_mode? "/" : "",
                       "policy",
                       NULL);
    }
  else
    {
      char *escapedmbox;

      escapedmbox = http_escape_string (mbox, "%;?&=+#");
      if (escapedmbox)
        {
          uri = strconcat ("https://",
                           domain,
                           portstr,
                           "/.well-known/openpgpkey/",
                           subdomain_mode? domain_orig : "",
                           subdomain_mode? "/" : "",
                           "hu/",
                           encodedhash,
                           "?l=",
                           escapedmbox,
                           NULL);
          xfree (escapedmbox);
          no_log = 1;
          if (uri)
            {
              err = dirmngr_status_printf (ctrl, "SOURCE", "https://%s%s",
                                           domain, portstr);
              if (err)
                goto leave;
            }
        }
    }
  if (!uri)
    {
      err = gpg_error_from_syserror ();
      goto leave;
    }

  /* Setup an output stream and perform the get.  */
  {
    estream_t outfp;

    outfp = ctx? es_fopencookie (ctx, "w", data_line_cookie_functions) : NULL;
    if (!outfp && ctx)
      err = set_error (GPG_ERR_ASS_GENERAL,
                       "error setting up a data stream");
    else
      {
        if (ctrl->server_local)
          {
            if (no_log)
              ctrl->server_local->inhibit_data_logging = 1;
            ctrl->server_local->inhibit_data_logging_now = 0;
            ctrl->server_local->inhibit_data_logging_count = 0;
          }
        err = ks_action_fetch (ctrl, uri, outfp);
        es_fclose (outfp);
        if (ctrl->server_local)
          ctrl->server_local->inhibit_data_logging = 0;

        /* Register the result under the domain name of MBOX. */
        switch (gpg_err_code (err))
          {
          case 0:
            domaininfo_set_wkd_supported (domain_orig);
            break;

          case GPG_ERR_NO_NAME:
            /* There is no such domain.  */
            domaininfo_set_no_name (domain_orig);
            break;

          case GPG_ERR_NO_DATA:
            if (is_wkd_query && ctrl->server_local)
              {
                /* Mark that and schedule a check.  */
                domaininfo_set_wkd_not_found (domain_orig);
                workqueue_add_task (task_check_wkd_support, domain_orig,
                                    ctrl->server_local->session_id, 1);
              }
            else if (opt_policy_flags) /* No policy file - no support.  */
              domaininfo_set_wkd_not_supported (domain_orig);
            break;

          default:
            /* Don't register other errors.  */
            break;
          }
      }
  }

 leave:
  xfree (uri);
  xfree (encodedhash);
  xfree (mbox);
  xfree (domainbuf);
  return err;
}


static const char hlp_wkd_get[] =
  "WKD_GET [--submission-address|--policy-flags] <user_id>\n"
  "\n"
  "Return the key or other info for <user_id>\n"
  "from the Web Key Directory.";
static gpg_error_t
cmd_wkd_get (assuan_context_t ctx, char *line)
{
  ctrl_t ctrl = assuan_get_pointer (ctx);
  gpg_error_t err;

  err = proc_wkd_get (ctrl, ctx, line);

  return leave_cmd (ctx, err);
}


/* A task to check whether DOMAIN supports WKD.  This is done by
 * checking whether the policy flags file can be read.  */
static const char *
task_check_wkd_support (ctrl_t ctrl, const char *domain)
{
  char *string;

  if (!ctrl || !domain)
    return "check_wkd_support";

  string = strconcat ("--policy-flags foo@", domain, NULL);
  if (!string)
    log_error ("%s: %s\n", __func__, gpg_strerror (gpg_error_from_syserror ()));
  else
    {
      proc_wkd_get (ctrl, NULL, string);
      xfree (string);
    }

  return NULL;
}



static const char hlp_ldapserver[] =
  "LDAPSERVER [--clear] <data>\n"
  "\n"
  "Add a new LDAP server to the list of configured LDAP servers.\n"
  "DATA is in the same format as expected in the configure file.\n"
  "An optional prefix \"ldap:\" is allowed.  With no args all\n"
  "configured ldapservers are listed.  Option --clear removes all\n"
  "servers configured in this session.";
static gpg_error_t
cmd_ldapserver (assuan_context_t ctx, char *line)
{
#if USE_LDAP
  ctrl_t ctrl = assuan_get_pointer (ctx);
  ldap_server_t server;
  ldap_server_t *last_next_p;
  int clear_flag;

  clear_flag = has_option (line, "--clear");
  line = skip_options (line);
  while (spacep (line))
    line++;

  if (clear_flag)
    {
#if USE_LDAP
      ldapserver_list_free (ctrl->server_local->ldapservers);
#endif /*USE_LDAP*/
      ctrl->server_local->ldapservers = NULL;
    }

  if (!*line && clear_flag)
    return leave_cmd (ctx, 0);

  if (!*line)
    {
      /* List all ldapservers.  */
      struct ldapserver_iter ldapserver_iter;
      char *tmpstr;
      char portstr[20];

      for (ldapserver_iter_begin (&ldapserver_iter, ctrl);
           !ldapserver_iter_end_p (&ldapserver_iter);
           ldapserver_iter_next (&ldapserver_iter))
        {
          server = ldapserver_iter.server;
          if (server->port)
            snprintf (portstr, sizeof portstr, "%d", server->port);
          else
            *portstr = 0;

          tmpstr = xtryasprintf ("ldap:%s:%s:%s:%s:%s:%s%s:",
                                 server->host? server->host : "",
                                 portstr,
                                 server->user? server->user : "",
                                 server->pass? "*****": "",
                                 server->base? server->base : "",
                                 server->starttls ? "starttls" :
                                 server->ldap_over_tls ? "ldaptls" : "none",
                                 server->ntds ? ",ntds" : "");
          if (!tmpstr)
            return leave_cmd (ctx, gpg_error_from_syserror ());
          dirmngr_status (ctrl, "LDAPSERVER", tmpstr, NULL);
          xfree (tmpstr);
        }
      return leave_cmd (ctx, 0);
    }

  /* Skip an "ldap:" prefix unless it is a valid ldap url.  */
  if (!strncmp (line, "ldap:", 5) && !(line[5] == '/' && line[6] == '/'))
    line += 5;

  server = ldapserver_parse_one (line, NULL, 0);
  if (! server)
    return leave_cmd (ctx, gpg_error (GPG_ERR_INV_ARG));

  last_next_p = &ctrl->server_local->ldapservers;
  while (*last_next_p)
    last_next_p = &(*last_next_p)->next;
  *last_next_p = server;
  return leave_cmd (ctx, 0);
#else
  (void)line;
  return leave_cmd (ctx, gpg_error (GPG_ERR_NOT_IMPLEMENTED));
#endif
}


static const char hlp_isvalid[] =
  "ISVALID [--only-ocsp] [--force-default-responder]"
  " <certificate_id> [<certificate_fpr>]\n"
  "\n"
  "This command checks whether the certificate identified by the\n"
  "certificate_id is valid.  This is done by consulting CRLs or\n"
  "whatever has been configured.  Note, that the returned error codes\n"
  "are from gpg-error.h.  The command may callback using the inquire\n"
  "function.  See the manual for details.\n"
  "\n"
  "The CERTIFICATE_ID is a hex encoded string consisting of two parts,\n"
  "delimited by a single dot.  The first part is the SHA-1 hash of the\n"
  "issuer name and the second part the serial number.\n"
  "\n"
  "If an OCSP check is desired CERTIFICATE_FPR with the hex encoded\n"
  "fingerprint of the certificate is required.  In this case an OCSP\n"
  "request is done before consulting the CRL.\n"
  "\n"
  "If the option --only-ocsp is given, no fallback to a CRL check will\n"
  "be used.\n"
  "\n"
  "If the option --force-default-responder is given, only the default\n"
  "OCSP responder will be used and any other methods of obtaining an\n"
  "OCSP responder URL won't be used.";
static gpg_error_t
cmd_isvalid (assuan_context_t ctx, char *line)
{
  ctrl_t ctrl = assuan_get_pointer (ctx);
  char *issuerhash, *serialno, *fpr;
  gpg_error_t err;
  int did_inquire = 0;
  int ocsp_mode = 0;
  int only_ocsp;
  int force_default_responder;

  only_ocsp = has_option (line, "--only-ocsp");
  force_default_responder = has_option (line, "--force-default-responder");
  line = skip_options (line);

  /* We need to work on a copy of the line because that same Assuan
   * context may be used for an inquiry.  That is because Assuan
   * reuses its line buffer.  */
  issuerhash = xstrdup (line);

  serialno = strchr (issuerhash, '.');
  if (!serialno)
    {
      xfree (issuerhash);
      return leave_cmd (ctx, PARM_ERROR (_("serialno missing in cert ID")));
    }
  *serialno++ = 0;
  if (strlen (issuerhash) != 40)
    {
      xfree (issuerhash);
      return leave_cmd (ctx, PARM_ERROR ("cert ID is too short"));
    }

  fpr = strchr (serialno, ' ');
  while (fpr && spacep (fpr))
    fpr++;
  if (fpr && *fpr)
    {
      char *endp = strchr (fpr, ' ');
      if (endp)
        *endp = 0;
      if (strlen (fpr) != 40)
        {
          xfree (issuerhash);
          return leave_cmd (ctx, PARM_ERROR ("fingerprint too short"));
        }
      ocsp_mode = 1;
    }


 again:
  if (ocsp_mode)
    {
      /* Note, that we currently ignore the supplied fingerprint FPR;
       * instead ocsp_isvalid does an inquire to ask for the cert.
       * The fingerprint may eventually be used to lookup the
       * certificate in a local cache.  */
      if (!opt.allow_ocsp)
        err = gpg_error (GPG_ERR_NOT_SUPPORTED);
      else
        err = ocsp_isvalid (ctrl, NULL, NULL, force_default_responder);

      if (gpg_err_code (err) == GPG_ERR_CONFIGURATION
          && gpg_err_source (err) == GPG_ERR_SOURCE_DIRMNGR)
        {
          /* No default responder configured - fallback to CRL.  */
          if (!only_ocsp)
            log_info ("falling back to CRL check\n");
          ocsp_mode = 0;
          goto again;
        }
    }
  else if (only_ocsp)
    err = gpg_error (GPG_ERR_NO_CRL_KNOWN);
  else
    {
      switch (crl_cache_isvalid (ctrl,
                                 issuerhash, serialno,
                                 ctrl->force_crl_refresh))
        {
        case CRL_CACHE_VALID:
          err = 0;
          break;
        case CRL_CACHE_INVALID:
          err = gpg_error (GPG_ERR_CERT_REVOKED);
          break;
        case CRL_CACHE_DONTKNOW:
          if (did_inquire)
            err = gpg_error (GPG_ERR_NO_CRL_KNOWN);
          else if (!(err = inquire_cert_and_load_crl (ctx)))
            {
              did_inquire = 1;
              goto again;
            }
          break;
        case CRL_CACHE_CANTUSE:
          err = gpg_error (GPG_ERR_NO_CRL_KNOWN);
          break;
        default:
          log_fatal ("crl_cache_isvalid returned invalid code\n");
        }
    }

  xfree (issuerhash);
  return leave_cmd (ctx, err);
}


/* If the line contains a SHA-1 fingerprint as the first argument,
   return the FPR vuffer on success.  The function checks that the
   fingerprint consists of valid characters and prints and error
   message if it does not and returns NULL.  Fingerprints are
   considered optional and thus no explicit error is returned. NULL is
   also returned if there is no fingerprint at all available.
   FPR must be a caller provided buffer of at least 20 bytes.

   Note that colons within the fingerprint are allowed to separate 2
   hex digits; this allows for easier cutting and pasting using the
   usual fingerprint rendering.
*/
static unsigned char *
get_fingerprint_from_line (const char *line, unsigned char *fpr)
{
  const char *s;
  int i;

  for (s=line, i=0; *s && *s != ' '; s++ )
    {
      if ( hexdigitp (s) && hexdigitp (s+1) )
        {
          if ( i >= 20 )
            return NULL;  /* Fingerprint too long.  */
          fpr[i++] = xtoi_2 (s);
          s++;
        }
      else if ( *s != ':' )
        return NULL; /* Invalid.  */
    }
  if ( i != 20 )
    return NULL; /* Fingerprint to short.  */
  return fpr;
}



static const char hlp_checkcrl[] =
  "CHECKCRL [<fingerprint>]\n"
  "\n"
  "Check whether the certificate with FINGERPRINT (SHA-1 hash of the\n"
  "entire X.509 certificate blob) is valid or not by consulting the\n"
  "CRL responsible for this certificate.  If the fingerprint has not\n"
  "been given or the certificate is not known, the function \n"
  "inquires the certificate using an\n"
  "\n"
  "  INQUIRE TARGETCERT\n"
  "\n"
  "and the caller is expected to return the certificate for the\n"
  "request (which should match FINGERPRINT) as a binary blob.\n"
  "Processing then takes place without further interaction; in\n"
  "particular dirmngr tries to locate other required certificate by\n"
  "its own mechanism which includes a local certificate store as well\n"
  "as a list of trusted root certificates.\n"
  "\n"
  "The return value is the usual gpg-error code or 0 for ducesss;\n"
  "i.e. the certificate validity has been confirmed by a valid CRL.";
static gpg_error_t
cmd_checkcrl (assuan_context_t ctx, char *line)
{
  ctrl_t ctrl = assuan_get_pointer (ctx);
  gpg_error_t err;
  unsigned char fprbuffer[20], *fpr;
  ksba_cert_t cert;

  fpr = get_fingerprint_from_line (line, fprbuffer);
  cert = fpr? get_cert_byfpr (fpr) : NULL;

  if (!cert)
    {
      /* We do not have this certificate yet or the fingerprint has
         not been given.  Inquire it from the client.  */
      unsigned char *value = NULL;
      size_t valuelen;

      err = assuan_inquire (ctrl->server_local->assuan_ctx, "TARGETCERT",
                           &value, &valuelen, MAX_CERT_LENGTH);
      if (err)
        {
          log_error (_("assuan_inquire failed: %s\n"), gpg_strerror (err));
          goto leave;
        }

      if (!valuelen) /* No data returned; return a comprehensible error. */
        err = gpg_error (GPG_ERR_MISSING_CERT);
      else
        {
          err = ksba_cert_new (&cert);
          if (!err)
            err = ksba_cert_init_from_mem (cert, value, valuelen);
        }
      xfree (value);
      if(err)
        goto leave;
    }

  assert (cert);

  err = crl_cache_cert_isvalid (ctrl, cert, ctrl->force_crl_refresh);
  if (gpg_err_code (err) == GPG_ERR_NO_CRL_KNOWN)
    {
      err = crl_cache_reload_crl (ctrl, cert);
      if (!err)
        err = crl_cache_cert_isvalid (ctrl, cert, 0);
    }

 leave:
  ksba_cert_release (cert);
  return leave_cmd (ctx, err);
}


static const char hlp_checkocsp[] =
  "CHECKOCSP [--force-default-responder] [<fingerprint>]\n"
  "\n"
  "Check whether the certificate with FINGERPRINT (SHA-1 hash of the\n"
  "entire X.509 certificate blob) is valid or not by asking an OCSP\n"
  "responder responsible for this certificate.  The optional\n"
  "fingerprint may be used for a quick check in case an OCSP check has\n"
  "been done for this certificate recently (we always cache OCSP\n"
  "responses for a couple of minutes). If the fingerprint has not been\n"
  "given or there is no cached result, the function inquires the\n"
  "certificate using an\n"
  "\n"
  "   INQUIRE TARGETCERT\n"
  "\n"
  "and the caller is expected to return the certificate for the\n"
  "request (which should match FINGERPRINT) as a binary blob.\n"
  "Processing then takes place without further interaction; in\n"
  "particular dirmngr tries to locate other required certificates by\n"
  "its own mechanism which includes a local certificate store as well\n"
  "as a list of trusted root certificates.\n"
  "\n"
  "If the option --force-default-responder is given, only the default\n"
  "OCSP responder will be used and any other methods of obtaining an\n"
  "OCSP responder URL won't be used.\n"
  "\n"
  "The return value is the usual gpg-error code or 0 for ducesss;\n"
  "i.e. the certificate validity has been confirmed by a valid CRL.";
static gpg_error_t
cmd_checkocsp (assuan_context_t ctx, char *line)
{
  ctrl_t ctrl = assuan_get_pointer (ctx);
  gpg_error_t err;
  unsigned char fprbuffer[20], *fpr;
  ksba_cert_t cert;
  int force_default_responder;

  force_default_responder = has_option (line, "--force-default-responder");
  line = skip_options (line);

  fpr = get_fingerprint_from_line (line, fprbuffer);
  cert = fpr? get_cert_byfpr (fpr) : NULL;

  if (!cert)
    {
      /* We do not have this certificate yet or the fingerprint has
         not been given.  Inquire it from the client.  */
      unsigned char *value = NULL;
      size_t valuelen;

      err = assuan_inquire (ctrl->server_local->assuan_ctx, "TARGETCERT",
                           &value, &valuelen, MAX_CERT_LENGTH);
      if (err)
        {
          log_error (_("assuan_inquire failed: %s\n"), gpg_strerror (err));
          goto leave;
        }

      if (!valuelen) /* No data returned; return a comprehensible error. */
        err = gpg_error (GPG_ERR_MISSING_CERT);
      else
        {
          err = ksba_cert_new (&cert);
          if (!err)
            err = ksba_cert_init_from_mem (cert, value, valuelen);
        }
      xfree (value);
      if(err)
        goto leave;
    }

  assert (cert);

  if (!opt.allow_ocsp)
    err = gpg_error (GPG_ERR_NOT_SUPPORTED);
  else
    err = ocsp_isvalid (ctrl, cert, NULL, force_default_responder);

 leave:
  ksba_cert_release (cert);
  return leave_cmd (ctx, err);
}



static int
lookup_cert_by_url (assuan_context_t ctx, const char *url)
{
  ctrl_t ctrl = assuan_get_pointer (ctx);
  gpg_error_t err = 0;
  unsigned char *value = NULL;
  size_t valuelen;

  /* Fetch single certificate given it's URL.  */
  err = fetch_cert_by_url (ctrl, url, &value, &valuelen);
  if (err)
    {
      log_error (_("fetch_cert_by_url failed: %s\n"), gpg_strerror (err));
      goto leave;
    }

  /* Send the data, flush the buffer and then send an END. */
  err = assuan_send_data (ctx, value, valuelen);
  if (!err)
    err = assuan_send_data (ctx, NULL, 0);
  if (!err)
    err = assuan_write_line (ctx, "END");
  if (err)
    {
      log_error (_("error sending data: %s\n"), gpg_strerror (err));
      goto leave;
    }

 leave:

  return err;
}


/* Send the certificate, flush the buffer and then send an END. */
static gpg_error_t
return_one_cert (void *opaque, ksba_cert_t cert)
{
  assuan_context_t ctx = opaque;
  gpg_error_t err;
  const unsigned char *der;
  size_t derlen;

  der = ksba_cert_get_image (cert, &derlen);
  if (!der)
    err = gpg_error (GPG_ERR_INV_CERT_OBJ);
  else
    {
      err = assuan_send_data (ctx, der, derlen);
      if (!err)
        err = assuan_send_data (ctx, NULL, 0);
      if (!err)
        err = assuan_write_line (ctx, "END");
    }
  if (err)
    log_error (_("error sending data: %s\n"), gpg_strerror (err));
  return err;
}


/* Lookup certificates from the internal cache or using the ldap
   servers. */
static int
lookup_cert_by_pattern (assuan_context_t ctx, char *line,
                        int single, int cache_only)
{
  gpg_error_t err = 0;
  char *p;
  strlist_t sl, list = NULL;
  int truncated = 0, truncation_forced = 0;
  int count = 0;
  int local_count = 0;
#if USE_LDAP
  ctrl_t ctrl = assuan_get_pointer (ctx);
  unsigned char *value = NULL;
  size_t valuelen;
  struct ldapserver_iter ldapserver_iter;
  cert_fetch_context_t fetch_context;
#endif /*USE_LDAP*/
  int any_no_data = 0;

  /* Break the line down into an STRLIST */
  for (p=line; *p; line = p)
    {
      while (*p && *p != ' ')
        p++;
      if (*p)
        *p++ = 0;

      if (*line)
        {
          sl = xtrymalloc (sizeof *sl + strlen (line));
          if (!sl)
            {
              err = gpg_error_from_errno (errno);
              goto leave;
            }
          memset (sl, 0, sizeof *sl);
          strcpy_escaped_plus (sl->d, line);
          sl->next = list;
          list = sl;
        }
    }

  /* First look through the internal cache.  The certificates returned
     here are not counted towards the truncation limit.  */
  if (single && !cache_only)
    ; /* Do not read from the local cache in this case.  */
  else
    {
      for (sl=list; sl; sl = sl->next)
        {
          err = get_certs_bypattern (sl->d, return_one_cert, ctx);
          if (!err)
            local_count++;
          if (!err && single)
            goto ready;

          if (gpg_err_code (err) == GPG_ERR_NO_DATA
              || gpg_err_code (err) == GPG_ERR_NOT_FOUND)
            {
              err = 0;
              if (cache_only)
                any_no_data = 1;
            }
          else if (gpg_err_code (err) == GPG_ERR_INV_NAME && !cache_only)
            {
              /* No real fault because the internal pattern lookup
                 can't yet cope with all types of pattern.  */
              err = 0;
            }
          if (err)
            goto ready;
        }
    }

  /* Loop over all configured servers unless we want only the
     certificates from the cache.  */
#if USE_LDAP
  for (ldapserver_iter_begin (&ldapserver_iter, ctrl);
       !cache_only && !ldapserver_iter_end_p (&ldapserver_iter)
	 && ldapserver_iter.server->host && !truncation_forced;
       ldapserver_iter_next (&ldapserver_iter))
    {
      ldap_server_t ldapserver = ldapserver_iter.server;

      if (DBG_LOOKUP)
        log_debug ("cmd_lookup: trying %s:%d base=%s\n",
                   ldapserver->host, ldapserver->port,
                   ldapserver->base?ldapserver->base : "[default]");

      /* Fetch certificates matching pattern */
      err = start_cert_fetch (ctrl, &fetch_context, list, ldapserver);
      if ( gpg_err_code (err) == GPG_ERR_NO_DATA )
        {
          if (DBG_LOOKUP)
            log_debug ("cmd_lookup: no data\n");
          err = 0;
          any_no_data = 1;
          continue;
        }
      if (err)
        {
          log_error (_("start_cert_fetch failed: %s\n"), gpg_strerror (err));
          goto leave;
        }

      /* Fetch the certificates for this query. */
      while (!truncation_forced)
        {
          xfree (value); value = NULL;
          err = fetch_next_cert (fetch_context, &value, &valuelen);
          if (gpg_err_code (err) == GPG_ERR_NO_DATA )
            {
              err = 0;
              any_no_data = 1;
              break; /* Ready. */
            }
          if (gpg_err_code (err) == GPG_ERR_TRUNCATED)
            {
              truncated = 1;
              err = 0;
              break;  /* Ready.  */
            }
          if (gpg_err_code (err) == GPG_ERR_EOF)
            {
              err = 0;
              break; /* Ready. */
            }
          if (!err && !value)
            {
              err = gpg_error (GPG_ERR_BUG);
              goto leave;
            }
          if (err)
            {
              log_error (_("fetch_next_cert failed: %s\n"),
                         gpg_strerror (err));
              end_cert_fetch (fetch_context);
              goto leave;
            }

          if (DBG_LOOKUP)
            log_debug ("cmd_lookup: returning one cert%s\n",
                       truncated? " (truncated)":"");

          /* Send the data, flush the buffer and then send an END line
             as a certificate delimiter. */
          err = assuan_send_data (ctx, value, valuelen);
          if (!err)
            err = assuan_send_data (ctx, NULL, 0);
          if (!err)
            err = assuan_write_line (ctx, "END");
          if (err)
            {
              log_error (_("error sending data: %s\n"), gpg_strerror (err));
              end_cert_fetch (fetch_context);
              goto leave;
            }

          if (++count >= opt.max_replies )
            {
              truncation_forced = 1;
              log_info (_("max_replies %d exceeded\n"), opt.max_replies );
            }
          if (single)
            break;
        }

      end_cert_fetch (fetch_context);
    }
#endif /*USE_LDAP*/

 ready:
  if (truncated || truncation_forced)
    {
      char str[50];

      sprintf (str, "%d", count);
      assuan_write_status (ctx, "TRUNCATED", str);
    }

  if (!err && !count && !local_count && any_no_data)
    err = gpg_error (GPG_ERR_NO_DATA);

 leave:
  free_strlist (list);
  return err;
}


static const char hlp_lookup[] =
  "LOOKUP [--url] [--single] [--cache-only] <pattern>\n"
  "\n"
  "Lookup certificates matching PATTERN. With --url the pattern is\n"
  "expected to be one URL.\n"
  "\n"
  "If --url is not given:  To allow for multiple patterns (which are ORed)\n"
  "quoting is required: Spaces are translated to \"+\" or \"%20\";\n"
  "obviously this requires that the usual escape quoting rules are applied.\n"
  "\n"
  "If --url is given no special escaping is required because URLs are\n"
  "already escaped this way.\n"
  "\n"
  "If --single is given the first and only the first match will be\n"
  "returned.  If --cache-only is _not_ given, no local query will be\n"
  "done.\n"
  "\n"
  "If --cache-only is given no external lookup is done so that only\n"
  "certificates from the cache may get returned.";
static gpg_error_t
cmd_lookup (assuan_context_t ctx, char *line)
{
  gpg_error_t err;
  int lookup_url, single, cache_only;

  lookup_url = has_leading_option (line, "--url");
  single = has_leading_option (line, "--single");
  cache_only = has_leading_option (line, "--cache-only");
  line = skip_options (line);

  if (lookup_url && cache_only)
    err = gpg_error (GPG_ERR_NOT_FOUND);
  else if (lookup_url && single)
    err = gpg_error (GPG_ERR_NOT_IMPLEMENTED);
  else if (lookup_url)
    err = lookup_cert_by_url (ctx, line);
  else
    err = lookup_cert_by_pattern (ctx, line, single, cache_only);

  return leave_cmd (ctx, err);
}


static const char hlp_loadcrl[] =
  "LOADCRL [--url] <filename|url>\n"
  "\n"
  "Load the CRL in the file with name FILENAME into our cache.  Note\n"
  "that FILENAME should be given with an absolute path because\n"
  "Dirmngrs cwd is not known.  With --url the CRL is directly loaded\n"
  "from the given URL.\n"
  "\n"
  "This command is usually used by gpgsm using the invocation \"gpgsm\n"
  "--call-dirmngr loadcrl <filename>\".  A direct invocation of Dirmngr\n"
  "is not useful because gpgsm might need to callback gpgsm to ask for\n"
  "the CA's certificate.";
static gpg_error_t
cmd_loadcrl (assuan_context_t ctx, char *line)
{
  ctrl_t ctrl = assuan_get_pointer (ctx);
  gpg_error_t err = 0;
  int use_url = has_leading_option (line, "--url");

  line = skip_options (line);

  if (use_url)
    {
      ksba_reader_t reader;

      err = crl_fetch (ctrl, line, &reader);
      if (err)
        log_error (_("fetching CRL from '%s' failed: %s\n"),
                   line, gpg_strerror (err));
      else
        {
          err = crl_cache_insert (ctrl, line, reader);
          if (err)
            log_error (_("processing CRL from '%s' failed: %s\n"),
                       line, gpg_strerror (err));
          crl_close_reader (reader);
        }
    }
  else
    {
      char *buf;

      buf = xtrymalloc (strlen (line)+1);
      if (!buf)
        err = gpg_error_from_syserror ();
      else
        {
          strcpy_escaped_plus (buf, line);
          err = crl_cache_load (ctrl, buf);
          xfree (buf);
        }
    }

  return leave_cmd (ctx, err);
}


static const char hlp_listcrls[] =
  "LISTCRLS\n"
  "\n"
  "List the content of all CRLs in a readable format.  This command is\n"
  "usually used by gpgsm using the invocation \"gpgsm --call-dirmngr\n"
  "listcrls\".  It may also be used directly using \"dirmngr\n"
  "--list-crls\".";
static gpg_error_t
cmd_listcrls (assuan_context_t ctx, char *line)
{
  gpg_error_t err;
  estream_t fp;

  (void)line;

  fp = es_fopencookie (ctx, "w", data_line_cookie_functions);
  if (!fp)
    err = set_error (GPG_ERR_ASS_GENERAL, "error setting up a data stream");
  else
    {
      err = crl_cache_list (fp);
      es_fclose (fp);
    }
  return leave_cmd (ctx, err);
}


static const char hlp_cachecert[] =
  "CACHECERT\n"
  "\n"
  "Put a certificate into the internal cache.  This command might be\n"
  "useful if a client knows in advance certificates required for a\n"
  "test and wants to make sure they get added to the internal cache.\n"
  "It is also helpful for debugging.  To get the actual certificate,\n"
  "this command immediately inquires it using\n"
  "\n"
  "  INQUIRE TARGETCERT\n"
  "\n"
  "and the caller is expected to return the certificate for the\n"
  "request as a binary blob.";
static gpg_error_t
cmd_cachecert (assuan_context_t ctx, char *line)
{
  ctrl_t ctrl = assuan_get_pointer (ctx);
  gpg_error_t err;
  ksba_cert_t cert = NULL;
  unsigned char *value = NULL;
  size_t valuelen;

  (void)line;

  err = assuan_inquire (ctrl->server_local->assuan_ctx, "TARGETCERT",
                       &value, &valuelen, MAX_CERT_LENGTH);
  if (err)
    {
      log_error (_("assuan_inquire failed: %s\n"), gpg_strerror (err));
      goto leave;
    }

  if (!valuelen) /* No data returned; return a comprehensible error. */
    err = gpg_error (GPG_ERR_MISSING_CERT);
  else
    {
      err = ksba_cert_new (&cert);
      if (!err)
        err = ksba_cert_init_from_mem (cert, value, valuelen);
    }
  xfree (value);
  if(err)
    goto leave;

  err = cache_cert (cert);

 leave:
  ksba_cert_release (cert);
  return leave_cmd (ctx, err);
}


static const char hlp_validate[] =
  "VALIDATE [--systrust] [--tls] [--no-crl]\n"
  "\n"
  "Validate a certificate using the certificate validation function\n"
  "used internally by dirmngr.  This command is only useful for\n"
  "debugging.  To get the actual certificate, this command immediately\n"
  "inquires it using\n"
  "\n"
  "  INQUIRE TARGETCERT\n"
  "\n"
  "and the caller is expected to return the certificate for the\n"
  "request as a binary blob.  The option --tls modifies this by asking\n"
  "for list of certificates with\n"
  "\n"
  "  INQUIRE CERTLIST\n"
  "\n"
  "Here the first certificate is the target certificate, the remaining\n"
  "certificates are suggested intermediary certificates.  All certificates\n"
  "need to be PEM encoded.\n"
  "\n"
  "The option --systrust changes the behaviour to include the system\n"
  "provided root certificates as trust anchors.  The option --no-crl\n"
  "skips CRL checks";
static gpg_error_t
cmd_validate (assuan_context_t ctx, char *line)
{
  ctrl_t ctrl = assuan_get_pointer (ctx);
  gpg_error_t err;
  ksba_cert_t cert = NULL;
  certlist_t certlist = NULL;
  unsigned char *value = NULL;
  size_t valuelen;
  int systrust_mode, tls_mode, no_crl;

  systrust_mode = has_option (line, "--systrust");
  tls_mode = has_option (line, "--tls");
  no_crl = has_option (line, "--no-crl");
  line = skip_options (line);

  if (tls_mode)
    err = assuan_inquire (ctrl->server_local->assuan_ctx, "CERTLIST",
                          &value, &valuelen, MAX_CERTLIST_LENGTH);
  else
    err = assuan_inquire (ctrl->server_local->assuan_ctx, "TARGETCERT",
                          &value, &valuelen, MAX_CERT_LENGTH);
  if (err)
    {
      log_error (_("assuan_inquire failed: %s\n"), gpg_strerror (err));
      goto leave;
    }

  if (!valuelen) /* No data returned; return a comprehensible error. */
    err = gpg_error (GPG_ERR_MISSING_CERT);
  else if (tls_mode)
    {
      estream_t fp;

      fp = es_fopenmem_init (0, "rb", value, valuelen);
      if (!fp)
        err = gpg_error_from_syserror ();
      else
        {
          err = read_certlist_from_stream (&certlist, fp);
          es_fclose (fp);
          if (!err && !certlist)
            err = gpg_error (GPG_ERR_MISSING_CERT);
          if (!err)
            {
              /* Extract the first certificate from the list.  */
              cert = certlist->cert;
              ksba_cert_ref (cert);
            }
        }
    }
  else
    {
      err = ksba_cert_new (&cert);
      if (!err)
        err = ksba_cert_init_from_mem (cert, value, valuelen);
    }
  xfree (value);
  if(err)
    goto leave;

  if (!tls_mode)
    {
      /* If we have this certificate already in our cache, use the
       * cached version for validation because this will take care of
       * any cached results.  We don't need to do this in tls mode
       * because this has already been done for certificate in a
       * certlist_t. */
      unsigned char fpr[20];
      ksba_cert_t tmpcert;

      cert_compute_fpr (cert, fpr);
      tmpcert = get_cert_byfpr (fpr);
      if (tmpcert)
        {
          ksba_cert_release (cert);
          cert = tmpcert;
        }
    }

  /* Quick hack to make verification work by inserting the supplied
   * certs into the cache.  */
  if (tls_mode && certlist)
    {
      certlist_t cl;

      for (cl = certlist->next; cl; cl = cl->next)
        cache_cert (cl->cert);
    }

  err = validate_cert_chain (ctrl, cert, NULL,
                             (VALIDATE_FLAG_TRUST_CONFIG
                              | (tls_mode ? VALIDATE_FLAG_TLS : 0)
                              | (systrust_mode ? VALIDATE_FLAG_TRUST_SYSTEM : 0)
                              | (no_crl ? VALIDATE_FLAG_NOCRLCHECK : 0)),
                             NULL);

 leave:
  ksba_cert_release (cert);
  release_certlist (certlist);
  return leave_cmd (ctx, err);
}



/* Parse an keyserver URI and store it in a new uri item which is
   returned at R_ITEM.  On error return an error code.  */
static gpg_error_t
make_keyserver_item (const char *uri, uri_item_t *r_item)
{
  gpg_error_t err;
  uri_item_t item;
  const char *s;
  char *tmpstr = NULL;

  *r_item = NULL;

  /* We used to have DNS CNAME redirection from the URLs below to
   * sks-keyserver. pools.  The idea was to allow for a quick way to
   * switch to a different set of pools.  The problem with that
   * approach is that TLS needs to verify the hostname and - because
   * DNS is not secured - it can only check the user supplied hostname
   * and not a hostname from a CNAME RR.  Thus the final server all
   * need to have certificates with the actual pool name as well as
   * for keys.gnupg.net - that would render the advantage of
   * keys.gnupg.net useless and so we better give up on this.  Because
   * the keys.gnupg.net URL are still in widespread use we do a static
   * mapping here.
   */
  if (!strcmp (uri, "hkps://keys.gnupg.net")
      || !strcmp (uri, "keys.gnupg.net"))
    uri = "hkps://keyserver.ubuntu.com";
  else if (!strcmp (uri, "https://keys.gnupg.net"))
    uri = "hkps://keyserver.ubuntu.com";
  else if (!strcmp (uri, "hkp://keys.gnupg.net"))
    uri = "hkp://pgp.surf.nl";
  else if (!strcmp (uri, "http://keys.gnupg.net"))
    uri = "hkp://pgp.surf.nl:80";
  else if (!strcmp (uri, "hkps://http-keys.gnupg.net")
           || !strcmp (uri, "http-keys.gnupg.net"))
    uri = "hkps://keyserver.ubuntu.com";
  else if (!strcmp (uri, "https://http-keys.gnupg.net"))
    uri = "hkps://keyserver.ubuntu.com";
  else if (!strcmp (uri, "hkp://http-keys.gnupg.net"))
    uri = "hkp://pgp.surf.nl";
  else if (!strcmp (uri, "http://http-keys.gnupg.net"))
    uri = "hkp://pgp.surf.nl:80";

  item = xtrymalloc (sizeof *item + strlen (uri));
  if (!item)
    return gpg_error_from_syserror ();

  item->next = NULL;
  item->parsed_uri = NULL;
  strcpy (item->uri, uri);

#if USE_LDAP
  if (!strncmp (uri, "ldap:", 5) && !(uri[5] == '/' && uri[6] == '/'))
    {
      /* Special ldap scheme given.  This differs from a valid ldap
       * scheme in that no double slash follows..  Use http_parse_uri
       * to put it as opaque value into parsed_uri.  */
      tmpstr = strconcat ("opaque:", uri+5, NULL);
      if (!tmpstr)
        err = gpg_error_from_syserror ();
      else
        err = http_parse_uri (&item->parsed_uri, tmpstr, 0);
    }
  else if ((s=strchr (uri, ':')) && !(s[1] == '/' && s[2] == '/'))
    {
      /* No valid scheme given.  Use http_parse_uri to put the string
       * as opaque value into parsed_uri.  */
      tmpstr = strconcat ("opaque:", uri, NULL);
      if (!tmpstr)
        err = gpg_error_from_syserror ();
      else
        err = http_parse_uri (&item->parsed_uri, tmpstr, 0);
    }
  else if (ldap_uri_p (uri))
    {
      int fixup = 0;
      /* Fixme: We should get rid of that parser and repalce it with
       * our generic (http) URI parser.  */

      /* If no port has been specified and the scheme ist ldaps we use
       * our idea of the default port because the standard LDAP URL
       * parser would use 636 here.  This is because we redefined
       * ldaps to mean starttls.  */
#ifdef HAVE_W32_SYSTEM
      if (!strcmp (uri, "ldap:///"))
          fixup = 1;
      else
#endif
        if (!http_parse_uri (&item->parsed_uri,uri,HTTP_PARSE_NO_SCHEME_CHECK))
        {
          if (!item->parsed_uri->port
              && !strcmp (item->parsed_uri->scheme, "ldaps"))
            fixup = 2;
          http_release_parsed_uri (item->parsed_uri);
          item->parsed_uri = NULL;
        }

      err = ldap_parse_uri (&item->parsed_uri, uri);
      if (!err && fixup == 1)
        item->parsed_uri->ad_current = 1;
      else if (!err && fixup == 2)
        item->parsed_uri->port = 389;
    }
  else
#endif /* USE_LDAP */
    {
      err = http_parse_uri (&item->parsed_uri, uri, HTTP_PARSE_NO_SCHEME_CHECK);
    }

  xfree (tmpstr);
  if (err)
    xfree (item);
  else
    *r_item = item;
  return err;
}


/* If no keyserver is stored in CTRL but a global keyserver has been
   set, put that global keyserver into CTRL.  We need use this
   function to help migrate from the old gpg based keyserver
   configuration to the new dirmngr based configuration.  */
static gpg_error_t
ensure_keyserver (ctrl_t ctrl)
{
  gpg_error_t err;
  uri_item_t item;
  uri_item_t onion_items = NULL;
  uri_item_t plain_items = NULL;
  uri_item_t ui;
  strlist_t sl;

  if (ctrl->server_local->keyservers)
    return 0; /* Already set for this session.  */
  if (!opt.keyserver)
    {
      /* No global option set.  Fall back to default:  */
      return make_keyserver_item (DIRMNGR_DEFAULT_KEYSERVER,
                                  &ctrl->server_local->keyservers);
    }

  for (sl = opt.keyserver; sl; sl = sl->next)
    {
      err = make_keyserver_item (sl->d, &item);
      if (err)
        goto leave;
      if (item->parsed_uri->onion)
        {
          item->next = onion_items;
          onion_items = item;
        }
      else
        {
          item->next = plain_items;
          plain_items = item;
        }
    }

  /* Decide which to use.  Note that the session has no keyservers
     yet set. */
  if (onion_items && !onion_items->next && plain_items && !plain_items->next)
    {
      /* If there is just one onion and one plain keyserver given, we take
         only one depending on whether Tor is running or not.  */
      if (!dirmngr_never_use_tor_p () && is_tor_running (ctrl))
        {
          ctrl->server_local->keyservers = onion_items;
          onion_items = NULL;
        }
      else
        {
          ctrl->server_local->keyservers = plain_items;
          plain_items = NULL;
        }
    }
  else if (dirmngr_never_use_tor_p () || !is_tor_running (ctrl))
    {
      /* Tor is not running.  It does not make sense to add Onion
         addresses.  */
      ctrl->server_local->keyservers = plain_items;
      plain_items = NULL;
    }
  else
    {
      /* In all other cases add all keyservers.  */
      ctrl->server_local->keyservers = onion_items;
      onion_items = NULL;
      for (ui = ctrl->server_local->keyservers; ui && ui->next; ui = ui->next)
        ;
      if (ui)
        ui->next = plain_items;
      else
        ctrl->server_local->keyservers = plain_items;
      plain_items = NULL;
    }

 leave:
  release_uri_item_list (onion_items);
  release_uri_item_list (plain_items);

  return err;
}


static const char hlp_keyserver[] =
  "KEYSERVER [<options>] [<uri>|<host>]\n"
  "Options are:\n"
  "  --help\n"
  "  --clear      Remove all configured keyservers\n"
  "  --resolve    Resolve HKP host names and rotate\n"
  "  --hosttable  Print table of known hosts and pools\n"
  "  --dead       Mark <host> as dead\n"
  "  --alive      Mark <host> as alive\n"
  "\n"
  "If called without arguments list all configured keyserver URLs.\n"
  "If called with an URI add this as keyserver.  Note that keyservers\n"
  "are configured on a per-session base.  A default keyserver may already be\n"
  "present, thus the \"--clear\" option must be used to get full control.\n"
  "If \"--clear\" and an URI are used together the clear command is\n"
  "obviously executed first.  A RESET command does not change the list\n"
  "of configured keyservers.";
static gpg_error_t
cmd_keyserver (assuan_context_t ctx, char *line)
{
  ctrl_t ctrl = assuan_get_pointer (ctx);
  gpg_error_t err = 0;
  int clear_flag, add_flag, help_flag, host_flag, resolve_flag;
  int dead_flag, alive_flag;
  uri_item_t item = NULL; /* gcc 4.4.5 is not able to detect that it
                             is always initialized.  */

  clear_flag = has_option (line, "--clear");
  help_flag = has_option (line, "--help");
  resolve_flag = has_option (line, "--resolve");
  host_flag = has_option (line, "--hosttable");
  dead_flag = has_option (line, "--dead");
  alive_flag = has_option (line, "--alive");
  line = skip_options (line);
  add_flag = !!*line;

  if (help_flag)
    {
      err = ks_action_help (ctrl, line);
      goto leave;
    }

  if (resolve_flag)
    {
      err = ensure_keyserver (ctrl);
      if (err)
        {
          assuan_set_error (ctx, err,
                            "Bad keyserver configuration in dirmngr.conf");
          goto leave;
        }
      err = ks_action_resolve (ctrl, ctrl->server_local->keyservers);
      if (err)
        goto leave;
    }

  if (alive_flag && dead_flag)
    {
      err = set_error (GPG_ERR_ASS_PARAMETER, "no support for zombies");
      goto leave;
    }
  if (dead_flag)
    {
      err = check_owner_permission (ctx, "no permission to use --dead");
      if (err)
        goto leave;
    }
  if (alive_flag || dead_flag)
    {
      if (!*line)
        {
          err = set_error (GPG_ERR_ASS_PARAMETER, "name of host missing");
          goto leave;
        }

      err = ks_hkp_mark_host (ctrl, line, alive_flag);
      if (err)
        goto leave;
    }

  if (host_flag)
    {
      err = ks_hkp_print_hosttable (ctrl);
      if (err)
        goto leave;
    }
  if (resolve_flag || host_flag || alive_flag || dead_flag)
    goto leave;

  if (add_flag)
    {
      err = make_keyserver_item (line, &item);
      if (err)
        goto leave;
    }
  if (clear_flag)
    release_ctrl_keyservers (ctrl);
  if (add_flag)
    {
      item->next = ctrl->server_local->keyservers;
      ctrl->server_local->keyservers = item;
    }

  if (!add_flag && !clear_flag && !help_flag)
    {
      /* List configured keyservers.  However, we first add a global
         keyserver. */
      uri_item_t u;

      err = ensure_keyserver (ctrl);
      if (err)
        {
          assuan_set_error (ctx, err,
                            "Bad keyserver configuration in dirmngr.conf");
          goto leave;
        }

      for (u=ctrl->server_local->keyservers; u; u = u->next)
        dirmngr_status (ctrl, "KEYSERVER", u->uri, NULL);
    }
  err = 0;

 leave:
  return leave_cmd (ctx, err);
}



static const char hlp_ks_search[] =
  "KS_SEARCH {<pattern>}\n"
  "\n"
  "Search the configured OpenPGP keyservers (see command KEYSERVER)\n"
  "for keys matching PATTERN";
static gpg_error_t
cmd_ks_search (assuan_context_t ctx, char *line)
{
  ctrl_t ctrl = assuan_get_pointer (ctx);
  gpg_error_t err;
  strlist_t list, sl;
  char *p;
  estream_t outfp;

  if (has_option (line, "--quick"))
    ctrl->timeout = opt.connect_quick_timeout;
  line = skip_options (line);

  /* Break the line down into an strlist.  Each pattern is
     percent-plus escaped. */
  list = NULL;
  for (p=line; *p; line = p)
    {
      while (*p && *p != ' ')
        p++;
      if (*p)
        *p++ = 0;
      if (*line)
        {
          sl = xtrymalloc (sizeof *sl + strlen (line));
          if (!sl)
            {
              err = gpg_error_from_syserror ();
              goto leave;
            }
          sl->flags = 0;
          strcpy_escaped_plus (sl->d, line);
          sl->next = list;
          list = sl;
        }
    }

  err = ensure_keyserver (ctrl);
  if (err)
    goto leave;

  /* Setup an output stream and perform the search.  */
  outfp = es_fopencookie (ctx, "w", data_line_cookie_functions);
  if (!outfp)
    err = set_error (GPG_ERR_ASS_GENERAL, "error setting up a data stream");
  else
    {
      err = ks_action_search (ctrl, ctrl->server_local->keyservers,
			      list, outfp);
      es_fclose (outfp);
    }

 leave:
  free_strlist (list);
  return leave_cmd (ctx, err);
}



static const char hlp_ks_get[] =
  "KS_GET [--quick] [--ldap] [--first|--next] {<pattern>}\n"
  "\n"
  "Get the keys matching PATTERN from the configured OpenPGP keyservers\n"
  "(see command KEYSERVER).  Each pattern should be a keyid, a fingerprint,\n"
  "or an exact name indicated by the '=' prefix.  Option --quick uses a\n"
  "shorter timeout; --ldap will use only ldap servers.  With --first only\n"
  "the first item is returned; --next is used to return the next item";
static gpg_error_t
cmd_ks_get (assuan_context_t ctx, char *line)
{
  ctrl_t ctrl = assuan_get_pointer (ctx);
  gpg_error_t err;
  strlist_t list, sl;
  char *p;
  estream_t outfp;
  unsigned int flags = 0;

  if (has_option (line, "--quick"))
    ctrl->timeout = opt.connect_quick_timeout;
  if (has_option (line, "--ldap"))
    flags |= KS_GET_FLAG_ONLY_LDAP;
  if (has_option (line, "--first"))
    flags |= KS_GET_FLAG_FIRST;
  if (has_option (line, "--next"))
    flags |= KS_GET_FLAG_NEXT;
  line = skip_options (line);

  /* Break the line into a strlist.  Each pattern is by
     definition percent-plus escaped.  However we only support keyids
     and fingerprints and thus the client has no need to apply the
     escaping.  */
  list = NULL;
  for (p=line; *p; line = p)
    {
      while (*p && *p != ' ')
        p++;
      if (*p)
        *p++ = 0;
      if (*line)
        {
          sl = xtrymalloc (sizeof *sl + strlen (line));
          if (!sl)
            {
              err = gpg_error_from_syserror ();
              goto leave;
            }
          sl->flags = 0;
          strcpy_escaped_plus (sl->d, line);
          sl->next = list;
          list = sl;
        }
    }

  if ((flags & KS_GET_FLAG_FIRST) && !(flags & KS_GET_FLAG_ONLY_LDAP))
    {
      err = PARM_ERROR ("--first is only supported with --ldap");
      goto leave;
    }

  if (list && list->next && (flags & KS_GET_FLAG_FIRST))
    {
      /* ks_action_get loops over the pattern and we can't easily keep
       * this state.  */
      err = PARM_ERROR ("Only one pattern allowed with --first");
      goto leave;
    }

  if (!list && (flags & KS_GET_FLAG_FIRST))
    {
      /* Need to add a dummy pattern if no pattern is given.  */
      if (!add_to_strlist_try (&list, ""))
        {
          err = gpg_error_from_syserror ();
          goto leave;
        }
    }


  if ((flags & KS_GET_FLAG_NEXT))
    {
      if (list || (flags & ~KS_GET_FLAG_NEXT))
        {
          err = PARM_ERROR ("No pattern or other options allowed with --next");
          goto leave;
        }
      /* Add a dummy pattern.  */
      if (!add_to_strlist_try (&list, ""))
        {
          err = gpg_error_from_syserror ();
          goto leave;
        }
    }


  err = ensure_keyserver (ctrl);
  if (err)
    goto leave;

  /* Setup an output stream and perform the get.  */
  outfp = es_fopencookie (ctx, "w", data_line_cookie_functions);
  if (!outfp)
    err = set_error (GPG_ERR_ASS_GENERAL, "error setting up a data stream");
  else
    {
      ctrl->server_local->inhibit_data_logging = 1;
      ctrl->server_local->inhibit_data_logging_now = 0;
      ctrl->server_local->inhibit_data_logging_count = 0;
      err = ks_action_get (ctrl, ctrl->server_local->keyservers,
                           list, flags, outfp);
      es_fclose (outfp);
      ctrl->server_local->inhibit_data_logging = 0;
    }

 leave:
  free_strlist (list);
  return leave_cmd (ctx, err);
}


static const char hlp_ks_fetch[] =
  "KS_FETCH <URL>\n"
  "\n"
  "Get the key(s) from URL.";
static gpg_error_t
cmd_ks_fetch (assuan_context_t ctx, char *line)
{
  ctrl_t ctrl = assuan_get_pointer (ctx);
  gpg_error_t err;
  estream_t outfp;

  if (has_option (line, "--quick"))
    ctrl->timeout = opt.connect_quick_timeout;
  line = skip_options (line);

  err = ensure_keyserver (ctrl);  /* FIXME: Why do we needs this here?  */
  if (err)
    goto leave;

  /* Setup an output stream and perform the get.  */
  outfp = es_fopencookie (ctx, "w", data_line_cookie_functions);
  if (!outfp)
    err = set_error (GPG_ERR_ASS_GENERAL, "error setting up a data stream");
  else
    {
      ctrl->server_local->inhibit_data_logging = 1;
      ctrl->server_local->inhibit_data_logging_now = 0;
      ctrl->server_local->inhibit_data_logging_count = 0;
      err = ks_action_fetch (ctrl, line, outfp);
      es_fclose (outfp);
      ctrl->server_local->inhibit_data_logging = 0;
    }

 leave:
  return leave_cmd (ctx, err);
}



static const char hlp_ks_put[] =
  "KS_PUT\n"
  "\n"
  "Send a key to the configured OpenPGP keyservers.  The actual key material\n"
  "is then requested by Dirmngr using\n"
  "\n"
  "  INQUIRE KEYBLOCK\n"
  "\n"
  "The client shall respond with a binary version of the keyblock (e.g.,\n"
  "the output of `gpg --export KEYID').  For LDAP\n"
  "keyservers Dirmngr may ask for meta information of the provided keyblock\n"
  "using:\n"
  "\n"
  "  INQUIRE KEYBLOCK_INFO\n"
  "\n"
  "The client shall respond with a colon delimited info lines (the output\n"
  "of 'gpg --list-keys --with-colons KEYID').\n";
static gpg_error_t
cmd_ks_put (assuan_context_t ctx, char *line)
{
  ctrl_t ctrl = assuan_get_pointer (ctx);
  gpg_error_t err;
  unsigned char *value = NULL;
  size_t valuelen;
  unsigned char *info = NULL;
  size_t infolen;

  /* No options for now.  */
  line = skip_options (line);

  err = ensure_keyserver (ctrl);
  if (err)
    goto leave;

  /* Ask for the key material.  */
  err = assuan_inquire (ctx, "KEYBLOCK",
                        &value, &valuelen, MAX_KEYBLOCK_LENGTH);
  if (err)
    {
      log_error (_("assuan_inquire failed: %s\n"), gpg_strerror (err));
      goto leave;
    }

  if (!valuelen) /* No data returned; return a comprehensible error. */
    {
      err = gpg_error (GPG_ERR_MISSING_CERT);
      goto leave;
    }

  /* Ask for the key meta data.  */
  err = assuan_inquire (ctx, "KEYBLOCK_INFO",
                        &info, &infolen, MAX_KEYBLOCK_LENGTH);
  if (err)
    {
      log_error (_("assuan_inquire failed: %s\n"), gpg_strerror (err));
      goto leave;
    }

  /* Send the key.  */
  err = ks_action_put (ctrl, ctrl->server_local->keyservers,
		       value, valuelen, info, infolen);

 leave:
  xfree (info);
  xfree (value);
  return leave_cmd (ctx, err);
}



static const char hlp_loadswdb[] =
  "LOADSWDB [--force]\n"
  "\n"
  "Load and verify the swdb.lst from the Net.";
static gpg_error_t
cmd_loadswdb (assuan_context_t ctx, char *line)
{
  ctrl_t ctrl = assuan_get_pointer (ctx);
  gpg_error_t err;

  err = dirmngr_load_swdb (ctrl, has_option (line, "--force"));

  return leave_cmd (ctx, err);
}



static const char hlp_getinfo[] =
  "GETINFO <what>\n"
  "\n"
  "Multi purpose command to return certain information.  \n"
  "Supported values of WHAT are:\n"
  "\n"
  "version     - Return the version of the program.\n"
  "pid         - Return the process id of the server.\n"
  "tor         - Return OK if running in Tor mode\n"
  "dnsinfo     - Return info about the DNS resolver\n"
  "socket_name - Return the name of the socket.\n"
  "session_id  - Return the current session_id.\n"
  "workqueue   - Inspect the work queue\n"
  "getenv NAME - Return value of envvar NAME\n";
static gpg_error_t
cmd_getinfo (assuan_context_t ctx, char *line)
{
  ctrl_t ctrl = assuan_get_pointer (ctx);
  gpg_error_t err;
  char numbuf[50];

  if (!strcmp (line, "version"))
    {
      const char *s = VERSION;
      err = assuan_send_data (ctx, s, strlen (s));
    }
  else if (!strcmp (line, "pid"))
    {
      snprintf (numbuf, sizeof numbuf, "%lu", (unsigned long)getpid ());
      err = assuan_send_data (ctx, numbuf, strlen (numbuf));
    }
  else if (!strcmp (line, "socket_name"))
    {
      const char *s = dirmngr_get_current_socket_name ();
      err = assuan_send_data (ctx, s, strlen (s));
    }
  else if (!strcmp (line, "session_id"))
    {
      snprintf (numbuf, sizeof numbuf, "%u", ctrl->server_local->session_id);
      err = assuan_send_data (ctx, numbuf, strlen (numbuf));
    }
  else if (!strcmp (line, "tor"))
    {
      int use_tor;

      use_tor = dirmngr_use_tor ();
      if (use_tor)
        {
          if (!is_tor_running (ctrl))
            err = assuan_write_status (ctx, "NO_TOR", "Tor not running");
          else
            err = 0;
          if (!err)
            assuan_set_okay_line (ctx, use_tor == 1 ? "- Tor mode is enabled"
                                  /**/              : "- Tor mode is enforced");
        }
      else
        err = set_error (GPG_ERR_FALSE, "Tor mode is NOT enabled");
    }
  else if (!strcmp (line, "dnsinfo"))
    {
      if (standard_resolver_p ())
        assuan_set_okay_line
          (ctx, "- Forced use of System resolver (w/o Tor support)");
      else
        {
#ifdef USE_LIBDNS
          assuan_set_okay_line (ctx, (recursive_resolver_p ()
                                      ? "- Libdns recursive resolver"
                                      : "- Libdns stub resolver"));
#else
          assuan_set_okay_line (ctx, "- System resolver (w/o Tor support)");
#endif
        }
      err = 0;
    }
  else if (!strcmp (line, "workqueue"))
    {
      workqueue_dump_queue (ctrl);
      err = 0;
    }
  else if (!strncmp (line, "getenv", 6)
           && (line[6] == ' ' || line[6] == '\t' || !line[6]))
    {
      line += 6;
      while (*line == ' ' || *line == '\t')
        line++;
      if (!*line)
        err = gpg_error (GPG_ERR_MISSING_VALUE);
      else
        {
          const char *s = getenv (line);
          if (!s)
            err = set_error (GPG_ERR_NOT_FOUND, "No such envvar");
          else
            err = assuan_send_data (ctx, s, strlen (s));
        }
    }
  else
    err = set_error (GPG_ERR_ASS_PARAMETER, "unknown value for WHAT");

  return leave_cmd (ctx, err);
}



static const char hlp_killdirmngr[] =
  "KILLDIRMNGR\n"
  "\n"
  "This command allows a user - given sufficient permissions -\n"
  "to kill this dirmngr process.\n";
static gpg_error_t
cmd_killdirmngr (assuan_context_t ctx, char *line)
{
  ctrl_t ctrl = assuan_get_pointer (ctx);

  (void)line;

  ctrl->server_local->stopme = 1;
  assuan_set_flag (ctx, ASSUAN_FORCE_CLOSE, 1);
  return 0;
}


static const char hlp_reloaddirmngr[] =
  "RELOADDIRMNGR\n"
  "\n"
  "This command is an alternative to SIGHUP\n"
  "to reload the configuration.";
static gpg_error_t
cmd_reloaddirmngr (assuan_context_t ctx, char *line)
{
  (void)ctx;
  (void)line;

  dirmngr_sighup_action ();
  return 0;
}


static const char hlp_flushcrls[] =
  "FLUSHCRLS\n"
  "\n"
  "Remove all cached CRLs from memory and\n"
  "the file system.";
static gpg_error_t
cmd_flushcrls (assuan_context_t ctx, char *line)
{
  (void)line;

  return leave_cmd (ctx, crl_cache_flush () ? GPG_ERR_GENERAL : 0);
}



/* Tell the assuan library about our commands. */
static int
register_commands (assuan_context_t ctx)
{
  static struct {
    const char *name;
    assuan_handler_t handler;
    const char * const help;
  } table[] = {
    { "DNS_CERT",   cmd_dns_cert,   hlp_dns_cert },
    { "WKD_GET",    cmd_wkd_get,    hlp_wkd_get },
    { "LDAPSERVER", cmd_ldapserver, hlp_ldapserver },
    { "ISVALID",    cmd_isvalid,    hlp_isvalid },
    { "CHECKCRL",   cmd_checkcrl,   hlp_checkcrl },
    { "CHECKOCSP",  cmd_checkocsp,  hlp_checkocsp },
    { "LOOKUP",     cmd_lookup,     hlp_lookup },
    { "LOADCRL",    cmd_loadcrl,    hlp_loadcrl },
    { "LISTCRLS",   cmd_listcrls,   hlp_listcrls },
    { "CACHECERT",  cmd_cachecert,  hlp_cachecert },
    { "VALIDATE",   cmd_validate,   hlp_validate },
    { "KEYSERVER",  cmd_keyserver,  hlp_keyserver },
    { "KS_SEARCH",  cmd_ks_search,  hlp_ks_search },
    { "KS_GET",     cmd_ks_get,     hlp_ks_get },
    { "KS_FETCH",   cmd_ks_fetch,   hlp_ks_fetch },
    { "KS_PUT",     cmd_ks_put,     hlp_ks_put },
    { "GETINFO",    cmd_getinfo,    hlp_getinfo },
    { "LOADSWDB",   cmd_loadswdb,   hlp_loadswdb },
    { "KILLDIRMNGR",cmd_killdirmngr,hlp_killdirmngr },
    { "RELOADDIRMNGR",cmd_reloaddirmngr,hlp_reloaddirmngr },
    { "FLUSHCRLS",  cmd_flushcrls,  hlp_flushcrls },
    { NULL, NULL }
  };
  int i, j, rc;

  for (i=j=0; table[i].name; i++)
    {
      rc = assuan_register_command (ctx, table[i].name, table[i].handler,
                                    table[i].help);
      if (rc)
        return rc;
    }
  return 0;
}


/* Note that we do not reset the list of configured keyservers.  */
static gpg_error_t
reset_notify (assuan_context_t ctx, char *line)
{
  ctrl_t ctrl = assuan_get_pointer (ctx);
  (void)line;

#if USE_LDAP
  ldapserver_list_free (ctrl->server_local->ldapservers);
#endif /*USE_LDAP*/
  ctrl->server_local->ldapservers = NULL;
  return 0;
}


/* This function is called by our assuan log handler to test whether a
 * log message shall really be printed.  The function must return
 * false to inhibit the logging of MSG.  CAT gives the requested log
 * category.  MSG might be NULL. */
int
dirmngr_assuan_log_monitor (assuan_context_t ctx, unsigned int cat,
                            const char *msg)
{
  ctrl_t ctrl = assuan_get_pointer (ctx);

  (void)cat;
  (void)msg;

  if (!ctrl || !ctrl->server_local)
    return 1; /* Can't decide - allow logging.  */

  if (!ctrl->server_local->inhibit_data_logging)
    return 1; /* Not requested - allow logging.  */

  /* Disallow logging if *_now is true.  */
  return !ctrl->server_local->inhibit_data_logging_now;
}


/* Startup the server and run the main command loop.  With FD = -1,
 * use stdin/stdout.  SESSION_ID is either 0 or a unique number
 * identifying a session.  */
void
start_command_handler (assuan_fd_t fd, unsigned int session_id)
{
  static const char hello[] = "Dirmngr " VERSION " at your service";
  static char *hello_line;
  int rc;
  assuan_context_t ctx;
  ctrl_t ctrl;

  ctrl = xtrycalloc (1, sizeof *ctrl);
  if (ctrl)
    ctrl->server_local = xtrycalloc (1, sizeof *ctrl->server_local);
  if (!ctrl || !ctrl->server_local)
    {
      log_error (_("can't allocate control structure: %s\n"),
                 strerror (errno));
      xfree (ctrl);
      return;
    }

  dirmngr_init_default_ctrl (ctrl);

  rc = assuan_new (&ctx);
  if (rc)
    {
      log_error (_("failed to allocate assuan context: %s\n"),
		 gpg_strerror (rc));
      dirmngr_exit (2);
    }

  if (fd == ASSUAN_INVALID_FD)
    {
      assuan_fd_t filedes[2];

      filedes[0] = assuan_fdopen (0);
      filedes[1] = assuan_fdopen (1);
      rc = assuan_init_pipe_server (ctx, filedes);
    }
  else
    {
      rc = assuan_init_socket_server (ctx, fd, ASSUAN_SOCKET_SERVER_ACCEPTED);
    }

  if (rc)
    {
      assuan_release (ctx);
      log_error (_("failed to initialize the server: %s\n"),
                 gpg_strerror(rc));
      dirmngr_exit (2);
    }

  rc = register_commands (ctx);
  if (rc)
    {
      log_error (_("failed to the register commands with Assuan: %s\n"),
                 gpg_strerror(rc));
      dirmngr_exit (2);
    }


  if (!hello_line)
    {
      hello_line = xtryasprintf
        ("Home: %s\n"
         "Config: %s\n"
         "%s",
         gnupg_homedir (),
         opt.config_filename? opt.config_filename : "[none]",
         hello);
    }

  ctrl->server_local->assuan_ctx = ctx;
  assuan_set_pointer (ctx, ctrl);

  assuan_set_hello_line (ctx, hello_line);
  assuan_register_option_handler (ctx, option_handler);
  assuan_register_reset_notify (ctx, reset_notify);

  ctrl->server_local->session_id = session_id;

  for (;;)
    {
      rc = assuan_accept (ctx);
      if (rc == -1)
        break;
      if (rc)
        {
          log_info (_("Assuan accept problem: %s\n"), gpg_strerror (rc));
          break;
        }

#ifndef HAVE_W32_SYSTEM
      if (opt.verbose)
        {
	  assuan_peercred_t peercred;

          if (!assuan_get_peercred (ctx, &peercred))
            log_info ("connection from process %ld (%ld:%ld)\n",
                      (long)peercred->pid, (long)peercred->uid,
		      (long)peercred->gid);
        }
#endif

      rc = assuan_process (ctx);
      if (rc)
        {
          log_info (_("Assuan processing failed: %s\n"), gpg_strerror (rc));
          continue;
        }
    }


#if USE_LDAP
  ldap_wrapper_connection_cleanup (ctrl);

  ldapserver_list_free (ctrl->server_local->ldapservers);
#endif /*USE_LDAP*/
  ctrl->server_local->ldapservers = NULL;

  release_ctrl_keyservers (ctrl);

  ctrl->server_local->assuan_ctx = NULL;
  assuan_release (ctx);

  if (ctrl->server_local->stopme)
    dirmngr_exit (0);

  if (ctrl->refcount)
    log_error ("oops: connection control structure still referenced (%d)\n",
               ctrl->refcount);
  else
    {
      ks_ldap_free_state (ctrl->ks_get_state);
      ctrl->ks_get_state = NULL;
      release_ctrl_ocsp_certs (ctrl);
      xfree (ctrl->server_local);
      dirmngr_deinit_default_ctrl (ctrl);
      xfree (ctrl);
    }
}


/* Send a status line back to the client.  KEYWORD is the status
   keyword, the optional string arguments are blank separated added to
   the line, the last argument must be a NULL. */
gpg_error_t
dirmngr_status (ctrl_t ctrl, const char *keyword, ...)
{
  gpg_error_t err = 0;
  va_list arg_ptr;
  assuan_context_t ctx;

  va_start (arg_ptr, keyword);

  if (ctrl->server_local && (ctx = ctrl->server_local->assuan_ctx))
    {
      err = vprint_assuan_status_strings (ctx, keyword, arg_ptr);
    }

  va_end (arg_ptr);
  return err;
}


/* Print a help status line.  The function splits text at LFs.  */
gpg_error_t
dirmngr_status_help (ctrl_t ctrl, const char *text)
{
  gpg_error_t err = 0;
  assuan_context_t ctx;

  if (ctrl->server_local && (ctx = ctrl->server_local->assuan_ctx))
    {
      char buf[950], *p;
      size_t n;

      do
        {
          p = buf;
          n = 0;
          for ( ; *text && *text != '\n' && n < DIM (buf)-2; n++)
            *p++ = *text++;
          if (*text == '\n')
            text++;
          *p = 0;
          err = assuan_write_status (ctx, "#", buf);
        }
      while (!err && *text);
    }

  return err;
}


/* Print a help status line using a printf like format.  The function
 * splits text at LFs.  */
gpg_error_t
dirmngr_status_helpf (ctrl_t ctrl, const char *format, ...)
{
  va_list arg_ptr;
  gpg_error_t err;
  char *buf;

  va_start (arg_ptr, format);
  buf = es_vbsprintf (format, arg_ptr);
  err = buf? 0 : gpg_error_from_syserror ();
  va_end (arg_ptr);
  if (!err)
    err = dirmngr_status_help (ctrl, buf);
  es_free (buf);
  return err;
}


/* This function is similar to print_assuan_status but takes a CTRL
 * arg instead of an assuan context as first argument.  */
gpg_error_t
dirmngr_status_printf (ctrl_t ctrl, const char *keyword,
                       const char *format, ...)
{
  gpg_error_t err;
  va_list arg_ptr;
  assuan_context_t ctx;

  if (!ctrl->server_local || !(ctx = ctrl->server_local->assuan_ctx))
    return 0;

  va_start (arg_ptr, format);
  err = vprint_assuan_status (ctx, keyword, format, arg_ptr);
  va_end (arg_ptr);
  return err;
}


/* Send a tick progress indicator back.  Fixme: This is only done for
   the currently active channel.  */
gpg_error_t
dirmngr_tick (ctrl_t ctrl)
{
  static time_t next_tick = 0;
  gpg_error_t err = 0;
  time_t now = time (NULL);

  if (!next_tick)
    {
      next_tick = now + 1;
    }
  else if ( now > next_tick )
    {
      if (ctrl)
        {
          err = dirmngr_status (ctrl, "PROGRESS", "tick", "? 0 0", NULL);
          if (err)
            {
              /* Take this as in indication for a cancel request.  */
              err = gpg_error (GPG_ERR_CANCELED);
            }
          now = time (NULL);
        }

      next_tick = now + 1;
    }
  return err;
}