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