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;
49 LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY, "do_bind: conn %d\n", conn->c_connid ));
51 Debug( LDAP_DEBUG_TRACE, "do_bind\n", 0, 0, 0 );
60 * Force to connection to "anonymous" until bind succeeds.
62 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
63 connection2anonymous( conn );
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 ) {
100 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
101 "do_bind: conn %d ber_scanf failed\n", conn->c_connid ));
103 Debug( LDAP_DEBUG_ANY, "bind: ber_scanf failed\n", 0, 0, 0 );
105 send_ldap_disconnect( conn, op,
106 LDAP_PROTOCOL_ERROR, "decoding error" );
111 op->o_protocol = version;
113 if( method != LDAP_AUTH_SASL ) {
114 tag = ber_scanf( ber, /*{*/ "o}", &cred );
117 tag = ber_scanf( ber, "{a" /*}*/, &mech );
119 if ( tag != LBER_ERROR ) {
121 tag = ber_peek_tag( ber, &len );
123 if ( tag == LDAP_TAG_LDAPCRED ) {
124 tag = ber_scanf( ber, "o", &cred );
126 tag = LDAP_TAG_LDAPCRED;
131 if ( tag != LBER_ERROR ) {
132 tag = ber_scanf( ber, /*{{*/ "}}" );
137 if ( tag == LBER_ERROR ) {
138 send_ldap_disconnect( conn, op,
141 rc = SLAPD_DISCONNECT;
145 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
147 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
148 "do_bind: conn %d get_ctrls failed\n", conn->c_connid ));
150 Debug( LDAP_DEBUG_ANY, "do_bind: get_ctrls failed\n", 0, 0, 0 );
155 ndn = ch_strdup( dn );
157 if ( dn_normalize( ndn ) == NULL ) {
159 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
160 "do_bind: conn %d invalid dn (%s)\n", conn->c_connid, dn ));
162 Debug( LDAP_DEBUG_ANY, "bind: invalid dn (%s)\n", dn, 0, 0 );
164 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
165 "invalid DN", NULL, NULL );
169 if( method == LDAP_AUTH_SASL ) {
171 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
172 "do_sasl_bind: conn %d dn (%s) mech %s\n", conn->c_connid,
175 Debug( LDAP_DEBUG_TRACE, "do_sasl_bind: dn (%s) mech %s\n",
180 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
181 "do_bind: conn %d version=%ld dn=\"%s\" method=%ld\n",
182 conn->c_connid, (unsigned long) version, dn, (unsigned long)method ));
184 Debug( LDAP_DEBUG_TRACE, "do_bind: version=%ld dn=\"%s\" method=%ld\n",
185 (unsigned long) version, dn, (unsigned long) method );
189 Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d BIND dn=\"%s\" method=%ld\n",
190 op->o_connid, op->o_opid, ndn, (unsigned long) method, 0 );
192 if ( version < LDAP_VERSION_MIN || version > LDAP_VERSION_MAX ) {
194 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
195 "do_bind: conn %d unknown version = %ld\n",
196 conn->c_connid, (unsigned long)version ));
198 Debug( LDAP_DEBUG_ANY, "do_bind: unknown version=%ld\n",
199 (unsigned long) version, 0, 0 );
201 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
202 NULL, "requested protocol version not supported", NULL, NULL );
205 } else if (( global_disallows & SLAP_DISALLOW_BIND_V2 ) &&
206 version < LDAP_VERSION3 )
208 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
209 NULL, "requested protocol version not allowed", NULL, NULL );
213 /* we set connection version regardless of whether bind succeeds
216 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
217 conn->c_protocol = version;
218 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
220 if ( method == LDAP_AUTH_SASL ) {
224 if ( version < LDAP_VERSION3 ) {
226 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
227 "do_bind: conn %d sasl with LDAPv%ld\n",
228 conn->c_connid, (unsigned long)version ));
230 Debug( LDAP_DEBUG_ANY, "do_bind: sasl with LDAPv%ld\n",
231 (unsigned long) version, 0, 0 );
233 send_ldap_disconnect( conn, op,
234 LDAP_PROTOCOL_ERROR, "SASL bind requires LDAPv3" );
235 rc = SLAPD_DISCONNECT;
239 if( mech == NULL || *mech == '\0' ) {
241 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
242 "do_bind: conn %d no SASL mechanism provided\n",
245 Debug( LDAP_DEBUG_ANY,
246 "do_bind: no sasl mechanism provided\n",
249 send_ldap_result( conn, op, rc = LDAP_AUTH_METHOD_NOT_SUPPORTED,
250 NULL, "no SASL mechanism provided", NULL, NULL );
254 /* check restrictions */
255 rc = backend_check_restrictions( NULL, conn, op, mech, &text );
256 if( rc != LDAP_SUCCESS ) {
257 send_ldap_result( conn, op, rc,
258 NULL, text, NULL, NULL );
262 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
263 if ( conn->c_sasl_bind_in_progress ) {
264 if((strcmp(conn->c_sasl_bind_mech, mech) != 0)) {
265 /* mechanism changed between bind steps */
266 slap_sasl_reset(conn);
269 conn->c_sasl_bind_mech = mech;
272 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
275 rc = slap_sasl_bind( conn, op, dn, ndn, &cred, &edn, &ssf );
277 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
278 if( rc == LDAP_SUCCESS ) {
281 conn->c_ndn = ch_strdup( edn );
282 dn_normalize( conn->c_ndn );
284 conn->c_authmech = conn->c_sasl_bind_mech;
285 conn->c_sasl_bind_mech = NULL;
286 conn->c_sasl_bind_in_progress = 0;
288 conn->c_sasl_ssf = ssf;
289 if( ssf > conn->c_ssf ) {
293 if( conn->c_dn != NULL ) {
294 ber_len_t max = sockbuf_max_incoming;
295 ber_sockbuf_ctrl( conn->c_sb,
296 LBER_SB_OPT_SET_MAX_INCOMING, &max );
299 } else if ( rc == LDAP_SASL_BIND_IN_PROGRESS ) {
300 conn->c_sasl_bind_in_progress = 1;
303 if ( conn->c_sasl_bind_mech ) {
304 free( conn->c_sasl_bind_mech );
305 conn->c_sasl_bind_mech = NULL;
307 conn->c_sasl_bind_in_progress = 0;
309 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
314 /* Not SASL, cancel any in-progress bind */
315 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
317 if ( conn->c_sasl_bind_mech != NULL ) {
318 free(conn->c_sasl_bind_mech);
319 conn->c_sasl_bind_mech = NULL;
321 conn->c_sasl_bind_in_progress = 0;
323 slap_sasl_reset( conn );
324 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
327 if ( method == LDAP_AUTH_SIMPLE ) {
328 /* accept "anonymous" binds */
329 if ( cred.bv_len == 0 || ndn == NULL || *ndn == '\0' ) {
334 ( global_disallows & SLAP_DISALLOW_BIND_ANON_CRED ))
336 /* cred is not empty, disallow */
337 rc = LDAP_INVALID_CREDENTIALS;
339 } else if ( ndn != NULL && *ndn != '\0' &&
340 ( global_disallows & SLAP_DISALLOW_BIND_ANON_DN ))
342 /* DN is not empty, disallow */
343 rc = LDAP_UNWILLING_TO_PERFORM;
344 text = "unwilling to allow anonymous bind with non-empty DN";
346 } else if ( global_disallows & SLAP_DISALLOW_BIND_ANON ) {
348 rc = LDAP_INAPPROPRIATE_AUTH;
349 text = "anonymous bind disallowed";
352 rc = backend_check_restrictions( NULL, conn, op, mech, &text );
356 * we already forced connection to "anonymous",
357 * just need to send success
359 send_ldap_result( conn, op, rc,
360 NULL, text, NULL, NULL );
362 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
363 "do_bind: conn %d v%d anonymous bind\n",
364 conn->c_connid, version ));
366 Debug( LDAP_DEBUG_TRACE, "do_bind: v%d anonymous bind\n",
371 } else if ( global_disallows & SLAP_DISALLOW_BIND_SIMPLE ) {
372 /* disallow simple authentication */
373 rc = LDAP_UNWILLING_TO_PERFORM;
374 text = "unwilling to perform simple authentication";
376 send_ldap_result( conn, op, rc,
377 NULL, text, NULL, NULL );
379 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
380 "do_bind: conn %d v%d simple bind(%s) disallowed\n",
381 conn->c_connid, version, ndn ));
383 Debug( LDAP_DEBUG_TRACE,
384 "do_bind: v%d simple bind(%s) disallowed\n",
390 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
391 } else if ( method == LDAP_AUTH_KRBV41 || method == LDAP_AUTH_KRBV42 ) {
392 if ( global_disallows & SLAP_DISALLOW_BIND_KRBV4 ) {
393 /* disallow simple authentication */
394 rc = LDAP_UNWILLING_TO_PERFORM;
395 text = "unwilling to perform Kerberos V4 bind";
397 send_ldap_result( conn, op, rc,
398 NULL, text, NULL, NULL );
400 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
401 "do_bind: conn %d v%d Kerberos V4 bind\n",
402 conn->c_connid, version ));
404 Debug( LDAP_DEBUG_TRACE, "do_bind: v%d Kerberos V4 bind\n",
412 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
413 text = "unknown authentication method";
415 send_ldap_result( conn, op, rc,
416 NULL, text, NULL, NULL );
418 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
419 "do_bind: conn %ld v%d unknown authentication method (%ld)\n",
420 conn->c_connid, version, method ));
422 Debug( LDAP_DEBUG_TRACE,
423 "do_bind: v%d unknown authentication method (%ld)\n",
424 version, method, 0 );
430 * We could be serving multiple database backends. Select the
431 * appropriate one, or send a referral to our "referral server"
432 * if we don't hold it.
435 if ( (be = select_backend( ndn, 0, 0 )) == NULL ) {
436 if ( default_referral ) {
437 struct berval **ref = referral_rewrite( default_referral,
438 NULL, dn, LDAP_SCOPE_DEFAULT );
440 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
441 NULL, NULL, ref ? ref : default_referral, NULL );
446 /* noSuchObject is not allowed to be returned by bind */
447 send_ldap_result( conn, op, rc = LDAP_INVALID_CREDENTIALS,
448 NULL, NULL, NULL, NULL );
454 /* check restrictions */
455 rc = backend_check_restrictions( be, conn, op, NULL, &text );
456 if( rc != LDAP_SUCCESS ) {
457 send_ldap_result( conn, op, rc,
458 NULL, text, NULL, NULL );
462 conn->c_authz_backend = be;
469 /* deref suffix alias if appropriate */
470 ndn = suffix_alias( be, ndn );
472 ret = (*be->be_bind)( be, conn, op, dn, ndn,
473 method, &cred, &edn );
476 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
484 conn->c_dn = ch_strdup( conn->c_cdn );
489 if( conn->c_dn != NULL ) {
490 ber_len_t max = sockbuf_max_incoming;
491 ber_sockbuf_ctrl( conn->c_sb,
492 LBER_SB_OPT_SET_MAX_INCOMING, &max );
496 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
497 "do_bind: conn %d v%d bind: \"%s\" to \"%s\" \n",
498 conn->c_connid, version, conn->c_cdn, conn->c_dn ));
500 Debug( LDAP_DEBUG_TRACE, "do_bind: v%d bind: \"%s\" to \"%s\"\n",
501 version, conn->c_cdn, conn->c_dn );
504 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
506 /* send this here to avoid a race condition */
507 send_ldap_result( conn, op, LDAP_SUCCESS,
508 NULL, NULL, NULL, NULL );
510 } else if (edn != NULL) {
515 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
516 NULL, "operation not supported within namingContext", NULL, NULL );
526 if ( mech != NULL ) {
529 if ( cred.bv_val != NULL ) {