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, &qpw->rs_old,
77 &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\n",
86 op->o_log_prefix, 0, 0, 0, 0 );
89 if ( rs->sr_err != LDAP_SUCCESS ) {
93 if ( !BER_BVISEMPTY( &id ) ) {
94 rs->sr_err = dnPrettyNormal( NULL, &id, &op->o_req_dn,
95 &op->o_req_ndn, op->o_tmpmemctx );
96 if ( rs->sr_err != LDAP_SUCCESS ) {
97 rs->sr_text = "Invalid DN";
101 op->o_bd = select_backend( &op->o_req_ndn, 0, 1 );
104 ber_dupbv_x( &op->o_req_dn, &op->o_dn, op->o_tmpmemctx );
105 ber_dupbv_x( &op->o_req_ndn, &op->o_ndn, op->o_tmpmemctx );
106 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
107 op->o_bd = op->o_conn->c_authz_backend;
108 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
111 if( op->o_bd == NULL ) {
112 #ifdef HAVE_CYRUS_SASL
113 rc = slap_sasl_setpass( op, rs );
115 rs->sr_text = "no authz backend";
121 if ( op->o_req_ndn.bv_len == 0 ) {
122 rs->sr_text = "no password is associated with the Root DSE";
123 rc = LDAP_UNWILLING_TO_PERFORM;
127 /* If we've got a glued backend, check the real backend */
129 if ( SLAP_GLUE_INSTANCE( op->o_bd )) {
130 op->o_bd = select_backend( &op->o_req_ndn, 0, 0 );
133 if (backend_check_restrictions( op, rs,
134 (struct berval *)&slap_EXOP_MODIFY_PASSWD ) != LDAP_SUCCESS) {
139 /* check for referrals */
140 if ( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
145 #ifndef SLAPD_MULTIMASTER
146 /* This does not apply to multi-master case */
147 if(!( !SLAP_SHADOW( op->o_bd ) || be_isupdate( op ))) {
148 /* we SHOULD return a referral in this case */
149 BerVarray defref = op->o_bd->be_update_refs
150 ? op->o_bd->be_update_refs : default_referral;
152 if( defref != NULL ) {
153 rs->sr_ref = referral_rewrite( op->o_bd->be_update_refs,
154 NULL, NULL, LDAP_SCOPE_DEFAULT );
156 rs->sr_flags |= REP_REF_MUSTBEFREED;
165 rs->sr_text = "shadow context; no update referral";
166 rc = LDAP_UNWILLING_TO_PERFORM;
169 #endif /* !SLAPD_MULTIMASTER */
171 /* generate a new password if none was provided */
172 if ( qpw->rs_new.bv_len == 0 ) {
173 slap_passwd_generate( &qpw->rs_new );
174 if ( qpw->rs_new.bv_len ) {
175 rsp = slap_passwd_return( &qpw->rs_new );
178 if ( qpw->rs_new.bv_len == 0 ) {
179 rs->sr_text = "password generation failed";
186 /* Give the backend a chance to handle this itself */
187 if ( op->o_bd->be_extended ) {
188 rs->sr_err = op->o_bd->be_extended( op, rs );
189 if ( rs->sr_err != LDAP_UNWILLING_TO_PERFORM &&
190 rs->sr_err != SLAP_CB_CONTINUE ) {
196 /* The backend didn't handle it, so try it here */
197 if( op->o_bd && !op->o_bd->be_modify ) {
198 rs->sr_text = "operation not supported for current user";
199 rc = LDAP_UNWILLING_TO_PERFORM;
203 ml = ch_malloc( sizeof(Modifications) );
204 if ( !qpw->rs_modtail ) qpw->rs_modtail = &ml->sml_next;
206 if ( default_passwd_hash ) {
207 for ( nhash = 0; default_passwd_hash[nhash]; nhash++ );
208 hashes = default_passwd_hash;
211 hashes = (char **)defhash;
213 ml->sml_values = ch_malloc( (nhash+1)*sizeof(struct berval) );
214 for ( i=0; hashes[i]; i++ ) {
215 slap_passwd_hash_type( &qpw->rs_new, &hash, hashes[i], &rs->sr_text );
216 if ( hash.bv_len == 0 ) {
217 if ( !rs->sr_text ) {
218 rs->sr_text = "password hash failed";
222 ml->sml_values[i] = hash;
224 ml->sml_values[i].bv_val = NULL;
225 ml->sml_nvalues = NULL;
226 ml->sml_desc = slap_schema.si_ad_userPassword;
227 ml->sml_op = LDAP_MOD_REPLACE;
229 ml->sml_next = qpw->rs_mods;
233 rs->sr_err = LDAP_OTHER;
236 slap_callback *sc = op->o_callback;
238 op->o_tag = LDAP_REQ_MODIFY;
239 op->o_callback = &cb2;
240 op->orm_modlist = qpw->rs_mods;
241 cb2.sc_private = qpw; /* let Modify know this was pwdMod,
244 rs->sr_err = slap_mods_opattrs( op, ml, qpw->rs_modtail, &rs->sr_text,
247 if ( rs->sr_err == LDAP_SUCCESS ) {
248 rs->sr_err = op->o_bd->be_modify( op, rs );
250 if ( rs->sr_err == LDAP_SUCCESS ) {
251 rs->sr_rspdata = rsp;
255 op->o_tag = LDAP_REQ_EXTENDED;
258 slap_mods_free( qpw->rs_mods, 1 );
260 free( qpw->rs_new.bv_val );
264 op->oq_extended = qext;
267 if ( !BER_BVISNULL( &op->o_req_dn ) ) {
268 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
270 if ( !BER_BVISNULL( &op->o_req_ndn ) ) {
271 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
277 int slap_passwd_parse( struct berval *reqdata,
279 struct berval *oldpass,
280 struct berval *newpass,
283 int rc = LDAP_SUCCESS;
286 BerElementBuffer berbuf;
287 BerElement *ber = (BerElement *)&berbuf;
289 if( reqdata == NULL ) {
293 if( reqdata->bv_len == 0 ) {
294 *text = "empty request data field";
295 return LDAP_PROTOCOL_ERROR;
298 /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
299 ber_init2( ber, reqdata, 0 );
301 tag = ber_scanf( ber, "{" /*}*/ );
303 if( tag == LBER_ERROR ) {
304 Debug( LDAP_DEBUG_TRACE,
305 "slap_passwd_parse: decoding error\n", 0, 0, 0 );
306 rc = LDAP_PROTOCOL_ERROR;
310 tag = ber_peek_tag( ber, &len );
311 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_ID ) {
313 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID not allowed.\n",
316 *text = "user must change own password";
317 rc = LDAP_UNWILLING_TO_PERFORM;
321 tag = ber_scanf( ber, "m", id );
323 if( tag == LBER_ERROR ) {
324 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
330 tag = ber_peek_tag( ber, &len );
333 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_OLD ) {
334 if( oldpass == NULL ) {
335 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD not allowed.\n",
338 *text = "use bind to verify old password";
339 rc = LDAP_UNWILLING_TO_PERFORM;
343 tag = ber_scanf( ber, "m", oldpass );
345 if( tag == LBER_ERROR ) {
346 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD parse failed.\n",
352 tag = ber_peek_tag( ber, &len );
355 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ) {
356 if( newpass == NULL ) {
357 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW not allowed.\n",
360 *text = "user specified passwords disallowed";
361 rc = LDAP_UNWILLING_TO_PERFORM;
365 tag = ber_scanf( ber, "m", newpass );
367 if( tag == LBER_ERROR ) {
368 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW parse failed.\n",
374 tag = ber_peek_tag( ber, &len );
379 Debug( LDAP_DEBUG_TRACE,
380 "slap_passwd_parse: decoding error, len=%ld\n",
383 *text = "data decoding error";
384 rc = LDAP_PROTOCOL_ERROR;
391 struct berval * slap_passwd_return(
392 struct berval *cred )
395 struct berval *bv = NULL;
396 BerElementBuffer berbuf;
397 /* opaque structure, size unknown but smaller than berbuf */
398 BerElement *ber = (BerElement *)&berbuf;
400 assert( cred != NULL );
402 Debug( LDAP_DEBUG_TRACE, "slap_passwd_return: %ld\n",
403 (long) cred->bv_len, 0, 0 );
405 ber_init_w_nullc( ber, LBER_USE_DER );
407 rc = ber_printf( ber, "{tON}",
408 LDAP_TAG_EXOP_MODIFY_PASSWD_GEN, cred );
411 (void) ber_flatten( ber, &bv );
420 * if "e" is provided, access to each value of the password is checked first
432 AccessControlState acl_state = ACL_STATE_INIT;
435 ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind,
436 op->o_conn->c_sasl_authctx, NULL );
439 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
440 /* if e is provided, check access */
441 if ( e && access_allowed( op, e, a->a_desc, bv,
442 ACL_AUTH, &acl_state ) == 0 )
447 if ( !lutil_passwd( bv, cred, NULL, text ) ) {
454 ldap_pvt_thread_pool_setkey( op->o_threadctx, slap_sasl_bind,
462 slap_passwd_generate( struct berval *pass )
464 Debug( LDAP_DEBUG_TRACE, "slap_passwd_generate\n", 0, 0, 0 );
469 * generate passwords of only 8 characters as some getpass(3)
470 * implementations truncate at 8 characters.
472 lutil_passwd_generate( pass, 8 );
476 slap_passwd_hash_type(
477 struct berval * cred,
485 assert( hash != NULL );
487 lutil_passwd_hash( cred , hash, new, text );
491 struct berval * cred,
496 if ( default_passwd_hash ) {
497 hash = default_passwd_hash[0];
500 hash = (char *)defhash[0];
503 slap_passwd_hash_type( cred, new, hash, text );
507 static ldap_pvt_thread_mutex_t passwd_mutex;
508 static lutil_cryptfunc slapd_crypt;
510 static int slapd_crypt( const char *key, const char *salt, char **hash )
515 ldap_pvt_thread_mutex_lock( &passwd_mutex );
517 cr = crypt( key, salt );
518 if ( cr == NULL || cr[0] == '\0' ) {
519 /* salt must have been invalid */
520 rc = LUTIL_PASSWD_ERR;
523 *hash = ber_strdup( cr );
524 rc = LUTIL_PASSWD_OK;
527 rc = strcmp( salt, cr ) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
531 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
534 #endif /* SLAPD_CRYPT */
536 void slap_passwd_init()
539 ldap_pvt_thread_mutex_init( &passwd_mutex );
540 lutil_cryptptr = slapd_crypt;