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;
40 int rc = LDAP_SUCCESS;
44 Debug( LDAP_DEBUG_TRACE, "do_bind\n", 0, 0, 0 );
51 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
53 /* Force to connection to "anonymous" until bind succeeds.
54 * This may need to be relocated or done on a case by case basis
55 * to handle certain SASL mechanisms.
58 if ( conn->c_cdn != NULL ) {
63 if ( conn->c_dn != NULL ) {
68 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
70 if ( op->o_dn != NULL ) {
72 op->o_dn = ch_strdup( "" );
75 if ( op->o_ndn != NULL ) {
77 op->o_ndn = ch_strdup( "" );
81 * Parse the bind request. It looks like this:
83 * BindRequest ::= SEQUENCE {
84 * version INTEGER, -- version
85 * name DistinguishedName, -- dn
86 * authentication CHOICE {
87 * simple [0] OCTET STRING -- passwd
88 * krbv42ldap [1] OCTET STRING
89 * krbv42dsa [2] OCTET STRING
90 * SASL [3] SaslCredentials
94 * SaslCredentials ::= SEQUENCE {
95 * mechanism LDAPString,
96 * credentials OCTET STRING OPTIONAL
100 tag = ber_scanf( ber, "{iat" /*}*/, &version, &dn, &method );
102 if ( tag == LBER_ERROR ) {
103 Debug( LDAP_DEBUG_ANY, "bind: ber_scanf failed\n", 0, 0, 0 );
104 send_ldap_disconnect( conn, op,
105 LDAP_PROTOCOL_ERROR, "decoding error" );
110 ndn = ch_strdup( dn );
112 if ( dn_normalize_case( ndn ) == NULL ) {
113 Debug( LDAP_DEBUG_ANY, "bind: invalid dn (%s)\n", dn, 0, 0 );
114 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
115 "invalid DN", NULL, NULL );
119 op->o_protocol = version;
121 if( method != LDAP_AUTH_SASL ) {
122 tag = ber_scanf( ber, /*{*/ "o}", &cred );
125 tag = ber_scanf( ber, "{a" /*}*/, &mech );
127 if ( tag != LBER_ERROR ) {
129 tag = ber_peek_tag( ber, &len );
131 if ( tag == LDAP_TAG_LDAPCRED ) {
132 tag = ber_scanf( ber, "o", &cred );
135 if ( tag != LBER_ERROR ) {
136 tag = ber_scanf( ber, /*{{*/ "}}" );
141 if ( tag == LBER_ERROR ) {
142 send_ldap_disconnect( conn, op,
149 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
150 Debug( LDAP_DEBUG_ANY, "do_bind: get_ctrls failed\n", 0, 0, 0 );
154 if( method == LDAP_AUTH_SASL ) {
155 Debug( LDAP_DEBUG_TRACE, "do_sasl_bind: dn (%s) mech %s\n",
158 Debug( LDAP_DEBUG_TRACE, "do_bind: version %d dn (%s) method %d\n",
159 version, dn, method );
162 Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d BIND dn=\"%s\" method=%ld\n",
163 op->o_connid, op->o_opid, ndn, (unsigned long) method, 0 );
165 if ( version < LDAP_VERSION_MIN || version > LDAP_VERSION_MAX ) {
166 Debug( LDAP_DEBUG_ANY, "unknown version %d\n", version, 0, 0 );
167 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
168 NULL, "version not supported", NULL, NULL );
172 if ( method == LDAP_AUTH_SASL ) {
173 if ( version < LDAP_VERSION3 ) {
174 Debug( LDAP_DEBUG_ANY, "do_bind: sasl with LDAPv%d\n",
176 send_ldap_disconnect( conn, op,
177 LDAP_PROTOCOL_ERROR, "sasl bind requires LDAPv3" );
182 if( mech == NULL || *mech == '\0' ) {
183 Debug( LDAP_DEBUG_ANY,
184 "do_bind: no sasl mechanism provided\n",
186 send_ldap_result( conn, op, rc = LDAP_AUTH_METHOD_NOT_SUPPORTED,
187 NULL, "no sasl mechanism provided", NULL, NULL );
191 if( !charray_inlist( supportedSASLMechanisms, mech ) ) {
192 Debug( LDAP_DEBUG_ANY,
193 "do_bind: sasl mechanism \"%s\" not supported.\n",
195 send_ldap_result( conn, op, rc = LDAP_AUTH_METHOD_NOT_SUPPORTED,
196 NULL, "sasl mechanism not supported", NULL, NULL );
200 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
202 if ( conn->c_authmech != NULL ) {
203 assert( conn->c_bind_in_progress );
205 if((strcmp(conn->c_authmech, mech) != 0)) {
206 /* mechanism changed, cancel in progress bind */
207 conn->c_bind_in_progress = 0;
208 if( conn->c_authstate != NULL ) {
209 free(conn->c_authstate);
210 conn->c_authstate = NULL;
212 free(conn->c_authmech);
213 conn->c_authmech = NULL;
218 assert( !conn->c_bind_in_progress );
219 assert( conn->c_authmech == NULL );
220 assert( conn->c_authstate == NULL );
225 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
227 if ( conn->c_authmech != NULL ) {
228 assert( conn->c_bind_in_progress );
230 /* cancel in progress bind */
231 conn->c_bind_in_progress = 0;
233 if( conn->c_authstate != NULL ) {
234 free(conn->c_authstate);
235 conn->c_authstate = NULL;
237 free(conn->c_authmech);
238 conn->c_authmech = NULL;
242 conn->c_protocol = version;
243 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
245 /* accept null binds */
246 if ( ndn == NULL || *ndn == '\0' ) {
248 * we already forced connection to "anonymous", we just
249 * need to send success
251 send_ldap_result( conn, op, LDAP_SUCCESS,
252 NULL, NULL, NULL, NULL );
257 * We could be serving multiple database backends. Select the
258 * appropriate one, or send a referral to our "referral server"
259 * if we don't hold it.
262 if ( (be = select_backend( ndn )) == NULL ) {
263 if ( cred.bv_len == 0 ) {
264 send_ldap_result( conn, op, LDAP_SUCCESS,
265 NULL, NULL, NULL, NULL );
267 } else if ( default_referral ) {
268 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
269 NULL, NULL, default_referral, NULL );
272 send_ldap_result( conn, op, rc = LDAP_INVALID_CREDENTIALS,
273 NULL, NULL, NULL, NULL );
283 /* deref suffix alias if appropriate */
284 ndn = suffix_alias( be, ndn );
286 if ( (*be->be_bind)( be, conn, op, ndn, method, mech, &cred, &edn ) == 0 ) {
287 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
299 Debug( LDAP_DEBUG_TRACE, "do_bind: bound \"%s\" to \"%s\"\n",
300 conn->c_cdn, conn->c_dn, method );
302 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
304 /* send this here to avoid a race condition */
305 send_ldap_result( conn, op, LDAP_SUCCESS,
306 NULL, NULL, NULL, NULL );
308 } else if (edn != NULL) {
313 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
314 NULL, "Function not implemented", NULL, NULL );
324 if ( mech != NULL ) {
327 if ( cred.bv_val != NULL ) {