]> git.sur5r.net Git - openldap/blob - servers/slapd/bind.c
Import ITS#4158 fixes from HEAD
[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-2005 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                                 op->orb_cred.bv_val = NULL;
141                                 op->orb_cred.bv_len = 0;
142                         }
143
144                         if ( tag != LBER_ERROR ) {
145                                 tag = ber_scanf( ber, /*{{*/ "}}" );
146                         }
147                 }
148         }
149
150         if ( tag == LBER_ERROR ) {
151                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
152                 rs->sr_err = SLAPD_DISCONNECT;
153                 goto cleanup;
154         }
155
156         if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
157 #ifdef NEW_LOGGING
158                 LDAP_LOG( OPERATION, INFO, 
159                         "do_bind: conn %d  get_ctrls failed\n", op->o_connid, 0, 0 );
160 #else
161                 Debug( LDAP_DEBUG_ANY, "do_bind: get_ctrls failed\n", 0, 0, 0 );
162 #endif
163                 goto cleanup;
164         } 
165
166         /* We use the tmpmemctx here because it speeds up normalization.
167          * However, we must dup with regular malloc when storing any
168          * resulting DNs in the op or conn structures.
169          */
170         rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn,
171                 op->o_tmpmemctx );
172         if ( rs->sr_err != LDAP_SUCCESS ) {
173 #ifdef NEW_LOGGING
174                 LDAP_LOG( OPERATION, INFO, 
175                         "do_bind: conn %d  invalid dn (%s)\n", 
176                         op->o_connid, dn.bv_val, 0 );
177 #else
178                 Debug( LDAP_DEBUG_ANY, "bind: invalid dn (%s)\n",
179                         dn.bv_val, 0, 0 );
180 #endif
181                 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
182                 goto cleanup;
183         }
184
185         if( op->orb_method == LDAP_AUTH_SASL ) {
186 #ifdef NEW_LOGGING
187                 LDAP_LOG( OPERATION,     DETAIL1, 
188                         "do_sasl_bind: conn %d  dn (%s) mech %s\n", 
189                         op->o_connid, op->o_req_dn.bv_val, mech.bv_val );
190 #else
191                 Debug( LDAP_DEBUG_TRACE, "do_sasl_bind: dn (%s) mech %s\n",
192                         op->o_req_dn.bv_val, mech.bv_val, NULL );
193 #endif
194
195         } else {
196 #ifdef NEW_LOGGING
197                 LDAP_LOG( OPERATION, DETAIL1, 
198                         "do_bind: version=%ld dn=\"%s\" method=%ld\n",
199                         (unsigned long) version, op->o_req_dn.bv_val,
200                         (unsigned long) op->orb_method );
201 #else
202                 Debug( LDAP_DEBUG_TRACE,
203                         "do_bind: version=%ld dn=\"%s\" method=%ld\n",
204                         (unsigned long) version, op->o_req_dn.bv_val,
205                         (unsigned long) op->orb_method );
206 #endif
207         }
208
209         Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu BIND dn=\"%s\" method=%ld\n",
210             op->o_connid, op->o_opid, op->o_req_dn.bv_val,
211                 (unsigned long) op->orb_method, 0 );
212
213         if ( version < LDAP_VERSION_MIN || version > LDAP_VERSION_MAX ) {
214 #ifdef NEW_LOGGING
215                 LDAP_LOG( OPERATION, INFO, 
216                         "do_bind: conn %d  unknown version = %ld\n",
217                         op->o_connid, (unsigned long)version, 0 );
218 #else
219                 Debug( LDAP_DEBUG_ANY, "do_bind: unknown version=%ld\n",
220                         (unsigned long) version, 0, 0 );
221 #endif
222                 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
223                         "requested protocol version not supported" );
224                 goto cleanup;
225
226         } else if (!( global_allows & SLAP_ALLOW_BIND_V2 ) &&
227                 version < LDAP_VERSION3 )
228         {
229                 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
230                         "historical protocol version requested, use LDAPv3 instead" );
231                 goto cleanup;
232         }
233
234         /*
235          * we set connection version regardless of whether bind succeeds or not.
236          */
237         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
238         op->o_conn->c_protocol = version;
239         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
240
241         /* check for inappropriate controls */
242         if( get_manageDSAit( op ) == SLAP_CRITICAL_CONTROL ) {
243                 send_ldap_error( op, rs,
244                         LDAP_UNAVAILABLE_CRITICAL_EXTENSION,
245                         "manageDSAit control inappropriate" );
246                 goto cleanup;
247         }
248
249         /* Set the bindop for the benefit of in-directory SASL lookups */
250         op->o_conn->c_sasl_bindop = op;
251
252         if ( op->orb_method == LDAP_AUTH_SASL ) {
253                 if ( version < LDAP_VERSION3 ) {
254 #ifdef NEW_LOGGING
255                         LDAP_LOG( OPERATION, INFO, 
256                                 "do_bind: conn %d  sasl with LDAPv%ld\n",
257                                 op->o_connid, (unsigned long)version , 0 );
258 #else
259                         Debug( LDAP_DEBUG_ANY, "do_bind: sasl with LDAPv%ld\n",
260                                 (unsigned long) version, 0, 0 );
261 #endif
262                         send_ldap_discon( op, rs,
263                                 LDAP_PROTOCOL_ERROR, "SASL bind requires LDAPv3" );
264                         rs->sr_err = SLAPD_DISCONNECT;
265                         goto cleanup;
266                 }
267
268                 if( mech.bv_len == 0 ) {
269 #ifdef NEW_LOGGING
270                         LDAP_LOG( OPERATION, INFO, 
271                                    "do_bind: conn %d  no SASL mechanism provided\n",
272                                    op->o_connid, 0, 0 );
273 #else
274                         Debug( LDAP_DEBUG_ANY,
275                                 "do_bind: no sasl mechanism provided\n",
276                                 0, 0, 0 );
277 #endif
278                         send_ldap_error( op, rs, LDAP_AUTH_METHOD_NOT_SUPPORTED,
279                                 "no SASL mechanism provided" );
280                         goto cleanup;
281                 }
282
283                 /* check restrictions */
284                 if( backend_check_restrictions( op, rs, &mech ) != LDAP_SUCCESS ) {
285                         send_ldap_result( op, rs );
286                         goto cleanup;
287                 }
288
289                 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
290                 if ( op->o_conn->c_sasl_bind_in_progress ) {
291                         if( !bvmatch( &op->o_conn->c_sasl_bind_mech, &mech ) ) {
292                                 /* mechanism changed between bind steps */
293                                 slap_sasl_reset(op->o_conn);
294                         }
295                 } else {
296                         ber_dupbv(&op->o_conn->c_sasl_bind_mech, &mech);
297                 }
298                 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
299
300                 rs->sr_err = slap_sasl_bind( op, rs );
301
302 #ifdef LDAP_SLAPI
303 #define pb      op->o_pb
304                 /*
305                  * Normally post-operation plugins are called only after the
306                  * backend operation. Because the front-end performs SASL
307                  * binds on behalf of the backend, we'll make a special
308                  * exception to call the post-operation plugins after a
309                  * SASL bind.
310                  */
311                 if ( pb ) {
312                         slapi_int_pblock_set_operation( pb, op );
313                         slapi_pblock_set( pb, SLAPI_BIND_TARGET, (void *)dn.bv_val );
314                         slapi_pblock_set( pb, SLAPI_BIND_METHOD, (void *)op->orb_method );
315                         slapi_pblock_set( pb, SLAPI_BIND_CREDENTIALS, (void *)&op->orb_cred );
316                         slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)(0) );
317                         (void) slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_POST_BIND_FN, pb );
318                 }
319 #endif /* LDAP_SLAPI */
320
321                 goto cleanup;
322
323         } else {
324                 /* Not SASL, cancel any in-progress bind */
325                 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
326
327                 if ( op->o_conn->c_sasl_bind_mech.bv_val != NULL ) {
328                         free(op->o_conn->c_sasl_bind_mech.bv_val);
329                         op->o_conn->c_sasl_bind_mech.bv_val = NULL;
330                         op->o_conn->c_sasl_bind_mech.bv_len = 0;
331                 }
332                 op->o_conn->c_sasl_bind_in_progress = 0;
333
334                 slap_sasl_reset( op->o_conn );
335                 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
336         }
337
338         if ( op->orb_method == LDAP_AUTH_SIMPLE ) {
339                 ber_str2bv( "SIMPLE", sizeof("SIMPLE")-1, 0, &mech );
340                 /* accept "anonymous" binds */
341                 if ( op->orb_cred.bv_len == 0 || op->o_req_ndn.bv_len == 0 ) {
342                         rs->sr_err = LDAP_SUCCESS;
343
344                         if( op->orb_cred.bv_len &&
345                                 !( global_allows & SLAP_ALLOW_BIND_ANON_CRED ))
346                         {
347                                 /* cred is not empty, disallow */
348                                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
349
350                         } else if ( op->o_req_ndn.bv_len &&
351                                 !( global_allows & SLAP_ALLOW_BIND_ANON_DN ))
352                         {
353                                 /* DN is not empty, disallow */
354                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
355                                 rs->sr_text =
356                                         "unauthenticated bind (DN with no password) disallowed";
357
358                         } else if ( global_disallows & SLAP_DISALLOW_BIND_ANON ) {
359                                 /* disallow */
360                                 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
361                                 rs->sr_text = "anonymous bind disallowed";
362
363                         } else {
364                                 backend_check_restrictions( op, rs, &mech );
365                         }
366
367                         /*
368                          * we already forced connection to "anonymous",
369                          * just need to send success
370                          */
371                         send_ldap_result( op, rs );
372 #ifdef NEW_LOGGING
373                         LDAP_LOG( OPERATION, DETAIL1, 
374                                 "do_bind: conn %d  v%d anonymous bind\n",
375                                 op->o_connid, version , 0 );
376 #else
377                         Debug( LDAP_DEBUG_TRACE, "do_bind: v%d anonymous bind\n",
378                                 version, 0, 0 );
379 #endif
380                         goto cleanup;
381
382                 } else if ( global_disallows & SLAP_DISALLOW_BIND_SIMPLE ) {
383                         /* disallow simple authentication */
384                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
385                         rs->sr_text = "unwilling to perform simple authentication";
386
387                         send_ldap_result( op, rs );
388 #ifdef NEW_LOGGING
389                         LDAP_LOG( OPERATION, INFO, 
390                                 "do_bind: conn %d  v%d simple bind(%s) disallowed\n",
391                                 op->o_connid, version, op->o_req_ndn.bv_val );
392 #else
393                         Debug( LDAP_DEBUG_TRACE,
394                                 "do_bind: v%d simple bind(%s) disallowed\n",
395                                 version, op->o_req_ndn.bv_val, 0 );
396 #endif
397                         goto cleanup;
398                 }
399
400 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
401         } else if ( op->orb_method == LDAP_AUTH_KRBV41 ) {
402                 if ( global_disallows & SLAP_DISALLOW_BIND_KRBV4 ) {
403                         /* disallow krbv4 authentication */
404                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
405                         rs->sr_text = "unwilling to perform Kerberos V4 bind";
406
407                         send_ldap_result( op, rs );
408
409 #ifdef NEW_LOGGING
410                         LDAP_LOG( OPERATION, DETAIL1, 
411                                 "do_bind: conn %d  v%d Kerberos V4 (step 1) bind refused\n",
412                                 op->o_connid, version , 0 );
413 #else
414                         Debug( LDAP_DEBUG_TRACE,
415                                 "do_bind: v%d Kerberos V4 (step 1) bind refused\n",
416                                 version, 0, 0 );
417 #endif
418                         goto cleanup;
419                 }
420                 ber_str2bv( "KRBV4", sizeof("KRBV4")-1, 0, &mech );
421
422         } else if ( op->orb_method == LDAP_AUTH_KRBV42 ) {
423                 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
424                 rs->sr_text = "Kerberos V4 (step 2) bind not supported";
425                 send_ldap_result( op, rs );
426
427 #ifdef NEW_LOGGING
428                 LDAP_LOG( OPERATION, DETAIL1, 
429                         "do_bind: conn %d  v%d Kerberos V4 (step 2) bind refused\n",
430                         op->o_connid, version , 0 );
431 #else
432                 Debug( LDAP_DEBUG_TRACE,
433                         "do_bind: v%d Kerberos V4 (step 2) bind refused\n",
434                         version, 0, 0 );
435 #endif
436                 goto cleanup;
437 #endif
438
439         } else {
440                 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
441                 rs->sr_text = "unknown authentication method";
442
443                 send_ldap_result( op, rs );
444 #ifdef NEW_LOGGING
445                 LDAP_LOG( OPERATION, INFO, 
446                         "do_bind: conn %ld  v%d unknown authentication method (%ld)\n",
447                         op->o_connid, version, op->orb_method );
448 #else
449                 Debug( LDAP_DEBUG_TRACE,
450                         "do_bind: v%d unknown authentication method (%ld)\n",
451                         version, op->orb_method, 0 );
452 #endif
453                 goto cleanup;
454         }
455
456         /*
457          * We could be serving multiple database backends.  Select the
458          * appropriate one, or send a referral to our "referral server"
459          * if we don't hold it.
460          */
461
462         if ( (op->o_bd = select_backend( &op->o_req_ndn, 0, 0 )) == NULL ) {
463                 /* don't return referral for bind requests */
464                 /* noSuchObject is not allowed to be returned by bind */
465                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
466                 send_ldap_result( op, rs );
467                 goto cleanup;
468         }
469
470         /* check restrictions */
471         if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
472                 send_ldap_result( op, rs );
473                 goto cleanup;
474         }
475
476 #ifdef LDAP_SLAPI
477         if ( pb ) {
478                 int rc;
479                 slapi_int_pblock_set_operation( pb, op );
480                 slapi_pblock_set( pb, SLAPI_BIND_TARGET, (void *)dn.bv_val );
481                 slapi_pblock_set( pb, SLAPI_BIND_METHOD, (void *)op->orb_method );
482                 slapi_pblock_set( pb, SLAPI_BIND_CREDENTIALS, (void *)&op->orb_cred );
483                 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)(0) );
484                 slapi_pblock_set( pb, SLAPI_CONN_DN, (void *)(0) );
485
486                 rc = slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_PRE_BIND_FN, pb );
487
488 #ifdef NEW_LOGGING
489                 LDAP_LOG( OPERATION, INFO,
490                         "do_bind: Bind preoperation plugin returned %d\n",
491                         rs->sr_err, 0, 0);
492 #else
493                 Debug(LDAP_DEBUG_TRACE,
494                         "do_bind: Bind preoperation plugin returned %d.\n",
495                         rs->sr_err, 0, 0);
496 #endif
497
498                 switch ( rc ) {
499                 case SLAPI_BIND_SUCCESS:
500                         /* Continue with backend processing */
501                         break;
502                 case SLAPI_BIND_FAIL:
503                         /* Failure, server sends result */
504                         rs->sr_err = LDAP_INVALID_CREDENTIALS;
505                         send_ldap_result( op, rs );
506                         goto cleanup;
507                         break;
508                 case SLAPI_BIND_ANONYMOUS:
509                         /* SLAPI_BIND_ANONYMOUS is undocumented XXX */
510                 default:
511                         /* Authoritative, plugin sent result, or no plugins called. */
512                         if ( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE,
513                                 (void *)&rs->sr_err) != 0 )
514                         {
515                                 rs->sr_err = LDAP_OTHER;
516                         }
517
518                         op->orb_edn.bv_val = NULL;
519                         op->orb_edn.bv_len = 0;
520
521                         if ( rs->sr_err == LDAP_SUCCESS ) {
522                                 slapi_pblock_get( pb, SLAPI_CONN_DN,
523                                         (void *)&op->orb_edn.bv_val );
524                                 if ( op->orb_edn.bv_val == NULL ) {
525                                         if ( rc == 1 ) {
526                                                 /* No plugins were called; continue. */
527                                                 break;
528                                         }
529                                 } else {
530                                         op->orb_edn.bv_len = strlen( op->orb_edn.bv_val );
531                                 }
532                                 rs->sr_err = dnPrettyNormal( NULL, &op->orb_edn,
533                                         &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
534                                 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
535                                 ber_dupbv(&op->o_conn->c_dn, &op->o_req_dn);
536                                 ber_dupbv(&op->o_conn->c_ndn, &op->o_req_ndn);
537                                 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
538                                 op->o_req_dn.bv_val = NULL;
539                                 op->o_req_dn.bv_len = 0;
540                                 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
541                                 op->o_req_ndn.bv_val = NULL;
542                                 op->o_req_ndn.bv_len = 0;
543                                 if ( op->o_conn->c_dn.bv_len != 0 ) {
544                                         ber_len_t max = sockbuf_max_incoming_auth;
545                                         ber_sockbuf_ctrl( op->o_conn->c_sb,
546                                                 LBER_SB_OPT_SET_MAX_INCOMING, &max );
547                                 }
548                                 /* log authorization identity */
549                                 Statslog( LDAP_DEBUG_STATS,
550                                         "conn=%lu op=%lu BIND dn=\"%s\" mech=%s (SLAPI) ssf=0\n",
551                                         op->o_connid, op->o_opid,
552                                         op->o_conn->c_dn.bv_val ? op->o_conn->c_dn.bv_val : "<empty>",
553                                         mech.bv_val, 0 );
554                                 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
555                         }
556                         goto cleanup;
557                         break;
558                 }
559         }
560 #endif /* LDAP_SLAPI */
561
562         if( op->o_bd->be_bind ) {
563                 rs->sr_err = (op->o_bd->be_bind)( op, rs );
564
565                 if ( rs->sr_err == 0 ) {
566                         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
567
568                         if( op->o_conn->c_authz_backend == NULL ) {
569                                 op->o_conn->c_authz_backend = op->o_bd;
570                         }
571
572                         /* be_bind returns regular/global edn */
573                         if( op->orb_edn.bv_len ) {
574                                 op->o_conn->c_dn = op->orb_edn;
575                         } else {
576                                 ber_dupbv(&op->o_conn->c_dn, &op->o_req_dn);
577                         }
578
579                         ber_dupbv( &op->o_conn->c_ndn, &op->o_req_ndn );
580
581                         if( op->o_conn->c_dn.bv_len != 0 ) {
582                                 ber_len_t max = sockbuf_max_incoming_auth;
583                                 ber_sockbuf_ctrl( op->o_conn->c_sb,
584                                         LBER_SB_OPT_SET_MAX_INCOMING, &max );
585                         }
586
587                         /* log authorization identity */
588                         Statslog( LDAP_DEBUG_STATS,
589                                 "conn=%lu op=%lu BIND dn=\"%s\" mech=%s ssf=0\n",
590                                 op->o_connid, op->o_opid,
591                                 op->o_conn->c_dn.bv_val, mech.bv_val, 0 );
592
593 #ifdef NEW_LOGGING
594                         LDAP_LOG( OPERATION, DETAIL1, 
595                                 "do_bind: v%d bind: \"%s\" to \"%s\" \n",
596                                 version, op->o_conn->c_dn.bv_val, op->o_conn->c_dn.bv_val );
597 #else
598                         Debug( LDAP_DEBUG_TRACE,
599                                 "do_bind: v%d bind: \"%s\" to \"%s\"\n",
600                                 version, dn.bv_val, op->o_conn->c_dn.bv_val );
601 #endif
602
603                         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
604
605                         /* send this here to avoid a race condition */
606                         send_ldap_result( op, rs );
607
608                 } else if (op->orb_edn.bv_val != NULL) {
609                         free( op->orb_edn.bv_val );
610                 }
611
612         } else {
613                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
614                         "operation not supported within naming context" );
615         }
616
617 #ifdef LDAP_SLAPI
618         if ( pb != NULL &&
619                 slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_POST_BIND_FN, pb ) < 0 )
620         {
621 #ifdef NEW_LOGGING
622                 LDAP_LOG( OPERATION, INFO,
623                         "do_bind: Bind postoperation plugins failed\n",
624                         0, 0, 0);
625 #else
626                 Debug(LDAP_DEBUG_TRACE,
627                         "do_bind: Bind postoperation plugins failed.\n",
628                         0, 0, 0);
629 #endif
630         }
631 #endif /* LDAP_SLAPI */
632
633 cleanup:
634         if ( rs->sr_err == LDAP_SUCCESS ) {
635                 if ( op->orb_method != LDAP_AUTH_SASL ) {
636                         ber_dupbv( &op->o_conn->c_authmech, &mech );
637                 }
638                 op->o_conn->c_authtype = op->orb_method;
639         }
640
641         if( op->o_req_dn.bv_val != NULL ) {
642                 sl_free( op->o_req_dn.bv_val, op->o_tmpmemctx );
643                 op->o_req_dn.bv_val = NULL;
644         }
645         if( op->o_req_ndn.bv_val != NULL ) {
646                 sl_free( op->o_req_ndn.bv_val, op->o_tmpmemctx );
647                 op->o_req_ndn.bv_val = NULL;
648         }
649
650         return rs->sr_err;
651 }