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