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(!( !SLAP_SHADOW( op->o_bd ) || be_isupdate( op ))) {
114 /* we SHOULD return a referral in this case */
115 BerVarray defref = op->o_bd->be_update_refs
116 ? op->o_bd->be_update_refs : default_referral;
118 if( defref != NULL ) {
119 rs->sr_ref = referral_rewrite( op->o_bd->be_update_refs,
120 NULL, NULL, LDAP_SCOPE_DEFAULT );
122 rs->sr_flags |= REP_REF_MUSTBEFREED;
126 return LDAP_REFERRAL;
130 rs->sr_text = "shadow context; no update referral";
131 return LDAP_UNWILLING_TO_PERFORM;
133 #endif /* !SLAPD_MULTIMASTER */
135 /* generate a new password if none was provided */
136 if ( qpw->rs_new.bv_len == 0 ) {
137 slap_passwd_generate( &qpw->rs_new );
138 if ( qpw->rs_new.bv_len ) {
139 rsp = slap_passwd_return( &qpw->rs_new );
142 if ( qpw->rs_new.bv_len == 0 ) {
143 rs->sr_text = "password generation failed";
147 /* Give the backend a chance to handle this itself */
148 if ( op->o_bd->be_extended ) {
149 rs->sr_err = op->o_bd->be_extended( op, rs );
150 if ( rs->sr_err != LDAP_UNWILLING_TO_PERFORM &&
151 rs->sr_err != SLAP_CB_CONTINUE ) {
156 /* The backend didn't handle it, so try it here */
157 if( op->o_bd && !op->o_bd->be_modify ) {
158 rs->sr_text = "operation not supported for current user";
159 return LDAP_UNWILLING_TO_PERFORM;
162 ml = ch_malloc( sizeof(Modifications) );
163 if ( !qpw->rs_modtail ) qpw->rs_modtail = &ml->sml_next;
165 if ( default_passwd_hash ) {
166 for ( nhash = 0; default_passwd_hash[nhash]; nhash++ );
167 hashes = default_passwd_hash;
170 hashes = (char **)defhash;
172 ml->sml_values = ch_malloc( (nhash+1)*sizeof(struct berval) );
173 for ( i=0; hashes[i]; i++ ) {
174 slap_passwd_hash_type( &qpw->rs_new, &hash, hashes[i], &rs->sr_text );
175 if ( hash.bv_len == 0 ) {
176 if ( !rs->sr_text ) {
177 rs->sr_text = "password hash failed";
181 ml->sml_values[i] = hash;
183 ml->sml_values[i].bv_val = NULL;
184 ml->sml_nvalues = NULL;
185 ml->sml_desc = slap_schema.si_ad_userPassword;
186 ml->sml_op = LDAP_MOD_REPLACE;
187 ml->sml_next = qpw->rs_mods;
191 rs->sr_err = LDAP_OTHER;
195 op2.o_tag = LDAP_REQ_MODIFY;
196 op2.o_callback = &cb2;
197 op2.orm_modlist = qpw->rs_mods;
198 cb2.sc_private = qpw; /* let Modify know this was pwdMod,
201 rs->sr_err = slap_mods_opattrs( &op2, ml, qpw->rs_modtail, &rs->sr_text,
204 if ( rs->sr_err == LDAP_SUCCESS ) {
205 rs->sr_err = op2.o_bd->be_modify( &op2, rs );
207 if ( rs->sr_err == LDAP_SUCCESS ) {
208 rs->sr_rspdata = rsp;
213 slap_mods_free( qpw->rs_mods );
215 free( qpw->rs_new.bv_val );
221 int slap_passwd_parse( struct berval *reqdata,
223 struct berval *oldpass,
224 struct berval *newpass,
227 int rc = LDAP_SUCCESS;
230 BerElementBuffer berbuf;
231 BerElement *ber = (BerElement *)&berbuf;
233 if( reqdata == NULL ) {
237 if( reqdata->bv_len == 0 ) {
238 *text = "empty request data field";
239 return LDAP_PROTOCOL_ERROR;
242 /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
243 ber_init2( ber, reqdata, 0 );
245 tag = ber_scanf( ber, "{" /*}*/ );
247 if( tag == LBER_ERROR ) {
249 LDAP_LOG( OPERATION, ERR,
250 "slap_passwd_parse: decoding error\n", 0, 0, 0 );
252 Debug( LDAP_DEBUG_TRACE,
253 "slap_passwd_parse: decoding error\n", 0, 0, 0 );
255 rc = LDAP_PROTOCOL_ERROR;
259 tag = ber_peek_tag( ber, &len );
260 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_ID ) {
263 LDAP_LOG( OPERATION, ERR,
264 "slap_passwd_parse: ID not allowed.\n", 0, 0, 0 );
266 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID not allowed.\n",
270 *text = "user must change own password";
271 rc = LDAP_UNWILLING_TO_PERFORM;
275 tag = ber_scanf( ber, "m", id );
277 if( tag == LBER_ERROR ) {
279 LDAP_LOG( OPERATION, ERR,
280 "slap_passwd_parse: ID parse failed.\n", 0, 0, 0 );
282 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
289 tag = ber_peek_tag( ber, &len);
292 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_OLD ) {
293 if( oldpass == NULL ) {
295 LDAP_LOG( OPERATION, ERR,
296 "slap_passwd_parse: OLD not allowed.\n" , 0, 0, 0 );
298 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD not allowed.\n",
302 *text = "use bind to verify old password";
303 rc = LDAP_UNWILLING_TO_PERFORM;
307 tag = ber_scanf( ber, "m", oldpass );
309 if( tag == LBER_ERROR ) {
311 LDAP_LOG( OPERATION, ERR,
312 "slap_passwd_parse: ID parse failed.\n" , 0, 0, 0 );
314 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
321 tag = ber_peek_tag( ber, &len );
324 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ) {
325 if( newpass == NULL ) {
327 LDAP_LOG( OPERATION, ERR,
328 "slap_passwd_parse: NEW not allowed.\n", 0, 0, 0 );
330 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW not allowed.\n",
334 *text = "user specified passwords disallowed";
335 rc = LDAP_UNWILLING_TO_PERFORM;
339 tag = ber_scanf( ber, "m", newpass );
341 if( tag == LBER_ERROR ) {
343 LDAP_LOG( OPERATION, ERR,
344 "slap_passwd_parse: OLD parse failed.\n", 0, 0, 0 );
346 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD parse failed.\n",
353 tag = ber_peek_tag( ber, &len );
359 LDAP_LOG( OPERATION, ERR,
360 "slap_passwd_parse: decoding error, len=%ld\n", (long)len, 0, 0 );
362 Debug( LDAP_DEBUG_TRACE,
363 "slap_passwd_parse: decoding error, len=%ld\n",
367 *text = "data decoding error";
368 rc = LDAP_PROTOCOL_ERROR;
375 struct berval * slap_passwd_return(
376 struct berval *cred )
379 struct berval *bv = NULL;
380 BerElementBuffer berbuf;
381 /* opaque structure, size unknown but smaller than berbuf */
382 BerElement *ber = (BerElement *)&berbuf;
384 assert( cred != NULL );
387 LDAP_LOG( OPERATION, ENTRY,
388 "slap_passwd_return: %ld\n",(long)cred->bv_len, 0, 0 );
390 Debug( LDAP_DEBUG_TRACE, "slap_passwd_return: %ld\n",
391 (long) cred->bv_len, 0, 0 );
394 ber_init_w_nullc( ber, LBER_USE_DER );
396 rc = ber_printf( ber, "{tON}",
397 LDAP_TAG_EXOP_MODIFY_PASSWD_GEN, cred );
400 (void) ber_flatten( ber, &bv );
418 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
419 ldap_pvt_thread_mutex_lock( &passwd_mutex );
421 lutil_passwd_sasl_conn = conn->c_sasl_authctx;
425 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
426 if( !lutil_passwd( bv, cred, NULL, text ) ) {
432 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
434 lutil_passwd_sasl_conn = NULL;
436 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
443 slap_passwd_generate( struct berval *pass )
446 LDAP_LOG( OPERATION, ENTRY, "slap_passwd_generate: begin\n", 0, 0, 0 );
448 Debug( LDAP_DEBUG_TRACE, "slap_passwd_generate\n", 0, 0, 0 );
454 * generate passwords of only 8 characters as some getpass(3)
455 * implementations truncate at 8 characters.
457 lutil_passwd_generate( pass, 8 );
461 slap_passwd_hash_type(
462 struct berval * cred,
472 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
473 ldap_pvt_thread_mutex_lock( &passwd_mutex );
476 lutil_passwd_hash( cred , hash, new, text );
478 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
479 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
485 struct berval * cred,
490 if ( default_passwd_hash ) {
491 hash = default_passwd_hash[0];
494 hash = (char *)defhash[0];
497 slap_passwd_hash_type( cred, new, hash, text );