]> git.sur5r.net Git - openldap/blob - servers/slapd/bind.c
212a5802b470eae09306095347d12dabf0e1a4ab
[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         struct berval mech = { 0, NULL };
50         struct berval dn = { 0, NULL };
51         ber_tag_t tag;
52         Backend *be = NULL;
53
54 #ifdef NEW_LOGGING
55         LDAP_LOG( OPERATION, ENTRY, "do_bind: conn %d\n", op->o_connid, 0, 0 );
56 #else
57         Debug( LDAP_DEBUG_TRACE, "do_bind\n", 0, 0, 0 );
58 #endif
59
60         /*
61          * Force to connection to "anonymous" until bind succeeds.
62          */
63         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
64         if ( op->o_conn->c_sasl_bind_in_progress ) {
65                 be = op->o_conn->c_authz_backend;
66         }
67         if ( op->o_conn->c_dn.bv_len ) {
68                 /* log authorization identity demotion */
69                 Statslog( LDAP_DEBUG_STATS,
70                         "conn=%lu op=%lu BIND anonymous mech=implicit ssf=0\n",
71                         op->o_connid, op->o_opid, 0, 0, 0 );
72         }
73         connection2anonymous( op->o_conn );
74         if ( op->o_conn->c_sasl_bind_in_progress ) {
75                 op->o_conn->c_authz_backend = be;
76         }
77         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
78         if ( op->o_dn.bv_val != NULL ) {
79                 free( op->o_dn.bv_val );
80                 op->o_dn.bv_val = ch_strdup( "" );
81                 op->o_dn.bv_len = 0;
82         }
83         if ( op->o_ndn.bv_val != NULL ) {
84                 free( op->o_ndn.bv_val );
85                 op->o_ndn.bv_val = ch_strdup( "" );
86                 op->o_ndn.bv_len = 0;
87         }
88
89         /*
90          * Parse the bind request.  It looks like this:
91          *
92          *      BindRequest ::= SEQUENCE {
93          *              version         INTEGER,                 -- version
94          *              name            DistinguishedName,       -- dn
95          *              authentication  CHOICE {
96          *                      simple          [0] OCTET STRING -- passwd
97          *                      krbv42ldap      [1] OCTET STRING
98          *                      krbv42dsa       [2] OCTET STRING
99          *                      SASL            [3] SaslCredentials
100          *              }
101          *      }
102          *
103          *      SaslCredentials ::= SEQUENCE {
104          *              mechanism           LDAPString,
105          *              credentials         OCTET STRING OPTIONAL
106          *      }
107          */
108
109         tag = ber_scanf( ber, "{imt" /*}*/, &version, &dn, &op->orb_method );
110
111         if ( tag == LBER_ERROR ) {
112 #ifdef NEW_LOGGING
113                 LDAP_LOG( OPERATION, ERR, 
114                         "do_bind: conn %d  ber_scanf failed\n", op->o_connid, 0, 0 );
115 #else
116                 Debug( LDAP_DEBUG_ANY, "bind: ber_scanf failed\n", 0, 0, 0 );
117 #endif
118                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
119                 rs->sr_err = -1;
120                 goto cleanup;
121         }
122
123         op->o_protocol = version;
124
125         if( op->orb_method != LDAP_AUTH_SASL ) {
126                 tag = ber_scanf( ber, /*{*/ "m}", &op->orb_cred );
127
128         } else {
129                 tag = ber_scanf( ber, "{m" /*}*/, &mech );
130
131                 if ( tag != LBER_ERROR ) {
132                         ber_len_t len;
133                         tag = ber_peek_tag( ber, &len );
134
135                         if ( tag == LDAP_TAG_LDAPCRED ) { 
136                                 tag = ber_scanf( ber, "m", &op->orb_cred );
137                         } else {
138                                 tag = LDAP_TAG_LDAPCRED;
139                                 op->orb_cred.bv_val = NULL;
140                                 op->orb_cred.bv_len = 0;
141                         }
142
143                         if ( tag != LBER_ERROR ) {
144                                 tag = ber_scanf( ber, /*{{*/ "}}" );
145                         }
146                 }
147         }
148
149         if ( tag == LBER_ERROR ) {
150                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
151                 rs->sr_err = SLAPD_DISCONNECT;
152                 goto cleanup;
153         }
154
155         if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
156 #ifdef NEW_LOGGING
157                 LDAP_LOG( OPERATION, INFO, 
158                         "do_bind: conn %d  get_ctrls failed\n", op->o_connid, 0, 0 );
159 #else
160                 Debug( LDAP_DEBUG_ANY, "do_bind: get_ctrls failed\n", 0, 0, 0 );
161 #endif
162                 goto cleanup;
163         } 
164
165         /* We use the tmpmemctx here because it speeds up normalization.
166          * However, we must dup with regular malloc when storing any
167          * resulting DNs in the op or conn structures.
168          */
169         rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn,
170                 op->o_tmpmemctx );
171         if ( rs->sr_err != LDAP_SUCCESS ) {
172 #ifdef NEW_LOGGING
173                 LDAP_LOG( OPERATION, INFO, 
174                         "do_bind: conn %d  invalid dn (%s)\n", 
175                         op->o_connid, dn.bv_val, 0 );
176 #else
177                 Debug( LDAP_DEBUG_ANY, "bind: invalid dn (%s)\n",
178                         dn.bv_val, 0, 0 );
179 #endif
180                 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
181                 goto cleanup;
182         }
183
184         if( op->orb_method == LDAP_AUTH_SASL ) {
185 #ifdef NEW_LOGGING
186                 LDAP_LOG( OPERATION,     DETAIL1, 
187                         "do_sasl_bind: conn %d  dn (%s) mech %s\n", 
188                         op->o_connid, op->o_req_dn.bv_val, mech.bv_val );
189 #else
190                 Debug( LDAP_DEBUG_TRACE, "do_sasl_bind: dn (%s) mech %s\n",
191                         op->o_req_dn.bv_val, mech.bv_val, NULL );
192 #endif
193
194         } else {
195 #ifdef NEW_LOGGING
196                 LDAP_LOG( OPERATION, DETAIL1, 
197                         "do_bind: version=%ld dn=\"%s\" method=%ld\n",
198                         (unsigned long) version, op->o_req_dn.bv_val,
199                         (unsigned long) op->orb_method );
200 #else
201                 Debug( LDAP_DEBUG_TRACE,
202                         "do_bind: version=%ld dn=\"%s\" method=%ld\n",
203                         (unsigned long) version, op->o_req_dn.bv_val,
204                         (unsigned long) op->orb_method );
205 #endif
206         }
207
208         Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu BIND dn=\"%s\" method=%ld\n",
209             op->o_connid, op->o_opid, op->o_req_dn.bv_val,
210                 (unsigned long) op->orb_method, 0 );
211
212         if ( version < LDAP_VERSION_MIN || version > LDAP_VERSION_MAX ) {
213 #ifdef NEW_LOGGING
214                 LDAP_LOG( OPERATION, INFO, 
215                         "do_bind: conn %d  unknown version = %ld\n",
216                         op->o_connid, (unsigned long)version, 0 );
217 #else
218                 Debug( LDAP_DEBUG_ANY, "do_bind: unknown version=%ld\n",
219                         (unsigned long) version, 0, 0 );
220 #endif
221                 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
222                         "requested protocol version not supported" );
223                 goto cleanup;
224
225         } else if (!( global_allows & SLAP_ALLOW_BIND_V2 ) &&
226                 version < LDAP_VERSION3 )
227         {
228                 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
229                         "historical protocol version requested, use LDAPv3 instead" );
230                 goto cleanup;
231         }
232
233         /*
234          * we set connection version regardless of whether bind succeeds or not.
235          */
236         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
237         op->o_conn->c_protocol = version;
238         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
239
240         /* check for inappropriate controls */
241         if( get_manageDSAit( op ) == SLAP_CRITICAL_CONTROL ) {
242                 send_ldap_error( op, rs,
243                         LDAP_UNAVAILABLE_CRITICAL_EXTENSION,
244                         "manageDSAit control inappropriate" );
245                 goto cleanup;
246         }
247
248         /* Set the bindop for the benefit of in-directory SASL lookups */
249         op->o_conn->c_sasl_bindop = op;
250
251         if ( op->orb_method == LDAP_AUTH_SASL ) {
252                 if ( version < LDAP_VERSION3 ) {
253 #ifdef NEW_LOGGING
254                         LDAP_LOG( OPERATION, INFO, 
255                                 "do_bind: conn %d  sasl with LDAPv%ld\n",
256                                 op->o_connid, (unsigned long)version , 0 );
257 #else
258                         Debug( LDAP_DEBUG_ANY, "do_bind: sasl with LDAPv%ld\n",
259                                 (unsigned long) version, 0, 0 );
260 #endif
261                         send_ldap_discon( op, rs,
262                                 LDAP_PROTOCOL_ERROR, "SASL bind requires LDAPv3" );
263                         rs->sr_err = SLAPD_DISCONNECT;
264                         goto cleanup;
265                 }
266
267                 if( mech.bv_len == 0 ) {
268 #ifdef NEW_LOGGING
269                         LDAP_LOG( OPERATION, INFO, 
270                                    "do_bind: conn %d  no SASL mechanism provided\n",
271                                    op->o_connid, 0, 0 );
272 #else
273                         Debug( LDAP_DEBUG_ANY,
274                                 "do_bind: no sasl mechanism provided\n",
275                                 0, 0, 0 );
276 #endif
277                         send_ldap_error( op, rs, LDAP_AUTH_METHOD_NOT_SUPPORTED,
278                                 "no SASL mechanism provided" );
279                         goto cleanup;
280                 }
281
282                 /* check restrictions */
283                 if( backend_check_restrictions( op, rs, &mech ) != LDAP_SUCCESS ) {
284                         send_ldap_result( op, rs );
285                         goto cleanup;
286                 }
287
288                 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
289                 if ( op->o_conn->c_sasl_bind_in_progress ) {
290                         if( !bvmatch( &op->o_conn->c_sasl_bind_mech, &mech ) ) {
291                                 /* mechanism changed between bind steps */
292                                 slap_sasl_reset(op->o_conn);
293                         }
294                 } else {
295                         ber_dupbv(&op->o_conn->c_sasl_bind_mech, &mech);
296                 }
297                 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
298
299                 rs->sr_err = slap_sasl_bind( op, rs );
300
301                 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
302                 if( rs->sr_err == LDAP_SUCCESS ) {
303                         ber_dupbv(&op->o_conn->c_dn, &op->orb_edn);
304                         if( op->orb_edn.bv_len != 0 ) {
305                                 /* edn is always normalized already */
306                                 ber_dupbv( &op->o_conn->c_ndn, &op->o_conn->c_dn );
307                         }
308                         op->o_tmpfree( op->orb_edn.bv_val, op->o_tmpmemctx );
309                         op->orb_edn.bv_val = NULL;
310                         op->orb_edn.bv_len = 0;
311                         op->o_conn->c_authmech = op->o_conn->c_sasl_bind_mech;
312                         op->o_conn->c_sasl_bind_mech.bv_val = NULL;
313                         op->o_conn->c_sasl_bind_mech.bv_len = 0;
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                 op->orb_method == LDAP_AUTH_KRBV42 )
463         {
464                 if ( global_disallows & SLAP_DISALLOW_BIND_KRBV4 ) {
465                         /* disallow simple 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 #ifdef NEW_LOGGING
471                         LDAP_LOG( OPERATION, DETAIL1, 
472                                 "do_bind: conn %d  v%d Kerberos V4 bind\n",
473                                 op->o_connid, version , 0 );
474 #else
475                         Debug( LDAP_DEBUG_TRACE, "do_bind: v%d Kerberos V4 bind\n",
476                                 version, 0, 0 );
477 #endif
478                         goto cleanup;
479                 }
480                 ber_str2bv( "KRBV4", sizeof("KRBV4")-1, 0, &mech );
481 #endif
482
483         } else {
484                 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
485                 rs->sr_text = "unknown authentication method";
486
487                 send_ldap_result( op, rs );
488 #ifdef NEW_LOGGING
489                 LDAP_LOG( OPERATION, INFO, 
490                         "do_bind: conn %ld  v%d unknown authentication method (%ld)\n",
491                         op->o_connid, version, op->orb_method );
492 #else
493                 Debug( LDAP_DEBUG_TRACE,
494                         "do_bind: v%d unknown authentication method (%ld)\n",
495                         version, op->orb_method, 0 );
496 #endif
497                 goto cleanup;
498         }
499
500         /*
501          * We could be serving multiple database backends.  Select the
502          * appropriate one, or send a referral to our "referral server"
503          * if we don't hold it.
504          */
505
506         if ( (op->o_bd = select_backend( &op->o_req_ndn, 0, 0 )) == NULL ) {
507                 if ( default_referral ) {
508                         rs->sr_ref = referral_rewrite( default_referral,
509                                 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
510                         if (!rs->sr_ref) rs->sr_ref = default_referral;
511
512                         rs->sr_err = LDAP_REFERRAL;
513                         send_ldap_result( op, rs );
514
515                         if (rs->sr_ref != default_referral) ber_bvarray_free( rs->sr_ref );
516
517                 } else {
518                         /* noSuchObject is not allowed to be returned by bind */
519                         rs->sr_err = LDAP_INVALID_CREDENTIALS;
520                         send_ldap_result( op, rs );
521                 }
522
523                 goto cleanup;
524         }
525
526         /* check restrictions */
527         if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
528                 send_ldap_result( op, rs );
529                 goto cleanup;
530         }
531
532 #if defined( LDAP_SLAPI )
533         if ( pb ) {
534                 int rc;
535                 slapi_int_pblock_set_operation( pb, op );
536                 slapi_pblock_set( pb, SLAPI_BIND_TARGET, (void *)dn.bv_val );
537                 slapi_pblock_set( pb, SLAPI_BIND_METHOD, (void *)op->orb_method );
538                 slapi_pblock_set( pb, SLAPI_BIND_CREDENTIALS, (void *)&op->orb_cred );
539                 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)(0) );
540                 slapi_pblock_set( pb, SLAPI_CONN_DN, (void *)(0) );
541
542                 rc = slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_PRE_BIND_FN, pb );
543
544 #ifdef NEW_LOGGING
545                 LDAP_LOG( OPERATION, INFO,
546                         "do_bind: Bind preoperation plugin returned %d\n",
547                         rs->sr_err, 0, 0);
548 #else
549                 Debug(LDAP_DEBUG_TRACE,
550                         "do_bind: Bind preoperation plugin returned %d.\n",
551                         rs->sr_err, 0, 0);
552 #endif
553
554                 switch ( rc ) {
555                 case SLAPI_BIND_SUCCESS:
556                         /* Continue with backend processing */
557                         break;
558                 case SLAPI_BIND_FAIL:
559                         /* Failure, server sends result */
560                         rs->sr_err = LDAP_INVALID_CREDENTIALS;
561                         send_ldap_result( op, rs );
562                         goto cleanup;
563                         break;
564                 case SLAPI_BIND_ANONYMOUS:
565                         /* SLAPI_BIND_ANONYMOUS is undocumented XXX */
566                 default:
567                         /* Authoritative, plugin sent result, or no plugins called. */
568                         if ( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE,
569                                 (void *)&rs->sr_err) != 0 )
570                         {
571                                 rs->sr_err = LDAP_OTHER;
572                         }
573
574                         op->orb_edn.bv_val = NULL;
575                         op->orb_edn.bv_len = 0;
576
577                         if ( rs->sr_err == LDAP_SUCCESS ) {
578                                 slapi_pblock_get( pb, SLAPI_CONN_DN, (void *)&op->orb_edn.bv_val );
579                                 if ( op->orb_edn.bv_val == NULL ) {
580                                         if ( rc == 1 ) {
581                                                 /* No plugins were called; continue. */
582                                                 break;
583                                         }
584                                 } else {
585                                         op->orb_edn.bv_len = strlen( op->orb_edn.bv_val );
586                                 }
587                                 rs->sr_err = dnPrettyNormal( NULL, &op->orb_edn,
588                                         &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
589                                 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
590                                 ber_dupbv(&op->o_conn->c_dn, &op->o_req_dn);
591                                 ber_dupbv(&op->o_conn->c_ndn, &op->o_req_ndn);
592                                 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
593                                 op->o_req_dn.bv_val = NULL;
594                                 op->o_req_dn.bv_len = 0;
595                                 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
596                                 op->o_req_ndn.bv_val = NULL;
597                                 op->o_req_ndn.bv_len = 0;
598                                 if ( op->o_conn->c_dn.bv_len != 0 ) {
599                                         ber_len_t max = sockbuf_max_incoming_auth;
600                                         ber_sockbuf_ctrl( op->o_conn->c_sb,
601                                                 LBER_SB_OPT_SET_MAX_INCOMING, &max );
602                                 }
603                                 /* log authorization identity */
604                                 Statslog( LDAP_DEBUG_STATS,
605                                         "conn=%lu op=%lu BIND dn=\"%s\" mech=%s (SLAPI) ssf=0\n",
606                                         op->o_connid, op->o_opid,
607                                         op->o_conn->c_dn.bv_val ? op->o_conn->c_dn.bv_val : "<empty>",
608                                         mech.bv_val, 0 );
609                                 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
610                         }
611                         goto cleanup;
612                         break;
613                 }
614         }
615 #endif /* defined( LDAP_SLAPI ) */
616
617         if( op->o_bd->be_bind ) {
618                 rs->sr_err = (op->o_bd->be_bind)( op, rs );
619
620                 if ( rs->sr_err == 0 ) {
621                         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
622
623                         if( op->o_conn->c_authz_backend == NULL ) {
624                                 op->o_conn->c_authz_backend = op->o_bd;
625                         }
626
627                         /* be_bind returns regular/global edn */
628                         if( op->orb_edn.bv_len ) {
629                                 op->o_conn->c_dn = op->orb_edn;
630                         } else {
631                                 ber_dupbv(&op->o_conn->c_dn, &op->o_req_dn);
632                         }
633
634                         ber_dupbv( &op->o_conn->c_ndn, &op->o_req_ndn );
635
636                         if( op->o_conn->c_dn.bv_len != 0 ) {
637                                 ber_len_t max = sockbuf_max_incoming_auth;
638                                 ber_sockbuf_ctrl( op->o_conn->c_sb,
639                                         LBER_SB_OPT_SET_MAX_INCOMING, &max );
640                         }
641
642                         /* log authorization identity */
643                         Statslog( LDAP_DEBUG_STATS,
644                                 "conn=%lu op=%lu BIND dn=\"%s\" mech=%s ssf=0\n",
645                                 op->o_connid, op->o_opid,
646                                 op->o_conn->c_dn.bv_val, mech.bv_val, 0 );
647
648 #ifdef NEW_LOGGING
649                         LDAP_LOG( OPERATION, DETAIL1, 
650                                 "do_bind: v%d bind: \"%s\" to \"%s\" \n",
651                                 version, op->o_conn->c_dn.bv_val, op->o_conn->c_dn.bv_val );
652 #else
653                         Debug( LDAP_DEBUG_TRACE,
654                                 "do_bind: v%d bind: \"%s\" to \"%s\"\n",
655                                 version, dn.bv_val, op->o_conn->c_dn.bv_val );
656 #endif
657
658                         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
659
660                         /* send this here to avoid a race condition */
661                         send_ldap_result( op, rs );
662
663                 } else if (op->orb_edn.bv_val != NULL) {
664                         free( op->orb_edn.bv_val );
665                 }
666
667         } else {
668                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
669                         "operation not supported within naming context" );
670         }
671
672 #if defined( LDAP_SLAPI )
673         if ( pb != NULL && slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_POST_BIND_FN, pb ) < 0 ) {
674 #ifdef NEW_LOGGING
675                 LDAP_LOG( OPERATION, INFO,
676                         "do_bind: Bind postoperation plugins failed\n",
677                         0, 0, 0);
678 #else
679                 Debug(LDAP_DEBUG_TRACE,
680                         "do_bind: Bind postoperation plugins failed.\n",
681                         0, 0, 0);
682 #endif
683         }
684 #endif /* defined( LDAP_SLAPI ) */
685
686 cleanup:
687         if ( rs->sr_err == LDAP_SUCCESS ) {
688                 if ( op->orb_method != LDAP_AUTH_SASL ) {
689                         ber_dupbv( &op->o_conn->c_authmech, &mech );
690                 }
691                 op->o_conn->c_authtype = op->orb_method;
692         }
693
694         op->o_conn->c_sasl_bindop = NULL;
695
696         if( op->o_req_dn.bv_val != NULL ) {
697                 sl_free( op->o_req_dn.bv_val, op->o_tmpmemctx );
698                 op->o_req_dn.bv_val = NULL;
699         }
700         if( op->o_req_ndn.bv_val != NULL ) {
701                 sl_free( op->o_req_ndn.bv_val, op->o_tmpmemctx );
702                 op->o_req_ndn.bv_val = NULL;
703         }
704
705         return rs->sr_err;
706 }