1 /* bind.c - ldbm backend bind and unbind routines */
4 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
13 #include <ac/socket.h>
14 #include <ac/string.h>
15 #include <ac/unistd.h>
26 assert( ber_bvcmp( &slap_EXOP_MODIFY_PASSWD, &op->ore_reqoid ) == 0 );
28 if( op->o_dn.bv_len == 0 ) {
29 rs->sr_text = "only authenticated users may change passwords";
30 return LDAP_STRONG_AUTH_REQUIRED;
33 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
34 op->o_bd = op->o_conn->c_authz_backend;
35 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
37 if( op->o_bd && !op->o_bd->be_extended ) {
38 rs->sr_text = "operation not supported for current user";
39 return LDAP_UNWILLING_TO_PERFORM;
42 if (backend_check_restrictions( op, rs,
43 (struct berval *)&slap_EXOP_MODIFY_PASSWD ) != LDAP_SUCCESS) {
47 if( op->o_bd == NULL ) {
48 #ifdef HAVE_CYRUS_SASL
49 rs->sr_err = slap_sasl_setpass( op, rs );
51 rs->sr_text = "no authz backend";
52 rs->sr_err = LDAP_OTHER;
55 #ifndef SLAPD_MULTIMASTER
56 /* This does not apply to multi-master case */
57 } else if( op->o_bd->be_update_ndn.bv_len ) {
58 /* we SHOULD return a referral in this case */
59 BerVarray defref = NULL;
60 if ( op->o_bd->syncinfo ) {
61 defref = op->o_bd->syncinfo->provideruri_bv;
64 defref = referral_rewrite( op->o_bd->be_update_refs,
65 NULL, NULL, LDAP_SCOPE_DEFAULT );
68 rs->sr_err = LDAP_REFERRAL;
69 #endif /* !SLAPD_MULTIMASTER */
72 rs->sr_err = op->o_bd->be_extended( op, rs );
78 int slap_passwd_parse( struct berval *reqdata,
80 struct berval *oldpass,
81 struct berval *newpass,
84 int rc = LDAP_SUCCESS;
87 char berbuf[LBER_ELEMENT_SIZEOF];
88 BerElement *ber = (BerElement *)berbuf;
90 if( reqdata == NULL ) {
94 if( reqdata->bv_len == 0 ) {
95 *text = "empty request data field";
96 return LDAP_PROTOCOL_ERROR;
99 /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
100 ber_init2( ber, reqdata, 0 );
102 tag = ber_scanf( ber, "{" /*}*/ );
104 if( tag != LBER_ERROR ) {
105 tag = ber_peek_tag( ber, &len );
108 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_ID ) {
111 LDAP_LOG( OPERATION, ERR,
112 "slap_passwd_parse: ID not allowed.\n", 0, 0, 0 );
114 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID not allowed.\n",
118 *text = "user must change own password";
119 rc = LDAP_UNWILLING_TO_PERFORM;
123 tag = ber_scanf( ber, "m", id );
125 if( tag == LBER_ERROR ) {
127 LDAP_LOG( OPERATION, ERR,
128 "slap_passwd_parse: ID parse failed.\n", 0, 0, 0 );
130 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
137 tag = ber_peek_tag( ber, &len);
140 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_OLD ) {
141 if( oldpass == NULL ) {
143 LDAP_LOG( OPERATION, ERR,
144 "slap_passwd_parse: OLD not allowed.\n" , 0, 0, 0 );
146 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD not allowed.\n",
150 *text = "use bind to verify old password";
151 rc = LDAP_UNWILLING_TO_PERFORM;
155 tag = ber_scanf( ber, "m", oldpass );
157 if( tag == LBER_ERROR ) {
159 LDAP_LOG( OPERATION, ERR,
160 "slap_passwd_parse: ID parse failed.\n" , 0, 0, 0 );
162 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
169 tag = ber_peek_tag( ber, &len );
172 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ) {
173 if( newpass == NULL ) {
175 LDAP_LOG( OPERATION, ERR,
176 "slap_passwd_parse: NEW not allowed.\n", 0, 0, 0 );
178 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW not allowed.\n",
182 *text = "user specified passwords disallowed";
183 rc = LDAP_UNWILLING_TO_PERFORM;
187 tag = ber_scanf( ber, "m", newpass );
189 if( tag == LBER_ERROR ) {
191 LDAP_LOG( OPERATION, ERR,
192 "slap_passwd_parse: OLD parse failed.\n", 0, 0, 0 );
194 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD parse failed.\n",
201 tag = ber_peek_tag( ber, &len );
207 LDAP_LOG( OPERATION, ERR,
208 "slap_passwd_parse: decoding error, len=%ld\n", (long)len, 0, 0 );
210 Debug( LDAP_DEBUG_TRACE,
211 "slap_passwd_parse: decoding error, len=%ld\n",
215 *text = "data decoding error";
216 rc = LDAP_PROTOCOL_ERROR;
223 struct berval * slap_passwd_return(
224 struct berval *cred )
227 struct berval *bv = NULL;
228 char berbuf[LBER_ELEMENT_SIZEOF];
229 /* opaque structure, size unknown but smaller than berbuf */
230 BerElement *ber = (BerElement *)berbuf;
232 assert( cred != NULL );
235 LDAP_LOG( OPERATION, ENTRY,
236 "slap_passwd_return: %ld\n",(long)cred->bv_len, 0, 0 );
238 Debug( LDAP_DEBUG_TRACE, "slap_passwd_return: %ld\n",
239 (long) cred->bv_len, 0, 0 );
242 ber_init_w_nullc( ber, LBER_USE_DER );
244 rc = ber_printf( ber, "{tON}",
245 LDAP_TAG_EXOP_MODIFY_PASSWD_GEN, cred );
248 (void) ber_flatten( ber, &bv );
266 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
267 ldap_pvt_thread_mutex_lock( &passwd_mutex );
269 lutil_passwd_sasl_conn = conn->c_sasl_authctx;
273 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
274 if( !lutil_passwd( bv, cred, NULL, text ) ) {
280 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
282 lutil_passwd_sasl_conn = NULL;
284 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
291 slap_passwd_generate( struct berval *pass )
295 LDAP_LOG( OPERATION, ENTRY, "slap_passwd_generate: begin\n", 0, 0, 0 );
297 Debug( LDAP_DEBUG_TRACE, "slap_passwd_generate\n", 0, 0, 0 );
300 * generate passwords of only 8 characters as some getpass(3)
301 * implementations truncate at 8 characters.
303 tmp = lutil_passwd_generate( 8 );
315 struct berval * cred,
320 #ifdef LUTIL_SHA1_BYTES
321 char* hash = default_passwd_hash ? default_passwd_hash : "{SSHA}";
323 char* hash = default_passwd_hash ? default_passwd_hash : "{SMD5}";
327 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
328 ldap_pvt_thread_mutex_lock( &passwd_mutex );
331 tmp = lutil_passwd_hash( cred , hash, text );
333 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
334 ldap_pvt_thread_mutex_unlock( &passwd_mutex );