]> git.sur5r.net Git - openldap/blob - servers/slapd/bind.c
Tweak entry caching: only maintain LRU list on cached entries, not
[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 = NULL;
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 ) {
63                 be = op->o_conn->c_authz_backend;
64         }
65         if ( op->o_conn->c_dn.bv_len ) {
66                 /* log authorization identity demotion */
67                 Statslog( LDAP_DEBUG_STATS,
68                         "conn=%lu op=%lu BIND anonymous mech=implicit ssf=0\n",
69                         op->o_connid, op->o_opid, 0, 0, 0 );
70         }
71         connection2anonymous( op->o_conn );
72         if ( op->o_conn->c_sasl_bind_in_progress ) {
73                 op->o_conn->c_authz_backend = be;
74         }
75         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
76         if ( op->o_dn.bv_val != NULL ) {
77                 free( op->o_dn.bv_val );
78                 op->o_dn.bv_val = ch_strdup( "" );
79                 op->o_dn.bv_len = 0;
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, "decoding error" );
149                 rs->sr_err = SLAPD_DISCONNECT;
150                 goto cleanup;
151         }
152
153         if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
154 #ifdef NEW_LOGGING
155                 LDAP_LOG( OPERATION, INFO, 
156                         "do_bind: conn %d  get_ctrls failed\n", op->o_connid, 0, 0 );
157 #else
158                 Debug( LDAP_DEBUG_ANY, "do_bind: get_ctrls failed\n", 0, 0, 0 );
159 #endif
160                 goto cleanup;
161         } 
162
163         /* We use the tmpmemctx here because it speeds up normalization.
164          * However, we must dup with regular malloc when storing any
165          * resulting DNs in the op or conn structures.
166          */
167         rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn,
168                 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,
197                         (unsigned long) method );
198 #else
199                 Debug( LDAP_DEBUG_TRACE,
200                         "do_bind: version=%ld dn=\"%s\" method=%ld\n",
201                         (unsigned long) version, op->o_req_dn.bv_val,
202                         (unsigned long) method );
203 #endif
204         }
205
206         Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu BIND dn=\"%s\" method=%ld\n",
207             op->o_connid, op->o_opid, op->o_req_dn.bv_val, (unsigned long) method,
208                 0 );
209
210         if ( version < LDAP_VERSION_MIN || version > LDAP_VERSION_MAX ) {
211 #ifdef NEW_LOGGING
212                 LDAP_LOG( OPERATION, INFO, 
213                         "do_bind: conn %d  unknown version = %ld\n",
214                         op->o_connid, (unsigned long)version, 0 );
215 #else
216                 Debug( LDAP_DEBUG_ANY, "do_bind: unknown version=%ld\n",
217                         (unsigned long) version, 0, 0 );
218 #endif
219                 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
220                         "requested protocol version not supported" );
221                 goto cleanup;
222
223         } else if (!( global_allows & SLAP_ALLOW_BIND_V2 ) &&
224                 version < LDAP_VERSION3 )
225         {
226                 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
227                         "historical protocol version requested, use LDAPv3 instead" );
228                 goto cleanup;
229         }
230
231         /*
232          * we set connection version regardless of whether bind succeeds or not.
233          */
234         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
235         op->o_conn->c_protocol = version;
236         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
237
238         /* check for inappropriate controls */
239         if( get_manageDSAit( op ) == SLAP_CRITICAL_CONTROL ) {
240                 send_ldap_error( op, rs,
241                         LDAP_UNAVAILABLE_CRITICAL_EXTENSION,
242                         "manageDSAit control inappropriate" );
243                 goto cleanup;
244         }
245
246         /* Set the bindop for the benefit of in-directory SASL lookups */
247         op->o_conn->c_sasl_bindop = op;
248
249         if ( method == LDAP_AUTH_SASL ) {
250                 if ( version < LDAP_VERSION3 ) {
251 #ifdef NEW_LOGGING
252                         LDAP_LOG( OPERATION, INFO, 
253                                 "do_bind: conn %d  sasl with LDAPv%ld\n",
254                                 op->o_connid, (unsigned long)version , 0 );
255 #else
256                         Debug( LDAP_DEBUG_ANY, "do_bind: sasl with LDAPv%ld\n",
257                                 (unsigned long) version, 0, 0 );
258 #endif
259                         send_ldap_discon( op, rs,
260                                 LDAP_PROTOCOL_ERROR, "SASL bind requires LDAPv3" );
261                         rs->sr_err = SLAPD_DISCONNECT;
262                         goto cleanup;
263                 }
264
265                 if( mech.bv_len == 0 ) {
266 #ifdef NEW_LOGGING
267                         LDAP_LOG( OPERATION, INFO, 
268                                    "do_bind: conn %d  no SASL mechanism provided\n",
269                                    op->o_connid, 0, 0 );
270 #else
271                         Debug( LDAP_DEBUG_ANY,
272                                 "do_bind: no sasl mechanism provided\n",
273                                 0, 0, 0 );
274 #endif
275                         send_ldap_error( op, rs, LDAP_AUTH_METHOD_NOT_SUPPORTED,
276                                 "no SASL mechanism provided" );
277                         goto cleanup;
278                 }
279
280                 /* check restrictions */
281                 if( backend_check_restrictions( op, rs, &mech ) != LDAP_SUCCESS ) {
282                         send_ldap_result( op, rs );
283                         goto cleanup;
284                 }
285
286                 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
287                 if ( op->o_conn->c_sasl_bind_in_progress ) {
288                         if( !bvmatch( &op->o_conn->c_sasl_bind_mech, &mech ) ) {
289                                 /* mechanism changed between bind steps */
290                                 slap_sasl_reset(op->o_conn);
291                         }
292                 } else {
293                         ber_dupbv(&op->o_conn->c_sasl_bind_mech, &mech);
294                 }
295                 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
296
297                 rs->sr_err = slap_sasl_bind( op, rs );
298
299                 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
300                 if( rs->sr_err == LDAP_SUCCESS ) {
301                         ber_dupbv(&op->o_conn->c_dn, &op->orb_edn);
302                         if( op->orb_edn.bv_len != 0 ) {
303                                 /* edn is always normalized already */
304                                 ber_dupbv( &op->o_conn->c_ndn, &op->o_conn->c_dn );
305                         }
306                         op->o_tmpfree( op->orb_edn.bv_val, op->o_tmpmemctx );
307                         op->orb_edn.bv_val = NULL;
308                         op->orb_edn.bv_len = 0;
309                         op->o_conn->c_authmech = op->o_conn->c_sasl_bind_mech;
310                         op->o_conn->c_sasl_bind_mech.bv_val = NULL;
311                         op->o_conn->c_sasl_bind_mech.bv_len = 0;
312                         op->o_conn->c_sasl_bind_in_progress = 0;
313
314                         op->o_conn->c_sasl_ssf = op->orb_ssf;
315                         if( op->orb_ssf > op->o_conn->c_ssf ) {
316                                 op->o_conn->c_ssf = op->orb_ssf;
317                         }
318
319                         if( op->o_conn->c_dn.bv_len != 0 ) {
320                                 ber_len_t max = sockbuf_max_incoming_auth;
321                                 ber_sockbuf_ctrl( op->o_conn->c_sb,
322                                         LBER_SB_OPT_SET_MAX_INCOMING, &max );
323                         }
324
325                         /* log authorization identity */
326                         Statslog( LDAP_DEBUG_STATS,
327                                 "conn=%lu op=%lu BIND dn=\"%s\" mech=%s ssf=%d\n",
328                                 op->o_connid, op->o_opid,
329                                 op->o_conn->c_dn.bv_val ? op->o_conn->c_dn.bv_val : "<empty>",
330                                 op->o_conn->c_authmech.bv_val, op->orb_ssf );
331
332 #ifdef NEW_LOGGING
333                         LDAP_LOG( OPERATION, DETAIL1, 
334                                 "do_bind: SASL/%s bind: dn=\"%s\" ssf=%d\n",
335                                 op->o_conn->c_authmech.bv_val,
336                                 op->o_conn->c_dn.bv_val ? op->o_conn->c_dn.bv_val : "<empty>",
337                                 op->orb_ssf );
338 #else
339                         Debug( LDAP_DEBUG_TRACE,
340                                 "do_bind: SASL/%s bind: dn=\"%s\" ssf=%d\n",
341                                 op->o_conn->c_authmech.bv_val,
342                                 op->o_conn->c_dn.bv_val ? op->o_conn->c_dn.bv_val : "<empty>",
343                                 op->orb_ssf );
344 #endif
345
346                 } else if ( rs->sr_err == LDAP_SASL_BIND_IN_PROGRESS ) {
347                         op->o_conn->c_sasl_bind_in_progress = 1;
348
349                 } else {
350                         if ( op->o_conn->c_sasl_bind_mech.bv_val ) {
351                                 free( op->o_conn->c_sasl_bind_mech.bv_val );
352                                 op->o_conn->c_sasl_bind_mech.bv_val = NULL;
353                                 op->o_conn->c_sasl_bind_mech.bv_len = 0;
354                         }
355                         op->o_conn->c_sasl_bind_in_progress = 0;
356                 }
357
358 #ifdef LDAP_SLAPI
359                 /*
360                  * Normally post-operation plugins are called only after the
361                  * backend operation. Because the front-end performs SASL
362                  * binds on behalf of the backend, we'll make a special
363                  * exception to call the post-operation plugins after a
364                  * SASL bind.
365                  */
366                 slapi_x_pblock_set_operation( pb, op );
367                 slapi_pblock_set( pb, SLAPI_BIND_TARGET, (void *)dn.bv_val );
368                 slapi_pblock_set( pb, SLAPI_BIND_METHOD, (void *)method );
369                 slapi_pblock_set( pb, SLAPI_BIND_CREDENTIALS, (void *)&op->orb_cred );
370                 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)(0) );
371                 (void) doPluginFNs( op->o_bd, SLAPI_PLUGIN_POST_BIND_FN, pb );
372 #endif /* LDAP_SLAPI */
373
374                 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
375
376                 goto cleanup;
377
378         } else {
379                 /* Not SASL, cancel any in-progress bind */
380                 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
381
382                 if ( op->o_conn->c_sasl_bind_mech.bv_val != NULL ) {
383                         free(op->o_conn->c_sasl_bind_mech.bv_val);
384                         op->o_conn->c_sasl_bind_mech.bv_val = NULL;
385                         op->o_conn->c_sasl_bind_mech.bv_len = 0;
386                 }
387                 op->o_conn->c_sasl_bind_in_progress = 0;
388
389                 slap_sasl_reset( op->o_conn );
390                 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
391         }
392
393         if ( method == LDAP_AUTH_SIMPLE ) {
394                 /* accept "anonymous" binds */
395                 if ( op->orb_cred.bv_len == 0 || op->o_req_ndn.bv_len == 0 ) {
396                         rs->sr_err = LDAP_SUCCESS;
397
398                         if( op->orb_cred.bv_len &&
399                                 !( global_allows & SLAP_ALLOW_BIND_ANON_CRED ))
400                         {
401                                 /* cred is not empty, disallow */
402                                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
403
404                         } else if ( op->o_req_ndn.bv_len &&
405                                 !( global_allows & SLAP_ALLOW_BIND_ANON_DN ))
406                         {
407                                 /* DN is not empty, disallow */
408                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
409                                 rs->sr_text =
410                                         "unauthenticated bind (DN with no password) disallowed";
411
412                         } else if ( global_disallows & SLAP_DISALLOW_BIND_ANON ) {
413                                 /* disallow */
414                                 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
415                                 rs->sr_text = "anonymous bind disallowed";
416
417                         } else {
418                                 backend_check_restrictions( op, rs, &mech );
419                         }
420
421                         /*
422                          * we already forced connection to "anonymous",
423                          * just need to send success
424                          */
425                         send_ldap_result( op, rs );
426 #ifdef NEW_LOGGING
427                         LDAP_LOG( OPERATION, DETAIL1, 
428                                    "do_bind: conn %d  v%d anonymous bind\n",
429                                    op->o_connid, version , 0 );
430 #else
431                         Debug( LDAP_DEBUG_TRACE, "do_bind: v%d anonymous bind\n",
432                                 version, 0, 0 );
433 #endif
434                         goto cleanup;
435
436                 } else if ( global_disallows & SLAP_DISALLOW_BIND_SIMPLE ) {
437                         /* disallow simple authentication */
438                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
439                         rs->sr_text = "unwilling to perform simple authentication";
440
441                         send_ldap_result( op, rs );
442 #ifdef NEW_LOGGING
443                         LDAP_LOG( OPERATION, INFO, 
444                                 "do_bind: conn %d  v%d 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 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
532 #ifdef NEW_LOGGING
533         LDAP_LOG( OPERATION, INFO,
534                 "do_bind: Bind preoperation plugin returned %d\n",
535                 rs->sr_err, 0, 0);
536 #else
537         Debug(LDAP_DEBUG_TRACE,
538                 "do_bind: Bind preoperation plugin returned %d.\n",
539                 rs->sr_err, 0, 0);
540 #endif
541
542         switch ( rs->sr_err ) {
543         case SLAPI_BIND_SUCCESS:
544                 /* Continue with backend processing */
545                 break;
546         case SLAPI_BIND_FAIL:
547                 /* Failure, server sends result */
548                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
549                 send_ldap_result( op, rs );
550                 goto cleanup;
551                 break;
552         case SLAPI_BIND_ANONYMOUS:
553                 /* SLAPI_BIND_ANONYMOUS is undocumented XXX */
554         default:
555                 /* Authoritative, plugin sent result */
556                 if ( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE,
557                         (void *)&rs->sr_err) != 0 )
558                 {
559                         rs->sr_err = LDAP_OTHER;
560                 }
561
562                 op->orb_edn.bv_val = NULL;
563                 op->orb_edn.bv_len = 0;
564
565                 if ( rs->sr_err == LDAP_SUCCESS ) {
566                         slapi_pblock_get( pb, SLAPI_CONN_DN, (void *)&op->orb_edn.bv_val );
567                         if ( op->orb_edn.bv_val != NULL ) {
568                                 op->orb_edn.bv_len = strlen( op->orb_edn.bv_val );
569                         }
570                         rs->sr_err = dnPrettyNormal( NULL, &op->orb_edn,
571                                 &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
572                         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
573                         ber_dupbv(&op->o_conn->c_dn, &op->o_req_dn);
574                         ber_dupbv(&op->o_conn->c_ndn, &op->o_req_ndn);
575                         op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
576                         op->o_req_dn.bv_val = NULL;
577                         op->o_req_dn.bv_len = 0;
578                         op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
579                         op->o_req_ndn.bv_val = NULL;
580                         op->o_req_ndn.bv_len = 0;
581                         if ( op->o_conn->c_dn.bv_len != 0 ) {
582                                 ber_len_t max = sockbuf_max_incoming_auth;
583                                 ber_sockbuf_ctrl( op->o_conn->c_sb,
584                                         LBER_SB_OPT_SET_MAX_INCOMING, &max );
585                         }
586                         /* log authorization identity */
587                         Statslog( LDAP_DEBUG_STATS,
588                                 "conn=%lu op=%lu BIND dn=\"%s\" mech=simple (SLAPI) ssf=0\n",
589                                 op->o_connid, op->o_opid,
590                                 op->o_conn->c_dn.bv_val ? op->o_conn->c_dn.bv_val : "<empty>",
591                                 0, 0 );
592                         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
593                 }
594                 goto cleanup;
595                 break;
596         }
597 #endif /* defined( LDAP_SLAPI ) */
598
599         if( op->o_bd->be_bind ) {
600                 op->orb_method = method;
601                 rs->sr_err = (op->o_bd->be_bind)( op, rs );
602
603                 if ( rs->sr_err == 0 ) {
604                         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
605
606                         if( op->o_conn->c_authz_backend == NULL ) {
607                                 op->o_conn->c_authz_backend = op->o_bd;
608                         }
609
610                         /* be_bind returns regular/global edn */
611                         if( op->orb_edn.bv_len ) {
612                                 op->o_conn->c_dn = op->orb_edn;
613                         } else {
614                                 ber_dupbv(&op->o_conn->c_dn, &op->o_req_dn);
615                         }
616
617                         ber_dupbv( &op->o_conn->c_ndn, &op->o_req_ndn );
618
619                         if( op->o_conn->c_dn.bv_len != 0 ) {
620                                 ber_len_t max = sockbuf_max_incoming_auth;
621                                 ber_sockbuf_ctrl( op->o_conn->c_sb,
622                                         LBER_SB_OPT_SET_MAX_INCOMING, &max );
623                         }
624
625                         /* log authorization identity */
626                         Statslog( LDAP_DEBUG_STATS,
627                                 "conn=%lu op=%lu BIND dn=\"%s\" mech=simple ssf=0\n",
628                                 op->o_connid, op->o_opid,
629                                 op->o_conn->c_dn.bv_val, 0, 0 );
630
631 #ifdef NEW_LOGGING
632                         LDAP_LOG( OPERATION, DETAIL1, 
633                                 "do_bind: v%d bind: \"%s\" to \"%s\" \n",
634                                 version, op->o_conn->c_dn.bv_val, op->o_conn->c_dn.bv_val );
635 #else
636                         Debug( LDAP_DEBUG_TRACE,
637                                 "do_bind: v%d bind: \"%s\" to \"%s\"\n",
638                                 version, dn.bv_val, op->o_conn->c_dn.bv_val );
639 #endif
640
641                         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
642
643                         /* send this here to avoid a race condition */
644                         send_ldap_result( op, rs );
645
646                 } else if (op->orb_edn.bv_val != NULL) {
647                         free( op->orb_edn.bv_val );
648                 }
649
650         } else {
651                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
652                         "operation not supported within naming context" );
653         }
654
655 #if defined( LDAP_SLAPI )
656         if ( doPluginFNs( op->o_bd, SLAPI_PLUGIN_POST_BIND_FN, pb ) < 0 ) {
657 #ifdef NEW_LOGGING
658                 LDAP_LOG( OPERATION, INFO,
659                         "do_bind: Bind postoperation plugins failed\n",
660                         0, 0, 0);
661 #else
662                 Debug(LDAP_DEBUG_TRACE,
663                         "do_bind: Bind postoperation plugins failed.\n",
664                         0, 0, 0);
665 #endif
666         }
667 #endif /* defined( LDAP_SLAPI ) */
668
669 cleanup:
670
671         op->o_conn->c_sasl_bindop = NULL;
672
673         if( op->o_req_dn.bv_val != NULL ) {
674                 sl_free( op->o_req_dn.bv_val, op->o_tmpmemctx );
675                 op->o_req_dn.bv_val = NULL;
676         }
677         if( op->o_req_ndn.bv_val != NULL ) {
678                 sl_free( op->o_req_ndn.bv_val, op->o_tmpmemctx );
679                 op->o_req_ndn.bv_val = NULL;
680         }
681
682         return rs->sr_err;
683 }