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;
48 Debug( LDAP_DEBUG_TRACE, "do_bind\n", 0, 0, 0 );
55 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
58 * Force to connection to "anonymous" until bind succeeds.
61 if ( conn->c_cdn != NULL ) {
66 if ( conn->c_dn != NULL ) {
71 conn->c_authc_backend = NULL;
72 conn->c_authz_backend = NULL;
74 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
76 if ( op->o_dn != NULL ) {
78 op->o_dn = ch_strdup( "" );
81 if ( op->o_ndn != NULL ) {
83 op->o_ndn = ch_strdup( "" );
87 * Parse the bind request. It looks like this:
89 * BindRequest ::= SEQUENCE {
90 * version INTEGER, -- version
91 * name DistinguishedName, -- dn
92 * authentication CHOICE {
93 * simple [0] OCTET STRING -- passwd
94 * krbv42ldap [1] OCTET STRING
95 * krbv42dsa [2] OCTET STRING
96 * SASL [3] SaslCredentials
100 * SaslCredentials ::= SEQUENCE {
101 * mechanism LDAPString,
102 * credentials OCTET STRING OPTIONAL
106 tag = ber_scanf( ber, "{iat" /*}*/, &version, &dn, &method );
108 if ( tag == LBER_ERROR ) {
109 Debug( LDAP_DEBUG_ANY, "bind: ber_scanf failed\n", 0, 0, 0 );
110 send_ldap_disconnect( conn, op,
111 LDAP_PROTOCOL_ERROR, "decoding error" );
116 op->o_protocol = version;
118 if( method != LDAP_AUTH_SASL ) {
119 tag = ber_scanf( ber, /*{*/ "o}", &cred );
122 tag = ber_scanf( ber, "{a" /*}*/, &mech );
124 if ( tag != LBER_ERROR ) {
126 tag = ber_peek_tag( ber, &len );
128 if ( tag == LDAP_TAG_LDAPCRED ) {
129 tag = ber_scanf( ber, "o", &cred );
132 if ( tag != LBER_ERROR ) {
133 tag = ber_scanf( ber, /*{{*/ "}}" );
138 if ( tag == LBER_ERROR ) {
139 send_ldap_disconnect( conn, op,
142 rc = SLAPD_DISCONNECT;
146 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
147 Debug( LDAP_DEBUG_ANY, "do_bind: get_ctrls failed\n", 0, 0, 0 );
151 ndn = ch_strdup( dn );
153 if ( dn_normalize( ndn ) == NULL ) {
154 Debug( LDAP_DEBUG_ANY, "bind: invalid dn (%s)\n", dn, 0, 0 );
155 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
156 "invalid DN", NULL, NULL );
160 if( method == LDAP_AUTH_SASL ) {
161 Debug( LDAP_DEBUG_TRACE, "do_sasl_bind: dn (%s) mech %s\n",
164 Debug( LDAP_DEBUG_TRACE, "do_bind: version=%ld dn=\"%s\" method=%ld\n",
165 (unsigned long) version, dn, (unsigned long) method );
168 Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d BIND dn=\"%s\" method=%ld\n",
169 op->o_connid, op->o_opid, ndn, (unsigned long) method, 0 );
171 if ( version < LDAP_VERSION_MIN || version > LDAP_VERSION_MAX ) {
172 Debug( LDAP_DEBUG_ANY, "do_bind: unknown version=%ld\n",
173 (unsigned long) version, 0, 0 );
174 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
175 NULL, "requested protocol version not supported", NULL, NULL );
179 /* we set connection version regardless of whether bind succeeds
182 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
183 conn->c_protocol = version;
184 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
186 if ( method == LDAP_AUTH_SASL ) {
187 if ( version < LDAP_VERSION3 ) {
188 Debug( LDAP_DEBUG_ANY, "do_bind: sasl with LDAPv%ld\n",
189 (unsigned long) version, 0, 0 );
190 send_ldap_disconnect( conn, op,
191 LDAP_PROTOCOL_ERROR, "SASL bind requires LDAPv3" );
192 rc = SLAPD_DISCONNECT;
196 if( mech == NULL || *mech == '\0' ) {
197 Debug( LDAP_DEBUG_ANY,
198 "do_bind: no sasl mechanism provided\n",
200 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
201 NULL, "no SASL mechanism provided", NULL, NULL );
205 if( !charray_inlist( supportedSASLMechanisms, mech ) ) {
206 Debug( LDAP_DEBUG_ANY,
207 "do_bind: sasl mechanism=\"%s\" not supported.\n",
209 send_ldap_result( conn, op, rc = LDAP_AUTH_METHOD_NOT_SUPPORTED,
210 NULL, "SASL mechanism not supported", NULL, NULL );
214 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
216 if ( conn->c_sasl_bind_mech != NULL ) {
217 assert( conn->c_sasl_bind_in_progress );
219 if((strcmp(conn->c_sasl_bind_mech, mech) != 0)) {
220 /* mechanism changed, cancel in progress bind */
221 conn->c_sasl_bind_in_progress = 0;
222 free( conn->c_sasl_bind_mech );
223 conn->c_sasl_bind_mech = NULL;
224 #ifdef HAVE_CYRUS_SASL
225 sasl_dispose(&conn->c_sasl_bind_context);
226 conn->c_sasl_bind_context = NULL;
232 assert( !conn->c_sasl_bind_in_progress );
233 assert( conn->c_sasl_bind_mech == NULL );
234 #ifdef HAVE_CYRUS_SASL
235 assert( conn->c_sasl_bind_context == NULL );
239 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
242 /* Not SASL, cancel any in-progress bind */
243 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
245 if ( conn->c_sasl_bind_mech != NULL ) {
246 assert( conn->c_sasl_bind_in_progress );
248 /* cancel in progress bind */
249 conn->c_sasl_bind_in_progress = 0;
251 free(conn->c_sasl_bind_mech);
252 conn->c_sasl_bind_mech = NULL;
254 #ifdef HAVE_CYRUS_SASL
255 sasl_dispose(&conn->c_sasl_bind_context);
256 conn->c_sasl_bind_context = NULL;
260 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
263 /* accept "anonymous" binds */
264 if ( cred.bv_len == 0 || ndn == NULL || *ndn == '\0' ) {
266 * we already forced connection to "anonymous",
267 * just need to send success
269 send_ldap_result( conn, op, LDAP_SUCCESS,
270 NULL, NULL, NULL, NULL );
271 Debug( LDAP_DEBUG_TRACE, "do_bind: v%d anonymous bind\n",
277 * We could be serving multiple database backends. Select the
278 * appropriate one, or send a referral to our "referral server"
279 * if we don't hold it.
282 if ( (be = select_backend( ndn )) == NULL ) {
283 if ( default_referral ) {
284 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
285 NULL, NULL, default_referral, NULL );
288 /* noSuchObject is not allowed to be returned by bind */
289 send_ldap_result( conn, op, rc = LDAP_INVALID_CREDENTIALS,
290 NULL, NULL, NULL, NULL );
296 conn->c_authz_backend = be;
298 /* make sure this backend recongizes critical controls */
299 rc = backend_check_controls( be, conn, op, &text ) ;
301 if( rc != LDAP_SUCCESS ) {
302 send_ldap_result( conn, op, rc,
303 NULL, text, NULL, NULL );
312 /* deref suffix alias if appropriate */
313 ndn = suffix_alias( be, ndn );
315 ret = (*be->be_bind)( be, conn, op, dn, ndn,
316 method, mech, &cred, &edn );
319 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
331 Debug( LDAP_DEBUG_TRACE, "do_bind: v%d bind: \"%s\" to \"%s\"\n",
332 version, conn->c_cdn, conn->c_dn );
334 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
336 /* send this here to avoid a race condition */
337 send_ldap_result( conn, op, LDAP_SUCCESS,
338 NULL, NULL, NULL, NULL );
340 } else if (edn != NULL) {
345 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
346 NULL, "bind function not implemented", NULL, NULL );
356 if ( mech != NULL ) {
359 if ( cred.bv_val != NULL ) {