1 /* passwd.c - password extended operation routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2005 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 static const char *defhash[] = {
37 #ifdef LUTIL_SHA1_BYTES
49 struct berval id = {0, NULL}, hash, *rsp = NULL;
50 req_pwdexop_s *qpw = &op->oq_pwdexop;
51 req_extended_s qext = op->oq_extended;
53 slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
54 slap_callback cb2 = { NULL, slap_replog_cb, NULL, NULL };
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_val = NULL;
72 qpw->rs_new.bv_val = NULL;
74 qpw->rs_modtail = NULL;
76 rs->sr_err = slap_passwd_parse( op->ore_reqdata, &id,
77 &qpw->rs_old, &qpw->rs_new, &rs->sr_text );
79 if ( rs->sr_err == LDAP_SUCCESS && !BER_BVISEMPTY( &id ) ) {
80 Statslog( LDAP_DEBUG_STATS, "%s PASSMOD id=\"%s\"%s%s\n",
81 op->o_log_prefix, id.bv_val,
82 qpw->rs_old.bv_val ? " old" : "",
83 qpw->rs_new.bv_val ? " new" : "", 0 );
85 Statslog( LDAP_DEBUG_STATS, "%s PASSMOD %s%s\n",
87 qpw->rs_old.bv_val ? " old" : "",
88 qpw->rs_new.bv_val ? " new" : "", 0, 0 );
91 if ( rs->sr_err != LDAP_SUCCESS ) {
95 if ( !BER_BVISEMPTY( &id ) ) {
96 rs->sr_err = dnPrettyNormal( NULL, &id, &op->o_req_dn,
97 &op->o_req_ndn, op->o_tmpmemctx );
98 if ( rs->sr_err != LDAP_SUCCESS ) {
99 rs->sr_text = "Invalid DN";
103 op->o_bd = select_backend( &op->o_req_ndn, 0, 1 );
106 ber_dupbv_x( &op->o_req_dn, &op->o_dn, op->o_tmpmemctx );
107 ber_dupbv_x( &op->o_req_ndn, &op->o_ndn, op->o_tmpmemctx );
108 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
109 op->o_bd = op->o_conn->c_authz_backend;
110 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
113 if( op->o_bd == NULL ) {
114 if ( qpw->rs_old.bv_val != NULL ) {
115 rs->sr_text = "unwilling to verify old password";
116 rc = LDAP_UNWILLING_TO_PERFORM;
120 #ifdef HAVE_CYRUS_SASL
121 rc = slap_sasl_setpass( op, rs );
123 rs->sr_text = "no authz backend";
129 if ( op->o_req_ndn.bv_len == 0 ) {
130 rs->sr_text = "no password is associated with the Root DSE";
131 rc = LDAP_UNWILLING_TO_PERFORM;
135 /* If we've got a glued backend, check the real backend */
137 if ( SLAP_GLUE_INSTANCE( op->o_bd )) {
138 op->o_bd = select_backend( &op->o_req_ndn, 0, 0 );
141 if (backend_check_restrictions( op, rs,
142 (struct berval *)&slap_EXOP_MODIFY_PASSWD ) != LDAP_SUCCESS) {
147 /* check for referrals */
148 if ( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
153 #ifndef SLAPD_MULTIMASTER
154 /* This does not apply to multi-master case */
155 if(!( !SLAP_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;
177 #endif /* !SLAPD_MULTIMASTER */
179 /* generate a new password if none was provided */
180 if ( qpw->rs_new.bv_len == 0 ) {
181 slap_passwd_generate( &qpw->rs_new );
182 if ( qpw->rs_new.bv_len ) {
183 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 ) {
204 /* The backend didn't handle it, so try it here */
205 if( op->o_bd && !op->o_bd->be_modify ) {
206 rs->sr_text = "operation not supported for current user";
207 rc = LDAP_UNWILLING_TO_PERFORM;
211 if ( qpw->rs_old.bv_val != NULL ) {
214 rc = be_entry_get_rw( op, &op->o_req_ndn, NULL,
215 slap_schema.si_ad_userPassword, 0, &e );
216 if ( rc == LDAP_SUCCESS && e ) {
217 Attribute *a = attr_find( e->e_attrs,
218 slap_schema.si_ad_userPassword );
220 rc = slap_passwd_check( op, e, a, &qpw->rs_old, &rs->sr_text );
223 be_entry_release_r( op, e );
224 if ( rc == LDAP_SUCCESS )
227 rs->sr_text = "unwilling to verify old password";
228 rc = LDAP_UNWILLING_TO_PERFORM;
233 ml = ch_malloc( sizeof(Modifications) );
234 if ( !qpw->rs_modtail ) qpw->rs_modtail = &ml->sml_next;
236 if ( default_passwd_hash ) {
237 for ( nhash = 0; default_passwd_hash[nhash]; nhash++ );
238 hashes = default_passwd_hash;
241 hashes = (char **)defhash;
243 ml->sml_values = ch_malloc( (nhash+1)*sizeof(struct berval) );
244 for ( i=0; hashes[i]; i++ ) {
245 slap_passwd_hash_type( &qpw->rs_new, &hash, hashes[i], &rs->sr_text );
246 if ( hash.bv_len == 0 ) {
247 if ( !rs->sr_text ) {
248 rs->sr_text = "password hash failed";
252 ml->sml_values[i] = hash;
254 ml->sml_values[i].bv_val = NULL;
255 ml->sml_nvalues = NULL;
256 ml->sml_desc = slap_schema.si_ad_userPassword;
257 ml->sml_op = LDAP_MOD_REPLACE;
259 ml->sml_next = qpw->rs_mods;
263 rs->sr_err = LDAP_OTHER;
266 slap_callback *sc = op->o_callback;
268 op->o_tag = LDAP_REQ_MODIFY;
269 op->o_callback = &cb2;
270 op->orm_modlist = qpw->rs_mods;
271 cb2.sc_private = qpw; /* let Modify know this was pwdMod,
274 rs->sr_err = slap_mods_opattrs( op, ml, qpw->rs_modtail, &rs->sr_text,
277 if ( rs->sr_err == LDAP_SUCCESS ) {
278 rs->sr_err = op->o_bd->be_modify( op, rs );
280 if ( rs->sr_err == LDAP_SUCCESS ) {
281 rs->sr_rspdata = rsp;
285 op->o_tag = LDAP_REQ_EXTENDED;
288 slap_mods_free( qpw->rs_mods, 1 );
290 free( qpw->rs_new.bv_val );
294 op->oq_extended = qext;
297 if ( !BER_BVISNULL( &op->o_req_dn ) ) {
298 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
300 if ( !BER_BVISNULL( &op->o_req_ndn ) ) {
301 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
307 int slap_passwd_parse( struct berval *reqdata,
309 struct berval *oldpass,
310 struct berval *newpass,
313 int rc = LDAP_SUCCESS;
316 BerElementBuffer berbuf;
317 BerElement *ber = (BerElement *)&berbuf;
319 if( reqdata == NULL ) {
323 if( reqdata->bv_len == 0 ) {
324 *text = "empty request data field";
325 return LDAP_PROTOCOL_ERROR;
328 /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
329 ber_init2( ber, reqdata, 0 );
331 tag = ber_scanf( ber, "{" /*}*/ );
333 if( tag == LBER_ERROR ) {
334 Debug( LDAP_DEBUG_TRACE,
335 "slap_passwd_parse: decoding error\n", 0, 0, 0 );
336 rc = LDAP_PROTOCOL_ERROR;
340 tag = ber_peek_tag( ber, &len );
341 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_ID ) {
343 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID not allowed.\n",
346 *text = "user must change own password";
347 rc = LDAP_UNWILLING_TO_PERFORM;
351 tag = ber_scanf( ber, "m", id );
353 if( tag == LBER_ERROR ) {
354 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
360 tag = ber_peek_tag( ber, &len );
363 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_OLD ) {
364 if( oldpass == NULL ) {
365 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD not allowed.\n",
368 *text = "use bind to verify old password";
369 rc = LDAP_UNWILLING_TO_PERFORM;
373 tag = ber_scanf( ber, "m", oldpass );
375 if( tag == LBER_ERROR ) {
376 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD parse failed.\n",
382 if( oldpass->bv_len == 0 ) {
383 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD empty.\n",
386 *text = "old password value is empty";
387 rc = LDAP_UNWILLING_TO_PERFORM;
391 tag = ber_peek_tag( ber, &len );
394 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ) {
395 if( newpass == NULL ) {
396 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW not allowed.\n",
399 *text = "user specified passwords disallowed";
400 rc = LDAP_UNWILLING_TO_PERFORM;
404 tag = ber_scanf( ber, "m", newpass );
406 if( tag == LBER_ERROR ) {
407 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW parse failed.\n",
413 if( newpass->bv_len == 0 ) {
414 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW empty.\n",
417 *text = "new password value is empty";
418 rc = LDAP_UNWILLING_TO_PERFORM;
422 tag = ber_peek_tag( ber, &len );
427 Debug( LDAP_DEBUG_TRACE,
428 "slap_passwd_parse: decoding error, len=%ld\n",
431 *text = "data decoding error";
432 rc = LDAP_PROTOCOL_ERROR;
439 struct berval * slap_passwd_return(
440 struct berval *cred )
443 struct berval *bv = NULL;
444 BerElementBuffer berbuf;
445 /* opaque structure, size unknown but smaller than berbuf */
446 BerElement *ber = (BerElement *)&berbuf;
448 assert( cred != NULL );
450 Debug( LDAP_DEBUG_TRACE, "slap_passwd_return: %ld\n",
451 (long) cred->bv_len, 0, 0 );
453 ber_init_w_nullc( ber, LBER_USE_DER );
455 rc = ber_printf( ber, "{tON}",
456 LDAP_TAG_EXOP_MODIFY_PASSWD_GEN, cred );
459 (void) ber_flatten( ber, &bv );
468 * if "e" is provided, access to each value of the password is checked first
480 AccessControlState acl_state = ACL_STATE_INIT;
483 ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind,
484 op->o_conn->c_sasl_authctx, NULL );
487 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
488 /* if e is provided, check access */
489 if ( e && access_allowed( op, e, a->a_desc, bv,
490 ACL_AUTH, &acl_state ) == 0 )
495 if ( !lutil_passwd( bv, cred, NULL, text ) ) {
502 ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind,
510 slap_passwd_generate( struct berval *pass )
512 Debug( LDAP_DEBUG_TRACE, "slap_passwd_generate\n", 0, 0, 0 );
517 * generate passwords of only 8 characters as some getpass(3)
518 * implementations truncate at 8 characters.
520 lutil_passwd_generate( pass, 8 );
524 slap_passwd_hash_type(
525 struct berval * cred,
533 assert( hash != NULL );
535 lutil_passwd_hash( cred , hash, new, text );
539 struct berval * cred,
544 if ( default_passwd_hash ) {
545 hash = default_passwd_hash[0];
548 hash = (char *)defhash[0];
551 slap_passwd_hash_type( cred, new, hash, text );
555 static ldap_pvt_thread_mutex_t passwd_mutex;
556 static lutil_cryptfunc slapd_crypt;
558 static int slapd_crypt( const char *key, const char *salt, char **hash )
563 ldap_pvt_thread_mutex_lock( &passwd_mutex );
565 cr = crypt( key, salt );
566 if ( cr == NULL || cr[0] == '\0' ) {
567 /* salt must have been invalid */
568 rc = LUTIL_PASSWD_ERR;
571 *hash = ber_strdup( cr );
572 rc = LUTIL_PASSWD_OK;
575 rc = strcmp( salt, cr ) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
579 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
582 #endif /* SLAPD_CRYPT */
584 void slap_passwd_init()
587 ldap_pvt_thread_mutex_init( &passwd_mutex );
588 lutil_cryptptr = slapd_crypt;