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