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