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