1 /* bind.c - decode an ldap bind operation and pass it to a backend db */
4 * Copyright 1998-2002 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 = { 0, NULL };
42 struct berval ndn = { 0, 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 = dnPrettyNormal( NULL, &dn, &pdn, &ndn );
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 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",
176 pdn.bv_val, mech, NULL );
181 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
182 "do_bind: conn %d version=%ld dn=\"%s\" method=%ld\n",
183 conn->c_connid, (unsigned long) version,
184 pdn.bv_val, (unsigned long)method ));
186 Debug( LDAP_DEBUG_TRACE,
187 "do_bind: version=%ld dn=\"%s\" method=%ld\n",
188 (unsigned long) version,
189 pdn.bv_val, (unsigned long) method );
193 Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d BIND dn=\"%s\" method=%ld\n",
194 op->o_connid, op->o_opid, pdn.bv_val, (unsigned long) method, 0 );
196 if ( version < LDAP_VERSION_MIN || version > LDAP_VERSION_MAX ) {
198 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
199 "do_bind: conn %d unknown version = %ld\n",
200 conn->c_connid, (unsigned long)version ));
202 Debug( LDAP_DEBUG_ANY, "do_bind: unknown version=%ld\n",
203 (unsigned long) version, 0, 0 );
205 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
206 NULL, "requested protocol version not supported", NULL, NULL );
209 } else if (!( global_allows & SLAP_ALLOW_BIND_V2 ) &&
210 version < LDAP_VERSION3 )
212 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
213 NULL, "requested protocol version not allowed", NULL, NULL );
217 /* we set connection version regardless of whether bind succeeds
220 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
221 conn->c_protocol = version;
222 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
224 if ( method == LDAP_AUTH_SASL ) {
228 if ( version < LDAP_VERSION3 ) {
230 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
231 "do_bind: conn %d sasl with LDAPv%ld\n",
232 conn->c_connid, (unsigned long)version ));
234 Debug( LDAP_DEBUG_ANY, "do_bind: sasl with LDAPv%ld\n",
235 (unsigned long) version, 0, 0 );
237 send_ldap_disconnect( conn, op,
238 LDAP_PROTOCOL_ERROR, "SASL bind requires LDAPv3" );
239 rc = SLAPD_DISCONNECT;
243 if( mech == NULL || *mech == '\0' ) {
245 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
246 "do_bind: conn %d no SASL mechanism provided\n",
249 Debug( LDAP_DEBUG_ANY,
250 "do_bind: no sasl mechanism provided\n",
253 send_ldap_result( conn, op, rc = LDAP_AUTH_METHOD_NOT_SUPPORTED,
254 NULL, "no SASL mechanism provided", NULL, NULL );
258 /* check restrictions */
259 rc = backend_check_restrictions( NULL, conn, op, mech, &text );
260 if( rc != LDAP_SUCCESS ) {
261 send_ldap_result( conn, op, rc,
262 NULL, text, NULL, NULL );
266 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
267 if ( conn->c_sasl_bind_in_progress ) {
268 if((strcmp(conn->c_sasl_bind_mech, mech) != 0)) {
269 /* mechanism changed between bind steps */
270 slap_sasl_reset(conn);
273 conn->c_sasl_bind_mech = mech;
276 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
279 rc = slap_sasl_bind( conn, op,
283 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
284 if( rc == LDAP_SUCCESS ) {
285 conn->c_dn.bv_val = edn;
287 conn->c_dn.bv_len = strlen( edn );
288 dnNormalize2( NULL, &conn->c_dn, &conn->c_ndn );
290 conn->c_authmech = conn->c_sasl_bind_mech;
291 conn->c_sasl_bind_mech = NULL;
292 conn->c_sasl_bind_in_progress = 0;
294 conn->c_sasl_ssf = ssf;
295 if( ssf > conn->c_ssf ) {
299 if( conn->c_dn.bv_len != 0 ) {
300 ber_len_t max = sockbuf_max_incoming;
301 ber_sockbuf_ctrl( conn->c_sb,
302 LBER_SB_OPT_SET_MAX_INCOMING, &max );
305 } else if ( rc == LDAP_SASL_BIND_IN_PROGRESS ) {
306 conn->c_sasl_bind_in_progress = 1;
309 if ( conn->c_sasl_bind_mech ) {
310 free( conn->c_sasl_bind_mech );
311 conn->c_sasl_bind_mech = NULL;
313 conn->c_sasl_bind_in_progress = 0;
315 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
320 /* Not SASL, cancel any in-progress bind */
321 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
323 if ( conn->c_sasl_bind_mech != NULL ) {
324 free(conn->c_sasl_bind_mech);
325 conn->c_sasl_bind_mech = NULL;
327 conn->c_sasl_bind_in_progress = 0;
329 slap_sasl_reset( conn );
330 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
333 if ( method == LDAP_AUTH_SIMPLE ) {
334 /* accept "anonymous" binds */
335 if ( cred.bv_len == 0 || ndn.bv_len == 0 ) {
340 !( global_allows & SLAP_ALLOW_BIND_ANON_CRED ))
342 /* cred is not empty, disallow */
343 rc = LDAP_INVALID_CREDENTIALS;
345 } else if ( ndn.bv_len &&
346 !( global_allows & SLAP_ALLOW_BIND_ANON_DN ))
348 /* DN is not empty, disallow */
349 rc = LDAP_UNWILLING_TO_PERFORM;
350 text = "unwilling to allow anonymous bind with non-empty DN";
352 } else if ( global_disallows & SLAP_DISALLOW_BIND_ANON ) {
354 rc = LDAP_INAPPROPRIATE_AUTH;
355 text = "anonymous bind disallowed";
358 rc = backend_check_restrictions( NULL, conn, op, mech, &text );
362 * we already forced connection to "anonymous",
363 * just need to send success
365 send_ldap_result( conn, op, rc,
366 NULL, text, NULL, NULL );
368 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
369 "do_bind: conn %d v%d anonymous bind\n",
370 conn->c_connid, version ));
372 Debug( LDAP_DEBUG_TRACE, "do_bind: v%d anonymous bind\n",
377 } else if ( global_disallows & SLAP_DISALLOW_BIND_SIMPLE ) {
378 /* disallow simple authentication */
379 rc = LDAP_UNWILLING_TO_PERFORM;
380 text = "unwilling to perform simple authentication";
382 send_ldap_result( conn, op, rc,
383 NULL, text, NULL, NULL );
385 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
386 "do_bind: conn %d v%d simple bind(%s) disallowed\n",
387 conn->c_connid, version, ndn.bv_val ));
389 Debug( LDAP_DEBUG_TRACE,
390 "do_bind: v%d simple bind(%s) disallowed\n",
391 version, ndn.bv_val, 0 );
396 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
397 } else if ( method == LDAP_AUTH_KRBV41 || method == LDAP_AUTH_KRBV42 ) {
398 if ( global_disallows & SLAP_DISALLOW_BIND_KRBV4 ) {
399 /* disallow simple authentication */
400 rc = LDAP_UNWILLING_TO_PERFORM;
401 text = "unwilling to perform Kerberos V4 bind";
403 send_ldap_result( conn, op, rc,
404 NULL, text, NULL, NULL );
406 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
407 "do_bind: conn %d v%d Kerberos V4 bind\n",
408 conn->c_connid, version ));
410 Debug( LDAP_DEBUG_TRACE, "do_bind: v%d Kerberos V4 bind\n",
418 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
419 text = "unknown authentication method";
421 send_ldap_result( conn, op, rc,
422 NULL, text, NULL, NULL );
424 LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
425 "do_bind: conn %ld v%d unknown authentication method (%ld)\n",
426 conn->c_connid, version, method ));
428 Debug( LDAP_DEBUG_TRACE,
429 "do_bind: v%d unknown authentication method (%ld)\n",
430 version, method, 0 );
436 * We could be serving multiple database backends. Select the
437 * appropriate one, or send a referral to our "referral server"
438 * if we don't hold it.
441 if ( (be = select_backend( &ndn, 0, 0 )) == NULL ) {
442 if ( default_referral ) {
443 BVarray ref = referral_rewrite( default_referral,
444 NULL, &pdn, LDAP_SCOPE_DEFAULT );
446 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
447 NULL, NULL, ref ? ref : default_referral, NULL );
452 /* noSuchObject is not allowed to be returned by bind */
453 send_ldap_result( conn, op, rc = LDAP_INVALID_CREDENTIALS,
454 NULL, NULL, NULL, NULL );
460 /* check restrictions */
461 rc = backend_check_restrictions( be, conn, op, NULL, &text );
462 if( rc != LDAP_SUCCESS ) {
463 send_ldap_result( conn, op, rc,
464 NULL, text, NULL, NULL );
468 conn->c_authz_backend = be;
473 struct berval edn = { 0, NULL };
475 /* deref suffix alias if appropriate */
476 suffix_alias( be, &ndn );
478 ret = (*be->be_bind)( be, conn, op,
479 &pdn, &ndn, method, &cred, &edn );
482 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
487 ber_dupbv( &conn->c_dn, &pdn );
489 conn->c_cdn = pdn.bv_val;
497 if( conn->c_dn.bv_len != 0 ) {
498 ber_len_t max = sockbuf_max_incoming;
499 ber_sockbuf_ctrl( conn->c_sb,
500 LBER_SB_OPT_SET_MAX_INCOMING, &max );
504 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
505 "do_bind: conn %d v%d bind: \"%s\" to \"%s\" \n",
506 conn->c_connid, version, conn->c_cdn, conn->c_dn.bv_val ));
508 Debug( LDAP_DEBUG_TRACE,
509 "do_bind: v%d bind: \"%s\" to \"%s\"\n",
510 version, conn->c_cdn, conn->c_dn.bv_val );
513 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
515 /* send this here to avoid a race condition */
516 send_ldap_result( conn, op, LDAP_SUCCESS,
517 NULL, NULL, NULL, NULL );
519 } else if (edn.bv_val != NULL) {
524 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
525 NULL, "operation not supported within namingContext",
531 if( pdn.bv_val != NULL ) {
534 if( ndn.bv_val != NULL ) {
537 if ( mech != NULL ) {
540 if ( cred.bv_val != NULL ) {