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 );
57 * Force to connection to "anonymous" until bind succeeds.
59 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
60 connection2anonymous( conn );
61 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
63 if ( op->o_dn != NULL ) {
65 op->o_dn = ch_strdup( "" );
68 if ( op->o_ndn != NULL ) {
70 op->o_ndn = ch_strdup( "" );
74 * Parse the bind request. It looks like this:
76 * BindRequest ::= SEQUENCE {
77 * version INTEGER, -- version
78 * name DistinguishedName, -- dn
79 * authentication CHOICE {
80 * simple [0] OCTET STRING -- passwd
81 * krbv42ldap [1] OCTET STRING
82 * krbv42dsa [2] OCTET STRING
83 * SASL [3] SaslCredentials
87 * SaslCredentials ::= SEQUENCE {
88 * mechanism LDAPString,
89 * credentials OCTET STRING OPTIONAL
93 tag = ber_scanf( ber, "{iat" /*}*/, &version, &dn, &method );
95 if ( tag == LBER_ERROR ) {
96 Debug( LDAP_DEBUG_ANY, "bind: ber_scanf failed\n", 0, 0, 0 );
97 send_ldap_disconnect( conn, op,
98 LDAP_PROTOCOL_ERROR, "decoding error" );
103 op->o_protocol = version;
105 if( method != LDAP_AUTH_SASL ) {
106 tag = ber_scanf( ber, /*{*/ "o}", &cred );
109 tag = ber_scanf( ber, "{a" /*}*/, &mech );
111 if ( tag != LBER_ERROR ) {
113 tag = ber_peek_tag( ber, &len );
115 if ( tag == LDAP_TAG_LDAPCRED ) {
116 tag = ber_scanf( ber, "o", &cred );
119 if ( tag != LBER_ERROR ) {
120 tag = ber_scanf( ber, /*{{*/ "}}" );
125 if ( tag == LBER_ERROR ) {
126 send_ldap_disconnect( conn, op,
129 rc = SLAPD_DISCONNECT;
133 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
134 Debug( LDAP_DEBUG_ANY, "do_bind: get_ctrls failed\n", 0, 0, 0 );
138 ndn = ch_strdup( dn );
140 if ( dn_normalize( ndn ) == NULL ) {
141 Debug( LDAP_DEBUG_ANY, "bind: invalid dn (%s)\n", dn, 0, 0 );
142 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
143 "invalid DN", NULL, NULL );
147 if( method == LDAP_AUTH_SASL ) {
148 Debug( LDAP_DEBUG_TRACE, "do_sasl_bind: dn (%s) mech %s\n",
151 Debug( LDAP_DEBUG_TRACE, "do_bind: version=%ld dn=\"%s\" method=%ld\n",
152 (unsigned long) version, dn, (unsigned long) method );
155 Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d BIND dn=\"%s\" method=%ld\n",
156 op->o_connid, op->o_opid, ndn, (unsigned long) method, 0 );
158 if ( version < LDAP_VERSION_MIN || version > LDAP_VERSION_MAX ) {
159 Debug( LDAP_DEBUG_ANY, "do_bind: unknown version=%ld\n",
160 (unsigned long) version, 0, 0 );
161 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
162 NULL, "requested protocol version not supported", NULL, NULL );
165 } else if (( global_disallows & SLAP_DISALLOW_BIND_V2 ) &&
166 version < LDAP_VERSION3 )
168 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
169 NULL, "requested protocol version not allowed", NULL, NULL );
173 /* we set connection version regardless of whether bind succeeds
176 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
177 conn->c_protocol = version;
178 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
180 if ( method == LDAP_AUTH_SASL ) {
184 if ( version < LDAP_VERSION3 ) {
185 Debug( LDAP_DEBUG_ANY, "do_bind: sasl with LDAPv%ld\n",
186 (unsigned long) version, 0, 0 );
187 send_ldap_disconnect( conn, op,
188 LDAP_PROTOCOL_ERROR, "SASL bind requires LDAPv3" );
189 rc = SLAPD_DISCONNECT;
193 if( mech == NULL || *mech == '\0' ) {
194 Debug( LDAP_DEBUG_ANY,
195 "do_bind: no sasl mechanism provided\n",
197 send_ldap_result( conn, op, rc = LDAP_AUTH_METHOD_NOT_SUPPORTED,
198 NULL, "no SASL mechanism provided", NULL, NULL );
202 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
204 if ( conn->c_sasl_bind_mech != NULL ) {
205 /* SASL bind is in progress */
208 if((strcmp(conn->c_sasl_bind_mech, mech) != 0)) {
209 /* mechanism changed */
210 slap_sasl_reset(conn);
213 free( conn->c_sasl_bind_mech );
214 conn->c_sasl_bind_mech = NULL;
218 /* SASL bind is NOT in progress */
220 assert( conn->c_sasl_bind_mech == NULL );
224 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
227 rc = slap_sasl_bind( conn, op, dn, ndn, saslmech, &cred,
230 if( rc == LDAP_SUCCESS ) {
231 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
233 conn->c_authmech = mech;
234 if( ssf ) conn->c_sasl_layers++;
235 conn->c_sasl_ssf = ssf;
236 if( ssf > conn->c_ssf ) {
239 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
241 } else if ( rc == LDAP_SASL_BIND_IN_PROGRESS ) {
242 conn->c_sasl_bind_mech = mech;
250 /* Not SASL, cancel any in-progress bind */
251 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
253 if ( conn->c_sasl_bind_mech != NULL ) {
254 assert( conn->c_sasl_bind_in_progress );
256 free(conn->c_sasl_bind_mech);
257 conn->c_sasl_bind_mech = NULL;
260 assert( !conn->c_sasl_bind_in_progress );
263 slap_sasl_reset( conn );
264 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
267 if ( method == LDAP_AUTH_SIMPLE ) {
268 /* accept "anonymous" binds */
269 if ( cred.bv_len == 0 || ndn == NULL || *ndn == '\0' ) {
274 ( global_disallows & SLAP_DISALLOW_BIND_ANON_CRED ))
276 /* cred is not empty, disallow */
277 rc = LDAP_INVALID_CREDENTIALS;
279 } else if ( ndn != NULL && *ndn != '\0' &&
280 ( global_disallows & SLAP_DISALLOW_BIND_ANON_DN ))
282 /* DN is not empty, disallow */
283 rc = LDAP_UNWILLING_TO_PERFORM;
284 text = "unwilling to allow anonymous bind with non-empty DN";
286 } else if ( global_disallows & SLAP_DISALLOW_BIND_ANON ) {
288 rc = LDAP_INAPPROPRIATE_AUTH;
289 text = "anonymous bind disallowed";
293 * we already forced connection to "anonymous",
294 * just need to send success
296 send_ldap_result( conn, op, rc,
297 NULL, text, NULL, NULL );
298 Debug( LDAP_DEBUG_TRACE, "do_bind: v%d anonymous bind\n",
302 } else if ( global_disallows & SLAP_DISALLOW_BIND_SIMPLE ) {
303 /* disallow simple authentication */
304 rc = LDAP_UNWILLING_TO_PERFORM;
305 text = "unwilling to perform simple authentication";
307 send_ldap_result( conn, op, rc,
308 NULL, text, NULL, NULL );
309 Debug( LDAP_DEBUG_TRACE,
310 "do_bind: v%d simple bind(%s) disallowed\n",
315 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
316 } else if ( method == LDAP_AUTH_KRBV41 || method == LDAP_AUTH_KRBV42 ) {
317 if ( global_disallows & SLAP_DISALLOW_BIND_KRBV4 ) {
318 /* disallow simple authentication */
319 rc = LDAP_UNWILLING_TO_PERFORM;
320 text = "unwilling to perform Kerberos V4 bind";
322 send_ldap_result( conn, op, rc,
323 NULL, text, NULL, NULL );
324 Debug( LDAP_DEBUG_TRACE, "do_bind: v%d Kerberos V4 bind\n",
331 rc = LDAP_AUTH_UNKNOWN;
332 text = "unknown authentication method";
334 send_ldap_result( conn, op, rc,
335 NULL, text, NULL, NULL );
336 Debug( LDAP_DEBUG_TRACE,
337 "do_bind: v%d unknown authentication method (%d)\n",
338 version, method, 0 );
343 * We could be serving multiple database backends. Select the
344 * appropriate one, or send a referral to our "referral server"
345 * if we don't hold it.
348 if ( (be = select_backend( ndn )) == NULL ) {
349 if ( default_referral ) {
350 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
351 NULL, NULL, default_referral, NULL );
354 /* noSuchObject is not allowed to be returned by bind */
355 send_ldap_result( conn, op, rc = LDAP_INVALID_CREDENTIALS,
356 NULL, NULL, NULL, NULL );
362 /* check restrictions */
363 rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
364 if( rc != LDAP_SUCCESS ) {
365 send_ldap_result( conn, op, rc,
366 NULL, text, NULL, NULL );
370 conn->c_authz_backend = be;
377 /* deref suffix alias if appropriate */
378 ndn = suffix_alias( be, ndn );
380 ret = (*be->be_bind)( be, conn, op, dn, ndn,
381 method, &cred, &edn );
384 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
396 Debug( LDAP_DEBUG_TRACE, "do_bind: v%d bind: \"%s\" to \"%s\"\n",
397 version, conn->c_cdn, conn->c_dn );
399 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
401 /* send this here to avoid a race condition */
402 send_ldap_result( conn, op, LDAP_SUCCESS,
403 NULL, NULL, NULL, NULL );
405 } else if (edn != NULL) {
410 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
411 NULL, "operation not supported within namingContext", NULL, NULL );
415 if( rc != LDAP_SASL_BIND_IN_PROGRESS ) {
416 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
418 /* dispose of mech */
419 free( conn->c_sasl_bind_mech );
420 conn->c_sasl_bind_mech = NULL;
422 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
431 if ( mech != NULL ) {
434 if ( cred.bv_val != NULL ) {