]> git.sur5r.net Git - openldap/blob - servers/slapd/bind.c
eb5ead1044627c1ca0e5fb91d69de452896b9533
[openldap] / servers / slapd / bind.c
1 /* bind.c - decode an ldap bind operation and pass it to a backend db */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2004 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17  * All rights reserved.
18  *
19  * Redistribution and use in source and binary forms are permitted
20  * provided that this notice is preserved and that due credit is given
21  * to the University of Michigan at Ann Arbor. The name of the University
22  * may not be used to endorse or promote products derived from this
23  * software without specific prior written permission. This software
24  * is provided ``as is'' without express or implied warranty.
25  */
26
27 #include "portable.h"
28
29 #include <stdio.h>
30
31 #include <ac/string.h>
32 #include <ac/socket.h>
33
34 #include "ldap_pvt.h"
35 #include "slap.h"
36 #ifdef LDAP_SLAPI
37 #include "slapi/slapi.h"
38 #endif
39
40
41 int
42 do_bind(
43     Operation   *op,
44     SlapReply   *rs
45 )
46 {
47         BerElement *ber = op->o_ber;
48         ber_int_t version;
49         ber_tag_t method;
50         struct berval mech = BER_BVNULL;
51         struct berval dn = BER_BVNULL;
52         ber_tag_t tag;
53         Backend *be = NULL;
54
55 #ifdef NEW_LOGGING
56         LDAP_LOG( OPERATION, ENTRY, "do_bind: conn %d\n", op->o_connid, 0, 0 );
57 #else
58         Debug( LDAP_DEBUG_TRACE, "do_bind\n", 0, 0, 0 );
59 #endif
60
61         /*
62          * Force to connection to "anonymous" until bind succeeds.
63          */
64         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
65         if ( op->o_conn->c_sasl_bind_in_progress ) {
66                 be = op->o_conn->c_authz_backend;
67         }
68         if ( op->o_conn->c_dn.bv_len ) {
69                 /* log authorization identity demotion */
70                 Statslog( LDAP_DEBUG_STATS,
71                         "conn=%lu op=%lu BIND anonymous mech=implicit ssf=0\n",
72                         op->o_connid, op->o_opid, 0, 0, 0 );
73         }
74         connection2anonymous( op->o_conn );
75         if ( op->o_conn->c_sasl_bind_in_progress ) {
76                 op->o_conn->c_authz_backend = be;
77         }
78         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
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         if ( op->o_ndn.bv_val != NULL ) {
85                 free( op->o_ndn.bv_val );
86                 op->o_ndn.bv_val = ch_strdup( "" );
87                 op->o_ndn.bv_len = 0;
88         }
89
90         /*
91          * Parse the bind request.  It looks like this:
92          *
93          *      BindRequest ::= SEQUENCE {
94          *              version         INTEGER,                 -- version
95          *              name            DistinguishedName,       -- dn
96          *              authentication  CHOICE {
97          *                      simple          [0] OCTET STRING -- passwd
98          *                      krbv42ldap      [1] OCTET STRING
99          *                      krbv42dsa       [2] OCTET STRING
100          *                      SASL            [3] SaslCredentials
101          *              }
102          *      }
103          *
104          *      SaslCredentials ::= SEQUENCE {
105          *              mechanism           LDAPString,
106          *              credentials         OCTET STRING OPTIONAL
107          *      }
108          */
109
110         tag = ber_scanf( ber, "{imt" /*}*/, &version, &dn, &method );
111
112         if ( tag == LBER_ERROR ) {
113 #ifdef NEW_LOGGING
114                 LDAP_LOG( OPERATION, ERR, 
115                         "do_bind: conn %d  ber_scanf failed\n", op->o_connid, 0, 0 );
116 #else
117                 Debug( LDAP_DEBUG_ANY, "bind: ber_scanf failed\n", 0, 0, 0 );
118 #endif
119                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
120                 rs->sr_err = -1;
121                 goto cleanup;
122         }
123
124         op->o_protocol = version;
125         op->orb_method = method;
126
127         if( op->orb_method != LDAP_AUTH_SASL ) {
128                 tag = ber_scanf( ber, /*{*/ "m}", &op->orb_cred );
129
130         } else {
131                 tag = ber_scanf( ber, "{m" /*}*/, &mech );
132
133                 if ( tag != LBER_ERROR ) {
134                         ber_len_t len;
135                         tag = ber_peek_tag( ber, &len );
136
137                         if ( tag == LDAP_TAG_LDAPCRED ) { 
138                                 tag = ber_scanf( ber, "m", &op->orb_cred );
139                         } else {
140                                 tag = LDAP_TAG_LDAPCRED;
141                                 op->orb_cred.bv_val = NULL;
142                                 op->orb_cred.bv_len = 0;
143                         }
144
145                         if ( tag != LBER_ERROR ) {
146                                 tag = ber_scanf( ber, /*{{*/ "}}" );
147                         }
148                 }
149         }
150
151         if ( tag == LBER_ERROR ) {
152                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
153                 rs->sr_err = SLAPD_DISCONNECT;
154                 goto cleanup;
155         }
156
157         if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
158 #ifdef NEW_LOGGING
159                 LDAP_LOG( OPERATION, INFO, 
160                         "do_bind: conn %d  get_ctrls failed\n", op->o_connid, 0, 0 );
161 #else
162                 Debug( LDAP_DEBUG_ANY, "do_bind: get_ctrls failed\n", 0, 0, 0 );
163 #endif
164                 goto cleanup;
165         } 
166
167         /* We use the tmpmemctx here because it speeds up normalization.
168          * However, we must dup with regular malloc when storing any
169          * resulting DNs in the op or conn structures.
170          */
171         rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn,
172                 op->o_tmpmemctx );
173         if ( rs->sr_err != LDAP_SUCCESS ) {
174 #ifdef NEW_LOGGING
175                 LDAP_LOG( OPERATION, INFO, 
176                         "do_bind: conn %d  invalid dn (%s)\n", 
177                         op->o_connid, dn.bv_val, 0 );
178 #else
179                 Debug( LDAP_DEBUG_ANY, "bind: invalid dn (%s)\n",
180                         dn.bv_val, 0, 0 );
181 #endif
182                 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
183                 goto cleanup;
184         }
185
186         if( op->orb_method == LDAP_AUTH_SASL ) {
187 #ifdef NEW_LOGGING
188                 LDAP_LOG( OPERATION,     DETAIL1, 
189                         "do_sasl_bind: conn %d  dn (%s) mech %s\n", 
190                         op->o_connid, op->o_req_dn.bv_val, mech.bv_val );
191 #else
192                 Debug( LDAP_DEBUG_TRACE, "do_sasl_bind: dn (%s) mech %s\n",
193                         op->o_req_dn.bv_val, mech.bv_val, NULL );
194 #endif
195
196         } else {
197 #ifdef NEW_LOGGING
198                 LDAP_LOG( OPERATION, DETAIL1, 
199                         "do_bind: version=%ld dn=\"%s\" method=%ld\n",
200                         (unsigned long) version, op->o_req_dn.bv_val,
201                         (unsigned long) op->orb_method );
202 #else
203                 Debug( LDAP_DEBUG_TRACE,
204                         "do_bind: version=%ld dn=\"%s\" method=%ld\n",
205                         (unsigned long) version, op->o_req_dn.bv_val,
206                         (unsigned long) op->orb_method );
207 #endif
208         }
209
210         Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu BIND dn=\"%s\" method=%ld\n",
211             op->o_connid, op->o_opid, op->o_req_dn.bv_val,
212                 (unsigned long) op->orb_method, 0 );
213
214         if ( version < LDAP_VERSION_MIN || version > LDAP_VERSION_MAX ) {
215 #ifdef NEW_LOGGING
216                 LDAP_LOG( OPERATION, INFO, 
217                         "do_bind: conn %d  unknown version = %ld\n",
218                         op->o_connid, (unsigned long)version, 0 );
219 #else
220                 Debug( LDAP_DEBUG_ANY, "do_bind: unknown version=%ld\n",
221                         (unsigned long) version, 0, 0 );
222 #endif
223                 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
224                         "requested protocol version not supported" );
225                 goto cleanup;
226
227         } else if (!( global_allows & SLAP_ALLOW_BIND_V2 ) &&
228                 version < LDAP_VERSION3 )
229         {
230                 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
231                         "historical protocol version requested, use LDAPv3 instead" );
232                 goto cleanup;
233         }
234
235         /*
236          * we set connection version regardless of whether bind succeeds or not.
237          */
238         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
239         op->o_conn->c_protocol = version;
240         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
241
242         /* check for inappropriate controls */
243         if( get_manageDSAit( op ) == SLAP_CRITICAL_CONTROL ) {
244                 send_ldap_error( op, rs,
245                         LDAP_UNAVAILABLE_CRITICAL_EXTENSION,
246                         "manageDSAit control inappropriate" );
247                 goto cleanup;
248         }
249
250         /* Set the bindop for the benefit of in-directory SASL lookups */
251         op->o_conn->c_sasl_bindop = op;
252
253         if ( op->orb_method == LDAP_AUTH_SASL ) {
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                                 op->o_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_discon( op, rs,
264                                 LDAP_PROTOCOL_ERROR, "SASL bind requires LDAPv3" );
265                         rs->sr_err = 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                                    op->o_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_error( op, rs, LDAP_AUTH_METHOD_NOT_SUPPORTED,
280                                 "no SASL mechanism provided" );
281                         goto cleanup;
282                 }
283
284                 /* check restrictions */
285                 if( backend_check_restrictions( op, rs, &mech ) != LDAP_SUCCESS ) {
286                         send_ldap_result( op, rs );
287                         goto cleanup;
288                 }
289
290                 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
291                 if ( op->o_conn->c_sasl_bind_in_progress ) {
292                         if( !bvmatch( &op->o_conn->c_sasl_bind_mech, &mech ) ) {
293                                 /* mechanism changed between bind steps */
294                                 slap_sasl_reset(op->o_conn);
295                         }
296                 } else {
297                         ber_dupbv(&op->o_conn->c_sasl_bind_mech, &mech);
298                 }
299                 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
300
301                 rs->sr_err = slap_sasl_bind( op, rs );
302
303                 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
304                 if( rs->sr_err == LDAP_SUCCESS ) {
305                         ber_dupbv(&op->o_conn->c_dn, &op->orb_edn);
306                         if( op->orb_edn.bv_len != 0 ) {
307                                 /* edn is always normalized already */
308                                 ber_dupbv( &op->o_conn->c_ndn, &op->o_conn->c_dn );
309                         }
310                         op->o_tmpfree( op->orb_edn.bv_val, op->o_tmpmemctx );
311                         BER_BVZERO( &op->orb_edn );
312                         op->o_conn->c_authmech = op->o_conn->c_sasl_bind_mech;
313                         BER_BVZERO( &op->o_conn->c_sasl_bind_mech );
314                         op->o_conn->c_sasl_bind_in_progress = 0;
315
316                         op->o_conn->c_sasl_ssf = op->orb_ssf;
317                         if( op->orb_ssf > op->o_conn->c_ssf ) {
318                                 op->o_conn->c_ssf = op->orb_ssf;
319                         }
320
321                         if( op->o_conn->c_dn.bv_len != 0 ) {
322                                 ber_len_t max = sockbuf_max_incoming_auth;
323                                 ber_sockbuf_ctrl( op->o_conn->c_sb,
324                                         LBER_SB_OPT_SET_MAX_INCOMING, &max );
325                         }
326
327                         /* log authorization identity */
328                         Statslog( LDAP_DEBUG_STATS,
329                                 "conn=%lu op=%lu BIND dn=\"%s\" mech=%s ssf=%d\n",
330                                 op->o_connid, op->o_opid,
331                                 op->o_conn->c_dn.bv_val ? op->o_conn->c_dn.bv_val : "<empty>",
332                                 op->o_conn->c_authmech.bv_val, op->orb_ssf );
333
334 #ifdef NEW_LOGGING
335                         LDAP_LOG( OPERATION, DETAIL1, 
336                                 "do_bind: SASL/%s bind: dn=\"%s\" ssf=%d\n",
337                                 op->o_conn->c_authmech.bv_val,
338                                 op->o_conn->c_dn.bv_val ? op->o_conn->c_dn.bv_val : "<empty>",
339                                 op->orb_ssf );
340 #else
341                         Debug( LDAP_DEBUG_TRACE,
342                                 "do_bind: SASL/%s bind: dn=\"%s\" ssf=%d\n",
343                                 op->o_conn->c_authmech.bv_val,
344                                 op->o_conn->c_dn.bv_val ? op->o_conn->c_dn.bv_val : "<empty>",
345                                 op->orb_ssf );
346 #endif
347
348                 } else if ( rs->sr_err == LDAP_SASL_BIND_IN_PROGRESS ) {
349                         op->o_conn->c_sasl_bind_in_progress = 1;
350
351                 } else {
352                         if ( op->o_conn->c_sasl_bind_mech.bv_val ) {
353                                 free( op->o_conn->c_sasl_bind_mech.bv_val );
354                                 op->o_conn->c_sasl_bind_mech.bv_val = NULL;
355                                 op->o_conn->c_sasl_bind_mech.bv_len = 0;
356                         }
357                         op->o_conn->c_sasl_bind_in_progress = 0;
358                 }
359
360 #ifdef LDAP_SLAPI
361 #define pb      op->o_pb
362                 /*
363                  * Normally post-operation plugins are called only after the
364                  * backend operation. Because the front-end performs SASL
365                  * binds on behalf of the backend, we'll make a special
366                  * exception to call the post-operation plugins after a
367                  * SASL bind.
368                  */
369                 if ( pb ) {
370                         slapi_int_pblock_set_operation( pb, op );
371                         slapi_pblock_set( pb, SLAPI_BIND_TARGET, (void *)dn.bv_val );
372                         slapi_pblock_set( pb, SLAPI_BIND_METHOD, (void *)op->orb_method );
373                         slapi_pblock_set( pb, SLAPI_BIND_CREDENTIALS, (void *)&op->orb_cred );
374                         slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)(0) );
375                         (void) slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_POST_BIND_FN, pb );
376                 }
377 #endif /* LDAP_SLAPI */
378
379                 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
380
381                 goto cleanup;
382
383         } else {
384                 /* Not SASL, cancel any in-progress bind */
385                 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
386
387                 if ( op->o_conn->c_sasl_bind_mech.bv_val != NULL ) {
388                         free(op->o_conn->c_sasl_bind_mech.bv_val);
389                         op->o_conn->c_sasl_bind_mech.bv_val = NULL;
390                         op->o_conn->c_sasl_bind_mech.bv_len = 0;
391                 }
392                 op->o_conn->c_sasl_bind_in_progress = 0;
393
394                 slap_sasl_reset( op->o_conn );
395                 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
396         }
397
398         if ( op->orb_method == LDAP_AUTH_SIMPLE ) {
399                 ber_str2bv( "SIMPLE", sizeof("SIMPLE")-1, 0, &mech );
400                 /* accept "anonymous" binds */
401                 if ( op->orb_cred.bv_len == 0 || op->o_req_ndn.bv_len == 0 ) {
402                         rs->sr_err = LDAP_SUCCESS;
403
404                         if( op->orb_cred.bv_len &&
405                                 !( global_allows & SLAP_ALLOW_BIND_ANON_CRED ))
406                         {
407                                 /* cred is not empty, disallow */
408                                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
409
410                         } else if ( op->o_req_ndn.bv_len &&
411                                 !( global_allows & SLAP_ALLOW_BIND_ANON_DN ))
412                         {
413                                 /* DN is not empty, disallow */
414                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
415                                 rs->sr_text =
416                                         "unauthenticated bind (DN with no password) disallowed";
417
418                         } else if ( global_disallows & SLAP_DISALLOW_BIND_ANON ) {
419                                 /* disallow */
420                                 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
421                                 rs->sr_text = "anonymous bind disallowed";
422
423                         } else {
424                                 backend_check_restrictions( op, rs, &mech );
425                         }
426
427                         /*
428                          * we already forced connection to "anonymous",
429                          * just need to send success
430                          */
431                         send_ldap_result( op, rs );
432 #ifdef NEW_LOGGING
433                         LDAP_LOG( OPERATION, DETAIL1, 
434                                 "do_bind: conn %d  v%d anonymous bind\n",
435                                 op->o_connid, version , 0 );
436 #else
437                         Debug( LDAP_DEBUG_TRACE, "do_bind: v%d anonymous bind\n",
438                                 version, 0, 0 );
439 #endif
440                         goto cleanup;
441
442                 } else if ( global_disallows & SLAP_DISALLOW_BIND_SIMPLE ) {
443                         /* disallow simple authentication */
444                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
445                         rs->sr_text = "unwilling to perform simple authentication";
446
447                         send_ldap_result( op, rs );
448 #ifdef NEW_LOGGING
449                         LDAP_LOG( OPERATION, INFO, 
450                                 "do_bind: conn %d  v%d simple bind(%s) disallowed\n",
451                                 op->o_connid, version, op->o_req_ndn.bv_val );
452 #else
453                         Debug( LDAP_DEBUG_TRACE,
454                                 "do_bind: v%d simple bind(%s) disallowed\n",
455                                 version, op->o_req_ndn.bv_val, 0 );
456 #endif
457                         goto cleanup;
458                 }
459
460 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
461         } else if ( op->orb_method == LDAP_AUTH_KRBV41 ) {
462                 if ( global_disallows & SLAP_DISALLOW_BIND_KRBV4 ) {
463                         /* disallow krbv4 authentication */
464                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
465                         rs->sr_text = "unwilling to perform Kerberos V4 bind";
466
467                         send_ldap_result( op, rs );
468
469 #ifdef NEW_LOGGING
470                         LDAP_LOG( OPERATION, DETAIL1, 
471                                 "do_bind: conn %d  v%d Kerberos V4 (step 1) bind refused\n",
472                                 op->o_connid, version , 0 );
473 #else
474                         Debug( LDAP_DEBUG_TRACE,
475                                 "do_bind: v%d Kerberos V4 (step 1) bind refused\n",
476                                 version, 0, 0 );
477 #endif
478                         goto cleanup;
479                 }
480                 ber_str2bv( "KRBV4", sizeof("KRBV4")-1, 0, &mech );
481
482         } else if ( op->orb_method == LDAP_AUTH_KRBV42 ) {
483                 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
484                 rs->sr_text = "Kerberos V4 (step 2) bind not supported";
485                 send_ldap_result( op, rs );
486
487 #ifdef NEW_LOGGING
488                 LDAP_LOG( OPERATION, DETAIL1, 
489                         "do_bind: conn %d  v%d Kerberos V4 (step 2) bind refused\n",
490                         op->o_connid, version , 0 );
491 #else
492                 Debug( LDAP_DEBUG_TRACE,
493                         "do_bind: v%d Kerberos V4 (step 2) bind refused\n",
494                         version, 0, 0 );
495 #endif
496                 goto cleanup;
497 #endif
498
499         } else {
500                 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
501                 rs->sr_text = "unknown authentication method";
502
503                 send_ldap_result( op, rs );
504 #ifdef NEW_LOGGING
505                 LDAP_LOG( OPERATION, INFO, 
506                         "do_bind: conn %ld  v%d unknown authentication method (%ld)\n",
507                         op->o_connid, version, op->orb_method );
508 #else
509                 Debug( LDAP_DEBUG_TRACE,
510                         "do_bind: v%d unknown authentication method (%ld)\n",
511                         version, op->orb_method, 0 );
512 #endif
513                 goto cleanup;
514         }
515
516         /*
517          * We could be serving multiple database backends.  Select the
518          * appropriate one, or send a referral to our "referral server"
519          * if we don't hold it.
520          */
521
522         if ( (op->o_bd = select_backend( &op->o_req_ndn, 0, 0 )) == NULL ) {
523                 if ( default_referral ) {
524                         rs->sr_ref = referral_rewrite( default_referral,
525                                 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
526                         if (!rs->sr_ref) rs->sr_ref = default_referral;
527
528                         rs->sr_err = LDAP_REFERRAL;
529                         send_ldap_result( op, rs );
530
531                         if (rs->sr_ref != default_referral) ber_bvarray_free( rs->sr_ref );
532
533                 } else {
534                         /* noSuchObject is not allowed to be returned by bind */
535                         rs->sr_err = LDAP_INVALID_CREDENTIALS;
536                         send_ldap_result( op, rs );
537                 }
538
539                 goto cleanup;
540         }
541
542         /* check restrictions */
543         if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
544                 send_ldap_result( op, rs );
545                 goto cleanup;
546         }
547
548 #ifdef LDAP_SLAPI
549         if ( pb ) {
550                 int rc;
551                 slapi_int_pblock_set_operation( pb, op );
552                 slapi_pblock_set( pb, SLAPI_BIND_TARGET, (void *)dn.bv_val );
553                 slapi_pblock_set( pb, SLAPI_BIND_METHOD, (void *)op->orb_method );
554                 slapi_pblock_set( pb, SLAPI_BIND_CREDENTIALS, (void *)&op->orb_cred );
555                 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)(0) );
556                 slapi_pblock_set( pb, SLAPI_CONN_DN, (void *)(0) );
557
558                 rc = slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_PRE_BIND_FN, pb );
559
560 #ifdef NEW_LOGGING
561                 LDAP_LOG( OPERATION, INFO,
562                         "do_bind: Bind preoperation plugin returned %d\n",
563                         rs->sr_err, 0, 0);
564 #else
565                 Debug(LDAP_DEBUG_TRACE,
566                         "do_bind: Bind preoperation plugin returned %d.\n",
567                         rs->sr_err, 0, 0);
568 #endif
569
570                 switch ( rc ) {
571                 case SLAPI_BIND_SUCCESS:
572                         /* Continue with backend processing */
573                         break;
574                 case SLAPI_BIND_FAIL:
575                         /* Failure, server sends result */
576                         rs->sr_err = LDAP_INVALID_CREDENTIALS;
577                         send_ldap_result( op, rs );
578                         goto cleanup;
579                         break;
580                 case SLAPI_BIND_ANONYMOUS:
581                         /* SLAPI_BIND_ANONYMOUS is undocumented XXX */
582                 default:
583                         /* Authoritative, plugin sent result, or no plugins called. */
584                         if ( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE,
585                                 (void *)&rs->sr_err) != 0 )
586                         {
587                                 rs->sr_err = LDAP_OTHER;
588                         }
589
590                         op->orb_edn.bv_val = NULL;
591                         op->orb_edn.bv_len = 0;
592
593                         if ( rs->sr_err == LDAP_SUCCESS ) {
594                                 slapi_pblock_get( pb, SLAPI_CONN_DN,
595                                         (void *)&op->orb_edn.bv_val );
596                                 if ( op->orb_edn.bv_val == NULL ) {
597                                         if ( rc == 1 ) {
598                                                 /* No plugins were called; continue. */
599                                                 break;
600                                         }
601                                 } else {
602                                         op->orb_edn.bv_len = strlen( op->orb_edn.bv_val );
603                                 }
604                                 rs->sr_err = dnPrettyNormal( NULL, &op->orb_edn,
605                                         &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
606                                 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
607                                 ber_dupbv(&op->o_conn->c_dn, &op->o_req_dn);
608                                 ber_dupbv(&op->o_conn->c_ndn, &op->o_req_ndn);
609                                 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
610                                 op->o_req_dn.bv_val = NULL;
611                                 op->o_req_dn.bv_len = 0;
612                                 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
613                                 op->o_req_ndn.bv_val = NULL;
614                                 op->o_req_ndn.bv_len = 0;
615                                 if ( op->o_conn->c_dn.bv_len != 0 ) {
616                                         ber_len_t max = sockbuf_max_incoming_auth;
617                                         ber_sockbuf_ctrl( op->o_conn->c_sb,
618                                                 LBER_SB_OPT_SET_MAX_INCOMING, &max );
619                                 }
620                                 /* log authorization identity */
621                                 Statslog( LDAP_DEBUG_STATS,
622                                         "conn=%lu op=%lu BIND dn=\"%s\" mech=%s (SLAPI) ssf=0\n",
623                                         op->o_connid, op->o_opid,
624                                         op->o_conn->c_dn.bv_val ? op->o_conn->c_dn.bv_val : "<empty>",
625                                         mech.bv_val, 0 );
626                                 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
627                         }
628                         goto cleanup;
629                         break;
630                 }
631         }
632 #endif /* LDAP_SLAPI */
633
634         if( op->o_bd->be_bind ) {
635                 rs->sr_err = (op->o_bd->be_bind)( op, rs );
636
637                 if ( rs->sr_err == 0 ) {
638                         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
639
640                         if( op->o_conn->c_authz_backend == NULL ) {
641                                 op->o_conn->c_authz_backend = op->o_bd;
642                         }
643
644                         /* be_bind returns regular/global edn */
645                         if( op->orb_edn.bv_len ) {
646                                 op->o_conn->c_dn = op->orb_edn;
647                         } else {
648                                 ber_dupbv(&op->o_conn->c_dn, &op->o_req_dn);
649                         }
650
651                         ber_dupbv( &op->o_conn->c_ndn, &op->o_req_ndn );
652
653                         if( op->o_conn->c_dn.bv_len != 0 ) {
654                                 ber_len_t max = sockbuf_max_incoming_auth;
655                                 ber_sockbuf_ctrl( op->o_conn->c_sb,
656                                         LBER_SB_OPT_SET_MAX_INCOMING, &max );
657                         }
658
659                         /* log authorization identity */
660                         Statslog( LDAP_DEBUG_STATS,
661                                 "conn=%lu op=%lu BIND dn=\"%s\" mech=%s ssf=0\n",
662                                 op->o_connid, op->o_opid,
663                                 op->o_conn->c_dn.bv_val, mech.bv_val, 0 );
664
665 #ifdef NEW_LOGGING
666                         LDAP_LOG( OPERATION, DETAIL1, 
667                                 "do_bind: v%d bind: \"%s\" to \"%s\" \n",
668                                 version, op->o_conn->c_dn.bv_val, op->o_conn->c_dn.bv_val );
669 #else
670                         Debug( LDAP_DEBUG_TRACE,
671                                 "do_bind: v%d bind: \"%s\" to \"%s\"\n",
672                                 version, dn.bv_val, op->o_conn->c_dn.bv_val );
673 #endif
674
675                         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
676
677                         /* send this here to avoid a race condition */
678                         send_ldap_result( op, rs );
679
680                 } else if (op->orb_edn.bv_val != NULL) {
681                         free( op->orb_edn.bv_val );
682                 }
683
684         } else {
685                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
686                         "operation not supported within naming context" );
687         }
688
689 #ifdef LDAP_SLAPI
690         if ( pb != NULL &&
691                 slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_POST_BIND_FN, pb ) < 0 )
692         {
693 #ifdef NEW_LOGGING
694                 LDAP_LOG( OPERATION, INFO,
695                         "do_bind: Bind postoperation plugins failed\n",
696                         0, 0, 0);
697 #else
698                 Debug(LDAP_DEBUG_TRACE,
699                         "do_bind: Bind postoperation plugins failed.\n",
700                         0, 0, 0);
701 #endif
702         }
703 #endif /* LDAP_SLAPI */
704
705 cleanup:
706         if ( rs->sr_err == LDAP_SUCCESS ) {
707                 if ( op->orb_method != LDAP_AUTH_SASL ) {
708                         ber_dupbv( &op->o_conn->c_authmech, &mech );
709                 }
710                 op->o_conn->c_authtype = op->orb_method;
711         }
712
713         op->o_conn->c_sasl_bindop = NULL;
714
715         if( op->o_req_dn.bv_val != NULL ) {
716                 slap_sl_free( op->o_req_dn.bv_val, op->o_tmpmemctx );
717                 op->o_req_dn.bv_val = NULL;
718         }
719         if( op->o_req_ndn.bv_val != NULL ) {
720                 slap_sl_free( op->o_req_ndn.bv_val, op->o_tmpmemctx );
721                 op->o_req_ndn.bv_val = NULL;
722         }
723
724         return rs->sr_err;
725 }