Debian Codegraph @

servers/slapd/overlays/ppolicy.c

raw
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3 *
4 * Copyright 2004-2024 The OpenLDAP Foundation.
5 * Portions Copyright 2004-2005 Howard Chu, Symas Corporation.
6 * Portions Copyright 2004 Hewlett-Packard Company.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
11 * Public License.
12 *
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
16 */
17 /* ACKNOWLEDGEMENTS:
18 * This work was developed by Howard Chu for inclusion in
19 * OpenLDAP Software, based on prior work by Neil Dunbar (HP).
20 * This work was sponsored by the Hewlett-Packard Company.
21 */
22
23 #include "portable.h"
24
25 /* This file implements "Password Policy for LDAP Directories",
26 * based on draft behera-ldap-password-policy-09
27 */
28
29 #ifdef SLAPD_OVER_PPOLICY
30
31 #include <ldap.h>
32 #include "lutil.h"
33 #include "slap.h"
34 #ifdef SLAPD_MODULES
35 #define LIBLTDL_DLL_IMPORT /* Win32: don't re-export libltdl's symbols */
36 #include <ltdl.h>
37 #endif
38 #include <ac/errno.h>
39 #include <ac/time.h>
40 #include <ac/string.h>
41 #include <ac/ctype.h>
42 #include "slap-config.h"
43
44 #ifndef PPOLICY_DEFAULT_MAXRECORDED_FAILURE
45 #define PPOLICY_DEFAULT_MAXRECORDED_FAILURE 5
46 #endif
47
48 /* External password quality checking function.
49 * The error message must have a preallocated buffer and size
50 * passed in. Module can still allocate a buffer for
51 * it if the provided one is too small.
52 */
53 typedef int (check_func)( char *passwd, struct berval *errmsg, Entry *ent, struct berval *arg );
54 #define ERRBUFSIZ 256
55
56 /* Per-instance configuration information */
57 typedef struct pp_info {
58 struct berval def_policy; /* DN of default policy subentry */
59 int use_lockout; /* send AccountLocked result? */
60 int hash_passwords; /* transparently hash cleartext pwds */
61 int forward_updates; /* use frontend for policy state updates */
62 int disable_write;
63 int send_netscape_controls; /* send netscape password controls */
64 char *pwdCheckModule; /* name of module to dynamically
65 load to check password */
66 #ifdef SLAPD_MODULES
67 lt_dlhandle pwdCheckHandle; /* handle from lt_dlopen */
68 check_func *pwdCheckFunc;
69 #endif /* SLAPD_MODULES */
70 ldap_pvt_thread_mutex_t pwdFailureTime_mutex;
71 } pp_info;
72
73 /* Our per-connection info - note, it is not per-instance, it is
74 * used by all instances
75 */
76 typedef struct pw_conn {
77 struct berval dn; /* DN of restricted user */
78 } pw_conn;
79
80 static pw_conn *pwcons;
81 static int ppolicy_cid;
82 static int account_usability_cid;
83 static int ov_count;
84
85 typedef struct pass_policy {
86 AttributeDescription *ad; /* attribute to which the policy applies */
87 int pwdMinAge; /* minimum time (seconds) until passwd can change */
88 int pwdMaxAge; /* time in seconds until pwd will expire after change */
89 int pwdMaxIdle; /* number of seconds since last successful bind before
90 passwd gets locked out */
91 int pwdInHistory; /* number of previous passwords kept */
92 int pwdCheckQuality; /* 0 = don't check quality, 1 = check if possible,
93 2 = check mandatory; fail if not possible */
94 int pwdMinLength; /* minimum number of chars in password */
95 int pwdMaxLength; /* maximum number of chars in password */
96 int pwdExpireWarning; /* number of seconds that warning controls are
97 sent before a password expires */
98 int pwdGraceExpiry; /* number of seconds after expiry grace logins are
99 valid */
100 int pwdGraceAuthNLimit; /* number of times you can log in with an
101 expired password */
102 int pwdLockout; /* 0 = do not lockout passwords, 1 = lock them out */
103 int pwdLockoutDuration; /* time in seconds a password is locked out for */
104 int pwdMinDelay; /* base bind delay in seconds on failure */
105 int pwdMaxDelay; /* maximum bind delay in seconds */
106 int pwdMaxFailure; /* number of failed binds allowed before lockout */
107 int pwdMaxRecordedFailure; /* number of failed binds to store */
108 int pwdFailureCountInterval; /* number of seconds before failure
109 counts are zeroed */
110 int pwdMustChange; /* 0 = users can use admin set password
111 1 = users must change password after admin set */
112 int pwdAllowUserChange; /* 0 = users cannot change their passwords
113 1 = users can change them */
114 int pwdSafeModify; /* 0 = old password doesn't need to come
115 with password change request
116 1 = password change must supply existing pwd */
117 int pwdUseCheckModule; /* 0 = do not use password check module, 1 = use */
118 struct berval pwdCheckModuleArg; /* Optional argument to the password check
119 module */
120 } PassPolicy;
121
122 typedef struct pw_hist {
123 time_t t; /* timestamp of history entry */
124 struct berval pw; /* old password hash */
125 struct berval bv; /* text of entire entry */
126 struct pw_hist *next;
127 } pw_hist;
128
129 /* Operational attributes */
130 static AttributeDescription *ad_pwdChangedTime, *ad_pwdAccountLockedTime,
131 *ad_pwdFailureTime, *ad_pwdHistory, *ad_pwdGraceUseTime, *ad_pwdReset,
132 *ad_pwdPolicySubentry, *ad_pwdStartTime, *ad_pwdEndTime,
133 *ad_pwdLastSuccess, *ad_pwdAccountTmpLockoutEnd;
134
135 /* Policy attributes */
136 static AttributeDescription *ad_pwdMinAge, *ad_pwdMaxAge, *ad_pwdMaxIdle,
137 *ad_pwdInHistory, *ad_pwdCheckQuality, *ad_pwdMinLength, *ad_pwdMaxLength,
138 *ad_pwdMaxFailure, *ad_pwdGraceExpiry, *ad_pwdGraceAuthNLimit,
139 *ad_pwdExpireWarning, *ad_pwdMinDelay, *ad_pwdMaxDelay,
140 *ad_pwdLockoutDuration, *ad_pwdFailureCountInterval,
141 *ad_pwdCheckModule, *ad_pwdCheckModuleArg, *ad_pwdUseCheckModule, *ad_pwdLockout,
142 *ad_pwdMustChange, *ad_pwdAllowUserChange, *ad_pwdSafeModify,
143 *ad_pwdAttribute, *ad_pwdMaxRecordedFailure;
144
145 static struct schema_info {
146 char *def;
147 AttributeDescription **ad;
148 } pwd_OpSchema[] = {
149 { "( 1.3.6.1.4.1.42.2.27.8.1.16 "
150 "NAME ( 'pwdChangedTime' ) "
151 "DESC 'The time the password was last changed' "
152 "EQUALITY generalizedTimeMatch "
153 "ORDERING generalizedTimeOrderingMatch "
154 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
155 "SINGLE-VALUE "
156 "NO-USER-MODIFICATION "
157 "USAGE directoryOperation )",
158 &ad_pwdChangedTime },
159 { "( 1.3.6.1.4.1.42.2.27.8.1.17 "
160 "NAME ( 'pwdAccountLockedTime' ) "
161 "DESC 'The time an user account was locked' "
162 "EQUALITY generalizedTimeMatch "
163 "ORDERING generalizedTimeOrderingMatch "
164 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
165 "SINGLE-VALUE "
166 #if 0 /* FIXME: ITS#9671 until we introduce a separate lockout flag? */
167 "NO-USER-MODIFICATION "
168 #endif
169 "USAGE directoryOperation )",
170 &ad_pwdAccountLockedTime },
171 { "( 1.3.6.1.4.1.42.2.27.8.1.19 "
172 "NAME ( 'pwdFailureTime' ) "
173 "DESC 'The timestamps of the last consecutive authentication failures' "
174 "EQUALITY generalizedTimeMatch "
175 "ORDERING generalizedTimeOrderingMatch "
176 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
177 "NO-USER-MODIFICATION "
178 "USAGE directoryOperation )",
179 &ad_pwdFailureTime },
180 { "( 1.3.6.1.4.1.42.2.27.8.1.20 "
181 "NAME ( 'pwdHistory' ) "
182 "DESC 'The history of users passwords' "
183 "EQUALITY octetStringMatch "
184 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.40 "
185 "NO-USER-MODIFICATION "
186 "USAGE directoryOperation )",
187 &ad_pwdHistory },
188 { "( 1.3.6.1.4.1.42.2.27.8.1.21 "
189 "NAME ( 'pwdGraceUseTime' ) "
190 "DESC 'The timestamps of the grace login once the password has expired' "
191 "EQUALITY generalizedTimeMatch "
192 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
193 "NO-USER-MODIFICATION "
194 "USAGE directoryOperation )",
195 &ad_pwdGraceUseTime },
196 { "( 1.3.6.1.4.1.42.2.27.8.1.22 "
197 "NAME ( 'pwdReset' ) "
198 "DESC 'The indication that the password has been reset' "
199 "EQUALITY booleanMatch "
200 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.7 "
201 "SINGLE-VALUE "
202 "USAGE directoryOperation )",
203 &ad_pwdReset },
204 { "( 1.3.6.1.4.1.42.2.27.8.1.23 "
205 "NAME ( 'pwdPolicySubentry' ) "
206 "DESC 'The pwdPolicy subentry in effect for this object' "
207 "EQUALITY distinguishedNameMatch "
208 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.12 "
209 "SINGLE-VALUE "
210 #if 0 /* ITS#9671: until we implement ITS#9343 or similar */
211 "NO-USER-MODIFICATION "
212 #endif
213 "USAGE directoryOperation )",
214 &ad_pwdPolicySubentry },
215 { "( 1.3.6.1.4.1.42.2.27.8.1.27 "
216 "NAME ( 'pwdStartTime' ) "
217 "DESC 'The time the password becomes enabled' "
218 "EQUALITY generalizedTimeMatch "
219 "ORDERING generalizedTimeOrderingMatch "
220 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
221 "SINGLE-VALUE "
222 "USAGE directoryOperation )",
223 &ad_pwdStartTime },
224 { "( 1.3.6.1.4.1.42.2.27.8.1.28 "
225 "NAME ( 'pwdEndTime' ) "
226 "DESC 'The time the password becomes disabled' "
227 "EQUALITY generalizedTimeMatch "
228 "ORDERING generalizedTimeOrderingMatch "
229 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
230 "SINGLE-VALUE "
231 "USAGE directoryOperation )",
232 &ad_pwdEndTime },
233 /* Defined in schema_prep.c now
234 { "( 1.3.6.1.4.1.42.2.27.8.1.29 "
235 "NAME ( 'pwdLastSuccess' ) "
236 "DESC 'The timestamp of the last successful authentication' "
237 "EQUALITY generalizedTimeMatch "
238 "ORDERING generalizedTimeOrderingMatch "
239 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
240 "SINGLE-VALUE "
241 "NO-USER-MODIFICATION "
242 "USAGE directoryOperation )",
243 &ad_pwdLastSuccess },
244 */
245 { "( 1.3.6.1.4.1.42.2.27.8.1.33 "
246 "NAME ( 'pwdAccountTmpLockoutEnd' ) "
247 "DESC 'Temporary lockout end' "
248 "EQUALITY generalizedTimeMatch "
249 "ORDERING generalizedTimeOrderingMatch "
250 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
251 "SINGLE-VALUE "
252 "NO-USER-MODIFICATION "
253 "USAGE directoryOperation )",
254 &ad_pwdAccountTmpLockoutEnd },
255
256 { "( 1.3.6.1.4.1.42.2.27.8.1.1 "
257 "NAME ( 'pwdAttribute' ) "
258 "EQUALITY objectIdentifierMatch "
259 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.38 )",
260 &ad_pwdAttribute },
261 { "( 1.3.6.1.4.1.42.2.27.8.1.2 "
262 "NAME ( 'pwdMinAge' ) "
263 "EQUALITY integerMatch "
264 "ORDERING integerOrderingMatch "
265 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 "
266 "SINGLE-VALUE )",
267 &ad_pwdMinAge },
268 { "( 1.3.6.1.4.1.42.2.27.8.1.3 "
269 "NAME ( 'pwdMaxAge' ) "
270 "EQUALITY integerMatch "
271 "ORDERING integerOrderingMatch "
272 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 "
273 "SINGLE-VALUE )",
274 &ad_pwdMaxAge },
275 { "( 1.3.6.1.4.1.42.2.27.8.1.4 "
276 "NAME ( 'pwdInHistory' ) "
277 "EQUALITY integerMatch "
278 "ORDERING integerOrderingMatch "
279 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 "
280 "SINGLE-VALUE )",
281 &ad_pwdInHistory },
282 { "( 1.3.6.1.4.1.42.2.27.8.1.5 "
283 "NAME ( 'pwdCheckQuality' ) "
284 "EQUALITY integerMatch "
285 "ORDERING integerOrderingMatch "
286 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 "
287 "SINGLE-VALUE )",
288 &ad_pwdCheckQuality },
289 { "( 1.3.6.1.4.1.42.2.27.8.1.6 "
290 "NAME ( 'pwdMinLength' ) "
291 "EQUALITY integerMatch "
292 "ORDERING integerOrderingMatch "
293 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 "
294 "SINGLE-VALUE )",
295 &ad_pwdMinLength },
296 { "( 1.3.6.1.4.1.42.2.27.8.1.31 "
297 "NAME ( 'pwdMaxLength' ) "
298 "EQUALITY integerMatch "
299 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 "
300 "SINGLE-VALUE )",
301 &ad_pwdMaxLength },
302 { "( 1.3.6.1.4.1.42.2.27.8.1.7 "
303 "NAME ( 'pwdExpireWarning' ) "
304 "EQUALITY integerMatch "
305 "ORDERING integerOrderingMatch "
306 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 "
307 "SINGLE-VALUE )",
308 &ad_pwdExpireWarning },
309 { "( 1.3.6.1.4.1.42.2.27.8.1.8 "
310 "NAME ( 'pwdGraceAuthNLimit' ) "
311 "EQUALITY integerMatch "
312 "ORDERING integerOrderingMatch "
313 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 "
314 "SINGLE-VALUE )",
315 &ad_pwdGraceAuthNLimit },
316 { "( 1.3.6.1.4.1.42.2.27.8.1.30 "
317 "NAME ( 'pwdGraceExpiry' ) "
318 "EQUALITY integerMatch "
319 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 "
320 "SINGLE-VALUE )",
321 &ad_pwdGraceExpiry },
322 { "( 1.3.6.1.4.1.42.2.27.8.1.9 "
323 "NAME ( 'pwdLockout' ) "
324 "EQUALITY booleanMatch "
325 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.7 "
326 "SINGLE-VALUE )",
327 &ad_pwdLockout },
328 { "( 1.3.6.1.4.1.42.2.27.8.1.10 "
329 "NAME ( 'pwdLockoutDuration' ) "
330 "EQUALITY integerMatch "
331 "ORDERING integerOrderingMatch "
332 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 "
333 "SINGLE-VALUE )",
334 &ad_pwdLockoutDuration },
335 { "( 1.3.6.1.4.1.42.2.27.8.1.11 "
336 "NAME ( 'pwdMaxFailure' ) "
337 "EQUALITY integerMatch "
338 "ORDERING integerOrderingMatch "
339 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 "
340 "SINGLE-VALUE )",
341 &ad_pwdMaxFailure },
342 { "( 1.3.6.1.4.1.42.2.27.8.1.12 "
343 "NAME ( 'pwdFailureCountInterval' ) "
344 "EQUALITY integerMatch "
345 "ORDERING integerOrderingMatch "
346 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 "
347 "SINGLE-VALUE )",
348 &ad_pwdFailureCountInterval },
349 { "( 1.3.6.1.4.1.42.2.27.8.1.13 "
350 "NAME ( 'pwdMustChange' ) "
351 "EQUALITY booleanMatch "
352 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.7 "
353 "SINGLE-VALUE )",
354 &ad_pwdMustChange },
355 { "( 1.3.6.1.4.1.42.2.27.8.1.14 "
356 "NAME ( 'pwdAllowUserChange' ) "
357 "EQUALITY booleanMatch "
358 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.7 "
359 "SINGLE-VALUE )",
360 &ad_pwdAllowUserChange },
361 { "( 1.3.6.1.4.1.42.2.27.8.1.15 "
362 "NAME ( 'pwdSafeModify' ) "
363 "EQUALITY booleanMatch "
364 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.7 "
365 "SINGLE-VALUE )",
366 &ad_pwdSafeModify },
367 { "( 1.3.6.1.4.1.42.2.27.8.1.24 "
368 "NAME ( 'pwdMinDelay' ) "
369 "EQUALITY integerMatch "
370 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 "
371 "SINGLE-VALUE )",
372 &ad_pwdMinDelay },
373 { "( 1.3.6.1.4.1.42.2.27.8.1.25 "
374 "NAME ( 'pwdMaxDelay' ) "
375 "EQUALITY integerMatch "
376 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 "
377 "SINGLE-VALUE )",
378 &ad_pwdMaxDelay },
379 { "( 1.3.6.1.4.1.42.2.27.8.1.26 "
380 "NAME ( 'pwdMaxIdle' ) "
381 "EQUALITY integerMatch "
382 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 "
383 "SINGLE-VALUE )",
384 &ad_pwdMaxIdle },
385 { "( 1.3.6.1.4.1.42.2.27.8.1.32 "
386 "NAME ( 'pwdMaxRecordedFailure' ) "
387 "EQUALITY integerMatch "
388 "ORDERING integerOrderingMatch "
389 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.27 "
390 "SINGLE-VALUE )",
391 &ad_pwdMaxRecordedFailure },
392 { "( 1.3.6.1.4.1.4754.1.99.1 "
393 "NAME ( 'pwdCheckModule' ) "
394 "EQUALITY caseExactIA5Match "
395 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.26 "
396 "DESC 'Obsolete, no longer used' "
397 "OBSOLETE "
398 "SINGLE-VALUE )",
399 &ad_pwdCheckModule },
400 { "( 1.3.6.1.4.1.4754.1.99.2 "
401 "NAME ( 'pwdCheckModuleArg' ) "
402 "EQUALITY octetStringMatch "
403 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.40 "
404 "DESC 'Argument to pass to check_password() function' "
405 "SINGLE-VALUE )",
406 &ad_pwdCheckModuleArg },
407 { "( 1.3.6.1.4.1.4754.1.99.3 "
408 "NAME ( 'pwdUseCheckModule' ) "
409 "EQUALITY booleanMatch "
410 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.7 "
411 "DESC 'Toggle use of the loaded pwdCheckModule' "
412 "SINGLE-VALUE )",
413 &ad_pwdUseCheckModule },
414
415 { NULL, NULL }
416 };
417
418 static char *pwd_ocs[] = {
419 "( 1.3.6.1.4.1.4754.2.99.1 "
420 "NAME 'pwdPolicyChecker' "
421 "SUP top "
422 "AUXILIARY "
423 "MAY ( pwdCheckModule $ pwdCheckModuleArg $ pwdUseCheckModule ) )" ,
424 "( 1.3.6.1.4.1.42.2.27.8.2.1 "
425 "NAME 'pwdPolicy' "
426 "SUP top "
427 "AUXILIARY "
428 "MUST ( pwdAttribute ) "
429 "MAY ( pwdMinAge $ pwdMaxAge $ pwdInHistory $ pwdCheckQuality $ "
430 "pwdMinLength $ pwdMaxLength $ pwdExpireWarning $ "
431 "pwdGraceAuthNLimit $ pwdGraceExpiry $ pwdLockout $ "
432 "pwdLockoutDuration $ pwdMaxFailure $ pwdFailureCountInterval $ "
433 "pwdMustChange $ pwdAllowUserChange $ pwdSafeModify $ "
434 "pwdMinDelay $ pwdMaxDelay $ pwdMaxIdle $ "
435 "pwdMaxRecordedFailure ) )",
436 NULL
437 };
438
439 static ldap_pvt_thread_mutex_t chk_syntax_mutex;
440
441 enum {
442 PPOLICY_DEFAULT = 1,
443 PPOLICY_HASH_CLEARTEXT,
444 PPOLICY_USE_LOCKOUT,
445 PPOLICY_DISABLE_WRITE,
446 PPOLICY_CHECK_MODULE,
447 };
448
449 static ConfigDriver ppolicy_cf_default, ppolicy_cf_checkmod;
450
451 static ConfigTable ppolicycfg[] = {
452 { "ppolicy_default", "policyDN", 2, 2, 0,
453 ARG_DN|ARG_QUOTE|ARG_MAGIC|PPOLICY_DEFAULT, ppolicy_cf_default,
454 "( OLcfgOvAt:12.1 NAME 'olcPPolicyDefault' "
455 "DESC 'DN of a pwdPolicy object for uncustomized objects' "
456 "EQUALITY distinguishedNameMatch "
457 "SYNTAX OMsDN SINGLE-VALUE )", NULL, NULL },
458 { "ppolicy_hash_cleartext", "on|off", 1, 2, 0,
459 ARG_ON_OFF|ARG_OFFSET|PPOLICY_HASH_CLEARTEXT,
460 (void *)offsetof(pp_info,hash_passwords),
461 "( OLcfgOvAt:12.2 NAME 'olcPPolicyHashCleartext' "
462 "DESC 'Hash passwords on add or modify' "
463 "EQUALITY booleanMatch "
464 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
465 { "ppolicy_forward_updates", "on|off", 1, 2, 0,
466 ARG_ON_OFF|ARG_OFFSET,
467 (void *)offsetof(pp_info,forward_updates),
468 "( OLcfgOvAt:12.4 NAME 'olcPPolicyForwardUpdates' "
469 "DESC 'Allow policy state updates to be forwarded via updateref' "
470 "EQUALITY booleanMatch "
471 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
472 { "ppolicy_use_lockout", "on|off", 1, 2, 0,
473 ARG_ON_OFF|ARG_OFFSET|PPOLICY_USE_LOCKOUT,
474 (void *)offsetof(pp_info,use_lockout),
475 "( OLcfgOvAt:12.3 NAME 'olcPPolicyUseLockout' "
476 "DESC 'Warn clients with AccountLocked' "
477 "EQUALITY booleanMatch "
478 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
479 { "ppolicy_disable_write", "on|off", 1, 2, 0,
480 ARG_ON_OFF|ARG_OFFSET|PPOLICY_DISABLE_WRITE,
481 (void *)offsetof(pp_info,disable_write),
482 "( OLcfgOvAt:12.5 NAME 'olcPPolicyDisableWrite' "
483 "DESC 'Prevent all policy overlay writes' "
484 "EQUALITY booleanMatch "
485 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
486 { "ppolicy_send_netscape_controls", "on|off", 1, 2, 0,
487 ARG_ON_OFF|ARG_OFFSET,
488 (void *)offsetof(pp_info,send_netscape_controls),
489 "( OLcfgOvAt:12.6 NAME 'olcPPolicySendNetscapeControls' "
490 "DESC 'Send Netscape policy controls' "
491 "EQUALITY booleanMatch "
492 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
493 { "ppolicy_check_module", "path", 2, 2, 0,
494 #ifdef SLAPD_MODULES
495 ARG_STRING|ARG_MAGIC|PPOLICY_CHECK_MODULE, ppolicy_cf_checkmod,
496 #else
497 ARG_IGNORED, NULL,
498 #endif /* SLAPD_MODULES */
499 "( OLcfgOvAt:12.7 NAME 'olcPPolicyCheckModule' "
500 "DESC 'Loadable module that instantiates check_password() function' "
501 "EQUALITY caseExactIA5Match "
502 "SYNTAX OMsIA5String "
503 "SINGLE-VALUE )", NULL, NULL },
504 { NULL, NULL, 0, 0, 0, ARG_IGNORED }
505 };
506
507 static ConfigOCs ppolicyocs[] = {
508 { "( OLcfgOvOc:12.1 "
509 "NAME 'olcPPolicyConfig' "
510 "DESC 'Password Policy configuration' "
511 "SUP olcOverlayConfig "
512 "MAY ( olcPPolicyDefault $ olcPPolicyHashCleartext $ "
513 "olcPPolicyUseLockout $ olcPPolicyForwardUpdates $ "
514 "olcPPolicyDisableWrite $ olcPPolicySendNetscapeControls $ "
515 "olcPPolicyCheckModule ) )",
516 Cft_Overlay, ppolicycfg },
517 { NULL, 0, NULL }
518 };
519
520 static int
521 ppolicy_cf_default( ConfigArgs *c )
522 {
523 slap_overinst *on = (slap_overinst *)c->bi;
524 pp_info *pi = (pp_info *)on->on_bi.bi_private;
525 int rc = ARG_BAD_CONF;
526
527 assert ( c->type == PPOLICY_DEFAULT );
528 Debug(LDAP_DEBUG_TRACE, "==> ppolicy_cf_default\n" );
529
530 switch ( c->op ) {
531 case SLAP_CONFIG_EMIT:
532 Debug(LDAP_DEBUG_TRACE, "==> ppolicy_cf_default emit\n" );
533 rc = 0;
534 if ( !BER_BVISEMPTY( &pi->def_policy )) {
535 rc = value_add_one( &c->rvalue_vals,
536 &pi->def_policy );
537 if ( rc ) return rc;
538 rc = value_add_one( &c->rvalue_nvals,
539 &pi->def_policy );
540 }
541 break;
542 case LDAP_MOD_DELETE:
543 Debug(LDAP_DEBUG_TRACE, "==> ppolicy_cf_default delete\n" );
544 if ( pi->def_policy.bv_val ) {
545 ber_memfree ( pi->def_policy.bv_val );
546 pi->def_policy.bv_val = NULL;
547 }
548 pi->def_policy.bv_len = 0;
549 rc = 0;
550 break;
551 case SLAP_CONFIG_ADD:
552 /* fallthru to LDAP_MOD_ADD */
553 case LDAP_MOD_ADD:
554 Debug(LDAP_DEBUG_TRACE, "==> ppolicy_cf_default add\n" );
555 if ( pi->def_policy.bv_val ) {
556 ber_memfree ( pi->def_policy.bv_val );
557 }
558 pi->def_policy = c->value_ndn;
559 ber_memfree( c->value_dn.bv_val );
560 BER_BVZERO( &c->value_dn );
561 BER_BVZERO( &c->value_ndn );
562 rc = 0;
563 break;
564 default:
565 abort ();
566 }
567
568 return rc;
569 }
570
571 #ifdef SLAPD_MODULES
572 static int
573 ppolicy_cf_checkmod( ConfigArgs *c )
574 {
575 slap_overinst *on = (slap_overinst *)c->bi;
576 pp_info *pi = (pp_info *)on->on_bi.bi_private;
577 int rc = ARG_BAD_CONF;
578
579 assert ( c->type == PPOLICY_CHECK_MODULE );
580 Debug(LDAP_DEBUG_TRACE, "==> ppolicy_cf_checkmod\n" );
581
582 switch ( c->op ) {
583 case SLAP_CONFIG_EMIT:
584 if ( pi->pwdCheckModule ) {
585 c->value_string = ch_strdup( pi->pwdCheckModule );
586 rc = 0;
587 }
588 break;
589 case LDAP_MOD_DELETE:
590 if ( pi->pwdCheckHandle ) {
591 lt_dlclose( pi->pwdCheckHandle );
592 pi->pwdCheckHandle = NULL;
593 pi->pwdCheckFunc = NULL;
594 }
595 ch_free( pi->pwdCheckModule );
596 pi->pwdCheckModule = NULL;
597 rc = 0;
598 break;
599 case SLAP_CONFIG_ADD:
600 /* fallthru to LDAP_MOD_ADD */
601 case LDAP_MOD_ADD:
602 pi->pwdCheckHandle = lt_dlopen( c->value_string );
603 if ( pi->pwdCheckHandle == NULL ) {
604 const char *dlerr = lt_dlerror();
605 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> lt_dlopen(%s) failed: %s",
606 c->argv[0], c->value_string, dlerr );
607 Debug(LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->cr_msg );
608 } else {
609 if (( pi->pwdCheckFunc = lt_dlsym( pi->pwdCheckHandle, "check_password" )) == NULL) {
610 const char *dlerr = lt_dlerror();
611 snprintf( c->cr_msg, sizeof( c->cr_msg ), "<%s> lt_dlsym(%s) failed: %s",
612 c->argv[0], c->value_string, dlerr );
613 Debug(LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->cr_msg );
614 } else {
615 pi->pwdCheckModule = c->value_string;
616 rc = 0;
617 }
618 }
619 break;
620 default:
621 abort ();
622 }
623
624 return rc;
625 }
626 #endif /* SLAPD_MODULES */
627
628 static time_t
629 parse_time( char *atm )
630 {
631 struct lutil_tm tm;
632 struct lutil_timet tt;
633 time_t ret = (time_t)-1;
634
635 if ( lutil_parsetime( atm, &tm ) == 0) {
636 lutil_tm2time( &tm, &tt );
637 ret = tt.tt_sec;
638 }
639 return ret;
640 }
641
642 static int
643 account_locked( Operation *op, Entry *e,
644 PassPolicy *pp, Modifications **mod )
645 {
646 Attribute *la;
647
648 if ( (la = attr_find( e->e_attrs, ad_pwdStartTime )) != NULL ) {
649 BerVarray vals = la->a_nvals;
650 time_t then, now = op->o_time;
651
652 /*
653 * Password has a defined start of validity
654 */
655 if ( vals[0].bv_val != NULL ) {
656 if ( (then = parse_time( vals[0].bv_val )) == (time_t)-1 ) {
657 return 1;
658 }
659 if ( now < then ) {
660 return 1;
661 }
662 }
663 }
664
665 if ( (la = attr_find( e->e_attrs, ad_pwdEndTime )) != NULL ) {
666 BerVarray vals = la->a_nvals;
667 time_t then, now = op->o_time;
668
669 /*
670 * Password has a defined end of validity
671 */
672 if ( vals[0].bv_val != NULL ) {
673 if ( (then = parse_time( vals[0].bv_val )) == (time_t)-1 ) {
674 return 1;
675 }
676 if ( then <= now ) {
677 return 1;
678 }
679 }
680 }
681
682 if ( !pp->pwdLockout )
683 return 0;
684
685 if ( (la = attr_find( e->e_attrs, ad_pwdAccountTmpLockoutEnd )) != NULL ) {
686 BerVarray vals = la->a_nvals;
687 time_t then, now = op->o_time;
688
689 /*
690 * We have temporarily locked the account after a failure
691 */
692 if ( vals[0].bv_val != NULL ) {
693 if ( (then = parse_time( vals[0].bv_val )) == (time_t)-1 ) {
694 return 1;
695 }
696 if ( now < then ) {
697 return 1;
698 }
699 }
700 }
701
702 /* Only check if database maintains lastbind */
703 if ( pp->pwdMaxIdle && SLAP_LASTBIND( op->o_bd ) ) {
704 time_t lastbindtime = (time_t)-1;
705
706 la = attr_find( e->e_attrs, ad_pwdLastSuccess );
707 if ( la == NULL ) {
708 la = attr_find( e->e_attrs, ad_pwdChangedTime );
709 }
710 if ( la != NULL ) {
711 lastbindtime = parse_time( la->a_nvals[0].bv_val );
712 }
713
714 if ( lastbindtime != (time_t)-1 &&
715 op->o_time > lastbindtime + pp->pwdMaxIdle ) {
716 return 1;
717 }
718 }
719
720 if ( (la = attr_find( e->e_attrs, ad_pwdAccountLockedTime )) != NULL ) {
721 BerVarray vals = la->a_nvals;
722
723 /*
724 * there is a lockout stamp - we now need to know if it's
725 * a valid one.
726 */
727 if (vals[0].bv_val != NULL) {
728 time_t then, now;
729 Modifications *m;
730
731 if ((then = parse_time( vals[0].bv_val )) == (time_t)0)
732 return 1;
733
734 now = slap_get_time();
735
736 /* Still in the future? not yet in effect */
737 if (now < then)
738 return 0;
739
740 if (!pp->pwdLockoutDuration)
741 return 1;
742
743 if (now < then + pp->pwdLockoutDuration)
744 return 1;
745
746 if ( mod != NULL ) {
747 m = ch_calloc( sizeof(Modifications), 1 );
748 m->sml_op = LDAP_MOD_DELETE;
749 m->sml_flags = 0;
750 m->sml_type = ad_pwdAccountLockedTime->ad_cname;
751 m->sml_desc = ad_pwdAccountLockedTime;
752 m->sml_next = *mod;
753 *mod = m;
754 }
755 }
756 }
757
758 return 0;
759 }
760
761 /* IMPLICIT TAGS, all context-specific */
762 #define PPOLICY_WARNING 0xa0L /* constructed + 0 */
763 #define PPOLICY_ERROR 0x81L /* primitive + 1 */
764
765 #define PPOLICY_EXPIRE 0x80L /* primitive + 0 */
766 #define PPOLICY_GRACE 0x81L /* primitive + 1 */
767
768 static const char ppolicy_ctrl_oid[] = LDAP_CONTROL_PASSWORDPOLICYRESPONSE;
769 static const char ppolicy_account_ctrl_oid[] = LDAP_CONTROL_X_ACCOUNT_USABILITY;
770 static const char ppolicy_pwd_expired_oid[] = LDAP_CONTROL_X_PASSWORD_EXPIRED;
771 static const char ppolicy_pwd_expiring_oid[] = LDAP_CONTROL_X_PASSWORD_EXPIRING;
772
773 static LDAPControl *
774 create_passcontrol( Operation *op, int exptime, int grace, LDAPPasswordPolicyError err )
775 {
776 BerElementBuffer berbuf, bb2;
777 BerElement *ber = (BerElement *) &berbuf, *b2 = (BerElement *) &bb2;
778 LDAPControl c = { 0 }, *cp;
779 struct berval bv;
780 int rc;
781
782 BER_BVZERO( &c.ldctl_value );
783
784 ber_init2( ber, NULL, LBER_USE_DER );
785 ber_printf( ber, "{" /*}*/ );
786
787 if ( exptime >= 0 ) {
788 ber_init2( b2, NULL, LBER_USE_DER );
789 ber_printf( b2, "ti", PPOLICY_EXPIRE, exptime );
790 rc = ber_flatten2( b2, &bv, 1 );
791 (void)ber_free_buf(b2);
792 if (rc == -1) {
793 cp = NULL;
794 goto fail;
795 }
796 ber_printf( ber, "tO", PPOLICY_WARNING, &bv );
797 ch_free( bv.bv_val );
798 } else if ( grace >= 0 ) {
799 ber_init2( b2, NULL, LBER_USE_DER );
800 ber_printf( b2, "ti", PPOLICY_GRACE, grace );
801 rc = ber_flatten2( b2, &bv, 1 );
802 (void)ber_free_buf(b2);
803 if (rc == -1) {
804 cp = NULL;
805 goto fail;
806 }
807 ber_printf( ber, "tO", PPOLICY_WARNING, &bv );
808 ch_free( bv.bv_val );
809 }
810
811 if (err != PP_noError ) {
812 ber_printf( ber, "te", PPOLICY_ERROR, err );
813 }
814 ber_printf( ber, /*{*/ "N}" );
815
816 if (ber_flatten2( ber, &c.ldctl_value, 0 ) == -1) {
817 return NULL;
818 }
819 cp = op->o_tmpalloc( sizeof( LDAPControl ) + c.ldctl_value.bv_len, op->o_tmpmemctx );
820 cp->ldctl_oid = (char *)ppolicy_ctrl_oid;
821 cp->ldctl_iscritical = 0;
822 cp->ldctl_value.bv_val = (char *)&cp[1];
823 cp->ldctl_value.bv_len = c.ldctl_value.bv_len;
824 AC_MEMCPY( cp->ldctl_value.bv_val, c.ldctl_value.bv_val, c.ldctl_value.bv_len );
825 fail:
826 (void)ber_free_buf(ber);
827
828 return cp;
829 }
830
831 static LDAPControl *
832 create_passexpiry( Operation *op, int expired, int warn )
833 {
834 LDAPControl *cp;
835 char buf[sizeof("-2147483648")];
836 struct berval bv = { .bv_val = buf, .bv_len = sizeof(buf) };
837
838 bv.bv_len = snprintf( bv.bv_val, bv.bv_len, "%d", warn );
839
840 cp = op->o_tmpalloc( sizeof( LDAPControl ) + bv.bv_len, op->o_tmpmemctx );
841 if ( expired ) {
842 cp->ldctl_oid = (char *)ppolicy_pwd_expired_oid;
843 } else {
844 cp->ldctl_oid = (char *)ppolicy_pwd_expiring_oid;
845 }
846 cp->ldctl_iscritical = 0;
847 cp->ldctl_value.bv_val = (char *)&cp[1];
848 cp->ldctl_value.bv_len = bv.bv_len;
849 AC_MEMCPY( cp->ldctl_value.bv_val, bv.bv_val, bv.bv_len );
850 return cp;
851 }
852
853 static LDAPControl **
854 add_passcontrol( Operation *op, SlapReply *rs, LDAPControl *ctrl )
855 {
856 LDAPControl **ctrls, **oldctrls = rs->sr_ctrls;
857 int n;
858
859 n = 0;
860 if ( oldctrls ) {
861 for ( ; oldctrls[n]; n++ )
862 ;
863 }
864 n += 2;
865
866 ctrls = op->o_tmpcalloc( sizeof( LDAPControl * ), n, op->o_tmpmemctx );
867
868 n = 0;
869 if ( oldctrls ) {
870 for ( ; oldctrls[n]; n++ ) {
871 ctrls[n] = oldctrls[n];
872 }
873 }
874 ctrls[n] = ctrl;
875 ctrls[n+1] = NULL;
876
877 rs->sr_ctrls = ctrls;
878
879 return oldctrls;
880 }
881
882 static void
883 add_account_control(
884 Operation *op,
885 SlapReply *rs,
886 int available,
887 int remaining,
888 LDAPAccountUsabilityMoreInfo *more_info )
889 {
890 BerElementBuffer berbuf;
891 BerElement *ber = (BerElement *) &berbuf;
892 LDAPControl c = { 0 }, *cp = NULL, **ctrls;
893 int i = 0;
894
895 BER_BVZERO( &c.ldctl_value );
896
897 ber_init2( ber, NULL, LBER_USE_DER );
898
899 if ( available ) {
900 ber_put_int( ber, remaining, LDAP_TAG_X_ACCOUNT_USABILITY_AVAILABLE );
901 } else {
902 assert( more_info != NULL );
903
904 ber_start_seq( ber, LDAP_TAG_X_ACCOUNT_USABILITY_NOT_AVAILABLE );
905 ber_put_boolean( ber, more_info->inactive, LDAP_TAG_X_ACCOUNT_USABILITY_INACTIVE );
906 ber_put_boolean( ber, more_info->reset, LDAP_TAG_X_ACCOUNT_USABILITY_RESET );
907 ber_put_boolean( ber, more_info->expired, LDAP_TAG_X_ACCOUNT_USABILITY_EXPIRED );
908 ber_put_int( ber, more_info->remaining_grace, LDAP_TAG_X_ACCOUNT_USABILITY_REMAINING_GRACE );
909 ber_put_int( ber, more_info->seconds_before_unlock, LDAP_TAG_X_ACCOUNT_USABILITY_UNTIL_UNLOCK );
910 ber_put_seq( ber );
911 }
912
913 if (ber_flatten2( ber, &c.ldctl_value, 0 ) == -1) {
914 goto fail;
915 }
916
917 if ( rs->sr_ctrls != NULL ) {
918 for ( ; rs->sr_ctrls[ i ] != NULL; i++ ) /* Count */;
919 }
920
921 ctrls = op->o_tmprealloc( rs->sr_ctrls, sizeof(LDAPControl *)*( i + 2 ), op->o_tmpmemctx );
922 if ( ctrls == NULL ) {
923 goto fail;
924 }
925
926 cp = op->o_tmpalloc( sizeof( LDAPControl ) + c.ldctl_value.bv_len, op->o_tmpmemctx );
927 cp->ldctl_oid = (char *)ppolicy_account_ctrl_oid;
928 cp->ldctl_iscritical = 0;
929 cp->ldctl_value.bv_val = (char *)&cp[1];
930 cp->ldctl_value.bv_len = c.ldctl_value.bv_len;
931 AC_MEMCPY( cp->ldctl_value.bv_val, c.ldctl_value.bv_val, c.ldctl_value.bv_len );
932
933 ctrls[ i ] = cp;
934 ctrls[ i + 1 ] = NULL;
935 rs->sr_ctrls = ctrls;
936
937 fail:
938 (void)ber_free_buf(ber);
939 }
940
941 static void
942 ppolicy_get_default( PassPolicy *pp )
943 {
944 memset( pp, 0, sizeof(PassPolicy) );
945
946 pp->ad = slap_schema.si_ad_userPassword;
947
948 /* Users can change their own password by default */
949 pp->pwdAllowUserChange = 1;
950 }
951
952
953 static int
954 ppolicy_get( Operation *op, Entry *e, PassPolicy *pp )
955 {
956 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
957 pp_info *pi = on->on_bi.bi_private;
958 BackendDB *bd, *bd_orig = op->o_bd;
959 AttributeDescription *ad = NULL;
960 Attribute *a;
961 BerVarray vals;
962 int rc = LDAP_SUCCESS;
963 Entry *pe = NULL;
964 #if 0
965 const char *text;
966 #endif
967
968 ppolicy_get_default( pp );
969
970 ad = ad_pwdPolicySubentry;
971 if ( (a = attr_find( e->e_attrs, ad )) == NULL ) {
972 /*
973 * entry has no password policy assigned - use default
974 */
975 vals = &pi->def_policy;
976 if ( !vals->bv_val )
977 goto defaultpol;
978 } else {
979 vals = a->a_nvals;
980 if (vals[0].bv_val == NULL) {
981 Debug( LDAP_DEBUG_ANY,
982 "ppolicy_get: NULL value for policySubEntry\n" );
983 goto defaultpol;
984 }
985 }
986
987 op->o_bd = bd = select_backend( vals, 0 );
988 if ( op->o_bd == NULL ) {
989 op->o_bd = bd_orig;
990 goto defaultpol;
991 }
992
993 rc = be_entry_get_rw( op, vals, NULL, NULL, 0, &pe );
994 op->o_bd = bd_orig;
995
996 if ( rc ) goto defaultpol;
997
998 #if 0 /* Only worry about userPassword for now */
999 if ((a = attr_find( pe->e_attrs, ad_pwdAttribute )))
1000 slap_bv2ad( &a->a_vals[0], &pp->ad, &text );
1001 #endif
1002
1003 ad = ad_pwdMinAge;
1004 if ( (a = attr_find( pe->e_attrs, ad ))
1005 && lutil_atoi( &pp->pwdMinAge, a->a_vals[0].bv_val ) != 0 ) {
1006 rc = LDAP_CONSTRAINT_VIOLATION;
1007 goto defaultpol;
1008 }
1009
1010 ad = ad_pwdMaxAge;
1011 if ( (a = attr_find( pe->e_attrs, ad ))
1012 && lutil_atoi( &pp->pwdMaxAge, a->a_vals[0].bv_val ) != 0 ) {
1013 rc = LDAP_CONSTRAINT_VIOLATION;
1014 goto defaultpol;
1015 }
1016
1017 ad = ad_pwdMaxIdle;
1018 if ( (a = attr_find( pe->e_attrs, ad ))
1019 && lutil_atoi( &pp->pwdMaxIdle, a->a_vals[0].bv_val ) != 0 ) {
1020 rc = LDAP_CONSTRAINT_VIOLATION;
1021 goto defaultpol;
1022 }
1023
1024 ad = ad_pwdInHistory;
1025 if ( (a = attr_find( pe->e_attrs, ad ))
1026 && lutil_atoi( &pp->pwdInHistory, a->a_vals[0].bv_val ) != 0 ) {
1027 rc = LDAP_CONSTRAINT_VIOLATION;
1028 goto defaultpol;
1029 }
1030
1031 ad = ad_pwdCheckQuality;
1032 if ( (a = attr_find( pe->e_attrs, ad ))
1033 && lutil_atoi( &pp->pwdCheckQuality, a->a_vals[0].bv_val ) != 0 ) {
1034 rc = LDAP_CONSTRAINT_VIOLATION;
1035 goto defaultpol;
1036 }
1037
1038 ad = ad_pwdMinLength;
1039 if ( (a = attr_find( pe->e_attrs, ad ))
1040 && lutil_atoi( &pp->pwdMinLength, a->a_vals[0].bv_val ) != 0 ) {
1041 rc = LDAP_CONSTRAINT_VIOLATION;
1042 goto defaultpol;
1043 }
1044
1045 ad = ad_pwdMaxLength;
1046 if ( (a = attr_find( pe->e_attrs, ad ))
1047 && lutil_atoi( &pp->pwdMaxLength, a->a_vals[0].bv_val ) != 0 ) {
1048 rc = LDAP_CONSTRAINT_VIOLATION;
1049 goto defaultpol;
1050 }
1051
1052 ad = ad_pwdMaxFailure;
1053 if ( (a = attr_find( pe->e_attrs, ad ))
1054 && lutil_atoi( &pp->pwdMaxFailure, a->a_vals[0].bv_val ) != 0 ) {
1055 rc = LDAP_CONSTRAINT_VIOLATION;
1056 goto defaultpol;
1057 }
1058
1059 ad = ad_pwdMaxRecordedFailure;
1060 if ( (a = attr_find( pe->e_attrs, ad ))
1061 && lutil_atoi( &pp->pwdMaxRecordedFailure, a->a_vals[0].bv_val ) != 0 ) {
1062 rc = LDAP_CONSTRAINT_VIOLATION;
1063 goto defaultpol;
1064 }
1065
1066 ad = ad_pwdGraceExpiry;
1067 if ( (a = attr_find( pe->e_attrs, ad ))
1068 && lutil_atoi( &pp->pwdGraceExpiry, a->a_vals[0].bv_val ) != 0 ) {
1069 rc = LDAP_CONSTRAINT_VIOLATION;
1070 goto defaultpol;
1071 }
1072
1073 ad = ad_pwdGraceAuthNLimit;
1074 if ( (a = attr_find( pe->e_attrs, ad ))
1075 && lutil_atoi( &pp->pwdGraceAuthNLimit, a->a_vals[0].bv_val ) != 0 ) {
1076 rc = LDAP_CONSTRAINT_VIOLATION;
1077 goto defaultpol;
1078 }
1079
1080 ad = ad_pwdExpireWarning;
1081 if ( (a = attr_find( pe->e_attrs, ad ))
1082 && lutil_atoi( &pp->pwdExpireWarning, a->a_vals[0].bv_val ) != 0 ) {
1083 rc = LDAP_CONSTRAINT_VIOLATION;
1084 goto defaultpol;
1085 }
1086
1087 ad = ad_pwdFailureCountInterval;
1088 if ( (a = attr_find( pe->e_attrs, ad ))
1089 && lutil_atoi( &pp->pwdFailureCountInterval, a->a_vals[0].bv_val ) != 0 ) {
1090 rc = LDAP_CONSTRAINT_VIOLATION;
1091 goto defaultpol;
1092 }
1093
1094 ad = ad_pwdLockoutDuration;
1095 if ( (a = attr_find( pe->e_attrs, ad ))
1096 && lutil_atoi( &pp->pwdLockoutDuration, a->a_vals[0].bv_val ) != 0 ) {
1097 rc = LDAP_CONSTRAINT_VIOLATION;
1098 goto defaultpol;
1099 }
1100
1101 ad = ad_pwdMinDelay;
1102 if ( (a = attr_find( pe->e_attrs, ad ))
1103 && lutil_atoi( &pp->pwdMinDelay, a->a_vals[0].bv_val ) != 0 ) {
1104 rc = LDAP_CONSTRAINT_VIOLATION;
1105 goto defaultpol;
1106 }
1107
1108 ad = ad_pwdMaxDelay;
1109 if ( (a = attr_find( pe->e_attrs, ad ))
1110 && lutil_atoi( &pp->pwdMaxDelay, a->a_vals[0].bv_val ) != 0 ) {
1111 rc = LDAP_CONSTRAINT_VIOLATION;
1112 goto defaultpol;
1113 }
1114
1115 ad = ad_pwdCheckModule;
1116 if ( attr_find( pe->e_attrs, ad )) {
1117 Debug( LDAP_DEBUG_ANY, "ppolicy_get: "
1118 "WARNING: Ignoring OBSOLETE attribute %s in policy %s.\n",
1119 ad->ad_cname.bv_val, pe->e_name.bv_val );
1120 }
1121
1122 ad = ad_pwdUseCheckModule;
1123 if ( (a = attr_find( pe->e_attrs, ad )) )
1124 pp->pwdUseCheckModule = bvmatch( &a->a_nvals[0], &slap_true_bv );
1125
1126 ad = ad_pwdCheckModuleArg;
1127 if ( (a = attr_find( pe->e_attrs, ad )) ) {
1128 ber_dupbv_x( &pp->pwdCheckModuleArg, &a->a_vals[0], op->o_tmpmemctx );
1129 }
1130
1131 ad = ad_pwdLockout;
1132 if ( (a = attr_find( pe->e_attrs, ad )) )
1133 pp->pwdLockout = bvmatch( &a->a_nvals[0], &slap_true_bv );
1134
1135 ad = ad_pwdMustChange;
1136 if ( (a = attr_find( pe->e_attrs, ad )) )
1137 pp->pwdMustChange = bvmatch( &a->a_nvals[0], &slap_true_bv );
1138
1139 ad = ad_pwdAllowUserChange;
1140 if ( (a = attr_find( pe->e_attrs, ad )) )
1141 pp->pwdAllowUserChange = bvmatch( &a->a_nvals[0], &slap_true_bv );
1142
1143 ad = ad_pwdSafeModify;
1144 if ( (a = attr_find( pe->e_attrs, ad )) )
1145 pp->pwdSafeModify = bvmatch( &a->a_nvals[0], &slap_true_bv );
1146
1147 if ( pp->pwdMaxRecordedFailure < pp->pwdMaxFailure )
1148 pp->pwdMaxRecordedFailure = pp->pwdMaxFailure;
1149
1150 if ( !pp->pwdMaxRecordedFailure && pp->pwdMinDelay )
1151 pp->pwdMaxRecordedFailure = PPOLICY_DEFAULT_MAXRECORDED_FAILURE;
1152
1153 if ( pp->pwdMinDelay && !pp->pwdMaxDelay ) {
1154 Debug( LDAP_DEBUG_ANY, "ppolicy_get: "
1155 "pwdMinDelay was set but pwdMaxDelay wasn't, assuming they "
1156 "are equal\n" );
1157 pp->pwdMaxDelay = pp->pwdMinDelay;
1158 }
1159
1160 op->o_bd = bd;
1161 be_entry_release_r( op, pe );
1162 op->o_bd = bd_orig;
1163
1164 return LDAP_SUCCESS;
1165
1166 defaultpol:
1167 if ( pe ) {
1168 op->o_bd = bd;
1169 be_entry_release_r( op, pe );
1170 op->o_bd = bd_orig;
1171 }
1172
1173 if ( rc && !BER_BVISNULL( vals ) ) {
1174 Debug( LDAP_DEBUG_ANY, "ppolicy_get: "
1175 "policy subentry %s missing or invalid at '%s', "
1176 "no policy will be applied!\n",
1177 vals->bv_val, ad ? ad->ad_cname.bv_val : "" );
1178 } else {
1179 Debug( LDAP_DEBUG_TRACE,
1180 "ppolicy_get: using default policy\n" );
1181 }
1182
1183 ppolicy_get_default( pp );
1184
1185 return -1;
1186 }
1187
1188 static int
1189 password_scheme( struct berval *cred, struct berval *sch )
1190 {
1191 int e;
1192
1193 assert( cred != NULL );
1194
1195 if (sch) {
1196 sch->bv_val = NULL;
1197 sch->bv_len = 0;
1198 }
1199
1200 if ((cred->bv_len == 0) || (cred->bv_val == NULL) ||
1201 (cred->bv_val[0] != '{')) return LDAP_OTHER;
1202
1203 for(e = 1; cred->bv_val[e] && cred->bv_val[e] != '}'; e++);
1204 if (cred->bv_val[e]) {
1205 int rc;
1206 rc = lutil_passwd_scheme( cred->bv_val );
1207 if (rc) {
1208 if (sch) {
1209 sch->bv_val = cred->bv_val;
1210 sch->bv_len = e;
1211 }
1212 return LDAP_SUCCESS;
1213 }
1214 }
1215 return LDAP_OTHER;
1216 }
1217
1218 static int
1219 check_password_quality( struct berval *cred, pp_info *pi, PassPolicy *pp, LDAPPasswordPolicyError *err,
1220 Entry *e, struct berval *errmsg )
1221 {
1222 int rc = LDAP_SUCCESS, ok = LDAP_SUCCESS;
1223 char *ptr;
1224 struct berval sch;
1225
1226 assert( cred != NULL );
1227 assert( pp != NULL );
1228 assert( errmsg != NULL );
1229
1230 ptr = errmsg->bv_val;
1231 *ptr = '\0';
1232
1233 ptr = cred->bv_val;
1234
1235 if ((cred->bv_len == 0) || (pp->pwdMinLength > cred->bv_len)) {
1236 rc = LDAP_CONSTRAINT_VIOLATION;
1237 if ( err ) *err = PP_passwordTooShort;
1238 return rc;
1239 }
1240
1241 if ( pp->pwdMaxLength && cred->bv_len > pp->pwdMaxLength ) {
1242 rc = LDAP_CONSTRAINT_VIOLATION;
1243 if ( err ) *err = PP_passwordTooLong;
1244 return rc;
1245 }
1246
1247 /*
1248 * We need to know if the password is already hashed - if so
1249 * what scheme is it. The reason being that the "hash" of
1250 * {cleartext} still allows us to check the password.
1251 */
1252 rc = password_scheme( cred, &sch );
1253 if (rc == LDAP_SUCCESS) {
1254 if ((sch.bv_val) && (strncasecmp( sch.bv_val, "{cleartext}",
1255 sch.bv_len ) == 0)) {
1256 /*
1257 * We can check the cleartext "hash"
1258 */
1259 ptr = cred->bv_val + sch.bv_len;
1260 } else {
1261 /* everything else, we can't check */
1262 if (pp->pwdCheckQuality == 2) {
1263 rc = LDAP_CONSTRAINT_VIOLATION;
1264 if (err) *err = PP_insufficientPasswordQuality;
1265 return rc;
1266 }
1267 /*
1268 * We can't check the syntax of the password, but it's not
1269 * mandatory (according to the policy), so we return success.
1270 */
1271
1272 return LDAP_SUCCESS;
1273 }
1274 }
1275
1276 rc = LDAP_SUCCESS;
1277
1278 if (pp->pwdUseCheckModule) {
1279 #ifdef SLAPD_MODULES
1280 check_func *prog;
1281
1282 if ( !pi->pwdCheckFunc ) {
1283 Debug(LDAP_DEBUG_ANY,
1284 "check_password_quality: no CheckModule loaded\n" );
1285 ok = LDAP_OTHER;
1286 } else {
1287 struct berval *arg = NULL;
1288 if ( !BER_BVISNULL( &pp->pwdCheckModuleArg ) ) {
1289 arg = &pp->pwdCheckModuleArg;
1290 }
1291
1292 ldap_pvt_thread_mutex_lock( &chk_syntax_mutex );
1293 ok = pi->pwdCheckFunc( ptr, errmsg, e, arg );
1294 ldap_pvt_thread_mutex_unlock( &chk_syntax_mutex );
1295 if (ok != LDAP_SUCCESS) {
1296 Debug(LDAP_DEBUG_ANY,
1297 "check_password_quality: module error: (%s) %s.[%d]\n",
1298 pi->pwdCheckModule, errmsg->bv_val ? errmsg->bv_val : "", ok );
1299 }
1300 }
1301 #else
1302 Debug(LDAP_DEBUG_ANY, "check_password_quality: external modules not "
1303 "supported. pwdCheckModule ignored.\n" );
1304 #endif /* SLAPD_MODULES */
1305 }
1306
1307 if (ok != LDAP_SUCCESS) {
1308 rc = LDAP_CONSTRAINT_VIOLATION;
1309 if (err) *err = PP_insufficientPasswordQuality;
1310 }
1311
1312 return rc;
1313 }
1314
1315 static int
1316 parse_pwdhistory( struct berval *bv, char **oid, time_t *oldtime, struct berval *oldpw )
1317 {
1318 char *ptr;
1319 struct berval nv, npw;
1320 ber_len_t i, j;
1321
1322 assert (bv && (bv->bv_len > 0) && (bv->bv_val) && oldtime && oldpw );
1323
1324 if ( oid ) {
1325 *oid = 0;
1326 }
1327 *oldtime = (time_t)-1;
1328 BER_BVZERO( oldpw );
1329
1330 ber_dupbv( &nv, bv );
1331
1332 /* first get the time field */
1333 for ( i = 0; (i < nv.bv_len) && (nv.bv_val[i] != '#'); i++ )
1334 ;
1335 if ( i == nv.bv_len ) {
1336 goto exit_failure; /* couldn't locate the '#' separator */
1337 }
1338 nv.bv_val[i++] = '\0'; /* terminate the string & move to next field */
1339 ptr = nv.bv_val;
1340 *oldtime = parse_time( ptr );
1341 if (*oldtime == (time_t)-1) {
1342 goto exit_failure;
1343 }
1344
1345 /* get the OID field */
1346 for (ptr = &(nv.bv_val[i]); (i < nv.bv_len) && (nv.bv_val[i] != '#'); i++ )
1347 ;
1348 if ( i == nv.bv_len ) {
1349 goto exit_failure; /* couldn't locate the '#' separator */
1350 }
1351 nv.bv_val[i++] = '\0'; /* terminate the string & move to next field */
1352 if ( oid ) {
1353 *oid = ber_strdup( ptr );
1354 }
1355
1356 /* get the length field */
1357 for ( ptr = &(nv.bv_val[i]); (i < nv.bv_len) && (nv.bv_val[i] != '#'); i++ )
1358 ;
1359 if ( i == nv.bv_len ) {
1360 goto exit_failure; /* couldn't locate the '#' separator */
1361 }
1362 nv.bv_val[i++] = '\0'; /* terminate the string & move to next field */
1363 oldpw->bv_len = strtol( ptr, NULL, 10 );
1364 if (errno == ERANGE) {
1365 goto exit_failure;
1366 }
1367
1368 /* lastly, get the octets of the string */
1369 for ( j = i, ptr = &(nv.bv_val[i]); i < nv.bv_len; i++ )
1370 ;
1371 if ( i - j != oldpw->bv_len) {
1372 goto exit_failure; /* length is wrong */
1373 }
1374
1375 npw.bv_val = ptr;
1376 npw.bv_len = oldpw->bv_len;
1377 ber_dupbv( oldpw, &npw );
1378 ber_memfree( nv.bv_val );
1379
1380 return LDAP_SUCCESS;
1381
1382 exit_failure:;
1383 if ( oid && *oid ) {
1384 ber_memfree(*oid);
1385 *oid = NULL;
1386 }
1387 if ( oldpw->bv_val ) {
1388 ber_memfree( oldpw->bv_val);
1389 BER_BVZERO( oldpw );
1390 }
1391 ber_memfree( nv.bv_val );
1392
1393 return LDAP_OTHER;
1394 }
1395
1396 static void
1397 add_to_pwd_history( pw_hist **l, time_t t,
1398 struct berval *oldpw, struct berval *bv )
1399 {
1400 pw_hist *p, *p1, *p2;
1401
1402 if (!l) return;
1403
1404 p = ch_malloc( sizeof( pw_hist ));
1405 p->pw = *oldpw;
1406 ber_dupbv( &p->bv, bv );
1407 p->t = t;
1408 p->next = NULL;
1409
1410 if (*l == NULL) {
1411 /* degenerate case */
1412 *l = p;
1413 return;
1414 }
1415 /*
1416 * advance p1 and p2 such that p1 is the node before the
1417 * new one, and p2 is the node after it
1418 */
1419 for (p1 = NULL, p2 = *l; p2 && p2->t <= t; p1 = p2, p2=p2->next );
1420 p->next = p2;
1421 if (p1 == NULL) { *l = p; return; }
1422 p1->next = p;
1423 }
1424
1425 #ifndef MAX_PWD_HISTORY_SZ
1426 #define MAX_PWD_HISTORY_SZ 1024
1427 #endif /* MAX_PWD_HISTORY_SZ */
1428
1429 static void
1430 make_pwd_history_value( char *timebuf, struct berval *bv, Attribute *pa )
1431 {
1432 char str[ MAX_PWD_HISTORY_SZ ];
1433 int nlen;
1434
1435 snprintf( str, MAX_PWD_HISTORY_SZ,
1436 "%s#%s#%lu#", timebuf,
1437 pa->a_desc->ad_type->sat_syntax->ssyn_oid,
1438 (unsigned long) pa->a_nvals[0].bv_len );
1439 str[MAX_PWD_HISTORY_SZ-1] = 0;
1440 nlen = strlen(str);
1441
1442 /*
1443 * We have to assume that the string is a string of octets,
1444 * not readable characters. In reality, yes, it probably is
1445 * a readable (ie, base64) string, but we can't count on that
1446 * Hence, while the first 3 fields of the password history
1447 * are definitely readable (a timestamp, an OID and an integer
1448 * length), the remaining octets of the actual password
1449 * are deemed to be binary data.
1450 */
1451 AC_MEMCPY( str + nlen, pa->a_nvals[0].bv_val, pa->a_nvals[0].bv_len );
1452 nlen += pa->a_nvals[0].bv_len;
1453 bv->bv_val = ch_malloc( nlen + 1 );
1454 AC_MEMCPY( bv->bv_val, str, nlen );
1455 bv->bv_val[nlen] = '\0';
1456 bv->bv_len = nlen;
1457 }
1458
1459 static void
1460 free_pwd_history_list( pw_hist **l )
1461 {
1462 pw_hist *p;
1463
1464 if (!l) return;
1465 p = *l;
1466 while (p) {
1467 pw_hist *pp = p->next;
1468
1469 free(p->pw.bv_val);
1470 free(p->bv.bv_val);
1471 free(p);
1472 p = pp;
1473 }
1474 *l = NULL;
1475 }
1476
1477 typedef struct ppbind {
1478 pp_info *pi;
1479 BackendDB *be;
1480 int send_ctrl;
1481 int set_restrict;
1482 LDAPControl **oldctrls;
1483 Modifications *mod;
1484 LDAPPasswordPolicyError pErr;
1485 PassPolicy pp;
1486 } ppbind;
1487
1488 static void
1489 ctrls_cleanup( Operation *op, SlapReply *rs, LDAPControl **oldctrls )
1490 {
1491 int n;
1492
1493 assert( rs->sr_ctrls != NULL );
1494 assert( rs->sr_ctrls[0] != NULL );
1495
1496 for ( n = 0; rs->sr_ctrls[n]; n++ ) {
1497 if ( rs->sr_ctrls[n]->ldctl_oid == ppolicy_ctrl_oid ||
1498 rs->sr_ctrls[n]->ldctl_oid == ppolicy_pwd_expired_oid ||
1499 rs->sr_ctrls[n]->ldctl_oid == ppolicy_pwd_expiring_oid ) {
1500 op->o_tmpfree( rs->sr_ctrls[n], op->o_tmpmemctx );
1501 rs->sr_ctrls[n] = (LDAPControl *)(-1);
1502 break;
1503 }
1504 }
1505
1506 if ( rs->sr_ctrls[n] == NULL ) {
1507 /* missed? */
1508 }
1509
1510 op->o_tmpfree( rs->sr_ctrls, op->o_tmpmemctx );
1511
1512 rs->sr_ctrls = oldctrls;
1513 }
1514
1515 static int
1516 ppolicy_ctrls_cleanup( Operation *op, SlapReply *rs )
1517 {
1518 ppbind *ppb = op->o_callback->sc_private;
1519 if ( ppb->send_ctrl ) {
1520 ctrls_cleanup( op, rs, ppb->oldctrls );
1521 }
1522 return SLAP_CB_CONTINUE;
1523 }
1524
1525 static int
1526 ppolicy_bind_response( Operation *op, SlapReply *rs )
1527 {
1528 ppbind *ppb = op->o_callback->sc_private;
1529 pp_info *pi = ppb->pi;
1530 Modifications *mod = ppb->mod, *m;
1531 int pwExpired = 0;
1532 int ngut = -1, warn = -1, fc = 0, age, rc;
1533 Attribute *a;
1534 time_t now, pwtime = (time_t)-1;
1535 struct lutil_tm now_tm;
1536 struct lutil_timet now_usec;
1537 char nowstr[ LDAP_LUTIL_GENTIME_BUFSIZE ];
1538 char nowstr_usec[ LDAP_LUTIL_GENTIME_BUFSIZE+8 ];
1539 struct berval timestamp, timestamp_usec;
1540 BackendDB *be = op->o_bd;
1541 LDAPControl *ctrl = NULL;
1542 Entry *e;
1543
1544 ldap_pvt_thread_mutex_lock( &pi->pwdFailureTime_mutex );
1545 /* If we already know it's locked, just get on with it */
1546 if ( ppb->pErr != PP_noError ) {
1547 goto locked;
1548 }
1549
1550 op->o_bd = ppb->be;
1551 rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e );
1552 op->o_bd = be;
1553
1554 if ( rc != LDAP_SUCCESS ) {
1555 ldap_pvt_thread_mutex_unlock( &pi->pwdFailureTime_mutex );
1556 return SLAP_CB_CONTINUE;
1557 }
1558
1559 /* ITS#7089 Skip lockout checks/modifications if password attribute missing */
1560 if ( attr_find( e->e_attrs, ppb->pp.ad ) == NULL ) {
1561 goto done;
1562 }
1563
1564 ldap_pvt_gettime(&now_tm); /* stored for later consideration */
1565 lutil_tm2time(&now_tm, &now_usec);
1566 now = now_usec.tt_sec;
1567 timestamp.bv_val = nowstr;
1568 timestamp.bv_len = sizeof(nowstr);
1569 slap_timestamp( &now, &timestamp );
1570
1571 /* Separate timestamp for pwdFailureTime with microsecond granularity */
1572 strcpy(nowstr_usec, nowstr);
1573 timestamp_usec.bv_val = nowstr_usec;
1574 timestamp_usec.bv_len = timestamp.bv_len;
1575 snprintf( timestamp_usec.bv_val + timestamp_usec.bv_len-1, sizeof(".123456Z"), ".%06dZ", now_usec.tt_nsec / 1000 );
1576 timestamp_usec.bv_len += STRLENOF(".123456");
1577
1578 if ( rs->sr_err == LDAP_INVALID_CREDENTIALS && ppb->pp.pwdMaxRecordedFailure ) {
1579 int i = 0;
1580
1581 m = ch_calloc( sizeof(Modifications), 1 );
1582 m->sml_op = LDAP_MOD_ADD;
1583 m->sml_flags = 0;
1584 m->sml_type = ad_pwdFailureTime->ad_cname;
1585 m->sml_desc = ad_pwdFailureTime;
1586 m->sml_numvals = 1;
1587 m->sml_values = ch_calloc( sizeof(struct berval), 2 );
1588 m->sml_nvalues = ch_calloc( sizeof(struct berval), 2 );
1589
1590 ber_dupbv( &m->sml_values[0], &timestamp_usec );
1591 ber_dupbv( &m->sml_nvalues[0], &timestamp_usec );
1592 m->sml_next = mod;
1593 mod = m;
1594
1595 /*
1596 * Count the pwdFailureTimes - if it's
1597 * greater than the policy pwdMaxFailure,
1598 * then lock the account.
1599 */
1600 if ((a = attr_find( e->e_attrs, ad_pwdFailureTime )) != NULL) {
1601 for(i=0; a->a_nvals[i].bv_val; i++) {
1602
1603 /*
1604 * If the interval is 0, then failures
1605 * stay on the record until explicitly
1606 * reset by successful authentication.
1607 */
1608 if (ppb->pp.pwdFailureCountInterval == 0) {
1609 fc++;
1610 } else if (now <=
1611 parse_time(a->a_nvals[i].bv_val) +
1612 ppb->pp.pwdFailureCountInterval) {
1613
1614 fc++;
1615 }
1616 /*
1617 * We only count those failures
1618 * which are not due to expire.
1619 */
1620 }
1621 /* Do we have too many timestamps? If so, delete some values.
1622 * We don't bother to sort the values here. OpenLDAP keeps the
1623 * values in order by default. Fundamentally, relying on the
1624 * information here is wrong anyway; monitoring systems should
1625 * be tracking Bind failures in syslog, not here.
1626 */
1627 if (a->a_numvals >= ppb->pp.pwdMaxRecordedFailure) {
1628 int j = ppb->pp.pwdMaxRecordedFailure-1;
1629 /* If more than 2x, cheaper to perform a Replace */
1630 if (a->a_numvals >= 2 * ppb->pp.pwdMaxRecordedFailure) {
1631 struct berval v, nv;
1632
1633 /* Change the mod we constructed above */
1634 m->sml_op = LDAP_MOD_REPLACE;
1635 m->sml_numvals = ppb->pp.pwdMaxRecordedFailure;
1636 v = m->sml_values[0];
1637 nv = m->sml_nvalues[0];
1638 ch_free(m->sml_values);
1639 ch_free(m->sml_nvalues);
1640 m->sml_values = ch_calloc( sizeof(struct berval), ppb->pp.pwdMaxRecordedFailure+1 );
1641 m->sml_nvalues = ch_calloc( sizeof(struct berval), ppb->pp.pwdMaxRecordedFailure+1 );
1642 for (i=0; i<j; i++) {
1643 ber_dupbv(&m->sml_values[i], &a->a_vals[a->a_numvals-j+i]);
1644 ber_dupbv(&m->sml_nvalues[i], &a->a_nvals[a->a_numvals-j+i]);
1645 }
1646 m->sml_values[i] = v;
1647 m->sml_nvalues[i] = nv;
1648 } else {
1649 /* else just delete some */
1650 m = ch_calloc( sizeof(Modifications), 1 );
1651 m->sml_op = LDAP_MOD_DELETE;
1652 m->sml_type = ad_pwdFailureTime->ad_cname;
1653 m->sml_desc = ad_pwdFailureTime;
1654 m->sml_numvals = a->a_numvals - j;
1655 m->sml_values = ch_calloc( sizeof(struct berval), m->sml_numvals+1 );
1656 m->sml_nvalues = ch_calloc( sizeof(struct berval), m->sml_numvals+1 );
1657 for (i=0; i<m->sml_numvals; i++) {
1658 ber_dupbv(&m->sml_values[i], &a->a_vals[i]);
1659 ber_dupbv(&m->sml_nvalues[i], &a->a_nvals[i]);
1660 }
1661 m->sml_next = mod;
1662 mod = m;
1663 }
1664 }
1665 }
1666
1667 if ((ppb->pp.pwdMaxFailure > 0) &&
1668 (fc >= ppb->pp.pwdMaxFailure - 1)) {
1669
1670 /*
1671 * We subtract 1 from the failure max
1672 * because the new failure entry hasn't
1673 * made it to the entry yet.
1674 */
1675 m = ch_calloc( sizeof(Modifications), 1 );
1676 m->sml_op = LDAP_MOD_REPLACE;
1677 m->sml_flags = 0;
1678 m->sml_type = ad_pwdAccountLockedTime->ad_cname;
1679 m->sml_desc = ad_pwdAccountLockedTime;
1680 m->sml_numvals = 1;
1681 m->sml_values = ch_calloc( sizeof(struct berval), 2 );
1682 m->sml_nvalues = ch_calloc( sizeof(struct berval), 2 );
1683 ber_dupbv( &m->sml_values[0], &timestamp );
1684 ber_dupbv( &m->sml_nvalues[0], &timestamp );
1685 m->sml_next = mod;
1686 mod = m;
1687 } else if ( ppb->pp.pwdMinDelay ) {
1688 int waittime = ppb->pp.pwdMinDelay << fc;
1689 time_t wait_end;
1690 char lockout_stamp_buf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
1691 struct berval lockout_stamp = BER_BVC(lockout_stamp_buf);
1692
1693 if ( waittime > ppb->pp.pwdMaxDelay ) {
1694 waittime = ppb->pp.pwdMaxDelay;
1695 }
1696 wait_end = now + waittime;
1697
1698 slap_timestamp( &wait_end, &lockout_stamp );
1699
1700 m = ch_calloc( sizeof(Modifications), 1 );
1701 m->sml_op = LDAP_MOD_REPLACE;
1702 m->sml_flags = 0;
1703 m->sml_type = ad_pwdAccountTmpLockoutEnd->ad_cname;
1704 m->sml_desc = ad_pwdAccountTmpLockoutEnd;
1705 m->sml_numvals = 1;
1706 m->sml_values = ch_calloc( sizeof(struct berval), 2 );
1707 m->sml_nvalues = ch_calloc( sizeof(struct berval), 2 );
1708 ber_dupbv( &m->sml_values[0], &lockout_stamp );
1709 ber_dupbv( &m->sml_nvalues[0], &lockout_stamp );
1710 m->sml_next = mod;
1711 mod = m;
1712 }
1713 } else if ( rs->sr_err == LDAP_SUCCESS ) {
1714 if ((a = attr_find( e->e_attrs, ad_pwdChangedTime )) != NULL)
1715 pwtime = parse_time( a->a_nvals[0].bv_val );
1716
1717 /* delete all pwdFailureTimes */
1718 if ( attr_find( e->e_attrs, ad_pwdFailureTime )) {
1719 m = ch_calloc( sizeof(Modifications), 1 );
1720 m->sml_op = LDAP_MOD_DELETE;
1721 m->sml_flags = 0;
1722 m->sml_type = ad_pwdFailureTime->ad_cname;
1723 m->sml_desc = ad_pwdFailureTime;
1724 m->sml_next = mod;
1725 mod = m;
1726 }
1727
1728 /*
1729 * check to see if the password must be changed
1730 */
1731 if ( ppb->pp.pwdMustChange &&
1732 (a = attr_find( e->e_attrs, ad_pwdReset )) &&
1733 bvmatch( &a->a_nvals[0], &slap_true_bv ) )
1734 {
1735 /*
1736 * need to inject client controls here to give
1737 * more information. For the moment, we ensure
1738 * that we are disallowed from doing anything
1739 * other than change password.
1740 */
1741 if ( ppb->set_restrict ) {
1742 ber_dupbv( &pwcons[op->o_conn->c_conn_idx].dn,
1743 &op->o_conn->c_ndn );
1744 }
1745
1746 ppb->pErr = PP_changeAfterReset;
1747
1748 } else {
1749 /*
1750 * the password does not need to be changed, so
1751 * we now check whether the password has expired.
1752 *
1753 * We can skip this bit if passwords don't age in
1754 * the policy. Also, if there was no pwdChangedTime
1755 * attribute in the entry, the password never expires.
1756 */
1757 if (ppb->pp.pwdMaxAge == 0) goto grace;
1758
1759 if (pwtime != (time_t)-1) {
1760 /*
1761 * Check: was the last change time of
1762 * the password older than the maximum age
1763 * allowed. (Ignore case 2 from I-D, it's just silly.)
1764 */
1765 if (now - pwtime > ppb->pp.pwdMaxAge ) pwExpired = 1;
1766 }
1767 }
1768
1769 grace:
1770 if (!pwExpired) goto check_expiring_password;
1771
1772 if ( ppb->pp.pwdGraceExpiry &&
1773 now - pwtime > ppb->pp.pwdMaxAge + ppb->pp.pwdGraceExpiry ) {
1774 /* Grace logins have expired now */
1775 ngut = 0;
1776 } else if ((a = attr_find( e->e_attrs, ad_pwdGraceUseTime )) == NULL) {
1777 ngut = ppb->pp.pwdGraceAuthNLimit;
1778 } else {
1779 for(ngut=0; a->a_nvals[ngut].bv_val; ngut++);
1780 ngut = ppb->pp.pwdGraceAuthNLimit - ngut;
1781 }
1782
1783 /*
1784 * ngut is the number of remaining grace logins
1785 */
1786 Debug( LDAP_DEBUG_ANY,
1787 "ppolicy_bind: Entry %s has an expired password: %d grace logins\n",
1788 e->e_name.bv_val, ngut );
1789
1790 ngut--;
1791
1792 if (ngut < 0) {
1793 ppb->pErr = PP_passwordExpired;
1794 rs->sr_err = LDAP_INVALID_CREDENTIALS;
1795 goto done;
1796 }
1797
1798 /*
1799 * Add a grace user time to the entry
1800 */
1801 m = ch_calloc( sizeof(Modifications), 1 );
1802 m->sml_op = LDAP_MOD_ADD;
1803 m->sml_flags = 0;
1804 m->sml_type = ad_pwdGraceUseTime->ad_cname;
1805 m->sml_desc = ad_pwdGraceUseTime;
1806 m->sml_numvals = 1;
1807 m->sml_values = ch_calloc( sizeof(struct berval), 2 );
1808 m->sml_nvalues = ch_calloc( sizeof(struct berval), 2 );
1809 ber_dupbv( &m->sml_values[0], &timestamp_usec );
1810 ber_dupbv( &m->sml_nvalues[0], &timestamp_usec );
1811 m->sml_next = mod;
1812 mod = m;
1813
1814 check_expiring_password:
1815 /*
1816 * Now we need to check to see
1817 * if it is about to expire, and if so, should the user
1818 * be warned about it in the password policy control.
1819 *
1820 * If the password has expired, and we're in the grace period, then
1821 * we don't need to do this bit. Similarly, if we don't have password
1822 * aging, then there's no need to do this bit either.
1823 *
1824 * If pwdtime is -1 there is no password Change Time attribute on the
1825 * entry so we skip the expiry check.
1826 *
1827 */
1828 if ((ppb->pp.pwdMaxAge < 1) || (pwExpired) || (ppb->pp.pwdExpireWarning < 1) ||
1829 (pwtime == -1))
1830 goto done;
1831
1832 age = (int)(now - pwtime);
1833
1834 /*
1835 * We know that there is a password Change Time attribute - if
1836 * there wasn't, then the pwdExpired value would be true, unless
1837 * there is no password aging - and if there is no password aging,
1838 * then this section isn't called anyway - you can't have an
1839 * expiring password if there's no limit to expire.
1840 */
1841 if (ppb->pp.pwdMaxAge - age < ppb->pp.pwdExpireWarning ) {
1842 /*
1843 * Set the warning value.
1844 */
1845 warn = ppb->pp.pwdMaxAge - age; /* seconds left until expiry */
1846 if (warn < 0) warn = 0; /* something weird here - why is pwExpired not set? */
1847
1848 Debug( LDAP_DEBUG_TRACE,
1849 "ppolicy_bind: Setting warning for password expiry for %s = %d seconds\n",
1850 op->o_req_dn.bv_val, warn );
1851 }
1852 }
1853
1854 done:
1855 op->o_bd = ppb->be;
1856 be_entry_release_r( op, e );
1857 op->o_bd = be;
1858
1859 locked:
1860 if ( mod && !pi->disable_write ) {
1861 Operation op2 = *op;
1862 SlapReply r2 = { REP_RESULT };
1863 slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
1864 LDAPControl c, *ca[2];
1865
1866 op2.o_tag = LDAP_REQ_MODIFY;
1867 op2.o_callback = &cb;
1868 op2.orm_modlist = mod;
1869 op2.orm_no_opattrs = 0;
1870 op2.o_dn = op->o_bd->be_rootdn;
1871 op2.o_ndn = op->o_bd->be_rootndn;
1872
1873 /* If this server is a shadow and forward_updates is true,
1874 * use the frontend to perform this modify. That will trigger
1875 * the update referral, which can then be forwarded by the
1876 * chain overlay. Obviously the updateref and chain overlay
1877 * must be configured appropriately for this to be useful.
1878 */
1879 if ( SLAP_SHADOW( op->o_bd ) && pi->forward_updates ) {
1880 op2.o_bd = frontendDB;
1881
1882 /* Must use Relax control since these are no-user-mod */
1883 op2.o_relax = SLAP_CONTROL_CRITICAL;
1884 op2.o_ctrls = ca;
1885 ca[0] = &c;
1886 ca[1] = NULL;
1887 BER_BVZERO( &c.ldctl_value );
1888 c.ldctl_iscritical = 1;
1889 c.ldctl_oid = LDAP_CONTROL_RELAX;
1890 } else {
1891 /* If not forwarding, don't update opattrs and don't replicate */
1892 if ( SLAP_SINGLE_SHADOW( op->o_bd )) {
1893 op2.orm_no_opattrs = 1;
1894 op2.o_dont_replicate = 1;
1895 }
1896 op2.o_bd = ppb->be;
1897 }
1898 rc = op2.o_bd->be_modify( &op2, &r2 );
1899 if ( rc != LDAP_SUCCESS ) {
1900 Debug( LDAP_DEBUG_ANY, "%s ppolicy_bind_response: "
1901 "ppolicy state change failed with rc=%d text=%s\n",
1902 op->o_log_prefix, rc, r2.sr_text );
1903 }
1904 }
1905 if ( mod ) {
1906 slap_mods_free( mod, 1 );
1907 }
1908
1909 if ( ppb->send_ctrl ) {
1910
1911 /* Do we really want to tell that the account is locked? */
1912 if ( ppb->pErr == PP_accountLocked && !pi->use_lockout ) {
1913 ppb->pErr = PP_noError;
1914 }
1915 ctrl = create_passcontrol( op, warn, ngut, ppb->pErr );
1916 } else if ( pi->send_netscape_controls ) {
1917 if ( ppb->pErr != PP_noError || pwExpired ) {
1918 ctrl = create_passexpiry( op, 1, 0 );
1919 } else if ( warn > 0 ) {
1920 ctrl = create_passexpiry( op, 0, warn );
1921 }
1922 }
1923 if ( ctrl ) {
1924 ppb->oldctrls = add_passcontrol( op, rs, ctrl );
1925 op->o_callback->sc_cleanup = ppolicy_ctrls_cleanup;
1926 }
1927 ldap_pvt_thread_mutex_unlock( &pi->pwdFailureTime_mutex );
1928 return SLAP_CB_CONTINUE;
1929 }
1930
1931 static int
1932 ppolicy_bind( Operation *op, SlapReply *rs )
1933 {
1934 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1935
1936 /* Reset lockout status on all Bind requests */
1937 if ( !BER_BVISEMPTY( &pwcons[op->o_conn->c_conn_idx].dn )) {
1938 ch_free( pwcons[op->o_conn->c_conn_idx].dn.bv_val );
1939 BER_BVZERO( &pwcons[op->o_conn->c_conn_idx].dn );
1940 }
1941
1942 /* Root bypasses policy */
1943 if ( !be_isroot_dn( op->o_bd, &op->o_req_ndn )) {
1944 Entry *e;
1945 int rc;
1946 ppbind *ppb;
1947 slap_callback *cb;
1948
1949 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1950 rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e );
1951
1952 if ( rc != LDAP_SUCCESS ) {
1953 return SLAP_CB_CONTINUE;
1954 }
1955
1956 cb = op->o_tmpcalloc( sizeof(ppbind)+sizeof(slap_callback),
1957 1, op->o_tmpmemctx );
1958 ppb = (ppbind *)(cb+1);
1959 ppb->pi = on->on_bi.bi_private;
1960 ppb->be = op->o_bd->bd_self;
1961 ppb->pErr = PP_noError;
1962 ppb->set_restrict = 1;
1963
1964 /* Setup a callback so we can munge the result */
1965
1966 cb->sc_response = ppolicy_bind_response;
1967 cb->sc_private = ppb;
1968 overlay_callback_after_backover( op, cb, 1 );
1969
1970 /* Did we receive a password policy request control? */
1971 if ( op->o_ctrlflag[ppolicy_cid] ) {
1972 ppb->send_ctrl = 1;
1973 }
1974
1975 op->o_bd->bd_info = (BackendInfo *)on;
1976
1977 if ( ppolicy_get( op, e, &ppb->pp ) == LDAP_SUCCESS ) {
1978 rc = account_locked( op, e, &ppb->pp, &ppb->mod );
1979 }
1980
1981 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1982 be_entry_release_r( op, e );
1983
1984 if ( rc ) {
1985 ppb->pErr = PP_accountLocked;
1986 send_ldap_error( op, rs, LDAP_INVALID_CREDENTIALS, NULL );
1987 return rs->sr_err;
1988 }
1989
1990 }
1991
1992 return SLAP_CB_CONTINUE;
1993 }
1994
1995 /* Reset the restricted info for the next session on this connection */
1996 static int
1997 ppolicy_connection_destroy( BackendDB *bd, Connection *conn )
1998 {
1999 if ( pwcons && !BER_BVISEMPTY( &pwcons[conn->c_conn_idx].dn )) {
2000 ch_free( pwcons[conn->c_conn_idx].dn.bv_val );
2001 BER_BVZERO( &pwcons[conn->c_conn_idx].dn );
2002 }
2003 return SLAP_CB_CONTINUE;
2004 }
2005
2006 /* Check if this connection is restricted */
2007 static int
2008 ppolicy_restrict(
2009 Operation *op,
2010 SlapReply *rs )
2011 {
2012 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
2013 int send_ctrl = 0;
2014
2015 /* Did we receive a password policy request control? */
2016 if ( op->o_ctrlflag[ppolicy_cid] ) {
2017 send_ctrl = 1;
2018 }
2019
2020 if ( op->o_conn && !BER_BVISEMPTY( &pwcons[op->o_conn->c_conn_idx].dn )) {
2021 LDAPControl **oldctrls;
2022 /* if the current authcDN doesn't match the one we recorded,
2023 * then an intervening Bind has succeeded and the restriction
2024 * no longer applies. (ITS#4516)
2025 */
2026 if ( !dn_match( &op->o_conn->c_ndn,
2027 &pwcons[op->o_conn->c_conn_idx].dn )) {
2028 ch_free( pwcons[op->o_conn->c_conn_idx].dn.bv_val );
2029 BER_BVZERO( &pwcons[op->o_conn->c_conn_idx].dn );
2030 return SLAP_CB_CONTINUE;
2031 }
2032
2033 Debug( LDAP_DEBUG_TRACE,
2034 "connection restricted to password changing only\n" );
2035 if ( send_ctrl ) {
2036 LDAPControl *ctrl = NULL;
2037 ctrl = create_passcontrol( op, -1, -1, PP_changeAfterReset );
2038 oldctrls = add_passcontrol( op, rs, ctrl );
2039 }
2040 op->o_bd->bd_info = (BackendInfo *)on->on_info;
2041 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
2042 "Operations are restricted to bind/unbind/abandon/StartTLS/modify password" );
2043 if ( send_ctrl ) {
2044 ctrls_cleanup( op, rs, oldctrls );
2045 }
2046 return rs->sr_err;
2047 }
2048
2049 return SLAP_CB_CONTINUE;
2050 }
2051
2052 static int
2053 ppolicy_account_usability_entry_cb( Operation *op, SlapReply *rs )
2054 {
2055 slap_overinst *on = op->o_callback->sc_private;
2056 BackendInfo *bi = op->o_bd->bd_info;
2057 LDAPControl *ctrl = NULL;
2058 PassPolicy pp;
2059 Attribute *a;
2060 Entry *e = NULL;
2061 time_t pwtime = 0, seconds_until_expiry = -1, now = op->o_time;
2062 int isExpired = 0, grace = -1;
2063
2064 if ( rs->sr_type != REP_SEARCH ) {
2065 return SLAP_CB_CONTINUE;
2066 }
2067
2068 if ( be_entry_get_rw( op, &rs->sr_entry->e_nname, NULL, NULL, 0, &e ) != LDAP_SUCCESS ) {
2069 goto done;
2070 }
2071
2072 op->o_bd->bd_info = (BackendInfo *)on;
2073
2074 if ( ppolicy_get( op, e, &pp ) != LDAP_SUCCESS ) {
2075 /* TODO: If there is no policy, should we check if */
2076 goto done;
2077 }
2078
2079 if ( !access_allowed( op, e, pp.ad, NULL, ACL_COMPARE, NULL ) ) {
2080 goto done;
2081 }
2082
2083 if ( attr_find( e->e_attrs, pp.ad ) == NULL ) {
2084 goto done;
2085 }
2086
2087 if ((a = attr_find( e->e_attrs, ad_pwdChangedTime )) != NULL) {
2088 pwtime = parse_time( a->a_nvals[0].bv_val );
2089 }
2090
2091 if ( pp.pwdMaxAge && pwtime ) {
2092 seconds_until_expiry = pwtime + pp.pwdMaxAge - now;
2093 if ( seconds_until_expiry <= 0 ) isExpired = 1;
2094 if ( pp.pwdGraceAuthNLimit ) {
2095 if ( !pp.pwdGraceExpiry || seconds_until_expiry + pp.pwdGraceExpiry > 0 ) {
2096 grace = pp.pwdGraceAuthNLimit;
2097 if ( attr_find( e->e_attrs, ad_pwdGraceUseTime ) ) {
2098 grace -= a->a_numvals;
2099 }
2100 }
2101 }
2102 }
2103 if ( !isExpired && pp.pwdMaxIdle && (a = attr_find( e->e_attrs, ad_pwdLastSuccess )) ) {
2104 time_t lastbindtime = pwtime;
2105
2106 if ( (a = attr_find( e->e_attrs, ad_pwdLastSuccess )) != NULL ) {
2107 lastbindtime = parse_time( a->a_nvals[0].bv_val );
2108 }
2109
2110 if ( lastbindtime ) {
2111 int remaining_idle = lastbindtime + pp.pwdMaxIdle - now;
2112 if ( remaining_idle <= 0 ) {
2113 isExpired = 1;
2114 } else if ( seconds_until_expiry == -1 || remaining_idle < seconds_until_expiry ) {
2115 seconds_until_expiry = remaining_idle;
2116 }
2117 }
2118 }
2119
2120 if ( isExpired || account_locked( op, e, &pp, NULL ) ) {
2121 LDAPAccountUsabilityMoreInfo more_info = { 0, 0, 0, -1, -1 };
2122 time_t then, lockoutEnd = 0;
2123
2124 if ( isExpired ) more_info.remaining_grace = grace;
2125
2126 if ( (a = attr_find( e->e_attrs, ad_pwdAccountLockedTime )) != NULL ) {
2127 then = parse_time( a->a_vals[0].bv_val );
2128 if ( then == 0 )
2129 lockoutEnd = -1;
2130
2131 /* Still in the future? not yet in effect */
2132 if ( now < then )
2133 then = 0;
2134
2135 if ( !pp.pwdLockoutDuration )
2136 lockoutEnd = -1;
2137
2138 if ( now < then + pp.pwdLockoutDuration )
2139 lockoutEnd = then + pp.pwdLockoutDuration;
2140 }
2141
2142 if ( (a = attr_find( e->e_attrs, ad_pwdAccountTmpLockoutEnd )) != NULL ) {
2143 then = parse_time( a->a_vals[0].bv_val );
2144 if ( lockoutEnd != -1 && then > lockoutEnd )
2145 lockoutEnd = then;
2146 }
2147
2148 if ( lockoutEnd > now ) {
2149 more_info.inactive = 1;
2150 more_info.seconds_before_unlock = lockoutEnd - now;
2151 }
2152
2153 if ( pp.pwdMustChange &&
2154 (a = attr_find( e->e_attrs, ad_pwdReset )) &&
2155 bvmatch( &a->a_nvals[0], &slap_true_bv ) )
2156 {
2157 more_info.reset = 1;
2158 }
2159
2160 add_account_control( op, rs, 0, -1, &more_info );
2161 } else {
2162 add_account_control( op, rs, 1, seconds_until_expiry, NULL );
2163 }
2164
2165 done:
2166 op->o_bd->bd_info = bi;
2167 if ( e ) {
2168 be_entry_release_r( op, e );
2169 }
2170 return SLAP_CB_CONTINUE;
2171 }
2172
2173 static int
2174 ppolicy_search(
2175 Operation *op,
2176 SlapReply *rs )
2177 {
2178 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
2179 int rc = ppolicy_restrict( op, rs );
2180
2181 if ( rc != SLAP_CB_CONTINUE ) {
2182 return rc;
2183 }
2184
2185 if ( op->o_ctrlflag[account_usability_cid] ) {
2186 slap_callback *cb;
2187
2188 cb = op->o_tmpcalloc( sizeof(slap_callback), 1, op->o_tmpmemctx );
2189
2190 cb->sc_response = ppolicy_account_usability_entry_cb;
2191 cb->sc_private = on;
2192 overlay_callback_after_backover( op, cb, 1 );
2193 }
2194
2195 return SLAP_CB_CONTINUE;
2196 }
2197
2198 static int
2199 ppolicy_compare_response(
2200 Operation *op,
2201 SlapReply *rs )
2202 {
2203 /* map compare responses to bind responses */
2204 if ( rs->sr_err == LDAP_COMPARE_TRUE )
2205 rs->sr_err = LDAP_SUCCESS;
2206 else if ( rs->sr_err == LDAP_COMPARE_FALSE )
2207 rs->sr_err = LDAP_INVALID_CREDENTIALS;
2208
2209 ppolicy_bind_response( op, rs );
2210
2211 /* map back to compare */
2212 if ( rs->sr_err == LDAP_SUCCESS )
2213 rs->sr_err = LDAP_COMPARE_TRUE;
2214 else if ( rs->sr_err == LDAP_INVALID_CREDENTIALS )
2215 rs->sr_err = LDAP_COMPARE_FALSE;
2216
2217 return SLAP_CB_CONTINUE;
2218 }
2219
2220 static int
2221 ppolicy_compare(
2222 Operation *op,
2223 SlapReply *rs )
2224 {
2225 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
2226
2227 if ( ppolicy_restrict( op, rs ) != SLAP_CB_CONTINUE )
2228 return rs->sr_err;
2229
2230 /* Did we receive a password policy request control?
2231 * Are we testing the userPassword?
2232 */
2233 if ( op->o_ctrlflag[ppolicy_cid] &&
2234 op->orc_ava->aa_desc == slap_schema.si_ad_userPassword ) {
2235 Entry *e;
2236 int rc;
2237 ppbind *ppb;
2238 slap_callback *cb;
2239
2240 op->o_bd->bd_info = (BackendInfo *)on->on_info;
2241 rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e );
2242
2243 if ( rc != LDAP_SUCCESS ) {
2244 return SLAP_CB_CONTINUE;
2245 }
2246
2247 cb = op->o_tmpcalloc( sizeof(ppbind)+sizeof(slap_callback),
2248 1, op->o_tmpmemctx );
2249 ppb = (ppbind *)(cb+1);
2250 ppb->pi = on->on_bi.bi_private;
2251 ppb->be = op->o_bd->bd_self;
2252 ppb->pErr = PP_noError;
2253 ppb->send_ctrl = 1;
2254 /* failures here don't lockout the connection */
2255 ppb->set_restrict = 0;
2256
2257 /* Setup a callback so we can munge the result */
2258
2259 cb->sc_response = ppolicy_compare_response;
2260 cb->sc_private = ppb;
2261 overlay_callback_after_backover( op, cb, 1 );
2262
2263 op->o_bd->bd_info = (BackendInfo *)on;
2264
2265 if ( ppolicy_get( op, e, &ppb->pp ) == LDAP_SUCCESS ) {
2266 rc = account_locked( op, e, &ppb->pp, &ppb->mod );
2267 }
2268
2269 op->o_bd->bd_info = (BackendInfo *)on->on_info;
2270 be_entry_release_r( op, e );
2271
2272 if ( rc ) {
2273 ppb->pErr = PP_accountLocked;
2274 send_ldap_error( op, rs, LDAP_COMPARE_FALSE, NULL );
2275 return rs->sr_err;
2276 }
2277 }
2278 return SLAP_CB_CONTINUE;
2279 }
2280
2281 static int
2282 ppolicy_add(
2283 Operation *op,
2284 SlapReply *rs )
2285 {
2286 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
2287 pp_info *pi = on->on_bi.bi_private;
2288 PassPolicy pp;
2289 Attribute *pa;
2290 const char *txt;
2291 int is_pwdadmin = 0;
2292
2293 if ( ppolicy_restrict( op, rs ) != SLAP_CB_CONTINUE )
2294 return rs->sr_err;
2295
2296 /* If this is a replica, assume the provider checked everything */
2297 if ( be_shadow_update( op ) )
2298 return SLAP_CB_CONTINUE;
2299
2300 ppolicy_get( op, op->ora_e, &pp );
2301
2302 if ( access_allowed( op, op->ora_e, pp.ad, NULL, ACL_MANAGE, NULL ) ) {
2303 is_pwdadmin = 1;
2304 }
2305
2306 /* Check for password in entry */
2307 if ( (pa = attr_find( op->oq_add.rs_e->e_attrs, pp.ad )) ) {
2308 assert( pa->a_vals != NULL );
2309 assert( !BER_BVISNULL( &pa->a_vals[ 0 ] ) );
2310
2311 if ( !BER_BVISNULL( &pa->a_vals[ 1 ] ) ) {
2312 send_ldap_error( op, rs, LDAP_CONSTRAINT_VIOLATION, "Password policy only allows one password value" );
2313 return rs->sr_err;
2314 }
2315
2316 /*
2317 * new entry contains a password - if we're not the password admin
2318 * then we need to check that the password fits in with the
2319 * security policy for the new entry.
2320 */
2321
2322 if ( pp.pwdCheckQuality > 0 && !is_pwdadmin ) {
2323 struct berval *bv = &(pa->a_vals[0]);
2324 int rc, send_ctrl = 0;
2325 LDAPPasswordPolicyError pErr = PP_noError;
2326 char errbuf[ERRBUFSIZ];
2327 struct berval errmsg = BER_BVC( errbuf );
2328
2329 /* Did we receive a password policy request control? */
2330 if ( op->o_ctrlflag[ppolicy_cid] ) {
2331 send_ctrl = 1;
2332 }
2333 rc = check_password_quality( bv, pi, &pp, &pErr, op->ora_e, &errmsg );
2334 if (rc != LDAP_SUCCESS) {
2335 char *txt = errmsg.bv_val;
2336 LDAPControl **oldctrls = NULL;
2337 op->o_bd->bd_info = (BackendInfo *)on->on_info;
2338 if ( send_ctrl ) {
2339 LDAPControl *ctrl = NULL;
2340 ctrl = create_passcontrol( op, -1, -1, pErr );
2341 oldctrls = add_passcontrol( op, rs, ctrl );
2342 }
2343 send_ldap_error( op, rs, rc, txt && txt[0] ? txt : "Password fails quality checking policy" );
2344 if ( txt != errbuf ) {
2345 free( txt );
2346 }
2347 if ( send_ctrl ) {
2348 ctrls_cleanup( op, rs, oldctrls );
2349 }
2350 return rs->sr_err;
2351 }
2352 }
2353 /*
2354 * A controversial bit. We hash cleartext
2355 * passwords provided via add and modify operations
2356 * You're not really supposed to do this, since
2357 * the X.500 model says "store attributes" as they
2358 * get provided. By default, this is what we do
2359 *
2360 * But if the hash_passwords flag is set, we hash
2361 * any cleartext password attribute values via the
2362 * default password hashing scheme.
2363 */
2364 if ((pi->hash_passwords) &&
2365 (password_scheme( &(pa->a_vals[0]), NULL ) != LDAP_SUCCESS)) {
2366 struct berval hpw;
2367
2368 slap_passwd_hash( &(pa->a_vals[0]), &hpw, &txt );
2369 if (hpw.bv_val == NULL) {
2370 /*
2371 * hashing didn't work. Emit an error.
2372 */
2373 rs->sr_err = LDAP_OTHER;
2374 rs->sr_text = txt;
2375 send_ldap_error( op, rs, LDAP_OTHER, "Password hashing failed" );
2376 return rs->sr_err;
2377 }
2378
2379 memset( pa->a_vals[0].bv_val, 0, pa->a_vals[0].bv_len);
2380 ber_memfree( pa->a_vals[0].bv_val );
2381 pa->a_vals[0].bv_val = hpw.bv_val;
2382 pa->a_vals[0].bv_len = hpw.bv_len;
2383 }
2384
2385 /* If password aging is in effect, set the pwdChangedTime */
2386 if ( ( pp.pwdMaxAge || pp.pwdMinAge ) &&
2387 !attr_find( op->ora_e->e_attrs, ad_pwdChangedTime ) ) {
2388 struct berval timestamp;
2389 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
2390 time_t now = slap_get_time();
2391
2392 timestamp.bv_val = timebuf;
2393 timestamp.bv_len = sizeof(timebuf);
2394 slap_timestamp( &now, &timestamp );
2395
2396 attr_merge_one( op->ora_e, ad_pwdChangedTime, &timestamp, &timestamp );
2397 }
2398 }
2399 return SLAP_CB_CONTINUE;
2400 }
2401
2402 static int
2403 ppolicy_mod_cb( Operation *op, SlapReply *rs )
2404 {
2405 slap_callback *sc = op->o_callback;
2406 op->o_callback = sc->sc_next;
2407 if ( rs->sr_err == LDAP_SUCCESS ) {
2408 ch_free( pwcons[op->o_conn->c_conn_idx].dn.bv_val );
2409 BER_BVZERO( &pwcons[op->o_conn->c_conn_idx].dn );
2410 }
2411 op->o_tmpfree( sc, op->o_tmpmemctx );
2412 return SLAP_CB_CONTINUE;
2413 }
2414
2415 static int
2416 ppolicy_text_cleanup( Operation *op, SlapReply *rs )
2417 {
2418 slap_callback *sc = op->o_callback;
2419
2420 if ( rs->sr_text == sc->sc_private ) {
2421 rs->sr_text = NULL;
2422 }
2423 free( sc->sc_private );
2424
2425 op->o_callback = sc->sc_next;
2426 op->o_tmpfree( sc, op->o_tmpmemctx );
2427 return SLAP_CB_CONTINUE;
2428 }
2429
2430 static int
2431 ppolicy_modify( Operation *op, SlapReply *rs )
2432 {
2433 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
2434 pp_info *pi = on->on_bi.bi_private;
2435 int i, rc, mod_pw_only, pwmod = 0, pwmop = -1, deladd,
2436 hsize = 0, hskip;
2437 PassPolicy pp;
2438 Modifications *mods = NULL, *modtail = NULL,
2439 *ml, *delmod, *addmod;
2440 Attribute *pa, *ha, at;
2441 const char *txt;
2442 char errbuf[ERRBUFSIZ];
2443 pw_hist *tl = NULL, *p;
2444 int zapReset, send_ctrl = 0, free_txt = 0;
2445 Entry *e;
2446 struct berval newpw = BER_BVNULL, oldpw = BER_BVNULL,
2447 *bv, cr[2];
2448 LDAPPasswordPolicyError pErr = PP_noError;
2449 LDAPControl *ctrl = NULL;
2450 LDAPControl **oldctrls = NULL;
2451 int is_pwdexop = 0, is_pwdadmin = 0;
2452 int got_del_grace = 0, got_del_lock = 0, got_pw = 0, got_del_fail = 0,
2453 got_del_success = 0;
2454 int got_changed = 0, got_history = 0;
2455 int have_policy = 0;
2456
2457 op->o_bd->bd_info = (BackendInfo *)on->on_info;
2458 rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e );
2459 op->o_bd->bd_info = (BackendInfo *)on;
2460
2461 if ( rc != LDAP_SUCCESS ) return SLAP_CB_CONTINUE;
2462 if ( pi->disable_write ) return SLAP_CB_CONTINUE;
2463
2464 /* If this is a replica, we may need to tweak some of the
2465 * provider's modifications. Otherwise, just pass it through.
2466 */
2467 if ( be_shadow_update( op ) ) {
2468 Modifications **prev;
2469 Attribute *a_grace, *a_lock, *a_fail, *a_success;
2470
2471 a_grace = attr_find( e->e_attrs, ad_pwdGraceUseTime );
2472 a_lock = attr_find( e->e_attrs, ad_pwdAccountLockedTime );
2473 a_fail = attr_find( e->e_attrs, ad_pwdFailureTime );
2474 a_success = attr_find( e->e_attrs, ad_pwdLastSuccess );
2475
2476 for( prev = &op->orm_modlist, ml = *prev; ml; ml = *prev ) {
2477
2478 if ( ml->sml_desc == slap_schema.si_ad_userPassword )
2479 got_pw = 1;
2480
2481 /* If we're deleting an attr that didn't exist,
2482 * drop this delete op
2483 */
2484 if ( ml->sml_op == LDAP_MOD_DELETE ||
2485 ml->sml_op == SLAP_MOD_SOFTDEL ) {
2486 int drop = 0;
2487
2488 if ( ml->sml_desc == ad_pwdGraceUseTime ) {
2489 if ( !a_grace || got_del_grace ) {
2490 drop = ml->sml_op == LDAP_MOD_DELETE;
2491 } else {
2492 got_del_grace = 1;
2493 }
2494 } else
2495 if ( ml->sml_desc == ad_pwdAccountLockedTime ) {
2496 if ( !a_lock || got_del_lock ) {
2497 drop = ml->sml_op == LDAP_MOD_DELETE;
2498 } else {
2499 got_del_lock = 1;
2500 }
2501 } else
2502 if ( ml->sml_desc == ad_pwdFailureTime ) {
2503 if ( !a_fail || got_del_fail ) {
2504 drop = ml->sml_op == LDAP_MOD_DELETE;
2505 } else {
2506 got_del_fail = 1;
2507 }
2508 }
2509 if ( ml->sml_desc == ad_pwdLastSuccess ) {
2510 if ( !a_success || got_del_success ) {
2511 drop = ml->sml_op == LDAP_MOD_DELETE;
2512 } else {
2513 got_del_success = 1;
2514 }
2515 }
2516 if ( drop ) {
2517 *prev = ml->sml_next;
2518 ml->sml_next = NULL;
2519 slap_mods_free( ml, 1 );
2520 continue;
2521 }
2522 }
2523 prev = &ml->sml_next;
2524 }
2525
2526 /* If we're resetting the password, make sure grace, accountlock,
2527 * success, and failure also get removed.
2528 */
2529 if ( got_pw ) {
2530 if ( a_grace && !got_del_grace ) {
2531 ml = (Modifications *) ch_malloc( sizeof( Modifications ) );
2532 ml->sml_op = LDAP_MOD_DELETE;
2533 ml->sml_flags = SLAP_MOD_INTERNAL;
2534 ml->sml_type.bv_val = NULL;
2535 ml->sml_desc = ad_pwdGraceUseTime;
2536 ml->sml_numvals = 0;
2537 ml->sml_values = NULL;
2538 ml->sml_nvalues = NULL;
2539 ml->sml_next = NULL;
2540 *prev = ml;
2541 prev = &ml->sml_next;
2542 }
2543 if ( a_lock && !got_del_lock ) {
2544 ml = (Modifications *) ch_malloc( sizeof( Modifications ) );
2545 ml->sml_op = LDAP_MOD_DELETE;
2546 ml->sml_flags = SLAP_MOD_INTERNAL;
2547 ml->sml_type.bv_val = NULL;
2548 ml->sml_desc = ad_pwdAccountLockedTime;
2549 ml->sml_numvals = 0;
2550 ml->sml_values = NULL;
2551 ml->sml_nvalues = NULL;
2552 ml->sml_next = NULL;
2553 *prev = ml;
2554 }
2555 if ( a_fail && !got_del_fail ) {
2556 ml = (Modifications *) ch_malloc( sizeof( Modifications ) );
2557 ml->sml_op = LDAP_MOD_DELETE;
2558 ml->sml_flags = SLAP_MOD_INTERNAL;
2559 ml->sml_type.bv_val = NULL;
2560 ml->sml_desc = ad_pwdFailureTime;
2561 ml->sml_numvals = 0;
2562 ml->sml_values = NULL;
2563 ml->sml_nvalues = NULL;
2564 ml->sml_next = NULL;
2565 *prev = ml;
2566 }
2567 if ( a_success && !got_del_success ) {
2568 ml = (Modifications *) ch_malloc( sizeof( Modifications ) );
2569 ml->sml_op = LDAP_MOD_DELETE;
2570 ml->sml_flags = SLAP_MOD_INTERNAL;
2571 ml->sml_type.bv_val = NULL;
2572 ml->sml_desc = ad_pwdLastSuccess;
2573 ml->sml_numvals = 0;
2574 ml->sml_values = NULL;
2575 ml->sml_nvalues = NULL;
2576 ml->sml_next = NULL;
2577 *prev = ml;
2578 }
2579 }
2580 op->o_bd->bd_info = (BackendInfo *)on->on_info;
2581 be_entry_release_r( op, e );
2582 return SLAP_CB_CONTINUE;
2583 }
2584
2585 /* Did we receive a password policy request control? */
2586 if ( op->o_ctrlflag[ppolicy_cid] ) {
2587 send_ctrl = 1;
2588 }
2589
2590 /* See if this is a pwdModify exop. If so, we can
2591 * access the plaintext passwords from that request.
2592 */
2593 {
2594 slap_callback *sc;
2595
2596 for ( sc = op->o_callback; sc; sc=sc->sc_next ) {
2597 if ( sc->sc_response == slap_null_cb &&
2598 sc->sc_private ) {
2599 req_pwdexop_s *qpw = sc->sc_private;
2600 newpw = qpw->rs_new;
2601 oldpw = qpw->rs_old;
2602 is_pwdexop = 1;
2603 break;
2604 }
2605 }
2606 }
2607
2608 /* ppolicy_hash_cleartext depends on pwmod being determined first */
2609 if ( ppolicy_get( op, e, &pp ) == LDAP_SUCCESS ) {
2610 have_policy = 1;
2611 }
2612
2613 if ( access_allowed( op, e, pp.ad, NULL, ACL_MANAGE, NULL ) ) {
2614 is_pwdadmin = 1;
2615 }
2616
2617 for ( ml = op->orm_modlist,
2618 pwmod = 0, mod_pw_only = 1,
2619 deladd = 0, delmod = NULL,
2620 addmod = NULL,
2621 zapReset = 1;
2622 ml != NULL; modtail = ml, ml = ml->sml_next )
2623 {
2624 if ( ml->sml_desc == pp.ad ) {
2625 pwmod = 1;
2626 pwmop = ml->sml_op;
2627 if ((deladd == 0) && (ml->sml_op == LDAP_MOD_DELETE) &&
2628 (ml->sml_values) && !BER_BVISNULL( &ml->sml_values[0] ))
2629 {
2630 deladd = 1;
2631 delmod = ml;
2632 }
2633
2634 if ((ml->sml_op == LDAP_MOD_ADD) ||
2635 (ml->sml_op == LDAP_MOD_REPLACE))
2636 {
2637 if ( ml->sml_values && !BER_BVISNULL( &ml->sml_values[0] )) {
2638 if ( deladd == 1 )
2639 deladd = 2;
2640
2641 /* FIXME: there's no easy way to ensure
2642 * that add does not cause multiple
2643 * userPassword values; one way (that
2644 * would be consistent with the single
2645 * password constraint) would be to turn
2646 * add into replace); another would be
2647 * to disallow add.
2648 *
2649 * Let's check at least that a single value
2650 * is being added
2651 */
2652 if ( addmod || !BER_BVISNULL( &ml->sml_values[ 1 ] ) ) {
2653 rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
2654 rs->sr_text = "Password policy only allows one password value";
2655 goto return_results;
2656 }
2657
2658 addmod = ml;
2659 } else {
2660 /* replace can have no values, add cannot */
2661 assert( ml->sml_op == LDAP_MOD_REPLACE );
2662 }
2663 }
2664
2665 } else if ( !(ml->sml_flags & SLAP_MOD_INTERNAL) && !is_at_operational( ml->sml_desc->ad_type ) ) {
2666 mod_pw_only = 0;
2667 /* modifying something other than password */
2668 }
2669
2670 /*
2671 * If there is a request to explicitly add a pwdReset
2672 * attribute, then we suppress the normal behaviour on
2673 * password change, which is to remove the pwdReset
2674 * attribute.
2675 *
2676 * This enables an administrator to assign a new password
2677 * and place a "must reset" flag on the entry, which will
2678 * stay until the user explicitly changes his/her password.
2679 */
2680 if (ml->sml_desc == ad_pwdReset ) {
2681 if ((ml->sml_op == LDAP_MOD_ADD) ||
2682 (ml->sml_op == LDAP_MOD_REPLACE))
2683 zapReset = 0;
2684 }
2685 if ( ml->sml_op == LDAP_MOD_DELETE ) {
2686 if ( ml->sml_desc == ad_pwdGraceUseTime ) {
2687 got_del_grace = 1;
2688 } else if ( ml->sml_desc == ad_pwdAccountLockedTime ) {
2689 got_del_lock = 1;
2690 } else if ( ml->sml_desc == ad_pwdFailureTime ) {
2691 got_del_fail = 1;
2692 } else if ( ml->sml_desc == ad_pwdLastSuccess ) {
2693 got_del_success = 1;
2694 }
2695 }
2696 if ( ml->sml_desc == ad_pwdChangedTime ) {
2697 got_changed = 1;
2698 } else if (ml->sml_desc == ad_pwdHistory ) {
2699 got_history = 1;
2700 }
2701 }
2702
2703 if (!BER_BVISEMPTY( &pwcons[op->o_conn->c_conn_idx].dn ) && !mod_pw_only ) {
2704 if ( dn_match( &op->o_conn->c_ndn,
2705 &pwcons[op->o_conn->c_conn_idx].dn )) {
2706 Debug( LDAP_DEBUG_TRACE,
2707 "connection restricted to password changing only\n" );
2708 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
2709 rs->sr_text = "Operations are restricted to bind/unbind/abandon/StartTLS/modify password";
2710 pErr = PP_changeAfterReset;
2711 goto return_results;
2712 } else {
2713 ch_free( pwcons[op->o_conn->c_conn_idx].dn.bv_val );
2714 BER_BVZERO( &pwcons[op->o_conn->c_conn_idx].dn );
2715 }
2716 }
2717
2718 /*
2719 * if we have a "safe password modify policy", then we need to check if we're doing
2720 * a delete (with the old password), followed by an add (with the new password).
2721 *
2722 * If we got just a delete with nothing else, just let it go. We also skip all the checks if
2723 * the root user is bound. Root can do anything, including avoid the policies.
2724 */
2725
2726 if (!have_policy || !pwmod) goto do_modify;
2727
2728 /*
2729 * Build the password history list in ascending time order
2730 * We need this, even if the user is root, in order to maintain
2731 * the pwdHistory operational attributes properly.
2732 */
2733 if (addmod && pp.pwdInHistory > 0 && (ha = attr_find( e->e_attrs, ad_pwdHistory ))) {
2734 struct berval oldpw;
2735 time_t oldtime;
2736
2737 for(i=0; ha->a_nvals[i].bv_val; i++) {
2738 rc = parse_pwdhistory( &(ha->a_nvals[i]), NULL,
2739 &oldtime, &oldpw );
2740
2741 if (rc != LDAP_SUCCESS) continue; /* invalid history entry */
2742
2743 if (oldpw.bv_val) {
2744 add_to_pwd_history( &tl, oldtime, &oldpw,
2745 &(ha->a_nvals[i]) );
2746 oldpw.bv_val = NULL;
2747 oldpw.bv_len = 0;
2748 }
2749 }
2750 for(p=tl; p; p=p->next, hsize++); /* count history size */
2751 }
2752
2753 if (is_pwdadmin) goto do_modify;
2754
2755 /* NOTE: according to draft-behera-ldap-password-policy
2756 * pwdAllowUserChange == FALSE must only prevent pwd changes
2757 * by the user the pwd belongs to (ITS#7021) */
2758 if (!pp.pwdAllowUserChange && dn_match(&op->o_req_ndn, &op->o_ndn)) {
2759 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
2760 rs->sr_text = "User alteration of password is not allowed";
2761 pErr = PP_passwordModNotAllowed;
2762 goto return_results;
2763 }
2764
2765 /* Just deleting? */
2766 if (!addmod) {
2767 /* skip everything else */
2768 pwmod = 0;
2769 goto do_modify;
2770 }
2771
2772 /* This is a pwdModify exop that provided the old pw.
2773 * We need to create a Delete mod for this old pw and
2774 * let the matching value get found later
2775 */
2776 if (pp.pwdSafeModify && oldpw.bv_val ) {
2777 ml = (Modifications *)ch_calloc( sizeof( Modifications ), 1 );
2778 ml->sml_op = LDAP_MOD_DELETE;
2779 ml->sml_flags = SLAP_MOD_INTERNAL;
2780 ml->sml_desc = pp.ad;
2781 ml->sml_type = pp.ad->ad_cname;
2782 ml->sml_numvals = 1;
2783 ml->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
2784 ber_dupbv( &ml->sml_values[0], &oldpw );
2785 BER_BVZERO( &ml->sml_values[1] );
2786 ml->sml_next = op->orm_modlist;
2787 op->orm_modlist = ml;
2788 delmod = ml;
2789 deladd = 2;
2790 }
2791
2792 if (pp.pwdSafeModify && deladd != 2) {
2793 Debug( LDAP_DEBUG_TRACE,
2794 "change password must use DELETE followed by ADD/REPLACE\n" );
2795 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
2796 rs->sr_text = "Must supply old password to be changed as well as new one";
2797 pErr = PP_mustSupplyOldPassword;
2798 goto return_results;
2799 }
2800
2801 /* Check age, but only if pwdReset is not TRUE */
2802 pa = attr_find( e->e_attrs, ad_pwdReset );
2803 if ((!pa || !bvmatch( &pa->a_nvals[0], &slap_true_bv )) &&
2804 pp.pwdMinAge > 0) {
2805 time_t pwtime = (time_t)-1, now;
2806 int age;
2807
2808 if ((pa = attr_find( e->e_attrs, ad_pwdChangedTime )) != NULL)
2809 pwtime = parse_time( pa->a_nvals[0].bv_val );
2810 now = slap_get_time();
2811 age = (int)(now - pwtime);
2812 if ((pwtime != (time_t)-1) && (age < pp.pwdMinAge)) {
2813 rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
2814 rs->sr_text = "Password is too young to change";
2815 pErr = PP_passwordTooYoung;
2816 goto return_results;
2817 }
2818 }
2819
2820 /* pa is used in password history check below, be sure it's set */
2821 if ((pa = attr_find( e->e_attrs, pp.ad )) != NULL && delmod) {
2822 /*
2823 * we have a password to check
2824 */
2825 bv = oldpw.bv_val ? &oldpw : delmod->sml_values;
2826 /* FIXME: no access checking? */
2827 rc = slap_passwd_check( op, NULL, pa, bv, &txt );
2828 if (rc != LDAP_SUCCESS) {
2829 Debug( LDAP_DEBUG_TRACE,
2830 "old password check failed: %s\n", txt );
2831
2832 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
2833 rs->sr_text = "Must supply correct old password to change to new one";
2834 pErr = PP_mustSupplyOldPassword;
2835 goto return_results;
2836
2837 } else {
2838 int i;
2839
2840 /*
2841 * replace the delete value with the (possibly hashed)
2842 * value which is currently in the password.
2843 */
2844 for ( i = 0; !BER_BVISNULL( &delmod->sml_values[i] ); i++ ) {
2845 free( delmod->sml_values[i].bv_val );
2846 BER_BVZERO( &delmod->sml_values[i] );
2847 }
2848 free( delmod->sml_values );
2849 delmod->sml_values = ch_calloc( sizeof(struct berval), 2 );
2850 BER_BVZERO( &delmod->sml_values[1] );
2851 ber_dupbv( &(delmod->sml_values[0]), &(pa->a_nvals[0]) );
2852 }
2853 }
2854
2855 bv = newpw.bv_val ? &newpw : &addmod->sml_values[0];
2856 if (pp.pwdCheckQuality > 0) {
2857 struct berval errmsg = BER_BVC( errbuf );
2858
2859 rc = check_password_quality( bv, pi, &pp, &pErr, e, &errmsg );
2860 if (rc != LDAP_SUCCESS) {
2861 rs->sr_err = rc;
2862 txt = errmsg.bv_val;
2863 if ( txt && txt[0] ) {
2864 rs->sr_text = txt;
2865 if ( txt != errbuf )
2866 free_txt = 1;
2867 } else {
2868 rs->sr_text = "Password fails quality checking policy";
2869 }
2870 goto return_results;
2871 }
2872 }
2873
2874 /* If pwdInHistory is zero, passwords may be reused */
2875 if (pa && pp.pwdInHistory > 0) {
2876 /*
2877 * Last check - the password history.
2878 */
2879 /* FIXME: no access checking? */
2880 if (slap_passwd_check( op, NULL, pa, bv, &txt ) == LDAP_SUCCESS) {
2881 /*
2882 * This is bad - it means that the user is attempting
2883 * to set the password to the same as the old one.
2884 */
2885 rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
2886 rs->sr_text = "Password is not being changed from existing value";
2887 pErr = PP_passwordInHistory;
2888 goto return_results;
2889 }
2890
2891 /* We need this when reduce pwdInHistory */
2892 hskip = hsize - pp.pwdInHistory;
2893
2894 /*
2895 * Iterate through the password history, and fail on any
2896 * password matches.
2897 */
2898 at = *pa;
2899 at.a_vals = cr;
2900 cr[1].bv_val = NULL;
2901 for(p=tl; p; p=p->next) {
2902 if(hskip > 0){
2903 hskip--;
2904 continue;
2905 }
2906 cr[0] = p->pw;
2907 /* FIXME: no access checking? */
2908 rc = slap_passwd_check( op, NULL, &at, bv, &txt );
2909
2910 if (rc != LDAP_SUCCESS) continue;
2911
2912 rs->sr_err = LDAP_CONSTRAINT_VIOLATION;
2913 rs->sr_text = "Password is in history of old passwords";
2914 pErr = PP_passwordInHistory;
2915 goto return_results;
2916 }
2917 }
2918
2919 do_modify:
2920 if (pwmod) {
2921 struct berval timestamp;
2922 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
2923 time_t now = slap_get_time();
2924
2925 /* If the conn is restricted, set a callback to clear it
2926 * if the pwmod succeeds
2927 */
2928 if (!BER_BVISEMPTY( &pwcons[op->o_conn->c_conn_idx].dn )) {
2929 slap_callback *sc = op->o_tmpcalloc( 1, sizeof( slap_callback ),
2930 op->o_tmpmemctx );
2931 sc->sc_next = op->o_callback;
2932 /* Must use sc_response to insure we reset on success, before
2933 * the client sees the response. Must use sc_cleanup to insure
2934 * that it gets cleaned up if sc_response is not called.
2935 */
2936 sc->sc_response = ppolicy_mod_cb;
2937 sc->sc_cleanup = ppolicy_mod_cb;
2938 op->o_callback = sc;
2939 }
2940
2941 /*
2942 * keep the necessary pwd.. operational attributes
2943 * up to date.
2944 */
2945
2946 if (!got_changed) {
2947 timestamp.bv_val = timebuf;
2948 timestamp.bv_len = sizeof(timebuf);
2949 slap_timestamp( &now, &timestamp );
2950
2951 mods = NULL;
2952 if (pwmop != LDAP_MOD_DELETE) {
2953 mods = (Modifications *) ch_calloc( sizeof( Modifications ), 1 );
2954 mods->sml_op = LDAP_MOD_REPLACE;
2955 mods->sml_numvals = 1;
2956 mods->sml_values = (BerVarray) ch_calloc( sizeof( struct berval ), 2 );
2957 mods->sml_nvalues = (BerVarray) ch_calloc( sizeof( struct berval ), 2 );
2958
2959 ber_dupbv( &mods->sml_values[0], &timestamp );
2960 ber_dupbv( &mods->sml_nvalues[0], &timestamp );
2961 } else if (attr_find(e->e_attrs, ad_pwdChangedTime )) {
2962 mods = (Modifications *) ch_calloc( sizeof( Modifications ), 1 );
2963 mods->sml_op = LDAP_MOD_DELETE;
2964 }
2965 if (mods) {
2966 mods->sml_desc = ad_pwdChangedTime;
2967 mods->sml_flags = SLAP_MOD_INTERNAL;
2968 mods->sml_next = NULL;
2969 modtail->sml_next = mods;
2970 modtail = mods;
2971 }
2972 }
2973
2974 if (!got_del_grace && attr_find(e->e_attrs, ad_pwdGraceUseTime )) {
2975 mods = (Modifications *) ch_calloc( sizeof( Modifications ), 1 );
2976 mods->sml_op = LDAP_MOD_DELETE;
2977 mods->sml_desc = ad_pwdGraceUseTime;
2978 mods->sml_flags = SLAP_MOD_INTERNAL;
2979 mods->sml_next = NULL;
2980 modtail->sml_next = mods;
2981 modtail = mods;
2982 }
2983
2984 if (!got_del_lock && attr_find(e->e_attrs, ad_pwdAccountLockedTime )) {
2985 mods = (Modifications *) ch_calloc( sizeof( Modifications ), 1 );
2986 mods->sml_op = LDAP_MOD_DELETE;
2987 mods->sml_desc = ad_pwdAccountLockedTime;
2988 mods->sml_flags = SLAP_MOD_INTERNAL;
2989 mods->sml_next = NULL;
2990 modtail->sml_next = mods;
2991 modtail = mods;
2992 }
2993
2994 if (!got_del_fail && attr_find(e->e_attrs, ad_pwdFailureTime )) {
2995 mods = (Modifications *) ch_calloc( sizeof( Modifications ), 1 );
2996 mods->sml_op = LDAP_MOD_DELETE;
2997 mods->sml_desc = ad_pwdFailureTime;
2998 mods->sml_flags = SLAP_MOD_INTERNAL;
2999 mods->sml_next = NULL;
3000 modtail->sml_next = mods;
3001 modtail = mods;
3002 }
3003
3004 if ( zapReset ) {
3005 /*
3006 * ITS#7084 Is this a modification by the password
3007 * administrator? Then force a reset if configured.
3008 * Otherwise clear it.
3009 */
3010 if ( pp.pwdMustChange && is_pwdadmin ) {
3011 mods = (Modifications *) ch_calloc( sizeof( Modifications ), 1 );
3012 mods->sml_op = LDAP_MOD_REPLACE;
3013 mods->sml_desc = ad_pwdReset;
3014 mods->sml_flags = SLAP_MOD_INTERNAL;
3015 mods->sml_numvals = 1;
3016 mods->sml_values = (BerVarray) ch_calloc( sizeof( struct berval ), 2 );
3017 mods->sml_nvalues = (BerVarray) ch_calloc( sizeof( struct berval ), 2 );
3018
3019 ber_dupbv( &mods->sml_values[0], (struct berval *)&slap_true_bv );
3020 ber_dupbv( &mods->sml_nvalues[0], (struct berval *)&slap_true_bv );
3021
3022 mods->sml_next = NULL;
3023 modtail->sml_next = mods;
3024 modtail = mods;
3025 } else if ( attr_find( e->e_attrs, ad_pwdReset ) ) {
3026 mods = (Modifications *) ch_calloc( sizeof( Modifications ), 1 );
3027 mods->sml_op = LDAP_MOD_DELETE;
3028 mods->sml_desc = ad_pwdReset;
3029 mods->sml_flags = SLAP_MOD_INTERNAL;
3030 mods->sml_next = NULL;
3031 modtail->sml_next = mods;
3032 modtail = mods;
3033 }
3034 }
3035
3036 /* TODO: do we remove pwdLastSuccess or set it to 'now'? */
3037 if (!got_del_success && attr_find(e->e_attrs, ad_pwdLastSuccess )){
3038 mods = (Modifications *) ch_calloc( sizeof( Modifications ), 1 );
3039 mods->sml_op = LDAP_MOD_DELETE;
3040 mods->sml_flags = SLAP_MOD_INTERNAL;
3041 mods->sml_desc = ad_pwdLastSuccess;
3042 mods->sml_next = NULL;
3043 modtail->sml_next = mods;
3044 modtail = mods;
3045 }
3046
3047 /* Delete all pwdInHistory attribute */
3048 if (!got_history && pp.pwdInHistory == 0 &&
3049 attr_find(e->e_attrs, ad_pwdHistory )){
3050 mods = (Modifications *) ch_calloc( sizeof( Modifications ), 1 );
3051 mods->sml_op = LDAP_MOD_DELETE;
3052 mods->sml_flags = SLAP_MOD_INTERNAL;
3053 mods->sml_desc = ad_pwdHistory;
3054 mods->sml_next = NULL;
3055 modtail->sml_next = mods;
3056 modtail = mods;
3057 }
3058
3059 if (!got_history && pp.pwdInHistory > 0){
3060 if (hsize >= pp.pwdInHistory) {
3061 /*
3062 * We use the >= operator, since we are going to add
3063 * the existing password attribute value into the
3064 * history - thus the cardinality of history values is
3065 * about to rise by one.
3066 *
3067 * If this would push it over the limit of history
3068 * values (remembering - the password policy could have
3069 * changed since the password was last altered), we must
3070 * delete at least 1 value from the pwdHistory list.
3071 *
3072 * In fact, we delete '(#pwdHistory attrs - max pwd
3073 * history length) + 1' values, starting with the oldest.
3074 * This is easily evaluated, since the linked list is
3075 * created in ascending time order.
3076 */
3077 mods = (Modifications *) ch_calloc( sizeof( Modifications ), 1 );
3078 mods->sml_op = LDAP_MOD_DELETE;
3079 mods->sml_flags = SLAP_MOD_INTERNAL;
3080 mods->sml_desc = ad_pwdHistory;
3081 mods->sml_numvals = hsize - pp.pwdInHistory + 1;
3082 mods->sml_values = ch_calloc( sizeof( struct berval ),
3083 hsize - pp.pwdInHistory + 2 );
3084 BER_BVZERO( &mods->sml_values[ hsize - pp.pwdInHistory + 1 ] );
3085 for(i=0,p=tl; i < (hsize - pp.pwdInHistory + 1); i++, p=p->next) {
3086 BER_BVZERO( &mods->sml_values[i] );
3087 ber_dupbv( &(mods->sml_values[i]), &p->bv );
3088 }
3089 mods->sml_next = NULL;
3090 modtail->sml_next = mods;
3091 modtail = mods;
3092 }
3093 free_pwd_history_list( &tl );
3094
3095 /*
3096 * Now add the existing password into the history list.
3097 * This will be executed even if the operation is to delete
3098 * the password entirely.
3099 *
3100 * This isn't in the spec explicitly, but it seems to make
3101 * sense that the password history list is the list of all
3102 * previous passwords - even if they were deleted. Thus, if
3103 * someone tries to add a historical password at some future
3104 * point, it will fail.
3105 */
3106 if ((pa = attr_find( e->e_attrs, pp.ad )) != NULL) {
3107 mods = (Modifications *) ch_malloc( sizeof( Modifications ) );
3108 mods->sml_op = LDAP_MOD_ADD;
3109 mods->sml_flags = SLAP_MOD_INTERNAL;
3110 mods->sml_type.bv_val = NULL;
3111 mods->sml_desc = ad_pwdHistory;
3112 mods->sml_nvalues = NULL;
3113 mods->sml_numvals = 1;
3114 mods->sml_values = ch_calloc( sizeof( struct berval ), 2 );
3115 mods->sml_values[ 1 ].bv_val = NULL;
3116 mods->sml_values[ 1 ].bv_len = 0;
3117 make_pwd_history_value( timebuf, &mods->sml_values[0], pa );
3118 mods->sml_next = NULL;
3119 modtail->sml_next = mods;
3120 modtail = mods;
3121
3122 } else {
3123 Debug( LDAP_DEBUG_TRACE,
3124 "ppolicy_modify: password attr lookup failed\n" );
3125 }
3126 }
3127
3128 /*
3129 * Controversial bit here. If the new password isn't hashed
3130 * (ie, is cleartext), we probably should hash it according
3131 * to the default hash. The reason for this is that we want
3132 * to use the policy if possible, but if we hash the password
3133 * before, then we're going to run into trouble when it
3134 * comes time to check the password.
3135 *
3136 * Now, the right thing to do is to use the extended password
3137 * modify operation, but not all software can do this,
3138 * therefore it makes sense to hash the new password, now
3139 * we know it passes the policy requirements.
3140 *
3141 * Of course, if the password is already hashed, then we
3142 * leave it alone.
3143 */
3144
3145 if ((pi->hash_passwords) && (addmod) && !newpw.bv_val &&
3146 (password_scheme( &(addmod->sml_values[0]), NULL ) != LDAP_SUCCESS))
3147 {
3148 struct berval hpw, bv;
3149
3150 slap_passwd_hash( &(addmod->sml_values[0]), &hpw, &txt );
3151 if (hpw.bv_val == NULL) {
3152 /*
3153 * hashing didn't work. Emit an error.
3154 */
3155 rs->sr_err = LDAP_OTHER;
3156 rs->sr_text = txt;
3157 goto return_results;
3158 }
3159 bv = addmod->sml_values[0];
3160 /* clear and discard the clear password */
3161 memset(bv.bv_val, 0, bv.bv_len);
3162 ber_memfree(bv.bv_val);
3163 addmod->sml_values[0] = hpw;
3164 }
3165 } else {
3166 /* ITS#8762 Make sure we drop pwdFailureTime if unlocking */
3167 if (got_del_lock && !got_del_fail && attr_find(e->e_attrs, ad_pwdFailureTime )) {
3168 mods = (Modifications *) ch_calloc( sizeof( Modifications ), 1 );
3169 mods->sml_op = LDAP_MOD_DELETE;
3170 mods->sml_desc = ad_pwdFailureTime;
3171 mods->sml_flags = SLAP_MOD_INTERNAL;
3172 mods->sml_next = NULL;
3173 modtail->sml_next = mods;
3174 modtail = mods;
3175 }
3176 }
3177 op->o_bd->bd_info = (BackendInfo *)on->on_info;
3178 be_entry_release_r( op, e );
3179 return SLAP_CB_CONTINUE;
3180
3181 return_results:
3182 free_pwd_history_list( &tl );
3183 op->o_bd->bd_info = (BackendInfo *)on->on_info;
3184 be_entry_release_r( op, e );
3185 if ( send_ctrl ) {
3186 ctrl = create_passcontrol( op, -1, -1, pErr );
3187 oldctrls = add_passcontrol( op, rs, ctrl );
3188 }
3189 send_ldap_result( op, rs );
3190 if ( free_txt ) {
3191 if ( is_pwdexop ) {
3192 slap_callback *cb;
3193 cb = op->o_tmpcalloc( sizeof(ppbind)+sizeof(slap_callback),
3194 1, op->o_tmpmemctx );
3195
3196 /* Setup a callback so we can free the text when sent */
3197 cb->sc_cleanup = ppolicy_text_cleanup;
3198 cb->sc_private = (void *)txt;
3199 overlay_callback_after_backover( op, cb, 1 );
3200 } else {
3201 if ( rs->sr_text == txt ) {
3202 rs->sr_text = NULL;
3203 }
3204 free( (char *)txt );
3205 }
3206 }
3207 if ( send_ctrl ) {
3208 if ( is_pwdexop ) {
3209 if ( rs->sr_flags & REP_CTRLS_MUSTBEFREED ) {
3210 op->o_tmpfree( oldctrls, op->o_tmpmemctx );
3211 }
3212 oldctrls = NULL;
3213 rs->sr_flags |= REP_CTRLS_MUSTBEFREED;
3214
3215 } else {
3216 ctrls_cleanup( op, rs, oldctrls );
3217 }
3218 }
3219 return rs->sr_err;
3220 }
3221
3222 static int
3223 ppolicy_parseCtrl(
3224 Operation *op,
3225 SlapReply *rs,
3226 LDAPControl *ctrl )
3227 {
3228 if ( !BER_BVISNULL( &ctrl->ldctl_value ) ) {
3229 rs->sr_text = "passwordPolicyRequest control value not absent";
3230 return LDAP_PROTOCOL_ERROR;
3231 }
3232 op->o_ctrlflag[ppolicy_cid] = ctrl->ldctl_iscritical
3233 ? SLAP_CONTROL_CRITICAL
3234 : SLAP_CONTROL_NONCRITICAL;
3235
3236 return LDAP_SUCCESS;
3237 }
3238
3239 static int
3240 ppolicy_au_parseCtrl(
3241 Operation *op,
3242 SlapReply *rs,
3243 LDAPControl *ctrl )
3244 {
3245 if ( !BER_BVISNULL( &ctrl->ldctl_value ) ) {
3246 rs->sr_text = "account usability control value not absent";
3247 return LDAP_PROTOCOL_ERROR;
3248 }
3249 op->o_ctrlflag[account_usability_cid] = ctrl->ldctl_iscritical
3250 ? SLAP_CONTROL_CRITICAL
3251 : SLAP_CONTROL_NONCRITICAL;
3252
3253 return LDAP_SUCCESS;
3254 }
3255
3256 static int
3257 attrPretty(
3258 Syntax *syntax,
3259 struct berval *val,
3260 struct berval *out,
3261 void *ctx )
3262 {
3263 AttributeDescription *ad = NULL;
3264 const char *err;
3265 int code;
3266
3267 code = slap_bv2ad( val, &ad, &err );
3268 if ( !code ) {
3269 ber_dupbv_x( out, &ad->ad_type->sat_cname, ctx );
3270 }
3271 return code;
3272 }
3273
3274 static int
3275 attrNormalize(
3276 slap_mask_t use,
3277 Syntax *syntax,
3278 MatchingRule *mr,
3279 struct berval *val,
3280 struct berval *out,
3281 void *ctx )
3282 {
3283 AttributeDescription *ad = NULL;
3284 const char *err;
3285 int code;
3286
3287 code = slap_bv2ad( val, &ad, &err );
3288 if ( !code ) {
3289 ber_str2bv_x( ad->ad_type->sat_oid, 0, 1, out, ctx );
3290 }
3291 return code;
3292 }
3293
3294 static int
3295 ppolicy_db_init(
3296 BackendDB *be,
3297 ConfigReply *cr
3298 )
3299 {
3300 slap_overinst *on = (slap_overinst *) be->bd_info;
3301 pp_info *pi;
3302
3303 if ( SLAP_ISGLOBALOVERLAY( be ) ) {
3304 /* do not allow slapo-ppolicy to be global by now (ITS#5858) */
3305 if ( cr ){
3306 snprintf( cr->msg, sizeof(cr->msg),
3307 "slapo-ppolicy cannot be global" );
3308 Debug( LDAP_DEBUG_ANY, "%s\n", cr->msg );
3309 }
3310 return 1;
3311 }
3312
3313 pi = on->on_bi.bi_private = ch_calloc( sizeof(pp_info), 1 );
3314
3315 if ( !pwcons ) {
3316 /* accommodate for c_conn_idx == -1 */
3317 pwcons = ch_calloc( sizeof(pw_conn), dtblsize + 1 );
3318 pwcons++;
3319 }
3320
3321 ov_count++;
3322
3323 ldap_pvt_thread_mutex_init( &pi->pwdFailureTime_mutex );
3324
3325 return 0;
3326 }
3327
3328 static int
3329 ppolicy_db_open(
3330 BackendDB *be,
3331 ConfigReply *cr
3332 )
3333 {
3334 int rc;
3335
3336 if ( (rc = overlay_register_control( be, LDAP_CONTROL_X_ACCOUNT_USABILITY )) != LDAP_SUCCESS ) {
3337 return rc;
3338 }
3339 return overlay_register_control( be, LDAP_CONTROL_PASSWORDPOLICYREQUEST );
3340 }
3341
3342 static int
3343 ppolicy_db_close(
3344 BackendDB *be,
3345 ConfigReply *cr
3346 )
3347 {
3348 #ifdef SLAP_CONFIG_DELETE
3349 overlay_unregister_control( be, LDAP_CONTROL_PASSWORDPOLICYREQUEST );
3350 overlay_unregister_control( be, LDAP_CONTROL_X_ACCOUNT_USABILITY );
3351 #endif /* SLAP_CONFIG_DELETE */
3352
3353 return 0;
3354 }
3355
3356 static int
3357 ppolicy_db_destroy(
3358 BackendDB *be,
3359 ConfigReply *cr
3360 )
3361 {
3362 slap_overinst *on = (slap_overinst *) be->bd_info;
3363 pp_info *pi = on->on_bi.bi_private;
3364
3365 on->on_bi.bi_private = NULL;
3366 ldap_pvt_thread_mutex_destroy( &pi->pwdFailureTime_mutex );
3367 free( pi->def_policy.bv_val );
3368 free( pi );
3369
3370 ov_count--;
3371 if ( ov_count <=0 && pwcons ) {
3372 pw_conn *pwc = pwcons;
3373 pwcons = NULL;
3374 pwc--;
3375 ch_free( pwc );
3376 }
3377 return 0;
3378 }
3379
3380 static char *extops[] = {
3381 LDAP_EXOP_MODIFY_PASSWD,
3382 NULL
3383 };
3384
3385 static slap_overinst ppolicy;
3386
3387 int ppolicy_initialize()
3388 {
3389 int i, code;
3390
3391 for (i=0; pwd_OpSchema[i].def; i++) {
3392 code = register_at( pwd_OpSchema[i].def, pwd_OpSchema[i].ad, 0 );
3393 if ( code ) {
3394 Debug( LDAP_DEBUG_ANY,
3395 "ppolicy_initialize: register_at failed\n" );
3396 return code;
3397 }
3398 /* Allow Manager to set these as needed */
3399 if ( is_at_no_user_mod( (*pwd_OpSchema[i].ad)->ad_type )) {
3400 (*pwd_OpSchema[i].ad)->ad_type->sat_flags |=
3401 SLAP_AT_MANAGEABLE;
3402 }
3403 }
3404 ad_pwdLastSuccess = slap_schema.si_ad_pwdLastSuccess;
3405 {
3406 Syntax *syn;
3407 MatchingRule *mr;
3408
3409 syn = ch_malloc( sizeof( Syntax ));
3410 *syn = *ad_pwdAttribute->ad_type->sat_syntax;
3411 syn->ssyn_pretty = attrPretty;
3412 ad_pwdAttribute->ad_type->sat_syntax = syn;
3413
3414 mr = ch_malloc( sizeof( MatchingRule ));
3415 *mr = *ad_pwdAttribute->ad_type->sat_equality;
3416 mr->smr_normalize = attrNormalize;
3417 ad_pwdAttribute->ad_type->sat_equality = mr;
3418 }
3419
3420 for (i=0; pwd_ocs[i]; i++) {
3421 code = register_oc( pwd_ocs[i], NULL, 0 );
3422 if ( code ) {
3423 Debug( LDAP_DEBUG_ANY, "ppolicy_initialize: "
3424 "register_oc failed\n" );
3425 return code;
3426 }
3427 }
3428
3429 code = register_supported_control( LDAP_CONTROL_PASSWORDPOLICYREQUEST,
3430 SLAP_CTRL_ADD|SLAP_CTRL_BIND|SLAP_CTRL_MODIFY, extops,
3431 ppolicy_parseCtrl, &ppolicy_cid );
3432 if ( code != LDAP_SUCCESS ) {
3433 Debug( LDAP_DEBUG_ANY, "Failed to register control %d\n", code );
3434 return code;
3435 }
3436
3437 code = register_supported_control( LDAP_CONTROL_X_ACCOUNT_USABILITY,
3438 SLAP_CTRL_SEARCH, NULL,
3439 ppolicy_au_parseCtrl, &account_usability_cid );
3440 if ( code != LDAP_SUCCESS ) {
3441 Debug( LDAP_DEBUG_ANY, "Failed to register control %d\n", code );
3442 return code;
3443 }
3444
3445 /* We don't expect to receive these controls, only send them */
3446 code = register_supported_control( LDAP_CONTROL_X_PASSWORD_EXPIRED,
3447 0, NULL, NULL, NULL );
3448 if ( code != LDAP_SUCCESS ) {
3449 Debug( LDAP_DEBUG_ANY, "Failed to register control %d\n", code );
3450 return code;
3451 }
3452
3453 code = register_supported_control( LDAP_CONTROL_X_PASSWORD_EXPIRING,
3454 0, NULL, NULL, NULL );
3455 if ( code != LDAP_SUCCESS ) {
3456 Debug( LDAP_DEBUG_ANY, "Failed to register control %d\n", code );
3457 return code;
3458 }
3459
3460 ldap_pvt_thread_mutex_init( &chk_syntax_mutex );
3461
3462 ppolicy.on_bi.bi_type = "ppolicy";
3463 ppolicy.on_bi.bi_flags = SLAPO_BFLAG_SINGLE;
3464 ppolicy.on_bi.bi_db_init = ppolicy_db_init;
3465 ppolicy.on_bi.bi_db_open = ppolicy_db_open;
3466 ppolicy.on_bi.bi_db_close = ppolicy_db_close;
3467 ppolicy.on_bi.bi_db_destroy = ppolicy_db_destroy;
3468
3469 ppolicy.on_bi.bi_op_add = ppolicy_add;
3470 ppolicy.on_bi.bi_op_bind = ppolicy_bind;
3471 ppolicy.on_bi.bi_op_compare = ppolicy_compare;
3472 ppolicy.on_bi.bi_op_delete = ppolicy_restrict;
3473 ppolicy.on_bi.bi_op_modify = ppolicy_modify;
3474 ppolicy.on_bi.bi_op_search = ppolicy_search;
3475 ppolicy.on_bi.bi_connection_destroy = ppolicy_connection_destroy;
3476
3477 ppolicy.on_bi.bi_cf_ocs = ppolicyocs;
3478 code = config_register_schema( ppolicycfg, ppolicyocs );
3479 if ( code ) return code;
3480
3481 return overlay_register( &ppolicy );
3482 }
3483
3484 #if SLAPD_OVER_PPOLICY == SLAPD_MOD_DYNAMIC
3485 int init_module(int argc, char *argv[]) {
3486 return ppolicy_initialize();
3487 }
3488 #endif
3489
3490 #endif /* defined(SLAPD_OVER_PPOLICY) */