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