Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / openldap / dist / servers / slapd / bind.c
blob02277f1be8864d14c36439485c3e5d7b7ecec2ee
1 /* bind.c - decode an ldap bind operation and pass it to a backend db */
2 /* $OpenLDAP: pkg/ldap/servers/slapd/bind.c,v 1.201.2.4 2008/02/11 23:26:43 kurt Exp $ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2008 The OpenLDAP Foundation.
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
10 * Public License.
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.
27 #include "portable.h"
29 #include <stdio.h>
31 #include <ac/string.h>
32 #include <ac/socket.h>
34 #include "slap.h"
36 int
37 do_bind(
38 Operation *op,
39 SlapReply *rs )
41 BerElement *ber = op->o_ber;
42 ber_int_t version;
43 ber_tag_t method;
44 struct berval mech = BER_BVNULL;
45 struct berval dn = BER_BVNULL;
46 ber_tag_t tag;
47 Backend *be = NULL;
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';
75 op->o_dn.bv_len = 0;
77 if ( !BER_BVISNULL( &op->o_ndn ) ) {
78 op->o_ndn.bv_val[0] = '\0';
79 op->o_ndn.bv_len = 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
93 * }
94 * }
96 * SaslCredentials ::= SEQUENCE {
97 * mechanism LDAPString,
98 * credentials OCTET STRING OPTIONAL
99 * }
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;
109 goto cleanup;
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 );
118 } else {
119 tag = ber_scanf( ber, "{m" /*}*/, &mech );
121 if ( tag != LBER_ERROR ) {
122 ber_len_t len;
123 tag = ber_peek_tag( ber, &len );
125 if ( tag == LDAP_TAG_LDAPCRED ) {
126 tag = ber_scanf( ber, "m", &op->orb_cred );
127 } else {
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;
143 goto cleanup;
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 );
149 goto cleanup;
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,
157 op->o_tmpmemctx );
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" );
162 goto cleanup;
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 );
173 } else {
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" );
185 goto cleanup;
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" );
192 goto cleanup;
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 );
202 op->orb_mech = mech;
204 op->o_bd = frontendDB;
205 rs->sr_err = frontendDB->be_bind( op, rs );
207 cleanup:
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 );
224 return rs->sr_err;
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" );
237 goto cleanup;
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;
247 goto cleanup;
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",
253 0, 0, 0 );
254 send_ldap_error( op, rs, LDAP_AUTH_METHOD_NOT_SUPPORTED,
255 "no SASL mechanism provided" );
256 goto cleanup;
259 /* check restrictions */
260 if( backend_check_restrictions( op, rs, &op->orb_mech ) != LDAP_SUCCESS ) {
261 send_ldap_result( op, rs );
262 goto cleanup;
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);
271 } else {
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 );
282 goto cleanup;
284 } else {
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;
315 rs->sr_text =
316 "unauthenticated bind (DN with no password) disallowed";
318 } else if ( global_disallows & SLAP_DISALLOW_BIND_ANON ) {
319 /* disallow */
320 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
321 rs->sr_text = "anonymous bind disallowed";
323 } else {
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 );
334 goto cleanup;
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 );
345 goto cleanup;
348 } else {
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 );
356 goto cleanup;
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;
369 op->o_bd = bd;
370 send_ldap_result( op, rs );
371 goto cleanup;
374 /* check restrictions */
375 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
376 send_ldap_result( op, rs );
377 goto cleanup;
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 );
393 } else {
394 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
395 "operation not supported within naming context" );
398 cleanup:;
399 op->o_bd = bd;
400 return rs->sr_err;
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;
415 } else {
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",
430 op->o_log_prefix,
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 );
442 return LDAP_SUCCESS;