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 };
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 ) {
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";
83 op->o_bd = select_backend( &op->o_req_ndn, 0, 0 );
85 op->o_req_dn = op->o_dn;
86 op->o_req_ndn = op->o_ndn;
87 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
88 op->o_bd = op->o_conn->c_authz_backend;
89 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
92 if( op->o_bd == NULL ) {
93 #ifdef HAVE_CYRUS_SASL
94 return slap_sasl_setpass( op, rs );
96 rs->sr_text = "no authz backend";
101 if ( op->o_req_ndn.bv_len == 0 ) {
102 rs->sr_text = "no password is associated with the Root DSE";
103 return LDAP_UNWILLING_TO_PERFORM;
106 if (backend_check_restrictions( op, rs,
107 (struct berval *)&slap_EXOP_MODIFY_PASSWD ) != LDAP_SUCCESS) {
112 #ifndef SLAPD_MULTIMASTER
113 /* This does not apply to multi-master case */
114 if(!( !SLAP_SHADOW( op->o_bd ) || be_isupdate( op ))) {
115 /* we SHOULD return a referral in this case */
116 BerVarray defref = op->o_bd->be_update_refs
117 ? op->o_bd->be_update_refs : default_referral;
119 if( defref != NULL ) {
120 rs->sr_ref = referral_rewrite( op->o_bd->be_update_refs,
121 NULL, NULL, LDAP_SCOPE_DEFAULT );
123 rs->sr_flags |= REP_REF_MUSTBEFREED;
127 return LDAP_REFERRAL;
131 rs->sr_text = "shadow context; no update referral";
132 return LDAP_UNWILLING_TO_PERFORM;
134 #endif /* !SLAPD_MULTIMASTER */
136 /* generate a new password if none was provided */
137 if ( qpw->rs_new.bv_len == 0 ) {
138 slap_passwd_generate( &qpw->rs_new );
139 if ( qpw->rs_new.bv_len ) {
140 rsp = slap_passwd_return( &qpw->rs_new );
143 if ( qpw->rs_new.bv_len == 0 ) {
144 rs->sr_text = "password generation failed";
148 /* Give the backend a chance to handle this itself */
149 if ( op->o_bd->be_extended ) {
150 rs->sr_err = op->o_bd->be_extended( op, rs );
151 if ( rs->sr_err != LDAP_UNWILLING_TO_PERFORM &&
152 rs->sr_err != SLAP_CB_CONTINUE ) {
157 /* The backend didn't handle it, so try it here */
158 if( op->o_bd && !op->o_bd->be_modify ) {
159 rs->sr_text = "operation not supported for current user";
160 return LDAP_UNWILLING_TO_PERFORM;
163 ml = ch_malloc( sizeof(Modifications) );
164 if ( !qpw->rs_modtail ) qpw->rs_modtail = &ml->sml_next;
166 if ( default_passwd_hash ) {
167 for ( nhash = 0; default_passwd_hash[nhash]; nhash++ );
168 hashes = default_passwd_hash;
171 hashes = (char **)defhash;
173 ml->sml_values = ch_malloc( (nhash+1)*sizeof(struct berval) );
174 for ( i=0; hashes[i]; i++ ) {
175 slap_passwd_hash_type( &qpw->rs_new, &hash, hashes[i], &rs->sr_text );
176 if ( hash.bv_len == 0 ) {
177 if ( !rs->sr_text ) {
178 rs->sr_text = "password hash failed";
182 ml->sml_values[i] = hash;
184 ml->sml_values[i].bv_val = NULL;
185 ml->sml_nvalues = NULL;
186 ml->sml_desc = slap_schema.si_ad_userPassword;
187 ml->sml_op = LDAP_MOD_REPLACE;
188 ml->sml_next = qpw->rs_mods;
192 rs->sr_err = LDAP_OTHER;
194 slap_callback *sc = op->o_callback;
196 op->o_tag = LDAP_REQ_MODIFY;
197 op->o_callback = &cb2;
198 op->orm_modlist = qpw->rs_mods;
199 cb2.sc_private = qpw; /* let Modify know this was pwdMod,
202 rs->sr_err = slap_mods_opattrs( op, ml, qpw->rs_modtail, &rs->sr_text,
205 if ( rs->sr_err == LDAP_SUCCESS ) {
206 rs->sr_err = op->o_bd->be_modify( op, rs );
208 if ( rs->sr_err == LDAP_SUCCESS ) {
209 rs->sr_rspdata = rsp;
213 op->o_tag = LDAP_REQ_EXTENDED;
216 slap_mods_free( qpw->rs_mods );
218 free( qpw->rs_new.bv_val );
221 op->oq_extended = qext;
225 int slap_passwd_parse( struct berval *reqdata,
227 struct berval *oldpass,
228 struct berval *newpass,
231 int rc = LDAP_SUCCESS;
234 BerElementBuffer berbuf;
235 BerElement *ber = (BerElement *)&berbuf;
237 if( reqdata == NULL ) {
241 if( reqdata->bv_len == 0 ) {
242 *text = "empty request data field";
243 return LDAP_PROTOCOL_ERROR;
246 /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
247 ber_init2( ber, reqdata, 0 );
249 tag = ber_scanf( ber, "{" /*}*/ );
251 if( tag == LBER_ERROR ) {
253 LDAP_LOG( OPERATION, ERR,
254 "slap_passwd_parse: decoding error\n", 0, 0, 0 );
256 Debug( LDAP_DEBUG_TRACE,
257 "slap_passwd_parse: decoding error\n", 0, 0, 0 );
259 rc = LDAP_PROTOCOL_ERROR;
263 tag = ber_peek_tag( ber, &len );
264 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_ID ) {
267 LDAP_LOG( OPERATION, ERR,
268 "slap_passwd_parse: ID not allowed.\n", 0, 0, 0 );
270 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID not allowed.\n",
274 *text = "user must change own password";
275 rc = LDAP_UNWILLING_TO_PERFORM;
279 tag = ber_scanf( ber, "m", id );
281 if( tag == LBER_ERROR ) {
283 LDAP_LOG( OPERATION, ERR,
284 "slap_passwd_parse: ID parse failed.\n", 0, 0, 0 );
286 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
293 tag = ber_peek_tag( ber, &len);
296 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_OLD ) {
297 if( oldpass == NULL ) {
299 LDAP_LOG( OPERATION, ERR,
300 "slap_passwd_parse: OLD not allowed.\n" , 0, 0, 0 );
302 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 ) {
315 LDAP_LOG( OPERATION, ERR,
316 "slap_passwd_parse: ID parse failed.\n" , 0, 0, 0 );
318 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
325 tag = ber_peek_tag( ber, &len );
328 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ) {
329 if( newpass == NULL ) {
331 LDAP_LOG( OPERATION, ERR,
332 "slap_passwd_parse: NEW not allowed.\n", 0, 0, 0 );
334 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW not allowed.\n",
338 *text = "user specified passwords disallowed";
339 rc = LDAP_UNWILLING_TO_PERFORM;
343 tag = ber_scanf( ber, "m", newpass );
345 if( tag == LBER_ERROR ) {
347 LDAP_LOG( OPERATION, ERR,
348 "slap_passwd_parse: OLD parse failed.\n", 0, 0, 0 );
350 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD parse failed.\n",
357 tag = ber_peek_tag( ber, &len );
363 LDAP_LOG( OPERATION, ERR,
364 "slap_passwd_parse: decoding error, len=%ld\n", (long)len, 0, 0 );
366 Debug( LDAP_DEBUG_TRACE,
367 "slap_passwd_parse: decoding error, len=%ld\n",
371 *text = "data decoding error";
372 rc = LDAP_PROTOCOL_ERROR;
379 struct berval * slap_passwd_return(
380 struct berval *cred )
383 struct berval *bv = NULL;
384 BerElementBuffer berbuf;
385 /* opaque structure, size unknown but smaller than berbuf */
386 BerElement *ber = (BerElement *)&berbuf;
388 assert( cred != NULL );
391 LDAP_LOG( OPERATION, ENTRY,
392 "slap_passwd_return: %ld\n",(long)cred->bv_len, 0, 0 );
394 Debug( LDAP_DEBUG_TRACE, "slap_passwd_return: %ld\n",
395 (long) cred->bv_len, 0, 0 );
398 ber_init_w_nullc( ber, LBER_USE_DER );
400 rc = ber_printf( ber, "{tON}",
401 LDAP_TAG_EXOP_MODIFY_PASSWD_GEN, cred );
404 (void) ber_flatten( ber, &bv );
422 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
423 ldap_pvt_thread_mutex_lock( &passwd_mutex );
425 lutil_passwd_sasl_conn = conn->c_sasl_authctx;
429 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
430 if( !lutil_passwd( bv, cred, NULL, text ) ) {
436 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
438 lutil_passwd_sasl_conn = NULL;
440 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
447 slap_passwd_generate( struct berval *pass )
450 LDAP_LOG( OPERATION, ENTRY, "slap_passwd_generate: begin\n", 0, 0, 0 );
452 Debug( LDAP_DEBUG_TRACE, "slap_passwd_generate\n", 0, 0, 0 );
458 * generate passwords of only 8 characters as some getpass(3)
459 * implementations truncate at 8 characters.
461 lutil_passwd_generate( pass, 8 );
465 slap_passwd_hash_type(
466 struct berval * cred,
476 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
477 ldap_pvt_thread_mutex_lock( &passwd_mutex );
480 lutil_passwd_hash( cred , hash, new, text );
482 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
483 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
489 struct berval * cred,
494 if ( default_passwd_hash ) {
495 hash = default_passwd_hash[0];
498 hash = (char *)defhash[0];
501 slap_passwd_hash_type( cred, new, hash, text );