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 #if defined(LDAP_SYNCREPL) || !defined(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;
61 if ( op->o_bd->syncinfo ) {
62 defref = op->o_bd->syncinfo->provideruri_bv;
66 defref = referral_rewrite( op->o_bd->be_update_refs,
67 NULL, NULL, LDAP_SCOPE_DEFAULT );
70 rs->sr_err = LDAP_REFERRAL;
71 #endif /* !SLAPD_MULTIMASTER */
74 rs->sr_err = op->o_bd->be_extended( op, rs );
80 int slap_passwd_parse( struct berval *reqdata,
82 struct berval *oldpass,
83 struct berval *newpass,
86 int rc = LDAP_SUCCESS;
89 char berbuf[LBER_ELEMENT_SIZEOF];
90 BerElement *ber = (BerElement *)berbuf;
92 if( reqdata == NULL ) {
96 if( reqdata->bv_len == 0 ) {
97 *text = "empty request data field";
98 return LDAP_PROTOCOL_ERROR;
101 /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
102 ber_init2( ber, reqdata, 0 );
104 tag = ber_scanf( ber, "{" /*}*/ );
106 if( tag != LBER_ERROR ) {
107 tag = ber_peek_tag( ber, &len );
110 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_ID ) {
113 LDAP_LOG( OPERATION, ERR,
114 "slap_passwd_parse: ID not allowed.\n", 0, 0, 0 );
116 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID not allowed.\n",
120 *text = "user must change own password";
121 rc = LDAP_UNWILLING_TO_PERFORM;
125 tag = ber_scanf( ber, "m", id );
127 if( tag == LBER_ERROR ) {
129 LDAP_LOG( OPERATION, ERR,
130 "slap_passwd_parse: ID parse failed.\n", 0, 0, 0 );
132 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
139 tag = ber_peek_tag( ber, &len);
142 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_OLD ) {
143 if( oldpass == NULL ) {
145 LDAP_LOG( OPERATION, ERR,
146 "slap_passwd_parse: OLD not allowed.\n" , 0, 0, 0 );
148 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD not allowed.\n",
152 *text = "use bind to verify old password";
153 rc = LDAP_UNWILLING_TO_PERFORM;
157 tag = ber_scanf( ber, "m", oldpass );
159 if( tag == LBER_ERROR ) {
161 LDAP_LOG( OPERATION, ERR,
162 "slap_passwd_parse: ID parse failed.\n" , 0, 0, 0 );
164 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
171 tag = ber_peek_tag( ber, &len );
174 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ) {
175 if( newpass == NULL ) {
177 LDAP_LOG( OPERATION, ERR,
178 "slap_passwd_parse: NEW not allowed.\n", 0, 0, 0 );
180 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW not allowed.\n",
184 *text = "user specified passwords disallowed";
185 rc = LDAP_UNWILLING_TO_PERFORM;
189 tag = ber_scanf( ber, "m", newpass );
191 if( tag == LBER_ERROR ) {
193 LDAP_LOG( OPERATION, ERR,
194 "slap_passwd_parse: OLD parse failed.\n", 0, 0, 0 );
196 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD parse failed.\n",
203 tag = ber_peek_tag( ber, &len );
209 LDAP_LOG( OPERATION, ERR,
210 "slap_passwd_parse: decoding error, len=%ld\n", (long)len, 0, 0 );
212 Debug( LDAP_DEBUG_TRACE,
213 "slap_passwd_parse: decoding error, len=%ld\n",
217 *text = "data decoding error";
218 rc = LDAP_PROTOCOL_ERROR;
225 struct berval * slap_passwd_return(
226 struct berval *cred )
229 struct berval *bv = NULL;
230 char berbuf[LBER_ELEMENT_SIZEOF];
231 /* opaque structure, size unknown but smaller than berbuf */
232 BerElement *ber = (BerElement *)berbuf;
234 assert( cred != NULL );
237 LDAP_LOG( OPERATION, ENTRY,
238 "slap_passwd_return: %ld\n",(long)cred->bv_len, 0, 0 );
240 Debug( LDAP_DEBUG_TRACE, "slap_passwd_return: %ld\n",
241 (long) cred->bv_len, 0, 0 );
244 ber_init_w_nullc( ber, LBER_USE_DER );
246 rc = ber_printf( ber, "{tON}",
247 LDAP_TAG_EXOP_MODIFY_PASSWD_GEN, cred );
250 (void) ber_flatten( ber, &bv );
268 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
269 ldap_pvt_thread_mutex_lock( &passwd_mutex );
271 lutil_passwd_sasl_conn = conn->c_sasl_authctx;
275 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
276 if( !lutil_passwd( bv, cred, NULL, text ) ) {
282 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
284 lutil_passwd_sasl_conn = NULL;
286 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
293 slap_passwd_generate( struct berval *pass )
297 LDAP_LOG( OPERATION, ENTRY, "slap_passwd_generate: begin\n", 0, 0, 0 );
299 Debug( LDAP_DEBUG_TRACE, "slap_passwd_generate\n", 0, 0, 0 );
302 * generate passwords of only 8 characters as some getpass(3)
303 * implementations truncate at 8 characters.
305 tmp = lutil_passwd_generate( 8 );
317 struct berval * cred,
322 #ifdef LUTIL_SHA1_BYTES
323 char* hash = default_passwd_hash ? default_passwd_hash : "{SSHA}";
325 char* hash = default_passwd_hash ? default_passwd_hash : "{SMD5}";
329 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
330 ldap_pvt_thread_mutex_lock( &passwd_mutex );
333 tmp = lutil_passwd_hash( cred , hash, text );
335 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
336 ldap_pvt_thread_mutex_unlock( &passwd_mutex );