#include <ldap.h>
#include "lutil.h"
#include "slap.h"
+#if SLAPD_MODULES
#include <ltdl.h>
+#endif
#include <ac/errno.h>
#include <ac/time.h>
#include <ac/string.h>
typedef struct pp_info {
struct berval def_policy; /* DN of default policy subentry */
int use_lockout; /* send AccountLocked result? */
+ int hash_passwords; /* transparently hash cleartext pwds */
} pp_info;
/* Our per-connection info - note, it is not per-instance, it is
} pw_conn;
static pw_conn *pwcons;
+static int ppolicy_cid;
typedef struct pass_policy {
AttributeDescription *ad; /* attribute to which the policy applies */
}
static int
-check_password_quality( struct berval *cred, PassPolicy *pp, LDAPPasswordPolicyError *err )
+check_password_quality( struct berval *cred, PassPolicy *pp, LDAPPasswordPolicyError *err, Entry *e )
{
int rc = LDAP_SUCCESS, ok = LDAP_SUCCESS;
char *ptr = cred->bv_val;
}
rc = LDAP_SUCCESS;
-
+
if (pp->pwdCheckModule[0]) {
+#if SLAPD_MODULES
lt_dlhandle mod;
const char *err;
pp->pwdCheckModule, err, 0 );
ok = LDAP_OTHER; /* internal error */
} else {
- int (*prog)( char *passwd, char **text, void *arg );
+ int (*prog)( char *passwd, char **text, Attribute *attrs );
if ((prog = lt_dlsym( mod, "check_password" )) == NULL) {
err = lt_dlerror();
char *txt = NULL;
ldap_pvt_thread_mutex_lock( &chk_syntax_mutex );
- ok = prog( cred->bv_val, &txt, NULL );
+ ok = prog( cred->bv_val, &txt, e ? e->e_attrs : NULL );
ldap_pvt_thread_mutex_unlock( &chk_syntax_mutex );
if (txt) {
Debug(LDAP_DEBUG_ANY,
lt_dlclose( mod );
}
+#else
+ Debug(LDAP_DEBUG_ANY, "check_password_quality: external modules not "
+ "supported. pwdCheckModule ignored.\n", 0, 0, 0);
+#endif /* SLAPD_MODULES */
}
}
now = slap_get_time(); /* stored for later consideration */
- ldap_pvt_thread_mutex_lock( &SLAPD_GLOBAL(gmtime_mutex ));
+ ldap_pvt_thread_mutex_lock( &gmtime_mutex );
tm = gmtime(&now);
lutil_gentime( nowstr, sizeof(nowstr), tm );
- ldap_pvt_thread_mutex_unlock( &SLAPD_GLOBAL(gmtime_mutex ));
+ ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
if ( rs->sr_err == LDAP_INVALID_CREDENTIALS ) {
int i = 0, fc = 0;
op->o_callback->sc_next = cb;
/* Did we receive a password policy request control? */
- for ( i=0; op->o_ctrls && op->o_ctrls[i]; i++ ) {
- if ( !strcmp( op->o_ctrls[i]->ldctl_oid,
- LDAP_CONTROL_PASSWORDPOLICYREQUEST ) )
- {
- ppb->send_ctrl = 1;
- break;
- }
+ if ( op->o_ctrlflag[ppolicy_cid] ) {
+ ppb->send_ctrl = 1;
}
op->o_bd->bd_info = (BackendInfo *)on;
int i, send_ctrl = 0;
/* Did we receive a password policy request control? */
- for ( i=0; op->o_ctrls && op->o_ctrls[i]; i++ ) {
- if ( !strcmp( op->o_ctrls[i]->ldctl_oid,
- LDAP_CONTROL_PASSWORDPOLICYREQUEST ) ) {
- send_ctrl = 1;
- break;
- }
+ if ( op->o_ctrlflag[ppolicy_cid] ) {
+ send_ctrl = 1;
}
if ( op->o_conn && pwcons[op->o_conn->c_conn_idx].restrict ) {
SlapReply *rs )
{
slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
+ pp_info *pi = on->on_bi.bi_private;
PassPolicy pp;
int pw;
Attribute *pa;
+ const char *txt;
if ( ppolicy_restrict( op, rs ) != SLAP_CB_CONTINUE )
return rs->sr_err;
* then we need to check that the password fits in with the
* security policy for the new entry.
*/
- ppolicy_get( op, op->oq_add.rs_e, &pp );
+ ppolicy_get( op, op->ora_e, &pp );
if (pp.pwdCheckQuality > 0 && !be_isroot( op )) {
struct berval *bv = &(pa->a_vals[0]);
int rc, i, send_ctrl = 0;
LDAPPasswordPolicyError pErr = PP_noError;
/* Did we receive a password policy request control? */
- for ( i=0; op->o_ctrls && op->o_ctrls[i]; i++ ) {
- if ( !strcmp( op->o_ctrls[i]->ldctl_oid,
- LDAP_CONTROL_PASSWORDPOLICYREQUEST ) ) {
- send_ctrl = 1;
- break;
- }
+ if ( op->o_ctrlflag[ppolicy_cid] ) {
+ send_ctrl = 1;
}
- rc = check_password_quality( bv, &pp, &pErr );
+ rc = check_password_quality( bv, &pp, &pErr, op->ora_e );
if (rc != LDAP_SUCCESS) {
op->o_bd->bd_info = (BackendInfo *)on->on_info;
if ( send_ctrl ) {
send_ldap_error( op, rs, rc, "Password fails quality checking policy" );
return rs->sr_err;
}
+ /*
+ * A controversial bit. We hash cleartext
+ * passwords provided via add and modify operations
+ * You're not really supposed to do this, since
+ * the X.500 model says "store attributes" as they
+ * get provided. By default, this is what we do
+ *
+ * But if the hash_passwords flag is set, we hash
+ * any cleartext password attribute values via the
+ * default password hashing scheme.
+ */
+ if ((pi->hash_passwords) &&
+ (password_scheme( &(pa->a_vals[0]), NULL ) != LDAP_SUCCESS)) {
+ struct berval hpw;
+
+ slap_passwd_hash( &(pa->a_vals[0]), &hpw, &txt );
+ if (hpw.bv_val == NULL) {
+ /*
+ * hashing didn't work. Emit an error.
+ */
+ rs->sr_err = LDAP_OTHER;
+ rs->sr_text = txt;
+ send_ldap_error( op, rs, LDAP_OTHER, "Password hashing failed" );
+ return rs->sr_err;
+ }
+
+ memset( pa->a_vals[0].bv_val, 0, pa->a_vals[0].bv_len);
+ ber_memfree( pa->a_vals[0].bv_val );
+ pa->a_vals[0].bv_val = hpw.bv_val;
+ pa->a_vals[0].bv_len = hpw.bv_len;
+ }
}
/* If password aging is in effect, set the pwdChangedTime */
if (( pp.pwdMaxAge || pp.pwdMinAge ) && !be_isupdate( op )) {
struct tm *ltm;
time_t now = slap_get_time();
- ldap_pvt_thread_mutex_lock( &SLAPD_GLOBAL(gmtime_mutex ));
+ ldap_pvt_thread_mutex_lock( &gmtime_mutex );
ltm = gmtime( &now );
lutil_gentime( timebuf, sizeof(timebuf), ltm );
- ldap_pvt_thread_mutex_unlock( &SLAPD_GLOBAL(gmtime_mutex ));
+ ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
timestamp.bv_val = timebuf;
timestamp.bv_len = strlen(timebuf);
ppolicy_modify( Operation *op, SlapReply *rs )
{
slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
+ pp_info *pi = on->on_bi.bi_private;
int i, rc, mod_pw_only, pwmod, pwmop, deladd,
hsize = 0;
PassPolicy pp;
if ( rc != LDAP_SUCCESS ) return SLAP_CB_CONTINUE;
/* Did we receive a password policy request control? */
- for ( i=0; op->o_ctrls && op->o_ctrls[i]; i++ ) {
- if ( !strcmp( op->o_ctrls[i]->ldctl_oid,
- LDAP_CONTROL_PASSWORDPOLICYREQUEST ) ) {
- send_ctrl = 1;
- break;
- }
+ if ( op->o_ctrlflag[ppolicy_cid] ) {
+ send_ctrl = 1;
}
/* See if this is a pwdModify exop. If so, we can
bv = newpw.bv_val ? &newpw : addmod->sml_values;
if (pp.pwdCheckQuality > 0) {
- rc = check_password_quality( bv, &pp, &pErr );
+ rc = check_password_quality( bv, &pp, &pErr, e );
if (rc != LDAP_SUCCESS) {
rs->sr_err = rc;
rs->sr_text = "Password fails quality checking policy";
* up to date.
*/
- ldap_pvt_thread_mutex_lock( &SLAPD_GLOBAL(gmtime_mutex ));
+ ldap_pvt_thread_mutex_lock( &gmtime_mutex );
ltm = gmtime( &now );
lutil_gentime( timebuf, sizeof(timebuf), ltm );
- ldap_pvt_thread_mutex_unlock( &SLAPD_GLOBAL(gmtime_mutex ));
+ ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
timestamp.bv_val = timebuf;
timestamp.bv_len = strlen(timebuf);
* leave it alone.
*/
- if ((addmod) && !newpw.bv_val &&
+ if ((pi->hash_passwords) && (addmod) && !newpw.bv_val &&
(password_scheme( &(addmod->sml_values[0]), NULL ) != LDAP_SUCCESS)) {
struct berval hpw, bv;
rs->sr_text = "passwordPolicyRequest control invalid criticality";
return LDAP_PROTOCOL_ERROR;
}
+ op->o_ctrlflag[ppolicy_cid] = SLAP_CONTROL_NONCRITICAL;
return LDAP_SUCCESS;
}
on->on_bi.bi_private = ch_calloc( sizeof(pp_info), 1 );
- if ( SLAPD_GLOBAL(dtblsize) && !pwcons )
- pwcons = ch_calloc(sizeof(pw_conn), SLAPD_GLOBAL(dtblsize) );
+ if ( dtblsize && !pwcons )
+ pwcons = ch_calloc(sizeof(pw_conn), dtblsize );
return 0;
}
}
pi->use_lockout = 1;
return 0;
+ } else if ( strcasecmp( argv[0], "ppolicy_hash_cleartext" ) == 0 ) {
+ if ( argc != 1 ) {
+ fprintf( stderr, "%s: line %d: ppolicy_hash_cleartext "
+ "takes no arguments\n", fname, lineno );
+ return ( 1 );
+ }
+ pi->hash_passwords = 1;
}
return SLAP_CONF_UNKNOWN;
}
code = register_supported_control( LDAP_CONTROL_PASSWORDPOLICYREQUEST,
SLAP_CTRL_ADD|SLAP_CTRL_BIND|SLAP_CTRL_MODIFY, extops,
- ppolicy_parseCtrl );
+ ppolicy_parseCtrl, &ppolicy_cid );
if ( code != LDAP_SUCCESS ) {
fprintf( stderr, "Failed to register control %d\n", code );
return code;