1 /* passwd.c - password extended operation routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2014 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>
33 #include <lutil_sha1.h>
35 const struct berval slap_EXOP_MODIFY_PASSWD = BER_BVC(LDAP_EXOP_MODIFY_PASSWD);
37 static const char *defhash[] = {
38 #ifdef LUTIL_SHA1_BYTES
50 struct berval id = {0, NULL}, hash, *rsp = NULL;
51 req_pwdexop_s *qpw = &op->oq_pwdexop;
52 req_extended_s qext = op->oq_extended;
54 slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
60 struct berval dn = BER_BVNULL, ndn = BER_BVNULL;
62 assert( ber_bvcmp( &slap_EXOP_MODIFY_PASSWD, &op->ore_reqoid ) == 0 );
64 if( op->o_dn.bv_len == 0 ) {
65 Statslog( LDAP_DEBUG_STATS, "%s PASSMOD\n",
66 op->o_log_prefix, 0, 0, 0, 0 );
67 rs->sr_text = "only authenticated users may change passwords";
68 return LDAP_STRONG_AUTH_REQUIRED;
71 qpw->rs_old.bv_len = 0;
72 qpw->rs_old.bv_val = NULL;
73 qpw->rs_new.bv_len = 0;
74 qpw->rs_new.bv_val = NULL;
76 qpw->rs_modtail = NULL;
78 rs->sr_err = slap_passwd_parse( op->ore_reqdata, &id,
79 &qpw->rs_old, &qpw->rs_new, &rs->sr_text );
81 if ( !BER_BVISNULL( &id )) {
82 idNul = id.bv_val[id.bv_len];
83 id.bv_val[id.bv_len] = '\0';
85 if ( rs->sr_err == LDAP_SUCCESS && !BER_BVISEMPTY( &id ) ) {
86 Statslog( LDAP_DEBUG_STATS, "%s PASSMOD id=\"%s\"%s%s\n",
87 op->o_log_prefix, id.bv_val,
88 qpw->rs_old.bv_val ? " old" : "",
89 qpw->rs_new.bv_val ? " new" : "", 0 );
91 Statslog( LDAP_DEBUG_STATS, "%s PASSMOD%s%s\n",
93 qpw->rs_old.bv_val ? " old" : "",
94 qpw->rs_new.bv_val ? " new" : "", 0, 0 );
97 if ( rs->sr_err != LDAP_SUCCESS ) {
98 if ( !BER_BVISNULL( &id ))
99 id.bv_val[id.bv_len] = idNul;
103 if ( !BER_BVISEMPTY( &id ) ) {
104 rs->sr_err = dnPrettyNormal( NULL, &id, &dn, &ndn, op->o_tmpmemctx );
105 id.bv_val[id.bv_len] = idNul;
106 if ( rs->sr_err != LDAP_SUCCESS ) {
107 rs->sr_text = "Invalid DN";
113 op->o_bd = select_backend( &op->o_req_ndn, 1 );
116 ber_dupbv_x( &dn, &op->o_dn, op->o_tmpmemctx );
117 ber_dupbv_x( &ndn, &op->o_ndn, op->o_tmpmemctx );
120 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
121 op->o_bd = op->o_conn->c_authz_backend;
122 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
125 if( op->o_bd == NULL ) {
126 if ( qpw->rs_old.bv_val != NULL ) {
127 rs->sr_text = "unwilling to verify old password";
128 rc = LDAP_UNWILLING_TO_PERFORM;
132 #ifdef HAVE_CYRUS_SASL
133 rc = slap_sasl_setpass( op, rs );
135 rs->sr_text = "no authz backend";
141 if ( op->o_req_ndn.bv_len == 0 ) {
142 rs->sr_text = "no password is associated with the Root DSE";
143 rc = LDAP_UNWILLING_TO_PERFORM;
147 /* If we've got a glued backend, check the real backend */
149 if ( SLAP_GLUE_INSTANCE( op->o_bd )) {
150 op->o_bd = select_backend( &op->o_req_ndn, 0 );
153 if (backend_check_restrictions( op, rs,
154 (struct berval *)&slap_EXOP_MODIFY_PASSWD ) != LDAP_SUCCESS) {
159 /* check for referrals */
160 if ( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
165 /* This does not apply to multi-master case */
166 if(!( !SLAP_SINGLE_SHADOW( op->o_bd ) || be_isupdate( op ))) {
167 /* we SHOULD return a referral in this case */
168 BerVarray defref = op->o_bd->be_update_refs
169 ? op->o_bd->be_update_refs : default_referral;
171 if( defref != NULL ) {
172 rs->sr_ref = referral_rewrite( op->o_bd->be_update_refs,
173 NULL, NULL, LDAP_SCOPE_DEFAULT );
175 rs->sr_flags |= REP_REF_MUSTBEFREED;
184 rs->sr_text = "shadow context; no update referral";
185 rc = LDAP_UNWILLING_TO_PERFORM;
189 /* generate a new password if none was provided */
190 if ( qpw->rs_new.bv_len == 0 ) {
191 slap_passwd_generate( &qpw->rs_new );
192 if ( qpw->rs_new.bv_len ) {
193 rsp = slap_passwd_return( &qpw->rs_new );
197 if ( qpw->rs_new.bv_len == 0 ) {
198 rs->sr_text = "password generation failed";
205 /* Give the backend a chance to handle this itself */
206 if ( op->o_bd->be_extended ) {
207 rs->sr_err = op->o_bd->be_extended( op, rs );
208 if ( rs->sr_err != LDAP_UNWILLING_TO_PERFORM &&
209 rs->sr_err != SLAP_CB_CONTINUE )
213 rs->sr_rspdata = rsp;
220 /* The backend didn't handle it, so try it here */
221 if( op->o_bd && !op->o_bd->be_modify ) {
222 rs->sr_text = "operation not supported for current user";
223 rc = LDAP_UNWILLING_TO_PERFORM;
227 if ( qpw->rs_old.bv_val != NULL ) {
230 rc = be_entry_get_rw( op, &op->o_req_ndn, NULL,
231 slap_schema.si_ad_userPassword, 0, &e );
232 if ( rc == LDAP_SUCCESS && e ) {
233 Attribute *a = attr_find( e->e_attrs,
234 slap_schema.si_ad_userPassword );
236 rc = slap_passwd_check( op, e, a, &qpw->rs_old, &rs->sr_text );
239 be_entry_release_r( op, e );
240 if ( rc == LDAP_SUCCESS )
243 rs->sr_text = "unwilling to verify old password";
244 rc = LDAP_UNWILLING_TO_PERFORM;
249 ml = ch_malloc( sizeof(Modifications) );
250 if ( !qpw->rs_modtail ) qpw->rs_modtail = &ml->sml_next;
252 if ( default_passwd_hash ) {
253 for ( nhash = 0; default_passwd_hash[nhash]; nhash++ );
254 hashes = default_passwd_hash;
257 hashes = (char **)defhash;
259 ml->sml_numvals = nhash;
260 ml->sml_values = ch_malloc( (nhash+1)*sizeof(struct berval) );
261 for ( i=0; hashes[i]; i++ ) {
262 slap_passwd_hash_type( &qpw->rs_new, &hash, hashes[i], &rs->sr_text );
263 if ( hash.bv_len == 0 ) {
264 if ( !rs->sr_text ) {
265 rs->sr_text = "password hash failed";
269 ml->sml_values[i] = hash;
271 ml->sml_values[i].bv_val = NULL;
272 ml->sml_nvalues = NULL;
273 ml->sml_desc = slap_schema.si_ad_userPassword;
274 ml->sml_type = ml->sml_desc->ad_cname;
275 ml->sml_op = LDAP_MOD_REPLACE;
277 ml->sml_next = qpw->rs_mods;
281 rs->sr_err = LDAP_OTHER;
284 slap_callback *sc = op->o_callback;
286 op->o_tag = LDAP_REQ_MODIFY;
287 op->o_callback = &cb;
288 op->orm_modlist = qpw->rs_mods;
289 op->orm_no_opattrs = 0;
291 cb.sc_private = qpw; /* let Modify know this was pwdMod,
294 rs->sr_err = op->o_bd->be_modify( op, rs );
296 /* be_modify() might have shuffled modifications */
297 qpw->rs_mods = op->orm_modlist;
299 if ( rs->sr_err == LDAP_SUCCESS ) {
300 rs->sr_rspdata = rsp;
306 op->o_tag = LDAP_REQ_EXTENDED;
311 op->oq_extended = qext;
314 if ( qpw->rs_mods ) {
315 slap_mods_free( qpw->rs_mods, 1 );
318 free( qpw->rs_new.bv_val );
320 if ( !BER_BVISNULL( &dn ) ) {
321 op->o_tmpfree( dn.bv_val, op->o_tmpmemctx );
322 BER_BVZERO( &op->o_req_dn );
324 if ( !BER_BVISNULL( &ndn ) ) {
325 op->o_tmpfree( ndn.bv_val, op->o_tmpmemctx );
326 BER_BVZERO( &op->o_req_ndn );
332 /* NOTE: The DN in *id is NOT NUL-terminated here. dnNormalize will
333 * reject it in this condition, the caller must NUL-terminate it.
334 * FIXME: should dnNormalize still be complaining about that?
336 int slap_passwd_parse( struct berval *reqdata,
338 struct berval *oldpass,
339 struct berval *newpass,
342 int rc = LDAP_SUCCESS;
345 BerElementBuffer berbuf;
346 BerElement *ber = (BerElement *)&berbuf;
348 if( reqdata == NULL ) {
352 if( reqdata->bv_len == 0 ) {
353 *text = "empty request data field";
354 return LDAP_PROTOCOL_ERROR;
357 /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
358 ber_init2( ber, reqdata, 0 );
360 tag = ber_skip_tag( ber, &len );
362 if( tag != LBER_SEQUENCE ) {
363 Debug( LDAP_DEBUG_TRACE,
364 "slap_passwd_parse: decoding error\n", 0, 0, 0 );
365 rc = LDAP_PROTOCOL_ERROR;
369 tag = ber_peek_tag( ber, &len );
370 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_ID ) {
372 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID not allowed.\n",
375 *text = "user must change own password";
376 rc = LDAP_UNWILLING_TO_PERFORM;
380 tag = ber_get_stringbv( ber, id, LBER_BV_NOTERM );
382 if( tag == LBER_ERROR ) {
383 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
389 tag = ber_peek_tag( ber, &len );
392 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_OLD ) {
393 if( oldpass == NULL ) {
394 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD not allowed.\n",
397 *text = "use bind to verify old password";
398 rc = LDAP_UNWILLING_TO_PERFORM;
402 tag = ber_get_stringbv( ber, oldpass, LBER_BV_NOTERM );
404 if( tag == LBER_ERROR ) {
405 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD parse failed.\n",
411 if( oldpass->bv_len == 0 ) {
412 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD empty.\n",
415 *text = "old password value is empty";
416 rc = LDAP_UNWILLING_TO_PERFORM;
420 tag = ber_peek_tag( ber, &len );
423 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ) {
424 if( newpass == NULL ) {
425 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW not allowed.\n",
428 *text = "user specified passwords disallowed";
429 rc = LDAP_UNWILLING_TO_PERFORM;
433 tag = ber_get_stringbv( ber, newpass, LBER_BV_NOTERM );
435 if( tag == LBER_ERROR ) {
436 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW parse failed.\n",
442 if( newpass->bv_len == 0 ) {
443 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW empty.\n",
446 *text = "new password value is empty";
447 rc = LDAP_UNWILLING_TO_PERFORM;
451 tag = ber_peek_tag( ber, &len );
456 Debug( LDAP_DEBUG_TRACE,
457 "slap_passwd_parse: decoding error, len=%ld\n",
460 *text = "data decoding error";
461 rc = LDAP_PROTOCOL_ERROR;
468 struct berval * slap_passwd_return(
469 struct berval *cred )
472 struct berval *bv = NULL;
473 BerElementBuffer berbuf;
474 /* opaque structure, size unknown but smaller than berbuf */
475 BerElement *ber = (BerElement *)&berbuf;
477 assert( cred != NULL );
479 Debug( LDAP_DEBUG_TRACE, "slap_passwd_return: %ld\n",
480 (long) cred->bv_len, 0, 0 );
482 ber_init_w_nullc( ber, LBER_USE_DER );
484 rc = ber_printf( ber, "{tON}",
485 LDAP_TAG_EXOP_MODIFY_PASSWD_GEN, cred );
488 (void) ber_flatten( ber, &bv );
497 * if "e" is provided, access to each value of the password is checked first
509 AccessControlState acl_state = ACL_STATE_INIT;
510 char credNul = cred->bv_val[cred->bv_len];
513 void *old_authctx = NULL;
515 ldap_pvt_thread_pool_setkey( op->o_threadctx, (void *)slap_sasl_bind,
516 op->o_conn->c_sasl_authctx, 0, &old_authctx, NULL );
519 if ( credNul ) cred->bv_val[cred->bv_len] = 0;
521 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
522 /* if e is provided, check access */
523 if ( e && access_allowed( op, e, a->a_desc, bv,
524 ACL_AUTH, &acl_state ) == 0 )
529 if ( !lutil_passwd( bv, cred, NULL, text ) ) {
535 if ( credNul ) cred->bv_val[cred->bv_len] = credNul;
538 ldap_pvt_thread_pool_setkey( op->o_threadctx, (void *)slap_sasl_bind,
539 old_authctx, 0, NULL, NULL );
546 slap_passwd_generate( struct berval *pass )
548 Debug( LDAP_DEBUG_TRACE, "slap_passwd_generate\n", 0, 0, 0 );
552 * generate passwords of only 8 characters as some getpass(3)
553 * implementations truncate at 8 characters.
555 lutil_passwd_generate( pass, 8 );
559 slap_passwd_hash_type(
560 struct berval * cred,
568 assert( hash != NULL );
570 lutil_passwd_hash( cred , hash, new, text );
574 struct berval * cred,
579 if ( default_passwd_hash ) {
580 hash = default_passwd_hash[0];
583 hash = (char *)defhash[0];
586 slap_passwd_hash_type( cred, new, hash, text );
590 static ldap_pvt_thread_mutex_t passwd_mutex;
591 static lutil_cryptfunc slapd_crypt;
593 static int slapd_crypt( const char *key, const char *salt, char **hash )
598 ldap_pvt_thread_mutex_lock( &passwd_mutex );
600 cr = crypt( key, salt );
601 if ( cr == NULL || cr[0] == '\0' ) {
602 /* salt must have been invalid */
603 rc = LUTIL_PASSWD_ERR;
606 *hash = ber_strdup( cr );
607 rc = LUTIL_PASSWD_OK;
610 rc = strcmp( salt, cr ) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
614 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
617 #endif /* SLAPD_CRYPT */
619 void slap_passwd_init()
622 ldap_pvt_thread_mutex_init( &passwd_mutex );
623 lutil_cryptptr = slapd_crypt;