1 /* passwd.c - password extended operation routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2007 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 };
61 assert( ber_bvcmp( &slap_EXOP_MODIFY_PASSWD, &op->ore_reqoid ) == 0 );
63 if( op->o_dn.bv_len == 0 ) {
64 Statslog( LDAP_DEBUG_STATS, "%s PASSMOD\n",
65 op->o_log_prefix, 0, 0, 0, 0 );
66 rs->sr_text = "only authenticated users may change passwords";
67 return LDAP_STRONG_AUTH_REQUIRED;
70 qpw->rs_old.bv_len = 0;
71 qpw->rs_old.bv_val = NULL;
72 qpw->rs_new.bv_len = 0;
73 qpw->rs_new.bv_val = NULL;
75 qpw->rs_modtail = NULL;
77 rs->sr_err = slap_passwd_parse( op->ore_reqdata, &id,
78 &qpw->rs_old, &qpw->rs_new, &rs->sr_text );
80 if ( rs->sr_err == LDAP_SUCCESS && !BER_BVISEMPTY( &id ) ) {
81 Statslog( LDAP_DEBUG_STATS, "%s PASSMOD id=\"%s\"%s%s\n",
82 op->o_log_prefix, id.bv_val,
83 qpw->rs_old.bv_val ? " old" : "",
84 qpw->rs_new.bv_val ? " new" : "", 0 );
86 Statslog( LDAP_DEBUG_STATS, "%s PASSMOD%s%s\n",
88 qpw->rs_old.bv_val ? " old" : "",
89 qpw->rs_new.bv_val ? " new" : "", 0, 0 );
92 if ( rs->sr_err != LDAP_SUCCESS ) {
96 if ( !BER_BVISEMPTY( &id ) ) {
97 rs->sr_err = dnPrettyNormal( NULL, &id, &op->o_req_dn,
98 &op->o_req_ndn, op->o_tmpmemctx );
99 if ( rs->sr_err != LDAP_SUCCESS ) {
100 rs->sr_text = "Invalid DN";
104 op->o_bd = select_backend( &op->o_req_ndn, 0, 1 );
107 ber_dupbv_x( &op->o_req_dn, &op->o_dn, op->o_tmpmemctx );
108 ber_dupbv_x( &op->o_req_ndn, &op->o_ndn, op->o_tmpmemctx );
109 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
110 op->o_bd = op->o_conn->c_authz_backend;
111 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
114 if( op->o_bd == NULL ) {
115 if ( qpw->rs_old.bv_val != NULL ) {
116 rs->sr_text = "unwilling to verify old password";
117 rc = LDAP_UNWILLING_TO_PERFORM;
121 #ifdef HAVE_CYRUS_SASL
122 rc = slap_sasl_setpass( op, rs );
124 rs->sr_text = "no authz backend";
130 if ( op->o_req_ndn.bv_len == 0 ) {
131 rs->sr_text = "no password is associated with the Root DSE";
132 rc = LDAP_UNWILLING_TO_PERFORM;
136 /* If we've got a glued backend, check the real backend */
138 if ( SLAP_GLUE_INSTANCE( op->o_bd )) {
139 op->o_bd = select_backend( &op->o_req_ndn, 0, 0 );
142 if (backend_check_restrictions( op, rs,
143 (struct berval *)&slap_EXOP_MODIFY_PASSWD ) != LDAP_SUCCESS) {
148 /* check for referrals */
149 if ( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
154 /* This does not apply to multi-master case */
155 if(!( !SLAP_SINGLE_SHADOW( op->o_bd ) || be_isupdate( op ))) {
156 /* we SHOULD return a referral in this case */
157 BerVarray defref = op->o_bd->be_update_refs
158 ? op->o_bd->be_update_refs : default_referral;
160 if( defref != NULL ) {
161 rs->sr_ref = referral_rewrite( op->o_bd->be_update_refs,
162 NULL, NULL, LDAP_SCOPE_DEFAULT );
164 rs->sr_flags |= REP_REF_MUSTBEFREED;
173 rs->sr_text = "shadow context; no update referral";
174 rc = LDAP_UNWILLING_TO_PERFORM;
178 /* generate a new password if none was provided */
179 if ( qpw->rs_new.bv_len == 0 ) {
180 slap_passwd_generate( &qpw->rs_new );
181 if ( qpw->rs_new.bv_len ) {
182 rsp = slap_passwd_return( &qpw->rs_new );
186 if ( qpw->rs_new.bv_len == 0 ) {
187 rs->sr_text = "password generation failed";
194 /* Give the backend a chance to handle this itself */
195 if ( op->o_bd->be_extended ) {
196 rs->sr_err = op->o_bd->be_extended( op, rs );
197 if ( rs->sr_err != LDAP_UNWILLING_TO_PERFORM &&
198 rs->sr_err != SLAP_CB_CONTINUE )
202 rs->sr_rspdata = rsp;
209 /* The backend didn't handle it, so try it here */
210 if( op->o_bd && !op->o_bd->be_modify ) {
211 rs->sr_text = "operation not supported for current user";
212 rc = LDAP_UNWILLING_TO_PERFORM;
216 if ( qpw->rs_old.bv_val != NULL ) {
219 rc = be_entry_get_rw( op, &op->o_req_ndn, NULL,
220 slap_schema.si_ad_userPassword, 0, &e );
221 if ( rc == LDAP_SUCCESS && e ) {
222 Attribute *a = attr_find( e->e_attrs,
223 slap_schema.si_ad_userPassword );
225 rc = slap_passwd_check( op, e, a, &qpw->rs_old, &rs->sr_text );
228 be_entry_release_r( op, e );
229 if ( rc == LDAP_SUCCESS )
232 rs->sr_text = "unwilling to verify old password";
233 rc = LDAP_UNWILLING_TO_PERFORM;
238 ml = ch_malloc( sizeof(Modifications) );
239 if ( !qpw->rs_modtail ) qpw->rs_modtail = &ml->sml_next;
241 if ( default_passwd_hash ) {
242 for ( nhash = 0; default_passwd_hash[nhash]; nhash++ );
243 hashes = default_passwd_hash;
246 hashes = (char **)defhash;
248 ml->sml_values = ch_malloc( (nhash+1)*sizeof(struct berval) );
249 for ( i=0; hashes[i]; i++ ) {
250 slap_passwd_hash_type( &qpw->rs_new, &hash, hashes[i], &rs->sr_text );
251 if ( hash.bv_len == 0 ) {
252 if ( !rs->sr_text ) {
253 rs->sr_text = "password hash failed";
257 ml->sml_values[i] = hash;
259 ml->sml_values[i].bv_val = NULL;
260 ml->sml_nvalues = NULL;
261 ml->sml_desc = slap_schema.si_ad_userPassword;
262 ml->sml_type = ml->sml_desc->ad_cname;
263 ml->sml_op = LDAP_MOD_REPLACE;
265 ml->sml_next = qpw->rs_mods;
269 rs->sr_err = LDAP_OTHER;
272 slap_callback *sc = op->o_callback;
274 op->o_tag = LDAP_REQ_MODIFY;
275 op->o_callback = &cb;
276 op->orm_modlist = qpw->rs_mods;
277 cb.sc_private = qpw; /* let Modify know this was pwdMod,
280 rs->sr_err = op->o_bd->be_modify( op, rs );
282 /* be_modify() might have shuffled modifications */
283 qpw->rs_mods = op->orm_modlist;
285 if ( rs->sr_err == LDAP_SUCCESS ) {
286 rs->sr_rspdata = rsp;
292 op->o_tag = LDAP_REQ_EXTENDED;
297 op->oq_extended = qext;
300 if ( qpw->rs_mods ) {
301 slap_mods_free( qpw->rs_mods, 1 );
304 free( qpw->rs_new.bv_val );
306 if ( !BER_BVISNULL( &op->o_req_dn ) ) {
307 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
308 BER_BVZERO( &op->o_req_dn );
310 if ( !BER_BVISNULL( &op->o_req_ndn ) ) {
311 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
312 BER_BVZERO( &op->o_req_ndn );
318 int slap_passwd_parse( struct berval *reqdata,
320 struct berval *oldpass,
321 struct berval *newpass,
324 int rc = LDAP_SUCCESS;
327 BerElementBuffer berbuf;
328 BerElement *ber = (BerElement *)&berbuf;
330 if( reqdata == NULL ) {
334 if( reqdata->bv_len == 0 ) {
335 *text = "empty request data field";
336 return LDAP_PROTOCOL_ERROR;
339 /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
340 ber_init2( ber, reqdata, 0 );
342 tag = ber_scanf( ber, "{" /*}*/ );
344 if( tag == LBER_ERROR ) {
345 Debug( LDAP_DEBUG_TRACE,
346 "slap_passwd_parse: decoding error\n", 0, 0, 0 );
347 rc = LDAP_PROTOCOL_ERROR;
351 tag = ber_peek_tag( ber, &len );
352 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_ID ) {
354 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID not allowed.\n",
357 *text = "user must change own password";
358 rc = LDAP_UNWILLING_TO_PERFORM;
362 tag = ber_scanf( ber, "m", id );
364 if( tag == LBER_ERROR ) {
365 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
371 tag = ber_peek_tag( ber, &len );
374 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_OLD ) {
375 if( oldpass == NULL ) {
376 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD not allowed.\n",
379 *text = "use bind to verify old password";
380 rc = LDAP_UNWILLING_TO_PERFORM;
384 tag = ber_scanf( ber, "m", oldpass );
386 if( tag == LBER_ERROR ) {
387 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD parse failed.\n",
393 if( oldpass->bv_len == 0 ) {
394 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD empty.\n",
397 *text = "old password value is empty";
398 rc = LDAP_UNWILLING_TO_PERFORM;
402 tag = ber_peek_tag( ber, &len );
405 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ) {
406 if( newpass == NULL ) {
407 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW not allowed.\n",
410 *text = "user specified passwords disallowed";
411 rc = LDAP_UNWILLING_TO_PERFORM;
415 tag = ber_scanf( ber, "m", newpass );
417 if( tag == LBER_ERROR ) {
418 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW parse failed.\n",
424 if( newpass->bv_len == 0 ) {
425 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW empty.\n",
428 *text = "new password value is empty";
429 rc = LDAP_UNWILLING_TO_PERFORM;
433 tag = ber_peek_tag( ber, &len );
438 Debug( LDAP_DEBUG_TRACE,
439 "slap_passwd_parse: decoding error, len=%ld\n",
442 *text = "data decoding error";
443 rc = LDAP_PROTOCOL_ERROR;
450 struct berval * slap_passwd_return(
451 struct berval *cred )
454 struct berval *bv = NULL;
455 BerElementBuffer berbuf;
456 /* opaque structure, size unknown but smaller than berbuf */
457 BerElement *ber = (BerElement *)&berbuf;
459 assert( cred != NULL );
461 Debug( LDAP_DEBUG_TRACE, "slap_passwd_return: %ld\n",
462 (long) cred->bv_len, 0, 0 );
464 ber_init_w_nullc( ber, LBER_USE_DER );
466 rc = ber_printf( ber, "{tON}",
467 LDAP_TAG_EXOP_MODIFY_PASSWD_GEN, cred );
470 (void) ber_flatten( ber, &bv );
479 * if "e" is provided, access to each value of the password is checked first
491 AccessControlState acl_state = ACL_STATE_INIT;
494 ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind,
495 op->o_conn->c_sasl_authctx, NULL );
498 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
499 /* if e is provided, check access */
500 if ( e && access_allowed( op, e, a->a_desc, bv,
501 ACL_AUTH, &acl_state ) == 0 )
506 if ( !lutil_passwd( bv, cred, NULL, text ) ) {
513 ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind,
521 slap_passwd_generate( struct berval *pass )
523 Debug( LDAP_DEBUG_TRACE, "slap_passwd_generate\n", 0, 0, 0 );
527 * generate passwords of only 8 characters as some getpass(3)
528 * implementations truncate at 8 characters.
530 lutil_passwd_generate( pass, 8 );
534 slap_passwd_hash_type(
535 struct berval * cred,
543 assert( hash != NULL );
545 lutil_passwd_hash( cred , hash, new, text );
549 struct berval * cred,
554 if ( default_passwd_hash ) {
555 hash = default_passwd_hash[0];
558 hash = (char *)defhash[0];
561 slap_passwd_hash_type( cred, new, hash, text );
565 static ldap_pvt_thread_mutex_t passwd_mutex;
566 static lutil_cryptfunc slapd_crypt;
568 static int slapd_crypt( const char *key, const char *salt, char **hash )
573 ldap_pvt_thread_mutex_lock( &passwd_mutex );
575 cr = crypt( key, salt );
576 if ( cr == NULL || cr[0] == '\0' ) {
577 /* salt must have been invalid */
578 rc = LUTIL_PASSWD_ERR;
581 *hash = ber_strdup( cr );
582 rc = LUTIL_PASSWD_OK;
585 rc = strcmp( salt, cr ) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
589 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
592 #endif /* SLAPD_CRYPT */
594 void slap_passwd_init()
597 ldap_pvt_thread_mutex_init( &passwd_mutex );
598 lutil_cryptptr = slapd_crypt;