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