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;
39 struct berval mech = { 0, NULL };
40 struct berval dn = { 0, NULL };
41 struct berval pdn = { 0, NULL };
42 struct berval ndn = { 0, NULL };
43 struct berval edn = { 0, NULL };
45 int rc = LDAP_SUCCESS;
47 struct berval cred = { 0, NULL };
51 LDAP_LOG( OPERATION, ENTRY, "do_bind: conn %d\n", conn->c_connid, 0, 0 );
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 if ( conn->c_sasl_bind_in_progress ) be = conn->c_authz_backend;
61 connection2anonymous( conn );
62 if ( conn->c_sasl_bind_in_progress ) conn->c_authz_backend = be;
63 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
65 if ( op->o_dn.bv_val != NULL ) {
66 free( op->o_dn.bv_val );
67 op->o_dn.bv_val = ch_strdup( "" );
71 if ( op->o_ndn.bv_val != NULL ) {
72 free( op->o_ndn.bv_val );
73 op->o_ndn.bv_val = ch_strdup( "" );
78 * Parse the bind request. It looks like this:
80 * BindRequest ::= SEQUENCE {
81 * version INTEGER, -- version
82 * name DistinguishedName, -- dn
83 * authentication CHOICE {
84 * simple [0] OCTET STRING -- passwd
85 * krbv42ldap [1] OCTET STRING
86 * krbv42dsa [2] OCTET STRING
87 * SASL [3] SaslCredentials
91 * SaslCredentials ::= SEQUENCE {
92 * mechanism LDAPString,
93 * credentials OCTET STRING OPTIONAL
97 tag = ber_scanf( ber, "{imt" /*}*/, &version, &dn, &method );
99 if ( tag == LBER_ERROR ) {
101 LDAP_LOG( OPERATION, ERR,
102 "do_bind: conn %d ber_scanf failed\n", conn->c_connid, 0, 0 );
104 Debug( LDAP_DEBUG_ANY, "bind: ber_scanf failed\n", 0, 0, 0 );
106 send_ldap_disconnect( conn, op,
107 LDAP_PROTOCOL_ERROR, "decoding error" );
112 op->o_protocol = version;
114 if( method != LDAP_AUTH_SASL ) {
115 tag = ber_scanf( ber, /*{*/ "m}", &cred );
118 tag = ber_scanf( ber, "{o" /*}*/, &mech );
120 if ( tag != LBER_ERROR ) {
122 tag = ber_peek_tag( ber, &len );
124 if ( tag == LDAP_TAG_LDAPCRED ) {
125 tag = ber_scanf( ber, "m", &cred );
127 tag = LDAP_TAG_LDAPCRED;
132 if ( tag != LBER_ERROR ) {
133 tag = ber_scanf( ber, /*{{*/ "}}" );
138 if ( tag == LBER_ERROR ) {
139 send_ldap_disconnect( conn, op,
142 rc = SLAPD_DISCONNECT;
146 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
148 LDAP_LOG( OPERATION, INFO,
149 "do_bind: conn %d get_ctrls failed\n", conn->c_connid, 0, 0 );
151 Debug( LDAP_DEBUG_ANY, "do_bind: get_ctrls failed\n", 0, 0, 0 );
156 rc = dnPrettyNormal( NULL, &dn, &pdn, &ndn );
157 if ( rc != LDAP_SUCCESS ) {
159 LDAP_LOG( OPERATION, INFO,
160 "do_bind: conn %d invalid dn (%s)\n",
161 conn->c_connid, dn.bv_val, 0 );
163 Debug( LDAP_DEBUG_ANY, "bind: invalid dn (%s)\n",
166 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
167 "invalid DN", NULL, NULL );
171 if( method == LDAP_AUTH_SASL ) {
173 LDAP_LOG( OPERATION, DETAIL1,
174 "do_sasl_bind: conn %d dn (%s) mech %s\n",
175 conn->c_connid, pdn.bv_val, mech.bv_val );
177 Debug( LDAP_DEBUG_TRACE, "do_sasl_bind: dn (%s) mech %s\n",
178 pdn.bv_val, mech.bv_val, NULL );
183 LDAP_LOG( OPERATION, DETAIL1,
184 "do_bind: version=%ld dn=\"%s\" method=%ld\n",
185 (unsigned long) version, pdn.bv_val, (unsigned long)method );
187 Debug( LDAP_DEBUG_TRACE,
188 "do_bind: version=%ld dn=\"%s\" method=%ld\n",
189 (unsigned long) version,
190 pdn.bv_val, (unsigned long) method );
194 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu BIND dn=\"%s\" method=%ld\n",
195 op->o_connid, op->o_opid, pdn.bv_val, (unsigned long) method, 0 );
197 if ( version < LDAP_VERSION_MIN || version > LDAP_VERSION_MAX ) {
199 LDAP_LOG( OPERATION, INFO,
200 "do_bind: conn %d unknown version = %ld\n",
201 conn->c_connid, (unsigned long)version, 0 );
203 Debug( LDAP_DEBUG_ANY, "do_bind: unknown version=%ld\n",
204 (unsigned long) version, 0, 0 );
206 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
207 NULL, "requested protocol version not supported", NULL, NULL );
210 } else if (!( global_allows & SLAP_ALLOW_BIND_V2 ) &&
211 version < LDAP_VERSION3 )
213 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
214 NULL, "requested protocol version not allowed", NULL, NULL );
218 /* we set connection version regardless of whether bind succeeds
221 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
222 conn->c_protocol = version;
223 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
225 /* check for inappropriate controls */
226 if( get_manageDSAit( op ) == SLAP_CRITICAL_CONTROL ) {
227 send_ldap_result( conn, op,
228 rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION,
229 NULL, "manageDSAit control inappropriate",
234 /* Set the bindop for the benefit of in-directory SASL lookups */
235 ldap_pvt_thread_mutex_lock( &conn->c_sasl_bindmutex );
236 conn->c_sasl_bindop = op;
238 if ( method == LDAP_AUTH_SASL ) {
241 if ( version < LDAP_VERSION3 ) {
243 LDAP_LOG( OPERATION, INFO,
244 "do_bind: conn %d sasl with LDAPv%ld\n",
245 conn->c_connid, (unsigned long)version , 0 );
247 Debug( LDAP_DEBUG_ANY, "do_bind: sasl with LDAPv%ld\n",
248 (unsigned long) version, 0, 0 );
250 send_ldap_disconnect( conn, op,
251 LDAP_PROTOCOL_ERROR, "SASL bind requires LDAPv3" );
252 rc = SLAPD_DISCONNECT;
256 if( mech.bv_len == 0 ) {
258 LDAP_LOG( OPERATION, INFO,
259 "do_bind: conn %d no SASL mechanism provided\n",
260 conn->c_connid, 0, 0 );
262 Debug( LDAP_DEBUG_ANY,
263 "do_bind: no sasl mechanism provided\n",
266 send_ldap_result( conn, op, rc = LDAP_AUTH_METHOD_NOT_SUPPORTED,
267 NULL, "no SASL mechanism provided", NULL, NULL );
271 /* check restrictions */
272 rc = backend_check_restrictions( NULL, conn, op, &mech, &text );
273 if( rc != LDAP_SUCCESS ) {
274 send_ldap_result( conn, op, rc,
275 NULL, text, NULL, NULL );
279 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
280 if ( conn->c_sasl_bind_in_progress ) {
281 if((ber_bvcmp(&conn->c_sasl_bind_mech, &mech) != 0)) {
282 /* mechanism changed between bind steps */
283 slap_sasl_reset(conn);
286 conn->c_sasl_bind_mech = mech;
290 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
292 rc = slap_sasl_bind( conn, op,
296 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
297 if( rc == LDAP_SUCCESS ) {
299 if( edn.bv_len != 0 ) {
300 /* edn is always normalized already */
301 ber_dupbv( &conn->c_ndn, &conn->c_dn );
303 conn->c_authmech = conn->c_sasl_bind_mech;
304 conn->c_sasl_bind_mech.bv_val = NULL;
305 conn->c_sasl_bind_mech.bv_len = 0;
306 conn->c_sasl_bind_in_progress = 0;
308 conn->c_sasl_ssf = ssf;
309 if( ssf > conn->c_ssf ) {
313 if( conn->c_dn.bv_len != 0 ) {
314 ber_len_t max = sockbuf_max_incoming;
315 ber_sockbuf_ctrl( conn->c_sb,
316 LBER_SB_OPT_SET_MAX_INCOMING, &max );
320 LDAP_LOG( OPERATION, DETAIL1,
321 "do_bind: SASL/%s bind: dn=\"%s\" ssf=%d\n",
322 conn->c_authmech.bv_val, conn->c_dn.bv_val, ssf );
324 Debug( LDAP_DEBUG_TRACE,
325 "do_bind: SASL/%s bind: dn=\"%s\" ssf=%d\n",
326 conn->c_authmech.bv_val, conn->c_dn.bv_val, ssf );
329 } else if ( rc == LDAP_SASL_BIND_IN_PROGRESS ) {
330 conn->c_sasl_bind_in_progress = 1;
333 if ( conn->c_sasl_bind_mech.bv_val ) {
334 free( conn->c_sasl_bind_mech.bv_val );
335 conn->c_sasl_bind_mech.bv_val = NULL;
336 conn->c_sasl_bind_mech.bv_len = 0;
338 conn->c_sasl_bind_in_progress = 0;
340 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
345 /* Not SASL, cancel any in-progress bind */
346 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
348 if ( conn->c_sasl_bind_mech.bv_val != NULL ) {
349 free(conn->c_sasl_bind_mech.bv_val);
350 conn->c_sasl_bind_mech.bv_val = NULL;
351 conn->c_sasl_bind_mech.bv_len = 0;
353 conn->c_sasl_bind_in_progress = 0;
355 slap_sasl_reset( conn );
356 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
359 if ( method == LDAP_AUTH_SIMPLE ) {
360 /* accept "anonymous" binds */
361 if ( cred.bv_len == 0 || ndn.bv_len == 0 ) {
366 !( global_allows & SLAP_ALLOW_BIND_ANON_CRED ))
368 /* cred is not empty, disallow */
369 rc = LDAP_INVALID_CREDENTIALS;
371 } else if ( ndn.bv_len &&
372 !( global_allows & SLAP_ALLOW_BIND_ANON_DN ))
374 /* DN is not empty, disallow */
375 rc = LDAP_UNWILLING_TO_PERFORM;
376 text = "unwilling to allow anonymous bind with non-empty DN";
378 } else if ( global_disallows & SLAP_DISALLOW_BIND_ANON ) {
380 rc = LDAP_INAPPROPRIATE_AUTH;
381 text = "anonymous bind disallowed";
384 rc = backend_check_restrictions( NULL, conn, op,
389 * we already forced connection to "anonymous",
390 * just need to send success
392 send_ldap_result( conn, op, rc,
393 NULL, text, NULL, NULL );
395 LDAP_LOG( OPERATION, DETAIL1,
396 "do_bind: conn %d v%d anonymous bind\n",
397 conn->c_connid, version , 0 );
399 Debug( LDAP_DEBUG_TRACE, "do_bind: v%d anonymous bind\n",
404 } else if ( global_disallows & SLAP_DISALLOW_BIND_SIMPLE ) {
405 /* disallow simple authentication */
406 rc = LDAP_UNWILLING_TO_PERFORM;
407 text = "unwilling to perform simple authentication";
409 send_ldap_result( conn, op, rc,
410 NULL, text, NULL, NULL );
412 LDAP_LOG( OPERATION, INFO,
413 "do_bind: conn %d v%d simple bind(%s) disallowed\n",
414 conn->c_connid, version, ndn.bv_val );
416 Debug( LDAP_DEBUG_TRACE,
417 "do_bind: v%d simple bind(%s) disallowed\n",
418 version, ndn.bv_val, 0 );
422 } else if (( global_disallows & SLAP_DISALLOW_BIND_SIMPLE_UNPROTECTED )
423 && ( op->o_ssf < global_ssf_set.sss_ssf ))
425 rc = LDAP_CONFIDENTIALITY_REQUIRED;
426 text = "unwilling to perform simple authentication "
427 "without confidentilty protection";
429 send_ldap_result( conn, op, rc,
430 NULL, text, NULL, NULL );
433 LDAP_LOG( OPERATION, INFO, "do_bind: conn %d "
434 "v%d unprotected simple bind(%s) disallowed\n",
435 conn->c_connid, version, ndn.bv_val );
437 Debug( LDAP_DEBUG_TRACE,
438 "do_bind: v%d unprotected simple bind(%s) disallowed\n",
439 version, ndn.bv_val, 0 );
444 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
445 } else if ( method == LDAP_AUTH_KRBV41 || method == LDAP_AUTH_KRBV42 ) {
446 if ( global_disallows & SLAP_DISALLOW_BIND_KRBV4 ) {
447 /* disallow simple authentication */
448 rc = LDAP_UNWILLING_TO_PERFORM;
449 text = "unwilling to perform Kerberos V4 bind";
451 send_ldap_result( conn, op, rc,
452 NULL, text, NULL, NULL );
454 LDAP_LOG( OPERATION, DETAIL1,
455 "do_bind: conn %d v%d Kerberos V4 bind\n",
456 conn->c_connid, version , 0 );
458 Debug( LDAP_DEBUG_TRACE, "do_bind: v%d Kerberos V4 bind\n",
466 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
467 text = "unknown authentication method";
469 send_ldap_result( conn, op, rc,
470 NULL, text, NULL, NULL );
472 LDAP_LOG( OPERATION, INFO,
473 "do_bind: conn %ld v%d unknown authentication method (%ld)\n",
474 conn->c_connid, version, method );
476 Debug( LDAP_DEBUG_TRACE,
477 "do_bind: v%d unknown authentication method (%ld)\n",
478 version, method, 0 );
484 * We could be serving multiple database backends. Select the
485 * appropriate one, or send a referral to our "referral server"
486 * if we don't hold it.
489 if ( (be = select_backend( &ndn, 0, 0 )) == NULL ) {
490 if ( default_referral ) {
491 BerVarray ref = referral_rewrite( default_referral,
492 NULL, &pdn, LDAP_SCOPE_DEFAULT );
494 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
495 NULL, NULL, ref ? ref : default_referral, NULL );
497 ber_bvarray_free( ref );
500 /* noSuchObject is not allowed to be returned by bind */
501 send_ldap_result( conn, op, rc = LDAP_INVALID_CREDENTIALS,
502 NULL, NULL, NULL, NULL );
508 /* check restrictions */
509 rc = backend_check_restrictions( be, conn, op, NULL, &text );
510 if( rc != LDAP_SUCCESS ) {
511 send_ldap_result( conn, op, rc,
512 NULL, text, NULL, NULL );
519 /* deref suffix alias if appropriate */
520 suffix_alias( be, &ndn );
522 ret = (*be->be_bind)( be, conn, op,
523 &pdn, &ndn, method, &cred, &edn );
526 ldap_pvt_thread_mutex_lock( &conn->c_mutex );
528 if( conn->c_authz_backend == NULL ) {
529 conn->c_authz_backend = be;
544 if( conn->c_dn.bv_len != 0 ) {
545 ber_len_t max = sockbuf_max_incoming;
546 ber_sockbuf_ctrl( conn->c_sb,
547 LBER_SB_OPT_SET_MAX_INCOMING, &max );
551 LDAP_LOG( OPERATION, DETAIL1,
552 "do_bind: v%d bind: \"%s\" to \"%s\" \n",
553 version, conn->c_dn.bv_val, conn->c_dn.bv_val );
555 Debug( LDAP_DEBUG_TRACE,
556 "do_bind: v%d bind: \"%s\" to \"%s\"\n",
557 version, dn.bv_val, conn->c_dn.bv_val );
560 ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
562 /* send this here to avoid a race condition */
563 send_ldap_result( conn, op, LDAP_SUCCESS,
564 NULL, NULL, NULL, NULL );
566 } else if (edn.bv_val != NULL) {
571 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
572 NULL, "operation not supported within namingContext",
577 conn->c_sasl_bindop = NULL;
578 ldap_pvt_thread_mutex_unlock( &conn->c_sasl_bindmutex );
580 if( pdn.bv_val != NULL ) {
583 if( ndn.bv_val != NULL ) {
586 if ( mech.bv_val != NULL ) {