]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/bind.c
668143f682c73e2682374faf3cb845c42d65314b
[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, 0, 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 0
507                         /* FIXME: if we set lc_bound_ndn = o_ndn
508                          * we end up with a bind with DN but no password! */
509                         if ( !BER_BVISEMPTY( &op->o_ndn )
510                                 && SLAP_IS_AUTHZ_BACKEND( op ) )
511                         {
512                                 ber_dupbv( &lc->lc_bound_ndn, &op->o_ndn );
513                         }
514 #endif
515                 }
516
517 #ifdef HAVE_TLS
518                 /* if start TLS failed but it was not mandatory,
519                  * check if the non-TLS connection was already
520                  * in cache; in case, destroy the newly created
521                  * connection and use the existing one */
522                 if ( lc->lc_conn == LDAP_BACK_PCONN_TLS
523                                 && !ldap_tls_inplace( lc->lc_ld ) )
524                 {
525                         struct ldapconn *tmplc;
526                         
527                         lc_curr.lc_conn = LDAP_BACK_PCONN;
528                         ldap_pvt_thread_mutex_lock( &li->conn_mutex );
529                         tmplc = (struct ldapconn *)avl_find( li->conntree, 
530                                         (caddr_t)&lc_curr, ldap_back_conn_cmp );
531                         if ( tmplc != NULL ) {
532                                 refcnt = ++tmplc->lc_refcnt;
533                                 ldap_back_conn_free( lc );
534                                 lc = tmplc;
535                         }
536                         ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
537
538                         if ( tmplc != NULL ) {
539                                 goto done;
540                         }
541                 }
542 #endif /* HAVE_TLS */
543
544                 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
545
546                 /* Inserts the newly created ldapconn in the avl tree */
547                 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
548
549                 assert( lc->lc_refcnt == 1 );
550                 rs->sr_err = avl_insert( &li->conntree, (caddr_t)lc,
551                         ldap_back_conn_cmp, ldap_back_conn_dup );
552
553 #if PRINT_CONNTREE > 0
554                 myprint( li->conntree );
555 #endif /* PRINT_CONNTREE */
556         
557                 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
558
559                 Debug( LDAP_DEBUG_TRACE,
560                         "=>ldap_back_getconn: conn %p inserted (refcnt=%u)\n",
561                         (void *)lc, refcnt, 0 );
562         
563                 /* Err could be -1 in case a duplicate ldapconn is inserted */
564                 if ( rs->sr_err != 0 ) {
565                         ldap_back_conn_free( lc );
566                         rs->sr_err = LDAP_OTHER;
567                         if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
568                                 send_ldap_error( op, rs, LDAP_OTHER,
569                                         "internal server error" );
570                         }
571                         return NULL;
572                 }
573
574         } else {
575                 Debug( LDAP_DEBUG_TRACE,
576                         "=>ldap_back_getconn: conn %p fetched (refcnt=%u)\n",
577                         (void *)lc, refcnt, 0 );
578         }
579
580 done:;
581         return lc;
582 }
583
584 void
585 ldap_back_release_conn(
586         Operation               *op,
587         SlapReply               *rs,
588         struct ldapconn         *lc )
589 {
590         struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
591
592         ldap_pvt_thread_mutex_lock( &li->conn_mutex );
593         assert( lc->lc_refcnt > 0 );
594         lc->lc_refcnt--;
595         ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
596 }
597
598 /*
599  * ldap_back_dobind
600  *
601  * Note: as the check for the value of lc->lc_bound was already here, I removed
602  * it from all the callers, and I made the function return the flag, so
603  * it can be used to simplify the check.
604  *
605  * Note: dolock indicates whether li->conn_mutex must be locked or not
606  */
607 static int
608 ldap_back_dobind_int(
609         struct ldapconn         *lc,
610         Operation               *op,
611         SlapReply               *rs,
612         ldap_back_send_t        sendok,
613         int                     retries,
614         int                     dolock )
615 {       
616         struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
617
618         int             rc = LDAP_BACK_CONN_ISBOUND( lc );
619         ber_int_t       msgid;
620
621         assert( retries >= 0 );
622
623         if ( rc ) {
624                 return rc;
625         }
626
627         /*
628          * FIXME: we need to let clients use proxyAuthz
629          * otherwise we cannot do symmetric pools of servers;
630          * we have to live with the fact that a user can
631          * authorize itself as any ID that is allowed
632          * by the authzTo directive of the "proxyauthzdn".
633          */
634         /*
635          * NOTE: current Proxy Authorization specification
636          * and implementation do not allow proxy authorization
637          * control to be provided with Bind requests
638          */
639         /*
640          * if no bind took place yet, but the connection is bound
641          * and the "idassert-authcDN" (or other ID) is set, 
642          * then bind as the asserting identity and explicitly 
643          * add the proxyAuthz control to every operation with the
644          * dn bound to the connection as control value.
645          * This is done also if this is the authrizing backend,
646          * but the "override" flag is given to idassert.
647          * It allows to use SASL bind and yet proxyAuthz users
648          */
649         if ( op->o_conn != NULL &&
650                         !op->o_do_not_cache &&
651                         ( BER_BVISNULL( &lc->lc_bound_ndn ) ||
652                           ( li->idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) )
653         {
654                 (void)ldap_back_proxy_authz_bind( lc, op, rs );
655                 goto done;
656         }
657
658 #ifdef HAVE_CYRUS_SASL
659         if ( LDAP_BACK_CONN_ISPRIV( lc )
660                 && li->acl_authmethod == LDAP_AUTH_SASL )
661         {
662                 void            *defaults = NULL;
663
664                 if ( li->acl_secprops != NULL ) {
665                         rc = ldap_set_option( lc->lc_ld,
666                                 LDAP_OPT_X_SASL_SECPROPS, li->acl_secprops);
667
668                         if ( rc != LDAP_OPT_SUCCESS ) {
669                                 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
670                                         "(%s,SECPROPS,\"%s\") failed!\n",
671                                         li->url, li->acl_secprops, 0 );
672                                 goto done;
673                         }
674                 }
675
676                 defaults = lutil_sasl_defaults( lc->lc_ld,
677                                 li->acl_sasl_mech.bv_val,
678                                 li->acl_sasl_realm.bv_val,
679                                 li->acl_authcID.bv_val,
680                                 li->acl_passwd.bv_val,
681                                 NULL );
682
683                 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld,
684                                 li->acl_authcDN.bv_val,
685                                 li->acl_sasl_mech.bv_val, NULL, NULL,
686                                 LDAP_SASL_QUIET, lutil_sasl_interact,
687                                 defaults );
688
689                 lutil_sasl_freedefs( defaults );
690
691                 rs->sr_err = slap_map_api2result( rs );
692                 if ( rs->sr_err != LDAP_SUCCESS ) {
693                         LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
694                         send_ldap_result( op, rs );
695
696                 } else {
697                         LDAP_BACK_CONN_ISBOUND_SET( lc );
698                 }
699                 goto done;
700         }
701 #endif /* HAVE_CYRUS_SASL */
702
703 retry:;
704         rs->sr_err = ldap_sasl_bind( lc->lc_ld,
705                         lc->lc_bound_ndn.bv_val,
706                         LDAP_SASL_SIMPLE, &lc->lc_cred,
707                         NULL, NULL, &msgid );
708
709         if ( rs->sr_err == LDAP_SERVER_DOWN ) {
710                 if ( retries != LDAP_BACK_RETRY_NEVER ) {
711                         if ( dolock ) {
712                                 ldap_pvt_thread_mutex_lock( &li->conn_mutex );
713                         }
714
715                         assert( lc->lc_refcnt > 0 );
716                         if ( lc->lc_refcnt == 1 ) {
717                                 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
718                                 lc->lc_ld = NULL;
719
720                                 /* lc here must be the regular lc, reset and ready for init */
721                                 rs->sr_err = ldap_back_prepare_conn( &lc, op, rs, sendok );
722                         }
723                         if ( dolock ) {
724                                 ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
725                         }
726                         if ( rs->sr_err == LDAP_SUCCESS ) {
727                                 if ( retries > 0 ) {
728                                         retries--;
729                                 }
730                                 goto retry;
731                         }
732                 }
733
734                 ldap_back_freeconn( op, lc );
735                 rs->sr_err = slap_map_api2result( rs );
736
737                 return 0;
738         }
739
740         rc = ldap_back_op_result( lc, op, rs, msgid, 0, sendok );
741         if ( rc == LDAP_SUCCESS ) {
742                 LDAP_BACK_CONN_ISBOUND_SET( lc );
743
744         } else {
745                 ldap_back_release_conn( op, rs, lc );
746         }
747
748 done:;
749         rc = LDAP_BACK_CONN_ISBOUND( lc );
750
751         return rc;
752 }
753
754 int
755 ldap_back_dobind( struct ldapconn *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
756 {
757         struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
758
759         return ldap_back_dobind_int( lc, op, rs, sendok, li->nretries, 1 );
760 }
761
762 /*
763  * ldap_back_default_rebind
764  *
765  * This is a callback used for chasing referrals using the same
766  * credentials as the original user on this session.
767  */
768 static int 
769 ldap_back_default_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
770         ber_int_t msgid, void *params )
771 {
772         struct ldapconn *lc = (struct ldapconn *)params;
773
774 #ifdef HAVE_TLS
775         /* ... otherwise we couldn't get here */
776         assert( lc != NULL );
777
778         if ( !ldap_tls_inplace( ld ) ) {
779                 int             is_tls = LDAP_BACK_CONN_ISTLS( lc ),
780                                 rc;
781                 const char      *text = NULL;
782
783                 rc = ldap_back_start_tls( ld, 0, &is_tls, url, lc->lc_flags,
784                         LDAP_BACK_RETRY_DEFAULT, &text );
785                 if ( rc != LDAP_SUCCESS ) {
786                         return rc;
787                 }
788         }
789 #endif /* HAVE_TLS */
790
791         /* FIXME: add checks on the URL/identity? */
792
793         return ldap_sasl_bind_s( ld, lc->lc_bound_ndn.bv_val,
794                         LDAP_SASL_SIMPLE, &lc->lc_cred, NULL, NULL, NULL );
795 }
796
797 int
798 ldap_back_op_result(
799                 struct ldapconn         *lc,
800                 Operation               *op,
801                 SlapReply               *rs,
802                 ber_int_t               msgid,
803                 time_t                  timeout,
804                 ldap_back_send_t        sendok )
805 {
806         char            *match = NULL;
807         LDAPMessage     *res = NULL;
808         char            *text = NULL;
809
810 #define ERR_OK(err) ((err) == LDAP_SUCCESS || (err) == LDAP_COMPARE_FALSE || (err) == LDAP_COMPARE_TRUE)
811
812         rs->sr_text = NULL;
813         rs->sr_matched = NULL;
814
815         /* if the error recorded in the reply corresponds
816          * to a successful state, get the error from the
817          * remote server response */
818         if ( ERR_OK( rs->sr_err ) ) {
819                 int             rc;
820                 struct timeval  tv;
821
822                 if ( timeout ) {
823                         tv.tv_sec = timeout;
824                         tv.tv_usec = 0;
825
826                 } else {
827                         LDAP_BACK_TV_SET( &tv );
828                 }
829
830 retry:;
831                 /* if result parsing fails, note the failure reason */
832                 rc = ldap_result( lc->lc_ld, msgid, 1, &tv, &res );
833                 switch ( rc ) {
834                 case 0:
835                         if ( timeout ) {
836                                 (void)ldap_abandon_ext( lc->lc_ld, msgid, NULL, NULL );
837                                 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
838                                 rs->sr_text = "Operation timed out";
839                                 break;
840                         }
841
842                         LDAP_BACK_TV_SET( &tv );
843                         ldap_pvt_thread_yield();
844                         goto retry;
845
846                 case -1:
847                         ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
848                                         &rs->sr_err );
849                         break;
850
851
852                 /* otherwise get the result; if it is not
853                  * LDAP_SUCCESS, record it in the reply
854                  * structure (this includes 
855                  * LDAP_COMPARE_{TRUE|FALSE}) */
856                 default:
857                         rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
858                                         &match, &text, NULL, NULL, 1 );
859                         rs->sr_text = text;
860                         if ( rc != LDAP_SUCCESS ) {
861                                 rs->sr_err = rc;
862                         }
863                 }
864         }
865
866         /* if the error in the reply structure is not
867          * LDAP_SUCCESS, try to map it from client 
868          * to server error */
869         if ( !ERR_OK( rs->sr_err ) ) {
870                 rs->sr_err = slap_map_api2result( rs );
871
872                 /* internal ops ( op->o_conn == NULL ) 
873                  * must not reply to client */
874                 if ( op->o_conn && !op->o_do_not_cache && match ) {
875
876                         /* record the (massaged) matched
877                          * DN into the reply structure */
878                         rs->sr_matched = match;
879                 }
880         }
881         if ( op->o_conn &&
882                         ( ( sendok & LDAP_BACK_SENDOK ) 
883                           || ( ( sendok & LDAP_BACK_SENDERR ) && rs->sr_err != LDAP_SUCCESS ) ) )
884         {
885                 send_ldap_result( op, rs );
886         }
887         if ( match ) {
888                 if ( rs->sr_matched != match ) {
889                         free( (char *)rs->sr_matched );
890                 }
891                 rs->sr_matched = NULL;
892                 ldap_memfree( match );
893         }
894         if ( text ) {
895                 ldap_memfree( text );
896         }
897         rs->sr_text = NULL;
898         return( ERR_OK( rs->sr_err ) ? LDAP_SUCCESS : rs->sr_err );
899 }
900
901 /* return true if bound, false if failed */
902 int
903 ldap_back_retry( struct ldapconn *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
904 {
905         int             rc = 0;
906         struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
907         
908         ldap_pvt_thread_mutex_lock( &li->conn_mutex );
909
910         if ( lc->lc_refcnt == 1 ) {
911                 Debug( LDAP_DEBUG_ANY,
912                         "%s ldap_back_retry: retrying URI=\"%s\" DN=\"%s\"\n",
913                         op->o_log_prefix, li->url,
914                         BER_BVISNULL( &lc->lc_bound_ndn ) ?
915                                 "" : lc->lc_bound_ndn.bv_val );
916
917                 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
918                 lc->lc_ld = NULL;
919                 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
920
921                 /* lc here must be the regular lc, reset and ready for init */
922                 rc = ldap_back_prepare_conn( &lc, op, rs, sendok );
923                 if ( rc == LDAP_SUCCESS ) {
924                         rc = ldap_back_dobind_int( lc, op, rs, sendok, 0, 0 );
925                 }
926         }
927
928         ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
929
930         return rc;
931 }
932
933 static int
934 ldap_back_proxy_authz_bind( struct ldapconn *lc, Operation *op, SlapReply *rs )
935 {
936         struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
937         struct berval   binddn = slap_empty_bv;
938         struct berval   bindcred = slap_empty_bv;
939         int             dobind = 0;
940         int             msgid;
941         int             rc;
942
943         /*
944          * FIXME: we need to let clients use proxyAuthz
945          * otherwise we cannot do symmetric pools of servers;
946          * we have to live with the fact that a user can
947          * authorize itself as any ID that is allowed
948          * by the authzTo directive of the "proxyauthzdn".
949          */
950         /*
951          * NOTE: current Proxy Authorization specification
952          * and implementation do not allow proxy authorization
953          * control to be provided with Bind requests
954          */
955         /*
956          * if no bind took place yet, but the connection is bound
957          * and the "proxyauthzdn" is set, then bind as 
958          * "proxyauthzdn" and explicitly add the proxyAuthz 
959          * control to every operation with the dn bound 
960          * to the connection as control value.
961          */
962
963         /* bind as proxyauthzdn only if no idassert mode
964          * is requested, or if the client's identity
965          * is authorized */
966         switch ( li->idassert_mode ) {
967         case LDAP_BACK_IDASSERT_LEGACY:
968                 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) && !BER_BVISEMPTY( &op->o_conn->c_ndn ) ) {
969                         if ( !BER_BVISNULL( &li->idassert_authcDN ) && !BER_BVISEMPTY( &li->idassert_authcDN ) )
970                         {
971                                 binddn = li->idassert_authcDN;
972                                 bindcred = li->idassert_passwd;
973                                 dobind = 1;
974                         }
975                 }
976                 break;
977
978         default:
979                 /* NOTE: rootdn can always idassert */
980                 if ( li->idassert_authz && !be_isroot( op ) ) {
981                         struct berval authcDN;
982
983                         if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
984                                 authcDN = slap_empty_bv;
985
986                         } else {
987                                 authcDN = op->o_conn->c_ndn;
988                         }       
989                         rs->sr_err = slap_sasl_matches( op, li->idassert_authz,
990                                         &authcDN, &authcDN );
991                         if ( rs->sr_err != LDAP_SUCCESS ) {
992                                 if ( li->idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
993                                         send_ldap_result( op, rs );
994                                         LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
995
996                                 } else {
997                                         rs->sr_err = LDAP_SUCCESS;
998                                         binddn = slap_empty_bv;
999                                         bindcred = slap_empty_bv;
1000                                         break;
1001                                 }
1002
1003                                 goto done;
1004                         }
1005                 }
1006
1007                 binddn = li->idassert_authcDN;
1008                 bindcred = li->idassert_passwd;
1009                 dobind = 1;
1010                 break;
1011         }
1012
1013         if ( dobind && li->idassert_authmethod == LDAP_AUTH_SASL ) {
1014 #ifdef HAVE_CYRUS_SASL
1015                 void            *defaults = NULL;
1016                 struct berval   authzID = BER_BVNULL;
1017                 int             freeauthz = 0;
1018
1019                 /* if SASL supports native authz, prepare for it */
1020                 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
1021                                 ( li->idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
1022                 {
1023                         switch ( li->idassert_mode ) {
1024                         case LDAP_BACK_IDASSERT_OTHERID:
1025                         case LDAP_BACK_IDASSERT_OTHERDN:
1026                                 authzID = li->idassert_authzID;
1027                                 break;
1028
1029                         case LDAP_BACK_IDASSERT_ANONYMOUS:
1030                                 BER_BVSTR( &authzID, "dn:" );
1031                                 break;
1032
1033                         case LDAP_BACK_IDASSERT_SELF:
1034                                 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1035                                         /* connection is not authc'd, so don't idassert */
1036                                         BER_BVSTR( &authzID, "dn:" );
1037                                         break;
1038                                 }
1039                                 authzID.bv_len = STRLENOF( "dn:" ) + op->o_conn->c_ndn.bv_len;
1040                                 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
1041                                 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
1042                                 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
1043                                                 op->o_conn->c_ndn.bv_val, op->o_conn->c_ndn.bv_len + 1 );
1044                                 freeauthz = 1;
1045                                 break;
1046
1047                         default:
1048                                 break;
1049                         }
1050                 }
1051
1052                 if ( li->idassert_secprops != NULL ) {
1053                         rs->sr_err = ldap_set_option( lc->lc_ld,
1054                                 LDAP_OPT_X_SASL_SECPROPS,
1055                                 (void *)li->idassert_secprops );
1056
1057                         if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
1058                                 send_ldap_result( op, rs );
1059                                 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1060                                 goto done;
1061                         }
1062                 }
1063
1064                 defaults = lutil_sasl_defaults( lc->lc_ld,
1065                                 li->idassert_sasl_mech.bv_val,
1066                                 li->idassert_sasl_realm.bv_val,
1067                                 li->idassert_authcID.bv_val,
1068                                 li->idassert_passwd.bv_val,
1069                                 authzID.bv_val );
1070
1071                 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld, binddn.bv_val,
1072                                 li->idassert_sasl_mech.bv_val, NULL, NULL,
1073                                 LDAP_SASL_QUIET, lutil_sasl_interact,
1074                                 defaults );
1075
1076                 rs->sr_err = slap_map_api2result( rs );
1077                 if ( rs->sr_err != LDAP_SUCCESS ) {
1078                         LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1079                         send_ldap_result( op, rs );
1080
1081                 } else {
1082                         LDAP_BACK_CONN_ISBOUND_SET( lc );
1083                 }
1084
1085                 lutil_sasl_freedefs( defaults );
1086                 if ( freeauthz ) {
1087                         slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
1088                 }
1089
1090                 goto done;
1091 #endif /* HAVE_CYRUS_SASL */
1092         }
1093
1094         switch ( li->idassert_authmethod ) {
1095         case LDAP_AUTH_NONE:
1096                 LDAP_BACK_CONN_ISBOUND_SET( lc );
1097                 goto done;
1098
1099         case LDAP_AUTH_SIMPLE:
1100                 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
1101                                 binddn.bv_val, LDAP_SASL_SIMPLE,
1102                                 &bindcred, NULL, NULL, &msgid );
1103                 break;
1104
1105         default:
1106                 /* unsupported! */
1107                 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1108                 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
1109                 send_ldap_result( op, rs );
1110                 goto done;
1111         }
1112
1113         rc = ldap_back_op_result( lc, op, rs, msgid, 0, LDAP_BACK_SENDERR );
1114         if ( rc == LDAP_SUCCESS ) {
1115                 LDAP_BACK_CONN_ISBOUND_SET( lc );
1116         }
1117 done:;
1118         return LDAP_BACK_CONN_ISBOUND( lc );
1119 }
1120
1121 /*
1122  * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
1123  * to existing server-side controls if required; if not,
1124  * the existing server-side controls are placed in *pctrls.
1125  * The caller, after using the controls in client API 
1126  * operations, if ( *pctrls != op->o_ctrls ), should
1127  * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
1128  * The function returns success if the control could
1129  * be added if required, or if it did nothing; in the future,
1130  * it might return some error if it failed.
1131  * 
1132  * if no bind took place yet, but the connection is bound
1133  * and the "proxyauthzdn" is set, then bind as "proxyauthzdn" 
1134  * and explicitly add proxyAuthz the control to every operation
1135  * with the dn bound to the connection as control value.
1136  *
1137  * If no server-side controls are defined for the operation,
1138  * simply add the proxyAuthz control; otherwise, if the
1139  * proxyAuthz control is not already set, add it as
1140  * the first one
1141  *
1142  * FIXME: is controls order significant for security?
1143  * ANSWER: controls ordering and interoperability
1144  * must be indicated by the specs of each control; if none
1145  * is specified, the order is irrelevant.
1146  */
1147 int
1148 ldap_back_proxy_authz_ctrl(
1149                 struct ldapconn *lc,
1150                 Operation       *op,
1151                 SlapReply       *rs,
1152                 LDAPControl     ***pctrls )
1153 {
1154         struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
1155         LDAPControl     **ctrls = NULL;
1156         int             i = 0,
1157                         mode;
1158         struct berval   assertedID;
1159
1160         *pctrls = NULL;
1161
1162         rs->sr_err = LDAP_SUCCESS;
1163
1164         /* FIXME: SASL/EXTERNAL over ldapi:// doesn't honor the authcID,
1165          * but if it is not set this test fails.  We need a different
1166          * means to detect if idassert is enabled */
1167         if ( ( BER_BVISNULL( &li->idassert_authcID ) || BER_BVISEMPTY( &li->idassert_authcID ) )
1168                         && ( BER_BVISNULL( &li->idassert_authcDN ) || BER_BVISEMPTY( &li->idassert_authcDN ) ) )
1169         {
1170                 goto done;
1171         }
1172
1173         if ( !op->o_conn || op->o_do_not_cache || be_isroot( op ) ) {
1174                 goto done;
1175         }
1176
1177         if ( li->idassert_mode == LDAP_BACK_IDASSERT_LEGACY ) {
1178                 if ( op->o_proxy_authz ) {
1179                         /*
1180                          * FIXME: we do not want to perform proxyAuthz
1181                          * on behalf of the client, because this would
1182                          * be performed with "proxyauthzdn" privileges.
1183                          *
1184                          * This might actually be too strict, since
1185                          * the "proxyauthzdn" authzTo, and each entry's
1186                          * authzFrom attributes may be crafted
1187                          * to avoid unwanted proxyAuthz to take place.
1188                          */
1189 #if 0
1190                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1191                         rs->sr_text = "proxyAuthz not allowed within namingContext";
1192 #endif
1193                         goto done;
1194                 }
1195
1196                 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
1197                         goto done;
1198                 }
1199
1200                 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1201                         goto done;
1202                 }
1203
1204                 if ( BER_BVISNULL( &li->idassert_authcDN ) ) {
1205                         goto done;
1206                 }
1207
1208         } else if ( li->idassert_authmethod == LDAP_AUTH_SASL ) {
1209                 if ( ( li->idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ )
1210                                 /* && ( !BER_BVISNULL( &op->o_conn->c_ndn )
1211                                         || LDAP_BACK_CONN_ISBOUND( lc ) ) */ )
1212                 {
1213                         /* already asserted in SASL via native authz */
1214                         /* NOTE: the test on lc->lc_bound is used to trap
1215                          * native authorization of anonymous users,
1216                          * since in that case op->o_conn->c_ndn is NULL */
1217                         goto done;
1218                 }
1219
1220         } else if ( li->idassert_authz && !be_isroot( op ) ) {
1221                 int             rc;
1222                 struct berval authcDN;
1223
1224                 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1225                         authcDN = slap_empty_bv;
1226                 } else {
1227                         authcDN = op->o_conn->c_ndn;
1228                 }
1229                 rc = slap_sasl_matches( op, li->idassert_authz,
1230                                 &authcDN, & authcDN );
1231                 if ( rc != LDAP_SUCCESS ) {
1232                         if ( li->idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE )
1233                         {
1234                                 /* op->o_conn->c_ndn is not authorized
1235                                  * to use idassert */
1236                                 return rc;
1237                         }
1238                         return rs->sr_err;
1239                 }
1240         }
1241
1242         if ( op->o_proxy_authz ) {
1243                 /*
1244                  * FIXME: we can:
1245                  * 1) ignore the already set proxyAuthz control
1246                  * 2) leave it in place, and don't set ours
1247                  * 3) add both
1248                  * 4) reject the operation
1249                  *
1250                  * option (4) is very drastic
1251                  * option (3) will make the remote server reject
1252                  * the operation, thus being equivalent to (4)
1253                  * option (2) will likely break the idassert
1254                  * assumptions, so we cannot accept it;
1255                  * option (1) means that we are contradicting
1256                  * the client's reques.
1257                  *
1258                  * I think (4) is the only correct choice.
1259                  */
1260                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1261                 rs->sr_text = "proxyAuthz not allowed within namingContext";
1262         }
1263
1264         if ( op->o_do_not_cache && op->o_is_auth_check ) {
1265                 mode = LDAP_BACK_IDASSERT_NOASSERT;
1266
1267         } else {
1268                 mode = li->idassert_mode;
1269         }
1270
1271         switch ( mode ) {
1272         case LDAP_BACK_IDASSERT_LEGACY:
1273         case LDAP_BACK_IDASSERT_SELF:
1274                 /* original behavior:
1275                  * assert the client's identity */
1276                 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1277                         assertedID = slap_empty_bv;
1278                 } else {
1279                         assertedID = op->o_conn->c_ndn;
1280                 }
1281                 break;
1282
1283         case LDAP_BACK_IDASSERT_ANONYMOUS:
1284                 /* assert "anonymous" */
1285                 assertedID = slap_empty_bv;
1286                 break;
1287
1288         case LDAP_BACK_IDASSERT_NOASSERT:
1289                 /* don't assert; bind as proxyauthzdn */
1290                 goto done;
1291
1292         case LDAP_BACK_IDASSERT_OTHERID:
1293         case LDAP_BACK_IDASSERT_OTHERDN:
1294                 /* assert idassert DN */
1295                 assertedID = li->idassert_authzID;
1296                 break;
1297
1298         default:
1299                 assert( 0 );
1300         }
1301
1302         if ( BER_BVISNULL( &assertedID ) ) {
1303                 assertedID = slap_empty_bv;
1304         }
1305
1306         if ( op->o_ctrls ) {
1307                 for ( i = 0; op->o_ctrls[ i ]; i++ )
1308                         /* just count ctrls */ ;
1309         }
1310
1311         ctrls = ch_malloc( sizeof( LDAPControl * ) * (i + 2) );
1312         ctrls[ 0 ] = ch_malloc( sizeof( LDAPControl ) );
1313         
1314         ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1315         ctrls[ 0 ]->ldctl_iscritical = 1;
1316
1317         switch ( li->idassert_mode ) {
1318         /* already in u:ID or dn:DN form */
1319         case LDAP_BACK_IDASSERT_OTHERID:
1320         case LDAP_BACK_IDASSERT_OTHERDN:
1321                 ber_dupbv( &ctrls[ 0 ]->ldctl_value, &assertedID );
1322                 break;
1323
1324         /* needs the dn: prefix */
1325         default:
1326                 ctrls[ 0 ]->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
1327                 ctrls[ 0 ]->ldctl_value.bv_val = ch_malloc( ctrls[ 0 ]->ldctl_value.bv_len + 1 );
1328                 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
1329                 AC_MEMCPY( &ctrls[ 0 ]->ldctl_value.bv_val[ STRLENOF( "dn:" ) ],
1330                                 assertedID.bv_val, assertedID.bv_len + 1 );
1331                 break;
1332         }
1333
1334         if ( op->o_ctrls ) {
1335                 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
1336                         ctrls[ i + 1 ] = op->o_ctrls[ i ];
1337                 }
1338         }
1339         ctrls[ i + 1 ] = NULL;
1340
1341 done:;
1342         if ( ctrls == NULL ) {
1343                 ctrls = op->o_ctrls;
1344         }
1345
1346         *pctrls = ctrls;
1347         
1348         return rs->sr_err;
1349 }
1350
1351 int
1352 ldap_back_proxy_authz_ctrl_free( Operation *op, LDAPControl ***pctrls )
1353 {
1354         LDAPControl     **ctrls = *pctrls;
1355
1356         /* we assume that the first control is the proxyAuthz
1357          * added by back-ldap, so it's the only one we explicitly 
1358          * free */
1359         if ( ctrls && ctrls != op->o_ctrls ) {
1360                 assert( ctrls[ 0 ] != NULL );
1361
1362                 if ( !BER_BVISNULL( &ctrls[ 0 ]->ldctl_value ) ) {
1363                         free( ctrls[ 0 ]->ldctl_value.bv_val );
1364                 }
1365
1366                 free( ctrls[ 0 ] );
1367                 free( ctrls );
1368         } 
1369
1370         *pctrls = NULL;
1371
1372         return 0;
1373 }