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_len = 0;
72 qpw->rs_old.bv_val = NULL;
73 qpw->rs_new.bv_len = 0;
74 qpw->rs_new.bv_val = NULL;
76 qpw->rs_modtail = NULL;
78 rs->sr_err = slap_passwd_parse( op->ore_reqdata, &id,
79 &qpw->rs_old, &qpw->rs_new, &rs->sr_text );
81 if ( rs->sr_err == LDAP_SUCCESS && !BER_BVISEMPTY( &id ) ) {
82 Statslog( LDAP_DEBUG_STATS, "%s PASSMOD id=\"%s\"%s%s\n",
83 op->o_log_prefix, id.bv_val,
84 qpw->rs_old.bv_val ? " old" : "",
85 qpw->rs_new.bv_val ? " new" : "", 0 );
87 Statslog( LDAP_DEBUG_STATS, "%s PASSMOD %s%s\n",
89 qpw->rs_old.bv_val ? " old" : "",
90 qpw->rs_new.bv_val ? " new" : "", 0, 0 );
93 if ( rs->sr_err != LDAP_SUCCESS ) {
97 if ( !BER_BVISEMPTY( &id ) ) {
98 rs->sr_err = dnPrettyNormal( NULL, &id, &op->o_req_dn,
99 &op->o_req_ndn, op->o_tmpmemctx );
100 if ( rs->sr_err != LDAP_SUCCESS ) {
101 rs->sr_text = "Invalid DN";
105 op->o_bd = select_backend( &op->o_req_ndn, 0, 1 );
108 ber_dupbv_x( &op->o_req_dn, &op->o_dn, op->o_tmpmemctx );
109 ber_dupbv_x( &op->o_req_ndn, &op->o_ndn, op->o_tmpmemctx );
110 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
111 op->o_bd = op->o_conn->c_authz_backend;
112 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
115 if( op->o_bd == NULL ) {
116 if ( qpw->rs_old.bv_val != NULL ) {
117 rs->sr_text = "unwilling to verify old password";
118 rc = LDAP_UNWILLING_TO_PERFORM;
122 #ifdef HAVE_CYRUS_SASL
123 rc = slap_sasl_setpass( op, rs );
125 rs->sr_text = "no authz backend";
131 if ( op->o_req_ndn.bv_len == 0 ) {
132 rs->sr_text = "no password is associated with the Root DSE";
133 rc = LDAP_UNWILLING_TO_PERFORM;
137 /* If we've got a glued backend, check the real backend */
139 if ( SLAP_GLUE_INSTANCE( op->o_bd )) {
140 op->o_bd = select_backend( &op->o_req_ndn, 0, 0 );
143 if (backend_check_restrictions( op, rs,
144 (struct berval *)&slap_EXOP_MODIFY_PASSWD ) != LDAP_SUCCESS) {
149 /* check for referrals */
150 if ( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
155 #ifndef SLAPD_MULTIMASTER
156 /* This does not apply to multi-master case */
157 if(!( !SLAP_SHADOW( op->o_bd ) || be_isupdate( op ))) {
158 /* we SHOULD return a referral in this case */
159 BerVarray defref = op->o_bd->be_update_refs
160 ? op->o_bd->be_update_refs : default_referral;
162 if( defref != NULL ) {
163 rs->sr_ref = referral_rewrite( op->o_bd->be_update_refs,
164 NULL, NULL, LDAP_SCOPE_DEFAULT );
166 rs->sr_flags |= REP_REF_MUSTBEFREED;
175 rs->sr_text = "shadow context; no update referral";
176 rc = LDAP_UNWILLING_TO_PERFORM;
179 #endif /* !SLAPD_MULTIMASTER */
181 /* generate a new password if none was provided */
182 if ( qpw->rs_new.bv_len == 0 ) {
183 slap_passwd_generate( &qpw->rs_new );
184 if ( qpw->rs_new.bv_len ) {
185 rsp = slap_passwd_return( &qpw->rs_new );
188 if ( qpw->rs_new.bv_len == 0 ) {
189 rs->sr_text = "password generation failed";
196 /* Give the backend a chance to handle this itself */
197 if ( op->o_bd->be_extended ) {
198 rs->sr_err = op->o_bd->be_extended( op, rs );
199 if ( rs->sr_err != LDAP_UNWILLING_TO_PERFORM &&
200 rs->sr_err != SLAP_CB_CONTINUE ) {
206 /* The backend didn't handle it, so try it here */
207 if( op->o_bd && !op->o_bd->be_modify ) {
208 rs->sr_text = "operation not supported for current user";
209 rc = LDAP_UNWILLING_TO_PERFORM;
213 if ( qpw->rs_old.bv_val != NULL ) {
216 rc = be_entry_get_rw( op, &op->o_req_ndn, NULL,
217 slap_schema.si_ad_userPassword, 0, &e );
218 if ( rc == LDAP_SUCCESS && e ) {
219 Attribute *a = attr_find( e->e_attrs,
220 slap_schema.si_ad_userPassword );
222 rc = slap_passwd_check( op, e, a, &qpw->rs_old, &rs->sr_text );
225 be_entry_release_r( op, e );
226 if ( rc == LDAP_SUCCESS )
229 rs->sr_text = "unwilling to verify old password";
230 rc = LDAP_UNWILLING_TO_PERFORM;
235 ml = ch_malloc( sizeof(Modifications) );
236 if ( !qpw->rs_modtail ) qpw->rs_modtail = &ml->sml_next;
238 if ( default_passwd_hash ) {
239 for ( nhash = 0; default_passwd_hash[nhash]; nhash++ );
240 hashes = default_passwd_hash;
243 hashes = (char **)defhash;
245 ml->sml_values = ch_malloc( (nhash+1)*sizeof(struct berval) );
246 for ( i=0; hashes[i]; i++ ) {
247 slap_passwd_hash_type( &qpw->rs_new, &hash, hashes[i], &rs->sr_text );
248 if ( hash.bv_len == 0 ) {
249 if ( !rs->sr_text ) {
250 rs->sr_text = "password hash failed";
254 ml->sml_values[i] = hash;
256 ml->sml_values[i].bv_val = NULL;
257 ml->sml_nvalues = NULL;
258 ml->sml_desc = slap_schema.si_ad_userPassword;
259 ml->sml_op = LDAP_MOD_REPLACE;
261 ml->sml_next = qpw->rs_mods;
265 rs->sr_err = LDAP_OTHER;
268 slap_callback *sc = op->o_callback;
270 op->o_tag = LDAP_REQ_MODIFY;
271 op->o_callback = &cb2;
272 op->orm_modlist = qpw->rs_mods;
273 cb2.sc_private = qpw; /* let Modify know this was pwdMod,
276 rs->sr_err = op->o_bd->be_modify( op, rs );
277 if ( rs->sr_err == LDAP_SUCCESS ) {
278 rs->sr_rspdata = rsp;
282 op->o_tag = LDAP_REQ_EXTENDED;
285 slap_mods_free( qpw->rs_mods, 1 );
287 free( qpw->rs_new.bv_val );
291 op->oq_extended = qext;
294 if ( !BER_BVISNULL( &op->o_req_dn ) ) {
295 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
297 if ( !BER_BVISNULL( &op->o_req_ndn ) ) {
298 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
304 int slap_passwd_parse( struct berval *reqdata,
306 struct berval *oldpass,
307 struct berval *newpass,
310 int rc = LDAP_SUCCESS;
313 BerElementBuffer berbuf;
314 BerElement *ber = (BerElement *)&berbuf;
316 if( reqdata == NULL ) {
320 if( reqdata->bv_len == 0 ) {
321 *text = "empty request data field";
322 return LDAP_PROTOCOL_ERROR;
325 /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
326 ber_init2( ber, reqdata, 0 );
328 tag = ber_scanf( ber, "{" /*}*/ );
330 if( tag == LBER_ERROR ) {
331 Debug( LDAP_DEBUG_TRACE,
332 "slap_passwd_parse: decoding error\n", 0, 0, 0 );
333 rc = LDAP_PROTOCOL_ERROR;
337 tag = ber_peek_tag( ber, &len );
338 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_ID ) {
340 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID not allowed.\n",
343 *text = "user must change own password";
344 rc = LDAP_UNWILLING_TO_PERFORM;
348 tag = ber_scanf( ber, "m", id );
350 if( tag == LBER_ERROR ) {
351 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
357 tag = ber_peek_tag( ber, &len );
360 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_OLD ) {
361 if( oldpass == NULL ) {
362 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD not allowed.\n",
365 *text = "use bind to verify old password";
366 rc = LDAP_UNWILLING_TO_PERFORM;
370 tag = ber_scanf( ber, "m", oldpass );
372 if( tag == LBER_ERROR ) {
373 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD parse failed.\n",
379 if( oldpass->bv_len == 0 ) {
380 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD empty.\n",
383 *text = "old password value is empty";
384 rc = LDAP_UNWILLING_TO_PERFORM;
388 tag = ber_peek_tag( ber, &len );
391 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ) {
392 if( newpass == NULL ) {
393 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW not allowed.\n",
396 *text = "user specified passwords disallowed";
397 rc = LDAP_UNWILLING_TO_PERFORM;
401 tag = ber_scanf( ber, "m", newpass );
403 if( tag == LBER_ERROR ) {
404 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW parse failed.\n",
410 if( newpass->bv_len == 0 ) {
411 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW empty.\n",
414 *text = "new password value is empty";
415 rc = LDAP_UNWILLING_TO_PERFORM;
419 tag = ber_peek_tag( ber, &len );
424 Debug( LDAP_DEBUG_TRACE,
425 "slap_passwd_parse: decoding error, len=%ld\n",
428 *text = "data decoding error";
429 rc = LDAP_PROTOCOL_ERROR;
436 struct berval * slap_passwd_return(
437 struct berval *cred )
440 struct berval *bv = NULL;
441 BerElementBuffer berbuf;
442 /* opaque structure, size unknown but smaller than berbuf */
443 BerElement *ber = (BerElement *)&berbuf;
445 assert( cred != NULL );
447 Debug( LDAP_DEBUG_TRACE, "slap_passwd_return: %ld\n",
448 (long) cred->bv_len, 0, 0 );
450 ber_init_w_nullc( ber, LBER_USE_DER );
452 rc = ber_printf( ber, "{tON}",
453 LDAP_TAG_EXOP_MODIFY_PASSWD_GEN, cred );
456 (void) ber_flatten( ber, &bv );
465 * if "e" is provided, access to each value of the password is checked first
477 AccessControlState acl_state = ACL_STATE_INIT;
480 ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind,
481 op->o_conn->c_sasl_authctx, NULL );
484 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
485 /* if e is provided, check access */
486 if ( e && access_allowed( op, e, a->a_desc, bv,
487 ACL_AUTH, &acl_state ) == 0 )
492 if ( !lutil_passwd( bv, cred, NULL, text ) ) {
499 ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind,
507 slap_passwd_generate( struct berval *pass )
509 Debug( LDAP_DEBUG_TRACE, "slap_passwd_generate\n", 0, 0, 0 );
514 * generate passwords of only 8 characters as some getpass(3)
515 * implementations truncate at 8 characters.
517 lutil_passwd_generate( pass, 8 );
521 slap_passwd_hash_type(
522 struct berval * cred,
530 assert( hash != NULL );
532 lutil_passwd_hash( cred , hash, new, text );
536 struct berval * cred,
541 if ( default_passwd_hash ) {
542 hash = default_passwd_hash[0];
545 hash = (char *)defhash[0];
548 slap_passwd_hash_type( cred, new, hash, text );
552 static ldap_pvt_thread_mutex_t passwd_mutex;
553 static lutil_cryptfunc slapd_crypt;
555 static int slapd_crypt( const char *key, const char *salt, char **hash )
560 ldap_pvt_thread_mutex_lock( &passwd_mutex );
562 cr = crypt( key, salt );
563 if ( cr == NULL || cr[0] == '\0' ) {
564 /* salt must have been invalid */
565 rc = LUTIL_PASSWD_ERR;
568 *hash = ber_strdup( cr );
569 rc = LUTIL_PASSWD_OK;
572 rc = strcmp( salt, cr ) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
576 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
579 #endif /* SLAPD_CRYPT */
581 void slap_passwd_init()
584 ldap_pvt_thread_mutex_init( &passwd_mutex );
585 lutil_cryptptr = slapd_crypt;