]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/bind.c
don't lookup rootdn if the password is incorrect (ITS#4004)
[openldap] / servers / slapd / back-meta / bind.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1999-2005 The OpenLDAP Foundation.
5  * Portions Copyright 2001-2003 Pierangelo Masarati.
6  * Portions Copyright 1999-2003 Howard Chu.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in the file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17 /* ACKNOWLEDGEMENTS:
18  * This work was initially developed by the Howard Chu for inclusion
19  * in OpenLDAP Software and subsequently enhanced by Pierangelo
20  * Masarati.
21  */
22
23 #include "portable.h"
24
25 #include <stdio.h>
26
27 #include <ac/errno.h>
28 #include <ac/socket.h>
29 #include <ac/string.h>
30
31
32 #define AVL_INTERNAL
33 #include "slap.h"
34 #include "../back-ldap/back-ldap.h"
35 #include "back-meta.h"
36
37 static LDAP_REBIND_PROC meta_back_rebind;
38
39 static int
40 meta_back_single_bind(
41         Operation               *op,
42         SlapReply               *rs,
43         metaconn_t              *mc,
44         int                     candidate );
45
46 int
47 meta_back_bind( Operation *op, SlapReply *rs )
48 {
49         metainfo_t      *mi = ( metainfo_t * )op->o_bd->be_private;
50         metaconn_t      *mc = NULL;
51
52         int             rc = LDAP_OTHER,
53                         i, gotit = 0, isroot = 0;
54
55         SlapReply       *candidates = meta_back_candidates_get( op );
56
57         rs->sr_err = LDAP_SUCCESS;
58
59         Debug( LDAP_DEBUG_ARGS, "meta_back_bind: dn: %s.\n%s%s",
60                         op->o_req_dn.bv_val, "", "" );
61
62         if ( op->orb_method == LDAP_AUTH_SIMPLE
63                 && be_isroot_dn( op->o_bd, &op->o_req_ndn ) )
64         {
65                 if ( !be_isroot_pw( op ) ) {
66                         rs->sr_err = LDAP_INVALID_CREDENTIALS;
67                         rs->sr_text = NULL;
68                         send_ldap_result( op, rs );
69                         return rs->sr_err;
70                 }
71                 isroot = 1;
72         }
73
74         /* we need meta_back_getconn() not send result even on error,
75          * because we want to intercept the error and make it
76          * invalidCredentials */
77         mc = meta_back_getconn( op, rs, NULL, LDAP_BACK_DONTSEND );
78         if ( !mc ) {
79                 Debug( LDAP_DEBUG_ANY,
80                                 "meta_back_bind: no target "
81                                 "for dn \"%s\" (%d: %s).\n",
82                                 op->o_req_dn.bv_val, rs->sr_err,
83                                 rs->sr_text ? rs->sr_text : "" );
84                 /* FIXME: there might be cases where we don't want
85                  * to map the error onto invalidCredentials */
86                 switch ( rs->sr_err ) {
87                 case LDAP_NO_SUCH_OBJECT:
88                 case LDAP_UNWILLING_TO_PERFORM:
89                         rs->sr_err = LDAP_INVALID_CREDENTIALS;
90                         rs->sr_text = NULL;
91                         break;
92                 }
93                 send_ldap_result( op, rs );
94                 return rs->sr_err;
95         }
96
97         /*
98          * Each target is scanned ...
99          */
100         mc->mc_auth_target = META_BOUND_NONE;
101         for ( i = 0; i < mi->mi_ntargets; i++ ) {
102                 int             lerr;
103                 Operation       op2 = *op;
104
105                 /*
106                  * Skip non-candidates
107                  */
108                 if ( candidates[ i ].sr_tag != META_CANDIDATE ) {
109                         continue;
110                 }
111
112                 if ( gotit == 0 ) {
113                         gotit = 1;
114
115                 } else if ( isroot == 0 ) {
116                         /*
117                          * A bind operation is expected to have
118                          * ONE CANDIDATE ONLY!
119                          */
120                         Debug( LDAP_DEBUG_ANY,
121                                         "==>meta_back_bind: more than one"
122                                         " candidate is trying to bind...\n",
123                                         0, 0, 0 );
124                 }
125
126                 if ( isroot && !BER_BVISNULL( &mi->mi_targets[ i ].mt_pseudorootdn ) )
127                 {
128                         op2.o_req_dn = mi->mi_targets[ i ].mt_pseudorootdn;
129                         op2.o_req_ndn = mi->mi_targets[ i ].mt_pseudorootdn;
130                         op2.orb_cred = mi->mi_targets[ i ].mt_pseudorootpw;
131                         op2.orb_method = LDAP_AUTH_SIMPLE;
132                 }
133                 
134                 lerr = meta_back_single_bind( &op2, rs, mc, i );
135                 if ( lerr != LDAP_SUCCESS ) {
136                         rs->sr_err = lerr;
137                         candidates[ i ].sr_tag = META_NOT_CANDIDATE;
138
139                         if ( META_BACK_ONERR_STOP( mi ) ) {
140                                 rc = rs->sr_err;
141                                 break;
142                         }
143                                 
144                 } else {
145                         rc = LDAP_SUCCESS;
146                 }
147         }
148
149         if ( isroot && rc == LDAP_SUCCESS ) {
150                 mc->mc_auth_target = META_BOUND_ALL;
151                 ber_dupbv( &op->orb_edn, be_root_dn( op->o_bd ) );
152         }
153
154         meta_back_release_conn( op, mc );
155
156         /*
157          * rc is LDAP_SUCCESS if at least one bind succeeded,
158          * err is the last error that occurred during a bind;
159          * if at least (and at most?) one bind succeedes, fine.
160          */
161         if ( rc != LDAP_SUCCESS ) {
162                 
163                 /*
164                  * deal with bind failure ...
165                  */
166
167                 /*
168                  * no target was found within the naming context, 
169                  * so bind must fail with invalid credentials
170                  */
171                 if ( rs->sr_err == LDAP_SUCCESS && gotit == 0 ) {
172                         rs->sr_err = LDAP_INVALID_CREDENTIALS;
173                 }
174
175                 rs->sr_err = slap_map_api2result( rs );
176                 send_ldap_result( op, rs );
177                 return rs->sr_err;
178         }
179
180         return LDAP_SUCCESS;
181 }
182
183 /*
184  * meta_back_single_bind
185  *
186  * attempts to perform a bind with creds
187  */
188 static int
189 meta_back_single_bind(
190         Operation               *op,
191         SlapReply               *rs,
192         metaconn_t              *mc,
193         int                     candidate )
194 {
195         metainfo_t              *mi = ( metainfo_t * )op->o_bd->be_private;
196         metatarget_t            *mt = &mi->mi_targets[ candidate ];
197         struct berval           mdn = BER_BVNULL;
198         dncookie                dc;
199         metasingleconn_t        *msc = &mc->mc_conns[ candidate ];
200         int                     msgid,
201                                 rebinding = 0;
202         
203         /*
204          * Rewrite the bind dn if needed
205          */
206         dc.target = &mi->mi_targets[ candidate ];
207         dc.conn = op->o_conn;
208         dc.rs = rs;
209         dc.ctx = "bindDN";
210
211         if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
212                 send_ldap_result( op, rs );
213                 return -1;
214         }
215
216         /* FIXME: this fixes the bind problem right now; we need
217          * to use the asynchronous version to get the "matched"
218          * and more in case of failure ... */
219         /* FIXME: should be check if at least some of the op->o_ctrls
220          * can/should be passed? */
221 rebind:;
222         rs->sr_err = ldap_sasl_bind( msc->msc_ld, mdn.bv_val,
223                         LDAP_SASL_SIMPLE, &op->orb_cred,
224                         op->o_ctrls, NULL, &msgid );
225         if ( rs->sr_err == LDAP_SUCCESS ) {
226                 LDAPMessage     *res;
227                 struct timeval  tv = { 0, 100000 };
228                 int             rc;
229                 int             nretries = mt->mt_nretries;
230
231                 /*
232                  * handle response!!!
233                  */
234 retry:;
235                 tv.tv_sec = 0;
236                 tv.tv_usec = META_BIND_TIMEOUT;
237                 switch ( ldap_result( msc->msc_ld, msgid, 0, &tv, &res ) ) {
238                 case 0:
239                         Debug( LDAP_DEBUG_ANY,
240                                 "%s meta_back_single_bind: "
241                                 "ldap_result=0 nretries=%d%s\n",
242                                 op->o_log_prefix, nretries,
243                                 rebinding ? " rebinding" : "" );
244
245                         if ( nretries != META_RETRY_NEVER ) {
246                                 ldap_pvt_thread_yield();
247                                 if ( nretries > 0 ) {
248                                         nretries--;
249                                 }
250                                 tv.tv_sec = 0;
251                                 tv.tv_usec = 100000;
252                                 goto retry;
253                         }
254                         rs->sr_err = LDAP_BUSY;
255                         if ( rebinding ) {
256                                 ldap_abandon_ext( msc->msc_ld, msgid, NULL, NULL );
257                                 break;
258                         }
259
260                         /* FIXME: some times the request times out
261                          * while the other party is not willing to
262                          * send a response any more.  Give it a second
263                          * chance with a freshly bound connection */
264                         rebinding = 1;
265                         nretries = mt->mt_nretries;
266                         /* fallthru */
267
268                 case -1:
269                         ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER,
270                                         &rs->sr_err );
271
272                         if ( rebinding ) {
273                                 ldap_abandon_ext( msc->msc_ld, msgid, NULL, NULL );
274                         }
275
276                         Debug( LDAP_DEBUG_ANY,
277                                 "### %s meta_back_single_bind: "
278                                 "err=%d nretries=%d\n",
279                                 op->o_log_prefix, rs->sr_err, nretries );
280
281                         rc = slap_map_api2result( rs );
282                         if ( rs->sr_err == LDAP_UNAVAILABLE && nretries != META_RETRY_NEVER ) {
283                                 ldap_pvt_thread_mutex_lock( &mi->mi_conn_mutex );
284                                 if ( mc->mc_refcnt == 1 ) {
285                                         ldap_unbind_ext_s( msc->msc_ld, NULL, NULL );
286                                         msc->msc_ld = NULL;
287                                         msc->msc_bound = 0;
288
289                                         ( void )rewrite_session_delete( mt->mt_rwmap.rwm_rw, op->o_conn );
290
291                                         /* mc here must be the regular mc,
292                                          * reset and ready for init */
293                                         rc = meta_back_init_one_conn( op, rs,
294                                                 mt, msc, LDAP_BACK_DONTSEND );
295
296                                 } else {
297                                         /* can't do anything about it */
298                                         rc = 0;
299                                 }
300
301                                 ldap_pvt_thread_mutex_unlock( &mi->mi_conn_mutex );
302
303                                 if ( rc ) {
304                                         if ( nretries > 0 ) {
305                                                 nretries--;
306                                         }
307                                         ldap_pvt_thread_yield();
308                                         goto rebind;
309                                 }
310                         }
311                         break;
312
313                 default:
314                         rc = ldap_parse_result( msc->msc_ld, res, &rs->sr_err,
315                                         NULL, NULL, NULL, NULL, 1 );
316                         if ( rc != LDAP_SUCCESS ) {
317                                 rs->sr_err = rc;
318                         }
319                         break;
320                 }
321         }
322
323         if ( rs->sr_err != LDAP_SUCCESS ) {
324                 rs->sr_err = slap_map_api2result( rs );
325                 goto return_results;
326         }
327
328         ber_bvreplace( &msc->msc_bound_ndn, &op->o_req_dn );
329         msc->msc_bound = META_BOUND;
330         mc->mc_auth_target = candidate;
331
332         if ( LDAP_BACK_SAVECRED( mi ) ) {
333                 ber_bvreplace( &msc->msc_cred, &op->orb_cred );
334                 ldap_set_rebind_proc( msc->msc_ld, meta_back_rebind, msc );
335         }
336
337         if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED
338                         && op->o_req_ndn.bv_len != 0 )
339         {
340                 ( void )meta_dncache_update_entry( &mi->mi_cache,
341                                 &op->o_req_ndn, candidate );
342         }
343
344 return_results:;
345         if ( mdn.bv_val != op->o_req_dn.bv_val ) {
346                 free( mdn.bv_val );
347         }
348
349         return rs->sr_err;
350 }
351
352 /*
353  * meta_back_single_dobind
354  */
355 int
356 meta_back_single_dobind(
357         Operation               *op,
358         SlapReply               *rs,
359         metaconn_t              *mc,
360         int                     candidate,
361         ldap_back_send_t        sendok,
362         int                     nretries,
363         int                     dolock )
364 {
365         metainfo_t              *mi = ( metainfo_t * )op->o_bd->be_private;
366         metatarget_t            *mt = &mi->mi_targets[ candidate ];
367         metasingleconn_t        *msc = &mc->mc_conns[ candidate ];
368         int                     rc;
369         struct berval           cred = BER_BVC( "" );
370         int                     msgid,
371                                 rebinding = 0,
372                                 save_nretries = nretries;
373
374         /*
375          * Otherwise an anonymous bind is performed
376          * (note: if the target was already bound, the anonymous
377          * bind clears the previous bind).
378          */
379         if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
380                 ber_memfree( msc->msc_bound_ndn.bv_val );
381                 BER_BVZERO( &msc->msc_bound_ndn );
382         }
383                 
384         if ( !BER_BVISNULL( &msc->msc_cred ) ) {
385                 /* destroy sensitive data */
386                 memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
387                 ber_memfree( msc->msc_cred.bv_val );
388                 BER_BVZERO( &msc->msc_cred );
389         }
390
391         /* FIXME: should we check if at least some of the op->o_ctrls
392          * can/should be passed? */
393 rebind:;
394         rc = ldap_sasl_bind( msc->msc_ld, "", LDAP_SASL_SIMPLE, &cred,
395                         NULL, NULL, &msgid );
396         if ( rc == LDAP_SUCCESS ) {
397                 LDAPMessage     *res;
398                 struct timeval  tv = { 0, 100000 };
399
400                 /*
401                  * handle response!!!
402                  */
403 retry:;
404                 tv.tv_sec = 0;
405                 tv.tv_usec = META_BIND_TIMEOUT;
406                 switch ( ldap_result( msc->msc_ld, msgid, 0, &tv, &res ) ) {
407                 case 0:
408                         Debug( LDAP_DEBUG_ANY,
409                                 "%s meta_back_single_dobind: "
410                                 "ldap_result=0 nretries=%d%s\n",
411                                 op->o_log_prefix, nretries,
412                                 rebinding ? " rebinding" : "" );
413
414                         if ( nretries != META_RETRY_NEVER ) {
415                                 ldap_pvt_thread_yield();
416                                 if ( nretries > 0 ) {
417                                         nretries--;
418                                 }
419                                 tv.tv_sec = 0;
420                                 tv.tv_usec = 100000;
421                                 goto retry;
422                         }
423
424                         rc = LDAP_BUSY;
425                         if ( rebinding ) {
426                                 ldap_abandon_ext( msc->msc_ld, msgid, NULL, NULL );
427                                 break;
428                         }
429
430                         /* FIXME: some times the request times out
431                          * while the other party is not willing to
432                          * send a response any more.  Give it a second
433                          * chance with a freshly bound connection */
434                         rebinding = 1;
435                         nretries = save_nretries;
436                         /* fallthru */
437
438                 case -1:
439                         ldap_get_option( msc->msc_ld,
440                                         LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
441
442                         if ( rebinding ) {
443                                 ldap_abandon_ext( msc->msc_ld, msgid, NULL, NULL );
444                         }
445
446                         Debug( LDAP_DEBUG_ANY,
447                                 "### %s meta_back_single_dobind: "
448                                 "err=%d nretries=%d\n",
449                                 op->o_log_prefix, rs->sr_err, nretries );
450
451                         rc = slap_map_api2result( rs );
452                         if ( rc == LDAP_UNAVAILABLE && nretries != META_RETRY_NEVER ) {
453                                 if ( dolock ) {
454                                         ldap_pvt_thread_mutex_lock( &mi->mi_conn_mutex );
455                                 }
456
457                                 if ( mc->mc_refcnt == 1 ) {
458                                         ldap_unbind_ext_s( msc->msc_ld, NULL, NULL );
459                                         msc->msc_ld = NULL;
460                                         msc->msc_bound = 0;
461
462                                         ( void )rewrite_session_delete( mt->mt_rwmap.rwm_rw, op->o_conn );
463
464                                         /* mc here must be the regular mc,
465                                          * reset and ready for init */
466                                         rc = meta_back_init_one_conn( op, rs,
467                                                 mt, msc, LDAP_BACK_DONTSEND );
468                                 
469
470                                 } else {
471                                         /* can't do anything about it */
472                                         rc = LDAP_UNAVAILABLE;
473                                 }
474
475                                 if ( dolock ) {
476                                         ldap_pvt_thread_mutex_unlock( &mi->mi_conn_mutex );
477                                 }
478
479                                 if ( rc == LDAP_SUCCESS ) {
480                                         ldap_pvt_thread_yield();
481                                         if ( nretries > 0 ) {
482                                                 nretries--;
483                                         }
484                                         goto rebind;
485                                 }
486                         }
487                         break;
488
489                 default:
490                         rc = ldap_parse_result( msc->msc_ld, res, &rs->sr_err,
491                                         NULL, NULL, NULL, NULL, 1 );
492                         if ( rc == LDAP_SUCCESS ) {
493                                 rc = slap_map_api2result( rs );
494                         }
495                         break;
496                 }
497
498         } else {
499                 rs->sr_err = rc;
500                 rc = slap_map_api2result( rs );
501         }
502
503         rs->sr_err = rc;
504         if ( rc != LDAP_SUCCESS && ( sendok & LDAP_BACK_SENDERR ) ) {
505                 send_ldap_result( op, rs );
506         }
507
508         return rc;
509 }
510
511 /*
512  * meta_back_dobind
513  */
514 int
515 meta_back_dobind(
516         Operation               *op,
517         SlapReply               *rs,
518         metaconn_t              *mc,
519         ldap_back_send_t        sendok )
520 {
521         metainfo_t              *mi = ( metainfo_t * )op->o_bd->be_private;
522
523         int                     bound = 0,
524                                 i,
525                                 isroot = 0;
526
527         SlapReply               *candidates = meta_back_candidates_get( op );
528
529         if ( be_isroot( op ) ) {
530                 isroot = 1;
531         }
532
533         Debug( LDAP_DEBUG_TRACE,
534                 "%s meta_back_dobind: conn=%ld%s\n",
535                 op->o_log_prefix, mc->mc_conn->c_connid,
536                 isroot ? " (isroot)" : "" );
537
538         ldap_pvt_thread_mutex_lock( &mc->mc_mutex );
539
540         /*
541          * all the targets are bound as pseudoroot
542          */
543         if ( mc->mc_auth_target == META_BOUND_ALL ) {
544                 bound = 1;
545                 goto done;
546         }
547
548         for ( i = 0; i < mi->mi_ntargets; i++ ) {
549                 metatarget_t            *mt = &mi->mi_targets[ i ];
550                 metasingleconn_t        *msc = &mc->mc_conns[ i ];
551                 int                     rc;
552
553                 /*
554                  * Not a candidate
555                  */
556                 if ( candidates[ i ].sr_tag != META_CANDIDATE ) {
557                         continue;
558                 }
559
560                 assert( msc->msc_ld != NULL );
561
562                 /*
563                  * If the target is already bound it is skipped
564                  */
565                 if ( msc->msc_bound == META_BOUND && mc->mc_auth_target == i ) {
566                         ++bound;
567
568                         Debug( LDAP_DEBUG_TRACE, "%s meta_back_dobind[%d]: "
569                                         "authcTarget\n",
570                                         op->o_log_prefix, i, 0 );
571                         continue;
572                 }
573
574                 if ( isroot && !BER_BVISNULL( &mi->mi_targets[ i ].mt_pseudorootdn ) )
575                 {
576                         Operation       op2 = *op;
577
578                         op2.o_tag = LDAP_REQ_BIND;
579                         op2.o_req_dn = mi->mi_targets[ i ].mt_pseudorootdn;
580                         op2.o_req_ndn = mi->mi_targets[ i ].mt_pseudorootdn;
581                         op2.orb_cred = mi->mi_targets[ i ].mt_pseudorootpw;
582                         op2.orb_method = LDAP_AUTH_SIMPLE;
583
584                         rc = meta_back_single_bind( &op2, rs, mc, i );
585
586                 } else {
587                         rc = meta_back_single_dobind( op, rs, mc, i,
588                                 LDAP_BACK_DONTSEND, mt->mt_nretries, 1 );
589                 }
590
591                 if ( rc != LDAP_SUCCESS ) {
592                         Debug( LDAP_DEBUG_ANY, "%s meta_back_dobind[%d]: "
593                                         "(anonymous) err=%d\n",
594                                         op->o_log_prefix, i, rc );
595
596                         /*
597                          * null cred bind should always succeed
598                          * as anonymous, so a failure means
599                          * the target is no longer candidate possibly
600                          * due to technical reasons (remote host down?)
601                          * so better clear the handle
602                          */
603                         /* leave the target candidate, but record the error for later use */
604                         candidates[ i ].sr_err = rc;
605                         if ( META_BACK_ONERR_STOP( mi ) ) {
606                                 bound = 0;
607                                 goto done;
608                         }
609                         continue;
610                 } /* else */
611                 
612                 Debug( LDAP_DEBUG_TRACE, "%s meta_back_dobind[%d]: "
613                                 "(anonymous)\n",
614                                 op->o_log_prefix, i, 0 );
615
616                 msc->msc_bound = META_ANONYMOUS;
617                 ++bound;
618         }
619
620 done:;
621         ldap_pvt_thread_mutex_unlock( &mc->mc_mutex );
622
623         Debug( LDAP_DEBUG_TRACE,
624                 "%s meta_back_dobind: conn=%ld bound=%d\n",
625                 op->o_log_prefix, mc->mc_conn->c_connid, bound );
626
627         if ( bound == 0 ) {
628                 meta_back_release_conn( op, mc );
629
630                 if ( sendok & LDAP_BACK_SENDERR ) {
631                         if ( rs->sr_err == LDAP_SUCCESS ) {
632                                 rs->sr_err = LDAP_BUSY;
633                         }
634                         send_ldap_result( op, rs );
635                 }
636         }
637
638         return( bound > 0 );
639 }
640
641 /*
642  * meta_back_rebind
643  *
644  * This is a callback used for chasing referrals using the same
645  * credentials as the original user on this session.
646  */
647 static int 
648 meta_back_rebind(
649         LDAP                    *ld,
650         LDAP_CONST char         *url,
651         ber_tag_t               request,
652         ber_int_t               msgid,
653         void                    *params )
654 {
655         metasingleconn_t        *msc = ( metasingleconn_t * )params;
656
657         return ldap_sasl_bind_s( ld, msc->msc_bound_ndn.bv_val,
658                         LDAP_SASL_SIMPLE, &msc->msc_cred,
659                         NULL, NULL, NULL );
660 }
661
662 /*
663  * FIXME: error return must be handled in a cleaner way ...
664  */
665 int
666 meta_back_op_result(
667         metaconn_t      *mc,
668         Operation       *op,
669         SlapReply       *rs,
670         int             candidate )
671 {
672         metainfo_t              *mi = ( metainfo_t * )op->o_bd->be_private;
673
674         int                     i,
675                                 rerr = LDAP_SUCCESS;
676         char                    *rmsg = NULL,
677                                 *rmatch = NULL;
678         const char              *save_rmsg = NULL,
679                                 *save_rmatch = NULL;
680         void                    *rmatch_ctx = NULL;
681
682         if ( candidate != META_TARGET_NONE ) {
683                 metasingleconn_t        *msc = &mc->mc_conns[ candidate ];
684
685                 rs->sr_err = LDAP_SUCCESS;
686
687                 ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
688                 if ( rs->sr_err != LDAP_SUCCESS ) {
689                         /*
690                          * better check the type of error. In some cases
691                          * (search ?) it might be better to return a
692                          * success if at least one of the targets gave
693                          * positive result ...
694                          */
695                         ldap_get_option( msc->msc_ld,
696                                         LDAP_OPT_ERROR_STRING, &rmsg );
697                         if ( rmsg != NULL && rmsg[ 0 ] == '\0' ) {
698                                 ldap_memfree( rmsg );
699                                 rmsg = NULL;
700                         }
701
702                         ldap_get_option( msc->msc_ld,
703                                         LDAP_OPT_MATCHED_DN, &rmatch );
704                         if ( rmatch != NULL && rmatch[ 0 ] == '\0' ) {
705                                 ldap_memfree( rmatch );
706                                 rmatch = NULL;
707                         }
708
709                         rerr = rs->sr_err = slap_map_api2result( rs );
710
711                         Debug(LDAP_DEBUG_ANY,
712                                         "==> meta_back_op_result: target"
713                                         " <%d> sending msg \"%s\""
714                                         " (matched \"%s\")\n", 
715                                         candidate, ( rmsg ? rmsg : "" ),
716                                         ( rmatch ? rmatch : "" ) );
717                 }
718
719         } else {
720                 for ( i = 0; i < mi->mi_ntargets; i++ ) {
721                         metasingleconn_t        *msc = &mc->mc_conns[ i ];
722                         char                    *msg = NULL;
723                         char                    *match = NULL;
724
725                         rs->sr_err = LDAP_SUCCESS;
726
727                         ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
728                         if ( rs->sr_err != LDAP_SUCCESS ) {
729                                 /*
730                                  * better check the type of error. In some cases
731                                  * (search ?) it might be better to return a
732                                  * success if at least one of the targets gave
733                                  * positive result ...
734                                  */
735                                 ldap_get_option( msc->msc_ld,
736                                                 LDAP_OPT_ERROR_STRING, &msg );
737                                 if ( msg != NULL && msg[ 0 ] == '\0' ) {
738                                         ldap_memfree( msg );
739                                         msg = NULL;
740                                 }
741
742                                 ldap_get_option( msc->msc_ld,
743                                                 LDAP_OPT_MATCHED_DN, &match );
744                                 if ( match != NULL && match[ 0 ] == '\0' ) {
745                                         ldap_memfree( match );
746                                         match = NULL;
747                                 }
748
749                                 rs->sr_err = slap_map_api2result( rs );
750         
751                                 Debug(LDAP_DEBUG_ANY,
752                                                 "==> meta_back_op_result: target"
753                                                 " <%d> sending msg \"%s\""
754                                                 " (matched \"%s\")\n", 
755                                                 i, ( msg ? msg : "" ),
756                                                 ( match ? match : "" ) );
757         
758                                 /*
759                                  * FIXME: need to rewrite "match" (need rwinfo)
760                                  */
761                                 switch ( rs->sr_err ) {
762                                 default:
763                                         rerr = rs->sr_err;
764                                         if ( msg != NULL ) {
765                                                 if ( rmsg ) {
766                                                         ldap_memfree( rmsg );
767                                                 }
768                                                 rmsg = msg;
769                                                 msg = NULL;
770                                         }
771                                         if ( match != NULL ) {
772                                                 if ( rmatch ) {
773                                                         ldap_memfree( rmatch );
774                                                 }
775                                                 rmatch = match;
776                                                 match = NULL;
777                                         }
778                                         break;
779                                 }
780
781                                 if ( msg ) {
782                                         ldap_memfree( msg );
783                                 }
784         
785                                 if ( match ) {
786                                         ldap_memfree( match );
787                                 }
788                         }
789                 }
790         }
791         
792         rs->sr_err = rerr;
793         if ( rmsg != NULL ) {
794                 save_rmsg = rs->sr_text;
795                 rs->sr_text = rmsg;
796         }
797         if ( rmatch != NULL ) {
798                 struct berval   dn, pdn;
799
800                 ber_str2bv( rmatch, 0, 0, &dn );
801                 if ( dnPretty( NULL, &dn, &pdn, op->o_tmpmemctx ) == LDAP_SUCCESS ) {
802                         ldap_memfree( rmatch );
803                         rmatch_ctx = op->o_tmpmemctx;
804                         rmatch = pdn.bv_val;
805                 }
806                 save_rmatch = rs->sr_matched;
807                 rs->sr_matched = rmatch;
808         }
809         send_ldap_result( op, rs );
810         if ( rmsg != NULL ) {
811                 ber_memfree( rmsg );
812                 rs->sr_text = save_rmsg;
813         }
814         if ( rmatch != NULL ) {
815                 ber_memfree_x( rmatch, rmatch_ctx );
816                 rs->sr_matched = save_rmatch;
817         }
818
819         return ( ( rerr == LDAP_SUCCESS ) ? 0 : -1 );
820 }
821