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