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 };
54 assert( ber_bvcmp( &slap_EXOP_MODIFY_PASSWD, &op->ore_reqoid ) == 0 );
56 if( op->o_dn.bv_len == 0 ) {
57 rs->sr_text = "only authenticated users may change passwords";
58 return LDAP_STRONG_AUTH_REQUIRED;
61 qpw->rs_old.bv_val = NULL;
62 qpw->rs_new.bv_val = NULL;
64 qpw->rs_modtail = NULL;
66 rs->sr_err = slap_passwd_parse( op->ore_reqdata, &id, &qpw->rs_old,
67 &qpw->rs_new, &rs->sr_text );
69 if ( rs->sr_err != LDAP_SUCCESS ) {
75 /* ndn is in tmpmem, so we don't need to free it */
76 rs->sr_err = dnNormalize( 0, NULL, NULL, &id, &op->o_req_ndn, op->o_tmpmemctx );
77 if ( rs->sr_err != LDAP_SUCCESS ) {
78 rs->sr_text = "Invalid DN";
81 op->o_bd = select_backend( &op->o_req_ndn, 0, 0 );
83 op->o_req_dn = op->o_dn;
84 op->o_req_ndn = op->o_ndn;
85 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
86 op->o_bd = op->o_conn->c_authz_backend;
87 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
90 if( op->o_bd == NULL ) {
91 #ifdef HAVE_CYRUS_SASL
92 return slap_sasl_setpass( op, rs );
94 rs->sr_text = "no authz backend";
99 if ( op->o_req_ndn.bv_len == 0 ) {
100 rs->sr_text = "no password is associated with the Root DSE";
101 return LDAP_UNWILLING_TO_PERFORM;
104 if (backend_check_restrictions( op, rs,
105 (struct berval *)&slap_EXOP_MODIFY_PASSWD ) != LDAP_SUCCESS) {
110 #ifndef SLAPD_MULTIMASTER
111 /* This does not apply to multi-master case */
112 if( op->o_bd->be_update_ndn.bv_len ) {
113 /* we SHOULD return a referral in this case */
114 BerVarray defref = NULL;
115 if ( !LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
117 LDAP_STAILQ_FOREACH( si, &op->o_bd->be_syncinfo, si_next ) {
119 ber_dupbv( &tmpbv, &si->si_provideruri_bv[0] );
120 ber_bvarray_add( &defref, &tmpbv );
123 defref = referral_rewrite( op->o_bd->be_update_refs,
124 NULL, NULL, LDAP_SCOPE_DEFAULT );
127 return LDAP_REFERRAL;
129 #endif /* !SLAPD_MULTIMASTER */
131 /* generate a new password if none was provided */
132 if ( qpw->rs_new.bv_len == 0 ) {
133 slap_passwd_generate( &qpw->rs_new );
134 if ( qpw->rs_new.bv_len ) {
135 rsp = slap_passwd_return( &qpw->rs_new );
138 if ( qpw->rs_new.bv_len == 0 ) {
139 rs->sr_text = "password generation failed";
143 /* Give the backend a chance to handle this itself */
144 if ( op->o_bd->be_extended ) {
145 rs->sr_err = op->o_bd->be_extended( op, rs );
146 if ( rs->sr_err != LDAP_UNWILLING_TO_PERFORM &&
147 rs->sr_err != SLAP_CB_CONTINUE ) {
152 /* The backend didn't handle it, so try it here */
153 if( op->o_bd && !op->o_bd->be_modify ) {
154 rs->sr_text = "operation not supported for current user";
155 return LDAP_UNWILLING_TO_PERFORM;
158 ml = ch_malloc( sizeof(Modifications) );
159 if ( !qpw->rs_modtail ) qpw->rs_modtail = &ml->sml_next;
161 if ( default_passwd_hash ) {
162 for ( nhash = 0; default_passwd_hash[nhash]; nhash++ );
163 hashes = default_passwd_hash;
166 hashes = (char **)defhash;
168 ml->sml_values = ch_malloc( (nhash+1)*sizeof(struct berval) );
169 for ( i=0; hashes[i]; i++ ) {
170 slap_passwd_hash( hashes[i], &qpw->rs_new, &hash, &rs->sr_text );
171 if ( hash.bv_len == 0 ) {
172 if ( !rs->sr_text ) {
173 rs->sr_text = "password hash failed";
177 ml->sml_values[i] = hash;
179 ml->sml_values[i].bv_val = NULL;
180 ml->sml_nvalues = NULL;
181 ml->sml_desc = slap_schema.si_ad_userPassword;
182 ml->sml_op = LDAP_MOD_REPLACE;
183 ml->sml_next = qpw->rs_mods;
186 free( qpw->rs_new.bv_val );
189 rs->sr_err = LDAP_OTHER;
193 op2.o_tag = LDAP_REQ_MODIFY;
194 op2.o_callback = &cb2;
195 op2.orm_modlist = qpw->rs_mods;
197 rs->sr_err = slap_mods_opattrs( &op2, ml, qpw->rs_modtail, &rs->sr_text,
200 if ( rs->sr_err == LDAP_SUCCESS ) {
201 rs->sr_err = op2.o_bd->be_modify( &op2, rs );
203 if ( rs->sr_err == LDAP_SUCCESS ) {
204 rs->sr_rspdata = rsp;
209 slap_mods_free( qpw->rs_mods );
214 int slap_passwd_parse( struct berval *reqdata,
216 struct berval *oldpass,
217 struct berval *newpass,
220 int rc = LDAP_SUCCESS;
223 BerElementBuffer berbuf;
224 BerElement *ber = (BerElement *)&berbuf;
226 if( reqdata == NULL ) {
230 if( reqdata->bv_len == 0 ) {
231 *text = "empty request data field";
232 return LDAP_PROTOCOL_ERROR;
235 /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
236 ber_init2( ber, reqdata, 0 );
238 tag = ber_scanf( ber, "{" /*}*/ );
240 if( tag == LBER_ERROR ) {
242 LDAP_LOG( OPERATION, ERR,
243 "slap_passwd_parse: decoding error\n", 0, 0, 0 );
245 Debug( LDAP_DEBUG_TRACE,
246 "slap_passwd_parse: decoding error\n", 0, 0, 0 );
248 rc = LDAP_PROTOCOL_ERROR;
252 tag = ber_peek_tag( ber, &len );
253 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_ID ) {
256 LDAP_LOG( OPERATION, ERR,
257 "slap_passwd_parse: ID not allowed.\n", 0, 0, 0 );
259 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID not allowed.\n",
263 *text = "user must change own password";
264 rc = LDAP_UNWILLING_TO_PERFORM;
268 tag = ber_scanf( ber, "m", id );
270 if( tag == LBER_ERROR ) {
272 LDAP_LOG( OPERATION, ERR,
273 "slap_passwd_parse: ID parse failed.\n", 0, 0, 0 );
275 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
282 tag = ber_peek_tag( ber, &len);
285 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_OLD ) {
286 if( oldpass == NULL ) {
288 LDAP_LOG( OPERATION, ERR,
289 "slap_passwd_parse: OLD not allowed.\n" , 0, 0, 0 );
291 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD not allowed.\n",
295 *text = "use bind to verify old password";
296 rc = LDAP_UNWILLING_TO_PERFORM;
300 tag = ber_scanf( ber, "m", oldpass );
302 if( tag == LBER_ERROR ) {
304 LDAP_LOG( OPERATION, ERR,
305 "slap_passwd_parse: ID parse failed.\n" , 0, 0, 0 );
307 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
314 tag = ber_peek_tag( ber, &len );
317 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ) {
318 if( newpass == NULL ) {
320 LDAP_LOG( OPERATION, ERR,
321 "slap_passwd_parse: NEW not allowed.\n", 0, 0, 0 );
323 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW not allowed.\n",
327 *text = "user specified passwords disallowed";
328 rc = LDAP_UNWILLING_TO_PERFORM;
332 tag = ber_scanf( ber, "m", newpass );
334 if( tag == LBER_ERROR ) {
336 LDAP_LOG( OPERATION, ERR,
337 "slap_passwd_parse: OLD parse failed.\n", 0, 0, 0 );
339 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD parse failed.\n",
346 tag = ber_peek_tag( ber, &len );
352 LDAP_LOG( OPERATION, ERR,
353 "slap_passwd_parse: decoding error, len=%ld\n", (long)len, 0, 0 );
355 Debug( LDAP_DEBUG_TRACE,
356 "slap_passwd_parse: decoding error, len=%ld\n",
360 *text = "data decoding error";
361 rc = LDAP_PROTOCOL_ERROR;
368 struct berval * slap_passwd_return(
369 struct berval *cred )
372 struct berval *bv = NULL;
373 BerElementBuffer berbuf;
374 /* opaque structure, size unknown but smaller than berbuf */
375 BerElement *ber = (BerElement *)&berbuf;
377 assert( cred != NULL );
380 LDAP_LOG( OPERATION, ENTRY,
381 "slap_passwd_return: %ld\n",(long)cred->bv_len, 0, 0 );
383 Debug( LDAP_DEBUG_TRACE, "slap_passwd_return: %ld\n",
384 (long) cred->bv_len, 0, 0 );
387 ber_init_w_nullc( ber, LBER_USE_DER );
389 rc = ber_printf( ber, "{tON}",
390 LDAP_TAG_EXOP_MODIFY_PASSWD_GEN, cred );
393 (void) ber_flatten( ber, &bv );
411 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
412 ldap_pvt_thread_mutex_lock( &passwd_mutex );
414 lutil_passwd_sasl_conn = conn->c_sasl_authctx;
418 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
419 if( !lutil_passwd( bv, cred, NULL, text ) ) {
425 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
427 lutil_passwd_sasl_conn = NULL;
429 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
436 slap_passwd_generate( struct berval *pass )
439 LDAP_LOG( OPERATION, ENTRY, "slap_passwd_generate: begin\n", 0, 0, 0 );
441 Debug( LDAP_DEBUG_TRACE, "slap_passwd_generate\n", 0, 0, 0 );
447 * generate passwords of only 8 characters as some getpass(3)
448 * implementations truncate at 8 characters.
450 lutil_passwd_generate( pass, 8 );
456 struct berval * cred,
463 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
464 ldap_pvt_thread_mutex_lock( &passwd_mutex );
467 lutil_passwd_hash( cred , hash, new, text );
469 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
470 ldap_pvt_thread_mutex_unlock( &passwd_mutex );