1 /* bind.c - ldbm backend bind and unbind routines */
4 * Copyright 1998-2000 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,
29 struct berval ***refs )
33 assert( reqoid != NULL );
34 assert( strcmp( LDAP_EXOP_X_MODIFY_PASSWD, reqoid ) == 0 );
36 if( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
37 *text = "only authenicated 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 != NULL ) {
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, "O", 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, "O", 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, "O", 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 ) {
223 if( oldpass != NULL ) {
224 ber_bvfree( *oldpass );
228 if( newpass != NULL ) {
229 ber_bvfree( *newpass );
238 struct berval * slap_passwd_return(
239 struct berval *cred )
243 BerElement *ber = ber_alloc_t(LBER_USE_DER);
245 assert( cred != NULL );
248 LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
249 "slap_passwd_return: %ld\n",(long)cred->bv_len ));
251 Debug( LDAP_DEBUG_TRACE, "slap_passwd_return: %ld\n",
252 (long) cred->bv_len, 0, 0 );
256 if( ber == NULL ) return NULL;
258 rc = ber_printf( ber, "{tON}",
259 LDAP_TAG_EXOP_X_MODIFY_PASSWD_GEN, cred );
266 (void) ber_flatten( ber, &bv );
277 struct berval *cred )
282 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
283 ldap_pvt_thread_mutex_lock( &passwd_mutex );
285 lutil_passwd_sasl_conn = conn->c_sasl_context;
289 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
290 if( !lutil_passwd( a->a_vals[i], cred, NULL ) ) {
296 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
298 lutil_passwd_sasl_conn = NULL;
300 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
306 struct berval * slap_passwd_generate( void )
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 );
317 * generate passwords of only 8 characters as some getpass(3)
318 * implementations truncate at 8 characters.
320 return lutil_passwd_generate( 8 );
323 struct berval * slap_passwd_hash(
324 struct berval * cred )
326 #ifdef LUTIL_SHA1_BYTES
327 char* hash = default_passwd_hash ? default_passwd_hash : "{SSHA}";
329 char* hash = default_passwd_hash ? default_passwd_hash : "{SMD5}";
335 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
336 ldap_pvt_thread_mutex_lock( &passwd_mutex );
339 new = lutil_passwd_hash( cred , hash );
341 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
342 ldap_pvt_thread_mutex_unlock( &passwd_mutex );