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 rs->sr_ref = referral_rewrite( op->o_bd->be_update_refs,
60 NULL, NULL, LDAP_SCOPE_DEFAULT );
61 rs->sr_err = LDAP_REFERRAL;
62 #endif /* !SLAPD_MULTIMASTER */
65 rs->sr_err = op->o_bd->be_extended( op, rs );
71 int slap_passwd_parse( struct berval *reqdata,
73 struct berval *oldpass,
74 struct berval *newpass,
77 int rc = LDAP_SUCCESS;
80 char berbuf[LBER_ELEMENT_SIZEOF];
81 BerElement *ber = (BerElement *)berbuf;
83 if( reqdata == NULL ) {
87 if( reqdata->bv_len == 0 ) {
88 *text = "empty request data field";
89 return LDAP_PROTOCOL_ERROR;
92 /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
93 ber_init2( ber, reqdata, 0 );
95 tag = ber_scanf( ber, "{" /*}*/ );
97 if( tag != LBER_ERROR ) {
98 tag = ber_peek_tag( ber, &len );
101 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_ID ) {
104 LDAP_LOG( OPERATION, ERR,
105 "slap_passwd_parse: ID not allowed.\n", 0, 0, 0 );
107 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID not allowed.\n",
111 *text = "user must change own password";
112 rc = LDAP_UNWILLING_TO_PERFORM;
116 tag = ber_scanf( ber, "m", id );
118 if( tag == LBER_ERROR ) {
120 LDAP_LOG( OPERATION, ERR,
121 "slap_passwd_parse: ID parse failed.\n", 0, 0, 0 );
123 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
130 tag = ber_peek_tag( ber, &len);
133 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_OLD ) {
134 if( oldpass == NULL ) {
136 LDAP_LOG( OPERATION, ERR,
137 "slap_passwd_parse: OLD not allowed.\n" , 0, 0, 0 );
139 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD not allowed.\n",
143 *text = "use bind to verify old password";
144 rc = LDAP_UNWILLING_TO_PERFORM;
148 tag = ber_scanf( ber, "m", oldpass );
150 if( tag == LBER_ERROR ) {
152 LDAP_LOG( OPERATION, ERR,
153 "slap_passwd_parse: ID parse failed.\n" , 0, 0, 0 );
155 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
162 tag = ber_peek_tag( ber, &len );
165 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ) {
166 if( newpass == NULL ) {
168 LDAP_LOG( OPERATION, ERR,
169 "slap_passwd_parse: NEW not allowed.\n", 0, 0, 0 );
171 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW not allowed.\n",
175 *text = "user specified passwords disallowed";
176 rc = LDAP_UNWILLING_TO_PERFORM;
180 tag = ber_scanf( ber, "m", newpass );
182 if( tag == LBER_ERROR ) {
184 LDAP_LOG( OPERATION, ERR,
185 "slap_passwd_parse: OLD parse failed.\n", 0, 0, 0 );
187 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD parse failed.\n",
194 tag = ber_peek_tag( ber, &len );
200 LDAP_LOG( OPERATION, ERR,
201 "slap_passwd_parse: decoding error, len=%ld\n", (long)len, 0, 0 );
203 Debug( LDAP_DEBUG_TRACE,
204 "slap_passwd_parse: decoding error, len=%ld\n",
208 *text = "data decoding error";
209 rc = LDAP_PROTOCOL_ERROR;
216 struct berval * slap_passwd_return(
217 struct berval *cred )
220 struct berval *bv = NULL;
221 char berbuf[LBER_ELEMENT_SIZEOF];
222 /* opaque structure, size unknown but smaller than berbuf */
223 BerElement *ber = (BerElement *)berbuf;
225 assert( cred != NULL );
228 LDAP_LOG( OPERATION, ENTRY,
229 "slap_passwd_return: %ld\n",(long)cred->bv_len, 0, 0 );
231 Debug( LDAP_DEBUG_TRACE, "slap_passwd_return: %ld\n",
232 (long) cred->bv_len, 0, 0 );
235 ber_init_w_nullc( ber, LBER_USE_DER );
237 rc = ber_printf( ber, "{tON}",
238 LDAP_TAG_EXOP_MODIFY_PASSWD_GEN, cred );
241 (void) ber_flatten( ber, &bv );
253 struct berval *cred )
258 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
259 ldap_pvt_thread_mutex_lock( &passwd_mutex );
261 lutil_passwd_sasl_conn = conn->c_sasl_context;
265 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
266 if( !lutil_passwd( bv, cred, NULL ) ) {
272 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
274 lutil_passwd_sasl_conn = NULL;
276 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
283 slap_passwd_generate( struct berval *pass )
287 LDAP_LOG( OPERATION, ENTRY, "slap_passwd_generate: begin\n", 0, 0, 0 );
289 Debug( LDAP_DEBUG_TRACE, "slap_passwd_generate\n", 0, 0, 0 );
292 * generate passwords of only 8 characters as some getpass(3)
293 * implementations truncate at 8 characters.
295 tmp = lutil_passwd_generate( 8 );
307 struct berval * cred,
308 struct berval * new )
311 #ifdef LUTIL_SHA1_BYTES
312 char* hash = default_passwd_hash ? default_passwd_hash : "{SSHA}";
314 char* hash = default_passwd_hash ? default_passwd_hash : "{SMD5}";
318 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
319 ldap_pvt_thread_mutex_lock( &passwd_mutex );
322 tmp = lutil_passwd_hash( cred , hash );
324 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
325 ldap_pvt_thread_mutex_unlock( &passwd_mutex );