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