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 ) {
273 LDAP_LOG( OPERATION, ERR,
274 "slap_passwd_parse: decoding error\n", 0, 0, 0 );
276 Debug( LDAP_DEBUG_TRACE,
277 "slap_passwd_parse: decoding error\n", 0, 0, 0 );
279 rc = LDAP_PROTOCOL_ERROR;
283 tag = ber_peek_tag( ber, &len );
284 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_ID ) {
287 LDAP_LOG( OPERATION, ERR,
288 "slap_passwd_parse: ID not allowed.\n", 0, 0, 0 );
290 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID not allowed.\n",
294 *text = "user must change own password";
295 rc = LDAP_UNWILLING_TO_PERFORM;
299 tag = ber_scanf( ber, "m", id );
301 if( tag == LBER_ERROR ) {
303 LDAP_LOG( OPERATION, ERR,
304 "slap_passwd_parse: ID parse failed.\n", 0, 0, 0 );
306 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
313 tag = ber_peek_tag( ber, &len);
316 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_OLD ) {
317 if( oldpass == NULL ) {
319 LDAP_LOG( OPERATION, ERR,
320 "slap_passwd_parse: OLD not allowed.\n" , 0, 0, 0 );
322 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD not allowed.\n",
326 *text = "use bind to verify old password";
327 rc = LDAP_UNWILLING_TO_PERFORM;
331 tag = ber_scanf( ber, "m", oldpass );
333 if( tag == LBER_ERROR ) {
335 LDAP_LOG( OPERATION, ERR,
336 "slap_passwd_parse: ID parse failed.\n" , 0, 0, 0 );
338 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
345 tag = ber_peek_tag( ber, &len );
348 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ) {
349 if( newpass == NULL ) {
351 LDAP_LOG( OPERATION, ERR,
352 "slap_passwd_parse: NEW not allowed.\n", 0, 0, 0 );
354 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW not allowed.\n",
358 *text = "user specified passwords disallowed";
359 rc = LDAP_UNWILLING_TO_PERFORM;
363 tag = ber_scanf( ber, "m", newpass );
365 if( tag == LBER_ERROR ) {
367 LDAP_LOG( OPERATION, ERR,
368 "slap_passwd_parse: OLD parse failed.\n", 0, 0, 0 );
370 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD parse failed.\n",
377 tag = ber_peek_tag( ber, &len );
383 LDAP_LOG( OPERATION, ERR,
384 "slap_passwd_parse: decoding error, len=%ld\n", (long)len, 0, 0 );
386 Debug( LDAP_DEBUG_TRACE,
387 "slap_passwd_parse: decoding error, len=%ld\n",
391 *text = "data decoding error";
392 rc = LDAP_PROTOCOL_ERROR;
399 struct berval * slap_passwd_return(
400 struct berval *cred )
403 struct berval *bv = NULL;
404 BerElementBuffer berbuf;
405 /* opaque structure, size unknown but smaller than berbuf */
406 BerElement *ber = (BerElement *)&berbuf;
408 assert( cred != NULL );
411 LDAP_LOG( OPERATION, ENTRY,
412 "slap_passwd_return: %ld\n",(long)cred->bv_len, 0, 0 );
414 Debug( LDAP_DEBUG_TRACE, "slap_passwd_return: %ld\n",
415 (long) cred->bv_len, 0, 0 );
418 ber_init_w_nullc( ber, LBER_USE_DER );
420 rc = ber_printf( ber, "{tON}",
421 LDAP_TAG_EXOP_MODIFY_PASSWD_GEN, cred );
424 (void) ber_flatten( ber, &bv );
442 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
443 ldap_pvt_thread_mutex_lock( &passwd_mutex );
445 lutil_passwd_sasl_conn = conn->c_sasl_authctx;
449 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
450 if( !lutil_passwd( bv, cred, NULL, text ) ) {
456 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
458 lutil_passwd_sasl_conn = NULL;
460 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
467 slap_passwd_generate( struct berval *pass )
470 LDAP_LOG( OPERATION, ENTRY, "slap_passwd_generate: begin\n", 0, 0, 0 );
472 Debug( LDAP_DEBUG_TRACE, "slap_passwd_generate\n", 0, 0, 0 );
478 * generate passwords of only 8 characters as some getpass(3)
479 * implementations truncate at 8 characters.
481 lutil_passwd_generate( pass, 8 );
485 slap_passwd_hash_type(
486 struct berval * cred,
496 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
497 ldap_pvt_thread_mutex_lock( &passwd_mutex );
500 lutil_passwd_hash( cred , hash, new, text );
502 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
503 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
509 struct berval * cred,
514 if ( default_passwd_hash ) {
515 hash = default_passwd_hash[0];
518 hash = (char *)defhash[0];
521 slap_passwd_hash_type( cred, new, hash, text );