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;
44 int rc = LDAP_SUCCESS;
49 Debug( LDAP_DEBUG_TRACE, "do_bind\n", 0, 0, 0 );
56 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
59 * Force to connection to "anonymous" until bind succeeds.
62 if ( conn->c_authmech != NULL ) {
63 free( conn->c_authmech );
64 conn->c_authmech = NULL;
67 if ( conn->c_cdn != NULL ) {
72 if ( conn->c_dn != NULL ) {
77 conn->c_authc_backend = NULL;
78 conn->c_authz_backend = NULL;
80 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
82 if ( op->o_dn != NULL ) {
84 op->o_dn = ch_strdup( "" );
87 if ( op->o_ndn != NULL ) {
89 op->o_ndn = ch_strdup( "" );
93 * Parse the bind request. It looks like this:
95 * BindRequest ::= SEQUENCE {
96 * version INTEGER, -- version
97 * name DistinguishedName, -- dn
98 * authentication CHOICE {
99 * simple [0] OCTET STRING -- passwd
100 * krbv42ldap [1] OCTET STRING
101 * krbv42dsa [2] OCTET STRING
102 * SASL [3] SaslCredentials
106 * SaslCredentials ::= SEQUENCE {
107 * mechanism LDAPString,
108 * credentials OCTET STRING OPTIONAL
112 tag = ber_scanf( ber, "{iat" /*}*/, &version, &dn, &method );
114 if ( tag == LBER_ERROR ) {
115 Debug( LDAP_DEBUG_ANY, "bind: ber_scanf failed\n", 0, 0, 0 );
116 send_ldap_disconnect( conn, op,
117 LDAP_PROTOCOL_ERROR, "decoding error" );
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,
148 rc = SLAPD_DISCONNECT;
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 ndn = ch_strdup( dn );
159 if ( dn_normalize( ndn ) == NULL ) {
160 Debug( LDAP_DEBUG_ANY, "bind: invalid dn (%s)\n", dn, 0, 0 );
161 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
162 "invalid DN", NULL, NULL );
166 if( method == LDAP_AUTH_SASL ) {
167 Debug( LDAP_DEBUG_TRACE, "do_sasl_bind: dn (%s) mech %s\n",
170 Debug( LDAP_DEBUG_TRACE, "do_bind: version=%ld dn=\"%s\" method=%ld\n",
171 (unsigned long) version, dn, (unsigned long) method );
174 Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d BIND dn=\"%s\" method=%ld\n",
175 op->o_connid, op->o_opid, ndn, (unsigned long) method, 0 );
177 if ( version < LDAP_VERSION_MIN || version > LDAP_VERSION_MAX ) {
178 Debug( LDAP_DEBUG_ANY, "do_bind: unknown version=%ld\n",
179 (unsigned long) version, 0, 0 );
180 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
181 NULL, "requested protocol version not supported", NULL, NULL );
184 } else if (( global_disallows & SLAP_DISALLOW_BIND_V2 ) &&
185 version < LDAP_VERSION3 )
187 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
188 NULL, "requested protocol version not allowed", NULL, NULL );
192 /* we set connection version regardless of whether bind succeeds
195 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
196 conn->c_protocol = version;
197 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
199 if ( method == LDAP_AUTH_SASL ) {
203 if ( version < LDAP_VERSION3 ) {
204 Debug( LDAP_DEBUG_ANY, "do_bind: sasl with LDAPv%ld\n",
205 (unsigned long) version, 0, 0 );
206 send_ldap_disconnect( conn, op,
207 LDAP_PROTOCOL_ERROR, "SASL bind requires LDAPv3" );
208 rc = SLAPD_DISCONNECT;
212 if( mech == NULL || *mech == '\0' ) {
213 Debug( LDAP_DEBUG_ANY,
214 "do_bind: no sasl mechanism provided\n",
216 send_ldap_result( conn, op, rc = LDAP_AUTH_METHOD_NOT_SUPPORTED,
217 NULL, "no SASL mechanism provided", NULL, NULL );
221 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
223 if ( conn->c_sasl_bind_mech != NULL ) {
224 /* SASL bind is in progress */
227 if((strcmp(conn->c_sasl_bind_mech, mech) != 0)) {
228 /* mechanism changed */
229 slap_sasl_reset(conn);
232 free( conn->c_sasl_bind_mech );
233 conn->c_sasl_bind_mech = NULL;
237 /* SASL bind is NOT in progress */
239 assert( conn->c_sasl_bind_mech == NULL );
243 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
246 rc = slap_sasl_bind( conn, op, dn, ndn, saslmech, &cred,
249 if( rc == LDAP_SUCCESS ) {
250 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
252 conn->c_authmech = mech;
253 if( ssf ) conn->c_sasl_layers++;
254 conn->c_sasl_ssf = ssf;
255 if( ssf > conn->c_ssf ) {
258 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
260 } else if ( rc == LDAP_SASL_BIND_IN_PROGRESS ) {
261 conn->c_sasl_bind_mech = mech;
269 /* Not SASL, cancel any in-progress bind */
270 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
272 if ( conn->c_sasl_bind_mech != NULL ) {
273 assert( conn->c_sasl_bind_in_progress );
275 free(conn->c_sasl_bind_mech);
276 conn->c_sasl_bind_mech = NULL;
279 assert( !conn->c_sasl_bind_in_progress );
282 slap_sasl_reset( conn );
283 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
286 /* accept "anonymous" binds */
287 if ( cred.bv_len == 0 || ndn == NULL || *ndn == '\0' ) {
292 ( global_disallows & SLAP_DISALLOW_BIND_ANON_CRED ))
294 /* cred is not empty, disallow */
295 rc = LDAP_INVALID_CREDENTIALS;
297 } else if ( ndn != NULL && *ndn != '\0' &&
298 ( global_disallows & SLAP_DISALLOW_BIND_ANON_DN ))
300 /* DN is not empty, disallow */
301 rc = LDAP_UNWILLING_TO_PERFORM;
302 text = "unwilling to allow anonymous bind with non-empty DN";
304 } else if ( global_disallows & SLAP_DISALLOW_BIND_ANON ) {
306 rc = LDAP_UNWILLING_TO_PERFORM;
307 text = "anonymous bind disallowed";
311 * we already forced connection to "anonymous",
312 * just need to send success
314 send_ldap_result( conn, op, rc,
315 NULL, text, NULL, NULL );
316 Debug( LDAP_DEBUG_TRACE, "do_bind: v%d anonymous bind\n",
322 * We could be serving multiple database backends. Select the
323 * appropriate one, or send a referral to our "referral server"
324 * if we don't hold it.
327 if ( (be = select_backend( ndn )) == NULL ) {
328 if ( default_referral ) {
329 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
330 NULL, NULL, default_referral, NULL );
333 /* noSuchObject is not allowed to be returned by bind */
334 send_ldap_result( conn, op, rc = LDAP_INVALID_CREDENTIALS,
335 NULL, NULL, NULL, NULL );
341 /* check restrictions */
342 rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
343 if( rc != LDAP_SUCCESS ) {
344 send_ldap_result( conn, op, rc,
345 NULL, text, NULL, NULL );
349 if( op->o_ssf < be->be_ssf_set.sss_ssf ) {
350 text = "confidentiality required";
351 rc = LDAP_CONFIDENTIALITY_REQUIRED;
353 } else if( op->o_transport_ssf < be->be_ssf_set.sss_transport ) {
354 text = "transport confidentiality required";
355 rc = LDAP_CONFIDENTIALITY_REQUIRED;
357 } else if( op->o_tls_ssf < be->be_ssf_set.sss_tls ) {
358 text = "TLS confidentiality required";
359 rc = LDAP_CONFIDENTIALITY_REQUIRED;
361 } else if( op->o_sasl_ssf < be->be_ssf_set.sss_sasl ) {
362 text = "SASL confidentiality required";
363 rc = LDAP_CONFIDENTIALITY_REQUIRED;
365 } else if( be->be_restrictops & SLAP_RESTRICT_OP_BIND ) {
366 text = "bind operation restricted";
367 rc = LDAP_UNWILLING_TO_PERFORM;
370 if( rc != LDAP_SUCCESS ) {
371 send_ldap_result( conn, op, rc,
372 NULL, text, NULL, NULL );
376 conn->c_authz_backend = be;
383 /* deref suffix alias if appropriate */
384 ndn = suffix_alias( be, ndn );
386 ret = (*be->be_bind)( be, conn, op, dn, ndn,
387 method, &cred, &edn );
390 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
402 Debug( LDAP_DEBUG_TRACE, "do_bind: v%d bind: \"%s\" to \"%s\"\n",
403 version, conn->c_cdn, conn->c_dn );
405 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
407 /* send this here to avoid a race condition */
408 send_ldap_result( conn, op, LDAP_SUCCESS,
409 NULL, NULL, NULL, NULL );
411 } else if (edn != NULL) {
416 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
417 NULL, "operation not supported within namingContext", NULL, NULL );
421 if( rc != LDAP_SASL_BIND_IN_PROGRESS ) {
422 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
424 /* dispose of mech */
425 free( conn->c_sasl_bind_mech );
426 conn->c_sasl_bind_mech = NULL;
428 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
437 if ( mech != NULL ) {
440 if ( cred.bv_val != NULL ) {