File: iterate.c

package info (click to toggle)
certmonger 0.79.17-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 6,460 kB
  • sloc: ansic: 58,328; sh: 6,735; makefile: 885; python: 563; xml: 296
file content (2852 lines) | stat: -rw-r--r-- 85,944 bytes parent folder | download | duplicates (3)
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
/*
 * Copyright (C) 2009,2010,2011,2012,2013,2014,2015,2016 Red Hat, Inc.
 * 
 * This program 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.
 *
 * This program 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 <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <sys/types.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include <dbus/dbus.h>
#include <talloc.h>

#include "cadata.h"
#include "canalyze.h"
#include "casave.h"
#include "certread.h"
#include "certsave.h"
#include "cm.h"
#include "csrgen.h"
#include "hook.h"
#include "iterate.h"
#include "keygen.h"
#include "keyiread.h"
#include "log.h"
#include "notify.h"
#include "prefs.h"
#include "scepgen.h"
#include "store.h"
#include "store-int.h"
#include "submit.h"
#include "tm.h"

struct cm_entry_state {
	struct cm_keygen_state *cm_keygen_state;
	struct cm_keyiread_state *cm_keyiread_state;
	struct cm_csrgen_state *cm_csrgen_state;
	struct cm_scepgen_state *cm_scepgen_state;
	struct cm_submit_state *cm_submit_state;
	struct cm_certsave_state *cm_certsave_state;
	struct cm_hook_state *cm_hook_state;
	struct cm_certread_state *cm_certread_state;
	struct cm_notify_state *cm_notify_state;
	struct cm_casave_state *cm_casave_state;
};

struct cm_ca_state {
	enum cm_ca_phase cm_phase;
	struct cm_ca_analyze_state *cm_ca_cert_analyze_state;
	struct cm_ca_analyze_state *cm_ca_ecert_analyze_state;
	time_t cm_cert_refresh_delay;
	time_t cm_ecert_refresh_delay;
	struct cm_cadata_state *cm_task_state;
	struct cm_hook_state *cm_hook_state;
	struct cm_casave_state *cm_casave_state;
};

/* Helper routine to replace in-progress states with the previous "stable"
 * state. */
static void
cm_entry_reset_state(struct cm_store_entry *entry)
{
	switch (entry->cm_state) {
	case CM_NEED_KEY_PAIR:
		break;
	case CM_GENERATING_KEY_PAIR:
		entry->cm_state = CM_NEED_KEY_PAIR;
		break;
	case CM_NEED_KEY_GEN_TOKEN:
		entry->cm_state = CM_NEED_KEY_PAIR;
		break;
	case CM_NEED_KEY_GEN_PIN:
		entry->cm_state = CM_NEED_KEY_PAIR;
		break;
	case CM_NEED_KEY_GEN_PERMS:
		entry->cm_state = CM_NEED_KEY_PAIR;
		break;
	case CM_HAVE_KEY_PAIR:
		break;
	case CM_NEED_KEYINFO:
		break;
	case CM_READING_KEYINFO:
		entry->cm_state = CM_NEED_KEYINFO;
		break;
	case CM_NEED_KEYINFO_READ_TOKEN:
		entry->cm_state = CM_NEED_KEYINFO;
		break;
	case CM_NEED_KEYINFO_READ_PIN:
		entry->cm_state = CM_NEED_KEYINFO;
		break;
	case CM_HAVE_KEYINFO:
		break;
	case CM_NEED_CSR:
		entry->cm_state = CM_HAVE_KEYINFO;
		break;
	case CM_NEED_CSR_GEN_TOKEN:
		entry->cm_state = CM_HAVE_KEYINFO;
		break;
	case CM_NEED_CSR_GEN_PIN:
		entry->cm_state = CM_HAVE_KEYINFO;
		break;
	case CM_GENERATING_CSR:
		entry->cm_state = CM_HAVE_KEYINFO;
		break;
	case CM_HAVE_CSR:
		break;
	case CM_NEED_SCEP_DATA:
		break;
	case CM_NEED_SCEP_GEN_TOKEN:
		entry->cm_state = CM_NEED_SCEP_DATA;
		break;
	case CM_NEED_SCEP_GEN_PIN:
		entry->cm_state = CM_NEED_SCEP_DATA;
		break;
	case CM_NEED_SCEP_ENCRYPTION_CERT:
		entry->cm_state = CM_NEED_SCEP_DATA;
		break;
	case CM_NEED_SCEP_RSA_CLIENT_KEY:
		entry->cm_state = CM_NEED_SCEP_DATA;
		break;
	case CM_GENERATING_SCEP_DATA:
		entry->cm_state = CM_NEED_SCEP_DATA;
		break;
	case CM_HAVE_SCEP_DATA:
		break;
	case CM_NEED_TO_SUBMIT:
		entry->cm_state = CM_HAVE_CSR;
		break;
	case CM_SUBMITTING:
		entry->cm_state = CM_HAVE_CSR;
		break;
	case CM_NEED_TO_SAVE_CERT:
		break;
	case CM_START_SAVING_CERT:
		entry->cm_state = CM_NEED_TO_SAVE_CERT;
		break;
	case CM_PRE_SAVE_CERT:
		entry->cm_state = CM_NEED_TO_SAVE_CERT;
		break;
	case CM_SAVING_CERT:
		entry->cm_state = CM_NEED_TO_SAVE_CERT;
		break;
	case CM_NEED_CERTSAVE_PERMS:
		entry->cm_state = CM_NEED_TO_SAVE_CERT;
		break;
	case CM_NEED_CERTSAVE_TOKEN:
		entry->cm_state = CM_NEED_TO_SAVE_CERT;
		break;
	case CM_NEED_CERTSAVE_PIN:
		entry->cm_state = CM_NEED_TO_SAVE_CERT;
		break;
	case CM_NEED_TO_SAVE_CA_CERTS:
		break;
	case CM_START_SAVING_CA_CERTS:
		entry->cm_state = CM_NEED_TO_SAVE_CA_CERTS;
		break;
	case CM_SAVING_CA_CERTS:
		entry->cm_state = CM_NEED_TO_SAVE_CA_CERTS;
		break;
	case CM_NEED_CA_CERT_SAVE_PERMS:
		entry->cm_state = CM_NEED_TO_SAVE_CA_CERTS;
		break;
	case CM_NEED_TO_NOTIFY_ISSUED_CA_SAVE_FAILED:
		entry->cm_state = CM_NEED_TO_NOTIFY_ISSUED_CA_SAVE_FAILED;
		break;
	case CM_NOTIFYING_ISSUED_CA_SAVE_FAILED:
		entry->cm_state = CM_NEED_TO_NOTIFY_ISSUED_CA_SAVE_FAILED;
		break;
	case CM_NEED_TO_READ_CERT:
		break;
	case CM_READING_CERT:
		entry->cm_state = CM_NEED_TO_READ_CERT;
		break;
	case CM_SAVED_CERT:
		break;
	case CM_POST_SAVED_CERT:
		entry->cm_state = CM_NEED_TO_NOTIFY_ISSUED_SAVED;
		break;
	case CM_CA_REJECTED:
		break;
	case CM_CA_WORKING:
		entry->cm_state = CM_HAVE_CSR;
		break;
	case CM_CA_UNREACHABLE:
		entry->cm_state = CM_HAVE_CSR;
		break;
	case CM_CA_UNCONFIGURED:
		entry->cm_state = CM_HAVE_CSR;
		break;
	case CM_NEED_CA:
		entry->cm_state = CM_HAVE_CSR;
		break;
	case CM_NEED_GUIDANCE:
		break;
	case CM_MONITORING:
		break;
	case CM_NEED_TO_NOTIFY_VALIDITY:
		entry->cm_state = CM_MONITORING;
		break;
	case CM_NOTIFYING_VALIDITY:
		entry->cm_state = CM_NEED_TO_NOTIFY_VALIDITY;
		break;
	case CM_NEED_TO_NOTIFY_REJECTION:
		break;
	case CM_NOTIFYING_REJECTION:
		entry->cm_state = CM_NEED_TO_NOTIFY_REJECTION;
		break;
	case CM_NEED_TO_NOTIFY_ISSUED_SAVE_FAILED:
		break;
	case CM_NOTIFYING_ISSUED_SAVE_FAILED:
		entry->cm_state = CM_NEED_TO_NOTIFY_ISSUED_SAVE_FAILED;
		break;
	case CM_NEED_TO_NOTIFY_ISSUED_SAVED:
		break;
	case CM_NOTIFYING_ISSUED_SAVED:
		entry->cm_state = CM_NEED_TO_NOTIFY_ISSUED_SAVED;
		break;
	case CM_NEED_TO_SAVE_ONLY_CA_CERTS:
		entry->cm_state = CM_NEED_TO_SAVE_ONLY_CA_CERTS;
		break;
	case CM_START_SAVING_ONLY_CA_CERTS:
		entry->cm_state = CM_NEED_TO_SAVE_ONLY_CA_CERTS;
		break;
	case CM_SAVING_ONLY_CA_CERTS:
		entry->cm_state = CM_NEED_TO_SAVE_ONLY_CA_CERTS;
		break;
	case CM_NEED_ONLY_CA_CERT_SAVE_PERMS:
		entry->cm_state = CM_NEED_TO_SAVE_ONLY_CA_CERTS;
		break;
	case CM_NEED_TO_NOTIFY_ONLY_CA_SAVE_FAILED:
		entry->cm_state = CM_NEED_TO_NOTIFY_ONLY_CA_SAVE_FAILED;
		break;
	case CM_NOTIFYING_ONLY_CA_SAVE_FAILED:
		entry->cm_state = CM_NEED_TO_NOTIFY_ONLY_CA_SAVE_FAILED;
		break;
	case CM_NEWLY_ADDED:
		break;
	case CM_NEWLY_ADDED_START_READING_KEYINFO:
		entry->cm_state = CM_NEWLY_ADDED;
		break;
	case CM_NEWLY_ADDED_READING_KEYINFO:
		entry->cm_state = CM_NEWLY_ADDED;
		break;
	case CM_NEWLY_ADDED_NEED_KEYINFO_READ_TOKEN:
		entry->cm_state = CM_NEWLY_ADDED;
		break;
	case CM_NEWLY_ADDED_NEED_KEYINFO_READ_PIN:
		entry->cm_state = CM_NEWLY_ADDED;
		break;
	case CM_NEWLY_ADDED_START_READING_CERT:
		entry->cm_state = CM_NEWLY_ADDED;
		break;
	case CM_NEWLY_ADDED_READING_CERT:
		entry->cm_state = CM_NEWLY_ADDED;
		break;
	case CM_NEWLY_ADDED_DECIDING:
		entry->cm_state = CM_NEWLY_ADDED;
		break;
	case CM_INVALID:
		/* not reached */
		abort();
		break;
	}
}

void
cm_waitfor_readable_fd(int fd, int delay)
{
	fd_set fds, *fdset = NULL;
	struct timeval tv;

	memset(&tv, 0, sizeof(tv));
	tv.tv_sec = delay;
	FD_ZERO(&fds);
	if (fd != -1) {
		fdset = &fds;
		FD_SET(fd, fdset);
	}
	if (select(fd + 1, fdset, NULL, fdset, (delay >= 0) ? &tv : NULL) < 0) {
		if (delay < 0) {
			/* No defined delay, but an error. */
			cm_log(3, "indefinite select() on %d returned error: "
			       "%s\n", fd, strerror(errno));
		}
	}
}

/* Decide how long to wait before contacting the CA for an enrollment request
 * again. */
static time_t
cm_decide_ca_delay(time_t remaining)
{
	time_t delay;

	delay = CM_DELAY_CA_POLL;
	if ((remaining != (time_t) -1) && (remaining < 2 * delay)) {
		delay = remaining / 2;
		if (delay < CM_DELAY_CA_POLL_MINIMUM) {
			delay = CM_DELAY_CA_POLL_MINIMUM;
		}
		if (delay > CM_DELAY_CA_POLL_MAXIMUM) {
			delay = CM_DELAY_CA_POLL_MAXIMUM;
		}
	}
	return delay;
}

/* Decide how long to wait before looking at a certificate again. */
static time_t
cm_decide_monitor_delay(time_t remaining,
			int (*get_ttls1)(const time_t **,
					 unsigned int *),
			int (*get_ttls2)(const time_t **,
					 unsigned int *))
{
	time_t delay, next_ttl;
	unsigned i, n_ttls;
	const time_t *ttls;

	delay = CM_DELAY_MONITOR_POLL;
	if ((remaining != (time_t) -1) && (remaining > 0)) {
		next_ttl = 0;
		if ((get_ttls1(&ttls, &n_ttls) == 0) && (n_ttls > 0)) {
			for (i = 0; i < n_ttls; i++) {
				if ((remaining > ttls[i]) && (ttls[i] > next_ttl)) {
					next_ttl = ttls[i];
				}
			}
		}
		if ((get_ttls2(&ttls, &n_ttls) == 0) && (n_ttls > 0)) {
			for (i = 0; i < n_ttls; i++) {
				if ((remaining > ttls[i]) && (ttls[i] > next_ttl)) {
					next_ttl = ttls[i];
				}
			}
		}
		if ((next_ttl != 0) && (delay > remaining - next_ttl + CM_DELAY_SOON)) {
			delay = remaining - next_ttl + CM_DELAY_SOON;
		} else
		if (remaining < 2 * delay) {
			delay = remaining / 2;
		}
	}
	if (delay < CM_DELAY_MONITOR_POLL_MINIMUM) {
		delay = CM_DELAY_MONITOR_POLL_MINIMUM;
	}
	if (delay > CM_DELAY_MONITOR_POLL_MAXIMUM) {
		delay = CM_DELAY_MONITOR_POLL_MAXIMUM;
	}
	return delay;
}

