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