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