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