]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/bind.c
3c5e3f1dfca109b2f934096d934615c182df0aa4
[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-2005 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 #define PRINT_CONNTREE 0
39
40 /*
41  * FIXME: temporarily disable pooled connections, as there seem to be
42  * some concurrency problem
43  */
44 /* #undef LDAP_BACK_POOLED_CONNS */
45
46 static LDAP_REBIND_PROC ldap_back_rebind;
47
48 static int
49 ldap_back_proxy_authz_bind( struct ldapconn *lc, Operation *op, SlapReply *rs );
50
51 static int
52 ldap_back_prepare_conn( struct ldapconn **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok );
53
54 int
55 ldap_back_bind( Operation *op, SlapReply *rs )
56 {
57         struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
58         struct ldapconn *lc;
59
60         int rc = 0;
61         ber_int_t msgid;
62
63         lc = ldap_back_getconn( op, rs, LDAP_BACK_SENDERR );
64         if ( !lc ) {
65                 return rs->sr_err;
66         }
67
68         if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
69                 ch_free( lc->lc_bound_ndn.bv_val );
70                 BER_BVZERO( &lc->lc_bound_ndn );
71         }
72         lc->lc_bound = 0;
73
74         /* method is always LDAP_AUTH_SIMPLE if we got here */
75         rs->sr_err = ldap_sasl_bind( lc->lc_ld, op->o_req_dn.bv_val,
76                         LDAP_SASL_SIMPLE,
77                         &op->orb_cred, op->o_ctrls, NULL, &msgid );
78         rc = ldap_back_op_result( lc, op, rs, msgid, LDAP_BACK_SENDERR );
79
80         if ( rc == LDAP_SUCCESS ) {
81                 /* If defined, proxyAuthz will be used also when
82                  * back-ldap is the authorizing backend; for this
83                  * purpose, a successful bind is followed by a
84                  * bind with the configured identity assertion */
85                 /* NOTE: use with care */
86                 if ( li->idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) {
87                         ldap_back_proxy_authz_bind( lc, op, rs );
88                         if ( lc->lc_bound == 0 ) {
89                                 rc = 1;
90                                 goto done;
91                         }
92                 }
93
94                 lc->lc_bound = 1;
95                 ber_dupbv( &lc->lc_bound_ndn, &op->o_req_ndn );
96
97                 if ( LDAP_BACK_SAVECRED( li ) ) {
98                         if ( !BER_BVISNULL( &lc->lc_cred ) ) {
99                                 memset( lc->lc_cred.bv_val, 0,
100                                                 lc->lc_cred.bv_len );
101                         }
102                         ber_bvreplace( &lc->lc_cred, &op->orb_cred );
103                         ldap_set_rebind_proc( lc->lc_ld, ldap_back_rebind, lc );
104                 }
105         }
106 done:;
107
108         /* must re-insert if local DN changed as result of bind */
109         if ( lc->lc_bound && !dn_match( &op->o_req_ndn, &lc->lc_local_ndn ) ) {
110                 int             lerr;
111
112                 /* wait for all other ops to release the connection */
113 retry_lock:;
114                 switch ( ldap_pvt_thread_mutex_trylock( &li->conn_mutex ) ) {
115                 case LDAP_PVT_THREAD_EBUSY:
116                 default:
117                         ldap_pvt_thread_yield();
118                         goto retry_lock;
119
120                 case 0:
121                         if ( lc->lc_refcnt > 1 ) {
122                                 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
123                                 ldap_pvt_thread_yield();
124                                 goto retry_lock;
125                         }
126                         break;
127                 }
128
129                 assert( lc->lc_refcnt == 1 );
130                 lc = avl_delete( &li->conntree, (caddr_t)lc,
131                                 ldap_back_conn_cmp );
132                 assert( lc != NULL );
133
134                 ber_bvreplace( &lc->lc_local_ndn, &op->o_req_ndn );
135                 lerr = avl_insert( &li->conntree, (caddr_t)lc,
136                         ldap_back_conn_cmp, ldap_back_conn_dup );
137                 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
138                 if ( lerr == -1 ) {
139                         /* we can do this because lc_refcnt == 1 */
140                         ldap_back_conn_free( lc );
141                         lc = NULL;
142                 }
143         }
144
145         if ( lc != NULL ) {
146                 ldap_back_release_conn( op, rs, lc );
147         }
148
149         return( rc );
150 }
151
152 /*
153  * ldap_back_conn_cmp
154  *
155  * compares two struct ldapconn based on the value of the conn pointer;
156  * used by avl stuff
157  */
158 int
159 ldap_back_conn_cmp( const void *c1, const void *c2 )
160 {
161         const struct ldapconn *lc1 = (const struct ldapconn *)c1;
162         const struct ldapconn *lc2 = (const struct ldapconn *)c2;
163         int rc;
164
165         /* If local DNs don't match, it is definitely not a match */
166         rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
167         if ( rc ) {
168                 return rc;
169         }
170
171         /* For shared sessions, conn is NULL. Only explicitly
172          * bound sessions will have non-NULL conn.
173          */
174         return SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
175 }
176
177 /*
178  * ldap_back_conn_dup
179  *
180  * returns -1 in case a duplicate struct ldapconn has been inserted;
181  * used by avl stuff
182  */
183 int
184 ldap_back_conn_dup( void *c1, void *c2 )
185 {
186         struct ldapconn *lc1 = (struct ldapconn *)c1;
187         struct ldapconn *lc2 = (struct ldapconn *)c2;
188
189         /* Cannot have more than one shared session with same DN */
190         if ( dn_match( &lc1->lc_local_ndn, &lc2->lc_local_ndn ) &&
191                         lc1->lc_conn == lc2->lc_conn )
192         {
193                 return -1;
194         }
195                 
196         return 0;
197 }
198
199 #if PRINT_CONNTREE > 0
200 static void
201 ravl_print( Avlnode *root, int depth )
202 {
203         int     i;
204         struct ldapconn *lc;
205         
206         if ( root == 0 ) {
207                 return;
208         }
209         
210         ravl_print( root->avl_right, depth+1 );
211         
212         for ( i = 0; i < depth; i++ ) {
213                 printf( "   " );
214         }
215
216         lc = root->avl_data;
217         printf( "lc(%lx) local(%s) conn(%lx) %d\n",
218                         lc, lc->lc_local_ndn.bv_val, lc->lc_conn, root->avl_bf );
219         
220         ravl_print( root->avl_left, depth+1 );
221 }
222
223 static void
224 myprint( Avlnode *root )
225 {
226         printf( "********\n" );
227         
228         if ( root == 0 ) {
229                 printf( "\tNULL\n" );
230
231         } else {
232                 ravl_print( root, 0 );
233         }
234         
235         printf( "********\n" );
236 }
237 #endif /* PRINT_CONNTREE */
238
239 int
240 ldap_back_freeconn( Operation *op, struct ldapconn *lc )
241 {
242         struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
243
244 retry_lock:;
245         switch ( ldap_pvt_thread_mutex_trylock( &li->conn_mutex ) ) {
246         case LDAP_PVT_THREAD_EBUSY:
247         default:
248                 ldap_pvt_thread_yield();
249                 goto retry_lock;
250
251         case 0:
252                 break;
253         }
254
255         assert( lc->lc_refcnt > 0 );
256         if ( --lc->lc_refcnt == 0 ) {
257                 lc = avl_delete( &li->conntree, (caddr_t)lc,
258                                 ldap_back_conn_cmp );
259                 assert( lc != NULL );
260
261                 ldap_back_conn_free( (void *)lc );
262         }
263
264         ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
265
266         return 0;
267 }
268
269 static int
270 ldap_back_prepare_conn( struct ldapconn **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
271 {
272         struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
273         int             vers = op->o_protocol;
274         LDAP            *ld = NULL;
275
276         assert( lcp != NULL );
277
278         rs->sr_err = ldap_initialize( &ld, li->url );
279         if ( rs->sr_err != LDAP_SUCCESS ) {
280                 goto error_return;
281         }
282
283         /* Set LDAP version. This will always succeed: If the client
284          * bound with a particular version, then so can we.
285          */
286         ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, (const void *)&vers );
287
288         /* automatically chase referrals ("[dont-]chase-referrals" statement) */
289         if ( LDAP_BACK_CHASE_REFERRALS( li ) ) {
290                 ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_ON );
291         }
292
293 #ifdef HAVE_TLS
294         /* start TLS ("tls-[try-]{start,propagate}" statements) */
295         if ( ( LDAP_BACK_USE_TLS( li ) || ( op->o_conn->c_is_tls && LDAP_BACK_PROPAGATE_TLS( li ) ) )
296                                 && !ldap_is_ldaps_url( li->url ) )
297         {
298 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
299                 /*
300                  * use asynchronous StartTLS
301                  * in case, chase referral (not implemented yet)
302                  */
303                 int             msgid;
304
305                 rs->sr_err = ldap_start_tls( ld, NULL, NULL, &msgid );
306                 if ( rs->sr_err == LDAP_SUCCESS ) {
307                         LDAPMessage     *res = NULL;
308                         int             rc, retries = 1;
309                         struct timeval  tv = { 0, 0 };
310
311 retry:;
312                         rc = ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res );
313                         if ( rc < 0 ) {
314                                 rs->sr_err = LDAP_OTHER;
315
316                         } else if ( rc == 0 ) {
317                                 if ( retries ) {
318                                         retries--;
319                                         tv.tv_sec = 0;
320                                         tv.tv_usec = 100000;
321                                         goto retry;
322                                 }
323                                 rs->sr_err = LDAP_OTHER;
324
325                         } else if ( rc == LDAP_RES_EXTENDED ) {
326                                 struct berval   *data = NULL;
327
328                                 rs->sr_err = ldap_parse_extended_result( ld, res,
329                                                 NULL, &data, 0 );
330                                 if ( rs->sr_err == LDAP_SUCCESS ) {
331                                         rs->sr_err = ldap_result2error( ld, res, 1 );
332                                         res = NULL;
333                                         
334                                         /* FIXME: in case a referral 
335                                          * is returned, should we try
336                                          * using it instead of the 
337                                          * configured URI? */
338                                         if ( rs->sr_err == LDAP_SUCCESS ) {
339                                                 ldap_install_tls( ld );
340
341                                         } else if ( rs->sr_err == LDAP_REFERRAL ) {
342                                                 rs->sr_err = LDAP_OTHER;
343                                                 rs->sr_text = "unwilling to chase referral returned by Start TLS exop";
344                                         }
345
346                                         if ( data ) {
347                                                 if ( data->bv_val ) {
348                                                         ber_memfree( data->bv_val );
349                                                 }
350                                                 ber_memfree( data );
351                                         }
352                                 }
353
354                         } else {
355                                 rs->sr_err = LDAP_OTHER;
356                         }
357
358                         if ( res != NULL ) {
359                                 ldap_msgfree( res );
360                         }
361                 }
362 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
363                 /*
364                  * use synchronous StartTLS
365                  */
366                 rs->sr_err = ldap_start_tls_s( ld, NULL, NULL );
367 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
368
369                 /* if StartTLS is requested, only attempt it if the URL
370                  * is not "ldaps://"; this may occur not only in case
371                  * of misconfiguration, but also when used in the chain 
372                  * overlay, where the "uri" can be parsed out of a referral */
373                 if ( rs->sr_err == LDAP_SERVER_DOWN
374                                 || ( rs->sr_err != LDAP_SUCCESS && LDAP_BACK_TLS_CRITICAL( li ) ) )
375                 {
376                         ldap_unbind_ext_s( ld, NULL, NULL );
377                         goto error_return;
378                 }
379
380                 /* in case Start TLS is not critical */
381                 rs->sr_err = LDAP_SUCCESS;
382         }
383 #endif /* HAVE_TLS */
384
385         if ( *lcp == NULL ) {
386                 *lcp = (struct ldapconn *)ch_calloc( 1, sizeof( struct ldapconn ) );
387         }
388         (*lcp)->lc_ld = ld;
389         (*lcp)->lc_refcnt = 1;
390
391 error_return:;
392         if ( rs->sr_err != LDAP_SUCCESS ) {
393                 rs->sr_err = slap_map_api2result( rs );
394                 if ( sendok & LDAP_BACK_SENDERR ) {
395                         if ( rs->sr_text == NULL ) {
396                                 rs->sr_text = "ldap_initialize() failed";
397                         }
398                         send_ldap_result( op, rs );
399                         rs->sr_text = NULL;
400                 }
401         }
402
403         return rs->sr_err;
404 }
405
406 struct ldapconn *
407 ldap_back_getconn( Operation *op, SlapReply *rs, ldap_back_send_t sendok )
408 {
409         struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
410         struct ldapconn *lc, lc_curr = { 0 };
411         int             refcnt = 1;
412
413         /* Searches for a ldapconn in the avl tree */
414
415         /* Explicit binds must not be shared */
416         if ( op->o_tag == LDAP_REQ_BIND
417                 || ( op->o_conn
418                         && op->o_conn->c_authz_backend
419                         && op->o_bd->be_private == op->o_conn->c_authz_backend->be_private ) )
420         {
421                 lc_curr.lc_conn = op->o_conn;
422
423         } else {
424                 lc_curr.lc_conn = NULL;
425         }
426         
427         /* Internal searches are privileged and shared. So is root. */
428 #ifdef LDAP_BACK_POOLED_CONNS
429         /* FIXME: there seem to be concurrency issues */
430         if ( op->o_do_not_cache || be_isroot( op ) ) {
431                 lc_curr.lc_local_ndn = op->o_bd->be_rootndn;
432                 lc_curr.lc_conn = NULL;
433                 lc_curr.lc_ispriv = 1;
434
435         } else 
436 #endif /* LDAP_BACK_POOLED_CONNS */
437         {
438                 lc_curr.lc_local_ndn = op->o_ndn;
439         }
440
441 retry_lock:;
442         switch ( ldap_pvt_thread_mutex_trylock( &li->conn_mutex ) ) {
443         case LDAP_PVT_THREAD_EBUSY:
444         default:
445                 ldap_pvt_thread_yield();
446                 goto retry_lock;
447
448         case 0:
449                 break;
450         }
451
452         lc = (struct ldapconn *)avl_find( li->conntree, 
453                         (caddr_t)&lc_curr, ldap_back_conn_cmp );
454         if ( lc != NULL ) {
455                 refcnt = ++lc->lc_refcnt;
456         }
457         ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
458
459         /* Looks like we didn't get a bind. Open a new session... */
460         if ( lc == NULL ) {
461                 if ( ldap_back_prepare_conn( &lc, op, rs, sendok ) != LDAP_SUCCESS ) {
462                         return NULL;
463                 }
464
465                 lc->lc_conn = lc_curr.lc_conn;
466                 ber_dupbv( &lc->lc_local_ndn, &lc_curr.lc_local_ndn );
467
468                 ldap_pvt_thread_mutex_init( &lc->lc_mutex );
469
470                 if ( lc_curr.lc_ispriv ) {
471                         ber_dupbv( &lc->lc_cred, &li->acl_passwd );
472                         ber_dupbv( &lc->lc_bound_ndn, &li->acl_authcDN );
473                         lc->lc_ispriv = lc_curr.lc_ispriv;
474
475                 } else {
476                         BER_BVZERO( &lc->lc_cred );
477                         BER_BVZERO( &lc->lc_bound_ndn );
478                         if ( op->o_conn && !BER_BVISEMPTY( &op->o_ndn )
479                                 && op->o_bd->be_private == op->o_conn->c_authz_backend->be_private )
480                         {
481                                 ber_dupbv( &lc->lc_bound_ndn, &op->o_ndn );
482                         }
483                 }
484
485                 lc->lc_bound = 0;
486
487                 /* Inserts the newly created ldapconn in the avl tree */
488 retry_lock2:;
489                 switch ( ldap_pvt_thread_mutex_trylock( &li->conn_mutex ) ) {
490                 case LDAP_PVT_THREAD_EBUSY:
491                 default:
492                         ldap_pvt_thread_yield();
493                         goto retry_lock2;
494
495                 case 0:
496                         break;
497                 }
498
499                 assert( lc->lc_refcnt == 1 );
500                 rs->sr_err = avl_insert( &li->conntree, (caddr_t)lc,
501                         ldap_back_conn_cmp, ldap_back_conn_dup );
502
503 #if PRINT_CONNTREE > 0
504                 myprint( li->conntree );
505 #endif /* PRINT_CONNTREE */
506         
507                 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
508
509                 Debug( LDAP_DEBUG_TRACE,
510                         "=>ldap_back_getconn: conn %p inserted (refcnt=%u)\n",
511                         (void *)lc, refcnt, 0 );
512         
513                 /* Err could be -1 in case a duplicate ldapconn is inserted */
514                 if ( rs->sr_err != 0 ) {
515                         ldap_back_conn_free( lc );
516                         rs->sr_err = LDAP_OTHER;
517                         if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
518                                 send_ldap_error( op, rs, LDAP_OTHER,
519                                         "internal server error" );
520                         }
521                         return NULL;
522                 }
523
524         } else {
525                 Debug( LDAP_DEBUG_TRACE,
526                         "=>ldap_back_getconn: conn %p fetched (refcnt=%u)\n",
527                         (void *)lc, refcnt, 0 );
528         }
529         
530         return lc;
531 }
532
533 void
534 ldap_back_release_conn(
535         Operation               *op,
536         SlapReply               *rs,
537         struct ldapconn         *lc )
538 {
539         struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
540
541 retry_lock:;
542         switch ( ldap_pvt_thread_mutex_trylock( &li->conn_mutex ) ) {
543         case LDAP_PVT_THREAD_EBUSY:
544         default:
545                 ldap_pvt_thread_yield();
546                 goto retry_lock;
547
548         case 0:
549                 break;
550         }
551
552         assert( lc->lc_refcnt > 0 );
553         lc->lc_refcnt--;
554         ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
555 }
556
557 /*
558  * ldap_back_dobind
559  *
560  * Note: as the check for the value of lc->lc_bound was already here, I removed
561  * it from all the callers, and I made the function return the flag, so
562  * it can be used to simplify the check.
563  *
564  * Note: lc_mutex is locked; dolock indicates whether li->conn_mutex
565  * must be locked or not
566  */
567 static int
568 ldap_back_dobind_int(
569         struct ldapconn         *lc,
570         Operation               *op,
571         SlapReply               *rs,
572         ldap_back_send_t        sendok,
573         int                     retries,
574         int                     dolock )
575 {       
576         int             rc;
577         ber_int_t       msgid;
578
579         assert( retries >= 0 );
580
581         if ( !lc->lc_bound ) {
582                 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
583
584                 /*
585                  * FIXME: we need to let clients use proxyAuthz
586                  * otherwise we cannot do symmetric pools of servers;
587                  * we have to live with the fact that a user can
588                  * authorize itself as any ID that is allowed
589                  * by the authzTo directive of the "proxyauthzdn".
590                  */
591                 /*
592                  * NOTE: current Proxy Authorization specification
593                  * and implementation do not allow proxy authorization
594                  * control to be provided with Bind requests
595                  */
596                 /*
597                  * if no bind took place yet, but the connection is bound
598                  * and the "idassert-authcDN" (or other ID) is set, 
599                  * then bind as the asserting identity and explicitly 
600                  * add the proxyAuthz control to every operation with the
601                  * dn bound to the connection as control value.
602                  * This is done also if this is the authrizing backend,
603                  * but the "override" flag is given to idassert.
604                  * It allows to use SASL bind and yet proxyAuthz users
605                  */
606                 if ( op->o_conn != NULL &&
607                                 !op->o_do_not_cache &&
608                                 ( BER_BVISNULL( &lc->lc_bound_ndn ) ||
609                                   ( li->idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) )
610                 {
611                         (void)ldap_back_proxy_authz_bind( lc, op, rs );
612                         goto done;
613                 }
614
615 #ifdef HAVE_CYRUS_SASL
616                 if ( lc->lc_ispriv && li->acl_authmethod == LDAP_AUTH_SASL ) {
617                         void            *defaults = NULL;
618
619 #if 1   /* will deal with this later... */
620                         if ( li->acl_secprops != NULL ) {
621                                 rc = ldap_set_option( lc->lc_ld,
622                                         LDAP_OPT_X_SASL_SECPROPS, li->acl_secprops);
623
624                                 if( rc != LDAP_OPT_SUCCESS ) {
625                                         Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
626                                                 "(%s,SECPROPS,\"%s\") failed!\n",
627                                                 li->url, li->acl_secprops, 0 );
628                                         goto done;
629                                 }
630                         }
631 #endif
632
633                         defaults = lutil_sasl_defaults( lc->lc_ld,
634                                         li->acl_sasl_mech.bv_val,
635                                         li->acl_sasl_realm.bv_val,
636                                         li->acl_authcID.bv_val,
637                                         li->acl_passwd.bv_val,
638                                         NULL );
639
640                         rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld,
641                                         li->acl_authcDN.bv_val,
642                                         li->acl_sasl_mech.bv_val, NULL, NULL,
643                                         LDAP_SASL_QUIET, lutil_sasl_interact,
644                                         defaults );
645
646                         lutil_sasl_freedefs( defaults );
647
648                         rs->sr_err = slap_map_api2result( rs );
649                         if ( rs->sr_err != LDAP_SUCCESS ) {
650                                 lc->lc_bound = 0;
651                                 send_ldap_result( op, rs );
652
653                         } else {
654                                 lc->lc_bound = 1;
655                         }
656                         goto done;
657                 }
658 #endif /* HAVE_CYRUS_SASL */
659
660 retry:;
661                 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
662                                 lc->lc_bound_ndn.bv_val,
663                                 LDAP_SASL_SIMPLE, &lc->lc_cred,
664                                 NULL, NULL, &msgid );
665
666                 if ( rs->sr_err == LDAP_SERVER_DOWN ) {
667                         if ( retries > 0 ) {
668                                 if ( dolock ) {
669 retry_lock:;
670                                         switch ( ldap_pvt_thread_mutex_trylock( &li->conn_mutex ) ) {
671                                         case LDAP_PVT_THREAD_EBUSY:
672                                         default:
673                                                 ldap_pvt_thread_yield();
674                                                 goto retry_lock;
675
676                                         case 0:
677                                                 break;
678                                         }
679                                 }
680
681                                 assert( lc->lc_refcnt > 0 );
682                                 if ( lc->lc_refcnt == 1 ) {
683                                         ldap_unbind_ext_s( lc->lc_ld, NULL, NULL );
684                                         lc->lc_ld = NULL;
685
686                                         /* lc here must be the regular lc, reset and ready for init */
687                                         rs->sr_err = ldap_back_prepare_conn( &lc, op, rs, sendok );
688                                 }
689                                 if ( dolock ) {
690                                         ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
691                                 }
692                                 if ( rs->sr_err == LDAP_SUCCESS ) {
693                                         retries--;
694                                         goto retry;
695                                 }
696                         }
697
698                         ldap_back_freeconn( op, lc );
699                         rs->sr_err = slap_map_api2result( rs );
700
701                         return 0;
702                 }
703
704                 rc = ldap_back_op_result( lc, op, rs, msgid, sendok );
705                 if ( rc == LDAP_SUCCESS ) {
706                         lc->lc_bound = 1;
707                 }
708         }
709
710 done:;
711         rc = lc->lc_bound;
712         return rc;
713 }
714
715 int
716 ldap_back_dobind( struct ldapconn *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
717 {
718         int     rc;
719
720         ldap_pvt_thread_mutex_lock( &lc->lc_mutex );
721         rc = ldap_back_dobind_int( lc, op, rs, sendok, 1, 1 );
722         ldap_pvt_thread_mutex_unlock( &lc->lc_mutex );
723
724         return rc;
725 }
726
727 /*
728  * ldap_back_rebind
729  *
730  * This is a callback used for chasing referrals using the same
731  * credentials as the original user on this session.
732  */
733 static int 
734 ldap_back_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
735         ber_int_t msgid, void *params )
736 {
737         struct ldapconn *lc = (struct ldapconn *)params;
738
739         /* FIXME: add checks on the URL/identity? */
740
741         return ldap_sasl_bind_s( ld, lc->lc_bound_ndn.bv_val,
742                         LDAP_SASL_SIMPLE, &lc->lc_cred, NULL, NULL, NULL );
743 }
744
745 int
746 ldap_back_op_result(
747                 struct ldapconn         *lc,
748                 Operation               *op,
749                 SlapReply               *rs,
750                 ber_int_t               msgid,
751                 ldap_back_send_t        sendok )
752 {
753         char            *match = NULL;
754         LDAPMessage     *res = NULL;
755         char            *text = NULL;
756
757 #define ERR_OK(err) ((err) == LDAP_SUCCESS || (err) == LDAP_COMPARE_FALSE || (err) == LDAP_COMPARE_TRUE)
758
759         rs->sr_text = NULL;
760         rs->sr_matched = NULL;
761
762         /* if the error recorded in the reply corresponds
763          * to a successful state, get the error from the
764          * remote server response */
765         if ( ERR_OK( rs->sr_err ) ) {
766                 int             rc;
767                 struct timeval  tv = { 0, 0 };
768
769 retry:;
770                 /* if result parsing fails, note the failure reason */
771                 switch ( ldap_result( lc->lc_ld, msgid, 1, &tv, &res ) ) {
772                 case 0:
773                         tv.tv_sec = 0;
774                         tv.tv_usec = 100000;    /* 0.1 s */
775                         ldap_pvt_thread_yield();
776                         goto retry;
777
778                 case -1:
779                         ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
780                                         &rs->sr_err );
781                         break;
782
783
784                 /* otherwise get the result; if it is not
785                  * LDAP_SUCCESS, record it in the reply
786                  * structure (this includes 
787                  * LDAP_COMPARE_{TRUE|FALSE}) */
788                 default:
789                         rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
790                                         &match, &text, NULL, NULL, 1 );
791                         rs->sr_text = text;
792                         if ( rc != LDAP_SUCCESS ) {
793                                 rs->sr_err = rc;
794                         }
795                 }
796         }
797
798         /* if the error in the reply structure is not
799          * LDAP_SUCCESS, try to map it from client 
800          * to server error */
801         if ( !ERR_OK( rs->sr_err ) ) {
802                 rs->sr_err = slap_map_api2result( rs );
803
804                 /* internal ops ( op->o_conn == NULL ) 
805                  * must not reply to client */
806                 if ( op->o_conn && !op->o_do_not_cache && match ) {
807
808                         /* record the (massaged) matched
809                          * DN into the reply structure */
810                         rs->sr_matched = match;
811                 }
812         }
813         if ( op->o_conn &&
814                         ( ( sendok & LDAP_BACK_SENDOK ) 
815                           || ( ( sendok & LDAP_BACK_SENDERR ) && rs->sr_err != LDAP_SUCCESS ) ) )
816         {
817                 send_ldap_result( op, rs );
818         }
819         if ( match ) {
820                 if ( rs->sr_matched != match ) {
821                         free( (char *)rs->sr_matched );
822                 }
823                 rs->sr_matched = NULL;
824                 ldap_memfree( match );
825         }
826         if ( text ) {
827                 ldap_memfree( text );
828         }
829         rs->sr_text = NULL;
830         return( ERR_OK( rs->sr_err ) ? 0 : -1 );
831 }
832
833 /* return true if bound, false if failed */
834 int
835 ldap_back_retry( struct ldapconn *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
836 {
837         int             rc = 0;
838         struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
839         
840 retry_lock:;
841         switch ( ldap_pvt_thread_mutex_trylock( &li->conn_mutex ) ) {
842         case LDAP_PVT_THREAD_EBUSY:
843         default:
844                 ldap_pvt_thread_yield();
845                 goto retry_lock;
846
847         case 0:
848                 break;
849         }
850
851         if ( lc->lc_refcnt == 1 ) {
852                 ldap_unbind_ext_s( lc->lc_ld, NULL, NULL );
853                 lc->lc_ld = NULL;
854                 lc->lc_bound = 0;
855
856                 /* lc here must be the regular lc, reset and ready for init */
857                 rc = ldap_back_prepare_conn( &lc, op, rs, sendok );
858                 if ( rc == LDAP_SUCCESS ) {
859                         rc = ldap_back_dobind_int( lc, op, rs, sendok, 0, 0 );
860                 }
861         }
862
863         ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
864
865         return rc;
866 }
867
868 static int
869 ldap_back_proxy_authz_bind( struct ldapconn *lc, Operation *op, SlapReply *rs )
870 {
871         struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
872         struct berval   binddn = slap_empty_bv;
873         struct berval   bindcred = slap_empty_bv;
874         int             dobind = 0;
875         int             msgid;
876         int             rc;
877
878         /*
879          * FIXME: we need to let clients use proxyAuthz
880          * otherwise we cannot do symmetric pools of servers;
881          * we have to live with the fact that a user can
882          * authorize itself as any ID that is allowed
883          * by the authzTo directive of the "proxyauthzdn".
884          */
885         /*
886          * NOTE: current Proxy Authorization specification
887          * and implementation do not allow proxy authorization
888          * control to be provided with Bind requests
889          */
890         /*
891          * if no bind took place yet, but the connection is bound
892          * and the "proxyauthzdn" is set, then bind as 
893          * "proxyauthzdn" and explicitly add the proxyAuthz 
894          * control to every operation with the dn bound 
895          * to the connection as control value.
896          */
897
898         /* bind as proxyauthzdn only if no idassert mode
899          * is requested, or if the client's identity
900          * is authorized */
901         switch ( li->idassert_mode ) {
902         case LDAP_BACK_IDASSERT_LEGACY:
903                 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) && !BER_BVISEMPTY( &op->o_conn->c_ndn ) ) {
904                         if ( !BER_BVISNULL( &li->idassert_authcDN ) && !BER_BVISEMPTY( &li->idassert_authcDN ) )
905                         {
906                                 binddn = li->idassert_authcDN;
907                                 bindcred = li->idassert_passwd;
908                                 dobind = 1;
909                         }
910                 }
911                 break;
912
913         default:
914                 /* NOTE: rootdn can always idassert */
915                 if ( li->idassert_authz && !be_isroot( op ) ) {
916                         struct berval authcDN;
917
918                         if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
919                                 authcDN = slap_empty_bv;
920
921                         } else {
922                                 authcDN = op->o_conn->c_ndn;
923                         }       
924                         rs->sr_err = slap_sasl_matches( op, li->idassert_authz,
925                                         &authcDN, &authcDN );
926                         if ( rs->sr_err != LDAP_SUCCESS ) {
927                                 if ( li->idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
928                                         send_ldap_result( op, rs );
929                                         lc->lc_bound = 0;
930
931                                 } else {
932                                         rs->sr_err = LDAP_SUCCESS;
933                                         binddn = slap_empty_bv;
934                                         bindcred = slap_empty_bv;
935                                         break;
936                                 }
937
938                                 goto done;
939                         }
940                 }
941
942                 binddn = li->idassert_authcDN;
943                 bindcred = li->idassert_passwd;
944                 dobind = 1;
945                 break;
946         }
947
948         if ( dobind && li->idassert_authmethod == LDAP_AUTH_SASL ) {
949 #ifdef HAVE_CYRUS_SASL
950                 void            *defaults = NULL;
951                 struct berval   authzID = BER_BVNULL;
952                 int             freeauthz = 0;
953
954                 /* if SASL supports native authz, prepare for it */
955                 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
956                                 ( li->idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
957                 {
958                         switch ( li->idassert_mode ) {
959                         case LDAP_BACK_IDASSERT_OTHERID:
960                         case LDAP_BACK_IDASSERT_OTHERDN:
961                                 authzID = li->idassert_authzID;
962                                 break;
963
964                         case LDAP_BACK_IDASSERT_ANONYMOUS:
965                                 BER_BVSTR( &authzID, "dn:" );
966                                 break;
967
968                         case LDAP_BACK_IDASSERT_SELF:
969                                 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
970                                         /* connection is not authc'd, so don't idassert */
971                                         BER_BVSTR( &authzID, "dn:" );
972                                         break;
973                                 }
974                                 authzID.bv_len = STRLENOF( "dn:" ) + op->o_conn->c_ndn.bv_len;
975                                 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
976                                 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
977                                 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
978                                                 op->o_conn->c_ndn.bv_val, op->o_conn->c_ndn.bv_len + 1 );
979                                 freeauthz = 1;
980                                 break;
981
982                         default:
983                                 break;
984                         }
985                 }
986
987 #if 0   /* will deal with this later... */
988                 if ( sasl_secprops != NULL ) {
989                         rs->sr_err = ldap_set_option( lc->lc_ld, LDAP_OPT_X_SASL_SECPROPS,
990                                 (void *) sasl_secprops );
991
992                         if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
993                                 send_ldap_result( op, rs );
994                                 lc->lc_bound = 0;
995                                 goto done;
996                         }
997                 }
998 #endif
999
1000                 defaults = lutil_sasl_defaults( lc->lc_ld,
1001                                 li->idassert_sasl_mech.bv_val,
1002                                 li->idassert_sasl_realm.bv_val,
1003                                 li->idassert_authcID.bv_val,
1004                                 li->idassert_passwd.bv_val,
1005                                 authzID.bv_val );
1006
1007                 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld, binddn.bv_val,
1008                                 li->idassert_sasl_mech.bv_val, NULL, NULL,
1009                                 LDAP_SASL_QUIET, lutil_sasl_interact,
1010                                 defaults );
1011
1012                 lutil_sasl_freedefs( defaults );
1013                 if ( freeauthz ) {
1014                         slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
1015                 }
1016
1017                 rs->sr_err = slap_map_api2result( rs );
1018                 if ( rs->sr_err != LDAP_SUCCESS ) {
1019                         lc->lc_bound = 0;
1020                         send_ldap_result( op, rs );
1021
1022                 } else {
1023                         lc->lc_bound = 1;
1024                 }
1025                 goto done;
1026 #endif /* HAVE_CYRUS_SASL */
1027         }
1028
1029         switch ( li->idassert_authmethod ) {
1030         case LDAP_AUTH_SIMPLE:
1031                 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
1032                                 binddn.bv_val, LDAP_SASL_SIMPLE,
1033                                 &bindcred, NULL, NULL, &msgid );
1034                 break;
1035
1036         case LDAP_AUTH_NONE:
1037                 lc->lc_bound = 1;
1038                 goto done;
1039
1040         default:
1041                 /* unsupported! */
1042                 lc->lc_bound = 0;
1043                 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
1044                 send_ldap_result( op, rs );
1045                 goto done;
1046         }
1047
1048         rc = ldap_back_op_result( lc, op, rs, msgid, LDAP_BACK_SENDERR );
1049         if ( rc == LDAP_SUCCESS ) {
1050                 lc->lc_bound = 1;
1051         }
1052 done:;
1053         return lc->lc_bound;
1054 }
1055
1056 /*
1057  * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
1058  * to existing server-side controls if required; if not,
1059  * the existing server-side controls are placed in *pctrls.
1060  * The caller, after using the controls in client API 
1061  * operations, if ( *pctrls != op->o_ctrls ), should
1062  * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
1063  * The function returns success if the control could
1064  * be added if required, or if it did nothing; in the future,
1065  * it might return some error if it failed.
1066  * 
1067  * if no bind took place yet, but the connection is bound
1068  * and the "proxyauthzdn" is set, then bind as "proxyauthzdn" 
1069  * and explicitly add proxyAuthz the control to every operation
1070  * with the dn bound to the connection as control value.
1071  *
1072  * If no server-side controls are defined for the operation,
1073  * simply add the proxyAuthz control; otherwise, if the
1074  * proxyAuthz control is not already set, add it as
1075  * the first one
1076  *
1077  * FIXME: is controls order significant for security?
1078  * ANSWER: controls ordering and interoperability
1079  * must be indicated by the specs of each control; if none
1080  * is specified, the order is irrelevant.
1081  */
1082 int
1083 ldap_back_proxy_authz_ctrl(
1084                 struct ldapconn *lc,
1085                 Operation       *op,
1086                 SlapReply       *rs,
1087                 LDAPControl     ***pctrls )
1088 {
1089         struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
1090         LDAPControl     **ctrls = NULL;
1091         int             i = 0,
1092                         mode;
1093         struct berval   assertedID;
1094
1095         *pctrls = NULL;
1096
1097         rs->sr_err = LDAP_SUCCESS;
1098
1099         /* FIXME: SASL/EXTERNAL over ldapi:// doesn't honor the authcID,
1100          * but if it is not set this test fails.  We need a different
1101          * means to detect if idassert is enabled */
1102         if ( ( BER_BVISNULL( &li->idassert_authcID ) || BER_BVISEMPTY( &li->idassert_authcID ) )
1103                         && ( BER_BVISNULL( &li->idassert_authcDN ) || BER_BVISEMPTY( &li->idassert_authcDN ) ) )
1104         {
1105                 goto done;
1106         }
1107
1108         if ( !op->o_conn || op->o_do_not_cache || be_isroot( op ) ) {
1109                 goto done;
1110         }
1111
1112         if ( li->idassert_mode == LDAP_BACK_IDASSERT_LEGACY ) {
1113                 if ( op->o_proxy_authz ) {
1114                         /*
1115                          * FIXME: we do not want to perform proxyAuthz
1116                          * on behalf of the client, because this would
1117                          * be performed with "proxyauthzdn" privileges.
1118                          *
1119                          * This might actually be too strict, since
1120                          * the "proxyauthzdn" authzTo, and each entry's
1121                          * authzFrom attributes may be crafted
1122                          * to avoid unwanted proxyAuthz to take place.
1123                          */
1124 #if 0
1125                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1126                         rs->sr_text = "proxyAuthz not allowed within namingContext";
1127 #endif
1128                         goto done;
1129                 }
1130
1131                 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
1132                         goto done;
1133                 }
1134
1135                 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1136                         goto done;
1137                 }
1138
1139                 if ( BER_BVISNULL( &li->idassert_authcDN ) ) {
1140                         goto done;
1141                 }
1142
1143         } else if ( li->idassert_authmethod == LDAP_AUTH_SASL ) {
1144                 if ( ( li->idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ )
1145                                 /* && ( !BER_BVISNULL( &op->o_conn->c_ndn ) || lc->lc_bound ) */ )
1146                 {
1147                         /* already asserted in SASL via native authz */
1148                         /* NOTE: the test on lc->lc_bound is used to trap
1149                          * native authorization of anonymous users,
1150                          * since in that case op->o_conn->c_ndn is NULL */
1151                         goto done;
1152                 }
1153
1154         } else if ( li->idassert_authz && !be_isroot( op ) ) {
1155                 int             rc;
1156                 struct berval authcDN;
1157
1158                 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1159                         authcDN = slap_empty_bv;
1160                 } else {
1161                         authcDN = op->o_conn->c_ndn;
1162                 }
1163                 rc = slap_sasl_matches( op, li->idassert_authz,
1164                                 &authcDN, & authcDN );
1165                 if ( rc != LDAP_SUCCESS ) {
1166                         if ( li->idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE )
1167                         {
1168                                 /* op->o_conn->c_ndn is not authorized
1169                                  * to use idassert */
1170                                 return rc;
1171                         }
1172                         return rs->sr_err;
1173                 }
1174         }
1175
1176         if ( op->o_proxy_authz ) {
1177                 /*
1178                  * FIXME: we can:
1179                  * 1) ignore the already set proxyAuthz control
1180                  * 2) leave it in place, and don't set ours
1181                  * 3) add both
1182                  * 4) reject the operation
1183                  *
1184                  * option (4) is very drastic
1185                  * option (3) will make the remote server reject
1186                  * the operation, thus being equivalent to (4)
1187                  * option (2) will likely break the idassert
1188                  * assumptions, so we cannot accept it;
1189                  * option (1) means that we are contradicting
1190                  * the client's reques.
1191                  *
1192                  * I think (4) is the only correct choice.
1193                  */
1194                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1195                 rs->sr_text = "proxyAuthz not allowed within namingContext";
1196         }
1197
1198         if ( op->o_do_not_cache && op->o_is_auth_check ) {
1199                 mode = LDAP_BACK_IDASSERT_NOASSERT;
1200
1201         } else {
1202                 mode = li->idassert_mode;
1203         }
1204
1205         switch ( mode ) {
1206         case LDAP_BACK_IDASSERT_LEGACY:
1207         case LDAP_BACK_IDASSERT_SELF:
1208                 /* original behavior:
1209                  * assert the client's identity */
1210                 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1211                         assertedID = slap_empty_bv;
1212                 } else {
1213                         assertedID = op->o_conn->c_ndn;
1214                 }
1215                 break;
1216
1217         case LDAP_BACK_IDASSERT_ANONYMOUS:
1218                 /* assert "anonymous" */
1219                 assertedID = slap_empty_bv;
1220                 break;
1221
1222         case LDAP_BACK_IDASSERT_NOASSERT:
1223                 /* don't assert; bind as proxyauthzdn */
1224                 goto done;
1225
1226         case LDAP_BACK_IDASSERT_OTHERID:
1227         case LDAP_BACK_IDASSERT_OTHERDN:
1228                 /* assert idassert DN */
1229                 assertedID = li->idassert_authzID;
1230                 break;
1231
1232         default:
1233                 assert( 0 );
1234         }
1235
1236         if ( BER_BVISNULL( &assertedID ) ) {
1237                 assertedID = slap_empty_bv;
1238         }
1239
1240         if ( op->o_ctrls ) {
1241                 for ( i = 0; op->o_ctrls[ i ]; i++ )
1242                         /* just count ctrls */ ;
1243         }
1244
1245         ctrls = ch_malloc( sizeof( LDAPControl * ) * (i + 2) );
1246         ctrls[ 0 ] = ch_malloc( sizeof( LDAPControl ) );
1247         
1248         ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1249         ctrls[ 0 ]->ldctl_iscritical = 1;
1250
1251         switch ( li->idassert_mode ) {
1252         /* already in u:ID or dn:DN form */
1253         case LDAP_BACK_IDASSERT_OTHERID:
1254         case LDAP_BACK_IDASSERT_OTHERDN:
1255                 ber_dupbv( &ctrls[ 0 ]->ldctl_value, &assertedID );
1256                 break;
1257
1258         /* needs the dn: prefix */
1259         default:
1260                 ctrls[ 0 ]->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
1261                 ctrls[ 0 ]->ldctl_value.bv_val = ch_malloc( ctrls[ 0 ]->ldctl_value.bv_len + 1 );
1262                 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
1263                 AC_MEMCPY( &ctrls[ 0 ]->ldctl_value.bv_val[ STRLENOF( "dn:" ) ],
1264                                 assertedID.bv_val, assertedID.bv_len + 1 );
1265                 break;
1266         }
1267
1268         if ( op->o_ctrls ) {
1269                 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
1270                         ctrls[ i + 1 ] = op->o_ctrls[ i ];
1271                 }
1272         }
1273         ctrls[ i + 1 ] = NULL;
1274
1275 done:;
1276         if ( ctrls == NULL ) {
1277                 ctrls = op->o_ctrls;
1278         }
1279
1280         *pctrls = ctrls;
1281         
1282         return rs->sr_err;
1283 }
1284
1285 int
1286 ldap_back_proxy_authz_ctrl_free( Operation *op, LDAPControl ***pctrls )
1287 {
1288         LDAPControl     **ctrls = *pctrls;
1289
1290         /* we assume that the first control is the proxyAuthz
1291          * added by back-ldap, so it's the only one we explicitly 
1292          * free */
1293         if ( ctrls && ctrls != op->o_ctrls ) {
1294                 assert( ctrls[ 0 ] != NULL );
1295
1296                 if ( !BER_BVISNULL( &ctrls[ 0 ]->ldctl_value ) ) {
1297                         free( ctrls[ 0 ]->ldctl_value.bv_val );
1298                 }
1299
1300                 free( ctrls[ 0 ] );
1301                 free( ctrls );
1302         } 
1303
1304         *pctrls = NULL;
1305
1306         return 0;
1307 }