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>
22 Connection *conn, Operation *op,
24 struct berval *reqdata,
26 struct berval **rspdata,
27 LDAPControl ***rspctrls,
33 assert( reqoid != NULL );
34 assert( strcmp( LDAP_EXOP_X_MODIFY_PASSWD, reqoid ) == 0 );
36 if( op->o_dn.bv_len == 0 ) {
37 *text = "only authenticated users may change passwords";
38 return LDAP_STRONG_AUTH_REQUIRED;
41 if( conn->c_authz_backend != NULL && conn->c_authz_backend->be_extended ) {
42 if( conn->c_authz_backend->be_restrictops & SLAP_RESTRICT_OP_MODIFY ) {
43 *text = "authorization database is read only";
44 rc = LDAP_UNWILLING_TO_PERFORM;
46 } else if( conn->c_authz_backend->be_update_ndn.bv_len ) {
47 /* we SHOULD return a referral in this case */
48 *refs = referral_rewrite( conn->c_authz_backend->be_update_refs,
49 NULL, NULL, LDAP_SCOPE_DEFAULT );
53 rc = conn->c_authz_backend->be_extended(
54 conn->c_authz_backend, conn, op,
56 rspoid, rspdata, rspctrls,
61 *text = "operation not supported for current user";
62 rc = LDAP_UNWILLING_TO_PERFORM;
68 int slap_passwd_parse( struct berval *reqdata,
70 struct berval *oldpass,
71 struct berval *newpass,
74 int rc = LDAP_SUCCESS;
79 if( reqdata == NULL ) {
83 ber = ber_init( reqdata );
87 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
88 "slap_passwd_parse: ber_init failed\n" ));
90 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ber_init failed\n",
94 *text = "password decoding error";
95 return LDAP_PROTOCOL_ERROR;
98 tag = ber_scanf( ber, "{" /*}*/ );
100 if( tag != LBER_ERROR ) {
101 tag = ber_peek_tag( ber, &len );
104 if( tag == LDAP_TAG_EXOP_X_MODIFY_PASSWD_ID ) {
107 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
108 "slap_passwd_parse: ID not allowed.\n"));
110 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID not allowed.\n",
114 *text = "user must change own password";
115 rc = LDAP_UNWILLING_TO_PERFORM;
119 tag = ber_scanf( ber, "m", id );
121 if( tag == LBER_ERROR ) {
123 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
124 "slap_passwd_parse: ID parse failed.\n"));
126 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
133 tag = ber_peek_tag( ber, &len);
136 if( tag == LDAP_TAG_EXOP_X_MODIFY_PASSWD_OLD ) {
137 if( oldpass == NULL ) {
139 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
140 "slap_passwd_parse: OLD not allowed.\n" ));
142 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD not allowed.\n",
146 *text = "use bind to verify old password";
147 rc = LDAP_UNWILLING_TO_PERFORM;
151 tag = ber_scanf( ber, "m", oldpass );
153 if( tag == LBER_ERROR ) {
155 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
156 "slap_passwd_parse: ID parse failed.\n" ));
158 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
165 tag = ber_peek_tag( ber, &len);
168 if( tag == LDAP_TAG_EXOP_X_MODIFY_PASSWD_NEW ) {
169 if( newpass == NULL ) {
171 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
172 "slap_passwd_parse: NEW not allowed.\n" ));
174 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW not allowed.\n",
178 *text = "user specified passwords disallowed";
179 rc = LDAP_UNWILLING_TO_PERFORM;
183 tag = ber_scanf( ber, "m", newpass );
185 if( tag == LBER_ERROR ) {
187 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
188 "slap_passwd_parse: OLD parse failed.\n"));
190 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD parse failed.\n",
197 tag = ber_peek_tag( ber, &len );
203 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
204 "slap_passwd_parse: decoding error, len=%ld\n", (long)len ));
206 Debug( LDAP_DEBUG_TRACE,
207 "slap_passwd_parse: decoding error, len=%ld\n",
212 *text = "data decoding error";
213 rc = LDAP_PROTOCOL_ERROR;
217 if( rc != LDAP_SUCCESS ) {
218 if( id && id->bv_val != NULL ) {
223 if( oldpass && oldpass->bv_val != NULL ) {
224 free( oldpass->bv_val );
225 oldpass->bv_val = NULL;
228 if( newpass && newpass->bv_val != NULL ) {
229 free( newpass->bv_val );
230 newpass->bv_val = NULL;
238 struct berval * slap_passwd_return(
239 struct berval *cred )
242 struct berval *bv = NULL;
244 /* opaque structure, size unknown but smaller than berbuf */
245 BerElement *ber = (BerElement *)berbuf;
247 assert( cred != NULL );
250 LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
251 "slap_passwd_return: %ld\n",(long)cred->bv_len ));
253 Debug( LDAP_DEBUG_TRACE, "slap_passwd_return: %ld\n",
254 (long) cred->bv_len, 0, 0 );
257 ber_init_w_nullc( ber, LBER_USE_DER );
259 rc = ber_printf( ber, "{tON}",
260 LDAP_TAG_EXOP_X_MODIFY_PASSWD_GEN, cred );
263 (void) ber_flatten( ber, &bv );
275 struct berval *cred )
280 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
281 ldap_pvt_thread_mutex_lock( &passwd_mutex );
283 lutil_passwd_sasl_conn = conn->c_sasl_context;
287 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
288 if( !lutil_passwd( bv, cred, NULL ) ) {
294 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
296 lutil_passwd_sasl_conn = NULL;
298 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
305 slap_passwd_generate( struct berval *pass )
309 LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
310 "slap_passwd_generate: begin\n" ));
312 Debug( LDAP_DEBUG_TRACE, "slap_passwd_generate\n", 0, 0, 0 );
315 * generate passwords of only 8 characters as some getpass(3)
316 * implementations truncate at 8 characters.
318 tmp = lutil_passwd_generate( 8 );
330 struct berval * cred,
331 struct berval * new )
334 #ifdef LUTIL_SHA1_BYTES
335 char* hash = default_passwd_hash ? default_passwd_hash : "{SSHA}";
337 char* hash = default_passwd_hash ? default_passwd_hash : "{SMD5}";
341 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
342 ldap_pvt_thread_mutex_lock( &passwd_mutex );
345 tmp = lutil_passwd_hash( cred , hash );
347 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
348 ldap_pvt_thread_mutex_unlock( &passwd_mutex );