]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/bind.c
eedc66cfb856ddb5ed72bc1209c2a6ab10772e86
[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                 int             rc;
432                 struct timeval  tv = { 0, 0 };
433
434 retry:;
435                 /* if result parsing fails, note the failure reason */
436                 switch ( ldap_result( lc->lc_ld, msgid, 1, &tv, &res ) ) {
437                 case 0:
438                         tv.tv_sec = 0;
439                         tv.tv_usec = 100000;    /* 0.1 s */
440                         ldap_pvt_thread_yield();
441                         goto retry;
442
443                 case -1:
444                         ldap_get_option( lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
445                                         &rs->sr_err );
446                         break;
447
448
449                 /* otherwise get the result; if it is not
450                  * LDAP_SUCCESS, record it in the reply
451                  * structure (this includes 
452                  * LDAP_COMPARE_{TRUE|FALSE}) */
453                 default:
454                         rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
455                                         &match, &text, NULL, NULL, 1 );
456                         rs->sr_text = text;
457                         if ( rc != LDAP_SUCCESS ) {
458                                 rs->sr_err = rc;
459                         }
460                 }
461         }
462
463         /* if the error in the reply structure is not
464          * LDAP_SUCCESS, try to map it from client 
465          * to server error */
466         if ( !ERR_OK( rs->sr_err ) ) {
467                 rs->sr_err = slap_map_api2result( rs );
468
469                 /* internal ops ( op->o_conn == NULL ) 
470                  * must not reply to client */
471                 if ( op->o_conn && !op->o_do_not_cache && match ) {
472
473                         /* record the (massaged) matched
474                          * DN into the reply structure */
475                         rs->sr_matched = match;
476                 }
477         }
478         if ( op->o_conn && ( sendok || rs->sr_err != LDAP_SUCCESS ) ) {
479                 send_ldap_result( op, rs );
480         }
481         if ( match ) {
482                 if ( rs->sr_matched != match ) {
483                         free( (char *)rs->sr_matched );
484                 }
485                 rs->sr_matched = NULL;
486                 ldap_memfree( match );
487         }
488         if ( text ) {
489                 ldap_memfree( text );
490         }
491         rs->sr_text = NULL;
492         return( ERR_OK( rs->sr_err ) ? 0 : -1 );
493 }
494
495 /* return true if bound, false if failed */
496 int
497 ldap_back_retry( struct ldapconn *lc, Operation *op, SlapReply *rs )
498 {
499         struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
500         int vers = op->o_protocol;
501         LDAP *ld;
502
503         ldap_pvt_thread_mutex_lock( &lc->lc_mutex );
504         ldap_unbind_ext_s( lc->lc_ld, NULL, NULL );
505         lc->lc_bound = 0;
506         rs->sr_err = ldap_initialize( &ld, li->url );
507                 
508         if ( rs->sr_err != LDAP_SUCCESS ) {
509                 rs->sr_err = slap_map_api2result( rs );
510                 if ( rs->sr_text == NULL ) {
511                         rs->sr_text = "ldap_initialize() failed";
512                 }
513                 if ( op->o_conn ) {
514                         send_ldap_result( op, rs );
515                 }
516                 rs->sr_text = NULL;
517                 return 0;
518         }
519         /* Set LDAP version. This will always succeed: If the client
520          * bound with a particular version, then so can we.
521          */
522         ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, (const void *)&vers );
523         /* FIXME: configurable? */
524         ldap_set_option( ld, LDAP_OPT_REFERRALS, LDAP_OPT_ON );
525         lc->lc_ld = ld;
526         ldap_pvt_thread_mutex_unlock( &lc->lc_mutex );
527         return ldap_back_dobind( lc, op, rs );
528 }
529
530 #ifdef LDAP_BACK_PROXY_AUTHZ
531 static int
532 ldap_back_proxy_authz_bind( struct ldapconn *lc, Operation *op, SlapReply *rs )
533 {
534         struct ldapinfo *li = (struct ldapinfo *)op->o_bd->be_private;
535         struct berval   binddn = slap_empty_bv;
536         struct berval   bindcred = slap_empty_bv;
537         int             dobind = 0;
538         int             msgid;
539         int             rc;
540
541         /*
542          * FIXME: we need to let clients use proxyAuthz
543          * otherwise we cannot do symmetric pools of servers;
544          * we have to live with the fact that a user can
545          * authorize itself as any ID that is allowed
546          * by the authzTo directive of the "proxyauthzdn".
547          */
548         /*
549          * NOTE: current Proxy Authorization specification
550          * and implementation do not allow proxy authorization
551          * control to be provided with Bind requests
552          */
553         /*
554          * if no bind took place yet, but the connection is bound
555          * and the "proxyauthzdn" is set, then bind as 
556          * "proxyauthzdn" and explicitly add the proxyAuthz 
557          * control to every operation with the dn bound 
558          * to the connection as control value.
559          */
560
561         /* bind as proxyauthzdn only if no idassert mode
562          * is requested, or if the client's identity
563          * is authorized */
564         switch ( li->idassert_mode ) {
565         case LDAP_BACK_IDASSERT_LEGACY:
566                 if ( !BER_BVISNULL( &op->o_conn->c_ndn ) && !BER_BVISEMPTY( &op->o_conn->c_ndn ) ) {
567                         if ( !BER_BVISNULL( &li->idassert_authcDN ) && !BER_BVISEMPTY( &li->idassert_authcDN ) )
568                         {
569                                 binddn = li->idassert_authcDN;
570                                 bindcred = li->idassert_passwd;
571                                 dobind = 1;
572                         }
573                 }
574                 break;
575
576         default:
577                 if ( li->idassert_authz ) {
578                         struct berval authcDN;
579
580                         if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
581                                 authcDN = slap_empty_bv;
582                         } else {
583                                 authcDN = op->o_conn->c_ndn;
584                         }       
585                         rs->sr_err = slap_sasl_matches( op, li->idassert_authz,
586                                         &authcDN, &authcDN );
587                         if ( rs->sr_err != LDAP_SUCCESS ) {
588                                 send_ldap_result( op, rs );
589                                 lc->lc_bound = 0;
590                                 goto done;
591                         }
592                 }
593
594                 binddn = li->idassert_authcDN;
595                 bindcred = li->idassert_passwd;
596                 dobind = 1;
597                 break;
598         }
599
600         if ( dobind && li->idassert_authmethod == LDAP_AUTH_SASL ) {
601 #ifdef HAVE_CYRUS_SASL
602                 void            *defaults = NULL;
603                 struct berval   authzID = BER_BVNULL;
604                 int             freeauthz = 0;
605
606                 /* if SASL supports native authz, prepare for it */
607                 if ( ( !op->o_do_not_cache || !op->o_is_auth_check ) &&
608                                 ( li->idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ ) )
609                 {
610                         switch ( li->idassert_mode ) {
611                         case LDAP_BACK_IDASSERT_OTHERID:
612                         case LDAP_BACK_IDASSERT_OTHERDN:
613                                 authzID = li->idassert_authzID;
614                                 break;
615
616                         case LDAP_BACK_IDASSERT_ANONYMOUS:
617                                 BER_BVSTR( &authzID, "dn:" );
618                                 break;
619
620                         case LDAP_BACK_IDASSERT_SELF:
621                                 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
622                                         /* connection is not authc'd, so don't idassert */
623                                         BER_BVSTR( &authzID, "dn:" );
624                                         break;
625                                 }
626                                 authzID.bv_len = STRLENOF( "dn:" ) + op->o_conn->c_ndn.bv_len;
627                                 authzID.bv_val = slap_sl_malloc( authzID.bv_len + 1, op->o_tmpmemctx );
628                                 AC_MEMCPY( authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
629                                 AC_MEMCPY( authzID.bv_val + STRLENOF( "dn:" ),
630                                                 op->o_conn->c_ndn.bv_val, op->o_conn->c_ndn.bv_len + 1 );
631                                 freeauthz = 1;
632                                 break;
633
634                         default:
635                                 break;
636                         }
637                 }
638
639 #if 0   /* will deal with this later... */
640                 if ( sasl_secprops != NULL ) {
641                         rs->sr_err = ldap_set_option( lc->lc_ld, LDAP_OPT_X_SASL_SECPROPS,
642                                 (void *) sasl_secprops );
643
644                         if ( rs->sr_err != LDAP_OPT_SUCCESS ) {
645                                 send_ldap_result( op, rs );
646                                 lc->lc_bound = 0;
647                                 goto done;
648                         }
649                 }
650 #endif
651
652                 defaults = lutil_sasl_defaults( lc->lc_ld,
653                                 li->idassert_sasl_mech.bv_val,
654                                 li->idassert_sasl_realm.bv_val,
655                                 li->idassert_authcID.bv_val,
656                                 li->idassert_passwd.bv_val,
657                                 authzID.bv_val );
658
659                 rs->sr_err = ldap_sasl_interactive_bind_s( lc->lc_ld, binddn.bv_val,
660                                 li->idassert_sasl_mech.bv_val, NULL, NULL,
661                                 li->idassert_sasl_flags, lutil_sasl_interact,
662                                 defaults );
663
664                 lutil_sasl_freedefs( defaults );
665                 if ( freeauthz ) {
666                         slap_sl_free( authzID.bv_val, op->o_tmpmemctx );
667                 }
668
669                 rs->sr_err = slap_map_api2result( rs );
670                 if ( rs->sr_err != LDAP_SUCCESS ) {
671                         lc->lc_bound = 0;
672                         send_ldap_result( op, rs );
673
674                 } else {
675                         lc->lc_bound = 1;
676                 }
677                 goto done;
678 #endif /* HAVE_CYRUS_SASL */
679         }
680
681         switch ( li->idassert_authmethod ) {
682         case LDAP_AUTH_SIMPLE:
683                 rs->sr_err = ldap_sasl_bind( lc->lc_ld,
684                                 binddn.bv_val, LDAP_SASL_SIMPLE,
685                                 &bindcred, NULL, NULL, &msgid );
686                 break;
687
688         case LDAP_AUTH_NONE:
689                 lc->lc_bound = 1;
690                 goto done;
691
692         default:
693                 /* unsupported! */
694                 lc->lc_bound = 0;
695                 rs->sr_err = LDAP_AUTH_METHOD_NOT_SUPPORTED;
696                 send_ldap_result( op, rs );
697                 goto done;
698         }
699
700         rc = ldap_back_op_result( lc, op, rs, msgid, 0 );
701         if ( rc == LDAP_SUCCESS ) {
702                 lc->lc_bound = 1;
703         }
704 done:;
705         return lc->lc_bound;
706 }
707
708 /*
709  * ldap_back_proxy_authz_ctrl() prepends a proxyAuthz control
710  * to existing server-side controls if required; if not,
711  * the existing server-side controls are placed in *pctrls.
712  * The caller, after using the controls in client API 
713  * operations, if ( *pctrls != op->o_ctrls ), should
714  * free( (*pctrls)[ 0 ] ) and free( *pctrls ).
715  * The function returns success if the control could
716  * be added if required, or if it did nothing; in the future,
717  * it might return some error if it failed.
718  * 
719  * if no bind took place yet, but the connection is bound
720  * and the "proxyauthzdn" is set, then bind as "proxyauthzdn" 
721  * and explicitly add proxyAuthz the control to every operation
722  * with the dn bound to the connection as control value.
723  *
724  * If no server-side controls are defined for the operation,
725  * simply add the proxyAuthz control; otherwise, if the
726  * proxyAuthz control is not already set, add it as
727  * the first one (FIXME: is controls order significant
728  * for security?).
729  */
730 int
731 ldap_back_proxy_authz_ctrl(
732                 struct ldapconn *lc,
733                 Operation       *op,
734                 SlapReply       *rs,
735                 LDAPControl     ***pctrls )
736 {
737         struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
738         LDAPControl     **ctrls = NULL;
739         int             i = 0,
740                         mode;
741         struct berval   assertedID;
742
743         *pctrls = NULL;
744
745         rs->sr_err = LDAP_SUCCESS;
746
747         if ( ( BER_BVISNULL( &li->idassert_authcID ) || BER_BVISEMPTY( &li->idassert_authcID ) )
748                         && ( BER_BVISNULL( &li->idassert_authcDN ) || BER_BVISEMPTY( &li->idassert_authcDN ) ) ) {
749                 goto done;
750         }
751
752         if ( !op->o_conn ) {
753                 goto done;
754         }
755
756         if ( li->idassert_mode == LDAP_BACK_IDASSERT_LEGACY ) {
757                 if ( op->o_proxy_authz ) {
758                         /*
759                          * FIXME: we do not want to perform proxyAuthz
760                          * on behalf of the client, because this would
761                          * be performed with "proxyauthzdn" privileges.
762                          *
763                          * This might actually be too strict, since
764                          * the "proxyauthzdn" authzTo, and each entry's
765                          * authzFrom attributes may be crafted
766                          * to avoid unwanted proxyAuthz to take place.
767                          */
768 #if 0
769                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
770                         rs->sr_text = "proxyAuthz not allowed within namingContext";
771 #endif
772                         goto done;
773                 }
774
775                 if ( !BER_BVISNULL( &lc->lc_bound_ndn ) ) {
776                         goto done;
777                 }
778
779                 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
780                         goto done;
781                 }
782
783                 if ( BER_BVISNULL( &li->idassert_authcDN ) ) {
784                         goto done;
785                 }
786
787         } else if ( li->idassert_authmethod == LDAP_AUTH_SASL ) {
788                 if ( ( li->idassert_flags & LDAP_BACK_AUTH_NATIVE_AUTHZ )
789                                 /* && ( !BER_BVISNULL( &op->o_conn->c_ndn ) || lc->lc_bound ) */ )
790                 {
791                         /* already asserted in SASL via native authz */
792                         /* NOTE: the test on lc->lc_bound is used to trap
793                          * native authorization of anonymous users,
794                          * since in that case op->o_conn->c_ndn is NULL */
795                         goto done;
796                 }
797
798         } else if ( li->idassert_authz ) {
799                 int             rc;
800                 struct berval authcDN;
801
802                 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
803                         authcDN = slap_empty_bv;
804                 } else {
805                         authcDN = op->o_conn->c_ndn;
806                 }
807                 rc = slap_sasl_matches( op, li->idassert_authz,
808                                 &authcDN, & authcDN );
809                 if ( rc != LDAP_SUCCESS ) {
810                         /* op->o_conn->c_ndn is not authorized
811                          * to use idassert */
812                         return rc;
813                 }
814         }
815
816         if ( op->o_proxy_authz ) {
817                 /*
818                  * FIXME: we can:
819                  * 1) ignore the already set proxyAuthz control
820                  * 2) leave it in place, and don't set ours
821                  * 3) add both
822                  * 4) reject the operation
823                  *
824                  * option (4) is very drastic
825                  * option (3) will make the remote server reject
826                  * the operation, thus being equivalent to (4)
827                  * option (2) will likely break the idassert
828                  * assumptions, so we cannot accept it;
829                  * option (1) means that we are contradicting
830                  * the client's reques.
831                  *
832                  * I think (4) is the only correct choice.
833                  */
834                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
835                 rs->sr_text = "proxyAuthz not allowed within namingContext";
836         }
837
838         if ( op->o_do_not_cache && op->o_is_auth_check ) {
839                 mode = LDAP_BACK_IDASSERT_NOASSERT;
840
841         } else {
842                 mode = li->idassert_mode;
843         }
844
845         switch ( mode ) {
846         case LDAP_BACK_IDASSERT_LEGACY:
847         case LDAP_BACK_IDASSERT_SELF:
848                 /* original behavior:
849                  * assert the client's identity */
850                 if ( BER_BVISNULL( &op->o_conn->c_ndn ) ) {
851                         assertedID = slap_empty_bv;
852                 } else {
853                         assertedID = op->o_conn->c_ndn;
854                 }
855                 break;
856
857         case LDAP_BACK_IDASSERT_ANONYMOUS:
858                 /* assert "anonymous" */
859                 assertedID = slap_empty_bv;
860                 break;
861
862         case LDAP_BACK_IDASSERT_NOASSERT:
863                 /* don't assert; bind as proxyauthzdn */
864                 goto done;
865
866         case LDAP_BACK_IDASSERT_OTHERID:
867         case LDAP_BACK_IDASSERT_OTHERDN:
868                 /* assert idassert DN */
869                 assertedID = li->idassert_authzID;
870                 break;
871
872         default:
873                 assert( 0 );
874         }
875
876         if ( BER_BVISNULL( &assertedID ) ) {
877                 assertedID = slap_empty_bv;
878         }
879
880         if ( op->o_ctrls ) {
881                 for ( i = 0; op->o_ctrls[ i ]; i++ )
882                         /* just count ctrls */ ;
883         }
884
885         ctrls = ch_malloc( sizeof( LDAPControl * ) * (i + 2) );
886         ctrls[ 0 ] = ch_malloc( sizeof( LDAPControl ) );
887         
888         ctrls[ 0 ]->ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
889         ctrls[ 0 ]->ldctl_iscritical = 1;
890
891         switch ( li->idassert_mode ) {
892         /* already in u:ID or dn:DN form */
893         case LDAP_BACK_IDASSERT_OTHERID:
894         case LDAP_BACK_IDASSERT_OTHERDN:
895                 ber_dupbv( &ctrls[ 0 ]->ldctl_value, &assertedID );
896                 break;
897
898         /* needs the dn: prefix */
899         default:
900                 ctrls[ 0 ]->ldctl_value.bv_len = assertedID.bv_len + STRLENOF( "dn:" );
901                 ctrls[ 0 ]->ldctl_value.bv_val = ch_malloc( ctrls[ 0 ]->ldctl_value.bv_len + 1 );
902                 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val, "dn:", STRLENOF( "dn:" ) );
903                 AC_MEMCPY( ctrls[ 0 ]->ldctl_value.bv_val + STRLENOF( "dn:" ),
904                                 assertedID.bv_val, assertedID.bv_len + 1 );
905                 break;
906         }
907
908         if ( op->o_ctrls ) {
909                 for ( i = 0; op->o_ctrls[ i ]; i++ ) {
910                         ctrls[ i + 1 ] = op->o_ctrls[ i ];
911                 }
912         }
913         ctrls[ i + 1 ] = NULL;
914
915 done:;
916         if ( ctrls == NULL ) {
917                 ctrls = op->o_ctrls;
918         }
919
920         *pctrls = ctrls;
921         
922         return rs->sr_err;
923 }
924
925 int
926 ldap_back_proxy_authz_ctrl_free( Operation *op, LDAPControl ***pctrls )
927 {
928         LDAPControl     **ctrls = *pctrls;
929
930         if ( ctrls && ctrls != op->o_ctrls ) {
931                 assert( ctrls[ 0 ] );
932
933                 if ( !BER_BVISNULL( &ctrls[ 0 ]->ldctl_value ) ) {
934                         free( ctrls[ 0 ]->ldctl_value.bv_val );
935                 }
936
937                 free( ctrls[ 0 ] );
938                 free( ctrls );
939         } 
940
941         *pctrls = NULL;
942
943         return 0;
944 }
945 #endif /* LDAP_BACK_PROXY_AUTHZ */