1 /* bind.c - decode an ldap bind operation and pass it to a backend db */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2004 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17 * All rights reserved.
19 * Redistribution and use in source and binary forms are permitted
20 * provided that this notice is preserved and that due credit is given
21 * to the University of Michigan at Ann Arbor. The name of the University
22 * may not be used to endorse or promote products derived from this
23 * software without specific prior written permission. This software
24 * is provided ``as is'' without express or implied warranty.
31 #include <ac/string.h>
32 #include <ac/socket.h>
37 #include "slapi/slapi.h"
46 BerElement *ber = op->o_ber;
49 struct berval mech = BER_BVNULL;
50 struct berval dn = BER_BVNULL;
54 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( &op->o_conn->c_mutex );
60 if ( op->o_conn->c_sasl_bind_in_progress ) {
61 be = op->o_conn->c_authz_backend;
63 if ( op->o_conn->c_dn.bv_len ) {
64 /* log authorization identity demotion */
65 Statslog( LDAP_DEBUG_STATS,
66 "conn=%lu op=%lu BIND anonymous mech=implicit ssf=0\n",
67 op->o_connid, op->o_opid, 0, 0, 0 );
69 connection2anonymous( op->o_conn );
70 if ( op->o_conn->c_sasl_bind_in_progress ) {
71 op->o_conn->c_authz_backend = be;
73 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
74 if ( op->o_dn.bv_val != NULL ) {
75 free( op->o_dn.bv_val );
76 op->o_dn.bv_val = ch_strdup( "" );
79 if ( op->o_ndn.bv_val != NULL ) {
80 free( op->o_ndn.bv_val );
81 op->o_ndn.bv_val = ch_strdup( "" );
86 * Parse the bind request. It looks like this:
88 * BindRequest ::= SEQUENCE {
89 * version INTEGER, -- version
90 * name DistinguishedName, -- dn
91 * authentication CHOICE {
92 * simple [0] OCTET STRING -- passwd
93 * krbv42ldap [1] OCTET STRING
94 * krbv42dsa [2] OCTET STRING
95 * SASL [3] SaslCredentials
99 * SaslCredentials ::= SEQUENCE {
100 * mechanism LDAPString,
101 * credentials OCTET STRING OPTIONAL
105 tag = ber_scanf( ber, "{imt" /*}*/, &version, &dn, &method );
107 if ( tag == LBER_ERROR ) {
108 Debug( LDAP_DEBUG_ANY, "bind: ber_scanf failed\n", 0, 0, 0 );
109 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
110 rs->sr_err = SLAPD_DISCONNECT;
114 op->o_protocol = version;
115 op->orb_method = method;
117 if( op->orb_method != LDAP_AUTH_SASL ) {
118 tag = ber_scanf( ber, /*{*/ "m}", &op->orb_cred );
121 tag = ber_scanf( ber, "{m" /*}*/, &mech );
123 if ( tag != LBER_ERROR ) {
125 tag = ber_peek_tag( ber, &len );
127 if ( tag == LDAP_TAG_LDAPCRED ) {
128 tag = ber_scanf( ber, "m", &op->orb_cred );
130 tag = LDAP_TAG_LDAPCRED;
131 BER_BVZERO( &op->orb_cred );
134 if ( tag != LBER_ERROR ) {
135 tag = ber_scanf( ber, /*{{*/ "}}" );
140 if ( tag == LBER_ERROR ) {
141 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
142 rs->sr_err = SLAPD_DISCONNECT;
146 if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
147 Debug( LDAP_DEBUG_ANY, "do_bind: get_ctrls failed\n", 0, 0, 0 );
151 /* We use the tmpmemctx here because it speeds up normalization.
152 * However, we must dup with regular malloc when storing any
153 * resulting DNs in the op or conn structures.
155 rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn,
157 if ( rs->sr_err != LDAP_SUCCESS ) {
158 Debug( LDAP_DEBUG_ANY, "bind: invalid dn (%s)\n",
160 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
164 if( op->orb_method == LDAP_AUTH_SASL ) {
165 Debug( LDAP_DEBUG_TRACE, "do_sasl_bind: dn (%s) mech %s\n",
166 op->o_req_dn.bv_val, mech.bv_val, NULL );
169 Debug( LDAP_DEBUG_TRACE,
170 "do_bind: version=%ld dn=\"%s\" method=%ld\n",
171 (unsigned long) version, op->o_req_dn.bv_val,
172 (unsigned long) op->orb_method );
175 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu BIND dn=\"%s\" method=%ld\n",
176 op->o_connid, op->o_opid, op->o_req_dn.bv_val,
177 (unsigned long) op->orb_method, 0 );
179 if ( version < LDAP_VERSION_MIN || version > LDAP_VERSION_MAX ) {
180 Debug( LDAP_DEBUG_ANY, "do_bind: unknown version=%ld\n",
181 (unsigned long) version, 0, 0 );
182 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
183 "requested protocol version not supported" );
186 } else if (!( global_allows & SLAP_ALLOW_BIND_V2 ) &&
187 version < LDAP_VERSION3 )
189 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
190 "historical protocol version requested, use LDAPv3 instead" );
195 * we set connection version regardless of whether bind succeeds or not.
197 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
198 op->o_conn->c_protocol = version;
199 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
201 op->orb_tmp_mech = mech;
203 op->o_bd = frontendDB;
204 rs->sr_err = frontendDB->be_bind( op, rs );
207 if ( rs->sr_err == LDAP_SUCCESS ) {
208 if ( op->orb_method != LDAP_AUTH_SASL ) {
209 ber_dupbv( &op->o_conn->c_authmech, &mech );
211 op->o_conn->c_authtype = op->orb_method;
214 op->o_conn->c_sasl_bindop = NULL;
216 if( op->o_req_dn.bv_val != NULL ) {
217 slap_sl_free( op->o_req_dn.bv_val, op->o_tmpmemctx );
218 BER_BVZERO( &op->o_req_dn );
220 if( op->o_req_ndn.bv_val != NULL ) {
221 slap_sl_free( op->o_req_ndn.bv_val, op->o_tmpmemctx );
222 BER_BVZERO( &op->o_req_ndn );
229 fe_op_bind( Operation *op, SlapReply *rs )
231 struct berval mech = op->orb_tmp_mech;
233 /* check for inappropriate controls */
234 if( get_manageDSAit( op ) == SLAP_CRITICAL_CONTROL ) {
235 send_ldap_error( op, rs,
236 LDAP_UNAVAILABLE_CRITICAL_EXTENSION,
237 "manageDSAit control inappropriate" );
241 /* Set the bindop for the benefit of in-directory SASL lookups */
242 op->o_conn->c_sasl_bindop = op;
244 if ( op->orb_method == LDAP_AUTH_SASL ) {
245 if ( op->o_protocol < LDAP_VERSION3 ) {
246 Debug( LDAP_DEBUG_ANY, "do_bind: sasl with LDAPv%ld\n",
247 (unsigned long)op->o_protocol, 0, 0 );
248 send_ldap_discon( op, rs,
249 LDAP_PROTOCOL_ERROR, "SASL bind requires LDAPv3" );
250 rs->sr_err = SLAPD_DISCONNECT;
254 if( mech.bv_len == 0 ) {
255 Debug( LDAP_DEBUG_ANY,
256 "do_bind: no sasl mechanism provided\n",
258 send_ldap_error( op, rs, LDAP_AUTH_METHOD_NOT_SUPPORTED,
259 "no SASL mechanism provided" );
263 /* check restrictions */
264 if( backend_check_restrictions( op, rs, &mech ) != LDAP_SUCCESS ) {
265 send_ldap_result( op, rs );
269 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
270 if ( op->o_conn->c_sasl_bind_in_progress ) {
271 if( !bvmatch( &op->o_conn->c_sasl_bind_mech, &mech ) ) {
272 /* mechanism changed between bind steps */
273 slap_sasl_reset(op->o_conn);
276 ber_dupbv(&op->o_conn->c_sasl_bind_mech, &mech);
278 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
280 rs->sr_err = slap_sasl_bind( op, rs );
282 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
283 if( rs->sr_err == LDAP_SUCCESS ) {
284 ber_dupbv(&op->o_conn->c_dn, &op->orb_edn);
285 if( op->orb_edn.bv_len != 0 ) {
286 /* edn is always normalized already */
287 ber_dupbv( &op->o_conn->c_ndn, &op->o_conn->c_dn );
289 op->o_tmpfree( op->orb_edn.bv_val, op->o_tmpmemctx );
290 BER_BVZERO( &op->orb_edn );
291 op->o_conn->c_authmech = op->o_conn->c_sasl_bind_mech;
292 BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
293 op->o_conn->c_sasl_bind_in_progress = 0;
295 op->o_conn->c_sasl_ssf = op->orb_ssf;
296 if( op->orb_ssf > op->o_conn->c_ssf ) {
297 op->o_conn->c_ssf = op->orb_ssf;
300 if( op->o_conn->c_dn.bv_len != 0 ) {
301 ber_len_t max = sockbuf_max_incoming_auth;
302 ber_sockbuf_ctrl( op->o_conn->c_sb,
303 LBER_SB_OPT_SET_MAX_INCOMING, &max );
306 /* log authorization identity */
307 Statslog( LDAP_DEBUG_STATS,
308 "conn=%lu op=%lu BIND dn=\"%s\" mech=%s ssf=%d\n",
309 op->o_connid, op->o_opid,
310 op->o_conn->c_dn.bv_val ? op->o_conn->c_dn.bv_val : "<empty>",
311 op->o_conn->c_authmech.bv_val, op->orb_ssf );
313 Debug( LDAP_DEBUG_TRACE,
314 "do_bind: SASL/%s bind: dn=\"%s\" ssf=%d\n",
315 op->o_conn->c_authmech.bv_val,
316 op->o_conn->c_dn.bv_val ? op->o_conn->c_dn.bv_val : "<empty>",
319 } else if ( rs->sr_err == LDAP_SASL_BIND_IN_PROGRESS ) {
320 op->o_conn->c_sasl_bind_in_progress = 1;
323 if ( op->o_conn->c_sasl_bind_mech.bv_val ) {
324 free( op->o_conn->c_sasl_bind_mech.bv_val );
325 BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
327 op->o_conn->c_sasl_bind_in_progress = 0;
333 * Normally post-operation plugins are called only after the
334 * backend operation. Because the front-end performs SASL
335 * binds on behalf of the backend, we'll make a special
336 * exception to call the post-operation plugins after a
340 slapi_int_pblock_set_operation( pb, op );
341 slapi_pblock_set( pb, SLAPI_BIND_TARGET, (void *)op->o_req_dn.bv_val );
342 slapi_pblock_set( pb, SLAPI_BIND_METHOD, (void *)op->orb_method );
343 slapi_pblock_set( pb,
344 SLAPI_BIND_CREDENTIALS, (void *)&op->orb_cred );
345 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)(0) );
346 (void) slapi_int_call_plugins( op->o_bd,
347 SLAPI_PLUGIN_POST_BIND_FN, pb );
349 #endif /* LDAP_SLAPI */
351 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
356 /* Not SASL, cancel any in-progress bind */
357 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
359 if ( op->o_conn->c_sasl_bind_mech.bv_val != NULL ) {
360 free(op->o_conn->c_sasl_bind_mech.bv_val);
361 BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
363 op->o_conn->c_sasl_bind_in_progress = 0;
365 slap_sasl_reset( op->o_conn );
366 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
369 if ( op->orb_method == LDAP_AUTH_SIMPLE ) {
370 ber_str2bv( "SIMPLE", sizeof("SIMPLE")-1, 0, &mech );
371 /* accept "anonymous" binds */
372 if ( op->orb_cred.bv_len == 0 || op->o_req_ndn.bv_len == 0 ) {
373 rs->sr_err = LDAP_SUCCESS;
375 if( op->orb_cred.bv_len &&
376 !( global_allows & SLAP_ALLOW_BIND_ANON_CRED ))
378 /* cred is not empty, disallow */
379 rs->sr_err = LDAP_INVALID_CREDENTIALS;
381 } else if ( op->o_req_ndn.bv_len &&
382 !( global_allows & SLAP_ALLOW_BIND_ANON_DN ))
384 /* DN is not empty, disallow */
385 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
387 "unauthenticated bind (DN with no password) disallowed";
389 } else if ( global_disallows & SLAP_DISALLOW_BIND_ANON ) {
391 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
392 rs->sr_text = "anonymous bind disallowed";
395 backend_check_restrictions( op, rs, &mech );
399 * we already forced connection to "anonymous",
400 * just need to send success
402 send_ldap_result( op, rs );
403 Debug( LDAP_DEBUG_TRACE, "do_bind: v%d anonymous bind\n",
404 op->o_protocol, 0, 0 );
407 } else if ( global_disallows & SLAP_DISALLOW_BIND_SIMPLE ) {
408 /* disallow simple authentication */
409 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
410 rs->sr_text = "unwilling to perform simple authentication";
412 send_ldap_result( op, rs );
413 Debug( LDAP_DEBUG_TRACE,
414 "do_bind: v%d simple bind(%s) disallowed\n",
415 op->o_protocol, op->o_req_ndn.bv_val, 0 );
419 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
420 } else if ( op->orb_method == LDAP_AUTH_KRBV41 ) {
421 if ( global_disallows & SLAP_DISALLOW_BIND_KRBV4 ) {
422 /* disallow krbv4 authentication */
423 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
424 rs->sr_text = "unwilling to perform Kerberos V4 bind";
426 send_ldap_result( op, rs );
428 Debug( LDAP_DEBUG_TRACE,
429 "do_bind: v%d Kerberos V4 (step 1) bind refused\n",
430 op->o_protocol, 0, 0 );
433 ber_str2bv( "KRBV4", sizeof("KRBV4")-1, 0, &mech );
435 } else if ( op->orb_method == LDAP_AUTH_KRBV42 ) {
436 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
437 rs->sr_text = "Kerberos V4 (step 2) bind not supported";
438 send_ldap_result( op, rs );
440 Debug( LDAP_DEBUG_TRACE,
441 "do_bind: v%d Kerberos V4 (step 2) bind refused\n",
442 op->o_protocol, 0, 0 );
447 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
448 rs->sr_text = "unknown authentication method";
450 send_ldap_result( op, rs );
451 Debug( LDAP_DEBUG_TRACE,
452 "do_bind: v%d unknown authentication method (%ld)\n",
453 op->o_protocol, op->orb_method, 0 );
458 * We could be serving multiple database backends. Select the
459 * appropriate one, or send a referral to our "referral server"
460 * if we don't hold it.
463 if ( (op->o_bd = select_backend( &op->o_req_ndn, 0, 0 )) == NULL ) {
464 /* don't return referral for bind requests */
465 /* noSuchObject is not allowed to be returned by bind */
466 rs->sr_err = LDAP_INVALID_CREDENTIALS;
467 send_ldap_result( op, rs );
471 /* check restrictions */
472 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
473 send_ldap_result( op, rs );
480 slapi_int_pblock_set_operation( pb, op );
481 slapi_pblock_set( pb, SLAPI_BIND_TARGET, (void *)op->o_req_dn.bv_val );
482 slapi_pblock_set( pb, SLAPI_BIND_METHOD, (void *)op->orb_method );
483 slapi_pblock_set( pb, SLAPI_BIND_CREDENTIALS, (void *)&op->orb_cred );
484 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)(0) );
485 slapi_pblock_set( pb, SLAPI_CONN_DN, (void *)(0) );
487 rc = slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_PRE_BIND_FN, pb );
489 Debug(LDAP_DEBUG_TRACE,
490 "do_bind: Bind preoperation plugin returned %d.\n",
494 case SLAPI_BIND_SUCCESS:
495 /* Continue with backend processing */
497 case SLAPI_BIND_FAIL:
498 /* Failure, server sends result */
499 rs->sr_err = LDAP_INVALID_CREDENTIALS;
500 send_ldap_result( op, rs );
503 case SLAPI_BIND_ANONYMOUS:
504 /* SLAPI_BIND_ANONYMOUS is undocumented XXX */
506 /* Authoritative, plugin sent result, or no plugins called. */
507 if ( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE,
508 (void *)&rs->sr_err) != 0 )
510 rs->sr_err = LDAP_OTHER;
513 BER_BVZERO( &op->orb_edn );
515 if ( rs->sr_err == LDAP_SUCCESS ) {
516 slapi_pblock_get( pb, SLAPI_CONN_DN,
517 (void *)&op->orb_edn.bv_val );
518 if ( BER_BVISNULL( &op->orb_edn ) ) {
520 /* No plugins were called; continue. */
524 op->orb_edn.bv_len = strlen( op->orb_edn.bv_val );
526 rs->sr_err = dnPrettyNormal( NULL, &op->orb_edn,
527 &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
528 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
529 ber_dupbv(&op->o_conn->c_dn, &op->o_req_dn);
530 ber_dupbv(&op->o_conn->c_ndn, &op->o_req_ndn);
531 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
532 BER_BVZERO( &op->o_req_dn );
533 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
534 BER_BVZERO( &op->o_req_ndn );
535 if ( op->o_conn->c_dn.bv_len != 0 ) {
536 ber_len_t max = sockbuf_max_incoming_auth;
537 ber_sockbuf_ctrl( op->o_conn->c_sb,
538 LBER_SB_OPT_SET_MAX_INCOMING, &max );
540 /* log authorization identity */
541 Statslog( LDAP_DEBUG_STATS,
542 "conn=%lu op=%lu BIND dn=\"%s\" mech=%s (SLAPI) ssf=0\n",
543 op->o_connid, op->o_opid,
544 op->o_conn->c_dn.bv_val
545 ? op->o_conn->c_dn.bv_val : "<empty>",
547 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
553 #endif /* LDAP_SLAPI */
555 if( op->o_bd->be_bind ) {
556 rs->sr_err = (op->o_bd->be_bind)( op, rs );
558 if ( rs->sr_err == 0 ) {
559 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
561 if( op->o_conn->c_authz_backend == NULL ) {
562 op->o_conn->c_authz_backend = op->o_bd;
565 /* be_bind returns regular/global edn */
566 if( op->orb_edn.bv_len ) {
567 op->o_conn->c_dn = op->orb_edn;
569 ber_dupbv(&op->o_conn->c_dn, &op->o_req_dn);
572 ber_dupbv( &op->o_conn->c_ndn, &op->o_req_ndn );
574 if( op->o_conn->c_dn.bv_len != 0 ) {
575 ber_len_t max = sockbuf_max_incoming_auth;
576 ber_sockbuf_ctrl( op->o_conn->c_sb,
577 LBER_SB_OPT_SET_MAX_INCOMING, &max );
580 /* log authorization identity */
581 Statslog( LDAP_DEBUG_STATS,
582 "conn=%lu op=%lu BIND dn=\"%s\" mech=%s ssf=0\n",
583 op->o_connid, op->o_opid,
584 op->o_conn->c_dn.bv_val, mech.bv_val, 0 );
586 Debug( LDAP_DEBUG_TRACE,
587 "do_bind: v%d bind: \"%s\" to \"%s\"\n",
588 op->o_protocol, op->o_req_dn.bv_val, op->o_conn->c_dn.bv_val );
590 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
592 /* send this here to avoid a race condition */
593 send_ldap_result( op, rs );
595 } else if (op->orb_edn.bv_val != NULL) {
596 free( op->orb_edn.bv_val );
600 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
601 "operation not supported within naming context" );
606 slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_POST_BIND_FN, pb ) < 0 )
608 Debug(LDAP_DEBUG_TRACE,
609 "do_bind: Bind postoperation plugins failed.\n",
612 #endif /* LDAP_SLAPI */