]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/bind.c
2b0d39bd2557912e64a3553025987cc3c4ee5e91
[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 #ifdef HAVE_TLS
742 done:;
743 #endif /* HAVE_TLS */
744         if ( li->li_idle_timeout && lc ) {
745                 lc->lc_time = op->o_time;
746         }
747
748         return lc;
749 }
750
751 void
752 ldap_back_release_conn_lock(
753         Operation               *op,
754         SlapReply               *rs,
755         ldapconn_t              *lc,
756         int                     dolock )
757 {
758         ldapinfo_t      *li = (ldapinfo_t *)op->o_bd->be_private;
759
760         if ( dolock ) {
761                 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
762         }
763         assert( lc->lc_refcnt > 0 );
764         LDAP_BACK_CONN_BINDING_CLEAR( lc );
765         lc->lc_refcnt--;
766         if ( LDAP_BACK_CONN_TAINTED( lc ) ) {
767                 ldap_back_freeconn( op, lc, 0 );
768         }
769         if ( dolock ) {
770                 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
771         }
772 }
773
774 /*
775  * ldap_back_dobind
776  *
777  * Note: as the check for the value of lc->lc_bound was already here, I removed
778  * it from all the callers, and I made the function return the flag, so
779  * it can be used to simplify the check.
780  *
781  * Note: dolock indicates whether li->li_conninfo.lai_mutex must be locked or not
782  */
783 static int
784 ldap_back_dobind_int(
785         ldapconn_t              *lc,
786         Operation               *op,
787         SlapReply               *rs,
788         ldap_back_send_t        sendok,
789         int                     retries,
790         int                     dolock )
791 {       
792         ldapinfo_t      *li = (ldapinfo_t *)op->o_bd->be_private;
793
794         int             rc, binding = 0;
795         ber_int_t       msgid;
796
797         assert( retries >= 0 );
798
799 retry_lock:;
800         if ( dolock ) {
801                 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
802         }
803
804         if ( binding == 0 ) {
805                 /* check if already bound */
806                 rc = LDAP_BACK_CONN_ISBOUND( lc );
807                 if ( rc ) {
808                         lc->lc_binding--;
809                         if ( dolock ) {
810                                 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
811                         }
812                         return rc;
813                 }
814
815                 if ( LDAP_BACK_CONN_BINDING( lc ) ) {
816                         /* if someone else is about to bind it, give up and retry */
817                         if ( dolock ) {
818                                 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
819                         }
820                         ldap_pvt_thread_yield();
821                         goto retry_lock;
822
823                 } else {
824                         /* otherwise this thread will bind it */
825                         LDAP_BACK_CONN_BINDING_SET( lc );
826                         binding = 1;
827                 }
828         }
829
830         /* wait for pending operations to finish */
831         /* FIXME: may become a bottleneck! */
832         if ( lc->lc_refcnt != lc->lc_binding ) {
833                 if ( dolock ) {
834                         ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
835                 }
836                 ldap_pvt_thread_yield();
837                 goto retry_lock;
838         }
839
840         if ( dolock ) {
841                 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
842         }
843
844 #if 0
845         while ( lc->lc_refcnt > 1 ) {
846                 ldap_pvt_thread_yield();
847                 rc = LDAP_BACK_CONN_ISBOUND( lc );
848                 if ( rc ) {
849                         return rc;
850                 }
851         }
852
853         if ( dolock ) {
854                 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
855         }
856         LDAP_BACK_CONN_BINDING_SET( lc );
857         if ( dolock ) {
858                 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
859         }
860 #endif
861
862         /*
863          * FIXME: we need to let clients use proxyAuthz
864          * otherwise we cannot do symmetric pools of servers;
865          * we have to live with the fact that a user can
866          * authorize itself as any ID that is allowed
867          * by the authzTo directive of the "proxyauthzdn".
868          */
869         /*
870          * NOTE: current Proxy Authorization specification
871          * and implementation do not allow proxy authorization
872          * control to be provided with Bind requests
873          */
874         /*
875          * if no bind took place yet, but the connection is bound
876          * and the "idassert-authcDN" (or other ID) is set, 
877          * then bind as the asserting identity and explicitly 
878          * add the proxyAuthz control to every operation with the
879          * dn bound to the connection as control value.
880          * This is done also if this is the authrizing backend,
881          * but the "override" flag is given to idassert.
882          * It allows to use SASL bind and yet proxyAuthz users
883          */
884         if ( op->o_conn != NULL &&
885                         !op->o_do_not_cache &&
886                         ( BER_BVISNULL( &lc->lc_bound_ndn ) ||
887                           ( li->li_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) )
888         {
889                 (void)ldap_back_proxy_authz_bind( lc, op, rs, sendok );
890                 goto done;
891         }
892
893 #ifdef HAVE_CYRUS_SASL
894         if ( LDAP_BACK_CONN_ISPRIV( lc )
895                 && li->li_acl_authmethod == LDAP_AUTH_SASL )
896         {
897                 void            *defaults = NULL;
898
899                 if ( li->li_acl_secprops != NULL ) {
900                         rc = ldap_set_option( lc->lc_ld,
901                                 LDAP_OPT_X_SASL_SECPROPS, li->li_acl_secprops);
902
903                         if ( rc != LDAP_OPT_SUCCESS ) {
904                                 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
905                                         "(SECPROPS,\"%s\") failed!\n",
906                                         li->li_acl_secprops, 0, 0 );
907                                 goto done;
908                         }
909                 }
910
911                 defaults = lutil_sasl_defaults( lc->lc_ld,
912                                 li->li_acl_sasl_mech.bv_val,
913                                 li->li_acl_sasl_realm.bv_val,
914                                 li->li_acl_authcID.bv_val,
915                                 li->li_acl_passwd.bv_val,
916                                 NULL );
917
918                 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld,
919                                 li->li_acl_authcDN.bv_val,
920                                 li->li_acl_sasl_mech.bv_val, NULL, NULL,
921                                 LDAP_SASL_QUIET, lutil_sasl_interact,
922                                 defaults );
923
924                 lutil_sasl_freedefs( defaults );
925
926                 rs->sr_err = slap_map_api2result( rs );
927                 if ( rs->sr_err != LDAP_SUCCESS ) {
928                         LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
929                         send_ldap_result( op, rs );
930
931                 } else {
932                         LDAP_BACK_CONN_ISBOUND_SET( lc );
933                 }
934                 goto done;
935         }
936 #endif /* HAVE_CYRUS_SASL */
937
938 retry:;
939         rs->sr_err = ldap_sasl_bind( lc->lc_ld,
940                         lc->lc_bound_ndn.bv_val,
941                         LDAP_SASL_SIMPLE, &lc->lc_cred,
942                         NULL, NULL, &msgid );
943
944         if ( rs->sr_err == LDAP_SERVER_DOWN ) {
945                 if ( retries != LDAP_BACK_RETRY_NEVER ) {
946                         if ( dolock ) {
947                                 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
948                         }
949
950                         assert( lc->lc_refcnt > 0 );
951                         if ( lc->lc_refcnt == 1 ) {
952                                 ldap_unbind_ext( lc->lc_ld, NULL, NULL );
953                                 lc->lc_ld = NULL;
954
955                                 /* lc here must be the regular lc, reset and ready for init */
956                                 rs->sr_err = ldap_back_prepare_conn( &lc, op, rs, sendok );
957                                 if ( rs->sr_err != LDAP_SUCCESS ) {
958                                         lc->lc_binding--;
959                                         lc->lc_refcnt = 0;
960                                 }
961                         }
962
963                         if ( dolock ) {
964                                 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
965                         }
966
967                         if ( rs->sr_err == LDAP_SUCCESS ) {
968                                 if ( retries > 0 ) {
969                                         retries--;
970                                 }
971                                 goto retry;
972                         }
973
974                 } else {
975                         if ( dolock ) {
976                                 ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
977                         }
978                         lc->lc_binding--;
979                         if ( dolock ) {
980                                 ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
981                         }
982                 }
983
984                 ldap_back_freeconn( op, lc, dolock );
985                 rs->sr_err = slap_map_api2result( rs );
986
987                 return 0;
988         }
989
990         rc = ldap_back_op_result( lc, op, rs, msgid, 0, sendok );
991         if ( rc == LDAP_SUCCESS ) {
992                 LDAP_BACK_CONN_ISBOUND_SET( lc );
993         }
994
995 done:;
996         lc->lc_binding--;
997         LDAP_BACK_CONN_BINDING_CLEAR( lc );
998         rc = LDAP_BACK_CONN_ISBOUND( lc );
999         if ( !rc ) {
1000                 ldap_back_release_conn_lock( op, rs, lc, dolock );
1001         }
1002
1003         return rc;
1004 }
1005
1006 int
1007 ldap_back_dobind( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1008 {
1009         ldapinfo_t      *li = (ldapinfo_t *)op->o_bd->be_private;
1010
1011         return ldap_back_dobind_int( lc, op, rs, sendok, li->li_nretries, 1 );
1012 }
1013
1014 /*
1015  * ldap_back_default_rebind
1016  *
1017  * This is a callback used for chasing referrals using the same
1018  * credentials as the original user on this session.
1019  */
1020 int 
1021 ldap_back_default_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
1022         ber_int_t msgid, void *params )
1023 {
1024         ldapconn_t      *lc = (ldapconn_t *)params;
1025
1026 #ifdef HAVE_TLS
1027         /* ... otherwise we couldn't get here */
1028         assert( lc != NULL );
1029
1030         if ( !ldap_tls_inplace( ld ) ) {
1031                 int             is_tls = LDAP_BACK_CONN_ISTLS( lc ),
1032                                 rc;
1033                 const char      *text = NULL;
1034
1035                 rc = ldap_back_start_tls( ld, 0, &is_tls, url, lc->lc_flags,
1036                         LDAP_BACK_RETRY_DEFAULT, &text );
1037                 if ( rc != LDAP_SUCCESS ) {
1038                         return rc;
1039                 }
1040         }
1041 #endif /* HAVE_TLS */
1042
1043         /* FIXME: add checks on the URL/identity? */
1044
1045         return ldap_sasl_bind_s( ld, lc->lc_bound_ndn.bv_val,
1046                         LDAP_SASL_SIMPLE, &lc->lc_cred, NULL, NULL, NULL );
1047 }
1048
1049 /*
1050  * ldap_back_default_urllist
1051  */
1052 int 
1053 ldap_back_default_urllist(
1054         LDAP            *ld,
1055         LDAPURLDesc     **urllist,
1056         LDAPURLDesc     **url,
1057         void            *params )
1058 {
1059         ldapinfo_t      *li = (ldapinfo_t *)params;
1060         LDAPURLDesc     **urltail;
1061
1062         if ( urllist == url ) {
1063                 return LDAP_SUCCESS;
1064         }
1065
1066         for ( urltail = &(*url)->lud_next; *urltail; urltail = &(*urltail)->lud_next )
1067                 /* count */ ;
1068
1069         *urltail = *urllist;
1070         *urllist = *url;
1071         *url = NULL;
1072
1073         ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
1074         if ( li->li_uri ) {
1075                 ch_free( li->li_uri );
1076         }
1077
1078         ldap_get_option( ld, LDAP_OPT_URI, (void *)&li->li_uri );
1079         ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
1080
1081         return LDAP_SUCCESS;
1082 }
1083
1084 int
1085 ldap_back_op_result(
1086                 ldapconn_t              *lc,
1087                 Operation               *op,
1088                 SlapReply               *rs,
1089                 ber_int_t               msgid,
1090                 time_t                  timeout,
1091                 ldap_back_send_t        sendok )
1092 {
1093         char            *match = NULL;
1094         LDAPMessage     *res = NULL;
1095         char            *text = NULL;
1096
1097 #define ERR_OK(err) ((err) == LDAP_SUCCESS || (err) == LDAP_COMPARE_FALSE || (err) == LDAP_COMPARE_TRUE)
1098
1099         rs->sr_text = NULL;
1100         rs->sr_matched = NULL;
1101
1102         /* if the error recorded in the reply corresponds
1103          * to a successful state, get the error from the
1104          * remote server response */
1105         if ( ERR_OK( rs->sr_err ) ) {
1106                 int             rc;
1107                 struct timeval  tv;
1108
1109                 if ( timeout ) {
1110                         tv.tv_sec = timeout;
1111                         tv.tv_usec = 0;
1112
1113                 } else {
1114                         LDAP_BACK_TV_SET( &tv );
1115                 }
1116
1117 retry:;
1118                 /* if result parsing fails, note the failure reason */
1119                 rc = ldap_result( lc->lc_ld, msgid, LDAP_MSG_ALL, &tv, &res );
1120                 switch ( rc ) {
1121                 case 0:
1122                         if ( timeout ) {
1123                                 (void)ldap_abandon_ext( lc->lc_ld, msgid, NULL, NULL );
1124                                 rs->sr_err = op->o_protocol >= LDAP_VERSION3 ?
1125                                         LDAP_ADMINLIMIT_EXCEEDED : LDAP_OPERATIONS_ERROR;
1126                                 rs->sr_text = "Operation timed out";
1127                                 break;
1128                         }
1129
1130                         LDAP_BACK_TV_SET( &tv );
1131                         ldap_pvt_thread_yield();
1132                         goto retry;
1133
1134                 case -1:
1135                         ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
1136                                         &rs->sr_err );
1137                         break;
1138
1139
1140                 /* otherwise get the result; if it is not
1141                  * LDAP_SUCCESS, record it in the reply
1142                  * structure (this includes 
1143                  * LDAP_COMPARE_{TRUE|FALSE}) */
1144                 default:
1145                         rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
1146                                         &match, &text, NULL, NULL, 1 );
1147                         rs->sr_text = text;
1148                         if ( rc != LDAP_SUCCESS ) {
1149                                 rs->sr_err = rc;
1150                         }
1151                 }
1152         }
1153
1154         /* if the error in the reply structure is not
1155          * LDAP_SUCCESS, try to map it from client 
1156          * to server error */
1157         if ( !ERR_OK( rs->sr_err ) ) {
1158                 rs->sr_err = slap_map_api2result( rs );
1159
1160                 /* internal ops ( op->o_conn == NULL ) 
1161                  * must not reply to client */
1162                 if ( op->o_conn && !op->o_do_not_cache && match ) {
1163
1164                         /* record the (massaged) matched
1165                          * DN into the reply structure */
1166                         rs->sr_matched = match;
1167                 }
1168         }
1169         if ( op->o_conn &&
1170                         ( ( sendok & LDAP_BACK_SENDOK ) 
1171                           || ( ( sendok & LDAP_BACK_SENDERR ) && rs->sr_err != LDAP_SUCCESS ) ) )
1172         {
1173                 send_ldap_result( op, rs );
1174         }
1175         if ( match ) {
1176                 if ( rs->sr_matched != match ) {
1177                         free( (char *)rs->sr_matched );
1178                 }
1179                 rs->sr_matched = NULL;
1180                 ldap_memfree( match );
1181         }
1182         if ( text ) {
1183                 ldap_memfree( text );
1184         }
1185         rs->sr_text = NULL;
1186         return( ERR_OK( rs->sr_err ) ? LDAP_SUCCESS : rs->sr_err );
1187 }
1188
1189 /* return true if bound, false if failed */
1190 int
1191 ldap_back_retry( ldapconn_t **lcp, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1192 {
1193         int             rc = 0;
1194         ldapinfo_t      *li = (ldapinfo_t *)op->o_bd->be_private;
1195
1196         assert( lcp != NULL );
1197         assert( *lcp != NULL );
1198
1199         ldap_pvt_thread_mutex_lock( &li->li_conninfo.lai_mutex );
1200
1201         if ( (*lcp)->lc_refcnt == 1 ) {
1202                 ldap_pvt_thread_mutex_lock( &li->li_uri_mutex );
1203                 Debug( LDAP_DEBUG_ANY,
1204                         "%s ldap_back_retry: retrying URI=\"%s\" DN=\"%s\"\n",
1205                         op->o_log_prefix, li->li_uri,
1206                         BER_BVISNULL( &(*lcp)->lc_bound_ndn ) ?
1207                                 "" : (*lcp)->lc_bound_ndn.bv_val );
1208                 ldap_pvt_thread_mutex_unlock( &li->li_uri_mutex );
1209
1210                 ldap_unbind_ext( (*lcp)->lc_ld, NULL, NULL );
1211                 (*lcp)->lc_ld = NULL;
1212                 LDAP_BACK_CONN_ISBOUND_CLEAR( (*lcp) );
1213
1214                 /* lc here must be the regular lc, reset and ready for init */
1215                 rc = ldap_back_prepare_conn( lcp, op, rs, sendok );
1216                 if ( rc != LDAP_SUCCESS ) {
1217                         rc = 0;
1218                         *lcp = NULL;
1219
1220                 } else {
1221                         rc = ldap_back_dobind_int( *lcp, op, rs, sendok, 0, 0 );
1222                         if ( rc == 0 ) {
1223                                 *lcp = NULL;
1224                         }
1225                 }
1226
1227         } else {
1228                 Debug( LDAP_DEBUG_TRACE,
1229                         "ldap_back_retry: conn %p refcnt=%u unable to retry.\n",
1230                         (void *)(*lcp), (*lcp)->lc_refcnt, 0 );
1231
1232                 ldap_back_release_conn_lock( op, rs, *lcp, 0 );
1233                 *lcp = NULL;
1234
1235                 if ( sendok ) {
1236                         rs->sr_err = LDAP_UNAVAILABLE;
1237                         rs->sr_text = "unable to retry";
1238                         send_ldap_result( op, rs );
1239                 }
1240         }
1241
1242         ldap_pvt_thread_mutex_unlock( &li->li_conninfo.lai_mutex );
1243
1244         return rc;
1245 }
1246
1247 static int
1248 ldap_back_proxy_authz_bind( ldapconn_t *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
1249 {
1250         ldapinfo_t      *li = (ldapinfo_t *)op->o_bd->be_private;
1251         struct berval   binddn = slap_empty_bv;
1252         struct berval   bindcred = slap_empty_bv;
1253         struct berval   ndn;
1254         int             dobind = 0;
1255         int             msgid;
1256         int             rc;
1257
1258         /* don't proxyAuthz if protocol is not LDAPv3 */
1259         switch ( li->li_version ) {
1260         case LDAP_VERSION3:
1261                 break;
1262
1263         case 0:
1264                 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
1265                         break;
1266                 }
1267                 /* fall thru */
1268
1269         default:
1270                 goto done;
1271         }
1272
1273         if ( op->o_tag == LDAP_REQ_BIND ) {
1274                 ndn = op->o_req_ndn;
1275
1276         } else if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1277                 ndn = op->o_conn->c_ndn;
1278
1279         } else {
1280                 ndn = op->o_ndn;
1281         }
1282
1283         /*
1284          * FIXME: we need to let clients use proxyAuthz
1285          * otherwise we cannot do symmetric pools of servers;
1286          * we have to live with the fact that a user can
1287          * authorize itself as any ID that is allowed
1288          * by the authzTo directive of the "proxyauthzdn".
1289          */
1290         /*
1291          * NOTE: current Proxy Authorization specification
1292          * and implementation do not allow proxy authorization
1293          * control to be provided with Bind requests
1294          */
1295         /*
1296          * if no bind took place yet, but the connection is bound
1297          * and the "proxyauthzdn" is set, then bind as 
1298          * "proxyauthzdn" and explicitly add the proxyAuthz 
1299          * control to every operation with the dn bound 
1300          * to the connection as control value.
1301          */
1302
1303         /* bind as proxyauthzdn only if no idassert mode
1304          * is requested, or if the client's identity
1305          * is authorized */
1306         switch ( li->li_idassert_mode ) {
1307         case LDAP_BACK_IDASSERT_LEGACY:
1308                 if ( !BER_BVISNULL( &ndn ) && !BER_BVISEMPTY( &ndn ) ) {
1309                         if ( !BER_BVISNULL( &li->li_idassert_authcDN ) && !BER_BVISEMPTY( &li->li_idassert_authcDN ) )
1310                         {
1311                                 binddn = li->li_idassert_authcDN;
1312                                 bindcred = li->li_idassert_passwd;
1313                                 dobind = 1;
1314                         }
1315                 }
1316                 break;
1317
1318         default:
1319                 /* NOTE: rootdn can always idassert */
1320                 if ( BER_BVISNULL( &ndn ) && li->li_idassert_authz == NULL ) {
1321                         if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1322                                 rs->sr_err = LDAP_INAPPROPRIATE_AUTH;
1323                                 if ( sendok & LDAP_BACK_SENDERR ) {
1324                                         send_ldap_result( op, rs );
1325                                 }
1326                                 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1327
1328                         } else {
1329                                 rs->sr_err = LDAP_SUCCESS;
1330                                 binddn = slap_empty_bv;
1331                                 bindcred = slap_empty_bv;
1332                                 break;
1333                         }
1334
1335                         goto done;
1336
1337                 } else if ( li->li_idassert_authz && !be_isroot( op ) ) {
1338                         struct berval authcDN;
1339
1340                         if ( BER_BVISNULL( &ndn ) ) {
1341                                 authcDN = slap_empty_bv;
1342
1343                         } else {
1344                                 authcDN = ndn;
1345                         }       
1346                         rs->sr_err = slap_sasl_matches( op, li->li_idassert_authz,
1347                                         &authcDN, &authcDN );
1348                         if ( rs->sr_err != LDAP_SUCCESS ) {
1349                                 if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE ) {
1350                                         if ( sendok & LDAP_BACK_SENDERR ) {
1351                                                 send_ldap_result( op, rs );
1352                                         }
1353                                         LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1354
1355                                 } else {
1356                                         rs->sr_err = LDAP_SUCCESS;
1357                                         binddn = slap_empty_bv;
1358                                         bindcred = slap_empty_bv;
1359                                         break;
1360                                 }
1361
1362                                 goto done;
1363                         }
1364                 }
1365
1366                 binddn = li->li_idassert_authcDN;
1367                 bindcred = li->li_idassert_passwd;
1368                 dobind = 1;
1369                 break;
1370         }
1371
1372         if ( dobind && li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
1373 #ifdef HAVE_CYRUS_SASL
1374                 void            *defaults = NULL;
1375                 struct berval   authzID = BER_BVNULL;
1376                 int             freeauthz = 0;
1377
1378                 /* if SASL supports native authz, prepare for it */
1379                 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
1380                                 ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
1381                 {
1382                         switch ( li->li_idassert_mode ) {
1383                         case LDAP_BACK_IDASSERT_OTHERID:
1384                         case LDAP_BACK_IDASSERT_OTHERDN:
1385                                 authzID = li->li_idassert_authzID;
1386                                 break;
1387
1388                         case LDAP_BACK_IDASSERT_ANONYMOUS:
1389                                 BER_BVSTR( &authzID, "dn:" );
1390                                 break;
1391
1392                         case LDAP_BACK_IDASSERT_SELF:
1393                                 if ( BER_BVISNULL( &ndn ) ) {
1394                                         /* connection is not authc'd, so don't idassert */
1395                                         BER_BVSTR( &authzID, "dn:" );
1396                                         break;
1397                                 }
1398                                 authzID.bv_len = STRLENOF( "dn:" ) + ndn.bv_len;
1399                                 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
1400                                 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
1401                                 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
1402                                                 ndn.bv_val, ndn.bv_len + 1 );
1403                                 freeauthz = 1;
1404                                 break;
1405
1406                         default:
1407                                 break;
1408                         }
1409                 }
1410
1411                 if ( li->li_idassert_secprops != NULL ) {
1412                         rs->sr_err = ldap_set_option( lc->lc_ld,
1413                                 LDAP_OPT_X_SASL_SECPROPS,
1414                                 (void *)li->li_idassert_secprops );
1415
1416                         if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
1417                                 rs->sr_err = LDAP_OTHER;
1418                                 if ( sendok & LDAP_BACK_SENDERR ) {
1419                                         send_ldap_result( op, rs );
1420                                 }
1421                                 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1422                                 goto done;
1423                         }
1424                 }
1425
1426                 defaults = lutil_sasl_defaults( lc->lc_ld,
1427                                 li->li_idassert_sasl_mech.bv_val,
1428                                 li->li_idassert_sasl_realm.bv_val,
1429                                 li->li_idassert_authcID.bv_val,
1430                                 li->li_idassert_passwd.bv_val,
1431                                 authzID.bv_val );
1432
1433                 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld, binddn.bv_val,
1434                                 li->li_idassert_sasl_mech.bv_val, NULL, NULL,
1435                                 LDAP_SASL_QUIET, lutil_sasl_interact,
1436                                 defaults );
1437
1438                 rs->sr_err = slap_map_api2result( rs );
1439                 if ( rs->sr_err != LDAP_SUCCESS ) {
1440                         LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1441                         if ( sendok & LDAP_BACK_SENDERR ) {
1442                                 send_ldap_result( op, rs );
1443                         }
1444
1445                 } else {
1446                         LDAP_BACK_CONN_ISBOUND_SET( lc );
1447                 }
1448
1449                 lutil_sasl_freedefs( defaults );
1450                 if ( freeauthz ) {
1451                         slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
1452                 }
1453
1454                 goto done;
1455 #endif /* HAVE_CYRUS_SASL */
1456         }
1457
1458         switch ( li->li_idassert_authmethod ) {
1459         case LDAP_AUTH_NONE:
1460                 LDAP_BACK_CONN_ISBOUND_SET( lc );
1461                 goto done;
1462
1463         case LDAP_AUTH_SIMPLE:
1464                 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
1465                                 binddn.bv_val, LDAP_SASL_SIMPLE,
1466                                 &bindcred, NULL, NULL, &msgid );
1467                 break;
1468
1469         default:
1470                 /* unsupported! */
1471                 LDAP_BACK_CONN_ISBOUND_CLEAR( lc );
1472                 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
1473                 if ( sendok & LDAP_BACK_SENDERR ) {
1474                         send_ldap_result( op, rs );
1475                 }
1476                 goto done;
1477         }
1478
1479         rc = ldap_back_op_result( lc, op, rs, msgid, 0, sendok );
1480         if ( rc == LDAP_SUCCESS ) {
1481                 LDAP_BACK_CONN_ISBOUND_SET( lc );
1482         }
1483 done:;
1484         return LDAP_BACK_CONN_ISBOUND( lc );
1485 }
1486
1487 /*
1488  * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
1489  * to existing server-side controls if required; if not,
1490  * the existing server-side controls are placed in *pctrls.
1491  * The caller, after using the controls in client API 
1492  * operations, if ( *pctrls != op->o_ctrls ), should
1493  * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
1494  * The function returns success if the control could
1495  * be added if required, or if it did nothing; in the future,
1496  * it might return some error if it failed.
1497  * 
1498  * if no bind took place yet, but the connection is bound
1499  * and the "proxyauthzdn" is set, then bind as "proxyauthzdn" 
1500  * and explicitly add proxyAuthz the control to every operation
1501  * with the dn bound to the connection as control value.
1502  *
1503  * If no server-side controls are defined for the operation,
1504  * simply add the proxyAuthz control; otherwise, if the
1505  * proxyAuthz control is not already set, add it as
1506  * the first one
1507  *
1508  * FIXME: is controls order significant for security?
1509  * ANSWER: controls ordering and interoperability
1510  * must be indicated by the specs of each control; if none
1511  * is specified, the order is irrelevant.
1512  */
1513 int
1514 ldap_back_proxy_authz_ctrl(
1515                 ldapconn_t      *lc,
1516                 Operation       *op,
1517                 SlapReply       *rs,
1518                 LDAPControl     ***pctrls )
1519 {
1520         ldapinfo_t      *li = (ldapinfo_t *) op->o_bd->be_private;
1521         LDAPControl     **ctrls = NULL;
1522         int             i = 0,
1523                         mode;
1524         struct berval   assertedID,
1525                         ndn;
1526
1527         *pctrls = NULL;
1528
1529         rs->sr_err = LDAP_SUCCESS;
1530
1531         /* don't proxyAuthz if protocol is not LDAPv3 */
1532         switch ( li->li_version ) {
1533         case LDAP_VERSION3:
1534                 break;
1535
1536         case 0:
1537                 if ( op->o_protocol == 0 || op->o_protocol == LDAP_VERSION3 ) {
1538                         break;
1539                 }
1540                 /* fall thru */
1541
1542         default:
1543                 goto done;
1544         }
1545
1546         /* FIXME: SASL/EXTERNAL over ldapi:// doesn't honor the authcID,
1547          * but if it is not set this test fails.  We need a different
1548          * means to detect if idassert is enabled */
1549         if ( ( BER_BVISNULL( &li->li_idassert_authcID ) || BER_BVISEMPTY( &li->li_idassert_authcID ) )
1550                         && ( BER_BVISNULL( &li->li_idassert_authcDN ) || BER_BVISEMPTY( &li->li_idassert_authcDN ) ) )
1551         {
1552                 goto done;
1553         }
1554
1555         if ( !op->o_conn || op->o_do_not_cache || be_isroot( op ) ) {
1556                 goto done;
1557         }
1558
1559         if ( !BER_BVISNULL( &op->o_conn->c_ndn ) ) {
1560                 ndn = op->o_conn->c_ndn;
1561
1562         } else {
1563                 ndn = op->o_ndn;
1564         }
1565
1566         if ( li->li_idassert_mode == LDAP_BACK_IDASSERT_LEGACY ) {
1567                 if ( op->o_proxy_authz ) {
1568                         /*
1569                          * FIXME: we do not want to perform proxyAuthz
1570                          * on behalf of the client, because this would
1571                          * be performed with "proxyauthzdn" privileges.
1572                          *
1573                          * This might actually be too strict, since
1574                          * the "proxyauthzdn" authzTo, and each entry's
1575                          * authzFrom attributes may be crafted
1576                          * to avoid unwanted proxyAuthz to take place.
1577                          */
1578 #if 0
1579                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1580                         rs->sr_text = "proxyAuthz not allowed within namingContext";
1581 #endif
1582                         goto done;
1583                 }
1584
1585                 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
1586                         goto done;
1587                 }
1588
1589                 if ( BER_BVISNULL( &ndn ) ) {
1590                         goto done;
1591                 }
1592
1593                 if ( BER_BVISNULL( &li->li_idassert_authcDN ) ) {
1594                         goto done;
1595                 }
1596
1597         } else if ( li->li_idassert_authmethod == LDAP_AUTH_SASL ) {
1598                 if ( ( li->li_idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ )
1599                                 /* && ( !BER_BVISNULL( &ndn )
1600                                         || LDAP_BACK_CONN_ISBOUND( lc ) ) */ )
1601                 {
1602                         /* already asserted in SASL via native authz */
1603                         /* NOTE: the test on lc->lc_bound is used to trap
1604                          * native authorization of anonymous users,
1605                          * since in that case ndn is NULL */
1606                         goto done;
1607                 }
1608
1609         } else if ( li->li_idassert_authz && !be_isroot( op ) ) {
1610                 int             rc;
1611                 struct berval authcDN;
1612
1613                 if ( BER_BVISNULL( &ndn ) ) {
1614                         authcDN = slap_empty_bv;
1615                 } else {
1616                         authcDN = ndn;
1617                 }
1618                 rc = slap_sasl_matches( op, li->li_idassert_authz,
1619                                 &authcDN, & authcDN );
1620                 if ( rc != LDAP_SUCCESS ) {
1621                         if ( li->li_idassert_flags & LDAP_BACK_AUTH_PRESCRIPTIVE )
1622                         {
1623                                 /* ndn is not authorized
1624                                  * to use idassert */
1625                                 rs->sr_err = rc;
1626                         }
1627                         goto done;
1628                 }
1629         }
1630
1631         if ( op->o_proxy_authz ) {
1632                 /*
1633                  * FIXME: we can:
1634                  * 1) ignore the already set proxyAuthz control
1635                  * 2) leave it in place, and don't set ours
1636                  * 3) add both
1637                  * 4) reject the operation
1638                  *
1639                  * option (4) is very drastic
1640                  * option (3) will make the remote server reject
1641                  * the operation, thus being equivalent to (4)
1642                  * option (2) will likely break the idassert
1643                  * assumptions, so we cannot accept it;
1644                  * option (1) means that we are contradicting
1645                  * the client's reques.
1646                  *
1647                  * I think (4) is the only correct choice.
1648                  */
1649                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1650                 rs->sr_text = "proxyAuthz not allowed within namingContext";
1651         }
1652
1653         if ( op->o_is_auth_check ) {
1654                 mode = LDAP_BACK_IDASSERT_NOASSERT;
1655
1656         } else {
1657                 mode = li->li_idassert_mode;
1658         }
1659
1660         switch ( mode ) {
1661         case LDAP_BACK_IDASSERT_SELF:
1662                 if ( BER_BVISNULL( &ndn ) ) {
1663                         goto done;
1664                 }
1665                 assertedID = ndn;
1666                 break;
1667
1668         case LDAP_BACK_IDASSERT_LEGACY:
1669                 /* original behavior:
1670                  * assert the client's identity */
1671                 if ( BER_BVISNULL( &ndn ) ) {
1672                         assertedID = slap_empty_bv;
1673                 } else {
1674                         assertedID = ndn;
1675                 }
1676                 break;
1677
1678         case LDAP_BACK_IDASSERT_ANONYMOUS:
1679                 /* assert "anonymous" */
1680                 assertedID = slap_empty_bv;
1681                 break;
1682
1683         case LDAP_BACK_IDASSERT_NOASSERT:
1684                 /* don't assert; bind as proxyauthzdn */
1685                 goto done;
1686
1687         case LDAP_BACK_IDASSERT_OTHERID:
1688         case LDAP_BACK_IDASSERT_OTHERDN:
1689                 /* assert idassert DN */
1690                 assertedID = li->li_idassert_authzID;
1691                 break;
1692
1693         default:
1694                 assert( 0 );
1695         }
1696
1697         if ( BER_BVISNULL( &assertedID ) ) {
1698                 assertedID = slap_empty_bv;
1699         }
1700
1701         /* don't idassert the bound DN (ITS#4497) */
1702         if ( dn_match( &assertedID, &lc->lc_bound_ndn ) ) {
1703                 goto done;
1704         }
1705
1706         if ( op->o_ctrls ) {
1707                 for ( i = 0; op->o_ctrls[ i ]; i++ )
1708                         /* just count ctrls */ ;
1709         }
1710
1711         ctrls = op->o_tmpalloc( sizeof( LDAPControl * ) * (i + 2) + sizeof( LDAPControl ),
1712                         op->o_tmpmemctx );
1713         ctrls[ 0 ] = (LDAPControl *)&ctrls[ i + 2 ];
1714         
1715         ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1716         ctrls[ 0 ]->ldctl_iscritical = 1;
1717
1718         switch ( li->li_idassert_mode ) {
1719         /* already in u:ID or dn:DN form */
1720         case LDAP_BACK_IDASSERT_OTHERID:
1721         case LDAP_BACK_IDASSERT_OTHERDN:
1722                 ber_dupbv_x( &ctrls[ 0 ]->ldctl_value, &assertedID, op->o_tmpmemctx );
1723                 break;
1724
1725         /* needs the dn: prefix */
1726         default:
1727                 ctrls[ 0 ]->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
1728                 ctrls[ 0 ]->ldctl_value.bv_val = op->o_tmpalloc( ctrls[ 0 ]->ldctl_value.bv_len + 1,
1729                                 op->o_tmpmemctx );
1730                 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
1731                 AC_MEMCPY( &ctrls[ 0 ]->ldctl_value.bv_val[ STRLENOF( "dn:" ) ],
1732                                 assertedID.bv_val, assertedID.bv_len + 1 );
1733                 break;
1734         }
1735
1736         /* Older versions of <draft-weltman-ldapv3-proxy> required
1737          * to encode the value of the authzID (and called it proxyDN);
1738          * this hack provides compatibility with those DSAs that
1739          * implement it this way */
1740         if ( li->li_idassert_flags & LDAP_BACK_AUTH_OBSOLETE_ENCODING_WORKAROUND ) {
1741                 struct berval           authzID = ctrls[ 0 ]->ldctl_value;
1742                 BerElementBuffer        berbuf;
1743                 BerElement              *ber = (BerElement *)&berbuf;
1744                 ber_tag_t               tag;
1745
1746                 ber_init2( ber, 0, LBER_USE_DER );
1747                 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1748
1749                 tag = ber_printf( ber, "O", &authzID );
1750                 if ( tag == LBER_ERROR ) {
1751                         rs->sr_err = LDAP_OTHER;
1752                         goto free_ber;
1753                 }
1754
1755                 if ( ber_flatten2( ber, &ctrls[ 0 ]->ldctl_value, 1 ) == -1 ) {
1756                         rs->sr_err = LDAP_OTHER;
1757                         goto free_ber;
1758                 }
1759
1760 free_ber:;
1761                 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
1762                 ber_free_buf( ber );
1763
1764                 if ( rs->sr_err != LDAP_SUCCESS ) {
1765                         op->o_tmpfree( ctrls, op->o_tmpmemctx );
1766                         ctrls = NULL;
1767                         goto done;
1768                 }
1769
1770         } else if ( li->li_idassert_flags & LDAP_BACK_AUTH_OBSOLETE_PROXY_AUTHZ ) {
1771                 struct berval           authzID = ctrls[ 0 ]->ldctl_value,
1772                                         tmp;
1773                 BerElementBuffer        berbuf;
1774                 BerElement              *ber = (BerElement *)&berbuf;
1775                 ber_tag_t               tag;
1776
1777                 if ( strncasecmp( authzID.bv_val, "dn:", STRLENOF( "dn:" ) ) != 0 ) {
1778                         op->o_tmpfree( ctrls[ 0 ]->ldctl_value.bv_val, op->o_tmpmemctx );
1779                         op->o_tmpfree( ctrls, op->o_tmpmemctx );
1780                         ctrls = NULL;
1781                         rs->sr_err = LDAP_PROTOCOL_ERROR;
1782                         goto done;
1783                 }
1784
1785                 tmp = authzID;
1786                 tmp.bv_val += STRLENOF( "dn:" );
1787                 tmp.bv_len -= STRLENOF( "dn:" );
1788
1789                 ber_init2( ber, 0, LBER_USE_DER );
1790                 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1791
1792                 /* apparently, Mozilla API encodes this
1793                  * as "SEQUENCE { LDAPDN }" */
1794                 tag = ber_printf( ber, "{O}", &tmp );
1795                 if ( tag == LBER_ERROR ) {
1796                         rs->sr_err = LDAP_OTHER;
1797                         goto free_ber2;
1798                 }
1799
1800                 if ( ber_flatten2( ber, &ctrls[ 0 ]->ldctl_value, 1 ) == -1 ) {
1801                         rs->sr_err = LDAP_OTHER;
1802                         goto free_ber2;
1803                 }
1804
1805 free_ber2:;
1806                 op->o_tmpfree( authzID.bv_val, op->o_tmpmemctx );
1807                 ber_free_buf( ber );
1808
1809                 if ( rs->sr_err != LDAP_SUCCESS ) {
1810                         op->o_tmpfree( ctrls, op->o_tmpmemctx );
1811                         ctrls = NULL;
1812                         goto done;
1813                 }
1814
1815                 ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_OBSOLETE_PROXY_AUTHZ;
1816         }
1817
1818         if ( op->o_ctrls ) {
1819                 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
1820                         ctrls[ i + 1 ] = op->o_ctrls[ i ];
1821                 }
1822         }
1823         ctrls[ i + 1 ] = NULL;
1824
1825 done:;
1826         if ( ctrls == NULL ) {
1827                 ctrls = op->o_ctrls;
1828         }
1829
1830         *pctrls = ctrls;
1831         
1832         return rs->sr_err;
1833 }
1834
1835 int
1836 ldap_back_proxy_authz_ctrl_free( Operation *op, LDAPControl ***pctrls )
1837 {
1838         LDAPControl     **ctrls = *pctrls;
1839
1840         /* we assume that the first control is the proxyAuthz
1841          * added by back-ldap, so it's the only one we explicitly 
1842          * free */
1843         if ( ctrls && ctrls != op->o_ctrls ) {
1844                 assert( ctrls[ 0 ] != NULL );
1845
1846                 if ( !BER_BVISNULL( &ctrls[ 0 ]->ldctl_value ) ) {
1847                         op->o_tmpfree( ctrls[ 0 ]->ldctl_value.bv_val, op->o_tmpmemctx );
1848                 }
1849
1850                 op->o_tmpfree( ctrls, op->o_tmpmemctx );
1851         } 
1852
1853         *pctrls = NULL;
1854
1855         return 0;
1856 }