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