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