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