[e66ed4]: src / core / PWScore.cpp Maximize Restore History

Download this file

PWScore.cpp    3281 lines (2847 with data), 105.9 kB

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
/*
* Copyright (c) 2003-2013 Rony Shapiro <ronys@users.sourceforge.net>.
* All rights reserved. Use of the code is allowed under the
* Artistic License 2.0 terms, as specified in the LICENSE file
* distributed with this code, or available from
* http://www.opensource.org/licenses/artistic-license-2.0.php
*/
// file PWScore.cpp
//-----------------------------------------------------------------------------
#include "PWScore.h"
#include "core.h"
#include "TwoFish.h"
#include "PWSprefs.h"
#include "PWSrand.h"
#include "Util.h"
#include "SysInfo.h"
#include "UTF8Conv.h"
#include "Report.h"
#include "VerifyFormat.h"
#include "PWSfileV3.h" // XXX cleanup with dynamic_cast
#include "StringXStream.h"
#include "os/typedefs.h"
#include "os/dir.h"
#include "os/debug.h"
#include "os/file.h"
#include "os/mem.h"
#include "os/logit.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <functional>
#include <algorithm>
#include <set>
#include <iterator>
using namespace std;
using pws_os::CUUID;
unsigned char PWScore::m_session_key[32];
unsigned char PWScore::m_session_initialized = false;
Asker *PWScore::m_pAsker = NULL;
Reporter *PWScore::m_pReporter = NULL;
// Following structure used in ReadFile and Validate
static bool GTUCompareV1(const st_GroupTitleUser &gtu1, const st_GroupTitleUser &gtu2)
{
if (gtu1.group != gtu2.group)
return gtu1.group.compare(gtu2.group) < 0;
else if (gtu1.title != gtu2.title)
return gtu1.title.compare(gtu2.title) < 0;
else
return gtu1.user.compare(gtu2.user) < 0;
}
PWScore::PWScore() :
m_currfile(_T("")),
m_passkey(NULL), m_passkey_len(0),
m_hashIters(MIN_HASH_ITERATIONS),
m_lockFileHandle(INVALID_HANDLE_VALUE),
m_lockFileHandle2(INVALID_HANDLE_VALUE),
m_LockCount(0), m_LockCount2(0),
m_ReadFileVersion(PWSfile::UNKNOWN_VERSION),
m_bDBChanged(false), m_bDBPrefsChanged(false),
m_IsReadOnly(false), m_bUniqueGTUValidated(false),
m_nRecordsWithUnknownFields(0),
m_bNotifyDB(false), m_pUIIF(NULL), m_pFileSig(NULL),
m_iAppHotKey(0)
{
// following should ideally be wrapped in a mutex
if (!PWScore::m_session_initialized) {
PWScore::m_session_initialized = true;
CItemData::SetSessionKey(); // per-session initialization
pws_os::mlock(m_session_key, sizeof(m_session_key));
PWSrand::GetInstance()->GetRandomData(m_session_key, sizeof(m_session_key));
if (!pws_os::mcryptProtect(m_session_key, sizeof(m_session_key))) {
pws_os::Trace(_T("pws_os::mcryptProtect failed"));
}
}
m_undo_iter = m_redo_iter = m_vpcommands.end();
}
PWScore::~PWScore()
{
// do NOT trash m_session_*, as there may be other cores around
// relying on it. Trashing the ciphertext encrypted with it is enough
const unsigned int BS = TwoFish::BLOCKSIZE;
if (m_passkey_len > 0) {
trashMemory(m_passkey, ((m_passkey_len + (BS - 1)) / BS) * BS);
delete[] m_passkey;
m_passkey = NULL;
m_passkey_len = 0;
}
m_UHFL.clear();
m_vnodes_modified.clear();
delete m_pFileSig;
}
void PWScore::SetApplicationNameAndVersion(const stringT &appName,
DWORD dwMajorMinor)
{
int nMajor = HIWORD(dwMajorMinor);
int nMinor = LOWORD(dwMajorMinor);
Format(m_AppNameAndVersion, _T("%s V%d.%02d"), appName.c_str(),
nMajor, nMinor);
}
// Return whether first [g:t:u] is greater than the second [g:t:u]
// used in std::sort in SortDependents below.
static bool GTUCompare(const StringX &elem1, const StringX &elem2)
{
StringX::size_type i1 = elem1.find(_T(':'));
StringX g1 = (i1 == StringX::npos) ? elem1 : elem1.substr(0, i1);
StringX::size_type i2 = elem2.find(_T(':'));
StringX g2 = (i2 == StringX::npos) ? elem2 : elem2.substr(0, i2);
if (g1 != g2)
return g1.compare(g2) < 0;
StringX tmp1 = elem1.substr(g1.length() + 1);
StringX tmp2 = elem2.substr(g2.length() + 1);
i1 = tmp1.find(_T(':'));
StringX t1 = (i1 == StringX::npos) ? tmp1 : tmp1.substr(0, i1);
i2 = tmp2.find(_T(':'));
StringX t2 = (i2 == StringX::npos) ? tmp2 : tmp2.substr(0, i2);
if (t1 != t2)
return t1.compare(t2) < 0;
tmp1 = tmp1.substr(t1.length() + 1);
tmp2 = tmp2.substr(t2.length() + 1);
i1 = tmp1.find(_T(':'));
StringX u1 = (i1 == StringX::npos) ? tmp1 : tmp1.substr(0, i1);
i2 = tmp2.find(_T(':'));
StringX u2 = (i2 == StringX::npos) ? tmp2 : tmp2.substr(0, i2);
return u1.compare(u2) < 0;
}
void PWScore::SortDependents(UUIDVector &dlist, StringX &sxDependents)
{
std::vector<StringX> sorted_dependents;
std::vector<StringX>::iterator sd_iter;
ItemListIter iter;
UUIDVectorIter diter;
StringX sx_dependent;
for (diter = dlist.begin(); diter != dlist.end(); diter++) {
iter = Find(*diter);
if (iter != GetEntryEndIter()) {
sx_dependent = iter->second.GetGroup() + _T(":") +
iter->second.GetTitle() + _T(":") +
iter->second.GetUser();
sorted_dependents.push_back(sx_dependent);
}
}
std::sort(sorted_dependents.begin(), sorted_dependents.end(), GTUCompare);
sxDependents = _T("");
for (sd_iter = sorted_dependents.begin(); sd_iter != sorted_dependents.end(); sd_iter++)
sxDependents += _T("\t[") + *sd_iter + _T("]\r\n");
}
void PWScore::DoAddEntry(const CItemData &item)
{
// Also "UndoDeleteEntry" !
ASSERT(m_pwlist.find(item.GetUUID()) == m_pwlist.end());
m_pwlist[item.GetUUID()] = item;
if (item.NumberUnknownFields() > 0)
IncrementNumRecordsWithUnknownFields();
if (item.IsNormal() && item.IsPolicyNameSet()) {
IncrementPasswordPolicy(item.GetPolicyName());
}
int32 iKBShortcut;
item.GetKBShortcut(iKBShortcut);
if (iKBShortcut != 0)
VERIFY(AddKBShortcut(iKBShortcut, item.GetUUID()));
m_bDBChanged = true;
}
bool PWScore::ConfirmDelete(const CItemData *pci)
{
ASSERT(pci != NULL);
if (pci->IsBase() && m_pAsker != NULL) {
UUIDVector dependentslist;
CUUID entry_uuid = pci->GetUUID();
CItemData::EntryType entrytype = pci->GetEntryType();
// If we're deleting a base (entry with aliases or shortcuts
// referencing it), notify user and give her a chance to bail out:
if (entrytype == CItemData::ET_ALIASBASE)
GetAllDependentEntries(entry_uuid, dependentslist, CItemData::ET_ALIAS);
else if (entrytype == CItemData::ET_SHORTCUTBASE)
GetAllDependentEntries(entry_uuid, dependentslist, CItemData::ET_SHORTCUT);
size_t num_dependents = dependentslist.size();
ASSERT(num_dependents > 0); // otherwise pci shouldn't be a base!
if (num_dependents > 0) {
StringX sxDependents;
SortDependents(dependentslist, sxDependents);
stringT cs_msg, cs_type;
stringT cs_title;
LoadAString(cs_title, IDSC_DELETEBASET);
if (entrytype == CItemData::ET_ALIASBASE) {
LoadAString(cs_type, num_dependents == 1 ? IDSC_ALIAS : IDSC_ALIASES);
Format(cs_msg, IDSC_DELETEABASE, dependentslist.size(),
cs_type.c_str(), sxDependents.c_str());
} else {
LoadAString(cs_type, num_dependents == 1 ? IDSC_SHORTCUT : IDSC_SHORTCUTS);
Format(cs_msg, IDSC_DELETESBASE, dependentslist.size(),
cs_type.c_str(), sxDependents.c_str());
}
return (*m_pAsker)(cs_title, cs_msg);
} // num_dependents > 0
}
return true;
}
void PWScore::DoDeleteEntry(const CItemData &item)
{
// Also "UndoAddEntry" !
// Added handling of alias/shortcut/base entry types
// Most of this will go away once the entry types
// are implemented as subclasses.
CUUID entry_uuid = item.GetUUID();
ItemListIter pos = m_pwlist.find(entry_uuid);
if (pos != m_pwlist.end()) {
// Simple cases first: Aliases or shortcuts, update maps
// and refresh base's display, if changed
CItemData::EntryType entrytype = item.GetEntryType();
CUUID base_uuid(CUUID::NullUUID());
if (item.IsDependent()) {
GetDependentEntryBaseUUID(entry_uuid, base_uuid, entrytype);
DoRemoveDependentEntry(base_uuid, entry_uuid, entrytype);
} else if (item.IsAliasBase()) {
ResetAllAliasPasswords(entry_uuid);
} else if (item.IsShortcutBase()) {
// Recurse on all dependents
ItemMMap deps(m_base2shortcuts_mmap.lower_bound(entry_uuid),
m_base2shortcuts_mmap.upper_bound(entry_uuid));
for (ItemMMapIter iter = deps.begin(); iter != deps.end(); iter++) {
CItemData depItem = Find(iter->second)->second;
DoDeleteEntry(depItem);
// Set deleted for GUIRefreshEntry() which will remove from display
depItem.SetStatus(CItemData::ES_DELETED);
GUIRefreshEntry(depItem);
}
}
int32 iKBShortcut;
item.GetKBShortcut(iKBShortcut);
if (iKBShortcut != 0)
VERIFY(DelKBShortcut(iKBShortcut, item.GetUUID()));
m_bDBChanged = true;
m_pwlist.erase(pos); // at last!
if (item.NumberUnknownFields() > 0)
DecrementNumRecordsWithUnknownFields();
if (item.IsNormal() && item.IsPolicyNameSet()) {
DecrementPasswordPolicy(item.GetPolicyName());
}
NotifyDBModified();
} // pos != m_pwlist.end()
}
void PWScore::DoReplaceEntry(const CItemData &old_ci, const CItemData &new_ci)
{
// Assumes that old_uuid == new_uuid
ASSERT(old_ci.GetUUID() == new_ci.GetUUID());
m_pwlist[old_ci.GetUUID()] = new_ci;
if (old_ci.GetEntryType() != new_ci.GetEntryType() || old_ci.IsProtected() != new_ci.IsProtected())
GUIRefreshEntry(new_ci);
// Check if we need to update Expiry vector
time_t tttXTime_old, tttXTime_new;
old_ci.GetXTime(tttXTime_old);
new_ci.GetXTime(tttXTime_new);
if (tttXTime_old != tttXTime_new) {
if (tttXTime_old != time_t(0))
RemoveExpiryEntry(old_ci);
if (tttXTime_new != time_t(0))
AddExpiryEntry(new_ci);
}
if (old_ci.IsNormal() && old_ci.IsPolicyNameSet()) {
DecrementPasswordPolicy(old_ci.GetPolicyName());
}
if (new_ci.IsNormal() && new_ci.IsPolicyNameSet()) {
IncrementPasswordPolicy(new_ci.GetPolicyName());
}
int ioldKBShortcut, inewKBShortcut;
old_ci.GetKBShortcut(ioldKBShortcut);
new_ci.GetKBShortcut(inewKBShortcut);
if (ioldKBShortcut != inewKBShortcut) {
if (ioldKBShortcut != 0)
VERIFY(DelKBShortcut(ioldKBShortcut, old_ci.GetUUID()));
if (inewKBShortcut != 0)
VERIFY(AddKBShortcut(inewKBShortcut, new_ci.GetUUID()));
}
m_bDBChanged = true;
}
void PWScore::ClearData(void)
{
const unsigned int BS = TwoFish::BLOCKSIZE;
if (m_passkey_len > 0) {
trashMemory(m_passkey, ((m_passkey_len + (BS - 1)) / BS) * BS);
delete[] m_passkey;
m_passkey = NULL;
m_passkey_len = 0;
}
m_passkey = NULL;
//Composed of ciphertext, so doesn't need to be overwritten
m_pwlist.clear();
// Clear out out dependents mappings
m_base2aliases_mmap.clear();
m_alias2base_map.clear();
m_base2shortcuts_mmap.clear();
m_shortcut2base_map.clear();
// Clear out unknown fields
m_UHFL.clear();
// Clear out database filters
m_MapFilters.clear();
// Clear out policies
m_MapPSWDPLC.clear();
// Clear out Empty Groups
m_vEmptyGroups.clear();
// Clear out commands
ClearCommands();
}
void PWScore::ReInit(bool bNewFile)
{
PWS_LOGIT;
// Now reset all values as if created from new
if (bNewFile)
m_ReadFileVersion = PWSfile::NEWFILE;
else
m_ReadFileVersion = PWSfile::UNKNOWN_VERSION;
const unsigned int BS = TwoFish::BLOCKSIZE;
if (m_passkey_len > 0) {
trashMemory(m_passkey, ((m_passkey_len + (BS - 1)) / BS) * BS);
delete[] m_passkey;
m_passkey = NULL;
m_passkey_len = 0;
}
m_nRecordsWithUnknownFields = 0;
m_UHFL.clear();
ClearChangedNodes();
// Clear expired password entries
m_ExpireCandidates.clear();
// Clear entry keyboard shortcuts
m_KBShortcutMap.clear();
// Clear any unknown preferences from previous databases
PWSprefs::GetInstance()->ClearUnknownPrefs();
SetChanged(false, false);
}
void PWScore::NewFile(const StringX &passkey)
{
ClearData();
SetPassKey(passkey);
m_ReadFileVersion = PWSfile::VCURRENT;
SetChanged(false, false);
}
// functor object type for for_each:
struct RecordWriter {
RecordWriter(PWSfile *pout, PWScore *pcore) : m_pout(pout), m_pcore(pcore) {}
void operator()(pair<CUUID const, CItemData> &p)
{
StringX savePassword = p.second.GetPassword();
StringX uuid_str(savePassword);
CUUID base_uuid(CUUID::NullUUID());
CUUID item_uuid = p.second.GetUUID();
if (p.second.IsAlias()) {
m_pcore->GetDependentEntryBaseUUID(item_uuid, base_uuid, CItemData::ET_ALIAS);
uuid_str = _T("[[");
uuid_str += base_uuid;
uuid_str += _T("]]");
} else if (p.second.IsShortcut()) {
m_pcore->GetDependentEntryBaseUUID(item_uuid, base_uuid, CItemData::ET_SHORTCUT);
uuid_str = _T("[~");
uuid_str += base_uuid;
uuid_str += _T("~]");
}
p.second.SetPassword(uuid_str);
m_pout->WriteRecord(p.second);
p.second.SetPassword(savePassword);
p.second.ClearStatus();
}
private:
PWSfile *m_pout;
PWScore *m_pcore;
};
int PWScore::WriteFile(const StringX &filename, const bool bUpdateSig,
PWSfile::VERSION version)
{
PWS_LOGIT_ARGS("bUpdateSig=%s", bUpdateSig ? _T("true") : _T("false"));
int status;
PWSfile *out = PWSfile::MakePWSfile(filename, version,
PWSfile::Write, status);
if (status != PWSfile::SUCCESS) {
delete out;
return status;
}
if (bUpdateSig) {
// since we're writing a new file, the previous sig's
// about to be invalidated but NOT if a user initiated Backup
delete m_pFileSig;
m_pFileSig = NULL;
}
m_hdr.m_prefString = PWSprefs::GetInstance()->Store();
m_hdr.m_whatlastsaved = m_AppNameAndVersion.c_str();
m_hdr.m_RUEList = m_RUEList;
out->SetHeader(m_hdr);
// Give PWSfileV3 the unknown headers to write out
// XXX cleanup gross dynamic_cast
PWSfileV3 *out3 = dynamic_cast<PWSfileV3 *>(out);
if (out3 != NULL) {
out3->SetNHashIters(GetHashIters());
out3->SetUnknownHeaderFields(m_UHFL);
out3->SetFilters(m_MapFilters); // Give it the filters to write out
out3->SetPasswordPolicies(m_MapPSWDPLC); // Give it the password policies to write out
out3->SetEmptyGroups(m_vEmptyGroups); // Give it the Empty Groups to write out
}
try { // exception thrown on write error
status = out->Open(GetPassKey());
if (status != PWSfile::SUCCESS) {
delete out;
return status;
}
RecordWriter write_record(out, this);
for_each(m_pwlist.begin(), m_pwlist.end(), write_record);
m_hdr = out->GetHeader(); // update time saved, etc.
} catch (...) {
out->Close();
delete out;
return FAILURE;
}
out->Close();
delete out;
SetChanged(false, false);
m_ReadFileVersion = version; // needed when saving a V17 as V20 1st time [871893]
// Create new signature if required
if (bUpdateSig)
m_pFileSig = new PWSFileSig(filename.c_str());
return SUCCESS;
}
void PWScore::ClearCommands()
{
while (!m_vpcommands.empty()) {
delete m_vpcommands.back();
m_vpcommands.pop_back();
}
m_undo_iter = m_redo_iter = m_vpcommands.end();
}
void PWScore::ResetStateAfterSave()
{
PWS_LOGIT;
// After a Save/SaveAs, need to change all saved DB modified states
// in any commands in the list that can be undone or redone
// State = whether DB changed (false: no, true: yes)
// After a save, all commands in the undo/redo chain should now have
// saved state = changed
std::vector<Command *>::iterator cmd_iter;
for (cmd_iter = m_vpcommands.begin(); cmd_iter != m_vpcommands.end(); cmd_iter++) {
(*cmd_iter)->ResetSavedState(true);
}
// However, the next command in the list (redo) should now have
// saved state = unchanged.
if (m_redo_iter != m_vpcommands.end())
(*m_redo_iter)->ResetSavedState(false);
}
int PWScore::Execute(Command *pcmd)
{
if (m_redo_iter != m_vpcommands.end()) {
std::vector<Command *>::iterator cmd_Iter;
for (cmd_Iter = m_redo_iter; cmd_Iter != m_vpcommands.end(); cmd_Iter++) {
delete (*cmd_Iter);
}
m_vpcommands.erase(m_redo_iter, m_vpcommands.end());
}
m_vpcommands.push_back(pcmd);
m_undo_iter = m_redo_iter = m_vpcommands.end();
int rc = pcmd->Execute();
m_undo_iter--;
NotifyGUINeedsUpdating(UpdateGUICommand::GUI_UPDATE_STATUSBAR, CUUID::NullUUID());
return rc;
}
void PWScore::Undo()
{
ASSERT(m_undo_iter != m_vpcommands.end());
m_redo_iter = m_undo_iter;
(*m_undo_iter)->Undo();
if (m_undo_iter == m_vpcommands.begin())
m_undo_iter = m_vpcommands.end();
else
m_undo_iter--;
NotifyGUINeedsUpdating(UpdateGUICommand::GUI_UPDATE_STATUSBAR, CUUID::NullUUID());
}
void PWScore::Redo()
{
ASSERT(m_redo_iter != m_vpcommands.end());
m_undo_iter = m_redo_iter;
(*m_redo_iter)->Redo();
if (m_redo_iter != m_vpcommands.end())
m_redo_iter++;
NotifyGUINeedsUpdating(UpdateGUICommand::GUI_UPDATE_STATUSBAR, CUUID::NullUUID());
}
bool PWScore::AnyToUndo() const
{
return (m_undo_iter != m_vpcommands.end());
}
bool PWScore::AnyToRedo() const
{
return (m_redo_iter != m_vpcommands.end());
}
int PWScore::CheckPasskey(const StringX &filename, const StringX &passkey)
{
int status;
if (!filename.empty())
status = PWSfile::CheckPasskey(filename, passkey, m_ReadFileVersion);
else { // can happen if tries to export b4 save
size_t t_passkey_len = passkey.length();
if (t_passkey_len != m_passkey_len) // trivial test
return WRONG_PASSWORD;
size_t BlockLength = ((m_passkey_len + 7) / 8) * 8;
unsigned char *t_passkey = new unsigned char[BlockLength];
LPCTSTR plaintext = LPCTSTR(passkey.c_str());
EncryptPassword(reinterpret_cast<const unsigned char *>(plaintext), t_passkey_len, t_passkey);
if (memcmp(t_passkey, m_passkey, BlockLength) == 0)
status = PWSfile::SUCCESS;
else
status = PWSfile::WRONG_PASSWORD;
delete[] t_passkey;
}
return status;
}
#define MRE_FS _T("\xbb")
int PWScore::ReadFile(const StringX &a_filename, const StringX &a_passkey,
const bool bValidate, const size_t iMAXCHARS,
CReport *pRpt)
{
PWS_LOGIT_ARGS("bValidate=%s; iMAXCHARS=%d; pRpt=%p",
bValidate ? _T("true") : _T("false"), iMAXCHARS,
pRpt);
int status;
st_ValidateResults st_vr;
std::vector<st_GroupTitleUser> vGTU_INVALID_UUID, vGTU_DUPLICATE_UUID;
// Clear any old expired password entries
m_ExpireCandidates.clear();
// Clear any old entry keyboard shortcuts
m_KBShortcutMap.clear();
PWSfile *in = PWSfile::MakePWSfile(a_filename, m_ReadFileVersion,
PWSfile::Read, status, m_pAsker, m_pReporter);
if (status != PWSfile::SUCCESS) {
delete in;
return status;
}
status = in->Open(a_passkey);
// in the old times we could open even 1.x files
// for compatibility reasons, we open them again, to see if this is really a "1.x" file
if ((m_ReadFileVersion == PWSfile::V20) && (status == PWSfile::WRONG_VERSION)) {
PWSfile::VERSION tmp_version; // only for getting compatible to "1.x" files
tmp_version = m_ReadFileVersion;
m_ReadFileVersion = PWSfile::V17;
//Closing previously opened file
in->Close();
in->SetCurVersion(PWSfile::V17);
status = in->Open(a_passkey);
if (status != PWSfile::SUCCESS) {
m_ReadFileVersion = tmp_version;
}
}
if (status != PWSfile::SUCCESS) {
delete in;
return status;
}
if (m_ReadFileVersion == PWSfile::UNKNOWN_VERSION) {
delete in;
return UNKNOWN_VERSION;
}
m_hdr = in->GetHeader();
m_OrigDisplayStatus = m_hdr.m_displaystatus; // for WasDisplayStatusChanged
m_RUEList = m_hdr.m_RUEList;
// Get pref string and tree display status & who saved when
// all possibly empty!
PWSprefs *prefs = PWSprefs::GetInstance();
prefs->Load(m_hdr.m_prefString);
// prepare handling of pre-2.0 DEFUSERCHR conversion
if (m_ReadFileVersion == PWSfile::V17) {
in->SetDefUsername(prefs->GetPref(PWSprefs::DefaultUsername).c_str());
m_hdr.m_nCurrentMajorVersion = PWSfile::V17;
m_hdr.m_nCurrentMinorVersion = 0;
} else {
// for 2.0 & later...
in->SetDefUsername(prefs->GetPref(PWSprefs::DefaultUsername).c_str());
}
ClearData(); //Before overwriting old data, but after opening the file...
SetChanged(false, false);
SetPassKey(a_passkey); // so user won't be prompted for saves
CItemData ci_temp;
StringX csMyPassword, cs_possibleUUID;
bool go = true;
bool limited = false;
PWSfileV3 *in3 = dynamic_cast<PWSfileV3 *>(in); // XXX cleanup
if (in3 != NULL) {
m_hashIters = in3->GetNHashIters();
m_MapFilters = in3->GetFilters();
m_MapPSWDPLC = in3->GetPasswordPolicies();
m_vEmptyGroups = in3->GetEmptyGroups();
}
if (pRpt != NULL) {
std::wstring cs_title;
LoadAString(cs_title, IDSC_RPTVALIDATE);
pRpt->StartReport(cs_title.c_str(), m_currfile.c_str());
}
size_t uimaxsize(0);
int numlarge(0);
do {
ci_temp.Clear(); // Rather than creating a new one each time.
status = in->ReadRecord(ci_temp);
switch (status) {
case PWSfile::FAILURE:
{
// Show a useful(?) error message - better than
// silently losing data (but not by much)
// Best if title intact. What to do if not?
if (m_pReporter != NULL) {
stringT cs_msg, cs_caption;
LoadAString(cs_caption, IDSC_READ_ERROR);
Format(cs_msg, IDSC_ENCODING_PROBLEM, ci_temp.GetTitle().c_str());
cs_msg = cs_caption + _S(": ") + cs_caption;
(*m_pReporter)(cs_msg);
}
}
// deliberate fall-through
case PWSfile::SUCCESS:
if (iMAXCHARS > 0 && ci_temp.GetSize() > iMAXCHARS) {
numlarge++;
uimaxsize = MAX(uimaxsize, ci_temp.GetSize());
}
/*
* If, for some reason, we're reading in an invalid UUID,
* we will change the UUID before adding it to the list.
*
* To date, we know that databases of format 0x0200 and 0x0300 have a UUID
* problem if records were duplicated. Databases of format 0x0100 did not
* have the duplicate function and it has been fixed in databases in format
* 0x0301 and so not an issue in V1 (0x0100) or V3.03 (0x0301) or later
*
* But a Null CUUID is invalid even if another application using core.lib
* does it and they could have got the version wrong - so fix it anyway
*/
if (ci_temp.GetUUID() == CUUID::NullUUID()) {
vGTU_INVALID_UUID.push_back(st_GroupTitleUser(ci_temp.GetGroup(),
ci_temp.GetTitle(), ci_temp.GetUser()));
st_vr.num_invalid_UUIDs++;
ci_temp.CreateUUID(); // replace invalid UUID
ci_temp.SetStatus(CItemData::ES_MODIFIED); // Show modified
} // UUID invalid
/*
* If, for some reason, we're reading in a UUID that we already have
* we will change the UUID, rather than overwrite an entry.
* This is to protect the user from possible bugs that break
* the uniqueness requirement of UUIDs.
*/
if (m_pwlist.find(ci_temp.GetUUID()) != m_pwlist.end()) {
vGTU_DUPLICATE_UUID.push_back(st_GroupTitleUser(ci_temp.GetGroup(),
ci_temp.GetTitle(), ci_temp.GetUser()));
st_vr.num_duplicate_UUIDs++;
ci_temp.CreateUUID(); // replace duplicated UUID
ci_temp.SetStatus(CItemData::ES_MODIFIED); // Show modified
} // UUID duplicate
if (ci_temp.IsPasswordPolicySet() && ci_temp.IsPolicyNameSet()) {
// Error can't have both - clear Password Policy Name
ci_temp.ClearField(CItemData::POLICYNAME);
}
if (ci_temp.IsPolicyNameSet()) {
const StringX sxPolicyName = ci_temp.GetPolicyName();
PSWDPolicyMapIter iter = m_MapPSWDPLC.find(sxPolicyName);
if (iter == m_MapPSWDPLC.end()) {
// Map name not present in database - clear it!
ci_temp.ClearField(CItemData::POLICYNAME);
} else {
// Increase use count
iter->second.usecount++;
}
}
if (in3 != NULL) {
int32 iKBShortcut;
ci_temp.GetKBShortcut(iKBShortcut);
// Entry can't have same HotKey as the Application
if (m_iAppHotKey == iKBShortcut) {
ci_temp.SetKBShortcut(0);
} else
if (iKBShortcut != 0) {
if (!ValidateKBShortcut(iKBShortcut)) {
m_KBShortcutMap.insert(KBShortcutMapPair(iKBShortcut, ci_temp.GetUUID()));
} else {
ci_temp.SetKBShortcut(0);
}
}
}
#ifdef DEMO
if (m_pwlist.size() < MAXDEMO) {
m_pwlist.insert(make_pair(CUUID(uuid), ci_temp));
} else {
limited = true;
}
#else
m_pwlist.insert(make_pair(ci_temp.GetUUID(), ci_temp));
#endif
time_t tttXTime;
ci_temp.GetXTime(tttXTime);
if (!limited && tttXTime != time_t(0)) {
ExpPWEntry ee(ci_temp);
m_ExpireCandidates.push_back(ee);
}
break;
case PWSfile::END_OF_FILE:
go = false;
break;
default:
break;
} // switch
} while (go);
ParseDependants();
m_nRecordsWithUnknownFields = in->GetNumRecordsWithUnknownFields();
in->GetUnknownHeaderFields(m_UHFL);
int closeStatus = in->Close(); // in V3 this checks integrity
#ifdef DEMO
if (closeStatus == PWSfile::SUCCESS && limited)
closeStatus = LIMIT_REACHED; // if integrity OK but LIMIT_REACHED, return latter
#endif
delete in;
// Write out error heading
if ((!vGTU_INVALID_UUID.empty() || !vGTU_DUPLICATE_UUID.empty()) &&
pRpt != NULL) {
stringT cs_Error;
pRpt->WriteLine();
LoadAString(cs_Error, IDSC_VALIDATE_ERRORS);
pRpt->WriteLine(cs_Error);
// Report invalid UUIDs
if (!vGTU_INVALID_UUID.empty()) {
std::sort(vGTU_INVALID_UUID.begin(), vGTU_INVALID_UUID.end(), GTUCompareV1);
pRpt->WriteLine();
LoadAString(cs_Error, IDSC_VALIDATE_BADUUID);
pRpt->WriteLine(cs_Error);
for (size_t iv = 0; iv < vGTU_INVALID_UUID.size(); iv++) {
st_GroupTitleUser &gtu = vGTU_INVALID_UUID[iv];
Format(cs_Error, IDSC_VALIDATE_ENTRY,
gtu.group.c_str(), gtu.title.c_str(), gtu.user.c_str(), _T(""));
pRpt->WriteLine(cs_Error);
}
}
// Report Duplicate UUIDs
if (!vGTU_DUPLICATE_UUID.empty()) {
std::sort(vGTU_DUPLICATE_UUID.begin(), vGTU_DUPLICATE_UUID.end(), GTUCompareV1);
pRpt->WriteLine();
LoadAString(cs_Error, IDSC_VALIDATE_DUPUUID);
pRpt->WriteLine(cs_Error);
for (size_t iv = 0; iv < vGTU_DUPLICATE_UUID.size(); iv++) {
st_GroupTitleUser &gtu = vGTU_DUPLICATE_UUID[iv];
Format(cs_Error, IDSC_VALIDATE_ENTRY,
gtu.group.c_str(), gtu.title.c_str(), gtu.user.c_str(), _T(""));
pRpt->WriteLine(cs_Error);
}
}
}
// Validate rest of things in the database (excluding duplicate UUIDs fixed above
// as needed for m_pwlist - map uses UUID as its key)
bool bValidateRC = !vGTU_INVALID_UUID.empty() || !vGTU_DUPLICATE_UUID.empty();
// Only do the rest if user hasn't explicitly disabled the checks
// NOTE: When a "other" core is involved (Compare, Merge etc.), we NEVER validate
// the "other" core.
if (bValidate)
bValidateRC = Validate(iMAXCHARS, true, pRpt, st_vr);
if (pRpt != NULL)
pRpt->EndReport();
SetDBChanged(bValidateRC);
// Setup file signature for checking file integrity upon backup.
// Goal is to prevent overwriting a good backup with a corrupt file.
if (a_filename == m_currfile) {
delete m_pFileSig;
m_pFileSig = new PWSFileSig(a_filename.c_str());
}
// Make return code negative if validation errors
if (closeStatus == SUCCESS && pRpt != NULL && bValidateRC)
closeStatus = OK_WITH_VALIDATION_ERRORS;
return closeStatus;
}
static void ManageIncBackupFiles(const stringT &cs_filenamebase,
size_t maxnumincbackups, stringT &cs_newname)
{
/**
* make sure we've no more than maxnumincbackups backup files,
* and return the base name of the next backup file
* (sans the suffix, which will be added by caller)
*
* The current solution breaks when maxnumincbackups >= 999.
* Best solution is to delete by modification time,
* but that requires a bit too much for the cost/benefit.
* So for now we're "good enough" - limiting maxnumincbackups to <= 998
*/
if (maxnumincbackups >= 999) {
pws_os::Trace(_T("Maxnumincbackups: truncating maxnumincbackups to 998"));
maxnumincbackups = 998;
}
stringT cs_filenamemask(cs_filenamebase);
vector<stringT> files;
vector<int> file_nums;
cs_filenamemask += _T("_???.ibak");
pws_os::FindFiles(cs_filenamemask, files);
for (vector<stringT>::iterator iter = files.begin();
iter != files.end(); iter++) {
stringT ibak_number_str = iter->substr(iter->length() - 8, 3);
if (ibak_number_str.find_first_not_of(_T("0123456789")) != stringT::npos)
continue;
istringstreamT is(ibak_number_str);
int n;
is >> n;
file_nums.push_back(n);
}
if (file_nums.empty()) {
cs_newname = cs_filenamebase + _T("_001");
return;
}
sort(file_nums.begin(), file_nums.end());
// nnn is the number of the file in the returned value: cs_filebasename_nnn
int nnn = file_nums.back();
nnn++;
if (nnn > 999) {
// as long as there's a _999 file, we set n starting from 001
nnn = 1;
size_t x = file_nums.size() - maxnumincbackups;
while (file_nums[x++] == nnn && x < file_nums.size())
nnn++;
// Now we need to determine who to delete.
int next = 999 - (maxnumincbackups - nnn);
int m = 1;
for (x = 0; x < file_nums.size(); x++)
if (file_nums[x] < next)
file_nums[x] = next <= 999 ? next++ : m++;
}
Format(cs_newname, _T("%s_%03d"), cs_filenamebase.c_str(), nnn);
int i = 0;
size_t num_found = file_nums.size();
stringT excess_file;
while (num_found >= maxnumincbackups) {
nnn = file_nums[i];
Format(excess_file, _T("%s_%03d.ibak"), cs_filenamebase.c_str(), nnn);
i++;
num_found--;
if (!pws_os::DeleteAFile(excess_file)) {
pws_os::Trace(_T("DeleteFile(%s) failed"), excess_file.c_str());
continue;
}
}
}
bool PWScore::BackupCurFile(int maxNumIncBackups, int backupSuffix,
const stringT &userBackupPrefix,
const stringT &userBackupDir, stringT &bu_fname)
{
stringT cs_temp;
const stringT path(m_currfile.c_str());
stringT drv, dir, name, ext;
// Check if the file we're about to backup is unchanged since
// we opened it, to avoid overwriting a good file with a bad one
if (m_pFileSig != NULL) {
PWSFileSig curSig(m_currfile.c_str());
bool passed = (curSig == *m_pFileSig);
if (!passed) // XXX yell scream & shout
return false;
}
pws_os::splitpath(path, drv, dir, name, ext);
// Get location for intermediate backup
if (userBackupDir.empty()) {
// directory same as database's
// Get directory containing database
cs_temp = drv + dir;
// (in Windows, need to verify for non-Windows)
// splitpath directory ends with a '/', therefore do not need:
// cs_temp += pws_os::PathSeparator;
} else {
// User specified somewhere else
cs_temp = userBackupDir;
// Ensure ends with path separator
if (userBackupDir[userBackupDir.length() - 1] != pws_os::PathSeparator)
cs_temp += pws_os::PathSeparator;
}
// generate prefix of intermediate backup file name
if (userBackupPrefix.empty()) {
cs_temp += name;
} else {
cs_temp += userBackupPrefix;
}
// Add on suffix
switch (backupSuffix) { // case values from order in listbox.
case 1: // YYYYMMDD_HHMMSS suffix
{
time_t now;
time(&now);
StringX cs_datetime = PWSUtil::ConvertToDateTimeString(now,
PWSUtil::TMC_EXPORT_IMPORT);
cs_temp += _T("_");
StringX nf = cs_temp.c_str() +
cs_datetime.substr( 0, 4) + // YYYY
cs_datetime.substr( 5, 2) + // MM
cs_datetime.substr( 8, 2) + // DD
StringX(_T("_")) +
cs_datetime.substr(11, 2) + // HH
cs_datetime.substr(14, 2) + // MM
cs_datetime.substr(17, 2); // SS
bu_fname = nf.c_str();
break;
}
case 2: // _nnn suffix
ManageIncBackupFiles(cs_temp, maxNumIncBackups, bu_fname);
break;
case 0: // no suffix
default:
bu_fname = cs_temp;
break;
}
bu_fname += _T(".ibak");
// Current file becomes backup
// Directories along the specified backup path are created as needed
return pws_os::RenameFile(m_currfile.c_str(), bu_fname);
}
void PWScore::ChangePasskey(const StringX &newPasskey)
{
SetPassKey(newPasskey);
SetDBChanged(true);
}
// functor object type for find_if:
struct FieldsMatch {
bool operator()(pair<CUUID, CItemData> p) {
const CItemData &item = p.second;
return (m_group == item.GetGroup() &&
m_title == item.GetTitle() &&
m_user == item.GetUser());
}
FieldsMatch(const StringX &a_group, const StringX &a_title,
const StringX &a_user) :
m_group(a_group), m_title(a_title), m_user(a_user) {}
private:
FieldsMatch& operator=(const FieldsMatch&); // Do not implement
const StringX &m_group;
const StringX &m_title;
const StringX &m_user;
};
// Finds stuff based on group, title & user fields only
ItemListIter PWScore::Find(const StringX &a_group,const StringX &a_title,
const StringX &a_user)
{
FieldsMatch fields_match(a_group, a_title, a_user);
ItemListIter retval = find_if(m_pwlist.begin(), m_pwlist.end(),
fields_match);
return retval;
}
struct TitleMatch {
bool operator()(pair<CUUID, CItemData> p) {
const CItemData &item = p.second;
return (m_title == item.GetTitle());
}
TitleMatch(const StringX &a_title) :
m_title(a_title) {}
private:
TitleMatch& operator=(const TitleMatch&); // Do not implement
const StringX &m_title;
};
ItemListIter PWScore::GetUniqueBase(const StringX &a_title, bool &bMultiple)
{
ItemListIter retval(m_pwlist.end());
int num(0);
TitleMatch TitleMatch(a_title);
ItemListIter found(m_pwlist.begin());
do {
found = find_if(found, m_pwlist.end(), TitleMatch);
if (found != m_pwlist.end()) {
num++;
if (num == 1) {
// Save first
retval = found;
} else {
// More than 1, set to end()
retval = m_pwlist.end();
break;
}
found++;
} else
break;
} while (found != m_pwlist.end());
// It is 1 if only 1, but 0 if none & 2 if more than 1 (we just stopped at the second)
bMultiple = (num > 1);
return retval;
}
struct GroupTitle_TitleUserMatch {
bool operator()(pair<CUUID, CItemData> p) {
const CItemData &item = p.second;
return ((m_gt == item.GetGroup() && m_tu == item.GetTitle()) ||
(m_gt == item.GetTitle() && m_tu == item.GetUser()));
}
GroupTitle_TitleUserMatch(const StringX &a_grouptitle,
const StringX &a_titleuser) :
m_gt(a_grouptitle), m_tu(a_titleuser) {}
private:
GroupTitle_TitleUserMatch& operator=(const GroupTitle_TitleUserMatch&); // Do not implement
const StringX &m_gt;
const StringX &m_tu;
};
ItemListIter PWScore::GetUniqueBase(const StringX &grouptitle,
const StringX &titleuser, bool &bMultiple)
{
ItemListIter retval(m_pwlist.end());
int num(0);
GroupTitle_TitleUserMatch GroupTitle_TitleUserMatch(grouptitle, titleuser);
ItemListIter found(m_pwlist.begin());
do {
found = find_if(found, m_pwlist.end(), GroupTitle_TitleUserMatch);
if (found != m_pwlist.end()) {
num++;
if (num == 1) {
// Save first
retval = found;
} else {
// More than 1, set to end()
retval = m_pwlist.end();
break;
}
found++;
} else
break;
} while (found != m_pwlist.end());
// It is 1 if only 1, but 0 if none & 2 if more than 1 (we just stopped at the second)
bMultiple = (num > 1);
return retval;
}
void PWScore::EncryptPassword(const unsigned char *plaintext, size_t len,
unsigned char *ciphertext) const
{
// Chicken out of an interface change, or just a sanity check?
// Maybe both...
ASSERT(len > 0);
unsigned int ulen = static_cast<unsigned int>(len);
const unsigned int BS = TwoFish::BLOCKSIZE;
if (!pws_os::mcryptUnprotect(m_session_key, sizeof(m_session_key))) {
pws_os::Trace(_T("pws_os::mcryptUnprotect failed"));
}
TwoFish tf(m_session_key, sizeof(m_session_key));
if (!pws_os::mcryptProtect(m_session_key, sizeof(m_session_key))) {
pws_os::Trace(_T("pws_os::mcryptProtect failed"));
}
unsigned int BlockLength = ((ulen + (BS - 1)) / BS) * BS;
unsigned char curblock[BS];
for (unsigned int x = 0; x < BlockLength; x += BS) {
unsigned int i;
if ((ulen == 0) ||
((ulen % BS != 0) && (ulen - x < BS))) {
//This is for an uneven last block
memset(curblock, 0, BS);
for (i = 0; i < len % BS; i++)
curblock[i] = plaintext[x + i];
} else
for (i = 0; i < BS; i++) {
curblock[i] = plaintext[x + i];
}
tf.Encrypt(curblock, curblock);
memcpy(ciphertext + x, curblock, BS);
}
trashMemory(curblock, sizeof(curblock));
}
void PWScore::SetPassKey(const StringX &new_passkey)
{
// Only used when opening files and for new files
const unsigned int BS = TwoFish::BLOCKSIZE;
// if changing, clear old
if (m_passkey_len > 0) {
trashMemory(m_passkey, ((m_passkey_len + (BS -1)) / BS) * BS);
delete[] m_passkey;
}
m_passkey_len = new_passkey.length() * sizeof(TCHAR);
size_t BlockLength = ((m_passkey_len + (BS - 1)) / BS) * BS;
m_passkey = new unsigned char[BlockLength];
LPCTSTR plaintext = LPCTSTR(new_passkey.c_str());
EncryptPassword(reinterpret_cast<const unsigned char *>(plaintext), m_passkey_len, m_passkey);
}
StringX PWScore::GetPassKey() const
{
StringX retval(_T(""));
if (m_passkey_len > 0) {
const unsigned int BS = TwoFish::BLOCKSIZE;
size_t BlockLength = ((m_passkey_len + (BS - 1)) / BS) * BS;
if (!pws_os::mcryptUnprotect(m_session_key, sizeof(m_session_key))) {
pws_os::Trace(_T("pws_os::mcryptUnprotect failed"));
}
TwoFish tf(m_session_key, sizeof(m_session_key));
if (!pws_os::mcryptProtect(m_session_key, sizeof(m_session_key))) {
pws_os::Trace(_T("pws_os::mcryptProtect failed"));
}
unsigned char curblock[BS];
for (unsigned int x = 0; x < BlockLength; x += BS) {
unsigned int i;
for (i = 0; i < BS; i++) {
curblock[i] = m_passkey[x + i];
}
tf.Decrypt(curblock, curblock);
for (i = 0; i < BS; i += sizeof(TCHAR)) {
if (x + i < m_passkey_len) {
retval += *(reinterpret_cast<TCHAR*>(curblock + i));
}
}
}
trashMemory(curblock, sizeof(curblock));
}
return retval;
}
void PWScore::SetDisplayStatus(const vector<bool> &s)
{
PWS_LOGIT;
// DON'T set m_bDBChanged!
// Application should use WasDisplayStatusChanged()
// to determine if state has changed.
// This allows app to silently save without nagging user
m_hdr.m_displaystatus = s;
}
const vector<bool> &PWScore::GetDisplayStatus() const
{
PWS_LOGIT;
return m_hdr.m_displaystatus;
}
bool PWScore::WasDisplayStatusChanged() const
{
// m_OrigDisplayStatus is set while reading file.
// m_hdr.m_displaystatus may be changed via SetDisplayStatus
return m_hdr.m_displaystatus != m_OrigDisplayStatus;
}
// GetUniqueGroups - returns an array of all group names, with no duplicates.
void PWScore::GetUniqueGroups(vector<stringT> &vUniqueGroups) const
{
// use the fact that set eliminates dups for us
set<stringT> setGroups;
ItemListConstIter iter;
for (iter = m_pwlist.begin(); iter != m_pwlist.end(); iter++ ) {
const CItemData &ci = iter->second;
setGroups.insert(ci.GetGroup().c_str());
}
vUniqueGroups.clear();
// copy unique results from set to caller's vector
copy(setGroups.begin(), setGroups.end(), back_inserter(vUniqueGroups));
}
// GetPolicyNames - returns an array of all password policy names
// They are in sort order as a map is always sorted by its key
void PWScore::GetPolicyNames(vector<stringT> &vNames) const
{
vNames.clear();
PSWDPolicyMapCIter citer;
for (citer = m_MapPSWDPLC.begin(); citer != m_MapPSWDPLC.end(); citer++ ) {
vNames.push_back(citer->first.c_str());
}
}
bool PWScore::GetPolicyFromName(const StringX &sxPolicyName, PWPolicy &st_pp) const
{
// - An empty policy name is never valid.
ASSERT(!sxPolicyName.empty());
// - The default policy is not stored in the map (but read from preferences)
StringX sxDefPolicyStr;
LoadAString(sxDefPolicyStr, IDSC_DEFAULT_POLICY);
if (sxPolicyName == sxDefPolicyStr) {
st_pp = PWSprefs::GetInstance()->GetDefaultPolicy();
return true;
} else {
PSWDPolicyMapCIter iter = m_MapPSWDPLC.find(sxPolicyName);
if (iter != m_MapPSWDPLC.end()) {
st_pp = iter->second;
return true;
} else {
st_pp.Empty();
return false;
}
}
}
class PolicyNameMatch
{
public:
PolicyNameMatch(StringX &policyname) : m_policyname(policyname) {}
bool operator()(const std::pair<StringX, StringX> &pr)
{
return (m_policyname == pr.second);
}
private:
StringX m_policyname;
};
void PWScore::MakePolicyUnique(std::map<StringX, StringX> &mapRenamedPolicies,
StringX &sxPolicyName, const StringX &sxDateTime,
const UINT IDS_MESSAGE)
{
// 'mapRenamedPolicies' contains those policies already renamed. It will be
// updated with any new name generated.
// The map key is old name and the value is new name
// 'sxPolicyName' is the current name and will be returned as the new name
std::map<StringX, StringX>::iterator iter;
// Have we done it already?
iter = mapRenamedPolicies.find(sxPolicyName);
if (iter != mapRenamedPolicies.end()) {
// Yes - give them the new name
sxPolicyName = iter->second;
return;
}
StringX sxNewPolicyName;
Format(sxNewPolicyName, IDS_MESSAGE, sxPolicyName.c_str(), sxDateTime.c_str());
// Verify new policy name not already in this database
if (m_MapPSWDPLC.find(sxNewPolicyName) != m_MapPSWDPLC.end())
ASSERT(0); // Already there - how can this be????
// Now try to see if we have added this
if (std::find_if(mapRenamedPolicies.begin(), mapRenamedPolicies.end(),
PolicyNameMatch(sxNewPolicyName)) == mapRenamedPolicies.end()) {
// No. OK we have got a new unique policy name - save it
mapRenamedPolicies[sxPolicyName] = sxNewPolicyName;
}
sxPolicyName = sxNewPolicyName;
return;
}
// For Validate only
struct st_GroupTitleUser2 {
StringX group;
StringX title;
StringX user;
StringX newtitle;
st_GroupTitleUser2() {}
st_GroupTitleUser2(const StringX &g, const StringX &t, const StringX &u,
const StringX &n)
: group(g), title(t), user(u), newtitle(n) {}
st_GroupTitleUser2 &operator=(const st_GroupTitleUser2 &that) {
if (this != &that) {
group = that.group; title = that.title; user = that.user;
newtitle = that.newtitle;
}
return *this;
}
};
static bool GTUCompareV2(const st_GroupTitleUser2 &gtu1, const st_GroupTitleUser2 &gtu2)
{
if (gtu1.group != gtu2.group)
return gtu1.group.compare(gtu2.group) < 0;
else if (gtu1.title != gtu2.title)
return gtu1.title.compare(gtu2.title) < 0;
else if (gtu1.user != gtu2.user)
return gtu1.user.compare(gtu2.user) < 0;
else
return gtu1.newtitle.compare(gtu2.newtitle) < 0;
}
void PWScore::ParseDependants()
{
UUIDVector Possible_Aliases, Possible_Shortcuts;
for (ItemListIter iter = m_pwlist.begin(); iter != m_pwlist.end(); iter++) {
const CItemData &ci = iter->second;
// Get all possible Aliases/Shortcuts for future checking if base entries exist
const StringX csMyPassword = ci.GetPassword();
if (csMyPassword.length() == 36) { // look for "[[uuid]]" or "[~uuid~]"
StringX cs_possibleUUID = csMyPassword.substr(2, 32); // try to extract uuid
ToLower(cs_possibleUUID);
if (((csMyPassword.substr(0,2) == _T("[[") &&
csMyPassword.substr(csMyPassword.length() - 2) == _T("]]")) ||
(csMyPassword.substr(0, 2) == _T("[~") &&
csMyPassword.substr(csMyPassword.length() - 2) == _T("~]"))) &&
cs_possibleUUID.find_first_not_of(_T("0123456789abcdef")) == StringX::npos) {
CUUID buuid(cs_possibleUUID.c_str());
if (csMyPassword.substr(0, 2) == _T("[[")) {
m_alias2base_map[ci.GetUUID()] = buuid;
Possible_Aliases.push_back(ci.GetUUID());
} else {
m_shortcut2base_map[ci.GetUUID()] = buuid;
Possible_Shortcuts.push_back(ci.GetUUID());
}
}
}
} // iter over m_pwlist
if (!Possible_Aliases.empty()) {
DoAddDependentEntries(Possible_Aliases, NULL, CItemData::ET_ALIAS, CItemData::UUID);
}
if (!Possible_Shortcuts.empty()) {
DoAddDependentEntries(Possible_Shortcuts, NULL, CItemData::ET_SHORTCUT, CItemData::UUID);
}
}
bool PWScore::Validate(const size_t iMAXCHARS, const bool bInReadfile,
CReport *pRpt, st_ValidateResults &st_vr)
{
/*
1. Check PWH is valid
2. Check that the 2 mandatory fields are present (Title & Password)
3. Check group/title/user must be unique.
4. Check that no text field has more than iMAXCHARS, that can displayed
in the GUI's text control.
Notes:
1. m_pwlist is implemented as a map keyed on UUIDs, each entry is
guaranteed to have a unique uuid. The uniqueness invariant
should be enforced elsewhere.
(ReadFile during Open and Import have already ensured UUIDs are unique
and valid)
2. If bInReadfile is true, the validation is being performed during normal
initial file opening.
*/
PWS_LOGIT_ARGS("iMAXCHARS=%d; bInReadfile=%s; pRpt=%p", iMAXCHARS,
bInReadfile ? _T("true") : _T("false"), pRpt);
int n = -1;
unsigned int uimaxsize(0);
MultiCommands *pmulticmds(NULL);
// We do not use the Command infrastructure with Undo/Redo when reading in
// the database
if (!bInReadfile)
pmulticmds = MultiCommands::Create(this);
stringT cs_Error;
pws_os::Trace(_T("Start validation\n"));
StringX sxMissingPassword;
LoadAString(sxMissingPassword, IDSC_MISSINGPASSWORD);
st_GroupTitleUser st_gtu;
GTUSet setGTU;
GTUSetPair pr_gtu;
std::vector<st_GroupTitleUser> vGTU_UUID, vGTU_EmptyPassword, vGTU_PWH, vGTU_TEXT,
vGTU_ALIASES, vGTU_SHORTCUTS;
std::vector<st_GroupTitleUser2> vGTU_NONUNIQUE, vGTU_EmptyTitle;
ItemListIter iter;
for (iter = m_pwlist.begin(); iter != m_pwlist.end(); iter++) {
CItemData &ci = iter->second;
CItemData fixedItem(ci);
bool bFixed(false);
int flags = CItemData::VF_OK;
n++;
// Fix GTU uniqueness - can't do this in a CItemData member function as it causes
// circular includes:
// "ItemData.h" would need to include "coredefs.h", which needs to include "ItemData.h"!
StringX sxgroup(ci.GetGroup()), sxtitle(ci.GetTitle()), sxuser(ci.GetUser());
st_gtu.group = sxgroup;
st_gtu.title = sxtitle;
st_gtu.user = sxuser;
if (sxtitle.empty()) {
// This field is mandatory!
// Change it and insert into a std::set which guarantees uniqueness
int i = 0;
StringX s_copy, sxnewtitle(sxtitle);
do {
i++;
Format(sxnewtitle, IDSC_MISSINGTITLE, i);
st_gtu.title = sxnewtitle;
pr_gtu = setGTU.insert(st_gtu);
} while (!pr_gtu.second);
fixedItem.SetTitle(sxnewtitle);
bFixed = true;
vGTU_EmptyTitle.push_back(st_GroupTitleUser2(sxgroup, sxtitle, sxuser, sxnewtitle));
st_vr.num_empty_titles++;
flags |= CItemData::VF_EMPTY_TITLE;
sxtitle = sxnewtitle;
} else {
// Title was not empty
// Insert into a std::set which guarantees uniqueness
pr_gtu = setGTU.insert(st_gtu);
if (!pr_gtu.second) {
// Already have this group/title/user entry
int i = 0;
StringX s_copy, sxnewtitle(sxtitle);
do {
i++;
Format(s_copy, IDSC_DUPLICATENUMBER, i);
sxnewtitle = sxtitle + s_copy;
st_gtu.title = sxnewtitle;
pr_gtu = setGTU.insert(st_gtu);
} while (!pr_gtu.second);
fixedItem.SetTitle(sxnewtitle);
bFixed = true;
vGTU_NONUNIQUE.push_back(st_GroupTitleUser2(sxgroup, sxtitle, sxuser, sxnewtitle));
st_vr.num_duplicate_GTU_fixed++;
flags |= CItemData::VF_NOT_UNIQUE_GTU;
sxtitle = sxnewtitle;
}
}
// Test if Password is present as it is mandatory! was fixed
if (ci.GetPassword().empty()) {
fixedItem.SetPassword(sxMissingPassword);
bFixed = true;
vGTU_EmptyPassword.push_back(st_GroupTitleUser(sxgroup, sxtitle, sxuser));
st_vr.num_empty_passwords++;
flags |= CItemData::VF_EMPTY_PASSWORD;
}
// Test if Password History was fixed
if (!fixedItem.ValidatePWHistory()) {
bFixed = true;
vGTU_PWH.push_back(st_GroupTitleUser(sxgroup, sxtitle, sxuser));
st_vr.num_PWH_fixed++;
flags |= CItemData::VF_BAD_PSWDHISTORY;
}
// Note excessively sized text fields
if (iMAXCHARS > 0) {
bool bEntryHasBigField(false);
for (unsigned char uc = static_cast<unsigned char>(CItemData::GROUP);
uc < static_cast<unsigned char>(CItemData::LAST); uc++) {
if (CItemData::IsTextField(uc)) {
StringX sxvalue = ci.GetFieldValue(static_cast<CItemData::FieldType>(uc));
if (sxvalue.length() > iMAXCHARS) {
bEntryHasBigField = true;
// We don't truncate the field, but if we did, then the the code would be:
// fixedItem.SetFieldValue((CItemData::FieldType)uc, sxvalue.substr(0, iMAXCHARS))
break;
}
}
}
if (bEntryHasBigField) {
uimaxsize = MAX(uimaxsize, ci.GetSize());
vGTU_TEXT.push_back(st_GroupTitleUser(sxgroup, sxtitle, sxuser));
st_vr.num_excessivetxt_found++;
}
}
if (bFixed) {
// Mark as modified
fixedItem.SetStatus(CItemData::ES_MODIFIED);
if (bInReadfile) {
// We must fix entry without using the Command mechanism and Undo/Redo during
// initial read of the file
m_pwlist[fixedItem.GetUUID()] = fixedItem;
} else {
// Otherwise, we must do it via the normal Command mechanism
Command *pcmd = EditEntryCommand::Create(this, ci, fixedItem);
pmulticmds->Add(pcmd);
}
}
} // iteration over m_pwlist
#if 0 // XXX We've separated alias/shortcut processing from Validate - reconsider this!
// See if we have any entries with passwords that imply they are an alias
// but there is no equivalent base entry
for (size_t ipa = 0; ipa < Possible_Aliases.size(); ipa++) {
if (m_pwlist.find(m_alias2base_map[Possible_Aliases[ipa]]) == m_pwlist.end()) {
ItemListIter iter = m_pwlist.find(Possible_Aliases[ipa]);
if (iter != m_pwlist.end()) {
StringX sxgroup = iter->second.GetGroup();
StringX sxtitle = iter->second.GetTitle();
StringX sxuser = iter->second.GetUser();
vGTU_ALIASES.push_back(st_GroupTitleUser(sxgroup, sxtitle, sxuser));
}
st_vr.num_alias_warnings++;
}
}
// See if we have any entries with passwords that imply they are a shortcut
// but there is no equivalent base entry
for (size_t ips = 0; ips < Possible_Shortcuts.size(); ips++) {
if (m_pwlist.find(m_shortcut2base_map[Possible_Shortcuts[ips]]) == m_pwlist.end()) {
ItemListIter iter = m_pwlist.find(Possible_Shortcuts[ips]);
if (iter != m_pwlist.end()) {
StringX sxgroup = iter->second.GetGroup();
StringX sxtitle = iter->second.GetTitle();
StringX sxuser = iter->second.GetUser();
vGTU_SHORTCUTS.push_back(st_GroupTitleUser(sxgroup, sxtitle, sxuser));
}
st_vr.num_shortcuts_warnings++;
}
}
#endif
if (st_vr.TotalIssues() != 0 && pRpt != NULL) {
if ((st_vr.num_invalid_UUIDs == 0 && st_vr.num_duplicate_UUIDs == 0)) {
// As both zero, we didn't put error header in report - so do it now
pRpt->WriteLine();
LoadAString(cs_Error, IDSC_VALIDATE_ERRORS);
pRpt->WriteLine(cs_Error);
}
if (!vGTU_EmptyTitle.empty()) {
std::sort(vGTU_EmptyTitle.begin(), vGTU_EmptyTitle.end(), GTUCompareV2);
pRpt->WriteLine();
LoadAString(cs_Error, IDSC_VALIDATE_EMPTYTITLE);
pRpt->WriteLine(cs_Error);
for (size_t iv = 0; iv < vGTU_EmptyTitle.size(); iv++) {
st_GroupTitleUser2 &gtu2 = vGTU_EmptyTitle[iv];
stringT cs_newtitle;
Format(cs_newtitle, IDSC_VALIDATE_ENTRY2, gtu2.newtitle.c_str());
Format(cs_Error, IDSC_VALIDATE_ENTRY,
gtu2.group.c_str(), gtu2.title.c_str(), gtu2.user.c_str(), cs_newtitle.c_str());
pRpt->WriteLine(cs_Error);
}
}
if (!vGTU_EmptyPassword.empty()) {
std::sort(vGTU_EmptyPassword.begin(), vGTU_EmptyPassword.end(), GTUCompareV1);
pRpt->WriteLine();
Format(cs_Error, IDSC_VALIDATE_EMPTYPSWD, sxMissingPassword.c_str());
pRpt->WriteLine(cs_Error);
for (size_t iv = 0; iv < vGTU_EmptyPassword.size(); iv++) {
st_GroupTitleUser &gtu = vGTU_EmptyPassword[iv];
Format(cs_Error, IDSC_VALIDATE_ENTRY,
gtu.group.c_str(), gtu.title.c_str(), gtu.user.c_str(), _T(""));
pRpt->WriteLine(cs_Error);
}
}
if (!vGTU_NONUNIQUE.empty()) {
std::sort(vGTU_NONUNIQUE.begin(), vGTU_NONUNIQUE.end(), GTUCompareV2);
pRpt->WriteLine();
LoadAString(cs_Error, IDSC_VALIDATE_DUPLICATES);
pRpt->WriteLine(cs_Error);
for (size_t iv = 0; iv < vGTU_NONUNIQUE.size(); iv++) {
st_GroupTitleUser2 &gtu2 = vGTU_NONUNIQUE[iv];
stringT cs_newtitle;
Format(cs_newtitle, IDSC_VALIDATE_ENTRY2, gtu2.newtitle.c_str());
Format(cs_Error, IDSC_VALIDATE_ENTRY,
gtu2.group.c_str(), gtu2.title.c_str(), gtu2.user.c_str(), cs_newtitle.c_str());
pRpt->WriteLine(cs_Error);
}
}
if (!vGTU_UUID.empty()) {
std::sort(vGTU_UUID.begin(), vGTU_UUID.end(), GTUCompareV1);
pRpt->WriteLine();
LoadAString(cs_Error, IDSC_VALIDATE_BADUUID);
pRpt->WriteLine(cs_Error);
for (size_t iv = 0; iv < vGTU_UUID.size(); iv++) {
st_GroupTitleUser &gtu = vGTU_UUID[iv];
Format(cs_Error, IDSC_VALIDATE_ENTRY,
gtu.group.c_str(), gtu.title.c_str(), gtu.user.c_str(), _T(""));
pRpt->WriteLine(cs_Error);
}
}
if (!vGTU_PWH.empty()) {
std::sort(vGTU_PWH.begin(), vGTU_PWH.end(), GTUCompareV1);
pRpt->WriteLine();
LoadAString(cs_Error, IDSC_VALIDATE_PWH);
pRpt->WriteLine(cs_Error);
for (size_t iv = 0; iv < vGTU_PWH.size(); iv++) {
st_GroupTitleUser &gtu = vGTU_PWH[iv];
Format(cs_Error, IDSC_VALIDATE_ENTRY,
gtu.group.c_str(), gtu.title.c_str(), gtu.user.c_str(), _T(""));
pRpt->WriteLine(cs_Error);
}
}
if ((!vGTU_ALIASES.empty() || !vGTU_SHORTCUTS.empty() || !vGTU_TEXT.empty()) &&
pRpt != NULL) {
// We have warnings
pRpt->WriteLine();
LoadAString(cs_Error, IDSC_VALIDATE_WARNINGS);
pRpt->WriteLine(cs_Error);
}
if (!vGTU_ALIASES.empty()) {
std::sort(vGTU_ALIASES.begin(), vGTU_ALIASES.end(), GTUCompareV1);
pRpt->WriteLine();
stringT sxAlias;
LoadAString(sxAlias, IDSC_FALIAS);
Format(cs_Error, IDSC_VALIDATE_DEPS, sxAlias.c_str());
pRpt->WriteLine(cs_Error);
for (size_t iv = 0; iv < vGTU_ALIASES.size(); iv++) {
st_GroupTitleUser &gtu = vGTU_ALIASES[iv];
Format(cs_Error, IDSC_VALIDATE_ENTRY,
gtu.group.c_str(), gtu.title.c_str(), gtu.user.c_str(), _T(""));
pRpt->WriteLine(cs_Error);
}
}
if (!vGTU_SHORTCUTS.empty()) {
std::sort(vGTU_SHORTCUTS.begin(), vGTU_SHORTCUTS.end(), GTUCompareV1);
pRpt->WriteLine();
stringT sxShortcut;
LoadAString(sxShortcut, IDSC_FSHORTCUT);
Format(cs_Error, IDSC_VALIDATE_DEPS, sxShortcut.c_str());
pRpt->WriteLine(cs_Error);
for (size_t iv = 0; iv < vGTU_SHORTCUTS.size(); iv++) {
st_GroupTitleUser &gtu = vGTU_SHORTCUTS[iv];
Format(cs_Error, IDSC_VALIDATE_ENTRY,
gtu.group.c_str(), gtu.title.c_str(), gtu.user.c_str(), _T(""));
pRpt->WriteLine(cs_Error);
}
}
if (!vGTU_TEXT.empty()) {
std::sort(vGTU_TEXT.begin(), vGTU_TEXT.end(), GTUCompareV1);
pRpt->WriteLine();
int units(0);
uimaxsize >>= 10; // make bytes -> KB
if (uimaxsize > 999) {
uimaxsize >>= 10; // make KB -> MB
units++;
}
Format(cs_Error, IDSC_VALIDATE_TEXT, iMAXCHARS, uimaxsize,
units == 0 ? _T("KB") : _T("MB"));
pRpt->WriteLine(cs_Error);
for (size_t iv = 0; iv < vGTU_TEXT.size(); iv++) {
st_GroupTitleUser &gtu = vGTU_TEXT[iv];
Format(cs_Error, IDSC_VALIDATE_ENTRY,
gtu.group.c_str(), gtu.title.c_str(), gtu.user.c_str(), _T(""));
pRpt->WriteLine(cs_Error);
}
}
}
pws_os::Trace(_T("End validation. %d entries processed\n"), n + 1);
m_bUniqueGTUValidated = true;
if (st_vr.TotalIssues() > 0) {
SetDBChanged(true);
return true;
} else {
return false;
}
}
bool PWScore::ValidateKBShortcut(int32 &iKBShortcut)
{
// Verify Entry Keyboard shortcut is valid
// Note: to support cross-platforms, can't use "Virtual Key Codes"
// as they differ between Windows, Linux & Mac.
// However, the alphanumeric ASCII values are common
// and so entry keyboard shortcuts are restricted to these values.
if (iKBShortcut != 0) {
static const TCHAR *tcValidKeys =
_T("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
static const WORD wValidModifiers = 0xffff ^ (
PWS_HOTKEYF_ALT | PWS_HOTKEYF_CONTROL |
PWS_HOTKEYF_SHIFT | PWS_HOTKEYF_EXT |
PWS_HOTKEYF_META | PWS_HOTKEYF_WIN |
PWS_HOTKEYF_CMD);
WORD wVirtualKeyCode = iKBShortcut & 0xff;
WORD wPWSModifiers = iKBShortcut >> 16;
// If there are too many bits in the Modifiers or
// Not a valid ASCII character (0-9, A-Z) - remove
if ((wValidModifiers & wPWSModifiers) ||
_tcschr(tcValidKeys, wVirtualKeyCode) == NULL) {
// Remove Entry Keyboard Shortcut
iKBShortcut = 0;
return true; // Changed
}
}
return false; // Unchanged
}
StringX PWScore::GetUniqueTitle(const StringX &group, const StringX &title,
const StringX &user, const int IDS_MESSAGE)
{
StringX new_title(title);
if (Find(group, title, user) != m_pwlist.end()) {
// Find a unique "Title"
ItemListConstIter listpos;
int i = 0;
StringX s_copy;
do {
i++;
Format(s_copy, IDS_MESSAGE, i);
new_title = title + s_copy;
listpos = Find(group, new_title, user);
} while (listpos != m_pwlist.end());
}
return new_title;
}
bool PWScore::InitialiseGTU(GTUSet &setGTU)
{
// Populate the set of all group/title/user entries
GTUSetPair pr_gtu;
ItemListConstIter citer;
setGTU.clear();
for (citer = m_pwlist.begin(); citer != m_pwlist.end(); citer++) {
const CItemData &ci = citer->second;
pr_gtu = setGTU.insert(st_GroupTitleUser(ci.GetGroup(), ci.GetTitle(), ci.GetUser()));
if (!pr_gtu.second) {
// Could happen if merging or synching a bad database!
setGTU.clear();
return false;
}
}
m_bUniqueGTUValidated = true;
return true;
}
bool PWScore::InitialiseGTU(GTUSet &setGTU, const StringX &sxPolicyName)
{
setGTU.clear();
if (sxPolicyName.empty())
return false;
// Populate the set of all group/title/user entries
GTUSetPair pr_gtu;
ItemListConstIter citer;
for (citer = m_pwlist.begin(); citer != m_pwlist.end(); citer++) {
const CItemData &ci = citer->second;
if (ci.GetPolicyName() == sxPolicyName) {
pr_gtu = setGTU.insert(st_GroupTitleUser(ci.GetGroup(), ci.GetTitle(), ci.GetUser()));
if (!pr_gtu.second) {
// Could happen if merging or synching a bad database!
setGTU.clear();
return false;
}
}
}
return true;
}
bool PWScore::InitialiseUUID(UUIDSet &setUUID)
{
// Populate the set of all UUID entries
UUIDSetPair pr_uuid;
ItemListConstIter citer;
setUUID.clear();
for (citer = m_pwlist.begin(); citer != m_pwlist.end(); citer++) {
pr_uuid = setUUID.insert(citer->second.GetUUID());
if (!pr_uuid.second) {
// Could happen if merging or synching a bad database!
setUUID.clear();
return false;
}
}
return true;
}
bool PWScore::MakeEntryUnique(GTUSet &setGTU,
const StringX &sxgroup, StringX &sxtitle,
const StringX &sxuser, const int IDS_MESSAGE)
{
StringX sxnewtitle(_T(""));
GTUSetPair pr_gtu;
bool retval = true;
// Add supplied GTU - if already present, change title until a
// unique combination is found.
pr_gtu = setGTU.insert(st_GroupTitleUser(sxgroup, sxtitle, sxuser));
if (!pr_gtu.second) { // insert failed => already in set!
retval = false;
int i = 0;
StringX s_copy;
do {
i++;
Format(s_copy, IDS_MESSAGE, i);
sxnewtitle = sxtitle + s_copy;
pr_gtu = setGTU.insert(st_GroupTitleUser(sxgroup, sxnewtitle, sxuser));
} while (!pr_gtu.second);
sxtitle = sxnewtitle;
}
return retval; // false iff we had to modify sxtitle
}
void PWScore::DoAddDependentEntry(const CUUID &base_uuid,
const CUUID &entry_uuid,
const CItemData::EntryType type)
{
ItemMMap *pmmap;
ItemMap *pmap;
if (type == CItemData::ET_ALIAS) {
pmap = &m_alias2base_map;
pmmap = &m_base2aliases_mmap;
} else if (type == CItemData::ET_SHORTCUT) {
pmap = &m_shortcut2base_map;
pmmap = &m_base2shortcuts_mmap;
} else
return;
ItemListIter iter = m_pwlist.find(base_uuid);
ASSERT(iter != m_pwlist.end());
bool baseWasNormal = iter->second.IsNormal();
if (type == CItemData::ET_ALIAS) {
// Mark base entry as a base entry - must be a normal entry or already an alias base
ASSERT(iter->second.IsNormal() || iter->second.IsAliasBase());
iter->second.SetAliasBase();
if (baseWasNormal)
GUIRefreshEntry(iter->second);
} else if (type == CItemData::ET_SHORTCUT) {
// Mark base entry as a base entry - must be a normal entry or already a shortcut base
ASSERT(iter->second.IsNormal() || iter->second.IsShortcutBase());
iter->second.SetShortcutBase();
if (baseWasNormal)
GUIRefreshEntry(iter->second);
}
// Add to both the base->type multimap and the type->base map
pmmap->insert(ItemMMap_Pair(base_uuid, entry_uuid));
pmap->insert(ItemMap_Pair(entry_uuid, base_uuid));
}
void PWScore::DoRemoveDependentEntry(const CUUID &base_uuid,
const CUUID &entry_uuid,
const CItemData::EntryType type)
{
ItemMMap *pmmap;
ItemMap *pmap;
if (type == CItemData::ET_ALIAS) {
pmap = &m_alias2base_map;
pmmap = &m_base2aliases_mmap;
} else if (type == CItemData::ET_SHORTCUT) {
pmap = &m_shortcut2base_map;
pmmap = &m_base2shortcuts_mmap;
} else
return;
// Remove from entry -> base map
pmap->erase(entry_uuid);
// Remove from base -> entry multimap
ItemMMapIter mmiter;
ItemMMapIter mmlastElement;
mmiter = pmmap->find(base_uuid);
if (mmiter == pmmap->end())
return;
mmlastElement = pmmap->upper_bound(base_uuid);
CUUID mmiter_uuid;
for ( ; mmiter != mmlastElement; mmiter++) {
mmiter_uuid = mmiter->second;
if (entry_uuid == mmiter_uuid) {
pmmap->erase(mmiter);
break;
}
}
// Reset base entry to normal if it has no more aliases
if (pmmap->find(base_uuid) == pmmap->end()) {
ItemListIter iter = m_pwlist.find(base_uuid);
if (iter != m_pwlist.end()) {
iter->second.SetNormal();
GUIRefreshEntry(iter->second);
}
}
}
void PWScore::DoRemoveAllDependentEntries(const CUUID &base_uuid,
const CItemData::EntryType type)
{
ItemMMap *pmmap;
ItemMap *pmap;
if (type == CItemData::ET_ALIAS) {
pmap = &m_alias2base_map;
pmmap = &m_base2aliases_mmap;
} else if (type == CItemData::ET_SHORTCUT) {
pmap = &m_shortcut2base_map;
pmmap = &m_base2shortcuts_mmap;
} else
return;
// Remove from entry -> base map for each entry
ItemMMapIter itr;
ItemMMapIter lastElement;
itr = pmmap->find(base_uuid);
if (itr == pmmap->end())
return;
lastElement = pmmap->upper_bound(base_uuid);
for ( ; itr != lastElement; itr++) {
// Remove from entry -> base map
pmap->erase(itr->second);
}
// Remove from base -> entry multimap
pmmap->erase(base_uuid);
// Reset base entry to normal
ItemListIter iter = m_pwlist.find(base_uuid);
if (iter != m_pwlist.end())
iter->second.SetNormal();
}
void PWScore::DoMoveDependentEntries(const CUUID &from_baseuuid,
const CUUID &to_baseuuid,
const CItemData::EntryType type)
{
ItemMMap *pmmap;
ItemMap *pmap;
if (type == CItemData::ET_ALIAS) {
pmap = &m_alias2base_map;
pmmap = &m_base2aliases_mmap;
} else if (type == CItemData::ET_SHORTCUT) {
pmap = &m_shortcut2base_map;
pmmap = &m_base2shortcuts_mmap;
} else
return;
ItemMMapIter from_itr;
ItemMMapIter lastfromElement;
from_itr = pmmap->find(from_baseuuid);
if (from_itr == pmmap->end())
return;
lastfromElement = pmmap->upper_bound(from_baseuuid);
for ( ; from_itr != lastfromElement; from_itr++) {
// Add to new base in base -> entry multimap
pmmap->insert(ItemMMap_Pair(to_baseuuid, from_itr->second));
// Remove from entry -> base map
pmap->erase(from_itr->second);
// Add to entry -> base map (new base)
pmap->insert(ItemMap_Pair(from_itr->second, to_baseuuid));
}
// Now delete all old base entries
pmmap->erase(from_baseuuid);
}
int PWScore::DoAddDependentEntries(UUIDVector &dependentlist, CReport *pRpt,
const CItemData::EntryType type, const int &iVia,
ItemList *pmapDeletedItems,
SaveTypePWMap *pmapSaveTypePW)
{
// When called during validation of a database - *pRpt is valid
// When called during the opening of a database or during drag & drop
// - *pRpt is NULL and no report generated
// type is either CItemData::ET_ALIAS or CItemData::ET_SHORTCUT
// If iVia == CItemData::UUID, the password was "[[uuidstr]]" or "[~uuidstr~]" of the
// associated base entry
// If iVia == CItemData::PASSWORD, the password is expected to be in the full format
// [g:t:u], where g and/or u may be empty.
if (pmapDeletedItems != NULL)
pmapDeletedItems->clear();
if (pmapSaveTypePW != NULL)
pmapSaveTypePW->clear();
ItemMap *pmap;
ItemMMap *pmmap;
if (type == CItemData::ET_ALIAS) {
pmap = &m_alias2base_map;
pmmap = &m_base2aliases_mmap;
} else if (type == CItemData::ET_SHORTCUT) {
pmap = &m_shortcut2base_map;
pmmap = &m_base2shortcuts_mmap;
} else
return -1;
int num_warnings(0);
st_SaveTypePW st_typepw;
if (!dependentlist.empty()) {
UUIDVectorIter paiter;
ItemListIter iter;
StringX csPwdGroup, csPwdTitle, csPwdUser, tmp;
CUUID base_uuid(CUUID::NullUUID());
bool bwarnings(false);
stringT strError;
for (paiter = dependentlist.begin();
paiter != dependentlist.end(); paiter++) {
iter = m_pwlist.find(*paiter);
if (iter == m_pwlist.end())
return num_warnings;
CItemData *pci_curitem = &iter->second;
CUUID entry_uuid = pci_curitem->GetUUID();
GetDependentEntryBaseUUID(entry_uuid, base_uuid, type);
// Delete it - we will put it back if it is an alias/shortcut
pmap->erase(entry_uuid);
if (iVia == CItemData::UUID) {
iter = m_pwlist.find(base_uuid);
} else {
tmp = pci_curitem->GetPassword();
// Remove leading '[['/'[~' & trailing ']]'/'~]'
tmp = tmp.substr(2, tmp.length() - 4);
if (std::count(tmp.begin(), tmp.end(), _T(':')) == 2) {
csPwdGroup = tmp.substr(0, tmp.find_first_of(_T(":")));
// Skip over 'group:'
tmp = tmp.substr(csPwdGroup.length() + 1);
csPwdTitle = tmp.substr(0, tmp.find_first_of(_T(":")));
// Skip over 'title:'
csPwdUser = tmp.substr(csPwdTitle.length() + 1);
iter = Find(csPwdGroup, csPwdTitle, csPwdUser);
} else {
iter = m_pwlist.end();
}
}
if (iter != m_pwlist.end()) {
if (type == CItemData::ET_SHORTCUT) {
// Adding shortcuts -> Base must be normal or already a shortcut base
if (!iter->second.IsNormal() && !iter->second.IsShortcutBase()) {
// Bad news!
if (pRpt != NULL) {
if (!bwarnings) {
bwarnings = true;
LoadAString(strError, IDSC_IMPORTWARNINGHDR);
pRpt->WriteLine(strError);
}
stringT cs_type;
LoadAString(cs_type, IDSC_SHORTCUT);
Format(strError, IDSC_IMPORTWARNING3, cs_type.c_str(),
pci_curitem->GetGroup().c_str(), pci_curitem->GetTitle().c_str(),
pci_curitem->GetUser().c_str(), cs_type.c_str());
pRpt->WriteLine(strError);
}
// Invalid - delete!
if (pmapDeletedItems != NULL)
pmapDeletedItems->insert(ItemList_Pair(*paiter, *pci_curitem));
m_pwlist.erase(iter);
continue;
}
}
if (type == CItemData::ET_ALIAS) {
// Adding Aliases -> Base must be normal or already a alias base
if (!iter->second.IsNormal() && !iter->second.IsAliasBase()) {
// Bad news!
if (pRpt != NULL) {
if (!bwarnings) {
bwarnings = true;
LoadAString(strError, IDSC_IMPORTWARNINGHDR);
pRpt->WriteLine(strError);
}
stringT cs_type;
LoadAString(cs_type, IDSC_ALIAS);
Format(strError, IDSC_IMPORTWARNING3, cs_type.c_str(),
pci_curitem->GetGroup().c_str(), pci_curitem->GetTitle().c_str(),
pci_curitem->GetUser().c_str(), cs_type.c_str());
pRpt->WriteLine(strError);
}
// Invalid - delete!
if (pmapDeletedItems != NULL)
pmapDeletedItems->insert(ItemList_Pair(*paiter, *pci_curitem));
m_pwlist.erase(iter);
continue;
}
if (iter->second.IsAlias()) {
// This is an alias too! Not allowed! Make new one point to original base
// Note: this may be random as who knows the order of reading records?
CUUID temp_uuid = iter->second.GetUUID();
GetDependentEntryBaseUUID(temp_uuid, base_uuid, type);
if (pRpt != NULL) {
if (!bwarnings) {
bwarnings = true;
LoadAString(strError, IDSC_IMPORTWARNINGHDR);
pRpt->WriteLine(strError);
}
Format(strError, IDSC_IMPORTWARNING1, pci_curitem->GetGroup().c_str(),
pci_curitem->GetTitle().c_str(), pci_curitem->GetUser().c_str());
pRpt->WriteLine(strError);
LoadAString(strError, IDSC_IMPORTWARNING1A);
pRpt->WriteLine(strError);
}
if (pmapSaveTypePW != NULL) {
st_typepw.et = iter->second.GetEntryType();
st_typepw.sxpw = _T("");
pmapSaveTypePW->insert(SaveTypePWMap_Pair(*paiter, st_typepw));
}
pci_curitem->SetAlias();
num_warnings++;
}
}
base_uuid = iter->second.GetUUID();
if (type == CItemData::ET_ALIAS) {
if (pmapSaveTypePW != NULL) {
st_typepw.et = iter->second.GetEntryType();
st_typepw.sxpw = _T("");
pmapSaveTypePW->insert(SaveTypePWMap_Pair(*paiter, st_typepw));
}
iter->second.SetAliasBase();
} else
if (type == CItemData::ET_SHORTCUT) {
if (pmapSaveTypePW != NULL) {
st_typepw.et = iter->second.GetEntryType();
st_typepw.sxpw = _T("");
pmapSaveTypePW->insert(SaveTypePWMap_Pair(*paiter, st_typepw));
}
iter->second.SetShortcutBase();
}
pmmap->insert(ItemMMap_Pair(base_uuid, entry_uuid));
pmap->insert(ItemMap_Pair(entry_uuid, base_uuid));
if (type == CItemData::ET_ALIAS) {
if (pmapSaveTypePW != NULL) {
st_typepw.et = iter->second.GetEntryType();
st_typepw.sxpw = pci_curitem->GetPassword();
pmapSaveTypePW->insert(SaveTypePWMap_Pair(*paiter, st_typepw));
}
pci_curitem->SetPassword(_T("[Alias]"));
pci_curitem->SetAlias();
} else
if (type == CItemData::ET_SHORTCUT) {
if (pmapSaveTypePW != NULL) {
st_typepw.et = iter->second.GetEntryType();
st_typepw.sxpw = pci_curitem->GetPassword();
pmapSaveTypePW->insert(SaveTypePWMap_Pair(*paiter, st_typepw));
}
pci_curitem->SetPassword(_T("[Shortcut]"));
pci_curitem->SetShortcut();
}
} else {
// Specified base does not exist!
if (pRpt != NULL) {
if (!bwarnings) {
bwarnings = true;
LoadAString(strError, IDSC_IMPORTWARNINGHDR);
pRpt->WriteLine(strError);
}
Format(strError, IDSC_IMPORTWARNING2, pci_curitem->GetGroup().c_str(),
pci_curitem->GetTitle().c_str(), pci_curitem->GetUser().c_str());
pRpt->WriteLine(strError);
LoadAString(strError, IDSC_IMPORTWARNING2A);
pRpt->WriteLine(strError);
}
if (type == CItemData::ET_SHORTCUT) {
if (pmapDeletedItems != NULL)
pmapDeletedItems->insert(ItemList_Pair(*paiter, *pci_curitem));
} else {
if (pmapSaveTypePW != NULL) {
st_typepw.et = CItemData::ET_ALIAS;
st_typepw.sxpw = _T("");
pmapSaveTypePW->insert(SaveTypePWMap_Pair(*paiter, st_typepw));
}
pci_curitem->SetNormal(); // but can make invalid alias a normal entry
}
num_warnings++;
}
}
}
return num_warnings;
}
void PWScore::UndoAddDependentEntries(ItemList *pmapDeletedItems,
SaveTypePWMap *pmapSaveTypePW)
{
ItemListIter iter, add_iter;
SaveTypePWMap::iterator restore_iter;
for (add_iter = pmapDeletedItems->begin();
add_iter != pmapDeletedItems->end();
add_iter++) {
m_pwlist[add_iter->first] = add_iter->second;
}
for (restore_iter = pmapSaveTypePW->begin();
restore_iter != pmapSaveTypePW->end();
restore_iter++) {
iter = m_pwlist.find(restore_iter->first);
if (iter == m_pwlist.end())
continue;
CItemData *pci_changeditem = &iter->second;
st_SaveTypePW *pst_typepw = &restore_iter->second;
pci_changeditem->SetEntryType(pst_typepw->et);
if (!pst_typepw->sxpw.empty())
pci_changeditem->SetPassword(pst_typepw->sxpw);
}
}
void PWScore::ResetAllAliasPasswords(const CUUID &base_uuid)
{
// Alias ONLY - no shortcut version needed
ItemMMapIter itr;
ItemMMapIter lastElement;
ItemListIter base_itr, alias_itr;
StringX csBasePassword;
itr = m_base2aliases_mmap.find(base_uuid);
if (itr == m_base2aliases_mmap.end())
return;
base_itr = m_pwlist.find(base_uuid);
if (base_itr != m_pwlist.end()) {
csBasePassword = base_itr->second.GetPassword();
} else {
m_base2aliases_mmap.erase(base_uuid);
return;
}
lastElement = m_base2aliases_mmap.upper_bound(base_uuid);
for ( ; itr != lastElement; itr++) {
CUUID alias_uuid = itr->second;
alias_itr = m_pwlist.find(alias_uuid);
if (alias_itr != m_pwlist.end()) {
alias_itr->second.SetPassword(csBasePassword);
alias_itr->second.SetNormal();
GUIRefreshEntry(alias_itr->second);
}
}
m_base2aliases_mmap.erase(base_uuid);
}
void PWScore::GetAllDependentEntries(const CUUID &base_uuid, UUIDVector &tlist,
const CItemData::EntryType type)
{
ItemMMapIter itr;
ItemMMapIter lastElement;
ItemMMap *pmmap;
if (type == CItemData::ET_ALIAS)
pmmap = &m_base2aliases_mmap;
else if (type == CItemData::ET_SHORTCUT)
pmmap = &m_base2shortcuts_mmap;
else
return;
itr = pmmap->find(base_uuid);
if (itr == pmmap->end())
return;
lastElement = pmmap->upper_bound(base_uuid);
for ( ; itr != lastElement; itr++) {
tlist.push_back(itr->second);
}
}
bool PWScore::ParseBaseEntryPWD(const StringX &Password, BaseEntryParms &pl)
{
// pl.ibasedata is:
// +n: password contains (n-1) colons and base entry found (n = 1, 2 or 3)
// 0: password not in alias format
// -n: password contains (n-1) colons but either no base entry found or no unique entry found (n = 1, 2 or 3)
// "bMultipleEntriesFound" is set if no "unique" base entry could be found and is only valid if n = -1 or -2.
pl.bMultipleEntriesFound = false;
// Take a copy of the Password field to do the counting!
StringX passwd(Password);
int num_colonsP1 = Replace(passwd, _T(':'), _T(';')) + 1;
if ((Password[0] == _T('[')) &&
(Password[Password.length() - 1] == _T(']')) &&
num_colonsP1 <= 3) {
StringX tmp;
ItemListIter iter;
switch (num_colonsP1) {
case 1:
// [X] - OK if unique entry [g:X:u], [g:X:], [:X:u] or [:X:] exists for any value of g or u
pl.csPwdTitle = Password.substr(1, Password.length() - 2); // Skip over '[' & ']'
iter = GetUniqueBase(pl.csPwdTitle, pl.bMultipleEntriesFound);
if (iter != m_pwlist.end()) {
// Fill in the fields found during search
pl.csPwdGroup = iter->second.GetGroup();
pl.csPwdUser = iter->second.GetUser();
}
break;
case 2:
// [X:Y] - OK if unique entry [X:Y:u] or [g:X:Y] exists for any value of g or u
pl.csPwdUser = _T("");
tmp = Password.substr(1, Password.length() - 2); // Skip over '[' & ']'
pl.csPwdGroup = tmp.substr(0, tmp.find_first_of(_T(":")));
pl.csPwdTitle = tmp.substr(pl.csPwdGroup.length() + 1); // Skip over 'group:'
iter = GetUniqueBase(pl.csPwdGroup, pl.csPwdTitle, pl.bMultipleEntriesFound);
if (iter != m_pwlist.end()) {
// Fill in the fields found during search
pl.csPwdGroup = iter->second.GetGroup();
pl.csPwdTitle = iter->second.GetTitle();
pl.csPwdUser = iter->second.GetUser();
}
break;
case 3:
// [X:Y:Z], [X:Y:], [:Y:Z], [:Y:] (title cannot be empty)
tmp = Password.substr(1, Password.length() - 2); // Skip over '[' & ']'
pl.csPwdGroup = tmp.substr(0, tmp.find_first_of(_T(":")));
tmp = tmp.substr(pl.csPwdGroup.length() + 1); // Skip over 'group:'
pl.csPwdTitle = tmp.substr(0, tmp.find_first_of(_T(":"))); // Skip over 'title:'
pl.csPwdUser = tmp.substr(pl.csPwdTitle.length() + 1);
iter = Find(pl.csPwdGroup, pl.csPwdTitle, pl.csPwdUser);
break;
default:
ASSERT(0);
}
if (iter != m_pwlist.end()) {
pl.TargetType = iter->second.GetEntryType();
if (pl.InputType == CItemData::ET_ALIAS && pl.TargetType == CItemData::ET_ALIAS) {
// Check if base is already an alias, if so, set this entry -> real base entry
CUUID temp_uuid = iter->second.GetUUID();
GetDependentEntryBaseUUID(temp_uuid, pl.base_uuid, CItemData::ET_ALIAS);
} else {
// This may not be a valid combination of source+target entries - sorted out by caller
pl.base_uuid = iter->second.GetUUID();
}
// Valid and found
pl.ibasedata = num_colonsP1;
return true;
}
// Valid but either exact [g:t:u] not found or
// no or multiple entries satisfy [x] or [x:y]
pl.ibasedata = -num_colonsP1;
return true;
}
pl.ibasedata = 0; // invalid password format for an alias
return false;
}
const CItemData *PWScore::GetBaseEntry(const CItemData *pAliasOrSC) const
{
return const_cast<PWScore *>(this)->GetBaseEntry(pAliasOrSC);
}
CItemData *PWScore::GetBaseEntry(const CItemData *pAliasOrSC)
{
// Alas, we need both a const and non-const version.
ASSERT(pAliasOrSC != NULL);
CItemData::EntryType et = pAliasOrSC->GetEntryType();
if (et != CItemData::ET_ALIAS && et != CItemData::ET_SHORTCUT) {
//pws_os::Trace(_T("PWScore::GetBaseEntry called with non-dependent element!\n"));
return NULL;
}
CUUID base_uuid(CUUID::NullUUID());
CUUID dep_uuid = pAliasOrSC->GetUUID();
if (!GetDependentEntryBaseUUID(dep_uuid, base_uuid, et)) {
// pws_os::Trace(_T("PWScore::GetBaseEntry - couldn't find base uuid!\n"));
return NULL;
}
ItemListIter iter = Find(base_uuid);
if (iter == GetEntryEndIter()) {
//pws_os::Trace(_T("PWScore::GetBaseEntry - Find(base_uuid) failed!\n"));
return NULL;
}
return &iter->second;
}
bool PWScore::GetDependentEntryBaseUUID(const CUUID &entry_uuid,
CUUID &base_uuid,
const CItemData::EntryType type) const
{
base_uuid = CUUID::NullUUID();
const ItemMap *pmap;
if (type == CItemData::ET_ALIAS)
pmap = &m_alias2base_map;
else if (type == CItemData::ET_SHORTCUT)
pmap = &m_shortcut2base_map;
else
return false;
ItemMapConstIter iter = pmap->find(entry_uuid);
if (iter != pmap->end()) {
base_uuid = iter->second;
return true;
} else {
return false;
}
}
bool PWScore::SetUIInterFace(UIInterFace *pUIIF, size_t numsupported,
std::bitset<UIInterFace::NUM_SUPPORTED> bsSupportedFunctions)
{
bool brc(true);
m_pUIIF = pUIIF;
ASSERT(numsupported == UIInterFace::NUM_SUPPORTED);
m_bsSupportedFunctions.reset();
if (numsupported == UIInterFace::NUM_SUPPORTED) {
m_bsSupportedFunctions = bsSupportedFunctions;
} else {
size_t minsupported = min(numsupported, size_t(UIInterFace::NUM_SUPPORTED));
for (size_t i = 0; i < minsupported; i++) {
m_bsSupportedFunctions.set(i, bsSupportedFunctions.test(i));
}
brc = false;
}
return brc;
}
/*
* Start UI Interface feedback routines
*/
void PWScore::NotifyDBModified()
{
// This allows the core to provide feedback to the UI that the Database
// has changed particularly to invalidate any current Find results and
// to populate message during Vista and later shutdowns
if (m_bNotifyDB && m_pUIIF != NULL &&
m_bsSupportedFunctions.test(UIInterFace::DATABASEMODIFIED))
m_pUIIF->DatabaseModified(m_bDBChanged || m_bDBPrefsChanged);
}
void PWScore::NotifyGUINeedsUpdating(UpdateGUICommand::GUI_Action ga,
const CUUID &entry_uuid,
CItemData::FieldType ft,
bool bUpdateGUI)
{
// This allows the core to provide feedback to the UI that the GUI needs
// uupdating due to a field having its value changed
if (m_pUIIF != NULL &&
m_bsSupportedFunctions.test(UIInterFace::UPDATEGUI))
m_pUIIF->UpdateGUI(ga, entry_uuid, ft, bUpdateGUI);
}
void PWScore::GUISetupDisplayInfo(CItemData &ci)
{
// This allows the core to provide feedback to the UI that ???
if (m_pUIIF != NULL &&
m_bsSupportedFunctions.test(UIInterFace::GUISETUPDISPLAYINFO))
m_pUIIF->GUISetupDisplayInfo(ci);
}
void PWScore::GUIRefreshEntry(const CItemData &ci)
{
// This allows the core to provide feedback to the UI that a particular
// entry has been modifed
if (m_pUIIF != NULL &&
m_bsSupportedFunctions.test(UIInterFace::GUIREFRESHENTRY))
m_pUIIF->GUIRefreshEntry(ci);
}
void PWScore::UpdateWizard(const stringT &s)
{
// This allows the core to provide feedback to the Compare, Merge, Synchronize,
// Exort (Text/XML) UI wizard as to the entry currently being processed.
// The UI must be able to access the control in the wizard and the supplied
// string gives the full 'group, title, user' of the entry.
// It is expected that the UI will implement a pointer or other reference to
// this control so that it can update the text displayed there (see MFC implementation).
if (m_pUIIF != NULL &&
m_bsSupportedFunctions.test(UIInterFace::UPDATEWIZARD))
m_pUIIF->UpdateWizard(s);
}
/*
* End UI Interface feedback routines
*/
bool PWScore::LockFile(const stringT &filename, stringT &locker)
{
return pws_os::LockFile(filename, locker,
m_lockFileHandle, m_LockCount);
}
bool PWScore::IsLockedFile(const stringT &filename) const
{
return pws_os::IsLockedFile(filename);
}
void PWScore::UnlockFile(const stringT &filename)
{
return pws_os::UnlockFile(filename,
m_lockFileHandle, m_LockCount);
}
bool PWScore::LockFile2(const stringT &filename, stringT &locker)
{
return pws_os::LockFile(filename, locker,
m_lockFileHandle2, m_LockCount2);
}
void PWScore::UnlockFile2(const stringT &filename)
{
return pws_os::UnlockFile(filename,
m_lockFileHandle2, m_LockCount2);
}
bool PWScore::IsNodeModified(StringX &path) const
{
return std::find(m_vnodes_modified.begin(),
m_vnodes_modified.end(), path) != m_vnodes_modified.end();
}
void PWScore::AddChangedNodes(StringX path)
{
StringX nextpath(path);
while (!nextpath.empty()) {
if (std::find(m_vnodes_modified.begin(), m_vnodes_modified.end(), nextpath) == m_vnodes_modified.end())
m_vnodes_modified.push_back(nextpath);
size_t i = nextpath.find_last_of(_T("."));
if (i == nextpath.npos)
i = 0;
nextpath = nextpath.substr(0, i);
}
}
// functor objects for updating password history for each entry
struct HistoryUpdater {
HistoryUpdater(int &num_altered,
SavePWHistoryMap &mapSavedHistory, bool bExcludeProtected)
: m_num_altered(num_altered), m_mapSavedHistory(mapSavedHistory),
m_bExcludeProtected(bExcludeProtected)
{}
virtual void operator() (CItemData &ci) = 0;
protected:
int &m_num_altered;
SavePWHistoryMap &m_mapSavedHistory;
std::vector<BYTE> m_vSavedEntryStatus;
bool m_bExcludeProtected;
private:
HistoryUpdater& operator=(const HistoryUpdater&); // Do not implement
};
struct HistoryUpdateResetOff : public HistoryUpdater {
HistoryUpdateResetOff(int &num_altered,
SavePWHistoryMap &mapSavedHistory, bool bExcludeProtected)
: HistoryUpdater(num_altered, mapSavedHistory, bExcludeProtected) {}
void operator()(CItemData &ci) {
if (!ci.IsProtected() ||
(!m_bExcludeProtected && ci.IsProtected())) {
StringX cs_tmp = ci.GetPWHistory();
if (cs_tmp.length() >= 5 && cs_tmp[0] == L'1') {
st_PWH_status st_pwhs;
st_pwhs.pwh = cs_tmp;
st_pwhs.es = ci.GetStatus();
m_mapSavedHistory[ci.GetUUID()] = st_pwhs;
cs_tmp[0] = L'0';
ci.SetPWHistory(cs_tmp);
ci.SetStatus(CItemData::ES_MODIFIED);
m_num_altered++;
}
}
}
private:
HistoryUpdateResetOff& operator=(const HistoryUpdateResetOff&); // Do not implement
};
struct HistoryUpdateResetOn : public HistoryUpdater {
HistoryUpdateResetOn(int &num_altered, int new_default_max,
SavePWHistoryMap &mapSavedHistory, bool bExcludeProtected)
: HistoryUpdater(num_altered, mapSavedHistory, bExcludeProtected)
{Format(m_text, _T("1%02x00"), new_default_max);}
void operator()(CItemData &ci) {
if (!ci.IsProtected() ||
(!m_bExcludeProtected && ci.IsProtected())) {
StringX cs_tmp = ci.GetPWHistory();
st_PWH_status st_pwhs;
st_pwhs.pwh = cs_tmp;
st_pwhs.es = ci.GetStatus();
if (cs_tmp.length() < 5) {
m_mapSavedHistory[ci.GetUUID()] = st_pwhs;
ci.SetPWHistory(m_text);
m_num_altered++;
} else {
if (cs_tmp[0] == L'0') {
m_mapSavedHistory[ci.GetUUID()] = st_pwhs;
cs_tmp[0] = L'1';
ci.SetPWHistory(cs_tmp);
ci.SetStatus(CItemData::ES_MODIFIED);
m_num_altered++;
}
}
}
}
private:
HistoryUpdateResetOn& operator=(const HistoryUpdateResetOn&); // Do not implement
StringX m_text;
};
struct HistoryUpdateSetMax : public HistoryUpdater {
HistoryUpdateSetMax(int &num_altered, int new_default_max,
SavePWHistoryMap &mapSavedHistory, bool bExcludeProtected)
: HistoryUpdater(num_altered, mapSavedHistory, bExcludeProtected),
m_new_default_max(new_default_max)
{Format(m_text, _T("1%02x"), new_default_max);}
void operator()(CItemData &ci) {
if (!ci.IsProtected() ||
(!m_bExcludeProtected && ci.IsProtected())) {
StringX cs_tmp = ci.GetPWHistory();
size_t len = cs_tmp.length();
if (len >= 5) {
st_PWH_status st_pwhs;
st_pwhs.pwh = cs_tmp;
st_pwhs.es = ci.GetStatus();
m_mapSavedHistory[ci.GetUUID()] = st_pwhs;
int status, old_max, num_saved;
const wchar_t *lpszPWHistory = cs_tmp.c_str();
#if (_MSC_VER >= 1400)
int iread = swscanf_s(lpszPWHistory, _T("%01d%02x%02x"),
&status, &old_max, &num_saved);
#else
int iread = swscanf(lpszPWHistory, _T("%01d%02x%02x"),
&status, &old_max, &num_saved);
#endif
if (iread == 3 && status == 1 && num_saved <= m_new_default_max) {
cs_tmp = m_text + cs_tmp.substr(3);
ci.SetPWHistory(cs_tmp);
ci.SetStatus(CItemData::ES_MODIFIED);
m_num_altered++;
}
}
}
}
private:
HistoryUpdateSetMax& operator=(const HistoryUpdateSetMax&); // Do not implement
int m_new_default_max;
StringX m_text;
};
struct HistoryUpdateClearAll : public HistoryUpdater {
HistoryUpdateClearAll(int &num_altered,
SavePWHistoryMap &mapSavedHistory, bool bExcludeProtected)
: HistoryUpdater(num_altered, mapSavedHistory, bExcludeProtected) {}
void operator()(CItemData &ci) {
if (!ci.IsProtected() ||
(!m_bExcludeProtected && ci.IsProtected())) {
StringX cs_tmp = ci.GetPWHistory();
size_t len = cs_tmp.length();
if (len != 0 && cs_tmp != _T("00000")) {
st_PWH_status st_pwhs;
st_pwhs.pwh = cs_tmp;
st_pwhs.es = ci.GetStatus();
m_mapSavedHistory[ci.GetUUID()] = st_pwhs;
ci.SetPWHistory(L"");
ci.SetStatus(CItemData::ES_MODIFIED);
m_num_altered++;
}
}
}
private:
HistoryUpdateClearAll& operator=(const HistoryUpdateClearAll&); // Do not implement
};
int PWScore::DoUpdatePasswordHistory(int iAction, int new_default_max,
SavePWHistoryMap &mapSavedHistory)
{
int num_altered = 0;
HistoryUpdater *updater = NULL;
bool bExcludeProtected(true);
if (iAction < 0)
bExcludeProtected = false;
HistoryUpdateResetOff reset_off(num_altered, mapSavedHistory, bExcludeProtected);
HistoryUpdateResetOn reset_on(num_altered, new_default_max, mapSavedHistory,
bExcludeProtected);
HistoryUpdateSetMax set_max(num_altered, new_default_max, mapSavedHistory,
bExcludeProtected);
HistoryUpdateClearAll clearall(num_altered, mapSavedHistory, bExcludeProtected);
switch (iAction) {
case -1: // reset off - include protected entries
case 1: // reset off - exclude protected entries
updater = &reset_off;
break;
case -2: // reset on - include protected entries
case 2: // reset on - exclude protected entries
updater = &reset_on;
break;
case -3: // setmax - include protected entries
case 3: // setmax - exclude protected entries
updater = &set_max;
break;
case -4: // clearall - include protected entries
case 4: // clearall - exclude protected entries
updater = &clearall;
break;
default:
ASSERT(0);
break;
} // switch (iAction)
/**
* Interesting problem - a for_each iterator
* cause a copy c'tor of the pair to be invoked, resulting
* in a temporary copy of the CItemDatum being modified.
* Couldn't find a handy way to workaround this (e.g.,
* operator()(pair<...> &p) failed to compile
* so reverted to slightly less elegant for loop
* using polymorphism for the history updater
* is an unrelated tweak.
*/
if (updater != NULL) {
ItemListIter listPos;
for (listPos = m_pwlist.begin(); listPos != m_pwlist.end(); listPos++) {
CItemData &curitem = listPos->second;
(*updater)(curitem);
}
}
return num_altered;
}
void PWScore::UndoUpdatePasswordHistory(SavePWHistoryMap &mapSavedHistory)
{
SavePWHistoryMap::iterator itr;
for (itr = mapSavedHistory.begin(); itr != mapSavedHistory.end(); itr++) {
ItemListIter listPos = m_pwlist.find(itr->first);
if (listPos != m_pwlist.end()) {
listPos->second.SetPWHistory(itr->second.pwh);
listPos->second.SetStatus(itr->second.es);
}
}
}
int PWScore::DoRenameGroup(const StringX &sxOldPath, const StringX &sxNewPath)
{
const StringX sxDot(L".");
const wchar_t wcDot=L'.';
StringX sxOldPath2 = sxOldPath + sxDot;
const size_t len2 = sxOldPath2.length();
ItemListIter iter;
for (iter = m_pwlist.begin(); iter != m_pwlist.end(); iter++) {
if (iter->second.GetGroup() == sxOldPath) {
iter->second.SetGroup(sxNewPath);
}
else if ((iter->second.GetGroup().length() > len2) && (iter->second.GetGroup().substr(0, len2) == sxOldPath2) &&
(iter->second.GetGroup()[len2] != wcDot)) {
// Need to check that next symbol is not a dot
// to ensure not affecting another group
// (group name could contain traling dots, for example abc..def.g)
// subgroup name will have len > len2 (old_name + dot + subgroup_name)
StringX sxSubGroups = iter->second.GetGroup().substr(len2);
iter->second.SetGroup(sxNewPath + sxDot + sxSubGroups);
}
}
return 0;
}
void PWScore::UndoRenameGroup(const StringX &sxOldPath, const StringX &sxNewPath)
{
DoRenameGroup(sxNewPath, sxOldPath);
}
void PWScore::GetDBProperties(st_DBProperties &st_dbp)
{
st_dbp.database = m_currfile;
Format(st_dbp.databaseformat, _T("%d.%02d"),
m_hdr.m_nCurrentMajorVersion,
m_hdr.m_nCurrentMinorVersion);
std::vector<std::wstring> aryGroups;
GetUniqueGroups(aryGroups);
Format(st_dbp.numgroups, _T("%d"), aryGroups.size());
Format(st_dbp.numentries, _T("%d"), m_pwlist.size());
time_t twls = m_hdr.m_whenlastsaved;
if (twls == 0) {
LoadAString(st_dbp.whenlastsaved, IDSC_UNKNOWN);
} else {
st_dbp.whenlastsaved = PWSUtil::ConvertToDateTimeString(twls, PWSUtil::TMC_EXPORT_IMPORT);
}
if (m_hdr.m_lastsavedby.empty() && m_hdr.m_lastsavedon.empty()) {
LoadAString(st_dbp.wholastsaved, IDSC_UNKNOWN);
} else {
StringX user = m_hdr.m_lastsavedby.empty() ?
_T("?") : m_hdr.m_lastsavedby.c_str();
StringX host = m_hdr.m_lastsavedon.empty() ?
_T("?") : m_hdr.m_lastsavedon.c_str();
Format(st_dbp.wholastsaved, IDSC_USERONHOST, user.c_str(), host.c_str());
}
if (m_hdr.m_whatlastsaved.empty()) {
LoadAString(st_dbp.whatlastsaved, IDSC_UNKNOWN);
} else
st_dbp.whatlastsaved = m_hdr.m_whatlastsaved;
if(m_hdr.m_file_uuid == CUUID::NullUUID())
st_dbp.file_uuid = _T("N/A");
else {
CUUID huuid(*m_hdr.m_file_uuid.GetARep(), true); // true for canonical format
ostringstreamT os;
os << uppercase << huuid;
st_dbp.file_uuid = os.str().c_str();
}
int num = m_nRecordsWithUnknownFields;
if (num != 0 || !m_UHFL.empty()) {
StringX cs_Yes, cs_No, cs_HdrYesNo;
LoadAString(cs_Yes, IDSC_YES);
LoadAString(cs_No, IDSC_NO);
cs_HdrYesNo = m_UHFL.empty() ? cs_No : cs_Yes;
Format(st_dbp.unknownfields, IDSC_UNKNOWNFIELDS, cs_HdrYesNo.c_str());
if (num == 0) {
st_dbp.unknownfields += cs_No;
st_dbp.unknownfields += _T(")");
} else {
StringX wls;
Format(wls, _T("%d"), num);
st_dbp.unknownfields += wls;
st_dbp.unknownfields += _T(")");
}
} else {
LoadAString(st_dbp.unknownfields, IDSC_NONE);
}
st_dbp.db_name = m_hdr.m_dbname;
st_dbp.db_description = m_hdr.m_dbdesc;
}
void PWScore::SetHeaderUserFields(st_DBProperties &st_dbp)
{
// Currently only 2 user fields in DB header
m_hdr.m_dbname = st_dbp.db_name;
m_hdr.m_dbdesc = st_dbp.db_description;
SetDBChanged(true);
}
void PWScore::UpdateExpiryEntry(const CUUID &uuid, const CItemData::FieldType ft,
const StringX &value)
{
ExpiredList::iterator iter;
iter = std::find_if(m_ExpireCandidates.begin(), m_ExpireCandidates.end(),
std::bind2nd(std::equal_to<pws_os::CUUID>(), uuid));
if (iter == m_ExpireCandidates.end())
return;
if (ft == CItemData::XTIME) {
time_t t;
if ((VerifyImportDateTimeString(value.c_str(), t) ||
VerifyXMLDateTimeString(value.c_str(), t) ||
VerifyASCDateTimeString(value.c_str(), t)) &&
(t != time_t(-1))) { // checkerror despite all our verification!
iter->expirytttXTime = t;
} else {
ASSERT(0);
}
} else {
ASSERT(0);
}
}
bool PWScore::ChangeMode(stringT &locker, int &iErrorCode)
{
PWS_LOGIT;
// We do not have to close or re-open the database as the database is closed after processing.
/*
So what do we need to do?
If currently R/O, we need to lock the database.
If currently R/W, we need to unlock the database.
*/
iErrorCode = SUCCESS;
locker = _T(""); // Important!
if (m_IsReadOnly) {
// We know the file did exist but this will also determine if it is R-O
bool isRO;
if (pws_os::FileExists(m_currfile.c_str(), isRO) && isRO) {
// OK - still exists but is R-O - can't change mode!
// Need new return code but not this close to release - later
iErrorCode = READ_FAIL;
PWS_LOGIT_ARGS0("Failed: READ_FAIL");
return false;
}
// Need to lock it
bool brc = pws_os::LockFile(m_currfile.c_str(), locker,
m_lockFileHandle, m_LockCount);
if (!brc) {
iErrorCode = CANT_GET_LOCK;
PWS_LOGIT_ARGS0("Failed: CANT_GET_LOCK");
return false;
}
// It was R-O, better check no-one has changed anything from in-memory copy
// The one calculated when we read it in is 'm_pFileSig' (R-O - so we haven't changed it)
// This is the new one
PWSFileSig newFileSig = PWSFileSig(m_currfile.c_str());
if (newFileSig.IsValid() && *m_pFileSig != newFileSig) {
// Oops - someone else has changed this user will need to close and open properly.
// Or the file signature is invalid e.g. file not there or fie size too small.
// Tell them the bad news after unlocking file and not changing mode
iErrorCode = DB_HAS_CHANGED;
} else {
// Other error - e.g. can't open file or it is too small.
iErrorCode = newFileSig.GetErrorCode();
}
if (iErrorCode != 0) {
pws_os::UnlockFile(m_currfile.c_str(),
m_lockFileHandle, m_LockCount);
PWS_LOGIT_ARGS("Failed code: %d", iErrorCode);
return false;
}
} else {
// In R/W mode
if (m_LockCount != 1) {
iErrorCode = FAILURE; // Not actually used as only one failure type
PWS_LOGIT_ARGS0("Failed count not 1");
return false;
}
// Try to unlock file
pws_os::UnlockFile(m_currfile.c_str(),
m_lockFileHandle, m_LockCount);
// If successful - should be invalid handle and lock count is zero
if (m_lockFileHandle != INVALID_HANDLE_VALUE || m_LockCount != 0) {
// Try to put lock back
stringT tmp_locker = _T("");
bool brc = pws_os::LockFile(m_currfile.c_str(), tmp_locker,
m_lockFileHandle, m_LockCount);
// No idea what to do if we can't put it back :-(
#ifdef DEBUG
ASSERT(brc);
#else
UNREFERENCED_PARAMETER(brc); // In Release build only otherwise MS Compiler warning
#endif
return false;
}
}
// Swap Read/Write : Read/Only status
m_IsReadOnly = !m_IsReadOnly;
return true;
}
// Yubi support:
const unsigned char *PWScore::GetYubiSK() const
{
return m_hdr.m_yubi_sk;
}
void PWScore::SetYubiSK(const unsigned char *sk)
{
if (m_hdr.m_yubi_sk)
trashMemory(m_hdr.m_yubi_sk, PWSfile::HeaderRecord::YUBI_SK_LEN);
delete[] m_hdr.m_yubi_sk;
m_hdr.m_yubi_sk = NULL;
if (sk != NULL) {
m_hdr.m_yubi_sk = new unsigned char[PWSfile::HeaderRecord::YUBI_SK_LEN];
memcpy(m_hdr.m_yubi_sk, sk, PWSfile::HeaderRecord::YUBI_SK_LEN);
}
}
bool PWScore::IncrementPasswordPolicy(const StringX &sxPolicyName)
{
PSWDPolicyMapIter iter = m_MapPSWDPLC.find(sxPolicyName);
if (iter == m_MapPSWDPLC.end()) {
return false;
} else {
iter->second.usecount++;
return true;
}
}
bool PWScore::DecrementPasswordPolicy(const StringX &sxPolicyName)
{
PSWDPolicyMapIter iter = m_MapPSWDPLC.find(sxPolicyName);
if (iter == m_MapPSWDPLC.end() || iter->second.usecount == 0) {
return false;
} else {
iter->second.usecount--;
return true;
}
}
void PWScore::AddPolicy(const StringX &sxPolicyName, const PWPolicy &st_pp,
const bool bAllowReplace)
{
bool bDoIt(false);
PSWDPolicyMapIter iter = m_MapPSWDPLC.find(sxPolicyName);
if (iter == m_MapPSWDPLC.end())
bDoIt = true;
else if (bAllowReplace) {
bDoIt = true;
m_MapPSWDPLC.erase(iter);
}
if (bDoIt) {
m_MapPSWDPLC[sxPolicyName] = st_pp;
SetDBChanged(true);
}
}
bool PWScore::IsEmptyGroup(const StringX &sxEmptyGroup)
{
return find(m_vEmptyGroups.begin(), m_vEmptyGroups.end(), sxEmptyGroup) !=
m_vEmptyGroups.end();
}
bool PWScore::AddEmptyGroup(const StringX &sxEmptyGroup)
{
if (find(m_vEmptyGroups.begin(), m_vEmptyGroups.end(), sxEmptyGroup) ==
m_vEmptyGroups.end()) {
m_vEmptyGroups.push_back(sxEmptyGroup);
return true;
} else
return false;
}
bool PWScore::RemoveEmptyGroup(const StringX &sxEmptyGroup)
{
std::vector<StringX>::iterator iter;
iter = find(m_vEmptyGroups.begin(), m_vEmptyGroups.end(), sxEmptyGroup);
if (iter != m_vEmptyGroups.end()) {
m_vEmptyGroups.erase(iter);
return true;
} else
return false;
}
void PWScore::RenameEmptyGroup(const StringX &sxOldPath, const StringX &sxNewPath)
{
std::vector<StringX>::iterator iter;
iter = find(m_vEmptyGroups.begin(), m_vEmptyGroups.end(), sxOldPath);
ASSERT(iter != m_vEmptyGroups.end());
m_vEmptyGroups.erase(iter);
m_vEmptyGroups.push_back(sxNewPath);
}
bool PWScore::AddKBShortcut(const int &iKBShortcut, const pws_os::CUUID &uuid)
{
pair< map<int, pws_os::CUUID>::iterator, bool > pr;
pr = m_KBShortcutMap.insert(KBShortcutMapPair(iKBShortcut, uuid));
return pr.second;
}
bool PWScore::DelKBShortcut(const int32 &iKBShortcut, const pws_os::CUUID &uuid)
{
KBShortcutMapIter iter = m_KBShortcutMap.find(iKBShortcut);
if (iter == m_KBShortcutMap.end())
return false;
else {
ASSERT(uuid == iter->second);
if (uuid == iter->second) {
m_KBShortcutMap.erase(iter);
}
return true;
}
}
const pws_os::CUUID & PWScore::GetKBShortcut(const int32 &iKBShortcut)
{
KBShortcutMapConstIter iter = m_KBShortcutMap.find(iKBShortcut);
if (iter == m_KBShortcutMap.end())
return CUUID::NullUUID();
else
return iter->second;
}
uint32 PWScore::GetHashIters() const
{
return m_hashIters;
}
void PWScore::SetHashIters(uint32 value)
{
if (value != m_hashIters) {
m_hashIters = value;
SetDBPrefsChanged(true);
}
}