/* Decide how long to wait before again attempting to contact the CA to
 * retrieve information about it. */
static time_t
cm_decide_cadata_delay(void)
{
	time_t delay;

	delay = CM_DELAY_CADATA_POLL;
	return delay;
}

/* Manage a "lock" that we use to serialize access to THE REST OF THE WORLD. */
static void *writing_lock;
static enum cm_ca_phase writing_lock_ca_phase = cm_ca_phase_invalid;
static dbus_bool_t
cm_writing_has_lock(void *holder, enum cm_ca_phase phase)
{
	return (writing_lock == holder) &&
	       ((writing_lock_ca_phase == cm_ca_phase_invalid) ||
		(writing_lock_ca_phase == phase));
}
static dbus_bool_t
cm_writing_lock_by_entry(struct cm_store_entry *entry)
{
	if ((writing_lock == entry) || (writing_lock == NULL)) {
		if (writing_lock == NULL) {
			cm_log(3, "%s('%s') taking writing lock\n",
			       entry->cm_busname, entry->cm_nickname);
			writing_lock = entry;
		} else {
			abort();
		}
		return TRUE;
	} else {
		return FALSE;
	}
}
static dbus_bool_t
cm_writing_unlock_by_entry(struct cm_store_entry *entry)
{
	if ((writing_lock == entry) || (writing_lock == NULL)) {
		if (writing_lock == entry) {
			cm_log(3, "%s('%s') releasing writing lock\n",
			       entry->cm_busname, entry->cm_nickname);
			writing_lock = NULL;
		} else {
			abort();
		}
		return TRUE;
	} else {
		return FALSE;
	}
}
static dbus_bool_t
cm_writing_lock_by_ca(struct cm_store_ca *ca, enum cm_ca_phase phase)
{
	if (((writing_lock == ca) && (writing_lock_ca_phase == phase)) ||
	    (writing_lock == NULL)) {
		if (writing_lock == NULL) {
			cm_log(3, "%s('%s').%s taking writing lock\n",
			       ca->cm_busname, ca->cm_nickname,
			       cm_store_ca_phase_as_string(phase));
			writing_lock = ca;
			if (phase == cm_ca_phase_invalid) {
				abort();
			}
			writing_lock_ca_phase = phase;
		} else {
			abort();
		}
		return TRUE;
	} else {
		return FALSE;
	}
}
static dbus_bool_t
cm_writing_unlock_by_ca(struct cm_store_ca *ca, enum cm_ca_phase phase)
{
	if (((writing_lock == ca) && (writing_lock_ca_phase == phase)) ||
	    (writing_lock == NULL)) {
		if (writing_lock == ca) {
			cm_log(3, "%s('%s').%s releasing writing lock\n",
			       ca->cm_busname, ca->cm_nickname,
			       cm_store_ca_phase_as_string(phase));
			writing_lock = NULL;
			writing_lock_ca_phase = cm_ca_phase_invalid;
		} else {
			abort();
		}
		return TRUE;
	} else {
		return FALSE;
	}
}

/* Set up run-time data associated with the entry. */
int
cm_iterate_entry_init(struct cm_store_entry *entry, void **cm_iterate_state)
{
	struct cm_entry_state *state;
	int fd;
	state = talloc_ptrtype(entry, state);
	if (state == NULL) {
		return ENOMEM;
	}
	memset(state, 0, sizeof(*state));
	*cm_iterate_state = state;
	cm_entry_reset_state(entry);
	if (cm_writing_has_lock(entry, cm_ca_phase_invalid)) {
		cm_writing_unlock_by_entry(entry);
	}
	state->cm_keyiread_state = cm_keyiread_start(entry);
	if (state->cm_keyiread_state != NULL) {
		while (cm_keyiread_ready(state->cm_keyiread_state) != 0) {
			fd = cm_keyiread_get_fd(state->cm_keyiread_state);
			if (fd != -1) {
				cm_waitfor_readable_fd(fd, -1);
			}
		}
		cm_keyiread_done(state->cm_keyiread_state);
		state->cm_keyiread_state = NULL;
	}
	state->cm_certread_state = cm_certread_start(entry);
	if (state->cm_certread_state != NULL) {
		while (cm_certread_ready(state->cm_certread_state) != 0) {
			fd = cm_certread_get_fd(state->cm_certread_state);
			if (fd != -1) {
				cm_waitfor_readable_fd(fd, -1);
			}
		}
		cm_certread_done(state->cm_certread_state);
		state->cm_certread_state = NULL;
	}
	cm_store_entry_save(entry);
	cm_log(3, "%s('%s') starts in state '%s'\n",
	       entry->cm_busname, entry->cm_nickname,
	       cm_store_state_as_string(entry->cm_state));
	return 0;
}

/* Check if the entry's expiration has crossed an interesting threshold. */
static int
cm_check_expiration_is_noteworthy(struct cm_store_entry *entry,
				  int (*get_ttls)(const time_t **,
						  unsigned int *),
				  time_t *last_check)
{
	unsigned int i, n_ttls;
	time_t now, ttl, previous_ttl;
	const time_t *ttls;

	now = cm_time(NULL);
	/* Do we have validity information? */
	if (entry->cm_cert_not_after == 0) {
		return -1;
	}
	/* Is it at least (some arbitrary minimum) old? */
	if (entry->cm_cert_not_before > (now - CM_DELAY_MONITOR_POLL_MINIMUM)) {
		return -1;
	}
	/* How much time is left? */
	if (entry->cm_cert_not_after < now) {
		ttl = 0;
	} else {
		ttl = entry->cm_cert_not_after - now;
	}
	/* How much time was left, last time we checked? */
	if (entry->cm_cert_not_after < *last_check) {
		previous_ttl = 0;
	} else {
		previous_ttl = entry->cm_cert_not_after - *last_check;
	}
	/* Note that we're checking now. */
	*last_check = now;
	/* Which list of interesting values are we consulting? */
	ttls = NULL;
	n_ttls = 0;
	if (((*get_ttls)(&ttls, &n_ttls) != 0) || (n_ttls == 0)) {
		return -1;
	}
	/* Check for crosses. */
	for (i = 0; i < n_ttls; i++) {
		/* We crossed a threshold. */
		if ((ttl < ttls[i]) && (previous_ttl >= ttls[i])) {
			return 0;
		}
		/* We crossed a threshold... and time is running backwards. */
		if ((ttl >= ttls[i]) && (previous_ttl < ttls[i])) {
			return 0;
		}
	}
	/* The certificate has expired. */
	if (ttl == 0) {
		return 0;
	}
	return -1;
}

/* Check if our policy means that the entry needs a new key. */
static dbus_bool_t
cm_check_rekey_is_expected(struct cm_store_entry *entry)
{
	long long t, now;
	char tstr[25], nowstr[25];
	long i;

	if (entry->cm_key_generated_date != 0) {
		t = prefs_key_end_of_life(entry->cm_key_generated_date);
		if (t >= 0) {
			now = time(NULL);
			if (now >= t) {
				cm_store_timestamp_from_time_for_display(t, tstr);
				cm_store_timestamp_from_time_for_display(now, nowstr);
				cm_log(1, "%s('%s') needs its key replaced "
				       "after %s, and it's %s now.\n",
				       entry->cm_busname, entry->cm_nickname,
				       tstr, nowstr);
				return TRUE;
			}
		}
	}
	i = prefs_max_key_use_count();
	if (i > 0) {
		if (entry->cm_key_issued_count >= i) {
			cm_log(1, "%s('%s') needs its key replaced after %ld "
			       "uses, and it's been used %lld times.\n",
			       entry->cm_busname, entry->cm_nickname,
			       i, (long long) entry->cm_key_issued_count);
			return TRUE;
		}
	}
	return FALSE;
}

int
cm_iterate_entry(struct cm_store_entry *entry, struct cm_store_ca *ca,
		 struct cm_context *context,
		 struct cm_store_ca *(*get_ca_by_index)(struct cm_context *, int),
		 int (*get_n_cas)(struct cm_context *),
		 struct cm_store_entry *(*get_entry_by_index)(struct cm_context *, int),
		 int (*get_n_entries)(struct cm_context *),
		 void (*emit_entry_saved_cert)(struct cm_context *,
					       struct cm_store_entry *),
		 void (*emit_entry_changes)(struct cm_context *,
					    struct cm_store_entry *,
					    struct cm_store_entry *),
		 void *cm_iterate_state,
		 enum cm_time *when, int *delay, int *readfd)
{
	int i, j;
	time_t remaining;
	struct cm_entry_state *state;
	struct cm_store_ca *tmp_ca;
	struct cm_store_entry *old_entry;
	char *serial;
	const char *tmp_ca_name;

	state = cm_iterate_state;
	*readfd = -1;
	*when = cm_time_no_time;
	*delay = 0;

	old_entry = cm_store_entry_dup(entry, entry);
	if (entry->cm_cert_not_after != 0) {
		remaining = entry->cm_cert_not_after - cm_time(NULL);
	} else {
		remaining = -1;
	}

