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_type( &qpw->rs_new, &hash, hashes[i], &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;
188 rs->sr_err = LDAP_OTHER;
192 op2.o_tag = LDAP_REQ_MODIFY;
193 op2.o_callback = &cb2;
194 op2.orm_modlist = qpw->rs_mods;
195 cb2.sc_private = qpw; /* let Modify know this was pwdMod,
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 );
212 free( qpw->rs_new.bv_val );
218 int slap_passwd_parse( struct berval *reqdata,
220 struct berval *oldpass,
221 struct berval *newpass,
224 int rc = LDAP_SUCCESS;
227 BerElementBuffer berbuf;
228 BerElement *ber = (BerElement *)&berbuf;
230 if( reqdata == NULL ) {
234 if( reqdata->bv_len == 0 ) {
235 *text = "empty request data field";
236 return LDAP_PROTOCOL_ERROR;
239 /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
240 ber_init2( ber, reqdata, 0 );
242 tag = ber_scanf( ber, "{" /*}*/ );
244 if( tag == LBER_ERROR ) {
246 LDAP_LOG( OPERATION, ERR,
247 "slap_passwd_parse: decoding error\n", 0, 0, 0 );
249 Debug( LDAP_DEBUG_TRACE,
250 "slap_passwd_parse: decoding error\n", 0, 0, 0 );
252 rc = LDAP_PROTOCOL_ERROR;
256 tag = ber_peek_tag( ber, &len );
257 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_ID ) {
260 LDAP_LOG( OPERATION, ERR,
261 "slap_passwd_parse: ID not allowed.\n", 0, 0, 0 );
263 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID not allowed.\n",
267 *text = "user must change own password";
268 rc = LDAP_UNWILLING_TO_PERFORM;
272 tag = ber_scanf( ber, "m", id );
274 if( tag == LBER_ERROR ) {
276 LDAP_LOG( OPERATION, ERR,
277 "slap_passwd_parse: ID parse failed.\n", 0, 0, 0 );
279 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
286 tag = ber_peek_tag( ber, &len);
289 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_OLD ) {
290 if( oldpass == NULL ) {
292 LDAP_LOG( OPERATION, ERR,
293 "slap_passwd_parse: OLD not allowed.\n" , 0, 0, 0 );
295 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD not allowed.\n",
299 *text = "use bind to verify old password";
300 rc = LDAP_UNWILLING_TO_PERFORM;
304 tag = ber_scanf( ber, "m", oldpass );
306 if( tag == LBER_ERROR ) {
308 LDAP_LOG( OPERATION, ERR,
309 "slap_passwd_parse: ID parse failed.\n" , 0, 0, 0 );
311 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
318 tag = ber_peek_tag( ber, &len );
321 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ) {
322 if( newpass == NULL ) {
324 LDAP_LOG( OPERATION, ERR,
325 "slap_passwd_parse: NEW not allowed.\n", 0, 0, 0 );
327 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW not allowed.\n",
331 *text = "user specified passwords disallowed";
332 rc = LDAP_UNWILLING_TO_PERFORM;
336 tag = ber_scanf( ber, "m", newpass );
338 if( tag == LBER_ERROR ) {
340 LDAP_LOG( OPERATION, ERR,
341 "slap_passwd_parse: OLD parse failed.\n", 0, 0, 0 );
343 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD parse failed.\n",
350 tag = ber_peek_tag( ber, &len );
356 LDAP_LOG( OPERATION, ERR,
357 "slap_passwd_parse: decoding error, len=%ld\n", (long)len, 0, 0 );
359 Debug( LDAP_DEBUG_TRACE,
360 "slap_passwd_parse: decoding error, len=%ld\n",
364 *text = "data decoding error";
365 rc = LDAP_PROTOCOL_ERROR;
372 struct berval * slap_passwd_return(
373 struct berval *cred )
376 struct berval *bv = NULL;
377 BerElementBuffer berbuf;
378 /* opaque structure, size unknown but smaller than berbuf */
379 BerElement *ber = (BerElement *)&berbuf;
381 assert( cred != NULL );
384 LDAP_LOG( OPERATION, ENTRY,
385 "slap_passwd_return: %ld\n",(long)cred->bv_len, 0, 0 );
387 Debug( LDAP_DEBUG_TRACE, "slap_passwd_return: %ld\n",
388 (long) cred->bv_len, 0, 0 );
391 ber_init_w_nullc( ber, LBER_USE_DER );
393 rc = ber_printf( ber, "{tON}",
394 LDAP_TAG_EXOP_MODIFY_PASSWD_GEN, cred );
397 (void) ber_flatten( ber, &bv );
415 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
416 ldap_pvt_thread_mutex_lock( &passwd_mutex );
418 lutil_passwd_sasl_conn = conn->c_sasl_authctx;
422 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
423 if( !lutil_passwd( bv, cred, NULL, text ) ) {
429 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
431 lutil_passwd_sasl_conn = NULL;
433 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
440 slap_passwd_generate( struct berval *pass )
443 LDAP_LOG( OPERATION, ENTRY, "slap_passwd_generate: begin\n", 0, 0, 0 );
445 Debug( LDAP_DEBUG_TRACE, "slap_passwd_generate\n", 0, 0, 0 );
451 * generate passwords of only 8 characters as some getpass(3)
452 * implementations truncate at 8 characters.
454 lutil_passwd_generate( pass, 8 );
458 slap_passwd_hash_type(
459 struct berval * cred,
469 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
470 ldap_pvt_thread_mutex_lock( &passwd_mutex );
473 lutil_passwd_hash( cred , hash, new, text );
475 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
476 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
482 struct berval * cred,
487 if ( default_passwd_hash ) {
488 hash = default_passwd_hash[0];
491 hash = (char *)defhash[0];
494 slap_passwd_hash_type( cred, new, hash, text );