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 !LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
115 /* we SHOULD return a referral in this case */
116 BerVarray defref = NULL;
117 if ( !LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
119 LDAP_STAILQ_FOREACH( si, &op->o_bd->be_syncinfo, si_next ) {
121 ber_dupbv( &tmpbv, &si->si_provideruri_bv[0] );
122 ber_bvarray_add( &defref, &tmpbv );
125 defref = referral_rewrite( op->o_bd->be_update_refs,
126 NULL, NULL, LDAP_SCOPE_DEFAULT );
129 return LDAP_REFERRAL;
131 #endif /* !SLAPD_MULTIMASTER */
133 /* generate a new password if none was provided */
134 if ( qpw->rs_new.bv_len == 0 ) {
135 slap_passwd_generate( &qpw->rs_new );
136 if ( qpw->rs_new.bv_len ) {
137 rsp = slap_passwd_return( &qpw->rs_new );
140 if ( qpw->rs_new.bv_len == 0 ) {
141 rs->sr_text = "password generation failed";
145 /* Give the backend a chance to handle this itself */
146 if ( op->o_bd->be_extended ) {
147 rs->sr_err = op->o_bd->be_extended( op, rs );
148 if ( rs->sr_err != LDAP_UNWILLING_TO_PERFORM &&
149 rs->sr_err != SLAP_CB_CONTINUE ) {
154 /* The backend didn't handle it, so try it here */
155 if( op->o_bd && !op->o_bd->be_modify ) {
156 rs->sr_text = "operation not supported for current user";
157 return LDAP_UNWILLING_TO_PERFORM;
160 ml = ch_malloc( sizeof(Modifications) );
161 if ( !qpw->rs_modtail ) qpw->rs_modtail = &ml->sml_next;
163 if ( default_passwd_hash ) {
164 for ( nhash = 0; default_passwd_hash[nhash]; nhash++ );
165 hashes = default_passwd_hash;
168 hashes = (char **)defhash;
170 ml->sml_values = ch_malloc( (nhash+1)*sizeof(struct berval) );
171 for ( i=0; hashes[i]; i++ ) {
172 slap_passwd_hash_type( &qpw->rs_new, &hash, hashes[i], &rs->sr_text );
173 if ( hash.bv_len == 0 ) {
174 if ( !rs->sr_text ) {
175 rs->sr_text = "password hash failed";
179 ml->sml_values[i] = hash;
181 ml->sml_values[i].bv_val = NULL;
182 ml->sml_nvalues = NULL;
183 ml->sml_desc = slap_schema.si_ad_userPassword;
184 ml->sml_op = LDAP_MOD_REPLACE;
185 ml->sml_next = qpw->rs_mods;
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;
196 cb2.sc_private = qpw; /* let Modify know this was pwdMod,
199 rs->sr_err = slap_mods_opattrs( &op2, ml, qpw->rs_modtail, &rs->sr_text,
202 if ( rs->sr_err == LDAP_SUCCESS ) {
203 rs->sr_err = op2.o_bd->be_modify( &op2, rs );
205 if ( rs->sr_err == LDAP_SUCCESS ) {
206 rs->sr_rspdata = rsp;
211 slap_mods_free( qpw->rs_mods );
213 free( qpw->rs_new.bv_val );
219 int slap_passwd_parse( struct berval *reqdata,
221 struct berval *oldpass,
222 struct berval *newpass,
225 int rc = LDAP_SUCCESS;
228 BerElementBuffer berbuf;
229 BerElement *ber = (BerElement *)&berbuf;
231 if( reqdata == NULL ) {
235 if( reqdata->bv_len == 0 ) {
236 *text = "empty request data field";
237 return LDAP_PROTOCOL_ERROR;
240 /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
241 ber_init2( ber, reqdata, 0 );
243 tag = ber_scanf( ber, "{" /*}*/ );
245 if( tag == LBER_ERROR ) {
247 LDAP_LOG( OPERATION, ERR,
248 "slap_passwd_parse: decoding error\n", 0, 0, 0 );
250 Debug( LDAP_DEBUG_TRACE,
251 "slap_passwd_parse: decoding error\n", 0, 0, 0 );
253 rc = LDAP_PROTOCOL_ERROR;
257 tag = ber_peek_tag( ber, &len );
258 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_ID ) {
261 LDAP_LOG( OPERATION, ERR,
262 "slap_passwd_parse: ID not allowed.\n", 0, 0, 0 );
264 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID not allowed.\n",
268 *text = "user must change own password";
269 rc = LDAP_UNWILLING_TO_PERFORM;
273 tag = ber_scanf( ber, "m", id );
275 if( tag == LBER_ERROR ) {
277 LDAP_LOG( OPERATION, ERR,
278 "slap_passwd_parse: ID parse failed.\n", 0, 0, 0 );
280 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
287 tag = ber_peek_tag( ber, &len);
290 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_OLD ) {
291 if( oldpass == NULL ) {
293 LDAP_LOG( OPERATION, ERR,
294 "slap_passwd_parse: OLD not allowed.\n" , 0, 0, 0 );
296 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD not allowed.\n",
300 *text = "use bind to verify old password";
301 rc = LDAP_UNWILLING_TO_PERFORM;
305 tag = ber_scanf( ber, "m", oldpass );
307 if( tag == LBER_ERROR ) {
309 LDAP_LOG( OPERATION, ERR,
310 "slap_passwd_parse: ID parse failed.\n" , 0, 0, 0 );
312 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
319 tag = ber_peek_tag( ber, &len );
322 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ) {
323 if( newpass == NULL ) {
325 LDAP_LOG( OPERATION, ERR,
326 "slap_passwd_parse: NEW not allowed.\n", 0, 0, 0 );
328 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW not allowed.\n",
332 *text = "user specified passwords disallowed";
333 rc = LDAP_UNWILLING_TO_PERFORM;
337 tag = ber_scanf( ber, "m", newpass );
339 if( tag == LBER_ERROR ) {
341 LDAP_LOG( OPERATION, ERR,
342 "slap_passwd_parse: OLD parse failed.\n", 0, 0, 0 );
344 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD parse failed.\n",
351 tag = ber_peek_tag( ber, &len );
357 LDAP_LOG( OPERATION, ERR,
358 "slap_passwd_parse: decoding error, len=%ld\n", (long)len, 0, 0 );
360 Debug( LDAP_DEBUG_TRACE,
361 "slap_passwd_parse: decoding error, len=%ld\n",
365 *text = "data decoding error";
366 rc = LDAP_PROTOCOL_ERROR;
373 struct berval * slap_passwd_return(
374 struct berval *cred )
377 struct berval *bv = NULL;
378 BerElementBuffer berbuf;
379 /* opaque structure, size unknown but smaller than berbuf */
380 BerElement *ber = (BerElement *)&berbuf;
382 assert( cred != NULL );
385 LDAP_LOG( OPERATION, ENTRY,
386 "slap_passwd_return: %ld\n",(long)cred->bv_len, 0, 0 );
388 Debug( LDAP_DEBUG_TRACE, "slap_passwd_return: %ld\n",
389 (long) cred->bv_len, 0, 0 );
392 ber_init_w_nullc( ber, LBER_USE_DER );
394 rc = ber_printf( ber, "{tON}",
395 LDAP_TAG_EXOP_MODIFY_PASSWD_GEN, cred );
398 (void) ber_flatten( ber, &bv );
416 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
417 ldap_pvt_thread_mutex_lock( &passwd_mutex );
419 lutil_passwd_sasl_conn = conn->c_sasl_authctx;
423 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
424 if( !lutil_passwd( bv, cred, NULL, text ) ) {
430 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
432 lutil_passwd_sasl_conn = NULL;
434 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
441 slap_passwd_generate( struct berval *pass )
444 LDAP_LOG( OPERATION, ENTRY, "slap_passwd_generate: begin\n", 0, 0, 0 );
446 Debug( LDAP_DEBUG_TRACE, "slap_passwd_generate\n", 0, 0, 0 );
452 * generate passwords of only 8 characters as some getpass(3)
453 * implementations truncate at 8 characters.
455 lutil_passwd_generate( pass, 8 );
459 slap_passwd_hash_type(
460 struct berval * cred,
470 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
471 ldap_pvt_thread_mutex_lock( &passwd_mutex );
474 lutil_passwd_hash( cred , hash, new, text );
476 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
477 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
483 struct berval * cred,
488 if ( default_passwd_hash ) {
489 hash = default_passwd_hash[0];
492 hash = (char *)defhash[0];
495 slap_passwd_hash_type( cred, new, hash, text );