	switch (entry->cm_state) {
	case CM_NEED_KEY_PAIR:
		if (!cm_writing_lock_by_entry(entry)) {
			/* Just hang out in this state while we're messing
			 * around with the outside world for another entry. */
			cm_log(3, "%s('%s') waiting for saving lock\n",
			       entry->cm_busname, entry->cm_nickname);
			*when = cm_time_soon;
			break;
		}
		/* Start a helper. */
		state->cm_keygen_state = cm_keygen_start(entry);
		if (state->cm_keygen_state != NULL) {
			/* Note that we're generating a key. */
			entry->cm_state = CM_GENERATING_KEY_PAIR;
			/* Wait for status update, or poll. */
			*readfd = cm_keygen_get_fd(state->cm_keygen_state);
			if (*readfd == -1) {
				*when = cm_time_soon;
			} else {
				*when = cm_time_no_time;
			}
		} else {
			/* Failed to start generating a key; try again. */
			cm_writing_unlock_by_entry(entry);
			*when = cm_time_soonish;
		}
		break;

	case CM_GENERATING_KEY_PAIR:
		if (cm_keygen_ready(state->cm_keygen_state) == 0) {
			if (!cm_writing_unlock_by_entry(entry)) {
				/* If for some reason we fail to release the
				 * lock that we have, try to release it again
				 * soon. */
				*when = cm_time_soon;
				cm_log(1, "%s('%s') failed to release saving "
				       "lock, probably a bug\n",
				       entry->cm_busname, entry->cm_nickname);
				break;
			}
			if (cm_keygen_saved_keypair(state->cm_keygen_state) == 0) {
				/* Saved key pair; move on. */
				cm_keygen_done(state->cm_keygen_state);
				state->cm_keygen_state = NULL;
				entry->cm_state = CM_HAVE_KEY_PAIR;
				*when = cm_time_now;
			} else
			if (cm_keygen_need_perms(state->cm_keygen_state) == 0) {
				/* Whoops, we need help. */
				cm_keygen_done(state->cm_keygen_state);
				state->cm_keygen_state = NULL;
				entry->cm_state = CM_NEED_KEY_GEN_PERMS;
				*when = cm_time_now;
			} else
			if (cm_keygen_need_token(state->cm_keygen_state) == 0) {
				/* Whoops, we need help. */
				cm_keygen_done(state->cm_keygen_state);
				state->cm_keygen_state = NULL;
				entry->cm_state = CM_NEED_KEY_GEN_TOKEN;
				*when = cm_time_now;
			} else
			if (cm_keygen_need_pin(state->cm_keygen_state) == 0) {
				/* Whoops, we need help. */
				cm_keygen_done(state->cm_keygen_state);
				state->cm_keygen_state = NULL;
				entry->cm_state = CM_NEED_KEY_GEN_PIN;
				*when = cm_time_now;
			} else {
				/* Failed to save key pair; take a breather and
				 * try again. */
				cm_keygen_done(state->cm_keygen_state);
				state->cm_keygen_state = NULL;
				entry->cm_state = CM_NEED_KEY_PAIR;
				*when = cm_time_soonish;
			}
		} else {
			/* Wait for status update, or poll. */
			*readfd = cm_keygen_get_fd(state->cm_keygen_state);
			if (*readfd == -1) {
				*when = cm_time_soon;
			} else {
				*when = cm_time_no_time;
			}
		}
		break;

	case CM_NEED_KEY_GEN_PERMS:
		/* Revisit this later. */
		*when = cm_time_no_time;
		break;

	case CM_NEED_KEY_GEN_TOKEN:
		/* Revisit this later. */
		*when = cm_time_no_time;
		break;

	case CM_NEED_KEY_GEN_PIN:
		/* Revisit this later. */
		*when = cm_time_no_time;
		break;

	case CM_HAVE_KEY_PAIR:
		entry->cm_state = CM_NEED_KEYINFO;
		*when = cm_time_now;
		break;

	case CM_NEED_KEYINFO:
		/* Try to read information about the key. */
		state->cm_keyiread_state = cm_keyiread_start(entry);
		if (state->cm_keyiread_state != NULL) {
			entry->cm_state = CM_READING_KEYINFO;
			/* Note that we're reading information about
			 * the key. */
			*readfd = cm_keyiread_get_fd(state->cm_keyiread_state);
			if (*readfd == -1) {
				*when = cm_time_soon;
			} else {
				*when = cm_time_no_time;
			}
		} else {
			/* Failed to start reading info about the key;
			 * try again soon. */
			*when = cm_time_soonish;
		}
		break;

	case CM_READING_KEYINFO:
		/* If we finished reading info about the key, move on to
		 * generating a CSR. */
		if (cm_keyiread_ready(state->cm_keyiread_state) == 0) {
			if (cm_keyiread_finished_reading(state->cm_keyiread_state) == 0) {
				entry->cm_state = CM_HAVE_KEYINFO;
				*when = cm_time_now;
			} else
			if (cm_keyiread_need_token(state->cm_keyiread_state) == 0) {
				/* If we need the token, just hang on. */
				entry->cm_state = CM_NEED_KEYINFO_READ_TOKEN;
				*when = cm_time_now;
			} else
			if (cm_keyiread_need_pin(state->cm_keyiread_state) == 0) {
				/* If we need the PIN, just hang on. */
				entry->cm_state = CM_NEED_KEYINFO_READ_PIN;
				*when = cm_time_now;
			} else {
				/* Otherwise try to generate a new key pair. */
				entry->cm_state = CM_NEED_KEY_PAIR;
				*when = cm_time_soonish;
			}
			cm_keyiread_done(state->cm_keyiread_state);
			state->cm_keyiread_state = NULL;
		} else {
			/* Wait for status update, or poll. */
			*readfd = cm_keyiread_get_fd(state->cm_keyiread_state);
			if (*readfd == -1) {
				*when = cm_time_soon;
			} else {
				*when = cm_time_no_time;
			}
		}
		break;

	case CM_NEED_KEYINFO_READ_TOKEN:
		/* Revisit this later. */
		*when = cm_time_no_time;
		break;

	case CM_NEED_KEYINFO_READ_PIN:
		/* Revisit this later. */
		*when = cm_time_no_time;
		break;

	case CM_HAVE_KEYINFO:
		entry->cm_state = CM_NEED_CSR;
		*when = cm_time_now;
		break;

	case CM_NEED_CSR:
		state->cm_csrgen_state = cm_csrgen_start(entry);
		if (state->cm_csrgen_state != NULL) {
			/* Note that we're generating a CSR. */
			entry->cm_state = CM_GENERATING_CSR;
			/* Wait for status update, or poll. */
			*readfd = cm_csrgen_get_fd(state->cm_csrgen_state);
			if (*readfd == -1) {
				*when = cm_time_soon;
			} else {
				*when = cm_time_no_time;
			}
		} else {
			/* Failed to start generating a CSR; take a breather
			 * and try again. */
			*when = cm_time_soonish;
		}
		break;

	case CM_GENERATING_CSR:
		if (cm_csrgen_ready(state->cm_csrgen_state) == 0) {
			if (cm_csrgen_save_csr(state->cm_csrgen_state) == 0) {
				/* Saved CSR; move on. */
				cm_csrgen_done(state->cm_csrgen_state);
				state->cm_csrgen_state = NULL;
				entry->cm_state = CM_HAVE_CSR;
				*when = cm_time_now;
			} else
			if (cm_csrgen_need_token(state->cm_csrgen_state) == 0) {
				/* Need a token; wait for it. */
				cm_csrgen_done(state->cm_csrgen_state);
				state->cm_csrgen_state = NULL;
				entry->cm_state = CM_NEED_CSR_GEN_TOKEN;
				*when = cm_time_now;
			} else
			if (cm_csrgen_need_pin(state->cm_csrgen_state) == 0) {
				/* Need a PIN; wait for it. */
				cm_csrgen_done(state->cm_csrgen_state);
				state->cm_csrgen_state = NULL;
				entry->cm_state = CM_NEED_CSR_GEN_PIN;
				*when = cm_time_now;
			} else {
				/* Failed to save CSR; try again. */
				cm_csrgen_done(state->cm_csrgen_state);
				state->cm_csrgen_state = NULL;
				entry->cm_state = CM_NEED_CSR;
				*when = cm_time_soonish;
			}
		} else {
			/* Wait for status update, or poll. */
			*readfd = cm_csrgen_get_fd(state->cm_csrgen_state);
			if (*readfd == -1) {
				*when = cm_time_soon;
			} else {
				*when = cm_time_no_time;
			}
		}
		break;

	case CM_NEED_CSR_GEN_TOKEN:
		*when = cm_time_no_time;
		break;

	case CM_NEED_CSR_GEN_PIN:
		*when = cm_time_no_time;
		break;

	case CM_HAVE_CSR:
		entry->cm_state = CM_NEED_TO_SUBMIT;
		*when = cm_time_now;
		break;

	case CM_NEED_TO_SUBMIT:
		state->cm_submit_state = cm_submit_start(ca, entry);
		if (state->cm_submit_state != NULL) {
			/* Note that we're in the process of submitting the CSR
			 * to a CA. */
			entry->cm_state = CM_SUBMITTING;
			/* Wait for status update, or poll. */
			*readfd = cm_submit_get_fd(state->cm_submit_state);
			if (*readfd == -1) {
				*when = cm_time_soon;
			} else {
				*when = cm_time_no_time;
			}
			/* If we're doing internal-CA, mark this serial number
			 * as used. */
			if (ca != NULL) {
				switch (ca->cm_ca_type) {
				case cm_ca_external:
					break;
				case cm_ca_internal_self:
					serial = ca->cm_ca_internal_serial;
					ca->cm_ca_internal_serial =
						cm_store_increment_serial(ca, serial);
					talloc_free(serial);
					cm_store_ca_save(ca);
				}
			}
			/* In case we're talking to a server over SCEP, make a
			 * note of the nonce, so that we won't re-send an
			 * identical request. */
			if (entry->cm_scep_nonce != NULL) {
				entry->cm_scep_last_nonce = talloc_strdup(entry, entry->cm_scep_nonce);
			}
		} else {
			if (ca == NULL) {
				/* No known CA is associated with this entry. */
				entry->cm_state = CM_NEED_CA;
				*when = cm_time_now;
			} else {
				/* Failed to start submission; take a breather
				 * and try again. */
				*when = cm_time_soonish;
			}
		}
		break;

	case CM_NEED_SCEP_DATA:
		state->cm_scepgen_state = cm_scepgen_start(ca, entry);
		if (state->cm_scepgen_state != NULL) {
			/* Note that we're in the process of generating SCEP
			 * data. */
			entry->cm_state = CM_GENERATING_SCEP_DATA;
			/* Wait for status update, or poll. */
			*readfd = cm_scepgen_get_fd(state->cm_scepgen_state);
			if (*readfd == -1) {
				*when = cm_time_soon;
			} else {
				*when = cm_time_no_time;
			}
		} else {
			/* Failed to start generating data; take a breather and
			 * try again. */
			*when = cm_time_soonish;
		}
		break;

	case CM_GENERATING_SCEP_DATA:
		if (cm_scepgen_ready(state->cm_scepgen_state) == 0) {
			if (cm_scepgen_save_scep(state->cm_scepgen_state) == 0) {
				/* Saved SCEP data; move on. */
				cm_scepgen_done(state->cm_scepgen_state);
				state->cm_scepgen_state = NULL;
				entry->cm_state = CM_HAVE_SCEP_DATA;
				*when = cm_time_now;
			} else
			if (cm_scepgen_need_token(state->cm_scepgen_state) == 0) {
				/* Need a token; wait for it. */
				cm_scepgen_done(state->cm_scepgen_state);
				state->cm_scepgen_state = NULL;
				entry->cm_state = CM_NEED_SCEP_GEN_TOKEN;
				*when = cm_time_now;
			} else
			if (cm_scepgen_need_pin(state->cm_scepgen_state) == 0) {
				/* Need a PIN; wait for it. */
				cm_scepgen_done(state->cm_scepgen_state);
				state->cm_scepgen_state = NULL;
				entry->cm_state = CM_NEED_SCEP_GEN_PIN;
				*when = cm_time_now;
			} else
			if (cm_scepgen_need_encryption_certs(state->cm_scepgen_state) == 0) {
				/* Need the RA's encryption cert; wait for it. */
				cm_scepgen_done(state->cm_scepgen_state);
				state->cm_scepgen_state = NULL;
				entry->cm_state = CM_NEED_SCEP_ENCRYPTION_CERT;
				*when = cm_time_now;
			} else
			if (cm_scepgen_need_different_key_type(state->cm_scepgen_state) == 0) {
				/* Need an RSA key. */
				cm_scepgen_done(state->cm_scepgen_state);
				state->cm_scepgen_state = NULL;
				entry->cm_state = CM_NEED_SCEP_RSA_CLIENT_KEY;
				*when = cm_time_now;
			} else {
				/* Failed to save SCEP data; try again. */
				cm_scepgen_done(state->cm_scepgen_state);
				state->cm_scepgen_state = NULL;
				entry->cm_state = CM_NEED_SCEP_DATA;
				*when = cm_time_soonish;
			}
		} else {
			/* Wait for status update, or poll. */
			*readfd = cm_scepgen_get_fd(state->cm_scepgen_state);
			if (*readfd == -1) {
				*when = cm_time_soon;
			} else {
				*when = cm_time_no_time;
			}
		}
		break;

	case CM_NEED_SCEP_GEN_TOKEN:
		*when = cm_time_no_time;
		break;

	case CM_NEED_SCEP_GEN_PIN:
		*when = cm_time_no_time;
		break;

	case CM_NEED_SCEP_ENCRYPTION_CERT:
		*when = cm_time_no_time;
		break;

	case CM_NEED_SCEP_RSA_CLIENT_KEY:
		*when = cm_time_no_time;
		break;

	case CM_HAVE_SCEP_DATA:
		entry->cm_state = CM_NEED_TO_SUBMIT;
		*when = cm_time_now;
		break;

	case CM_SUBMITTING:
		if (cm_submit_ready(state->cm_submit_state) == 0) {
			entry->cm_submitted = cm_time(NULL);
			if (cm_submit_issued(state->cm_submit_state) == 0) {
				/* We're all done.  Save the certificate to its
				 * real home. */
				cm_submit_clear_ca_cookie(state->cm_submit_state);
				cm_submit_done(state->cm_submit_state);
				state->cm_submit_state = NULL;
				entry->cm_state = CM_NEED_TO_SAVE_CERT;
				*when = cm_time_now;
			} else
			if (cm_submit_rejected(state->cm_submit_state) == 0) {
				/* The request was flat-out rejected. */
				cm_submit_clear_ca_cookie(state->cm_submit_state);
				cm_submit_done(state->cm_submit_state);
				state->cm_submit_state = NULL;
				if (entry->cm_cert != NULL) {
					cm_log(3, "%s('%s') already had a "
					       "certificate, going back to "
					       "monitoring it\n",
					       entry->cm_busname,
					       entry->cm_nickname);
					entry->cm_state = CM_MONITORING;
					*when = cm_time_soonish;
				} else {
					entry->cm_state = CM_NEED_TO_NOTIFY_REJECTION;
					*when = cm_time_now;
				}
			} else
			if (cm_submit_unreachable(state->cm_submit_state) == 0) {
				/* Let's try again later.  The cookie is left
				 * unmodified. */
				*delay = cm_submit_specified_delay(state->cm_submit_state);
				cm_submit_done(state->cm_submit_state);
				state->cm_submit_state = NULL;
				entry->cm_state = CM_CA_UNREACHABLE;
				*when = cm_time_delay;
				if (*delay < 0) {
					*delay = cm_decide_ca_delay(remaining);
				}
			} else
			if (cm_submit_save_ca_cookie(state->cm_submit_state) == 0) {
				/* Saved CA's identifier for our request; give
				 * it the specified time, or a little time, and
				 * then ask for a progress update. */
				cm_log(4, "%s('%s') provided CA "
				       "cookie \"%s\"\n", entry->cm_busname,
				       entry->cm_nickname, entry->cm_ca_cookie);
				*delay = cm_submit_specified_delay(state->cm_submit_state);
				cm_submit_done(state->cm_submit_state);
				state->cm_submit_state = NULL;
				entry->cm_state = CM_CA_WORKING;
				*when = cm_time_delay;
				if (*delay < 0) {
					*delay = cm_decide_ca_delay(remaining);
				}
			} else
			if (cm_submit_unconfigured(state->cm_submit_state) == 0) {
				/* Saved CA's identifier for our request; give
				 * it a little time and then ask. */
				*delay = cm_submit_specified_delay(state->cm_submit_state);
				cm_submit_done(state->cm_submit_state);
				state->cm_submit_state = NULL;
				if (entry->cm_cert != NULL) {
					cm_log(3, "%s('%s') already had a "
					       "certificate, going back to "
					       "monitoring it\n",
					       entry->cm_busname,
					       entry->cm_nickname);
					entry->cm_state = CM_MONITORING;
					*when = cm_time_soonish;
				} else {
					entry->cm_state = CM_CA_UNCONFIGURED;
					*when = cm_time_delay;
					if (*delay < 0) {
						*delay = cm_decide_ca_delay(remaining);
					}
				}
			} else
			if (cm_submit_need_scep_messages(state->cm_submit_state) == 0) {
				/* We need to generate SCEP data. */
				cm_submit_done(state->cm_submit_state);
				state->cm_submit_state = NULL;
				cm_log(3, "%s('%s') goes to a CA over SCEP, "
				       "need to generate SCEP data.\n",
				       entry->cm_busname, entry->cm_nickname);
				entry->cm_state = CM_NEED_SCEP_DATA;
				*when = cm_time_now;
			} else
			if (cm_submit_need_rekey(state->cm_submit_state) == 0) {
				/* We need to generate a new key pair. */
				cm_submit_done(state->cm_submit_state);
				state->cm_submit_state = NULL;
				cm_log(3, "%s('%s') needs to be rekeyed.\n",
				       entry->cm_busname, entry->cm_nickname);
				entry->cm_state = CM_NEED_KEY_PAIR;
				*when = cm_time_soonish;
			} else {
				/* Don't know what's going on. HELP! */
				cm_log(1,
				       "Unable to determine course of action "
				       "for %s('%s').\n",
				       entry->cm_busname,
				       entry->cm_nickname);
				cm_submit_done(state->cm_submit_state);
				state->cm_submit_state = NULL;
				entry->cm_state = CM_NEED_GUIDANCE;
				*when = cm_time_now;
			}
		} else {
			/* Wait for status update, or poll. */
			*readfd = cm_submit_get_fd(state->cm_submit_state);
			if (*readfd == -1) {
				*when = cm_time_soon;
			} else {
				*when = cm_time_no_time;
			}
		}
		break;

	case CM_NEED_TO_SAVE_CERT:
		if (!cm_writing_lock_by_entry(entry)) {
			/* Just hang out in this state while we're messing
			 * around with the outside world for another entry. */
			cm_log(3, "%s('%s') waiting for saving lock\n",
			       entry->cm_busname, entry->cm_nickname);
			*when = cm_time_soon;
			break;
		}
		state->cm_hook_state = cm_hook_start_presave(entry,
							     context,
							     get_ca_by_index,
							     get_n_cas,
							     get_entry_by_index,
							     get_n_entries);
		if (state->cm_hook_state != NULL) {
			/* Note that we're doing the pre-save. */
			entry->cm_state = CM_PRE_SAVE_CERT;
			/* Wait for status update, or poll. */
			*readfd = cm_hook_get_fd(state->cm_hook_state);
			if (*readfd == -1) {
				*when = cm_time_soon;
			} else {
				*when = cm_time_no_time;
			}
		} else {
			/* Failed to start the pre-save, or nothing to do; skip
			 * it. */
			entry->cm_state = CM_START_SAVING_CERT;
			*when = cm_time_now;
		}
		break;

	case CM_PRE_SAVE_CERT:
		if (cm_hook_ready(state->cm_hook_state) == 0) {
			cm_hook_done(state->cm_hook_state);
			state->cm_hook_state = NULL;
			entry->cm_state = CM_START_SAVING_CERT;
			*when = cm_time_now;
		} else {
			/* Wait for status update, or poll. */
			*readfd = cm_hook_get_fd(state->cm_hook_state);
			if (*readfd == -1) {
				*when = cm_time_soon;
			} else {
				*when = cm_time_no_time;
			}
		}
		break;

	case CM_START_SAVING_CERT:
		state->cm_certsave_state = cm_certsave_start(entry);
		if (state->cm_certsave_state != NULL) {
			/* Note that we're saving the cert. */
			entry->cm_state = CM_SAVING_CERT;
			/* Wait for status update, or poll. */
			*readfd = cm_certsave_get_fd(state->cm_certsave_state);
			if (*readfd == -1) {
				*when = cm_time_soon;
			} else {
				*when = cm_time_no_time;
			}
		} else {
			/* Failed to start saving the certificate; try again. */
			*when = cm_time_soonish;
		}
		break;

	case CM_SAVING_CERT:
		if (cm_certsave_ready(state->cm_certsave_state) == 0) {
			if (cm_certsave_saved(state->cm_certsave_state) == 0) {
				/* Saved certificate. */
				cm_certsave_done(state->cm_certsave_state);
				state->cm_certsave_state = NULL;
				entry->cm_state = CM_SAVED_CERT;
				*when = cm_time_now;
			} else
			if (cm_certsave_permissions_error(state->cm_certsave_state) == 0) {
				/* Whoops, we need help. */
				cm_certsave_done(state->cm_certsave_state);
				state->cm_certsave_state = NULL;
				entry->cm_state = CM_NEED_CERTSAVE_PERMS;
				*when = cm_time_now;
			} else
			if (cm_certsave_token_error(state->cm_certsave_state) == 0) {
				/* Whoops, we need help. */
				cm_certsave_done(state->cm_certsave_state);
				state->cm_certsave_state = NULL;
				entry->cm_state = CM_NEED_CERTSAVE_TOKEN;
				*when = cm_time_now;
			} else
			if (cm_certsave_pin_error(state->cm_certsave_state) == 0) {
				/* Whoops, we need help. */
				cm_certsave_done(state->cm_certsave_state);
				state->cm_certsave_state = NULL;
				entry->cm_state = CM_NEED_CERTSAVE_PIN;
				*when = cm_time_now;
			} else {
				/* Failed to save cert; make a note and try
				 * again in a bit. */
				cm_certsave_done(state->cm_certsave_state);
				state->cm_certsave_state = NULL;
				entry->cm_state = CM_NEED_TO_NOTIFY_ISSUED_SAVE_FAILED;
				*when = cm_time_soonish;
			}
		} else {
			/* Wait for status update, or poll. */
			*readfd = cm_certsave_get_fd(state->cm_certsave_state);
			if (*readfd == -1) {
				*when = cm_time_soon;
			} else {
				*when = cm_time_no_time;
			}
		}
		break;

	case CM_NEED_CERTSAVE_PERMS:
		/* Revisit this later. */
		*when = cm_time_no_time;
		break;

	case CM_NEED_CERTSAVE_TOKEN:
		/* Revisit this later. */
		*when = cm_time_no_time;
		break;

	case CM_NEED_CERTSAVE_PIN:
		/* Revisit this later. */
		*when = cm_time_no_time;
		break;

	case CM_NEED_TO_READ_CERT:
		/* We should already have the lock here.  In cases where we're
		 * resuming things at startup, try to acquire it if we don't
		 * have it. */
		if (!cm_writing_has_lock(entry, cm_ca_phase_invalid) && !cm_writing_lock_by_entry(entry)) {
			/* Just hang out in this state while we're messing
			 * around with the outside world for another entry. */
			cm_log(3, "%s('%s') waiting for saving lock\n",
			       entry->cm_busname, entry->cm_nickname);
			*when = cm_time_soon;
			break;
		}
		state->cm_certread_state = cm_certread_start(entry);
		if (state->cm_certread_state != NULL) {
			/* Note that we're reading the cert. */
			entry->cm_state = CM_READING_CERT;
			/* Wait for status update, or poll. */
			*readfd = cm_certread_get_fd(state->cm_certread_state);
			if (*readfd == -1) {
				*when = cm_time_soon;
			} else {
				*when = cm_time_no_time;
			}
		} else {
			/* Failed to start re-reading the certificate; try
			 * again. */
			*when = cm_time_soonish;
		}
		break;

	case CM_READING_CERT:
		if (cm_certread_ready(state->cm_certread_state) == 0) {
			/* Finished reloading certificate. */
			cm_certread_done(state->cm_certread_state);
			state->cm_certread_state = NULL;
			if (emit_entry_saved_cert != NULL) {
				(*emit_entry_saved_cert)(context, entry);
			}
			/* Start the post-save hoook, if there is one. */
			state->cm_hook_state = cm_hook_start_postsave(entry,
								      context,
								      get_ca_by_index,
								      get_n_cas,
								      get_entry_by_index,
								      get_n_entries);
			if (state->cm_hook_state != NULL) {
				/* Note that we're doing the post-save. */
				entry->cm_state = CM_POST_SAVED_CERT;
				/* Wait for status update, or poll. */
				*readfd = cm_hook_get_fd(state->cm_hook_state);
				if (*readfd == -1) {
					*when = cm_time_soon;
				} else {
					*when = cm_time_no_time;
				}
			} else {
				/* Failed to start the post-save, or nothing to do;
				 * skip it. */
				entry->cm_state = CM_NEED_TO_NOTIFY_ISSUED_SAVED;
				*when = cm_time_now;
			}
		} else {
			/* Wait for status update, or poll. */
			*readfd = cm_certread_get_fd(state->cm_certread_state);
			if (*readfd == -1) {
				*when = cm_time_soon;
			} else {
				*when = cm_time_no_time;
			}
		}
		break;

	case CM_POST_SAVED_CERT:
		if (cm_hook_ready(state->cm_hook_state) == 0) {
			cm_hook_done(state->cm_hook_state);
			state->cm_hook_state = NULL;
			entry->cm_state = CM_NEED_TO_NOTIFY_ISSUED_SAVED;
			*when = cm_time_now;
		} else {
			/* Wait for status update, or poll. */
			*readfd = cm_hook_get_fd(state->cm_hook_state);
			if (*readfd == -1) {
				*when = cm_time_soon;
			} else {
				*when = cm_time_no_time;
			}
		}
		break;

	case CM_SAVED_CERT:
		entry->cm_state = CM_NEED_TO_SAVE_CA_CERTS;
		*when = cm_time_now;
		break;

	case CM_CA_REJECTED:
		*when = cm_time_no_time;
		break;

	case CM_CA_WORKING:
		entry->cm_state = CM_NEED_TO_SUBMIT;
		*when = cm_time_now;
		break;

	case CM_CA_UNREACHABLE:
		entry->cm_state = CM_NEED_TO_SUBMIT;
		*when = cm_time_soonish;
		break;

	case CM_CA_UNCONFIGURED:
		*when = cm_time_no_time;
		break;

	case CM_NEED_GUIDANCE:
		*when = cm_time_no_time;
		break;

	case CM_NEED_CA:
		*when = cm_time_no_time;
		break;

	case CM_MONITORING:
		if (entry->cm_monitor &&
		    (cm_check_expiration_is_noteworthy(entry,
						       &cm_prefs_notify_ttls,
						       &entry->cm_last_need_notify_check) == 0)) {
			/* Kick off a notification. */
			entry->cm_state = CM_NEED_TO_NOTIFY_VALIDITY;
			*when = cm_time_now;
		} else
		if (entry->cm_autorenew &&
		    (cm_check_expiration_is_noteworthy(entry,
						       &cm_prefs_enroll_ttls,
						       &entry->cm_last_need_enroll_check) == 0)) {
			/* Kick off an enrollment attempt.  We need to go all
			 * the way back to generating the CSR because the user
			 * may have asked us to request with parameters that
			 * have changed since we last generated a CSR. */
			entry->cm_state = cm_check_rekey_is_expected(entry) ?
					  CM_NEED_KEY_PAIR : CM_NEED_CSR;
			*when = cm_time_now;
		} else {
			/* Nothing to do here.  Check again at an appropriate time. */
			*when = cm_time_delay;
			*delay = cm_decide_monitor_delay(remaining,
							 &cm_prefs_notify_ttls,
							 &cm_prefs_enroll_ttls);
		}
		break;

	case CM_NEED_TO_NOTIFY_VALIDITY:
		state->cm_notify_state = cm_notify_start(entry,
							 cm_notify_event_validity_ending);
		if (state->cm_notify_state != NULL) {
			entry->cm_state = CM_NOTIFYING_VALIDITY;
			/* Wait for status update, or poll. */
			*readfd = cm_notify_get_fd(state->cm_notify_state);
			if (*readfd == -1) {
				*when = cm_time_soon;
			} else {
				*when = cm_time_no_time;
			}
		} else {
			/* Failed to start notifying; try again. */
			*when = cm_time_soonish;
		}
		break;

	case CM_NOTIFYING_VALIDITY:
		if (cm_notify_ready(state->cm_notify_state) == 0) {
			cm_notify_done(state->cm_notify_state);
			state->cm_notify_state = NULL;
			if (entry->cm_autorenew &&
			    (cm_check_expiration_is_noteworthy(entry,
							       &cm_prefs_enroll_ttls,
							       &entry->cm_last_need_enroll_check) == 0)) {
				/* Kick off an enrollment attempt.  We need to go all
				 * the way back to generating the CSR because the user
				 * may have asked us to request with parameters that
				 * have changed since we last generated a CSR. */
				entry->cm_state = cm_check_rekey_is_expected(entry) ?
						  CM_NEED_KEY_PAIR : CM_NEED_CSR;
				*when = cm_time_now;
			} else {
				entry->cm_state = CM_MONITORING;
				*when = cm_time_delay;
				*delay = cm_decide_monitor_delay(remaining,
								 &cm_prefs_notify_ttls,
								 &cm_prefs_enroll_ttls);
			}
		} else {
			/* Wait for status update, or poll. */
			*readfd = cm_notify_get_fd(state->cm_notify_state);
			if (*readfd == -1) {
				*when = cm_time_soon;
			} else {
				*when = cm_time_no_time;
			}
		}
		break;

	case CM_NEED_TO_NOTIFY_REJECTION:
		state->cm_notify_state = cm_notify_start(entry,
							 cm_notify_event_rejected);
		if (state->cm_notify_state != NULL) {
			entry->cm_state = CM_NOTIFYING_REJECTION;
			/* Wait for status update, or poll. */
			*readfd = cm_notify_get_fd(state->cm_notify_state);
			if (*readfd == -1) {
				*when = cm_time_soon;
			} else {
				*when = cm_time_no_time;
			}
		} else {
			/* Failed to start notifying; try again. */
			*when = cm_time_soonish;
		}
		break;

	case CM_NOTIFYING_REJECTION:
		if (cm_notify_ready(state->cm_notify_state) == 0) {
			cm_notify_done(state->cm_notify_state);
			state->cm_notify_state = NULL;
			entry->cm_state = CM_CA_REJECTED;
			*when = cm_time_soon;
		} else {
			/* Wait for status update, or poll. */
			*readfd = cm_notify_get_fd(state->cm_notify_state);
			if (*readfd == -1) {
				*when = cm_time_soon;
			} else {
				*when = cm_time_no_time;
			}
		}
		break;

	case CM_NEED_TO_NOTIFY_ISSUED_SAVE_FAILED:
		/* We should already have the lock here.  In cases where we're
		 * resuming things at startup, try to acquire it if we don't
		 * have it. */
		if (!cm_writing_has_lock(entry, cm_ca_phase_invalid) && !cm_writing_lock_by_entry(entry)) {
			/* Just hang out in this state while we're messing
			 * around with the outside world for another entry. */
			cm_log(3, "%s('%s') waiting for saving lock\n",
			       entry->cm_busname, entry->cm_nickname);
			*when = cm_time_soon;
			break;
		}
		if (!cm_writing_unlock_by_entry(entry)) {
			/* If for some reason we fail to release the lock that
			 * we have, try to release it again soon. */
			*when = cm_time_soon;
			cm_log(1, "%s('%s') failed to release saving "
			       "lock, probably a bug\n",
			       entry->cm_busname, entry->cm_nickname);
			break;
		}
		state->cm_notify_state = cm_notify_start(entry,
							 cm_notify_event_issued_not_saved);
		if (state->cm_notify_state != NULL) {
			entry->cm_state = CM_NOTIFYING_ISSUED_SAVE_FAILED;
			/* Wait for status update, or poll. */
			*readfd = cm_notify_get_fd(state->cm_notify_state);
			if (*readfd == -1) {
				*when = cm_time_soon;
			} else {
				*when = cm_time_no_time;
			}
		} else {
			/* Failed to start notifying; try again. */
			*when = cm_time_soonish;
		}
		break;

	case CM_NOTIFYING_ISSUED_SAVE_FAILED:
		if (cm_notify_ready(state->cm_notify_state) == 0) {
			cm_notify_done(state->cm_notify_state);
			state->cm_notify_state = NULL;
			entry->cm_state = CM_START_SAVING_CERT;
			*when = cm_time_soonish;
		} else {
			/* Wait for status update, or poll. */
			*readfd = cm_notify_get_fd(state->cm_notify_state);
			if (*readfd == -1) {
				*when = cm_time_soon;
			} else {
				*when = cm_time_no_time;
			}
		}
		break;

	case CM_NEED_TO_SAVE_CA_CERTS:
		/* We should already have the lock here.  In cases where we're
		 * resuming things at startup, try to acquire it if we don't
		 * have it. */
		if (!cm_writing_has_lock(entry, cm_ca_phase_invalid) && !cm_writing_lock_by_entry(entry)) {
			/* Just hang out in this state while we're messing
			 * around with the outside world for another entry. */
			cm_log(3, "%s('%s') waiting for saving lock\n",
			       entry->cm_busname, entry->cm_nickname);
			*when = cm_time_soon;
			break;
		}
		entry->cm_state = CM_START_SAVING_CA_CERTS;
		*when = cm_time_now;
		break;

	case CM_START_SAVING_CA_CERTS:
		state->cm_casave_state = cm_casave_start(entry, NULL, context,
							 get_ca_by_index,
							 get_n_cas,
							 get_entry_by_index,
							 get_n_entries);
		if (state->cm_casave_state != NULL) {
			entry->cm_state = CM_SAVING_CA_CERTS;
			/* Wait for status update, or poll. */
			*readfd = cm_casave_get_fd(state->cm_casave_state);
			if (*readfd == -1) {
				*when = cm_time_soon;
			} else {
				*when = cm_time_no_time;
			}
		} else {
			/* Failed to start saving CA certs; try again. */
			*when = cm_time_soonish;
		}
		break;

	case CM_SAVING_CA_CERTS:
		if (cm_casave_ready(state->cm_casave_state) == 0) {
			if (cm_casave_saved(state->cm_casave_state) == 0) {
				/* Saved CA certificates, no go re-read the
				 * issued certificate. */
				cm_casave_done(state->cm_casave_state);
				state->cm_casave_state = NULL;
				entry->cm_state = CM_NEED_TO_READ_CERT;
				*when = cm_time_now;
			} else
			if (cm_casave_permissions_error(state->cm_casave_state) == 0) {
				/* Whoops, we need help. */
				cm_casave_done(state->cm_casave_state);
				state->cm_casave_state = NULL;
				entry->cm_state = CM_NEED_CA_CERT_SAVE_PERMS;
				*when = cm_time_now;
			} else {
				/* Failed to save CA certs. */
				cm_casave_done(state->cm_casave_state);
				state->cm_casave_state = NULL;
				entry->cm_state = CM_NEED_TO_NOTIFY_ISSUED_CA_SAVE_FAILED;
				*when = cm_time_soonish;
			}
		} else {
			/* Wait for status update, or poll. */
			*readfd = cm_casave_get_fd(state->cm_casave_state);
			if (*readfd == -1) {
				*when = cm_time_soon;
			} else {
				*when = cm_time_no_time;
			}
		}
		break;

	case CM_NEED_CA_CERT_SAVE_PERMS:
		/* Revisit this later. */
		*when = cm_time_no_time;
		break;

	case CM_NEED_TO_NOTIFY_ISSUED_SAVED:
		/* We should already have the lock here.  In cases where we're
		 * resuming things at startup, try to acquire it if we don't
		 * have it. */
		if (!cm_writing_has_lock(entry, cm_ca_phase_invalid) && !cm_writing_lock_by_entry(entry)) {
			/* Just hang out in this state while we're messing
			 * around with the outside world for another entry. */
			cm_log(3, "%s('%s') waiting for saving lock\n",
			       entry->cm_busname, entry->cm_nickname);
			*when = cm_time_soon;
			break;
		}
		if (!cm_writing_unlock_by_entry(entry)) {
			/* If for some reason we fail to release the lock that
			 * we have, try to release it again soon. */
			*when = cm_time_soon;
			cm_log(1, "%s('%s') failed to release saving "
			       "lock, probably a bug\n",
			       entry->cm_busname, entry->cm_nickname);
			break;
		}
		state->cm_notify_state = cm_notify_start(entry,
							 cm_notify_event_issued_and_saved);
		if (state->cm_notify_state != NULL) {
			entry->cm_state = CM_NOTIFYING_ISSUED_SAVED;
			/* Wait for status update, or poll. */
			*readfd = cm_notify_get_fd(state->cm_notify_state);
			if (*readfd == -1) {
				*when = cm_time_soon;
			} else {
				*when = cm_time_no_time;
			}
		} else {
			/* Failed to start notifying; try again. */
			*when = cm_time_soonish;
		}
		break;

	case CM_NOTIFYING_ISSUED_SAVED:
		if (cm_notify_ready(state->cm_notify_state) == 0) {
			cm_notify_done(state->cm_notify_state);
			state->cm_notify_state = NULL;
			entry->cm_state = CM_MONITORING;
			*when = cm_time_soon;
		} else {
			/* Wait for status update, or poll. */
			*readfd = cm_notify_get_fd(state->cm_notify_state);
			if (*readfd == -1) {
				*when = cm_time_soon;
			} else {
				*when = cm_time_no_time;
			}
		}
		break;

	case CM_NEED_TO_NOTIFY_ISSUED_CA_SAVE_FAILED:
		/* We should already have the lock here.  In cases where we're
		 * resuming things at startup, try to acquire it if we don't
		 * have it. */
		if (!cm_writing_has_lock(entry, cm_ca_phase_invalid) && !cm_writing_lock_by_entry(entry)) {
			/* Just hang out in this state while we're messing
			 * around with the outside world for another entry. */
			cm_log(3, "%s('%s') waiting for saving lock\n",
			       entry->cm_busname, entry->cm_nickname);
			*when = cm_time_soon;
			break;
		}
		if (!cm_writing_unlock_by_entry(entry)) {
			/* If for some reason we fail to release the lock that
			 * we have, try to release it again soon. */
			*when = cm_time_soon;
			cm_log(1, "%s('%s') failed to release saving "
			       "lock, probably a bug\n",
			       entry->cm_busname, entry->cm_nickname);
			break;
		}
		state->cm_notify_state = cm_notify_start(entry,
							 cm_notify_event_issued_ca_not_saved);
		if (state->cm_notify_state != NULL) {
			entry->cm_state = CM_NOTIFYING_ISSUED_CA_SAVE_FAILED;
			/* Wait for status update, or poll. */
			*readfd = cm_notify_get_fd(state->cm_notify_state);
			if (*readfd == -1) {
				*when = cm_time_soon;
			} else {
				*when = cm_time_no_time;
			}
		} else {
			/* Failed to start notifying; try again. */
			*when = cm_time_soonish;
		}
		break;

	case CM_NOTIFYING_ISSUED_CA_SAVE_FAILED:
		if (cm_notify_ready(state->cm_notify_state) == 0) {
			cm_notify_done(state->cm_notify_state);
			state->cm_notify_state = NULL;
			entry->cm_state = CM_NEED_TO_SAVE_CA_CERTS;
			*when = cm_time_soonish;
		} else {
			/* Wait for status update, or poll. */
			*readfd = cm_notify_get_fd(state->cm_notify_state);
			if (*readfd == -1) {
				*when = cm_time_soon;
			} else {
				*when = cm_time_no_time;
			}
		}
		break;

	case CM_NEED_TO_SAVE_ONLY_CA_CERTS:
		if (!cm_writing_has_lock(entry, cm_ca_phase_invalid) && !cm_writing_lock_by_entry(entry)) {
			/* Just hang out in this state while we're messing
			 * around with the outside world for another entry. */
			cm_log(3, "%s('%s') waiting for saving lock\n",
			       entry->cm_busname, entry->cm_nickname);
			*when = cm_time_soon;
			break;
		}
		entry->cm_state = CM_START_SAVING_ONLY_CA_CERTS;
		*when = cm_time_now;
		break;

	case CM_START_SAVING_ONLY_CA_CERTS:
		state->cm_casave_state = cm_casave_start(entry, NULL, context,
							 get_ca_by_index,
							 get_n_cas,
							 get_entry_by_index,
							 get_n_entries);
		if (state->cm_casave_state != NULL) {
			entry->cm_state = CM_SAVING_ONLY_CA_CERTS;
			/* Wait for status update, or poll. */
			*readfd = cm_casave_get_fd(state->cm_casave_state);
			if (*readfd == -1) {
				*when = cm_time_soon;
			} else {
				*when = cm_time_no_time;
			}
		} else {
			/* Failed to start saving CA certs; try again. */
			*when = cm_time_soonish;
		}
		break;

	case CM_SAVING_ONLY_CA_CERTS:
		if (cm_casave_ready(state->cm_casave_state) == 0) {
			if (cm_casave_saved(state->cm_casave_state) == 0) {
				if (!cm_writing_unlock_by_entry(entry)) {
					/* If for some reason we fail to release the lock that
					 * we have, try to release it again soon. */
					*when = cm_time_soon;
					cm_log(1, "%s('%s') failed to release saving "
					       "lock, probably a bug\n",
					       entry->cm_busname, entry->cm_nickname);
					break;
				}
				/* Saved certificates. */
				cm_casave_done(state->cm_casave_state);
				state->cm_casave_state = NULL;
				entry->cm_state = CM_MONITORING;
				*when = cm_time_now;
			} else
			if (cm_casave_permissions_error(state->cm_casave_state) == 0) {
				if (!cm_writing_unlock_by_entry(entry)) {
					/* If for some reason we fail to release the lock that
					 * we have, try to release it again soon. */
					*when = cm_time_soon;
					cm_log(1, "%s('%s') failed to release saving "
					       "lock, probably a bug\n",
					       entry->cm_busname, entry->cm_nickname);
					break;
				}
				/* Whoops, we need help. */
				cm_casave_done(state->cm_casave_state);
				state->cm_casave_state = NULL;
				entry->cm_state = CM_NEED_ONLY_CA_CERT_SAVE_PERMS;
				*when = cm_time_now;
			} else {
				/* Failed to save certs. */
				cm_casave_done(state->cm_casave_state);
				state->cm_casave_state = NULL;
				entry->cm_state = CM_NEED_TO_NOTIFY_ONLY_CA_SAVE_FAILED;
				*when = cm_time_soonish;
			}
		} else {
			/* Wait for status update, or poll. */
			*readfd = cm_casave_get_fd(state->cm_casave_state);
			if (*readfd == -1) {
				*when = cm_time_soon;
			} else {
				*when = cm_time_no_time;
			}
		}
		break;

	case CM_NEED_ONLY_CA_CERT_SAVE_PERMS:
		/* Revisit this later. */
		*when = cm_time_no_time;
		break;

	case CM_NEED_TO_NOTIFY_ONLY_CA_SAVE_FAILED:
		/* We should already have the lock here.  In cases where we're
		 * resuming things at startup, try to acquire it if we don't
		 * have it. */
		if (!cm_writing_has_lock(entry, cm_ca_phase_invalid) && !cm_writing_lock_by_entry(entry)) {
			/* Just hang out in this state while we're messing
			 * around with the outside world for another entry. */
			cm_log(3, "%s('%s') waiting for saving lock\n",
			       entry->cm_busname, entry->cm_nickname);
			*when = cm_time_soon;
			break;
		}
		if (!cm_writing_unlock_by_entry(entry)) {
			/* If for some reason we fail to release the lock that
			 * we have, try to release it again soon. */
			*when = cm_time_soon;
			cm_log(1, "%s('%s') failed to release saving "
			       "lock, probably a bug\n",
			       entry->cm_busname, entry->cm_nickname);
			break;
		}
		state->cm_notify_state = cm_notify_start(entry,
							 cm_notify_event_ca_not_saved);
		if (state->cm_notify_state != NULL) {
			entry->cm_state = CM_NOTIFYING_ONLY_CA_SAVE_FAILED;
			/* Wait for status update, or poll. */
			*readfd = cm_notify_get_fd(state->cm_notify_state);
			if (*readfd == -1) {
				*when = cm_time_soon;
			} else {
				*when = cm_time_no_time;
			}
		} else {
			/* Failed to start notifying; try again. */
			*when = cm_time_soonish;
		}
		break;

	case CM_NOTIFYING_ONLY_CA_SAVE_FAILED:
		if (cm_notify_ready(state->cm_notify_state) == 0) {
			cm_notify_done(state->cm_notify_state);
			state->cm_notify_state = NULL;
			entry->cm_state = CM_MONITORING;
			*when = cm_time_soonish;
		} else {
			/* Wait for status update, or poll. */
			*readfd = cm_notify_get_fd(state->cm_notify_state);
			if (*readfd == -1) {
				*when = cm_time_soon;
			} else {
				*when = cm_time_no_time;
			}
		}
		break;

	case CM_NEWLY_ADDED:
		/* Take the lock here because the database is opened read-write
		 * in case we need to set a password on it. */
		if (!cm_writing_lock_by_entry(entry)) {
			/* Just hang out in this state while we're messing
			 * around with the outside world for another entry. */
			cm_log(3, "%s('%s') waiting for reading lock\n",
			       entry->cm_busname, entry->cm_nickname);
			*when = cm_time_soon;
			break;
		}
		/* We need to do some recon, and then decide what we need to
		 * do to make things the way the user has specified that they
		 * should be. */
		if (entry->cm_key_storage_type != cm_key_storage_none) {
			entry->cm_state = CM_NEWLY_ADDED_START_READING_KEYINFO;
			*when = cm_time_now;
		} else {
			entry->cm_state = CM_NEWLY_ADDED_START_READING_CERT;
			*when = cm_time_now;
		}
		break;

	case CM_NEWLY_ADDED_START_READING_KEYINFO:
		/* Try to read information about the key. */
		state->cm_keyiread_state = cm_keyiread_start(entry);
		if (state->cm_keyiread_state != NULL) {
			entry->cm_state = CM_NEWLY_ADDED_READING_KEYINFO;
			/* Note that we're reading information about
			 * the key. */
			*readfd = cm_keyiread_get_fd(state->cm_keyiread_state);
			if (*readfd == -1) {
				*when = cm_time_soon;
			} else {
				*when = cm_time_no_time;
			}
		} else {
			/* Failed to start reading info about the key;
			 * try again soon. */
			*when = cm_time_soonish;
		}
		break;

	case CM_NEWLY_ADDED_READING_KEYINFO:
		/* If we finished reading info about the key, move on to try
		 * and read the certificate. */
		if (cm_keyiread_ready(state->cm_keyiread_state) == 0) {
			if (cm_keyiread_finished_reading(state->cm_keyiread_state) == 0) {
				entry->cm_state = CM_NEWLY_ADDED_START_READING_CERT;
				*when = cm_time_now;
			} else
			if (cm_keyiread_need_token(state->cm_keyiread_state) == 0) {
				if (!cm_writing_unlock_by_entry(entry)) {
					/* If for some reason we fail to
					 * release the lock that we have, try
					 * to release it again soon. */
					*when = cm_time_soon;
					cm_log(1, "%s('%s') failed to release "
					       "reading lock, probably a bug\n",
					       entry->cm_busname,
					       entry->cm_nickname);
					break;
				}
				/* If we need the token, just hang on. */
				entry->cm_state = CM_NEWLY_ADDED_NEED_KEYINFO_READ_TOKEN;
				*when = cm_time_now;
			} else
			if (cm_keyiread_need_pin(state->cm_keyiread_state) == 0) {
				if (!cm_writing_unlock_by_entry(entry)) {
					/* If for some reason we fail to
					 * release the lock that we have, try
					 * to release it again soon. */
					*when = cm_time_soon;
					cm_log(1, "%s('%s') failed to release "
					       "reading lock, probably a bug\n",
					       entry->cm_busname,
					       entry->cm_nickname);
					break;
				}
				/* If we need the PIN, just hang on. */
				entry->cm_state = CM_NEWLY_ADDED_NEED_KEYINFO_READ_PIN;
				*when = cm_time_now;
			} else {
				/* Otherwise try to move on. */
				entry->cm_state = CM_NEWLY_ADDED_START_READING_CERT;
				*when = cm_time_now;
			}
			cm_keyiread_done(state->cm_keyiread_state);
			state->cm_keyiread_state = NULL;
		} else {
			/* Wait for status update, or poll. */
			*readfd = cm_keyiread_get_fd(state->cm_keyiread_state);
			if (*readfd == -1) {
				*when = cm_time_soon;
			} else {
				*when = cm_time_no_time;
			}
		}
		break;

	case CM_NEWLY_ADDED_NEED_KEYINFO_READ_TOKEN:
		/* Revisit this later. */
		*when = cm_time_no_time;
		break;

	case CM_NEWLY_ADDED_NEED_KEYINFO_READ_PIN:
		/* Revisit this later. */
		*when = cm_time_no_time;
		break;

	case CM_NEWLY_ADDED_START_READING_CERT:
		/* Try to read the certificate. */
		state->cm_certread_state = cm_certread_start(entry);
		if (state->cm_certread_state != NULL) {
			entry->cm_state = CM_NEWLY_ADDED_READING_CERT;
			/* Note that we're reading information about
			 * the certificate. */
			*readfd = cm_certread_get_fd(state->cm_certread_state);
			if (*readfd == -1) {
				*when = cm_time_soon;
			} else {
				*when = cm_time_no_time;
			}
		} else {
			/* Failed to start reading info about the certificate;
			 * try again soon. */
			*when = cm_time_soonish;
		}
		break;

	case CM_NEWLY_ADDED_READING_CERT:
		/* If we finished reading info about the cert, move on to try
		 * to figure out what we should do next. */
		if (cm_certread_ready(state->cm_certread_state) == 0) {
			cm_certread_done(state->cm_certread_state);
			state->cm_certread_state = NULL;
			entry->cm_state = CM_NEWLY_ADDED_DECIDING;
			*when = cm_time_now;
		} else {
			/* Wait for status update, or poll. */
			*readfd = cm_certread_get_fd(state->cm_certread_state);
			if (*readfd == -1) {
				*when = cm_time_soon;
			} else {
				*when = cm_time_no_time;
			}
		}
		break;

	case CM_NEWLY_ADDED_DECIDING:
		if (!cm_writing_unlock_by_entry(entry)) {
			/* If for some reason we fail to release the lock that
			 * we have, try to release it again soon. */
			*when = cm_time_soon;
			cm_log(1, "%s('%s') failed to release reading lock, "
			       "probably a bug\n",
			       entry->cm_busname, entry->cm_nickname);
			break;
		}
		/* Decide what to do next.  Assign a CA if it doesn't have one
		 * assigned to it already. */
		if ((entry->cm_ca_nickname == NULL) &&
		    (entry->cm_cert_issuer != NULL)) {
			/* Walk the list of known names of known CAs and try to
			 * match one with the issuer of the certificate we
			 * already have. */
			for (i = 0; i < (*get_n_cas)(context); i++) {
				tmp_ca = (*get_ca_by_index)(context, i);
				for (j = 0;
				     (tmp_ca->cm_ca_known_issuer_names != NULL) &&
				     (tmp_ca->cm_ca_known_issuer_names[j] != NULL);
				     j++) {
					if (strcmp(tmp_ca->cm_ca_known_issuer_names[j],
						   entry->cm_cert_issuer) == 0) {
						entry->cm_ca_nickname = talloc_strdup(entry, tmp_ca->cm_nickname);
					}
				}
			}
		}
		/* No match -> assign the default. */
		if (entry->cm_ca_nickname == NULL) {
			for (i = 0; i < (*get_n_cas)(context); i++) {
				tmp_ca = (*get_ca_by_index)(context, i);
				if (tmp_ca->cm_ca_is_default) {
					entry->cm_ca_nickname = talloc_strdup(entry, tmp_ca->cm_nickname);
				}
			}
		}
		/* No default in our data store -> use the config file's. */
		if (entry->cm_ca_nickname == NULL) {
			tmp_ca_name = cm_prefs_default_ca();
			if (tmp_ca_name != NULL) {
				entry->cm_ca_nickname = talloc_strdup(entry,
								      tmp_ca_name);
			}
		}
		/* If we have a certificate in the expected location, we go
		 * straight to monitoring it.  If we didn't get any explicit
		 * requests for names, SAN, KU and EKU values, then try to pull
		 * them from the certificate, too. */
		if (entry->cm_cert != NULL) {
			cm_store_set_if_not_set_s(entry,
						  &entry->cm_template_subject_der,
						  entry->cm_cert_subject_der);
			cm_store_set_if_not_set_s(entry,
						  &entry->cm_template_subject,
						  entry->cm_cert_subject);
			cm_store_set_if_not_set_as(entry,
						   &entry->cm_template_hostname,
						   entry->cm_cert_hostname);
			cm_store_set_if_not_set_as(entry,
						   &entry->cm_template_email,
						   entry->cm_cert_email);
			cm_store_set_if_not_set_as(entry,
						   &entry->cm_template_principal,
						   entry->cm_cert_principal);
			cm_store_set_if_not_set_as(entry,
						   &entry->cm_template_ipaddress,
						   entry->cm_cert_ipaddress);
			cm_store_set_if_not_set_s(entry,
						  &entry->cm_template_ku,
						  entry->cm_cert_ku);
			cm_store_set_if_not_set_s(entry,
						  &entry->cm_template_eku,
						  entry->cm_cert_eku);
			cm_store_set_if_not_set_s(entry,
						  &entry->cm_template_ns_comment,
						  entry->cm_cert_ns_comment);
			cm_store_set_if_not_set_s(entry,
						  &entry->cm_template_profile,
						  entry->cm_cert_profile);
			cm_store_set_if_not_set_s(entry,
						  &entry->cm_template_ns_certtype,
						  entry->cm_cert_ns_certtype);
			/* Walk the list of known names of known CAs and try to
			 * find the entry's CA. */
			tmp_ca = NULL;
			for (i = 0; i < (*get_n_cas)(context); i++) {
				tmp_ca = (*get_ca_by_index)(context, i);
				if ((tmp_ca->cm_nickname != NULL) &&
				    (entry->cm_ca_nickname != NULL) &&
				    (strcmp(entry->cm_ca_nickname,
					    tmp_ca->cm_nickname) == 0)) {
					break;
				}
				tmp_ca = NULL;
			}
			/* If there's an associated CA, and we know of
			 * certificates for it, and we need them to be stored
			 * somewhere, we need to make sure they'll show up in
			 * the expected locations. */
			if ((tmp_ca != NULL) &&
			    (((tmp_ca->cm_ca_root_certs != NULL) &&
			      ((entry->cm_root_cert_store_files != NULL) ||
			       (entry->cm_root_cert_store_nssdbs != NULL))) ||
			     ((tmp_ca->cm_ca_other_root_certs != NULL) &&
			      ((entry->cm_other_root_cert_store_files != NULL) ||
			       (entry->cm_other_root_cert_store_nssdbs != NULL))) ||
			     ((tmp_ca->cm_ca_other_certs != NULL) &&
			      ((entry->cm_other_cert_store_files != NULL) ||
			       (entry->cm_other_cert_store_nssdbs != NULL))))) {
				cm_log(3, "%s('%s') already had a "
				       "certificate, making sure CA "
				       "certificates will be there\n",
				       entry->cm_busname,
				       entry->cm_nickname);
				entry->cm_state = CM_NEED_TO_SAVE_ONLY_CA_CERTS;
			} else {
				cm_log(3, "%s('%s') has a certificate, "
				       "monitoring it\n",
				       entry->cm_busname,
				       entry->cm_nickname);
				entry->cm_state = CM_MONITORING;
			}
			*when = cm_time_now;
		} else
		/* If we don't have a certificate, but we know where the key
		 * should be, we have some options. */
		if (entry->cm_key_storage_type != cm_key_storage_none) {
			/* If we don't have a certificate, but we have a key,
			 * the next step is to generate a CSR. */
			if (entry->cm_key_type.cm_key_size > 0) {
				cm_log(3, "%s('%s') has no certificate, will "
				       "attempt enrollment using "
				       "already-present key\n",
				       entry->cm_busname, entry->cm_nickname);
				entry->cm_state = CM_NEED_CSR;
				*when = cm_time_now;
			} else {
				/* No certificate, no key, start with
				 * generating the key. */
				cm_log(3, "%s('%s') has no key or certificate, "
				       "will generate keys and attempt "
				       "enrollment\n",
				       entry->cm_busname, entry->cm_nickname);
				entry->cm_state = CM_NEED_KEY_PAIR;
				*when = cm_time_now;
			}
		} else {
			/* And if we don't have a place for the key, we're
			 * screwed.  Hopefully this didn't happen normally. */
			cm_log(3, "%s('%s') has no key or certificate location,"
			       " don't know what to do about that\n",
			       entry->cm_busname, entry->cm_nickname);
			entry->cm_state = CM_NEED_GUIDANCE;
			*when = cm_time_now;
		}
		break;

	case CM_INVALID:
		/* not reached */
		abort();
		break;
	}
	if (old_entry->cm_state != entry->cm_state) {
		cm_log(3, "%s('%s') moved to state '%s'\n",
		       entry->cm_busname,
		       entry->cm_nickname ?
		       entry->cm_nickname : "(unnamed entry)",
		       cm_store_state_as_string(entry->cm_state));
		cm_store_entry_save(entry);
	}
	if (emit_entry_changes != NULL) {
		(*emit_entry_changes)(context, old_entry, entry);
	}
	talloc_free(old_entry);
	return 0;
}

