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