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>
30 #include <lutil_sha1.h>
32 static const char *defhash[] = {
33 #ifdef LUTIL_SHA1_BYTES
45 struct berval id = {0, NULL}, hash, *rsp = NULL;
46 req_pwdexop_s *qpw = &op->oq_pwdexop;
49 slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
50 slap_callback cb2 = { NULL, slap_replog_cb, NULL, NULL };
57 assert( ber_bvcmp( &slap_EXOP_MODIFY_PASSWD, &op->ore_reqoid ) == 0 );
59 if( op->o_dn.bv_len == 0 ) {
60 rs->sr_text = "only authenticated users may change passwords";
61 return LDAP_STRONG_AUTH_REQUIRED;
64 qpw->rs_old.bv_val = NULL;
65 qpw->rs_new.bv_val = NULL;
67 qpw->rs_modtail = NULL;
69 rs->sr_err = slap_passwd_parse( op->ore_reqdata, &id, &qpw->rs_old,
70 &qpw->rs_new, &rs->sr_text );
72 if ( rs->sr_err != LDAP_SUCCESS ) {
77 ber_dupbv_x( &op->o_req_dn, &id, op->o_tmpmemctx );
78 /* ndn is in tmpmem, so we don't need to free it */
79 rs->sr_err = dnNormalize( 0, NULL, NULL, &id, &op->o_req_ndn, op->o_tmpmemctx );
80 if ( rs->sr_err != LDAP_SUCCESS ) {
81 rs->sr_text = "Invalid DN";
85 op->o_bd = select_backend( &op->o_req_ndn, 0, 1 );
88 ber_dupbv_x( &op->o_req_dn, &op->o_dn, op->o_tmpmemctx );
89 ber_dupbv_x( &op->o_req_ndn, &op->o_ndn, op->o_tmpmemctx );
90 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
91 op->o_bd = op->o_conn->c_authz_backend;
92 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
95 if( op->o_bd == NULL ) {
96 #ifdef HAVE_CYRUS_SASL
97 rc = slap_sasl_setpass( op, rs );
99 rs->sr_text = "no authz backend";
105 if ( op->o_req_ndn.bv_len == 0 ) {
106 rs->sr_text = "no password is associated with the Root DSE";
107 rc = LDAP_UNWILLING_TO_PERFORM;
111 if (backend_check_restrictions( op, rs,
112 (struct berval *)&slap_EXOP_MODIFY_PASSWD ) != LDAP_SUCCESS) {
117 /* check for referrals */
118 if ( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
123 #ifndef SLAPD_MULTIMASTER
124 /* This does not apply to multi-master case */
125 if(!( !SLAP_SHADOW( op->o_bd ) || be_isupdate( op ))) {
126 /* we SHOULD return a referral in this case */
127 BerVarray defref = op->o_bd->be_update_refs
128 ? op->o_bd->be_update_refs : default_referral;
130 if( defref != NULL ) {
131 rs->sr_ref = referral_rewrite( op->o_bd->be_update_refs,
132 NULL, NULL, LDAP_SCOPE_DEFAULT );
134 rs->sr_flags |= REP_REF_MUSTBEFREED;
143 rs->sr_text = "shadow context; no update referral";
144 rc = LDAP_UNWILLING_TO_PERFORM;
147 #endif /* !SLAPD_MULTIMASTER */
149 /* generate a new password if none was provided */
150 if ( qpw->rs_new.bv_len == 0 ) {
151 slap_passwd_generate( &qpw->rs_new );
152 if ( qpw->rs_new.bv_len ) {
153 rsp = slap_passwd_return( &qpw->rs_new );
156 if ( qpw->rs_new.bv_len == 0 ) {
157 rs->sr_text = "password generation failed";
162 /* Give the backend a chance to handle this itself */
163 if ( op->o_bd->be_extended ) {
164 rs->sr_err = op->o_bd->be_extended( op, rs );
165 if ( rs->sr_err != LDAP_UNWILLING_TO_PERFORM &&
166 rs->sr_err != SLAP_CB_CONTINUE ) {
172 /* The backend didn't handle it, so try it here */
173 if( op->o_bd && !op->o_bd->be_modify ) {
174 rs->sr_text = "operation not supported for current user";
175 rc = LDAP_UNWILLING_TO_PERFORM;
179 ml = ch_malloc( sizeof(Modifications) );
180 if ( !qpw->rs_modtail ) qpw->rs_modtail = &ml->sml_next;
182 if ( default_passwd_hash ) {
183 for ( nhash = 0; default_passwd_hash[nhash]; nhash++ );
184 hashes = default_passwd_hash;
187 hashes = (char **)defhash;
189 ml->sml_values = ch_malloc( (nhash+1)*sizeof(struct berval) );
190 for ( i=0; hashes[i]; i++ ) {
191 slap_passwd_hash_type( &qpw->rs_new, &hash, hashes[i], &rs->sr_text );
192 if ( hash.bv_len == 0 ) {
193 if ( !rs->sr_text ) {
194 rs->sr_text = "password hash failed";
198 ml->sml_values[i] = hash;
200 ml->sml_values[i].bv_val = NULL;
201 ml->sml_nvalues = NULL;
202 ml->sml_desc = slap_schema.si_ad_userPassword;
203 ml->sml_op = LDAP_MOD_REPLACE;
204 ml->sml_next = qpw->rs_mods;
208 rs->sr_err = LDAP_OTHER;
212 op2.o_tag = LDAP_REQ_MODIFY;
213 op2.o_callback = &cb2;
214 op2.orm_modlist = qpw->rs_mods;
215 cb2.sc_private = qpw; /* let Modify know this was pwdMod,
218 rs->sr_err = slap_mods_opattrs( &op2, ml, qpw->rs_modtail, &rs->sr_text,
221 if ( rs->sr_err == LDAP_SUCCESS ) {
222 rs->sr_err = op2.o_bd->be_modify( &op2, rs );
223 /* FIXME: in case it got rewritten... */
224 op->o_req_dn = op2.o_req_dn;
225 op->o_req_ndn = op2.o_req_ndn;
227 if ( rs->sr_err == LDAP_SUCCESS ) {
228 rs->sr_rspdata = rsp;
233 slap_mods_free( qpw->rs_mods );
235 free( qpw->rs_new.bv_val );
241 if ( !BER_BVISNULL( &op->o_req_dn ) ) {
242 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
244 if ( !BER_BVISNULL( &op->o_req_ndn ) ) {
245 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
251 int slap_passwd_parse( struct berval *reqdata,
253 struct berval *oldpass,
254 struct berval *newpass,
257 int rc = LDAP_SUCCESS;
260 BerElementBuffer berbuf;
261 BerElement *ber = (BerElement *)&berbuf;
263 if( reqdata == NULL ) {
267 if( reqdata->bv_len == 0 ) {
268 *text = "empty request data field";
269 return LDAP_PROTOCOL_ERROR;
272 /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
273 ber_init2( ber, reqdata, 0 );
275 tag = ber_scanf( ber, "{" /*}*/ );
277 if( tag == LBER_ERROR ) {
278 Debug( LDAP_DEBUG_TRACE,
279 "slap_passwd_parse: decoding error\n", 0, 0, 0 );
280 rc = LDAP_PROTOCOL_ERROR;
284 tag = ber_peek_tag( ber, &len );
285 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_ID ) {
287 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID not allowed.\n",
290 *text = "user must change own password";
291 rc = LDAP_UNWILLING_TO_PERFORM;
295 tag = ber_scanf( ber, "m", id );
297 if( tag == LBER_ERROR ) {
298 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
304 tag = ber_peek_tag( ber, &len);
307 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_OLD ) {
308 if( oldpass == NULL ) {
309 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD not allowed.\n",
312 *text = "use bind to verify old password";
313 rc = LDAP_UNWILLING_TO_PERFORM;
317 tag = ber_scanf( ber, "m", oldpass );
319 if( tag == LBER_ERROR ) {
320 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD parse failed.\n",
326 tag = ber_peek_tag( ber, &len );
329 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ) {
330 if( newpass == NULL ) {
331 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW not allowed.\n",
334 *text = "user specified passwords disallowed";
335 rc = LDAP_UNWILLING_TO_PERFORM;
339 tag = ber_scanf( ber, "m", newpass );
341 if( tag == LBER_ERROR ) {
342 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW parse failed.\n",
348 tag = ber_peek_tag( ber, &len );
353 Debug( LDAP_DEBUG_TRACE,
354 "slap_passwd_parse: decoding error, len=%ld\n",
357 *text = "data decoding error";
358 rc = LDAP_PROTOCOL_ERROR;
365 struct berval * slap_passwd_return(
366 struct berval *cred )
369 struct berval *bv = NULL;
370 BerElementBuffer berbuf;
371 /* opaque structure, size unknown but smaller than berbuf */
372 BerElement *ber = (BerElement *)&berbuf;
374 assert( cred != NULL );
376 Debug( LDAP_DEBUG_TRACE, "slap_passwd_return: %ld\n",
377 (long) cred->bv_len, 0, 0 );
379 ber_init_w_nullc( ber, LBER_USE_DER );
381 rc = ber_printf( ber, "{tON}",
382 LDAP_TAG_EXOP_MODIFY_PASSWD_GEN, cred );
385 (void) ber_flatten( ber, &bv );
394 * if "e" is provided, access to each value of the password is checked first
406 AccessControlState acl_state = ACL_STATE_INIT;
408 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
409 ldap_pvt_thread_mutex_lock( &passwd_mutex );
411 lutil_passwd_sasl_conn = op->o_conn->c_sasl_authctx;
415 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
416 /* if e is provided, check access */
417 if ( e && access_allowed( op, e, a->a_desc, bv,
418 ACL_AUTH, &acl_state ) == 0 )
423 if ( !lutil_passwd( bv, cred, NULL, text ) ) {
429 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
431 lutil_passwd_sasl_conn = NULL;
433 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
440 slap_passwd_generate( struct berval *pass )
442 Debug( LDAP_DEBUG_TRACE, "slap_passwd_generate\n", 0, 0, 0 );
447 * generate passwords of only 8 characters as some getpass(3)
448 * implementations truncate at 8 characters.
450 lutil_passwd_generate( pass, 8 );
454 slap_passwd_hash_type(
455 struct berval * cred,
465 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
466 ldap_pvt_thread_mutex_lock( &passwd_mutex );
469 lutil_passwd_hash( cred , hash, new, text );
471 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
472 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
478 struct berval * cred,
483 if ( default_passwd_hash ) {
484 hash = default_passwd_hash[0];
487 hash = (char *)defhash[0];
490 slap_passwd_hash_type( cred, new, hash, text );