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