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