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