1 /* bind.c - decode an ldap bind operation and pass it to a backend db */
3 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
8 * Copyright (c) 1995 Regents of the University of Michigan.
11 * Redistribution and use in source and binary forms are permitted
12 * provided that this notice is preserved and that due credit is given
13 * to the University of Michigan at Ann Arbor. The name of the University
14 * may not be used to endorse or promote products derived from this
15 * software without specific prior written permission. This software
16 * is provided ``as is'' without express or implied warranty.
23 #include <ac/string.h>
24 #include <ac/socket.h>
34 BerElement *ber = op->o_ber;
41 int rc = LDAP_SUCCESS;
45 Debug( LDAP_DEBUG_TRACE, "do_bind\n", 0, 0, 0 );
52 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
54 /* Force to connection to "anonymous" until bind succeeds.
55 * This may need to be relocated or done on a case by case basis
56 * to handle certain SASL mechanisms.
59 if ( conn->c_cdn != NULL ) {
64 if ( conn->c_dn != NULL ) {
69 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
71 if ( op->o_dn != NULL ) {
73 op->o_dn = ch_strdup( "" );
76 if ( op->o_ndn != NULL ) {
78 op->o_ndn = ch_strdup( "" );
82 * Parse the bind request. It looks like this:
84 * BindRequest ::= SEQUENCE {
85 * version INTEGER, -- version
86 * name DistinguishedName, -- dn
87 * authentication CHOICE {
88 * simple [0] OCTET STRING -- passwd
89 * krbv42ldap [1] OCTET STRING
90 * krbv42dsa [2] OCTET STRING
91 * SASL [3] SaslCredentials
95 * SaslCredentials ::= SEQUENCE {
96 * mechanism LDAPString,
97 * credentials OCTET STRING OPTIONAL
101 tag = ber_scanf( ber, "{iat" /*}*/, &version, &dn, &method );
103 if ( tag == LBER_ERROR ) {
104 Debug( LDAP_DEBUG_ANY, "bind: ber_scanf failed\n", 0, 0, 0 );
105 send_ldap_disconnect( conn, op,
106 LDAP_PROTOCOL_ERROR, "decoding error" );
111 ndn = ch_strdup( dn );
113 if ( dn_normalize_case( ndn ) == NULL ) {
114 Debug( LDAP_DEBUG_ANY, "bind: invalid dn (%s)\n", dn, 0, 0 );
115 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
116 "invalid DN", NULL, NULL );
120 op->o_protocol = version;
122 if( method != LDAP_AUTH_SASL ) {
123 tag = ber_scanf( ber, /*{*/ "o}", &cred );
126 tag = ber_scanf( ber, "{a" /*}*/, &mech );
128 if ( tag != LBER_ERROR ) {
130 tag = ber_peek_tag( ber, &len );
132 if ( tag == LDAP_TAG_LDAPCRED ) {
133 tag = ber_scanf( ber, "o", &cred );
136 if ( tag != LBER_ERROR ) {
137 tag = ber_scanf( ber, /*{{*/ "}}" );
142 if ( tag == LBER_ERROR ) {
143 send_ldap_disconnect( conn, op,
150 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
151 Debug( LDAP_DEBUG_ANY, "do_bind: get_ctrls failed\n", 0, 0, 0 );
155 if( method == LDAP_AUTH_SASL ) {
156 Debug( LDAP_DEBUG_TRACE, "do_sasl_bind: dn (%s) mech %s\n",
159 Debug( LDAP_DEBUG_TRACE, "do_bind: version=%ld dn=\"%s\" method=%ld\n",
160 (unsigned long) version, dn, (unsigned long) method );
163 Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d BIND dn=\"%s\" method=%ld\n",
164 op->o_connid, op->o_opid, ndn, (unsigned long) method, 0 );
166 if ( version < LDAP_VERSION_MIN || version > LDAP_VERSION_MAX ) {
167 Debug( LDAP_DEBUG_ANY, "do_bind: unknown version=%ld\n",
168 (unsigned long) version, 0, 0 );
169 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
170 NULL, "version not supported", NULL, NULL );
174 if ( method == LDAP_AUTH_SASL ) {
175 if ( version < LDAP_VERSION3 ) {
176 Debug( LDAP_DEBUG_ANY, "do_bind: sasl with LDAPv%ld\n",
177 (unsigned long) version, 0, 0 );
178 send_ldap_disconnect( conn, op,
179 LDAP_PROTOCOL_ERROR, "sasl bind requires LDAPv3" );
184 if( mech == NULL || *mech == '\0' ) {
185 Debug( LDAP_DEBUG_ANY,
186 "do_bind: no sasl mechanism provided\n",
188 send_ldap_result( conn, op, rc = LDAP_AUTH_METHOD_NOT_SUPPORTED,
189 NULL, "no sasl mechanism provided", NULL, NULL );
193 if( !charray_inlist( supportedSASLMechanisms, mech ) ) {
194 Debug( LDAP_DEBUG_ANY,
195 "do_bind: sasl mechanism=\"%s\" not supported.\n",
197 send_ldap_result( conn, op, rc = LDAP_AUTH_METHOD_NOT_SUPPORTED,
198 NULL, "sasl mechanism not supported", NULL, NULL );
202 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
204 if ( conn->c_authmech != NULL ) {
205 assert( conn->c_bind_in_progress );
207 if((strcmp(conn->c_authmech, mech) != 0)) {
208 /* mechanism changed, cancel in progress bind */
209 conn->c_bind_in_progress = 0;
210 if( conn->c_authstate != NULL ) {
211 free(conn->c_authstate);
212 conn->c_authstate = NULL;
214 free(conn->c_authmech);
215 conn->c_authmech = NULL;
220 assert( !conn->c_bind_in_progress );
221 assert( conn->c_authmech == NULL );
222 assert( conn->c_authstate == NULL );
227 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
229 if ( conn->c_authmech != NULL ) {
230 assert( conn->c_bind_in_progress );
232 /* cancel in progress bind */
233 conn->c_bind_in_progress = 0;
235 if( conn->c_authstate != NULL ) {
236 free(conn->c_authstate);
237 conn->c_authstate = NULL;
239 free(conn->c_authmech);
240 conn->c_authmech = NULL;
244 conn->c_protocol = version;
245 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
247 /* accept null binds */
248 if ( ndn == NULL || *ndn == '\0' ) {
250 * we already forced connection to "anonymous", we just
251 * need to send success
253 send_ldap_result( conn, op, LDAP_SUCCESS,
254 NULL, NULL, NULL, NULL );
259 * We could be serving multiple database backends. Select the
260 * appropriate one, or send a referral to our "referral server"
261 * if we don't hold it.
264 if ( (be = select_backend( ndn )) == NULL ) {
265 if ( cred.bv_len == 0 ) {
266 send_ldap_result( conn, op, LDAP_SUCCESS,
267 NULL, NULL, NULL, NULL );
269 } else if ( default_referral ) {
270 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
271 NULL, NULL, default_referral, NULL );
274 send_ldap_result( conn, op, rc = LDAP_INVALID_CREDENTIALS,
275 NULL, NULL, NULL, NULL );
285 /* deref suffix alias if appropriate */
286 ndn = suffix_alias( be, ndn );
288 if ( (*be->be_bind)( be, conn, op, ndn, method, mech, &cred, &edn ) == 0 ) {
289 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
301 Debug( LDAP_DEBUG_TRACE, "do_bind: bound \"%s\" to \"%s\"\n",
302 conn->c_cdn, conn->c_dn, method );
304 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
306 /* send this here to avoid a race condition */
307 send_ldap_result( conn, op, LDAP_SUCCESS,
308 NULL, NULL, NULL, NULL );
310 } else if (edn != NULL) {
315 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
316 NULL, "Function not implemented", NULL, NULL );
326 if ( mech != NULL ) {
329 if ( cred.bv_val != NULL ) {