1 /* bind.c - decode an ldap bind operation and pass it to a backend db */
4 * Copyright 1998-1999 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;
43 int rc = LDAP_SUCCESS;
47 Debug( LDAP_DEBUG_TRACE, "do_bind\n", 0, 0, 0 );
54 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
56 /* Force to connection to "anonymous" until bind succeeds.
57 * This may need to be relocated or done on a case by case basis
58 * to handle certain SASL mechanisms.
61 if ( conn->c_cdn != NULL ) {
66 if ( conn->c_dn != NULL ) {
71 conn->c_authz_backend = NULL;
73 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
75 if ( op->o_dn != NULL ) {
77 op->o_dn = ch_strdup( "" );
80 if ( op->o_ndn != NULL ) {
82 op->o_ndn = ch_strdup( "" );
86 * Parse the bind request. It looks like this:
88 * BindRequest ::= SEQUENCE {
89 * version INTEGER, -- version
90 * name DistinguishedName, -- dn
91 * authentication CHOICE {
92 * simple [0] OCTET STRING -- passwd
93 * krbv42ldap [1] OCTET STRING
94 * krbv42dsa [2] OCTET STRING
95 * SASL [3] SaslCredentials
99 * SaslCredentials ::= SEQUENCE {
100 * mechanism LDAPString,
101 * credentials OCTET STRING OPTIONAL
105 tag = ber_scanf( ber, "{iat" /*}*/, &version, &dn, &method );
107 if ( tag == LBER_ERROR ) {
108 Debug( LDAP_DEBUG_ANY, "bind: ber_scanf failed\n", 0, 0, 0 );
109 send_ldap_disconnect( conn, op,
110 LDAP_PROTOCOL_ERROR, "decoding error" );
115 ndn = ch_strdup( dn );
117 if ( dn_normalize( ndn ) == NULL ) {
118 Debug( LDAP_DEBUG_ANY, "bind: invalid dn (%s)\n", dn, 0, 0 );
119 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
120 "invalid DN", NULL, NULL );
124 op->o_protocol = version;
126 if( method != LDAP_AUTH_SASL ) {
127 tag = ber_scanf( ber, /*{*/ "o}", &cred );
130 tag = ber_scanf( ber, "{a" /*}*/, &mech );
132 if ( tag != LBER_ERROR ) {
134 tag = ber_peek_tag( ber, &len );
136 if ( tag == LDAP_TAG_LDAPCRED ) {
137 tag = ber_scanf( ber, "o", &cred );
140 if ( tag != LBER_ERROR ) {
141 tag = ber_scanf( ber, /*{{*/ "}}" );
146 if ( tag == LBER_ERROR ) {
147 send_ldap_disconnect( conn, op,
154 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
155 Debug( LDAP_DEBUG_ANY, "do_bind: get_ctrls failed\n", 0, 0, 0 );
159 if( method == LDAP_AUTH_SASL ) {
160 Debug( LDAP_DEBUG_TRACE, "do_sasl_bind: dn (%s) mech %s\n",
163 Debug( LDAP_DEBUG_TRACE, "do_bind: version=%ld dn=\"%s\" method=%ld\n",
164 (unsigned long) version, dn, (unsigned long) method );
167 Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d BIND dn=\"%s\" method=%ld\n",
168 op->o_connid, op->o_opid, ndn, (unsigned long) method, 0 );
170 if ( version < LDAP_VERSION_MIN || version > LDAP_VERSION_MAX ) {
171 Debug( LDAP_DEBUG_ANY, "do_bind: unknown version=%ld\n",
172 (unsigned long) version, 0, 0 );
173 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
174 NULL, "version not supported", NULL, NULL );
178 if ( method == LDAP_AUTH_SASL ) {
179 if ( version < LDAP_VERSION3 ) {
180 Debug( LDAP_DEBUG_ANY, "do_bind: sasl with LDAPv%ld\n",
181 (unsigned long) version, 0, 0 );
182 send_ldap_disconnect( conn, op,
183 LDAP_PROTOCOL_ERROR, "sasl bind requires LDAPv3" );
188 if( mech == NULL || *mech == '\0' ) {
189 Debug( LDAP_DEBUG_ANY,
190 "do_bind: no sasl mechanism provided\n",
192 send_ldap_result( conn, op, rc = LDAP_AUTH_METHOD_NOT_SUPPORTED,
193 NULL, "no sasl mechanism provided", NULL, NULL );
197 if( !charray_inlist( supportedSASLMechanisms, mech ) ) {
198 Debug( LDAP_DEBUG_ANY,
199 "do_bind: sasl mechanism=\"%s\" not supported.\n",
201 send_ldap_result( conn, op, rc = LDAP_AUTH_METHOD_NOT_SUPPORTED,
202 NULL, "sasl mechanism not supported", NULL, NULL );
206 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
208 if ( conn->c_authmech != NULL ) {
209 assert( conn->c_bind_in_progress );
211 if((strcmp(conn->c_authmech, mech) != 0)) {
212 /* mechanism changed, cancel in progress bind */
213 conn->c_bind_in_progress = 0;
214 if( conn->c_authstate != NULL ) {
215 free(conn->c_authstate);
216 conn->c_authstate = NULL;
218 free(conn->c_authmech);
219 conn->c_authmech = NULL;
224 assert( !conn->c_bind_in_progress );
225 assert( conn->c_authmech == NULL );
226 assert( conn->c_authstate == NULL );
230 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
232 if ( conn->c_authmech != NULL ) {
233 assert( conn->c_bind_in_progress );
235 /* cancel in progress bind */
236 conn->c_bind_in_progress = 0;
238 if( conn->c_authstate != NULL ) {
239 free(conn->c_authstate);
240 conn->c_authstate = NULL;
242 free(conn->c_authmech);
243 conn->c_authmech = NULL;
247 conn->c_protocol = version;
248 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
250 /* accept null binds */
251 if ( ndn == NULL || *ndn == '\0' ) {
253 * we already forced connection to "anonymous", we just
254 * need to send success
256 send_ldap_result( conn, op, LDAP_SUCCESS,
257 NULL, NULL, NULL, NULL );
262 * We could be serving multiple database backends. Select the
263 * appropriate one, or send a referral to our "referral server"
264 * if we don't hold it.
267 if ( (be = select_backend( ndn )) == NULL ) {
268 if ( cred.bv_len == 0 ) {
269 send_ldap_result( conn, op, LDAP_SUCCESS,
270 NULL, NULL, NULL, NULL );
272 } else if ( default_referral ) {
273 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
274 NULL, NULL, default_referral, NULL );
277 /* noSuchObject is not allowed to be returned by bind */
278 send_ldap_result( conn, op, rc = LDAP_INVALID_CREDENTIALS,
279 NULL, NULL, NULL, NULL );
285 conn->c_authz_backend = be;
287 /* make sure this backend recongizes critical controls */
288 rc = backend_check_controls( be, conn, op ) ;
290 if( rc != LDAP_SUCCESS ) {
291 send_ldap_result( conn, op, rc,
292 NULL, NULL, NULL, NULL );
300 /* deref suffix alias if appropriate */
301 ndn = suffix_alias( be, ndn );
303 ret = (*be->be_bind)( be, conn, op, dn, ndn,
304 method, mech, &cred, &edn );
307 #ifdef HAVE_CYRUS_SASL
311 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
323 Debug( LDAP_DEBUG_TRACE, "do_bind: bound \"%s\" to \"%s\"\n",
324 conn->c_cdn, conn->c_dn, method );
326 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
328 /* send this here to avoid a race condition */
329 send_ldap_result( conn, op, LDAP_SUCCESS,
330 NULL, NULL, NULL, NULL );
332 #ifdef HAVE_CYRUS_SASL
333 if ( conn->c_sasl_context != NULL &&
334 sasl_getprop( conn->c_sasl_context, SASL_SSF, (void **)&ssf )
335 == SASL_OK && ssf ) {
336 /* Enable encode/decode */
337 ldap_pvt_sasl_install( conn->c_sb, conn->c_sasl_context );
340 } else if (edn != NULL) {
345 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
346 NULL, "Function not implemented", NULL, NULL );
356 if ( mech != NULL ) {
359 if ( cred.bv_val != NULL ) {