1 /* bind.c - ldbm backend bind and unbind routines */
4 * Copyright 1998-2002 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>
23 Connection *conn, Operation *op,
25 struct berval *reqdata,
27 struct berval **rspdata,
28 LDAPControl ***rspctrls,
35 assert( reqoid != NULL );
36 assert( strcmp( LDAP_EXOP_MODIFY_PASSWD, reqoid ) == 0 );
38 if( op->o_dn.bv_len == 0 ) {
39 *text = "only authenticated users may change passwords";
40 return LDAP_STRONG_AUTH_REQUIRED;
43 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
44 be = conn->c_authz_backend;
45 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
47 if( be && !be->be_extended ) {
48 *text = "operation not supported for current user";
49 return LDAP_UNWILLING_TO_PERFORM;
53 struct berval passwd = BER_BVC( LDAP_EXOP_MODIFY_PASSWD );
54 rc = backend_check_restrictions( be, conn, op, &passwd, text );
57 if( rc != LDAP_SUCCESS ) {
62 #ifdef HAVE_CYRUS_SASL
63 rc = slap_sasl_setpass( conn, op,
65 rspoid, rspdata, rspctrls,
68 *text = "no authz backend";
72 } else if( be->be_update_ndn.bv_len ) {
73 /* we SHOULD return a referral in this case */
74 *refs = referral_rewrite( be->be_update_refs,
75 NULL, NULL, LDAP_SCOPE_DEFAULT );
82 rspoid, rspdata, rspctrls,
89 int slap_passwd_parse( struct berval *reqdata,
91 struct berval *oldpass,
92 struct berval *newpass,
95 int rc = LDAP_SUCCESS;
98 char berbuf[LBER_ELEMENT_SIZEOF];
99 BerElement *ber = (BerElement *)berbuf;
101 if( reqdata == NULL ) {
105 if( reqdata->bv_len == 0 ) {
106 *text = "empty request data field";
107 return LDAP_PROTOCOL_ERROR;
110 /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
111 ber_init2( ber, reqdata, 0 );
113 tag = ber_scanf( ber, "{" /*}*/ );
115 if( tag != LBER_ERROR ) {
116 tag = ber_peek_tag( ber, &len );
119 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_ID ) {
122 LDAP_LOG( OPERATION, ERR,
123 "slap_passwd_parse: ID not allowed.\n", 0, 0, 0 );
125 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID not allowed.\n",
129 *text = "user must change own password";
130 rc = LDAP_UNWILLING_TO_PERFORM;
134 tag = ber_scanf( ber, "m", id );
136 if( tag == LBER_ERROR ) {
138 LDAP_LOG( OPERATION, ERR,
139 "slap_passwd_parse: ID parse failed.\n", 0, 0, 0 );
141 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
148 tag = ber_peek_tag( ber, &len);
151 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_OLD ) {
152 if( oldpass == NULL ) {
154 LDAP_LOG( OPERATION, ERR,
155 "slap_passwd_parse: OLD not allowed.\n" , 0, 0, 0 );
157 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD not allowed.\n",
161 *text = "use bind to verify old password";
162 rc = LDAP_UNWILLING_TO_PERFORM;
166 tag = ber_scanf( ber, "m", oldpass );
168 if( tag == LBER_ERROR ) {
170 LDAP_LOG( OPERATION, ERR,
171 "slap_passwd_parse: ID parse failed.\n" , 0, 0, 0 );
173 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
180 tag = ber_peek_tag( ber, &len );
183 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ) {
184 if( newpass == NULL ) {
186 LDAP_LOG( OPERATION, ERR,
187 "slap_passwd_parse: NEW not allowed.\n", 0, 0, 0 );
189 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW not allowed.\n",
193 *text = "user specified passwords disallowed";
194 rc = LDAP_UNWILLING_TO_PERFORM;
198 tag = ber_scanf( ber, "m", newpass );
200 if( tag == LBER_ERROR ) {
202 LDAP_LOG( OPERATION, ERR,
203 "slap_passwd_parse: OLD parse failed.\n", 0, 0, 0 );
205 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD parse failed.\n",
212 tag = ber_peek_tag( ber, &len );
218 LDAP_LOG( OPERATION, ERR,
219 "slap_passwd_parse: decoding error, len=%ld\n", (long)len, 0, 0 );
221 Debug( LDAP_DEBUG_TRACE,
222 "slap_passwd_parse: decoding error, len=%ld\n",
226 *text = "data decoding error";
227 rc = LDAP_PROTOCOL_ERROR;
234 struct berval * slap_passwd_return(
235 struct berval *cred )
238 struct berval *bv = NULL;
239 char berbuf[LBER_ELEMENT_SIZEOF];
240 /* opaque structure, size unknown but smaller than berbuf */
241 BerElement *ber = (BerElement *)berbuf;
243 assert( cred != NULL );
246 LDAP_LOG( OPERATION, ENTRY,
247 "slap_passwd_return: %ld\n",(long)cred->bv_len, 0, 0 );
249 Debug( LDAP_DEBUG_TRACE, "slap_passwd_return: %ld\n",
250 (long) cred->bv_len, 0, 0 );
253 ber_init_w_nullc( ber, LBER_USE_DER );
255 rc = ber_printf( ber, "{tON}",
256 LDAP_TAG_EXOP_MODIFY_PASSWD_GEN, cred );
259 (void) ber_flatten( ber, &bv );
271 struct berval *cred )
276 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
277 ldap_pvt_thread_mutex_lock( &passwd_mutex );
279 lutil_passwd_sasl_conn = conn->c_sasl_context;
283 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
284 if( !lutil_passwd( bv, cred, NULL ) ) {
290 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
292 lutil_passwd_sasl_conn = NULL;
294 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
301 slap_passwd_generate( struct berval *pass )
305 LDAP_LOG( OPERATION, ENTRY, "slap_passwd_generate: begin\n", 0, 0, 0 );
307 Debug( LDAP_DEBUG_TRACE, "slap_passwd_generate\n", 0, 0, 0 );
310 * generate passwords of only 8 characters as some getpass(3)
311 * implementations truncate at 8 characters.
313 tmp = lutil_passwd_generate( 8 );
325 struct berval * cred,
326 struct berval * new )
329 #ifdef LUTIL_SHA1_BYTES
330 char* hash = default_passwd_hash ? default_passwd_hash : "{SSHA}";
332 char* hash = default_passwd_hash ? default_passwd_hash : "{SMD5}";
336 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
337 ldap_pvt_thread_mutex_lock( &passwd_mutex );
340 tmp = lutil_passwd_hash( cred , hash );
342 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
343 ldap_pvt_thread_mutex_unlock( &passwd_mutex );