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 );
85 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ber_init failed\n",
87 *text = "password decoding error";
88 return LDAP_PROTOCOL_ERROR;
91 tag = ber_scanf( ber, "{" /*}*/ );
93 if( tag != LBER_ERROR ) {
94 tag = ber_peek_tag( ber, &len );
97 if( tag == LDAP_TAG_EXOP_X_MODIFY_PASSWD_ID ) {
99 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID not allowed.\n",
101 *text = "user must change own password";
102 rc = LDAP_UNWILLING_TO_PERFORM;
106 tag = ber_scanf( ber, "O", id );
108 if( tag == LBER_ERROR ) {
109 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
114 tag = ber_peek_tag( ber, &len);
117 if( tag == LDAP_TAG_EXOP_X_MODIFY_PASSWD_OLD ) {
118 if( oldpass == NULL ) {
119 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD not allowed.\n",
121 *text = "use bind to verify old password";
122 rc = LDAP_UNWILLING_TO_PERFORM;
126 tag = ber_scanf( ber, "O", oldpass );
128 if( tag == LBER_ERROR ) {
129 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
134 tag = ber_peek_tag( ber, &len);
137 if( tag == LDAP_TAG_EXOP_X_MODIFY_PASSWD_NEW ) {
138 if( newpass == NULL ) {
139 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW not allowed.\n",
141 *text = "user specified passwords disallowed";
142 rc = LDAP_UNWILLING_TO_PERFORM;
146 tag = ber_scanf( ber, "O", newpass );
148 if( tag == LBER_ERROR ) {
149 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD parse failed.\n",
154 tag = ber_peek_tag( ber, &len );
159 Debug( LDAP_DEBUG_TRACE,
160 "slap_passwd_parse: decoding error, len=%ld\n",
163 *text = "data decoding error";
164 rc = LDAP_PROTOCOL_ERROR;
168 if( rc != LDAP_SUCCESS ) {
174 if( oldpass != NULL ) {
175 ber_bvfree( *oldpass );
179 if( newpass != NULL ) {
180 ber_bvfree( *newpass );
189 struct berval * slap_passwd_return(
190 struct berval *cred )
194 BerElement *ber = ber_alloc_t(LBER_USE_DER);
196 assert( cred != NULL );
198 Debug( LDAP_DEBUG_TRACE, "slap_passwd_return: %ld\n",
199 (long) cred->bv_len, 0, 0 );
201 if( ber == NULL ) return NULL;
203 rc = ber_printf( ber, "{tON}",
204 LDAP_TAG_EXOP_X_MODIFY_PASSWD_GEN, cred );
211 (void) ber_flatten( ber, &bv );
222 struct berval *cred )
227 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
228 ldap_pvt_thread_mutex_lock( &passwd_mutex );
230 lutil_passwd_sasl_conn = conn->c_sasl_context;
234 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
235 if( !lutil_passwd( a->a_vals[i], cred, NULL ) ) {
241 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
243 lutil_passwd_sasl_conn = NULL;
245 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
251 struct berval * slap_passwd_generate( void )
253 Debug( LDAP_DEBUG_TRACE, "slap_passwd_generate\n", 0, 0, 0 );
256 * generate passwords of only 8 characters as some getpass(3)
257 * implementations truncate at 8 characters.
259 return lutil_passwd_generate( 8 );
262 struct berval * slap_passwd_hash(
263 struct berval * cred )
265 char* hash = default_passwd_hash ? default_passwd_hash : "{SSHA}";
269 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
270 ldap_pvt_thread_mutex_lock( &passwd_mutex );
273 new = lutil_passwd_hash( cred , hash );
275 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
276 ldap_pvt_thread_mutex_unlock( &passwd_mutex );