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