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 );
286 /* be_modify() might have shuffled modifications */
287 qpw->rs_mods = op->orm_modlist;
289 if ( rs->sr_err == LDAP_SUCCESS ) {
290 rs->sr_rspdata = rsp;
296 op->o_tag = LDAP_REQ_EXTENDED;
301 op->oq_extended = qext;
304 if ( qpw->rs_mods ) {
305 slap_mods_free( qpw->rs_mods, 1 );
308 free( qpw->rs_new.bv_val );
310 if ( !BER_BVISNULL( &op->o_req_dn ) ) {
311 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
312 BER_BVZERO( &op->o_req_dn );
314 if ( !BER_BVISNULL( &op->o_req_ndn ) ) {
315 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
316 BER_BVZERO( &op->o_req_ndn );
322 int slap_passwd_parse( struct berval *reqdata,
324 struct berval *oldpass,
325 struct berval *newpass,
328 int rc = LDAP_SUCCESS;
331 BerElementBuffer berbuf;
332 BerElement *ber = (BerElement *)&berbuf;
334 if( reqdata == NULL ) {
338 if( reqdata->bv_len == 0 ) {
339 *text = "empty request data field";
340 return LDAP_PROTOCOL_ERROR;
343 /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
344 ber_init2( ber, reqdata, 0 );
346 tag = ber_scanf( ber, "{" /*}*/ );
348 if( tag == LBER_ERROR ) {
349 Debug( LDAP_DEBUG_TRACE,
350 "slap_passwd_parse: decoding error\n", 0, 0, 0 );
351 rc = LDAP_PROTOCOL_ERROR;
355 tag = ber_peek_tag( ber, &len );
356 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_ID ) {
358 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID not allowed.\n",
361 *text = "user must change own password";
362 rc = LDAP_UNWILLING_TO_PERFORM;
366 tag = ber_scanf( ber, "m", id );
368 if( tag == LBER_ERROR ) {
369 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
375 tag = ber_peek_tag( ber, &len );
378 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_OLD ) {
379 if( oldpass == NULL ) {
380 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD not allowed.\n",
383 *text = "use bind to verify old password";
384 rc = LDAP_UNWILLING_TO_PERFORM;
388 tag = ber_scanf( ber, "m", oldpass );
390 if( tag == LBER_ERROR ) {
391 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD parse failed.\n",
397 if( oldpass->bv_len == 0 ) {
398 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD empty.\n",
401 *text = "old password value is empty";
402 rc = LDAP_UNWILLING_TO_PERFORM;
406 tag = ber_peek_tag( ber, &len );
409 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ) {
410 if( newpass == NULL ) {
411 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW not allowed.\n",
414 *text = "user specified passwords disallowed";
415 rc = LDAP_UNWILLING_TO_PERFORM;
419 tag = ber_scanf( ber, "m", newpass );
421 if( tag == LBER_ERROR ) {
422 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW parse failed.\n",
428 if( newpass->bv_len == 0 ) {
429 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW empty.\n",
432 *text = "new password value is empty";
433 rc = LDAP_UNWILLING_TO_PERFORM;
437 tag = ber_peek_tag( ber, &len );
442 Debug( LDAP_DEBUG_TRACE,
443 "slap_passwd_parse: decoding error, len=%ld\n",
446 *text = "data decoding error";
447 rc = LDAP_PROTOCOL_ERROR;
454 struct berval * slap_passwd_return(
455 struct berval *cred )
458 struct berval *bv = NULL;
459 BerElementBuffer berbuf;
460 /* opaque structure, size unknown but smaller than berbuf */
461 BerElement *ber = (BerElement *)&berbuf;
463 assert( cred != NULL );
465 Debug( LDAP_DEBUG_TRACE, "slap_passwd_return: %ld\n",
466 (long) cred->bv_len, 0, 0 );
468 ber_init_w_nullc( ber, LBER_USE_DER );
470 rc = ber_printf( ber, "{tON}",
471 LDAP_TAG_EXOP_MODIFY_PASSWD_GEN, cred );
474 (void) ber_flatten( ber, &bv );
483 * if "e" is provided, access to each value of the password is checked first
495 AccessControlState acl_state = ACL_STATE_INIT;
498 ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind,
499 op->o_conn->c_sasl_authctx, NULL );
502 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
503 /* if e is provided, check access */
504 if ( e && access_allowed( op, e, a->a_desc, bv,
505 ACL_AUTH, &acl_state ) == 0 )
510 if ( !lutil_passwd( bv, cred, NULL, text ) ) {
517 ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind,
525 slap_passwd_generate( struct berval *pass )
527 Debug( LDAP_DEBUG_TRACE, "slap_passwd_generate\n", 0, 0, 0 );
531 * generate passwords of only 8 characters as some getpass(3)
532 * implementations truncate at 8 characters.
534 lutil_passwd_generate( pass, 8 );
538 slap_passwd_hash_type(
539 struct berval * cred,
547 assert( hash != NULL );
549 lutil_passwd_hash( cred , hash, new, text );
553 struct berval * cred,
558 if ( default_passwd_hash ) {
559 hash = default_passwd_hash[0];
562 hash = (char *)defhash[0];
565 slap_passwd_hash_type( cred, new, hash, text );
569 static ldap_pvt_thread_mutex_t passwd_mutex;
570 static lutil_cryptfunc slapd_crypt;
572 static int slapd_crypt( const char *key, const char *salt, char **hash )
577 ldap_pvt_thread_mutex_lock( &passwd_mutex );
579 cr = crypt( key, salt );
580 if ( cr == NULL || cr[0] == '\0' ) {
581 /* salt must have been invalid */
582 rc = LUTIL_PASSWD_ERR;
585 *hash = ber_strdup( cr );
586 rc = LUTIL_PASSWD_OK;
589 rc = strcmp( salt, cr ) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
593 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
596 #endif /* SLAPD_CRYPT */
598 void slap_passwd_init()
601 ldap_pvt_thread_mutex_init( &passwd_mutex );
602 lutil_cryptptr = slapd_crypt;