]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/bind.c
improve previous commit
[openldap] / servers / slapd / back-ldap / bind.c
1 /* bind.c - ldap backend bind function */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1999-2006 The OpenLDAP Foundation.
6  * Portions Copyright 2000-2003 Pierangelo Masarati.
7  * Portions Copyright 1999-2003 Howard Chu.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted only as authorized by the OpenLDAP
12  * Public License.
13  *
14  * A copy of this license is available in the file LICENSE in the
15  * top-level directory of the distribution or, alternatively, at
16  * <http://www.OpenLDAP.org/license.html>.
17  */
18 /* ACKNOWLEDGEMENTS:
19  * This work was initially developed by Howard Chu for inclusion
20  * in OpenLDAP Software and subsequently enhanced by Pierangelo
21  * Masarati.
22  */
23
24 #include "portable.h"
25
26 #include <stdio.h>
27
28 #include <ac/errno.h>
29 #include <ac/socket.h>
30 #include <ac/string.h>
31
32 #define AVL_INTERNAL
33 #include "slap.h"
34 #include "back-ldap.h"
35
36 #include <lutil_ldap.h>
37
38 #ifndef PRINT_CONNTREE
39 #define PRINT_CONNTREE 0
40 #endif /* !PRINT_CONNTREE */
41
42 #define LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ       "2.16.840.1.113730.3.4.12"
43
44 #if PRINT_CONNTREE > 0
45 static void
46 ravl_print( Avlnode *root, int depth )
47 {
48         int             i;
49         ldapconn_t      *lc;
50         
51         if ( root == 0 ) {
52                 return;
53         }
54         
55         ravl_print( root->avl_right, depth+1 );
56         
57         for ( i = 0; i < depth; i++ ) {
58                 fprintf( stderr, "-" );
59         }
60
61         lc = root->avl_data;
62         fprintf( stderr, "lc=%p local=\"%s\" conn=%p %s refcnt=%d\n",
63                 (void *)lc, lc->lc_local_ndn.bv_val, (void *)lc->lc_conn,
64                 avl_bf2str( root->avl_bf ), lc->lc_refcnt );
65         
66         ravl_print( root->avl_left, depth+1 );
67 }
68
69 static void
70 myprint( Avlnode *root, char *msg )
71 {
72         fprintf( stderr, "========> %s\n", msg );
73         
74         if ( root == 0 ) {
75                 fprintf( stderr, "\tNULL\n" );
76
77         } else {
78                 ravl_print( root, 0 );
79         }
80         
81         fprintf( stderr, "<======== %s\n", msg );
82 }
83 #endif /* PRINT_CONNTREE */
84
85 static int
86 ldap_back_proxy_authz_bind( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok );
87
88 static int
89 ldap_back_prepare_conn( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok );
90
91 static int
92 ldap_back_conndnlc_cmp( const void *c1, const void *c2 );
93
94 int
95 ldap_back_bind( Operation *op, SlapReply *rs )
96 {
97         ldapinfo_t      *li = (ldapinfo_t *) op->o_bd->be_private;
98         ldapconn_t      *lc;
99
100         int rc = 0;
101         ber_int_t msgid;
102
103         lc = ldap_back_getconn( op, rs, LDAP_BACK_BIND_SERR );
104         if ( !lc ) {
105                 return rs->sr_err;
106         }
107
108         if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
109                 ch_free( lc->lc_bound_ndn.bv_val );
110                 BER_BVZERO( &lc->lc_bound_ndn );
111         }
112         LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
113
114         /* method is always LDAP_AUTH_SIMPLE if we got here */
115         rs->sr_err = ldap_sasl_bind( lc->lc_ld, op->o_req_dn.bv_val,
116                         LDAP_SASL_SIMPLE,
117                         &op->orb_cred, op->o_ctrls, NULL, &msgid );
118         rc = ldap_back_op_result( lc, op, rs, msgid, 0, LDAP_BACK_SENDERR );
119
120         if ( rc == LDAP_SUCCESS ) {
121                 /* If defined, proxyAuthz will be used also when
122                  * back-ldap is the authorizing backend; for this
123                  * purpose, a successful bind is followed by a
124                  * bind with the configured identity assertion */
125                 /* NOTE: use with care */
126                 if ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) {
127                         ldap_back_proxy_authz_bind( lc, op, rs, LDAP_BACK_SENDERR );
128                         if ( !LDAP_BACK_CONN_ISBOUND( lc ) ) {
129                                 rc = 1;
130                                 goto done;
131                         }
132                 }
133
134                 LDAP_BACK_CONN_ISBOUND_SET( lc );
135                 ber_dupbv( &lc->lc_bound_ndn, &op->o_req_ndn );
136
137                 if ( LDAP_BACK_SAVECRED( li ) ) {
138                         if ( !BER_BVISNULL( &lc->lc_cred ) ) {
139                                 memset( lc->lc_cred.bv_val, 0,
140                                                 lc->lc_cred.bv_len );
141                         }
142                         ber_bvreplace( &lc->lc_cred, &op->orb_cred );
143                         ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
144                 }
145         }
146 done:;
147
148         assert( lc->lc_binding == 1 );
149         lc->lc_binding = 0;
150
151         /* must re-insert if local DN changed as result of bind */
152         if ( !LDAP_BACK_CONN_ISBOUND( lc )
153                 || ( LDAP_BACK_CONN_ISBOUND( lc )
154                         && !dn_match( &op->o_req_ndn, &lc->lc_local_ndn ) ) )
155         {
156                 int             lerr = -1;
157                 ldapconn_t      *tmplc;
158
159                 /* wait for all other ops to release the connection */
160 retry_lock:;
161                 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
162                 if ( lc->lc_refcnt > 1 ) {
163                         ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
164                         ldap_pvt_thread_yield();
165                         goto retry_lock;
166                 }
167
168                 assert( lc->lc_refcnt == 1 );
169                 tmplc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
170                                 ldap_back_conndnlc_cmp );
171                 assert( tmplc == NULL || lc == tmplc );
172
173                 /* delete all cached connections with the current connection */
174                 if ( LDAP_BACK_SINGLECONN( li ) ) {
175                         while ( ( tmplc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc, ldap_back_conn_cmp ) ) != NULL )
176                         {
177                                 Debug( LDAP_DEBUG_TRACE,
178                                         "=>ldap_back_bind: destroying conn %ld (refcnt=%u)\n",
179                                         LDAP_BACK_PCONN_ID( lc->lc_conn ), lc->lc_refcnt, 0 );
180
181                                 if ( lc->lc_refcnt != 0 ) {
182                                         /* taint it */
183                                         LDAP_BACK_CONN_TAINTED_SET( tmplc );
184
185                                 } else {
186                                         /*
187                                          * Needs a test because the handler may be corrupted,
188                                          * and calling ldap_unbind on a corrupted header results
189                                          * in a segmentation fault
190                                          */
191                                         ldap_back_conn_free( tmplc );
192                                 }
193                         }
194                 }
195
196                 if ( LDAP_BACK_CONN_ISBOUND( lc ) ) {
197                         ber_bvreplace( &lc->lc_local_ndn, &op->o_req_ndn );
198                         lerr = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
199                                 ldap_back_conndn_cmp, ldap_back_conndn_dup );
200                 }
201
202 #if PRINT_CONNTREE > 0
203                 myprint( li->li_conninfo.lai_tree, "ldap_back_bind" );
204 #endif /* PRINT_CONNTREE */
205         
206                 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
207                 switch ( lerr ) {
208                 case 0:
209                         break;
210
211                 case -1:
212                         /* duplicate; someone else successfully bound
213                          * on the same connection with the same identity;
214                          * we can do this because lc_refcnt == 1 */
215                         ldap_back_conn_free( lc );
216                         lc = NULL;
217                 }
218         }
219
220         if ( lc != NULL ) {
221                 ldap_back_release_conn( op, rs, lc );
222         }
223
224         return( rc );
225 }
226
227 /*
228  * ldap_back_conndn_cmp
229  *
230  * compares two ldapconn_t based on the value of the conn pointer
231  * and of the local DN; used by avl stuff for insert, lookup
232  * and direct delete
233  */
234 int
235 ldap_back_conndn_cmp( const void *c1, const void *c2 )
236 {
237         const ldapconn_t        *lc1 = (const ldapconn_t *)c1;
238         const ldapconn_t        *lc2 = (const ldapconn_t *)c2;
239         int rc;
240
241         /* If local DNs don't match, it is definitely not a match */
242         /* For shared sessions, conn is NULL. Only explicitly
243          * bound sessions will have non-NULL conn.
244          */
245         rc = SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
246         if ( rc == 0 ) {
247                 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
248         }
249
250         return rc;
251 }
252
253 /*
254  * ldap_back_conndnlc_cmp
255  *
256  * compares two ldapconn_t based on the value of the conn pointer,
257  * the local DN and the lc pointer; used by avl stuff for insert, lookup
258  * and direct delete
259  */
260 static int
261 ldap_back_conndnlc_cmp( const void *c1, const void *c2 )
262 {
263         const ldapconn_t        *lc1 = (const ldapconn_t *)c1;
264         const ldapconn_t        *lc2 = (const ldapconn_t *)c2;
265         int rc;
266
267         /* If local DNs don't match, it is definitely not a match */
268         /* For shared sessions, conn is NULL. Only explicitly
269          * bound sessions will have non-NULL conn.
270          */
271         rc = SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
272         if ( rc == 0 ) {
273                 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
274                 if ( rc == 0 ) {
275                         rc = SLAP_PTRCMP( lc1, lc2 );
276                 }
277         }
278
279         return rc;
280 }
281
282 /*
283  * ldap_back_conn_cmp
284  *
285  * compares two ldapconn_t based on the value of the conn pointer;
286  * used by avl stuff for delete of all conns with the same connid
287  */
288 int
289 ldap_back_conn_cmp( const void *c1, const void *c2 )
290 {
291         const ldapconn_t        *lc1 = (const ldapconn_t *)c1;
292         const ldapconn_t        *lc2 = (const ldapconn_t *)c2;
293
294         /* For shared sessions, conn is NULL. Only explicitly
295          * bound sessions will have non-NULL conn.
296          */
297         return SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
298 }
299
300 /*
301  * ldap_back_conndn_dup
302  *
303  * returns -1 in case a duplicate ldapconn_t has been inserted;
304  * used by avl stuff
305  */
306 int
307 ldap_back_conndn_dup( void *c1, void *c2 )
308 {
309         ldapconn_t      *lc1 = (ldapconn_t *)c1;
310         ldapconn_t      *lc2 = (ldapconn_t *)c2;
311
312         /* Cannot have more than one shared session with same DN */
313         if ( lc1->lc_conn == lc2->lc_conn &&
314                 dn_match( &lc1->lc_local_ndn, &lc2->lc_local_ndn ) )
315         {
316                 return -1;
317         }
318                 
319         return 0;
320 }
321
322 int
323 ldap_back_freeconn( Operation *op, ldapconn_t *lc, int dolock )
324 {
325         ldapinfo_t      *li = (ldapinfo_t *) op->o_bd->be_private;
326         ldapconn_t      *tmplc;
327
328         if ( dolock ) {
329                 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
330         }
331
332         tmplc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
333                         ldap_back_conndnlc_cmp );
334         assert( LDAP_BACK_CONN_TAINTED( lc ) || tmplc == lc );
335         if ( lc->lc_refcnt == 0 ) {
336                 ldap_back_conn_free( (void *)lc );
337         }
338
339         if ( dolock ) {
340                 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
341         }
342
343         return 0;
344 }
345
346 #ifdef HAVE_TLS
347 static int
348 ldap_back_start_tls(
349         LDAP            *ld,
350         int             protocol,
351         int             *is_tls,
352         const char      *url,
353         unsigned        flags,
354         int             retries,
355         const char      **text )
356 {
357         int             rc = LDAP_SUCCESS;
358         ldapinfo_t      dummy;
359
360         /* this is ridiculous... */
361         dummy.li_flags = flags;
362
363         /* start TLS ("tls-[try-]{start,propagate}" statements) */
364         if ( ( LDAP_BACK_USE_TLS( &dummy ) || ( *is_tls && LDAP_BACK_PROPAGATE_TLS( &dummy ) ) )
365                                 && !ldap_is_ldaps_url( url ) )
366         {
367 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
368                 /*
369                  * use asynchronous StartTLS
370                  * in case, chase referral (not implemented yet)
371                  */
372                 int             msgid;
373
374                 if ( protocol == 0 ) {
375                         ldap_get_option( ld, LDAP_OPT_PROTOCOL_VERSION,
376                                         (void *)&protocol );
377                 }
378
379                 if ( protocol < LDAP_VERSION3 ) {
380                         /* we should rather bail out... */
381                         rc = LDAP_UNWILLING_TO_PERFORM;
382                         *text = "invalid protocol version";
383                 }
384
385                 if ( rc == LDAP_SUCCESS ) {
386                         rc = ldap_start_tls( ld, NULL, NULL, &msgid );
387                 }
388
389                 if ( rc == LDAP_SUCCESS ) {
390                         LDAPMessage     *res = NULL;
391                         struct timeval  tv;
392
393                         LDAP_BACK_TV_SET( &tv );
394
395 retry:;
396                         rc = ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res );
397                         if ( rc < 0 ) {
398                                 rc = LDAP_UNAVAILABLE;
399
400                         } else if ( rc == 0 ) {
401                                 if ( retries != LDAP_BACK_RETRY_NEVER ) {
402                                         ldap_pvt_thread_yield();
403                                         if ( retries > 0 ) {
404                                                 retries--;
405                                         }
406                                         LDAP_BACK_TV_SET( &tv );
407                                         goto retry;
408                                 }
409                                 rc = LDAP_UNAVAILABLE;
410
411                         } else if ( rc == LDAP_RES_EXTENDED ) {
412                                 struct berval   *data = NULL;
413
414                                 rc = ldap_parse_extended_result( ld, res,
415                                                 NULL, &data, 0 );
416                                 if ( rc == LDAP_SUCCESS ) {
417                                         int err;
418                                         rc = ldap_parse_result( ld, res, &err,
419                                                 NULL, NULL, NULL, NULL, 1 );
420                                         if ( rc == LDAP_SUCCESS ) {
421                                                 rc = err;
422                                         }
423                                         res = NULL;
424                                         
425                                         /* FIXME: in case a referral 
426                                          * is returned, should we try
427                                          * using it instead of the 
428                                          * configured URI? */
429                                         if ( rc == LDAP_SUCCESS ) {
430                                                 rc = ldap_install_tls( ld );
431
432                                         } else if ( rc == LDAP_REFERRAL ) {
433                                                 rc = LDAP_UNWILLING_TO_PERFORM;
434                                                 *text = "unwilling to chase referral returned by Start TLS exop";
435                                         }
436
437                                         if ( data ) {
438                                                 if ( data->bv_val ) {
439                                                         ber_memfree( data->bv_val );
440                                                 }
441                                                 ber_memfree( data );
442                                         }
443                                 }
444
445                         } else {
446                                 rc = LDAP_OTHER;
447                         }
448
449                         if ( res != NULL ) {
450                                 ldap_msgfree( res );
451                         }
452                 }
453 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
454                 /*
455                  * use synchronous StartTLS
456                  */
457                 rc = ldap_start_tls_s( ld, NULL, NULL );
458 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
459
460                 /* if StartTLS is requested, only attempt it if the URL
461                  * is not "ldaps://"; this may occur not only in case
462                  * of misconfiguration, but also when used in the chain 
463                  * overlay, where the "uri" can be parsed out of a referral */
464                 switch ( rc ) {
465                 case LDAP_SUCCESS:
466                         *is_tls = 1;
467                         break;
468
469                 case LDAP_SERVER_DOWN:
470                         break;
471
472                 default:
473                         if ( LDAP_BACK_TLS_CRITICAL( &dummy ) ) {
474                                 *text = "could not start TLS";
475                                 break;
476                         }
477
478                         /* in case Start TLS is not critical */
479                         *is_tls = 0;
480                         rc = LDAP_SUCCESS;
481                         break;
482                 }
483
484         } else {
485                 *is_tls = 0;
486         }
487
488         return rc;
489 }
490 #endif /* HAVE_TLS */
491
492 static int
493 ldap_back_prepare_conn( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
494 {
495         ldapinfo_t      *li = (ldapinfo_t *)op->o_bd->be_private;
496         int             version;
497         LDAP            *ld = NULL;
498 #ifdef HAVE_TLS
499         int             is_tls = op->o_conn->c_is_tls;
500 #endif /* HAVE_TLS */
501
502         assert( lcp != NULL );
503
504         ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
505         rs->sr_err = ldap_initialize( &ld, li->li_uri );
506         ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
507         if ( rs->sr_err != LDAP_SUCCESS ) {
508                 goto error_return;
509         }
510
511         if ( li->li_urllist_f ) {
512                 ldap_set_urllist_proc( ld, li->li_urllist_f, li->li_urllist_p );
513         }
514
515         /* Set LDAP version. This will always succeed: If the client
516          * bound with a particular version, then so can we.
517          */
518         if ( li->li_version != 0 ) {
519                 version = li->li_version;
520
521         } else if ( op->o_protocol != 0 ) {
522                 version = op->o_protocol;
523
524         } else {
525                 /* assume it's an internal op; set to LDAPv3 */
526                 version = LDAP_VERSION3;
527         }
528         ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, (const void *)&version );
529
530         /* automatically chase referrals ("chase-referrals [{yes|no}]" statement) */
531         ldap_set_option( ld, LDAP_OPT_REFERRALS,
532                 LDAP_BACK_CHASE_REFERRALS( li ) ? LDAP_OPT_ON : LDAP_OPT_OFF );
533
534         if ( li->li_network_timeout > 0 ) {
535                 struct timeval          tv;
536
537                 tv.tv_sec = li->li_network_timeout;
538                 tv.tv_usec = 0;
539                 ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, (const void *)&tv );
540         }
541
542 #ifdef HAVE_TLS
543         ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
544         rs->sr_err = ldap_back_start_tls( ld, op->o_protocol, &is_tls,
545                         li->li_uri, li->li_flags, li->li_nretries, &rs->sr_text );
546         ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
547         if ( rs->sr_err != LDAP_SUCCESS ) {
548                 ldap_unbind_ext( ld, NULL, NULL );
549                 goto error_return;
550         }
551 #endif /* HAVE_TLS */
552
553         if ( *lcp == NULL ) {
554                 *lcp = (ldapconn_t *)ch_calloc( 1, sizeof( ldapconn_t ) );
555                 (*lcp)->lc_flags = li->li_flags;
556         }
557         (*lcp)->lc_ld = ld;
558         (*lcp)->lc_refcnt = 1;
559         (*lcp)->lc_binding = 1;
560 #ifdef HAVE_TLS
561         if ( is_tls ) {
562                 LDAP_BACK_CONN_ISTLS_SET( *lcp );
563         } else {
564                 LDAP_BACK_CONN_ISTLS_CLEAR( *lcp );
565         }
566 #endif /* HAVE_TLS */
567
568 error_return:;
569         if ( rs->sr_err != LDAP_SUCCESS ) {
570                 rs->sr_err = slap_map_api2result( rs );
571                 if ( sendok & LDAP_BACK_SENDERR ) {
572                         if ( rs->sr_text == NULL ) {
573                                 rs->sr_text = "ldap_initialize() failed";
574                         }
575                         send_ldap_result( op, rs );
576                         rs->sr_text = NULL;
577                 }
578
579         } else {
580                 if ( li->li_conn_ttl > 0 ) {
581                         (*lcp)->lc_create_time = op->o_time;
582                 }
583         }
584
585         return rs->sr_err;
586 }
587
588 ldapconn_t *
589 ldap_back_getconn( Operation *op, SlapReply *rs, ldap_back_send_t sendok )
590 {
591         ldapinfo_t      *li = (ldapinfo_t *)op->o_bd->be_private;
592         ldapconn_t      *lc = NULL,
593                         lc_curr = { 0 };
594         int             refcnt = 1, binding = 1;
595
596         /* Internal searches are privileged and shared. So is root. */
597         if ( op->o_do_not_cache || be_isroot( op ) ) {
598                 LDAP_BACK_CONN_ISPRIV_SET( &lc_curr );
599                 lc_curr.lc_local_ndn = op->o_bd->be_rootndn;
600                 lc_curr.lc_conn = LDAP_BACK_PCONN_SET( op );
601
602         } else {
603                 lc_curr.lc_local_ndn = op->o_ndn;
604                 /* Explicit binds must not be shared */
605                 if ( op->o_tag == LDAP_REQ_BIND || SLAP_IS_AUTHZ_BACKEND( op ) ) {
606                         lc_curr.lc_conn = op->o_conn;
607         
608                 } else {
609                         lc_curr.lc_conn = LDAP_BACK_PCONN_SET( op );
610                 }
611         }
612
613         /* Explicit Bind requests always get their own conn */
614         if ( !( sendok & LDAP_BACK_BINDING ) ) {
615                 /* Searches for a ldapconn in the avl tree */
616 retry_lock:
617                 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
618
619                 lc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree, 
620                                 (caddr_t)&lc_curr, ldap_back_conndn_cmp );
621                 if ( lc != NULL ) {
622                         /* Don't reuse connections while they're still binding */
623                         if ( LDAP_BACK_CONN_BINDING( lc ) ) {
624                                 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
625                                 ldap_pvt_thread_yield();
626                                 goto retry_lock;
627                         }
628                         refcnt = ++lc->lc_refcnt;
629                         binding = ++lc->lc_binding;
630                 }
631                 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
632         }
633
634         /* Looks like we didn't get a bind. Open a new session... */
635         if ( lc == NULL ) {
636                 if ( ldap_back_prepare_conn( &lc, op, rs, sendok ) != LDAP_SUCCESS ) {
637                         return NULL;
638                 }
639                 if ( sendok & LDAP_BACK_BINDING ) {
640                         LDAP_BACK_CONN_BINDING_SET( lc );
641                 }
642                 lc->lc_conn = lc_curr.lc_conn;
643                 ber_dupbv( &lc->lc_local_ndn, &lc_curr.lc_local_ndn );
644
645                 if ( LDAP_BACK_CONN_ISPRIV( &lc_curr ) ) {
646                         ber_dupbv( &lc->lc_cred, &li->li_acl_passwd );
647                         ber_dupbv( &lc->lc_bound_ndn, &li->li_acl_authcDN );
648                         LDAP_BACK_CONN_ISPRIV_SET( lc );
649
650                 } else {
651                         BER_BVZERO( &lc->lc_cred );
652                         BER_BVZERO( &lc->lc_bound_ndn );
653 #if 0
654                         /* FIXME: if we set lc_bound_ndn = o_ndn
655                          * we end up with a bind with DN but no password! */
656                         if ( !BER_BVISEMPTY( &op->o_ndn )
657                                 && SLAP_IS_AUTHZ_BACKEND( op ) )
658                         {
659                                 ber_dupbv( &lc->lc_bound_ndn, &op->o_ndn );
660                         }
661 #endif
662                 }
663
664 #ifdef HAVE_TLS
665                 /* if start TLS failed but it was not mandatory,
666                  * check if the non-TLS connection was already
667                  * in cache; in case, destroy the newly created
668                  * connection and use the existing one */
669                 if ( lc->lc_conn == LDAP_BACK_PCONN_TLS
670                                 && !ldap_tls_inplace( lc->lc_ld ) )
671                 {
672                         ldapconn_t *tmplc;
673                         
674                         lc_curr.lc_conn = LDAP_BACK_PCONN;
675                         ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
676                         tmplc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree, 
677                                         (caddr_t)&lc_curr, ldap_back_conndn_cmp );
678                         if ( tmplc != NULL ) {
679                                 refcnt = ++tmplc->lc_refcnt;
680                                 binding = ++tmplc->lc_binding;
681                                 ldap_back_conn_free( lc );
682                                 lc = tmplc;
683                         }
684                         ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
685
686                         if ( tmplc != NULL ) {
687                                 goto done;
688                         }
689                 }
690 #endif /* HAVE_TLS */
691
692                 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
693
694                 /* Inserts the newly created ldapconn in the avl tree */
695                 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
696
697                 assert( lc->lc_refcnt == 1 );
698                 assert( lc->lc_binding == 1 );
699                 rs->sr_err = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
700                         ldap_back_conndn_cmp, ldap_back_conndn_dup );
701
702 #if PRINT_CONNTREE > 0
703                 myprint( li->li_conninfo.lai_tree, "ldap_back_getconn" );
704 #endif /* PRINT_CONNTREE */
705         
706                 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
707
708                 Debug( LDAP_DEBUG_TRACE,
709                         "=>ldap_back_getconn: conn %p inserted refcnt=%u binding=%u\n",
710                         (void *)lc, refcnt, binding );
711         
712                 /* Err could be -1 in case a duplicate ldapconn is inserted */
713                 switch ( rs->sr_err ) {
714                 case 0:
715                         break;
716
717                 case -1:
718                         if ( !( sendok & LDAP_BACK_BINDING ) ) {
719                                 /* duplicate: free and try to get the newly created one */
720                                 goto retry_lock;
721                         }
722                         /* taint connection, so that it'll be freed when released */
723                         ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
724                         (void *)avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
725                                         ldap_back_conndnlc_cmp );
726                         ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
727                         LDAP_BACK_CONN_TAINTED_SET( lc );
728                         break;
729
730                 default:
731                         ldap_back_conn_free( lc );
732                         rs->sr_err = LDAP_OTHER;
733                         rs->sr_text = "proxy bind collision";
734                         if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
735                                 send_ldap_result( op, rs );
736                                 rs->sr_text = NULL;
737                         }
738                         return NULL;
739                 }
740
741         } else {
742                 char    buf[ SLAP_TEXT_BUFLEN ];
743                 int     expiring = 0;
744
745                 if ( ( li->li_idle_timeout != 0 && op->o_time > lc->lc_time + li->li_idle_timeout )
746                         || ( li->li_conn_ttl != 0 && op->o_time > lc->lc_create_time + li->li_conn_ttl ) )
747                 {
748                         expiring = 1;
749
750                         /* let it be used, but taint/delete it so that 
751                          * no-one else can look it up any further */
752                         ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
753                         (void *)avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
754                                         ldap_back_conndnlc_cmp );
755                         ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
756                         LDAP_BACK_CONN_TAINTED_SET( lc );
757                 }
758
759                 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
760                         snprintf( buf, sizeof( buf ),
761                                 "conn %p fetched refcnt=%u binding=%u%s",
762                                 (void *)lc, refcnt, binding, expiring ? " expiring" : "" );
763                         Debug( LDAP_DEBUG_TRACE,
764                                 "=>ldap_back_getconn: %s.\n", buf, 0, 0 );
765                 }
766         
767         }
768
769 #ifdef HAVE_TLS
770 done:;
771 #endif /* HAVE_TLS */
772         if ( li->li_idle_timeout && lc ) {
773                 lc->lc_time = op->o_time;
774         }
775
776         return lc;
777 }
778
779 void
780 ldap_back_release_conn_lock(
781         Operation               *op,
782         SlapReply               *rs,
783         ldapconn_t              *lc,
784         int                     dolock )
785 {
786         ldapinfo_t      *li = (ldapinfo_t *)op->o_bd->be_private;
787
788         if ( dolock ) {
789                 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
790         }
791         assert( lc->lc_refcnt > 0 );
792         LDAP_BACK_CONN_BINDING_CLEAR( lc );
793         lc->lc_refcnt--;
794         if ( LDAP_BACK_CONN_TAINTED( lc ) ) {
795                 ldap_back_freeconn( op, lc, 0 );
796         }
797         if ( dolock ) {
798                 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
799         }
800 }
801
802 /*
803  * ldap_back_dobind
804  *
805  * Note: as the check for the value of lc->lc_bound was already here, I removed
806  * it from all the callers, and I made the function return the flag, so
807  * it can be used to simplify the check.
808  *
809  * Note: dolock indicates whether li->li_conninfo.lai_mutex must be locked or not
810  */
811 static int
812 ldap_back_dobind_int(
813         ldapconn_t              *lc,
814         Operation               *op,
815         SlapReply               *rs,
816         ldap_back_send_t        sendok,
817         int                     retries,
818         int                     dolock )
819 {       
820         ldapinfo_t      *li = (ldapinfo_t *)op->o_bd->be_private;
821
822         int             rc, binding = 0;
823         ber_int_t       msgid;
824
825         assert( retries >= 0 );
826
827 retry_lock:;
828         if ( dolock ) {
829                 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
830         }
831
832         if ( binding == 0 ) {
833                 /* check if already bound */
834                 rc = LDAP_BACK_CONN_ISBOUND( lc );
835                 if ( rc ) {
836                         lc->lc_binding--;
837                         if ( dolock ) {
838                                 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
839                         }
840                         return rc;
841                 }
842
843                 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
844                         /* if someone else is about to bind it, give up and retry */
845                         if ( dolock ) {
846                                 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
847                         }
848                         ldap_pvt_thread_yield();
849                         goto retry_lock;
850
851                 } else {
852                         /* otherwise this thread will bind it */
853                         LDAP_BACK_CONN_BINDING_SET( lc );
854                         binding = 1;
855                 }
856         }
857
858         /* wait for pending operations to finish */
859         /* FIXME: may become a bottleneck! */
860         if ( lc->lc_refcnt != lc->lc_binding ) {
861                 if ( dolock ) {
862                         ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
863                 }
864                 ldap_pvt_thread_yield();
865                 goto retry_lock;
866         }
867
868         if ( dolock ) {
869                 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
870         }
871
872 #if 0
873         while ( lc->lc_refcnt > 1 ) {
874                 ldap_pvt_thread_yield();
875                 rc = LDAP_BACK_CONN_ISBOUND( lc );
876                 if ( rc ) {
877                         return rc;
878                 }
879         }
880
881         if ( dolock ) {
882                 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
883         }
884         LDAP_BACK_CONN_BINDING_SET( lc );
885         if ( dolock ) {
886                 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
887         }
888 #endif
889
890         /*
891          * FIXME: we need to let clients use proxyAuthz
892          * otherwise we cannot do symmetric pools of servers;
893          * we have to live with the fact that a user can
894          * authorize itself as any ID that is allowed
895          * by the authzTo directive of the "proxyauthzdn".
896          */
897         /*
898          * NOTE: current Proxy Authorization specification
899          * and implementation do not allow proxy authorization
900          * control to be provided with Bind requests
901          */
902         /*
903          * if no bind took place yet, but the connection is bound
904          * and the "idassert-authcDN" (or other ID) is set, 
905          * then bind as the asserting identity and explicitly 
906          * add the proxyAuthz control to every operation with the
907          * dn bound to the connection as control value.
908          * This is done also if this is the authrizing backend,
909          * but the "override" flag is given to idassert.
910          * It allows to use SASL bind and yet proxyAuthz users
911          */
912         if ( op->o_conn != NULL &&
913                         !op->o_do_not_cache &&
914                         ( BER_BVISNULL( &lc->lc_bound_ndn ) ||
915                           ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) )
916         {
917                 (void)ldap_back_proxy_authz_bind( lc, op, rs, sendok );
918                 goto done;
919         }
920
921 #ifdef HAVE_CYRUS_SASL
922         if ( LDAP_BACK_CONN_ISPRIV( lc )
923                 && li->li_acl_authmethod == LDAP_AUTH_SASL )
924         {
925                 void            *defaults = NULL;
926
927                 if ( li->li_acl_secprops != NULL ) {
928                         rc = ldap_set_option( lc->lc_ld,
929                                 LDAP_OPT_X_SASL_SECPROPS, li->li_acl_secprops);
930
931                         if ( rc != LDAP_OPT_SUCCESS ) {
932                                 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
933                                         "(SECPROPS,\"%s\") failed!\n",
934                                         li->li_acl_secprops, 0, 0 );
935                                 goto done;
936                         }
937                 }
938
939                 defaults = lutil_sasl_defaults( lc->lc_ld,
940                                 li->li_acl_sasl_mech.bv_val,
941                                 li->li_acl_sasl_realm.bv_val,
942                                 li->li_acl_authcID.bv_val,
943                                 li->li_acl_passwd.bv_val,
944                                 NULL );
945
946                 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld,
947                                 li->li_acl_authcDN.bv_val,
948                                 li->li_acl_sasl_mech.bv_val, NULL, NULL,
949                                 LDAP_SASL_QUIET, lutil_sasl_interact,
950                                 defaults );
951
952                 lutil_sasl_freedefs( defaults );
953
954                 rs->sr_err = slap_map_api2result( rs );
955                 if ( rs->sr_err != LDAP_SUCCESS ) {
956                         LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
957                         send_ldap_result( op, rs );
958
959                 } else {
960                         LDAP_BACK_CONN_ISBOUND_SET( lc );
961                 }
962                 goto done;
963         }
964 #endif /* HAVE_CYRUS_SASL */
965
966 retry:;
967         rs->sr_err = ldap_sasl_bind( lc->lc_ld,
968                         lc->lc_bound_ndn.bv_val,
969                         LDAP_SASL_SIMPLE, &lc->lc_cred,
970                         NULL, NULL, &msgid );
971
972         if ( rs->sr_err == LDAP_SERVER_DOWN ) {
973                 if ( retries != LDAP_BACK_RETRY_NEVER ) {
974                         if ( dolock ) {
975                                 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
976                         }
977
978                         assert( lc->lc_refcnt > 0 );
979                         if ( lc->lc_refcnt == 1 ) {
980                                 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
981                                 lc->lc_ld = NULL;
982
983                                 /* lc here must be the regular lc, reset and ready for init */
984                                 rs->sr_err = ldap_back_prepare_conn( &lc, op, rs, sendok );
985                                 if ( rs->sr_err != LDAP_SUCCESS ) {
986                                         lc->lc_binding--;
987                                         lc->lc_refcnt = 0;
988                                 }
989                         }
990
991                         if ( dolock ) {
992                                 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
993                         }
994
995                         if ( rs->sr_err == LDAP_SUCCESS ) {
996                                 if ( retries > 0 ) {
997                                         retries--;
998                                 }
999                                 goto retry;
1000                         }
1001
1002                 } else {
1003                         if ( dolock ) {
1004                                 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1005                         }
1006                         lc->lc_binding--;
1007                         if ( dolock ) {
1008                                 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1009                         }
1010                 }
1011
1012                 lc->lc_binding--;
1013                 ldap_back_freeconn( op, lc, dolock );
1014                 rs->sr_err = slap_map_api2result( rs );
1015
1016                 return 0;
1017         }
1018
1019         rc = ldap_back_op_result( lc, op, rs, msgid, 0, sendok );
1020         if ( rc == LDAP_SUCCESS ) {
1021                 LDAP_BACK_CONN_ISBOUND_SET( lc );
1022         }
1023
1024 done:;
1025         lc->lc_binding--;
1026         LDAP_BACK_CONN_BINDING_CLEAR( lc );
1027         rc = LDAP_BACK_CONN_ISBOUND( lc );
1028         if ( !rc ) {
1029                 ldap_back_release_conn_lock( op, rs, lc, dolock );
1030         }
1031
1032         return rc;
1033 }
1034
1035 int
1036 ldap_back_dobind( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1037 {
1038         ldapinfo_t      *li = (ldapinfo_t *)op->o_bd->be_private;
1039
1040         return ldap_back_dobind_int( lc, op, rs, sendok, li->li_nretries, 1 );
1041 }
1042
1043 /*
1044  * ldap_back_default_rebind
1045  *
1046  * This is a callback used for chasing referrals using the same
1047  * credentials as the original user on this session.
1048  */
1049 int 
1050 ldap_back_default_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
1051         ber_int_t msgid, void *params )
1052 {
1053         ldapconn_t      *lc = (ldapconn_t *)params;
1054
1055 #ifdef HAVE_TLS
1056         /* ... otherwise we couldn't get here */
1057         assert( lc != NULL );
1058
1059         if ( !ldap_tls_inplace( ld ) ) {
1060                 int             is_tls = LDAP_BACK_CONN_ISTLS( lc ),
1061                                 rc;
1062                 const char      *text = NULL;
1063
1064                 rc = ldap_back_start_tls( ld, 0, &is_tls, url, lc->lc_flags,
1065                         LDAP_BACK_RETRY_DEFAULT, &text );
1066                 if ( rc != LDAP_SUCCESS ) {
1067                         return rc;
1068                 }
1069         }
1070 #endif /* HAVE_TLS */
1071
1072         /* FIXME: add checks on the URL/identity? */
1073
1074         return ldap_sasl_bind_s( ld, lc->lc_bound_ndn.bv_val,
1075                         LDAP_SASL_SIMPLE, &lc->lc_cred, NULL, NULL, NULL );
1076 }
1077
1078 /*
1079  * ldap_back_default_urllist
1080  */
1081 int 
1082 ldap_back_default_urllist(
1083         LDAP            *ld,
1084         LDAPURLDesc     **urllist,
1085         LDAPURLDesc     **url,
1086         void            *params )
1087 {
1088         ldapinfo_t      *li = (ldapinfo_t *)params;
1089         LDAPURLDesc     **urltail;
1090
1091         if ( urllist == url ) {
1092                 return LDAP_SUCCESS;
1093         }
1094
1095         for ( urltail = &(*url)->lud_next; *urltail; urltail = &(*urltail)->lud_next )
1096                 /* count */ ;
1097
1098         *urltail = *urllist;
1099         *urllist = *url;
1100         *url = NULL;
1101
1102         ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
1103         if ( li->li_uri ) {
1104                 ch_free( li->li_uri );
1105         }
1106
1107         ldap_get_option( ld, LDAP_OPT_URI, (void *)&li->li_uri );
1108         ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
1109
1110         return LDAP_SUCCESS;
1111 }
1112
1113 int
1114 ldap_back_op_result(
1115                 ldapconn_t              *lc,
1116                 Operation               *op,
1117                 SlapReply               *rs,
1118                 ber_int_t               msgid,
1119                 time_t                  timeout,
1120                 ldap_back_send_t        sendok )
1121 {
1122         char            *match = NULL;
1123         LDAPMessage     *res = NULL;
1124         char            *text = NULL;
1125
1126 #define ERR_OK(err) ((err) == LDAP_SUCCESS || (err) == LDAP_COMPARE_FALSE || (err) == LDAP_COMPARE_TRUE)
1127
1128         rs->sr_text = NULL;
1129         rs->sr_matched = NULL;
1130
1131         /* if the error recorded in the reply corresponds
1132          * to a successful state, get the error from the
1133          * remote server response */
1134         if ( ERR_OK( rs->sr_err ) ) {
1135                 int             rc;
1136                 struct timeval  tv;
1137
1138                 if ( timeout ) {
1139                         tv.tv_sec = timeout;
1140                         tv.tv_usec = 0;
1141
1142                 } else {
1143                         LDAP_BACK_TV_SET( &tv );
1144                 }
1145
1146 retry:;
1147                 /* if result parsing fails, note the failure reason */
1148                 rc = ldap_result( lc->lc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
1149                 switch ( rc ) {
1150                 case 0:
1151                         if ( timeout ) {
1152                                 (void)ldap_abandon_ext( lc->lc_ld, msgid, NULL, NULL );
1153                                 rs->sr_err = op->o_protocol >= LDAP_VERSION3 ?
1154                                         LDAP_ADMINLIMIT_EXCEEDED : LDAP_OPERATIONS_ERROR;
1155                                 rs->sr_text = "Operation timed out";
1156                                 break;
1157                         }
1158
1159                         LDAP_BACK_TV_SET( &tv );
1160                         ldap_pvt_thread_yield();
1161                         goto retry;
1162
1163                 case -1:
1164                         ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
1165                                         &rs->sr_err );
1166                         break;
1167
1168
1169                 /* otherwise get the result; if it is not
1170                  * LDAP_SUCCESS, record it in the reply
1171                  * structure (this includes 
1172                  * LDAP_COMPARE_{TRUE|FALSE}) */
1173                 default:
1174                         rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
1175                                         &match, &text, NULL, NULL, 1 );
1176                         rs->sr_text = text;
1177                         if ( rc != LDAP_SUCCESS ) {
1178                                 rs->sr_err = rc;
1179                         }
1180                 }
1181         }
1182
1183         /* if the error in the reply structure is not
1184          * LDAP_SUCCESS, try to map it from client 
1185          * to server error */
1186         if ( !ERR_OK( rs->sr_err ) ) {
1187                 rs->sr_err = slap_map_api2result( rs );
1188
1189                 /* internal ops ( op->o_conn == NULL ) 
1190                  * must not reply to client */
1191                 if ( op->o_conn && !op->o_do_not_cache && match ) {
1192
1193                         /* record the (massaged) matched
1194                          * DN into the reply structure */
1195                         rs->sr_matched = match;
1196                 }
1197         }
1198         if ( op->o_conn &&
1199                         ( ( sendok & LDAP_BACK_SENDOK ) 
1200                           || ( ( sendok & LDAP_BACK_SENDERR ) && rs->sr_err != LDAP_SUCCESS ) ) )
1201         {
1202                 send_ldap_result( op, rs );
1203         }
1204         if ( match ) {
1205                 if ( rs->sr_matched != match ) {
1206                         free( (char *)rs->sr_matched );
1207                 }
1208                 rs->sr_matched = NULL;
1209                 ldap_memfree( match );
1210         }
1211         if ( text ) {
1212                 ldap_memfree( text );
1213         }
1214         rs->sr_text = NULL;
1215         return( ERR_OK( rs->sr_err ) ? LDAP_SUCCESS : rs->sr_err );
1216 }
1217
1218 /* return true if bound, false if failed */
1219 int
1220 ldap_back_retry( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1221 {
1222         int             rc = 0;
1223         ldapinfo_t      *li = (ldapinfo_t *)op->o_bd->be_private;
1224
1225         assert( lcp != NULL );
1226         assert( *lcp != NULL );
1227
1228         ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1229
1230         if ( (*lcp)->lc_refcnt == 1 ) {
1231                 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
1232                 Debug( LDAP_DEBUG_ANY,
1233                         "%s ldap_back_retry: retrying URI=\"%s\" DN=\"%s\"\n",
1234                         op->o_log_prefix, li->li_uri,
1235                         BER_BVISNULL( &(*lcp)->lc_bound_ndn ) ?
1236                                 "" : (*lcp)->lc_bound_ndn.bv_val );
1237                 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
1238
1239                 ldap_unbind_ext( (*lcp)->lc_ld, NULL, NULL );
1240                 (*lcp)->lc_ld = NULL;
1241                 LDAP_BACK_CONN_ISBOUND_CLEAR( (*lcp) );
1242
1243                 /* lc here must be the regular lc, reset and ready for init */
1244                 rc = ldap_back_prepare_conn( lcp, op, rs, sendok );
1245                 if ( rc != LDAP_SUCCESS ) {
1246                         rc = 0;
1247                         *lcp = NULL;
1248
1249                 } else {
1250                         rc = ldap_back_dobind_int( *lcp, op, rs, sendok, 0, 0 );
1251                         if ( rc == 0 ) {
1252                                 *lcp = NULL;
1253                         }
1254                 }
1255
1256         } else {
1257                 Debug( LDAP_DEBUG_TRACE,
1258                         "ldap_back_retry: conn %p refcnt=%u unable to retry.\n",
1259                         (void *)(*lcp), (*lcp)->lc_refcnt, 0 );
1260
1261                 ldap_back_release_conn_lock( op, rs, *lcp, 0 );
1262                 *lcp = NULL;
1263
1264                 if ( sendok ) {
1265                         rs->sr_err = LDAP_UNAVAILABLE;
1266                         rs->sr_text = "unable to retry";
1267                         send_ldap_result( op, rs );
1268                 }
1269         }
1270
1271         ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1272
1273         return rc;
1274 }
1275
1276 static int
1277 ldap_back_proxy_authz_bind( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1278 {
1279         ldapinfo_t      *li = (ldapinfo_t *)op->o_bd->be_private;
1280         struct berval   binddn = slap_empty_bv;
1281         struct berval   bindcred = slap_empty_bv;
1282         struct berval   ndn;
1283         int             dobind = 0;
1284         int             msgid;
1285         int             rc;
1286
1287         /* don't proxyAuthz if protocol is not LDAPv3 */
1288         switch ( li->li_version ) {
1289         case LDAP_VERSION3:
1290                 break;
1291
1292         case 0:
1293                 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
1294                         break;
1295                 }
1296                 /* fall thru */
1297
1298         default:
1299                 goto done;
1300         }
1301
1302         if ( op->o_tag == LDAP_REQ_BIND ) {
1303                 ndn = op->o_req_ndn;
1304
1305         } else if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1306                 ndn = op->o_conn->c_ndn;
1307
1308         } else {
1309                 ndn = op->o_ndn;
1310         }
1311
1312         /*
1313          * FIXME: we need to let clients use proxyAuthz
1314          * otherwise we cannot do symmetric pools of servers;
1315          * we have to live with the fact that a user can
1316          * authorize itself as any ID that is allowed
1317          * by the authzTo directive of the "proxyauthzdn".
1318          */
1319         /*
1320          * NOTE: current Proxy Authorization specification
1321          * and implementation do not allow proxy authorization
1322          * control to be provided with Bind requests
1323          */
1324         /*
1325          * if no bind took place yet, but the connection is bound
1326          * and the "proxyauthzdn" is set, then bind as 
1327          * "proxyauthzdn" and explicitly add the proxyAuthz 
1328          * control to every operation with the dn bound 
1329          * to the connection as control value.
1330          */
1331
1332         /* bind as proxyauthzdn only if no idassert mode
1333          * is requested, or if the client's identity
1334          * is authorized */
1335         switch ( li->li_idassert_mode ) {
1336         case LDAP_BACK_IDASSERT_LEGACY:
1337                 if ( !BER_BVISNULL( &ndn ) && !BER_BVISEMPTY( &ndn ) ) {
1338                         if ( !BER_BVISNULL( &li->li_idassert_authcDN ) && !BER_BVISEMPTY( &li->li_idassert_authcDN ) )
1339                         {
1340                                 binddn = li->li_idassert_authcDN;
1341                                 bindcred = li->li_idassert_passwd;
1342                                 dobind = 1;
1343                         }
1344                 }
1345                 break;
1346
1347         default:
1348                 /* NOTE: rootdn can always idassert */
1349                 if ( BER_BVISNULL( &ndn ) && li->li_idassert_authz == NULL ) {
1350                         if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1351                                 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
1352                                 if ( sendok & LDAP_BACK_SENDERR ) {
1353                                         send_ldap_result( op, rs );
1354                                 }
1355                                 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1356
1357                         } else {
1358                                 rs->sr_err = LDAP_SUCCESS;
1359                                 binddn = slap_empty_bv;
1360                                 bindcred = slap_empty_bv;
1361                                 break;
1362                         }
1363
1364                         goto done;
1365
1366                 } else if ( li->li_idassert_authz && !be_isroot( op ) ) {
1367                         struct berval authcDN;
1368
1369                         if ( BER_BVISNULL( &ndn ) ) {
1370                                 authcDN = slap_empty_bv;
1371
1372                         } else {
1373                                 authcDN = ndn;
1374                         }       
1375                         rs->sr_err = slap_sasl_matches( op, li->li_idassert_authz,
1376                                         &authcDN, &authcDN );
1377                         if ( rs->sr_err != LDAP_SUCCESS ) {
1378                                 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1379                                         if ( sendok & LDAP_BACK_SENDERR ) {
1380                                                 send_ldap_result( op, rs );
1381                                         }
1382                                         LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1383
1384                                 } else {
1385                                         rs->sr_err = LDAP_SUCCESS;
1386                                         binddn = slap_empty_bv;
1387                                         bindcred = slap_empty_bv;
1388                                         break;
1389                                 }
1390
1391                                 goto done;
1392                         }
1393                 }
1394
1395                 binddn = li->li_idassert_authcDN;
1396                 bindcred = li->li_idassert_passwd;
1397                 dobind = 1;
1398                 break;
1399         }
1400
1401         if ( dobind && li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
1402 #ifdef HAVE_CYRUS_SASL
1403                 void            *defaults = NULL;
1404                 struct berval   authzID = BER_BVNULL;
1405                 int             freeauthz = 0;
1406
1407                 /* if SASL supports native authz, prepare for it */
1408                 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
1409                                 ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
1410                 {
1411                         switch ( li->li_idassert_mode ) {
1412                         case LDAP_BACK_IDASSERT_OTHERID:
1413                         case LDAP_BACK_IDASSERT_OTHERDN:
1414                                 authzID = li->li_idassert_authzID;
1415                                 break;
1416
1417                         case LDAP_BACK_IDASSERT_ANONYMOUS:
1418                                 BER_BVSTR( &authzID, "dn:" );
1419                                 break;
1420
1421                         case LDAP_BACK_IDASSERT_SELF:
1422                                 if ( BER_BVISNULL( &ndn ) ) {
1423                                         /* connection is not authc'd, so don't idassert */
1424                                         BER_BVSTR( &authzID, "dn:" );
1425                                         break;
1426                                 }
1427                                 authzID.bv_len = STRLENOF( "dn:" ) + ndn.bv_len;
1428                                 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
1429                                 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
1430                                 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
1431                                                 ndn.bv_val, ndn.bv_len + 1 );
1432                                 freeauthz = 1;
1433                                 break;
1434
1435                         default:
1436                                 break;
1437                         }
1438                 }
1439
1440                 if ( li->li_idassert_secprops != NULL ) {
1441                         rs->sr_err = ldap_set_option( lc->lc_ld,
1442                                 LDAP_OPT_X_SASL_SECPROPS,
1443                                 (void *)li->li_idassert_secprops );
1444
1445                         if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
1446                                 rs->sr_err = LDAP_OTHER;
1447                                 if ( sendok & LDAP_BACK_SENDERR ) {
1448                                         send_ldap_result( op, rs );
1449                                 }
1450                                 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1451                                 goto done;
1452                         }
1453                 }
1454
1455                 defaults = lutil_sasl_defaults( lc->lc_ld,
1456                                 li->li_idassert_sasl_mech.bv_val,
1457                                 li->li_idassert_sasl_realm.bv_val,
1458                                 li->li_idassert_authcID.bv_val,
1459                                 li->li_idassert_passwd.bv_val,
1460                                 authzID.bv_val );
1461
1462                 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld, binddn.bv_val,
1463                                 li->li_idassert_sasl_mech.bv_val, NULL, NULL,
1464                                 LDAP_SASL_QUIET, lutil_sasl_interact,
1465                                 defaults );
1466
1467                 rs->sr_err = slap_map_api2result( rs );
1468                 if ( rs->sr_err != LDAP_SUCCESS ) {
1469                         LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1470                         if ( sendok & LDAP_BACK_SENDERR ) {
1471                                 send_ldap_result( op, rs );
1472                         }
1473
1474                 } else {
1475                         LDAP_BACK_CONN_ISBOUND_SET( lc );
1476                 }
1477
1478                 lutil_sasl_freedefs( defaults );
1479                 if ( freeauthz ) {
1480                         slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
1481                 }
1482
1483                 goto done;
1484 #endif /* HAVE_CYRUS_SASL */
1485         }
1486
1487         switch ( li->li_idassert_authmethod ) {
1488         case LDAP_AUTH_NONE:
1489                 LDAP_BACK_CONN_ISBOUND_SET( lc );
1490                 goto done;
1491
1492         case LDAP_AUTH_SIMPLE:
1493                 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
1494                                 binddn.bv_val, LDAP_SASL_SIMPLE,
1495                                 &bindcred, NULL, NULL, &msgid );
1496                 break;
1497
1498         default:
1499                 /* unsupported! */
1500                 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1501                 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
1502                 if ( sendok & LDAP_BACK_SENDERR ) {
1503                         send_ldap_result( op, rs );
1504                 }
1505                 goto done;
1506         }
1507
1508         rc = ldap_back_op_result( lc, op, rs, msgid, 0, sendok );
1509         if ( rc == LDAP_SUCCESS ) {
1510                 LDAP_BACK_CONN_ISBOUND_SET( lc );
1511         }
1512 done:;
1513         return LDAP_BACK_CONN_ISBOUND( lc );
1514 }
1515
1516 /*
1517  * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
1518  * to existing server-side controls if required; if not,
1519  * the existing server-side controls are placed in *pctrls.
1520  * The caller, after using the controls in client API 
1521  * operations, if ( *pctrls != op->o_ctrls ), should
1522  * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
1523  * The function returns success if the control could
1524  * be added if required, or if it did nothing; in the future,
1525  * it might return some error if it failed.
1526  * 
1527  * if no bind took place yet, but the connection is bound
1528  * and the "proxyauthzdn" is set, then bind as "proxyauthzdn" 
1529  * and explicitly add proxyAuthz the control to every operation
1530  * with the dn bound to the connection as control value.
1531  *
1532  * If no server-side controls are defined for the operation,
1533  * simply add the proxyAuthz control; otherwise, if the
1534  * proxyAuthz control is not already set, add it as
1535  * the first one
1536  *
1537  * FIXME: is controls order significant for security?
1538  * ANSWER: controls ordering and interoperability
1539  * must be indicated by the specs of each control; if none
1540  * is specified, the order is irrelevant.
1541  */
1542 int
1543 ldap_back_proxy_authz_ctrl(
1544                 ldapconn_t      *lc,
1545                 Operation       *op,
1546                 SlapReply       *rs,
1547                 LDAPControl     ***pctrls )
1548 {
1549         ldapinfo_t      *li = (ldapinfo_t *) op->o_bd->be_private;
1550         LDAPControl     **ctrls = NULL;
1551         int             i = 0,
1552                         mode;
1553         struct berval   assertedID,
1554                         ndn;
1555
1556         *pctrls = NULL;
1557
1558         rs->sr_err = LDAP_SUCCESS;
1559
1560         /* don't proxyAuthz if protocol is not LDAPv3 */
1561         switch ( li->li_version ) {
1562         case LDAP_VERSION3:
1563                 break;
1564
1565         case 0:
1566                 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
1567                         break;
1568                 }
1569                 /* fall thru */
1570
1571         default:
1572                 goto done;
1573         }
1574
1575         /* FIXME: SASL/EXTERNAL over ldapi:// doesn't honor the authcID,
1576          * but if it is not set this test fails.  We need a different
1577          * means to detect if idassert is enabled */
1578         if ( ( BER_BVISNULL( &li->li_idassert_authcID ) || BER_BVISEMPTY( &li->li_idassert_authcID ) )
1579                         && ( BER_BVISNULL( &li->li_idassert_authcDN ) || BER_BVISEMPTY( &li->li_idassert_authcDN ) ) )
1580         {
1581                 goto done;
1582         }
1583
1584         if ( !op->o_conn || op->o_do_not_cache || be_isroot( op ) ) {
1585                 goto done;
1586         }
1587
1588         if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1589                 ndn = op->o_conn->c_ndn;
1590
1591         } else {
1592                 ndn = op->o_ndn;
1593         }
1594
1595         if ( li->li_idassert_mode == LDAP_BACK_IDASSERT_LEGACY ) {
1596                 if ( op->o_proxy_authz ) {
1597                         /*
1598                          * FIXME: we do not want to perform proxyAuthz
1599                          * on behalf of the client, because this would
1600                          * be performed with "proxyauthzdn" privileges.
1601                          *
1602                          * This might actually be too strict, since
1603                          * the "proxyauthzdn" authzTo, and each entry's
1604                          * authzFrom attributes may be crafted
1605                          * to avoid unwanted proxyAuthz to take place.
1606                          */
1607 #if 0
1608                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1609                         rs->sr_text = "proxyAuthz not allowed within namingContext";
1610 #endif
1611                         goto done;
1612                 }
1613
1614                 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
1615                         goto done;
1616                 }
1617
1618                 if ( BER_BVISNULL( &ndn ) ) {
1619                         goto done;
1620                 }
1621
1622                 if ( BER_BVISNULL( &li->li_idassert_authcDN ) ) {
1623                         goto done;
1624                 }
1625
1626         } else if ( li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
1627                 if ( ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ )
1628                                 /* && ( !BER_BVISNULL( &ndn )
1629                                         || LDAP_BACK_CONN_ISBOUND( lc ) ) */ )
1630                 {
1631                         /* already asserted in SASL via native authz */
1632                         /* NOTE: the test on lc->lc_bound is used to trap
1633                          * native authorization of anonymous users,
1634                          * since in that case ndn is NULL */
1635                         goto done;
1636                 }
1637
1638         } else if ( li->li_idassert_authz && !be_isroot( op ) ) {
1639                 int             rc;
1640                 struct berval authcDN;
1641
1642                 if ( BER_BVISNULL( &ndn ) ) {
1643                         authcDN = slap_empty_bv;
1644                 } else {
1645                         authcDN = ndn;
1646                 }
1647                 rc = slap_sasl_matches( op, li->li_idassert_authz,
1648                                 &authcDN, & authcDN );
1649                 if ( rc != LDAP_SUCCESS ) {
1650                         if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE )
1651                         {
1652                                 /* ndn is not authorized
1653                                  * to use idassert */
1654                                 rs->sr_err = rc;
1655                         }
1656                         goto done;
1657                 }
1658         }
1659
1660         if ( op->o_proxy_authz ) {
1661                 /*
1662                  * FIXME: we can:
1663                  * 1) ignore the already set proxyAuthz control
1664                  * 2) leave it in place, and don't set ours
1665                  * 3) add both
1666                  * 4) reject the operation
1667                  *
1668                  * option (4) is very drastic
1669                  * option (3) will make the remote server reject
1670                  * the operation, thus being equivalent to (4)
1671                  * option (2) will likely break the idassert
1672                  * assumptions, so we cannot accept it;
1673                  * option (1) means that we are contradicting
1674                  * the client's reques.
1675                  *
1676                  * I think (4) is the only correct choice.
1677                  */
1678                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1679                 rs->sr_text = "proxyAuthz not allowed within namingContext";
1680         }
1681
1682         if ( op->o_is_auth_check ) {
1683                 mode = LDAP_BACK_IDASSERT_NOASSERT;
1684
1685         } else {
1686                 mode = li->li_idassert_mode;
1687         }
1688
1689         switch ( mode ) {
1690         case LDAP_BACK_IDASSERT_SELF:
1691                 if ( BER_BVISNULL( &ndn ) ) {
1692                         goto done;
1693                 }
1694                 assertedID = ndn;
1695                 break;
1696
1697         case LDAP_BACK_IDASSERT_LEGACY:
1698                 /* original behavior:
1699                  * assert the client's identity */
1700                 if ( BER_BVISNULL( &ndn ) ) {
1701                         assertedID = slap_empty_bv;
1702                 } else {
1703                         assertedID = ndn;
1704                 }
1705                 break;
1706
1707         case LDAP_BACK_IDASSERT_ANONYMOUS:
1708                 /* assert "anonymous" */
1709                 assertedID = slap_empty_bv;
1710                 break;
1711
1712         case LDAP_BACK_IDASSERT_NOASSERT:
1713                 /* don't assert; bind as proxyauthzdn */
1714                 goto done;
1715
1716         case LDAP_BACK_IDASSERT_OTHERID:
1717         case LDAP_BACK_IDASSERT_OTHERDN:
1718                 /* assert idassert DN */
1719                 assertedID = li->li_idassert_authzID;
1720                 break;
1721
1722         default:
1723                 assert( 0 );
1724         }
1725
1726         if ( BER_BVISNULL( &assertedID ) ) {
1727                 assertedID = slap_empty_bv;
1728         }
1729
1730         /* don't idassert the bound DN (ITS#4497) */
1731         if ( dn_match( &assertedID, &lc->lc_bound_ndn ) ) {
1732                 goto done;
1733         }
1734
1735         if ( op->o_ctrls ) {
1736                 for ( i = 0; op->o_ctrls[ i ]; i++ )
1737                         /* just count ctrls */ ;
1738         }
1739
1740         ctrls = op->o_tmpalloc( sizeof( LDAPControl * ) * (i + 2) + sizeof( LDAPControl ),
1741                         op->o_tmpmemctx );
1742         ctrls[ 0 ] = (LDAPControl *)&ctrls[ i + 2 ];
1743         
1744         ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1745         ctrls[ 0 ]->ldctl_iscritical = 1;
1746
1747         switch ( li->li_idassert_mode ) {
1748         /* already in u:ID or dn:DN form */
1749         case LDAP_BACK_IDASSERT_OTHERID:
1750         case LDAP_BACK_IDASSERT_OTHERDN:
1751                 ber_dupbv_x( &ctrls[ 0 ]->ldctl_value, &assertedID, op->o_tmpmemctx );
1752                 break;
1753
1754         /* needs the dn: prefix */
1755         default:
1756                 ctrls[ 0 ]->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
1757                 ctrls[ 0 ]->ldctl_value.bv_val = op->o_tmpalloc( ctrls[ 0 ]->ldctl_value.bv_len + 1,
1758                                 op->o_tmpmemctx );
1759                 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
1760                 AC_MEMCPY( &ctrls[ 0 ]->ldctl_value.bv_val[ STRLENOF( "dn:" ) ],
1761                                 assertedID.bv_val, assertedID.bv_len + 1 );
1762                 break;
1763         }
1764
1765         /* Older versions of <draft-weltman-ldapv3-proxy> required
1766          * to encode the value of the authzID (and called it proxyDN);
1767          * this hack provides compatibility with those DSAs that
1768          * implement it this way */
1769         if ( li->li_idassert_flags & LDAP_BACK_AUTH_OBSOLETE_ENCODING_WORKAROUND ) {
1770                 struct berval           authzID = ctrls[ 0 ]->ldctl_value;
1771                 BerElementBuffer        berbuf;
1772                 BerElement              *ber = (BerElement *)&berbuf;
1773                 ber_tag_t               tag;
1774
1775                 ber_init2( ber, 0, LBER_USE_DER );
1776                 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1777
1778                 tag = ber_printf( ber, "O", &authzID );
1779                 if ( tag == LBER_ERROR ) {
1780                         rs->sr_err = LDAP_OTHER;
1781                         goto free_ber;
1782                 }
1783
1784                 if ( ber_flatten2( ber, &ctrls[ 0 ]->ldctl_value, 1 ) == -1 ) {
1785                         rs->sr_err = LDAP_OTHER;
1786                         goto free_ber;
1787                 }
1788
1789 free_ber:;
1790                 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
1791                 ber_free_buf( ber );
1792
1793                 if ( rs->sr_err != LDAP_SUCCESS ) {
1794                         op->o_tmpfree( ctrls, op->o_tmpmemctx );
1795                         ctrls = NULL;
1796                         goto done;
1797                 }
1798
1799         } else if ( li->li_idassert_flags & LDAP_BACK_AUTH_OBSOLETE_PROXY_AUTHZ ) {
1800                 struct berval           authzID = ctrls[ 0 ]->ldctl_value,
1801                                         tmp;
1802                 BerElementBuffer        berbuf;
1803                 BerElement              *ber = (BerElement *)&berbuf;
1804                 ber_tag_t               tag;
1805
1806                 if ( strncasecmp( authzID.bv_val, "dn:", STRLENOF( "dn:" ) ) != 0 ) {
1807                         op->o_tmpfree( ctrls[ 0 ]->ldctl_value.bv_val, op->o_tmpmemctx );
1808                         op->o_tmpfree( ctrls, op->o_tmpmemctx );
1809                         ctrls = NULL;
1810                         rs->sr_err = LDAP_PROTOCOL_ERROR;
1811                         goto done;
1812                 }
1813
1814                 tmp = authzID;
1815                 tmp.bv_val += STRLENOF( "dn:" );
1816                 tmp.bv_len -= STRLENOF( "dn:" );
1817
1818                 ber_init2( ber, 0, LBER_USE_DER );
1819                 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1820
1821                 /* apparently, Mozilla API encodes this
1822                  * as "SEQUENCE { LDAPDN }" */
1823                 tag = ber_printf( ber, "{O}", &tmp );
1824                 if ( tag == LBER_ERROR ) {
1825                         rs->sr_err = LDAP_OTHER;
1826                         goto free_ber2;
1827                 }
1828
1829                 if ( ber_flatten2( ber, &ctrls[ 0 ]->ldctl_value, 1 ) == -1 ) {
1830                         rs->sr_err = LDAP_OTHER;
1831                         goto free_ber2;
1832                 }
1833
1834 free_ber2:;
1835                 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
1836                 ber_free_buf( ber );
1837
1838                 if ( rs->sr_err != LDAP_SUCCESS ) {
1839                         op->o_tmpfree( ctrls, op->o_tmpmemctx );
1840                         ctrls = NULL;
1841                         goto done;
1842                 }
1843
1844                 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ;
1845         }
1846
1847         if ( op->o_ctrls ) {
1848                 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
1849                         ctrls[ i + 1 ] = op->o_ctrls[ i ];
1850                 }
1851         }
1852         ctrls[ i + 1 ] = NULL;
1853
1854 done:;
1855         if ( ctrls == NULL ) {
1856                 ctrls = op->o_ctrls;
1857         }
1858
1859         *pctrls = ctrls;
1860         
1861         return rs->sr_err;
1862 }
1863
1864 int
1865 ldap_back_proxy_authz_ctrl_free( Operation *op, LDAPControl ***pctrls )
1866 {
1867         LDAPControl     **ctrls = *pctrls;
1868
1869         /* we assume that the first control is the proxyAuthz
1870          * added by back-ldap, so it's the only one we explicitly 
1871          * free */
1872         if ( ctrls && ctrls != op->o_ctrls ) {
1873                 assert( ctrls[ 0 ] != NULL );
1874
1875                 if ( !BER_BVISNULL( &ctrls[ 0 ]->ldctl_value ) ) {
1876                         op->o_tmpfree( ctrls[ 0 ]->ldctl_value.bv_val, op->o_tmpmemctx );
1877                 }
1878
1879                 op->o_tmpfree( ctrls, op->o_tmpmemctx );
1880         } 
1881
1882         *pctrls = NULL;
1883
1884         return 0;
1885 }