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