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