1 /* bind.c - decode an ldap bind operation and pass it to a backend db */
4 * Copyright (c) 1995 Regents of the University of Michigan.
7 * Redistribution and use in source and binary forms are permitted
8 * provided that this notice is preserved and that due credit is given
9 * to the University of Michigan at Ann Arbor. The name of the University
10 * may not be used to endorse or promote products derived from this
11 * software without specific prior written permission. This software
12 * is provided ``as is'' without express or implied warranty.
19 #include <ac/string.h>
20 #include <ac/socket.h>
24 char *supportedSASLMechanisms[] = {
35 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, &cdn, &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 op->o_protocol = version;
113 if( method != LDAP_AUTH_SASL ) {
114 tag = ber_scanf( ber, /*{*/ "o}", &cred );
117 tag = ber_scanf( ber, "{a" /*}*/, &mech );
119 if ( tag != LBER_ERROR ) {
121 tag = ber_peek_tag( ber, &len );
123 if ( tag == LDAP_TAG_LDAPCRED ) {
124 tag = ber_scanf( ber, "o", &cred );
127 if ( tag != LBER_ERROR ) {
128 tag = ber_scanf( ber, /*{{*/ "}}" );
133 if ( tag == LBER_ERROR ) {
134 send_ldap_disconnect( conn, op,
141 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
142 Debug( LDAP_DEBUG_ANY, "do_bind: get_ctrls failed\n", 0, 0, 0 );
146 if( method == LDAP_AUTH_SASL ) {
147 Debug( LDAP_DEBUG_TRACE, "do_sasl_bind: dn (%s) mech %s\n",
150 Debug( LDAP_DEBUG_TRACE, "do_bind: version %d dn (%s) method %d\n",
151 version, cdn, method );
154 ndn = dn_normalize_case( ch_strdup( cdn ) );
156 Statslog( LDAP_DEBUG_STATS, "conn=%d op=%d BIND dn=\"%s\" method=%d\n",
157 conn->c_connid, op->o_opid, ndn, method, 0 );
159 if ( version < LDAP_VERSION_MIN || version > LDAP_VERSION_MAX ) {
160 Debug( LDAP_DEBUG_ANY, "unknown version %d\n", version, 0, 0 );
161 send_ldap_result( conn, op,
162 rc = LDAP_PROTOCOL_ERROR, NULL, "version not supported" );
166 if ( method == LDAP_AUTH_SASL ) {
167 if ( version < LDAP_VERSION3 ) {
168 Debug( LDAP_DEBUG_ANY, "do_bind: sasl with LDAPv%d\n",
170 send_ldap_disconnect( conn, op,
171 LDAP_PROTOCOL_ERROR, "sasl bind requires LDAPv3" );
176 if( mech == NULL || *mech == '\0' ) {
177 Debug( LDAP_DEBUG_ANY,
178 "do_bind: no sasl mechanism provided\n",
180 send_ldap_result( conn, op, rc = LDAP_AUTH_METHOD_NOT_SUPPORTED,
181 NULL, "no sasl mechanism provided" );
185 if( !charray_inlist( supportedSASLMechanisms, mech ) ) {
186 Debug( LDAP_DEBUG_ANY,
187 "do_bind: sasl mechanism \"%s\" not supported.\n",
189 send_ldap_result( conn, op, rc = LDAP_AUTH_METHOD_NOT_SUPPORTED,
190 NULL, "sasl mechanism not supported" );
194 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
196 if ( conn->c_authmech != NULL ) {
197 assert( conn->c_bind_in_progress );
199 if((strcmp(conn->c_authmech, mech) != 0)) {
200 /* mechanism changed, cancel in progress bind */
201 conn->c_bind_in_progress = 0;
202 if( conn->c_authstate != NULL ) {
203 free(conn->c_authstate);
204 conn->c_authstate = NULL;
206 free(conn->c_authmech);
207 conn->c_authmech = NULL;
212 assert( !conn->c_bind_in_progress );
213 assert( conn->c_authmech == NULL );
214 assert( conn->c_authstate == NULL );
219 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
221 if ( conn->c_authmech != NULL ) {
222 assert( conn->c_bind_in_progress );
224 /* cancel in progress bind */
225 conn->c_bind_in_progress = 0;
227 if( conn->c_authstate != NULL ) {
228 free(conn->c_authstate);
229 conn->c_authstate = NULL;
231 free(conn->c_authmech);
232 conn->c_authmech = NULL;
236 conn->c_protocol = version;
237 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
239 /* accept null binds */
240 if ( ndn == NULL || *ndn == '\0' ) {
242 * we already forced connection to "anonymous", we just
243 * need to send success
245 send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
250 * We could be serving multiple database backends. Select the
251 * appropriate one, or send a referral to our "referral server"
252 * if we don't hold it.
255 if ( (be = select_backend( ndn )) == NULL ) {
256 if ( cred.bv_len == 0 ) {
257 send_ldap_result( conn, op, LDAP_SUCCESS,
260 } else if ( default_referral && *default_referral ) {
261 send_ldap_result( conn, op, rc = LDAP_PARTIAL_RESULTS,
262 NULL, default_referral );
265 send_ldap_result( conn, op, rc = LDAP_INVALID_CREDENTIALS,
266 NULL, default_referral );
276 ndn = suffixAlias( ndn, op, be );
278 if ( (*be->be_bind)( be, conn, op, ndn, method, mech, &cred, &edn ) == 0 ) {
279 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
291 Debug( LDAP_DEBUG_TRACE, "do_bind: bound \"%s\" to \"%s\"\n",
292 conn->c_cdn, conn->c_dn, method );
294 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
296 /* send this here to avoid a race condition */
297 send_ldap_result( conn, op, LDAP_SUCCESS, NULL, NULL );
299 } else if (edn != NULL) {
304 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM, NULL,
305 "Function not implemented" );
315 if ( mech != NULL ) {
318 if ( cred.bv_val != NULL ) {