]> git.sur5r.net Git - openldap/blob - servers/slapd/bind.c
allow backwards compatibility for 'T' option (single char)
[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                         op->orb_edn.bv_val = NULL;
312                         op->orb_edn.bv_len = 0;
313                         op->o_conn->c_authmech = op->o_conn->c_sasl_bind_mech;
314                         op->o_conn->c_sasl_bind_mech.bv_val = NULL;
315                         op->o_conn->c_sasl_bind_mech.bv_len = 0;
316                         op->o_conn->c_sasl_bind_in_progress = 0;
317
318                         op->o_conn->c_sasl_ssf = op->orb_ssf;
319                         if( op->orb_ssf > op->o_conn->c_ssf ) {
320                                 op->o_conn->c_ssf = op->orb_ssf;
321                         }
322
323                         if( op->o_conn->c_dn.bv_len != 0 ) {
324                                 ber_len_t max = sockbuf_max_incoming_auth;
325                                 ber_sockbuf_ctrl( op->o_conn->c_sb,
326                                         LBER_SB_OPT_SET_MAX_INCOMING, &max );
327                         }
328
329                         /* log authorization identity */
330                         Statslog( LDAP_DEBUG_STATS,
331                                 "conn=%lu op=%lu BIND dn=\"%s\" mech=%s ssf=%d\n",
332                                 op->o_connid, op->o_opid,
333                                 op->o_conn->c_dn.bv_val ? op->o_conn->c_dn.bv_val : "<empty>",
334                                 op->o_conn->c_authmech.bv_val, op->orb_ssf );
335
336 #ifdef NEW_LOGGING
337                         LDAP_LOG( OPERATION, DETAIL1, 
338                                 "do_bind: SASL/%s bind: dn=\"%s\" ssf=%d\n",
339                                 op->o_conn->c_authmech.bv_val,
340                                 op->o_conn->c_dn.bv_val ? op->o_conn->c_dn.bv_val : "<empty>",
341                                 op->orb_ssf );
342 #else
343                         Debug( LDAP_DEBUG_TRACE,
344                                 "do_bind: SASL/%s bind: dn=\"%s\" ssf=%d\n",
345                                 op->o_conn->c_authmech.bv_val,
346                                 op->o_conn->c_dn.bv_val ? op->o_conn->c_dn.bv_val : "<empty>",
347                                 op->orb_ssf );
348 #endif
349
350                 } else if ( rs->sr_err == LDAP_SASL_BIND_IN_PROGRESS ) {
351                         op->o_conn->c_sasl_bind_in_progress = 1;
352
353                 } else {
354                         if ( op->o_conn->c_sasl_bind_mech.bv_val ) {
355                                 free( op->o_conn->c_sasl_bind_mech.bv_val );
356                                 op->o_conn->c_sasl_bind_mech.bv_val = NULL;
357                                 op->o_conn->c_sasl_bind_mech.bv_len = 0;
358                         }
359                         op->o_conn->c_sasl_bind_in_progress = 0;
360                 }
361
362 #ifdef LDAP_SLAPI
363 #define pb      op->o_pb
364                 /*
365                  * Normally post-operation plugins are called only after the
366                  * backend operation. Because the front-end performs SASL
367                  * binds on behalf of the backend, we'll make a special
368                  * exception to call the post-operation plugins after a
369                  * SASL bind.
370                  */
371                 if ( pb ) {
372                         slapi_int_pblock_set_operation( pb, op );
373                         slapi_pblock_set( pb, SLAPI_BIND_TARGET, (void *)dn.bv_val );
374                         slapi_pblock_set( pb, SLAPI_BIND_METHOD, (void *)op->orb_method );
375                         slapi_pblock_set( pb, SLAPI_BIND_CREDENTIALS, (void *)&op->orb_cred );
376                         slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)(0) );
377                         (void) slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_POST_BIND_FN, pb );
378                 }
379 #endif /* LDAP_SLAPI */
380
381                 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
382
383                 goto cleanup;
384
385         } else {
386                 /* Not SASL, cancel any in-progress bind */
387                 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
388
389                 if ( op->o_conn->c_sasl_bind_mech.bv_val != NULL ) {
390                         free(op->o_conn->c_sasl_bind_mech.bv_val);
391                         op->o_conn->c_sasl_bind_mech.bv_val = NULL;
392                         op->o_conn->c_sasl_bind_mech.bv_len = 0;
393                 }
394                 op->o_conn->c_sasl_bind_in_progress = 0;
395
396                 slap_sasl_reset( op->o_conn );
397                 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
398         }
399
400         if ( op->orb_method == LDAP_AUTH_SIMPLE ) {
401                 ber_str2bv( "SIMPLE", sizeof("SIMPLE")-1, 0, &mech );
402                 /* accept "anonymous" binds */
403                 if ( op->orb_cred.bv_len == 0 || op->o_req_ndn.bv_len == 0 ) {
404                         rs->sr_err = LDAP_SUCCESS;
405
406                         if( op->orb_cred.bv_len &&
407                                 !( global_allows & SLAP_ALLOW_BIND_ANON_CRED ))
408                         {
409                                 /* cred is not empty, disallow */
410                                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
411
412                         } else if ( op->o_req_ndn.bv_len &&
413                                 !( global_allows & SLAP_ALLOW_BIND_ANON_DN ))
414                         {
415                                 /* DN is not empty, disallow */
416                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
417                                 rs->sr_text =
418                                         "unauthenticated bind (DN with no password) disallowed";
419
420                         } else if ( global_disallows & SLAP_DISALLOW_BIND_ANON ) {
421                                 /* disallow */
422                                 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
423                                 rs->sr_text = "anonymous bind disallowed";
424
425                         } else {
426                                 backend_check_restrictions( op, rs, &mech );
427                         }
428
429                         /*
430                          * we already forced connection to "anonymous",
431                          * just need to send success
432                          */
433                         send_ldap_result( op, rs );
434 #ifdef NEW_LOGGING
435                         LDAP_LOG( OPERATION, DETAIL1, 
436                                 "do_bind: conn %d  v%d anonymous bind\n",
437                                 op->o_connid, version , 0 );
438 #else
439                         Debug( LDAP_DEBUG_TRACE, "do_bind: v%d anonymous bind\n",
440                                 version, 0, 0 );
441 #endif
442                         goto cleanup;
443
444                 } else if ( global_disallows & SLAP_DISALLOW_BIND_SIMPLE ) {
445                         /* disallow simple authentication */
446                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
447                         rs->sr_text = "unwilling to perform simple authentication";
448
449                         send_ldap_result( op, rs );
450 #ifdef NEW_LOGGING
451                         LDAP_LOG( OPERATION, INFO, 
452                                 "do_bind: conn %d  v%d simple bind(%s) disallowed\n",
453                                 op->o_connid, version, op->o_req_ndn.bv_val );
454 #else
455                         Debug( LDAP_DEBUG_TRACE,
456                                 "do_bind: v%d simple bind(%s) disallowed\n",
457                                 version, op->o_req_ndn.bv_val, 0 );
458 #endif
459                         goto cleanup;
460                 }
461
462 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
463         } else if ( op->orb_method == LDAP_AUTH_KRBV41 ) {
464                 if ( global_disallows & SLAP_DISALLOW_BIND_KRBV4 ) {
465                         /* disallow krbv4 authentication */
466                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
467                         rs->sr_text = "unwilling to perform Kerberos V4 bind";
468
469                         send_ldap_result( op, rs );
470
471 #ifdef NEW_LOGGING
472                         LDAP_LOG( OPERATION, DETAIL1, 
473                                 "do_bind: conn %d  v%d Kerberos V4 (step 1) bind refused\n",
474                                 op->o_connid, version , 0 );
475 #else
476                         Debug( LDAP_DEBUG_TRACE,
477                                 "do_bind: v%d Kerberos V4 (step 1) bind refused\n",
478                                 version, 0, 0 );
479 #endif
480                         goto cleanup;
481                 }
482                 ber_str2bv( "KRBV4", sizeof("KRBV4")-1, 0, &mech );
483
484         } else if ( op->orb_method == LDAP_AUTH_KRBV42 ) {
485                 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
486                 rs->sr_text = "Kerberos V4 (step 2) bind not supported";
487                 send_ldap_result( op, rs );
488
489 #ifdef NEW_LOGGING
490                 LDAP_LOG( OPERATION, DETAIL1, 
491                         "do_bind: conn %d  v%d Kerberos V4 (step 2) bind refused\n",
492                         op->o_connid, version , 0 );
493 #else
494                 Debug( LDAP_DEBUG_TRACE,
495                         "do_bind: v%d Kerberos V4 (step 2) bind refused\n",
496                         version, 0, 0 );
497 #endif
498                 goto cleanup;
499 #endif
500
501         } else {
502                 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
503                 rs->sr_text = "unknown authentication method";
504
505                 send_ldap_result( op, rs );
506 #ifdef NEW_LOGGING
507                 LDAP_LOG( OPERATION, INFO, 
508                         "do_bind: conn %ld  v%d unknown authentication method (%ld)\n",
509                         op->o_connid, version, op->orb_method );
510 #else
511                 Debug( LDAP_DEBUG_TRACE,
512                         "do_bind: v%d unknown authentication method (%ld)\n",
513                         version, op->orb_method, 0 );
514 #endif
515                 goto cleanup;
516         }
517
518         /*
519          * We could be serving multiple database backends.  Select the
520          * appropriate one, or send a referral to our "referral server"
521          * if we don't hold it.
522          */
523
524         if ( (op->o_bd = select_backend( &op->o_req_ndn, 0, 0 )) == NULL ) {
525                 if ( default_referral ) {
526                         rs->sr_ref = referral_rewrite( default_referral,
527                                 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
528                         if (!rs->sr_ref) rs->sr_ref = default_referral;
529
530                         rs->sr_err = LDAP_REFERRAL;
531                         send_ldap_result( op, rs );
532
533                         if (rs->sr_ref != default_referral) ber_bvarray_free( rs->sr_ref );
534
535                 } else {
536                         /* noSuchObject is not allowed to be returned by bind */
537                         rs->sr_err = LDAP_INVALID_CREDENTIALS;
538                         send_ldap_result( op, rs );
539                 }
540
541                 goto cleanup;
542         }
543
544         /* check restrictions */
545         if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
546                 send_ldap_result( op, rs );
547                 goto cleanup;
548         }
549
550 #ifdef LDAP_SLAPI
551         if ( pb ) {
552                 int rc;
553                 slapi_int_pblock_set_operation( pb, op );
554                 slapi_pblock_set( pb, SLAPI_BIND_TARGET, (void *)dn.bv_val );
555                 slapi_pblock_set( pb, SLAPI_BIND_METHOD, (void *)op->orb_method );
556                 slapi_pblock_set( pb, SLAPI_BIND_CREDENTIALS, (void *)&op->orb_cred );
557                 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)(0) );
558                 slapi_pblock_set( pb, SLAPI_CONN_DN, (void *)(0) );
559
560                 rc = slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_PRE_BIND_FN, pb );
561
562 #ifdef NEW_LOGGING
563                 LDAP_LOG( OPERATION, INFO,
564                         "do_bind: Bind preoperation plugin returned %d\n",
565                         rs->sr_err, 0, 0);
566 #else
567                 Debug(LDAP_DEBUG_TRACE,
568                         "do_bind: Bind preoperation plugin returned %d.\n",
569                         rs->sr_err, 0, 0);
570 #endif
571
572                 switch ( rc ) {
573                 case SLAPI_BIND_SUCCESS:
574                         /* Continue with backend processing */
575                         break;
576                 case SLAPI_BIND_FAIL:
577                         /* Failure, server sends result */
578                         rs->sr_err = LDAP_INVALID_CREDENTIALS;
579                         send_ldap_result( op, rs );
580                         goto cleanup;
581                         break;
582                 case SLAPI_BIND_ANONYMOUS:
583                         /* SLAPI_BIND_ANONYMOUS is undocumented XXX */
584                 default:
585                         /* Authoritative, plugin sent result, or no plugins called. */
586                         if ( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE,
587                                 (void *)&rs->sr_err) != 0 )
588                         {
589                                 rs->sr_err = LDAP_OTHER;
590                         }
591
592                         op->orb_edn.bv_val = NULL;
593                         op->orb_edn.bv_len = 0;
594
595                         if ( rs->sr_err == LDAP_SUCCESS ) {
596                                 slapi_pblock_get( pb, SLAPI_CONN_DN,
597                                         (void *)&op->orb_edn.bv_val );
598                                 if ( op->orb_edn.bv_val == NULL ) {
599                                         if ( rc == 1 ) {
600                                                 /* No plugins were called; continue. */
601                                                 break;
602                                         }
603                                 } else {
604                                         op->orb_edn.bv_len = strlen( op->orb_edn.bv_val );
605                                 }
606                                 rs->sr_err = dnPrettyNormal( NULL, &op->orb_edn,
607                                         &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
608                                 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
609                                 ber_dupbv(&op->o_conn->c_dn, &op->o_req_dn);
610                                 ber_dupbv(&op->o_conn->c_ndn, &op->o_req_ndn);
611                                 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
612                                 op->o_req_dn.bv_val = NULL;
613                                 op->o_req_dn.bv_len = 0;
614                                 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
615                                 op->o_req_ndn.bv_val = NULL;
616                                 op->o_req_ndn.bv_len = 0;
617                                 if ( op->o_conn->c_dn.bv_len != 0 ) {
618                                         ber_len_t max = sockbuf_max_incoming_auth;
619                                         ber_sockbuf_ctrl( op->o_conn->c_sb,
620                                                 LBER_SB_OPT_SET_MAX_INCOMING, &max );
621                                 }
622                                 /* log authorization identity */
623                                 Statslog( LDAP_DEBUG_STATS,
624                                         "conn=%lu op=%lu BIND dn=\"%s\" mech=%s (SLAPI) ssf=0\n",
625                                         op->o_connid, op->o_opid,
626                                         op->o_conn->c_dn.bv_val ? op->o_conn->c_dn.bv_val : "<empty>",
627                                         mech.bv_val, 0 );
628                                 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
629                         }
630                         goto cleanup;
631                         break;
632                 }
633         }
634 #endif /* LDAP_SLAPI */
635
636         if( op->o_bd->be_bind ) {
637                 rs->sr_err = (op->o_bd->be_bind)( op, rs );
638
639                 if ( rs->sr_err == 0 ) {
640                         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
641
642                         if( op->o_conn->c_authz_backend == NULL ) {
643                                 op->o_conn->c_authz_backend = op->o_bd;
644                         }
645
646                         /* be_bind returns regular/global edn */
647                         if( op->orb_edn.bv_len ) {
648                                 op->o_conn->c_dn = op->orb_edn;
649                         } else {
650                                 ber_dupbv(&op->o_conn->c_dn, &op->o_req_dn);
651                         }
652
653                         ber_dupbv( &op->o_conn->c_ndn, &op->o_req_ndn );
654
655                         if( op->o_conn->c_dn.bv_len != 0 ) {
656                                 ber_len_t max = sockbuf_max_incoming_auth;
657                                 ber_sockbuf_ctrl( op->o_conn->c_sb,
658                                         LBER_SB_OPT_SET_MAX_INCOMING, &max );
659                         }
660
661                         /* log authorization identity */
662                         Statslog( LDAP_DEBUG_STATS,
663                                 "conn=%lu op=%lu BIND dn=\"%s\" mech=%s ssf=0\n",
664                                 op->o_connid, op->o_opid,
665                                 op->o_conn->c_dn.bv_val, mech.bv_val, 0 );
666
667 #ifdef NEW_LOGGING
668                         LDAP_LOG( OPERATION, DETAIL1, 
669                                 "do_bind: v%d bind: \"%s\" to \"%s\" \n",
670                                 version, op->o_conn->c_dn.bv_val, op->o_conn->c_dn.bv_val );
671 #else
672                         Debug( LDAP_DEBUG_TRACE,
673                                 "do_bind: v%d bind: \"%s\" to \"%s\"\n",
674                                 version, dn.bv_val, op->o_conn->c_dn.bv_val );
675 #endif
676
677                         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
678
679                         /* send this here to avoid a race condition */
680                         send_ldap_result( op, rs );
681
682                 } else if (op->orb_edn.bv_val != NULL) {
683                         free( op->orb_edn.bv_val );
684                 }
685
686         } else {
687                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
688                         "operation not supported within naming context" );
689         }
690
691 #ifdef LDAP_SLAPI
692         if ( pb != NULL &&
693                 slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_POST_BIND_FN, pb ) < 0 )
694         {
695 #ifdef NEW_LOGGING
696                 LDAP_LOG( OPERATION, INFO,
697                         "do_bind: Bind postoperation plugins failed\n",
698                         0, 0, 0);
699 #else
700                 Debug(LDAP_DEBUG_TRACE,
701                         "do_bind: Bind postoperation plugins failed.\n",
702                         0, 0, 0);
703 #endif
704         }
705 #endif /* LDAP_SLAPI */
706
707 cleanup:
708         if ( rs->sr_err == LDAP_SUCCESS ) {
709                 if ( op->orb_method != LDAP_AUTH_SASL ) {
710                         ber_dupbv( &op->o_conn->c_authmech, &mech );
711                 }
712                 op->o_conn->c_authtype = op->orb_method;
713         }
714
715         op->o_conn->c_sasl_bindop = NULL;
716
717         if( op->o_req_dn.bv_val != NULL ) {
718                 sl_free( op->o_req_dn.bv_val, op->o_tmpmemctx );
719                 op->o_req_dn.bv_val = NULL;
720         }
721         if( op->o_req_ndn.bv_val != NULL ) {
722                 sl_free( op->o_req_ndn.bv_val, op->o_tmpmemctx );
723                 op->o_req_ndn.bv_val = NULL;
724         }
725
726         return rs->sr_err;
727 }