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