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>
30 BerElement *ber = op->o_ber;
36 int rc = LDAP_SUCCESS;
40 Debug( LDAP_DEBUG_TRACE, "do_bind\n", 0, 0, 0 );
47 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
49 /* Force to connection to "anonymous" until bind succeeds.
50 * This may need to be relocated or done on a case by case basis
51 * to handle certain SASL mechanisms.
54 if ( conn->c_cdn != NULL ) {
59 if ( conn->c_dn != NULL ) {
64 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
66 if ( op->o_dn != NULL ) {
68 op->o_dn = ch_strdup( "" );
71 if ( op->o_ndn != NULL ) {
73 op->o_ndn = ch_strdup( "" );
77 * Parse the bind request. It looks like this:
79 * BindRequest ::= SEQUENCE {
80 * version INTEGER, -- version
81 * name DistinguishedName, -- dn
82 * authentication CHOICE {
83 * simple [0] OCTET STRING -- passwd
84 * krbv42ldap [1] OCTET STRING
85 * krbv42dsa [2] OCTET STRING
86 * SASL [3] SaslCredentials
90 * SaslCredentials ::= SEQUENCE {
91 * mechanism LDAPString,
92 * credentials OCTET STRING OPTIONAL
96 tag = ber_scanf( ber, "{iat" /*}*/, &version, &dn, &method );
98 if ( tag == LBER_ERROR ) {
99 Debug( LDAP_DEBUG_ANY, "bind: ber_scanf failed\n", 0, 0, 0 );
100 send_ldap_disconnect( conn, op,
101 LDAP_PROTOCOL_ERROR, "decoding error" );
106 ndn = ch_strdup( dn );
108 if ( dn_normalize_case( ndn ) == NULL ) {
109 Debug( LDAP_DEBUG_ANY, "bind: invalid dn (%s)\n", dn, 0, 0 );
110 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
111 "invalid DN", NULL, NULL );
115 op->o_protocol = version;
117 if( method != LDAP_AUTH_SASL ) {
118 tag = ber_scanf( ber, /*{*/ "o}", &cred );
121 tag = ber_scanf( ber, "{a" /*}*/, &mech );
123 if ( tag != LBER_ERROR ) {
125 tag = ber_peek_tag( ber, &len );
127 if ( tag == LDAP_TAG_LDAPCRED ) {
128 tag = ber_scanf( ber, "o", &cred );
131 if ( tag != LBER_ERROR ) {
132 tag = ber_scanf( ber, /*{{*/ "}}" );
137 if ( tag == LBER_ERROR ) {
138 send_ldap_disconnect( conn, op,
145 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
146 Debug( LDAP_DEBUG_ANY, "do_bind: get_ctrls failed\n", 0, 0, 0 );
150 if( method == LDAP_AUTH_SASL ) {
151 Debug( LDAP_DEBUG_TRACE, "do_sasl_bind: dn (%s) mech %s\n",
154 Debug( LDAP_DEBUG_TRACE, "do_bind: version %d dn (%s) method %d\n",
155 version, dn, method );
158 Statslog( LDAP_DEBUG_STATS, "conn=%d op=%d BIND dn=\"%s\" method=%d\n",
159 op->o_connid, op->o_opid, ndn, method, 0 );
161 if ( version < LDAP_VERSION_MIN || version > LDAP_VERSION_MAX ) {
162 Debug( LDAP_DEBUG_ANY, "unknown version %d\n", version, 0, 0 );
163 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
164 NULL, "version not supported", NULL, NULL );
168 if ( method == LDAP_AUTH_SASL ) {
169 if ( version < LDAP_VERSION3 ) {
170 Debug( LDAP_DEBUG_ANY, "do_bind: sasl with LDAPv%d\n",
172 send_ldap_disconnect( conn, op,
173 LDAP_PROTOCOL_ERROR, "sasl bind requires LDAPv3" );
178 if( mech == NULL || *mech == '\0' ) {
179 Debug( LDAP_DEBUG_ANY,
180 "do_bind: no sasl mechanism provided\n",
182 send_ldap_result( conn, op, rc = LDAP_AUTH_METHOD_NOT_SUPPORTED,
183 NULL, "no sasl mechanism provided", NULL, NULL );
187 if( !charray_inlist( supportedSASLMechanisms, mech ) ) {
188 Debug( LDAP_DEBUG_ANY,
189 "do_bind: sasl mechanism \"%s\" not supported.\n",
191 send_ldap_result( conn, op, rc = LDAP_AUTH_METHOD_NOT_SUPPORTED,
192 NULL, "sasl mechanism not supported", NULL, NULL );
196 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
198 if ( conn->c_authmech != NULL ) {
199 assert( conn->c_bind_in_progress );
201 if((strcmp(conn->c_authmech, mech) != 0)) {
202 /* mechanism changed, cancel in progress bind */
203 conn->c_bind_in_progress = 0;
204 if( conn->c_authstate != NULL ) {
205 free(conn->c_authstate);
206 conn->c_authstate = NULL;
208 free(conn->c_authmech);
209 conn->c_authmech = NULL;
214 assert( !conn->c_bind_in_progress );
215 assert( conn->c_authmech == NULL );
216 assert( conn->c_authstate == NULL );
221 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
223 if ( conn->c_authmech != NULL ) {
224 assert( conn->c_bind_in_progress );
226 /* cancel in progress bind */
227 conn->c_bind_in_progress = 0;
229 if( conn->c_authstate != NULL ) {
230 free(conn->c_authstate);
231 conn->c_authstate = NULL;
233 free(conn->c_authmech);
234 conn->c_authmech = NULL;
238 conn->c_protocol = version;
239 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
241 /* accept null binds */
242 if ( ndn == NULL || *ndn == '\0' ) {
244 * we already forced connection to "anonymous", we just
245 * need to send success
247 send_ldap_result( conn, op, LDAP_SUCCESS,
248 NULL, NULL, NULL, NULL );
253 * We could be serving multiple database backends. Select the
254 * appropriate one, or send a referral to our "referral server"
255 * if we don't hold it.
258 if ( (be = select_backend( ndn )) == NULL ) {
259 if ( cred.bv_len == 0 ) {
260 send_ldap_result( conn, op, LDAP_SUCCESS,
261 NULL, NULL, NULL, NULL );
263 } else if ( default_referral ) {
264 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
265 NULL, NULL, default_referral, NULL );
268 send_ldap_result( conn, op, rc = LDAP_INVALID_CREDENTIALS,
269 NULL, NULL, NULL, NULL );
279 /* deref suffix alias if appropriate */
280 ndn = suffix_alias( be, ndn );
282 if ( (*be->be_bind)( be, conn, op, ndn, method, mech, &cred, &edn ) == 0 ) {
283 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
295 Debug( LDAP_DEBUG_TRACE, "do_bind: bound \"%s\" to \"%s\"\n",
296 conn->c_cdn, conn->c_dn, method );
298 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
300 /* send this here to avoid a race condition */
301 send_ldap_result( conn, op, LDAP_SUCCESS,
302 NULL, NULL, NULL, NULL );
304 } else if (edn != NULL) {
309 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
310 NULL, "Function not implemented", NULL, NULL );
320 if ( mech != NULL ) {
323 if ( cred.bv_val != NULL ) {