1 /* passwd.c - password extended operation routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2017 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
21 #include <ac/socket.h>
22 #include <ac/string.h>
23 #include <ac/unistd.h>
28 #endif /* HAVE_CRYPT_R */
30 #endif /* SLAPD_CRYPT */
36 #include <lutil_sha1.h>
38 const struct berval slap_EXOP_MODIFY_PASSWD = BER_BVC(LDAP_EXOP_MODIFY_PASSWD);
40 static const char *defhash[] = {
41 #ifdef LUTIL_SHA1_BYTES
53 struct berval id = {0, NULL}, hash, *rsp = NULL;
54 req_pwdexop_s *qpw = &op->oq_pwdexop;
55 req_extended_s qext = op->oq_extended;
57 slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
63 struct berval dn = BER_BVNULL, ndn = BER_BVNULL;
65 assert( ber_bvcmp( &slap_EXOP_MODIFY_PASSWD, &op->ore_reqoid ) == 0 );
67 if( op->o_dn.bv_len == 0 ) {
68 Statslog( LDAP_DEBUG_STATS, "%s PASSMOD\n",
69 op->o_log_prefix, 0, 0, 0, 0 );
70 rs->sr_text = "only authenticated users may change passwords";
71 return LDAP_STRONG_AUTH_REQUIRED;
74 qpw->rs_old.bv_len = 0;
75 qpw->rs_old.bv_val = NULL;
76 qpw->rs_new.bv_len = 0;
77 qpw->rs_new.bv_val = NULL;
79 qpw->rs_modtail = NULL;
81 rs->sr_err = slap_passwd_parse( op->ore_reqdata, &id,
82 &qpw->rs_old, &qpw->rs_new, &rs->sr_text );
84 if ( !BER_BVISNULL( &id )) {
85 idNul = id.bv_val[id.bv_len];
86 id.bv_val[id.bv_len] = '\0';
88 if ( rs->sr_err == LDAP_SUCCESS && !BER_BVISEMPTY( &id ) ) {
89 Statslog( LDAP_DEBUG_STATS, "%s PASSMOD id=\"%s\"%s%s\n",
90 op->o_log_prefix, id.bv_val,
91 qpw->rs_old.bv_val ? " old" : "",
92 qpw->rs_new.bv_val ? " new" : "", 0 );
94 Statslog( LDAP_DEBUG_STATS, "%s PASSMOD%s%s\n",
96 qpw->rs_old.bv_val ? " old" : "",
97 qpw->rs_new.bv_val ? " new" : "", 0, 0 );
100 if ( rs->sr_err != LDAP_SUCCESS ) {
101 if ( !BER_BVISNULL( &id ))
102 id.bv_val[id.bv_len] = idNul;
106 if ( !BER_BVISEMPTY( &id ) ) {
107 rs->sr_err = dnPrettyNormal( NULL, &id, &dn, &ndn, op->o_tmpmemctx );
108 id.bv_val[id.bv_len] = idNul;
109 if ( rs->sr_err != LDAP_SUCCESS ) {
110 rs->sr_text = "Invalid DN";
116 op->o_bd = select_backend( &op->o_req_ndn, 1 );
119 ber_dupbv_x( &dn, &op->o_dn, op->o_tmpmemctx );
120 ber_dupbv_x( &ndn, &op->o_ndn, op->o_tmpmemctx );
123 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
124 op->o_bd = op->o_conn->c_authz_backend;
125 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
128 if( op->o_bd == NULL ) {
129 if ( qpw->rs_old.bv_val != NULL ) {
130 rs->sr_text = "unwilling to verify old password";
131 rc = LDAP_UNWILLING_TO_PERFORM;
135 #ifdef HAVE_CYRUS_SASL
136 rc = slap_sasl_setpass( op, rs );
138 rs->sr_text = "no authz backend";
144 if ( op->o_req_ndn.bv_len == 0 ) {
145 rs->sr_text = "no password is associated with the Root DSE";
146 rc = LDAP_UNWILLING_TO_PERFORM;
150 /* If we've got a glued backend, check the real backend */
152 if ( SLAP_GLUE_INSTANCE( op->o_bd )) {
153 op->o_bd = select_backend( &op->o_req_ndn, 0 );
156 if (backend_check_restrictions( op, rs,
157 (struct berval *)&slap_EXOP_MODIFY_PASSWD ) != LDAP_SUCCESS) {
162 /* check for referrals */
163 if ( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
168 /* This does not apply to multi-master case */
169 if(!( !SLAP_SINGLE_SHADOW( op->o_bd ) || be_isupdate( op ))) {
170 /* we SHOULD return a referral in this case */
171 BerVarray defref = op->o_bd->be_update_refs
172 ? op->o_bd->be_update_refs : default_referral;
174 if( defref != NULL ) {
175 rs->sr_ref = referral_rewrite( op->o_bd->be_update_refs,
176 NULL, NULL, LDAP_SCOPE_DEFAULT );
178 rs->sr_flags |= REP_REF_MUSTBEFREED;
187 rs->sr_text = "shadow context; no update referral";
188 rc = LDAP_UNWILLING_TO_PERFORM;
192 /* generate a new password if none was provided */
193 if ( qpw->rs_new.bv_len == 0 ) {
194 slap_passwd_generate( &qpw->rs_new );
195 if ( qpw->rs_new.bv_len ) {
196 rsp = slap_passwd_return( &qpw->rs_new );
200 if ( qpw->rs_new.bv_len == 0 ) {
201 rs->sr_text = "password generation failed";
208 /* Give the backend a chance to handle this itself */
209 if ( op->o_bd->be_extended ) {
210 rs->sr_err = op->o_bd->be_extended( op, rs );
211 if ( rs->sr_err != LDAP_UNWILLING_TO_PERFORM &&
212 rs->sr_err != SLAP_CB_CONTINUE )
216 rs->sr_rspdata = rsp;
223 /* The backend didn't handle it, so try it here */
224 if( op->o_bd && !op->o_bd->be_modify ) {
225 rs->sr_text = "operation not supported for current user";
226 rc = LDAP_UNWILLING_TO_PERFORM;
230 if ( qpw->rs_old.bv_val != NULL ) {
233 rc = be_entry_get_rw( op, &op->o_req_ndn, NULL,
234 slap_schema.si_ad_userPassword, 0, &e );
235 if ( rc == LDAP_SUCCESS && e ) {
236 Attribute *a = attr_find( e->e_attrs,
237 slap_schema.si_ad_userPassword );
239 rc = slap_passwd_check( op, e, a, &qpw->rs_old, &rs->sr_text );
242 be_entry_release_r( op, e );
243 if ( rc == LDAP_SUCCESS )
246 rs->sr_text = "unwilling to verify old password";
247 rc = LDAP_UNWILLING_TO_PERFORM;
252 ml = ch_malloc( sizeof(Modifications) );
253 if ( !qpw->rs_modtail ) qpw->rs_modtail = &ml->sml_next;
255 if ( default_passwd_hash ) {
256 for ( nhash = 0; default_passwd_hash[nhash]; nhash++ );
257 hashes = default_passwd_hash;
260 hashes = (char **)defhash;
262 ml->sml_numvals = nhash;
263 ml->sml_values = ch_malloc( (nhash+1)*sizeof(struct berval) );
264 for ( i=0; hashes[i]; i++ ) {
265 slap_passwd_hash_type( &qpw->rs_new, &hash, hashes[i], &rs->sr_text );
266 if ( hash.bv_len == 0 ) {
267 if ( !rs->sr_text ) {
268 rs->sr_text = "password hash failed";
272 ml->sml_values[i] = hash;
274 ml->sml_values[i].bv_val = NULL;
275 ml->sml_nvalues = NULL;
276 ml->sml_desc = slap_schema.si_ad_userPassword;
277 ml->sml_type = ml->sml_desc->ad_cname;
278 ml->sml_op = LDAP_MOD_REPLACE;
280 ml->sml_next = qpw->rs_mods;
284 rs->sr_err = LDAP_OTHER;
287 slap_callback *sc = op->o_callback;
289 op->o_tag = LDAP_REQ_MODIFY;
290 op->o_callback = &cb;
291 op->orm_modlist = qpw->rs_mods;
292 op->orm_no_opattrs = 0;
294 cb.sc_private = qpw; /* let Modify know this was pwdMod,
297 rs->sr_err = op->o_bd->be_modify( op, rs );
299 /* be_modify() might have shuffled modifications */
300 qpw->rs_mods = op->orm_modlist;
302 if ( rs->sr_err == LDAP_SUCCESS ) {
303 rs->sr_rspdata = rsp;
309 op->o_tag = LDAP_REQ_EXTENDED;
314 op->oq_extended = qext;
317 if ( qpw->rs_mods ) {
318 slap_mods_free( qpw->rs_mods, 1 );
321 free( qpw->rs_new.bv_val );
323 if ( !BER_BVISNULL( &dn ) ) {
324 op->o_tmpfree( dn.bv_val, op->o_tmpmemctx );
325 BER_BVZERO( &op->o_req_dn );
327 if ( !BER_BVISNULL( &ndn ) ) {
328 op->o_tmpfree( ndn.bv_val, op->o_tmpmemctx );
329 BER_BVZERO( &op->o_req_ndn );
335 /* NOTE: The DN in *id is NOT NUL-terminated here. dnNormalize will
336 * reject it in this condition, the caller must NUL-terminate it.
337 * FIXME: should dnNormalize still be complaining about that?
339 int slap_passwd_parse( struct berval *reqdata,
341 struct berval *oldpass,
342 struct berval *newpass,
345 int rc = LDAP_SUCCESS;
348 BerElementBuffer berbuf;
349 BerElement *ber = (BerElement *)&berbuf;
351 if( reqdata == NULL ) {
355 if( reqdata->bv_len == 0 ) {
356 *text = "empty request data field";
357 return LDAP_PROTOCOL_ERROR;
360 /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
361 ber_init2( ber, reqdata, 0 );
363 tag = ber_skip_tag( ber, &len );
365 if( tag != LBER_SEQUENCE ) {
366 Debug( LDAP_DEBUG_TRACE,
367 "slap_passwd_parse: decoding error\n", 0, 0, 0 );
368 rc = LDAP_PROTOCOL_ERROR;
372 tag = ber_peek_tag( ber, &len );
373 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_ID ) {
375 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID not allowed.\n",
378 *text = "user must change own password";
379 rc = LDAP_UNWILLING_TO_PERFORM;
383 tag = ber_get_stringbv( ber, id, LBER_BV_NOTERM );
385 if( tag == LBER_ERROR ) {
386 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
392 tag = ber_peek_tag( ber, &len );
395 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_OLD ) {
396 if( oldpass == NULL ) {
397 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD not allowed.\n",
400 *text = "use bind to verify old password";
401 rc = LDAP_UNWILLING_TO_PERFORM;
405 tag = ber_get_stringbv( ber, oldpass, LBER_BV_NOTERM );
407 if( tag == LBER_ERROR ) {
408 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD parse failed.\n",
414 if( oldpass->bv_len == 0 ) {
415 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD empty.\n",
418 *text = "old password value is empty";
419 rc = LDAP_UNWILLING_TO_PERFORM;
423 tag = ber_peek_tag( ber, &len );
426 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ) {
427 if( newpass == NULL ) {
428 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW not allowed.\n",
431 *text = "user specified passwords disallowed";
432 rc = LDAP_UNWILLING_TO_PERFORM;
436 tag = ber_get_stringbv( ber, newpass, LBER_BV_NOTERM );
438 if( tag == LBER_ERROR ) {
439 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW parse failed.\n",
445 if( newpass->bv_len == 0 ) {
446 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW empty.\n",
449 *text = "new password value is empty";
450 rc = LDAP_UNWILLING_TO_PERFORM;
454 tag = ber_peek_tag( ber, &len );
459 Debug( LDAP_DEBUG_TRACE,
460 "slap_passwd_parse: decoding error, len=%ld\n",
463 *text = "data decoding error";
464 rc = LDAP_PROTOCOL_ERROR;
471 struct berval * slap_passwd_return(
472 struct berval *cred )
475 struct berval *bv = NULL;
476 BerElementBuffer berbuf;
477 /* opaque structure, size unknown but smaller than berbuf */
478 BerElement *ber = (BerElement *)&berbuf;
480 assert( cred != NULL );
482 Debug( LDAP_DEBUG_TRACE, "slap_passwd_return: %ld\n",
483 (long) cred->bv_len, 0, 0 );
485 ber_init_w_nullc( ber, LBER_USE_DER );
487 rc = ber_printf( ber, "{tON}",
488 LDAP_TAG_EXOP_MODIFY_PASSWD_GEN, cred );
491 (void) ber_flatten( ber, &bv );
500 * if "e" is provided, access to each value of the password is checked first
512 AccessControlState acl_state = ACL_STATE_INIT;
513 char credNul = cred->bv_val[cred->bv_len];
516 void *old_authctx = NULL;
518 ldap_pvt_thread_pool_setkey( op->o_threadctx, (void *)slap_sasl_bind,
519 op->o_conn->c_sasl_authctx, 0, &old_authctx, NULL );
522 if ( credNul ) cred->bv_val[cred->bv_len] = 0;
524 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
525 /* if e is provided, check access */
526 if ( e && access_allowed( op, e, a->a_desc, bv,
527 ACL_AUTH, &acl_state ) == 0 )
532 if ( !lutil_passwd( bv, cred, NULL, text ) ) {
538 if ( credNul ) cred->bv_val[cred->bv_len] = credNul;
541 ldap_pvt_thread_pool_setkey( op->o_threadctx, (void *)slap_sasl_bind,
542 old_authctx, 0, NULL, NULL );
549 slap_passwd_generate( struct berval *pass )
551 Debug( LDAP_DEBUG_TRACE, "slap_passwd_generate\n", 0, 0, 0 );
555 * generate passwords of only 8 characters as some getpass(3)
556 * implementations truncate at 8 characters.
558 lutil_passwd_generate( pass, 8 );
562 slap_passwd_hash_type(
563 struct berval * cred,
571 assert( hash != NULL );
573 lutil_passwd_hash( cred , hash, new, text );
577 struct berval * cred,
582 if ( default_passwd_hash ) {
583 hash = default_passwd_hash[0];
586 hash = (char *)defhash[0];
589 slap_passwd_hash_type( cred, new, hash, text );
593 static ldap_pvt_thread_mutex_t passwd_mutex;
594 static lutil_cryptfunc slapd_crypt;
597 static int slapd_crypt( const char *key, const char *salt, char **hash )
601 struct crypt_data data;
603 data.initialized = 0;
604 cr = crypt_r( key, salt, &data );
605 if ( cr == NULL || cr[0] == '\0' ) {
606 /* salt must have been invalid */
607 rc = LUTIL_PASSWD_ERR;
610 *hash = ber_strdup( cr );
611 rc = LUTIL_PASSWD_OK;
613 rc = strcmp( salt, cr ) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
620 static int slapd_crypt( const char *key, const char *salt, char **hash )
625 ldap_pvt_thread_mutex_lock( &passwd_mutex );
627 cr = crypt( key, salt );
628 if ( cr == NULL || cr[0] == '\0' ) {
629 /* salt must have been invalid */
630 rc = LUTIL_PASSWD_ERR;
633 *hash = ber_strdup( cr );
634 rc = LUTIL_PASSWD_OK;
637 rc = strcmp( salt, cr ) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
641 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
644 #endif /* HAVE_CRYPT_R */
646 #endif /* SLAPD_CRYPT */
648 void slap_passwd_init()
651 ldap_pvt_thread_mutex_init( &passwd_mutex );
652 lutil_cryptptr = slapd_crypt;