1 /* bind.c - ldbm backend bind and unbind routines */
4 * Copyright 1998-2003 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,
24 struct berval *reqoid,
25 struct berval *reqdata,
27 struct berval **rspdata,
28 LDAPControl ***rspctrls,
35 assert( reqoid != NULL );
36 assert( ber_bvcmp( &slap_EXOP_MODIFY_PASSWD, reqoid ) == 0 );
38 if( op->o_dn.bv_len == 0 ) {
39 *text = "only authenticated users may change passwords";
40 return LDAP_STRONG_AUTH_REQUIRED;
43 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
44 be = conn->c_authz_backend;
45 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
47 if( be && !be->be_extended ) {
48 *text = "operation not supported for current user";
49 return LDAP_UNWILLING_TO_PERFORM;
53 rc = backend_check_restrictions( be, conn, op,
54 (struct berval *)&slap_EXOP_MODIFY_PASSWD, text );
57 if( rc != LDAP_SUCCESS ) {
62 #ifdef HAVE_CYRUS_SASL
63 rc = slap_sasl_setpass( conn, op,
65 rspoid, rspdata, rspctrls,
68 *text = "no authz backend";
72 #ifndef SLAPD_MULTIMASTER
73 /* This does not apply to multi-master case */
74 } else if( be->be_update_ndn.bv_len ) {
75 /* we SHOULD return a referral in this case */
76 *refs = referral_rewrite( be->be_update_refs,
77 NULL, NULL, LDAP_SCOPE_DEFAULT );
79 #endif /* !SLAPD_MULTIMASTER */
85 rspoid, rspdata, rspctrls,
92 int slap_passwd_parse( struct berval *reqdata,
94 struct berval *oldpass,
95 struct berval *newpass,
98 int rc = LDAP_SUCCESS;
101 char berbuf[LBER_ELEMENT_SIZEOF];
102 BerElement *ber = (BerElement *)berbuf;
104 if( reqdata == NULL ) {
108 if( reqdata->bv_len == 0 ) {
109 *text = "empty request data field";
110 return LDAP_PROTOCOL_ERROR;
113 /* ber_init2 uses reqdata directly, doesn't allocate new buffers */
114 ber_init2( ber, reqdata, 0 );
116 tag = ber_scanf( ber, "{" /*}*/ );
118 if( tag != LBER_ERROR ) {
119 tag = ber_peek_tag( ber, &len );
122 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_ID ) {
125 LDAP_LOG( OPERATION, ERR,
126 "slap_passwd_parse: ID not allowed.\n", 0, 0, 0 );
128 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID not allowed.\n",
132 *text = "user must change own password";
133 rc = LDAP_UNWILLING_TO_PERFORM;
137 tag = ber_scanf( ber, "m", id );
139 if( tag == LBER_ERROR ) {
141 LDAP_LOG( OPERATION, ERR,
142 "slap_passwd_parse: ID parse failed.\n", 0, 0, 0 );
144 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
151 tag = ber_peek_tag( ber, &len);
154 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_OLD ) {
155 if( oldpass == NULL ) {
157 LDAP_LOG( OPERATION, ERR,
158 "slap_passwd_parse: OLD not allowed.\n" , 0, 0, 0 );
160 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD not allowed.\n",
164 *text = "use bind to verify old password";
165 rc = LDAP_UNWILLING_TO_PERFORM;
169 tag = ber_scanf( ber, "m", oldpass );
171 if( tag == LBER_ERROR ) {
173 LDAP_LOG( OPERATION, ERR,
174 "slap_passwd_parse: ID parse failed.\n" , 0, 0, 0 );
176 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: ID parse failed.\n",
183 tag = ber_peek_tag( ber, &len );
186 if( tag == LDAP_TAG_EXOP_MODIFY_PASSWD_NEW ) {
187 if( newpass == NULL ) {
189 LDAP_LOG( OPERATION, ERR,
190 "slap_passwd_parse: NEW not allowed.\n", 0, 0, 0 );
192 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: NEW not allowed.\n",
196 *text = "user specified passwords disallowed";
197 rc = LDAP_UNWILLING_TO_PERFORM;
201 tag = ber_scanf( ber, "m", newpass );
203 if( tag == LBER_ERROR ) {
205 LDAP_LOG( OPERATION, ERR,
206 "slap_passwd_parse: OLD parse failed.\n", 0, 0, 0 );
208 Debug( LDAP_DEBUG_TRACE, "slap_passwd_parse: OLD parse failed.\n",
215 tag = ber_peek_tag( ber, &len );
221 LDAP_LOG( OPERATION, ERR,
222 "slap_passwd_parse: decoding error, len=%ld\n", (long)len, 0, 0 );
224 Debug( LDAP_DEBUG_TRACE,
225 "slap_passwd_parse: decoding error, len=%ld\n",
229 *text = "data decoding error";
230 rc = LDAP_PROTOCOL_ERROR;
237 struct berval * slap_passwd_return(
238 struct berval *cred )
241 struct berval *bv = NULL;
242 char berbuf[LBER_ELEMENT_SIZEOF];
243 /* opaque structure, size unknown but smaller than berbuf */
244 BerElement *ber = (BerElement *)berbuf;
246 assert( cred != NULL );
249 LDAP_LOG( OPERATION, ENTRY,
250 "slap_passwd_return: %ld\n",(long)cred->bv_len, 0, 0 );
252 Debug( LDAP_DEBUG_TRACE, "slap_passwd_return: %ld\n",
253 (long) cred->bv_len, 0, 0 );
256 ber_init_w_nullc( ber, LBER_USE_DER );
258 rc = ber_printf( ber, "{tON}",
259 LDAP_TAG_EXOP_MODIFY_PASSWD_GEN, cred );
262 (void) ber_flatten( ber, &bv );
274 struct berval *cred )
279 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
280 ldap_pvt_thread_mutex_lock( &passwd_mutex );
282 lutil_passwd_sasl_conn = conn->c_sasl_context;
286 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
287 if( !lutil_passwd( bv, cred, NULL ) ) {
293 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
295 lutil_passwd_sasl_conn = NULL;
297 ldap_pvt_thread_mutex_unlock( &passwd_mutex );
304 slap_passwd_generate( struct berval *pass )
308 LDAP_LOG( OPERATION, ENTRY, "slap_passwd_generate: begin\n", 0, 0, 0 );
310 Debug( LDAP_DEBUG_TRACE, "slap_passwd_generate\n", 0, 0, 0 );
313 * generate passwords of only 8 characters as some getpass(3)
314 * implementations truncate at 8 characters.
316 tmp = lutil_passwd_generate( 8 );
328 struct berval * cred,
329 struct berval * new )
332 #ifdef LUTIL_SHA1_BYTES
333 char* hash = default_passwd_hash ? default_passwd_hash : "{SSHA}";
335 char* hash = default_passwd_hash ? default_passwd_hash : "{SMD5}";
339 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
340 ldap_pvt_thread_mutex_lock( &passwd_mutex );
343 tmp = lutil_passwd_hash( cred , hash );
345 #if defined( SLAPD_CRYPT ) || defined( SLAPD_SPASSWD )
346 ldap_pvt_thread_mutex_unlock( &passwd_mutex );