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 = qpw->rs_old.bv_val ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
184 ml->sml_next = qpw->rs_mods;
186 if ( qpw->rs_old.bv_val ) {
187 ml = ch_malloc( sizeof(Modifications) );
188 ml->sml_values = ch_malloc( (nhash+1)*sizeof(struct berval) );
189 for ( i=0; hashes[i]; i++ ) {
190 slap_passwd_hash_type( &qpw->rs_old, &hash, hashes[i], &rs->sr_text );
191 if ( hash.bv_len == 0 ) {
192 if ( !rs->sr_text ) {
193 rs->sr_text = "password hash failed";
197 ml->sml_values[i] = hash;
199 ml->sml_values[i].bv_val = NULL;
200 ml->sml_desc = slap_schema.si_ad_userPassword;
201 ml->sml_op = LDAP_MOD_DELETE;
202 ml->sml_next = qpw->rs_mods;
206 rs->sr_err = LDAP_OTHER;
210 op2.o_tag = LDAP_REQ_MODIFY;
211 op2.o_callback = &cb2;
212 op2.orm_modlist = qpw->rs_mods;
213 cb2.sc_private = qpw; /* let Modify know this was pwdMod,
216 rs->sr_err = slap_mods_opattrs( &op2, ml, qpw->rs_modtail, &rs->sr_text,
219 if ( rs->sr_err == LDAP_SUCCESS ) {
220 rs->sr_err = op2.o_bd->be_modify( &op2, rs );
222 if ( rs->sr_err == LDAP_SUCCESS ) {
223 rs->sr_rspdata = rsp;
228 slap_mods_free( qpw->rs_mods );
230 free( qpw->rs_new.bv_val );
236 int slap_passwd_parse( struct berval *reqdata,
238 struct berval *oldpass,
239 struct berval *newpass,
242 int rc = LDAP_SUCCESS;
245 BerElementBuffer berbuf;
246 BerElement *ber = (BerElement *)&berbuf;
248 if( reqdata == NULL ) {
252 if( reqdata->bv_len == 0 ) {
253 *text = "empty request data field";
254 return LDAP_PROTOCOL_ERROR;
257 /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
258 ber_init2( ber, reqdata, 0 );
260 tag = ber_scanf( ber, "{" /*}*/ );
262 if( tag == LBER_ERROR ) {
264 LDAP_LOG( OPERATION, ERR,
265 "slap_passwd_parse: decoding error\n", 0, 0, 0 );
267 Debug( LDAP_DEBUG_TRACE,
268 "slap_passwd_parse: decoding error\n", 0, 0, 0 );
270 rc = LDAP_PROTOCOL_ERROR;
274 tag = ber_peek_tag( ber, &len );
275 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_ID ) {
278 LDAP_LOG( OPERATION, ERR,
279 "slap_passwd_parse: ID not allowed.\n", 0, 0, 0 );
281 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID not allowed.\n",
285 *text = "user must change own password";
286 rc = LDAP_UNWILLING_TO_PERFORM;
290 tag = ber_scanf( ber, "m", id );
292 if( tag == LBER_ERROR ) {
294 LDAP_LOG( OPERATION, ERR,
295 "slap_passwd_parse: ID parse failed.\n", 0, 0, 0 );
297 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
304 tag = ber_peek_tag( ber, &len);
307 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_OLD ) {
308 if( oldpass == NULL ) {
310 LDAP_LOG( OPERATION, ERR,
311 "slap_passwd_parse: OLD not allowed.\n" , 0, 0, 0 );
313 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD not allowed.\n",
317 *text = "use bind to verify old password";
318 rc = LDAP_UNWILLING_TO_PERFORM;
322 tag = ber_scanf( ber, "m", oldpass );
324 if( tag == LBER_ERROR ) {
326 LDAP_LOG( OPERATION, ERR,
327 "slap_passwd_parse: ID parse failed.\n" , 0, 0, 0 );
329 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
336 tag = ber_peek_tag( ber, &len );
339 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ) {
340 if( newpass == NULL ) {
342 LDAP_LOG( OPERATION, ERR,
343 "slap_passwd_parse: NEW not allowed.\n", 0, 0, 0 );
345 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW not allowed.\n",
349 *text = "user specified passwords disallowed";
350 rc = LDAP_UNWILLING_TO_PERFORM;
354 tag = ber_scanf( ber, "m", newpass );
356 if( tag == LBER_ERROR ) {
358 LDAP_LOG( OPERATION, ERR,
359 "slap_passwd_parse: OLD parse failed.\n", 0, 0, 0 );
361 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD parse failed.\n",
368 tag = ber_peek_tag( ber, &len );
374 LDAP_LOG( OPERATION, ERR,
375 "slap_passwd_parse: decoding error, len=%ld\n", (long)len, 0, 0 );
377 Debug( LDAP_DEBUG_TRACE,
378 "slap_passwd_parse: decoding error, len=%ld\n",
382 *text = "data decoding error";
383 rc = LDAP_PROTOCOL_ERROR;
390 struct berval * slap_passwd_return(
391 struct berval *cred )
394 struct berval *bv = NULL;
395 BerElementBuffer berbuf;
396 /* opaque structure, size unknown but smaller than berbuf */
397 BerElement *ber = (BerElement *)&berbuf;
399 assert( cred != NULL );
402 LDAP_LOG( OPERATION, ENTRY,
403 "slap_passwd_return: %ld\n",(long)cred->bv_len, 0, 0 );
405 Debug( LDAP_DEBUG_TRACE, "slap_passwd_return: %ld\n",
406 (long) cred->bv_len, 0, 0 );
409 ber_init_w_nullc( ber, LBER_USE_DER );
411 rc = ber_printf( ber, "{tON}",
412 LDAP_TAG_EXOP_MODIFY_PASSWD_GEN, cred );
415 (void) ber_flatten( ber, &bv );
433 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
434 ldap_pvt_thread_mutex_lock( &passwd_mutex );
436 lutil_passwd_sasl_conn = conn->c_sasl_authctx;
440 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
441 if( !lutil_passwd( bv, cred, NULL, text ) ) {
447 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
449 lutil_passwd_sasl_conn = NULL;
451 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
458 slap_passwd_generate( struct berval *pass )
461 LDAP_LOG( OPERATION, ENTRY, "slap_passwd_generate: begin\n", 0, 0, 0 );
463 Debug( LDAP_DEBUG_TRACE, "slap_passwd_generate\n", 0, 0, 0 );
469 * generate passwords of only 8 characters as some getpass(3)
470 * implementations truncate at 8 characters.
472 lutil_passwd_generate( pass, 8 );
476 slap_passwd_hash_type(
477 struct berval * cred,
487 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
488 ldap_pvt_thread_mutex_lock( &passwd_mutex );
491 lutil_passwd_hash( cred , hash, new, text );
493 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
494 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
500 struct berval * cred,
505 if ( default_passwd_hash ) {
506 hash = default_passwd_hash[0];
509 hash = (char *)defhash[0];
512 slap_passwd_hash_type( cred, new, hash, text );