1 /* passwd.c - password extended operation routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2006 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>.
22 #include <ac/socket.h>
23 #include <ac/string.h>
24 #include <ac/unistd.h>
34 #include <lutil_sha1.h>
36 const struct berval slap_EXOP_MODIFY_PASSWD = BER_BVC(LDAP_EXOP_MODIFY_PASSWD);
38 static const char *defhash[] = {
39 #ifdef LUTIL_SHA1_BYTES
51 struct berval id = {0, NULL}, hash, *rsp = NULL;
52 req_pwdexop_s *qpw = &op->oq_pwdexop;
53 req_extended_s qext = op->oq_extended;
55 slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
56 slap_callback cb2 = { NULL, slap_replog_cb, NULL, NULL };
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 ( rs->sr_err == LDAP_SUCCESS && !BER_BVISEMPTY( &id ) ) {
85 Statslog( LDAP_DEBUG_STATS, "%s PASSMOD id=\"%s\"%s%s\n",
86 op->o_log_prefix, id.bv_val,
87 qpw->rs_old.bv_val ? " old" : "",
88 qpw->rs_new.bv_val ? " new" : "", 0 );
90 Statslog( LDAP_DEBUG_STATS, "%s PASSMOD%s%s\n",
92 qpw->rs_old.bv_val ? " old" : "",
93 qpw->rs_new.bv_val ? " new" : "", 0, 0 );
96 if ( rs->sr_err != LDAP_SUCCESS ) {
100 if ( !BER_BVISEMPTY( &id ) ) {
101 rs->sr_err = dnPrettyNormal( NULL, &id, &op->o_req_dn,
102 &op->o_req_ndn, op->o_tmpmemctx );
103 if ( rs->sr_err != LDAP_SUCCESS ) {
104 rs->sr_text = "Invalid DN";
108 op->o_bd = select_backend( &op->o_req_ndn, 0, 1 );
111 ber_dupbv_x( &op->o_req_dn, &op->o_dn, op->o_tmpmemctx );
112 ber_dupbv_x( &op->o_req_ndn, &op->o_ndn, op->o_tmpmemctx );
113 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
114 op->o_bd = op->o_conn->c_authz_backend;
115 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
118 if( op->o_bd == NULL ) {
119 if ( qpw->rs_old.bv_val != NULL ) {
120 rs->sr_text = "unwilling to verify old password";
121 rc = LDAP_UNWILLING_TO_PERFORM;
125 #ifdef HAVE_CYRUS_SASL
126 rc = slap_sasl_setpass( op, rs );
128 rs->sr_text = "no authz backend";
134 if ( op->o_req_ndn.bv_len == 0 ) {
135 rs->sr_text = "no password is associated with the Root DSE";
136 rc = LDAP_UNWILLING_TO_PERFORM;
140 /* If we've got a glued backend, check the real backend */
142 if ( SLAP_GLUE_INSTANCE( op->o_bd )) {
143 op->o_bd = select_backend( &op->o_req_ndn, 0, 0 );
146 if (backend_check_restrictions( op, rs,
147 (struct berval *)&slap_EXOP_MODIFY_PASSWD ) != LDAP_SUCCESS) {
152 /* check for referrals */
153 if ( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
158 /* This does not apply to multi-master case */
159 if(!( !SLAP_SINGLE_SHADOW( op->o_bd ) || be_isupdate( op ))) {
160 /* we SHOULD return a referral in this case */
161 BerVarray defref = op->o_bd->be_update_refs
162 ? op->o_bd->be_update_refs : default_referral;
164 if( defref != NULL ) {
165 rs->sr_ref = referral_rewrite( op->o_bd->be_update_refs,
166 NULL, NULL, LDAP_SCOPE_DEFAULT );
168 rs->sr_flags |= REP_REF_MUSTBEFREED;
177 rs->sr_text = "shadow context; no update referral";
178 rc = LDAP_UNWILLING_TO_PERFORM;
182 /* generate a new password if none was provided */
183 if ( qpw->rs_new.bv_len == 0 ) {
184 slap_passwd_generate( &qpw->rs_new );
185 if ( qpw->rs_new.bv_len ) {
186 rsp = slap_passwd_return( &qpw->rs_new );
190 if ( qpw->rs_new.bv_len == 0 ) {
191 rs->sr_text = "password generation failed";
198 /* Give the backend a chance to handle this itself */
199 if ( op->o_bd->be_extended ) {
200 rs->sr_err = op->o_bd->be_extended( op, rs );
201 if ( rs->sr_err != LDAP_UNWILLING_TO_PERFORM &&
202 rs->sr_err != SLAP_CB_CONTINUE )
206 rs->sr_rspdata = rsp;
213 /* The backend didn't handle it, so try it here */
214 if( op->o_bd && !op->o_bd->be_modify ) {
215 rs->sr_text = "operation not supported for current user";
216 rc = LDAP_UNWILLING_TO_PERFORM;
220 if ( qpw->rs_old.bv_val != NULL ) {
223 rc = be_entry_get_rw( op, &op->o_req_ndn, NULL,
224 slap_schema.si_ad_userPassword, 0, &e );
225 if ( rc == LDAP_SUCCESS && e ) {
226 Attribute *a = attr_find( e->e_attrs,
227 slap_schema.si_ad_userPassword );
229 rc = slap_passwd_check( op, e, a, &qpw->rs_old, &rs->sr_text );
232 be_entry_release_r( op, e );
233 if ( rc == LDAP_SUCCESS )
236 rs->sr_text = "unwilling to verify old password";
237 rc = LDAP_UNWILLING_TO_PERFORM;
242 ml = ch_malloc( sizeof(Modifications) );
243 if ( !qpw->rs_modtail ) qpw->rs_modtail = &ml->sml_next;
245 if ( default_passwd_hash ) {
246 for ( nhash = 0; default_passwd_hash[nhash]; nhash++ );
247 hashes = default_passwd_hash;
250 hashes = (char **)defhash;
252 ml->sml_values = ch_malloc( (nhash+1)*sizeof(struct berval) );
253 for ( i=0; hashes[i]; i++ ) {
254 slap_passwd_hash_type( &qpw->rs_new, &hash, hashes[i], &rs->sr_text );
255 if ( hash.bv_len == 0 ) {
256 if ( !rs->sr_text ) {
257 rs->sr_text = "password hash failed";
261 ml->sml_values[i] = hash;
263 ml->sml_values[i].bv_val = NULL;
264 ml->sml_nvalues = NULL;
265 ml->sml_desc = slap_schema.si_ad_userPassword;
266 ml->sml_type = ml->sml_desc->ad_cname;
267 ml->sml_op = LDAP_MOD_REPLACE;
269 ml->sml_next = qpw->rs_mods;
273 rs->sr_err = LDAP_OTHER;
276 slap_callback *sc = op->o_callback;
278 op->o_tag = LDAP_REQ_MODIFY;
279 op->o_callback = &cb2;
280 op->orm_modlist = qpw->rs_mods;
281 cb2.sc_private = qpw; /* let Modify know this was pwdMod,
284 rs->sr_err = op->o_bd->be_modify( op, rs );
285 if ( rs->sr_err == LDAP_SUCCESS ) {
286 rs->sr_rspdata = rsp;
291 op->o_tag = LDAP_REQ_EXTENDED;
296 op->oq_extended = qext;
299 if ( qpw->rs_mods ) {
300 slap_mods_free( qpw->rs_mods, 1 );
303 free( qpw->rs_new.bv_val );
305 if ( !BER_BVISNULL( &op->o_req_dn ) ) {
306 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
307 BER_BVZERO( &op->o_req_dn );
309 if ( !BER_BVISNULL( &op->o_req_ndn ) ) {
310 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
311 BER_BVZERO( &op->o_req_ndn );
317 int slap_passwd_parse( struct berval *reqdata,
319 struct berval *oldpass,
320 struct berval *newpass,
323 int rc = LDAP_SUCCESS;
326 BerElementBuffer berbuf;
327 BerElement *ber = (BerElement *)&berbuf;
329 if( reqdata == NULL ) {
333 if( reqdata->bv_len == 0 ) {
334 *text = "empty request data field";
335 return LDAP_PROTOCOL_ERROR;
338 /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
339 ber_init2( ber, reqdata, 0 );
341 tag = ber_scanf( ber, "{" /*}*/ );
343 if( tag == LBER_ERROR ) {
344 Debug( LDAP_DEBUG_TRACE,
345 "slap_passwd_parse: decoding error\n", 0, 0, 0 );
346 rc = LDAP_PROTOCOL_ERROR;
350 tag = ber_peek_tag( ber, &len );
351 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_ID ) {
353 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID not allowed.\n",
356 *text = "user must change own password";
357 rc = LDAP_UNWILLING_TO_PERFORM;
361 tag = ber_scanf( ber, "m", id );
363 if( tag == LBER_ERROR ) {
364 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
370 tag = ber_peek_tag( ber, &len );
373 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_OLD ) {
374 if( oldpass == NULL ) {
375 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD not allowed.\n",
378 *text = "use bind to verify old password";
379 rc = LDAP_UNWILLING_TO_PERFORM;
383 tag = ber_scanf( ber, "m", oldpass );
385 if( tag == LBER_ERROR ) {
386 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD parse failed.\n",
392 if( oldpass->bv_len == 0 ) {
393 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD empty.\n",
396 *text = "old password value is empty";
397 rc = LDAP_UNWILLING_TO_PERFORM;
401 tag = ber_peek_tag( ber, &len );
404 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ) {
405 if( newpass == NULL ) {
406 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW not allowed.\n",
409 *text = "user specified passwords disallowed";
410 rc = LDAP_UNWILLING_TO_PERFORM;
414 tag = ber_scanf( ber, "m", newpass );
416 if( tag == LBER_ERROR ) {
417 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW parse failed.\n",
423 if( newpass->bv_len == 0 ) {
424 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW empty.\n",
427 *text = "new password value is empty";
428 rc = LDAP_UNWILLING_TO_PERFORM;
432 tag = ber_peek_tag( ber, &len );
437 Debug( LDAP_DEBUG_TRACE,
438 "slap_passwd_parse: decoding error, len=%ld\n",
441 *text = "data decoding error";
442 rc = LDAP_PROTOCOL_ERROR;
449 struct berval * slap_passwd_return(
450 struct berval *cred )
453 struct berval *bv = NULL;
454 BerElementBuffer berbuf;
455 /* opaque structure, size unknown but smaller than berbuf */
456 BerElement *ber = (BerElement *)&berbuf;
458 assert( cred != NULL );
460 Debug( LDAP_DEBUG_TRACE, "slap_passwd_return: %ld\n",
461 (long) cred->bv_len, 0, 0 );
463 ber_init_w_nullc( ber, LBER_USE_DER );
465 rc = ber_printf( ber, "{tON}",
466 LDAP_TAG_EXOP_MODIFY_PASSWD_GEN, cred );
469 (void) ber_flatten( ber, &bv );
478 * if "e" is provided, access to each value of the password is checked first
490 AccessControlState acl_state = ACL_STATE_INIT;
493 ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind,
494 op->o_conn->c_sasl_authctx, NULL );
497 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
498 /* if e is provided, check access */
499 if ( e && access_allowed( op, e, a->a_desc, bv,
500 ACL_AUTH, &acl_state ) == 0 )
505 if ( !lutil_passwd( bv, cred, NULL, text ) ) {
512 ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind,
520 slap_passwd_generate( struct berval *pass )
522 Debug( LDAP_DEBUG_TRACE, "slap_passwd_generate\n", 0, 0, 0 );
526 * generate passwords of only 8 characters as some getpass(3)
527 * implementations truncate at 8 characters.
529 lutil_passwd_generate( pass, 8 );
533 slap_passwd_hash_type(
534 struct berval * cred,
542 assert( hash != NULL );
544 lutil_passwd_hash( cred , hash, new, text );
548 struct berval * cred,
553 if ( default_passwd_hash ) {
554 hash = default_passwd_hash[0];
557 hash = (char *)defhash[0];
560 slap_passwd_hash_type( cred, new, hash, text );
564 static ldap_pvt_thread_mutex_t passwd_mutex;
565 static lutil_cryptfunc slapd_crypt;
567 static int slapd_crypt( const char *key, const char *salt, char **hash )
572 ldap_pvt_thread_mutex_lock( &passwd_mutex );
574 cr = crypt( key, salt );
575 if ( cr == NULL || cr[0] == '\0' ) {
576 /* salt must have been invalid */
577 rc = LUTIL_PASSWD_ERR;
580 *hash = ber_strdup( cr );
581 rc = LUTIL_PASSWD_OK;
584 rc = strcmp( salt, cr ) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
588 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
591 #endif /* SLAPD_CRYPT */
593 void slap_passwd_init()
596 ldap_pvt_thread_mutex_init( &passwd_mutex );
597 lutil_cryptptr = slapd_crypt;