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