/* Cancel and clean up any in-progress work and then free the working state. */
int
cm_iterate_entry_done(struct cm_store_entry *entry, void *cm_iterate_state)
{
	struct cm_entry_state *state;

	state = cm_iterate_state;
	if (state != NULL) {
		if (state->cm_submit_state != NULL) {
			cm_submit_done(state->cm_submit_state);
			state->cm_submit_state = NULL;
		}
		if (state->cm_csrgen_state != NULL) {
			cm_csrgen_done(state->cm_csrgen_state);
			state->cm_csrgen_state = NULL;
		}
		if (state->cm_scepgen_state != NULL) {
			cm_scepgen_done(state->cm_scepgen_state);
			state->cm_scepgen_state = NULL;
		}
		if (state->cm_keyiread_state != NULL) {
			cm_keyiread_done(state->cm_keyiread_state);
			state->cm_keyiread_state = NULL;
		}
		if (state->cm_keygen_state != NULL) {
			cm_keygen_done(state->cm_keygen_state);
			state->cm_keygen_state = NULL;
		}
		if (state->cm_notify_state != NULL) {
			cm_notify_done(state->cm_notify_state);
			state->cm_notify_state = NULL;
		}
		if (state->cm_casave_state != NULL) {
			cm_casave_done(state->cm_casave_state);
			state->cm_casave_state = NULL;
		}
		talloc_free(state);
	}
	cm_entry_reset_state(entry);
	cm_log(3, "%s('%s') ends in state '%s'\n",
	       entry->cm_busname, entry->cm_nickname,
	       cm_store_state_as_string(entry->cm_state));
	if (cm_writing_has_lock(entry, cm_ca_phase_invalid)) {
		cm_writing_unlock_by_entry(entry);
	}
	return 0;
}

