1 /* passwd.c - password extended operation routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2004 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, 0 );
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) {
117 #ifndef SLAPD_MULTIMASTER
118 /* This does not apply to multi-master case */
119 if(!( !SLAP_SHADOW( op->o_bd ) || be_isupdate( op ))) {
120 /* we SHOULD return a referral in this case */
121 BerVarray defref = op->o_bd->be_update_refs
122 ? op->o_bd->be_update_refs : default_referral;
124 if( defref != NULL ) {
125 rs->sr_ref = referral_rewrite( op->o_bd->be_update_refs,
126 NULL, NULL, LDAP_SCOPE_DEFAULT );
128 rs->sr_flags |= REP_REF_MUSTBEFREED;
137 rs->sr_text = "shadow context; no update referral";
138 rc = LDAP_UNWILLING_TO_PERFORM;
141 #endif /* !SLAPD_MULTIMASTER */
143 /* generate a new password if none was provided */
144 if ( qpw->rs_new.bv_len == 0 ) {
145 slap_passwd_generate( &qpw->rs_new );
146 if ( qpw->rs_new.bv_len ) {
147 rsp = slap_passwd_return( &qpw->rs_new );
150 if ( qpw->rs_new.bv_len == 0 ) {
151 rs->sr_text = "password generation failed";
156 /* Give the backend a chance to handle this itself */
157 if ( op->o_bd->be_extended ) {
158 rs->sr_err = op->o_bd->be_extended( op, rs );
159 if ( rs->sr_err != LDAP_UNWILLING_TO_PERFORM &&
160 rs->sr_err != SLAP_CB_CONTINUE ) {
166 /* The backend didn't handle it, so try it here */
167 if( op->o_bd && !op->o_bd->be_modify ) {
168 rs->sr_text = "operation not supported for current user";
169 rc = LDAP_UNWILLING_TO_PERFORM;
173 ml = ch_malloc( sizeof(Modifications) );
174 if ( !qpw->rs_modtail ) qpw->rs_modtail = &ml->sml_next;
176 if ( default_passwd_hash ) {
177 for ( nhash = 0; default_passwd_hash[nhash]; nhash++ );
178 hashes = default_passwd_hash;
181 hashes = (char **)defhash;
183 ml->sml_values = ch_malloc( (nhash+1)*sizeof(struct berval) );
184 for ( i=0; hashes[i]; i++ ) {
185 slap_passwd_hash_type( &qpw->rs_new, &hash, hashes[i], &rs->sr_text );
186 if ( hash.bv_len == 0 ) {
187 if ( !rs->sr_text ) {
188 rs->sr_text = "password hash failed";
192 ml->sml_values[i] = hash;
194 ml->sml_values[i].bv_val = NULL;
195 ml->sml_nvalues = NULL;
196 ml->sml_desc = slap_schema.si_ad_userPassword;
197 ml->sml_op = LDAP_MOD_REPLACE;
198 ml->sml_next = qpw->rs_mods;
202 rs->sr_err = LDAP_OTHER;
206 op2.o_tag = LDAP_REQ_MODIFY;
207 op2.o_callback = &cb2;
208 op2.orm_modlist = qpw->rs_mods;
209 cb2.sc_private = qpw; /* let Modify know this was pwdMod,
212 rs->sr_err = slap_mods_opattrs( &op2, ml, qpw->rs_modtail, &rs->sr_text,
215 if ( rs->sr_err == LDAP_SUCCESS ) {
216 rs->sr_err = op2.o_bd->be_modify( &op2, rs );
217 /* FIXME: in case it got rewritten... */
218 op->o_req_dn = op2.o_req_dn;
219 op->o_req_ndn = op2.o_req_ndn;
221 if ( rs->sr_err == LDAP_SUCCESS ) {
222 rs->sr_rspdata = rsp;
227 slap_mods_free( qpw->rs_mods );
229 free( qpw->rs_new.bv_val );
235 if ( !BER_BVISNULL( &op->o_req_dn ) ) {
236 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
238 if ( !BER_BVISNULL( &op->o_req_ndn ) ) {
239 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
245 int slap_passwd_parse( struct berval *reqdata,
247 struct berval *oldpass,
248 struct berval *newpass,
251 int rc = LDAP_SUCCESS;
254 BerElementBuffer berbuf;
255 BerElement *ber = (BerElement *)&berbuf;
257 if( reqdata == NULL ) {
261 if( reqdata->bv_len == 0 ) {
262 *text = "empty request data field";
263 return LDAP_PROTOCOL_ERROR;
266 /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
267 ber_init2( ber, reqdata, 0 );
269 tag = ber_scanf( ber, "{" /*}*/ );
271 if( tag == LBER_ERROR ) {
272 Debug( LDAP_DEBUG_TRACE,
273 "slap_passwd_parse: decoding error\n", 0, 0, 0 );
274 rc = LDAP_PROTOCOL_ERROR;
278 tag = ber_peek_tag( ber, &len );
279 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_ID ) {
281 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID not allowed.\n",
284 *text = "user must change own password";
285 rc = LDAP_UNWILLING_TO_PERFORM;
289 tag = ber_scanf( ber, "m", id );
291 if( tag == LBER_ERROR ) {
292 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
298 tag = ber_peek_tag( ber, &len);
301 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_OLD ) {
302 if( oldpass == NULL ) {
303 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD not allowed.\n",
306 *text = "use bind to verify old password";
307 rc = LDAP_UNWILLING_TO_PERFORM;
311 tag = ber_scanf( ber, "m", oldpass );
313 if( tag == LBER_ERROR ) {
314 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
320 tag = ber_peek_tag( ber, &len );
323 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ) {
324 if( newpass == NULL ) {
325 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW not allowed.\n",
328 *text = "user specified passwords disallowed";
329 rc = LDAP_UNWILLING_TO_PERFORM;
333 tag = ber_scanf( ber, "m", newpass );
335 if( tag == LBER_ERROR ) {
336 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD parse failed.\n",
342 tag = ber_peek_tag( ber, &len );
347 Debug( LDAP_DEBUG_TRACE,
348 "slap_passwd_parse: decoding error, len=%ld\n",
351 *text = "data decoding error";
352 rc = LDAP_PROTOCOL_ERROR;
359 struct berval * slap_passwd_return(
360 struct berval *cred )
363 struct berval *bv = NULL;
364 BerElementBuffer berbuf;
365 /* opaque structure, size unknown but smaller than berbuf */
366 BerElement *ber = (BerElement *)&berbuf;
368 assert( cred != NULL );
370 Debug( LDAP_DEBUG_TRACE, "slap_passwd_return: %ld\n",
371 (long) cred->bv_len, 0, 0 );
373 ber_init_w_nullc( ber, LBER_USE_DER );
375 rc = ber_printf( ber, "{tON}",
376 LDAP_TAG_EXOP_MODIFY_PASSWD_GEN, cred );
379 (void) ber_flatten( ber, &bv );
397 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
398 ldap_pvt_thread_mutex_lock( &passwd_mutex );
400 lutil_passwd_sasl_conn = conn->c_sasl_authctx;
404 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
405 if( !lutil_passwd( bv, cred, NULL, text ) ) {
411 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
413 lutil_passwd_sasl_conn = NULL;
415 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
422 slap_passwd_generate( struct berval *pass )
424 Debug( LDAP_DEBUG_TRACE, "slap_passwd_generate\n", 0, 0, 0 );
429 * generate passwords of only 8 characters as some getpass(3)
430 * implementations truncate at 8 characters.
432 lutil_passwd_generate( pass, 8 );
436 slap_passwd_hash_type(
437 struct berval * cred,
447 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
448 ldap_pvt_thread_mutex_lock( &passwd_mutex );
451 lutil_passwd_hash( cred , hash, new, text );
453 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
454 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
460 struct berval * cred,
465 if ( default_passwd_hash ) {
466 hash = default_passwd_hash[0];
469 hash = (char *)defhash[0];
472 slap_passwd_hash_type( cred, new, hash, text );