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