]> git.sur5r.net Git - openldap/blob - servers/slapd/bind.c
frontend stuff moved into a database structure, essentially to allow overlays to...
[openldap] / servers / slapd / bind.c
1 /* bind.c - decode an ldap bind operation and pass it to a backend db */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2004 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
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.
11  *
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>.
15  */
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17  * All rights reserved.
18  *
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.
25  */
26
27 #include "portable.h"
28
29 #include <stdio.h>
30
31 #include <ac/string.h>
32 #include <ac/socket.h>
33
34 #include "ldap_pvt.h"
35 #include "slap.h"
36 #ifdef LDAP_SLAPI
37 #include "slapi/slapi.h"
38 #endif
39
40
41 int
42 do_bind(
43     Operation   *op,
44     SlapReply   *rs )
45 {
46         BerElement *ber = op->o_ber;
47         ber_int_t version;
48         ber_tag_t method;
49         struct berval mech = BER_BVNULL;
50         struct berval dn = BER_BVNULL;
51         ber_tag_t tag;
52         Backend *be = NULL;
53
54 #ifdef NEW_LOGGING
55         LDAP_LOG( OPERATION, ENTRY, "do_bind: conn %d\n", op->o_connid, 0, 0 );
56 #else
57         Debug( LDAP_DEBUG_TRACE, "do_bind\n", 0, 0, 0 );
58 #endif
59
60         /*
61          * Force to connection to "anonymous" until bind succeeds.
62          */
63         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
64         if ( op->o_conn->c_sasl_bind_in_progress ) {
65                 be = op->o_conn->c_authz_backend;
66         }
67         if ( op->o_conn->c_dn.bv_len ) {
68                 /* log authorization identity demotion */
69                 Statslog( LDAP_DEBUG_STATS,
70                         "conn=%lu op=%lu BIND anonymous mech=implicit ssf=0\n",
71                         op->o_connid, op->o_opid, 0, 0, 0 );
72         }
73         connection2anonymous( op->o_conn );
74         if ( op->o_conn->c_sasl_bind_in_progress ) {
75                 op->o_conn->c_authz_backend = be;
76         }
77         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
78         if ( op->o_dn.bv_val != NULL ) {
79                 free( op->o_dn.bv_val );
80                 op->o_dn.bv_val = ch_strdup( "" );
81                 op->o_dn.bv_len = 0;
82         }
83         if ( op->o_ndn.bv_val != NULL ) {
84                 free( op->o_ndn.bv_val );
85                 op->o_ndn.bv_val = ch_strdup( "" );
86                 op->o_ndn.bv_len = 0;
87         }
88
89         /*
90          * Parse the bind request.  It looks like this:
91          *
92          *      BindRequest ::= SEQUENCE {
93          *              version         INTEGER,                 -- version
94          *              name            DistinguishedName,       -- dn
95          *              authentication  CHOICE {
96          *                      simple          [0] OCTET STRING -- passwd
97          *                      krbv42ldap      [1] OCTET STRING
98          *                      krbv42dsa       [2] OCTET STRING
99          *                      SASL            [3] SaslCredentials
100          *              }
101          *      }
102          *
103          *      SaslCredentials ::= SEQUENCE {
104          *              mechanism           LDAPString,
105          *              credentials         OCTET STRING OPTIONAL
106          *      }
107          */
108
109         tag = ber_scanf( ber, "{imt" /*}*/, &version, &dn, &method );
110
111         if ( tag == LBER_ERROR ) {
112 #ifdef NEW_LOGGING
113                 LDAP_LOG( OPERATION, ERR, 
114                         "do_bind: conn %d  ber_scanf failed\n", op->o_connid, 0, 0 );
115 #else
116                 Debug( LDAP_DEBUG_ANY, "bind: ber_scanf failed\n", 0, 0, 0 );
117 #endif
118                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
119                 rs->sr_err = SLAPD_DISCONNECT;
120                 goto cleanup;
121         }
122
123         op->o_protocol = version;
124         op->orb_method = method;
125
126         if( op->orb_method != LDAP_AUTH_SASL ) {
127                 tag = ber_scanf( ber, /*{*/ "m}", &op->orb_cred );
128
129         } else {
130                 tag = ber_scanf( ber, "{m" /*}*/, &mech );
131
132                 if ( tag != LBER_ERROR ) {
133                         ber_len_t len;
134                         tag = ber_peek_tag( ber, &len );
135
136                         if ( tag == LDAP_TAG_LDAPCRED ) { 
137                                 tag = ber_scanf( ber, "m", &op->orb_cred );
138                         } else {
139                                 tag = LDAP_TAG_LDAPCRED;
140                                 BER_BVZERO( &op->orb_cred );
141                         }
142
143                         if ( tag != LBER_ERROR ) {
144                                 tag = ber_scanf( ber, /*{{*/ "}}" );
145                         }
146                 }
147         }
148
149         if ( tag == LBER_ERROR ) {
150                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
151                 rs->sr_err = SLAPD_DISCONNECT;
152                 goto cleanup;
153         }
154
155         if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
156 #ifdef NEW_LOGGING
157                 LDAP_LOG( OPERATION, INFO, 
158                         "do_bind: conn %d  get_ctrls failed\n", op->o_connid, 0, 0 );
159 #else
160                 Debug( LDAP_DEBUG_ANY, "do_bind: get_ctrls failed\n", 0, 0, 0 );
161 #endif
162                 goto cleanup;
163         } 
164
165         /* We use the tmpmemctx here because it speeds up normalization.
166          * However, we must dup with regular malloc when storing any
167          * resulting DNs in the op or conn structures.
168          */
169         rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn,
170                 op->o_tmpmemctx );
171         if ( rs->sr_err != LDAP_SUCCESS ) {
172 #ifdef NEW_LOGGING
173                 LDAP_LOG( OPERATION, INFO, 
174                         "do_bind: conn %d  invalid dn (%s)\n", 
175                         op->o_connid, dn.bv_val, 0 );
176 #else
177                 Debug( LDAP_DEBUG_ANY, "bind: invalid dn (%s)\n",
178                         dn.bv_val, 0, 0 );
179 #endif
180                 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
181                 goto cleanup;
182         }
183
184         if( op->orb_method == LDAP_AUTH_SASL ) {
185 #ifdef NEW_LOGGING
186                 LDAP_LOG( OPERATION,     DETAIL1, 
187                         "do_sasl_bind: conn %d  dn (%s) mech %s\n", 
188                         op->o_connid, op->o_req_dn.bv_val, mech.bv_val );
189 #else
190                 Debug( LDAP_DEBUG_TRACE, "do_sasl_bind: dn (%s) mech %s\n",
191                         op->o_req_dn.bv_val, mech.bv_val, NULL );
192 #endif
193
194         } else {
195 #ifdef NEW_LOGGING
196                 LDAP_LOG( OPERATION, DETAIL1, 
197                         "do_bind: version=%ld dn=\"%s\" method=%ld\n",
198                         (unsigned long) version, op->o_req_dn.bv_val,
199                         (unsigned long) op->orb_method );
200 #else
201                 Debug( LDAP_DEBUG_TRACE,
202                         "do_bind: version=%ld dn=\"%s\" method=%ld\n",
203                         (unsigned long) version, op->o_req_dn.bv_val,
204                         (unsigned long) op->orb_method );
205 #endif
206         }
207
208         Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu BIND dn=\"%s\" method=%ld\n",
209             op->o_connid, op->o_opid, op->o_req_dn.bv_val,
210                 (unsigned long) op->orb_method, 0 );
211
212         if ( version < LDAP_VERSION_MIN || version > LDAP_VERSION_MAX ) {
213 #ifdef NEW_LOGGING
214                 LDAP_LOG( OPERATION, INFO, 
215                         "do_bind: conn %d  unknown version = %ld\n",
216                         op->o_connid, (unsigned long)version, 0 );
217 #else
218                 Debug( LDAP_DEBUG_ANY, "do_bind: unknown version=%ld\n",
219                         (unsigned long) version, 0, 0 );
220 #endif
221                 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
222                         "requested protocol version not supported" );
223                 goto cleanup;
224
225         } else if (!( global_allows & SLAP_ALLOW_BIND_V2 ) &&
226                 version < LDAP_VERSION3 )
227         {
228                 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
229                         "historical protocol version requested, use LDAPv3 instead" );
230                 goto cleanup;
231         }
232
233         /*
234          * we set connection version regardless of whether bind succeeds or not.
235          */
236         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
237         op->o_conn->c_protocol = version;
238         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
239
240         op->orb_tmp_mech = mech;
241
242         op->o_bd = frontendDB;
243         rs->sr_err = frontendDB->be_bind( op, rs );
244
245 cleanup:
246         if ( rs->sr_err == LDAP_SUCCESS ) {
247                 if ( op->orb_method != LDAP_AUTH_SASL ) {
248                         ber_dupbv( &op->o_conn->c_authmech, &mech );
249                 }
250                 op->o_conn->c_authtype = op->orb_method;
251         }
252
253         op->o_conn->c_sasl_bindop = NULL;
254
255         if( op->o_req_dn.bv_val != NULL ) {
256                 slap_sl_free( op->o_req_dn.bv_val, op->o_tmpmemctx );
257                 BER_BVZERO( &op->o_req_dn );
258         }
259         if( op->o_req_ndn.bv_val != NULL ) {
260                 slap_sl_free( op->o_req_ndn.bv_val, op->o_tmpmemctx );
261                 BER_BVZERO( &op->o_req_ndn );
262         }
263
264         return rs->sr_err;
265 }
266
267 int
268 fe_op_bind( Operation *op, SlapReply *rs )
269 {
270         struct berval   mech = op->orb_tmp_mech;
271
272         /* check for inappropriate controls */
273         if( get_manageDSAit( op ) == SLAP_CRITICAL_CONTROL ) {
274                 send_ldap_error( op, rs,
275                         LDAP_UNAVAILABLE_CRITICAL_EXTENSION,
276                         "manageDSAit control inappropriate" );
277                 goto cleanup;
278         }
279
280         /* Set the bindop for the benefit of in-directory SASL lookups */
281         op->o_conn->c_sasl_bindop = op;
282
283         if ( op->orb_method == LDAP_AUTH_SASL ) {
284                 if ( op->o_protocol < LDAP_VERSION3 ) {
285 #ifdef NEW_LOGGING
286                         LDAP_LOG( OPERATION, INFO, 
287                                 "do_bind: conn %d  sasl with LDAPv%ld\n",
288                                 op->o_connid, (unsigned long)op->o_protocol, 0 );
289 #else
290                         Debug( LDAP_DEBUG_ANY, "do_bind: sasl with LDAPv%ld\n",
291                                 (unsigned long)op->o_protocol, 0, 0 );
292 #endif
293                         send_ldap_discon( op, rs,
294                                 LDAP_PROTOCOL_ERROR, "SASL bind requires LDAPv3" );
295                         rs->sr_err = SLAPD_DISCONNECT;
296                         goto cleanup;
297                 }
298
299                 if( mech.bv_len == 0 ) {
300 #ifdef NEW_LOGGING
301                         LDAP_LOG( OPERATION, INFO, 
302                                    "do_bind: conn %d  no SASL mechanism provided\n",
303                                    op->o_connid, 0, 0 );
304 #else
305                         Debug( LDAP_DEBUG_ANY,
306                                 "do_bind: no sasl mechanism provided\n",
307                                 0, 0, 0 );
308 #endif
309                         send_ldap_error( op, rs, LDAP_AUTH_METHOD_NOT_SUPPORTED,
310                                 "no SASL mechanism provided" );
311                         goto cleanup;
312                 }
313
314                 /* check restrictions */
315                 if( backend_check_restrictions( op, rs, &mech ) != LDAP_SUCCESS ) {
316                         send_ldap_result( op, rs );
317                         goto cleanup;
318                 }
319
320                 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
321                 if ( op->o_conn->c_sasl_bind_in_progress ) {
322                         if( !bvmatch( &op->o_conn->c_sasl_bind_mech, &mech ) ) {
323                                 /* mechanism changed between bind steps */
324                                 slap_sasl_reset(op->o_conn);
325                         }
326                 } else {
327                         ber_dupbv(&op->o_conn->c_sasl_bind_mech, &mech);
328                 }
329                 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
330
331                 rs->sr_err = slap_sasl_bind( op, rs );
332
333                 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
334                 if( rs->sr_err == LDAP_SUCCESS ) {
335                         ber_dupbv(&op->o_conn->c_dn, &op->orb_edn);
336                         if( op->orb_edn.bv_len != 0 ) {
337                                 /* edn is always normalized already */
338                                 ber_dupbv( &op->o_conn->c_ndn, &op->o_conn->c_dn );
339                         }
340                         op->o_tmpfree( op->orb_edn.bv_val, op->o_tmpmemctx );
341                         BER_BVZERO( &op->orb_edn );
342                         op->o_conn->c_authmech = op->o_conn->c_sasl_bind_mech;
343                         BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
344                         op->o_conn->c_sasl_bind_in_progress = 0;
345
346                         op->o_conn->c_sasl_ssf = op->orb_ssf;
347                         if( op->orb_ssf > op->o_conn->c_ssf ) {
348                                 op->o_conn->c_ssf = op->orb_ssf;
349                         }
350
351                         if( op->o_conn->c_dn.bv_len != 0 ) {
352                                 ber_len_t max = sockbuf_max_incoming_auth;
353                                 ber_sockbuf_ctrl( op->o_conn->c_sb,
354                                         LBER_SB_OPT_SET_MAX_INCOMING, &max );
355                         }
356
357                         /* log authorization identity */
358                         Statslog( LDAP_DEBUG_STATS,
359                                 "conn=%lu op=%lu BIND dn=\"%s\" mech=%s ssf=%d\n",
360                                 op->o_connid, op->o_opid,
361                                 op->o_conn->c_dn.bv_val ? op->o_conn->c_dn.bv_val : "<empty>",
362                                 op->o_conn->c_authmech.bv_val, op->orb_ssf );
363
364 #ifdef NEW_LOGGING
365                         LDAP_LOG( OPERATION, DETAIL1, 
366                                 "do_bind: SASL/%s bind: dn=\"%s\" ssf=%d\n",
367                                 op->o_conn->c_authmech.bv_val,
368                                 op->o_conn->c_dn.bv_val ? op->o_conn->c_dn.bv_val : "<empty>",
369                                 op->orb_ssf );
370 #else
371                         Debug( LDAP_DEBUG_TRACE,
372                                 "do_bind: SASL/%s bind: dn=\"%s\" ssf=%d\n",
373                                 op->o_conn->c_authmech.bv_val,
374                                 op->o_conn->c_dn.bv_val ? op->o_conn->c_dn.bv_val : "<empty>",
375                                 op->orb_ssf );
376 #endif
377
378                 } else if ( rs->sr_err == LDAP_SASL_BIND_IN_PROGRESS ) {
379                         op->o_conn->c_sasl_bind_in_progress = 1;
380
381                 } else {
382                         if ( op->o_conn->c_sasl_bind_mech.bv_val ) {
383                                 free( op->o_conn->c_sasl_bind_mech.bv_val );
384                                 BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
385                         }
386                         op->o_conn->c_sasl_bind_in_progress = 0;
387                 }
388
389 #ifdef LDAP_SLAPI
390 #define pb      op->o_pb
391                 /*
392                  * Normally post-operation plugins are called only after the
393                  * backend operation. Because the front-end performs SASL
394                  * binds on behalf of the backend, we'll make a special
395                  * exception to call the post-operation plugins after a
396                  * SASL bind.
397                  */
398                 if ( pb ) {
399                         slapi_int_pblock_set_operation( pb, op );
400                         slapi_pblock_set( pb, SLAPI_BIND_TARGET, (void *)op->o_req_dn.bv_val );
401                         slapi_pblock_set( pb, SLAPI_BIND_METHOD, (void *)op->orb_method );
402                         slapi_pblock_set( pb,
403                                 SLAPI_BIND_CREDENTIALS, (void *)&op->orb_cred );
404                         slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)(0) );
405                         (void) slapi_int_call_plugins( op->o_bd,
406                                 SLAPI_PLUGIN_POST_BIND_FN, pb );
407                 }
408 #endif /* LDAP_SLAPI */
409
410                 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
411
412                 goto cleanup;
413
414         } else {
415                 /* Not SASL, cancel any in-progress bind */
416                 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
417
418                 if ( op->o_conn->c_sasl_bind_mech.bv_val != NULL ) {
419                         free(op->o_conn->c_sasl_bind_mech.bv_val);
420                         BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
421                 }
422                 op->o_conn->c_sasl_bind_in_progress = 0;
423
424                 slap_sasl_reset( op->o_conn );
425                 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
426         }
427
428         if ( op->orb_method == LDAP_AUTH_SIMPLE ) {
429                 ber_str2bv( "SIMPLE", sizeof("SIMPLE")-1, 0, &mech );
430                 /* accept "anonymous" binds */
431                 if ( op->orb_cred.bv_len == 0 || op->o_req_ndn.bv_len == 0 ) {
432                         rs->sr_err = LDAP_SUCCESS;
433
434                         if( op->orb_cred.bv_len &&
435                                 !( global_allows & SLAP_ALLOW_BIND_ANON_CRED ))
436                         {
437                                 /* cred is not empty, disallow */
438                                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
439
440                         } else if ( op->o_req_ndn.bv_len &&
441                                 !( global_allows & SLAP_ALLOW_BIND_ANON_DN ))
442                         {
443                                 /* DN is not empty, disallow */
444                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
445                                 rs->sr_text =
446                                         "unauthenticated bind (DN with no password) disallowed";
447
448                         } else if ( global_disallows & SLAP_DISALLOW_BIND_ANON ) {
449                                 /* disallow */
450                                 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
451                                 rs->sr_text = "anonymous bind disallowed";
452
453                         } else {
454                                 backend_check_restrictions( op, rs, &mech );
455                         }
456
457                         /*
458                          * we already forced connection to "anonymous",
459                          * just need to send success
460                          */
461                         send_ldap_result( op, rs );
462 #ifdef NEW_LOGGING
463                         LDAP_LOG( OPERATION, DETAIL1, 
464                                 "do_bind: conn %d  v%d anonymous bind\n",
465                                 op->o_connid, op->o_protocol, 0 );
466 #else
467                         Debug( LDAP_DEBUG_TRACE, "do_bind: v%d anonymous bind\n",
468                                 op->o_protocol, 0, 0 );
469 #endif
470                         goto cleanup;
471
472                 } else if ( global_disallows & SLAP_DISALLOW_BIND_SIMPLE ) {
473                         /* disallow simple authentication */
474                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
475                         rs->sr_text = "unwilling to perform simple authentication";
476
477                         send_ldap_result( op, rs );
478 #ifdef NEW_LOGGING
479                         LDAP_LOG( OPERATION, INFO, 
480                                 "do_bind: conn %d  v%d simple bind(%s) disallowed\n",
481                                 op->o_connid, op->o_protocol, op->o_req_ndn.bv_val );
482 #else
483                         Debug( LDAP_DEBUG_TRACE,
484                                 "do_bind: v%d simple bind(%s) disallowed\n",
485                                 op->o_protocol, op->o_req_ndn.bv_val, 0 );
486 #endif
487                         goto cleanup;
488                 }
489
490 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
491         } else if ( op->orb_method == LDAP_AUTH_KRBV41 ) {
492                 if ( global_disallows & SLAP_DISALLOW_BIND_KRBV4 ) {
493                         /* disallow krbv4 authentication */
494                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
495                         rs->sr_text = "unwilling to perform Kerberos V4 bind";
496
497                         send_ldap_result( op, rs );
498
499 #ifdef NEW_LOGGING
500                         LDAP_LOG( OPERATION, DETAIL1, 
501                                 "do_bind: conn %d  v%d Kerberos V4 (step 1) bind refused\n",
502                                 op->o_connid, op->o_protocol, 0 );
503 #else
504                         Debug( LDAP_DEBUG_TRACE,
505                                 "do_bind: v%d Kerberos V4 (step 1) bind refused\n",
506                                 op->o_protocol, 0, 0 );
507 #endif
508                         goto cleanup;
509                 }
510                 ber_str2bv( "KRBV4", sizeof("KRBV4")-1, 0, &mech );
511
512         } else if ( op->orb_method == LDAP_AUTH_KRBV42 ) {
513                 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
514                 rs->sr_text = "Kerberos V4 (step 2) bind not supported";
515                 send_ldap_result( op, rs );
516
517 #ifdef NEW_LOGGING
518                 LDAP_LOG( OPERATION, DETAIL1, 
519                         "do_bind: conn %d  v%d Kerberos V4 (step 2) bind refused\n",
520                         op->o_connid, op->o_protocol, 0 );
521 #else
522                 Debug( LDAP_DEBUG_TRACE,
523                         "do_bind: v%d Kerberos V4 (step 2) bind refused\n",
524                         op->o_protocol, 0, 0 );
525 #endif
526                 goto cleanup;
527 #endif
528
529         } else {
530                 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
531                 rs->sr_text = "unknown authentication method";
532
533                 send_ldap_result( op, rs );
534 #ifdef NEW_LOGGING
535                 LDAP_LOG( OPERATION, INFO, 
536                         "do_bind: conn %ld  v%d unknown authentication method (%ld)\n",
537                         op->o_connid, op->o_protocol, op->orb_method );
538 #else
539                 Debug( LDAP_DEBUG_TRACE,
540                         "do_bind: v%d unknown authentication method (%ld)\n",
541                         op->o_protocol, op->orb_method, 0 );
542 #endif
543                 goto cleanup;
544         }
545
546         /*
547          * We could be serving multiple database backends.  Select the
548          * appropriate one, or send a referral to our "referral server"
549          * if we don't hold it.
550          */
551
552         if ( (op->o_bd = select_backend( &op->o_req_ndn, 0, 0 )) == NULL ) {
553                 if ( default_referral ) {
554                         rs->sr_ref = referral_rewrite( default_referral,
555                                 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
556                         if (!rs->sr_ref) rs->sr_ref = default_referral;
557
558                         rs->sr_err = LDAP_REFERRAL;
559                         send_ldap_result( op, rs );
560
561                         if (rs->sr_ref != default_referral) ber_bvarray_free( rs->sr_ref );
562
563                 } else {
564                         /* noSuchObject is not allowed to be returned by bind */
565                         rs->sr_err = LDAP_INVALID_CREDENTIALS;
566                         send_ldap_result( op, rs );
567                 }
568
569                 goto cleanup;
570         }
571
572         /* check restrictions */
573         if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
574                 send_ldap_result( op, rs );
575                 goto cleanup;
576         }
577
578 #ifdef LDAP_SLAPI
579         if ( pb ) {
580                 int rc;
581                 slapi_int_pblock_set_operation( pb, op );
582                 slapi_pblock_set( pb, SLAPI_BIND_TARGET, (void *)op->o_req_dn.bv_val );
583                 slapi_pblock_set( pb, SLAPI_BIND_METHOD, (void *)op->orb_method );
584                 slapi_pblock_set( pb, SLAPI_BIND_CREDENTIALS, (void *)&op->orb_cred );
585                 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)(0) );
586                 slapi_pblock_set( pb, SLAPI_CONN_DN, (void *)(0) );
587
588                 rc = slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_PRE_BIND_FN, pb );
589
590 #ifdef NEW_LOGGING
591                 LDAP_LOG( OPERATION, INFO,
592                         "do_bind: Bind preoperation plugin returned %d\n",
593                         rs->sr_err, 0, 0);
594 #else
595                 Debug(LDAP_DEBUG_TRACE,
596                         "do_bind: Bind preoperation plugin returned %d.\n",
597                         rs->sr_err, 0, 0);
598 #endif
599
600                 switch ( rc ) {
601                 case SLAPI_BIND_SUCCESS:
602                         /* Continue with backend processing */
603                         break;
604                 case SLAPI_BIND_FAIL:
605                         /* Failure, server sends result */
606                         rs->sr_err = LDAP_INVALID_CREDENTIALS;
607                         send_ldap_result( op, rs );
608                         goto cleanup;
609                         break;
610                 case SLAPI_BIND_ANONYMOUS:
611                         /* SLAPI_BIND_ANONYMOUS is undocumented XXX */
612                 default:
613                         /* Authoritative, plugin sent result, or no plugins called. */
614                         if ( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE,
615                                 (void *)&rs->sr_err) != 0 )
616                         {
617                                 rs->sr_err = LDAP_OTHER;
618                         }
619
620                         BER_BVZERO( &op->orb_edn );
621
622                         if ( rs->sr_err == LDAP_SUCCESS ) {
623                                 slapi_pblock_get( pb, SLAPI_CONN_DN,
624                                         (void *)&op->orb_edn.bv_val );
625                                 if ( BER_BVISNULL( &op->orb_edn ) ) {
626                                         if ( rc == 1 ) {
627                                                 /* No plugins were called; continue. */
628                                                 break;
629                                         }
630                                 } else {
631                                         op->orb_edn.bv_len = strlen( op->orb_edn.bv_val );
632                                 }
633                                 rs->sr_err = dnPrettyNormal( NULL, &op->orb_edn,
634                                         &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
635                                 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
636                                 ber_dupbv(&op->o_conn->c_dn, &op->o_req_dn);
637                                 ber_dupbv(&op->o_conn->c_ndn, &op->o_req_ndn);
638                                 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
639                                 BER_BVZERO( &op->o_req_dn );
640                                 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
641                                 BER_BVZERO( &op->o_req_ndn );
642                                 if ( op->o_conn->c_dn.bv_len != 0 ) {
643                                         ber_len_t max = sockbuf_max_incoming_auth;
644                                         ber_sockbuf_ctrl( op->o_conn->c_sb,
645                                                 LBER_SB_OPT_SET_MAX_INCOMING, &max );
646                                 }
647                                 /* log authorization identity */
648                                 Statslog( LDAP_DEBUG_STATS,
649                                         "conn=%lu op=%lu BIND dn=\"%s\" mech=%s (SLAPI) ssf=0\n",
650                                         op->o_connid, op->o_opid,
651                                         op->o_conn->c_dn.bv_val
652                                                 ? op->o_conn->c_dn.bv_val : "<empty>",
653                                         mech.bv_val, 0 );
654                                 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
655                         }
656                         goto cleanup;
657                         break;
658                 }
659         }
660 #endif /* LDAP_SLAPI */
661
662         if( op->o_bd->be_bind ) {
663                 rs->sr_err = (op->o_bd->be_bind)( op, rs );
664
665                 if ( rs->sr_err == 0 ) {
666                         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
667
668                         if( op->o_conn->c_authz_backend == NULL ) {
669                                 op->o_conn->c_authz_backend = op->o_bd;
670                         }
671
672                         /* be_bind returns regular/global edn */
673                         if( op->orb_edn.bv_len ) {
674                                 op->o_conn->c_dn = op->orb_edn;
675                         } else {
676                                 ber_dupbv(&op->o_conn->c_dn, &op->o_req_dn);
677                         }
678
679                         ber_dupbv( &op->o_conn->c_ndn, &op->o_req_ndn );
680
681                         if( op->o_conn->c_dn.bv_len != 0 ) {
682                                 ber_len_t max = sockbuf_max_incoming_auth;
683                                 ber_sockbuf_ctrl( op->o_conn->c_sb,
684                                         LBER_SB_OPT_SET_MAX_INCOMING, &max );
685                         }
686
687                         /* log authorization identity */
688                         Statslog( LDAP_DEBUG_STATS,
689                                 "conn=%lu op=%lu BIND dn=\"%s\" mech=%s ssf=0\n",
690                                 op->o_connid, op->o_opid,
691                                 op->o_conn->c_dn.bv_val, mech.bv_val, 0 );
692
693 #ifdef NEW_LOGGING
694                         LDAP_LOG( OPERATION, DETAIL1, 
695                                 "do_bind: v%d bind: \"%s\" to \"%s\" \n",
696                                 op->o_protocol, op->o_conn->c_dn.bv_val, op->o_conn->c_dn.bv_val );
697 #else
698                         Debug( LDAP_DEBUG_TRACE,
699                                 "do_bind: v%d bind: \"%s\" to \"%s\"\n",
700                                 op->o_protocol, op->o_req_dn.bv_val, op->o_conn->c_dn.bv_val );
701 #endif
702
703                         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
704
705                         /* send this here to avoid a race condition */
706                         send_ldap_result( op, rs );
707
708                 } else if (op->orb_edn.bv_val != NULL) {
709                         free( op->orb_edn.bv_val );
710                 }
711
712         } else {
713                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
714                         "operation not supported within naming context" );
715         }
716
717 #ifdef LDAP_SLAPI
718         if ( pb != NULL &&
719                 slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_POST_BIND_FN, pb ) < 0 )
720         {
721 #ifdef NEW_LOGGING
722                 LDAP_LOG( OPERATION, INFO,
723                         "do_bind: Bind postoperation plugins failed\n",
724                         0, 0, 0);
725 #else
726                 Debug(LDAP_DEBUG_TRACE,
727                         "do_bind: Bind postoperation plugins failed.\n",
728                         0, 0, 0);
729 #endif
730         }
731 #endif /* LDAP_SLAPI */
732
733 cleanup:;
734         return rs->sr_err;
735 }
736