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