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