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