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>
31 static const char *defhash[] = {
32 #ifdef LUTIL_SHA1_BYTES
44 struct berval id = {0, NULL}, hash, *rsp = NULL;
45 req_pwdexop_s *qpw = &op->oq_pwdexop;
48 slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
49 slap_callback cb2 = { NULL, slap_replog_cb, NULL, NULL };
56 assert( ber_bvcmp( &slap_EXOP_MODIFY_PASSWD, &op->ore_reqoid ) == 0 );
58 if( op->o_dn.bv_len == 0 ) {
59 rs->sr_text = "only authenticated users may change passwords";
60 return LDAP_STRONG_AUTH_REQUIRED;
63 qpw->rs_old.bv_val = NULL;
64 qpw->rs_new.bv_val = NULL;
66 qpw->rs_modtail = NULL;
68 rs->sr_err = slap_passwd_parse( op->ore_reqdata, &id, &qpw->rs_old,
69 &qpw->rs_new, &rs->sr_text );
71 if ( rs->sr_err != LDAP_SUCCESS ) {
76 ber_dupbv_x( &op->o_req_dn, &id, op->o_tmpmemctx );
77 /* ndn is in tmpmem, so we don't need to free it */
78 rs->sr_err = dnNormalize( 0, NULL, NULL, &id, &op->o_req_ndn, op->o_tmpmemctx );
79 if ( rs->sr_err != LDAP_SUCCESS ) {
80 rs->sr_text = "Invalid DN";
84 op->o_bd = select_backend( &op->o_req_ndn, 0, 1 );
87 ber_dupbv_x( &op->o_req_dn, &op->o_dn, op->o_tmpmemctx );
88 ber_dupbv_x( &op->o_req_ndn, &op->o_ndn, op->o_tmpmemctx );
89 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
90 op->o_bd = op->o_conn->c_authz_backend;
91 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
94 if( op->o_bd == NULL ) {
95 #ifdef HAVE_CYRUS_SASL
96 rc = slap_sasl_setpass( op, rs );
98 rs->sr_text = "no authz backend";
104 if ( op->o_req_ndn.bv_len == 0 ) {
105 rs->sr_text = "no password is associated with the Root DSE";
106 rc = LDAP_UNWILLING_TO_PERFORM;
110 if (backend_check_restrictions( op, rs,
111 (struct berval *)&slap_EXOP_MODIFY_PASSWD ) != LDAP_SUCCESS) {
116 /* check for referrals */
117 if ( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
122 #ifndef SLAPD_MULTIMASTER
123 /* This does not apply to multi-master case */
124 if(!( !SLAP_SHADOW( op->o_bd ) || be_isupdate( op ))) {
125 /* we SHOULD return a referral in this case */
126 BerVarray defref = op->o_bd->be_update_refs
127 ? op->o_bd->be_update_refs : default_referral;
129 if( defref != NULL ) {
130 rs->sr_ref = referral_rewrite( op->o_bd->be_update_refs,
131 NULL, NULL, LDAP_SCOPE_DEFAULT );
133 rs->sr_flags |= REP_REF_MUSTBEFREED;
142 rs->sr_text = "shadow context; no update referral";
143 rc = LDAP_UNWILLING_TO_PERFORM;
146 #endif /* !SLAPD_MULTIMASTER */
148 /* generate a new password if none was provided */
149 if ( qpw->rs_new.bv_len == 0 ) {
150 slap_passwd_generate( &qpw->rs_new );
151 if ( qpw->rs_new.bv_len ) {
152 rsp = slap_passwd_return( &qpw->rs_new );
155 if ( qpw->rs_new.bv_len == 0 ) {
156 rs->sr_text = "password generation failed";
161 /* Give the backend a chance to handle this itself */
162 if ( op->o_bd->be_extended ) {
163 rs->sr_err = op->o_bd->be_extended( op, rs );
164 if ( rs->sr_err != LDAP_UNWILLING_TO_PERFORM &&
165 rs->sr_err != SLAP_CB_CONTINUE ) {
171 /* The backend didn't handle it, so try it here */
172 if( op->o_bd && !op->o_bd->be_modify ) {
173 rs->sr_text = "operation not supported for current user";
174 rc = LDAP_UNWILLING_TO_PERFORM;
178 ml = ch_malloc( sizeof(Modifications) );
179 if ( !qpw->rs_modtail ) qpw->rs_modtail = &ml->sml_next;
181 if ( default_passwd_hash ) {
182 for ( nhash = 0; default_passwd_hash[nhash]; nhash++ );
183 hashes = default_passwd_hash;
186 hashes = (char **)defhash;
188 ml->sml_values = ch_malloc( (nhash+1)*sizeof(struct berval) );
189 for ( i=0; hashes[i]; i++ ) {
190 slap_passwd_hash_type( &qpw->rs_new, &hash, hashes[i], &rs->sr_text );
191 if ( hash.bv_len == 0 ) {
192 if ( !rs->sr_text ) {
193 rs->sr_text = "password hash failed";
197 ml->sml_values[i] = hash;
199 ml->sml_values[i].bv_val = NULL;
200 ml->sml_nvalues = NULL;
201 ml->sml_desc = slap_schema.si_ad_userPassword;
202 ml->sml_op = LDAP_MOD_REPLACE;
203 ml->sml_next = qpw->rs_mods;
207 rs->sr_err = LDAP_OTHER;
211 op2.o_tag = LDAP_REQ_MODIFY;
212 op2.o_callback = &cb2;
213 op2.orm_modlist = qpw->rs_mods;
214 cb2.sc_private = qpw; /* let Modify know this was pwdMod,
217 rs->sr_err = slap_mods_opattrs( &op2, ml, qpw->rs_modtail, &rs->sr_text,
220 if ( rs->sr_err == LDAP_SUCCESS ) {
221 rs->sr_err = op2.o_bd->be_modify( &op2, rs );
222 /* FIXME: in case it got rewritten... */
223 op->o_req_dn = op2.o_req_dn;
224 op->o_req_ndn = op2.o_req_ndn;
226 if ( rs->sr_err == LDAP_SUCCESS ) {
227 rs->sr_rspdata = rsp;
232 slap_mods_free( qpw->rs_mods );
234 free( qpw->rs_new.bv_val );
240 if ( !BER_BVISNULL( &op->o_req_dn ) ) {
241 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
243 if ( !BER_BVISNULL( &op->o_req_ndn ) ) {
244 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
250 int slap_passwd_parse( struct berval *reqdata,
252 struct berval *oldpass,
253 struct berval *newpass,
256 int rc = LDAP_SUCCESS;
259 BerElementBuffer berbuf;
260 BerElement *ber = (BerElement *)&berbuf;
262 if( reqdata == NULL ) {
266 if( reqdata->bv_len == 0 ) {
267 *text = "empty request data field";
268 return LDAP_PROTOCOL_ERROR;
271 /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
272 ber_init2( ber, reqdata, 0 );
274 tag = ber_scanf( ber, "{" /*}*/ );
276 if( tag == LBER_ERROR ) {
277 Debug( LDAP_DEBUG_TRACE,
278 "slap_passwd_parse: decoding error\n", 0, 0, 0 );
279 rc = LDAP_PROTOCOL_ERROR;
283 tag = ber_peek_tag( ber, &len );
284 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_ID ) {
286 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID not allowed.\n",
289 *text = "user must change own password";
290 rc = LDAP_UNWILLING_TO_PERFORM;
294 tag = ber_scanf( ber, "m", id );
296 if( tag == LBER_ERROR ) {
297 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
303 tag = ber_peek_tag( ber, &len);
306 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_OLD ) {
307 if( oldpass == NULL ) {
308 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD not allowed.\n",
311 *text = "use bind to verify old password";
312 rc = LDAP_UNWILLING_TO_PERFORM;
316 tag = ber_scanf( ber, "m", oldpass );
318 if( tag == LBER_ERROR ) {
319 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD parse failed.\n",
325 tag = ber_peek_tag( ber, &len );
328 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ) {
329 if( newpass == NULL ) {
330 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW not allowed.\n",
333 *text = "user specified passwords disallowed";
334 rc = LDAP_UNWILLING_TO_PERFORM;
338 tag = ber_scanf( ber, "m", newpass );
340 if( tag == LBER_ERROR ) {
341 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW parse failed.\n",
347 tag = ber_peek_tag( ber, &len );
352 Debug( LDAP_DEBUG_TRACE,
353 "slap_passwd_parse: decoding error, len=%ld\n",
356 *text = "data decoding error";
357 rc = LDAP_PROTOCOL_ERROR;
364 struct berval * slap_passwd_return(
365 struct berval *cred )
368 struct berval *bv = NULL;
369 BerElementBuffer berbuf;
370 /* opaque structure, size unknown but smaller than berbuf */
371 BerElement *ber = (BerElement *)&berbuf;
373 assert( cred != NULL );
375 Debug( LDAP_DEBUG_TRACE, "slap_passwd_return: %ld\n",
376 (long) cred->bv_len, 0, 0 );
378 ber_init_w_nullc( ber, LBER_USE_DER );
380 rc = ber_printf( ber, "{tON}",
381 LDAP_TAG_EXOP_MODIFY_PASSWD_GEN, cred );
384 (void) ber_flatten( ber, &bv );
393 * if "e" is provided, access to each value of the password is checked first
405 AccessControlState acl_state = ACL_STATE_INIT;
407 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
408 ldap_pvt_thread_mutex_lock( &passwd_mutex );
410 lutil_passwd_sasl_conn = op->o_conn->c_sasl_authctx;
414 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
415 /* if e is provided, check access */
416 if ( e && access_allowed( op, e, a->a_desc, bv,
417 ACL_AUTH, &acl_state ) == 0 )
422 if ( !lutil_passwd( bv, cred, NULL, text ) ) {
428 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
430 lutil_passwd_sasl_conn = NULL;
432 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
439 slap_passwd_generate( struct berval *pass )
441 Debug( LDAP_DEBUG_TRACE, "slap_passwd_generate\n", 0, 0, 0 );
446 * generate passwords of only 8 characters as some getpass(3)
447 * implementations truncate at 8 characters.
449 lutil_passwd_generate( pass, 8 );
453 slap_passwd_hash_type(
454 struct berval * cred,
464 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
465 ldap_pvt_thread_mutex_lock( &passwd_mutex );
468 lutil_passwd_hash( cred , hash, new, text );
470 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
471 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
477 struct berval * cred,
482 if ( default_passwd_hash ) {
483 hash = default_passwd_hash[0];
486 hash = (char *)defhash[0];
489 slap_passwd_hash_type( cred, new, hash, text );