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