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>
35 struct berval id = {0, NULL}, old = {0, NULL}, new = {0, NULL},
36 dn, ndn, hash, vals[2], tmpbv, *rsp = NULL;
37 Modifications ml, **modtail;
39 slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
40 slap_callback cb2 = { NULL, slap_replog_cb, NULL, NULL };
43 assert( ber_bvcmp( &slap_EXOP_MODIFY_PASSWD, &op->ore_reqoid ) == 0 );
45 if( op->o_dn.bv_len == 0 ) {
46 rs->sr_text = "only authenticated users may change passwords";
47 return LDAP_STRONG_AUTH_REQUIRED;
50 if( op->oq_extended.rs_reqdata ) {
51 ber_dupbv_x( &tmpbv, op->oq_extended.rs_reqdata, op->o_tmpmemctx );
53 rs->sr_err = slap_passwd_parse(
54 op->oq_extended.rs_reqdata ? &tmpbv : NULL,
55 &id, &old, &new, &rs->sr_text );
57 if ( rs->sr_err != LDAP_SUCCESS ) {
63 /* ndn is in tmpmem, so we don't need to free it */
64 rs->sr_err = dnNormalize( 0, NULL, NULL, &dn, &ndn, op->o_tmpmemctx );
65 if ( rs->sr_err != LDAP_SUCCESS ) {
66 rs->sr_text = "Invalid DN";
69 op->o_bd = select_backend( &ndn, 0, 0 );
73 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
74 op->o_bd = op->o_conn->c_authz_backend;
75 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
78 if( op->o_bd == NULL ) {
79 #ifdef HAVE_CYRUS_SASL
80 return slap_sasl_setpass( op, rs );
82 rs->sr_text = "no authz backend";
87 if ( ndn.bv_len == 0 ) {
88 rs->sr_text = "no password is associated with the Root DSE";
89 return LDAP_UNWILLING_TO_PERFORM;
92 if (backend_check_restrictions( op, rs,
93 (struct berval *)&slap_EXOP_MODIFY_PASSWD ) != LDAP_SUCCESS) {
98 #ifndef SLAPD_MULTIMASTER
99 /* This does not apply to multi-master case */
100 if( op->o_bd->be_update_ndn.bv_len ) {
101 /* we SHOULD return a referral in this case */
102 BerVarray defref = NULL;
103 if ( !LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
105 LDAP_STAILQ_FOREACH( si, &op->o_bd->be_syncinfo, si_next ) {
107 ber_dupbv( &tmpbv, &si->si_provideruri_bv[0] );
108 ber_bvarray_add( &defref, &tmpbv );
111 defref = referral_rewrite( op->o_bd->be_update_refs,
112 NULL, NULL, LDAP_SCOPE_DEFAULT );
115 return LDAP_REFERRAL;
117 #endif /* !SLAPD_MULTIMASTER */
119 /* Give the backend a chance to handle this itself */
120 if ( op->o_bd->be_extended ) {
121 rs->sr_err = op->o_bd->be_extended( op, rs );
122 if ( rs->sr_err != LDAP_UNWILLING_TO_PERFORM ) {
127 /* The backend didn't handle it, so try it here */
128 if( op->o_bd && !op->o_bd->be_modify ) {
129 rs->sr_text = "operation not supported for current user";
130 return LDAP_UNWILLING_TO_PERFORM;
133 if ( new.bv_len == 0 ) {
134 slap_passwd_generate( &new );
135 rsp = slap_passwd_return( &new );
137 if ( new.bv_len == 0 ) {
138 rs->sr_text = "password generation failed";
141 slap_passwd_hash( &new, &hash, &rs->sr_text );
145 if ( hash.bv_len == 0 ) {
146 if ( !rs->sr_text ) {
147 rs->sr_text = "password hash failed";
152 vals[1].bv_val = NULL;
153 ml.sml_desc = slap_schema.si_ad_userPassword;
154 ml.sml_values = vals;
155 ml.sml_nvalues = NULL;
156 ml.sml_op = LDAP_MOD_REPLACE;
160 op2.o_tag = LDAP_REQ_MODIFY;
161 op2.o_callback = &cb2;
164 op2.orm_modlist = &ml;
166 modtail = &ml.sml_next;
167 rs->sr_err = slap_mods_opattrs( &op2, &ml, modtail, &rs->sr_text,
170 if ( rs->sr_err == LDAP_SUCCESS ) {
171 rs->sr_err = op2.o_bd->be_modify( &op2, rs );
173 if ( rs->sr_err == LDAP_SUCCESS ) {
174 rs->sr_rspdata = rsp;
178 slap_mods_free( ml.sml_next );
184 int slap_passwd_parse( struct berval *reqdata,
186 struct berval *oldpass,
187 struct berval *newpass,
190 int rc = LDAP_SUCCESS;
193 BerElementBuffer berbuf;
194 BerElement *ber = (BerElement *)&berbuf;
196 if( reqdata == NULL ) {
200 if( reqdata->bv_len == 0 ) {
201 *text = "empty request data field";
202 return LDAP_PROTOCOL_ERROR;
205 /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
206 ber_init2( ber, reqdata, 0 );
208 tag = ber_scanf( ber, "{" /*}*/ );
210 if( tag == LBER_ERROR ) {
212 LDAP_LOG( OPERATION, ERR,
213 "slap_passwd_parse: decoding error\n", 0, 0, 0 );
215 Debug( LDAP_DEBUG_TRACE,
216 "slap_passwd_parse: decoding error\n", 0, 0, 0 );
218 rc = LDAP_PROTOCOL_ERROR;
222 tag = ber_peek_tag( ber, &len );
223 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_ID ) {
226 LDAP_LOG( OPERATION, ERR,
227 "slap_passwd_parse: ID not allowed.\n", 0, 0, 0 );
229 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID not allowed.\n",
233 *text = "user must change own password";
234 rc = LDAP_UNWILLING_TO_PERFORM;
238 tag = ber_scanf( ber, "m", id );
240 if( tag == LBER_ERROR ) {
242 LDAP_LOG( OPERATION, ERR,
243 "slap_passwd_parse: ID parse failed.\n", 0, 0, 0 );
245 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
252 tag = ber_peek_tag( ber, &len);
255 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_OLD ) {
256 if( oldpass == NULL ) {
258 LDAP_LOG( OPERATION, ERR,
259 "slap_passwd_parse: OLD not allowed.\n" , 0, 0, 0 );
261 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD not allowed.\n",
265 *text = "use bind to verify old password";
266 rc = LDAP_UNWILLING_TO_PERFORM;
270 tag = ber_scanf( ber, "m", oldpass );
272 if( tag == LBER_ERROR ) {
274 LDAP_LOG( OPERATION, ERR,
275 "slap_passwd_parse: ID parse failed.\n" , 0, 0, 0 );
277 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
284 tag = ber_peek_tag( ber, &len );
287 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ) {
288 if( newpass == NULL ) {
290 LDAP_LOG( OPERATION, ERR,
291 "slap_passwd_parse: NEW not allowed.\n", 0, 0, 0 );
293 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW not allowed.\n",
297 *text = "user specified passwords disallowed";
298 rc = LDAP_UNWILLING_TO_PERFORM;
302 tag = ber_scanf( ber, "m", newpass );
304 if( tag == LBER_ERROR ) {
306 LDAP_LOG( OPERATION, ERR,
307 "slap_passwd_parse: OLD parse failed.\n", 0, 0, 0 );
309 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD parse failed.\n",
316 tag = ber_peek_tag( ber, &len );
322 LDAP_LOG( OPERATION, ERR,
323 "slap_passwd_parse: decoding error, len=%ld\n", (long)len, 0, 0 );
325 Debug( LDAP_DEBUG_TRACE,
326 "slap_passwd_parse: decoding error, len=%ld\n",
330 *text = "data decoding error";
331 rc = LDAP_PROTOCOL_ERROR;
338 struct berval * slap_passwd_return(
339 struct berval *cred )
342 struct berval *bv = NULL;
343 BerElementBuffer berbuf;
344 /* opaque structure, size unknown but smaller than berbuf */
345 BerElement *ber = (BerElement *)&berbuf;
347 assert( cred != NULL );
350 LDAP_LOG( OPERATION, ENTRY,
351 "slap_passwd_return: %ld\n",(long)cred->bv_len, 0, 0 );
353 Debug( LDAP_DEBUG_TRACE, "slap_passwd_return: %ld\n",
354 (long) cred->bv_len, 0, 0 );
357 ber_init_w_nullc( ber, LBER_USE_DER );
359 rc = ber_printf( ber, "{tON}",
360 LDAP_TAG_EXOP_MODIFY_PASSWD_GEN, cred );
363 (void) ber_flatten( ber, &bv );
381 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
382 ldap_pvt_thread_mutex_lock( &passwd_mutex );
384 lutil_passwd_sasl_conn = conn->c_sasl_authctx;
388 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
389 if( !lutil_passwd( bv, cred, NULL, text ) ) {
395 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
397 lutil_passwd_sasl_conn = NULL;
399 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
406 slap_passwd_generate( struct berval *pass )
410 LDAP_LOG( OPERATION, ENTRY, "slap_passwd_generate: begin\n", 0, 0, 0 );
412 Debug( LDAP_DEBUG_TRACE, "slap_passwd_generate\n", 0, 0, 0 );
415 * generate passwords of only 8 characters as some getpass(3)
416 * implementations truncate at 8 characters.
418 tmp = lutil_passwd_generate( 8 );
430 struct berval * cred,
435 #ifdef LUTIL_SHA1_BYTES
436 char* hash = default_passwd_hash ? default_passwd_hash : "{SSHA}";
438 char* hash = default_passwd_hash ? default_passwd_hash : "{SMD5}";
442 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
443 ldap_pvt_thread_mutex_lock( &passwd_mutex );
446 tmp = lutil_passwd_hash( cred , hash, text );
448 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
449 ldap_pvt_thread_mutex_unlock( &passwd_mutex );