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,
34 assert( reqoid != NULL );
35 assert( strcmp( LDAP_EXOP_MODIFY_PASSWD, reqoid ) == 0 );
37 if( op->o_dn.bv_len == 0 ) {
38 *text = "only authenticated users may change passwords";
39 return LDAP_STRONG_AUTH_REQUIRED;
42 if( conn->c_authz_backend == NULL || !conn->c_authz_backend->be_extended ) {
43 *text = "operation not supported for current user";
44 return LDAP_UNWILLING_TO_PERFORM;
48 struct berval passwd = BER_BVC( LDAP_EXOP_MODIFY_PASSWD );
50 rc = backend_check_restrictions( conn->c_authz_backend,
51 conn, op, &passwd, text );
54 if( rc != LDAP_SUCCESS ) {
58 if( conn->c_authz_backend->be_update_ndn.bv_len ) {
59 /* we SHOULD return a referral in this case */
60 *refs = referral_rewrite( conn->c_authz_backend->be_update_refs,
61 NULL, NULL, LDAP_SCOPE_DEFAULT );
65 rc = conn->c_authz_backend->be_extended(
66 conn->c_authz_backend, conn, op,
68 rspoid, rspdata, rspctrls,
75 int slap_passwd_parse( struct berval *reqdata,
77 struct berval *oldpass,
78 struct berval *newpass,
81 int rc = LDAP_SUCCESS;
85 BerElement *ber = (BerElement *)berbuf;
87 if( reqdata == NULL ) {
91 if( reqdata->bv_len == 0 ) {
92 *text = "empty request data field";
93 return LDAP_PROTOCOL_ERROR;
96 /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
97 ber_init2( ber, reqdata, 0 );
99 tag = ber_scanf( ber, "{" /*}*/ );
101 if( tag != LBER_ERROR ) {
102 tag = ber_peek_tag( ber, &len );
105 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_ID ) {
108 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
109 "slap_passwd_parse: ID not allowed.\n"));
111 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID not allowed.\n",
115 *text = "user must change own password";
116 rc = LDAP_UNWILLING_TO_PERFORM;
120 tag = ber_scanf( ber, "m", id );
122 if( tag == LBER_ERROR ) {
124 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
125 "slap_passwd_parse: ID parse failed.\n"));
127 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
134 tag = ber_peek_tag( ber, &len);
137 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_OLD ) {
138 if( oldpass == NULL ) {
140 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
141 "slap_passwd_parse: OLD not allowed.\n" ));
143 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD not allowed.\n",
147 *text = "use bind to verify old password";
148 rc = LDAP_UNWILLING_TO_PERFORM;
152 tag = ber_scanf( ber, "m", oldpass );
154 if( tag == LBER_ERROR ) {
156 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
157 "slap_passwd_parse: ID parse failed.\n" ));
159 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
166 tag = ber_peek_tag( ber, &len );
169 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ) {
170 if( newpass == NULL ) {
172 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
173 "slap_passwd_parse: NEW not allowed.\n" ));
175 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW not allowed.\n",
179 *text = "user specified passwords disallowed";
180 rc = LDAP_UNWILLING_TO_PERFORM;
184 tag = ber_scanf( ber, "m", newpass );
186 if( tag == LBER_ERROR ) {
188 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
189 "slap_passwd_parse: OLD parse failed.\n"));
191 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD parse failed.\n",
198 tag = ber_peek_tag( ber, &len );
204 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
205 "slap_passwd_parse: decoding error, len=%ld\n", (long)len ));
207 Debug( LDAP_DEBUG_TRACE,
208 "slap_passwd_parse: decoding error, len=%ld\n",
212 *text = "data decoding error";
213 rc = LDAP_PROTOCOL_ERROR;
220 struct berval * slap_passwd_return(
221 struct berval *cred )
224 struct berval *bv = NULL;
226 /* opaque structure, size unknown but smaller than berbuf */
227 BerElement *ber = (BerElement *)berbuf;
229 assert( cred != NULL );
232 LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
233 "slap_passwd_return: %ld\n",(long)cred->bv_len ));
235 Debug( LDAP_DEBUG_TRACE, "slap_passwd_return: %ld\n",
236 (long) cred->bv_len, 0, 0 );
239 ber_init_w_nullc( ber, LBER_USE_DER );
241 rc = ber_printf( ber, "{tON}",
242 LDAP_TAG_EXOP_MODIFY_PASSWD_GEN, cred );
245 (void) ber_flatten( ber, &bv );
257 struct berval *cred )
262 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
263 ldap_pvt_thread_mutex_lock( &passwd_mutex );
265 lutil_passwd_sasl_conn = conn->c_sasl_context;
269 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
270 if( !lutil_passwd( bv, cred, NULL ) ) {
276 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
278 lutil_passwd_sasl_conn = NULL;
280 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
287 slap_passwd_generate( struct berval *pass )
291 LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
292 "slap_passwd_generate: begin\n" ));
294 Debug( LDAP_DEBUG_TRACE, "slap_passwd_generate\n", 0, 0, 0 );
297 * generate passwords of only 8 characters as some getpass(3)
298 * implementations truncate at 8 characters.
300 tmp = lutil_passwd_generate( 8 );
312 struct berval * cred,
313 struct berval * new )
316 #ifdef LUTIL_SHA1_BYTES
317 char* hash = default_passwd_hash ? default_passwd_hash : "{SSHA}";
319 char* hash = default_passwd_hash ? default_passwd_hash : "{SMD5}";
323 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
324 ldap_pvt_thread_mutex_lock( &passwd_mutex );
327 tmp = lutil_passwd_hash( cred , hash );
329 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
330 ldap_pvt_thread_mutex_unlock( &passwd_mutex );