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