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