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 };
55 assert( ber_bvcmp( &slap_EXOP_MODIFY_PASSWD, &op->ore_reqoid ) == 0 );
57 if( op->o_dn.bv_len == 0 ) {
58 rs->sr_text = "only authenticated users may change passwords";
59 return LDAP_STRONG_AUTH_REQUIRED;
62 qpw->rs_old.bv_val = NULL;
63 qpw->rs_new.bv_val = NULL;
65 qpw->rs_modtail = NULL;
67 rs->sr_err = slap_passwd_parse( op->ore_reqdata, &id, &qpw->rs_old,
68 &qpw->rs_new, &rs->sr_text );
70 if ( rs->sr_err != LDAP_SUCCESS ) {
76 /* ndn is in tmpmem, so we don't need to free it */
77 rs->sr_err = dnNormalize( 0, NULL, NULL, &id, &op->o_req_ndn, op->o_tmpmemctx );
78 if ( rs->sr_err != LDAP_SUCCESS ) {
79 rs->sr_text = "Invalid DN";
82 op->o_bd = select_backend( &op->o_req_ndn, 0, 0 );
84 op->o_req_dn = op->o_dn;
85 op->o_req_ndn = op->o_ndn;
86 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
87 op->o_bd = op->o_conn->c_authz_backend;
88 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
91 if( op->o_bd == NULL ) {
92 #ifdef HAVE_CYRUS_SASL
93 return slap_sasl_setpass( op, rs );
95 rs->sr_text = "no authz backend";
100 if ( op->o_req_ndn.bv_len == 0 ) {
101 rs->sr_text = "no password is associated with the Root DSE";
102 return LDAP_UNWILLING_TO_PERFORM;
105 if (backend_check_restrictions( op, rs,
106 (struct berval *)&slap_EXOP_MODIFY_PASSWD ) != LDAP_SUCCESS) {
111 #ifndef SLAPD_MULTIMASTER
112 /* This does not apply to multi-master case */
113 if( op->o_bd->be_update_ndn.bv_len ) {
114 /* we SHOULD return a referral in this case */
115 BerVarray defref = NULL;
116 if ( !LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
118 LDAP_STAILQ_FOREACH( si, &op->o_bd->be_syncinfo, si_next ) {
120 ber_dupbv( &tmpbv, &si->si_provideruri_bv[0] );
121 ber_bvarray_add( &defref, &tmpbv );
124 defref = referral_rewrite( op->o_bd->be_update_refs,
125 NULL, NULL, LDAP_SCOPE_DEFAULT );
128 return LDAP_REFERRAL;
130 #endif /* !SLAPD_MULTIMASTER */
132 /* generate a new password if none was provided */
133 if ( qpw->rs_new.bv_len == 0 ) {
134 slap_passwd_generate( &qpw->rs_new );
135 if ( qpw->rs_new.bv_len ) {
136 rsp = slap_passwd_return( &qpw->rs_new );
139 if ( qpw->rs_new.bv_len == 0 ) {
140 rs->sr_text = "password generation failed";
144 /* Give the backend a chance to handle this itself */
145 if ( op->o_bd->be_extended ) {
146 rs->sr_err = op->o_bd->be_extended( op, rs );
147 if ( rs->sr_err != LDAP_UNWILLING_TO_PERFORM &&
148 rs->sr_err != SLAP_CB_CONTINUE ) {
153 /* The backend didn't handle it, so try it here */
154 if( op->o_bd && !op->o_bd->be_modify ) {
155 rs->sr_text = "operation not supported for current user";
156 return LDAP_UNWILLING_TO_PERFORM;
159 ml = ch_malloc( sizeof(Modifications) );
160 if ( !qpw->rs_modtail ) qpw->rs_modtail = &ml->sml_next;
162 if ( default_passwd_hash ) {
163 for ( nhash = 0; default_passwd_hash[nhash]; nhash++ );
164 hashes = default_passwd_hash;
167 hashes = (char **)defhash;
169 ml->sml_values = ch_malloc( (nhash+1)*sizeof(struct berval) );
170 for ( i=0; hashes[i]; i++ ) {
171 slap_passwd_hash( hashes[i], &qpw->rs_new, &hash, &rs->sr_text );
172 if ( hash.bv_len == 0 ) {
173 if ( !rs->sr_text ) {
174 rs->sr_text = "password hash failed";
178 ml->sml_values[i] = hash;
180 ml->sml_values[i].bv_val = NULL;
181 ml->sml_nvalues = NULL;
182 ml->sml_desc = slap_schema.si_ad_userPassword;
183 ml->sml_op = LDAP_MOD_REPLACE;
184 ml->sml_next = qpw->rs_mods;
187 free( qpw->rs_new.bv_val );
190 rs->sr_err = LDAP_OTHER;
194 op2.o_tag = LDAP_REQ_MODIFY;
195 op2.o_callback = &cb2;
196 op2.orm_modlist = qpw->rs_mods;
198 rs->sr_err = slap_mods_opattrs( &op2, ml, qpw->rs_modtail, &rs->sr_text,
201 if ( rs->sr_err == LDAP_SUCCESS ) {
202 rs->sr_err = op2.o_bd->be_modify( &op2, rs );
204 if ( rs->sr_err == LDAP_SUCCESS ) {
205 rs->sr_rspdata = rsp;
210 slap_mods_free( qpw->rs_mods );
215 int slap_passwd_parse( struct berval *reqdata,
217 struct berval *oldpass,
218 struct berval *newpass,
221 int rc = LDAP_SUCCESS;
224 BerElementBuffer berbuf;
225 BerElement *ber = (BerElement *)&berbuf;
227 if( reqdata == NULL ) {
231 if( reqdata->bv_len == 0 ) {
232 *text = "empty request data field";
233 return LDAP_PROTOCOL_ERROR;
236 /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
237 ber_init2( ber, reqdata, 0 );
239 tag = ber_scanf( ber, "{" /*}*/ );
241 if( tag == LBER_ERROR ) {
243 LDAP_LOG( OPERATION, ERR,
244 "slap_passwd_parse: decoding error\n", 0, 0, 0 );
246 Debug( LDAP_DEBUG_TRACE,
247 "slap_passwd_parse: decoding error\n", 0, 0, 0 );
249 rc = LDAP_PROTOCOL_ERROR;
253 tag = ber_peek_tag( ber, &len );
254 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_ID ) {
257 LDAP_LOG( OPERATION, ERR,
258 "slap_passwd_parse: ID not allowed.\n", 0, 0, 0 );
260 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID not allowed.\n",
264 *text = "user must change own password";
265 rc = LDAP_UNWILLING_TO_PERFORM;
269 tag = ber_scanf( ber, "m", id );
271 if( tag == LBER_ERROR ) {
273 LDAP_LOG( OPERATION, ERR,
274 "slap_passwd_parse: ID parse failed.\n", 0, 0, 0 );
276 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
283 tag = ber_peek_tag( ber, &len);
286 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_OLD ) {
287 if( oldpass == NULL ) {
289 LDAP_LOG( OPERATION, ERR,
290 "slap_passwd_parse: OLD not allowed.\n" , 0, 0, 0 );
292 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD not allowed.\n",
296 *text = "use bind to verify old password";
297 rc = LDAP_UNWILLING_TO_PERFORM;
301 tag = ber_scanf( ber, "m", oldpass );
303 if( tag == LBER_ERROR ) {
305 LDAP_LOG( OPERATION, ERR,
306 "slap_passwd_parse: ID parse failed.\n" , 0, 0, 0 );
308 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
315 tag = ber_peek_tag( ber, &len );
318 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ) {
319 if( newpass == NULL ) {
321 LDAP_LOG( OPERATION, ERR,
322 "slap_passwd_parse: NEW not allowed.\n", 0, 0, 0 );
324 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 ) {
337 LDAP_LOG( OPERATION, ERR,
338 "slap_passwd_parse: OLD parse failed.\n", 0, 0, 0 );
340 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD parse failed.\n",
347 tag = ber_peek_tag( ber, &len );
353 LDAP_LOG( OPERATION, ERR,
354 "slap_passwd_parse: decoding error, len=%ld\n", (long)len, 0, 0 );
356 Debug( LDAP_DEBUG_TRACE,
357 "slap_passwd_parse: decoding error, len=%ld\n",
361 *text = "data decoding error";
362 rc = LDAP_PROTOCOL_ERROR;
369 struct berval * slap_passwd_return(
370 struct berval *cred )
373 struct berval *bv = NULL;
374 BerElementBuffer berbuf;
375 /* opaque structure, size unknown but smaller than berbuf */
376 BerElement *ber = (BerElement *)&berbuf;
378 assert( cred != NULL );
381 LDAP_LOG( OPERATION, ENTRY,
382 "slap_passwd_return: %ld\n",(long)cred->bv_len, 0, 0 );
384 Debug( LDAP_DEBUG_TRACE, "slap_passwd_return: %ld\n",
385 (long) cred->bv_len, 0, 0 );
388 ber_init_w_nullc( ber, LBER_USE_DER );
390 rc = ber_printf( ber, "{tON}",
391 LDAP_TAG_EXOP_MODIFY_PASSWD_GEN, cred );
394 (void) ber_flatten( ber, &bv );
412 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
413 ldap_pvt_thread_mutex_lock( &passwd_mutex );
415 lutil_passwd_sasl_conn = conn->c_sasl_authctx;
419 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
420 if( !lutil_passwd( bv, cred, NULL, text ) ) {
426 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
428 lutil_passwd_sasl_conn = NULL;
430 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
437 slap_passwd_generate( struct berval *pass )
440 LDAP_LOG( OPERATION, ENTRY, "slap_passwd_generate: begin\n", 0, 0, 0 );
442 Debug( LDAP_DEBUG_TRACE, "slap_passwd_generate\n", 0, 0, 0 );
448 * generate passwords of only 8 characters as some getpass(3)
449 * implementations truncate at 8 characters.
451 lutil_passwd_generate( pass, 8 );
457 struct berval * cred,
464 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
465 ldap_pvt_thread_mutex_lock( &passwd_mutex );
468 lutil_passwd_hash( cred , hash, new, text );
470 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
471 ldap_pvt_thread_mutex_unlock( &passwd_mutex );