]> git.sur5r.net Git - openldap/blob - servers/slapd/result.c
69ec2f4740bf55cb2d4f2b8cb49962b9dee99080
[openldap] / servers / slapd / result.c
1 /* result.c - routines to send ldap results, errors, and referrals */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2004 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17  * All rights reserved.
18  *
19  * Redistribution and use in source and binary forms are permitted
20  * provided that this notice is preserved and that due credit is given
21  * to the University of Michigan at Ann Arbor. The name of the University
22  * may not be used to endorse or promote products derived from this
23  * software without specific prior written permission. This software
24  * is provided ``as is'' without express or implied warranty.
25  */
26
27 #include "portable.h"
28
29 #include <stdio.h>
30
31 #include <ac/socket.h>
32 #include <ac/errno.h>
33 #include <ac/string.h>
34 #include <ac/ctype.h>
35 #include <ac/time.h>
36 #include <ac/unistd.h>
37
38 #include "slap.h"
39
40 #ifdef LDAP_SLAPI
41 #include "slapi/slapi.h"
42 #endif
43
44 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                                 LDAP_TAG_EXOP_RES_OID, rs->sr_rspoid );
385                 }
386                 if( rc != -1 && rs->sr_rspdata != NULL ) {
387                         rc = ber_printf( ber, "tO",
388                                 LDAP_TAG_EXOP_RES_VALUE, rs->sr_rspdata );
389                 }
390         }
391
392         if( rc != -1 ) {
393                 rc = ber_printf( ber, /*"{"*/ "N}" );
394         }
395
396         if( rc != -1 ) {
397                 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
398         }
399
400         if( rc != -1 ) {
401                 rc = ber_printf( ber, /*"{"*/ "N}" );
402         }
403
404 #ifdef LDAP_CONNECTIONLESS
405         if( op->o_conn && op->o_conn->c_is_udp && op->o_protocol == LDAP_VERSION2
406                 && rc != -1 )
407         {
408                 rc = ber_printf( ber, /*"{"*/ "N}" );
409         }
410 #endif
411                 
412         if ( rc == -1 ) {
413                 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
414
415 #ifdef LDAP_CONNECTIONLESS
416                 if (!op->o_conn || op->o_conn->c_is_udp == 0)
417 #endif
418                 {
419                         ber_free_buf( ber );
420                 }
421                 goto cleanup;
422         }
423
424         /* send BER */
425         bytes = send_ldap_ber( op->o_conn, ber );
426 #ifdef LDAP_CONNECTIONLESS
427         if (!op->o_conn || op->o_conn->c_is_udp == 0)
428 #endif
429         {
430                 ber_free_buf( ber );
431         }
432
433         if ( bytes < 0 ) {
434                 Debug( LDAP_DEBUG_ANY,
435                         "send_ldap_response: ber write failed\n",
436                         0, 0, 0 );
437
438                 goto cleanup;
439         }
440
441 #ifdef LDAP_SLAPI
442         if ( op->o_pb ) {
443                 slapi_pblock_set( op->o_pb, SLAPI_RESULT_CODE, (void *)rs->sr_err );
444                 slapi_pblock_set( op->o_pb, SLAPI_RESULT_MATCHED,
445                         (void *)rs->sr_matched );
446                 slapi_pblock_set( op->o_pb, SLAPI_RESULT_TEXT, (void *)rs->sr_text );
447         }
448 #endif /* LDAP_SLAPI */
449
450         ldap_pvt_thread_mutex_lock( &num_sent_mutex );
451         num_bytes_sent += bytes;
452         num_pdu_sent++;
453         ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
454
455 cleanup:;
456         /* Tell caller that we did this for real, as opposed to being
457          * overridden by a callback
458          */
459         rc = SLAP_CB_CONTINUE;
460
461 clean2:;
462         if ( op->o_callback ) {
463                 int             first = 1;
464                 slap_callback   *sc = op->o_callback,
465                                 *sc_next = op->o_callback;
466
467                 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
468                         sc_next = op->o_callback->sc_next;
469                         if ( op->o_callback->sc_cleanup ) {
470                                 (void)op->o_callback->sc_cleanup( op, rs );
471                                 if ( first && op->o_callback == NULL ) {
472                                         sc = NULL;
473                                 }
474                         }
475                         first = 0;
476                 }
477
478                 op->o_callback = sc;
479         }
480
481
482         if ( rs->sr_matched && rs->sr_flags & REP_MATCHED_MUSTBEFREED ) {
483                 free( (char *)rs->sr_matched );
484                 rs->sr_matched = NULL;
485         }
486
487         if ( rs->sr_ref && rs->sr_flags & REP_REF_MUSTBEFREED ) {
488                 ber_bvarray_free( rs->sr_ref );
489                 rs->sr_ref = NULL;
490         }
491
492         return rc;
493 }
494
495
496 void
497 send_ldap_disconnect( Operation *op, SlapReply *rs )
498 {
499 #define LDAP_UNSOLICITED_ERROR(e) \
500         (  (e) == LDAP_PROTOCOL_ERROR \
501         || (e) == LDAP_STRONG_AUTH_REQUIRED \
502         || (e) == LDAP_UNAVAILABLE )
503
504         assert( LDAP_UNSOLICITED_ERROR( rs->sr_err ) );
505
506         rs->sr_type = REP_EXTENDED;
507
508         Debug( LDAP_DEBUG_TRACE,
509                 "send_ldap_disconnect %d:%s\n",
510                 rs->sr_err, rs->sr_text ? rs->sr_text : "", NULL );
511
512         if ( op->o_protocol < LDAP_VERSION3 ) {
513                 rs->sr_rspoid = NULL;
514                 rs->sr_tag = req2res( op->o_tag );
515                 rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
516
517         } else {
518                 rs->sr_rspoid = LDAP_NOTICE_DISCONNECT;
519                 rs->sr_tag = LDAP_RES_EXTENDED;
520                 rs->sr_msgid = 0;
521         }
522
523         if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
524                 Statslog( LDAP_DEBUG_STATS,
525                         "conn=%lu op=%lu DISCONNECT tag=%lu err=%d text=%s\n",
526                         op->o_connid, op->o_opid, rs->sr_tag, rs->sr_err,
527                         rs->sr_text ? rs->sr_text : "" );
528         }
529 }
530
531 void
532 slap_send_ldap_result( Operation *op, SlapReply *rs )
533 {
534         char *tmp = NULL;
535         const char *otext = rs->sr_text;
536         BerVarray oref = rs->sr_ref;
537
538         rs->sr_type = REP_RESULT;
539
540         assert( !LDAP_API_ERROR( rs->sr_err ));
541
542         Debug( LDAP_DEBUG_TRACE,
543                 "send_ldap_result: conn=%lu op=%lu p=%d\n",
544                 op->o_connid, op->o_opid, op->o_protocol );
545
546         Debug( LDAP_DEBUG_ARGS,
547                 "send_ldap_result: err=%d matched=\"%s\" text=\"%s\"\n",
548                 rs->sr_err, rs->sr_matched ? rs->sr_matched : "",
549                 rs->sr_text ? rs->sr_text : "" );
550
551
552         if( rs->sr_ref ) {
553                 Debug( LDAP_DEBUG_ARGS,
554                         "send_ldap_result: referral=\"%s\"\n",
555                         rs->sr_ref[0].bv_val ? rs->sr_ref[0].bv_val : "NULL",
556                         NULL, NULL );
557         }
558
559         assert( rs->sr_err != LDAP_PARTIAL_RESULTS );
560
561         if ( rs->sr_err == LDAP_REFERRAL ) {
562 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
563                 if( op->o_domain_scope ) {
564                         rs->sr_ref = NULL;
565                 }
566 #endif
567                 if( rs->sr_ref == NULL ) {
568                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
569                 } else if ( op->o_protocol < LDAP_VERSION3 ) {
570                         rs->sr_err = LDAP_PARTIAL_RESULTS;
571                 }
572         }
573
574 #ifdef LDAP_SLAPI
575         /*
576          * Call pre-result plugins. To avoid infinite recursion plugins
577          * should just set SLAPI_RESULT_CODE rather than sending a
578          * result if they wish to change the result.
579          */
580         if ( op->o_pb != NULL ) {
581                 slapi_int_pblock_set_operation( op->o_pb, op );
582                 slapi_pblock_set( op->o_pb, SLAPI_RESULT_CODE,
583                         (void *)rs->sr_err );
584                 slapi_pblock_set( op->o_pb, SLAPI_RESULT_TEXT,
585                         (void *)rs->sr_text );
586                 slapi_pblock_set( op->o_pb, SLAPI_RESULT_MATCHED,
587                         (void *)rs->sr_matched );
588
589                 (void) slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_PRE_RESULT_FN,
590                         op->o_pb );
591         }
592 #endif /* LDAP_SLAPI */
593
594         if ( op->o_protocol < LDAP_VERSION3 ) {
595                 tmp = v2ref( rs->sr_ref, rs->sr_text );
596                 rs->sr_text = tmp;
597                 rs->sr_ref = NULL;
598         }
599
600         rs->sr_tag = req2res( op->o_tag );
601         rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
602
603         if ( send_ldap_response( op, rs ) == SLAP_CB_CONTINUE ) {
604                 if ( op->o_tag == LDAP_REQ_SEARCH ) {
605                         char nbuf[64];
606                         snprintf( nbuf, sizeof nbuf, "%d nentries=%d",
607                                 rs->sr_err, rs->sr_nentries );
608
609                         Statslog( LDAP_DEBUG_STATS,
610                         "conn=%lu op=%lu SEARCH RESULT tag=%lu err=%s text=%s\n",
611                                 op->o_connid, op->o_opid, rs->sr_tag, nbuf,
612                                 rs->sr_text ? rs->sr_text : "" );
613                 } else {
614                         Statslog( LDAP_DEBUG_STATS,
615                                 "conn=%lu op=%lu RESULT tag=%lu err=%d text=%s\n",
616                                 op->o_connid, op->o_opid, rs->sr_tag, rs->sr_err,
617                                 rs->sr_text ? rs->sr_text : "" );
618                 }
619         }
620
621         if( tmp != NULL ) ch_free(tmp);
622         rs->sr_text = otext;
623         rs->sr_ref = oref;
624 }
625
626 void
627 send_ldap_sasl( Operation *op, SlapReply *rs )
628 {
629         rs->sr_type = REP_SASL;
630         Debug( LDAP_DEBUG_TRACE, "send_ldap_sasl: err=%d len=%ld\n",
631                 rs->sr_err,
632                 rs->sr_sasldata ? (long) rs->sr_sasldata->bv_len : -1, NULL );
633
634         rs->sr_tag = req2res( op->o_tag );
635         rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
636
637         send_ldap_response( op, rs );
638 }
639
640 void
641 slap_send_ldap_extended( Operation *op, SlapReply *rs )
642 {
643         rs->sr_type = REP_EXTENDED;
644
645         Debug( LDAP_DEBUG_TRACE,
646                 "send_ldap_extended: err=%d oid=%s len=%ld\n",
647                 rs->sr_err,
648                 rs->sr_rspoid ? rs->sr_rspoid : "",
649                 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
650
651         rs->sr_tag = req2res( op->o_tag );
652         rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
653
654         send_ldap_response( op, rs );
655 }
656
657 void
658 slap_send_ldap_intermediate( Operation *op, SlapReply *rs )
659 {
660         rs->sr_type = REP_INTERMEDIATE;
661         Debug( LDAP_DEBUG_TRACE,
662                 "send_ldap_intermediate: err=%d oid=%s len=%ld\n",
663                 rs->sr_err,
664                 rs->sr_rspoid ? rs->sr_rspoid : "",
665                 rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
666         rs->sr_tag = LDAP_RES_INTERMEDIATE;
667         rs->sr_msgid = op->o_msgid;
668         send_ldap_response( op, rs );
669 }
670
671 int
672 slap_send_search_entry( Operation *op, SlapReply *rs )
673 {
674         BerElementBuffer berbuf;
675         BerElement      *ber = (BerElement *) &berbuf;
676         Attribute       *a;
677         int             i, j, rc=-1, bytes;
678         char            *edn;
679         int             userattrs;
680         AccessControlState acl_state = ACL_STATE_INIT;
681 #ifdef LDAP_SLAPI
682         /* Support for computed attribute plugins */
683         computed_attr_context    ctx;
684         AttributeName   *anp;
685 #endif
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         rs->sr_type = REP_SEARCH;
695
696         /* eventually will loop through generated operational attributes */
697         /* only subschemaSubentry and numSubordinates are implemented */
698         /* NOTE: moved before overlays callback circling because
699          * they may modify entry and other stuff in rs */
700         /* check for special all operational attributes ("+") type */
701         /* FIXME: maybe we could se this flag at the operation level;
702          * however, in principle the caller of send_search_entry() may
703          * change the attribute list at each call */
704         rs->sr_attr_flags = slap_attr_flags( rs->sr_attrs );
705
706         rc = backend_operational( op, rs );
707         if ( rc ) {
708                 goto error_return;
709         }
710
711         if ( op->o_callback ) {
712                 int             first = 1;
713                 slap_callback   *sc = op->o_callback,
714                                 *sc_next = op->o_callback;
715
716                 rc = SLAP_CB_CONTINUE;
717                 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
718                         sc_next = op->o_callback->sc_next;
719                         if ( op->o_callback->sc_response ) {
720                                 rc = op->o_callback->sc_response( op, rs );
721                                 if ( first && op->o_callback == NULL ) {
722                                         sc = NULL;
723                                 }
724                                 if ( rc != SLAP_CB_CONTINUE ) break;
725                         }
726                         first = 0;
727                 }
728
729                 op->o_callback = sc;
730                 if ( rc != SLAP_CB_CONTINUE ) goto error_return;
731         }
732
733         Debug( LDAP_DEBUG_TRACE, "=> send_search_entry: conn %lu dn=\"%s\"%s\n",
734                 op->o_connid, rs->sr_entry->e_name.bv_val,
735                 op->ors_attrsonly ? " (attrsOnly)" : "" );
736
737         if ( !access_allowed( op, rs->sr_entry, ad_entry, NULL, ACL_READ, NULL )) {
738                 Debug( LDAP_DEBUG_ACL,
739                         "send_search_entry: conn %lu access to entry (%s) not allowed\n", 
740                         op->o_connid, rs->sr_entry->e_name.bv_val, 0 );
741
742                 rc = 1;
743                 goto error_return;
744         }
745
746         edn = rs->sr_entry->e_nname.bv_val;
747
748         if ( op->o_res_ber ) {
749                 /* read back control or LDAP_CONNECTIONLESS */
750             ber = op->o_res_ber;
751         } else {
752                 ber_len_t       siz, len;
753                 struct berval   bv;
754
755                 entry_flatsize( rs->sr_entry, &siz, &len, 0 );
756                 bv.bv_len = siz + len;
757                 bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
758
759                 ber_init2( ber, &bv, LBER_USE_DER );
760                 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
761         }
762
763 #ifdef LDAP_CONNECTIONLESS
764         if ( op->o_conn && op->o_conn->c_is_udp ) {
765                 /* CONNECTIONLESS */
766                 if ( op->o_protocol == LDAP_VERSION2 ) {
767                 rc = ber_printf(ber, "t{O{" /*}}*/,
768                                 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
769                 } else {
770                 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
771                                 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
772                 }
773         } else
774 #endif
775         if ( op->o_res_ber ) {
776                 /* read back control */
777             rc = ber_printf( ber, "{O{" /*}}*/, &rs->sr_entry->e_name );
778         } else {
779             rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
780                         LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
781         }
782
783         if ( rc == -1 ) {
784                 Debug( LDAP_DEBUG_ANY, 
785                         "send_search_entry: conn %lu  ber_printf failed\n", 
786                         op->o_connid, 0, 0 );
787
788                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
789                 send_ldap_error( op, rs, LDAP_OTHER, "encoding DN error" );
790                 goto error_return;
791         }
792
793         /* check for special all user attributes ("*") type */
794         userattrs = SLAP_USERATTRS( rs->sr_attr_flags );
795
796         /* create an array of arrays of flags. Each flag corresponds
797          * to particular value of attribute and equals 1 if value matches
798          * to ValuesReturnFilter or 0 if not
799          */     
800         if ( op->o_vrFilter != NULL ) {
801                 int     k = 0;
802                 size_t  size;
803
804                 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
805                         for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
806                 }
807
808                 size = i * sizeof(char *) + k;
809                 if ( size > 0 ) {
810                         char    *a_flags;
811                         e_flags = slap_sl_calloc ( 1, i * sizeof(char *) + k, op->o_tmpmemctx );
812                         if( e_flags == NULL ) {
813                         Debug( LDAP_DEBUG_ANY, 
814                                         "send_search_entry: conn %lu slap_sl_calloc failed\n",
815                                         op->o_connid ? op->o_connid : 0, 0, 0 );
816                                 ber_free( ber, 1 );
817         
818                                 send_ldap_error( op, rs, LDAP_OTHER, "out of memory" );
819                                 goto error_return;
820                         }
821                         a_flags = (char *)(e_flags + i);
822                         memset( a_flags, 0, k );
823                         for ( a=rs->sr_entry->e_attrs, i=0; a != NULL; a=a->a_next, i++ ) {
824                                 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
825                                 e_flags[i] = a_flags;
826                                 a_flags += j;
827                         }
828         
829                         rc = filter_matched_values(op, rs->sr_entry->e_attrs, &e_flags) ; 
830                         if ( rc == -1 ) {
831                                 Debug( LDAP_DEBUG_ANY, "send_search_entry: "
832                                         "conn %lu matched values filtering failed\n",
833                                         op->o_connid ? op->o_connid : 0, 0, 0 );
834                                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
835                                 send_ldap_error( op, rs, LDAP_OTHER,
836                                         "matched values filtering error" );
837                                 goto error_return;
838                         }
839                 }
840         }
841
842         for ( a = rs->sr_entry->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
843                 AttributeDescription *desc = a->a_desc;
844                 int finish = 0;
845
846                 if ( rs->sr_attrs == NULL ) {
847                         /* all attrs request, skip operational attributes */
848                         if( is_at_operational( desc->ad_type ) ) {
849                                 continue;
850                         }
851
852                 } else {
853                         /* specific attrs requested */
854                         if ( is_at_operational( desc->ad_type ) ) {
855                                 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
856                                                 !ad_inlist( desc, rs->sr_attrs ) )
857                                 {
858                                         continue;
859                                 }
860
861                         } else {
862                                 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) )
863                                 {
864                                         continue;
865                                 }
866                         }
867                 }
868
869                 if ( op->ors_attrsonly ) {
870                         if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
871                                 ACL_READ, &acl_state ) )
872                         {
873                                 Debug( LDAP_DEBUG_ACL, "send_search_entry: "
874                                         "conn %lu access to attribute %s not allowed\n",
875                                         op->o_connid, desc->ad_cname.bv_val, 0 );
876                                 continue;
877                         }
878
879                         if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
880                                 Debug( LDAP_DEBUG_ANY, 
881                                         "send_search_entry: conn %lu  ber_printf failed\n", 
882                                         op->o_connid, 0, 0 );
883
884                                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
885                                 send_ldap_error( op, rs, LDAP_OTHER,
886                                         "encoding description error");
887                                 goto error_return;
888                         }
889                         finish = 1;
890
891                 } else {
892                         int first = 1;
893                         for ( i = 0; a->a_nvals[i].bv_val != NULL; i++ ) {
894                                 if ( ! access_allowed( op, rs->sr_entry,
895                                         desc, &a->a_nvals[i], ACL_READ, &acl_state ) )
896                                 {
897                                         Debug( LDAP_DEBUG_ACL,
898                                                 "send_search_entry: conn %lu "
899                                                 "access to attribute %s, value #%d not allowed\n",
900                                                 op->o_connid, desc->ad_cname.bv_val, i );
901
902                                         continue;
903                                 }
904
905                                 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
906                                         continue;
907                                 }
908
909                                 if ( first ) {
910                                         first = 0;
911                                         finish = 1;
912                                         if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
913                                                 Debug( LDAP_DEBUG_ANY,
914                                                         "send_search_entry: conn %lu  ber_printf failed\n", 
915                                                         op->o_connid, 0, 0 );
916
917                                                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
918                                                 send_ldap_error( op, rs, LDAP_OTHER,
919                                                         "encoding description error");
920                                                 goto error_return;
921                                         }
922                                 }
923                                 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
924                                         Debug( LDAP_DEBUG_ANY,
925                                                 "send_search_entry: conn %lu  "
926                                                 "ber_printf failed.\n", op->o_connid, 0, 0 );
927
928                                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
929                                         send_ldap_error( op, rs, LDAP_OTHER,
930                                                 "encoding values error" );
931                                         goto error_return;
932                                 }
933                         }
934                 }
935
936                 if ( finish && ( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
937                         Debug( LDAP_DEBUG_ANY,
938                                 "send_search_entry: conn %lu ber_printf failed\n", 
939                                 op->o_connid, 0, 0 );
940
941                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
942                         send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
943                         goto error_return;
944                 }
945         }
946
947         /* NOTE: moved before overlays callback circling because
948          * they may modify entry and other stuff in rs */
949         if ( rs->sr_operational_attrs != NULL && op->o_vrFilter != NULL ) {
950                 int     k = 0;
951                 size_t  size;
952
953                 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
954                         for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
955                 }
956
957                 size = i * sizeof(char *) + k;
958                 if ( size > 0 ) {
959                         char    *a_flags, **tmp;
960                 
961                         /*
962                          * Reuse previous memory - we likely need less space
963                          * for operational attributes
964                          */
965                         tmp = slap_sl_realloc( e_flags, i * sizeof(char *) + k,
966                                 op->o_tmpmemctx );
967                         if ( tmp == NULL ) {
968                                 Debug( LDAP_DEBUG_ANY,
969                                         "send_search_entry: conn %lu "
970                                         "not enough memory "
971                                         "for matched values filtering\n",
972                                         op->o_connid, 0, 0 );
973                                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
974                                 send_ldap_error( op, rs, LDAP_OTHER,
975                                         "not enough memory for matched values filtering" );
976                                 goto error_return;
977                         }
978                         e_flags = tmp;
979                         a_flags = (char *)(e_flags + i);
980                         memset( a_flags, 0, k );
981                         for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
982                                 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
983                                 e_flags[i] = a_flags;
984                                 a_flags += j;
985                         }
986                         rc = filter_matched_values(op, rs->sr_operational_attrs, &e_flags) ; 
987                     
988                         if ( rc == -1 ) {
989                                 Debug( LDAP_DEBUG_ANY,
990                                         "send_search_entry: conn %lu "
991                                         "matched values filtering failed\n", 
992                                         op->o_connid ? op->o_connid : 0, 0, 0);
993                                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
994                                 send_ldap_error( op, rs, LDAP_OTHER,
995                                         "matched values filtering error" );
996                                 goto error_return;
997                         }
998                 }
999         }
1000
1001         for (a = rs->sr_operational_attrs, j=0; a != NULL; a = a->a_next, j++ ) {
1002                 AttributeDescription *desc = a->a_desc;
1003
1004                 if ( rs->sr_attrs == NULL ) {
1005                         /* all attrs request, skip operational attributes */
1006                         if( is_at_operational( desc->ad_type ) ) {
1007                                 continue;
1008                         }
1009
1010                 } else {
1011                         /* specific attrs requested */
1012                         if( is_at_operational( desc->ad_type ) ) {
1013                                 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) && 
1014                                                 !ad_inlist( desc, rs->sr_attrs ) )
1015                                 {
1016                                         continue;
1017                                 }
1018                         } else {
1019                                 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1020                                         continue;
1021                                 }
1022                         }
1023                 }
1024
1025                 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
1026                         ACL_READ, &acl_state ) )
1027                 {
1028                         Debug( LDAP_DEBUG_ACL,
1029                                 "send_search_entry: conn %lu "
1030                                 "access to attribute %s not allowed\n",
1031                                 op->o_connid, desc->ad_cname.bv_val, 0 );
1032
1033                         continue;
1034                 }
1035
1036                 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
1037                 if ( rc == -1 ) {
1038                         Debug( LDAP_DEBUG_ANY,
1039                                 "send_search_entry: conn %lu  "
1040                                 "ber_printf failed\n", op->o_connid, 0, 0 );
1041
1042                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1043                         send_ldap_error( op, rs, LDAP_OTHER,
1044                                 "encoding description error" );
1045                         goto error_return;
1046                 }
1047
1048                 if ( ! op->ors_attrsonly ) {
1049                         for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
1050                                 if ( ! access_allowed( op, rs->sr_entry,
1051                                         desc, &a->a_vals[i], ACL_READ, &acl_state ) )
1052                                 {
1053                                         Debug( LDAP_DEBUG_ACL,
1054                                                 "send_search_entry: conn %lu "
1055                                                 "access to %s, value %d not allowed\n",
1056                                                 op->o_connid, desc->ad_cname.bv_val, i );
1057
1058                                         continue;
1059                                 }
1060
1061                                 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1062                                         continue;
1063                                 }
1064
1065                                 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1066                                         Debug( LDAP_DEBUG_ANY,
1067                                                 "send_search_entry: conn %lu  ber_printf failed\n", 
1068                                                 op->o_connid, 0, 0 );
1069
1070                                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1071                                         send_ldap_error( op, rs, LDAP_OTHER,
1072                                                 "encoding values error" );
1073                                         goto error_return;
1074                                 }
1075                         }
1076                 }
1077
1078                 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1079                         Debug( LDAP_DEBUG_ANY,
1080                                 "send_search_entry: conn %lu  ber_printf failed\n",
1081                                 op->o_connid, 0, 0 );
1082
1083                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1084                         send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
1085                         goto error_return;
1086                 }
1087         }
1088
1089 #ifdef LDAP_SLAPI
1090         /*
1091          * First, setup the computed attribute context that is
1092          * passed to all plugins.
1093          */
1094         if ( op->o_pb ) {
1095                 ctx.cac_pb = op->o_pb;
1096                 ctx.cac_attrs = rs->sr_attrs;
1097                 ctx.cac_attrsonly = op->ors_attrsonly;
1098                 ctx.cac_userattrs = userattrs;
1099                 ctx.cac_opattrs = rs->sr_attr_flags;
1100                 ctx.cac_acl_state = acl_state;
1101                 ctx.cac_private = (void *)ber;
1102
1103                 /*
1104                  * For each client requested attribute, call the plugins.
1105                  */
1106                 if ( rs->sr_attrs != NULL ) {
1107                         for ( anp = rs->sr_attrs; anp->an_name.bv_val != NULL; anp++ ) {
1108                                 rc = compute_evaluator( &ctx, anp->an_name.bv_val,
1109                                         rs->sr_entry, slapi_int_compute_output_ber );
1110                                 if ( rc == 1 ) break;
1111                         }
1112                 } else {
1113                         /*
1114                          * Technically we shouldn't be returning operational attributes
1115                          * when the user requested only user attributes. We'll let the
1116                          * plugin decide whether to be naughty or not.
1117                          */
1118                         rc = compute_evaluator( &ctx, "*",
1119                                 rs->sr_entry, slapi_int_compute_output_ber );
1120                 }
1121                 if ( rc == 1 ) {
1122                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1123                         send_ldap_error( op, rs, LDAP_OTHER, "computed attribute error" );
1124                         goto error_return;
1125                 }
1126         }
1127 #endif /* LDAP_SLAPI */
1128
1129         /* free e_flags */
1130         if ( e_flags ) {
1131                 slap_sl_free( e_flags, op->o_tmpmemctx );
1132                 e_flags = NULL;
1133         }
1134
1135         rc = ber_printf( ber, /*{{*/ "}N}" );
1136
1137         if( rc != -1 ) {
1138                 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1139         }
1140
1141         if( rc != -1 ) {
1142 #ifdef LDAP_CONNECTIONLESS
1143                 if( op->o_conn && op->o_conn->c_is_udp ) {
1144                         if ( op->o_protocol != LDAP_VERSION2 ) {
1145                                 rc = ber_printf( ber, /*{*/ "N}" );
1146                         }
1147                 } else
1148 #endif
1149                 if ( op->o_res_ber == NULL ) {
1150                         rc = ber_printf( ber, /*{*/ "N}" );
1151                 }
1152         }
1153
1154         if ( rc == -1 ) {
1155                 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1156
1157                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1158                 send_ldap_error( op, rs, LDAP_OTHER, "encode entry end error" );
1159                 rc = 1;
1160                 goto error_return;
1161         }
1162
1163         if ( op->o_res_ber == NULL ) {
1164                 bytes = send_ldap_ber( op->o_conn, ber );
1165                 ber_free_buf( ber );
1166
1167                 if ( bytes < 0 ) {
1168                         Debug( LDAP_DEBUG_ANY,
1169                                 "send_search_entry: conn %lu  ber write failed.\n", 
1170                                 op->o_connid, 0, 0 );
1171
1172                         rc = -1;
1173                         goto error_return;
1174                 }
1175                 rs->sr_nentries++;
1176
1177                 ldap_pvt_thread_mutex_lock( &num_sent_mutex );
1178                 num_bytes_sent += bytes;
1179                 num_entries_sent++;
1180                 num_pdu_sent++;
1181                 ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
1182         }
1183
1184         Statslog( LDAP_DEBUG_STATS2, "conn=%lu op=%lu ENTRY dn=\"%s\"\n",
1185             op->o_connid, op->o_opid, rs->sr_entry->e_dn, 0, 0 );
1186
1187         Debug( LDAP_DEBUG_TRACE,
1188                 "<= send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1189
1190         rc = 0;
1191
1192 error_return:;
1193         if ( op->o_callback ) {
1194                 int             first = 1;
1195                 slap_callback   *sc = op->o_callback,
1196                                 *sc_next = op->o_callback;
1197
1198                 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1199                         sc_next = op->o_callback->sc_next;
1200                         if ( op->o_callback->sc_cleanup ) {
1201                                 (void)op->o_callback->sc_cleanup( op, rs );
1202                                 if ( first && op->o_callback == NULL ) {
1203                                         sc = NULL;
1204                                 }
1205                         }
1206                         first = 0;
1207                 }
1208
1209                 op->o_callback = sc;
1210         }
1211
1212         if ( e_flags ) {
1213                 slap_sl_free( e_flags, op->o_tmpmemctx );
1214         }
1215
1216         if ( rs->sr_operational_attrs ) {
1217                 attrs_free( rs->sr_operational_attrs );
1218                 rs->sr_operational_attrs = NULL;
1219         }
1220         rs->sr_attr_flags = SLAP_ATTRS_UNDEFINED;
1221
1222         /* FIXME: I think rs->sr_type should be explicitly set to
1223          * REP_SEARCH here. That's what it was when we entered this
1224          * function. send_ldap_error may have changed it, but we
1225          * should set it back so that the cleanup functions know
1226          * what they're doing.
1227          */
1228         if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH 
1229                 && rs->sr_entry 
1230                 && ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) ) 
1231         {
1232                 entry_free( rs->sr_entry );
1233                 rs->sr_entry = NULL;
1234                 rs->sr_flags &= ~REP_ENTRY_MUSTBEFREED;
1235         }
1236
1237         return( rc );
1238 }
1239
1240 int
1241 slap_send_search_reference( Operation *op, SlapReply *rs )
1242 {
1243         BerElementBuffer berbuf;
1244         BerElement      *ber = (BerElement *) &berbuf;
1245         int rc = 0;
1246         int bytes;
1247
1248         AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1249         AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1250
1251         rs->sr_type = REP_SEARCHREF;
1252         if ( op->o_callback ) {
1253                 int             first = 1;
1254                 slap_callback   *sc = op->o_callback,
1255                                 *sc_next = op->o_callback;
1256
1257                 rc = SLAP_CB_CONTINUE;
1258                 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1259                         sc_next = op->o_callback->sc_next;
1260                         if ( op->o_callback->sc_response ) {
1261                                 rc = op->o_callback->sc_response( op, rs );
1262                                 if ( first && op->o_callback == NULL ) {
1263                                         sc = NULL;
1264                                 }
1265                                 if ( rc != SLAP_CB_CONTINUE ) break;
1266                         }
1267                         first = 0;
1268                 }
1269
1270                 op->o_callback = sc;
1271                 if ( rc != SLAP_CB_CONTINUE ) goto rel;
1272         }
1273
1274         Debug( LDAP_DEBUG_TRACE,
1275                 "=> send_search_reference: dn=\"%s\"\n",
1276                 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0, 0 );
1277
1278         if (  rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1279                 ad_entry, NULL, ACL_READ, NULL ) )
1280         {
1281                 Debug( LDAP_DEBUG_ACL,
1282                         "send_search_reference: access to entry not allowed\n",
1283                     0, 0, 0 );
1284                 rc = 1;
1285                 goto rel;
1286         }
1287
1288         if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1289                 ad_ref, NULL, ACL_READ, NULL ) )
1290         {
1291                 Debug( LDAP_DEBUG_ACL,
1292                         "send_search_reference: access "
1293                         "to reference not allowed\n",
1294                     0, 0, 0 );
1295                 rc = 1;
1296                 goto rel;
1297         }
1298
1299 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1300         if( op->o_domain_scope ) {
1301                 Debug( LDAP_DEBUG_ANY,
1302                         "send_search_reference: domainScope control in (%s)\n", 
1303                         rs->sr_entry->e_dn, 0, 0 );
1304                 rc = 0;
1305                 goto rel;
1306         }
1307 #endif
1308
1309         if( rs->sr_ref == NULL ) {
1310                 Debug( LDAP_DEBUG_ANY,
1311                         "send_search_reference: null ref in (%s)\n", 
1312                         rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1313                 rc = 1;
1314                 goto rel;
1315         }
1316
1317         if( op->o_protocol < LDAP_VERSION3 ) {
1318                 rc = 0;
1319                 /* save the references for the result */
1320                 if( rs->sr_ref[0].bv_val != NULL ) {
1321                         if( value_add( &rs->sr_v2ref, rs->sr_ref ) )
1322                                 rc = LDAP_OTHER;
1323                 }
1324                 goto rel;
1325         }
1326
1327 #ifdef LDAP_CONNECTIONLESS
1328         if( op->o_conn && op->o_conn->c_is_udp ) {
1329                 ber = op->o_res_ber;
1330         } else
1331 #endif
1332         {
1333                 ber_init_w_nullc( ber, LBER_USE_DER );
1334                 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1335         }
1336
1337         rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
1338                 LDAP_RES_SEARCH_REFERENCE, rs->sr_ref );
1339
1340         if( rc != -1 ) {
1341                 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1342         }
1343
1344         if( rc != -1 ) {
1345                 rc = ber_printf( ber, /*"{"*/ "N}" );
1346         }
1347
1348         if ( rc == -1 ) {
1349                 Debug( LDAP_DEBUG_ANY,
1350                         "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1351
1352 #ifdef LDAP_CONNECTIONLESS
1353                 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1354 #endif
1355                 ber_free_buf( ber );
1356                 send_ldap_error( op, rs, LDAP_OTHER, "encode DN error" );
1357                 goto rel;
1358         }
1359
1360 #ifdef LDAP_CONNECTIONLESS
1361         if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1362 #endif
1363         bytes = send_ldap_ber( op->o_conn, ber );
1364         ber_free_buf( ber );
1365
1366         ldap_pvt_thread_mutex_lock( &num_sent_mutex );
1367         num_bytes_sent += bytes;
1368         num_refs_sent++;
1369         num_pdu_sent++;
1370         ldap_pvt_thread_mutex_unlock( &num_sent_mutex );
1371 #ifdef LDAP_CONNECTIONLESS
1372         }
1373 #endif
1374
1375         Statslog( LDAP_DEBUG_STATS2, "conn=%lu op=%lu REF dn=\"%s\"\n",
1376                 op->o_connid, op->o_opid, rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1377
1378         Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1379
1380 rel:
1381         if ( op->o_callback ) {
1382                 int             first = 1;
1383                 slap_callback   *sc = op->o_callback,
1384                                 *sc_next = op->o_callback;
1385
1386                 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1387                         sc_next = op->o_callback->sc_next;
1388                         if ( op->o_callback->sc_cleanup ) {
1389                                 (void)op->o_callback->sc_cleanup( op, rs );
1390                                 if ( first && op->o_callback == NULL ) {
1391                                         sc = NULL;
1392                                 }
1393                         }
1394                         first = 0;
1395                 }
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