]> git.sur5r.net Git - openldap/blob - servers/slapd/result.c
ITS#3842 from jtownsend@opendarwin.org - don't free slap_listeners until
[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                 struct berval   bv;
795
796                 bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
797                 bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
798
799                 ber_init2( ber, &bv, LBER_USE_DER );
800                 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
801         }
802
803 #ifdef LDAP_CONNECTIONLESS
804         if ( op->o_conn && op->o_conn->c_is_udp ) {
805                 /* CONNECTIONLESS */
806                 if ( op->o_protocol == LDAP_VERSION2 ) {
807                 rc = ber_printf(ber, "t{O{" /*}}*/,
808                                 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
809                 } else {
810                 rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
811                                 LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
812                 }
813         } else
814 #endif
815         if ( op->o_res_ber ) {
816                 /* read back control */
817             rc = ber_printf( ber, "{O{" /*}}*/, &rs->sr_entry->e_name );
818         } else {
819             rc = ber_printf( ber, "{it{O{" /*}}}*/, op->o_msgid,
820                         LDAP_RES_SEARCH_ENTRY, &rs->sr_entry->e_name );
821         }
822
823         if ( rc == -1 ) {
824                 Debug( LDAP_DEBUG_ANY, 
825                         "send_search_entry: conn %lu  ber_printf failed\n", 
826                         op->o_connid, 0, 0 );
827
828                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
829                 send_ldap_error( op, rs, LDAP_OTHER, "encoding DN error" );
830                 goto error_return;
831         }
832
833         /* check for special all user attributes ("*") type */
834         userattrs = SLAP_USERATTRS( rs->sr_attr_flags );
835
836         /* create an array of arrays of flags. Each flag corresponds
837          * to particular value of attribute and equals 1 if value matches
838          * to ValuesReturnFilter or 0 if not
839          */     
840         if ( op->o_vrFilter != NULL ) {
841                 int     k = 0;
842                 size_t  size;
843
844                 for ( a = rs->sr_entry->e_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
845                         for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
846                 }
847
848                 size = i * sizeof(char *) + k;
849                 if ( size > 0 ) {
850                         char    *a_flags;
851                         e_flags = slap_sl_calloc ( 1, i * sizeof(char *) + k, op->o_tmpmemctx );
852                         if( e_flags == NULL ) {
853                         Debug( LDAP_DEBUG_ANY, 
854                                         "send_search_entry: conn %lu slap_sl_calloc failed\n",
855                                         op->o_connid ? op->o_connid : 0, 0, 0 );
856                                 ber_free( ber, 1 );
857         
858                                 send_ldap_error( op, rs, LDAP_OTHER, "out of memory" );
859                                 goto error_return;
860                         }
861                         a_flags = (char *)(e_flags + i);
862                         memset( a_flags, 0, k );
863                         for ( a=rs->sr_entry->e_attrs, i=0; a != NULL; a=a->a_next, i++ ) {
864                                 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
865                                 e_flags[i] = a_flags;
866                                 a_flags += j;
867                         }
868         
869                         rc = filter_matched_values(op, rs->sr_entry->e_attrs, &e_flags) ; 
870                         if ( rc == -1 ) {
871                                 Debug( LDAP_DEBUG_ANY, "send_search_entry: "
872                                         "conn %lu matched values filtering failed\n",
873                                         op->o_connid ? op->o_connid : 0, 0, 0 );
874                                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
875                                 send_ldap_error( op, rs, LDAP_OTHER,
876                                         "matched values filtering error" );
877                                 goto error_return;
878                         }
879                 }
880         }
881
882         for ( a = rs->sr_entry->e_attrs, j = 0; a != NULL; a = a->a_next, j++ ) {
883                 AttributeDescription *desc = a->a_desc;
884                 int finish = 0;
885
886                 if ( rs->sr_attrs == NULL ) {
887                         /* all attrs request, skip operational attributes */
888                         if( is_at_operational( desc->ad_type ) ) {
889                                 continue;
890                         }
891
892                 } else {
893                         /* specific attrs requested */
894                         if ( is_at_operational( desc->ad_type ) ) {
895                                 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) &&
896                                                 !ad_inlist( desc, rs->sr_attrs ) )
897                                 {
898                                         continue;
899                                 }
900
901                         } else {
902                                 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) )
903                                 {
904                                         continue;
905                                 }
906                         }
907                 }
908
909                 if ( op->ors_attrsonly ) {
910                         if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
911                                 ACL_READ, &acl_state ) )
912                         {
913                                 Debug( LDAP_DEBUG_ACL, "send_search_entry: "
914                                         "conn %lu access to attribute %s not allowed\n",
915                                         op->o_connid, desc->ad_cname.bv_val, 0 );
916                                 continue;
917                         }
918
919                         if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
920                                 Debug( LDAP_DEBUG_ANY, 
921                                         "send_search_entry: conn %lu  ber_printf failed\n", 
922                                         op->o_connid, 0, 0 );
923
924                                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
925                                 send_ldap_error( op, rs, LDAP_OTHER,
926                                         "encoding description error");
927                                 goto error_return;
928                         }
929                         finish = 1;
930
931                 } else {
932                         int first = 1;
933                         for ( i = 0; a->a_nvals[i].bv_val != NULL; i++ ) {
934                                 if ( ! access_allowed( op, rs->sr_entry,
935                                         desc, &a->a_nvals[i], ACL_READ, &acl_state ) )
936                                 {
937                                         Debug( LDAP_DEBUG_ACL,
938                                                 "send_search_entry: conn %lu "
939                                                 "access to attribute %s, value #%d not allowed\n",
940                                                 op->o_connid, desc->ad_cname.bv_val, i );
941
942                                         continue;
943                                 }
944
945                                 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
946                                         continue;
947                                 }
948
949                                 if ( first ) {
950                                         first = 0;
951                                         finish = 1;
952                                         if (( rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname )) == -1 ) {
953                                                 Debug( LDAP_DEBUG_ANY,
954                                                         "send_search_entry: conn %lu  ber_printf failed\n", 
955                                                         op->o_connid, 0, 0 );
956
957                                                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
958                                                 send_ldap_error( op, rs, LDAP_OTHER,
959                                                         "encoding description error");
960                                                 goto error_return;
961                                         }
962                                 }
963                                 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
964                                         Debug( LDAP_DEBUG_ANY,
965                                                 "send_search_entry: conn %lu  "
966                                                 "ber_printf failed.\n", op->o_connid, 0, 0 );
967
968                                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
969                                         send_ldap_error( op, rs, LDAP_OTHER,
970                                                 "encoding values error" );
971                                         goto error_return;
972                                 }
973                         }
974                 }
975
976                 if ( finish && ( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
977                         Debug( LDAP_DEBUG_ANY,
978                                 "send_search_entry: conn %lu ber_printf failed\n", 
979                                 op->o_connid, 0, 0 );
980
981                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
982                         send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
983                         goto error_return;
984                 }
985         }
986
987         /* NOTE: moved before overlays callback circling because
988          * they may modify entry and other stuff in rs */
989         if ( rs->sr_operational_attrs != NULL && op->o_vrFilter != NULL ) {
990                 int     k = 0;
991                 size_t  size;
992
993                 for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
994                         for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) k++;
995                 }
996
997                 size = i * sizeof(char *) + k;
998                 if ( size > 0 ) {
999                         char    *a_flags, **tmp;
1000                 
1001                         /*
1002                          * Reuse previous memory - we likely need less space
1003                          * for operational attributes
1004                          */
1005                         tmp = slap_sl_realloc( e_flags, i * sizeof(char *) + k,
1006                                 op->o_tmpmemctx );
1007                         if ( tmp == NULL ) {
1008                                 Debug( LDAP_DEBUG_ANY,
1009                                         "send_search_entry: conn %lu "
1010                                         "not enough memory "
1011                                         "for matched values filtering\n",
1012                                         op->o_connid, 0, 0 );
1013                                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1014                                 send_ldap_error( op, rs, LDAP_OTHER,
1015                                         "not enough memory for matched values filtering" );
1016                                 goto error_return;
1017                         }
1018                         e_flags = tmp;
1019                         a_flags = (char *)(e_flags + i);
1020                         memset( a_flags, 0, k );
1021                         for ( a = rs->sr_operational_attrs, i=0; a != NULL; a = a->a_next, i++ ) {
1022                                 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ );
1023                                 e_flags[i] = a_flags;
1024                                 a_flags += j;
1025                         }
1026                         rc = filter_matched_values(op, rs->sr_operational_attrs, &e_flags) ; 
1027                     
1028                         if ( rc == -1 ) {
1029                                 Debug( LDAP_DEBUG_ANY,
1030                                         "send_search_entry: conn %lu "
1031                                         "matched values filtering failed\n", 
1032                                         op->o_connid ? op->o_connid : 0, 0, 0);
1033                                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1034                                 send_ldap_error( op, rs, LDAP_OTHER,
1035                                         "matched values filtering error" );
1036                                 goto error_return;
1037                         }
1038                 }
1039         }
1040
1041         for (a = rs->sr_operational_attrs, j=0; a != NULL; a = a->a_next, j++ ) {
1042                 AttributeDescription *desc = a->a_desc;
1043
1044                 if ( rs->sr_attrs == NULL ) {
1045                         /* all attrs request, skip operational attributes */
1046                         if( is_at_operational( desc->ad_type ) ) {
1047                                 continue;
1048                         }
1049
1050                 } else {
1051                         /* specific attrs requested */
1052                         if( is_at_operational( desc->ad_type ) ) {
1053                                 if ( !SLAP_OPATTRS( rs->sr_attr_flags ) && 
1054                                                 !ad_inlist( desc, rs->sr_attrs ) )
1055                                 {
1056                                         continue;
1057                                 }
1058                         } else {
1059                                 if ( !userattrs && !ad_inlist( desc, rs->sr_attrs ) ) {
1060                                         continue;
1061                                 }
1062                         }
1063                 }
1064
1065                 if ( ! access_allowed( op, rs->sr_entry, desc, NULL,
1066                         ACL_READ, &acl_state ) )
1067                 {
1068                         Debug( LDAP_DEBUG_ACL,
1069                                 "send_search_entry: conn %lu "
1070                                 "access to attribute %s not allowed\n",
1071                                 op->o_connid, desc->ad_cname.bv_val, 0 );
1072
1073                         continue;
1074                 }
1075
1076                 rc = ber_printf( ber, "{O[" /*]}*/ , &desc->ad_cname );
1077                 if ( rc == -1 ) {
1078                         Debug( LDAP_DEBUG_ANY,
1079                                 "send_search_entry: conn %lu  "
1080                                 "ber_printf failed\n", op->o_connid, 0, 0 );
1081
1082                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1083                         send_ldap_error( op, rs, LDAP_OTHER,
1084                                 "encoding description error" );
1085                         goto error_return;
1086                 }
1087
1088                 if ( ! op->ors_attrsonly ) {
1089                         for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
1090                                 if ( ! access_allowed( op, rs->sr_entry,
1091                                         desc, &a->a_vals[i], ACL_READ, &acl_state ) )
1092                                 {
1093                                         Debug( LDAP_DEBUG_ACL,
1094                                                 "send_search_entry: conn %lu "
1095                                                 "access to %s, value %d not allowed\n",
1096                                                 op->o_connid, desc->ad_cname.bv_val, i );
1097
1098                                         continue;
1099                                 }
1100
1101                                 if ( op->o_vrFilter && e_flags[j][i] == 0 ){
1102                                         continue;
1103                                 }
1104
1105                                 if (( rc = ber_printf( ber, "O", &a->a_vals[i] )) == -1 ) {
1106                                         Debug( LDAP_DEBUG_ANY,
1107                                                 "send_search_entry: conn %lu  ber_printf failed\n", 
1108                                                 op->o_connid, 0, 0 );
1109
1110                                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1111                                         send_ldap_error( op, rs, LDAP_OTHER,
1112                                                 "encoding values error" );
1113                                         goto error_return;
1114                                 }
1115                         }
1116                 }
1117
1118                 if (( rc = ber_printf( ber, /*{[*/ "]N}" )) == -1 ) {
1119                         Debug( LDAP_DEBUG_ANY,
1120                                 "send_search_entry: conn %lu  ber_printf failed\n",
1121                                 op->o_connid, 0, 0 );
1122
1123                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1124                         send_ldap_error( op, rs, LDAP_OTHER, "encode end error" );
1125                         goto error_return;
1126                 }
1127         }
1128
1129 #ifdef LDAP_SLAPI
1130         /*
1131          * First, setup the computed attribute context that is
1132          * passed to all plugins.
1133          */
1134         if ( op->o_pb ) {
1135                 ctx.cac_pb = op->o_pb;
1136                 ctx.cac_attrs = rs->sr_attrs;
1137                 ctx.cac_attrsonly = op->ors_attrsonly;
1138                 ctx.cac_userattrs = userattrs;
1139                 ctx.cac_opattrs = rs->sr_attr_flags;
1140                 ctx.cac_acl_state = acl_state;
1141                 ctx.cac_private = (void *)ber;
1142
1143                 /*
1144                  * For each client requested attribute, call the plugins.
1145                  */
1146                 if ( rs->sr_attrs != NULL ) {
1147                         for ( anp = rs->sr_attrs; anp->an_name.bv_val != NULL; anp++ ) {
1148                                 rc = compute_evaluator( &ctx, anp->an_name.bv_val,
1149                                         rs->sr_entry, slapi_int_compute_output_ber );
1150                                 if ( rc == 1 ) break;
1151                         }
1152                 } else {
1153                         /*
1154                          * Technically we shouldn't be returning operational attributes
1155                          * when the user requested only user attributes. We'll let the
1156                          * plugin decide whether to be naughty or not.
1157                          */
1158                         rc = compute_evaluator( &ctx, "*",
1159                                 rs->sr_entry, slapi_int_compute_output_ber );
1160                 }
1161                 if ( rc == 1 ) {
1162                         if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1163                         send_ldap_error( op, rs, LDAP_OTHER, "computed attribute error" );
1164                         goto error_return;
1165                 }
1166         }
1167 #endif /* LDAP_SLAPI */
1168
1169         /* free e_flags */
1170         if ( e_flags ) {
1171                 slap_sl_free( e_flags, op->o_tmpmemctx );
1172                 e_flags = NULL;
1173         }
1174
1175         rc = ber_printf( ber, /*{{*/ "}N}" );
1176
1177         if( rc != -1 ) {
1178                 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1179         }
1180
1181         if( rc != -1 ) {
1182 #ifdef LDAP_CONNECTIONLESS
1183                 if( op->o_conn && op->o_conn->c_is_udp ) {
1184                         if ( op->o_protocol != LDAP_VERSION2 ) {
1185                                 rc = ber_printf( ber, /*{*/ "N}" );
1186                         }
1187                 } else
1188 #endif
1189                 if ( op->o_res_ber == NULL ) {
1190                         rc = ber_printf( ber, /*{*/ "N}" );
1191                 }
1192         }
1193
1194         if ( rc == -1 ) {
1195                 Debug( LDAP_DEBUG_ANY, "ber_printf failed\n", 0, 0, 0 );
1196
1197                 if ( op->o_res_ber == NULL ) ber_free_buf( ber );
1198                 send_ldap_error( op, rs, LDAP_OTHER, "encode entry end error" );
1199                 rc = 1;
1200                 goto error_return;
1201         }
1202
1203         if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
1204                 be_entry_release_rw( op, rs->sr_entry, 0 );
1205                 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
1206                 rs->sr_entry = NULL;
1207         }
1208
1209         if ( op->o_res_ber == NULL ) {
1210                 bytes = send_ldap_ber( op->o_conn, ber );
1211                 ber_free_buf( ber );
1212
1213                 if ( bytes < 0 ) {
1214                         Debug( LDAP_DEBUG_ANY,
1215                                 "send_search_entry: conn %lu  ber write failed.\n", 
1216                                 op->o_connid, 0, 0 );
1217
1218                         rc = -1;
1219                         goto error_return;
1220                 }
1221                 rs->sr_nentries++;
1222
1223                 ldap_pvt_thread_mutex_lock( &slap_counters.sc_sent_mutex );
1224                 ldap_pvt_mp_add_ulong( slap_counters.sc_bytes, (unsigned long)bytes );
1225                 ldap_pvt_mp_add_ulong( slap_counters.sc_entries, 1 );
1226                 ldap_pvt_mp_add_ulong( slap_counters.sc_pdu, 1 );
1227                 ldap_pvt_thread_mutex_unlock( &slap_counters.sc_sent_mutex );
1228         }
1229
1230         Statslog( LDAP_DEBUG_STATS2, "%s ENTRY dn=\"%s\"\n",
1231             op->o_log_prefix, edn, 0, 0, 0 );
1232
1233         Debug( LDAP_DEBUG_TRACE,
1234                 "<= send_search_entry: conn %lu exit.\n", op->o_connid, 0, 0 );
1235
1236         rc = 0;
1237
1238 error_return:;
1239         if ( op->o_callback ) {
1240                 int             first = 1;
1241                 slap_callback   *sc = op->o_callback, *sc_next;
1242
1243                 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1244                         sc_next = op->o_callback->sc_next;
1245                         if ( op->o_callback->sc_cleanup ) {
1246                                 (void)op->o_callback->sc_cleanup( op, rs );
1247                                 if ( first && op->o_callback != sc ) {
1248                                         sc = op->o_callback;
1249                                 }
1250                         }
1251                         first = 0;
1252                 }
1253                 op->o_callback = sc;
1254         }
1255
1256         if ( e_flags ) {
1257                 slap_sl_free( e_flags, op->o_tmpmemctx );
1258         }
1259
1260         if ( rs->sr_operational_attrs ) {
1261                 attrs_free( rs->sr_operational_attrs );
1262                 rs->sr_operational_attrs = NULL;
1263         }
1264         rs->sr_attr_flags = SLAP_ATTRS_UNDEFINED;
1265
1266         /* FIXME: I think rs->sr_type should be explicitly set to
1267          * REP_SEARCH here. That's what it was when we entered this
1268          * function. send_ldap_error may have changed it, but we
1269          * should set it back so that the cleanup functions know
1270          * what they're doing.
1271          */
1272         if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH 
1273                 && rs->sr_entry 
1274                 && ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) ) 
1275         {
1276                 entry_free( rs->sr_entry );
1277                 rs->sr_entry = NULL;
1278                 rs->sr_flags &= ~REP_ENTRY_MUSTBEFREED;
1279         }
1280
1281         return( rc );
1282 }
1283
1284 int
1285 slap_send_search_reference( Operation *op, SlapReply *rs )
1286 {
1287         BerElementBuffer berbuf;
1288         BerElement      *ber = (BerElement *) &berbuf;
1289         int rc = 0;
1290         int bytes;
1291
1292         AttributeDescription *ad_ref = slap_schema.si_ad_ref;
1293         AttributeDescription *ad_entry = slap_schema.si_ad_entry;
1294
1295         rs->sr_type = REP_SEARCHREF;
1296         if ( op->o_callback ) {
1297                 int             first = 1;
1298                 slap_callback   *sc = op->o_callback,
1299                                 *sc_next = op->o_callback;
1300
1301                 rc = SLAP_CB_CONTINUE;
1302                 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1303                         sc_next = op->o_callback->sc_next;
1304                         if ( op->o_callback->sc_response ) {
1305                                 rc = op->o_callback->sc_response( op, rs );
1306                                 if ( first && op->o_callback == NULL ) {
1307                                         sc = NULL;
1308                                 }
1309                                 if ( rc != SLAP_CB_CONTINUE ) break;
1310                         }
1311                         first = 0;
1312                 }
1313
1314                 op->o_callback = sc;
1315                 if ( rc != SLAP_CB_CONTINUE ) goto rel;
1316         }
1317
1318         Debug( LDAP_DEBUG_TRACE,
1319                 "=> send_search_reference: dn=\"%s\"\n",
1320                 rs->sr_entry ? rs->sr_entry->e_name.bv_val : "(null)", 0, 0 );
1321
1322         if (  rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1323                 ad_entry, NULL, ACL_READ, NULL ) )
1324         {
1325                 Debug( LDAP_DEBUG_ACL,
1326                         "send_search_reference: access to entry not allowed\n",
1327                     0, 0, 0 );
1328                 rc = 1;
1329                 goto rel;
1330         }
1331
1332         if ( rs->sr_entry && ! access_allowed( op, rs->sr_entry,
1333                 ad_ref, NULL, ACL_READ, NULL ) )
1334         {
1335                 Debug( LDAP_DEBUG_ACL,
1336                         "send_search_reference: access "
1337                         "to reference not allowed\n",
1338                     0, 0, 0 );
1339                 rc = 1;
1340                 goto rel;
1341         }
1342
1343 #ifdef LDAP_CONTROL_X_DOMAIN_SCOPE
1344         if( op->o_domain_scope ) {
1345                 Debug( LDAP_DEBUG_ANY,
1346                         "send_search_reference: domainScope control in (%s)\n", 
1347                         rs->sr_entry->e_dn, 0, 0 );
1348                 rc = 0;
1349                 goto rel;
1350         }
1351 #endif
1352
1353         if( rs->sr_ref == NULL ) {
1354                 Debug( LDAP_DEBUG_ANY,
1355                         "send_search_reference: null ref in (%s)\n", 
1356                         rs->sr_entry ? rs->sr_entry->e_dn : "(null)", 0, 0 );
1357                 rc = 1;
1358                 goto rel;
1359         }
1360
1361         if( op->o_protocol < LDAP_VERSION3 ) {
1362                 rc = 0;
1363                 /* save the references for the result */
1364                 if( rs->sr_ref[0].bv_val != NULL ) {
1365                         if( value_add( &rs->sr_v2ref, rs->sr_ref ) )
1366                                 rc = LDAP_OTHER;
1367                 }
1368                 goto rel;
1369         }
1370
1371 #ifdef LDAP_CONNECTIONLESS
1372         if( op->o_conn && op->o_conn->c_is_udp ) {
1373                 ber = op->o_res_ber;
1374         } else
1375 #endif
1376         {
1377                 ber_init_w_nullc( ber, LBER_USE_DER );
1378                 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1379         }
1380
1381         rc = ber_printf( ber, "{it{W}" /*"}"*/ , op->o_msgid,
1382                 LDAP_RES_SEARCH_REFERENCE, rs->sr_ref );
1383
1384         if( rc != -1 ) {
1385                 rc = send_ldap_controls( op, ber, rs->sr_ctrls );
1386         }
1387
1388         if( rc != -1 ) {
1389                 rc = ber_printf( ber, /*"{"*/ "N}" );
1390         }
1391
1392         if ( rc == -1 ) {
1393                 Debug( LDAP_DEBUG_ANY,
1394                         "send_search_reference: ber_printf failed\n", 0, 0, 0 );
1395
1396 #ifdef LDAP_CONNECTIONLESS
1397                 if (!op->o_conn || op->o_conn->c_is_udp == 0)
1398 #endif
1399                 ber_free_buf( ber );
1400                 send_ldap_error( op, rs, LDAP_OTHER, "encode DN error" );
1401                 goto rel;
1402         }
1403
1404         rc = 0;
1405         if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
1406                 be_entry_release_rw( op, rs->sr_entry, 0 );
1407                 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
1408                 rs->sr_entry = NULL;
1409         }
1410
1411 #ifdef LDAP_CONNECTIONLESS
1412         if (!op->o_conn || op->o_conn->c_is_udp == 0) {
1413 #endif
1414         bytes = send_ldap_ber( op->o_conn, ber );
1415         ber_free_buf( ber );
1416
1417         ldap_pvt_thread_mutex_lock( &slap_counters.sc_sent_mutex );
1418         ldap_pvt_mp_add_ulong( slap_counters.sc_bytes, (unsigned long)bytes );
1419         ldap_pvt_mp_add_ulong( slap_counters.sc_refs, 1 );
1420         ldap_pvt_mp_add_ulong( slap_counters.sc_pdu, 1 );
1421         ldap_pvt_thread_mutex_unlock( &slap_counters.sc_sent_mutex );
1422 #ifdef LDAP_CONNECTIONLESS
1423         }
1424 #endif
1425
1426         Statslog( LDAP_DEBUG_STATS2, "%s REF dn=\"%s\"\n",
1427                 op->o_log_prefix, rs->sr_entry ? rs->sr_entry->e_dn : "(null)",
1428                 0, 0, 0 );
1429
1430         Debug( LDAP_DEBUG_TRACE, "<= send_search_reference\n", 0, 0, 0 );
1431
1432 rel:
1433         if ( op->o_callback ) {
1434                 int             first = 1;
1435                 slap_callback   *sc = op->o_callback, *sc_next;
1436
1437                 for ( sc_next = op->o_callback; sc_next; op->o_callback = sc_next) {
1438                         sc_next = op->o_callback->sc_next;
1439                         if ( op->o_callback->sc_cleanup ) {
1440                                 (void)op->o_callback->sc_cleanup( op, rs );
1441                                 if ( first && op->o_callback != sc ) {
1442                                         sc = op->o_callback;
1443                                 }
1444                         }
1445                         first = 0;
1446                 }
1447                 op->o_callback = sc;
1448         }
1449
1450         return rc;
1451 }
1452
1453 int
1454 str2result(
1455     char        *s,
1456     int         *code,
1457     char        **matched,
1458     char        **info )
1459 {
1460         int     rc;
1461         char    *c;
1462
1463         *code = LDAP_SUCCESS;
1464         *matched = NULL;
1465         *info = NULL;
1466
1467         if ( strncasecmp( s, "RESULT", STRLENOF( "RESULT" ) ) != 0 ) {
1468                 Debug( LDAP_DEBUG_ANY, "str2result (%s) expecting \"RESULT\"\n",
1469                     s, 0, 0 );
1470
1471                 return( -1 );
1472         }
1473
1474         rc = 0;
1475         while ( (s = strchr( s, '\n' )) != NULL ) {
1476                 *s++ = '\0';
1477                 if ( *s == '\0' ) {
1478                         break;
1479                 }
1480                 if ( (c = strchr( s, ':' )) != NULL ) {
1481                         c++;
1482                 }
1483
1484                 if ( strncasecmp( s, "code", STRLENOF( "code" ) ) == 0 ) {
1485                         if ( c != NULL ) {
1486                                 *code = atoi( c );
1487                         }
1488                 } else if ( strncasecmp( s, "matched", STRLENOF( "matched" ) ) == 0 ) {
1489                         if ( c != NULL ) {
1490                                 *matched = c;
1491                         }
1492                 } else if ( strncasecmp( s, "info", STRLENOF( "info" ) ) == 0 ) {
1493                         if ( c != NULL ) {
1494                                 *info = c;
1495                         }
1496                 } else {
1497                         Debug( LDAP_DEBUG_ANY, "str2result (%s) unknown\n",
1498                             s, 0, 0 );
1499
1500                         rc = -1;
1501                 }
1502         }
1503
1504         return( rc );
1505 }
1506
1507 int slap_read_controls(
1508         Operation *op,
1509         SlapReply *rs,
1510         Entry *e,
1511         const struct berval *oid,
1512         LDAPControl **ctrl )
1513 {
1514         int rc;
1515         struct berval bv;
1516         BerElementBuffer berbuf;
1517         BerElement *ber = (BerElement *) &berbuf;
1518         LDAPControl c;
1519         Operation myop;
1520
1521         Debug( LDAP_DEBUG_ANY, "slap_read_controls: (%s) %s\n",
1522                 oid->bv_val, e->e_dn, 0 );
1523
1524         rs->sr_entry = e;
1525         rs->sr_attrs = ( oid == &slap_pre_read_bv ) ?
1526                 op->o_preread_attrs : op->o_postread_attrs; 
1527
1528         bv.bv_len = entry_flatsize( rs->sr_entry, 0 );
1529         bv.bv_val = op->o_tmpalloc(bv.bv_len, op->o_tmpmemctx );
1530
1531         ber_init2( ber, &bv, LBER_USE_DER );
1532         ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
1533
1534         /* create new operation */
1535         myop = *op;
1536         myop.o_bd = NULL;
1537         myop.o_res_ber = ber;
1538
1539         rc = slap_send_search_entry( &myop, rs );
1540         if( rc ) return rc;
1541
1542         rc = ber_flatten2( ber, &c.ldctl_value, 0 );
1543
1544         if( rc == LBER_ERROR ) return LDAP_OTHER;
1545
1546         c.ldctl_oid = oid->bv_val;
1547         c.ldctl_iscritical = 0;
1548
1549         if ( ctrl == NULL ) {
1550                 /* first try */
1551                 *ctrl = (LDAPControl *) slap_sl_calloc( 1, sizeof(LDAPControl), NULL );
1552         } else {
1553                 /* retry: free previous try */
1554                 slap_sl_free( (*ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
1555         }
1556
1557         **ctrl = c;
1558         return LDAP_SUCCESS;
1559 }
1560
1561 /* Map API errors to protocol errors... */
1562 int
1563 slap_map_api2result( SlapReply *rs )
1564 {
1565         switch(rs->sr_err) {
1566         case LDAP_SERVER_DOWN:
1567                 return LDAP_UNAVAILABLE;
1568         case LDAP_LOCAL_ERROR:
1569                 return LDAP_OTHER;
1570         case LDAP_ENCODING_ERROR:
1571         case LDAP_DECODING_ERROR:
1572                 return LDAP_PROTOCOL_ERROR;
1573         case LDAP_TIMEOUT:
1574                 return LDAP_UNAVAILABLE;
1575         case LDAP_AUTH_UNKNOWN:
1576                 return LDAP_AUTH_METHOD_NOT_SUPPORTED;
1577         case LDAP_FILTER_ERROR:
1578                 rs->sr_text = "Filter error";
1579                 return LDAP_OTHER;
1580         case LDAP_USER_CANCELLED:
1581                 rs->sr_text = "User cancelled";
1582                 return LDAP_OTHER;
1583         case LDAP_PARAM_ERROR:
1584                 return LDAP_PROTOCOL_ERROR;
1585         case LDAP_NO_MEMORY:
1586                 return LDAP_OTHER;
1587         case LDAP_CONNECT_ERROR:
1588                 return LDAP_UNAVAILABLE;
1589         case LDAP_NOT_SUPPORTED:
1590                 return LDAP_UNWILLING_TO_PERFORM;
1591         case LDAP_CONTROL_NOT_FOUND:
1592                 return LDAP_PROTOCOL_ERROR;
1593         case LDAP_NO_RESULTS_RETURNED:
1594                 return LDAP_NO_SUCH_OBJECT;
1595         case LDAP_MORE_RESULTS_TO_RETURN:
1596                 rs->sr_text = "More results to return";
1597                 return LDAP_OTHER;
1598         case LDAP_CLIENT_LOOP:
1599         case LDAP_REFERRAL_LIMIT_EXCEEDED:
1600                 return LDAP_LOOP_DETECT;
1601         default:
1602                 if ( LDAP_API_ERROR(rs->sr_err) ) return LDAP_OTHER;
1603                 return rs->sr_err;
1604         }
1605 }
1606
1607
1608 slap_mask_t
1609 slap_attr_flags( AttributeName *an )
1610 {
1611         slap_mask_t     flags = SLAP_ATTRS_UNDEFINED;
1612
1613         if ( an == NULL ) {
1614                 flags |= ( SLAP_OPATTRS_NO | SLAP_USERATTRS_YES );
1615
1616         } else {
1617                 flags |= an_find( an, &AllOper )
1618                         ? SLAP_OPATTRS_YES : SLAP_OPATTRS_NO;
1619                 flags |= an_find( an, &AllUser )
1620                         ? SLAP_USERATTRS_YES : SLAP_USERATTRS_NO;
1621         }
1622
1623         return flags;
1624 }
1625