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_cdn != NULL ) {
67 if ( conn->c_dn != NULL ) {
72 conn->c_authc_backend = NULL;
73 conn->c_authz_backend = NULL;
75 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
77 if ( op->o_dn != NULL ) {
79 op->o_dn = ch_strdup( "" );
82 if ( op->o_ndn != NULL ) {
84 op->o_ndn = ch_strdup( "" );
88 * Parse the bind request. It looks like this:
90 * BindRequest ::= SEQUENCE {
91 * version INTEGER, -- version
92 * name DistinguishedName, -- dn
93 * authentication CHOICE {
94 * simple [0] OCTET STRING -- passwd
95 * krbv42ldap [1] OCTET STRING
96 * krbv42dsa [2] OCTET STRING
97 * SASL [3] SaslCredentials
101 * SaslCredentials ::= SEQUENCE {
102 * mechanism LDAPString,
103 * credentials OCTET STRING OPTIONAL
107 tag = ber_scanf( ber, "{iat" /*}*/, &version, &dn, &method );
109 if ( tag == LBER_ERROR ) {
110 Debug( LDAP_DEBUG_ANY, "bind: ber_scanf failed\n", 0, 0, 0 );
111 send_ldap_disconnect( conn, op,
112 LDAP_PROTOCOL_ERROR, "decoding error" );
117 op->o_protocol = version;
119 if( method != LDAP_AUTH_SASL ) {
120 tag = ber_scanf( ber, /*{*/ "o}", &cred );
123 tag = ber_scanf( ber, "{a" /*}*/, &mech );
125 if ( tag != LBER_ERROR ) {
127 tag = ber_peek_tag( ber, &len );
129 if ( tag == LDAP_TAG_LDAPCRED ) {
130 tag = ber_scanf( ber, "o", &cred );
133 if ( tag != LBER_ERROR ) {
134 tag = ber_scanf( ber, /*{{*/ "}}" );
139 if ( tag == LBER_ERROR ) {
140 send_ldap_disconnect( conn, op,
143 rc = SLAPD_DISCONNECT;
147 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
148 Debug( LDAP_DEBUG_ANY, "do_bind: get_ctrls failed\n", 0, 0, 0 );
152 ndn = ch_strdup( dn );
154 if ( dn_normalize( ndn ) == NULL ) {
155 Debug( LDAP_DEBUG_ANY, "bind: invalid dn (%s)\n", dn, 0, 0 );
156 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
157 "invalid DN", NULL, NULL );
161 if( method == LDAP_AUTH_SASL ) {
162 Debug( LDAP_DEBUG_TRACE, "do_sasl_bind: dn (%s) mech %s\n",
165 Debug( LDAP_DEBUG_TRACE, "do_bind: version=%ld dn=\"%s\" method=%ld\n",
166 (unsigned long) version, dn, (unsigned long) method );
169 Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d BIND dn=\"%s\" method=%ld\n",
170 op->o_connid, op->o_opid, ndn, (unsigned long) method, 0 );
172 if ( version < LDAP_VERSION_MIN || version > LDAP_VERSION_MAX ) {
173 Debug( LDAP_DEBUG_ANY, "do_bind: unknown version=%ld\n",
174 (unsigned long) version, 0, 0 );
175 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
176 NULL, "requested protocol version not supported", NULL, NULL );
180 /* we set connection version regardless of whether bind succeeds
183 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
184 conn->c_protocol = version;
185 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
187 if ( method == LDAP_AUTH_SASL ) {
189 unsigned long ssf = 0;
191 if ( version < LDAP_VERSION3 ) {
192 Debug( LDAP_DEBUG_ANY, "do_bind: sasl with LDAPv%ld\n",
193 (unsigned long) version, 0, 0 );
194 send_ldap_disconnect( conn, op,
195 LDAP_PROTOCOL_ERROR, "SASL bind requires LDAPv3" );
196 rc = SLAPD_DISCONNECT;
200 if( mech == NULL || *mech == '\0' ) {
201 Debug( LDAP_DEBUG_ANY,
202 "do_bind: no sasl mechanism provided\n",
204 send_ldap_result( conn, op, rc = LDAP_AUTH_METHOD_NOT_SUPPORTED,
205 NULL, "no SASL mechanism provided", NULL, NULL );
209 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
211 if ( conn->c_sasl_bind_mech != NULL ) {
212 /* SASL bind is in progress */
215 if((strcmp(conn->c_sasl_bind_mech, mech) != 0)) {
216 /* mechanism changed */
217 slap_sasl_reset(conn);
220 free( conn->c_sasl_bind_mech );
221 conn->c_sasl_bind_mech = NULL;
225 /* SASL bind is NOT in progress */
227 assert( conn->c_sasl_bind_mech == NULL );
231 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
234 rc = slap_sasl_bind( conn, op, dn, ndn, saslmech, &cred,
237 if( rc == LDAP_SUCCESS ) {
238 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
240 conn->c_authmech = mech;
241 if( ssf ) conn->c_sasl_layers++;
242 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
244 } else if ( rc == LDAP_SASL_BIND_IN_PROGRESS ) {
245 conn->c_sasl_bind_mech = mech;
253 /* Not SASL, cancel any in-progress bind */
254 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
256 if ( conn->c_sasl_bind_mech != NULL ) {
257 assert( conn->c_sasl_bind_in_progress );
259 free(conn->c_sasl_bind_mech);
260 conn->c_sasl_bind_mech = NULL;
263 assert( !conn->c_sasl_bind_in_progress );
266 slap_sasl_reset( conn );
267 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
270 /* accept "anonymous" binds */
271 if ( cred.bv_len == 0 || ndn == NULL || *ndn == '\0' ) {
273 * we already forced connection to "anonymous",
274 * just need to send success
276 send_ldap_result( conn, op, LDAP_SUCCESS,
277 NULL, NULL, NULL, NULL );
278 Debug( LDAP_DEBUG_TRACE, "do_bind: v%d anonymous bind\n",
284 * We could be serving multiple database backends. Select the
285 * appropriate one, or send a referral to our "referral server"
286 * if we don't hold it.
289 if ( (be = select_backend( ndn )) == NULL ) {
290 if ( default_referral ) {
291 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
292 NULL, NULL, default_referral, NULL );
295 /* noSuchObject is not allowed to be returned by bind */
296 send_ldap_result( conn, op, rc = LDAP_INVALID_CREDENTIALS,
297 NULL, NULL, NULL, NULL );
303 conn->c_authz_backend = be;
305 /* make sure this backend recongizes critical controls */
306 rc = backend_check_controls( be, conn, op, &text ) ;
308 if( rc != LDAP_SUCCESS ) {
309 send_ldap_result( conn, op, rc,
310 NULL, text, NULL, NULL );
319 /* deref suffix alias if appropriate */
320 ndn = suffix_alias( be, ndn );
322 ret = (*be->be_bind)( be, conn, op, dn, ndn,
323 method, &cred, &edn );
326 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
338 Debug( LDAP_DEBUG_TRACE, "do_bind: v%d bind: \"%s\" to \"%s\"\n",
339 version, conn->c_cdn, conn->c_dn );
341 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
343 /* send this here to avoid a race condition */
344 send_ldap_result( conn, op, LDAP_SUCCESS,
345 NULL, NULL, NULL, NULL );
347 } else if (edn != NULL) {
352 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
353 NULL, "operation not supported within namingContext", NULL, NULL );
357 if( rc != LDAP_SASL_BIND_IN_PROGRESS ) {
358 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
360 /* dispose of mech */
361 free( conn->c_sasl_bind_mech );
362 conn->c_sasl_bind_mech = NULL;
364 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
373 if ( mech != NULL ) {
376 if ( cred.bv_val != NULL ) {