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