1 /* passwd.c - password extended operation routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2003 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>
35 struct berval id = {0, NULL}, old = {0, NULL}, new = {0, NULL},
36 dn, ndn, hash, vals[2];
38 Modifications ml, **modtail;
40 slap_callback cb = { slap_null_cb, NULL };
42 assert( ber_bvcmp( &slap_EXOP_MODIFY_PASSWD, &op->ore_reqoid ) == 0 );
44 if( op->o_dn.bv_len == 0 ) {
45 rs->sr_text = "only authenticated users may change passwords";
46 return LDAP_STRONG_AUTH_REQUIRED;
49 rs->sr_err = slap_passwd_parse( op->oq_extended.rs_reqdata, &id,
50 &old, &new, &rs->sr_text );
51 if ( rs->sr_err != LDAP_SUCCESS ) {
57 /* ndn is in tmpmem, so we don't need to free it */
58 rs->sr_err = dnNormalize( 0, NULL, NULL, &dn, &ndn, op->o_tmpmemctx );
59 if ( rs->sr_err != LDAP_SUCCESS ) {
60 rs->sr_text = "Invalid DN";
63 op->o_bd = select_backend( &ndn, 0, 0 );
67 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
68 op->o_bd = op->o_conn->c_authz_backend;
69 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
72 if ( ndn.bv_len == 0 ) {
73 rs->sr_text = "no password is associated with the Root DSE";
74 return LDAP_UNWILLING_TO_PERFORM;
77 if( op->o_bd && !op->o_bd->be_modify ) {
78 rs->sr_text = "operation not supported for current user";
79 return LDAP_UNWILLING_TO_PERFORM;
82 if (backend_check_restrictions( op, rs,
83 (struct berval *)&slap_EXOP_MODIFY_PASSWD ) != LDAP_SUCCESS) {
87 if( op->o_bd == NULL ) {
88 #ifdef HAVE_CYRUS_SASL
89 return slap_sasl_setpass( op, rs );
91 rs->sr_text = "no authz backend";
96 #ifndef SLAPD_MULTIMASTER
97 /* This does not apply to multi-master case */
98 if( op->o_bd->be_update_ndn.bv_len ) {
99 /* we SHOULD return a referral in this case */
100 BerVarray defref = NULL;
101 if ( !LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
103 LDAP_STAILQ_FOREACH( si, &op->o_bd->be_syncinfo, si_next ) {
105 ber_dupbv( &tmpbv, &si->si_provideruri_bv[0] );
106 ber_bvarray_add( &defref, &tmpbv );
109 defref = referral_rewrite( op->o_bd->be_update_refs,
110 NULL, NULL, LDAP_SCOPE_DEFAULT );
113 return LDAP_REFERRAL;
115 #endif /* !SLAPD_MULTIMASTER */
116 if ( new.bv_len == 0 ) {
117 slap_passwd_generate( &new );
120 if ( new.bv_len == 0 ) {
121 rs->sr_text = "password generation failed";
124 slap_passwd_hash( &new, &hash, &rs->sr_text );
128 if ( hash.bv_len == 0 ) {
129 if ( !rs->sr_text ) {
130 rs->sr_text = "password hash failed";
135 vals[1].bv_val = NULL;
136 ml.sml_desc = slap_schema.si_ad_userPassword;
137 ml.sml_values = vals;
138 ml.sml_nvalues = NULL;
139 ml.sml_op = LDAP_MOD_REPLACE;
143 op2.o_tag = LDAP_REQ_MODIFY;
144 op2.o_callback = &cb;
147 op2.orm_modlist = &ml;
149 modtail = &ml.sml_next;
150 rs->sr_err = slap_mods_opattrs( &op2, &ml, modtail, &rs->sr_text,
153 if ( rs->sr_err == LDAP_SUCCESS ) {
154 rs->sr_err = op2.o_bd->be_modify( &op2, rs );
155 if ( rs->sr_err == LDAP_SUCCESS ) {
159 slap_mods_free( ml.sml_next );
165 int slap_passwd_parse( struct berval *reqdata,
167 struct berval *oldpass,
168 struct berval *newpass,
171 int rc = LDAP_SUCCESS;
174 BerElementBuffer berbuf;
175 BerElement *ber = (BerElement *)&berbuf;
177 if( reqdata == NULL ) {
181 if( reqdata->bv_len == 0 ) {
182 *text = "empty request data field";
183 return LDAP_PROTOCOL_ERROR;
186 /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
187 ber_init2( ber, reqdata, 0 );
189 tag = ber_scanf( ber, "{" /*}*/ );
191 if( tag != LBER_ERROR ) {
192 tag = ber_peek_tag( ber, &len );
195 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_ID ) {
198 LDAP_LOG( OPERATION, ERR,
199 "slap_passwd_parse: ID not allowed.\n", 0, 0, 0 );
201 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID not allowed.\n",
205 *text = "user must change own password";
206 rc = LDAP_UNWILLING_TO_PERFORM;
210 tag = ber_scanf( ber, "m", id );
212 if( tag == LBER_ERROR ) {
214 LDAP_LOG( OPERATION, ERR,
215 "slap_passwd_parse: ID parse failed.\n", 0, 0, 0 );
217 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
224 tag = ber_peek_tag( ber, &len);
227 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_OLD ) {
228 if( oldpass == NULL ) {
230 LDAP_LOG( OPERATION, ERR,
231 "slap_passwd_parse: OLD not allowed.\n" , 0, 0, 0 );
233 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD not allowed.\n",
237 *text = "use bind to verify old password";
238 rc = LDAP_UNWILLING_TO_PERFORM;
242 tag = ber_scanf( ber, "m", oldpass );
244 if( tag == LBER_ERROR ) {
246 LDAP_LOG( OPERATION, ERR,
247 "slap_passwd_parse: ID parse failed.\n" , 0, 0, 0 );
249 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
256 tag = ber_peek_tag( ber, &len );
259 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ) {
260 if( newpass == NULL ) {
262 LDAP_LOG( OPERATION, ERR,
263 "slap_passwd_parse: NEW not allowed.\n", 0, 0, 0 );
265 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW not allowed.\n",
269 *text = "user specified passwords disallowed";
270 rc = LDAP_UNWILLING_TO_PERFORM;
274 tag = ber_scanf( ber, "m", newpass );
276 if( tag == LBER_ERROR ) {
278 LDAP_LOG( OPERATION, ERR,
279 "slap_passwd_parse: OLD parse failed.\n", 0, 0, 0 );
281 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD parse failed.\n",
288 tag = ber_peek_tag( ber, &len );
294 LDAP_LOG( OPERATION, ERR,
295 "slap_passwd_parse: decoding error, len=%ld\n", (long)len, 0, 0 );
297 Debug( LDAP_DEBUG_TRACE,
298 "slap_passwd_parse: decoding error, len=%ld\n",
302 *text = "data decoding error";
303 rc = LDAP_PROTOCOL_ERROR;
310 struct berval * slap_passwd_return(
311 struct berval *cred )
314 struct berval *bv = NULL;
315 BerElementBuffer berbuf;
316 /* opaque structure, size unknown but smaller than berbuf */
317 BerElement *ber = (BerElement *)&berbuf;
319 assert( cred != NULL );
322 LDAP_LOG( OPERATION, ENTRY,
323 "slap_passwd_return: %ld\n",(long)cred->bv_len, 0, 0 );
325 Debug( LDAP_DEBUG_TRACE, "slap_passwd_return: %ld\n",
326 (long) cred->bv_len, 0, 0 );
329 ber_init_w_nullc( ber, LBER_USE_DER );
331 rc = ber_printf( ber, "{tON}",
332 LDAP_TAG_EXOP_MODIFY_PASSWD_GEN, cred );
335 (void) ber_flatten( ber, &bv );
353 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
354 ldap_pvt_thread_mutex_lock( &passwd_mutex );
356 lutil_passwd_sasl_conn = conn->c_sasl_authctx;
360 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
361 if( !lutil_passwd( bv, cred, NULL, text ) ) {
367 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
369 lutil_passwd_sasl_conn = NULL;
371 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
378 slap_passwd_generate( struct berval *pass )
382 LDAP_LOG( OPERATION, ENTRY, "slap_passwd_generate: begin\n", 0, 0, 0 );
384 Debug( LDAP_DEBUG_TRACE, "slap_passwd_generate\n", 0, 0, 0 );
387 * generate passwords of only 8 characters as some getpass(3)
388 * implementations truncate at 8 characters.
390 tmp = lutil_passwd_generate( 8 );
402 struct berval * cred,
407 #ifdef LUTIL_SHA1_BYTES
408 char* hash = default_passwd_hash ? default_passwd_hash : "{SSHA}";
410 char* hash = default_passwd_hash ? default_passwd_hash : "{SMD5}";
414 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
415 ldap_pvt_thread_mutex_lock( &passwd_mutex );
418 tmp = lutil_passwd_hash( cred , hash, text );
420 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
421 ldap_pvt_thread_mutex_unlock( &passwd_mutex );