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 if( ssf ) conn->c_sasl_layers++;
227 conn->c_sasl_ssf = ssf;
228 if( ssf > conn->c_ssf ) {
231 } else if ( rc == LDAP_SASL_BIND_IN_PROGRESS ) {
232 conn->c_sasl_bind_in_progress = 1;
235 if ( conn->c_sasl_bind_mech ) {
236 free( conn->c_sasl_bind_mech );
237 conn->c_sasl_bind_mech = NULL;
239 conn->c_sasl_bind_in_progress = 0;
241 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
246 /* Not SASL, cancel any in-progress bind */
247 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
249 if ( conn->c_sasl_bind_mech != NULL ) {
250 free(conn->c_sasl_bind_mech);
251 conn->c_sasl_bind_mech = NULL;
253 conn->c_sasl_bind_in_progress = 0;
255 slap_sasl_reset( conn );
256 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
259 if ( method == LDAP_AUTH_SIMPLE ) {
260 /* accept "anonymous" binds */
261 if ( cred.bv_len == 0 || ndn == NULL || *ndn == '\0' ) {
266 ( global_disallows & SLAP_DISALLOW_BIND_ANON_CRED ))
268 /* cred is not empty, disallow */
269 rc = LDAP_INVALID_CREDENTIALS;
271 } else if ( ndn != NULL && *ndn != '\0' &&
272 ( global_disallows & SLAP_DISALLOW_BIND_ANON_DN ))
274 /* DN is not empty, disallow */
275 rc = LDAP_UNWILLING_TO_PERFORM;
276 text = "unwilling to allow anonymous bind with non-empty DN";
278 } else if ( global_disallows & SLAP_DISALLOW_BIND_ANON ) {
280 rc = LDAP_INAPPROPRIATE_AUTH;
281 text = "anonymous bind disallowed";
285 * we already forced connection to "anonymous",
286 * just need to send success
288 send_ldap_result( conn, op, rc,
289 NULL, text, NULL, NULL );
290 Debug( LDAP_DEBUG_TRACE, "do_bind: v%d anonymous bind\n",
294 } else if ( global_disallows & SLAP_DISALLOW_BIND_SIMPLE ) {
295 /* disallow simple authentication */
296 rc = LDAP_UNWILLING_TO_PERFORM;
297 text = "unwilling to perform simple authentication";
299 send_ldap_result( conn, op, rc,
300 NULL, text, NULL, NULL );
301 Debug( LDAP_DEBUG_TRACE,
302 "do_bind: v%d simple bind(%s) disallowed\n",
307 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
308 } else if ( method == LDAP_AUTH_KRBV41 || method == LDAP_AUTH_KRBV42 ) {
309 if ( global_disallows & SLAP_DISALLOW_BIND_KRBV4 ) {
310 /* disallow simple authentication */
311 rc = LDAP_UNWILLING_TO_PERFORM;
312 text = "unwilling to perform Kerberos V4 bind";
314 send_ldap_result( conn, op, rc,
315 NULL, text, NULL, NULL );
316 Debug( LDAP_DEBUG_TRACE, "do_bind: v%d Kerberos V4 bind\n",
323 rc = LDAP_AUTH_UNKNOWN;
324 text = "unknown authentication method";
326 send_ldap_result( conn, op, rc,
327 NULL, text, NULL, NULL );
328 Debug( LDAP_DEBUG_TRACE,
329 "do_bind: v%d unknown authentication method (%d)\n",
330 version, method, 0 );
335 * We could be serving multiple database backends. Select the
336 * appropriate one, or send a referral to our "referral server"
337 * if we don't hold it.
340 if ( (be = select_backend( ndn )) == NULL ) {
341 if ( default_referral ) {
342 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
343 NULL, NULL, default_referral, NULL );
346 /* noSuchObject is not allowed to be returned by bind */
347 send_ldap_result( conn, op, rc = LDAP_INVALID_CREDENTIALS,
348 NULL, NULL, NULL, NULL );
354 /* check restrictions */
355 rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
356 if( rc != LDAP_SUCCESS ) {
357 send_ldap_result( conn, op, rc,
358 NULL, text, NULL, NULL );
362 conn->c_authz_backend = be;
369 /* deref suffix alias if appropriate */
370 ndn = suffix_alias( be, ndn );
372 ret = (*be->be_bind)( be, conn, op, dn, ndn,
373 method, &cred, &edn );
376 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
388 Debug( LDAP_DEBUG_TRACE, "do_bind: v%d bind: \"%s\" to \"%s\"\n",
389 version, conn->c_cdn, conn->c_dn );
391 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
393 /* send this here to avoid a race condition */
394 send_ldap_result( conn, op, LDAP_SUCCESS,
395 NULL, NULL, NULL, NULL );
397 } else if (edn != NULL) {
402 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
403 NULL, "operation not supported within namingContext", NULL, NULL );
413 if ( mech != NULL ) {
416 if ( cred.bv_val != NULL ) {