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 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
73 if ( op->o_dn != NULL ) {
75 op->o_dn = ch_strdup( "" );
78 if ( op->o_ndn != NULL ) {
80 op->o_ndn = ch_strdup( "" );
84 * Parse the bind request. It looks like this:
86 * BindRequest ::= SEQUENCE {
87 * version INTEGER, -- version
88 * name DistinguishedName, -- dn
89 * authentication CHOICE {
90 * simple [0] OCTET STRING -- passwd
91 * krbv42ldap [1] OCTET STRING
92 * krbv42dsa [2] OCTET STRING
93 * SASL [3] SaslCredentials
97 * SaslCredentials ::= SEQUENCE {
98 * mechanism LDAPString,
99 * credentials OCTET STRING OPTIONAL
103 tag = ber_scanf( ber, "{iat" /*}*/, &version, &dn, &method );
105 if ( tag == LBER_ERROR ) {
106 Debug( LDAP_DEBUG_ANY, "bind: ber_scanf failed\n", 0, 0, 0 );
107 send_ldap_disconnect( conn, op,
108 LDAP_PROTOCOL_ERROR, "decoding error" );
113 ndn = ch_strdup( dn );
115 if ( dn_normalize( ndn ) == NULL ) {
116 Debug( LDAP_DEBUG_ANY, "bind: invalid dn (%s)\n", dn, 0, 0 );
117 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
118 "invalid DN", NULL, NULL );
122 op->o_protocol = version;
124 if( method != LDAP_AUTH_SASL ) {
125 tag = ber_scanf( ber, /*{*/ "o}", &cred );
128 tag = ber_scanf( ber, "{a" /*}*/, &mech );
130 if ( tag != LBER_ERROR ) {
132 tag = ber_peek_tag( ber, &len );
134 if ( tag == LDAP_TAG_LDAPCRED ) {
135 tag = ber_scanf( ber, "o", &cred );
138 if ( tag != LBER_ERROR ) {
139 tag = ber_scanf( ber, /*{{*/ "}}" );
144 if ( tag == LBER_ERROR ) {
145 send_ldap_disconnect( conn, op,
152 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
153 Debug( LDAP_DEBUG_ANY, "do_bind: get_ctrls failed\n", 0, 0, 0 );
157 if( method == LDAP_AUTH_SASL ) {
158 Debug( LDAP_DEBUG_TRACE, "do_sasl_bind: dn (%s) mech %s\n",
161 Debug( LDAP_DEBUG_TRACE, "do_bind: version=%ld dn=\"%s\" method=%ld\n",
162 (unsigned long) version, dn, (unsigned long) method );
165 Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d BIND dn=\"%s\" method=%ld\n",
166 op->o_connid, op->o_opid, ndn, (unsigned long) method, 0 );
168 if ( version < LDAP_VERSION_MIN || version > LDAP_VERSION_MAX ) {
169 Debug( LDAP_DEBUG_ANY, "do_bind: unknown version=%ld\n",
170 (unsigned long) version, 0, 0 );
171 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
172 NULL, "version not supported", NULL, NULL );
176 if ( method == LDAP_AUTH_SASL ) {
177 if ( version < LDAP_VERSION3 ) {
178 Debug( LDAP_DEBUG_ANY, "do_bind: sasl with LDAPv%ld\n",
179 (unsigned long) version, 0, 0 );
180 send_ldap_disconnect( conn, op,
181 LDAP_PROTOCOL_ERROR, "sasl bind requires LDAPv3" );
186 if( mech == NULL || *mech == '\0' ) {
187 Debug( LDAP_DEBUG_ANY,
188 "do_bind: no sasl mechanism provided\n",
190 send_ldap_result( conn, op, rc = LDAP_AUTH_METHOD_NOT_SUPPORTED,
191 NULL, "no sasl mechanism provided", NULL, NULL );
195 if( !charray_inlist( supportedSASLMechanisms, mech ) ) {
196 Debug( LDAP_DEBUG_ANY,
197 "do_bind: sasl mechanism=\"%s\" not supported.\n",
199 send_ldap_result( conn, op, rc = LDAP_AUTH_METHOD_NOT_SUPPORTED,
200 NULL, "sasl mechanism not supported", NULL, NULL );
204 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
206 if ( conn->c_authmech != NULL ) {
207 assert( conn->c_bind_in_progress );
209 if((strcmp(conn->c_authmech, mech) != 0)) {
210 /* mechanism changed, cancel in progress bind */
211 conn->c_bind_in_progress = 0;
212 if( conn->c_authstate != NULL ) {
213 free(conn->c_authstate);
214 conn->c_authstate = NULL;
216 free(conn->c_authmech);
217 conn->c_authmech = NULL;
222 assert( !conn->c_bind_in_progress );
223 assert( conn->c_authmech == NULL );
224 assert( conn->c_authstate == NULL );
229 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
231 if ( conn->c_authmech != NULL ) {
232 assert( conn->c_bind_in_progress );
234 /* cancel in progress bind */
235 conn->c_bind_in_progress = 0;
237 if( conn->c_authstate != NULL ) {
238 free(conn->c_authstate);
239 conn->c_authstate = NULL;
241 free(conn->c_authmech);
242 conn->c_authmech = NULL;
246 conn->c_protocol = version;
247 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
249 /* accept null binds */
250 if ( ndn == NULL || *ndn == '\0' ) {
252 * we already forced connection to "anonymous", we just
253 * need to send success
255 send_ldap_result( conn, op, LDAP_SUCCESS,
256 NULL, NULL, NULL, NULL );
261 * We could be serving multiple database backends. Select the
262 * appropriate one, or send a referral to our "referral server"
263 * if we don't hold it.
266 if ( (be = select_backend( ndn )) == NULL ) {
267 if ( cred.bv_len == 0 ) {
268 send_ldap_result( conn, op, LDAP_SUCCESS,
269 NULL, NULL, NULL, NULL );
271 } else if ( default_referral ) {
272 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
273 NULL, NULL, default_referral, NULL );
276 /* noSuchObject is not allowed to be returned by bind */
277 send_ldap_result( conn, op, rc = LDAP_INVALID_CREDENTIALS,
278 NULL, NULL, NULL, NULL );
288 /* deref suffix alias if appropriate */
289 ndn = suffix_alias( be, ndn );
291 if ( (*be->be_bind)( be, conn, op, dn, ndn, method, mech, &cred, &edn ) == 0 ) {
292 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
304 Debug( LDAP_DEBUG_TRACE, "do_bind: bound \"%s\" to \"%s\"\n",
305 conn->c_cdn, conn->c_dn, method );
307 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
309 /* send this here to avoid a race condition */
310 send_ldap_result( conn, op, LDAP_SUCCESS,
311 NULL, NULL, NULL, NULL );
313 } else if (edn != NULL) {
318 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
319 NULL, "Function not implemented", NULL, NULL );
329 if ( mech != NULL ) {
332 if ( cred.bv_val != NULL ) {