]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/bind.c
21b49cf007745fad361581c73286f65cb22cbbd1
[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-2006 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 #ifndef PRINT_CONNTREE
39 #define PRINT_CONNTREE 0
40 #endif /* !PRINT_CONNTREE */
41
42 #define LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ       "2.16.840.1.113730.3.4.12"
43
44 static int
45 ldap_back_proxy_authz_bind( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok );
46
47 static int
48 ldap_back_prepare_conn( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok );
49
50 static int
51 ldap_back_conndnlc_cmp( const void *c1, const void *c2 );
52
53 int
54 ldap_back_bind( Operation *op, SlapReply *rs )
55 {
56         ldapinfo_t      *li = (ldapinfo_t *) op->o_bd->be_private;
57         ldapconn_t      *lc;
58
59         int rc = 0;
60         ber_int_t msgid;
61
62         lc = ldap_back_getconn( op, rs, LDAP_BACK_BIND_SERR );
63         if ( !lc ) {
64                 return rs->sr_err;
65         }
66
67         if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
68                 ch_free( lc->lc_bound_ndn.bv_val );
69                 BER_BVZERO( &lc->lc_bound_ndn );
70         }
71         LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
72
73         /* method is always LDAP_AUTH_SIMPLE if we got here */
74         rs->sr_err = ldap_sasl_bind( lc->lc_ld, op->o_req_dn.bv_val,
75                         LDAP_SASL_SIMPLE,
76                         &op->orb_cred, op->o_ctrls, NULL, &msgid );
77         rc = ldap_back_op_result( lc, op, rs, msgid, 0, LDAP_BACK_SENDERR );
78
79         if ( rc == LDAP_SUCCESS ) {
80                 /* If defined, proxyAuthz will be used also when
81                  * back-ldap is the authorizing backend; for this
82                  * purpose, a successful bind is followed by a
83                  * bind with the configured identity assertion */
84                 /* NOTE: use with care */
85                 if ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) {
86                         ldap_back_proxy_authz_bind( lc, op, rs, LDAP_BACK_SENDERR );
87                         if ( !LDAP_BACK_CONN_ISBOUND( lc ) ) {
88                                 rc = 1;
89                                 goto done;
90                         }
91                 }
92
93                 LDAP_BACK_CONN_ISBOUND_SET( lc );
94                 ber_dupbv( &lc->lc_bound_ndn, &op->o_req_ndn );
95
96                 if ( LDAP_BACK_SAVECRED( li ) ) {
97                         if ( !BER_BVISNULL( &lc->lc_cred ) ) {
98                                 memset( lc->lc_cred.bv_val, 0,
99                                                 lc->lc_cred.bv_len );
100                         }
101                         ber_bvreplace( &lc->lc_cred, &op->orb_cred );
102                         ldap_set_rebind_proc( lc->lc_ld, li->li_rebind_f, lc );
103                 }
104         }
105 done:;
106
107         assert( lc->lc_binding == 1 );
108         lc->lc_binding = 0;
109
110         /* must re-insert if local DN changed as result of bind */
111         if ( !LDAP_BACK_CONN_ISBOUND( lc )
112                 || ( LDAP_BACK_CONN_ISBOUND( lc )
113                         && !dn_match( &op->o_req_ndn, &lc->lc_local_ndn ) ) )
114         {
115                 int             lerr = -1;
116                 ldapconn_t      *tmplc;
117
118                 /* wait for all other ops to release the connection */
119 retry_lock:;
120                 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
121                 if ( lc->lc_refcnt > 1 ) {
122                         ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
123                         ldap_pvt_thread_yield();
124                         goto retry_lock;
125                 }
126
127                 assert( lc->lc_refcnt == 1 );
128                 tmplc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
129                                 ldap_back_conndnlc_cmp );
130                 assert( tmplc == NULL || lc == tmplc );
131
132                 if ( LDAP_BACK_CONN_ISBOUND( lc ) ) {
133                         ber_bvreplace( &lc->lc_local_ndn, &op->o_req_ndn );
134                         lerr = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
135                                 ldap_back_conndn_cmp, ldap_back_conndn_dup );
136                 }
137
138                 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
139                 switch ( lerr ) {
140                 case 0:
141                         break;
142
143                 case -1:
144                         /* duplicate; someone else successfully bound
145                          * on the same connection with the same identity;
146                          * we can do this because lc_refcnt == 1 */
147                         ldap_back_conn_free( lc );
148                         lc = NULL;
149                 }
150         }
151
152         if ( lc != NULL ) {
153                 ldap_back_release_conn( op, rs, lc );
154         }
155
156         return( rc );
157 }
158
159 /*
160  * ldap_back_conndn_cmp
161  *
162  * compares two ldapconn_t based on the value of the conn pointer
163  * and of the local DN; used by avl stuff for insert, lookup
164  * and direct delete
165  */
166 int
167 ldap_back_conndn_cmp( const void *c1, const void *c2 )
168 {
169         const ldapconn_t        *lc1 = (const ldapconn_t *)c1;
170         const ldapconn_t        *lc2 = (const ldapconn_t *)c2;
171         int rc;
172
173         /* If local DNs don't match, it is definitely not a match */
174         /* For shared sessions, conn is NULL. Only explicitly
175          * bound sessions will have non-NULL conn.
176          */
177         rc = SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
178         if ( rc == 0 ) {
179                 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
180         }
181
182         return rc;
183 }
184
185 /*
186  * ldap_back_conndnlc_cmp
187  *
188  * compares two ldapconn_t based on the value of the conn pointer,
189  * the local DN and the lc pointer; used by avl stuff for insert, lookup
190  * and direct delete
191  */
192 static int
193 ldap_back_conndnlc_cmp( const void *c1, const void *c2 )
194 {
195         const ldapconn_t        *lc1 = (const ldapconn_t *)c1;
196         const ldapconn_t        *lc2 = (const ldapconn_t *)c2;
197         int rc;
198
199         /* If local DNs don't match, it is definitely not a match */
200         /* For shared sessions, conn is NULL. Only explicitly
201          * bound sessions will have non-NULL conn.
202          */
203         rc = SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
204         if ( rc == 0 ) {
205                 rc = ber_bvcmp( &lc1->lc_local_ndn, &lc2->lc_local_ndn );
206                 if ( rc == 0 ) {
207                         rc = SLAP_PTRCMP( lc1, lc2 );
208                 }
209         }
210
211         return rc;
212 }
213
214 /*
215  * ldap_back_conn_cmp
216  *
217  * compares two ldapconn_t based on the value of the conn pointer;
218  * used by avl stuff for delete of all conns with the same connid
219  */
220 int
221 ldap_back_conn_cmp( const void *c1, const void *c2 )
222 {
223         const ldapconn_t        *lc1 = (const ldapconn_t *)c1;
224         const ldapconn_t        *lc2 = (const ldapconn_t *)c2;
225
226         /* For shared sessions, conn is NULL. Only explicitly
227          * bound sessions will have non-NULL conn.
228          */
229         return SLAP_PTRCMP( lc1->lc_conn, lc2->lc_conn );
230 }
231
232 /*
233  * ldap_back_conndn_dup
234  *
235  * returns -1 in case a duplicate ldapconn_t has been inserted;
236  * used by avl stuff
237  */
238 int
239 ldap_back_conndn_dup( void *c1, void *c2 )
240 {
241         ldapconn_t      *lc1 = (ldapconn_t *)c1;
242         ldapconn_t      *lc2 = (ldapconn_t *)c2;
243
244         /* Cannot have more than one shared session with same DN */
245         if ( lc1->lc_conn == lc2->lc_conn &&
246                 dn_match( &lc1->lc_local_ndn, &lc2->lc_local_ndn ) )
247         {
248                 return -1;
249         }
250                 
251         return 0;
252 }
253
254 #if PRINT_CONNTREE > 0
255 static void
256 ravl_print( Avlnode *root, int depth )
257 {
258         int             i;
259         ldapconn_t      *lc;
260         
261         if ( root == 0 ) {
262                 return;
263         }
264         
265         ravl_print( root->avl_right, depth+1 );
266         
267         for ( i = 0; i < depth; i++ ) {
268                 fprintf( stderr, "-" );
269         }
270
271         lc = root->avl_data;
272         fprintf( stderr, "lc=%p local=\"%s\" conn=%p %s refcnt=%d\n",
273                 (void *)lc, lc->lc_local_ndn.bv_val, (void *)lc->lc_conn,
274                 avl_bf2str( root->avl_bf ), lc->lc_refcnt );
275         
276         ravl_print( root->avl_left, depth+1 );
277 }
278
279 static void
280 myprint( Avlnode *root )
281 {
282         fprintf( stderr, "========>\n" );
283         
284         if ( root == 0 ) {
285                 fprintf( stderr, "\tNULL\n" );
286
287         } else {
288                 ravl_print( root, 0 );
289         }
290         
291         fprintf( stderr, "<========\n" );
292 }
293 #endif /* PRINT_CONNTREE */
294
295 int
296 ldap_back_freeconn( Operation *op, ldapconn_t *lc, int dolock )
297 {
298         ldapinfo_t      *li = (ldapinfo_t *) op->o_bd->be_private;
299         ldapconn_t      *tmplc;
300
301         if ( dolock ) {
302                 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
303         }
304
305         tmplc = avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
306                         ldap_back_conndnlc_cmp );
307         assert( LDAP_BACK_CONN_TAINTED( lc ) || tmplc == lc );
308         if ( lc->lc_refcnt == 0 ) {
309                 ldap_back_conn_free( (void *)lc );
310         }
311
312         if ( dolock ) {
313                 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
314         }
315
316         return 0;
317 }
318
319 #ifdef HAVE_TLS
320 static int
321 ldap_back_start_tls(
322         LDAP            *ld,
323         int             protocol,
324         int             *is_tls,
325         const char      *url,
326         unsigned        flags,
327         int             retries,
328         const char      **text )
329 {
330         int             rc = LDAP_SUCCESS;
331         ldapinfo_t      dummy;
332
333         /* this is ridiculous... */
334         dummy.li_flags = flags;
335
336         /* start TLS ("tls-[try-]{start,propagate}" statements) */
337         if ( ( LDAP_BACK_USE_TLS( &dummy ) || ( *is_tls && LDAP_BACK_PROPAGATE_TLS( &dummy ) ) )
338                                 && !ldap_is_ldaps_url( url ) )
339         {
340 #ifdef SLAP_STARTTLS_ASYNCHRONOUS
341                 /*
342                  * use asynchronous StartTLS
343                  * in case, chase referral (not implemented yet)
344                  */
345                 int             msgid;
346
347                 if ( protocol == 0 ) {
348                         ldap_get_option( ld, LDAP_OPT_PROTOCOL_VERSION,
349                                         (void *)&protocol );
350                 }
351
352                 if ( protocol < LDAP_VERSION3 ) {
353                         /* we should rather bail out... */
354                         rc = LDAP_UNWILLING_TO_PERFORM;
355                         *text = "invalid protocol version";
356                 }
357
358                 if ( rc == LDAP_SUCCESS ) {
359                         rc = ldap_start_tls( ld, NULL, NULL, &msgid );
360                 }
361
362                 if ( rc == LDAP_SUCCESS ) {
363                         LDAPMessage     *res = NULL;
364                         struct timeval  tv;
365
366                         LDAP_BACK_TV_SET( &tv );
367
368 retry:;
369                         rc = ldap_result( ld, msgid, LDAP_MSG_ALL, &tv, &res );
370                         if ( rc < 0 ) {
371                                 rc = LDAP_UNAVAILABLE;
372
373                         } else if ( rc == 0 ) {
374                                 if ( retries != LDAP_BACK_RETRY_NEVER ) {
375                                         ldap_pvt_thread_yield();
376                                         if ( retries > 0 ) {
377                                                 retries--;
378                                         }
379                                         LDAP_BACK_TV_SET( &tv );
380                                         goto retry;
381                                 }
382                                 rc = LDAP_UNAVAILABLE;
383
384                         } else if ( rc == LDAP_RES_EXTENDED ) {
385                                 struct berval   *data = NULL;
386
387                                 rc = ldap_parse_extended_result( ld, res,
388                                                 NULL, &data, 0 );
389                                 if ( rc == LDAP_SUCCESS ) {
390                                         int err;
391                                         rc = ldap_parse_result( ld, res, &err,
392                                                 NULL, NULL, NULL, NULL, 1 );
393                                         if ( rc == LDAP_SUCCESS ) {
394                                                 rc = err;
395                                         }
396                                         res = NULL;
397                                         
398                                         /* FIXME: in case a referral 
399                                          * is returned, should we try
400                                          * using it instead of the 
401                                          * configured URI? */
402                                         if ( rc == LDAP_SUCCESS ) {
403                                                 rc = ldap_install_tls( ld );
404
405                                         } else if ( rc == LDAP_REFERRAL ) {
406                                                 rc = LDAP_UNWILLING_TO_PERFORM;
407                                                 *text = "unwilling to chase referral returned by Start TLS exop";
408                                         }
409
410                                         if ( data ) {
411                                                 if ( data->bv_val ) {
412                                                         ber_memfree( data->bv_val );
413                                                 }
414                                                 ber_memfree( data );
415                                         }
416                                 }
417
418                         } else {
419                                 rc = LDAP_OTHER;
420                         }
421
422                         if ( res != NULL ) {
423                                 ldap_msgfree( res );
424                         }
425                 }
426 #else /* ! SLAP_STARTTLS_ASYNCHRONOUS */
427                 /*
428                  * use synchronous StartTLS
429                  */
430                 rc = ldap_start_tls_s( ld, NULL, NULL );
431 #endif /* ! SLAP_STARTTLS_ASYNCHRONOUS */
432
433                 /* if StartTLS is requested, only attempt it if the URL
434                  * is not "ldaps://"; this may occur not only in case
435                  * of misconfiguration, but also when used in the chain 
436                  * overlay, where the "uri" can be parsed out of a referral */
437                 switch ( rc ) {
438                 case LDAP_SUCCESS:
439                         *is_tls = 1;
440                         break;
441
442                 case LDAP_SERVER_DOWN:
443                         break;
444
445                 default:
446                         if ( LDAP_BACK_TLS_CRITICAL( &dummy ) ) {
447                                 *text = "could not start TLS";
448                                 break;
449                         }
450
451                         /* in case Start TLS is not critical */
452                         *is_tls = 0;
453                         rc = LDAP_SUCCESS;
454                         break;
455                 }
456
457         } else {
458                 *is_tls = 0;
459         }
460
461         return rc;
462 }
463 #endif /* HAVE_TLS */
464
465 static int
466 ldap_back_prepare_conn( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
467 {
468         ldapinfo_t      *li = (ldapinfo_t *)op->o_bd->be_private;
469         int             version;
470         LDAP            *ld = NULL;
471 #ifdef HAVE_TLS
472         int             is_tls = op->o_conn->c_is_tls;
473 #endif /* HAVE_TLS */
474
475         assert( lcp != NULL );
476
477         ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
478         rs->sr_err = ldap_initialize( &ld, li->li_uri );
479         ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
480         if ( rs->sr_err != LDAP_SUCCESS ) {
481                 goto error_return;
482         }
483
484         if ( li->li_urllist_f ) {
485                 ldap_set_urllist_proc( ld, li->li_urllist_f, li->li_urllist_p );
486         }
487
488         /* Set LDAP version. This will always succeed: If the client
489          * bound with a particular version, then so can we.
490          */
491         if ( li->li_version != 0 ) {
492                 version = li->li_version;
493
494         } else if ( op->o_protocol != 0 ) {
495                 version = op->o_protocol;
496
497         } else {
498                 /* assume it's an internal op; set to LDAPv3 */
499                 version = LDAP_VERSION3;
500         }
501         ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, (const void *)&version );
502
503         /* automatically chase referrals ("chase-referrals [{yes|no}]" statement) */
504         ldap_set_option( ld, LDAP_OPT_REFERRALS,
505                 LDAP_BACK_CHASE_REFERRALS( li ) ? LDAP_OPT_ON : LDAP_OPT_OFF );
506
507         if ( li->li_network_timeout > 0 ) {
508                 struct timeval          tv;
509
510                 tv.tv_sec = li->li_network_timeout;
511                 tv.tv_usec = 0;
512                 ldap_set_option( ld, LDAP_OPT_NETWORK_TIMEOUT, (const void *)&tv );
513         }
514
515 #ifdef HAVE_TLS
516         ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
517         rs->sr_err = ldap_back_start_tls( ld, op->o_protocol, &is_tls,
518                         li->li_uri, li->li_flags, li->li_nretries, &rs->sr_text );
519         ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
520         if ( rs->sr_err != LDAP_SUCCESS ) {
521                 ldap_unbind_ext( ld, NULL, NULL );
522                 goto error_return;
523         }
524 #endif /* HAVE_TLS */
525
526         if ( *lcp == NULL ) {
527                 *lcp = (ldapconn_t *)ch_calloc( 1, sizeof( ldapconn_t ) );
528                 (*lcp)->lc_flags = li->li_flags;
529         }
530         (*lcp)->lc_ld = ld;
531         (*lcp)->lc_refcnt = 1;
532         (*lcp)->lc_binding = 1;
533 #ifdef HAVE_TLS
534         if ( is_tls ) {
535                 LDAP_BACK_CONN_ISTLS_SET( *lcp );
536         } else {
537                 LDAP_BACK_CONN_ISTLS_CLEAR( *lcp );
538         }
539 #endif /* HAVE_TLS */
540
541 error_return:;
542         if ( rs->sr_err != LDAP_SUCCESS ) {
543                 rs->sr_err = slap_map_api2result( rs );
544                 if ( sendok & LDAP_BACK_SENDERR ) {
545                         if ( rs->sr_text == NULL ) {
546                                 rs->sr_text = "ldap_initialize() failed";
547                         }
548                         send_ldap_result( op, rs );
549                         rs->sr_text = NULL;
550                 }
551
552         } else {
553                 if ( li->li_conn_ttl > 0 ) {
554                         (*lcp)->lc_create_time = op->o_time;
555                 }
556         }
557
558         return rs->sr_err;
559 }
560
561 ldapconn_t *
562 ldap_back_getconn( Operation *op, SlapReply *rs, ldap_back_send_t sendok )
563 {
564         ldapinfo_t      *li = (ldapinfo_t *)op->o_bd->be_private;
565         ldapconn_t      *lc = NULL,
566                         lc_curr = { 0 };
567         int             refcnt = 1, binding = 1;
568
569         /* Internal searches are privileged and shared. So is root. */
570         if ( op->o_do_not_cache || be_isroot( op ) ) {
571                 LDAP_BACK_CONN_ISPRIV_SET( &lc_curr );
572                 lc_curr.lc_local_ndn = op->o_bd->be_rootndn;
573                 lc_curr.lc_conn = LDAP_BACK_PCONN_SET( op );
574
575         } else {
576                 lc_curr.lc_local_ndn = op->o_ndn;
577                 /* Explicit binds must not be shared */
578                 if ( op->o_tag == LDAP_REQ_BIND || SLAP_IS_AUTHZ_BACKEND( op ) ) {
579                         lc_curr.lc_conn = op->o_conn;
580         
581                 } else {
582                         lc_curr.lc_conn = LDAP_BACK_PCONN_SET( op );
583                 }
584         }
585
586         /* Explicit Bind requests always get their own conn */
587         if ( !( sendok & LDAP_BACK_BINDING ) ) {
588                 /* Searches for a ldapconn in the avl tree */
589 retry_lock:
590                 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
591
592                 lc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree, 
593                                 (caddr_t)&lc_curr, ldap_back_conndn_cmp );
594                 if ( lc != NULL ) {
595                         /* Don't reuse connections while they're still binding */
596                         if ( LDAP_BACK_CONN_BINDING( lc ) ) {
597                                 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
598                                 ldap_pvt_thread_yield();
599                                 goto retry_lock;
600                         }
601                         refcnt = ++lc->lc_refcnt;
602                         binding = ++lc->lc_binding;
603                 }
604                 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
605         }
606
607         /* Looks like we didn't get a bind. Open a new session... */
608         if ( lc == NULL ) {
609                 if ( ldap_back_prepare_conn( &lc, op, rs, sendok ) != LDAP_SUCCESS ) {
610                         return NULL;
611                 }
612                 if ( sendok & LDAP_BACK_BINDING ) {
613                         LDAP_BACK_CONN_BINDING_SET( lc );
614                 }
615                 lc->lc_conn = lc_curr.lc_conn;
616                 ber_dupbv( &lc->lc_local_ndn, &lc_curr.lc_local_ndn );
617
618                 if ( LDAP_BACK_CONN_ISPRIV( &lc_curr ) ) {
619                         ber_dupbv( &lc->lc_cred, &li->li_acl_passwd );
620                         ber_dupbv( &lc->lc_bound_ndn, &li->li_acl_authcDN );
621                         LDAP_BACK_CONN_ISPRIV_SET( lc );
622
623                 } else {
624                         BER_BVZERO( &lc->lc_cred );
625                         BER_BVZERO( &lc->lc_bound_ndn );
626 #if 0
627                         /* FIXME: if we set lc_bound_ndn = o_ndn
628                          * we end up with a bind with DN but no password! */
629                         if ( !BER_BVISEMPTY( &op->o_ndn )
630                                 && SLAP_IS_AUTHZ_BACKEND( op ) )
631                         {
632                                 ber_dupbv( &lc->lc_bound_ndn, &op->o_ndn );
633                         }
634 #endif
635                 }
636
637 #ifdef HAVE_TLS
638                 /* if start TLS failed but it was not mandatory,
639                  * check if the non-TLS connection was already
640                  * in cache; in case, destroy the newly created
641                  * connection and use the existing one */
642                 if ( lc->lc_conn == LDAP_BACK_PCONN_TLS
643                                 && !ldap_tls_inplace( lc->lc_ld ) )
644                 {
645                         ldapconn_t *tmplc;
646                         
647                         lc_curr.lc_conn = LDAP_BACK_PCONN;
648                         ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
649                         tmplc = (ldapconn_t *)avl_find( li->li_conninfo.lai_tree, 
650                                         (caddr_t)&lc_curr, ldap_back_conndn_cmp );
651                         if ( tmplc != NULL ) {
652                                 refcnt = ++tmplc->lc_refcnt;
653                                 binding = ++tmplc->lc_binding;
654                                 ldap_back_conn_free( lc );
655                                 lc = tmplc;
656                         }
657                         ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
658
659                         if ( tmplc != NULL ) {
660                                 goto done;
661                         }
662                 }
663 #endif /* HAVE_TLS */
664
665                 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
666
667                 /* Inserts the newly created ldapconn in the avl tree */
668                 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
669
670                 assert( lc->lc_refcnt == 1 );
671                 assert( lc->lc_binding == 1 );
672                 rs->sr_err = avl_insert( &li->li_conninfo.lai_tree, (caddr_t)lc,
673                         ldap_back_conndn_cmp, ldap_back_conndn_dup );
674
675 #if PRINT_CONNTREE > 0
676                 myprint( li->li_conninfo.lai_tree );
677 #endif /* PRINT_CONNTREE */
678         
679                 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
680
681                 Debug( LDAP_DEBUG_TRACE,
682                         "=>ldap_back_getconn: conn %p inserted refcnt=%u binding=%u\n",
683                         (void *)lc, refcnt, binding );
684         
685                 /* Err could be -1 in case a duplicate ldapconn is inserted */
686                 switch ( rs->sr_err ) {
687                 case 0:
688                         break;
689
690                 case -1:
691                         if ( !( sendok & LDAP_BACK_BINDING ) ) {
692                                 /* duplicate: free and try to get the newly created one */
693                                 goto retry_lock;
694                         }
695                         /* taint connection, so that it'll be freed when released */
696                         ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
697                         (void *)avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
698                                         ldap_back_conndnlc_cmp );
699                         ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
700                         LDAP_BACK_CONN_TAINTED_SET( lc );
701                         break;
702
703                 default:
704                         ldap_back_conn_free( lc );
705                         rs->sr_err = LDAP_OTHER;
706                         rs->sr_text = "proxy bind collision";
707                         if ( op->o_conn && ( sendok & LDAP_BACK_SENDERR ) ) {
708                                 send_ldap_result( op, rs );
709                                 rs->sr_text = NULL;
710                         }
711                         return NULL;
712                 }
713
714         } else {
715                 char    buf[ SLAP_TEXT_BUFLEN ];
716                 int     expiring = 0;
717
718                 if ( ( li->li_idle_timeout != 0 && op->o_time > lc->lc_time + li->li_idle_timeout )
719                         || ( li->li_conn_ttl != 0 && op->o_time > lc->lc_create_time + li->li_conn_ttl ) )
720                 {
721                         expiring = 1;
722
723                         /* let it be used, but taint/delete it so that 
724                          * no-one else can look it up any further */
725                         ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
726                         (void *)avl_delete( &li->li_conninfo.lai_tree, (caddr_t)lc,
727                                         ldap_back_conndnlc_cmp );
728                         ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
729                         LDAP_BACK_CONN_TAINTED_SET( lc );
730                 }
731
732                 if ( LogTest( LDAP_DEBUG_TRACE ) ) {
733                         snprintf( buf, sizeof( buf ),
734                                 "conn %p fetched refcnt=%u binding=%u%s",
735                                 (void *)lc, refcnt, binding, expiring ? " expiring" : "" );
736                         Debug( LDAP_DEBUG_TRACE,
737                                 "=>ldap_back_getconn: %s.\n", buf, 0, 0 );
738                 }
739         
740         }
741
742 #ifdef HAVE_TLS
743 done:;
744 #endif /* HAVE_TLS */
745         if ( li->li_idle_timeout && lc ) {
746                 lc->lc_time = op->o_time;
747         }
748
749         return lc;
750 }
751
752 void
753 ldap_back_release_conn_lock(
754         Operation               *op,
755         SlapReply               *rs,
756         ldapconn_t              *lc,
757         int                     dolock )
758 {
759         ldapinfo_t      *li = (ldapinfo_t *)op->o_bd->be_private;
760
761         if ( dolock ) {
762                 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
763         }
764         assert( lc->lc_refcnt > 0 );
765         LDAP_BACK_CONN_BINDING_CLEAR( lc );
766         lc->lc_refcnt--;
767         if ( LDAP_BACK_CONN_TAINTED( lc ) ) {
768                 ldap_back_freeconn( op, lc, 0 );
769         }
770         if ( dolock ) {
771                 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
772         }
773 }
774
775 /*
776  * ldap_back_dobind
777  *
778  * Note: as the check for the value of lc->lc_bound was already here, I removed
779  * it from all the callers, and I made the function return the flag, so
780  * it can be used to simplify the check.
781  *
782  * Note: dolock indicates whether li->li_conninfo.lai_mutex must be locked or not
783  */
784 static int
785 ldap_back_dobind_int(
786         ldapconn_t              *lc,
787         Operation               *op,
788         SlapReply               *rs,
789         ldap_back_send_t        sendok,
790         int                     retries,
791         int                     dolock )
792 {       
793         ldapinfo_t      *li = (ldapinfo_t *)op->o_bd->be_private;
794
795         int             rc, binding = 0;
796         ber_int_t       msgid;
797
798         assert( retries >= 0 );
799
800 retry_lock:;
801         if ( dolock ) {
802                 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
803         }
804
805         if ( binding == 0 ) {
806                 /* check if already bound */
807                 rc = LDAP_BACK_CONN_ISBOUND( lc );
808                 if ( rc ) {
809                         lc->lc_binding--;
810                         if ( dolock ) {
811                                 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
812                         }
813                         return rc;
814                 }
815
816                 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
817                         /* if someone else is about to bind it, give up and retry */
818                         if ( dolock ) {
819                                 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
820                         }
821                         ldap_pvt_thread_yield();
822                         goto retry_lock;
823
824                 } else {
825                         /* otherwise this thread will bind it */
826                         LDAP_BACK_CONN_BINDING_SET( lc );
827                         binding = 1;
828                 }
829         }
830
831         /* wait for pending operations to finish */
832         /* FIXME: may become a bottleneck! */
833         if ( lc->lc_refcnt != lc->lc_binding ) {
834                 if ( dolock ) {
835                         ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
836                 }
837                 ldap_pvt_thread_yield();
838                 goto retry_lock;
839         }
840
841         if ( dolock ) {
842                 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
843         }
844
845 #if 0
846         while ( lc->lc_refcnt > 1 ) {
847                 ldap_pvt_thread_yield();
848                 rc = LDAP_BACK_CONN_ISBOUND( lc );
849                 if ( rc ) {
850                         return rc;
851                 }
852         }
853
854         if ( dolock ) {
855                 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
856         }
857         LDAP_BACK_CONN_BINDING_SET( lc );
858         if ( dolock ) {
859                 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
860         }
861 #endif
862
863         /*
864          * FIXME: we need to let clients use proxyAuthz
865          * otherwise we cannot do symmetric pools of servers;
866          * we have to live with the fact that a user can
867          * authorize itself as any ID that is allowed
868          * by the authzTo directive of the "proxyauthzdn".
869          */
870         /*
871          * NOTE: current Proxy Authorization specification
872          * and implementation do not allow proxy authorization
873          * control to be provided with Bind requests
874          */
875         /*
876          * if no bind took place yet, but the connection is bound
877          * and the "idassert-authcDN" (or other ID) is set, 
878          * then bind as the asserting identity and explicitly 
879          * add the proxyAuthz control to every operation with the
880          * dn bound to the connection as control value.
881          * This is done also if this is the authrizing backend,
882          * but the "override" flag is given to idassert.
883          * It allows to use SASL bind and yet proxyAuthz users
884          */
885         if ( op->o_conn != NULL &&
886                         !op->o_do_not_cache &&
887                         ( BER_BVISNULL( &lc->lc_bound_ndn ) ||
888                           ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) )
889         {
890                 (void)ldap_back_proxy_authz_bind( lc, op, rs, sendok );
891                 goto done;
892         }
893
894 #ifdef HAVE_CYRUS_SASL
895         if ( LDAP_BACK_CONN_ISPRIV( lc )
896                 && li->li_acl_authmethod == LDAP_AUTH_SASL )
897         {
898                 void            *defaults = NULL;
899
900                 if ( li->li_acl_secprops != NULL ) {
901                         rc = ldap_set_option( lc->lc_ld,
902                                 LDAP_OPT_X_SASL_SECPROPS, li->li_acl_secprops);
903
904                         if ( rc != LDAP_OPT_SUCCESS ) {
905                                 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
906                                         "(SECPROPS,\"%s\") failed!\n",
907                                         li->li_acl_secprops, 0, 0 );
908                                 goto done;
909                         }
910                 }
911
912                 defaults = lutil_sasl_defaults( lc->lc_ld,
913                                 li->li_acl_sasl_mech.bv_val,
914                                 li->li_acl_sasl_realm.bv_val,
915                                 li->li_acl_authcID.bv_val,
916                                 li->li_acl_passwd.bv_val,
917                                 NULL );
918
919                 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld,
920                                 li->li_acl_authcDN.bv_val,
921                                 li->li_acl_sasl_mech.bv_val, NULL, NULL,
922                                 LDAP_SASL_QUIET, lutil_sasl_interact,
923                                 defaults );
924
925                 lutil_sasl_freedefs( defaults );
926
927                 rs->sr_err = slap_map_api2result( rs );
928                 if ( rs->sr_err != LDAP_SUCCESS ) {
929                         LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
930                         send_ldap_result( op, rs );
931
932                 } else {
933                         LDAP_BACK_CONN_ISBOUND_SET( lc );
934                 }
935                 goto done;
936         }
937 #endif /* HAVE_CYRUS_SASL */
938
939 retry:;
940         rs->sr_err = ldap_sasl_bind( lc->lc_ld,
941                         lc->lc_bound_ndn.bv_val,
942                         LDAP_SASL_SIMPLE, &lc->lc_cred,
943                         NULL, NULL, &msgid );
944
945         if ( rs->sr_err == LDAP_SERVER_DOWN ) {
946                 if ( retries != LDAP_BACK_RETRY_NEVER ) {
947                         if ( dolock ) {
948                                 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
949                         }
950
951                         assert( lc->lc_refcnt > 0 );
952                         if ( lc->lc_refcnt == 1 ) {
953                                 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
954                                 lc->lc_ld = NULL;
955
956                                 /* lc here must be the regular lc, reset and ready for init */
957                                 rs->sr_err = ldap_back_prepare_conn( &lc, op, rs, sendok );
958                                 if ( rs->sr_err != LDAP_SUCCESS ) {
959                                         lc->lc_binding--;
960                                         lc->lc_refcnt = 0;
961                                 }
962                         }
963
964                         if ( dolock ) {
965                                 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
966                         }
967
968                         if ( rs->sr_err == LDAP_SUCCESS ) {
969                                 if ( retries > 0 ) {
970                                         retries--;
971                                 }
972                                 goto retry;
973                         }
974
975                 } else {
976                         if ( dolock ) {
977                                 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
978                         }
979                         lc->lc_binding--;
980                         if ( dolock ) {
981                                 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
982                         }
983                 }
984
985                 lc->lc_binding--;
986                 ldap_back_freeconn( op, lc, dolock );
987                 rs->sr_err = slap_map_api2result( rs );
988
989                 return 0;
990         }
991
992         rc = ldap_back_op_result( lc, op, rs, msgid, 0, sendok );
993         if ( rc == LDAP_SUCCESS ) {
994                 LDAP_BACK_CONN_ISBOUND_SET( lc );
995         }
996
997 done:;
998         lc->lc_binding--;
999         LDAP_BACK_CONN_BINDING_CLEAR( lc );
1000         rc = LDAP_BACK_CONN_ISBOUND( lc );
1001         if ( !rc ) {
1002                 ldap_back_release_conn_lock( op, rs, lc, dolock );
1003         }
1004
1005         return rc;
1006 }
1007
1008 int
1009 ldap_back_dobind( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1010 {
1011         ldapinfo_t      *li = (ldapinfo_t *)op->o_bd->be_private;
1012
1013         return ldap_back_dobind_int( lc, op, rs, sendok, li->li_nretries, 1 );
1014 }
1015
1016 /*
1017  * ldap_back_default_rebind
1018  *
1019  * This is a callback used for chasing referrals using the same
1020  * credentials as the original user on this session.
1021  */
1022 int 
1023 ldap_back_default_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
1024         ber_int_t msgid, void *params )
1025 {
1026         ldapconn_t      *lc = (ldapconn_t *)params;
1027
1028 #ifdef HAVE_TLS
1029         /* ... otherwise we couldn't get here */
1030         assert( lc != NULL );
1031
1032         if ( !ldap_tls_inplace( ld ) ) {
1033                 int             is_tls = LDAP_BACK_CONN_ISTLS( lc ),
1034                                 rc;
1035                 const char      *text = NULL;
1036
1037                 rc = ldap_back_start_tls( ld, 0, &is_tls, url, lc->lc_flags,
1038                         LDAP_BACK_RETRY_DEFAULT, &text );
1039                 if ( rc != LDAP_SUCCESS ) {
1040                         return rc;
1041                 }
1042         }
1043 #endif /* HAVE_TLS */
1044
1045         /* FIXME: add checks on the URL/identity? */
1046
1047         return ldap_sasl_bind_s( ld, lc->lc_bound_ndn.bv_val,
1048                         LDAP_SASL_SIMPLE, &lc->lc_cred, NULL, NULL, NULL );
1049 }
1050
1051 /*
1052  * ldap_back_default_urllist
1053  */
1054 int 
1055 ldap_back_default_urllist(
1056         LDAP            *ld,
1057         LDAPURLDesc     **urllist,
1058         LDAPURLDesc     **url,
1059         void            *params )
1060 {
1061         ldapinfo_t      *li = (ldapinfo_t *)params;
1062         LDAPURLDesc     **urltail;
1063
1064         if ( urllist == url ) {
1065                 return LDAP_SUCCESS;
1066         }
1067
1068         for ( urltail = &(*url)->lud_next; *urltail; urltail = &(*urltail)->lud_next )
1069                 /* count */ ;
1070
1071         *urltail = *urllist;
1072         *urllist = *url;
1073         *url = NULL;
1074
1075         ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
1076         if ( li->li_uri ) {
1077                 ch_free( li->li_uri );
1078         }
1079
1080         ldap_get_option( ld, LDAP_OPT_URI, (void *)&li->li_uri );
1081         ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
1082
1083         return LDAP_SUCCESS;
1084 }
1085
1086 int
1087 ldap_back_op_result(
1088                 ldapconn_t              *lc,
1089                 Operation               *op,
1090                 SlapReply               *rs,
1091                 ber_int_t               msgid,
1092                 time_t                  timeout,
1093                 ldap_back_send_t        sendok )
1094 {
1095         char            *match = NULL;
1096         LDAPMessage     *res = NULL;
1097         char            *text = NULL;
1098
1099 #define ERR_OK(err) ((err) == LDAP_SUCCESS || (err) == LDAP_COMPARE_FALSE || (err) == LDAP_COMPARE_TRUE)
1100
1101         rs->sr_text = NULL;
1102         rs->sr_matched = NULL;
1103
1104         /* if the error recorded in the reply corresponds
1105          * to a successful state, get the error from the
1106          * remote server response */
1107         if ( ERR_OK( rs->sr_err ) ) {
1108                 int             rc;
1109                 struct timeval  tv;
1110
1111                 if ( timeout ) {
1112                         tv.tv_sec = timeout;
1113                         tv.tv_usec = 0;
1114
1115                 } else {
1116                         LDAP_BACK_TV_SET( &tv );
1117                 }
1118
1119 retry:;
1120                 /* if result parsing fails, note the failure reason */
1121                 rc = ldap_result( lc->lc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
1122                 switch ( rc ) {
1123                 case 0:
1124                         if ( timeout ) {
1125                                 (void)ldap_abandon_ext( lc->lc_ld, msgid, NULL, NULL );
1126                                 rs->sr_err = op->o_protocol >= LDAP_VERSION3 ?
1127                                         LDAP_ADMINLIMIT_EXCEEDED : LDAP_OPERATIONS_ERROR;
1128                                 rs->sr_text = "Operation timed out";
1129                                 break;
1130                         }
1131
1132                         LDAP_BACK_TV_SET( &tv );
1133                         ldap_pvt_thread_yield();
1134                         goto retry;
1135
1136                 case -1:
1137                         ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
1138                                         &rs->sr_err );
1139                         break;
1140
1141
1142                 /* otherwise get the result; if it is not
1143                  * LDAP_SUCCESS, record it in the reply
1144                  * structure (this includes 
1145                  * LDAP_COMPARE_{TRUE|FALSE}) */
1146                 default:
1147                         rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
1148                                         &match, &text, NULL, NULL, 1 );
1149                         rs->sr_text = text;
1150                         if ( rc != LDAP_SUCCESS ) {
1151                                 rs->sr_err = rc;
1152                         }
1153                 }
1154         }
1155
1156         /* if the error in the reply structure is not
1157          * LDAP_SUCCESS, try to map it from client 
1158          * to server error */
1159         if ( !ERR_OK( rs->sr_err ) ) {
1160                 rs->sr_err = slap_map_api2result( rs );
1161
1162                 /* internal ops ( op->o_conn == NULL ) 
1163                  * must not reply to client */
1164                 if ( op->o_conn && !op->o_do_not_cache && match ) {
1165
1166                         /* record the (massaged) matched
1167                          * DN into the reply structure */
1168                         rs->sr_matched = match;
1169                 }
1170         }
1171         if ( op->o_conn &&
1172                         ( ( sendok & LDAP_BACK_SENDOK ) 
1173                           || ( ( sendok & LDAP_BACK_SENDERR ) && rs->sr_err != LDAP_SUCCESS ) ) )
1174         {
1175                 send_ldap_result( op, rs );
1176         }
1177         if ( match ) {
1178                 if ( rs->sr_matched != match ) {
1179                         free( (char *)rs->sr_matched );
1180                 }
1181                 rs->sr_matched = NULL;
1182                 ldap_memfree( match );
1183         }
1184         if ( text ) {
1185                 ldap_memfree( text );
1186         }
1187         rs->sr_text = NULL;
1188         return( ERR_OK( rs->sr_err ) ? LDAP_SUCCESS : rs->sr_err );
1189 }
1190
1191 /* return true if bound, false if failed */
1192 int
1193 ldap_back_retry( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1194 {
1195         int             rc = 0;
1196         ldapinfo_t      *li = (ldapinfo_t *)op->o_bd->be_private;
1197
1198         assert( lcp != NULL );
1199         assert( *lcp != NULL );
1200
1201         ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1202
1203         if ( (*lcp)->lc_refcnt == 1 ) {
1204                 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
1205                 Debug( LDAP_DEBUG_ANY,
1206                         "%s ldap_back_retry: retrying URI=\"%s\" DN=\"%s\"\n",
1207                         op->o_log_prefix, li->li_uri,
1208                         BER_BVISNULL( &(*lcp)->lc_bound_ndn ) ?
1209                                 "" : (*lcp)->lc_bound_ndn.bv_val );
1210                 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
1211
1212                 ldap_unbind_ext( (*lcp)->lc_ld, NULL, NULL );
1213                 (*lcp)->lc_ld = NULL;
1214                 LDAP_BACK_CONN_ISBOUND_CLEAR( (*lcp) );
1215
1216                 /* lc here must be the regular lc, reset and ready for init */
1217                 rc = ldap_back_prepare_conn( lcp, op, rs, sendok );
1218                 if ( rc != LDAP_SUCCESS ) {
1219                         rc = 0;
1220                         *lcp = NULL;
1221
1222                 } else {
1223                         rc = ldap_back_dobind_int( *lcp, op, rs, sendok, 0, 0 );
1224                         if ( rc == 0 ) {
1225                                 *lcp = NULL;
1226                         }
1227                 }
1228
1229         } else {
1230                 Debug( LDAP_DEBUG_TRACE,
1231                         "ldap_back_retry: conn %p refcnt=%u unable to retry.\n",
1232                         (void *)(*lcp), (*lcp)->lc_refcnt, 0 );
1233
1234                 ldap_back_release_conn_lock( op, rs, *lcp, 0 );
1235                 *lcp = NULL;
1236
1237                 if ( sendok ) {
1238                         rs->sr_err = LDAP_UNAVAILABLE;
1239                         rs->sr_text = "unable to retry";
1240                         send_ldap_result( op, rs );
1241                 }
1242         }
1243
1244         ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1245
1246         return rc;
1247 }
1248
1249 static int
1250 ldap_back_proxy_authz_bind( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1251 {
1252         ldapinfo_t      *li = (ldapinfo_t *)op->o_bd->be_private;
1253         struct berval   binddn = slap_empty_bv;
1254         struct berval   bindcred = slap_empty_bv;
1255         struct berval   ndn;
1256         int             dobind = 0;
1257         int             msgid;
1258         int             rc;
1259
1260         /* don't proxyAuthz if protocol is not LDAPv3 */
1261         switch ( li->li_version ) {
1262         case LDAP_VERSION3:
1263                 break;
1264
1265         case 0:
1266                 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
1267                         break;
1268                 }
1269                 /* fall thru */
1270
1271         default:
1272                 goto done;
1273         }
1274
1275         if ( op->o_tag == LDAP_REQ_BIND ) {
1276                 ndn = op->o_req_ndn;
1277
1278         } else if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1279                 ndn = op->o_conn->c_ndn;
1280
1281         } else {
1282                 ndn = op->o_ndn;
1283         }
1284
1285         /*
1286          * FIXME: we need to let clients use proxyAuthz
1287          * otherwise we cannot do symmetric pools of servers;
1288          * we have to live with the fact that a user can
1289          * authorize itself as any ID that is allowed
1290          * by the authzTo directive of the "proxyauthzdn".
1291          */
1292         /*
1293          * NOTE: current Proxy Authorization specification
1294          * and implementation do not allow proxy authorization
1295          * control to be provided with Bind requests
1296          */
1297         /*
1298          * if no bind took place yet, but the connection is bound
1299          * and the "proxyauthzdn" is set, then bind as 
1300          * "proxyauthzdn" and explicitly add the proxyAuthz 
1301          * control to every operation with the dn bound 
1302          * to the connection as control value.
1303          */
1304
1305         /* bind as proxyauthzdn only if no idassert mode
1306          * is requested, or if the client's identity
1307          * is authorized */
1308         switch ( li->li_idassert_mode ) {
1309         case LDAP_BACK_IDASSERT_LEGACY:
1310                 if ( !BER_BVISNULL( &ndn ) && !BER_BVISEMPTY( &ndn ) ) {
1311                         if ( !BER_BVISNULL( &li->li_idassert_authcDN ) && !BER_BVISEMPTY( &li->li_idassert_authcDN ) )
1312                         {
1313                                 binddn = li->li_idassert_authcDN;
1314                                 bindcred = li->li_idassert_passwd;
1315                                 dobind = 1;
1316                         }
1317                 }
1318                 break;
1319
1320         default:
1321                 /* NOTE: rootdn can always idassert */
1322                 if ( BER_BVISNULL( &ndn ) && li->li_idassert_authz == NULL ) {
1323                         if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1324                                 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
1325                                 if ( sendok & LDAP_BACK_SENDERR ) {
1326                                         send_ldap_result( op, rs );
1327                                 }
1328                                 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1329
1330                         } else {
1331                                 rs->sr_err = LDAP_SUCCESS;
1332                                 binddn = slap_empty_bv;
1333                                 bindcred = slap_empty_bv;
1334                                 break;
1335                         }
1336
1337                         goto done;
1338
1339                 } else if ( li->li_idassert_authz && !be_isroot( op ) ) {
1340                         struct berval authcDN;
1341
1342                         if ( BER_BVISNULL( &ndn ) ) {
1343                                 authcDN = slap_empty_bv;
1344
1345                         } else {
1346                                 authcDN = ndn;
1347                         }       
1348                         rs->sr_err = slap_sasl_matches( op, li->li_idassert_authz,
1349                                         &authcDN, &authcDN );
1350                         if ( rs->sr_err != LDAP_SUCCESS ) {
1351                                 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1352                                         if ( sendok & LDAP_BACK_SENDERR ) {
1353                                                 send_ldap_result( op, rs );
1354                                         }
1355                                         LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1356
1357                                 } else {
1358                                         rs->sr_err = LDAP_SUCCESS;
1359                                         binddn = slap_empty_bv;
1360                                         bindcred = slap_empty_bv;
1361                                         break;
1362                                 }
1363
1364                                 goto done;
1365                         }
1366                 }
1367
1368                 binddn = li->li_idassert_authcDN;
1369                 bindcred = li->li_idassert_passwd;
1370                 dobind = 1;
1371                 break;
1372         }
1373
1374         if ( dobind && li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
1375 #ifdef HAVE_CYRUS_SASL
1376                 void            *defaults = NULL;
1377                 struct berval   authzID = BER_BVNULL;
1378                 int             freeauthz = 0;
1379
1380                 /* if SASL supports native authz, prepare for it */
1381                 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
1382                                 ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
1383                 {
1384                         switch ( li->li_idassert_mode ) {
1385                         case LDAP_BACK_IDASSERT_OTHERID:
1386                         case LDAP_BACK_IDASSERT_OTHERDN:
1387                                 authzID = li->li_idassert_authzID;
1388                                 break;
1389
1390                         case LDAP_BACK_IDASSERT_ANONYMOUS:
1391                                 BER_BVSTR( &authzID, "dn:" );
1392                                 break;
1393
1394                         case LDAP_BACK_IDASSERT_SELF:
1395                                 if ( BER_BVISNULL( &ndn ) ) {
1396                                         /* connection is not authc'd, so don't idassert */
1397                                         BER_BVSTR( &authzID, "dn:" );
1398                                         break;
1399                                 }
1400                                 authzID.bv_len = STRLENOF( "dn:" ) + ndn.bv_len;
1401                                 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
1402                                 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
1403                                 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
1404                                                 ndn.bv_val, ndn.bv_len + 1 );
1405                                 freeauthz = 1;
1406                                 break;
1407
1408                         default:
1409                                 break;
1410                         }
1411                 }
1412
1413                 if ( li->li_idassert_secprops != NULL ) {
1414                         rs->sr_err = ldap_set_option( lc->lc_ld,
1415                                 LDAP_OPT_X_SASL_SECPROPS,
1416                                 (void *)li->li_idassert_secprops );
1417
1418                         if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
1419                                 rs->sr_err = LDAP_OTHER;
1420                                 if ( sendok & LDAP_BACK_SENDERR ) {
1421                                         send_ldap_result( op, rs );
1422                                 }
1423                                 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1424                                 goto done;
1425                         }
1426                 }
1427
1428                 defaults = lutil_sasl_defaults( lc->lc_ld,
1429                                 li->li_idassert_sasl_mech.bv_val,
1430                                 li->li_idassert_sasl_realm.bv_val,
1431                                 li->li_idassert_authcID.bv_val,
1432                                 li->li_idassert_passwd.bv_val,
1433                                 authzID.bv_val );
1434
1435                 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld, binddn.bv_val,
1436                                 li->li_idassert_sasl_mech.bv_val, NULL, NULL,
1437                                 LDAP_SASL_QUIET, lutil_sasl_interact,
1438                                 defaults );
1439
1440                 rs->sr_err = slap_map_api2result( rs );
1441                 if ( rs->sr_err != LDAP_SUCCESS ) {
1442                         LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1443                         if ( sendok & LDAP_BACK_SENDERR ) {
1444                                 send_ldap_result( op, rs );
1445                         }
1446
1447                 } else {
1448                         LDAP_BACK_CONN_ISBOUND_SET( lc );
1449                 }
1450
1451                 lutil_sasl_freedefs( defaults );
1452                 if ( freeauthz ) {
1453                         slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
1454                 }
1455
1456                 goto done;
1457 #endif /* HAVE_CYRUS_SASL */
1458         }
1459
1460         switch ( li->li_idassert_authmethod ) {
1461         case LDAP_AUTH_NONE:
1462                 LDAP_BACK_CONN_ISBOUND_SET( lc );
1463                 goto done;
1464
1465         case LDAP_AUTH_SIMPLE:
1466                 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
1467                                 binddn.bv_val, LDAP_SASL_SIMPLE,
1468                                 &bindcred, NULL, NULL, &msgid );
1469                 break;
1470
1471         default:
1472                 /* unsupported! */
1473                 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1474                 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
1475                 if ( sendok & LDAP_BACK_SENDERR ) {
1476                         send_ldap_result( op, rs );
1477                 }
1478                 goto done;
1479         }
1480
1481         rc = ldap_back_op_result( lc, op, rs, msgid, 0, sendok );
1482         if ( rc == LDAP_SUCCESS ) {
1483                 LDAP_BACK_CONN_ISBOUND_SET( lc );
1484         }
1485 done:;
1486         return LDAP_BACK_CONN_ISBOUND( lc );
1487 }
1488
1489 /*
1490  * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
1491  * to existing server-side controls if required; if not,
1492  * the existing server-side controls are placed in *pctrls.
1493  * The caller, after using the controls in client API 
1494  * operations, if ( *pctrls != op->o_ctrls ), should
1495  * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
1496  * The function returns success if the control could
1497  * be added if required, or if it did nothing; in the future,
1498  * it might return some error if it failed.
1499  * 
1500  * if no bind took place yet, but the connection is bound
1501  * and the "proxyauthzdn" is set, then bind as "proxyauthzdn" 
1502  * and explicitly add proxyAuthz the control to every operation
1503  * with the dn bound to the connection as control value.
1504  *
1505  * If no server-side controls are defined for the operation,
1506  * simply add the proxyAuthz control; otherwise, if the
1507  * proxyAuthz control is not already set, add it as
1508  * the first one
1509  *
1510  * FIXME: is controls order significant for security?
1511  * ANSWER: controls ordering and interoperability
1512  * must be indicated by the specs of each control; if none
1513  * is specified, the order is irrelevant.
1514  */
1515 int
1516 ldap_back_proxy_authz_ctrl(
1517                 ldapconn_t      *lc,
1518                 Operation       *op,
1519                 SlapReply       *rs,
1520                 LDAPControl     ***pctrls )
1521 {
1522         ldapinfo_t      *li = (ldapinfo_t *) op->o_bd->be_private;
1523         LDAPControl     **ctrls = NULL;
1524         int             i = 0,
1525                         mode;
1526         struct berval   assertedID,
1527                         ndn;
1528
1529         *pctrls = NULL;
1530
1531         rs->sr_err = LDAP_SUCCESS;
1532
1533         /* don't proxyAuthz if protocol is not LDAPv3 */
1534         switch ( li->li_version ) {
1535         case LDAP_VERSION3:
1536                 break;
1537
1538         case 0:
1539                 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
1540                         break;
1541                 }
1542                 /* fall thru */
1543
1544         default:
1545                 goto done;
1546         }
1547
1548         /* FIXME: SASL/EXTERNAL over ldapi:// doesn't honor the authcID,
1549          * but if it is not set this test fails.  We need a different
1550          * means to detect if idassert is enabled */
1551         if ( ( BER_BVISNULL( &li->li_idassert_authcID ) || BER_BVISEMPTY( &li->li_idassert_authcID ) )
1552                         && ( BER_BVISNULL( &li->li_idassert_authcDN ) || BER_BVISEMPTY( &li->li_idassert_authcDN ) ) )
1553         {
1554                 goto done;
1555         }
1556
1557         if ( !op->o_conn || op->o_do_not_cache || be_isroot( op ) ) {
1558                 goto done;
1559         }
1560
1561         if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1562                 ndn = op->o_conn->c_ndn;
1563
1564         } else {
1565                 ndn = op->o_ndn;
1566         }
1567
1568         if ( li->li_idassert_mode == LDAP_BACK_IDASSERT_LEGACY ) {
1569                 if ( op->o_proxy_authz ) {
1570                         /*
1571                          * FIXME: we do not want to perform proxyAuthz
1572                          * on behalf of the client, because this would
1573                          * be performed with "proxyauthzdn" privileges.
1574                          *
1575                          * This might actually be too strict, since
1576                          * the "proxyauthzdn" authzTo, and each entry's
1577                          * authzFrom attributes may be crafted
1578                          * to avoid unwanted proxyAuthz to take place.
1579                          */
1580 #if 0
1581                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1582                         rs->sr_text = "proxyAuthz not allowed within namingContext";
1583 #endif
1584                         goto done;
1585                 }
1586
1587                 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
1588                         goto done;
1589                 }
1590
1591                 if ( BER_BVISNULL( &ndn ) ) {
1592                         goto done;
1593                 }
1594
1595                 if ( BER_BVISNULL( &li->li_idassert_authcDN ) ) {
1596                         goto done;
1597                 }
1598
1599         } else if ( li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
1600                 if ( ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ )
1601                                 /* && ( !BER_BVISNULL( &ndn )
1602                                         || LDAP_BACK_CONN_ISBOUND( lc ) ) */ )
1603                 {
1604                         /* already asserted in SASL via native authz */
1605                         /* NOTE: the test on lc->lc_bound is used to trap
1606                          * native authorization of anonymous users,
1607                          * since in that case ndn is NULL */
1608                         goto done;
1609                 }
1610
1611         } else if ( li->li_idassert_authz && !be_isroot( op ) ) {
1612                 int             rc;
1613                 struct berval authcDN;
1614
1615                 if ( BER_BVISNULL( &ndn ) ) {
1616                         authcDN = slap_empty_bv;
1617                 } else {
1618                         authcDN = ndn;
1619                 }
1620                 rc = slap_sasl_matches( op, li->li_idassert_authz,
1621                                 &authcDN, & authcDN );
1622                 if ( rc != LDAP_SUCCESS ) {
1623                         if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE )
1624                         {
1625                                 /* ndn is not authorized
1626                                  * to use idassert */
1627                                 rs->sr_err = rc;
1628                         }
1629                         goto done;
1630                 }
1631         }
1632
1633         if ( op->o_proxy_authz ) {
1634                 /*
1635                  * FIXME: we can:
1636                  * 1) ignore the already set proxyAuthz control
1637                  * 2) leave it in place, and don't set ours
1638                  * 3) add both
1639                  * 4) reject the operation
1640                  *
1641                  * option (4) is very drastic
1642                  * option (3) will make the remote server reject
1643                  * the operation, thus being equivalent to (4)
1644                  * option (2) will likely break the idassert
1645                  * assumptions, so we cannot accept it;
1646                  * option (1) means that we are contradicting
1647                  * the client's reques.
1648                  *
1649                  * I think (4) is the only correct choice.
1650                  */
1651                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1652                 rs->sr_text = "proxyAuthz not allowed within namingContext";
1653         }
1654
1655         if ( op->o_is_auth_check ) {
1656                 mode = LDAP_BACK_IDASSERT_NOASSERT;
1657
1658         } else {
1659                 mode = li->li_idassert_mode;
1660         }
1661
1662         switch ( mode ) {
1663         case LDAP_BACK_IDASSERT_SELF:
1664                 if ( BER_BVISNULL( &ndn ) ) {
1665                         goto done;
1666                 }
1667                 assertedID = ndn;
1668                 break;
1669
1670         case LDAP_BACK_IDASSERT_LEGACY:
1671                 /* original behavior:
1672                  * assert the client's identity */
1673                 if ( BER_BVISNULL( &ndn ) ) {
1674                         assertedID = slap_empty_bv;
1675                 } else {
1676                         assertedID = ndn;
1677                 }
1678                 break;
1679
1680         case LDAP_BACK_IDASSERT_ANONYMOUS:
1681                 /* assert "anonymous" */
1682                 assertedID = slap_empty_bv;
1683                 break;
1684
1685         case LDAP_BACK_IDASSERT_NOASSERT:
1686                 /* don't assert; bind as proxyauthzdn */
1687                 goto done;
1688
1689         case LDAP_BACK_IDASSERT_OTHERID:
1690         case LDAP_BACK_IDASSERT_OTHERDN:
1691                 /* assert idassert DN */
1692                 assertedID = li->li_idassert_authzID;
1693                 break;
1694
1695         default:
1696                 assert( 0 );
1697         }
1698
1699         if ( BER_BVISNULL( &assertedID ) ) {
1700                 assertedID = slap_empty_bv;
1701         }
1702
1703         /* don't idassert the bound DN (ITS#4497) */
1704         if ( dn_match( &assertedID, &lc->lc_bound_ndn ) ) {
1705                 goto done;
1706         }
1707
1708         if ( op->o_ctrls ) {
1709                 for ( i = 0; op->o_ctrls[ i ]; i++ )
1710                         /* just count ctrls */ ;
1711         }
1712
1713         ctrls = op->o_tmpalloc( sizeof( LDAPControl * ) * (i + 2) + sizeof( LDAPControl ),
1714                         op->o_tmpmemctx );
1715         ctrls[ 0 ] = (LDAPControl *)&ctrls[ i + 2 ];
1716         
1717         ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1718         ctrls[ 0 ]->ldctl_iscritical = 1;
1719
1720         switch ( li->li_idassert_mode ) {
1721         /* already in u:ID or dn:DN form */
1722         case LDAP_BACK_IDASSERT_OTHERID:
1723         case LDAP_BACK_IDASSERT_OTHERDN:
1724                 ber_dupbv_x( &ctrls[ 0 ]->ldctl_value, &assertedID, op->o_tmpmemctx );
1725                 break;
1726
1727         /* needs the dn: prefix */
1728         default:
1729                 ctrls[ 0 ]->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
1730                 ctrls[ 0 ]->ldctl_value.bv_val = op->o_tmpalloc( ctrls[ 0 ]->ldctl_value.bv_len + 1,
1731                                 op->o_tmpmemctx );
1732                 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
1733                 AC_MEMCPY( &ctrls[ 0 ]->ldctl_value.bv_val[ STRLENOF( "dn:" ) ],
1734                                 assertedID.bv_val, assertedID.bv_len + 1 );
1735                 break;
1736         }
1737
1738         /* Older versions of <draft-weltman-ldapv3-proxy> required
1739          * to encode the value of the authzID (and called it proxyDN);
1740          * this hack provides compatibility with those DSAs that
1741          * implement it this way */
1742         if ( li->li_idassert_flags & LDAP_BACK_AUTH_OBSOLETE_ENCODING_WORKAROUND ) {
1743                 struct berval           authzID = ctrls[ 0 ]->ldctl_value;
1744                 BerElementBuffer        berbuf;
1745                 BerElement              *ber = (BerElement *)&berbuf;
1746                 ber_tag_t               tag;
1747
1748                 ber_init2( ber, 0, LBER_USE_DER );
1749                 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1750
1751                 tag = ber_printf( ber, "O", &authzID );
1752                 if ( tag == LBER_ERROR ) {
1753                         rs->sr_err = LDAP_OTHER;
1754                         goto free_ber;
1755                 }
1756
1757                 if ( ber_flatten2( ber, &ctrls[ 0 ]->ldctl_value, 1 ) == -1 ) {
1758                         rs->sr_err = LDAP_OTHER;
1759                         goto free_ber;
1760                 }
1761
1762 free_ber:;
1763                 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
1764                 ber_free_buf( ber );
1765
1766                 if ( rs->sr_err != LDAP_SUCCESS ) {
1767                         op->o_tmpfree( ctrls, op->o_tmpmemctx );
1768                         ctrls = NULL;
1769                         goto done;
1770                 }
1771
1772         } else if ( li->li_idassert_flags & LDAP_BACK_AUTH_OBSOLETE_PROXY_AUTHZ ) {
1773                 struct berval           authzID = ctrls[ 0 ]->ldctl_value,
1774                                         tmp;
1775                 BerElementBuffer        berbuf;
1776                 BerElement              *ber = (BerElement *)&berbuf;
1777                 ber_tag_t               tag;
1778
1779                 if ( strncasecmp( authzID.bv_val, "dn:", STRLENOF( "dn:" ) ) != 0 ) {
1780                         op->o_tmpfree( ctrls[ 0 ]->ldctl_value.bv_val, op->o_tmpmemctx );
1781                         op->o_tmpfree( ctrls, op->o_tmpmemctx );
1782                         ctrls = NULL;
1783                         rs->sr_err = LDAP_PROTOCOL_ERROR;
1784                         goto done;
1785                 }
1786
1787                 tmp = authzID;
1788                 tmp.bv_val += STRLENOF( "dn:" );
1789                 tmp.bv_len -= STRLENOF( "dn:" );
1790
1791                 ber_init2( ber, 0, LBER_USE_DER );
1792                 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1793
1794                 /* apparently, Mozilla API encodes this
1795                  * as "SEQUENCE { LDAPDN }" */
1796                 tag = ber_printf( ber, "{O}", &tmp );
1797                 if ( tag == LBER_ERROR ) {
1798                         rs->sr_err = LDAP_OTHER;
1799                         goto free_ber2;
1800                 }
1801
1802                 if ( ber_flatten2( ber, &ctrls[ 0 ]->ldctl_value, 1 ) == -1 ) {
1803                         rs->sr_err = LDAP_OTHER;
1804                         goto free_ber2;
1805                 }
1806
1807 free_ber2:;
1808                 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
1809                 ber_free_buf( ber );
1810
1811                 if ( rs->sr_err != LDAP_SUCCESS ) {
1812                         op->o_tmpfree( ctrls, op->o_tmpmemctx );
1813                         ctrls = NULL;
1814                         goto done;
1815                 }
1816
1817                 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ;
1818         }
1819
1820         if ( op->o_ctrls ) {
1821                 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
1822                         ctrls[ i + 1 ] = op->o_ctrls[ i ];
1823                 }
1824         }
1825         ctrls[ i + 1 ] = NULL;
1826
1827 done:;
1828         if ( ctrls == NULL ) {
1829                 ctrls = op->o_ctrls;
1830         }
1831
1832         *pctrls = ctrls;
1833         
1834         return rs->sr_err;
1835 }
1836
1837 int
1838 ldap_back_proxy_authz_ctrl_free( Operation *op, LDAPControl ***pctrls )
1839 {
1840         LDAPControl     **ctrls = *pctrls;
1841
1842         /* we assume that the first control is the proxyAuthz
1843          * added by back-ldap, so it's the only one we explicitly 
1844          * free */
1845         if ( ctrls && ctrls != op->o_ctrls ) {
1846                 assert( ctrls[ 0 ] != NULL );
1847
1848                 if ( !BER_BVISNULL( &ctrls[ 0 ]->ldctl_value ) ) {
1849                         op->o_tmpfree( ctrls[ 0 ]->ldctl_value.bv_val, op->o_tmpmemctx );
1850                 }
1851
1852                 op->o_tmpfree( ctrls, op->o_tmpmemctx );
1853         } 
1854
1855         *pctrls = NULL;
1856
1857         return 0;
1858 }