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;
63 defref = referral_rewrite( op->o_bd->be_update_refs,
64 NULL, NULL, LDAP_SCOPE_DEFAULT );
67 rs->sr_err = LDAP_REFERRAL;
68 #endif /* !SLAPD_MULTIMASTER */
71 rs->sr_err = op->o_bd->be_extended( op, rs );
77 int slap_passwd_parse( struct berval *reqdata,
79 struct berval *oldpass,
80 struct berval *newpass,
83 int rc = LDAP_SUCCESS;
86 char berbuf[LBER_ELEMENT_SIZEOF];
87 BerElement *ber = (BerElement *)berbuf;
89 if( reqdata == NULL ) {
93 if( reqdata->bv_len == 0 ) {
94 *text = "empty request data field";
95 return LDAP_PROTOCOL_ERROR;
98 /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
99 ber_init2( ber, reqdata, 0 );
101 tag = ber_scanf( ber, "{" /*}*/ );
103 if( tag != LBER_ERROR ) {
104 tag = ber_peek_tag( ber, &len );
107 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_ID ) {
110 LDAP_LOG( OPERATION, ERR,
111 "slap_passwd_parse: ID not allowed.\n", 0, 0, 0 );
113 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID not allowed.\n",
117 *text = "user must change own password";
118 rc = LDAP_UNWILLING_TO_PERFORM;
122 tag = ber_scanf( ber, "m", id );
124 if( tag == LBER_ERROR ) {
126 LDAP_LOG( OPERATION, ERR,
127 "slap_passwd_parse: ID parse failed.\n", 0, 0, 0 );
129 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
136 tag = ber_peek_tag( ber, &len);
139 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_OLD ) {
140 if( oldpass == NULL ) {
142 LDAP_LOG( OPERATION, ERR,
143 "slap_passwd_parse: OLD not allowed.\n" , 0, 0, 0 );
145 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD not allowed.\n",
149 *text = "use bind to verify old password";
150 rc = LDAP_UNWILLING_TO_PERFORM;
154 tag = ber_scanf( ber, "m", oldpass );
156 if( tag == LBER_ERROR ) {
158 LDAP_LOG( OPERATION, ERR,
159 "slap_passwd_parse: ID parse failed.\n" , 0, 0, 0 );
161 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
168 tag = ber_peek_tag( ber, &len );
171 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ) {
172 if( newpass == NULL ) {
174 LDAP_LOG( OPERATION, ERR,
175 "slap_passwd_parse: NEW not allowed.\n", 0, 0, 0 );
177 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW not allowed.\n",
181 *text = "user specified passwords disallowed";
182 rc = LDAP_UNWILLING_TO_PERFORM;
186 tag = ber_scanf( ber, "m", newpass );
188 if( tag == LBER_ERROR ) {
190 LDAP_LOG( OPERATION, ERR,
191 "slap_passwd_parse: OLD parse failed.\n", 0, 0, 0 );
193 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD parse failed.\n",
200 tag = ber_peek_tag( ber, &len );
206 LDAP_LOG( OPERATION, ERR,
207 "slap_passwd_parse: decoding error, len=%ld\n", (long)len, 0, 0 );
209 Debug( LDAP_DEBUG_TRACE,
210 "slap_passwd_parse: decoding error, len=%ld\n",
214 *text = "data decoding error";
215 rc = LDAP_PROTOCOL_ERROR;
222 struct berval * slap_passwd_return(
223 struct berval *cred )
226 struct berval *bv = NULL;
227 char berbuf[LBER_ELEMENT_SIZEOF];
228 /* opaque structure, size unknown but smaller than berbuf */
229 BerElement *ber = (BerElement *)berbuf;
231 assert( cred != NULL );
234 LDAP_LOG( OPERATION, ENTRY,
235 "slap_passwd_return: %ld\n",(long)cred->bv_len, 0, 0 );
237 Debug( LDAP_DEBUG_TRACE, "slap_passwd_return: %ld\n",
238 (long) cred->bv_len, 0, 0 );
241 ber_init_w_nullc( ber, LBER_USE_DER );
243 rc = ber_printf( ber, "{tON}",
244 LDAP_TAG_EXOP_MODIFY_PASSWD_GEN, cred );
247 (void) ber_flatten( ber, &bv );
265 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
266 ldap_pvt_thread_mutex_lock( &passwd_mutex );
268 lutil_passwd_sasl_conn = conn->c_sasl_authctx;
272 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
273 if( !lutil_passwd( bv, cred, NULL, text ) ) {
279 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
281 lutil_passwd_sasl_conn = NULL;
283 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
290 slap_passwd_generate( struct berval *pass )
294 LDAP_LOG( OPERATION, ENTRY, "slap_passwd_generate: begin\n", 0, 0, 0 );
296 Debug( LDAP_DEBUG_TRACE, "slap_passwd_generate\n", 0, 0, 0 );
299 * generate passwords of only 8 characters as some getpass(3)
300 * implementations truncate at 8 characters.
302 tmp = lutil_passwd_generate( 8 );
314 struct berval * cred,
319 #ifdef LUTIL_SHA1_BYTES
320 char* hash = default_passwd_hash ? default_passwd_hash : "{SSHA}";
322 char* hash = default_passwd_hash ? default_passwd_hash : "{SMD5}";
326 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
327 ldap_pvt_thread_mutex_lock( &passwd_mutex );
330 tmp = lutil_passwd_hash( cred , hash, text );
332 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
333 ldap_pvt_thread_mutex_unlock( &passwd_mutex );