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