/* Set up run-time data associated with the CA. */
int
cm_iterate_ca_init(struct cm_store_ca *ca, enum cm_ca_phase phase,
		   void **cm_iterate_state)
{
	struct cm_ca_state *state;

	state = talloc_ptrtype(ca, state);
	if (state == NULL) {
		return ENOMEM;
	}
	memset(state, 0, sizeof(*state));

	state->cm_phase = phase;
	ca->cm_ca_state[phase] = CM_CA_NEED_TO_REFRESH;
	*cm_iterate_state = state;

	if (cm_writing_has_lock(ca, phase)) {
		cm_writing_unlock_by_ca(ca, phase);
	}

	cm_store_ca_save(ca);

	cm_log(3, "%s('%s').%s starts (%s)\n",
	       ca->cm_busname, ca->cm_nickname,
	       cm_store_ca_phase_as_string(state->cm_phase),
	       cm_store_ca_state_as_string(ca->cm_ca_state[phase]));
	return 0;
}

int
cm_iterate_ca(struct cm_store_ca *ca,
	      struct cm_context *context,
	      struct cm_store_ca *(*get_ca_by_index)(struct cm_context *, int),
	      int (*get_n_cas)(struct cm_context *),
	      struct cm_store_entry *(*get_entry_by_index)(struct cm_context *, int),
	      int (*get_n_entries)(struct cm_context *),
	      void (*emit_ca_changes)(struct cm_context *,
				      struct cm_store_ca *,
				      struct cm_store_ca *),
	      void *cm_iterate_state,
	      enum cm_time *when,
	      int *delay,
	      int *readfd)
{
	struct cm_store_ca *old_ca;
	struct cm_ca_state *state = cm_iterate_state;

	*readfd = -1;
	old_ca = cm_store_ca_dup(ca, ca);

	switch (ca->cm_ca_state[state->cm_phase]) {
	case CM_CA_NEED_TO_REFRESH:
		switch (state->cm_phase) {
		case cm_ca_phase_identify:
			state->cm_task_state = cm_cadata_start_identify(ca);
			break;
		case cm_ca_phase_certs:
			state->cm_task_state = cm_cadata_start_certs(ca);
			break;
		case cm_ca_phase_profiles:
			state->cm_task_state = cm_cadata_start_profiles(ca);
			break;
		case cm_ca_phase_default_profile:
			state->cm_task_state =
				cm_cadata_start_default_profile(ca);
			break;
		case cm_ca_phase_enroll_reqs:
			state->cm_task_state = cm_cadata_start_enroll_reqs(ca);
			break;
		case cm_ca_phase_renew_reqs:
			state->cm_task_state = cm_cadata_start_renew_reqs(ca);
			break;
		case cm_ca_phase_capabilities:
			state->cm_task_state = cm_cadata_start_capabilities(ca);
			break;
		case cm_ca_phase_encryption_certs:
			state->cm_task_state = cm_cadata_start_encryption_certs(ca);
			break;
		case cm_ca_phase_invalid:
			abort();
			break;
		}
		if (state->cm_task_state == NULL) {
			if (ca->cm_ca_type == cm_ca_external) {
				/* Reap any failed child processes to prevent zombies */
				cm_casave_done(state->cm_casave_state);
			}
			ca->cm_ca_state[state->cm_phase] = CM_CA_DISABLED;
			*when = cm_time_now;
		} else {
			ca->cm_ca_state[state->cm_phase] = CM_CA_REFRESHING;
			*readfd = cm_cadata_get_fd(state->cm_task_state);
			if (*readfd == -1) {
				*when = cm_time_soon;
			} else {
				*when = cm_time_no_time;
			}
		}
		break;
	case CM_CA_REFRESHING:
		if (cm_cadata_ready(state->cm_task_state) == 0) {
			if (cm_cadata_modified(state->cm_task_state) == 0) {
				cm_log(3, "%s('%s').%s data updated\n",
				       ca->cm_busname, ca->cm_nickname,
				       cm_store_ca_phase_as_string(state->cm_phase));
				cm_cadata_done(state->cm_task_state);
				state->cm_task_state = NULL;
				switch (state->cm_phase) {
				case cm_ca_phase_certs:
					ca->cm_ca_state[state->cm_phase] = CM_CA_NEED_TO_SAVE_DATA;
					break;
				case cm_ca_phase_identify:
				case cm_ca_phase_profiles:
				case cm_ca_phase_default_profile:
				case cm_ca_phase_enroll_reqs:
				case cm_ca_phase_renew_reqs:
				case cm_ca_phase_capabilities:
					if (emit_ca_changes != NULL) {
						cm_restart_entries_by_ca(context,
									 ca->cm_nickname);
					}
					ca->cm_ca_state[state->cm_phase] = CM_CA_NEED_TO_ANALYZE;
					break;
				case cm_ca_phase_encryption_certs:
					if (emit_ca_changes != NULL) {
						cm_restart_entries_by_ca(context,
									 ca->cm_nickname);
					}
					ca->cm_ca_state[state->cm_phase] = CM_CA_NEED_TO_ANALYZE;
					break;
				case cm_ca_phase_invalid:
					abort();
					break;
				}
				*when = cm_time_now;
			} else
			if (cm_cadata_needs_retry(state->cm_task_state) == 0) {
				*when = cm_time_delay;
				*delay = cm_cadata_specified_delay(state->cm_task_state);
				if (*delay < 0) {
					*delay = cm_decide_cadata_delay();
				}
				cm_cadata_done(state->cm_task_state);
				state->cm_task_state = NULL;
				cm_log(3, "%s('%s').%s server needs retry\n",
				       ca->cm_busname, ca->cm_nickname,
				       cm_store_ca_phase_as_string(state->cm_phase));
				ca->cm_ca_state[state->cm_phase] = CM_CA_NEED_TO_REFRESH;
			} else
			if (cm_cadata_rejected(state->cm_task_state) == 0) {
				cm_cadata_done(state->cm_task_state);
				state->cm_task_state = NULL;
				cm_log(3, "%s('%s').%s server doesn't support that\n",
				       ca->cm_busname, ca->cm_nickname,
				       cm_store_ca_phase_as_string(state->cm_phase));
				ca->cm_ca_state[state->cm_phase] = CM_CA_IDLE;
				*when = cm_time_delay;
				*delay = CM_DELAY_CA_POLL_MAXIMUM;
			} else
			if (cm_cadata_unreachable(state->cm_task_state) == 0) {
				cm_cadata_done(state->cm_task_state);
				state->cm_task_state = NULL;
				cm_log(3, "%s('%s').%s server unreachable\n",
				       ca->cm_busname, ca->cm_nickname,
				       cm_store_ca_phase_as_string(state->cm_phase));
				ca->cm_ca_state[state->cm_phase] = CM_CA_DATA_UNREACHABLE;
				*when = cm_time_delay;
				*delay = cm_decide_cadata_delay();
			} else
			if (cm_cadata_unsupported(state->cm_task_state) == 0) {
				cm_cadata_done(state->cm_task_state);
				switch (state->cm_phase) {
				case cm_ca_phase_certs:
					ca->cm_ca_root_certs = NULL;
					ca->cm_ca_other_root_certs = NULL;
					ca->cm_ca_other_certs = NULL;
					break;
				case cm_ca_phase_identify:
					break;
				case cm_ca_phase_profiles:
					break;
				case cm_ca_phase_default_profile:
					break;
				case cm_ca_phase_enroll_reqs:
					break;
				case cm_ca_phase_renew_reqs:
					break;
				case cm_ca_phase_capabilities:
					ca->cm_ca_capabilities = NULL;
					break;
				case cm_ca_phase_encryption_certs:
					ca->cm_ca_encryption_cert = NULL;
					ca->cm_ca_encryption_issuer_cert = NULL;
					ca->cm_ca_encryption_cert_pool = NULL;
					break;
				case cm_ca_phase_invalid:
					abort();
					break;
				}
				state->cm_task_state = NULL;
				cm_log(3, "%s('%s').%s retrieval unsupported\n",
				       ca->cm_busname, ca->cm_nickname,
				       cm_store_ca_phase_as_string(state->cm_phase));
				ca->cm_ca_state[state->cm_phase] = CM_CA_DISABLED;
				*when = cm_time_now;
			} else
			if (cm_cadata_unconfigured(state->cm_task_state) == 0) {
				cm_cadata_done(state->cm_task_state);
				state->cm_task_state = NULL;
				cm_log(3, "%s('%s').%s missing configuration\n",
				       ca->cm_busname, ca->cm_nickname,
				       cm_store_ca_phase_as_string(state->cm_phase));
				ca->cm_ca_state[state->cm_phase] = CM_CA_DATA_UNREACHABLE;
				*when = cm_time_delay;
				*delay = cm_decide_cadata_delay();
			} else {
				cm_cadata_done(state->cm_task_state);
				state->cm_task_state = NULL;
				cm_log(3, "%s('%s').%s data is unchanged\n",
				       ca->cm_busname, ca->cm_nickname,
				       cm_store_ca_phase_as_string(state->cm_phase));
				ca->cm_ca_state[state->cm_phase] = CM_CA_NEED_TO_ANALYZE;
				*when = cm_time_now;
			}
		} else {
			/* Wait for status update, or poll. */
			*readfd = cm_cadata_get_fd(state->cm_task_state);
			if (*readfd == -1) {
				*when = cm_time_soon;
			} else {
				*when = cm_time_no_time;
			}
		}
		break;
	case CM_CA_NEED_TO_SAVE_DATA:
		if (!cm_writing_lock_by_ca(ca, state->cm_phase)) {
			/* Just hang out in this state while we're messing
			 * around with the outside world for another CA. */
			cm_log(3, "%s('%s').%s waiting for saving lock\n",
			       ca->cm_busname, ca->cm_nickname,
			       cm_store_ca_phase_as_string(state->cm_phase));
			*when = cm_time_soon;
			break;
		}
		state->cm_hook_state = cm_hook_start_ca_presave(ca,
							        context,
							        get_ca_by_index,
							        get_n_cas,
							        get_entry_by_index,
							        get_n_entries);
		if (state->cm_hook_state != NULL) {
			/* Note that we're doing the pre-save. */
			ca->cm_ca_state[state->cm_phase] = CM_CA_PRE_SAVE_DATA;
			/* Wait for status update, or poll. */
			*readfd = cm_hook_get_fd(state->cm_hook_state);
			if (*readfd == -1) {
				*when = cm_time_soon;
			} else {
				*when = cm_time_no_time;
			}
		} else {
			/* Failed to start the pre-save; skip it. */
			ca->cm_ca_state[state->cm_phase] = CM_CA_START_SAVING_DATA;
			*when = cm_time_now;
		}
		break;
	case CM_CA_PRE_SAVE_DATA:
		if (cm_hook_ready(state->cm_hook_state) == 0) {
			cm_hook_done(state->cm_hook_state);
			state->cm_hook_state = NULL;
			ca->cm_ca_state[state->cm_phase] = CM_CA_START_SAVING_DATA;
			*when = cm_time_now;
		} else {
			/* Wait for status update, or poll. */
			*readfd = cm_hook_get_fd(state->cm_hook_state);
			if (*readfd == -1) {
				*when = cm_time_soon;
			} else {
				*when = cm_time_no_time;
			}
		}
		break;
	case CM_CA_START_SAVING_DATA:
		state->cm_casave_state = cm_casave_start(NULL, ca, context,
							 get_ca_by_index,
							 get_n_cas,
							 get_entry_by_index,
							 get_n_entries);
		if (state->cm_casave_state != NULL) {
			ca->cm_ca_state[state->cm_phase] = CM_CA_SAVING_DATA;
			/* Wait for status update, or poll. */
			*readfd = cm_casave_get_fd(state->cm_casave_state);
			if (*readfd == -1) {
				*when = cm_time_soon;
			} else {
				*when = cm_time_no_time;
			}
		}
		break;
	case CM_CA_SAVING_DATA:
		if (cm_casave_ready(state->cm_casave_state) == 0) {
			if (cm_casave_saved(state->cm_casave_state) == 0) {
				/* Saved certificates. */
				cm_casave_done(state->cm_casave_state);
				state->cm_casave_state = NULL;
				ca->cm_ca_state[state->cm_phase] = CM_CA_NEED_POST_SAVE_DATA;
				*when = cm_time_now;
			} else
			if (cm_casave_permissions_error(state->cm_casave_state) == 0) {
				/* Whoops, we need help. */
				cm_casave_done(state->cm_casave_state);
				state->cm_casave_state = NULL;
				ca->cm_ca_state[state->cm_phase] = CM_CA_NEED_POST_SAVE_DATA;
				*when = cm_time_now;
			} else {
				/* Failed to save certs. */
				cm_casave_done(state->cm_casave_state);
				state->cm_casave_state = NULL;
				ca->cm_ca_state[state->cm_phase] = CM_CA_NEED_POST_SAVE_DATA;
				*when = cm_time_soonish;
			}
		} else {
			/* Wait for status update, or poll. */
			*readfd = cm_casave_get_fd(state->cm_casave_state);
			if (*readfd == -1) {
				*when = cm_time_soon;
			} else {
				*when = cm_time_no_time;
			}
		}
		break;
	case CM_CA_NEED_POST_SAVE_DATA:
		state->cm_hook_state = cm_hook_start_ca_postsave(ca,
							         context,
							         get_ca_by_index,
							         get_n_cas,
							         get_entry_by_index,
							         get_n_entries);
		if (state->cm_hook_state != NULL) {
			/* Note that we're doing the post-save. */
			ca->cm_ca_state[state->cm_phase] = CM_CA_POST_SAVE_DATA;
			/* Wait for status update, or poll. */
			*readfd = cm_hook_get_fd(state->cm_hook_state);
			if (*readfd == -1) {
				*when = cm_time_soon;
			} else {
				*when = cm_time_no_time;
			}
		} else {
			/* Failed to start the post-save, or nothing to do;
			 * skip it. */
			ca->cm_ca_state[state->cm_phase] = CM_CA_SAVED_DATA;
			*when = cm_time_now;
		}
		break;
	case CM_CA_POST_SAVE_DATA:
		if (cm_hook_ready(state->cm_hook_state) == 0) {
			cm_hook_done(state->cm_hook_state);
			state->cm_hook_state = NULL;
			ca->cm_ca_state[state->cm_phase] = CM_CA_SAVED_DATA;
			*when = cm_time_now;
		} else {
			/* Wait for status update, or poll. */
			*readfd = cm_hook_get_fd(state->cm_hook_state);
			if (*readfd == -1) {
				*when = cm_time_soon;
			} else {
				*when = cm_time_no_time;
			}
		}
		break;
	case CM_CA_SAVED_DATA:
		if (!cm_writing_unlock_by_ca(ca, state->cm_phase)) {
			/* If for some reason we fail to release the lock that
			 * we have, try to release it again soon. */
			*when = cm_time_soon;
			cm_log(1, "%s('%s').%s failed to release saving "
			       "lock, probably a bug\n",
			       ca->cm_busname, ca->cm_nickname,
			       cm_store_ca_phase_as_string(state->cm_phase));
			break;
		}
		ca->cm_ca_state[state->cm_phase] = CM_CA_NEED_TO_ANALYZE;
		*when = cm_time_now;
		break;
	case CM_CA_NEED_TO_ANALYZE:
		switch (state->cm_phase) {
		case cm_ca_phase_certs:
			state->cm_ca_cert_analyze_state = cm_ca_analyze_start_certs(ca);
			if (state->cm_ca_cert_analyze_state == NULL) {
				ca->cm_ca_state[state->cm_phase] = CM_CA_DISABLED;
				*when = cm_time_now;
			} else {
				*readfd = cm_ca_analyze_get_fd(state->cm_ca_cert_analyze_state);
				if (*readfd == -1) {
					cm_ca_analyze_done(state->cm_ca_cert_analyze_state);
					ca->cm_ca_state[state->cm_phase] = CM_CA_DISABLED;
				} else {
					ca->cm_ca_state[state->cm_phase] = CM_CA_ANALYZING;
					*when = cm_time_no_time;
				}
			}
			break;
		case cm_ca_phase_identify:
		case cm_ca_phase_profiles:
		case cm_ca_phase_default_profile:
		case cm_ca_phase_enroll_reqs:
		case cm_ca_phase_renew_reqs:
		case cm_ca_phase_capabilities:
			ca->cm_ca_state[state->cm_phase] = CM_CA_IDLE;
			*when = cm_time_now;
			break;
		case cm_ca_phase_encryption_certs:
			state->cm_ca_ecert_analyze_state = cm_ca_analyze_start_encryption_certs(ca);
			if (state->cm_ca_ecert_analyze_state == NULL) {
				ca->cm_ca_state[state->cm_phase] = CM_CA_DISABLED;
				*when = cm_time_now;
			} else {
				*readfd = cm_ca_analyze_get_fd(state->cm_ca_ecert_analyze_state);
				if (*readfd == -1) {
					cm_ca_analyze_done(state->cm_ca_ecert_analyze_state);
					ca->cm_ca_state[state->cm_phase] = CM_CA_DISABLED;
				} else {
					ca->cm_ca_state[state->cm_phase] = CM_CA_ANALYZING;
					*when = cm_time_no_time;
				}
			}
			break;
		case cm_ca_phase_invalid:
			abort();
			break;
		}
		break;
	case CM_CA_ANALYZING:
		switch (state->cm_phase) {
		case cm_ca_phase_certs:
			if (cm_ca_analyze_ready(state->cm_ca_cert_analyze_state) == 0) {
				state->cm_cert_refresh_delay = cm_ca_analyze_get_delay(state->cm_ca_cert_analyze_state);
				cm_ca_analyze_done(state->cm_ca_cert_analyze_state);
				state->cm_ca_cert_analyze_state = NULL;
				if (state->cm_cert_refresh_delay != 0) {
					ca->cm_ca_state[state->cm_phase] = CM_CA_NEED_TO_REFRESH;
					*delay = state->cm_cert_refresh_delay;
					if (*delay < CM_DELAY_CA_POLL_MINIMUM) {
						*delay = CM_DELAY_CA_POLL_MINIMUM;
					}
					if (*delay > CM_DELAY_CA_POLL_MAXIMUM) {
						*delay = CM_DELAY_CA_POLL_MAXIMUM;
					}
					*when = cm_time_delay;
				} else {
					ca->cm_ca_state[state->cm_phase] = CM_CA_IDLE;
					*when = cm_time_now;
				}
			} else {
				/* Wait for status update, or poll. */
				*readfd = cm_ca_analyze_get_fd(state->cm_ca_cert_analyze_state);
				if (*readfd == -1) {
					*when = cm_time_soon;
				} else {
					*when = cm_time_no_time;
				}
			}
			break;
		case cm_ca_phase_encryption_certs:
			if (cm_ca_analyze_ready(state->cm_ca_ecert_analyze_state) == 0) {
				state->cm_ecert_refresh_delay = cm_ca_analyze_get_delay(state->cm_ca_ecert_analyze_state);
				cm_ca_analyze_done(state->cm_ca_ecert_analyze_state);
				state->cm_ca_ecert_analyze_state = NULL;
				if (state->cm_ecert_refresh_delay != 0) {
					ca->cm_ca_state[state->cm_phase] = CM_CA_NEED_TO_REFRESH;
					*delay = state->cm_ecert_refresh_delay;
					if (*delay < CM_DELAY_CA_POLL_MINIMUM) {
						*delay = CM_DELAY_CA_POLL_MINIMUM;
					}
					if (*delay > CM_DELAY_CA_POLL_MAXIMUM) {
						*delay = CM_DELAY_CA_POLL_MAXIMUM;
					}
					*when = cm_time_delay;
				} else {
					ca->cm_ca_state[state->cm_phase] = CM_CA_IDLE;
					*when = cm_time_now;
				}
			} else {
				/* Wait for status update, or poll. */
				*readfd = cm_ca_analyze_get_fd(state->cm_ca_ecert_analyze_state);
				if (*readfd == -1) {
					*when = cm_time_soon;
				} else {
					*when = cm_time_no_time;
				}
			}
			break;
		case cm_ca_phase_identify:
		case cm_ca_phase_profiles:
		case cm_ca_phase_default_profile:
		case cm_ca_phase_enroll_reqs:
		case cm_ca_phase_renew_reqs:
		case cm_ca_phase_capabilities:
		case cm_ca_phase_invalid:
			abort();
			break;
		}
		break;
	case CM_CA_DATA_UNREACHABLE:
		ca->cm_ca_state[state->cm_phase] = CM_CA_NEED_TO_REFRESH;
		*when = cm_time_soonish;
		break;
	case CM_CA_IDLE:
	case CM_CA_DISABLED:
		*when = cm_time_no_time;
		break;
	}
	if (ca->cm_ca_state[state->cm_phase] != old_ca->cm_ca_state[state->cm_phase]) {
		cm_log(3, "%s('%s').%s moved to state '%s'\n",
		       ca->cm_busname, ca->cm_nickname,
		       cm_store_ca_phase_as_string(state->cm_phase),
		       cm_store_ca_state_as_string(ca->cm_ca_state[state->cm_phase]));
		cm_store_ca_save(ca);
	}
	if (emit_ca_changes != NULL) {
		(*emit_ca_changes)(context, old_ca, ca);
	}
	talloc_free(old_ca);
	return 0;
}

