]> git.sur5r.net Git - openldap/blob - servers/slapd/result.c
bedbc26a1db813ae657c9b7ca39f7b8713b15440
[openldap] / servers / slapd / result.c
1 /* result.c - routines to send ldap results, errors, and referrals */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2004 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17  * All rights reserved.
18  *
19  * Redistribution and use in source and binary forms are permitted
20  * provided that this notice is preserved and that due credit is given
21  * to the University of Michigan at Ann Arbor. The name of the University
22  * may not be used to endorse or promote products derived from this
23  * software without specific prior written permission. This software
24  * is provided ``as is'' without express or implied warranty.
25  */
26
27 #include "portable.h"
28
29 #include <stdio.h>
30
31 #include <ac/socket.h>
32 #include <ac/errno.h>
33 #include <ac/string.h>
34 #include <ac/ctype.h>
35 #include <ac/time.h>
36 #include <ac/unistd.h>
37
38 #include "slap.h"
39
40 #ifdef LDAP_SLAPI
41 #include "slapi/slapi.h"
42 #endif
43
44 const struct berval slap_dummy_bv = BER_BVNULL;
45
46 int slap_null_cb( Operation *op, SlapReply *rs )
47 {
48         return 0;
49 }
50
51 int slap_freeself_cb( Operation *op, SlapReply *rs )
52 {
53         assert( op->o_callback );
54
55         op->o_tmpfree( op->o_callback, op->o_tmpmemctx );
56         op->o_callback = NULL;
57
58         return SLAP_CB_CONTINUE;
59 }
60
61 int slap_replog_cb( Operation *op, SlapReply *rs )
62 {
63         if ( rs->sr_err == LDAP_SUCCESS ) {
64                 replog( op );
65         }
66         return SLAP_CB_CONTINUE;
67 }
68
69 static char *v2ref( BerVarray ref, const char *text )
70 {
71         size_t len = 0, i = 0;
72         char *v2;
73
74         if(ref == NULL) {
75                 if (text) {
76                         return ch_strdup(text);
77                 } else {
78                         return NULL;
79                 }
80         }
81         
82         if ( text != NULL ) {
83                 len = strlen( text );
84                 if (text[len-1] != '\n') {
85                     i = 1;
86                 }
87         }
88
89         v2 = SLAP_MALLOC( len+i+sizeof("Referral:") );
90         if( v2 == NULL ) {
91 #ifdef NEW_LOGGING
92                 LDAP_LOG( OPERATION, ERR, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
93 #else
94                 Debug( LDAP_DEBUG_ANY, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
95 #endif
96                 return NULL;
97         }
98
99         if( text != NULL ) {
100                 strcpy(v2, text);
101                 if( i ) {
102                         v2[len++] = '\n';
103                 }
104         }
105         strcpy( v2+len, "Referral:" );
106         len += sizeof("Referral:");
107
108         for( i=0; ref[i].bv_val != NULL; i++ ) {
109                 v2 = SLAP_REALLOC( v2, len + ref[i].bv_len + 1 );
110                 if( v2 == NULL ) {
111 #ifdef NEW_LOGGING
112                         LDAP_LOG( OPERATION, ERR, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
113 #else
114                         Debug( LDAP_DEBUG_ANY, "v2ref: SLAP_MALLOC failed", 0, 0, 0 );
115 #endif
116                         return NULL;
117                 }
118                 v2[len-1] = '\n';
119                 AC_MEMCPY(&v2[len], ref[i].bv_val, ref[i].bv_len );
120                 len += ref[i].bv_len;
121                 if (ref[i].bv_val[ref[i].bv_len-1] != '/') {
122                         ++len;
123                 }
124         }
125
126         v2[len-1] = '\0';
127         return v2;
128 }
129
130 static ber_tag_t req2res( ber_tag_t tag )
131 {
132         switch( tag ) {
133         case LDAP_REQ_ADD:
134         case LDAP_REQ_BIND:
135         case LDAP_REQ_COMPARE:
136         case LDAP_REQ_EXTENDED:
137         case LDAP_REQ_MODIFY:
138         case LDAP_REQ_MODRDN:
139                 tag++;
140                 break;
141
142         case LDAP_REQ_DELETE:
143                 tag = LDAP_RES_DELETE;
144                 break;
145
146         case LDAP_REQ_ABANDON:
147         case LDAP_REQ_UNBIND:
148                 tag = LBER_SEQUENCE;
149                 break;
150
151         case LDAP_REQ_SEARCH:
152                 tag = LDAP_RES_SEARCH_RESULT;
153                 break;
154
155         default:
156                 tag = LBER_SEQUENCE;
157         }
158
159         return tag;
160 }
161
162 static long send_ldap_ber(
163         Connection *conn,
164         BerElement *ber )
165 {
166         ber_len_t bytes;
167
168         ber_get_option( ber, LBER_OPT_BER_BYTES_TO_WRITE, &bytes );
169
170         /* write only one pdu at a time - wait til it's our turn */
171         ldap_pvt_thread_mutex_lock( &conn->c_write_mutex );
172
173         /* lock the connection */ 
174         ldap_pvt_thread_mutex_lock( &conn->c_mutex );
175
176         /* write the pdu */
177         while( 1 ) {
178                 int err;
179                 ber_socket_t    sd;
180
181                 if ( connection_state_closing( conn ) ) {
182                         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
183                         ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
184
185                         return 0;
186                 }
187
188                 if ( ber_flush( conn->c_sb, ber, 0 ) == 0 ) {
189                         break;
190                 }
191
192                 err = errno;
193
194                 /*
195                  * we got an error.  if it's ewouldblock, we need to
196                  * wait on the socket being writable.  otherwise, figure
197                  * it's a hard error and return.
198                  */
199
200 #ifdef NEW_LOGGING
201                 LDAP_LOG( OPERATION, ERR, 
202                         "send_ldap_ber: conn %lu  ber_flush failed err=%d (%s)\n",
203                         conn ? conn->c_connid : 0, err, sock_errstr(err) );
204 #else
205                 Debug( LDAP_DEBUG_CONNS, "ber_flush failed errno=%d reason=\"%s\"\n",
206                     err, sock_errstr(err), 0 );
207 #endif
208
209                 if ( err != EWOULDBLOCK && err != EAGAIN ) {
210                         connection_closing( conn );
211
212                         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
213                         ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
214
215                         return( -1 );
216                 }
217
218                 /* wait for socket to be write-ready */
219                 conn->c_writewaiter = 1;
220                 ber_sockbuf_ctrl( conn->c_sb, LBER_SB_OPT_GET_FD, &sd );
221                 slapd_set_write( sd, 1 );
222
223                 ldap_pvt_thread_cond_wait( &conn->c_write_cv, &conn->c_mutex );
224                 conn->c_writewaiter = 0;
225         }
226
227         ldap_pvt_thread_mutex_unlock( &conn->c_mutex );
228         ldap_pvt_thread_mutex_unlock( &conn->c_write_mutex );
229
230         return bytes;
231 }
232
233 static int
234 send_ldap_control( BerElement *ber, LDAPControl *c )
235 {
236         int rc;
237
238         assert( c != NULL );
239
240         rc = ber_printf( ber, "{s" /*}*/, c->ldctl_oid );
241
242         if( c->ldctl_iscritical ) {
243                 rc = ber_printf( ber, "b",
244                         (ber_int_t) c->ldctl_iscritical ) ;
245                 if( rc == -1 ) return rc;
246         }
247
248         if( c->ldctl_value.bv_val != NULL ) {
249                 rc = ber_printf( ber, "O", &c->ldctl_value ); 
250                 if( rc == -1 ) return rc;
251         }
252
253         rc = ber_printf( ber, /*{*/"N}" );
254         if( rc == -1 ) return rc;
255
256         return 0;
257 }
258
259 static int
260 send_ldap_controls( Operation *o, BerElement *ber, LDAPControl **c )
261 {
262         int rc;
263 #ifdef LDAP_SLAPI
264         LDAPControl **sctrls = NULL;
265
266         /*
267          * Retrieve any additional controls that may be set by the
268          * plugin.
269          */
270
271         if ( o->o_pb && slapi_pblock_get( o->o_pb, SLAPI_RESCONTROLS, &sctrls ) != 0 ) {
272                 sctrls = NULL;
273         }
274
275         if ( c == NULL && sctrls == NULL ) return 0;
276 #else
277         if( c == NULL ) return 0;
278 #endif /* LDAP_SLAPI */
279
280         rc = ber_printf( ber, "t{"/*}*/, LDAP_TAG_CONTROLS );
281         if( rc == -1 ) return rc;
282
283 #ifdef LDAP_SLAPI
284         if ( c != NULL )
285 #endif /* LDAP_SLAPI */
286         for( ; *c != NULL; c++) {
287                 rc = send_ldap_control( ber, *c );
288                 if( rc == -1 ) return rc;
289         }
290
291 #ifdef LDAP_SLAPI
292         if ( sctrls != NULL ) {
293                 for ( c = sctrls; *c != NULL; c++ ) {
294                         rc = send_ldap_control( ber, *c );
295                         if( rc == -1 ) return rc;
296                 }
297         }
298 #endif /* LDAP_SLAPI */
299
300         rc = ber_printf( ber, /*{*/"N}" );
301
302         return rc;
303 }
304
305 static int
306 send_ldap_response(
307         Operation *op,
308         SlapReply *rs )
309 {
310         BerElementBuffer berbuf;
311         BerElement      *ber = (BerElement *) &berbuf;
312         int             rc = LDAP_SUCCESS;
313         long    bytes;
314
315         if ( op->o_callback ) {
316                 int             first = 1;
317                 slap_callback   *sc = op->o_callback,
318                                 *sc_next = op->o_callback;
319
320                 rc = SLAP_CB_CONTINUE;
321                 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
322                         sc_next = op->o_callback->sc_next;
323                         if ( op->o_callback->sc_response ) {
324                                 rc = op->o_callback->sc_response( op, rs );
325                                 if ( first && op->o_callback == NULL ) {
326                                         sc = NULL;
327                                 }
328                                 if ( rc != SLAP_CB_CONTINUE ) break;
329                         }
330                         first = 0;
331                 }
332
333                 op->o_callback = sc;
334                 if ( rc != SLAP_CB_CONTINUE ) goto clean2;
335         }
336
337 #ifdef LDAP_CONNECTIONLESS
338         if (op->o_conn && op->o_conn->c_is_udp)
339                 ber = op->o_res_ber;
340         else
341 #endif
342         {
343                 ber_init_w_nullc( ber, LBER_USE_DER );
344                 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
345         }
346
347 #ifdef NEW_LOGGING
348         LDAP_LOG( OPERATION, ENTRY, 
349                 "send_ldap_response:  msgid=%d tag=%lu err=%d\n",
350                 rs->sr_msgid, rs->sr_tag, rs->sr_err );
351 #else
352         Debug( LDAP_DEBUG_TRACE,
353                 "send_ldap_response: msgid=%d tag=%lu err=%d\n",
354                 rs->sr_msgid, rs->sr_tag, rs->sr_err );
355 #endif
356
357         if( rs->sr_ref ) {
358 #ifdef NEW_LOGGING
359                 LDAP_LOG( OPERATION, ARGS, 
360                         "send_ldap_response: conn %lu  ref=\"%s\"\n",
361                         op->o_connid,
362                         rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL" , 0 );
363 #else
364                 Debug( LDAP_DEBUG_ARGS, "send_ldap_response: ref=\"%s\"\n",
365                         rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
366                         NULL, NULL );
367 #endif
368         }
369
370 #ifdef LDAP_CONNECTIONLESS
371         if (op->o_conn && op->o_conn->c_is_udp &&
372                 op->o_protocol == LDAP_VERSION2 )
373         {
374                 rc = ber_printf( ber, "t{ess" /*"}"*/,
375                         rs->sr_tag, rs->sr_err,
376                 rs->sr_matched == NULL ? "" : rs->sr_matched,
377                 rs->sr_text == NULL ? "" : rs->sr_text );
378         } else 
379 #endif
380         if ( rs->sr_type == REP_INTERMEDIATE ) {
381             rc = ber_printf( ber, "{it{" /*"}}"*/,
382                         rs->sr_msgid, rs->sr_tag );
383
384         } else {
385             rc = ber_printf( ber, "{it{ess" /*"}}"*/,
386                 rs->sr_msgid, rs->sr_tag, rs->sr_err,
387                 rs->sr_matched == NULL ? "" : rs->sr_matched,
388                 rs->sr_text == NULL ? "" : rs->sr_text );
389         }
390
391         if( rc != -1 ) {
392                 if ( rs->sr_ref != NULL ) {
393                         assert( rs->sr_err == LDAP_REFERRAL );
394                         rc = ber_printf( ber, "t{W}",
395                                 LDAP_TAG_REFERRAL, rs->sr_ref );
396                 } else {
397                         assert( rs->sr_err != LDAP_REFERRAL );
398                 }
399         }
400
401         if( rc != -1 && rs->sr_type == REP_SASL && rs->sr_sasldata != NULL ) {
402                 rc = ber_printf( ber, "tO",
403                         LDAP_TAG_SASL_RES_CREDS, rs->sr_sasldata );
404         }
405
406         if( rc != -1 &&
407                 ( rs->sr_type == REP_EXTENDED || rs->sr_type == REP_INTERMEDIATE ))
408         {
409                 if ( rs->sr_rspoid != NULL ) {
410                         rc = ber_printf( ber, "ts",
411                                 LDAP_TAG_EXOP_RES_OID, rs->sr_rspoid );
412                 }
413                 if( rc != -1 && rs->sr_rspdata != NULL ) {
414                         rc = ber_printf( ber, "tO",
415                                 LDAP_TAG_EXOP_RES_VALUE, rs->sr_rspdata );
416                 }
417         }
418
419         if( rc != -1 ) {
420                 rc = ber_printf( ber, /*"{"*/ "N}" );
421         }
422
423         if( rc != -1 ) {
424                 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
425         }
426
427         if( rc != -1 ) {
428                 rc = ber_printf( ber, /*"{"*/ "N}" );
429         }
430
431 #ifdef LDAP_CONNECTIONLESS
432         if( op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2
433                 && rc != -1 )
434         {
435                 rc = ber_printf( ber, /*"{"*/ "N}" );
436         }
437 #endif
438                 
439         if ( rc == -1 ) {
440 #ifdef NEW_LOGGING
441                 LDAP_LOG( OPERATION, ERR, 
442                         "send_ldap_response: conn %lu  ber_printf failed\n",
443                         op->o_connid, 0, 0 );
444 #else
445                 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
446 #endif
447
448 #ifdef LDAP_CONNECTIONLESS
449                 if (!op->o_conn || op->o_conn->c_is_udp == 0)
450 #endif
451                 {
452                         ber_free_buf( ber );
453                 }
454                 goto cleanup;
455         }
456
457         /* send BER */
458         bytes = send_ldap_ber( op->o_conn, ber );
459 #ifdef LDAP_CONNECTIONLESS
460         if (!op->o_conn || op->o_conn->c_is_udp == 0)
461 #endif
462         {
463                 ber_free_buf( ber );
464         }
465
466         if ( bytes < 0 ) {
467 #ifdef NEW_LOGGING
468                 LDAP_LOG( OPERATION, ERR, 
469                         "send_ldap_response: conn %lu ber write failed\n",
470                         op->o_connid ? op->o_connid : 0, 0, 0 );
471 #else
472                 Debug( LDAP_DEBUG_ANY,
473                         "send_ldap_response: ber write failed\n",
474                         0, 0, 0 );
475 #endif
476
477                 goto cleanup;
478         }
479
480 #ifdef LDAP_SLAPI
481         if ( op->o_pb ) {
482                 slapi_pblock_set( op->o_pb, SLAPI_RESULT_CODE, (void *)rs->sr_err );
483                 slapi_pblock_set( op->o_pb, SLAPI_RESULT_MATCHED,
484                         (void *)rs->sr_matched );
485                 slapi_pblock_set( op->o_pb, SLAPI_RESULT_TEXT, (void *)rs->sr_text );
486         }
487 #endif /* LDAP_SLAPI */
488
489         ldap_pvt_thread_mutex_lock( &num_sent_mutex );
490         num_bytes_sent += bytes;
491         num_pdu_sent++;
492         ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
493
494 cleanup:;
495         /* Tell caller that we did this for real, as opposed to being
496          * overridden by a callback
497          */
498         rc = SLAP_CB_CONTINUE;
499
500 clean2:;
501         if ( op->o_callback ) {
502                 int             first = 1;
503                 slap_callback   *sc = op->o_callback,
504                                 *sc_next = op->o_callback;
505
506                 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
507                         sc_next = op->o_callback->sc_next;
508                         if ( op->o_callback->sc_cleanup ) {
509                                 (void)op->o_callback->sc_cleanup( op, rs );
510                                 if ( first && op->o_callback == NULL ) {
511                                         sc = NULL;
512                                 }
513                         }
514                         first = 0;
515                 }
516
517                 op->o_callback = sc;
518         }
519
520
521         if ( rs->sr_matched && rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
522                 free( (char *)rs->sr_matched );
523                 rs->sr_matched = NULL;
524         }
525
526         if ( rs->sr_ref && rs->sr_flags & REP_REF_MUSTBEFREED ) {
527                 ber_bvarray_free( rs->sr_ref );
528                 rs->sr_ref = NULL;
529         }
530
531         return rc;
532 }
533
534
535 void
536 send_ldap_disconnect( Operation *op, SlapReply *rs )
537 {
538 #define LDAP_UNSOLICITED_ERROR(e) \
539         (  (e) == LDAP_PROTOCOL_ERROR \
540         || (e) == LDAP_STRONG_AUTH_REQUIRED \
541         || (e) == LDAP_UNAVAILABLE )
542
543         assert( LDAP_UNSOLICITED_ERROR( rs->sr_err ) );
544
545         rs->sr_type = REP_EXTENDED;
546
547 #ifdef NEW_LOGGING
548         LDAP_LOG( OPERATION, ENTRY, 
549                 "send_ldap_disconnect: conn %lu  %d:%s\n",
550                 op->o_connid, rs->sr_err, rs->sr_text ? rs->sr_text : "" );
551 #else
552         Debug( LDAP_DEBUG_TRACE,
553                 "send_ldap_disconnect %d:%s\n",
554                 rs->sr_err, rs->sr_text ? rs->sr_text : "", NULL );
555 #endif
556
557         if ( op->o_protocol < LDAP_VERSION3 ) {
558                 rs->sr_rspoid = NULL;
559                 rs->sr_tag = req2res( op->o_tag );
560                 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
561
562         } else {
563                 rs->sr_rspoid = LDAP_NOTICE_DISCONNECT;
564                 rs->sr_tag = LDAP_RES_EXTENDED;
565                 rs->sr_msgid = 0;
566         }
567
568         if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
569                 Statslog( LDAP_DEBUG_STATS,
570                         "conn=%lu op=%lu DISCONNECT tag=%lu err=%d text=%s\n",
571                         op->o_connid, op->o_opid, rs->sr_tag, rs->sr_err,
572                         rs->sr_text ? rs->sr_text : "" );
573         }
574 }
575
576 void
577 slap_send_ldap_result( Operation *op, SlapReply *rs )
578 {
579         char *tmp = NULL;
580         const char *otext = rs->sr_text;
581         BerVarray oref = rs->sr_ref;
582
583         rs->sr_type = REP_RESULT;
584
585         assert( !LDAP_API_ERROR( rs->sr_err ));
586
587 #ifdef NEW_LOGGING
588         LDAP_LOG( OPERATION, ENTRY, 
589                 "send_ldap_result: conn=%lu op=%lu p=%d\n",
590                 op->o_connid, op->o_opid, op->o_protocol );
591 #else
592         Debug( LDAP_DEBUG_TRACE,
593                 "send_ldap_result: conn=%lu op=%lu p=%d\n",
594                 op->o_connid, op->o_opid, op->o_protocol );
595 #endif
596
597 #ifdef NEW_LOGGING
598         LDAP_LOG( OPERATION, ARGS, 
599                 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
600                 rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
601                 rs->sr_text ? rs->sr_text : "" );
602 #else
603         Debug( LDAP_DEBUG_ARGS,
604                 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
605                 rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
606                 rs->sr_text ? rs->sr_text : "" );
607 #endif
608
609
610         if( rs->sr_ref ) {
611 #ifdef NEW_LOGGING
612                 LDAP_LOG( OPERATION, ARGS, 
613                         "send_ldap_result: referral=\"%s\"\n",
614                         rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL", 0, 0 );
615 #else
616                 Debug( LDAP_DEBUG_ARGS,
617                         "send_ldap_result: referral=\"%s\"\n",
618                         rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
619                         NULL, NULL );
620 #endif
621         }
622
623         assert( rs->sr_err != LDAP_PARTIAL_RESULTS );
624
625         if ( rs->sr_err == LDAP_REFERRAL ) {
626 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
627                 if( op->o_domain_scope ) {
628                         rs->sr_ref = NULL;
629                 }
630 #endif
631                 if( rs->sr_ref == NULL ) {
632                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
633                 } else if ( op->o_protocol < LDAP_VERSION3 ) {
634                         rs->sr_err = LDAP_PARTIAL_RESULTS;
635                 }
636         }
637
638 #ifdef LDAP_SLAPI
639         /*
640          * Call pre-result plugins. To avoid infinite recursion plugins
641          * should just set SLAPI_RESULT_CODE rather than sending a
642          * result if they wish to change the result.
643          */
644         if ( op->o_pb != NULL ) {
645                 slapi_int_pblock_set_operation( op->o_pb, op );
646                 slapi_pblock_set( op->o_pb, SLAPI_RESULT_CODE,
647                         (void *)rs->sr_err );
648                 slapi_pblock_set( op->o_pb, SLAPI_RESULT_TEXT,
649                         (void *)rs->sr_text );
650                 slapi_pblock_set( op->o_pb, SLAPI_RESULT_MATCHED,
651                         (void *)rs->sr_matched );
652
653                 (void) slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_PRE_RESULT_FN,
654                         op->o_pb );
655         }
656 #endif /* LDAP_SLAPI */
657
658         if ( op->o_protocol < LDAP_VERSION3 ) {
659                 tmp = v2ref( rs->sr_ref, rs->sr_text );
660                 rs->sr_text = tmp;
661                 rs->sr_ref = NULL;
662         }
663
664         rs->sr_tag = req2res( op->o_tag );
665         rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
666
667         if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
668                 if ( op->o_tag == LDAP_REQ_SEARCH ) {
669                         char nbuf[64];
670                         snprintf( nbuf, sizeof nbuf, "%d nentries=%d",
671                                 rs->sr_err, rs->sr_nentries );
672
673                         Statslog( LDAP_DEBUG_STATS,
674                         "conn=%lu op=%lu SEARCH RESULT tag=%lu err=%s text=%s\n",
675                                 op->o_connid, op->o_opid, rs->sr_tag, nbuf,
676                                 rs->sr_text ? rs->sr_text : "" );
677                 } else {
678                         Statslog( LDAP_DEBUG_STATS,
679                                 "conn=%lu op=%lu RESULT tag=%lu err=%d text=%s\n",
680                                 op->o_connid, op->o_opid, rs->sr_tag, rs->sr_err,
681                                 rs->sr_text ? rs->sr_text : "" );
682                 }
683         }
684
685         if( tmp != NULL ) ch_free(tmp);
686         rs->sr_text = otext;
687         rs->sr_ref = oref;
688 }
689
690 void
691 send_ldap_sasl( Operation *op, SlapReply *rs )
692 {
693         rs->sr_type = REP_SASL;
694 #ifdef NEW_LOGGING
695         LDAP_LOG( OPERATION, ENTRY, 
696                 "send_ldap_sasl: conn %lu err=%d len=%lu\n",
697                 op->o_connid, rs->sr_err,
698                 rs->sr_sasldata ? rs->sr_sasldata->bv_len : -1 );
699 #else
700         Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl: err=%d len=%ld\n",
701                 rs->sr_err,
702                 rs->sr_sasldata ? (long) rs->sr_sasldata->bv_len : -1, NULL );
703 #endif
704
705         rs->sr_tag = req2res( op->o_tag );
706         rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
707
708         send_ldap_response( op, rs );
709 }
710
711 void
712 slap_send_ldap_extended( Operation *op, SlapReply *rs )
713 {
714         rs->sr_type = REP_EXTENDED;
715
716 #ifdef NEW_LOGGING
717         LDAP_LOG( OPERATION, ENTRY, 
718                 "send_ldap_extended: err=%d oid=%s len=%ld\n",
719                 rs->sr_err, rs->sr_rspoid ? rs->sr_rspoid : "",
720                 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
721 #else
722         Debug( LDAP_DEBUG_TRACE,
723                 "send_ldap_extended: err=%d oid=%s len=%ld\n",
724                 rs->sr_err,
725                 rs->sr_rspoid ? rs->sr_rspoid : "",
726                 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
727 #endif
728
729         rs->sr_tag = req2res( op->o_tag );
730         rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
731
732         send_ldap_response( op, rs );
733 }
734
735 void
736 slap_send_ldap_intermediate( Operation *op, SlapReply *rs )
737 {
738         rs->sr_type = REP_INTERMEDIATE;
739 #ifdef NEW_LOGGING
740         LDAP_LOG( OPERATION, ENTRY,
741                 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
742                 rs->sr_err, rs->sr_rspoid ? rs->sr_rspoid : "",
743                 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
744 #else
745         Debug( LDAP_DEBUG_TRACE,
746                 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
747                 rs->sr_err,
748                 rs->sr_rspoid ? rs->sr_rspoid : "",
749                 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
750 #endif
751         rs->sr_tag = LDAP_RES_INTERMEDIATE;
752         rs->sr_msgid = op->o_msgid;
753         send_ldap_response( op, rs );
754 }
755
756 int
757 slap_send_search_entry( Operation *op, SlapReply *rs )
758 {
759         BerElementBuffer berbuf;
760         BerElement      *ber = (BerElement *) &berbuf;
761         Attribute       *a;
762         int             i, j, rc=-1, bytes;
763         char            *edn;
764         int             userattrs;
765         AccessControlState acl_state = ACL_STATE_INIT;
766 #ifdef LDAP_SLAPI
767         /* Support for computed attribute plugins */
768         computed_attr_context    ctx;
769         AttributeName   *anp;
770 #endif
771         AttributeDescription *ad_entry = slap_schema.si_ad_entry;
772
773         /* a_flags: array of flags telling if the i-th element will be
774          *          returned or filtered out
775          * e_flags: array of a_flags
776          */
777         char **e_flags = NULL;
778         
779         rs->sr_type = REP_SEARCH;
780
781         /* eventually will loop through generated operational attributes */
782         /* only subschemaSubentry and numSubordinates are implemented */
783         /* NOTE: moved before overlays callback circling because
784          * they may modify entry and other stuff in rs */
785         /* check for special all operational attributes ("+") type */
786         /* FIXME: maybe we could se this flag at the operation level;
787          * however, in principle the caller of send_search_entry() may
788          * change the attribute list at each call */
789         if ( rs->sr_attrs == NULL ) {
790                 rs->sr_attr_flags = ( SLAP_OPATTRS_NO | SLAP_USERATTRS_YES );
791
792         } else {
793                 rs->sr_attr_flags |= an_find( rs->sr_attrs, &AllOper ) ?
794                         SLAP_OPATTRS_YES : SLAP_OPATTRS_NO;
795                 rs->sr_attr_flags |= an_find( rs->sr_attrs, &AllUser ) ?
796                         SLAP_USERATTRS_YES : SLAP_USERATTRS_NO;
797         }
798
799         rc = backend_operational( op, rs );
800         if ( rc ) {
801                 goto error_return;
802         }
803
804         if ( op->o_callback ) {
805                 int             first = 1;
806                 slap_callback   *sc = op->o_callback,
807                                 *sc_next = op->o_callback;
808
809                 rc = SLAP_CB_CONTINUE;
810                 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
811                         sc_next = op->o_callback->sc_next;
812                         if ( op->o_callback->sc_response ) {
813                                 rc = op->o_callback->sc_response( op, rs );
814                                 if ( first && op->o_callback == NULL ) {
815                                         sc = NULL;
816                                 }
817                                 if ( rc != SLAP_CB_CONTINUE ) break;
818                         }
819                         first = 0;
820                 }
821
822                 op->o_callback = sc;
823                 if ( rc != SLAP_CB_CONTINUE ) goto error_return;
824         }
825
826 #ifdef NEW_LOGGING
827         LDAP_LOG( OPERATION, ENTRY, "send_search_entry: conn %lu dn=\"%s\"%s\n",
828                 op->o_connid, rs->sr_entry->e_name.bv_val,
829                 op->ors_attrsonly ? " (attrsOnly)" : "" );
830 #else
831         Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: conn %lu dn=\"%s\"%s\n",
832                 op->o_connid, rs->sr_entry->e_name.bv_val,
833                 op->ors_attrsonly ? " (attrsOnly)" : "" );
834 #endif
835
836         if ( !access_allowed( op, rs->sr_entry, ad_entry, NULL, ACL_READ, NULL )) {
837 #ifdef NEW_LOGGING
838                 LDAP_LOG( ACL, INFO, 
839                         "send_search_entry: conn %lu access to entry (%s) not allowed\n", 
840                         op->o_connid, rs->sr_entry->e_name.bv_val, 0 );
841 #else
842                 Debug( LDAP_DEBUG_ACL,
843                         "send_search_entry: conn %lu access to entry (%s) not allowed\n", 
844                         op->o_connid, rs->sr_entry->e_name.bv_val, 0 );
845 #endif
846
847                 rc = 1;
848                 goto error_return;
849         }
850
851         edn = rs->sr_entry->e_nname.bv_val;
852
853         if ( op->o_res_ber ) {
854                 /* read back control or LDAP_CONNECTIONLESS */
855             ber = op->o_res_ber;
856         } else {
857                 ber_len_t       siz, len;
858                 struct berval   bv;
859
860                 entry_flatsize( rs->sr_entry, &siz, &len, 0 );
861                 bv.bv_len = siz + len;
862                 bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
863
864                 ber_init2( ber, &bv, LBER_USE_DER );
865                 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
866         }
867
868 #ifdef LDAP_CONNECTIONLESS
869         if ( op->o_conn && op->o_conn->c_is_udp ) {
870                 /* CONNECTIONLESS */
871                 if ( op->o_protocol == LDAP_VERSION2 ) {
872                 rc = ber_printf(ber, "t{O{" /*}}*/,
873                                 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
874                 } else {
875                 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
876                                 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
877                 }
878         } else
879 #endif
880         if ( op->o_res_ber ) {
881                 /* read back control */
882             rc = ber_printf( ber, "{O{" /*}}*/, &rs->sr_entry->e_name );
883         } else {
884             rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
885                         LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
886         }
887
888         if ( rc == -1 ) {
889 #ifdef NEW_LOGGING
890                 LDAP_LOG( OPERATION, ERR, 
891                         "send_search_entry: conn %lu  ber_printf failed\n", 
892                         op->o_connid, 0, 0 );
893 #else
894                 Debug( LDAP_DEBUG_ANY, 
895                         "send_search_entry: conn %lu  ber_printf failed\n", 
896                         op->o_connid, 0, 0 );
897 #endif
898
899                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
900                 send_ldap_error( op, rs, LDAP_OTHER, "encoding DN error" );
901                 goto error_return;
902         }
903
904         /* check for special all user attributes ("*") type */
905         userattrs = SLAP_USERATTRS( rs->sr_attr_flags );
906
907         /* create an array of arrays of flags. Each flag corresponds
908          * to particular value of attribute and equals 1 if value matches
909          * to ValuesReturnFilter or 0 if not
910          */     
911         if ( op->o_vrFilter != NULL ) {
912                 int     k = 0;
913                 size_t  size;
914
915                 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
916                         for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
917                 }
918
919                 size = i * sizeof(char *) + k;
920                 if ( size > 0 ) {
921                         char    *a_flags;
922                         e_flags = slap_sl_calloc ( 1, i * sizeof(char *) + k, op->o_tmpmemctx );
923                         if( e_flags == NULL ) {
924 #ifdef NEW_LOGGING
925                                 LDAP_LOG( OPERATION, ERR, 
926                                         "send_search_entry: conn %lu slap_sl_calloc failed\n",
927                                         op->o_connid ? op->o_connid : 0, 0, 0 );
928 #else
929                         Debug( LDAP_DEBUG_ANY, 
930                                         "send_search_entry: conn %lu slap_sl_calloc failed\n",
931                                         op->o_connid ? op->o_connid : 0, 0, 0 );
932 #endif
933                                 ber_free( ber, 1 );
934         
935                                 send_ldap_error( op, rs, LDAP_OTHER, "out of memory" );
936                                 goto error_return;
937                         }
938                         a_flags = (char *)(e_flags + i);
939                         memset( a_flags, 0, k );
940                         for ( a=rs->sr_entry->e_attrs, i=0; a != NULL; a=a->a_next, i++ ) {
941                                 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
942                                 e_flags[i] = a_flags;
943                                 a_flags += j;
944                         }
945         
946                         rc = filter_matched_values(op, rs->sr_entry->e_attrs, &e_flags) ; 
947                         if ( rc == -1 ) {
948 #ifdef NEW_LOGGING
949                                 LDAP_LOG( OPERATION, ERR, "send_search_entry: "
950                                         "conn %lu matched values filtering failed\n",
951                                         op->o_connid ? op->o_connid : 0, 0, 0 );
952 #else
953                                 Debug( LDAP_DEBUG_ANY, "send_search_entry: "
954                                         "conn %lu matched values filtering failed\n",
955                                         op->o_connid ? op->o_connid : 0, 0, 0 );
956 #endif
957                                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
958                                 send_ldap_error( op, rs, LDAP_OTHER,
959                                         "matched values filtering error" );
960                                 goto error_return;
961                         }
962                 }
963         }
964
965         for ( a = rs->sr_entry->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
966                 AttributeDescription *desc = a->a_desc;
967                 int finish = 0;
968
969                 if ( rs->sr_attrs == NULL ) {
970                         /* all attrs request, skip operational attributes */
971                         if( is_at_operational( desc->ad_type ) ) {
972                                 continue;
973                         }
974
975                 } else {
976                         /* specific attrs requested */
977                         if ( is_at_operational( desc->ad_type ) ) {
978                                 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
979                                                 !ad_inlist( desc, rs->sr_attrs ) )
980                                 {
981                                         continue;
982                                 }
983
984                         } else {
985                                 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) )
986                                 {
987                                         continue;
988                                 }
989                         }
990                 }
991
992                 if ( op->ors_attrsonly ) {
993                         if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
994                                 ACL_READ, &acl_state ) )
995                         {
996 #ifdef NEW_LOGGING
997                                 LDAP_LOG( ACL, INFO, 
998                                         "send_search_entry: conn %lu access to attribute %s not "
999                                         "allowed\n", op->o_connid, desc->ad_cname.bv_val, 0 );
1000 #else
1001                                 Debug( LDAP_DEBUG_ACL, "send_search_entry: "
1002                                         "conn %lu access to attribute %s not allowed\n",
1003                                         op->o_connid, desc->ad_cname.bv_val, 0 );
1004 #endif
1005                                 continue;
1006                         }
1007
1008                         if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
1009 #ifdef NEW_LOGGING
1010                                 LDAP_LOG( OPERATION, ERR, 
1011                                         "send_search_entry: conn %lu  ber_printf failed\n", 
1012                                         op->o_connid, 0, 0 );
1013 #else
1014                                 Debug( LDAP_DEBUG_ANY, 
1015                                         "send_search_entry: conn %lu  ber_printf failed\n", 
1016                                         op->o_connid, 0, 0 );
1017 #endif
1018
1019                                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1020                                 send_ldap_error( op, rs, LDAP_OTHER,
1021                                         "encoding description error");
1022                                 goto error_return;
1023                         }
1024                         finish = 1;
1025
1026                 } else {
1027                         int first = 1;
1028                         for ( i = 0; a->a_nvals[i].bv_val != NULL; i++ ) {
1029                                 if ( ! access_allowed( op, rs->sr_entry,
1030                                         desc, &a->a_nvals[i], ACL_READ, &acl_state ) )
1031                                 {
1032 #ifdef NEW_LOGGING
1033                                         LDAP_LOG( ACL, INFO, 
1034                                                 "send_search_entry: conn %lu "
1035                                                 "access to attribute %s, value #%d not allowed\n",
1036                                                 op->o_connid, desc->ad_cname.bv_val, i );
1037 #else
1038                                         Debug( LDAP_DEBUG_ACL,
1039                                                 "send_search_entry: conn %lu "
1040                                                 "access to attribute %s, value #%d not allowed\n",
1041                                                 op->o_connid, desc->ad_cname.bv_val, i );
1042 #endif
1043
1044                                         continue;
1045                                 }
1046
1047                                 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1048                                         continue;
1049                                 }
1050
1051                                 if ( first ) {
1052                                         first = 0;
1053                                         finish = 1;
1054                                         if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
1055 #ifdef NEW_LOGGING
1056                                                 LDAP_LOG( OPERATION, ERR, 
1057                                                         "send_search_entry: conn %lu  ber_printf failed\n", 
1058                                                         op->o_connid, 0, 0 );
1059 #else
1060                                                 Debug( LDAP_DEBUG_ANY,
1061                                                         "send_search_entry: conn %lu  ber_printf failed\n", 
1062                                                         op->o_connid, 0, 0 );
1063 #endif
1064
1065                                                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1066                                                 send_ldap_error( op, rs, LDAP_OTHER,
1067                                                         "encoding description error");
1068                                                 goto error_return;
1069                                         }
1070                                 }
1071                                 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1072 #ifdef NEW_LOGGING
1073                                         LDAP_LOG( OPERATION, ERR, 
1074                                                 "send_search_entry: conn %lu  "
1075                                                 "ber_printf failed.\n", op->o_connid, 0, 0 );
1076 #else
1077                                         Debug( LDAP_DEBUG_ANY,
1078                                                 "send_search_entry: conn %lu  "
1079                                                 "ber_printf failed.\n", op->o_connid, 0, 0 );
1080 #endif
1081
1082                                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1083                                         send_ldap_error( op, rs, LDAP_OTHER,
1084                                                 "encoding values error" );
1085                                         goto error_return;
1086                                 }
1087                         }
1088                 }
1089
1090                 if ( finish && ( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1091 #ifdef NEW_LOGGING
1092                         LDAP_LOG( OPERATION, ERR, 
1093                                 "send_search_entry: conn %lu ber_printf failed\n", 
1094                                 op->o_connid, 0, 0 );
1095 #else
1096                         Debug( LDAP_DEBUG_ANY,
1097                                 "send_search_entry: conn %lu ber_printf failed\n", 
1098                                 op->o_connid, 0, 0 );
1099 #endif
1100
1101                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1102                         send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
1103                         goto error_return;
1104                 }
1105         }
1106
1107         /* NOTE: moved before overlays callback circling because
1108          * they may modify entry and other stuff in rs */
1109         if ( rs->sr_operational_attrs != NULL && op->o_vrFilter != NULL ) {
1110                 int     k = 0;
1111                 size_t  size;
1112
1113                 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
1114                         for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
1115                 }
1116
1117                 size = i * sizeof(char *) + k;
1118                 if ( size > 0 ) {
1119                         char    *a_flags, **tmp;
1120                 
1121                         /*
1122                          * Reuse previous memory - we likely need less space
1123                          * for operational attributes
1124                          */
1125                         tmp = slap_sl_realloc( e_flags, i * sizeof(char *) + k,
1126                                 op->o_tmpmemctx );
1127                         if ( tmp == NULL ) {
1128 #ifdef NEW_LOGGING
1129                                 LDAP_LOG( OPERATION, ERR, 
1130                                         "send_search_entry: conn %lu "
1131                                         "not enough memory "
1132                                         "for matched values filtering\n", 
1133                                         op->o_connid, 0, 0);
1134 #else
1135                                 Debug( LDAP_DEBUG_ANY,
1136                                         "send_search_entry: conn %lu "
1137                                         "not enough memory "
1138                                         "for matched values filtering\n",
1139                                         op->o_connid, 0, 0 );
1140 #endif
1141                                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1142                                 send_ldap_error( op, rs, LDAP_OTHER,
1143                                         "not enough memory for matched values filtering" );
1144                                 goto error_return;
1145                         }
1146                         e_flags = tmp;
1147                         a_flags = (char *)(e_flags + i);
1148                         memset( a_flags, 0, k );
1149                         for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
1150                                 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
1151                                 e_flags[i] = a_flags;
1152                                 a_flags += j;
1153                         }
1154                         rc = filter_matched_values(op, rs->sr_operational_attrs, &e_flags) ; 
1155                     
1156                         if ( rc == -1 ) {
1157 #ifdef NEW_LOGGING
1158                                 LDAP_LOG( OPERATION, ERR, 
1159                                         "send_search_entry: conn %lu "
1160                                         "matched values filtering failed\n", 
1161                                         op->o_connid ? op->o_connid : 0, 0, 0);
1162 #else
1163                                 Debug( LDAP_DEBUG_ANY,
1164                                         "send_search_entry: conn %lu "
1165                                         "matched values filtering failed\n", 
1166                                         op->o_connid ? op->o_connid : 0, 0, 0);
1167 #endif
1168                                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1169                                 send_ldap_error( op, rs, LDAP_OTHER,
1170                                         "matched values filtering error" );
1171                                 goto error_return;
1172                         }
1173                 }
1174         }
1175
1176         for (a = rs->sr_operational_attrs, j=0; a != NULL; a = a->a_next, j++ ) {
1177                 AttributeDescription *desc = a->a_desc;
1178
1179                 if ( rs->sr_attrs == NULL ) {
1180                         /* all attrs request, skip operational attributes */
1181                         if( is_at_operational( desc->ad_type ) ) {
1182                                 continue;
1183                         }
1184
1185                 } else {
1186                         /* specific attrs requested */
1187                         if( is_at_operational( desc->ad_type ) ) {
1188                                 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) && 
1189                                                 !ad_inlist( desc, rs->sr_attrs ) )
1190                                 {
1191                                         continue;
1192                                 }
1193                         } else {
1194                                 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1195                                         continue;
1196                                 }
1197                         }
1198                 }
1199
1200                 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
1201                         ACL_READ, &acl_state ) )
1202                 {
1203 #ifdef NEW_LOGGING
1204                         LDAP_LOG( ACL, INFO, 
1205                                 "send_search_entry: conn %lu "
1206                                 "access to attribute %s not allowed\n",
1207                                 op->o_connid, desc->ad_cname.bv_val, 0 );
1208 #else
1209                         Debug( LDAP_DEBUG_ACL,
1210                                 "send_search_entry: conn %lu "
1211                                 "access to attribute %s not allowed\n",
1212                                 op->o_connid, desc->ad_cname.bv_val, 0 );
1213 #endif
1214
1215                         continue;
1216                 }
1217
1218                 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
1219                 if ( rc == -1 ) {
1220 #ifdef NEW_LOGGING
1221                         LDAP_LOG( OPERATION, ERR, 
1222                                 "send_search_entry: conn %lu  "
1223                                 "ber_printf failed\n", op->o_connid, 0, 0 );
1224 #else
1225                         Debug( LDAP_DEBUG_ANY,
1226                                 "send_search_entry: conn %lu  "
1227                                 "ber_printf failed\n", op->o_connid, 0, 0 );
1228 #endif
1229
1230                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1231                         send_ldap_error( op, rs, LDAP_OTHER,
1232                                 "encoding description error" );
1233                         goto error_return;
1234                 }
1235
1236                 if ( ! op->ors_attrsonly ) {
1237                         for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
1238                                 if ( ! access_allowed( op, rs->sr_entry,
1239                                         desc, &a->a_vals[i], ACL_READ, &acl_state ) )
1240                                 {
1241 #ifdef NEW_LOGGING
1242                                         LDAP_LOG( ACL, INFO, 
1243                                                 "send_search_entry: conn %lu "
1244                                                 "access to %s, value %d not allowed\n",
1245                                                 op->o_connid, desc->ad_cname.bv_val, i );
1246 #else
1247                                         Debug( LDAP_DEBUG_ACL,
1248                                                 "send_search_entry: conn %lu "
1249                                                 "access to %s, value %d not allowed\n",
1250                                                 op->o_connid, desc->ad_cname.bv_val, i );
1251 #endif
1252
1253                                         continue;
1254                                 }
1255
1256                                 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1257                                         continue;
1258                                 }
1259
1260                                 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1261 #ifdef NEW_LOGGING
1262                                         LDAP_LOG( OPERATION, ERR, 
1263                                                 "send_search_entry: conn %lu  ber_printf failed\n", 
1264                                                 op->o_connid, 0, 0 );
1265 #else
1266                                         Debug( LDAP_DEBUG_ANY,
1267                                                 "send_search_entry: conn %lu  ber_printf failed\n", 
1268                                                 op->o_connid, 0, 0 );
1269 #endif
1270
1271                                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1272                                         send_ldap_error( op, rs, LDAP_OTHER,
1273                                                 "encoding values error" );
1274                                         goto error_return;
1275                                 }
1276                         }
1277                 }
1278
1279                 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1280 #ifdef NEW_LOGGING
1281                         LDAP_LOG( OPERATION, ERR, 
1282                                 "send_search_entry: conn %lu  ber_printf failed\n",
1283                                 op->o_connid, 0, 0 );
1284 #else
1285                         Debug( LDAP_DEBUG_ANY,
1286                                 "send_search_entry: conn %lu  ber_printf failed\n",
1287                                 op->o_connid, 0, 0 );
1288 #endif
1289
1290                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1291                         send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
1292                         goto error_return;
1293                 }
1294         }
1295
1296 #ifdef LDAP_SLAPI
1297         /*
1298          * First, setup the computed attribute context that is
1299          * passed to all plugins.
1300          */
1301         if ( op->o_pb ) {
1302                 ctx.cac_pb = op->o_pb;
1303                 ctx.cac_attrs = rs->sr_attrs;
1304                 ctx.cac_attrsonly = op->ors_attrsonly;
1305                 ctx.cac_userattrs = userattrs;
1306                 ctx.cac_opattrs = rs->sr_attr_flags;
1307                 ctx.cac_acl_state = acl_state;
1308                 ctx.cac_private = (void *)ber;
1309
1310                 /*
1311                  * For each client requested attribute, call the plugins.
1312                  */
1313                 if ( rs->sr_attrs != NULL ) {
1314                         for ( anp = rs->sr_attrs; anp->an_name.bv_val != NULL; anp++ ) {
1315                                 rc = compute_evaluator( &ctx, anp->an_name.bv_val,
1316                                         rs->sr_entry, slapi_int_compute_output_ber );
1317                                 if ( rc == 1 ) break;
1318                         }
1319                 } else {
1320                         /*
1321                          * Technically we shouldn't be returning operational attributes
1322                          * when the user requested only user attributes. We'll let the
1323                          * plugin decide whether to be naughty or not.
1324                          */
1325                         rc = compute_evaluator( &ctx, "*",
1326                                 rs->sr_entry, slapi_int_compute_output_ber );
1327                 }
1328                 if ( rc == 1 ) {
1329                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1330                         send_ldap_error( op, rs, LDAP_OTHER, "computed attribute error" );
1331                         goto error_return;
1332                 }
1333         }
1334 #endif /* LDAP_SLAPI */
1335
1336         /* free e_flags */
1337         if ( e_flags ) {
1338                 slap_sl_free( e_flags, op->o_tmpmemctx );
1339                 e_flags = NULL;
1340         }
1341
1342         rc = ber_printf( ber, /*{{*/ "}N}" );
1343
1344         if( rc != -1 ) {
1345                 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1346         }
1347
1348         if( rc != -1 ) {
1349 #ifdef LDAP_CONNECTIONLESS
1350                 if( op->o_conn && op->o_conn->c_is_udp ) {
1351                         if ( op->o_protocol != LDAP_VERSION2 ) {
1352                                 rc = ber_printf( ber, /*{*/ "N}" );
1353                         }
1354                 } else
1355 #endif
1356                 if ( op->o_res_ber == NULL ) {
1357                         rc = ber_printf( ber, /*{*/ "N}" );
1358                 }
1359         }
1360
1361         if ( rc == -1 ) {
1362 #ifdef NEW_LOGGING
1363                 LDAP_LOG( OPERATION, ERR, 
1364                         "send_search_entry: conn %lu ber_printf failed\n", 
1365                         op->o_connid, 0, 0 );
1366 #else
1367                 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1368 #endif
1369
1370                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1371                 send_ldap_error( op, rs, LDAP_OTHER, "encode entry end error" );
1372                 rc = 1;
1373                 goto error_return;
1374         }
1375
1376         if ( op->o_res_ber == NULL ) {
1377                 bytes = send_ldap_ber( op->o_conn, ber );
1378                 ber_free_buf( ber );
1379
1380                 if ( bytes < 0 ) {
1381 #ifdef NEW_LOGGING
1382                         LDAP_LOG( OPERATION, ERR, 
1383                                 "send_search_entry: conn %lu  ber write failed.\n", 
1384                                 op->o_connid, 0, 0 );
1385 #else
1386                         Debug( LDAP_DEBUG_ANY,
1387                                 "send_search_entry: conn %lu  ber write failed.\n", 
1388                                 op->o_connid, 0, 0 );
1389 #endif
1390
1391                         rc = -1;
1392                         goto error_return;
1393                 }
1394                 rs->sr_nentries++;
1395
1396                 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
1397                 num_bytes_sent += bytes;
1398                 num_entries_sent++;
1399                 num_pdu_sent++;
1400                 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
1401         }
1402
1403         Statslog( LDAP_DEBUG_STATS2, "conn=%lu op=%lu ENTRY dn=\"%s\"\n",
1404             op->o_connid, op->o_opid, rs->sr_entry->e_dn, 0, 0 );
1405
1406 #ifdef NEW_LOGGING
1407         LDAP_LOG( OPERATION, ENTRY, 
1408                 "send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1409 #else
1410         Debug( LDAP_DEBUG_TRACE,
1411                 "<= send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1412 #endif
1413
1414         rc = 0;
1415
1416 error_return:;
1417         if ( op->o_callback ) {
1418                 int             first = 1;
1419                 slap_callback   *sc = op->o_callback,
1420                                 *sc_next = op->o_callback;
1421
1422                 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1423                         sc_next = op->o_callback->sc_next;
1424                         if ( op->o_callback->sc_cleanup ) {
1425                                 (void)op->o_callback->sc_cleanup( op, rs );
1426                                 if ( first && op->o_callback == NULL ) {
1427                                         sc = NULL;
1428                                 }
1429                         }
1430                         first = 0;
1431                 }
1432
1433                 op->o_callback = sc;
1434         }
1435
1436         if ( e_flags ) {
1437                 slap_sl_free( e_flags, op->o_tmpmemctx );
1438         }
1439
1440         if ( rs->sr_operational_attrs ) {
1441                 attrs_free( rs->sr_operational_attrs );
1442                 rs->sr_operational_attrs = NULL;
1443         }
1444         rs->sr_attr_flags = SLAP_ATTRS_UNDEFINED;
1445
1446         /* FIXME: I think rs->sr_type should be explicitly set to
1447          * REP_SEARCH here. That's what it was when we entered this
1448          * function. send_ldap_error may have changed it, but we
1449          * should set it back so that the cleanup functions know
1450          * what they're doing.
1451          */
1452         if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH 
1453                 && rs->sr_entry 
1454                 && ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) ) 
1455         {
1456                 entry_free( rs->sr_entry );
1457                 rs->sr_entry = NULL;
1458                 rs->sr_flags &= ~REP_ENTRY_MUSTBEFREED;
1459         }
1460
1461         return( rc );
1462 }
1463
1464 int
1465 slap_send_search_reference( Operation *op, SlapReply *rs )
1466 {
1467         BerElementBuffer berbuf;
1468         BerElement      *ber = (BerElement *) &berbuf;
1469         int rc = 0;
1470         int bytes;
1471
1472         AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1473         AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1474
1475         rs->sr_type = REP_SEARCHREF;
1476         if ( op->o_callback ) {
1477                 int             first = 1;
1478                 slap_callback   *sc = op->o_callback,
1479                                 *sc_next = op->o_callback;
1480
1481                 rc = SLAP_CB_CONTINUE;
1482                 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1483                         sc_next = op->o_callback->sc_next;
1484                         if ( op->o_callback->sc_response ) {
1485                                 rc = op->o_callback->sc_response( op, rs );
1486                                 if ( first && op->o_callback == NULL ) {
1487                                         sc = NULL;
1488                                 }
1489                                 if ( rc != SLAP_CB_CONTINUE ) break;
1490                         }
1491                         first = 0;
1492                 }
1493
1494                 op->o_callback = sc;
1495                 if ( rc != SLAP_CB_CONTINUE ) goto rel;
1496         }
1497
1498 #ifdef NEW_LOGGING
1499         LDAP_LOG( OPERATION, ENTRY, 
1500                 "send_search_reference: conn %lu  dn=\"%s\"\n", 
1501                 op->o_connid,
1502                 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0 );
1503 #else
1504         Debug( LDAP_DEBUG_TRACE,
1505                 "=> send_search_reference: dn=\"%s\"\n",
1506                 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0, 0 );
1507 #endif
1508
1509         if (  rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1510                 ad_entry, NULL, ACL_READ, NULL ) )
1511         {
1512 #ifdef NEW_LOGGING
1513                 LDAP_LOG( ACL, INFO, 
1514                         "send_search_reference: conn %lu        "
1515                         "access to entry %s not allowed\n",
1516                         op->o_connid, rs->sr_entry->e_dn, 0 );
1517 #else
1518                 Debug( LDAP_DEBUG_ACL,
1519                         "send_search_reference: access to entry not allowed\n",
1520                     0, 0, 0 );
1521 #endif
1522                 rc = 1;
1523                 goto rel;
1524         }
1525
1526         if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1527                 ad_ref, NULL, ACL_READ, NULL ) )
1528         {
1529 #ifdef NEW_LOGGING
1530                 LDAP_LOG( ACL, INFO, 
1531                         "send_search_reference: conn %lu access "
1532                         "to reference not allowed.\n", op->o_connid, 0, 0 );
1533 #else
1534                 Debug( LDAP_DEBUG_ACL,
1535                         "send_search_reference: access "
1536                         "to reference not allowed\n",
1537                     0, 0, 0 );
1538 #endif
1539                 rc = 1;
1540                 goto rel;
1541         }
1542
1543 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1544         if( op->o_domain_scope ) {
1545 #ifdef NEW_LOGGING
1546                 LDAP_LOG( OPERATION, ERR, 
1547                         "send_search_reference: conn %lu domainScope control in (%s).\n",
1548                         op->o_connid, rs->sr_entry->e_dn, 0 );
1549 #else
1550                 Debug( LDAP_DEBUG_ANY,
1551                         "send_search_reference: domainScope control in (%s)\n", 
1552                         rs->sr_entry->e_dn, 0, 0 );
1553 #endif
1554                 rc = 0;
1555                 goto rel;
1556         }
1557 #endif
1558
1559         if( rs->sr_ref == NULL ) {
1560 #ifdef NEW_LOGGING
1561                 LDAP_LOG( OPERATION, ERR, 
1562                         "send_search_reference: conn %lu null ref in (%s).\n",
1563                         op->o_connid, rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0 );
1564 #else
1565                 Debug( LDAP_DEBUG_ANY,
1566                         "send_search_reference: null ref in (%s)\n", 
1567                         rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1568 #endif
1569                 rc = 1;
1570                 goto rel;
1571         }
1572
1573         if( op->o_protocol < LDAP_VERSION3 ) {
1574                 rc = 0;
1575                 /* save the references for the result */
1576                 if( rs->sr_ref[0].bv_val != NULL ) {
1577                         if( value_add( &rs->sr_v2ref, rs->sr_ref ) )
1578                                 rc = LDAP_OTHER;
1579                 }
1580                 goto rel;
1581         }
1582
1583 #ifdef LDAP_CONNECTIONLESS
1584         if( op->o_conn && op->o_conn->c_is_udp ) {
1585                 ber = op->o_res_ber;
1586         } else
1587 #endif
1588         {
1589                 ber_init_w_nullc( ber, LBER_USE_DER );
1590                 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1591         }
1592
1593         rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
1594                 LDAP_RES_SEARCH_REFERENCE, rs->sr_ref );
1595
1596         if( rc != -1 ) {
1597                 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1598         }
1599
1600         if( rc != -1 ) {
1601                 rc = ber_printf( ber, /*"{"*/ "N}" );
1602         }
1603
1604         if ( rc == -1 ) {
1605 #ifdef NEW_LOGGING
1606                 LDAP_LOG( OPERATION, ERR, 
1607                         "send_search_reference: conn %lu        "
1608                         "ber_printf failed.\n", op->o_connid, 0, 0 );
1609 #else
1610                 Debug( LDAP_DEBUG_ANY,
1611                         "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1612 #endif
1613
1614 #ifdef LDAP_CONNECTIONLESS
1615                 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1616 #endif
1617                 ber_free_buf( ber );
1618                 send_ldap_error( op, rs, LDAP_OTHER, "encode DN error" );
1619                 goto rel;
1620         }
1621
1622 #ifdef LDAP_CONNECTIONLESS
1623         if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1624 #endif
1625         bytes = send_ldap_ber( op->o_conn, ber );
1626         ber_free_buf( ber );
1627
1628         ldap_pvt_thread_mutex_lock( &num_sent_mutex );
1629         num_bytes_sent += bytes;
1630         num_refs_sent++;
1631         num_pdu_sent++;
1632         ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
1633 #ifdef LDAP_CONNECTIONLESS
1634         }
1635 #endif
1636
1637         Statslog( LDAP_DEBUG_STATS2, "conn=%lu op=%lu REF dn=\"%s\"\n",
1638                 op->o_connid, op->o_opid, rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1639
1640 #ifdef NEW_LOGGING
1641         LDAP_LOG( OPERATION, ENTRY, 
1642                 "send_search_reference: conn %lu exit.\n", op->o_connid, 0, 0 );
1643 #else
1644         Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1645 #endif
1646
1647 rel:
1648         if ( op->o_callback ) {
1649                 int             first = 1;
1650                 slap_callback   *sc = op->o_callback,
1651                                 *sc_next = op->o_callback;
1652
1653                 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1654                         sc_next = op->o_callback->sc_next;
1655                         if ( op->o_callback->sc_cleanup ) {
1656                                 (void)op->o_callback->sc_cleanup( op, rs );
1657                                 if ( first && op->o_callback == NULL ) {
1658                                         sc = NULL;
1659                                 }
1660                         }
1661                         first = 0;
1662                 }
1663
1664                 op->o_callback = sc;
1665         }
1666
1667         return rc;
1668 }
1669
1670 int
1671 str2result(
1672     char        *s,
1673     int         *code,
1674     char        **matched,
1675     char        **info
1676 )
1677 {
1678         int     rc;
1679         char    *c;
1680
1681         *code = LDAP_SUCCESS;
1682         *matched = NULL;
1683         *info = NULL;
1684
1685         if ( strncasecmp( s, "RESULT", STRLENOF( "RESULT" ) ) != 0 ) {
1686 #ifdef NEW_LOGGING
1687                 LDAP_LOG( OPERATION, INFO, 
1688                         "str2result: (%s), expecting \"RESULT\"\n", s, 0, 0 );
1689 #else
1690                 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1691                     s, 0, 0 );
1692 #endif
1693
1694                 return( -1 );
1695         }
1696
1697         rc = 0;
1698         while ( (s = strchr( s, '\n' )) != NULL ) {
1699                 *s++ = '\0';
1700                 if ( *s == '\0' ) {
1701                         break;
1702                 }
1703                 if ( (c = strchr( s, ':' )) != NULL ) {
1704                         c++;
1705                 }
1706
1707                 if ( strncasecmp( s, "code", STRLENOF( "code" ) ) == 0 ) {
1708                         if ( c != NULL ) {
1709                                 *code = atoi( c );
1710                         }
1711                 } else if ( strncasecmp( s, "matched", STRLENOF( "matched" ) ) == 0 ) {
1712                         if ( c != NULL ) {
1713                                 *matched = c;
1714                         }
1715                 } else if ( strncasecmp( s, "info", STRLENOF( "info" ) ) == 0 ) {
1716                         if ( c != NULL ) {
1717                                 *info = c;
1718                         }
1719                 } else {
1720 #ifdef NEW_LOGGING
1721                         LDAP_LOG( OPERATION, INFO, "str2result: (%s) unknown.\n", s, 0, 0 );
1722 #else
1723                         Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
1724                             s, 0, 0 );
1725 #endif
1726
1727                         rc = -1;
1728                 }
1729         }
1730
1731         return( rc );
1732 }
1733
1734 int slap_read_controls(
1735         Operation *op,
1736         SlapReply *rs,
1737         Entry *e,
1738         const struct berval *oid,
1739         LDAPControl **ctrl )
1740 {
1741         int rc;
1742         struct berval bv;
1743         BerElementBuffer berbuf;
1744         BerElement *ber = (BerElement *) &berbuf;
1745         LDAPControl c;
1746         ber_len_t       siz, len;
1747         Operation myop;
1748
1749 #ifdef NEW_LOGGING
1750         LDAP_LOG( OPERATION, INFO, "slap_read_controls: (%s) %s\n",
1751                 oid->bv_val, e->e_dn, 0 );
1752 #else
1753         Debug( LDAP_DEBUG_ANY, "slap_read_controls: (%s) %s\n",
1754                 oid->bv_val, e->e_dn, 0 );
1755 #endif
1756
1757         rs->sr_entry = e;
1758         rs->sr_attrs = ( oid == &slap_pre_read_bv ) ?
1759                 op->o_preread_attrs : op->o_postread_attrs; 
1760
1761         entry_flatsize( rs->sr_entry, &siz, &len, 0 );
1762         bv.bv_len = siz + len;
1763         bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
1764
1765         ber_init2( ber, &bv, LBER_USE_DER );
1766         ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1767
1768         /* create new operation */
1769         myop = *op;
1770         myop.o_bd = NULL;
1771         myop.o_res_ber = ber;
1772
1773         rc = slap_send_search_entry( &myop, rs );
1774         if( rc ) return rc;
1775
1776         rc = ber_flatten2( ber, &c.ldctl_value, 0 );
1777
1778         if( rc == LBER_ERROR ) return LDAP_OTHER;
1779
1780         c.ldctl_oid = oid->bv_val;
1781         c.ldctl_iscritical = 0;
1782
1783         if ( ctrl == NULL ) {
1784                 /* first try */
1785                 *ctrl = (LDAPControl *) slap_sl_calloc( 1, sizeof(LDAPControl), NULL );
1786         } else {
1787                 /* retry: free previous try */
1788                 slap_sl_free( (*ctrl)->ldctl_value.bv_val, &op->o_tmpmemctx );
1789         }
1790
1791         **ctrl = c;
1792         return LDAP_SUCCESS;
1793 }
1794
1795 /* Map API errors to protocol errors... */
1796 int
1797 slap_map_api2result( SlapReply *rs )
1798 {
1799         switch(rs->sr_err) {
1800         case LDAP_SERVER_DOWN:
1801                 return LDAP_UNAVAILABLE;
1802         case LDAP_LOCAL_ERROR:
1803                 return LDAP_OTHER;
1804         case LDAP_ENCODING_ERROR:
1805         case LDAP_DECODING_ERROR:
1806                 return LDAP_PROTOCOL_ERROR;
1807         case LDAP_TIMEOUT:
1808                 return LDAP_UNAVAILABLE;
1809         case LDAP_AUTH_UNKNOWN:
1810                 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
1811         case LDAP_FILTER_ERROR:
1812                 rs->sr_text = "Filter error";
1813                 return LDAP_OTHER;
1814         case LDAP_USER_CANCELLED:
1815                 rs->sr_text = "User cancelled";
1816                 return LDAP_OTHER;
1817         case LDAP_PARAM_ERROR:
1818                 return LDAP_PROTOCOL_ERROR;
1819         case LDAP_NO_MEMORY:
1820                 return LDAP_OTHER;
1821         case LDAP_CONNECT_ERROR:
1822                 return LDAP_UNAVAILABLE;
1823         case LDAP_NOT_SUPPORTED:
1824                 return LDAP_UNWILLING_TO_PERFORM;
1825         case LDAP_CONTROL_NOT_FOUND:
1826                 return LDAP_PROTOCOL_ERROR;
1827         case LDAP_NO_RESULTS_RETURNED:
1828                 return LDAP_NO_SUCH_OBJECT;
1829         case LDAP_MORE_RESULTS_TO_RETURN:
1830                 rs->sr_text = "More results to return";
1831                 return LDAP_OTHER;
1832         case LDAP_CLIENT_LOOP:
1833         case LDAP_REFERRAL_LIMIT_EXCEEDED:
1834                 return LDAP_LOOP_DETECT;
1835         default:
1836                 if ( LDAP_API_ERROR(rs->sr_err) ) return LDAP_OTHER;
1837                 return rs->sr_err;
1838         }
1839 }
1840