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