/* Cancel and clean up any in-progress work and then free the working state. */
int
cm_iterate_ca_done(struct cm_store_ca *ca, void *cm_iterate_state)
{
	struct cm_ca_state *state;
	enum cm_ca_phase phase = cm_ca_phase_invalid;
	const char *phases, *states;

	state = cm_iterate_state;

	phases = cm_store_ca_phase_as_string(phase);
	states = cm_store_ca_state_as_string(CM_CA_DISABLED);

	if (state != NULL) {
	       phase = state->cm_phase,
	       phases = cm_store_ca_phase_as_string(phase),
	       states = cm_store_ca_state_as_string(ca->cm_ca_state[phase]);
		if (state->cm_ca_cert_analyze_state != NULL) {
			cm_ca_analyze_done(state->cm_ca_cert_analyze_state);
			state->cm_ca_cert_analyze_state = NULL;
		}
		if (state->cm_ca_ecert_analyze_state != NULL) {
			cm_ca_analyze_done(state->cm_ca_ecert_analyze_state);
			state->cm_ca_ecert_analyze_state = NULL;
		}
		if (state->cm_task_state != NULL) {
			cm_cadata_done(state->cm_task_state);
			state->cm_task_state = NULL;
		}
		if (state->cm_hook_state != NULL) {
			cm_hook_done(state->cm_hook_state);
			state->cm_hook_state = NULL;
		}
		if (state->cm_casave_state != NULL) {
			cm_casave_done(state->cm_casave_state);
			state->cm_casave_state = NULL;
		}
		talloc_free(state);
	}

	cm_log(3, "%s('%s').%s ends (%s)\n",
	       ca->cm_busname, ca->cm_nickname, phases, states);

	if (cm_writing_has_lock(ca, phase)) {
		cm_writing_unlock_by_ca(ca, phase);
	}
	return 0;
}