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;
40 struct berval dn = { 0, NULL };
41 struct berval *pdn = NULL;
42 struct berval *ndn = NULL;
44 int rc = LDAP_SUCCESS;
46 struct berval cred = { 0, NULL };
50 LDAP_LOG(( "operation", LDAP_LEVEL_ENTRY,
51 "do_bind: conn %d\n", conn->c_connid ));
53 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.bv_val != NULL ) {
64 free( op->o_dn.bv_val );
65 op->o_dn.bv_val = ch_strdup( "" );
69 if ( op->o_ndn.bv_val != NULL ) {
70 free( op->o_ndn.bv_val );
71 op->o_ndn.bv_val = ch_strdup( "" );
76 * Parse the bind request. It looks like this:
78 * BindRequest ::= SEQUENCE {
79 * version INTEGER, -- version
80 * name DistinguishedName, -- dn
81 * authentication CHOICE {
82 * simple [0] OCTET STRING -- passwd
83 * krbv42ldap [1] OCTET STRING
84 * krbv42dsa [2] OCTET STRING
85 * SASL [3] SaslCredentials
89 * SaslCredentials ::= SEQUENCE {
90 * mechanism LDAPString,
91 * credentials OCTET STRING OPTIONAL
95 tag = ber_scanf( ber, "{iot" /*}*/, &version, &dn, &method );
97 if ( tag == LBER_ERROR ) {
99 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
100 "do_bind: conn %d ber_scanf failed\n", conn->c_connid ));
102 Debug( LDAP_DEBUG_ANY, "bind: ber_scanf failed\n", 0, 0, 0 );
104 send_ldap_disconnect( conn, op,
105 LDAP_PROTOCOL_ERROR, "decoding error" );
110 op->o_protocol = version;
112 if( method != LDAP_AUTH_SASL ) {
113 tag = ber_scanf( ber, /*{*/ "o}", &cred );
116 tag = ber_scanf( ber, "{a" /*}*/, &mech );
118 if ( tag != LBER_ERROR ) {
120 tag = ber_peek_tag( ber, &len );
122 if ( tag == LDAP_TAG_LDAPCRED ) {
123 tag = ber_scanf( ber, "o", &cred );
125 tag = LDAP_TAG_LDAPCRED;
130 if ( tag != LBER_ERROR ) {
131 tag = ber_scanf( ber, /*{{*/ "}}" );
136 if ( tag == LBER_ERROR ) {
137 send_ldap_disconnect( conn, op,
140 rc = SLAPD_DISCONNECT;
144 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
146 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
147 "do_bind: conn %d get_ctrls failed\n", conn->c_connid ));
149 Debug( LDAP_DEBUG_ANY, "do_bind: get_ctrls failed\n", 0, 0, 0 );
154 rc = dnPretty( NULL, &dn, &pdn );
155 if ( rc != LDAP_SUCCESS ) {
157 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
158 "do_bind: conn %d invalid dn (%s)\n",
159 conn->c_connid, dn.bv_val ));
161 Debug( LDAP_DEBUG_ANY, "bind: invalid dn (%s)\n",
164 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
165 "invalid DN", NULL, NULL );
169 rc = dnNormalize( NULL, &dn, &ndn );
170 if ( rc != LDAP_SUCCESS ) {
172 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
173 "do_bind: conn %d invalid dn (%s)\n",
174 conn->c_connid, dn.bv_val ));
176 Debug( LDAP_DEBUG_ANY, "bind: invalid dn (%s)\n",
179 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
180 "invalid DN", NULL, NULL );
184 if( method == LDAP_AUTH_SASL ) {
186 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
187 "do_sasl_bind: conn %d dn (%s) mech %s\n", conn->c_connid,
188 pdn->bv_val, mech ));
190 Debug( LDAP_DEBUG_TRACE, "do_sasl_bind: dn (%s) mech %s\n",
191 pdn->bv_val, mech, NULL );
196 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
197 "do_bind: conn %d version=%ld dn=\"%s\" method=%ld\n",
198 conn->c_connid, (unsigned long) version,
199 pdn->bv_val, (unsigned long)method ));
201 Debug( LDAP_DEBUG_TRACE,
202 "do_bind: version=%ld dn=\"%s\" method=%ld\n",
203 (unsigned long) version,
204 pdn->bv_val, (unsigned long) method );
208 Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d BIND dn=\"%s\" method=%ld\n",
209 op->o_connid, op->o_opid, pdn->bv_val, (unsigned long) method, 0 );
211 if ( version < LDAP_VERSION_MIN || version > LDAP_VERSION_MAX ) {
213 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
214 "do_bind: conn %d unknown version = %ld\n",
215 conn->c_connid, (unsigned long)version ));
217 Debug( LDAP_DEBUG_ANY, "do_bind: unknown version=%ld\n",
218 (unsigned long) version, 0, 0 );
220 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
221 NULL, "requested protocol version not supported", NULL, NULL );
224 } else if (!( global_allows & SLAP_ALLOW_BIND_V2 ) &&
225 version < LDAP_VERSION3 )
227 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
228 NULL, "requested protocol version not allowed", NULL, NULL );
232 /* we set connection version regardless of whether bind succeeds
235 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
236 conn->c_protocol = version;
237 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
239 if ( method == LDAP_AUTH_SASL ) {
243 if ( version < LDAP_VERSION3 ) {
245 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
246 "do_bind: conn %d sasl with LDAPv%ld\n",
247 conn->c_connid, (unsigned long)version ));
249 Debug( LDAP_DEBUG_ANY, "do_bind: sasl with LDAPv%ld\n",
250 (unsigned long) version, 0, 0 );
252 send_ldap_disconnect( conn, op,
253 LDAP_PROTOCOL_ERROR, "SASL bind requires LDAPv3" );
254 rc = SLAPD_DISCONNECT;
258 if( mech == NULL || *mech == '\0' ) {
260 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
261 "do_bind: conn %d no SASL mechanism provided\n",
264 Debug( LDAP_DEBUG_ANY,
265 "do_bind: no sasl mechanism provided\n",
268 send_ldap_result( conn, op, rc = LDAP_AUTH_METHOD_NOT_SUPPORTED,
269 NULL, "no SASL mechanism provided", NULL, NULL );
273 /* check restrictions */
274 rc = backend_check_restrictions( NULL, conn, op, mech, &text );
275 if( rc != LDAP_SUCCESS ) {
276 send_ldap_result( conn, op, rc,
277 NULL, text, NULL, NULL );
281 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
282 if ( conn->c_sasl_bind_in_progress ) {
283 if((strcmp(conn->c_sasl_bind_mech, mech) != 0)) {
284 /* mechanism changed between bind steps */
285 slap_sasl_reset(conn);
288 conn->c_sasl_bind_mech = mech;
291 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
294 rc = slap_sasl_bind( conn, op,
295 pdn->bv_val, ndn->bv_val,
298 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
299 if( rc == LDAP_SUCCESS ) {
300 conn->c_dn.bv_val = edn;
303 conn->c_dn.bv_len = strlen( edn );
304 dnNormalize( NULL, &conn->c_dn, &cndn );
308 conn->c_authmech = conn->c_sasl_bind_mech;
309 conn->c_sasl_bind_mech = NULL;
310 conn->c_sasl_bind_in_progress = 0;
312 conn->c_sasl_ssf = ssf;
313 if( ssf > conn->c_ssf ) {
317 if( conn->c_dn.bv_len != 0 ) {
318 ber_len_t max = sockbuf_max_incoming;
319 ber_sockbuf_ctrl( conn->c_sb,
320 LBER_SB_OPT_SET_MAX_INCOMING, &max );
323 } else if ( rc == LDAP_SASL_BIND_IN_PROGRESS ) {
324 conn->c_sasl_bind_in_progress = 1;
327 if ( conn->c_sasl_bind_mech ) {
328 free( conn->c_sasl_bind_mech );
329 conn->c_sasl_bind_mech = NULL;
331 conn->c_sasl_bind_in_progress = 0;
333 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
338 /* Not SASL, cancel any in-progress bind */
339 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
341 if ( conn->c_sasl_bind_mech != NULL ) {
342 free(conn->c_sasl_bind_mech);
343 conn->c_sasl_bind_mech = NULL;
345 conn->c_sasl_bind_in_progress = 0;
347 slap_sasl_reset( conn );
348 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
351 if ( method == LDAP_AUTH_SIMPLE ) {
352 /* accept "anonymous" binds */
353 if ( cred.bv_len == 0 || ndn->bv_len == 0 ) {
358 !( global_allows & SLAP_ALLOW_BIND_ANON_CRED ))
360 /* cred is not empty, disallow */
361 rc = LDAP_INVALID_CREDENTIALS;
363 } else if ( ndn->bv_len &&
364 !( global_allows & SLAP_ALLOW_BIND_ANON_DN ))
366 /* DN is not empty, disallow */
367 rc = LDAP_UNWILLING_TO_PERFORM;
368 text = "unwilling to allow anonymous bind with non-empty DN";
370 } else if ( global_disallows & SLAP_DISALLOW_BIND_ANON ) {
372 rc = LDAP_INAPPROPRIATE_AUTH;
373 text = "anonymous bind disallowed";
376 rc = backend_check_restrictions( NULL, conn, op, mech, &text );
380 * we already forced connection to "anonymous",
381 * just need to send success
383 send_ldap_result( conn, op, rc,
384 NULL, text, NULL, NULL );
386 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
387 "do_bind: conn %d v%d anonymous bind\n",
388 conn->c_connid, version ));
390 Debug( LDAP_DEBUG_TRACE, "do_bind: v%d anonymous bind\n",
395 } else if ( global_disallows & SLAP_DISALLOW_BIND_SIMPLE ) {
396 /* disallow simple authentication */
397 rc = LDAP_UNWILLING_TO_PERFORM;
398 text = "unwilling to perform simple authentication";
400 send_ldap_result( conn, op, rc,
401 NULL, text, NULL, NULL );
403 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
404 "do_bind: conn %d v%d simple bind(%s) disallowed\n",
405 conn->c_connid, version, ndn ));
407 Debug( LDAP_DEBUG_TRACE,
408 "do_bind: v%d simple bind(%s) disallowed\n",
414 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
415 } else if ( method == LDAP_AUTH_KRBV41 || method == LDAP_AUTH_KRBV42 ) {
416 if ( global_disallows & SLAP_DISALLOW_BIND_KRBV4 ) {
417 /* disallow simple authentication */
418 rc = LDAP_UNWILLING_TO_PERFORM;
419 text = "unwilling to perform Kerberos V4 bind";
421 send_ldap_result( conn, op, rc,
422 NULL, text, NULL, NULL );
424 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
425 "do_bind: conn %d v%d Kerberos V4 bind\n",
426 conn->c_connid, version ));
428 Debug( LDAP_DEBUG_TRACE, "do_bind: v%d Kerberos V4 bind\n",
436 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
437 text = "unknown authentication method";
439 send_ldap_result( conn, op, rc,
440 NULL, text, NULL, NULL );
442 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
443 "do_bind: conn %ld v%d unknown authentication method (%ld)\n",
444 conn->c_connid, version, method ));
446 Debug( LDAP_DEBUG_TRACE,
447 "do_bind: v%d unknown authentication method (%ld)\n",
448 version, method, 0 );
454 * We could be serving multiple database backends. Select the
455 * appropriate one, or send a referral to our "referral server"
456 * if we don't hold it.
459 if ( (be = select_backend( ndn->bv_val, 0, 0 )) == NULL ) {
460 if ( default_referral ) {
461 struct berval **ref = referral_rewrite( default_referral,
462 NULL, pdn->bv_val, LDAP_SCOPE_DEFAULT );
464 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
465 NULL, NULL, ref ? ref : default_referral, NULL );
470 /* noSuchObject is not allowed to be returned by bind */
471 send_ldap_result( conn, op, rc = LDAP_INVALID_CREDENTIALS,
472 NULL, NULL, NULL, NULL );
478 /* check restrictions */
479 rc = backend_check_restrictions( be, conn, op, NULL, &text );
480 if( rc != LDAP_SUCCESS ) {
481 send_ldap_result( conn, op, rc,
482 NULL, text, NULL, NULL );
486 conn->c_authz_backend = be;
493 /* deref suffix alias if appropriate */
494 ndn->bv_val = suffix_alias( be, ndn->bv_val );
495 ndn->bv_len = strlen( ndn->bv_val );
497 ret = (*be->be_bind)( be, conn, op,
498 pdn->bv_val, ndn->bv_val,
499 method, &cred, &edn );
502 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
505 conn->c_dn.bv_val = edn;
506 conn->c_dn.bv_len = strlen( edn );
508 conn->c_dn.bv_val = ch_strdup( pdn->bv_val );
509 conn->c_dn.bv_len = pdn->bv_len;
511 conn->c_cdn = pdn->bv_val;
519 if( conn->c_dn.bv_len != 0 ) {
520 ber_len_t max = sockbuf_max_incoming;
521 ber_sockbuf_ctrl( conn->c_sb,
522 LBER_SB_OPT_SET_MAX_INCOMING, &max );
526 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
527 "do_bind: conn %d v%d bind: \"%s\" to \"%s\" \n",
528 conn->c_connid, version, conn->c_cdn, conn->c_dn.bv_val ));
530 Debug( LDAP_DEBUG_TRACE,
531 "do_bind: v%d bind: \"%s\" to \"%s\"\n",
532 version, conn->c_cdn, conn->c_dn.bv_val );
535 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
537 /* send this here to avoid a race condition */
538 send_ldap_result( conn, op, LDAP_SUCCESS,
539 NULL, NULL, NULL, NULL );
541 } else if (edn != NULL) {
546 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
547 NULL, "operation not supported within namingContext",
559 if ( mech != NULL ) {
562 if ( cred.bv_val != NULL ) {