1 /* bind.c - decode an ldap bind operation and pass it to a backend db */
4 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
9 * Copyright (c) 1995 Regents of the University of Michigan.
10 * All rights reserved.
12 * Redistribution and use in source and binary forms are permitted
13 * provided that this notice is preserved and that due credit is given
14 * to the University of Michigan at Ann Arbor. The name of the University
15 * may not be used to endorse or promote products derived from this
16 * software without specific prior written permission. This software
17 * is provided ``as is'' without express or implied warranty.
24 #include <ac/string.h>
25 #include <ac/socket.h>
36 BerElement *ber = op->o_ber;
44 int rc = LDAP_SUCCESS;
49 Debug( LDAP_DEBUG_TRACE, "do_bind\n", 0, 0, 0 );
56 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
59 * Force to connection to "anonymous" until bind succeeds.
62 if ( conn->c_authmech != NULL ) {
63 free( conn->c_authmech );
64 conn->c_authmech = NULL;
67 if ( conn->c_cdn != NULL ) {
72 if ( conn->c_dn != NULL ) {
77 conn->c_authc_backend = NULL;
78 conn->c_authz_backend = NULL;
80 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
82 if ( op->o_dn != NULL ) {
84 op->o_dn = ch_strdup( "" );
87 if ( op->o_ndn != NULL ) {
89 op->o_ndn = ch_strdup( "" );
93 * Parse the bind request. It looks like this:
95 * BindRequest ::= SEQUENCE {
96 * version INTEGER, -- version
97 * name DistinguishedName, -- dn
98 * authentication CHOICE {
99 * simple [0] OCTET STRING -- passwd
100 * krbv42ldap [1] OCTET STRING
101 * krbv42dsa [2] OCTET STRING
102 * SASL [3] SaslCredentials
106 * SaslCredentials ::= SEQUENCE {
107 * mechanism LDAPString,
108 * credentials OCTET STRING OPTIONAL
112 tag = ber_scanf( ber, "{iat" /*}*/, &version, &dn, &method );
114 if ( tag == LBER_ERROR ) {
115 Debug( LDAP_DEBUG_ANY, "bind: ber_scanf failed\n", 0, 0, 0 );
116 send_ldap_disconnect( conn, op,
117 LDAP_PROTOCOL_ERROR, "decoding error" );
122 op->o_protocol = version;
124 if( method != LDAP_AUTH_SASL ) {
125 tag = ber_scanf( ber, /*{*/ "o}", &cred );
128 tag = ber_scanf( ber, "{a" /*}*/, &mech );
130 if ( tag != LBER_ERROR ) {
132 tag = ber_peek_tag( ber, &len );
134 if ( tag == LDAP_TAG_LDAPCRED ) {
135 tag = ber_scanf( ber, "o", &cred );
138 if ( tag != LBER_ERROR ) {
139 tag = ber_scanf( ber, /*{{*/ "}}" );
144 if ( tag == LBER_ERROR ) {
145 send_ldap_disconnect( conn, op,
148 rc = SLAPD_DISCONNECT;
152 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
153 Debug( LDAP_DEBUG_ANY, "do_bind: get_ctrls failed\n", 0, 0, 0 );
157 ndn = ch_strdup( dn );
159 if ( dn_normalize( ndn ) == NULL ) {
160 Debug( LDAP_DEBUG_ANY, "bind: invalid dn (%s)\n", dn, 0, 0 );
161 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
162 "invalid DN", NULL, NULL );
166 if( method == LDAP_AUTH_SASL ) {
167 Debug( LDAP_DEBUG_TRACE, "do_sasl_bind: dn (%s) mech %s\n",
170 Debug( LDAP_DEBUG_TRACE, "do_bind: version=%ld dn=\"%s\" method=%ld\n",
171 (unsigned long) version, dn, (unsigned long) method );
174 Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d BIND dn=\"%s\" method=%ld\n",
175 op->o_connid, op->o_opid, ndn, (unsigned long) method, 0 );
177 if ( version < LDAP_VERSION_MIN || version > LDAP_VERSION_MAX ) {
178 Debug( LDAP_DEBUG_ANY, "do_bind: unknown version=%ld\n",
179 (unsigned long) version, 0, 0 );
180 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
181 NULL, "requested protocol version not supported", NULL, NULL );
184 } else if (( global_disallows & SLAP_DISALLOW_BIND_V2 ) &&
185 version < LDAP_VERSION3 )
187 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
188 NULL, "requested protocol version not allowed", NULL, NULL );
192 /* we set connection version regardless of whether bind succeeds
195 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
196 conn->c_protocol = version;
197 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
199 if ( method == LDAP_AUTH_SASL ) {
203 if ( version < LDAP_VERSION3 ) {
204 Debug( LDAP_DEBUG_ANY, "do_bind: sasl with LDAPv%ld\n",
205 (unsigned long) version, 0, 0 );
206 send_ldap_disconnect( conn, op,
207 LDAP_PROTOCOL_ERROR, "SASL bind requires LDAPv3" );
208 rc = SLAPD_DISCONNECT;
212 if( mech == NULL || *mech == '\0' ) {
213 Debug( LDAP_DEBUG_ANY,
214 "do_bind: no sasl mechanism provided\n",
216 send_ldap_result( conn, op, rc = LDAP_AUTH_METHOD_NOT_SUPPORTED,
217 NULL, "no SASL mechanism provided", NULL, NULL );
221 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
223 if ( conn->c_sasl_bind_mech != NULL ) {
224 /* SASL bind is in progress */
227 if((strcmp(conn->c_sasl_bind_mech, mech) != 0)) {
228 /* mechanism changed */
229 slap_sasl_reset(conn);
232 free( conn->c_sasl_bind_mech );
233 conn->c_sasl_bind_mech = NULL;
237 /* SASL bind is NOT in progress */
239 assert( conn->c_sasl_bind_mech == NULL );
243 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
246 rc = slap_sasl_bind( conn, op, dn, ndn, saslmech, &cred,
249 if( rc == LDAP_SUCCESS ) {
250 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
252 conn->c_authmech = mech;
253 if( ssf ) conn->c_sasl_layers++;
254 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
256 } else if ( rc == LDAP_SASL_BIND_IN_PROGRESS ) {
257 conn->c_sasl_bind_mech = mech;
265 /* Not SASL, cancel any in-progress bind */
266 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
268 if ( conn->c_sasl_bind_mech != NULL ) {
269 assert( conn->c_sasl_bind_in_progress );
271 free(conn->c_sasl_bind_mech);
272 conn->c_sasl_bind_mech = NULL;
275 assert( !conn->c_sasl_bind_in_progress );
278 slap_sasl_reset( conn );
279 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
282 /* accept "anonymous" binds */
283 if ( cred.bv_len == 0 || ndn == NULL || *ndn == '\0' ) {
288 ( global_disallows & SLAP_DISALLOW_BIND_ANON_CRED ))
290 /* cred is not empty, disallow */
291 rc = LDAP_INVALID_CREDENTIALS;
293 } else if ( ndn != NULL && *ndn != '\0' &&
294 ( global_disallows & SLAP_DISALLOW_BIND_ANON_DN ))
296 /* DN is not empty, disallow */
297 rc = LDAP_UNWILLING_TO_PERFORM;
298 text = "unwilling to allow anonymous bind with non-empty DN";
300 } else if ( global_disallows & SLAP_DISALLOW_BIND_ANON ) {
302 rc = LDAP_UNWILLING_TO_PERFORM;
303 text = "anonymous bind disallowed";
307 * we already forced connection to "anonymous",
308 * just need to send success
310 send_ldap_result( conn, op, rc,
311 NULL, text, NULL, NULL );
312 Debug( LDAP_DEBUG_TRACE, "do_bind: v%d anonymous bind\n",
318 * We could be serving multiple database backends. Select the
319 * appropriate one, or send a referral to our "referral server"
320 * if we don't hold it.
323 if ( (be = select_backend( ndn )) == NULL ) {
324 if ( default_referral ) {
325 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
326 NULL, NULL, default_referral, NULL );
329 /* noSuchObject is not allowed to be returned by bind */
330 send_ldap_result( conn, op, rc = LDAP_INVALID_CREDENTIALS,
331 NULL, NULL, NULL, NULL );
337 conn->c_authz_backend = be;
339 /* check restrictions */
340 rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
341 if( rc != LDAP_SUCCESS ) {
342 send_ldap_result( conn, op, rc,
343 NULL, text, NULL, NULL );
352 /* deref suffix alias if appropriate */
353 ndn = suffix_alias( be, ndn );
355 ret = (*be->be_bind)( be, conn, op, dn, ndn,
356 method, &cred, &edn );
359 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
371 Debug( LDAP_DEBUG_TRACE, "do_bind: v%d bind: \"%s\" to \"%s\"\n",
372 version, conn->c_cdn, conn->c_dn );
374 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
376 /* send this here to avoid a race condition */
377 send_ldap_result( conn, op, LDAP_SUCCESS,
378 NULL, NULL, NULL, NULL );
380 } else if (edn != NULL) {
385 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
386 NULL, "operation not supported within namingContext", NULL, NULL );
390 if( rc != LDAP_SASL_BIND_IN_PROGRESS ) {
391 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
393 /* dispose of mech */
394 free( conn->c_sasl_bind_mech );
395 conn->c_sasl_bind_mech = NULL;
397 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
406 if ( mech != NULL ) {
409 if ( cred.bv_val != NULL ) {