]> git.sur5r.net Git - openldap/blob - servers/slapd/bind.c
ITS#3353 consolidate slapd globals into a single struct
[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 "slap.h"
35 #ifdef LDAP_SLAPI
36 #include "slapi/slapi.h"
37 #endif
38
39
40 int
41 do_bind(
42     Operation   *op,
43     SlapReply   *rs )
44 {
45         BerElement *ber = op->o_ber;
46         ber_int_t version;
47         ber_tag_t method;
48         struct berval mech = BER_BVNULL;
49         struct berval dn = BER_BVNULL;
50         ber_tag_t tag;
51         Backend *be = NULL;
52
53         Debug( LDAP_DEBUG_TRACE, "do_bind\n", 0, 0, 0 );
54
55         /*
56          * Force to connection to "anonymous" until bind succeeds.
57          */
58         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
59         if ( op->o_conn->c_sasl_bind_in_progress ) {
60                 be = op->o_conn->c_authz_backend;
61         }
62         if ( !BER_BVISEMPTY( &op->o_conn->c_dn ) ) {
63                 /* log authorization identity demotion */
64                 Statslog( LDAP_DEBUG_STATS,
65                         "%s BIND anonymous mech=implicit ssf=0\n",
66                         op->o_log_prefix, 0, 0, 0, 0 );
67         }
68         connection2anonymous( op->o_conn );
69         if ( op->o_conn->c_sasl_bind_in_progress ) {
70                 op->o_conn->c_authz_backend = be;
71         }
72         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
73         if ( !BER_BVISNULL( &op->o_dn ) ) {
74                 /* NOTE: temporarily wasting few bytes
75                  * (until bind is completed), but saving
76                  * a couple of ch_free() and ch_strdup("") */ 
77                 op->o_dn.bv_val[0] = '\0';
78                 op->o_dn.bv_len = 0;
79         }
80         if ( !BER_BVISNULL( &op->o_ndn ) ) {
81                 op->o_ndn.bv_val[0] = '\0';
82                 op->o_ndn.bv_len = 0;
83         }
84
85         /*
86          * Parse the bind request.  It looks like this:
87          *
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
96          *              }
97          *      }
98          *
99          *      SaslCredentials ::= SEQUENCE {
100          *              mechanism           LDAPString,
101          *              credentials         OCTET STRING OPTIONAL
102          *      }
103          */
104
105         tag = ber_scanf( ber, "{imt" /*}*/, &version, &dn, &method );
106
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;
111                 goto cleanup;
112         }
113
114         op->o_protocol = version;
115         op->orb_method = method;
116
117         if( op->orb_method != LDAP_AUTH_SASL ) {
118                 tag = ber_scanf( ber, /*{*/ "m}", &op->orb_cred );
119
120         } else {
121                 tag = ber_scanf( ber, "{m" /*}*/, &mech );
122
123                 if ( tag != LBER_ERROR ) {
124                         ber_len_t len;
125                         tag = ber_peek_tag( ber, &len );
126
127                         if ( tag == LDAP_TAG_LDAPCRED ) { 
128                                 tag = ber_scanf( ber, "m", &op->orb_cred );
129                         } else {
130                                 tag = LDAP_TAG_LDAPCRED;
131                                 BER_BVZERO( &op->orb_cred );
132                         }
133
134                         if ( tag != LBER_ERROR ) {
135                                 tag = ber_scanf( ber, /*{{*/ "}}" );
136                         }
137                 }
138         }
139
140         if ( tag == LBER_ERROR ) {
141                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
142                 rs->sr_err = SLAPD_DISCONNECT;
143                 goto cleanup;
144         }
145
146         if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
147                 Debug( LDAP_DEBUG_ANY, "do_bind: get_ctrls failed\n", 0, 0, 0 );
148                 goto cleanup;
149         } 
150
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.
154          */
155         rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn,
156                 op->o_tmpmemctx );
157         if ( rs->sr_err != LDAP_SUCCESS ) {
158                 Debug( LDAP_DEBUG_ANY, "bind: invalid dn (%s)\n",
159                         dn.bv_val, 0, 0 );
160                 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
161                 goto cleanup;
162         }
163
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 );
167
168         } else {
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 );
173         }
174
175         Statslog( LDAP_DEBUG_STATS, "%s BIND dn=\"%s\" method=%ld\n",
176             op->o_log_prefix, op->o_req_dn.bv_val,
177                 (unsigned long) op->orb_method, 0, 0 );
178
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" );
184                 goto cleanup;
185
186         } else if (!( SLAPD_GLOBAL(allows) & SLAP_ALLOW_BIND_V2 ) &&
187                 version < LDAP_VERSION3 )
188         {
189                 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
190                         "historical protocol version requested, use LDAPv3 instead" );
191                 goto cleanup;
192         }
193
194         /*
195          * we set connection version regardless of whether bind succeeds or not.
196          */
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 );
200
201         op->orb_tmp_mech = mech;
202
203         op->o_bd = frontendDB;
204         rs->sr_err = frontendDB->be_bind( op, rs );
205
206 cleanup:
207         if ( rs->sr_err == LDAP_SUCCESS ) {
208                 if ( op->orb_method != LDAP_AUTH_SASL ) {
209                         ber_dupbv( &op->o_conn->c_authmech, &mech );
210                 }
211                 op->o_conn->c_authtype = op->orb_method;
212         }
213
214         op->o_conn->c_sasl_bindop = NULL;
215
216         if( !BER_BVISNULL( &op->o_req_dn ) ) {
217                 slap_sl_free( op->o_req_dn.bv_val, op->o_tmpmemctx );
218                 BER_BVZERO( &op->o_req_dn );
219         }
220         if( !BER_BVISNULL( &op->o_req_ndn ) ) {
221                 slap_sl_free( op->o_req_ndn.bv_val, op->o_tmpmemctx );
222                 BER_BVZERO( &op->o_req_ndn );
223         }
224
225         return rs->sr_err;
226 }
227
228 int
229 fe_op_bind( Operation *op, SlapReply *rs )
230 {
231         struct berval   mech = op->orb_tmp_mech;
232
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" );
238                 goto cleanup;
239         }
240
241         /* Set the bindop for the benefit of in-directory SASL lookups */
242         op->o_conn->c_sasl_bindop = op;
243
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;
251                         goto cleanup;
252                 }
253
254                 if( BER_BVISNULL( &mech ) || BER_BVISEMPTY( &mech ) ) {
255                         Debug( LDAP_DEBUG_ANY,
256                                 "do_bind: no sasl mechanism provided\n",
257                                 0, 0, 0 );
258                         send_ldap_error( op, rs, LDAP_AUTH_METHOD_NOT_SUPPORTED,
259                                 "no SASL mechanism provided" );
260                         goto cleanup;
261                 }
262
263                 /* check restrictions */
264                 if( backend_check_restrictions( op, rs, &mech ) != LDAP_SUCCESS ) {
265                         send_ldap_result( op, rs );
266                         goto cleanup;
267                 }
268
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);
274                         }
275                 } else {
276                         ber_dupbv(&op->o_conn->c_sasl_bind_mech, &mech);
277                 }
278                 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
279
280                 rs->sr_err = slap_sasl_bind( op, rs );
281
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( !BER_BVISEMPTY( &op->orb_edn ) ) {
286                                 /* edn is always normalized already */
287                                 ber_dupbv( &op->o_conn->c_ndn, &op->o_conn->c_dn );
288                         }
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;
294
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;
298                         }
299
300                         if( !BER_BVISEMPTY( &op->o_conn->c_dn ) ) {
301                                 ber_len_t max = SLAPD_GLOBAL(sockbuf_max_incoming_auth);
302                                 ber_sockbuf_ctrl( op->o_conn->c_sb,
303                                         LBER_SB_OPT_SET_MAX_INCOMING, &max );
304                         }
305
306                         /* log authorization identity */
307                         Statslog( LDAP_DEBUG_STATS,
308                                 "%s BIND dn=\"%s\" mech=%s ssf=%d\n",
309                                 op->o_log_prefix,
310                                 BER_BVISNULL( &op->o_conn->c_dn ) ? "<empty>" : op->o_conn->c_dn.bv_val,
311                                 op->o_conn->c_authmech.bv_val, op->orb_ssf, 0 );
312
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                                 BER_BVISNULL( &op->o_conn->c_dn ) ? "<empty>" : op->o_conn->c_dn.bv_val,
317                                 op->orb_ssf );
318
319                 } else if ( rs->sr_err == LDAP_SASL_BIND_IN_PROGRESS ) {
320                         op->o_conn->c_sasl_bind_in_progress = 1;
321
322                 } else {
323                         if ( !BER_BVISNULL( &op->o_conn->c_sasl_bind_mech ) ) {
324                                 free( op->o_conn->c_sasl_bind_mech.bv_val );
325                                 BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
326                         }
327                         op->o_conn->c_sasl_bind_in_progress = 0;
328                 }
329
330 #ifdef LDAP_SLAPI
331 #define pb      op->o_pb
332                 /*
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
337                  * SASL bind.
338                  */
339                 if ( pb ) {
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 );
348                 }
349 #endif /* LDAP_SLAPI */
350
351                 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
352
353                 goto cleanup;
354
355         } else {
356                 /* Not SASL, cancel any in-progress bind */
357                 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
358
359                 if ( !BER_BVISNULL( &op->o_conn->c_sasl_bind_mech ) ) {
360                         free( op->o_conn->c_sasl_bind_mech.bv_val );
361                         BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
362                 }
363                 op->o_conn->c_sasl_bind_in_progress = 0;
364
365                 slap_sasl_reset( op->o_conn );
366                 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
367         }
368
369         if ( op->orb_method == LDAP_AUTH_SIMPLE ) {
370                 BER_BVSTR( &mech, "SIMPLE" );
371                 /* accept "anonymous" binds */
372                 if ( BER_BVISEMPTY( &op->orb_cred ) || BER_BVISEMPTY( &op->o_req_ndn ) ) {
373                         rs->sr_err = LDAP_SUCCESS;
374
375                         if( !BER_BVISEMPTY( &op->orb_cred ) &&
376                                 !( SLAPD_GLOBAL(allows) & SLAP_ALLOW_BIND_ANON_CRED ))
377                         {
378                                 /* cred is not empty, disallow */
379                                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
380
381                         } else if ( !BER_BVISEMPTY( &op->o_req_ndn ) &&
382                                 !( SLAPD_GLOBAL(allows) & SLAP_ALLOW_BIND_ANON_DN ))
383                         {
384                                 /* DN is not empty, disallow */
385                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
386                                 rs->sr_text =
387                                         "unauthenticated bind (DN with no password) disallowed";
388
389                         } else if ( SLAPD_GLOBAL(disallows) & SLAP_DISALLOW_BIND_ANON ) {
390                                 /* disallow */
391                                 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
392                                 rs->sr_text = "anonymous bind disallowed";
393
394                         } else {
395                                 backend_check_restrictions( op, rs, &mech );
396                         }
397
398                         /*
399                          * we already forced connection to "anonymous",
400                          * just need to send success
401                          */
402                         send_ldap_result( op, rs );
403                         Debug( LDAP_DEBUG_TRACE, "do_bind: v%d anonymous bind\n",
404                                 op->o_protocol, 0, 0 );
405                         goto cleanup;
406
407                 } else if ( SLAPD_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";
411
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 );
416                         goto cleanup;
417                 }
418
419 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
420         } else if ( op->orb_method == LDAP_AUTH_KRBV41 ) {
421                 if ( SLAPD_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";
425
426                         send_ldap_result( op, rs );
427
428                         Debug( LDAP_DEBUG_TRACE,
429                                 "do_bind: v%d Kerberos V4 (step 1) bind refused\n",
430                                 op->o_protocol, 0, 0 );
431                         goto cleanup;
432                 }
433                 BER_BVSTR( &mech, "KRBV4" );
434
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 );
439
440                 Debug( LDAP_DEBUG_TRACE,
441                         "do_bind: v%d Kerberos V4 (step 2) bind refused\n",
442                         op->o_protocol, 0, 0 );
443                 goto cleanup;
444 #endif
445
446         } else {
447                 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
448                 rs->sr_text = "unknown authentication method";
449
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 );
454                 goto cleanup;
455         }
456
457         /*
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.
461          */
462
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 );
468                 goto cleanup;
469         }
470
471         /* check restrictions */
472         if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
473                 send_ldap_result( op, rs );
474                 goto cleanup;
475         }
476
477 #ifdef LDAP_SLAPI
478         if ( pb ) {
479                 int rc;
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) );
486
487                 rc = slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_PRE_BIND_FN, pb );
488
489                 Debug(LDAP_DEBUG_TRACE,
490                         "do_bind: Bind preoperation plugin returned %d.\n",
491                         rs->sr_err, 0, 0);
492
493                 switch ( rc ) {
494                 case SLAPI_BIND_SUCCESS:
495                         /* Continue with backend processing */
496                         break;
497                 case SLAPI_BIND_FAIL:
498                         /* Failure, server sends result */
499                         rs->sr_err = LDAP_INVALID_CREDENTIALS;
500                         send_ldap_result( op, rs );
501                         goto cleanup;
502                         break;
503                 case SLAPI_BIND_ANONYMOUS:
504                         /* SLAPI_BIND_ANONYMOUS is undocumented XXX */
505                 default:
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 )
509                         {
510                                 rs->sr_err = LDAP_OTHER;
511                         }
512
513                         BER_BVZERO( &op->orb_edn );
514
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 ) ) {
519                                         if ( rc == 1 ) {
520                                                 /* No plugins were called; continue. */
521                                                 break;
522                                         }
523                                 } else {
524                                         op->orb_edn.bv_len = strlen( op->orb_edn.bv_val );
525                                 }
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 ( !BER_BVISEMPTY( &op->o_conn->c_dn ) ) {
536                                         ber_len_t max = SLAPD_GLOBAL(sockbuf_max_incoming_auth);
537                                         ber_sockbuf_ctrl( op->o_conn->c_sb,
538                                                 LBER_SB_OPT_SET_MAX_INCOMING, &max );
539                                 }
540                                 /* log authorization identity */
541                                 Statslog( LDAP_DEBUG_STATS,
542                                         "%s BIND dn=\"%s\" mech=%s (SLAPI) ssf=0\n",
543                                         op->o_log_prefix,
544                                         BER_BVISNULL( &op->o_conn->c_dn )
545                                                 ? "<empty>" : op->o_conn->c_dn.bv_val,
546                                         mech.bv_val, 0, 0 );
547                                 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
548                         }
549                         goto cleanup;
550                         break;
551                 }
552         }
553 #endif /* LDAP_SLAPI */
554
555         if( op->o_bd->be_bind ) {
556                 rs->sr_err = (op->o_bd->be_bind)( op, rs );
557
558                 if ( rs->sr_err == 0 ) {
559                         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
560
561                         if( op->o_conn->c_authz_backend == NULL ) {
562                                 op->o_conn->c_authz_backend = op->o_bd;
563                         }
564
565                         /* be_bind returns regular/global edn */
566                         if( !BER_BVISEMPTY( &op->orb_edn ) ) {
567                                 op->o_conn->c_dn = op->orb_edn;
568                         } else {
569                                 ber_dupbv(&op->o_conn->c_dn, &op->o_req_dn);
570                         }
571
572                         ber_dupbv( &op->o_conn->c_ndn, &op->o_req_ndn );
573
574                         if( !BER_BVISEMPTY( &op->o_conn->c_dn ) ) {
575                                 ber_len_t max = SLAPD_GLOBAL(sockbuf_max_incoming_auth);
576                                 ber_sockbuf_ctrl( op->o_conn->c_sb,
577                                         LBER_SB_OPT_SET_MAX_INCOMING, &max );
578                         }
579
580                         /* log authorization identity */
581                         Statslog( LDAP_DEBUG_STATS,
582                                 "%s BIND dn=\"%s\" mech=%s ssf=0\n",
583                                 op->o_log_prefix,
584                                 op->o_conn->c_dn.bv_val, mech.bv_val, 0, 0 );
585
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 );
589
590                         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
591
592                         /* send this here to avoid a race condition */
593                         send_ldap_result( op, rs );
594
595                 } else if ( !BER_BVISNULL( &op->orb_edn ) ) {
596                         free( op->orb_edn.bv_val );
597                 }
598
599         } else {
600                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
601                         "operation not supported within naming context" );
602         }
603
604 #ifdef LDAP_SLAPI
605         if ( pb != NULL &&
606                 slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_POST_BIND_FN, pb ) < 0 )
607         {
608                 Debug(LDAP_DEBUG_TRACE,
609                         "do_bind: Bind postoperation plugins failed.\n",
610                         0, 0, 0);
611         }
612 #endif /* LDAP_SLAPI */
613
614 cleanup:;
615         return rs->sr_err;
616 }
617