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 = conn->c_authz_backend->be_update_refs;
52 rc = conn->c_authz_backend->be_extended(
53 conn->c_authz_backend, conn, op,
55 rspoid, rspdata, rspctrls,
60 *text = "operation not supported for current user";
61 rc = LDAP_UNWILLING_TO_PERFORM;
67 int slap_passwd_parse( struct berval *reqdata,
69 struct berval **oldpass,
70 struct berval **newpass,
73 int rc = LDAP_SUCCESS;
78 if( reqdata == NULL ) {
82 ber = ber_init( reqdata );
86 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
87 "slap_passwd_parse: ber_init failed\n" ));
89 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ber_init failed\n",
93 *text = "password decoding error";
94 return LDAP_PROTOCOL_ERROR;
97 tag = ber_scanf( ber, "{" /*}*/ );
99 if( tag != LBER_ERROR ) {
100 tag = ber_peek_tag( ber, &len );
103 if( tag == LDAP_TAG_EXOP_X_MODIFY_PASSWD_ID ) {
106 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
107 "slap_passwd_parse: ID not allowed.\n"));
109 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID not allowed.\n",
113 *text = "user must change own password";
114 rc = LDAP_UNWILLING_TO_PERFORM;
118 tag = ber_scanf( ber, "O", id );
120 if( tag == LBER_ERROR ) {
122 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
123 "slap_passwd_parse: ID parse failed.\n"));
125 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
132 tag = ber_peek_tag( ber, &len);
135 if( tag == LDAP_TAG_EXOP_X_MODIFY_PASSWD_OLD ) {
136 if( oldpass == NULL ) {
138 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
139 "slap_passwd_parse: OLD not allowed.\n" ));
141 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD not allowed.\n",
145 *text = "use bind to verify old password";
146 rc = LDAP_UNWILLING_TO_PERFORM;
150 tag = ber_scanf( ber, "O", oldpass );
152 if( tag == LBER_ERROR ) {
154 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
155 "slap_passwd_parse: ID parse failed.\n" ));
157 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
164 tag = ber_peek_tag( ber, &len);
167 if( tag == LDAP_TAG_EXOP_X_MODIFY_PASSWD_NEW ) {
168 if( newpass == NULL ) {
170 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
171 "slap_passwd_parse: NEW not allowed.\n" ));
173 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW not allowed.\n",
177 *text = "user specified passwords disallowed";
178 rc = LDAP_UNWILLING_TO_PERFORM;
182 tag = ber_scanf( ber, "O", newpass );
184 if( tag == LBER_ERROR ) {
186 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
187 "slap_passwd_parse: OLD parse failed.\n"));
189 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD parse failed.\n",
196 tag = ber_peek_tag( ber, &len );
202 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
203 "slap_passwd_parse: decoding error, len=%ld\n", (long)len ));
205 Debug( LDAP_DEBUG_TRACE,
206 "slap_passwd_parse: decoding error, len=%ld\n",
211 *text = "data decoding error";
212 rc = LDAP_PROTOCOL_ERROR;
216 if( rc != LDAP_SUCCESS ) {
222 if( oldpass != NULL ) {
223 ber_bvfree( *oldpass );
227 if( newpass != NULL ) {
228 ber_bvfree( *newpass );
237 struct berval * slap_passwd_return(
238 struct berval *cred )
242 BerElement *ber = ber_alloc_t(LBER_USE_DER);
244 assert( cred != NULL );
247 LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
248 "slap_passwd_return: %ld\n",(long)cred->bv_len ));
250 Debug( LDAP_DEBUG_TRACE, "slap_passwd_return: %ld\n",
251 (long) cred->bv_len, 0, 0 );
255 if( ber == NULL ) return NULL;
257 rc = ber_printf( ber, "{tON}",
258 LDAP_TAG_EXOP_X_MODIFY_PASSWD_GEN, cred );
265 (void) ber_flatten( ber, &bv );
276 struct berval *cred )
281 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
282 ldap_pvt_thread_mutex_lock( &passwd_mutex );
284 lutil_passwd_sasl_conn = conn->c_sasl_context;
288 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
289 if( !lutil_passwd( a->a_vals[i], cred, NULL ) ) {
295 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
297 lutil_passwd_sasl_conn = NULL;
299 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
305 struct berval * slap_passwd_generate( void )
308 LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
309 "slap_passwd_generate: begin\n" ));
311 Debug( LDAP_DEBUG_TRACE, "slap_passwd_generate\n", 0, 0, 0 );
316 * generate passwords of only 8 characters as some getpass(3)
317 * implementations truncate at 8 characters.
319 return lutil_passwd_generate( 8 );
322 struct berval * slap_passwd_hash(
323 struct berval * cred )
325 char* hash = default_passwd_hash ? default_passwd_hash : "{SSHA}";
329 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
330 ldap_pvt_thread_mutex_lock( &passwd_mutex );
333 new = lutil_passwd_hash( cred , hash );
335 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
336 ldap_pvt_thread_mutex_unlock( &passwd_mutex );