1 /* bind.c - decode an ldap bind operation and pass it to a backend db */
4 * Copyright 1998-2000 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 );
56 * Force to connection to "anonymous" until bind succeeds.
58 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
59 connection2anonymous( conn );
60 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
62 if ( op->o_dn != NULL ) {
64 op->o_dn = ch_strdup( "" );
67 if ( op->o_ndn != NULL ) {
69 op->o_ndn = ch_strdup( "" );
73 * Parse the bind request. It looks like this:
75 * BindRequest ::= SEQUENCE {
76 * version INTEGER, -- version
77 * name DistinguishedName, -- dn
78 * authentication CHOICE {
79 * simple [0] OCTET STRING -- passwd
80 * krbv42ldap [1] OCTET STRING
81 * krbv42dsa [2] OCTET STRING
82 * SASL [3] SaslCredentials
86 * SaslCredentials ::= SEQUENCE {
87 * mechanism LDAPString,
88 * credentials OCTET STRING OPTIONAL
92 tag = ber_scanf( ber, "{iat" /*}*/, &version, &dn, &method );
94 if ( tag == LBER_ERROR ) {
95 Debug( LDAP_DEBUG_ANY, "bind: ber_scanf failed\n", 0, 0, 0 );
96 send_ldap_disconnect( conn, op,
97 LDAP_PROTOCOL_ERROR, "decoding error" );
102 op->o_protocol = version;
104 if( method != LDAP_AUTH_SASL ) {
105 tag = ber_scanf( ber, /*{*/ "o}", &cred );
108 tag = ber_scanf( ber, "{a" /*}*/, &mech );
110 if ( tag != LBER_ERROR ) {
112 tag = ber_peek_tag( ber, &len );
114 if ( tag == LDAP_TAG_LDAPCRED ) {
115 tag = ber_scanf( ber, "o", &cred );
117 tag = LDAP_TAG_LDAPCRED;
122 if ( tag != LBER_ERROR ) {
123 tag = ber_scanf( ber, /*{{*/ "}}" );
128 if ( tag == LBER_ERROR ) {
129 send_ldap_disconnect( conn, op,
132 rc = SLAPD_DISCONNECT;
136 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
137 Debug( LDAP_DEBUG_ANY, "do_bind: get_ctrls failed\n", 0, 0, 0 );
141 ndn = ch_strdup( dn );
143 if ( dn_normalize( ndn ) == NULL ) {
144 Debug( LDAP_DEBUG_ANY, "bind: invalid dn (%s)\n", dn, 0, 0 );
145 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
146 "invalid DN", NULL, NULL );
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=%ld dn=\"%s\" method=%ld\n",
155 (unsigned long) version, dn, (unsigned long) method );
158 Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d BIND dn=\"%s\" method=%ld\n",
159 op->o_connid, op->o_opid, ndn, (unsigned long) method, 0 );
161 if ( version < LDAP_VERSION_MIN || version > LDAP_VERSION_MAX ) {
162 Debug( LDAP_DEBUG_ANY, "do_bind: unknown version=%ld\n",
163 (unsigned long) version, 0, 0 );
164 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
165 NULL, "requested protocol version not supported", NULL, NULL );
168 } else if (( global_disallows & SLAP_DISALLOW_BIND_V2 ) &&
169 version < LDAP_VERSION3 )
171 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
172 NULL, "requested protocol version not allowed", NULL, NULL );
176 /* we set connection version regardless of whether bind succeeds
179 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
180 conn->c_protocol = version;
181 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
183 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_AUTH_METHOD_NOT_SUPPORTED,
201 NULL, "no SASL mechanism provided", NULL, NULL );
205 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
206 if ( conn->c_sasl_bind_in_progress ) {
207 if((strcmp(conn->c_sasl_bind_mech, mech) != 0)) {
208 /* mechanism changed between bind steps */
209 slap_sasl_reset(conn);
212 conn->c_sasl_bind_mech = mech;
215 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
218 rc = slap_sasl_bind( conn, op, dn, ndn, &cred, &edn, &ssf );
220 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
221 if( rc == LDAP_SUCCESS ) {
223 conn->c_authmech = conn->c_sasl_bind_mech;
224 conn->c_sasl_bind_mech = NULL;
225 conn->c_sasl_bind_in_progress = 0;
226 conn->c_sasl_ssf = ssf;
227 if( ssf > conn->c_ssf ) {
230 } else if ( rc == LDAP_SASL_BIND_IN_PROGRESS ) {
231 conn->c_sasl_bind_in_progress = 1;
234 if ( conn->c_sasl_bind_mech ) {
235 free( conn->c_sasl_bind_mech );
236 conn->c_sasl_bind_mech = NULL;
238 conn->c_sasl_bind_in_progress = 0;
240 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
245 /* Not SASL, cancel any in-progress bind */
246 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
248 if ( conn->c_sasl_bind_mech != NULL ) {
249 free(conn->c_sasl_bind_mech);
250 conn->c_sasl_bind_mech = NULL;
252 conn->c_sasl_bind_in_progress = 0;
254 slap_sasl_reset( conn );
255 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
258 if ( method == LDAP_AUTH_SIMPLE ) {
259 /* accept "anonymous" binds */
260 if ( cred.bv_len == 0 || ndn == NULL || *ndn == '\0' ) {
265 ( global_disallows & SLAP_DISALLOW_BIND_ANON_CRED ))
267 /* cred is not empty, disallow */
268 rc = LDAP_INVALID_CREDENTIALS;
270 } else if ( ndn != NULL && *ndn != '\0' &&
271 ( global_disallows & SLAP_DISALLOW_BIND_ANON_DN ))
273 /* DN is not empty, disallow */
274 rc = LDAP_UNWILLING_TO_PERFORM;
275 text = "unwilling to allow anonymous bind with non-empty DN";
277 } else if ( global_disallows & SLAP_DISALLOW_BIND_ANON ) {
279 rc = LDAP_INAPPROPRIATE_AUTH;
280 text = "anonymous bind disallowed";
284 * we already forced connection to "anonymous",
285 * just need to send success
287 send_ldap_result( conn, op, rc,
288 NULL, text, NULL, NULL );
289 Debug( LDAP_DEBUG_TRACE, "do_bind: v%d anonymous bind\n",
293 } else if ( global_disallows & SLAP_DISALLOW_BIND_SIMPLE ) {
294 /* disallow simple authentication */
295 rc = LDAP_UNWILLING_TO_PERFORM;
296 text = "unwilling to perform simple authentication";
298 send_ldap_result( conn, op, rc,
299 NULL, text, NULL, NULL );
300 Debug( LDAP_DEBUG_TRACE,
301 "do_bind: v%d simple bind(%s) disallowed\n",
306 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
307 } else if ( method == LDAP_AUTH_KRBV41 || method == LDAP_AUTH_KRBV42 ) {
308 if ( global_disallows & SLAP_DISALLOW_BIND_KRBV4 ) {
309 /* disallow simple authentication */
310 rc = LDAP_UNWILLING_TO_PERFORM;
311 text = "unwilling to perform Kerberos V4 bind";
313 send_ldap_result( conn, op, rc,
314 NULL, text, NULL, NULL );
315 Debug( LDAP_DEBUG_TRACE, "do_bind: v%d Kerberos V4 bind\n",
322 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
323 text = "unknown authentication method";
325 send_ldap_result( conn, op, rc,
326 NULL, text, NULL, NULL );
327 Debug( LDAP_DEBUG_TRACE,
328 "do_bind: v%d unknown authentication method (%d)\n",
329 version, method, 0 );
334 * We could be serving multiple database backends. Select the
335 * appropriate one, or send a referral to our "referral server"
336 * if we don't hold it.
339 if ( (be = select_backend( ndn, 0 )) == NULL ) {
340 if ( default_referral ) {
341 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
342 NULL, NULL, default_referral, NULL );
345 /* noSuchObject is not allowed to be returned by bind */
346 send_ldap_result( conn, op, rc = LDAP_INVALID_CREDENTIALS,
347 NULL, NULL, NULL, NULL );
353 /* check restrictions */
354 rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
355 if( rc != LDAP_SUCCESS ) {
356 send_ldap_result( conn, op, rc,
357 NULL, text, NULL, NULL );
361 conn->c_authz_backend = be;
368 /* deref suffix alias if appropriate */
369 ndn = suffix_alias( be, ndn );
371 ret = (*be->be_bind)( be, conn, op, dn, ndn,
372 method, &cred, &edn );
375 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
387 Debug( LDAP_DEBUG_TRACE, "do_bind: v%d bind: \"%s\" to \"%s\"\n",
388 version, conn->c_cdn, conn->c_dn );
390 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
392 /* send this here to avoid a race condition */
393 send_ldap_result( conn, op, LDAP_SUCCESS,
394 NULL, NULL, NULL, NULL );
396 } else if (edn != NULL) {
401 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
402 NULL, "operation not supported within namingContext", NULL, NULL );
412 if ( mech != NULL ) {
415 if ( cred.bv_val != NULL ) {