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;
47 req_extended_s qext = op->oq_extended;
49 slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
50 slap_callback cb2 = { NULL, slap_replog_cb, NULL, NULL };
58 assert( ber_bvcmp( &slap_EXOP_MODIFY_PASSWD, &op->ore_reqoid ) == 0 );
60 if( op->o_dn.bv_len == 0 ) {
61 rs->sr_text = "only authenticated users may change passwords";
62 return LDAP_STRONG_AUTH_REQUIRED;
65 qpw->rs_old.bv_val = NULL;
66 qpw->rs_new.bv_val = NULL;
68 qpw->rs_modtail = NULL;
70 rs->sr_err = slap_passwd_parse( op->ore_reqdata, &id, &qpw->rs_old,
71 &qpw->rs_new, &rs->sr_text );
73 if ( rs->sr_err != LDAP_SUCCESS ) {
78 ber_dupbv_x( &op->o_req_dn, &id, op->o_tmpmemctx );
79 /* ndn is in tmpmem, so we don't need to free it */
80 rs->sr_err = dnNormalize( 0, NULL, NULL, &id, &op->o_req_ndn, op->o_tmpmemctx );
81 if ( rs->sr_err != LDAP_SUCCESS ) {
82 rs->sr_text = "Invalid DN";
86 op->o_bd = select_backend( &op->o_req_ndn, 0, 1 );
89 ber_dupbv_x( &op->o_req_dn, &op->o_dn, op->o_tmpmemctx );
90 ber_dupbv_x( &op->o_req_ndn, &op->o_ndn, op->o_tmpmemctx );
91 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
92 op->o_bd = op->o_conn->c_authz_backend;
93 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
96 if( op->o_bd == NULL ) {
97 #ifdef HAVE_CYRUS_SASL
98 rc = slap_sasl_setpass( op, rs );
100 rs->sr_text = "no authz backend";
106 if ( op->o_req_ndn.bv_len == 0 ) {
107 rs->sr_text = "no password is associated with the Root DSE";
108 rc = LDAP_UNWILLING_TO_PERFORM;
112 /* If we've got a glued backend, check the real backend */
114 if ( SLAP_GLUE_INSTANCE( op->o_bd )) {
115 op->o_bd = select_backend( &op->o_req_ndn, 0, 0 );
118 if (backend_check_restrictions( op, rs,
119 (struct berval *)&slap_EXOP_MODIFY_PASSWD ) != LDAP_SUCCESS) {
124 /* check for referrals */
125 if ( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
130 #ifndef SLAPD_MULTIMASTER
131 /* This does not apply to multi-master case */
132 if(!( !SLAP_SHADOW( op->o_bd ) || be_isupdate( op ))) {
133 /* we SHOULD return a referral in this case */
134 BerVarray defref = op->o_bd->be_update_refs
135 ? op->o_bd->be_update_refs : default_referral;
137 if( defref != NULL ) {
138 rs->sr_ref = referral_rewrite( op->o_bd->be_update_refs,
139 NULL, NULL, LDAP_SCOPE_DEFAULT );
141 rs->sr_flags |= REP_REF_MUSTBEFREED;
150 rs->sr_text = "shadow context; no update referral";
151 rc = LDAP_UNWILLING_TO_PERFORM;
154 #endif /* !SLAPD_MULTIMASTER */
156 /* generate a new password if none was provided */
157 if ( qpw->rs_new.bv_len == 0 ) {
158 slap_passwd_generate( &qpw->rs_new );
159 if ( qpw->rs_new.bv_len ) {
160 rsp = slap_passwd_return( &qpw->rs_new );
163 if ( qpw->rs_new.bv_len == 0 ) {
164 rs->sr_text = "password generation failed";
171 /* Give the backend a chance to handle this itself */
172 if ( op->o_bd->be_extended ) {
173 rs->sr_err = op->o_bd->be_extended( op, rs );
174 if ( rs->sr_err != LDAP_UNWILLING_TO_PERFORM &&
175 rs->sr_err != SLAP_CB_CONTINUE ) {
181 /* The backend didn't handle it, so try it here */
182 if( op->o_bd && !op->o_bd->be_modify ) {
183 rs->sr_text = "operation not supported for current user";
184 rc = LDAP_UNWILLING_TO_PERFORM;
188 ml = ch_malloc( sizeof(Modifications) );
189 if ( !qpw->rs_modtail ) qpw->rs_modtail = &ml->sml_next;
191 if ( default_passwd_hash ) {
192 for ( nhash = 0; default_passwd_hash[nhash]; nhash++ );
193 hashes = default_passwd_hash;
196 hashes = (char **)defhash;
198 ml->sml_values = ch_malloc( (nhash+1)*sizeof(struct berval) );
199 for ( i=0; hashes[i]; i++ ) {
200 slap_passwd_hash_type( &qpw->rs_new, &hash, hashes[i], &rs->sr_text );
201 if ( hash.bv_len == 0 ) {
202 if ( !rs->sr_text ) {
203 rs->sr_text = "password hash failed";
207 ml->sml_values[i] = hash;
209 ml->sml_values[i].bv_val = NULL;
210 ml->sml_nvalues = NULL;
211 ml->sml_desc = slap_schema.si_ad_userPassword;
212 ml->sml_op = LDAP_MOD_REPLACE;
213 ml->sml_next = qpw->rs_mods;
217 rs->sr_err = LDAP_OTHER;
220 slap_callback *sc = op->o_callback;
222 op->o_tag = LDAP_REQ_MODIFY;
223 op->o_callback = &cb2;
224 op->orm_modlist = qpw->rs_mods;
225 cb2.sc_private = qpw; /* let Modify know this was pwdMod,
228 rs->sr_err = slap_mods_opattrs( op, ml, qpw->rs_modtail, &rs->sr_text,
231 if ( rs->sr_err == LDAP_SUCCESS ) {
232 rs->sr_err = op->o_bd->be_modify( op, rs );
234 if ( rs->sr_err == LDAP_SUCCESS ) {
235 rs->sr_rspdata = rsp;
239 op->o_tag = LDAP_REQ_EXTENDED;
242 slap_mods_free( qpw->rs_mods );
244 free( qpw->rs_new.bv_val );
248 op->oq_extended = qext;
251 if ( !BER_BVISNULL( &op->o_req_dn ) ) {
252 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
254 if ( !BER_BVISNULL( &op->o_req_ndn ) ) {
255 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
261 int slap_passwd_parse( struct berval *reqdata,
263 struct berval *oldpass,
264 struct berval *newpass,
267 int rc = LDAP_SUCCESS;
270 BerElementBuffer berbuf;
271 BerElement *ber = (BerElement *)&berbuf;
273 if( reqdata == NULL ) {
277 if( reqdata->bv_len == 0 ) {
278 *text = "empty request data field";
279 return LDAP_PROTOCOL_ERROR;
282 /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
283 ber_init2( ber, reqdata, 0 );
285 tag = ber_scanf( ber, "{" /*}*/ );
287 if( tag == LBER_ERROR ) {
288 Debug( LDAP_DEBUG_TRACE,
289 "slap_passwd_parse: decoding error\n", 0, 0, 0 );
290 rc = LDAP_PROTOCOL_ERROR;
294 tag = ber_peek_tag( ber, &len );
295 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_ID ) {
297 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID not allowed.\n",
300 *text = "user must change own password";
301 rc = LDAP_UNWILLING_TO_PERFORM;
305 tag = ber_scanf( ber, "m", id );
307 if( tag == LBER_ERROR ) {
308 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
314 tag = ber_peek_tag( ber, &len);
317 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_OLD ) {
318 if( oldpass == NULL ) {
319 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD not allowed.\n",
322 *text = "use bind to verify old password";
323 rc = LDAP_UNWILLING_TO_PERFORM;
327 tag = ber_scanf( ber, "m", oldpass );
329 if( tag == LBER_ERROR ) {
330 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD parse failed.\n",
336 tag = ber_peek_tag( ber, &len );
339 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ) {
340 if( newpass == NULL ) {
341 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW not allowed.\n",
344 *text = "user specified passwords disallowed";
345 rc = LDAP_UNWILLING_TO_PERFORM;
349 tag = ber_scanf( ber, "m", newpass );
351 if( tag == LBER_ERROR ) {
352 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW parse failed.\n",
358 tag = ber_peek_tag( ber, &len );
363 Debug( LDAP_DEBUG_TRACE,
364 "slap_passwd_parse: decoding error, len=%ld\n",
367 *text = "data decoding error";
368 rc = LDAP_PROTOCOL_ERROR;
375 struct berval * slap_passwd_return(
376 struct berval *cred )
379 struct berval *bv = NULL;
380 BerElementBuffer berbuf;
381 /* opaque structure, size unknown but smaller than berbuf */
382 BerElement *ber = (BerElement *)&berbuf;
384 assert( cred != NULL );
386 Debug( LDAP_DEBUG_TRACE, "slap_passwd_return: %ld\n",
387 (long) cred->bv_len, 0, 0 );
389 ber_init_w_nullc( ber, LBER_USE_DER );
391 rc = ber_printf( ber, "{tON}",
392 LDAP_TAG_EXOP_MODIFY_PASSWD_GEN, cred );
395 (void) ber_flatten( ber, &bv );
404 * if "e" is provided, access to each value of the password is checked first
416 AccessControlState acl_state = ACL_STATE_INIT;
418 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
419 ldap_pvt_thread_mutex_lock( &passwd_mutex );
421 lutil_passwd_sasl_conn = op->o_conn->c_sasl_authctx;
425 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
426 /* if e is provided, check access */
427 if ( e && access_allowed( op, e, a->a_desc, bv,
428 ACL_AUTH, &acl_state ) == 0 )
433 if ( !lutil_passwd( bv, cred, NULL, text ) ) {
439 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
441 lutil_passwd_sasl_conn = NULL;
443 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
450 slap_passwd_generate( struct berval *pass )
452 Debug( LDAP_DEBUG_TRACE, "slap_passwd_generate\n", 0, 0, 0 );
457 * generate passwords of only 8 characters as some getpass(3)
458 * implementations truncate at 8 characters.
460 lutil_passwd_generate( pass, 8 );
464 slap_passwd_hash_type(
465 struct berval * cred,
475 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
476 ldap_pvt_thread_mutex_lock( &passwd_mutex );
479 lutil_passwd_hash( cred , hash, new, text );
481 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
482 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
488 struct berval * cred,
493 if ( default_passwd_hash ) {
494 hash = default_passwd_hash[0];
497 hash = (char *)defhash[0];
500 slap_passwd_hash_type( cred, new, hash, text );