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 ( !LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
62 LDAP_STAILQ_FOREACH( si, &op->o_bd->be_syncinfo, si_next ) {
64 ber_dupbv( &tmpbv, &si->si_provideruri_bv[0] );
65 ber_bvarray_add( &defref, &tmpbv );
68 defref = referral_rewrite( op->o_bd->be_update_refs,
69 NULL, NULL, LDAP_SCOPE_DEFAULT );
72 rs->sr_err = LDAP_REFERRAL;
73 #endif /* !SLAPD_MULTIMASTER */
76 rs->sr_err = op->o_bd->be_extended( op, rs );
82 int slap_passwd_parse( struct berval *reqdata,
84 struct berval *oldpass,
85 struct berval *newpass,
88 int rc = LDAP_SUCCESS;
91 BerElementBuffer berbuf;
92 BerElement *ber = (BerElement *)&berbuf;
94 if( reqdata == NULL ) {
98 if( reqdata->bv_len == 0 ) {
99 *text = "empty request data field";
100 return LDAP_PROTOCOL_ERROR;
103 /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
104 ber_init2( ber, reqdata, 0 );
106 tag = ber_scanf( ber, "{" /*}*/ );
108 if( tag != LBER_ERROR ) {
109 tag = ber_peek_tag( ber, &len );
112 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_ID ) {
115 LDAP_LOG( OPERATION, ERR,
116 "slap_passwd_parse: ID not allowed.\n", 0, 0, 0 );
118 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID not allowed.\n",
122 *text = "user must change own password";
123 rc = LDAP_UNWILLING_TO_PERFORM;
127 tag = ber_scanf( ber, "m", id );
129 if( tag == LBER_ERROR ) {
131 LDAP_LOG( OPERATION, ERR,
132 "slap_passwd_parse: ID parse failed.\n", 0, 0, 0 );
134 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
141 tag = ber_peek_tag( ber, &len);
144 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_OLD ) {
145 if( oldpass == NULL ) {
147 LDAP_LOG( OPERATION, ERR,
148 "slap_passwd_parse: OLD not allowed.\n" , 0, 0, 0 );
150 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD not allowed.\n",
154 *text = "use bind to verify old password";
155 rc = LDAP_UNWILLING_TO_PERFORM;
159 tag = ber_scanf( ber, "m", oldpass );
161 if( tag == LBER_ERROR ) {
163 LDAP_LOG( OPERATION, ERR,
164 "slap_passwd_parse: ID parse failed.\n" , 0, 0, 0 );
166 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
173 tag = ber_peek_tag( ber, &len );
176 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ) {
177 if( newpass == NULL ) {
179 LDAP_LOG( OPERATION, ERR,
180 "slap_passwd_parse: NEW not allowed.\n", 0, 0, 0 );
182 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW not allowed.\n",
186 *text = "user specified passwords disallowed";
187 rc = LDAP_UNWILLING_TO_PERFORM;
191 tag = ber_scanf( ber, "m", newpass );
193 if( tag == LBER_ERROR ) {
195 LDAP_LOG( OPERATION, ERR,
196 "slap_passwd_parse: OLD parse failed.\n", 0, 0, 0 );
198 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD parse failed.\n",
205 tag = ber_peek_tag( ber, &len );
211 LDAP_LOG( OPERATION, ERR,
212 "slap_passwd_parse: decoding error, len=%ld\n", (long)len, 0, 0 );
214 Debug( LDAP_DEBUG_TRACE,
215 "slap_passwd_parse: decoding error, len=%ld\n",
219 *text = "data decoding error";
220 rc = LDAP_PROTOCOL_ERROR;
227 struct berval * slap_passwd_return(
228 struct berval *cred )
231 struct berval *bv = NULL;
232 BerElementBuffer berbuf;
233 /* opaque structure, size unknown but smaller than berbuf */
234 BerElement *ber = (BerElement *)&berbuf;
236 assert( cred != NULL );
239 LDAP_LOG( OPERATION, ENTRY,
240 "slap_passwd_return: %ld\n",(long)cred->bv_len, 0, 0 );
242 Debug( LDAP_DEBUG_TRACE, "slap_passwd_return: %ld\n",
243 (long) cred->bv_len, 0, 0 );
246 ber_init_w_nullc( ber, LBER_USE_DER );
248 rc = ber_printf( ber, "{tON}",
249 LDAP_TAG_EXOP_MODIFY_PASSWD_GEN, cred );
252 (void) ber_flatten( ber, &bv );
270 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
271 ldap_pvt_thread_mutex_lock( &passwd_mutex );
273 lutil_passwd_sasl_conn = conn->c_sasl_authctx;
277 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
278 if( !lutil_passwd( bv, cred, NULL, text ) ) {
284 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
286 lutil_passwd_sasl_conn = NULL;
288 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
295 slap_passwd_generate( struct berval *pass )
299 LDAP_LOG( OPERATION, ENTRY, "slap_passwd_generate: begin\n", 0, 0, 0 );
301 Debug( LDAP_DEBUG_TRACE, "slap_passwd_generate\n", 0, 0, 0 );
304 * generate passwords of only 8 characters as some getpass(3)
305 * implementations truncate at 8 characters.
307 tmp = lutil_passwd_generate( 8 );
319 struct berval * cred,
324 #ifdef LUTIL_SHA1_BYTES
325 char* hash = default_passwd_hash ? default_passwd_hash : "{SSHA}";
327 char* hash = default_passwd_hash ? default_passwd_hash : "{SMD5}";
331 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
332 ldap_pvt_thread_mutex_lock( &passwd_mutex );
335 tmp = lutil_passwd_hash( cred , hash, text );
337 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
338 ldap_pvt_thread_mutex_unlock( &passwd_mutex );