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 ) {
190 if ( version < LDAP_VERSION3 ) {
191 Debug( LDAP_DEBUG_ANY, "do_bind: sasl with LDAPv%ld\n",
192 (unsigned long) version, 0, 0 );
193 send_ldap_disconnect( conn, op,
194 LDAP_PROTOCOL_ERROR, "SASL bind requires LDAPv3" );
195 rc = SLAPD_DISCONNECT;
199 if( mech == NULL || *mech == '\0' ) {
200 Debug( LDAP_DEBUG_ANY,
201 "do_bind: no sasl mechanism provided\n",
203 send_ldap_result( conn, op, rc = LDAP_AUTH_METHOD_NOT_SUPPORTED,
204 NULL, "no SASL mechanism provided", NULL, NULL );
208 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
210 if ( conn->c_sasl_bind_mech != NULL ) {
211 /* SASL bind is in progress */
214 if((strcmp(conn->c_sasl_bind_mech, mech) != 0)) {
215 /* mechanism changed */
216 slap_sasl_reset(conn);
219 free( conn->c_sasl_bind_mech );
220 conn->c_sasl_bind_mech = NULL;
224 /* SASL bind is NOT in progress */
226 assert( conn->c_sasl_bind_mech == NULL );
230 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
233 rc = slap_sasl_bind( conn, op, dn, ndn, saslmech, &cred, &edn );
235 if( rc == LDAP_SUCCESS ) {
236 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
238 conn->c_authmech = mech;
239 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
241 } else if ( rc == LDAP_SASL_BIND_IN_PROGRESS ) {
242 conn->c_sasl_bind_mech = mech;
250 /* Not SASL, cancel any in-progress bind */
251 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
253 if ( conn->c_sasl_bind_mech != NULL ) {
254 assert( conn->c_sasl_bind_in_progress );
256 free(conn->c_sasl_bind_mech);
257 conn->c_sasl_bind_mech = NULL;
260 assert( !conn->c_sasl_bind_in_progress );
263 slap_sasl_reset( conn );
264 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
267 /* accept "anonymous" binds */
268 if ( cred.bv_len == 0 || ndn == NULL || *ndn == '\0' ) {
270 * we already forced connection to "anonymous",
271 * just need to send success
273 send_ldap_result( conn, op, LDAP_SUCCESS,
274 NULL, NULL, NULL, NULL );
275 Debug( LDAP_DEBUG_TRACE, "do_bind: v%d anonymous bind\n",
281 * We could be serving multiple database backends. Select the
282 * appropriate one, or send a referral to our "referral server"
283 * if we don't hold it.
286 if ( (be = select_backend( ndn )) == NULL ) {
287 if ( default_referral ) {
288 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
289 NULL, NULL, default_referral, NULL );
292 /* noSuchObject is not allowed to be returned by bind */
293 send_ldap_result( conn, op, rc = LDAP_INVALID_CREDENTIALS,
294 NULL, NULL, NULL, NULL );
300 conn->c_authz_backend = be;
302 /* make sure this backend recongizes critical controls */
303 rc = backend_check_controls( be, conn, op, &text ) ;
305 if( rc != LDAP_SUCCESS ) {
306 send_ldap_result( conn, op, rc,
307 NULL, text, NULL, NULL );
316 /* deref suffix alias if appropriate */
317 ndn = suffix_alias( be, ndn );
319 ret = (*be->be_bind)( be, conn, op, dn, ndn,
320 method, &cred, &edn );
323 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
335 Debug( LDAP_DEBUG_TRACE, "do_bind: v%d bind: \"%s\" to \"%s\"\n",
336 version, conn->c_cdn, conn->c_dn );
338 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
340 /* send this here to avoid a race condition */
341 send_ldap_result( conn, op, LDAP_SUCCESS,
342 NULL, NULL, NULL, NULL );
344 } else if (edn != NULL) {
349 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
350 NULL, "operation not supported within namingContext", NULL, NULL );
354 if( rc != LDAP_SASL_BIND_IN_PROGRESS ) {
355 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
357 /* dispose of mech */
358 free( conn->c_sasl_bind_mech );
359 conn->c_sasl_bind_mech = NULL;
361 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
370 if ( mech != NULL ) {
373 if ( cred.bv_val != NULL ) {