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
|
/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
*
* Copyright 2004-2018 The OpenLDAP Foundation.
* Portions Copyright 2004-2005 Howard Chu, Symas Corporation.
* Portions Copyright 2004 Hewlett-Packard Company.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted only as authorized by the OpenLDAP
* Public License.
*
* A copy of this license is available in the file LICENSE in the
* top-level directory of the distribution or, alternatively, at
* <http://www.OpenLDAP.org/license.html>.
*/
/* ACKNOWLEDGEMENTS:
* This work was developed by Howard Chu for inclusion in
* OpenLDAP Software, based on prior work by Neil Dunbar (HP).
* This work was sponsored by the Hewlett-Packard Company.
*/
#include "portable.h"
/* This file implements "Password Policy for LDAP Directories",
* based on draft behera-ldap-password-policy-09
*/
#ifdef SLAPD_OVER_PPOLICY
#include <ldap.h>
#include "lutil.h"
#include "slap.h"
#ifdef SLAPD_MODULES
#define LIBLTDL_DLL_IMPORT /* Win32: don't re-export libltdl's symbols */
#include <ltdl.h>
#endif
#include <ac/errno.h>
#include <ac/time.h>
#include <ac/string.h>
#include <ac/ctype.h>
#include "config.h"
#ifndef MODULE_NAME_SZ
#define MODULE_NAME_SZ 256
#endif
#ifndef PPOLICY_DEFAULT_MAXRECORDED_FAILURE
#define PPOLICY_DEFAULT_MAXRECORDED_FAILURE 5
#endif
/* Per-instance configuration information */
typedef struct pp_info {
struct berval def_policy; /* DN of default policy subentry */
int use_lockout; /* send AccountLocked result? */
int hash_passwords; /* transparently hash cleartext pwds */
int forward_updates; /* use frontend for policy state updates */
} pp_info;
/* Our per-connection info - note, it is not per-instance, it is
* used by all instances
*/
typedef struct pw_conn {
struct berval dn; /* DN of restricted user */
} pw_conn;
static pw_conn *pwcons;
static int ppolicy_cid;
static int ov_count;
typedef struct pass_policy {
AttributeDescription *ad; /* attribute to which the policy applies */
int pwdMinAge; /* minimum time (seconds) until passwd can change */
int pwdMaxAge; /* time in seconds until pwd will expire after change */
int pwdInHistory; /* number of previous passwords kept */
int pwdCheckQuality; /* 0 = don't check quality, 1 = check if possible,
2 = check mandatory; fail if not possible */
int pwdMinLength; /* minimum number of chars in password */
int pwdExpireWarning; /* number of seconds that warning controls are
sent before a password expires */
int pwdGraceAuthNLimit; /* number of times you can log in with an
expired password */
int pwdLockout; /* 0 = do not lockout passwords, 1 = lock them out */
int pwdLockoutDuration; /* time in seconds a password is locked out for */
int pwdMaxFailure; /* number of failed binds allowed before lockout */
int pwdMaxRecordedFailure; /* number of failed binds to store */
int pwdFailureCountInterval; /* number of seconds before failure
counts are zeroed */
int pwdMustChange; /* 0 = users can use admin set password
1 = users must change password after admin set */
int pwdAllowUserChange; /* 0 = users cannot change their passwords
1 = users can change them */
int pwdSafeModify; /* 0 = old password doesn't need to come
with password change request
1 = password change must supply existing pwd */
char pwdCheckModule[MODULE_NAME_SZ]; /* name of module to dynamically
load to check password */
} PassPolicy;
typedef struct pw_hist {
time_t t; /* timestamp of history entry */
struct berval pw; /* old password hash */
struct berval bv; /* text of entire entry */
struct pw_hist *next;
} pw_hist;
/* Operational attributes */
static AttributeDescription *ad_pwdChangedTime, *ad_pwdAccountLockedTime,
*ad_pwdFailureTime, *ad_pwdHistory, *ad_pwdGraceUseTime, *ad_pwdReset,
*ad_pwdPolicySubentry;
static struct schema_info {
char *def;
AttributeDescription **ad;
} pwd_OpSchema[] = {
{ "( 1.3.6.1.4.1.42.2.27.8.1.16 "
"NAME ( 'pwdChangedTime' ) "
"DESC 'The time the password was last changed' "
"EQUALITY generalizedTimeMatch "
"ORDERING generalizedTimeOrderingMatch "
"SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
"SINGLE-VALUE NO-USER-MODIFICATION USAGE directoryOperation )",
&ad_pwdChangedTime },
{ "( 1.3.6.1.4.1.42.2.27.8.1.17 "
"NAME ( 'pwdAccountLockedTime' ) "
"DESC 'The time an user account was locked' "
"EQUALITY generalizedTimeMatch "
"ORDERING generalizedTimeOrderingMatch "
"SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
"SINGLE-VALUE "
#if 0
/* Not until Relax control is released */
"NO-USER-MODIFICATION "
#endif
"USAGE directoryOperation )",
&ad_pwdAccountLockedTime },
{ "( 1.3.6.1.4.1.42.2.27.8.1.19 "
"NAME ( 'pwdFailureTime' ) "
"DESC 'The timestamps of the last consecutive authentication failures' "
"EQUALITY generalizedTimeMatch "
"ORDERING generalizedTimeOrderingMatch "
"SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
"NO-USER-MODIFICATION USAGE directoryOperation )",
&ad_pwdFailureTime },
{ "( 1.3.6.1.4.1.42.2.27.8.1.20 "
"NAME ( 'pwdHistory' ) "
"DESC 'The history of users passwords' "
"EQUALITY octetStringMatch "
"SYNTAX 1.3.6.1.4.1.1466.115.121.1.40 "
"NO-USER-MODIFICATION USAGE directoryOperation )",
&ad_pwdHistory },
{ "( 1.3.6.1.4.1.42.2.27.8.1.21 "
"NAME ( 'pwdGraceUseTime' ) "
"DESC 'The timestamps of the grace login once the password has expired' "
"EQUALITY generalizedTimeMatch "
"SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
"NO-USER-MODIFICATION USAGE directoryOperation )",
&ad_pwdGraceUseTime },
{ "( 1.3.6.1.4.1.42.2.27.8.1.22 "
"NAME ( 'pwdReset' ) "
"DESC 'The indication that the password has been reset' "
"EQUALITY booleanMatch "
"SYNTAX 1.3.6.1.4.1.1466.115.121.1.7 "
"SINGLE-VALUE USAGE directoryOperation )",
&ad_pwdReset },
{ "( 1.3.6.1.4.1.42.2.27.8.1.23 "
"NAME ( 'pwdPolicySubentry' ) "
"DESC 'The pwdPolicy subentry in effect for this object' "
"EQUALITY distinguishedNameMatch "
"SYNTAX 1.3.6.1.4.1.1466.115.121.1.12 "
"SINGLE-VALUE "
#if 0
/* Not until Relax control is released */
"NO-USER-MODIFICATION "
#endif
"USAGE directoryOperation )",
&ad_pwdPolicySubentry },
{ NULL, NULL }
};
/* User attributes */
static AttributeDescription *ad_pwdMinAge, *ad_pwdMaxAge, *ad_pwdInHistory,
*ad_pwdCheckQuality, *ad_pwdMinLength, *ad_pwdMaxFailure,
*ad_pwdGraceAuthNLimit, *ad_pwdExpireWarning, *ad_pwdLockoutDuration,
*ad_pwdFailureCountInterval, *ad_pwdCheckModule, *ad_pwdLockout,
*ad_pwdMustChange, *ad_pwdAllowUserChange, *ad_pwdSafeModify,
*ad_pwdAttribute, *ad_pwdMaxRecordedFailure;
#define TAB(name) { #name, &ad_##name }
static struct schema_info pwd_UsSchema[] = {
TAB(pwdAttribute),
TAB(pwdMinAge),
TAB(pwdMaxAge),
TAB(pwdInHistory),
TAB(pwdCheckQuality),
TAB(pwdMinLength),
TAB(pwdMaxFailure),
TAB(pwdMaxRecordedFailure),
TAB(pwdGraceAuthNLimit),
TAB(pwdExpireWarning),
TAB(pwdLockout),
TAB(pwdLockoutDuration),
TAB(pwdFailureCountInterval),
TAB(pwdCheckModule),
TAB(pwdMustChange),
TAB(pwdAllowUserChange),
TAB(pwdSafeModify),
{ NULL, NULL }
};
static ldap_pvt_thread_mutex_t chk_syntax_mutex;
enum {
PPOLICY_DEFAULT = 1,
PPOLICY_HASH_CLEARTEXT,
PPOLICY_USE_LOCKOUT
};
static ConfigDriver ppolicy_cf_default;
static ConfigTable ppolicycfg[] = {
{ "ppolicy_default", "policyDN", 2, 2, 0,
ARG_DN|ARG_QUOTE|ARG_MAGIC|PPOLICY_DEFAULT, ppolicy_cf_default,
"( OLcfgOvAt:12.1 NAME 'olcPPolicyDefault' "
"DESC 'DN of a pwdPolicy object for uncustomized objects' "
"SYNTAX OMsDN SINGLE-VALUE )", NULL, NULL },
{ "ppolicy_hash_cleartext", "on|off", 1, 2, 0,
ARG_ON_OFF|ARG_OFFSET|PPOLICY_HASH_CLEARTEXT,
(void *)offsetof(pp_info,hash_passwords),
"( OLcfgOvAt:12.2 NAME 'olcPPolicyHashCleartext' "
"DESC 'Hash passwords on add or modify' "
"SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
{ "ppolicy_forward_updates", "on|off", 1, 2, 0,
ARG_ON_OFF|ARG_OFFSET,
(void *)offsetof(pp_info,forward_updates),
"( OLcfgOvAt:12.4 NAME 'olcPPolicyForwardUpdates' "
"DESC 'Allow policy state updates to be forwarded via updateref' "
"SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
{ "ppolicy_use_lockout", "on|off", 1, 2, 0,
ARG_ON_OFF|ARG_OFFSET|PPOLICY_USE_LOCKOUT,
(void *)offsetof(pp_info,use_lockout),
"( OLcfgOvAt:12.3 NAME 'olcPPolicyUseLockout' "
"DESC 'Warn clients with AccountLocked' "
"SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
{ NULL, NULL, 0, 0, 0, ARG_IGNORED }
};
static ConfigOCs ppolicyocs[] = {
{ "( OLcfgOvOc:12.1 "
"NAME 'olcPPolicyConfig' "
"DESC 'Password Policy configuration' "
"SUP olcOverlayConfig "
"MAY ( olcPPolicyDefault $ olcPPolicyHashCleartext $ "
"olcPPolicyUseLockout $ olcPPolicyForwardUpdates ) )",
Cft_Overlay, ppolicycfg },
{ NULL, 0, NULL }
};
static int
ppolicy_cf_default( ConfigArgs *c )
{
slap_overinst *on = (slap_overinst *)c->bi;
pp_info *pi = (pp_info *)on->on_bi.bi_private;
int rc = ARG_BAD_CONF;
assert ( c->type == PPOLICY_DEFAULT );
Debug(LDAP_DEBUG_TRACE, "==> ppolicy_cf_default\n", 0, 0, 0);
switch ( c->op ) {
case SLAP_CONFIG_EMIT:
Debug(LDAP_DEBUG_TRACE, "==> ppolicy_cf_default emit\n", 0, 0, 0);
rc = 0;
if ( !BER_BVISEMPTY( &pi->def_policy )) {
rc = value_add_one( &c->rvalue_vals,
&pi->def_policy );
if ( rc ) return rc;
rc = value_add_one( &c->rvalue_nvals,
&pi->def_policy );
}
break;
case LDAP_MOD_DELETE:
Debug(LDAP_DEBUG_TRACE, "==> ppolicy_cf_default delete\n", 0, 0, 0);
if ( pi->def_policy.bv_val ) {
ber_memfree ( pi->def_policy.bv_val );
pi->def_policy.bv_val = NULL;
}
pi->def_policy.bv_len = 0;
rc = 0;
break;
case SLAP_CONFIG_ADD:
/* fallthrough to LDAP_MOD_ADD */
case LDAP_MOD_ADD:
Debug(LDAP_DEBUG_TRACE, "==> ppolicy_cf_default add\n", 0, 0, 0);
if ( pi->def_policy.bv_val ) {
ber_memfree ( pi->def_policy.bv_val );
}
pi->def_policy = c->value_ndn;
ber_memfree( c->value_dn.bv_val );
BER_BVZERO( &c->value_dn );
BER_BVZERO( &c->value_ndn );
rc = 0;
break;
default:
abort ();
}
return rc;
}
static time_t
parse_time( char *atm )
{
struct lutil_tm tm;
struct lutil_timet tt;
time_t ret = (time_t)-1;
if ( lutil_parsetime( atm, &tm ) == 0) {
lutil_tm2time( &tm, &tt );
ret = tt.tt_sec;
}
return ret;
}
static int
account_locked( Operation *op, Entry *e,
PassPolicy *pp, Modifications **mod )
{
Attribute *la;
assert(mod != NULL);
if ( !pp->pwdLockout )
return 0;
if ( (la = attr_find( e->e_attrs, ad_pwdAccountLockedTime )) != NULL ) {
BerVarray vals = la->a_nvals;
/*
* there is a lockout stamp - we now need to know if it's
* a valid one.
*/
if (vals[0].bv_val != NULL) {
time_t then, now;
Modifications *m;
if (!pp->pwdLockoutDuration)
return 1;
if ((then = parse_time( vals[0].bv_val )) == (time_t)0)
return 1;
now = slap_get_time();
if (now < then + pp->pwdLockoutDuration)
return 1;
m = ch_calloc( sizeof(Modifications), 1 );
m->sml_op = LDAP_MOD_DELETE;
m->sml_flags = 0;
m->sml_type = ad_pwdAccountLockedTime->ad_cname;
m->sml_desc = ad_pwdAccountLockedTime;
m->sml_next = *mod;
*mod = m;
}
}
return 0;
}
/* IMPLICIT TAGS, all context-specific */
#define PPOLICY_WARNING 0xa0L /* constructed + 0 */
#define PPOLICY_ERROR 0x81L /* primitive + 1 */
#define PPOLICY_EXPIRE 0x80L /* primitive + 0 */
#define PPOLICY_GRACE 0x81L /* primitive + 1 */
static const char ppolicy_ctrl_oid[] = LDAP_CONTROL_PASSWORDPOLICYRESPONSE;
static LDAPControl *
create_passcontrol( Operation *op, int exptime, int grace, LDAPPasswordPolicyError err )
{
BerElementBuffer berbuf, bb2;
BerElement *ber = (BerElement *) &berbuf, *b2 = (BerElement *) &bb2;
LDAPControl c = { 0 }, *cp;
struct berval bv;
int rc;
BER_BVZERO( &c.ldctl_value );
ber_init2( ber, NULL, LBER_USE_DER );
ber_printf( ber, "{" /*}*/ );
if ( exptime >= 0 ) {
ber_init2( b2, NULL, LBER_USE_DER );
ber_printf( b2, "ti", PPOLICY_EXPIRE, exptime );
rc = ber_flatten2( b2, &bv, 1 );
(void)ber_free_buf(b2);
if (rc == -1) {
cp = NULL;
goto fail;
}
ber_printf( ber, "tO", PPOLICY_WARNING, &bv );
ch_free( bv.bv_val );
} else if ( grace > 0 ) {
ber_init2( b2, NULL, LBER_USE_DER );
ber_printf( b2, "ti", PPOLICY_GRACE, grace );
rc = ber_flatten2( b2, &bv, 1 );
(void)ber_free_buf(b2);
if (rc == -1) {
cp = NULL;
goto fail;
}
ber_printf( ber, "tO", PPOLICY_WARNING, &bv );
ch_free( bv.bv_val );
}
if (err != PP_noError ) {
ber_printf( ber, "te", PPOLICY_ERROR, err );
}
ber_printf( ber, /*{*/ "N}" );
if (ber_flatten2( ber, &c.ldctl_value, 0 ) == -1) {
return NULL;
}
cp = op->o_tmpalloc( sizeof( LDAPControl ) + c.ldctl_value.bv_len, op->o_tmpmemctx );
cp->ldctl_oid = (char *)ppolicy_ctrl_oid;
cp->ldctl_iscritical = 0;
cp->ldctl_value.bv_val = (char *)&cp[1];
cp->ldctl_value.bv_len = c.ldctl_value.bv_len;
AC_MEMCPY( cp->ldctl_value.bv_val, c.ldctl_value.bv_val, c.ldctl_value.bv_len );
fail:
(void)ber_free_buf(ber);
return cp;
}
static LDAPControl **
add_passcontrol( Operation *op, SlapReply *rs, LDAPControl *ctrl )
{
LDAPControl **ctrls, **oldctrls = rs->sr_ctrls;
int n;
n = 0;
if ( oldctrls ) {
for ( ; oldctrls[n]; n++ )
;
}
n += 2;
ctrls = op->o_tmpcalloc( sizeof( LDAPControl * ), n, op->o_tmpmemctx );
n = 0;
if ( oldctrls ) {
for ( ; oldctrls[n]; n++ ) {
ctrls[n] = oldctrls[n];
}
}
ctrls[n] = ctrl;
ctrls[n+1] = NULL;
rs->sr_ctrls = ctrls;
return oldctrls;
}
static void
ppolicy_get_default( PassPolicy *pp )
{
memset( pp, 0, sizeof(PassPolicy) );
pp->ad = slap_schema.si_ad_userPassword;
/* Users can change their own password by default */
pp->pwdAllowUserChange = 1;
if ( !pp->pwdMaxRecordedFailure )
pp->pwdMaxRecordedFailure = PPOLICY_DEFAULT_MAXRECORDED_FAILURE;
}
static void
ppolicy_get( Operation *op, Entry *e, PassPolicy *pp )
{
slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
pp_info *pi = on->on_bi.bi_private;
Attribute *a;
BerVarray vals;
int rc;
Entry *pe = NULL;
#if 0
const char *text;
#endif
ppolicy_get_default( pp );
if ((a = attr_find( e->e_attrs, ad_pwdPolicySubentry )) == NULL) {
/*
* entry has no password policy assigned - use default
*/
vals = &pi->def_policy;
if ( !vals->bv_val )
goto defaultpol;
} else {
vals = a->a_nvals;
if (vals[0].bv_val == NULL) {
Debug( LDAP_DEBUG_ANY,
"ppolicy_get: NULL value for policySubEntry\n", 0, 0, 0 );
goto defaultpol;
}
}
op->o_bd->bd_info = (BackendInfo *)on->on_info;
rc = be_entry_get_rw( op, vals, NULL, NULL, 0, &pe );
op->o_bd->bd_info = (BackendInfo *)on;
if ( rc ) goto defaultpol;
#if 0 /* Only worry about userPassword for now */
if ((a = attr_find( pe->e_attrs, ad_pwdAttribute )))
slap_bv2ad( &a->a_vals[0], &pp->ad, &text );
#endif
if ( ( a = attr_find( pe->e_attrs, ad_pwdMinAge ) )
&& lutil_atoi( &pp->pwdMinAge, a->a_vals[0].bv_val ) != 0 )
goto defaultpol;
if ( ( a = attr_find( pe->e_attrs, ad_pwdMaxAge ) )
&& lutil_atoi( &pp->pwdMaxAge, a->a_vals[0].bv_val ) != 0 )
goto defaultpol;
if ( ( a = attr_find( pe->e_attrs, ad_pwdInHistory ) )
&& lutil_atoi( &pp->pwdInHistory, a->a_vals[0].bv_val ) != 0 )
goto defaultpol;
if ( ( a = attr_find( pe->e_attrs, ad_pwdCheckQuality ) )
&& lutil_atoi( &pp->pwdCheckQuality, a->a_vals[0].bv_val ) != 0 )
goto defaultpol;
if ( ( a = attr_find( pe->e_attrs, ad_pwdMinLength ) )
&& lutil_atoi( &pp->pwdMinLength, a->a_vals[0].bv_val ) != 0 )
goto defaultpol;
if ( ( a = attr_find( pe->e_attrs, ad_pwdMaxFailure ) )
&& lutil_atoi( &pp->pwdMaxFailure, a->a_vals[0].bv_val ) != 0 )
goto defaultpol;
if ( ( a = attr_find( pe->e_attrs, ad_pwdMaxRecordedFailure ) )
&& lutil_atoi( &pp->pwdMaxRecordedFailure, a->a_vals[0].bv_val ) != 0 )
goto defaultpol;
if ( ( a = attr_find( pe->e_attrs, ad_pwdGraceAuthNLimit ) )
&& lutil_atoi( &pp->pwdGraceAuthNLimit, a->a_vals[0].bv_val ) != 0 )
goto defaultpol;
if ( ( a = attr_find( pe->e_attrs, ad_pwdExpireWarning ) )
&& lutil_atoi( &pp->pwdExpireWarning, a->a_vals[0].bv_val ) != 0 )
goto defaultpol;
if ( ( a = attr_find( pe->e_attrs, ad_pwdFailureCountInterval ) )
&& lutil_atoi( &pp->pwdFailureCountInterval, a->a_vals[0].bv_val ) != 0 )
goto defaultpol;
if ( ( a = attr_find( pe->e_attrs, ad_pwdLockoutDuration ) )
&& lutil_atoi( &pp->pwdLockoutDuration, a->a_vals[0].bv_val ) != 0 )
goto defaultpol;
if ( ( a = attr_find( pe->e_attrs, ad_pwdCheckModule ) ) ) {
strncpy( pp->pwdCheckModule, a->a_vals[0].bv_val,
sizeof(pp->pwdCheckModule) );
pp->pwdCheckModule[sizeof(pp->pwdCheckModule)-1] = '\0';
}
if ((a = attr_find( pe->e_attrs, ad_pwdLockout )))
pp->pwdLockout = bvmatch( &a->a_nvals[0], &slap_true_bv );
if ((a = attr_find( pe->e_attrs, ad_pwdMustChange )))
pp->pwdMustChange = bvmatch( &a->a_nvals[0], &slap_true_bv );
if ((a = attr_find( pe->e_attrs, ad_pwdAllowUserChange )))
pp->pwdAllowUserChange = bvmatch( &a->a_nvals[0], &slap_true_bv );
if ((a = attr_find( pe->e_attrs, ad_pwdSafeModify )))
pp->pwdSafeModify = bvmatch( &a->a_nvals[0], &slap_true_bv );
if ( pp->pwdMaxRecordedFailure < pp->pwdMaxFailure )
pp->pwdMaxRecordedFailure = pp->pwdMaxFailure;
if ( !pp->pwdMaxRecordedFailure )
pp->pwdMaxRecordedFailure = PPOLICY_DEFAULT_MAXRECORDED_FAILURE;
op->o_bd->bd_info = (BackendInfo *)on->on_info;
be_entry_release_r( op, pe );
op->o_bd->bd_info = (BackendInfo *)on;
return;
defaultpol:
if ( pe ) {
op->o_bd->bd_info = (BackendInfo *)on->on_info;
be_entry_release_r( op, pe );
op->o_bd->bd_info = (BackendInfo *)on;
}
Debug( LDAP_DEBUG_TRACE,
"ppolicy_get: using default policy\n", 0, 0, 0 );
ppolicy_get_default( pp );
return;
}
static int
password_scheme( struct berval *cred, struct berval *sch )
{
int e;
assert( cred != NULL );
if (sch) {
sch->bv_val = NULL;
sch->bv_len = 0;
}
if ((cred->bv_len == 0) || (cred->bv_val == NULL) ||
(cred->bv_val[0] != '{')) return LDAP_OTHER;
for(e = 1; cred->bv_val[e] && cred->bv_val[e] != '}'; e++);
if (cred->bv_val[e]) {
int rc;
rc = lutil_passwd_scheme( cred->bv_val );
if (rc) {
if (sch) {
sch->bv_val = cred->bv_val;
sch->bv_len = e;
}
return LDAP_SUCCESS;
}
}
return LDAP_OTHER;
}
static int
check_password_quality( struct berval *cred, PassPolicy *pp, LDAPPasswordPolicyError *err, Entry *e, char **txt )
{
int rc = LDAP_SUCCESS, ok = LDAP_SUCCESS;
char *ptr;
struct berval sch;
assert( cred != NULL );
assert( pp != NULL );
assert( txt != NULL );
ptr = cred->bv_val;
*txt = NULL;
if ((cred->bv_len == 0) || (pp->pwdMinLength > cred->bv_len)) {
rc = LDAP_CONSTRAINT_VIOLATION;
if ( err ) *err = PP_passwordTooShort;
return rc;
}
/*
* We need to know if the password is already hashed - if so
* what scheme is it. The reason being that the "hash" of
* {cleartext} still allows us to check the password.
*/
rc = password_scheme( cred, &sch );
if (rc == LDAP_SUCCESS) {
if ((sch.bv_val) && (strncasecmp( sch.bv_val, "{cleartext}",
sch.bv_len ) == 0)) {
/*
* We can check the cleartext "hash"
*/
ptr = cred->bv_val + sch.bv_len;
} else {
/* everything else, we can't check */
if (pp->pwdCheckQuality == 2) {
rc = LDAP_CONSTRAINT_VIOLATION;
if (err) *err = PP_insufficientPasswordQuality;
return rc;
}
/*
* We can't check the syntax of the password, but it's not
* mandatory (according to the policy), so we return success.
*/
return LDAP_SUCCESS;
}
}
rc = LDAP_SUCCESS;
if (pp->pwdCheckModule[0]) {
#ifdef SLAPD_MODULES
lt_dlhandle mod;
const char *err;
if ((mod = lt_dlopen( pp->pwdCheckModule )) == NULL) {
err = lt_dlerror();
Debug(LDAP_DEBUG_ANY,
"check_password_quality: lt_dlopen failed: (%s) %s.\n",
pp->pwdCheckModule, err, 0 );
ok = LDAP_OTHER; /* internal error */
} else {
/* FIXME: the error message ought to be passed thru a
* struct berval, with preallocated buffer and size
* passed in. Module can still allocate a buffer for
* it if the provided one is too small.
*/
int (*prog)( char *passwd, char **text, Entry *ent );
if ((prog = lt_dlsym( mod, "check_password" )) == NULL) {
err = lt_dlerror();
Debug(LDAP_DEBUG_ANY,
"check_password_quality: lt_dlsym failed: (%s) %s.\n",
pp->pwdCheckModule, err, 0 );
ok = LDAP_OTHER;
} else {
ldap_pvt_thread_mutex_lock( &chk_syntax_mutex );
ok = prog( ptr, txt, e );
ldap_pvt_thread_mutex_unlock( &chk_syntax_mutex );
if (ok != LDAP_SUCCESS) {
Debug(LDAP_DEBUG_ANY,
"check_password_quality: module error: (%s) %s.[%d]\n",
pp->pwdCheckModule, *txt ? *txt : "", ok );
}
}
lt_dlclose( mod );
}
#else
Debug(LDAP_DEBUG_ANY, "check_password_quality: external modules not "
"supported. pwdCheckModule ignored.\n", 0, 0, 0);
#endif /* SLAPD_MODULES */
}
if (ok != LDAP_SUCCESS) {
rc = LDAP_CONSTRAINT_VIOLATION;
if (err) *err = PP_insufficientPasswordQuality;
}
return rc;
}
static int
parse_pwdhistory( struct berval *bv, char **oid, time_t *oldtime, struct berval *oldpw )
{
char *ptr;
struct berval nv, npw;
ber_len_t i, j;
assert (bv && (bv->bv_len > 0) && (bv->bv_val) && oldtime && oldpw );
if ( oid ) {
*oid = 0;
}
*oldtime = (time_t)-1;
BER_BVZERO( oldpw );
ber_dupbv( &nv, bv );
/* first get the time field */
for ( i = 0; (i < nv.bv_len) && (nv.bv_val[i] != '#'); i++ )
;
if ( i == nv.bv_len ) {
goto exit_failure; /* couldn't locate the '#' separator */
}
nv.bv_val[i++] = '\0'; /* terminate the string & move to next field */
ptr = nv.bv_val;
*oldtime = parse_time( ptr );
if (*oldtime == (time_t)-1) {
goto exit_failure;
}
/* get the OID field */
for (ptr = &(nv.bv_val[i]); (i < nv.bv_len) && (nv.bv_val[i] != '#'); i++ )
;
if ( i == nv.bv_len ) {
goto exit_failure; /* couldn't locate the '#' separator */
}
nv.bv_val[i++] = '\0'; /* terminate the string & move to next field */
if ( oid ) {
*oid = ber_strdup( ptr );
}
/* get the length field */
for ( ptr = &(nv.bv_val[i]); (i < nv.bv_len) && (nv.bv_val[i] != '#'); i++ )
;
if ( i == nv.bv_len ) {
goto exit_failure; /* couldn't locate the '#' separator */
}
nv.bv_val[i++] = '\0'; /* terminate the string & move to next field */
oldpw->bv_len = strtol( ptr, NULL, 10 );
if (errno == ERANGE) {
goto exit_failure;
}
/* lastly, get the octets of the string */
for ( j = i, ptr = &(nv.bv_val[i]); i < nv.bv_len; i++ )
;
if ( i - j != oldpw->bv_len) {
goto exit_failure; /* length is wrong */
}
npw.bv_val = ptr;
npw.bv_len = oldpw->bv_len;
ber_dupbv( oldpw, &npw );
ber_memfree( nv.bv_val );
return LDAP_SUCCESS;
exit_failure:;
if ( oid && *oid ) {
ber_memfree(*oid);
*oid = NULL;
}
if ( oldpw->bv_val ) {
ber_memfree( oldpw->bv_val);
BER_BVZERO( oldpw );
}
ber_memfree( nv.bv_val );
return LDAP_OTHER;
}
static void
add_to_pwd_history( pw_hist **l, time_t t,
struct berval *oldpw, struct berval *bv )
{
pw_hist *p, *p1, *p2;
if (!l) return;
p = ch_malloc( sizeof( pw_hist ));
p->pw = *oldpw;
ber_dupbv( &p->bv, bv );
p->t = t;
p->next = NULL;
if (*l == NULL) {
/* degenerate case */
*l = p;
return;
}
/*
* advance p1 and p2 such that p1 is the node before the
* new one, and p2 is the node after it
*/
for (p1 = NULL, p2 = *l; p2 && p2->t <= t; p1 = p2, p2=p2->next );
p->next = p2;
if (p1 == NULL) { *l = p; return; }
p1->next = p;
}
#ifndef MAX_PWD_HISTORY_SZ
#define MAX_PWD_HISTORY_SZ 1024
#endif /* MAX_PWD_HISTORY_SZ */
static void
make_pwd_history_value( char *timebuf, struct berval *bv, Attribute *pa )
{
char str[ MAX_PWD_HISTORY_SZ ];
int nlen;
snprintf( str, MAX_PWD_HISTORY_SZ,
"%s#%s#%lu#", timebuf,
pa->a_desc->ad_type->sat_syntax->ssyn_oid,
(unsigned long) pa->a_nvals[0].bv_len );
str[MAX_PWD_HISTORY_SZ-1] = 0;
nlen = strlen(str);
/*
* We have to assume that the string is a string of octets,
* not readable characters. In reality, yes, it probably is
* a readable (ie, base64) string, but we can't count on that
* Hence, while the first 3 fields of the password history
* are definitely readable (a timestamp, an OID and an integer
* length), the remaining octets of the actual password
* are deemed to be binary data.
*/
AC_MEMCPY( str + nlen, pa->a_nvals[0].bv_val, pa->a_nvals[0].bv_len );
nlen += pa->a_nvals[0].bv_len;
bv->bv_val = ch_malloc( nlen + 1 );
AC_MEMCPY( bv->bv_val, str, nlen );
bv->bv_val[nlen] = '\0';
bv->bv_len = nlen;
}
static void
free_pwd_history_list( pw_hist **l )
{
pw_hist *p;
if (!l) return;
p = *l;
while (p) {
pw_hist *pp = p->next;
free(p->pw.bv_val);
free(p->bv.bv_val);
free(p);
p = pp;
}
*l = NULL;
}
typedef struct ppbind {
slap_overinst *on;
int send_ctrl;
int set_restrict;
LDAPControl **oldctrls;
Modifications *mod;
LDAPPasswordPolicyError pErr;
PassPolicy pp;
} ppbind;
static void
ctrls_cleanup( Operation *op, SlapReply *rs, LDAPControl **oldctrls )
{
int n;
assert( rs->sr_ctrls != NULL );
assert( rs->sr_ctrls[0] != NULL );
for ( n = 0; rs->sr_ctrls[n]; n++ ) {
if ( rs->sr_ctrls[n]->ldctl_oid == ppolicy_ctrl_oid ) {
op->o_tmpfree( rs->sr_ctrls[n], op->o_tmpmemctx );
rs->sr_ctrls[n] = (LDAPControl *)(-1);
break;
}
}
if ( rs->sr_ctrls[n] == NULL ) {
/* missed? */
}
op->o_tmpfree( rs->sr_ctrls, op->o_tmpmemctx );
rs->sr_ctrls = oldctrls;
}
static int
ppolicy_ctrls_cleanup( Operation *op, SlapReply *rs )
{
ppbind *ppb = op->o_callback->sc_private;
if ( ppb->send_ctrl ) {
ctrls_cleanup( op, rs, ppb->oldctrls );
}
return SLAP_CB_CONTINUE;
}
static int
ppolicy_bind_response( Operation *op, SlapReply *rs )
{
ppbind *ppb = op->o_callback->sc_private;
slap_overinst *on = ppb->on;
Modifications *mod = ppb->mod, *m;
int pwExpired = 0;
int ngut = -1, warn = -1, age, rc;
Attribute *a;
time_t now, pwtime = (time_t)-1;
struct lutil_tm now_tm;
struct lutil_timet now_usec;
char nowstr[ LDAP_LUTIL_GENTIME_BUFSIZE ];
char nowstr_usec[ LDAP_LUTIL_GENTIME_BUFSIZE+8 ];
struct berval timestamp, timestamp_usec;
BackendInfo *bi = op->o_bd->bd_info;
Entry *e;
/* If we already know it's locked, just get on with it */
if ( ppb->pErr != PP_noError ) {
goto locked;
}
op->o_bd->bd_info = (BackendInfo *)on->on_info;
rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e );
op->o_bd->bd_info = bi;
if ( rc != LDAP_SUCCESS ) {
return SLAP_CB_CONTINUE;
}
ldap_pvt_gettime(&now_tm); /* stored for later consideration */
lutil_tm2time(&now_tm, &now_usec);
now = now_usec.tt_sec;
timestamp.bv_val = nowstr;
timestamp.bv_len = sizeof(nowstr);
slap_timestamp( &now, ×tamp );
/* Separate timestamp for pwdFailureTime with microsecond granularity */
strcpy(nowstr_usec, nowstr);
timestamp_usec.bv_val = nowstr_usec;
timestamp_usec.bv_len = timestamp.bv_len;
snprintf( timestamp_usec.bv_val + timestamp_usec.bv_len-1, sizeof(".123456Z"), ".%06dZ", now_usec.tt_usec );
timestamp_usec.bv_len += STRLENOF(".123456");
if ( rs->sr_err == LDAP_INVALID_CREDENTIALS ) {
int i = 0, fc = 0;
m = ch_calloc( sizeof(Modifications), 1 );
m->sml_op = LDAP_MOD_ADD;
m->sml_flags = 0;
m->sml_type = ad_pwdFailureTime->ad_cname;
m->sml_desc = ad_pwdFailureTime;
m->sml_numvals = 1;
m->sml_values = ch_calloc( sizeof(struct berval), 2 );
m->sml_nvalues = ch_calloc( sizeof(struct berval), 2 );
ber_dupbv( &m->sml_values[0], ×tamp_usec );
ber_dupbv( &m->sml_nvalues[0], ×tamp_usec );
m->sml_next = mod;
mod = m;
/*
* Count the pwdFailureTimes - if it's
* greater than the policy pwdMaxFailure,
* then lock the account.
*/
if ((a = attr_find( e->e_attrs, ad_pwdFailureTime )) != NULL) {
for(i=0; a->a_nvals[i].bv_val; i++) {
/*
* If the interval is 0, then failures
* stay on the record until explicitly
* reset by successful authentication.
*/
if (ppb->pp.pwdFailureCountInterval == 0) {
fc++;
} else if (now <=
parse_time(a->a_nvals[i].bv_val) +
ppb->pp.pwdFailureCountInterval) {
fc++;
}
/*
* We only count those failures
* which are not due to expire.
*/
}
/* Do we have too many timestamps? If so, delete some values.
* We don't bother to sort the values here. OpenLDAP keeps the
* values in order by default. Fundamentally, relying on the
* information here is wrong anyway; monitoring systems should
* be tracking Bind failures in syslog, not here.
*/
if (a->a_numvals >= ppb->pp.pwdMaxRecordedFailure) {
int j = ppb->pp.pwdMaxRecordedFailure-1;
/* If more than 2x, cheaper to perform a Replace */
if (a->a_numvals >= 2 * ppb->pp.pwdMaxRecordedFailure) {
struct berval v, nv;
/* Change the mod we constructed above */
m->sml_op = LDAP_MOD_REPLACE;
m->sml_numvals = ppb->pp.pwdMaxRecordedFailure;
v = m->sml_values[0];
nv = m->sml_nvalues[0];
ch_free(m->sml_values);
ch_free(m->sml_nvalues);
m->sml_values = ch_calloc( sizeof(struct berval), ppb->pp.pwdMaxRecordedFailure+1 );
m->sml_nvalues = ch_calloc( sizeof(struct berval), ppb->pp.pwdMaxRecordedFailure+1 );
for (i=0; i<j; i++) {
ber_dupbv(&m->sml_values[i], &a->a_vals[a->a_numvals-j+i]);
ber_dupbv(&m->sml_nvalues[i], &a->a_nvals[a->a_numvals-j+i]);
}
m->sml_values[i] = v;
m->sml_nvalues[i] = nv;
} else {
/* else just delete some */
m = ch_calloc( sizeof(Modifications), 1 );
m->sml_op = LDAP_MOD_DELETE;
m->sml_type = ad_pwdFailureTime->ad_cname;
m->sml_desc = ad_pwdFailureTime;
m->sml_numvals = a->a_numvals - j;
m->sml_values = ch_calloc( sizeof(struct berval), m->sml_numvals+1 );
m->sml_nvalues = ch_calloc( sizeof(struct berval), m->sml_numvals+1 );
for (i=0; i<m->sml_numvals; i++) {
ber_dupbv(&m->sml_values[i], &a->a_vals[i]);
ber_dupbv(&m->sml_nvalues[i], &a->a_nvals[i]);
}
m->sml_next = mod;
mod = m;
}
}
}
if ((ppb->pp.pwdMaxFailure > 0) &&
(fc >= ppb->pp.pwdMaxFailure - 1)) {
/*
* We subtract 1 from the failure max
* because the new failure entry hasn't
* made it to the entry yet.
*/
m = ch_calloc( sizeof(Modifications), 1 );
m->sml_op = LDAP_MOD_REPLACE;
m->sml_flags = 0;
m->sml_type = ad_pwdAccountLockedTime->ad_cname;
m->sml_desc = ad_pwdAccountLockedTime;
m->sml_numvals = 1;
m->sml_values = ch_calloc( sizeof(struct berval), 2 );
m->sml_nvalues = ch_calloc( sizeof(struct berval), 2 );
ber_dupbv( &m->sml_values[0], ×tamp );
ber_dupbv( &m->sml_nvalues[0], ×tamp );
m->sml_next = mod;
mod = m;
}
} else if ( rs->sr_err == LDAP_SUCCESS ) {
if ((a = attr_find( e->e_attrs, ad_pwdChangedTime )) != NULL)
pwtime = parse_time( a->a_nvals[0].bv_val );
/* delete all pwdFailureTimes */
if ( attr_find( e->e_attrs, ad_pwdFailureTime )) {
m = ch_calloc( sizeof(Modifications), 1 );
m->sml_op = LDAP_MOD_DELETE;
m->sml_flags = 0;
m->sml_type = ad_pwdFailureTime->ad_cname;
m->sml_desc = ad_pwdFailureTime;
m->sml_next = mod;
mod = m;
}
/*
* check to see if the password must be changed
*/
if ( ppb->pp.pwdMustChange &&
(a = attr_find( e->e_attrs, ad_pwdReset )) &&
bvmatch( &a->a_nvals[0], &slap_true_bv ) )
{
/*
* need to inject client controls here to give
* more information. For the moment, we ensure
* that we are disallowed from doing anything
* other than change password.
*/
if ( ppb->set_restrict ) {
ber_dupbv( &pwcons[op->o_conn->c_conn_idx].dn,
&op->o_conn->c_ndn );
}
ppb->pErr = PP_changeAfterReset;
} else {
/*
* the password does not need to be changed, so
* we now check whether the password has expired.
*
* We can skip this bit if passwords don't age in
* the policy. Also, if there was no pwdChangedTime
* attribute in the entry, the password never expires.
*/
if (ppb->pp.pwdMaxAge == 0) goto grace;
if (pwtime != (time_t)-1) {
/*
* Check: was the last change time of
* the password older than the maximum age
* allowed. (Ignore case 2 from I-D, it's just silly.)
*/
if (now - pwtime > ppb->pp.pwdMaxAge ) pwExpired = 1;
}
}
grace:
if (!pwExpired) goto check_expiring_password;
if ((a = attr_find( e->e_attrs, ad_pwdGraceUseTime )) == NULL)
ngut = ppb->pp.pwdGraceAuthNLimit;
else {
for(ngut=0; a->a_nvals[ngut].bv_val; ngut++);
ngut = ppb->pp.pwdGraceAuthNLimit - ngut;
}
/*
* ngut is the number of remaining grace logins
*/
Debug( LDAP_DEBUG_ANY,
"ppolicy_bind: Entry %s has an expired password: %d grace logins\n",
e->e_name.bv_val, ngut, 0);
if (ngut < 1) {
ppb->pErr = PP_passwordExpired;
rs->sr_err = LDAP_INVALID_CREDENTIALS;
goto done;
}
/*
* Add a grace user time to the entry
*/
m = ch_calloc( sizeof(Modifications), 1 );
m->sml_op = LDAP_MOD_ADD;
m->sml_flags = 0;
m->sml_type = ad_pwdGraceUseTime->ad_cname;
m->sml_desc = ad_pwdGraceUseTime;
m->sml_numvals = 1;
m->sml_values = ch_calloc( sizeof(struct berval), 2 );
m->sml_nvalues = ch_calloc( sizeof(struct berval), 2 );
ber_dupbv( &m->sml_values[0], ×tamp );
ber_dupbv( &m->sml_nvalues[0], ×tamp );
m->sml_next = mod;
mod = m;
check_expiring_password:
/*
* Now we need to check to see
* if it is about to expire, and if so, should the user
* be warned about it in the password policy control.
*
* If the password has expired, and we're in the grace period, then
* we don't need to do this bit. Similarly, if we don't have password
* aging, then there's no need to do this bit either.
*/
if ((ppb->pp.pwdMaxAge < 1) || (pwExpired) || (ppb->pp.pwdExpireWarning < 1))
goto done;
age = (int)(now - pwtime);
/*
* We know that there is a password Change Time attribute - if
* there wasn't, then the pwdExpired value would be true, unless
* there is no password aging - and if there is no password aging,
* then this section isn't called anyway - you can't have an
* expiring password if there's no limit to expire.
*/
if (ppb->pp.pwdMaxAge - age < ppb->pp.pwdExpireWarning ) {
/*
* Set the warning value.
*/
warn = ppb->pp.pwdMaxAge - age; /* seconds left until expiry */
if (warn < 0) warn = 0; /* something weird here - why is pwExpired not set? */
Debug( LDAP_DEBUG_ANY,
"ppolicy_bind: Setting warning for password expiry for %s = %d seconds\n",
op->o_req_dn.bv_val, warn, 0 );
}
}
done:
op->o_bd->bd_info = (BackendInfo *)on->on_info;
be_entry_release_r( op, e );
locked:
if ( mod ) {
Operation op2 = *op;
SlapReply r2 = { REP_RESULT };
slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
pp_info *pi = on->on_bi.bi_private;
LDAPControl c, *ca[2];
op2.o_tag = LDAP_REQ_MODIFY;
op2.o_callback = &cb;
op2.orm_modlist = mod;
op2.orm_no_opattrs = 0;
op2.o_dn = op->o_bd->be_rootdn;
op2.o_ndn = op->o_bd->be_rootndn;
/* If this server is a shadow and forward_updates is true,
* use the frontend to perform this modify. That will trigger
* the update referral, which can then be forwarded by the
* chain overlay. Obviously the updateref and chain overlay
* must be configured appropriately for this to be useful.
*/
if ( SLAP_SHADOW( op->o_bd ) && pi->forward_updates ) {
op2.o_bd = frontendDB;
/* Must use Relax control since these are no-user-mod */
op2.o_relax = SLAP_CONTROL_CRITICAL;
op2.o_ctrls = ca;
ca[0] = &c;
ca[1] = NULL;
BER_BVZERO( &c.ldctl_value );
c.ldctl_iscritical = 1;
c.ldctl_oid = LDAP_CONTROL_RELAX;
} else {
/* If not forwarding, don't update opattrs and don't replicate */
if ( SLAP_SINGLE_SHADOW( op->o_bd )) {
op2.orm_no_opattrs = 1;
op2.o_dont_replicate = 1;
}
op2.o_bd->bd_info = (BackendInfo *)on->on_info;
}
rc = op2.o_bd->be_modify( &op2, &r2 );
slap_mods_free( mod, 1 );
}
if ( ppb->send_ctrl ) {
LDAPControl *ctrl = NULL;
pp_info *pi = on->on_bi.bi_private;
/* Do we really want to tell that the account is locked? */
if ( ppb->pErr == PP_accountLocked && !pi->use_lockout ) {
ppb->pErr = PP_noError;
}
ctrl = create_passcontrol( op, warn, ngut, ppb->pErr );
ppb->oldctrls = add_passcontrol( op, rs, ctrl );
op->o_callback->sc_cleanup = ppolicy_ctrls_cleanup;
}
op->o_bd->bd_info = bi;
return SLAP_CB_CONTINUE;
}
static int
ppolicy_bind( Operation *op, SlapReply *rs )
{
slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
/* Reset lockout status on all Bind requests */
if ( !BER_BVISEMPTY( &pwcons[op->o_conn->c_conn_idx].dn )) {
ch_free( pwcons[op->o_conn->c_conn_idx].dn.bv_val );
BER_BVZERO( &pwcons[op->o_conn->c_conn_idx].dn );
}
/* Root bypasses policy */
if ( !be_isroot_dn( op->o_bd, &op->o_req_ndn )) {
Entry *e;
int rc;
ppbind *ppb;
slap_callback *cb;
op->o_bd->bd_info = (BackendInfo *)on->on_info;
rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e );
if ( rc != LDAP_SUCCESS ) {
return SLAP_CB_CONTINUE;
}
cb = op->o_tmpcalloc( sizeof(ppbind)+sizeof(slap_callback),
1, op->o_tmpmemctx );
ppb = (ppbind *)(cb+1);
ppb->on = on;
ppb->pErr = PP_noError;
ppb->set_restrict = 1;
/* Setup a callback so we can munge the result */
cb->sc_response = ppolicy_bind_response;
cb->sc_next = op->o_callback->sc_next;
cb->sc_private = ppb;
op->o_callback->sc_next = cb;
/* Did we receive a password policy request control? */
if ( op->o_ctrlflag[ppolicy_cid] ) {
ppb->send_ctrl = 1;
}
op->o_bd->bd_info = (BackendInfo *)on;
ppolicy_get( op, e, &ppb->pp );
rc = account_locked( op, e, &ppb->pp, &ppb->mod );
op->o_bd->bd_info = (BackendInfo *)on->on_info;
be_entry_release_r( op, e );
if ( rc ) {
ppb->pErr = PP_accountLocked;
send_ldap_error( op, rs, LDAP_INVALID_CREDENTIALS, NULL );
return rs->sr_err;
}
}
return SLAP_CB_CONTINUE;
}
/* Reset the restricted info for the next session on this connection */
static int
ppolicy_connection_destroy( BackendDB *bd, Connection *conn )
{
if ( pwcons && !BER_BVISEMPTY( &pwcons[conn->c_conn_idx].dn )) {
ch_free( pwcons[conn->c_conn_idx].dn.bv_val );
BER_BVZERO( &pwcons[conn->c_conn_idx].dn );
}
return SLAP_CB_CONTINUE;
}
/* Check if this connection is restricted */
static int
ppolicy_restrict(
Operation *op,
SlapReply *rs )
{
slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
int send_ctrl = 0;
/* Did we receive a password policy request control? */
if ( op->o_ctrlflag[ppolicy_cid] ) {
send_ctrl = 1;
}
if ( op->o_conn && !BER_BVISEMPTY( &pwcons[op->o_conn->c_conn_idx].dn )) {
LDAPControl **oldctrls;
/* if the current authcDN doesn't match the one we recorded,
* then an intervening Bind has succeeded and the restriction
* no longer applies. (ITS#4516)
*/
if ( !dn_match( &op->o_conn->c_ndn,
&pwcons[op->o_conn->c_conn_idx].dn )) {
ch_free( pwcons[op->o_conn->c_conn_idx].dn.bv_val );
BER_BVZERO( &pwcons[op->o_conn->c_conn_idx].dn );
return SLAP_CB_CONTINUE;
}
Debug( LDAP_DEBUG_TRACE,
"connection restricted to password changing only\n", 0, 0, 0);
if ( send_ctrl ) {
LDAPControl *ctrl = NULL;
ctrl = create_passcontrol( op, -1, -1, PP_changeAfterReset );
oldctrls = add_passcontrol( op, rs, ctrl );
}
op->o_bd->bd_info = (BackendInfo *)on->on_info;
send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
"Operations are restricted to bind/unbind/abandon/StartTLS/modify password" );
if ( send_ctrl ) {
ctrls_cleanup( op, rs, oldctrls );
}
return rs->sr_err;
}
return SLAP_CB_CONTINUE;
}
static int
ppolicy_compare_response(
Operation *op,
SlapReply *rs )
{
/* map compare responses to bind responses */
if ( rs->sr_err == LDAP_COMPARE_TRUE )
rs->sr_err = LDAP_SUCCESS;
else if ( rs->sr_err == LDAP_COMPARE_FALSE )
rs->sr_err = LDAP_INVALID_CREDENTIALS;
ppolicy_bind_response( op, rs );
/* map back to compare */
if ( rs->sr_err == LDAP_SUCCESS )
rs->sr_err = LDAP_COMPARE_TRUE;
else if ( rs->sr_err == LDAP_INVALID_CREDENTIALS )
rs->sr_err = LDAP_COMPARE_FALSE;
return SLAP_CB_CONTINUE;
}
static int
ppolicy_compare(
Operation *op,
SlapReply *rs )
{
slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
if ( ppolicy_restrict( op, rs ) != SLAP_CB_CONTINUE )
return rs->sr_err;
/* Did we receive a password policy request control?
* Are we testing the userPassword?
*/
if ( op->o_ctrlflag[ppolicy_cid] &&
op->orc_ava->aa_desc == slap_schema.si_ad_userPassword ) {
Entry *e;
int rc;
ppbind *ppb;
slap_callback *cb;
op->o_bd->bd_info = (BackendInfo *)on->on_info;
rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e );
if ( rc != LDAP_SUCCESS ) {
return SLAP_CB_CONTINUE;
}
cb = op->o_tmpcalloc( sizeof(ppbind)+sizeof(slap_callback),
1, op->o_tmpmemctx );
ppb = (ppbind *)(cb+1);
ppb->on = on;
ppb->pErr = PP_noError;
ppb->send_ctrl = 1;
/* failures here don't lockout the connection */
ppb->set_restrict = 0;
/* Setup a callback so we can munge the result */
cb->sc_response = ppolicy_compare_response;
cb->sc_next = op->o_callback->sc_next;
cb->sc_private = ppb;
op->o_callback->sc_next = cb;
op->o_bd->bd_info = (BackendInfo *)on;
ppolicy_get( op, e, &ppb->pp );
rc = account_locked( op, e, &ppb->pp, &ppb->mod );
op->o_bd->bd_info = (BackendInfo *)on->on_info;
be_entry_release_r( op, e );
if ( rc ) {
ppb->pErr = PP_accountLocked;
send_ldap_error( op, rs, LDAP_COMPARE_FALSE, NULL );
return rs->sr_err;
}
}
return SLAP_CB_CONTINUE;
}
static int
ppolicy_add(
Operation *op,
SlapReply *rs )
{
slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
pp_info *pi = on->on_bi.bi_private;
PassPolicy pp;
Attribute *pa;
const char *txt;
if ( ppolicy_restrict( op, rs ) != SLAP_CB_CONTINUE )
return rs->sr_err;
/* If this is a replica, assume the master checked everything */
if ( SLAPD_SYNC_IS_SYNCCONN( op->o_connid ) )
return SLAP_CB_CONTINUE;
/* Check for password in entry */
if ((pa = attr_find( op->oq_add.rs_e->e_attrs,
slap_schema.si_ad_userPassword )))
{
assert( pa->a_vals != NULL );
assert( !BER_BVISNULL( &pa->a_vals[ 0 ] ) );
if ( !BER_BVISNULL( &pa->a_vals[ 1 ] ) ) {
send_ldap_error( op, rs, LDAP_CONSTRAINT_VIOLATION, "Password policy only allows one password value" );
return rs->sr_err;
}
/*
* new entry contains a password - if we're not the root user
* then we need to check that the password fits in with the
* security policy for the new entry.
*/
ppolicy_get( op, op->ora_e, &pp );
if (pp.pwdCheckQuality > 0 && !be_isroot( op )) {
struct berval *bv = &(pa->a_vals[0]);
int rc, send_ctrl = 0;
LDAPPasswordPolicyError pErr = PP_noError;
char *txt;
/* Did we receive a password policy request control? */
if ( op->o_ctrlflag[ppolicy_cid] ) {
send_ctrl = 1;
}
rc = check_password_quality( bv, &pp, &pErr, op->ora_e, &txt );
if (rc != LDAP_SUCCESS) {
LDAPControl **oldctrls = NULL;
op->o_bd->bd_info = (BackendInfo *)on->on_info;
if ( send_ctrl ) {
LDAPControl *ctrl = NULL;
ctrl = create_passcontrol( op, -1, -1, pErr );
oldctrls = add_passcontrol( op, rs, ctrl );
}
send_ldap_error( op, rs, rc, txt ? txt : "Password fails quality checking policy" );
if ( txt ) {
free( txt );
}
if ( send_ctrl ) {
ctrls_cleanup( op, rs, oldctrls );
}
return rs->sr_err;
}
}
/*
* A controversial bit. We hash cleartext
* passwords provided via add and modify operations
* You're not really supposed to do this, since
* the X.500 model says "store attributes" as they
* get provided. By default, this is what we do
*
* But if the hash_passwords flag is set, we hash
* any cleartext password attribute values via the
* default password hashing scheme.
*/
if ((pi->hash_passwords) &&
(password_scheme( &(pa->a_vals[0]), NULL ) != LDAP_SUCCESS)) {
struct berval hpw;
slap_passwd_hash( &(pa->a_vals[0]), &hpw, &txt );
if (hpw.bv_val == NULL) {
/*
* hashing didn't work. Emit an error.
*/
rs->sr_err = LDAP_OTHER;
rs->sr_text = txt;
send_ldap_error( op, rs, LDAP_OTHER, "Password hashing failed" );
return rs->sr_err;
}
memset( pa->a_vals[0].bv_val, 0, pa->a_vals[0].bv_len);
ber_memfree( pa->a_vals[0].bv_val );
pa->a_vals[0].bv_val = hpw.bv_val;
pa->a_vals[0].bv_len = hpw.bv_len;
}
/* If password aging is in effect, set the pwdChangedTime */
if ( pp.pwdMaxAge || pp.pwdMinAge ) {
struct berval timestamp;
char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
time_t now = slap_get_time();
timestamp.bv_val = timebuf;
timestamp.bv_len = sizeof(timebuf);
slap_timestamp( &now, ×tamp );
attr_merge_one( op->ora_e, ad_pwdChangedTime, ×tamp, ×tamp );
}
}
return SLAP_CB_CONTINUE;
}
static int
ppolicy_mod_cb( Operation *op, SlapReply *rs )
{
slap_callback *sc = op->o_callback;
op->o_callback = sc->sc_next;
if ( rs->sr_err == LDAP_SUCCESS ) {
ch_free( pwcons[op->o_conn->c_conn_idx].dn.bv_val );
BER_BVZERO( &pwcons[op->o_conn->c_conn_idx].dn );
}
op->o_tmpfree( sc, op->o_tmpmemctx );
return SLAP_CB_CONTINUE;
}
static int
ppolicy_modify( Operation *op, SlapReply *rs )
{
slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
pp_info *pi = on->on_bi.bi_private;
int i, rc, mod_pw_only, pwmod, pwmop = -1, deladd,
hsize = 0;
PassPolicy pp;
Modifications *mods = NULL, *modtail = NULL,
*ml, *delmod, *addmod;
Attribute *pa, *ha, at;
const char *txt;
pw_hist *tl = NULL, *p;
int zapReset, send_ctrl = 0, free_txt = 0;
Entry *e;
struct berval newpw = BER_BVNULL, oldpw = BER_BVNULL,
*bv, cr[2];
LDAPPasswordPolicyError pErr = PP_noError;
LDAPControl *ctrl = NULL;
LDAPControl **oldctrls = NULL;
int is_pwdexop = 0;
int got_del_grace = 0, got_del_lock = 0, got_pw = 0, got_del_fail = 0;
int got_changed = 0, got_history = 0;
op->o_bd->bd_info = (BackendInfo *)on->on_info;
rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e );
op->o_bd->bd_info = (BackendInfo *)on;
if ( rc != LDAP_SUCCESS ) return SLAP_CB_CONTINUE;
/* If this is a replica, we may need to tweak some of the
* master's modifications. Otherwise, just pass it through.
*/
if ( SLAPD_SYNC_IS_SYNCCONN( op->o_connid ) ) {
Modifications **prev;
Attribute *a_grace, *a_lock, *a_fail;
a_grace = attr_find( e->e_attrs, ad_pwdGraceUseTime );
a_lock = attr_find( e->e_attrs, ad_pwdAccountLockedTime );
a_fail = attr_find( e->e_attrs, ad_pwdFailureTime );
for( prev = &op->orm_modlist, ml = *prev; ml; ml = *prev ) {
if ( ml->sml_desc == slap_schema.si_ad_userPassword )
got_pw = 1;
/* If we're deleting an attr that didn't exist,
* drop this delete op
*/
if ( ml->sml_op == LDAP_MOD_DELETE ||
ml->sml_op == SLAP_MOD_SOFTDEL ) {
int drop = 0;
if ( ml->sml_desc == ad_pwdGraceUseTime ) {
if ( !a_grace || got_del_grace ) {
drop = ml->sml_op == LDAP_MOD_DELETE;
} else {
got_del_grace = 1;
}
} else
if ( ml->sml_desc == ad_pwdAccountLockedTime ) {
if ( !a_lock || got_del_lock ) {
drop = ml->sml_op == LDAP_MOD_DELETE;
} else {
got_del_lock = 1;
}
} else
if ( ml->sml_desc == ad_pwdFailureTime ) {
if ( !a_fail || got_del_fail ) {
drop = ml->sml_op == LDAP_MOD_DELETE;
} else {
got_del_fail = 1;
}
}
if ( drop ) {
*prev = ml->sml_next;
ml->sml_next = NULL;
slap_mods_free( ml, 1 );
continue;
}
}
prev = &ml->sml_next;
}
/* If we're resetting the password, make sure grace, accountlock,
* and failure also get removed.
*/
if ( got_pw ) {
if ( a_grace && !got_del_grace ) {
ml = (Modifications *) ch_malloc( sizeof( Modifications ) );
ml->sml_op = LDAP_MOD_DELETE;
ml->sml_flags = SLAP_MOD_INTERNAL;
ml->sml_type.bv_val = NULL;
ml->sml_desc = ad_pwdGraceUseTime;
ml->sml_numvals = 0;
ml->sml_values = NULL;
ml->sml_nvalues = NULL;
ml->sml_next = NULL;
*prev = ml;
prev = &ml->sml_next;
}
if ( a_lock && !got_del_lock ) {
ml = (Modifications *) ch_malloc( sizeof( Modifications ) );
ml->sml_op = LDAP_MOD_DELETE;
ml->sml_flags = SLAP_MOD_INTERNAL;
ml->sml_type.bv_val = NULL;
ml->sml_desc = ad_pwdAccountLockedTime;
ml->sml_numvals = 0;
ml->sml_values = NULL;
ml->sml_nvalues = NULL;
ml->sml_next = NULL;
*prev = ml;
}
if ( a_fail && !got_del_fail ) {
ml = (Modifications *) ch_malloc( sizeof( Modifications ) );
ml->sml_op = LDAP_MOD_DELETE;
ml->sml_flags = SLAP_MOD_INTERNAL;
ml->sml_type.bv_val = NULL;
ml->sml_desc = ad_pwdFailureTime;
ml->sml_numvals = 0;
ml->sml_values = NULL;
ml->sml_nvalues = NULL;
ml->sml_next = NULL;
*prev = ml;
}
}
op->o_bd->bd_info = (BackendInfo *)on->on_info;
be_entry_release_r( op, e );
return SLAP_CB_CONTINUE;
}
/* Did we receive a password policy request control? */
if ( op->o_ctrlflag[ppolicy_cid] ) {
send_ctrl = 1;
}
/* See if this is a pwdModify exop. If so, we can
* access the plaintext passwords from that request.
*/
{
slap_callback *sc;
for ( sc = op->o_callback; sc; sc=sc->sc_next ) {
if ( sc->sc_response == slap_null_cb &&
sc->sc_private ) {
req_pwdexop_s *qpw = sc->sc_private;
newpw = qpw->rs_new;
oldpw = qpw->rs_old;
is_pwdexop = 1;
break;
}
}
}
ppolicy_get( op, e, &pp );
for ( ml = op->orm_modlist,
pwmod = 0, mod_pw_only = 1,
deladd = 0, delmod = NULL,
addmod = NULL,
zapReset = 1;
ml != NULL; modtail = ml, ml = ml->sml_next )
{
if ( ml->sml_desc == pp.ad ) {
pwmod = 1;
pwmop = ml->sml_op;
if ((deladd == 0) && (ml->sml_op == LDAP_MOD_DELETE) &&
(ml->sml_values) && !BER_BVISNULL( &ml->sml_values[0] ))
{
deladd = 1;
delmod = ml;
}
if ((ml->sml_op == LDAP_MOD_ADD) ||
(ml->sml_op == LDAP_MOD_REPLACE))
{
if ( ml->sml_values && !BER_BVISNULL( &ml->sml_values[0] )) {
if ( deladd == 1 )
deladd = 2;
/* FIXME: there's no easy way to ensure
* that add does not cause multiple
* userPassword values; one way (that
* would be consistent with the single
* password constraint) would be to turn
* add into replace); another would be
* to disallow add.
*
* Let's check at least that a single value
* is being added
*/
if ( addmod || !BER_BVISNULL( &ml->sml_values[ 1 ] ) ) {
rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
rs->sr_text = "Password policy only allows one password value";
goto return_results;
}
addmod = ml;
} else {
/* replace can have no values, add cannot */
assert( ml->sml_op == LDAP_MOD_REPLACE );
}
}
} else if ( !(ml->sml_flags & SLAP_MOD_INTERNAL) && !is_at_operational( ml->sml_desc->ad_type ) ) {
mod_pw_only = 0;
/* modifying something other than password */
}
/*
* If there is a request to explicitly add a pwdReset
* attribute, then we suppress the normal behaviour on
* password change, which is to remove the pwdReset
* attribute.
*
* This enables an administrator to assign a new password
* and place a "must reset" flag on the entry, which will
* stay until the user explicitly changes his/her password.
*/
if (ml->sml_desc == ad_pwdReset ) {
if ((ml->sml_op == LDAP_MOD_ADD) ||
(ml->sml_op == LDAP_MOD_REPLACE))
zapReset = 0;
}
if ( ml->sml_op == LDAP_MOD_DELETE ) {
if ( ml->sml_desc == ad_pwdGraceUseTime ) {
got_del_grace = 1;
} else if ( ml->sml_desc == ad_pwdAccountLockedTime ) {
got_del_lock = 1;
} else if ( ml->sml_desc == ad_pwdFailureTime ) {
got_del_fail = 1;
}
}
if ( ml->sml_desc == ad_pwdChangedTime ) {
got_changed = 1;
} else if (ml->sml_desc == ad_pwdHistory ) {
got_history = 1;
}
}
if (!BER_BVISEMPTY( &pwcons[op->o_conn->c_conn_idx].dn ) && !mod_pw_only ) {
if ( dn_match( &op->o_conn->c_ndn,
&pwcons[op->o_conn->c_conn_idx].dn )) {
Debug( LDAP_DEBUG_TRACE,
"connection restricted to password changing only\n", 0, 0, 0 );
rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
rs->sr_text = "Operations are restricted to bind/unbind/abandon/StartTLS/modify password";
pErr = PP_changeAfterReset;
goto return_results;
} else {
ch_free( pwcons[op->o_conn->c_conn_idx].dn.bv_val );
BER_BVZERO( &pwcons[op->o_conn->c_conn_idx].dn );
}
}
/*
* if we have a "safe password modify policy", then we need to check if we're doing
* a delete (with the old password), followed by an add (with the new password).
*
* If we got just a delete with nothing else, just let it go. We also skip all the checks if
* the root user is bound. Root can do anything, including avoid the policies.
*/
if (!pwmod) goto do_modify;
/*
* Build the password history list in ascending time order
* We need this, even if the user is root, in order to maintain
* the pwdHistory operational attributes properly.
*/
if (addmod && pp.pwdInHistory > 0 && (ha = attr_find( e->e_attrs, ad_pwdHistory ))) {
struct berval oldpw;
time_t oldtime;
for(i=0; ha->a_nvals[i].bv_val; i++) {
rc = parse_pwdhistory( &(ha->a_nvals[i]), NULL,
&oldtime, &oldpw );
if (rc != LDAP_SUCCESS) continue; /* invalid history entry */
if (oldpw.bv_val) {
add_to_pwd_history( &tl, oldtime, &oldpw,
&(ha->a_nvals[i]) );
oldpw.bv_val = NULL;
oldpw.bv_len = 0;
}
}
for(p=tl; p; p=p->next, hsize++); /* count history size */
}
if (be_isroot( op )) goto do_modify;
/* NOTE: according to draft-behera-ldap-password-policy
* pwdAllowUserChange == FALSE must only prevent pwd changes
* by the user the pwd belongs to (ITS#7021) */
if (!pp.pwdAllowUserChange && dn_match(&op->o_req_ndn, &op->o_ndn)) {
rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
rs->sr_text = "User alteration of password is not allowed";
pErr = PP_passwordModNotAllowed;
goto return_results;
}
/* Just deleting? */
if (!addmod) {
/* skip everything else */
pwmod = 0;
goto do_modify;
}
/* This is a pwdModify exop that provided the old pw.
* We need to create a Delete mod for this old pw and
* let the matching value get found later
*/
if (pp.pwdSafeModify && oldpw.bv_val ) {
ml = (Modifications *)ch_calloc( sizeof( Modifications ), 1 );
ml->sml_op = LDAP_MOD_DELETE;
ml->sml_flags = SLAP_MOD_INTERNAL;
ml->sml_desc = pp.ad;
ml->sml_type = pp.ad->ad_cname;
ml->sml_numvals = 1;
ml->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
ber_dupbv( &ml->sml_values[0], &oldpw );
BER_BVZERO( &ml->sml_values[1] );
ml->sml_next = op->orm_modlist;
op->orm_modlist = ml;
delmod = ml;
deladd = 2;
}
if (pp.pwdSafeModify && deladd != 2) {
Debug( LDAP_DEBUG_TRACE,
"change password must use DELETE followed by ADD/REPLACE\n",
0, 0, 0 );
rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
rs->sr_text = "Must supply old password to be changed as well as new one";
pErr = PP_mustSupplyOldPassword;
goto return_results;
}
/* Check age, but only if pwdReset is not TRUE */
pa = attr_find( e->e_attrs, ad_pwdReset );
if ((!pa || !bvmatch( &pa->a_nvals[0], &slap_true_bv )) &&
pp.pwdMinAge > 0) {
time_t pwtime = (time_t)-1, now;
int age;
if ((pa = attr_find( e->e_attrs, ad_pwdChangedTime )) != NULL)
pwtime = parse_time( pa->a_nvals[0].bv_val );
now = slap_get_time();
age = (int)(now - pwtime);
if ((pwtime != (time_t)-1) && (age < pp.pwdMinAge)) {
rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
rs->sr_text = "Password is too young to change";
pErr = PP_passwordTooYoung;
goto return_results;
}
}
/* pa is used in password history check below, be sure it's set */
if ((pa = attr_find( e->e_attrs, pp.ad )) != NULL && delmod) {
/*
* we have a password to check
*/
bv = oldpw.bv_val ? &oldpw : delmod->sml_values;
/* FIXME: no access checking? */
rc = slap_passwd_check( op, NULL, pa, bv, &txt );
if (rc != LDAP_SUCCESS) {
Debug( LDAP_DEBUG_TRACE,
"old password check failed: %s\n", txt, 0, 0 );
rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
rs->sr_text = "Must supply correct old password to change to new one";
pErr = PP_mustSupplyOldPassword;
goto return_results;
} else {
int i;
/*
* replace the delete value with the (possibly hashed)
* value which is currently in the password.
*/
for ( i = 0; !BER_BVISNULL( &delmod->sml_values[i] ); i++ ) {
free( delmod->sml_values[i].bv_val );
BER_BVZERO( &delmod->sml_values[i] );
}
free( delmod->sml_values );
delmod->sml_values = ch_calloc( sizeof(struct berval), 2 );
BER_BVZERO( &delmod->sml_values[1] );
ber_dupbv( &(delmod->sml_values[0]), &(pa->a_nvals[0]) );
}
}
bv = newpw.bv_val ? &newpw : &addmod->sml_values[0];
if (pp.pwdCheckQuality > 0) {
rc = check_password_quality( bv, &pp, &pErr, e, (char **)&txt );
if (rc != LDAP_SUCCESS) {
rs->sr_err = rc;
if ( txt ) {
rs->sr_text = txt;
free_txt = 1;
} else {
rs->sr_text = "Password fails quality checking policy";
}
goto return_results;
}
}
/* If pwdInHistory is zero, passwords may be reused */
if (pa && pp.pwdInHistory > 0) {
/*
* Last check - the password history.
*/
/* FIXME: no access checking? */
if (slap_passwd_check( op, NULL, pa, bv, &txt ) == LDAP_SUCCESS) {
/*
* This is bad - it means that the user is attempting
* to set the password to the same as the old one.
*/
rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
rs->sr_text = "Password is not being changed from existing value";
pErr = PP_passwordInHistory;
goto return_results;
}
/*
* Iterate through the password history, and fail on any
* password matches.
*/
at = *pa;
at.a_vals = cr;
cr[1].bv_val = NULL;
for(p=tl; p; p=p->next) {
cr[0] = p->pw;
/* FIXME: no access checking? */
rc = slap_passwd_check( op, NULL, &at, bv, &txt );
if (rc != LDAP_SUCCESS) continue;
rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
rs->sr_text = "Password is in history of old passwords";
pErr = PP_passwordInHistory;
goto return_results;
}
}
do_modify:
if (pwmod) {
struct berval timestamp;
char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
time_t now = slap_get_time();
/* If the conn is restricted, set a callback to clear it
* if the pwmod succeeds
*/
if (!BER_BVISEMPTY( &pwcons[op->o_conn->c_conn_idx].dn )) {
slap_callback *sc = op->o_tmpcalloc( 1, sizeof( slap_callback ),
op->o_tmpmemctx );
sc->sc_next = op->o_callback;
/* Must use sc_response to insure we reset on success, before
* the client sees the response. Must use sc_cleanup to insure
* that it gets cleaned up if sc_response is not called.
*/
sc->sc_response = ppolicy_mod_cb;
sc->sc_cleanup = ppolicy_mod_cb;
op->o_callback = sc;
}
/*
* keep the necessary pwd.. operational attributes
* up to date.
*/
if (!got_changed) {
timestamp.bv_val = timebuf;
timestamp.bv_len = sizeof(timebuf);
slap_timestamp( &now, ×tamp );
mods = NULL;
if (pwmop != LDAP_MOD_DELETE) {
mods = (Modifications *) ch_calloc( sizeof( Modifications ), 1 );
mods->sml_op = LDAP_MOD_REPLACE;
mods->sml_numvals = 1;
mods->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
ber_dupbv( &mods->sml_values[0], ×tamp );
BER_BVZERO( &mods->sml_values[1] );
assert( !BER_BVISNULL( &mods->sml_values[0] ) );
} else if (attr_find(e->e_attrs, ad_pwdChangedTime )) {
mods = (Modifications *) ch_calloc( sizeof( Modifications ), 1 );
mods->sml_op = LDAP_MOD_DELETE;
}
if (mods) {
mods->sml_desc = ad_pwdChangedTime;
mods->sml_flags = SLAP_MOD_INTERNAL;
mods->sml_next = NULL;
modtail->sml_next = mods;
modtail = mods;
}
}
if (!got_del_grace && attr_find(e->e_attrs, ad_pwdGraceUseTime )) {
mods = (Modifications *) ch_calloc( sizeof( Modifications ), 1 );
mods->sml_op = LDAP_MOD_DELETE;
mods->sml_desc = ad_pwdGraceUseTime;
mods->sml_flags = SLAP_MOD_INTERNAL;
mods->sml_next = NULL;
modtail->sml_next = mods;
modtail = mods;
}
if (!got_del_lock && attr_find(e->e_attrs, ad_pwdAccountLockedTime )) {
mods = (Modifications *) ch_calloc( sizeof( Modifications ), 1 );
mods->sml_op = LDAP_MOD_DELETE;
mods->sml_desc = ad_pwdAccountLockedTime;
mods->sml_flags = SLAP_MOD_INTERNAL;
mods->sml_next = NULL;
modtail->sml_next = mods;
modtail = mods;
}
if (!got_del_fail && attr_find(e->e_attrs, ad_pwdFailureTime )) {
mods = (Modifications *) ch_calloc( sizeof( Modifications ), 1 );
mods->sml_op = LDAP_MOD_DELETE;
mods->sml_desc = ad_pwdFailureTime;
mods->sml_flags = SLAP_MOD_INTERNAL;
mods->sml_next = NULL;
modtail->sml_next = mods;
modtail = mods;
}
/* Delete the pwdReset attribute, since it's being reset */
if ((zapReset) && (attr_find(e->e_attrs, ad_pwdReset ))) {
mods = (Modifications *) ch_calloc( sizeof( Modifications ), 1 );
mods->sml_op = LDAP_MOD_DELETE;
mods->sml_desc = ad_pwdReset;
mods->sml_flags = SLAP_MOD_INTERNAL;
mods->sml_next = NULL;
modtail->sml_next = mods;
modtail = mods;
}
if (!got_history && pp.pwdInHistory > 0) {
if (hsize >= pp.pwdInHistory) {
/*
* We use the >= operator, since we are going to add
* the existing password attribute value into the
* history - thus the cardinality of history values is
* about to rise by one.
*
* If this would push it over the limit of history
* values (remembering - the password policy could have
* changed since the password was last altered), we must
* delete at least 1 value from the pwdHistory list.
*
* In fact, we delete '(#pwdHistory attrs - max pwd
* history length) + 1' values, starting with the oldest.
* This is easily evaluated, since the linked list is
* created in ascending time order.
*/
mods = (Modifications *) ch_calloc( sizeof( Modifications ), 1 );
mods->sml_op = LDAP_MOD_DELETE;
mods->sml_flags = SLAP_MOD_INTERNAL;
mods->sml_desc = ad_pwdHistory;
mods->sml_numvals = hsize - pp.pwdInHistory + 1;
mods->sml_values = ch_calloc( sizeof( struct berval ),
hsize - pp.pwdInHistory + 2 );
BER_BVZERO( &mods->sml_values[ hsize - pp.pwdInHistory + 1 ] );
for(i=0,p=tl; i < (hsize - pp.pwdInHistory + 1); i++, p=p->next) {
BER_BVZERO( &mods->sml_values[i] );
ber_dupbv( &(mods->sml_values[i]), &p->bv );
}
mods->sml_next = NULL;
modtail->sml_next = mods;
modtail = mods;
}
free_pwd_history_list( &tl );
/*
* Now add the existing password into the history list.
* This will be executed even if the operation is to delete
* the password entirely.
*
* This isn't in the spec explicitly, but it seems to make
* sense that the password history list is the list of all
* previous passwords - even if they were deleted. Thus, if
* someone tries to add a historical password at some future
* point, it will fail.
*/
if ((pa = attr_find( e->e_attrs, pp.ad )) != NULL) {
mods = (Modifications *) ch_malloc( sizeof( Modifications ) );
mods->sml_op = LDAP_MOD_ADD;
mods->sml_flags = SLAP_MOD_INTERNAL;
mods->sml_type.bv_val = NULL;
mods->sml_desc = ad_pwdHistory;
mods->sml_nvalues = NULL;
mods->sml_numvals = 1;
mods->sml_values = ch_calloc( sizeof( struct berval ), 2 );
mods->sml_values[ 1 ].bv_val = NULL;
mods->sml_values[ 1 ].bv_len = 0;
make_pwd_history_value( timebuf, &mods->sml_values[0], pa );
mods->sml_next = NULL;
modtail->sml_next = mods;
modtail = mods;
} else {
Debug( LDAP_DEBUG_TRACE,
"ppolicy_modify: password attr lookup failed\n", 0, 0, 0 );
}
}
/*
* Controversial bit here. If the new password isn't hashed
* (ie, is cleartext), we probably should hash it according
* to the default hash. The reason for this is that we want
* to use the policy if possible, but if we hash the password
* before, then we're going to run into trouble when it
* comes time to check the password.
*
* Now, the right thing to do is to use the extended password
* modify operation, but not all software can do this,
* therefore it makes sense to hash the new password, now
* we know it passes the policy requirements.
*
* Of course, if the password is already hashed, then we
* leave it alone.
*/
if ((pi->hash_passwords) && (addmod) && !newpw.bv_val &&
(password_scheme( &(addmod->sml_values[0]), NULL ) != LDAP_SUCCESS))
{
struct berval hpw, bv;
slap_passwd_hash( &(addmod->sml_values[0]), &hpw, &txt );
if (hpw.bv_val == NULL) {
/*
* hashing didn't work. Emit an error.
*/
rs->sr_err = LDAP_OTHER;
rs->sr_text = txt;
goto return_results;
}
bv = addmod->sml_values[0];
/* clear and discard the clear password */
memset(bv.bv_val, 0, bv.bv_len);
ber_memfree(bv.bv_val);
addmod->sml_values[0] = hpw;
}
}
op->o_bd->bd_info = (BackendInfo *)on->on_info;
be_entry_release_r( op, e );
return SLAP_CB_CONTINUE;
return_results:
free_pwd_history_list( &tl );
op->o_bd->bd_info = (BackendInfo *)on->on_info;
be_entry_release_r( op, e );
if ( send_ctrl ) {
ctrl = create_passcontrol( op, -1, -1, pErr );
oldctrls = add_passcontrol( op, rs, ctrl );
}
send_ldap_result( op, rs );
if ( free_txt ) {
free( (char *)txt );
rs->sr_text = NULL;
}
if ( send_ctrl ) {
if ( is_pwdexop ) {
if ( rs->sr_flags & REP_CTRLS_MUSTBEFREED ) {
op->o_tmpfree( oldctrls, op->o_tmpmemctx );
}
oldctrls = NULL;
rs->sr_flags |= REP_CTRLS_MUSTBEFREED;
} else {
ctrls_cleanup( op, rs, oldctrls );
}
}
return rs->sr_err;
}
static int
ppolicy_parseCtrl(
Operation *op,
SlapReply *rs,
LDAPControl *ctrl )
{
if ( !BER_BVISNULL( &ctrl->ldctl_value ) ) {
rs->sr_text = "passwordPolicyRequest control value not absent";
return LDAP_PROTOCOL_ERROR;
}
op->o_ctrlflag[ppolicy_cid] = ctrl->ldctl_iscritical
? SLAP_CONTROL_CRITICAL
: SLAP_CONTROL_NONCRITICAL;
return LDAP_SUCCESS;
}
static int
attrPretty(
Syntax *syntax,
struct berval *val,
struct berval *out,
void *ctx )
{
AttributeDescription *ad = NULL;
const char *err;
int code;
code = slap_bv2ad( val, &ad, &err );
if ( !code ) {
ber_dupbv_x( out, &ad->ad_type->sat_cname, ctx );
}
return code;
}
static int
attrNormalize(
slap_mask_t use,
Syntax *syntax,
MatchingRule *mr,
struct berval *val,
struct berval *out,
void *ctx )
{
AttributeDescription *ad = NULL;
const char *err;
int code;
code = slap_bv2ad( val, &ad, &err );
if ( !code ) {
ber_str2bv_x( ad->ad_type->sat_oid, 0, 1, out, ctx );
}
return code;
}
static int
ppolicy_db_init(
BackendDB *be,
ConfigReply *cr
)
{
slap_overinst *on = (slap_overinst *) be->bd_info;
if ( SLAP_ISGLOBALOVERLAY( be ) ) {
/* do not allow slapo-ppolicy to be global by now (ITS#5858) */
if ( cr ){
snprintf( cr->msg, sizeof(cr->msg),
"slapo-ppolicy cannot be global" );
Debug( LDAP_DEBUG_ANY, "%s\n", cr->msg, 0, 0 );
}
return 1;
}
/* Has User Schema been initialized yet? */
if ( !pwd_UsSchema[0].ad[0] ) {
const char *err;
int i, code;
for (i=0; pwd_UsSchema[i].def; i++) {
code = slap_str2ad( pwd_UsSchema[i].def, pwd_UsSchema[i].ad, &err );
if ( code ) {
if ( cr ){
snprintf( cr->msg, sizeof(cr->msg),
"User Schema load failed for attribute \"%s\". Error code %d: %s",
pwd_UsSchema[i].def, code, err );
Debug( LDAP_DEBUG_ANY, "%s\n", cr->msg, 0, 0 );
}
return code;
}
}
{
Syntax *syn;
MatchingRule *mr;
syn = ch_malloc( sizeof( Syntax ));
*syn = *ad_pwdAttribute->ad_type->sat_syntax;
syn->ssyn_pretty = attrPretty;
ad_pwdAttribute->ad_type->sat_syntax = syn;
mr = ch_malloc( sizeof( MatchingRule ));
*mr = *ad_pwdAttribute->ad_type->sat_equality;
mr->smr_normalize = attrNormalize;
ad_pwdAttribute->ad_type->sat_equality = mr;
}
}
on->on_bi.bi_private = ch_calloc( sizeof(pp_info), 1 );
if ( dtblsize && !pwcons ) {
/* accommodate for c_conn_idx == -1 */
pwcons = ch_calloc( sizeof(pw_conn), dtblsize + 1 );
pwcons++;
}
ov_count++;
return 0;
}
static int
ppolicy_db_open(
BackendDB *be,
ConfigReply *cr
)
{
return overlay_register_control( be, LDAP_CONTROL_PASSWORDPOLICYREQUEST );
}
static int
ppolicy_db_close(
BackendDB *be,
ConfigReply *cr
)
{
#ifdef SLAP_CONFIG_DELETE
overlay_unregister_control( be, LDAP_CONTROL_PASSWORDPOLICYREQUEST );
#endif /* SLAP_CONFIG_DELETE */
return 0;
}
static int
ppolicy_db_destroy(
BackendDB *be,
ConfigReply *cr
)
{
slap_overinst *on = (slap_overinst *) be->bd_info;
pp_info *pi = on->on_bi.bi_private;
on->on_bi.bi_private = NULL;
free( pi->def_policy.bv_val );
free( pi );
ov_count--;
if ( ov_count <=0 && pwcons ) {
pw_conn *pwc = pwcons;
pwcons = NULL;
pwc--;
ch_free( pwc );
}
return 0;
}
static char *extops[] = {
LDAP_EXOP_MODIFY_PASSWD,
NULL
};
static slap_overinst ppolicy;
int ppolicy_initialize()
{
int i, code;
for (i=0; pwd_OpSchema[i].def; i++) {
code = register_at( pwd_OpSchema[i].def, pwd_OpSchema[i].ad, 0 );
if ( code ) {
Debug( LDAP_DEBUG_ANY,
"ppolicy_initialize: register_at failed\n", 0, 0, 0 );
return code;
}
/* Allow Manager to set these as needed */
if ( is_at_no_user_mod( (*pwd_OpSchema[i].ad)->ad_type )) {
(*pwd_OpSchema[i].ad)->ad_type->sat_flags |=
SLAP_AT_MANAGEABLE;
}
}
code = register_supported_control( LDAP_CONTROL_PASSWORDPOLICYREQUEST,
SLAP_CTRL_ADD|SLAP_CTRL_BIND|SLAP_CTRL_MODIFY|SLAP_CTRL_HIDE, extops,
ppolicy_parseCtrl, &ppolicy_cid );
if ( code != LDAP_SUCCESS ) {
Debug( LDAP_DEBUG_ANY, "Failed to register control %d\n", code, 0, 0 );
return code;
}
ldap_pvt_thread_mutex_init( &chk_syntax_mutex );
ppolicy.on_bi.bi_type = "ppolicy";
ppolicy.on_bi.bi_db_init = ppolicy_db_init;
ppolicy.on_bi.bi_db_open = ppolicy_db_open;
ppolicy.on_bi.bi_db_close = ppolicy_db_close;
ppolicy.on_bi.bi_db_destroy = ppolicy_db_destroy;
ppolicy.on_bi.bi_op_add = ppolicy_add;
ppolicy.on_bi.bi_op_bind = ppolicy_bind;
ppolicy.on_bi.bi_op_compare = ppolicy_compare;
ppolicy.on_bi.bi_op_delete = ppolicy_restrict;
ppolicy.on_bi.bi_op_modify = ppolicy_modify;
ppolicy.on_bi.bi_op_search = ppolicy_restrict;
ppolicy.on_bi.bi_connection_destroy = ppolicy_connection_destroy;
ppolicy.on_bi.bi_cf_ocs = ppolicyocs;
code = config_register_schema( ppolicycfg, ppolicyocs );
if ( code ) return code;
return overlay_register( &ppolicy );
}
#if SLAPD_OVER_PPOLICY == SLAPD_MOD_DYNAMIC
int init_module(int argc, char *argv[]) {
return ppolicy_initialize();
}
#endif
#endif /* defined(SLAPD_OVER_PPOLICY) */
|