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 tmpbv, id, old, new;
37 assert( ber_bvcmp( &slap_EXOP_MODIFY_PASSWD, &op->ore_reqoid ) == 0 );
39 if( op->o_dn.bv_len == 0 ) {
40 rs->sr_text = "only authenticated users may change passwords";
41 return LDAP_STRONG_AUTH_REQUIRED;
44 /* FIXME: need to parse the reqdata to determine the backend
45 * of the DN the passwd_extop () will apply to; this, on turn,
46 * requires to duplicate the reqdata because slap_passwd_parse()
47 * apparently alters it!
49 * Maybe we can make a lightweight version of slap_passwd_parse()
50 * that extracts the DN with no impact on the reqdata value?
52 ber_dupbv_x( &tmpbv, op->oq_extended.rs_reqdata, op->o_tmpmemctx );
53 rs->sr_err = slap_passwd_parse( &tmpbv, &id, &old, &new, &rs->sr_text );
54 if (rs->sr_err != LDAP_SUCCESS) {
55 ber_memfree_x( tmpbv.bv_val, op->o_tmpmemctx );
59 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
60 /* FIXME: we select the appropriate backend based on the DN
64 op->o_bd = select_backend( &id, 0, 0 );
67 op->o_bd = op->o_conn->c_authz_backend;
69 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
70 ber_memfree_x( tmpbv.bv_val, op->o_tmpmemctx );
72 if( op->o_bd && !op->o_bd->be_extended ) {
73 rs->sr_text = "operation not supported for current user";
74 return LDAP_UNWILLING_TO_PERFORM;
77 if (backend_check_restrictions( op, rs,
78 (struct berval *)&slap_EXOP_MODIFY_PASSWD ) != LDAP_SUCCESS) {
82 if( op->o_bd == NULL ) {
83 #ifdef HAVE_CYRUS_SASL
84 rs->sr_err = slap_sasl_setpass( op, rs );
86 rs->sr_text = "no authz backend";
87 rs->sr_err = LDAP_OTHER;
90 #ifndef SLAPD_MULTIMASTER
91 /* This does not apply to multi-master case */
92 } else if( op->o_bd->be_update_ndn.bv_len ) {
93 /* we SHOULD return a referral in this case */
94 BerVarray defref = NULL;
95 if ( !LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
97 LDAP_STAILQ_FOREACH( si, &op->o_bd->be_syncinfo, si_next ) {
99 ber_dupbv( &tmpbv, &si->si_provideruri_bv[0] );
100 ber_bvarray_add( &defref, &tmpbv );
103 defref = referral_rewrite( op->o_bd->be_update_refs,
104 NULL, NULL, LDAP_SCOPE_DEFAULT );
107 rs->sr_err = LDAP_REFERRAL;
108 #endif /* !SLAPD_MULTIMASTER */
111 rs->sr_err = op->o_bd->be_extended( op, rs );
117 int slap_passwd_parse( struct berval *reqdata,
119 struct berval *oldpass,
120 struct berval *newpass,
123 int rc = LDAP_SUCCESS;
126 BerElementBuffer berbuf;
127 BerElement *ber = (BerElement *)&berbuf;
129 if( reqdata == NULL ) {
133 if( reqdata->bv_len == 0 ) {
134 *text = "empty request data field";
135 return LDAP_PROTOCOL_ERROR;
138 /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
139 ber_init2( ber, reqdata, 0 );
141 tag = ber_scanf( ber, "{" /*}*/ );
143 if( tag != LBER_ERROR ) {
144 tag = ber_peek_tag( ber, &len );
147 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_ID ) {
150 LDAP_LOG( OPERATION, ERR,
151 "slap_passwd_parse: ID not allowed.\n", 0, 0, 0 );
153 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID not allowed.\n",
157 *text = "user must change own password";
158 rc = LDAP_UNWILLING_TO_PERFORM;
162 tag = ber_scanf( ber, "m", id );
164 if( tag == LBER_ERROR ) {
166 LDAP_LOG( OPERATION, ERR,
167 "slap_passwd_parse: ID parse failed.\n", 0, 0, 0 );
169 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
176 tag = ber_peek_tag( ber, &len);
179 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_OLD ) {
180 if( oldpass == NULL ) {
182 LDAP_LOG( OPERATION, ERR,
183 "slap_passwd_parse: OLD not allowed.\n" , 0, 0, 0 );
185 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD not allowed.\n",
189 *text = "use bind to verify old password";
190 rc = LDAP_UNWILLING_TO_PERFORM;
194 tag = ber_scanf( ber, "m", oldpass );
196 if( tag == LBER_ERROR ) {
198 LDAP_LOG( OPERATION, ERR,
199 "slap_passwd_parse: ID parse failed.\n" , 0, 0, 0 );
201 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
208 tag = ber_peek_tag( ber, &len );
211 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ) {
212 if( newpass == NULL ) {
214 LDAP_LOG( OPERATION, ERR,
215 "slap_passwd_parse: NEW not allowed.\n", 0, 0, 0 );
217 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW not allowed.\n",
221 *text = "user specified passwords disallowed";
222 rc = LDAP_UNWILLING_TO_PERFORM;
226 tag = ber_scanf( ber, "m", newpass );
228 if( tag == LBER_ERROR ) {
230 LDAP_LOG( OPERATION, ERR,
231 "slap_passwd_parse: OLD parse failed.\n", 0, 0, 0 );
233 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD parse failed.\n",
240 tag = ber_peek_tag( ber, &len );
246 LDAP_LOG( OPERATION, ERR,
247 "slap_passwd_parse: decoding error, len=%ld\n", (long)len, 0, 0 );
249 Debug( LDAP_DEBUG_TRACE,
250 "slap_passwd_parse: decoding error, len=%ld\n",
254 *text = "data decoding error";
255 rc = LDAP_PROTOCOL_ERROR;
262 struct berval * slap_passwd_return(
263 struct berval *cred )
266 struct berval *bv = NULL;
267 BerElementBuffer berbuf;
268 /* opaque structure, size unknown but smaller than berbuf */
269 BerElement *ber = (BerElement *)&berbuf;
271 assert( cred != NULL );
274 LDAP_LOG( OPERATION, ENTRY,
275 "slap_passwd_return: %ld\n",(long)cred->bv_len, 0, 0 );
277 Debug( LDAP_DEBUG_TRACE, "slap_passwd_return: %ld\n",
278 (long) cred->bv_len, 0, 0 );
281 ber_init_w_nullc( ber, LBER_USE_DER );
283 rc = ber_printf( ber, "{tON}",
284 LDAP_TAG_EXOP_MODIFY_PASSWD_GEN, cred );
287 (void) ber_flatten( ber, &bv );
305 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
306 ldap_pvt_thread_mutex_lock( &passwd_mutex );
308 lutil_passwd_sasl_conn = conn->c_sasl_authctx;
312 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
313 if( !lutil_passwd( bv, cred, NULL, text ) ) {
319 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
321 lutil_passwd_sasl_conn = NULL;
323 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
330 slap_passwd_generate( struct berval *pass )
334 LDAP_LOG( OPERATION, ENTRY, "slap_passwd_generate: begin\n", 0, 0, 0 );
336 Debug( LDAP_DEBUG_TRACE, "slap_passwd_generate\n", 0, 0, 0 );
339 * generate passwords of only 8 characters as some getpass(3)
340 * implementations truncate at 8 characters.
342 tmp = lutil_passwd_generate( 8 );
354 struct berval * cred,
359 #ifdef LUTIL_SHA1_BYTES
360 char* hash = default_passwd_hash ? default_passwd_hash : "{SSHA}";
362 char* hash = default_passwd_hash ? default_passwd_hash : "{SMD5}";
366 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
367 ldap_pvt_thread_mutex_lock( &passwd_mutex );
370 tmp = lutil_passwd_hash( cred , hash, text );
372 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
373 ldap_pvt_thread_mutex_unlock( &passwd_mutex );