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-2009 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>
41 BerElement *ber = op->o_ber;
44 struct berval mech = BER_BVNULL;
45 struct berval dn = BER_BVNULL;
49 Debug( LDAP_DEBUG_TRACE, "%s do_bind\n",
50 op->o_log_prefix, 0, 0 );
53 * Force the connection to "anonymous" until bind succeeds.
55 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
56 if ( op->o_conn->c_sasl_bind_in_progress ) {
57 be = op->o_conn->c_authz_backend;
59 if ( !BER_BVISEMPTY( &op->o_conn->c_dn ) ) {
60 /* log authorization identity demotion */
61 Statslog( LDAP_DEBUG_STATS,
62 "%s BIND anonymous mech=implicit ssf=0\n",
63 op->o_log_prefix, 0, 0, 0, 0 );
65 connection2anonymous( op->o_conn );
66 if ( op->o_conn->c_sasl_bind_in_progress ) {
67 op->o_conn->c_authz_backend = be;
69 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
70 if ( !BER_BVISNULL( &op->o_dn ) ) {
71 /* NOTE: temporarily wasting few bytes
72 * (until bind is completed), but saving
73 * a couple of ch_free() and ch_strdup("") */
74 op->o_dn.bv_val[0] = '\0';
77 if ( !BER_BVISNULL( &op->o_ndn ) ) {
78 op->o_ndn.bv_val[0] = '\0';
83 * Parse the bind request. It looks like this:
85 * BindRequest ::= SEQUENCE {
86 * version INTEGER, -- version
87 * name DistinguishedName, -- dn
88 * authentication CHOICE {
89 * simple [0] OCTET STRING -- passwd
90 * krbv42ldap [1] OCTET STRING -- OBSOLETE
91 * krbv42dsa [2] OCTET STRING -- OBSOLETE
92 * SASL [3] SaslCredentials
96 * SaslCredentials ::= SEQUENCE {
97 * mechanism LDAPString,
98 * credentials OCTET STRING OPTIONAL
102 tag = ber_scanf( ber, "{imt" /*}*/, &version, &dn, &method );
104 if ( tag == LBER_ERROR ) {
105 Debug( LDAP_DEBUG_ANY, "%s do_bind: ber_scanf failed\n",
106 op->o_log_prefix, 0, 0 );
107 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
108 rs->sr_err = SLAPD_DISCONNECT;
112 op->o_protocol = version;
113 op->orb_method = method;
115 if( op->orb_method != LDAP_AUTH_SASL ) {
116 tag = ber_scanf( ber, /*{*/ "m}", &op->orb_cred );
119 tag = ber_scanf( ber, "{m" /*}*/, &mech );
121 if ( tag != LBER_ERROR ) {
123 tag = ber_peek_tag( ber, &len );
125 if ( tag == LDAP_TAG_LDAPCRED ) {
126 tag = ber_scanf( ber, "m", &op->orb_cred );
128 tag = LDAP_TAG_LDAPCRED;
129 BER_BVZERO( &op->orb_cred );
132 if ( tag != LBER_ERROR ) {
133 tag = ber_scanf( ber, /*{{*/ "}}" );
138 if ( tag == LBER_ERROR ) {
139 Debug( LDAP_DEBUG_ANY, "%s do_bind: ber_scanf failed\n",
140 op->o_log_prefix, 0, 0 );
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, "%s do_bind: get_ctrls failed\n",
148 op->o_log_prefix, 0, 0 );
152 /* We use the tmpmemctx here because it speeds up normalization.
153 * However, we must dup with regular malloc when storing any
154 * resulting DNs in the op or conn structures.
156 rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn,
158 if ( rs->sr_err != LDAP_SUCCESS ) {
159 Debug( LDAP_DEBUG_ANY, "%s do_bind: invalid dn (%s)\n",
160 op->o_log_prefix, dn.bv_val, 0 );
161 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
165 Statslog( LDAP_DEBUG_STATS, "%s BIND dn=\"%s\" method=%ld\n",
166 op->o_log_prefix, op->o_req_dn.bv_val,
167 (unsigned long) op->orb_method, 0, 0 );
169 if( op->orb_method == LDAP_AUTH_SASL ) {
170 Debug( LDAP_DEBUG_TRACE, "do_bind: dn (%s) SASL mech %s\n",
171 op->o_req_dn.bv_val, mech.bv_val, NULL );
174 Debug( LDAP_DEBUG_TRACE,
175 "do_bind: version=%ld dn=\"%s\" method=%ld\n",
176 (unsigned long) version, op->o_req_dn.bv_val,
177 (unsigned long) op->orb_method );
180 if ( version < LDAP_VERSION_MIN || version > LDAP_VERSION_MAX ) {
181 Debug( LDAP_DEBUG_ANY, "%s do_bind: unknown version=%ld\n",
182 op->o_log_prefix, (unsigned long) version, 0 );
183 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
184 "requested protocol version not supported" );
187 } else if (!( global_allows & SLAP_ALLOW_BIND_V2 ) &&
188 version < LDAP_VERSION3 )
190 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
191 "historical protocol version requested, use LDAPv3 instead" );
196 * we set connection version regardless of whether bind succeeds or not.
198 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
199 op->o_conn->c_protocol = version;
200 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
204 op->o_bd = frontendDB;
205 rs->sr_err = frontendDB->be_bind( op, rs );
208 if ( rs->sr_err == LDAP_SUCCESS ) {
209 if ( op->orb_method != LDAP_AUTH_SASL ) {
210 ber_dupbv( &op->o_conn->c_authmech, &mech );
212 op->o_conn->c_authtype = op->orb_method;
215 if( !BER_BVISNULL( &op->o_req_dn ) ) {
216 slap_sl_free( op->o_req_dn.bv_val, op->o_tmpmemctx );
217 BER_BVZERO( &op->o_req_dn );
219 if( !BER_BVISNULL( &op->o_req_ndn ) ) {
220 slap_sl_free( op->o_req_ndn.bv_val, op->o_tmpmemctx );
221 BER_BVZERO( &op->o_req_ndn );
228 fe_op_bind( Operation *op, SlapReply *rs )
230 BackendDB *bd = op->o_bd;
232 /* check for inappropriate controls */
233 if( get_manageDSAit( op ) == SLAP_CONTROL_CRITICAL ) {
234 send_ldap_error( op, rs,
235 LDAP_UNAVAILABLE_CRITICAL_EXTENSION,
236 "manageDSAit control inappropriate" );
240 if ( op->orb_method == LDAP_AUTH_SASL ) {
241 if ( op->o_protocol < LDAP_VERSION3 ) {
242 Debug( LDAP_DEBUG_ANY, "do_bind: sasl with LDAPv%ld\n",
243 (unsigned long)op->o_protocol, 0, 0 );
244 send_ldap_discon( op, rs,
245 LDAP_PROTOCOL_ERROR, "SASL bind requires LDAPv3" );
246 rs->sr_err = SLAPD_DISCONNECT;
250 if( BER_BVISNULL( &op->orb_mech ) || BER_BVISEMPTY( &op->orb_mech ) ) {
251 Debug( LDAP_DEBUG_ANY,
252 "do_bind: no sasl mechanism provided\n",
254 send_ldap_error( op, rs, LDAP_AUTH_METHOD_NOT_SUPPORTED,
255 "no SASL mechanism provided" );
259 /* check restrictions */
260 if( backend_check_restrictions( op, rs, &op->orb_mech ) != LDAP_SUCCESS ) {
261 send_ldap_result( op, rs );
265 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
266 if ( op->o_conn->c_sasl_bind_in_progress ) {
267 if( !bvmatch( &op->o_conn->c_sasl_bind_mech, &op->orb_mech ) ) {
268 /* mechanism changed between bind steps */
269 slap_sasl_reset(op->o_conn);
272 ber_dupbv(&op->o_conn->c_sasl_bind_mech, &op->orb_mech);
275 /* Set the bindop for the benefit of in-directory SASL lookups */
276 op->o_conn->c_sasl_bindop = op;
278 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
280 rs->sr_err = slap_sasl_bind( op, rs );
285 /* Not SASL, cancel any in-progress bind */
286 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
288 if ( !BER_BVISNULL( &op->o_conn->c_sasl_bind_mech ) ) {
289 free( op->o_conn->c_sasl_bind_mech.bv_val );
290 BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
292 op->o_conn->c_sasl_bind_in_progress = 0;
294 slap_sasl_reset( op->o_conn );
295 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
298 if ( op->orb_method == LDAP_AUTH_SIMPLE ) {
299 BER_BVSTR( &op->orb_mech, "SIMPLE" );
300 /* accept "anonymous" binds */
301 if ( BER_BVISEMPTY( &op->orb_cred ) || BER_BVISEMPTY( &op->o_req_ndn ) ) {
302 rs->sr_err = LDAP_SUCCESS;
304 if( !BER_BVISEMPTY( &op->orb_cred ) &&
305 !( global_allows & SLAP_ALLOW_BIND_ANON_CRED ))
307 /* cred is not empty, disallow */
308 rs->sr_err = LDAP_INVALID_CREDENTIALS;
310 } else if ( !BER_BVISEMPTY( &op->o_req_ndn ) &&
311 !( global_allows & SLAP_ALLOW_BIND_ANON_DN ))
313 /* DN is not empty, disallow */
314 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
316 "unauthenticated bind (DN with no password) disallowed";
318 } else if ( global_disallows & SLAP_DISALLOW_BIND_ANON ) {
320 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
321 rs->sr_text = "anonymous bind disallowed";
324 backend_check_restrictions( op, rs, &op->orb_mech );
328 * we already forced connection to "anonymous",
329 * just need to send success
331 send_ldap_result( op, rs );
332 Debug( LDAP_DEBUG_TRACE, "do_bind: v%d anonymous bind\n",
333 op->o_protocol, 0, 0 );
336 } else if ( global_disallows & SLAP_DISALLOW_BIND_SIMPLE ) {
337 /* disallow simple authentication */
338 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
339 rs->sr_text = "unwilling to perform simple authentication";
341 send_ldap_result( op, rs );
342 Debug( LDAP_DEBUG_TRACE,
343 "do_bind: v%d simple bind(%s) disallowed\n",
344 op->o_protocol, op->o_req_ndn.bv_val, 0 );
349 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
350 rs->sr_text = "unknown authentication method";
352 send_ldap_result( op, rs );
353 Debug( LDAP_DEBUG_TRACE,
354 "do_bind: v%d unknown authentication method (%d)\n",
355 op->o_protocol, op->orb_method, 0 );
360 * We could be serving multiple database backends. Select the
361 * appropriate one, or send a referral to our "referral server"
362 * if we don't hold it.
365 if ( (op->o_bd = select_backend( &op->o_req_ndn, 0 )) == NULL ) {
366 /* don't return referral for bind requests */
367 /* noSuchObject is not allowed to be returned by bind */
368 rs->sr_err = LDAP_INVALID_CREDENTIALS;
370 send_ldap_result( op, rs );
374 /* check restrictions */
375 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
376 send_ldap_result( op, rs );
380 if( op->o_bd->be_bind ) {
381 op->o_conn->c_authz_cookie = NULL;
383 rs->sr_err = (op->o_bd->be_bind)( op, rs );
385 if ( rs->sr_err == 0 ) {
386 (void)fe_op_bind_success( op, rs );
388 } else if ( !BER_BVISNULL( &op->orb_edn ) ) {
389 free( op->orb_edn.bv_val );
390 BER_BVZERO( &op->orb_edn );
394 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
395 "operation not supported within naming context" );
404 fe_op_bind_success( Operation *op, SlapReply *rs )
406 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
408 if( op->o_conn->c_authz_backend == NULL ) {
409 op->o_conn->c_authz_backend = op->o_bd;
412 /* be_bind returns regular/global edn */
413 if( !BER_BVISEMPTY( &op->orb_edn ) ) {
414 op->o_conn->c_dn = op->orb_edn;
416 ber_dupbv(&op->o_conn->c_dn, &op->o_req_dn);
419 ber_dupbv( &op->o_conn->c_ndn, &op->o_req_ndn );
421 if( !BER_BVISEMPTY( &op->o_conn->c_dn ) ) {
422 ber_len_t max = sockbuf_max_incoming_auth;
423 ber_sockbuf_ctrl( op->o_conn->c_sb,
424 LBER_SB_OPT_SET_MAX_INCOMING, &max );
427 /* log authorization identity */
428 Statslog( LDAP_DEBUG_STATS,
429 "%s BIND dn=\"%s\" mech=%s ssf=0\n",
431 op->o_conn->c_dn.bv_val, op->orb_mech.bv_val, 0, 0 );
433 Debug( LDAP_DEBUG_TRACE,
434 "do_bind: v%d bind: \"%s\" to \"%s\"\n",
435 op->o_protocol, op->o_req_dn.bv_val, op->o_conn->c_dn.bv_val );
437 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
439 /* send this here to avoid a race condition */
440 send_ldap_result( op, rs );