]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/bind.c
use asynchronous StartTLS
[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 #if 1
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
319                 /*
320                  * use synchronous StartTLS
321                  */
322                 rs->sr_err = ldap_start_tls_s( ld, NULL, NULL );
323 #endif
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;
364         int             is_priv = 0;
365
366         /* Searches for a ldapconn in the avl tree */
367
368         /* Explicit binds must not be shared */
369         if ( op->o_tag == LDAP_REQ_BIND
370                 || ( op->o_conn
371                         && op->o_conn->c_authz_backend
372                         && op->o_bd->be_private == op->o_conn->c_authz_backend->be_private ) )
373         {
374                 lc_curr.lc_conn = op->o_conn;
375
376         } else {
377                 lc_curr.lc_conn = NULL;
378         }
379         
380         /* Internal searches are privileged and shared. So is root. */
381         if ( op->o_do_not_cache || be_isroot( op ) ) {
382                 lc_curr.lc_local_ndn = op->o_bd->be_rootndn;
383                 lc_curr.lc_conn = NULL;
384                 is_priv = 1;
385
386         } else {
387                 lc_curr.lc_local_ndn = op->o_ndn;
388         }
389
390         ldap_pvt_thread_mutex_lock( &li->conn_mutex );
391         lc = (struct ldapconn *)avl_find( li->conntree, 
392                         (caddr_t)&lc_curr, ldap_back_conn_cmp );
393         ldap_pvt_thread_mutex_unlock( &li->conn_mutex );
394
395         /* Looks like we didn't get a bind. Open a new session... */
396         if ( !lc ) {
397                 /* lc here must be NULL */
398                 if ( ldap_back_prepare_conn( &lc, op, rs, sendok ) != LDAP_SUCCESS ) {
399                         return NULL;
400                 }
401
402                 lc->lc_conn = lc_curr.lc_conn;
403                 ber_dupbv( &lc->lc_local_ndn, &lc_curr.lc_local_ndn );
404
405                 ldap_pvt_thread_mutex_init( &lc->lc_mutex );
406
407                 if ( is_priv ) {
408                         ber_dupbv( &lc->lc_cred, &li->acl_passwd );
409                         ber_dupbv( &lc->lc_bound_ndn, &li->acl_authcDN );
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         ldap_pvt_thread_mutex_lock( &lc->lc_mutex );
475         if ( !lc->lc_bound ) {
476                 struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
477
478                 /*
479                  * FIXME: we need to let clients use proxyAuthz
480                  * otherwise we cannot do symmetric pools of servers;
481                  * we have to live with the fact that a user can
482                  * authorize itself as any ID that is allowed
483                  * by the authzTo directive of the "proxyauthzdn".
484                  */
485                 /*
486                  * NOTE: current Proxy Authorization specification
487                  * and implementation do not allow proxy authorization
488                  * control to be provided with Bind requests
489                  */
490                 /*
491                  * if no bind took place yet, but the connection is bound
492                  * and the "idassert-authcDN" (or other ID) is set, 
493                  * then bind as the asserting identity and explicitly 
494                  * add the proxyAuthz control to every operation with the
495                  * dn bound to the connection as control value.
496                  * This is done also if this is the authrizing backend,
497                  * but the "override" flag is given to idassert.
498                  * It allows to use SASL bind and yet proxyAuthz users
499                  */
500                 if ( op->o_conn != NULL &&
501                                 ( BER_BVISNULL( &lc->lc_bound_ndn ) ||
502                                   ( li->idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) )
503                 {
504                         (void)ldap_back_proxy_authz_bind( lc, op, rs );
505                         goto done;
506                 }
507
508 retry:;
509                 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
510                                 lc->lc_bound_ndn.bv_val,
511                                 LDAP_SASL_SIMPLE, &lc->lc_cred,
512                                 NULL, NULL, &msgid );
513
514                 if ( rs->sr_err == LDAP_SERVER_DOWN ) {
515                         if ( retries > 0 ) {
516                                 ldap_unbind_ext_s( lc->lc_ld, NULL, NULL );
517                                 lc->lc_ld = NULL;
518
519                                 /* lc here must be the regular lc, reset and ready for init */
520                                 if ( ldap_back_prepare_conn( &lc, op, rs, sendok ) != LDAP_SUCCESS ) {
521                                         return 0;
522                                 }
523
524                                 retries--;
525                                 goto retry;
526                         }
527
528                         ldap_back_freeconn( op, lc );
529                         rs->sr_err = slap_map_api2result( rs );
530
531                         return 0;
532                 }
533
534                 rc = ldap_back_op_result( lc, op, rs, msgid, sendok );
535                 if ( rc == LDAP_SUCCESS ) {
536                         lc->lc_bound = 1;
537                 }
538         }
539
540 done:;
541         rc = lc->lc_bound;
542         ldap_pvt_thread_mutex_unlock( &lc->lc_mutex );
543         return rc;
544 }
545
546 int
547 ldap_back_dobind( struct ldapconn *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
548 {
549         return ldap_back_dobind_int( lc, op, rs, sendok, 1 );
550 }
551
552 /*
553  * ldap_back_rebind
554  *
555  * This is a callback used for chasing referrals using the same
556  * credentials as the original user on this session.
557  */
558 static int 
559 ldap_back_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
560         ber_int_t msgid, void *params )
561 {
562         struct ldapconn *lc = (struct ldapconn *)params;
563
564         /* FIXME: add checks on the URL/identity? */
565
566         return ldap_sasl_bind_s( ld, lc->lc_bound_ndn.bv_val,
567                         LDAP_SASL_SIMPLE, &lc->lc_cred, NULL, NULL, NULL );
568 }
569
570 int
571 ldap_back_op_result(
572                 struct ldapconn         *lc,
573                 Operation               *op,
574                 SlapReply               *rs,
575                 ber_int_t               msgid,
576                 ldap_back_send_t        sendok )
577 {
578         char            *match = NULL;
579         LDAPMessage     *res = NULL;
580         char            *text = NULL;
581
582 #define ERR_OK(err) ((err) == LDAP_SUCCESS || (err) == LDAP_COMPARE_FALSE || (err) == LDAP_COMPARE_TRUE)
583
584         rs->sr_text = NULL;
585         rs->sr_matched = NULL;
586
587         /* if the error recorded in the reply corresponds
588          * to a successful state, get the error from the
589          * remote server response */
590         if ( ERR_OK( rs->sr_err ) ) {
591                 int             rc;
592                 struct timeval  tv = { 0, 0 };
593
594 retry:;
595                 /* if result parsing fails, note the failure reason */
596                 switch ( ldap_result( lc->lc_ld, msgid, 1, &tv, &res ) ) {
597                 case 0:
598                         tv.tv_sec = 0;
599                         tv.tv_usec = 100000;    /* 0.1 s */
600                         ldap_pvt_thread_yield();
601                         goto retry;
602
603                 case -1:
604                         ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
605                                         &rs->sr_err );
606                         break;
607
608
609                 /* otherwise get the result; if it is not
610                  * LDAP_SUCCESS, record it in the reply
611                  * structure (this includes 
612                  * LDAP_COMPARE_{TRUE|FALSE}) */
613                 default:
614                         rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
615                                         &match, &text, NULL, NULL, 1 );
616                         rs->sr_text = text;
617                         if ( rc != LDAP_SUCCESS ) {
618                                 rs->sr_err = rc;
619                         }
620                 }
621         }
622
623         /* if the error in the reply structure is not
624          * LDAP_SUCCESS, try to map it from client 
625          * to server error */
626         if ( !ERR_OK( rs->sr_err ) ) {
627                 rs->sr_err = slap_map_api2result( rs );
628
629                 /* internal ops ( op->o_conn == NULL ) 
630                  * must not reply to client */
631                 if ( op->o_conn && !op->o_do_not_cache && match ) {
632
633                         /* record the (massaged) matched
634                          * DN into the reply structure */
635                         rs->sr_matched = match;
636                 }
637         }
638         if ( op->o_conn &&
639                         ( ( sendok & LDAP_BACK_SENDOK ) 
640                           || ( ( sendok & LDAP_BACK_SENDERR ) && rs->sr_err != LDAP_SUCCESS ) ) )
641         {
642                 send_ldap_result( op, rs );
643         }
644         if ( match ) {
645                 if ( rs->sr_matched != match ) {
646                         free( (char *)rs->sr_matched );
647                 }
648                 rs->sr_matched = NULL;
649                 ldap_memfree( match );
650         }
651         if ( text ) {
652                 ldap_memfree( text );
653         }
654         rs->sr_text = NULL;
655         return( ERR_OK( rs->sr_err ) ? 0 : -1 );
656 }
657
658 /* return true if bound, false if failed */
659 int
660 ldap_back_retry( struct ldapconn *lc, Operation *op, SlapReply *rs, ldap_back_send_t sendok )
661 {
662         ldap_pvt_thread_mutex_lock( &lc->lc_mutex );
663         ldap_unbind_ext_s( lc->lc_ld, NULL, NULL );
664         lc->lc_ld = NULL;
665         lc->lc_bound = 0;
666
667         /* lc here must be the regular lc, reset and ready for init */
668         if ( ldap_back_prepare_conn( &lc, op, rs, sendok ) != LDAP_SUCCESS ) {
669                 return 0;
670         }
671
672         ldap_pvt_thread_mutex_unlock( &lc->lc_mutex );
673         return ldap_back_dobind_int( lc, op, rs, sendok, 0 );
674 }
675
676 static int
677 ldap_back_proxy_authz_bind( struct ldapconn *lc, Operation *op, SlapReply *rs )
678 {
679         struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
680         struct berval   binddn = slap_empty_bv;
681         struct berval   bindcred = slap_empty_bv;
682         int             dobind = 0;
683         int             msgid;
684         int             rc;
685
686         /*
687          * FIXME: we need to let clients use proxyAuthz
688          * otherwise we cannot do symmetric pools of servers;
689          * we have to live with the fact that a user can
690          * authorize itself as any ID that is allowed
691          * by the authzTo directive of the "proxyauthzdn".
692          */
693         /*
694          * NOTE: current Proxy Authorization specification
695          * and implementation do not allow proxy authorization
696          * control to be provided with Bind requests
697          */
698         /*
699          * if no bind took place yet, but the connection is bound
700          * and the "proxyauthzdn" is set, then bind as 
701          * "proxyauthzdn" and explicitly add the proxyAuthz 
702          * control to every operation with the dn bound 
703          * to the connection as control value.
704          */
705
706         /* bind as proxyauthzdn only if no idassert mode
707          * is requested, or if the client's identity
708          * is authorized */
709         switch ( li->idassert_mode ) {
710         case LDAP_BACK_IDASSERT_LEGACY:
711                 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) && !BER_BVISEMPTY( &op->o_conn->c_ndn ) ) {
712                         if ( !BER_BVISNULL( &li->idassert_authcDN ) && !BER_BVISEMPTY( &li->idassert_authcDN ) )
713                         {
714                                 binddn = li->idassert_authcDN;
715                                 bindcred = li->idassert_passwd;
716                                 dobind = 1;
717                         }
718                 }
719                 break;
720
721         default:
722                 if ( li->idassert_authz ) {
723                         struct berval authcDN;
724
725                         if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
726                                 authcDN = slap_empty_bv;
727                         } else {
728                                 authcDN = op->o_conn->c_ndn;
729                         }       
730                         rs->sr_err = slap_sasl_matches( op, li->idassert_authz,
731                                         &authcDN, &authcDN );
732                         if ( rs->sr_err != LDAP_SUCCESS ) {
733                                 send_ldap_result( op, rs );
734                                 lc->lc_bound = 0;
735                                 goto done;
736                         }
737                 }
738
739                 binddn = li->idassert_authcDN;
740                 bindcred = li->idassert_passwd;
741                 dobind = 1;
742                 break;
743         }
744
745         if ( dobind && li->idassert_authmethod == LDAP_AUTH_SASL ) {
746 #ifdef HAVE_CYRUS_SASL
747                 void            *defaults = NULL;
748                 struct berval   authzID = BER_BVNULL;
749                 int             freeauthz = 0;
750
751                 /* if SASL supports native authz, prepare for it */
752                 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
753                                 ( li->idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
754                 {
755                         switch ( li->idassert_mode ) {
756                         case LDAP_BACK_IDASSERT_OTHERID:
757                         case LDAP_BACK_IDASSERT_OTHERDN:
758                                 authzID = li->idassert_authzID;
759                                 break;
760
761                         case LDAP_BACK_IDASSERT_ANONYMOUS:
762                                 BER_BVSTR( &authzID, "dn:" );
763                                 break;
764
765                         case LDAP_BACK_IDASSERT_SELF:
766                                 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
767                                         /* connection is not authc'd, so don't idassert */
768                                         BER_BVSTR( &authzID, "dn:" );
769                                         break;
770                                 }
771                                 authzID.bv_len = STRLENOF( "dn:" ) + op->o_conn->c_ndn.bv_len;
772                                 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
773                                 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
774                                 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
775                                                 op->o_conn->c_ndn.bv_val, op->o_conn->c_ndn.bv_len + 1 );
776                                 freeauthz = 1;
777                                 break;
778
779                         default:
780                                 break;
781                         }
782                 }
783
784 #if 0   /* will deal with this later... */
785                 if ( sasl_secprops != NULL ) {
786                         rs->sr_err = ldap_set_option( lc->lc_ld, LDAP_OPT_X_SASL_SECPROPS,
787                                 (void *) sasl_secprops );
788
789                         if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
790                                 send_ldap_result( op, rs );
791                                 lc->lc_bound = 0;
792                                 goto done;
793                         }
794                 }
795 #endif
796
797                 defaults = lutil_sasl_defaults( lc->lc_ld,
798                                 li->idassert_sasl_mech.bv_val,
799                                 li->idassert_sasl_realm.bv_val,
800                                 li->idassert_authcID.bv_val,
801                                 li->idassert_passwd.bv_val,
802                                 authzID.bv_val );
803
804                 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld, binddn.bv_val,
805                                 li->idassert_sasl_mech.bv_val, NULL, NULL,
806                                 li->idassert_sasl_flags, lutil_sasl_interact,
807                                 defaults );
808
809                 lutil_sasl_freedefs( defaults );
810                 if ( freeauthz ) {
811                         slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
812                 }
813
814                 rs->sr_err = slap_map_api2result( rs );
815                 if ( rs->sr_err != LDAP_SUCCESS ) {
816                         lc->lc_bound = 0;
817                         send_ldap_result( op, rs );
818
819                 } else {
820                         lc->lc_bound = 1;
821                 }
822                 goto done;
823 #endif /* HAVE_CYRUS_SASL */
824         }
825
826         switch ( li->idassert_authmethod ) {
827         case LDAP_AUTH_SIMPLE:
828                 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
829                                 binddn.bv_val, LDAP_SASL_SIMPLE,
830                                 &bindcred, NULL, NULL, &msgid );
831                 break;
832
833         case LDAP_AUTH_NONE:
834                 lc->lc_bound = 1;
835                 goto done;
836
837         default:
838                 /* unsupported! */
839                 lc->lc_bound = 0;
840                 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
841                 send_ldap_result( op, rs );
842                 goto done;
843         }
844
845         rc = ldap_back_op_result( lc, op, rs, msgid, LDAP_BACK_SENDERR );
846         if ( rc == LDAP_SUCCESS ) {
847                 lc->lc_bound = 1;
848         }
849 done:;
850         return lc->lc_bound;
851 }
852
853 /*
854  * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
855  * to existing server-side controls if required; if not,
856  * the existing server-side controls are placed in *pctrls.
857  * The caller, after using the controls in client API 
858  * operations, if ( *pctrls != op->o_ctrls ), should
859  * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
860  * The function returns success if the control could
861  * be added if required, or if it did nothing; in the future,
862  * it might return some error if it failed.
863  * 
864  * if no bind took place yet, but the connection is bound
865  * and the "proxyauthzdn" is set, then bind as "proxyauthzdn" 
866  * and explicitly add proxyAuthz the control to every operation
867  * with the dn bound to the connection as control value.
868  *
869  * If no server-side controls are defined for the operation,
870  * simply add the proxyAuthz control; otherwise, if the
871  * proxyAuthz control is not already set, add it as
872  * the first one (FIXME: is controls order significant
873  * for security?).
874  */
875 int
876 ldap_back_proxy_authz_ctrl(
877                 struct ldapconn *lc,
878                 Operation       *op,
879                 SlapReply       *rs,
880                 LDAPControl     ***pctrls )
881 {
882         struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
883         LDAPControl     **ctrls = NULL;
884         int             i = 0,
885                         mode;
886         struct berval   assertedID;
887
888         *pctrls = NULL;
889
890         rs->sr_err = LDAP_SUCCESS;
891
892         if ( ( BER_BVISNULL( &li->idassert_authcID ) || BER_BVISEMPTY( &li->idassert_authcID ) )
893                         && ( BER_BVISNULL( &li->idassert_authcDN ) || BER_BVISEMPTY( &li->idassert_authcDN ) ) ) {
894                 goto done;
895         }
896
897         if ( !op->o_conn ) {
898                 goto done;
899         }
900
901         if ( li->idassert_mode == LDAP_BACK_IDASSERT_LEGACY ) {
902                 if ( op->o_proxy_authz ) {
903                         /*
904                          * FIXME: we do not want to perform proxyAuthz
905                          * on behalf of the client, because this would
906                          * be performed with "proxyauthzdn" privileges.
907                          *
908                          * This might actually be too strict, since
909                          * the "proxyauthzdn" authzTo, and each entry's
910                          * authzFrom attributes may be crafted
911                          * to avoid unwanted proxyAuthz to take place.
912                          */
913 #if 0
914                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
915                         rs->sr_text = "proxyAuthz not allowed within namingContext";
916 #endif
917                         goto done;
918                 }
919
920                 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
921                         goto done;
922                 }
923
924                 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
925                         goto done;
926                 }
927
928                 if ( BER_BVISNULL( &li->idassert_authcDN ) ) {
929                         goto done;
930                 }
931
932         } else if ( li->idassert_authmethod == LDAP_AUTH_SASL ) {
933                 if ( ( li->idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ )
934                                 /* && ( !BER_BVISNULL( &op->o_conn->c_ndn ) || lc->lc_bound ) */ )
935                 {
936                         /* already asserted in SASL via native authz */
937                         /* NOTE: the test on lc->lc_bound is used to trap
938                          * native authorization of anonymous users,
939                          * since in that case op->o_conn->c_ndn is NULL */
940                         goto done;
941                 }
942
943         } else if ( li->idassert_authz ) {
944                 int             rc;
945                 struct berval authcDN;
946
947                 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
948                         authcDN = slap_empty_bv;
949                 } else {
950                         authcDN = op->o_conn->c_ndn;
951                 }
952                 rc = slap_sasl_matches( op, li->idassert_authz,
953                                 &authcDN, & authcDN );
954                 if ( rc != LDAP_SUCCESS ) {
955                         /* op->o_conn->c_ndn is not authorized
956                          * to use idassert */
957                         return rc;
958                 }
959         }
960
961         if ( op->o_proxy_authz ) {
962                 /*
963                  * FIXME: we can:
964                  * 1) ignore the already set proxyAuthz control
965                  * 2) leave it in place, and don't set ours
966                  * 3) add both
967                  * 4) reject the operation
968                  *
969                  * option (4) is very drastic
970                  * option (3) will make the remote server reject
971                  * the operation, thus being equivalent to (4)
972                  * option (2) will likely break the idassert
973                  * assumptions, so we cannot accept it;
974                  * option (1) means that we are contradicting
975                  * the client's reques.
976                  *
977                  * I think (4) is the only correct choice.
978                  */
979                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
980                 rs->sr_text = "proxyAuthz not allowed within namingContext";
981         }
982
983         if ( op->o_do_not_cache && op->o_is_auth_check ) {
984                 mode = LDAP_BACK_IDASSERT_NOASSERT;
985
986         } else {
987                 mode = li->idassert_mode;
988         }
989
990         switch ( mode ) {
991         case LDAP_BACK_IDASSERT_LEGACY:
992         case LDAP_BACK_IDASSERT_SELF:
993                 /* original behavior:
994                  * assert the client's identity */
995                 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
996                         assertedID = slap_empty_bv;
997                 } else {
998                         assertedID = op->o_conn->c_ndn;
999                 }
1000                 break;
1001
1002         case LDAP_BACK_IDASSERT_ANONYMOUS:
1003                 /* assert "anonymous" */
1004                 assertedID = slap_empty_bv;
1005                 break;
1006
1007         case LDAP_BACK_IDASSERT_NOASSERT:
1008                 /* don't assert; bind as proxyauthzdn */
1009                 goto done;
1010
1011         case LDAP_BACK_IDASSERT_OTHERID:
1012         case LDAP_BACK_IDASSERT_OTHERDN:
1013                 /* assert idassert DN */
1014                 assertedID = li->idassert_authzID;
1015                 break;
1016
1017         default:
1018                 assert( 0 );
1019         }
1020
1021         if ( BER_BVISNULL( &assertedID ) ) {
1022                 assertedID = slap_empty_bv;
1023         }
1024
1025         if ( op->o_ctrls ) {
1026                 for ( i = 0; op->o_ctrls[ i ]; i++ )
1027                         /* just count ctrls */ ;
1028         }
1029
1030         ctrls = ch_malloc( sizeof( LDAPControl * ) * (i + 2) );
1031         ctrls[ 0 ] = ch_malloc( sizeof( LDAPControl ) );
1032         
1033         ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
1034         ctrls[ 0 ]->ldctl_iscritical = 1;
1035
1036         switch ( li->idassert_mode ) {
1037         /* already in u:ID or dn:DN form */
1038         case LDAP_BACK_IDASSERT_OTHERID:
1039         case LDAP_BACK_IDASSERT_OTHERDN:
1040                 ber_dupbv( &ctrls[ 0 ]->ldctl_value, &assertedID );
1041                 break;
1042
1043         /* needs the dn: prefix */
1044         default:
1045                 ctrls[ 0 ]->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
1046                 ctrls[ 0 ]->ldctl_value.bv_val = ch_malloc( ctrls[ 0 ]->ldctl_value.bv_len + 1 );
1047                 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
1048                 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val + STRLENOF( "dn:" ),
1049                                 assertedID.bv_val, assertedID.bv_len + 1 );
1050                 break;
1051         }
1052
1053         if ( op->o_ctrls ) {
1054                 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
1055                         ctrls[ i + 1 ] = op->o_ctrls[ i ];
1056                 }
1057         }
1058         ctrls[ i + 1 ] = NULL;
1059
1060 done:;
1061         if ( ctrls == NULL ) {
1062                 ctrls = op->o_ctrls;
1063         }
1064
1065         *pctrls = ctrls;
1066         
1067         return rs->sr_err;
1068 }
1069
1070 int
1071 ldap_back_proxy_authz_ctrl_free( Operation *op, LDAPControl ***pctrls )
1072 {
1073         LDAPControl     **ctrls = *pctrls;
1074
1075         /* we assume that the first control is the proxyAuthz
1076          * added by back-ldap, so it's the only one we explicitly 
1077          * free */
1078         if ( ctrls && ctrls != op->o_ctrls ) {
1079                 assert( ctrls[ 0 ] );
1080
1081                 if ( !BER_BVISNULL( &ctrls[ 0 ]->ldctl_value ) ) {
1082                         free( ctrls[ 0 ]->ldctl_value.bv_val );
1083                 }
1084
1085                 free( ctrls[ 0 ] );
1086                 free( ctrls );
1087         } 
1088
1089         *pctrls = NULL;
1090
1091         return 0;
1092 }